classify: fix parsing for l4 match
[vpp.git] / src / vnet / classify / vnet_classify.c
index ca34eec..ab933eb 100644 (file)
@@ -128,17 +128,16 @@ 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_memcpy_fast (t->mask, mask, match_n_vectors * sizeof (u32x4));
@@ -393,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;
     }
 }
@@ -409,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;
@@ -743,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;
@@ -916,7 +911,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)
@@ -985,6 +980,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 _
@@ -1891,13 +1887,13 @@ classify_filter_command_fn (vlib_main_t * vm,
        break;
     }
 
-  if (is_add && mask == 0 && table_index == ~0)
+  if (is_add && mask == 0)
     err = clib_error_return (0, "Mask required");
 
-  else if (is_add && skip == ~0 && table_index == ~0)
+  else if (is_add && skip == ~0)
     err = clib_error_return (0, "skip count required");
 
-  else if (is_add && match == ~0 && table_index == ~0)
+  else if (is_add && match == ~0)
     err = clib_error_return (0, "match count required");
 
   else if (sw_if_index == ~0 && pkt_trace == 0 && pcap == 0)
@@ -1941,20 +1937,30 @@ classify_filter_command_fn (vlib_main_t * vm,
     table_index = classify_get_pcap_chain (cm, sw_if_index);
 
   if (table_index != ~0)
-    table_index = classify_lookup_chain (table_index, mask, skip, match);
+    {
+      /*
+       * 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);
+    }
 
   /*
    * When no table is found, make one.
    */
   if (table_index == ~0)
     {
+      u32 new_head_index;
+
       /*
        * Matching table wasn't found, so create a new one at the
        * head of the next_table_index chain.
        */
-      next_table_index = table_index;
-      table_index = ~0;
-
       rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size,
                                        skip, match, next_table_index,
                                        miss_next_index, &table_index,
@@ -1973,16 +1979,16 @@ classify_filter_command_fn (vlib_main_t * vm,
       /*
        * Reorder tables such that masks are most-specify to least-specific.
        */
-      table_index = classify_sort_table_chain (cm, table_index);
+      new_head_index = classify_sort_table_chain (cm, table_index);
 
       /*
        * 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, table_index);
+       classify_set_trace_chain (cm, new_head_index);
       else
-       classify_set_pcap_chain (cm, sw_if_index, table_index);
+       classify_set_pcap_chain (cm, sw_if_index, new_head_index);
     }
 
   vec_free (mask);
@@ -2197,11 +2203,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);
@@ -2311,7 +2314,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);
@@ -2739,13 +2742,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, u32 hit_next_index,
+                              u32 opaque_index, i32 advance, u8 action,
+                              u16 metadata, int is_add)
 {
   vnet_classify_table_t *t;
   vnet_classify_entry_5_t _max_e __attribute__ ((aligned (16)));