2 Copyright (c) 2014 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
20 * Note: to instantiate the template multiple times in a single file,
21 * #undef __included_bihash_template_h__...
23 #ifndef __included_bihash_template_h__
24 #define __included_bihash_template_h__
26 #include <vppinfra/heap.h>
27 #include <vppinfra/format.h>
28 #include <vppinfra/pool.h>
29 #include <vppinfra/cache.h>
32 #error BIHASH_TYPE not defined
36 #define __bv(a,b) _bv(a,b)
37 #define BV(a) __bv(a,BIHASH_TYPE)
39 #define _bvt(a,b) a##b##_t
40 #define __bvt(a,b) _bvt(a,b)
41 #define BVT(a) __bvt(a,BIHASH_TYPE)
43 typedef struct BV (clib_bihash_value)
47 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
48 struct BV (clib_bihash_value) * next_free;
50 } BVT (clib_bihash_value);
52 #if BIHASH_KVP_CACHE_SIZE > 5
53 #error Requested KVP cache LRU data exceeds 16 bits
69 #if BIHASH_KVP_CACHE_SIZE > 0
71 BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
73 } BVT (clib_bihash_bucket);
75 #if BIHASH_KVP_CACHE_SIZE == 0
76 STATIC_ASSERT_SIZEOF (BVT (clib_bihash_bucket), sizeof (u64));
81 BVT (clib_bihash_value) * values;
82 BVT (clib_bihash_bucket) * buckets;
83 volatile u32 *writer_lock;
85 BVT (clib_bihash_value) ** working_copies;
86 int *working_copy_lengths;
87 BVT (clib_bihash_bucket) saved_bucket;
96 BVT (clib_bihash_value) ** freelists;
99 * Backing store allocation. Since bihash manages its own
100 * freelists, we simple dole out memory at alloc_arena_next.
103 uword alloc_arena_next;
104 uword alloc_arena_size;
107 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
109 format_function_t *fmt_fn;
115 BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
117 #if BIHASH_KVP_CACHE_SIZE > 1
118 u16 value, tmp, mask;
122 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
124 /* First, find the slot in cache_lru */
126 if (BIHASH_KVP_CACHE_SIZE > 1)
128 if (BIHASH_KVP_CACHE_SIZE > 2)
130 if (BIHASH_KVP_CACHE_SIZE > 3)
132 if (BIHASH_KVP_CACHE_SIZE > 4)
135 value = b->cache_lru;
138 /* Already the most-recently used? */
142 found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
143 if (BIHASH_KVP_CACHE_SIZE > 2)
144 found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
145 if (BIHASH_KVP_CACHE_SIZE > 3)
146 found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
147 if (BIHASH_KVP_CACHE_SIZE > 4)
148 found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
150 ASSERT (found_lru_pos);
152 /* create a mask to kill bits in or above slot */
153 mask = 0xFFFF << found_lru_pos;
154 mask <<= found_lru_pos;
155 mask <<= found_lru_pos;
159 /* Save bits above slot */
162 save_hi = value & mask;
164 value = save_hi | (tmp << 3) | slot;
166 b->cache_lru = value;
171 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
174 static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
176 #if BIHASH_KVP_CACHE_SIZE > 0
177 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
183 static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
185 #if BIHASH_KVP_CACHE_SIZE > 0
186 u16 initial_lru_value;
188 memset (b->cache, 0xff, sizeof (b->cache));
191 * We'll want the cache to be loaded from slot 0 -> slot N, so
192 * the initial LRU order is reverse index order.
194 if (BIHASH_KVP_CACHE_SIZE == 1)
195 initial_lru_value = 0;
196 else if (BIHASH_KVP_CACHE_SIZE == 2)
197 initial_lru_value = (0 << 3) | (1 << 0);
198 else if (BIHASH_KVP_CACHE_SIZE == 3)
199 initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
200 else if (BIHASH_KVP_CACHE_SIZE == 4)
201 initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
202 else if (BIHASH_KVP_CACHE_SIZE == 5)
203 initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
205 b->cache_lru = initial_lru_value;
209 static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
211 #if BIHASH_KVP_CACHE_SIZE > 0
215 cache_lru_bit = 1 << 15;
217 rv = __sync_fetch_and_or (&b->cache_lru, cache_lru_bit);
218 /* Was already locked? */
225 static inline void BV (clib_bihash_unlock_bucket)
226 (BVT (clib_bihash_bucket) * b)
228 #if BIHASH_KVP_CACHE_SIZE > 0
231 cache_lru = b->cache_lru & ~(1 << 15);
232 b->cache_lru = cache_lru;
236 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
239 u8 *hp = (u8 *) h->alloc_arena;
240 u8 *vp = hp + offset;
245 static inline int BV (clib_bihash_bucket_is_empty)
246 (BVT (clib_bihash_bucket) * b)
248 return b->as_u64 == 0;
251 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
256 hp = (u8 *) h->alloc_arena;
262 void BV (clib_bihash_init)
263 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
265 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
266 format_function_t * fmt_fn);
268 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
270 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
271 BVT (clib_bihash_kv) * add_v, int is_add);
272 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
273 BVT (clib_bihash_kv) * search_v,
274 BVT (clib_bihash_kv) * return_v);
276 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
277 void *callback, void *arg);
279 format_function_t BV (format_bihash);
280 format_function_t BV (format_bihash_kvp);
281 format_function_t BV (format_bihash_lru);
283 static inline int BV (clib_bihash_search_inline_with_hash)
284 (BVT (clib_bihash) * h, u64 hash, BVT (clib_bihash_kv) * key_result)
287 BVT (clib_bihash_value) * v;
288 BVT (clib_bihash_bucket) * b;
289 #if BIHASH_KVP_CACHE_SIZE > 0
290 BVT (clib_bihash_kv) * kvp;
294 bucket_index = hash & (h->nbuckets - 1);
295 b = &h->buckets[bucket_index];
297 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
300 #if BIHASH_KVP_CACHE_SIZE > 0
301 /* Check the cache, if not currently locked */
302 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
304 limit = BIHASH_KVP_CACHE_SIZE;
306 for (i = 0; i < limit; i++)
308 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
310 *key_result = kvp[i];
318 hash >>= h->log2_nbuckets;
320 v = BV (clib_bihash_get_value) (h, b->offset);
322 /* If the bucket has unresolvable collisions, use linear search */
323 limit = BIHASH_KVP_PER_PAGE;
324 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
325 if (PREDICT_FALSE (b->linear_search))
326 limit <<= b->log2_pages;
328 for (i = 0; i < limit; i++)
330 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
332 *key_result = v->kvp[i];
334 #if BIHASH_KVP_CACHE_SIZE > 0
336 /* Try to lock the bucket */
337 if (BV (clib_bihash_lock_bucket) (b))
339 cache_slot = BV (clib_bihash_get_lru) (b);
340 b->cache[cache_slot] = v->kvp[i];
341 BV (clib_bihash_update_lru) (b, cache_slot);
343 /* Unlock the bucket */
344 BV (clib_bihash_unlock_bucket) (b);
354 static inline int BV (clib_bihash_search_inline)
355 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
359 hash = BV (clib_bihash_hash) (key_result);
361 return BV (clib_bihash_search_inline_with_hash) (h, hash, key_result);
364 static inline void BV (clib_bihash_prefetch_bucket)
365 (BVT (clib_bihash) * h, u64 hash)
368 BVT (clib_bihash_bucket) * b;
370 bucket_index = hash & (h->nbuckets - 1);
371 b = &h->buckets[bucket_index];
373 CLIB_PREFETCH (b, CLIB_CACHE_LINE_BYTES, READ);
376 static inline void BV (clib_bihash_prefetch_data)
377 (BVT (clib_bihash) * h, u64 hash)
380 BVT (clib_bihash_value) * v;
381 BVT (clib_bihash_bucket) * b;
383 bucket_index = hash & (h->nbuckets - 1);
384 b = &h->buckets[bucket_index];
386 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
389 hash >>= h->log2_nbuckets;
390 v = BV (clib_bihash_get_value) (h, b->offset);
392 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
394 CLIB_PREFETCH (v, CLIB_CACHE_LINE_BYTES, READ);
397 static inline int BV (clib_bihash_search_inline_2_with_hash)
398 (BVT (clib_bihash) * h,
399 u64 hash, BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
402 BVT (clib_bihash_value) * v;
403 BVT (clib_bihash_bucket) * b;
404 #if BIHASH_KVP_CACHE_SIZE > 0
405 BVT (clib_bihash_kv) * kvp;
411 bucket_index = hash & (h->nbuckets - 1);
412 b = &h->buckets[bucket_index];
414 if (PREDICT_FALSE (BV (clib_bihash_bucket_is_empty) (b)))
417 /* Check the cache, if currently unlocked */
418 #if BIHASH_KVP_CACHE_SIZE > 0
419 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
421 limit = BIHASH_KVP_CACHE_SIZE;
423 for (i = 0; i < limit; i++)
425 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
435 hash >>= h->log2_nbuckets;
436 v = BV (clib_bihash_get_value) (h, b->offset);
438 /* If the bucket has unresolvable collisions, use linear search */
439 limit = BIHASH_KVP_PER_PAGE;
440 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
441 if (PREDICT_FALSE (b->linear_search))
442 limit <<= b->log2_pages;
444 for (i = 0; i < limit; i++)
446 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
450 #if BIHASH_KVP_CACHE_SIZE > 0
453 /* Try to lock the bucket */
454 if (BV (clib_bihash_lock_bucket) (b))
456 cache_slot = BV (clib_bihash_get_lru) (b);
457 b->cache[cache_slot] = v->kvp[i];
458 BV (clib_bihash_update_lru) (b, cache_slot);
460 /* Reenable the cache */
461 BV (clib_bihash_unlock_bucket) (b);
471 static inline int BV (clib_bihash_search_inline_2)
472 (BVT (clib_bihash) * h,
473 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
477 hash = BV (clib_bihash_hash) (search_key);
479 return BV (clib_bihash_search_inline_2_with_hash) (h, hash, search_key,
484 #endif /* __included_bihash_template_h__ */
489 * fd.io coding-style-patch-verification: ON
492 * eval: (c-set-style "gnu")