baeaeaf18e980774c59d447af56c3afaf448afb9
[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   u64 xor_sum;
254   ASSERT (t);
255   h += t->skip_n_vectors * 16;
256
257 #if defined(CLIB_HAVE_VEC128)
258   u64x2 *mask = (u64x2 *) t->mask;
259   u64x2u *data = (u64x2u *) h;
260   u64x2 xor_sum_x2;
261
262   xor_sum_x2 = data[0] & mask[0];
263
264   switch (t->match_n_vectors)
265     {
266     case 5:
267       xor_sum_x2 ^= data[4] & mask[4];
268       /* FALLTHROUGH */
269     case 4:
270       xor_sum_x2 ^= data[3] & mask[3];
271       /* FALLTHROUGH */
272     case 3:
273       xor_sum_x2 ^= data[2] & mask[2];
274       /* FALLTHROUGH */
275     case 2:
276       xor_sum_x2 ^= data[1] & mask[1];
277       /* FALLTHROUGH */
278     case 1:
279       break;
280     default:
281       abort ();
282     }
283   xor_sum = xor_sum_x2[0] ^ xor_sum_x2[1];
284 #else
285   u64 *data = (u64 *) h;
286   u64 *mask = (u64 *) t->mask;
287
288   xor_sum = (data[0] & mask[0]) ^ (data[1] & mask[1]);
289
290   switch (t->match_n_vectors)
291     {
292     case 5:
293       xor_sum ^= (data[8] & mask[8]) ^ (data[9] & mask[9]);
294       /* FALLTHROUGH */
295     case 4:
296       xor_sum ^= (data[6] & mask[6]) ^ (data[7] & mask[7]);
297       /* FALLTHROUGH */
298     case 3:
299       xor_sum ^= (data[4] & mask[4]) ^ (data[5] & mask[5]);
300       /* FALLTHROUGH */
301     case 2:
302       xor_sum ^= (data[2] & mask[2]) ^ (data[3] & mask[3]);
303       /* FALLTHROUGH */
304     case 1:
305       break;
306
307     default:
308       abort ();
309     }
310 #endif /* CLIB_HAVE_VEC128 */
311
312 #ifdef clib_crc32c_uses_intrinsics
313   return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
314 #else
315   return clib_xxhash (xor_sum.as_u64[0] ^ xor_sum.as_u64[1]);
316 #endif
317 }
318
319 static inline void
320 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
321 {
322   u32 bucket_index;
323
324   ASSERT (is_pow2 (t->nbuckets));
325
326   bucket_index = hash & (t->nbuckets - 1);
327
328   clib_prefetch_load (&t->buckets[bucket_index]);
329 }
330
331 static inline vnet_classify_entry_t *
332 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
333 {
334   u8 *hp = clib_mem_get_heap_base (t->mheap);
335   u8 *vp = hp + offset;
336
337   return (vnet_classify_entry_t *) vp;
338 }
339
340 static inline uword
341 vnet_classify_get_offset (vnet_classify_table_t * t,
342                           vnet_classify_entry_t * v)
343 {
344   u8 *hp, *vp;
345
346   hp = (u8 *) clib_mem_get_heap_base (t->mheap);
347   vp = (u8 *) v;
348
349   ASSERT ((vp - hp) < 0x100000000ULL);
350   return vp - hp;
351 }
352
353 static inline vnet_classify_entry_t *
354 vnet_classify_entry_at_index (vnet_classify_table_t * t,
355                               vnet_classify_entry_t * e, u32 index)
356 {
357   u8 *eu8;
358
359   eu8 = (u8 *) e;
360
361   eu8 += index * (sizeof (vnet_classify_entry_t) +
362                   (t->match_n_vectors * sizeof (u32x4)));
363
364   return (vnet_classify_entry_t *) eu8;
365 }
366
367 static inline void
368 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
369 {
370   u32 bucket_index;
371   u32 value_index;
372   vnet_classify_bucket_t *b;
373   vnet_classify_entry_t *e;
374
375   bucket_index = hash & (t->nbuckets - 1);
376
377   b = &t->buckets[bucket_index];
378
379   if (b->offset == 0)
380     return;
381
382   hash >>= t->log2_nbuckets;
383
384   e = vnet_classify_get_entry (t, b->offset);
385   value_index = hash & ((1 << b->log2_pages) - 1);
386
387   e = vnet_classify_entry_at_index (t, e, value_index);
388
389   clib_prefetch_load (e);
390 }
391
392 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
393                                                  u8 * h, u64 hash, f64 now);
394
395 static_always_inline int
396 vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m,
397                               u32 match_n_vectors)
398 {
399 #ifdef CLIB_HAVE_VEC128
400   u64x2u *data = (u64x2 *) d;
401   u64x2 *key = (u64x2 *) v->key;
402   u64x2 *mask = (u64x2 *) m;
403   u64x2 r;
404
405   r = (data[0] & mask[0]) ^ key[0];
406   switch (match_n_vectors)
407     {
408     case 5:
409       r |= (data[4] & mask[4]) ^ key[4];
410       /* fall through */
411     case 4:
412       r |= (data[3] & mask[3]) ^ key[3];
413       /* fall through */
414     case 3:
415       r |= (data[2] & mask[2]) ^ key[2];
416       /* fall through */
417     case 2:
418       r |= (data[1] & mask[1]) ^ key[1];
419       /* fall through */
420     case 1:
421       break;
422     default:
423       abort ();
424     }
425
426   if (u64x2_is_all_zero (r))
427     return 1;
428
429 #else
430   u64 *data = (u64 *) d;
431   u64 *key = (u64 *) v->key;
432   u64 *mask = (u64 *) m;
433   u64 r;
434
435   r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]);
436   switch (match_n_vectors)
437     {
438     case 5:
439       r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]);
440       /* fall through */
441     case 4:
442       r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]);
443       /* fall through */
444     case 3:
445       r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]);
446       /* fall through */
447     case 2:
448       r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]);
449       /* fall through */
450     case 1:
451       break;
452     default:
453       abort ();
454     }
455
456   if (r == 0)
457     return 1;
458
459 #endif /* CLIB_HAVE_VEC128 */
460   return 0;
461 }
462
463 static inline vnet_classify_entry_t *
464 vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h,
465                                  u64 hash, f64 now)
466 {
467   vnet_classify_entry_t *v;
468   vnet_classify_bucket_t *b;
469   u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors;
470   u8 *mask = (u8 *) t->mask;
471   int i;
472
473   bucket_index = hash & (t->nbuckets - 1);
474   b = &t->buckets[bucket_index];
475
476   if (b->offset == 0)
477     return 0;
478
479   pages = 1 << b->log2_pages;
480   v = vnet_classify_get_entry (t, b->offset);
481   limit = t->entries_per_page;
482   if (PREDICT_FALSE (b->linear_search))
483     {
484       limit *= pages;
485       v = vnet_classify_entry_at_index (t, v, 0);
486     }
487   else
488     {
489       hash >>= t->log2_nbuckets;
490       v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
491     }
492
493   h += t->skip_n_vectors * 16;
494
495   for (i = 0; i < limit; i++)
496     {
497       if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors))
498         {
499           if (PREDICT_TRUE (now))
500             {
501               v->hits++;
502               v->last_heard = now;
503             }
504           return (v);
505         }
506       v = vnet_classify_entry_at_index (t, v, 1);
507     }
508   return 0;
509 }
510
511 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
512                                                 const u8 *mask, u32 nbuckets,
513                                                 u32 memory_size,
514                                                 u32 skip_n_vectors,
515                                                 u32 match_n_vectors);
516
517 int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
518                                    const u8 *match, u32 hit_next_index,
519                                    u32 opaque_index, i32 advance, u8 action,
520                                    u16 metadata, int is_add);
521
522 int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
523                                  u32 nbuckets, u32 memory_size, u32 skip,
524                                  u32 match, u32 next_table_index,
525                                  u32 miss_next_index, u32 *table_index,
526                                  u8 current_data_flag, i16 current_data_offset,
527                                  int is_add, int del_chain);
528 void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
529                                        u32 table_index, int del_chain);
530
531 unformat_function_t unformat_ip4_mask;
532 unformat_function_t unformat_ip6_mask;
533 unformat_function_t unformat_l3_mask;
534 unformat_function_t unformat_l2_mask;
535 unformat_function_t unformat_classify_mask;
536 unformat_function_t unformat_l2_next_index;
537 unformat_function_t unformat_ip_next_index;
538 unformat_function_t unformat_ip4_match;
539 unformat_function_t unformat_ip6_match;
540 unformat_function_t unformat_l3_match;
541 unformat_function_t unformat_l4_match;
542 unformat_function_t unformat_vlan_tag;
543 unformat_function_t unformat_l2_match;
544 unformat_function_t unformat_classify_match;
545
546 void vnet_classify_register_unformat_ip_next_index_fn
547   (unformat_function_t * fn);
548
549 void vnet_classify_register_unformat_l2_next_index_fn
550   (unformat_function_t * fn);
551
552 void vnet_classify_register_unformat_acl_next_index_fn
553   (unformat_function_t * fn);
554
555 void vnet_classify_register_unformat_policer_next_index_fn
556   (unformat_function_t * fn);
557
558 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
559                                                       fn);
560
561 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
562 void classify_set_pcap_chain (vnet_classify_main_t * cm,
563                               u32 sw_if_index, u32 table_index);
564
565 u32 classify_get_trace_chain (void);
566 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
567
568 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
569 u32 classify_lookup_chain (u32 table_index,
570                            u8 * mask, u32 n_skip, u32 n_match);
571
572 #endif /* __included_vnet_classify_h__ */
573
574 /*
575  * fd.io coding-style-patch-verification: ON
576  *
577  * Local Variables:
578  * eval: (c-set-style "gnu")
579  * End:
580  */