avoid using thread local storage for thread index
[vpp.git] / src / vnet / gre / node.c
index 1a5fc87..056d00b 100644 (file)
@@ -73,18 +73,20 @@ gre_input (vlib_main_t * vm,
 {
   gre_main_t *gm = &gre_main;
   __attribute__ ((unused)) u32 n_left_from, next_index, *from, *to_next;
-  u64 cached_tunnel_key4;
-  u64 cached_tunnel_key6[4];
-  u32 cached_tunnel_sw_if_index = 0, tunnel_sw_if_index = 0;
+  gre_tunnel_key_t cached_tunnel_key;
 
-  u32 thread_index = vlib_get_thread_index ();
+  u32 cached_tunnel_sw_if_index = ~0, tunnel_sw_if_index = ~0;
+
+  u32 thread_index = vm->thread_index;
   u32 len;
   vnet_interface_main_t *im = &gm->vnet_main->interface_main;
 
   if (!is_ipv6)
-    memset (&cached_tunnel_key4, 0xff, sizeof (cached_tunnel_key4));
+    memset (&cached_tunnel_key.gtk_v4, 0xff,
+           sizeof (cached_tunnel_key.gtk_v4));
   else
-    memset (&cached_tunnel_key6, 0xff, sizeof (cached_tunnel_key6));
+    memset (&cached_tunnel_key.gtk_v6, 0xff,
+           sizeof (cached_tunnel_key.gtk_v6));
 
   from = vlib_frame_vector_args (from_frame);
   n_left_from = from_frame->n_vectors;
@@ -107,10 +109,7 @@ gre_input (vlib_main_t * vm,
          u32 i0, i1, next0, next1, protocol0, protocol1;
          ip4_header_t *ip4_0, *ip4_1;
          ip6_header_t *ip6_0, *ip6_1;
-         u32 ip4_tun_src0, ip4_tun_dst0;
-         u32 ip4_tun_src1, ip4_tun_dst1;
-         u64 ip6_tun_src0[2], ip6_tun_dst0[2];
-         u64 ip6_tun_src1[2], ip6_tun_dst1[2];
+         gre_tunnel_key_t key0, key1;
 
          /* Prefetch next iteration. */
          {
@@ -143,11 +142,6 @@ gre_input (vlib_main_t * vm,
              /* ip4_local hands us the ip header, not the gre header */
              ip4_0 = vlib_buffer_get_current (b0);
              ip4_1 = vlib_buffer_get_current (b1);
-             /* Save src + dst ip4 address, e.g. for mpls-o-gre */
-             ip4_tun_src0 = ip4_0->src_address.as_u32;
-             ip4_tun_dst0 = ip4_0->dst_address.as_u32;
-             ip4_tun_src1 = ip4_1->src_address.as_u32;
-             ip4_tun_dst1 = ip4_1->dst_address.as_u32;
 
              vlib_buffer_advance (b0, sizeof (*ip4_0));
              vlib_buffer_advance (b1, sizeof (*ip4_1));
@@ -157,15 +151,6 @@ gre_input (vlib_main_t * vm,
              /* ip6_local hands us the ip header, not the gre header */
              ip6_0 = vlib_buffer_get_current (b0);
              ip6_1 = vlib_buffer_get_current (b1);
-             /* Save src + dst ip6 address, e.g. for mpls-o-gre */
-             ip6_tun_src0[0] = ip6_0->src_address.as_u64[0];
-             ip6_tun_src0[1] = ip6_0->src_address.as_u64[1];
-             ip6_tun_dst0[0] = ip6_0->dst_address.as_u64[0];
-             ip6_tun_dst0[1] = ip6_0->dst_address.as_u64[1];
-             ip6_tun_src1[0] = ip6_1->src_address.as_u64[0];
-             ip6_tun_src1[1] = ip6_1->src_address.as_u64[1];
-             ip6_tun_dst1[0] = ip6_1->dst_address.as_u64[0];
-             ip6_tun_dst1[1] = ip6_1->dst_address.as_u64[1];
 
              vlib_buffer_advance (b0, sizeof (*ip6_0));
              vlib_buffer_advance (b1, sizeof (*ip6_1));
@@ -179,8 +164,10 @@ gre_input (vlib_main_t * vm,
          protocol1 = h1->protocol;
          sparse_vec_index2 (gm->next_by_protocol, protocol0, protocol1,
                             &i0, &i1);
-         next0 = vec_elt (gm->next_by_protocol, i0);
-         next1 = vec_elt (gm->next_by_protocol, i1);
+         next0 = vec_elt (gm->next_by_protocol, i0).next_index;
+         next1 = vec_elt (gm->next_by_protocol, i1).next_index;
+         u8 ttype0 = vec_elt (gm->next_by_protocol, i0).tunnel_type;
+         u8 ttype1 = vec_elt (gm->next_by_protocol, i1).tunnel_type;
 
          b0->error =
            node->errors[i0 ==
@@ -205,39 +192,39 @@ gre_input (vlib_main_t * vm,
 
 
          /* RPF check for ip4/ip6 input */
-         if (PREDICT_TRUE (next0 == GRE_INPUT_NEXT_IP4_INPUT
-                           || next0 == GRE_INPUT_NEXT_IP6_INPUT
-                           || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT
-                           || next0 == GRE_INPUT_NEXT_MPLS_INPUT))
+         if (PREDICT_TRUE (next0 > GRE_INPUT_NEXT_DROP))
            {
-
-             u64 key4, key6[4];
-             if (!is_ipv6)
+             if (is_ipv6)
                {
-                 key4 = ((u64) (ip4_tun_dst0) << 32) | (u64) (ip4_tun_src0);
+                 gre_mk_key6 (&ip6_0->dst_address,
+                              &ip6_0->src_address,
+                              vnet_buffer (b0)->ip.fib_index,
+                              ttype0, 0, &key0.gtk_v6);
                }
              else
                {
-                 key6[0] = ip6_tun_dst0[0];
-                 key6[1] = ip6_tun_dst0[1];
-                 key6[2] = ip6_tun_src0[0];
-                 key6[3] = ip6_tun_src0[1];
+                 gre_mk_key4 (ip4_0->dst_address,
+                              ip4_0->src_address,
+                              vnet_buffer (b0)->ip.fib_index,
+                              ttype0, 0, &key0.gtk_v4);
                }
 
-             if ((!is_ipv6 && cached_tunnel_key4 != key4) ||
-                 (is_ipv6 && cached_tunnel_key6[0] != key6[0] &&
-                  cached_tunnel_key6[1] != key6[1] &&
-                  cached_tunnel_key6[2] != key6[2] &&
-                  cached_tunnel_key6[3] != key6[3]))
+             if ((!is_ipv6 && !gre_match_key4 (&cached_tunnel_key.gtk_v4,
+                                               &key0.gtk_v4)) ||
+                 (is_ipv6 && !gre_match_key6 (&cached_tunnel_key.gtk_v6,
+                                              &key0.gtk_v6)))
                {
-                 vnet_hw_interface_t *hi;
                  gre_tunnel_t *t;
                  uword *p;
 
                  if (!is_ipv6)
-                   p = hash_get (gm->tunnel_by_key4, key4);
+                   {
+                     p = hash_get_mem (gm->tunnel_by_key4, &key0.gtk_v4);
+                   }
                  else
-                   p = hash_get_mem (gm->tunnel_by_key6, key6);
+                   {
+                     p = hash_get_mem (gm->tunnel_by_key6, &key0.gtk_v6);
+                   }
                  if (!p)
                    {
                      next0 = GRE_INPUT_NEXT_DROP;
@@ -245,10 +232,17 @@ gre_input (vlib_main_t * vm,
                      goto drop0;
                    }
                  t = pool_elt_at_index (gm->tunnels, p[0]);
-                 hi = vnet_get_hw_interface (gm->vnet_main, t->hw_if_index);
-                 tunnel_sw_if_index = hi->sw_if_index;
+                 tunnel_sw_if_index = t->sw_if_index;
 
                  cached_tunnel_sw_if_index = tunnel_sw_if_index;
+                 if (!is_ipv6)
+                   {
+                     cached_tunnel_key.gtk_v4 = key0.gtk_v4;
+                   }
+                 else
+                   {
+                     cached_tunnel_key.gtk_v6 = key0.gtk_v6;
+                   }
                }
              else
                {
@@ -271,39 +265,39 @@ gre_input (vlib_main_t * vm,
          vnet_buffer (b0)->sw_if_index[VLIB_RX] = tunnel_sw_if_index;
 
        drop0:
-         if (PREDICT_TRUE (next1 == GRE_INPUT_NEXT_IP4_INPUT
-                           || next1 == GRE_INPUT_NEXT_IP6_INPUT
-                           || next1 == GRE_INPUT_NEXT_ETHERNET_INPUT
-                           || next1 == GRE_INPUT_NEXT_MPLS_INPUT))
+         if (PREDICT_TRUE (next1 > GRE_INPUT_NEXT_DROP))
            {
-             u64 key4, key6[4];
-             if (!is_ipv6)
+             if (is_ipv6)
                {
-                 key4 = ((u64) (ip4_tun_dst1) << 32) | (u64) (ip4_tun_src1);
+                 gre_mk_key6 (&ip6_1->dst_address,
+                              &ip6_1->src_address,
+                              vnet_buffer (b1)->ip.fib_index,
+                              ttype1, 0, &key1.gtk_v6);
                }
              else
                {
-                 key6[0] = ip6_tun_dst1[0];
-                 key6[1] = ip6_tun_dst1[1];
-                 key6[2] = ip6_tun_src1[0];
-                 key6[3] = ip6_tun_src1[1];
+                 gre_mk_key4 (ip4_1->dst_address,
+                              ip4_1->src_address,
+                              vnet_buffer (b1)->ip.fib_index,
+                              ttype1, 0, &key1.gtk_v4);
                }
 
-             if ((!is_ipv6 && cached_tunnel_key4 != key4) ||
-                 (is_ipv6 && cached_tunnel_key6[0] != key6[0] &&
-                  cached_tunnel_key6[1] != key6[1] &&
-                  cached_tunnel_key6[2] != key6[2] &&
-                  cached_tunnel_key6[3] != key6[3]))
+             if ((!is_ipv6 && !gre_match_key4 (&cached_tunnel_key.gtk_v4,
+                                               &key1.gtk_v4)) ||
+                 (is_ipv6 && !gre_match_key6 (&cached_tunnel_key.gtk_v6,
+                                              &key1.gtk_v6)))
                {
-                 vnet_hw_interface_t *hi;
                  gre_tunnel_t *t;
                  uword *p;
 
                  if (!is_ipv6)
-                   p = hash_get (gm->tunnel_by_key4, key4);
+                   {
+                     p = hash_get_mem (gm->tunnel_by_key4, &key1.gtk_v4);
+                   }
                  else
-                   p = hash_get_mem (gm->tunnel_by_key6, key6);
-
+                   {
+                     p = hash_get_mem (gm->tunnel_by_key6, &key1.gtk_v6);
+                   }
                  if (!p)
                    {
                      next1 = GRE_INPUT_NEXT_DROP;
@@ -311,10 +305,17 @@ gre_input (vlib_main_t * vm,
                      goto drop1;
                    }
                  t = pool_elt_at_index (gm->tunnels, p[0]);
-                 hi = vnet_get_hw_interface (gm->vnet_main, t->hw_if_index);
-                 tunnel_sw_if_index = hi->sw_if_index;
+                 tunnel_sw_if_index = t->sw_if_index;
 
                  cached_tunnel_sw_if_index = tunnel_sw_if_index;
+                 if (!is_ipv6)
+                   {
+                     cached_tunnel_key.gtk_v4 = key1.gtk_v4;
+                   }
+                 else
+                   {
+                     cached_tunnel_key.gtk_v6 = key1.gtk_v6;
+                   }
                }
              else
                {
@@ -397,8 +398,7 @@ gre_input (vlib_main_t * vm,
          u16 version0;
          int verr0;
          u32 i0, next0;
-         u32 ip4_tun_src0, ip4_tun_dst0;
-         u64 ip6_tun_src0[2], ip6_tun_dst0[2];
+         gre_tunnel_key_t key0;
 
          bi0 = from[0];
          to_next[0] = bi0;
@@ -413,25 +413,18 @@ gre_input (vlib_main_t * vm,
 
          if (!is_ipv6)
            {
-             ip4_tun_src0 = ip4_0->src_address.as_u32;
-             ip4_tun_dst0 = ip4_0->dst_address.as_u32;
-
              vlib_buffer_advance (b0, sizeof (*ip4_0));
            }
          else
            {
-             ip6_tun_src0[0] = ip6_0->src_address.as_u64[0];
-             ip6_tun_src0[1] = ip6_0->src_address.as_u64[1];
-             ip6_tun_dst0[0] = ip6_0->dst_address.as_u64[0];
-             ip6_tun_dst0[1] = ip6_0->dst_address.as_u64[1];
-
              vlib_buffer_advance (b0, sizeof (*ip6_0));
            }
 
          h0 = vlib_buffer_get_current (b0);
 
          i0 = sparse_vec_index (gm->next_by_protocol, h0->protocol);
-         next0 = vec_elt (gm->next_by_protocol, i0);
+         next0 = vec_elt (gm->next_by_protocol, i0).next_index;
+         u8 ttype0 = vec_elt (gm->next_by_protocol, i0).tunnel_type;
 
          b0->error =
            node->errors[i0 == SPARSE_VEC_INVALID_INDEX
@@ -448,39 +441,39 @@ gre_input (vlib_main_t * vm,
             so we can increase counters and help forward node to
             pick right FIB */
          /* RPF check for ip4/ip6 input */
-         if (PREDICT_TRUE (next0 == GRE_INPUT_NEXT_IP4_INPUT
-                           || next0 == GRE_INPUT_NEXT_IP6_INPUT
-                           || next0 == GRE_INPUT_NEXT_ETHERNET_INPUT
-                           || next0 == GRE_INPUT_NEXT_MPLS_INPUT))
+         if (PREDICT_TRUE (next0 > GRE_INPUT_NEXT_DROP))
            {
-             u64 key4, key6[4];
-             if (!is_ipv6)
+             if (is_ipv6)
                {
-                 key4 = ((u64) (ip4_tun_dst0) << 32) | (u64) (ip4_tun_src0);
+                 gre_mk_key6 (&ip6_0->dst_address,
+                              &ip6_0->src_address,
+                              vnet_buffer (b0)->ip.fib_index,
+                              ttype0, 0, &key0.gtk_v6);
                }
              else
                {
-                 key6[0] = ip6_tun_dst0[0];
-                 key6[1] = ip6_tun_dst0[1];
-                 key6[2] = ip6_tun_src0[0];
-                 key6[3] = ip6_tun_src0[1];
+                 gre_mk_key4 (ip4_0->dst_address,
+                              ip4_0->src_address,
+                              vnet_buffer (b0)->ip.fib_index,
+                              ttype0, 0, &key0.gtk_v4);
                }
 
-             if ((!is_ipv6 && cached_tunnel_key4 != key4) ||
-                 (is_ipv6 && cached_tunnel_key6[0] != key6[0] &&
-                  cached_tunnel_key6[1] != key6[1] &&
-                  cached_tunnel_key6[2] != key6[2] &&
-                  cached_tunnel_key6[3] != key6[3]))
+             if ((!is_ipv6 && !gre_match_key4 (&cached_tunnel_key.gtk_v4,
+                                               &key0.gtk_v4)) ||
+                 (is_ipv6 && !gre_match_key6 (&cached_tunnel_key.gtk_v6,
+                                              &key0.gtk_v6)))
                {
-                 vnet_hw_interface_t *hi;
                  gre_tunnel_t *t;
                  uword *p;
 
                  if (!is_ipv6)
-                   p = hash_get (gm->tunnel_by_key4, key4);
+                   {
+                     p = hash_get_mem (gm->tunnel_by_key4, &key0.gtk_v4);
+                   }
                  else
-                   p = hash_get_mem (gm->tunnel_by_key6, key6);
-
+                   {
+                     p = hash_get_mem (gm->tunnel_by_key6, &key0.gtk_v6);
+                   }
                  if (!p)
                    {
                      next0 = GRE_INPUT_NEXT_DROP;
@@ -488,10 +481,17 @@ gre_input (vlib_main_t * vm,
                      goto drop;
                    }
                  t = pool_elt_at_index (gm->tunnels, p[0]);
-                 hi = vnet_get_hw_interface (gm->vnet_main, t->hw_if_index);
-                 tunnel_sw_if_index = hi->sw_if_index;
+                 tunnel_sw_if_index = t->sw_if_index;
 
                  cached_tunnel_sw_if_index = tunnel_sw_if_index;
+                 if (!is_ipv6)
+                   {
+                     cached_tunnel_key.gtk_v4 = key0.gtk_v4;
+                   }
+                 else
+                   {
+                     cached_tunnel_key.gtk_v6 = key0.gtk_v6;
+                   }
                }
              else
                {
@@ -592,9 +592,7 @@ VLIB_REGISTER_NODE (gre4_input_node) = {
   .format_trace = format_gre_rx_trace,
   .unformat_buffer = unformat_gre_header,
 };
-/* *INDENT-ON* */
 
-/* *INDENT-OFF* */
 VLIB_REGISTER_NODE (gre6_input_node) = {
   .function = gre6_input,
   .name = "gre6-input",
@@ -617,17 +615,19 @@ VLIB_REGISTER_NODE (gre6_input_node) = {
   .format_trace = format_gre_rx_trace,
   .unformat_buffer = unformat_gre_header,
 };
-/* *INDENT-ON* */
 
 VLIB_NODE_FUNCTION_MULTIARCH (gre4_input_node, gre4_input)
 VLIB_NODE_FUNCTION_MULTIARCH (gre6_input_node, gre6_input)
-     void
-       gre_register_input_protocol (vlib_main_t * vm,
-                                   gre_protocol_t protocol, u32 node_index)
+/* *INDENT-ON* */
+
+void
+gre_register_input_protocol (vlib_main_t * vm,
+                            gre_protocol_t protocol, u32 node_index,
+                            gre_tunnel_type_t tunnel_type)
 {
   gre_main_t *em = &gre_main;
   gre_protocol_info_t *pi;
-  u16 *n;
+  next_info_t *n;
   u32 i;
 
   {
@@ -638,6 +638,7 @@ VLIB_NODE_FUNCTION_MULTIARCH (gre6_input_node, gre6_input)
 
   pi = gre_get_protocol_info (em, protocol);
   pi->node_index = node_index;
+  pi->tunnel_type = tunnel_type;
   pi->next_index = vlib_node_add_next (vm, gre4_input_node.index, node_index);
   i = vlib_node_add_next (vm, gre6_input_node.index, node_index);
   ASSERT (i == pi->next_index);
@@ -645,7 +646,8 @@ VLIB_NODE_FUNCTION_MULTIARCH (gre6_input_node, gre6_input)
   /* Setup gre protocol -> next index sparse vector mapping. */
   n = sparse_vec_validate (em->next_by_protocol,
                           clib_host_to_net_u16 (protocol));
-  n[0] = pi->next_index;
+  n->next_index = pi->next_index;
+  n->tunnel_type = tunnel_type;
 }
 
 static void
@@ -689,14 +691,17 @@ gre_input_init (vlib_main_t * vm)
   mpls_unicast_input = vlib_get_node_by_name (vm, (u8 *) "mpls-input");
   ASSERT (mpls_unicast_input);
 
-  gre_register_input_protocol (vm, GRE_PROTOCOL_teb, ethernet_input->index);
+  gre_register_input_protocol (vm, GRE_PROTOCOL_teb,
+                              ethernet_input->index, GRE_TUNNEL_TYPE_TEB);
 
-  gre_register_input_protocol (vm, GRE_PROTOCOL_ip4, ip4_input->index);
+  gre_register_input_protocol (vm, GRE_PROTOCOL_ip4,
+                              ip4_input->index, GRE_TUNNEL_TYPE_L3);
 
-  gre_register_input_protocol (vm, GRE_PROTOCOL_ip6, ip6_input->index);
+  gre_register_input_protocol (vm, GRE_PROTOCOL_ip6,
+                              ip6_input->index, GRE_TUNNEL_TYPE_L3);
 
   gre_register_input_protocol (vm, GRE_PROTOCOL_mpls_unicast,
-                              mpls_unicast_input->index);
+                              mpls_unicast_input->index, GRE_TUNNEL_TYPE_L3);
 
   ip4_register_protocol (IP_PROTOCOL_GRE, gre4_input_node.index);
   ip6_register_protocol (IP_PROTOCOL_GRE, gre6_input_node.index);