blob: 2d6a1d4439922efa3421801addd84275141460b3 [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);
Daniele Moro7c3a0022019-07-12 13:38:34 -070043 fabric_metadata.last_eth_type = hdr.ethernet.eth_type;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080044 fabric_metadata.vlan_id = DEFAULT_VLAN_ID;
45 transition select(hdr.ethernet.eth_type){
Yi Tsengbe342052017-11-03 10:21:23 -070046 ETHERTYPE_VLAN: parse_vlan_tag;
47 ETHERTYPE_MPLS: parse_mpls;
Daniele Moro7c3a0022019-07-12 13:38:34 -070048 ETHERTYPE_IPV4: pre_parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080049#ifdef WITH_IPV6
Daniele Moro7c3a0022019-07-12 13:38:34 -070050 ETHERTYPE_IPV6: pre_parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080051#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070052 default: accept;
53 }
54 }
55
56 state parse_vlan_tag {
57 packet.extract(hdr.vlan_tag);
Carmelo Casconeb5324e72018-11-25 02:26:32 -080058 transition select(hdr.vlan_tag.eth_type){
Daniele Moro7c3a0022019-07-12 13:38:34 -070059 ETHERTYPE_IPV4: pre_parse_ipv4;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080060#ifdef WITH_IPV6
Daniele Moro7c3a0022019-07-12 13:38:34 -070061 ETHERTYPE_IPV6: pre_parse_ipv6;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080062#endif // WITH_IPV6
63 ETHERTYPE_MPLS: parse_mpls;
Daniele Moro7c3a0022019-07-12 13:38:34 -070064#if defined(WITH_XCONNECT) || defined(WITH_BNG) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080065 ETHERTYPE_VLAN: parse_inner_vlan_tag;
Daniele Moro7c3a0022019-07-12 13:38:34 -070066 ETHERTYPE_QINQ: parse_inner_vlan_tag;
67 ETHERTYPE_QINQ_NON_STD: parse_inner_vlan_tag;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080068#endif // WITH_XCONNECT
69 default: accept;
70 }
71 }
72
Daniele Moro7c3a0022019-07-12 13:38:34 -070073#if defined(WITH_XCONNECT) || defined(WITH_BNG) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080074 state parse_inner_vlan_tag {
75 packet.extract(hdr.inner_vlan_tag);
76 transition select(hdr.inner_vlan_tag.eth_type){
Daniele Moro7c3a0022019-07-12 13:38:34 -070077 ETHERTYPE_IPV4: pre_parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080078#ifdef WITH_IPV6
Daniele Moro7c3a0022019-07-12 13:38:34 -070079 ETHERTYPE_IPV6: pre_parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080080#endif // WITH_IPV6
Yi Tsengbd46d052018-01-22 17:18:16 -080081 ETHERTYPE_MPLS: parse_mpls;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070082#ifdef WITH_BNG
83 ETHERTYPE_PPPOED: parse_pppoe;
84 ETHERTYPE_PPPOES: parse_pppoe;
85#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -070086 default: accept;
87 }
88 }
Daniele Moro7c3a0022019-07-12 13:38:34 -070089#endif // WITH_XCONNECT || WITH_BNG || WITH_DOUBLE_VLAN_TERMINATION
Yi Tsengbe342052017-11-03 10:21:23 -070090
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 Moro7c3a0022019-07-12 13:38:34 -070096 PPPOE_PROTOCOL_IP4: pre_parse_ipv4;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070097#ifdef WITH_IPV6
Daniele Moro7c3a0022019-07-12 13:38:34 -070098 PPPOE_PROTOCOL_IP6: pre_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);
Daniele Moro7c3a0022019-07-12 13:38:34 -0700107 fabric_metadata.is_mpls = _TRUE;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800108 fabric_metadata.mpls_label = hdr.mpls.label;
109 fabric_metadata.mpls_ttl = hdr.mpls.ttl;
Yi Tsengc6844f52017-12-19 11:58:25 -0800110 // There is only one MPLS label for this fabric.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800111 // Assume header after MPLS header is IPv4/IPv6
Yi Tsengc6844f52017-12-19 11:58:25 -0800112 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -0800113 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Daniele Moro7c3a0022019-07-12 13:38:34 -0700114 // The packet should be either IPv4 or IPv6.
115 // If we have MPLS, go directly to parsing state without
116 // moving to pre_ states, the packet is considered MPLS
Yi Tsengbe342052017-11-03 10:21:23 -0700117 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800118#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700119 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800120#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700121 default: parse_ethernet;
122 }
123 }
124
Daniele Moro7c3a0022019-07-12 13:38:34 -0700125 // Intermediate state to set is_ipv4
126 state pre_parse_ipv4 {
127 fabric_metadata.is_ipv4 = _TRUE;
128 transition parse_ipv4;
129 }
Yi Tsengbe342052017-11-03 10:21:23 -0700130 state parse_ipv4 {
131 packet.extract(hdr.ipv4);
132 fabric_metadata.ip_proto = hdr.ipv4.protocol;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800133 fabric_metadata.ip_eth_type = ETHERTYPE_IPV4;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700134 last_ipv4_dscp = hdr.ipv4.dscp;
Yi Tsengbe342052017-11-03 10:21:23 -0700135 //Need header verification?
136 transition select(hdr.ipv4.protocol) {
137 PROTO_TCP: parse_tcp;
138 PROTO_UDP: parse_udp;
139 PROTO_ICMP: parse_icmp;
140 default: accept;
141 }
142 }
143
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800144#ifdef WITH_IPV6
Daniele Moro7c3a0022019-07-12 13:38:34 -0700145 // Intermediate state to set is_ipv6
146 state pre_parse_ipv6 {
147 fabric_metadata.is_ipv6 = _TRUE;
148 transition parse_ipv6;
149 }
Yi Tsengbe342052017-11-03 10:21:23 -0700150 state parse_ipv6 {
151 packet.extract(hdr.ipv6);
152 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800153 fabric_metadata.ip_eth_type = ETHERTYPE_IPV6;
Yi Tsengbe342052017-11-03 10:21:23 -0700154 transition select(hdr.ipv6.next_hdr) {
155 PROTO_TCP: parse_tcp;
156 PROTO_UDP: parse_udp;
157 PROTO_ICMPV6: parse_icmp;
158 default: accept;
159 }
160 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800161#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700162
Yi Tsengbe342052017-11-03 10:21:23 -0700163 state parse_tcp {
164 packet.extract(hdr.tcp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800165 fabric_metadata.l4_sport = hdr.tcp.sport;
166 fabric_metadata.l4_dport = hdr.tcp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900167#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700168 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900169#else
Yi Tsengbe342052017-11-03 10:21:23 -0700170 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900171#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700172 }
173
174 state parse_udp {
175 packet.extract(hdr.udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800176 fabric_metadata.l4_sport = hdr.udp.sport;
177 fabric_metadata.l4_dport = hdr.udp.dport;
178 transition select(hdr.udp.dport) {
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700179#ifdef WITH_SPGW
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800180 UDP_PORT_GTPU: parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700181#endif // WITH_SPGW
182#ifdef WITH_INT
183 default: parse_int;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800184#else
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700185 default: accept;
186#endif // WITH_INT
187 }
Yi Tsengbe342052017-11-03 10:21:23 -0700188 }
189
190 state parse_icmp {
191 packet.extract(hdr.icmp);
192 transition accept;
193 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800194
195#ifdef WITH_SPGW
196 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700197 transition select(hdr.ipv4.dst_addr[31:32-S1U_SGW_PREFIX_LEN]) {
198 // Avoid parsing GTP and inner headers if we know this GTP packet
199 // is not to be processed by this switch.
200 // FIXME: use parser value sets when support is ready in ONOS.
201 // To set the S1U_SGW_PREFIX value at runtime.
202 S1U_SGW_PREFIX[31:32-S1U_SGW_PREFIX_LEN]: do_parse_gtpu;
203 default: accept;
204 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800205 }
206
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700207 state do_parse_gtpu {
208 packet.extract(hdr.gtpu);
209 transition parse_inner_ipv4;
210 }
211
212 state parse_inner_ipv4 {
213 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700214 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700215 transition select(hdr.inner_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800216 PROTO_TCP: parse_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700217 PROTO_UDP: parse_inner_udp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800218 PROTO_ICMP: parse_icmp;
219 default: accept;
220 }
221 }
222
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700223 state parse_inner_udp {
224 packet.extract(hdr.inner_udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800225 fabric_metadata.l4_sport = hdr.inner_udp.sport;
226 fabric_metadata.l4_dport = hdr.inner_udp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900227#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700228 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900229#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800230 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900231#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800232 }
233#endif // WITH_SPGW
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700234
235#ifdef WITH_INT
236 state parse_int {
237 transition select(last_ipv4_dscp) {
238 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
239 default: accept;
240 }
241 }
242
243 state parse_intl4_shim {
244 packet.extract(hdr.intl4_shim);
245 transition parse_int_header;
246 }
247
248 state parse_int_header {
249 packet.extract(hdr.int_header);
250 // If there is no INT metadata but the INT header (plus shim and tail)
251 // exists, default value of length field in shim header should be
252 // INT_HEADER_LEN_WORDS.
253 transition select (hdr.intl4_shim.len_words) {
254 INT_HEADER_LEN_WORDS: parse_intl4_tail;
255 default: parse_int_data;
256 }
257 }
258
259 state parse_int_data {
260#ifdef WITH_INT_SINK
261 // Parse INT metadata stack, but not tail
262 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
263 transition parse_intl4_tail;
264#else // not interested in INT data
265 transition accept;
266#endif // WITH_INT_SINK
267 }
268
269 state parse_intl4_tail {
270 packet.extract(hdr.intl4_tail);
271 transition accept;
272 }
273#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700274}
275
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800276control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
277
Yi Tseng3d3956d2018-01-31 17:28:05 -0800278 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700279 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700280#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900281 packet.emit(hdr.report_ethernet);
282 packet.emit(hdr.report_ipv4);
283 packet.emit(hdr.report_udp);
284 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700285#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700286 packet.emit(hdr.ethernet);
287 packet.emit(hdr.vlan_tag);
Daniele Moro7c3a0022019-07-12 13:38:34 -0700288#if defined(WITH_XCONNECT) || defined(WITH_BNG) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800289 packet.emit(hdr.inner_vlan_tag);
Daniele Moro7c3a0022019-07-12 13:38:34 -0700290#endif // WITH_XCONNECT || WITH_BNG || WITH_DOUBLE_VLAN_TERMINATION
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700291#ifdef WITH_BNG
292 packet.emit(hdr.pppoe);
293#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -0700294 packet.emit(hdr.mpls);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800295#ifdef WITH_SPGW
296 packet.emit(hdr.gtpu_ipv4);
297 packet.emit(hdr.gtpu_udp);
298 packet.emit(hdr.gtpu);
299#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700300 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800301#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700302 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800303#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700304 packet.emit(hdr.tcp);
305 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800306 packet.emit(hdr.icmp);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900307#ifdef WITH_INT
308 packet.emit(hdr.intl4_shim);
309 packet.emit(hdr.int_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700310#ifdef WITH_INT_TRANSIT
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900311 packet.emit(hdr.int_switch_id);
312 packet.emit(hdr.int_port_ids);
313 packet.emit(hdr.int_hop_latency);
314 packet.emit(hdr.int_q_occupancy);
315 packet.emit(hdr.int_ingress_tstamp);
316 packet.emit(hdr.int_egress_tstamp);
317 packet.emit(hdr.int_q_congestion);
318 packet.emit(hdr.int_egress_tx_util);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700319#endif // WITH_INT_TRANSIT
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700320#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900321 packet.emit(hdr.int_data);
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700322#endif // WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900323 packet.emit(hdr.intl4_tail);
324#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700325 }
326}
327
328#endif