blob: 21032c7391d930c614f45f43b3050719a935ad39 [file] [log] [blame]
Yi Tsengbe342052017-11-03 10:21:23 -07001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __PARSER__
18#define __PARSER__
19
20#include "define.p4"
21
Carmelo Casconeb5324e72018-11-25 02:26:32 -080022parser FabricParser (packet_in packet,
23 out parsed_headers_t hdr,
24 inout fabric_metadata_t fabric_metadata,
25 inout standard_metadata_t standard_metadata) {
Yi Tseng1d842672017-11-28 16:06:52 -080026
Carmelo Cascone79a3a312018-08-16 17:14:43 -070027 bit<6> last_ipv4_dscp = 0;
28
Yi Tsengbe342052017-11-03 10:21:23 -070029 state start {
30 transition select(standard_metadata.ingress_port) {
Carmelo Cascone2388cc12021-05-26 19:30:30 +020031 CPU_PORT: check_packet_out;
Yi Tsengbe342052017-11-03 10:21:23 -070032 default: parse_ethernet;
33 }
34 }
35
Carmelo Cascone2388cc12021-05-26 19:30:30 +020036 state check_packet_out {
37 packet_out_header_t tmp = packet.lookahead<packet_out_header_t>();
38 transition select(tmp.do_forwarding) {
39 0: parse_packet_out_and_accept;
40 default: strip_packet_out;
41 }
42 }
43
44 state parse_packet_out_and_accept {
45 // Will transmit over requested egress port as-is. No need to parse further.
Yi Tsengbe342052017-11-03 10:21:23 -070046 packet.extract(hdr.packet_out);
Carmelo Cascone2388cc12021-05-26 19:30:30 +020047 transition accept;
48 }
49
50 state strip_packet_out {
51 // Remove packet-out header and process as a regular packet.
52 packet.advance(PACKET_OUT_HDR_SIZE * 8);
Yi Tsengbe342052017-11-03 10:21:23 -070053 transition parse_ethernet;
54 }
55
56 state parse_ethernet {
57 packet.extract(hdr.ethernet);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080058 fabric_metadata.vlan_id = DEFAULT_VLAN_ID;
Daniele Moro5a2de712019-09-24 14:34:07 -070059 transition select(packet.lookahead<bit<16>>()){
Daniele Moro77654f92019-07-30 10:29:54 -070060 ETHERTYPE_QINQ: parse_vlan_tag;
61 ETHERTYPE_QINQ_NON_STD: parse_vlan_tag;
Yi Tsengbe342052017-11-03 10:21:23 -070062 ETHERTYPE_VLAN: parse_vlan_tag;
Daniele Moro5a2de712019-09-24 14:34:07 -070063 default: parse_eth_type;
Yi Tsengbe342052017-11-03 10:21:23 -070064 }
65 }
66
67 state parse_vlan_tag {
68 packet.extract(hdr.vlan_tag);
Daniele Morob3d199b2019-11-01 14:01:46 -070069#ifdef WITH_BNG
70 fabric_metadata.bng.s_tag = hdr.vlan_tag.vlan_id;
71#endif // WITH_BNG
Daniele Moro5a2de712019-09-24 14:34:07 -070072 transition select(packet.lookahead<bit<16>>()){
73#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080074 ETHERTYPE_VLAN: parse_inner_vlan_tag;
Daniele Moro5a2de712019-09-24 14:34:07 -070075#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
76 default: parse_eth_type;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080077 }
78 }
79
Daniele Moro5a2de712019-09-24 14:34:07 -070080#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080081 state parse_inner_vlan_tag {
82 packet.extract(hdr.inner_vlan_tag);
Daniele Morob3d199b2019-11-01 14:01:46 -070083#ifdef WITH_BNG
84 fabric_metadata.bng.c_tag = hdr.inner_vlan_tag.vlan_id;
85#endif // WITH_BNG
Daniele Moro5a2de712019-09-24 14:34:07 -070086 transition parse_eth_type;
87 }
88#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
89
90 state parse_eth_type {
91 packet.extract(hdr.eth_type);
92 transition select(hdr.eth_type.value) {
Yi Tsengbd46d052018-01-22 17:18:16 -080093 ETHERTYPE_MPLS: parse_mpls;
Daniele Moro5a2de712019-09-24 14:34:07 -070094 ETHERTYPE_IPV4: parse_ipv4;
95#ifdef WITH_IPV6
96 ETHERTYPE_IPV6: parse_ipv6;
97#endif // WITH_IPV6
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070098#ifdef WITH_BNG
99 ETHERTYPE_PPPOED: parse_pppoe;
100 ETHERTYPE_PPPOES: parse_pppoe;
101#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -0700102 default: accept;
103 }
104 }
105
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700106#ifdef WITH_BNG
107 state parse_pppoe {
108 packet.extract(hdr.pppoe);
109 transition select(hdr.pppoe.protocol) {
Daniele Moroe22b5742019-06-28 15:32:37 -0700110 PPPOE_PROTOCOL_MPLS: parse_mpls;
Daniele Moro5a2de712019-09-24 14:34:07 -0700111 PPPOE_PROTOCOL_IP4: parse_ipv4;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700112#ifdef WITH_IPV6
Daniele Moro5a2de712019-09-24 14:34:07 -0700113 PPPOE_PROTOCOL_IP6: parse_ipv6;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700114#endif // WITH_IPV6
115 default: accept;
116 }
117 }
118#endif // WITH_BNG
119
Yi Tsengbe342052017-11-03 10:21:23 -0700120 state parse_mpls {
121 packet.extract(hdr.mpls);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800122 fabric_metadata.mpls_label = hdr.mpls.label;
123 fabric_metadata.mpls_ttl = hdr.mpls.ttl;
Yi Tsengc6844f52017-12-19 11:58:25 -0800124 // There is only one MPLS label for this fabric.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800125 // Assume header after MPLS header is IPv4/IPv6
Yi Tsengc6844f52017-12-19 11:58:25 -0800126 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -0800127 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Daniele Moro7c3a0022019-07-12 13:38:34 -0700128 // The packet should be either IPv4 or IPv6.
129 // If we have MPLS, go directly to parsing state without
130 // moving to pre_ states, the packet is considered MPLS
Yi Tsengbe342052017-11-03 10:21:23 -0700131 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800132#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700133 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800134#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700135 default: parse_ethernet;
136 }
137 }
138
139 state parse_ipv4 {
140 packet.extract(hdr.ipv4);
141 fabric_metadata.ip_proto = hdr.ipv4.protocol;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800142 fabric_metadata.ip_eth_type = ETHERTYPE_IPV4;
Robert MacDavidbec6b6a2020-05-21 21:32:38 -0400143 fabric_metadata.ipv4_src_addr = hdr.ipv4.src_addr;
144 fabric_metadata.ipv4_dst_addr = hdr.ipv4.dst_addr;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700145 last_ipv4_dscp = hdr.ipv4.dscp;
Yi Tsengbe342052017-11-03 10:21:23 -0700146 //Need header verification?
147 transition select(hdr.ipv4.protocol) {
148 PROTO_TCP: parse_tcp;
149 PROTO_UDP: parse_udp;
150 PROTO_ICMP: parse_icmp;
151 default: accept;
152 }
153 }
154
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800155#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700156 state parse_ipv6 {
157 packet.extract(hdr.ipv6);
158 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800159 fabric_metadata.ip_eth_type = ETHERTYPE_IPV6;
Yi Tsengbe342052017-11-03 10:21:23 -0700160 transition select(hdr.ipv6.next_hdr) {
161 PROTO_TCP: parse_tcp;
162 PROTO_UDP: parse_udp;
163 PROTO_ICMPV6: parse_icmp;
164 default: accept;
165 }
166 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800167#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700168
Yi Tsengbe342052017-11-03 10:21:23 -0700169 state parse_tcp {
170 packet.extract(hdr.tcp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800171 fabric_metadata.l4_sport = hdr.tcp.sport;
172 fabric_metadata.l4_dport = hdr.tcp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900173#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700174 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900175#else
Yi Tsengbe342052017-11-03 10:21:23 -0700176 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900177#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700178 }
179
180 state parse_udp {
181 packet.extract(hdr.udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800182 fabric_metadata.l4_sport = hdr.udp.sport;
183 fabric_metadata.l4_dport = hdr.udp.dport;
184 transition select(hdr.udp.dport) {
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700185#ifdef WITH_SPGW
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800186 UDP_PORT_GTPU: parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700187#endif // WITH_SPGW
188#ifdef WITH_INT
189 default: parse_int;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800190#else
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700191 default: accept;
192#endif // WITH_INT
193 }
Yi Tsengbe342052017-11-03 10:21:23 -0700194 }
195
196 state parse_icmp {
197 packet.extract(hdr.icmp);
198 transition accept;
199 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800200
201#ifdef WITH_SPGW
202 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700203 packet.extract(hdr.gtpu);
204 transition parse_inner_ipv4;
205 }
206
207 state parse_inner_ipv4 {
208 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700209 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700210 transition select(hdr.inner_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800211 PROTO_TCP: parse_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700212 PROTO_UDP: parse_inner_udp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800213 PROTO_ICMP: parse_icmp;
214 default: accept;
215 }
216 }
217
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700218 state parse_inner_udp {
219 packet.extract(hdr.inner_udp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700220 fabric_metadata.inner_l4_sport = hdr.inner_udp.sport;
221 fabric_metadata.inner_l4_dport = hdr.inner_udp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900222#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700223 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900224#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800225 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900226#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800227 }
Robert MacDavidde12b982020-07-15 18:38:59 -0700228
229 state parse_inner_tcp {
230 packet.extract(hdr.inner_tcp);
231 fabric_metadata.inner_l4_sport = hdr.inner_tcp.sport;
232 fabric_metadata.inner_l4_dport = hdr.inner_tcp.dport;
233 transition accept;
234 }
235
236 state parse_inner_icmp {
237 packet.extract(hdr.inner_icmp);
238 transition accept;
239 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800240#endif // WITH_SPGW
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700241
242#ifdef WITH_INT
243 state parse_int {
244 transition select(last_ipv4_dscp) {
245 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
246 default: accept;
247 }
248 }
249
250 state parse_intl4_shim {
251 packet.extract(hdr.intl4_shim);
252 transition parse_int_header;
253 }
254
255 state parse_int_header {
256 packet.extract(hdr.int_header);
257 // If there is no INT metadata but the INT header (plus shim and tail)
258 // exists, default value of length field in shim header should be
259 // INT_HEADER_LEN_WORDS.
260 transition select (hdr.intl4_shim.len_words) {
261 INT_HEADER_LEN_WORDS: parse_intl4_tail;
262 default: parse_int_data;
263 }
264 }
265
266 state parse_int_data {
267#ifdef WITH_INT_SINK
268 // Parse INT metadata stack, but not tail
269 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
270 transition parse_intl4_tail;
271#else // not interested in INT data
272 transition accept;
273#endif // WITH_INT_SINK
274 }
275
276 state parse_intl4_tail {
277 packet.extract(hdr.intl4_tail);
278 transition accept;
279 }
280#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700281}
282
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800283control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
284
Yi Tseng3d3956d2018-01-31 17:28:05 -0800285 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700286 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700287#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900288 packet.emit(hdr.report_ethernet);
Daniele Moro5a2de712019-09-24 14:34:07 -0700289 packet.emit(hdr.report_eth_type);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900290 packet.emit(hdr.report_ipv4);
291 packet.emit(hdr.report_udp);
292 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700293#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700294 packet.emit(hdr.ethernet);
295 packet.emit(hdr.vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700296#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800297 packet.emit(hdr.inner_vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700298#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
299 packet.emit(hdr.eth_type);
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700300#ifdef WITH_BNG
301 packet.emit(hdr.pppoe);
302#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -0700303 packet.emit(hdr.mpls);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800304#ifdef WITH_SPGW
305 packet.emit(hdr.gtpu_ipv4);
306 packet.emit(hdr.gtpu_udp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700307 packet.emit(hdr.outer_gtpu);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800308#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700309 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800310#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700311 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800312#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700313 packet.emit(hdr.tcp);
314 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800315 packet.emit(hdr.icmp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700316#ifdef WITH_SPGW
317 // if we parsed a GTPU packet but did not decap it
318 packet.emit(hdr.gtpu);
319 packet.emit(hdr.inner_ipv4);
320 packet.emit(hdr.inner_tcp);
321 packet.emit(hdr.inner_udp);
322 packet.emit(hdr.inner_icmp);
323#endif // WITH_SPGW
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900324#ifdef WITH_INT
325 packet.emit(hdr.intl4_shim);
326 packet.emit(hdr.int_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700327#ifdef WITH_INT_TRANSIT
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900328 packet.emit(hdr.int_switch_id);
329 packet.emit(hdr.int_port_ids);
330 packet.emit(hdr.int_hop_latency);
331 packet.emit(hdr.int_q_occupancy);
332 packet.emit(hdr.int_ingress_tstamp);
333 packet.emit(hdr.int_egress_tstamp);
334 packet.emit(hdr.int_q_congestion);
335 packet.emit(hdr.int_egress_tx_util);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700336#endif // WITH_INT_TRANSIT
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700337#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900338 packet.emit(hdr.int_data);
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700339#endif // WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900340 packet.emit(hdr.intl4_tail);
341#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700342 }
343}
344
Robert MacDavidbec6b6a2020-05-21 21:32:38 -0400345#endif