blob: 2101d44defe71b3eeb0cd8b4a1c20fbbfc16fea0 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Luke16bcf7d2016-09-01 14:31:46 -040017/** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
Dave Barachdd3a57f2016-07-27 16:58:51 -040018
Dave Barachc3799992016-08-15 11:12:27 -040019/*
Ed Warnickecb9cada2015-12-08 15:45:58 -070020 * 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 Barach30765e72018-02-23 07:45:36 -050029#include <vppinfra/cache.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
31#ifndef BIHASH_TYPE
32#error BIHASH_TYPE not defined
33#endif
34
35#define _bv(a,b) a##b
36#define __bv(a,b) _bv(a,b)
37#define BV(a) __bv(a,BIHASH_TYPE)
38
39#define _bvt(a,b) a##b##_t
40#define __bvt(a,b) _bvt(a,b)
41#define BVT(a) __bvt(a,BIHASH_TYPE)
42
Dave Barachc3799992016-08-15 11:12:27 -040043typedef struct BV (clib_bihash_value)
44{
45 union
46 {
47 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
48 struct BV (clib_bihash_value) * next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 };
Dave Barachc3799992016-08-15 11:12:27 -040050} BVT (clib_bihash_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Dave Barach908a5ea2017-07-14 12:42:21 -040052#if BIHASH_KVP_CACHE_SIZE > 5
53#error Requested KVP cache LRU data exceeds 16 bits
54#endif
55
Dave Barachc3799992016-08-15 11:12:27 -040056typedef struct
57{
58 union
59 {
60 struct
61 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 u32 offset;
Dave Barach5e6b9582016-12-12 15:37:29 -050063 u8 linear_search;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064 u8 log2_pages;
Dave Barache7d212f2018-02-07 13:14:06 -050065 i16 refcnt;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066 };
67 u64 as_u64;
68 };
Dave Barachc8ef08a2017-08-31 05:58:22 -040069#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barache7d212f2018-02-07 13:14:06 -050070 u16 cache_lru;
Dave Barach908a5ea2017-07-14 12:42:21 -040071 BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
Dave Barachc8ef08a2017-08-31 05:58:22 -040072#endif
Dave Barach908a5ea2017-07-14 12:42:21 -040073} BVT (clib_bihash_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -070074
Dave Barachc3799992016-08-15 11:12:27 -040075typedef struct
76{
77 BVT (clib_bihash_value) * values;
Dave Barach908a5ea2017-07-14 12:42:21 -040078 BVT (clib_bihash_bucket) * buckets;
Dave Barachc3799992016-08-15 11:12:27 -040079 volatile u32 *writer_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Dave Barachc3799992016-08-15 11:12:27 -040081 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -040082 int *working_copy_lengths;
Dave Barach908a5ea2017-07-14 12:42:21 -040083 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -070084
85 u32 nbuckets;
86 u32 log2_nbuckets;
Dave Barachc3799992016-08-15 11:12:27 -040087 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070088
Dave Barach908a5ea2017-07-14 12:42:21 -040089 u64 cache_hits;
90 u64 cache_misses;
91
Dave Barachc3799992016-08-15 11:12:27 -040092 BVT (clib_bihash_value) ** freelists;
Dave Barach97f5af02018-02-22 09:48:45 -050093
94 /*
Dave Barach30765e72018-02-23 07:45:36 -050095 * Backing store allocation. Since bihash manages its own
Dave Barach97f5af02018-02-22 09:48:45 -050096 * freelists, we simple dole out memory at alloc_arena_next.
97 */
98 uword alloc_arena;
99 uword alloc_arena_next;
100 uword alloc_arena_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800102 /**
103 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
104 */
105 format_function_t *fmt_fn;
106
Dave Barachc3799992016-08-15 11:12:27 -0400107} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700108
109
Dave Barach908a5ea2017-07-14 12:42:21 -0400110static inline void
111BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
112{
Dave Barache7d212f2018-02-07 13:14:06 -0500113#if BIHASH_KVP_CACHE_SIZE > 1
Dave Barach908a5ea2017-07-14 12:42:21 -0400114 u16 value, tmp, mask;
115 u8 found_lru_pos;
116 u16 save_hi;
117
Dave Barach908a5ea2017-07-14 12:42:21 -0400118 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
119
120 /* First, find the slot in cache_lru */
121 mask = slot;
122 if (BIHASH_KVP_CACHE_SIZE > 1)
123 mask |= slot << 3;
124 if (BIHASH_KVP_CACHE_SIZE > 2)
125 mask |= slot << 6;
126 if (BIHASH_KVP_CACHE_SIZE > 3)
127 mask |= slot << 9;
128 if (BIHASH_KVP_CACHE_SIZE > 4)
129 mask |= slot << 12;
130
131 value = b->cache_lru;
132 tmp = value ^ mask;
133
134 /* Already the most-recently used? */
135 if ((tmp & 7) == 0)
136 return;
137
138 found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
139 if (BIHASH_KVP_CACHE_SIZE > 2)
140 found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
141 if (BIHASH_KVP_CACHE_SIZE > 3)
142 found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
143 if (BIHASH_KVP_CACHE_SIZE > 4)
144 found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
145
146 ASSERT (found_lru_pos);
147
148 /* create a mask to kill bits in or above slot */
149 mask = 0xFFFF << found_lru_pos;
150 mask <<= found_lru_pos;
151 mask <<= found_lru_pos;
152 mask ^= 0xFFFF;
153 tmp = value & mask;
154
155 /* Save bits above slot */
156 mask ^= 0xFFFF;
157 mask <<= 3;
158 save_hi = value & mask;
159
160 value = save_hi | (tmp << 3) | slot;
161
162 b->cache_lru = value;
Dave Barache7d212f2018-02-07 13:14:06 -0500163#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400164}
165
166void
167BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
168 u8 slot);
169
170static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
171{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400172#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400173 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400174#else
175 return 0;
176#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400177}
178
179static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
180{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400181#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400182 u16 initial_lru_value;
183
184 memset (b->cache, 0xff, sizeof (b->cache));
185
186 /*
187 * We'll want the cache to be loaded from slot 0 -> slot N, so
188 * the initial LRU order is reverse index order.
189 */
190 if (BIHASH_KVP_CACHE_SIZE == 1)
191 initial_lru_value = 0;
192 else if (BIHASH_KVP_CACHE_SIZE == 2)
193 initial_lru_value = (0 << 3) | (1 << 0);
194 else if (BIHASH_KVP_CACHE_SIZE == 3)
195 initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
196 else if (BIHASH_KVP_CACHE_SIZE == 4)
197 initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
198 else if (BIHASH_KVP_CACHE_SIZE == 5)
199 initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
200
201 b->cache_lru = initial_lru_value;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400202#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400203}
204
Dave Barach858c06f2017-07-21 10:44:27 -0400205static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400206{
Dave Barache7d212f2018-02-07 13:14:06 -0500207#if BIHASH_KVP_CACHE_SIZE > 0
208 u16 cache_lru_bit;
209 u16 rv;
Dave Barach858c06f2017-07-21 10:44:27 -0400210
Dave Barache7d212f2018-02-07 13:14:06 -0500211 cache_lru_bit = 1 << 15;
Dave Barach858c06f2017-07-21 10:44:27 -0400212
Dave Barache7d212f2018-02-07 13:14:06 -0500213 rv = __sync_fetch_and_or (&b->cache_lru, cache_lru_bit);
Dave Barach858c06f2017-07-21 10:44:27 -0400214 /* Was already locked? */
Dave Barache7d212f2018-02-07 13:14:06 -0500215 if (rv & (1 << 15))
Dave Barach858c06f2017-07-21 10:44:27 -0400216 return 0;
Dave Barache7d212f2018-02-07 13:14:06 -0500217#endif
Dave Barach858c06f2017-07-21 10:44:27 -0400218 return 1;
219}
220
221static inline void BV (clib_bihash_unlock_bucket)
222 (BVT (clib_bihash_bucket) * b)
223{
Dave Barache7d212f2018-02-07 13:14:06 -0500224#if BIHASH_KVP_CACHE_SIZE > 0
225 u16 cache_lru;
Dave Barach858c06f2017-07-21 10:44:27 -0400226
Dave Barache7d212f2018-02-07 13:14:06 -0500227 cache_lru = b->cache_lru & ~(1 << 15);
228 b->cache_lru = cache_lru;
229#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400230}
231
232static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400233 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234{
Dave Barach97f5af02018-02-22 09:48:45 -0500235 u8 *hp = (u8 *) h->alloc_arena;
Dave Barachc3799992016-08-15 11:12:27 -0400236 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
238 return (void *) vp;
239}
240
Dave Barach908a5ea2017-07-14 12:42:21 -0400241static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400242 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243{
Dave Barachc3799992016-08-15 11:12:27 -0400244 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Dave Barach97f5af02018-02-22 09:48:45 -0500246 hp = (u8 *) h->alloc_arena;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247 vp = (u8 *) v;
248
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 return vp - hp;
250}
251
Dave Barachc3799992016-08-15 11:12:27 -0400252void BV (clib_bihash_init)
253 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800255void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
256 format_function_t * fmt_fn);
257
Dave Barachc3799992016-08-15 11:12:27 -0400258void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Dave Barachc3799992016-08-15 11:12:27 -0400260int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
261 BVT (clib_bihash_kv) * add_v, int is_add);
Dave Barach908a5ea2017-07-14 12:42:21 -0400262int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400263 BVT (clib_bihash_kv) * search_v,
264 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Dave Barachc3799992016-08-15 11:12:27 -0400266void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
267 void *callback, void *arg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
Dave Barachc3799992016-08-15 11:12:27 -0400269format_function_t BV (format_bihash);
270format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400271format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272
Dave Barach30765e72018-02-23 07:45:36 -0500273static inline int BV (clib_bihash_search_inline_with_hash)
274 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400277 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400278 BVT (clib_bihash_bucket) * b;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400279#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400280 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400281#endif
Dave Barach5e6b9582016-12-12 15:37:29 -0500282 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Dave Barachc3799992016-08-15 11:12:27 -0400284 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285 b = &h->buckets[bucket_index];
286
287 if (b->offset == 0)
288 return -1;
289
Dave Barachc8ef08a2017-08-31 05:58:22 -0400290#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach858c06f2017-07-21 10:44:27 -0400291 /* Check the cache, if not currently locked */
292 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400293 {
294 limit = BIHASH_KVP_CACHE_SIZE;
295 kvp = b->cache;
296 for (i = 0; i < limit; i++)
297 {
298 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
299 {
300 *key_result = kvp[i];
301 h->cache_hits++;
302 return 0;
303 }
304 }
305 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400306#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400307
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 hash >>= h->log2_nbuckets;
309
Dave Barachc3799992016-08-15 11:12:27 -0400310 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400311
Dave Barach5e6b9582016-12-12 15:37:29 -0500312 /* If the bucket has unresolvable collisions, use linear search */
313 limit = BIHASH_KVP_PER_PAGE;
314 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
315 if (PREDICT_FALSE (b->linear_search))
316 limit <<= b->log2_pages;
317
318 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400320 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400321 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400322 *key_result = v->kvp[i];
323
Dave Barachc8ef08a2017-08-31 05:58:22 -0400324#if BIHASH_KVP_CACHE_SIZE > 0
325 u8 cache_slot;
Dave Barach858c06f2017-07-21 10:44:27 -0400326 /* Try to lock the bucket */
327 if (BV (clib_bihash_lock_bucket) (b))
328 {
329 cache_slot = BV (clib_bihash_get_lru) (b);
330 b->cache[cache_slot] = v->kvp[i];
331 BV (clib_bihash_update_lru) (b, cache_slot);
Dave Barach908a5ea2017-07-14 12:42:21 -0400332
Dave Barach858c06f2017-07-21 10:44:27 -0400333 /* Unlock the bucket */
334 BV (clib_bihash_unlock_bucket) (b);
335 h->cache_misses++;
336 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400337#endif
Dave Barachc3799992016-08-15 11:12:27 -0400338 return 0;
339 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 }
341 return -1;
342}
343
Dave Barach30765e72018-02-23 07:45:36 -0500344static inline int BV (clib_bihash_search_inline)
345 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
346{
347 u64 hash;
348
349 hash = BV (clib_bihash_hash) (key_result);
350
351 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
352}
353
354static inline void BV (clib_bihash_prefetch_bucket)
355 (BVT (clib_bihash) * h, u64 hash)
356{
357 u32 bucket_index;
358 BVT (clib_bihash_bucket) * b;
359
360 bucket_index = hash & (h->nbuckets - 1);
361 b = &h->buckets[bucket_index];
362
363 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
364}
365
366static inline void BV (clib_bihash_prefetch_data)
367 (BVT (clib_bihash) * h, u64 hash)
368{
369 u32 bucket_index;
370 BVT (clib_bihash_value) * v;
371 BVT (clib_bihash_bucket) * b;
372
373 bucket_index = hash & (h->nbuckets - 1);
374 b = &h->buckets[bucket_index];
375
376 if (PREDICT_FALSE (b->offset == 0))
377 return;
378
379 hash >>= h->log2_nbuckets;
380 v = BV (clib_bihash_get_value) (h, b->offset);
381
382 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
383
384 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
385}
386
Dave Barachc3799992016-08-15 11:12:27 -0400387static inline int BV (clib_bihash_search_inline_2)
Dave Barach908a5ea2017-07-14 12:42:21 -0400388 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400389 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700390{
391 u64 hash;
392 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400393 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400394 BVT (clib_bihash_bucket) * b;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400395#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400396 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400397#endif
Dave Barach5e6b9582016-12-12 15:37:29 -0500398 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399
Dave Barachc3799992016-08-15 11:12:27 -0400400 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700401
Dave Barachc3799992016-08-15 11:12:27 -0400402 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403
Dave Barachc3799992016-08-15 11:12:27 -0400404 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 b = &h->buckets[bucket_index];
406
407 if (b->offset == 0)
408 return -1;
409
Dave Barach858c06f2017-07-21 10:44:27 -0400410 /* Check the cache, if currently unlocked */
Dave Barachc8ef08a2017-08-31 05:58:22 -0400411#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach858c06f2017-07-21 10:44:27 -0400412 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400413 {
414 limit = BIHASH_KVP_CACHE_SIZE;
415 kvp = b->cache;
416 for (i = 0; i < limit; i++)
417 {
418 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
419 {
420 *valuep = kvp[i];
421 h->cache_hits++;
422 return 0;
423 }
424 }
425 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400426#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400427
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 hash >>= h->log2_nbuckets;
Dave Barachc3799992016-08-15 11:12:27 -0400429 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400430
Dave Barach5e6b9582016-12-12 15:37:29 -0500431 /* If the bucket has unresolvable collisions, use linear search */
432 limit = BIHASH_KVP_PER_PAGE;
433 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
434 if (PREDICT_FALSE (b->linear_search))
435 limit <<= b->log2_pages;
436
437 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438 {
Dave Barachc3799992016-08-15 11:12:27 -0400439 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
440 {
441 *valuep = v->kvp[i];
Dave Barach908a5ea2017-07-14 12:42:21 -0400442
Dave Barachc8ef08a2017-08-31 05:58:22 -0400443#if BIHASH_KVP_CACHE_SIZE > 0
444 u8 cache_slot;
445
Dave Barach858c06f2017-07-21 10:44:27 -0400446 /* Try to lock the bucket */
447 if (BV (clib_bihash_lock_bucket) (b))
448 {
449 cache_slot = BV (clib_bihash_get_lru) (b);
450 b->cache[cache_slot] = v->kvp[i];
451 BV (clib_bihash_update_lru) (b, cache_slot);
Dave Barach908a5ea2017-07-14 12:42:21 -0400452
Dave Barach858c06f2017-07-21 10:44:27 -0400453 /* Reenable the cache */
454 BV (clib_bihash_unlock_bucket) (b);
455 h->cache_misses++;
456 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400457#endif
Dave Barachc3799992016-08-15 11:12:27 -0400458 return 0;
459 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 }
461 return -1;
462}
463
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400465
Chris Luke16bcf7d2016-09-01 14:31:46 -0400466/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400467
468/*
469 * fd.io coding-style-patch-verification: ON
470 *
471 * Local Variables:
472 * eval: (c-set-style "gnu")
473 * End:
474 */