Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * decap.c: vxlan tunnel decap packet processing |
| 3 | * |
| 4 | * Copyright (c) 2013 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vnet/pg/pg.h> |
| 20 | #include <vnet/vxlan/vxlan.h> |
| 21 | |
John Lo | 13e3d45 | 2016-08-09 19:20:51 -0400 | [diff] [blame] | 22 | vlib_node_registration_t vxlan4_input_node; |
| 23 | vlib_node_registration_t vxlan6_input_node; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 24 | |
| 25 | typedef struct { |
| 26 | u32 next_index; |
| 27 | u32 tunnel_index; |
| 28 | u32 error; |
| 29 | u32 vni; |
| 30 | } vxlan_rx_trace_t; |
| 31 | |
| 32 | static u8 * format_vxlan_rx_trace (u8 * s, va_list * args) |
| 33 | { |
| 34 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 35 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 36 | vxlan_rx_trace_t * t = va_arg (*args, vxlan_rx_trace_t *); |
| 37 | |
| 38 | if (t->tunnel_index != ~0) |
| 39 | { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 40 | s = format (s, "VXLAN decap from vxlan_tunnel%d vni %d next %d error %d", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | t->tunnel_index, t->vni, t->next_index, t->error); |
| 42 | } |
| 43 | else |
| 44 | { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 45 | s = format (s, "VXLAN decap error - tunnel for vni %d does not exist", |
| 46 | t->vni); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | } |
| 48 | return s; |
| 49 | } |
| 50 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 51 | always_inline u32 |
| 52 | validate_vxlan_fib (vlib_buffer_t *b, vxlan_tunnel_t *t, u32 is_ip4) |
| 53 | { |
| 54 | u32 fib_index, sw_if_index; |
| 55 | |
| 56 | sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX]; |
| 57 | |
| 58 | if (is_ip4) |
| 59 | fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ? |
| 60 | vec_elt (ip4_main.fib_index_by_sw_if_index, sw_if_index) : |
| 61 | vnet_buffer (b)->sw_if_index[VLIB_TX]; |
| 62 | else |
| 63 | fib_index = (vnet_buffer (b)->sw_if_index[VLIB_TX] == (u32) ~ 0) ? |
| 64 | vec_elt (ip6_main.fib_index_by_sw_if_index, sw_if_index) : |
| 65 | vnet_buffer (b)->sw_if_index[VLIB_TX]; |
| 66 | |
| 67 | return (fib_index == t->encap_fib_index); |
| 68 | } |
| 69 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 70 | always_inline uword |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | vxlan_input (vlib_main_t * vm, |
| 72 | vlib_node_runtime_t * node, |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 73 | vlib_frame_t * from_frame, |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 74 | u32 is_ip4) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
| 76 | u32 n_left_from, next_index, * from, * to_next; |
| 77 | vxlan_main_t * vxm = &vxlan_main; |
| 78 | vnet_main_t * vnm = vxm->vnet_main; |
| 79 | vnet_interface_main_t * im = &vnm->interface_main; |
| 80 | u32 last_tunnel_index = ~0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 81 | vxlan4_tunnel_key_t last_key4; |
| 82 | vxlan6_tunnel_key_t last_key6; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | u32 pkts_decapsulated = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 84 | u32 thread_index = vlib_get_thread_index(); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | u32 stats_sw_if_index, stats_n_packets, stats_n_bytes; |
| 86 | |
Dave Barach | f9c231e | 2016-08-05 10:10:18 -0400 | [diff] [blame] | 87 | if (is_ip4) |
| 88 | last_key4.as_u64 = ~0; |
| 89 | else |
| 90 | memset (&last_key6, 0xff, sizeof (last_key6)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
| 92 | from = vlib_frame_vector_args (from_frame); |
| 93 | n_left_from = from_frame->n_vectors; |
| 94 | |
| 95 | next_index = node->cached_next_index; |
| 96 | stats_sw_if_index = node->runtime_data[0]; |
| 97 | stats_n_packets = stats_n_bytes = 0; |
| 98 | |
| 99 | while (n_left_from > 0) |
| 100 | { |
| 101 | u32 n_left_to_next; |
| 102 | |
| 103 | vlib_get_next_frame (vm, node, next_index, |
| 104 | to_next, n_left_to_next); |
| 105 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 106 | { |
| 107 | u32 bi0, bi1; |
| 108 | vlib_buffer_t * b0, * b1; |
| 109 | u32 next0, next1; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 110 | ip4_header_t * ip4_0, * ip4_1; |
| 111 | ip6_header_t * ip6_0, * ip6_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | vxlan_header_t * vxlan0, * vxlan1; |
| 113 | uword * p0, * p1; |
| 114 | u32 tunnel_index0, tunnel_index1; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 115 | vxlan_tunnel_t * t0, * t1, * mt0 = NULL, * mt1 = NULL; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 116 | vxlan4_tunnel_key_t key4_0, key4_1; |
| 117 | vxlan6_tunnel_key_t key6_0, key6_1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | u32 error0, error1; |
| 119 | u32 sw_if_index0, sw_if_index1, len0, len1; |
| 120 | |
| 121 | /* Prefetch next iteration. */ |
| 122 | { |
| 123 | vlib_buffer_t * p2, * p3; |
| 124 | |
| 125 | p2 = vlib_get_buffer (vm, from[2]); |
| 126 | p3 = vlib_get_buffer (vm, from[3]); |
| 127 | |
| 128 | vlib_prefetch_buffer_header (p2, LOAD); |
| 129 | vlib_prefetch_buffer_header (p3, LOAD); |
| 130 | |
| 131 | CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 132 | CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 133 | } |
| 134 | |
| 135 | bi0 = from[0]; |
| 136 | bi1 = from[1]; |
| 137 | to_next[0] = bi0; |
| 138 | to_next[1] = bi1; |
| 139 | from += 2; |
| 140 | to_next += 2; |
| 141 | n_left_to_next -= 2; |
| 142 | n_left_from -= 2; |
| 143 | |
| 144 | b0 = vlib_get_buffer (vm, bi0); |
| 145 | b1 = vlib_get_buffer (vm, bi1); |
| 146 | |
| 147 | /* udp leaves current_data pointing at the vxlan header */ |
| 148 | vxlan0 = vlib_buffer_get_current (b0); |
| 149 | vxlan1 = vlib_buffer_get_current (b1); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 150 | if (is_ip4) { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 151 | vlib_buffer_advance |
| 152 | (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t))); |
| 153 | vlib_buffer_advance |
| 154 | (b1, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t))); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 155 | ip4_0 = vlib_buffer_get_current (b0); |
| 156 | ip4_1 = vlib_buffer_get_current (b1); |
| 157 | } else { |
| 158 | vlib_buffer_advance |
| 159 | (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t))); |
| 160 | vlib_buffer_advance |
| 161 | (b1, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t))); |
| 162 | ip6_0 = vlib_buffer_get_current (b0); |
| 163 | ip6_1 = vlib_buffer_get_current (b1); |
| 164 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 165 | |
| 166 | /* pop (ip, udp, vxlan) */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 167 | if (is_ip4) { |
| 168 | vlib_buffer_advance |
| 169 | (b0, sizeof(*ip4_0)+sizeof(udp_header_t)+sizeof(*vxlan0)); |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 170 | vlib_buffer_advance |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 171 | (b1, sizeof(*ip4_1)+sizeof(udp_header_t)+sizeof(*vxlan1)); |
| 172 | } else { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 173 | vlib_buffer_advance |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 174 | (b0, sizeof(*ip6_0)+sizeof(udp_header_t)+sizeof(*vxlan0)); |
| 175 | vlib_buffer_advance |
| 176 | (b1, sizeof(*ip6_1)+sizeof(udp_header_t)+sizeof(*vxlan1)); |
| 177 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | |
| 179 | tunnel_index0 = ~0; |
| 180 | error0 = 0; |
| 181 | |
| 182 | tunnel_index1 = ~0; |
| 183 | error1 = 0; |
| 184 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 185 | if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I)) |
| 186 | { |
| 187 | error0 = VXLAN_ERROR_BAD_FLAGS; |
| 188 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 189 | goto trace0; |
| 190 | } |
| 191 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 192 | if (is_ip4) { |
| 193 | key4_0.src = ip4_0->src_address.as_u32; |
| 194 | key4_0.vni = vxlan0->vni_reserved; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 195 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 196 | /* Make sure VXLAN tunnel exist according to packet SIP and VNI */ |
| 197 | if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64)) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 198 | { |
| 199 | p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 200 | if (PREDICT_FALSE (p0 == NULL)) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 201 | { |
| 202 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 203 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 204 | goto trace0; |
| 205 | } |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 206 | last_key4.as_u64 = key4_0.as_u64; |
| 207 | tunnel_index0 = last_tunnel_index = p0[0]; |
| 208 | } |
| 209 | else |
| 210 | tunnel_index0 = last_tunnel_index; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 211 | t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); |
| 212 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 213 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
| 214 | if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0)) |
| 215 | { |
| 216 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 217 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 218 | goto trace0; |
| 219 | } |
| 220 | |
| 221 | /* Validate VXLAN tunnel SIP against packet DIP */ |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 222 | if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32)) |
| 223 | goto next0; /* valid packet */ |
| 224 | if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address))) |
| 225 | { |
| 226 | key4_0.src = ip4_0->dst_address.as_u32; |
| 227 | key4_0.vni = vxlan0->vni_reserved; |
| 228 | /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */ |
| 229 | p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64); |
| 230 | if (PREDICT_TRUE (p0 != NULL)) |
| 231 | { |
| 232 | mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); |
| 233 | goto next0; /* valid packet */ |
| 234 | } |
| 235 | } |
| 236 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 237 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 238 | goto trace0; |
| 239 | |
| 240 | } else /* !is_ip4 */ { |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 241 | key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0]; |
| 242 | key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1]; |
| 243 | key6_0.vni = vxlan0->vni_reserved; |
| 244 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 245 | /* Make sure VXLAN tunnel exist according to packet SIP and VNI */ |
| 246 | if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0)) |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 247 | { |
| 248 | p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 249 | if (PREDICT_FALSE (p0 == NULL)) |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 250 | { |
| 251 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 252 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 253 | goto trace0; |
| 254 | } |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 255 | clib_memcpy (&last_key6, &key6_0, sizeof(key6_0)); |
| 256 | tunnel_index0 = last_tunnel_index = p0[0]; |
| 257 | } |
| 258 | else |
| 259 | tunnel_index0 = last_tunnel_index; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 260 | t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); |
| 261 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 262 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
| 263 | if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0)) |
| 264 | { |
| 265 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 266 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 267 | goto trace0; |
| 268 | } |
| 269 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 270 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 271 | if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, |
| 272 | &t0->src.ip6))) |
| 273 | goto next0; /* valid packet */ |
| 274 | if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address))) |
| 275 | { |
| 276 | key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0]; |
| 277 | key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1]; |
| 278 | key6_0.vni = vxlan0->vni_reserved; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 279 | p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 280 | if (PREDICT_TRUE (p0 != NULL)) |
| 281 | { |
| 282 | mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); |
| 283 | goto next0; /* valid packet */ |
| 284 | } |
| 285 | } |
| 286 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 287 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 288 | goto trace0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 289 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 290 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 291 | next0: |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 292 | next0 = t0->decap_next_index; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 293 | sw_if_index0 = t0->sw_if_index; |
| 294 | len0 = vlib_buffer_length_in_chain (vm, b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | |
| 296 | /* Required to make the l2 tag push / pop code work on l2 subifs */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 297 | if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT)) |
| 298 | vnet_update_l2_len (b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 299 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 300 | /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 302 | sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | |
| 304 | pkts_decapsulated ++; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 305 | stats_n_packets += 1; |
| 306 | stats_n_bytes += len0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | |
| 308 | /* Batch stats increment on the same vxlan tunnel so counter |
| 309 | is not incremented per packet */ |
| 310 | if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) |
| 311 | { |
| 312 | stats_n_packets -= 1; |
| 313 | stats_n_bytes -= len0; |
| 314 | if (stats_n_packets) |
| 315 | vlib_increment_combined_counter |
| 316 | (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 317 | thread_index, stats_sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 318 | stats_n_packets, stats_n_bytes); |
| 319 | stats_n_packets = 1; |
| 320 | stats_n_bytes = len0; |
| 321 | stats_sw_if_index = sw_if_index0; |
| 322 | } |
| 323 | |
| 324 | trace0: |
| 325 | b0->error = error0 ? node->errors[error0] : 0; |
| 326 | |
| 327 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 328 | { |
| 329 | vxlan_rx_trace_t *tr |
| 330 | = vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 331 | tr->next_index = next0; |
| 332 | tr->error = error0; |
| 333 | tr->tunnel_index = tunnel_index0; |
| 334 | tr->vni = vnet_get_vni (vxlan0); |
| 335 | } |
| 336 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 337 | if (PREDICT_FALSE (vxlan1->flags != VXLAN_FLAGS_I)) |
| 338 | { |
| 339 | error1 = VXLAN_ERROR_BAD_FLAGS; |
| 340 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 341 | goto trace1; |
| 342 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 343 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 344 | if (is_ip4) { |
| 345 | key4_1.src = ip4_1->src_address.as_u32; |
| 346 | key4_1.vni = vxlan1->vni_reserved; |
| 347 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 348 | /* Make sure unicast VXLAN tunnel exist by packet SIP and VNI */ |
| 349 | if (PREDICT_FALSE (key4_1.as_u64 != last_key4.as_u64)) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 350 | { |
| 351 | p1 = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 352 | if (PREDICT_FALSE (p1 == NULL)) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 353 | { |
| 354 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 355 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 356 | goto trace1; |
| 357 | } |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 358 | last_key4.as_u64 = key4_1.as_u64; |
| 359 | tunnel_index1 = last_tunnel_index = p1[0]; |
| 360 | } |
| 361 | else |
| 362 | tunnel_index1 = last_tunnel_index; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 363 | t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1); |
| 364 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 365 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
| 366 | if (PREDICT_FALSE (validate_vxlan_fib (b1, t1, is_ip4) == 0)) |
| 367 | { |
| 368 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 369 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 370 | goto trace1; |
| 371 | } |
| 372 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 373 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 374 | if (PREDICT_TRUE (ip4_1->dst_address.as_u32 == t1->src.ip4.as_u32)) |
| 375 | goto next1; /* valid packet */ |
| 376 | if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_1->dst_address))) |
| 377 | { |
| 378 | key4_1.src = ip4_1->dst_address.as_u32; |
| 379 | key4_1.vni = vxlan1->vni_reserved; |
| 380 | /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */ |
| 381 | p1 = hash_get (vxm->vxlan4_tunnel_by_key, key4_1.as_u64); |
| 382 | if (PREDICT_TRUE (p1 != NULL)) |
| 383 | { |
| 384 | mt1 = pool_elt_at_index (vxm->tunnels, p1[0]); |
| 385 | goto next1; /* valid packet */ |
| 386 | } |
| 387 | } |
| 388 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 389 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 390 | goto trace1; |
| 391 | |
| 392 | } else /* !is_ip4 */ { |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 393 | key6_1.src.as_u64[0] = ip6_1->src_address.as_u64[0]; |
| 394 | key6_1.src.as_u64[1] = ip6_1->src_address.as_u64[1]; |
| 395 | key6_1.vni = vxlan1->vni_reserved; |
| 396 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 397 | /* Make sure VXLAN tunnel exist according to packet SIP and VNI */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 398 | if (PREDICT_FALSE (memcmp(&key6_1, &last_key6, sizeof(last_key6)) != 0)) |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 399 | { |
| 400 | p1 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 401 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 402 | if (PREDICT_FALSE (p1 == NULL)) |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 403 | { |
| 404 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 405 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 406 | goto trace1; |
| 407 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 408 | |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 409 | clib_memcpy (&last_key6, &key6_1, sizeof(key6_1)); |
| 410 | tunnel_index1 = last_tunnel_index = p1[0]; |
| 411 | } |
| 412 | else |
| 413 | tunnel_index1 = last_tunnel_index; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 414 | t1 = pool_elt_at_index (vxm->tunnels, tunnel_index1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 415 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 416 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
| 417 | if (PREDICT_FALSE (validate_vxlan_fib (b1, t1, is_ip4) == 0)) |
| 418 | { |
| 419 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 420 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 421 | goto trace1; |
| 422 | } |
| 423 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 424 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 425 | if (PREDICT_TRUE (ip6_address_is_equal (&ip6_1->dst_address, |
| 426 | &t1->src.ip6))) |
| 427 | goto next1; /* valid packet */ |
| 428 | if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_1->dst_address))) |
| 429 | { |
| 430 | key6_1.src.as_u64[0] = ip6_1->dst_address.as_u64[0]; |
| 431 | key6_1.src.as_u64[1] = ip6_1->dst_address.as_u64[1]; |
| 432 | key6_1.vni = vxlan1->vni_reserved; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 433 | p1 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_1); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 434 | if (PREDICT_TRUE (p1 != NULL)) |
| 435 | { |
| 436 | mt1 = pool_elt_at_index (vxm->tunnels, p1[0]); |
| 437 | goto next1; /* valid packet */ |
| 438 | } |
| 439 | } |
| 440 | error1 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 441 | next1 = VXLAN_INPUT_NEXT_DROP; |
| 442 | goto trace1; |
| 443 | } |
| 444 | |
| 445 | next1: |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 446 | next1 = t1->decap_next_index; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 447 | sw_if_index1 = t1->sw_if_index; |
| 448 | len1 = vlib_buffer_length_in_chain (vm, b1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 449 | |
| 450 | /* Required to make the l2 tag push / pop code work on l2 subifs */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 451 | if (PREDICT_TRUE(next1 == VXLAN_INPUT_NEXT_L2_INPUT)) |
| 452 | vnet_update_l2_len (b1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 454 | /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | vnet_buffer(b1)->sw_if_index[VLIB_RX] = sw_if_index1; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 456 | sw_if_index1 = (mt1) ? mt1->sw_if_index : sw_if_index1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 457 | |
| 458 | pkts_decapsulated ++; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 459 | stats_n_packets += 1; |
| 460 | stats_n_bytes += len1; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | |
| 462 | /* Batch stats increment on the same vxlan tunnel so counter |
| 463 | is not incremented per packet */ |
| 464 | if (PREDICT_FALSE (sw_if_index1 != stats_sw_if_index)) |
| 465 | { |
| 466 | stats_n_packets -= 1; |
| 467 | stats_n_bytes -= len1; |
| 468 | if (stats_n_packets) |
| 469 | vlib_increment_combined_counter |
| 470 | (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 471 | thread_index, stats_sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | stats_n_packets, stats_n_bytes); |
| 473 | stats_n_packets = 1; |
| 474 | stats_n_bytes = len1; |
| 475 | stats_sw_if_index = sw_if_index1; |
| 476 | } |
| 477 | |
| 478 | trace1: |
| 479 | b1->error = error1 ? node->errors[error1] : 0; |
| 480 | |
| 481 | if (PREDICT_FALSE(b1->flags & VLIB_BUFFER_IS_TRACED)) |
| 482 | { |
| 483 | vxlan_rx_trace_t *tr |
| 484 | = vlib_add_trace (vm, node, b1, sizeof (*tr)); |
| 485 | tr->next_index = next1; |
| 486 | tr->error = error1; |
| 487 | tr->tunnel_index = tunnel_index1; |
| 488 | tr->vni = vnet_get_vni (vxlan1); |
| 489 | } |
| 490 | |
| 491 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 492 | to_next, n_left_to_next, |
| 493 | bi0, bi1, next0, next1); |
| 494 | } |
| 495 | |
| 496 | while (n_left_from > 0 && n_left_to_next > 0) |
| 497 | { |
| 498 | u32 bi0; |
| 499 | vlib_buffer_t * b0; |
| 500 | u32 next0; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 501 | ip4_header_t * ip4_0; |
| 502 | ip6_header_t * ip6_0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | vxlan_header_t * vxlan0; |
| 504 | uword * p0; |
| 505 | u32 tunnel_index0; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 506 | vxlan_tunnel_t * t0, * mt0 = NULL; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 507 | vxlan4_tunnel_key_t key4_0; |
| 508 | vxlan6_tunnel_key_t key6_0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 509 | u32 error0; |
| 510 | u32 sw_if_index0, len0; |
| 511 | |
| 512 | bi0 = from[0]; |
| 513 | to_next[0] = bi0; |
| 514 | from += 1; |
| 515 | to_next += 1; |
| 516 | n_left_from -= 1; |
| 517 | n_left_to_next -= 1; |
| 518 | |
| 519 | b0 = vlib_get_buffer (vm, bi0); |
| 520 | |
| 521 | /* udp leaves current_data pointing at the vxlan header */ |
| 522 | vxlan0 = vlib_buffer_get_current (b0); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 523 | if (is_ip4) { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 524 | vlib_buffer_advance |
| 525 | (b0, -(word)(sizeof(udp_header_t)+sizeof(ip4_header_t))); |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 526 | ip4_0 = vlib_buffer_get_current (b0); |
| 527 | } else { |
| 528 | vlib_buffer_advance |
| 529 | (b0, -(word)(sizeof(udp_header_t)+sizeof(ip6_header_t))); |
| 530 | ip6_0 = vlib_buffer_get_current (b0); |
| 531 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | |
| 533 | /* pop (ip, udp, vxlan) */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 534 | if (is_ip4) { |
| 535 | vlib_buffer_advance |
| 536 | (b0, sizeof(*ip4_0)+sizeof(udp_header_t)+sizeof(*vxlan0)); |
| 537 | } else { |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 538 | vlib_buffer_advance |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 539 | (b0, sizeof(*ip6_0)+sizeof(udp_header_t)+sizeof(*vxlan0)); |
| 540 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 541 | |
| 542 | tunnel_index0 = ~0; |
| 543 | error0 = 0; |
| 544 | |
John Lo | c42912d | 2016-11-07 18:30:47 -0500 | [diff] [blame] | 545 | if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I)) |
| 546 | { |
| 547 | error0 = VXLAN_ERROR_BAD_FLAGS; |
| 548 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 549 | goto trace00; |
| 550 | } |
| 551 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 552 | if (is_ip4) { |
| 553 | key4_0.src = ip4_0->src_address.as_u32; |
| 554 | key4_0.vni = vxlan0->vni_reserved; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 555 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 556 | /* Make sure unicast VXLAN tunnel exist by packet SIP and VNI */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 557 | if (PREDICT_FALSE (key4_0.as_u64 != last_key4.as_u64)) |
| 558 | { |
| 559 | p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 560 | if (PREDICT_FALSE (p0 == NULL)) |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 561 | { |
| 562 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 563 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 564 | goto trace00; |
| 565 | } |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 566 | last_key4.as_u64 = key4_0.as_u64; |
| 567 | tunnel_index0 = last_tunnel_index = p0[0]; |
| 568 | } |
| 569 | else |
| 570 | tunnel_index0 = last_tunnel_index; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 571 | t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); |
| 572 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 573 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
| 574 | if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0)) |
| 575 | { |
| 576 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 577 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 578 | goto trace00; |
| 579 | } |
| 580 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 581 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 582 | if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32)) |
| 583 | goto next00; /* valid packet */ |
| 584 | if (PREDICT_FALSE (ip4_address_is_multicast (&ip4_0->dst_address))) |
| 585 | { |
| 586 | key4_0.src = ip4_0->dst_address.as_u32; |
| 587 | key4_0.vni = vxlan0->vni_reserved; |
| 588 | /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */ |
| 589 | p0 = hash_get (vxm->vxlan4_tunnel_by_key, key4_0.as_u64); |
| 590 | if (PREDICT_TRUE (p0 != NULL)) |
| 591 | { |
| 592 | mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); |
| 593 | goto next00; /* valid packet */ |
| 594 | } |
| 595 | } |
| 596 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 597 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 598 | goto trace00; |
| 599 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 600 | } else /* !is_ip4 */ { |
| 601 | key6_0.src.as_u64[0] = ip6_0->src_address.as_u64[0]; |
| 602 | key6_0.src.as_u64[1] = ip6_0->src_address.as_u64[1]; |
| 603 | key6_0.vni = vxlan0->vni_reserved; |
| 604 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 605 | /* Make sure VXLAN tunnel exist according to packet SIP and VNI */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 606 | if (PREDICT_FALSE (memcmp(&key6_0, &last_key6, sizeof(last_key6)) != 0)) |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 607 | { |
| 608 | p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 609 | if (PREDICT_FALSE (p0 == NULL)) |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 610 | { |
| 611 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 612 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 613 | goto trace00; |
| 614 | } |
Chris Luke | c794915 | 2016-05-31 10:42:14 -0400 | [diff] [blame] | 615 | clib_memcpy (&last_key6, &key6_0, sizeof(key6_0)); |
| 616 | tunnel_index0 = last_tunnel_index = p0[0]; |
| 617 | } |
| 618 | else |
| 619 | tunnel_index0 = last_tunnel_index; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 620 | t0 = pool_elt_at_index (vxm->tunnels, tunnel_index0); |
| 621 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 622 | /* Validate VXLAN tunnel encap-fib index agaist packet */ |
| 623 | if (PREDICT_FALSE (validate_vxlan_fib (b0, t0, is_ip4) == 0)) |
| 624 | { |
| 625 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 626 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 627 | goto trace00; |
| 628 | } |
| 629 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 630 | /* Validate VXLAN tunnel SIP against packet DIP */ |
| 631 | if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, |
| 632 | &t0->src.ip6))) |
| 633 | goto next00; /* valid packet */ |
| 634 | if (PREDICT_FALSE (ip6_address_is_multicast (&ip6_0->dst_address))) |
| 635 | { |
| 636 | key6_0.src.as_u64[0] = ip6_0->dst_address.as_u64[0]; |
| 637 | key6_0.src.as_u64[1] = ip6_0->dst_address.as_u64[1]; |
| 638 | key6_0.vni = vxlan0->vni_reserved; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 639 | p0 = hash_get_mem (vxm->vxlan6_tunnel_by_key, &key6_0); |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 640 | if (PREDICT_TRUE (p0 != NULL)) |
| 641 | { |
| 642 | mt0 = pool_elt_at_index (vxm->tunnels, p0[0]); |
| 643 | goto next00; /* valid packet */ |
| 644 | } |
| 645 | } |
| 646 | error0 = VXLAN_ERROR_NO_SUCH_TUNNEL; |
| 647 | next0 = VXLAN_INPUT_NEXT_DROP; |
| 648 | goto trace00; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 649 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 650 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 651 | next00: |
Hongjun Ni | beb4bf7 | 2016-11-25 00:03:46 +0800 | [diff] [blame] | 652 | next0 = t0->decap_next_index; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 653 | sw_if_index0 = t0->sw_if_index; |
| 654 | len0 = vlib_buffer_length_in_chain (vm, b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 655 | |
| 656 | /* Required to make the l2 tag push / pop code work on l2 subifs */ |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 657 | if (PREDICT_TRUE(next0 == VXLAN_INPUT_NEXT_L2_INPUT)) |
| 658 | vnet_update_l2_len (b0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 659 | |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 660 | /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | vnet_buffer(b0)->sw_if_index[VLIB_RX] = sw_if_index0; |
John Lo | 56912c8 | 2016-12-08 16:10:02 -0500 | [diff] [blame] | 662 | sw_if_index0 = (mt0) ? mt0->sw_if_index : sw_if_index0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 663 | |
| 664 | pkts_decapsulated ++; |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 665 | stats_n_packets += 1; |
| 666 | stats_n_bytes += len0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 667 | |
| 668 | /* Batch stats increment on the same vxlan tunnel so counter |
| 669 | is not incremented per packet */ |
| 670 | if (PREDICT_FALSE (sw_if_index0 != stats_sw_if_index)) |
| 671 | { |
| 672 | stats_n_packets -= 1; |
| 673 | stats_n_bytes -= len0; |
| 674 | if (stats_n_packets) |
| 675 | vlib_increment_combined_counter |
| 676 | (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 677 | thread_index, stats_sw_if_index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | stats_n_packets, stats_n_bytes); |
| 679 | stats_n_packets = 1; |
| 680 | stats_n_bytes = len0; |
| 681 | stats_sw_if_index = sw_if_index0; |
| 682 | } |
| 683 | |
| 684 | trace00: |
| 685 | b0->error = error0 ? node->errors[error0] : 0; |
| 686 | |
| 687 | if (PREDICT_FALSE(b0->flags & VLIB_BUFFER_IS_TRACED)) |
| 688 | { |
| 689 | vxlan_rx_trace_t *tr |
| 690 | = vlib_add_trace (vm, node, b0, sizeof (*tr)); |
| 691 | tr->next_index = next0; |
| 692 | tr->error = error0; |
| 693 | tr->tunnel_index = tunnel_index0; |
| 694 | tr->vni = vnet_get_vni (vxlan0); |
| 695 | } |
| 696 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 697 | to_next, n_left_to_next, |
| 698 | bi0, next0); |
| 699 | } |
| 700 | |
| 701 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 702 | } |
| 703 | /* Do we still need this now that tunnel tx stats is kept? */ |
John Lo | 13e3d45 | 2016-08-09 19:20:51 -0400 | [diff] [blame] | 704 | vlib_node_increment_counter (vm, is_ip4? |
| 705 | vxlan4_input_node.index:vxlan6_input_node.index, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 706 | VXLAN_ERROR_DECAPSULATED, |
| 707 | pkts_decapsulated); |
| 708 | |
| 709 | /* Increment any remaining batch stats */ |
| 710 | if (stats_n_packets) |
| 711 | { |
| 712 | vlib_increment_combined_counter |
| 713 | (im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX, |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 714 | thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 715 | node->runtime_data[0] = stats_sw_if_index; |
| 716 | } |
| 717 | |
| 718 | return from_frame->n_vectors; |
| 719 | } |
| 720 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 721 | static uword |
| 722 | vxlan4_input (vlib_main_t * vm, |
| 723 | vlib_node_runtime_t * node, |
| 724 | vlib_frame_t * from_frame) |
| 725 | { |
| 726 | return vxlan_input(vm, node, from_frame, /* is_ip4 */ 1); |
| 727 | } |
| 728 | |
| 729 | static uword |
| 730 | vxlan6_input (vlib_main_t * vm, |
| 731 | vlib_node_runtime_t * node, |
| 732 | vlib_frame_t * from_frame) |
| 733 | { |
| 734 | return vxlan_input(vm, node, from_frame, /* is_ip4 */ 0); |
| 735 | } |
| 736 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 737 | static char * vxlan_error_strings[] = { |
| 738 | #define vxlan_error(n,s) s, |
| 739 | #include <vnet/vxlan/vxlan_error.def> |
| 740 | #undef vxlan_error |
| 741 | #undef _ |
| 742 | }; |
| 743 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 744 | VLIB_REGISTER_NODE (vxlan4_input_node) = { |
| 745 | .function = vxlan4_input, |
| 746 | .name = "vxlan4-input", |
| 747 | /* Takes a vector of packets. */ |
| 748 | .vector_size = sizeof (u32), |
| 749 | |
| 750 | .n_errors = VXLAN_N_ERROR, |
| 751 | .error_strings = vxlan_error_strings, |
| 752 | |
| 753 | .n_next_nodes = VXLAN_INPUT_N_NEXT, |
| 754 | .next_nodes = { |
| 755 | #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n, |
| 756 | foreach_vxlan_input_next |
| 757 | #undef _ |
| 758 | }, |
| 759 | |
| 760 | //temp .format_buffer = format_vxlan_header, |
| 761 | .format_trace = format_vxlan_rx_trace, |
| 762 | // $$$$ .unformat_buffer = unformat_vxlan_header, |
| 763 | }; |
| 764 | |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 765 | VLIB_NODE_FUNCTION_MULTIARCH (vxlan4_input_node, vxlan4_input) |
| 766 | |
Chris Luke | 99cb335 | 2016-04-26 10:49:53 -0400 | [diff] [blame] | 767 | VLIB_REGISTER_NODE (vxlan6_input_node) = { |
| 768 | .function = vxlan6_input, |
| 769 | .name = "vxlan6-input", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 770 | /* Takes a vector of packets. */ |
| 771 | .vector_size = sizeof (u32), |
| 772 | |
| 773 | .n_errors = VXLAN_N_ERROR, |
| 774 | .error_strings = vxlan_error_strings, |
| 775 | |
| 776 | .n_next_nodes = VXLAN_INPUT_N_NEXT, |
| 777 | .next_nodes = { |
| 778 | #define _(s,n) [VXLAN_INPUT_NEXT_##s] = n, |
| 779 | foreach_vxlan_input_next |
| 780 | #undef _ |
| 781 | }, |
| 782 | |
| 783 | //temp .format_buffer = format_vxlan_header, |
| 784 | .format_trace = format_vxlan_rx_trace, |
| 785 | // $$$$ .unformat_buffer = unformat_vxlan_header, |
| 786 | }; |
Damjan Marion | 1c80e83 | 2016-05-11 23:07:18 +0200 | [diff] [blame] | 787 | |
| 788 | VLIB_NODE_FUNCTION_MULTIARCH (vxlan6_input_node, vxlan6_input) |
| 789 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 790 | |
| 791 | typedef enum { |
| 792 | IP_VXLAN_BYPASS_NEXT_DROP, |
| 793 | IP_VXLAN_BYPASS_NEXT_VXLAN, |
| 794 | IP_VXLAN_BYPASS_N_NEXT, |
| 795 | } ip_vxan_bypass_next_t; |
| 796 | |
| 797 | always_inline uword |
| 798 | ip_vxlan_bypass_inline (vlib_main_t * vm, |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 799 | vlib_node_runtime_t * node, |
| 800 | vlib_frame_t * frame, |
| 801 | u32 is_ip4) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 802 | { |
| 803 | vxlan_main_t * vxm = &vxlan_main; |
| 804 | u32 * from, * to_next, n_left_from, n_left_to_next, next_index; |
| 805 | vlib_node_runtime_t * error_node = vlib_node_get_runtime (vm, ip4_input_node.index); |
| 806 | ip4_address_t addr4; /* last IPv4 address matching a local VTEP address */ |
| 807 | ip6_address_t addr6; /* last IPv6 address matching a local VTEP address */ |
| 808 | |
| 809 | from = vlib_frame_vector_args (frame); |
| 810 | n_left_from = frame->n_vectors; |
| 811 | next_index = node->cached_next_index; |
| 812 | |
| 813 | if (node->flags & VLIB_NODE_FLAG_TRACE) |
| 814 | ip4_forward_next_trace (vm, node, frame, VLIB_TX); |
| 815 | |
| 816 | if (is_ip4) addr4.data_u32 = ~0; |
| 817 | else ip6_address_set_zero (&addr6); |
| 818 | |
| 819 | while (n_left_from > 0) |
| 820 | { |
| 821 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 822 | |
| 823 | while (n_left_from >= 4 && n_left_to_next >= 2) |
| 824 | { |
| 825 | vlib_buffer_t * b0, * b1; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 826 | ip4_header_t * ip40, * ip41; |
| 827 | ip6_header_t * ip60, * ip61; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 828 | udp_header_t * udp0, * udp1; |
| 829 | u32 bi0, ip_len0, udp_len0, flags0, next0; |
| 830 | u32 bi1, ip_len1, udp_len1, flags1, next1; |
| 831 | i32 len_diff0, len_diff1; |
| 832 | u8 error0, good_udp0, proto0; |
| 833 | u8 error1, good_udp1, proto1; |
| 834 | |
| 835 | /* Prefetch next iteration. */ |
| 836 | { |
| 837 | vlib_buffer_t * p2, * p3; |
| 838 | |
| 839 | p2 = vlib_get_buffer (vm, from[2]); |
| 840 | p3 = vlib_get_buffer (vm, from[3]); |
| 841 | |
| 842 | vlib_prefetch_buffer_header (p2, LOAD); |
| 843 | vlib_prefetch_buffer_header (p3, LOAD); |
| 844 | |
| 845 | CLIB_PREFETCH (p2->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 846 | CLIB_PREFETCH (p3->data, 2*CLIB_CACHE_LINE_BYTES, LOAD); |
| 847 | } |
| 848 | |
| 849 | bi0 = to_next[0] = from[0]; |
| 850 | bi1 = to_next[1] = from[1]; |
| 851 | from += 2; |
| 852 | n_left_from -= 2; |
| 853 | to_next += 2; |
| 854 | n_left_to_next -= 2; |
| 855 | |
| 856 | b0 = vlib_get_buffer (vm, bi0); |
| 857 | b1 = vlib_get_buffer (vm, bi1); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 858 | if (is_ip4) |
| 859 | { |
| 860 | ip40 = vlib_buffer_get_current (b0); |
| 861 | ip41 = vlib_buffer_get_current (b1); |
| 862 | } |
| 863 | else |
| 864 | { |
| 865 | ip60 = vlib_buffer_get_current (b0); |
| 866 | ip61 = vlib_buffer_get_current (b1); |
| 867 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 868 | |
| 869 | /* Setup packet for next IP feature */ |
| 870 | vnet_feature_next(vnet_buffer(b0)->sw_if_index[VLIB_RX], &next0, b0); |
| 871 | vnet_feature_next(vnet_buffer(b1)->sw_if_index[VLIB_RX], &next1, b1); |
| 872 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 873 | if (is_ip4) |
| 874 | { |
| 875 | /* Treat IP frag packets as "experimental" protocol for now |
| 876 | until support of IP frag reassembly is implemented */ |
| 877 | proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol; |
| 878 | proto1 = ip4_is_fragment(ip41) ? 0xfe : ip41->protocol; |
| 879 | } |
| 880 | else |
| 881 | { |
| 882 | proto0 = ip60->protocol; |
| 883 | proto1 = ip61->protocol; |
| 884 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 885 | |
| 886 | /* Process packet 0 */ |
| 887 | if (proto0 != IP_PROTOCOL_UDP) |
| 888 | goto exit0; /* not UDP packet */ |
| 889 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 890 | if (is_ip4) |
| 891 | udp0 = ip4_next_header (ip40); |
| 892 | else |
| 893 | udp0 = ip6_next_header (ip60); |
| 894 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 895 | if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan)) |
| 896 | goto exit0; /* not VXLAN packet */ |
| 897 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 898 | /* Validate DIP against VTEPs*/ |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 899 | if (is_ip4) |
| 900 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 901 | if (addr4.as_u32 != ip40->dst_address.as_u32) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 902 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 903 | if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32)) |
| 904 | goto exit0; /* no local VTEP for VXLAN packet */ |
| 905 | addr4 = ip40->dst_address; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 906 | } |
| 907 | } |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 908 | else |
| 909 | { |
| 910 | if (!ip6_address_is_equal (&addr6, &ip60->dst_address)) |
| 911 | { |
| 912 | if (!hash_get_mem (vxm->vtep6, &ip60->dst_address)) |
| 913 | goto exit0; /* no local VTEP for VXLAN packet */ |
| 914 | addr6 = ip60->dst_address; |
| 915 | } |
| 916 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 917 | |
| 918 | flags0 = b0->flags; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 919 | good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 920 | |
| 921 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 922 | good_udp0 |= udp0->checksum == 0; |
| 923 | |
| 924 | /* Verify UDP length */ |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 925 | if (is_ip4) |
| 926 | ip_len0 = clib_net_to_host_u16 (ip40->length); |
| 927 | else |
| 928 | ip_len0 = clib_net_to_host_u16 (ip60->payload_length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 929 | udp_len0 = clib_net_to_host_u16 (udp0->length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 930 | len_diff0 = ip_len0 - udp_len0; |
| 931 | |
| 932 | /* Verify UDP checksum */ |
| 933 | if (PREDICT_FALSE (!good_udp0)) |
| 934 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 935 | if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 936 | { |
| 937 | if (is_ip4) |
| 938 | flags0 = ip4_tcp_udp_validate_checksum (vm, b0); |
| 939 | else |
| 940 | flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0); |
| 941 | good_udp0 = |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 942 | (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 943 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 944 | } |
| 945 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 946 | if (is_ip4) |
| 947 | { |
| 948 | error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM; |
| 949 | error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH; |
| 950 | } |
| 951 | else |
| 952 | { |
| 953 | error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM; |
| 954 | error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH; |
| 955 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 956 | |
| 957 | next0 = error0 ? |
| 958 | IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN; |
| 959 | b0->error = error0 ? error_node->errors[error0] : 0; |
| 960 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 961 | /* vxlan-input node expect current at VXLAN header */ |
| 962 | if (is_ip4) |
| 963 | vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t)); |
| 964 | else |
| 965 | vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t)); |
| 966 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 967 | exit0: |
| 968 | /* Process packet 1 */ |
| 969 | if (proto1 != IP_PROTOCOL_UDP) |
| 970 | goto exit1; /* not UDP packet */ |
| 971 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 972 | if (is_ip4) |
| 973 | udp1 = ip4_next_header (ip41); |
| 974 | else |
| 975 | udp1 = ip6_next_header (ip61); |
| 976 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 977 | if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan)) |
| 978 | goto exit1; /* not VXLAN packet */ |
| 979 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 980 | /* Validate DIP against VTEPs*/ |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 981 | if (is_ip4) |
| 982 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 983 | if (addr4.as_u32 != ip41->dst_address.as_u32) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 984 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 985 | if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32)) |
| 986 | goto exit1; /* no local VTEP for VXLAN packet */ |
| 987 | addr4 = ip41->dst_address; |
| 988 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 989 | } |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 990 | else |
| 991 | { |
| 992 | if (!ip6_address_is_equal (&addr6, &ip61->dst_address)) |
| 993 | { |
| 994 | if (!hash_get_mem (vxm->vtep6, &ip61->dst_address)) |
| 995 | goto exit1; /* no local VTEP for VXLAN packet */ |
| 996 | addr6 = ip61->dst_address; |
| 997 | } |
| 998 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 999 | |
| 1000 | flags1 = b1->flags; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1001 | good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1002 | |
| 1003 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 1004 | good_udp1 |= udp1->checksum == 0; |
| 1005 | |
| 1006 | /* Verify UDP length */ |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1007 | if (is_ip4) |
| 1008 | ip_len1 = clib_net_to_host_u16 (ip41->length); |
| 1009 | else |
| 1010 | ip_len1 = clib_net_to_host_u16 (ip61->payload_length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1011 | udp_len1 = clib_net_to_host_u16 (udp1->length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1012 | len_diff1 = ip_len1 - udp_len1; |
| 1013 | |
| 1014 | /* Verify UDP checksum */ |
| 1015 | if (PREDICT_FALSE (!good_udp1)) |
| 1016 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1017 | if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1018 | { |
| 1019 | if (is_ip4) |
| 1020 | flags1 = ip4_tcp_udp_validate_checksum (vm, b1); |
| 1021 | else |
| 1022 | flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1); |
| 1023 | good_udp1 = |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1024 | (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1025 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1026 | } |
| 1027 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1028 | if (is_ip4) |
| 1029 | { |
| 1030 | error1 = good_udp1 ? 0 : IP4_ERROR_UDP_CHECKSUM; |
| 1031 | error1 = (len_diff1 >= 0) ? error1 : IP4_ERROR_UDP_LENGTH; |
| 1032 | } |
| 1033 | else |
| 1034 | { |
Eyal Bari | a93ea42 | 2017-02-01 13:36:15 +0200 | [diff] [blame] | 1035 | error1 = good_udp1 ? 0 : IP6_ERROR_UDP_CHECKSUM; |
| 1036 | error1 = (len_diff1 >= 0) ? error1 : IP6_ERROR_UDP_LENGTH; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1037 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1038 | |
| 1039 | next1 = error1 ? |
| 1040 | IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN; |
| 1041 | b1->error = error1 ? error_node->errors[error1] : 0; |
| 1042 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1043 | /* vxlan-input node expect current at VXLAN header */ |
| 1044 | if (is_ip4) |
| 1045 | vlib_buffer_advance (b1, sizeof(ip4_header_t)+sizeof(udp_header_t)); |
| 1046 | else |
| 1047 | vlib_buffer_advance (b1, sizeof(ip6_header_t)+sizeof(udp_header_t)); |
| 1048 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1049 | exit1: |
| 1050 | vlib_validate_buffer_enqueue_x2 (vm, node, next_index, |
| 1051 | to_next, n_left_to_next, |
| 1052 | bi0, bi1, next0, next1); |
| 1053 | } |
| 1054 | |
| 1055 | while (n_left_from > 0 && n_left_to_next > 0) |
| 1056 | { |
| 1057 | vlib_buffer_t * b0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1058 | ip4_header_t * ip40; |
| 1059 | ip6_header_t * ip60; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1060 | udp_header_t * udp0; |
| 1061 | u32 bi0, ip_len0, udp_len0, flags0, next0; |
| 1062 | i32 len_diff0; |
| 1063 | u8 error0, good_udp0, proto0; |
| 1064 | |
| 1065 | bi0 = to_next[0] = from[0]; |
| 1066 | from += 1; |
| 1067 | n_left_from -= 1; |
| 1068 | to_next += 1; |
| 1069 | n_left_to_next -= 1; |
| 1070 | |
| 1071 | b0 = vlib_get_buffer (vm, bi0); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1072 | if (is_ip4) |
| 1073 | ip40 = vlib_buffer_get_current (b0); |
| 1074 | else |
| 1075 | ip60 = vlib_buffer_get_current (b0); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1076 | |
| 1077 | /* Setup packet for next IP feature */ |
| 1078 | vnet_feature_next(vnet_buffer(b0)->sw_if_index[VLIB_RX], &next0, b0); |
| 1079 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1080 | if (is_ip4) |
| 1081 | /* Treat IP4 frag packets as "experimental" protocol for now |
| 1082 | until support of IP frag reassembly is implemented */ |
| 1083 | proto0 = ip4_is_fragment(ip40) ? 0xfe : ip40->protocol; |
| 1084 | else |
| 1085 | proto0 = ip60->protocol; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1086 | |
| 1087 | if (proto0 != IP_PROTOCOL_UDP) |
| 1088 | goto exit; /* not UDP packet */ |
| 1089 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1090 | if (is_ip4) |
| 1091 | udp0 = ip4_next_header (ip40); |
| 1092 | else |
| 1093 | udp0 = ip6_next_header (ip60); |
| 1094 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1095 | if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan)) |
| 1096 | goto exit; /* not VXLAN packet */ |
| 1097 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1098 | /* Validate DIP against VTEPs*/ |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1099 | if (is_ip4) |
| 1100 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1101 | if (addr4.as_u32 != ip40->dst_address.as_u32) |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1102 | { |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1103 | if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32)) |
| 1104 | goto exit; /* no local VTEP for VXLAN packet */ |
| 1105 | addr4 = ip40->dst_address; |
| 1106 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1107 | } |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1108 | else |
| 1109 | { |
| 1110 | if (!ip6_address_is_equal (&addr6, &ip60->dst_address)) |
| 1111 | { |
| 1112 | if (!hash_get_mem (vxm->vtep6, &ip60->dst_address)) |
| 1113 | goto exit; /* no local VTEP for VXLAN packet */ |
| 1114 | addr6 = ip60->dst_address; |
| 1115 | } |
| 1116 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1117 | |
| 1118 | flags0 = b0->flags; |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1119 | good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1120 | |
| 1121 | /* Don't verify UDP checksum for packets with explicit zero checksum. */ |
| 1122 | good_udp0 |= udp0->checksum == 0; |
| 1123 | |
| 1124 | /* Verify UDP length */ |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1125 | if (is_ip4) |
| 1126 | ip_len0 = clib_net_to_host_u16 (ip40->length); |
| 1127 | else |
| 1128 | ip_len0 = clib_net_to_host_u16 (ip60->payload_length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1129 | udp_len0 = clib_net_to_host_u16 (udp0->length); |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1130 | len_diff0 = ip_len0 - udp_len0; |
| 1131 | |
| 1132 | /* Verify UDP checksum */ |
| 1133 | if (PREDICT_FALSE (!good_udp0)) |
| 1134 | { |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1135 | if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0) |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1136 | { |
| 1137 | if (is_ip4) |
| 1138 | flags0 = ip4_tcp_udp_validate_checksum (vm, b0); |
| 1139 | else |
| 1140 | flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0); |
| 1141 | good_udp0 = |
Damjan Marion | 213b5aa | 2017-07-13 21:19:27 +0200 | [diff] [blame] | 1142 | (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0; |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1143 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1144 | } |
| 1145 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1146 | if (is_ip4) |
| 1147 | { |
| 1148 | error0 = good_udp0 ? 0 : IP4_ERROR_UDP_CHECKSUM; |
| 1149 | error0 = (len_diff0 >= 0) ? error0 : IP4_ERROR_UDP_LENGTH; |
| 1150 | } |
| 1151 | else |
| 1152 | { |
| 1153 | error0 = good_udp0 ? 0 : IP6_ERROR_UDP_CHECKSUM; |
| 1154 | error0 = (len_diff0 >= 0) ? error0 : IP6_ERROR_UDP_LENGTH; |
| 1155 | } |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1156 | |
| 1157 | next0 = error0 ? |
| 1158 | IP_VXLAN_BYPASS_NEXT_DROP : IP_VXLAN_BYPASS_NEXT_VXLAN; |
| 1159 | b0->error = error0 ? error_node->errors[error0] : 0; |
| 1160 | |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1161 | /* vxlan-input node expect current at VXLAN header */ |
| 1162 | if (is_ip4) |
| 1163 | vlib_buffer_advance (b0, sizeof(ip4_header_t)+sizeof(udp_header_t)); |
| 1164 | else |
| 1165 | vlib_buffer_advance (b0, sizeof(ip6_header_t)+sizeof(udp_header_t)); |
| 1166 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1167 | exit: |
| 1168 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 1169 | to_next, n_left_to_next, |
| 1170 | bi0, next0); |
| 1171 | } |
| 1172 | |
| 1173 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 1174 | } |
| 1175 | |
| 1176 | return frame->n_vectors; |
| 1177 | } |
| 1178 | |
| 1179 | static uword |
| 1180 | ip4_vxlan_bypass (vlib_main_t * vm, |
| 1181 | vlib_node_runtime_t * node, |
| 1182 | vlib_frame_t * frame) |
| 1183 | { |
| 1184 | return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 1); |
| 1185 | } |
| 1186 | |
| 1187 | VLIB_REGISTER_NODE (ip4_vxlan_bypass_node) = { |
| 1188 | .function = ip4_vxlan_bypass, |
| 1189 | .name = "ip4-vxlan-bypass", |
| 1190 | .vector_size = sizeof (u32), |
| 1191 | |
| 1192 | .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT, |
| 1193 | .next_nodes = { |
| 1194 | [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop", |
| 1195 | [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan4-input", |
| 1196 | }, |
| 1197 | |
| 1198 | .format_buffer = format_ip4_header, |
| 1199 | .format_trace = format_ip4_forward_next_trace, |
| 1200 | }; |
| 1201 | |
| 1202 | VLIB_NODE_FUNCTION_MULTIARCH (ip4_vxlan_bypass_node,ip4_vxlan_bypass) |
| 1203 | |
John Lo | 37682e1 | 2016-11-30 12:51:39 -0500 | [diff] [blame] | 1204 | /* Dummy init function to get us linked in. */ |
| 1205 | clib_error_t * ip4_vxlan_bypass_init (vlib_main_t * vm) |
| 1206 | { return 0; } |
| 1207 | |
| 1208 | VLIB_INIT_FUNCTION (ip4_vxlan_bypass_init); |
John Lo | 2b81eb8 | 2017-01-30 13:12:10 -0500 | [diff] [blame] | 1209 | |
| 1210 | static uword |
| 1211 | ip6_vxlan_bypass (vlib_main_t * vm, |
| 1212 | vlib_node_runtime_t * node, |
| 1213 | vlib_frame_t * frame) |
| 1214 | { |
| 1215 | return ip_vxlan_bypass_inline (vm, node, frame, /* is_ip4 */ 0); |
| 1216 | } |
| 1217 | |
| 1218 | VLIB_REGISTER_NODE (ip6_vxlan_bypass_node) = { |
| 1219 | .function = ip6_vxlan_bypass, |
| 1220 | .name = "ip6-vxlan-bypass", |
| 1221 | .vector_size = sizeof (u32), |
| 1222 | |
| 1223 | .n_next_nodes = IP_VXLAN_BYPASS_N_NEXT, |
| 1224 | .next_nodes = { |
| 1225 | [IP_VXLAN_BYPASS_NEXT_DROP] = "error-drop", |
| 1226 | [IP_VXLAN_BYPASS_NEXT_VXLAN] = "vxlan6-input", |
| 1227 | }, |
| 1228 | |
| 1229 | .format_buffer = format_ip6_header, |
| 1230 | .format_trace = format_ip6_forward_next_trace, |
| 1231 | }; |
| 1232 | |
| 1233 | VLIB_NODE_FUNCTION_MULTIARCH (ip6_vxlan_bypass_node,ip6_vxlan_bypass) |
| 1234 | |
| 1235 | /* Dummy init function to get us linked in. */ |
| 1236 | clib_error_t * ip6_vxlan_bypass_init (vlib_main_t * vm) |
| 1237 | { return 0; } |
| 1238 | |
| 1239 | VLIB_INIT_FUNCTION (ip6_vxlan_bypass_init); |