Pipes
[vpp.git] / src / vnet / ethernet / node.c
index 6d57da3..8667667 100755 (executable)
@@ -40,6 +40,8 @@
 #include <vlib/vlib.h>
 #include <vnet/pg/pg.h>
 #include <vnet/ethernet/ethernet.h>
+#include <vnet/ethernet/p2p_ethernet.h>
+#include <vnet/devices/pipe/pipe.h>
 #include <vppinfra/sparse_vec.h>
 #include <vnet/l2/l2_bvi.h>
 
@@ -101,7 +103,8 @@ parse_header (ethernet_input_variant_t variant,
 
       e0 = (void *) (b0->data + b0->current_data);
 
-      vnet_buffer (b0)->ethernet.start_of_ethernet_header = b0->current_data;
+      vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
+      b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID;
 
       vlib_buffer_advance (b0, sizeof (e0[0]));
 
@@ -142,6 +145,8 @@ parse_header (ethernet_input_variant_t variant,
       tag = clib_net_to_host_u16 (h0->priority_cfi_and_id);
 
       *outer_id = tag & 0xfff;
+      if (0 == *outer_id)
+       *match_flags &= ~SUBINT_CONFIG_MATCH_1_TAG;
 
       *type = clib_net_to_host_u16 (h0->type);
 
@@ -163,11 +168,12 @@ parse_header (ethernet_input_variant_t variant,
 
          vlib_buffer_advance (b0, sizeof (h0[0]));
          vlan_count = 2;
-
          if (*type == ETHERNET_TYPE_VLAN)
            {
              // More than double tagged packet
              *match_flags = SUBINT_CONFIG_VALID | SUBINT_CONFIG_MATCH_3_TAG;
+
+             vlib_buffer_advance (b0, sizeof (h0[0]));
              vlan_count = 3;   // "unknown" number, aka, 3-or-more
            }
        }
@@ -202,9 +208,7 @@ identify_subint (vnet_hw_interface_t * hi,
       if (!(*is_l2))
        {
          ethernet_header_t *e0;
-         e0 =
-           (void *) (b0->data +
-                     vnet_buffer (b0)->ethernet.start_of_ethernet_header);
+         e0 = (void *) (b0->data + vnet_buffer (b0)->l2_hdr_offset);
 
          if (!(ethernet_address_cast (e0->dst_address)))
            {
@@ -233,10 +237,12 @@ determine_next_node (ethernet_main_t * em,
     }
   else if (is_l20)
     {
-      *next0 = em->l2_next;
       // record the L2 len and reset the buffer so the L2 header is preserved
-      u32 eth_start = vnet_buffer (b0)->ethernet.start_of_ethernet_header;
+      u32 eth_start = vnet_buffer (b0)->l2_hdr_offset;
       vnet_buffer (b0)->l2.l2_len = b0->current_data - eth_start;
+      *next0 = em->l2_next;
+      ASSERT (vnet_buffer (b0)->l2.l2_len ==
+             ethernet_buffer_header_size (b0));
       vlib_buffer_advance (b0, -ethernet_buffer_header_size (b0));
 
       // check for common IP/MPLS ethertypes
@@ -249,7 +255,7 @@ determine_next_node (ethernet_main_t * em,
     {
       *next0 = em->l3_next.input_next_ip6;
     }
-  else if (type0 == ETHERNET_TYPE_MPLS_UNICAST)
+  else if (type0 == ETHERNET_TYPE_MPLS)
     {
       *next0 = em->l3_next.input_next_mpls;
 
@@ -291,9 +297,10 @@ ethernet_input_inline (vlib_main_t * vm,
   vlib_node_runtime_t *error_node;
   u32 n_left_from, next_index, *from, *to_next;
   u32 stats_sw_if_index, stats_n_packets, stats_n_bytes;
-  u32 cpu_index = os_get_cpu_number ();
+  u32 thread_index = vlib_get_thread_index ();
   u32 cached_sw_if_index = ~0;
   u32 cached_is_l2 = 0;                /* shut up gcc */
+  vnet_hw_interface_t *hi = NULL;      /* used for main interface only */
 
   if (variant != ETHERNET_INPUT_VARIANT_ETHERNET)
     error_node = vlib_node_get_runtime (vm, ethernet_input_node.index);
@@ -371,8 +378,8 @@ ethernet_input_inline (vlib_main_t * vm,
 
          /* Speed-path for the untagged case */
          if (PREDICT_TRUE (variant == ETHERNET_INPUT_VARIANT_ETHERNET
-                           && !ethernet_frame_is_tagged (type0)
-                           && !ethernet_frame_is_tagged (type1)))
+                           && !ethernet_frame_is_any_tagged_x2 (type0,
+                                                                type1)))
            {
              main_intf_t *intf0;
              subint_config_t *subint0;
@@ -386,19 +393,26 @@ ethernet_input_inline (vlib_main_t * vm,
              if (PREDICT_FALSE (sw_if_index0 != sw_if_index1))
                goto slowpath;
 
+             /* Now sw_if_index0 == sw_if_index1  */
              if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
                {
                  cached_sw_if_index = sw_if_index0;
-                 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
-                 intf0 = vec_elt_at_index (em->main_intfs, hi0->hw_if_index);
+                 hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
+                 intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
                  subint0 = &intf0->untagged_subint;
                  cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
                }
 
-             vnet_buffer (b0)->ethernet.start_of_ethernet_header =
-               b0->current_data;
-             vnet_buffer (b1)->ethernet.start_of_ethernet_header =
-               b1->current_data;
+             vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
+             vnet_buffer (b1)->l2_hdr_offset = b1->current_data;
+             vnet_buffer (b0)->l3_hdr_offset =
+               vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
+             vnet_buffer (b1)->l3_hdr_offset =
+               vnet_buffer (b1)->l2_hdr_offset + sizeof (ethernet_header_t);
+             b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
+               VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
+             b1->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
+               VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
 
              if (PREDICT_TRUE (is_l20 != 0))
                {
@@ -409,12 +423,20 @@ ethernet_input_inline (vlib_main_t * vm,
                }
              else
                {
+                 if (!ethernet_address_cast (e0->dst_address) &&
+                     (hi->hw_address != 0) &&
+                     !eth_mac_equal ((u8 *) e0, hi->hw_address))
+                   error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
+                 if (!ethernet_address_cast (e1->dst_address) &&
+                     (hi->hw_address != 0) &&
+                     !eth_mac_equal ((u8 *) e1, hi->hw_address))
+                   error1 = ETHERNET_ERROR_L3_MAC_MISMATCH;
+                 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
                  determine_next_node (em, variant, 0, type0, b0,
                                       &error0, &next0);
-                 vlib_buffer_advance (b0, sizeof (ethernet_header_t));
+                 vlib_buffer_advance (b1, sizeof (ethernet_header_t));
                  determine_next_node (em, variant, 0, type1, b1,
                                       &error1, &next1);
-                 vlib_buffer_advance (b1, sizeof (ethernet_header_t));
                }
              goto ship_it01;
            }
@@ -482,9 +504,9 @@ ethernet_input_inline (vlib_main_t * vm,
            {
 
              len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
-               - vnet_buffer (b0)->ethernet.start_of_ethernet_header;
+               - vnet_buffer (b0)->l2_hdr_offset;
              len1 = vlib_buffer_length_in_chain (vm, b1) + b1->current_data
-               - vnet_buffer (b1)->ethernet.start_of_ethernet_header;
+               - vnet_buffer (b1)->l2_hdr_offset;
 
              stats_n_packets += 2;
              stats_n_bytes += len0 + len1;
@@ -502,7 +524,7 @@ ethernet_input_inline (vlib_main_t * vm,
                                                     interface_main.combined_sw_if_counters
                                                     +
                                                     VNET_INTERFACE_COUNTER_RX,
-                                                    cpu_index,
+                                                    thread_index,
                                                     new_sw_if_index0, 1,
                                                     len0);
                  if (new_sw_if_index1 != old_sw_if_index1
@@ -511,7 +533,7 @@ ethernet_input_inline (vlib_main_t * vm,
                                                     interface_main.combined_sw_if_counters
                                                     +
                                                     VNET_INTERFACE_COUNTER_RX,
-                                                    cpu_index,
+                                                    thread_index,
                                                     new_sw_if_index1, 1,
                                                     len1);
 
@@ -522,7 +544,7 @@ ethernet_input_inline (vlib_main_t * vm,
                          vlib_increment_combined_counter
                            (vnm->interface_main.combined_sw_if_counters
                             + VNET_INTERFACE_COUNTER_RX,
-                            cpu_index,
+                            thread_index,
                             stats_sw_if_index,
                             stats_n_packets, stats_n_bytes);
                          stats_n_packets = stats_n_bytes = 0;
@@ -539,11 +561,17 @@ ethernet_input_inline (vlib_main_t * vm,
                               &next0);
          determine_next_node (em, variant, is_l21, type1, b1, &error1,
                               &next1);
+         vnet_buffer (b0)->l3_hdr_offset = vnet_buffer (b0)->l2_hdr_offset +
+           vnet_buffer (b0)->l2.l2_len;
+         vnet_buffer (b1)->l3_hdr_offset = vnet_buffer (b1)->l2_hdr_offset +
+           vnet_buffer (b1)->l2.l2_len;
+         b0->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
+         b1->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
 
+       ship_it01:
          b0->error = error_node->errors[error0];
          b1->error = error_node->errors[error1];
 
-       ship_it01:
          // verify speculative enqueue
          vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
                                           n_left_to_next, bi0, bi1, next0,
@@ -603,14 +631,17 @@ ethernet_input_inline (vlib_main_t * vm,
              if (PREDICT_FALSE (cached_sw_if_index != sw_if_index0))
                {
                  cached_sw_if_index = sw_if_index0;
-                 hi0 = vnet_get_sup_hw_interface (vnm, sw_if_index0);
-                 intf0 = vec_elt_at_index (em->main_intfs, hi0->hw_if_index);
+                 hi = vnet_get_sup_hw_interface (vnm, sw_if_index0);
+                 intf0 = vec_elt_at_index (em->main_intfs, hi->hw_if_index);
                  subint0 = &intf0->untagged_subint;
                  cached_is_l2 = is_l20 = subint0->flags & SUBINT_CONFIG_L2;
                }
 
-             vnet_buffer (b0)->ethernet.start_of_ethernet_header =
-               b0->current_data;
+             vnet_buffer (b0)->l2_hdr_offset = b0->current_data;
+             vnet_buffer (b0)->l3_hdr_offset =
+               vnet_buffer (b0)->l2_hdr_offset + sizeof (ethernet_header_t);
+             b0->flags |= VNET_BUFFER_F_L2_HDR_OFFSET_VALID |
+               VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
 
              if (PREDICT_TRUE (is_l20 != 0))
                {
@@ -619,6 +650,10 @@ ethernet_input_inline (vlib_main_t * vm,
                }
              else
                {
+                 if (!ethernet_address_cast (e0->dst_address) &&
+                     (hi->hw_address != 0) &&
+                     !eth_mac_equal ((u8 *) e0, hi->hw_address))
+                   error0 = ETHERNET_ERROR_L3_MAC_MISMATCH;
                  determine_next_node (em, variant, 0, type0, b0,
                                       &error0, &next0);
                  vlib_buffer_advance (b0, sizeof (ethernet_header_t));
@@ -669,7 +704,7 @@ ethernet_input_inline (vlib_main_t * vm,
            {
 
              len0 = vlib_buffer_length_in_chain (vm, b0) + b0->current_data
-               - vnet_buffer (b0)->ethernet.start_of_ethernet_header;
+               - vnet_buffer (b0)->l2_hdr_offset;
 
              stats_n_packets += 1;
              stats_n_bytes += len0;
@@ -685,13 +720,13 @@ ethernet_input_inline (vlib_main_t * vm,
                    vlib_increment_combined_counter
                      (vnm->interface_main.combined_sw_if_counters
                       + VNET_INTERFACE_COUNTER_RX,
-                      cpu_index, new_sw_if_index0, 1, len0);
+                      thread_index, new_sw_if_index0, 1, len0);
                  if (stats_n_packets > 0)
                    {
                      vlib_increment_combined_counter
                        (vnm->interface_main.combined_sw_if_counters
                         + VNET_INTERFACE_COUNTER_RX,
-                        cpu_index,
+                        thread_index,
                         stats_sw_if_index, stats_n_packets, stats_n_bytes);
                      stats_n_packets = stats_n_bytes = 0;
                    }
@@ -704,11 +739,14 @@ ethernet_input_inline (vlib_main_t * vm,
 
          determine_next_node (em, variant, is_l20, type0, b0, &error0,
                               &next0);
+         vnet_buffer (b0)->l3_hdr_offset = vnet_buffer (b0)->l2_hdr_offset +
+           vnet_buffer (b0)->l2.l2_len;
+         b0->flags |= VNET_BUFFER_F_L3_HDR_OFFSET_VALID;
 
+       ship_it0:
          b0->error = error_node->errors[error0];
 
          // verify speculative enqueue
-       ship_it0:
          vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
                                           to_next, n_left_to_next,
                                           bi0, next0);
@@ -723,7 +761,7 @@ ethernet_input_inline (vlib_main_t * vm,
       vlib_increment_combined_counter
        (vnm->interface_main.combined_sw_if_counters
         + VNET_INTERFACE_COUNTER_RX,
-        cpu_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
+        thread_index, stats_sw_if_index, stats_n_packets, stats_n_bytes);
       node->runtime_data[0] = stats_sw_if_index;
     }
 
@@ -787,7 +825,29 @@ ethernet_sw_interface_get_config (vnet_main_t * vnm,
   // Locate the subint for the given ethernet config
   si = vnet_get_sw_interface (vnm, sw_if_index);
 
-  if (si->sub.eth.flags.default_sub)
+  if (si->type == VNET_SW_INTERFACE_TYPE_P2P)
+    {
+      p2p_ethernet_main_t *p2pm = &p2p_main;
+      u32 p2pe_sw_if_index =
+       p2p_ethernet_lookup (hi->hw_if_index, si->p2p.client_mac);
+      if (p2pe_sw_if_index == ~0)
+       {
+         pool_get (p2pm->p2p_subif_pool, subint);
+         si->p2p.pool_index = subint - p2pm->p2p_subif_pool;
+       }
+      else
+       subint = vec_elt_at_index (p2pm->p2p_subif_pool, si->p2p.pool_index);
+      *flags = SUBINT_CONFIG_P2P;
+    }
+  else if (si->type == VNET_SW_INTERFACE_TYPE_PIPE)
+    {
+      pipe_t *pipe;
+
+      pipe = pipe_get (sw_if_index);
+      subint = &pipe->subint;
+      *flags = SUBINT_CONFIG_P2P;
+    }
+  else if (si->sub.eth.flags.default_sub)
     {
       subint = &main_intf->default_subint;
       *flags = SUBINT_CONFIG_MATCH_0_TAG |
@@ -1076,7 +1136,7 @@ ethernet_sw_interface_add_del (vnet_main_t * vnm,
     }
   else
     {
-      // Note that config is L3 by defaulty
+      // Note that config is L3 by default
       subint->flags = SUBINT_CONFIG_VALID | match_flags;
       subint->sw_if_index = ~0;        // because interfaces are initially down
     }
@@ -1241,7 +1301,7 @@ next_by_ethertype_register (next_by_ethertype_t * l3_next,
        {
          l3_next->input_next_ip6 = next_index;
        }
-      else if (ethertype == ETHERNET_TYPE_MPLS_UNICAST)
+      else if (ethertype == ETHERNET_TYPE_MPLS)
        {
          l3_next->input_next_mpls = next_index;
        }