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