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 | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 55 | #define _bvs(a,b) struct a##b |
| 56 | #define __bvs(a,b) _bvs(a,b) |
| 57 | #define BVS(a) __bvs(a,BIHASH_TYPE) |
| 58 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 59 | #if _LP64 == 0 |
| 60 | #define OVERFLOW_ASSERT(x) ASSERT(((x) & 0xFFFFFFFF00000000ULL) == 0) |
| 61 | #define u64_to_pointer(x) (void *)(u32)((x)) |
| 62 | #define pointer_to_u64(x) (u64)(u32)((x)) |
| 63 | #else |
| 64 | #define OVERFLOW_ASSERT(x) |
| 65 | #define u64_to_pointer(x) (void *)((x)) |
| 66 | #define pointer_to_u64(x) (u64)((x)) |
| 67 | #endif |
| 68 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 69 | typedef struct BV (clib_bihash_value) |
| 70 | { |
| 71 | union |
| 72 | { |
| 73 | BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE]; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 74 | u64 next_free_as_u64; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | }; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 76 | } BVT (clib_bihash_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 78 | #define BIHASH_BUCKET_OFFSET_BITS 36 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 79 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 80 | typedef struct |
| 81 | { |
| 82 | union |
| 83 | { |
| 84 | struct |
| 85 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 86 | u64 offset:BIHASH_BUCKET_OFFSET_BITS; |
| 87 | u64 lock:1; |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 88 | u64 linear_search:1; |
| 89 | u64 log2_pages:8; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 90 | u64 refcnt:16; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | }; |
| 92 | u64 as_u64; |
| 93 | }; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 94 | } BVT (clib_bihash_bucket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 96 | STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64)); |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 97 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 98 | /* *INDENT-OFF* */ |
| 99 | typedef CLIB_PACKED (struct { |
| 100 | /* |
| 101 | * Backing store allocation. Since bihash manages its own |
Andrew Yourtchenko | a713254 | 2018-09-19 15:50:55 +0200 | [diff] [blame] | 102 | * 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] | 103 | */ |
Andrew Yourtchenko | a713254 | 2018-09-19 15:50:55 +0200 | [diff] [blame] | 104 | 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] | 105 | u64 alloc_arena_size; /* Size of the arena */ |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 106 | /* Two SVM pointers stored as 8-byte integers */ |
| 107 | u64 alloc_lock_as_u64; |
| 108 | u64 buckets_as_u64; |
| 109 | /* freelist list-head arrays/vectors */ |
| 110 | u64 freelists_as_u64; |
| 111 | u32 nbuckets; /* Number of buckets */ |
| 112 | /* Set when header valid */ |
| 113 | volatile u32 ready; |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 114 | u64 pad[2]; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 115 | }) BVT (clib_bihash_shared_header); |
| 116 | /* *INDENT-ON* */ |
| 117 | |
| 118 | STATIC_ASSERT_SIZEOF (BVT (clib_bihash_shared_header), 8 * sizeof (u64)); |
| 119 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 120 | typedef |
| 121 | BVS (clib_bihash) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 122 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 123 | BVT (clib_bihash_bucket) * buckets; |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 124 | volatile u32 *alloc_lock; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 125 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 126 | BVT (clib_bihash_value) ** working_copies; |
Dave Barach | ba7ddfe | 2017-05-17 20:20:50 -0400 | [diff] [blame] | 127 | int *working_copy_lengths; |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 128 | BVT (clib_bihash_bucket) saved_bucket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 129 | |
| 130 | u32 nbuckets; |
| 131 | u32 log2_nbuckets; |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 132 | u64 memory_size; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 133 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 135 | u64 *freelists; |
Dave Barach | 97f5af0 | 2018-02-22 09:48:45 -0500 | [diff] [blame] | 136 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 137 | #if BIHASH_32_64_SVM |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 138 | BVT (clib_bihash_shared_header) * sh; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 139 | int memfd; |
| 140 | #else |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 141 | BVT (clib_bihash_shared_header) sh; |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 142 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 143 | |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 144 | u64 alloc_arena; /* Base of the allocation arena */ |
| 145 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 146 | /** |
| 147 | * A custom format function to print the Key and Value of bihash_key instead of default hexdump |
| 148 | */ |
| 149 | format_function_t *fmt_fn; |
| 150 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 151 | /** Optional statistics-gathering callback */ |
| 152 | #if BIHASH_ENABLE_STATS |
| 153 | void (*inc_stats_callback) (BVS (clib_bihash) *, int stat_id, u64 count); |
| 154 | |
| 155 | /** Statistics callback context (e.g. address of stats data structure) */ |
| 156 | void *inc_stats_context; |
| 157 | #endif |
| 158 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 159 | } BVT (clib_bihash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 160 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 161 | extern void **clib_all_bihashes; |
| 162 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 163 | #if BIHASH_32_64_SVM |
| 164 | #undef alloc_arena_next |
| 165 | #undef alloc_arena_size |
| 166 | #undef alloc_arena |
| 167 | #undef CLIB_BIHASH_READY_MAGIC |
| 168 | #define alloc_arena_next(h) (((h)->sh)->alloc_arena_next) |
| 169 | #define alloc_arena_size(h) (((h)->sh)->alloc_arena_size) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 170 | #define alloc_arena(h) ((h)->alloc_arena) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 171 | #define CLIB_BIHASH_READY_MAGIC 0xFEEDFACE |
| 172 | #else |
| 173 | #undef alloc_arena_next |
| 174 | #undef alloc_arena_size |
| 175 | #undef alloc_arena |
| 176 | #undef CLIB_BIHASH_READY_MAGIC |
| 177 | #define alloc_arena_next(h) ((h)->sh.alloc_arena_next) |
| 178 | #define alloc_arena_size(h) ((h)->sh.alloc_arena_size) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 179 | #define alloc_arena(h) ((h)->alloc_arena) |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 180 | #define CLIB_BIHASH_READY_MAGIC 0 |
| 181 | #endif |
| 182 | |
Dave Barach | 2ce28d6 | 2019-05-03 12:58:01 -0400 | [diff] [blame] | 183 | #ifndef BIHASH_STAT_IDS |
| 184 | #define BIHASH_STAT_IDS 1 |
| 185 | |
| 186 | #define foreach_bihash_stat \ |
| 187 | _(alloc_add) \ |
| 188 | _(add) \ |
| 189 | _(split_add) \ |
| 190 | _(replace) \ |
| 191 | _(update) \ |
| 192 | _(del) \ |
| 193 | _(del_free) \ |
| 194 | _(linear) \ |
| 195 | _(resplit) \ |
| 196 | _(working_copy_lost) \ |
| 197 | _(splits) /* must be last */ |
| 198 | |
| 199 | typedef enum |
| 200 | { |
| 201 | #define _(a) BIHASH_STAT_##a, |
| 202 | foreach_bihash_stat |
| 203 | #undef _ |
| 204 | BIHASH_STAT_N_STATS, |
| 205 | } BVT (clib_bihash_stat_id); |
| 206 | #endif /* BIHASH_STAT_IDS */ |
| 207 | |
| 208 | static inline void BV (clib_bihash_increment_stat) (BVT (clib_bihash) * h, |
| 209 | int stat_id, u64 count) |
| 210 | { |
| 211 | #if BIHASH_ENABLE_STATS |
| 212 | if (PREDICT_FALSE (h->inc_stats_callback != 0)) |
| 213 | h->inc_stats_callback (h, stat_id, count); |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | #if BIHASH_ENABLE_STATS |
| 218 | static inline void BV (clib_bihash_set_stats_callback) |
| 219 | (BVT (clib_bihash) * h, void (*cb) (BVT (clib_bihash) *, int, u64), |
| 220 | void *ctx) |
| 221 | { |
| 222 | h->inc_stats_callback = cb; |
| 223 | h->inc_stats_context = ctx; |
| 224 | } |
| 225 | #endif |
| 226 | |
| 227 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 228 | static inline void BV (clib_bihash_alloc_lock) (BVT (clib_bihash) * h) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 229 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 230 | while (__atomic_test_and_set (h->alloc_lock, __ATOMIC_ACQUIRE)) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 231 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 232 | } |
| 233 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 234 | static inline void BV (clib_bihash_alloc_unlock) (BVT (clib_bihash) * h) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 235 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 236 | __atomic_clear (h->alloc_lock, __ATOMIC_RELEASE); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 237 | } |
| 238 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 239 | 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] | 240 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 241 | BVT (clib_bihash_bucket) unlocked_bucket, locked_bucket; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 242 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 243 | do |
| 244 | { |
| 245 | locked_bucket.as_u64 = unlocked_bucket.as_u64 = b->as_u64; |
| 246 | unlocked_bucket.lock = 0; |
| 247 | locked_bucket.lock = 1; |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 248 | CLIB_PAUSE (); |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 249 | } |
| 250 | while (__atomic_compare_exchange_n (&b->as_u64, &unlocked_bucket.as_u64, |
| 251 | locked_bucket.as_u64, 1 /* weak */ , |
| 252 | __ATOMIC_ACQUIRE, |
| 253 | __ATOMIC_ACQUIRE) == 0); |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static inline void BV (clib_bihash_unlock_bucket) |
| 257 | (BVT (clib_bihash_bucket) * b) |
| 258 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 259 | CLIB_MEMORY_BARRIER (); |
| 260 | b->lock = 0; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 264 | uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 265 | { |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 266 | u8 *hp = (u8 *) (uword) alloc_arena (h); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 267 | u8 *vp = hp + offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
| 269 | return (void *) vp; |
| 270 | } |
| 271 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 272 | static inline int BV (clib_bihash_bucket_is_empty) |
| 273 | (BVT (clib_bihash_bucket) * b) |
| 274 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 275 | /* Note: applied to locked buckets, test offset */ |
| 276 | return b->offset == 0; |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 279 | static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 280 | void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 281 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 282 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 283 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 284 | hp = (u8 *) (uword) alloc_arena (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | vp = (u8 *) v; |
| 286 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 287 | return vp - hp; |
| 288 | } |
| 289 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 290 | void BV (clib_bihash_init) |
| 291 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 292 | |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 293 | #if BIHASH_32_64_SVM |
| 294 | void BV (clib_bihash_master_init_svm) |
Dave Barach | ffb14b9 | 2018-09-11 17:20:23 -0400 | [diff] [blame] | 295 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size); |
Dave Barach | 9466c45 | 2018-08-24 17:21:14 -0400 | [diff] [blame] | 296 | void BV (clib_bihash_slave_init_svm) |
| 297 | (BVT (clib_bihash) * h, char *name, int fd); |
| 298 | #endif |
| 299 | |
Vijayabhaskar Katamreddy | fb8e61c | 2017-12-14 13:20:50 -0800 | [diff] [blame] | 300 | void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h, |
| 301 | format_function_t * fmt_fn); |
| 302 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 303 | void BV (clib_bihash_free) (BVT (clib_bihash) * h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 304 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 305 | int BV (clib_bihash_add_del) (BVT (clib_bihash) * h, |
| 306 | BVT (clib_bihash_kv) * add_v, int is_add); |
Matus Fabian | 828d27e | 2018-08-21 03:15:50 -0700 | [diff] [blame] | 307 | int BV (clib_bihash_add_or_overwrite_stale) (BVT (clib_bihash) * h, |
| 308 | BVT (clib_bihash_kv) * add_v, |
| 309 | int (*is_stale_cb) (BVT |
| 310 | (clib_bihash_kv) |
| 311 | *, void *), |
| 312 | void *arg); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 313 | int BV (clib_bihash_search) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 314 | BVT (clib_bihash_kv) * search_v, |
| 315 | BVT (clib_bihash_kv) * return_v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 316 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 317 | void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h, |
| 318 | void *callback, void *arg); |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 319 | void *clib_all_bihash_set_heap (void); |
| 320 | void clib_bihash_copied (void *dst, void *src); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 322 | format_function_t BV (format_bihash); |
| 323 | format_function_t BV (format_bihash_kvp); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 324 | format_function_t BV (format_bihash_lru); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 325 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 326 | static inline int BV (clib_bihash_search_inline_with_hash) |
| 327 | (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 329 | u32 bucket_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 330 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 331 | BVT (clib_bihash_bucket) * b; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 332 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 333 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 334 | if (PREDICT_FALSE (alloc_arena (h) == 0)) |
| 335 | return -1; |
| 336 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 337 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 338 | b = &h->buckets[bucket_index]; |
| 339 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 340 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 341 | return -1; |
| 342 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 343 | if (PREDICT_FALSE (b->lock)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 344 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 345 | volatile BVT (clib_bihash_bucket) * bv = b; |
| 346 | while (bv->lock) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 347 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 348 | } |
| 349 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | hash >>= h->log2_nbuckets; |
| 351 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 352 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 353 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 354 | /* If the bucket has unresolvable collisions, use linear search */ |
| 355 | limit = BIHASH_KVP_PER_PAGE; |
| 356 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 357 | if (PREDICT_FALSE (b->linear_search)) |
| 358 | limit <<= b->log2_pages; |
| 359 | |
| 360 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 361 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 362 | 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] | 363 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 364 | *key_result = v->kvp[i]; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 365 | return 0; |
| 366 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | } |
| 368 | return -1; |
| 369 | } |
| 370 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 371 | static inline int BV (clib_bihash_search_inline) |
| 372 | (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result) |
| 373 | { |
| 374 | u64 hash; |
| 375 | |
| 376 | hash = BV (clib_bihash_hash) (key_result); |
| 377 | |
| 378 | return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result); |
| 379 | } |
| 380 | |
| 381 | static inline void BV (clib_bihash_prefetch_bucket) |
| 382 | (BVT (clib_bihash) * h, u64 hash) |
| 383 | { |
| 384 | u32 bucket_index; |
| 385 | BVT (clib_bihash_bucket) * b; |
| 386 | |
| 387 | bucket_index = hash & (h->nbuckets - 1); |
| 388 | b = &h->buckets[bucket_index]; |
| 389 | |
| 390 | CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ); |
| 391 | } |
| 392 | |
| 393 | static inline void BV (clib_bihash_prefetch_data) |
| 394 | (BVT (clib_bihash) * h, u64 hash) |
| 395 | { |
| 396 | u32 bucket_index; |
| 397 | BVT (clib_bihash_value) * v; |
| 398 | BVT (clib_bihash_bucket) * b; |
| 399 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 400 | if (PREDICT_FALSE (alloc_arena (h) == 0)) |
| 401 | return; |
| 402 | |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 403 | bucket_index = hash & (h->nbuckets - 1); |
| 404 | b = &h->buckets[bucket_index]; |
| 405 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 406 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Dave Barach | 30765e7 | 2018-02-23 07:45:36 -0500 | [diff] [blame] | 407 | return; |
| 408 | |
| 409 | hash >>= h->log2_nbuckets; |
| 410 | v = BV (clib_bihash_get_value) (h, b->offset); |
| 411 | |
| 412 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 413 | |
| 414 | CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ); |
| 415 | } |
| 416 | |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 417 | static inline int BV (clib_bihash_search_inline_2_with_hash) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 418 | (BVT (clib_bihash) * h, |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 419 | 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] | 420 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | u32 bucket_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 422 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 423 | BVT (clib_bihash_bucket) * b; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 424 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 425 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 426 | ASSERT (valuep); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 427 | |
Dave Barach | 32dcd3b | 2019-07-08 12:25:38 -0400 | [diff] [blame] | 428 | if (PREDICT_FALSE (alloc_arena (h) == 0)) |
| 429 | return -1; |
| 430 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 431 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 432 | b = &h->buckets[bucket_index]; |
| 433 | |
Damjan Marion | 882fcfe | 2018-07-17 23:01:49 +0200 | [diff] [blame] | 434 | if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 435 | return -1; |
| 436 | |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 437 | if (PREDICT_FALSE (b->lock)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 438 | { |
Dave Barach | 508498f | 2018-07-19 12:11:16 -0400 | [diff] [blame] | 439 | volatile BVT (clib_bihash_bucket) * bv = b; |
| 440 | while (bv->lock) |
Damjan Marion | 2a03efe | 2018-07-20 21:48:59 +0200 | [diff] [blame] | 441 | CLIB_PAUSE (); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 442 | } |
| 443 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 444 | hash >>= h->log2_nbuckets; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 445 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 446 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 447 | /* If the bucket has unresolvable collisions, use linear search */ |
| 448 | limit = BIHASH_KVP_PER_PAGE; |
| 449 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 450 | if (PREDICT_FALSE (b->linear_search)) |
| 451 | limit <<= b->log2_pages; |
| 452 | |
| 453 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 454 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 455 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key)) |
| 456 | { |
| 457 | *valuep = v->kvp[i]; |
| 458 | return 0; |
| 459 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 460 | } |
| 461 | return -1; |
| 462 | } |
| 463 | |
Andrew Yourtchenko | abcddcb | 2018-05-27 20:56:26 +0200 | [diff] [blame] | 464 | static inline int BV (clib_bihash_search_inline_2) |
| 465 | (BVT (clib_bihash) * h, |
| 466 | BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep) |
| 467 | { |
| 468 | u64 hash; |
| 469 | |
| 470 | hash = BV (clib_bihash_hash) (search_key); |
| 471 | |
| 472 | return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key, |
| 473 | valuep); |
| 474 | } |
| 475 | |
| 476 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 477 | #endif /* __included_bihash_template_h__ */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 478 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 479 | /** @endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * fd.io coding-style-patch-verification: ON |
| 483 | * |
| 484 | * Local Variables: |
| 485 | * eval: (c-set-style "gnu") |
| 486 | * End: |
| 487 | */ |