blob: 0865c2b3ab43825d4b512510e9e83529091cee14 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Chris Luke16bcf7d2016-09-01 14:31:46 -040017/** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
Dave Barachdd3a57f2016-07-27 16:58:51 -040018
Dave Barachc3799992016-08-15 11:12:27 -040019/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070020 * 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>
Dave Barach30765e72018-02-23 07:45:36 -050029#include <vppinfra/cache.h>
Damjan Marion2a03efe2018-07-20 21:48:59 +020030#include <vppinfra/lock.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
32#ifndef BIHASH_TYPE
33#error BIHASH_TYPE not defined
34#endif
35
Dave Barach9466c452018-08-24 17:21:14 -040036#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
Ed Warnickecb9cada2015-12-08 15:45:58 -070047#define _bv(a,b) a##b
48#define __bv(a,b) _bv(a,b)
49#define BV(a) __bv(a,BIHASH_TYPE)
50
51#define _bvt(a,b) a##b##_t
52#define __bvt(a,b) _bvt(a,b)
53#define BVT(a) __bvt(a,BIHASH_TYPE)
54
Dave Barach2ce28d62019-05-03 12:58:01 -040055#define _bvs(a,b) struct a##b
56#define __bvs(a,b) _bvs(a,b)
57#define BVS(a) __bvs(a,BIHASH_TYPE)
58
Dave Barach9466c452018-08-24 17:21:14 -040059#if _LP64 == 0
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))
63#else
64#define OVERFLOW_ASSERT(x)
65#define u64_to_pointer(x) (void *)((x))
66#define pointer_to_u64(x) (u64)((x))
67#endif
68
Dave Barachc3799992016-08-15 11:12:27 -040069typedef struct BV (clib_bihash_value)
70{
71 union
72 {
73 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
Dave Barach9466c452018-08-24 17:21:14 -040074 u64 next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075 };
Dave Barachc3799992016-08-15 11:12:27 -040076} BVT (clib_bihash_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -070077
Dave Barach508498f2018-07-19 12:11:16 -040078#define BIHASH_BUCKET_OFFSET_BITS 36
Dave Barach908a5ea2017-07-14 12:42:21 -040079
Dave Barachc3799992016-08-15 11:12:27 -040080typedef struct
81{
82 union
83 {
84 struct
85 {
Dave Barach508498f2018-07-19 12:11:16 -040086 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
87 u64 lock:1;
Damjan Marion882fcfe2018-07-17 23:01:49 +020088 u64 linear_search:1;
89 u64 log2_pages:8;
Dave Barach9466c452018-08-24 17:21:14 -040090 u64 refcnt:16;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091 };
92 u64 as_u64;
93 };
Dave Barach908a5ea2017-07-14 12:42:21 -040094} BVT (clib_bihash_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -070095
Damjan Marion882fcfe2018-07-17 23:01:49 +020096STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
Damjan Marion882fcfe2018-07-17 23:01:49 +020097
Dave Barach9466c452018-08-24 17:21:14 -040098/* *INDENT-OFF* */
99typedef CLIB_PACKED (struct {
100 /*
101 * Backing store allocation. Since bihash manages its own
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200102 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
Dave Barach9466c452018-08-24 17:21:14 -0400103 */
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200104 u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
Dave Barach9466c452018-08-24 17:21:14 -0400105 u64 alloc_arena_size; /* Size of the arena */
Dave Barach9466c452018-08-24 17:21:14 -0400106 /* Two SVM pointers stored as 8-byte integers */
107 u64 alloc_lock_as_u64;
108 u64 buckets_as_u64;
109 /* freelist list-head arrays/vectors */
110 u64 freelists_as_u64;
111 u32 nbuckets; /* Number of buckets */
112 /* Set when header valid */
113 volatile u32 ready;
Dave Barachffb14b92018-09-11 17:20:23 -0400114 u64 pad[2];
Dave Barach9466c452018-08-24 17:21:14 -0400115}) BVT (clib_bihash_shared_header);
116/* *INDENT-ON* */
117
118STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
119
Dave Barach2ce28d62019-05-03 12:58:01 -0400120typedef
121BVS (clib_bihash)
Dave Barachc3799992016-08-15 11:12:27 -0400122{
Dave Barach908a5ea2017-07-14 12:42:21 -0400123 BVT (clib_bihash_bucket) * buckets;
Dave Barach508498f2018-07-19 12:11:16 -0400124 volatile u32 *alloc_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125
Dave Barach2ce28d62019-05-03 12:58:01 -0400126 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400127 int *working_copy_lengths;
Dave Barach2ce28d62019-05-03 12:58:01 -0400128 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
130 u32 nbuckets;
131 u32 log2_nbuckets;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400132 u64 memory_size;
Dave Barachc3799992016-08-15 11:12:27 -0400133 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barach9466c452018-08-24 17:21:14 -0400135 u64 *freelists;
Dave Barach97f5af02018-02-22 09:48:45 -0500136
Dave Barach9466c452018-08-24 17:21:14 -0400137#if BIHASH_32_64_SVM
Dave Barach2ce28d62019-05-03 12:58:01 -0400138 BVT (clib_bihash_shared_header) * sh;
Dave Barach9466c452018-08-24 17:21:14 -0400139 int memfd;
140#else
Dave Barach2ce28d62019-05-03 12:58:01 -0400141 BVT (clib_bihash_shared_header) sh;
Dave Barach9466c452018-08-24 17:21:14 -0400142#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143
Dave Barachffb14b92018-09-11 17:20:23 -0400144 u64 alloc_arena; /* Base of the allocation arena */
Dave Barach67d09e02019-08-01 08:15:01 -0400145 volatile u8 instantiated;
Dave Barachffb14b92018-09-11 17:20:23 -0400146
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800147 /**
148 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
149 */
150 format_function_t *fmt_fn;
151
Dave Barach2ce28d62019-05-03 12:58:01 -0400152 /** Optional statistics-gathering callback */
153#if BIHASH_ENABLE_STATS
154 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
155
156 /** Statistics callback context (e.g. address of stats data structure) */
157 void *inc_stats_context;
158#endif
159
Dave Barachc3799992016-08-15 11:12:27 -0400160} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach32dcd3b2019-07-08 12:25:38 -0400162extern void **clib_all_bihashes;
163
Dave Barach9466c452018-08-24 17:21:14 -0400164#if BIHASH_32_64_SVM
165#undef alloc_arena_next
166#undef alloc_arena_size
167#undef alloc_arena
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)
Dave Barachffb14b92018-09-11 17:20:23 -0400171#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400172#define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
173#else
174#undef alloc_arena_next
175#undef alloc_arena_size
176#undef alloc_arena
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)
Dave Barachffb14b92018-09-11 17:20:23 -0400180#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400181#define CLIB_BIHASH_READY_MAGIC 0
182#endif
183
Dave Barach2ce28d62019-05-03 12:58:01 -0400184#ifndef BIHASH_STAT_IDS
185#define BIHASH_STAT_IDS 1
186
187#define foreach_bihash_stat \
188_(alloc_add) \
189_(add) \
190_(split_add) \
191_(replace) \
192_(update) \
193_(del) \
194_(del_free) \
195_(linear) \
196_(resplit) \
197_(working_copy_lost) \
198_(splits) /* must be last */
199
200typedef enum
201{
202#define _(a) BIHASH_STAT_##a,
203 foreach_bihash_stat
204#undef _
205 BIHASH_STAT_N_STATS,
206} BVT (clib_bihash_stat_id);
207#endif /* BIHASH_STAT_IDS */
208
209static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
210 int stat_id, u64 count)
211{
212#if BIHASH_ENABLE_STATS
213 if (PREDICT_FALSE (h->inc_stats_callback != 0))
214 h->inc_stats_callback (h, stat_id, count);
215#endif
216}
217
218#if BIHASH_ENABLE_STATS
219static inline void BV (clib_bihash_set_stats_callback)
220 (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
221 void *ctx)
222{
223 h->inc_stats_callback = cb;
224 h->inc_stats_context = ctx;
225}
226#endif
227
228
Dave Barach508498f2018-07-19 12:11:16 -0400229static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400230{
Dave Barach508498f2018-07-19 12:11:16 -0400231 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
Damjan Marion2a03efe2018-07-20 21:48:59 +0200232 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400233}
234
Dave Barach508498f2018-07-19 12:11:16 -0400235static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400236{
Dave Barach508498f2018-07-19 12:11:16 -0400237 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
Dave Barach908a5ea2017-07-14 12:42:21 -0400238}
239
Dave Barach508498f2018-07-19 12:11:16 -0400240static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400241{
Dave Barach508498f2018-07-19 12:11:16 -0400242 BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket;
Dave Barach908a5ea2017-07-14 12:42:21 -0400243
Dave Barach508498f2018-07-19 12:11:16 -0400244 do
245 {
246 locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64;
247 unlocked_bucket.lock = 0;
248 locked_bucket.lock = 1;
Damjan Marion2a03efe2018-07-20 21:48:59 +0200249 CLIB_PAUSE ();
Dave Barach508498f2018-07-19 12:11:16 -0400250 }
251 while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64,
252 locked_bucket.as_u64, 1 /* weak */ ,
253 __ATOMIC_ACQUIRE,
254 __ATOMIC_ACQUIRE) == 0);
Dave Barach858c06f2017-07-21 10:44:27 -0400255}
256
257static inline void BV (clib_bihash_unlock_bucket)
258 (BVT (clib_bihash_bucket) * b)
259{
Dave Barach508498f2018-07-19 12:11:16 -0400260 CLIB_MEMORY_BARRIER ();
261 b->lock = 0;
Dave Barach908a5ea2017-07-14 12:42:21 -0400262}
263
264static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400265 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266{
Dave Barach9466c452018-08-24 17:21:14 -0400267 u8 *hp = (u8 *) (uword) alloc_arena (h);
Dave Barachc3799992016-08-15 11:12:27 -0400268 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269
270 return (void *) vp;
271}
272
Damjan Marion882fcfe2018-07-17 23:01:49 +0200273static inline int BV (clib_bihash_bucket_is_empty)
274 (BVT (clib_bihash_bucket) * b)
275{
Dave Barach508498f2018-07-19 12:11:16 -0400276 /* Note: applied to locked buckets, test offset */
277 return b->offset == 0;
Damjan Marion882fcfe2018-07-17 23:01:49 +0200278}
279
Dave Barach908a5ea2017-07-14 12:42:21 -0400280static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400281 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282{
Dave Barachc3799992016-08-15 11:12:27 -0400283 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Dave Barach9466c452018-08-24 17:21:14 -0400285 hp = (u8 *) (uword) alloc_arena (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700286 vp = (u8 *) v;
287
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 return vp - hp;
289}
290
Dave Barachc3799992016-08-15 11:12:27 -0400291void BV (clib_bihash_init)
292 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
Dave Barach9466c452018-08-24 17:21:14 -0400294#if BIHASH_32_64_SVM
295void BV (clib_bihash_master_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400296 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
Dave Barach9466c452018-08-24 17:21:14 -0400297void BV (clib_bihash_slave_init_svm)
298 (BVT (clib_bihash) * h, char *name, int fd);
299#endif
300
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800301void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
302 format_function_t * fmt_fn);
303
Dave Barachc3799992016-08-15 11:12:27 -0400304void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305
Dave Barachc3799992016-08-15 11:12:27 -0400306int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
307 BVT (clib_bihash_kv) * add_v, int is_add);
Matus Fabian828d27e2018-08-21 03:15:50 -0700308int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
309 BVT (clib_bihash_kv) * add_v,
310 int (*is_stale_cb) (BVT
311 (clib_bihash_kv)
312 *, void *),
313 void *arg);
Dave Barach908a5ea2017-07-14 12:42:21 -0400314int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400315 BVT (clib_bihash_kv) * search_v,
316 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317
Dave Barachc3799992016-08-15 11:12:27 -0400318void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
319 void *callback, void *arg);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400320void *clib_all_bihash_set_heap (void);
321void clib_bihash_copied (void *dst, void *src);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322
Dave Barachc3799992016-08-15 11:12:27 -0400323format_function_t BV (format_bihash);
324format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400325format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Dave Barach30765e72018-02-23 07:45:36 -0500327static inline int BV (clib_bihash_search_inline_with_hash)
328 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400331 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400332 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500333 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334
Dave Barach32dcd3b2019-07-08 12:25:38 -0400335 if (PREDICT_FALSE (alloc_arena (h) == 0))
336 return -1;
337
Dave Barachc3799992016-08-15 11:12:27 -0400338 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700339 b = &h->buckets[bucket_index];
340
Damjan Marion882fcfe2018-07-17 23:01:49 +0200341 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 return -1;
343
Dave Barach508498f2018-07-19 12:11:16 -0400344 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400345 {
Dave Barach508498f2018-07-19 12:11:16 -0400346 volatile BVT (clib_bihash_bucket) * bv = b;
347 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200348 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400349 }
350
Ed Warnickecb9cada2015-12-08 15:45:58 -0700351 hash >>= h->log2_nbuckets;
352
Dave Barachc3799992016-08-15 11:12:27 -0400353 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400354
Dave Barach5e6b9582016-12-12 15:37:29 -0500355 /* 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;
360
361 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400363 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400364 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400365 *key_result = v->kvp[i];
Dave Barachc3799992016-08-15 11:12:27 -0400366 return 0;
367 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 }
369 return -1;
370}
371
Dave Barach30765e72018-02-23 07:45:36 -0500372static inline int BV (clib_bihash_search_inline)
373 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
374{
375 u64 hash;
376
377 hash = BV (clib_bihash_hash) (key_result);
378
379 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
380}
381
382static inline void BV (clib_bihash_prefetch_bucket)
383 (BVT (clib_bihash) * h, u64 hash)
384{
385 u32 bucket_index;
386 BVT (clib_bihash_bucket) * b;
387
388 bucket_index = hash & (h->nbuckets - 1);
389 b = &h->buckets[bucket_index];
390
391 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
392}
393
394static inline void BV (clib_bihash_prefetch_data)
395 (BVT (clib_bihash) * h, u64 hash)
396{
397 u32 bucket_index;
398 BVT (clib_bihash_value) * v;
399 BVT (clib_bihash_bucket) * b;
400
Dave Barach32dcd3b2019-07-08 12:25:38 -0400401 if (PREDICT_FALSE (alloc_arena (h) == 0))
402 return;
403
Dave Barach30765e72018-02-23 07:45:36 -0500404 bucket_index = hash & (h->nbuckets - 1);
405 b = &h->buckets[bucket_index];
406
Damjan Marion882fcfe2018-07-17 23:01:49 +0200407 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Dave Barach30765e72018-02-23 07:45:36 -0500408 return;
409
410 hash >>= h->log2_nbuckets;
411 v = BV (clib_bihash_get_value) (h, b->offset);
412
413 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
414
415 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
416}
417
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200418static inline int BV (clib_bihash_search_inline_2_with_hash)
Dave Barach908a5ea2017-07-14 12:42:21 -0400419 (BVT (clib_bihash) * h,
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200420 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400423 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400424 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500425 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Dave Barachc3799992016-08-15 11:12:27 -0400427 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
Dave Barach32dcd3b2019-07-08 12:25:38 -0400429 if (PREDICT_FALSE (alloc_arena (h) == 0))
430 return -1;
431
Dave Barachc3799992016-08-15 11:12:27 -0400432 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 b = &h->buckets[bucket_index];
434
Damjan Marion882fcfe2018-07-17 23:01:49 +0200435 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 return -1;
437
Dave Barach508498f2018-07-19 12:11:16 -0400438 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400439 {
Dave Barach508498f2018-07-19 12:11:16 -0400440 volatile BVT (clib_bihash_bucket) * bv = b;
441 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200442 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400443 }
444
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 hash >>= h->log2_nbuckets;
Dave Barachc3799992016-08-15 11:12:27 -0400446 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400447
Dave Barach5e6b9582016-12-12 15:37:29 -0500448 /* 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;
453
454 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455 {
Dave Barachc3799992016-08-15 11:12:27 -0400456 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
457 {
458 *valuep = v->kvp[i];
459 return 0;
460 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700461 }
462 return -1;
463}
464
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200465static 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)
468{
469 u64 hash;
470
471 hash = BV (clib_bihash_hash) (search_key);
472
473 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
474 valuep);
475}
476
477
Ed Warnickecb9cada2015-12-08 15:45:58 -0700478#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400479
Chris Luke16bcf7d2016-09-01 14:31:46 -0400480/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400481
482/*
483 * fd.io coding-style-patch-verification: ON
484 *
485 * Local Variables:
486 * eval: (c-set-style "gnu")
487 * End:
488 */