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