blob: 7e4216bdeb984c6a652bbedd147575032c8e7cca [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 Barachc3799992016-08-15 11:12:27 -040018void BV (clib_bihash_init)
19 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -070020{
Dave Barachc3799992016-08-15 11:12:27 -040021 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070022
23 nbuckets = 1 << (max_log2 (nbuckets));
24
Dave Barachc3799992016-08-15 11:12:27 -040025 h->name = (u8 *) name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070026 h->nbuckets = nbuckets;
27 h->log2_nbuckets = max_log2 (nbuckets);
28
Dave Barachc3799992016-08-15 11:12:27 -040029 h->mheap = mheap_alloc (0 /* use VM */ , memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -070030
31 oldheap = clib_mem_set_heap (h->mheap);
32 vec_validate_aligned (h->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
Dave Barachc3799992016-08-15 11:12:27 -040033 h->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
34 CLIB_CACHE_LINE_BYTES);
Ed Warnickecb9cada2015-12-08 15:45:58 -070035
36 clib_mem_set_heap (oldheap);
37}
38
Dave Barachc3799992016-08-15 11:12:27 -040039void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -070040{
Dave Barachc3799992016-08-15 11:12:27 -040041 mheap_free (h->mheap);
42 memset (h, 0, sizeof (*h));
Ed Warnickecb9cada2015-12-08 15:45:58 -070043}
44
Dave Barachc3799992016-08-15 11:12:27 -040045static
46BVT (clib_bihash_value) *
47BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070048{
Dave Barachc3799992016-08-15 11:12:27 -040049 BVT (clib_bihash_value) * rv = 0;
50 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
Dave Barachc3799992016-08-15 11:12:27 -040052 ASSERT (h->writer_lock[0]);
53 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070054 {
Dave Barachc3799992016-08-15 11:12:27 -040055 oldheap = clib_mem_set_heap (h->mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070056
Dave Barachc3799992016-08-15 11:12:27 -040057 vec_validate (h->freelists, log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -040058 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
59 CLIB_CACHE_LINE_BYTES);
Dave Barachc3799992016-08-15 11:12:27 -040060 clib_mem_set_heap (oldheap);
61 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -070062 }
Dave Barachc3799992016-08-15 11:12:27 -040063 rv = h->freelists[log2_pages];
64 h->freelists[log2_pages] = rv->next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070065
Dave Barachc3799992016-08-15 11:12:27 -040066initialize:
67 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -040068 /*
69 * Latest gcc complains that the length arg is zero
70 * if we replace (1<<log2_pages) with vec_len(rv).
71 * No clue.
72 */
73 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
74 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070075}
76
77static void
Dave Barachba7ddfe2017-05-17 20:20:50 -040078BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
79 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070080{
Dave Barachc3799992016-08-15 11:12:27 -040081 ASSERT (h->writer_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070082
Dave Barachc3799992016-08-15 11:12:27 -040083 ASSERT (vec_len (h->freelists) > log2_pages);
84
85 v->next_free = h->freelists[log2_pages];
86 h->freelists[log2_pages] = v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070087}
88
89static inline void
Dave Barachc3799992016-08-15 11:12:27 -040090BV (make_working_copy) (BVT (clib_bihash) * h, clib_bihash_bucket_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070091{
Dave Barachc3799992016-08-15 11:12:27 -040092 BVT (clib_bihash_value) * v;
93 clib_bihash_bucket_t working_bucket __attribute__ ((aligned (8)));
94 void *oldheap;
95 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +020096 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -040097 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -070098
Damjan Marionf55f9b82017-05-10 21:06:28 +020099 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100 {
101 oldheap = clib_mem_set_heap (h->mheap);
Damjan Marionf55f9b82017-05-10 21:06:28 +0200102 vec_validate (h->working_copies, thread_index);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400103 vec_validate (h->working_copy_lengths, thread_index);
104 h->working_copy_lengths[thread_index] = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105 clib_mem_set_heap (oldheap);
106 }
107
Dave Barachc3799992016-08-15 11:12:27 -0400108 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 * working_copies are per-cpu so that near-simultaneous
110 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400111 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700112 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200113 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400114 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115
116 h->saved_bucket.as_u64 = b->as_u64;
117 oldheap = clib_mem_set_heap (h->mheap);
118
Dave Barachba7ddfe2017-05-17 20:20:50 -0400119 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700120 {
Dave Barachba7ddfe2017-05-17 20:20:50 -0400121 if (working_copy)
122 clib_mem_free (working_copy);
123
124 working_copy = clib_mem_alloc_aligned
125 (sizeof (working_copy[0]) * (1 << b->log2_pages),
126 CLIB_CACHE_LINE_BYTES);
127 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200128 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129 }
130
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 clib_mem_set_heap (oldheap);
132
Dave Barachc3799992016-08-15 11:12:27 -0400133 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134
Dave Barachc3799992016-08-15 11:12:27 -0400135 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700136 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400137 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
138 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200140 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700141}
142
Dave Barachc3799992016-08-15 11:12:27 -0400143static
144BVT (clib_bihash_value) *
145BV (split_and_rehash)
146 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400147 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
148 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149{
Dave Barach5e6b9582016-12-12 15:37:29 -0500150 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400151 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Dave Barachc3799992016-08-15 11:12:27 -0400153 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400154 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700155
Dave Barachba7ddfe2017-05-17 20:20:50 -0400156 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 {
158 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400159
Dave Barach5e6b9582016-12-12 15:37:29 -0500160 /* Entry not in use? Forget it */
161 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
162 continue;
163
164 /* rehash the item onto its new home-page */
165 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
166 new_hash >>= h->log2_nbuckets;
167 new_hash &= (1 << new_log2_pages) - 1;
168 new_v = &new_values[new_hash];
169
170 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700171 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400172 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500173 /* Empty slot */
174 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400175 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500176 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
177 sizeof (new_v->kvp[j]));
178 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400179 }
Dave Barachc3799992016-08-15 11:12:27 -0400180 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500181 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400182 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500183 return 0;
184 doublebreak:;
185 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400186
Dave Barach5e6b9582016-12-12 15:37:29 -0500187 return new_values;
188}
189
190static
191BVT (clib_bihash_value) *
192BV (split_and_rehash_linear)
193 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400194 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
195 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500196{
197 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400198 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500199
200 new_values = BV (value_alloc) (h, new_log2_pages);
201 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400202 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500203
204 j = 0;
205 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400206 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500207 {
208 /* Find a free slot in the new linear scan bucket */
209 for (; j < new_length; j++)
210 {
Dave Barach8f544962017-01-18 10:23:22 -0500211 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500212 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
213 goto doublebreak;
214
215 /* New value should never be in use */
216 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
217 {
218 /* Copy the old value and move along */
219 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
220 sizeof (new_values->kvp[j]));
221 j++;
222 goto doublebreak;
223 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500224 }
Dave Barach8f544962017-01-18 10:23:22 -0500225 /* This should never happen... */
226 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400227 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500228 return 0;
229
Dave Barach5e6b9582016-12-12 15:37:29 -0500230 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 }
232 return new_values;
233}
234
Dave Barachc3799992016-08-15 11:12:27 -0400235int BV (clib_bihash_add_del)
236 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700237{
238 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400239 clib_bihash_bucket_t *b, tmp_b;
240 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 int rv = 0;
Dave Barach5e6b9582016-12-12 15:37:29 -0500242 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700243 u64 hash, new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400244 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200245 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500246 int mark_bucket_linear;
247 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
Dave Barachc3799992016-08-15 11:12:27 -0400249 hash = BV (clib_bihash_hash) (add_v);
250
251 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700252 b = &h->buckets[bucket_index];
253
254 hash >>= h->log2_nbuckets;
255
256 while (__sync_lock_test_and_set (h->writer_lock, 1))
Dave Barachc3799992016-08-15 11:12:27 -0400257 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
259 /* First elt in the bucket? */
260 if (b->offset == 0)
261 {
262 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400263 {
264 rv = -1;
265 goto unlock;
266 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700267
Dave Barachc3799992016-08-15 11:12:27 -0400268 v = BV (value_alloc) (h, 0);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400269
Dave Barachc3799992016-08-15 11:12:27 -0400270 *v->kvp = *add_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 tmp_b.as_u64 = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400272 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700273
274 b->as_u64 = tmp_b.as_u64;
275 goto unlock;
276 }
277
Dave Barachc3799992016-08-15 11:12:27 -0400278 BV (make_working_copy) (h, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barachc3799992016-08-15 11:12:27 -0400280 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500281
282 limit = BIHASH_KVP_PER_PAGE;
283 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
284 if (b->linear_search)
285 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400286
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287 if (is_add)
288 {
Dave Barachc3799992016-08-15 11:12:27 -0400289 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700290 * For obvious (in hindsight) reasons, see if we're supposed to
291 * replace an existing key, then look for an empty slot.
292 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500293 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400294 {
295 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
296 {
297 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
298 CLIB_MEMORY_BARRIER ();
299 /* Restore the previous (k,v) pairs */
300 b->as_u64 = h->saved_bucket.as_u64;
301 goto unlock;
302 }
303 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500304 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400305 {
306 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
307 {
308 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
309 CLIB_MEMORY_BARRIER ();
310 b->as_u64 = h->saved_bucket.as_u64;
311 goto unlock;
312 }
313 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700314 /* no room at the inn... split case... */
315 }
316 else
317 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500318 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400319 {
320 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
321 {
322 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
323 CLIB_MEMORY_BARRIER ();
324 b->as_u64 = h->saved_bucket.as_u64;
325 goto unlock;
326 }
327 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700328 rv = -3;
329 b->as_u64 = h->saved_bucket.as_u64;
330 goto unlock;
331 }
332
Dave Barachba7ddfe2017-05-17 20:20:50 -0400333 old_log2_pages = h->saved_bucket.log2_pages;
334 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500335 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336
Damjan Marionf55f9b82017-05-10 21:06:28 +0200337 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500338 resplit_once = 0;
339
Dave Barachba7ddfe2017-05-17 20:20:50 -0400340 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
341 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700342 if (new_v == 0)
343 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500344 try_resplit:
345 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500347 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400348 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
349 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500350 if (new_v == 0)
351 {
352 mark_linear:
353 new_log2_pages--;
354 /* pinned collisions, use linear search */
355 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400356 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
357 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500358 mark_bucket_linear = 1;
359 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 }
361
362 /* Try to add the new entry */
363 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400364 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500365 limit = BIHASH_KVP_PER_PAGE;
366 if (mark_bucket_linear)
367 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500369 new_hash &= (1 << new_log2_pages) - 1;
370 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400371
Dave Barach5e6b9582016-12-12 15:37:29 -0500372 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 {
Dave Barachc3799992016-08-15 11:12:27 -0400374 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
375 {
376 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
377 goto expand_ok;
378 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700379 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400380
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400382 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500383 /*
384 * If we've already doubled the size of the bucket once,
385 * fall back to linear search now.
386 */
387 if (resplit_once)
388 goto mark_linear;
389 else
390 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391
Dave Barachc3799992016-08-15 11:12:27 -0400392expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500393 /* Keep track of the number of linear-scan buckets */
394 if (tmp_b.linear_search ^ mark_bucket_linear)
395 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
396
397 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400398 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500399 tmp_b.linear_search = mark_bucket_linear;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400400
Dave Barachc3799992016-08-15 11:12:27 -0400401 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 b->as_u64 = tmp_b.as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400403 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400404 BV (value_free) (h, v, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405
Dave Barachc3799992016-08-15 11:12:27 -0400406unlock:
407 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408 h->writer_lock[0] = 0;
409 return rv;
410}
411
Dave Barachc3799992016-08-15 11:12:27 -0400412int BV (clib_bihash_search)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100413 (const BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400414 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415{
416 u64 hash;
417 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400418 BVT (clib_bihash_value) * v;
419 clib_bihash_bucket_t *b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500420 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700421
Dave Barachc3799992016-08-15 11:12:27 -0400422 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Dave Barachc3799992016-08-15 11:12:27 -0400424 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425
Dave Barachc3799992016-08-15 11:12:27 -0400426 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 b = &h->buckets[bucket_index];
428
429 if (b->offset == 0)
430 return -1;
431
432 hash >>= h->log2_nbuckets;
433
Dave Barachc3799992016-08-15 11:12:27 -0400434 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500435 limit = BIHASH_KVP_PER_PAGE;
436 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
437 if (PREDICT_FALSE (b->linear_search))
438 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400439
Dave Barach5e6b9582016-12-12 15:37:29 -0500440 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 {
Dave Barachc3799992016-08-15 11:12:27 -0400442 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
443 {
444 *valuep = v->kvp[i];
445 return 0;
446 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447 }
448 return -1;
449}
450
Dave Barachc3799992016-08-15 11:12:27 -0400451u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452{
Dave Barachc3799992016-08-15 11:12:27 -0400453 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454 int verbose = va_arg (*args, int);
Dave Barachc3799992016-08-15 11:12:27 -0400455 clib_bihash_bucket_t *b;
456 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457 int i, j, k;
458 u64 active_elements = 0;
459
460 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400461
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 for (i = 0; i < h->nbuckets; i++)
463 {
Dave Barachc3799992016-08-15 11:12:27 -0400464 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700465 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400466 {
467 if (verbose > 1)
468 s = format (s, "[%d]: empty\n", i);
469 continue;
470 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
472 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400473 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500474 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
475 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400476 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Dave Barachc3799992016-08-15 11:12:27 -0400478 v = BV (clib_bihash_get_value) (h, b->offset);
479 for (j = 0; j < (1 << b->log2_pages); j++)
480 {
481 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
482 {
483 if (BV (clib_bihash_is_free) (&v->kvp[k]))
484 {
485 if (verbose > 1)
486 s = format (s, " %d: empty\n",
487 j * BIHASH_KVP_PER_PAGE + k);
488 continue;
489 }
490 if (verbose)
491 {
492 s = format (s, " %d: %U\n",
493 j * BIHASH_KVP_PER_PAGE + k,
494 BV (format_bihash_kvp), &(v->kvp[k]));
495 }
496 active_elements++;
497 }
498 v++;
499 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 }
501
502 s = format (s, " %lld active elements\n", active_elements);
503 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barach5e6b9582016-12-12 15:37:29 -0500504 s = format (s, " %d linear search buckets\n", h->linear_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
506 return s;
507}
508
Dave Barachc3799992016-08-15 11:12:27 -0400509void BV (clib_bihash_foreach_key_value_pair)
510 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700511{
512 int i, j, k;
Dave Barachc3799992016-08-15 11:12:27 -0400513 clib_bihash_bucket_t *b;
514 BVT (clib_bihash_value) * v;
515 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
516
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 for (i = 0; i < h->nbuckets; i++)
518 {
Dave Barachc3799992016-08-15 11:12:27 -0400519 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400521 continue;
522
523 v = BV (clib_bihash_get_value) (h, b->offset);
524 for (j = 0; j < (1 << b->log2_pages); j++)
525 {
526 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
527 {
528 if (BV (clib_bihash_is_free) (&v->kvp[k]))
529 continue;
530
531 (*fp) (&v->kvp[k], arg);
532 }
533 v++;
534 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 }
536}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400537
Chris Luke16bcf7d2016-09-01 14:31:46 -0400538/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400539
540/*
541 * fd.io coding-style-patch-verification: ON
542 *
543 * Local Variables:
544 * eval: (c-set-style "gnu")
545 * End:
546 */