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