blob: fb9e76448ade484112a64414d80a48a057b1bda9 [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;
Carmelo Cascone2a308ff2021-06-01 18:31:57 -0700184 gtpu_t gtpu = packet.lookahead<gtpu_t>();
185 transition select(hdr.udp.dport, gtpu.version, gtpu.msgtype) {
Wailok Shum4f51bde2021-06-11 22:48:41 +0800186 // Treat GTP control traffic as payload.
187 (UDP_PORT_GTPU, GTP_V1, GTP_GPDU): parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700188#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
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800201 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700202 packet.extract(hdr.gtpu);
203 transition parse_inner_ipv4;
204 }
205
206 state parse_inner_ipv4 {
207 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700208 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700209 transition select(hdr.inner_ipv4.protocol) {
Daniele Moroae26f0a2021-07-08 12:53:26 +0200210 PROTO_TCP: parse_inner_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700211 PROTO_UDP: parse_inner_udp;
Daniele Moroae26f0a2021-07-08 12:53:26 +0200212 PROTO_ICMP: parse_inner_icmp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800213 default: accept;
214 }
215 }
216
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700217 state parse_inner_udp {
218 packet.extract(hdr.inner_udp);
Wailok Shum4f51bde2021-06-11 22:48:41 +0800219#ifdef WITH_SPGW
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;
Wailok Shum4f51bde2021-06-11 22:48:41 +0800222#endif // WITH_SPGW
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900223#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700224 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900225#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800226 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900227#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800228 }
Robert MacDavidde12b982020-07-15 18:38:59 -0700229
Wailok Shum4f51bde2021-06-11 22:48:41 +0800230 state parse_inner_tcp {
Robert MacDavidde12b982020-07-15 18:38:59 -0700231 packet.extract(hdr.inner_tcp);
Wailok Shum4f51bde2021-06-11 22:48:41 +0800232#ifdef WITH_SPGW
Robert MacDavidde12b982020-07-15 18:38:59 -0700233 fabric_metadata.inner_l4_sport = hdr.inner_tcp.sport;
234 fabric_metadata.inner_l4_dport = hdr.inner_tcp.dport;
Wailok Shum4f51bde2021-06-11 22:48:41 +0800235#endif // WITH_SPGW
Robert MacDavidde12b982020-07-15 18:38:59 -0700236 transition accept;
237 }
238
Wailok Shum4f51bde2021-06-11 22:48:41 +0800239 state parse_inner_icmp {
Robert MacDavidde12b982020-07-15 18:38:59 -0700240 packet.extract(hdr.inner_icmp);
241 transition accept;
242 }
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700243
244#ifdef WITH_INT
245 state parse_int {
246 transition select(last_ipv4_dscp) {
247 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
248 default: accept;
249 }
250 }
251
252 state parse_intl4_shim {
253 packet.extract(hdr.intl4_shim);
254 transition parse_int_header;
255 }
256
257 state parse_int_header {
258 packet.extract(hdr.int_header);
259 // If there is no INT metadata but the INT header (plus shim and tail)
260 // exists, default value of length field in shim header should be
261 // INT_HEADER_LEN_WORDS.
262 transition select (hdr.intl4_shim.len_words) {
263 INT_HEADER_LEN_WORDS: parse_intl4_tail;
264 default: parse_int_data;
265 }
266 }
267
268 state parse_int_data {
269#ifdef WITH_INT_SINK
270 // Parse INT metadata stack, but not tail
271 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
272 transition parse_intl4_tail;
273#else // not interested in INT data
274 transition accept;
275#endif // WITH_INT_SINK
276 }
277
278 state parse_intl4_tail {
279 packet.extract(hdr.intl4_tail);
280 transition accept;
281 }
282#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700283}
284
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800285control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
286
Yi Tseng3d3956d2018-01-31 17:28:05 -0800287 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700288 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700289#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900290 packet.emit(hdr.report_ethernet);
Daniele Moro5a2de712019-09-24 14:34:07 -0700291 packet.emit(hdr.report_eth_type);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900292 packet.emit(hdr.report_ipv4);
293 packet.emit(hdr.report_udp);
294 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700295#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700296 packet.emit(hdr.ethernet);
297 packet.emit(hdr.vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700298#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800299 packet.emit(hdr.inner_vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700300#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
301 packet.emit(hdr.eth_type);
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700302#ifdef WITH_BNG
303 packet.emit(hdr.pppoe);
304#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -0700305 packet.emit(hdr.mpls);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800306#ifdef WITH_SPGW
307 packet.emit(hdr.gtpu_ipv4);
308 packet.emit(hdr.gtpu_udp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700309 packet.emit(hdr.outer_gtpu);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800310#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700311 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800312#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700313 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800314#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700315 packet.emit(hdr.tcp);
316 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800317 packet.emit(hdr.icmp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700318 // if we parsed a GTPU packet but did not decap it
319 packet.emit(hdr.gtpu);
320 packet.emit(hdr.inner_ipv4);
321 packet.emit(hdr.inner_tcp);
322 packet.emit(hdr.inner_udp);
323 packet.emit(hdr.inner_icmp);
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