blob: 8094321bc8989c70e4c74e7781309b480d285550 [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;
Tom Jones67e7f012024-01-29 15:16:27 +0000109#if __linux__
Damjan Marione6db7752020-05-24 20:43:10 +0200110 int mmap_flags_huge = (mmap_flags | MAP_HUGETLB | MAP_LOCKED |
Dave Barach16e4a4a2020-04-16 12:00:14 -0400111 BIHASH_LOG2_HUGEPAGE_SIZE << MAP_HUGE_SHIFT);
Tom Jones67e7f012024-01-29 15:16:27 +0000112#endif /* __linux__ */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400113
114 /* new allocation is 25% of existing one */
115 if (alloc_arena_mapped (h) >> 2 > alloc)
116 alloc = alloc_arena_mapped (h) >> 2;
117
118 /* round allocation to page size */
119 alloc = round_pow2 (alloc, 1 << BIHASH_LOG2_HUGEPAGE_SIZE);
120
121 base = (void *) (uword) (alloc_arena (h) + alloc_arena_mapped (h));
122
Tom Jones67e7f012024-01-29 15:16:27 +0000123#if __linux__
Dave Barach16e4a4a2020-04-16 12:00:14 -0400124 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags_huge, -1, 0);
Tom Jones67e7f012024-01-29 15:16:27 +0000125#elif __FreeBSD__
126 rv = MAP_FAILED;
127#endif /* __linux__ */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400128
129 /* fallback - maybe we are still able to allocate normal pages */
Damjan Marion6183cf42020-05-27 16:43:35 +0200130 if (rv == MAP_FAILED || mlock (base, alloc) != 0)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400131 rv = mmap (base, alloc, PROT_READ | PROT_WRITE, mmap_flags, -1, 0);
132
133 if (rv == MAP_FAILED)
134 os_out_of_memory ();
135
136 alloc_arena_mapped (h) += alloc;
137 }
138
Dave Barachffb14b92018-09-11 17:20:23 -0400139 return (void *) (uword) (rv + alloc_arena (h));
Dave Barach97f5af02018-02-22 09:48:45 -0500140}
141
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800142static void BV (clib_bihash_instantiate) (BVT (clib_bihash) * h)
Dave Barach32dcd3b2019-07-08 12:25:38 -0400143{
144 uword bucket_size;
145
Damjan Marion2454de22020-09-26 19:32:34 +0200146 if (BIHASH_USE_HEAP)
147 {
148 h->heap = clib_mem_get_heap ();
149 h->chunks = 0;
150 alloc_arena (h) = (uword) clib_mem_get_heap_base (h->heap);
151 }
152 else
153 {
154 alloc_arena (h) = clib_mem_vm_reserve (0, h->memory_size,
155 BIHASH_LOG2_HUGEPAGE_SIZE);
156 if (alloc_arena (h) == ~0)
157 os_out_of_memory ();
158 alloc_arena_next (h) = 0;
159 alloc_arena_size (h) = h->memory_size;
160 alloc_arena_mapped (h) = 0;
161 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400162
163 bucket_size = h->nbuckets * sizeof (h->buckets[0]);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400164
165 if (BIHASH_KVP_AT_BUCKET_LEVEL)
166 bucket_size +=
167 h->nbuckets * BIHASH_KVP_PER_PAGE * sizeof (BVT (clib_bihash_kv));
168
Dave Barach32dcd3b2019-07-08 12:25:38 -0400169 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200170 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barach16e4a4a2020-04-16 12:00:14 -0400171
172 if (BIHASH_KVP_AT_BUCKET_LEVEL)
173 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400174 int i, j;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400175 BVT (clib_bihash_bucket) * b;
176
177 b = h->buckets;
178
179 for (i = 0; i < h->nbuckets; i++)
180 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400181 BVT (clib_bihash_kv) * v;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400182 b->offset = BV (clib_bihash_get_offset) (h, (void *) (b + 1));
183 b->refcnt = 1;
184 /* Mark all elements free */
Dave Barachb9c8c572023-03-16 13:03:47 -0400185 v = (void *) (b + 1);
186 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
187 {
188 BV (clib_bihash_mark_free) (v);
189 v++;
190 }
Dave Barach16e4a4a2020-04-16 12:00:14 -0400191 /* Compute next bucket start address */
192 b = (void *) (((uword) b) + sizeof (*b) +
193 (BIHASH_KVP_PER_PAGE *
194 sizeof (BVT (clib_bihash_kv))));
195 }
196 }
Damjan Marion801ec2a2020-04-21 19:42:30 +0200197 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach67d09e02019-08-01 08:15:01 -0400198 h->instantiated = 1;
Dave Barach32dcd3b2019-07-08 12:25:38 -0400199}
Dave Barach97f5af02018-02-22 09:48:45 -0500200
Dave Barachbdf9b972019-09-03 10:57:19 -0400201void BV (clib_bihash_init2) (BVT (clib_bihash_init2_args) * a)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700202{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400203 int i;
204 void *oldheap;
Dave Barachbdf9b972019-09-03 10:57:19 -0400205 BVT (clib_bihash) * h = a->h;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700206
Dave Barachbdf9b972019-09-03 10:57:19 -0400207 a->nbuckets = 1 << (max_log2 (a->nbuckets));
208
209 h->name = (u8 *) a->name;
210 h->nbuckets = a->nbuckets;
211 h->log2_nbuckets = max_log2 (a->nbuckets);
Damjan Marion2454de22020-09-26 19:32:34 +0200212 h->memory_size = BIHASH_USE_HEAP ? 0 : a->memory_size;
Dave Barach67d09e02019-08-01 08:15:01 -0400213 h->instantiated = 0;
Nathan Skrzypczaka8c720e2021-08-06 12:03:11 +0200214 h->dont_add_to_all_bihash_list = a->dont_add_to_all_bihash_list;
Damjan Marionf2b4a372020-09-30 14:15:24 +0200215 h->fmt_fn = BV (format_bihash);
216 h->kvp_fmt_fn = a->kvp_fmt_fn;
Dave Barachbdf9b972019-09-03 10:57:19 -0400217
Dave Barach32dcd3b2019-07-08 12:25:38 -0400218 alloc_arena (h) = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700219
Dave Barach508498f2018-07-19 12:11:16 -0400220 /*
221 * Make sure the requested size is rational. The max table
222 * size without playing the alignment card is 64 Gbytes.
223 * If someone starts complaining that's not enough, we can shift
224 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
225 */
Damjan Marion2454de22020-09-26 19:32:34 +0200226 if (BIHASH_USE_HEAP)
227 ASSERT (h->memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400228
229 /* Add this hash table to the list */
Dave Barachbdf9b972019-09-03 10:57:19 -0400230 if (a->dont_add_to_all_bihash_list == 0)
231 {
232 for (i = 0; i < vec_len (clib_all_bihashes); i++)
233 if (clib_all_bihashes[i] == h)
234 goto do_lock;
235 oldheap = clib_all_bihash_set_heap ();
236 vec_add1 (clib_all_bihashes, (void *) h);
237 clib_mem_set_heap (oldheap);
238 }
Dave Barach32dcd3b2019-07-08 12:25:38 -0400239
Dave Barachbdf9b972019-09-03 10:57:19 -0400240do_lock:
241 if (h->alloc_lock)
242 clib_mem_free ((void *) h->alloc_lock);
Dave Barach67d09e02019-08-01 08:15:01 -0400243
244 /*
245 * Set up the lock now, so we can use it to make the first add
246 * thread-safe
247 */
248 h->alloc_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
249 CLIB_CACHE_LINE_BYTES);
250 h->alloc_lock[0] = 0;
251
Dave Barach16e4a4a2020-04-16 12:00:14 -0400252#if BIHASH_LAZY_INSTANTIATE
Dave Barachbdf9b972019-09-03 10:57:19 -0400253 if (a->instantiate_immediately)
Dave Barach16e4a4a2020-04-16 12:00:14 -0400254#endif
Dave Barachbdf9b972019-09-03 10:57:19 -0400255 BV (clib_bihash_instantiate) (h);
256}
257
258void BV (clib_bihash_init)
259 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
260{
261 BVT (clib_bihash_init2_args) _a, *a = &_a;
262
263 memset (a, 0, sizeof (*a));
264
265 a->h = h;
266 a->name = name;
267 a->nbuckets = nbuckets;
268 a->memory_size = memory_size;
269
270 BV (clib_bihash_init2) (a);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800271}
272
Dave Barach9466c452018-08-24 17:21:14 -0400273#if BIHASH_32_64_SVM
274#if !defined (MFD_ALLOW_SEALING)
275#define MFD_ALLOW_SEALING 0x0002U
276#endif
277
Dave Barachd4a639b2020-08-06 11:38:40 -0400278void BV (clib_bihash_initiator_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -0400279 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
Dave Barach9466c452018-08-24 17:21:14 -0400280{
281 uword bucket_size;
282 u8 *mmap_addr;
283 vec_header_t *freelist_vh;
284 int fd;
285
Damjan Marion2454de22020-09-26 19:32:34 +0200286 ASSERT (BIHASH_USE_HEAP == 0);
287
Dave Barachffb14b92018-09-11 17:20:23 -0400288 ASSERT (memory_size < (1ULL << 32));
Dave Barach9466c452018-08-24 17:21:14 -0400289 /* Set up for memfd sharing */
Damjan Marionf8cb7012020-10-09 17:16:55 +0200290 if ((fd = clib_mem_vm_create_fd (CLIB_MEM_PAGE_SZ_DEFAULT, name) == -1)
Dave Barach9466c452018-08-24 17:21:14 -0400291 {
292 clib_unix_warning ("memfd_create");
293 return;
294 }
295
296 if (ftruncate (fd, memory_size) < 0)
297 {
298 clib_unix_warning ("ftruncate");
299 return;
300 }
301
302 /* Not mission-critical, complain and continue */
303 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
304 clib_unix_warning ("fcntl (F_ADD_SEALS)");
305
Dave Barachffb14b92018-09-11 17:20:23 -0400306 mmap_addr = mmap (0, memory_size,
307 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400308
309 if (mmap_addr == MAP_FAILED)
310 {
311 clib_unix_warning ("mmap failed");
312 ASSERT (0);
313 }
314
315 h->sh = (void *) mmap_addr;
316 h->memfd = fd;
317 nbuckets = 1 << (max_log2 (nbuckets));
318
319 h->name = (u8 *) name;
320 h->sh->nbuckets = h->nbuckets = nbuckets;
321 h->log2_nbuckets = max_log2 (nbuckets);
322
323 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400324 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
Dave Barach9466c452018-08-24 17:21:14 -0400325 alloc_arena_size (h) = memory_size;
326
327 bucket_size = nbuckets * sizeof (h->buckets[0]);
328 h->buckets = BV (alloc_aligned) (h, bucket_size);
Damjan Marion2454de22020-09-26 19:32:34 +0200329 clib_memset_u8 (h->buckets, 0, bucket_size);
Dave Barachffb14b92018-09-11 17:20:23 -0400330 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
Dave Barach9466c452018-08-24 17:21:14 -0400331
332 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
333 h->alloc_lock[0] = 0;
334
Dave Barachffb14b92018-09-11 17:20:23 -0400335 h->sh->alloc_lock_as_u64 =
336 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
337 freelist_vh =
338 BV (alloc_aligned) (h,
339 sizeof (vec_header_t) +
340 BIHASH_FREELIST_LENGTH * sizeof (u64));
Dave Barach9466c452018-08-24 17:21:14 -0400341 freelist_vh->len = BIHASH_FREELIST_LENGTH;
Dave Barachffb14b92018-09-11 17:20:23 -0400342 h->sh->freelists_as_u64 =
343 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
344 h->freelists = (void *) (freelist_vh->vector_data);
Dave Barach9466c452018-08-24 17:21:14 -0400345
Damjan Marionf2b4a372020-09-30 14:15:24 +0200346 h->fmt_fn = BV (format_bihash);
347 h->kvp_fmt_fn = NULL;
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800348 h->instantiated = 1;
Dave Barach9466c452018-08-24 17:21:14 -0400349}
350
Dave Barachd4a639b2020-08-06 11:38:40 -0400351void BV (clib_bihash_responder_init_svm)
Dave Barach9466c452018-08-24 17:21:14 -0400352 (BVT (clib_bihash) * h, char *name, int fd)
353{
354 u8 *mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400355 u64 memory_size;
Dave Barach9466c452018-08-24 17:21:14 -0400356 BVT (clib_bihash_shared_header) * sh;
357
Damjan Marion2454de22020-09-26 19:32:34 +0200358 ASSERT (BIHASH_USE_HEAP == 0);
359
Dave Barachffb14b92018-09-11 17:20:23 -0400360 /* Trial mapping, to learn the segment size */
Dave Barach9466c452018-08-24 17:21:14 -0400361 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
362 if (mmap_addr == MAP_FAILED)
363 {
364 clib_unix_warning ("trial mmap failed");
365 ASSERT (0);
366 }
367
368 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
369
Dave Barach9466c452018-08-24 17:21:14 -0400370 memory_size = sh->alloc_arena_size;
371
372 munmap (mmap_addr, 4096);
373
Dave Barachffb14b92018-09-11 17:20:23 -0400374 /* Actual mapping, at the required size */
375 mmap_addr = mmap (0, memory_size,
376 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400377
378 if (mmap_addr == MAP_FAILED)
379 {
380 clib_unix_warning ("mmap failed");
381 ASSERT (0);
382 }
383
384 (void) close (fd);
385
386 h->sh = (void *) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400387 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barach9466c452018-08-24 17:21:14 -0400388 h->memfd = -1;
389
390 h->name = (u8 *) name;
Dave Barachffb14b92018-09-11 17:20:23 -0400391 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
Dave Barach9466c452018-08-24 17:21:14 -0400392 h->nbuckets = h->sh->nbuckets;
393 h->log2_nbuckets = max_log2 (h->nbuckets);
394
Dave Barachffb14b92018-09-11 17:20:23 -0400395 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
396 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
Damjan Marionf2b4a372020-09-30 14:15:24 +0200397 h->fmt_fn = BV (format_bihash);
398 h->kvp_fmt_fn = NULL;
Dave Barach9466c452018-08-24 17:21:14 -0400399}
400#endif /* BIHASH_32_64_SVM */
401
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800402void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
Damjan Marionf2b4a372020-09-30 14:15:24 +0200403 format_function_t * kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800404{
Damjan Marionf2b4a372020-09-30 14:15:24 +0200405 h->kvp_fmt_fn = kvp_fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700406}
407
Neale Rannseb696482020-09-29 14:44:23 +0000408int BV (clib_bihash_is_initialised) (const BVT (clib_bihash) * h)
409{
410 return (h->instantiated != 0);
411}
412
Dave Barachc3799992016-08-15 11:12:27 -0400413void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414{
Dave Barach32dcd3b2019-07-08 12:25:38 -0400415 int i;
416
Dave Barach67d09e02019-08-01 08:15:01 -0400417 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400418 goto never_initialized;
419
Dave Barach67d09e02019-08-01 08:15:01 -0400420 h->instantiated = 0;
Damjan Marion2454de22020-09-26 19:32:34 +0200421
422 if (BIHASH_USE_HEAP)
423 {
424 BVT (clib_bihash_alloc_chunk) * next, *chunk;
425 void *oldheap = clib_mem_set_heap (h->heap);
426
427 chunk = h->chunks;
428 while (chunk)
429 {
430 next = chunk->next;
431 clib_mem_free (chunk);
432 chunk = next;
433 }
434 clib_mem_set_heap (oldheap);
435 }
436
Dave Barach97f5af02018-02-22 09:48:45 -0500437 vec_free (h->working_copies);
Vijayabhaskar Katamreddy72739a62019-05-07 13:27:32 -0700438 vec_free (h->working_copy_lengths);
Han Wu3d5e48b2021-11-19 17:45:40 +0800439 clib_mem_free ((void *) h->alloc_lock);
Dave Barach9466c452018-08-24 17:21:14 -0400440#if BIHASH_32_64_SVM == 0
Dave Barach97f5af02018-02-22 09:48:45 -0500441 vec_free (h->freelists);
Dave Barach9466c452018-08-24 17:21:14 -0400442#else
443 if (h->memfd > 0)
444 (void) close (h->memfd);
445#endif
Damjan Marion2454de22020-09-26 19:32:34 +0200446 if (BIHASH_USE_HEAP == 0)
447 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)),
448 alloc_arena_size (h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400449never_initialized:
Nathan Skrzypczaka8c720e2021-08-06 12:03:11 +0200450 if (h->dont_add_to_all_bihash_list)
451 {
452 clib_memset_u8 (h, 0, sizeof (*h));
453 return;
454 }
Damjan Marion2454de22020-09-26 19:32:34 +0200455 clib_memset_u8 (h, 0, sizeof (*h));
Dave Barach32dcd3b2019-07-08 12:25:38 -0400456 for (i = 0; i < vec_len (clib_all_bihashes); i++)
457 {
458 if ((void *) h == clib_all_bihashes[i])
459 {
460 vec_delete (clib_all_bihashes, 1, i);
461 return;
462 }
463 }
464 clib_warning ("Couldn't find hash table %llx on clib_all_bihashes...",
Vijayabhaskar Katamreddyf0bae642020-01-15 13:45:19 -0800465 (u64) (uword) h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466}
467
Dave Barachc3799992016-08-15 11:12:27 -0400468static
469BVT (clib_bihash_value) *
470BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700471{
Dave Barachb9c8c572023-03-16 13:03:47 -0400472 int i;
Dave Barachc3799992016-08-15 11:12:27 -0400473 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700474
Dave Barach508498f2018-07-19 12:11:16 -0400475 ASSERT (h->alloc_lock[0]);
Dave Barach9466c452018-08-24 17:21:14 -0400476
477#if BIHASH_32_64_SVM
478 ASSERT (log2_pages < vec_len (h->freelists));
479#endif
480
Dave Barachc3799992016-08-15 11:12:27 -0400481 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700482 {
Dave Barach97f5af02018-02-22 09:48:45 -0500483 vec_validate_init_empty (h->freelists, log2_pages, 0);
484 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -0400485 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700486 }
Dave Barachffb14b92018-09-11 17:20:23 -0400487 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
Dave Barach9466c452018-08-24 17:21:14 -0400488 h->freelists[log2_pages] = rv->next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700489
Dave Barachc3799992016-08-15 11:12:27 -0400490initialize:
491 ASSERT (rv);
Dave Barachb9c8c572023-03-16 13:03:47 -0400492
493 BVT (clib_bihash_kv) * v;
494 v = (BVT (clib_bihash_kv) *) rv;
495
496 for (i = 0; i < BIHASH_KVP_PER_PAGE * (1 << log2_pages); i++)
497 {
498 BV (clib_bihash_mark_free) (v);
499 v++;
500 }
Dave Barachc3799992016-08-15 11:12:27 -0400501 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502}
503
504static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400505BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
506 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700507{
Dave Barach508498f2018-07-19 12:11:16 -0400508 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700509
Dave Barachc3799992016-08-15 11:12:27 -0400510 ASSERT (vec_len (h->freelists) > log2_pages);
511
Damjan Marion2454de22020-09-26 19:32:34 +0200512 if (BIHASH_USE_HEAP && log2_pages >= BIIHASH_MIN_ALLOC_LOG2_PAGES)
513 {
514 /* allocations bigger or equal to chunk size always contain single
515 * alloc and they can be given back to heap */
516 void *oldheap;
517 BVT (clib_bihash_alloc_chunk) * c;
518 c = (BVT (clib_bihash_alloc_chunk) *) v - 1;
519
520 if (c->prev)
521 c->prev->next = c->next;
522 else
523 h->chunks = c->next;
524
525 if (c->next)
526 c->next->prev = c->prev;
527
528 oldheap = clib_mem_set_heap (h->heap);
529 clib_mem_free (c);
530 clib_mem_set_heap (oldheap);
531 return;
532 }
533
Dave Barach508498f2018-07-19 12:11:16 -0400534 if (CLIB_DEBUG > 0)
Damjan Marion2454de22020-09-26 19:32:34 +0200535 clib_memset_u8 (v, 0xFE, sizeof (*v) * (1 << log2_pages));
Dave Barach508498f2018-07-19 12:11:16 -0400536
Dave Barach9466c452018-08-24 17:21:14 -0400537 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
Dave Barachffb14b92018-09-11 17:20:23 -0400538 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539}
540
541static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400542BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543{
Dave Barachc3799992016-08-15 11:12:27 -0400544 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400545 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400546 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200547 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400548 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700549
Dave Barach508498f2018-07-19 12:11:16 -0400550 ASSERT (h->alloc_lock[0]);
551
Damjan Marionf55f9b82017-05-10 21:06:28 +0200552 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200554 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700555 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556 }
557
Dave Barachc3799992016-08-15 11:12:27 -0400558 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700559 * working_copies are per-cpu so that near-simultaneous
560 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400561 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200563 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400564 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
566 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700567
Dave Barachba7ddfe2017-05-17 20:20:50 -0400568 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569 {
Dave Barach97f5af02018-02-22 09:48:45 -0500570 /*
571 * It's not worth the bookkeeping to free working copies
572 * if (working_copy)
573 * clib_mem_free (working_copy);
574 */
575 working_copy = BV (alloc_aligned)
576 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400577 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200578 h->working_copies[thread_index] = working_copy;
Dave Barach2ce28d62019-05-03 12:58:01 -0400579
580 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
581 1ULL << b->log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700582 }
583
Dave Barachc3799992016-08-15 11:12:27 -0400584 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700585
Dave Barach178cf492018-11-13 16:34:13 -0500586 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400588 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200589 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200591 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700592}
593
Dave Barachc3799992016-08-15 11:12:27 -0400594static
595BVT (clib_bihash_value) *
596BV (split_and_rehash)
597 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400598 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
599 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700600{
Dave Barach5e6b9582016-12-12 15:37:29 -0500601 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400602 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700603
Dave Barach508498f2018-07-19 12:11:16 -0400604 ASSERT (h->alloc_lock[0]);
605
Dave Barachc3799992016-08-15 11:12:27 -0400606 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400607 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608
Dave Barachba7ddfe2017-05-17 20:20:50 -0400609 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 {
611 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400612
Dave Barach5e6b9582016-12-12 15:37:29 -0500613 /* Entry not in use? Forget it */
614 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
615 continue;
616
617 /* rehash the item onto its new home-page */
618 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
Damjan Marion68e5fd52020-04-23 13:41:47 +0200619 new_hash = extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500620 new_v = &new_values[new_hash];
621
622 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700623 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400624 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500625 /* Empty slot */
626 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400627 {
Dave Barach178cf492018-11-13 16:34:13 -0500628 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
629 sizeof (new_v->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500630 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400631 }
Dave Barachc3799992016-08-15 11:12:27 -0400632 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500633 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400634 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500635 return 0;
636 doublebreak:;
637 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400638
Dave Barach5e6b9582016-12-12 15:37:29 -0500639 return new_values;
640}
641
642static
643BVT (clib_bihash_value) *
644BV (split_and_rehash_linear)
645 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400646 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
647 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500648{
649 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400650 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500651
Dave Barach508498f2018-07-19 12:11:16 -0400652 ASSERT (h->alloc_lock[0]);
653
Dave Barach5e6b9582016-12-12 15:37:29 -0500654 new_values = BV (value_alloc) (h, new_log2_pages);
655 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400656 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500657
658 j = 0;
659 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400660 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500661 {
662 /* Find a free slot in the new linear scan bucket */
663 for (; j < new_length; j++)
664 {
Dave Barach8f544962017-01-18 10:23:22 -0500665 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500666 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
667 goto doublebreak;
668
669 /* New value should never be in use */
670 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
671 {
672 /* Copy the old value and move along */
Dave Barach178cf492018-11-13 16:34:13 -0500673 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
674 sizeof (new_values->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500675 j++;
676 goto doublebreak;
677 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500678 }
Dave Barach8f544962017-01-18 10:23:22 -0500679 /* This should never happen... */
680 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400681 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500682 return 0;
683
Dave Barach5e6b9582016-12-12 15:37:29 -0500684 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700685 }
686 return new_values;
687}
688
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100689static_always_inline int BV (clib_bihash_add_del_inline_with_hash) (
690 BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, u64 hash, int is_add,
691 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *is_stale_arg,
692 void (*overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *overwrite_arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700693{
Dave Barach908a5ea2017-07-14 12:42:21 -0400694 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400695 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500696 int i, limit;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200697 u64 new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400698 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200699 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500700 int mark_bucket_linear;
701 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702
Damjan Marion68e5fd52020-04-23 13:41:47 +0200703 /* *INDENT-OFF* */
704 static const BVT (clib_bihash_bucket) mask = {
705 .linear_search = 1,
706 .log2_pages = -1
707 };
708 /* *INDENT-ON* */
709
710#if BIHASH_LAZY_INSTANTIATE
Dave Barach67d09e02019-08-01 08:15:01 -0400711 /*
712 * Create the table (is_add=1,2), or flunk the request now (is_add=0)
713 * Use the alloc_lock to protect the instantiate operation.
714 */
715 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -0400716 {
717 if (is_add == 0)
718 return (-1);
Dave Barach67d09e02019-08-01 08:15:01 -0400719
720 BV (clib_bihash_alloc_lock) (h);
721 if (h->instantiated == 0)
722 BV (clib_bihash_instantiate) (h);
723 BV (clib_bihash_alloc_unlock) (h);
Dave Barach32dcd3b2019-07-08 12:25:38 -0400724 }
Dave Baracha90ba642020-04-23 16:56:15 -0400725#else
726 /* Debug image: make sure the table has been instantiated */
727 ASSERT (h->instantiated != 0);
Damjan Marion68e5fd52020-04-23 13:41:47 +0200728#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -0400729
Dave Barachb9c8c572023-03-16 13:03:47 -0400730 /*
731 * Debug image: make sure that an item being added doesn't accidentally
732 * look like a free item.
733 */
734 ASSERT ((is_add && BV (clib_bihash_is_free) (add_v)) == 0);
735
Damjan Marion4e149772020-03-27 16:57:28 +0100736 b = BV (clib_bihash_get_bucket) (h, hash);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700737
Dave Barach508498f2018-07-19 12:11:16 -0400738 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700739
740 /* First elt in the bucket? */
Dave Barach16e4a4a2020-04-16 12:00:14 -0400741 if (BIHASH_KVP_AT_BUCKET_LEVEL == 0 && BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700742 {
743 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400744 {
Dave Barach508498f2018-07-19 12:11:16 -0400745 BV (clib_bihash_unlock_bucket) (b);
746 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400747 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700748
Dave Barach508498f2018-07-19 12:11:16 -0400749 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400750 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400751 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400752
Dave Barachc3799992016-08-15 11:12:27 -0400753 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400754 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400755 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500756 tmp_b.refcnt = 1;
Damjan Marion801ec2a2020-04-21 19:42:30 +0200757 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700758
Tom Seidenberg97f8ae92019-03-15 10:15:26 -0400759 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
Dave Barach2ce28d62019-05-03 12:58:01 -0400760 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
761
Dave Barach508498f2018-07-19 12:11:16 -0400762 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700763 }
764
Dave Barach508498f2018-07-19 12:11:16 -0400765 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500766 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400767 v = BV (clib_bihash_get_value) (h, b->offset);
768
Damjan Marion68e5fd52020-04-23 13:41:47 +0200769 if (PREDICT_FALSE (b->as_u64 & mask.as_u64))
770 {
771 if (PREDICT_FALSE (b->linear_search))
772 limit <<= b->log2_pages;
773 else
774 v += extract_bits (hash, h->log2_nbuckets, b->log2_pages);
775 }
Dave Barachc3799992016-08-15 11:12:27 -0400776
Ed Warnickecb9cada2015-12-08 15:45:58 -0700777 if (is_add)
778 {
Dave Barachc3799992016-08-15 11:12:27 -0400779 /*
Dave Barach508498f2018-07-19 12:11:16 -0400780 * Because reader threads are looking at live data,
781 * we have to be extra careful. Readers do NOT hold the
782 * bucket lock. We need to be SLOWER than a search, past the
783 * point where readers CHECK the bucket lock.
784 */
785
786 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700787 * For obvious (in hindsight) reasons, see if we're supposed to
788 * replace an existing key, then look for an empty slot.
789 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500790 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400791 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400792 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
793 continue;
Dave Baracha11bf452019-04-17 17:27:31 -0400794 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400795 {
Dave Barach9e4946b2019-07-08 14:47:44 -0400796 /* Add but do not overwrite? */
797 if (is_add == 2)
798 {
799 BV (clib_bihash_unlock_bucket) (b);
800 return (-2);
801 }
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100802 if (overwrite_cb)
803 overwrite_cb (&(v->kvp[i]), overwrite_arg);
Damjan Marion801ec2a2020-04-21 19:42:30 +0200804 clib_memcpy_fast (&(v->kvp[i].value),
805 &add_v->value, sizeof (add_v->value));
Dave Barach508498f2018-07-19 12:11:16 -0400806 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400807 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400808 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400809 }
810 }
Dave Barach508498f2018-07-19 12:11:16 -0400811 /*
812 * Look for an empty slot. If found, use it
813 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500814 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400815 {
816 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
817 {
Dave Barach508498f2018-07-19 12:11:16 -0400818 /*
819 * Copy the value first, so that if a reader manages
820 * to match the new key, the value will be right...
821 */
Dave Barach178cf492018-11-13 16:34:13 -0500822 clib_memcpy_fast (&(v->kvp[i].value),
823 &add_v->value, sizeof (add_v->value));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200824 CLIB_MEMORY_STORE_BARRIER (); /* Make sure the value has settled */
Dave Barach178cf492018-11-13 16:34:13 -0500825 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
826 sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500827 b->refcnt++;
Dave Barach9466c452018-08-24 17:21:14 -0400828 ASSERT (b->refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400829 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400830 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400831 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400832 }
833 }
Matus Fabian828d27e2018-08-21 03:15:50 -0700834 /* look for stale data to overwrite */
835 if (is_stale_cb)
836 {
837 for (i = 0; i < limit; i++)
838 {
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +0100839 if (is_stale_cb (&(v->kvp[i]), is_stale_arg))
Matus Fabian828d27e2018-08-21 03:15:50 -0700840 {
Dave Barach178cf492018-11-13 16:34:13 -0500841 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Damjan Marion801ec2a2020-04-21 19:42:30 +0200842 CLIB_MEMORY_STORE_BARRIER ();
Matus Fabian828d27e2018-08-21 03:15:50 -0700843 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400844 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Matus Fabian828d27e2018-08-21 03:15:50 -0700845 return (0);
846 }
847 }
848 }
Dave Barach508498f2018-07-19 12:11:16 -0400849 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700850 }
Dave Barach508498f2018-07-19 12:11:16 -0400851 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700852 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500853 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400854 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400855 /* no sense even looking at this one */
856 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
857 continue;
Dave Barach508498f2018-07-19 12:11:16 -0400858 /* Found the key? Kill it... */
Dave Baracha11bf452019-04-17 17:27:31 -0400859 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400860 {
Dave Barachb9c8c572023-03-16 13:03:47 -0400861 BV (clib_bihash_mark_free) (&(v->kvp[i]));
Dave Barach508498f2018-07-19 12:11:16 -0400862 /* Is the bucket empty? */
863 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500864 {
Dave Barach508498f2018-07-19 12:11:16 -0400865 b->refcnt--;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400866 /* Switch back to the bucket-level kvp array? */
867 if (BIHASH_KVP_AT_BUCKET_LEVEL && b->refcnt == 1
868 && b->log2_pages > 0)
869 {
870 tmp_b.as_u64 = b->as_u64;
871 b->offset = BV (clib_bihash_get_offset)
872 (h, (void *) (b + 1));
873 b->linear_search = 0;
874 b->log2_pages = 0;
875 /* Clean up the bucket-level kvp array */
Dave Barachb9c8c572023-03-16 13:03:47 -0400876 BVT (clib_bihash_kv) *v = (void *) (b + 1);
877 int j;
878 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
879 {
880 BV (clib_bihash_mark_free) (v);
881 v++;
882 }
Damjan Marion801ec2a2020-04-21 19:42:30 +0200883 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach16e4a4a2020-04-16 12:00:14 -0400884 BV (clib_bihash_unlock_bucket) (b);
885 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
886 goto free_backing_store;
887 }
888
Damjan Marion801ec2a2020-04-21 19:42:30 +0200889 CLIB_MEMORY_STORE_BARRIER ();
Dave Barach508498f2018-07-19 12:11:16 -0400890 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400891 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400892 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500893 }
Dave Barach508498f2018-07-19 12:11:16 -0400894 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500895 {
Dave Barach508498f2018-07-19 12:11:16 -0400896 /* Save old bucket value, need log2_pages to free it */
897 tmp_b.as_u64 = b->as_u64;
Dave Barach508498f2018-07-19 12:11:16 -0400898
899 /* Kill and unlock the bucket */
900 b->as_u64 = 0;
901
Dave Barach16e4a4a2020-04-16 12:00:14 -0400902 free_backing_store:
Dave Barach508498f2018-07-19 12:11:16 -0400903 /* And free the backing storage */
904 BV (clib_bihash_alloc_lock) (h);
905 /* Note: v currently points into the middle of the bucket */
906 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
907 BV (value_free) (h, v, tmp_b.log2_pages);
908 BV (clib_bihash_alloc_unlock) (h);
Dave Barach2ce28d62019-05-03 12:58:01 -0400909 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
910 1);
Dave Barach508498f2018-07-19 12:11:16 -0400911 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500912 }
Dave Barachc3799992016-08-15 11:12:27 -0400913 }
914 }
Dave Barach508498f2018-07-19 12:11:16 -0400915 /* Not found... */
916 BV (clib_bihash_unlock_bucket) (b);
917 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700918 }
919
Dave Barach508498f2018-07-19 12:11:16 -0400920 /* Move readers to a (locked) temp copy of the bucket */
921 BV (clib_bihash_alloc_lock) (h);
922 BV (make_working_copy) (h, b);
923
924 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
925
Dave Barachba7ddfe2017-05-17 20:20:50 -0400926 old_log2_pages = h->saved_bucket.log2_pages;
927 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500928 mark_bucket_linear = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400929 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
930 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700931
Damjan Marionf55f9b82017-05-10 21:06:28 +0200932 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500933 resplit_once = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400934 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500935
Dave Barachba7ddfe2017-05-17 20:20:50 -0400936 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
937 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 if (new_v == 0)
939 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500940 try_resplit:
941 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500943 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400944 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
945 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500946 if (new_v == 0)
947 {
948 mark_linear:
949 new_log2_pages--;
950 /* pinned collisions, use linear search */
951 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400952 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
953 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500954 mark_bucket_linear = 1;
Dave Barach2ce28d62019-05-03 12:58:01 -0400955 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500956 }
Dave Barach2ce28d62019-05-03 12:58:01 -0400957 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
958 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
959 old_log2_pages + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700960 }
961
962 /* Try to add the new entry */
963 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400964 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500965 limit = BIHASH_KVP_PER_PAGE;
966 if (mark_bucket_linear)
967 limit <<= new_log2_pages;
Damjan Marion68e5fd52020-04-23 13:41:47 +0200968 else
969 new_v += extract_bits (new_hash, h->log2_nbuckets, new_log2_pages);
Dave Barachc3799992016-08-15 11:12:27 -0400970
Dave Barach5e6b9582016-12-12 15:37:29 -0500971 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 {
Dave Barachc3799992016-08-15 11:12:27 -0400973 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
974 {
Dave Barach178cf492018-11-13 16:34:13 -0500975 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
Dave Barachc3799992016-08-15 11:12:27 -0400976 goto expand_ok;
977 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700978 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400979
Ed Warnickecb9cada2015-12-08 15:45:58 -0700980 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400981 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500982 /*
983 * If we've already doubled the size of the bucket once,
984 * fall back to linear search now.
985 */
986 if (resplit_once)
987 goto mark_linear;
988 else
989 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700990
Dave Barachc3799992016-08-15 11:12:27 -0400991expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500992 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400993 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500994 tmp_b.linear_search = mark_bucket_linear;
Dave Barach16e4a4a2020-04-16 12:00:14 -0400995#if BIHASH_KVP_AT_BUCKET_LEVEL
996 /* Compensate for permanent refcount bump at the bucket level */
997 if (new_log2_pages > 0)
998#endif
999 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach9466c452018-08-24 17:21:14 -04001000 ASSERT (tmp_b.refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -04001001 tmp_b.lock = 0;
Damjan Marion801ec2a2020-04-21 19:42:30 +02001002 CLIB_MEMORY_STORE_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -07001003 b->as_u64 = tmp_b.as_u64;
Dave Barach16e4a4a2020-04-16 12:00:14 -04001004
1005#if BIHASH_KVP_AT_BUCKET_LEVEL
1006 if (h->saved_bucket.log2_pages > 0)
1007 {
1008#endif
1009
1010 /* free the old bucket, except at the bucket level if so configured */
1011 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
1012 BV (value_free) (h, v, h->saved_bucket.log2_pages);
1013
1014#if BIHASH_KVP_AT_BUCKET_LEVEL
1015 }
1016#endif
1017
1018
Dave Barach508498f2018-07-19 12:11:16 -04001019 BV (clib_bihash_alloc_unlock) (h);
1020 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021}
1022
Damjan Marion801ec2a2020-04-21 19:42:30 +02001023static_always_inline int BV (clib_bihash_add_del_inline)
1024 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
1025 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
1026{
1027 u64 hash = BV (clib_bihash_hash) (add_v);
1028 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add,
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +01001029 is_stale_cb, arg, 0, 0);
1030}
1031
1032int BV (clib_bihash_add_del_with_hash) (BVT (clib_bihash) * h,
1033 BVT (clib_bihash_kv) * add_v, u64 hash,
1034 int is_add)
1035{
1036 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, is_add, 0,
1037 0, 0, 0);
Damjan Marion801ec2a2020-04-21 19:42:30 +02001038}
1039
Matus Fabian828d27e2018-08-21 03:15:50 -07001040int BV (clib_bihash_add_del)
1041 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
1042{
1043 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
1044}
1045
1046int BV (clib_bihash_add_or_overwrite_stale)
1047 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
1048 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
1049{
1050 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
1051}
1052
Nathan Skrzypczak17ecd852021-12-02 14:40:06 +01001053int BV (clib_bihash_add_with_overwrite_cb) (
1054 BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
1055 void (overwrite_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
1056{
1057 u64 hash = BV (clib_bihash_hash) (add_v);
1058 return BV (clib_bihash_add_del_inline_with_hash) (h, add_v, hash, 1, 0, 0,
1059 overwrite_cb, arg);
1060}
1061
Dave Barachc3799992016-08-15 11:12:27 -04001062int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -04001063 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -04001064 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001065{
Damjan Marion68e5fd52020-04-23 13:41:47 +02001066 return BV (clib_bihash_search_inline_2) (h, search_key, valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001067}
1068
Dave Barachc3799992016-08-15 11:12:27 -04001069u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070{
Dave Barachc3799992016-08-15 11:12:27 -04001071 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001072 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -04001073 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001074 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001075 int i, j, k;
1076 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -05001077 u64 active_buckets = 0;
1078 u64 linear_buckets = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001079
Damjan Marionf2b4a372020-09-30 14:15:24 +02001080 s = format (s, "Hash table '%s'\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -04001081
Dave Barach16e4a4a2020-04-16 12:00:14 -04001082#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001083 if (PREDICT_FALSE (h->instantiated == 0))
Damjan Marionf2b4a372020-09-30 14:15:24 +02001084 return format (s, " empty, uninitialized");
Dave Barach16e4a4a2020-04-16 12:00:14 -04001085#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001086
Ed Warnickecb9cada2015-12-08 15:45:58 -07001087 for (i = 0; i < h->nbuckets; i++)
1088 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001089 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001090 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001091 {
1092 if (verbose > 1)
1093 s = format (s, "[%d]: empty\n", i);
1094 continue;
1095 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001096
Dave Barache7d212f2018-02-07 13:14:06 -05001097 active_buckets++;
1098
1099 if (b->linear_search)
1100 linear_buckets++;
1101
Ed Warnickecb9cada2015-12-08 15:45:58 -07001102 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -04001103 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001104 s = format
1105 (s, "[%d]: heap offset %lld, len %d, refcnt %d, linear %d\n", i,
1106 b->offset, (1 << b->log2_pages), b->refcnt, b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -04001107 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001108
Dave Barachc3799992016-08-15 11:12:27 -04001109 v = BV (clib_bihash_get_value) (h, b->offset);
1110 for (j = 0; j < (1 << b->log2_pages); j++)
1111 {
1112 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1113 {
1114 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1115 {
1116 if (verbose > 1)
1117 s = format (s, " %d: empty\n",
1118 j * BIHASH_KVP_PER_PAGE + k);
1119 continue;
1120 }
1121 if (verbose)
1122 {
Damjan Marionf2b4a372020-09-30 14:15:24 +02001123 if (h->kvp_fmt_fn)
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001124 {
1125 s = format (s, " %d: %U\n",
1126 j * BIHASH_KVP_PER_PAGE + k,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001127 h->kvp_fmt_fn, &(v->kvp[k]), verbose);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -08001128 }
1129 else
1130 {
1131 s = format (s, " %d: %U\n",
1132 j * BIHASH_KVP_PER_PAGE + k,
1133 BV (format_bihash_kvp), &(v->kvp[k]));
1134 }
Dave Barachc3799992016-08-15 11:12:27 -04001135 }
1136 active_elements++;
1137 }
1138 v++;
1139 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001140 }
1141
Dave Barache7d212f2018-02-07 13:14:06 -05001142 s = format (s, " %lld active elements %lld active buckets\n",
1143 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -05001145
1146 for (i = 0; i < vec_len (h->freelists); i++)
1147 {
1148 u32 nfree = 0;
1149 BVT (clib_bihash_value) * free_elt;
Dave Barachffb14b92018-09-11 17:20:23 -04001150 u64 free_elt_as_u64 = h->freelists[i];
Dave Barache7d212f2018-02-07 13:14:06 -05001151
Dave Barachffb14b92018-09-11 17:20:23 -04001152 while (free_elt_as_u64)
Dave Barache7d212f2018-02-07 13:14:06 -05001153 {
Dave Barachffb14b92018-09-11 17:20:23 -04001154 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
Dave Barache7d212f2018-02-07 13:14:06 -05001155 nfree++;
Dave Barachffb14b92018-09-11 17:20:23 -04001156 free_elt_as_u64 = free_elt->next_free_as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -05001157 }
1158
Dave Barach9466c452018-08-24 17:21:14 -04001159 if (nfree || verbose)
1160 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
Dave Barache7d212f2018-02-07 13:14:06 -05001161 }
1162
1163 s = format (s, " %lld linear search buckets\n", linear_buckets);
Damjan Marion2454de22020-09-26 19:32:34 +02001164 if (BIHASH_USE_HEAP)
1165 {
1166 BVT (clib_bihash_alloc_chunk) * c = h->chunks;
1167 uword bytes_left = 0, total_size = 0, n_chunks = 0;
1168
1169 while (c)
1170 {
1171 bytes_left += c->bytes_left;
1172 total_size += c->size;
1173 n_chunks += 1;
1174 c = c->next;
1175 }
1176 s = format (s,
Damjan Marionf2b4a372020-09-30 14:15:24 +02001177 " heap: %u chunk(s) allocated\n"
1178 " bytes: used %U, scrap %U\n", n_chunks,
Damjan Marion2454de22020-09-26 19:32:34 +02001179 format_memory_size, total_size,
1180 format_memory_size, bytes_left);
1181 }
1182 else
1183 {
1184 u64 used_bytes = alloc_arena_next (h);
1185 s = format (s,
1186 " arena: base %llx, next %llx\n"
1187 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
1188 alloc_arena (h), alloc_arena_next (h),
1189 used_bytes, used_bytes >> 20,
1190 alloc_arena_size (h), alloc_arena_size (h) >> 20);
1191 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001192 return s;
1193}
1194
Dave Barachc3799992016-08-15 11:12:27 -04001195void BV (clib_bihash_foreach_key_value_pair)
Neale Rannsf50bac12019-12-06 05:53:17 +00001196 (BVT (clib_bihash) * h,
1197 BV (clib_bihash_foreach_key_value_pair_cb) cb, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001198{
1199 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -04001200 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -04001201 BVT (clib_bihash_value) * v;
Dave Barachc3799992016-08-15 11:12:27 -04001202
Dave Barach16e4a4a2020-04-16 12:00:14 -04001203
1204#if BIHASH_LAZY_INSTANTIATE
Nathan Skrzypczak42b29ba2020-08-17 14:14:56 +02001205 if (PREDICT_FALSE (h->instantiated == 0))
Dave Barach32dcd3b2019-07-08 12:25:38 -04001206 return;
Dave Barach16e4a4a2020-04-16 12:00:14 -04001207#endif
Dave Barach32dcd3b2019-07-08 12:25:38 -04001208
Ed Warnickecb9cada2015-12-08 15:45:58 -07001209 for (i = 0; i < h->nbuckets; i++)
1210 {
Dave Barach16e4a4a2020-04-16 12:00:14 -04001211 b = BV (clib_bihash_get_bucket) (h, i);
Damjan Marion882fcfe2018-07-17 23:01:49 +02001212 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -04001213 continue;
1214
1215 v = BV (clib_bihash_get_value) (h, b->offset);
1216 for (j = 0; j < (1 << b->log2_pages); j++)
1217 {
1218 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
1219 {
1220 if (BV (clib_bihash_is_free) (&v->kvp[k]))
1221 continue;
1222
Neale Rannsf50bac12019-12-06 05:53:17 +00001223 if (BIHASH_WALK_STOP == cb (&v->kvp[k], arg))
1224 return;
Dave Barachca45ee72018-08-06 08:43:47 -04001225 /*
1226 * In case the callback deletes the last entry in the bucket...
1227 */
1228 if (BV (clib_bihash_bucket_is_empty) (b))
1229 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -04001230 }
1231 v++;
1232 }
Dave Barachca45ee72018-08-06 08:43:47 -04001233 doublebreak:
1234 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001235 }
1236}
Dave Barachdd3a57f2016-07-27 16:58:51 -04001237
Chris Luke16bcf7d2016-09-01 14:31:46 -04001238/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -04001239
1240/*
1241 * fd.io coding-style-patch-verification: ON
1242 *
1243 * Local Variables:
1244 * eval: (c-set-style "gnu")
1245 * End:
1246 */