blob: a10dee025bf11cb5b3d593fc6d7988a116144904 [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 Barach97f5af02018-02-22 09:48:45 -050018static inline void *BV (alloc_aligned) (BVT (clib_bihash) * h, uword nbytes)
19{
20 uword rv;
21
22 /* Round to an even number of cache lines */
23 nbytes += CLIB_CACHE_LINE_BYTES - 1;
24 nbytes &= ~(CLIB_CACHE_LINE_BYTES - 1);
25
Dave Barach9466c452018-08-24 17:21:14 -040026 rv = alloc_arena_next (h);
27 alloc_arena_next (h) += nbytes;
Dave Barach97f5af02018-02-22 09:48:45 -050028
Andrew Yourtchenkoa7132542018-09-19 15:50:55 +020029 if (rv >= alloc_arena_size (h))
Dave Barach97f5af02018-02-22 09:48:45 -050030 os_out_of_memory ();
31
Dave Barachffb14b92018-09-11 17:20:23 -040032 return (void *) (uword) (rv + alloc_arena (h));
Dave Barach97f5af02018-02-22 09:48:45 -050033}
34
35
Dave Barachc3799992016-08-15 11:12:27 -040036void BV (clib_bihash_init)
37 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -070038{
Dave Barach97f5af02018-02-22 09:48:45 -050039 uword bucket_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -070040
41 nbuckets = 1 << (max_log2 (nbuckets));
42
Dave Barachc3799992016-08-15 11:12:27 -040043 h->name = (u8 *) name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070044 h->nbuckets = nbuckets;
45 h->log2_nbuckets = max_log2 (nbuckets);
46
Dave Barach508498f2018-07-19 12:11:16 -040047 /*
48 * Make sure the requested size is rational. The max table
49 * size without playing the alignment card is 64 Gbytes.
50 * If someone starts complaining that's not enough, we can shift
51 * the offset by CLIB_LOG2_CACHE_LINE_BYTES...
52 */
53 ASSERT (memory_size < (1ULL << BIHASH_BUCKET_OFFSET_BITS));
54
Dave Barach9466c452018-08-24 17:21:14 -040055 alloc_arena (h) = (uword) clib_mem_vm_alloc (memory_size);
Dave Barachffb14b92018-09-11 17:20:23 -040056 alloc_arena_next (h) = 0;
Dave Barach9466c452018-08-24 17:21:14 -040057 alloc_arena_size (h) = memory_size;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
Dave Barach97f5af02018-02-22 09:48:45 -050059 bucket_size = nbuckets * sizeof (h->buckets[0]);
60 h->buckets = BV (alloc_aligned) (h, bucket_size);
61
Dave Barach508498f2018-07-19 12:11:16 -040062 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
63 h->alloc_lock[0] = 0;
Dave Barach908a5ea2017-07-14 12:42:21 -040064
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -080065 h->fmt_fn = NULL;
66}
67
Dave Barach9466c452018-08-24 17:21:14 -040068#if BIHASH_32_64_SVM
69#if !defined (MFD_ALLOW_SEALING)
70#define MFD_ALLOW_SEALING 0x0002U
71#endif
72
73void BV (clib_bihash_master_init_svm)
Dave Barachffb14b92018-09-11 17:20:23 -040074 (BVT (clib_bihash) * h, char *name, u32 nbuckets, u64 memory_size)
Dave Barach9466c452018-08-24 17:21:14 -040075{
76 uword bucket_size;
77 u8 *mmap_addr;
78 vec_header_t *freelist_vh;
79 int fd;
80
Dave Barachffb14b92018-09-11 17:20:23 -040081 ASSERT (memory_size < (1ULL << 32));
Dave Barach9466c452018-08-24 17:21:14 -040082 /* Set up for memfd sharing */
83 if ((fd = memfd_create (name, MFD_ALLOW_SEALING)) == -1)
84 {
85 clib_unix_warning ("memfd_create");
86 return;
87 }
88
89 if (ftruncate (fd, memory_size) < 0)
90 {
91 clib_unix_warning ("ftruncate");
92 return;
93 }
94
95 /* Not mission-critical, complain and continue */
96 if ((fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
97 clib_unix_warning ("fcntl (F_ADD_SEALS)");
98
Dave Barachffb14b92018-09-11 17:20:23 -040099 mmap_addr = mmap (0, memory_size,
100 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400101
102 if (mmap_addr == MAP_FAILED)
103 {
104 clib_unix_warning ("mmap failed");
105 ASSERT (0);
106 }
107
108 h->sh = (void *) mmap_addr;
109 h->memfd = fd;
110 nbuckets = 1 << (max_log2 (nbuckets));
111
112 h->name = (u8 *) name;
113 h->sh->nbuckets = h->nbuckets = nbuckets;
114 h->log2_nbuckets = max_log2 (nbuckets);
115
116 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400117 alloc_arena_next (h) = CLIB_CACHE_LINE_BYTES;
Dave Barach9466c452018-08-24 17:21:14 -0400118 alloc_arena_size (h) = memory_size;
119
120 bucket_size = nbuckets * sizeof (h->buckets[0]);
121 h->buckets = BV (alloc_aligned) (h, bucket_size);
Dave Barachffb14b92018-09-11 17:20:23 -0400122 h->sh->buckets_as_u64 = (u64) BV (clib_bihash_get_offset) (h, h->buckets);
Dave Barach9466c452018-08-24 17:21:14 -0400123
124 h->alloc_lock = BV (alloc_aligned) (h, CLIB_CACHE_LINE_BYTES);
125 h->alloc_lock[0] = 0;
126
Dave Barachffb14b92018-09-11 17:20:23 -0400127 h->sh->alloc_lock_as_u64 =
128 (u64) BV (clib_bihash_get_offset) (h, (void *) h->alloc_lock);
129 freelist_vh =
130 BV (alloc_aligned) (h,
131 sizeof (vec_header_t) +
132 BIHASH_FREELIST_LENGTH * sizeof (u64));
Dave Barach9466c452018-08-24 17:21:14 -0400133 freelist_vh->len = BIHASH_FREELIST_LENGTH;
134 freelist_vh->dlmalloc_header_offset = 0xDEADBEEF;
Dave Barachffb14b92018-09-11 17:20:23 -0400135 h->sh->freelists_as_u64 =
136 (u64) BV (clib_bihash_get_offset) (h, freelist_vh->vector_data);
137 h->freelists = (void *) (freelist_vh->vector_data);
Dave Barach9466c452018-08-24 17:21:14 -0400138
139 h->fmt_fn = NULL;
140}
141
142void BV (clib_bihash_slave_init_svm)
143 (BVT (clib_bihash) * h, char *name, int fd)
144{
145 u8 *mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400146 u64 memory_size;
Dave Barach9466c452018-08-24 17:21:14 -0400147 BVT (clib_bihash_shared_header) * sh;
148
Dave Barachffb14b92018-09-11 17:20:23 -0400149 /* Trial mapping, to learn the segment size */
Dave Barach9466c452018-08-24 17:21:14 -0400150 mmap_addr = mmap (0, 4096, PROT_READ, MAP_SHARED, fd, 0 /* offset */ );
151 if (mmap_addr == MAP_FAILED)
152 {
153 clib_unix_warning ("trial mmap failed");
154 ASSERT (0);
155 }
156
157 sh = (BVT (clib_bihash_shared_header) *) mmap_addr;
158
Dave Barach9466c452018-08-24 17:21:14 -0400159 memory_size = sh->alloc_arena_size;
160
161 munmap (mmap_addr, 4096);
162
Dave Barachffb14b92018-09-11 17:20:23 -0400163 /* Actual mapping, at the required size */
164 mmap_addr = mmap (0, memory_size,
165 PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 /* offset */ );
Dave Barach9466c452018-08-24 17:21:14 -0400166
167 if (mmap_addr == MAP_FAILED)
168 {
169 clib_unix_warning ("mmap failed");
170 ASSERT (0);
171 }
172
173 (void) close (fd);
174
175 h->sh = (void *) mmap_addr;
Dave Barachffb14b92018-09-11 17:20:23 -0400176 alloc_arena (h) = (u64) (uword) mmap_addr;
Dave Barach9466c452018-08-24 17:21:14 -0400177 h->memfd = -1;
178
179 h->name = (u8 *) name;
Dave Barachffb14b92018-09-11 17:20:23 -0400180 h->buckets = BV (clib_bihash_get_value) (h, h->sh->buckets_as_u64);
Dave Barach9466c452018-08-24 17:21:14 -0400181 h->nbuckets = h->sh->nbuckets;
182 h->log2_nbuckets = max_log2 (h->nbuckets);
183
Dave Barachffb14b92018-09-11 17:20:23 -0400184 h->alloc_lock = BV (clib_bihash_get_value) (h, h->sh->alloc_lock_as_u64);
185 h->freelists = BV (clib_bihash_get_value) (h, h->sh->freelists_as_u64);
Dave Barach9466c452018-08-24 17:21:14 -0400186 h->fmt_fn = NULL;
187}
188#endif /* BIHASH_32_64_SVM */
189
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800190void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
191 format_function_t * fmt_fn)
192{
193 h->fmt_fn = fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700194}
195
Dave Barachc3799992016-08-15 11:12:27 -0400196void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700197{
Dave Barach97f5af02018-02-22 09:48:45 -0500198 vec_free (h->working_copies);
Vijayabhaskar Katamreddy72739a62019-05-07 13:27:32 -0700199 vec_free (h->working_copy_lengths);
Dave Barach9466c452018-08-24 17:21:14 -0400200#if BIHASH_32_64_SVM == 0
Dave Barach97f5af02018-02-22 09:48:45 -0500201 vec_free (h->freelists);
Dave Barach9466c452018-08-24 17:21:14 -0400202#else
203 if (h->memfd > 0)
204 (void) close (h->memfd);
205#endif
206 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)), alloc_arena_size (h));
Dave Barachb7b92992018-10-17 10:38:51 -0400207 clib_memset (h, 0, sizeof (*h));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700208}
209
Dave Barachc3799992016-08-15 11:12:27 -0400210static
211BVT (clib_bihash_value) *
212BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700213{
Dave Barachc3799992016-08-15 11:12:27 -0400214 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700215
Dave Barach508498f2018-07-19 12:11:16 -0400216 ASSERT (h->alloc_lock[0]);
Dave Barach9466c452018-08-24 17:21:14 -0400217
218#if BIHASH_32_64_SVM
219 ASSERT (log2_pages < vec_len (h->freelists));
220#endif
221
Dave Barachc3799992016-08-15 11:12:27 -0400222 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700223 {
Dave Barach97f5af02018-02-22 09:48:45 -0500224 vec_validate_init_empty (h->freelists, log2_pages, 0);
225 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -0400226 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700227 }
Dave Barachffb14b92018-09-11 17:20:23 -0400228 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
Dave Barach9466c452018-08-24 17:21:14 -0400229 h->freelists[log2_pages] = rv->next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700230
Dave Barachc3799992016-08-15 11:12:27 -0400231initialize:
232 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -0400233 /*
234 * Latest gcc complains that the length arg is zero
235 * if we replace (1<<log2_pages) with vec_len(rv).
236 * No clue.
237 */
Dave Barachb7b92992018-10-17 10:38:51 -0400238 clib_memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
Dave Barachc3799992016-08-15 11:12:27 -0400239 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700240}
241
242static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400243BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
244 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700245{
Dave Barach508498f2018-07-19 12:11:16 -0400246 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247
Dave Barachc3799992016-08-15 11:12:27 -0400248 ASSERT (vec_len (h->freelists) > log2_pages);
249
Dave Barach508498f2018-07-19 12:11:16 -0400250 if (CLIB_DEBUG > 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400251 clib_memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
Dave Barach508498f2018-07-19 12:11:16 -0400252
Dave Barach9466c452018-08-24 17:21:14 -0400253 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
Dave Barachffb14b92018-09-11 17:20:23 -0400254 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255}
256
257static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400258BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259{
Dave Barachc3799992016-08-15 11:12:27 -0400260 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400261 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400262 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200263 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400264 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700265
Dave Barach508498f2018-07-19 12:11:16 -0400266 ASSERT (h->alloc_lock[0]);
267
Damjan Marionf55f9b82017-05-10 21:06:28 +0200268 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700269 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200270 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700271 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700272 }
273
Dave Barachc3799992016-08-15 11:12:27 -0400274 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700275 * working_copies are per-cpu so that near-simultaneous
276 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400277 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200279 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400280 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700281
282 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283
Dave Barachba7ddfe2017-05-17 20:20:50 -0400284 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285 {
Dave Barach97f5af02018-02-22 09:48:45 -0500286 /*
287 * It's not worth the bookkeeping to free working copies
288 * if (working_copy)
289 * clib_mem_free (working_copy);
290 */
291 working_copy = BV (alloc_aligned)
292 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400293 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200294 h->working_copies[thread_index] = working_copy;
Dave Barach2ce28d62019-05-03 12:58:01 -0400295
296 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_working_copy_lost,
297 1ULL << b->log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700298 }
299
Dave Barachc3799992016-08-15 11:12:27 -0400300 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Dave Barach178cf492018-11-13 16:34:13 -0500302 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400304 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
305 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700306 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200307 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308}
309
Dave Barachc3799992016-08-15 11:12:27 -0400310static
311BVT (clib_bihash_value) *
312BV (split_and_rehash)
313 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400314 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
315 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700316{
Dave Barach5e6b9582016-12-12 15:37:29 -0500317 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400318 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700319
Dave Barach508498f2018-07-19 12:11:16 -0400320 ASSERT (h->alloc_lock[0]);
321
Dave Barachc3799992016-08-15 11:12:27 -0400322 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400323 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700324
Dave Barachba7ddfe2017-05-17 20:20:50 -0400325 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700326 {
327 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400328
Dave Barach5e6b9582016-12-12 15:37:29 -0500329 /* Entry not in use? Forget it */
330 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
331 continue;
332
333 /* rehash the item onto its new home-page */
334 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
335 new_hash >>= h->log2_nbuckets;
336 new_hash &= (1 << new_log2_pages) - 1;
337 new_v = &new_values[new_hash];
338
339 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700340 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400341 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500342 /* Empty slot */
343 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400344 {
Dave Barach178cf492018-11-13 16:34:13 -0500345 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
346 sizeof (new_v->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500347 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400348 }
Dave Barachc3799992016-08-15 11:12:27 -0400349 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500350 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400351 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500352 return 0;
353 doublebreak:;
354 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400355
Dave Barach5e6b9582016-12-12 15:37:29 -0500356 return new_values;
357}
358
359static
360BVT (clib_bihash_value) *
361BV (split_and_rehash_linear)
362 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400363 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
364 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500365{
366 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400367 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500368
Dave Barach508498f2018-07-19 12:11:16 -0400369 ASSERT (h->alloc_lock[0]);
370
Dave Barach5e6b9582016-12-12 15:37:29 -0500371 new_values = BV (value_alloc) (h, new_log2_pages);
372 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400373 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500374
375 j = 0;
376 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400377 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500378 {
379 /* Find a free slot in the new linear scan bucket */
380 for (; j < new_length; j++)
381 {
Dave Barach8f544962017-01-18 10:23:22 -0500382 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500383 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
384 goto doublebreak;
385
386 /* New value should never be in use */
387 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
388 {
389 /* Copy the old value and move along */
Dave Barach178cf492018-11-13 16:34:13 -0500390 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
391 sizeof (new_values->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500392 j++;
393 goto doublebreak;
394 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500395 }
Dave Barach8f544962017-01-18 10:23:22 -0500396 /* This should never happen... */
397 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400398 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500399 return 0;
400
Dave Barach5e6b9582016-12-12 15:37:29 -0500401 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 }
403 return new_values;
404}
405
Matus Fabian828d27e2018-08-21 03:15:50 -0700406static inline int BV (clib_bihash_add_del_inline)
407 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
408 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700409{
410 u32 bucket_index;
Dave Barach908a5ea2017-07-14 12:42:21 -0400411 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400412 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500413 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700414 u64 hash, new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400415 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200416 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500417 int mark_bucket_linear;
418 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419
Dave Barachc3799992016-08-15 11:12:27 -0400420 hash = BV (clib_bihash_hash) (add_v);
421
422 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 b = &h->buckets[bucket_index];
424
425 hash >>= h->log2_nbuckets;
426
Dave Barach508498f2018-07-19 12:11:16 -0400427 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428
429 /* First elt in the bucket? */
Damjan Marion882fcfe2018-07-17 23:01:49 +0200430 if (BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 {
432 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400433 {
Dave Barach508498f2018-07-19 12:11:16 -0400434 BV (clib_bihash_unlock_bucket) (b);
435 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400436 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Dave Barach508498f2018-07-19 12:11:16 -0400438 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400439 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400440 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400441
Dave Barachc3799992016-08-15 11:12:27 -0400442 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400443 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400444 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500445 tmp_b.refcnt = 1;
Dave Barach508498f2018-07-19 12:11:16 -0400446 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Tom Seidenberg97f8ae92019-03-15 10:15:26 -0400448 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
Dave Barach2ce28d62019-05-03 12:58:01 -0400449 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_alloc_add, 1);
450
Dave Barach508498f2018-07-19 12:11:16 -0400451 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700452 }
453
Dave Barach508498f2018-07-19 12:11:16 -0400454 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500455 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400456 v = BV (clib_bihash_get_value) (h, b->offset);
457
Dave Barach5e6b9582016-12-12 15:37:29 -0500458 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
459 if (b->linear_search)
460 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400461
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 if (is_add)
463 {
Dave Barachc3799992016-08-15 11:12:27 -0400464 /*
Dave Barach508498f2018-07-19 12:11:16 -0400465 * Because reader threads are looking at live data,
466 * we have to be extra careful. Readers do NOT hold the
467 * bucket lock. We need to be SLOWER than a search, past the
468 * point where readers CHECK the bucket lock.
469 */
470
471 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700472 * For obvious (in hindsight) reasons, see if we're supposed to
473 * replace an existing key, then look for an empty slot.
474 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500475 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400476 {
Dave Baracha11bf452019-04-17 17:27:31 -0400477 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400478 {
Dave Barach508498f2018-07-19 12:11:16 -0400479 CLIB_MEMORY_BARRIER (); /* Add a delay */
Dave Barach178cf492018-11-13 16:34:13 -0500480 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Dave Barach508498f2018-07-19 12:11:16 -0400481 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400482 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400483 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400484 }
485 }
Dave Barach508498f2018-07-19 12:11:16 -0400486 /*
487 * Look for an empty slot. If found, use it
488 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500489 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400490 {
491 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
492 {
Dave Barach508498f2018-07-19 12:11:16 -0400493 /*
494 * Copy the value first, so that if a reader manages
495 * to match the new key, the value will be right...
496 */
Dave Barach178cf492018-11-13 16:34:13 -0500497 clib_memcpy_fast (&(v->kvp[i].value),
498 &add_v->value, sizeof (add_v->value));
Dave Barach508498f2018-07-19 12:11:16 -0400499 CLIB_MEMORY_BARRIER (); /* Make sure the value has settled */
Dave Barach178cf492018-11-13 16:34:13 -0500500 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
501 sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500502 b->refcnt++;
Dave Barach9466c452018-08-24 17:21:14 -0400503 ASSERT (b->refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400504 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400505 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_add, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400506 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400507 }
508 }
Matus Fabian828d27e2018-08-21 03:15:50 -0700509 /* look for stale data to overwrite */
510 if (is_stale_cb)
511 {
512 for (i = 0; i < limit; i++)
513 {
514 if (is_stale_cb (&(v->kvp[i]), arg))
515 {
516 CLIB_MEMORY_BARRIER ();
Dave Barach178cf492018-11-13 16:34:13 -0500517 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Matus Fabian828d27e2018-08-21 03:15:50 -0700518 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400519 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_replace, 1);
Matus Fabian828d27e2018-08-21 03:15:50 -0700520 return (0);
521 }
522 }
523 }
Dave Barach508498f2018-07-19 12:11:16 -0400524 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 }
Dave Barach508498f2018-07-19 12:11:16 -0400526 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500528 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400529 {
Dave Barach508498f2018-07-19 12:11:16 -0400530 /* Found the key? Kill it... */
Dave Baracha11bf452019-04-17 17:27:31 -0400531 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400532 {
Dave Barachb7b92992018-10-17 10:38:51 -0400533 clib_memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
Dave Barach508498f2018-07-19 12:11:16 -0400534 /* Is the bucket empty? */
535 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500536 {
Dave Barach508498f2018-07-19 12:11:16 -0400537 b->refcnt--;
538 BV (clib_bihash_unlock_bucket) (b);
Dave Barach2ce28d62019-05-03 12:58:01 -0400539 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del, 1);
Dave Barach508498f2018-07-19 12:11:16 -0400540 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500541 }
Dave Barach508498f2018-07-19 12:11:16 -0400542 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500543 {
Dave Barach508498f2018-07-19 12:11:16 -0400544 /* Save old bucket value, need log2_pages to free it */
545 tmp_b.as_u64 = b->as_u64;
546 CLIB_MEMORY_BARRIER ();
547
548 /* Kill and unlock the bucket */
549 b->as_u64 = 0;
550
551 /* And free the backing storage */
552 BV (clib_bihash_alloc_lock) (h);
553 /* Note: v currently points into the middle of the bucket */
554 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
555 BV (value_free) (h, v, tmp_b.log2_pages);
556 BV (clib_bihash_alloc_unlock) (h);
Dave Barach2ce28d62019-05-03 12:58:01 -0400557 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_del_free,
558 1);
Dave Barach508498f2018-07-19 12:11:16 -0400559 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500560 }
Dave Barachc3799992016-08-15 11:12:27 -0400561 }
562 }
Dave Barach508498f2018-07-19 12:11:16 -0400563 /* Not found... */
564 BV (clib_bihash_unlock_bucket) (b);
565 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700566 }
567
Dave Barach508498f2018-07-19 12:11:16 -0400568 /* Move readers to a (locked) temp copy of the bucket */
569 BV (clib_bihash_alloc_lock) (h);
570 BV (make_working_copy) (h, b);
571
572 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
573
Dave Barachba7ddfe2017-05-17 20:20:50 -0400574 old_log2_pages = h->saved_bucket.log2_pages;
575 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500576 mark_bucket_linear = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400577 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_split_add, 1);
578 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Damjan Marionf55f9b82017-05-10 21:06:28 +0200580 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500581 resplit_once = 0;
Dave Barach2ce28d62019-05-03 12:58:01 -0400582 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500583
Dave Barachba7ddfe2017-05-17 20:20:50 -0400584 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
585 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586 if (new_v == 0)
587 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500588 try_resplit:
589 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700590 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500591 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400592 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
593 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500594 if (new_v == 0)
595 {
596 mark_linear:
597 new_log2_pages--;
598 /* pinned collisions, use linear search */
599 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400600 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
601 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500602 mark_bucket_linear = 1;
Dave Barach2ce28d62019-05-03 12:58:01 -0400603 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_linear, 1);
Dave Barach5e6b9582016-12-12 15:37:29 -0500604 }
Dave Barach2ce28d62019-05-03 12:58:01 -0400605 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_resplit, 1);
606 BV (clib_bihash_increment_stat) (h, BIHASH_STAT_splits,
607 old_log2_pages + 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 }
609
610 /* Try to add the new entry */
611 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400612 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500613 limit = BIHASH_KVP_PER_PAGE;
614 if (mark_bucket_linear)
615 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500617 new_hash &= (1 << new_log2_pages) - 1;
618 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400619
Dave Barach5e6b9582016-12-12 15:37:29 -0500620 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621 {
Dave Barachc3799992016-08-15 11:12:27 -0400622 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
623 {
Dave Barach178cf492018-11-13 16:34:13 -0500624 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
Dave Barachc3799992016-08-15 11:12:27 -0400625 goto expand_ok;
626 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700627 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400628
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400630 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500631 /*
632 * If we've already doubled the size of the bucket once,
633 * fall back to linear search now.
634 */
635 if (resplit_once)
636 goto mark_linear;
637 else
638 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639
Dave Barachc3799992016-08-15 11:12:27 -0400640expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500641 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400642 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500643 tmp_b.linear_search = mark_bucket_linear;
Dave Barache7d212f2018-02-07 13:14:06 -0500644 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach9466c452018-08-24 17:21:14 -0400645 ASSERT (tmp_b.refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400646 tmp_b.lock = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400647 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700648 b->as_u64 = tmp_b.as_u64;
Andrew Yourtchenkodf32bc42018-09-20 15:36:51 +0200649 /* free the old bucket */
650 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
651 BV (value_free) (h, v, h->saved_bucket.log2_pages);
Dave Barach508498f2018-07-19 12:11:16 -0400652 BV (clib_bihash_alloc_unlock) (h);
653 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654}
655
Matus Fabian828d27e2018-08-21 03:15:50 -0700656int BV (clib_bihash_add_del)
657 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
658{
659 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
660}
661
662int BV (clib_bihash_add_or_overwrite_stale)
663 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
664 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
665{
666 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
667}
668
Dave Barachc3799992016-08-15 11:12:27 -0400669int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -0400670 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400671 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672{
673 u64 hash;
674 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400675 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400676 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500677 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Dave Barachc3799992016-08-15 11:12:27 -0400679 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Dave Barachc3799992016-08-15 11:12:27 -0400681 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682
Dave Barachc3799992016-08-15 11:12:27 -0400683 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700684 b = &h->buckets[bucket_index];
685
Damjan Marion882fcfe2018-07-17 23:01:49 +0200686 if (BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700687 return -1;
688
Dave Barach508498f2018-07-19 12:11:16 -0400689 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400690 {
Dave Barach508498f2018-07-19 12:11:16 -0400691 volatile BVT (clib_bihash_bucket) * bv = b;
692 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200693 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400694 }
695
Ed Warnickecb9cada2015-12-08 15:45:58 -0700696 hash >>= h->log2_nbuckets;
697
Dave Barachc3799992016-08-15 11:12:27 -0400698 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500699 limit = BIHASH_KVP_PER_PAGE;
700 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
701 if (PREDICT_FALSE (b->linear_search))
702 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400703
Dave Barach5e6b9582016-12-12 15:37:29 -0500704 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700705 {
Dave Barachc3799992016-08-15 11:12:27 -0400706 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
707 {
708 *valuep = v->kvp[i];
709 return 0;
710 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700711 }
712 return -1;
713}
714
Dave Barachc3799992016-08-15 11:12:27 -0400715u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700716{
Dave Barachc3799992016-08-15 11:12:27 -0400717 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700718 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -0400719 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400720 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700721 int i, j, k;
722 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -0500723 u64 active_buckets = 0;
724 u64 linear_buckets = 0;
Dave Barach97f5af02018-02-22 09:48:45 -0500725 u64 used_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700726
727 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400728
Ed Warnickecb9cada2015-12-08 15:45:58 -0700729 for (i = 0; i < h->nbuckets; i++)
730 {
Dave Barachc3799992016-08-15 11:12:27 -0400731 b = &h->buckets[i];
Damjan Marion882fcfe2018-07-17 23:01:49 +0200732 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -0400733 {
734 if (verbose > 1)
735 s = format (s, "[%d]: empty\n", i);
736 continue;
737 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700738
Dave Barache7d212f2018-02-07 13:14:06 -0500739 active_buckets++;
740
741 if (b->linear_search)
742 linear_buckets++;
743
Ed Warnickecb9cada2015-12-08 15:45:58 -0700744 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400745 {
Dave Barach9466c452018-08-24 17:21:14 -0400746 s = format (s, "[%d]: heap offset %lld, len %d, linear %d\n", i,
Dave Barach5e6b9582016-12-12 15:37:29 -0500747 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400748 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700749
Dave Barachc3799992016-08-15 11:12:27 -0400750 v = BV (clib_bihash_get_value) (h, b->offset);
751 for (j = 0; j < (1 << b->log2_pages); j++)
752 {
753 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
754 {
755 if (BV (clib_bihash_is_free) (&v->kvp[k]))
756 {
757 if (verbose > 1)
758 s = format (s, " %d: empty\n",
759 j * BIHASH_KVP_PER_PAGE + k);
760 continue;
761 }
762 if (verbose)
763 {
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800764 if (h->fmt_fn)
765 {
766 s = format (s, " %d: %U\n",
767 j * BIHASH_KVP_PER_PAGE + k,
Vijayabhaskar Katamreddy72739a62019-05-07 13:27:32 -0700768 h->fmt_fn, &(v->kvp[k]), verbose);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800769 }
770 else
771 {
772 s = format (s, " %d: %U\n",
773 j * BIHASH_KVP_PER_PAGE + k,
774 BV (format_bihash_kvp), &(v->kvp[k]));
775 }
Dave Barachc3799992016-08-15 11:12:27 -0400776 }
777 active_elements++;
778 }
779 v++;
780 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700781 }
782
Dave Barache7d212f2018-02-07 13:14:06 -0500783 s = format (s, " %lld active elements %lld active buckets\n",
784 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700785 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -0500786
787 for (i = 0; i < vec_len (h->freelists); i++)
788 {
789 u32 nfree = 0;
790 BVT (clib_bihash_value) * free_elt;
Dave Barachffb14b92018-09-11 17:20:23 -0400791 u64 free_elt_as_u64 = h->freelists[i];
Dave Barache7d212f2018-02-07 13:14:06 -0500792
Dave Barachffb14b92018-09-11 17:20:23 -0400793 while (free_elt_as_u64)
Dave Barache7d212f2018-02-07 13:14:06 -0500794 {
Dave Barachffb14b92018-09-11 17:20:23 -0400795 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
Dave Barache7d212f2018-02-07 13:14:06 -0500796 nfree++;
Dave Barachffb14b92018-09-11 17:20:23 -0400797 free_elt_as_u64 = free_elt->next_free_as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -0500798 }
799
Dave Barach9466c452018-08-24 17:21:14 -0400800 if (nfree || verbose)
801 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
Dave Barache7d212f2018-02-07 13:14:06 -0500802 }
803
804 s = format (s, " %lld linear search buckets\n", linear_buckets);
Dave Barachffb14b92018-09-11 17:20:23 -0400805 used_bytes = alloc_arena_next (h);
Dave Barach97f5af02018-02-22 09:48:45 -0500806 s = format (s,
807 " arena: base %llx, next %llx\n"
808 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
Dave Barach9466c452018-08-24 17:21:14 -0400809 alloc_arena (h), alloc_arena_next (h),
Dave Barach97f5af02018-02-22 09:48:45 -0500810 used_bytes, used_bytes >> 20,
Dave Barach9466c452018-08-24 17:21:14 -0400811 alloc_arena_size (h), alloc_arena_size (h) >> 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700812 return s;
813}
814
Dave Barachc3799992016-08-15 11:12:27 -0400815void BV (clib_bihash_foreach_key_value_pair)
816 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700817{
818 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -0400819 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400820 BVT (clib_bihash_value) * v;
821 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
822
Ed Warnickecb9cada2015-12-08 15:45:58 -0700823 for (i = 0; i < h->nbuckets; i++)
824 {
Dave Barachc3799992016-08-15 11:12:27 -0400825 b = &h->buckets[i];
Damjan Marion882fcfe2018-07-17 23:01:49 +0200826 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -0400827 continue;
828
829 v = BV (clib_bihash_get_value) (h, b->offset);
830 for (j = 0; j < (1 << b->log2_pages); j++)
831 {
832 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
833 {
834 if (BV (clib_bihash_is_free) (&v->kvp[k]))
835 continue;
836
837 (*fp) (&v->kvp[k], arg);
Dave Barachca45ee72018-08-06 08:43:47 -0400838 /*
839 * In case the callback deletes the last entry in the bucket...
840 */
841 if (BV (clib_bihash_bucket_is_empty) (b))
842 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400843 }
844 v++;
845 }
Dave Barachca45ee72018-08-06 08:43:47 -0400846 doublebreak:
847 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700848 }
849}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400850
Chris Luke16bcf7d2016-09-01 14:31:46 -0400851/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400852
853/*
854 * fd.io coding-style-patch-verification: ON
855 *
856 * Local Variables:
857 * eval: (c-set-style "gnu")
858 * End:
859 */