forked from p4lang/p4app-switchML
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsers.p4
296 lines (253 loc) · 9.03 KB
/
parsers.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
/*
Copyright 2021 Intel-KAUST-Microsoft
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.
*/
#ifndef _PARSERS_
#define _PARSERS_
#include "types.p4"
#include "headers.p4"
parser IngressParser(
packet_in pkt,
out header_t hdr,
out ingress_metadata_t ig_md,
out ingress_intrinsic_metadata_t ig_intr_md) {
Checksum() ipv4_checksum;
state start {
pkt.extract(ig_intr_md);
transition select(ig_intr_md.resubmit_flag) {
1 : parse_resubmit;
default : parse_port_metadata;
}
}
state parse_resubmit {
// Resubmission not currently used; just skip header
// assume recirculated packets will never be resubmitted for now
pkt.advance(64);
transition parse_ethernet;
}
state parse_port_metadata {
// parse port metadata
ig_md.port_metadata = port_metadata_unpack<port_metadata_t>(pkt);
transition select(ig_intr_md.ingress_port) {
64: parse_recirculate; // pipe 0 CPU Eth port
68: parse_recirculate; // pipe 0 recirc port
320: parse_ethernet; // pipe 2 CPU PCIe port
0x080 &&& 0x180: parse_recirculate; // all pipe 1 ports
0x100 &&& 0x180: parse_recirculate; // all pipe 2 ports
0x180 &&& 0x180: parse_recirculate; // all pipe 3 ports
default: parse_ethernet;
}
}
state parse_recirculate {
// Parse switchml metadata
pkt.extract(ig_md.switchml_md);
pkt.extract(ig_md.switchml_rdma_md);
// Now parse the rest of the packet
transition select(ig_md.switchml_md.packet_type) {
(packet_type_t.CONSUME0) : parse_consume;
(packet_type_t.CONSUME1) : parse_consume;
(packet_type_t.CONSUME2) : parse_consume;
(packet_type_t.CONSUME3) : parse_consume;
default : parse_harvest; // default to parsing for harvests
}
}
state parse_consume {
// Extract the next 256B values
pkt.extract(hdr.d0);
pkt.extract(hdr.d1);
transition accept;
}
state parse_harvest {
// One of these will be filled in by the pipeline, and the other set invalid
hdr.d0.setValid();
hdr.d1.setValid();
transition accept;
}
state parse_ethernet {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.ether_type) {
ETHERTYPE_ARP : parse_arp;
ETHERTYPE_IPV4 : parse_ipv4;
default : accept_regular;
}
}
state parse_arp {
pkt.extract(hdr.arp);
transition select(hdr.arp.hw_type, hdr.arp.proto_type) {
(0x0001, ETHERTYPE_IPV4) : parse_arp_ipv4;
default: accept_regular;
}
}
state parse_arp_ipv4 {
pkt.extract(hdr.arp_ipv4);
transition accept_regular;
}
state parse_ipv4 {
pkt.extract(hdr.ipv4);
ipv4_checksum.add(hdr.ipv4);
ig_md.checksum_err_ipv4 = ipv4_checksum.verify();
ig_md.update_ipv4_checksum = false;
// parse only non-fragmented IP packets with no options
transition select(hdr.ipv4.ihl, hdr.ipv4.frag_offset, hdr.ipv4.protocol) {
(5, 0, ip_protocol_t.ICMP) : parse_icmp;
(5, 0, ip_protocol_t.UDP) : parse_udp;
default : accept_regular;
}
}
state parse_icmp {
pkt.extract(hdr.icmp);
transition accept_regular;
}
state parse_udp {
pkt.extract(hdr.udp);
transition select(hdr.udp.dst_port) {
UDP_PORT_ROCEV2 : parse_ib_bth;
UDP_PORT_SWITCHML_BASE &&& UDP_PORT_SWITCHML_MASK : parse_switchml;
default : accept_regular;
}
}
state parse_ib_bth {
pkt.extract(hdr.ib_bth);
transition select(hdr.ib_bth.opcode) {
// include only UC operations here
ib_opcode_t.UC_SEND_FIRST : parse_ib_payload;
ib_opcode_t.UC_SEND_MIDDLE : parse_ib_payload;
ib_opcode_t.UC_SEND_LAST : parse_ib_payload;
ib_opcode_t.UC_SEND_LAST_IMMEDIATE : parse_ib_immediate;
ib_opcode_t.UC_SEND_ONLY : parse_ib_payload;
ib_opcode_t.UC_SEND_ONLY_IMMEDIATE : parse_ib_immediate;
ib_opcode_t.UC_RDMA_WRITE_FIRST : parse_ib_reth;
ib_opcode_t.UC_RDMA_WRITE_MIDDLE : parse_ib_payload;
ib_opcode_t.UC_RDMA_WRITE_LAST : parse_ib_payload;
ib_opcode_t.UC_RDMA_WRITE_LAST_IMMEDIATE : parse_ib_immediate;
ib_opcode_t.UC_RDMA_WRITE_ONLY : parse_ib_reth;
ib_opcode_t.UC_RDMA_WRITE_ONLY_IMMEDIATE : parse_ib_reth_immediate;
default: accept_regular;
}
}
state parse_ib_immediate {
pkt.extract(hdr.ib_immediate);
transition parse_ib_payload;
}
state parse_ib_reth {
pkt.extract(hdr.ib_reth);
transition parse_ib_payload;
}
state parse_ib_reth_immediate {
pkt.extract(hdr.ib_reth);
pkt.extract(hdr.ib_immediate);
transition parse_ib_payload;
}
state parse_ib_payload {
pkt.extract(hdr.d0);
pkt.extract(hdr.d1);
// do NOT extract ICRC, since this might be in the middle of a >256B packet
ig_md.switchml_md.setValid();
ig_md.switchml_md.ether_type_msb = 16w0xffff;
ig_md.switchml_md.packet_type = packet_type_t.CONSUME0;
ig_md.switchml_rdma_md.setValid();
transition accept;
}
state parse_switchml {
pkt.extract(hdr.switchml);
pkt.extract(hdr.exponents);
transition parse_values;
}
state parse_values {
pkt.extract(hdr.d0);
pkt.extract(hdr.d1);
// At this point we know this is a SwitchML packet that wasn't recirculated,
// so mark it for consumption
ig_md.switchml_md.setValid();
ig_md.switchml_md.packet_type = packet_type_t.CONSUME0;
ig_md.switchml_rdma_md.setValid();
transition accept;
}
state accept_regular {
ig_md.switchml_md.setValid();
ig_md.switchml_md.packet_type = packet_type_t.IGNORE;
ig_md.switchml_rdma_md.setValid();
transition accept;
}
}
control IngressDeparser(
packet_out pkt,
inout header_t hdr,
in ingress_metadata_t ig_md,
in ingress_intrinsic_metadata_for_deparser_t ig_dprsr_md) {
Checksum() ipv4_checksum;
apply {
if (ig_md.update_ipv4_checksum) {
hdr.ipv4.hdr_checksum = ipv4_checksum.update({
hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.total_len,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.frag_offset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.src_addr,
hdr.ipv4.dst_addr});
}
pkt.emit(ig_md.switchml_md);
pkt.emit(ig_md.switchml_rdma_md);
pkt.emit(hdr);
}
}
parser EgressParser(
packet_in pkt,
out header_t hdr,
out egress_metadata_t eg_md,
out egress_intrinsic_metadata_t eg_intr_md) {
state start {
pkt.extract(eg_intr_md);
// All egress packets have a bridged metadata header
transition select(eg_intr_md.pkt_length) {
0 : parse_switchml_md;
_ : parse_switchml_md;
}
}
state parse_switchml_md {
pkt.extract(eg_md.switchml_md);
transition parse_switchml_rdma_md;
}
state parse_switchml_rdma_md {
pkt.extract(eg_md.switchml_rdma_md);
transition accept;
}
}
control EgressDeparser(
packet_out pkt,
inout header_t hdr,
in egress_metadata_t eg_md,
in egress_intrinsic_metadata_for_deparser_t eg_intr_dprs_md) {
Checksum() ipv4_checksum;
apply {
if (eg_md.update_ipv4_checksum) {
hdr.ipv4.hdr_checksum = ipv4_checksum.update({
hdr.ipv4.version,
hdr.ipv4.ihl,
hdr.ipv4.diffserv,
hdr.ipv4.total_len,
hdr.ipv4.identification,
hdr.ipv4.flags,
hdr.ipv4.frag_offset,
hdr.ipv4.ttl,
hdr.ipv4.protocol,
hdr.ipv4.src_addr,
hdr.ipv4.dst_addr});
}
pkt.emit(hdr);
}
}
#endif /* _PARSERS_ */