-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_11.p4
459 lines (374 loc) · 10.9 KB
/
task_11.p4
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* 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.
*/
#include "includes/headers.p4"
#include "includes/parser.p4"
#define BITMATRIX_SIZE 16384
field_list ipv4_checksum_list {
ipv4.version;
ipv4.ihl;
ipv4.diffserv;
ipv4.totalLen;
ipv4.id;
ipv4.flags;
ipv4.fragOffset;
ipv4.ttl;
ipv4.protocol;
ipv4.srcAddr;
ipv4.dstAddr;
}
field_list_calculation ipv4_checksum {
input {
ipv4_checksum_list;
}
algorithm : csum16;
output_width : 16;
}
calculated_field ipv4.hdrChecksum {
update ipv4_checksum if (ipv4.ihl == 5);
}
action _drop() {
drop();
}
header_type routing_metadata_t {
fields {
nhop_ipv4 : 32;
nhop_add : 48;
}
}
metadata routing_metadata_t routing_metadata;
////////////////////////////////////////////////////////////////////////
// bitmatrix definitions
header_type custom_metadata_t {
fields {
bitmatrix_idx : 16;
bitmatrix_flag : 4;
bitmatrix_ptr : 1;
bitmatrix_tenant : 4;
bitmatrix_value : 20;
}
}
metadata custom_metadata_t custom_metadata;
field_list hash_fields {
ipv4.version; // 4
ipv4.ihl; // 4
ipv4.totalLen; //16
ipv4.id; //16
ipv4.flags; // 3
ipv4.fragOffset; //13
ipv4.protocol; // 8
ipv4.srcAddr; //32
ipv4.dstAddr; //32
payld.userdata8B; //64
}
field_list_calculation hash {
input {
hash_fields;
}
algorithm : crc16;
output_width : 16;
}
register bitmatrix_0{
width : 4;
instance_count : BITMATRIX_SIZE;
}
register counter_array_0A{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register counter_array_0B{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register counter_array_0C{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register counter_array_0D{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register bitmatrix_1{
width : 4;
instance_count : BITMATRIX_SIZE;
}
register counter_array_1A{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register counter_array_1B{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register counter_array_1C{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register counter_array_1D{
width : 20;
instance_count : BITMATRIX_SIZE;
}
register bitmatrix_pointer{
width : 1;
instance_count : 1;
}
////////////////////////////////////////////////////////////////////////
// counters
counter pckt_counter {
type: packets_and_bytes;
static: count_table;
instance_count: 1;
}
action count_action() {
count(pckt_counter, 0);
register_read(custom_metadata.bitmatrix_ptr, bitmatrix_pointer, 0);
}
table count_table {
actions {
count_action;
}
size : 1;
}
counter pckt_counter_0 {
type: packets_and_bytes;
direct : set_bitmatrix_0_table;
}
counter pckt_counter_1 {
type: packets_and_bytes;
direct : set_bitmatrix_1_table;
}
////////////////////////////////////////////////////////////////////////
// bitmatrix_0
action set_bitmatrix_0(tenant_flag) {
modify_field_with_hash_based_offset(custom_metadata.bitmatrix_idx, 0, hash, BITMATRIX_SIZE - 1);
register_read(custom_metadata.bitmatrix_flag, bitmatrix_0, custom_metadata.bitmatrix_idx);
bit_or(custom_metadata.bitmatrix_flag, custom_metadata.bitmatrix_flag, tenant_flag);
register_write(bitmatrix_0, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_flag);
modify_field(custom_metadata.bitmatrix_tenant, tenant_flag);
}
table set_bitmatrix_0_table {
reads {
ipv4.srcAddr : lpm;
}
actions {
set_bitmatrix_0;
_drop;
}
size: 32;
}
////////////////////////////////////////////////////////////////////////
// bitmatrix_1
action set_bitmatrix_1(tenant_flag) {
modify_field_with_hash_based_offset(custom_metadata.bitmatrix_idx, 0, hash, BITMATRIX_SIZE - 1);
register_read(custom_metadata.bitmatrix_flag, bitmatrix_1, custom_metadata.bitmatrix_idx);
bit_or(custom_metadata.bitmatrix_flag, custom_metadata.bitmatrix_flag, tenant_flag);
register_write(bitmatrix_1, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_flag);
modify_field(custom_metadata.bitmatrix_tenant, tenant_flag);
}
table set_bitmatrix_1_table {
reads {
ipv4.srcAddr : lpm;
}
actions {
set_bitmatrix_1;
_drop;
}
size: 32;
}
////////////////////////////////////////////////////////////////////////
// counter arrays 0
action set_counter_array_0A() {
register_read(custom_metadata.bitmatrix_value, counter_array_0A, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_0A, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_0A_table {
actions {
set_counter_array_0A;
}
size : 1;
}
action set_counter_array_0B() {
register_read(custom_metadata.bitmatrix_value, counter_array_0B, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_0B, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_0B_table {
actions {
set_counter_array_0B;
}
size : 1;
}
action set_counter_array_0C() {
register_read(custom_metadata.bitmatrix_value, counter_array_0C, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_0C, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_0C_table {
actions {
set_counter_array_0C;
}
size : 1;
}
action set_counter_array_0D() {
register_read(custom_metadata.bitmatrix_value, counter_array_0D, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_0D, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_0D_table {
actions {
set_counter_array_0D;
}
size : 1;
}
////////////////////////////////////////////////////////////////////////
// counter arrays 1
action set_counter_array_1A() {
register_read(custom_metadata.bitmatrix_value, counter_array_1A, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_1A, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_1A_table {
actions {
set_counter_array_1A;
}
size : 1;
}
action set_counter_array_1B() {
register_read(custom_metadata.bitmatrix_value, counter_array_1B, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_1B, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_1B_table {
actions {
set_counter_array_1B;
}
size : 1;
}
action set_counter_array_1C() {
register_read(custom_metadata.bitmatrix_value, counter_array_1C, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_1C, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_1C_table {
actions {
set_counter_array_1C;
}
size : 1;
}
action set_counter_array_1D() {
register_read(custom_metadata.bitmatrix_value, counter_array_1D, custom_metadata.bitmatrix_idx);
add_to_field(custom_metadata.bitmatrix_value, ipv4.totalLen);
register_write(counter_array_1D, custom_metadata.bitmatrix_idx, custom_metadata.bitmatrix_value);
}
table set_counter_array_1D_table {
actions {
set_counter_array_1D;
}
size : 1;
}
////////////////////////////////////////////////////////////////////////
// forwarding
action set_nhop(nhop_ipv4, port) {
modify_field(routing_metadata.nhop_ipv4, nhop_ipv4);
modify_field(standard_metadata.egress_spec, port);
modify_field(ipv4.ttl, ipv4.ttl - 1);
}
table ipv4_lpm {
reads {
ipv4.dstAddr : lpm;
}
actions {
set_nhop;
_drop;
}
size: 1024;
}
////////////////////////////////////////////////////////////////////////
action set_dmac(dmac) {
modify_field(eth.dstAddr, dmac);
}
table forward {
reads {
routing_metadata.nhop_ipv4 : exact;
}
actions {
set_dmac;
_drop;
}
size: 512;
}
////////////////////////////////////////////////////////////////////////
action rewrite_mac(smac) {
modify_field(eth.srcAddr, smac);
}
table send_frame {
reads {
standard_metadata.egress_port: exact;
}
actions {
rewrite_mac;
_drop;
}
size: 256;
}
////////////////////////////////////////////////////////////////////////
control ingress {
if(valid(ipv4) and ipv4.ttl > 0) {
apply(count_table);
if (custom_metadata.bitmatrix_ptr == 0) {
apply(set_bitmatrix_0_table);
if (custom_metadata.bitmatrix_tenant == 1) {
apply(set_counter_array_0A_table);
}
else {
if (custom_metadata.bitmatrix_tenant == 2) {
apply(set_counter_array_0B_table);
}
else {
if (custom_metadata.bitmatrix_tenant == 4) {
apply(set_counter_array_0C_table);
}
else {
apply(set_counter_array_0D_table);
}
}
}
}
if (custom_metadata.bitmatrix_ptr == 1) {
apply(set_bitmatrix_1_table);
if (custom_metadata.bitmatrix_tenant == 1) {
apply(set_counter_array_1A_table);
}
else {
if (custom_metadata.bitmatrix_tenant == 2) {
apply(set_counter_array_1B_table);
}
else {
if (custom_metadata.bitmatrix_tenant == 4) {
apply(set_counter_array_1C_table);
}
else {
apply(set_counter_array_1D_table);
}
}
}
}
apply(ipv4_lpm);
apply(forward);
}
}
control egress {
apply(send_frame);
}