blob: e8a2c01cecedb3be1a6c9f038e93253f2733beb1 [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
Dave Barach9466c452018-08-24 17:21:14 -040037#include <vppinfra/linux/syscall.h>
38#include <fcntl.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
44#endif
45
Dave Barach16e4a4a2020-04-16 12:00:14 -040046/* default is 2MB, use 30 for 1GB */
47#ifndef BIHASH_LOG2_HUGEPAGE_SIZE
48#define BIHASH_LOG2_HUGEPAGE_SIZE 21
49#endif
50
Ed Warnickecb9cada2015-12-08 15:45:58 -070051#define _bv(a,b) a##b
52#define __bv(a,b) _bv(a,b)
53#define BV(a) __bv(a,BIHASH_TYPE)
54
55#define _bvt(a,b) a##b##_t
56#define __bvt(a,b) _bvt(a,b)
57#define BVT(a) __bvt(a,BIHASH_TYPE)
58
Dave Barach2ce28d62019-05-03 12:58:01 -040059#define _bvs(a,b) struct a##b
60#define __bvs(a,b) _bvs(a,b)
61#define BVS(a) __bvs(a,BIHASH_TYPE)
62
Dave Barach9466c452018-08-24 17:21:14 -040063#if _LP64 == 0
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))
67#else
68#define OVERFLOW_ASSERT(x)
69#define u64_to_pointer(x) (void *)((x))
70#define pointer_to_u64(x) (u64)((x))
71#endif
72
Dave Barachc3799992016-08-15 11:12:27 -040073typedef struct BV (clib_bihash_value)
74{
75 union
76 {
77 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
Dave Barach9466c452018-08-24 17:21:14 -040078 u64 next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 };
Dave Barachc3799992016-08-15 11:12:27 -040080} BVT (clib_bihash_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Dave Barach508498f2018-07-19 12:11:16 -040082#define BIHASH_BUCKET_OFFSET_BITS 36
Dave Barach908a5ea2017-07-14 12:42:21 -040083
Dave Barachc3799992016-08-15 11:12:27 -040084typedef struct
85{
86 union
87 {
88 struct
89 {
Dave Barach508498f2018-07-19 12:11:16 -040090 u64 offset:BIHASH_BUCKET_OFFSET_BITS;
91 u64 lock:1;
Damjan Marion882fcfe2018-07-17 23:01:49 +020092 u64 linear_search:1;
93 u64 log2_pages:8;
Dave Barach9466c452018-08-24 17:21:14 -040094 u64 refcnt:16;
Ed Warnickecb9cada2015-12-08 15:45:58 -070095 };
96 u64 as_u64;
97 };
Dave Barach908a5ea2017-07-14 12:42:21 -040098} BVT (clib_bihash_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Damjan Marion882fcfe2018-07-17 23:01:49 +0200100STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
Damjan Marion882fcfe2018-07-17 23:01:49 +0200101
Dave Barach9466c452018-08-24 17:21:14 -0400102/* *INDENT-OFF* */
103typedef CLIB_PACKED (struct {
104 /*
105 * Backing store allocation. Since bihash manages its own
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200106 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
Dave Barach9466c452018-08-24 17:21:14 -0400107 */
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200108 u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
Dave Barach9466c452018-08-24 17:21:14 -0400109 u64 alloc_arena_size; /* Size of the arena */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400110 u64 alloc_arena_mapped; /* Size of the mapped memory in the arena */
Dave Barach9466c452018-08-24 17:21:14 -0400111 /* Two SVM pointers stored as 8-byte integers */
112 u64 alloc_lock_as_u64;
113 u64 buckets_as_u64;
114 /* freelist list-head arrays/vectors */
115 u64 freelists_as_u64;
116 u32 nbuckets; /* Number of buckets */
117 /* Set when header valid */
118 volatile u32 ready;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400119 u64 pad[1];
Dave Barach9466c452018-08-24 17:21:14 -0400120}) BVT (clib_bihash_shared_header);
121/* *INDENT-ON* */
122
123STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
124
Dave Barach2ce28d62019-05-03 12:58:01 -0400125typedef
Damjan Marion2454de22020-09-26 19:32:34 +0200126BVS (clib_bihash_alloc_chunk)
127{
128 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
129
130 /* chunk size */
131 uword size;
132
133 /* pointer to the next allocation */
134 u8 *next_alloc;
135
136 /* number of bytes left in this chunk */
137 uword bytes_left;
138
139 /* doubly linked list of heap allocated chunks */
140 BVS (clib_bihash_alloc_chunk) * prev, *next;
141
142} BVT (clib_bihash_alloc_chunk);
143
144typedef
Dave Barach2ce28d62019-05-03 12:58:01 -0400145BVS (clib_bihash)
Dave Barachc3799992016-08-15 11:12:27 -0400146{
Dave Barach908a5ea2017-07-14 12:42:21 -0400147 BVT (clib_bihash_bucket) * buckets;
Dave Barach508498f2018-07-19 12:11:16 -0400148 volatile u32 *alloc_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Dave Barach2ce28d62019-05-03 12:58:01 -0400150 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400151 int *working_copy_lengths;
Dave Barach2ce28d62019-05-03 12:58:01 -0400152 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153
154 u32 nbuckets;
155 u32 log2_nbuckets;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400156 u64 memory_size;
Dave Barachc3799992016-08-15 11:12:27 -0400157 u8 *name;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200158 format_function_t *fmt_fn;
Damjan Marion2454de22020-09-26 19:32:34 +0200159 void *heap;
160 BVT (clib_bihash_alloc_chunk) * chunks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700161
Dave Barach9466c452018-08-24 17:21:14 -0400162 u64 *freelists;
Dave Barach97f5af02018-02-22 09:48:45 -0500163
Dave Barach9466c452018-08-24 17:21:14 -0400164#if BIHASH_32_64_SVM
Dave Barach2ce28d62019-05-03 12:58:01 -0400165 BVT (clib_bihash_shared_header) * sh;
Dave Barach9466c452018-08-24 17:21:14 -0400166 int memfd;
167#else
Dave Barach2ce28d62019-05-03 12:58:01 -0400168 BVT (clib_bihash_shared_header) sh;
Dave Barach9466c452018-08-24 17:21:14 -0400169#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Dave Barachffb14b92018-09-11 17:20:23 -0400171 u64 alloc_arena; /* Base of the allocation arena */
Dave Barach67d09e02019-08-01 08:15:01 -0400172 volatile u8 instantiated;
Nathan Skrzypczaka8c720e2021-08-06 12:03:11 +0200173 u8 dont_add_to_all_bihash_list;
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);
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100359
360int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h,
361 BVT (clib_bihash_kv) * add_v, u64 hash,
362 int is_add);
Matus Fabian828d27e2018-08-21 03:15:50 -0700363int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
364 BVT (clib_bihash_kv) * add_v,
365 int (*is_stale_cb) (BVT
366 (clib_bihash_kv)
367 *, void *),
368 void *arg);
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100369int 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);
Dave Barach908a5ea2017-07-14 12:42:21 -0400372int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400373 BVT (clib_bihash_kv) * search_v,
374 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Neale Rannseb696482020-09-29 14:44:23 +0000376int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
377
Neale Rannsf50bac12019-12-06 05:53:17 +0000378#define BIHASH_WALK_STOP 0
379#define BIHASH_WALK_CONTINUE 1
380
381typedef
382 int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
383 void *);
Dave Barachc3799992016-08-15 11:12:27 -0400384void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
Neale Rannsf50bac12019-12-06 05:53:17 +0000385 BV
386 (clib_bihash_foreach_key_value_pair_cb)
387 cb, void *arg);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400388void *clib_all_bihash_set_heap (void);
389void clib_bihash_copied (void *dst, void *src);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barachc3799992016-08-15 11:12:27 -0400391format_function_t BV (format_bihash);
392format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400393format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394
Dave Barach16e4a4a2020-04-16 12:00:14 -0400395static inline
396BVT (clib_bihash_bucket) *
397BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
398{
399#if BIHASH_KVP_AT_BUCKET_LEVEL
400 uword offset;
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));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200405#else
Dave Barach16e4a4a2020-04-16 12:00:14 -0400406 return h->buckets + (hash & (h->nbuckets - 1));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200407#endif
Dave Barach16e4a4a2020-04-16 12:00:14 -0400408}
409
Dave Barach30765e72018-02-23 07:45:36 -0500410static inline int BV (clib_bihash_search_inline_with_hash)
411 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412{
Dave Barachb9c8c572023-03-16 13:03:47 -0400413 BVT (clib_bihash_kv) rv;
Dave Barachc3799992016-08-15 11:12:27 -0400414 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400415 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500416 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417
Damjan Marion68e5fd52020-04-23 13:41:47 +0200418 /* *INDENT-OFF* */
419 static const BVT (clib_bihash_bucket) mask = {
420 .linear_search = 1,
421 .log2_pages = -1
422 };
423 /* *INDENT-ON* */
424
Dave Barach16e4a4a2020-04-16 12:00:14 -0400425#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200426 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400427 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400428#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400429
Dave Barach16e4a4a2020-04-16 12:00:14 -0400430 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431
Damjan Marion882fcfe2018-07-17 23:01:49 +0200432 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 return -1;
434
Dave Barach508498f2018-07-19 12:11:16 -0400435 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400436 {
Dave Barach508498f2018-07-19 12:11:16 -0400437 volatile BVT (clib_bihash_bucket) * bv = b;
438 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200439 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400440 }
441
Dave Barachc3799992016-08-15 11:12:27 -0400442 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400443
Dave Barach5e6b9582016-12-12 15:37:29 -0500444 /* If the bucket has unresolvable collisions, use linear search */
445 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200446
447 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
448 {
449 if (PREDICT_FALSE (b->linear_search))
450 limit <<= b->log2_pages;
451 else
452 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
453 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500454
455 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400457 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400458 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400459 rv = v->kvp[i];
460 if (BV (clib_bihash_is_free) (&rv))
461 return -1;
462 *key_result = rv;
Dave Barachc3799992016-08-15 11:12:27 -0400463 return 0;
464 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 }
466 return -1;
467}
468
Dave Barach30765e72018-02-23 07:45:36 -0500469static inline int BV (clib_bihash_search_inline)
470 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
471{
472 u64 hash;
473
474 hash = BV (clib_bihash_hash) (key_result);
475
476 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
477}
478
479static inline void BV (clib_bihash_prefetch_bucket)
480 (BVT (clib_bihash) * h, u64 hash)
481{
Dave Barach16e4a4a2020-04-16 12:00:14 -0400482 CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
483 BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
484 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500485}
486
487static inline void BV (clib_bihash_prefetch_data)
488 (BVT (clib_bihash) * h, u64 hash)
489{
Dave Barach30765e72018-02-23 07:45:36 -0500490 BVT (clib_bihash_value) * v;
491 BVT (clib_bihash_bucket) * b;
492
Dave Barach16e4a4a2020-04-16 12:00:14 -0400493#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200494 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400495 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400496#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400497
Damjan Marion4e149772020-03-27 16:57:28 +0100498 b = BV (clib_bihash_get_bucket) (h, hash);
Dave Barach30765e72018-02-23 07:45:36 -0500499
Damjan Marion882fcfe2018-07-17 23:01:49 +0200500 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Dave Barach30765e72018-02-23 07:45:36 -0500501 return;
502
Dave Barach30765e72018-02-23 07:45:36 -0500503 v = BV (clib_bihash_get_value) (h, b->offset);
504
Damjan Marion68e5fd52020-04-23 13:41:47 +0200505 if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
506 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
Dave Barach30765e72018-02-23 07:45:36 -0500507
Damjan Marion68e5fd52020-04-23 13:41:47 +0200508 CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
509 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500510}
511
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200512static inline int BV (clib_bihash_search_inline_2_with_hash)
Dave Barach908a5ea2017-07-14 12:42:21 -0400513 (BVT (clib_bihash) * h,
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200514 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515{
Dave Barachb9c8c572023-03-16 13:03:47 -0400516 BVT (clib_bihash_kv) rv;
Dave Barachc3799992016-08-15 11:12:27 -0400517 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400518 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500519 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
Damjan Marion68e5fd52020-04-23 13:41:47 +0200521/* *INDENT-OFF* */
522 static const BVT (clib_bihash_bucket) mask = {
523 .linear_search = 1,
524 .log2_pages = -1
525 };
526/* *INDENT-ON* */
527
Dave Barachc3799992016-08-15 11:12:27 -0400528 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529
Dave Barach16e4a4a2020-04-16 12:00:14 -0400530#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200531 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400532 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400533#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400534
Damjan Marion4e149772020-03-27 16:57:28 +0100535 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536
Damjan Marion882fcfe2018-07-17 23:01:49 +0200537 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 return -1;
539
Dave Barach508498f2018-07-19 12:11:16 -0400540 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400541 {
Dave Barach508498f2018-07-19 12:11:16 -0400542 volatile BVT (clib_bihash_bucket) * bv = b;
543 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200544 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400545 }
546
Dave Barachc3799992016-08-15 11:12:27 -0400547 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400548
Dave Barach5e6b9582016-12-12 15:37:29 -0500549 /* If the bucket has unresolvable collisions, use linear search */
550 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200551
552 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
553 {
554 if (PREDICT_FALSE (b->linear_search))
555 limit <<= b->log2_pages;
556 else
557 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
558 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500559
560 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 {
Dave Barachc3799992016-08-15 11:12:27 -0400562 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
563 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400564 rv = v->kvp[i];
565 if (BV (clib_bihash_is_free) (&rv))
566 return -1;
567 *valuep = rv;
Dave Barachc3799992016-08-15 11:12:27 -0400568 return 0;
569 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570 }
571 return -1;
572}
573
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200574static inline int BV (clib_bihash_search_inline_2)
575 (BVT (clib_bihash) * h,
576 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
577{
578 u64 hash;
579
580 hash = BV (clib_bihash_hash) (search_key);
581
582 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
583 valuep);
584}
585
586
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400588
Chris Luke16bcf7d2016-09-01 14:31:46 -0400589/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400590
591/*
592 * fd.io coding-style-patch-verification: ON
593 *
594 * Local Variables:
595 * eval: (c-set-style "gnu")
596 * End:
597 */