blob: de0a76baa7fb14f68749c466784db16d74020363 [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) {
31 CPU_PORT: parse_packet_out;
32 default: parse_ethernet;
33 }
34 }
35
36 state parse_packet_out {
37 packet.extract(hdr.packet_out);
38 transition parse_ethernet;
39 }
40
41 state parse_ethernet {
42 packet.extract(hdr.ethernet);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080043 fabric_metadata.vlan_id = DEFAULT_VLAN_ID;
Daniele Moro5a2de712019-09-24 14:34:07 -070044 transition select(packet.lookahead<bit<16>>()){
Daniele Moro77654f92019-07-30 10:29:54 -070045 ETHERTYPE_QINQ: parse_vlan_tag;
46 ETHERTYPE_QINQ_NON_STD: parse_vlan_tag;
Yi Tsengbe342052017-11-03 10:21:23 -070047 ETHERTYPE_VLAN: parse_vlan_tag;
Daniele Moro5a2de712019-09-24 14:34:07 -070048 default: parse_eth_type;
Yi Tsengbe342052017-11-03 10:21:23 -070049 }
50 }
51
52 state parse_vlan_tag {
53 packet.extract(hdr.vlan_tag);
Daniele Morob3d199b2019-11-01 14:01:46 -070054#ifdef WITH_BNG
55 fabric_metadata.bng.s_tag = hdr.vlan_tag.vlan_id;
56#endif // WITH_BNG
Daniele Moro5a2de712019-09-24 14:34:07 -070057 transition select(packet.lookahead<bit<16>>()){
58#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080059 ETHERTYPE_VLAN: parse_inner_vlan_tag;
Daniele Moro5a2de712019-09-24 14:34:07 -070060#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
61 default: parse_eth_type;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080062 }
63 }
64
Daniele Moro5a2de712019-09-24 14:34:07 -070065#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080066 state parse_inner_vlan_tag {
67 packet.extract(hdr.inner_vlan_tag);
Daniele Morob3d199b2019-11-01 14:01:46 -070068#ifdef WITH_BNG
69 fabric_metadata.bng.c_tag = hdr.inner_vlan_tag.vlan_id;
70#endif // WITH_BNG
Daniele Moro5a2de712019-09-24 14:34:07 -070071 transition parse_eth_type;
72 }
73#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
74
75 state parse_eth_type {
76 packet.extract(hdr.eth_type);
77 transition select(hdr.eth_type.value) {
Yi Tsengbd46d052018-01-22 17:18:16 -080078 ETHERTYPE_MPLS: parse_mpls;
Daniele Moro5a2de712019-09-24 14:34:07 -070079 ETHERTYPE_IPV4: parse_ipv4;
80#ifdef WITH_IPV6
81 ETHERTYPE_IPV6: parse_ipv6;
82#endif // WITH_IPV6
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070083#ifdef WITH_BNG
84 ETHERTYPE_PPPOED: parse_pppoe;
85 ETHERTYPE_PPPOES: parse_pppoe;
86#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -070087 default: accept;
88 }
89 }
90
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070091#ifdef WITH_BNG
92 state parse_pppoe {
93 packet.extract(hdr.pppoe);
94 transition select(hdr.pppoe.protocol) {
Daniele Moroe22b5742019-06-28 15:32:37 -070095 PPPOE_PROTOCOL_MPLS: parse_mpls;
Daniele Moro5a2de712019-09-24 14:34:07 -070096 PPPOE_PROTOCOL_IP4: parse_ipv4;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070097#ifdef WITH_IPV6
Daniele Moro5a2de712019-09-24 14:34:07 -070098 PPPOE_PROTOCOL_IP6: parse_ipv6;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070099#endif // WITH_IPV6
100 default: accept;
101 }
102 }
103#endif // WITH_BNG
104
Yi Tsengbe342052017-11-03 10:21:23 -0700105 state parse_mpls {
106 packet.extract(hdr.mpls);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800107 fabric_metadata.mpls_label = hdr.mpls.label;
108 fabric_metadata.mpls_ttl = hdr.mpls.ttl;
Yi Tsengc6844f52017-12-19 11:58:25 -0800109 // There is only one MPLS label for this fabric.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800110 // Assume header after MPLS header is IPv4/IPv6
Yi Tsengc6844f52017-12-19 11:58:25 -0800111 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -0800112 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Daniele Moro7c3a0022019-07-12 13:38:34 -0700113 // The packet should be either IPv4 or IPv6.
114 // If we have MPLS, go directly to parsing state without
115 // moving to pre_ states, the packet is considered MPLS
Yi Tsengbe342052017-11-03 10:21:23 -0700116 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800117#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700118 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800119#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700120 default: parse_ethernet;
121 }
122 }
123
124 state parse_ipv4 {
125 packet.extract(hdr.ipv4);
126 fabric_metadata.ip_proto = hdr.ipv4.protocol;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800127 fabric_metadata.ip_eth_type = ETHERTYPE_IPV4;
Robert MacDavidbec6b6a2020-05-21 21:32:38 -0400128 fabric_metadata.ipv4_src_addr = hdr.ipv4.src_addr;
129 fabric_metadata.ipv4_dst_addr = hdr.ipv4.dst_addr;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700130 last_ipv4_dscp = hdr.ipv4.dscp;
Yi Tsengbe342052017-11-03 10:21:23 -0700131 //Need header verification?
132 transition select(hdr.ipv4.protocol) {
133 PROTO_TCP: parse_tcp;
134 PROTO_UDP: parse_udp;
135 PROTO_ICMP: parse_icmp;
136 default: accept;
137 }
138 }
139
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800140#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700141 state parse_ipv6 {
142 packet.extract(hdr.ipv6);
143 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800144 fabric_metadata.ip_eth_type = ETHERTYPE_IPV6;
Yi Tsengbe342052017-11-03 10:21:23 -0700145 transition select(hdr.ipv6.next_hdr) {
146 PROTO_TCP: parse_tcp;
147 PROTO_UDP: parse_udp;
148 PROTO_ICMPV6: parse_icmp;
149 default: accept;
150 }
151 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800152#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700153
Yi Tsengbe342052017-11-03 10:21:23 -0700154 state parse_tcp {
155 packet.extract(hdr.tcp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800156 fabric_metadata.l4_sport = hdr.tcp.sport;
157 fabric_metadata.l4_dport = hdr.tcp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900158#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700159 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900160#else
Yi Tsengbe342052017-11-03 10:21:23 -0700161 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900162#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700163 }
164
165 state parse_udp {
166 packet.extract(hdr.udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800167 fabric_metadata.l4_sport = hdr.udp.sport;
168 fabric_metadata.l4_dport = hdr.udp.dport;
169 transition select(hdr.udp.dport) {
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700170#ifdef WITH_SPGW
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800171 UDP_PORT_GTPU: parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700172#endif // WITH_SPGW
173#ifdef WITH_INT
174 default: parse_int;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800175#else
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700176 default: accept;
177#endif // WITH_INT
178 }
Yi Tsengbe342052017-11-03 10:21:23 -0700179 }
180
181 state parse_icmp {
182 packet.extract(hdr.icmp);
183 transition accept;
184 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800185
186#ifdef WITH_SPGW
187 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700188 packet.extract(hdr.gtpu);
189 transition parse_inner_ipv4;
190 }
191
192 state parse_inner_ipv4 {
193 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700194 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700195 transition select(hdr.inner_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800196 PROTO_TCP: parse_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700197 PROTO_UDP: parse_inner_udp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800198 PROTO_ICMP: parse_icmp;
199 default: accept;
200 }
201 }
202
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700203 state parse_inner_udp {
204 packet.extract(hdr.inner_udp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700205 fabric_metadata.inner_l4_sport = hdr.inner_udp.sport;
206 fabric_metadata.inner_l4_dport = hdr.inner_udp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900207#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700208 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900209#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800210 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900211#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800212 }
Robert MacDavidde12b982020-07-15 18:38:59 -0700213
214 state parse_inner_tcp {
215 packet.extract(hdr.inner_tcp);
216 fabric_metadata.inner_l4_sport = hdr.inner_tcp.sport;
217 fabric_metadata.inner_l4_dport = hdr.inner_tcp.dport;
218 transition accept;
219 }
220
221 state parse_inner_icmp {
222 packet.extract(hdr.inner_icmp);
223 transition accept;
224 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800225#endif // WITH_SPGW
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700226
227#ifdef WITH_INT
228 state parse_int {
229 transition select(last_ipv4_dscp) {
230 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
231 default: accept;
232 }
233 }
234
235 state parse_intl4_shim {
236 packet.extract(hdr.intl4_shim);
237 transition parse_int_header;
238 }
239
240 state parse_int_header {
241 packet.extract(hdr.int_header);
242 // If there is no INT metadata but the INT header (plus shim and tail)
243 // exists, default value of length field in shim header should be
244 // INT_HEADER_LEN_WORDS.
245 transition select (hdr.intl4_shim.len_words) {
246 INT_HEADER_LEN_WORDS: parse_intl4_tail;
247 default: parse_int_data;
248 }
249 }
250
251 state parse_int_data {
252#ifdef WITH_INT_SINK
253 // Parse INT metadata stack, but not tail
254 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
255 transition parse_intl4_tail;
256#else // not interested in INT data
257 transition accept;
258#endif // WITH_INT_SINK
259 }
260
261 state parse_intl4_tail {
262 packet.extract(hdr.intl4_tail);
263 transition accept;
264 }
265#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700266}
267
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800268control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
269
Yi Tseng3d3956d2018-01-31 17:28:05 -0800270 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700271 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700272#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900273 packet.emit(hdr.report_ethernet);
Daniele Moro5a2de712019-09-24 14:34:07 -0700274 packet.emit(hdr.report_eth_type);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900275 packet.emit(hdr.report_ipv4);
276 packet.emit(hdr.report_udp);
277 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700278#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700279 packet.emit(hdr.ethernet);
280 packet.emit(hdr.vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700281#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800282 packet.emit(hdr.inner_vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700283#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
284 packet.emit(hdr.eth_type);
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700285#ifdef WITH_BNG
286 packet.emit(hdr.pppoe);
287#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -0700288 packet.emit(hdr.mpls);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800289#ifdef WITH_SPGW
290 packet.emit(hdr.gtpu_ipv4);
291 packet.emit(hdr.gtpu_udp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700292 packet.emit(hdr.outer_gtpu);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800293#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700294 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800295#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700296 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800297#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700298 packet.emit(hdr.tcp);
299 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800300 packet.emit(hdr.icmp);
Robert MacDavidde12b982020-07-15 18:38:59 -0700301#ifdef WITH_SPGW
302 // if we parsed a GTPU packet but did not decap it
303 packet.emit(hdr.gtpu);
304 packet.emit(hdr.inner_ipv4);
305 packet.emit(hdr.inner_tcp);
306 packet.emit(hdr.inner_udp);
307 packet.emit(hdr.inner_icmp);
308#endif // WITH_SPGW
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900309#ifdef WITH_INT
310 packet.emit(hdr.intl4_shim);
311 packet.emit(hdr.int_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700312#ifdef WITH_INT_TRANSIT
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900313 packet.emit(hdr.int_switch_id);
314 packet.emit(hdr.int_port_ids);
315 packet.emit(hdr.int_hop_latency);
316 packet.emit(hdr.int_q_occupancy);
317 packet.emit(hdr.int_ingress_tstamp);
318 packet.emit(hdr.int_egress_tstamp);
319 packet.emit(hdr.int_q_congestion);
320 packet.emit(hdr.int_egress_tx_util);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700321#endif // WITH_INT_TRANSIT
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700322#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900323 packet.emit(hdr.int_data);
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700324#endif // WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900325 packet.emit(hdr.intl4_tail);
326#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700327 }
328}
329
Robert MacDavidbec6b6a2020-05-21 21:32:38 -0400330#endif