blob: 56c410b5a51eda2da00f3f765beb141fe3fe8c74 [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);
44}
45
Dave Barachc3799992016-08-15 11:12:27 -040046void BV (clib_bihash_free) (BVT (clib_bihash) * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -070047{
Dave Barachc3799992016-08-15 11:12:27 -040048 mheap_free (h->mheap);
49 memset (h, 0, sizeof (*h));
Ed Warnickecb9cada2015-12-08 15:45:58 -070050}
51
Dave Barachc3799992016-08-15 11:12:27 -040052static
53BVT (clib_bihash_value) *
54BV (value_alloc) (BVT (clib_bihash) * h, u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070055{
Dave Barachc3799992016-08-15 11:12:27 -040056 BVT (clib_bihash_value) * rv = 0;
57 void *oldheap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070058
Dave Barachc3799992016-08-15 11:12:27 -040059 ASSERT (h->writer_lock[0]);
60 if (log2_pages >= vec_len (h->freelists) || h->freelists[log2_pages] == 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -070061 {
Dave Barachc3799992016-08-15 11:12:27 -040062 oldheap = clib_mem_set_heap (h->mheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
Dave Barachc3799992016-08-15 11:12:27 -040064 vec_validate (h->freelists, log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -040065 rv = clib_mem_alloc_aligned ((sizeof (*rv) * (1 << log2_pages)),
66 CLIB_CACHE_LINE_BYTES);
Dave Barachc3799992016-08-15 11:12:27 -040067 clib_mem_set_heap (oldheap);
68 goto initialize;
Ed Warnickecb9cada2015-12-08 15:45:58 -070069 }
Dave Barachc3799992016-08-15 11:12:27 -040070 rv = h->freelists[log2_pages];
71 h->freelists[log2_pages] = rv->next_free;
Ed Warnickecb9cada2015-12-08 15:45:58 -070072
Dave Barachc3799992016-08-15 11:12:27 -040073initialize:
74 ASSERT (rv);
Dave Barachc3799992016-08-15 11:12:27 -040075 /*
76 * Latest gcc complains that the length arg is zero
77 * if we replace (1<<log2_pages) with vec_len(rv).
78 * No clue.
79 */
80 memset (rv, 0xff, sizeof (*rv) * (1 << log2_pages));
81 return rv;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082}
83
84static void
Dave Barachba7ddfe2017-05-17 20:20:50 -040085BV (value_free) (BVT (clib_bihash) * h, BVT (clib_bihash_value) * v,
86 u32 log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -070087{
Dave Barachc3799992016-08-15 11:12:27 -040088 ASSERT (h->writer_lock[0]);
Ed Warnickecb9cada2015-12-08 15:45:58 -070089
Dave Barachc3799992016-08-15 11:12:27 -040090 ASSERT (vec_len (h->freelists) > log2_pages);
91
92 v->next_free = h->freelists[log2_pages];
93 h->freelists[log2_pages] = v;
Ed Warnickecb9cada2015-12-08 15:45:58 -070094}
95
96static inline void
Dave Barach908a5ea2017-07-14 12:42:21 -040097BV (make_working_copy) (BVT (clib_bihash) * h, BVT (clib_bihash_bucket) * b)
Ed Warnickecb9cada2015-12-08 15:45:58 -070098{
Dave Barachc3799992016-08-15 11:12:27 -040099 BVT (clib_bihash_value) * v;
Dave Barach908a5ea2017-07-14 12:42:21 -0400100 BVT (clib_bihash_bucket) working_bucket __attribute__ ((aligned (8)));
Dave Barachc3799992016-08-15 11:12:27 -0400101 void *oldheap;
102 BVT (clib_bihash_value) * working_copy;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200103 u32 thread_index = os_get_thread_index ();
Dave Barachba7ddfe2017-05-17 20:20:50 -0400104 int log2_working_copy_length;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700105
Damjan Marionf55f9b82017-05-10 21:06:28 +0200106 if (thread_index >= vec_len (h->working_copies))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700107 {
108 oldheap = clib_mem_set_heap (h->mheap);
Damjan Marionf55f9b82017-05-10 21:06:28 +0200109 vec_validate (h->working_copies, thread_index);
Steve Shin871cdec2017-06-02 10:09:02 -0700110 vec_validate_init_empty (h->working_copy_lengths, thread_index, ~0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111 clib_mem_set_heap (oldheap);
112 }
113
Dave Barachc3799992016-08-15 11:12:27 -0400114 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 * working_copies are per-cpu so that near-simultaneous
116 * updates from multiple threads will not result in sporadic, spurious
Dave Barachc3799992016-08-15 11:12:27 -0400117 * lookup failures.
Ed Warnickecb9cada2015-12-08 15:45:58 -0700118 */
Damjan Marionf55f9b82017-05-10 21:06:28 +0200119 working_copy = h->working_copies[thread_index];
Dave Barachba7ddfe2017-05-17 20:20:50 -0400120 log2_working_copy_length = h->working_copy_lengths[thread_index];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121
122 h->saved_bucket.as_u64 = b->as_u64;
123 oldheap = clib_mem_set_heap (h->mheap);
124
Dave Barachba7ddfe2017-05-17 20:20:50 -0400125 if (b->log2_pages > log2_working_copy_length)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700126 {
Dave Barachba7ddfe2017-05-17 20:20:50 -0400127 if (working_copy)
128 clib_mem_free (working_copy);
129
130 working_copy = clib_mem_alloc_aligned
131 (sizeof (working_copy[0]) * (1 << b->log2_pages),
132 CLIB_CACHE_LINE_BYTES);
133 h->working_copy_lengths[thread_index] = b->log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200134 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135 }
136
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137 clib_mem_set_heap (oldheap);
138
Dave Barach858c06f2017-07-21 10:44:27 -0400139 /* Lock the bucket... */
140 while (BV (clib_bihash_lock_bucket) (b) == 0)
141 ;
Dave Barach908a5ea2017-07-14 12:42:21 -0400142
Dave Barachc3799992016-08-15 11:12:27 -0400143 v = BV (clib_bihash_get_value) (h, b->offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700144
Dave Barachc3799992016-08-15 11:12:27 -0400145 clib_memcpy (working_copy, v, sizeof (*v) * (1 << b->log2_pages));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700146 working_bucket.as_u64 = b->as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400147 working_bucket.offset = BV (clib_bihash_get_offset) (h, working_copy);
148 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700149 b->as_u64 = working_bucket.as_u64;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200150 h->working_copies[thread_index] = working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700151}
152
Dave Barachc3799992016-08-15 11:12:27 -0400153static
154BVT (clib_bihash_value) *
155BV (split_and_rehash)
156 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400157 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
158 u32 new_log2_pages)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159{
Dave Barach5e6b9582016-12-12 15:37:29 -0500160 BVT (clib_bihash_value) * new_values, *new_v;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400161 int i, j, length_in_kvs;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700162
Dave Barachc3799992016-08-15 11:12:27 -0400163 new_values = BV (value_alloc) (h, new_log2_pages);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400164 length_in_kvs = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700165
Dave Barachba7ddfe2017-05-17 20:20:50 -0400166 for (i = 0; i < length_in_kvs; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167 {
168 u64 new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400169
Dave Barach5e6b9582016-12-12 15:37:29 -0500170 /* Entry not in use? Forget it */
171 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
172 continue;
173
174 /* rehash the item onto its new home-page */
175 new_hash = BV (clib_bihash_hash) (&(old_values->kvp[i]));
176 new_hash >>= h->log2_nbuckets;
177 new_hash &= (1 << new_log2_pages) - 1;
178 new_v = &new_values[new_hash];
179
180 /* Across the new home-page */
Ed Warnickecb9cada2015-12-08 15:45:58 -0700181 for (j = 0; j < BIHASH_KVP_PER_PAGE; j++)
Dave Barachc3799992016-08-15 11:12:27 -0400182 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500183 /* Empty slot */
184 if (BV (clib_bihash_is_free) (&(new_v->kvp[j])))
Dave Barachc3799992016-08-15 11:12:27 -0400185 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500186 clib_memcpy (&(new_v->kvp[j]), &(old_values->kvp[i]),
187 sizeof (new_v->kvp[j]));
188 goto doublebreak;
Dave Barachc3799992016-08-15 11:12:27 -0400189 }
Dave Barachc3799992016-08-15 11:12:27 -0400190 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500191 /* Crap. Tell caller to try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400192 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500193 return 0;
194 doublebreak:;
195 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400196
Dave Barach5e6b9582016-12-12 15:37:29 -0500197 return new_values;
198}
199
200static
201BVT (clib_bihash_value) *
202BV (split_and_rehash_linear)
203 (BVT (clib_bihash) * h,
Dave Barachba7ddfe2017-05-17 20:20:50 -0400204 BVT (clib_bihash_value) * old_values, u32 old_log2_pages,
205 u32 new_log2_pages)
Dave Barach5e6b9582016-12-12 15:37:29 -0500206{
207 BVT (clib_bihash_value) * new_values;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400208 int i, j, new_length, old_length;
Dave Barach5e6b9582016-12-12 15:37:29 -0500209
210 new_values = BV (value_alloc) (h, new_log2_pages);
211 new_length = (1 << new_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400212 old_length = (1 << old_log2_pages) * BIHASH_KVP_PER_PAGE;
Dave Barach5e6b9582016-12-12 15:37:29 -0500213
214 j = 0;
215 /* Across the old value array */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400216 for (i = 0; i < old_length; i++)
Dave Barach5e6b9582016-12-12 15:37:29 -0500217 {
218 /* Find a free slot in the new linear scan bucket */
219 for (; j < new_length; j++)
220 {
Dave Barach8f544962017-01-18 10:23:22 -0500221 /* Old value not in use? Forget it. */
Dave Barach5e6b9582016-12-12 15:37:29 -0500222 if (BV (clib_bihash_is_free) (&(old_values->kvp[i])))
223 goto doublebreak;
224
225 /* New value should never be in use */
226 if (BV (clib_bihash_is_free) (&(new_values->kvp[j])))
227 {
228 /* Copy the old value and move along */
229 clib_memcpy (&(new_values->kvp[j]), &(old_values->kvp[i]),
230 sizeof (new_values->kvp[j]));
231 j++;
232 goto doublebreak;
233 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500234 }
Dave Barach8f544962017-01-18 10:23:22 -0500235 /* This should never happen... */
236 clib_warning ("BUG: linear rehash failed!");
Dave Barachba7ddfe2017-05-17 20:20:50 -0400237 BV (value_free) (h, new_values, new_log2_pages);
Dave Barach8f544962017-01-18 10:23:22 -0500238 return 0;
239
Dave Barach5e6b9582016-12-12 15:37:29 -0500240 doublebreak:;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700241 }
242 return new_values;
243}
244
Dave Barachc3799992016-08-15 11:12:27 -0400245int BV (clib_bihash_add_del)
246 (BVT (clib_bihash) * h, BVT (clib_bihash_kv) * add_v, int is_add)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700247{
248 u32 bucket_index;
Dave Barach908a5ea2017-07-14 12:42:21 -0400249 BVT (clib_bihash_bucket) * b, tmp_b;
Dave Barachc3799992016-08-15 11:12:27 -0400250 BVT (clib_bihash_value) * v, *new_v, *save_new_v, *working_copy;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700251 int rv = 0;
Dave Barach5e6b9582016-12-12 15:37:29 -0500252 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700253 u64 hash, new_hash;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400254 u32 new_log2_pages, old_log2_pages;
Damjan Marionf55f9b82017-05-10 21:06:28 +0200255 u32 thread_index = os_get_thread_index ();
Dave Barach5e6b9582016-12-12 15:37:29 -0500256 int mark_bucket_linear;
257 int resplit_once;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700258
Dave Barachc3799992016-08-15 11:12:27 -0400259 hash = BV (clib_bihash_hash) (add_v);
260
261 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700262 b = &h->buckets[bucket_index];
263
264 hash >>= h->log2_nbuckets;
265
Marco Varlese47366b12017-06-05 17:59:24 +0200266 tmp_b.linear_search = 0;
267
Ed Warnickecb9cada2015-12-08 15:45:58 -0700268 while (__sync_lock_test_and_set (h->writer_lock, 1))
Dave Barachc3799992016-08-15 11:12:27 -0400269 ;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700270
271 /* First elt in the bucket? */
272 if (b->offset == 0)
273 {
274 if (is_add == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400275 {
276 rv = -1;
277 goto unlock;
278 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700279
Dave Barachc3799992016-08-15 11:12:27 -0400280 v = BV (value_alloc) (h, 0);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400281
Dave Barachc3799992016-08-15 11:12:27 -0400282 *v->kvp = *add_v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700283 tmp_b.as_u64 = 0;
Dave Barachc3799992016-08-15 11:12:27 -0400284 tmp_b.offset = BV (clib_bihash_get_offset) (h, v);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700285
286 b->as_u64 = tmp_b.as_u64;
287 goto unlock;
288 }
289
Dave Barach908a5ea2017-07-14 12:42:21 -0400290 /* Note: this leaves the cache disabled */
Dave Barachc3799992016-08-15 11:12:27 -0400291 BV (make_working_copy) (h, b);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700292
Dave Barachc3799992016-08-15 11:12:27 -0400293 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500294
295 limit = BIHASH_KVP_PER_PAGE;
296 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
297 if (b->linear_search)
298 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400299
Ed Warnickecb9cada2015-12-08 15:45:58 -0700300 if (is_add)
301 {
Dave Barachc3799992016-08-15 11:12:27 -0400302 /*
Ed Warnickecb9cada2015-12-08 15:45:58 -0700303 * For obvious (in hindsight) reasons, see if we're supposed to
304 * replace an existing key, then look for an empty slot.
305 */
Dave Barach5e6b9582016-12-12 15:37:29 -0500306 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400307 {
308 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
309 {
310 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
311 CLIB_MEMORY_BARRIER ();
312 /* Restore the previous (k,v) pairs */
313 b->as_u64 = h->saved_bucket.as_u64;
314 goto unlock;
315 }
316 }
Dave Barach5e6b9582016-12-12 15:37:29 -0500317 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400318 {
319 if (BV (clib_bihash_is_free) (&(v->kvp[i])))
320 {
321 clib_memcpy (&(v->kvp[i]), add_v, sizeof (*add_v));
322 CLIB_MEMORY_BARRIER ();
323 b->as_u64 = h->saved_bucket.as_u64;
324 goto unlock;
325 }
326 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700327 /* no room at the inn... split case... */
328 }
329 else
330 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500331 for (i = 0; i < limit; i++)
Dave Barachc3799992016-08-15 11:12:27 -0400332 {
333 if (!memcmp (&(v->kvp[i]), &add_v->key, sizeof (add_v->key)))
334 {
335 memset (&(v->kvp[i]), 0xff, sizeof (*(add_v)));
336 CLIB_MEMORY_BARRIER ();
337 b->as_u64 = h->saved_bucket.as_u64;
338 goto unlock;
339 }
340 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700341 rv = -3;
342 b->as_u64 = h->saved_bucket.as_u64;
343 goto unlock;
344 }
345
Dave Barachba7ddfe2017-05-17 20:20:50 -0400346 old_log2_pages = h->saved_bucket.log2_pages;
347 new_log2_pages = old_log2_pages + 1;
Dave Barach5e6b9582016-12-12 15:37:29 -0500348 mark_bucket_linear = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700349
Damjan Marionf55f9b82017-05-10 21:06:28 +0200350 working_copy = h->working_copies[thread_index];
Dave Barach5e6b9582016-12-12 15:37:29 -0500351 resplit_once = 0;
352
Dave Barachba7ddfe2017-05-17 20:20:50 -0400353 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
354 new_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700355 if (new_v == 0)
356 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500357 try_resplit:
358 resplit_once = 1;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700359 new_log2_pages++;
Dave Barach5e6b9582016-12-12 15:37:29 -0500360 /* Try re-splitting. If that fails, fall back to linear search */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400361 new_v = BV (split_and_rehash) (h, working_copy, old_log2_pages,
362 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500363 if (new_v == 0)
364 {
365 mark_linear:
366 new_log2_pages--;
367 /* pinned collisions, use linear search */
368 new_v =
Dave Barachba7ddfe2017-05-17 20:20:50 -0400369 BV (split_and_rehash_linear) (h, working_copy, old_log2_pages,
370 new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500371 mark_bucket_linear = 1;
372 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700373 }
374
375 /* Try to add the new entry */
376 save_new_v = new_v;
Dave Barachc3799992016-08-15 11:12:27 -0400377 new_hash = BV (clib_bihash_hash) (add_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500378 limit = BIHASH_KVP_PER_PAGE;
379 if (mark_bucket_linear)
380 limit <<= new_log2_pages;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700381 new_hash >>= h->log2_nbuckets;
Dave Barach5e6b9582016-12-12 15:37:29 -0500382 new_hash &= (1 << new_log2_pages) - 1;
383 new_v += mark_bucket_linear ? 0 : new_hash;
Dave Barachc3799992016-08-15 11:12:27 -0400384
Dave Barach5e6b9582016-12-12 15:37:29 -0500385 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700386 {
Dave Barachc3799992016-08-15 11:12:27 -0400387 if (BV (clib_bihash_is_free) (&(new_v->kvp[i])))
388 {
389 clib_memcpy (&(new_v->kvp[i]), add_v, sizeof (*add_v));
390 goto expand_ok;
391 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700392 }
Dave Barachba7ddfe2017-05-17 20:20:50 -0400393
Ed Warnickecb9cada2015-12-08 15:45:58 -0700394 /* Crap. Try again */
Dave Barachba7ddfe2017-05-17 20:20:50 -0400395 BV (value_free) (h, save_new_v, new_log2_pages);
Dave Barach5e6b9582016-12-12 15:37:29 -0500396 /*
397 * If we've already doubled the size of the bucket once,
398 * fall back to linear search now.
399 */
400 if (resplit_once)
401 goto mark_linear;
402 else
403 goto try_resplit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700404
Dave Barachc3799992016-08-15 11:12:27 -0400405expand_ok:
Dave Barach5e6b9582016-12-12 15:37:29 -0500406 /* Keep track of the number of linear-scan buckets */
407 if (tmp_b.linear_search ^ mark_bucket_linear)
408 h->linear_buckets += (mark_bucket_linear == 1) ? 1 : -1;
409
410 tmp_b.log2_pages = new_log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400411 tmp_b.offset = BV (clib_bihash_get_offset) (h, save_new_v);
Dave Barach5e6b9582016-12-12 15:37:29 -0500412 tmp_b.linear_search = mark_bucket_linear;
Dave Barachba7ddfe2017-05-17 20:20:50 -0400413
Dave Barachc3799992016-08-15 11:12:27 -0400414 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700415 b->as_u64 = tmp_b.as_u64;
Dave Barachc3799992016-08-15 11:12:27 -0400416 v = BV (clib_bihash_get_value) (h, h->saved_bucket.offset);
Dave Barachba7ddfe2017-05-17 20:20:50 -0400417 BV (value_free) (h, v, old_log2_pages);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700418
Dave Barachc3799992016-08-15 11:12:27 -0400419unlock:
Dave Barach908a5ea2017-07-14 12:42:21 -0400420 BV (clib_bihash_reset_cache) (b);
Dave Barach858c06f2017-07-21 10:44:27 -0400421 BV (clib_bihash_unlock_bucket) (b);
Dave Barachc3799992016-08-15 11:12:27 -0400422 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423 h->writer_lock[0] = 0;
424 return rv;
425}
426
Dave Barachc3799992016-08-15 11:12:27 -0400427int BV (clib_bihash_search)
Dave Barach908a5ea2017-07-14 12:42:21 -0400428 (BVT (clib_bihash) * h,
Dave Barachc3799992016-08-15 11:12:27 -0400429 BVT (clib_bihash_kv) * search_key, BVT (clib_bihash_kv) * valuep)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700430{
431 u64 hash;
432 u32 bucket_index;
Dave Barachc3799992016-08-15 11:12:27 -0400433 BVT (clib_bihash_value) * v;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400434#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400435 BVT (clib_bihash_kv) * kvp;
Dave Barachc8ef08a2017-08-31 05:58:22 -0400436#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400437 BVT (clib_bihash_bucket) * b;
Dave Barach5e6b9582016-12-12 15:37:29 -0500438 int i, limit;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700439
Dave Barachc3799992016-08-15 11:12:27 -0400440 ASSERT (valuep);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barachc3799992016-08-15 11:12:27 -0400442 hash = BV (clib_bihash_hash) (search_key);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443
Dave Barachc3799992016-08-15 11:12:27 -0400444 bucket_index = hash & (h->nbuckets - 1);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700445 b = &h->buckets[bucket_index];
446
447 if (b->offset == 0)
448 return -1;
449
Dave Barachc8ef08a2017-08-31 05:58:22 -0400450#if BIHASH_KVP_CACHE_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400451 /* Check the cache, if currently enabled */
Dave Barach858c06f2017-07-21 10:44:27 -0400452 if (PREDICT_TRUE ((b->cache_lru & (1 << 15)) == 0))
Dave Barach908a5ea2017-07-14 12:42:21 -0400453 {
454 limit = BIHASH_KVP_CACHE_SIZE;
455 kvp = b->cache;
456 for (i = 0; i < limit; i++)
457 {
458 if (BV (clib_bihash_key_compare) (kvp[i].key, search_key->key))
459 {
460 *valuep = kvp[i];
461 h->cache_hits++;
462 return 0;
463 }
464 }
465 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400466#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400467
Ed Warnickecb9cada2015-12-08 15:45:58 -0700468 hash >>= h->log2_nbuckets;
469
Dave Barachc3799992016-08-15 11:12:27 -0400470 v = BV (clib_bihash_get_value) (h, b->offset);
Dave Barach5e6b9582016-12-12 15:37:29 -0500471 limit = BIHASH_KVP_PER_PAGE;
472 v += (b->linear_search == 0) ? hash & ((1 << b->log2_pages) - 1) : 0;
473 if (PREDICT_FALSE (b->linear_search))
474 limit <<= b->log2_pages;
Dave Barachc3799992016-08-15 11:12:27 -0400475
Dave Barach5e6b9582016-12-12 15:37:29 -0500476 for (i = 0; i < limit; i++)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 {
Dave Barachc3799992016-08-15 11:12:27 -0400478 if (BV (clib_bihash_key_compare) (v->kvp[i].key, search_key->key))
479 {
480 *valuep = v->kvp[i];
Dave Barach908a5ea2017-07-14 12:42:21 -0400481
Dave Barachc8ef08a2017-08-31 05:58:22 -0400482#if BIHASH_KVP_CACHE_SIZE > 0
483 u8 cache_slot;
Dave Barach908a5ea2017-07-14 12:42:21 -0400484 /* Shut off the cache */
Dave Barach858c06f2017-07-21 10:44:27 -0400485 if (BV (clib_bihash_lock_bucket) (b))
486 {
487 cache_slot = BV (clib_bihash_get_lru) (b);
488 b->cache[cache_slot] = v->kvp[i];
489 BV (clib_bihash_update_lru) (b, cache_slot);
Dave Barach908a5ea2017-07-14 12:42:21 -0400490
Dave Barach858c06f2017-07-21 10:44:27 -0400491 /* Reenable the cache */
492 BV (clib_bihash_unlock_bucket) (b);
493 h->cache_misses++;
494 }
Dave Barachc8ef08a2017-08-31 05:58:22 -0400495#endif
Dave Barachc3799992016-08-15 11:12:27 -0400496 return 0;
497 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700498 }
499 return -1;
500}
501
Dave Barach908a5ea2017-07-14 12:42:21 -0400502u8 *BV (format_bihash_lru) (u8 * s, va_list * args)
503{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400504#if BIHASH_KVP_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400505 int i;
506 BVT (clib_bihash_bucket) * b = va_arg (*args, BVT (clib_bihash_bucket) *);
507 u16 cache_lru = b->cache_lru;
508
509 s = format (s, "cache %s, order ", cache_lru & (1 << 15) ? "on" : "off");
510
511 for (i = 0; i < BIHASH_KVP_CACHE_SIZE; i++)
512 s = format (s, "[%d] ", ((cache_lru >> (3 * i)) & 7));
Chris Luke2951d902017-09-05 11:59:55 -0400513
514 return (s);
Dave Barachc8ef08a2017-08-31 05:58:22 -0400515#else
516 return format (s, "cache not configured");
517#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400518}
519
520void
521BV (clib_bihash_update_lru_not_inline) (BVT (clib_bihash_bucket) * b, u8 slot)
522{
Dave Barachc8ef08a2017-08-31 05:58:22 -0400523#if BIHASH_KVP_SIZE > 0
Dave Barach908a5ea2017-07-14 12:42:21 -0400524 BV (clib_bihash_update_lru) (b, slot);
Dave Barachc8ef08a2017-08-31 05:58:22 -0400525#endif
Dave Barach908a5ea2017-07-14 12:42:21 -0400526}
527
Dave Barachc3799992016-08-15 11:12:27 -0400528u8 *BV (format_bihash) (u8 * s, va_list * args)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700529{
Dave Barachc3799992016-08-15 11:12:27 -0400530 BVT (clib_bihash) * h = va_arg (*args, BVT (clib_bihash) *);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531 int verbose = va_arg (*args, int);
Dave Barach908a5ea2017-07-14 12:42:21 -0400532 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400533 BVT (clib_bihash_value) * v;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700534 int i, j, k;
535 u64 active_elements = 0;
536
537 s = format (s, "Hash table %s\n", h->name ? h->name : (u8 *) "(unnamed)");
Dave Barachc3799992016-08-15 11:12:27 -0400538
Ed Warnickecb9cada2015-12-08 15:45:58 -0700539 for (i = 0; i < h->nbuckets; i++)
540 {
Dave Barachc3799992016-08-15 11:12:27 -0400541 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700542 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400543 {
544 if (verbose > 1)
545 s = format (s, "[%d]: empty\n", i);
546 continue;
547 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548
549 if (verbose)
Dave Barachc3799992016-08-15 11:12:27 -0400550 {
Dave Barach5e6b9582016-12-12 15:37:29 -0500551 s = format (s, "[%d]: heap offset %d, len %d, linear %d\n", i,
552 b->offset, (1 << b->log2_pages), b->linear_search);
Dave Barachc3799992016-08-15 11:12:27 -0400553 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700554
Dave Barachc3799992016-08-15 11:12:27 -0400555 v = BV (clib_bihash_get_value) (h, b->offset);
556 for (j = 0; j < (1 << b->log2_pages); j++)
557 {
558 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
559 {
560 if (BV (clib_bihash_is_free) (&v->kvp[k]))
561 {
562 if (verbose > 1)
563 s = format (s, " %d: empty\n",
564 j * BIHASH_KVP_PER_PAGE + k);
565 continue;
566 }
567 if (verbose)
568 {
569 s = format (s, " %d: %U\n",
570 j * BIHASH_KVP_PER_PAGE + k,
571 BV (format_bihash_kvp), &(v->kvp[k]));
572 }
573 active_elements++;
574 }
575 v++;
576 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700577 }
578
579 s = format (s, " %lld active elements\n", active_elements);
580 s = format (s, " %d free lists\n", vec_len (h->freelists));
Dave Barach5e6b9582016-12-12 15:37:29 -0500581 s = format (s, " %d linear search buckets\n", h->linear_buckets);
Dave Barach908a5ea2017-07-14 12:42:21 -0400582 s = format (s, " %lld cache hits, %lld cache misses\n",
583 h->cache_hits, h->cache_misses);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700584 return s;
585}
586
Dave Barachc3799992016-08-15 11:12:27 -0400587void BV (clib_bihash_foreach_key_value_pair)
588 (BVT (clib_bihash) * h, void *callback, void *arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700589{
590 int i, j, k;
Dave Barach908a5ea2017-07-14 12:42:21 -0400591 BVT (clib_bihash_bucket) * b;
Dave Barachc3799992016-08-15 11:12:27 -0400592 BVT (clib_bihash_value) * v;
593 void (*fp) (BVT (clib_bihash_kv) *, void *) = callback;
594
Ed Warnickecb9cada2015-12-08 15:45:58 -0700595 for (i = 0; i < h->nbuckets; i++)
596 {
Dave Barachc3799992016-08-15 11:12:27 -0400597 b = &h->buckets[i];
Ed Warnickecb9cada2015-12-08 15:45:58 -0700598 if (b->offset == 0)
Dave Barachc3799992016-08-15 11:12:27 -0400599 continue;
600
601 v = BV (clib_bihash_get_value) (h, b->offset);
602 for (j = 0; j < (1 << b->log2_pages); j++)
603 {
604 for (k = 0; k < BIHASH_KVP_PER_PAGE; k++)
605 {
606 if (BV (clib_bihash_is_free) (&v->kvp[k]))
607 continue;
608
609 (*fp) (&v->kvp[k], arg);
610 }
611 v++;
612 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700613 }
614}
Dave Barachdd3a57f2016-07-27 16:58:51 -0400615
Chris Luke16bcf7d2016-09-01 14:31:46 -0400616/** @endcond */
Dave Barachc3799992016-08-15 11:12:27 -0400617
618/*
619 * fd.io coding-style-patch-verification: ON
620 *
621 * Local Variables:
622 * eval: (c-set-style "gnu")
623 * End:
624 */