Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | parser FabricParser ( |
| 23 | packet_in packet, |
| 24 | out parsed_headers_t hdr, |
| 25 | inout fabric_metadata_t fabric_metadata, |
| 26 | inout standard_metadata_t standard_metadata) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 27 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 28 | 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 Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 42 | fabric_metadata.original_ether_type = hdr.ethernet.ether_type; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 43 | transition select(hdr.ethernet.ether_type){ |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 44 | ETHERTYPE_VLAN: parse_vlan_tag; |
| 45 | ETHERTYPE_MPLS: parse_mpls; |
| 46 | ETHERTYPE_ARP: parse_arp; |
| 47 | ETHERTYPE_IPV4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 48 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 49 | ETHERTYPE_IPV6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 50 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 51 | default: accept; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | state parse_vlan_tag { |
| 56 | packet.extract(hdr.vlan_tag); |
Yi Tseng | 20f9e7b | 2018-05-24 23:27:39 +0800 | [diff] [blame] | 57 | fabric_metadata.original_ether_type = hdr.vlan_tag.ether_type; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 58 | transition select(hdr.vlan_tag.ether_type){ |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 59 | ETHERTYPE_ARP: parse_arp; |
| 60 | ETHERTYPE_IPV4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 61 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 62 | ETHERTYPE_IPV6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 63 | #endif // WITH_IPV6 |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 64 | ETHERTYPE_MPLS: parse_mpls; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 65 | default: accept; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | state parse_mpls { |
| 70 | packet.extract(hdr.mpls); |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 71 | // 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 Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 74 | transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 75 | //The packet should be either IPv4 or IPv6. |
| 76 | IP_VERSION_4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 77 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 78 | IP_VERSION_6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 79 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 80 | default: parse_ethernet; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | state parse_ipv4 { |
| 85 | packet.extract(hdr.ipv4); |
| 86 | fabric_metadata.ip_proto = hdr.ipv4.protocol; |
| 87 | //Need header verification? |
| 88 | transition select(hdr.ipv4.protocol) { |
| 89 | PROTO_TCP: parse_tcp; |
| 90 | PROTO_UDP: parse_udp; |
| 91 | PROTO_ICMP: parse_icmp; |
| 92 | default: accept; |
| 93 | } |
| 94 | } |
| 95 | |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 96 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 97 | state parse_ipv6 { |
| 98 | packet.extract(hdr.ipv6); |
| 99 | fabric_metadata.ip_proto = hdr.ipv6.next_hdr; |
| 100 | transition select(hdr.ipv6.next_hdr) { |
| 101 | PROTO_TCP: parse_tcp; |
| 102 | PROTO_UDP: parse_udp; |
| 103 | PROTO_ICMPV6: parse_icmp; |
| 104 | default: accept; |
| 105 | } |
| 106 | } |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 107 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 108 | |
| 109 | state parse_arp { |
| 110 | packet.extract(hdr.arp); |
| 111 | transition accept; |
| 112 | } |
| 113 | |
| 114 | state parse_tcp { |
| 115 | packet.extract(hdr.tcp); |
| 116 | fabric_metadata.l4_src_port = hdr.tcp.src_port; |
| 117 | fabric_metadata.l4_dst_port = hdr.tcp.dst_port; |
| 118 | transition accept; |
| 119 | } |
| 120 | |
| 121 | state parse_udp { |
| 122 | packet.extract(hdr.udp); |
| 123 | fabric_metadata.l4_src_port = hdr.udp.src_port; |
| 124 | fabric_metadata.l4_dst_port = hdr.udp.dst_port; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 125 | #ifdef WITH_SPGW |
| 126 | transition select(hdr.udp.dst_port) { |
| 127 | UDP_PORT_GTPU: parse_gtpu; |
| 128 | default: accept; |
| 129 | } |
| 130 | #else |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 131 | transition accept; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 132 | #endif // WITH_SPGW |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | state parse_icmp { |
| 136 | packet.extract(hdr.icmp); |
| 137 | transition accept; |
| 138 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 139 | |
| 140 | #ifdef WITH_SPGW |
| 141 | state parse_gtpu { |
| 142 | packet.extract(hdr.gtpu); |
| 143 | transition parse_ipv4_inner; |
| 144 | } |
| 145 | |
| 146 | state parse_ipv4_inner { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 147 | packet.extract(hdr.gtpu_ipv4); |
| 148 | transition select(hdr.gtpu_ipv4.protocol) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 149 | PROTO_TCP: parse_tcp; |
| 150 | PROTO_UDP: parse_udp_inner; |
| 151 | PROTO_ICMP: parse_icmp; |
| 152 | default: accept; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | state parse_udp_inner { |
Carmelo Cascone | b757dbc | 2018-01-25 17:53:17 -0800 | [diff] [blame] | 157 | packet.extract(hdr.gtpu_udp); |
| 158 | fabric_metadata.l4_src_port = hdr.gtpu_udp.src_port; |
| 159 | fabric_metadata.l4_dst_port = hdr.gtpu_udp.dst_port; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 160 | transition accept; |
| 161 | } |
| 162 | #endif // WITH_SPGW |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | control FabricDeparser(packet_out packet, in parsed_headers_t hdr) { |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 166 | apply { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 167 | packet.emit(hdr.packet_in); |
| 168 | packet.emit(hdr.ethernet); |
| 169 | packet.emit(hdr.vlan_tag); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 170 | packet.emit(hdr.mpls); |
| 171 | packet.emit(hdr.arp); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 172 | #ifdef WITH_SPGW |
| 173 | packet.emit(hdr.gtpu_ipv4); |
| 174 | packet.emit(hdr.gtpu_udp); |
| 175 | packet.emit(hdr.gtpu); |
| 176 | #endif // WITH_SPGW |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 177 | packet.emit(hdr.ipv4); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 178 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 179 | packet.emit(hdr.ipv6); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 180 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 181 | packet.emit(hdr.tcp); |
| 182 | packet.emit(hdr.udp); |
Yi Tseng | f73a553 | 2017-11-17 15:58:57 -0800 | [diff] [blame] | 183 | packet.emit(hdr.icmp); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
| 187 | #endif |