Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 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 Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 17 | /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 18 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 19 | /* |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 20 | * 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 Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 29 | #include <vppinfra/cache.h> |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 30 | #include <vppinfra/lock.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 31 | |
| 32 | #ifndef BIHASH_TYPE |
| 33 | #error BIHASH_TYPE not defined |
| 34 | #endif |
| 35 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 36 | #ifdef BIHASH_32_64_SVM |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 37 | #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 Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 46 | /* default is 2MB, use 30 for 1GB */ |
| 47 | #ifndef BIHASH_LOG2_HUGEPAGE_SIZE |
| 48 | #define BIHASH_LOG2_HUGEPAGE_SIZE 21 |
| 49 | #endif |
| 50 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | #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 Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 59 | #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 Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 63 | #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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 73 | typedef struct BV (clib_bihash_value) |
| 74 | { |
| 75 | union |
| 76 | { |
| 77 | BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE]; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 78 | u64 next_free_as_u64; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 79 | }; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 80 | } BVT (clib_bihash_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 81 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 82 | #define BIHASH_BUCKET_OFFSET_BITS 36 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 83 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 84 | typedef struct |
| 85 | { |
| 86 | union |
| 87 | { |
| 88 | struct |
| 89 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 90 | u64 offset:BIHASH_BUCKET_OFFSET_BITS; |
| 91 | u64 lock:1; |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 92 | u64 linear_search:1; |
| 93 | u64 log2_pages:8; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 94 | u64 refcnt:16; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | }; |
| 96 | u64 as_u64; |
| 97 | }; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 98 | } BVT (clib_bihash_bucket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 100 | STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64)); |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 101 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 102 | /* *INDENT-OFF* */ |
| 103 | typedef CLIB_PACKED (struct { |
| 104 | /* |
| 105 | * Backing store allocation. Since bihash manages its own |
Andrew Yourtchenko | a713254 | 2018-09-19 15:50:55 +0200 | [diff] [blame] | 106 | * freelists, we simple dole out memory starting from alloc_arena[alloc_arena_next]. |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 107 | */ |
Andrew Yourtchenko | a713254 | 2018-09-19 15:50:55 +0200 | [diff] [blame] | 108 | u64 alloc_arena_next; /* Next offset from alloc_arena to allocate, definitely NOT a constant */ |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 109 | u64 alloc_arena_size; /* Size of the arena */ |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 110 | u64 alloc_arena_mapped; /* Size of the mapped memory in the arena */ |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 111 | /* 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 Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 119 | u64 pad[1]; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 120 | }) BVT (clib_bihash_shared_header); |
| 121 | /* *INDENT-ON* */ |
| 122 | |
| 123 | STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64)); |
| 124 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 125 | typedef |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 126 | BVS (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 | |
| 144 | typedef |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 145 | BVS (clib_bihash) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 146 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 147 | BVT (clib_bihash_bucket) * buckets; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 148 | volatile u32 *alloc_lock; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 149 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 150 | BVT (clib_bihash_value) ** working_copies; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 151 | int *working_copy_lengths; |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 152 | BVT (clib_bihash_bucket) saved_bucket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 153 | |
| 154 | u32 nbuckets; |
| 155 | u32 log2_nbuckets; |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 156 | u64 memory_size; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 157 | u8 *name; |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 158 | format_function_t *fmt_fn; |
Damjan Marion | 2454de2 | 2020-09-26 19:32:34 +0200 | [diff] [blame] | 159 | void *heap; |
| 160 | BVT (clib_bihash_alloc_chunk) * chunks; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 162 | u64 *freelists; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 163 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 164 | #if BIHASH_32_64_SVM |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 165 | BVT (clib_bihash_shared_header) * sh; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 166 | int memfd; |
| 167 | #else |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 168 | BVT (clib_bihash_shared_header) sh; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 169 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 171 | u64 alloc_arena; /* Base of the allocation arena */ |
Dave Barach | 67d09e0 | 2019-08-01 08:15:01 -0400 | [diff] [blame] | 172 | volatile u8 instantiated; |
Nathan Skrzypczak | a8c720e | 2021-08-06 12:03:11 +0200 | [diff] [blame] | 173 | u8 dont_add_to_all_bihash_list; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 174 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 175 | /** |
| 176 | * A custom format function to print the Key and Value of bihash_key instead of default hexdump |
| 177 | */ |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 178 | format_function_t *kvp_fmt_fn; |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 179 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 180 | /** 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 Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 188 | } BVT (clib_bihash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 189 | |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 190 | typedef struct |
| 191 | { |
| 192 | BVT (clib_bihash) * h; |
| 193 | char *name; |
| 194 | u32 nbuckets; |
| 195 | uword memory_size; |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 196 | format_function_t *kvp_fmt_fn; |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 197 | u8 instantiate_immediately; |
| 198 | u8 dont_add_to_all_bihash_list; |
| 199 | } BVT (clib_bihash_init2_args); |
| 200 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 201 | extern void **clib_all_bihashes; |
| 202 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 203 | #if BIHASH_32_64_SVM |
| 204 | #undef alloc_arena_next |
| 205 | #undef alloc_arena_size |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 206 | #undef alloc_arena_mapped |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 207 | #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 Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 211 | #define alloc_arena_mapped(h) (((h)->sh)->alloc_arena_mapped) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 212 | #define alloc_arena(h) ((h)->alloc_arena) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 213 | #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE |
| 214 | #else |
| 215 | #undef alloc_arena_next |
| 216 | #undef alloc_arena_size |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 217 | #undef alloc_arena_mapped |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 218 | #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 Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 222 | #define alloc_arena_mapped(h) ((h)->sh.alloc_arena_mapped) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 223 | #define alloc_arena(h) ((h)->alloc_arena) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 224 | #define CLIB_BIHASH_READY_MAGIC 0 |
| 225 | #endif |
| 226 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 227 | #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 | |
| 243 | typedef 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 | |
| 252 | static 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 |
| 262 | static 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 Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 272 | static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 273 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 274 | while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE)) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 275 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 276 | } |
| 277 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 278 | static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 279 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 280 | __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 281 | } |
| 282 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 283 | static inline void BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 284 | { |
Damjan Marion | 74ee18b | 2020-04-21 20:14:34 +0200 | [diff] [blame] | 285 | /* *INDENT-OFF* */ |
| 286 | BVT (clib_bihash_bucket) mask = { .lock = 1 }; |
| 287 | /* *INDENT-ON* */ |
| 288 | u64 old; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 289 | |
Damjan Marion | 74ee18b | 2020-04-21 20:14:34 +0200 | [diff] [blame] | 290 | try_again: |
| 291 | old = clib_atomic_fetch_or (&b->as_u64, mask.as_u64); |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 292 | |
Damjan Marion | 74ee18b | 2020-04-21 20:14:34 +0200 | [diff] [blame] | 293 | if (PREDICT_FALSE (old & mask.as_u64)) |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 294 | { |
Damjan Marion | 74ee18b | 2020-04-21 20:14:34 +0200 | [diff] [blame] | 295 | /* somebody else flipped the bit, try again */ |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 296 | CLIB_PAUSE (); |
Damjan Marion | 74ee18b | 2020-04-21 20:14:34 +0200 | [diff] [blame] | 297 | goto try_again; |
Damjan Marion | 801ec2a | 2020-04-21 19:42:30 +0200 | [diff] [blame] | 298 | } |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static inline void BV (clib_bihash_unlock_bucket) |
| 302 | (BVT (clib_bihash_bucket) * b) |
| 303 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 304 | b->lock = 0; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 308 | uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 309 | { |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 310 | u8 *hp = (u8 *) (uword) alloc_arena (h); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 311 | u8 *vp = hp + offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | |
| 313 | return (void *) vp; |
| 314 | } |
| 315 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 316 | static inline int BV (clib_bihash_bucket_is_empty) |
| 317 | (BVT (clib_bihash_bucket) * b) |
| 318 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 319 | /* Note: applied to locked buckets, test offset */ |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 320 | if (BIHASH_KVP_AT_BUCKET_LEVEL == 0) |
| 321 | return b->offset == 0; |
| 322 | else |
| 323 | return (b->log2_pages == 0 && b->refcnt == 1); |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 326 | static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 327 | void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 329 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 330 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 331 | hp = (u8 *) (uword) alloc_arena (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | vp = (u8 *) v; |
| 333 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | return vp - hp; |
| 335 | } |
| 336 | |
Paul Atkins | 2ac4135 | 2021-01-19 15:22:23 +0000 | [diff] [blame] | 337 | #define BIHASH_ADD 1 |
| 338 | #define BIHASH_DEL 0 |
| 339 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 340 | void BV (clib_bihash_init) |
| 341 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 342 | |
Dave Barach | bdf9b97 | 2019-09-03 10:57:19 -0400 | [diff] [blame] | 343 | void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a); |
| 344 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 345 | #if BIHASH_32_64_SVM |
Dave Barach | d4a639b | 2020-08-06 11:38:40 -0400 | [diff] [blame] | 346 | void BV (clib_bihash_initiator_init_svm) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 347 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size); |
Dave Barach | d4a639b | 2020-08-06 11:38:40 -0400 | [diff] [blame] | 348 | void BV (clib_bihash_responder_init_svm) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 349 | (BVT (clib_bihash) * h, char *name, int fd); |
| 350 | #endif |
| 351 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 352 | void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h, |
Damjan Marion | f2b4a37 | 2020-09-30 14:15:24 +0200 | [diff] [blame] | 353 | format_function_t * kvp_fmt_fn); |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 354 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 355 | void BV (clib_bihash_free) (BVT (clib_bihash) * h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 357 | int BV (clib_bihash_add_del) (BVT (clib_bihash) * h, |
| 358 | BVT (clib_bihash_kv) * add_v, int is_add); |
Nathan Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame^] | 359 | |
| 360 | int 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 Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 363 | int 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 Skrzypczak | 17ecd85 | 2021-12-02 14:40:06 +0100 | [diff] [blame^] | 369 | int 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 Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 372 | int BV (clib_bihash_search) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 373 | BVT (clib_bihash_kv) * search_v, |
| 374 | BVT (clib_bihash_kv) * return_v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | |
Neale Ranns | eb69648 | 2020-09-29 14:44:23 +0000 | [diff] [blame] | 376 | int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h); |
| 377 | |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 378 | #define BIHASH_WALK_STOP 0 |
| 379 | #define BIHASH_WALK_CONTINUE 1 |
| 380 | |
| 381 | typedef |
| 382 | int (*BV (clib_bihash_foreach_key_value_pair_cb)) (BVT (clib_bihash_kv) *, |
| 383 | void *); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 384 | void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h, |
Neale Ranns | f50bac1 | 2019-12-06 05:53:17 +0000 | [diff] [blame] | 385 | BV |
| 386 | (clib_bihash_foreach_key_value_pair_cb) |
| 387 | cb, void *arg); |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 388 | void *clib_all_bihash_set_heap (void); |
| 389 | void clib_bihash_copied (void *dst, void *src); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 390 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 391 | format_function_t BV (format_bihash); |
| 392 | format_function_t BV (format_bihash_kvp); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 393 | format_function_t BV (format_bihash_lru); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 395 | static inline |
| 396 | BVT (clib_bihash_bucket) * |
| 397 | BV (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 Skrzypczak | 7be4746 | 2020-09-01 09:35:31 +0200 | [diff] [blame] | 405 | #else |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 406 | return h->buckets + (hash & (h->nbuckets - 1)); |
Nathan Skrzypczak | 7be4746 | 2020-09-01 09:35:31 +0200 | [diff] [blame] | 407 | #endif |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 408 | } |
| 409 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 410 | static inline int BV (clib_bihash_search_inline_with_hash) |
| 411 | (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 412 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 413 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 414 | BVT (clib_bihash_bucket) * b; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 415 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 416 | |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 417 | /* *INDENT-OFF* */ |
| 418 | static const BVT (clib_bihash_bucket) mask = { |
| 419 | .linear_search = 1, |
| 420 | .log2_pages = -1 |
| 421 | }; |
| 422 | /* *INDENT-ON* */ |
| 423 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 424 | #if BIHASH_LAZY_INSTANTIATE |
Nathan Skrzypczak | 42b29ba | 2020-08-17 14:14:56 +0200 | [diff] [blame] | 425 | if (PREDICT_FALSE (h->instantiated == 0)) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 426 | return -1; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 427 | #endif |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 428 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 429 | b = BV (clib_bihash_get_bucket) (h, hash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 430 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 431 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | return -1; |
| 433 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 434 | if (PREDICT_FALSE (b->lock)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 435 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 436 | volatile BVT (clib_bihash_bucket) * bv = b; |
| 437 | while (bv->lock) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 438 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 441 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 442 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 443 | /* If the bucket has unresolvable collisions, use linear search */ |
| 444 | limit = BIHASH_KVP_PER_PAGE; |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 445 | |
| 446 | if (PREDICT_FALSE (b->as_u64 & mask.as_u64)) |
| 447 | { |
| 448 | if (PREDICT_FALSE (b->linear_search)) |
| 449 | limit <<= b->log2_pages; |
| 450 | else |
| 451 | v += extract_bits (hash, h->log2_nbuckets, b->log2_pages); |
| 452 | } |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 453 | |
| 454 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 455 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 456 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key)) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 457 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 458 | *key_result = v->kvp[i]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 459 | return 0; |
| 460 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 461 | } |
| 462 | return -1; |
| 463 | } |
| 464 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 465 | static inline int BV (clib_bihash_search_inline) |
| 466 | (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result) |
| 467 | { |
| 468 | u64 hash; |
| 469 | |
| 470 | hash = BV (clib_bihash_hash) (key_result); |
| 471 | |
| 472 | return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result); |
| 473 | } |
| 474 | |
| 475 | static inline void BV (clib_bihash_prefetch_bucket) |
| 476 | (BVT (clib_bihash) * h, u64 hash) |
| 477 | { |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 478 | CLIB_PREFETCH (BV (clib_bihash_get_bucket) (h, hash), |
| 479 | BIHASH_BUCKET_PREFETCH_CACHE_LINES * CLIB_CACHE_LINE_BYTES, |
| 480 | LOAD); |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static inline void BV (clib_bihash_prefetch_data) |
| 484 | (BVT (clib_bihash) * h, u64 hash) |
| 485 | { |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 486 | BVT (clib_bihash_value) * v; |
| 487 | BVT (clib_bihash_bucket) * b; |
| 488 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 489 | #if BIHASH_LAZY_INSTANTIATE |
Nathan Skrzypczak | 42b29ba | 2020-08-17 14:14:56 +0200 | [diff] [blame] | 490 | if (PREDICT_FALSE (h->instantiated == 0)) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 491 | return; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 492 | #endif |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 493 | |
Damjan Marion | 4e14977 | 2020-03-27 16:57:28 +0100 | [diff] [blame] | 494 | b = BV (clib_bihash_get_bucket) (h, hash); |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 495 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 496 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 497 | return; |
| 498 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 499 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 500 | |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 501 | if (PREDICT_FALSE (b->log2_pages && b->linear_search == 0)) |
| 502 | v += extract_bits (hash, h->log2_nbuckets, b->log2_pages); |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 503 | |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 504 | CLIB_PREFETCH (v, BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)), |
| 505 | LOAD); |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 506 | } |
| 507 | |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 508 | static inline int BV (clib_bihash_search_inline_2_with_hash) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 509 | (BVT (clib_bihash) * h, |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 510 | u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 512 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 513 | BVT (clib_bihash_bucket) * b; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 514 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 515 | |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 516 | /* *INDENT-OFF* */ |
| 517 | static const BVT (clib_bihash_bucket) mask = { |
| 518 | .linear_search = 1, |
| 519 | .log2_pages = -1 |
| 520 | }; |
| 521 | /* *INDENT-ON* */ |
| 522 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 523 | ASSERT (valuep); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 524 | |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 525 | #if BIHASH_LAZY_INSTANTIATE |
Nathan Skrzypczak | 42b29ba | 2020-08-17 14:14:56 +0200 | [diff] [blame] | 526 | if (PREDICT_FALSE (h->instantiated == 0)) |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 527 | return -1; |
Dave Barach | 16e4a4a | 2020-04-16 12:00:14 -0400 | [diff] [blame] | 528 | #endif |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 529 | |
Damjan Marion | 4e14977 | 2020-03-27 16:57:28 +0100 | [diff] [blame] | 530 | b = BV (clib_bihash_get_bucket) (h, hash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 531 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 532 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 533 | return -1; |
| 534 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 535 | if (PREDICT_FALSE (b->lock)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 536 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 537 | volatile BVT (clib_bihash_bucket) * bv = b; |
| 538 | while (bv->lock) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 539 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 540 | } |
| 541 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 542 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 543 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 544 | /* If the bucket has unresolvable collisions, use linear search */ |
| 545 | limit = BIHASH_KVP_PER_PAGE; |
Damjan Marion | 68e5fd5 | 2020-04-23 13:41:47 +0200 | [diff] [blame] | 546 | |
| 547 | if (PREDICT_FALSE (b->as_u64 & mask.as_u64)) |
| 548 | { |
| 549 | if (PREDICT_FALSE (b->linear_search)) |
| 550 | limit <<= b->log2_pages; |
| 551 | else |
| 552 | v += extract_bits (hash, h->log2_nbuckets, b->log2_pages); |
| 553 | } |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 554 | |
| 555 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 556 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 557 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key)) |
| 558 | { |
| 559 | *valuep = v->kvp[i]; |
| 560 | return 0; |
| 561 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 562 | } |
| 563 | return -1; |
| 564 | } |
| 565 | |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 566 | static 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 579 | #endif /* __included_bihash_template_h__ */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 580 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 581 | /** @endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 582 | |
| 583 | /* |
| 584 | * fd.io coding-style-patch-verification: ON |
| 585 | * |
| 586 | * Local Variables: |
| 587 | * eval: (c-set-style "gnu") |
| 588 | * End: |
| 589 | */ |