forked from p4lang/behavioral-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.cpp
343 lines (298 loc) · 8.95 KB
/
headers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Antonin Bas (antonin@barefootnetworks.com)
*
*/
#include <bm/bm_sim/headers.h>
#include <bm/bm_sim/header_unions.h>
#include <bm/bm_sim/phv.h>
#include <bm/bm_sim/expressions.h>
#include <algorithm> // for std::swap
#include <string>
#include <set>
#include <map>
#include <vector>
namespace bm {
namespace {
// hidden fields in the map needs to be ordered just like the enum, which may
// not be very robust code...
class HiddenFMap {
public:
// TODO(antonin): find something more elegant?
static const std::map<HeaderType::HiddenF, HeaderType::FInfo> &map();
static size_t size();
private:
static HiddenFMap *get_instance();
HiddenFMap();
std::map<HeaderType::HiddenF, HeaderType::FInfo> fmap;
};
HiddenFMap::HiddenFMap() {
fmap = {
{HeaderType::HiddenF::VALID, {"$valid$", 1, false, false, false, true}},
};
}
HiddenFMap *
HiddenFMap::get_instance() {
static HiddenFMap instance;
return &instance;
}
const std::map<HeaderType::HiddenF, HeaderType::FInfo> &
HiddenFMap::map() {
auto instance = get_instance();
return instance->fmap;
}
size_t
HiddenFMap::size() {
return map().size();
}
} // namespace
HeaderType::HeaderType(const std::string &name, p4object_id_t id)
: NamedP4Object(name, id) {
const auto &fmap = HiddenFMap::map();
for (auto p : fmap) fields_info.push_back(p.second);
}
int
HeaderType::push_back_field(const std::string &field_name, int field_bit_width,
bool is_signed, bool is_saturating, bool is_VL) {
auto pos = fields_info.end() - HiddenFMap::size();
auto offset = std::distance(fields_info.begin(), pos);
fields_info.insert(
pos,
{field_name, field_bit_width, is_signed, is_saturating, is_VL, false});
return offset;
}
int
HeaderType::push_back_VL_field(
const std::string &field_name,
int max_header_bytes,
std::unique_ptr<VLHeaderExpression> field_length_expr,
bool is_signed,
bool is_saturating) {
auto offset = push_back_field(field_name, 0, is_signed, is_saturating, true);
// TODO(antonin)
assert(!is_VL_header() && "header can only have one VL field");
VL_expr_raw = std::move(field_length_expr);
VL_offset = offset;
VL_max_header_bytes = max_header_bytes;
return offset;
}
bool
HeaderType::has_VL_expr() const {
return (VL_expr_raw != nullptr);
}
std::unique_ptr<ArithExpression>
HeaderType::resolve_VL_expr(header_id_t header_id) const {
if (!is_VL_header()) return nullptr;
// expression will be provided by extract, so nothing to do
if (VL_expr_raw == nullptr) return nullptr;
std::unique_ptr<ArithExpression> expr(new ArithExpression());
*expr = VL_expr_raw->resolve(header_id);
return expr;
}
const std::vector<int> &
HeaderType::get_VL_input_offsets() const {
return VL_expr_raw->get_input_offsets();
}
int
HeaderType::get_VL_max_header_bytes() const {
return VL_max_header_bytes;
}
Header::Header(const std::string &name, p4object_id_t id,
const HeaderType &header_type,
const std::set<int> &arith_offsets,
const bool metadata)
: NamedP4Object(name, id), header_type(header_type), metadata(metadata) {
// header_type_id = header_type.get_type_id();
for (int i = 0; i < header_type.get_num_fields(); i++) {
const auto &finfo = header_type.get_finfo(i);
bool arith_flag = true;
if (arith_offsets.find(i) == arith_offsets.end()) {
arith_flag = false;
}
fields.emplace_back(finfo.bitwidth, this, arith_flag, finfo.is_signed,
finfo.is_hidden, finfo.is_VL, finfo.is_saturating);
uint64_t field_unique_id = id;
field_unique_id <<= 32;
field_unique_id |= i;
fields.back().set_id(field_unique_id);
if (!finfo.is_hidden) nbytes_packet += fields.back().get_nbits();
}
assert(nbytes_packet % 8 == 0);
nbytes_packet /= 8;
valid_field = &fields.at(header_type.get_hidden_offset(
HeaderType::HiddenF::VALID));
for (int i = 0; i < header_type.get_num_fields(); i++) {
const auto &finfo = header_type.get_finfo(i);
if (finfo.is_VL) {
fields.at(i).reserve_VL(
header_type.get_VL_max_header_bytes() - nbytes_packet);
break;
}
}
}
int
Header::recompute_nbytes_packet() {
nbytes_packet = 0;
for (const auto &f : fields) {
if (f.is_hidden()) break; // all hidden fields are at the end
nbytes_packet += f.get_nbits();
}
assert(nbytes_packet % 8 == 0);
nbytes_packet /= 8;
return nbytes_packet;
}
void
Header::mark_valid() {
valid = true;
valid_field->set(1);
if (union_membership) union_membership->make_valid();
}
void
Header::mark_invalid() {
valid = false;
valid_field->set(0);
if (union_membership) union_membership->make_invalid();
}
void
Header::reset() {
for (Field &f : fields)
f.set(0);
}
void
Header::reset_VL_header() {
if (!is_VL_header()) return;
int VL_offset = header_type.get_VL_offset();
auto &VL_f = fields[VL_offset];
// this works because we only support VL fields whose bitwidth is a multiple
// of 8
nbytes_packet -= VL_f.get_nbytes();
VL_f.reset_VL();
}
void
Header::set_written_to(bool written_to_value) {
for (Field &f : fields)
f.set_written_to(written_to_value);
}
void
Header::extract(const char *data, const PHV &phv) {
if (is_VL_header()) return extract_VL(data, phv);
int hdr_offset = 0;
for (Field &f : fields) {
if (f.is_hidden()) break; // all hidden fields are at the end
hdr_offset += f.extract(data, hdr_offset);
data += hdr_offset / 8;
hdr_offset = hdr_offset % 8;
}
mark_valid();
}
template <typename Fn>
void
Header::extract_VL_common(const char *data, const Fn &VL_fn) {
int VL_offset = header_type.get_VL_offset();
int hdr_offset = 0;
nbytes_packet = 0;
for (int i = 0; i < header_type.get_num_fields(); i++) {
Field &f = fields[i];
if (f.is_hidden()) break; // all hidden fields are at the end
if (VL_offset == i) {
hdr_offset += f.extract_VL(data, hdr_offset, VL_fn());
} else {
hdr_offset += f.extract(data, hdr_offset);
}
data += hdr_offset / 8;
hdr_offset = hdr_offset % 8;
nbytes_packet += f.get_nbits();
}
assert(nbytes_packet % 8 == 0);
nbytes_packet /= 8;
mark_valid();
}
void
Header::extract_VL(const char *data, int VL_nbits) {
extract_VL_common(data, [VL_nbits]() { return VL_nbits; });
}
void
Header::extract_VL(const char *data, const PHV &phv) {
static thread_local Data computed_nbits;
auto VL_fn = [&phv, this]() {
VL_expr->eval(phv, &computed_nbits);
return computed_nbits.get<int>();
};
extract_VL_common(data, VL_fn);
}
void
Header::deparse(char *data) const {
// TODO(antonin): special case for VL header ?
int hdr_offset = 0;
for (const Field &f : fields) {
if (f.is_hidden()) break; // all hidden fields are at the end
hdr_offset += f.deparse(data, hdr_offset);
data += hdr_offset / 8;
hdr_offset = hdr_offset % 8;
}
}
#ifdef BMDEBUG_ON
void
Header::set_packet_id(const Debugger::PacketId *id) {
for (Field &f : fields) f.set_packet_id(id);
}
#endif
const std::string &
Header::get_field_name(int field_offset) const {
return header_type.get_field_name(field_offset);
}
const std::string
Header::get_field_full_name(int field_offset) const {
return name + "." + get_field_name(field_offset);
}
void
Header::swap_values(Header *other) {
std::swap(valid, other->valid);
// cannot do that, would invalidate references
// std::swap(fields, other.fields);
for (size_t i = 0; i < fields.size(); i++)
fields[i].swap_values(&other->fields[i]);
// in case header has a VL field
std::swap(nbytes_packet, other->nbytes_packet);
}
void
Header::copy_fields(const Header &src) {
for (size_t f = 0; f < fields.size(); f++)
fields[f].copy_value(src.fields[f]);
// in case header has a VL field
nbytes_packet = src.nbytes_packet;
}
bool
Header::cmp(const Header &other) const {
return (header_type.get_type_id() == other.header_type.get_type_id()) &&
is_valid() && other.is_valid() &&
(fields == other.fields);
}
Header::UnionMembership::UnionMembership(HeaderUnion *header_union, size_t idx)
: header_union(header_union), idx(idx) { }
void
Header::UnionMembership::make_valid() {
header_union->make_header_valid(idx);
}
void
Header::UnionMembership::make_invalid() {
header_union->make_header_invalid(idx);
}
void
Header::set_union_membership(HeaderUnion *header_union, size_t idx) {
union_membership.reset(new UnionMembership(header_union, idx));
}
} // namespace bm