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;
94 * Backing store allocation. Since bihash mananges its own
95 * freelists, we simple dole out memory at alloc_arena_next.
98 uword alloc_arena_next;
99 uword alloc_arena_size;
102 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
104 format_function_t *fmt_fn;
110 BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
112 #if BIHASH_KVP_CACHE_SIZE > 1
113 u16 value, tmp, mask;
117 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
119 /* First, find the slot in cache_lru */
121 if (BIHASH_KVP_CACHE_SIZE > 1)
123 if (BIHASH_KVP_CACHE_SIZE > 2)
125 if (BIHASH_KVP_CACHE_SIZE > 3)
127 if (BIHASH_KVP_CACHE_SIZE > 4)
130 value = b->cache_lru;
133 /* Already the most-recently used? */
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;
145 ASSERT (found_lru_pos);
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;
154 /* Save bits above slot */
157 save_hi = value & mask;
159 value = save_hi | (tmp << 3) | slot;
161 b->cache_lru = value;
166 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
169 static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
171 #if BIHASH_KVP_CACHE_SIZE > 0
172 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
178 static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
180 #if BIHASH_KVP_CACHE_SIZE > 0
181 u16 initial_lru_value;
183 memset (b->cache, 0xff, sizeof (b->cache));
186 * We'll want the cache to be loaded from slot 0 -> slot N, so
187 * the initial LRU order is reverse index order.
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);
200 b->cache_lru = initial_lru_value;
204 static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
206 #if BIHASH_KVP_CACHE_SIZE > 0
210 cache_lru_bit = 1 << 15;
212 rv = __sync_fetch_and_or (&b->cache_lru, cache_lru_bit);
213 /* Was already locked? */
220 static inline void BV (clib_bihash_unlock_bucket)
221 (BVT (clib_bihash_bucket) * b)
223 #if BIHASH_KVP_CACHE_SIZE > 0
226 cache_lru = b->cache_lru & ~(1 << 15);
227 b->cache_lru = cache_lru;
231 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
234 u8 *hp = (u8 *) h->alloc_arena;
235 u8 *vp = hp + offset;
240 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
245 hp = (u8 *) h->alloc_arena;
251 void BV (clib_bihash_init)
252 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
254 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
255 format_function_t * fmt_fn);
257 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
259 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
260 BVT (clib_bihash_kv) * add_v, int is_add);
261 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
262 BVT (clib_bihash_kv) * search_v,
263 BVT (clib_bihash_kv) * return_v);
265 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
266 void *callback, void *arg);
268 format_function_t BV (format_bihash);
269 format_function_t BV (format_bihash_kvp);
270 format_function_t BV (format_bihash_lru);
272 static inline int BV (clib_bihash_search_inline)
273 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
277 BVT (clib_bihash_value) * v;
278 BVT (clib_bihash_bucket) * b;
279 #if BIHASH_KVP_CACHE_SIZE > 0
280 BVT (clib_bihash_kv) * kvp;
284 hash = BV (clib_bihash_hash) (key_result);
286 bucket_index = hash & (h->nbuckets - 1);
287 b = &h->buckets[bucket_index];
292 #if BIHASH_KVP_CACHE_SIZE > 0
293 /* Check the cache, if not currently locked */
294 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
296 limit = BIHASH_KVP_CACHE_SIZE;
298 for (i = 0; i < limit; i++)
300 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
302 *key_result = kvp[i];
310 hash >>= h->log2_nbuckets;
312 v = BV (clib_bihash_get_value) (h, b->offset);
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;
320 for (i = 0; i < limit; i++)
322 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
324 *key_result = v->kvp[i];
326 #if BIHASH_KVP_CACHE_SIZE > 0
328 /* Try to lock the bucket */
329 if (BV (clib_bihash_lock_bucket) (b))
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);
335 /* Unlock the bucket */
336 BV (clib_bihash_unlock_bucket) (b);
346 static inline int BV (clib_bihash_search_inline_2)
347 (BVT (clib_bihash) * h,
348 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
352 BVT (clib_bihash_value) * v;
353 BVT (clib_bihash_bucket) * b;
354 #if BIHASH_KVP_CACHE_SIZE > 0
355 BVT (clib_bihash_kv) * kvp;
361 hash = BV (clib_bihash_hash) (search_key);
363 bucket_index = hash & (h->nbuckets - 1);
364 b = &h->buckets[bucket_index];
369 /* Check the cache, if currently unlocked */
370 #if BIHASH_KVP_CACHE_SIZE > 0
371 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
373 limit = BIHASH_KVP_CACHE_SIZE;
375 for (i = 0; i < limit; i++)
377 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
387 hash >>= h->log2_nbuckets;
388 v = BV (clib_bihash_get_value) (h, b->offset);
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;
396 for (i = 0; i < limit; i++)
398 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
402 #if BIHASH_KVP_CACHE_SIZE > 0
405 /* Try to lock the bucket */
406 if (BV (clib_bihash_lock_bucket) (b))
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);
412 /* Reenable the cache */
413 BV (clib_bihash_unlock_bucket) (b);
423 #endif /* __included_bihash_template_h__ */
428 * fd.io coding-style-patch-verification: ON
431 * eval: (c-set-style "gnu")