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 |
| 37 | #undef HAVE_MEMFD_CREATE |
| 38 | #include <vppinfra/linux/syscall.h> |
| 39 | #include <fcntl.h> |
| 40 | #define F_LINUX_SPECIFIC_BASE 1024 |
| 41 | #define F_ADD_SEALS (F_LINUX_SPECIFIC_BASE + 9) |
| 42 | #define F_SEAL_SHRINK (2) |
| 43 | /* Max page size 2**16 due to refcount width */ |
| 44 | #define BIHASH_FREELIST_LENGTH 17 |
| 45 | #endif |
| 46 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 47 | #define _bv(a,b) a##b |
| 48 | #define __bv(a,b) _bv(a,b) |
| 49 | #define BV(a) __bv(a,BIHASH_TYPE) |
| 50 | |
| 51 | #define _bvt(a,b) a##b##_t |
| 52 | #define __bvt(a,b) _bvt(a,b) |
| 53 | #define BVT(a) __bvt(a,BIHASH_TYPE) |
| 54 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 55 | #if _LP64 == 0 |
| 56 | #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0) |
| 57 | #define u64_to_pointer(x) (void *)(u32)((x)) |
| 58 | #define pointer_to_u64(x) (u64)(u32)((x)) |
| 59 | #else |
| 60 | #define OVERFLOW_ASSERT(x) |
| 61 | #define u64_to_pointer(x) (void *)((x)) |
| 62 | #define pointer_to_u64(x) (u64)((x)) |
| 63 | #endif |
| 64 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 65 | typedef struct BV (clib_bihash_value) |
| 66 | { |
| 67 | union |
| 68 | { |
| 69 | BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE]; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 70 | u64 next_free_as_u64; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 71 | }; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 72 | } BVT (clib_bihash_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 74 | #define BIHASH_BUCKET_OFFSET_BITS 36 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 75 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | typedef struct |
| 77 | { |
| 78 | union |
| 79 | { |
| 80 | struct |
| 81 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 82 | u64 offset:BIHASH_BUCKET_OFFSET_BITS; |
| 83 | u64 lock:1; |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 84 | u64 linear_search:1; |
| 85 | u64 log2_pages:8; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 86 | u64 refcnt:16; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | }; |
| 88 | u64 as_u64; |
| 89 | }; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 90 | } BVT (clib_bihash_bucket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 92 | STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64)); |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 93 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 94 | /* *INDENT-OFF* */ |
| 95 | typedef CLIB_PACKED (struct { |
| 96 | /* |
| 97 | * Backing store allocation. Since bihash manages its own |
| 98 | * freelists, we simple dole out memory at alloc_arena_next. |
| 99 | */ |
| 100 | u64 alloc_arena_next; /* Next VA to allocate, definitely NOT a constant */ |
| 101 | u64 alloc_arena_size; /* Size of the arena */ |
| 102 | u64 alloc_arena; /* Base VA of the arena */ |
| 103 | /* Two SVM pointers stored as 8-byte integers */ |
| 104 | u64 alloc_lock_as_u64; |
| 105 | u64 buckets_as_u64; |
| 106 | /* freelist list-head arrays/vectors */ |
| 107 | u64 freelists_as_u64; |
| 108 | u32 nbuckets; /* Number of buckets */ |
| 109 | /* Set when header valid */ |
| 110 | volatile u32 ready; |
| 111 | u64 pad; |
| 112 | }) BVT (clib_bihash_shared_header); |
| 113 | /* *INDENT-ON* */ |
| 114 | |
| 115 | STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64)); |
| 116 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 117 | typedef struct |
| 118 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 119 | BVT (clib_bihash_bucket) * buckets; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 120 | volatile u32 *alloc_lock; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 122 | BVT (clib_bihash_value) ** working_copies; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 123 | int *working_copy_lengths; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 124 | BVT (clib_bihash_bucket) saved_bucket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
| 126 | u32 nbuckets; |
| 127 | u32 log2_nbuckets; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 128 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 130 | u64 *freelists; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 131 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 132 | #if BIHASH_32_64_SVM |
| 133 | BVT (clib_bihash_shared_header) * sh; |
| 134 | int memfd; |
| 135 | #else |
| 136 | BVT (clib_bihash_shared_header) sh; |
| 137 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 139 | /** |
| 140 | * A custom format function to print the Key and Value of bihash_key instead of default hexdump |
| 141 | */ |
| 142 | format_function_t *fmt_fn; |
| 143 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 144 | } BVT (clib_bihash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 145 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 146 | #if BIHASH_32_64_SVM |
| 147 | #undef alloc_arena_next |
| 148 | #undef alloc_arena_size |
| 149 | #undef alloc_arena |
| 150 | #undef CLIB_BIHASH_READY_MAGIC |
| 151 | #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next) |
| 152 | #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size) |
| 153 | #define alloc_arena(h) (((h)->sh)->alloc_arena) |
| 154 | #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE |
| 155 | #else |
| 156 | #undef alloc_arena_next |
| 157 | #undef alloc_arena_size |
| 158 | #undef alloc_arena |
| 159 | #undef CLIB_BIHASH_READY_MAGIC |
| 160 | #define alloc_arena_next(h) ((h)->sh.alloc_arena_next) |
| 161 | #define alloc_arena_size(h) ((h)->sh.alloc_arena_size) |
| 162 | #define alloc_arena(h) ((h)->sh.alloc_arena) |
| 163 | #define CLIB_BIHASH_READY_MAGIC 0 |
| 164 | #endif |
| 165 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 166 | static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 167 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 168 | while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE)) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 169 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 170 | } |
| 171 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 172 | static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 173 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 174 | __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 177 | 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] | 178 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 179 | BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 180 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 181 | do |
| 182 | { |
| 183 | locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64; |
| 184 | unlocked_bucket.lock = 0; |
| 185 | locked_bucket.lock = 1; |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 186 | CLIB_PAUSE (); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 187 | } |
| 188 | while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64, |
| 189 | locked_bucket.as_u64, 1 /* weak */ , |
| 190 | __ATOMIC_ACQUIRE, |
| 191 | __ATOMIC_ACQUIRE) == 0); |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | static inline void BV (clib_bihash_unlock_bucket) |
| 195 | (BVT (clib_bihash_bucket) * b) |
| 196 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 197 | CLIB_MEMORY_BARRIER (); |
| 198 | b->lock = 0; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 202 | uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 203 | { |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 204 | u8 *hp = (u8 *) (uword) alloc_arena (h); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 205 | u8 *vp = hp + offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 206 | |
| 207 | return (void *) vp; |
| 208 | } |
| 209 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 210 | static inline int BV (clib_bihash_bucket_is_empty) |
| 211 | (BVT (clib_bihash_bucket) * b) |
| 212 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 213 | /* Note: applied to locked buckets, test offset */ |
| 214 | return b->offset == 0; |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 217 | static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 218 | void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 220 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 222 | hp = (u8 *) (uword) alloc_arena (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 223 | vp = (u8 *) v; |
| 224 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | return vp - hp; |
| 226 | } |
| 227 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 228 | void BV (clib_bihash_init) |
| 229 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 231 | #if BIHASH_32_64_SVM |
| 232 | void BV (clib_bihash_master_init_svm) |
| 233 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, |
| 234 | u64 base_address, u64 memory_size); |
| 235 | void BV (clib_bihash_slave_init_svm) |
| 236 | (BVT (clib_bihash) * h, char *name, int fd); |
| 237 | #endif |
| 238 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 239 | void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h, |
| 240 | format_function_t * fmt_fn); |
| 241 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 242 | void BV (clib_bihash_free) (BVT (clib_bihash) * h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 244 | int BV (clib_bihash_add_del) (BVT (clib_bihash) * h, |
| 245 | BVT (clib_bihash_kv) * add_v, int is_add); |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 246 | int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h, |
| 247 | BVT (clib_bihash_kv) * add_v, |
| 248 | int (*is_stale_cb) (BVT |
| 249 | (clib_bihash_kv) |
| 250 | *, void *), |
| 251 | void *arg); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 252 | int BV (clib_bihash_search) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 253 | BVT (clib_bihash_kv) * search_v, |
| 254 | BVT (clib_bihash_kv) * return_v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 255 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 256 | void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h, |
| 257 | void *callback, void *arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 259 | format_function_t BV (format_bihash); |
| 260 | format_function_t BV (format_bihash_kvp); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 261 | format_function_t BV (format_bihash_lru); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 262 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 263 | static inline int BV (clib_bihash_search_inline_with_hash) |
| 264 | (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 266 | u32 bucket_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 267 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 268 | BVT (clib_bihash_bucket) * b; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 269 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 270 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 271 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 272 | b = &h->buckets[bucket_index]; |
| 273 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 274 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 275 | return -1; |
| 276 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 277 | if (PREDICT_FALSE (b->lock)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 278 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 279 | volatile BVT (clib_bihash_bucket) * bv = b; |
| 280 | while (bv->lock) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 281 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | hash >>= h->log2_nbuckets; |
| 285 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 286 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 287 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 288 | /* If the bucket has unresolvable collisions, use linear search */ |
| 289 | limit = BIHASH_KVP_PER_PAGE; |
| 290 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 291 | if (PREDICT_FALSE (b->linear_search)) |
| 292 | limit <<= b->log2_pages; |
| 293 | |
| 294 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 295 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 296 | 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] | 297 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 298 | *key_result = v->kvp[i]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 299 | return 0; |
| 300 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | } |
| 302 | return -1; |
| 303 | } |
| 304 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 305 | static inline int BV (clib_bihash_search_inline) |
| 306 | (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result) |
| 307 | { |
| 308 | u64 hash; |
| 309 | |
| 310 | hash = BV (clib_bihash_hash) (key_result); |
| 311 | |
| 312 | return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result); |
| 313 | } |
| 314 | |
| 315 | static inline void BV (clib_bihash_prefetch_bucket) |
| 316 | (BVT (clib_bihash) * h, u64 hash) |
| 317 | { |
| 318 | u32 bucket_index; |
| 319 | BVT (clib_bihash_bucket) * b; |
| 320 | |
| 321 | bucket_index = hash & (h->nbuckets - 1); |
| 322 | b = &h->buckets[bucket_index]; |
| 323 | |
| 324 | CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ); |
| 325 | } |
| 326 | |
| 327 | static inline void BV (clib_bihash_prefetch_data) |
| 328 | (BVT (clib_bihash) * h, u64 hash) |
| 329 | { |
| 330 | u32 bucket_index; |
| 331 | BVT (clib_bihash_value) * v; |
| 332 | BVT (clib_bihash_bucket) * b; |
| 333 | |
| 334 | bucket_index = hash & (h->nbuckets - 1); |
| 335 | b = &h->buckets[bucket_index]; |
| 336 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 337 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 338 | return; |
| 339 | |
| 340 | hash >>= h->log2_nbuckets; |
| 341 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 342 | |
| 343 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 344 | |
| 345 | CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ); |
| 346 | } |
| 347 | |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 348 | static inline int BV (clib_bihash_search_inline_2_with_hash) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 349 | (BVT (clib_bihash) * h, |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 350 | 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] | 351 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 352 | u32 bucket_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 353 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 354 | BVT (clib_bihash_bucket) * b; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 355 | int i, limit; |
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 | ASSERT (valuep); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 359 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 360 | b = &h->buckets[bucket_index]; |
| 361 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 362 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 363 | return -1; |
| 364 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 365 | if (PREDICT_FALSE (b->lock)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 366 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 367 | volatile BVT (clib_bihash_bucket) * bv = b; |
| 368 | while (bv->lock) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 369 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 370 | } |
| 371 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 372 | hash >>= h->log2_nbuckets; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 373 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 374 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 375 | /* If the bucket has unresolvable collisions, use linear search */ |
| 376 | limit = BIHASH_KVP_PER_PAGE; |
| 377 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 378 | if (PREDICT_FALSE (b->linear_search)) |
| 379 | limit <<= b->log2_pages; |
| 380 | |
| 381 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 382 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 383 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key)) |
| 384 | { |
| 385 | *valuep = v->kvp[i]; |
| 386 | return 0; |
| 387 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 388 | } |
| 389 | return -1; |
| 390 | } |
| 391 | |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 392 | static inline int BV (clib_bihash_search_inline_2) |
| 393 | (BVT (clib_bihash) * h, |
| 394 | BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep) |
| 395 | { |
| 396 | u64 hash; |
| 397 | |
| 398 | hash = BV (clib_bihash_hash) (search_key); |
| 399 | |
| 400 | return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key, |
| 401 | valuep); |
| 402 | } |
| 403 | |
| 404 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | #endif /* __included_bihash_template_h__ */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 406 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 407 | /** @endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 408 | |
| 409 | /* |
| 410 | * fd.io coding-style-patch-verification: ON |
| 411 | * |
| 412 | * Local Variables: |
| 413 | * eval: (c-set-style "gnu") |
| 414 | * End: |
| 415 | */ |