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