blob: 8b03da102e94492692d14cde84d69542451228d8 [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
Yi Tsengbe342052017-11-03 10:21:23 -070028 state start {
29 transition select(standard_metadata.ingress_port) {
30 CPU_PORT: parse_packet_out;
31 default: parse_ethernet;
32 }
33 }
34
35 state parse_packet_out {
36 packet.extract(hdr.packet_out);
37 transition parse_ethernet;
38 }
39
40 state parse_ethernet {
41 packet.extract(hdr.ethernet);
Yi Tseng1d842672017-11-28 16:06:52 -080042 fabric_metadata.original_ether_type = hdr.ethernet.ether_type;
Yi Tsengbe342052017-11-03 10:21:23 -070043 transition select(hdr.ethernet.ether_type){
Yi Tsengbe342052017-11-03 10:21:23 -070044 ETHERTYPE_VLAN: parse_vlan_tag;
45 ETHERTYPE_MPLS: parse_mpls;
46 ETHERTYPE_ARP: parse_arp;
47 ETHERTYPE_IPV4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080048#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070049 ETHERTYPE_IPV6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080050#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070051 default: accept;
52 }
53 }
54
55 state parse_vlan_tag {
56 packet.extract(hdr.vlan_tag);
57 transition select(hdr.vlan_tag.ether_type){
Yi Tsengbe342052017-11-03 10:21:23 -070058 ETHERTYPE_ARP: parse_arp;
59 ETHERTYPE_IPV4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080060#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070061 ETHERTYPE_IPV6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080062#endif // WITH_IPV6
Yi Tsengbd46d052018-01-22 17:18:16 -080063 ETHERTYPE_MPLS: parse_mpls;
Yi Tsengbe342052017-11-03 10:21:23 -070064 default: accept;
65 }
66 }
67
68 state parse_mpls {
69 packet.extract(hdr.mpls);
Yi Tsengc6844f52017-12-19 11:58:25 -080070 // There is only one MPLS label for this fabric.
71 // Assume header after MPLS header is IP/IPv6
72 // Lookup first 4 bits for version
Yi Tseng3d3956d2018-01-31 17:28:05 -080073 transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) {
Yi Tsengbe342052017-11-03 10:21:23 -070074 //The packet should be either IPv4 or IPv6.
75 IP_VERSION_4: parse_ipv4;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080076#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070077 IP_VERSION_6: parse_ipv6;
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080078#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070079 default: parse_ethernet;
80 }
81 }
82
83 state parse_ipv4 {
84 packet.extract(hdr.ipv4);
85 fabric_metadata.ip_proto = hdr.ipv4.protocol;
86 //Need header verification?
87 transition select(hdr.ipv4.protocol) {
88 PROTO_TCP: parse_tcp;
89 PROTO_UDP: parse_udp;
90 PROTO_ICMP: parse_icmp;
91 default: accept;
92 }
93 }
94
Carmelo Casconeed88f2b2018-01-26 17:36:34 -080095#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -070096 state parse_ipv6 {
97 packet.extract(hdr.ipv6);
98 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
99 transition select(hdr.ipv6.next_hdr) {
100 PROTO_TCP: parse_tcp;
101 PROTO_UDP: parse_udp;
102 PROTO_ICMPV6: parse_icmp;
103 default: accept;
104 }
105 }
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800106#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700107
108 state parse_arp {
109 packet.extract(hdr.arp);
110 transition accept;
111 }
112
113 state parse_tcp {
114 packet.extract(hdr.tcp);
115 fabric_metadata.l4_src_port = hdr.tcp.src_port;
116 fabric_metadata.l4_dst_port = hdr.tcp.dst_port;
117 transition accept;
118 }
119
120 state parse_udp {
121 packet.extract(hdr.udp);
122 fabric_metadata.l4_src_port = hdr.udp.src_port;
123 fabric_metadata.l4_dst_port = hdr.udp.dst_port;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800124#ifdef WITH_SPGW
125 transition select(hdr.udp.dst_port) {
126 UDP_PORT_GTPU: parse_gtpu;
127 default: accept;
128 }
129#else
Yi Tsengbe342052017-11-03 10:21:23 -0700130 transition accept;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800131#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700132 }
133
134 state parse_icmp {
135 packet.extract(hdr.icmp);
136 transition accept;
137 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800138
139#ifdef WITH_SPGW
140 state parse_gtpu {
141 packet.extract(hdr.gtpu);
142 transition parse_ipv4_inner;
143 }
144
145 state parse_ipv4_inner {
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800146 packet.extract(hdr.gtpu_ipv4);
147 transition select(hdr.gtpu_ipv4.protocol) {
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800148 PROTO_TCP: parse_tcp;
149 PROTO_UDP: parse_udp_inner;
150 PROTO_ICMP: parse_icmp;
151 default: accept;
152 }
153 }
154
155 state parse_udp_inner {
Carmelo Casconeb757dbc2018-01-25 17:53:17 -0800156 packet.extract(hdr.gtpu_udp);
157 fabric_metadata.l4_src_port = hdr.gtpu_udp.src_port;
158 fabric_metadata.l4_dst_port = hdr.gtpu_udp.dst_port;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800159 transition accept;
160 }
161#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700162}
163
164control FabricDeparser(packet_out packet, in parsed_headers_t hdr) {
Yi Tseng3d3956d2018-01-31 17:28:05 -0800165 apply {
Yi Tsengbe342052017-11-03 10:21:23 -0700166 packet.emit(hdr.packet_in);
167 packet.emit(hdr.ethernet);
168 packet.emit(hdr.vlan_tag);
Yi Tsengbe342052017-11-03 10:21:23 -0700169 packet.emit(hdr.mpls);
170 packet.emit(hdr.arp);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800171#ifdef WITH_SPGW
172 packet.emit(hdr.gtpu_ipv4);
173 packet.emit(hdr.gtpu_udp);
174 packet.emit(hdr.gtpu);
175#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700176 packet.emit(hdr.ipv4);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800177#ifdef WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700178 packet.emit(hdr.ipv6);
Carmelo Casconeed88f2b2018-01-26 17:36:34 -0800179#endif // WITH_IPV6
Yi Tsengbe342052017-11-03 10:21:23 -0700180 packet.emit(hdr.tcp);
181 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800182 packet.emit(hdr.icmp);
Yi Tsengbe342052017-11-03 10:21:23 -0700183 }
184}
185
186#endif