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