23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
33 #error BIHASH_TYPE not defined
36 #ifdef BIHASH_32_64_SVM
37 #include <vppinfra/linux/syscall.h>
39 #define F_LINUX_SPECIFIC_BASE 1024
40 #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9)
41 #define F_SEAL_SHRINK (2)
43 #define BIHASH_FREELIST_LENGTH 17
47 #ifndef BIHASH_LOG2_HUGEPAGE_SIZE
48 #define BIHASH_LOG2_HUGEPAGE_SIZE 21
52 #define __bv(a,b) _bv(a,b)
53 #define BV(a) __bv(a,BIHASH_TYPE)
55 #define _bvt(a,b) a##b##_t
56 #define __bvt(a,b) _bvt(a,b)
57 #define BVT(a) __bvt(a,BIHASH_TYPE)
59 #define _bvs(a,b) struct a##b
60 #define __bvs(a,b) _bvs(a,b)
61 #define BVS(a) __bvs(a,BIHASH_TYPE)
64 #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
65 #define u64_to_pointer(x) (void *)(u32)((x))
66 #define pointer_to_u64(x) (u64)(u32)((x))
68 #define OVERFLOW_ASSERT(x)
69 #define u64_to_pointer(x) (void *)((x))
70 #define pointer_to_u64(x) (u64)((x))
73 typedef struct BV (clib_bihash_value)
82 #define BIHASH_BUCKET_OFFSET_BITS 36
98 }
BVT (clib_bihash_bucket);
108 u64 alloc_arena_next;
109 u64 alloc_arena_size;
110 u64 alloc_arena_mapped;
112 u64 alloc_lock_as_u64;
115 u64 freelists_as_u64;
120 })
BVT (clib_bihash_shared_header);
126 BVS (clib_bihash_alloc_chunk)
140 BVS (clib_bihash_alloc_chunk) * prev, *
next;
142 }
BVT (clib_bihash_alloc_chunk);
147 BVT (clib_bihash_bucket) * buckets;
148 volatile u32 *alloc_lock;
151 int *working_copy_lengths;
152 BVT (clib_bihash_bucket) saved_bucket;
160 BVT (clib_bihash_alloc_chunk) * chunks;
165 BVT (clib_bihash_shared_header) * sh;
168 BVT (clib_bihash_shared_header) sh;
172 volatile u8 instantiated;
173 u8 dont_add_to_all_bihash_list;
181 #if BIHASH_ENABLE_STATS
182 void (*inc_stats_callback) (BVS (clib_bihash) *,
int stat_id,
u64 count);
185 void *inc_stats_context;
192 BVT (clib_bihash) *
h;
197 u8 instantiate_immediately;
198 u8 dont_add_to_all_bihash_list;
199 }
BVT (clib_bihash_init2_args);
204 #undef alloc_arena_next
205 #undef alloc_arena_size
206 #undef alloc_arena_mapped
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
215 #undef alloc_arena_next
216 #undef alloc_arena_size
217 #undef alloc_arena_mapped
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
227 #ifndef BIHASH_STAT_IDS
228 #define BIHASH_STAT_IDS 1
230 #define foreach_bihash_stat \
240 _(working_copy_lost) \
245 #define _(a) BIHASH_STAT_##a,
249 }
BVT (clib_bihash_stat_id);
252 static inline void BV (clib_bihash_increment_stat) (
BVT (clib_bihash) *
h,
255 #if BIHASH_ENABLE_STATS
257 h->inc_stats_callback (
h, stat_id,
count);
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),
266 h->inc_stats_callback = cb;
267 h->inc_stats_context =
ctx;
272 static inline void BV (clib_bihash_alloc_lock) (
BVT (clib_bihash) *
h)
274 while (__atomic_test_and_set (
h->alloc_lock, __ATOMIC_ACQUIRE))
278 static inline void BV (clib_bihash_alloc_unlock) (
BVT (clib_bihash) *
h)
280 __atomic_clear (
h->alloc_lock, __ATOMIC_RELEASE);
283 static inline void BV (clib_bihash_lock_bucket) (
BVT (clib_bihash_bucket) *
b)
286 BVT (clib_bihash_bucket)
mask = { .lock = 1 };
301 static inline void BV (clib_bihash_unlock_bucket)
302 (
BVT (clib_bihash_bucket) *
b)
316 static inline int BV (clib_bihash_bucket_is_empty)
317 (
BVT (clib_bihash_bucket) *
b)
321 return b->offset == 0;
323 return (
b->log2_pages == 0 &&
b->refcnt == 1);
331 hp = (
u8 *) (
uword) alloc_arena (
h);
343 void BV (clib_bihash_init2) (
BVT (clib_bihash_init2_args) *
a);
346 void BV (clib_bihash_initiator_init_svm)
348 void BV (clib_bihash_responder_init_svm)
349 (
BVT (clib_bihash) *
h,
char *
name,
int fd);
352 void BV (clib_bihash_set_kvp_format_fn) (
BVT (clib_bihash) *
h,
358 BVT (clib_bihash_kv) * add_v,
int is_add);
359 int BV (clib_bihash_add_or_overwrite_stale) (
BVT (clib_bihash) *
h,
360 BVT (clib_bihash_kv) * add_v,
361 int (*is_stale_cb) (
BVT
365 int BV (clib_bihash_search) (
BVT (clib_bihash) *
h,
366 BVT (clib_bihash_kv) * search_v,
367 BVT (clib_bihash_kv) * return_v);
369 int BV (clib_bihash_is_initialised) (
const BVT (clib_bihash) *
h);
371 #define BIHASH_WALK_STOP 0
372 #define BIHASH_WALK_CONTINUE 1
389 BVT (clib_bihash_bucket) *
390 BV (clib_bihash_get_bucket) (
BVT (clib_bihash) *
h,
u64 hash)
392 #if BIHASH_KVP_AT_BUCKET_LEVEL
394 offset = (hash & (
h->nbuckets - 1));
397 return ((
BVT (clib_bihash_bucket) *) (((
u8 *)
h->buckets) +
offset));
399 return h->buckets + (hash & (
h->nbuckets - 1));
404 (
BVT (clib_bihash) *
h,
u64 hash,
BVT (clib_bihash_kv) * key_result)
407 BVT (clib_bihash_bucket) *
b;
411 static const BVT (clib_bihash_bucket)
mask = {
417 #if BIHASH_LAZY_INSTANTIATE
422 b = BV (clib_bihash_get_bucket) (
h, hash);
429 volatile BVT (clib_bihash_bucket) * bv =
b;
442 limit <<=
b->log2_pages;
447 for (
i = 0;
i < limit;
i++)
449 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, key_result->key))
451 *key_result = v->kvp[
i];
459 (
BVT (clib_bihash) *
h,
BVT (clib_bihash_kv) * key_result)
463 hash = BV (clib_bihash_hash) (key_result);
469 (
BVT (clib_bihash) *
h,
u64 hash)
477 (
BVT (clib_bihash) *
h,
u64 hash)
480 BVT (clib_bihash_bucket) *
b;
482 #if BIHASH_LAZY_INSTANTIATE
487 b = BV (clib_bihash_get_bucket) (
h, hash);
501 static inline int BV (clib_bihash_search_inline_2_with_hash)
502 (
BVT (clib_bihash) *
h,
503 u64 hash,
BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
506 BVT (clib_bihash_bucket) *
b;
510 static const BVT (clib_bihash_bucket)
mask = {
518 #if BIHASH_LAZY_INSTANTIATE
523 b = BV (clib_bihash_get_bucket) (
h, hash);
530 volatile BVT (clib_bihash_bucket) * bv =
b;
543 limit <<=
b->log2_pages;
548 for (
i = 0;
i < limit;
i++)
550 if (BV (clib_bihash_key_compare) (v->kvp[
i].key, search_key->key))
560 (
BVT (clib_bihash) *
h,
561 BVT (clib_bihash_kv) * search_key,
BVT (clib_bihash_kv) * valuep)
565 hash = BV (clib_bihash_hash) (search_key);
567 return BV (clib_bihash_search_inline_2_with_hash) (
h, hash, search_key,