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
48 #define __bv(a,b) _bv(a,b)
49 #define BV(a) __bv(a,BIHASH_TYPE)
51 #define _bvt(a,b) a##b##_t
52 #define __bvt(a,b) _bvt(a,b)
53 #define BVT(a) __bvt(a,BIHASH_TYPE)
55 #define _bvs(a,b) struct a##b
56 #define __bvs(a,b) _bvs(a,b)
57 #define BVS(a) __bvs(a,BIHASH_TYPE)
60 #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0)
61 #define u64_to_pointer(x) (void *)(u32)((x))
62 #define pointer_to_u64(x) (u64)(u32)((x))
64 #define OVERFLOW_ASSERT(x)
65 #define u64_to_pointer(x) (void *)((x))
66 #define pointer_to_u64(x) (u64)((x))
69 typedef struct BV (clib_bihash_value)
73 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
76 } BVT (clib_bihash_value);
78 #define BIHASH_BUCKET_OFFSET_BITS 36
86 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
94 } BVT (clib_bihash_bucket);
96 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
99 typedef CLIB_PACKED (struct {
101 * Backing store allocation. Since bihash manages its own
102 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
104 u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
105 u64 alloc_arena_size; /* Size of the arena */
106 /* Two SVM pointers stored as 8-byte integers */
107 u64 alloc_lock_as_u64;
109 /* freelist list-head arrays/vectors */
110 u64 freelists_as_u64;
111 u32 nbuckets; /* Number of buckets */
112 /* Set when header valid */
115 }) BVT (clib_bihash_shared_header);
118 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
123 BVT (clib_bihash_bucket) * buckets;
124 volatile u32 *alloc_lock;
126 BVT (clib_bihash_value) ** working_copies;
127 int *working_copy_lengths;
128 BVT (clib_bihash_bucket) saved_bucket;
138 BVT (clib_bihash_shared_header) * sh;
141 BVT (clib_bihash_shared_header) sh;
144 u64 alloc_arena; /* Base of the allocation arena */
145 volatile u8 instantiated;
148 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
150 format_function_t *fmt_fn;
152 /** Optional statistics-gathering callback */
153 #if BIHASH_ENABLE_STATS
154 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
156 /** Statistics callback context (e.g. address of stats data structure) */
157 void *inc_stats_context;
162 extern void **clib_all_bihashes;
165 #undef alloc_arena_next
166 #undef alloc_arena_size
168 #undef CLIB_BIHASH_READY_MAGIC
169 #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
170 #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
171 #define alloc_arena(h) ((h)->alloc_arena)
172 #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
174 #undef alloc_arena_next
175 #undef alloc_arena_size
177 #undef CLIB_BIHASH_READY_MAGIC
178 #define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
179 #define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
180 #define alloc_arena(h) ((h)->alloc_arena)
181 #define CLIB_BIHASH_READY_MAGIC 0
184 #ifndef BIHASH_STAT_IDS
185 #define BIHASH_STAT_IDS 1
187 #define foreach_bihash_stat \
197 _(working_copy_lost) \
198 _(splits) /* must be last */
202 #define _(a) BIHASH_STAT_##a,
206 } BVT (clib_bihash_stat_id);
207 #endif /* BIHASH_STAT_IDS */
209 static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
210 int stat_id, u64 count)
212 #if BIHASH_ENABLE_STATS
213 if (PREDICT_FALSE (h->inc_stats_callback != 0))
214 h->inc_stats_callback (h, stat_id, count);
218 #if BIHASH_ENABLE_STATS
219 static inline void BV (clib_bihash_set_stats_callback)
220 (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
223 h->inc_stats_callback = cb;
224 h->inc_stats_context = ctx;
229 static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
231 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
235 static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
237 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
240 static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
242 BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
246 locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
247 unlocked_bucket.lock = 0;
248 locked_bucket.lock = 1;
251 while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
252 locked_bucket.as_u64, 1 /* weak */ ,
254 __ATOMIC_ACQUIRE) == 0);
257 static inline void BV (clib_bihash_unlock_bucket)
258 (BVT (clib_bihash_bucket) * b)
260 CLIB_MEMORY_BARRIER ();
264 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
267 u8 *hp = (u8 *) (uword) alloc_arena (h);
268 u8 *vp = hp + offset;
273 static inline int BV (clib_bihash_bucket_is_empty)
274 (BVT (clib_bihash_bucket) * b)
276 /* Note: applied to locked buckets, test offset */
277 return b->offset == 0;
280 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
285 hp = (u8 *) (uword) alloc_arena (h);
291 void BV (clib_bihash_init)
292 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
295 void BV (clib_bihash_master_init_svm)
296 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
297 void BV (clib_bihash_slave_init_svm)
298 (BVT (clib_bihash) * h, char *name, int fd);
301 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
302 format_function_t * fmt_fn);
304 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
306 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
307 BVT (clib_bihash_kv) * add_v, int is_add);
308 int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
309 BVT (clib_bihash_kv) * add_v,
310 int (*is_stale_cb) (BVT
314 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
315 BVT (clib_bihash_kv) * search_v,
316 BVT (clib_bihash_kv) * return_v);
318 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
319 void *callback, void *arg);
320 void *clib_all_bihash_set_heap (void);
321 void clib_bihash_copied (void *dst, void *src);
323 format_function_t BV (format_bihash);
324 format_function_t BV (format_bihash_kvp);
325 format_function_t BV (format_bihash_lru);
327 static inline int BV (clib_bihash_search_inline_with_hash)
328 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
331 BVT (clib_bihash_value) * v;
332 BVT (clib_bihash_bucket) * b;
335 if (PREDICT_FALSE (alloc_arena (h) == 0))
338 bucket_index = hash & (h->nbuckets - 1);
339 b = &h->buckets[bucket_index];
341 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
344 if (PREDICT_FALSE (b->lock))
346 volatile BVT (clib_bihash_bucket) * bv = b;
351 hash >>= h->log2_nbuckets;
353 v = BV (clib_bihash_get_value) (h, b->offset);
355 /* If the bucket has unresolvable collisions, use linear search */
356 limit = BIHASH_KVP_PER_PAGE;
357 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
358 if (PREDICT_FALSE (b->linear_search))
359 limit <<= b->log2_pages;
361 for (i = 0; i < limit; i++)
363 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
365 *key_result = v->kvp[i];
372 static inline int BV (clib_bihash_search_inline)
373 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
377 hash = BV (clib_bihash_hash) (key_result);
379 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
382 static inline void BV (clib_bihash_prefetch_bucket)
383 (BVT (clib_bihash) * h, u64 hash)
386 BVT (clib_bihash_bucket) * b;
388 bucket_index = hash & (h->nbuckets - 1);
389 b = &h->buckets[bucket_index];
391 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
394 static inline void BV (clib_bihash_prefetch_data)
395 (BVT (clib_bihash) * h, u64 hash)
398 BVT (clib_bihash_value) * v;
399 BVT (clib_bihash_bucket) * b;
401 if (PREDICT_FALSE (alloc_arena (h) == 0))
404 bucket_index = hash & (h->nbuckets - 1);
405 b = &h->buckets[bucket_index];
407 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
410 hash >>= h->log2_nbuckets;
411 v = BV (clib_bihash_get_value) (h, b->offset);
413 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
415 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
418 static inline int BV (clib_bihash_search_inline_2_with_hash)
419 (BVT (clib_bihash) * h,
420 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
423 BVT (clib_bihash_value) * v;
424 BVT (clib_bihash_bucket) * b;
429 if (PREDICT_FALSE (alloc_arena (h) == 0))
432 bucket_index = hash & (h->nbuckets - 1);
433 b = &h->buckets[bucket_index];
435 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
438 if (PREDICT_FALSE (b->lock))
440 volatile BVT (clib_bihash_bucket) * bv = b;
445 hash >>= h->log2_nbuckets;
446 v = BV (clib_bihash_get_value) (h, b->offset);
448 /* If the bucket has unresolvable collisions, use linear search */
449 limit = BIHASH_KVP_PER_PAGE;
450 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
451 if (PREDICT_FALSE (b->linear_search))
452 limit <<= b->log2_pages;
454 for (i = 0; i < limit; i++)
456 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
465 static inline int BV (clib_bihash_search_inline_2)
466 (BVT (clib_bihash) * h,
467 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
471 hash = BV (clib_bihash_hash) (search_key);
473 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
478 #endif /* __included_bihash_template_h__ */
483 * fd.io coding-style-patch-verification: ON
486 * eval: (c-set-style "gnu")