FD.io VPP  v19.04.4-rc0-5-ge88582fac
Vector Packet Processing
vnet_classify.c
Go to the documentation of this file.
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  */
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_INPUT_CLASSIFY_NEXT_xxx */
20 #include <vnet/fib/fib_table.h>
21 
23 
24 #if VALIDATION_SCAFFOLDING
25 /* Validation scaffolding */
26 void
28 {
29  void *oldheap;
30 
31  oldheap = clib_mem_set_heap (t->mheap);
33  clib_mem_set_heap (oldheap);
34 }
35 
36 void
38 {
39  int i, j, k;
40  vnet_classify_entry_t *v, *save_v;
41  u32 active_elements = 0;
43 
44  for (i = 0; i < t->nbuckets; i++)
45  {
46  b = &t->buckets[i];
47  if (b->offset == 0)
48  continue;
49  save_v = vnet_classify_get_entry (t, b->offset);
50  for (j = 0; j < (1 << b->log2_pages); j++)
51  {
52  for (k = 0; k < t->entries_per_page; k++)
53  {
55  (t, save_v, j * t->entries_per_page + k);
56 
58  active_elements++;
59  }
60  }
61  }
62 
63  if (active_elements != t->active_elements)
64  clib_warning ("found %u expected %u elts", active_elements,
65  t->active_elements);
66 }
67 #else
68 void
70 {
71 }
72 
73 void
75 {
76 }
77 #endif
78 
79 void
81 {
83 
84  vec_add1 (cm->unformat_l2_next_index_fns, fn);
85 }
86 
87 void
89 {
91 
92  vec_add1 (cm->unformat_ip_next_index_fns, fn);
93 }
94 
95 void
97 {
99 
100  vec_add1 (cm->unformat_acl_next_index_fns, fn);
101 }
102 
103 void
105  fn)
106 {
108 
109  vec_add1 (cm->unformat_policer_next_index_fns, fn);
110 }
111 
112 void
114 {
116 
117  vec_add1 (cm->unformat_opaque_index_fns, fn);
118 }
119 
122  u8 * mask, u32 nbuckets, u32 memory_size,
123  u32 skip_n_vectors, u32 match_n_vectors)
124 {
126  void *oldheap;
127 
128  nbuckets = 1 << (max_log2 (nbuckets));
129 
130  pool_get_aligned (cm->tables, t, CLIB_CACHE_LINE_BYTES);
131  clib_memset (t, 0, sizeof (*t));
132 
133  vec_validate_aligned (t->mask, match_n_vectors - 1, sizeof (u32x4));
134  clib_memcpy_fast (t->mask, mask, match_n_vectors * sizeof (u32x4));
135 
136  t->next_table_index = ~0;
137  t->nbuckets = nbuckets;
138  t->log2_nbuckets = max_log2 (nbuckets);
139  t->match_n_vectors = match_n_vectors;
140  t->skip_n_vectors = skip_n_vectors;
141  t->entries_per_page = 2;
142 
143 #if USE_DLMALLOC == 0
144  t->mheap = mheap_alloc (0 /* use VM */ , memory_size);
145 #else
146  t->mheap = create_mspace (memory_size, 1 /* locked */ );
147  /* classifier requires the memory to be contiguous, so can not expand. */
149 #endif
150 
152  oldheap = clib_mem_set_heap (t->mheap);
153 
156  t->writer_lock[0] = 0;
157 
158  clib_mem_set_heap (oldheap);
159  return (t);
160 }
161 
162 void
164  u32 table_index, int del_chain)
165 {
167 
168  /* Tolerate multiple frees, up to a point */
169  if (pool_is_free_index (cm->tables, table_index))
170  return;
171 
172  t = pool_elt_at_index (cm->tables, table_index);
173  if (del_chain && t->next_table_index != ~0)
174  /* Recursively delete the entire chain */
176 
177  vec_free (t->mask);
178  vec_free (t->buckets);
179 #if USE_DLMALLOC == 0
180  mheap_free (t->mheap);
181 #else
182  destroy_mspace (t->mheap);
183 #endif
184 
185  pool_put (cm->tables, t);
186 }
187 
188 static vnet_classify_entry_t *
190 {
191  vnet_classify_entry_t *rv = 0;
192  u32 required_length;
193  void *oldheap;
194 
195  ASSERT (t->writer_lock[0]);
196  required_length =
197  (sizeof (vnet_classify_entry_t) + (t->match_n_vectors * sizeof (u32x4)))
198  * t->entries_per_page * (1 << log2_pages);
199 
200  if (log2_pages >= vec_len (t->freelists) || t->freelists[log2_pages] == 0)
201  {
202  oldheap = clib_mem_set_heap (t->mheap);
203 
204  vec_validate (t->freelists, log2_pages);
205 
206  rv = clib_mem_alloc_aligned (required_length, CLIB_CACHE_LINE_BYTES);
207  clib_mem_set_heap (oldheap);
208  goto initialize;
209  }
210  rv = t->freelists[log2_pages];
211  t->freelists[log2_pages] = rv->next_free;
212 
213 initialize:
214  ASSERT (rv);
215 
216  clib_memset (rv, 0xff, required_length);
217  return rv;
218 }
219 
220 static void
222  vnet_classify_entry_t * v, u32 log2_pages)
223 {
224  ASSERT (t->writer_lock[0]);
225 
226  ASSERT (vec_len (t->freelists) > log2_pages);
227 
228  v->next_free = t->freelists[log2_pages];
229  t->freelists[log2_pages] = v;
230 }
231 
232 static inline void make_working_copy
234 {
235  vnet_classify_entry_t *v;
236  vnet_classify_bucket_t working_bucket __attribute__ ((aligned (8)));
237  void *oldheap;
238  vnet_classify_entry_t *working_copy;
239  u32 thread_index = vlib_get_thread_index ();
240  int working_copy_length, required_length;
241 
242  if (thread_index >= vec_len (t->working_copies))
243  {
244  oldheap = clib_mem_set_heap (t->mheap);
245  vec_validate (t->working_copies, thread_index);
246  vec_validate (t->working_copy_lengths, thread_index);
247  t->working_copy_lengths[thread_index] = -1;
248  clib_mem_set_heap (oldheap);
249  }
250 
251  /*
252  * working_copies are per-cpu so that near-simultaneous
253  * updates from multiple threads will not result in sporadic, spurious
254  * lookup failures.
255  */
256  working_copy = t->working_copies[thread_index];
257  working_copy_length = t->working_copy_lengths[thread_index];
258  required_length =
259  (sizeof (vnet_classify_entry_t) + (t->match_n_vectors * sizeof (u32x4)))
260  * t->entries_per_page * (1 << b->log2_pages);
261 
262  t->saved_bucket.as_u64 = b->as_u64;
263  oldheap = clib_mem_set_heap (t->mheap);
264 
265  if (required_length > working_copy_length)
266  {
267  if (working_copy)
268  clib_mem_free (working_copy);
269  working_copy =
271  t->working_copies[thread_index] = working_copy;
272  }
273 
274  clib_mem_set_heap (oldheap);
275 
276  v = vnet_classify_get_entry (t, b->offset);
277 
278  clib_memcpy_fast (working_copy, v, required_length);
279 
280  working_bucket.as_u64 = b->as_u64;
281  working_bucket.offset = vnet_classify_get_offset (t, working_copy);
283  b->as_u64 = working_bucket.as_u64;
284  t->working_copies[thread_index] = working_copy;
285 }
286 
287 static vnet_classify_entry_t *
289  vnet_classify_entry_t * old_values, u32 old_log2_pages,
290  u32 new_log2_pages)
291 {
292  vnet_classify_entry_t *new_values, *v, *new_v;
293  int i, j, length_in_entries;
294 
295  new_values = vnet_classify_entry_alloc (t, new_log2_pages);
296  length_in_entries = (1 << old_log2_pages) * t->entries_per_page;
297 
298  for (i = 0; i < length_in_entries; i++)
299  {
300  u64 new_hash;
301 
302  v = vnet_classify_entry_at_index (t, old_values, i);
303 
305  {
306  /* Hack so we can use the packet hash routine */
307  u8 *key_minus_skip;
308  key_minus_skip = (u8 *) v->key;
309  key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
310 
311  new_hash = vnet_classify_hash_packet (t, key_minus_skip);
312  new_hash >>= t->log2_nbuckets;
313  new_hash &= (1 << new_log2_pages) - 1;
314 
315  for (j = 0; j < t->entries_per_page; j++)
316  {
317  new_v = vnet_classify_entry_at_index (t, new_values,
318  new_hash + j);
319 
320  if (vnet_classify_entry_is_free (new_v))
321  {
322  clib_memcpy_fast (new_v, v, sizeof (vnet_classify_entry_t)
323  + (t->match_n_vectors * sizeof (u32x4)));
324  new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
325  goto doublebreak;
326  }
327  }
328  /* Crap. Tell caller to try again */
329  vnet_classify_entry_free (t, new_values, new_log2_pages);
330  return 0;
331  doublebreak:
332  ;
333  }
334  }
335  return new_values;
336 }
337 
338 static vnet_classify_entry_t *
340  vnet_classify_entry_t * old_values,
341  u32 old_log2_pages, u32 new_log2_pages)
342 {
343  vnet_classify_entry_t *new_values, *v, *new_v;
344  int i, j, new_length_in_entries, old_length_in_entries;
345 
346  new_values = vnet_classify_entry_alloc (t, new_log2_pages);
347  new_length_in_entries = (1 << new_log2_pages) * t->entries_per_page;
348  old_length_in_entries = (1 << old_log2_pages) * t->entries_per_page;
349 
350  j = 0;
351  for (i = 0; i < old_length_in_entries; i++)
352  {
353  v = vnet_classify_entry_at_index (t, old_values, i);
354 
356  {
357  for (; j < new_length_in_entries; j++)
358  {
359  new_v = vnet_classify_entry_at_index (t, new_values, j);
360 
361  if (vnet_classify_entry_is_busy (new_v))
362  {
363  clib_warning ("BUG: linear rehash new entry not free!");
364  continue;
365  }
366  clib_memcpy_fast (new_v, v, sizeof (vnet_classify_entry_t)
367  + (t->match_n_vectors * sizeof (u32x4)));
368  new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
369  j++;
370  goto doublebreak;
371  }
372  /*
373  * Crap. Tell caller to try again.
374  * This should never happen...
375  */
376  clib_warning ("BUG: linear rehash failed!");
377  vnet_classify_entry_free (t, new_values, new_log2_pages);
378  return 0;
379  }
380  doublebreak:
381  ;
382  }
383 
384  return new_values;
385 }
386 
387 static void
388 vnet_classify_entry_claim_resource (vnet_classify_entry_t * e)
389 {
390  switch (e->action)
391  {
394  break;
397  break;
399  break;
400  }
401 }
402 
403 static void
404 vnet_classify_entry_release_resource (vnet_classify_entry_t * e)
405 {
406  switch (e->action)
407  {
410  break;
413  break;
415  break;
416  }
417 }
418 
419 int
421  vnet_classify_entry_t * add_v, int is_add)
422 {
423  u32 bucket_index;
424  vnet_classify_bucket_t *b, tmp_b;
425  vnet_classify_entry_t *v, *new_v, *save_new_v, *working_copy, *save_v;
426  u32 value_index;
427  int rv = 0;
428  int i;
429  u64 hash, new_hash;
430  u32 limit;
431  u32 old_log2_pages, new_log2_pages;
432  u32 thread_index = vlib_get_thread_index ();
433  u8 *key_minus_skip;
434  int resplit_once = 0;
435  int mark_bucket_linear;
436 
437  ASSERT ((add_v->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
438 
439  key_minus_skip = (u8 *) add_v->key;
440  key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
441 
442  hash = vnet_classify_hash_packet (t, key_minus_skip);
443 
444  bucket_index = hash & (t->nbuckets - 1);
445  b = &t->buckets[bucket_index];
446 
447  hash >>= t->log2_nbuckets;
448 
450  ;
451 
452  /* First elt in the bucket? */
453  if (b->offset == 0)
454  {
455  if (is_add == 0)
456  {
457  rv = -1;
458  goto unlock;
459  }
460 
461  v = vnet_classify_entry_alloc (t, 0 /* new_log2_pages */ );
462  clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) +
463  t->match_n_vectors * sizeof (u32x4));
464  v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
466 
467  tmp_b.as_u64 = 0;
468  tmp_b.offset = vnet_classify_get_offset (t, v);
469 
470  b->as_u64 = tmp_b.as_u64;
471  t->active_elements++;
472 
473  goto unlock;
474  }
475 
476  make_working_copy (t, b);
477 
479  value_index = hash & ((1 << t->saved_bucket.log2_pages) - 1);
480  limit = t->entries_per_page;
481  if (PREDICT_FALSE (b->linear_search))
482  {
483  value_index = 0;
484  limit *= (1 << b->log2_pages);
485  }
486 
487  if (is_add)
488  {
489  /*
490  * For obvious (in hindsight) reasons, see if we're supposed to
491  * replace an existing key, then look for an empty slot.
492  */
493 
494  for (i = 0; i < limit; i++)
495  {
496  v = vnet_classify_entry_at_index (t, save_v, value_index + i);
497 
498  if (!memcmp
499  (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4)))
500  {
501  clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) +
502  t->match_n_vectors * sizeof (u32x4));
503  v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
505 
507  /* Restore the previous (k,v) pairs */
508  b->as_u64 = t->saved_bucket.as_u64;
509  goto unlock;
510  }
511  }
512  for (i = 0; i < limit; i++)
513  {
514  v = vnet_classify_entry_at_index (t, save_v, value_index + i);
515 
517  {
518  clib_memcpy_fast (v, add_v, sizeof (vnet_classify_entry_t) +
519  t->match_n_vectors * sizeof (u32x4));
520  v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
522 
524  b->as_u64 = t->saved_bucket.as_u64;
525  t->active_elements++;
526  goto unlock;
527  }
528  }
529  /* no room at the inn... split case... */
530  }
531  else
532  {
533  for (i = 0; i < limit; i++)
534  {
535  v = vnet_classify_entry_at_index (t, save_v, value_index + i);
536 
537  if (!memcmp
538  (v->key, add_v->key, t->match_n_vectors * sizeof (u32x4)))
539  {
541  clib_memset (v, 0xff, sizeof (vnet_classify_entry_t) +
542  t->match_n_vectors * sizeof (u32x4));
543  v->flags |= VNET_CLASSIFY_ENTRY_FREE;
544 
546  b->as_u64 = t->saved_bucket.as_u64;
547  t->active_elements--;
548  goto unlock;
549  }
550  }
551  rv = -3;
552  b->as_u64 = t->saved_bucket.as_u64;
553  goto unlock;
554  }
555 
556  old_log2_pages = t->saved_bucket.log2_pages;
557  new_log2_pages = old_log2_pages + 1;
558  working_copy = t->working_copies[thread_index];
559 
561  goto linear_resplit;
562 
563  mark_bucket_linear = 0;
564 
565  new_v = split_and_rehash (t, working_copy, old_log2_pages, new_log2_pages);
566 
567  if (new_v == 0)
568  {
569  try_resplit:
570  resplit_once = 1;
571  new_log2_pages++;
572 
573  new_v = split_and_rehash (t, working_copy, old_log2_pages,
574  new_log2_pages);
575  if (new_v == 0)
576  {
577  mark_linear:
578  new_log2_pages--;
579 
580  linear_resplit:
581  /* pinned collisions, use linear search */
582  new_v = split_and_rehash_linear (t, working_copy, old_log2_pages,
583  new_log2_pages);
584  /* A new linear-search bucket? */
585  if (!t->saved_bucket.linear_search)
586  t->linear_buckets++;
587  mark_bucket_linear = 1;
588  }
589  }
590 
591  /* Try to add the new entry */
592  save_new_v = new_v;
593 
594  key_minus_skip = (u8 *) add_v->key;
595  key_minus_skip -= t->skip_n_vectors * sizeof (u32x4);
596 
597  new_hash = vnet_classify_hash_packet_inline (t, key_minus_skip);
598  new_hash >>= t->log2_nbuckets;
599  new_hash &= (1 << new_log2_pages) - 1;
600 
601  limit = t->entries_per_page;
602  if (mark_bucket_linear)
603  {
604  limit *= (1 << new_log2_pages);
605  new_hash = 0;
606  }
607 
608  for (i = 0; i < limit; i++)
609  {
610  new_v = vnet_classify_entry_at_index (t, save_new_v, new_hash + i);
611 
612  if (vnet_classify_entry_is_free (new_v))
613  {
614  clib_memcpy_fast (new_v, add_v, sizeof (vnet_classify_entry_t) +
615  t->match_n_vectors * sizeof (u32x4));
616  new_v->flags &= ~(VNET_CLASSIFY_ENTRY_FREE);
618 
619  goto expand_ok;
620  }
621  }
622  /* Crap. Try again */
623  vnet_classify_entry_free (t, save_new_v, new_log2_pages);
624  new_log2_pages++;
625 
626  if (resplit_once)
627  goto mark_linear;
628  else
629  goto try_resplit;
630 
631 expand_ok:
632  tmp_b.log2_pages = new_log2_pages;
633  tmp_b.offset = vnet_classify_get_offset (t, save_new_v);
634  tmp_b.linear_search = mark_bucket_linear;
635 
637  b->as_u64 = tmp_b.as_u64;
638  t->active_elements++;
640  vnet_classify_entry_free (t, v, old_log2_pages);
641 
642 unlock:
644  t->writer_lock[0] = 0;
645  return rv;
646 }
647 
648 /* *INDENT-OFF* */
649 typedef CLIB_PACKED(struct {
651  ip4_header_t ip;
652 }) classify_data_or_mask_t;
653 /* *INDENT-ON* */
654 
655 u64
657 {
658  return vnet_classify_hash_packet_inline (t, h);
659 }
660 
661 vnet_classify_entry_t *
663  u8 * h, u64 hash, f64 now)
664 {
665  return vnet_classify_find_entry_inline (t, h, hash, now);
666 }
667 
668 static u8 *
669 format_classify_entry (u8 * s, va_list * args)
670 {
671  vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *);
672  vnet_classify_entry_t *e = va_arg (*args, vnet_classify_entry_t *);
673 
674  s = format
675  (s, "[%u]: next_index %d advance %d opaque %d action %d metadata %d\n",
676  vnet_classify_get_offset (t, e), e->next_index, e->advance,
677  e->opaque_index, e->action, e->metadata);
678 
679 
680  s = format (s, " k: %U\n", format_hex_bytes, e->key,
681  t->match_n_vectors * sizeof (u32x4));
682 
684  s = format (s, " hits %lld, last_heard %.2f\n",
685  e->hits, e->last_heard);
686  else
687  s = format (s, " entry is free\n");
688  return s;
689 }
690 
691 u8 *
692 format_classify_table (u8 * s, va_list * args)
693 {
694  vnet_classify_table_t *t = va_arg (*args, vnet_classify_table_t *);
695  int verbose = va_arg (*args, int);
697  vnet_classify_entry_t *v, *save_v;
698  int i, j, k;
699  u64 active_elements = 0;
700 
701  for (i = 0; i < t->nbuckets; i++)
702  {
703  b = &t->buckets[i];
704  if (b->offset == 0)
705  {
706  if (verbose > 1)
707  s = format (s, "[%d]: empty\n", i);
708  continue;
709  }
710 
711  if (verbose)
712  {
713  s = format (s, "[%d]: heap offset %d, elts %d, %s\n", i,
714  b->offset, (1 << b->log2_pages) * t->entries_per_page,
715  b->linear_search ? "LINEAR" : "normal");
716  }
717 
718  save_v = vnet_classify_get_entry (t, b->offset);
719  for (j = 0; j < (1 << b->log2_pages); j++)
720  {
721  for (k = 0; k < t->entries_per_page; k++)
722  {
723 
724  v = vnet_classify_entry_at_index (t, save_v,
725  j * t->entries_per_page + k);
726 
728  {
729  if (verbose > 1)
730  s = format (s, " %d: empty\n",
731  j * t->entries_per_page + k);
732  continue;
733  }
734  if (verbose)
735  {
736  s = format (s, " %d: %U\n",
737  j * t->entries_per_page + k,
738  format_classify_entry, t, v);
739  }
740  active_elements++;
741  }
742  }
743  }
744 
745  s = format (s, " %lld active elements\n", active_elements);
746  s = format (s, " %d free lists\n", vec_len (t->freelists));
747  s = format (s, " %d linear-search buckets\n", t->linear_buckets);
748  return s;
749 }
750 
751 int
753  u8 * mask,
754  u32 nbuckets,
756  u32 skip,
757  u32 match,
758  u32 next_table_index,
759  u32 miss_next_index,
760  u32 * table_index,
761  u8 current_data_flag,
762  i16 current_data_offset,
763  int is_add, int del_chain)
764 {
766 
767  if (is_add)
768  {
769  if (*table_index == ~0) /* add */
770  {
771  if (memory_size == 0)
772  return VNET_API_ERROR_INVALID_MEMORY_SIZE;
773 
774  if (nbuckets == 0)
775  return VNET_API_ERROR_INVALID_VALUE;
776 
777  t = vnet_classify_new_table (cm, mask, nbuckets, memory_size,
778  skip, match);
779  t->next_table_index = next_table_index;
780  t->miss_next_index = miss_next_index;
781  t->current_data_flag = current_data_flag;
782  t->current_data_offset = current_data_offset;
783  *table_index = t - cm->tables;
784  }
785  else /* update */
786  {
788  t = pool_elt_at_index (cm->tables, *table_index);
789 
790  t->next_table_index = next_table_index;
791  }
792  return 0;
793  }
794 
795  vnet_classify_delete_table_index (cm, *table_index, del_chain);
796  return 0;
797 }
798 
799 #define foreach_tcp_proto_field \
800 _(src) \
801 _(dst)
802 
803 #define foreach_udp_proto_field \
804 _(src_port) \
805 _(dst_port)
806 
807 #define foreach_ip4_proto_field \
808 _(src_address) \
809 _(dst_address) \
810 _(tos) \
811 _(length) \
812 _(fragment_id) \
813 _(ttl) \
814 _(protocol) \
815 _(checksum)
816 
817 uword
818 unformat_tcp_mask (unformat_input_t * input, va_list * args)
819 {
820  u8 **maskp = va_arg (*args, u8 **);
821  u8 *mask = 0;
822  u8 found_something = 0;
823  tcp_header_t *tcp;
824 
825 #define _(a) u8 a=0;
827 #undef _
828 
830  {
831  if (0);
832 #define _(a) else if (unformat (input, #a)) a=1;
834 #undef _
835  else
836  break;
837  }
838 
839 #define _(a) found_something += a;
841 #undef _
842 
843  if (found_something == 0)
844  return 0;
845 
846  vec_validate (mask, sizeof (*tcp) - 1);
847 
848  tcp = (tcp_header_t *) mask;
849 
850 #define _(a) if (a) clib_memset (&tcp->a, 0xff, sizeof (tcp->a));
852 #undef _
853 
854  *maskp = mask;
855  return 1;
856 }
857 
858 uword
859 unformat_udp_mask (unformat_input_t * input, va_list * args)
860 {
861  u8 **maskp = va_arg (*args, u8 **);
862  u8 *mask = 0;
863  u8 found_something = 0;
864  udp_header_t *udp;
865 
866 #define _(a) u8 a=0;
868 #undef _
869 
871  {
872  if (0);
873 #define _(a) else if (unformat (input, #a)) a=1;
875 #undef _
876  else
877  break;
878  }
879 
880 #define _(a) found_something += a;
882 #undef _
883 
884  if (found_something == 0)
885  return 0;
886 
887  vec_validate (mask, sizeof (*udp) - 1);
888 
889  udp = (udp_header_t *) mask;
890 
891 #define _(a) if (a) clib_memset (&udp->a, 0xff, sizeof (udp->a));
893 #undef _
894 
895  *maskp = mask;
896  return 1;
897 }
898 
899 typedef struct
900 {
903 
904 uword
905 unformat_l4_mask (unformat_input_t * input, va_list * args)
906 {
907  u8 **maskp = va_arg (*args, u8 **);
908  u16 src_port = 0, dst_port = 0;
909  tcpudp_header_t *tcpudp;
910 
912  {
913  if (unformat (input, "tcp %U", unformat_tcp_mask, maskp))
914  return 1;
915  else if (unformat (input, "udp %U", unformat_udp_mask, maskp))
916  return 1;
917  else if (unformat (input, "src_port"))
918  src_port = 0xFFFF;
919  else if (unformat (input, "dst_port"))
920  dst_port = 0xFFFF;
921  else
922  return 0;
923  }
924 
925  if (!src_port && !dst_port)
926  return 0;
927 
928  u8 *mask = 0;
929  vec_validate (mask, sizeof (tcpudp_header_t) - 1);
930 
931  tcpudp = (tcpudp_header_t *) mask;
932  tcpudp->src_port = src_port;
933  tcpudp->dst_port = dst_port;
934 
935  *maskp = mask;
936 
937  return 1;
938 }
939 
940 uword
941 unformat_ip4_mask (unformat_input_t * input, va_list * args)
942 {
943  u8 **maskp = va_arg (*args, u8 **);
944  u8 *mask = 0;
945  u8 found_something = 0;
946  ip4_header_t *ip;
947 
948 #define _(a) u8 a=0;
950 #undef _
951  u8 version = 0;
952  u8 hdr_length = 0;
953 
954 
956  {
957  if (unformat (input, "version"))
958  version = 1;
959  else if (unformat (input, "hdr_length"))
960  hdr_length = 1;
961  else if (unformat (input, "src"))
962  src_address = 1;
963  else if (unformat (input, "dst"))
964  dst_address = 1;
965  else if (unformat (input, "proto"))
966  protocol = 1;
967 
968 #define _(a) else if (unformat (input, #a)) a=1;
970 #undef _
971  else
972  break;
973  }
974 
975 #define _(a) found_something += a;
977 #undef _
978 
979  if (found_something == 0)
980  return 0;
981 
982  vec_validate (mask, sizeof (*ip) - 1);
983 
984  ip = (ip4_header_t *) mask;
985 
986 #define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a));
988 #undef _
989 
991 
992  if (version)
993  ip->ip_version_and_header_length |= 0xF0;
994 
995  if (hdr_length)
996  ip->ip_version_and_header_length |= 0x0F;
997 
998  *maskp = mask;
999  return 1;
1000 }
1001 
1002 #define foreach_ip6_proto_field \
1003 _(src_address) \
1004 _(dst_address) \
1005 _(payload_length) \
1006 _(hop_limit) \
1007 _(protocol)
1008 
1009 uword
1010 unformat_ip6_mask (unformat_input_t * input, va_list * args)
1011 {
1012  u8 **maskp = va_arg (*args, u8 **);
1013  u8 *mask = 0;
1014  u8 found_something = 0;
1015  ip6_header_t *ip;
1016  u32 ip_version_traffic_class_and_flow_label;
1017 
1018 #define _(a) u8 a=0;
1020 #undef _
1021  u8 version = 0;
1022  u8 traffic_class = 0;
1023  u8 flow_label = 0;
1024 
1025  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1026  {
1027  if (unformat (input, "version"))
1028  version = 1;
1029  else if (unformat (input, "traffic-class"))
1030  traffic_class = 1;
1031  else if (unformat (input, "flow-label"))
1032  flow_label = 1;
1033  else if (unformat (input, "src"))
1034  src_address = 1;
1035  else if (unformat (input, "dst"))
1036  dst_address = 1;
1037  else if (unformat (input, "proto"))
1038  protocol = 1;
1039 
1040 #define _(a) else if (unformat (input, #a)) a=1;
1042 #undef _
1043  else
1044  break;
1045  }
1046 
1047 #define _(a) found_something += a;
1049 #undef _
1050 
1051  if (found_something == 0)
1052  return 0;
1053 
1054  vec_validate (mask, sizeof (*ip) - 1);
1055 
1056  ip = (ip6_header_t *) mask;
1057 
1058 #define _(a) if (a) clib_memset (&ip->a, 0xff, sizeof (ip->a));
1060 #undef _
1061 
1062  ip_version_traffic_class_and_flow_label = 0;
1063 
1064  if (version)
1065  ip_version_traffic_class_and_flow_label |= 0xF0000000;
1066 
1067  if (traffic_class)
1068  ip_version_traffic_class_and_flow_label |= 0x0FF00000;
1069 
1070  if (flow_label)
1071  ip_version_traffic_class_and_flow_label |= 0x000FFFFF;
1072 
1074  clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
1075 
1076  *maskp = mask;
1077  return 1;
1078 }
1079 
1080 uword
1081 unformat_l3_mask (unformat_input_t * input, va_list * args)
1082 {
1083  u8 **maskp = va_arg (*args, u8 **);
1084 
1085  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1086  {
1087  if (unformat (input, "ip4 %U", unformat_ip4_mask, maskp))
1088  return 1;
1089  else if (unformat (input, "ip6 %U", unformat_ip6_mask, maskp))
1090  return 1;
1091  else
1092  break;
1093  }
1094  return 0;
1095 }
1096 
1097 uword
1098 unformat_l2_mask (unformat_input_t * input, va_list * args)
1099 {
1100  u8 **maskp = va_arg (*args, u8 **);
1101  u8 *mask = 0;
1102  u8 src = 0;
1103  u8 dst = 0;
1104  u8 proto = 0;
1105  u8 tag1 = 0;
1106  u8 tag2 = 0;
1107  u8 ignore_tag1 = 0;
1108  u8 ignore_tag2 = 0;
1109  u8 cos1 = 0;
1110  u8 cos2 = 0;
1111  u8 dot1q = 0;
1112  u8 dot1ad = 0;
1113  int len = 14;
1114 
1115  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1116  {
1117  if (unformat (input, "src"))
1118  src = 1;
1119  else if (unformat (input, "dst"))
1120  dst = 1;
1121  else if (unformat (input, "proto"))
1122  proto = 1;
1123  else if (unformat (input, "tag1"))
1124  tag1 = 1;
1125  else if (unformat (input, "tag2"))
1126  tag2 = 1;
1127  else if (unformat (input, "ignore-tag1"))
1128  ignore_tag1 = 1;
1129  else if (unformat (input, "ignore-tag2"))
1130  ignore_tag2 = 1;
1131  else if (unformat (input, "cos1"))
1132  cos1 = 1;
1133  else if (unformat (input, "cos2"))
1134  cos2 = 1;
1135  else if (unformat (input, "dot1q"))
1136  dot1q = 1;
1137  else if (unformat (input, "dot1ad"))
1138  dot1ad = 1;
1139  else
1140  break;
1141  }
1142  if ((src + dst + proto + tag1 + tag2 + dot1q + dot1ad +
1143  ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
1144  return 0;
1145 
1146  if (tag1 || ignore_tag1 || cos1 || dot1q)
1147  len = 18;
1148  if (tag2 || ignore_tag2 || cos2 || dot1ad)
1149  len = 22;
1150 
1151  vec_validate (mask, len - 1);
1152 
1153  if (dst)
1154  clib_memset (mask, 0xff, 6);
1155 
1156  if (src)
1157  clib_memset (mask + 6, 0xff, 6);
1158 
1159  if (tag2 || dot1ad)
1160  {
1161  /* inner vlan tag */
1162  if (tag2)
1163  {
1164  mask[19] = 0xff;
1165  mask[18] = 0x0f;
1166  }
1167  if (cos2)
1168  mask[18] |= 0xe0;
1169  if (proto)
1170  mask[21] = mask[20] = 0xff;
1171  if (tag1)
1172  {
1173  mask[15] = 0xff;
1174  mask[14] = 0x0f;
1175  }
1176  if (cos1)
1177  mask[14] |= 0xe0;
1178  *maskp = mask;
1179  return 1;
1180  }
1181  if (tag1 | dot1q)
1182  {
1183  if (tag1)
1184  {
1185  mask[15] = 0xff;
1186  mask[14] = 0x0f;
1187  }
1188  if (cos1)
1189  mask[14] |= 0xe0;
1190  if (proto)
1191  mask[16] = mask[17] = 0xff;
1192  *maskp = mask;
1193  return 1;
1194  }
1195  if (cos2)
1196  mask[18] |= 0xe0;
1197  if (cos1)
1198  mask[14] |= 0xe0;
1199  if (proto)
1200  mask[12] = mask[13] = 0xff;
1201 
1202  *maskp = mask;
1203  return 1;
1204 }
1205 
1206 uword
1207 unformat_classify_mask (unformat_input_t * input, va_list * args)
1208 {
1209  u8 **maskp = va_arg (*args, u8 **);
1210  u32 *skipp = va_arg (*args, u32 *);
1211  u32 *matchp = va_arg (*args, u32 *);
1212  u32 match;
1213  u8 *mask = 0;
1214  u8 *l2 = 0;
1215  u8 *l3 = 0;
1216  u8 *l4 = 0;
1217  int i;
1218 
1219  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1220  {
1221  if (unformat (input, "hex %U", unformat_hex_string, &mask))
1222  ;
1223  else if (unformat (input, "l2 %U", unformat_l2_mask, &l2))
1224  ;
1225  else if (unformat (input, "l3 %U", unformat_l3_mask, &l3))
1226  ;
1227  else if (unformat (input, "l4 %U", unformat_l4_mask, &l4))
1228  ;
1229  else
1230  break;
1231  }
1232 
1233  if (l4 && !l3)
1234  {
1235  vec_free (mask);
1236  vec_free (l2);
1237  vec_free (l4);
1238  return 0;
1239  }
1240 
1241  if (mask || l2 || l3 || l4)
1242  {
1243  if (l2 || l3 || l4)
1244  {
1245  /* "With a free Ethernet header in every package" */
1246  if (l2 == 0)
1247  vec_validate (l2, 13);
1248  mask = l2;
1249  if (l3)
1250  {
1251  vec_append (mask, l3);
1252  vec_free (l3);
1253  }
1254  if (l4)
1255  {
1256  vec_append (mask, l4);
1257  vec_free (l4);
1258  }
1259  }
1260 
1261  /* Scan forward looking for the first significant mask octet */
1262  for (i = 0; i < vec_len (mask); i++)
1263  if (mask[i])
1264  break;
1265 
1266  /* compute (skip, match) params */
1267  *skipp = i / sizeof (u32x4);
1268  vec_delete (mask, *skipp * sizeof (u32x4), 0);
1269 
1270  /* Pad mask to an even multiple of the vector size */
1271  while (vec_len (mask) % sizeof (u32x4))
1272  vec_add1 (mask, 0);
1273 
1274  match = vec_len (mask) / sizeof (u32x4);
1275 
1276  for (i = match * sizeof (u32x4); i > 0; i -= sizeof (u32x4))
1277  {
1278  u64 *tmp = (u64 *) (mask + (i - sizeof (u32x4)));
1279  if (*tmp || *(tmp + 1))
1280  break;
1281  match--;
1282  }
1283  if (match == 0)
1284  clib_warning ("BUG: match 0");
1285 
1286  _vec_len (mask) = match * sizeof (u32x4);
1287 
1288  *matchp = match;
1289  *maskp = mask;
1290 
1291  return 1;
1292  }
1293 
1294  return 0;
1295 }
1296 
1297 #define foreach_l2_input_next \
1298 _(drop, DROP) \
1299 _(ethernet, ETHERNET_INPUT) \
1300 _(ip4, IP4_INPUT) \
1301 _(ip6, IP6_INPUT) \
1302 _(li, LI)
1303 
1304 uword
1306 {
1308  u32 *miss_next_indexp = va_arg (*args, u32 *);
1309  u32 next_index = 0;
1310  u32 tmp;
1311  int i;
1312 
1313  /* First try registered unformat fns, allowing override... */
1314  for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++)
1315  {
1316  if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp))
1317  {
1318  next_index = tmp;
1319  goto out;
1320  }
1321  }
1322 
1323 #define _(n,N) \
1324  if (unformat (input, #n)) { next_index = L2_INPUT_CLASSIFY_NEXT_##N; goto out;}
1326 #undef _
1327 
1328  if (unformat (input, "%d", &tmp))
1329  {
1330  next_index = tmp;
1331  goto out;
1332  }
1333 
1334  return 0;
1335 
1336 out:
1337  *miss_next_indexp = next_index;
1338  return 1;
1339 }
1340 
1341 #define foreach_l2_output_next \
1342 _(drop, DROP)
1343 
1344 uword
1346 {
1348  u32 *miss_next_indexp = va_arg (*args, u32 *);
1349  u32 next_index = 0;
1350  u32 tmp;
1351  int i;
1352 
1353  /* First try registered unformat fns, allowing override... */
1354  for (i = 0; i < vec_len (cm->unformat_l2_next_index_fns); i++)
1355  {
1356  if (unformat (input, "%U", cm->unformat_l2_next_index_fns[i], &tmp))
1357  {
1358  next_index = tmp;
1359  goto out;
1360  }
1361  }
1362 
1363 #define _(n,N) \
1364  if (unformat (input, #n)) { next_index = L2_OUTPUT_CLASSIFY_NEXT_##N; goto out;}
1366 #undef _
1367 
1368  if (unformat (input, "%d", &tmp))
1369  {
1370  next_index = tmp;
1371  goto out;
1372  }
1373 
1374  return 0;
1375 
1376 out:
1377  *miss_next_indexp = next_index;
1378  return 1;
1379 }
1380 
1381 #define foreach_ip_next \
1382 _(drop, DROP) \
1383 _(rewrite, REWRITE)
1384 
1385 uword
1386 unformat_ip_next_index (unformat_input_t * input, va_list * args)
1387 {
1388  u32 *miss_next_indexp = va_arg (*args, u32 *);
1390  u32 next_index = 0;
1391  u32 tmp;
1392  int i;
1393 
1394  /* First try registered unformat fns, allowing override... */
1395  for (i = 0; i < vec_len (cm->unformat_ip_next_index_fns); i++)
1396  {
1397  if (unformat (input, "%U", cm->unformat_ip_next_index_fns[i], &tmp))
1398  {
1399  next_index = tmp;
1400  goto out;
1401  }
1402  }
1403 
1404 #define _(n,N) \
1405  if (unformat (input, #n)) { next_index = IP_LOOKUP_NEXT_##N; goto out;}
1407 #undef _
1408 
1409  if (unformat (input, "%d", &tmp))
1410  {
1411  next_index = tmp;
1412  goto out;
1413  }
1414 
1415  return 0;
1416 
1417 out:
1418  *miss_next_indexp = next_index;
1419  return 1;
1420 }
1421 
1422 #define foreach_acl_next \
1423 _(deny, DENY)
1424 
1425 uword
1427 {
1428  u32 *next_indexp = va_arg (*args, u32 *);
1430  u32 next_index = 0;
1431  u32 tmp;
1432  int i;
1433 
1434  /* First try registered unformat fns, allowing override... */
1435  for (i = 0; i < vec_len (cm->unformat_acl_next_index_fns); i++)
1436  {
1437  if (unformat (input, "%U", cm->unformat_acl_next_index_fns[i], &tmp))
1438  {
1439  next_index = tmp;
1440  goto out;
1441  }
1442  }
1443 
1444 #define _(n,N) \
1445  if (unformat (input, #n)) { next_index = ACL_NEXT_INDEX_##N; goto out;}
1447 #undef _
1448 
1449  if (unformat (input, "permit"))
1450  {
1451  next_index = ~0;
1452  goto out;
1453  }
1454  else if (unformat (input, "%d", &tmp))
1455  {
1456  next_index = tmp;
1457  goto out;
1458  }
1459 
1460  return 0;
1461 
1462 out:
1463  *next_indexp = next_index;
1464  return 1;
1465 }
1466 
1467 uword
1469 {
1470  u32 *next_indexp = va_arg (*args, u32 *);
1472  u32 next_index = 0;
1473  u32 tmp;
1474  int i;
1475 
1476  /* First try registered unformat fns, allowing override... */
1477  for (i = 0; i < vec_len (cm->unformat_policer_next_index_fns); i++)
1478  {
1479  if (unformat
1480  (input, "%U", cm->unformat_policer_next_index_fns[i], &tmp))
1481  {
1482  next_index = tmp;
1483  goto out;
1484  }
1485  }
1486 
1487  if (unformat (input, "%d", &tmp))
1488  {
1489  next_index = tmp;
1490  goto out;
1491  }
1492 
1493  return 0;
1494 
1495 out:
1496  *next_indexp = next_index;
1497  return 1;
1498 }
1499 
1500 static clib_error_t *
1502  unformat_input_t * input, vlib_cli_command_t * cmd)
1503 {
1504  u32 nbuckets = 2;
1505  u32 skip = ~0;
1506  u32 match = ~0;
1507  int is_add = 1;
1508  int del_chain = 0;
1509  u32 table_index = ~0;
1510  u32 next_table_index = ~0;
1511  u32 miss_next_index = ~0;
1512  u32 memory_size = 2 << 20;
1513  u32 tmp;
1514  u32 current_data_flag = 0;
1515  int current_data_offset = 0;
1516 
1517  u8 *mask = 0;
1519  int rv;
1520 
1521  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1522  {
1523  if (unformat (input, "del"))
1524  is_add = 0;
1525  else if (unformat (input, "del-chain"))
1526  {
1527  is_add = 0;
1528  del_chain = 1;
1529  }
1530  else if (unformat (input, "buckets %d", &nbuckets))
1531  ;
1532  else if (unformat (input, "skip %d", &skip))
1533  ;
1534  else if (unformat (input, "match %d", &match))
1535  ;
1536  else if (unformat (input, "table %d", &table_index))
1537  ;
1538  else if (unformat (input, "mask %U", unformat_classify_mask,
1539  &mask, &skip, &match))
1540  ;
1541  else if (unformat (input, "memory-size %uM", &tmp))
1542  memory_size = tmp << 20;
1543  else if (unformat (input, "memory-size %uG", &tmp))
1544  memory_size = tmp << 30;
1545  else if (unformat (input, "next-table %d", &next_table_index))
1546  ;
1547  else if (unformat (input, "miss-next %U", unformat_ip_next_index,
1548  &miss_next_index))
1549  ;
1550  else
1551  if (unformat
1552  (input, "l2-input-miss-next %U", unformat_l2_input_next_index,
1553  &miss_next_index))
1554  ;
1555  else
1556  if (unformat
1557  (input, "l2-output-miss-next %U", unformat_l2_output_next_index,
1558  &miss_next_index))
1559  ;
1560  else if (unformat (input, "acl-miss-next %U", unformat_acl_next_index,
1561  &miss_next_index))
1562  ;
1563  else if (unformat (input, "current-data-flag %d", &current_data_flag))
1564  ;
1565  else
1566  if (unformat (input, "current-data-offset %d", &current_data_offset))
1567  ;
1568 
1569  else
1570  break;
1571  }
1572 
1573  if (is_add && mask == 0 && table_index == ~0)
1574  return clib_error_return (0, "Mask required");
1575 
1576  if (is_add && skip == ~0 && table_index == ~0)
1577  return clib_error_return (0, "skip count required");
1578 
1579  if (is_add && match == ~0 && table_index == ~0)
1580  return clib_error_return (0, "match count required");
1581 
1582  if (!is_add && table_index == ~0)
1583  return clib_error_return (0, "table index required for delete");
1584 
1585  rv = vnet_classify_add_del_table (cm, mask, nbuckets, memory_size,
1586  skip, match, next_table_index,
1587  miss_next_index, &table_index,
1588  current_data_flag, current_data_offset,
1589  is_add, del_chain);
1590  switch (rv)
1591  {
1592  case 0:
1593  break;
1594 
1595  default:
1596  return clib_error_return (0, "vnet_classify_add_del_table returned %d",
1597  rv);
1598  }
1599  return 0;
1600 }
1601 
1602 /* *INDENT-OFF* */
1603 VLIB_CLI_COMMAND (classify_table, static) = {
1604  .path = "classify table",
1605  .short_help =
1606  "classify table [miss-next|l2-miss_next|acl-miss-next <next_index>]"
1607  "\n mask <mask-value> buckets <nn> [skip <n>] [match <n>]"
1608  "\n [current-data-flag <n>] [current-data-offset <n>] [table <n>]"
1609  "\n [memory-size <nn>[M][G]] [next-table <n>]"
1610  "\n [del] [del-chain]",
1611  .function = classify_table_command_fn,
1612 };
1613 /* *INDENT-ON* */
1614 
1615 static u8 *
1616 format_vnet_classify_table (u8 * s, va_list * args)
1617 {
1618  vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *);
1619  int verbose = va_arg (*args, int);
1620  u32 index = va_arg (*args, u32);
1622 
1623  if (index == ~0)
1624  {
1625  s = format (s, "%10s%10s%10s%10s", "TableIdx", "Sessions", "NextTbl",
1626  "NextNode", verbose ? "Details" : "");
1627  return s;
1628  }
1629 
1630  t = pool_elt_at_index (cm->tables, index);
1631  s = format (s, "%10u%10d%10d%10d", index, t->active_elements,
1633 
1634  s = format (s, "\n Heap: %U", format_mheap, t->mheap, 0 /*verbose */ );
1635 
1636  s = format (s, "\n nbuckets %d, skip %d match %d flag %d offset %d",
1639  s = format (s, "\n mask %U", format_hex_bytes, t->mask,
1640  t->match_n_vectors * sizeof (u32x4));
1641  s = format (s, "\n linear-search buckets %d\n", t->linear_buckets);
1642 
1643  if (verbose == 0)
1644  return s;
1645 
1646  s = format (s, "\n%U", format_classify_table, t, verbose);
1647 
1648  return s;
1649 }
1650 
1651 static clib_error_t *
1653  unformat_input_t * input,
1654  vlib_cli_command_t * cmd)
1655 {
1658  u32 match_index = ~0;
1659  u32 *indices = 0;
1660  int verbose = 0;
1661  int i;
1662 
1663  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1664  {
1665  if (unformat (input, "index %d", &match_index))
1666  ;
1667  else if (unformat (input, "verbose %d", &verbose))
1668  ;
1669  else if (unformat (input, "verbose"))
1670  verbose = 1;
1671  else
1672  break;
1673  }
1674 
1675  /* *INDENT-OFF* */
1676  pool_foreach (t, cm->tables,
1677  ({
1678  if (match_index == ~0 || (match_index == t - cm->tables))
1679  vec_add1 (indices, t - cm->tables);
1680  }));
1681  /* *INDENT-ON* */
1682 
1683  if (vec_len (indices))
1684  {
1685  vlib_cli_output (vm, "%U", format_vnet_classify_table, cm, verbose,
1686  ~0 /* hdr */ );
1687  for (i = 0; i < vec_len (indices); i++)
1689  verbose, indices[i]);
1690  }
1691  else
1692  vlib_cli_output (vm, "No classifier tables configured");
1693 
1694  vec_free (indices);
1695 
1696  return 0;
1697 }
1698 
1699 /* *INDENT-OFF* */
1700 VLIB_CLI_COMMAND (show_classify_table_command, static) = {
1701  .path = "show classify tables",
1702  .short_help = "show classify tables [index <nn>]",
1703  .function = show_classify_tables_command_fn,
1704 };
1705 /* *INDENT-ON* */
1706 
1707 uword
1708 unformat_l4_match (unformat_input_t * input, va_list * args)
1709 {
1710  u8 **matchp = va_arg (*args, u8 **);
1711 
1712  u8 *proto_header = 0;
1713  int src_port = 0;
1714  int dst_port = 0;
1715 
1717 
1718  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1719  {
1720  if (unformat (input, "src_port %d", &src_port))
1721  ;
1722  else if (unformat (input, "dst_port %d", &dst_port))
1723  ;
1724  else
1725  return 0;
1726  }
1727 
1728  h.src_port = clib_host_to_net_u16 (src_port);
1729  h.dst_port = clib_host_to_net_u16 (dst_port);
1730  vec_validate (proto_header, sizeof (h) - 1);
1731  memcpy (proto_header, &h, sizeof (h));
1732 
1733  *matchp = proto_header;
1734 
1735  return 1;
1736 }
1737 
1738 uword
1739 unformat_ip4_match (unformat_input_t * input, va_list * args)
1740 {
1741  u8 **matchp = va_arg (*args, u8 **);
1742  u8 *match = 0;
1743  ip4_header_t *ip;
1744  int version = 0;
1745  u32 version_val;
1746  int hdr_length = 0;
1747  u32 hdr_length_val;
1748  int src = 0, dst = 0;
1749  ip4_address_t src_val, dst_val;
1750  int proto = 0;
1751  u32 proto_val;
1752  int tos = 0;
1753  u32 tos_val;
1754  int length = 0;
1755  u32 length_val;
1756  int fragment_id = 0;
1757  u32 fragment_id_val;
1758  int ttl = 0;
1759  int ttl_val;
1760  int checksum = 0;
1761  u32 checksum_val;
1762 
1763  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1764  {
1765  if (unformat (input, "version %d", &version_val))
1766  version = 1;
1767  else if (unformat (input, "hdr_length %d", &hdr_length_val))
1768  hdr_length = 1;
1769  else if (unformat (input, "src %U", unformat_ip4_address, &src_val))
1770  src = 1;
1771  else if (unformat (input, "dst %U", unformat_ip4_address, &dst_val))
1772  dst = 1;
1773  else if (unformat (input, "proto %d", &proto_val))
1774  proto = 1;
1775  else if (unformat (input, "tos %d", &tos_val))
1776  tos = 1;
1777  else if (unformat (input, "length %d", &length_val))
1778  length = 1;
1779  else if (unformat (input, "fragment_id %d", &fragment_id_val))
1780  fragment_id = 1;
1781  else if (unformat (input, "ttl %d", &ttl_val))
1782  ttl = 1;
1783  else if (unformat (input, "checksum %d", &checksum_val))
1784  checksum = 1;
1785  else
1786  break;
1787  }
1788 
1789  if (version + hdr_length + src + dst + proto + tos + length + fragment_id
1790  + ttl + checksum == 0)
1791  return 0;
1792 
1793  /*
1794  * Aligned because we use the real comparison functions
1795  */
1796  vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4));
1797 
1798  ip = (ip4_header_t *) match;
1799 
1800  /* These are realistically matched in practice */
1801  if (src)
1802  ip->src_address.as_u32 = src_val.as_u32;
1803 
1804  if (dst)
1805  ip->dst_address.as_u32 = dst_val.as_u32;
1806 
1807  if (proto)
1808  ip->protocol = proto_val;
1809 
1810 
1811  /* These are not, but they're included for completeness */
1812  if (version)
1813  ip->ip_version_and_header_length |= (version_val & 0xF) << 4;
1814 
1815  if (hdr_length)
1816  ip->ip_version_and_header_length |= (hdr_length_val & 0xF);
1817 
1818  if (tos)
1819  ip->tos = tos_val;
1820 
1821  if (length)
1822  ip->length = clib_host_to_net_u16 (length_val);
1823 
1824  if (ttl)
1825  ip->ttl = ttl_val;
1826 
1827  if (checksum)
1828  ip->checksum = clib_host_to_net_u16 (checksum_val);
1829 
1830  *matchp = match;
1831  return 1;
1832 }
1833 
1834 uword
1835 unformat_ip6_match (unformat_input_t * input, va_list * args)
1836 {
1837  u8 **matchp = va_arg (*args, u8 **);
1838  u8 *match = 0;
1839  ip6_header_t *ip;
1840  int version = 0;
1841  u32 version_val;
1842  u8 traffic_class = 0;
1843  u32 traffic_class_val;
1844  u8 flow_label = 0;
1845  u8 flow_label_val;
1846  int src = 0, dst = 0;
1847  ip6_address_t src_val, dst_val;
1848  int proto = 0;
1849  u32 proto_val;
1850  int payload_length = 0;
1851  u32 payload_length_val;
1852  int hop_limit = 0;
1853  int hop_limit_val;
1854  u32 ip_version_traffic_class_and_flow_label;
1855 
1856  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1857  {
1858  if (unformat (input, "version %d", &version_val))
1859  version = 1;
1860  else if (unformat (input, "traffic_class %d", &traffic_class_val))
1861  traffic_class = 1;
1862  else if (unformat (input, "flow_label %d", &flow_label_val))
1863  flow_label = 1;
1864  else if (unformat (input, "src %U", unformat_ip6_address, &src_val))
1865  src = 1;
1866  else if (unformat (input, "dst %U", unformat_ip6_address, &dst_val))
1867  dst = 1;
1868  else if (unformat (input, "proto %d", &proto_val))
1869  proto = 1;
1870  else if (unformat (input, "payload_length %d", &payload_length_val))
1871  payload_length = 1;
1872  else if (unformat (input, "hop_limit %d", &hop_limit_val))
1873  hop_limit = 1;
1874  else
1875  break;
1876  }
1877 
1878  if (version + traffic_class + flow_label + src + dst + proto +
1879  payload_length + hop_limit == 0)
1880  return 0;
1881 
1882  /*
1883  * Aligned because we use the real comparison functions
1884  */
1885  vec_validate_aligned (match, sizeof (*ip) - 1, sizeof (u32x4));
1886 
1887  ip = (ip6_header_t *) match;
1888 
1889  if (src)
1890  clib_memcpy_fast (&ip->src_address, &src_val, sizeof (ip->src_address));
1891 
1892  if (dst)
1893  clib_memcpy_fast (&ip->dst_address, &dst_val, sizeof (ip->dst_address));
1894 
1895  if (proto)
1896  ip->protocol = proto_val;
1897 
1898  ip_version_traffic_class_and_flow_label = 0;
1899 
1900  if (version)
1901  ip_version_traffic_class_and_flow_label |= (version_val & 0xF) << 28;
1902 
1903  if (traffic_class)
1904  ip_version_traffic_class_and_flow_label |=
1905  (traffic_class_val & 0xFF) << 20;
1906 
1907  if (flow_label)
1908  ip_version_traffic_class_and_flow_label |= (flow_label_val & 0xFFFFF);
1909 
1911  clib_host_to_net_u32 (ip_version_traffic_class_and_flow_label);
1912 
1913  if (payload_length)
1914  ip->payload_length = clib_host_to_net_u16 (payload_length_val);
1915 
1916  if (hop_limit)
1917  ip->hop_limit = hop_limit_val;
1918 
1919  *matchp = match;
1920  return 1;
1921 }
1922 
1923 uword
1924 unformat_l3_match (unformat_input_t * input, va_list * args)
1925 {
1926  u8 **matchp = va_arg (*args, u8 **);
1927 
1928  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1929  {
1930  if (unformat (input, "ip4 %U", unformat_ip4_match, matchp))
1931  return 1;
1932  else if (unformat (input, "ip6 %U", unformat_ip6_match, matchp))
1933  return 1;
1934  /* $$$$ add mpls */
1935  else
1936  break;
1937  }
1938  return 0;
1939 }
1940 
1941 uword
1942 unformat_vlan_tag (unformat_input_t * input, va_list * args)
1943 {
1944  u8 *tagp = va_arg (*args, u8 *);
1945  u32 tag;
1946 
1947  if (unformat (input, "%d", &tag))
1948  {
1949  tagp[0] = (tag >> 8) & 0x0F;
1950  tagp[1] = tag & 0xFF;
1951  return 1;
1952  }
1953 
1954  return 0;
1955 }
1956 
1957 uword
1958 unformat_l2_match (unformat_input_t * input, va_list * args)
1959 {
1960  u8 **matchp = va_arg (*args, u8 **);
1961  u8 *match = 0;
1962  u8 src = 0;
1963  u8 src_val[6];
1964  u8 dst = 0;
1965  u8 dst_val[6];
1966  u8 proto = 0;
1967  u16 proto_val;
1968  u8 tag1 = 0;
1969  u8 tag1_val[2];
1970  u8 tag2 = 0;
1971  u8 tag2_val[2];
1972  int len = 14;
1973  u8 ignore_tag1 = 0;
1974  u8 ignore_tag2 = 0;
1975  u8 cos1 = 0;
1976  u8 cos2 = 0;
1977  u32 cos1_val = 0;
1978  u32 cos2_val = 0;
1979 
1980  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1981  {
1982  if (unformat (input, "src %U", unformat_ethernet_address, &src_val))
1983  src = 1;
1984  else
1985  if (unformat (input, "dst %U", unformat_ethernet_address, &dst_val))
1986  dst = 1;
1987  else if (unformat (input, "proto %U",
1989  proto = 1;
1990  else if (unformat (input, "tag1 %U", unformat_vlan_tag, tag1_val))
1991  tag1 = 1;
1992  else if (unformat (input, "tag2 %U", unformat_vlan_tag, tag2_val))
1993  tag2 = 1;
1994  else if (unformat (input, "ignore-tag1"))
1995  ignore_tag1 = 1;
1996  else if (unformat (input, "ignore-tag2"))
1997  ignore_tag2 = 1;
1998  else if (unformat (input, "cos1 %d", &cos1_val))
1999  cos1 = 1;
2000  else if (unformat (input, "cos2 %d", &cos2_val))
2001  cos2 = 1;
2002  else
2003  break;
2004  }
2005  if ((src + dst + proto + tag1 + tag2 +
2006  ignore_tag1 + ignore_tag2 + cos1 + cos2) == 0)
2007  return 0;
2008 
2009  if (tag1 || ignore_tag1 || cos1)
2010  len = 18;
2011  if (tag2 || ignore_tag2 || cos2)
2012  len = 22;
2013 
2014  vec_validate_aligned (match, len - 1, sizeof (u32x4));
2015 
2016  if (dst)
2017  clib_memcpy_fast (match, dst_val, 6);
2018 
2019  if (src)
2020  clib_memcpy_fast (match + 6, src_val, 6);
2021 
2022  if (tag2)
2023  {
2024  /* inner vlan tag */
2025  match[19] = tag2_val[1];
2026  match[18] = tag2_val[0];
2027  if (cos2)
2028  match[18] |= (cos2_val & 0x7) << 5;
2029  if (proto)
2030  {
2031  match[21] = proto_val & 0xff;
2032  match[20] = proto_val >> 8;
2033  }
2034  if (tag1)
2035  {
2036  match[15] = tag1_val[1];
2037  match[14] = tag1_val[0];
2038  }
2039  if (cos1)
2040  match[14] |= (cos1_val & 0x7) << 5;
2041  *matchp = match;
2042  return 1;
2043  }
2044  if (tag1)
2045  {
2046  match[15] = tag1_val[1];
2047  match[14] = tag1_val[0];
2048  if (proto)
2049  {
2050  match[17] = proto_val & 0xff;
2051  match[16] = proto_val >> 8;
2052  }
2053  if (cos1)
2054  match[14] |= (cos1_val & 0x7) << 5;
2055 
2056  *matchp = match;
2057  return 1;
2058  }
2059  if (cos2)
2060  match[18] |= (cos2_val & 0x7) << 5;
2061  if (cos1)
2062  match[14] |= (cos1_val & 0x7) << 5;
2063  if (proto)
2064  {
2065  match[13] = proto_val & 0xff;
2066  match[12] = proto_val >> 8;
2067  }
2068 
2069  *matchp = match;
2070  return 1;
2071 }
2072 
2073 
2074 uword
2076 {
2077  vnet_classify_main_t *cm = va_arg (*args, vnet_classify_main_t *);
2078  u8 **matchp = va_arg (*args, u8 **);
2079  u32 table_index = va_arg (*args, u32);
2081 
2082  u8 *match = 0;
2083  u8 *l2 = 0;
2084  u8 *l3 = 0;
2085  u8 *l4 = 0;
2086 
2087  if (pool_is_free_index (cm->tables, table_index))
2088  return 0;
2089 
2090  t = pool_elt_at_index (cm->tables, table_index);
2091 
2092  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2093  {
2094  if (unformat (input, "hex %U", unformat_hex_string, &match))
2095  ;
2096  else if (unformat (input, "l2 %U", unformat_l2_match, &l2))
2097  ;
2098  else if (unformat (input, "l3 %U", unformat_l3_match, &l3))
2099  ;
2100  else if (unformat (input, "l4 %U", unformat_l4_match, &l4))
2101  ;
2102  else
2103  break;
2104  }
2105 
2106  if (l4 && !l3)
2107  {
2108  vec_free (match);
2109  vec_free (l2);
2110  vec_free (l4);
2111  return 0;
2112  }
2113 
2114  if (match || l2 || l3 || l4)
2115  {
2116  if (l2 || l3 || l4)
2117  {
2118  /* "Win a free Ethernet header in every packet" */
2119  if (l2 == 0)
2120  vec_validate_aligned (l2, 13, sizeof (u32x4));
2121  match = l2;
2122  if (l3)
2123  {
2124  vec_append_aligned (match, l3, sizeof (u32x4));
2125  vec_free (l3);
2126  }
2127  if (l4)
2128  {
2129  vec_append_aligned (match, l4, sizeof (u32x4));
2130  vec_free (l4);
2131  }
2132  }
2133 
2134  /* Make sure the vector is big enough even if key is all 0's */
2136  (match,
2137  ((t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4)) - 1,
2138  sizeof (u32x4));
2139 
2140  /* Set size, include skipped vectors */
2141  _vec_len (match) =
2142  (t->match_n_vectors + t->skip_n_vectors) * sizeof (u32x4);
2143 
2144  *matchp = match;
2145 
2146  return 1;
2147  }
2148 
2149  return 0;
2150 }
2151 
2152 int
2154  u32 table_index,
2155  u8 * match,
2156  u32 hit_next_index,
2157  u32 opaque_index,
2158  i32 advance,
2159  u8 action, u32 metadata, int is_add)
2160 {
2162  vnet_classify_entry_5_t _max_e __attribute__ ((aligned (16)));
2163  vnet_classify_entry_t *e;
2164  int i, rv;
2165 
2166  if (pool_is_free_index (cm->tables, table_index))
2167  return VNET_API_ERROR_NO_SUCH_TABLE;
2168 
2169  t = pool_elt_at_index (cm->tables, table_index);
2170 
2171  e = (vnet_classify_entry_t *) & _max_e;
2172  e->next_index = hit_next_index;
2173  e->opaque_index = opaque_index;
2174  e->advance = advance;
2175  e->hits = 0;
2176  e->last_heard = 0;
2177  e->flags = 0;
2178  e->action = action;
2179  if (e->action == CLASSIFY_ACTION_SET_IP4_FIB_INDEX)
2181  metadata,
2183  else if (e->action == CLASSIFY_ACTION_SET_IP6_FIB_INDEX)
2185  metadata,
2187  else if (e->action == CLASSIFY_ACTION_SET_METADATA)
2188  e->metadata = metadata;
2189  else
2190  e->metadata = 0;
2191 
2192  /* Copy key data, honoring skip_n_vectors */
2193  clib_memcpy_fast (&e->key, match + t->skip_n_vectors * sizeof (u32x4),
2194  t->match_n_vectors * sizeof (u32x4));
2195 
2196  /* Clear don't-care bits; likely when dynamically creating sessions */
2197  for (i = 0; i < t->match_n_vectors; i++)
2198  e->key[i] &= t->mask[i];
2199 
2200  rv = vnet_classify_add_del (t, e, is_add);
2201 
2203 
2204  if (rv)
2205  return VNET_API_ERROR_NO_SUCH_ENTRY;
2206  return 0;
2207 }
2208 
2209 static clib_error_t *
2211  unformat_input_t * input,
2212  vlib_cli_command_t * cmd)
2213 {
2215  int is_add = 1;
2216  u32 table_index = ~0;
2217  u32 hit_next_index = ~0;
2218  u64 opaque_index = ~0;
2219  u8 *match = 0;
2220  i32 advance = 0;
2221  u32 action = 0;
2222  u32 metadata = 0;
2223  int i, rv;
2224 
2225  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2226  {
2227  if (unformat (input, "del"))
2228  is_add = 0;
2229  else if (unformat (input, "hit-next %U", unformat_ip_next_index,
2230  &hit_next_index))
2231  ;
2232  else
2233  if (unformat
2234  (input, "l2-input-hit-next %U", unformat_l2_input_next_index,
2235  &hit_next_index))
2236  ;
2237  else
2238  if (unformat
2239  (input, "l2-output-hit-next %U", unformat_l2_output_next_index,
2240  &hit_next_index))
2241  ;
2242  else if (unformat (input, "acl-hit-next %U", unformat_acl_next_index,
2243  &hit_next_index))
2244  ;
2245  else if (unformat (input, "policer-hit-next %U",
2246  unformat_policer_next_index, &hit_next_index))
2247  ;
2248  else if (unformat (input, "opaque-index %lld", &opaque_index))
2249  ;
2250  else if (unformat (input, "match %U", unformat_classify_match,
2251  cm, &match, table_index))
2252  ;
2253  else if (unformat (input, "advance %d", &advance))
2254  ;
2255  else if (unformat (input, "table-index %d", &table_index))
2256  ;
2257  else if (unformat (input, "action set-ip4-fib-id %d", &metadata))
2258  action = 1;
2259  else if (unformat (input, "action set-ip6-fib-id %d", &metadata))
2260  action = 2;
2261  else if (unformat (input, "action set-sr-policy-index %d", &metadata))
2262  action = 3;
2263  else
2264  {
2265  /* Try registered opaque-index unformat fns */
2266  for (i = 0; i < vec_len (cm->unformat_opaque_index_fns); i++)
2267  {
2268  if (unformat (input, "%U", cm->unformat_opaque_index_fns[i],
2269  &opaque_index))
2270  goto found_opaque;
2271  }
2272  break;
2273  }
2274  found_opaque:
2275  ;
2276  }
2277 
2278  if (table_index == ~0)
2279  return clib_error_return (0, "Table index required");
2280 
2281  if (is_add && match == 0)
2282  return clib_error_return (0, "Match value required");
2283 
2284  rv = vnet_classify_add_del_session (cm, table_index, match,
2285  hit_next_index,
2286  opaque_index, advance,
2287  action, metadata, is_add);
2288 
2289  switch (rv)
2290  {
2291  case 0:
2292  break;
2293 
2294  default:
2295  return clib_error_return (0,
2296  "vnet_classify_add_del_session returned %d",
2297  rv);
2298  }
2299 
2300  return 0;
2301 }
2302 
2303 /* *INDENT-OFF* */
2304 VLIB_CLI_COMMAND (classify_session_command, static) = {
2305  .path = "classify session",
2306  .short_help =
2307  "classify session [hit-next|l2-input-hit-next|l2-output-hit-next|"
2308  "acl-hit-next <next_index>|policer-hit-next <policer_name>]"
2309  "\n table-index <nn> match [hex] [l2] [l3 ip4] [opaque-index <index>]"
2310  "\n [action set-ip4-fib-id|set-ip6-fib-id|set-sr-policy-index <n>] [del]",
2311  .function = classify_session_command_fn,
2312 };
2313 /* *INDENT-ON* */
2314 
2315 static uword
2317 {
2318  u64 *opaquep = va_arg (*args, u64 *);
2319  u32 sw_if_index;
2320 
2321  if (unformat (input, "opaque-sw_if_index %U", unformat_vnet_sw_interface,
2322  vnet_get_main (), &sw_if_index))
2323  {
2324  *opaquep = sw_if_index;
2325  return 1;
2326  }
2327  return 0;
2328 }
2329 
2330 static uword
2331 unformat_ip_next_node (unformat_input_t * input, va_list * args)
2332 {
2334  u32 *next_indexp = va_arg (*args, u32 *);
2335  u32 node_index;
2336  u32 next_index = ~0;
2337 
2338  if (unformat (input, "ip6-node %U", unformat_vlib_node,
2339  cm->vlib_main, &node_index))
2340  {
2341  next_index = vlib_node_add_next (cm->vlib_main,
2342  ip6_classify_node.index, node_index);
2343  }
2344  else if (unformat (input, "ip4-node %U", unformat_vlib_node,
2345  cm->vlib_main, &node_index))
2346  {
2347  next_index = vlib_node_add_next (cm->vlib_main,
2348  ip4_classify_node.index, node_index);
2349  }
2350  else
2351  return 0;
2352 
2353  *next_indexp = next_index;
2354  return 1;
2355 }
2356 
2357 static uword
2358 unformat_acl_next_node (unformat_input_t * input, va_list * args)
2359 {
2361  u32 *next_indexp = va_arg (*args, u32 *);
2362  u32 node_index;
2363  u32 next_index;
2364 
2365  if (unformat (input, "ip6-node %U", unformat_vlib_node,
2366  cm->vlib_main, &node_index))
2367  {
2368  next_index = vlib_node_add_next (cm->vlib_main,
2369  ip6_inacl_node.index, node_index);
2370  }
2371  else if (unformat (input, "ip4-node %U", unformat_vlib_node,
2372  cm->vlib_main, &node_index))
2373  {
2374  next_index = vlib_node_add_next (cm->vlib_main,
2375  ip4_inacl_node.index, node_index);
2376  }
2377  else
2378  return 0;
2379 
2380  *next_indexp = next_index;
2381  return 1;
2382 }
2383 
2384 static uword
2386 {
2388  u32 *next_indexp = va_arg (*args, u32 *);
2389  u32 node_index;
2390  u32 next_index;
2391 
2392  if (unformat (input, "input-node %U", unformat_vlib_node,
2393  cm->vlib_main, &node_index))
2394  {
2395  next_index = vlib_node_add_next
2396  (cm->vlib_main, l2_input_classify_node.index, node_index);
2397 
2398  *next_indexp = next_index;
2399  return 1;
2400  }
2401  return 0;
2402 }
2403 
2404 static uword
2406 {
2408  u32 *next_indexp = va_arg (*args, u32 *);
2409  u32 node_index;
2410  u32 next_index;
2411 
2412  if (unformat (input, "output-node %U", unformat_vlib_node,
2413  cm->vlib_main, &node_index))
2414  {
2415  next_index = vlib_node_add_next
2416  (cm->vlib_main, l2_output_classify_node.index, node_index);
2417 
2418  *next_indexp = next_index;
2419  return 1;
2420  }
2421  return 0;
2422 }
2423 
2424 static clib_error_t *
2426 {
2428 
2429  cm->vlib_main = vm;
2430  cm->vnet_main = vnet_get_main ();
2431 
2434 
2436 
2439 
2442 
2444 
2445  return 0;
2446 }
2447 
2449 
2450 #define TEST_CODE 1
2451 
2452 #if TEST_CODE > 0
2453 
2454 typedef struct
2455 {
2458 } test_entry_t;
2459 
2460 typedef struct
2461 {
2463 
2464  /* test parameters */
2472  int verbose;
2473 
2474  /* Random seed */
2476 
2477  /* Test data */
2478  classify_data_or_mask_t *mask;
2479  classify_data_or_mask_t *data;
2480 
2481  /* convenience */
2484 
2486 
2488 
2489 static clib_error_t *
2491 {
2492  classify_data_or_mask_t *mask, *data;
2493  vlib_main_t *vm = tm->vlib_main;
2494  test_entry_t *ep;
2495  u8 *mp = 0, *dp = 0;
2496  u32 tmp;
2497  int i, rv;
2498 
2499  vec_validate_aligned (mp, 3 * sizeof (u32x4), sizeof (u32x4));
2500  vec_validate_aligned (dp, 3 * sizeof (u32x4), sizeof (u32x4));
2501 
2502  mask = (classify_data_or_mask_t *) mp;
2503  data = (classify_data_or_mask_t *) dp;
2504 
2505  /* Mask on src address */
2506  clib_memset (&mask->ip.src_address, 0xff, 4);
2507 
2508  tmp = clib_host_to_net_u32 (tm->src.as_u32);
2509 
2510  for (i = 0; i < tm->sessions; i++)
2511  {
2512  vec_add2 (tm->entries, ep, 1);
2513  ep->addr.as_u32 = clib_host_to_net_u32 (tmp);
2514  ep->in_table = 0;
2515  tmp++;
2516  }
2517 
2519  (u8 *) mask,
2520  tm->buckets,
2521  tm->memory_size, 0 /* skip */ ,
2522  3 /* vectors to match */ );
2524  tm->table_index = tm->table - tm->classify_main->tables;
2525  vlib_cli_output (vm, "Created table %d, buckets %d",
2526  tm->table_index, tm->buckets);
2527 
2528  vlib_cli_output (vm, "Initialize: add %d (approx. half of %d sessions)...",
2529  tm->sessions / 2, tm->sessions);
2530 
2531  for (i = 0; i < tm->sessions / 2; i++)
2532  {
2533  ep = vec_elt_at_index (tm->entries, i);
2534 
2535  data->ip.src_address.as_u32 = ep->addr.as_u32;
2536  ep->in_table = 1;
2537 
2539  tm->table_index,
2540  (u8 *) data,
2542  i /* opaque_index */ ,
2543  0 /* advance */ ,
2544  0 /* action */ ,
2545  0 /* metadata */ ,
2546  1 /* is_add */ );
2547 
2548  if (rv != 0)
2549  clib_warning ("add: returned %d", rv);
2550 
2551  if (tm->verbose)
2552  vlib_cli_output (vm, "add: %U", format_ip4_address, &ep->addr.as_u32);
2553  }
2554 
2555  vlib_cli_output (vm, "Execute %d random add/delete operations",
2556  tm->iterations);
2557 
2558  for (i = 0; i < tm->iterations; i++)
2559  {
2560  int index, is_add;
2561 
2562  /* Pick a random entry */
2563  index = random_u32 (&tm->seed) % tm->sessions;
2564 
2565  ep = vec_elt_at_index (tm->entries, index);
2566 
2567  data->ip.src_address.as_u32 = ep->addr.as_u32;
2568 
2569  /* If it's in the table, remove it. Else, add it */
2570  is_add = !ep->in_table;
2571 
2572  if (tm->verbose)
2573  vlib_cli_output (vm, "%s: %U",
2574  is_add ? "add" : "del",
2576 
2578  tm->table_index,
2579  (u8 *) data,
2581  i /* opaque_index */ ,
2582  0 /* advance */ ,
2583  0 /* action */ ,
2584  0 /* metadata */ ,
2585  is_add);
2586  if (rv != 0)
2587  vlib_cli_output (vm,
2588  "%s[%d]: %U returned %d", is_add ? "add" : "del",
2589  index, format_ip4_address, &ep->addr.as_u32, rv);
2590  else
2591  ep->in_table = is_add;
2592  }
2593 
2594  vlib_cli_output (vm, "Remove remaining %d entries from the table",
2595  tm->table->active_elements);
2596 
2597  for (i = 0; i < tm->sessions; i++)
2598  {
2599  u8 *key_minus_skip;
2600  u64 hash;
2601  vnet_classify_entry_t *e;
2602 
2603  ep = tm->entries + i;
2604  if (ep->in_table == 0)
2605  continue;
2606 
2607  data->ip.src_address.as_u32 = ep->addr.as_u32;
2608 
2609  hash = vnet_classify_hash_packet (tm->table, (u8 *) data);
2610 
2612  (u8 *) data, hash, 0 /* time_now */ );
2613  if (e == 0)
2614  {
2615  clib_warning ("Couldn't find %U index %d which should be present",
2616  format_ip4_address, ep->addr, i);
2617  continue;
2618  }
2619 
2620  key_minus_skip = (u8 *) e->key;
2621  key_minus_skip -= tm->table->skip_n_vectors * sizeof (u32x4);
2622 
2624  (tm->classify_main,
2625  tm->table_index,
2626  key_minus_skip, IP_LOOKUP_NEXT_DROP, i /* opaque_index */ ,
2627  0 /* advance */ , 0, 0,
2628  0 /* is_add */ );
2629 
2630  if (rv != 0)
2631  clib_warning ("del: returned %d", rv);
2632 
2633  if (tm->verbose)
2634  vlib_cli_output (vm, "del: %U", format_ip4_address, &ep->addr.as_u32);
2635  }
2636 
2637  vlib_cli_output (vm, "%d entries remain, MUST be zero",
2638  tm->table->active_elements);
2639 
2640  vlib_cli_output (vm, "Table after cleanup: \n%U\n",
2641  format_classify_table, tm->table, 0 /* verbose */ );
2642 
2643  vec_free (mp);
2644  vec_free (dp);
2645 
2647  tm->table_index, 1 /* del_chain */ );
2648  tm->table = 0;
2649  tm->table_index = ~0;
2650  vec_free (tm->entries);
2651 
2652  return 0;
2653 }
2654 
2655 static clib_error_t *
2657  unformat_input_t * input, vlib_cli_command_t * cmd)
2658 {
2661  u32 tmp;
2662  int which = 0;
2663  clib_error_t *error = 0;
2664 
2665  tm->buckets = 1024;
2666  tm->sessions = 8192;
2667  tm->iterations = 8192;
2668  tm->memory_size = 64 << 20;
2669  tm->src.as_u32 = clib_net_to_host_u32 (0x0100000A);
2670  tm->table = 0;
2671  tm->seed = 0xDEADDABE;
2672  tm->classify_main = cm;
2673  tm->vlib_main = vm;
2674  tm->verbose = 0;
2675 
2676  /* Default starting address 1.0.0.10 */
2677 
2678  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
2679  {
2680  if (unformat (input, "sessions %d", &tmp))
2681  tm->sessions = tmp;
2682  else
2683  if (unformat (input, "src %U", unformat_ip4_address, &tm->src.as_u32))
2684  ;
2685  else if (unformat (input, "buckets %d", &tm->buckets))
2686  ;
2687  else if (unformat (input, "memory-size %uM", &tmp))
2688  tm->memory_size = tmp << 20;
2689  else if (unformat (input, "memory-size %uG", &tmp))
2690  tm->memory_size = tmp << 30;
2691  else if (unformat (input, "seed %d", &tm->seed))
2692  ;
2693  else if (unformat (input, "verbose"))
2694  tm->verbose = 1;
2695 
2696  else if (unformat (input, "iterations %d", &tm->iterations))
2697  ;
2698  else if (unformat (input, "churn-test"))
2699  which = 0;
2700  else
2701  break;
2702  }
2703 
2704  switch (which)
2705  {
2706  case 0:
2707  error = test_classify_churn (tm);
2708  break;
2709  default:
2710  error = clib_error_return (0, "No such test");
2711  break;
2712  }
2713 
2714  return error;
2715 }
2716 
2717 /* *INDENT-OFF* */
2718 VLIB_CLI_COMMAND (test_classify_command, static) = {
2719  .path = "test classify",
2720  .short_help =
2721  "test classify [src <ip>] [sessions <nn>] [buckets <nn>] [seed <nnn>]\n"
2722  " [memory-size <nn>[M|G]]\n"
2723  " [churn-test]",
2724  .function = test_classify_command_fn,
2725 };
2726 /* *INDENT-ON* */
2727 #endif /* TEST_CODE */
2728 
2729 /*
2730  * fd.io coding-style-patch-verification: ON
2731  *
2732  * Local Variables:
2733  * eval: (c-set-style "gnu")
2734  * End:
2735  */
u64 vnet_classify_hash_packet(vnet_classify_table_t *t, u8 *h)
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:439
u32 sw_if_index
Definition: ipsec_gre.api:37
#define foreach_ip_next
vnet_classify_entry_t ** working_copies
void clib_mem_validate(void)
Definition: mem_dlmalloc.c:390
uword unformat_classify_mask(unformat_input_t *input, va_list *args)
void rogue(vnet_classify_table_t *t)
Definition: vnet_classify.c:74
static clib_error_t * show_classify_tables_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
ip4_address_t src_address
Definition: ip4_packet.h:170
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
classify_data_or_mask_t * mask
static vnet_classify_entry_t * vnet_classify_find_entry_inline(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
unsigned long u64
Definition: types.h:89
void * mheap_alloc(void *memory, uword size)
Definition: mheap.c:963
#define VNET_CLASSIFY_ENTRY_FREE
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static u8 * format_vnet_classify_table(u8 *s, va_list *args)
unformat_function_t unformat_hex_string
Definition: format.h:288
uword unformat_vlan_tag(unformat_input_t *input, va_list *args)
static clib_error_t * classify_table_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
static void vnet_classify_entry_claim_resource(vnet_classify_entry_t *e)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:560
for(i=1;i<=collision_buckets;i++)
int i
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
unformat_function_t unformat_vnet_sw_interface
u8 data[128]
Definition: ipsec.api:248
vl_api_ip4_address_t dst
Definition: ipsec_gre.api:39
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:450
#define foreach_ip6_proto_field
static clib_error_t * test_classify_churn(test_classify_main_t *tm)
void vnet_classify_register_unformat_acl_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:96
struct _tcp_header tcp_header_t
ip6_address_t src_address
Definition: ip6_packet.h:385
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1092
unsigned char u8
Definition: types.h:56
vlib_node_registration_t ip4_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_classify_node)
Definition: ip_classify.c:320
uword unformat_l2_output_next_index(unformat_input_t *input, va_list *args)
double f64
Definition: types.h:142
#define foreach_acl_next
u16 src_port
Definition: udp.api:41
vnet_classify_main_t * classify_main
format_function_t format_ip4_address
Definition: format.h:75
void mv(vnet_classify_table_t *t)
Definition: vnet_classify.c:69
vlib_node_registration_t l2_input_classify_node
(constructor) VLIB_REGISTER_NODE (l2_input_classify_node)
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
unformat_function_t unformat_ip4_address
Definition: format.h:70
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:163
int vnet_classify_add_del(vnet_classify_table_t *t, vnet_classify_entry_t *add_v, int is_add)
static clib_error_t * classify_session_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
ip4_address_t dst_address
Definition: ip4_packet.h:170
uword unformat_l2_mask(unformat_input_t *input, va_list *args)
vlib_node_registration_t l2_output_classify_node
(constructor) VLIB_REGISTER_NODE (l2_output_classify_node)
static uword unformat_l2_input_next_node(unformat_input_t *input, va_list *args)
uword unformat_classify_match(unformat_input_t *input, va_list *args)
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)
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u8 * format_hex_bytes(u8 *s, va_list *va)
Definition: std-formats.c:84
#define clib_error_return(e, args...)
Definition: error.h:99
#define foreach_ip4_proto_field
unsigned int u32
Definition: types.h:88
#define mheap_free(v)
Definition: mheap.h:65
#define clib_atomic_test_and_set(a)
Definition: atomics.h:40
static uword unformat_opaque_sw_if_index(unformat_input_t *input, va_list *args)
static int vnet_classify_entry_is_free(vnet_classify_entry_t *e)
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)
u8 * format_mheap(u8 *s, va_list *va)
Definition: mem_dlmalloc.c:354
static u64 vnet_classify_hash_packet_inline(vnet_classify_table_t *t, u8 *h)
uword unformat_l4_match(unformat_input_t *input, va_list *args)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
static test_classify_main_t test_classify_main
uword unformat_ip4_mask(unformat_input_t *input, va_list *args)
uword unformat_l2_input_next_index(unformat_input_t *input, va_list *args)
uword unformat_ip6_mask(unformat_input_t *input, va_list *args)
DLMALLOC_EXPORT void mspace_disable_expand(mspace msp)
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
u64 memory_size
Definition: vhost_user.h:115
void vnet_classify_delete_table_index(vnet_classify_main_t *cm, u32 table_index, int del_chain)
ip4_address_t addr
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
static vnet_classify_entry_t * split_and_rehash_linear(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
#define PREDICT_FALSE(x)
Definition: clib.h:111
void vnet_classify_register_unformat_opaque_index_fn(unformat_function_t *fn)
vl_api_address_union_t src_address
Definition: ip_types.api:44
vl_api_ip4_address_t src
Definition: ipsec_gre.api:38
uword() unformat_function_t(unformat_input_t *input, va_list *args)
Definition: format.h:232
static vnet_classify_entry_t * vnet_classify_entry_at_index(vnet_classify_table_t *t, vnet_classify_entry_t *e, u32 index)
void fib_table_unlock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Take a reference counting lock on the table.
Definition: fib_table.c:1237
uword unformat_policer_next_index(unformat_input_t *input, va_list *args)
u8 len
Definition: ip_types.api:49
vlib_node_registration_t ip4_inacl_node
(constructor) VLIB_REGISTER_NODE (ip4_inacl_node)
#define foreach_udp_proto_field
unformat_function_t unformat_ip6_address
Definition: format.h:91
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P with alignment A.
Definition: pool.h:230
static vnet_classify_entry_t * split_and_rehash(vnet_classify_table_t *t, vnet_classify_entry_t *old_values, u32 old_log2_pages, u32 new_log2_pages)
uword unformat_udp_mask(unformat_input_t *input, va_list *args)
static uword vnet_classify_get_offset(vnet_classify_table_t *t, vnet_classify_entry_t *v)
static u8 * format_classify_entry(u8 *s, va_list *args)
uword unformat_l2_match(unformat_input_t *input, va_list *args)
vnet_classify_bucket_t saved_bucket
Adjacency to drop this packet.
Definition: adj.h:53
#define UNFORMAT_END_OF_INPUT
Definition: format.h:144
static uword unformat_acl_next_node(unformat_input_t *input, va_list *args)
DLMALLOC_EXPORT mspace create_mspace(size_t capacity, int locked)
static_always_inline uword vlib_get_thread_index(void)
Definition: threads.h:212
vlib_main_t * vm
Definition: buffer.c:312
vlib_node_registration_t ip6_inacl_node
(constructor) VLIB_REGISTER_NODE (ip6_inacl_node)
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:341
uword unformat_acl_next_index(unformat_input_t *input, va_list *args)
classify_data_or_mask_t * data
static void * clib_mem_set_heap(void *heap)
Definition: mem.h:261
static uword unformat_ip_next_node(unformat_input_t *input, va_list *args)
static void vnet_classify_entry_free(vnet_classify_table_t *t, vnet_classify_entry_t *v, u32 log2_pages)
u8 * format_classify_table(u8 *s, va_list *args)
#define clib_warning(format, args...)
Definition: error.h:59
uword unformat_l3_match(unformat_input_t *input, va_list *args)
test_entry_t * entries
uword unformat_ip4_match(unformat_input_t *input, va_list *args)
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
static clib_error_t * vnet_classify_init(vlib_main_t *vm)
void fib_table_lock(u32 fib_index, fib_protocol_t proto, fib_source_t source)
Release a reference counting lock on the table.
Definition: fib_table.c:1257
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:155
struct _vnet_classify_main vnet_classify_main_t
Definition: vnet_classify.h:67
vlib_main_t * vlib_main
static uword unformat_l2_output_next_node(unformat_input_t *input, va_list *args)
static void make_working_copy(vnet_classify_table_t *t, vnet_classify_bucket_t *b)
uword unformat_l3_mask(unformat_input_t *input, va_list *args)
int in_table
signed int i32
Definition: types.h:77
uword unformat_ethernet_address(unformat_input_t *input, va_list *args)
Definition: format.c:233
#define ASSERT(truth)
#define vec_delete(V, N, M)
Delete N elements starting at element M.
Definition: vec.h:784
u8 is_add
Definition: ipsec_gre.api:36
uword unformat_ip6_match(unformat_input_t *input, va_list *args)
Classify.
Definition: fib_entry.h:44
static void clib_mem_free(void *p)
Definition: mem.h:205
u8 log2_pages
Definition: bihash_doc.h:62
u32 fib_table_find_or_create_and_lock(fib_protocol_t proto, u32 table_id, fib_source_t src)
Get the index of the FIB for a Table-ID.
Definition: fib_table.c:1123
option version
Definition: memclnt.api:17
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:818
uword unformat_ip_next_index(unformat_input_t *input, va_list *args)
vnet_classify_main_t vnet_classify_main
Definition: vnet_classify.c:22
static void vnet_classify_entry_release_resource(vnet_classify_entry_t *e)
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, int is_add, int del_chain)
static int vnet_classify_entry_is_busy(vnet_classify_entry_t *e)
u32 ip_version_traffic_class_and_flow_label
Definition: ip6_packet.h:372
static clib_error_t * test_classify_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
u16 payload_length
Definition: ip6_packet.h:376
uword unformat_tcp_mask(unformat_input_t *input, va_list *args)
uword unformat_ethernet_type_host_byte_order(unformat_input_t *input, va_list *args)
Definition: format.c:249
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
vnet_classify_bucket_t * buckets
void vnet_classify_register_unformat_ip_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:88
static uword max_log2(uword x)
Definition: clib.h:191
typedef CLIB_PACKED(struct { ethernet_header_t eh;ip4_header_t ip;})
u64 uword
Definition: types.h:112
uword unformat_l4_mask(unformat_input_t *input, va_list *args)
#define vec_append_aligned(v1, v2, align)
Append v2 after v1.
Definition: vec.h:834
volatile u32 * writer_lock
static vnet_classify_entry_t * vnet_classify_entry_alloc(vnet_classify_table_t *t, u32 log2_pages)
unformat_function_t unformat_vlib_node
Definition: node_funcs.h:1147
DLMALLOC_EXPORT size_t destroy_mspace(mspace msp)
static void * clib_mem_alloc_aligned(uword size, uword align)
Definition: mem.h:140
static u32 random_u32(u32 *seed)
32-bit random number generator
Definition: random.h:69
unsigned long long u32x4
Definition: ixge.c:28
#define foreach_l2_output_next
#define foreach_l2_input_next
#define CLIB_MEMORY_BARRIER()
Definition: clib.h:115
void vnet_classify_register_unformat_policer_next_index_fn(unformat_function_t *fn)
u16 dst_port
Definition: udp.api:42
u8 ip_version_and_header_length
Definition: ip4_packet.h:138
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:341
#define foreach_tcp_proto_field
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:762
vnet_classify_entry_t ** freelists
vnet_classify_entry_t * vnet_classify_find_entry(vnet_classify_table_t *t, u8 *h, u64 hash, f64 now)
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:972
u8 protocol
Definition: ipsec.api:96
void vnet_classify_register_unformat_l2_next_index_fn(unformat_function_t *fn)
Definition: vnet_classify.c:80
static vnet_classify_entry_t * vnet_classify_get_entry(vnet_classify_table_t *t, uword offset)
ip6_address_t dst_address
Definition: ip6_packet.h:385
static uword unformat_check_input(unformat_input_t *i)
Definition: format.h:170
signed short i16
Definition: types.h:46
vnet_classify_table_t * table