ipsec: Deprecated the old IPsec Tunnel interface
[vpp.git] / src / vppinfra / bihash_template.h
1 /*
2   Copyright (c) 2014 Cisco and/or its affiliates.
3
4   * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
18
19 /*
20  * Note: to instantiate the template multiple times in a single file,
21  * #undef __included_bihash_template_h__...
22  */
23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
25
26 #include <vppinfra/heap.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/pool.h>
29 #include <vppinfra/cache.h>
30 #include <vppinfra/lock.h>
31
32 #ifndef BIHASH_TYPE
33 #error BIHASH_TYPE not defined
34 #endif
35
36 #ifdef BIHASH_32_64_SVM
37 #undef HAVE_MEMFD_CREATE
38 #include <vppinfra/linux/syscall.h>
39 #include <fcntl.h>
40 #define F_LINUX_SPECIFIC_BASE 1024
41 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
42 #define F_SEAL_SHRINK (2)
43 /* Max page size 2**16 due to refcount width  */
44 #define BIHASH_FREELIST_LENGTH 17
45 #endif
46
47 /* default is 2MB, use 30 for 1GB */
48 #ifndef BIHASH_LOG2_HUGEPAGE_SIZE
49 #define BIHASH_LOG2_HUGEPAGE_SIZE 21
50 #endif
51
52 #define _bv(a,b) a##b
53 #define __bv(a,b) _bv(a,b)
54 #define BV(a) __bv(a,BIHASH_TYPE)
55
56 #define _bvt(a,b) a##b##_t
57 #define __bvt(a,b) _bvt(a,b)
58 #define BVT(a) __bvt(a,BIHASH_TYPE)
59
60 #define _bvs(a,b) struct a##b
61 #define __bvs(a,b) _bvs(a,b)
62 #define BVS(a) __bvs(a,BIHASH_TYPE)
63
64 #if _LP64 == 0
65 #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
66 #define u64_to_pointer(x) (void *)(u32)((x))
67 #define pointer_to_u64(x) (u64)(u32)((x))
68 #else
69 #define OVERFLOW_ASSERT(x)
70 #define u64_to_pointer(x) (void *)((x))
71 #define pointer_to_u64(x) (u64)((x))
72 #endif
73
74 typedef struct BV (clib_bihash_value)
75 {
76   union
77   {
78     BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
79     u64 next_free_as_u64;
80   };
81 } BVT (clib_bihash_value);
82
83 #define BIHASH_BUCKET_OFFSET_BITS 36
84
85 typedef struct
86 {
87   union
88   {
89     struct
90     {
91       u64 offset:BIHASH_BUCKET_OFFSET_BITS;
92       u64 lock:1;
93       u64 linear_search:1;
94       u64 log2_pages:8;
95       u64 refcnt:16;
96     };
97     u64 as_u64;
98   };
99 } BVT (clib_bihash_bucket);
100
101 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
102
103 /* *INDENT-OFF* */
104 typedef CLIB_PACKED (struct {
105   /*
106    * Backing store allocation. Since bihash manages its own
107    * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
108    */
109   u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
110   u64 alloc_arena_size; /* Size of the arena */
111   u64 alloc_arena_mapped;       /* Size of the mapped memory in the arena */
112   /* Two SVM pointers stored as 8-byte integers */
113   u64 alloc_lock_as_u64;
114   u64 buckets_as_u64;
115   /* freelist list-head arrays/vectors */
116   u64 freelists_as_u64;
117   u32 nbuckets; /* Number of buckets */
118   /* Set when header valid */
119   volatile u32 ready;
120   u64 pad[1];
121 }) BVT (clib_bihash_shared_header);
122 /* *INDENT-ON* */
123
124 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
125
126 typedef
127 BVS (clib_bihash_alloc_chunk)
128 {
129   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
130
131   /* chunk size */
132   uword size;
133
134   /* pointer to the next allocation */
135   u8 *next_alloc;
136
137   /* number of bytes left in this chunk */
138   uword bytes_left;
139
140   /* doubly linked list of heap allocated chunks */
141   BVS (clib_bihash_alloc_chunk) * prev, *next;
142
143 } BVT (clib_bihash_alloc_chunk);
144
145 typedef
146 BVS (clib_bihash)
147 {
148   BVT (clib_bihash_bucket) * buckets;
149   volatile u32 *alloc_lock;
150
151   BVT (clib_bihash_value) ** working_copies;
152   int *working_copy_lengths;
153   BVT (clib_bihash_bucket) saved_bucket;
154
155   u32 nbuckets;
156   u32 log2_nbuckets;
157   u64 memory_size;
158   u8 *name;
159   format_function_t *fmt_fn;
160   void *heap;
161   BVT (clib_bihash_alloc_chunk) * chunks;
162
163   u64 *freelists;
164
165 #if BIHASH_32_64_SVM
166   BVT (clib_bihash_shared_header) * sh;
167   int memfd;
168 #else
169   BVT (clib_bihash_shared_header) sh;
170 #endif
171
172   u64 alloc_arena;              /* Base of the allocation arena */
173   volatile u8 instantiated;
174
175   /**
176     * A custom format function to print the Key and Value of bihash_key instead of default hexdump
177     */
178   format_function_t *kvp_fmt_fn;
179
180   /** Optional statistics-gathering callback */
181 #if BIHASH_ENABLE_STATS
182   void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
183
184   /** Statistics callback context (e.g. address of stats data structure) */
185   void *inc_stats_context;
186 #endif
187
188 } BVT (clib_bihash);
189
190 typedef struct
191 {
192   BVT (clib_bihash) * h;
193   char *name;
194   u32 nbuckets;
195   uword memory_size;
196   format_function_t *kvp_fmt_fn;
197   u8 instantiate_immediately;
198   u8 dont_add_to_all_bihash_list;
199 } BVT (clib_bihash_init2_args);
200
201 extern void **clib_all_bihashes;
202
203 #if BIHASH_32_64_SVM
204 #undef alloc_arena_next
205 #undef alloc_arena_size
206 #undef alloc_arena_mapped
207 #undef alloc_arena
208 #undef CLIB_BIHASH_READY_MAGIC
209 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
210 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
211 #define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
212 #define alloc_arena(h) ((h)->alloc_arena)
213 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
214 #else
215 #undef alloc_arena_next
216 #undef alloc_arena_size
217 #undef alloc_arena_mapped
218 #undef alloc_arena
219 #undef CLIB_BIHASH_READY_MAGIC
220 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
221 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
222 #define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
223 #define alloc_arena(h) ((h)->alloc_arena)
224 #define CLIB_BIHASH_READY_MAGIC 0
225 #endif
226
227 #ifndef BIHASH_STAT_IDS
228 #define BIHASH_STAT_IDS 1
229
230 #define foreach_bihash_stat                     \
231 _(alloc_add)                                    \
232 _(add)                                          \
233 _(split_add)                                    \
234 _(replace)                                      \
235 _(update)                                       \
236 _(del)                                          \
237 _(del_free)                                     \
238 _(linear)                                       \
239 _(resplit)                                      \
240 _(working_copy_lost)                            \
241 _(splits)                       /* must be last */
242
243 typedef enum
244 {
245 #define _(a) BIHASH_STAT_##a,
246   foreach_bihash_stat
247 #undef _
248     BIHASH_STAT_N_STATS,
249 } BVT (clib_bihash_stat_id);
250 #endif /* BIHASH_STAT_IDS */
251
252 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
253                                                     int stat_id, u64 count)
254 {
255 #if BIHASH_ENABLE_STATS
256   if (PREDICT_FALSE (h->inc_stats_callback != 0))
257     h->inc_stats_callback (h, stat_id, count);
258 #endif
259 }
260
261 #if BIHASH_ENABLE_STATS
262 static inline void BV (clib_bihash_set_stats_callback)
263   (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
264    void *ctx)
265 {
266   h->inc_stats_callback = cb;
267   h->inc_stats_context = ctx;
268 }
269 #endif
270
271
272 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
273 {
274   while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
275     CLIB_PAUSE ();
276 }
277
278 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
279 {
280   __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
281 }
282
283 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
284 {
285   /* *INDENT-OFF* */
286   BVT (clib_bihash_bucket) mask = { .lock = 1 };
287   /* *INDENT-ON* */
288   u64 old;
289
290 try_again:
291   old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
292
293   if (PREDICT_FALSE (old & mask.as_u64))
294     {
295       /* somebody else flipped the bit, try again */
296       CLIB_PAUSE ();
297       goto try_again;
298     }
299 }
300
301 static inline void BV (clib_bihash_unlock_bucket)
302   (BVT (clib_bihash_bucket) * b)
303 {
304   b->lock = 0;
305 }
306
307 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
308                                                 uword offset)
309 {
310   u8 *hp = (u8 *) (uword) alloc_arena (h);
311   u8 *vp = hp + offset;
312
313   return (void *) vp;
314 }
315
316 static inline int BV (clib_bihash_bucket_is_empty)
317   (BVT (clib_bihash_bucket) * b)
318 {
319   /* Note: applied to locked buckets, test offset */
320   if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
321     return b->offset == 0;
322   else
323     return (b->log2_pages == 0 && b->refcnt == 1);
324 }
325
326 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
327                                                  void *v)
328 {
329   u8 *hp, *vp;
330
331   hp = (u8 *) (uword) alloc_arena (h);
332   vp = (u8 *) v;
333
334   return vp - hp;
335 }
336
337 void BV (clib_bihash_init)
338   (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
339
340 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
341
342 #if BIHASH_32_64_SVM
343 void BV (clib_bihash_initiator_init_svm)
344   (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
345 void BV (clib_bihash_responder_init_svm)
346   (BVT (clib_bihash) * h, char *name, int fd);
347 #endif
348
349 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
350                                          format_function_t * kvp_fmt_fn);
351
352 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
353
354 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
355                               BVT (clib_bihash_kv) * add_v, int is_add);
356 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
357                                              BVT (clib_bihash_kv) * add_v,
358                                              int (*is_stale_cb) (BVT
359                                                                  (clib_bihash_kv)
360                                                                  *, void *),
361                                              void *arg);
362 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
363                              BVT (clib_bihash_kv) * search_v,
364                              BVT (clib_bihash_kv) * return_v);
365
366 int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
367
368 #define BIHASH_WALK_STOP 0
369 #define BIHASH_WALK_CONTINUE 1
370
371 typedef
372   int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
373                                                      void *);
374 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
375                                               BV
376                                               (clib_bihash_foreach_key_value_pair_cb)
377                                               cb, void *arg);
378 void *clib_all_bihash_set_heap (void);
379 void clib_bihash_copied (void *dst, void *src);
380
381 format_function_t BV (format_bihash);
382 format_function_t BV (format_bihash_kvp);
383 format_function_t BV (format_bihash_lru);
384
385 static inline
386 BVT (clib_bihash_bucket) *
387 BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
388 {
389 #if BIHASH_KVP_AT_BUCKET_LEVEL
390   uword offset;
391   offset = (hash & (h->nbuckets - 1));
392   offset = offset * (sizeof (BVT (clib_bihash_bucket))
393                      + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
394   return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
395 #else
396   return h->buckets + (hash & (h->nbuckets - 1));
397 #endif
398 }
399
400 static inline int BV (clib_bihash_search_inline_with_hash)
401   (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
402 {
403   BVT (clib_bihash_value) * v;
404   BVT (clib_bihash_bucket) * b;
405   int i, limit;
406
407   /* *INDENT-OFF* */
408   static const BVT (clib_bihash_bucket) mask = {
409     .linear_search = 1,
410     .log2_pages = -1
411   };
412   /* *INDENT-ON* */
413
414 #if BIHASH_LAZY_INSTANTIATE
415   if (PREDICT_FALSE (h->instantiated == 0))
416     return -1;
417 #endif
418
419   b = BV (clib_bihash_get_bucket) (h, hash);
420
421   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
422     return -1;
423
424   if (PREDICT_FALSE (b->lock))
425     {
426       volatile BVT (clib_bihash_bucket) * bv = b;
427       while (bv->lock)
428         CLIB_PAUSE ();
429     }
430
431   v = BV (clib_bihash_get_value) (h, b->offset);
432
433   /* If the bucket has unresolvable collisions, use linear search */
434   limit = BIHASH_KVP_PER_PAGE;
435
436   if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
437     {
438       if (PREDICT_FALSE (b->linear_search))
439         limit <<= b->log2_pages;
440       else
441         v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
442     }
443
444   for (i = 0; i < limit; i++)
445     {
446       if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
447         {
448           *key_result = v->kvp[i];
449           return 0;
450         }
451     }
452   return -1;
453 }
454
455 static inline int BV (clib_bihash_search_inline)
456   (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
457 {
458   u64 hash;
459
460   hash = BV (clib_bihash_hash) (key_result);
461
462   return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
463 }
464
465 static inline void BV (clib_bihash_prefetch_bucket)
466   (BVT (clib_bihash) * h, u64 hash)
467 {
468   CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
469                  BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
470                  LOAD);
471 }
472
473 static inline void BV (clib_bihash_prefetch_data)
474   (BVT (clib_bihash) * h, u64 hash)
475 {
476   BVT (clib_bihash_value) * v;
477   BVT (clib_bihash_bucket) * b;
478
479 #if BIHASH_LAZY_INSTANTIATE
480   if (PREDICT_FALSE (h->instantiated == 0))
481     return;
482 #endif
483
484   b = BV (clib_bihash_get_bucket) (h, hash);
485
486   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
487     return;
488
489   v = BV (clib_bihash_get_value) (h, b->offset);
490
491   if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
492     v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
493
494   CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
495                  LOAD);
496 }
497
498 static inline int BV (clib_bihash_search_inline_2_with_hash)
499   (BVT (clib_bihash) * h,
500    u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
501 {
502   BVT (clib_bihash_value) * v;
503   BVT (clib_bihash_bucket) * b;
504   int i, limit;
505
506 /* *INDENT-OFF* */
507   static const BVT (clib_bihash_bucket) mask = {
508     .linear_search = 1,
509     .log2_pages = -1
510   };
511 /* *INDENT-ON* */
512
513   ASSERT (valuep);
514
515 #if BIHASH_LAZY_INSTANTIATE
516   if (PREDICT_FALSE (h->instantiated == 0))
517     return -1;
518 #endif
519
520   b = BV (clib_bihash_get_bucket) (h, hash);
521
522   if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
523     return -1;
524
525   if (PREDICT_FALSE (b->lock))
526     {
527       volatile BVT (clib_bihash_bucket) * bv = b;
528       while (bv->lock)
529         CLIB_PAUSE ();
530     }
531
532   v = BV (clib_bihash_get_value) (h, b->offset);
533
534   /* If the bucket has unresolvable collisions, use linear search */
535   limit = BIHASH_KVP_PER_PAGE;
536
537   if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
538     {
539       if (PREDICT_FALSE (b->linear_search))
540         limit <<= b->log2_pages;
541       else
542         v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
543     }
544
545   for (i = 0; i < limit; i++)
546     {
547       if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
548         {
549           *valuep = v->kvp[i];
550           return 0;
551         }
552     }
553   return -1;
554 }
555
556 static inline int BV (clib_bihash_search_inline_2)
557   (BVT (clib_bihash) * h,
558    BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
559 {
560   u64 hash;
561
562   hash = BV (clib_bihash_hash) (search_key);
563
564   return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
565                                                      valuep);
566 }
567
568
569 #endif /* __included_bihash_template_h__ */
570
571 /** @endcond */
572
573 /*
574  * fd.io coding-style-patch-verification: ON
575  *
576  * Local Variables:
577  * eval: (c-set-style "gnu")
578  * End:
579  */