vlib: introduce trace filter functions
[vpp.git] / src / vnet / classify / vnet_classify.c
old mode 100755 (executable)
new mode 100644 (file)
index 938f583..fb9a59c
@@ -12,6 +12,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 #include <vnet/classify/vnet_classify.h>
 #include <vnet/classify/in_out_acl.h>
 #include <vnet/ip/ip.h>
@@ -19,6 +20,9 @@
 #include <vnet/l2/l2_classify.h>       /* for L2_INPUT_CLASSIFY_NEXT_xxx */
 #include <vnet/fib/fib_table.h>
 #include <vppinfra/lock.h>
+#include <vnet/classify/trace_classify.h>
+
+
 
 /**
  * @file
@@ -124,19 +128,18 @@ vnet_classify_register_unformat_opaque_index_fn (unformat_function_t * fn)
 }
 
 vnet_classify_table_t *
-vnet_classify_new_table (vnet_classify_main_t * cm,
-                        u8 * mask, u32 nbuckets, u32 memory_size,
-                        u32 skip_n_vectors, u32 match_n_vectors)
+vnet_classify_new_table (vnet_classify_main_t *cm, const u8 *mask,
+                        u32 nbuckets, u32 memory_size, u32 skip_n_vectors,
+                        u32 match_n_vectors)
 {
   vnet_classify_table_t *t;
   void *oldheap;
 
   nbuckets = 1 << (max_log2 (nbuckets));
 
-  pool_get_aligned (cm->tables, t, CLIB_CACHE_LINE_BYTES);
-  clib_memset (t, 0, sizeof (*t));
+  pool_get_aligned_zero (cm->tables, t, CLIB_CACHE_LINE_BYTES);
 
-  vec_validate_aligned (t->mask, match_n_vectors - 1, sizeof (u32x4));
+  clib_memset_u32 (t->mask, 0, 4 * ARRAY_LEN (t->mask));
   clib_memcpy_fast (t->mask, mask, match_n_vectors * sizeof (u32x4));
 
   t->next_table_index = ~0;
@@ -145,14 +148,10 @@ vnet_classify_new_table (vnet_classify_main_t * cm,
   t->match_n_vectors = match_n_vectors;
   t->skip_n_vectors = skip_n_vectors;
   t->entries_per_page = 2;
+  t->load_mask = pow2_mask (match_n_vectors * 2);
 
-#if USE_DLMALLOC == 0
-  t->mheap = mheap_alloc (0 /* use VM */ , memory_size);
-#else
-  t->mheap = create_mspace (memory_size, 1 /* locked */ );
-  /* classifier requires the memory to be contiguous, so can not expand. */
-  mspace_disable_expand (t->mheap);
-#endif
+  t->mheap = clib_mem_create_heap (0, memory_size, 1 /* locked */ ,
+                                  "classify");
 
   vec_validate_aligned (t->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
   oldheap = clib_mem_set_heap (t->mheap);
@@ -177,14 +176,8 @@ vnet_classify_delete_table_index (vnet_classify_main_t * cm,
     /* Recursively delete the entire chain */
     vnet_classify_delete_table_index (cm, t->next_table_index, del_chain);
 
-  vec_free (t->mask);
   vec_free (t->buckets);
-#if USE_DLMALLOC == 0
-  mheap_free (t->mheap);
-#else
-  destroy_mspace (t->mheap);
-#endif
-
+  clib_mem_destroy_heap (t->mheap);
   pool_put (cm->tables, t);
 }
 
@@ -300,7 +293,7 @@ split_and_rehash (vnet_classify_table_t * t,
 
   for (i = 0; i < length_in_entries; i++)
     {
-      u64 new_hash;
+      u32 new_hash;
 
       v = vnet_classify_entry_at_index (t, old_values, i);
 
@@ -399,6 +392,7 @@ vnet_classify_entry_claim_resource (vnet_classify_entry_t * e)
       fib_table_lock (e->metadata, FIB_PROTOCOL_IP6, FIB_SOURCE_CLASSIFY);
       break;
     case CLASSIFY_ACTION_SET_METADATA:
+    case CLASSIFY_ACTION_NONE:
       break;
     }
 }
@@ -415,13 +409,14 @@ vnet_classify_entry_release_resource (vnet_classify_entry_t * e)
       fib_table_unlock (e->metadata, FIB_PROTOCOL_IP6, FIB_SOURCE_CLASSIFY);
       break;
     case CLASSIFY_ACTION_SET_METADATA:
+    case CLASSIFY_ACTION_NONE:
       break;
     }
 }
 
-int
-vnet_classify_add_del (vnet_classify_table_t * t,
-                      vnet_classify_entry_t * add_v, int is_add)
+static int
+vnet_classify_add_del (vnet_classify_table_t *t, vnet_classify_entry_t *add_v,
+                      int is_add)
 {
   u32 bucket_index;
   vnet_classify_bucket_t *b, tmp_b;
@@ -429,7 +424,7 @@ vnet_classify_add_del (vnet_classify_table_t * t,
   u32 value_index;
   int rv = 0;
   int i;
-  u64 hash, new_hash;
+  u32 hash, new_hash;
   u32 limit;
   u32 old_log2_pages, new_log2_pages;
   u32 thread_index = vlib_get_thread_index ();
@@ -652,21 +647,21 @@ typedef CLIB_PACKED(struct {
 }) classify_data_or_mask_t;
 /* *INDENT-ON* */
 
-u64
-vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h)
+u32
+vnet_classify_hash_packet (const vnet_classify_table_t *t, u8 *h)
 {
   return vnet_classify_hash_packet_inline (t, h);
 }
 
 vnet_classify_entry_t *
-vnet_classify_find_entry (vnet_classify_table_t * t,
-                         u8 * h, u64 hash, f64 now)
+vnet_classify_find_entry (const vnet_classify_table_t *t, u8 *h, u32 hash,
+                         f64 now)
 {
   return vnet_classify_find_entry_inline (t, h, hash, now);
 }
 
-static u8 *
-format_classify_entry (u8 * s, va_list * args)
+u8 *
+format_classify_entry (u8 *s, va_list *args)
 {
   vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *);
   vnet_classify_entry_t *e = va_arg (*args, vnet_classify_entry_t *);
@@ -749,17 +744,11 @@ format_classify_table (u8 * s, va_list * args)
 }
 
 int
-vnet_classify_add_del_table (vnet_classify_main_t * cm,
-                            u8 * mask,
-                            u32 nbuckets,
-                            u32 memory_size,
-                            u32 skip,
-                            u32 match,
-                            u32 next_table_index,
-                            u32 miss_next_index,
-                            u32 * table_index,
-                            u8 current_data_flag,
-                            i16 current_data_offset,
+vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
+                            u32 nbuckets, u32 memory_size, u32 skip,
+                            u32 match, u32 next_table_index,
+                            u32 miss_next_index, u32 *table_index,
+                            u8 current_data_flag, i16 current_data_offset,
                             int is_add, int del_chain)
 {
   vnet_classify_table_t *t;
@@ -774,6 +763,9 @@ vnet_classify_add_del_table (vnet_classify_main_t * cm,
          if (nbuckets == 0)
            return VNET_API_ERROR_INVALID_VALUE;
 
+         if (match < 1 || match > 5)
+           return VNET_API_ERROR_INVALID_VALUE;
+
          t = vnet_classify_new_table (cm, mask, nbuckets, memory_size,
                                       skip, match);
          t->next_table_index = next_table_index;
@@ -785,8 +777,10 @@ vnet_classify_add_del_table (vnet_classify_main_t * cm,
       else                     /* update */
        {
          vnet_classify_main_t *cm = &vnet_classify_main;
-         t = pool_elt_at_index (cm->tables, *table_index);
+         if (pool_is_free_index (cm->tables, *table_index))
+           return VNET_API_ERROR_CLASSIFY_TABLE_NOT_FOUND;
 
+         t = pool_elt_at_index (cm->tables, *table_index);
          t->next_table_index = next_table_index;
        }
       return 0;
@@ -919,7 +913,7 @@ unformat_l4_mask (unformat_input_t * input, va_list * args)
       else if (unformat (input, "dst_port"))
        dst_port = 0xFFFF;
       else
-       return 0;
+       break;
     }
 
   if (!src_port && !dst_port)
@@ -988,6 +982,7 @@ unformat_ip4_mask (unformat_input_t * input, va_list * args)
        break;
     }
 
+  found_something = version + hdr_length;
 #define _(a) found_something += a;
   foreach_ip4_proto_field;
 #undef _
@@ -1033,7 +1028,7 @@ unformat_ip6_mask (unformat_input_t * input, va_list * args)
 {
   u8 **maskp = va_arg (*args, u8 **);
   u8 *mask = 0;
-  u8 found_something = 0;
+  u8 found_something;
   ip6_header_t *ip;
   u32 ip_version_traffic_class_and_flow_label;
 
@@ -1066,6 +1061,10 @@ unformat_ip6_mask (unformat_input_t * input, va_list * args)
        break;
     }
 
+  /* Account for "special" field names */
+  found_something = version + traffic_class + flow_label
+    + src_address + dst_address + protocol;
+
 #define _(a) found_something += a;
   foreach_ip6_proto_field;
 #undef _
@@ -1236,12 +1235,16 @@ unformat_classify_mask (unformat_input_t * input, va_list * args)
   u8 *l2 = 0;
   u8 *l3 = 0;
   u8 *l4 = 0;
+  u8 add_l2 = 1;
   int i;
 
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
       if (unformat (input, "hex %U", unformat_hex_string, &mask))
        ;
+      else if (unformat (input, "l2 none"))
+       /* Don't add the l2 header in the mask */
+       add_l2 = 0;
       else if (unformat (input, "l2 %U", unformat_l2_mask, &l2))
        ;
       else if (unformat (input, "l3 %U", unformat_l3_mask, &l3))
@@ -1252,6 +1255,15 @@ unformat_classify_mask (unformat_input_t * input, va_list * args)
        break;
     }
 
+  if (l2 && !add_l2)
+    {
+      vec_free (mask);
+      vec_free (l2);
+      vec_free (l3);
+      vec_free (l4);
+      return 0;
+    }
+
   if (l4 && !l3)
     {
       vec_free (mask);
@@ -1264,15 +1276,20 @@ unformat_classify_mask (unformat_input_t * input, va_list * args)
     {
       if (l2 || l3 || l4)
        {
-         /* "With a free Ethernet header in every package" */
-         if (l2 == 0)
-           vec_validate (l2, 13);
-         mask = l2;
-         if (l3)
+         if (add_l2)
            {
-             vec_append (mask, l3);
-             vec_free (l3);
+             /* "With a free Ethernet header in every package" */
+             if (l2 == 0)
+               vec_validate (l2, 13);
+             mask = l2;
+             if (l3)
+               {
+                 vec_append (mask, l3);
+                 vec_free (l3);
+               }
            }
+         else
+           mask = l3;
          if (l4)
            {
              vec_append (mask, l4);
@@ -1305,7 +1322,7 @@ unformat_classify_mask (unformat_input_t * input, va_list * args)
       if (match == 0)
        clib_warning ("BUG: match 0");
 
-      _vec_len (mask) = match * sizeof (u32x4);
+      vec_set_len (mask, match * sizeof (u32x4));
 
       *matchp = match;
       *maskp = mask;
@@ -1652,13 +1669,13 @@ filter_table_mask_compare (void *a1, void *a2)
   m1 = (u8 *) (t1->mask);
   m2 = (u8 *) (t2->mask);
 
-  for (i = 0; i < vec_len (t1->mask) * sizeof (u32x4); i++)
+  for (i = 0; i < t1->match_n_vectors * sizeof (u32x4); i++)
     {
       n1 += count_set_bits (m1[0]);
       m1++;
     }
 
-  for (i = 0; i < vec_len (t2->mask) * sizeof (u32x4); i++)
+  for (i = 0; i < t2->match_n_vectors * sizeof (u32x4); i++)
     {
       n2 += count_set_bits (m2[0]);
       m2++;
@@ -1673,6 +1690,166 @@ filter_table_mask_compare (void *a1, void *a2)
     return 0;
 }
 
+
+/*
+ * Reorder the chain of tables starting with table_index such
+ * that more more-specific masks come before less-specific masks.
+ * Return the new head of the table chain.
+ */
+u32
+classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index)
+{
+  /*
+   * Form a vector of all classifier tables in this chain.
+   */
+  u32 *tables = 0;
+  vnet_classify_table_t *t;
+  u32 cti;
+  for (cti = table_index; cti != ~0; cti = t->next_table_index)
+    {
+      vec_add1 (tables, cti);
+      t = pool_elt_at_index (cm->tables, cti);
+    }
+
+  /*
+   * Sort filter tables from most-specific mask to least-specific mask.
+   */
+  vec_sort_with_function (tables, filter_table_mask_compare);
+
+  /*
+   * Relink tables via next_table_index fields.
+   */
+  int i;
+  for (i = 0; i < vec_len (tables); i++)
+    {
+      t = pool_elt_at_index (cm->tables, tables[i]);
+
+      if ((i + 1) < vec_len (tables))
+       t->next_table_index = tables[i + 1];
+      else
+       t->next_table_index = ~0;
+    }
+
+  table_index = tables[0];
+  vec_free (tables);
+
+  return table_index;
+}
+
+
+u32
+classify_get_trace_chain (void)
+{
+  u32 table_index;
+
+  table_index = vlib_global_main.trace_filter.classify_table_index;
+
+  return table_index;
+}
+
+/*
+ * Seting the Trace chain to ~0 is a request to delete and clear it.
+ */
+void
+classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index)
+{
+  if (table_index == ~0)
+    {
+      u32 old_table_index;
+
+      old_table_index = vlib_global_main.trace_filter.classify_table_index;
+      vnet_classify_delete_table_index (cm, old_table_index, 1);
+    }
+
+  vlib_global_main.trace_filter.classify_table_index = table_index;
+}
+
+
+u32
+classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index)
+{
+  u32 table_index = ~0;
+
+  if (sw_if_index != ~0
+      && (sw_if_index < vec_len (cm->classify_table_index_by_sw_if_index)))
+    table_index = cm->classify_table_index_by_sw_if_index[sw_if_index];
+
+  return table_index;
+}
+
+void
+classify_set_pcap_chain (vnet_classify_main_t * cm,
+                        u32 sw_if_index, u32 table_index)
+{
+  vnet_main_t *vnm = vnet_get_main ();
+
+  if (sw_if_index != ~0 && table_index != ~0)
+    vec_validate_init_empty (cm->classify_table_index_by_sw_if_index,
+                            sw_if_index, ~0);
+
+  if (table_index == ~0)
+    {
+      u32 old_table_index = ~0;
+
+      if (sw_if_index < vec_len (cm->classify_table_index_by_sw_if_index))
+       old_table_index =
+         cm->classify_table_index_by_sw_if_index[sw_if_index];
+
+      vnet_classify_delete_table_index (cm, old_table_index, 1);
+    }
+
+  /*
+   * Put the table index where device drivers can find them.
+   * This table index will be either a valid table or a ~0 to clear it.
+   */
+  if (vec_len (cm->classify_table_index_by_sw_if_index) > sw_if_index)
+    cm->classify_table_index_by_sw_if_index[sw_if_index] = table_index;
+  if (sw_if_index > 0)
+    {
+      vnet_hw_interface_t *hi;
+      hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
+      hi->trace_classify_table_index = table_index;
+    }
+}
+
+
+/*
+ * Search for a mask-compatible Classify table within the given table chain.
+ */
+u32
+classify_lookup_chain (u32 table_index, u8 * mask, u32 n_skip, u32 n_match)
+{
+  vnet_classify_main_t *cm = &vnet_classify_main;
+  vnet_classify_table_t *t;
+  u32 cti;
+
+  if (table_index == ~0)
+    return ~0;
+
+  for (cti = table_index; cti != ~0; cti = t->next_table_index)
+    {
+      t = pool_elt_at_index (cm->tables, cti);
+
+      /* Classifier geometry mismatch, can't use this table. */
+      if (t->match_n_vectors != n_match || t->skip_n_vectors != n_skip)
+       continue;
+
+      /* Masks aren't congruent, can't use this table. */
+      if (t->match_n_vectors * sizeof (u32x4) != vec_len (mask))
+       continue;
+
+      /* Masks aren't bit-for-bit identical, can't use this table. */
+      if (memcmp (t->mask, mask, t->match_n_vectors * sizeof (u32x4)))
+       continue;
+
+      /* Winner... */
+      return cti;
+    }
+
+  return ~0;
+}
+
+
 static clib_error_t *
 classify_filter_command_fn (vlib_main_t * vm,
                            unformat_input_t * input,
@@ -1685,178 +1862,161 @@ classify_filter_command_fn (vlib_main_t * vm,
   u32 match = ~0;
   u8 *match_vector;
   int is_add = 1;
-  int del_chain = 0;
   u32 table_index = ~0;
   u32 next_table_index = ~0;
   u32 miss_next_index = ~0;
   u32 current_data_flag = 0;
   int current_data_offset = 0;
   u32 sw_if_index = ~0;
-  int i;
-  vnet_classify_table_t *t;
+  int pkt_trace = 0;
+  int pcap = 0;
   u8 *mask = 0;
   vnet_classify_main_t *cm = &vnet_classify_main;
   int rv = 0;
-  vnet_classify_filter_set_t *set = 0;
+  clib_error_t *err = 0;
 
-  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+  unformat_input_t _line_input, *line_input = &_line_input;
+
+  /* Get a line of input. */
+  if (!unformat_user (input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      if (unformat (input, "del"))
+      if (unformat (line_input, "del"))
        is_add = 0;
-      else if (unformat (input, "pcap %=", &sw_if_index, 0))
-       ;
-      else if (unformat (input, "%U",
+      else if (unformat (line_input, "pcap %=", &pcap, 1))
+       sw_if_index = 0;
+      else if (unformat (line_input, "trace"))
+       pkt_trace = 1;
+      else if (unformat (line_input, "%U",
                         unformat_vnet_sw_interface, vnm, &sw_if_index))
+       {
+         if (sw_if_index == 0)
+           return clib_error_return (0, "Local interface not supported...");
+       }
+      else if (unformat (line_input, "buckets %d", &nbuckets))
        ;
-      else if (unformat (input, "buckets %d", &nbuckets))
-       ;
-      else if (unformat (input, "mask %U", unformat_classify_mask,
+      else if (unformat (line_input, "mask %U", unformat_classify_mask,
                         &mask, &skip, &match))
        ;
-      else if (unformat (input, "memory-size %U", unformat_memory_size,
+      else if (unformat (line_input, "memory-size %U", unformat_memory_size,
                         &memory_size))
        ;
       else
        break;
     }
 
-  if (is_add && mask == 0 && table_index == ~0)
-    return clib_error_return (0, "Mask required");
-
-  if (is_add && skip == ~0 && table_index == ~0)
-    return clib_error_return (0, "skip count required");
-
-  if (is_add && match == ~0 && table_index == ~0)
-    return clib_error_return (0, "match count required");
+  if (is_add && mask == 0)
+    err = clib_error_return (0, "Mask required");
 
-  if (sw_if_index == ~0)
-    return clib_error_return (0, "Must specify pcap or interface...");
+  else if (is_add && skip == ~0)
+    err = clib_error_return (0, "skip count required");
 
-  if (!is_add)
-    {
-      u32 set_index = 0;
+  else if (is_add && match == ~0)
+    err = clib_error_return (0, "match count required");
 
-      if (sw_if_index < vec_len (cm->filter_set_by_sw_if_index))
-       set_index = cm->filter_set_by_sw_if_index[sw_if_index];
+  else if (sw_if_index == ~0 && pkt_trace == 0 && pcap == 0)
+    err = clib_error_return (0, "Must specify trace, pcap or interface...");
 
-      if (set_index == 0)
-       {
-         if (sw_if_index == 0)
-           return clib_error_return (0, "No pcap classify filter set...");
-         else
-           return clib_error_return (0, "No classify filter set for %U...",
-                                     format_vnet_sw_if_index_name, vnm,
-                                     sw_if_index);
-       }
+  else if (pkt_trace && pcap)
+    err = clib_error_return
+      (0, "Packet trace and pcap are mutually exclusive...");
 
-      set = pool_elt_at_index (cm->filter_sets, set_index);
+  else if (pkt_trace && sw_if_index != ~0)
+    err = clib_error_return (0, "Packet trace filter is per-system");
 
-      set->refcnt--;
-      ASSERT (set->refcnt >= 0);
-      if (set->refcnt == 0)
-       {
-         del_chain = 1;
-         table_index = set->table_indices[0];
-         vec_reset_length (set->table_indices);
-         pool_put (cm->filter_sets, set);
-         cm->filter_set_by_sw_if_index[sw_if_index] = 0;
-         if (sw_if_index > 0)
-           {
-             vnet_hw_interface_t *hi =
-               vnet_get_sup_hw_interface (vnm, sw_if_index);
-             hi->trace_classify_table_index = ~0;
-           }
-       }
+  if (err)
+    {
+      unformat_free (line_input);
+      return err;
     }
 
-  if (is_add)
+  if (!is_add)
     {
-      u32 set_index = 0;
-
-      if (sw_if_index < vec_len (cm->filter_set_by_sw_if_index))
-       set_index = cm->filter_set_by_sw_if_index[sw_if_index];
-
-      /* Do we have a filter set for this intfc / pcap yet? */
-      if (set_index == 0)
-       {
-         pool_get (cm->filter_sets, set);
-         set->refcnt = 1;
-       }
+      /*
+       * Delete an existing PCAP or trace classify table.
+       */
+      if (pkt_trace)
+       classify_set_trace_chain (cm, ~0);
       else
-       set = pool_elt_at_index (cm->filter_sets, set_index);
+       classify_set_pcap_chain (cm, sw_if_index, ~0);
 
-      for (i = 0; i < vec_len (set->table_indices); i++)
-       {
-         t = pool_elt_at_index (cm->tables, i);
-         /* classifier geometry mismatch, can't use this table */
-         if (t->match_n_vectors != match || t->skip_n_vectors != skip)
-           continue;
-         /* Masks aren't congruent, can't use this table */
-         if (vec_len (t->mask) != vec_len (mask))
-           continue;
-         /* Masks aren't bit-for-bit identical, can't use this table */
-         if (memcmp (t->mask, mask, vec_len (mask)))
-           continue;
-
-         /* Winner... */
-         table_index = i;
-         goto found_table;
-       }
+      vec_free (mask);
+      unformat_free (line_input);
+
+      return 0;
     }
 
-  rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size,
-                                   skip, match, next_table_index,
-                                   miss_next_index, &table_index,
-                                   current_data_flag, current_data_offset,
-                                   is_add, del_chain);
-  vec_free (mask);
+  /*
+   * Find an existing compatible table or else make a new one.
+   */
+  if (pkt_trace)
+    table_index = classify_get_trace_chain ();
+  else
+    table_index = classify_get_pcap_chain (cm, sw_if_index);
 
-  switch (rv)
+  if (table_index != ~0)
     {
-    case 0:
-      break;
-
-    default:
-      return clib_error_return (0, "vnet_classify_add_del_table returned %d",
-                               rv);
+      /*
+       * look for a compatible table in the existing chain
+       *  - if a compatible table is found, table_index is updated with it
+       *  - if not, table_index is updated to ~0 (aka nil) and because of that
+       *    we are going to create one (see below). We save the original head
+       *    in next_table_index so we can chain it with the newly created
+       *    table
+       */
+      next_table_index = table_index;
+      table_index = classify_lookup_chain (table_index, mask, skip, match);
     }
 
-  if (is_add == 0)
-    return 0;
-
-  /* Remember the table */
-  vec_add1 (set->table_indices, table_index);
-  vec_validate_init_empty (cm->filter_set_by_sw_if_index, sw_if_index, 0);
-  cm->filter_set_by_sw_if_index[sw_if_index] = set - cm->filter_sets;
-
-  /* Put top table index where device drivers can find them */
-  if (sw_if_index > 0)
+  /*
+   * When no table is found, make one.
+   */
+  if (table_index == ~0)
     {
-      vnet_hw_interface_t *hi = vnet_get_sup_hw_interface (vnm, sw_if_index);
-      ASSERT (vec_len (set->table_indices) > 0);
-      hi->trace_classify_table_index = set->table_indices[0];
-    }
+      u32 new_head_index;
 
-  /* Sort filter tables from most-specific mask to least-specific mask */
-  vec_sort_with_function (set->table_indices, filter_table_mask_compare);
+      /*
+       * Matching table wasn't found, so create a new one at the
+       * head of the next_table_index chain.
+       */
+      rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size,
+                                       skip, match, next_table_index,
+                                       miss_next_index, &table_index,
+                                       current_data_flag,
+                                       current_data_offset, 1, 0);
 
-  ASSERT (set);
+      if (rv != 0)
+       {
+         vec_free (mask);
+         unformat_free (line_input);
+         return clib_error_return (0,
+                                   "vnet_classify_add_del_table returned %d",
+                                   rv);
+       }
 
-  /* Setup next_table_index fields */
-  for (i = 0; i < vec_len (set->table_indices); i++)
-    {
-      t = pool_elt_at_index (cm->tables, set->table_indices[i]);
+      /*
+       * Reorder tables such that masks are most-specify to least-specific.
+       */
+      new_head_index = classify_sort_table_chain (cm, table_index);
 
-      if ((i + 1) < vec_len (set->table_indices))
-       t->next_table_index = set->table_indices[i + 1];
+      /*
+       * Put first classifier table in chain in a place where
+       * other data structures expect to find and use it.
+       */
+      if (pkt_trace)
+       classify_set_trace_chain (cm, new_head_index);
       else
-       t->next_table_index = ~0;
+       classify_set_pcap_chain (cm, sw_if_index, new_head_index);
     }
 
-found_table:
+  vec_free (mask);
 
-  /* Now try to parse a session */
-  if (unformat (input, "match %U", unformat_classify_match,
+  /*
+   * Now try to parse a and add a filter-match session.
+   */
+  if (unformat (line_input, "match %U", unformat_classify_match,
                cm, &match_vector, table_index) == 0)
     return 0;
 
@@ -1877,10 +2037,24 @@ found_table:
   return 0;
 }
 
+/** Enable / disable packet trace filter */
+int
+vlib_enable_disable_pkt_trace_filter (int enable)
+{
+  if (enable)
+    {
+      vlib_global_main.trace_filter.trace_filter_enable = 1;
+    }
+  else
+    {
+      vlib_global_main.trace_filter.trace_filter_enable = 0;
+    }
+  return 0;
+}
+
 /*?
  * Construct an arbitrary set of packet classifier tables for use with
- * "pcap rx | tx trace," and (eventually) with the vpp packet
- * tracer
+ * "pcap trace rx | tx," and with the vpp packet tracer
  *
  * Packets which match a rule in the classifier table chain
  * will be traced. The tables are automatically ordered so that
@@ -1889,7 +2063,7 @@ found_table:
  * It's reasonably likely that folks will configure a single
  * table with one or two matches. As a result, we configure
  * 8 hash buckets and 128K of match rule space. One can override
- * the defaults by specifiying "buckets <nnn>" and "memory-size <xxx>"
+ * the defaults by specifying "buckets <nnn>" and "memory-size <xxx>"
  * as desired.
  *
  * To build up complex filter chains, repeatedly issue the
@@ -1923,18 +2097,28 @@ found_table:
  * @cliexpar
  * Configuring the classify filter
  *
- * Configure a simple classify filter, and configure pcap rx trace to use it:
+ * Configure a simple classify filter, and configure pcap trace rx to use it:
  *
- * <b><em>classify filter mask l3 ip4 src match l3 ip4 src 192.168.1.11"</em></b><br>
- * <b><em>pcap rx trace on max 100 filter</em></b>
+ * @cliexcmd{classify filter rx mask l3 ip4 src match l3 ip4 src 192.168.1.11}
+ * <b><em>pcap trace rx max 100 filter</em></b>
  *
  * Configure another fairly simple filter
  *
- * <b><em>classify filter mask l3 ip4 src dst match l3 ip4 src 192.168.1.10 dst 192.168.2.10"</em></b>
+ * @cliexcmd{classify filter mask l3 ip4 src dst match l3 ip4 src 192.168.1.10
+ * dst 192.168.2.10}
+ *
  *
- * Clear all current classifier filters
+ * Configure a filter for use with the vpp packet tracer:
+ * @cliexcmd{classify filter trace mask l3 ip4 src dst match l3 ip4 src
+ * 192.168.1.10 dst 192.168.2.10}
+ * <b><em>trace add dpdk-input 100 filter</em></b>
  *
- * <b><em>classify filter del</em></b>
+ * Clear classifier filters
+ *
+ * <b><em>classify filter [trace | rx | tx  | <intfc>] del</em></b>
+ *
+ * To display the top-level classifier tables for each use case:
+ * <b><em>show classify filter</em></b>
  *
  * To inspect the classifier tables, use
  *
@@ -1947,8 +2131,9 @@ VLIB_CLI_COMMAND (classify_filter, static) =
 {
   .path = "classify filter",
   .short_help =
-  "classify filter <intfc> | pcap mask <mask-value> match <match-value> [del]"
-  "[buckets <nn>] [memory-size <n>]",
+  "classify filter <intfc> | pcap mask <mask-value> match <match-value>\n"
+  "  | trace mask <mask-value> match <match-value> [del]\n"
+  "    [buckets <nn>] [memory-size <n>]",
   .function = classify_filter_command_fn,
 };
 /* *INDENT-ON* */
@@ -1960,55 +2145,61 @@ show_classify_filter_command_fn (vlib_main_t * vm,
 {
   vnet_classify_main_t *cm = &vnet_classify_main;
   vnet_main_t *vnm = vnet_get_main ();
-  vnet_classify_filter_set_t *set;
   u8 *name = 0;
   u8 *s = 0;
-  u32 set_index;
   u32 table_index;
   int verbose = 0;
-  int i, j;
+  int i, j, limit;
 
   (void) unformat (input, "verbose %=", &verbose, 1);
 
   vlib_cli_output (vm, "%-30s%s", "Filter Used By", " Table(s)");
   vlib_cli_output (vm, "%-30s%s", "--------------", " --------");
 
-  for (i = 0; i < vec_len (cm->filter_set_by_sw_if_index); i++)
-    {
-      set_index = cm->filter_set_by_sw_if_index[i];
+  limit = vec_len (cm->classify_table_index_by_sw_if_index);
 
-      if (set_index == 0 && verbose == 0)
-       continue;
+  for (i = -1; i < limit; i++)
+    {
+      switch (i)
+       {
+       case -1:
+         table_index = vlib_global_main.trace_filter.classify_table_index;
+         name = format (0, "packet tracer:");
+         break;
 
-      set = pool_elt_at_index (cm->filter_sets, set_index);
+       case 0:
+         table_index = cm->classify_table_index_by_sw_if_index[i];
+         name = format (0, "pcap rx/tx/drop:");
+         break;
 
-      if (i == 0)
-       name = format (0, "pcap rx/tx/drop:");
-      else
-       name = format (0, "%U:", format_vnet_sw_if_index_name, vnm, i);
+       default:
+         table_index = cm->classify_table_index_by_sw_if_index[i];
+         name = format (0, "%U:", format_vnet_sw_if_index_name, vnm, i);
+         break;
+       }
 
       if (verbose)
        {
-         u8 *s = 0;
-         u32 table_index;
-
-         for (j = 0; j < vec_len (set->table_indices); j++)
+         vnet_classify_table_t *t;
+         j = table_index;
+         do
            {
-             table_index = set->table_indices[j];
-             if (table_index != ~0)
-               s = format (s, " %u", table_index);
-             else
+             if (j == ~0)
                s = format (s, " none");
+             else
+               {
+                 s = format (s, " %u", j);
+                 t = pool_elt_at_index (cm->tables, j);
+                 j = t->next_table_index;
+               }
            }
+         while (j != ~0);
 
          vlib_cli_output (vm, "%-30v table(s)%v", name, s);
          vec_reset_length (s);
        }
       else
        {
-         u8 *s = 0;
-         table_index = set->table_indices[0];
-
          if (table_index != ~0)
            s = format (s, " %u", table_index);
          else
@@ -2034,11 +2225,8 @@ VLIB_CLI_COMMAND (show_classify_filter, static) =
 };
 /* *INDENT-ON* */
 
-
-
-
-static u8 *
-format_vnet_classify_table (u8 * s, va_list * args)
+u8 *
+format_vnet_classify_table (u8 *s, va_list *args)
 {
   vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *);
   int verbose = va_arg (*args, int);
@@ -2047,7 +2235,7 @@ format_vnet_classify_table (u8 * s, va_list * args)
 
   if (index == ~0)
     {
-      s = format (s, "%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl",
+      s = format (s, "\n%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl",
                  "NextNode", verbose ? "Details" : "");
       return s;
     }
@@ -2056,7 +2244,8 @@ format_vnet_classify_table (u8 * s, va_list * args)
   s = format (s, "%10u%10d%10d%10d", index, t->active_elements,
              t->next_table_index, t->miss_next_index);
 
-  s = format (s, "\n  Heap: %U", format_mheap, t->mheap, 0 /*verbose */ );
+  s = format (s, "\n  Heap: %U", format_clib_mem_heap, t->mheap,
+             0 /*verbose */ );
 
   s = format (s, "\n  nbuckets %d, skip %d match %d flag %d offset %d",
              t->nbuckets, t->skip_n_vectors, t->match_n_vectors,
@@ -2098,20 +2287,22 @@ show_classify_tables_command_fn (vlib_main_t * vm,
     }
 
   /* *INDENT-OFF* */
-  pool_foreach (t, cm->tables,
-  ({
+  pool_foreach (t, cm->tables)
+   {
     if (match_index == ~0 || (match_index == t - cm->tables))
       vec_add1 (indices, t - cm->tables);
-  }));
+  }
   /* *INDENT-ON* */
 
   if (vec_len (indices))
     {
-      vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose,
-                      ~0 /* hdr */ );
       for (i = 0; i < vec_len (indices); i++)
-       vlib_cli_output (vm, "%U", format_vnet_classify_table, cm,
-                        verbose, indices[i]);
+       {
+         vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose,
+                          ~0 /* hdr */);
+         vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose,
+                          indices[i]);
+       }
     }
   else
     vlib_cli_output (vm, "No classifier tables configured");
@@ -2147,7 +2338,7 @@ unformat_l4_match (unformat_input_t * input, va_list * args)
       else if (unformat (input, "dst_port %d", &dst_port))
        ;
       else
-       return 0;
+       break;
     }
 
   h.src_port = clib_host_to_net_u16 (src_port);
@@ -2508,6 +2699,7 @@ unformat_classify_match (unformat_input_t * input, va_list * args)
   u8 *l2 = 0;
   u8 *l3 = 0;
   u8 *l4 = 0;
+  u8 add_l2 = 1;
 
   if (pool_is_free_index (cm->tables, table_index))
     return 0;
@@ -2518,6 +2710,9 @@ unformat_classify_match (unformat_input_t * input, va_list * args)
     {
       if (unformat (input, "hex %U", unformat_hex_string, &match))
        ;
+      else if (unformat (input, "l2 none"))
+       /* Don't add the l2 header in the mask */
+       add_l2 = 0;
       else if (unformat (input, "l2 %U", unformat_l2_match, &l2))
        ;
       else if (unformat (input, "l3 %U", unformat_l3_match, &l3))
@@ -2528,6 +2723,15 @@ unformat_classify_match (unformat_input_t * input, va_list * args)
        break;
     }
 
+  if (l2 && !add_l2)
+    {
+      vec_free (match);
+      vec_free (l2);
+      vec_free (l3);
+      vec_free (l4);
+      return 0;
+    }
+
   if (l4 && !l3)
     {
       vec_free (match);
@@ -2540,15 +2744,20 @@ unformat_classify_match (unformat_input_t * input, va_list * args)
     {
       if (l2 || l3 || l4)
        {
-         /* "Win a free Ethernet header in every packet" */
-         if (l2 == 0)
-           vec_validate_aligned (l2, 13, sizeof (u32x4));
-         match = l2;
-         if (l3)
+         if (add_l2)
            {
-             vec_append_aligned (match, l3, sizeof (u32x4));
-             vec_free (l3);
+             /* "Win a free Ethernet header in every packet" */
+             if (l2 == 0)
+               vec_validate_aligned (l2, 13, sizeof (u32x4));
+             match = l2;
+             if (l3)
+               {
+                 vec_append_aligned (match, l3, sizeof (u32x4));
+                 vec_free (l3);
+               }
            }
+         else
+           match = l3;
          if (l4)
            {
              vec_append_aligned (match, l4, sizeof (u32x4));
@@ -2563,8 +2772,8 @@ unformat_classify_match (unformat_input_t * input, va_list * args)
         sizeof (u32x4));
 
       /* Set size, include skipped vectors */
-      _vec_len (match) =
-       (t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4);
+      vec_set_len (match,
+                  (t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4));
 
       *matchp = match;
 
@@ -2575,13 +2784,10 @@ unformat_classify_match (unformat_input_t * input, va_list * args)
 }
 
 int
-vnet_classify_add_del_session (vnet_classify_main_t * cm,
-                              u32 table_index,
-                              u8 * match,
-                              u32 hit_next_index,
-                              u32 opaque_index,
-                              i32 advance,
-                              u8 action, u32 metadata, int is_add)
+vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
+                              const u8 *match, u16 hit_next_index,
+                              u32 opaque_index, i32 advance, u8 action,
+                              u32 metadata, int is_add)
 {
   vnet_classify_table_t *t;
   vnet_classify_entry_5_t _max_e __attribute__ ((aligned (16)));
@@ -2850,7 +3056,6 @@ static clib_error_t *
 vnet_classify_init (vlib_main_t * vm)
 {
   vnet_classify_main_t *cm = &vnet_classify_main;
-  vnet_classify_filter_set_t *set;
 
   cm->vlib_main = vm;
   cm->vnet_main = vnet_get_main ();
@@ -2868,19 +3073,25 @@ vnet_classify_init (vlib_main_t * vm)
 
   vnet_classify_register_unformat_acl_next_index_fn (unformat_acl_next_node);
 
-  /* Filter set 0 is grounded... */
-  pool_get (cm->filter_sets, set);
-  set->refcnt = 0x7FFFFFFF;
-  vec_validate (set->table_indices, 0);
-  set->table_indices[0] = ~0;
-  /* Initialize the pcap filter set */
-  vec_validate (cm->filter_set_by_sw_if_index, 0);
+  vlib_global_main.trace_filter.classify_table_index = ~0;
 
   return 0;
 }
 
 VLIB_INIT_FUNCTION (vnet_classify_init);
 
+int
+vnet_is_packet_traced (vlib_buffer_t * b, u32 classify_table_index, int func)
+{
+  return vnet_is_packet_traced_inline (b, classify_table_index, func);
+}
+VLIB_REGISTER_TRACE_FILTER_FUNCTION (vnet_is_packet_traced_fn, static) = {
+  .name = "vnet_is_packet_traced",
+  .description = "classifier based filter",
+  .priority = 50,
+  .function = vnet_is_packet_traced
+};
+
 #define TEST_CODE 0
 
 #if TEST_CODE > 0
@@ -3031,7 +3242,7 @@ test_classify_churn (test_classify_main_t * tm)
   for (i = 0; i < tm->sessions; i++)
     {
       u8 *key_minus_skip;
-      u64 hash;
+      u32 hash;
       vnet_classify_entry_t *e;
 
       ep = tm->entries + i;