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> |
| 29 | |
| 30 | #ifndef BIHASH_TYPE |
| 31 | #error BIHASH_TYPE not defined |
| 32 | #endif |
| 33 | |
| 34 | #define _bv(a,b) a##b |
| 35 | #define __bv(a,b) _bv(a,b) |
| 36 | #define BV(a) __bv(a,BIHASH_TYPE) |
| 37 | |
| 38 | #define _bvt(a,b) a##b##_t |
| 39 | #define __bvt(a,b) _bvt(a,b) |
| 40 | #define BVT(a) __bvt(a,BIHASH_TYPE) |
| 41 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 42 | typedef struct BV (clib_bihash_value) |
| 43 | { |
| 44 | union |
| 45 | { |
| 46 | BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE]; |
| 47 | struct BV (clib_bihash_value) * next_free; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 48 | }; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | } BVT (clib_bihash_value); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 51 | #if BIHASH_KVP_CACHE_SIZE > 5 |
| 52 | #error Requested KVP cache LRU data exceeds 16 bits |
| 53 | #endif |
| 54 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 55 | typedef struct |
| 56 | { |
| 57 | union |
| 58 | { |
| 59 | struct |
| 60 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 61 | u32 offset; |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 62 | u8 linear_search; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 63 | u8 log2_pages; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 64 | u16 cache_lru; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 65 | }; |
| 66 | u64 as_u64; |
| 67 | }; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 68 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 69 | BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE]; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 70 | #endif |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 71 | } BVT (clib_bihash_bucket); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [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 | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 77 | volatile u32 *writer_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 | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 85 | u32 linear_buckets; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 86 | u8 *name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 88 | u64 cache_hits; |
| 89 | u64 cache_misses; |
| 90 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 91 | BVT (clib_bihash_value) ** freelists; |
| 92 | void *mheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 93 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 94 | } BVT (clib_bihash); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
| 96 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 97 | static inline void |
| 98 | BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot) |
| 99 | { |
| 100 | u16 value, tmp, mask; |
| 101 | u8 found_lru_pos; |
| 102 | u16 save_hi; |
| 103 | |
| 104 | if (BIHASH_KVP_CACHE_SIZE < 2) |
| 105 | return; |
| 106 | |
| 107 | ASSERT (slot < BIHASH_KVP_CACHE_SIZE); |
| 108 | |
| 109 | /* First, find the slot in cache_lru */ |
| 110 | mask = slot; |
| 111 | if (BIHASH_KVP_CACHE_SIZE > 1) |
| 112 | mask |= slot << 3; |
| 113 | if (BIHASH_KVP_CACHE_SIZE > 2) |
| 114 | mask |= slot << 6; |
| 115 | if (BIHASH_KVP_CACHE_SIZE > 3) |
| 116 | mask |= slot << 9; |
| 117 | if (BIHASH_KVP_CACHE_SIZE > 4) |
| 118 | mask |= slot << 12; |
| 119 | |
| 120 | value = b->cache_lru; |
| 121 | tmp = value ^ mask; |
| 122 | |
| 123 | /* Already the most-recently used? */ |
| 124 | if ((tmp & 7) == 0) |
| 125 | return; |
| 126 | |
| 127 | found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0; |
| 128 | if (BIHASH_KVP_CACHE_SIZE > 2) |
| 129 | found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos; |
| 130 | if (BIHASH_KVP_CACHE_SIZE > 3) |
| 131 | found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos; |
| 132 | if (BIHASH_KVP_CACHE_SIZE > 4) |
| 133 | found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos; |
| 134 | |
| 135 | ASSERT (found_lru_pos); |
| 136 | |
| 137 | /* create a mask to kill bits in or above slot */ |
| 138 | mask = 0xFFFF << found_lru_pos; |
| 139 | mask <<= found_lru_pos; |
| 140 | mask <<= found_lru_pos; |
| 141 | mask ^= 0xFFFF; |
| 142 | tmp = value & mask; |
| 143 | |
| 144 | /* Save bits above slot */ |
| 145 | mask ^= 0xFFFF; |
| 146 | mask <<= 3; |
| 147 | save_hi = value & mask; |
| 148 | |
| 149 | value = save_hi | (tmp << 3) | slot; |
| 150 | |
| 151 | b->cache_lru = value; |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, |
| 156 | u8 slot); |
| 157 | |
| 158 | static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b) |
| 159 | { |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 160 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 161 | return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 162 | #else |
| 163 | return 0; |
| 164 | #endif |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b) |
| 168 | { |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 169 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 170 | u16 initial_lru_value; |
| 171 | |
| 172 | memset (b->cache, 0xff, sizeof (b->cache)); |
| 173 | |
| 174 | /* |
| 175 | * We'll want the cache to be loaded from slot 0 -> slot N, so |
| 176 | * the initial LRU order is reverse index order. |
| 177 | */ |
| 178 | if (BIHASH_KVP_CACHE_SIZE == 1) |
| 179 | initial_lru_value = 0; |
| 180 | else if (BIHASH_KVP_CACHE_SIZE == 2) |
| 181 | initial_lru_value = (0 << 3) | (1 << 0); |
| 182 | else if (BIHASH_KVP_CACHE_SIZE == 3) |
| 183 | initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0); |
| 184 | else if (BIHASH_KVP_CACHE_SIZE == 4) |
| 185 | initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0); |
| 186 | else if (BIHASH_KVP_CACHE_SIZE == 5) |
| 187 | initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0); |
| 188 | |
| 189 | b->cache_lru = initial_lru_value; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 190 | #endif |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 193 | static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 194 | { |
| 195 | BVT (clib_bihash_bucket) tmp_b; |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 196 | u64 rv; |
| 197 | |
| 198 | tmp_b.as_u64 = 0; |
| 199 | tmp_b.cache_lru = 1 << 15; |
| 200 | |
| 201 | rv = __sync_fetch_and_or (&b->as_u64, tmp_b.as_u64); |
| 202 | tmp_b.as_u64 = rv; |
| 203 | /* Was already locked? */ |
| 204 | if (tmp_b.cache_lru & (1 << 15)) |
| 205 | return 0; |
| 206 | return 1; |
| 207 | } |
| 208 | |
| 209 | static inline void BV (clib_bihash_unlock_bucket) |
| 210 | (BVT (clib_bihash_bucket) * b) |
| 211 | { |
| 212 | BVT (clib_bihash_bucket) tmp_b; |
| 213 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 214 | tmp_b.as_u64 = b->as_u64; |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 215 | tmp_b.cache_lru &= ~(1 << 15); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 216 | b->as_u64 = tmp_b.as_u64; |
| 217 | } |
| 218 | |
| 219 | static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 220 | uword offset) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 221 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 222 | u8 *hp = h->mheap; |
| 223 | u8 *vp = hp + offset; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 224 | |
| 225 | return (void *) vp; |
| 226 | } |
| 227 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 228 | static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 229 | void *v) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 230 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 231 | u8 *hp, *vp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | |
| 233 | hp = (u8 *) h->mheap; |
| 234 | vp = (u8 *) v; |
| 235 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 236 | ASSERT ((vp - hp) < 0x100000000ULL); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 237 | return vp - hp; |
| 238 | } |
| 239 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 240 | void BV (clib_bihash_init) |
| 241 | (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 242 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 243 | void BV (clib_bihash_free) (BVT (clib_bihash) * h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 245 | int BV (clib_bihash_add_del) (BVT (clib_bihash) * h, |
| 246 | BVT (clib_bihash_kv) * add_v, int is_add); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 247 | int BV (clib_bihash_search) (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 248 | BVT (clib_bihash_kv) * search_v, |
| 249 | BVT (clib_bihash_kv) * return_v); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 250 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 251 | void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h, |
| 252 | void *callback, void *arg); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 254 | format_function_t BV (format_bihash); |
| 255 | format_function_t BV (format_bihash_kvp); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 256 | format_function_t BV (format_bihash_lru); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 257 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 258 | static inline int BV (clib_bihash_search_inline) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 259 | (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
| 261 | u64 hash; |
| 262 | u32 bucket_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 263 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 264 | BVT (clib_bihash_bucket) * b; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 265 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 266 | BVT (clib_bihash_kv) * kvp; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 267 | #endif |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 268 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 270 | hash = BV (clib_bihash_hash) (key_result); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 271 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 272 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 273 | b = &h->buckets[bucket_index]; |
| 274 | |
| 275 | if (b->offset == 0) |
| 276 | return -1; |
| 277 | |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 278 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 279 | /* Check the cache, if not currently locked */ |
| 280 | if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 281 | { |
| 282 | limit = BIHASH_KVP_CACHE_SIZE; |
| 283 | kvp = b->cache; |
| 284 | for (i = 0; i < limit; i++) |
| 285 | { |
| 286 | if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key)) |
| 287 | { |
| 288 | *key_result = kvp[i]; |
| 289 | h->cache_hits++; |
| 290 | return 0; |
| 291 | } |
| 292 | } |
| 293 | } |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 294 | #endif |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 295 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 296 | hash >>= h->log2_nbuckets; |
| 297 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 298 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 299 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 300 | /* If the bucket has unresolvable collisions, use linear search */ |
| 301 | limit = BIHASH_KVP_PER_PAGE; |
| 302 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 303 | if (PREDICT_FALSE (b->linear_search)) |
| 304 | limit <<= b->log2_pages; |
| 305 | |
| 306 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 308 | 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] | 309 | { |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 310 | *key_result = v->kvp[i]; |
| 311 | |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 312 | #if BIHASH_KVP_CACHE_SIZE > 0 |
| 313 | u8 cache_slot; |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 314 | /* Try to lock the bucket */ |
| 315 | if (BV (clib_bihash_lock_bucket) (b)) |
| 316 | { |
| 317 | cache_slot = BV (clib_bihash_get_lru) (b); |
| 318 | b->cache[cache_slot] = v->kvp[i]; |
| 319 | BV (clib_bihash_update_lru) (b, cache_slot); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 320 | |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 321 | /* Unlock the bucket */ |
| 322 | BV (clib_bihash_unlock_bucket) (b); |
| 323 | h->cache_misses++; |
| 324 | } |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 325 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 326 | return 0; |
| 327 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | } |
| 329 | return -1; |
| 330 | } |
| 331 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 332 | static inline int BV (clib_bihash_search_inline_2) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 333 | (BVT (clib_bihash) * h, |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 334 | BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | { |
| 336 | u64 hash; |
| 337 | u32 bucket_index; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 338 | BVT (clib_bihash_value) * v; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 339 | BVT (clib_bihash_bucket) * b; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 340 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 341 | BVT (clib_bihash_kv) * kvp; |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 342 | #endif |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 343 | int i, limit; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 344 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 345 | ASSERT (valuep); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 346 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 347 | hash = BV (clib_bihash_hash) (search_key); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 348 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 349 | bucket_index = hash & (h->nbuckets - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 350 | b = &h->buckets[bucket_index]; |
| 351 | |
| 352 | if (b->offset == 0) |
| 353 | return -1; |
| 354 | |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 355 | /* Check the cache, if currently unlocked */ |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 356 | #if BIHASH_KVP_CACHE_SIZE > 0 |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 357 | if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0)) |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 358 | { |
| 359 | limit = BIHASH_KVP_CACHE_SIZE; |
| 360 | kvp = b->cache; |
| 361 | for (i = 0; i < limit; i++) |
| 362 | { |
| 363 | if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key)) |
| 364 | { |
| 365 | *valuep = kvp[i]; |
| 366 | h->cache_hits++; |
| 367 | return 0; |
| 368 | } |
| 369 | } |
| 370 | } |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 371 | #endif |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 372 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 373 | hash >>= h->log2_nbuckets; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 374 | v = BV (clib_bihash_get_value) (h, b->offset); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 375 | |
Dave Barach | 5e6b958 | 2016-12-12 15:37:29 -0500 | [diff] [blame] | 376 | /* If the bucket has unresolvable collisions, use linear search */ |
| 377 | limit = BIHASH_KVP_PER_PAGE; |
| 378 | v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0; |
| 379 | if (PREDICT_FALSE (b->linear_search)) |
| 380 | limit <<= b->log2_pages; |
| 381 | |
| 382 | for (i = 0; i < limit; i++) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 383 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 384 | if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key)) |
| 385 | { |
| 386 | *valuep = v->kvp[i]; |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 387 | |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 388 | #if BIHASH_KVP_CACHE_SIZE > 0 |
| 389 | u8 cache_slot; |
| 390 | |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 391 | /* Try to lock the bucket */ |
| 392 | if (BV (clib_bihash_lock_bucket) (b)) |
| 393 | { |
| 394 | cache_slot = BV (clib_bihash_get_lru) (b); |
| 395 | b->cache[cache_slot] = v->kvp[i]; |
| 396 | BV (clib_bihash_update_lru) (b, cache_slot); |
Dave Barach | 908a5ea | 2017-07-14 12:42:21 -0400 | [diff] [blame] | 397 | |
Dave Barach | 858c06f | 2017-07-21 10:44:27 -0400 | [diff] [blame] | 398 | /* Reenable the cache */ |
| 399 | BV (clib_bihash_unlock_bucket) (b); |
| 400 | h->cache_misses++; |
| 401 | } |
Dave Barach | c8ef08a | 2017-08-31 05:58:22 -0400 | [diff] [blame] | 402 | #endif |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 403 | return 0; |
| 404 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | } |
| 406 | return -1; |
| 407 | } |
| 408 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | #endif /* __included_bihash_template_h__ */ |
Dave Barach | dd3a57f | 2016-07-27 16:58:51 -0400 | [diff] [blame] | 410 | |
Chris Luke | 16bcf7d | 2016-09-01 14:31:46 -0400 | [diff] [blame] | 411 | /** @endcond */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 412 | |
| 413 | /* |
| 414 | * fd.io coding-style-patch-verification: ON |
| 415 | * |
| 416 | * Local Variables: |
| 417 | * eval: (c-set-style "gnu") |
| 418 | * End: |
| 419 | */ |