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