6af8ca42e94f38ffcf57c126ed6222f631252581
[dpdk.git] / lib / librte_hash / rte_cuckoo_hash.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  * Copyright(c) 2018 Arm Limited
4  */
5
6 #include <string.h>
7 #include <stdint.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <sys/queue.h>
12
13 #include <rte_common.h>
14 #include <rte_memory.h>         /* for definition of RTE_CACHE_LINE_SIZE */
15 #include <rte_log.h>
16 #include <rte_prefetch.h>
17 #include <rte_branch_prediction.h>
18 #include <rte_malloc.h>
19 #include <rte_eal.h>
20 #include <rte_eal_memconfig.h>
21 #include <rte_per_lcore.h>
22 #include <rte_errno.h>
23 #include <rte_string_fns.h>
24 #include <rte_cpuflags.h>
25 #include <rte_rwlock.h>
26 #include <rte_spinlock.h>
27 #include <rte_ring_elem.h>
28 #include <rte_compat.h>
29 #include <rte_vect.h>
30 #include <rte_tailq.h>
31
32 #include "rte_hash.h"
33 #include "rte_cuckoo_hash.h"
34
35 #define FOR_EACH_BUCKET(CURRENT_BKT, START_BUCKET)                            \
36         for (CURRENT_BKT = START_BUCKET;                                      \
37                 CURRENT_BKT != NULL;                                          \
38                 CURRENT_BKT = CURRENT_BKT->next)
39
40 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
41
42 static struct rte_tailq_elem rte_hash_tailq = {
43         .name = "RTE_HASH",
44 };
45 EAL_REGISTER_TAILQ(rte_hash_tailq)
46
47 struct rte_hash *
48 rte_hash_find_existing(const char *name)
49 {
50         struct rte_hash *h = NULL;
51         struct rte_tailq_entry *te;
52         struct rte_hash_list *hash_list;
53
54         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
55
56         rte_mcfg_tailq_read_lock();
57         TAILQ_FOREACH(te, hash_list, next) {
58                 h = (struct rte_hash *) te->data;
59                 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
60                         break;
61         }
62         rte_mcfg_tailq_read_unlock();
63
64         if (te == NULL) {
65                 rte_errno = ENOENT;
66                 return NULL;
67         }
68         return h;
69 }
70
71 static inline struct rte_hash_bucket *
72 rte_hash_get_last_bkt(struct rte_hash_bucket *lst_bkt)
73 {
74         while (lst_bkt->next != NULL)
75                 lst_bkt = lst_bkt->next;
76         return lst_bkt;
77 }
78
79 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
80 {
81         h->cmp_jump_table_idx = KEY_CUSTOM;
82         h->rte_hash_custom_cmp_eq = func;
83 }
84
85 static inline int
86 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
87 {
88         if (h->cmp_jump_table_idx == KEY_CUSTOM)
89                 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
90         else
91                 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
92 }
93
94 /*
95  * We use higher 16 bits of hash as the signature value stored in table.
96  * We use the lower bits for the primary bucket
97  * location. Then we XOR primary bucket location and the signature
98  * to get the secondary bucket location. This is same as
99  * proposed in Bin Fan, et al's paper
100  * "MemC3: Compact and Concurrent MemCache with Dumber Caching and
101  * Smarter Hashing". The benefit to use
102  * XOR is that one could derive the alternative bucket location
103  * by only using the current bucket location and the signature.
104  */
105 static inline uint16_t
106 get_short_sig(const hash_sig_t hash)
107 {
108         return hash >> 16;
109 }
110
111 static inline uint32_t
112 get_prim_bucket_index(const struct rte_hash *h, const hash_sig_t hash)
113 {
114         return hash & h->bucket_bitmask;
115 }
116
117 static inline uint32_t
118 get_alt_bucket_index(const struct rte_hash *h,
119                         uint32_t cur_bkt_idx, uint16_t sig)
120 {
121         return (cur_bkt_idx ^ sig) & h->bucket_bitmask;
122 }
123
124 struct rte_hash *
125 rte_hash_create(const struct rte_hash_parameters *params)
126 {
127         struct rte_hash *h = NULL;
128         struct rte_tailq_entry *te = NULL;
129         struct rte_hash_list *hash_list;
130         struct rte_ring *r = NULL;
131         struct rte_ring *r_ext = NULL;
132         char hash_name[RTE_HASH_NAMESIZE];
133         void *k = NULL;
134         void *buckets = NULL;
135         void *buckets_ext = NULL;
136         char ring_name[RTE_RING_NAMESIZE];
137         char ext_ring_name[RTE_RING_NAMESIZE];
138         unsigned num_key_slots;
139         unsigned int hw_trans_mem_support = 0, use_local_cache = 0;
140         unsigned int ext_table_support = 0;
141         unsigned int readwrite_concur_support = 0;
142         unsigned int writer_takes_lock = 0;
143         unsigned int no_free_on_del = 0;
144         uint32_t *ext_bkt_to_free = NULL;
145         uint32_t *tbl_chng_cnt = NULL;
146         unsigned int readwrite_concur_lf_support = 0;
147         uint32_t i;
148
149         rte_hash_function default_hash_func = (rte_hash_function)rte_jhash;
150
151         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
152
153         if (params == NULL) {
154                 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
155                 return NULL;
156         }
157
158         /* Check for valid parameters */
159         if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
160                         (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
161                         (params->key_len == 0)) {
162                 rte_errno = EINVAL;
163                 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
164                 return NULL;
165         }
166
167         /* Validate correct usage of extra options */
168         if ((params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) &&
169             (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF)) {
170                 rte_errno = EINVAL;
171                 RTE_LOG(ERR, HASH, "rte_hash_create: choose rw concurrency or "
172                         "rw concurrency lock free\n");
173                 return NULL;
174         }
175
176         /* Check extra flags field to check extra options. */
177         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
178                 hw_trans_mem_support = 1;
179
180         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
181                 use_local_cache = 1;
182                 writer_takes_lock = 1;
183         }
184
185         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) {
186                 readwrite_concur_support = 1;
187                 writer_takes_lock = 1;
188         }
189
190         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_EXT_TABLE)
191                 ext_table_support = 1;
192
193         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL)
194                 no_free_on_del = 1;
195
196         if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF) {
197                 readwrite_concur_lf_support = 1;
198                 /* Enable not freeing internal memory/index on delete */
199                 no_free_on_del = 1;
200         }
201
202         /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
203         if (use_local_cache)
204                 /*
205                  * Increase number of slots by total number of indices
206                  * that can be stored in the lcore caches
207                  * except for the first cache
208                  */
209                 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
210                                         (LCORE_CACHE_SIZE - 1) + 1;
211         else
212                 num_key_slots = params->entries + 1;
213
214         snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
215         /* Create ring (Dummy slot index is not enqueued) */
216         r = rte_ring_create_elem(ring_name, sizeof(uint32_t),
217                         rte_align32pow2(num_key_slots), params->socket_id, 0);
218         if (r == NULL) {
219                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
220                 goto err;
221         }
222
223         const uint32_t num_buckets = rte_align32pow2(params->entries) /
224                                                 RTE_HASH_BUCKET_ENTRIES;
225
226         /* Create ring for extendable buckets. */
227         if (ext_table_support) {
228                 snprintf(ext_ring_name, sizeof(ext_ring_name), "HT_EXT_%s",
229                                                                 params->name);
230                 r_ext = rte_ring_create_elem(ext_ring_name, sizeof(uint32_t),
231                                 rte_align32pow2(num_buckets + 1),
232                                 params->socket_id, 0);
233
234                 if (r_ext == NULL) {
235                         RTE_LOG(ERR, HASH, "ext buckets memory allocation "
236                                                                 "failed\n");
237                         goto err;
238                 }
239         }
240
241         snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
242
243         rte_mcfg_tailq_write_lock();
244
245         /* guarantee there's no existing: this is normally already checked
246          * by ring creation above */
247         TAILQ_FOREACH(te, hash_list, next) {
248                 h = (struct rte_hash *) te->data;
249                 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
250                         break;
251         }
252         h = NULL;
253         if (te != NULL) {
254                 rte_errno = EEXIST;
255                 te = NULL;
256                 goto err_unlock;
257         }
258
259         te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
260         if (te == NULL) {
261                 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
262                 goto err_unlock;
263         }
264
265         h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
266                                         RTE_CACHE_LINE_SIZE, params->socket_id);
267
268         if (h == NULL) {
269                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
270                 goto err_unlock;
271         }
272
273         buckets = rte_zmalloc_socket(NULL,
274                                 num_buckets * sizeof(struct rte_hash_bucket),
275                                 RTE_CACHE_LINE_SIZE, params->socket_id);
276
277         if (buckets == NULL) {
278                 RTE_LOG(ERR, HASH, "buckets memory allocation failed\n");
279                 goto err_unlock;
280         }
281
282         /* Allocate same number of extendable buckets */
283         if (ext_table_support) {
284                 buckets_ext = rte_zmalloc_socket(NULL,
285                                 num_buckets * sizeof(struct rte_hash_bucket),
286                                 RTE_CACHE_LINE_SIZE, params->socket_id);
287                 if (buckets_ext == NULL) {
288                         RTE_LOG(ERR, HASH, "ext buckets memory allocation "
289                                                         "failed\n");
290                         goto err_unlock;
291                 }
292                 /* Populate ext bkt ring. We reserve 0 similar to the
293                  * key-data slot, just in case in future we want to
294                  * use bucket index for the linked list and 0 means NULL
295                  * for next bucket
296                  */
297                 for (i = 1; i <= num_buckets; i++)
298                         rte_ring_sp_enqueue_elem(r_ext, &i, sizeof(uint32_t));
299
300                 if (readwrite_concur_lf_support) {
301                         ext_bkt_to_free = rte_zmalloc(NULL, sizeof(uint32_t) *
302                                                                 num_key_slots, 0);
303                         if (ext_bkt_to_free == NULL) {
304                                 RTE_LOG(ERR, HASH, "ext bkt to free memory allocation "
305                                                                 "failed\n");
306                                 goto err_unlock;
307                         }
308                 }
309         }
310
311         const uint32_t key_entry_size =
312                 RTE_ALIGN(sizeof(struct rte_hash_key) + params->key_len,
313                           KEY_ALIGNMENT);
314         const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
315
316         k = rte_zmalloc_socket(NULL, key_tbl_size,
317                         RTE_CACHE_LINE_SIZE, params->socket_id);
318
319         if (k == NULL) {
320                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
321                 goto err_unlock;
322         }
323
324         tbl_chng_cnt = rte_zmalloc_socket(NULL, sizeof(uint32_t),
325                         RTE_CACHE_LINE_SIZE, params->socket_id);
326
327         if (tbl_chng_cnt == NULL) {
328                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
329                 goto err_unlock;
330         }
331
332 /*
333  * If x86 architecture is used, select appropriate compare function,
334  * which may use x86 intrinsics, otherwise use memcmp
335  */
336 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
337         /* Select function to compare keys */
338         switch (params->key_len) {
339         case 16:
340                 h->cmp_jump_table_idx = KEY_16_BYTES;
341                 break;
342         case 32:
343                 h->cmp_jump_table_idx = KEY_32_BYTES;
344                 break;
345         case 48:
346                 h->cmp_jump_table_idx = KEY_48_BYTES;
347                 break;
348         case 64:
349                 h->cmp_jump_table_idx = KEY_64_BYTES;
350                 break;
351         case 80:
352                 h->cmp_jump_table_idx = KEY_80_BYTES;
353                 break;
354         case 96:
355                 h->cmp_jump_table_idx = KEY_96_BYTES;
356                 break;
357         case 112:
358                 h->cmp_jump_table_idx = KEY_112_BYTES;
359                 break;
360         case 128:
361                 h->cmp_jump_table_idx = KEY_128_BYTES;
362                 break;
363         default:
364                 /* If key is not multiple of 16, use generic memcmp */
365                 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
366         }
367 #else
368         h->cmp_jump_table_idx = KEY_OTHER_BYTES;
369 #endif
370
371         if (use_local_cache) {
372                 h->local_free_slots = rte_zmalloc_socket(NULL,
373                                 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
374                                 RTE_CACHE_LINE_SIZE, params->socket_id);
375         }
376
377         /* Default hash function */
378 #if defined(RTE_ARCH_X86)
379         default_hash_func = (rte_hash_function)rte_hash_crc;
380 #elif defined(RTE_ARCH_ARM64)
381         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
382                 default_hash_func = (rte_hash_function)rte_hash_crc;
383 #endif
384         /* Setup hash context */
385         strlcpy(h->name, params->name, sizeof(h->name));
386         h->entries = params->entries;
387         h->key_len = params->key_len;
388         h->key_entry_size = key_entry_size;
389         h->hash_func_init_val = params->hash_func_init_val;
390
391         h->num_buckets = num_buckets;
392         h->bucket_bitmask = h->num_buckets - 1;
393         h->buckets = buckets;
394         h->buckets_ext = buckets_ext;
395         h->free_ext_bkts = r_ext;
396         h->hash_func = (params->hash_func == NULL) ?
397                 default_hash_func : params->hash_func;
398         h->key_store = k;
399         h->free_slots = r;
400         h->ext_bkt_to_free = ext_bkt_to_free;
401         h->tbl_chng_cnt = tbl_chng_cnt;
402         *h->tbl_chng_cnt = 0;
403         h->hw_trans_mem_support = hw_trans_mem_support;
404         h->use_local_cache = use_local_cache;
405         h->readwrite_concur_support = readwrite_concur_support;
406         h->ext_table_support = ext_table_support;
407         h->writer_takes_lock = writer_takes_lock;
408         h->no_free_on_del = no_free_on_del;
409         h->readwrite_concur_lf_support = readwrite_concur_lf_support;
410
411 #if defined(RTE_ARCH_X86)
412         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
413                 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
414         else
415 #elif defined(RTE_ARCH_ARM64)
416         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
417                 h->sig_cmp_fn = RTE_HASH_COMPARE_NEON;
418         else
419 #endif
420                 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
421
422         /* Writer threads need to take the lock when:
423          * 1) RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY is enabled OR
424          * 2) RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD is enabled
425          */
426         if (h->writer_takes_lock) {
427                 h->readwrite_lock = rte_malloc(NULL, sizeof(rte_rwlock_t),
428                                                 RTE_CACHE_LINE_SIZE);
429                 if (h->readwrite_lock == NULL)
430                         goto err_unlock;
431
432                 rte_rwlock_init(h->readwrite_lock);
433         }
434
435         /* Populate free slots ring. Entry zero is reserved for key misses. */
436         for (i = 1; i < num_key_slots; i++)
437                 rte_ring_sp_enqueue_elem(r, &i, sizeof(uint32_t));
438
439         te->data = (void *) h;
440         TAILQ_INSERT_TAIL(hash_list, te, next);
441         rte_mcfg_tailq_write_unlock();
442
443         return h;
444 err_unlock:
445         rte_mcfg_tailq_write_unlock();
446 err:
447         rte_ring_free(r);
448         rte_ring_free(r_ext);
449         rte_free(te);
450         rte_free(h);
451         rte_free(buckets);
452         rte_free(buckets_ext);
453         rte_free(k);
454         rte_free(tbl_chng_cnt);
455         rte_free(ext_bkt_to_free);
456         return NULL;
457 }
458
459 void
460 rte_hash_free(struct rte_hash *h)
461 {
462         struct rte_tailq_entry *te;
463         struct rte_hash_list *hash_list;
464
465         if (h == NULL)
466                 return;
467
468         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
469
470         rte_mcfg_tailq_write_lock();
471
472         /* find out tailq entry */
473         TAILQ_FOREACH(te, hash_list, next) {
474                 if (te->data == (void *) h)
475                         break;
476         }
477
478         if (te == NULL) {
479                 rte_mcfg_tailq_write_unlock();
480                 return;
481         }
482
483         TAILQ_REMOVE(hash_list, te, next);
484
485         rte_mcfg_tailq_write_unlock();
486
487         if (h->use_local_cache)
488                 rte_free(h->local_free_slots);
489         if (h->writer_takes_lock)
490                 rte_free(h->readwrite_lock);
491         rte_ring_free(h->free_slots);
492         rte_ring_free(h->free_ext_bkts);
493         rte_free(h->key_store);
494         rte_free(h->buckets);
495         rte_free(h->buckets_ext);
496         rte_free(h->tbl_chng_cnt);
497         rte_free(h->ext_bkt_to_free);
498         rte_free(h);
499         rte_free(te);
500 }
501
502 hash_sig_t
503 rte_hash_hash(const struct rte_hash *h, const void *key)
504 {
505         /* calc hash result by key */
506         return h->hash_func(key, h->key_len, h->hash_func_init_val);
507 }
508
509 int32_t
510 rte_hash_max_key_id(const struct rte_hash *h)
511 {
512         RETURN_IF_TRUE((h == NULL), -EINVAL);
513         if (h->use_local_cache)
514                 /*
515                  * Increase number of slots by total number of indices
516                  * that can be stored in the lcore caches
517                  */
518                 return (h->entries + ((RTE_MAX_LCORE - 1) *
519                                         (LCORE_CACHE_SIZE - 1)));
520         else
521                 return h->entries;
522 }
523
524 int32_t
525 rte_hash_count(const struct rte_hash *h)
526 {
527         uint32_t tot_ring_cnt, cached_cnt = 0;
528         uint32_t i, ret;
529
530         if (h == NULL)
531                 return -EINVAL;
532
533         if (h->use_local_cache) {
534                 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
535                                         (LCORE_CACHE_SIZE - 1);
536                 for (i = 0; i < RTE_MAX_LCORE; i++)
537                         cached_cnt += h->local_free_slots[i].len;
538
539                 ret = tot_ring_cnt - rte_ring_count(h->free_slots) -
540                                                                 cached_cnt;
541         } else {
542                 tot_ring_cnt = h->entries;
543                 ret = tot_ring_cnt - rte_ring_count(h->free_slots);
544         }
545         return ret;
546 }
547
548 /* Read write locks implemented using rte_rwlock */
549 static inline void
550 __hash_rw_writer_lock(const struct rte_hash *h)
551 {
552         if (h->writer_takes_lock && h->hw_trans_mem_support)
553                 rte_rwlock_write_lock_tm(h->readwrite_lock);
554         else if (h->writer_takes_lock)
555                 rte_rwlock_write_lock(h->readwrite_lock);
556 }
557
558 static inline void
559 __hash_rw_reader_lock(const struct rte_hash *h)
560 {
561         if (h->readwrite_concur_support && h->hw_trans_mem_support)
562                 rte_rwlock_read_lock_tm(h->readwrite_lock);
563         else if (h->readwrite_concur_support)
564                 rte_rwlock_read_lock(h->readwrite_lock);
565 }
566
567 static inline void
568 __hash_rw_writer_unlock(const struct rte_hash *h)
569 {
570         if (h->writer_takes_lock && h->hw_trans_mem_support)
571                 rte_rwlock_write_unlock_tm(h->readwrite_lock);
572         else if (h->writer_takes_lock)
573                 rte_rwlock_write_unlock(h->readwrite_lock);
574 }
575
576 static inline void
577 __hash_rw_reader_unlock(const struct rte_hash *h)
578 {
579         if (h->readwrite_concur_support && h->hw_trans_mem_support)
580                 rte_rwlock_read_unlock_tm(h->readwrite_lock);
581         else if (h->readwrite_concur_support)
582                 rte_rwlock_read_unlock(h->readwrite_lock);
583 }
584
585 void
586 rte_hash_reset(struct rte_hash *h)
587 {
588         uint32_t tot_ring_cnt, i;
589
590         if (h == NULL)
591                 return;
592
593         __hash_rw_writer_lock(h);
594         memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
595         memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
596         *h->tbl_chng_cnt = 0;
597
598         /* reset the free ring */
599         rte_ring_reset(h->free_slots);
600
601         /* flush free extendable bucket ring and memory */
602         if (h->ext_table_support) {
603                 memset(h->buckets_ext, 0, h->num_buckets *
604                                                 sizeof(struct rte_hash_bucket));
605                 rte_ring_reset(h->free_ext_bkts);
606         }
607
608         /* Repopulate the free slots ring. Entry zero is reserved for key misses */
609         if (h->use_local_cache)
610                 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
611                                         (LCORE_CACHE_SIZE - 1);
612         else
613                 tot_ring_cnt = h->entries;
614
615         for (i = 1; i < tot_ring_cnt + 1; i++)
616                 rte_ring_sp_enqueue_elem(h->free_slots, &i, sizeof(uint32_t));
617
618         /* Repopulate the free ext bkt ring. */
619         if (h->ext_table_support) {
620                 for (i = 1; i <= h->num_buckets; i++)
621                         rte_ring_sp_enqueue_elem(h->free_ext_bkts, &i,
622                                                         sizeof(uint32_t));
623         }
624
625         if (h->use_local_cache) {
626                 /* Reset local caches per lcore */
627                 for (i = 0; i < RTE_MAX_LCORE; i++)
628                         h->local_free_slots[i].len = 0;
629         }
630         __hash_rw_writer_unlock(h);
631 }
632
633 /*
634  * Function called to enqueue back an index in the cache/ring,
635  * as slot has not being used and it can be used in the
636  * next addition attempt.
637  */
638 static inline void
639 enqueue_slot_back(const struct rte_hash *h,
640                 struct lcore_cache *cached_free_slots,
641                 uint32_t slot_id)
642 {
643         if (h->use_local_cache) {
644                 cached_free_slots->objs[cached_free_slots->len] = slot_id;
645                 cached_free_slots->len++;
646         } else
647                 rte_ring_sp_enqueue_elem(h->free_slots, &slot_id,
648                                                 sizeof(uint32_t));
649 }
650
651 /* Search a key from bucket and update its data.
652  * Writer holds the lock before calling this.
653  */
654 static inline int32_t
655 search_and_update(const struct rte_hash *h, void *data, const void *key,
656         struct rte_hash_bucket *bkt, uint16_t sig)
657 {
658         int i;
659         struct rte_hash_key *k, *keys = h->key_store;
660
661         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
662                 if (bkt->sig_current[i] == sig) {
663                         k = (struct rte_hash_key *) ((char *)keys +
664                                         bkt->key_idx[i] * h->key_entry_size);
665                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
666                                 /* The store to application data at *data
667                                  * should not leak after the store to pdata
668                                  * in the key store. i.e. pdata is the guard
669                                  * variable. Release the application data
670                                  * to the readers.
671                                  */
672                                 __atomic_store_n(&k->pdata,
673                                         data,
674                                         __ATOMIC_RELEASE);
675                                 /*
676                                  * Return index where key is stored,
677                                  * subtracting the first dummy index
678                                  */
679                                 return bkt->key_idx[i] - 1;
680                         }
681                 }
682         }
683         return -1;
684 }
685
686 /* Only tries to insert at one bucket (@prim_bkt) without trying to push
687  * buckets around.
688  * return 1 if matching existing key, return 0 if succeeds, return -1 for no
689  * empty entry.
690  */
691 static inline int32_t
692 rte_hash_cuckoo_insert_mw(const struct rte_hash *h,
693                 struct rte_hash_bucket *prim_bkt,
694                 struct rte_hash_bucket *sec_bkt,
695                 const struct rte_hash_key *key, void *data,
696                 uint16_t sig, uint32_t new_idx,
697                 int32_t *ret_val)
698 {
699         unsigned int i;
700         struct rte_hash_bucket *cur_bkt;
701         int32_t ret;
702
703         __hash_rw_writer_lock(h);
704         /* Check if key was inserted after last check but before this
705          * protected region in case of inserting duplicated keys.
706          */
707         ret = search_and_update(h, data, key, prim_bkt, sig);
708         if (ret != -1) {
709                 __hash_rw_writer_unlock(h);
710                 *ret_val = ret;
711                 return 1;
712         }
713
714         FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
715                 ret = search_and_update(h, data, key, cur_bkt, sig);
716                 if (ret != -1) {
717                         __hash_rw_writer_unlock(h);
718                         *ret_val = ret;
719                         return 1;
720                 }
721         }
722
723         /* Insert new entry if there is room in the primary
724          * bucket.
725          */
726         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
727                 /* Check if slot is available */
728                 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
729                         prim_bkt->sig_current[i] = sig;
730                         /* Store to signature and key should not
731                          * leak after the store to key_idx. i.e.
732                          * key_idx is the guard variable for signature
733                          * and key.
734                          */
735                         __atomic_store_n(&prim_bkt->key_idx[i],
736                                          new_idx,
737                                          __ATOMIC_RELEASE);
738                         break;
739                 }
740         }
741         __hash_rw_writer_unlock(h);
742
743         if (i != RTE_HASH_BUCKET_ENTRIES)
744                 return 0;
745
746         /* no empty entry */
747         return -1;
748 }
749
750 /* Shift buckets along provided cuckoo_path (@leaf and @leaf_slot) and fill
751  * the path head with new entry (sig, alt_hash, new_idx)
752  * return 1 if matched key found, return -1 if cuckoo path invalided and fail,
753  * return 0 if succeeds.
754  */
755 static inline int
756 rte_hash_cuckoo_move_insert_mw(const struct rte_hash *h,
757                         struct rte_hash_bucket *bkt,
758                         struct rte_hash_bucket *alt_bkt,
759                         const struct rte_hash_key *key, void *data,
760                         struct queue_node *leaf, uint32_t leaf_slot,
761                         uint16_t sig, uint32_t new_idx,
762                         int32_t *ret_val)
763 {
764         uint32_t prev_alt_bkt_idx;
765         struct rte_hash_bucket *cur_bkt;
766         struct queue_node *prev_node, *curr_node = leaf;
767         struct rte_hash_bucket *prev_bkt, *curr_bkt = leaf->bkt;
768         uint32_t prev_slot, curr_slot = leaf_slot;
769         int32_t ret;
770
771         __hash_rw_writer_lock(h);
772
773         /* In case empty slot was gone before entering protected region */
774         if (curr_bkt->key_idx[curr_slot] != EMPTY_SLOT) {
775                 __hash_rw_writer_unlock(h);
776                 return -1;
777         }
778
779         /* Check if key was inserted after last check but before this
780          * protected region.
781          */
782         ret = search_and_update(h, data, key, bkt, sig);
783         if (ret != -1) {
784                 __hash_rw_writer_unlock(h);
785                 *ret_val = ret;
786                 return 1;
787         }
788
789         FOR_EACH_BUCKET(cur_bkt, alt_bkt) {
790                 ret = search_and_update(h, data, key, cur_bkt, sig);
791                 if (ret != -1) {
792                         __hash_rw_writer_unlock(h);
793                         *ret_val = ret;
794                         return 1;
795                 }
796         }
797
798         while (likely(curr_node->prev != NULL)) {
799                 prev_node = curr_node->prev;
800                 prev_bkt = prev_node->bkt;
801                 prev_slot = curr_node->prev_slot;
802
803                 prev_alt_bkt_idx = get_alt_bucket_index(h,
804                                         prev_node->cur_bkt_idx,
805                                         prev_bkt->sig_current[prev_slot]);
806
807                 if (unlikely(&h->buckets[prev_alt_bkt_idx]
808                                 != curr_bkt)) {
809                         /* revert it to empty, otherwise duplicated keys */
810                         __atomic_store_n(&curr_bkt->key_idx[curr_slot],
811                                 EMPTY_SLOT,
812                                 __ATOMIC_RELEASE);
813                         __hash_rw_writer_unlock(h);
814                         return -1;
815                 }
816
817                 if (h->readwrite_concur_lf_support) {
818                         /* Inform the previous move. The current move need
819                          * not be informed now as the current bucket entry
820                          * is present in both primary and secondary.
821                          * Since there is one writer, load acquires on
822                          * tbl_chng_cnt are not required.
823                          */
824                         __atomic_store_n(h->tbl_chng_cnt,
825                                          *h->tbl_chng_cnt + 1,
826                                          __ATOMIC_RELEASE);
827                         /* The store to sig_current should not
828                          * move above the store to tbl_chng_cnt.
829                          */
830                         __atomic_thread_fence(__ATOMIC_RELEASE);
831                 }
832
833                 /* Need to swap current/alt sig to allow later
834                  * Cuckoo insert to move elements back to its
835                  * primary bucket if available
836                  */
837                 curr_bkt->sig_current[curr_slot] =
838                         prev_bkt->sig_current[prev_slot];
839                 /* Release the updated bucket entry */
840                 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
841                         prev_bkt->key_idx[prev_slot],
842                         __ATOMIC_RELEASE);
843
844                 curr_slot = prev_slot;
845                 curr_node = prev_node;
846                 curr_bkt = curr_node->bkt;
847         }
848
849         if (h->readwrite_concur_lf_support) {
850                 /* Inform the previous move. The current move need
851                  * not be informed now as the current bucket entry
852                  * is present in both primary and secondary.
853                  * Since there is one writer, load acquires on
854                  * tbl_chng_cnt are not required.
855                  */
856                 __atomic_store_n(h->tbl_chng_cnt,
857                                  *h->tbl_chng_cnt + 1,
858                                  __ATOMIC_RELEASE);
859                 /* The store to sig_current should not
860                  * move above the store to tbl_chng_cnt.
861                  */
862                 __atomic_thread_fence(__ATOMIC_RELEASE);
863         }
864
865         curr_bkt->sig_current[curr_slot] = sig;
866         /* Release the new bucket entry */
867         __atomic_store_n(&curr_bkt->key_idx[curr_slot],
868                          new_idx,
869                          __ATOMIC_RELEASE);
870
871         __hash_rw_writer_unlock(h);
872
873         return 0;
874
875 }
876
877 /*
878  * Make space for new key, using bfs Cuckoo Search and Multi-Writer safe
879  * Cuckoo
880  */
881 static inline int
882 rte_hash_cuckoo_make_space_mw(const struct rte_hash *h,
883                         struct rte_hash_bucket *bkt,
884                         struct rte_hash_bucket *sec_bkt,
885                         const struct rte_hash_key *key, void *data,
886                         uint16_t sig, uint32_t bucket_idx,
887                         uint32_t new_idx, int32_t *ret_val)
888 {
889         unsigned int i;
890         struct queue_node queue[RTE_HASH_BFS_QUEUE_MAX_LEN];
891         struct queue_node *tail, *head;
892         struct rte_hash_bucket *curr_bkt, *alt_bkt;
893         uint32_t cur_idx, alt_idx;
894
895         tail = queue;
896         head = queue + 1;
897         tail->bkt = bkt;
898         tail->prev = NULL;
899         tail->prev_slot = -1;
900         tail->cur_bkt_idx = bucket_idx;
901
902         /* Cuckoo bfs Search */
903         while (likely(tail != head && head <
904                                         queue + RTE_HASH_BFS_QUEUE_MAX_LEN -
905                                         RTE_HASH_BUCKET_ENTRIES)) {
906                 curr_bkt = tail->bkt;
907                 cur_idx = tail->cur_bkt_idx;
908                 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
909                         if (curr_bkt->key_idx[i] == EMPTY_SLOT) {
910                                 int32_t ret = rte_hash_cuckoo_move_insert_mw(h,
911                                                 bkt, sec_bkt, key, data,
912                                                 tail, i, sig,
913                                                 new_idx, ret_val);
914                                 if (likely(ret != -1))
915                                         return ret;
916                         }
917
918                         /* Enqueue new node and keep prev node info */
919                         alt_idx = get_alt_bucket_index(h, cur_idx,
920                                                 curr_bkt->sig_current[i]);
921                         alt_bkt = &(h->buckets[alt_idx]);
922                         head->bkt = alt_bkt;
923                         head->cur_bkt_idx = alt_idx;
924                         head->prev = tail;
925                         head->prev_slot = i;
926                         head++;
927                 }
928                 tail++;
929         }
930
931         return -ENOSPC;
932 }
933
934 static inline int32_t
935 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
936                                                 hash_sig_t sig, void *data)
937 {
938         uint16_t short_sig;
939         uint32_t prim_bucket_idx, sec_bucket_idx;
940         struct rte_hash_bucket *prim_bkt, *sec_bkt, *cur_bkt;
941         struct rte_hash_key *new_k, *keys = h->key_store;
942         uint32_t slot_id;
943         uint32_t ext_bkt_id;
944         int ret;
945         unsigned n_slots;
946         unsigned lcore_id;
947         unsigned int i;
948         struct lcore_cache *cached_free_slots = NULL;
949         int32_t ret_val;
950         struct rte_hash_bucket *last;
951
952         short_sig = get_short_sig(sig);
953         prim_bucket_idx = get_prim_bucket_index(h, sig);
954         sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
955         prim_bkt = &h->buckets[prim_bucket_idx];
956         sec_bkt = &h->buckets[sec_bucket_idx];
957         rte_prefetch0(prim_bkt);
958         rte_prefetch0(sec_bkt);
959
960         /* Check if key is already inserted in primary location */
961         __hash_rw_writer_lock(h);
962         ret = search_and_update(h, data, key, prim_bkt, short_sig);
963         if (ret != -1) {
964                 __hash_rw_writer_unlock(h);
965                 return ret;
966         }
967
968         /* Check if key is already inserted in secondary location */
969         FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
970                 ret = search_and_update(h, data, key, cur_bkt, short_sig);
971                 if (ret != -1) {
972                         __hash_rw_writer_unlock(h);
973                         return ret;
974                 }
975         }
976
977         __hash_rw_writer_unlock(h);
978
979         /* Did not find a match, so get a new slot for storing the new key */
980         if (h->use_local_cache) {
981                 lcore_id = rte_lcore_id();
982                 cached_free_slots = &h->local_free_slots[lcore_id];
983                 /* Try to get a free slot from the local cache */
984                 if (cached_free_slots->len == 0) {
985                         /* Need to get another burst of free slots from global ring */
986                         n_slots = rte_ring_mc_dequeue_burst_elem(h->free_slots,
987                                         cached_free_slots->objs,
988                                         sizeof(uint32_t),
989                                         LCORE_CACHE_SIZE, NULL);
990                         if (n_slots == 0) {
991                                 return -ENOSPC;
992                         }
993
994                         cached_free_slots->len += n_slots;
995                 }
996
997                 /* Get a free slot from the local cache */
998                 cached_free_slots->len--;
999                 slot_id = cached_free_slots->objs[cached_free_slots->len];
1000         } else {
1001                 if (rte_ring_sc_dequeue_elem(h->free_slots, &slot_id,
1002                                                 sizeof(uint32_t)) != 0) {
1003                         return -ENOSPC;
1004                 }
1005         }
1006
1007         new_k = RTE_PTR_ADD(keys, slot_id * h->key_entry_size);
1008         /* The store to application data (by the application) at *data should
1009          * not leak after the store of pdata in the key store. i.e. pdata is
1010          * the guard variable. Release the application data to the readers.
1011          */
1012         __atomic_store_n(&new_k->pdata,
1013                 data,
1014                 __ATOMIC_RELEASE);
1015         /* Copy key */
1016         memcpy(new_k->key, key, h->key_len);
1017
1018         /* Find an empty slot and insert */
1019         ret = rte_hash_cuckoo_insert_mw(h, prim_bkt, sec_bkt, key, data,
1020                                         short_sig, slot_id, &ret_val);
1021         if (ret == 0)
1022                 return slot_id - 1;
1023         else if (ret == 1) {
1024                 enqueue_slot_back(h, cached_free_slots, slot_id);
1025                 return ret_val;
1026         }
1027
1028         /* Primary bucket full, need to make space for new entry */
1029         ret = rte_hash_cuckoo_make_space_mw(h, prim_bkt, sec_bkt, key, data,
1030                                 short_sig, prim_bucket_idx, slot_id, &ret_val);
1031         if (ret == 0)
1032                 return slot_id - 1;
1033         else if (ret == 1) {
1034                 enqueue_slot_back(h, cached_free_slots, slot_id);
1035                 return ret_val;
1036         }
1037
1038         /* Also search secondary bucket to get better occupancy */
1039         ret = rte_hash_cuckoo_make_space_mw(h, sec_bkt, prim_bkt, key, data,
1040                                 short_sig, sec_bucket_idx, slot_id, &ret_val);
1041
1042         if (ret == 0)
1043                 return slot_id - 1;
1044         else if (ret == 1) {
1045                 enqueue_slot_back(h, cached_free_slots, slot_id);
1046                 return ret_val;
1047         }
1048
1049         /* if ext table not enabled, we failed the insertion */
1050         if (!h->ext_table_support) {
1051                 enqueue_slot_back(h, cached_free_slots, slot_id);
1052                 return ret;
1053         }
1054
1055         /* Now we need to go through the extendable bucket. Protection is needed
1056          * to protect all extendable bucket processes.
1057          */
1058         __hash_rw_writer_lock(h);
1059         /* We check for duplicates again since could be inserted before the lock */
1060         ret = search_and_update(h, data, key, prim_bkt, short_sig);
1061         if (ret != -1) {
1062                 enqueue_slot_back(h, cached_free_slots, slot_id);
1063                 goto failure;
1064         }
1065
1066         FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1067                 ret = search_and_update(h, data, key, cur_bkt, short_sig);
1068                 if (ret != -1) {
1069                         enqueue_slot_back(h, cached_free_slots, slot_id);
1070                         goto failure;
1071                 }
1072         }
1073
1074         /* Search sec and ext buckets to find an empty entry to insert. */
1075         FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1076                 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1077                         /* Check if slot is available */
1078                         if (likely(cur_bkt->key_idx[i] == EMPTY_SLOT)) {
1079                                 cur_bkt->sig_current[i] = short_sig;
1080                                 /* Store to signature and key should not
1081                                  * leak after the store to key_idx. i.e.
1082                                  * key_idx is the guard variable for signature
1083                                  * and key.
1084                                  */
1085                                 __atomic_store_n(&cur_bkt->key_idx[i],
1086                                                  slot_id,
1087                                                  __ATOMIC_RELEASE);
1088                                 __hash_rw_writer_unlock(h);
1089                                 return slot_id - 1;
1090                         }
1091                 }
1092         }
1093
1094         /* Failed to get an empty entry from extendable buckets. Link a new
1095          * extendable bucket. We first get a free bucket from ring.
1096          */
1097         if (rte_ring_sc_dequeue_elem(h->free_ext_bkts, &ext_bkt_id,
1098                                                 sizeof(uint32_t)) != 0) {
1099                 ret = -ENOSPC;
1100                 goto failure;
1101         }
1102
1103         /* Use the first location of the new bucket */
1104         (h->buckets_ext[ext_bkt_id - 1]).sig_current[0] = short_sig;
1105         /* Store to signature and key should not leak after
1106          * the store to key_idx. i.e. key_idx is the guard variable
1107          * for signature and key.
1108          */
1109         __atomic_store_n(&(h->buckets_ext[ext_bkt_id - 1]).key_idx[0],
1110                          slot_id,
1111                          __ATOMIC_RELEASE);
1112         /* Link the new bucket to sec bucket linked list */
1113         last = rte_hash_get_last_bkt(sec_bkt);
1114         last->next = &h->buckets_ext[ext_bkt_id - 1];
1115         __hash_rw_writer_unlock(h);
1116         return slot_id - 1;
1117
1118 failure:
1119         __hash_rw_writer_unlock(h);
1120         return ret;
1121
1122 }
1123
1124 int32_t
1125 rte_hash_add_key_with_hash(const struct rte_hash *h,
1126                         const void *key, hash_sig_t sig)
1127 {
1128         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1129         return __rte_hash_add_key_with_hash(h, key, sig, 0);
1130 }
1131
1132 int32_t
1133 rte_hash_add_key(const struct rte_hash *h, const void *key)
1134 {
1135         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1136         return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
1137 }
1138
1139 int
1140 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
1141                         const void *key, hash_sig_t sig, void *data)
1142 {
1143         int ret;
1144
1145         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1146         ret = __rte_hash_add_key_with_hash(h, key, sig, data);
1147         if (ret >= 0)
1148                 return 0;
1149         else
1150                 return ret;
1151 }
1152
1153 int
1154 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
1155 {
1156         int ret;
1157
1158         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1159
1160         ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
1161         if (ret >= 0)
1162                 return 0;
1163         else
1164                 return ret;
1165 }
1166
1167 /* Search one bucket to find the match key - uses rw lock */
1168 static inline int32_t
1169 search_one_bucket_l(const struct rte_hash *h, const void *key,
1170                 uint16_t sig, void **data,
1171                 const struct rte_hash_bucket *bkt)
1172 {
1173         int i;
1174         struct rte_hash_key *k, *keys = h->key_store;
1175
1176         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1177                 if (bkt->sig_current[i] == sig &&
1178                                 bkt->key_idx[i] != EMPTY_SLOT) {
1179                         k = (struct rte_hash_key *) ((char *)keys +
1180                                         bkt->key_idx[i] * h->key_entry_size);
1181
1182                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1183                                 if (data != NULL)
1184                                         *data = k->pdata;
1185                                 /*
1186                                  * Return index where key is stored,
1187                                  * subtracting the first dummy index
1188                                  */
1189                                 return bkt->key_idx[i] - 1;
1190                         }
1191                 }
1192         }
1193         return -1;
1194 }
1195
1196 /* Search one bucket to find the match key */
1197 static inline int32_t
1198 search_one_bucket_lf(const struct rte_hash *h, const void *key, uint16_t sig,
1199                         void **data, const struct rte_hash_bucket *bkt)
1200 {
1201         int i;
1202         uint32_t key_idx;
1203         struct rte_hash_key *k, *keys = h->key_store;
1204
1205         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1206                 /* Signature comparison is done before the acquire-load
1207                  * of the key index to achieve better performance.
1208                  * This can result in the reader loading old signature
1209                  * (which matches), while the key_idx is updated to a
1210                  * value that belongs to a new key. However, the full
1211                  * key comparison will ensure that the lookup fails.
1212                  */
1213                 if (bkt->sig_current[i] == sig) {
1214                         key_idx = __atomic_load_n(&bkt->key_idx[i],
1215                                           __ATOMIC_ACQUIRE);
1216                         if (key_idx != EMPTY_SLOT) {
1217                                 k = (struct rte_hash_key *) ((char *)keys +
1218                                                 key_idx * h->key_entry_size);
1219
1220                                 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1221                                         if (data != NULL) {
1222                                                 *data = __atomic_load_n(
1223                                                         &k->pdata,
1224                                                         __ATOMIC_ACQUIRE);
1225                                         }
1226                                         /*
1227                                          * Return index where key is stored,
1228                                          * subtracting the first dummy index
1229                                          */
1230                                         return key_idx - 1;
1231                                 }
1232                         }
1233                 }
1234         }
1235         return -1;
1236 }
1237
1238 static inline int32_t
1239 __rte_hash_lookup_with_hash_l(const struct rte_hash *h, const void *key,
1240                                 hash_sig_t sig, void **data)
1241 {
1242         uint32_t prim_bucket_idx, sec_bucket_idx;
1243         struct rte_hash_bucket *bkt, *cur_bkt;
1244         int ret;
1245         uint16_t short_sig;
1246
1247         short_sig = get_short_sig(sig);
1248         prim_bucket_idx = get_prim_bucket_index(h, sig);
1249         sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1250
1251         bkt = &h->buckets[prim_bucket_idx];
1252
1253         __hash_rw_reader_lock(h);
1254
1255         /* Check if key is in primary location */
1256         ret = search_one_bucket_l(h, key, short_sig, data, bkt);
1257         if (ret != -1) {
1258                 __hash_rw_reader_unlock(h);
1259                 return ret;
1260         }
1261         /* Calculate secondary hash */
1262         bkt = &h->buckets[sec_bucket_idx];
1263
1264         /* Check if key is in secondary location */
1265         FOR_EACH_BUCKET(cur_bkt, bkt) {
1266                 ret = search_one_bucket_l(h, key, short_sig,
1267                                         data, cur_bkt);
1268                 if (ret != -1) {
1269                         __hash_rw_reader_unlock(h);
1270                         return ret;
1271                 }
1272         }
1273
1274         __hash_rw_reader_unlock(h);
1275
1276         return -ENOENT;
1277 }
1278
1279 static inline int32_t
1280 __rte_hash_lookup_with_hash_lf(const struct rte_hash *h, const void *key,
1281                                         hash_sig_t sig, void **data)
1282 {
1283         uint32_t prim_bucket_idx, sec_bucket_idx;
1284         struct rte_hash_bucket *bkt, *cur_bkt;
1285         uint32_t cnt_b, cnt_a;
1286         int ret;
1287         uint16_t short_sig;
1288
1289         short_sig = get_short_sig(sig);
1290         prim_bucket_idx = get_prim_bucket_index(h, sig);
1291         sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1292
1293         do {
1294                 /* Load the table change counter before the lookup
1295                  * starts. Acquire semantics will make sure that
1296                  * loads in search_one_bucket are not hoisted.
1297                  */
1298                 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1299                                 __ATOMIC_ACQUIRE);
1300
1301                 /* Check if key is in primary location */
1302                 bkt = &h->buckets[prim_bucket_idx];
1303                 ret = search_one_bucket_lf(h, key, short_sig, data, bkt);
1304                 if (ret != -1)
1305                         return ret;
1306                 /* Calculate secondary hash */
1307                 bkt = &h->buckets[sec_bucket_idx];
1308
1309                 /* Check if key is in secondary location */
1310                 FOR_EACH_BUCKET(cur_bkt, bkt) {
1311                         ret = search_one_bucket_lf(h, key, short_sig,
1312                                                 data, cur_bkt);
1313                         if (ret != -1)
1314                                 return ret;
1315                 }
1316
1317                 /* The loads of sig_current in search_one_bucket
1318                  * should not move below the load from tbl_chng_cnt.
1319                  */
1320                 __atomic_thread_fence(__ATOMIC_ACQUIRE);
1321                 /* Re-read the table change counter to check if the
1322                  * table has changed during search. If yes, re-do
1323                  * the search.
1324                  * This load should not get hoisted. The load
1325                  * acquires on cnt_b, key index in primary bucket
1326                  * and key index in secondary bucket will make sure
1327                  * that it does not get hoisted.
1328                  */
1329                 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
1330                                         __ATOMIC_ACQUIRE);
1331         } while (cnt_b != cnt_a);
1332
1333         return -ENOENT;
1334 }
1335
1336 static inline int32_t
1337 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
1338                                         hash_sig_t sig, void **data)
1339 {
1340         if (h->readwrite_concur_lf_support)
1341                 return __rte_hash_lookup_with_hash_lf(h, key, sig, data);
1342         else
1343                 return __rte_hash_lookup_with_hash_l(h, key, sig, data);
1344 }
1345
1346 int32_t
1347 rte_hash_lookup_with_hash(const struct rte_hash *h,
1348                         const void *key, hash_sig_t sig)
1349 {
1350         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1351         return __rte_hash_lookup_with_hash(h, key, sig, NULL);
1352 }
1353
1354 int32_t
1355 rte_hash_lookup(const struct rte_hash *h, const void *key)
1356 {
1357         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1358         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
1359 }
1360
1361 int
1362 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
1363                         const void *key, hash_sig_t sig, void **data)
1364 {
1365         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1366         return __rte_hash_lookup_with_hash(h, key, sig, data);
1367 }
1368
1369 int
1370 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
1371 {
1372         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1373         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
1374 }
1375
1376 static inline void
1377 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
1378 {
1379         unsigned lcore_id, n_slots;
1380         struct lcore_cache *cached_free_slots;
1381
1382         if (h->use_local_cache) {
1383                 lcore_id = rte_lcore_id();
1384                 cached_free_slots = &h->local_free_slots[lcore_id];
1385                 /* Cache full, need to free it. */
1386                 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1387                         /* Need to enqueue the free slots in global ring. */
1388                         n_slots = rte_ring_mp_enqueue_burst_elem(h->free_slots,
1389                                                 cached_free_slots->objs,
1390                                                 sizeof(uint32_t),
1391                                                 LCORE_CACHE_SIZE, NULL);
1392                         ERR_IF_TRUE((n_slots == 0),
1393                                 "%s: could not enqueue free slots in global ring\n",
1394                                 __func__);
1395                         cached_free_slots->len -= n_slots;
1396                 }
1397                 /* Put index of new free slot in cache. */
1398                 cached_free_slots->objs[cached_free_slots->len] =
1399                                                         bkt->key_idx[i];
1400                 cached_free_slots->len++;
1401         } else {
1402                 rte_ring_sp_enqueue_elem(h->free_slots,
1403                                 &bkt->key_idx[i], sizeof(uint32_t));
1404         }
1405 }
1406
1407 /* Compact the linked list by moving key from last entry in linked list to the
1408  * empty slot.
1409  */
1410 static inline void
1411 __rte_hash_compact_ll(const struct rte_hash *h,
1412                         struct rte_hash_bucket *cur_bkt, int pos) {
1413         int i;
1414         struct rte_hash_bucket *last_bkt;
1415
1416         if (!cur_bkt->next)
1417                 return;
1418
1419         last_bkt = rte_hash_get_last_bkt(cur_bkt);
1420
1421         for (i = RTE_HASH_BUCKET_ENTRIES - 1; i >= 0; i--) {
1422                 if (last_bkt->key_idx[i] != EMPTY_SLOT) {
1423                         cur_bkt->sig_current[pos] = last_bkt->sig_current[i];
1424                         __atomic_store_n(&cur_bkt->key_idx[pos],
1425                                          last_bkt->key_idx[i],
1426                                          __ATOMIC_RELEASE);
1427                         if (h->readwrite_concur_lf_support) {
1428                                 /* Inform the readers that the table has changed
1429                                  * Since there is one writer, load acquire on
1430                                  * tbl_chng_cnt is not required.
1431                                  */
1432                                 __atomic_store_n(h->tbl_chng_cnt,
1433                                          *h->tbl_chng_cnt + 1,
1434                                          __ATOMIC_RELEASE);
1435                                 /* The store to sig_current should
1436                                  * not move above the store to tbl_chng_cnt.
1437                                  */
1438                                 __atomic_thread_fence(__ATOMIC_RELEASE);
1439                         }
1440                         last_bkt->sig_current[i] = NULL_SIGNATURE;
1441                         __atomic_store_n(&last_bkt->key_idx[i],
1442                                          EMPTY_SLOT,
1443                                          __ATOMIC_RELEASE);
1444                         return;
1445                 }
1446         }
1447 }
1448
1449 /* Search one bucket and remove the matched key.
1450  * Writer is expected to hold the lock while calling this
1451  * function.
1452  */
1453 static inline int32_t
1454 search_and_remove(const struct rte_hash *h, const void *key,
1455                         struct rte_hash_bucket *bkt, uint16_t sig, int *pos)
1456 {
1457         struct rte_hash_key *k, *keys = h->key_store;
1458         unsigned int i;
1459         uint32_t key_idx;
1460
1461         /* Check if key is in bucket */
1462         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1463                 key_idx = __atomic_load_n(&bkt->key_idx[i],
1464                                           __ATOMIC_ACQUIRE);
1465                 if (bkt->sig_current[i] == sig && key_idx != EMPTY_SLOT) {
1466                         k = (struct rte_hash_key *) ((char *)keys +
1467                                         key_idx * h->key_entry_size);
1468                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1469                                 bkt->sig_current[i] = NULL_SIGNATURE;
1470                                 /* Free the key store index if
1471                                  * no_free_on_del is disabled.
1472                                  */
1473                                 if (!h->no_free_on_del)
1474                                         remove_entry(h, bkt, i);
1475
1476                                 __atomic_store_n(&bkt->key_idx[i],
1477                                                  EMPTY_SLOT,
1478                                                  __ATOMIC_RELEASE);
1479
1480                                 *pos = i;
1481                                 /*
1482                                  * Return index where key is stored,
1483                                  * subtracting the first dummy index
1484                                  */
1485                                 return key_idx - 1;
1486                         }
1487                 }
1488         }
1489         return -1;
1490 }
1491
1492 static inline int32_t
1493 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
1494                                                 hash_sig_t sig)
1495 {
1496         uint32_t prim_bucket_idx, sec_bucket_idx;
1497         struct rte_hash_bucket *prim_bkt, *sec_bkt, *prev_bkt, *last_bkt;
1498         struct rte_hash_bucket *cur_bkt;
1499         int pos;
1500         int32_t ret, i;
1501         uint16_t short_sig;
1502
1503         short_sig = get_short_sig(sig);
1504         prim_bucket_idx = get_prim_bucket_index(h, sig);
1505         sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1506         prim_bkt = &h->buckets[prim_bucket_idx];
1507
1508         __hash_rw_writer_lock(h);
1509         /* look for key in primary bucket */
1510         ret = search_and_remove(h, key, prim_bkt, short_sig, &pos);
1511         if (ret != -1) {
1512                 __rte_hash_compact_ll(h, prim_bkt, pos);
1513                 last_bkt = prim_bkt->next;
1514                 prev_bkt = prim_bkt;
1515                 goto return_bkt;
1516         }
1517
1518         /* Calculate secondary hash */
1519         sec_bkt = &h->buckets[sec_bucket_idx];
1520
1521         FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1522                 ret = search_and_remove(h, key, cur_bkt, short_sig, &pos);
1523                 if (ret != -1) {
1524                         __rte_hash_compact_ll(h, cur_bkt, pos);
1525                         last_bkt = sec_bkt->next;
1526                         prev_bkt = sec_bkt;
1527                         goto return_bkt;
1528                 }
1529         }
1530
1531         __hash_rw_writer_unlock(h);
1532         return -ENOENT;
1533
1534 /* Search last bucket to see if empty to be recycled */
1535 return_bkt:
1536         if (!last_bkt) {
1537                 __hash_rw_writer_unlock(h);
1538                 return ret;
1539         }
1540         while (last_bkt->next) {
1541                 prev_bkt = last_bkt;
1542                 last_bkt = last_bkt->next;
1543         }
1544
1545         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1546                 if (last_bkt->key_idx[i] != EMPTY_SLOT)
1547                         break;
1548         }
1549         /* found empty bucket and recycle */
1550         if (i == RTE_HASH_BUCKET_ENTRIES) {
1551                 prev_bkt->next = NULL;
1552                 uint32_t index = last_bkt - h->buckets_ext + 1;
1553                 /* Recycle the empty bkt if
1554                  * no_free_on_del is disabled.
1555                  */
1556                 if (h->no_free_on_del)
1557                         /* Store index of an empty ext bkt to be recycled
1558                          * on calling rte_hash_del_xxx APIs.
1559                          * When lock free read-write concurrency is enabled,
1560                          * an empty ext bkt cannot be put into free list
1561                          * immediately (as readers might be using it still).
1562                          * Hence freeing of the ext bkt is piggy-backed to
1563                          * freeing of the key index.
1564                          */
1565                         h->ext_bkt_to_free[ret] = index;
1566                 else
1567                         rte_ring_sp_enqueue_elem(h->free_ext_bkts, &index,
1568                                                         sizeof(uint32_t));
1569         }
1570         __hash_rw_writer_unlock(h);
1571         return ret;
1572 }
1573
1574 int32_t
1575 rte_hash_del_key_with_hash(const struct rte_hash *h,
1576                         const void *key, hash_sig_t sig)
1577 {
1578         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1579         return __rte_hash_del_key_with_hash(h, key, sig);
1580 }
1581
1582 int32_t
1583 rte_hash_del_key(const struct rte_hash *h, const void *key)
1584 {
1585         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1586         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
1587 }
1588
1589 int
1590 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
1591                                void **key)
1592 {
1593         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1594
1595         struct rte_hash_key *k, *keys = h->key_store;
1596         k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
1597                                      h->key_entry_size);
1598         *key = k->key;
1599
1600         if (position !=
1601             __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
1602                                         NULL)) {
1603                 return -ENOENT;
1604         }
1605
1606         return 0;
1607 }
1608
1609 int
1610 rte_hash_free_key_with_position(const struct rte_hash *h,
1611                                 const int32_t position)
1612 {
1613         /* Key index where key is stored, adding the first dummy index */
1614         uint32_t key_idx = position + 1;
1615
1616         RETURN_IF_TRUE(((h == NULL) || (key_idx == EMPTY_SLOT)), -EINVAL);
1617
1618         unsigned int lcore_id, n_slots;
1619         struct lcore_cache *cached_free_slots;
1620         const uint32_t total_entries = h->use_local_cache ?
1621                 h->entries + (RTE_MAX_LCORE - 1) * (LCORE_CACHE_SIZE - 1) + 1
1622                                                         : h->entries + 1;
1623
1624         /* Out of bounds */
1625         if (key_idx >= total_entries)
1626                 return -EINVAL;
1627         if (h->ext_table_support && h->readwrite_concur_lf_support) {
1628                 uint32_t index = h->ext_bkt_to_free[position];
1629                 if (index) {
1630                         /* Recycle empty ext bkt to free list. */
1631                         rte_ring_sp_enqueue_elem(h->free_ext_bkts, &index,
1632                                                         sizeof(uint32_t));
1633                         h->ext_bkt_to_free[position] = 0;
1634                 }
1635         }
1636
1637         if (h->use_local_cache) {
1638                 lcore_id = rte_lcore_id();
1639                 cached_free_slots = &h->local_free_slots[lcore_id];
1640                 /* Cache full, need to free it. */
1641                 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1642                         /* Need to enqueue the free slots in global ring. */
1643                         n_slots = rte_ring_mp_enqueue_burst_elem(h->free_slots,
1644                                                 cached_free_slots->objs,
1645                                                 sizeof(uint32_t),
1646                                                 LCORE_CACHE_SIZE, NULL);
1647                         RETURN_IF_TRUE((n_slots == 0), -EFAULT);
1648                         cached_free_slots->len -= n_slots;
1649                 }
1650                 /* Put index of new free slot in cache. */
1651                 cached_free_slots->objs[cached_free_slots->len] = key_idx;
1652                 cached_free_slots->len++;
1653         } else {
1654                 rte_ring_sp_enqueue_elem(h->free_slots, &key_idx,
1655                                                 sizeof(uint32_t));
1656         }
1657
1658         return 0;
1659 }
1660
1661 static inline void
1662 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
1663                         const struct rte_hash_bucket *prim_bkt,
1664                         const struct rte_hash_bucket *sec_bkt,
1665                         uint16_t sig,
1666                         enum rte_hash_sig_compare_function sig_cmp_fn)
1667 {
1668         unsigned int i;
1669
1670         /* For match mask the first bit of every two bits indicates the match */
1671         switch (sig_cmp_fn) {
1672 #if defined(RTE_MACHINE_CPUFLAG_SSE2)
1673         case RTE_HASH_COMPARE_SSE:
1674                 /* Compare all signatures in the bucket */
1675                 *prim_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1676                                 _mm_load_si128(
1677                                         (__m128i const *)prim_bkt->sig_current),
1678                                 _mm_set1_epi16(sig)));
1679                 /* Compare all signatures in the bucket */
1680                 *sec_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1681                                 _mm_load_si128(
1682                                         (__m128i const *)sec_bkt->sig_current),
1683                                 _mm_set1_epi16(sig)));
1684                 break;
1685 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
1686         case RTE_HASH_COMPARE_NEON: {
1687                 uint16x8_t vmat, vsig, x;
1688                 int16x8_t shift = {-15, -13, -11, -9, -7, -5, -3, -1};
1689
1690                 vsig = vld1q_dup_u16((uint16_t const *)&sig);
1691                 /* Compare all signatures in the primary bucket */
1692                 vmat = vceqq_u16(vsig,
1693                         vld1q_u16((uint16_t const *)prim_bkt->sig_current));
1694                 x = vshlq_u16(vandq_u16(vmat, vdupq_n_u16(0x8000)), shift);
1695                 *prim_hash_matches = (uint32_t)(vaddvq_u16(x));
1696                 /* Compare all signatures in the secondary bucket */
1697                 vmat = vceqq_u16(vsig,
1698                         vld1q_u16((uint16_t const *)sec_bkt->sig_current));
1699                 x = vshlq_u16(vandq_u16(vmat, vdupq_n_u16(0x8000)), shift);
1700                 *sec_hash_matches = (uint32_t)(vaddvq_u16(x));
1701                 }
1702                 break;
1703 #endif
1704         default:
1705                 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1706                         *prim_hash_matches |=
1707                                 ((sig == prim_bkt->sig_current[i]) << (i << 1));
1708                         *sec_hash_matches |=
1709                                 ((sig == sec_bkt->sig_current[i]) << (i << 1));
1710                 }
1711         }
1712 }
1713
1714 #define PREFETCH_OFFSET 4
1715 static inline void
1716 __rte_hash_lookup_bulk_l(const struct rte_hash *h, const void **keys,
1717                         int32_t num_keys, int32_t *positions,
1718                         uint64_t *hit_mask, void *data[])
1719 {
1720         uint64_t hits = 0;
1721         int32_t i;
1722         int32_t ret;
1723         uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
1724         uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
1725         uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
1726         uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
1727         const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1728         const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1729         uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1730         uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1731         struct rte_hash_bucket *cur_bkt, *next_bkt;
1732
1733         /* Prefetch first keys */
1734         for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1735                 rte_prefetch0(keys[i]);
1736
1737         /*
1738          * Prefetch rest of the keys, calculate primary and
1739          * secondary bucket and prefetch them
1740          */
1741         for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1742                 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1743
1744                 prim_hash[i] = rte_hash_hash(h, keys[i]);
1745
1746                 sig[i] = get_short_sig(prim_hash[i]);
1747                 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1748                 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1749
1750                 primary_bkt[i] = &h->buckets[prim_index[i]];
1751                 secondary_bkt[i] = &h->buckets[sec_index[i]];
1752
1753                 rte_prefetch0(primary_bkt[i]);
1754                 rte_prefetch0(secondary_bkt[i]);
1755         }
1756
1757         /* Calculate and prefetch rest of the buckets */
1758         for (; i < num_keys; i++) {
1759                 prim_hash[i] = rte_hash_hash(h, keys[i]);
1760
1761                 sig[i] = get_short_sig(prim_hash[i]);
1762                 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1763                 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1764
1765                 primary_bkt[i] = &h->buckets[prim_index[i]];
1766                 secondary_bkt[i] = &h->buckets[sec_index[i]];
1767
1768                 rte_prefetch0(primary_bkt[i]);
1769                 rte_prefetch0(secondary_bkt[i]);
1770         }
1771
1772         __hash_rw_reader_lock(h);
1773
1774         /* Compare signatures and prefetch key slot of first hit */
1775         for (i = 0; i < num_keys; i++) {
1776                 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1777                         primary_bkt[i], secondary_bkt[i],
1778                         sig[i], h->sig_cmp_fn);
1779
1780                 if (prim_hitmask[i]) {
1781                         uint32_t first_hit =
1782                                         __builtin_ctzl(prim_hitmask[i])
1783                                         >> 1;
1784                         uint32_t key_idx =
1785                                 primary_bkt[i]->key_idx[first_hit];
1786                         const struct rte_hash_key *key_slot =
1787                                 (const struct rte_hash_key *)(
1788                                 (const char *)h->key_store +
1789                                 key_idx * h->key_entry_size);
1790                         rte_prefetch0(key_slot);
1791                         continue;
1792                 }
1793
1794                 if (sec_hitmask[i]) {
1795                         uint32_t first_hit =
1796                                         __builtin_ctzl(sec_hitmask[i])
1797                                         >> 1;
1798                         uint32_t key_idx =
1799                                 secondary_bkt[i]->key_idx[first_hit];
1800                         const struct rte_hash_key *key_slot =
1801                                 (const struct rte_hash_key *)(
1802                                 (const char *)h->key_store +
1803                                 key_idx * h->key_entry_size);
1804                         rte_prefetch0(key_slot);
1805                 }
1806         }
1807
1808         /* Compare keys, first hits in primary first */
1809         for (i = 0; i < num_keys; i++) {
1810                 positions[i] = -ENOENT;
1811                 while (prim_hitmask[i]) {
1812                         uint32_t hit_index =
1813                                         __builtin_ctzl(prim_hitmask[i])
1814                                         >> 1;
1815                         uint32_t key_idx =
1816                                 primary_bkt[i]->key_idx[hit_index];
1817                         const struct rte_hash_key *key_slot =
1818                                 (const struct rte_hash_key *)(
1819                                 (const char *)h->key_store +
1820                                 key_idx * h->key_entry_size);
1821
1822                         /*
1823                          * If key index is 0, do not compare key,
1824                          * as it is checking the dummy slot
1825                          */
1826                         if (!!key_idx &
1827                                 !rte_hash_cmp_eq(
1828                                         key_slot->key, keys[i], h)) {
1829                                 if (data != NULL)
1830                                         data[i] = key_slot->pdata;
1831
1832                                 hits |= 1ULL << i;
1833                                 positions[i] = key_idx - 1;
1834                                 goto next_key;
1835                         }
1836                         prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
1837                 }
1838
1839                 while (sec_hitmask[i]) {
1840                         uint32_t hit_index =
1841                                         __builtin_ctzl(sec_hitmask[i])
1842                                         >> 1;
1843                         uint32_t key_idx =
1844                                 secondary_bkt[i]->key_idx[hit_index];
1845                         const struct rte_hash_key *key_slot =
1846                                 (const struct rte_hash_key *)(
1847                                 (const char *)h->key_store +
1848                                 key_idx * h->key_entry_size);
1849
1850                         /*
1851                          * If key index is 0, do not compare key,
1852                          * as it is checking the dummy slot
1853                          */
1854
1855                         if (!!key_idx &
1856                                 !rte_hash_cmp_eq(
1857                                         key_slot->key, keys[i], h)) {
1858                                 if (data != NULL)
1859                                         data[i] = key_slot->pdata;
1860
1861                                 hits |= 1ULL << i;
1862                                 positions[i] = key_idx - 1;
1863                                 goto next_key;
1864                         }
1865                         sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
1866                 }
1867 next_key:
1868                 continue;
1869         }
1870
1871         /* all found, do not need to go through ext bkt */
1872         if ((hits == ((1ULL << num_keys) - 1)) || !h->ext_table_support) {
1873                 if (hit_mask != NULL)
1874                         *hit_mask = hits;
1875                 __hash_rw_reader_unlock(h);
1876                 return;
1877         }
1878
1879         /* need to check ext buckets for match */
1880         for (i = 0; i < num_keys; i++) {
1881                 if ((hits & (1ULL << i)) != 0)
1882                         continue;
1883                 next_bkt = secondary_bkt[i]->next;
1884                 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
1885                         if (data != NULL)
1886                                 ret = search_one_bucket_l(h, keys[i],
1887                                                 sig[i], &data[i], cur_bkt);
1888                         else
1889                                 ret = search_one_bucket_l(h, keys[i],
1890                                                 sig[i], NULL, cur_bkt);
1891                         if (ret != -1) {
1892                                 positions[i] = ret;
1893                                 hits |= 1ULL << i;
1894                                 break;
1895                         }
1896                 }
1897         }
1898
1899         __hash_rw_reader_unlock(h);
1900
1901         if (hit_mask != NULL)
1902                 *hit_mask = hits;
1903 }
1904
1905 static inline void
1906 __rte_hash_lookup_bulk_lf(const struct rte_hash *h, const void **keys,
1907                         int32_t num_keys, int32_t *positions,
1908                         uint64_t *hit_mask, void *data[])
1909 {
1910         uint64_t hits = 0;
1911         int32_t i;
1912         int32_t ret;
1913         uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
1914         uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
1915         uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
1916         uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
1917         const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1918         const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1919         uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1920         uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1921         struct rte_hash_bucket *cur_bkt, *next_bkt;
1922         uint32_t cnt_b, cnt_a;
1923
1924         /* Prefetch first keys */
1925         for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1926                 rte_prefetch0(keys[i]);
1927
1928         /*
1929          * Prefetch rest of the keys, calculate primary and
1930          * secondary bucket and prefetch them
1931          */
1932         for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1933                 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1934
1935                 prim_hash[i] = rte_hash_hash(h, keys[i]);
1936
1937                 sig[i] = get_short_sig(prim_hash[i]);
1938                 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1939                 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1940
1941                 primary_bkt[i] = &h->buckets[prim_index[i]];
1942                 secondary_bkt[i] = &h->buckets[sec_index[i]];
1943
1944                 rte_prefetch0(primary_bkt[i]);
1945                 rte_prefetch0(secondary_bkt[i]);
1946         }
1947
1948         /* Calculate and prefetch rest of the buckets */
1949         for (; i < num_keys; i++) {
1950                 prim_hash[i] = rte_hash_hash(h, keys[i]);
1951
1952                 sig[i] = get_short_sig(prim_hash[i]);
1953                 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1954                 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1955
1956                 primary_bkt[i] = &h->buckets[prim_index[i]];
1957                 secondary_bkt[i] = &h->buckets[sec_index[i]];
1958
1959                 rte_prefetch0(primary_bkt[i]);
1960                 rte_prefetch0(secondary_bkt[i]);
1961         }
1962
1963         for (i = 0; i < num_keys; i++)
1964                 positions[i] = -ENOENT;
1965
1966         do {
1967                 /* Load the table change counter before the lookup
1968                  * starts. Acquire semantics will make sure that
1969                  * loads in compare_signatures are not hoisted.
1970                  */
1971                 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1972                                         __ATOMIC_ACQUIRE);
1973
1974                 /* Compare signatures and prefetch key slot of first hit */
1975                 for (i = 0; i < num_keys; i++) {
1976                         compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1977                                 primary_bkt[i], secondary_bkt[i],
1978                                 sig[i], h->sig_cmp_fn);
1979
1980                         if (prim_hitmask[i]) {
1981                                 uint32_t first_hit =
1982                                                 __builtin_ctzl(prim_hitmask[i])
1983                                                 >> 1;
1984                                 uint32_t key_idx =
1985                                         primary_bkt[i]->key_idx[first_hit];
1986                                 const struct rte_hash_key *key_slot =
1987                                         (const struct rte_hash_key *)(
1988                                         (const char *)h->key_store +
1989                                         key_idx * h->key_entry_size);
1990                                 rte_prefetch0(key_slot);
1991                                 continue;
1992                         }
1993
1994                         if (sec_hitmask[i]) {
1995                                 uint32_t first_hit =
1996                                                 __builtin_ctzl(sec_hitmask[i])
1997                                                 >> 1;
1998                                 uint32_t key_idx =
1999                                         secondary_bkt[i]->key_idx[first_hit];
2000                                 const struct rte_hash_key *key_slot =
2001                                         (const struct rte_hash_key *)(
2002                                         (const char *)h->key_store +
2003                                         key_idx * h->key_entry_size);
2004                                 rte_prefetch0(key_slot);
2005                         }
2006                 }
2007
2008                 /* Compare keys, first hits in primary first */
2009                 for (i = 0; i < num_keys; i++) {
2010                         while (prim_hitmask[i]) {
2011                                 uint32_t hit_index =
2012                                                 __builtin_ctzl(prim_hitmask[i])
2013                                                 >> 1;
2014                                 uint32_t key_idx =
2015                                 __atomic_load_n(
2016                                         &primary_bkt[i]->key_idx[hit_index],
2017                                         __ATOMIC_ACQUIRE);
2018                                 const struct rte_hash_key *key_slot =
2019                                         (const struct rte_hash_key *)(
2020                                         (const char *)h->key_store +
2021                                         key_idx * h->key_entry_size);
2022
2023                                 /*
2024                                  * If key index is 0, do not compare key,
2025                                  * as it is checking the dummy slot
2026                                  */
2027                                 if (!!key_idx &
2028                                         !rte_hash_cmp_eq(
2029                                                 key_slot->key, keys[i], h)) {
2030                                         if (data != NULL)
2031                                                 data[i] = __atomic_load_n(
2032                                                         &key_slot->pdata,
2033                                                         __ATOMIC_ACQUIRE);
2034
2035                                         hits |= 1ULL << i;
2036                                         positions[i] = key_idx - 1;
2037                                         goto next_key;
2038                                 }
2039                                 prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
2040                         }
2041
2042                         while (sec_hitmask[i]) {
2043                                 uint32_t hit_index =
2044                                                 __builtin_ctzl(sec_hitmask[i])
2045                                                 >> 1;
2046                                 uint32_t key_idx =
2047                                 __atomic_load_n(
2048                                         &secondary_bkt[i]->key_idx[hit_index],
2049                                         __ATOMIC_ACQUIRE);
2050                                 const struct rte_hash_key *key_slot =
2051                                         (const struct rte_hash_key *)(
2052                                         (const char *)h->key_store +
2053                                         key_idx * h->key_entry_size);
2054
2055                                 /*
2056                                  * If key index is 0, do not compare key,
2057                                  * as it is checking the dummy slot
2058                                  */
2059
2060                                 if (!!key_idx &
2061                                         !rte_hash_cmp_eq(
2062                                                 key_slot->key, keys[i], h)) {
2063                                         if (data != NULL)
2064                                                 data[i] = __atomic_load_n(
2065                                                         &key_slot->pdata,
2066                                                         __ATOMIC_ACQUIRE);
2067
2068                                         hits |= 1ULL << i;
2069                                         positions[i] = key_idx - 1;
2070                                         goto next_key;
2071                                 }
2072                                 sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
2073                         }
2074 next_key:
2075                         continue;
2076                 }
2077
2078                 /* all found, do not need to go through ext bkt */
2079                 if (hits == ((1ULL << num_keys) - 1)) {
2080                         if (hit_mask != NULL)
2081                                 *hit_mask = hits;
2082                         return;
2083                 }
2084                 /* need to check ext buckets for match */
2085                 if (h->ext_table_support) {
2086                         for (i = 0; i < num_keys; i++) {
2087                                 if ((hits & (1ULL << i)) != 0)
2088                                         continue;
2089                                 next_bkt = secondary_bkt[i]->next;
2090                                 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
2091                                         if (data != NULL)
2092                                                 ret = search_one_bucket_lf(h,
2093                                                         keys[i], sig[i],
2094                                                         &data[i], cur_bkt);
2095                                         else
2096                                                 ret = search_one_bucket_lf(h,
2097                                                                 keys[i], sig[i],
2098                                                                 NULL, cur_bkt);
2099                                         if (ret != -1) {
2100                                                 positions[i] = ret;
2101                                                 hits |= 1ULL << i;
2102                                                 break;
2103                                         }
2104                                 }
2105                         }
2106                 }
2107                 /* The loads of sig_current in compare_signatures
2108                  * should not move below the load from tbl_chng_cnt.
2109                  */
2110                 __atomic_thread_fence(__ATOMIC_ACQUIRE);
2111                 /* Re-read the table change counter to check if the
2112                  * table has changed during search. If yes, re-do
2113                  * the search.
2114                  * This load should not get hoisted. The load
2115                  * acquires on cnt_b, primary key index and secondary
2116                  * key index will make sure that it does not get
2117                  * hoisted.
2118                  */
2119                 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
2120                                         __ATOMIC_ACQUIRE);
2121         } while (cnt_b != cnt_a);
2122
2123         if (hit_mask != NULL)
2124                 *hit_mask = hits;
2125 }
2126
2127 static inline void
2128 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
2129                         int32_t num_keys, int32_t *positions,
2130                         uint64_t *hit_mask, void *data[])
2131 {
2132         if (h->readwrite_concur_lf_support)
2133                 __rte_hash_lookup_bulk_lf(h, keys, num_keys, positions,
2134                                           hit_mask, data);
2135         else
2136                 __rte_hash_lookup_bulk_l(h, keys, num_keys, positions,
2137                                          hit_mask, data);
2138 }
2139
2140 int
2141 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
2142                       uint32_t num_keys, int32_t *positions)
2143 {
2144         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
2145                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2146                         (positions == NULL)), -EINVAL);
2147
2148         __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
2149         return 0;
2150 }
2151
2152 int
2153 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
2154                       uint32_t num_keys, uint64_t *hit_mask, void *data[])
2155 {
2156         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
2157                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2158                         (hit_mask == NULL)), -EINVAL);
2159
2160         int32_t positions[num_keys];
2161
2162         __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
2163
2164         /* Return number of hits */
2165         return __builtin_popcountl(*hit_mask);
2166 }
2167
2168 int32_t
2169 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
2170 {
2171         uint32_t bucket_idx, idx, position;
2172         struct rte_hash_key *next_key;
2173
2174         RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
2175
2176         const uint32_t total_entries_main = h->num_buckets *
2177                                                         RTE_HASH_BUCKET_ENTRIES;
2178         const uint32_t total_entries = total_entries_main << 1;
2179
2180         /* Out of bounds of all buckets (both main table and ext table) */
2181         if (*next >= total_entries_main)
2182                 goto extend_table;
2183
2184         /* Calculate bucket and index of current iterator */
2185         bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
2186         idx = *next % RTE_HASH_BUCKET_ENTRIES;
2187
2188         /* If current position is empty, go to the next one */
2189         while ((position = __atomic_load_n(&h->buckets[bucket_idx].key_idx[idx],
2190                                         __ATOMIC_ACQUIRE)) == EMPTY_SLOT) {
2191                 (*next)++;
2192                 /* End of table */
2193                 if (*next == total_entries_main)
2194                         goto extend_table;
2195                 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
2196                 idx = *next % RTE_HASH_BUCKET_ENTRIES;
2197         }
2198
2199         __hash_rw_reader_lock(h);
2200         next_key = (struct rte_hash_key *) ((char *)h->key_store +
2201                                 position * h->key_entry_size);
2202         /* Return key and data */
2203         *key = next_key->key;
2204         *data = next_key->pdata;
2205
2206         __hash_rw_reader_unlock(h);
2207
2208         /* Increment iterator */
2209         (*next)++;
2210
2211         return position - 1;
2212
2213 /* Begin to iterate extendable buckets */
2214 extend_table:
2215         /* Out of total bound or if ext bucket feature is not enabled */
2216         if (*next >= total_entries || !h->ext_table_support)
2217                 return -ENOENT;
2218
2219         bucket_idx = (*next - total_entries_main) / RTE_HASH_BUCKET_ENTRIES;
2220         idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
2221
2222         while ((position = h->buckets_ext[bucket_idx].key_idx[idx]) == EMPTY_SLOT) {
2223                 (*next)++;
2224                 if (*next == total_entries)
2225                         return -ENOENT;
2226                 bucket_idx = (*next - total_entries_main) /
2227                                                 RTE_HASH_BUCKET_ENTRIES;
2228                 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
2229         }
2230         __hash_rw_reader_lock(h);
2231         next_key = (struct rte_hash_key *) ((char *)h->key_store +
2232                                 position * h->key_entry_size);
2233         /* Return key and data */
2234         *key = next_key->key;
2235         *data = next_key->pdata;
2236
2237         __hash_rw_reader_unlock(h);
2238
2239         /* Increment iterator */
2240         (*next)++;
2241         return position - 1;
2242 }