a7ee2b9c03d380b768db6bdae174f7ab8c653c90
[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->sig_alt[i] & 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]->key_idx[j] == EMPTY_SLOT)
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]->sig_alt[j] = bkt->sig_current[i];
438                 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
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]->sig_alt[ret] = bkt->sig_current[i];
465                 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
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->sig_current[i] == sig &&
548                                 prim_bkt->sig_alt[i] == 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->sig_alt[i] == sig &&
568                                 sec_bkt->sig_current[i] == 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->key_idx[i] == EMPTY_SLOT)) {
614                                 prim_bkt->sig_current[i] = sig;
615                                 prim_bkt->sig_alt[i] = 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->sig_current[ret] = sig;
636                         prim_bkt->sig_alt[ret] = 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->sig_current[i] == sig &&
711                                 bkt->key_idx[i] != EMPTY_SLOT) {
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->sig_current[i] == alt_hash &&
734                                 bkt->sig_alt[i] == 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->sig_current[i] = NULL_SIGNATURE;
789         bkt->sig_alt[i] = NULL_SIGNATURE;
790         if (h->hw_trans_mem_support) {
791                 lcore_id = rte_lcore_id();
792                 cached_free_slots = &h->local_free_slots[lcore_id];
793                 /* Cache full, need to free it. */
794                 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
795                         /* Need to enqueue the free slots in global ring. */
796                         n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
797                                                 cached_free_slots->objs,
798                                                 LCORE_CACHE_SIZE);
799                         cached_free_slots->len -= n_slots;
800                 }
801                 /* Put index of new free slot in cache. */
802                 cached_free_slots->objs[cached_free_slots->len] =
803                                 (void *)((uintptr_t)bkt->key_idx[i]);
804                 cached_free_slots->len++;
805         } else {
806                 rte_ring_sp_enqueue(h->free_slots,
807                                 (void *)((uintptr_t)bkt->key_idx[i]));
808         }
809 }
810
811 static inline int32_t
812 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
813                                                 hash_sig_t sig)
814 {
815         uint32_t bucket_idx;
816         hash_sig_t alt_hash;
817         unsigned i;
818         struct rte_hash_bucket *bkt;
819         struct rte_hash_key *k, *keys = h->key_store;
820         int32_t ret;
821
822         bucket_idx = sig & h->bucket_bitmask;
823         bkt = &h->buckets[bucket_idx];
824
825         /* Check if key is in primary location */
826         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
827                 if (bkt->sig_current[i] == sig &&
828                                 bkt->key_idx[i] != EMPTY_SLOT) {
829                         k = (struct rte_hash_key *) ((char *)keys +
830                                         bkt->key_idx[i] * h->key_entry_size);
831                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
832                                 remove_entry(h, bkt, i);
833
834                                 /*
835                                  * Return index where key is stored,
836                                  * substracting the first dummy index
837                                  */
838                                 ret = bkt->key_idx[i] - 1;
839                                 bkt->key_idx[i] = EMPTY_SLOT;
840                                 return ret;
841                         }
842                 }
843         }
844
845         /* Calculate secondary hash */
846         alt_hash = rte_hash_secondary_hash(sig);
847         bucket_idx = alt_hash & h->bucket_bitmask;
848         bkt = &h->buckets[bucket_idx];
849
850         /* Check if key is in secondary location */
851         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
852                 if (bkt->sig_current[i] == alt_hash &&
853                                 bkt->key_idx[i] != EMPTY_SLOT) {
854                         k = (struct rte_hash_key *) ((char *)keys +
855                                         bkt->key_idx[i] * h->key_entry_size);
856                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
857                                 remove_entry(h, bkt, i);
858
859                                 /*
860                                  * Return index where key is stored,
861                                  * substracting the first dummy index
862                                  */
863                                 ret = bkt->key_idx[i] - 1;
864                                 bkt->key_idx[i] = EMPTY_SLOT;
865                                 return ret;
866                         }
867                 }
868         }
869
870         return -ENOENT;
871 }
872
873 int32_t
874 rte_hash_del_key_with_hash(const struct rte_hash *h,
875                         const void *key, hash_sig_t sig)
876 {
877         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
878         return __rte_hash_del_key_with_hash(h, key, sig);
879 }
880
881 int32_t
882 rte_hash_del_key(const struct rte_hash *h, const void *key)
883 {
884         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
885         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
886 }
887
888 int
889 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
890                                void **key)
891 {
892         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
893
894         struct rte_hash_key *k, *keys = h->key_store;
895         k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
896                                      h->key_entry_size);
897         *key = k->key;
898
899         if (position !=
900             __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
901                                         NULL)) {
902                 return -ENOENT;
903         }
904
905         return 0;
906 }
907
908 /* Lookup bulk stage 0: Prefetch input key */
909 static inline void
910 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
911                 const void * const *keys)
912 {
913         *idx = __builtin_ctzl(*lookup_mask);
914         if (*lookup_mask == 0)
915                 *idx = 0;
916
917         rte_prefetch0(keys[*idx]);
918         *lookup_mask &= ~(1llu << *idx);
919 }
920
921 /*
922  * Lookup bulk stage 1: Calculate primary/secondary hashes
923  * and prefetch primary/secondary buckets
924  */
925 static inline void
926 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
927                 const struct rte_hash_bucket **primary_bkt,
928                 const struct rte_hash_bucket **secondary_bkt,
929                 hash_sig_t *hash_vals, const void * const *keys,
930                 const struct rte_hash *h)
931 {
932         *prim_hash = rte_hash_hash(h, keys[idx]);
933         hash_vals[idx] = *prim_hash;
934         *sec_hash = rte_hash_secondary_hash(*prim_hash);
935
936         *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
937         *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
938
939         rte_prefetch0(*primary_bkt);
940         rte_prefetch0(*secondary_bkt);
941 }
942
943 /*
944  * Lookup bulk stage 2:  Search for match hashes in primary/secondary locations
945  * and prefetch first key slot
946  */
947 static inline void
948 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
949                 const struct rte_hash_bucket *prim_bkt,
950                 const struct rte_hash_bucket *sec_bkt,
951                 const struct rte_hash_key **key_slot, int32_t *positions,
952                 uint64_t *extra_hits_mask, const void *keys,
953                 const struct rte_hash *h)
954 {
955         unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
956         unsigned total_hash_matches;
957
958         prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
959         sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
960         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
961                 prim_hash_matches |= ((prim_hash == prim_bkt->sig_current[i]) << i);
962                 sec_hash_matches |= ((sec_hash == sec_bkt->sig_current[i]) << i);
963         }
964
965         key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
966         if (key_idx == 0)
967                 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
968
969         total_hash_matches = (prim_hash_matches |
970                                 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
971         *key_slot = (const struct rte_hash_key *) ((const char *)keys +
972                                         key_idx * h->key_entry_size);
973
974         rte_prefetch0(*key_slot);
975         /*
976          * Return index where key is stored,
977          * substracting the first dummy index
978          */
979         positions[idx] = (key_idx - 1);
980
981         *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
982
983 }
984
985
986 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
987 static inline void
988 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
989                 const int32_t *positions, void *data[], uint64_t *hits,
990                 const struct rte_hash *h)
991 {
992         unsigned hit;
993         unsigned key_idx;
994
995         hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
996         if (data != NULL)
997                 data[idx] = key_slot->pdata;
998
999         key_idx = positions[idx] + 1;
1000         /*
1001          * If key index is 0, force hit to be 0, in case key to be looked up
1002          * is all zero (as in the dummy slot), which would result in a wrong hit
1003          */
1004         *hits |= (uint64_t)(hit && !!key_idx)  << idx;
1005 }
1006
1007 static inline void
1008 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1009                         uint32_t num_keys, int32_t *positions,
1010                         uint64_t *hit_mask, void *data[])
1011 {
1012         uint64_t hits = 0;
1013         uint64_t extra_hits_mask = 0;
1014         uint64_t lookup_mask, miss_mask;
1015         unsigned idx;
1016         const void *key_store = h->key_store;
1017         int ret;
1018         hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1019
1020         unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1021         const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1022         const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1023         const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1024         const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1025         const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1026         hash_sig_t primary_hash10, primary_hash11;
1027         hash_sig_t secondary_hash10, secondary_hash11;
1028         hash_sig_t primary_hash20, primary_hash21;
1029         hash_sig_t secondary_hash20, secondary_hash21;
1030
1031         lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1032         miss_mask = lookup_mask;
1033
1034         lookup_stage0(&idx00, &lookup_mask, keys);
1035         lookup_stage0(&idx01, &lookup_mask, keys);
1036
1037         idx10 = idx00, idx11 = idx01;
1038
1039         lookup_stage0(&idx00, &lookup_mask, keys);
1040         lookup_stage0(&idx01, &lookup_mask, keys);
1041         lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1042                         &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1043         lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1044                         &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1045
1046         primary_bkt20 = primary_bkt10;
1047         primary_bkt21 = primary_bkt11;
1048         secondary_bkt20 = secondary_bkt10;
1049         secondary_bkt21 = secondary_bkt11;
1050         primary_hash20 = primary_hash10;
1051         primary_hash21 = primary_hash11;
1052         secondary_hash20 = secondary_hash10;
1053         secondary_hash21 = secondary_hash11;
1054         idx20 = idx10, idx21 = idx11;
1055         idx10 = idx00, idx11 = idx01;
1056
1057         lookup_stage0(&idx00, &lookup_mask, keys);
1058         lookup_stage0(&idx01, &lookup_mask, keys);
1059         lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1060                         &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1061         lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1062                         &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1063         lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1064                         secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1065                         key_store, h);
1066         lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1067                         secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1068                         key_store, h);
1069
1070         while (lookup_mask) {
1071                 k_slot30 = k_slot20, k_slot31 = k_slot21;
1072                 idx30 = idx20, idx31 = idx21;
1073                 primary_bkt20 = primary_bkt10;
1074                 primary_bkt21 = primary_bkt11;
1075                 secondary_bkt20 = secondary_bkt10;
1076                 secondary_bkt21 = secondary_bkt11;
1077                 primary_hash20 = primary_hash10;
1078                 primary_hash21 = primary_hash11;
1079                 secondary_hash20 = secondary_hash10;
1080                 secondary_hash21 = secondary_hash11;
1081                 idx20 = idx10, idx21 = idx11;
1082                 idx10 = idx00, idx11 = idx01;
1083
1084                 lookup_stage0(&idx00, &lookup_mask, keys);
1085                 lookup_stage0(&idx01, &lookup_mask, keys);
1086                 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1087                         &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1088                 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1089                         &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1090                 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1091                         primary_bkt20, secondary_bkt20, &k_slot20, positions,
1092                         &extra_hits_mask, key_store, h);
1093                 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1094                         primary_bkt21, secondary_bkt21, &k_slot21, positions,
1095                         &extra_hits_mask, key_store, h);
1096                 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1097                 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1098         }
1099
1100         k_slot30 = k_slot20, k_slot31 = k_slot21;
1101         idx30 = idx20, idx31 = idx21;
1102         primary_bkt20 = primary_bkt10;
1103         primary_bkt21 = primary_bkt11;
1104         secondary_bkt20 = secondary_bkt10;
1105         secondary_bkt21 = secondary_bkt11;
1106         primary_hash20 = primary_hash10;
1107         primary_hash21 = primary_hash11;
1108         secondary_hash20 = secondary_hash10;
1109         secondary_hash21 = secondary_hash11;
1110         idx20 = idx10, idx21 = idx11;
1111         idx10 = idx00, idx11 = idx01;
1112
1113         lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1114                 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1115         lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1116                 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1117         lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1118                 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1119                 key_store, h);
1120         lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1121                 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1122                 key_store, h);
1123         lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1124         lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1125
1126         k_slot30 = k_slot20, k_slot31 = k_slot21;
1127         idx30 = idx20, idx31 = idx21;
1128         primary_bkt20 = primary_bkt10;
1129         primary_bkt21 = primary_bkt11;
1130         secondary_bkt20 = secondary_bkt10;
1131         secondary_bkt21 = secondary_bkt11;
1132         primary_hash20 = primary_hash10;
1133         primary_hash21 = primary_hash11;
1134         secondary_hash20 = secondary_hash10;
1135         secondary_hash21 = secondary_hash11;
1136         idx20 = idx10, idx21 = idx11;
1137
1138         lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1139                 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1140                 key_store, h);
1141         lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1142                 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1143                 key_store, h);
1144         lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1145         lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1146
1147         k_slot30 = k_slot20, k_slot31 = k_slot21;
1148         idx30 = idx20, idx31 = idx21;
1149
1150         lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1151         lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1152
1153         /* ignore any items we have already found */
1154         extra_hits_mask &= ~hits;
1155
1156         if (unlikely(extra_hits_mask)) {
1157                 /* run a single search for each remaining item */
1158                 do {
1159                         idx = __builtin_ctzl(extra_hits_mask);
1160                         if (data != NULL) {
1161                                 ret = rte_hash_lookup_with_hash_data(h,
1162                                                 keys[idx], hash_vals[idx], &data[idx]);
1163                                 if (ret >= 0)
1164                                         hits |= 1ULL << idx;
1165                         } else {
1166                                 positions[idx] = rte_hash_lookup_with_hash(h,
1167                                                         keys[idx], hash_vals[idx]);
1168                                 if (positions[idx] >= 0)
1169                                         hits |= 1llu << idx;
1170                         }
1171                         extra_hits_mask &= ~(1llu << idx);
1172                 } while (extra_hits_mask);
1173         }
1174
1175         miss_mask &= ~hits;
1176         if (unlikely(miss_mask)) {
1177                 do {
1178                         idx = __builtin_ctzl(miss_mask);
1179                         positions[idx] = -ENOENT;
1180                         miss_mask &= ~(1llu << idx);
1181                 } while (miss_mask);
1182         }
1183
1184         if (hit_mask != NULL)
1185                 *hit_mask = hits;
1186 }
1187
1188 int
1189 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1190                       uint32_t num_keys, int32_t *positions)
1191 {
1192         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1193                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1194                         (positions == NULL)), -EINVAL);
1195
1196         __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1197         return 0;
1198 }
1199
1200 int
1201 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1202                       uint32_t num_keys, uint64_t *hit_mask, void *data[])
1203 {
1204         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1205                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1206                         (hit_mask == NULL)), -EINVAL);
1207
1208         int32_t positions[num_keys];
1209
1210         __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1211
1212         /* Return number of hits */
1213         return __builtin_popcountl(*hit_mask);
1214 }
1215
1216 int32_t
1217 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1218 {
1219         uint32_t bucket_idx, idx, position;
1220         struct rte_hash_key *next_key;
1221
1222         RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1223
1224         const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1225         /* Out of bounds */
1226         if (*next >= total_entries)
1227                 return -ENOENT;
1228
1229         /* Calculate bucket and index of current iterator */
1230         bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1231         idx = *next % RTE_HASH_BUCKET_ENTRIES;
1232
1233         /* If current position is empty, go to the next one */
1234         while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1235                 (*next)++;
1236                 /* End of table */
1237                 if (*next == total_entries)
1238                         return -ENOENT;
1239                 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1240                 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1241         }
1242
1243         /* Get position of entry in key table */
1244         position = h->buckets[bucket_idx].key_idx[idx];
1245         next_key = (struct rte_hash_key *) ((char *)h->key_store +
1246                                 position * h->key_entry_size);
1247         /* Return key and data */
1248         *key = next_key->key;
1249         *data = next_key->pdata;
1250
1251         /* Increment iterator */
1252         (*next)++;
1253
1254         return position - 1;
1255 }