misc: move to new pool_foreach macros
[vpp.git] / src / vnet / classify / vnet_classify.c
old mode 100755 (executable)
new mode 100644 (file)
index de11a10..088890a
@@ -147,13 +147,8 @@ vnet_classify_new_table (vnet_classify_main_t * cm,
   t->skip_n_vectors = skip_n_vectors;
   t->entries_per_page = 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);
@@ -180,12 +175,7 @@ vnet_classify_delete_table_index (vnet_classify_main_t * cm,
 
   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);
 }
 
@@ -1037,7 +1027,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;
 
@@ -1070,6 +1060,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 _
@@ -1697,6 +1691,7 @@ classify_filter_command_fn (vlib_main_t * vm,
   int current_data_offset = 0;
   u32 sw_if_index = ~0;
   int pkt_trace = 0;
+  int pcap = 0;
   int i;
   vnet_classify_table_t *t;
   u8 *mask = 0;
@@ -1704,51 +1699,67 @@ classify_filter_command_fn (vlib_main_t * vm,
   int rv = 0;
   vnet_classify_filter_set_t *set = 0;
   u32 set_index = ~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, "trace"))
+      else if (unformat (line_input, "pcap %=", &pcap, 1))
+       sw_if_index = 0;
+      else if (unformat (line_input, "trace"))
        pkt_trace = 1;
-      else if (unformat (input, "%U",
+      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 (sw_if_index == 0)
-    return clib_error_return (0, "Local interface not supported...");
-
   if (is_add && mask == 0 && table_index == ~0)
-    return clib_error_return (0, "Mask required");
+    err = clib_error_return (0, "Mask required");
 
-  if (is_add && skip == ~0 && table_index == ~0)
-    return clib_error_return (0, "skip count required");
+  else if (is_add && skip == ~0 && table_index == ~0)
+    err = clib_error_return (0, "skip count required");
 
-  if (is_add && match == ~0 && table_index == ~0)
-    return clib_error_return (0, "match count required");
+  else if (is_add && match == ~0 && table_index == ~0)
+    err = clib_error_return (0, "match count required");
 
-  if (sw_if_index == ~0 && pkt_trace == 0)
-    return clib_error_return (0, "Must specify trace, pcap or interface...");
+  else if (sw_if_index == ~0 && pkt_trace == 0 && pcap == 0)
+    err = clib_error_return (0, "Must specify trace, pcap or interface...");
 
-  if (pkt_trace && sw_if_index != ~0)
-    return clib_error_return (0, "Packet trace filter is per-system");
+  else if (pkt_trace && pcap)
+    err = clib_error_return
+      (0, "Packet trace and pcap are mutually exclusive...");
 
-  if (!is_add)
+  else if (pkt_trace && sw_if_index != ~0)
+    err = clib_error_return (0, "Packet trace filter is per-system");
+
+  if (err)
     {
+      unformat_free (line_input);
+      return err;
+    }
 
+  if (!is_add)
+    {
       if (pkt_trace)
        set_index = vlib_global_main.trace_filter.trace_filter_set_index;
       else if (sw_if_index < vec_len (cm->filter_set_by_sw_if_index))
@@ -1757,14 +1768,16 @@ classify_filter_command_fn (vlib_main_t * vm,
       if (set_index == ~0)
        {
          if (pkt_trace)
-           return clib_error_return (0,
-                                     "No pkt trace classify filter set...");
-         if (sw_if_index == 0)
-           return clib_error_return (0, "No pcap classify filter set...");
+           err =
+             clib_error_return (0, "No pkt trace classify filter set...");
+         else if (sw_if_index == 0)
+           err = 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);
+           err = clib_error_return (0, "No classify filter set for %U...",
+                                    format_vnet_sw_if_index_name, vnm,
+                                    sw_if_index);
+         unformat_free (line_input);
+         return err;
        }
 
       set = pool_elt_at_index (cm->filter_sets, set_index);
@@ -1812,21 +1825,23 @@ classify_filter_command_fn (vlib_main_t * vm,
       else
        set = pool_elt_at_index (cm->filter_sets, set_index);
 
+      ASSERT (set);
+
       for (i = 0; i < vec_len (set->table_indices); i++)
        {
-         t = pool_elt_at_index (cm->tables, i);
+         t = pool_elt_at_index (cm->tables, set->table_indices[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))
+         if (vec_len (t->mask) * sizeof (u32x4) != 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;
+         table_index = set->table_indices[i];
          goto found_table;
        }
     }
@@ -1838,18 +1853,18 @@ classify_filter_command_fn (vlib_main_t * vm,
                                    is_add, del_chain);
   vec_free (mask);
 
-  switch (rv)
+  if (rv != 0)
     {
-    case 0:
-      break;
-
-    default:
+      unformat_free (line_input);
       return clib_error_return (0, "vnet_classify_add_del_table returned %d",
                                rv);
     }
 
   if (is_add == 0)
-    return 0;
+    {
+      unformat_free (line_input);
+      return 0;
+    }
 
   /* Remember the table */
   vec_add1 (set->table_indices, table_index);
@@ -1863,19 +1878,9 @@ classify_filter_command_fn (vlib_main_t * vm,
       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 && pkt_trace == 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];
-    }
-
   /* Sort filter tables from most-specific mask to least-specific mask */
   vec_sort_with_function (set->table_indices, filter_table_mask_compare);
 
-  ASSERT (set);
-
   /* Setup next_table_index fields */
   for (i = 0; i < vec_len (set->table_indices); i++)
     {
@@ -1887,10 +1892,18 @@ classify_filter_command_fn (vlib_main_t * vm,
        t->next_table_index = ~0;
     }
 
+  /* Put top table index where device drivers can find them */
+  if (sw_if_index > 0 && pkt_trace == 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];
+    }
+
 found_table:
 
   /* Now try to parse a session */
-  if (unformat (input, "match %U", unformat_classify_match,
+  if (unformat (line_input, "match %U", unformat_classify_match,
                cm, &match_vector, table_index) == 0)
     return 0;
 
@@ -2069,7 +2082,6 @@ show_classify_filter_command_fn (vlib_main_t * vm,
 
       if (verbose)
        {
-         u8 *s = 0;
          u32 table_index;
 
          for (j = 0; j < vec_len (set->table_indices); j++)
@@ -2086,8 +2098,7 @@ show_classify_filter_command_fn (vlib_main_t * vm,
        }
       else
        {
-         u8 *s = 0;
-         table_index = set->table_indices[0];
+         table_index = set->table_indices ? set->table_indices[0] : ~0;
 
          if (table_index != ~0)
            s = format (s, " %u", table_index);
@@ -2136,7 +2147,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,
@@ -2178,11 +2190,11 @@ 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))
@@ -2949,13 +2961,11 @@ 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);
+  pool_get_zero (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);
-  cm->filter_set_by_sw_if_index[0] = ~0;
+  cm->filter_set_by_sw_if_index[0] = 0;
   /* Initialize the packet tracer filter set */
   vlib_global_main.trace_filter.trace_filter_set_index = ~0;