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