VPP-369 Add an L2 output classification feature 58/2558/7
authorDave Barach <[email protected]>
Tue, 30 Aug 2016 21:01:52 +0000 (17:01 -0400)
committerKeith Burns <[email protected]>
Wed, 31 Aug 2016 21:53:01 +0000 (21:53 +0000)
Change-Id: If03162d328c1ea179249e734537ebb01bade3331
Signed-off-by: Dave Barach <[email protected]>
13 files changed:
vnet/Makefile.am
vnet/vnet/classify/vnet_classify.c
vnet/vnet/l2/l2_classify.h
vnet/vnet/l2/l2_input.c
vnet/vnet/l2/l2_input.h
vnet/vnet/l2/l2_input_classify.c [moved from vnet/vnet/l2/l2_classify.c with 58% similarity]
vnet/vnet/l2/l2_output.h
vnet/vnet/l2/l2_output_classify.c [new file with mode: 0644]
vpp-api-test/vat/api_format.c
vpp/app/sticky_hash.c
vpp/vpp-api/api.c
vpp/vpp-api/custom_dump.c
vpp/vpp-api/vpe.api

index fd9c3e5..71680b9 100644 (file)
@@ -111,7 +111,8 @@ libvnet_la_SOURCES +=                               \
  vnet/l2/feat_bitmap.c                         \
  vnet/l2/l2_bd.c                               \
  vnet/l2/l2_bvi.c                              \
- vnet/l2/l2_classify.c                         \
+ vnet/l2/l2_input_classify.c                   \
+ vnet/l2/l2_output_classify.c                  \
  vnet/l2/l2_efp_filter.c                       \
  vnet/l2/l2_fib.c                              \
  vnet/l2/l2_flood.c                            \
index 3b75ab5..2eee0f5 100644 (file)
@@ -16,7 +16,7 @@
 #include <vnet/classify/input_acl.h>
 #include <vnet/ip/ip.h>
 #include <vnet/api_errno.h>     /* for API error numbers */
-#include <vnet/l2/l2_classify.h> /* for L2_CLASSIFY_NEXT_xxx */
+#include <vnet/l2/l2_classify.h> /* for L2_INPUT_CLASSIFY_NEXT_xxx */
 
 vnet_classify_main_t vnet_classify_main;
 
@@ -1023,14 +1023,14 @@ uword unformat_classify_mask (unformat_input_t * input, va_list * args)
   return 0;
 }
 
-#define foreach_l2_next                         \
+#define foreach_l2_input_next                   \
 _(drop, DROP)                                   \
 _(ethernet, ETHERNET_INPUT)                     \
 _(ip4, IP4_INPUT)                               \
 _(ip6, IP6_INPUT)                              \
 _(li, LI)
 
-uword unformat_l2_next_index (unformat_input_t * input, va_list * args)
+uword unformat_l2_input_next_index (unformat_input_t * input, va_list * args)
 {
   vnet_classify_main_t * cm = &vnet_classify_main;
   u32 * miss_next_indexp = va_arg (*args, u32 *);
@@ -1049,8 +1049,47 @@ uword unformat_l2_next_index (unformat_input_t * input, va_list * args)
     }
 
 #define _(n,N) \
-  if (unformat (input, #n)) { next_index = L2_CLASSIFY_NEXT_##N; goto out;}
-  foreach_l2_next;
+  if (unformat (input, #n)) { next_index = L2_INPUT_CLASSIFY_NEXT_##N; goto out;}
+  foreach_l2_input_next;
+#undef _
+  
+  if (unformat (input, "%d", &tmp))
+    { 
+      next_index = tmp; 
+      goto out; 
+    }
+  
+  return 0;
+
+ out:
+  *miss_next_indexp = next_index;
+  return 1;
+}
+
+#define foreach_l2_output_next                   \
+_(drop, DROP)
+
+uword unformat_l2_output_next_index (unformat_input_t * input, va_list * args)
+{
+  vnet_classify_main_t * cm = &vnet_classify_main;
+  u32 * miss_next_indexp = va_arg (*args, u32 *);
+  u32 next_index = 0;
+  u32 tmp;
+  int i;
+  
+  /* First try registered unformat fns, allowing override... */
+  for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++)
+    {
+      if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp))
+        {
+          next_index = tmp;
+          goto out;
+        }
+    }
+
+#define _(n,N) \
+  if (unformat (input, #n)) { next_index = L2_OUTPUT_CLASSIFY_NEXT_##N; goto out;}
+  foreach_l2_output_next;
 #undef _
   
   if (unformat (input, "%d", &tmp))
@@ -1225,7 +1264,10 @@ classify_table_command_fn (vlib_main_t * vm,
     else if (unformat (input, "miss-next %U", unformat_ip_next_index,
                        &miss_next_index))
       ;
-    else if (unformat (input, "l2-miss-next %U", unformat_l2_next_index,
+    else if (unformat (input, "l2-input-miss-next %U", unformat_l2_input_next_index,
+                       &miss_next_index))
+        ;
+    else if (unformat (input, "l2-output-miss-next %U", unformat_l2_output_next_index,
                        &miss_next_index))
       ;
     else if (unformat (input, "acl-miss-next %U", unformat_acl_next_index,
@@ -1802,7 +1844,10 @@ classify_session_command_fn (vlib_main_t * vm,
       else if (unformat (input, "hit-next %U", unformat_ip_next_index,
                          &hit_next_index))
         ;
-      else if (unformat (input, "l2-hit-next %U", unformat_l2_next_index,
+      else if (unformat (input, "l2-input-hit-next %U", unformat_l2_input_next_index,
+                         &hit_next_index))
+        ;
+      else if (unformat (input, "l2-output-hit-next %U", unformat_l2_output_next_index,
                          &hit_next_index))
         ;
       else if (unformat (input, "acl-hit-next %U", unformat_acl_next_index,
@@ -1929,18 +1974,18 @@ unformat_acl_next_node (unformat_input_t * input, va_list * args)
 }
 
 static uword 
-unformat_l2_next_node (unformat_input_t * input, va_list * args)
+unformat_l2_input_next_node (unformat_input_t * input, va_list * args)
 {
   vnet_classify_main_t * cm = &vnet_classify_main;
   u32 * next_indexp = va_arg (*args, u32 *);
   u32 node_index;
   u32 next_index;
 
-  if (unformat (input, "node %U", unformat_vlib_node,
+  if (unformat (input, "input-node %U", unformat_vlib_node,
                 cm->vlib_main, &node_index))
     {
       next_index = vlib_node_add_next 
-        (cm->vlib_main, l2_classify_node.index, node_index);
+        (cm->vlib_main, l2_input_classify_node.index, node_index);
 
       *next_indexp = next_index;
       return 1;
@@ -1948,6 +1993,25 @@ unformat_l2_next_node (unformat_input_t * input, va_list * args)
   return 0;
 }
 
+static uword 
+unformat_l2_output_next_node (unformat_input_t * input, va_list * args)
+{
+  vnet_classify_main_t * cm = &vnet_classify_main;
+  u32 * next_indexp = va_arg (*args, u32 *);
+  u32 node_index;
+  u32 next_index;
+
+  if (unformat (input, "output-node %U", unformat_vlib_node,
+                cm->vlib_main, &node_index))
+    {
+      next_index = vlib_node_add_next 
+        (cm->vlib_main, l2_output_classify_node.index, node_index);
+
+      *next_indexp = next_index;
+      return 1;
+    }
+  return 0;
+}
 
 static clib_error_t * 
 vnet_classify_init (vlib_main_t * vm)
@@ -1964,7 +2028,13 @@ vnet_classify_init (vlib_main_t * vm)
     (unformat_ip_next_node);
 
   vnet_classify_register_unformat_l2_next_index_fn
-    (unformat_l2_next_node);
+    (unformat_l2_input_next_node);
+
+  vnet_classify_register_unformat_l2_next_index_fn
+    (unformat_l2_input_next_node);
+
+  vnet_classify_register_unformat_l2_next_index_fn
+    (unformat_l2_output_next_node);
 
   vnet_classify_register_unformat_acl_next_index_fn
     (unformat_acl_next_node);
index f66e520..184187f 100644 (file)
@@ -13,8 +13,8 @@
  * limitations under the License.
  */
 
-#ifndef __included_vnet_l2_classify_h__
-#define __included_vnet_l2_classify_h__
+#ifndef __included_vnet_l2_input_classify_h__
+#define __included_vnet_l2_input_classify_h__
 
 #include <vlib/vlib.h>
 #include <vnet/vnet.h>
@@ -26,6 +26,7 @@
 #include <vnet/ip/ip6_packet.h>
 #include <vlib/cli.h>
 #include <vnet/l2/l2_input.h>
+#include <vnet/l2/l2_output.h>
 #include <vnet/l2/feat_bitmap.h>
 #include <vppinfra/error.h>
 #include <vppinfra/hash.h>
 
 typedef enum
 {
-  L2_CLASSIFY_NEXT_DROP,
-  L2_CLASSIFY_NEXT_ETHERNET_INPUT,
-  L2_CLASSIFY_NEXT_IP4_INPUT,
-  L2_CLASSIFY_NEXT_IP6_INPUT,
-  L2_CLASSIFY_NEXT_LI,
-  L2_CLASSIFY_N_NEXT,
-} l2_classify_next_t;
+  L2_INPUT_CLASSIFY_NEXT_DROP,
+  L2_INPUT_CLASSIFY_NEXT_ETHERNET_INPUT,
+  L2_INPUT_CLASSIFY_NEXT_IP4_INPUT,
+  L2_INPUT_CLASSIFY_NEXT_IP6_INPUT,
+  L2_INPUT_CLASSIFY_NEXT_LI,
+  L2_INPUT_CLASSIFY_N_NEXT,
+} l2_input_classify_next_t;
 
 typedef enum
 {
-  L2_CLASSIFY_TABLE_IP4,
-  L2_CLASSIFY_TABLE_IP6,
-  L2_CLASSIFY_TABLE_OTHER,
-  L2_CLASSIFY_N_TABLES,
-} l2_classify_table_id_t;
+  L2_INPUT_CLASSIFY_TABLE_IP4,
+  L2_INPUT_CLASSIFY_TABLE_IP6,
+  L2_INPUT_CLASSIFY_TABLE_OTHER,
+  L2_INPUT_CLASSIFY_N_TABLES,
+} l2_input_classify_table_id_t;
 
-typedef struct
+typedef enum
 {
+  L2_OUTPUT_CLASSIFY_NEXT_DROP,
+  L2_OUTPUT_CLASSIFY_N_NEXT,
+} l2_output_classify_next_t;
 
+typedef enum
+{
+  L2_OUTPUT_CLASSIFY_TABLE_IP4,
+  L2_OUTPUT_CLASSIFY_TABLE_IP6,
+  L2_OUTPUT_CLASSIFY_TABLE_OTHER,
+  L2_OUTPUT_CLASSIFY_N_TABLES,
+} l2_output_classify_table_id_t;
+
+typedef struct _l2_classify_main
+{
   /* Next nodes for each feature */
   u32 feat_next_node_index[32];
 
   /* Per-address-family classifier table vectors */
-  u32 *classify_table_index_by_sw_if_index[L2_CLASSIFY_N_TABLES];
+  u32 *classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_N_TABLES];
+
+  /* Next nodes for features and output interfaces */
+  l2_output_next_nodes_st next_nodes;
 
   /* convenience variables */
   vlib_main_t *vlib_main;
   vnet_main_t *vnet_main;
   vnet_classify_main_t *vnet_classify_main;
-} l2_classify_main_t;
+} l2_input_classify_main_t;
+
+typedef struct _l2_classify_main l2_output_classify_main_t;
+
+extern l2_input_classify_main_t l2_input_classify_main;
+extern vlib_node_registration_t l2_input_classify_node;
+
+extern l2_output_classify_main_t l2_output_classify_main;
+extern vlib_node_registration_t l2_output_classify_node;
 
-l2_classify_main_t l2_classify_main;
+void vnet_l2_input_classify_enable_disable (u32 sw_if_index,
+                                           int enable_disable);
 
-extern vlib_node_registration_t l2_classify_node;
+int vnet_l2_input_classify_set_tables (u32 sw_if_index, u32 ip4_table_index,
+                                      u32 ip6_table_index,
+                                      u32 other_table_index);
 
-void vnet_l2_classify_enable_disable (u32 sw_if_index, int enable_disable);
+void vnet_l2_output_classify_enable_disable (u32 sw_if_index,
+                                            int enable_disable);
 
-int vnet_l2_classify_set_tables (u32 sw_if_index, u32 ip4_table_index,
-                                u32 ip6_table_index, u32 other_table_index);
+int vnet_l2_output_classify_set_tables (u32 sw_if_index, u32 ip4_table_index,
+                                       u32 ip6_table_index,
+                                       u32 other_table_index);
 
-#endif /* __included_vnet_l2_classify_h__ */
+#endif /* __included_vnet_l2_input_classify_h__ */
 
 /*
  * fd.io coding-style-patch-verification: ON
index f337b78..5c39b79 100644 (file)
@@ -621,7 +621,8 @@ set_int_l2_mode (vlib_main_t * vm, vnet_main_t * vnet_main, u32 mode, u32 sw_if_
       config->output_sw_if_index = xc_sw_if_index;
 
       /* Make sure last-chance drop is configured */
-      config->feature_bitmap |= L2INPUT_FEAT_DROP | L2INPUT_FEAT_CLASSIFY;
+      config->feature_bitmap |=
+       L2INPUT_FEAT_DROP | L2INPUT_FEAT_INPUT_CLASSIFY;
 
       /* Make sure bridging features are disabled */
       config->feature_bitmap &=
@@ -1030,7 +1031,7 @@ VLIB_CLI_COMMAND (show_l2_mode, static) = {
 #define foreach_l2_init_function                \
 _(feat_bitmap_drop_init)                        \
 _(l2fib_init)                                   \
-_(l2_classify_init)                             \
+_(l2_input_classify_init)                             \
 _(l2bd_init)                                    \
 _(l2fwd_init)                                   \
 _(l2_inacl_init)                                \
index 3d8fc0a..48597ce 100644 (file)
@@ -91,7 +91,6 @@ l2input_bd_config_from_index (l2input_main_t * l2im, u32 bd_index)
 /* Mappings from feature ID to graph node name */
 #define foreach_l2input_feat                    \
  _(DROP,          "feature-bitmap-drop")        \
- _(CLASSIFY,      "l2-classify")               \
  _(XCONNECT,      "l2-output")                  \
  _(IPIW,          "feature-bitmap-drop")        \
  _(FLOOD,         "l2-flood")                   \
@@ -113,7 +112,8 @@ l2input_bd_config_from_index (l2input_main_t * l2im, u32 bd_index)
  _(QOS,           "feature-bitmap-drop")        \
  _(CFM,           "feature-bitmap-drop")        \
  _(SPAN,          "feature-bitmap-drop")        \
- _(POLICER_CLAS,  "l2-policer-classify")
+ _(POLICER_CLAS,  "l2-policer-classify")       \
+ _(INPUT_CLASSIFY, "l2-input-classify")
 
 /* Feature bitmap positions */
 typedef enum
similarity index 58%
rename from vnet/vnet/l2/l2_classify.c
rename to vnet/vnet/l2/l2_input_classify.c
index 737532d..1b9f911 100644 (file)
 #include <vnet/l2/l2_classify.h>
 #include <vnet/api_errno.h>
 
+/**
+ * @file
+ * @brief L2 input classifier
+ *
+ * See also .../vnet/vnet/classify/vnet_classify.[ch]
+ */
+
+/**
+ * @brief l2_input_classifier packet trace record
+ */
 typedef struct
 {
-  /* per-pkt trace data */
+  /** interface handle for the ith packet */
   u32 sw_if_index;
+  /** graph arc index selected for this packet */
   u32 next_index;
+  /** classifier table which provided the final result */
   u32 table_index;
+  /** offset in classifier heap of the corresponding session */
   u32 session_offset;
-} l2_classify_trace_t;
+} l2_input_classify_trace_t;
 
+/**
+ * @brief vlib node runtime
+ */
 typedef struct
 {
+  /** use-case independent main object pointer */
   vnet_classify_main_t *vcm;
-  l2_classify_main_t *l2cm;
-} l2_classify_runtime_t;
+  /** l2 input classifier main object pointer */
+  l2_input_classify_main_t *l2cm;
+} l2_input_classify_runtime_t;
 
-/* packet trace format function */
+/** packet trace format function */
 static u8 *
-format_l2_classify_trace (u8 * s, va_list * args)
+format_l2_input_classify_trace (u8 * s, va_list * args)
 {
   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
-  l2_classify_trace_t *t = va_arg (*args, l2_classify_trace_t *);
+  l2_input_classify_trace_t *t = va_arg (*args, l2_input_classify_trace_t *);
 
   s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d",
              t->sw_if_index, t->table_index, t->session_offset,
@@ -48,11 +66,12 @@ format_l2_classify_trace (u8 * s, va_list * args)
   return s;
 }
 
-l2_classify_main_t l2_classify_main;
+/** l2 input classifier main data structure */
+l2_input_classify_main_t l2_input_classify_main;
 
-vlib_node_registration_t l2_classify_node;
+vlib_node_registration_t l2_input_classify_node;
 
-#define foreach_l2_classify_error               \
+#define foreach_l2_input_classify_error               \
 _(MISS, "Classify misses")                      \
 _(HIT, "Classify hits")                         \
 _(CHAIN_HIT, "Classify hits after chain walk")  \
@@ -60,27 +79,78 @@ _(DROP, "L2 Classify Drops")
 
 typedef enum
 {
-#define _(sym,str) L2_CLASSIFY_ERROR_##sym,
-  foreach_l2_classify_error
+#define _(sym,str) L2_INPUT_CLASSIFY_ERROR_##sym,
+  foreach_l2_input_classify_error
 #undef _
-    L2_CLASSIFY_N_ERROR,
-} l2_classify_error_t;
+    L2_INPUT_CLASSIFY_N_ERROR,
+} l2_input_classify_error_t;
 
-static char *l2_classify_error_strings[] = {
+static char *l2_input_classify_error_strings[] = {
 #define _(sym,string) string,
-  foreach_l2_classify_error
+  foreach_l2_input_classify_error
 #undef _
 };
 
+/**
+ * @brief l2 input classifier node.
+ * @node l2-input-classify
+ *
+ * This is the l2 input classifier dispatch node
+ *
+ * @param vm    vlib_main_t corresponding to the current thread.
+ * @param node  vlib_node_runtime_t data for this node.
+ * @param frame vlib_frame_t whose contents should be dispatched.
+ *
+ * @par Graph mechanics: buffer metadata, next index usage
+ *
+ * @em Uses:
+ * - <code>(l2_input_classify_runtime_t *)
+ *         rt->classify_table_index_by_sw_if_index</code>
+ *        Head of the per-interface, perprotocol classifier table chain
+ *        for a specific interface. ~0 => send pkts to the next
+ *        feature in the L2 feature chain.
+ * - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
+ *     - Indicates the @c sw_if_index value of the interface that the
+ *     packet was received on.
+ * - <code>vnet_buffer (b0)->l2.feature_bitmap</code>
+ *     - Used to steer packets across l2 features enabled on the interface
+ * - <code>(vnet_classify_entry_t) e0->next_index</code>
+ *     - Used to steer traffic when the classifier hits on a session
+ * - <code>(vnet_classify_entry_t) e0->advance</code>
+ *     - Signed quantity applied via <code>vlib_buffer_advance</code>
+ *     when the classifier hits on a session
+ * - <code>(vnet_classify_table_t) t0->miss_next_index</code>
+ *     - Used to steer traffic when the classifier misses
+ *
+ * @em Sets:
+ * - <code>vnet_buffer (b0)->l2_classify.table_index</code>
+ *     - Classifier table index of the first classifier table in
+ *     the classifier table chain
+ * - <code>vnet_buffer (b0)->l2_classify.hash</code>
+ *     - Bounded-index extensible hash corresponding to the
+ *     masked fields in the current packet
+ * - <code>vnet_buffer (b0)->l2.feature_bitmap</code>
+ *     - Used to steer packets across l2 features enabled on the interface
+ * - <code>vnet_buffer (b0)->l2_classify.opaque_index</code>
+ *     - Copied from the classifier session object upon classifier hit
+ *
+ * @em Counters:
+ * - <code>L2_INPUT_CLASSIFY_ERROR_MISS</code> Classifier misses
+ * - <code>L2_INPUT_CLASSIFY_ERROR_HIT</code> Classifier hits
+ * - <code>L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT</code>
+ *   Classifier hits in other than the first table
+ */
+
 static uword
-l2_classify_node_fn (vlib_main_t * vm,
-                    vlib_node_runtime_t * node, vlib_frame_t * frame)
+l2_input_classify_node_fn (vlib_main_t * vm,
+                          vlib_node_runtime_t * node, vlib_frame_t * frame)
 {
   u32 n_left_from, *from, *to_next;
-  l2_classify_next_t next_index;
-  l2_classify_main_t *cm = &l2_classify_main;
+  l2_input_classify_next_t next_index;
+  l2_input_classify_main_t *cm = &l2_input_classify_main;
   vnet_classify_main_t *vcm = cm->vnet_classify_main;
-  l2_classify_runtime_t *rt = (l2_classify_runtime_t *) node->runtime_data;
+  l2_input_classify_runtime_t *rt =
+    (l2_input_classify_runtime_t *) node->runtime_data;
   u32 feature_bitmap;
   u32 hits = 0;
   u32 misses = 0;
@@ -142,14 +212,14 @@ l2_classify_node_fn (vlib_main_t * vm,
       type1 = clib_net_to_host_u16 (h1->type);
 
       type_index0 = (type0 == ETHERNET_TYPE_IP4)
-       ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
+       ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER;
       type_index0 = (type0 == ETHERNET_TYPE_IP6)
-       ? L2_CLASSIFY_TABLE_IP6 : type_index0;
+       ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0;
 
       type_index1 = (type1 == ETHERNET_TYPE_IP4)
-       ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
+       ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER;
       type_index1 = (type1 == ETHERNET_TYPE_IP6)
-       ? L2_CLASSIFY_TABLE_IP6 : type_index1;
+       ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index1;
 
       vnet_buffer (b0)->l2_classify.table_index =
        table_index0 =
@@ -206,9 +276,9 @@ l2_classify_node_fn (vlib_main_t * vm,
       type0 = clib_net_to_host_u16 (h0->type);
 
       type_index0 = (type0 == ETHERNET_TYPE_IP4)
-       ? L2_CLASSIFY_TABLE_IP4 : L2_CLASSIFY_TABLE_OTHER;
+       ? L2_INPUT_CLASSIFY_TABLE_IP4 : L2_INPUT_CLASSIFY_TABLE_OTHER;
       type_index0 = (type0 == ETHERNET_TYPE_IP6)
-       ? L2_CLASSIFY_TABLE_IP6 : type_index0;
+       ? L2_INPUT_CLASSIFY_TABLE_IP6 : type_index0;
 
       vnet_buffer (b0)->l2_classify.table_index =
        table_index0 = rt->l2cm->classify_table_index_by_sw_if_index
@@ -241,7 +311,7 @@ l2_classify_node_fn (vlib_main_t * vm,
        {
          u32 bi0;
          vlib_buffer_t *b0;
-         u32 next0 = L2_CLASSIFY_NEXT_ETHERNET_INPUT;
+         u32 next0 = ~0;       /* next l2 input feature, please... */
          ethernet_header_t *h0;
          u32 table_index0;
          u64 hash0;
@@ -283,6 +353,13 @@ l2_classify_node_fn (vlib_main_t * vm,
          e0 = 0;
          vnet_buffer (b0)->l2_classify.opaque_index = ~0;
 
+         /* Remove ourself from the feature bitmap */
+         feature_bitmap = vnet_buffer (b0)->l2.feature_bitmap
+           & ~L2INPUT_FEAT_INPUT_CLASSIFY;
+
+         /* save for next feature graph nodes */
+         vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
+
          if (PREDICT_TRUE (table_index0 != ~0))
            {
              hash0 = vnet_buffer (b0)->l2_classify.hash;
@@ -332,18 +409,10 @@ l2_classify_node_fn (vlib_main_t * vm,
            }
 
          if (PREDICT_FALSE (next0 == 0))
-           b0->error = node->errors[L2_CLASSIFY_ERROR_DROP];
+           b0->error = node->errors[L2_INPUT_CLASSIFY_ERROR_DROP];
 
-         if (PREDICT_FALSE (next0 == ~0))
+         if (PREDICT_TRUE (next0 == ~0))
            {
-
-             // Remove ourself from the feature bitmap
-             feature_bitmap = vnet_buffer (b0)->l2.feature_bitmap
-               & ~L2INPUT_FEAT_CLASSIFY;
-
-             // save for next feature graph nodes
-             vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap;
-
              // Determine the next node
              next0 =
                feat_bitmap_get_next_node_index (cm->feat_next_node_index,
@@ -353,7 +422,7 @@ l2_classify_node_fn (vlib_main_t * vm,
          if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
                             && (b0->flags & VLIB_BUFFER_IS_TRACED)))
            {
-             l2_classify_trace_t *t =
+             l2_input_classify_trace_t *t =
                vlib_add_trace (vm, node, b0, sizeof (*t));
              t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
              t->table_index = table_index0;
@@ -371,47 +440,51 @@ l2_classify_node_fn (vlib_main_t * vm,
     }
 
   vlib_node_increment_counter (vm, node->node_index,
-                              L2_CLASSIFY_ERROR_MISS, misses);
+                              L2_INPUT_CLASSIFY_ERROR_MISS, misses);
   vlib_node_increment_counter (vm, node->node_index,
-                              L2_CLASSIFY_ERROR_HIT, hits);
+                              L2_INPUT_CLASSIFY_ERROR_HIT, hits);
   vlib_node_increment_counter (vm, node->node_index,
-                              L2_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
+                              L2_INPUT_CLASSIFY_ERROR_CHAIN_HIT, chain_hits);
   return frame->n_vectors;
 }
 
 /* *INDENT-OFF* */
-VLIB_REGISTER_NODE (l2_classify_node) = {
-  .function = l2_classify_node_fn,
-  .name = "l2-classify",
+VLIB_REGISTER_NODE (l2_input_classify_node) = {
+  .function = l2_input_classify_node_fn,
+  .name = "l2-input-classify",
   .vector_size = sizeof (u32),
-  .format_trace = format_l2_classify_trace,
+  .format_trace = format_l2_input_classify_trace,
   .type = VLIB_NODE_TYPE_INTERNAL,
 
-  .n_errors = ARRAY_LEN(l2_classify_error_strings),
-  .error_strings = l2_classify_error_strings,
+  .n_errors = ARRAY_LEN(l2_input_classify_error_strings),
+  .error_strings = l2_input_classify_error_strings,
 
-  .runtime_data_bytes = sizeof (l2_classify_runtime_t),
+  .runtime_data_bytes = sizeof (l2_input_classify_runtime_t),
 
-  .n_next_nodes = L2_CLASSIFY_N_NEXT,
+  .n_next_nodes = L2_INPUT_CLASSIFY_N_NEXT,
 
   /* edit / add dispositions here */
   .next_nodes = {
-    [L2_CLASSIFY_NEXT_DROP]  = "error-drop",
-    [L2_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
-    [L2_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
-    [L2_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
-    [L2_CLASSIFY_NEXT_LI] = "li-hit",
+    [L2_INPUT_CLASSIFY_NEXT_DROP]  = "error-drop",
+    [L2_INPUT_CLASSIFY_NEXT_ETHERNET_INPUT] = "ethernet-input-not-l2",
+    [L2_INPUT_CLASSIFY_NEXT_IP4_INPUT] = "ip4-input",
+    [L2_INPUT_CLASSIFY_NEXT_IP6_INPUT] = "ip6-input",
+    [L2_INPUT_CLASSIFY_NEXT_LI] = "li-hit",
   },
 };
 /* *INDENT-ON* */
 
-VLIB_NODE_FUNCTION_MULTIARCH (l2_classify_node, l2_classify_node_fn)
-     clib_error_t *l2_classify_init (vlib_main_t * vm)
+VLIB_NODE_FUNCTION_MULTIARCH (l2_input_classify_node,
+                             l2_input_classify_node_fn);
+
+/** l2 input classsifier feature initialization */
+clib_error_t *
+l2_input_classify_init (vlib_main_t * vm)
 {
-  l2_classify_main_t *cm = &l2_classify_main;
-  l2_classify_runtime_t *rt;
+  l2_input_classify_main_t *cm = &l2_input_classify_main;
+  l2_input_classify_runtime_t *rt;
 
-  rt = vlib_node_get_runtime_data (vm, l2_classify_node.index);
+  rt = vlib_node_get_runtime_data (vm, l2_input_classify_node.index);
 
   cm->vlib_main = vm;
   cm->vnet_main = vnet_get_main ();
@@ -419,7 +492,7 @@ VLIB_NODE_FUNCTION_MULTIARCH (l2_classify_node, l2_classify_node_fn)
 
   /* Initialize the feature next-node indexes */
   feat_bitmap_init_next_nodes (vm,
-                              l2_classify_node.index,
+                              l2_input_classify_node.index,
                               L2INPUT_N_FEAT,
                               l2input_get_feat_names (),
                               cm->feat_next_node_index);
@@ -429,27 +502,34 @@ VLIB_NODE_FUNCTION_MULTIARCH (l2_classify_node, l2_classify_node_fn)
   return 0;
 }
 
-VLIB_INIT_FUNCTION (l2_classify_init);
+VLIB_INIT_FUNCTION (l2_input_classify_init);
 
 
+/** enable/disable l2 input classification on a specific interface */
 void
-vnet_l2_classify_enable_disable (u32 sw_if_index, int enable_disable)
+vnet_l2_input_classify_enable_disable (u32 sw_if_index, int enable_disable)
 {
-  vlib_main_t *vm = vlib_get_main ();
-  vnet_main_t *vnm = vnet_get_main ();
-
-  if (enable_disable)
-    set_int_l2_mode (vm, vnm, MODE_L2_CLASSIFY, sw_if_index, 0, 0, 0, 0);
-  else
-    set_int_l2_mode (vm, vnm, MODE_L3, sw_if_index, 0, 0, 0, 0);
+  l2input_intf_bitmap_enable (sw_if_index, L2INPUT_FEAT_INPUT_CLASSIFY,
+                             (u32) enable_disable);
 }
 
+/** @brief Set l2 per-protocol, per-interface input classification tables
+ *
+ *  @param sw_if_index  interface handle
+ *  @param ip4_table_index  ip4 classification table index, or ~0
+ *  @param ip6_table_index  ip6 classification table index, or ~0
+ *  @param other_table_index  non-ip4, non-ip6 classification table index,
+ *         or ~0
+ *  @returns 0 on success, VNET_API_ERROR_NO_SUCH_TABLE, TABLE2, TABLE3
+ *  if the indicated (non-~0) table does not exist.
+ */
+
 int
-vnet_l2_classify_set_tables (u32 sw_if_index,
-                            u32 ip4_table_index,
-                            u32 ip6_table_index, u32 other_table_index)
+vnet_l2_input_classify_set_tables (u32 sw_if_index,
+                                  u32 ip4_table_index,
+                                  u32 ip6_table_index, u32 other_table_index)
 {
-  l2_classify_main_t *cm = &l2_classify_main;
+  l2_input_classify_main_t *cm = &l2_input_classify_main;
   vnet_classify_main_t *vcm = cm->vnet_classify_main;
 
   /* Assume that we've validated sw_if_index in the API layer */
@@ -467,33 +547,33 @@ vnet_l2_classify_set_tables (u32 sw_if_index,
     return VNET_API_ERROR_NO_SUCH_TABLE3;
 
   vec_validate
-    (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4],
+    (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4],
      sw_if_index);
 
   vec_validate
-    (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6],
+    (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6],
      sw_if_index);
 
   vec_validate
-    (cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER],
+    (cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER],
      sw_if_index);
 
-  cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
+  cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4]
     [sw_if_index] = ip4_table_index;
 
-  cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
+  cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6]
     [sw_if_index] = ip6_table_index;
 
-  cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
+  cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER]
     [sw_if_index] = other_table_index;
 
   return 0;
 }
 
 static clib_error_t *
-int_l2_classify_command_fn (vlib_main_t * vm,
-                           unformat_input_t * input,
-                           vlib_cli_command_t * cmd)
+int_l2_input_classify_command_fn (vlib_main_t * vm,
+                                 unformat_input_t * input,
+                                 vlib_cli_command_t * cmd)
 {
   vnet_main_t *vnm = vnet_get_main ();
   u32 sw_if_index = ~0;
@@ -525,38 +605,48 @@ int_l2_classify_command_fn (vlib_main_t * vm,
       && other_table_index == ~0)
     {
       vlib_cli_output (vm, "L2 classification disabled");
-      vnet_l2_classify_enable_disable (sw_if_index, 0 /* enable */ );
+      vnet_l2_input_classify_enable_disable (sw_if_index, 0 /* enable */ );
       return 0;
     }
 
-  rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
-                                   ip6_table_index, other_table_index);
+  rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
+                                         ip6_table_index, other_table_index);
   switch (rv)
     {
     case 0:
-      vnet_l2_classify_enable_disable (sw_if_index, 1 /* enable */ );
+      vnet_l2_input_classify_enable_disable (sw_if_index, 1 /* enable */ );
       break;
 
     default:
-      return clib_error_return (0, "vnet_l2_classify_set_tables: %d", rv);
+      return clib_error_return (0, "vnet_l2_input_classify_set_tables: %d",
+                               rv);
       break;
     }
 
   return 0;
 }
 
+/*?
+ * Configure l2 input classification.
+ *
+ * @cliexpar
+ * @cliexstart {set interface l2 input classify intfc <interface-name>
+ *            [ip4-table <index>]
+ *            [ip6-table <index>]
+ *            [other-table <index>]}
+ * @cliexend
+ ?*/
+
 /* *INDENT-OFF* */
-VLIB_CLI_COMMAND (int_l2_classify_cli, static) = {
-  .path = "set interface l2 classify",
+VLIB_CLI_COMMAND (int_l2_input_classify_cli, static) = {
+  .path = "set interface l2 input classify",
   .short_help =
-  "set interface l2 classify intfc <int> [ip4-table <n>]\n"
+  "set interface l2 input classify intfc <int> [ip4-table <n>]\n"
   "  [ip6-table <n>] [other-table <n>]",
-  .function = int_l2_classify_command_fn,
+  .function = int_l2_input_classify_command_fn,
 };
 /* *INDENT-ON* */
 
-
-
 /*
  * fd.io coding-style-patch-verification: ON
  *
index e29d32d..0aabbc0 100644 (file)
@@ -98,6 +98,7 @@ l2output_main_t l2output_main;
  _(IPIW,              "feature-bitmap-drop")        \
  _(STP_BLOCKED,       "feature-bitmap-drop")        \
  _(LINESTATUS_DOWN,   "feature-bitmap-drop")        \
+ _(OUTPUT_CLASSIFY,   "l2-output-classify")        \
  _(XCRW,             "l2-xcrw")
 
 /* Feature bitmap positions */
diff --git a/vnet/vnet/l2/l2_output_classify.c b/vnet/vnet/l2/l2_output_classify.c
new file mode 100644 (file)
index 0000000..1cb8b85
--- /dev/null
@@ -0,0 +1,661 @@
+/*
+ * Copyright (c) 2015 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * l2_classify.c
+ */
+
+#include <vnet/l2/l2_classify.h>
+#include <vnet/api_errno.h>
+
+/**
+ * @file
+ * @brief L2 input classifier
+ *
+ * See also .../vnet/vnet/classify/vnet_classify.[ch]
+ */
+
+typedef struct
+{
+  /** interface handle for the ith packet */
+  u32 sw_if_index;
+  /** graph arc index selected for this packet */
+  u32 next_index;
+  /** classifier table which provided the final result */
+  u32 table_index;
+  /** offset in classifier heap of the corresponding session */
+  u32 session_offset;
+} l2_output_classify_trace_t;
+
+typedef struct
+{
+  /** use-case independent main object pointer */
+  vnet_classify_main_t *vcm;
+  /** l2 input classifier main object pointer */
+  l2_output_classify_main_t *l2cm;
+} l2_output_classify_runtime_t;
+
+/** packet trace format function */
+static u8 *
+format_l2_output_classify_trace (u8 * s, va_list * args)
+{
+  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
+  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
+  l2_output_classify_trace_t *t =
+    va_arg (*args, l2_output_classify_trace_t *);
+
+  s = format (s, "l2-classify: sw_if_index %d, table %d, offset %x, next %d",
+             t->sw_if_index, t->table_index, t->session_offset,
+             t->next_index);
+  return s;
+}
+
+/** l2 output classifier main data structure */
+l2_output_classify_main_t l2_output_classify_main;
+
+vlib_node_registration_t l2_output_classify_node;
+
+#define foreach_l2_output_classify_error               \
+_(MISS, "Classify misses")                      \
+_(HIT, "Classify hits")                         \
+_(CHAIN_HIT, "Classify hits after chain walk")  \
+_(DROP, "L2 Classify Drops")
+
+typedef enum
+{
+#define _(sym,str) L2_OUTPUT_CLASSIFY_ERROR_##sym,
+  foreach_l2_output_classify_error
+#undef _
+    L2_OUTPUT_CLASSIFY_N_ERROR,
+} l2_output_classify_error_t;
+
+static char *l2_output_classify_error_strings[] = {
+#define _(sym,string) string,
+  foreach_l2_output_classify_error
+#undef _
+};
+
+/**
+ * @brief l2 output classifier node.
+ * @node l2-output-classify
+ *
+ * This is the l2 output classifier dispatch node
+ *
+ * @param vm    vlib_main_t corresponding to the current thread.
+ * @param node  vlib_node_runtime_t data for this node.
+ * @param frame vlib_frame_t whose contents should be dispatched.
+ *
+ * @par Graph mechanics: buffer metadata, next index usage
+ *
+ * @em Uses:
+ * - <code>(l2_input_classify_runtime_t *)
+ *         rt->classify_table_index_by_sw_if_index</code>
+ *        Head of the per-interface, perprotocol classifier table chain
+ *        for a specific interface. ~0 => send pkts to the next
+ *        feature in the L2 feature chain.
+ * - <code>vnet_buffer(b)->sw_if_index[VLIB_RX]</code>
+ *     - Indicates the @c sw_if_index value of the interface that the
+ *     packet was received on.
+ * - <code>vnet_buffer (b0)->l2.feature_bitmap</code>
+ *     - Used to steer packets across l2 features enabled on the interface
+ * - <code>(vnet_classify_entry_t) e0->next_index</code>
+ *     - Used to steer traffic when the classifier hits on a session
+ * - <code>(vnet_classify_entry_t) e0->advance</code>
+ *     - Signed quantity applied via <code>vlib_buffer_advance</code>
+ *     when the classifier hits on a session
+ * - <code>(vnet_classify_table_t) t0->miss_next_index</code>
+ *     - Used to steer traffic when the classifier misses
+ *
+ * @em Sets:
+ * - <code>vnet_buffer (b0)->l2_classify.table_index</code>
+ *     - Classifier table index of the first classifier table in
+ *     the classifier table chain
+ * - <code>vnet_buffer (b0)->l2_classify.hash</code>
+ *     - Bounded-index extensible hash corresponding to the
+ *     masked fields in the current packet
+ * - <code>vnet_buffer (b0)->l2.feature_bitmap</code>
+ *     - Used to steer packets across l2 features enabled on the interface
+ * - <code>vnet_buffer (b0)->l2_classify.opaque_index</code>
+ *     - Copied from the classifier session object upon classifier hit
+ *
+ * @em Counters:
+ * - <code>L2_OUTPUT_CLASSIFY_ERROR_MISS</code> Classifier misses
+ * - <code>L2_OUTPUT_CLASSIFY_ERROR_HIT</code> Classifier hits
+ * - <code>L2_OUTPUT_CLASSIFY_ERROR_CHAIN_HIT</code>
+ *   Classifier hits in other than the first table
+ */
+
+static uword
+l2_output_classify_node_fn (vlib_main_t * vm,
+                           vlib_node_runtime_t * node, vlib_frame_t * frame)
+{
+  u32 n_left_from, *from, *to_next;
+  l2_output_classify_next_t next_index;
+  l2_output_classify_main_t *cm = &l2_output_classify_main;
+  vnet_classify_main_t *vcm = cm->vnet_classify_main;
+  l2_output_classify_runtime_t *rt =
+    (l2_output_classify_runtime_t *) node->runtime_data;
+  u32 feature_bitmap0;
+  u32 hits = 0;
+  u32 misses = 0;
+  u32 chain_hits = 0;
+  f64 now;
+  u32 n_next_nodes;
+  u32 cached_sw_if_index = (u32) ~ 0;
+  u32 cached_next_index = (u32) ~ 0;
+  u32 sw_if_index0;
+
+  n_next_nodes = node->n_next_nodes;
+
+  now = vlib_time_now (vm);
+
+  n_left_from = frame->n_vectors;
+  from = vlib_frame_vector_args (frame);
+
+  /* First pass: compute hash */
+
+  while (n_left_from > 2)
+    {
+      vlib_buffer_t *b0, *b1;
+      u32 bi0, bi1;
+      ethernet_header_t *h0, *h1;
+      u32 sw_if_index0, sw_if_index1;
+      u16 type0, type1;
+      int type_index0, type_index1;
+      vnet_classify_table_t *t0, *t1;
+      u32 table_index0, table_index1;
+      u64 hash0, hash1;
+
+
+      /* prefetch next iteration */
+      {
+       vlib_buffer_t *p1, *p2;
+
+       p1 = vlib_get_buffer (vm, from[1]);
+       p2 = vlib_get_buffer (vm, from[2]);
+
+       vlib_prefetch_buffer_header (p1, STORE);
+       CLIB_PREFETCH (p1->data, CLIB_CACHE_LINE_BYTES, STORE);
+       vlib_prefetch_buffer_header (p2, STORE);
+       CLIB_PREFETCH (p2->data, CLIB_CACHE_LINE_BYTES, STORE);
+      }
+
+      bi0 = from[0];
+      b0 = vlib_get_buffer (vm, bi0);
+      h0 = vlib_buffer_get_current (b0);
+
+      bi1 = from[1];
+      b1 = vlib_get_buffer (vm, bi1);
+      h1 = vlib_buffer_get_current (b1);
+
+      sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
+      vnet_buffer (b0)->l2_classify.table_index = ~0;
+
+      sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];
+      vnet_buffer (b1)->l2_classify.table_index = ~0;
+
+      /* Select classifier table based on ethertype */
+      type0 = clib_net_to_host_u16 (h0->type);
+      type1 = clib_net_to_host_u16 (h1->type);
+
+      type_index0 = (type0 == ETHERNET_TYPE_IP4)
+       ? L2_OUTPUT_CLASSIFY_TABLE_IP4 : L2_OUTPUT_CLASSIFY_TABLE_OTHER;
+      type_index0 = (type0 == ETHERNET_TYPE_IP6)
+       ? L2_OUTPUT_CLASSIFY_TABLE_IP6 : type_index0;
+
+      type_index1 = (type1 == ETHERNET_TYPE_IP4)
+       ? L2_OUTPUT_CLASSIFY_TABLE_IP4 : L2_OUTPUT_CLASSIFY_TABLE_OTHER;
+      type_index1 = (type1 == ETHERNET_TYPE_IP6)
+       ? L2_OUTPUT_CLASSIFY_TABLE_IP6 : type_index1;
+
+      vnet_buffer (b0)->l2_classify.table_index =
+       table_index0 =
+       rt->l2cm->classify_table_index_by_sw_if_index
+       [type_index0][sw_if_index0];
+
+      if (table_index0 != ~0)
+       {
+         t0 = pool_elt_at_index (vcm->tables, table_index0);
+
+         vnet_buffer (b0)->l2_classify.hash = hash0 =
+           vnet_classify_hash_packet (t0, (u8 *) h0);
+         vnet_classify_prefetch_bucket (t0, hash0);
+       }
+
+      vnet_buffer (b1)->l2_classify.table_index =
+       table_index1 =
+       rt->l2cm->classify_table_index_by_sw_if_index
+       [type_index1][sw_if_index1];
+
+      if (table_index1 != ~0)
+       {
+         t1 = pool_elt_at_index (vcm->tables, table_index1);
+
+         vnet_buffer (b1)->l2_classify.hash = hash1 =
+           vnet_classify_hash_packet (t1, (u8 *) h1);
+         vnet_classify_prefetch_bucket (t1, hash1);
+       }
+
+      from += 2;
+      n_left_from -= 2;
+    }
+
+  while (n_left_from > 0)
+    {
+      vlib_buffer_t *b0;
+      u32 bi0;
+      ethernet_header_t *h0;
+      u16 type0;
+      u32 type_index0;
+      vnet_classify_table_t *t0;
+      u32 table_index0;
+      u64 hash0;
+
+      bi0 = from[0];
+      b0 = vlib_get_buffer (vm, bi0);
+      h0 = vlib_buffer_get_current (b0);
+
+      sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
+      vnet_buffer (b0)->l2_classify.table_index = ~0;
+
+      /* Select classifier table based on ethertype */
+      type0 = clib_net_to_host_u16 (h0->type);
+
+      type_index0 = (type0 == ETHERNET_TYPE_IP4)
+       ? L2_OUTPUT_CLASSIFY_TABLE_IP4 : L2_OUTPUT_CLASSIFY_TABLE_OTHER;
+      type_index0 = (type0 == ETHERNET_TYPE_IP6)
+       ? L2_OUTPUT_CLASSIFY_TABLE_IP6 : type_index0;
+
+      vnet_buffer (b0)->l2_classify.table_index =
+       table_index0 = rt->l2cm->classify_table_index_by_sw_if_index
+       [type_index0][sw_if_index0];
+
+      if (table_index0 != ~0)
+       {
+         t0 = pool_elt_at_index (vcm->tables, table_index0);
+
+         vnet_buffer (b0)->l2_classify.hash = hash0 =
+           vnet_classify_hash_packet (t0, (u8 *) h0);
+         vnet_classify_prefetch_bucket (t0, hash0);
+       }
+      from++;
+      n_left_from--;
+    }
+
+  next_index = node->cached_next_index;
+  from = vlib_frame_vector_args (frame);
+  n_left_from = frame->n_vectors;
+
+  while (n_left_from > 0)
+    {
+      u32 n_left_to_next;
+
+      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
+
+      /* Not enough load/store slots to dual loop... */
+      while (n_left_from > 0 && n_left_to_next > 0)
+       {
+         u32 bi0;
+         vlib_buffer_t *b0;
+         u32 next0 = ~0;
+         ethernet_header_t *h0;
+         u32 table_index0;
+         u64 hash0;
+         vnet_classify_table_t *t0;
+         vnet_classify_entry_t *e0;
+
+         if (PREDICT_TRUE (n_left_from > 2))
+           {
+             vlib_buffer_t *p2 = vlib_get_buffer (vm, from[2]);
+             u64 phash2;
+             u32 table_index2;
+             vnet_classify_table_t *tp2;
+
+             /*
+              * Prefetch table entry two ahead. Buffer / data
+              * were prefetched above...
+              */
+             table_index2 = vnet_buffer (p2)->l2_classify.table_index;
+
+             if (PREDICT_TRUE (table_index2 != ~0))
+               {
+                 tp2 = pool_elt_at_index (vcm->tables, table_index2);
+                 phash2 = vnet_buffer (p2)->l2_classify.hash;
+                 vnet_classify_prefetch_entry (tp2, phash2);
+               }
+           }
+
+         /* speculatively enqueue b0 to the current next frame */
+         bi0 = from[0];
+         to_next[0] = bi0;
+         from += 1;
+         to_next += 1;
+         n_left_from -= 1;
+         n_left_to_next -= 1;
+
+         b0 = vlib_get_buffer (vm, bi0);
+         h0 = vlib_buffer_get_current (b0);
+         table_index0 = vnet_buffer (b0)->l2_classify.table_index;
+         e0 = 0;
+         vnet_buffer (b0)->l2_classify.opaque_index = ~0;
+         /* Remove ourself from the feature bitmap */
+         feature_bitmap0 = vnet_buffer (b0)->l2.feature_bitmap
+           & ~L2OUTPUT_FEAT_OUTPUT_CLASSIFY;
+
+         /* save for next feature graph nodes */
+         vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;
+
+         if (PREDICT_TRUE (table_index0 != ~0))
+           {
+             hash0 = vnet_buffer (b0)->l2_classify.hash;
+             t0 = pool_elt_at_index (vcm->tables, table_index0);
+
+             e0 = vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
+             if (e0)
+               {
+                 vnet_buffer (b0)->l2_classify.opaque_index
+                   = e0->opaque_index;
+                 vlib_buffer_advance (b0, e0->advance);
+                 next0 = (e0->next_index < n_next_nodes) ?
+                   e0->next_index : next0;
+                 hits++;
+               }
+             else
+               {
+                 while (1)
+                   {
+                     if (t0->next_table_index != ~0)
+                       t0 = pool_elt_at_index (vcm->tables,
+                                               t0->next_table_index);
+                     else
+                       {
+                         next0 = (t0->miss_next_index < n_next_nodes) ?
+                           t0->miss_next_index : next0;
+                         misses++;
+                         break;
+                       }
+
+                     hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
+                     e0 =
+                       vnet_classify_find_entry (t0, (u8 *) h0, hash0, now);
+                     if (e0)
+                       {
+                         vnet_buffer (b0)->l2_classify.opaque_index
+                           = e0->opaque_index;
+                         vlib_buffer_advance (b0, e0->advance);
+                         next0 = (e0->next_index < n_next_nodes) ?
+                           e0->next_index : next0;
+                         hits++;
+                         chain_hits++;
+                         break;
+                       }
+                   }
+               }
+           }
+
+         if (PREDICT_FALSE (next0 == 0))
+           b0->error = node->errors[L2_OUTPUT_CLASSIFY_ERROR_DROP];
+
+         if (PREDICT_FALSE (next0 == ~0))
+           {
+             sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_TX];
+
+             /* Determine next node */
+             l2_output_dispatch (cm->vlib_main,
+                                 cm->vnet_main,
+                                 node,
+                                 l2_output_classify_node.index,
+                                 &cached_sw_if_index,
+                                 &cached_next_index,
+                                 &cm->next_nodes,
+                                 b0, sw_if_index0, feature_bitmap0, &next0);
+           }
+
+         if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
+                            && (b0->flags & VLIB_BUFFER_IS_TRACED)))
+           {
+             l2_output_classify_trace_t *t =
+               vlib_add_trace (vm, node, b0, sizeof (*t));
+             t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_RX];
+             t->table_index = table_index0;
+             t->next_index = next0;
+             t->session_offset = e0 ? vnet_classify_get_offset (t0, e0) : 0;
+           }
+
+         /* verify speculative enqueue, maybe switch current next frame */
+         vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
+                                          to_next, n_left_to_next,
+                                          bi0, next0);
+       }
+
+      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
+    }
+
+  vlib_node_increment_counter (vm, node->node_index,
+                              L2_OUTPUT_CLASSIFY_ERROR_MISS, misses);
+  vlib_node_increment_counter (vm, node->node_index,
+                              L2_OUTPUT_CLASSIFY_ERROR_HIT, hits);
+  vlib_node_increment_counter (vm, node->node_index,
+                              L2_OUTPUT_CLASSIFY_ERROR_CHAIN_HIT,
+                              chain_hits);
+  return frame->n_vectors;
+}
+
+/* *INDENT-OFF* */
+VLIB_REGISTER_NODE (l2_output_classify_node) = {
+  .function = l2_output_classify_node_fn,
+  .name = "l2-output-classify",
+  .vector_size = sizeof (u32),
+  .format_trace = format_l2_output_classify_trace,
+  .type = VLIB_NODE_TYPE_INTERNAL,
+
+  .n_errors = ARRAY_LEN(l2_output_classify_error_strings),
+  .error_strings = l2_output_classify_error_strings,
+
+  .runtime_data_bytes = sizeof (l2_output_classify_runtime_t),
+
+  .n_next_nodes = L2_OUTPUT_CLASSIFY_N_NEXT,
+
+  /* edit / add dispositions here */
+  .next_nodes = {
+    [L2_OUTPUT_CLASSIFY_NEXT_DROP]  = "error-drop",
+  },
+};
+/* *INDENT-ON* */
+
+VLIB_NODE_FUNCTION_MULTIARCH (l2_output_classify_node,
+                             l2_output_classify_node_fn);
+
+/** l2 output classsifier feature initialization */
+clib_error_t *
+l2_output_classify_init (vlib_main_t * vm)
+{
+  l2_output_classify_main_t *cm = &l2_output_classify_main;
+  l2_output_classify_runtime_t *rt;
+
+  rt = vlib_node_get_runtime_data (vm, l2_output_classify_node.index);
+
+  cm->vlib_main = vm;
+  cm->vnet_main = vnet_get_main ();
+  cm->vnet_classify_main = &vnet_classify_main;
+
+  /* Initialize the feature next-node indexes */
+  feat_bitmap_init_next_nodes (vm,
+                              l2_output_classify_node.index,
+                              L2OUTPUT_N_FEAT,
+                              l2output_get_feat_names (),
+                              cm->feat_next_node_index);
+  rt->l2cm = cm;
+  rt->vcm = cm->vnet_classify_main;
+
+  /* Initialize the output node mapping table */
+  l2output_init_output_node_vec (&cm->next_nodes.output_node_index_vec);
+
+  return 0;
+}
+
+VLIB_INIT_FUNCTION (l2_output_classify_init);
+
+/** enable/disable l2 input classification on a specific interface */
+void
+vnet_l2_output_classify_enable_disable (u32 sw_if_index, int enable_disable)
+{
+
+  l2output_intf_bitmap_enable (sw_if_index, L2OUTPUT_FEAT_OUTPUT_CLASSIFY,
+                              (u32) enable_disable);
+}
+
+/** @brief Set l2 per-protocol, per-interface output classification tables
+ *
+ *  @param sw_if_index  interface handle
+ *  @param ip4_table_index  ip4 classification table index, or ~0
+ *  @param ip6_table_index  ip6 classification table index, or ~0
+ *  @param other_table_index  non-ip4, non-ip6 classification table index,
+ *         or ~0
+ *  @returns 0 on success, VNET_API_ERROR_NO_SUCH_TABLE, TABLE2, TABLE3
+ *  if the indicated (non-~0) table does not exist.
+ */
+
+int
+vnet_l2_output_classify_set_tables (u32 sw_if_index,
+                                   u32 ip4_table_index,
+                                   u32 ip6_table_index,
+                                   u32 other_table_index)
+{
+  l2_output_classify_main_t *cm = &l2_output_classify_main;
+  vnet_classify_main_t *vcm = cm->vnet_classify_main;
+
+  /* Assume that we've validated sw_if_index in the API layer */
+
+  if (ip4_table_index != ~0 &&
+      pool_is_free_index (vcm->tables, ip4_table_index))
+    return VNET_API_ERROR_NO_SUCH_TABLE;
+
+  if (ip6_table_index != ~0 &&
+      pool_is_free_index (vcm->tables, ip6_table_index))
+    return VNET_API_ERROR_NO_SUCH_TABLE2;
+
+  if (other_table_index != ~0 &&
+      pool_is_free_index (vcm->tables, other_table_index))
+    return VNET_API_ERROR_NO_SUCH_TABLE3;
+
+  vec_validate
+    (cm->classify_table_index_by_sw_if_index[L2_OUTPUT_CLASSIFY_TABLE_IP4],
+     sw_if_index);
+
+  vec_validate
+    (cm->classify_table_index_by_sw_if_index[L2_OUTPUT_CLASSIFY_TABLE_IP6],
+     sw_if_index);
+
+  vec_validate
+    (cm->classify_table_index_by_sw_if_index[L2_OUTPUT_CLASSIFY_TABLE_OTHER],
+     sw_if_index);
+
+  cm->classify_table_index_by_sw_if_index[L2_OUTPUT_CLASSIFY_TABLE_IP4]
+    [sw_if_index] = ip4_table_index;
+
+  cm->classify_table_index_by_sw_if_index[L2_OUTPUT_CLASSIFY_TABLE_IP6]
+    [sw_if_index] = ip6_table_index;
+
+  cm->classify_table_index_by_sw_if_index[L2_OUTPUT_CLASSIFY_TABLE_OTHER]
+    [sw_if_index] = other_table_index;
+
+  return 0;
+}
+
+static clib_error_t *
+int_l2_output_classify_command_fn (vlib_main_t * vm,
+                                  unformat_input_t * input,
+                                  vlib_cli_command_t * cmd)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+  u32 sw_if_index = ~0;
+  u32 ip4_table_index = ~0;
+  u32 ip6_table_index = ~0;
+  u32 other_table_index = ~0;
+  int rv;
+
+  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (input, "intfc %U", unformat_vnet_sw_interface,
+                   vnm, &sw_if_index))
+       ;
+      else if (unformat (input, "ip4-table %d", &ip4_table_index))
+       ;
+      else if (unformat (input, "ip6-table %d", &ip6_table_index))
+       ;
+      else if (unformat (input, "other-table %d", &other_table_index))
+       ;
+      else
+       break;
+    }
+
+  if (sw_if_index == ~0)
+    return clib_error_return (0, "interface must be specified");
+
+
+  if (ip4_table_index == ~0 && ip6_table_index == ~0
+      && other_table_index == ~0)
+    {
+      vlib_cli_output (vm, "L2 classification disabled");
+      vnet_l2_output_classify_enable_disable (sw_if_index, 0 /* enable */ );
+      return 0;
+    }
+
+  rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
+                                          ip6_table_index,
+                                          other_table_index);
+  switch (rv)
+    {
+    case 0:
+      vnet_l2_output_classify_enable_disable (sw_if_index, 1 /* enable */ );
+      break;
+
+    default:
+      return clib_error_return (0, "vnet_l2_output_classify_set_tables: %d",
+                               rv);
+      break;
+    }
+
+  return 0;
+}
+
+/*?
+ * Configure l2 input classification.
+ *
+ * @cliexpar
+ * @cliexstart {set interface l2 output classify intfc <interface-name>
+ *            [ip4-table <index>]
+ *            [ip6-table <index>]
+ *            [other-table <index>]}
+ * @cliexend
+ ?*/
+
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (int_l2_output_classify_cli, static) = {
+  .path = "set interface l2 output classify",
+  .short_help =
+  "set interface l2 output classify intfc <int> [ip4-table <n>]\n"
+  "  [ip6-table <n>] [other-table <n>]",
+  .function = int_l2_output_classify_command_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
index 0926dfb..a87828f 100644 (file)
@@ -7855,7 +7855,7 @@ unformat_l2_next_index (unformat_input_t * input, va_list * args)
   u32 tmp;
 
 #define _(n,N) \
-  if (unformat (input, #n)) { next_index = L2_CLASSIFY_NEXT_##N; goto out;}
+  if (unformat (input, #n)) { next_index = L2_INPUT_CLASSIFY_NEXT_##N; goto out;}
   foreach_l2_next;
 #undef _
 
@@ -8570,6 +8570,7 @@ api_classify_set_interface_l2_tables (vat_main_t * vam)
   u32 ip4_table_index = ~0;
   u32 ip6_table_index = ~0;
   u32 other_table_index = ~0;
+  u32 is_input = 1;
 
   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
     {
@@ -8583,6 +8584,8 @@ api_classify_set_interface_l2_tables (vat_main_t * vam)
        ;
       else if (unformat (i, "other-table %d", &other_table_index))
        ;
+      else if (unformat (i, "is-input %d", &is_input))
+       ;
       else
        {
          clib_warning ("parse error '%U'", format_unformat_error, i);
@@ -8603,7 +8606,7 @@ api_classify_set_interface_l2_tables (vat_main_t * vam)
   mp->ip4_table_index = ntohl (ip4_table_index);
   mp->ip6_table_index = ntohl (ip6_table_index);
   mp->other_table_index = ntohl (other_table_index);
-
+  mp->is_input = (u8) is_input;
 
   S;
   W;
index 05d681f..5569c67 100644 (file)
@@ -49,7 +49,7 @@ typedef struct
   vlib_main_t *vlib_main;
   vnet_main_t *vnet_main;
   vnet_classify_main_t *vnet_classify_main;
-  l2_classify_main_t *l2_classify_main;
+  l2_input_classify_main_t *l2_input_classify_main;
 }
 sticky_hash_main_t;
 
@@ -197,11 +197,11 @@ sticky_hash_miss_node_fn (vlib_main_t * vm,
 
          fe0 =
            vnet_classify_find_or_add_entry_3 (ft0, mp->fdata, &was_found0);
-         fe0->next_index = L2_CLASSIFY_NEXT_IP4_INPUT;
+         fe0->next_index = L2_INPUT_CLASSIFY_NEXT_IP4_INPUT;
          fe0->advance = sizeof (ethernet_header_t);
 
          re0 = vnet_classify_find_or_add_entry_3 (rt0, mp->rdata, 0);
-         re0->next_index = L2_CLASSIFY_NEXT_IP4_INPUT; /* $$$ FIXME */
+         re0->next_index = L2_INPUT_CLASSIFY_NEXT_IP4_INPUT;   /* $$$ FIXME */
          re0->advance = sizeof (ethernet_header_t);
 
          /* Note: we could get a whole vector of misses for the same sess */
@@ -271,7 +271,7 @@ sticky_hash_miss_init (vlib_main_t * vm)
   mp->vlib_main = vm;
   mp->vnet_main = vnet_get_main ();
   mp->vnet_classify_main = &vnet_classify_main;
-  mp->l2_classify_main = &l2_classify_main;
+  mp->l2_input_classify_main = &l2_input_classify_main;
 
   return 0;
 }
@@ -287,7 +287,7 @@ static int ip4_sticky_hash_enable_disable
   u32 fib_index;
   ip4_fib_t *fib;
   vnet_classify_main_t *cm = mp->vnet_classify_main;
-  l2_classify_main_t *l2cm = mp->l2_classify_main;
+  l2_input_classify_main_t *l2cm = mp->l2_input_classify_main;
   vnet_classify_table_3_t *ft, *rt;
 
   fib_index = vec_elt (im->fib_index_by_sw_if_index, fwd_sw_if_index);
@@ -302,7 +302,7 @@ static int ip4_sticky_hash_enable_disable
       fib->fwd_classify_table_index
        = ft - (vnet_classify_table_3_t *) cm->tables;
       mp->fwd_miss_next_index =
-       vlib_node_add_next (mp->vlib_main, l2_classify_node.index,
+       vlib_node_add_next (mp->vlib_main, l2_input_classify_node.index,
                            sticky_hash_miss_node.index);
       ft->miss_next_index = mp->fwd_miss_next_index;
 
@@ -315,51 +315,51 @@ static int ip4_sticky_hash_enable_disable
     }
 
   vec_validate
-    (l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4],
+    (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4],
      fwd_sw_if_index);
 
   vec_validate
-    (l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6],
+    (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6],
      fwd_sw_if_index);
 
   vec_validate
-    (l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER],
+    (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER],
      fwd_sw_if_index);
 
-  l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
+  l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4]
     [fwd_sw_if_index] = fib->fwd_classify_table_index;
 
-  l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
+  l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6]
     [fwd_sw_if_index] = ~0;
 
-  l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
+  l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER]
     [fwd_sw_if_index] = ~0;
 
 
   vec_validate
-    (l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4],
+    (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4],
      rev_sw_if_index);
 
   vec_validate
-    (l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6],
+    (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6],
      rev_sw_if_index);
 
   vec_validate
-    (l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER],
+    (l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER],
      rev_sw_if_index);
 
 
-  l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP4]
+  l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP4]
     [rev_sw_if_index] = fib->rev_classify_table_index;
 
-  l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_IP6]
+  l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_IP6]
     [rev_sw_if_index] = ~0;
 
-  l2cm->classify_table_index_by_sw_if_index[L2_CLASSIFY_TABLE_OTHER]
+  l2cm->classify_table_index_by_sw_if_index[L2_INPUT_CLASSIFY_TABLE_OTHER]
     [rev_sw_if_index] = ~0;
 
-  vnet_l2_classify_enable_disable (fwd_sw_if_index, enable_disable);
-  vnet_l2_classify_enable_disable (rev_sw_if_index, enable_disable);
+  vnet_l2_input_classify_enable_disable (fwd_sw_if_index, enable_disable);
+  vnet_l2_input_classify_enable_disable (rev_sw_if_index, enable_disable);
   return 0;
 }
 
index 0098818..fbebfa6 100644 (file)
@@ -3996,8 +3996,14 @@ static void vl_api_classify_set_interface_l2_tables_t_handler
 
   VALIDATE_SW_IF_INDEX (mp);
 
-  rv = vnet_l2_classify_set_tables (sw_if_index, ip4_table_index,
-                                   ip6_table_index, other_table_index);
+  if (mp->is_input)
+    rv = vnet_l2_input_classify_set_tables (sw_if_index, ip4_table_index,
+                                           ip6_table_index,
+                                           other_table_index);
+  else
+    rv = vnet_l2_output_classify_set_tables (sw_if_index, ip4_table_index,
+                                            ip6_table_index,
+                                            other_table_index);
 
   if (rv == 0)
     {
@@ -4007,7 +4013,10 @@ static void vl_api_classify_set_interface_l2_tables_t_handler
       else
        enable = 0;
 
-      vnet_l2_classify_enable_disable (sw_if_index, enable);
+      if (mp->is_input)
+       vnet_l2_input_classify_enable_disable (sw_if_index, enable);
+      else
+       vnet_l2_output_classify_enable_disable (sw_if_index, enable);
     }
 
   BAD_SW_IF_INDEX_LABEL;
index 913ad1c..566bcb9 100644 (file)
@@ -1274,6 +1274,7 @@ static void *vl_api_classify_set_interface_l2_tables_t_print
   s = format (s, "ip4-table %d ", ntohl (mp->ip4_table_index));
   s = format (s, "ip6-table %d ", ntohl (mp->ip6_table_index));
   s = format (s, "other-table %d ", ntohl (mp->other_table_index));
+  s = format (s, "is-input %d ", mp->is_input);
 
   FINISH;
 }
index 0bfa2fa..1f26d55 100644 (file)
@@ -1731,6 +1731,7 @@ define classify_set_interface_l2_tables
   u32 ip4_table_index;
   u32 ip6_table_index;
   u32 other_table_index;
+  u8 is_input;
 };
 
 /** \brief Set/unset l2 classification tables for an interface response