blob: 8355c121bf3b70b40b81d1dfe4686e64130f8a33 [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;
48 ETHERTYPE_IPV6: parse_ipv6;
49 default: accept;
50 }
51 }
52
53 state parse_vlan_tag {
54 packet.extract(hdr.vlan_tag);
55 transition select(hdr.vlan_tag.ether_type){
Yi Tsengbe342052017-11-03 10:21:23 -070056 ETHERTYPE_ARP: parse_arp;
57 ETHERTYPE_IPV4: parse_ipv4;
58 ETHERTYPE_IPV6: parse_ipv6;
Yi Tsengbd46d052018-01-22 17:18:16 -080059 ETHERTYPE_MPLS: parse_mpls;
Yi Tsengbe342052017-11-03 10:21:23 -070060 default: accept;
61 }
62 }
63
64 state parse_mpls {
65 packet.extract(hdr.mpls);
Yi Tsengc6844f52017-12-19 11:58:25 -080066 // There is only one MPLS label for this fabric.
67 // Assume header after MPLS header is IP/IPv6
68 // Lookup first 4 bits for version
69 transition select(packet.lookahead<bit<4>>()) {
Yi Tsengbe342052017-11-03 10:21:23 -070070 //The packet should be either IPv4 or IPv6.
71 IP_VERSION_4: parse_ipv4;
72 IP_VERSION_6: parse_ipv6;
73 default: parse_ethernet;
74 }
75 }
76
77 state parse_ipv4 {
78 packet.extract(hdr.ipv4);
79 fabric_metadata.ip_proto = hdr.ipv4.protocol;
80 //Need header verification?
81 transition select(hdr.ipv4.protocol) {
82 PROTO_TCP: parse_tcp;
83 PROTO_UDP: parse_udp;
84 PROTO_ICMP: parse_icmp;
85 default: accept;
86 }
87 }
88
89 state parse_ipv6 {
90 packet.extract(hdr.ipv6);
91 fabric_metadata.ip_proto = hdr.ipv6.next_hdr;
92 transition select(hdr.ipv6.next_hdr) {
93 PROTO_TCP: parse_tcp;
94 PROTO_UDP: parse_udp;
95 PROTO_ICMPV6: parse_icmp;
96 default: accept;
97 }
98 }
99
100 state parse_arp {
101 packet.extract(hdr.arp);
102 transition accept;
103 }
104
105 state parse_tcp {
106 packet.extract(hdr.tcp);
107 fabric_metadata.l4_src_port = hdr.tcp.src_port;
108 fabric_metadata.l4_dst_port = hdr.tcp.dst_port;
109 transition accept;
110 }
111
112 state parse_udp {
113 packet.extract(hdr.udp);
114 fabric_metadata.l4_src_port = hdr.udp.src_port;
115 fabric_metadata.l4_dst_port = hdr.udp.dst_port;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800116#ifdef WITH_SPGW
117 transition select(hdr.udp.dst_port) {
118 UDP_PORT_GTPU: parse_gtpu;
119 default: accept;
120 }
121#else
Yi Tsengbe342052017-11-03 10:21:23 -0700122 transition accept;
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800123#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700124 }
125
126 state parse_icmp {
127 packet.extract(hdr.icmp);
128 transition accept;
129 }
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800130
131#ifdef WITH_SPGW
132 state parse_gtpu {
133 packet.extract(hdr.gtpu);
134 transition parse_ipv4_inner;
135 }
136
137 state parse_ipv4_inner {
138 hdr.gtpu_ipv4 = hdr.ipv4;
139 packet.extract(hdr.ipv4);
140 transition select(hdr.ipv4.protocol) {
141 PROTO_TCP: parse_tcp;
142 PROTO_UDP: parse_udp_inner;
143 PROTO_ICMP: parse_icmp;
144 default: accept;
145 }
146 }
147
148 state parse_udp_inner {
149 hdr.gtpu_udp = hdr.udp;
150 packet.extract(hdr.udp);
151 fabric_metadata.l4_src_port = hdr.udp.src_port;
152 fabric_metadata.l4_dst_port = hdr.udp.dst_port;
153 transition accept;
154 }
155#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700156}
157
158control FabricDeparser(packet_out packet, in parsed_headers_t hdr) {
159 apply{
160 packet.emit(hdr.packet_in);
161 packet.emit(hdr.ethernet);
162 packet.emit(hdr.vlan_tag);
Yi Tsengbe342052017-11-03 10:21:23 -0700163 packet.emit(hdr.mpls);
164 packet.emit(hdr.arp);
Carmelo Casconeb81f4be2018-01-16 23:24:01 -0800165#ifdef WITH_SPGW
166 packet.emit(hdr.gtpu_ipv4);
167 packet.emit(hdr.gtpu_udp);
168 packet.emit(hdr.gtpu);
169#endif // WITH_SPGW
Yi Tsengbe342052017-11-03 10:21:23 -0700170 packet.emit(hdr.ipv4);
171 packet.emit(hdr.ipv6);
172 packet.emit(hdr.tcp);
173 packet.emit(hdr.udp);
Yi Tsengf73a5532017-11-17 15:58:57 -0800174 packet.emit(hdr.icmp);
Yi Tsengbe342052017-11-03 10:21:23 -0700175 }
176}
177
178#endif