blob: 2840342a5ce81b8e8ea464d1b24527bd6fa45e40 [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);
Dave Barach9466c452018-08-24 17:21:14 -0400199#if BIHASH_32_64_SVM == 0
Dave Barach97f5af02018-02-22 09:48:45 -0500200 vec_free (h->freelists);
Dave Barach9466c452018-08-24 17:21:14 -0400201#else
202 if (h->memfd > 0)
203 (void) close (h->memfd);
204#endif
205 clib_mem_vm_free ((void *) (uword) (alloc_arena (h)), alloc_arena_size (h));
Dave Barachb7b92992018-10-17 10:38:51 -0400206 clib_memset (h, 0, sizeof (*h));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700207}
208
Dave Barachc3799992016-08-15 11:12:27 -0400209static
210BVT (clib_bihash_value) *
211BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700212{
Dave Barachc3799992016-08-15 11:12:27 -0400213 BVT (clib_bihash_value) * rv = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700214
Dave Barach508498f2018-07-19 12:11:16 -0400215 ASSERT (h->alloc_lock[0]);
Dave Barach9466c452018-08-24 17:21:14 -0400216
217#if BIHASH_32_64_SVM
218 ASSERT (log2_pages < vec_len (h->freelists));
219#endif
220
Dave Barachc3799992016-08-15 11:12:27 -0400221 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700222 {
Dave Barach97f5af02018-02-22 09:48:45 -0500223 vec_validate_init_empty (h->freelists, log2_pages, 0);
224 rv = BV (alloc_aligned) (h, (sizeof (*rv) * (1 << log2_pages)));
Dave Barachc3799992016-08-15 11:12:27 -0400225 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700226 }
Dave Barachffb14b92018-09-11 17:20:23 -0400227 rv = BV (clib_bihash_get_value) (h, (uword) h->freelists[log2_pages]);
Dave Barach9466c452018-08-24 17:21:14 -0400228 h->freelists[log2_pages] = rv->next_free_as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700229
Dave Barachc3799992016-08-15 11:12:27 -0400230initialize:
231 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -0400232 /*
233 * Latest gcc complains that the length arg is zero
234 * if we replace (1<<log2_pages) with vec_len(rv).
235 * No clue.
236 */
Dave Barachb7b92992018-10-17 10:38:51 -0400237 clib_memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
Dave Barachc3799992016-08-15 11:12:27 -0400238 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700239}
240
241static void
Dave Barachba7ddfe2017-05-17 20:20:50 -0400242BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
243 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700244{
Dave Barach508498f2018-07-19 12:11:16 -0400245 ASSERT (h->alloc_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700246
Dave Barachc3799992016-08-15 11:12:27 -0400247 ASSERT (vec_len (h->freelists) > log2_pages);
248
Dave Barach508498f2018-07-19 12:11:16 -0400249 if (CLIB_DEBUG > 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400250 clib_memset (v, 0xFE, sizeof (*v) * (1 << log2_pages));
Dave Barach508498f2018-07-19 12:11:16 -0400251
Dave Barach9466c452018-08-24 17:21:14 -0400252 v->next_free_as_u64 = (u64) h->freelists[log2_pages];
Dave Barachffb14b92018-09-11 17:20:23 -0400253 h->freelists[log2_pages] = (u64) BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700254}
255
256static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400257BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258{
Dave Barachc3799992016-08-15 11:12:27 -0400259 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400260 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400261 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200262 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400263 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700264
Dave Barach508498f2018-07-19 12:11:16 -0400265 ASSERT (h->alloc_lock[0]);
266
Damjan Marionf55f9b82017-05-10 21:06:28 +0200267 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 {
Damjan Marionf55f9b82017-05-10 21:06:28 +0200269 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700270 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700271 }
272
Dave Barachc3799992016-08-15 11:12:27 -0400273 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700274 * working_copies are per-cpu so that near-simultaneous
275 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400276 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700277 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200278 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400279 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700280
281 h->saved_bucket.as_u64 = b->as_u64;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700282
Dave Barachba7ddfe2017-05-17 20:20:50 -0400283 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700284 {
Dave Barach97f5af02018-02-22 09:48:45 -0500285 /*
286 * It's not worth the bookkeeping to free working copies
287 * if (working_copy)
288 * clib_mem_free (working_copy);
289 */
290 working_copy = BV (alloc_aligned)
291 (h, sizeof (working_copy[0]) * (1 << b->log2_pages));
Dave Barachba7ddfe2017-05-17 20:20:50 -0400292 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200293 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294 }
295
Dave Barachc3799992016-08-15 11:12:27 -0400296 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700297
Dave Barach178cf492018-11-13 16:34:13 -0500298 clib_memcpy_fast (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700299 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400300 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
301 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700302 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200303 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700304}
305
Dave Barachc3799992016-08-15 11:12:27 -0400306static
307BVT (clib_bihash_value) *
308BV (split_and_rehash)
309 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400310 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
311 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312{
Dave Barach5e6b9582016-12-12 15:37:29 -0500313 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400314 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700315
Dave Barach508498f2018-07-19 12:11:16 -0400316 ASSERT (h->alloc_lock[0]);
317
Dave Barachc3799992016-08-15 11:12:27 -0400318 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400319 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700320
Dave Barachba7ddfe2017-05-17 20:20:50 -0400321 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700322 {
323 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400324
Dave Barach5e6b9582016-12-12 15:37:29 -0500325 /* Entry not in use? Forget it */
326 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
327 continue;
328
329 /* rehash the item onto its new home-page */
330 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
331 new_hash >>= h->log2_nbuckets;
332 new_hash &= (1 << new_log2_pages) - 1;
333 new_v = &new_values[new_hash];
334
335 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700336 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400337 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500338 /* Empty slot */
339 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400340 {
Dave Barach178cf492018-11-13 16:34:13 -0500341 clib_memcpy_fast (&(new_v->kvp[j]), &(old_values->kvp[i]),
342 sizeof (new_v->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500343 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400344 }
Dave Barachc3799992016-08-15 11:12:27 -0400345 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500346 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400347 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500348 return 0;
349 doublebreak:;
350 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400351
Dave Barach5e6b9582016-12-12 15:37:29 -0500352 return new_values;
353}
354
355static
356BVT (clib_bihash_value) *
357BV (split_and_rehash_linear)
358 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400359 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
360 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500361{
362 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400363 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500364
Dave Barach508498f2018-07-19 12:11:16 -0400365 ASSERT (h->alloc_lock[0]);
366
Dave Barach5e6b9582016-12-12 15:37:29 -0500367 new_values = BV (value_alloc) (h, new_log2_pages);
368 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400369 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500370
371 j = 0;
372 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400373 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500374 {
375 /* Find a free slot in the new linear scan bucket */
376 for (; j < new_length; j++)
377 {
Dave Barach8f544962017-01-18 10:23:22 -0500378 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500379 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
380 goto doublebreak;
381
382 /* New value should never be in use */
383 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
384 {
385 /* Copy the old value and move along */
Dave Barach178cf492018-11-13 16:34:13 -0500386 clib_memcpy_fast (&(new_values->kvp[j]), &(old_values->kvp[i]),
387 sizeof (new_values->kvp[j]));
Dave Barach5e6b9582016-12-12 15:37:29 -0500388 j++;
389 goto doublebreak;
390 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500391 }
Dave Barach8f544962017-01-18 10:23:22 -0500392 /* This should never happen... */
393 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400394 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500395 return 0;
396
Dave Barach5e6b9582016-12-12 15:37:29 -0500397 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700398 }
399 return new_values;
400}
401
Matus Fabian828d27e2018-08-21 03:15:50 -0700402static inline int BV (clib_bihash_add_del_inline)
403 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add,
404 int (*is_stale_cb) (BVT (clib_bihash_kv) *, void *), void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405{
406 u32 bucket_index;
Dave Barach908a5ea2017-07-14 12:42:21 -0400407 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400408 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Dave Barach5e6b9582016-12-12 15:37:29 -0500409 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 u64 hash, new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400411 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200412 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500413 int mark_bucket_linear;
414 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415
Dave Barachc3799992016-08-15 11:12:27 -0400416 hash = BV (clib_bihash_hash) (add_v);
417
418 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700419 b = &h->buckets[bucket_index];
420
421 hash >>= h->log2_nbuckets;
422
Dave Barach508498f2018-07-19 12:11:16 -0400423 BV (clib_bihash_lock_bucket) (b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700424
425 /* First elt in the bucket? */
Damjan Marion882fcfe2018-07-17 23:01:49 +0200426 if (BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700427 {
428 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400429 {
Dave Barach508498f2018-07-19 12:11:16 -0400430 BV (clib_bihash_unlock_bucket) (b);
431 return (-1);
Dave Barachc3799992016-08-15 11:12:27 -0400432 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433
Dave Barach508498f2018-07-19 12:11:16 -0400434 BV (clib_bihash_alloc_lock) (h);
Dave Barachc3799992016-08-15 11:12:27 -0400435 v = BV (value_alloc) (h, 0);
Dave Barach508498f2018-07-19 12:11:16 -0400436 BV (clib_bihash_alloc_unlock) (h);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400437
Dave Barachc3799992016-08-15 11:12:27 -0400438 *v->kvp = *add_v;
Dave Barach508498f2018-07-19 12:11:16 -0400439 tmp_b.as_u64 = 0; /* clears bucket lock */
Dave Barachc3799992016-08-15 11:12:27 -0400440 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500441 tmp_b.refcnt = 1;
Dave Barach508498f2018-07-19 12:11:16 -0400442 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Tom Seidenberg97f8ae92019-03-15 10:15:26 -0400444 b->as_u64 = tmp_b.as_u64; /* unlocks the bucket */
Dave Barach508498f2018-07-19 12:11:16 -0400445 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446 }
447
Dave Barach508498f2018-07-19 12:11:16 -0400448 /* WARNING: we're still looking at the live copy... */
Dave Barach5e6b9582016-12-12 15:37:29 -0500449 limit = BIHASH_KVP_PER_PAGE;
Dave Barach508498f2018-07-19 12:11:16 -0400450 v = BV (clib_bihash_get_value) (h, b->offset);
451
Dave Barach5e6b9582016-12-12 15:37:29 -0500452 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
453 if (b->linear_search)
454 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400455
Ed Warnickecb9cada2015-12-08 15:45:58 -0700456 if (is_add)
457 {
Dave Barachc3799992016-08-15 11:12:27 -0400458 /*
Dave Barach508498f2018-07-19 12:11:16 -0400459 * Because reader threads are looking at live data,
460 * we have to be extra careful. Readers do NOT hold the
461 * bucket lock. We need to be SLOWER than a search, past the
462 * point where readers CHECK the bucket lock.
463 */
464
465 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700466 * For obvious (in hindsight) reasons, see if we're supposed to
467 * replace an existing key, then look for an empty slot.
468 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500469 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400470 {
Dave Baracha11bf452019-04-17 17:27:31 -0400471 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400472 {
Dave Barach508498f2018-07-19 12:11:16 -0400473 CLIB_MEMORY_BARRIER (); /* Add a delay */
Dave Barach178cf492018-11-13 16:34:13 -0500474 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Dave Barach508498f2018-07-19 12:11:16 -0400475 BV (clib_bihash_unlock_bucket) (b);
476 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400477 }
478 }
Dave Barach508498f2018-07-19 12:11:16 -0400479 /*
480 * Look for an empty slot. If found, use it
481 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500482 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400483 {
484 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
485 {
Dave Barach508498f2018-07-19 12:11:16 -0400486 /*
487 * Copy the value first, so that if a reader manages
488 * to match the new key, the value will be right...
489 */
Dave Barach178cf492018-11-13 16:34:13 -0500490 clib_memcpy_fast (&(v->kvp[i].value),
491 &add_v->value, sizeof (add_v->value));
Dave Barach508498f2018-07-19 12:11:16 -0400492 CLIB_MEMORY_BARRIER (); /* Make sure the value has settled */
Dave Barach178cf492018-11-13 16:34:13 -0500493 clib_memcpy_fast (&(v->kvp[i]), &add_v->key,
494 sizeof (add_v->key));
Dave Barache7d212f2018-02-07 13:14:06 -0500495 b->refcnt++;
Dave Barach9466c452018-08-24 17:21:14 -0400496 ASSERT (b->refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400497 BV (clib_bihash_unlock_bucket) (b);
498 return (0);
Dave Barachc3799992016-08-15 11:12:27 -0400499 }
500 }
Matus Fabian828d27e2018-08-21 03:15:50 -0700501 /* look for stale data to overwrite */
502 if (is_stale_cb)
503 {
504 for (i = 0; i < limit; i++)
505 {
506 if (is_stale_cb (&(v->kvp[i]), arg))
507 {
508 CLIB_MEMORY_BARRIER ();
Dave Barach178cf492018-11-13 16:34:13 -0500509 clib_memcpy_fast (&(v->kvp[i]), add_v, sizeof (*add_v));
Matus Fabian828d27e2018-08-21 03:15:50 -0700510 BV (clib_bihash_unlock_bucket) (b);
511 return (0);
512 }
513 }
514 }
Dave Barach508498f2018-07-19 12:11:16 -0400515 /* Out of space in this bucket, split the bucket... */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700516 }
Dave Barach508498f2018-07-19 12:11:16 -0400517 else /* delete case */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700518 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500519 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400520 {
Dave Barach508498f2018-07-19 12:11:16 -0400521 /* Found the key? Kill it... */
Dave Baracha11bf452019-04-17 17:27:31 -0400522 if (BV (clib_bihash_key_compare) (v->kvp[i].key, add_v->key))
Dave Barachc3799992016-08-15 11:12:27 -0400523 {
Dave Barachb7b92992018-10-17 10:38:51 -0400524 clib_memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
Dave Barach508498f2018-07-19 12:11:16 -0400525 /* Is the bucket empty? */
526 if (PREDICT_TRUE (b->refcnt > 1))
Dave Barache7d212f2018-02-07 13:14:06 -0500527 {
Dave Barach508498f2018-07-19 12:11:16 -0400528 b->refcnt--;
529 BV (clib_bihash_unlock_bucket) (b);
530 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500531 }
Dave Barach508498f2018-07-19 12:11:16 -0400532 else /* yes, free it */
Dave Barache7d212f2018-02-07 13:14:06 -0500533 {
Dave Barach508498f2018-07-19 12:11:16 -0400534 /* Save old bucket value, need log2_pages to free it */
535 tmp_b.as_u64 = b->as_u64;
536 CLIB_MEMORY_BARRIER ();
537
538 /* Kill and unlock the bucket */
539 b->as_u64 = 0;
540
541 /* And free the backing storage */
542 BV (clib_bihash_alloc_lock) (h);
543 /* Note: v currently points into the middle of the bucket */
544 v = BV (clib_bihash_get_value) (h, tmp_b.offset);
545 BV (value_free) (h, v, tmp_b.log2_pages);
546 BV (clib_bihash_alloc_unlock) (h);
547 return (0);
Dave Barache7d212f2018-02-07 13:14:06 -0500548 }
Dave Barachc3799992016-08-15 11:12:27 -0400549 }
550 }
Dave Barach508498f2018-07-19 12:11:16 -0400551 /* Not found... */
552 BV (clib_bihash_unlock_bucket) (b);
553 return (-3);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554 }
555
Dave Barach508498f2018-07-19 12:11:16 -0400556 /* Move readers to a (locked) temp copy of the bucket */
557 BV (clib_bihash_alloc_lock) (h);
558 BV (make_working_copy) (h, b);
559
560 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
561
Dave Barachba7ddfe2017-05-17 20:20:50 -0400562 old_log2_pages = h->saved_bucket.log2_pages;
563 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500564 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700565
Damjan Marionf55f9b82017-05-10 21:06:28 +0200566 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500567 resplit_once = 0;
568
Dave Barachba7ddfe2017-05-17 20:20:50 -0400569 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
570 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700571 if (new_v == 0)
572 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500573 try_resplit:
574 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500576 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400577 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
578 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500579 if (new_v == 0)
580 {
581 mark_linear:
582 new_log2_pages--;
583 /* pinned collisions, use linear search */
584 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400585 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
586 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500587 mark_bucket_linear = 1;
588 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589 }
590
591 /* Try to add the new entry */
592 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400593 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500594 limit = BIHASH_KVP_PER_PAGE;
595 if (mark_bucket_linear)
596 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700597 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500598 new_hash &= (1 << new_log2_pages) - 1;
599 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400600
Dave Barach5e6b9582016-12-12 15:37:29 -0500601 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602 {
Dave Barachc3799992016-08-15 11:12:27 -0400603 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
604 {
Dave Barach178cf492018-11-13 16:34:13 -0500605 clib_memcpy_fast (&(new_v->kvp[i]), add_v, sizeof (*add_v));
Dave Barachc3799992016-08-15 11:12:27 -0400606 goto expand_ok;
607 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700608 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400609
Ed Warnickecb9cada2015-12-08 15:45:58 -0700610 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400611 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500612 /*
613 * If we've already doubled the size of the bucket once,
614 * fall back to linear search now.
615 */
616 if (resplit_once)
617 goto mark_linear;
618 else
619 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620
Dave Barachc3799992016-08-15 11:12:27 -0400621expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500622 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400623 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500624 tmp_b.linear_search = mark_bucket_linear;
Dave Barache7d212f2018-02-07 13:14:06 -0500625 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
Dave Barach9466c452018-08-24 17:21:14 -0400626 ASSERT (tmp_b.refcnt > 0);
Dave Barach508498f2018-07-19 12:11:16 -0400627 tmp_b.lock = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400628 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700629 b->as_u64 = tmp_b.as_u64;
Andrew Yourtchenkodf32bc42018-09-20 15:36:51 +0200630 /* free the old bucket */
631 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
632 BV (value_free) (h, v, h->saved_bucket.log2_pages);
Dave Barach508498f2018-07-19 12:11:16 -0400633 BV (clib_bihash_alloc_unlock) (h);
634 return (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700635}
636
Matus Fabian828d27e2018-08-21 03:15:50 -0700637int BV (clib_bihash_add_del)
638 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
639{
640 return BV (clib_bihash_add_del_inline) (h, add_v, is_add, 0, 0);
641}
642
643int BV (clib_bihash_add_or_overwrite_stale)
644 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v,
645 int (*stale_callback) (BVT (clib_bihash_kv) *, void *), void *arg)
646{
647 return BV (clib_bihash_add_del_inline) (h, add_v, 1, stale_callback, arg);
648}
649
Dave Barachc3799992016-08-15 11:12:27 -0400650int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -0400651 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400652 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653{
654 u64 hash;
655 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400656 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400657 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500658 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Dave Barachc3799992016-08-15 11:12:27 -0400660 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661
Dave Barachc3799992016-08-15 11:12:27 -0400662 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700663
Dave Barachc3799992016-08-15 11:12:27 -0400664 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665 b = &h->buckets[bucket_index];
666
Damjan Marion882fcfe2018-07-17 23:01:49 +0200667 if (BV (clib_bihash_bucket_is_empty) (b))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700668 return -1;
669
Dave Barach508498f2018-07-19 12:11:16 -0400670 if (PREDICT_FALSE (b->lock))
Dave Barach908a5ea2017-07-14 12:42:21 -0400671 {
Dave Barach508498f2018-07-19 12:11:16 -0400672 volatile BVT (clib_bihash_bucket) * bv = b;
673 while (bv->lock)
Damjan Marion2a03efe2018-07-20 21:48:59 +0200674 CLIB_PAUSE ();
Dave Barach908a5ea2017-07-14 12:42:21 -0400675 }
676
Ed Warnickecb9cada2015-12-08 15:45:58 -0700677 hash >>= h->log2_nbuckets;
678
Dave Barachc3799992016-08-15 11:12:27 -0400679 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500680 limit = BIHASH_KVP_PER_PAGE;
681 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
682 if (PREDICT_FALSE (b->linear_search))
683 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400684
Dave Barach5e6b9582016-12-12 15:37:29 -0500685 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686 {
Dave Barachc3799992016-08-15 11:12:27 -0400687 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
688 {
689 *valuep = v->kvp[i];
690 return 0;
691 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700692 }
693 return -1;
694}
695
Dave Barachc3799992016-08-15 11:12:27 -0400696u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700697{
Dave Barachc3799992016-08-15 11:12:27 -0400698 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700699 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -0400700 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400701 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700702 int i, j, k;
703 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -0500704 u64 active_buckets = 0;
705 u64 linear_buckets = 0;
Dave Barach97f5af02018-02-22 09:48:45 -0500706 u64 used_bytes;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700707
708 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400709
Ed Warnickecb9cada2015-12-08 15:45:58 -0700710 for (i = 0; i < h->nbuckets; i++)
711 {
Dave Barachc3799992016-08-15 11:12:27 -0400712 b = &h->buckets[i];
Damjan Marion882fcfe2018-07-17 23:01:49 +0200713 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -0400714 {
715 if (verbose > 1)
716 s = format (s, "[%d]: empty\n", i);
717 continue;
718 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700719
Dave Barache7d212f2018-02-07 13:14:06 -0500720 active_buckets++;
721
722 if (b->linear_search)
723 linear_buckets++;
724
Ed Warnickecb9cada2015-12-08 15:45:58 -0700725 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400726 {
Dave Barach9466c452018-08-24 17:21:14 -0400727 s = format (s, "[%d]: heap offset %lld, len %d, linear %d\n", i,
Dave Barach5e6b9582016-12-12 15:37:29 -0500728 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400729 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700730
Dave Barachc3799992016-08-15 11:12:27 -0400731 v = BV (clib_bihash_get_value) (h, b->offset);
732 for (j = 0; j < (1 << b->log2_pages); j++)
733 {
734 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
735 {
736 if (BV (clib_bihash_is_free) (&v->kvp[k]))
737 {
738 if (verbose > 1)
739 s = format (s, " %d: empty\n",
740 j * BIHASH_KVP_PER_PAGE + k);
741 continue;
742 }
743 if (verbose)
744 {
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800745 if (h->fmt_fn)
746 {
747 s = format (s, " %d: %U\n",
748 j * BIHASH_KVP_PER_PAGE + k,
749 h->fmt_fn, &(v->kvp[k]));
750 }
751 else
752 {
753 s = format (s, " %d: %U\n",
754 j * BIHASH_KVP_PER_PAGE + k,
755 BV (format_bihash_kvp), &(v->kvp[k]));
756 }
Dave Barachc3799992016-08-15 11:12:27 -0400757 }
758 active_elements++;
759 }
760 v++;
761 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700762 }
763
Dave Barache7d212f2018-02-07 13:14:06 -0500764 s = format (s, " %lld active elements %lld active buckets\n",
765 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700766 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -0500767
768 for (i = 0; i < vec_len (h->freelists); i++)
769 {
770 u32 nfree = 0;
771 BVT (clib_bihash_value) * free_elt;
Dave Barachffb14b92018-09-11 17:20:23 -0400772 u64 free_elt_as_u64 = h->freelists[i];
Dave Barache7d212f2018-02-07 13:14:06 -0500773
Dave Barachffb14b92018-09-11 17:20:23 -0400774 while (free_elt_as_u64)
Dave Barache7d212f2018-02-07 13:14:06 -0500775 {
Dave Barachffb14b92018-09-11 17:20:23 -0400776 free_elt = BV (clib_bihash_get_value) (h, free_elt_as_u64);
Dave Barache7d212f2018-02-07 13:14:06 -0500777 nfree++;
Dave Barachffb14b92018-09-11 17:20:23 -0400778 free_elt_as_u64 = free_elt->next_free_as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -0500779 }
780
Dave Barach9466c452018-08-24 17:21:14 -0400781 if (nfree || verbose)
782 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
Dave Barache7d212f2018-02-07 13:14:06 -0500783 }
784
785 s = format (s, " %lld linear search buckets\n", linear_buckets);
Dave Barachffb14b92018-09-11 17:20:23 -0400786 used_bytes = alloc_arena_next (h);
Dave Barach97f5af02018-02-22 09:48:45 -0500787 s = format (s,
788 " arena: base %llx, next %llx\n"
789 " used %lld b (%lld Mbytes) of %lld b (%lld Mbytes)\n",
Dave Barach9466c452018-08-24 17:21:14 -0400790 alloc_arena (h), alloc_arena_next (h),
Dave Barach97f5af02018-02-22 09:48:45 -0500791 used_bytes, used_bytes >> 20,
Dave Barach9466c452018-08-24 17:21:14 -0400792 alloc_arena_size (h), alloc_arena_size (h) >> 20);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700793 return s;
794}
795
Dave Barachc3799992016-08-15 11:12:27 -0400796void BV (clib_bihash_foreach_key_value_pair)
797 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700798{
799 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -0400800 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400801 BVT (clib_bihash_value) * v;
802 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
803
Ed Warnickecb9cada2015-12-08 15:45:58 -0700804 for (i = 0; i < h->nbuckets; i++)
805 {
Dave Barachc3799992016-08-15 11:12:27 -0400806 b = &h->buckets[i];
Damjan Marion882fcfe2018-07-17 23:01:49 +0200807 if (BV (clib_bihash_bucket_is_empty) (b))
Dave Barachc3799992016-08-15 11:12:27 -0400808 continue;
809
810 v = BV (clib_bihash_get_value) (h, b->offset);
811 for (j = 0; j < (1 << b->log2_pages); j++)
812 {
813 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
814 {
815 if (BV (clib_bihash_is_free) (&v->kvp[k]))
816 continue;
817
818 (*fp) (&v->kvp[k], arg);
Dave Barachca45ee72018-08-06 08:43:47 -0400819 /*
820 * In case the callback deletes the last entry in the bucket...
821 */
822 if (BV (clib_bihash_bucket_is_empty) (b))
823 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400824 }
825 v++;
826 }
Dave Barachca45ee72018-08-06 08:43:47 -0400827 doublebreak:
828 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700829 }
830}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400831
Chris Luke16bcf7d2016-09-01 14:31:46 -0400832/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400833
834/*
835 * fd.io coding-style-patch-verification: ON
836 *
837 * Local Variables:
838 * eval: (c-set-style "gnu")
839 * End:
840 */