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