tunnel: support copying TTL and flow label from inner to outer
[vpp.git] / src / vnet / vxlan / decap.c
index 7c1b6b1..4678aa3 100644 (file)
@@ -16,8 +16,8 @@
  */
 
 #include <vlib/vlib.h>
-#include <vnet/pg/pg.h>
 #include <vnet/vxlan/vxlan.h>
+#include <vnet/udp/udp_local.h>
 
 #ifndef CLIB_MARCH_VARIANT
 vlib_node_registration_t vxlan4_input_node;
@@ -46,89 +46,98 @@ format_vxlan_rx_trace (u8 * s, va_list * args)
                 t->tunnel_index, t->vni, t->next_index, t->error);
 }
 
-always_inline u32
-buf_fib_index (vlib_buffer_t * b, u32 is_ip4)
-{
-  u32 sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_TX];
-  if (sw_if_index != (u32) ~ 0)
-    return sw_if_index;
-
-  u32 *fib_index_by_sw_if_index = is_ip4 ?
-    ip4_main.fib_index_by_sw_if_index : ip6_main.fib_index_by_sw_if_index;
-  sw_if_index = vnet_buffer (b)->sw_if_index[VLIB_RX];
+typedef vxlan4_tunnel_key_t last_tunnel_cache4;
 
-  return vec_elt (fib_index_by_sw_if_index, sw_if_index);
-}
+static const vxlan_decap_info_t decap_not_found = {
+  .sw_if_index = ~0,
+  .next_index = VXLAN_INPUT_NEXT_DROP,
+  .error = VXLAN_ERROR_NO_SUCH_TUNNEL
+};
 
-typedef vxlan4_tunnel_key_t last_tunnel_cache4;
+static const vxlan_decap_info_t decap_bad_flags = {
+  .sw_if_index = ~0,
+  .next_index = VXLAN_INPUT_NEXT_DROP,
+  .error = VXLAN_ERROR_BAD_FLAGS
+};
 
-always_inline vxlan_tunnel_t *
+always_inline vxlan_decap_info_t
 vxlan4_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache4 * cache,
                    u32 fib_index, ip4_header_t * ip4_0,
-                   vxlan_header_t * vxlan0, vxlan_tunnel_t ** stats_t0)
+                   vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
 {
-  /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
-  vxlan4_tunnel_key_t key4;
-  key4.key[1] = ((u64) fib_index << 32) | vxlan0->vni_reserved;
+  if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
+    return decap_bad_flags;
+
+  /* Make sure VXLAN tunnel exist according to packet S/D IP, UDP port, VRF,
+   * and VNI */
+  u32 dst = ip4_0->dst_address.as_u32;
+  u32 src = ip4_0->src_address.as_u32;
+  udp_header_t *udp = ip4_next_header (ip4_0);
+  vxlan4_tunnel_key_t key4 = {
+    .key[0] = ((u64) dst << 32) | src,
+    .key[1] = ((u64) udp->dst_port << 48) | ((u64) fib_index << 32) |
+             vxlan0->vni_reserved,
+  };
 
-  if (PREDICT_FALSE (key4.key[1] != cache->key[1] ||
-                    ip4_0->src_address.as_u32 != (u32) cache->key[0]))
+  if (PREDICT_TRUE
+      (key4.key[0] == cache->key[0] && key4.key[1] == cache->key[1]))
     {
-      key4.key[0] = ip4_0->src_address.as_u32;
-      int rv =
-       clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
-      if (PREDICT_FALSE (rv != 0))
-       {
-         *stats_t0 = 0;
-         return 0;
-       }
-
-      *cache = key4;
+      /* cache hit */
+      vxlan_decap_info_t di = {.as_u64 = cache->value };
+      *stats_sw_if_index = di.sw_if_index;
+      return di;
     }
-  vxlan_tunnel_t *t0 = pool_elt_at_index (vxm->tunnels, cache->value);
 
-  /* Validate VXLAN tunnel SIP against packet DIP */
-  if (PREDICT_TRUE (ip4_0->dst_address.as_u32 == t0->src.ip4.as_u32))
-    *stats_t0 = t0;
-  else
+  int rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
+  if (PREDICT_TRUE (rv == 0))
     {
-      /* try multicast */
-      if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
-       {
-         *stats_t0 = 0;
-         return 0;
-       }
-
-      key4.key[0] = ip4_0->dst_address.as_u32;
-      /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
-      int rv =
-       clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
-      if (PREDICT_FALSE (rv != 0))
-       {
-         *stats_t0 = 0;
-         return 0;
-       }
-
-      *stats_t0 = pool_elt_at_index (vxm->tunnels, key4.value);
+      *cache = key4;
+      vxlan_decap_info_t di = {.as_u64 = key4.value };
+      *stats_sw_if_index = di.sw_if_index;
+      return di;
     }
 
-  return t0;
+  /* try multicast */
+  if (PREDICT_TRUE (!ip4_address_is_multicast (&ip4_0->dst_address)))
+    return decap_not_found;
+
+  /* search for mcast decap info by mcast address */
+  key4.key[0] = dst;
+  rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
+  if (rv != 0)
+    return decap_not_found;
+
+  /* search for unicast tunnel using the mcast tunnel local(src) ip */
+  vxlan_decap_info_t mdi = {.as_u64 = key4.value };
+  key4.key[0] = ((u64) mdi.local_ip.as_u32 << 32) | src;
+  rv = clib_bihash_search_inline_16_8 (&vxm->vxlan4_tunnel_by_key, &key4);
+  if (PREDICT_FALSE (rv != 0))
+    return decap_not_found;
+
+  /* mcast traffic does not update the cache */
+  *stats_sw_if_index = mdi.sw_if_index;
+  vxlan_decap_info_t di = {.as_u64 = key4.value };
+  return di;
 }
 
 typedef vxlan6_tunnel_key_t last_tunnel_cache6;
 
-always_inline vxlan_tunnel_t *
+always_inline vxlan_decap_info_t
 vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
                    u32 fib_index, ip6_header_t * ip6_0,
-                   vxlan_header_t * vxlan0, vxlan_tunnel_t ** stats_t0)
+                   vxlan_header_t * vxlan0, u32 * stats_sw_if_index)
 {
-  /* Make sure VXLAN tunnel exist according to packet SIP and VNI */
+  if (PREDICT_FALSE (vxlan0->flags != VXLAN_FLAGS_I))
+    return decap_bad_flags;
+
+  /* Make sure VXLAN tunnel exist according to packet SIP, UDP port, VRF, and
+   * VNI */
+  udp_header_t *udp = ip6_next_header (ip6_0);
   vxlan6_tunnel_key_t key6 = {
-    .key = {
-           [0] = ip6_0->src_address.as_u64[0],
-           [1] = ip6_0->src_address.as_u64[1],
-           [2] = (((u64) fib_index) << 32) | vxlan0->vni_reserved,
-           }
+    .key[0] = ip6_0->src_address.as_u64[0],
+    .key[1] = ip6_0->src_address.as_u64[1],
+    .key[2] = ((u64) udp->dst_port << 48) | ((u64) fib_index << 32) |
+             vxlan0->vni_reserved,
   };
 
   if (PREDICT_FALSE
@@ -137,10 +146,7 @@ vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
       int rv =
        clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
       if (PREDICT_FALSE (rv != 0))
-       {
-         *stats_t0 = 0;
-         return 0;
-       }
+       return decap_not_found;
 
       *cache = key6;
     }
@@ -148,15 +154,12 @@ vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
 
   /* Validate VXLAN tunnel SIP against packet DIP */
   if (PREDICT_TRUE (ip6_address_is_equal (&ip6_0->dst_address, &t0->src.ip6)))
-    *stats_t0 = t0;
+    *stats_sw_if_index = t0->sw_if_index;
   else
     {
       /* try multicast */
       if (PREDICT_TRUE (!ip6_address_is_multicast (&ip6_0->dst_address)))
-       {
-         *stats_t0 = 0;
-         return 0;
-       }
+       return decap_not_found;
 
       /* Make sure mcast VXLAN tunnel exist by packet DIP and VNI */
       key6.key[0] = ip6_0->dst_address.as_u64[0];
@@ -164,15 +167,17 @@ vxlan6_find_tunnel (vxlan_main_t * vxm, last_tunnel_cache6 * cache,
       int rv =
        clib_bihash_search_inline_24_8 (&vxm->vxlan6_tunnel_by_key, &key6);
       if (PREDICT_FALSE (rv != 0))
-       {
-         *stats_t0 = 0;
-         return 0;
-       }
+       return decap_not_found;
 
-      *stats_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
+      vxlan_tunnel_t *mcast_t0 = pool_elt_at_index (vxm->tunnels, key6.value);
+      *stats_sw_if_index = mcast_t0->sw_if_index;
     }
 
-  return t0;
+  vxlan_decap_info_t di = {
+    .sw_if_index = t0->sw_if_index,
+    .next_index = t0->decap_next_index,
+  };
+  return di;
 }
 
 always_inline uword
@@ -185,17 +190,15 @@ vxlan_input (vlib_main_t * vm,
   vnet_interface_main_t *im = &vnm->interface_main;
   vlib_combined_counter_main_t *rx_counter =
     im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_RX;
-  vlib_combined_counter_main_t *drop_counter =
-    im->combined_sw_if_counters + VNET_INTERFACE_COUNTER_DROP;
   last_tunnel_cache4 last4;
   last_tunnel_cache6 last6;
   u32 pkts_dropped = 0;
   u32 thread_index = vlib_get_thread_index ();
 
   if (is_ip4)
-    memset (&last4, 0xff, sizeof last4);
+    clib_memset (&last4, 0xff, sizeof last4);
   else
-    memset (&last6, 0xff, sizeof last6);
+    clib_memset (&last6, 0xff, sizeof last6);
 
   u32 *from = vlib_frame_vector_args (from_frame);
   u32 n_left_from = from_frame->n_vectors;
@@ -203,6 +206,7 @@ vxlan_input (vlib_main_t * vm,
   vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
   vlib_get_buffers (vm, from, bufs, n_left_from);
 
+  u32 stats_if0 = ~0, stats_if1 = ~0;
   u16 nexts[VLIB_FRAME_SIZE], *next = nexts;
   while (n_left_from >= 4)
     {
@@ -216,8 +220,6 @@ vxlan_input (vlib_main_t * vm,
       vxlan_header_t *vxlan0 = cur0;
       vxlan_header_t *vxlan1 = cur1;
 
-      u8 error0 = vxlan0->flags != VXLAN_FLAGS_I ? VXLAN_ERROR_BAD_FLAGS : 0;
-      u8 error1 = vxlan1->flags != VXLAN_FLAGS_I ? VXLAN_ERROR_BAD_FLAGS : 0;
 
       ip4_header_t *ip4_0, *ip4_1;
       ip6_header_t *ip6_0, *ip6_1;
@@ -236,28 +238,15 @@ vxlan_input (vlib_main_t * vm,
       vlib_buffer_advance (b[0], sizeof *vxlan0);
       vlib_buffer_advance (b[1], sizeof *vxlan1);
 
-      u32 fi0 = buf_fib_index (b[0], is_ip4);
-      u32 fi1 = buf_fib_index (b[1], is_ip4);
-
-      vxlan_tunnel_t *t0, *stats_t0;
-      vxlan_tunnel_t *t1, *stats_t1;
-      if (is_ip4)
-       {
-         t0 =
-           vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_t0);
-         t1 =
-           vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_t1);
-       }
-      else
-       {
-         t0 =
-           vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_t0);
-         t1 =
-           vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_t1);
-       }
+      u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
+      u32 fi1 = vlib_buffer_get_ip_fib_index (b[1], is_ip4);
 
-      error0 = t0 == 0 ? VXLAN_ERROR_NO_SUCH_TUNNEL : error0;
-      error1 = t1 == 0 ? VXLAN_ERROR_NO_SUCH_TUNNEL : error1;
+      vxlan_decap_info_t di0 = is_ip4 ?
+       vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
+       vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
+      vxlan_decap_info_t di1 = is_ip4 ?
+       vxlan4_find_tunnel (vxm, &last4, fi1, ip4_1, vxlan1, &stats_if1) :
+       vxlan6_find_tunnel (vxm, &last6, fi1, ip6_1, vxlan1, &stats_if1);
 
       /* Prefetch next iteration. */
       CLIB_PREFETCH (b[2]->data, CLIB_CACHE_LINE_BYTES, LOAD);
@@ -266,59 +255,50 @@ vxlan_input (vlib_main_t * vm,
       u32 len0 = vlib_buffer_length_in_chain (vm, b[0]);
       u32 len1 = vlib_buffer_length_in_chain (vm, b[1]);
 
-      /* Validate VXLAN tunnel encap-fib index agaist packet */
-      if (PREDICT_FALSE (error0 != 0))
-       {
-         next[0] = VXLAN_INPUT_NEXT_DROP;
+      next[0] = di0.next_index;
+      next[1] = di1.next_index;
 
-         if (error0 == VXLAN_ERROR_BAD_FLAGS)
-           {
-             vlib_increment_combined_counter
-               (drop_counter, thread_index, stats_t0->sw_if_index, 1, len0);
-           }
-         b[0]->error = node->errors[error0];
-         pkts_dropped++;
-       }
-      else
+      u8 any_error = di0.error | di1.error;
+      if (PREDICT_TRUE (any_error == 0))
        {
-         next[0] = t0->decap_next_index;
-
          /* Required to make the l2 tag push / pop code work on l2 subifs */
-         if (PREDICT_TRUE (next[0] == VXLAN_INPUT_NEXT_L2_INPUT))
-           vnet_update_l2_len (b[0]);
-
+         vnet_update_l2_len (b[0]);
+         vnet_update_l2_len (b[1]);
          /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
-         vnet_buffer (b[0])->sw_if_index[VLIB_RX] = t0->sw_if_index;
-         vlib_increment_combined_counter
-           (rx_counter, thread_index, stats_t0->sw_if_index, 1, len0);
+         vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
+         vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
+         vlib_increment_combined_counter (rx_counter, thread_index,
+                                          stats_if0, 1, len0);
+         vlib_increment_combined_counter (rx_counter, thread_index,
+                                          stats_if1, 1, len1);
        }
-
-      /* Validate VXLAN tunnel encap-fib index agaist packet */
-      if (PREDICT_FALSE (error1 != 0))
+      else
        {
-         next[1] = VXLAN_INPUT_NEXT_DROP;
-
-         if (error1 == VXLAN_ERROR_BAD_FLAGS)
+         if (di0.error == 0)
            {
-             vlib_increment_combined_counter
-               (drop_counter, thread_index, stats_t1->sw_if_index, 1, len1);
+             vnet_update_l2_len (b[0]);
+             vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
+             vlib_increment_combined_counter (rx_counter, thread_index,
+                                              stats_if0, 1, len0);
+           }
+         else
+           {
+             b[0]->error = node->errors[di0.error];
+             pkts_dropped++;
            }
-         b[1]->error = node->errors[error1];
-         pkts_dropped++;
-       }
-      else
-       {
-         next[1] = t1->decap_next_index;
-
-         /* Required to make the l2 tag push / pop code work on l2 subifs */
-         if (PREDICT_TRUE (next[1] == VXLAN_INPUT_NEXT_L2_INPUT))
-           vnet_update_l2_len (b[1]);
-
-         /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
-         vnet_buffer (b[1])->sw_if_index[VLIB_RX] = t1->sw_if_index;
 
-         vlib_increment_combined_counter
-           (rx_counter, thread_index, stats_t1->sw_if_index, 1, len1);
+         if (di1.error == 0)
+           {
+             vnet_update_l2_len (b[1]);
+             vnet_buffer (b[1])->sw_if_index[VLIB_RX] = di1.sw_if_index;
+             vlib_increment_combined_counter (rx_counter, thread_index,
+                                              stats_if1, 1, len1);
+           }
+         else
+           {
+             b[1]->error = node->errors[di1.error];
+             pkts_dropped++;
+           }
        }
 
       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
@@ -326,8 +306,9 @@ vxlan_input (vlib_main_t * vm,
          vxlan_rx_trace_t *tr =
            vlib_add_trace (vm, node, b[0], sizeof (*tr));
          tr->next_index = next[0];
-         tr->error = error0;
-         tr->tunnel_index = t0 == 0 ? ~0 : t0 - vxm->tunnels;
+         tr->error = di0.error;
+         tr->tunnel_index = di0.sw_if_index == ~0 ?
+           ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
          tr->vni = vnet_get_vni (vxlan0);
        }
       if (PREDICT_FALSE (b[1]->flags & VLIB_BUFFER_IS_TRACED))
@@ -335,8 +316,9 @@ vxlan_input (vlib_main_t * vm,
          vxlan_rx_trace_t *tr =
            vlib_add_trace (vm, node, b[1], sizeof (*tr));
          tr->next_index = next[1];
-         tr->error = error1;
-         tr->tunnel_index = t1 == 0 ? ~0 : t1 - vxm->tunnels;
+         tr->error = di1.error;
+         tr->tunnel_index = di1.sw_if_index == ~0 ?
+           ~0 : vxm->tunnel_index_by_sw_if_index[di1.sw_if_index];
          tr->vni = vnet_get_vni (vxlan1);
        }
       b += 2;
@@ -349,7 +331,6 @@ vxlan_input (vlib_main_t * vm,
       /* udp leaves current_data pointing at the vxlan header */
       void *cur0 = vlib_buffer_get_current (b[0]);
       vxlan_header_t *vxlan0 = cur0;
-      u8 error0 = vxlan0->flags != VXLAN_FLAGS_I ? VXLAN_ERROR_BAD_FLAGS : 0;
       ip4_header_t *ip4_0;
       ip6_header_t *ip6_0;
       if (is_ip4)
@@ -360,43 +341,32 @@ vxlan_input (vlib_main_t * vm,
       /* pop (ip, udp, vxlan) */
       vlib_buffer_advance (b[0], sizeof (*vxlan0));
 
-      u32 fi0 = buf_fib_index (b[0], is_ip4);
+      u32 fi0 = vlib_buffer_get_ip_fib_index (b[0], is_ip4);
 
-      vxlan_tunnel_t *t0, *stats_t0;
-      if (is_ip4)
-       t0 = vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_t0);
-      else
-       t0 = vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_t0);
+      vxlan_decap_info_t di0 = is_ip4 ?
+       vxlan4_find_tunnel (vxm, &last4, fi0, ip4_0, vxlan0, &stats_if0) :
+       vxlan6_find_tunnel (vxm, &last6, fi0, ip6_0, vxlan0, &stats_if0);
 
-      error0 = t0 == 0 ? VXLAN_ERROR_NO_SUCH_TUNNEL : error0;
       uword len0 = vlib_buffer_length_in_chain (vm, b[0]);
 
-      /* Validate VXLAN tunnel encap-fib index agaist packet */
-      if (PREDICT_FALSE (error0 != 0))
-       {
-         next[0] = VXLAN_INPUT_NEXT_DROP;
+      next[0] = di0.next_index;
 
-         if (error0 == VXLAN_ERROR_BAD_FLAGS)
-           {
-             vlib_increment_combined_counter
-               (drop_counter, thread_index, stats_t0->sw_if_index, 1, len0);
-           }
-         b[0]->error = node->errors[error0];
-         pkts_dropped++;
-       }
-      else
+      /* Validate VXLAN tunnel encap-fib index against packet */
+      if (di0.error == 0)
        {
-         next[0] = t0->decap_next_index;
-
          /* Required to make the l2 tag push / pop code work on l2 subifs */
-         if (PREDICT_TRUE (next[0] == VXLAN_INPUT_NEXT_L2_INPUT))
-           vnet_update_l2_len (b[0]);
+         vnet_update_l2_len (b[0]);
 
          /* Set packet input sw_if_index to unicast VXLAN tunnel for learning */
-         vnet_buffer (b[0])->sw_if_index[VLIB_RX] = t0->sw_if_index;
+         vnet_buffer (b[0])->sw_if_index[VLIB_RX] = di0.sw_if_index;
 
-         vlib_increment_combined_counter
-           (rx_counter, thread_index, stats_t0->sw_if_index, 1, len0);
+         vlib_increment_combined_counter (rx_counter, thread_index,
+                                          stats_if0, 1, len0);
+       }
+      else
+       {
+         b[0]->error = node->errors[di0.error];
+         pkts_dropped++;
        }
 
       if (PREDICT_FALSE (b[0]->flags & VLIB_BUFFER_IS_TRACED))
@@ -404,8 +374,9 @@ vxlan_input (vlib_main_t * vm,
          vxlan_rx_trace_t *tr
            = vlib_add_trace (vm, node, b[0], sizeof (*tr));
          tr->next_index = next[0];
-         tr->error = error0;
-         tr->tunnel_index = t0 == 0 ? ~0 : t0 - vxm->tunnels;
+         tr->error = di0.error;
+         tr->tunnel_index = di0.sw_if_index == ~0 ?
+           ~0 : vxm->tunnel_index_by_sw_if_index[di0.sw_if_index];
          tr->vni = vnet_get_vni (vxlan0);
        }
       b += 1;
@@ -478,7 +449,7 @@ typedef enum
   IP_VXLAN_BYPASS_NEXT_DROP,
   IP_VXLAN_BYPASS_NEXT_VXLAN,
   IP_VXLAN_BYPASS_N_NEXT,
-} ip_vxan_bypass_next_t;
+} ip_vxlan_bypass_next_t;
 
 always_inline uword
 ip_vxlan_bypass_inline (vlib_main_t * vm,
@@ -489,20 +460,39 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
   u32 *from, *to_next, n_left_from, n_left_to_next, next_index;
   vlib_node_runtime_t *error_node =
     vlib_node_get_runtime (vm, ip4_input_node.index);
-  ip4_address_t addr4;         /* last IPv4 address matching a local VTEP address */
-  ip6_address_t addr6;         /* last IPv6 address matching a local VTEP address */
+  vtep4_key_t last_vtep4;      /* last IPv4 address / fib index
+                                  matching a local VTEP address */
+  vtep6_key_t last_vtep6;      /* last IPv6 address / fib index
+                                  matching a local VTEP address */
+  vlib_buffer_t *bufs[VLIB_FRAME_SIZE], **b = bufs;
+
+  last_tunnel_cache4 last4;
+  last_tunnel_cache6 last6;
+
+#ifdef CLIB_HAVE_VEC512
+  vtep4_cache_t vtep4_u512;
+  clib_memset (&vtep4_u512, 0, sizeof (vtep4_u512));
+#endif
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;
   next_index = node->cached_next_index;
 
+  vlib_get_buffers (vm, from, bufs, n_left_from);
+
   if (node->flags & VLIB_NODE_FLAG_TRACE)
     ip4_forward_next_trace (vm, node, frame, VLIB_TX);
 
   if (is_ip4)
-    addr4.data_u32 = ~0;
+    {
+      vtep4_key_init (&last_vtep4);
+      clib_memset (&last4, 0xff, sizeof last4);
+    }
   else
-    ip6_address_set_zero (&addr6);
+    {
+      vtep6_key_init (&last_vtep6);
+      clib_memset (&last6, 0xff, sizeof last6);
+    }
 
   while (n_left_from > 0)
     {
@@ -514,24 +504,21 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          ip4_header_t *ip40, *ip41;
          ip6_header_t *ip60, *ip61;
          udp_header_t *udp0, *udp1;
+         vxlan_header_t *vxlan0, *vxlan1;
          u32 bi0, ip_len0, udp_len0, flags0, next0;
          u32 bi1, ip_len1, udp_len1, flags1, next1;
          i32 len_diff0, len_diff1;
          u8 error0, good_udp0, proto0;
          u8 error1, good_udp1, proto1;
+         u32 stats_if0 = ~0, stats_if1 = ~0;
 
          /* Prefetch next iteration. */
          {
-           vlib_buffer_t *p2, *p3;
-
-           p2 = vlib_get_buffer (vm, from[2]);
-           p3 = vlib_get_buffer (vm, from[3]);
-
-           vlib_prefetch_buffer_header (p2, LOAD);
-           vlib_prefetch_buffer_header (p3, LOAD);
+           vlib_prefetch_buffer_header (b[2], LOAD);
+           vlib_prefetch_buffer_header (b[3], LOAD);
 
-           CLIB_PREFETCH (p2->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
-           CLIB_PREFETCH (p3->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
+           CLIB_PREFETCH (b[2]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
+           CLIB_PREFETCH (b[3]->data, 2 * CLIB_CACHE_LINE_BYTES, LOAD);
          }
 
          bi0 = to_next[0] = from[0];
@@ -541,8 +528,9 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          to_next += 2;
          n_left_to_next -= 2;
 
-         b0 = vlib_get_buffer (vm, bi0);
-         b1 = vlib_get_buffer (vm, bi1);
+         b0 = b[0];
+         b1 = b[1];
+         b += 2;
          if (is_ip4)
            {
              ip40 = vlib_buffer_get_current (b0);
@@ -580,27 +568,33 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          else
            udp0 = ip6_next_header (ip60);
 
-         if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
-           goto exit0;         /* not VXLAN packet */
+         u32 fi0 = vlib_buffer_get_ip_fib_index (b0, is_ip4);
+         vxlan0 = vlib_buffer_get_current (b0) + sizeof (udp_header_t) +
+                  sizeof (ip4_header_t);
+
+         vxlan_decap_info_t di0 =
+           is_ip4 ?
+             vxlan4_find_tunnel (vxm, &last4, fi0, ip40, vxlan0, &stats_if0) :
+             vxlan6_find_tunnel (vxm, &last6, fi0, ip60, vxlan0, &stats_if0);
+
+         if (PREDICT_FALSE (di0.sw_if_index == ~0))
+           goto exit0; /* unknown interface */
 
          /* Validate DIP against VTEPs */
          if (is_ip4)
            {
-             if (addr4.as_u32 != ip40->dst_address.as_u32)
-               {
-                 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
-                   goto exit0; /* no local VTEP for VXLAN packet */
-                 addr4 = ip40->dst_address;
-               }
+#ifdef CLIB_HAVE_VEC512
+             if (!vtep4_check_vector
+                 (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
+#else
+             if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
+#endif
+               goto exit0;     /* no local VTEP for VXLAN packet */
            }
          else
            {
-             if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
-               {
-                 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
-                   goto exit0; /* no local VTEP for VXLAN packet */
-                 addr6 = ip60->dst_address;
-               }
+             if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
+               goto exit0;     /* no local VTEP for VXLAN packet */
            }
 
          flags0 = b0->flags;
@@ -620,15 +614,11 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          /* Verify UDP checksum */
          if (PREDICT_FALSE (!good_udp0))
            {
-             if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
-               {
-                 if (is_ip4)
-                   flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
-                 else
-                   flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
-                 good_udp0 =
-                   (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
-               }
+             if (is_ip4)
+               flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
+             else
+               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
+             good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
            }
 
          if (is_ip4)
@@ -666,27 +656,33 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          else
            udp1 = ip6_next_header (ip61);
 
-         if (udp1->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
-           goto exit1;         /* not VXLAN packet */
+         u32 fi1 = vlib_buffer_get_ip_fib_index (b1, is_ip4);
+         vxlan1 = vlib_buffer_get_current (b1) + sizeof (udp_header_t) +
+                  sizeof (ip4_header_t);
+
+         vxlan_decap_info_t di1 =
+           is_ip4 ?
+             vxlan4_find_tunnel (vxm, &last4, fi1, ip41, vxlan1, &stats_if1) :
+             vxlan6_find_tunnel (vxm, &last6, fi1, ip61, vxlan1, &stats_if1);
+
+         if (PREDICT_FALSE (di1.sw_if_index == ~0))
+           goto exit1; /* unknown interface */
 
          /* Validate DIP against VTEPs */
          if (is_ip4)
            {
-             if (addr4.as_u32 != ip41->dst_address.as_u32)
-               {
-                 if (!hash_get (vxm->vtep4, ip41->dst_address.as_u32))
-                   goto exit1; /* no local VTEP for VXLAN packet */
-                 addr4 = ip41->dst_address;
-               }
+#ifdef CLIB_HAVE_VEC512
+             if (!vtep4_check_vector
+                 (&vxm->vtep_table, b1, ip41, &last_vtep4, &vtep4_u512))
+#else
+             if (!vtep4_check (&vxm->vtep_table, b1, ip41, &last_vtep4))
+#endif
+               goto exit1;     /* no local VTEP for VXLAN packet */
            }
          else
            {
-             if (!ip6_address_is_equal (&addr6, &ip61->dst_address))
-               {
-                 if (!hash_get_mem (vxm->vtep6, &ip61->dst_address))
-                   goto exit1; /* no local VTEP for VXLAN packet */
-                 addr6 = ip61->dst_address;
-               }
+             if (!vtep6_check (&vxm->vtep_table, b1, ip61, &last_vtep6))
+               goto exit1;     /* no local VTEP for VXLAN packet */
            }
 
          flags1 = b1->flags;
@@ -706,15 +702,11 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          /* Verify UDP checksum */
          if (PREDICT_FALSE (!good_udp1))
            {
-             if ((flags1 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
-               {
-                 if (is_ip4)
-                   flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
-                 else
-                   flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
-                 good_udp1 =
-                   (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
-               }
+             if (is_ip4)
+               flags1 = ip4_tcp_udp_validate_checksum (vm, b1);
+             else
+               flags1 = ip6_tcp_udp_icmp_validate_checksum (vm, b1);
+             good_udp1 = (flags1 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
            }
 
          if (is_ip4)
@@ -754,9 +746,11 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          ip4_header_t *ip40;
          ip6_header_t *ip60;
          udp_header_t *udp0;
+         vxlan_header_t *vxlan0;
          u32 bi0, ip_len0, udp_len0, flags0, next0;
          i32 len_diff0;
          u8 error0, good_udp0, proto0;
+         u32 stats_if0 = ~0;
 
          bi0 = to_next[0] = from[0];
          from += 1;
@@ -764,7 +758,8 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          to_next += 1;
          n_left_to_next -= 1;
 
-         b0 = vlib_get_buffer (vm, bi0);
+         b0 = b[0];
+         b++;
          if (is_ip4)
            ip40 = vlib_buffer_get_current (b0);
          else
@@ -788,27 +783,33 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          else
            udp0 = ip6_next_header (ip60);
 
-         if (udp0->dst_port != clib_host_to_net_u16 (UDP_DST_PORT_vxlan))
-           goto exit;          /* not VXLAN packet */
+         u32 fi0 = vlib_buffer_get_ip_fib_index (b0, is_ip4);
+         vxlan0 = vlib_buffer_get_current (b0) + sizeof (udp_header_t) +
+                  sizeof (ip4_header_t);
+
+         vxlan_decap_info_t di0 =
+           is_ip4 ?
+             vxlan4_find_tunnel (vxm, &last4, fi0, ip40, vxlan0, &stats_if0) :
+             vxlan6_find_tunnel (vxm, &last6, fi0, ip60, vxlan0, &stats_if0);
+
+         if (PREDICT_FALSE (di0.sw_if_index == ~0))
+           goto exit; /* unknown interface */
 
          /* Validate DIP against VTEPs */
          if (is_ip4)
            {
-             if (addr4.as_u32 != ip40->dst_address.as_u32)
-               {
-                 if (!hash_get (vxm->vtep4, ip40->dst_address.as_u32))
-                   goto exit;  /* no local VTEP for VXLAN packet */
-                 addr4 = ip40->dst_address;
-               }
+#ifdef CLIB_HAVE_VEC512
+             if (!vtep4_check_vector
+                 (&vxm->vtep_table, b0, ip40, &last_vtep4, &vtep4_u512))
+#else
+             if (!vtep4_check (&vxm->vtep_table, b0, ip40, &last_vtep4))
+#endif
+               goto exit;      /* no local VTEP for VXLAN packet */
            }
          else
            {
-             if (!ip6_address_is_equal (&addr6, &ip60->dst_address))
-               {
-                 if (!hash_get_mem (vxm->vtep6, &ip60->dst_address))
-                   goto exit;  /* no local VTEP for VXLAN packet */
-                 addr6 = ip60->dst_address;
-               }
+             if (!vtep6_check (&vxm->vtep_table, b0, ip60, &last_vtep6))
+               goto exit;      /* no local VTEP for VXLAN packet */
            }
 
          flags0 = b0->flags;
@@ -828,15 +829,11 @@ ip_vxlan_bypass_inline (vlib_main_t * vm,
          /* Verify UDP checksum */
          if (PREDICT_FALSE (!good_udp0))
            {
-             if ((flags0 & VNET_BUFFER_F_L4_CHECKSUM_COMPUTED) == 0)
-               {
-                 if (is_ip4)
-                   flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
-                 else
-                   flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
-                 good_udp0 =
-                   (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
-               }
+             if (is_ip4)
+               flags0 = ip4_tcp_udp_validate_checksum (vm, b0);
+             else
+               flags0 = ip6_tcp_udp_icmp_validate_checksum (vm, b0);
+             good_udp0 = (flags0 & VNET_BUFFER_F_L4_CHECKSUM_CORRECT) != 0;
            }
 
          if (is_ip4)