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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 /** @cond DOCUMENTATION_IS_IN_BIHASH_DOC_H */
18 #ifndef MAP_HUGE_SHIFT
19 #define MAP_HUGE_SHIFT 26
22 static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
26 /* Round to an even number of cache lines */
27 nbytes += CLIB_CACHE_LINE_BYTES - 1;
28 nbytes &= ~(CLIB_CACHE_LINE_BYTES - 1);
30 rv = alloc_arena_next (h);
31 alloc_arena_next (h) += nbytes;
33 if (alloc_arena_next (h) > alloc_arena_size (h))
36 if (alloc_arena_next (h) > alloc_arena_mapped (h))
39 uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
40 int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
41 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB |
42 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
44 /* new allocation is 25% of existing one */
45 if (alloc_arena_mapped (h) >> 2 > alloc)
46 alloc = alloc_arena_mapped (h) >> 2;
48 /* round allocation to page size */
49 alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
51 base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h));
53 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
55 /* fallback - maybe we are still able to allocate normal pages */
57 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
62 alloc_arena_mapped (h) += alloc;
65 return (void *) (uword) (rv + alloc_arena (h));
68 static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
72 alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size,
73 BIHASH_LOG2_HUGEPAGE_SIZE);
74 if (alloc_arena (h) == ~0)
76 alloc_arena_next (h) = 0;
77 alloc_arena_size (h) = h->memory_size;
78 alloc_arena_mapped (h) = 0;
80 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
82 if (BIHASH_KVP_AT_BUCKET_LEVEL)
84 h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv));
86 h->buckets = BV (alloc_aligned) (h, bucket_size);
88 if (BIHASH_KVP_AT_BUCKET_LEVEL)
91 BVT (clib_bihash_bucket) * b;
95 for (i = 0; i < h->nbuckets; i++)
97 b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1));
99 /* Mark all elements free */
100 clib_memset ((b + 1), 0xff,
101 BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv)));
103 /* Compute next bucket start address */
104 b = (void *) (((uword) b) + sizeof (*b) +
105 (BIHASH_KVP_PER_PAGE *
106 sizeof (BVT (clib_bihash_kv))));
109 CLIB_MEMORY_STORE_BARRIER ();
113 void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a)
117 BVT (clib_bihash) * h = a->h;
119 a->nbuckets = 1 << (max_log2 (a->nbuckets));
121 h->name = (u8 *) a->name;
122 h->nbuckets = a->nbuckets;
123 h->log2_nbuckets = max_log2 (a->nbuckets);
124 h->memory_size = a->memory_size;
126 h->fmt_fn = a->fmt_fn;
131 * Make sure the requested size is rational. The max table
132 * size without playing the alignment card is 64 Gbytes.
133 * If someone starts complaining that's not enough, we can shift
134 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
136 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
138 /* Add this hash table to the list */
139 if (a->dont_add_to_all_bihash_list == 0)
141 for (i = 0; i < vec_len (clib_all_bihashes); i++)
142 if (clib_all_bihashes[i] == h)
144 oldheap = clib_all_bihash_set_heap ();
145 vec_add1 (clib_all_bihashes, (void *) h);
146 clib_mem_set_heap (oldheap);
151 clib_mem_free ((void *) h->alloc_lock);
154 * Set up the lock now, so we can use it to make the first add
157 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
158 CLIB_CACHE_LINE_BYTES);
159 h->alloc_lock[0] = 0;
161 #if BIHASH_LAZY_INSTANTIATE
162 if (a->instantiate_immediately)
164 BV (clib_bihash_instantiate) (h);
167 void BV (clib_bihash_init)
168 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
170 BVT (clib_bihash_init2_args) _a, *a = &_a;
172 memset (a, 0, sizeof (*a));
176 a->nbuckets = nbuckets;
177 a->memory_size = memory_size;
179 BV (clib_bihash_init2) (a);
183 #if !defined (MFD_ALLOW_SEALING)
184 #define MFD_ALLOW_SEALING 0x0002U
187 void BV (clib_bihash_master_init_svm)
188 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
192 vec_header_t *freelist_vh;
195 ASSERT (memory_size < (1ULL << 32));
196 /* Set up for memfd sharing */
197 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
199 clib_unix_warning ("memfd_create");
203 if (ftruncate (fd, memory_size) < 0)
205 clib_unix_warning ("ftruncate");
209 /* Not mission-critical, complain and continue */
210 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
211 clib_unix_warning ("fcntl (F_ADD_SEALS)");
213 mmap_addr = mmap (0, memory_size,
214 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
216 if (mmap_addr == MAP_FAILED)
218 clib_unix_warning ("mmap failed");
222 h->sh = (void *) mmap_addr;
224 nbuckets = 1 << (max_log2 (nbuckets));
226 h->name = (u8 *) name;
227 h->sh->nbuckets = h->nbuckets = nbuckets;
228 h->log2_nbuckets = max_log2 (nbuckets);
230 alloc_arena (h) = (u64) (uword) mmap_addr;
231 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
232 alloc_arena_size (h) = memory_size;
234 bucket_size = nbuckets * sizeof (h->buckets[0]);
235 h->buckets = BV (alloc_aligned) (h, bucket_size);
236 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
238 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
239 h->alloc_lock[0] = 0;
241 h->sh->alloc_lock_as_u64 =
242 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
244 BV (alloc_aligned) (h,
245 sizeof (vec_header_t) +
246 BIHASH_FREELIST_LENGTH * sizeof (u64));
247 freelist_vh->len = BIHASH_FREELIST_LENGTH;
248 h->sh->freelists_as_u64 =
249 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
250 h->freelists = (void *) (freelist_vh->vector_data);
256 void BV (clib_bihash_slave_init_svm)
257 (BVT (clib_bihash) * h, char *name, int fd)
261 BVT (clib_bihash_shared_header) * sh;
263 /* Trial mapping, to learn the segment size */
264 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
265 if (mmap_addr == MAP_FAILED)
267 clib_unix_warning ("trial mmap failed");
271 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
273 memory_size = sh->alloc_arena_size;
275 munmap (mmap_addr, 4096);
277 /* Actual mapping, at the required size */
278 mmap_addr = mmap (0, memory_size,
279 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
281 if (mmap_addr == MAP_FAILED)
283 clib_unix_warning ("mmap failed");
289 h->sh = (void *) mmap_addr;
290 alloc_arena (h) = (u64) (uword) mmap_addr;
293 h->name = (u8 *) name;
294 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
295 h->nbuckets = h->sh->nbuckets;
296 h->log2_nbuckets = max_log2 (h->nbuckets);
298 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
299 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
302 #endif /* BIHASH_32_64_SVM */
304 void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
305 format_function_t * fmt_fn)
310 void BV (clib_bihash_free) (BVT (clib_bihash) * h)
314 if (PREDICT_FALSE (h->instantiated == 0))
315 goto never_initialized;
318 vec_free (h->working_copies);
319 vec_free (h->working_copy_lengths);
320 #if BIHASH_32_64_SVM == 0
321 vec_free (h->freelists);
324 (void) close (h->memfd);
326 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)), alloc_arena_size (h));
328 clib_memset (h, 0, sizeof (*h));
329 for (i = 0; i < vec_len (clib_all_bihashes); i++)
331 if ((void *) h == clib_all_bihashes[i])
333 vec_delete (clib_all_bihashes, 1, i);
337 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
342 BVT (clib_bihash_value) *
343 BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
345 BVT (clib_bihash_value) * rv = 0;
347 ASSERT (h->alloc_lock[0]);
350 ASSERT (log2_pages < vec_len (h->freelists));
353 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
355 vec_validate_init_empty (h->freelists, log2_pages, 0);
356 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
359 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
360 h->freelists[log2_pages] = rv->next_free_as_u64;
365 * Latest gcc complains that the length arg is zero
366 * if we replace (1<<log2_pages) with vec_len(rv).
369 clib_memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
374 BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
377 ASSERT (h->alloc_lock[0]);
379 ASSERT (vec_len (h->freelists) > log2_pages);
382 clib_memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
384 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
385 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
389 BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
391 BVT (clib_bihash_value) * v;
392 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
393 BVT (clib_bihash_value) * working_copy;
394 u32 thread_index = os_get_thread_index ();
395 int log2_working_copy_length;
397 ASSERT (h->alloc_lock[0]);
399 if (thread_index >= vec_len (h->working_copies))
401 vec_validate (h->working_copies, thread_index);
402 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
406 * working_copies are per-cpu so that near-simultaneous
407 * updates from multiple threads will not result in sporadic, spurious
410 working_copy = h->working_copies[thread_index];
411 log2_working_copy_length = h->working_copy_lengths[thread_index];
413 h->saved_bucket.as_u64 = b->as_u64;
415 if (b->log2_pages > log2_working_copy_length)
418 * It's not worth the bookkeeping to free working copies
420 * clib_mem_free (working_copy);
422 working_copy = BV (alloc_aligned)
423 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
424 h->working_copy_lengths[thread_index] = b->log2_pages;
425 h->working_copies[thread_index] = working_copy;
427 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
428 1ULL << b->log2_pages);
431 v = BV (clib_bihash_get_value) (h, b->offset);
433 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
434 working_bucket.as_u64 = b->as_u64;
435 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
436 CLIB_MEMORY_STORE_BARRIER ();
437 b->as_u64 = working_bucket.as_u64;
438 h->working_copies[thread_index] = working_copy;
442 BVT (clib_bihash_value) *
443 BV (split_and_rehash)
444 (BVT (clib_bihash) * h,
445 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
448 BVT (clib_bihash_value) * new_values, *new_v;
449 int i, j, length_in_kvs;
451 ASSERT (h->alloc_lock[0]);
453 new_values = BV (value_alloc) (h, new_log2_pages);
454 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
456 for (i = 0; i < length_in_kvs; i++)
460 /* Entry not in use? Forget it */
461 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
464 /* rehash the item onto its new home-page */
465 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
466 new_hash >>= h->log2_nbuckets;
467 new_hash &= (1 << new_log2_pages) - 1;
468 new_v = &new_values[new_hash];
470 /* Across the new home-page */
471 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
474 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
476 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
477 sizeof (new_v->kvp[j]));
481 /* Crap. Tell caller to try again */
482 BV (value_free) (h, new_values, new_log2_pages);
491 BVT (clib_bihash_value) *
492 BV (split_and_rehash_linear)
493 (BVT (clib_bihash) * h,
494 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
497 BVT (clib_bihash_value) * new_values;
498 int i, j, new_length, old_length;
500 ASSERT (h->alloc_lock[0]);
502 new_values = BV (value_alloc) (h, new_log2_pages);
503 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
504 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
507 /* Across the old value array */
508 for (i = 0; i < old_length; i++)
510 /* Find a free slot in the new linear scan bucket */
511 for (; j < new_length; j++)
513 /* Old value not in use? Forget it. */
514 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
517 /* New value should never be in use */
518 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
520 /* Copy the old value and move along */
521 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
522 sizeof (new_values->kvp[j]));
527 /* This should never happen... */
528 clib_warning ("BUG: linear rehash failed!");
529 BV (value_free) (h, new_values, new_log2_pages);
537 static_always_inline int BV (clib_bihash_add_del_inline_with_hash)
538 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
539 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
541 BVT (clib_bihash_bucket) * b, tmp_b;
542 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
545 u32 new_log2_pages, old_log2_pages;
546 u32 thread_index = os_get_thread_index ();
547 int mark_bucket_linear;
551 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
552 * Use the alloc_lock to protect the instantiate operation.
554 if (PREDICT_FALSE (h->instantiated == 0))
559 BV (clib_bihash_alloc_lock) (h);
560 if (h->instantiated == 0)
561 BV (clib_bihash_instantiate) (h);
562 BV (clib_bihash_alloc_unlock) (h);
565 b = BV (clib_bihash_get_bucket) (h, hash);
567 hash >>= h->log2_nbuckets;
569 BV (clib_bihash_lock_bucket) (b);
571 /* First elt in the bucket? */
572 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
576 BV (clib_bihash_unlock_bucket) (b);
580 BV (clib_bihash_alloc_lock) (h);
581 v = BV (value_alloc) (h, 0);
582 BV (clib_bihash_alloc_unlock) (h);
585 tmp_b.as_u64 = 0; /* clears bucket lock */
586 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
588 CLIB_MEMORY_STORE_BARRIER ();
590 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
591 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
596 /* WARNING: we're still looking at the live copy... */
597 limit = BIHASH_KVP_PER_PAGE;
598 v = BV (clib_bihash_get_value) (h, b->offset);
600 if (b->linear_search)
601 limit <<= b->log2_pages;
603 v += hash & ((1 << b->log2_pages) - 1);
608 * Because reader threads are looking at live data,
609 * we have to be extra careful. Readers do NOT hold the
610 * bucket lock. We need to be SLOWER than a search, past the
611 * point where readers CHECK the bucket lock.
615 * For obvious (in hindsight) reasons, see if we're supposed to
616 * replace an existing key, then look for an empty slot.
618 for (i = 0; i < limit; i++)
620 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
622 /* Add but do not overwrite? */
625 BV (clib_bihash_unlock_bucket) (b);
629 clib_memcpy_fast (&(v->kvp[i].value),
630 &add_v->value, sizeof (add_v->value));
631 BV (clib_bihash_unlock_bucket) (b);
632 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
637 * Look for an empty slot. If found, use it
639 for (i = 0; i < limit; i++)
641 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
644 * Copy the value first, so that if a reader manages
645 * to match the new key, the value will be right...
647 clib_memcpy_fast (&(v->kvp[i].value),
648 &add_v->value, sizeof (add_v->value));
649 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
650 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
651 sizeof (add_v->key));
653 ASSERT (b->refcnt > 0);
654 BV (clib_bihash_unlock_bucket) (b);
655 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
659 /* look for stale data to overwrite */
662 for (i = 0; i < limit; i++)
664 if (is_stale_cb (&(v->kvp[i]), arg))
666 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
667 CLIB_MEMORY_STORE_BARRIER ();
668 BV (clib_bihash_unlock_bucket) (b);
669 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
674 /* Out of space in this bucket, split the bucket... */
676 else /* delete case */
678 for (i = 0; i < limit; i++)
680 /* Found the key? Kill it... */
681 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
683 clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
684 /* Is the bucket empty? */
685 if (PREDICT_TRUE (b->refcnt > 1))
688 /* Switch back to the bucket-level kvp array? */
689 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
690 && b->log2_pages > 0)
692 tmp_b.as_u64 = b->as_u64;
693 b->offset = BV (clib_bihash_get_offset)
694 (h, (void *) (b + 1));
695 b->linear_search = 0;
697 /* Clean up the bucket-level kvp array */
698 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
699 sizeof (BVT (clib_bihash_kv)));
700 CLIB_MEMORY_STORE_BARRIER ();
701 BV (clib_bihash_unlock_bucket) (b);
702 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
703 goto free_backing_store;
706 CLIB_MEMORY_STORE_BARRIER ();
707 BV (clib_bihash_unlock_bucket) (b);
708 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
711 else /* yes, free it */
713 /* Save old bucket value, need log2_pages to free it */
714 tmp_b.as_u64 = b->as_u64;
716 /* Kill and unlock the bucket */
720 /* And free the backing storage */
721 BV (clib_bihash_alloc_lock) (h);
722 /* Note: v currently points into the middle of the bucket */
723 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
724 BV (value_free) (h, v, tmp_b.log2_pages);
725 BV (clib_bihash_alloc_unlock) (h);
726 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
733 BV (clib_bihash_unlock_bucket) (b);
737 /* Move readers to a (locked) temp copy of the bucket */
738 BV (clib_bihash_alloc_lock) (h);
739 BV (make_working_copy) (h, b);
741 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
743 old_log2_pages = h->saved_bucket.log2_pages;
744 new_log2_pages = old_log2_pages + 1;
745 mark_bucket_linear = 0;
746 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
747 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
749 working_copy = h->working_copies[thread_index];
751 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
753 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
760 /* Try re-splitting. If that fails, fall back to linear search */
761 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
767 /* pinned collisions, use linear search */
769 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
771 mark_bucket_linear = 1;
772 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
774 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
775 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
779 /* Try to add the new entry */
781 new_hash = BV (clib_bihash_hash) (add_v);
782 limit = BIHASH_KVP_PER_PAGE;
783 if (mark_bucket_linear)
784 limit <<= new_log2_pages;
785 new_hash >>= h->log2_nbuckets;
786 new_hash &= (1 << new_log2_pages) - 1;
787 new_v += mark_bucket_linear ? 0 : new_hash;
789 for (i = 0; i < limit; i++)
791 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
793 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
798 /* Crap. Try again */
799 BV (value_free) (h, save_new_v, new_log2_pages);
801 * If we've already doubled the size of the bucket once,
802 * fall back to linear search now.
810 tmp_b.log2_pages = new_log2_pages;
811 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
812 tmp_b.linear_search = mark_bucket_linear;
813 #if BIHASH_KVP_AT_BUCKET_LEVEL
814 /* Compensate for permanent refcount bump at the bucket level */
815 if (new_log2_pages > 0)
817 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
818 ASSERT (tmp_b.refcnt > 0);
820 CLIB_MEMORY_STORE_BARRIER ();
821 b->as_u64 = tmp_b.as_u64;
823 #if BIHASH_KVP_AT_BUCKET_LEVEL
824 if (h->saved_bucket.log2_pages > 0)
828 /* free the old bucket, except at the bucket level if so configured */
829 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
830 BV (value_free) (h, v, h->saved_bucket.log2_pages);
832 #if BIHASH_KVP_AT_BUCKET_LEVEL
837 BV (clib_bihash_alloc_unlock) (h);
841 static_always_inline int BV (clib_bihash_add_del_inline)
842 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
843 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
845 u64 hash = BV (clib_bihash_hash) (add_v);
846 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
850 int BV (clib_bihash_add_del)
851 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
853 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
856 int BV (clib_bihash_add_or_overwrite_stale)
857 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
858 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
860 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
863 int BV (clib_bihash_search)
864 (BVT (clib_bihash) * h,
865 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
868 BVT (clib_bihash_value) * v;
869 BVT (clib_bihash_bucket) * b;
874 #if BIHASH_LAZY_INSTANTIATE
875 if (PREDICT_FALSE (alloc_arena (h) == 0))
879 hash = BV (clib_bihash_hash) (search_key);
881 b = BV (clib_bihash_get_bucket) (h, hash);
883 if (BV (clib_bihash_bucket_is_empty) (b))
886 if (PREDICT_FALSE (b->lock))
888 volatile BVT (clib_bihash_bucket) * bv = b;
893 hash >>= h->log2_nbuckets;
895 v = BV (clib_bihash_get_value) (h, b->offset);
896 limit = BIHASH_KVP_PER_PAGE;
897 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
898 if (PREDICT_FALSE (b->linear_search))
899 limit <<= b->log2_pages;
901 for (i = 0; i < limit; i++)
903 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
912 u8 *BV (format_bihash) (u8 * s, va_list * args)
914 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
915 int verbose = va_arg (*args, int);
916 BVT (clib_bihash_bucket) * b;
917 BVT (clib_bihash_value) * v;
919 u64 active_elements = 0;
920 u64 active_buckets = 0;
921 u64 linear_buckets = 0;
924 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
926 #if BIHASH_LAZY_INSTANTIATE
927 if (PREDICT_FALSE (alloc_arena (h) == 0))
928 return format (s, "[empty, uninitialized]");
931 for (i = 0; i < h->nbuckets; i++)
933 b = BV (clib_bihash_get_bucket) (h, i);
934 if (BV (clib_bihash_bucket_is_empty) (b))
937 s = format (s, "[%d]: empty\n", i);
943 if (b->linear_search)
949 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
950 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
953 v = BV (clib_bihash_get_value) (h, b->offset);
954 for (j = 0; j < (1 << b->log2_pages); j++)
956 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
958 if (BV (clib_bihash_is_free) (&v->kvp[k]))
961 s = format (s, " %d: empty\n",
962 j * BIHASH_KVP_PER_PAGE + k);
969 s = format (s, " %d: %U\n",
970 j * BIHASH_KVP_PER_PAGE + k,
971 h->fmt_fn, &(v->kvp[k]), verbose);
975 s = format (s, " %d: %U\n",
976 j * BIHASH_KVP_PER_PAGE + k,
977 BV (format_bihash_kvp), &(v->kvp[k]));
986 s = format (s, " %lld active elements %lld active buckets\n",
987 active_elements, active_buckets);
988 s = format (s, " %d free lists\n", vec_len (h->freelists));
990 for (i = 0; i < vec_len (h->freelists); i++)
993 BVT (clib_bihash_value) * free_elt;
994 u64 free_elt_as_u64 = h->freelists[i];
996 while (free_elt_as_u64)
998 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
1000 free_elt_as_u64 = free_elt->next_free_as_u64;
1003 if (nfree || verbose)
1004 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
1007 s = format (s, " %lld linear search buckets\n", linear_buckets);
1008 used_bytes = alloc_arena_next (h);
1010 " arena: base %llx, next %llx\n"
1011 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1012 alloc_arena (h), alloc_arena_next (h),
1013 used_bytes, used_bytes >> 20,
1014 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1018 void BV (clib_bihash_foreach_key_value_pair)
1019 (BVT (clib_bihash) * h,
1020 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
1023 BVT (clib_bihash_bucket) * b;
1024 BVT (clib_bihash_value) * v;
1027 #if BIHASH_LAZY_INSTANTIATE
1028 if (PREDICT_FALSE (alloc_arena (h) == 0))
1032 for (i = 0; i < h->nbuckets; i++)
1034 b = BV (clib_bihash_get_bucket) (h, i);
1035 if (BV (clib_bihash_bucket_is_empty) (b))
1038 v = BV (clib_bihash_get_value) (h, b->offset);
1039 for (j = 0; j < (1 << b->log2_pages); j++)
1041 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1043 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1046 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1049 * In case the callback deletes the last entry in the bucket...
1051 if (BV (clib_bihash_bucket_is_empty) (b))
1064 * fd.io coding-style-patch-verification: ON
1067 * eval: (c-set-style "gnu")