ip: improve ip ACL traces
[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   u16 load_mask;
166
167   /* Index of next table to try */
168   u32 next_table_index;
169
170   /* packet offsets */
171   i16 current_data_offset;
172   vnet_classify_flags_t current_data_flag;
173   /* Miss next index, return if next_table_index = 0 */
174   u32 miss_next_index;
175
176   /**
177    * All members accessed in the DP above here
178    */
179   CLIB_CACHE_LINE_ALIGN_MARK (cacheline1);
180
181   /* Config parameters */
182   u32 linear_buckets;
183   u32 active_elements;
184   u32 data_offset;
185
186   /* Per-bucket working copies, one per thread */
187   vnet_classify_entry_t **working_copies;
188   int *working_copy_lengths;
189   vnet_classify_bucket_t saved_bucket;
190
191   /* Free entry freelists */
192   vnet_classify_entry_t **freelists;
193
194   /* Writer (only) lock for this table */
195   clib_spinlock_t writer_lock;
196
197   CLIB_CACHE_LINE_ALIGN_MARK (cacheline2);
198   /* Mask to apply after skipping N vectors */
199   union
200   {
201     u32x4 mask[8];
202     u32 mask_u32[32];
203   };
204
205 } vnet_classify_table_t;
206
207 /**
208  * Ensure DP fields don't spill over to cache-line 2
209  */
210 STATIC_ASSERT_OFFSET_OF (vnet_classify_table_t, cacheline1,
211                          CLIB_CACHE_LINE_BYTES);
212
213 /**
214  * The vector size for the classifier
215  *  in the add/del table 'match' is the number of vectors of this size
216  */
217 #define VNET_CLASSIFY_VECTOR_SIZE                                             \
218   sizeof (((vnet_classify_table_t *) 0)->mask[0])
219
220 struct _vnet_classify_main
221 {
222   /* Table pool */
223   vnet_classify_table_t *tables;
224
225   /* Registered next-index, opaque unformat fcns */
226   unformat_function_t **unformat_l2_next_index_fns;
227   unformat_function_t **unformat_ip_next_index_fns;
228   unformat_function_t **unformat_acl_next_index_fns;
229   unformat_function_t **unformat_policer_next_index_fns;
230   unformat_function_t **unformat_opaque_index_fns;
231
232   /* Per-interface filter table.  [0] is used for pcap */
233   u32 *classify_table_index_by_sw_if_index;
234
235   /* convenience variables */
236   vlib_main_t *vlib_main;
237   vnet_main_t *vnet_main;
238 };
239
240 extern vnet_classify_main_t vnet_classify_main;
241
242 u8 *format_classify_entry (u8 *s, va_list *args);
243 u8 *format_classify_table (u8 * s, va_list * args);
244 u8 *format_vnet_classify_table (u8 *s, va_list *args);
245
246 u64 vnet_classify_hash_packet (vnet_classify_table_t * t, u8 * h);
247
248 static_always_inline vnet_classify_table_t *
249 vnet_classify_table_get (u32 table_index)
250 {
251   vnet_classify_main_t *vcm = &vnet_classify_main;
252
253   return (pool_elt_at_index (vcm->tables, table_index));
254 }
255
256 static inline u64
257 vnet_classify_hash_packet_inline (vnet_classify_table_t *t, const u8 *h)
258 {
259   u64 xor_sum;
260   ASSERT (t);
261   h += t->skip_n_vectors * 16;
262
263 #if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
264   u64x8 xor_sum_x8, *mask = (u64x8 *) t->mask;
265   u16 load_mask = t->load_mask;
266   u64x8u *data = (u64x8u *) h;
267
268   xor_sum_x8 = u64x8_mask_load_zero (data, load_mask) & mask[0];
269
270   if (PREDICT_FALSE (load_mask >> 8))
271     xor_sum_x8 ^= u64x8_mask_load_zero (data + 1, load_mask >> 8) & mask[1];
272
273   xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 4);
274   xor_sum_x8 ^= u64x8_align_right (xor_sum_x8, xor_sum_x8, 2);
275   xor_sum = xor_sum_x8[0] ^ xor_sum_x8[1];
276 #elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
277   u64x4 xor_sum_x4, *mask = (u64x4 *) t->mask;
278   u16 load_mask = t->load_mask;
279   u64x4u *data = (u64x4u *) h;
280
281   xor_sum_x4 = u64x4_mask_load_zero (data, load_mask) & mask[0];
282   xor_sum_x4 ^= u64x4_mask_load_zero (data + 1, load_mask >> 4) & mask[1];
283
284   if (PREDICT_FALSE (load_mask >> 8))
285     xor_sum_x4 ^= u64x4_mask_load_zero (data + 2, load_mask >> 8) & mask[2];
286
287   xor_sum_x4 ^= u64x4_align_right (xor_sum_x4, xor_sum_x4, 2);
288   xor_sum = xor_sum_x4[0] ^ xor_sum_x4[1];
289 #elif defined(CLIB_HAVE_VEC128)
290   u64x2 *mask = (u64x2 *) t->mask;
291   u64x2u *data = (u64x2u *) h;
292   u64x2 xor_sum_x2;
293
294   xor_sum_x2 = data[0] & mask[0];
295
296   switch (t->match_n_vectors)
297     {
298     case 5:
299       xor_sum_x2 ^= data[4] & mask[4];
300       /* FALLTHROUGH */
301     case 4:
302       xor_sum_x2 ^= data[3] & mask[3];
303       /* FALLTHROUGH */
304     case 3:
305       xor_sum_x2 ^= data[2] & mask[2];
306       /* FALLTHROUGH */
307     case 2:
308       xor_sum_x2 ^= data[1] & mask[1];
309       /* FALLTHROUGH */
310     case 1:
311       break;
312     default:
313       abort ();
314     }
315   xor_sum = xor_sum_x2[0] ^ xor_sum_x2[1];
316 #else
317   u64 *data = (u64 *) h;
318   u64 *mask = (u64 *) t->mask;
319
320   xor_sum = (data[0] & mask[0]) ^ (data[1] & mask[1]);
321
322   switch (t->match_n_vectors)
323     {
324     case 5:
325       xor_sum ^= (data[8] & mask[8]) ^ (data[9] & mask[9]);
326       /* FALLTHROUGH */
327     case 4:
328       xor_sum ^= (data[6] & mask[6]) ^ (data[7] & mask[7]);
329       /* FALLTHROUGH */
330     case 3:
331       xor_sum ^= (data[4] & mask[4]) ^ (data[5] & mask[5]);
332       /* FALLTHROUGH */
333     case 2:
334       xor_sum ^= (data[2] & mask[2]) ^ (data[3] & mask[3]);
335       /* FALLTHROUGH */
336     case 1:
337       break;
338
339     default:
340       abort ();
341     }
342 #endif /* CLIB_HAVE_VEC128 */
343
344 #ifdef clib_crc32c_uses_intrinsics
345   return clib_crc32c ((u8 *) & xor_sum, sizeof (xor_sum));
346 #else
347   return clib_xxhash (xor_sum);
348 #endif
349 }
350
351 static inline void
352 vnet_classify_prefetch_bucket (vnet_classify_table_t * t, u64 hash)
353 {
354   u32 bucket_index;
355
356   ASSERT (is_pow2 (t->nbuckets));
357
358   bucket_index = hash & (t->nbuckets - 1);
359
360   clib_prefetch_load (&t->buckets[bucket_index]);
361 }
362
363 static inline vnet_classify_entry_t *
364 vnet_classify_get_entry (vnet_classify_table_t * t, uword offset)
365 {
366   u8 *hp = clib_mem_get_heap_base (t->mheap);
367   u8 *vp = hp + offset;
368
369   return (vnet_classify_entry_t *) vp;
370 }
371
372 static inline uword
373 vnet_classify_get_offset (vnet_classify_table_t * t,
374                           vnet_classify_entry_t * v)
375 {
376   u8 *hp, *vp;
377
378   hp = (u8 *) clib_mem_get_heap_base (t->mheap);
379   vp = (u8 *) v;
380
381   ASSERT ((vp - hp) < 0x100000000ULL);
382   return vp - hp;
383 }
384
385 static inline vnet_classify_entry_t *
386 vnet_classify_entry_at_index (vnet_classify_table_t * t,
387                               vnet_classify_entry_t * e, u32 index)
388 {
389   u8 *eu8;
390
391   eu8 = (u8 *) e;
392
393   eu8 += index * (sizeof (vnet_classify_entry_t) +
394                   (t->match_n_vectors * sizeof (u32x4)));
395
396   return (vnet_classify_entry_t *) eu8;
397 }
398
399 static inline void
400 vnet_classify_prefetch_entry (vnet_classify_table_t * t, u64 hash)
401 {
402   u32 bucket_index;
403   u32 value_index;
404   vnet_classify_bucket_t *b;
405   vnet_classify_entry_t *e;
406
407   bucket_index = hash & (t->nbuckets - 1);
408
409   b = &t->buckets[bucket_index];
410
411   if (b->offset == 0)
412     return;
413
414   hash >>= t->log2_nbuckets;
415
416   e = vnet_classify_get_entry (t, b->offset);
417   value_index = hash & ((1 << b->log2_pages) - 1);
418
419   e = vnet_classify_entry_at_index (t, e, value_index);
420
421   clib_prefetch_load (e);
422 }
423
424 vnet_classify_entry_t *vnet_classify_find_entry (vnet_classify_table_t * t,
425                                                  u8 * h, u64 hash, f64 now);
426
427 static_always_inline int
428 vnet_classify_entry_is_equal (vnet_classify_entry_t *v, const u8 *d, u8 *m,
429                               u32 match_n_vectors, u16 load_mask)
430 {
431 #if defined(CLIB_HAVE_VEC512) && defined(CLIB_HAVE_VEC512_MASK_LOAD_STORE)
432   u64x8 r, *mask = (u64x8 *) m;
433   u64x8u *data = (u64x8u *) d;
434   u64x8 *key = (u64x8 *) v->key;
435
436   r = (u64x8_mask_load_zero (data, load_mask) & mask[0]) ^
437       u64x8_mask_load_zero (key, load_mask);
438   load_mask >>= 8;
439
440   if (PREDICT_FALSE (load_mask))
441     r |= (u64x8_mask_load_zero (data + 1, load_mask) & mask[1]) ^
442          u64x8_mask_load_zero (key + 1, load_mask);
443
444   if (u64x8_is_all_zero (r))
445     return 1;
446
447 #elif defined(CLIB_HAVE_VEC256) && defined(CLIB_HAVE_VEC256_MASK_LOAD_STORE)
448   u64x4 r, *mask = (u64x4 *) m;
449   u64x4u *data = (u64x4u *) d;
450   u64x4 *key = (u64x4 *) v->key;
451
452   r = (u64x4_mask_load_zero (data, load_mask) & mask[0]) ^
453       u64x4_mask_load_zero (key, load_mask);
454   load_mask >>= 4;
455
456   r |= (u64x4_mask_load_zero (data + 1, load_mask) & mask[1]) ^
457        u64x4_mask_load_zero (key + 1, load_mask);
458   load_mask >>= 4;
459
460   if (PREDICT_FALSE (load_mask))
461     r |= (u64x4_mask_load_zero (data + 2, load_mask) & mask[2]) ^
462          u64x4_mask_load_zero (key + 2, load_mask);
463
464   if (u64x4_is_all_zero (r))
465     return 1;
466
467 #elif defined(CLIB_HAVE_VEC128)
468   u64x2u *data = (u64x2 *) d;
469   u64x2 *key = (u64x2 *) v->key;
470   u64x2 *mask = (u64x2 *) m;
471   u64x2 r;
472
473   r = (data[0] & mask[0]) ^ key[0];
474   switch (match_n_vectors)
475     {
476     case 5:
477       r |= (data[4] & mask[4]) ^ key[4];
478       /* fall through */
479     case 4:
480       r |= (data[3] & mask[3]) ^ key[3];
481       /* fall through */
482     case 3:
483       r |= (data[2] & mask[2]) ^ key[2];
484       /* fall through */
485     case 2:
486       r |= (data[1] & mask[1]) ^ key[1];
487       /* fall through */
488     case 1:
489       break;
490     default:
491       abort ();
492     }
493
494   if (u64x2_is_all_zero (r))
495     return 1;
496
497 #else
498   u64 *data = (u64 *) d;
499   u64 *key = (u64 *) v->key;
500   u64 *mask = (u64 *) m;
501   u64 r;
502
503   r = ((data[0] & mask[0]) ^ key[0]) | ((data[1] & mask[1]) ^ key[1]);
504   switch (match_n_vectors)
505     {
506     case 5:
507       r |= ((data[8] & mask[8]) ^ key[8]) | ((data[9] & mask[9]) ^ key[9]);
508       /* fall through */
509     case 4:
510       r |= ((data[6] & mask[6]) ^ key[6]) | ((data[7] & mask[7]) ^ key[7]);
511       /* fall through */
512     case 3:
513       r |= ((data[4] & mask[4]) ^ key[4]) | ((data[5] & mask[5]) ^ key[5]);
514       /* fall through */
515     case 2:
516       r |= ((data[2] & mask[2]) ^ key[2]) | ((data[3] & mask[3]) ^ key[3]);
517       /* fall through */
518     case 1:
519       break;
520     default:
521       abort ();
522     }
523
524   if (r == 0)
525     return 1;
526
527 #endif /* CLIB_HAVE_VEC128 */
528   return 0;
529 }
530
531 static inline vnet_classify_entry_t *
532 vnet_classify_find_entry_inline (vnet_classify_table_t *t, const u8 *h,
533                                  u64 hash, f64 now)
534 {
535   vnet_classify_entry_t *v;
536   vnet_classify_bucket_t *b;
537   u32 bucket_index, limit, pages, match_n_vectors = t->match_n_vectors;
538   u16 load_mask = t->load_mask;
539   u8 *mask = (u8 *) t->mask;
540   int i;
541
542   bucket_index = hash & (t->nbuckets - 1);
543   b = &t->buckets[bucket_index];
544
545   if (b->offset == 0)
546     return 0;
547
548   pages = 1 << b->log2_pages;
549   v = vnet_classify_get_entry (t, b->offset);
550   limit = t->entries_per_page;
551   if (PREDICT_FALSE (b->linear_search))
552     {
553       limit *= pages;
554       v = vnet_classify_entry_at_index (t, v, 0);
555     }
556   else
557     {
558       hash >>= t->log2_nbuckets;
559       v = vnet_classify_entry_at_index (t, v, hash & (pages - 1));
560     }
561
562   h += t->skip_n_vectors * 16;
563
564   for (i = 0; i < limit; i++)
565     {
566       if (vnet_classify_entry_is_equal (v, h, mask, match_n_vectors,
567                                         load_mask))
568         {
569           if (PREDICT_TRUE (now))
570             {
571               v->hits++;
572               v->last_heard = now;
573             }
574           return (v);
575         }
576       v = vnet_classify_entry_at_index (t, v, 1);
577     }
578   return 0;
579 }
580
581 vnet_classify_table_t *vnet_classify_new_table (vnet_classify_main_t *cm,
582                                                 const u8 *mask, u32 nbuckets,
583                                                 u32 memory_size,
584                                                 u32 skip_n_vectors,
585                                                 u32 match_n_vectors);
586
587 int vnet_classify_add_del_session (vnet_classify_main_t *cm, u32 table_index,
588                                    const u8 *match, u32 hit_next_index,
589                                    u32 opaque_index, i32 advance, u8 action,
590                                    u16 metadata, int is_add);
591
592 int vnet_classify_add_del_table (vnet_classify_main_t *cm, const u8 *mask,
593                                  u32 nbuckets, u32 memory_size, u32 skip,
594                                  u32 match, u32 next_table_index,
595                                  u32 miss_next_index, u32 *table_index,
596                                  u8 current_data_flag, i16 current_data_offset,
597                                  int is_add, int del_chain);
598 void vnet_classify_delete_table_index (vnet_classify_main_t *cm,
599                                        u32 table_index, int del_chain);
600
601 unformat_function_t unformat_ip4_mask;
602 unformat_function_t unformat_ip6_mask;
603 unformat_function_t unformat_l3_mask;
604 unformat_function_t unformat_l2_mask;
605 unformat_function_t unformat_classify_mask;
606 unformat_function_t unformat_l2_next_index;
607 unformat_function_t unformat_ip_next_index;
608 unformat_function_t unformat_ip4_match;
609 unformat_function_t unformat_ip6_match;
610 unformat_function_t unformat_l3_match;
611 unformat_function_t unformat_l4_match;
612 unformat_function_t unformat_vlan_tag;
613 unformat_function_t unformat_l2_match;
614 unformat_function_t unformat_classify_match;
615
616 void vnet_classify_register_unformat_ip_next_index_fn
617   (unformat_function_t * fn);
618
619 void vnet_classify_register_unformat_l2_next_index_fn
620   (unformat_function_t * fn);
621
622 void vnet_classify_register_unformat_acl_next_index_fn
623   (unformat_function_t * fn);
624
625 void vnet_classify_register_unformat_policer_next_index_fn
626   (unformat_function_t * fn);
627
628 void vnet_classify_register_unformat_opaque_index_fn (unformat_function_t *
629                                                       fn);
630
631 u32 classify_get_pcap_chain (vnet_classify_main_t * cm, u32 sw_if_index);
632 void classify_set_pcap_chain (vnet_classify_main_t * cm,
633                               u32 sw_if_index, u32 table_index);
634
635 u32 classify_get_trace_chain (void);
636 void classify_set_trace_chain (vnet_classify_main_t * cm, u32 table_index);
637
638 u32 classify_sort_table_chain (vnet_classify_main_t * cm, u32 table_index);
639 u32 classify_lookup_chain (u32 table_index,
640                            u8 * mask, u32 n_skip, u32 n_match);
641
642 #endif /* __included_vnet_classify_h__ */
643
644 /*
645  * fd.io coding-style-patch-verification: ON
646  *
647  * Local Variables:
648  * eval: (c-set-style "gnu")
649  * End:
650  */