blob: ddaccbdb126fd7cbba57725f65a26945cea37f35 [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;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200204 h->fmt_fn = BV (format_bihash);
205 h->kvp_fmt_fn = a->kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400206
Dave Barach32dcd3b2019-07-08 12:25:38 -0400207 alloc_arena (h) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208
Dave Barach508498f2018-07-19 12:11:16 -0400209 /*
210 * Make sure the requested size is rational. The max table
211 * size without playing the alignment card is 64 Gbytes.
212 * If someone starts complaining that's not enough, we can shift
213 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
214 */
Damjan Marion2454de22020-09-26 19:32:34 +0200215 if (BIHASH_USE_HEAP)
216 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400217
218 /* Add this hash table to the list */
Dave Barachbdf9b972019-09-03 10:57:19 -0400219 if (a->dont_add_to_all_bihash_list == 0)
220 {
221 for (i = 0; i < vec_len (clib_all_bihashes); i++)
222 if (clib_all_bihashes[i] == h)
223 goto do_lock;
224 oldheap = clib_all_bihash_set_heap ();
225 vec_add1 (clib_all_bihashes, (void *) h);
226 clib_mem_set_heap (oldheap);
227 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400228
Dave Barachbdf9b972019-09-03 10:57:19 -0400229do_lock:
230 if (h->alloc_lock)
231 clib_mem_free ((void *) h->alloc_lock);
Dave Barach67d09e02019-08-01 08:15:01 -0400232
233 /*
234 * Set up the lock now, so we can use it to make the first add
235 * thread-safe
236 */
237 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
238 CLIB_CACHE_LINE_BYTES);
239 h->alloc_lock[0] = 0;
240
Dave Barach16e4a4a2020-04-16 12:00:14 -0400241#if BIHASH_LAZY_INSTANTIATE
Dave Barachbdf9b972019-09-03 10:57:19 -0400242 if (a->instantiate_immediately)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400243#endif
Dave Barachbdf9b972019-09-03 10:57:19 -0400244 BV (clib_bihash_instantiate) (h);
245}
246
247void BV (clib_bihash_init)
248 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
249{
250 BVT (clib_bihash_init2_args) _a, *a = &_a;
251
252 memset (a, 0, sizeof (*a));
253
254 a->h = h;
255 a->name = name;
256 a->nbuckets = nbuckets;
257 a->memory_size = memory_size;
258
259 BV (clib_bihash_init2) (a);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800260}
261
Dave Barach9466c452018-08-24 17:21:14 -0400262#if BIHASH_32_64_SVM
263#if !defined (MFD_ALLOW_SEALING)
264#define MFD_ALLOW_SEALING 0x0002U
265#endif
266
Dave Barachd4a639b2020-08-06 11:38:40 -0400267void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400268 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
Dave Barach9466c452018-08-24 17:21:14 -0400269{
270 uword bucket_size;
271 u8 *mmap_addr;
272 vec_header_t *freelist_vh;
273 int fd;
274
Damjan Marion2454de22020-09-26 19:32:34 +0200275 ASSERT (BIHASH_USE_HEAP == 0);
276
Dave Barachffb14b92018-09-11 17:20:23 -0400277 ASSERT (memory_size < (1ULL << 32));
Dave Barach9466c452018-08-24 17:21:14 -0400278 /* Set up for memfd sharing */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200279 if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, name) == -1)
Dave Barach9466c452018-08-24 17:21:14 -0400280 {
281 clib_unix_warning ("memfd_create");
282 return;
283 }
284
285 if (ftruncate (fd, memory_size) < 0)
286 {
287 clib_unix_warning ("ftruncate");
288 return;
289 }
290
291 /* Not mission-critical, complain and continue */
292 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
293 clib_unix_warning ("fcntl (F_ADD_SEALS)");
294
Dave Barachffb14b92018-09-11 17:20:23 -0400295 mmap_addr = mmap (0, memory_size,
296 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400297
298 if (mmap_addr == MAP_FAILED)
299 {
300 clib_unix_warning ("mmap failed");
301 ASSERT (0);
302 }
303
304 h->sh = (void *) mmap_addr;
305 h->memfd = fd;
306 nbuckets = 1 << (max_log2 (nbuckets));
307
308 h->name = (u8 *) name;
309 h->sh->nbuckets = h->nbuckets = nbuckets;
310 h->log2_nbuckets = max_log2 (nbuckets);
311
312 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400313 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
Dave Barach9466c452018-08-24 17:21:14 -0400314 alloc_arena_size (h) = memory_size;
315
316 bucket_size = nbuckets * sizeof (h->buckets[0]);
317 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200318 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barachffb14b92018-09-11 17:20:23 -0400319 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
Dave Barach9466c452018-08-24 17:21:14 -0400320
321 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
322 h->alloc_lock[0] = 0;
323
Dave Barachffb14b92018-09-11 17:20:23 -0400324 h->sh->alloc_lock_as_u64 =
325 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
326 freelist_vh =
327 BV (alloc_aligned) (h,
328 sizeof (vec_header_t) +
329 BIHASH_FREELIST_LENGTH * sizeof (u64));
Dave Barach9466c452018-08-24 17:21:14 -0400330 freelist_vh->len = BIHASH_FREELIST_LENGTH;
Dave Barachffb14b92018-09-11 17:20:23 -0400331 h->sh->freelists_as_u64 =
332 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
333 h->freelists = (void *) (freelist_vh->vector_data);
Dave Barach9466c452018-08-24 17:21:14 -0400334
Damjan Marionf2b4a372020-09-30 14:15:24 +0200335 h->fmt_fn = BV (format_bihash);
336 h->kvp_fmt_fn = NULL;
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800337 h->instantiated = 1;
Dave Barach9466c452018-08-24 17:21:14 -0400338}
339
Dave Barachd4a639b2020-08-06 11:38:40 -0400340void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400341 (BVT (clib_bihash) * h, char *name, int fd)
342{
343 u8 *mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400344 u64 memory_size;
Dave Barach9466c452018-08-24 17:21:14 -0400345 BVT (clib_bihash_shared_header) * sh;
346
Damjan Marion2454de22020-09-26 19:32:34 +0200347 ASSERT (BIHASH_USE_HEAP == 0);
348
Dave Barachffb14b92018-09-11 17:20:23 -0400349 /* Trial mapping, to learn the segment size */
Dave Barach9466c452018-08-24 17:21:14 -0400350 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
351 if (mmap_addr == MAP_FAILED)
352 {
353 clib_unix_warning ("trial mmap failed");
354 ASSERT (0);
355 }
356
357 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
358
Dave Barach9466c452018-08-24 17:21:14 -0400359 memory_size = sh->alloc_arena_size;
360
361 munmap (mmap_addr, 4096);
362
Dave Barachffb14b92018-09-11 17:20:23 -0400363 /* Actual mapping, at the required size */
364 mmap_addr = mmap (0, memory_size,
365 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400366
367 if (mmap_addr == MAP_FAILED)
368 {
369 clib_unix_warning ("mmap failed");
370 ASSERT (0);
371 }
372
373 (void) close (fd);
374
375 h->sh = (void *) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400376 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barach9466c452018-08-24 17:21:14 -0400377 h->memfd = -1;
378
379 h->name = (u8 *) name;
Dave Barachffb14b92018-09-11 17:20:23 -0400380 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
Dave Barach9466c452018-08-24 17:21:14 -0400381 h->nbuckets = h->sh->nbuckets;
382 h->log2_nbuckets = max_log2 (h->nbuckets);
383
Dave Barachffb14b92018-09-11 17:20:23 -0400384 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
385 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
Damjan Marionf2b4a372020-09-30 14:15:24 +0200386 h->fmt_fn = BV (format_bihash);
387 h->kvp_fmt_fn = NULL;
Dave Barach9466c452018-08-24 17:21:14 -0400388}
389#endif /* BIHASH_32_64_SVM */
390
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800391void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200392 format_function_t * kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800393{
Damjan Marionf2b4a372020-09-30 14:15:24 +0200394 h->kvp_fmt_fn = kvp_fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700395}
396
Neale Rannseb696482020-09-29 14:44:23 +0000397int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h)
398{
399 return (h->instantiated != 0);
400}
401
Dave Barachc3799992016-08-15 11:12:27 -0400402void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700403{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400404 int i;
405
Dave Barach67d09e02019-08-01 08:15:01 -0400406 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400407 goto never_initialized;
408
Dave Barach67d09e02019-08-01 08:15:01 -0400409 h->instantiated = 0;
Damjan Marion2454de22020-09-26 19:32:34 +0200410
411 if (BIHASH_USE_HEAP)
412 {
413 BVT (clib_bihash_alloc_chunk) * next, *chunk;
414 void *oldheap = clib_mem_set_heap (h->heap);
415
416 chunk = h->chunks;
417 while (chunk)
418 {
419 next = chunk->next;
420 clib_mem_free (chunk);
421 chunk = next;
422 }
423 clib_mem_set_heap (oldheap);
424 }
425
Dave Barach97f5af02018-02-22 09:48:45 -0500426 vec_free (h->working_copies);
Vijayabhaskar Katamreddy72739a62019-05-07 13:27:32 -0700427 vec_free (h->working_copy_lengths);
Dave Barach9466c452018-08-24 17:21:14 -0400428#if BIHASH_32_64_SVM == 0
Dave Barach97f5af02018-02-22 09:48:45 -0500429 vec_free (h->freelists);
Dave Barach9466c452018-08-24 17:21:14 -0400430#else
431 if (h->memfd > 0)
432 (void) close (h->memfd);
433#endif
Damjan Marion2454de22020-09-26 19:32:34 +0200434 if (BIHASH_USE_HEAP == 0)
435 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)),
436 alloc_arena_size (h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400437never_initialized:
Damjan Marion2454de22020-09-26 19:32:34 +0200438 clib_memset_u8 (h, 0, sizeof (*h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400439 for (i = 0; i < vec_len (clib_all_bihashes); i++)
440 {
441 if ((void *) h == clib_all_bihashes[i])
442 {
443 vec_delete (clib_all_bihashes, 1, i);
444 return;
445 }
446 }
447 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800448 (u64) (uword) h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449}
450
Dave Barachc3799992016-08-15 11:12:27 -0400451static
452BVT (clib_bihash_value) *
453BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454{
Dave Barachc3799992016-08-15 11:12:27 -0400455 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456
Dave Barach508498f2018-07-19 12:11:16 -0400457 ASSERT (h->alloc_lock[0]);
Dave Barach9466c452018-08-24 17:21:14 -0400458
459#if BIHASH_32_64_SVM
460 ASSERT (log2_pages < vec_len (h->freelists));
461#endif
462
Dave Barachc3799992016-08-15 11:12:27 -0400463 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 {
Dave Barach97f5af02018-02-22 09:48:45 -0500465 vec_validate_init_empty (h->freelists, log2_pages, 0);
466 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -0400467 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 }
Dave Barachffb14b92018-09-11 17:20:23 -0400469 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
Dave Barach9466c452018-08-24 17:21:14 -0400470 h->freelists[log2_pages] = rv->next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471
Dave Barachc3799992016-08-15 11:12:27 -0400472initialize:
473 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -0400474 /*
475 * Latest gcc complains that the length arg is zero
476 * if we replace (1<<log2_pages) with vec_len(rv).
477 * No clue.
478 */
Damjan Marion2454de22020-09-26 19:32:34 +0200479 clib_memset_u8 (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
Dave Barachc3799992016-08-15 11:12:27 -0400480 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700481}
482
483static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400484BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
485 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486{
Dave Barach508498f2018-07-19 12:11:16 -0400487 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700488
Dave Barachc3799992016-08-15 11:12:27 -0400489 ASSERT (vec_len (h->freelists) > log2_pages);
490
Damjan Marion2454de22020-09-26 19:32:34 +0200491 if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES)
492 {
493 /* allocations bigger or equal to chunk size always contain single
494 * alloc and they can be given back to heap */
495 void *oldheap;
496 BVT (clib_bihash_alloc_chunk) * c;
497 c = (BVT (clib_bihash_alloc_chunk) *) v - 1;
498
499 if (c->prev)
500 c->prev->next = c->next;
501 else
502 h->chunks = c->next;
503
504 if (c->next)
505 c->next->prev = c->prev;
506
507 oldheap = clib_mem_set_heap (h->heap);
508 clib_mem_free (c);
509 clib_mem_set_heap (oldheap);
510 return;
511 }
512
Dave Barach508498f2018-07-19 12:11:16 -0400513 if (CLIB_DEBUG > 0)
Damjan Marion2454de22020-09-26 19:32:34 +0200514 clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages));
Dave Barach508498f2018-07-19 12:11:16 -0400515
Dave Barach9466c452018-08-24 17:21:14 -0400516 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
Dave Barachffb14b92018-09-11 17:20:23 -0400517 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518}
519
520static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400521BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522{
Dave Barachc3799992016-08-15 11:12:27 -0400523 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400524 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400525 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200526 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400527 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
Dave Barach508498f2018-07-19 12:11:16 -0400529 ASSERT (h->alloc_lock[0]);
530
Damjan Marionf55f9b82017-05-10 21:06:28 +0200531 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200533 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700534 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 }
536
Dave Barachc3799992016-08-15 11:12:27 -0400537 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 * working_copies are per-cpu so that near-simultaneous
539 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400540 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200542 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400543 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700544
545 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700546
Dave Barachba7ddfe2017-05-17 20:20:50 -0400547 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 {
Dave Barach97f5af02018-02-22 09:48:45 -0500549 /*
550 * It's not worth the bookkeeping to free working copies
551 * if (working_copy)
552 * clib_mem_free (working_copy);
553 */
554 working_copy = BV (alloc_aligned)
555 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400556 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200557 h->working_copies[thread_index] = working_copy;
Dave Barach2ce28d62019-05-03 12:58:01 -0400558
559 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
560 1ULL << b->log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561 }
562
Dave Barachc3799992016-08-15 11:12:27 -0400563 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700564
Dave Barach178cf492018-11-13 16:34:13 -0500565 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400567 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200568 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200570 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571}
572
Dave Barachc3799992016-08-15 11:12:27 -0400573static
574BVT (clib_bihash_value) *
575BV (split_and_rehash)
576 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400577 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
578 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579{
Dave Barach5e6b9582016-12-12 15:37:29 -0500580 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400581 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582
Dave Barach508498f2018-07-19 12:11:16 -0400583 ASSERT (h->alloc_lock[0]);
584
Dave Barachc3799992016-08-15 11:12:27 -0400585 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400586 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587
Dave Barachba7ddfe2017-05-17 20:20:50 -0400588 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 {
590 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400591
Dave Barach5e6b9582016-12-12 15:37:29 -0500592 /* Entry not in use? Forget it */
593 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
594 continue;
595
596 /* rehash the item onto its new home-page */
597 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
Damjan Marion68e5fd52020-04-23 13:41:47 +0200598 new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500599 new_v = &new_values[new_hash];
600
601 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400603 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500604 /* Empty slot */
605 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400606 {
Dave Barach178cf492018-11-13 16:34:13 -0500607 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
608 sizeof (new_v->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500609 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400610 }
Dave Barachc3799992016-08-15 11:12:27 -0400611 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500612 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400613 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500614 return 0;
615 doublebreak:;
616 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400617
Dave Barach5e6b9582016-12-12 15:37:29 -0500618 return new_values;
619}
620
621static
622BVT (clib_bihash_value) *
623BV (split_and_rehash_linear)
624 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400625 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
626 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500627{
628 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400629 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500630
Dave Barach508498f2018-07-19 12:11:16 -0400631 ASSERT (h->alloc_lock[0]);
632
Dave Barach5e6b9582016-12-12 15:37:29 -0500633 new_values = BV (value_alloc) (h, new_log2_pages);
634 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400635 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500636
637 j = 0;
638 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400639 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500640 {
641 /* Find a free slot in the new linear scan bucket */
642 for (; j < new_length; j++)
643 {
Dave Barach8f544962017-01-18 10:23:22 -0500644 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500645 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
646 goto doublebreak;
647
648 /* New value should never be in use */
649 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
650 {
651 /* Copy the old value and move along */
Dave Barach178cf492018-11-13 16:34:13 -0500652 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
653 sizeof (new_values->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500654 j++;
655 goto doublebreak;
656 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500657 }
Dave Barach8f544962017-01-18 10:23:22 -0500658 /* This should never happen... */
659 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400660 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500661 return 0;
662
Dave Barach5e6b9582016-12-12 15:37:29 -0500663 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700664 }
665 return new_values;
666}
667
Damjan Marion801ec2a2020-04-21 19:42:30 +0200668static_always_inline int BV (clib_bihash_add_del_inline_with_hash)
669 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
Matus Fabian828d27e2018-08-21 03:15:50 -0700670 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700671{
Dave Barach908a5ea2017-07-14 12:42:21 -0400672 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400673 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500674 int i, limit;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200675 u64 new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400676 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200677 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500678 int mark_bucket_linear;
679 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Damjan Marion68e5fd52020-04-23 13:41:47 +0200681 /* *INDENT-OFF* */
682 static const BVT (clib_bihash_bucket) mask = {
683 .linear_search = 1,
684 .log2_pages = -1
685 };
686 /* *INDENT-ON* */
687
688#if BIHASH_LAZY_INSTANTIATE
Dave Barach67d09e02019-08-01 08:15:01 -0400689 /*
690 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
691 * Use the alloc_lock to protect the instantiate operation.
692 */
693 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400694 {
695 if (is_add == 0)
696 return (-1);
Dave Barach67d09e02019-08-01 08:15:01 -0400697
698 BV (clib_bihash_alloc_lock) (h);
699 if (h->instantiated == 0)
700 BV (clib_bihash_instantiate) (h);
701 BV (clib_bihash_alloc_unlock) (h);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400702 }
Dave Baracha90ba642020-04-23 16:56:15 -0400703#else
704 /* Debug image: make sure the table has been instantiated */
705 ASSERT (h->instantiated != 0);
Damjan Marion68e5fd52020-04-23 13:41:47 +0200706#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400707
Damjan Marion4e149772020-03-27 16:57:28 +0100708 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700709
Dave Barach508498f2018-07-19 12:11:16 -0400710 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711
712 /* First elt in the bucket? */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400713 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700714 {
715 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400716 {
Dave Barach508498f2018-07-19 12:11:16 -0400717 BV (clib_bihash_unlock_bucket) (b);
718 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400719 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700720
Dave Barach508498f2018-07-19 12:11:16 -0400721 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400722 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400723 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400724
Dave Barachc3799992016-08-15 11:12:27 -0400725 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400726 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400727 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500728 tmp_b.refcnt = 1;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200729 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730
Tom Seidenberg97f8ae92019-03-15 10:15:26 -0400731 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
Dave Barach2ce28d62019-05-03 12:58:01 -0400732 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
733
Dave Barach508498f2018-07-19 12:11:16 -0400734 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700735 }
736
Dave Barach508498f2018-07-19 12:11:16 -0400737 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500738 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400739 v = BV (clib_bihash_get_value) (h, b->offset);
740
Damjan Marion68e5fd52020-04-23 13:41:47 +0200741 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
742 {
743 if (PREDICT_FALSE (b->linear_search))
744 limit <<= b->log2_pages;
745 else
746 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
747 }
Dave Barachc3799992016-08-15 11:12:27 -0400748
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749 if (is_add)
750 {
Dave Barachc3799992016-08-15 11:12:27 -0400751 /*
Dave Barach508498f2018-07-19 12:11:16 -0400752 * Because reader threads are looking at live data,
753 * we have to be extra careful. Readers do NOT hold the
754 * bucket lock. We need to be SLOWER than a search, past the
755 * point where readers CHECK the bucket lock.
756 */
757
758 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700759 * For obvious (in hindsight) reasons, see if we're supposed to
760 * replace an existing key, then look for an empty slot.
761 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500762 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400763 {
Dave Baracha11bf452019-04-17 17:27:31 -0400764 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400765 {
Dave Barach9e4946b2019-07-08 14:47:44 -0400766 /* Add but do not overwrite? */
767 if (is_add == 2)
768 {
769 BV (clib_bihash_unlock_bucket) (b);
770 return (-2);
771 }
772
Damjan Marion801ec2a2020-04-21 19:42:30 +0200773 clib_memcpy_fast (&(v->kvp[i].value),
774 &add_v->value, sizeof (add_v->value));
Dave Barach508498f2018-07-19 12:11:16 -0400775 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400776 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400777 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400778 }
779 }
Dave Barach508498f2018-07-19 12:11:16 -0400780 /*
781 * Look for an empty slot. If found, use it
782 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500783 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400784 {
785 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
786 {
Dave Barach508498f2018-07-19 12:11:16 -0400787 /*
788 * Copy the value first, so that if a reader manages
789 * to match the new key, the value will be right...
790 */
Dave Barach178cf492018-11-13 16:34:13 -0500791 clib_memcpy_fast (&(v->kvp[i].value),
792 &add_v->value, sizeof (add_v->value));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200793 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
Dave Barach178cf492018-11-13 16:34:13 -0500794 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
795 sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500796 b->refcnt++;
Dave Barach9466c452018-08-24 17:21:14 -0400797 ASSERT (b->refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400798 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400799 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400800 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400801 }
802 }
Matus Fabian828d27e2018-08-21 03:15:50 -0700803 /* look for stale data to overwrite */
804 if (is_stale_cb)
805 {
806 for (i = 0; i < limit; i++)
807 {
808 if (is_stale_cb (&(v->kvp[i]), arg))
809 {
Dave Barach178cf492018-11-13 16:34:13 -0500810 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200811 CLIB_MEMORY_STORE_BARRIER ();
Matus Fabian828d27e2018-08-21 03:15:50 -0700812 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400813 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Matus Fabian828d27e2018-08-21 03:15:50 -0700814 return (0);
815 }
816 }
817 }
Dave Barach508498f2018-07-19 12:11:16 -0400818 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700819 }
Dave Barach508498f2018-07-19 12:11:16 -0400820 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700821 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500822 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400823 {
Dave Barach508498f2018-07-19 12:11:16 -0400824 /* Found the key? Kill it... */
Dave Baracha11bf452019-04-17 17:27:31 -0400825 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400826 {
Damjan Marion801ec2a2020-04-21 19:42:30 +0200827 clib_memset_u8 (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
Dave Barach508498f2018-07-19 12:11:16 -0400828 /* Is the bucket empty? */
829 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500830 {
Dave Barach508498f2018-07-19 12:11:16 -0400831 b->refcnt--;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400832 /* Switch back to the bucket-level kvp array? */
833 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
834 && b->log2_pages > 0)
835 {
836 tmp_b.as_u64 = b->as_u64;
837 b->offset = BV (clib_bihash_get_offset)
838 (h, (void *) (b + 1));
839 b->linear_search = 0;
840 b->log2_pages = 0;
841 /* Clean up the bucket-level kvp array */
Damjan Marion801ec2a2020-04-21 19:42:30 +0200842 clib_memset_u8 ((b + 1), 0xff, BIHASH_KVP_PER_PAGE *
843 sizeof (BVT (clib_bihash_kv)));
844 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach16e4a4a2020-04-16 12:00:14 -0400845 BV (clib_bihash_unlock_bucket) (b);
846 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
847 goto free_backing_store;
848 }
849
Damjan Marion801ec2a2020-04-21 19:42:30 +0200850 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach508498f2018-07-19 12:11:16 -0400851 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400852 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400853 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500854 }
Dave Barach508498f2018-07-19 12:11:16 -0400855 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500856 {
Dave Barach508498f2018-07-19 12:11:16 -0400857 /* Save old bucket value, need log2_pages to free it */
858 tmp_b.as_u64 = b->as_u64;
Dave Barach508498f2018-07-19 12:11:16 -0400859
860 /* Kill and unlock the bucket */
861 b->as_u64 = 0;
862
Dave Barach16e4a4a2020-04-16 12:00:14 -0400863 free_backing_store:
Dave Barach508498f2018-07-19 12:11:16 -0400864 /* And free the backing storage */
865 BV (clib_bihash_alloc_lock) (h);
866 /* Note: v currently points into the middle of the bucket */
867 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
868 BV (value_free) (h, v, tmp_b.log2_pages);
869 BV (clib_bihash_alloc_unlock) (h);
Dave Barach2ce28d62019-05-03 12:58:01 -0400870 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
871 1);
Dave Barach508498f2018-07-19 12:11:16 -0400872 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500873 }
Dave Barachc3799992016-08-15 11:12:27 -0400874 }
875 }
Dave Barach508498f2018-07-19 12:11:16 -0400876 /* Not found... */
877 BV (clib_bihash_unlock_bucket) (b);
878 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700879 }
880
Dave Barach508498f2018-07-19 12:11:16 -0400881 /* Move readers to a (locked) temp copy of the bucket */
882 BV (clib_bihash_alloc_lock) (h);
883 BV (make_working_copy) (h, b);
884
885 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
886
Dave Barachba7ddfe2017-05-17 20:20:50 -0400887 old_log2_pages = h->saved_bucket.log2_pages;
888 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500889 mark_bucket_linear = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400890 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
891 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700892
Damjan Marionf55f9b82017-05-10 21:06:28 +0200893 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500894 resplit_once = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400895 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500896
Dave Barachba7ddfe2017-05-17 20:20:50 -0400897 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
898 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700899 if (new_v == 0)
900 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500901 try_resplit:
902 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700903 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500904 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400905 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
906 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500907 if (new_v == 0)
908 {
909 mark_linear:
910 new_log2_pages--;
911 /* pinned collisions, use linear search */
912 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400913 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
914 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500915 mark_bucket_linear = 1;
Dave Barach2ce28d62019-05-03 12:58:01 -0400916 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500917 }
Dave Barach2ce28d62019-05-03 12:58:01 -0400918 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
919 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
920 old_log2_pages + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700921 }
922
923 /* Try to add the new entry */
924 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400925 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500926 limit = BIHASH_KVP_PER_PAGE;
927 if (mark_bucket_linear)
928 limit <<= new_log2_pages;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200929 else
930 new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barachc3799992016-08-15 11:12:27 -0400931
Dave Barach5e6b9582016-12-12 15:37:29 -0500932 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700933 {
Dave Barachc3799992016-08-15 11:12:27 -0400934 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
935 {
Dave Barach178cf492018-11-13 16:34:13 -0500936 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
Dave Barachc3799992016-08-15 11:12:27 -0400937 goto expand_ok;
938 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700939 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400940
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400942 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500943 /*
944 * If we've already doubled the size of the bucket once,
945 * fall back to linear search now.
946 */
947 if (resplit_once)
948 goto mark_linear;
949 else
950 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
Dave Barachc3799992016-08-15 11:12:27 -0400952expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500953 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400954 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500955 tmp_b.linear_search = mark_bucket_linear;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400956#if BIHASH_KVP_AT_BUCKET_LEVEL
957 /* Compensate for permanent refcount bump at the bucket level */
958 if (new_log2_pages > 0)
959#endif
960 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach9466c452018-08-24 17:21:14 -0400961 ASSERT (tmp_b.refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400962 tmp_b.lock = 0;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200963 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964 b->as_u64 = tmp_b.as_u64;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400965
966#if BIHASH_KVP_AT_BUCKET_LEVEL
967 if (h->saved_bucket.log2_pages > 0)
968 {
969#endif
970
971 /* free the old bucket, except at the bucket level if so configured */
972 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
973 BV (value_free) (h, v, h->saved_bucket.log2_pages);
974
975#if BIHASH_KVP_AT_BUCKET_LEVEL
976 }
977#endif
978
979
Dave Barach508498f2018-07-19 12:11:16 -0400980 BV (clib_bihash_alloc_unlock) (h);
981 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700982}
983
Damjan Marion801ec2a2020-04-21 19:42:30 +0200984static_always_inline int BV (clib_bihash_add_del_inline)
985 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
986 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
987{
988 u64 hash = BV (clib_bihash_hash) (add_v);
989 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
990 is_stale_cb, arg);
991}
992
Matus Fabian828d27e2018-08-21 03:15:50 -0700993int BV (clib_bihash_add_del)
994 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
995{
996 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
997}
998
999int BV (clib_bihash_add_or_overwrite_stale)
1000 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
1001 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
1002{
1003 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
1004}
1005
Dave Barachc3799992016-08-15 11:12:27 -04001006int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -04001007 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -04001008 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009{
Damjan Marion68e5fd52020-04-23 13:41:47 +02001010 return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011}
1012
Dave Barachc3799992016-08-15 11:12:27 -04001013u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014{
Dave Barachc3799992016-08-15 11:12:27 -04001015 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001016 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -04001017 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001018 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001019 int i, j, k;
1020 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -05001021 u64 active_buckets = 0;
1022 u64 linear_buckets = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
Damjan Marionf2b4a372020-09-30 14:15:24 +02001024 s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -04001025
Dave Barach16e4a4a2020-04-16 12:00:14 -04001026#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001027 if (PREDICT_FALSE (h->instantiated == 0))
Damjan Marionf2b4a372020-09-30 14:15:24 +02001028 return format (s, " empty, uninitialized");
Dave Barach16e4a4a2020-04-16 12:00:14 -04001029#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001030
Ed Warnickecb9cada2015-12-08 15:45:58 -07001031 for (i = 0; i < h->nbuckets; i++)
1032 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001033 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001034 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001035 {
1036 if (verbose > 1)
1037 s = format (s, "[%d]: empty\n", i);
1038 continue;
1039 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040
Dave Barache7d212f2018-02-07 13:14:06 -05001041 active_buckets++;
1042
1043 if (b->linear_search)
1044 linear_buckets++;
1045
Ed Warnickecb9cada2015-12-08 15:45:58 -07001046 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -04001047 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001048 s = format
1049 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
1050 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -04001051 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052
Dave Barachc3799992016-08-15 11:12:27 -04001053 v = BV (clib_bihash_get_value) (h, b->offset);
1054 for (j = 0; j < (1 << b->log2_pages); j++)
1055 {
1056 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1057 {
1058 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1059 {
1060 if (verbose > 1)
1061 s = format (s, " %d: empty\n",
1062 j * BIHASH_KVP_PER_PAGE + k);
1063 continue;
1064 }
1065 if (verbose)
1066 {
Damjan Marionf2b4a372020-09-30 14:15:24 +02001067 if (h->kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001068 {
1069 s = format (s, " %d: %U\n",
1070 j * BIHASH_KVP_PER_PAGE + k,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001071 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001072 }
1073 else
1074 {
1075 s = format (s, " %d: %U\n",
1076 j * BIHASH_KVP_PER_PAGE + k,
1077 BV (format_bihash_kvp), &(v->kvp[k]));
1078 }
Dave Barachc3799992016-08-15 11:12:27 -04001079 }
1080 active_elements++;
1081 }
1082 v++;
1083 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001084 }
1085
Dave Barache7d212f2018-02-07 13:14:06 -05001086 s = format (s, " %lld active elements %lld active buckets\n",
1087 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -05001089
1090 for (i = 0; i < vec_len (h->freelists); i++)
1091 {
1092 u32 nfree = 0;
1093 BVT (clib_bihash_value) * free_elt;
Dave Barachffb14b92018-09-11 17:20:23 -04001094 u64 free_elt_as_u64 = h->freelists[i];
Dave Barache7d212f2018-02-07 13:14:06 -05001095
Dave Barachffb14b92018-09-11 17:20:23 -04001096 while (free_elt_as_u64)
Dave Barache7d212f2018-02-07 13:14:06 -05001097 {
Dave Barachffb14b92018-09-11 17:20:23 -04001098 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
Dave Barache7d212f2018-02-07 13:14:06 -05001099 nfree++;
Dave Barachffb14b92018-09-11 17:20:23 -04001100 free_elt_as_u64 = free_elt->next_free_as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -05001101 }
1102
Dave Barach9466c452018-08-24 17:21:14 -04001103 if (nfree || verbose)
1104 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
Dave Barache7d212f2018-02-07 13:14:06 -05001105 }
1106
1107 s = format (s, " %lld linear search buckets\n", linear_buckets);
Damjan Marion2454de22020-09-26 19:32:34 +02001108 if (BIHASH_USE_HEAP)
1109 {
1110 BVT (clib_bihash_alloc_chunk) * c = h->chunks;
1111 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1112
1113 while (c)
1114 {
1115 bytes_left += c->bytes_left;
1116 total_size += c->size;
1117 n_chunks += 1;
1118 c = c->next;
1119 }
1120 s = format (s,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001121 " heap: %u chunk(s) allocated\n"
1122 " bytes: used %U, scrap %U\n", n_chunks,
Damjan Marion2454de22020-09-26 19:32:34 +02001123 format_memory_size, total_size,
1124 format_memory_size, bytes_left);
1125 }
1126 else
1127 {
1128 u64 used_bytes = alloc_arena_next (h);
1129 s = format (s,
1130 " arena: base %llx, next %llx\n"
1131 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1132 alloc_arena (h), alloc_arena_next (h),
1133 used_bytes, used_bytes >> 20,
1134 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1135 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001136 return s;
1137}
1138
Dave Barachc3799992016-08-15 11:12:27 -04001139void BV (clib_bihash_foreach_key_value_pair)
Neale Rannsf50bac12019-12-06 05:53:17 +00001140 (BVT (clib_bihash) * h,
1141 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142{
1143 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -04001144 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001145 BVT (clib_bihash_value) * v;
Dave Barachc3799992016-08-15 11:12:27 -04001146
Dave Barach16e4a4a2020-04-16 12:00:14 -04001147
1148#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001149 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -04001150 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -04001151#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001152
Ed Warnickecb9cada2015-12-08 15:45:58 -07001153 for (i = 0; i < h->nbuckets; i++)
1154 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001155 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001156 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001157 continue;
1158
1159 v = BV (clib_bihash_get_value) (h, b->offset);
1160 for (j = 0; j < (1 << b->log2_pages); j++)
1161 {
1162 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1163 {
1164 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1165 continue;
1166
Neale Rannsf50bac12019-12-06 05:53:17 +00001167 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1168 return;
Dave Barachca45ee72018-08-06 08:43:47 -04001169 /*
1170 * In case the callback deletes the last entry in the bucket...
1171 */
1172 if (BV (clib_bihash_bucket_is_empty) (b))
1173 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -04001174 }
1175 v++;
1176 }
Dave Barachca45ee72018-08-06 08:43:47 -04001177 doublebreak:
1178 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001179 }
1180}
Dave Barachdd3a57f2016-07-27 16:58:51 -04001181
Chris Luke16bcf7d2016-09-01 14:31:46 -04001182/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -04001183
1184/*
1185 * fd.io coding-style-patch-verification: ON
1186 *
1187 * Local Variables:
1188 * eval: (c-set-style "gnu")
1189 * End:
1190 */