blob: e23467656aa9672c116a42bacea075f180daa19a [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.eth_type = hdr.ethernet.eth_type;
44 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;
Yi Tsengbe342052017-11-03 10:21:23 -070048 ETHERTYPE_IPV4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080049#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070050 ETHERTYPE_IPV6: 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){
59 ETHERTYPE_IPV4: parse_ipv4;
60#ifdef WITH_IPV6
61 ETHERTYPE_IPV6: parse_ipv6;
62#endif // WITH_IPV6
63 ETHERTYPE_MPLS: parse_mpls;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070064#if defined(WITH_XCONNECT) || defined(WITH_BNG)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080065 ETHERTYPE_VLAN: parse_inner_vlan_tag;
66#endif // WITH_XCONNECT
67 default: accept;
68 }
69 }
70
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070071#if defined(WITH_XCONNECT) || defined(WITH_BNG)
Carmelo Casconeb5324e72018-11-25 02:26:32 -080072 state parse_inner_vlan_tag {
73 packet.extract(hdr.inner_vlan_tag);
74 transition select(hdr.inner_vlan_tag.eth_type){
Yi Tsengbe342052017-11-03 10:21:23 -070075 ETHERTYPE_IPV4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080076#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070077 ETHERTYPE_IPV6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080078#endif // WITH_IPV6
Yi Tsengbd46d052018-01-22 17:18:16 -080079 ETHERTYPE_MPLS: parse_mpls;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070080#ifdef WITH_BNG
81 ETHERTYPE_PPPOED: parse_pppoe;
82 ETHERTYPE_PPPOES: parse_pppoe;
83#endif // WITH_BNG
Yi Tsengbe342052017-11-03 10:21:23 -070084 default: accept;
85 }
86 }
Carmelo Casconeb5324e72018-11-25 02:26:32 -080087#endif // WITH_XCONNECT
Yi Tsengbe342052017-11-03 10:21:23 -070088
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070089#ifdef WITH_BNG
90 state parse_pppoe {
91 packet.extract(hdr.pppoe);
92 transition select(hdr.pppoe.protocol) {
Daniele Moroe22b5742019-06-28 15:32:37 -070093 PPPOE_PROTOCOL_MPLS: parse_mpls;
Carmelo Cascone4d8785b2019-05-31 17:11:26 -070094 PPPOE_PROTOCOL_IP4: parse_ipv4;
95#ifdef WITH_IPV6
96 PPPOE_PROTOCOL_IP6: parse_ipv6;
97#endif // WITH_IPV6
98 default: accept;
99 }
100 }
101#endif // WITH_BNG
102
Yi Tsengbe342052017-11-03 10:21:23 -0700103 state parse_mpls {
104 packet.extract(hdr.mpls);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800105 fabric_metadata.mpls_label = hdr.mpls.label;
106 fabric_metadata.mpls_ttl = hdr.mpls.ttl;
Yi Tsengc6844f52017-12-19 11:58:25 -0800107 // There is only one MPLS label for this fabric.
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800108 // Assume header after MPLS header is IPv4/IPv6
Yi Tsengc6844f52017-12-19 11:58:25 -0800109 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -0800110 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Yi Tsengbe342052017-11-03 10:21:23 -0700111 //The packet should be either IPv4 or IPv6.
112 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800113#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700114 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800115#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700116 default: parse_ethernet;
117 }
118 }
119
120 state parse_ipv4 {
121 packet.extract(hdr.ipv4);
122 fabric_metadata.ip_proto = hdr.ipv4.protocol;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800123 fabric_metadata.ip_eth_type = ETHERTYPE_IPV4;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700124 last_ipv4_dscp = hdr.ipv4.dscp;
Yi Tsengbe342052017-11-03 10:21:23 -0700125 //Need header verification?
126 transition select(hdr.ipv4.protocol) {
127 PROTO_TCP: parse_tcp;
128 PROTO_UDP: parse_udp;
129 PROTO_ICMP: parse_icmp;
130 default: accept;
131 }
132 }
133
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800134#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700135 state parse_ipv6 {
136 packet.extract(hdr.ipv6);
137 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800138 fabric_metadata.ip_eth_type = ETHERTYPE_IPV6;
Yi Tsengbe342052017-11-03 10:21:23 -0700139 transition select(hdr.ipv6.next_hdr) {
140 PROTO_TCP: parse_tcp;
141 PROTO_UDP: parse_udp;
142 PROTO_ICMPV6: parse_icmp;
143 default: accept;
144 }
145 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800146#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700147
Yi Tsengbe342052017-11-03 10:21:23 -0700148 state parse_tcp {
149 packet.extract(hdr.tcp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800150 fabric_metadata.l4_sport = hdr.tcp.sport;
151 fabric_metadata.l4_dport = hdr.tcp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900152#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700153 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900154#else
Yi Tsengbe342052017-11-03 10:21:23 -0700155 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900156#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700157 }
158
159 state parse_udp {
160 packet.extract(hdr.udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800161 fabric_metadata.l4_sport = hdr.udp.sport;
162 fabric_metadata.l4_dport = hdr.udp.dport;
163 transition select(hdr.udp.dport) {
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700164#ifdef WITH_SPGW
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800165 UDP_PORT_GTPU: parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700166#endif // WITH_SPGW
167#ifdef WITH_INT
168 default: parse_int;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800169#else
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700170 default: accept;
171#endif // WITH_INT
172 }
Yi Tsengbe342052017-11-03 10:21:23 -0700173 }
174
175 state parse_icmp {
176 packet.extract(hdr.icmp);
177 transition accept;
178 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800179
180#ifdef WITH_SPGW
181 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700182 transition select(hdr.ipv4.dst_addr[31:32-S1U_SGW_PREFIX_LEN]) {
183 // Avoid parsing GTP and inner headers if we know this GTP packet
184 // is not to be processed by this switch.
185 // FIXME: use parser value sets when support is ready in ONOS.
186 // To set the S1U_SGW_PREFIX value at runtime.
187 S1U_SGW_PREFIX[31:32-S1U_SGW_PREFIX_LEN]: do_parse_gtpu;
188 default: accept;
189 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800190 }
191
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700192 state do_parse_gtpu {
193 packet.extract(hdr.gtpu);
194 transition parse_inner_ipv4;
195 }
196
197 state parse_inner_ipv4 {
198 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700199 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700200 transition select(hdr.inner_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800201 PROTO_TCP: parse_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700202 PROTO_UDP: parse_inner_udp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800203 PROTO_ICMP: parse_icmp;
204 default: accept;
205 }
206 }
207
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700208 state parse_inner_udp {
209 packet.extract(hdr.inner_udp);
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800210 fabric_metadata.l4_sport = hdr.inner_udp.sport;
211 fabric_metadata.l4_dport = hdr.inner_udp.dport;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900212#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700213 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900214#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800215 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900216#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800217 }
218#endif // WITH_SPGW
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700219
220#ifdef WITH_INT
221 state parse_int {
222 transition select(last_ipv4_dscp) {
223 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
224 default: accept;
225 }
226 }
227
228 state parse_intl4_shim {
229 packet.extract(hdr.intl4_shim);
230 transition parse_int_header;
231 }
232
233 state parse_int_header {
234 packet.extract(hdr.int_header);
235 // If there is no INT metadata but the INT header (plus shim and tail)
236 // exists, default value of length field in shim header should be
237 // INT_HEADER_LEN_WORDS.
238 transition select (hdr.intl4_shim.len_words) {
239 INT_HEADER_LEN_WORDS: parse_intl4_tail;
240 default: parse_int_data;
241 }
242 }
243
244 state parse_int_data {
245#ifdef WITH_INT_SINK
246 // Parse INT metadata stack, but not tail
247 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
248 transition parse_intl4_tail;
249#else // not interested in INT data
250 transition accept;
251#endif // WITH_INT_SINK
252 }
253
254 state parse_intl4_tail {
255 packet.extract(hdr.intl4_tail);
256 transition accept;
257 }
258#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700259}
260
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800261control FabricDeparser(packet_out packet,in parsed_headers_t hdr) {
262
Yi Tseng3d3956d2018-01-31 17:28:05 -0800263 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700264 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700265#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900266 packet.emit(hdr.report_ethernet);
267 packet.emit(hdr.report_ipv4);
268 packet.emit(hdr.report_udp);
269 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700270#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700271 packet.emit(hdr.ethernet);
272 packet.emit(hdr.vlan_tag);
Carmelo Cascone4d8785b2019-05-31 17:11:26 -0700273#if defined(WITH_XCONNECT) || defined(WITH_BNG)
Carmelo Casconeb5324e72018-11-25 02:26:32 -0800274 packet.emit(hdr.inner_vlan_tag);
275#endif // WITH_XCONNECT
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
313#endif