vnet_classify: Handle non-aligned vectors and various bugfixes
[vpp.git] / vnet / vnet / classify / vnet_classify.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vnet/classify/vnet_classify.h>
16 #include <vnet/classify/input_acl.h>
17 #include <vnet/ip/ip.h>
18 #include <vnet/api_errno.h>     /* for API error numbers */
19 #include <vnet/l2/l2_classify.h> /* for L2_CLASSIFY_NEXT_xxx */
20
21 #if VALIDATION_SCAFFOLDING
22 /* Validation scaffolding */
23 void mv (vnet_classify_table_t * t)
24 {
25   void * oldheap;
26
27   oldheap = clib_mem_set_heap (t->mheap);
28   clib_mem_validate();
29   clib_mem_set_heap (oldheap);
30 }
31
32 void rogue (vnet_classify_table_t * t)
33 {
34   int i, j, k;
35   vnet_classify_entry_t * v, * save_v;
36   u32 active_elements = 0;
37   vnet_classify_bucket_t * b;
38   
39   for (i = 0; i < t->nbuckets; i++)
40     {
41       b = &t->buckets [i];
42       if (b->offset == 0)
43         continue;
44       save_v = vnet_classify_get_entry (t, b->offset);
45       for (j = 0; j < (1<<b->log2_pages); j++)
46         {
47           for (k = 0; k < t->entries_per_page; k++)
48             {
49               v = vnet_classify_entry_at_index 
50                 (t, save_v, j*t->entries_per_page + k);
51
52               if (vnet_classify_entry_is_busy (v))
53                 active_elements++;
54             }
55         }
56     }
57
58   if (active_elements != t->active_elements)
59     clib_warning ("found %u expected %u elts", active_elements, 
60                   t->active_elements);
61 }
62 #else
63 void mv (vnet_classify_table_t * t) { }
64 void rogue (vnet_classify_table_t * t) { }
65 #endif
66
67 vnet_classify_table_t * 
68 vnet_classify_new_table (vnet_classify_main_t *cm,
69                          u8 * mask, u32 nbuckets, u32 memory_size,
70                          u32 skip_n_vectors,
71                          u32 match_n_vectors)
72 {
73   vnet_classify_table_t * t;
74   void * oldheap;
75     
76   nbuckets = 1 << (max_log2 (nbuckets));
77
78   pool_get_aligned (cm->tables, t, CLIB_CACHE_LINE_BYTES);
79   memset(t, 0, sizeof (*t));
80   
81   vec_validate_aligned (t->mask, match_n_vectors - 1, sizeof(u32x4));
82   memcpy (t->mask, mask, match_n_vectors * sizeof (u32x4));
83
84   t->next_table_index = ~0;
85   t->nbuckets = nbuckets;
86   t->log2_nbuckets = max_log2 (nbuckets);
87   t->match_n_vectors = match_n_vectors;
88   t->skip_n_vectors = skip_n_vectors;
89   t->entries_per_page = 2;
90
91   t->mheap = mheap_alloc (0 /* use VM */, memory_size);
92
93   vec_validate_aligned (t->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
94   oldheap = clib_mem_set_heap (t->mheap);
95
96   t->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, 
97                                            CLIB_CACHE_LINE_BYTES);
98   t->writer_lock[0] = 0;
99
100   clib_mem_set_heap (oldheap);
101   return (t);
102 }
103
104 void vnet_classify_delete_table_index (vnet_classify_main_t *cm, 
105                                        u32 table_index)
106 {
107   vnet_classify_table_t * t;
108
109   /* Tolerate multiple frees, up to a point */
110   if (pool_is_free_index (cm->tables, table_index))
111     return;
112
113   t = pool_elt_at_index (cm->tables, table_index);
114   if (t->next_table_index != ~0)
115     vnet_classify_delete_table_index (cm, t->next_table_index);
116
117   vec_free (t->mask);
118   vec_free (t->buckets);
119   mheap_free (t->mheap);
120   
121   pool_put (cm->tables, t);
122 }
123
124 static vnet_classify_entry_t *
125 vnet_classify_entry_alloc (vnet_classify_table_t * t, u32 log2_pages)
126 {
127   vnet_classify_entry_t * rv = 0;
128 #define _(size)                                 \
129   vnet_classify_entry_##size##_t * rv##size = 0;
130   foreach_size_in_u32x4;
131 #undef _
132   
133   void * oldheap;
134
135   ASSERT (t->writer_lock[0]);
136   if (log2_pages >= vec_len (t->freelists) || t->freelists [log2_pages] == 0)
137     {
138       oldheap = clib_mem_set_heap (t->mheap);
139       
140       vec_validate (t->freelists, log2_pages);
141
142       switch(t->match_n_vectors)
143         {
144           /* Euchre the vector allocator into allocating the right sizes */
145 #define _(size)                                                         \
146         case size:                                                      \
147           vec_validate_aligned                                          \
148             (rv##size, ((1<<log2_pages)*t->entries_per_page) - 1,       \
149           CLIB_CACHE_LINE_BYTES);                                       \
150           rv = (vnet_classify_entry_t *) rv##size;                      \
151           break;
152           foreach_size_in_u32x4;
153 #undef _
154
155         default:
156           abort();
157         }
158       
159       clib_mem_set_heap (oldheap);
160       goto initialize;
161     }
162   rv = t->freelists[log2_pages];
163   t->freelists[log2_pages] = rv->next_free;
164   
165 initialize:
166   ASSERT(rv);
167   ASSERT (vec_len(rv) == (1<<log2_pages)*t->entries_per_page);
168
169   switch (t->match_n_vectors)
170     {
171 #define _(size)                                                         \
172     case size:                                                          \
173       if(vec_len(rv))                                                   \
174         memset (rv, 0xff, sizeof (*rv##size) * vec_len(rv));            \
175       break;
176       foreach_size_in_u32x4;
177 #undef _
178       
179     default:
180       abort();
181     }
182   
183   return rv;
184 }
185
186 static void
187 vnet_classify_entry_free (vnet_classify_table_t * t,
188                           vnet_classify_entry_t * v)
189 {
190     u32 free_list_index;
191
192     ASSERT (t->writer_lock[0]);
193
194     free_list_index = min_log2(vec_len(v)/t->entries_per_page);
195
196     ASSERT(vec_len (t->freelists) > free_list_index);
197
198     v->next_free = t->freelists[free_list_index];
199     t->freelists[free_list_index] = v;
200 }
201
202 static inline void make_working_copy
203 (vnet_classify_table_t * t, vnet_classify_bucket_t * b)
204 {
205   vnet_classify_entry_t * v;
206   vnet_classify_bucket_t working_bucket __attribute__((aligned (8)));
207   void * oldheap;
208   vnet_classify_entry_t * working_copy;
209 #define _(size)                                 \
210   vnet_classify_entry_##size##_t * working_copy##size = 0;
211   foreach_size_in_u32x4;
212 #undef _
213   u32 cpu_number = os_get_cpu_number();
214
215   if (cpu_number >= vec_len (t->working_copies))
216     {
217       oldheap = clib_mem_set_heap (t->mheap);
218       vec_validate (t->working_copies, cpu_number);
219       clib_mem_set_heap (oldheap);
220     }
221
222   /* 
223    * working_copies are per-cpu so that near-simultaneous
224    * updates from multiple threads will not result in sporadic, spurious
225    * lookup failures. 
226    */
227   working_copy = t->working_copies[cpu_number];
228
229   t->saved_bucket.as_u64 = b->as_u64;
230   oldheap = clib_mem_set_heap (t->mheap);
231
232   if ((1<<b->log2_pages)*t->entries_per_page > vec_len (working_copy))
233     {
234       switch(t->match_n_vectors)
235         {
236           /* Euchre the vector allocator into allocating the right sizes */
237 #define _(size)                                                         \
238         case size:                                                      \
239           working_copy##size = (void *) working_copy;                   \
240           vec_validate_aligned                                          \
241             (working_copy##size,                                        \
242              ((1<<b->log2_pages)*t->entries_per_page) - 1,              \
243              CLIB_CACHE_LINE_BYTES);                                    \
244           working_copy = (void *) working_copy##size;                   \
245             break;
246         foreach_size_in_u32x4;
247 #undef _
248
249         default:
250           abort();
251         }
252       t->working_copies[cpu_number] = working_copy;
253     }
254
255   _vec_len(working_copy) = (1<<b->log2_pages)*t->entries_per_page;
256   clib_mem_set_heap (oldheap);
257
258   v = vnet_classify_get_entry (t, b->offset);
259   
260   switch(t->match_n_vectors)
261     {
262 #define _(size)                                         \
263     case size:                                          \
264       memcpy (working_copy, v,                          \
265               sizeof (vnet_classify_entry_##size##_t)   \
266               * (1<<b->log2_pages)                      \
267               * (t->entries_per_page));                 \
268       break;
269       foreach_size_in_u32x4 ;
270 #undef _
271
272     default: 
273       abort();
274     }
275   
276   working_bucket.as_u64 = b->as_u64;
277   working_bucket.offset = vnet_classify_get_offset (t, working_copy);
278   CLIB_MEMORY_BARRIER();
279   b->as_u64 = working_bucket.as_u64;
280   t->working_copies[cpu_number] = working_copy;
281 }
282
283 static vnet_classify_entry_t *
284 split_and_rehash (vnet_classify_table_t * t,
285                   vnet_classify_entry_t * old_values,
286                   u32 new_log2_pages)
287 {
288   vnet_classify_entry_t * new_values, * v, * new_v;
289   int i, j, k;
290   
291   new_values = vnet_classify_entry_alloc (t, new_log2_pages);
292   
293   for (i = 0; i < (vec_len (old_values)/t->entries_per_page); i++)
294     {
295       u64 new_hash;
296       
297       for (j = 0; j < t->entries_per_page; j++)
298         {
299           v = vnet_classify_entry_at_index 
300             (t, old_values, i * t->entries_per_page + j);
301           
302           if (vnet_classify_entry_is_busy (v))
303             {
304               /* Hack so we can use the packet hash routine */
305               u8 * key_minus_skip;
306               key_minus_skip = (u8 *) v->key;
307               key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
308
309               new_hash = vnet_classify_hash_packet (t, key_minus_skip);
310               new_hash >>= t->log2_nbuckets;
311               new_hash &= (1<<new_log2_pages) - 1;
312               
313               for (k = 0; k < t->entries_per_page; k++)
314                 {
315                   new_v = vnet_classify_entry_at_index (t, new_values, 
316                                                         new_hash + k);
317                   
318                   if (vnet_classify_entry_is_free (new_v))
319                     {
320                       memcpy (new_v, v, sizeof (vnet_classify_entry_t) 
321                               + (t->match_n_vectors * sizeof (u32x4)));
322                       new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
323                       goto doublebreak;
324                     }
325                 }
326               /* Crap. Tell caller to try again */
327               vnet_classify_entry_free (t, new_values);
328               return 0;
329             }
330         doublebreak:
331           ;
332         }
333     }
334   return new_values;
335 }
336
337 int vnet_classify_add_del (vnet_classify_table_t * t, 
338                            vnet_classify_entry_t * add_v,
339                            int is_add)
340 {
341   u32 bucket_index;
342   vnet_classify_bucket_t * b, tmp_b;
343   vnet_classify_entry_t * v, * new_v, * save_new_v, * working_copy, * save_v;
344   u32 value_index;
345   int rv = 0;
346   int i;
347   u64 hash, new_hash;
348   u32 new_log2_pages;
349   u32 cpu_number = os_get_cpu_number();
350   u8 * key_minus_skip;
351
352   ASSERT ((add_v->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
353
354   key_minus_skip = (u8 *) add_v->key;
355   key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
356
357   hash = vnet_classify_hash_packet (t, key_minus_skip);
358
359   bucket_index = hash & (t->nbuckets-1);
360   b = &t->buckets[bucket_index];
361
362   hash >>= t->log2_nbuckets;
363
364   while (__sync_lock_test_and_set (t->writer_lock, 1))
365     ; 
366
367   /* First elt in the bucket? */
368   if (b->offset == 0)
369     {
370       if (is_add == 0)
371         {
372           rv = -1;
373           goto unlock;
374         }
375
376       v = vnet_classify_entry_alloc (t, 0 /* new_log2_pages */);
377       memcpy (v, add_v, sizeof (vnet_classify_entry_t) +
378               t->match_n_vectors * sizeof (u32x4));
379       v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
380
381       tmp_b.as_u64 = 0;
382       tmp_b.offset = vnet_classify_get_offset (t, v);
383
384       b->as_u64 = tmp_b.as_u64;
385       t->active_elements ++;
386
387       goto unlock;
388     }
389   
390   make_working_copy (t, b);
391   
392   save_v = vnet_classify_get_entry (t, t->saved_bucket.offset);
393   value_index = hash & ((1<<t->saved_bucket.log2_pages)-1);
394   
395   if (is_add)
396     {
397       /* 
398        * For obvious (in hindsight) reasons, see if we're supposed to
399        * replace an existing key, then look for an empty slot.
400        */
401
402       for (i = 0; i < t->entries_per_page; i++)
403         {
404           v = vnet_classify_entry_at_index (t, save_v, value_index + i);
405
406           if (!memcmp (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4)))
407             {
408               memcpy (v, add_v, sizeof (vnet_classify_entry_t) +
409                       t->match_n_vectors * sizeof(u32x4));
410               v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
411
412               CLIB_MEMORY_BARRIER();
413               /* Restore the previous (k,v) pairs */
414               b->as_u64 = t->saved_bucket.as_u64;
415               goto unlock;
416             }
417         }
418       for (i = 0; i < t->entries_per_page; i++)
419         {
420           v = vnet_classify_entry_at_index (t, save_v, value_index + i);
421
422           if (vnet_classify_entry_is_free (v))
423             {
424               memcpy (v, add_v, sizeof (vnet_classify_entry_t) +
425                       t->match_n_vectors * sizeof(u32x4));
426               v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
427               CLIB_MEMORY_BARRIER();
428               b->as_u64 = t->saved_bucket.as_u64;
429               t->active_elements ++;
430               goto unlock;
431             }
432         }
433       /* no room at the inn... split case... */
434     }
435   else
436     {
437       for (i = 0; i < t->entries_per_page; i++)
438         {
439           v = vnet_classify_entry_at_index (t, save_v, value_index + i);
440
441           if (!memcmp (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4)))
442             {
443               memset (v, 0xff, sizeof (vnet_classify_entry_t) +
444                       t->match_n_vectors * sizeof(u32x4));
445               v->flags |= VNET_CLASSIFY_ENTRY_FREE;
446               CLIB_MEMORY_BARRIER();
447               b->as_u64 = t->saved_bucket.as_u64;
448               t->active_elements --;
449               goto unlock;
450             }
451         }
452       rv = -3;
453       b->as_u64 = t->saved_bucket.as_u64;
454       goto unlock;
455     }
456
457   new_log2_pages = t->saved_bucket.log2_pages + 1;
458
459  expand_again:
460   working_copy = t->working_copies[cpu_number];
461   new_v = split_and_rehash (t, working_copy, new_log2_pages);
462
463   if (new_v == 0)
464     {
465       new_log2_pages++;
466       goto expand_again;
467     }
468
469   /* Try to add the new entry */
470   save_new_v = new_v;
471
472   key_minus_skip = (u8 *) add_v->key;
473   key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
474
475   new_hash = vnet_classify_hash_packet_inline (t, key_minus_skip);
476   new_hash >>= t->log2_nbuckets;
477   new_hash &= (1<<min_log2((vec_len(new_v)/t->entries_per_page))) - 1;
478   
479   for (i = 0; i < t->entries_per_page; i++)
480     {
481       new_v = vnet_classify_entry_at_index (t, save_new_v, new_hash + i);
482
483       if (vnet_classify_entry_is_free (new_v))
484         {
485           memcpy (new_v, add_v, sizeof (vnet_classify_entry_t) +
486                   t->match_n_vectors * sizeof(u32x4));
487           new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
488           goto expand_ok;
489         }
490     }
491   /* Crap. Try again */
492   new_log2_pages++;
493   vnet_classify_entry_free (t, save_new_v);
494   goto expand_again;
495
496  expand_ok:
497   tmp_b.log2_pages = min_log2 (vec_len (save_new_v)/t->entries_per_page);
498   tmp_b.offset = vnet_classify_get_offset (t, save_new_v);
499   CLIB_MEMORY_BARRIER();
500   b->as_u64 = tmp_b.as_u64;
501   t->active_elements ++;
502   v = vnet_classify_get_entry (t, t->saved_bucket.offset);
503   vnet_classify_entry_free (t, v);
504
505  unlock:
506   CLIB_MEMORY_BARRIER();
507   t->writer_lock[0] = 0;
508
509   return rv;
510 }
511
512 typedef CLIB_PACKED(struct {
513   ethernet_header_t eh;
514   ip4_header_t ip;
515 }) classify_data_or_mask_t;
516
517 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h)
518 {
519   return vnet_classify_hash_packet_inline (t, h);      
520 }
521
522 vnet_classify_entry_t * 
523 vnet_classify_find_entry (vnet_classify_table_t * t,
524                           u8 * h, u64 hash, f64 now)
525 {
526   return vnet_classify_find_entry_inline (t, h, hash, now);
527 }
528
529 static u8 * format_classify_entry (u8 * s, va_list * args)
530   {
531   vnet_classify_table_t * t = va_arg (*args, vnet_classify_table_t *);
532   vnet_classify_entry_t * e = va_arg (*args, vnet_classify_entry_t *);
533
534   s = format
535     (s, "[%u]: next_index %d advance %d opaque %d\n",
536      vnet_classify_get_offset (t, e), e->next_index, e->advance, 
537      e->opaque_index);
538
539
540   s = format (s, "        k: %U\n", format_hex_bytes, e->key,
541               t->match_n_vectors * sizeof(u32x4));
542   
543   if (vnet_classify_entry_is_busy (e))
544     s = format (s, "        hits %lld, last_heard %.2f\n",
545                 e->hits, e->last_heard);
546   else
547     s = format (s, "  entry is free\n");
548   return s;
549   }
550  
551 u8 * format_classify_table (u8 * s, va_list * args)
552 {
553   vnet_classify_table_t * t = va_arg (*args, vnet_classify_table_t *);
554   int verbose = va_arg (*args, int);
555   vnet_classify_bucket_t * b;
556   vnet_classify_entry_t * v, * save_v;
557   int i, j, k;
558   u64 active_elements = 0;
559   
560   for (i = 0; i < t->nbuckets; i++)
561     {
562       b = &t->buckets [i];
563       if (b->offset == 0)
564         {
565           if (verbose > 1)
566             s = format (s, "[%d]: empty\n", i);
567           continue;
568         }
569
570       if (verbose)
571         {
572           s = format (s, "[%d]: heap offset %d, len %d\n", i, 
573                       b->offset, (1<<b->log2_pages));
574         }
575
576       save_v = vnet_classify_get_entry (t, b->offset);
577       for (j = 0; j < (1<<b->log2_pages); j++)
578         {
579           for (k = 0; k < t->entries_per_page; k++)
580             {
581   
582               v = vnet_classify_entry_at_index (t, save_v, 
583                                                 j*t->entries_per_page + k);
584
585               if (vnet_classify_entry_is_free (v))
586                 {
587                   if (verbose > 1)
588                     s = format (s, "    %d: empty\n", 
589                                 j * t->entries_per_page + k);
590                   continue;
591                 }
592               if (verbose)
593                 {
594                   s = format (s, "    %d: %U\n", 
595                               j * t->entries_per_page + k,
596                               format_classify_entry, t, v);
597                 }
598               active_elements++;
599             }
600         }
601     }
602
603   s = format (s, "    %lld active elements\n", active_elements);
604   s = format (s, "    %d free lists\n", vec_len (t->freelists));
605   return s;
606 }
607
608 int vnet_classify_add_del_table (vnet_classify_main_t * cm,
609                                  u8 * mask, 
610                                  u32 nbuckets,
611                                  u32 memory_size,
612                                  u32 skip,
613                                  u32 match,
614                                  u32 next_table_index,
615                                  u32 miss_next_index,
616                                  u32 * table_index,
617                                  int is_add)
618 {
619   vnet_classify_table_t * t;
620
621   if (is_add)
622     {
623       *table_index = ~0;
624       if (memory_size == 0)
625         return VNET_API_ERROR_INVALID_MEMORY_SIZE;
626
627       if (nbuckets == 0)
628         return VNET_API_ERROR_INVALID_VALUE;
629
630       t = vnet_classify_new_table (cm, mask, nbuckets, memory_size, 
631         skip, match);
632       t->next_table_index = next_table_index;
633       t->miss_next_index = miss_next_index;
634       *table_index = t - cm->tables;
635       return 0;
636     }
637   
638   vnet_classify_delete_table_index (cm, *table_index);
639   return 0;
640 }
641
642 #define foreach_ip4_proto_field                 \
643 _(src_address)                                  \
644 _(dst_address)                                  \
645 _(tos)                                          \
646 _(length)                                       \
647 _(fragment_id)                                  \
648 _(ttl)                                          \
649 _(protocol)                                     \
650 _(checksum)
651
652 uword unformat_ip4_mask (unformat_input_t * input, va_list * args)
653 {
654   u8 ** maskp = va_arg (*args, u8 **);
655   u8 * mask = 0;
656   u8 found_something = 0;
657   ip4_header_t * ip;
658   
659 #define _(a) u8 a=0;
660   foreach_ip4_proto_field;
661 #undef _
662   u8 version = 0;
663   u8 hdr_length = 0;
664   
665   
666   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
667     {
668       if (unformat (input, "version")) 
669         version = 1;
670       else if (unformat (input, "hdr_length"))
671         hdr_length = 1;
672       else if (unformat (input, "src"))
673         src_address = 1;
674       else if (unformat (input, "dst"))
675         dst_address = 1;
676       else if (unformat (input, "proto"))
677         protocol = 1;
678       
679 #define _(a) else if (unformat (input, #a)) a=1;
680       foreach_ip4_proto_field
681 #undef _
682       else
683         break;
684     }
685   
686 #define _(a) found_something += a;
687   foreach_ip4_proto_field;
688 #undef _
689   
690   if (found_something == 0)
691     return 0;
692   
693   vec_validate (mask, sizeof (*ip) - 1);
694   
695   ip = (ip4_header_t *) mask;
696   
697 #define _(a) if (a) memset (&ip->a, 0xff, sizeof (ip->a));
698   foreach_ip4_proto_field;
699 #undef _
700   
701   ip->ip_version_and_header_length = 0;
702   
703   if (version)
704     ip->ip_version_and_header_length |= 0xF0;
705   
706   if (hdr_length)
707     ip->ip_version_and_header_length |= 0x0F;
708   
709   *maskp = mask;
710   return 1;
711 }
712
713 #define foreach_ip6_proto_field                 \
714 _(src_address)                                  \
715 _(dst_address)                                  \
716 _(payload_length)                               \
717 _(hop_limit)                                    \
718 _(protocol)
719
720 uword unformat_ip6_mask (unformat_input_t * input, va_list * args)
721 {
722   u8 ** maskp = va_arg (*args, u8 **);
723   u8 * mask = 0;
724   u8 found_something = 0;
725   ip6_header_t * ip;
726   u32 ip_version_traffic_class_and_flow_label;
727   
728 #define _(a) u8 a=0;
729   foreach_ip6_proto_field;
730 #undef _
731   u8 version = 0;
732   u8 traffic_class = 0;
733   u8 flow_label = 0;
734   
735   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
736     {
737       if (unformat (input, "version")) 
738         version = 1;
739       else if (unformat (input, "traffic-class"))
740         traffic_class = 1;
741       else if (unformat (input, "flow-label"))
742         flow_label = 1;
743       else if (unformat (input, "src"))
744         src_address = 1;
745       else if (unformat (input, "dst"))
746         dst_address = 1;
747       else if (unformat (input, "proto"))
748         protocol = 1;
749       
750 #define _(a) else if (unformat (input, #a)) a=1;
751       foreach_ip6_proto_field
752 #undef _
753       else
754         break;
755     }
756   
757 #define _(a) found_something += a;
758   foreach_ip6_proto_field;
759 #undef _
760   
761   if (found_something == 0)
762     return 0;
763   
764   vec_validate (mask, sizeof (*ip) - 1);
765   
766   ip = (ip6_header_t *) mask;
767   
768 #define _(a) if (a) memset (&ip->a, 0xff, sizeof (ip->a));
769   foreach_ip6_proto_field;
770 #undef _
771   
772   ip_version_traffic_class_and_flow_label = 0;
773   
774   if (version)
775     ip_version_traffic_class_and_flow_label |= 0xF0000000;
776
777   if (traffic_class)
778     ip_version_traffic_class_and_flow_label |= 0x0FF00000;
779
780   if (flow_label)
781     ip_version_traffic_class_and_flow_label |= 0x000FFFFF;
782
783   ip->ip_version_traffic_class_and_flow_label = 
784     clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
785   
786   *maskp = mask;
787   return 1;
788 }
789
790 uword unformat_l3_mask (unformat_input_t * input, va_list * args)
791 {
792   u8 ** maskp = va_arg (*args, u8 **);
793
794   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
795     if (unformat (input, "ip4 %U", unformat_ip4_mask, maskp))
796       return 1;
797     else if (unformat (input, "ip6 %U", unformat_ip6_mask, maskp))
798       return 1;
799     else
800       break;
801   }
802   return 0;
803 }
804
805 uword unformat_l2_mask (unformat_input_t * input, va_list * args)
806 {
807   u8 ** maskp = va_arg (*args, u8 **);
808   u8 * mask = 0;
809   u8 src = 0;
810   u8 dst = 0;
811   u8 proto = 0;
812   u8 tag1 = 0;
813   u8 tag2 = 0;
814   u8 ignore_tag1 = 0;
815   u8 ignore_tag2 = 0;
816   u8 cos1 = 0;
817   u8 cos2 = 0;
818   u8 dot1q = 0;
819   u8 dot1ad = 0;
820   int len = 14;
821
822   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
823     if (unformat (input, "src"))
824       src = 1;
825     else if (unformat (input, "dst"))
826       dst = 1;
827     else if (unformat (input, "proto"))
828       proto = 1;
829     else if (unformat (input, "tag1"))
830       tag1 = 1;
831     else if (unformat (input, "tag2"))
832       tag2 = 1;
833     else if (unformat (input, "ignore-tag1"))
834       ignore_tag1 = 1;
835     else if (unformat (input, "ignore-tag2"))
836       ignore_tag2 = 1;
837     else if (unformat (input, "cos1"))
838       cos1 = 1;
839     else if (unformat (input, "cos2"))
840       cos2 = 1;
841     else if (unformat (input, "dot1q"))
842       dot1q = 1;
843     else if (unformat (input, "dot1ad"))
844       dot1ad = 1;
845     else
846       break;
847   }
848   if ((src + dst + proto + tag1 + tag2 + dot1q + dot1ad +
849       ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
850     return 0;
851
852   if (tag1 || ignore_tag1 || cos1 || dot1q)
853     len = 18;
854   if (tag2 || ignore_tag2 || cos2 || dot1ad)
855     len = 22;
856
857   vec_validate (mask, len-1);
858
859   if (dst)
860     memset (mask, 0xff, 6);
861
862   if (src)
863     memset (mask + 6, 0xff, 6);
864   
865   if (tag2 || dot1ad)
866     {
867       /* inner vlan tag */
868       if (tag2)
869         {
870           mask[19] = 0xff;
871           mask[18] = 0x0f;
872         }
873       if (cos2)
874         mask[18] |= 0xe0;
875       if (proto)
876         mask[21] = mask [20] = 0xff;
877       if (tag1)
878         {
879           mask [15] = 0xff;
880           mask [14] = 0x0f;
881         }
882       if (cos1)
883         mask[14] |= 0xe0;
884       *maskp = mask;
885       return 1;
886     }
887   if (tag1 | dot1q)
888     {
889       if (tag1)
890         {
891           mask [15] = 0xff;
892           mask [14] = 0x0f;
893         }
894       if (cos1)
895         mask[14] |= 0xe0;
896       if (proto)
897         mask[16] = mask [17] = 0xff;
898       *maskp = mask;
899       return 1;
900     }
901   if (cos2)
902     mask[18] |= 0xe0;
903   if (cos1)
904     mask[14] |= 0xe0;
905   if (proto)
906     mask[12] = mask [13] = 0xff;
907     
908   *maskp = mask;
909   return 1;
910 }
911
912 uword unformat_classify_mask (unformat_input_t * input, va_list * args)
913 {
914   vnet_classify_main_t * CLIB_UNUSED(cm) 
915     = va_arg (*args, vnet_classify_main_t *);
916   u8 ** maskp = va_arg (*args, u8 **);
917   u32 * skipp = va_arg (*args, u32 *);
918   u32 * matchp = va_arg (*args, u32 *);
919   u32 match;
920   u8 * mask = 0;
921   u8 * l2 = 0;
922   u8 * l3 = 0;
923   int i;
924   
925   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
926     if (unformat (input, "hex %U", unformat_hex_string, &mask))
927       ;
928     else if (unformat (input, "l2 %U", unformat_l2_mask, &l2))
929       ;
930     else if (unformat (input, "l3 %U", unformat_l3_mask, &l3))
931       ;
932     else
933       break;
934   }
935
936   if (mask || l2 || l3)
937     {
938       if (l2 || l3)
939         {
940           /* "With a free Ethernet header in every package" */
941           if (l2 == 0)
942             vec_validate (l2, 13);
943           mask = l2;
944           vec_append (mask, l3);
945           vec_free (l3);
946         }
947
948       /* Scan forward looking for the first significant mask octet */
949       for (i = 0; i < vec_len (mask); i++)
950         if (mask[i])
951           break;
952
953       /* compute (skip, match) params */
954       *skipp = i / sizeof(u32x4);
955       vec_delete (mask, *skipp * sizeof(u32x4), 0);
956
957       /* Pad mask to an even multiple of the vector size */
958       while (vec_len (mask) % sizeof (u32x4))
959         vec_add1 (mask, 0);
960
961       match = vec_len (mask) / sizeof (u32x4);
962
963       for (i = match*sizeof(u32x4); i > 0; i-= sizeof(u32x4))
964         {
965           u64 *tmp = (u64 *)(mask + (i-sizeof(u32x4)));
966           if (*tmp || *(tmp+1))
967             break;
968           match--;
969         }
970       if (match == 0)
971         clib_warning ("BUG: match 0");
972
973       _vec_len (mask) = match * sizeof(u32x4);
974
975       *matchp = match;
976       *maskp = mask;
977
978       return 1;
979     }
980
981   return 0;
982 }
983
984 #define foreach_l2_next                         \
985 _(drop, DROP)                                   \
986 _(ethernet, ETHERNET_INPUT)                     \
987 _(ip4, IP4_INPUT)                               \
988 _(ip6, IP6_INPUT)                               \
989 _(li, LI)
990
991 uword unformat_l2_next_index (unformat_input_t * input, va_list * args)
992 {
993   u32 * miss_next_indexp = va_arg (*args, u32 *);
994   u32 next_index = 0;
995   u32 tmp;
996   
997 #define _(n,N) \
998   if (unformat (input, #n)) { next_index = L2_CLASSIFY_NEXT_##N; goto out;}
999   foreach_l2_next;
1000 #undef _
1001   
1002   if (unformat (input, "%d", &tmp))
1003     { 
1004       next_index = tmp; 
1005       goto out; 
1006     }
1007   
1008   return 0;
1009
1010  out:
1011   *miss_next_indexp = next_index;
1012   return 1;
1013 }
1014
1015 #define foreach_ip_next                         \
1016 _(miss, MISS)                                   \
1017 _(drop, DROP)                                   \
1018 _(local, LOCAL)                                 \
1019 _(rewrite, REWRITE)
1020
1021 uword unformat_ip_next_index (unformat_input_t * input, va_list * args)
1022 {
1023   u32 * miss_next_indexp = va_arg (*args, u32 *);
1024   u32 next_index = 0;
1025   u32 tmp;
1026   
1027 #define _(n,N) \
1028   if (unformat (input, #n)) { next_index = IP_LOOKUP_NEXT_##N; goto out;}
1029   foreach_ip_next;
1030 #undef _
1031   
1032   if (unformat (input, "%d", &tmp))
1033     { 
1034       next_index = tmp; 
1035       goto out; 
1036     }
1037   
1038   return 0;
1039
1040  out:
1041   *miss_next_indexp = next_index;
1042   return 1;
1043 }
1044
1045 #define foreach_acl_next                        \
1046 _(deny, DENY)
1047
1048 uword unformat_acl_next_index (unformat_input_t * input, va_list * args)
1049 {
1050   u32 * miss_next_indexp = va_arg (*args, u32 *);
1051   u32 next_index = 0;
1052   u32 tmp;
1053
1054 #define _(n,N) \
1055   if (unformat (input, #n)) { next_index = ACL_NEXT_INDEX_##N; goto out;}
1056   foreach_acl_next;
1057 #undef _
1058
1059   if (unformat (input, "permit"))
1060     {
1061       next_index = ~0;
1062       goto out;
1063     }
1064   else if (unformat (input, "%d", &tmp))
1065     {
1066       next_index = tmp;
1067       goto out;
1068     }
1069
1070   return 0;
1071
1072  out:
1073   *miss_next_indexp = next_index;
1074   return 1;
1075 }
1076
1077 static clib_error_t *
1078 classify_table_command_fn (vlib_main_t * vm,
1079                            unformat_input_t * input,
1080                            vlib_cli_command_t * cmd)
1081 {
1082   u32 nbuckets = 2;
1083   u32 skip = ~0;
1084   u32 match = ~0;
1085   int is_add = 1;
1086   u32 table_index = ~0;
1087   u32 next_table_index = ~0;
1088   u32 miss_next_index = ~0;
1089   u32 memory_size = 2<<20;
1090   u32 tmp;
1091
1092   u8 * mask = 0;
1093   vnet_classify_main_t * cm = &vnet_classify_main;
1094   int rv;
1095
1096   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1097     if (unformat (input, "del"))
1098       is_add = 0;
1099     else if (unformat (input, "buckets %d", &nbuckets))
1100       ;
1101     else if (unformat (input, "skip %d", &skip))
1102       ;
1103     else if (unformat (input, "match %d", &match))
1104       ;
1105     else if (unformat (input, "table %d", &table_index))
1106       ;
1107     else if (unformat (input, "mask %U", unformat_classify_mask, 
1108                        cm, &mask, &skip, &match))
1109       ;
1110     else if (unformat (input, "memory-size %uM", &tmp))
1111       memory_size = tmp<<20;
1112     else if (unformat (input, "memory-size %uG", &tmp))
1113       memory_size = tmp<<30;
1114     else if (unformat (input, "next-table %d", &next_table_index))
1115       ;
1116     else if (unformat (input, "miss-next %U", unformat_ip_next_index,
1117                        &miss_next_index))
1118       ;
1119     else if (unformat (input, "l2-miss-next %U", unformat_l2_next_index,
1120                        &miss_next_index))
1121       ;
1122     else if (unformat (input, "acl-miss-next %U", unformat_acl_next_index,
1123                        &miss_next_index))
1124       ;
1125                        
1126     else
1127       break;
1128   }
1129   
1130   if (is_add && mask == 0)
1131     return clib_error_return (0, "Mask required");
1132
1133   if (is_add && skip == ~0)
1134     return clib_error_return (0, "skip count required");
1135
1136   if (is_add && match == ~0)
1137     return clib_error_return (0, "match count required");
1138       
1139   if (!is_add && table_index == ~0)
1140     return clib_error_return (0, "table index required for delete");
1141
1142   rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size,
1143         skip, match, next_table_index, miss_next_index,
1144         &table_index, is_add);
1145   switch (rv)
1146     {
1147     case 0:
1148       break;
1149
1150     default:
1151       return clib_error_return (0, "vnet_classify_add_del_table returned %d",
1152                                 rv);
1153     }
1154   return 0;
1155 }
1156
1157 VLIB_CLI_COMMAND (classify_table, static) = {
1158   .path = "classify table",
1159   .short_help = 
1160   "classify table [miss-next|l2-miss_next|acl-miss-next <next_index>]"
1161   "\n mask <mask-value> buckets <nn> [skip <n>] [match <n>] [del]",
1162   .function = classify_table_command_fn,
1163 };
1164
1165 static u8 * format_vnet_classify_table (u8 * s, va_list * args)
1166 {
1167   vnet_classify_main_t * cm = va_arg (*args, vnet_classify_main_t *);
1168   int verbose = va_arg (*args, int);
1169   u32 index = va_arg (*args, u32);
1170   vnet_classify_table_t * t;
1171
1172   if (index == ~0)
1173     {
1174       s = format (s, "%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl",
1175                   "NextNode", verbose ? "Details" : "");
1176       return s;
1177     }
1178
1179   t = pool_elt_at_index (cm->tables, index);
1180   s = format (s, "%10u%10d%10d%10d", index, t->active_elements,
1181               t->next_table_index, t->miss_next_index);
1182
1183   s = format (s, "\n  Heap: %U", format_mheap, t->mheap, 0 /*verbose*/); 
1184
1185   s = format (s, "\n  nbuckets %d, skip %d match %d", 
1186               t->nbuckets, t->skip_n_vectors, t->match_n_vectors);
1187   s = format (s, "\n  mask %U", format_hex_bytes, t->mask, 
1188               t->match_n_vectors * sizeof (u32x4));
1189
1190   if (verbose == 0)
1191     return s;
1192
1193   s = format (s, "\n%U", format_classify_table, t, verbose);
1194   
1195   return s;
1196 }
1197
1198 static clib_error_t *
1199 show_classify_tables_command_fn (vlib_main_t * vm,
1200                                  unformat_input_t * input,
1201                                  vlib_cli_command_t * cmd)
1202 {
1203   vnet_classify_main_t * cm = &vnet_classify_main;
1204   vnet_classify_table_t * t;
1205   u32 match_index = ~0;
1206   u32 * indices = 0;
1207   int verbose = 0;
1208   int i;
1209
1210   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
1211     {
1212       if (unformat (input, "index %d", &match_index))
1213         ;
1214       else if (unformat (input, "verbose %d", &verbose))
1215         ;
1216       else if (unformat (input, "verbose"))
1217         verbose = 1;
1218       else 
1219         break;
1220     }
1221   
1222   pool_foreach (t, cm->tables, 
1223   ({
1224     if (match_index == ~0 || (match_index == t - cm->tables))
1225       vec_add1 (indices, t - cm->tables);
1226   }));
1227
1228   if (vec_len(indices))
1229     {
1230       vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose,
1231                        ~0 /* hdr */);
1232       for (i = 0; i < vec_len (indices); i++)
1233         vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, 
1234                          verbose, indices[i]);
1235     }
1236   else
1237     vlib_cli_output (vm, "No classifier tables configured");
1238
1239   vec_free (indices);
1240
1241   return 0;
1242 }
1243
1244 VLIB_CLI_COMMAND (show_classify_table_command, static) = {
1245   .path = "show classify tables",
1246   .short_help = "show classify tables [index <nn>]",
1247   .function = show_classify_tables_command_fn,
1248 };
1249
1250 uword unformat_ip4_match (unformat_input_t * input, va_list * args)
1251 {
1252   u8 ** matchp = va_arg (*args, u8 **);
1253   u8 * match = 0;
1254   ip4_header_t * ip;
1255   int version = 0;
1256   u32 version_val;
1257   int hdr_length = 0;
1258   u32 hdr_length_val;
1259   int src = 0, dst = 0;
1260   ip4_address_t src_val, dst_val;
1261   int proto = 0;
1262   u32 proto_val;
1263   int tos = 0;
1264   u32 tos_val;
1265   int length = 0;
1266   u32 length_val;
1267   int fragment_id = 0;
1268   u32 fragment_id_val;
1269   int ttl = 0;
1270   int ttl_val;
1271   int checksum = 0;
1272   u32 checksum_val;
1273
1274   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
1275     {
1276       if (unformat (input, "version %d", &version_val)) 
1277         version = 1;
1278       else if (unformat (input, "hdr_length %d", &hdr_length_val))
1279         hdr_length = 1;
1280       else if (unformat (input, "src %U", unformat_ip4_address, &src_val))
1281         src = 1;
1282       else if (unformat (input, "dst %U", unformat_ip4_address, &dst_val))
1283         dst = 1;
1284       else if (unformat (input, "proto %d", &proto_val))
1285         proto = 1;
1286       else if (unformat (input, "tos %d", &tos_val))
1287         tos = 1;
1288       else if (unformat (input, "length %d", &length_val))
1289         length = 1;
1290       else if (unformat (input, "fragment_id %d", &fragment_id_val))
1291         fragment_id = 1;
1292       else if (unformat (input, "ttl %d", &ttl_val))
1293         ttl = 1;
1294       else if (unformat (input, "checksum %d", &checksum_val))
1295         checksum = 1;
1296       else
1297         break;
1298     }
1299   
1300   if (version + hdr_length + src + dst + proto + tos + length + fragment_id
1301       + ttl + checksum == 0)
1302     return 0;
1303
1304   /* 
1305    * Aligned because we use the real comparison functions
1306    */
1307   vec_validate_aligned (match, sizeof (*ip) - 1, sizeof(u32x4));
1308   
1309   ip = (ip4_header_t *) match;
1310   
1311   /* These are realistically matched in practice */
1312   if (src)
1313     ip->src_address.as_u32 = src_val.as_u32;
1314
1315   if (dst)
1316     ip->dst_address.as_u32 = dst_val.as_u32;
1317   
1318   if (proto)
1319     ip->protocol = proto_val;
1320     
1321
1322   /* These are not, but they're included for completeness */
1323   if (version)
1324     ip->ip_version_and_header_length |= (version_val & 0xF)<<4;
1325
1326   if (hdr_length)
1327     ip->ip_version_and_header_length |= (hdr_length_val & 0xF);
1328     
1329   if (tos)
1330     ip->tos = tos_val;
1331   
1332   if (length)
1333     ip->length = length_val;
1334   
1335   if (ttl)
1336     ip->ttl = ttl_val;
1337
1338   if (checksum)
1339     ip->checksum = checksum_val;
1340
1341   *matchp = match;
1342   return 1;
1343 }
1344
1345 uword unformat_ip6_match (unformat_input_t * input, va_list * args)
1346 {
1347   u8 ** matchp = va_arg (*args, u8 **);
1348   u8 * match = 0;
1349   ip6_header_t * ip;
1350   int version = 0;
1351   u32 version_val;
1352   u8  traffic_class = 0;
1353   u32 traffic_class_val;
1354   u8  flow_label = 0;
1355   u8  flow_label_val;
1356   int src = 0, dst = 0;
1357   ip6_address_t src_val, dst_val;
1358   int proto = 0;
1359   u32 proto_val;
1360   int payload_length = 0;
1361   u32 payload_length_val;
1362   int hop_limit = 0;
1363   int hop_limit_val;
1364   u32 ip_version_traffic_class_and_flow_label;
1365
1366   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
1367     {
1368       if (unformat (input, "version %d", &version_val)) 
1369         version = 1;
1370       else if (unformat (input, "traffic_class %d", &traffic_class_val))
1371         traffic_class = 1;
1372       else if (unformat (input, "flow_label %d", &flow_label_val))
1373         flow_label = 1;
1374       else if (unformat (input, "src %U", unformat_ip6_address, &src_val))
1375         src = 1;
1376       else if (unformat (input, "dst %U", unformat_ip6_address, &dst_val))
1377         dst = 1;
1378       else if (unformat (input, "proto %d", &proto_val))
1379         proto = 1;
1380       else if (unformat (input, "payload_length %d", &payload_length_val))
1381         payload_length = 1;
1382       else if (unformat (input, "hop_limit %d", &hop_limit_val))
1383         hop_limit = 1;
1384       else
1385         break;
1386     }
1387   
1388   if (version + traffic_class + flow_label + src + dst + proto +
1389       payload_length + hop_limit == 0)
1390     return 0;
1391
1392   /* 
1393    * Aligned because we use the real comparison functions
1394    */
1395   vec_validate_aligned (match, sizeof (*ip) - 1, sizeof(u32x4));
1396   
1397   ip = (ip6_header_t *) match;
1398   
1399   if (src)
1400     memcpy (&ip->src_address, &src_val, sizeof (ip->src_address));
1401
1402   if (dst)
1403     memcpy (&ip->dst_address, &dst_val, sizeof (ip->dst_address));
1404   
1405   if (proto)
1406     ip->protocol = proto_val;
1407     
1408   ip_version_traffic_class_and_flow_label = 0;
1409
1410   if (version)
1411     ip_version_traffic_class_and_flow_label |= (version_val & 0xF) << 28;
1412
1413   if (traffic_class)
1414     ip_version_traffic_class_and_flow_label |= (traffic_class_val & 0xFF) << 20;
1415
1416   if (flow_label)
1417     ip_version_traffic_class_and_flow_label |= (flow_label_val & 0xFFFFF);
1418     
1419   ip->ip_version_traffic_class_and_flow_label = 
1420     clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
1421
1422   if (payload_length)
1423     ip->payload_length = clib_host_to_net_u16 (payload_length_val);
1424   
1425   if (hop_limit)
1426     ip->hop_limit = hop_limit_val;
1427
1428   *matchp = match;
1429   return 1;
1430 }
1431
1432 uword unformat_l3_match (unformat_input_t * input, va_list * args)
1433 {
1434   u8 ** matchp = va_arg (*args, u8 **);
1435
1436   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1437     if (unformat (input, "ip4 %U", unformat_ip4_match, matchp))
1438       return 1;
1439     else if (unformat (input, "ip6 %U", unformat_ip6_match, matchp))
1440       return 1;
1441     /* $$$$ add mpls */
1442     else
1443       break;
1444   }
1445   return 0;
1446 }
1447
1448 uword unformat_vlan_tag (unformat_input_t * input, va_list * args)
1449 {
1450   u8 * tagp = va_arg (*args, u8 *);
1451   u32 tag;
1452
1453   if (unformat(input, "%d", &tag))
1454     {
1455       tagp[0] = (tag>>8) & 0x0F;
1456       tagp[1] = tag & 0xFF;
1457       return 1;
1458     }
1459
1460   return 0;
1461 }
1462
1463 uword unformat_l2_match (unformat_input_t * input, va_list * args)
1464 {
1465   u8 ** matchp = va_arg (*args, u8 **);
1466   u8 * match = 0;
1467   u8 src = 0;
1468   u8 src_val[6];
1469   u8 dst = 0;
1470   u8 dst_val[6];
1471   u8 proto = 0;
1472   u16 proto_val;
1473   u8 tag1 = 0;
1474   u8 tag1_val [2];
1475   u8 tag2 = 0;
1476   u8 tag2_val [2];
1477   int len = 14;
1478   u8 ignore_tag1 = 0;
1479   u8 ignore_tag2 = 0;
1480   u8 cos1 = 0;
1481   u8 cos2 = 0;
1482   u32 cos1_val = 0;
1483   u32 cos2_val = 0;
1484
1485   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1486     if (unformat (input, "src %U", unformat_ethernet_address, &src_val))
1487       src = 1;
1488     else if (unformat (input, "dst %U", unformat_ethernet_address, &dst_val))
1489       dst = 1;
1490     else if (unformat (input, "proto %U", 
1491                        unformat_ethernet_type_host_byte_order, &proto_val))
1492       proto = 1;
1493     else if (unformat (input, "tag1 %U", unformat_vlan_tag, tag1_val))
1494       tag1 = 1;
1495     else if (unformat (input, "tag2 %U", unformat_vlan_tag, tag2_val))
1496       tag2 = 1;
1497     else if (unformat (input, "ignore-tag1"))
1498       ignore_tag1 = 1;
1499     else if (unformat (input, "ignore-tag2"))
1500       ignore_tag2 = 1;
1501     else if (unformat (input, "cos1 %d", &cos1_val))
1502       cos1 = 1;
1503     else if (unformat (input, "cos2 %d", &cos2_val))
1504       cos2 = 1;
1505     else
1506       break;
1507   }
1508   if ((src + dst + proto + tag1 + tag2 +
1509       ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
1510     return 0;
1511
1512   if (tag1 || ignore_tag1 || cos1)
1513     len = 18;
1514   if (tag2 || ignore_tag2 || cos2)
1515     len = 22;
1516
1517   vec_validate_aligned (match, len-1, sizeof(u32x4));
1518
1519   if (dst)
1520     memcpy (match, dst_val, 6);
1521
1522   if (src)
1523     memcpy (match + 6, src_val, 6);
1524   
1525   if (tag2)
1526     {
1527       /* inner vlan tag */
1528       match[19] = tag2_val[1];
1529       match[18] = tag2_val[0];
1530       if (cos2)
1531         match [18] |= (cos2_val & 0x7) << 5;
1532       if (proto)
1533         {
1534           match[21] = proto_val & 0xff;
1535           match[20] = proto_val >> 8;
1536         }
1537       if (tag1)
1538         {
1539           match [15] = tag1_val[1];
1540           match [14] = tag1_val[0];
1541         }
1542       if (cos1)
1543         match [14] |= (cos1_val & 0x7) << 5;
1544       *matchp = match;
1545       return 1;
1546     }
1547   if (tag1)
1548     {
1549       match [15] = tag1_val[1];
1550       match [14] = tag1_val[0];
1551       if (proto)
1552         {
1553           match[17] = proto_val & 0xff;
1554           match[16] = proto_val >> 8;
1555         }
1556       if (cos1)
1557         match [14] |= (cos1_val & 0x7) << 5;
1558
1559       *matchp = match;
1560       return 1;
1561     }
1562   if (cos2)
1563     match [18] |= (cos2_val & 0x7) << 5;
1564   if (cos1)
1565     match [14] |= (cos1_val & 0x7) << 5;
1566   if (proto)
1567     {
1568       match[13] = proto_val & 0xff;
1569       match[12] = proto_val >> 8;
1570     }
1571   
1572   *matchp = match;
1573   return 1;
1574 }
1575
1576
1577 uword unformat_classify_match (unformat_input_t * input, va_list * args)
1578 {
1579   vnet_classify_main_t * cm = va_arg (*args, vnet_classify_main_t *);
1580   u8 ** matchp = va_arg (*args, u8 **);
1581   u32 table_index = va_arg (*args, u32);
1582   vnet_classify_table_t * t;
1583   
1584   u8 * match = 0;
1585   u8 * l2 = 0;
1586   u8 * l3 = 0;
1587
1588   if (pool_is_free_index (cm->tables, table_index))
1589     return 0;
1590
1591   t = pool_elt_at_index (cm->tables, table_index);
1592
1593   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1594     if (unformat (input, "hex %U", unformat_hex_string, &match))
1595       ;
1596     else if (unformat (input, "l2 %U", unformat_l2_match, &l2))
1597       ;
1598     else if (unformat (input, "l3 %U", unformat_l3_match, &l3))
1599       ;
1600     else
1601       break;
1602   }
1603
1604   if (match || l2 || l3)
1605     {
1606       if (l2 || l3)
1607         {
1608           /* "Win a free Ethernet header in every packet" */
1609           if (l2 == 0)
1610             vec_validate_aligned (l2, 13, sizeof(u32x4));
1611           match = l2;
1612           vec_append_aligned (match, l3, sizeof(u32x4));
1613           vec_free (l3);
1614         }
1615
1616       /* Make sure the vector is big enough even if key is all 0's */
1617       vec_validate_aligned 
1618         (match, ((t->match_n_vectors + t->skip_n_vectors) * sizeof(u32x4)) - 1,
1619          sizeof(u32x4));
1620       
1621       /* Set size, include skipped vectors*/
1622       _vec_len (match) = (t->match_n_vectors+t->skip_n_vectors) * sizeof(u32x4);
1623
1624       *matchp = match;
1625
1626       return 1;
1627     }
1628
1629   return 0;
1630 }
1631
1632 int vnet_classify_add_del_session (vnet_classify_main_t * cm, 
1633                                    u32 table_index, 
1634                                    u8 * match, 
1635                                    u32 hit_next_index,
1636                                    u32 opaque_index, 
1637                                    i32 advance,
1638                                    int is_add)
1639 {
1640   vnet_classify_table_t * t;
1641   vnet_classify_entry_5_t _max_e __attribute__((aligned (16)));
1642   vnet_classify_entry_t * e;
1643   int i, rv;
1644
1645   if (pool_is_free_index (cm->tables, table_index))
1646     return VNET_API_ERROR_NO_SUCH_TABLE;
1647   
1648   t = pool_elt_at_index (cm->tables, table_index);
1649   
1650   e = (vnet_classify_entry_t *)&_max_e;
1651   e->next_index = hit_next_index;
1652   e->opaque_index = opaque_index;
1653   e->advance = advance;
1654   e->hits = 0;
1655   e->last_heard = 0;
1656   e->flags = 0;
1657
1658   /* Copy key data, honoring skip_n_vectors */
1659   memcpy (&e->key, match + t->skip_n_vectors * sizeof (u32x4),
1660           t->match_n_vectors * sizeof (u32x4));
1661
1662   /* Clear don't-care bits; likely when dynamically creating sessions */
1663   for (i = 0; i < t->match_n_vectors; i++)
1664     e->key[i] &= t->mask[i];
1665
1666   rv = vnet_classify_add_del (t, e, is_add);
1667   if (rv)
1668     return VNET_API_ERROR_NO_SUCH_ENTRY;
1669   return 0;
1670 }
1671
1672 static clib_error_t *
1673 classify_session_command_fn (vlib_main_t * vm,
1674                              unformat_input_t * input,
1675                              vlib_cli_command_t * cmd)
1676 {
1677   vnet_classify_main_t * cm = &vnet_classify_main;
1678   int is_add = 1;
1679   u32 table_index = ~0;
1680   u32 hit_next_index = ~0;
1681   u32 opaque_index = ~0;
1682   u8 * match = 0;
1683   i32 advance = 0;
1684   int rv;
1685
1686   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) 
1687     {
1688       if (unformat (input, "del"))
1689         is_add = 0;
1690       else if (unformat (input, "hit-next %U", unformat_ip_next_index,
1691                          &hit_next_index))
1692         ;
1693       else if (unformat (input, "l2-hit-next %U", unformat_l2_next_index,
1694                          &hit_next_index))
1695         ;
1696       else if (unformat (input, "acl-hit-next %U", unformat_acl_next_index,
1697                          &hit_next_index))
1698         ;
1699       else if (unformat (input, "opaque-index %d", &opaque_index))
1700         ;
1701       else if (unformat (input, "match %U", unformat_classify_match,
1702                          cm, &match, table_index))
1703         ;
1704       else if (unformat (input, "advance %d", &advance))
1705         ;
1706       else if (unformat (input, "table-index %d", &table_index))
1707         ;
1708       else
1709         break;
1710     }
1711
1712   if (table_index == ~0)
1713     return clib_error_return (0, "Table index required");
1714
1715   if (is_add && match == 0)
1716     return clib_error_return (0, "Match value required");
1717
1718   rv = vnet_classify_add_del_session (cm, table_index, match, 
1719                                       hit_next_index,
1720                                       opaque_index, advance, is_add);
1721
1722   switch(rv)
1723     {
1724     case 0:
1725       break;
1726
1727     default:
1728       return clib_error_return (0, "vnet_classify_add_del_session returned %d",
1729                                 rv);
1730     }
1731
1732   return 0;
1733 }
1734
1735 VLIB_CLI_COMMAND (classify_session_command, static) = {
1736     .path = "classify session",
1737     .short_help = 
1738     "classify session [hit-next|l2-hit-next|acl-hit-next <next_index>]"
1739     "\n table-index <nn> match [hex] [l2] [l3 ip4] [opaque-index <index>]",
1740     .function = classify_session_command_fn,
1741 };
1742
1743 #define TEST_CODE 1
1744
1745 #if TEST_CODE > 0
1746 static clib_error_t *
1747 test_classify_command_fn (vlib_main_t * vm,
1748                  unformat_input_t * input,
1749                  vlib_cli_command_t * cmd)
1750 {
1751   u32 buckets = 2;
1752   u32 sessions = 10;
1753   int i, rv;
1754   vnet_classify_table_t * t = 0;
1755   classify_data_or_mask_t * mask;
1756   classify_data_or_mask_t * data;
1757   u8 *mp = 0, *dp = 0;
1758   vnet_classify_main_t * cm = &vnet_classify_main;
1759   vnet_classify_entry_t * e;
1760   int is_add = 1;
1761   u32 tmp;
1762   u32 table_index = ~0;
1763   ip4_address_t src;
1764   u32 deleted = 0;
1765   u32 memory_size = 64<<20;
1766
1767   /* Default starting address 1.0.0.10 */
1768   src.as_u32 = clib_net_to_host_u32 (0x0100000A);
1769
1770   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) {
1771     if (unformat (input, "sessions %d", &sessions))
1772       ;
1773     else if (unformat (input, "src %U", unformat_ip4_address, &src))
1774       ;
1775     else if (unformat (input, "buckets %d", &buckets))
1776       ;
1777     else if (unformat (input, "memory-size %uM", &tmp))
1778       memory_size = tmp<<20;
1779     else if (unformat (input, "memory-size %uG", &tmp))
1780       memory_size = tmp<<30;
1781     else if (unformat (input, "del"))
1782       is_add = 0;
1783     else if (unformat (input, "table %d", &table_index))
1784       ;
1785     else
1786       break;
1787     }
1788
1789   vec_validate_aligned (mp, 3 * sizeof(u32x4), sizeof(u32x4));
1790   vec_validate_aligned (dp, 3 * sizeof(u32x4), sizeof(u32x4));
1791
1792   mask = (classify_data_or_mask_t *) mp;
1793   data = (classify_data_or_mask_t *) dp;
1794
1795   data->ip.src_address.as_u32 = src.as_u32;
1796
1797   /* Mask on src address */
1798   memset (&mask->ip.src_address, 0xff, 4);
1799
1800   buckets = 1<<max_log2(buckets);
1801
1802   if (table_index != ~0)
1803     {
1804       if (pool_is_free_index (cm->tables, table_index))
1805         {
1806           vlib_cli_output (vm, "No such table %d", table_index);
1807           goto out;
1808         }
1809       t = pool_elt_at_index (cm->tables, table_index);
1810     }
1811
1812   if (is_add)
1813     {
1814       if (t == 0)
1815         {
1816           t = vnet_classify_new_table (cm, (u8 *)mask, buckets, 
1817                                        memory_size,
1818                                        0 /* skip */,
1819                                        3 /* vectors to match */);
1820           t->miss_next_index = IP_LOOKUP_NEXT_LOCAL;
1821           vlib_cli_output (vm, "Create table %d", t - cm->tables);
1822         }
1823       
1824       vlib_cli_output (vm, "Add %d sessions to %d buckets...", 
1825                        sessions, buckets);
1826       
1827       for (i = 0; i < sessions; i++)
1828         {
1829           rv = vnet_classify_add_del_session (cm, t - cm->tables, (u8 *) data,
1830                                               IP_LOOKUP_NEXT_DROP,
1831                                               i+100 /* opaque_index */, 
1832                                               0 /* advance */, 
1833                                               1 /* is_add */);
1834
1835           if (rv != 0)
1836             clib_warning ("add: returned %d", rv);
1837           
1838           tmp = clib_net_to_host_u32 (data->ip.src_address.as_u32) + 1;
1839           data->ip.src_address.as_u32 = clib_net_to_host_u32 (tmp);
1840         }
1841       goto out;
1842     }
1843
1844   if (t == 0)
1845     {
1846       vlib_cli_output (vm, "Must specify table index to delete sessions");
1847       goto out;
1848     }
1849
1850   vlib_cli_output (vm, "Try to delete %d sessions...", sessions);
1851
1852   for (i = 0; i < sessions; i++)
1853     {
1854       u8 * key_minus_skip;
1855       u64 hash;
1856
1857       hash = vnet_classify_hash_packet (t, (u8 *) data);
1858
1859       e = vnet_classify_find_entry (t, (u8 *) data, hash, 0 /* time_now */);
1860       /* Previous delete, perhaps... */
1861       if (e == 0)
1862         continue;
1863       ASSERT (e->opaque_index == (i+100));
1864
1865       key_minus_skip = (u8 *)e->key;
1866       key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
1867
1868       rv = vnet_classify_add_del_session (cm, t - cm->tables, key_minus_skip,
1869                                           IP_LOOKUP_NEXT_DROP,
1870                                           i+100 /* opaque_index */, 
1871                                           0 /* advance */, 
1872                                           0 /* is_add */);
1873       if (rv != 0)
1874         clib_warning ("del: returned %d", rv);
1875       
1876       tmp = clib_net_to_host_u32 (data->ip.src_address.as_u32) + 1;
1877       data->ip.src_address.as_u32 = clib_net_to_host_u32 (tmp);
1878       deleted++;
1879     }
1880
1881   vlib_cli_output (vm, "Deleted %d sessions...", deleted);
1882
1883  out:
1884   vec_free (mp);
1885   vec_free (dp);
1886
1887   return 0;
1888 }
1889
1890 VLIB_CLI_COMMAND (test_classify_command, static) = {
1891     .path = "test classify",
1892     .short_help = 
1893     "test classify [src <ip>] [sessions <nn>] [buckets <nn>] [table <nn>] [del]",
1894     .function = test_classify_command_fn,
1895 };
1896 #endif /* TEST_CODE */