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 #undef HAVE_MEMFD_CREATE
38 #include <vppinfra/linux/syscall.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
47 /* default is 2MB, use 30 for 1GB */
48 #ifndef BIHASH_LOG2_HUGEPAGE_SIZE
49 #define BIHASH_LOG2_HUGEPAGE_SIZE 21
53 #define __bv(a,b) _bv(a,b)
54 #define BV(a) __bv(a,BIHASH_TYPE)
56 #define _bvt(a,b) a##b##_t
57 #define __bvt(a,b) _bvt(a,b)
58 #define BVT(a) __bvt(a,BIHASH_TYPE)
60 #define _bvs(a,b) struct a##b
61 #define __bvs(a,b) _bvs(a,b)
62 #define BVS(a) __bvs(a,BIHASH_TYPE)
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))
69 #define OVERFLOW_ASSERT(x)
70 #define u64_to_pointer(x) (void *)((x))
71 #define pointer_to_u64(x) (u64)((x))
74 typedef struct BV (clib_bihash_value)
78 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
81 } BVT (clib_bihash_value);
83 #define BIHASH_BUCKET_OFFSET_BITS 36
91 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
99 } BVT (clib_bihash_bucket);
101 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
104 typedef CLIB_PACKED (struct {
106 * Backing store allocation. Since bihash manages its own
107 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
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;
115 /* freelist list-head arrays/vectors */
116 u64 freelists_as_u64;
117 u32 nbuckets; /* Number of buckets */
118 /* Set when header valid */
121 }) BVT (clib_bihash_shared_header);
124 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
129 BVT (clib_bihash_bucket) * buckets;
130 volatile u32 *alloc_lock;
132 BVT (clib_bihash_value) ** working_copies;
133 int *working_copy_lengths;
134 BVT (clib_bihash_bucket) saved_bucket;
144 BVT (clib_bihash_shared_header) * sh;
147 BVT (clib_bihash_shared_header) sh;
150 u64 alloc_arena; /* Base of the allocation arena */
151 volatile u8 instantiated;
154 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
156 format_function_t *fmt_fn;
158 /** Optional statistics-gathering callback */
159 #if BIHASH_ENABLE_STATS
160 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
162 /** Statistics callback context (e.g. address of stats data structure) */
163 void *inc_stats_context;
170 BVT (clib_bihash) * h;
174 format_function_t *fmt_fn;
175 u8 instantiate_immediately;
176 u8 dont_add_to_all_bihash_list;
177 } BVT (clib_bihash_init2_args);
179 extern void **clib_all_bihashes;
182 #undef alloc_arena_next
183 #undef alloc_arena_size
184 #undef alloc_arena_mapped
186 #undef CLIB_BIHASH_READY_MAGIC
187 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
188 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
189 #define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
190 #define alloc_arena(h) ((h)->alloc_arena)
191 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
193 #undef alloc_arena_next
194 #undef alloc_arena_size
195 #undef alloc_arena_mapped
197 #undef CLIB_BIHASH_READY_MAGIC
198 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
199 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
200 #define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
201 #define alloc_arena(h) ((h)->alloc_arena)
202 #define CLIB_BIHASH_READY_MAGIC 0
205 #ifndef BIHASH_STAT_IDS
206 #define BIHASH_STAT_IDS 1
208 #define foreach_bihash_stat \
218 _(working_copy_lost) \
219 _(splits) /* must be last */
223 #define _(a) BIHASH_STAT_##a,
227 } BVT (clib_bihash_stat_id);
228 #endif /* BIHASH_STAT_IDS */
230 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
231 int stat_id, u64 count)
233 #if BIHASH_ENABLE_STATS
234 if (PREDICT_FALSE (h->inc_stats_callback != 0))
235 h->inc_stats_callback (h, stat_id, count);
239 #if BIHASH_ENABLE_STATS
240 static inline void BV (clib_bihash_set_stats_callback)
241 (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
244 h->inc_stats_callback = cb;
245 h->inc_stats_context = ctx;
250 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
252 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
256 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
258 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
261 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
263 BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
267 locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
268 unlocked_bucket.lock = 0;
269 locked_bucket.lock = 1;
272 while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
273 locked_bucket.as_u64, 1 /* weak */ ,
275 __ATOMIC_ACQUIRE) == 0);
278 static inline void BV (clib_bihash_unlock_bucket)
279 (BVT (clib_bihash_bucket) * b)
281 CLIB_MEMORY_BARRIER ();
285 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
288 u8 *hp = (u8 *) (uword) alloc_arena (h);
289 u8 *vp = hp + offset;
294 static inline int BV (clib_bihash_bucket_is_empty)
295 (BVT (clib_bihash_bucket) * b)
297 /* Note: applied to locked buckets, test offset */
298 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
299 return b->offset == 0;
301 return (b->log2_pages == 0 && b->refcnt == 1);
304 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
309 hp = (u8 *) (uword) alloc_arena (h);
315 void BV (clib_bihash_init)
316 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
318 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
321 void BV (clib_bihash_master_init_svm)
322 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
323 void BV (clib_bihash_slave_init_svm)
324 (BVT (clib_bihash) * h, char *name, int fd);
327 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
328 format_function_t * fmt_fn);
330 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
332 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
333 BVT (clib_bihash_kv) * add_v, int is_add);
334 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
335 BVT (clib_bihash_kv) * add_v,
336 int (*is_stale_cb) (BVT
340 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
341 BVT (clib_bihash_kv) * search_v,
342 BVT (clib_bihash_kv) * return_v);
344 #define BIHASH_WALK_STOP 0
345 #define BIHASH_WALK_CONTINUE 1
348 int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
350 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
352 (clib_bihash_foreach_key_value_pair_cb)
354 void *clib_all_bihash_set_heap (void);
355 void clib_bihash_copied (void *dst, void *src);
357 format_function_t BV (format_bihash);
358 format_function_t BV (format_bihash_kvp);
359 format_function_t BV (format_bihash_lru);
362 BVT (clib_bihash_bucket) *
363 BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
365 #if BIHASH_KVP_AT_BUCKET_LEVEL
367 offset = (hash & (h->nbuckets - 1));
368 offset = offset * (sizeof (BVT (clib_bihash_bucket))
369 + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
370 return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
373 return h->buckets + (hash & (h->nbuckets - 1));
376 static inline int BV (clib_bihash_search_inline_with_hash)
377 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
379 BVT (clib_bihash_value) * v;
380 BVT (clib_bihash_bucket) * b;
383 #if BIHASH_LAZY_INSTANTIATE
384 if (PREDICT_FALSE (alloc_arena (h) == 0))
388 b = BV (clib_bihash_get_bucket) (h, hash);
390 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
393 if (PREDICT_FALSE (b->lock))
395 volatile BVT (clib_bihash_bucket) * bv = b;
400 hash >>= h->log2_nbuckets;
402 v = BV (clib_bihash_get_value) (h, b->offset);
404 /* If the bucket has unresolvable collisions, use linear search */
405 limit = BIHASH_KVP_PER_PAGE;
406 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
407 if (PREDICT_FALSE (b->linear_search))
408 limit <<= b->log2_pages;
410 for (i = 0; i < limit; i++)
412 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
414 *key_result = v->kvp[i];
421 static inline int BV (clib_bihash_search_inline)
422 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
426 hash = BV (clib_bihash_hash) (key_result);
428 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
431 static inline void BV (clib_bihash_prefetch_bucket)
432 (BVT (clib_bihash) * h, u64 hash)
434 CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
435 BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
439 static inline void BV (clib_bihash_prefetch_data)
440 (BVT (clib_bihash) * h, u64 hash)
442 BVT (clib_bihash_value) * v;
443 BVT (clib_bihash_bucket) * b;
445 #if BIHASH_LAZY_INSTANTIATE
446 if (PREDICT_FALSE (alloc_arena (h) == 0))
450 b = BV (clib_bihash_get_bucket) (h, hash);
452 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
455 hash >>= h->log2_nbuckets;
456 v = BV (clib_bihash_get_value) (h, b->offset);
458 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
460 clib_prefetch_load (v);
463 static inline int BV (clib_bihash_search_inline_2_with_hash)
464 (BVT (clib_bihash) * h,
465 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
467 BVT (clib_bihash_value) * v;
468 BVT (clib_bihash_bucket) * b;
473 #if BIHASH_LAZY_INSTANTIATE
474 if (PREDICT_FALSE (alloc_arena (h) == 0))
478 b = BV (clib_bihash_get_bucket) (h, hash);
480 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
483 if (PREDICT_FALSE (b->lock))
485 volatile BVT (clib_bihash_bucket) * bv = b;
490 hash >>= h->log2_nbuckets;
491 v = BV (clib_bihash_get_value) (h, b->offset);
493 /* If the bucket has unresolvable collisions, use linear search */
494 limit = BIHASH_KVP_PER_PAGE;
495 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
496 if (PREDICT_FALSE (b->linear_search))
497 limit <<= b->log2_pages;
499 for (i = 0; i < limit; i++)
501 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
510 static inline int BV (clib_bihash_search_inline_2)
511 (BVT (clib_bihash) * h,
512 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
516 hash = BV (clib_bihash_hash) (search_key);
518 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
523 #endif /* __included_bihash_template_h__ */
528 * fd.io coding-style-patch-verification: ON
531 * eval: (c-set-style "gnu")