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>
31 #error BIHASH_TYPE not defined
35 #define __bv(a,b) _bv(a,b)
36 #define BV(a) __bv(a,BIHASH_TYPE)
38 #define _bvt(a,b) a##b##_t
39 #define __bvt(a,b) _bvt(a,b)
40 #define BVT(a) __bvt(a,BIHASH_TYPE)
42 typedef struct BV (clib_bihash_value)
46 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
47 struct BV (clib_bihash_value) * next_free;
49 } BVT (clib_bihash_value);
51 #if BIHASH_KVP_CACHE_SIZE > 5
52 #error Requested KVP cache LRU data exceeds 16 bits
68 #if BIHASH_KVP_CACHE_SIZE > 0
70 BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
72 } BVT (clib_bihash_bucket);
76 BVT (clib_bihash_value) * values;
77 BVT (clib_bihash_bucket) * buckets;
78 volatile u32 *writer_lock;
80 BVT (clib_bihash_value) ** working_copies;
81 int *working_copy_lengths;
82 BVT (clib_bihash_bucket) saved_bucket;
91 BVT (clib_bihash_value) ** freelists;
95 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
97 format_function_t *fmt_fn;
103 BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
105 #if BIHASH_KVP_CACHE_SIZE > 1
106 u16 value, tmp, mask;
110 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
112 /* First, find the slot in cache_lru */
114 if (BIHASH_KVP_CACHE_SIZE > 1)
116 if (BIHASH_KVP_CACHE_SIZE > 2)
118 if (BIHASH_KVP_CACHE_SIZE > 3)
120 if (BIHASH_KVP_CACHE_SIZE > 4)
123 value = b->cache_lru;
126 /* Already the most-recently used? */
130 found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
131 if (BIHASH_KVP_CACHE_SIZE > 2)
132 found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
133 if (BIHASH_KVP_CACHE_SIZE > 3)
134 found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
135 if (BIHASH_KVP_CACHE_SIZE > 4)
136 found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
138 ASSERT (found_lru_pos);
140 /* create a mask to kill bits in or above slot */
141 mask = 0xFFFF << found_lru_pos;
142 mask <<= found_lru_pos;
143 mask <<= found_lru_pos;
147 /* Save bits above slot */
150 save_hi = value & mask;
152 value = save_hi | (tmp << 3) | slot;
154 b->cache_lru = value;
159 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
162 static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
164 #if BIHASH_KVP_CACHE_SIZE > 0
165 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
171 static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
173 #if BIHASH_KVP_CACHE_SIZE > 0
174 u16 initial_lru_value;
176 memset (b->cache, 0xff, sizeof (b->cache));
179 * We'll want the cache to be loaded from slot 0 -> slot N, so
180 * the initial LRU order is reverse index order.
182 if (BIHASH_KVP_CACHE_SIZE == 1)
183 initial_lru_value = 0;
184 else if (BIHASH_KVP_CACHE_SIZE == 2)
185 initial_lru_value = (0 << 3) | (1 << 0);
186 else if (BIHASH_KVP_CACHE_SIZE == 3)
187 initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
188 else if (BIHASH_KVP_CACHE_SIZE == 4)
189 initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
190 else if (BIHASH_KVP_CACHE_SIZE == 5)
191 initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
193 b->cache_lru = initial_lru_value;
197 static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
199 #if BIHASH_KVP_CACHE_SIZE > 0
203 cache_lru_bit = 1 << 15;
205 rv = __sync_fetch_and_or (&b->cache_lru, cache_lru_bit);
206 /* Was already locked? */
213 static inline void BV (clib_bihash_unlock_bucket)
214 (BVT (clib_bihash_bucket) * b)
216 #if BIHASH_KVP_CACHE_SIZE > 0
219 cache_lru = b->cache_lru & ~(1 << 15);
220 b->cache_lru = cache_lru;
224 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
228 u8 *vp = hp + offset;
233 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
238 hp = (u8 *) h->mheap;
241 ASSERT ((vp - hp) < 0x100000000ULL);
245 void BV (clib_bihash_init)
246 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
248 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
249 format_function_t * fmt_fn);
251 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
253 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
254 BVT (clib_bihash_kv) * add_v, int is_add);
255 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
256 BVT (clib_bihash_kv) * search_v,
257 BVT (clib_bihash_kv) * return_v);
259 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
260 void *callback, void *arg);
262 format_function_t BV (format_bihash);
263 format_function_t BV (format_bihash_kvp);
264 format_function_t BV (format_bihash_lru);
266 static inline int BV (clib_bihash_search_inline)
267 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
271 BVT (clib_bihash_value) * v;
272 BVT (clib_bihash_bucket) * b;
273 #if BIHASH_KVP_CACHE_SIZE > 0
274 BVT (clib_bihash_kv) * kvp;
278 hash = BV (clib_bihash_hash) (key_result);
280 bucket_index = hash & (h->nbuckets - 1);
281 b = &h->buckets[bucket_index];
286 #if BIHASH_KVP_CACHE_SIZE > 0
287 /* Check the cache, if not currently locked */
288 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
290 limit = BIHASH_KVP_CACHE_SIZE;
292 for (i = 0; i < limit; i++)
294 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
296 *key_result = kvp[i];
304 hash >>= h->log2_nbuckets;
306 v = BV (clib_bihash_get_value) (h, b->offset);
308 /* If the bucket has unresolvable collisions, use linear search */
309 limit = BIHASH_KVP_PER_PAGE;
310 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
311 if (PREDICT_FALSE (b->linear_search))
312 limit <<= b->log2_pages;
314 for (i = 0; i < limit; i++)
316 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
318 *key_result = v->kvp[i];
320 #if BIHASH_KVP_CACHE_SIZE > 0
322 /* Try to lock the bucket */
323 if (BV (clib_bihash_lock_bucket) (b))
325 cache_slot = BV (clib_bihash_get_lru) (b);
326 b->cache[cache_slot] = v->kvp[i];
327 BV (clib_bihash_update_lru) (b, cache_slot);
329 /* Unlock the bucket */
330 BV (clib_bihash_unlock_bucket) (b);
340 static inline int BV (clib_bihash_search_inline_2)
341 (BVT (clib_bihash) * h,
342 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
346 BVT (clib_bihash_value) * v;
347 BVT (clib_bihash_bucket) * b;
348 #if BIHASH_KVP_CACHE_SIZE > 0
349 BVT (clib_bihash_kv) * kvp;
355 hash = BV (clib_bihash_hash) (search_key);
357 bucket_index = hash & (h->nbuckets - 1);
358 b = &h->buckets[bucket_index];
363 /* Check the cache, if currently unlocked */
364 #if BIHASH_KVP_CACHE_SIZE > 0
365 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
367 limit = BIHASH_KVP_CACHE_SIZE;
369 for (i = 0; i < limit; i++)
371 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
381 hash >>= h->log2_nbuckets;
382 v = BV (clib_bihash_get_value) (h, b->offset);
384 /* If the bucket has unresolvable collisions, use linear search */
385 limit = BIHASH_KVP_PER_PAGE;
386 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
387 if (PREDICT_FALSE (b->linear_search))
388 limit <<= b->log2_pages;
390 for (i = 0; i < limit; i++)
392 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
396 #if BIHASH_KVP_CACHE_SIZE > 0
399 /* Try to lock the bucket */
400 if (BV (clib_bihash_lock_bucket) (b))
402 cache_slot = BV (clib_bihash_get_lru) (b);
403 b->cache[cache_slot] = v->kvp[i];
404 BV (clib_bihash_update_lru) (b, cache_slot);
406 /* Reenable the cache */
407 BV (clib_bihash_unlock_bucket) (b);
417 #endif /* __included_bihash_template_h__ */
422 * fd.io coding-style-patch-verification: ON
425 * eval: (c-set-style "gnu")