-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp-test8.p4
454 lines (416 loc) · 12.5 KB
/
tcp-test8.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
/*
* Copyright 2021 Universiteit van Amsterdam and Netherlands eScience Center
*
* Modified from the original at https://github.com/p4lang/p4c/blob/68b65262ca074b527e973efc888d94e877418bdf/testdata/p4_16_samples/checksum-tcp-bmv2.p4
*
* Copyright 2020 MNK Labs & Consulting, LLC.
*
* https://mnkcg.com
*
* 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.
*
* Jointly developed with Andy Fingerhut (ex-Cisco and Barefoot/Intel).
*
* This program does not support IPv6. However, IPv6 also uses
* a pseudo header for upper-layer protocols (e.g., TCP and UDP).
* More details for IPv6 checksum is available at the link below.
*
* https://tools.ietf.org/html/rfc8200#page-27
*
* The program does not support UDP options because none have been specified
* in a RFC.
*/
#include <core.p4>
#include <v1model.p4>
typedef bit<48> EthernetAddress;
typedef bit<32> IPv4Address;
header ethernet_t {
bit<48> dstAddr;
bit<48> srcAddr;
bit<16> etherType;
}
// IPv4 header _with_ options
header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
IPv4Address srcAddr;
IPv4Address dstAddr;
varbit<320> options;
}
header tcp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seqNo;
bit<32> ackNo;
bit<4> dataOffset;
bit<3> res;
bit<3> ecn;
bit<6> ctrl;
bit<16> window;
bit<16> checksum; // Includes Pseudo Hdr + TCP segment (hdr + payload)
bit<16> urgentPtr;
varbit<320> options;
}
header udp_t {
bit<16> srcPort;
bit<16> dstPort;
bit<16> length_;
bit<16> checksum;
}
header IPv4_up_to_ihl_only_h {
bit<4> version;
bit<4> ihl;
}
header tcp_upto_data_offset_only_h {
bit<16> srcPort;
bit<16> dstPort;
bit<32> seqNo;
bit<32> ackNo;
// dataOffset in TCP hdr uses 4 bits but needs padding.
// If 4 bits are used for it, p4c-bm2-ss complains the header
// is not a multiple of 8 bits.
bit<4> dataOffset;
bit<4> dontCare;
}
struct headers {
ethernet_t ethernet;
ipv4_t ipv4;
tcp_t tcp;
udp_t udp;
}
struct mystruct1_t {
bit<4> a;
bit<4> b;
}
struct metadata {
mystruct1_t mystruct1;
bit<16> l4Len; // includes TCP hdr len + TCP payload len in bytes.
}
typedef tuple<
bit<4>,
bit<4>,
bit<8>,
varbit<56>
> myTuple1;
// Declare user-defined errors that may be signaled during parsing
error {
IPv4HeaderTooShort,
TCPHeaderTooShort,
IPv4IncorrectVersion,
IPv4ChecksumError
}
parser parserI(packet_in pkt,
out headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta)
{
state start {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
0x0800: parse_ipv4;
default: accept;
}
}
state parse_ipv4 {
// The 4-bit IHL field of the IPv4 base header is the number
// of 32-bit words in the entire IPv4 header. It is an error
// for it to be less than 5. There are only IPv4 options
// present if the value is at least 6. The length of the IPv4
// options alone, without the 20-byte base header, is thus ((4
// * ihl) - 20) bytes, or 8 times that many bits.
pkt.extract(hdr.ipv4,
(bit<32>)
(8 *
(4 * (bit<9>) (pkt.lookahead<IPv4_up_to_ihl_only_h>().ihl)
- 20)));
verify(hdr.ipv4.version == 4w4, error.IPv4IncorrectVersion);
verify(hdr.ipv4.ihl >= 4w5, error.IPv4HeaderTooShort);
meta.l4Len = hdr.ipv4.totalLen - (bit<16>)(hdr.ipv4.ihl)*4;
transition select (hdr.ipv4.protocol) {
6: parse_tcp;
17: parse_udp;
default: accept;
}
}
state parse_tcp {
// The 4-bit dataOffset field of the TCP base header is the number
// of 32-bit words in the entire TCP header. It is an error
// for it to be less than 5. There are only TCP options
// present if the value is at least 6. The length of the TCP
// options alone, without the 20-byte base header, is thus ((4
// * dataOffset) - 20) bytes, or 8 times that many bits.
pkt.extract(hdr.tcp,
(bit<32>)
(8 *
(4 * (bit<9>) (pkt.lookahead<tcp_upto_data_offset_only_h>().dataOffset)
- 20)));
verify(hdr.tcp.dataOffset >= 4w5, error.TCPHeaderTooShort);
transition accept;
}
state parse_udp {
pkt.extract(hdr.udp);
transition accept;
}
}
control cIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta)
{
action client_send(bit<48> src_mac, bit<48> dst_mac, bit<32> src_ip, bit<9> port){
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.srcAddr=src_ip;
stdmeta.egress_spec = port;
}
action server_recieve(bit<48> src_mac, bit<48> dst_mac, bit<32> dst_ip, bit<9> port){
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.dstAddr=dst_ip;
stdmeta.egress_spec = port;
}
action server_send(bit<48> src_mac, bit<48> dst_mac, bit<32> src_ip, bit<9> port){
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.srcAddr=src_ip;
stdmeta.egress_spec = port;
}
action client_recieve(bit<48> src_mac, bit<48> dst_mac, bit<32> dst_ip, bit<9> port){
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.dstAddr=dst_ip;
stdmeta.egress_spec = port;
}
action foot() {
hdr.tcp.srcPort = hdr.tcp.srcPort + 1;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
hdr.ipv4.dstAddr = hdr.ipv4.dstAddr + 4;
}
action foou() {
hdr.udp.srcPort = hdr.udp.srcPort + 1;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
hdr.ipv4.dstAddr = hdr.ipv4.dstAddr + 4;
}
action to_port_action(bit<48> dst_mac,bit<32> dst_ip, bit<32> src_ip, bit<9> port){
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.dstAddr=dst_ip;
hdr.ipv4.srcAddr=src_ip;
stdmeta.egress_spec = port;
}
action send_to_nat(bit<48> src_mac,bit<48> dst_mac,bit<32> src_ip, bit<32> dst_ip, bit<9> port){
hdr.ethernet.srcAddr=src_mac;
hdr.ethernet.dstAddr=dst_mac;
hdr.ipv4.dstAddr=dst_ip;
hdr.ipv4.srcAddr=src_ip;
stdmeta.egress_spec = port;
}
table client_send_t {
key = {
hdr.tcp.dstPort :exact;
}
actions = {client_send;NoAction; }
default_action = NoAction;
}
table server_recieve_t {
key = {
hdr.tcp.dstPort :exact;
hdr.ipv4.srcAddr: exact;
}
actions = {server_recieve;NoAction; }
default_action = NoAction;
}
table server_send_t {
key = {
hdr.tcp.srcPort :exact;
}
actions = {server_send;NoAction; }
default_action = NoAction;
}
table client_recieve_t {
key = {
hdr.tcp.srcPort :exact;
hdr.ipv4.srcAddr: exact;
}
actions = {client_recieve;NoAction; }
default_action = NoAction;
}
table ipv4_match {
key = {
hdr.ipv4.dstAddr : lpm;
}
actions = {to_port_action;NoAction; }
default_action = NoAction;
}
table ipv4_src_match {
key = {
hdr.ipv4.srcAddr : exact;
}
actions = {to_port_action;NoAction; }
default_action = NoAction;
}
table guh {
key = {
hdr.tcp.dstPort : exact;
}
actions = { foot; NoAction; }
const default_action = NoAction;
const entries = {
(16w80) : foot();
}
}
table huh {
key = {
hdr.udp.dstPort : exact;
}
actions = { foou; NoAction; }
const default_action = NoAction;
const entries = {
(16w80) : foou();
}
}
apply {
server_recieve_t.apply();
}
}
control cEgress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t stdmeta)
{
apply {
}
}
control vc(inout headers hdr,
inout metadata meta)
{
apply {
verify_checksum(true,
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
hdr.ipv4.options
},
hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
verify_checksum_with_payload(hdr.tcp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.tcp.srcPort,
hdr.tcp.dstPort,
hdr.tcp.seqNo,
hdr.tcp.ackNo,
hdr.tcp.dataOffset,
hdr.tcp.res,
hdr.tcp.ecn,
hdr.tcp.ctrl,
hdr.tcp.window,
hdr.tcp.urgentPtr,
hdr.tcp.options
},
hdr.tcp.checksum, HashAlgorithm.csum16);
verify_checksum_with_payload(hdr.udp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.udp.srcPort,
hdr.udp.dstPort,
hdr.udp.length_
},
hdr.udp.checksum, HashAlgorithm.csum16);
}
}
control uc(inout headers hdr,
inout metadata meta)
{
apply {
update_checksum(true,
{ hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.totalLen,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.fragOffset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
hdr.ipv4.options
},
hdr.ipv4.hdrChecksum, HashAlgorithm.csum16);
update_checksum_with_payload(hdr.tcp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.tcp.srcPort,
hdr.tcp.dstPort,
hdr.tcp.seqNo,
hdr.tcp.ackNo,
hdr.tcp.dataOffset,
hdr.tcp.res,
hdr.tcp.ecn,
hdr.tcp.ctrl,
hdr.tcp.window,
hdr.tcp.urgentPtr,
hdr.tcp.options
},
hdr.tcp.checksum, HashAlgorithm.csum16);
update_checksum_with_payload(hdr.udp.isValid(),
{ hdr.ipv4.srcAddr,
hdr.ipv4.dstAddr,
8w0,
hdr.ipv4.protocol,
meta.l4Len,
hdr.udp.srcPort,
hdr.udp.dstPort,
hdr.udp.length_
},
hdr.udp.checksum, HashAlgorithm.csum16);
}
}
control DeparserI(packet_out packet,
in headers hdr)
{
apply {
packet.emit(hdr.ethernet);
packet.emit(hdr.ipv4);
packet.emit(hdr.tcp);
packet.emit(hdr.udp);
}
}
V1Switch<headers, metadata>(parserI(),
vc(),
cIngress(),
cEgress(),
uc(),
DeparserI()) main;