hash: fix ring size
[dpdk.git] / lib / librte_hash / rte_cuckoo_hash.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <stdint.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <sys/queue.h>
40
41 #include <rte_common.h>
42 #include <rte_memory.h>         /* for definition of RTE_CACHE_LINE_SIZE */
43 #include <rte_log.h>
44 #include <rte_memcpy.h>
45 #include <rte_prefetch.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_eal.h>
50 #include <rte_eal_memconfig.h>
51 #include <rte_per_lcore.h>
52 #include <rte_errno.h>
53 #include <rte_string_fns.h>
54 #include <rte_cpuflags.h>
55 #include <rte_log.h>
56 #include <rte_rwlock.h>
57 #include <rte_spinlock.h>
58 #include <rte_ring.h>
59 #include <rte_compat.h>
60
61 #include "rte_hash.h"
62 #include "rte_cuckoo_hash.h"
63
64 #if defined(RTE_ARCH_X86)
65 #include "rte_cuckoo_hash_x86.h"
66 #endif
67
68 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
69
70 static struct rte_tailq_elem rte_hash_tailq = {
71         .name = "RTE_HASH",
72 };
73 EAL_REGISTER_TAILQ(rte_hash_tailq)
74
75 struct rte_hash *
76 rte_hash_find_existing(const char *name)
77 {
78         struct rte_hash *h = NULL;
79         struct rte_tailq_entry *te;
80         struct rte_hash_list *hash_list;
81
82         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
83
84         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
85         TAILQ_FOREACH(te, hash_list, next) {
86                 h = (struct rte_hash *) te->data;
87                 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
88                         break;
89         }
90         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
91
92         if (te == NULL) {
93                 rte_errno = ENOENT;
94                 return NULL;
95         }
96         return h;
97 }
98
99 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
100 {
101         h->cmp_jump_table_idx = KEY_CUSTOM;
102         h->rte_hash_custom_cmp_eq = func;
103 }
104
105 static inline int
106 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
107 {
108         if (h->cmp_jump_table_idx == KEY_CUSTOM)
109                 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
110         else
111                 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
112 }
113
114 struct rte_hash *
115 rte_hash_create(const struct rte_hash_parameters *params)
116 {
117         struct rte_hash *h = NULL;
118         struct rte_tailq_entry *te = NULL;
119         struct rte_hash_list *hash_list;
120         struct rte_ring *r = NULL;
121         char hash_name[RTE_HASH_NAMESIZE];
122         void *k = NULL;
123         void *buckets = NULL;
124         char ring_name[RTE_RING_NAMESIZE];
125         unsigned num_key_slots;
126         unsigned hw_trans_mem_support = 0;
127         unsigned i;
128
129         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
130
131         if (params == NULL) {
132                 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
133                 return NULL;
134         }
135
136         /* Check for valid parameters */
137         if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
138                         (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
139                         !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
140                         (params->key_len == 0)) {
141                 rte_errno = EINVAL;
142                 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
143                 return NULL;
144         }
145
146         /* Check extra flags field to check extra options. */
147         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
148                 hw_trans_mem_support = 1;
149
150         /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
151         if (hw_trans_mem_support)
152                 /*
153                  * Increase number of slots by total number of indices
154                  * that can be stored in the lcore caches
155                  * except for the first cache
156                  */
157                 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
158                                         LCORE_CACHE_SIZE + 1;
159         else
160                 num_key_slots = params->entries + 1;
161
162         snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
163         /* Create ring (Dummy slot index is not enqueued) */
164         r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots - 1),
165                         params->socket_id, 0);
166         if (r == NULL) {
167                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
168                 goto err;
169         }
170
171         snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
172
173         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
174
175         /* guarantee there's no existing: this is normally already checked
176          * by ring creation above */
177         TAILQ_FOREACH(te, hash_list, next) {
178                 h = (struct rte_hash *) te->data;
179                 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
180                         break;
181         }
182         h = NULL;
183         if (te != NULL) {
184                 rte_errno = EEXIST;
185                 te = NULL;
186                 goto err_unlock;
187         }
188
189         te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
190         if (te == NULL) {
191                 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
192                 goto err_unlock;
193         }
194
195         h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
196                                         RTE_CACHE_LINE_SIZE, params->socket_id);
197
198         if (h == NULL) {
199                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
200                 goto err_unlock;
201         }
202
203         const uint32_t num_buckets = rte_align32pow2(params->entries)
204                                         / RTE_HASH_BUCKET_ENTRIES;
205
206         buckets = rte_zmalloc_socket(NULL,
207                                 num_buckets * sizeof(struct rte_hash_bucket),
208                                 RTE_CACHE_LINE_SIZE, params->socket_id);
209
210         if (buckets == NULL) {
211                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
212                 goto err_unlock;
213         }
214
215         const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
216         const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
217
218         k = rte_zmalloc_socket(NULL, key_tbl_size,
219                         RTE_CACHE_LINE_SIZE, params->socket_id);
220
221         if (k == NULL) {
222                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
223                 goto err_unlock;
224         }
225
226 /*
227  * If x86 architecture is used, select appropriate compare function,
228  * which may use x86 intrinsics, otherwise use memcmp
229  */
230 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
231         /* Select function to compare keys */
232         switch (params->key_len) {
233         case 16:
234                 h->cmp_jump_table_idx = KEY_16_BYTES;
235                 break;
236         case 32:
237                 h->cmp_jump_table_idx = KEY_32_BYTES;
238                 break;
239         case 48:
240                 h->cmp_jump_table_idx = KEY_48_BYTES;
241                 break;
242         case 64:
243                 h->cmp_jump_table_idx = KEY_64_BYTES;
244                 break;
245         case 80:
246                 h->cmp_jump_table_idx = KEY_80_BYTES;
247                 break;
248         case 96:
249                 h->cmp_jump_table_idx = KEY_96_BYTES;
250                 break;
251         case 112:
252                 h->cmp_jump_table_idx = KEY_112_BYTES;
253                 break;
254         case 128:
255                 h->cmp_jump_table_idx = KEY_128_BYTES;
256                 break;
257         default:
258                 /* If key is not multiple of 16, use generic memcmp */
259                 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
260         }
261 #else
262         h->cmp_jump_table_idx = KEY_OTHER_BYTES;
263 #endif
264
265         if (hw_trans_mem_support) {
266                 h->local_free_slots = rte_zmalloc_socket(NULL,
267                                 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
268                                 RTE_CACHE_LINE_SIZE, params->socket_id);
269         }
270
271         /* Setup hash context */
272         snprintf(h->name, sizeof(h->name), "%s", params->name);
273         h->entries = params->entries;
274         h->key_len = params->key_len;
275         h->key_entry_size = key_entry_size;
276         h->hash_func_init_val = params->hash_func_init_val;
277
278         h->num_buckets = num_buckets;
279         h->bucket_bitmask = h->num_buckets - 1;
280         h->buckets = buckets;
281         h->hash_func = (params->hash_func == NULL) ?
282                 DEFAULT_HASH_FUNC : params->hash_func;
283         h->key_store = k;
284         h->free_slots = r;
285         h->hw_trans_mem_support = hw_trans_mem_support;
286
287         /* Turn on multi-writer only with explicit flat from user and TM
288          * support.
289          */
290         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
291                 if (h->hw_trans_mem_support) {
292                         h->add_key = ADD_KEY_MULTIWRITER_TM;
293                 } else {
294                         h->add_key = ADD_KEY_MULTIWRITER;
295                         h->multiwriter_lock = rte_malloc(NULL,
296                                                         sizeof(rte_spinlock_t),
297                                                         LCORE_CACHE_SIZE);
298                         rte_spinlock_init(h->multiwriter_lock);
299                 }
300         } else
301                 h->add_key = ADD_KEY_SINGLEWRITER;
302
303         /* Populate free slots ring. Entry zero is reserved for key misses. */
304         for (i = 1; i < params->entries + 1; i++)
305                 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
306
307         te->data = (void *) h;
308         TAILQ_INSERT_TAIL(hash_list, te, next);
309         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
310
311         return h;
312 err_unlock:
313         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
314 err:
315         rte_ring_free(r);
316         rte_free(te);
317         rte_free(h);
318         rte_free(buckets);
319         rte_free(k);
320         return NULL;
321 }
322
323 void
324 rte_hash_free(struct rte_hash *h)
325 {
326         struct rte_tailq_entry *te;
327         struct rte_hash_list *hash_list;
328
329         if (h == NULL)
330                 return;
331
332         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
333
334         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
335
336         /* find out tailq entry */
337         TAILQ_FOREACH(te, hash_list, next) {
338                 if (te->data == (void *) h)
339                         break;
340         }
341
342         if (te == NULL) {
343                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
344                 return;
345         }
346
347         TAILQ_REMOVE(hash_list, te, next);
348
349         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
350
351         if (h->hw_trans_mem_support)
352                 rte_free(h->local_free_slots);
353
354         if (h->add_key == ADD_KEY_MULTIWRITER)
355                 rte_free(h->multiwriter_lock);
356         rte_ring_free(h->free_slots);
357         rte_free(h->key_store);
358         rte_free(h->buckets);
359         rte_free(h);
360         rte_free(te);
361 }
362
363 hash_sig_t
364 rte_hash_hash(const struct rte_hash *h, const void *key)
365 {
366         /* calc hash result by key */
367         return h->hash_func(key, h->key_len, h->hash_func_init_val);
368 }
369
370 /* Calc the secondary hash value from the primary hash value of a given key */
371 static inline hash_sig_t
372 rte_hash_secondary_hash(const hash_sig_t primary_hash)
373 {
374         static const unsigned all_bits_shift = 12;
375         static const unsigned alt_bits_xor = 0x5bd1e995;
376
377         uint32_t tag = primary_hash >> all_bits_shift;
378
379         return primary_hash ^ ((tag + 1) * alt_bits_xor);
380 }
381
382 void
383 rte_hash_reset(struct rte_hash *h)
384 {
385         void *ptr;
386         unsigned i;
387
388         if (h == NULL)
389                 return;
390
391         memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
392         memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
393
394         /* clear the free ring */
395         while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
396                 rte_pause();
397
398         /* Repopulate the free slots ring. Entry zero is reserved for key misses */
399         for (i = 1; i < h->entries + 1; i++)
400                 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
401
402         if (h->hw_trans_mem_support) {
403                 /* Reset local caches per lcore */
404                 for (i = 0; i < RTE_MAX_LCORE; i++)
405                         h->local_free_slots[i].len = 0;
406         }
407 }
408
409 /* Search for an entry that can be pushed to its alternative location */
410 static inline int
411 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
412 {
413         unsigned i, j;
414         int ret;
415         uint32_t next_bucket_idx;
416         struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
417
418         /*
419          * Push existing item (search for bucket with space in
420          * alternative locations) to its alternative location
421          */
422         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
423                 /* Search for space in alternative locations */
424                 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
425                 next_bkt[i] = &h->buckets[next_bucket_idx];
426                 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
427                         if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
428                                 break;
429                 }
430
431                 if (j != RTE_HASH_BUCKET_ENTRIES)
432                         break;
433         }
434
435         /* Alternative location has spare room (end of recursive function) */
436         if (i != RTE_HASH_BUCKET_ENTRIES) {
437                 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
438                 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
439                 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
440                 return i;
441         }
442
443         /* Pick entry that has not been pushed yet */
444         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
445                 if (bkt->flag[i] == 0)
446                         break;
447
448         /* All entries have been pushed, so entry cannot be added */
449         if (i == RTE_HASH_BUCKET_ENTRIES)
450                 return -ENOSPC;
451
452         /* Set flag to indicate that this entry is going to be pushed */
453         bkt->flag[i] = 1;
454         /* Need room in alternative bucket to insert the pushed entry */
455         ret = make_space_bucket(h, next_bkt[i]);
456         /*
457          * After recursive function.
458          * Clear flags and insert the pushed entry
459          * in its alternative location if successful,
460          * or return error
461          */
462         bkt->flag[i] = 0;
463         if (ret >= 0) {
464                 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
465                 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
466                 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
467                 return i;
468         } else
469                 return ret;
470
471 }
472
473 /*
474  * Function called to enqueue back an index in the cache/ring,
475  * as slot has not being used and it can be used in the
476  * next addition attempt.
477  */
478 static inline void
479 enqueue_slot_back(const struct rte_hash *h,
480                 struct lcore_cache *cached_free_slots,
481                 void *slot_id)
482 {
483         if (h->hw_trans_mem_support) {
484                 cached_free_slots->objs[cached_free_slots->len] = slot_id;
485                 cached_free_slots->len++;
486         } else
487                 rte_ring_sp_enqueue(h->free_slots, slot_id);
488 }
489
490 static inline int32_t
491 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
492                                                 hash_sig_t sig, void *data)
493 {
494         hash_sig_t alt_hash;
495         uint32_t prim_bucket_idx, sec_bucket_idx;
496         unsigned i;
497         struct rte_hash_bucket *prim_bkt, *sec_bkt;
498         struct rte_hash_key *new_k, *k, *keys = h->key_store;
499         void *slot_id = NULL;
500         uint32_t new_idx;
501         int ret;
502         unsigned n_slots;
503         unsigned lcore_id;
504         struct lcore_cache *cached_free_slots = NULL;
505
506         if (h->add_key == ADD_KEY_MULTIWRITER)
507                 rte_spinlock_lock(h->multiwriter_lock);
508
509         prim_bucket_idx = sig & h->bucket_bitmask;
510         prim_bkt = &h->buckets[prim_bucket_idx];
511         rte_prefetch0(prim_bkt);
512
513         alt_hash = rte_hash_secondary_hash(sig);
514         sec_bucket_idx = alt_hash & h->bucket_bitmask;
515         sec_bkt = &h->buckets[sec_bucket_idx];
516         rte_prefetch0(sec_bkt);
517
518         /* Get a new slot for storing the new key */
519         if (h->hw_trans_mem_support) {
520                 lcore_id = rte_lcore_id();
521                 cached_free_slots = &h->local_free_slots[lcore_id];
522                 /* Try to get a free slot from the local cache */
523                 if (cached_free_slots->len == 0) {
524                         /* Need to get another burst of free slots from global ring */
525                         n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
526                                         cached_free_slots->objs, LCORE_CACHE_SIZE);
527                         if (n_slots == 0)
528                                 return -ENOSPC;
529
530                         cached_free_slots->len += n_slots;
531                 }
532
533                 /* Get a free slot from the local cache */
534                 cached_free_slots->len--;
535                 slot_id = cached_free_slots->objs[cached_free_slots->len];
536         } else {
537                 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
538                         return -ENOSPC;
539         }
540
541         new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
542         rte_prefetch0(new_k);
543         new_idx = (uint32_t)((uintptr_t) slot_id);
544
545         /* Check if key is already inserted in primary location */
546         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
547                 if (prim_bkt->signatures[i].current == sig &&
548                                 prim_bkt->signatures[i].alt == alt_hash) {
549                         k = (struct rte_hash_key *) ((char *)keys +
550                                         prim_bkt->key_idx[i] * h->key_entry_size);
551                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
552                                 /* Enqueue index of free slot back in the ring. */
553                                 enqueue_slot_back(h, cached_free_slots, slot_id);
554                                 /* Update data */
555                                 k->pdata = data;
556                                 /*
557                                  * Return index where key is stored,
558                                  * substracting the first dummy index
559                                  */
560                                 return prim_bkt->key_idx[i] - 1;
561                         }
562                 }
563         }
564
565         /* Check if key is already inserted in secondary location */
566         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
567                 if (sec_bkt->signatures[i].alt == sig &&
568                                 sec_bkt->signatures[i].current == alt_hash) {
569                         k = (struct rte_hash_key *) ((char *)keys +
570                                         sec_bkt->key_idx[i] * h->key_entry_size);
571                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
572                                 /* Enqueue index of free slot back in the ring. */
573                                 enqueue_slot_back(h, cached_free_slots, slot_id);
574                                 /* Update data */
575                                 k->pdata = data;
576                                 /*
577                                  * Return index where key is stored,
578                                  * substracting the first dummy index
579                                  */
580                                 return sec_bkt->key_idx[i] - 1;
581                         }
582                 }
583         }
584
585         /* Copy key */
586         rte_memcpy(new_k->key, key, h->key_len);
587         new_k->pdata = data;
588
589 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
590         if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
591                 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
592                                 sig, alt_hash, new_idx);
593                 if (ret >= 0)
594                         return new_idx - 1;
595
596                 /* Primary bucket full, need to make space for new entry */
597                 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
598                                                         alt_hash, new_idx);
599
600                 if (ret >= 0)
601                         return new_idx - 1;
602
603                 /* Also search secondary bucket to get better occupancy */
604                 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
605                                                         alt_hash, new_idx);
606
607                 if (ret >= 0)
608                         return new_idx - 1;
609         } else {
610 #endif
611                 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
612                         /* Check if slot is available */
613                         if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
614                                 prim_bkt->signatures[i].current = sig;
615                                 prim_bkt->signatures[i].alt = alt_hash;
616                                 prim_bkt->key_idx[i] = new_idx;
617                                 break;
618                         }
619                 }
620
621                 if (i != RTE_HASH_BUCKET_ENTRIES) {
622                         if (h->add_key == ADD_KEY_MULTIWRITER)
623                                 rte_spinlock_unlock(h->multiwriter_lock);
624                         return new_idx - 1;
625                 }
626
627                 /* Primary bucket full, need to make space for new entry
628                  * After recursive function.
629                  * Insert the new entry in the position of the pushed entry
630                  * if successful or return error and
631                  * store the new slot back in the ring
632                  */
633                 ret = make_space_bucket(h, prim_bkt);
634                 if (ret >= 0) {
635                         prim_bkt->signatures[ret].current = sig;
636                         prim_bkt->signatures[ret].alt = alt_hash;
637                         prim_bkt->key_idx[ret] = new_idx;
638                         if (h->add_key == ADD_KEY_MULTIWRITER)
639                                 rte_spinlock_unlock(h->multiwriter_lock);
640                         return new_idx - 1;
641                 }
642 #if defined(RTE_ARCH_X86)
643         }
644 #endif
645         /* Error in addition, store new slot back in the ring and return error */
646         enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
647
648         if (h->add_key == ADD_KEY_MULTIWRITER)
649                 rte_spinlock_unlock(h->multiwriter_lock);
650         return ret;
651 }
652
653 int32_t
654 rte_hash_add_key_with_hash(const struct rte_hash *h,
655                         const void *key, hash_sig_t sig)
656 {
657         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
658         return __rte_hash_add_key_with_hash(h, key, sig, 0);
659 }
660
661 int32_t
662 rte_hash_add_key(const struct rte_hash *h, const void *key)
663 {
664         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
665         return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
666 }
667
668 int
669 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
670                         const void *key, hash_sig_t sig, void *data)
671 {
672         int ret;
673
674         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
675         ret = __rte_hash_add_key_with_hash(h, key, sig, data);
676         if (ret >= 0)
677                 return 0;
678         else
679                 return ret;
680 }
681
682 int
683 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
684 {
685         int ret;
686
687         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
688
689         ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
690         if (ret >= 0)
691                 return 0;
692         else
693                 return ret;
694 }
695 static inline int32_t
696 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
697                                         hash_sig_t sig, void **data)
698 {
699         uint32_t bucket_idx;
700         hash_sig_t alt_hash;
701         unsigned i;
702         struct rte_hash_bucket *bkt;
703         struct rte_hash_key *k, *keys = h->key_store;
704
705         bucket_idx = sig & h->bucket_bitmask;
706         bkt = &h->buckets[bucket_idx];
707
708         /* Check if key is in primary location */
709         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
710                 if (bkt->signatures[i].current == sig &&
711                                 bkt->signatures[i].sig != NULL_SIGNATURE) {
712                         k = (struct rte_hash_key *) ((char *)keys +
713                                         bkt->key_idx[i] * h->key_entry_size);
714                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
715                                 if (data != NULL)
716                                         *data = k->pdata;
717                                 /*
718                                  * Return index where key is stored,
719                                  * substracting the first dummy index
720                                  */
721                                 return bkt->key_idx[i] - 1;
722                         }
723                 }
724         }
725
726         /* Calculate secondary hash */
727         alt_hash = rte_hash_secondary_hash(sig);
728         bucket_idx = alt_hash & h->bucket_bitmask;
729         bkt = &h->buckets[bucket_idx];
730
731         /* Check if key is in secondary location */
732         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
733                 if (bkt->signatures[i].current == alt_hash &&
734                                 bkt->signatures[i].alt == sig) {
735                         k = (struct rte_hash_key *) ((char *)keys +
736                                         bkt->key_idx[i] * h->key_entry_size);
737                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
738                                 if (data != NULL)
739                                         *data = k->pdata;
740                                 /*
741                                  * Return index where key is stored,
742                                  * substracting the first dummy index
743                                  */
744                                 return bkt->key_idx[i] - 1;
745                         }
746                 }
747         }
748
749         return -ENOENT;
750 }
751
752 int32_t
753 rte_hash_lookup_with_hash(const struct rte_hash *h,
754                         const void *key, hash_sig_t sig)
755 {
756         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
757         return __rte_hash_lookup_with_hash(h, key, sig, NULL);
758 }
759
760 int32_t
761 rte_hash_lookup(const struct rte_hash *h, const void *key)
762 {
763         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
764         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
765 }
766
767 int
768 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
769                         const void *key, hash_sig_t sig, void **data)
770 {
771         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
772         return __rte_hash_lookup_with_hash(h, key, sig, data);
773 }
774
775 int
776 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
777 {
778         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
779         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
780 }
781
782 static inline void
783 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
784 {
785         unsigned lcore_id, n_slots;
786         struct lcore_cache *cached_free_slots;
787
788         bkt->signatures[i].sig = NULL_SIGNATURE;
789         if (h->hw_trans_mem_support) {
790                 lcore_id = rte_lcore_id();
791                 cached_free_slots = &h->local_free_slots[lcore_id];
792                 /* Cache full, need to free it. */
793                 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
794                         /* Need to enqueue the free slots in global ring. */
795                         n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
796                                                 cached_free_slots->objs,
797                                                 LCORE_CACHE_SIZE);
798                         cached_free_slots->len -= n_slots;
799                 }
800                 /* Put index of new free slot in cache. */
801                 cached_free_slots->objs[cached_free_slots->len] =
802                                 (void *)((uintptr_t)bkt->key_idx[i]);
803                 cached_free_slots->len++;
804         } else {
805                 rte_ring_sp_enqueue(h->free_slots,
806                                 (void *)((uintptr_t)bkt->key_idx[i]));
807         }
808 }
809
810 static inline int32_t
811 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
812                                                 hash_sig_t sig)
813 {
814         uint32_t bucket_idx;
815         hash_sig_t alt_hash;
816         unsigned i;
817         struct rte_hash_bucket *bkt;
818         struct rte_hash_key *k, *keys = h->key_store;
819
820         bucket_idx = sig & h->bucket_bitmask;
821         bkt = &h->buckets[bucket_idx];
822
823         /* Check if key is in primary location */
824         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
825                 if (bkt->signatures[i].current == sig &&
826                                 bkt->signatures[i].sig != NULL_SIGNATURE) {
827                         k = (struct rte_hash_key *) ((char *)keys +
828                                         bkt->key_idx[i] * h->key_entry_size);
829                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
830                                 remove_entry(h, bkt, i);
831
832                                 /*
833                                  * Return index where key is stored,
834                                  * substracting the first dummy index
835                                  */
836                                 return bkt->key_idx[i] - 1;
837                         }
838                 }
839         }
840
841         /* Calculate secondary hash */
842         alt_hash = rte_hash_secondary_hash(sig);
843         bucket_idx = alt_hash & h->bucket_bitmask;
844         bkt = &h->buckets[bucket_idx];
845
846         /* Check if key is in secondary location */
847         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
848                 if (bkt->signatures[i].current == alt_hash &&
849                                 bkt->signatures[i].sig != NULL_SIGNATURE) {
850                         k = (struct rte_hash_key *) ((char *)keys +
851                                         bkt->key_idx[i] * h->key_entry_size);
852                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
853                                 remove_entry(h, bkt, i);
854
855                                 /*
856                                  * Return index where key is stored,
857                                  * substracting the first dummy index
858                                  */
859                                 return bkt->key_idx[i] - 1;
860                         }
861                 }
862         }
863
864         return -ENOENT;
865 }
866
867 int32_t
868 rte_hash_del_key_with_hash(const struct rte_hash *h,
869                         const void *key, hash_sig_t sig)
870 {
871         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
872         return __rte_hash_del_key_with_hash(h, key, sig);
873 }
874
875 int32_t
876 rte_hash_del_key(const struct rte_hash *h, const void *key)
877 {
878         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
879         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
880 }
881
882 int
883 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
884                                void **key)
885 {
886         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
887
888         struct rte_hash_key *k, *keys = h->key_store;
889         k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
890                                      h->key_entry_size);
891         *key = k->key;
892
893         if (position !=
894             __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
895                                         NULL)) {
896                 return -ENOENT;
897         }
898
899         return 0;
900 }
901
902 /* Lookup bulk stage 0: Prefetch input key */
903 static inline void
904 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
905                 const void * const *keys)
906 {
907         *idx = __builtin_ctzl(*lookup_mask);
908         if (*lookup_mask == 0)
909                 *idx = 0;
910
911         rte_prefetch0(keys[*idx]);
912         *lookup_mask &= ~(1llu << *idx);
913 }
914
915 /*
916  * Lookup bulk stage 1: Calculate primary/secondary hashes
917  * and prefetch primary/secondary buckets
918  */
919 static inline void
920 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
921                 const struct rte_hash_bucket **primary_bkt,
922                 const struct rte_hash_bucket **secondary_bkt,
923                 hash_sig_t *hash_vals, const void * const *keys,
924                 const struct rte_hash *h)
925 {
926         *prim_hash = rte_hash_hash(h, keys[idx]);
927         hash_vals[idx] = *prim_hash;
928         *sec_hash = rte_hash_secondary_hash(*prim_hash);
929
930         *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
931         *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
932
933         rte_prefetch0(*primary_bkt);
934         rte_prefetch0(*secondary_bkt);
935 }
936
937 /*
938  * Lookup bulk stage 2:  Search for match hashes in primary/secondary locations
939  * and prefetch first key slot
940  */
941 static inline void
942 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
943                 const struct rte_hash_bucket *prim_bkt,
944                 const struct rte_hash_bucket *sec_bkt,
945                 const struct rte_hash_key **key_slot, int32_t *positions,
946                 uint64_t *extra_hits_mask, const void *keys,
947                 const struct rte_hash *h)
948 {
949         unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
950         unsigned total_hash_matches;
951
952         prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
953         sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
954         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
955                 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
956                 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
957         }
958
959         key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
960         if (key_idx == 0)
961                 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
962
963         total_hash_matches = (prim_hash_matches |
964                                 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
965         *key_slot = (const struct rte_hash_key *) ((const char *)keys +
966                                         key_idx * h->key_entry_size);
967
968         rte_prefetch0(*key_slot);
969         /*
970          * Return index where key is stored,
971          * substracting the first dummy index
972          */
973         positions[idx] = (key_idx - 1);
974
975         *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
976
977 }
978
979
980 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
981 static inline void
982 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
983                 const int32_t *positions, void *data[], uint64_t *hits,
984                 const struct rte_hash *h)
985 {
986         unsigned hit;
987         unsigned key_idx;
988
989         hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
990         if (data != NULL)
991                 data[idx] = key_slot->pdata;
992
993         key_idx = positions[idx] + 1;
994         /*
995          * If key index is 0, force hit to be 0, in case key to be looked up
996          * is all zero (as in the dummy slot), which would result in a wrong hit
997          */
998         *hits |= (uint64_t)(hit && !!key_idx)  << idx;
999 }
1000
1001 static inline void
1002 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1003                         uint32_t num_keys, int32_t *positions,
1004                         uint64_t *hit_mask, void *data[])
1005 {
1006         uint64_t hits = 0;
1007         uint64_t extra_hits_mask = 0;
1008         uint64_t lookup_mask, miss_mask;
1009         unsigned idx;
1010         const void *key_store = h->key_store;
1011         int ret;
1012         hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1013
1014         unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1015         const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1016         const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1017         const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1018         const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1019         const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1020         hash_sig_t primary_hash10, primary_hash11;
1021         hash_sig_t secondary_hash10, secondary_hash11;
1022         hash_sig_t primary_hash20, primary_hash21;
1023         hash_sig_t secondary_hash20, secondary_hash21;
1024
1025         lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1026         miss_mask = lookup_mask;
1027
1028         lookup_stage0(&idx00, &lookup_mask, keys);
1029         lookup_stage0(&idx01, &lookup_mask, keys);
1030
1031         idx10 = idx00, idx11 = idx01;
1032
1033         lookup_stage0(&idx00, &lookup_mask, keys);
1034         lookup_stage0(&idx01, &lookup_mask, keys);
1035         lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1036                         &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1037         lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1038                         &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1039
1040         primary_bkt20 = primary_bkt10;
1041         primary_bkt21 = primary_bkt11;
1042         secondary_bkt20 = secondary_bkt10;
1043         secondary_bkt21 = secondary_bkt11;
1044         primary_hash20 = primary_hash10;
1045         primary_hash21 = primary_hash11;
1046         secondary_hash20 = secondary_hash10;
1047         secondary_hash21 = secondary_hash11;
1048         idx20 = idx10, idx21 = idx11;
1049         idx10 = idx00, idx11 = idx01;
1050
1051         lookup_stage0(&idx00, &lookup_mask, keys);
1052         lookup_stage0(&idx01, &lookup_mask, keys);
1053         lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1054                         &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1055         lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1056                         &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1057         lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1058                         secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1059                         key_store, h);
1060         lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1061                         secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1062                         key_store, h);
1063
1064         while (lookup_mask) {
1065                 k_slot30 = k_slot20, k_slot31 = k_slot21;
1066                 idx30 = idx20, idx31 = idx21;
1067                 primary_bkt20 = primary_bkt10;
1068                 primary_bkt21 = primary_bkt11;
1069                 secondary_bkt20 = secondary_bkt10;
1070                 secondary_bkt21 = secondary_bkt11;
1071                 primary_hash20 = primary_hash10;
1072                 primary_hash21 = primary_hash11;
1073                 secondary_hash20 = secondary_hash10;
1074                 secondary_hash21 = secondary_hash11;
1075                 idx20 = idx10, idx21 = idx11;
1076                 idx10 = idx00, idx11 = idx01;
1077
1078                 lookup_stage0(&idx00, &lookup_mask, keys);
1079                 lookup_stage0(&idx01, &lookup_mask, keys);
1080                 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1081                         &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1082                 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1083                         &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1084                 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1085                         primary_bkt20, secondary_bkt20, &k_slot20, positions,
1086                         &extra_hits_mask, key_store, h);
1087                 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1088                         primary_bkt21, secondary_bkt21, &k_slot21, positions,
1089                         &extra_hits_mask, key_store, h);
1090                 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1091                 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1092         }
1093
1094         k_slot30 = k_slot20, k_slot31 = k_slot21;
1095         idx30 = idx20, idx31 = idx21;
1096         primary_bkt20 = primary_bkt10;
1097         primary_bkt21 = primary_bkt11;
1098         secondary_bkt20 = secondary_bkt10;
1099         secondary_bkt21 = secondary_bkt11;
1100         primary_hash20 = primary_hash10;
1101         primary_hash21 = primary_hash11;
1102         secondary_hash20 = secondary_hash10;
1103         secondary_hash21 = secondary_hash11;
1104         idx20 = idx10, idx21 = idx11;
1105         idx10 = idx00, idx11 = idx01;
1106
1107         lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1108                 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1109         lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1110                 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1111         lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1112                 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1113                 key_store, h);
1114         lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1115                 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1116                 key_store, h);
1117         lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1118         lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1119
1120         k_slot30 = k_slot20, k_slot31 = k_slot21;
1121         idx30 = idx20, idx31 = idx21;
1122         primary_bkt20 = primary_bkt10;
1123         primary_bkt21 = primary_bkt11;
1124         secondary_bkt20 = secondary_bkt10;
1125         secondary_bkt21 = secondary_bkt11;
1126         primary_hash20 = primary_hash10;
1127         primary_hash21 = primary_hash11;
1128         secondary_hash20 = secondary_hash10;
1129         secondary_hash21 = secondary_hash11;
1130         idx20 = idx10, idx21 = idx11;
1131
1132         lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1133                 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1134                 key_store, h);
1135         lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1136                 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1137                 key_store, h);
1138         lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1139         lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1140
1141         k_slot30 = k_slot20, k_slot31 = k_slot21;
1142         idx30 = idx20, idx31 = idx21;
1143
1144         lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1145         lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1146
1147         /* ignore any items we have already found */
1148         extra_hits_mask &= ~hits;
1149
1150         if (unlikely(extra_hits_mask)) {
1151                 /* run a single search for each remaining item */
1152                 do {
1153                         idx = __builtin_ctzl(extra_hits_mask);
1154                         if (data != NULL) {
1155                                 ret = rte_hash_lookup_with_hash_data(h,
1156                                                 keys[idx], hash_vals[idx], &data[idx]);
1157                                 if (ret >= 0)
1158                                         hits |= 1ULL << idx;
1159                         } else {
1160                                 positions[idx] = rte_hash_lookup_with_hash(h,
1161                                                         keys[idx], hash_vals[idx]);
1162                                 if (positions[idx] >= 0)
1163                                         hits |= 1llu << idx;
1164                         }
1165                         extra_hits_mask &= ~(1llu << idx);
1166                 } while (extra_hits_mask);
1167         }
1168
1169         miss_mask &= ~hits;
1170         if (unlikely(miss_mask)) {
1171                 do {
1172                         idx = __builtin_ctzl(miss_mask);
1173                         positions[idx] = -ENOENT;
1174                         miss_mask &= ~(1llu << idx);
1175                 } while (miss_mask);
1176         }
1177
1178         if (hit_mask != NULL)
1179                 *hit_mask = hits;
1180 }
1181
1182 int
1183 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1184                       uint32_t num_keys, int32_t *positions)
1185 {
1186         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1187                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1188                         (positions == NULL)), -EINVAL);
1189
1190         __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1191         return 0;
1192 }
1193
1194 int
1195 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1196                       uint32_t num_keys, uint64_t *hit_mask, void *data[])
1197 {
1198         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1199                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1200                         (hit_mask == NULL)), -EINVAL);
1201
1202         int32_t positions[num_keys];
1203
1204         __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1205
1206         /* Return number of hits */
1207         return __builtin_popcountl(*hit_mask);
1208 }
1209
1210 int32_t
1211 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1212 {
1213         uint32_t bucket_idx, idx, position;
1214         struct rte_hash_key *next_key;
1215
1216         RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1217
1218         const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1219         /* Out of bounds */
1220         if (*next >= total_entries)
1221                 return -ENOENT;
1222
1223         /* Calculate bucket and index of current iterator */
1224         bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1225         idx = *next % RTE_HASH_BUCKET_ENTRIES;
1226
1227         /* If current position is empty, go to the next one */
1228         while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1229                 (*next)++;
1230                 /* End of table */
1231                 if (*next == total_entries)
1232                         return -ENOENT;
1233                 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1234                 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1235         }
1236
1237         /* Get position of entry in key table */
1238         position = h->buckets[bucket_idx].key_idx[idx];
1239         next_key = (struct rte_hash_key *) ((char *)h->key_store +
1240                                 position * h->key_entry_size);
1241         /* Return key and data */
1242         *key = next_key->key;
1243         *data = next_key->pdata;
1244
1245         /* Increment iterator */
1246         (*next)++;
1247
1248         return position - 1;
1249 }