blob: 2b40af31d6fb9e8b174d45af151274c1fb60726d [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 Barachc3799992016-08-15 11:12:27 -040018void BV (clib_bihash_init)
19 (BVT (clib_bihash) * h, char *name, u32 nbuckets, uword memory_size)
Ed Warnickecb9cada2015-12-08 15:45:58 -070020{
Dave Barachc3799992016-08-15 11:12:27 -040021 void *oldheap;
Dave Barach908a5ea2017-07-14 12:42:21 -040022 int i;
Ed Warnickecb9cada2015-12-08 15:45:58 -070023
24 nbuckets = 1 << (max_log2 (nbuckets));
25
Dave Barachc3799992016-08-15 11:12:27 -040026 h->name = (u8 *) name;
Ed Warnickecb9cada2015-12-08 15:45:58 -070027 h->nbuckets = nbuckets;
28 h->log2_nbuckets = max_log2 (nbuckets);
Dave Barach908a5ea2017-07-14 12:42:21 -040029 h->cache_hits = 0;
30 h->cache_misses = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070031
Dave Barachc3799992016-08-15 11:12:27 -040032 h->mheap = mheap_alloc (0 /* use VM */ , memory_size);
Ed Warnickecb9cada2015-12-08 15:45:58 -070033
34 oldheap = clib_mem_set_heap (h->mheap);
35 vec_validate_aligned (h->buckets, nbuckets - 1, CLIB_CACHE_LINE_BYTES);
Dave Barachc3799992016-08-15 11:12:27 -040036 h->writer_lock = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES,
37 CLIB_CACHE_LINE_BYTES);
JingLiuZTE4c9f2a82017-11-08 15:35:01 +080038 h->writer_lock[0] = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -070039
Dave Barach908a5ea2017-07-14 12:42:21 -040040 for (i = 0; i < nbuckets; i++)
41 BV (clib_bihash_reset_cache) (h->buckets + i);
42
Ed Warnickecb9cada2015-12-08 15:45:58 -070043 clib_mem_set_heap (oldheap);
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -080044
45 h->fmt_fn = NULL;
46}
47
48void BV (clib_bihash_set_kvp_format_fn) (BVT (clib_bihash) * h,
49 format_function_t * fmt_fn)
50{
51 h->fmt_fn = fmt_fn;
Ed Warnickecb9cada2015-12-08 15:45:58 -070052}
53
Dave Barachc3799992016-08-15 11:12:27 -040054void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055{
Dave Barachc3799992016-08-15 11:12:27 -040056 mheap_free (h->mheap);
57 memset (h, 0, sizeof (*h));
Ed Warnickecb9cada2015-12-08 15:45:58 -070058}
59
Dave Barachc3799992016-08-15 11:12:27 -040060static
61BVT (clib_bihash_value) *
62BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070063{
Dave Barachc3799992016-08-15 11:12:27 -040064 BVT (clib_bihash_value) * rv = 0;
65 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
Dave Barachc3799992016-08-15 11:12:27 -040067 ASSERT (h->writer_lock[0]);
68 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 {
Dave Barachc3799992016-08-15 11:12:27 -040070 oldheap = clib_mem_set_heap (h->mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070071
Dave Barachc3799992016-08-15 11:12:27 -040072 vec_validate (h->freelists, log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -040073 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
74 CLIB_CACHE_LINE_BYTES);
Dave Barachc3799992016-08-15 11:12:27 -040075 clib_mem_set_heap (oldheap);
76 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 }
Dave Barachc3799992016-08-15 11:12:27 -040078 rv = h->freelists[log2_pages];
79 h->freelists[log2_pages] = rv->next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070080
Dave Barachc3799992016-08-15 11:12:27 -040081initialize:
82 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -040083 /*
84 * Latest gcc complains that the length arg is zero
85 * if we replace (1<<log2_pages) with vec_len(rv).
86 * No clue.
87 */
88 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
89 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070090}
91
92static void
Dave Barachba7ddfe2017-05-17 20:20:50 -040093BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
94 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070095{
Dave Barachc3799992016-08-15 11:12:27 -040096 ASSERT (h->writer_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070097
Dave Barachc3799992016-08-15 11:12:27 -040098 ASSERT (vec_len (h->freelists) > log2_pages);
99
100 v->next_free = h->freelists[log2_pages];
101 h->freelists[log2_pages] = v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700102}
103
104static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -0400105BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700106{
Dave Barachc3799992016-08-15 11:12:27 -0400107 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400108 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400109 void *oldheap;
110 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200111 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400112 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700113
Damjan Marionf55f9b82017-05-10 21:06:28 +0200114 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 {
116 oldheap = clib_mem_set_heap (h->mheap);
Damjan Marionf55f9b82017-05-10 21:06:28 +0200117 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700118 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119 clib_mem_set_heap (oldheap);
120 }
121
Dave Barachc3799992016-08-15 11:12:27 -0400122 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123 * working_copies are per-cpu so that near-simultaneous
124 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400125 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200127 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400128 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700129
130 h->saved_bucket.as_u64 = b->as_u64;
131 oldheap = clib_mem_set_heap (h->mheap);
132
Dave Barachba7ddfe2017-05-17 20:20:50 -0400133 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700134 {
Dave Barachba7ddfe2017-05-17 20:20:50 -0400135 if (working_copy)
136 clib_mem_free (working_copy);
137
138 working_copy = clib_mem_alloc_aligned
139 (sizeof (working_copy[0]) * (1 << b->log2_pages),
140 CLIB_CACHE_LINE_BYTES);
141 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200142 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143 }
144
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145 clib_mem_set_heap (oldheap);
146
Dave Barach858c06f2017-07-21 10:44:27 -0400147 /* Lock the bucket... */
148 while (BV (clib_bihash_lock_bucket) (b) == 0)
149 ;
Dave Barach908a5ea2017-07-14 12:42:21 -0400150
Dave Barachc3799992016-08-15 11:12:27 -0400151 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700152
Dave Barachc3799992016-08-15 11:12:27 -0400153 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400155 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
156 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200158 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159}
160
Dave Barachc3799992016-08-15 11:12:27 -0400161static
162BVT (clib_bihash_value) *
163BV (split_and_rehash)
164 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400165 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
166 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167{
Dave Barach5e6b9582016-12-12 15:37:29 -0500168 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400169 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700170
Dave Barachc3799992016-08-15 11:12:27 -0400171 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400172 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173
Dave Barachba7ddfe2017-05-17 20:20:50 -0400174 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700175 {
176 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400177
Dave Barach5e6b9582016-12-12 15:37:29 -0500178 /* Entry not in use? Forget it */
179 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
180 continue;
181
182 /* rehash the item onto its new home-page */
183 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
184 new_hash >>= h->log2_nbuckets;
185 new_hash &= (1 << new_log2_pages) - 1;
186 new_v = &new_values[new_hash];
187
188 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700189 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400190 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500191 /* Empty slot */
192 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400193 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500194 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
195 sizeof (new_v->kvp[j]));
196 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400197 }
Dave Barachc3799992016-08-15 11:12:27 -0400198 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500199 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400200 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500201 return 0;
202 doublebreak:;
203 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400204
Dave Barach5e6b9582016-12-12 15:37:29 -0500205 return new_values;
206}
207
208static
209BVT (clib_bihash_value) *
210BV (split_and_rehash_linear)
211 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400212 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
213 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500214{
215 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400216 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500217
218 new_values = BV (value_alloc) (h, new_log2_pages);
219 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400220 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500221
222 j = 0;
223 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400224 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500225 {
226 /* Find a free slot in the new linear scan bucket */
227 for (; j < new_length; j++)
228 {
Dave Barach8f544962017-01-18 10:23:22 -0500229 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500230 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
231 goto doublebreak;
232
233 /* New value should never be in use */
234 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
235 {
236 /* Copy the old value and move along */
237 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
238 sizeof (new_values->kvp[j]));
239 j++;
240 goto doublebreak;
241 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500242 }
Dave Barach8f544962017-01-18 10:23:22 -0500243 /* This should never happen... */
244 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400245 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500246 return 0;
247
Dave Barach5e6b9582016-12-12 15:37:29 -0500248 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700249 }
250 return new_values;
251}
252
Dave Barachc3799992016-08-15 11:12:27 -0400253int BV (clib_bihash_add_del)
254 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700255{
256 u32 bucket_index;
Dave Barach908a5ea2017-07-14 12:42:21 -0400257 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400258 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700259 int rv = 0;
Dave Barach5e6b9582016-12-12 15:37:29 -0500260 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700261 u64 hash, new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400262 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200263 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500264 int mark_bucket_linear;
265 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700266
Dave Barachc3799992016-08-15 11:12:27 -0400267 hash = BV (clib_bihash_hash) (add_v);
268
269 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270 b = &h->buckets[bucket_index];
271
272 hash >>= h->log2_nbuckets;
273
Marco Varlese47366b12017-06-05 17:59:24 +0200274 tmp_b.linear_search = 0;
275
Ed Warnickecb9cada2015-12-08 15:45:58 -0700276 while (__sync_lock_test_and_set (h->writer_lock, 1))
Dave Barachc3799992016-08-15 11:12:27 -0400277 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700278
279 /* First elt in the bucket? */
280 if (b->offset == 0)
281 {
282 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400283 {
284 rv = -1;
285 goto unlock;
286 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700287
Dave Barachc3799992016-08-15 11:12:27 -0400288 v = BV (value_alloc) (h, 0);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400289
Dave Barachc3799992016-08-15 11:12:27 -0400290 *v->kvp = *add_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700291 tmp_b.as_u64 = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400292 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Dave Barache7d212f2018-02-07 13:14:06 -0500293 tmp_b.refcnt = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700294
295 b->as_u64 = tmp_b.as_u64;
296 goto unlock;
297 }
298
Dave Barach908a5ea2017-07-14 12:42:21 -0400299 /* Note: this leaves the cache disabled */
Dave Barachc3799992016-08-15 11:12:27 -0400300 BV (make_working_copy) (h, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700301
Dave Barachc3799992016-08-15 11:12:27 -0400302 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500303
304 limit = BIHASH_KVP_PER_PAGE;
305 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
306 if (b->linear_search)
307 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400308
Ed Warnickecb9cada2015-12-08 15:45:58 -0700309 if (is_add)
310 {
Dave Barachc3799992016-08-15 11:12:27 -0400311 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700312 * For obvious (in hindsight) reasons, see if we're supposed to
313 * replace an existing key, then look for an empty slot.
314 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500315 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400316 {
317 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
318 {
319 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
320 CLIB_MEMORY_BARRIER ();
321 /* Restore the previous (k,v) pairs */
322 b->as_u64 = h->saved_bucket.as_u64;
323 goto unlock;
324 }
325 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500326 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400327 {
328 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
329 {
330 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
331 CLIB_MEMORY_BARRIER ();
332 b->as_u64 = h->saved_bucket.as_u64;
Dave Barache7d212f2018-02-07 13:14:06 -0500333 b->refcnt++;
Dave Barachc3799992016-08-15 11:12:27 -0400334 goto unlock;
335 }
336 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700337 /* no room at the inn... split case... */
338 }
339 else
340 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500341 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400342 {
343 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
344 {
345 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
346 CLIB_MEMORY_BARRIER ();
Dave Barache7d212f2018-02-07 13:14:06 -0500347 if (PREDICT_TRUE (h->saved_bucket.refcnt > 1))
348 {
349 h->saved_bucket.refcnt -= 1;
350 b->as_u64 = h->saved_bucket.as_u64;
351 goto unlock;
352 }
353 else
354 {
355 tmp_b.as_u64 = 0;
356 goto free_old_bucket;
357 }
Dave Barachc3799992016-08-15 11:12:27 -0400358 }
359 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360 rv = -3;
361 b->as_u64 = h->saved_bucket.as_u64;
362 goto unlock;
363 }
364
Dave Barachba7ddfe2017-05-17 20:20:50 -0400365 old_log2_pages = h->saved_bucket.log2_pages;
366 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500367 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Damjan Marionf55f9b82017-05-10 21:06:28 +0200369 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500370 resplit_once = 0;
371
Dave Barachba7ddfe2017-05-17 20:20:50 -0400372 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
373 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700374 if (new_v == 0)
375 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500376 try_resplit:
377 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700378 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500379 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400380 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
381 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500382 if (new_v == 0)
383 {
384 mark_linear:
385 new_log2_pages--;
386 /* pinned collisions, use linear search */
387 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400388 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
389 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500390 mark_bucket_linear = 1;
391 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 }
393
394 /* Try to add the new entry */
395 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400396 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500397 limit = BIHASH_KVP_PER_PAGE;
398 if (mark_bucket_linear)
399 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500401 new_hash &= (1 << new_log2_pages) - 1;
402 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400403
Dave Barach5e6b9582016-12-12 15:37:29 -0500404 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 {
Dave Barachc3799992016-08-15 11:12:27 -0400406 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
407 {
408 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
409 goto expand_ok;
410 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700411 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400412
Ed Warnickecb9cada2015-12-08 15:45:58 -0700413 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400414 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500415 /*
416 * If we've already doubled the size of the bucket once,
417 * fall back to linear search now.
418 */
419 if (resplit_once)
420 goto mark_linear;
421 else
422 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Dave Barachc3799992016-08-15 11:12:27 -0400424expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500425 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400426 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500427 tmp_b.linear_search = mark_bucket_linear;
Dave Barache7d212f2018-02-07 13:14:06 -0500428 tmp_b.refcnt = h->saved_bucket.refcnt + 1;
429
430free_old_bucket:
Dave Barachba7ddfe2017-05-17 20:20:50 -0400431
Dave Barachc3799992016-08-15 11:12:27 -0400432 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 b->as_u64 = tmp_b.as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400434 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barache7d212f2018-02-07 13:14:06 -0500435
436 BV (value_free) (h, v, h->saved_bucket.log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700437
Dave Barachc3799992016-08-15 11:12:27 -0400438unlock:
Dave Barach908a5ea2017-07-14 12:42:21 -0400439 BV (clib_bihash_reset_cache) (b);
Dave Barach858c06f2017-07-21 10:44:27 -0400440 BV (clib_bihash_unlock_bucket) (b);
Dave Barachc3799992016-08-15 11:12:27 -0400441 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700442 h->writer_lock[0] = 0;
443 return rv;
444}
445
Dave Barachc3799992016-08-15 11:12:27 -0400446int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -0400447 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400448 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449{
450 u64 hash;
451 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400452 BVT (clib_bihash_value) * v;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400453#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400454 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400455#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400456 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500457 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700458
Dave Barachc3799992016-08-15 11:12:27 -0400459 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700460
Dave Barachc3799992016-08-15 11:12:27 -0400461 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462
Dave Barachc3799992016-08-15 11:12:27 -0400463 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700464 b = &h->buckets[bucket_index];
465
466 if (b->offset == 0)
467 return -1;
468
Dave Barachc8ef08a2017-08-31 05:58:22 -0400469#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400470 /* Check the cache, if currently enabled */
Dave Barach858c06f2017-07-21 10:44:27 -0400471 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400472 {
473 limit = BIHASH_KVP_CACHE_SIZE;
474 kvp = b->cache;
475 for (i = 0; i < limit; i++)
476 {
477 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
478 {
479 *valuep = kvp[i];
480 h->cache_hits++;
481 return 0;
482 }
483 }
484 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400485#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400486
Ed Warnickecb9cada2015-12-08 15:45:58 -0700487 hash >>= h->log2_nbuckets;
488
Dave Barachc3799992016-08-15 11:12:27 -0400489 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500490 limit = BIHASH_KVP_PER_PAGE;
491 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
492 if (PREDICT_FALSE (b->linear_search))
493 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400494
Dave Barach5e6b9582016-12-12 15:37:29 -0500495 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700496 {
Dave Barachc3799992016-08-15 11:12:27 -0400497 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
498 {
499 *valuep = v->kvp[i];
Dave Barach908a5ea2017-07-14 12:42:21 -0400500
Dave Barachc8ef08a2017-08-31 05:58:22 -0400501#if BIHASH_KVP_CACHE_SIZE > 0
502 u8 cache_slot;
Dave Barach908a5ea2017-07-14 12:42:21 -0400503 /* Shut off the cache */
Dave Barach858c06f2017-07-21 10:44:27 -0400504 if (BV (clib_bihash_lock_bucket) (b))
505 {
506 cache_slot = BV (clib_bihash_get_lru) (b);
507 b->cache[cache_slot] = v->kvp[i];
508 BV (clib_bihash_update_lru) (b, cache_slot);
Dave Barach908a5ea2017-07-14 12:42:21 -0400509
Dave Barach858c06f2017-07-21 10:44:27 -0400510 /* Reenable the cache */
511 BV (clib_bihash_unlock_bucket) (b);
512 h->cache_misses++;
513 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400514#endif
Dave Barachc3799992016-08-15 11:12:27 -0400515 return 0;
516 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 }
518 return -1;
519}
520
Dave Barach908a5ea2017-07-14 12:42:21 -0400521u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
522{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400523#if BIHASH_KVP_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400524 int i;
525 BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
526 u16 cache_lru = b->cache_lru;
527
528 s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
529
530 for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
531 s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
Chris Luke2951d902017-09-05 11:59:55 -0400532
533 return (s);
Dave Barachc8ef08a2017-08-31 05:58:22 -0400534#else
535 return format (s, "cache not configured");
536#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400537}
538
539void
540BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
541{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400542#if BIHASH_KVP_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400543 BV (clib_bihash_update_lru) (b, slot);
Dave Barachc8ef08a2017-08-31 05:58:22 -0400544#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400545}
546
Dave Barachc3799992016-08-15 11:12:27 -0400547u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548{
Dave Barachc3799992016-08-15 11:12:27 -0400549 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -0400551 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400552 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700553 int i, j, k;
554 u64 active_elements = 0;
Dave Barache7d212f2018-02-07 13:14:06 -0500555 u64 active_buckets = 0;
556 u64 linear_buckets = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700557
558 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400559
Ed Warnickecb9cada2015-12-08 15:45:58 -0700560 for (i = 0; i < h->nbuckets; i++)
561 {
Dave Barachc3799992016-08-15 11:12:27 -0400562 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700563 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400564 {
565 if (verbose > 1)
566 s = format (s, "[%d]: empty\n", i);
567 continue;
568 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569
Dave Barache7d212f2018-02-07 13:14:06 -0500570 active_buckets++;
571
572 if (b->linear_search)
573 linear_buckets++;
574
Ed Warnickecb9cada2015-12-08 15:45:58 -0700575 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400576 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500577 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
578 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400579 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700580
Dave Barachc3799992016-08-15 11:12:27 -0400581 v = BV (clib_bihash_get_value) (h, b->offset);
582 for (j = 0; j < (1 << b->log2_pages); j++)
583 {
584 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
585 {
586 if (BV (clib_bihash_is_free) (&v->kvp[k]))
587 {
588 if (verbose > 1)
589 s = format (s, " %d: empty\n",
590 j * BIHASH_KVP_PER_PAGE + k);
591 continue;
592 }
593 if (verbose)
594 {
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800595 if (h->fmt_fn)
596 {
597 s = format (s, " %d: %U\n",
598 j * BIHASH_KVP_PER_PAGE + k,
599 h->fmt_fn, &(v->kvp[k]));
600 }
601 else
602 {
603 s = format (s, " %d: %U\n",
604 j * BIHASH_KVP_PER_PAGE + k,
605 BV (format_bihash_kvp), &(v->kvp[k]));
606 }
Dave Barachc3799992016-08-15 11:12:27 -0400607 }
608 active_elements++;
609 }
610 v++;
611 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612 }
613
Dave Barache7d212f2018-02-07 13:14:06 -0500614 s = format (s, " %lld active elements %lld active buckets\n",
615 active_elements, active_buckets);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700616 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barache7d212f2018-02-07 13:14:06 -0500617
618 for (i = 0; i < vec_len (h->freelists); i++)
619 {
620 u32 nfree = 0;
621 BVT (clib_bihash_value) * free_elt;
622
623 free_elt = h->freelists[i];
624 while (free_elt)
625 {
626 nfree++;
627 free_elt = free_elt->next_free;
628 }
629
630 s = format (s, " [len %d] %u free elts\n", 1 << i, nfree);
631 }
632
633 s = format (s, " %lld linear search buckets\n", linear_buckets);
Dave Barach908a5ea2017-07-14 12:42:21 -0400634 s = format (s, " %lld cache hits, %lld cache misses\n",
635 h->cache_hits, h->cache_misses);
Dave Barache7d212f2018-02-07 13:14:06 -0500636 if (h->mheap)
637 s = format (s, " mheap: %U", format_mheap, h->mheap, 0 /* verbose */ );
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638 return s;
639}
640
Dave Barachc3799992016-08-15 11:12:27 -0400641void BV (clib_bihash_foreach_key_value_pair)
642 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643{
644 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -0400645 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400646 BVT (clib_bihash_value) * v;
647 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
648
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649 for (i = 0; i < h->nbuckets; i++)
650 {
Dave Barachc3799992016-08-15 11:12:27 -0400651 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700652 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400653 continue;
654
655 v = BV (clib_bihash_get_value) (h, b->offset);
656 for (j = 0; j < (1 << b->log2_pages); j++)
657 {
658 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
659 {
660 if (BV (clib_bihash_is_free) (&v->kvp[k]))
661 continue;
662
663 (*fp) (&v->kvp[k], arg);
664 }
665 v++;
666 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700667 }
668}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400669
Chris Luke16bcf7d2016-09-01 14:31:46 -0400670/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400671
672/*
673 * fd.io coding-style-patch-verification: ON
674 *
675 * Local Variables:
676 * eval: (c-set-style "gnu")
677 * End:
678 */