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 BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
69 } BVT (clib_bihash_bucket);
73 BVT (clib_bihash_value) * values;
74 BVT (clib_bihash_bucket) * buckets;
75 volatile u32 *writer_lock;
77 BVT (clib_bihash_value) ** working_copies;
78 int *working_copy_lengths;
79 BVT (clib_bihash_bucket) saved_bucket;
89 BVT (clib_bihash_value) ** freelists;
96 BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
102 if (BIHASH_KVP_CACHE_SIZE < 2)
105 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
107 /* First, find the slot in cache_lru */
109 if (BIHASH_KVP_CACHE_SIZE > 1)
111 if (BIHASH_KVP_CACHE_SIZE > 2)
113 if (BIHASH_KVP_CACHE_SIZE > 3)
115 if (BIHASH_KVP_CACHE_SIZE > 4)
118 value = b->cache_lru;
121 /* Already the most-recently used? */
125 found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
126 if (BIHASH_KVP_CACHE_SIZE > 2)
127 found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
128 if (BIHASH_KVP_CACHE_SIZE > 3)
129 found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
130 if (BIHASH_KVP_CACHE_SIZE > 4)
131 found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
133 ASSERT (found_lru_pos);
135 /* create a mask to kill bits in or above slot */
136 mask = 0xFFFF << found_lru_pos;
137 mask <<= found_lru_pos;
138 mask <<= found_lru_pos;
142 /* Save bits above slot */
145 save_hi = value & mask;
147 value = save_hi | (tmp << 3) | slot;
149 b->cache_lru = value;
153 BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
156 static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
158 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
161 static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
163 u16 initial_lru_value;
165 memset (b->cache, 0xff, sizeof (b->cache));
168 * We'll want the cache to be loaded from slot 0 -> slot N, so
169 * the initial LRU order is reverse index order.
171 if (BIHASH_KVP_CACHE_SIZE == 1)
172 initial_lru_value = 0;
173 else if (BIHASH_KVP_CACHE_SIZE == 2)
174 initial_lru_value = (0 << 3) | (1 << 0);
175 else if (BIHASH_KVP_CACHE_SIZE == 3)
176 initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
177 else if (BIHASH_KVP_CACHE_SIZE == 4)
178 initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
179 else if (BIHASH_KVP_CACHE_SIZE == 5)
180 initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
182 b->cache_lru = initial_lru_value;
185 static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
187 BVT (clib_bihash_bucket) tmp_b;
191 tmp_b.cache_lru = 1 << 15;
193 rv = __sync_fetch_and_or (&b->as_u64, tmp_b.as_u64);
195 /* Was already locked? */
196 if (tmp_b.cache_lru & (1 << 15))
201 static inline void BV (clib_bihash_unlock_bucket)
202 (BVT (clib_bihash_bucket) * b)
204 BVT (clib_bihash_bucket) tmp_b;
206 tmp_b.as_u64 = b->as_u64;
207 tmp_b.cache_lru &= ~(1 << 15);
208 b->as_u64 = tmp_b.as_u64;
211 static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
215 u8 *vp = hp + offset;
220 static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
225 hp = (u8 *) h->mheap;
228 ASSERT ((vp - hp) < 0x100000000ULL);
232 void BV (clib_bihash_init)
233 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
235 void BV (clib_bihash_free) (BVT (clib_bihash) * h);
237 int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
238 BVT (clib_bihash_kv) * add_v, int is_add);
239 int BV (clib_bihash_search) (BVT (clib_bihash) * h,
240 BVT (clib_bihash_kv) * search_v,
241 BVT (clib_bihash_kv) * return_v);
243 void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
244 void *callback, void *arg);
246 format_function_t BV (format_bihash);
247 format_function_t BV (format_bihash_kvp);
248 format_function_t BV (format_bihash_lru);
250 static inline int BV (clib_bihash_search_inline)
251 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
255 BVT (clib_bihash_value) * v;
256 BVT (clib_bihash_bucket) * b;
257 BVT (clib_bihash_kv) * kvp;
260 hash = BV (clib_bihash_hash) (key_result);
262 bucket_index = hash & (h->nbuckets - 1);
263 b = &h->buckets[bucket_index];
268 /* Check the cache, if not currently locked */
269 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
271 limit = BIHASH_KVP_CACHE_SIZE;
273 for (i = 0; i < limit; i++)
275 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
277 *key_result = kvp[i];
284 hash >>= h->log2_nbuckets;
286 v = BV (clib_bihash_get_value) (h, b->offset);
288 /* If the bucket has unresolvable collisions, use linear search */
289 limit = BIHASH_KVP_PER_PAGE;
290 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
291 if (PREDICT_FALSE (b->linear_search))
292 limit <<= b->log2_pages;
294 for (i = 0; i < limit; i++)
296 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
299 *key_result = v->kvp[i];
301 /* Try to lock the bucket */
302 if (BV (clib_bihash_lock_bucket) (b))
304 cache_slot = BV (clib_bihash_get_lru) (b);
305 b->cache[cache_slot] = v->kvp[i];
306 BV (clib_bihash_update_lru) (b, cache_slot);
308 /* Unlock the bucket */
309 BV (clib_bihash_unlock_bucket) (b);
318 static inline int BV (clib_bihash_search_inline_2)
319 (BVT (clib_bihash) * h,
320 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
324 BVT (clib_bihash_value) * v;
325 BVT (clib_bihash_bucket) * b;
326 BVT (clib_bihash_kv) * kvp;
331 hash = BV (clib_bihash_hash) (search_key);
333 bucket_index = hash & (h->nbuckets - 1);
334 b = &h->buckets[bucket_index];
339 /* Check the cache, if currently unlocked */
340 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
342 limit = BIHASH_KVP_CACHE_SIZE;
344 for (i = 0; i < limit; i++)
346 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
355 hash >>= h->log2_nbuckets;
356 v = BV (clib_bihash_get_value) (h, b->offset);
358 /* If the bucket has unresolvable collisions, use linear search */
359 limit = BIHASH_KVP_PER_PAGE;
360 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
361 if (PREDICT_FALSE (b->linear_search))
362 limit <<= b->log2_pages;
364 for (i = 0; i < limit; i++)
366 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
371 /* Try to lock the bucket */
372 if (BV (clib_bihash_lock_bucket) (b))
374 cache_slot = BV (clib_bihash_get_lru) (b);
375 b->cache[cache_slot] = v->kvp[i];
376 BV (clib_bihash_update_lru) (b, cache_slot);
378 /* Reenable the cache */
379 BV (clib_bihash_unlock_bucket) (b);
388 #endif /* __included_bihash_template_h__ */
393 * fd.io coding-style-patch-verification: ON
396 * eval: (c-set-style "gnu")