2 Copyright (c) 2014 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
20 * Note: to instantiate the template multiple times in a single file,
21 * #undef __included_bihash_template_h__...
23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
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>
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)
42 /* Max page size 2**16 due to refcount width */
43 #define BIHASH_FREELIST_LENGTH 17
46 /* default is 2MB, use 30 for 1GB */
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)
77 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
80 } BVT (clib_bihash_value);
82 #define BIHASH_BUCKET_OFFSET_BITS 36
90 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
98 } BVT (clib_bihash_bucket);
100 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
103 typedef CLIB_PACKED (struct {
105 * Backing store allocation. Since bihash manages its own
106 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
108 u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
109 u64 alloc_arena_size; /* Size of the arena */
110 u64 alloc_arena_mapped; /* Size of the mapped memory in the arena */
111 /* Two SVM pointers stored as 8-byte integers */
112 u64 alloc_lock_as_u64;
114 /* freelist list-head arrays/vectors */
115 u64 freelists_as_u64;
116 u32 nbuckets; /* Number of buckets */
117 /* Set when header valid */
120 }) BVT (clib_bihash_shared_header);
123 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
126 BVS (clib_bihash_alloc_chunk)
128 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
133 /* pointer to the next allocation */
136 /* number of bytes left in this chunk */
139 /* doubly linked list of heap allocated chunks */
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;
150 BVT (clib_bihash_value) ** working_copies;
151 int *working_copy_lengths;
152 BVT (clib_bihash_bucket) saved_bucket;
158 format_function_t *fmt_fn;
160 BVT (clib_bihash_alloc_chunk) * chunks;
165 BVT (clib_bihash_shared_header) * sh;
168 BVT (clib_bihash_shared_header) sh;
171 u64 alloc_arena; /* Base of the allocation arena */
172 volatile u8 instantiated;
173 u8 dont_add_to_all_bihash_list;
176 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
178 format_function_t *kvp_fmt_fn;
180 /** Optional statistics-gathering callback */
181 #if BIHASH_ENABLE_STATS
182 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
184 /** Statistics callback context (e.g. address of stats data structure) */
185 void *inc_stats_context;
192 BVT (clib_bihash) * h;
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);
201 extern void **clib_all_bihashes;
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) \
241 _(splits) /* must be last */
245 #define _(a) BIHASH_STAT_##a,
249 } BVT (clib_bihash_stat_id);
250 #endif /* BIHASH_STAT_IDS */
252 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
253 int stat_id, u64 count)
255 #if BIHASH_ENABLE_STATS
256 if (PREDICT_FALSE (h->inc_stats_callback != 0))
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 };
291 old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
293 if (PREDICT_FALSE (old & mask.as_u64))
295 /* somebody else flipped the bit, try again */
301 static inline void BV (clib_bihash_unlock_bucket)
302 (BVT (clib_bihash_bucket) * b)
307 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
310 u8 *hp = (u8 *) (uword) alloc_arena (h);
311 u8 *vp = hp + offset;
316 static inline int BV (clib_bihash_bucket_is_empty)
317 (BVT (clib_bihash_bucket) * b)
319 /* Note: applied to locked buckets, test offset */
320 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
321 return b->offset == 0;
323 return (b->log2_pages == 0 && b->refcnt == 1);
326 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
331 hp = (u8 *) (uword) alloc_arena (h);
340 void BV (clib_bihash_init)
341 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
343 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
346 void BV (clib_bihash_initiator_init_svm)
347 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
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,
353 format_function_t * kvp_fmt_fn);
355 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
357 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
358 BVT (clib_bihash_kv) * add_v, int is_add);
360 int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h,
361 BVT (clib_bihash_kv) * add_v, u64 hash,
363 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
364 BVT (clib_bihash_kv) * add_v,
365 int (*is_stale_cb) (BVT
369 int BV (clib_bihash_add_with_overwrite_cb) (
370 BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
371 void (*overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *arg);
372 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
373 BVT (clib_bihash_kv) * search_v,
374 BVT (clib_bihash_kv) * return_v);
376 int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
378 #define BIHASH_WALK_STOP 0
379 #define BIHASH_WALK_CONTINUE 1
382 int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
384 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
386 (clib_bihash_foreach_key_value_pair_cb)
388 void *clib_all_bihash_set_heap (void);
389 void clib_bihash_copied (void *dst, void *src);
391 format_function_t BV (format_bihash);
392 format_function_t BV (format_bihash_kvp);
393 format_function_t BV (format_bihash_lru);
396 BVT (clib_bihash_bucket) *
397 BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
399 #if BIHASH_KVP_AT_BUCKET_LEVEL
401 offset = (hash & (h->nbuckets - 1));
402 offset = offset * (sizeof (BVT (clib_bihash_bucket))
403 + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
404 return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
406 return h->buckets + (hash & (h->nbuckets - 1));
410 static inline int BV (clib_bihash_search_inline_with_hash)
411 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
413 BVT (clib_bihash_value) * v;
414 BVT (clib_bihash_bucket) * b;
418 static const BVT (clib_bihash_bucket) mask = {
424 #if BIHASH_LAZY_INSTANTIATE
425 if (PREDICT_FALSE (h->instantiated == 0))
429 b = BV (clib_bihash_get_bucket) (h, hash);
431 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
434 if (PREDICT_FALSE (b->lock))
436 volatile BVT (clib_bihash_bucket) * bv = b;
441 v = BV (clib_bihash_get_value) (h, b->offset);
443 /* If the bucket has unresolvable collisions, use linear search */
444 limit = BIHASH_KVP_PER_PAGE;
446 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
448 if (PREDICT_FALSE (b->linear_search))
449 limit <<= b->log2_pages;
451 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
454 for (i = 0; i < limit; i++)
456 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
458 *key_result = v->kvp[i];
465 static inline int BV (clib_bihash_search_inline)
466 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
470 hash = BV (clib_bihash_hash) (key_result);
472 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
475 static inline void BV (clib_bihash_prefetch_bucket)
476 (BVT (clib_bihash) * h, u64 hash)
478 CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
479 BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
483 static inline void BV (clib_bihash_prefetch_data)
484 (BVT (clib_bihash) * h, u64 hash)
486 BVT (clib_bihash_value) * v;
487 BVT (clib_bihash_bucket) * b;
489 #if BIHASH_LAZY_INSTANTIATE
490 if (PREDICT_FALSE (h->instantiated == 0))
494 b = BV (clib_bihash_get_bucket) (h, hash);
496 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
499 v = BV (clib_bihash_get_value) (h, b->offset);
501 if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
502 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
504 CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
508 static inline int BV (clib_bihash_search_inline_2_with_hash)
509 (BVT (clib_bihash) * h,
510 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
512 BVT (clib_bihash_value) * v;
513 BVT (clib_bihash_bucket) * b;
517 static const BVT (clib_bihash_bucket) mask = {
525 #if BIHASH_LAZY_INSTANTIATE
526 if (PREDICT_FALSE (h->instantiated == 0))
530 b = BV (clib_bihash_get_bucket) (h, hash);
532 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
535 if (PREDICT_FALSE (b->lock))
537 volatile BVT (clib_bihash_bucket) * bv = b;
542 v = BV (clib_bihash_get_value) (h, b->offset);
544 /* If the bucket has unresolvable collisions, use linear search */
545 limit = BIHASH_KVP_PER_PAGE;
547 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
549 if (PREDICT_FALSE (b->linear_search))
550 limit <<= b->log2_pages;
552 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
555 for (i = 0; i < limit; i++)
557 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
566 static inline int BV (clib_bihash_search_inline_2)
567 (BVT (clib_bihash) * h,
568 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
572 hash = BV (clib_bihash_hash) (search_key);
574 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
579 #endif /* __included_bihash_template_h__ */
584 * fd.io coding-style-patch-verification: ON
587 * eval: (c-set-style "gnu")