blob: a6fa6267c79d9d528492b9d8b37759a2f133ebb3 [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
Damjan Marion4a251d02021-05-06 17:28:12 +020026#ifndef BIHASH_USE_HEAP
27#define BIHASH_USE_HEAP 1
28#endif
29
Dave Barach97f5af02018-02-22 09:48:45 -050030static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
31{
32 uword rv;
33
34 /* Round to an even number of cache lines */
Damjan Marion2454de22020-09-26 19:32:34 +020035 nbytes = round_pow2 (nbytes, CLIB_CACHE_LINE_BYTES);
36
37 if (BIHASH_USE_HEAP)
38 {
39 void *rv, *oldheap;
40 uword page_sz = sizeof (BVT (clib_bihash_value));
41 uword chunk_sz = round_pow2 (page_sz << BIIHASH_MIN_ALLOC_LOG2_PAGES,
42 CLIB_CACHE_LINE_BYTES);
43
44 BVT (clib_bihash_alloc_chunk) * chunk = h->chunks;
45
46 /* if there is enough space in the currenrt chunk */
47 if (chunk && chunk->bytes_left >= nbytes)
48 {
49 rv = chunk->next_alloc;
50 chunk->bytes_left -= nbytes;
51 chunk->next_alloc += nbytes;
52 return rv;
53 }
54
55 /* requested allocation is bigger than chunk size */
56 if (nbytes >= chunk_sz)
57 {
58 oldheap = clib_mem_set_heap (h->heap);
59 chunk = clib_mem_alloc_aligned (nbytes + sizeof (*chunk),
60 CLIB_CACHE_LINE_BYTES);
61 clib_mem_set_heap (oldheap);
62 clib_memset_u8 (chunk, 0, sizeof (*chunk));
63 chunk->size = nbytes;
64 rv = (u8 *) (chunk + 1);
65 if (h->chunks)
66 {
67 /* take 2nd place in the list */
68 chunk->next = h->chunks->next;
69 chunk->prev = h->chunks;
70 h->chunks->next = chunk;
71 if (chunk->next)
72 chunk->next->prev = chunk;
73 }
74 else
75 h->chunks = chunk;
76
77 return rv;
78 }
79
80 oldheap = clib_mem_set_heap (h->heap);
81 chunk = clib_mem_alloc_aligned (chunk_sz + sizeof (*chunk),
82 CLIB_CACHE_LINE_BYTES);
83 clib_mem_set_heap (oldheap);
84 chunk->size = chunk_sz;
85 chunk->bytes_left = chunk_sz;
86 chunk->next_alloc = (u8 *) (chunk + 1);
87 chunk->next = h->chunks;
88 chunk->prev = 0;
89 if (chunk->next)
90 chunk->next->prev = chunk;
91 h->chunks = chunk;
92 rv = chunk->next_alloc;
93 chunk->bytes_left -= nbytes;
94 chunk->next_alloc += nbytes;
95 return rv;
96 }
Dave Barach97f5af02018-02-22 09:48:45 -050097
Dave Barach9466c452018-08-24 17:21:14 -040098 rv = alloc_arena_next (h);
99 alloc_arena_next (h) += nbytes;
Dave Barach97f5af02018-02-22 09:48:45 -0500100
Andreas Schultzb4b525e2019-07-19 11:14:50 +0200101 if (alloc_arena_next (h) > alloc_arena_size (h))
Dave Barach97f5af02018-02-22 09:48:45 -0500102 os_out_of_memory ();
103
Dave Barach16e4a4a2020-04-16 12:00:14 -0400104 if (alloc_arena_next (h) > alloc_arena_mapped (h))
105 {
106 void *base, *rv;
107 uword alloc = alloc_arena_next (h) - alloc_arena_mapped (h);
108 int mmap_flags = MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS;
Damjan Marione6db7752020-05-24 20:43:10 +0200109 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
Dave Barach16e4a4a2020-04-16 12:00:14 -0400110 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
111
112 /* new allocation is 25% of existing one */
113 if (alloc_arena_mapped (h) >> 2 > alloc)
114 alloc = alloc_arena_mapped (h) >> 2;
115
116 /* round allocation to page size */
117 alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
118
119 base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h));
120
121 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
122
123 /* fallback - maybe we are still able to allocate normal pages */
Damjan Marion6183cf42020-05-27 16:43:35 +0200124 if (rv == MAP_FAILED || mlock (base, alloc) != 0)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400125 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
126
127 if (rv == MAP_FAILED)
128 os_out_of_memory ();
129
130 alloc_arena_mapped (h) += alloc;
131 }
132
Dave Barachffb14b92018-09-11 17:20:23 -0400133 return (void *) (uword) (rv + alloc_arena (h));
Dave Barach97f5af02018-02-22 09:48:45 -0500134}
135
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800136static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
Dave Barach32dcd3b2019-07-08 12:25:38 -0400137{
138 uword bucket_size;
139
Damjan Marion2454de22020-09-26 19:32:34 +0200140 if (BIHASH_USE_HEAP)
141 {
142 h->heap = clib_mem_get_heap ();
143 h->chunks = 0;
144 alloc_arena (h) = (uword) clib_mem_get_heap_base (h->heap);
145 }
146 else
147 {
148 alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size,
149 BIHASH_LOG2_HUGEPAGE_SIZE);
150 if (alloc_arena (h) == ~0)
151 os_out_of_memory ();
152 alloc_arena_next (h) = 0;
153 alloc_arena_size (h) = h->memory_size;
154 alloc_arena_mapped (h) = 0;
155 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400156
157 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400158
159 if (BIHASH_KVP_AT_BUCKET_LEVEL)
160 bucket_size +=
161 h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv));
162
Dave Barach32dcd3b2019-07-08 12:25:38 -0400163 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200164 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400165
166 if (BIHASH_KVP_AT_BUCKET_LEVEL)
167 {
168 int i;
169 BVT (clib_bihash_bucket) * b;
170
171 b = h->buckets;
172
173 for (i = 0; i < h->nbuckets; i++)
174 {
175 b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1));
176 b->refcnt = 1;
177 /* Mark all elements free */
Damjan Marion2454de22020-09-26 19:32:34 +0200178 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
179 sizeof (BVT (clib_bihash_kv)));
Dave Barach16e4a4a2020-04-16 12:00:14 -0400180
181 /* Compute next bucket start address */
182 b = (void *) (((uword) b) + sizeof (*b) +
183 (BIHASH_KVP_PER_PAGE *
184 sizeof (BVT (clib_bihash_kv))));
185 }
186 }
Damjan Marion801ec2a2020-04-21 19:42:30 +0200187 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach67d09e02019-08-01 08:15:01 -0400188 h->instantiated = 1;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400189}
Dave Barach97f5af02018-02-22 09:48:45 -0500190
Dave Barachbdf9b972019-09-03 10:57:19 -0400191void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700192{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400193 int i;
194 void *oldheap;
Dave Barachbdf9b972019-09-03 10:57:19 -0400195 BVT (clib_bihash) * h = a->h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700196
Dave Barachbdf9b972019-09-03 10:57:19 -0400197 a->nbuckets = 1 << (max_log2 (a->nbuckets));
198
199 h->name = (u8 *) a->name;
200 h->nbuckets = a->nbuckets;
201 h->log2_nbuckets = max_log2 (a->nbuckets);
Damjan Marion2454de22020-09-26 19:32:34 +0200202 h->memory_size = BIHASH_USE_HEAP ? 0 : a->memory_size;
Dave Barach67d09e02019-08-01 08:15:01 -0400203 h->instantiated = 0;
Nathan Skrzypczaka8c720e2021-08-06 12:03:11 +0200204 h->dont_add_to_all_bihash_list = a->dont_add_to_all_bihash_list;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200205 h->fmt_fn = BV (format_bihash);
206 h->kvp_fmt_fn = a->kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400207
Dave Barach32dcd3b2019-07-08 12:25:38 -0400208 alloc_arena (h) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700209
Dave Barach508498f2018-07-19 12:11:16 -0400210 /*
211 * Make sure the requested size is rational. The max table
212 * size without playing the alignment card is 64 Gbytes.
213 * If someone starts complaining that's not enough, we can shift
214 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
215 */
Damjan Marion2454de22020-09-26 19:32:34 +0200216 if (BIHASH_USE_HEAP)
217 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400218
219 /* Add this hash table to the list */
Dave Barachbdf9b972019-09-03 10:57:19 -0400220 if (a->dont_add_to_all_bihash_list == 0)
221 {
222 for (i = 0; i < vec_len (clib_all_bihashes); i++)
223 if (clib_all_bihashes[i] == h)
224 goto do_lock;
225 oldheap = clib_all_bihash_set_heap ();
226 vec_add1 (clib_all_bihashes, (void *) h);
227 clib_mem_set_heap (oldheap);
228 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400229
Dave Barachbdf9b972019-09-03 10:57:19 -0400230do_lock:
231 if (h->alloc_lock)
232 clib_mem_free ((void *) h->alloc_lock);
Dave Barach67d09e02019-08-01 08:15:01 -0400233
234 /*
235 * Set up the lock now, so we can use it to make the first add
236 * thread-safe
237 */
238 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
239 CLIB_CACHE_LINE_BYTES);
240 h->alloc_lock[0] = 0;
241
Dave Barach16e4a4a2020-04-16 12:00:14 -0400242#if BIHASH_LAZY_INSTANTIATE
Dave Barachbdf9b972019-09-03 10:57:19 -0400243 if (a->instantiate_immediately)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400244#endif
Dave Barachbdf9b972019-09-03 10:57:19 -0400245 BV (clib_bihash_instantiate) (h);
246}
247
248void BV (clib_bihash_init)
249 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
250{
251 BVT (clib_bihash_init2_args) _a, *a = &_a;
252
253 memset (a, 0, sizeof (*a));
254
255 a->h = h;
256 a->name = name;
257 a->nbuckets = nbuckets;
258 a->memory_size = memory_size;
259
260 BV (clib_bihash_init2) (a);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800261}
262
Dave Barach9466c452018-08-24 17:21:14 -0400263#if BIHASH_32_64_SVM
264#if !defined (MFD_ALLOW_SEALING)
265#define MFD_ALLOW_SEALING 0x0002U
266#endif
267
Dave Barachd4a639b2020-08-06 11:38:40 -0400268void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400269 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
Dave Barach9466c452018-08-24 17:21:14 -0400270{
271 uword bucket_size;
272 u8 *mmap_addr;
273 vec_header_t *freelist_vh;
274 int fd;
275
Damjan Marion2454de22020-09-26 19:32:34 +0200276 ASSERT (BIHASH_USE_HEAP == 0);
277
Dave Barachffb14b92018-09-11 17:20:23 -0400278 ASSERT (memory_size < (1ULL << 32));
Dave Barach9466c452018-08-24 17:21:14 -0400279 /* Set up for memfd sharing */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200280 if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, name) == -1)
Dave Barach9466c452018-08-24 17:21:14 -0400281 {
282 clib_unix_warning ("memfd_create");
283 return;
284 }
285
286 if (ftruncate (fd, memory_size) < 0)
287 {
288 clib_unix_warning ("ftruncate");
289 return;
290 }
291
292 /* Not mission-critical, complain and continue */
293 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
294 clib_unix_warning ("fcntl (F_ADD_SEALS)");
295
Dave Barachffb14b92018-09-11 17:20:23 -0400296 mmap_addr = mmap (0, memory_size,
297 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400298
299 if (mmap_addr == MAP_FAILED)
300 {
301 clib_unix_warning ("mmap failed");
302 ASSERT (0);
303 }
304
305 h->sh = (void *) mmap_addr;
306 h->memfd = fd;
307 nbuckets = 1 << (max_log2 (nbuckets));
308
309 h->name = (u8 *) name;
310 h->sh->nbuckets = h->nbuckets = nbuckets;
311 h->log2_nbuckets = max_log2 (nbuckets);
312
313 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400314 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
Dave Barach9466c452018-08-24 17:21:14 -0400315 alloc_arena_size (h) = memory_size;
316
317 bucket_size = nbuckets * sizeof (h->buckets[0]);
318 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200319 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barachffb14b92018-09-11 17:20:23 -0400320 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
Dave Barach9466c452018-08-24 17:21:14 -0400321
322 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
323 h->alloc_lock[0] = 0;
324
Dave Barachffb14b92018-09-11 17:20:23 -0400325 h->sh->alloc_lock_as_u64 =
326 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
327 freelist_vh =
328 BV (alloc_aligned) (h,
329 sizeof (vec_header_t) +
330 BIHASH_FREELIST_LENGTH * sizeof (u64));
Dave Barach9466c452018-08-24 17:21:14 -0400331 freelist_vh->len = BIHASH_FREELIST_LENGTH;
Dave Barachffb14b92018-09-11 17:20:23 -0400332 h->sh->freelists_as_u64 =
333 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
334 h->freelists = (void *) (freelist_vh->vector_data);
Dave Barach9466c452018-08-24 17:21:14 -0400335
Damjan Marionf2b4a372020-09-30 14:15:24 +0200336 h->fmt_fn = BV (format_bihash);
337 h->kvp_fmt_fn = NULL;
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800338 h->instantiated = 1;
Dave Barach9466c452018-08-24 17:21:14 -0400339}
340
Dave Barachd4a639b2020-08-06 11:38:40 -0400341void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400342 (BVT (clib_bihash) * h, char *name, int fd)
343{
344 u8 *mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400345 u64 memory_size;
Dave Barach9466c452018-08-24 17:21:14 -0400346 BVT (clib_bihash_shared_header) * sh;
347
Damjan Marion2454de22020-09-26 19:32:34 +0200348 ASSERT (BIHASH_USE_HEAP == 0);
349
Dave Barachffb14b92018-09-11 17:20:23 -0400350 /* Trial mapping, to learn the segment size */
Dave Barach9466c452018-08-24 17:21:14 -0400351 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
352 if (mmap_addr == MAP_FAILED)
353 {
354 clib_unix_warning ("trial mmap failed");
355 ASSERT (0);
356 }
357
358 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
359
Dave Barach9466c452018-08-24 17:21:14 -0400360 memory_size = sh->alloc_arena_size;
361
362 munmap (mmap_addr, 4096);
363
Dave Barachffb14b92018-09-11 17:20:23 -0400364 /* Actual mapping, at the required size */
365 mmap_addr = mmap (0, memory_size,
366 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400367
368 if (mmap_addr == MAP_FAILED)
369 {
370 clib_unix_warning ("mmap failed");
371 ASSERT (0);
372 }
373
374 (void) close (fd);
375
376 h->sh = (void *) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400377 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barach9466c452018-08-24 17:21:14 -0400378 h->memfd = -1;
379
380 h->name = (u8 *) name;
Dave Barachffb14b92018-09-11 17:20:23 -0400381 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
Dave Barach9466c452018-08-24 17:21:14 -0400382 h->nbuckets = h->sh->nbuckets;
383 h->log2_nbuckets = max_log2 (h->nbuckets);
384
Dave Barachffb14b92018-09-11 17:20:23 -0400385 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
386 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
Damjan Marionf2b4a372020-09-30 14:15:24 +0200387 h->fmt_fn = BV (format_bihash);
388 h->kvp_fmt_fn = NULL;
Dave Barach9466c452018-08-24 17:21:14 -0400389}
390#endif /* BIHASH_32_64_SVM */
391
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800392void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200393 format_function_t * kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800394{
Damjan Marionf2b4a372020-09-30 14:15:24 +0200395 h->kvp_fmt_fn = kvp_fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700396}
397
Neale Rannseb696482020-09-29 14:44:23 +0000398int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h)
399{
400 return (h->instantiated != 0);
401}
402
Dave Barachc3799992016-08-15 11:12:27 -0400403void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400405 int i;
406
Dave Barach67d09e02019-08-01 08:15:01 -0400407 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400408 goto never_initialized;
409
Dave Barach67d09e02019-08-01 08:15:01 -0400410 h->instantiated = 0;
Damjan Marion2454de22020-09-26 19:32:34 +0200411
412 if (BIHASH_USE_HEAP)
413 {
414 BVT (clib_bihash_alloc_chunk) * next, *chunk;
415 void *oldheap = clib_mem_set_heap (h->heap);
416
417 chunk = h->chunks;
418 while (chunk)
419 {
420 next = chunk->next;
421 clib_mem_free (chunk);
422 chunk = next;
423 }
424 clib_mem_set_heap (oldheap);
425 }
426
Dave Barach97f5af02018-02-22 09:48:45 -0500427 vec_free (h->working_copies);
Vijayabhaskar Katamreddy72739a62019-05-07 13:27:32 -0700428 vec_free (h->working_copy_lengths);
Dave Barach9466c452018-08-24 17:21:14 -0400429#if BIHASH_32_64_SVM == 0
Dave Barach97f5af02018-02-22 09:48:45 -0500430 vec_free (h->freelists);
Dave Barach9466c452018-08-24 17:21:14 -0400431#else
432 if (h->memfd > 0)
433 (void) close (h->memfd);
434#endif
Damjan Marion2454de22020-09-26 19:32:34 +0200435 if (BIHASH_USE_HEAP == 0)
436 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)),
437 alloc_arena_size (h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400438never_initialized:
Nathan Skrzypczaka8c720e2021-08-06 12:03:11 +0200439 if (h->dont_add_to_all_bihash_list)
440 {
441 clib_memset_u8 (h, 0, sizeof (*h));
442 return;
443 }
Damjan Marion2454de22020-09-26 19:32:34 +0200444 clib_memset_u8 (h, 0, sizeof (*h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400445 for (i = 0; i < vec_len (clib_all_bihashes); i++)
446 {
447 if ((void *) h == clib_all_bihashes[i])
448 {
449 vec_delete (clib_all_bihashes, 1, i);
450 return;
451 }
452 }
453 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800454 (u64) (uword) h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700455}
456
Dave Barachc3799992016-08-15 11:12:27 -0400457static
458BVT (clib_bihash_value) *
459BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460{
Dave Barachc3799992016-08-15 11:12:27 -0400461 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Dave Barach508498f2018-07-19 12:11:16 -0400463 ASSERT (h->alloc_lock[0]);
Dave Barach9466c452018-08-24 17:21:14 -0400464
465#if BIHASH_32_64_SVM
466 ASSERT (log2_pages < vec_len (h->freelists));
467#endif
468
Dave Barachc3799992016-08-15 11:12:27 -0400469 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700470 {
Dave Barach97f5af02018-02-22 09:48:45 -0500471 vec_validate_init_empty (h->freelists, log2_pages, 0);
472 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -0400473 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474 }
Dave Barachffb14b92018-09-11 17:20:23 -0400475 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
Dave Barach9466c452018-08-24 17:21:14 -0400476 h->freelists[log2_pages] = rv->next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477
Dave Barachc3799992016-08-15 11:12:27 -0400478initialize:
479 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -0400480 /*
481 * Latest gcc complains that the length arg is zero
482 * if we replace (1<<log2_pages) with vec_len(rv).
483 * No clue.
484 */
Damjan Marion2454de22020-09-26 19:32:34 +0200485 clib_memset_u8 (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
Dave Barachc3799992016-08-15 11:12:27 -0400486 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487}
488
489static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400490BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
491 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700492{
Dave Barach508498f2018-07-19 12:11:16 -0400493 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700494
Dave Barachc3799992016-08-15 11:12:27 -0400495 ASSERT (vec_len (h->freelists) > log2_pages);
496
Damjan Marion2454de22020-09-26 19:32:34 +0200497 if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES)
498 {
499 /* allocations bigger or equal to chunk size always contain single
500 * alloc and they can be given back to heap */
501 void *oldheap;
502 BVT (clib_bihash_alloc_chunk) * c;
503 c = (BVT (clib_bihash_alloc_chunk) *) v - 1;
504
505 if (c->prev)
506 c->prev->next = c->next;
507 else
508 h->chunks = c->next;
509
510 if (c->next)
511 c->next->prev = c->prev;
512
513 oldheap = clib_mem_set_heap (h->heap);
514 clib_mem_free (c);
515 clib_mem_set_heap (oldheap);
516 return;
517 }
518
Dave Barach508498f2018-07-19 12:11:16 -0400519 if (CLIB_DEBUG > 0)
Damjan Marion2454de22020-09-26 19:32:34 +0200520 clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages));
Dave Barach508498f2018-07-19 12:11:16 -0400521
Dave Barach9466c452018-08-24 17:21:14 -0400522 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
Dave Barachffb14b92018-09-11 17:20:23 -0400523 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700524}
525
526static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400527BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528{
Dave Barachc3799992016-08-15 11:12:27 -0400529 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400530 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400531 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200532 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400533 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534
Dave Barach508498f2018-07-19 12:11:16 -0400535 ASSERT (h->alloc_lock[0]);
536
Damjan Marionf55f9b82017-05-10 21:06:28 +0200537 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200539 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700540 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 }
542
Dave Barachc3799992016-08-15 11:12:27 -0400543 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544 * working_copies are per-cpu so that near-simultaneous
545 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400546 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200548 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400549 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550
551 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700552
Dave Barachba7ddfe2017-05-17 20:20:50 -0400553 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 {
Dave Barach97f5af02018-02-22 09:48:45 -0500555 /*
556 * It's not worth the bookkeeping to free working copies
557 * if (working_copy)
558 * clib_mem_free (working_copy);
559 */
560 working_copy = BV (alloc_aligned)
561 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400562 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200563 h->working_copies[thread_index] = working_copy;
Dave Barach2ce28d62019-05-03 12:58:01 -0400564
565 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
566 1ULL << b->log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567 }
568
Dave Barachc3799992016-08-15 11:12:27 -0400569 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700570
Dave Barach178cf492018-11-13 16:34:13 -0500571 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700572 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400573 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200574 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200576 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577}
578
Dave Barachc3799992016-08-15 11:12:27 -0400579static
580BVT (clib_bihash_value) *
581BV (split_and_rehash)
582 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400583 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
584 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585{
Dave Barach5e6b9582016-12-12 15:37:29 -0500586 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400587 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700588
Dave Barach508498f2018-07-19 12:11:16 -0400589 ASSERT (h->alloc_lock[0]);
590
Dave Barachc3799992016-08-15 11:12:27 -0400591 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400592 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700593
Dave Barachba7ddfe2017-05-17 20:20:50 -0400594 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 {
596 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400597
Dave Barach5e6b9582016-12-12 15:37:29 -0500598 /* Entry not in use? Forget it */
599 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
600 continue;
601
602 /* rehash the item onto its new home-page */
603 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
Damjan Marion68e5fd52020-04-23 13:41:47 +0200604 new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500605 new_v = &new_values[new_hash];
606
607 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400609 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500610 /* Empty slot */
611 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400612 {
Dave Barach178cf492018-11-13 16:34:13 -0500613 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
614 sizeof (new_v->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500615 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400616 }
Dave Barachc3799992016-08-15 11:12:27 -0400617 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500618 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400619 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500620 return 0;
621 doublebreak:;
622 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400623
Dave Barach5e6b9582016-12-12 15:37:29 -0500624 return new_values;
625}
626
627static
628BVT (clib_bihash_value) *
629BV (split_and_rehash_linear)
630 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400631 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
632 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500633{
634 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400635 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500636
Dave Barach508498f2018-07-19 12:11:16 -0400637 ASSERT (h->alloc_lock[0]);
638
Dave Barach5e6b9582016-12-12 15:37:29 -0500639 new_values = BV (value_alloc) (h, new_log2_pages);
640 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400641 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500642
643 j = 0;
644 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400645 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500646 {
647 /* Find a free slot in the new linear scan bucket */
648 for (; j < new_length; j++)
649 {
Dave Barach8f544962017-01-18 10:23:22 -0500650 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500651 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
652 goto doublebreak;
653
654 /* New value should never be in use */
655 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
656 {
657 /* Copy the old value and move along */
Dave Barach178cf492018-11-13 16:34:13 -0500658 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
659 sizeof (new_values->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500660 j++;
661 goto doublebreak;
662 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500663 }
Dave Barach8f544962017-01-18 10:23:22 -0500664 /* This should never happen... */
665 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400666 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500667 return 0;
668
Dave Barach5e6b9582016-12-12 15:37:29 -0500669 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700670 }
671 return new_values;
672}
673
Damjan Marion801ec2a2020-04-21 19:42:30 +0200674static_always_inline int BV (clib_bihash_add_del_inline_with_hash)
675 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
Matus Fabian828d27e2018-08-21 03:15:50 -0700676 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677{
Dave Barach908a5ea2017-07-14 12:42:21 -0400678 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400679 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500680 int i, limit;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200681 u64 new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400682 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200683 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500684 int mark_bucket_linear;
685 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686
Damjan Marion68e5fd52020-04-23 13:41:47 +0200687 /* *INDENT-OFF* */
688 static const BVT (clib_bihash_bucket) mask = {
689 .linear_search = 1,
690 .log2_pages = -1
691 };
692 /* *INDENT-ON* */
693
694#if BIHASH_LAZY_INSTANTIATE
Dave Barach67d09e02019-08-01 08:15:01 -0400695 /*
696 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
697 * Use the alloc_lock to protect the instantiate operation.
698 */
699 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400700 {
701 if (is_add == 0)
702 return (-1);
Dave Barach67d09e02019-08-01 08:15:01 -0400703
704 BV (clib_bihash_alloc_lock) (h);
705 if (h->instantiated == 0)
706 BV (clib_bihash_instantiate) (h);
707 BV (clib_bihash_alloc_unlock) (h);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400708 }
Dave Baracha90ba642020-04-23 16:56:15 -0400709#else
710 /* Debug image: make sure the table has been instantiated */
711 ASSERT (h->instantiated != 0);
Damjan Marion68e5fd52020-04-23 13:41:47 +0200712#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400713
Damjan Marion4e149772020-03-27 16:57:28 +0100714 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700715
Dave Barach508498f2018-07-19 12:11:16 -0400716 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700717
718 /* First elt in the bucket? */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400719 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720 {
721 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400722 {
Dave Barach508498f2018-07-19 12:11:16 -0400723 BV (clib_bihash_unlock_bucket) (b);
724 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400725 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
Dave Barach508498f2018-07-19 12:11:16 -0400727 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400728 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400729 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400730
Dave Barachc3799992016-08-15 11:12:27 -0400731 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400732 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400733 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500734 tmp_b.refcnt = 1;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200735 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700736
Tom Seidenberg97f8ae92019-03-15 10:15:26 -0400737 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
Dave Barach2ce28d62019-05-03 12:58:01 -0400738 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
739
Dave Barach508498f2018-07-19 12:11:16 -0400740 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700741 }
742
Dave Barach508498f2018-07-19 12:11:16 -0400743 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500744 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400745 v = BV (clib_bihash_get_value) (h, b->offset);
746
Damjan Marion68e5fd52020-04-23 13:41:47 +0200747 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
748 {
749 if (PREDICT_FALSE (b->linear_search))
750 limit <<= b->log2_pages;
751 else
752 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
753 }
Dave Barachc3799992016-08-15 11:12:27 -0400754
Ed Warnickecb9cada2015-12-08 15:45:58 -0700755 if (is_add)
756 {
Dave Barachc3799992016-08-15 11:12:27 -0400757 /*
Dave Barach508498f2018-07-19 12:11:16 -0400758 * Because reader threads are looking at live data,
759 * we have to be extra careful. Readers do NOT hold the
760 * bucket lock. We need to be SLOWER than a search, past the
761 * point where readers CHECK the bucket lock.
762 */
763
764 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700765 * For obvious (in hindsight) reasons, see if we're supposed to
766 * replace an existing key, then look for an empty slot.
767 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500768 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400769 {
Dave Baracha11bf452019-04-17 17:27:31 -0400770 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400771 {
Dave Barach9e4946b2019-07-08 14:47:44 -0400772 /* Add but do not overwrite? */
773 if (is_add == 2)
774 {
775 BV (clib_bihash_unlock_bucket) (b);
776 return (-2);
777 }
778
Damjan Marion801ec2a2020-04-21 19:42:30 +0200779 clib_memcpy_fast (&(v->kvp[i].value),
780 &add_v->value, sizeof (add_v->value));
Dave Barach508498f2018-07-19 12:11:16 -0400781 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400782 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400783 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400784 }
785 }
Dave Barach508498f2018-07-19 12:11:16 -0400786 /*
787 * Look for an empty slot. If found, use it
788 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500789 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400790 {
791 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
792 {
Dave Barach508498f2018-07-19 12:11:16 -0400793 /*
794 * Copy the value first, so that if a reader manages
795 * to match the new key, the value will be right...
796 */
Dave Barach178cf492018-11-13 16:34:13 -0500797 clib_memcpy_fast (&(v->kvp[i].value),
798 &add_v->value, sizeof (add_v->value));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200799 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
Dave Barach178cf492018-11-13 16:34:13 -0500800 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
801 sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500802 b->refcnt++;
Dave Barach9466c452018-08-24 17:21:14 -0400803 ASSERT (b->refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400804 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400805 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400806 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400807 }
808 }
Matus Fabian828d27e2018-08-21 03:15:50 -0700809 /* look for stale data to overwrite */
810 if (is_stale_cb)
811 {
812 for (i = 0; i < limit; i++)
813 {
814 if (is_stale_cb (&(v->kvp[i]), arg))
815 {
Dave Barach178cf492018-11-13 16:34:13 -0500816 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200817 CLIB_MEMORY_STORE_BARRIER ();
Matus Fabian828d27e2018-08-21 03:15:50 -0700818 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400819 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Matus Fabian828d27e2018-08-21 03:15:50 -0700820 return (0);
821 }
822 }
823 }
Dave Barach508498f2018-07-19 12:11:16 -0400824 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700825 }
Dave Barach508498f2018-07-19 12:11:16 -0400826 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700827 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500828 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400829 {
Dave Barach508498f2018-07-19 12:11:16 -0400830 /* Found the key? Kill it... */
Dave Baracha11bf452019-04-17 17:27:31 -0400831 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400832 {
Damjan Marion801ec2a2020-04-21 19:42:30 +0200833 clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
Dave Barach508498f2018-07-19 12:11:16 -0400834 /* Is the bucket empty? */
835 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500836 {
Dave Barach508498f2018-07-19 12:11:16 -0400837 b->refcnt--;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400838 /* Switch back to the bucket-level kvp array? */
839 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
840 && b->log2_pages > 0)
841 {
842 tmp_b.as_u64 = b->as_u64;
843 b->offset = BV (clib_bihash_get_offset)
844 (h, (void *) (b + 1));
845 b->linear_search = 0;
846 b->log2_pages = 0;
847 /* Clean up the bucket-level kvp array */
Damjan Marion801ec2a2020-04-21 19:42:30 +0200848 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
849 sizeof (BVT (clib_bihash_kv)));
850 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach16e4a4a2020-04-16 12:00:14 -0400851 BV (clib_bihash_unlock_bucket) (b);
852 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
853 goto free_backing_store;
854 }
855
Damjan Marion801ec2a2020-04-21 19:42:30 +0200856 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach508498f2018-07-19 12:11:16 -0400857 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400858 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400859 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500860 }
Dave Barach508498f2018-07-19 12:11:16 -0400861 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500862 {
Dave Barach508498f2018-07-19 12:11:16 -0400863 /* Save old bucket value, need log2_pages to free it */
864 tmp_b.as_u64 = b->as_u64;
Dave Barach508498f2018-07-19 12:11:16 -0400865
866 /* Kill and unlock the bucket */
867 b->as_u64 = 0;
868
Dave Barach16e4a4a2020-04-16 12:00:14 -0400869 free_backing_store:
Dave Barach508498f2018-07-19 12:11:16 -0400870 /* And free the backing storage */
871 BV (clib_bihash_alloc_lock) (h);
872 /* Note: v currently points into the middle of the bucket */
873 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
874 BV (value_free) (h, v, tmp_b.log2_pages);
875 BV (clib_bihash_alloc_unlock) (h);
Dave Barach2ce28d62019-05-03 12:58:01 -0400876 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
877 1);
Dave Barach508498f2018-07-19 12:11:16 -0400878 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500879 }
Dave Barachc3799992016-08-15 11:12:27 -0400880 }
881 }
Dave Barach508498f2018-07-19 12:11:16 -0400882 /* Not found... */
883 BV (clib_bihash_unlock_bucket) (b);
884 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700885 }
886
Dave Barach508498f2018-07-19 12:11:16 -0400887 /* Move readers to a (locked) temp copy of the bucket */
888 BV (clib_bihash_alloc_lock) (h);
889 BV (make_working_copy) (h, b);
890
891 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
892
Dave Barachba7ddfe2017-05-17 20:20:50 -0400893 old_log2_pages = h->saved_bucket.log2_pages;
894 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500895 mark_bucket_linear = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400896 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
897 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700898
Damjan Marionf55f9b82017-05-10 21:06:28 +0200899 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500900 resplit_once = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400901 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500902
Dave Barachba7ddfe2017-05-17 20:20:50 -0400903 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
904 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700905 if (new_v == 0)
906 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500907 try_resplit:
908 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700909 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500910 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400911 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
912 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500913 if (new_v == 0)
914 {
915 mark_linear:
916 new_log2_pages--;
917 /* pinned collisions, use linear search */
918 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400919 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
920 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500921 mark_bucket_linear = 1;
Dave Barach2ce28d62019-05-03 12:58:01 -0400922 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500923 }
Dave Barach2ce28d62019-05-03 12:58:01 -0400924 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
925 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
926 old_log2_pages + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927 }
928
929 /* Try to add the new entry */
930 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400931 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500932 limit = BIHASH_KVP_PER_PAGE;
933 if (mark_bucket_linear)
934 limit <<= new_log2_pages;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200935 else
936 new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barachc3799992016-08-15 11:12:27 -0400937
Dave Barach5e6b9582016-12-12 15:37:29 -0500938 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 {
Dave Barachc3799992016-08-15 11:12:27 -0400940 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
941 {
Dave Barach178cf492018-11-13 16:34:13 -0500942 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
Dave Barachc3799992016-08-15 11:12:27 -0400943 goto expand_ok;
944 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700945 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400946
Ed Warnickecb9cada2015-12-08 15:45:58 -0700947 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400948 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500949 /*
950 * If we've already doubled the size of the bucket once,
951 * fall back to linear search now.
952 */
953 if (resplit_once)
954 goto mark_linear;
955 else
956 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700957
Dave Barachc3799992016-08-15 11:12:27 -0400958expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500959 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400960 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500961 tmp_b.linear_search = mark_bucket_linear;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400962#if BIHASH_KVP_AT_BUCKET_LEVEL
963 /* Compensate for permanent refcount bump at the bucket level */
964 if (new_log2_pages > 0)
965#endif
966 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach9466c452018-08-24 17:21:14 -0400967 ASSERT (tmp_b.refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400968 tmp_b.lock = 0;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200969 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700970 b->as_u64 = tmp_b.as_u64;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400971
972#if BIHASH_KVP_AT_BUCKET_LEVEL
973 if (h->saved_bucket.log2_pages > 0)
974 {
975#endif
976
977 /* free the old bucket, except at the bucket level if so configured */
978 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
979 BV (value_free) (h, v, h->saved_bucket.log2_pages);
980
981#if BIHASH_KVP_AT_BUCKET_LEVEL
982 }
983#endif
984
985
Dave Barach508498f2018-07-19 12:11:16 -0400986 BV (clib_bihash_alloc_unlock) (h);
987 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700988}
989
Damjan Marion801ec2a2020-04-21 19:42:30 +0200990static_always_inline int BV (clib_bihash_add_del_inline)
991 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
992 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
993{
994 u64 hash = BV (clib_bihash_hash) (add_v);
995 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
996 is_stale_cb, arg);
997}
998
Matus Fabian828d27e2018-08-21 03:15:50 -0700999int BV (clib_bihash_add_del)
1000 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
1001{
1002 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
1003}
1004
1005int BV (clib_bihash_add_or_overwrite_stale)
1006 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
1007 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
1008{
1009 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
1010}
1011
Dave Barachc3799992016-08-15 11:12:27 -04001012int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -04001013 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -04001014 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015{
Damjan Marion68e5fd52020-04-23 13:41:47 +02001016 return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017}
1018
Dave Barachc3799992016-08-15 11:12:27 -04001019u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020{
Dave Barachc3799992016-08-15 11:12:27 -04001021 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -04001023 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001024 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 int i, j, k;
1026 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -05001027 u64 active_buckets = 0;
1028 u64 linear_buckets = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001029
Damjan Marionf2b4a372020-09-30 14:15:24 +02001030 s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -04001031
Dave Barach16e4a4a2020-04-16 12:00:14 -04001032#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001033 if (PREDICT_FALSE (h->instantiated == 0))
Damjan Marionf2b4a372020-09-30 14:15:24 +02001034 return format (s, " empty, uninitialized");
Dave Barach16e4a4a2020-04-16 12:00:14 -04001035#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001036
Ed Warnickecb9cada2015-12-08 15:45:58 -07001037 for (i = 0; i < h->nbuckets; i++)
1038 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001039 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001040 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001041 {
1042 if (verbose > 1)
1043 s = format (s, "[%d]: empty\n", i);
1044 continue;
1045 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046
Dave Barache7d212f2018-02-07 13:14:06 -05001047 active_buckets++;
1048
1049 if (b->linear_search)
1050 linear_buckets++;
1051
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -04001053 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001054 s = format
1055 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
1056 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -04001057 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001058
Dave Barachc3799992016-08-15 11:12:27 -04001059 v = BV (clib_bihash_get_value) (h, b->offset);
1060 for (j = 0; j < (1 << b->log2_pages); j++)
1061 {
1062 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1063 {
1064 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1065 {
1066 if (verbose > 1)
1067 s = format (s, " %d: empty\n",
1068 j * BIHASH_KVP_PER_PAGE + k);
1069 continue;
1070 }
1071 if (verbose)
1072 {
Damjan Marionf2b4a372020-09-30 14:15:24 +02001073 if (h->kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001074 {
1075 s = format (s, " %d: %U\n",
1076 j * BIHASH_KVP_PER_PAGE + k,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001077 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001078 }
1079 else
1080 {
1081 s = format (s, " %d: %U\n",
1082 j * BIHASH_KVP_PER_PAGE + k,
1083 BV (format_bihash_kvp), &(v->kvp[k]));
1084 }
Dave Barachc3799992016-08-15 11:12:27 -04001085 }
1086 active_elements++;
1087 }
1088 v++;
1089 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001090 }
1091
Dave Barache7d212f2018-02-07 13:14:06 -05001092 s = format (s, " %lld active elements %lld active buckets\n",
1093 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001094 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -05001095
1096 for (i = 0; i < vec_len (h->freelists); i++)
1097 {
1098 u32 nfree = 0;
1099 BVT (clib_bihash_value) * free_elt;
Dave Barachffb14b92018-09-11 17:20:23 -04001100 u64 free_elt_as_u64 = h->freelists[i];
Dave Barache7d212f2018-02-07 13:14:06 -05001101
Dave Barachffb14b92018-09-11 17:20:23 -04001102 while (free_elt_as_u64)
Dave Barache7d212f2018-02-07 13:14:06 -05001103 {
Dave Barachffb14b92018-09-11 17:20:23 -04001104 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
Dave Barache7d212f2018-02-07 13:14:06 -05001105 nfree++;
Dave Barachffb14b92018-09-11 17:20:23 -04001106 free_elt_as_u64 = free_elt->next_free_as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -05001107 }
1108
Dave Barach9466c452018-08-24 17:21:14 -04001109 if (nfree || verbose)
1110 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
Dave Barache7d212f2018-02-07 13:14:06 -05001111 }
1112
1113 s = format (s, " %lld linear search buckets\n", linear_buckets);
Damjan Marion2454de22020-09-26 19:32:34 +02001114 if (BIHASH_USE_HEAP)
1115 {
1116 BVT (clib_bihash_alloc_chunk) * c = h->chunks;
1117 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1118
1119 while (c)
1120 {
1121 bytes_left += c->bytes_left;
1122 total_size += c->size;
1123 n_chunks += 1;
1124 c = c->next;
1125 }
1126 s = format (s,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001127 " heap: %u chunk(s) allocated\n"
1128 " bytes: used %U, scrap %U\n", n_chunks,
Damjan Marion2454de22020-09-26 19:32:34 +02001129 format_memory_size, total_size,
1130 format_memory_size, bytes_left);
1131 }
1132 else
1133 {
1134 u64 used_bytes = alloc_arena_next (h);
1135 s = format (s,
1136 " arena: base %llx, next %llx\n"
1137 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1138 alloc_arena (h), alloc_arena_next (h),
1139 used_bytes, used_bytes >> 20,
1140 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1141 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142 return s;
1143}
1144
Dave Barachc3799992016-08-15 11:12:27 -04001145void BV (clib_bihash_foreach_key_value_pair)
Neale Rannsf50bac12019-12-06 05:53:17 +00001146 (BVT (clib_bihash) * h,
1147 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001148{
1149 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -04001150 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001151 BVT (clib_bihash_value) * v;
Dave Barachc3799992016-08-15 11:12:27 -04001152
Dave Barach16e4a4a2020-04-16 12:00:14 -04001153
1154#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001155 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -04001156 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -04001157#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001158
Ed Warnickecb9cada2015-12-08 15:45:58 -07001159 for (i = 0; i < h->nbuckets; i++)
1160 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001161 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001162 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001163 continue;
1164
1165 v = BV (clib_bihash_get_value) (h, b->offset);
1166 for (j = 0; j < (1 << b->log2_pages); j++)
1167 {
1168 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1169 {
1170 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1171 continue;
1172
Neale Rannsf50bac12019-12-06 05:53:17 +00001173 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1174 return;
Dave Barachca45ee72018-08-06 08:43:47 -04001175 /*
1176 * In case the callback deletes the last entry in the bucket...
1177 */
1178 if (BV (clib_bihash_bucket_is_empty) (b))
1179 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -04001180 }
1181 v++;
1182 }
Dave Barachca45ee72018-08-06 08:43:47 -04001183 doublebreak:
1184 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001185 }
1186}
Dave Barachdd3a57f2016-07-27 16:58:51 -04001187
Chris Luke16bcf7d2016-09-01 14:31:46 -04001188/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -04001189
1190/*
1191 * fd.io coding-style-patch-verification: ON
1192 *
1193 * Local Variables:
1194 * eval: (c-set-style "gnu")
1195 * End:
1196 */