blob: 8a6fa16389b598f95550d39ff4d0b9666df978e4 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
Chris Luke16bcf7d2016-09-01 14:31:46 -040016/** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
Dave Barachdd3a57f2016-07-27 16:58:51 -040017
Dave Barach97f5af02018-02-22 09:48:45 -050018static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
19{
20 uword rv;
21
22 /* Round to an even number of cache lines */
23 nbytes += CLIB_CACHE_LINE_BYTES - 1;
24 nbytes &= ~(CLIB_CACHE_LINE_BYTES - 1);
25
26 rv = h->alloc_arena_next;
27 h->alloc_arena_next += nbytes;
28
29 if (rv >= (h->alloc_arena + h->alloc_arena_size))
30 os_out_of_memory ();
31
32 return (void *) rv;
33}
34
35
Dave Barachc3799992016-08-15 11:12:27 -040036void BV (clib_bihash_init)
37 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -070038{
Dave Barach97f5af02018-02-22 09:48:45 -050039 uword bucket_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -070040
41 nbuckets = 1 << (max_log2 (nbuckets));
42
Dave Barachc3799992016-08-15 11:12:27 -040043 h->name = (u8 *) name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 h->nbuckets = nbuckets;
45 h->log2_nbuckets = max_log2 (nbuckets);
Dave Barach908a5ea2017-07-14 12:42:21 -040046 h->cache_hits = 0;
47 h->cache_misses = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070048
Dave Barach508498f2018-07-19 12:11:16 -040049 /*
50 * Make sure the requested size is rational. The max table
51 * size without playing the alignment card is 64 Gbytes.
52 * If someone starts complaining that's not enough, we can shift
53 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
54 */
55 ASSERT (memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
56
Dave Barach97f5af02018-02-22 09:48:45 -050057 h->alloc_arena = (uword) clib_mem_vm_alloc (memory_size);
58 h->alloc_arena_next = h->alloc_arena;
59 h->alloc_arena_size = memory_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -070060
Dave Barach97f5af02018-02-22 09:48:45 -050061 bucket_size = nbuckets * sizeof (h->buckets[0]);
62 h->buckets = BV (alloc_aligned) (h, bucket_size);
63
Dave Barach508498f2018-07-19 12:11:16 -040064 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
65 h->alloc_lock[0] = 0;
Dave Barach908a5ea2017-07-14 12:42:21 -040066
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -080067 h->fmt_fn = NULL;
68}
69
70void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
71 format_function_t * fmt_fn)
72{
73 h->fmt_fn = fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074}
75
Dave Barachc3799992016-08-15 11:12:27 -040076void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -070077{
Dave Barach97f5af02018-02-22 09:48:45 -050078 vec_free (h->working_copies);
79 vec_free (h->freelists);
80 clib_mem_vm_free ((void *) (h->alloc_arena), h->alloc_arena_size);
Dave Barachc3799992016-08-15 11:12:27 -040081 memset (h, 0, sizeof (*h));
Ed Warnickecb9cada2015-12-08 15:45:58 -070082}
83
Dave Barachc3799992016-08-15 11:12:27 -040084static
85BVT (clib_bihash_value) *
86BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Dave Barachc3799992016-08-15 11:12:27 -040088 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barach508498f2018-07-19 12:11:16 -040090 ASSERT (h->alloc_lock[0]);
Dave Barachc3799992016-08-15 11:12:27 -040091 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070092 {
Dave Barach97f5af02018-02-22 09:48:45 -050093 vec_validate_init_empty (h->freelists, log2_pages, 0);
94 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -040095 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 }
Dave Barachc3799992016-08-15 11:12:27 -040097 rv = h->freelists[log2_pages];
98 h->freelists[log2_pages] = rv->next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070099
Dave Barachc3799992016-08-15 11:12:27 -0400100initialize:
101 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -0400102 /*
103 * Latest gcc complains that the length arg is zero
104 * if we replace (1<<log2_pages) with vec_len(rv).
105 * No clue.
106 */
107 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
108 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109}
110
111static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400112BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
113 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700114{
Dave Barach508498f2018-07-19 12:11:16 -0400115 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700116
Dave Barachc3799992016-08-15 11:12:27 -0400117 ASSERT (vec_len (h->freelists) > log2_pages);
118
Dave Barach508498f2018-07-19 12:11:16 -0400119 if (CLIB_DEBUG > 0)
120 memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
121
Dave Barachc3799992016-08-15 11:12:27 -0400122 v->next_free = h->freelists[log2_pages];
123 h->freelists[log2_pages] = v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700124}
125
126static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400127BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700128{
Dave Barachc3799992016-08-15 11:12:27 -0400129 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400130 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400131 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200132 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400133 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barach508498f2018-07-19 12:11:16 -0400135 ASSERT (h->alloc_lock[0]);
136
Damjan Marionf55f9b82017-05-10 21:06:28 +0200137 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700138 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200139 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700140 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141 }
142
Dave Barachc3799992016-08-15 11:12:27 -0400143 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144 * working_copies are per-cpu so that near-simultaneous
145 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400146 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700147 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200148 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400149 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700150
151 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Dave Barachba7ddfe2017-05-17 20:20:50 -0400153 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 {
Dave Barach97f5af02018-02-22 09:48:45 -0500155 /*
156 * It's not worth the bookkeeping to free working copies
157 * if (working_copy)
158 * clib_mem_free (working_copy);
159 */
160 working_copy = BV (alloc_aligned)
161 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400162 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200163 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700164 }
165
Dave Barachc3799992016-08-15 11:12:27 -0400166 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
Dave Barachc3799992016-08-15 11:12:27 -0400168 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700169 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400170 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
171 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700172 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200173 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700174}
175
Dave Barachc3799992016-08-15 11:12:27 -0400176static
177BVT (clib_bihash_value) *
178BV (split_and_rehash)
179 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400180 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
181 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700182{
Dave Barach5e6b9582016-12-12 15:37:29 -0500183 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400184 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700185
Dave Barach508498f2018-07-19 12:11:16 -0400186 ASSERT (h->alloc_lock[0]);
187
Dave Barachc3799992016-08-15 11:12:27 -0400188 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400189 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700190
Dave Barachba7ddfe2017-05-17 20:20:50 -0400191 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192 {
193 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400194
Dave Barach5e6b9582016-12-12 15:37:29 -0500195 /* Entry not in use? Forget it */
196 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
197 continue;
198
199 /* rehash the item onto its new home-page */
200 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
201 new_hash >>= h->log2_nbuckets;
202 new_hash &= (1 << new_log2_pages) - 1;
203 new_v = &new_values[new_hash];
204
205 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400207 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500208 /* Empty slot */
209 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400210 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500211 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
212 sizeof (new_v->kvp[j]));
213 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400214 }
Dave Barachc3799992016-08-15 11:12:27 -0400215 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500216 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400217 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500218 return 0;
219 doublebreak:;
220 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400221
Dave Barach5e6b9582016-12-12 15:37:29 -0500222 return new_values;
223}
224
225static
226BVT (clib_bihash_value) *
227BV (split_and_rehash_linear)
228 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400229 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
230 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500231{
232 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400233 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500234
Dave Barach508498f2018-07-19 12:11:16 -0400235 ASSERT (h->alloc_lock[0]);
236
Dave Barach5e6b9582016-12-12 15:37:29 -0500237 new_values = BV (value_alloc) (h, new_log2_pages);
238 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400239 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500240
241 j = 0;
242 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400243 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500244 {
245 /* Find a free slot in the new linear scan bucket */
246 for (; j < new_length; j++)
247 {
Dave Barach8f544962017-01-18 10:23:22 -0500248 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500249 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
250 goto doublebreak;
251
252 /* New value should never be in use */
253 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
254 {
255 /* Copy the old value and move along */
256 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
257 sizeof (new_values->kvp[j]));
258 j++;
259 goto doublebreak;
260 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500261 }
Dave Barach8f544962017-01-18 10:23:22 -0500262 /* This should never happen... */
263 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400264 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500265 return 0;
266
Dave Barach5e6b9582016-12-12 15:37:29 -0500267 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 }
269 return new_values;
270}
271
Dave Barachc3799992016-08-15 11:12:27 -0400272int BV (clib_bihash_add_del)
273 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274{
275 u32 bucket_index;
Dave Barach908a5ea2017-07-14 12:42:21 -0400276 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400277 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500278 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 u64 hash, new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400280 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200281 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500282 int mark_bucket_linear;
283 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284
Dave Barachc3799992016-08-15 11:12:27 -0400285 hash = BV (clib_bihash_hash) (add_v);
286
287 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700288 b = &h->buckets[bucket_index];
289
290 hash >>= h->log2_nbuckets;
291
Dave Barach508498f2018-07-19 12:11:16 -0400292 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
294 /* First elt in the bucket? */
Damjan Marion882fcfe2018-07-17 23:01:49 +0200295 if (BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700296 {
297 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400298 {
Dave Barach508498f2018-07-19 12:11:16 -0400299 BV (clib_bihash_unlock_bucket) (b);
300 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400301 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302
Dave Barach508498f2018-07-19 12:11:16 -0400303 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400304 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400305 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400306
Dave Barachc3799992016-08-15 11:12:27 -0400307 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400308 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400309 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500310 tmp_b.refcnt = 1;
Dave Barach508498f2018-07-19 12:11:16 -0400311 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312
313 b->as_u64 = tmp_b.as_u64;
Dave Barach508498f2018-07-19 12:11:16 -0400314 BV (clib_bihash_unlock_bucket) (b);
315 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316 }
317
Dave Barach508498f2018-07-19 12:11:16 -0400318 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500319 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400320 v = BV (clib_bihash_get_value) (h, b->offset);
321
Dave Barach5e6b9582016-12-12 15:37:29 -0500322 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
323 if (b->linear_search)
324 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400325
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 if (is_add)
327 {
Dave Barachc3799992016-08-15 11:12:27 -0400328 /*
Dave Barach508498f2018-07-19 12:11:16 -0400329 * Because reader threads are looking at live data,
330 * we have to be extra careful. Readers do NOT hold the
331 * bucket lock. We need to be SLOWER than a search, past the
332 * point where readers CHECK the bucket lock.
333 */
334
335 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 * For obvious (in hindsight) reasons, see if we're supposed to
337 * replace an existing key, then look for an empty slot.
338 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500339 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400340 {
341 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
342 {
Dave Barach508498f2018-07-19 12:11:16 -0400343 CLIB_MEMORY_BARRIER (); /* Add a delay */
Dave Barachc3799992016-08-15 11:12:27 -0400344 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
Dave Barach508498f2018-07-19 12:11:16 -0400345 BV (clib_bihash_unlock_bucket) (b);
346 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400347 }
348 }
Dave Barach508498f2018-07-19 12:11:16 -0400349 /*
350 * Look for an empty slot. If found, use it
351 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500352 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400353 {
354 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
355 {
Dave Barach508498f2018-07-19 12:11:16 -0400356 /*
357 * Copy the value first, so that if a reader manages
358 * to match the new key, the value will be right...
359 */
360 clib_memcpy (&(v->kvp[i].value),
361 &add_v->value, sizeof (add_v->value));
362 CLIB_MEMORY_BARRIER (); /* Make sure the value has settled */
363 clib_memcpy (&(v->kvp[i]), &add_v->key, sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500364 b->refcnt++;
Dave Barach508498f2018-07-19 12:11:16 -0400365 BV (clib_bihash_unlock_bucket) (b);
366 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400367 }
368 }
Dave Barach508498f2018-07-19 12:11:16 -0400369 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700370 }
Dave Barach508498f2018-07-19 12:11:16 -0400371 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700372 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500373 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400374 {
Dave Barach508498f2018-07-19 12:11:16 -0400375 /* Found the key? Kill it... */
Dave Barachc3799992016-08-15 11:12:27 -0400376 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
377 {
378 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
Dave Barach508498f2018-07-19 12:11:16 -0400379 /* Is the bucket empty? */
380 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500381 {
Dave Barach508498f2018-07-19 12:11:16 -0400382 b->refcnt--;
383 BV (clib_bihash_unlock_bucket) (b);
384 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500385 }
Dave Barach508498f2018-07-19 12:11:16 -0400386 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500387 {
Dave Barach508498f2018-07-19 12:11:16 -0400388 /* Save old bucket value, need log2_pages to free it */
389 tmp_b.as_u64 = b->as_u64;
390 CLIB_MEMORY_BARRIER ();
391
392 /* Kill and unlock the bucket */
393 b->as_u64 = 0;
394
395 /* And free the backing storage */
396 BV (clib_bihash_alloc_lock) (h);
397 /* Note: v currently points into the middle of the bucket */
398 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
399 BV (value_free) (h, v, tmp_b.log2_pages);
400 BV (clib_bihash_alloc_unlock) (h);
401 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500402 }
Dave Barachc3799992016-08-15 11:12:27 -0400403 }
404 }
Dave Barach508498f2018-07-19 12:11:16 -0400405 /* Not found... */
406 BV (clib_bihash_unlock_bucket) (b);
407 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 }
409
Dave Barach508498f2018-07-19 12:11:16 -0400410 /* Move readers to a (locked) temp copy of the bucket */
411 BV (clib_bihash_alloc_lock) (h);
412 BV (make_working_copy) (h, b);
413
414 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
415
Dave Barachba7ddfe2017-05-17 20:20:50 -0400416 old_log2_pages = h->saved_bucket.log2_pages;
417 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500418 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Damjan Marionf55f9b82017-05-10 21:06:28 +0200420 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500421 resplit_once = 0;
422
Dave Barachba7ddfe2017-05-17 20:20:50 -0400423 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
424 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 if (new_v == 0)
426 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500427 try_resplit:
428 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700429 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500430 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400431 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
432 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500433 if (new_v == 0)
434 {
435 mark_linear:
436 new_log2_pages--;
437 /* pinned collisions, use linear search */
438 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400439 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
440 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500441 mark_bucket_linear = 1;
442 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 }
444
445 /* Try to add the new entry */
446 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400447 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500448 limit = BIHASH_KVP_PER_PAGE;
449 if (mark_bucket_linear)
450 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500452 new_hash &= (1 << new_log2_pages) - 1;
453 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400454
Dave Barach5e6b9582016-12-12 15:37:29 -0500455 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 {
Dave Barachc3799992016-08-15 11:12:27 -0400457 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
458 {
459 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
460 goto expand_ok;
461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400463
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400465 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500466 /*
467 * If we've already doubled the size of the bucket once,
468 * fall back to linear search now.
469 */
470 if (resplit_once)
471 goto mark_linear;
472 else
473 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474
Dave Barachc3799992016-08-15 11:12:27 -0400475expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500476 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400477 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500478 tmp_b.linear_search = mark_bucket_linear;
Dave Barache7d212f2018-02-07 13:14:06 -0500479 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach508498f2018-07-19 12:11:16 -0400480 tmp_b.lock = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400481 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 b->as_u64 = tmp_b.as_u64;
Dave Barach508498f2018-07-19 12:11:16 -0400483 BV (clib_bihash_alloc_unlock) (h);
484 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485}
486
Dave Barachc3799992016-08-15 11:12:27 -0400487int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -0400488 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400489 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700490{
491 u64 hash;
492 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400493 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400494 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500495 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496
Dave Barachc3799992016-08-15 11:12:27 -0400497 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498
Dave Barachc3799992016-08-15 11:12:27 -0400499 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500
Dave Barachc3799992016-08-15 11:12:27 -0400501 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502 b = &h->buckets[bucket_index];
503
Damjan Marion882fcfe2018-07-17 23:01:49 +0200504 if (BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505 return -1;
506
Dave Barach508498f2018-07-19 12:11:16 -0400507 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400508 {
Dave Barach508498f2018-07-19 12:11:16 -0400509 volatile BVT (clib_bihash_bucket) * bv = b;
510 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200511 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400512 }
513
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 hash >>= h->log2_nbuckets;
515
Dave Barachc3799992016-08-15 11:12:27 -0400516 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500517 limit = BIHASH_KVP_PER_PAGE;
518 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
519 if (PREDICT_FALSE (b->linear_search))
520 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400521
Dave Barach5e6b9582016-12-12 15:37:29 -0500522 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523 {
Dave Barachc3799992016-08-15 11:12:27 -0400524 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
525 {
526 *valuep = v->kvp[i];
527 return 0;
528 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529 }
530 return -1;
531}
532
Dave Barachc3799992016-08-15 11:12:27 -0400533u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534{
Dave Barachc3799992016-08-15 11:12:27 -0400535 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700536 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -0400537 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400538 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 int i, j, k;
540 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -0500541 u64 active_buckets = 0;
542 u64 linear_buckets = 0;
Dave Barach97f5af02018-02-22 09:48:45 -0500543 u64 used_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
545 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 for (i = 0; i < h->nbuckets; i++)
548 {
Dave Barachc3799992016-08-15 11:12:27 -0400549 b = &h->buckets[i];
Damjan Marion882fcfe2018-07-17 23:01:49 +0200550 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -0400551 {
552 if (verbose > 1)
553 s = format (s, "[%d]: empty\n", i);
554 continue;
555 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
Dave Barache7d212f2018-02-07 13:14:06 -0500557 active_buckets++;
558
559 if (b->linear_search)
560 linear_buckets++;
561
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400563 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500564 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
565 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400566 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
Dave Barachc3799992016-08-15 11:12:27 -0400568 v = BV (clib_bihash_get_value) (h, b->offset);
569 for (j = 0; j < (1 << b->log2_pages); j++)
570 {
571 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
572 {
573 if (BV (clib_bihash_is_free) (&v->kvp[k]))
574 {
575 if (verbose > 1)
576 s = format (s, " %d: empty\n",
577 j * BIHASH_KVP_PER_PAGE + k);
578 continue;
579 }
580 if (verbose)
581 {
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800582 if (h->fmt_fn)
583 {
584 s = format (s, " %d: %U\n",
585 j * BIHASH_KVP_PER_PAGE + k,
586 h->fmt_fn, &(v->kvp[k]));
587 }
588 else
589 {
590 s = format (s, " %d: %U\n",
591 j * BIHASH_KVP_PER_PAGE + k,
592 BV (format_bihash_kvp), &(v->kvp[k]));
593 }
Dave Barachc3799992016-08-15 11:12:27 -0400594 }
595 active_elements++;
596 }
597 v++;
598 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599 }
600
Dave Barache7d212f2018-02-07 13:14:06 -0500601 s = format (s, " %lld active elements %lld active buckets\n",
602 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -0500604
605 for (i = 0; i < vec_len (h->freelists); i++)
606 {
607 u32 nfree = 0;
608 BVT (clib_bihash_value) * free_elt;
609
610 free_elt = h->freelists[i];
611 while (free_elt)
612 {
613 nfree++;
614 free_elt = free_elt->next_free;
615 }
616
617 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
618 }
619
620 s = format (s, " %lld linear search buckets\n", linear_buckets);
Dave Barach908a5ea2017-07-14 12:42:21 -0400621 s = format (s, " %lld cache hits, %lld cache misses\n",
622 h->cache_hits, h->cache_misses);
Dave Barach97f5af02018-02-22 09:48:45 -0500623 used_bytes = h->alloc_arena_next - h->alloc_arena;
624 s = format (s,
625 " arena: base %llx, next %llx\n"
626 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
627 h->alloc_arena, h->alloc_arena_next,
628 used_bytes, used_bytes >> 20,
629 h->alloc_arena_size, h->alloc_arena_size >> 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 return s;
631}
632
Dave Barachc3799992016-08-15 11:12:27 -0400633void BV (clib_bihash_foreach_key_value_pair)
634 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635{
636 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -0400637 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400638 BVT (clib_bihash_value) * v;
639 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
640
Ed Warnickecb9cada2015-12-08 15:45:58 -0700641 for (i = 0; i < h->nbuckets; i++)
642 {
Dave Barachc3799992016-08-15 11:12:27 -0400643 b = &h->buckets[i];
Damjan Marion882fcfe2018-07-17 23:01:49 +0200644 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -0400645 continue;
646
647 v = BV (clib_bihash_get_value) (h, b->offset);
648 for (j = 0; j < (1 << b->log2_pages); j++)
649 {
650 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
651 {
652 if (BV (clib_bihash_is_free) (&v->kvp[k]))
653 continue;
654
655 (*fp) (&v->kvp[k], arg);
656 }
657 v++;
658 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659 }
660}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400661
Chris Luke16bcf7d2016-09-01 14:31:46 -0400662/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400663
664/*
665 * fd.io coding-style-patch-verification: ON
666 *
667 * Local Variables:
668 * eval: (c-set-style "gnu")
669 * End:
670 */