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