blob: 8f5879b46348a64f9d98a8a06ba162d780d990ad [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 -0400102typedef CLIB_PACKED (struct {
103 /*
104 * Backing store allocation. Since bihash manages its own
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200105 * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next].
Dave Barach9466c452018-08-24 17:21:14 -0400106 */
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +0200107 u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */
Dave Barach9466c452018-08-24 17:21:14 -0400108 u64 alloc_arena_size; /* Size of the arena */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400109 u64 alloc_arena_mapped; /* Size of the mapped memory in the arena */
Dave Barach9466c452018-08-24 17:21:14 -0400110 /* Two SVM pointers stored as 8-byte integers */
111 u64 alloc_lock_as_u64;
112 u64 buckets_as_u64;
113 /* freelist list-head arrays/vectors */
114 u64 freelists_as_u64;
115 u32 nbuckets; /* Number of buckets */
116 /* Set when header valid */
117 volatile u32 ready;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400118 u64 pad[1];
Dave Barach9466c452018-08-24 17:21:14 -0400119}) BVT (clib_bihash_shared_header);
Dave Barach9466c452018-08-24 17:21:14 -0400120
121STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64));
122
Dave Barach2ce28d62019-05-03 12:58:01 -0400123typedef
Damjan Marion2454de22020-09-26 19:32:34 +0200124BVS (clib_bihash_alloc_chunk)
125{
126 CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
127
128 /* chunk size */
129 uword size;
130
131 /* pointer to the next allocation */
132 u8 *next_alloc;
133
134 /* number of bytes left in this chunk */
135 uword bytes_left;
136
137 /* doubly linked list of heap allocated chunks */
138 BVS (clib_bihash_alloc_chunk) * prev, *next;
139
140} BVT (clib_bihash_alloc_chunk);
141
142typedef
Dave Barach2ce28d62019-05-03 12:58:01 -0400143BVS (clib_bihash)
Dave Barachc3799992016-08-15 11:12:27 -0400144{
Dave Barach908a5ea2017-07-14 12:42:21 -0400145 BVT (clib_bihash_bucket) * buckets;
Dave Barach508498f2018-07-19 12:11:16 -0400146 volatile u32 *alloc_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147
Dave Barach2ce28d62019-05-03 12:58:01 -0400148 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400149 int *working_copy_lengths;
Dave Barach2ce28d62019-05-03 12:58:01 -0400150 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151
152 u32 nbuckets;
153 u32 log2_nbuckets;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400154 u64 memory_size;
Dave Barachc3799992016-08-15 11:12:27 -0400155 u8 *name;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200156 format_function_t *fmt_fn;
Damjan Marion2454de22020-09-26 19:32:34 +0200157 void *heap;
158 BVT (clib_bihash_alloc_chunk) * chunks;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159
Dave Barach9466c452018-08-24 17:21:14 -0400160 u64 *freelists;
Dave Barach97f5af02018-02-22 09:48:45 -0500161
Dave Barach9466c452018-08-24 17:21:14 -0400162#if BIHASH_32_64_SVM
Dave Barach2ce28d62019-05-03 12:58:01 -0400163 BVT (clib_bihash_shared_header) * sh;
Dave Barach9466c452018-08-24 17:21:14 -0400164 int memfd;
165#else
Dave Barach2ce28d62019-05-03 12:58:01 -0400166 BVT (clib_bihash_shared_header) sh;
Dave Barach9466c452018-08-24 17:21:14 -0400167#endif
Ed Warnickecb9cada2015-12-08 15:45:58 -0700168
Dave Barachffb14b92018-09-11 17:20:23 -0400169 u64 alloc_arena; /* Base of the allocation arena */
Dave Barach67d09e02019-08-01 08:15:01 -0400170 volatile u8 instantiated;
Nathan Skrzypczaka8c720e2021-08-06 12:03:11 +0200171 u8 dont_add_to_all_bihash_list;
Dave Barachffb14b92018-09-11 17:20:23 -0400172
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800173 /**
174 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
175 */
Damjan Marionf2b4a372020-09-30 14:15:24 +0200176 format_function_t *kvp_fmt_fn;
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800177
Dave Barach2ce28d62019-05-03 12:58:01 -0400178 /** Optional statistics-gathering callback */
179#if BIHASH_ENABLE_STATS
180 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
181
182 /** Statistics callback context (e.g. address of stats data structure) */
183 void *inc_stats_context;
184#endif
185
Dave Barachc3799992016-08-15 11:12:27 -0400186} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700187
Dave Barachbdf9b972019-09-03 10:57:19 -0400188typedef struct
189{
190 BVT (clib_bihash) * h;
191 char *name;
192 u32 nbuckets;
193 uword memory_size;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200194 format_function_t *kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400195 u8 instantiate_immediately;
196 u8 dont_add_to_all_bihash_list;
197} BVT (clib_bihash_init2_args);
198
Dave Barach32dcd3b2019-07-08 12:25:38 -0400199extern void **clib_all_bihashes;
200
Dave Barach9466c452018-08-24 17:21:14 -0400201#if BIHASH_32_64_SVM
202#undef alloc_arena_next
203#undef alloc_arena_size
Dave Barach16e4a4a2020-04-16 12:00:14 -0400204#undef alloc_arena_mapped
Dave Barach9466c452018-08-24 17:21:14 -0400205#undef alloc_arena
206#undef CLIB_BIHASH_READY_MAGIC
207#define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
208#define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400209#define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
Dave Barachffb14b92018-09-11 17:20:23 -0400210#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400211#define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
212#else
213#undef alloc_arena_next
214#undef alloc_arena_size
Dave Barach16e4a4a2020-04-16 12:00:14 -0400215#undef alloc_arena_mapped
Dave Barach9466c452018-08-24 17:21:14 -0400216#undef alloc_arena
217#undef CLIB_BIHASH_READY_MAGIC
218#define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
219#define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400220#define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
Dave Barachffb14b92018-09-11 17:20:23 -0400221#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400222#define CLIB_BIHASH_READY_MAGIC 0
223#endif
224
Dave Barach2ce28d62019-05-03 12:58:01 -0400225#ifndef BIHASH_STAT_IDS
226#define BIHASH_STAT_IDS 1
227
228#define foreach_bihash_stat \
229_(alloc_add) \
230_(add) \
231_(split_add) \
232_(replace) \
233_(update) \
234_(del) \
235_(del_free) \
236_(linear) \
237_(resplit) \
238_(working_copy_lost) \
239_(splits) /* must be last */
240
241typedef enum
242{
243#define _(a) BIHASH_STAT_##a,
244 foreach_bihash_stat
245#undef _
246 BIHASH_STAT_N_STATS,
247} BVT (clib_bihash_stat_id);
248#endif /* BIHASH_STAT_IDS */
249
250static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
251 int stat_id, u64 count)
252{
253#if BIHASH_ENABLE_STATS
254 if (PREDICT_FALSE (h->inc_stats_callback != 0))
255 h->inc_stats_callback (h, stat_id, count);
256#endif
257}
258
259#if BIHASH_ENABLE_STATS
260static inline void BV (clib_bihash_set_stats_callback)
261 (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
262 void *ctx)
263{
264 h->inc_stats_callback = cb;
265 h->inc_stats_context = ctx;
266}
267#endif
268
269
Dave Barach508498f2018-07-19 12:11:16 -0400270static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400271{
Dave Barach508498f2018-07-19 12:11:16 -0400272 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
Damjan Marion2a03efe2018-07-20 21:48:59 +0200273 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400274}
275
Dave Barach508498f2018-07-19 12:11:16 -0400276static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400277{
Dave Barach508498f2018-07-19 12:11:16 -0400278 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
Dave Barach908a5ea2017-07-14 12:42:21 -0400279}
280
Dave Barach508498f2018-07-19 12:11:16 -0400281static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400282{
Damjan Marion74ee18b2020-04-21 20:14:34 +0200283 BVT (clib_bihash_bucket) mask = { .lock = 1 };
Damjan Marion74ee18b2020-04-21 20:14:34 +0200284 u64 old;
Dave Barach908a5ea2017-07-14 12:42:21 -0400285
Damjan Marion74ee18b2020-04-21 20:14:34 +0200286try_again:
287 old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200288
Damjan Marion74ee18b2020-04-21 20:14:34 +0200289 if (PREDICT_FALSE (old & mask.as_u64))
Damjan Marion801ec2a2020-04-21 19:42:30 +0200290 {
Damjan Marion74ee18b2020-04-21 20:14:34 +0200291 /* somebody else flipped the bit, try again */
Damjan Marion801ec2a2020-04-21 19:42:30 +0200292 CLIB_PAUSE ();
Damjan Marion74ee18b2020-04-21 20:14:34 +0200293 goto try_again;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200294 }
Dave Barach858c06f2017-07-21 10:44:27 -0400295}
296
297static inline void BV (clib_bihash_unlock_bucket)
298 (BVT (clib_bihash_bucket) * b)
299{
Dave Barach508498f2018-07-19 12:11:16 -0400300 b->lock = 0;
Dave Barach908a5ea2017-07-14 12:42:21 -0400301}
302
303static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400304 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700305{
Dave Barach9466c452018-08-24 17:21:14 -0400306 u8 *hp = (u8 *) (uword) alloc_arena (h);
Dave Barachc3799992016-08-15 11:12:27 -0400307 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308
309 return (void *) vp;
310}
311
Damjan Marion882fcfe2018-07-17 23:01:49 +0200312static inline int BV (clib_bihash_bucket_is_empty)
313 (BVT (clib_bihash_bucket) * b)
314{
Dave Barach508498f2018-07-19 12:11:16 -0400315 /* Note: applied to locked buckets, test offset */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400316 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
317 return b->offset == 0;
318 else
319 return (b->log2_pages == 0 && b->refcnt == 1);
Damjan Marion882fcfe2018-07-17 23:01:49 +0200320}
321
Dave Barach908a5ea2017-07-14 12:42:21 -0400322static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400323 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324{
Dave Barachc3799992016-08-15 11:12:27 -0400325 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326
Dave Barach9466c452018-08-24 17:21:14 -0400327 hp = (u8 *) (uword) alloc_arena (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 vp = (u8 *) v;
329
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330 return vp - hp;
331}
332
Paul Atkins2ac41352021-01-19 15:22:23 +0000333#define BIHASH_ADD 1
334#define BIHASH_DEL 0
335
Dave Barachc3799992016-08-15 11:12:27 -0400336void BV (clib_bihash_init)
337 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700338
Dave Barachbdf9b972019-09-03 10:57:19 -0400339void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
340
Dave Barach9466c452018-08-24 17:21:14 -0400341#if BIHASH_32_64_SVM
Dave Barachd4a639b2020-08-06 11:38:40 -0400342void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400343 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
Dave Barachd4a639b2020-08-06 11:38:40 -0400344void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400345 (BVT (clib_bihash) * h, char *name, int fd);
346#endif
347
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800348void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200349 format_function_t * kvp_fmt_fn);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800350
Dave Barachc3799992016-08-15 11:12:27 -0400351void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Dave Barachc3799992016-08-15 11:12:27 -0400353int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
354 BVT (clib_bihash_kv) * add_v, int is_add);
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100355
356int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h,
357 BVT (clib_bihash_kv) * add_v, u64 hash,
358 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);
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100365int BV (clib_bihash_add_with_overwrite_cb) (
366 BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
367 void (*overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *arg);
Dave Barach908a5ea2017-07-14 12:42:21 -0400368int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400369 BVT (clib_bihash_kv) * search_v,
370 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700371
Neale Rannseb696482020-09-29 14:44:23 +0000372int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
373
Neale Rannsf50bac12019-12-06 05:53:17 +0000374#define BIHASH_WALK_STOP 0
375#define BIHASH_WALK_CONTINUE 1
376
377typedef
378 int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
379 void *);
Dave Barachc3799992016-08-15 11:12:27 -0400380void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
Neale Rannsf50bac12019-12-06 05:53:17 +0000381 BV
382 (clib_bihash_foreach_key_value_pair_cb)
383 cb, void *arg);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400384void *clib_all_bihash_set_heap (void);
385void clib_bihash_copied (void *dst, void *src);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Dave Barachc3799992016-08-15 11:12:27 -0400387format_function_t BV (format_bihash);
388format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400389format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390
Dave Barach16e4a4a2020-04-16 12:00:14 -0400391static inline
392BVT (clib_bihash_bucket) *
393BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
394{
395#if BIHASH_KVP_AT_BUCKET_LEVEL
396 uword offset;
397 offset = (hash & (h->nbuckets - 1));
398 offset = offset * (sizeof (BVT (clib_bihash_bucket))
399 + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
400 return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200401#else
Dave Barach16e4a4a2020-04-16 12:00:14 -0400402 return h->buckets + (hash & (h->nbuckets - 1));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200403#endif
Dave Barach16e4a4a2020-04-16 12:00:14 -0400404}
405
Dave Barach30765e72018-02-23 07:45:36 -0500406static inline int BV (clib_bihash_search_inline_with_hash)
407 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408{
Dave Barachb9c8c572023-03-16 13:03:47 -0400409 BVT (clib_bihash_kv) rv;
Dave Barachc3799992016-08-15 11:12:27 -0400410 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400411 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500412 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413
Damjan Marion68e5fd52020-04-23 13:41:47 +0200414 static const BVT (clib_bihash_bucket) mask = {
415 .linear_search = 1,
416 .log2_pages = -1
417 };
Damjan Marion68e5fd52020-04-23 13:41:47 +0200418
Dave Barach16e4a4a2020-04-16 12:00:14 -0400419#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200420 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400421 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400422#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400423
Dave Barach16e4a4a2020-04-16 12:00:14 -0400424 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
Damjan Marion882fcfe2018-07-17 23:01:49 +0200426 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 return -1;
428
Dave Barach508498f2018-07-19 12:11:16 -0400429 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400430 {
Dave Barach508498f2018-07-19 12:11:16 -0400431 volatile BVT (clib_bihash_bucket) * bv = b;
432 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200433 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400434 }
435
Dave Barachc3799992016-08-15 11:12:27 -0400436 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400437
Dave Barach5e6b9582016-12-12 15:37:29 -0500438 /* If the bucket has unresolvable collisions, use linear search */
439 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200440
441 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
442 {
443 if (PREDICT_FALSE (b->linear_search))
444 limit <<= b->log2_pages;
445 else
446 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
447 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500448
449 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400451 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400452 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400453 rv = v->kvp[i];
454 if (BV (clib_bihash_is_free) (&rv))
455 return -1;
456 *key_result = rv;
Dave Barachc3799992016-08-15 11:12:27 -0400457 return 0;
458 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700459 }
460 return -1;
461}
462
Dave Barach30765e72018-02-23 07:45:36 -0500463static inline int BV (clib_bihash_search_inline)
464 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
465{
466 u64 hash;
467
468 hash = BV (clib_bihash_hash) (key_result);
469
470 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
471}
472
473static inline void BV (clib_bihash_prefetch_bucket)
474 (BVT (clib_bihash) * h, u64 hash)
475{
Dave Barach16e4a4a2020-04-16 12:00:14 -0400476 CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
477 BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
478 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500479}
480
481static inline void BV (clib_bihash_prefetch_data)
482 (BVT (clib_bihash) * h, u64 hash)
483{
Dave Barach30765e72018-02-23 07:45:36 -0500484 BVT (clib_bihash_value) * v;
485 BVT (clib_bihash_bucket) * b;
486
Dave Barach16e4a4a2020-04-16 12:00:14 -0400487#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200488 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400489 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400490#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400491
Damjan Marion4e149772020-03-27 16:57:28 +0100492 b = BV (clib_bihash_get_bucket) (h, hash);
Dave Barach30765e72018-02-23 07:45:36 -0500493
Damjan Marion882fcfe2018-07-17 23:01:49 +0200494 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Dave Barach30765e72018-02-23 07:45:36 -0500495 return;
496
Dave Barach30765e72018-02-23 07:45:36 -0500497 v = BV (clib_bihash_get_value) (h, b->offset);
498
Damjan Marion68e5fd52020-04-23 13:41:47 +0200499 if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
500 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
Dave Barach30765e72018-02-23 07:45:36 -0500501
Damjan Marion68e5fd52020-04-23 13:41:47 +0200502 CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
503 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500504}
505
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200506static inline int BV (clib_bihash_search_inline_2_with_hash)
Dave Barach908a5ea2017-07-14 12:42:21 -0400507 (BVT (clib_bihash) * h,
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200508 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509{
Dave Barachb9c8c572023-03-16 13:03:47 -0400510 BVT (clib_bihash_kv) rv;
Dave Barachc3799992016-08-15 11:12:27 -0400511 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400512 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500513 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514
Damjan Marion68e5fd52020-04-23 13:41:47 +0200515 static const BVT (clib_bihash_bucket) mask = {
516 .linear_search = 1,
517 .log2_pages = -1
518 };
Damjan Marion68e5fd52020-04-23 13:41:47 +0200519
Dave Barachc3799992016-08-15 11:12:27 -0400520 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700521
Dave Barach16e4a4a2020-04-16 12:00:14 -0400522#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200523 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400524 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400525#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400526
Damjan Marion4e149772020-03-27 16:57:28 +0100527 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Damjan Marion882fcfe2018-07-17 23:01:49 +0200529 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 return -1;
531
Dave Barach508498f2018-07-19 12:11:16 -0400532 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400533 {
Dave Barach508498f2018-07-19 12:11:16 -0400534 volatile BVT (clib_bihash_bucket) * bv = b;
535 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200536 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400537 }
538
Dave Barachc3799992016-08-15 11:12:27 -0400539 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400540
Dave Barach5e6b9582016-12-12 15:37:29 -0500541 /* If the bucket has unresolvable collisions, use linear search */
542 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200543
544 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
545 {
546 if (PREDICT_FALSE (b->linear_search))
547 limit <<= b->log2_pages;
548 else
549 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
550 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500551
552 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 {
Dave Barachc3799992016-08-15 11:12:27 -0400554 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
555 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400556 rv = v->kvp[i];
557 if (BV (clib_bihash_is_free) (&rv))
558 return -1;
559 *valuep = rv;
Dave Barachc3799992016-08-15 11:12:27 -0400560 return 0;
561 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 }
563 return -1;
564}
565
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200566static inline int BV (clib_bihash_search_inline_2)
567 (BVT (clib_bihash) * h,
568 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
569{
570 u64 hash;
571
572 hash = BV (clib_bihash_hash) (search_key);
573
574 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
575 valuep);
576}
577
578
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400580
Chris Luke16bcf7d2016-09-01 14:31:46 -0400581/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400582
583/*
584 * fd.io coding-style-patch-verification: ON
585 *
586 * Local Variables:
587 * eval: (c-set-style "gnu")
588 * End:
589 */