blob: 3f25b02d5dfff8b7d2a52723b9249ef92e12581b [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);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700293
294 b->as_u64 = tmp_b.as_u64;
295 goto unlock;
296 }
297
Dave Barach908a5ea2017-07-14 12:42:21 -0400298 /* Note: this leaves the cache disabled */
Dave Barachc3799992016-08-15 11:12:27 -0400299 BV (make_working_copy) (h, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300
Dave Barachc3799992016-08-15 11:12:27 -0400301 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500302
303 limit = BIHASH_KVP_PER_PAGE;
304 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
305 if (b->linear_search)
306 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400307
Ed Warnickecb9cada2015-12-08 15:45:58 -0700308 if (is_add)
309 {
Dave Barachc3799992016-08-15 11:12:27 -0400310 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700311 * For obvious (in hindsight) reasons, see if we're supposed to
312 * replace an existing key, then look for an empty slot.
313 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500314 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400315 {
316 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
317 {
318 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
319 CLIB_MEMORY_BARRIER ();
320 /* Restore the previous (k,v) pairs */
321 b->as_u64 = h->saved_bucket.as_u64;
322 goto unlock;
323 }
324 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500325 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400326 {
327 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
328 {
329 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
330 CLIB_MEMORY_BARRIER ();
331 b->as_u64 = h->saved_bucket.as_u64;
332 goto unlock;
333 }
334 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700335 /* no room at the inn... split case... */
336 }
337 else
338 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500339 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400340 {
341 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
342 {
343 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
344 CLIB_MEMORY_BARRIER ();
345 b->as_u64 = h->saved_bucket.as_u64;
346 goto unlock;
347 }
348 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349 rv = -3;
350 b->as_u64 = h->saved_bucket.as_u64;
351 goto unlock;
352 }
353
Dave Barachba7ddfe2017-05-17 20:20:50 -0400354 old_log2_pages = h->saved_bucket.log2_pages;
355 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500356 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700357
Damjan Marionf55f9b82017-05-10 21:06:28 +0200358 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500359 resplit_once = 0;
360
Dave Barachba7ddfe2017-05-17 20:20:50 -0400361 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
362 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700363 if (new_v == 0)
364 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500365 try_resplit:
366 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700367 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500368 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400369 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
370 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500371 if (new_v == 0)
372 {
373 mark_linear:
374 new_log2_pages--;
375 /* pinned collisions, use linear search */
376 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400377 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
378 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500379 mark_bucket_linear = 1;
380 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 }
382
383 /* Try to add the new entry */
384 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400385 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500386 limit = BIHASH_KVP_PER_PAGE;
387 if (mark_bucket_linear)
388 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700389 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500390 new_hash &= (1 << new_log2_pages) - 1;
391 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400392
Dave Barach5e6b9582016-12-12 15:37:29 -0500393 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 {
Dave Barachc3799992016-08-15 11:12:27 -0400395 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
396 {
397 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
398 goto expand_ok;
399 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700400 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400401
Ed Warnickecb9cada2015-12-08 15:45:58 -0700402 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400403 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500404 /*
405 * If we've already doubled the size of the bucket once,
406 * fall back to linear search now.
407 */
408 if (resplit_once)
409 goto mark_linear;
410 else
411 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700412
Dave Barachc3799992016-08-15 11:12:27 -0400413expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500414 /* Keep track of the number of linear-scan buckets */
415 if (tmp_b.linear_search ^ mark_bucket_linear)
416 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
417
418 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400419 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500420 tmp_b.linear_search = mark_bucket_linear;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400421
Dave Barachc3799992016-08-15 11:12:27 -0400422 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 b->as_u64 = tmp_b.as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400424 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400425 BV (value_free) (h, v, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Dave Barachc3799992016-08-15 11:12:27 -0400427unlock:
Dave Barach908a5ea2017-07-14 12:42:21 -0400428 BV (clib_bihash_reset_cache) (b);
Dave Barach858c06f2017-07-21 10:44:27 -0400429 BV (clib_bihash_unlock_bucket) (b);
Dave Barachc3799992016-08-15 11:12:27 -0400430 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700431 h->writer_lock[0] = 0;
432 return rv;
433}
434
Dave Barachc3799992016-08-15 11:12:27 -0400435int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -0400436 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400437 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438{
439 u64 hash;
440 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400441 BVT (clib_bihash_value) * v;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400442#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400443 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400444#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400445 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500446 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700447
Dave Barachc3799992016-08-15 11:12:27 -0400448 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
Dave Barachc3799992016-08-15 11:12:27 -0400450 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451
Dave Barachc3799992016-08-15 11:12:27 -0400452 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700453 b = &h->buckets[bucket_index];
454
455 if (b->offset == 0)
456 return -1;
457
Dave Barachc8ef08a2017-08-31 05:58:22 -0400458#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400459 /* Check the cache, if currently enabled */
Dave Barach858c06f2017-07-21 10:44:27 -0400460 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400461 {
462 limit = BIHASH_KVP_CACHE_SIZE;
463 kvp = b->cache;
464 for (i = 0; i < limit; i++)
465 {
466 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
467 {
468 *valuep = kvp[i];
469 h->cache_hits++;
470 return 0;
471 }
472 }
473 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400474#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400475
Ed Warnickecb9cada2015-12-08 15:45:58 -0700476 hash >>= h->log2_nbuckets;
477
Dave Barachc3799992016-08-15 11:12:27 -0400478 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500479 limit = BIHASH_KVP_PER_PAGE;
480 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
481 if (PREDICT_FALSE (b->linear_search))
482 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400483
Dave Barach5e6b9582016-12-12 15:37:29 -0500484 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 {
Dave Barachc3799992016-08-15 11:12:27 -0400486 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
487 {
488 *valuep = v->kvp[i];
Dave Barach908a5ea2017-07-14 12:42:21 -0400489
Dave Barachc8ef08a2017-08-31 05:58:22 -0400490#if BIHASH_KVP_CACHE_SIZE > 0
491 u8 cache_slot;
Dave Barach908a5ea2017-07-14 12:42:21 -0400492 /* Shut off the cache */
Dave Barach858c06f2017-07-21 10:44:27 -0400493 if (BV (clib_bihash_lock_bucket) (b))
494 {
495 cache_slot = BV (clib_bihash_get_lru) (b);
496 b->cache[cache_slot] = v->kvp[i];
497 BV (clib_bihash_update_lru) (b, cache_slot);
Dave Barach908a5ea2017-07-14 12:42:21 -0400498
Dave Barach858c06f2017-07-21 10:44:27 -0400499 /* Reenable the cache */
500 BV (clib_bihash_unlock_bucket) (b);
501 h->cache_misses++;
502 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400503#endif
Dave Barachc3799992016-08-15 11:12:27 -0400504 return 0;
505 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700506 }
507 return -1;
508}
509
Dave Barach908a5ea2017-07-14 12:42:21 -0400510u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
511{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400512#if BIHASH_KVP_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400513 int i;
514 BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
515 u16 cache_lru = b->cache_lru;
516
517 s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
518
519 for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
520 s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
Chris Luke2951d902017-09-05 11:59:55 -0400521
522 return (s);
Dave Barachc8ef08a2017-08-31 05:58:22 -0400523#else
524 return format (s, "cache not configured");
525#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400526}
527
528void
529BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
530{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400531#if BIHASH_KVP_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400532 BV (clib_bihash_update_lru) (b, slot);
Dave Barachc8ef08a2017-08-31 05:58:22 -0400533#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400534}
535
Dave Barachc3799992016-08-15 11:12:27 -0400536u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700537{
Dave Barachc3799992016-08-15 11:12:27 -0400538 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -0400540 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400541 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 int i, j, k;
543 u64 active_elements = 0;
544
545 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400546
Ed Warnickecb9cada2015-12-08 15:45:58 -0700547 for (i = 0; i < h->nbuckets; i++)
548 {
Dave Barachc3799992016-08-15 11:12:27 -0400549 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700550 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400551 {
552 if (verbose > 1)
553 s = format (s, "[%d]: empty\n", i);
554 continue;
555 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700556
557 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400558 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500559 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
560 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400561 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700562
Dave Barachc3799992016-08-15 11:12:27 -0400563 v = BV (clib_bihash_get_value) (h, b->offset);
564 for (j = 0; j < (1 << b->log2_pages); j++)
565 {
566 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
567 {
568 if (BV (clib_bihash_is_free) (&v->kvp[k]))
569 {
570 if (verbose > 1)
571 s = format (s, " %d: empty\n",
572 j * BIHASH_KVP_PER_PAGE + k);
573 continue;
574 }
575 if (verbose)
576 {
Vijayabhaskar Katamreddyfb8e61c2017-12-14 13:20:50 -0800577 if (h->fmt_fn)
578 {
579 s = format (s, " %d: %U\n",
580 j * BIHASH_KVP_PER_PAGE + k,
581 h->fmt_fn, &(v->kvp[k]));
582 }
583 else
584 {
585 s = format (s, " %d: %U\n",
586 j * BIHASH_KVP_PER_PAGE + k,
587 BV (format_bihash_kvp), &(v->kvp[k]));
588 }
Dave Barachc3799992016-08-15 11:12:27 -0400589 }
590 active_elements++;
591 }
592 v++;
593 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594 }
595
596 s = format (s, " %lld active elements\n", active_elements);
597 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barach5e6b9582016-12-12 15:37:29 -0500598 s = format (s, " %d linear search buckets\n", h->linear_buckets);
Dave Barach908a5ea2017-07-14 12:42:21 -0400599 s = format (s, " %lld cache hits, %lld cache misses\n",
600 h->cache_hits, h->cache_misses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700601 return s;
602}
603
Dave Barachc3799992016-08-15 11:12:27 -0400604void BV (clib_bihash_foreach_key_value_pair)
605 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700606{
607 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -0400608 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400609 BVT (clib_bihash_value) * v;
610 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
611
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612 for (i = 0; i < h->nbuckets; i++)
613 {
Dave Barachc3799992016-08-15 11:12:27 -0400614 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700615 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400616 continue;
617
618 v = BV (clib_bihash_get_value) (h, b->offset);
619 for (j = 0; j < (1 << b->log2_pages); j++)
620 {
621 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
622 {
623 if (BV (clib_bihash_is_free) (&v->kvp[k]))
624 continue;
625
626 (*fp) (&v->kvp[k], arg);
627 }
628 v++;
629 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700630 }
631}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400632
Chris Luke16bcf7d2016-09-01 14:31:46 -0400633/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400634
635/*
636 * fd.io coding-style-patch-verification: ON
637 *
638 * Local Variables:
639 * eval: (c-set-style "gnu")
640 * End:
641 */