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 | |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 28 | bit<6> last_ipv4_dscp = 0; |
| 29 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 30 | 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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 45 | ETHERTYPE_VLAN: parse_vlan_tag; |
| 46 | ETHERTYPE_MPLS: parse_mpls; |
| 47 | ETHERTYPE_ARP: parse_arp; |
| 48 | ETHERTYPE_IPV4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 49 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 50 | ETHERTYPE_IPV6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 51 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 52 | 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 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; |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 87 | last_ipv4_dscp = hdr.ipv4.dscp; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 88 | //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 Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 97 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 98 | 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 Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 108 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 109 | |
| 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 Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 119 | #ifdef WITH_INT |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 120 | transition parse_int; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 121 | #else |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 122 | transition accept; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 123 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 124 | } |
| 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 Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 130 | transition select(hdr.udp.dst_port) { |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 131 | #ifdef WITH_SPGW |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 132 | UDP_PORT_GTPU: parse_gtpu; |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 133 | #endif // WITH_SPGW |
| 134 | #ifdef WITH_INT |
| 135 | default: parse_int; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 136 | #else |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 137 | default: accept; |
| 138 | #endif // WITH_INT |
| 139 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | state parse_icmp { |
| 143 | packet.extract(hdr.icmp); |
| 144 | transition accept; |
| 145 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 146 | |
| 147 | #ifdef WITH_SPGW |
| 148 | state parse_gtpu { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 149 | 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 Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 159 | 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 Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 166 | last_ipv4_dscp = hdr.inner_ipv4.dscp; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 167 | transition select(hdr.inner_ipv4.protocol) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 168 | PROTO_TCP: parse_tcp; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 169 | PROTO_UDP: parse_inner_udp; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 170 | PROTO_ICMP: parse_icmp; |
| 171 | default: accept; |
| 172 | } |
| 173 | } |
| 174 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 175 | 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 Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 179 | #ifdef WITH_INT |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 180 | transition parse_int; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 181 | #else |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 182 | transition accept; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 183 | #endif // WITH_INT |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 184 | } |
| 185 | #endif // WITH_SPGW |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 186 | |
| 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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | control FabricDeparser(packet_out packet, in parsed_headers_t hdr) { |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 229 | apply { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 230 | packet.emit(hdr.packet_in); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 231 | #ifdef WITH_INT_SINK |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 232 | 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 Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 236 | #endif // WITH_INT_SINK |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 237 | packet.emit(hdr.ethernet); |
| 238 | packet.emit(hdr.vlan_tag); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 239 | packet.emit(hdr.mpls); |
| 240 | packet.emit(hdr.arp); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 241 | #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 Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 246 | packet.emit(hdr.ipv4); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 247 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 248 | packet.emit(hdr.ipv6); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 249 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 250 | packet.emit(hdr.tcp); |
| 251 | packet.emit(hdr.udp); |
Yi Tseng | f73a553 | 2017-11-17 15:58:57 -0800 | [diff] [blame] | 252 | packet.emit(hdr.icmp); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 253 | #ifdef WITH_INT |
| 254 | packet.emit(hdr.intl4_shim); |
| 255 | packet.emit(hdr.int_header); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 256 | #ifdef WITH_INT_TRANSIT |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 257 | 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 Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 265 | #endif // WITH_INT_TRANSIT |
Carmelo Cascone | 8e5818d | 2018-10-26 11:45:23 -0700 | [diff] [blame] | 266 | #ifdef WITH_INT_SINK |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 267 | packet.emit(hdr.int_data); |
Carmelo Cascone | 8e5818d | 2018-10-26 11:45:23 -0700 | [diff] [blame] | 268 | #endif // WITH_INT_SINK |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 269 | packet.emit(hdr.intl4_tail); |
| 270 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | #endif |