blob: ee6b13c675d614a0e2bb5a7524f44028b144eb43 [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 Moro5a2de712019-09-24 14:34:07 -070054 transition select(packet.lookahead<bit<16>>()){
55#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080056 ETHERTYPE_VLAN: parse_inner_vlan_tag;
Daniele Moro5a2de712019-09-24 14:34:07 -070057#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
58 default: parse_eth_type;
Carmelo Casconeb5324e72018-11-25 02:26:32 -080059 }
60 }
61
Daniele Moro5a2de712019-09-24 14:34:07 -070062#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080063 state parse_inner_vlan_tag {
64 packet.extract(hdr.inner_vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -070065 transition parse_eth_type;
66 }
67#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
68
69 state parse_eth_type {
70 packet.extract(hdr.eth_type);
71 transition select(hdr.eth_type.value) {
Yi Tsengbd46d052018-01-22 17:18:16 -080072 ETHERTYPE_MPLS: parse_mpls;
Daniele Moro5a2de712019-09-24 14:34:07 -070073 ETHERTYPE_IPV4: parse_ipv4;
74#ifdef WITH_IPV6
75 ETHERTYPE_IPV6: parse_ipv6;
76#endif // WITH_IPV6
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070077#ifdef WITH_BNG
78 ETHERTYPE_PPPOED: parse_pppoe;
79 ETHERTYPE_PPPOES: parse_pppoe;
80#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -070081 default: accept;
82 }
83 }
84
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070085#ifdef WITH_BNG
86 state parse_pppoe {
87 packet.extract(hdr.pppoe);
88 transition select(hdr.pppoe.protocol) {
Daniele Moroe22b5742019-06-28 15:32:37 -070089 PPPOE_PROTOCOL_MPLS: parse_mpls;
Daniele Moro5a2de712019-09-24 14:34:07 -070090 PPPOE_PROTOCOL_IP4: parse_ipv4;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070091#ifdef WITH_IPV6
Daniele Moro5a2de712019-09-24 14:34:07 -070092 PPPOE_PROTOCOL_IP6: parse_ipv6;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070093#endif // WITH_IPV6
94 default: accept;
95 }
96 }
97#endif // WITH_BNG
98
Yi Tsengbe342052017-11-03 10:21:23 -070099 state parse_mpls {
100 packet.extract(hdr.mpls);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800101 fabric_metadata.mpls_label = hdr.mpls.label;
102 fabric_metadata.mpls_ttl = hdr.mpls.ttl;
Yi Tsengc6844f52017-12-19 11:58:25 -0800103 // There is only one MPLS label for this fabric.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800104 // Assume header after MPLS header is IPv4/IPv6
Yi Tsengc6844f52017-12-19 11:58:25 -0800105 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -0800106 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Daniele Moro7c3a0022019-07-12 13:38:34 -0700107 // The packet should be either IPv4 or IPv6.
108 // If we have MPLS, go directly to parsing state without
109 // moving to pre_ states, the packet is considered MPLS
Yi Tsengbe342052017-11-03 10:21:23 -0700110 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800111#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700112 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800113#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700114 default: parse_ethernet;
115 }
116 }
117
118 state parse_ipv4 {
119 packet.extract(hdr.ipv4);
120 fabric_metadata.ip_proto = hdr.ipv4.protocol;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800121 fabric_metadata.ip_eth_type = ETHERTYPE_IPV4;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700122 last_ipv4_dscp = hdr.ipv4.dscp;
Yi Tsengbe342052017-11-03 10:21:23 -0700123 //Need header verification?
124 transition select(hdr.ipv4.protocol) {
125 PROTO_TCP: parse_tcp;
126 PROTO_UDP: parse_udp;
127 PROTO_ICMP: parse_icmp;
128 default: accept;
129 }
130 }
131
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800132#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700133 state parse_ipv6 {
134 packet.extract(hdr.ipv6);
135 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800136 fabric_metadata.ip_eth_type = ETHERTYPE_IPV6;
Yi Tsengbe342052017-11-03 10:21:23 -0700137 transition select(hdr.ipv6.next_hdr) {
138 PROTO_TCP: parse_tcp;
139 PROTO_UDP: parse_udp;
140 PROTO_ICMPV6: parse_icmp;
141 default: accept;
142 }
143 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800144#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700145
Yi Tsengbe342052017-11-03 10:21:23 -0700146 state parse_tcp {
147 packet.extract(hdr.tcp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800148 fabric_metadata.l4_sport = hdr.tcp.sport;
149 fabric_metadata.l4_dport = hdr.tcp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900150#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700151 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900152#else
Yi Tsengbe342052017-11-03 10:21:23 -0700153 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900154#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700155 }
156
157 state parse_udp {
158 packet.extract(hdr.udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800159 fabric_metadata.l4_sport = hdr.udp.sport;
160 fabric_metadata.l4_dport = hdr.udp.dport;
161 transition select(hdr.udp.dport) {
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700162#ifdef WITH_SPGW
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800163 UDP_PORT_GTPU: parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700164#endif // WITH_SPGW
165#ifdef WITH_INT
166 default: parse_int;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800167#else
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700168 default: accept;
169#endif // WITH_INT
170 }
Yi Tsengbe342052017-11-03 10:21:23 -0700171 }
172
173 state parse_icmp {
174 packet.extract(hdr.icmp);
175 transition accept;
176 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800177
178#ifdef WITH_SPGW
179 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700180 transition select(hdr.ipv4.dst_addr[31:32-S1U_SGW_PREFIX_LEN]) {
181 // Avoid parsing GTP and inner headers if we know this GTP packet
182 // is not to be processed by this switch.
183 // FIXME: use parser value sets when support is ready in ONOS.
184 // To set the S1U_SGW_PREFIX value at runtime.
185 S1U_SGW_PREFIX[31:32-S1U_SGW_PREFIX_LEN]: do_parse_gtpu;
186 default: accept;
187 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800188 }
189
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700190 state do_parse_gtpu {
191 packet.extract(hdr.gtpu);
192 transition parse_inner_ipv4;
193 }
194
195 state parse_inner_ipv4 {
196 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700197 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700198 transition select(hdr.inner_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800199 PROTO_TCP: parse_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700200 PROTO_UDP: parse_inner_udp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800201 PROTO_ICMP: parse_icmp;
202 default: accept;
203 }
204 }
205
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700206 state parse_inner_udp {
207 packet.extract(hdr.inner_udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800208 fabric_metadata.l4_sport = hdr.inner_udp.sport;
209 fabric_metadata.l4_dport = hdr.inner_udp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900210#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700211 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900212#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800213 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900214#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800215 }
216#endif // WITH_SPGW
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700217
218#ifdef WITH_INT
219 state parse_int {
220 transition select(last_ipv4_dscp) {
221 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
222 default: accept;
223 }
224 }
225
226 state parse_intl4_shim {
227 packet.extract(hdr.intl4_shim);
228 transition parse_int_header;
229 }
230
231 state parse_int_header {
232 packet.extract(hdr.int_header);
233 // If there is no INT metadata but the INT header (plus shim and tail)
234 // exists, default value of length field in shim header should be
235 // INT_HEADER_LEN_WORDS.
236 transition select (hdr.intl4_shim.len_words) {
237 INT_HEADER_LEN_WORDS: parse_intl4_tail;
238 default: parse_int_data;
239 }
240 }
241
242 state parse_int_data {
243#ifdef WITH_INT_SINK
244 // Parse INT metadata stack, but not tail
245 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
246 transition parse_intl4_tail;
247#else // not interested in INT data
248 transition accept;
249#endif // WITH_INT_SINK
250 }
251
252 state parse_intl4_tail {
253 packet.extract(hdr.intl4_tail);
254 transition accept;
255 }
256#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700257}
258
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800259control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
260
Yi Tseng3d3956d2018-01-31 17:28:05 -0800261 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700262 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700263#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900264 packet.emit(hdr.report_ethernet);
Daniele Moro5a2de712019-09-24 14:34:07 -0700265 packet.emit(hdr.report_eth_type);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900266 packet.emit(hdr.report_ipv4);
267 packet.emit(hdr.report_udp);
268 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700269#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700270 packet.emit(hdr.ethernet);
271 packet.emit(hdr.vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700272#if defined(WITH_XCONNECT) || defined(WITH_DOUBLE_VLAN_TERMINATION)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800273 packet.emit(hdr.inner_vlan_tag);
Daniele Moro5a2de712019-09-24 14:34:07 -0700274#endif // WITH_XCONNECT || WITH_DOUBLE_VLAN_TERMINATION
275 packet.emit(hdr.eth_type);
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700276#ifdef WITH_BNG
277 packet.emit(hdr.pppoe);
278#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -0700279 packet.emit(hdr.mpls);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800280#ifdef WITH_SPGW
281 packet.emit(hdr.gtpu_ipv4);
282 packet.emit(hdr.gtpu_udp);
283 packet.emit(hdr.gtpu);
284#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700285 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800286#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700287 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800288#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700289 packet.emit(hdr.tcp);
290 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800291 packet.emit(hdr.icmp);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900292#ifdef WITH_INT
293 packet.emit(hdr.intl4_shim);
294 packet.emit(hdr.int_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700295#ifdef WITH_INT_TRANSIT
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900296 packet.emit(hdr.int_switch_id);
297 packet.emit(hdr.int_port_ids);
298 packet.emit(hdr.int_hop_latency);
299 packet.emit(hdr.int_q_occupancy);
300 packet.emit(hdr.int_ingress_tstamp);
301 packet.emit(hdr.int_egress_tstamp);
302 packet.emit(hdr.int_q_congestion);
303 packet.emit(hdr.int_egress_tx_util);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700304#endif // WITH_INT_TRANSIT
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700305#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900306 packet.emit(hdr.int_data);
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700307#endif // WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900308 packet.emit(hdr.intl4_tail);
309#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700310 }
311}
312
Daniele Moro5a2de712019-09-24 14:34:07 -0700313#endif