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