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); |
| 42 | transition select(hdr.ethernet.ether_type){ |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 43 | ETHERTYPE_VLAN: parse_vlan_tag; |
| 44 | ETHERTYPE_MPLS: parse_mpls; |
| 45 | ETHERTYPE_ARP: parse_arp; |
| 46 | ETHERTYPE_IPV4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 47 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 48 | ETHERTYPE_IPV6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 49 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 50 | default: accept; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | state parse_vlan_tag { |
| 55 | packet.extract(hdr.vlan_tag); |
| 56 | transition select(hdr.vlan_tag.ether_type){ |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 57 | ETHERTYPE_ARP: parse_arp; |
| 58 | ETHERTYPE_IPV4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 59 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 60 | ETHERTYPE_IPV6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 61 | #endif // WITH_IPV6 |
Yi Tseng | bd46d05 | 2018-01-22 17:18:16 -0800 | [diff] [blame] | 62 | ETHERTYPE_MPLS: parse_mpls; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 63 | default: accept; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | state parse_mpls { |
| 68 | packet.extract(hdr.mpls); |
Yi Tseng | c6844f5 | 2017-12-19 11:58:25 -0800 | [diff] [blame] | 69 | // There is only one MPLS label for this fabric. |
| 70 | // Assume header after MPLS header is IP/IPv6 |
| 71 | // Lookup first 4 bits for version |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 72 | transition select(packet.lookahead<bit<IP_VER_LENGTH>>()) { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 73 | //The packet should be either IPv4 or IPv6. |
| 74 | IP_VERSION_4: parse_ipv4; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 75 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 76 | IP_VERSION_6: parse_ipv6; |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 77 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 78 | default: parse_ethernet; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | state parse_ipv4 { |
| 83 | packet.extract(hdr.ipv4); |
| 84 | fabric_metadata.ip_proto = hdr.ipv4.protocol; |
| 85 | //Need header verification? |
| 86 | transition select(hdr.ipv4.protocol) { |
| 87 | PROTO_TCP: parse_tcp; |
| 88 | PROTO_UDP: parse_udp; |
| 89 | PROTO_ICMP: parse_icmp; |
| 90 | default: accept; |
| 91 | } |
| 92 | } |
| 93 | |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 94 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 95 | state parse_ipv6 { |
| 96 | packet.extract(hdr.ipv6); |
| 97 | fabric_metadata.ip_proto = hdr.ipv6.next_hdr; |
| 98 | transition select(hdr.ipv6.next_hdr) { |
| 99 | PROTO_TCP: parse_tcp; |
| 100 | PROTO_UDP: parse_udp; |
| 101 | PROTO_ICMPV6: parse_icmp; |
| 102 | default: accept; |
| 103 | } |
| 104 | } |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 105 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 106 | |
| 107 | state parse_arp { |
| 108 | packet.extract(hdr.arp); |
| 109 | transition accept; |
| 110 | } |
| 111 | |
| 112 | state parse_tcp { |
| 113 | packet.extract(hdr.tcp); |
| 114 | fabric_metadata.l4_src_port = hdr.tcp.src_port; |
| 115 | fabric_metadata.l4_dst_port = hdr.tcp.dst_port; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 116 | #ifdef WITH_INT |
| 117 | transition select(hdr.ipv4.isValid() && ((hdr.ipv4.dscp & INT_DSCP) == INT_DSCP)) { |
| 118 | true: parse_intl4_shim; |
| 119 | default: accept; |
| 120 | } |
| 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 | #ifdef WITH_SPGW |
| 131 | transition select(hdr.udp.dst_port) { |
| 132 | UDP_PORT_GTPU: parse_gtpu; |
| 133 | default: accept; |
| 134 | } |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 135 | #elif WITH_INT |
| 136 | transition select(hdr.ipv4.isValid() && (hdr.ipv4.dscp & INT_DSCP) == INT_DSCP) { |
| 137 | true: parse_intl4_shim; |
| 138 | default: accept; |
| 139 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 140 | #else |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 141 | transition accept; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 142 | #endif // WITH_SPGW, WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | state parse_icmp { |
| 146 | packet.extract(hdr.icmp); |
| 147 | transition accept; |
| 148 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 149 | |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 150 | #ifdef WITH_INT |
| 151 | state parse_intl4_shim { |
| 152 | packet.extract(hdr.intl4_shim); |
| 153 | transition parse_int_header; |
| 154 | } |
| 155 | |
| 156 | state parse_int_header { |
| 157 | packet.extract(hdr.int_header); |
| 158 | // If there is no INT metadata but the INT header (and corresponding shim header |
| 159 | // and tail header) exists, default value of length field in shim header |
| 160 | // should be INT_HEADER_LEN_WORD. |
| 161 | fabric_metadata.int_meta.metadata_len = hdr.intl4_shim.len - INT_HEADER_LEN_WORD; |
| 162 | transition select (fabric_metadata.int_meta.metadata_len) { |
| 163 | 0: parse_intl4_tail; |
| 164 | default: parse_int_data; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | state parse_int_data { |
| 169 | // Parse INT metadata, not INT header, INT shim header and INT tail header |
| 170 | packet.extract(hdr.int_data, (bit<32>) ((hdr.intl4_shim.len - INT_HEADER_LEN_WORD) << 5)); |
| 171 | transition parse_intl4_tail; |
| 172 | } |
| 173 | |
| 174 | state parse_intl4_tail { |
| 175 | packet.extract(hdr.intl4_tail); |
| 176 | transition accept; |
| 177 | } |
| 178 | #endif // WITH_INT |
| 179 | |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 180 | #ifdef WITH_SPGW |
| 181 | state parse_gtpu { |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 182 | transition select(hdr.ipv4.dst_addr[31:32-S1U_SGW_PREFIX_LEN]) { |
| 183 | // Avoid parsing GTP and inner headers if we know this GTP packet |
| 184 | // is not to be processed by this switch. |
| 185 | // FIXME: use parser value sets when support is ready in ONOS. |
| 186 | // To set the S1U_SGW_PREFIX value at runtime. |
| 187 | S1U_SGW_PREFIX[31:32-S1U_SGW_PREFIX_LEN]: do_parse_gtpu; |
| 188 | default: accept; |
| 189 | } |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 192 | state do_parse_gtpu { |
| 193 | packet.extract(hdr.gtpu); |
| 194 | transition parse_inner_ipv4; |
| 195 | } |
| 196 | |
| 197 | state parse_inner_ipv4 { |
| 198 | packet.extract(hdr.inner_ipv4); |
| 199 | transition select(hdr.inner_ipv4.protocol) { |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 200 | PROTO_TCP: parse_tcp; |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 201 | PROTO_UDP: parse_inner_udp; |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 202 | PROTO_ICMP: parse_icmp; |
| 203 | default: accept; |
| 204 | } |
| 205 | } |
| 206 | |
Carmelo Cascone | 9b0171b | 2018-08-14 01:43:57 -0700 | [diff] [blame] | 207 | state parse_inner_udp { |
| 208 | packet.extract(hdr.inner_udp); |
| 209 | fabric_metadata.l4_src_port = hdr.inner_udp.src_port; |
| 210 | fabric_metadata.l4_dst_port = hdr.inner_udp.dst_port; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 211 | #ifdef WITH_INT |
| 212 | transition select(hdr.ipv4.isValid() && (hdr.ipv4.dscp & INT_DSCP) == INT_DSCP) { |
| 213 | true: parse_intl4_shim; |
| 214 | default: accept; |
| 215 | } |
| 216 | #else |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 217 | transition accept; |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 218 | #endif // WITH_INT |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 219 | } |
| 220 | #endif // WITH_SPGW |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | control FabricDeparser(packet_out packet, in parsed_headers_t hdr) { |
Yi Tseng | 3d3956d | 2018-01-31 17:28:05 -0800 | [diff] [blame] | 224 | apply { |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 225 | packet.emit(hdr.packet_in); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 226 | #ifdef WITH_INT |
| 227 | packet.emit(hdr.report_ethernet); |
| 228 | packet.emit(hdr.report_ipv4); |
| 229 | packet.emit(hdr.report_udp); |
| 230 | packet.emit(hdr.report_fixed_header); |
| 231 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 232 | packet.emit(hdr.ethernet); |
| 233 | packet.emit(hdr.vlan_tag); |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 234 | packet.emit(hdr.mpls); |
| 235 | packet.emit(hdr.arp); |
Carmelo Cascone | b81f4be | 2018-01-16 23:24:01 -0800 | [diff] [blame] | 236 | #ifdef WITH_SPGW |
| 237 | packet.emit(hdr.gtpu_ipv4); |
| 238 | packet.emit(hdr.gtpu_udp); |
| 239 | packet.emit(hdr.gtpu); |
| 240 | #endif // WITH_SPGW |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 241 | packet.emit(hdr.ipv4); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 242 | #ifdef WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 243 | packet.emit(hdr.ipv6); |
Carmelo Cascone | ed88f2b | 2018-01-26 17:36:34 -0800 | [diff] [blame] | 244 | #endif // WITH_IPV6 |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 245 | packet.emit(hdr.tcp); |
| 246 | packet.emit(hdr.udp); |
Yi Tseng | f73a553 | 2017-11-17 15:58:57 -0800 | [diff] [blame] | 247 | packet.emit(hdr.icmp); |
Jonghwan Hyun | ed478dc | 2018-08-06 15:35:18 +0900 | [diff] [blame] | 248 | #ifdef WITH_INT |
| 249 | packet.emit(hdr.intl4_shim); |
| 250 | packet.emit(hdr.int_header); |
| 251 | packet.emit(hdr.int_switch_id); |
| 252 | packet.emit(hdr.int_port_ids); |
| 253 | packet.emit(hdr.int_hop_latency); |
| 254 | packet.emit(hdr.int_q_occupancy); |
| 255 | packet.emit(hdr.int_ingress_tstamp); |
| 256 | packet.emit(hdr.int_egress_tstamp); |
| 257 | packet.emit(hdr.int_q_congestion); |
| 258 | packet.emit(hdr.int_egress_tx_util); |
| 259 | packet.emit(hdr.int_data); |
| 260 | packet.emit(hdr.intl4_tail); |
| 261 | #endif // WITH_INT |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | #endif |