196a24172cc59e09a5e35cfbf0114f2dbe84d2fc
[vpp.git] / src / vnet / classify / vnet_classify.h
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 #ifndef __included_vnet_classify_h__
16 #define __included_vnet_classify_h__
17
18 #include <vnet/vnet.h>
19 #include <vnet/api_errno.h>     /* for API error numbers */
20
21 #include <vppinfra/error.h>
22 #include <vppinfra/hash.h>
23 #include <vppinfra/cache.h>
24 #include <vppinfra/crc32.h>
25 #include <vppinfra/xxhash.h>
26
27 extern vlib_node_registration_t ip4_classify_node;
28 extern vlib_node_registration_t ip6_classify_node;
29
30 #define CLASSIFY_TRACE 0
31
32 /*
33  * Classify table option to process packets
34  *  CLASSIFY_FLAG_USE_CURR_DATA:
35  *   - classify packets starting from VPP node’s current data pointer
36  */
37 typedef enum vnet_classify_flags_t_
38 {
39   CLASSIFY_FLAG_NONE = 0,
40   CLASSIFY_FLAG_USE_CURR_DATA = (1 << 0),
41 } __clib_packed vnet_classify_flags_t;
42
43 /*
44  * Classify session action
45  *  CLASSIFY_ACTION_SET_IP4_FIB_INDEX:
46  *   - Classified IP packets will be looked up
47  *     from the specified ipv4 fib table
48  *  CLASSIFY_ACTION_SET_IP6_FIB_INDEX:
49  *   - Classified IP packets will be looked up
50  *     from the specified ipv6 fib table
51  */
52 typedef enum vnet_classify_action_t_
53 {
54   CLASSIFY_ACTION_NONE = 0,
55   CLASSIFY_ACTION_SET_IP4_FIB_INDEX = 1,
56   CLASSIFY_ACTION_SET_IP6_FIB_INDEX = 2,
57   CLASSIFY_ACTION_SET_METADATA = 3,
58 } __clib_packed vnet_classify_action_t;
59
60 struct _vnet_classify_main;
61 typedef struct _vnet_classify_main vnet_classify_main_t;
62
63 #define foreach_size_in_u32x4                   \
64 _(1)                                            \
65 _(2)                                            \
66 _(3)                                            \
67 _(4)                                            \
68 _(5)
69
70 typedef struct _vnet_classify_entry
71 {
72   /* put into vnet_buffer(b)->l2_classfy.opaque_index */
73   union
74   {
75     struct
76     {
77       u32 opaque_index;
78       /* advance on hit, note it's a signed quantity... */
79       i32 advance;
80     };
81     u64 opaque_count;
82   };
83   /* Hit counter */
84   union
85   {
86     u64 hits;
87     struct _vnet_classify_entry *next_free;
88   };
89   /* last heard time */
90   f64 last_heard;
91
92   /* Really only need 1 bit */
93   u8 flags;
94 #define VNET_CLASSIFY_ENTRY_FREE        (1<<0)
95
96   vnet_classify_action_t action;
97   u16 metadata;
98   /* Graph node next index */
99   u32 next_index;
100
101   /* Must be aligned to a 16-octet boundary */
102   u32x4 key[0];
103 } vnet_classify_entry_t;
104
105 /**
106  * Check there's no padding in the entry. the key lies on a 16 byte boundary.
107  */
108 STATIC_ASSERT_OFFSET_OF (vnet_classify_entry_t, key, 32);
109
110 static inline int
111 vnet_classify_entry_is_free (vnet_classify_entry_t * e)
112 {
113   return e->flags & VNET_CLASSIFY_ENTRY_FREE;
114 }
115
116 static inline int
117 vnet_classify_entry_is_busy (vnet_classify_entry_t * e)
118 {
119   return ((e->flags & VNET_CLASSIFY_ENTRY_FREE) == 0);
120 }
121
122 /* Need these to con the vector allocator */
123 #define _(size)                                                               \
124   typedef struct                                                              \
125   {                                                                           \
126     vnet_classify_entry_t e;                                                  \
127     u32x4 key[size];                                                          \
128   } __clib_packed vnet_classify_entry_##size##_t;
129 foreach_size_in_u32x4;
130 #undef _
131
132 typedef struct
133 {
134   union
135   {
136     struct
137     {
138       u32 offset;
139       u8 linear_search;
140       u8 pad[2];
141       u8 log2_pages;
142     };
143     u64 as_u64;
144   };
145 } vnet_classify_bucket_t;
146
147 typedef struct
148 {
149   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
150   /* hash Buckets */
151   vnet_classify_bucket_t *buckets;
152
153   /* Private allocation arena, protected by the writer lock,
154    * where the entries are stored. */
155   void *mheap;
156
157   /* User/client data associated with the table */
158   uword user_ctx;
159
160   u32 nbuckets;
161   u32 log2_nbuckets;
162   u32 entries_per_page;
163   u32 skip_n_vectors;
164   u32 match_n_vectors;
165
166   /* Index of next table to try */
167   u32 next_table_index;
168
169   /* packet offsets */
170   i16 current_data_offset;
171   vnet_classify_flags_t current_data_flag;
172   /* Miss next index, return if next_table_index = 0 */
173   u32 miss_next_index;
174
175   /**
176    * All members accessed in the DP above here
177    */
178   CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
179
180   /* Config parameters */
181   u32 linear_buckets;
182   u32 active_elements;
183   u32 data_offset;
184
185   /* Per-bucket working copies, one per thread */
186   vnet_classify_entry_t **working_copies;
187   int *working_copy_lengths;
188   vnet_classify_bucket_t saved_bucket;
189
190   /* Free entry freelists */
191   vnet_classify_entry_t **freelists;
192
193   /* Writer (only) lock for this table */
194   clib_spinlock_t writer_lock;
195
196   CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
197   /* Mask to apply after skipping N vectors */
198   u32x4 mask[8];
199
200 } vnet_classify_table_t;
201
202 /**
203  * Ensure DP fields don't spill over to cache-line 2
204  */
205 STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
206                          CLIB_CACHE_LINE_BYTES);
207
208 /**
209  * The vector size for the classifier
210  *  in the add/del table 'match' is the number of vectors of this size
211  */
212 #define VNET_CLASSIFY_VECTOR_SIZE                                             \
213   sizeof (((vnet_classify_table_t *) 0)->mask[0])
214
215 struct _vnet_classify_main
216 {
217   /* Table pool */
218   vnet_classify_table_t *tables;
219
220   /* Registered next-index, opaque unformat fcns */
221   unformat_function_t **unformat_l2_next_index_fns;
222   unformat_function_t **unformat_ip_next_index_fns;
223   unformat_function_t **unformat_acl_next_index_fns;
224   unformat_function_t **unformat_policer_next_index_fns;
225   unformat_function_t **unformat_opaque_index_fns;
226
227   /* Per-interface filter table.  [0] is used for pcap */
228   u32 *classify_table_index_by_sw_if_index;
229
230   /* convenience variables */
231   vlib_main_t *vlib_main;
232   vnet_main_t *vnet_main;
233 };
234
235 extern vnet_classify_main_t vnet_classify_main;
236
237 u8 *format_classify_table (u8 * s, va_list * args);
238 u8 *format_vnet_classify_table (u8 *s, va_list *args);
239
240 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
241
242 static_always_inline vnet_classify_table_t *
243 vnet_classify_table_get (u32 table_index)
244 {
245   vnet_classify_main_t *vcm = &vnet_classify_main;
246
247   return (pool_elt_at_index (vcm->tables, table_index));
248 }
249
250 static inline u64
251 vnet_classify_hash_packet_inline (vnet_classify_table_t *t, const u8 *h)
252 {
253   u32x4 *mask;
254
255   union
256   {
257     u32x4 as_u32x4;
258     u64 as_u64[2];
259   } xor_sum __attribute__ ((aligned (sizeof (u32x4))));
260
261   ASSERT (t);
262   mask = t->mask;
263 #ifdef CLIB_HAVE_VEC128
264   u32x4u *data = (u32x4u *) h;
265   xor_sum.as_u32x4 = data[0 + t->skip_n_vectors] & mask[0];
266   switch (t->match_n_vectors)
267     {
268     case 5:
269       xor_sum.as_u32x4 ^= data[4 + t->skip_n_vectors] & mask[4];
270       /* FALLTHROUGH */
271     case 4:
272       xor_sum.as_u32x4 ^= data[3 + t->skip_n_vectors] & mask[3];
273       /* FALLTHROUGH */
274     case 3:
275       xor_sum.as_u32x4 ^= data[2 + t->skip_n_vectors] & mask[2];
276       /* FALLTHROUGH */
277     case 2:
278       xor_sum.as_u32x4 ^= data[1 + t->skip_n_vectors] & mask[1];
279       /* FALLTHROUGH */
280     case 1:
281       break;
282     default:
283       abort ();
284     }
285 #else
286   u32 skip_u64 = t->skip_n_vectors * 2;
287   u64 *data64 = (u64 *) h;
288   xor_sum.as_u64[0] = data64[0 + skip_u64] & ((u64 *) mask)[0];
289   xor_sum.as_u64[1] = data64[1 + skip_u64] & ((u64 *) mask)[1];
290   switch (t->match_n_vectors)
291     {
292     case 5:
293       xor_sum.as_u64[0] ^= data64[8 + skip_u64] & ((u64 *) mask)[8];
294       xor_sum.as_u64[1] ^= data64[9 + skip_u64] & ((u64 *) mask)[9];
295       /* FALLTHROUGH */
296     case 4:
297       xor_sum.as_u64[0] ^= data64[6 + skip_u64] & ((u64 *) mask)[6];
298       xor_sum.as_u64[1] ^= data64[7 + skip_u64] & ((u64 *) mask)[7];
299       /* FALLTHROUGH */
300     case 3:
301       xor_sum.as_u64[0] ^= data64[4 + skip_u64] & ((u64 *) mask)[4];
302       xor_sum.as_u64[1] ^= data64[5 + skip_u64] & ((u64 *) mask)[5];
303       /* FALLTHROUGH */
304     case 2:
305       xor_sum.as_u64[0] ^= data64[2 + skip_u64] & ((u64 *) mask)[2];
306       xor_sum.as_u64[1] ^= data64[3 + skip_u64] & ((u64 *) mask)[3];
307       /* FALLTHROUGH */
308     case 1:
309       break;
310
311     default:
312       abort ();
313     }
314 #endif /* CLIB_HAVE_VEC128 */
315
316 #ifdef clib_crc32c_uses_intrinsics
317   return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
318 #else
319   return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
320 #endif
321 }
322
323 static inline void
324 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
325 {
326   u32 bucket_index;
327
328   ASSERT (is_pow2 (t->nbuckets));
329
330   bucket_index = hash & (t->nbuckets - 1);
331
332   clib_prefetch_load (&t->buckets[bucket_index]);
333 }
334
335 static inline vnet_classify_entry_t *
336 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
337 {
338   u8 *hp = clib_mem_get_heap_base (t->mheap);
339   u8 *vp = hp + offset;
340
341   return (vnet_classify_entry_t *) vp;
342 }
343
344 static inline uword
345 vnet_classify_get_offset (vnet_classify_table_t * t,
346                           vnet_classify_entry_t * v)
347 {
348   u8 *hp, *vp;
349
350   hp = (u8 *) clib_mem_get_heap_base (t->mheap);
351   vp = (u8 *) v;
352
353   ASSERT ((vp - hp) < 0x100000000ULL);
354   return vp - hp;
355 }
356
357 static inline vnet_classify_entry_t *
358 vnet_classify_entry_at_index (vnet_classify_table_t * t,
359                               vnet_classify_entry_t * e, u32 index)
360 {
361   u8 *eu8;
362
363   eu8 = (u8 *) e;
364
365   eu8 += index * (sizeof (vnet_classify_entry_t) +
366                   (t->match_n_vectors * sizeof (u32x4)));
367
368   return (vnet_classify_entry_t *) eu8;
369 }
370
371 static inline void
372 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
373 {
374   u32 bucket_index;
375   u32 value_index;
376   vnet_classify_bucket_t *b;
377   vnet_classify_entry_t *e;
378
379   bucket_index = hash & (t->nbuckets - 1);
380
381   b = &t->buckets[bucket_index];
382
383   if (b->offset == 0)
384     return;
385
386   hash >>= t->log2_nbuckets;
387
388   e = vnet_classify_get_entry (t, b->offset);
389   value_index = hash & ((1 << b->log2_pages) - 1);
390
391   e = vnet_classify_entry_at_index (t, e, value_index);
392
393   clib_prefetch_load (e);
394 }
395
396 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
397                                                  u8 * h, u64 hash, f64 now);
398
399 static inline vnet_classify_entry_t *
400 vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h,
401                                  u64 hash, f64 now)
402 {
403   vnet_classify_entry_t *v;
404   u32x4 *mask, *key;
405   union
406   {
407     u32x4 as_u32x4;
408     u64 as_u64[2];
409   } result __attribute__ ((aligned (sizeof (u32x4))));
410   vnet_classify_bucket_t *b;
411   u32 value_index;
412   u32 bucket_index;
413   u32 limit;
414   int i;
415
416   bucket_index = hash & (t->nbuckets - 1);
417   b = &t->buckets[bucket_index];
418   mask = t->mask;
419
420   if (b->offset == 0)
421     return 0;
422
423   hash >>= t->log2_nbuckets;
424
425   v = vnet_classify_get_entry (t, b->offset);
426   value_index = hash & ((1 << b->log2_pages) - 1);
427   limit = t->entries_per_page;
428   if (PREDICT_FALSE (b->linear_search))
429     {
430       value_index = 0;
431       limit *= (1 << b->log2_pages);
432     }
433
434   v = vnet_classify_entry_at_index (t, v, value_index);
435
436 #ifdef CLIB_HAVE_VEC128
437   const u32x4u *data = (const u32x4u *) h;
438   for (i = 0; i < limit; i++)
439     {
440       key = v->key;
441       result.as_u32x4 = (data[0 + t->skip_n_vectors] & mask[0]) ^ key[0];
442       switch (t->match_n_vectors)
443         {
444         case 5:
445           result.as_u32x4 |= (data[4 + t->skip_n_vectors] & mask[4]) ^ key[4];
446           /* FALLTHROUGH */
447         case 4:
448           result.as_u32x4 |= (data[3 + t->skip_n_vectors] & mask[3]) ^ key[3];
449           /* FALLTHROUGH */
450         case 3:
451           result.as_u32x4 |= (data[2 + t->skip_n_vectors] & mask[2]) ^ key[2];
452           /* FALLTHROUGH */
453         case 2:
454           result.as_u32x4 |= (data[1 + t->skip_n_vectors] & mask[1]) ^ key[1];
455           /* FALLTHROUGH */
456         case 1:
457           break;
458         default:
459           abort ();
460         }
461
462       if (u32x4_is_all_zero (result.as_u32x4))
463         {
464           if (PREDICT_TRUE (now))
465             {
466               v->hits++;
467               v->last_heard = now;
468             }
469           return (v);
470         }
471       v = vnet_classify_entry_at_index (t, v, 1);
472     }
473 #else
474   u32 skip_u64 = t->skip_n_vectors * 2;
475   const u64 *data64 = (const u64 *) h;
476   for (i = 0; i < limit; i++)
477     {
478       key = v->key;
479
480       result.as_u64[0] =
481         (data64[0 + skip_u64] & ((u64 *) mask)[0]) ^ ((u64 *) key)[0];
482       result.as_u64[1] =
483         (data64[1 + skip_u64] & ((u64 *) mask)[1]) ^ ((u64 *) key)[1];
484       switch (t->match_n_vectors)
485         {
486         case 5:
487           result.as_u64[0] |=
488             (data64[8 + skip_u64] & ((u64 *) mask)[8]) ^ ((u64 *) key)[8];
489           result.as_u64[1] |=
490             (data64[9 + skip_u64] & ((u64 *) mask)[9]) ^ ((u64 *) key)[9];
491           /* FALLTHROUGH */
492         case 4:
493           result.as_u64[0] |=
494             (data64[6 + skip_u64] & ((u64 *) mask)[6]) ^ ((u64 *) key)[6];
495           result.as_u64[1] |=
496             (data64[7 + skip_u64] & ((u64 *) mask)[7]) ^ ((u64 *) key)[7];
497           /* FALLTHROUGH */
498         case 3:
499           result.as_u64[0] |=
500             (data64[4 + skip_u64] & ((u64 *) mask)[4]) ^ ((u64 *) key)[4];
501           result.as_u64[1] |=
502             (data64[5 + skip_u64] & ((u64 *) mask)[5]) ^ ((u64 *) key)[5];
503           /* FALLTHROUGH */
504         case 2:
505           result.as_u64[0] |=
506             (data64[2 + skip_u64] & ((u64 *) mask)[2]) ^ ((u64 *) key)[2];
507           result.as_u64[1] |=
508             (data64[3 + skip_u64] & ((u64 *) mask)[3]) ^ ((u64 *) key)[3];
509           /* FALLTHROUGH */
510         case 1:
511           break;
512         default:
513           abort ();
514         }
515
516       if (result.as_u64[0] == 0 && result.as_u64[1] == 0)
517         {
518           if (PREDICT_TRUE (now))
519             {
520               v->hits++;
521               v->last_heard = now;
522             }
523           return (v);
524         }
525
526       v = vnet_classify_entry_at_index (t, v, 1);
527     }
528 #endif /* CLIB_HAVE_VEC128 */
529   return 0;
530 }
531
532 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
533                                                 const u8 *mask, u32 nbuckets,
534                                                 u32 memory_size,
535                                                 u32 skip_n_vectors,
536                                                 u32 match_n_vectors);
537
538 int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
539                                    const u8 *match, u32 hit_next_index,
540                                    u32 opaque_index, i32 advance, u8 action,
541                                    u16 metadata, int is_add);
542
543 int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
544                                  u32 nbuckets, u32 memory_size, u32 skip,
545                                  u32 match, u32 next_table_index,
546                                  u32 miss_next_index, u32 *table_index,
547                                  u8 current_data_flag, i16 current_data_offset,
548                                  int is_add, int del_chain);
549 void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
550                                        u32 table_index, int del_chain);
551
552 unformat_function_t unformat_ip4_mask;
553 unformat_function_t unformat_ip6_mask;
554 unformat_function_t unformat_l3_mask;
555 unformat_function_t unformat_l2_mask;
556 unformat_function_t unformat_classify_mask;
557 unformat_function_t unformat_l2_next_index;
558 unformat_function_t unformat_ip_next_index;
559 unformat_function_t unformat_ip4_match;
560 unformat_function_t unformat_ip6_match;
561 unformat_function_t unformat_l3_match;
562 unformat_function_t unformat_l4_match;
563 unformat_function_t unformat_vlan_tag;
564 unformat_function_t unformat_l2_match;
565 unformat_function_t unformat_classify_match;
566
567 void vnet_classify_register_unformat_ip_next_index_fn
568   (unformat_function_t * fn);
569
570 void vnet_classify_register_unformat_l2_next_index_fn
571   (unformat_function_t * fn);
572
573 void vnet_classify_register_unformat_acl_next_index_fn
574   (unformat_function_t * fn);
575
576 void vnet_classify_register_unformat_policer_next_index_fn
577   (unformat_function_t * fn);
578
579 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
580                                                       fn);
581
582 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
583 void classify_set_pcap_chain (vnet_classify_main_t * cm,
584                               u32 sw_if_index, u32 table_index);
585
586 u32 classify_get_trace_chain (void);
587 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
588
589 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
590 u32 classify_lookup_chain (u32 table_index,
591                            u8 * mask, u32 n_skip, u32 n_match);
592
593 #endif /* __included_vnet_classify_h__ */
594
595 /*
596  * fd.io coding-style-patch-verification: ON
597  *
598  * Local Variables:
599  * eval: (c-set-style "gnu")
600  * End:
601  */