blob: 3d9c59cb933d327c2c25515cf1b7bebe807efd99 [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>
29
30#ifndef BIHASH_TYPE
31#error BIHASH_TYPE not defined
32#endif
33
34#define _bv(a,b) a##b
35#define __bv(a,b) _bv(a,b)
36#define BV(a) __bv(a,BIHASH_TYPE)
37
38#define _bvt(a,b) a##b##_t
39#define __bvt(a,b) _bvt(a,b)
40#define BVT(a) __bvt(a,BIHASH_TYPE)
41
Dave Barachc3799992016-08-15 11:12:27 -040042typedef struct BV (clib_bihash_value)
43{
44 union
45 {
46 BVT (clib_bihash_kv) kvp[BIHASH_KVP_PER_PAGE];
47 struct BV (clib_bihash_value) * next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048 };
Dave Barachc3799992016-08-15 11:12:27 -040049} BVT (clib_bihash_value);
Ed Warnickecb9cada2015-12-08 15:45:58 -070050
Dave Barach908a5ea2017-07-14 12:42:21 -040051#if BIHASH_KVP_CACHE_SIZE > 5
52#error Requested KVP cache LRU data exceeds 16 bits
53#endif
54
Dave Barachc3799992016-08-15 11:12:27 -040055typedef struct
56{
57 union
58 {
59 struct
60 {
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 u32 offset;
Dave Barach5e6b9582016-12-12 15:37:29 -050062 u8 linear_search;
Ed Warnickecb9cada2015-12-08 15:45:58 -070063 u8 log2_pages;
Dave Barach908a5ea2017-07-14 12:42:21 -040064 u16 cache_lru;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065 };
66 u64 as_u64;
67 };
Dave Barach908a5ea2017-07-14 12:42:21 -040068 BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
69} BVT (clib_bihash_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -070070
Dave Barachc3799992016-08-15 11:12:27 -040071typedef struct
72{
73 BVT (clib_bihash_value) * values;
Dave Barach908a5ea2017-07-14 12:42:21 -040074 BVT (clib_bihash_bucket) * buckets;
Dave Barachc3799992016-08-15 11:12:27 -040075 volatile u32 *writer_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -070076
Dave Barachc3799992016-08-15 11:12:27 -040077 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -040078 int *working_copy_lengths;
Dave Barach908a5ea2017-07-14 12:42:21 -040079 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
81 u32 nbuckets;
82 u32 log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -050083 u32 linear_buckets;
Dave Barachc3799992016-08-15 11:12:27 -040084 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
Dave Barach908a5ea2017-07-14 12:42:21 -040086 u64 cache_hits;
87 u64 cache_misses;
88
Dave Barachc3799992016-08-15 11:12:27 -040089 BVT (clib_bihash_value) ** freelists;
90 void *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070091
Dave Barachc3799992016-08-15 11:12:27 -040092} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
94
Dave Barach908a5ea2017-07-14 12:42:21 -040095static inline void
96BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
97{
98 u16 value, tmp, mask;
99 u8 found_lru_pos;
100 u16 save_hi;
101
102 if (BIHASH_KVP_CACHE_SIZE < 2)
103 return;
104
105 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
106
107 /* First, find the slot in cache_lru */
108 mask = slot;
109 if (BIHASH_KVP_CACHE_SIZE > 1)
110 mask |= slot << 3;
111 if (BIHASH_KVP_CACHE_SIZE > 2)
112 mask |= slot << 6;
113 if (BIHASH_KVP_CACHE_SIZE > 3)
114 mask |= slot << 9;
115 if (BIHASH_KVP_CACHE_SIZE > 4)
116 mask |= slot << 12;
117
118 value = b->cache_lru;
119 tmp = value ^ mask;
120
121 /* Already the most-recently used? */
122 if ((tmp & 7) == 0)
123 return;
124
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;
132
133 ASSERT (found_lru_pos);
134
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;
139 mask ^= 0xFFFF;
140 tmp = value & mask;
141
142 /* Save bits above slot */
143 mask ^= 0xFFFF;
144 mask <<= 3;
145 save_hi = value & mask;
146
147 value = save_hi | (tmp << 3) | slot;
148
149 b->cache_lru = value;
150}
151
152void
153BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
154 u8 slot);
155
156static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
157{
158 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
159}
160
161static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
162{
163 u16 initial_lru_value;
164
165 memset (b->cache, 0xff, sizeof (b->cache));
166
167 /*
168 * We'll want the cache to be loaded from slot 0 -> slot N, so
169 * the initial LRU order is reverse index order.
170 */
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);
181
182 b->cache_lru = initial_lru_value;
183}
184
Dave Barach858c06f2017-07-21 10:44:27 -0400185static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400186{
187 BVT (clib_bihash_bucket) tmp_b;
Dave Barach858c06f2017-07-21 10:44:27 -0400188 u64 rv;
189
190 tmp_b.as_u64 = 0;
191 tmp_b.cache_lru = 1 << 15;
192
193 rv = __sync_fetch_and_or (&b->as_u64, tmp_b.as_u64);
194 tmp_b.as_u64 = rv;
195 /* Was already locked? */
196 if (tmp_b.cache_lru & (1 << 15))
197 return 0;
198 return 1;
199}
200
201static inline void BV (clib_bihash_unlock_bucket)
202 (BVT (clib_bihash_bucket) * b)
203{
204 BVT (clib_bihash_bucket) tmp_b;
205
Dave Barach908a5ea2017-07-14 12:42:21 -0400206 tmp_b.as_u64 = b->as_u64;
Dave Barach858c06f2017-07-21 10:44:27 -0400207 tmp_b.cache_lru &= ~(1 << 15);
Dave Barach908a5ea2017-07-14 12:42:21 -0400208 b->as_u64 = tmp_b.as_u64;
209}
210
211static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400212 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213{
Dave Barachc3799992016-08-15 11:12:27 -0400214 u8 *hp = h->mheap;
215 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700216
217 return (void *) vp;
218}
219
Dave Barach908a5ea2017-07-14 12:42:21 -0400220static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400221 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222{
Dave Barachc3799992016-08-15 11:12:27 -0400223 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700224
225 hp = (u8 *) h->mheap;
226 vp = (u8 *) v;
227
Dave Barachc3799992016-08-15 11:12:27 -0400228 ASSERT ((vp - hp) < 0x100000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229 return vp - hp;
230}
231
Dave Barachc3799992016-08-15 11:12:27 -0400232void BV (clib_bihash_init)
233 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700234
Dave Barachc3799992016-08-15 11:12:27 -0400235void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700236
Dave Barachc3799992016-08-15 11:12:27 -0400237int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
238 BVT (clib_bihash_kv) * add_v, int is_add);
Dave Barach908a5ea2017-07-14 12:42:21 -0400239int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400240 BVT (clib_bihash_kv) * search_v,
241 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242
Dave Barachc3799992016-08-15 11:12:27 -0400243void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
244 void *callback, void *arg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245
Dave Barachc3799992016-08-15 11:12:27 -0400246format_function_t BV (format_bihash);
247format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400248format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249
Dave Barachc3799992016-08-15 11:12:27 -0400250static inline int BV (clib_bihash_search_inline)
Dave Barach908a5ea2017-07-14 12:42:21 -0400251 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252{
253 u64 hash;
254 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400255 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400256 BVT (clib_bihash_bucket) * b;
257 BVT (clib_bihash_kv) * kvp;
Dave Barach5e6b9582016-12-12 15:37:29 -0500258 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259
Dave Barach908a5ea2017-07-14 12:42:21 -0400260 hash = BV (clib_bihash_hash) (key_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Dave Barachc3799992016-08-15 11:12:27 -0400262 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700263 b = &h->buckets[bucket_index];
264
265 if (b->offset == 0)
266 return -1;
267
Dave Barach858c06f2017-07-21 10:44:27 -0400268 /* Check the cache, if not currently locked */
269 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400270 {
271 limit = BIHASH_KVP_CACHE_SIZE;
272 kvp = b->cache;
273 for (i = 0; i < limit; i++)
274 {
275 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
276 {
277 *key_result = kvp[i];
278 h->cache_hits++;
279 return 0;
280 }
281 }
282 }
283
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 hash >>= h->log2_nbuckets;
285
Dave Barachc3799992016-08-15 11:12:27 -0400286 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400287
Dave Barach5e6b9582016-12-12 15:37:29 -0500288 /* 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;
293
294 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700295 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400296 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400297 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400298 u8 cache_slot;
299 *key_result = v->kvp[i];
300
Dave Barach858c06f2017-07-21 10:44:27 -0400301 /* Try to lock the bucket */
302 if (BV (clib_bihash_lock_bucket) (b))
303 {
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);
Dave Barach908a5ea2017-07-14 12:42:21 -0400307
Dave Barach858c06f2017-07-21 10:44:27 -0400308 /* Unlock the bucket */
309 BV (clib_bihash_unlock_bucket) (b);
310 h->cache_misses++;
311 }
Dave Barachc3799992016-08-15 11:12:27 -0400312 return 0;
313 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 }
315 return -1;
316}
317
Dave Barachc3799992016-08-15 11:12:27 -0400318static inline int BV (clib_bihash_search_inline_2)
Dave Barach908a5ea2017-07-14 12:42:21 -0400319 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400320 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700321{
322 u64 hash;
323 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400324 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400325 BVT (clib_bihash_bucket) * b;
326 BVT (clib_bihash_kv) * kvp;
Dave Barach5e6b9582016-12-12 15:37:29 -0500327 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328
Dave Barachc3799992016-08-15 11:12:27 -0400329 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700330
Dave Barachc3799992016-08-15 11:12:27 -0400331 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700332
Dave Barachc3799992016-08-15 11:12:27 -0400333 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700334 b = &h->buckets[bucket_index];
335
336 if (b->offset == 0)
337 return -1;
338
Dave Barach858c06f2017-07-21 10:44:27 -0400339 /* Check the cache, if currently unlocked */
340 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400341 {
342 limit = BIHASH_KVP_CACHE_SIZE;
343 kvp = b->cache;
344 for (i = 0; i < limit; i++)
345 {
346 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
347 {
348 *valuep = kvp[i];
349 h->cache_hits++;
350 return 0;
351 }
352 }
353 }
354
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 hash >>= h->log2_nbuckets;
Dave Barachc3799992016-08-15 11:12:27 -0400356 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400357
Dave Barach5e6b9582016-12-12 15:37:29 -0500358 /* 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;
363
364 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700365 {
Dave Barachc3799992016-08-15 11:12:27 -0400366 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
367 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400368 u8 cache_slot;
Dave Barachc3799992016-08-15 11:12:27 -0400369 *valuep = v->kvp[i];
Dave Barach908a5ea2017-07-14 12:42:21 -0400370
Dave Barach858c06f2017-07-21 10:44:27 -0400371 /* Try to lock the bucket */
372 if (BV (clib_bihash_lock_bucket) (b))
373 {
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);
Dave Barach908a5ea2017-07-14 12:42:21 -0400377
Dave Barach858c06f2017-07-21 10:44:27 -0400378 /* Reenable the cache */
379 BV (clib_bihash_unlock_bucket) (b);
380 h->cache_misses++;
381 }
Dave Barachc3799992016-08-15 11:12:27 -0400382 return 0;
383 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700384 }
385 return -1;
386}
387
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400389
Chris Luke16bcf7d2016-09-01 14:31:46 -0400390/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400391
392/*
393 * fd.io coding-style-patch-verification: ON
394 *
395 * Local Variables:
396 * eval: (c-set-style "gnu")
397 * End:
398 */