blob: 9df418b5d7d02e9a2370b6bf05f445aa80ff080f [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 Barachc8ef08a2017-08-31 05:58:22 -040068#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -040069 BVT (clib_bihash_kv) cache[BIHASH_KVP_CACHE_SIZE];
Dave Barachc8ef08a2017-08-31 05:58:22 -040070#endif
Dave Barach908a5ea2017-07-14 12:42:21 -040071} BVT (clib_bihash_bucket);
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Dave Barachc3799992016-08-15 11:12:27 -040073typedef struct
74{
75 BVT (clib_bihash_value) * values;
Dave Barach908a5ea2017-07-14 12:42:21 -040076 BVT (clib_bihash_bucket) * buckets;
Dave Barachc3799992016-08-15 11:12:27 -040077 volatile u32 *writer_lock;
Ed Warnickecb9cada2015-12-08 15:45:58 -070078
Dave Barachc3799992016-08-15 11:12:27 -040079 BVT (clib_bihash_value) ** working_copies;
Dave Barachba7ddfe2017-05-17 20:20:50 -040080 int *working_copy_lengths;
Dave Barach908a5ea2017-07-14 12:42:21 -040081 BVT (clib_bihash_bucket) saved_bucket;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
83 u32 nbuckets;
84 u32 log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -050085 u32 linear_buckets;
Dave Barachc3799992016-08-15 11:12:27 -040086 u8 *name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087
Dave Barach908a5ea2017-07-14 12:42:21 -040088 u64 cache_hits;
89 u64 cache_misses;
90
Dave Barachc3799992016-08-15 11:12:27 -040091 BVT (clib_bihash_value) ** freelists;
92 void *mheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070093
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -080094 /**
95 * A custom format function to print the Key and Value of bihash_key instead of default hexdump
96 */
97 format_function_t *fmt_fn;
98
Dave Barachc3799992016-08-15 11:12:27 -040099} BVT (clib_bihash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101
Dave Barach908a5ea2017-07-14 12:42:21 -0400102static inline void
103BV (clib_bihash_update_lru) (BVT (clib_bihash_bucket) * b, u8 slot)
104{
105 u16 value, tmp, mask;
106 u8 found_lru_pos;
107 u16 save_hi;
108
109 if (BIHASH_KVP_CACHE_SIZE < 2)
110 return;
111
112 ASSERT (slot < BIHASH_KVP_CACHE_SIZE);
113
114 /* First, find the slot in cache_lru */
115 mask = slot;
116 if (BIHASH_KVP_CACHE_SIZE > 1)
117 mask |= slot << 3;
118 if (BIHASH_KVP_CACHE_SIZE > 2)
119 mask |= slot << 6;
120 if (BIHASH_KVP_CACHE_SIZE > 3)
121 mask |= slot << 9;
122 if (BIHASH_KVP_CACHE_SIZE > 4)
123 mask |= slot << 12;
124
125 value = b->cache_lru;
126 tmp = value ^ mask;
127
128 /* Already the most-recently used? */
129 if ((tmp & 7) == 0)
130 return;
131
132 found_lru_pos = ((tmp & (7 << 3)) == 0) ? 1 : 0;
133 if (BIHASH_KVP_CACHE_SIZE > 2)
134 found_lru_pos = ((tmp & (7 << 6)) == 0) ? 2 : found_lru_pos;
135 if (BIHASH_KVP_CACHE_SIZE > 3)
136 found_lru_pos = ((tmp & (7 << 9)) == 0) ? 3 : found_lru_pos;
137 if (BIHASH_KVP_CACHE_SIZE > 4)
138 found_lru_pos = ((tmp & (7 << 12)) == 0) ? 4 : found_lru_pos;
139
140 ASSERT (found_lru_pos);
141
142 /* create a mask to kill bits in or above slot */
143 mask = 0xFFFF << found_lru_pos;
144 mask <<= found_lru_pos;
145 mask <<= found_lru_pos;
146 mask ^= 0xFFFF;
147 tmp = value & mask;
148
149 /* Save bits above slot */
150 mask ^= 0xFFFF;
151 mask <<= 3;
152 save_hi = value & mask;
153
154 value = save_hi | (tmp << 3) | slot;
155
156 b->cache_lru = value;
157}
158
159void
160BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b,
161 u8 slot);
162
163static inline u8 BV (clib_bihash_get_lru) (BVT (clib_bihash_bucket) * b)
164{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400165#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400166 return (b->cache_lru >> (3 * (BIHASH_KVP_CACHE_SIZE - 1))) & 7;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400167#else
168 return 0;
169#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400170}
171
172static inline void BV (clib_bihash_reset_cache) (BVT (clib_bihash_bucket) * b)
173{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400174#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400175 u16 initial_lru_value;
176
177 memset (b->cache, 0xff, sizeof (b->cache));
178
179 /*
180 * We'll want the cache to be loaded from slot 0 -> slot N, so
181 * the initial LRU order is reverse index order.
182 */
183 if (BIHASH_KVP_CACHE_SIZE == 1)
184 initial_lru_value = 0;
185 else if (BIHASH_KVP_CACHE_SIZE == 2)
186 initial_lru_value = (0 << 3) | (1 << 0);
187 else if (BIHASH_KVP_CACHE_SIZE == 3)
188 initial_lru_value = (0 << 6) | (1 << 3) | (2 << 0);
189 else if (BIHASH_KVP_CACHE_SIZE == 4)
190 initial_lru_value = (0 << 9) | (1 << 6) | (2 << 3) | (3 << 0);
191 else if (BIHASH_KVP_CACHE_SIZE == 5)
192 initial_lru_value = (0 << 12) | (1 << 9) | (2 << 6) | (3 << 3) | (4 << 0);
193
194 b->cache_lru = initial_lru_value;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400195#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400196}
197
Dave Barach858c06f2017-07-21 10:44:27 -0400198static inline int BV (clib_bihash_lock_bucket) (BVT (clib_bihash_bucket) * b)
Dave Barach908a5ea2017-07-14 12:42:21 -0400199{
200 BVT (clib_bihash_bucket) tmp_b;
Dave Barach858c06f2017-07-21 10:44:27 -0400201 u64 rv;
202
203 tmp_b.as_u64 = 0;
204 tmp_b.cache_lru = 1 << 15;
205
206 rv = __sync_fetch_and_or (&b->as_u64, tmp_b.as_u64);
207 tmp_b.as_u64 = rv;
208 /* Was already locked? */
209 if (tmp_b.cache_lru & (1 << 15))
210 return 0;
211 return 1;
212}
213
214static inline void BV (clib_bihash_unlock_bucket)
215 (BVT (clib_bihash_bucket) * b)
216{
217 BVT (clib_bihash_bucket) tmp_b;
218
Dave Barach908a5ea2017-07-14 12:42:21 -0400219 tmp_b.as_u64 = b->as_u64;
Dave Barach858c06f2017-07-21 10:44:27 -0400220 tmp_b.cache_lru &= ~(1 << 15);
Dave Barach908a5ea2017-07-14 12:42:21 -0400221 b->as_u64 = tmp_b.as_u64;
222}
223
224static inline void *BV (clib_bihash_get_value) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400225 uword offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226{
Dave Barachc3799992016-08-15 11:12:27 -0400227 u8 *hp = h->mheap;
228 u8 *vp = hp + offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
230 return (void *) vp;
231}
232
Dave Barach908a5ea2017-07-14 12:42:21 -0400233static inline uword BV (clib_bihash_get_offset) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400234 void *v)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700235{
Dave Barachc3799992016-08-15 11:12:27 -0400236 u8 *hp, *vp;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237
238 hp = (u8 *) h->mheap;
239 vp = (u8 *) v;
240
Dave Barachc3799992016-08-15 11:12:27 -0400241 ASSERT ((vp - hp) < 0x100000000ULL);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 return vp - hp;
243}
244
Dave Barachc3799992016-08-15 11:12:27 -0400245void BV (clib_bihash_init)
246 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800248void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
249 format_function_t * fmt_fn);
250
Dave Barachc3799992016-08-15 11:12:27 -0400251void BV (clib_bihash_free) (BVT (clib_bihash) * h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252
Dave Barachc3799992016-08-15 11:12:27 -0400253int BV (clib_bihash_add_del) (BVT (clib_bihash) * h,
254 BVT (clib_bihash_kv) * add_v, int is_add);
Dave Barach908a5ea2017-07-14 12:42:21 -0400255int BV (clib_bihash_search) (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400256 BVT (clib_bihash_kv) * search_v,
257 BVT (clib_bihash_kv) * return_v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
Dave Barachc3799992016-08-15 11:12:27 -0400259void BV (clib_bihash_foreach_key_value_pair) (BVT (clib_bihash) * h,
260 void *callback, void *arg);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261
Dave Barachc3799992016-08-15 11:12:27 -0400262format_function_t BV (format_bihash);
263format_function_t BV (format_bihash_kvp);
Dave Barach908a5ea2017-07-14 12:42:21 -0400264format_function_t BV (format_bihash_lru);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Dave Barachc3799992016-08-15 11:12:27 -0400266static inline int BV (clib_bihash_search_inline)
Dave Barach908a5ea2017-07-14 12:42:21 -0400267 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * key_result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268{
269 u64 hash;
270 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400271 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400272 BVT (clib_bihash_bucket) * b;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400273#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400274 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400275#endif
Dave Barach5e6b9582016-12-12 15:37:29 -0500276 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277
Dave Barach908a5ea2017-07-14 12:42:21 -0400278 hash = BV (clib_bihash_hash) (key_result);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barachc3799992016-08-15 11:12:27 -0400280 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281 b = &h->buckets[bucket_index];
282
283 if (b->offset == 0)
284 return -1;
285
Dave Barachc8ef08a2017-08-31 05:58:22 -0400286#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach858c06f2017-07-21 10:44:27 -0400287 /* Check the cache, if not currently locked */
288 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400289 {
290 limit = BIHASH_KVP_CACHE_SIZE;
291 kvp = b->cache;
292 for (i = 0; i < limit; i++)
293 {
294 if (BV (clib_bihash_key_compare) (kvp[i].key, key_result->key))
295 {
296 *key_result = kvp[i];
297 h->cache_hits++;
298 return 0;
299 }
300 }
301 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400302#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400303
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304 hash >>= h->log2_nbuckets;
305
Dave Barachc3799992016-08-15 11:12:27 -0400306 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400307
Dave Barach5e6b9582016-12-12 15:37:29 -0500308 /* 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;
313
314 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400316 if (BV (clib_bihash_key_compare) (v->kvp[i].key, key_result->key))
Dave Barachc3799992016-08-15 11:12:27 -0400317 {
Dave Barach908a5ea2017-07-14 12:42:21 -0400318 *key_result = v->kvp[i];
319
Dave Barachc8ef08a2017-08-31 05:58:22 -0400320#if BIHASH_KVP_CACHE_SIZE > 0
321 u8 cache_slot;
Dave Barach858c06f2017-07-21 10:44:27 -0400322 /* Try to lock the bucket */
323 if (BV (clib_bihash_lock_bucket) (b))
324 {
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);
Dave Barach908a5ea2017-07-14 12:42:21 -0400328
Dave Barach858c06f2017-07-21 10:44:27 -0400329 /* Unlock the bucket */
330 BV (clib_bihash_unlock_bucket) (b);
331 h->cache_misses++;
332 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400333#endif
Dave Barachc3799992016-08-15 11:12:27 -0400334 return 0;
335 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 }
337 return -1;
338}
339
Dave Barachc3799992016-08-15 11:12:27 -0400340static inline int BV (clib_bihash_search_inline_2)
Dave Barach908a5ea2017-07-14 12:42:21 -0400341 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400342 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700343{
344 u64 hash;
345 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400346 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400347 BVT (clib_bihash_bucket) * b;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400348#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400349 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400350#endif
Dave Barach5e6b9582016-12-12 15:37:29 -0500351 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700352
Dave Barachc3799992016-08-15 11:12:27 -0400353 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354
Dave Barachc3799992016-08-15 11:12:27 -0400355 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700356
Dave Barachc3799992016-08-15 11:12:27 -0400357 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 b = &h->buckets[bucket_index];
359
360 if (b->offset == 0)
361 return -1;
362
Dave Barach858c06f2017-07-21 10:44:27 -0400363 /* Check the cache, if currently unlocked */
Dave Barachc8ef08a2017-08-31 05:58:22 -0400364#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach858c06f2017-07-21 10:44:27 -0400365 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400366 {
367 limit = BIHASH_KVP_CACHE_SIZE;
368 kvp = b->cache;
369 for (i = 0; i < limit; i++)
370 {
371 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
372 {
373 *valuep = kvp[i];
374 h->cache_hits++;
375 return 0;
376 }
377 }
378 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400379#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400380
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 hash >>= h->log2_nbuckets;
Dave Barachc3799992016-08-15 11:12:27 -0400382 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barachc3799992016-08-15 11:12:27 -0400383
Dave Barach5e6b9582016-12-12 15:37:29 -0500384 /* 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;
389
390 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 {
Dave Barachc3799992016-08-15 11:12:27 -0400392 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
393 {
394 *valuep = v->kvp[i];
Dave Barach908a5ea2017-07-14 12:42:21 -0400395
Dave Barachc8ef08a2017-08-31 05:58:22 -0400396#if BIHASH_KVP_CACHE_SIZE > 0
397 u8 cache_slot;
398
Dave Barach858c06f2017-07-21 10:44:27 -0400399 /* Try to lock the bucket */
400 if (BV (clib_bihash_lock_bucket) (b))
401 {
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);
Dave Barach908a5ea2017-07-14 12:42:21 -0400405
Dave Barach858c06f2017-07-21 10:44:27 -0400406 /* Reenable the cache */
407 BV (clib_bihash_unlock_bucket) (b);
408 h->cache_misses++;
409 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400410#endif
Dave Barachc3799992016-08-15 11:12:27 -0400411 return 0;
412 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 }
414 return -1;
415}
416
Ed Warnickecb9cada2015-12-08 15:45:58 -0700417#endif /* __included_bihash_template_h__ */
Dave Barachdd3a57f2016-07-27 16:58:51 -0400418
Chris Luke16bcf7d2016-09-01 14:31:46 -0400419/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400420
421/*
422 * fd.io coding-style-patch-verification: ON
423 *
424 * Local Variables:
425 * eval: (c-set-style "gnu")
426 * End:
427 */