New upstream version 18.02
[deb_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                                 return prim_bkt->key_idx[i] - 1;
556                         }
557                 }
558         }
559
560         /* Check if key is already inserted in secondary location */
561         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
562                 if (sec_bkt->sig_alt[i] == sig &&
563                                 sec_bkt->sig_current[i] == alt_hash) {
564                         k = (struct rte_hash_key *) ((char *)keys +
565                                         sec_bkt->key_idx[i] * h->key_entry_size);
566                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
567                                 /* Enqueue index of free slot back in the ring. */
568                                 enqueue_slot_back(h, cached_free_slots, slot_id);
569                                 /* Update data */
570                                 k->pdata = data;
571                                 /*
572                                  * Return index where key is stored,
573                                  * subtracting the first dummy index
574                                  */
575                                 return sec_bkt->key_idx[i] - 1;
576                         }
577                 }
578         }
579
580         /* Copy key */
581         rte_memcpy(new_k->key, key, h->key_len);
582         new_k->pdata = data;
583
584 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
585         if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
586                 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
587                                 sig, alt_hash, new_idx);
588                 if (ret >= 0)
589                         return new_idx - 1;
590
591                 /* Primary bucket full, need to make space for new entry */
592                 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
593                                                         alt_hash, new_idx);
594
595                 if (ret >= 0)
596                         return new_idx - 1;
597
598                 /* Also search secondary bucket to get better occupancy */
599                 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
600                                                         alt_hash, new_idx);
601
602                 if (ret >= 0)
603                         return new_idx - 1;
604         } else {
605 #endif
606                 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
607                         /* Check if slot is available */
608                         if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
609                                 prim_bkt->sig_current[i] = sig;
610                                 prim_bkt->sig_alt[i] = alt_hash;
611                                 prim_bkt->key_idx[i] = new_idx;
612                                 break;
613                         }
614                 }
615
616                 if (i != RTE_HASH_BUCKET_ENTRIES) {
617                         if (h->add_key == ADD_KEY_MULTIWRITER)
618                                 rte_spinlock_unlock(h->multiwriter_lock);
619                         return new_idx - 1;
620                 }
621
622                 /* Primary bucket full, need to make space for new entry
623                  * After recursive function.
624                  * Insert the new entry in the position of the pushed entry
625                  * if successful or return error and
626                  * store the new slot back in the ring
627                  */
628                 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
629                 if (ret >= 0) {
630                         prim_bkt->sig_current[ret] = sig;
631                         prim_bkt->sig_alt[ret] = alt_hash;
632                         prim_bkt->key_idx[ret] = new_idx;
633                         if (h->add_key == ADD_KEY_MULTIWRITER)
634                                 rte_spinlock_unlock(h->multiwriter_lock);
635                         return new_idx - 1;
636                 }
637 #if defined(RTE_ARCH_X86)
638         }
639 #endif
640         /* Error in addition, store new slot back in the ring and return error */
641         enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
642
643 failure:
644         if (h->add_key == ADD_KEY_MULTIWRITER)
645                 rte_spinlock_unlock(h->multiwriter_lock);
646         return ret;
647 }
648
649 int32_t
650 rte_hash_add_key_with_hash(const struct rte_hash *h,
651                         const void *key, hash_sig_t sig)
652 {
653         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
654         return __rte_hash_add_key_with_hash(h, key, sig, 0);
655 }
656
657 int32_t
658 rte_hash_add_key(const struct rte_hash *h, const void *key)
659 {
660         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
661         return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
662 }
663
664 int
665 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
666                         const void *key, hash_sig_t sig, void *data)
667 {
668         int ret;
669
670         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
671         ret = __rte_hash_add_key_with_hash(h, key, sig, data);
672         if (ret >= 0)
673                 return 0;
674         else
675                 return ret;
676 }
677
678 int
679 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
680 {
681         int ret;
682
683         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
684
685         ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
686         if (ret >= 0)
687                 return 0;
688         else
689                 return ret;
690 }
691 static inline int32_t
692 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
693                                         hash_sig_t sig, void **data)
694 {
695         uint32_t bucket_idx;
696         hash_sig_t alt_hash;
697         unsigned i;
698         struct rte_hash_bucket *bkt;
699         struct rte_hash_key *k, *keys = h->key_store;
700
701         bucket_idx = sig & h->bucket_bitmask;
702         bkt = &h->buckets[bucket_idx];
703
704         /* Check if key is in primary location */
705         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
706                 if (bkt->sig_current[i] == sig &&
707                                 bkt->key_idx[i] != EMPTY_SLOT) {
708                         k = (struct rte_hash_key *) ((char *)keys +
709                                         bkt->key_idx[i] * h->key_entry_size);
710                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
711                                 if (data != NULL)
712                                         *data = k->pdata;
713                                 /*
714                                  * Return index where key is stored,
715                                  * subtracting the first dummy index
716                                  */
717                                 return bkt->key_idx[i] - 1;
718                         }
719                 }
720         }
721
722         /* Calculate secondary hash */
723         alt_hash = rte_hash_secondary_hash(sig);
724         bucket_idx = alt_hash & h->bucket_bitmask;
725         bkt = &h->buckets[bucket_idx];
726
727         /* Check if key is in secondary location */
728         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
729                 if (bkt->sig_current[i] == alt_hash &&
730                                 bkt->sig_alt[i] == sig) {
731                         k = (struct rte_hash_key *) ((char *)keys +
732                                         bkt->key_idx[i] * h->key_entry_size);
733                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
734                                 if (data != NULL)
735                                         *data = k->pdata;
736                                 /*
737                                  * Return index where key is stored,
738                                  * subtracting the first dummy index
739                                  */
740                                 return bkt->key_idx[i] - 1;
741                         }
742                 }
743         }
744
745         return -ENOENT;
746 }
747
748 int32_t
749 rte_hash_lookup_with_hash(const struct rte_hash *h,
750                         const void *key, hash_sig_t sig)
751 {
752         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
753         return __rte_hash_lookup_with_hash(h, key, sig, NULL);
754 }
755
756 int32_t
757 rte_hash_lookup(const struct rte_hash *h, const void *key)
758 {
759         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
760         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
761 }
762
763 int
764 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
765                         const void *key, hash_sig_t sig, void **data)
766 {
767         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
768         return __rte_hash_lookup_with_hash(h, key, sig, data);
769 }
770
771 int
772 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
773 {
774         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
775         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
776 }
777
778 static inline void
779 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
780 {
781         unsigned lcore_id, n_slots;
782         struct lcore_cache *cached_free_slots;
783
784         bkt->sig_current[i] = NULL_SIGNATURE;
785         bkt->sig_alt[i] = NULL_SIGNATURE;
786         if (h->hw_trans_mem_support) {
787                 lcore_id = rte_lcore_id();
788                 cached_free_slots = &h->local_free_slots[lcore_id];
789                 /* Cache full, need to free it. */
790                 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
791                         /* Need to enqueue the free slots in global ring. */
792                         n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
793                                                 cached_free_slots->objs,
794                                                 LCORE_CACHE_SIZE, NULL);
795                         cached_free_slots->len -= n_slots;
796                 }
797                 /* Put index of new free slot in cache. */
798                 cached_free_slots->objs[cached_free_slots->len] =
799                                 (void *)((uintptr_t)bkt->key_idx[i]);
800                 cached_free_slots->len++;
801         } else {
802                 rte_ring_sp_enqueue(h->free_slots,
803                                 (void *)((uintptr_t)bkt->key_idx[i]));
804         }
805 }
806
807 static inline int32_t
808 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
809                                                 hash_sig_t sig)
810 {
811         uint32_t bucket_idx;
812         hash_sig_t alt_hash;
813         unsigned i;
814         struct rte_hash_bucket *bkt;
815         struct rte_hash_key *k, *keys = h->key_store;
816         int32_t ret;
817
818         bucket_idx = sig & h->bucket_bitmask;
819         bkt = &h->buckets[bucket_idx];
820
821         /* Check if key is in primary location */
822         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
823                 if (bkt->sig_current[i] == sig &&
824                                 bkt->key_idx[i] != EMPTY_SLOT) {
825                         k = (struct rte_hash_key *) ((char *)keys +
826                                         bkt->key_idx[i] * h->key_entry_size);
827                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
828                                 remove_entry(h, bkt, i);
829
830                                 /*
831                                  * Return index where key is stored,
832                                  * subtracting the first dummy index
833                                  */
834                                 ret = bkt->key_idx[i] - 1;
835                                 bkt->key_idx[i] = EMPTY_SLOT;
836                                 return ret;
837                         }
838                 }
839         }
840
841         /* Calculate secondary hash */
842         alt_hash = rte_hash_secondary_hash(sig);
843         bucket_idx = alt_hash & h->bucket_bitmask;
844         bkt = &h->buckets[bucket_idx];
845
846         /* Check if key is in secondary location */
847         for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
848                 if (bkt->sig_current[i] == alt_hash &&
849                                 bkt->key_idx[i] != EMPTY_SLOT) {
850                         k = (struct rte_hash_key *) ((char *)keys +
851                                         bkt->key_idx[i] * h->key_entry_size);
852                         if (rte_hash_cmp_eq(key, k->key, h) == 0) {
853                                 remove_entry(h, bkt, i);
854
855                                 /*
856                                  * Return index where key is stored,
857                                  * subtracting the first dummy index
858                                  */
859                                 ret = bkt->key_idx[i] - 1;
860                                 bkt->key_idx[i] = EMPTY_SLOT;
861                                 return ret;
862                         }
863                 }
864         }
865
866         return -ENOENT;
867 }
868
869 int32_t
870 rte_hash_del_key_with_hash(const struct rte_hash *h,
871                         const void *key, hash_sig_t sig)
872 {
873         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
874         return __rte_hash_del_key_with_hash(h, key, sig);
875 }
876
877 int32_t
878 rte_hash_del_key(const struct rte_hash *h, const void *key)
879 {
880         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
881         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
882 }
883
884 int
885 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
886                                void **key)
887 {
888         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
889
890         struct rte_hash_key *k, *keys = h->key_store;
891         k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
892                                      h->key_entry_size);
893         *key = k->key;
894
895         if (position !=
896             __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
897                                         NULL)) {
898                 return -ENOENT;
899         }
900
901         return 0;
902 }
903
904 static inline void
905 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
906                         const struct rte_hash_bucket *prim_bkt,
907                         const struct rte_hash_bucket *sec_bkt,
908                         hash_sig_t prim_hash, hash_sig_t sec_hash,
909                         enum rte_hash_sig_compare_function sig_cmp_fn)
910 {
911         unsigned int i;
912
913         switch (sig_cmp_fn) {
914 #ifdef RTE_MACHINE_CPUFLAG_AVX2
915         case RTE_HASH_COMPARE_AVX2:
916                 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
917                                 _mm256_load_si256(
918                                         (__m256i const *)prim_bkt->sig_current),
919                                 _mm256_set1_epi32(prim_hash)));
920                 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
921                                 _mm256_load_si256(
922                                         (__m256i const *)sec_bkt->sig_current),
923                                 _mm256_set1_epi32(sec_hash)));
924                 break;
925 #endif
926 #ifdef RTE_MACHINE_CPUFLAG_SSE2
927         case RTE_HASH_COMPARE_SSE:
928                 /* Compare the first 4 signatures in the bucket */
929                 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
930                                 _mm_load_si128(
931                                         (__m128i const *)prim_bkt->sig_current),
932                                 _mm_set1_epi32(prim_hash)));
933                 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
934                                 _mm_load_si128(
935                                         (__m128i const *)&prim_bkt->sig_current[4]),
936                                 _mm_set1_epi32(prim_hash)))) << 4;
937                 /* Compare the first 4 signatures in the bucket */
938                 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
939                                 _mm_load_si128(
940                                         (__m128i const *)sec_bkt->sig_current),
941                                 _mm_set1_epi32(sec_hash)));
942                 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
943                                 _mm_load_si128(
944                                         (__m128i const *)&sec_bkt->sig_current[4]),
945                                 _mm_set1_epi32(sec_hash)))) << 4;
946                 break;
947 #endif
948         default:
949                 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
950                         *prim_hash_matches |=
951                                 ((prim_hash == prim_bkt->sig_current[i]) << i);
952                         *sec_hash_matches |=
953                                 ((sec_hash == sec_bkt->sig_current[i]) << i);
954                 }
955         }
956
957 }
958
959 #define PREFETCH_OFFSET 4
960 static inline void
961 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
962                         int32_t num_keys, int32_t *positions,
963                         uint64_t *hit_mask, void *data[])
964 {
965         uint64_t hits = 0;
966         int32_t i;
967         uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
968         uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
969         const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
970         const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
971         uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
972         uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
973
974         /* Prefetch first keys */
975         for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
976                 rte_prefetch0(keys[i]);
977
978         /*
979          * Prefetch rest of the keys, calculate primary and
980          * secondary bucket and prefetch them
981          */
982         for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
983                 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
984
985                 prim_hash[i] = rte_hash_hash(h, keys[i]);
986                 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
987
988                 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
989                 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
990
991                 rte_prefetch0(primary_bkt[i]);
992                 rte_prefetch0(secondary_bkt[i]);
993         }
994
995         /* Calculate and prefetch rest of the buckets */
996         for (; i < num_keys; i++) {
997                 prim_hash[i] = rte_hash_hash(h, keys[i]);
998                 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
999
1000                 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1001                 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1002
1003                 rte_prefetch0(primary_bkt[i]);
1004                 rte_prefetch0(secondary_bkt[i]);
1005         }
1006
1007         /* Compare signatures and prefetch key slot of first hit */
1008         for (i = 0; i < num_keys; i++) {
1009                 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1010                                 primary_bkt[i], secondary_bkt[i],
1011                                 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1012
1013                 if (prim_hitmask[i]) {
1014                         uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1015                         uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1016                         const struct rte_hash_key *key_slot =
1017                                 (const struct rte_hash_key *)(
1018                                 (const char *)h->key_store +
1019                                 key_idx * h->key_entry_size);
1020                         rte_prefetch0(key_slot);
1021                         continue;
1022                 }
1023
1024                 if (sec_hitmask[i]) {
1025                         uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1026                         uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1027                         const struct rte_hash_key *key_slot =
1028                                 (const struct rte_hash_key *)(
1029                                 (const char *)h->key_store +
1030                                 key_idx * h->key_entry_size);
1031                         rte_prefetch0(key_slot);
1032                 }
1033         }
1034
1035         /* Compare keys, first hits in primary first */
1036         for (i = 0; i < num_keys; i++) {
1037                 positions[i] = -ENOENT;
1038                 while (prim_hitmask[i]) {
1039                         uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1040
1041                         uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1042                         const struct rte_hash_key *key_slot =
1043                                 (const struct rte_hash_key *)(
1044                                 (const char *)h->key_store +
1045                                 key_idx * h->key_entry_size);
1046                         /*
1047                          * If key index is 0, do not compare key,
1048                          * as it is checking the dummy slot
1049                          */
1050                         if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1051                                 if (data != NULL)
1052                                         data[i] = key_slot->pdata;
1053
1054                                 hits |= 1ULL << i;
1055                                 positions[i] = key_idx - 1;
1056                                 goto next_key;
1057                         }
1058                         prim_hitmask[i] &= ~(1 << (hit_index));
1059                 }
1060
1061                 while (sec_hitmask[i]) {
1062                         uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1063
1064                         uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1065                         const struct rte_hash_key *key_slot =
1066                                 (const struct rte_hash_key *)(
1067                                 (const char *)h->key_store +
1068                                 key_idx * h->key_entry_size);
1069                         /*
1070                          * If key index is 0, do not compare key,
1071                          * as it is checking the dummy slot
1072                          */
1073
1074                         if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1075                                 if (data != NULL)
1076                                         data[i] = key_slot->pdata;
1077
1078                                 hits |= 1ULL << i;
1079                                 positions[i] = key_idx - 1;
1080                                 goto next_key;
1081                         }
1082                         sec_hitmask[i] &= ~(1 << (hit_index));
1083                 }
1084
1085 next_key:
1086                 continue;
1087         }
1088
1089         if (hit_mask != NULL)
1090                 *hit_mask = hits;
1091 }
1092
1093 int
1094 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1095                       uint32_t num_keys, int32_t *positions)
1096 {
1097         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1098                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1099                         (positions == NULL)), -EINVAL);
1100
1101         __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1102         return 0;
1103 }
1104
1105 int
1106 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1107                       uint32_t num_keys, uint64_t *hit_mask, void *data[])
1108 {
1109         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1110                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1111                         (hit_mask == NULL)), -EINVAL);
1112
1113         int32_t positions[num_keys];
1114
1115         __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1116
1117         /* Return number of hits */
1118         return __builtin_popcountl(*hit_mask);
1119 }
1120
1121 int32_t
1122 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1123 {
1124         uint32_t bucket_idx, idx, position;
1125         struct rte_hash_key *next_key;
1126
1127         RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1128
1129         const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1130         /* Out of bounds */
1131         if (*next >= total_entries)
1132                 return -ENOENT;
1133
1134         /* Calculate bucket and index of current iterator */
1135         bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1136         idx = *next % RTE_HASH_BUCKET_ENTRIES;
1137
1138         /* If current position is empty, go to the next one */
1139         while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1140                 (*next)++;
1141                 /* End of table */
1142                 if (*next == total_entries)
1143                         return -ENOENT;
1144                 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1145                 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1146         }
1147
1148         /* Get position of entry in key table */
1149         position = h->buckets[bucket_idx].key_idx[idx];
1150         next_key = (struct rte_hash_key *) ((char *)h->key_store +
1151                                 position * h->key_entry_size);
1152         /* Return key and data */
1153         *key = next_key->key;
1154         *data = next_key->pdata;
1155
1156         /* Increment iterator */
1157         (*next)++;
1158
1159         return position - 1;
1160 }