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 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 22 | parser FabricParser (packet_in packet, |
| 23 | out parsed_headers_t hdr, |
| 24 | inout fabric_metadata_t fabric_metadata, |
| 25 | inout standard_metadata_t standard_metadata) { |
Yi Tseng | 1d84267 | 2017-11-28 16:06:52 -0800 | [diff] [blame] | 26 | |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 27 | bit<6> last_ipv4_dscp = 0; |
| 28 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 29 | state start { |
| 30 | transition select(standard_metadata.ingress_port) { |
| 31 | CPU_PORT: parse_packet_out; |
| 32 | default: parse_ethernet; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | state parse_packet_out { |
| 37 | packet.extract(hdr.packet_out); |
| 38 | transition parse_ethernet; |
| 39 | } |
| 40 | |
| 41 | state parse_ethernet { |
| 42 | packet.extract(hdr.ethernet); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 43 | fabric_metadata.eth_type = hdr.ethernet.eth_type; |
| 44 | fabric_metadata.vlan_id = DEFAULT_VLAN_ID; |
| 45 | transition select(hdr.ethernet.eth_type){ |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 46 | ETHERTYPE_VLAN: parse_vlan_tag; |
| 47 | ETHERTYPE_MPLS: parse_mpls; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 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); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 58 | transition select(hdr.vlan_tag.eth_type){ |
| 59 | ETHERTYPE_IPV4: parse_ipv4; |
| 60 | #ifdef WITH_IPV6 |
| 61 | ETHERTYPE_IPV6: parse_ipv6; |
| 62 | #endif // WITH_IPV6 |
| 63 | ETHERTYPE_MPLS: parse_mpls; |
| 64 | #ifdef WITH_XCONNECT |
| 65 | ETHERTYPE_VLAN: parse_inner_vlan_tag; |
| 66 | #endif // WITH_XCONNECT |
| 67 | default: accept; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | #ifdef WITH_XCONNECT |
| 72 | state parse_inner_vlan_tag { |
| 73 | packet.extract(hdr.inner_vlan_tag); |
| 74 | transition select(hdr.inner_vlan_tag.eth_type){ |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 75 | ETHERTYPE_IPV4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 76 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 77 | ETHERTYPE_IPV6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 78 | #endif // WITH_IPV6 |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 79 | ETHERTYPE_MPLS: parse_mpls; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 80 | default: accept; |
| 81 | } |
| 82 | } |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 83 | #endif // WITH_XCONNECT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 84 | |
| 85 | state parse_mpls { |
| 86 | packet.extract(hdr.mpls); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 87 | fabric_metadata.mpls_label = hdr.mpls.label; |
| 88 | fabric_metadata.mpls_ttl = hdr.mpls.ttl; |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 89 | // There is only one MPLS label for this fabric. |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 90 | // Assume header after MPLS header is IPv4/IPv6 |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 91 | // Lookup first 4 bits for version |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 92 | transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 93 | //The packet should be either IPv4 or IPv6. |
| 94 | IP_VERSION_4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 95 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 96 | IP_VERSION_6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 97 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 98 | default: parse_ethernet; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | state parse_ipv4 { |
| 103 | packet.extract(hdr.ipv4); |
| 104 | fabric_metadata.ip_proto = hdr.ipv4.protocol; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 105 | fabric_metadata.ip_eth_type = ETHERTYPE_IPV4; |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 106 | last_ipv4_dscp = hdr.ipv4.dscp; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 107 | //Need header verification? |
| 108 | transition select(hdr.ipv4.protocol) { |
| 109 | PROTO_TCP: parse_tcp; |
| 110 | PROTO_UDP: parse_udp; |
| 111 | PROTO_ICMP: parse_icmp; |
| 112 | default: accept; |
| 113 | } |
| 114 | } |
| 115 | |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 116 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 117 | state parse_ipv6 { |
| 118 | packet.extract(hdr.ipv6); |
| 119 | fabric_metadata.ip_proto = hdr.ipv6.next_hdr; |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 120 | fabric_metadata.ip_eth_type = ETHERTYPE_IPV6; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 121 | transition select(hdr.ipv6.next_hdr) { |
| 122 | PROTO_TCP: parse_tcp; |
| 123 | PROTO_UDP: parse_udp; |
| 124 | PROTO_ICMPV6: parse_icmp; |
| 125 | default: accept; |
| 126 | } |
| 127 | } |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 128 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 129 | |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 130 | state parse_tcp { |
| 131 | packet.extract(hdr.tcp); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 132 | fabric_metadata.l4_sport = hdr.tcp.sport; |
| 133 | fabric_metadata.l4_dport = hdr.tcp.dport; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 134 | #ifdef WITH_INT |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 135 | transition parse_int; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 136 | #else |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 137 | transition accept; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 138 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | state parse_udp { |
| 142 | packet.extract(hdr.udp); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 143 | fabric_metadata.l4_sport = hdr.udp.sport; |
| 144 | fabric_metadata.l4_dport = hdr.udp.dport; |
| 145 | transition select(hdr.udp.dport) { |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 146 | #ifdef WITH_SPGW |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 147 | UDP_PORT_GTPU: parse_gtpu; |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 148 | #endif // WITH_SPGW |
| 149 | #ifdef WITH_INT |
| 150 | default: parse_int; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 151 | #else |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 152 | default: accept; |
| 153 | #endif // WITH_INT |
| 154 | } |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | state parse_icmp { |
| 158 | packet.extract(hdr.icmp); |
| 159 | transition accept; |
| 160 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 161 | |
| 162 | #ifdef WITH_SPGW |
| 163 | state parse_gtpu { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 164 | transition select(hdr.ipv4.dst_addr[31:32-S1U_SGW_PREFIX_LEN]) { |
| 165 | // Avoid parsing GTP and inner headers if we know this GTP packet |
| 166 | // is not to be processed by this switch. |
| 167 | // FIXME: use parser value sets when support is ready in ONOS. |
| 168 | // To set the S1U_SGW_PREFIX value at runtime. |
| 169 | S1U_SGW_PREFIX[31:32-S1U_SGW_PREFIX_LEN]: do_parse_gtpu; |
| 170 | default: accept; |
| 171 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 174 | state do_parse_gtpu { |
| 175 | packet.extract(hdr.gtpu); |
| 176 | transition parse_inner_ipv4; |
| 177 | } |
| 178 | |
| 179 | state parse_inner_ipv4 { |
| 180 | packet.extract(hdr.inner_ipv4); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 181 | last_ipv4_dscp = hdr.inner_ipv4.dscp; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 182 | transition select(hdr.inner_ipv4.protocol) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 183 | PROTO_TCP: parse_tcp; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 184 | PROTO_UDP: parse_inner_udp; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 185 | PROTO_ICMP: parse_icmp; |
| 186 | default: accept; |
| 187 | } |
| 188 | } |
| 189 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 190 | state parse_inner_udp { |
| 191 | packet.extract(hdr.inner_udp); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 192 | fabric_metadata.l4_sport = hdr.inner_udp.sport; |
| 193 | fabric_metadata.l4_dport = hdr.inner_udp.dport; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 194 | #ifdef WITH_INT |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 195 | transition parse_int; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 196 | #else |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 197 | transition accept; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 198 | #endif // WITH_INT |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 199 | } |
| 200 | #endif // WITH_SPGW |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 201 | |
| 202 | #ifdef WITH_INT |
| 203 | state parse_int { |
| 204 | transition select(last_ipv4_dscp) { |
| 205 | INT_DSCP &&& INT_DSCP: parse_intl4_shim; |
| 206 | default: accept; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | state parse_intl4_shim { |
| 211 | packet.extract(hdr.intl4_shim); |
| 212 | transition parse_int_header; |
| 213 | } |
| 214 | |
| 215 | state parse_int_header { |
| 216 | packet.extract(hdr.int_header); |
| 217 | // If there is no INT metadata but the INT header (plus shim and tail) |
| 218 | // exists, default value of length field in shim header should be |
| 219 | // INT_HEADER_LEN_WORDS. |
| 220 | transition select (hdr.intl4_shim.len_words) { |
| 221 | INT_HEADER_LEN_WORDS: parse_intl4_tail; |
| 222 | default: parse_int_data; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | state parse_int_data { |
| 227 | #ifdef WITH_INT_SINK |
| 228 | // Parse INT metadata stack, but not tail |
| 229 | packet.extract(hdr.int_data, (bit<32>) (hdr.intl4_shim.len_words - INT_HEADER_LEN_WORDS) << 5); |
| 230 | transition parse_intl4_tail; |
| 231 | #else // not interested in INT data |
| 232 | transition accept; |
| 233 | #endif // WITH_INT_SINK |
| 234 | } |
| 235 | |
| 236 | state parse_intl4_tail { |
| 237 | packet.extract(hdr.intl4_tail); |
| 238 | transition accept; |
| 239 | } |
| 240 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 243 | control FabricDeparser(packet_out packet,in parsed_headers_t hdr) { |
| 244 | |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 245 | apply { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 246 | packet.emit(hdr.packet_in); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 247 | #ifdef WITH_INT_SINK |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 248 | packet.emit(hdr.report_ethernet); |
| 249 | packet.emit(hdr.report_ipv4); |
| 250 | packet.emit(hdr.report_udp); |
| 251 | packet.emit(hdr.report_fixed_header); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 252 | #endif // WITH_INT_SINK |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 253 | packet.emit(hdr.ethernet); |
| 254 | packet.emit(hdr.vlan_tag); |
Carmelo Cascone | b5324e7 | 2018-11-25 02:26:32 -0800 | [diff] [blame] | 255 | #ifdef WITH_XCONNECT |
| 256 | packet.emit(hdr.inner_vlan_tag); |
| 257 | #endif // WITH_XCONNECT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 258 | packet.emit(hdr.mpls); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 259 | #ifdef WITH_SPGW |
| 260 | packet.emit(hdr.gtpu_ipv4); |
| 261 | packet.emit(hdr.gtpu_udp); |
| 262 | packet.emit(hdr.gtpu); |
| 263 | #endif // WITH_SPGW |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 264 | packet.emit(hdr.ipv4); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 265 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 266 | packet.emit(hdr.ipv6); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 267 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 268 | packet.emit(hdr.tcp); |
| 269 | packet.emit(hdr.udp); |
Yi Tseng | f73a553 | 2017-11-17 15:58:57 -0800 | [diff] [blame] | 270 | packet.emit(hdr.icmp); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 271 | #ifdef WITH_INT |
| 272 | packet.emit(hdr.intl4_shim); |
| 273 | packet.emit(hdr.int_header); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 274 | #ifdef WITH_INT_TRANSIT |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 275 | packet.emit(hdr.int_switch_id); |
| 276 | packet.emit(hdr.int_port_ids); |
| 277 | packet.emit(hdr.int_hop_latency); |
| 278 | packet.emit(hdr.int_q_occupancy); |
| 279 | packet.emit(hdr.int_ingress_tstamp); |
| 280 | packet.emit(hdr.int_egress_tstamp); |
| 281 | packet.emit(hdr.int_q_congestion); |
| 282 | packet.emit(hdr.int_egress_tx_util); |
Carmelo Cascone | 79a3a31 | 2018-08-16 17:14:43 -0700 | [diff] [blame] | 283 | #endif // WITH_INT_TRANSIT |
Carmelo Cascone | 8e5818d | 2018-10-26 11:45:23 -0700 | [diff] [blame] | 284 | #ifdef WITH_INT_SINK |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 285 | packet.emit(hdr.int_data); |
Carmelo Cascone | 8e5818d | 2018-10-26 11:45:23 -0700 | [diff] [blame] | 286 | #endif // WITH_INT_SINK |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 287 | packet.emit(hdr.intl4_tail); |
| 288 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | #endif |