blob: 555c2e00cfb6228a07f1962fae4e0ada66d4660c [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 Barach16e4a4a2020-04-16 12:00:14 -040018#ifndef MAP_HUGE_SHIFT
19#define MAP_HUGE_SHIFT 26
20#endif
21
Damjan Marion2454de22020-09-26 19:32:34 +020022#ifndef BIIHASH_MIN_ALLOC_LOG2_PAGES
23#define BIIHASH_MIN_ALLOC_LOG2_PAGES 10
24#endif
25
Dave Barach97f5af02018-02-22 09:48:45 -050026static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
27{
28 uword rv;
29
30 /* Round to an even number of cache lines */
Damjan Marion2454de22020-09-26 19:32:34 +020031 nbytes = round_pow2 (nbytes, CLIB_CACHE_LINE_BYTES);
32
33 if (BIHASH_USE_HEAP)
34 {
35 void *rv, *oldheap;
36 uword page_sz = sizeof (BVT (clib_bihash_value));
37 uword chunk_sz = round_pow2 (page_sz << BIIHASH_MIN_ALLOC_LOG2_PAGES,
38 CLIB_CACHE_LINE_BYTES);
39
40 BVT (clib_bihash_alloc_chunk) * chunk = h->chunks;
41
42 /* if there is enough space in the currenrt chunk */
43 if (chunk && chunk->bytes_left >= nbytes)
44 {
45 rv = chunk->next_alloc;
46 chunk->bytes_left -= nbytes;
47 chunk->next_alloc += nbytes;
48 return rv;
49 }
50
51 /* requested allocation is bigger than chunk size */
52 if (nbytes >= chunk_sz)
53 {
54 oldheap = clib_mem_set_heap (h->heap);
55 chunk = clib_mem_alloc_aligned (nbytes + sizeof (*chunk),
56 CLIB_CACHE_LINE_BYTES);
57 clib_mem_set_heap (oldheap);
58 clib_memset_u8 (chunk, 0, sizeof (*chunk));
59 chunk->size = nbytes;
60 rv = (u8 *) (chunk + 1);
61 if (h->chunks)
62 {
63 /* take 2nd place in the list */
64 chunk->next = h->chunks->next;
65 chunk->prev = h->chunks;
66 h->chunks->next = chunk;
67 if (chunk->next)
68 chunk->next->prev = chunk;
69 }
70 else
71 h->chunks = chunk;
72
73 return rv;
74 }
75
76 oldheap = clib_mem_set_heap (h->heap);
77 chunk = clib_mem_alloc_aligned (chunk_sz + sizeof (*chunk),
78 CLIB_CACHE_LINE_BYTES);
79 clib_mem_set_heap (oldheap);
80 chunk->size = chunk_sz;
81 chunk->bytes_left = chunk_sz;
82 chunk->next_alloc = (u8 *) (chunk + 1);
83 chunk->next = h->chunks;
84 chunk->prev = 0;
85 if (chunk->next)
86 chunk->next->prev = chunk;
87 h->chunks = chunk;
88 rv = chunk->next_alloc;
89 chunk->bytes_left -= nbytes;
90 chunk->next_alloc += nbytes;
91 return rv;
92 }
Dave Barach97f5af02018-02-22 09:48:45 -050093
Dave Barach9466c452018-08-24 17:21:14 -040094 rv = alloc_arena_next (h);
95 alloc_arena_next (h) += nbytes;
Dave Barach97f5af02018-02-22 09:48:45 -050096
Andreas Schultzb4b525e2019-07-19 11:14:50 +020097 if (alloc_arena_next (h) > alloc_arena_size (h))
Dave Barach97f5af02018-02-22 09:48:45 -050098 os_out_of_memory ();
99
Dave Barach16e4a4a2020-04-16 12:00:14 -0400100 if (alloc_arena_next (h) > alloc_arena_mapped (h))
101 {
102 void *base, *rv;
103 uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
104 int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marione6db7752020-05-24 20:43:10 +0200105 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
Dave Barach16e4a4a2020-04-16 12:00:14 -0400106 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
107
108 /* new allocation is 25% of existing one */
109 if (alloc_arena_mapped (h) >> 2 > alloc)
110 alloc = alloc_arena_mapped (h) >> 2;
111
112 /* round allocation to page size */
113 alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
114
115 base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h));
116
117 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
118
119 /* fallback - maybe we are still able to allocate normal pages */
Damjan Marion6183cf42020-05-27 16:43:35 +0200120 if (rv == MAP_FAILED || mlock (base, alloc) != 0)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400121 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
122
123 if (rv == MAP_FAILED)
124 os_out_of_memory ();
125
126 alloc_arena_mapped (h) += alloc;
127 }
128
Dave Barachffb14b92018-09-11 17:20:23 -0400129 return (void *) (uword) (rv + alloc_arena (h));
Dave Barach97f5af02018-02-22 09:48:45 -0500130}
131
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800132static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
Dave Barach32dcd3b2019-07-08 12:25:38 -0400133{
134 uword bucket_size;
135
Damjan Marion2454de22020-09-26 19:32:34 +0200136 if (BIHASH_USE_HEAP)
137 {
138 h->heap = clib_mem_get_heap ();
139 h->chunks = 0;
140 alloc_arena (h) = (uword) clib_mem_get_heap_base (h->heap);
141 }
142 else
143 {
144 alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size,
145 BIHASH_LOG2_HUGEPAGE_SIZE);
146 if (alloc_arena (h) == ~0)
147 os_out_of_memory ();
148 alloc_arena_next (h) = 0;
149 alloc_arena_size (h) = h->memory_size;
150 alloc_arena_mapped (h) = 0;
151 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400152
153 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400154
155 if (BIHASH_KVP_AT_BUCKET_LEVEL)
156 bucket_size +=
157 h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv));
158
Dave Barach32dcd3b2019-07-08 12:25:38 -0400159 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200160 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400161
162 if (BIHASH_KVP_AT_BUCKET_LEVEL)
163 {
164 int i;
165 BVT (clib_bihash_bucket) * b;
166
167 b = h->buckets;
168
169 for (i = 0; i < h->nbuckets; i++)
170 {
171 b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1));
172 b->refcnt = 1;
173 /* Mark all elements free */
Damjan Marion2454de22020-09-26 19:32:34 +0200174 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
175 sizeof (BVT (clib_bihash_kv)));
Dave Barach16e4a4a2020-04-16 12:00:14 -0400176
177 /* Compute next bucket start address */
178 b = (void *) (((uword) b) + sizeof (*b) +
179 (BIHASH_KVP_PER_PAGE *
180 sizeof (BVT (clib_bihash_kv))));
181 }
182 }
Damjan Marion801ec2a2020-04-21 19:42:30 +0200183 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach67d09e02019-08-01 08:15:01 -0400184 h->instantiated = 1;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400185}
Dave Barach97f5af02018-02-22 09:48:45 -0500186
Dave Barachbdf9b972019-09-03 10:57:19 -0400187void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700188{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400189 int i;
190 void *oldheap;
Dave Barachbdf9b972019-09-03 10:57:19 -0400191 BVT (clib_bihash) * h = a->h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192
Dave Barachbdf9b972019-09-03 10:57:19 -0400193 a->nbuckets = 1 << (max_log2 (a->nbuckets));
194
195 h->name = (u8 *) a->name;
196 h->nbuckets = a->nbuckets;
197 h->log2_nbuckets = max_log2 (a->nbuckets);
Damjan Marion2454de22020-09-26 19:32:34 +0200198 h->memory_size = BIHASH_USE_HEAP ? 0 : a->memory_size;
Dave Barach67d09e02019-08-01 08:15:01 -0400199 h->instantiated = 0;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200200 h->fmt_fn = BV (format_bihash);
201 h->kvp_fmt_fn = a->kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400202
Dave Barach32dcd3b2019-07-08 12:25:38 -0400203 alloc_arena (h) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700204
Dave Barach508498f2018-07-19 12:11:16 -0400205 /*
206 * Make sure the requested size is rational. The max table
207 * size without playing the alignment card is 64 Gbytes.
208 * If someone starts complaining that's not enough, we can shift
209 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
210 */
Damjan Marion2454de22020-09-26 19:32:34 +0200211 if (BIHASH_USE_HEAP)
212 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400213
214 /* Add this hash table to the list */
Dave Barachbdf9b972019-09-03 10:57:19 -0400215 if (a->dont_add_to_all_bihash_list == 0)
216 {
217 for (i = 0; i < vec_len (clib_all_bihashes); i++)
218 if (clib_all_bihashes[i] == h)
219 goto do_lock;
220 oldheap = clib_all_bihash_set_heap ();
221 vec_add1 (clib_all_bihashes, (void *) h);
222 clib_mem_set_heap (oldheap);
223 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400224
Dave Barachbdf9b972019-09-03 10:57:19 -0400225do_lock:
226 if (h->alloc_lock)
227 clib_mem_free ((void *) h->alloc_lock);
Dave Barach67d09e02019-08-01 08:15:01 -0400228
229 /*
230 * Set up the lock now, so we can use it to make the first add
231 * thread-safe
232 */
233 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
234 CLIB_CACHE_LINE_BYTES);
235 h->alloc_lock[0] = 0;
236
Dave Barach16e4a4a2020-04-16 12:00:14 -0400237#if BIHASH_LAZY_INSTANTIATE
Dave Barachbdf9b972019-09-03 10:57:19 -0400238 if (a->instantiate_immediately)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400239#endif
Dave Barachbdf9b972019-09-03 10:57:19 -0400240 BV (clib_bihash_instantiate) (h);
241}
242
243void BV (clib_bihash_init)
244 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
245{
246 BVT (clib_bihash_init2_args) _a, *a = &_a;
247
248 memset (a, 0, sizeof (*a));
249
250 a->h = h;
251 a->name = name;
252 a->nbuckets = nbuckets;
253 a->memory_size = memory_size;
254
255 BV (clib_bihash_init2) (a);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800256}
257
Dave Barach9466c452018-08-24 17:21:14 -0400258#if BIHASH_32_64_SVM
259#if !defined (MFD_ALLOW_SEALING)
260#define MFD_ALLOW_SEALING 0x0002U
261#endif
262
Dave Barachd4a639b2020-08-06 11:38:40 -0400263void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400264 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
Dave Barach9466c452018-08-24 17:21:14 -0400265{
266 uword bucket_size;
267 u8 *mmap_addr;
268 vec_header_t *freelist_vh;
269 int fd;
270
Damjan Marion2454de22020-09-26 19:32:34 +0200271 ASSERT (BIHASH_USE_HEAP == 0);
272
Dave Barachffb14b92018-09-11 17:20:23 -0400273 ASSERT (memory_size < (1ULL << 32));
Dave Barach9466c452018-08-24 17:21:14 -0400274 /* Set up for memfd sharing */
275 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
276 {
277 clib_unix_warning ("memfd_create");
278 return;
279 }
280
281 if (ftruncate (fd, memory_size) < 0)
282 {
283 clib_unix_warning ("ftruncate");
284 return;
285 }
286
287 /* Not mission-critical, complain and continue */
288 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
289 clib_unix_warning ("fcntl (F_ADD_SEALS)");
290
Dave Barachffb14b92018-09-11 17:20:23 -0400291 mmap_addr = mmap (0, memory_size,
292 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400293
294 if (mmap_addr == MAP_FAILED)
295 {
296 clib_unix_warning ("mmap failed");
297 ASSERT (0);
298 }
299
300 h->sh = (void *) mmap_addr;
301 h->memfd = fd;
302 nbuckets = 1 << (max_log2 (nbuckets));
303
304 h->name = (u8 *) name;
305 h->sh->nbuckets = h->nbuckets = nbuckets;
306 h->log2_nbuckets = max_log2 (nbuckets);
307
308 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400309 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
Dave Barach9466c452018-08-24 17:21:14 -0400310 alloc_arena_size (h) = memory_size;
311
312 bucket_size = nbuckets * sizeof (h->buckets[0]);
313 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200314 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barachffb14b92018-09-11 17:20:23 -0400315 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
Dave Barach9466c452018-08-24 17:21:14 -0400316
317 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
318 h->alloc_lock[0] = 0;
319
Dave Barachffb14b92018-09-11 17:20:23 -0400320 h->sh->alloc_lock_as_u64 =
321 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
322 freelist_vh =
323 BV (alloc_aligned) (h,
324 sizeof (vec_header_t) +
325 BIHASH_FREELIST_LENGTH * sizeof (u64));
Dave Barach9466c452018-08-24 17:21:14 -0400326 freelist_vh->len = BIHASH_FREELIST_LENGTH;
Dave Barachffb14b92018-09-11 17:20:23 -0400327 h->sh->freelists_as_u64 =
328 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
329 h->freelists = (void *) (freelist_vh->vector_data);
Dave Barach9466c452018-08-24 17:21:14 -0400330
Damjan Marionf2b4a372020-09-30 14:15:24 +0200331 h->fmt_fn = BV (format_bihash);
332 h->kvp_fmt_fn = NULL;
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800333 h->instantiated = 1;
Dave Barach9466c452018-08-24 17:21:14 -0400334}
335
Dave Barachd4a639b2020-08-06 11:38:40 -0400336void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400337 (BVT (clib_bihash) * h, char *name, int fd)
338{
339 u8 *mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400340 u64 memory_size;
Dave Barach9466c452018-08-24 17:21:14 -0400341 BVT (clib_bihash_shared_header) * sh;
342
Damjan Marion2454de22020-09-26 19:32:34 +0200343 ASSERT (BIHASH_USE_HEAP == 0);
344
Dave Barachffb14b92018-09-11 17:20:23 -0400345 /* Trial mapping, to learn the segment size */
Dave Barach9466c452018-08-24 17:21:14 -0400346 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
347 if (mmap_addr == MAP_FAILED)
348 {
349 clib_unix_warning ("trial mmap failed");
350 ASSERT (0);
351 }
352
353 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
354
Dave Barach9466c452018-08-24 17:21:14 -0400355 memory_size = sh->alloc_arena_size;
356
357 munmap (mmap_addr, 4096);
358
Dave Barachffb14b92018-09-11 17:20:23 -0400359 /* Actual mapping, at the required size */
360 mmap_addr = mmap (0, memory_size,
361 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400362
363 if (mmap_addr == MAP_FAILED)
364 {
365 clib_unix_warning ("mmap failed");
366 ASSERT (0);
367 }
368
369 (void) close (fd);
370
371 h->sh = (void *) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400372 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barach9466c452018-08-24 17:21:14 -0400373 h->memfd = -1;
374
375 h->name = (u8 *) name;
Dave Barachffb14b92018-09-11 17:20:23 -0400376 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
Dave Barach9466c452018-08-24 17:21:14 -0400377 h->nbuckets = h->sh->nbuckets;
378 h->log2_nbuckets = max_log2 (h->nbuckets);
379
Dave Barachffb14b92018-09-11 17:20:23 -0400380 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
381 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
Damjan Marionf2b4a372020-09-30 14:15:24 +0200382 h->fmt_fn = BV (format_bihash);
383 h->kvp_fmt_fn = NULL;
Dave Barach9466c452018-08-24 17:21:14 -0400384}
385#endif /* BIHASH_32_64_SVM */
386
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800387void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200388 format_function_t * kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800389{
Damjan Marionf2b4a372020-09-30 14:15:24 +0200390 h->kvp_fmt_fn = kvp_fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700391}
392
Neale Rannseb696482020-09-29 14:44:23 +0000393int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h)
394{
395 return (h->instantiated != 0);
396}
397
Dave Barachc3799992016-08-15 11:12:27 -0400398void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700399{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400400 int i;
401
Dave Barach67d09e02019-08-01 08:15:01 -0400402 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400403 goto never_initialized;
404
Dave Barach67d09e02019-08-01 08:15:01 -0400405 h->instantiated = 0;
Damjan Marion2454de22020-09-26 19:32:34 +0200406
407 if (BIHASH_USE_HEAP)
408 {
409 BVT (clib_bihash_alloc_chunk) * next, *chunk;
410 void *oldheap = clib_mem_set_heap (h->heap);
411
412 chunk = h->chunks;
413 while (chunk)
414 {
415 next = chunk->next;
416 clib_mem_free (chunk);
417 chunk = next;
418 }
419 clib_mem_set_heap (oldheap);
420 }
421
Dave Barach97f5af02018-02-22 09:48:45 -0500422 vec_free (h->working_copies);
Vijayabhaskar Katamreddy72739a62019-05-07 13:27:32 -0700423 vec_free (h->working_copy_lengths);
Dave Barach9466c452018-08-24 17:21:14 -0400424#if BIHASH_32_64_SVM == 0
Dave Barach97f5af02018-02-22 09:48:45 -0500425 vec_free (h->freelists);
Dave Barach9466c452018-08-24 17:21:14 -0400426#else
427 if (h->memfd > 0)
428 (void) close (h->memfd);
429#endif
Damjan Marion2454de22020-09-26 19:32:34 +0200430 if (BIHASH_USE_HEAP == 0)
431 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)),
432 alloc_arena_size (h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400433never_initialized:
Damjan Marion2454de22020-09-26 19:32:34 +0200434 clib_memset_u8 (h, 0, sizeof (*h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400435 for (i = 0; i < vec_len (clib_all_bihashes); i++)
436 {
437 if ((void *) h == clib_all_bihashes[i])
438 {
439 vec_delete (clib_all_bihashes, 1, i);
440 return;
441 }
442 }
443 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800444 (u64) (uword) h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445}
446
Dave Barachc3799992016-08-15 11:12:27 -0400447static
448BVT (clib_bihash_value) *
449BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700450{
Dave Barachc3799992016-08-15 11:12:27 -0400451 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452
Dave Barach508498f2018-07-19 12:11:16 -0400453 ASSERT (h->alloc_lock[0]);
Dave Barach9466c452018-08-24 17:21:14 -0400454
455#if BIHASH_32_64_SVM
456 ASSERT (log2_pages < vec_len (h->freelists));
457#endif
458
Dave Barachc3799992016-08-15 11:12:27 -0400459 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460 {
Dave Barach97f5af02018-02-22 09:48:45 -0500461 vec_validate_init_empty (h->freelists, log2_pages, 0);
462 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -0400463 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 }
Dave Barachffb14b92018-09-11 17:20:23 -0400465 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
Dave Barach9466c452018-08-24 17:21:14 -0400466 h->freelists[log2_pages] = rv->next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700467
Dave Barachc3799992016-08-15 11:12:27 -0400468initialize:
469 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -0400470 /*
471 * Latest gcc complains that the length arg is zero
472 * if we replace (1<<log2_pages) with vec_len(rv).
473 * No clue.
474 */
Damjan Marion2454de22020-09-26 19:32:34 +0200475 clib_memset_u8 (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
Dave Barachc3799992016-08-15 11:12:27 -0400476 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477}
478
479static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400480BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
481 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482{
Dave Barach508498f2018-07-19 12:11:16 -0400483 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700484
Dave Barachc3799992016-08-15 11:12:27 -0400485 ASSERT (vec_len (h->freelists) > log2_pages);
486
Damjan Marion2454de22020-09-26 19:32:34 +0200487 if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES)
488 {
489 /* allocations bigger or equal to chunk size always contain single
490 * alloc and they can be given back to heap */
491 void *oldheap;
492 BVT (clib_bihash_alloc_chunk) * c;
493 c = (BVT (clib_bihash_alloc_chunk) *) v - 1;
494
495 if (c->prev)
496 c->prev->next = c->next;
497 else
498 h->chunks = c->next;
499
500 if (c->next)
501 c->next->prev = c->prev;
502
503 oldheap = clib_mem_set_heap (h->heap);
504 clib_mem_free (c);
505 clib_mem_set_heap (oldheap);
506 return;
507 }
508
Dave Barach508498f2018-07-19 12:11:16 -0400509 if (CLIB_DEBUG > 0)
Damjan Marion2454de22020-09-26 19:32:34 +0200510 clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages));
Dave Barach508498f2018-07-19 12:11:16 -0400511
Dave Barach9466c452018-08-24 17:21:14 -0400512 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
Dave Barachffb14b92018-09-11 17:20:23 -0400513 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514}
515
516static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400517BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518{
Dave Barachc3799992016-08-15 11:12:27 -0400519 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400520 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400521 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200522 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400523 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524
Dave Barach508498f2018-07-19 12:11:16 -0400525 ASSERT (h->alloc_lock[0]);
526
Damjan Marionf55f9b82017-05-10 21:06:28 +0200527 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200529 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700530 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 }
532
Dave Barachc3799992016-08-15 11:12:27 -0400533 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 * working_copies are per-cpu so that near-simultaneous
535 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400536 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200538 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400539 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700540
541 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542
Dave Barachba7ddfe2017-05-17 20:20:50 -0400543 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 {
Dave Barach97f5af02018-02-22 09:48:45 -0500545 /*
546 * It's not worth the bookkeeping to free working copies
547 * if (working_copy)
548 * clib_mem_free (working_copy);
549 */
550 working_copy = BV (alloc_aligned)
551 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400552 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200553 h->working_copies[thread_index] = working_copy;
Dave Barach2ce28d62019-05-03 12:58:01 -0400554
555 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
556 1ULL << b->log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557 }
558
Dave Barachc3799992016-08-15 11:12:27 -0400559 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560
Dave Barach178cf492018-11-13 16:34:13 -0500561 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400563 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200564 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200566 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567}
568
Dave Barachc3799992016-08-15 11:12:27 -0400569static
570BVT (clib_bihash_value) *
571BV (split_and_rehash)
572 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400573 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
574 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575{
Dave Barach5e6b9582016-12-12 15:37:29 -0500576 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400577 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700578
Dave Barach508498f2018-07-19 12:11:16 -0400579 ASSERT (h->alloc_lock[0]);
580
Dave Barachc3799992016-08-15 11:12:27 -0400581 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400582 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583
Dave Barachba7ddfe2017-05-17 20:20:50 -0400584 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585 {
586 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400587
Dave Barach5e6b9582016-12-12 15:37:29 -0500588 /* Entry not in use? Forget it */
589 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
590 continue;
591
592 /* rehash the item onto its new home-page */
593 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
Damjan Marion68e5fd52020-04-23 13:41:47 +0200594 new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500595 new_v = &new_values[new_hash];
596
597 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400599 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500600 /* Empty slot */
601 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400602 {
Dave Barach178cf492018-11-13 16:34:13 -0500603 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
604 sizeof (new_v->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500605 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400606 }
Dave Barachc3799992016-08-15 11:12:27 -0400607 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500608 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400609 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500610 return 0;
611 doublebreak:;
612 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400613
Dave Barach5e6b9582016-12-12 15:37:29 -0500614 return new_values;
615}
616
617static
618BVT (clib_bihash_value) *
619BV (split_and_rehash_linear)
620 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400621 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
622 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500623{
624 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400625 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500626
Dave Barach508498f2018-07-19 12:11:16 -0400627 ASSERT (h->alloc_lock[0]);
628
Dave Barach5e6b9582016-12-12 15:37:29 -0500629 new_values = BV (value_alloc) (h, new_log2_pages);
630 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400631 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500632
633 j = 0;
634 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400635 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500636 {
637 /* Find a free slot in the new linear scan bucket */
638 for (; j < new_length; j++)
639 {
Dave Barach8f544962017-01-18 10:23:22 -0500640 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500641 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
642 goto doublebreak;
643
644 /* New value should never be in use */
645 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
646 {
647 /* Copy the old value and move along */
Dave Barach178cf492018-11-13 16:34:13 -0500648 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
649 sizeof (new_values->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500650 j++;
651 goto doublebreak;
652 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500653 }
Dave Barach8f544962017-01-18 10:23:22 -0500654 /* This should never happen... */
655 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400656 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500657 return 0;
658
Dave Barach5e6b9582016-12-12 15:37:29 -0500659 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700660 }
661 return new_values;
662}
663
Damjan Marion801ec2a2020-04-21 19:42:30 +0200664static_always_inline int BV (clib_bihash_add_del_inline_with_hash)
665 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
Matus Fabian828d27e2018-08-21 03:15:50 -0700666 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667{
Dave Barach908a5ea2017-07-14 12:42:21 -0400668 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400669 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500670 int i, limit;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200671 u64 new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400672 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200673 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500674 int mark_bucket_linear;
675 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700676
Damjan Marion68e5fd52020-04-23 13:41:47 +0200677 /* *INDENT-OFF* */
678 static const BVT (clib_bihash_bucket) mask = {
679 .linear_search = 1,
680 .log2_pages = -1
681 };
682 /* *INDENT-ON* */
683
684#if BIHASH_LAZY_INSTANTIATE
Dave Barach67d09e02019-08-01 08:15:01 -0400685 /*
686 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
687 * Use the alloc_lock to protect the instantiate operation.
688 */
689 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400690 {
691 if (is_add == 0)
692 return (-1);
Dave Barach67d09e02019-08-01 08:15:01 -0400693
694 BV (clib_bihash_alloc_lock) (h);
695 if (h->instantiated == 0)
696 BV (clib_bihash_instantiate) (h);
697 BV (clib_bihash_alloc_unlock) (h);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400698 }
Dave Baracha90ba642020-04-23 16:56:15 -0400699#else
700 /* Debug image: make sure the table has been instantiated */
701 ASSERT (h->instantiated != 0);
Damjan Marion68e5fd52020-04-23 13:41:47 +0200702#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400703
Damjan Marion4e149772020-03-27 16:57:28 +0100704 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705
Dave Barach508498f2018-07-19 12:11:16 -0400706 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
708 /* First elt in the bucket? */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400709 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 {
711 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400712 {
Dave Barach508498f2018-07-19 12:11:16 -0400713 BV (clib_bihash_unlock_bucket) (b);
714 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400715 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716
Dave Barach508498f2018-07-19 12:11:16 -0400717 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400718 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400719 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400720
Dave Barachc3799992016-08-15 11:12:27 -0400721 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400722 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400723 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500724 tmp_b.refcnt = 1;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200725 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Tom Seidenberg97f8ae92019-03-15 10:15:26 -0400727 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
Dave Barach2ce28d62019-05-03 12:58:01 -0400728 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
729
Dave Barach508498f2018-07-19 12:11:16 -0400730 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700731 }
732
Dave Barach508498f2018-07-19 12:11:16 -0400733 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500734 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400735 v = BV (clib_bihash_get_value) (h, b->offset);
736
Damjan Marion68e5fd52020-04-23 13:41:47 +0200737 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
738 {
739 if (PREDICT_FALSE (b->linear_search))
740 limit <<= b->log2_pages;
741 else
742 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
743 }
Dave Barachc3799992016-08-15 11:12:27 -0400744
Ed Warnickecb9cada2015-12-08 15:45:58 -0700745 if (is_add)
746 {
Dave Barachc3799992016-08-15 11:12:27 -0400747 /*
Dave Barach508498f2018-07-19 12:11:16 -0400748 * Because reader threads are looking at live data,
749 * we have to be extra careful. Readers do NOT hold the
750 * bucket lock. We need to be SLOWER than a search, past the
751 * point where readers CHECK the bucket lock.
752 */
753
754 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 * For obvious (in hindsight) reasons, see if we're supposed to
756 * replace an existing key, then look for an empty slot.
757 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500758 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400759 {
Dave Baracha11bf452019-04-17 17:27:31 -0400760 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400761 {
Dave Barach9e4946b2019-07-08 14:47:44 -0400762 /* Add but do not overwrite? */
763 if (is_add == 2)
764 {
765 BV (clib_bihash_unlock_bucket) (b);
766 return (-2);
767 }
768
Damjan Marion801ec2a2020-04-21 19:42:30 +0200769 clib_memcpy_fast (&(v->kvp[i].value),
770 &add_v->value, sizeof (add_v->value));
Dave Barach508498f2018-07-19 12:11:16 -0400771 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400772 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400773 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400774 }
775 }
Dave Barach508498f2018-07-19 12:11:16 -0400776 /*
777 * Look for an empty slot. If found, use it
778 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500779 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400780 {
781 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
782 {
Dave Barach508498f2018-07-19 12:11:16 -0400783 /*
784 * Copy the value first, so that if a reader manages
785 * to match the new key, the value will be right...
786 */
Dave Barach178cf492018-11-13 16:34:13 -0500787 clib_memcpy_fast (&(v->kvp[i].value),
788 &add_v->value, sizeof (add_v->value));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200789 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
Dave Barach178cf492018-11-13 16:34:13 -0500790 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
791 sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500792 b->refcnt++;
Dave Barach9466c452018-08-24 17:21:14 -0400793 ASSERT (b->refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400794 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400795 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400796 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400797 }
798 }
Matus Fabian828d27e2018-08-21 03:15:50 -0700799 /* look for stale data to overwrite */
800 if (is_stale_cb)
801 {
802 for (i = 0; i < limit; i++)
803 {
804 if (is_stale_cb (&(v->kvp[i]), arg))
805 {
Dave Barach178cf492018-11-13 16:34:13 -0500806 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200807 CLIB_MEMORY_STORE_BARRIER ();
Matus Fabian828d27e2018-08-21 03:15:50 -0700808 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400809 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Matus Fabian828d27e2018-08-21 03:15:50 -0700810 return (0);
811 }
812 }
813 }
Dave Barach508498f2018-07-19 12:11:16 -0400814 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700815 }
Dave Barach508498f2018-07-19 12:11:16 -0400816 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500818 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400819 {
Dave Barach508498f2018-07-19 12:11:16 -0400820 /* Found the key? Kill it... */
Dave Baracha11bf452019-04-17 17:27:31 -0400821 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400822 {
Damjan Marion801ec2a2020-04-21 19:42:30 +0200823 clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
Dave Barach508498f2018-07-19 12:11:16 -0400824 /* Is the bucket empty? */
825 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500826 {
Dave Barach508498f2018-07-19 12:11:16 -0400827 b->refcnt--;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400828 /* Switch back to the bucket-level kvp array? */
829 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
830 && b->log2_pages > 0)
831 {
832 tmp_b.as_u64 = b->as_u64;
833 b->offset = BV (clib_bihash_get_offset)
834 (h, (void *) (b + 1));
835 b->linear_search = 0;
836 b->log2_pages = 0;
837 /* Clean up the bucket-level kvp array */
Damjan Marion801ec2a2020-04-21 19:42:30 +0200838 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
839 sizeof (BVT (clib_bihash_kv)));
840 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach16e4a4a2020-04-16 12:00:14 -0400841 BV (clib_bihash_unlock_bucket) (b);
842 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
843 goto free_backing_store;
844 }
845
Damjan Marion801ec2a2020-04-21 19:42:30 +0200846 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach508498f2018-07-19 12:11:16 -0400847 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400848 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400849 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500850 }
Dave Barach508498f2018-07-19 12:11:16 -0400851 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500852 {
Dave Barach508498f2018-07-19 12:11:16 -0400853 /* Save old bucket value, need log2_pages to free it */
854 tmp_b.as_u64 = b->as_u64;
Dave Barach508498f2018-07-19 12:11:16 -0400855
856 /* Kill and unlock the bucket */
857 b->as_u64 = 0;
858
Dave Barach16e4a4a2020-04-16 12:00:14 -0400859 free_backing_store:
Dave Barach508498f2018-07-19 12:11:16 -0400860 /* And free the backing storage */
861 BV (clib_bihash_alloc_lock) (h);
862 /* Note: v currently points into the middle of the bucket */
863 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
864 BV (value_free) (h, v, tmp_b.log2_pages);
865 BV (clib_bihash_alloc_unlock) (h);
Dave Barach2ce28d62019-05-03 12:58:01 -0400866 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
867 1);
Dave Barach508498f2018-07-19 12:11:16 -0400868 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500869 }
Dave Barachc3799992016-08-15 11:12:27 -0400870 }
871 }
Dave Barach508498f2018-07-19 12:11:16 -0400872 /* Not found... */
873 BV (clib_bihash_unlock_bucket) (b);
874 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700875 }
876
Dave Barach508498f2018-07-19 12:11:16 -0400877 /* Move readers to a (locked) temp copy of the bucket */
878 BV (clib_bihash_alloc_lock) (h);
879 BV (make_working_copy) (h, b);
880
881 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
882
Dave Barachba7ddfe2017-05-17 20:20:50 -0400883 old_log2_pages = h->saved_bucket.log2_pages;
884 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500885 mark_bucket_linear = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400886 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
887 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700888
Damjan Marionf55f9b82017-05-10 21:06:28 +0200889 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500890 resplit_once = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400891 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500892
Dave Barachba7ddfe2017-05-17 20:20:50 -0400893 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
894 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700895 if (new_v == 0)
896 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500897 try_resplit:
898 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500900 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400901 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
902 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500903 if (new_v == 0)
904 {
905 mark_linear:
906 new_log2_pages--;
907 /* pinned collisions, use linear search */
908 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400909 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
910 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500911 mark_bucket_linear = 1;
Dave Barach2ce28d62019-05-03 12:58:01 -0400912 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500913 }
Dave Barach2ce28d62019-05-03 12:58:01 -0400914 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
915 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
916 old_log2_pages + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 }
918
919 /* Try to add the new entry */
920 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400921 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500922 limit = BIHASH_KVP_PER_PAGE;
923 if (mark_bucket_linear)
924 limit <<= new_log2_pages;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200925 else
926 new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barachc3799992016-08-15 11:12:27 -0400927
Dave Barach5e6b9582016-12-12 15:37:29 -0500928 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700929 {
Dave Barachc3799992016-08-15 11:12:27 -0400930 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
931 {
Dave Barach178cf492018-11-13 16:34:13 -0500932 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
Dave Barachc3799992016-08-15 11:12:27 -0400933 goto expand_ok;
934 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400936
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400938 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500939 /*
940 * If we've already doubled the size of the bucket once,
941 * fall back to linear search now.
942 */
943 if (resplit_once)
944 goto mark_linear;
945 else
946 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947
Dave Barachc3799992016-08-15 11:12:27 -0400948expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500949 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400950 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500951 tmp_b.linear_search = mark_bucket_linear;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400952#if BIHASH_KVP_AT_BUCKET_LEVEL
953 /* Compensate for permanent refcount bump at the bucket level */
954 if (new_log2_pages > 0)
955#endif
956 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach9466c452018-08-24 17:21:14 -0400957 ASSERT (tmp_b.refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400958 tmp_b.lock = 0;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200959 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960 b->as_u64 = tmp_b.as_u64;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400961
962#if BIHASH_KVP_AT_BUCKET_LEVEL
963 if (h->saved_bucket.log2_pages > 0)
964 {
965#endif
966
967 /* free the old bucket, except at the bucket level if so configured */
968 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
969 BV (value_free) (h, v, h->saved_bucket.log2_pages);
970
971#if BIHASH_KVP_AT_BUCKET_LEVEL
972 }
973#endif
974
975
Dave Barach508498f2018-07-19 12:11:16 -0400976 BV (clib_bihash_alloc_unlock) (h);
977 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978}
979
Damjan Marion801ec2a2020-04-21 19:42:30 +0200980static_always_inline int BV (clib_bihash_add_del_inline)
981 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
982 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
983{
984 u64 hash = BV (clib_bihash_hash) (add_v);
985 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
986 is_stale_cb, arg);
987}
988
Matus Fabian828d27e2018-08-21 03:15:50 -0700989int BV (clib_bihash_add_del)
990 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
991{
992 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
993}
994
995int BV (clib_bihash_add_or_overwrite_stale)
996 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
997 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
998{
999 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
1000}
1001
Dave Barachc3799992016-08-15 11:12:27 -04001002int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -04001003 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -04001004 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001005{
Damjan Marion68e5fd52020-04-23 13:41:47 +02001006 return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001007}
1008
Dave Barachc3799992016-08-15 11:12:27 -04001009u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001010{
Dave Barachc3799992016-08-15 11:12:27 -04001011 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001012 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -04001013 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001014 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 int i, j, k;
1016 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -05001017 u64 active_buckets = 0;
1018 u64 linear_buckets = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019
Damjan Marionf2b4a372020-09-30 14:15:24 +02001020 s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -04001021
Dave Barach16e4a4a2020-04-16 12:00:14 -04001022#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001023 if (PREDICT_FALSE (h->instantiated == 0))
Damjan Marionf2b4a372020-09-30 14:15:24 +02001024 return format (s, " empty, uninitialized");
Dave Barach16e4a4a2020-04-16 12:00:14 -04001025#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001026
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027 for (i = 0; i < h->nbuckets; i++)
1028 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001029 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001030 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001031 {
1032 if (verbose > 1)
1033 s = format (s, "[%d]: empty\n", i);
1034 continue;
1035 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036
Dave Barache7d212f2018-02-07 13:14:06 -05001037 active_buckets++;
1038
1039 if (b->linear_search)
1040 linear_buckets++;
1041
Ed Warnickecb9cada2015-12-08 15:45:58 -07001042 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -04001043 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001044 s = format
1045 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
1046 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -04001047 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
Dave Barachc3799992016-08-15 11:12:27 -04001049 v = BV (clib_bihash_get_value) (h, b->offset);
1050 for (j = 0; j < (1 << b->log2_pages); j++)
1051 {
1052 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1053 {
1054 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1055 {
1056 if (verbose > 1)
1057 s = format (s, " %d: empty\n",
1058 j * BIHASH_KVP_PER_PAGE + k);
1059 continue;
1060 }
1061 if (verbose)
1062 {
Damjan Marionf2b4a372020-09-30 14:15:24 +02001063 if (h->kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001064 {
1065 s = format (s, " %d: %U\n",
1066 j * BIHASH_KVP_PER_PAGE + k,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001067 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001068 }
1069 else
1070 {
1071 s = format (s, " %d: %U\n",
1072 j * BIHASH_KVP_PER_PAGE + k,
1073 BV (format_bihash_kvp), &(v->kvp[k]));
1074 }
Dave Barachc3799992016-08-15 11:12:27 -04001075 }
1076 active_elements++;
1077 }
1078 v++;
1079 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001080 }
1081
Dave Barache7d212f2018-02-07 13:14:06 -05001082 s = format (s, " %lld active elements %lld active buckets\n",
1083 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -05001085
1086 for (i = 0; i < vec_len (h->freelists); i++)
1087 {
1088 u32 nfree = 0;
1089 BVT (clib_bihash_value) * free_elt;
Dave Barachffb14b92018-09-11 17:20:23 -04001090 u64 free_elt_as_u64 = h->freelists[i];
Dave Barache7d212f2018-02-07 13:14:06 -05001091
Dave Barachffb14b92018-09-11 17:20:23 -04001092 while (free_elt_as_u64)
Dave Barache7d212f2018-02-07 13:14:06 -05001093 {
Dave Barachffb14b92018-09-11 17:20:23 -04001094 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
Dave Barache7d212f2018-02-07 13:14:06 -05001095 nfree++;
Dave Barachffb14b92018-09-11 17:20:23 -04001096 free_elt_as_u64 = free_elt->next_free_as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -05001097 }
1098
Dave Barach9466c452018-08-24 17:21:14 -04001099 if (nfree || verbose)
1100 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
Dave Barache7d212f2018-02-07 13:14:06 -05001101 }
1102
1103 s = format (s, " %lld linear search buckets\n", linear_buckets);
Damjan Marion2454de22020-09-26 19:32:34 +02001104 if (BIHASH_USE_HEAP)
1105 {
1106 BVT (clib_bihash_alloc_chunk) * c = h->chunks;
1107 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1108
1109 while (c)
1110 {
1111 bytes_left += c->bytes_left;
1112 total_size += c->size;
1113 n_chunks += 1;
1114 c = c->next;
1115 }
1116 s = format (s,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001117 " heap: %u chunk(s) allocated\n"
1118 " bytes: used %U, scrap %U\n", n_chunks,
Damjan Marion2454de22020-09-26 19:32:34 +02001119 format_memory_size, total_size,
1120 format_memory_size, bytes_left);
1121 }
1122 else
1123 {
1124 u64 used_bytes = alloc_arena_next (h);
1125 s = format (s,
1126 " arena: base %llx, next %llx\n"
1127 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1128 alloc_arena (h), alloc_arena_next (h),
1129 used_bytes, used_bytes >> 20,
1130 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1131 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001132 return s;
1133}
1134
Dave Barachc3799992016-08-15 11:12:27 -04001135void BV (clib_bihash_foreach_key_value_pair)
Neale Rannsf50bac12019-12-06 05:53:17 +00001136 (BVT (clib_bihash) * h,
1137 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001138{
1139 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -04001140 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001141 BVT (clib_bihash_value) * v;
Dave Barachc3799992016-08-15 11:12:27 -04001142
Dave Barach16e4a4a2020-04-16 12:00:14 -04001143
1144#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001145 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -04001146 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -04001147#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001148
Ed Warnickecb9cada2015-12-08 15:45:58 -07001149 for (i = 0; i < h->nbuckets; i++)
1150 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001151 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001152 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001153 continue;
1154
1155 v = BV (clib_bihash_get_value) (h, b->offset);
1156 for (j = 0; j < (1 << b->log2_pages); j++)
1157 {
1158 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1159 {
1160 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1161 continue;
1162
Neale Rannsf50bac12019-12-06 05:53:17 +00001163 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1164 return;
Dave Barachca45ee72018-08-06 08:43:47 -04001165 /*
1166 * In case the callback deletes the last entry in the bucket...
1167 */
1168 if (BV (clib_bihash_bucket_is_empty) (b))
1169 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -04001170 }
1171 v++;
1172 }
Dave Barachca45ee72018-08-06 08:43:47 -04001173 doublebreak:
1174 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001175 }
1176}
Dave Barachdd3a57f2016-07-27 16:58:51 -04001177
Chris Luke16bcf7d2016-09-01 14:31:46 -04001178/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -04001179
1180/*
1181 * fd.io coding-style-patch-verification: ON
1182 *
1183 * Local Variables:
1184 * eval: (c-set-style "gnu")
1185 * End:
1186 */