blob: 6aef63b86c5097ff583ebca7e1bf430803022d1a [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
22parser FabricParser (
23packet_in packet,
24out parsed_headers_t hdr,
25inout fabric_metadata_t fabric_metadata,
26inout standard_metadata_t standard_metadata) {
Yi Tseng1d842672017-11-28 16:06:52 -080027
Carmelo Cascone79a3a312018-08-16 17:14:43 -070028 bit<6> last_ipv4_dscp = 0;
29
Yi Tsengbe342052017-11-03 10:21:23 -070030 state start {
31 transition select(standard_metadata.ingress_port) {
32 CPU_PORT: parse_packet_out;
33 default: parse_ethernet;
34 }
35 }
36
37 state parse_packet_out {
38 packet.extract(hdr.packet_out);
39 transition parse_ethernet;
40 }
41
42 state parse_ethernet {
43 packet.extract(hdr.ethernet);
44 transition select(hdr.ethernet.ether_type){
Yi Tsengbe342052017-11-03 10:21:23 -070045 ETHERTYPE_VLAN: parse_vlan_tag;
46 ETHERTYPE_MPLS: parse_mpls;
47 ETHERTYPE_ARP: parse_arp;
48 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);
58 transition select(hdr.vlan_tag.ether_type){
Yi Tsengbe342052017-11-03 10:21:23 -070059 ETHERTYPE_ARP: parse_arp;
60 ETHERTYPE_IPV4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080061#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070062 ETHERTYPE_IPV6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080063#endif // WITH_IPV6
Yi Tsengbd46d052018-01-22 17:18:16 -080064 ETHERTYPE_MPLS: parse_mpls;
Yi Tsengbe342052017-11-03 10:21:23 -070065 default: accept;
66 }
67 }
68
69 state parse_mpls {
70 packet.extract(hdr.mpls);
Yi Tsengc6844f52017-12-19 11:58:25 -080071 // There is only one MPLS label for this fabric.
72 // Assume header after MPLS header is IP/IPv6
73 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -080074 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Yi Tsengbe342052017-11-03 10:21:23 -070075 //The packet should be either IPv4 or IPv6.
76 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080077#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070078 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080079#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070080 default: parse_ethernet;
81 }
82 }
83
84 state parse_ipv4 {
85 packet.extract(hdr.ipv4);
86 fabric_metadata.ip_proto = hdr.ipv4.protocol;
Carmelo Cascone79a3a312018-08-16 17:14:43 -070087 last_ipv4_dscp = hdr.ipv4.dscp;
Yi Tsengbe342052017-11-03 10:21:23 -070088 //Need header verification?
89 transition select(hdr.ipv4.protocol) {
90 PROTO_TCP: parse_tcp;
91 PROTO_UDP: parse_udp;
92 PROTO_ICMP: parse_icmp;
93 default: accept;
94 }
95 }
96
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080097#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070098 state parse_ipv6 {
99 packet.extract(hdr.ipv6);
100 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
101 transition select(hdr.ipv6.next_hdr) {
102 PROTO_TCP: parse_tcp;
103 PROTO_UDP: parse_udp;
104 PROTO_ICMPV6: parse_icmp;
105 default: accept;
106 }
107 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800108#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700109
110 state parse_arp {
111 packet.extract(hdr.arp);
112 transition accept;
113 }
114
115 state parse_tcp {
116 packet.extract(hdr.tcp);
117 fabric_metadata.l4_src_port = hdr.tcp.src_port;
118 fabric_metadata.l4_dst_port = hdr.tcp.dst_port;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900119#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700120 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900121#else
Yi Tsengbe342052017-11-03 10:21:23 -0700122 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900123#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700124 }
125
126 state parse_udp {
127 packet.extract(hdr.udp);
128 fabric_metadata.l4_src_port = hdr.udp.src_port;
129 fabric_metadata.l4_dst_port = hdr.udp.dst_port;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800130 transition select(hdr.udp.dst_port) {
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700131#ifdef WITH_SPGW
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800132 UDP_PORT_GTPU: parse_gtpu;
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700133#endif // WITH_SPGW
134#ifdef WITH_INT
135 default: parse_int;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800136#else
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700137 default: accept;
138#endif // WITH_INT
139 }
Yi Tsengbe342052017-11-03 10:21:23 -0700140 }
141
142 state parse_icmp {
143 packet.extract(hdr.icmp);
144 transition accept;
145 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800146
147#ifdef WITH_SPGW
148 state parse_gtpu {
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700149 transition select(hdr.ipv4.dst_addr[31:32-S1U_SGW_PREFIX_LEN]) {
150 // Avoid parsing GTP and inner headers if we know this GTP packet
151 // is not to be processed by this switch.
152 // FIXME: use parser value sets when support is ready in ONOS.
153 // To set the S1U_SGW_PREFIX value at runtime.
154 S1U_SGW_PREFIX[31:32-S1U_SGW_PREFIX_LEN]: do_parse_gtpu;
155 default: accept;
156 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800157 }
158
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700159 state do_parse_gtpu {
160 packet.extract(hdr.gtpu);
161 transition parse_inner_ipv4;
162 }
163
164 state parse_inner_ipv4 {
165 packet.extract(hdr.inner_ipv4);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700166 last_ipv4_dscp = hdr.inner_ipv4.dscp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700167 transition select(hdr.inner_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800168 PROTO_TCP: parse_tcp;
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700169 PROTO_UDP: parse_inner_udp;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800170 PROTO_ICMP: parse_icmp;
171 default: accept;
172 }
173 }
174
Carmelo Cascone9b0171b2018-08-14 01:43:57 -0700175 state parse_inner_udp {
176 packet.extract(hdr.inner_udp);
177 fabric_metadata.l4_src_port = hdr.inner_udp.src_port;
178 fabric_metadata.l4_dst_port = hdr.inner_udp.dst_port;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900179#ifdef WITH_INT
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700180 transition parse_int;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900181#else
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800182 transition accept;
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900183#endif // WITH_INT
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800184 }
185#endif // WITH_SPGW
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700186
187#ifdef WITH_INT
188 state parse_int {
189 transition select(last_ipv4_dscp) {
190 INT_DSCP &&& INT_DSCP: parse_intl4_shim;
191 default: accept;
192 }
193 }
194
195 state parse_intl4_shim {
196 packet.extract(hdr.intl4_shim);
197 transition parse_int_header;
198 }
199
200 state parse_int_header {
201 packet.extract(hdr.int_header);
202 // If there is no INT metadata but the INT header (plus shim and tail)
203 // exists, default value of length field in shim header should be
204 // INT_HEADER_LEN_WORDS.
205 transition select (hdr.intl4_shim.len_words) {
206 INT_HEADER_LEN_WORDS: parse_intl4_tail;
207 default: parse_int_data;
208 }
209 }
210
211 state parse_int_data {
212#ifdef WITH_INT_SINK
213 // Parse INT metadata stack, but not tail
214 packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5);
215 transition parse_intl4_tail;
216#else // not interested in INT data
217 transition accept;
218#endif // WITH_INT_SINK
219 }
220
221 state parse_intl4_tail {
222 packet.extract(hdr.intl4_tail);
223 transition accept;
224 }
225#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700226}
227
228control FabricDeparser(packet_out packet, in parsed_headers_t hdr) {
Yi Tseng3d3956d2018-01-31 17:28:05 -0800229 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700230 packet.emit(hdr.packet_in);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700231#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900232 packet.emit(hdr.report_ethernet);
233 packet.emit(hdr.report_ipv4);
234 packet.emit(hdr.report_udp);
235 packet.emit(hdr.report_fixed_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700236#endif // WITH_INT_SINK
Yi Tsengbe342052017-11-03 10:21:23 -0700237 packet.emit(hdr.ethernet);
238 packet.emit(hdr.vlan_tag);
Yi Tsengbe342052017-11-03 10:21:23 -0700239 packet.emit(hdr.mpls);
240 packet.emit(hdr.arp);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800241#ifdef WITH_SPGW
242 packet.emit(hdr.gtpu_ipv4);
243 packet.emit(hdr.gtpu_udp);
244 packet.emit(hdr.gtpu);
245#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700246 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800247#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700248 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800249#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700250 packet.emit(hdr.tcp);
251 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800252 packet.emit(hdr.icmp);
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900253#ifdef WITH_INT
254 packet.emit(hdr.intl4_shim);
255 packet.emit(hdr.int_header);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700256#ifdef WITH_INT_TRANSIT
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900257 packet.emit(hdr.int_switch_id);
258 packet.emit(hdr.int_port_ids);
259 packet.emit(hdr.int_hop_latency);
260 packet.emit(hdr.int_q_occupancy);
261 packet.emit(hdr.int_ingress_tstamp);
262 packet.emit(hdr.int_egress_tstamp);
263 packet.emit(hdr.int_q_congestion);
264 packet.emit(hdr.int_egress_tx_util);
Carmelo Cascone79a3a312018-08-16 17:14:43 -0700265#endif // WITH_INT_TRANSIT
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700266#ifdef WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900267 packet.emit(hdr.int_data);
Carmelo Cascone8e5818d2018-10-26 11:45:23 -0700268#endif // WITH_INT_SINK
Jonghwan Hyuned478dc2018-08-06 15:35:18 +0900269 packet.emit(hdr.intl4_tail);
270#endif // WITH_INT
Yi Tsengbe342052017-11-03 10:21:23 -0700271 }
272}
273
274#endif