blob: da2f684b6854e3a81bd2b5a2c4b02f7741861e3d [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;
Dave Barachffb14b92018-09-11 17:20:23 -0400173
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800174 /**
175 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
176 */
Damjan Marionf2b4a372020-09-30 14:15:24 +0200177 format_function_t *kvp_fmt_fn;
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800178
Dave Barach2ce28d62019-05-03 12:58:01 -0400179 /** Optional statistics-gathering callback */
180#if BIHASH_ENABLE_STATS
181 void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count);
182
183 /** Statistics callback context (e.g. address of stats data structure) */
184 void *inc_stats_context;
185#endif
186
Dave Barachc3799992016-08-15 11:12:27 -0400187} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188
Dave Barachbdf9b972019-09-03 10:57:19 -0400189typedef struct
190{
191 BVT (clib_bihash) * h;
192 char *name;
193 u32 nbuckets;
194 uword memory_size;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200195 format_function_t *kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400196 u8 instantiate_immediately;
197 u8 dont_add_to_all_bihash_list;
198} BVT (clib_bihash_init2_args);
199
Dave Barach32dcd3b2019-07-08 12:25:38 -0400200extern void **clib_all_bihashes;
201
Dave Barach9466c452018-08-24 17:21:14 -0400202#if BIHASH_32_64_SVM
203#undef alloc_arena_next
204#undef alloc_arena_size
Dave Barach16e4a4a2020-04-16 12:00:14 -0400205#undef alloc_arena_mapped
Dave Barach9466c452018-08-24 17:21:14 -0400206#undef alloc_arena
207#undef CLIB_BIHASH_READY_MAGIC
208#define alloc_arena_next(h) (((h)->sh)->alloc_arena_next)
209#define alloc_arena_size(h) (((h)->sh)->alloc_arena_size)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400210#define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped)
Dave Barachffb14b92018-09-11 17:20:23 -0400211#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400212#define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE
213#else
214#undef alloc_arena_next
215#undef alloc_arena_size
Dave Barach16e4a4a2020-04-16 12:00:14 -0400216#undef alloc_arena_mapped
Dave Barach9466c452018-08-24 17:21:14 -0400217#undef alloc_arena
218#undef CLIB_BIHASH_READY_MAGIC
219#define alloc_arena_next(h) ((h)->sh.alloc_arena_next)
220#define alloc_arena_size(h) ((h)->sh.alloc_arena_size)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400221#define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped)
Dave Barachffb14b92018-09-11 17:20:23 -0400222#define alloc_arena(h) ((h)->alloc_arena)
Dave Barach9466c452018-08-24 17:21:14 -0400223#define CLIB_BIHASH_READY_MAGIC 0
224#endif
225
Dave Barach2ce28d62019-05-03 12:58:01 -0400226#ifndef BIHASH_STAT_IDS
227#define BIHASH_STAT_IDS 1
228
229#define foreach_bihash_stat \
230_(alloc_add) \
231_(add) \
232_(split_add) \
233_(replace) \
234_(update) \
235_(del) \
236_(del_free) \
237_(linear) \
238_(resplit) \
239_(working_copy_lost) \
240_(splits) /* must be last */
241
242typedef enum
243{
244#define _(a) BIHASH_STAT_##a,
245 foreach_bihash_stat
246#undef _
247 BIHASH_STAT_N_STATS,
248} BVT (clib_bihash_stat_id);
249#endif /* BIHASH_STAT_IDS */
250
251static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h,
252 int stat_id, u64 count)
253{
254#if BIHASH_ENABLE_STATS
255 if (PREDICT_FALSE (h->inc_stats_callback != 0))
256 h->inc_stats_callback (h, stat_id, count);
257#endif
258}
259
260#if BIHASH_ENABLE_STATS
261static inline void BV (clib_bihash_set_stats_callback)
262 (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64),
263 void *ctx)
264{
265 h->inc_stats_callback = cb;
266 h->inc_stats_context = ctx;
267}
268#endif
269
270
Dave Barach508498f2018-07-19 12:11:16 -0400271static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400272{
Dave Barach508498f2018-07-19 12:11:16 -0400273 while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE))
Damjan Marion2a03efe2018-07-20 21:48:59 +0200274 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400275}
276
Dave Barach508498f2018-07-19 12:11:16 -0400277static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h)
Dave Barach908a5ea2017-07-14 12:42:21 -0400278{
Dave Barach508498f2018-07-19 12:11:16 -0400279 __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE);
Dave Barach908a5ea2017-07-14 12:42:21 -0400280}
281
Dave Barach508498f2018-07-19 12:11:16 -0400282static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400283{
Damjan Marion74ee18b2020-04-21 20:14:34 +0200284 /* *INDENT-OFF* */
285 BVT (clib_bihash_bucket) mask = { .lock = 1 };
286 /* *INDENT-ON* */
287 u64 old;
Dave Barach908a5ea2017-07-14 12:42:21 -0400288
Damjan Marion74ee18b2020-04-21 20:14:34 +0200289try_again:
290 old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200291
Damjan Marion74ee18b2020-04-21 20:14:34 +0200292 if (PREDICT_FALSE (old & mask.as_u64))
Damjan Marion801ec2a2020-04-21 19:42:30 +0200293 {
Damjan Marion74ee18b2020-04-21 20:14:34 +0200294 /* somebody else flipped the bit, try again */
Damjan Marion801ec2a2020-04-21 19:42:30 +0200295 CLIB_PAUSE ();
Damjan Marion74ee18b2020-04-21 20:14:34 +0200296 goto try_again;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200297 }
Dave Barach858c06f2017-07-21 10:44:27 -0400298}
299
300static inline void BV (clib_bihash_unlock_bucket)
301 (BVT (clib_bihash_bucket) * b)
302{
Dave Barach508498f2018-07-19 12:11:16 -0400303 b->lock = 0;
Dave Barach908a5ea2017-07-14 12:42:21 -0400304}
305
306static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400307 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308{
Dave Barach9466c452018-08-24 17:21:14 -0400309 u8 *hp = (u8 *) (uword) alloc_arena (h);
Dave Barachc3799992016-08-15 11:12:27 -0400310 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311
312 return (void *) vp;
313}
314
Damjan Marion882fcfe2018-07-17 23:01:49 +0200315static inline int BV (clib_bihash_bucket_is_empty)
316 (BVT (clib_bihash_bucket) * b)
317{
Dave Barach508498f2018-07-19 12:11:16 -0400318 /* Note: applied to locked buckets, test offset */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400319 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0)
320 return b->offset == 0;
321 else
322 return (b->log2_pages == 0 && b->refcnt == 1);
Damjan Marion882fcfe2018-07-17 23:01:49 +0200323}
324
Dave Barach908a5ea2017-07-14 12:42:21 -0400325static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400326 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327{
Dave Barachc3799992016-08-15 11:12:27 -0400328 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329
Dave Barach9466c452018-08-24 17:21:14 -0400330 hp = (u8 *) (uword) alloc_arena (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331 vp = (u8 *) v;
332
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 return vp - hp;
334}
335
Paul Atkins2ac41352021-01-19 15:22:23 +0000336#define BIHASH_ADD 1
337#define BIHASH_DEL 0
338
Dave Barachc3799992016-08-15 11:12:27 -0400339void BV (clib_bihash_init)
340 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341
Dave Barachbdf9b972019-09-03 10:57:19 -0400342void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a);
343
Dave Barach9466c452018-08-24 17:21:14 -0400344#if BIHASH_32_64_SVM
Dave Barachd4a639b2020-08-06 11:38:40 -0400345void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400346 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size);
Dave Barachd4a639b2020-08-06 11:38:40 -0400347void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400348 (BVT (clib_bihash) * h, char *name, int fd);
349#endif
350
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800351void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200352 format_function_t * kvp_fmt_fn);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800353
Dave Barachc3799992016-08-15 11:12:27 -0400354void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355
Dave Barachc3799992016-08-15 11:12:27 -0400356int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
357 BVT (clib_bihash_kv) * add_v, int is_add);
Matus Fabian828d27e2018-08-21 03:15:50 -0700358int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h,
359 BVT (clib_bihash_kv) * add_v,
360 int (*is_stale_cb) (BVT
361 (clib_bihash_kv)
362 *, void *),
363 void *arg);
Dave Barach908a5ea2017-07-14 12:42:21 -0400364int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400365 BVT (clib_bihash_kv) * search_v,
366 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367
Neale Rannseb696482020-09-29 14:44:23 +0000368int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h);
369
Neale Rannsf50bac12019-12-06 05:53:17 +0000370#define BIHASH_WALK_STOP 0
371#define BIHASH_WALK_CONTINUE 1
372
373typedef
374 int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *,
375 void *);
Dave Barachc3799992016-08-15 11:12:27 -0400376void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
Neale Rannsf50bac12019-12-06 05:53:17 +0000377 BV
378 (clib_bihash_foreach_key_value_pair_cb)
379 cb, void *arg);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400380void *clib_all_bihash_set_heap (void);
381void clib_bihash_copied (void *dst, void *src);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700382
Dave Barachc3799992016-08-15 11:12:27 -0400383format_function_t BV (format_bihash);
384format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400385format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386
Dave Barach16e4a4a2020-04-16 12:00:14 -0400387static inline
388BVT (clib_bihash_bucket) *
389BV (clib_bihash_get_bucket) (BVT (clib_bihash) * h, u64 hash)
390{
391#if BIHASH_KVP_AT_BUCKET_LEVEL
392 uword offset;
393 offset = (hash & (h->nbuckets - 1));
394 offset = offset * (sizeof (BVT (clib_bihash_bucket))
395 + (BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv))));
396 return ((BVT (clib_bihash_bucket) *) (((u8 *) h->buckets) + offset));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200397#else
Dave Barach16e4a4a2020-04-16 12:00:14 -0400398 return h->buckets + (hash & (h->nbuckets - 1));
Nathan Skrzypczak7be47462020-09-01 09:35:31 +0200399#endif
Dave Barach16e4a4a2020-04-16 12:00:14 -0400400}
401
Dave Barach30765e72018-02-23 07:45:36 -0500402static inline int BV (clib_bihash_search_inline_with_hash)
403 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404{
Dave Barachc3799992016-08-15 11:12:27 -0400405 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400406 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500407 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Damjan Marion68e5fd52020-04-23 13:41:47 +0200409 /* *INDENT-OFF* */
410 static const BVT (clib_bihash_bucket) mask = {
411 .linear_search = 1,
412 .log2_pages = -1
413 };
414 /* *INDENT-ON* */
415
Dave Barach16e4a4a2020-04-16 12:00:14 -0400416#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200417 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400418 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400419#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400420
Dave Barach16e4a4a2020-04-16 12:00:14 -0400421 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700422
Damjan Marion882fcfe2018-07-17 23:01:49 +0200423 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 return -1;
425
Dave Barach508498f2018-07-19 12:11:16 -0400426 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400427 {
Dave Barach508498f2018-07-19 12:11:16 -0400428 volatile BVT (clib_bihash_bucket) * bv = b;
429 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200430 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400431 }
432
Dave Barachc3799992016-08-15 11:12:27 -0400433 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400434
Dave Barach5e6b9582016-12-12 15:37:29 -0500435 /* If the bucket has unresolvable collisions, use linear search */
436 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200437
438 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
439 {
440 if (PREDICT_FALSE (b->linear_search))
441 limit <<= b->log2_pages;
442 else
443 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
444 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500445
446 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400448 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400449 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400450 *key_result = v->kvp[i];
Dave Barachc3799992016-08-15 11:12:27 -0400451 return 0;
452 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 }
454 return -1;
455}
456
Dave Barach30765e72018-02-23 07:45:36 -0500457static inline int BV (clib_bihash_search_inline)
458 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
459{
460 u64 hash;
461
462 hash = BV (clib_bihash_hash) (key_result);
463
464 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
465}
466
467static inline void BV (clib_bihash_prefetch_bucket)
468 (BVT (clib_bihash) * h, u64 hash)
469{
Dave Barach16e4a4a2020-04-16 12:00:14 -0400470 CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash),
471 BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES,
472 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500473}
474
475static inline void BV (clib_bihash_prefetch_data)
476 (BVT (clib_bihash) * h, u64 hash)
477{
Dave Barach30765e72018-02-23 07:45:36 -0500478 BVT (clib_bihash_value) * v;
479 BVT (clib_bihash_bucket) * b;
480
Dave Barach16e4a4a2020-04-16 12:00:14 -0400481#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200482 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400483 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400484#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400485
Damjan Marion4e149772020-03-27 16:57:28 +0100486 b = BV (clib_bihash_get_bucket) (h, hash);
Dave Barach30765e72018-02-23 07:45:36 -0500487
Damjan Marion882fcfe2018-07-17 23:01:49 +0200488 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Dave Barach30765e72018-02-23 07:45:36 -0500489 return;
490
Dave Barach30765e72018-02-23 07:45:36 -0500491 v = BV (clib_bihash_get_value) (h, b->offset);
492
Damjan Marion68e5fd52020-04-23 13:41:47 +0200493 if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0))
494 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
Dave Barach30765e72018-02-23 07:45:36 -0500495
Damjan Marion68e5fd52020-04-23 13:41:47 +0200496 CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)),
497 LOAD);
Dave Barach30765e72018-02-23 07:45:36 -0500498}
499
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200500static inline int BV (clib_bihash_search_inline_2_with_hash)
Dave Barach908a5ea2017-07-14 12:42:21 -0400501 (BVT (clib_bihash) * h,
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200502 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503{
Dave Barachc3799992016-08-15 11:12:27 -0400504 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400505 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500506 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507
Damjan Marion68e5fd52020-04-23 13:41:47 +0200508/* *INDENT-OFF* */
509 static const BVT (clib_bihash_bucket) mask = {
510 .linear_search = 1,
511 .log2_pages = -1
512 };
513/* *INDENT-ON* */
514
Dave Barachc3799992016-08-15 11:12:27 -0400515 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516
Dave Barach16e4a4a2020-04-16 12:00:14 -0400517#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +0200518 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400519 return -1;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400520#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400521
Damjan Marion4e149772020-03-27 16:57:28 +0100522 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523
Damjan Marion882fcfe2018-07-17 23:01:49 +0200524 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 return -1;
526
Dave Barach508498f2018-07-19 12:11:16 -0400527 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400528 {
Dave Barach508498f2018-07-19 12:11:16 -0400529 volatile BVT (clib_bihash_bucket) * bv = b;
530 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200531 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400532 }
533
Dave Barachc3799992016-08-15 11:12:27 -0400534 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400535
Dave Barach5e6b9582016-12-12 15:37:29 -0500536 /* If the bucket has unresolvable collisions, use linear search */
537 limit = BIHASH_KVP_PER_PAGE;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200538
539 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
540 {
541 if (PREDICT_FALSE (b->linear_search))
542 limit <<= b->log2_pages;
543 else
544 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
545 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500546
547 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 {
Dave Barachc3799992016-08-15 11:12:27 -0400549 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
550 {
551 *valuep = v->kvp[i];
552 return 0;
553 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 }
555 return -1;
556}
557
Andrew Yourtchenkoabcddcb2018-05-27 20:56:26 +0200558static inline int BV (clib_bihash_search_inline_2)
559 (BVT (clib_bihash) * h,
560 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
561{
562 u64 hash;
563
564 hash = BV (clib_bihash_hash) (search_key);
565
566 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
567 valuep);
568}
569
570
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400572
Chris Luke16bcf7d2016-09-01 14:31:46 -0400573/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400574
575/*
576 * fd.io coding-style-patch-verification: ON
577 *
578 * Local Variables:
579 * eval: (c-set-style "gnu")
580 * End:
581 */