blob: 1ca0ae447f8286d806b627d0e622fd81e1edd331 [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
Dave Barach16e4a4a2020-04-16 12:00:14 -040047/* default is 2MB, use 30 for 1GB */
48#ifndef BIHASH_LOG2_HUGEPAGE_SIZE
49#define BIHASH_LOG2_HUGEPAGE_SIZE 21
50#endif
51
Ed Warnickecb9cada2015-12-08 15:45:58 -070052#define _bv(a,b) a##b
53#define __bv(a,b) _bv(a,b)
54#define BV(a) __bv(a,BIHASH_TYPE)
55
56#define _bvt(a,b) a##b##_t
57#define __bvt(a,b) _bvt(a,b)
58#define BVT(a) __bvt(a,BIHASH_TYPE)
59
Dave Barach2ce28d62019-05-03 12:58:01 -040060#define _bvs(a,b) struct a##b
61#define __bvs(a,b) _bvs(a,b)
62#define BVS(a) __bvs(a,BIHASH_TYPE)
63
Dave Barach9466c452018-08-24 17:21:14 -040064#if _LP64 == 0
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))
68#else
69#define OVERFLOW_ASSERT(x)
70#define u64_to_pointer(x) (void *)((x))
71#define pointer_to_u64(x) (u64)((x))
72#endif
73
Dave Barachc3799992016-08-15 11:12:27 -040074typedef struct BV (clib_bihash_value)
75{
76 union
77 {
78 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
Dave Barach9466c452018-08-24 17:21:14 -040079 u64 next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080 };
Dave Barachc3799992016-08-15 11:12:27 -040081} BVT (clib_bihash_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Dave Barach508498f2018-07-19 12:11:16 -040083#define BIHASH_BUCKET_OFFSET_BITS 36
Dave Barach908a5ea2017-07-14 12:42:21 -040084
Dave Barachc3799992016-08-15 11:12:27 -040085typedef struct
86{
87 union
88 {
89 struct
90 {
Dave Barach508498f2018-07-19 12:11:16 -040091 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
92 u64 lock:1;
Damjan Marion882fcfe2018-07-17 23:01:49 +020093 u64 linear_search:1;
94 u64 log2_pages:8;
Dave Barach9466c452018-08-24 17:21:14 -040095 u64 refcnt:16;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 };
97 u64 as_u64;
98 };
Dave Barach908a5ea2017-07-14 12:42:21 -040099} BVT (clib_bihash_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
Damjan Marion882fcfe2018-07-17 23:01:49 +0200101STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
Damjan Marion882fcfe2018-07-17 23:01:49 +0200102
Dave Barach9466c452018-08-24 17:21:14 -0400103/* *INDENT-OFF* */
104typedef CLIB_PACKED (struct {
105 /*
106 * Backing store allocation. Since bihash manages its own
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200107 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
Dave Barach9466c452018-08-24 17:21:14 -0400108 */
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200109 u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
Dave Barach9466c452018-08-24 17:21:14 -0400110 u64 alloc_arena_size; /* Size of the arena */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400111 u64 alloc_arena_mapped; /* Size of the mapped memory in the arena */
Dave Barach9466c452018-08-24 17:21:14 -0400112 /* Two SVM pointers stored as 8-byte integers */
113 u64 alloc_lock_as_u64;
114 u64 buckets_as_u64;
115 /* freelist list-head arrays/vectors */
116 u64 freelists_as_u64;
117 u32 nbuckets; /* Number of buckets */
118 /* Set when header valid */
119 volatile u32 ready;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400120 u64 pad[1];
Dave Barach9466c452018-08-24 17:21:14 -0400121}) BVT (clib_bihash_shared_header);
122/* *INDENT-ON* */
123
124STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
125
Dave Barach2ce28d62019-05-03 12:58:01 -0400126typedef
Damjan Marion2454de22020-09-26 19:32:34 +0200127BVS (clib_bihash_alloc_chunk)
128{
129 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
130
131 /* chunk size */
132 uword size;
133
134 /* pointer to the next allocation */
135 u8 *next_alloc;
136
137 /* number of bytes left in this chunk */
138 uword bytes_left;
139
140 /* doubly linked list of heap allocated chunks */
141 BVS (clib_bihash_alloc_chunk) * prev, *next;
142
143} BVT (clib_bihash_alloc_chunk);
144
145typedef
Dave Barach2ce28d62019-05-03 12:58:01 -0400146BVS (clib_bihash)
Dave Barachc3799992016-08-15 11:12:27 -0400147{
Dave Barach908a5ea2017-07-14 12:42:21 -0400148 BVT (clib_bihash_bucket) * buckets;
Dave Barach508498f2018-07-19 12:11:16 -0400149 volatile u32 *alloc_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
Dave Barach2ce28d62019-05-03 12:58:01 -0400151 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400152 int *working_copy_lengths;
Dave Barach2ce28d62019-05-03 12:58:01 -0400153 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
155 u32 nbuckets;
156 u32 log2_nbuckets;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400157 u64 memory_size;
Dave Barachc3799992016-08-15 11:12:27 -0400158 u8 *name;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200159 format_function_t *fmt_fn;
Damjan Marion2454de22020-09-26 19:32:34 +0200160 void *heap;
161 BVT (clib_bihash_alloc_chunk) * chunks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Dave Barach9466c452018-08-24 17:21:14 -0400163 u64 *freelists;
Dave Barach97f5af02018-02-22 09:48:45 -0500164
Dave Barach9466c452018-08-24 17:21:14 -0400165#if BIHASH_32_64_SVM
Dave Barach2ce28d62019-05-03 12:58:01 -0400166 BVT (clib_bihash_shared_header) * sh;
Dave Barach9466c452018-08-24 17:21:14 -0400167 int memfd;
168#else
Dave Barach2ce28d62019-05-03 12:58:01 -0400169 BVT (clib_bihash_shared_header) sh;
Dave Barach9466c452018-08-24 17:21:14 -0400170#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171
Dave Barachffb14b92018-09-11 17:20:23 -0400172 u64 alloc_arena; /* Base of the allocation arena */
Dave Barach67d09e02019-08-01 08:15:01 -0400173 volatile u8 instantiated;
Dave Barachffb14b92018-09-11 17:20:23 -0400174
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800175 /**
176 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
177 */
Damjan Marionf2b4a372020-09-30 14:15:24 +0200178 format_function_t *kvp_fmt_fn;
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800179
Dave Barach2ce28d62019-05-03 12:58:01 -0400180 /** Optional statistics-gathering callback */
181#if BIHASH_ENABLE_STATS
182 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
183
184 /** Statistics callback context (e.g. address of stats data structure) */
185 void *inc_stats_context;
186#endif
187
Dave Barachc3799992016-08-15 11:12:27 -0400188} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189
Dave Barachbdf9b972019-09-03 10:57:19 -0400190typedef struct
191{
192 BVT (clib_bihash) * h;
193 char *name;
194 u32 nbuckets;
195 uword memory_size;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200196 format_function_t *kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400197 u8 instantiate_immediately;
198 u8 dont_add_to_all_bihash_list;
199} BVT (clib_bihash_init2_args);
200
Dave Barach32dcd3b2019-07-08 12:25:38 -0400201extern void **clib_all_bihashes;
202
Dave Barach9466c452018-08-24 17:21:14 -0400203#if BIHASH_32_64_SVM
204#undef alloc_arena_next
205#undef alloc_arena_size
Dave Barach16e4a4a2020-04-16 12:00:14 -0400206#undef alloc_arena_mapped
Dave Barach9466c452018-08-24 17:21:14 -0400207#undef alloc_arena
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)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400211#define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
Dave Barachffb14b92018-09-11 17:20:23 -0400212#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400213#define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
214#else
215#undef alloc_arena_next
216#undef alloc_arena_size
Dave Barach16e4a4a2020-04-16 12:00:14 -0400217#undef alloc_arena_mapped
Dave Barach9466c452018-08-24 17:21:14 -0400218#undef alloc_arena
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)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400222#define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
Dave Barachffb14b92018-09-11 17:20:23 -0400223#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400224#define CLIB_BIHASH_READY_MAGIC 0
225#endif
226
Dave Barach2ce28d62019-05-03 12:58:01 -0400227#ifndef BIHASH_STAT_IDS
228#define BIHASH_STAT_IDS 1
229
230#define foreach_bihash_stat \
231_(alloc_add) \
232_(add) \
233_(split_add) \
234_(replace) \
235_(update) \
236_(del) \
237_(del_free) \
238_(linear) \
239_(resplit) \
240_(working_copy_lost) \
241_(splits) /* must be last */
242
243typedef enum
244{
245#define _(a) BIHASH_STAT_##a,
246 foreach_bihash_stat
247#undef _
248 BIHASH_STAT_N_STATS,
249} BVT (clib_bihash_stat_id);
250#endif /* BIHASH_STAT_IDS */
251
252static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
253 int stat_id, u64 count)
254{
255#if BIHASH_ENABLE_STATS
256 if (PREDICT_FALSE (h->inc_stats_callback != 0))
257 h->inc_stats_callback (h, stat_id, count);
258#endif
259}
260
261#if BIHASH_ENABLE_STATS
262static inline void BV (clib_bihash_set_stats_callback)
263 (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
264 void *ctx)
265{
266 h->inc_stats_callback = cb;
267 h->inc_stats_context = ctx;
268}
269#endif
270
271
Dave Barach508498f2018-07-19 12:11:16 -0400272static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400273{
Dave Barach508498f2018-07-19 12:11:16 -0400274 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
Damjan Marion2a03efe2018-07-20 21:48:59 +0200275 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400276}
277
Dave Barach508498f2018-07-19 12:11:16 -0400278static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400279{
Dave Barach508498f2018-07-19 12:11:16 -0400280 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
Dave Barach908a5ea2017-07-14 12:42:21 -0400281}
282
Dave Barach508498f2018-07-19 12:11:16 -0400283static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400284{
Damjan Marion74ee18b2020-04-21 20:14:34 +0200285 /* *INDENT-OFF* */
286 BVT (clib_bihash_bucket) mask = { .lock = 1 };
287 /* *INDENT-ON* */
288 u64 old;
Dave Barach908a5ea2017-07-14 12:42:21 -0400289
Damjan Marion74ee18b2020-04-21 20:14:34 +0200290try_again:
291 old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200292
Damjan Marion74ee18b2020-04-21 20:14:34 +0200293 if (PREDICT_FALSE (old & mask.as_u64))
Damjan Marion801ec2a2020-04-21 19:42:30 +0200294 {
Damjan Marion74ee18b2020-04-21 20:14:34 +0200295 /* somebody else flipped the bit, try again */
Damjan Marion801ec2a2020-04-21 19:42:30 +0200296 CLIB_PAUSE ();
Damjan Marion74ee18b2020-04-21 20:14:34 +0200297 goto try_again;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200298 }
Dave Barach858c06f2017-07-21 10:44:27 -0400299}
300
301static inline void BV (clib_bihash_unlock_bucket)
302 (BVT (clib_bihash_bucket) * b)
303{
Dave Barach508498f2018-07-19 12:11:16 -0400304 b->lock = 0;
Dave Barach908a5ea2017-07-14 12:42:21 -0400305}
306
307static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400308 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309{
Dave Barach9466c452018-08-24 17:21:14 -0400310 u8 *hp = (u8 *) (uword) alloc_arena (h);
Dave Barachc3799992016-08-15 11:12:27 -0400311 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
313 return (void *) vp;
314}
315
Damjan Marion882fcfe2018-07-17 23:01:49 +0200316static inline int BV (clib_bihash_bucket_is_empty)
317 (BVT (clib_bihash_bucket) * b)
318{
Dave Barach508498f2018-07-19 12:11:16 -0400319 /* Note: applied to locked buckets, test offset */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400320 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
321 return b->offset == 0;
322 else
323 return (b->log2_pages == 0 && b->refcnt == 1);
Damjan Marion882fcfe2018-07-17 23:01:49 +0200324}
325
Dave Barach908a5ea2017-07-14 12:42:21 -0400326static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400327 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328{
Dave Barachc3799992016-08-15 11:12:27 -0400329 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
Dave Barach9466c452018-08-24 17:21:14 -0400331 hp = (u8 *) (uword) alloc_arena (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332 vp = (u8 *) v;
333
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 return vp - hp;
335}
336
Paul Atkins2ac41352021-01-19 15:22:23 +0000337#define BIHASH_ADD 1
338#define BIHASH_DEL 0
339
Dave Barachc3799992016-08-15 11:12:27 -0400340void BV (clib_bihash_init)
341 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342
Dave Barachbdf9b972019-09-03 10:57:19 -0400343void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
344
Dave Barach9466c452018-08-24 17:21:14 -0400345#if BIHASH_32_64_SVM
Dave Barachd4a639b2020-08-06 11:38:40 -0400346void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400347 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
Dave Barachd4a639b2020-08-06 11:38:40 -0400348void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400349 (BVT (clib_bihash) * h, char *name, int fd);
350#endif
351
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800352void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200353 format_function_t * kvp_fmt_fn);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800354
Dave Barachc3799992016-08-15 11:12:27 -0400355void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Dave Barachc3799992016-08-15 11:12:27 -0400357int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
358 BVT (clib_bihash_kv) * add_v, int is_add);
Matus Fabian828d27e2018-08-21 03:15:50 -0700359int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
360 BVT (clib_bihash_kv) * add_v,
361 int (*is_stale_cb) (BVT
362 (clib_bihash_kv)
363 *, void *),
364 void *arg);
Dave Barach908a5ea2017-07-14 12:42:21 -0400365int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400366 BVT (clib_bihash_kv) * search_v,
367 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Neale Rannseb696482020-09-29 14:44:23 +0000369int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
370
Neale Rannsf50bac12019-12-06 05:53:17 +0000371#define BIHASH_WALK_STOP 0
372#define BIHASH_WALK_CONTINUE 1
373
374typedef
375 int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
376 void *);
Dave Barachc3799992016-08-15 11:12:27 -0400377void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
Neale Rannsf50bac12019-12-06 05:53:17 +0000378 BV
379 (clib_bihash_foreach_key_value_pair_cb)
380 cb, void *arg);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400381void *clib_all_bihash_set_heap (void);
382void clib_bihash_copied (void *dst, void *src);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700383
Dave Barachc3799992016-08-15 11:12:27 -0400384format_function_t BV (format_bihash);
385format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400386format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700387
Dave Barach16e4a4a2020-04-16 12:00:14 -0400388static inline
389BVT (clib_bihash_bucket) *
390BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
391{
392#if BIHASH_KVP_AT_BUCKET_LEVEL
393 uword offset;
394 offset = (hash & (h->nbuckets - 1));
395 offset = offset * (sizeof (BVT (clib_bihash_bucket))
396 + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
397 return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200398#else
Dave Barach16e4a4a2020-04-16 12:00:14 -0400399 return h->buckets + (hash & (h->nbuckets - 1));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200400#endif
Dave Barach16e4a4a2020-04-16 12:00:14 -0400401}
402
Dave Barach30765e72018-02-23 07:45:36 -0500403static inline int BV (clib_bihash_search_inline_with_hash)
404 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
Dave Barachc3799992016-08-15 11:12:27 -0400406 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400407 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500408 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409
Damjan Marion68e5fd52020-04-23 13:41:47 +0200410 /* *INDENT-OFF* */
411 static const BVT (clib_bihash_bucket) mask = {
412 .linear_search = 1,
413 .log2_pages = -1
414 };
415 /* *INDENT-ON* */
416
Dave Barach16e4a4a2020-04-16 12:00:14 -0400417#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200418 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400419 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400420#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400421
Dave Barach16e4a4a2020-04-16 12:00:14 -0400422 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Damjan Marion882fcfe2018-07-17 23:01:49 +0200424 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 return -1;
426
Dave Barach508498f2018-07-19 12:11:16 -0400427 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400428 {
Dave Barach508498f2018-07-19 12:11:16 -0400429 volatile BVT (clib_bihash_bucket) * bv = b;
430 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200431 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400432 }
433
Dave Barachc3799992016-08-15 11:12:27 -0400434 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400435
Dave Barach5e6b9582016-12-12 15:37:29 -0500436 /* If the bucket has unresolvable collisions, use linear search */
437 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200438
439 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
440 {
441 if (PREDICT_FALSE (b->linear_search))
442 limit <<= b->log2_pages;
443 else
444 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
445 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500446
447 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400449 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400450 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400451 *key_result = v->kvp[i];
Dave Barachc3799992016-08-15 11:12:27 -0400452 return 0;
453 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 }
455 return -1;
456}
457
Dave Barach30765e72018-02-23 07:45:36 -0500458static inline int BV (clib_bihash_search_inline)
459 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
460{
461 u64 hash;
462
463 hash = BV (clib_bihash_hash) (key_result);
464
465 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
466}
467
468static inline void BV (clib_bihash_prefetch_bucket)
469 (BVT (clib_bihash) * h, u64 hash)
470{
Dave Barach16e4a4a2020-04-16 12:00:14 -0400471 CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
472 BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
473 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500474}
475
476static inline void BV (clib_bihash_prefetch_data)
477 (BVT (clib_bihash) * h, u64 hash)
478{
Dave Barach30765e72018-02-23 07:45:36 -0500479 BVT (clib_bihash_value) * v;
480 BVT (clib_bihash_bucket) * b;
481
Dave Barach16e4a4a2020-04-16 12:00:14 -0400482#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200483 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400484 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400485#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400486
Damjan Marion4e149772020-03-27 16:57:28 +0100487 b = BV (clib_bihash_get_bucket) (h, hash);
Dave Barach30765e72018-02-23 07:45:36 -0500488
Damjan Marion882fcfe2018-07-17 23:01:49 +0200489 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Dave Barach30765e72018-02-23 07:45:36 -0500490 return;
491
Dave Barach30765e72018-02-23 07:45:36 -0500492 v = BV (clib_bihash_get_value) (h, b->offset);
493
Damjan Marion68e5fd52020-04-23 13:41:47 +0200494 if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
495 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
Dave Barach30765e72018-02-23 07:45:36 -0500496
Damjan Marion68e5fd52020-04-23 13:41:47 +0200497 CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
498 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500499}
500
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200501static inline int BV (clib_bihash_search_inline_2_with_hash)
Dave Barach908a5ea2017-07-14 12:42:21 -0400502 (BVT (clib_bihash) * h,
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200503 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700504{
Dave Barachc3799992016-08-15 11:12:27 -0400505 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400506 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500507 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508
Damjan Marion68e5fd52020-04-23 13:41:47 +0200509/* *INDENT-OFF* */
510 static const BVT (clib_bihash_bucket) mask = {
511 .linear_search = 1,
512 .log2_pages = -1
513 };
514/* *INDENT-ON* */
515
Dave Barachc3799992016-08-15 11:12:27 -0400516 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517
Dave Barach16e4a4a2020-04-16 12:00:14 -0400518#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200519 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400520 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400521#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400522
Damjan Marion4e149772020-03-27 16:57:28 +0100523 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Damjan Marion882fcfe2018-07-17 23:01:49 +0200525 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700526 return -1;
527
Dave Barach508498f2018-07-19 12:11:16 -0400528 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400529 {
Dave Barach508498f2018-07-19 12:11:16 -0400530 volatile BVT (clib_bihash_bucket) * bv = b;
531 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200532 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400533 }
534
Dave Barachc3799992016-08-15 11:12:27 -0400535 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400536
Dave Barach5e6b9582016-12-12 15:37:29 -0500537 /* If the bucket has unresolvable collisions, use linear search */
538 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200539
540 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
541 {
542 if (PREDICT_FALSE (b->linear_search))
543 limit <<= b->log2_pages;
544 else
545 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
546 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500547
548 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549 {
Dave Barachc3799992016-08-15 11:12:27 -0400550 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
551 {
552 *valuep = v->kvp[i];
553 return 0;
554 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700555 }
556 return -1;
557}
558
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200559static inline int BV (clib_bihash_search_inline_2)
560 (BVT (clib_bihash) * h,
561 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
562{
563 u64 hash;
564
565 hash = BV (clib_bihash_hash) (search_key);
566
567 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
568 valuep);
569}
570
571
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400573
Chris Luke16bcf7d2016-09-01 14:31:46 -0400574/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400575
576/*
577 * fd.io coding-style-patch-verification: ON
578 *
579 * Local Variables:
580 * eval: (c-set-style "gnu")
581 * End:
582 */