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