blob: 7c817a205890aa2ed0099ef9bd85ac0699ad77c9 [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);
58 vec_validate_aligned (rv, (1 << log2_pages) - 1, CLIB_CACHE_LINE_BYTES);
59 clib_mem_set_heap (oldheap);
60 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 }
Dave Barachc3799992016-08-15 11:12:27 -040062 rv = h->freelists[log2_pages];
63 h->freelists[log2_pages] = rv->next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070064
Dave Barachc3799992016-08-15 11:12:27 -040065initialize:
66 ASSERT (rv);
67 ASSERT (vec_len (rv) == (1 << log2_pages));
68 /*
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 Barachc3799992016-08-15 11:12:27 -040078BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v)
Ed Warnickecb9cada2015-12-08 15:45:58 -070079{
Dave Barachc3799992016-08-15 11:12:27 -040080 u32 log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -070081
Dave Barachc3799992016-08-15 11:12:27 -040082 ASSERT (h->writer_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070083
Dave Barachc3799992016-08-15 11:12:27 -040084 log2_pages = min_log2 (vec_len (v));
Ed Warnickecb9cada2015-12-08 15:45:58 -070085
Dave Barachc3799992016-08-15 11:12:27 -040086 ASSERT (vec_len (h->freelists) > log2_pages);
87
88 v->next_free = h->freelists[log2_pages];
89 h->freelists[log2_pages] = v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090}
91
92static inline void
Dave Barachc3799992016-08-15 11:12:27 -040093BV (make_working_copy) (BVT (clib_bihash) * h, clib_bihash_bucket_t * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070094{
Dave Barachc3799992016-08-15 11:12:27 -040095 BVT (clib_bihash_value) * v;
96 clib_bihash_bucket_t working_bucket __attribute__ ((aligned (8)));
97 void *oldheap;
98 BVT (clib_bihash_value) * working_copy;
99 u32 cpu_number = os_get_cpu_number ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 if (cpu_number >= vec_len (h->working_copies))
102 {
103 oldheap = clib_mem_set_heap (h->mheap);
104 vec_validate (h->working_copies, cpu_number);
105 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 */
113 working_copy = h->working_copies[cpu_number];
114
115 h->saved_bucket.as_u64 = b->as_u64;
116 oldheap = clib_mem_set_heap (h->mheap);
117
Dave Barachc3799992016-08-15 11:12:27 -0400118 if ((1 << b->log2_pages) > vec_len (working_copy))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 {
Dave Barachc3799992016-08-15 11:12:27 -0400120 vec_validate_aligned (working_copy, (1 << b->log2_pages) - 1,
121 sizeof (u64));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700122 h->working_copies[cpu_number] = working_copy;
123 }
124
Dave Barachc3799992016-08-15 11:12:27 -0400125 _vec_len (working_copy) = 1 << b->log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 clib_mem_set_heap (oldheap);
127
Dave Barachc3799992016-08-15 11:12:27 -0400128 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
Dave Barachc3799992016-08-15 11:12:27 -0400130 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700131 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400132 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
133 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 b->as_u64 = working_bucket.as_u64;
135 h->working_copies[cpu_number] = working_copy;
136}
137
Dave Barachc3799992016-08-15 11:12:27 -0400138static
139BVT (clib_bihash_value) *
140BV (split_and_rehash)
141 (BVT (clib_bihash) * h,
142 BVT (clib_bihash_value) * old_values, u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach5e6b9582016-12-12 15:37:29 -0500144 BVT (clib_bihash_value) * new_values, *new_v;
145 int i, j, length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146
Dave Barachc3799992016-08-15 11:12:27 -0400147 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500148 length = vec_len (old_values) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149
Dave Barach5e6b9582016-12-12 15:37:29 -0500150 for (i = 0; i < length; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151 {
152 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400153
Dave Barach5e6b9582016-12-12 15:37:29 -0500154 /* Entry not in use? Forget it */
155 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
156 continue;
157
158 /* rehash the item onto its new home-page */
159 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
160 new_hash >>= h->log2_nbuckets;
161 new_hash &= (1 << new_log2_pages) - 1;
162 new_v = &new_values[new_hash];
163
164 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400166 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500167 /* Empty slot */
168 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400169 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500170 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
171 sizeof (new_v->kvp[j]));
172 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400173 }
Dave Barachc3799992016-08-15 11:12:27 -0400174 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500175 /* Crap. Tell caller to try again */
176 BV (value_free) (h, new_values);
177 return 0;
178 doublebreak:;
179 }
180 return new_values;
181}
182
183static
184BVT (clib_bihash_value) *
185BV (split_and_rehash_linear)
186 (BVT (clib_bihash) * h,
187 BVT (clib_bihash_value) * old_values, u32 new_log2_pages)
188{
189 BVT (clib_bihash_value) * new_values;
190 int i, j, new_length;
191
192 new_values = BV (value_alloc) (h, new_log2_pages);
193 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
194
195 j = 0;
196 /* Across the old value array */
197 for (i = 0; i < vec_len (old_values) * BIHASH_KVP_PER_PAGE; i++)
198 {
199 /* Find a free slot in the new linear scan bucket */
200 for (; j < new_length; j++)
201 {
202 /* Old value in use? Forget it. */
203 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
204 goto doublebreak;
205
206 /* New value should never be in use */
207 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
208 {
209 /* Copy the old value and move along */
210 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
211 sizeof (new_values->kvp[j]));
212 j++;
213 goto doublebreak;
214 }
215 /* This should never happen... */
216 clib_warning ("BUG: linear rehash failed!");
217 BV (value_free) (h, new_values);
218 return 0;
219 }
220 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700221 }
222 return new_values;
223}
224
Dave Barachc3799992016-08-15 11:12:27 -0400225int BV (clib_bihash_add_del)
226 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227{
228 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400229 clib_bihash_bucket_t *b, tmp_b;
230 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700231 int rv = 0;
Dave Barach5e6b9582016-12-12 15:37:29 -0500232 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700233 u64 hash, new_hash;
234 u32 new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400235 u32 cpu_number = os_get_cpu_number ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500236 int mark_bucket_linear;
237 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700238
Dave Barachc3799992016-08-15 11:12:27 -0400239 hash = BV (clib_bihash_hash) (add_v);
240
241 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700242 b = &h->buckets[bucket_index];
243
244 hash >>= h->log2_nbuckets;
245
246 while (__sync_lock_test_and_set (h->writer_lock, 1))
Dave Barachc3799992016-08-15 11:12:27 -0400247 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700248
249 /* First elt in the bucket? */
250 if (b->offset == 0)
251 {
252 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400253 {
254 rv = -1;
255 goto unlock;
256 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700257
Dave Barachc3799992016-08-15 11:12:27 -0400258 v = BV (value_alloc) (h, 0);
259 *v->kvp = *add_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700260 tmp_b.as_u64 = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400261 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262
263 b->as_u64 = tmp_b.as_u64;
264 goto unlock;
265 }
266
Dave Barachc3799992016-08-15 11:12:27 -0400267 BV (make_working_copy) (h, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268
Dave Barachc3799992016-08-15 11:12:27 -0400269 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500270
271 limit = BIHASH_KVP_PER_PAGE;
272 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
273 if (b->linear_search)
274 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400275
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 if (is_add)
277 {
Dave Barachc3799992016-08-15 11:12:27 -0400278 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279 * For obvious (in hindsight) reasons, see if we're supposed to
280 * replace an existing key, then look for an empty slot.
281 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500282 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400283 {
284 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
285 {
286 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
287 CLIB_MEMORY_BARRIER ();
288 /* Restore the previous (k,v) pairs */
289 b->as_u64 = h->saved_bucket.as_u64;
290 goto unlock;
291 }
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 (BV (clib_bihash_is_free) (&(v->kvp[i])))
296 {
297 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
298 CLIB_MEMORY_BARRIER ();
299 b->as_u64 = h->saved_bucket.as_u64;
300 goto unlock;
301 }
302 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 /* no room at the inn... split case... */
304 }
305 else
306 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500307 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400308 {
309 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
310 {
311 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
312 CLIB_MEMORY_BARRIER ();
313 b->as_u64 = h->saved_bucket.as_u64;
314 goto unlock;
315 }
316 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700317 rv = -3;
318 b->as_u64 = h->saved_bucket.as_u64;
319 goto unlock;
320 }
321
322 new_log2_pages = h->saved_bucket.log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500323 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Ed Warnickecb9cada2015-12-08 15:45:58 -0700325 working_copy = h->working_copies[cpu_number];
Dave Barach5e6b9582016-12-12 15:37:29 -0500326 resplit_once = 0;
327
Dave Barachc3799992016-08-15 11:12:27 -0400328 new_v = BV (split_and_rehash) (h, working_copy, new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700329 if (new_v == 0)
330 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500331 try_resplit:
332 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700333 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500334 /* Try re-splitting. If that fails, fall back to linear search */
335 new_v = BV (split_and_rehash) (h, working_copy, new_log2_pages);
336 if (new_v == 0)
337 {
338 mark_linear:
339 new_log2_pages--;
340 /* pinned collisions, use linear search */
341 new_v =
342 BV (split_and_rehash_linear) (h, working_copy, new_log2_pages);
343 mark_bucket_linear = 1;
344 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345 }
346
347 /* Try to add the new entry */
348 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400349 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500350 limit = BIHASH_KVP_PER_PAGE;
351 if (mark_bucket_linear)
352 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700353 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500354 new_hash &= (1 << new_log2_pages) - 1;
355 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400356
Dave Barach5e6b9582016-12-12 15:37:29 -0500357 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700358 {
Dave Barachc3799992016-08-15 11:12:27 -0400359 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
360 {
361 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
362 goto expand_ok;
363 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700364 }
365 /* Crap. Try again */
Dave Barachc3799992016-08-15 11:12:27 -0400366 BV (value_free) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500367 /*
368 * If we've already doubled the size of the bucket once,
369 * fall back to linear search now.
370 */
371 if (resplit_once)
372 goto mark_linear;
373 else
374 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700375
Dave Barachc3799992016-08-15 11:12:27 -0400376expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500377 /* Keep track of the number of linear-scan buckets */
378 if (tmp_b.linear_search ^ mark_bucket_linear)
379 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
380
381 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400382 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500383 tmp_b.linear_search = mark_bucket_linear;
Dave Barachc3799992016-08-15 11:12:27 -0400384 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700385 b->as_u64 = tmp_b.as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400386 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
387 BV (value_free) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700388
Dave Barachc3799992016-08-15 11:12:27 -0400389unlock:
390 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391 h->writer_lock[0] = 0;
392 return rv;
393}
394
Dave Barachc3799992016-08-15 11:12:27 -0400395int BV (clib_bihash_search)
Neale Ranns0bfe5d82016-08-25 15:29:12 +0100396 (const BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400397 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398{
399 u64 hash;
400 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400401 BVT (clib_bihash_value) * v;
402 clib_bihash_bucket_t *b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500403 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
Dave Barachc3799992016-08-15 11:12:27 -0400405 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406
Dave Barachc3799992016-08-15 11:12:27 -0400407 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700408
Dave Barachc3799992016-08-15 11:12:27 -0400409 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 b = &h->buckets[bucket_index];
411
412 if (b->offset == 0)
413 return -1;
414
415 hash >>= h->log2_nbuckets;
416
Dave Barachc3799992016-08-15 11:12:27 -0400417 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500418 limit = BIHASH_KVP_PER_PAGE;
419 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
420 if (PREDICT_FALSE (b->linear_search))
421 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400422
Dave Barach5e6b9582016-12-12 15:37:29 -0500423 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424 {
Dave Barachc3799992016-08-15 11:12:27 -0400425 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
426 {
427 *valuep = v->kvp[i];
428 return 0;
429 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430 }
431 return -1;
432}
433
Dave Barachc3799992016-08-15 11:12:27 -0400434u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435{
Dave Barachc3799992016-08-15 11:12:27 -0400436 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437 int verbose = va_arg (*args, int);
Dave Barachc3799992016-08-15 11:12:27 -0400438 clib_bihash_bucket_t *b;
439 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700440 int i, j, k;
441 u64 active_elements = 0;
442
443 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400444
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 for (i = 0; i < h->nbuckets; i++)
446 {
Dave Barachc3799992016-08-15 11:12:27 -0400447 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700448 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400449 {
450 if (verbose > 1)
451 s = format (s, "[%d]: empty\n", i);
452 continue;
453 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
455 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400456 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500457 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
458 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400459 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
Dave Barachc3799992016-08-15 11:12:27 -0400461 v = BV (clib_bihash_get_value) (h, b->offset);
462 for (j = 0; j < (1 << b->log2_pages); j++)
463 {
464 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
465 {
466 if (BV (clib_bihash_is_free) (&v->kvp[k]))
467 {
468 if (verbose > 1)
469 s = format (s, " %d: empty\n",
470 j * BIHASH_KVP_PER_PAGE + k);
471 continue;
472 }
473 if (verbose)
474 {
475 s = format (s, " %d: %U\n",
476 j * BIHASH_KVP_PER_PAGE + k,
477 BV (format_bihash_kvp), &(v->kvp[k]));
478 }
479 active_elements++;
480 }
481 v++;
482 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700483 }
484
485 s = format (s, " %lld active elements\n", active_elements);
486 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barach5e6b9582016-12-12 15:37:29 -0500487 s = format (s, " %d linear search buckets\n", h->linear_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
489 return s;
490}
491
Dave Barachc3799992016-08-15 11:12:27 -0400492void BV (clib_bihash_foreach_key_value_pair)
493 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494{
495 int i, j, k;
Dave Barachc3799992016-08-15 11:12:27 -0400496 clib_bihash_bucket_t *b;
497 BVT (clib_bihash_value) * v;
498 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
499
Ed Warnickecb9cada2015-12-08 15:45:58 -0700500 for (i = 0; i < h->nbuckets; i++)
501 {
Dave Barachc3799992016-08-15 11:12:27 -0400502 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700503 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400504 continue;
505
506 v = BV (clib_bihash_get_value) (h, b->offset);
507 for (j = 0; j < (1 << b->log2_pages); j++)
508 {
509 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
510 {
511 if (BV (clib_bihash_is_free) (&v->kvp[k]))
512 continue;
513
514 (*fp) (&v->kvp[k], arg);
515 }
516 v++;
517 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 }
519}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400520
Chris Luke16bcf7d2016-09-01 14:31:46 -0400521/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400522
523/*
524 * fd.io coding-style-patch-verification: ON
525 *
526 * Local Variables:
527 * eval: (c-set-style "gnu")
528 * End:
529 */