4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <sys/queue.h>
41 #include <rte_common.h>
42 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
44 #include <rte_memcpy.h>
45 #include <rte_prefetch.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
50 #include <rte_eal_memconfig.h>
51 #include <rte_per_lcore.h>
52 #include <rte_errno.h>
53 #include <rte_string_fns.h>
54 #include <rte_cpuflags.h>
56 #include <rte_rwlock.h>
57 #include <rte_spinlock.h>
59 #include <rte_compat.h>
62 #include "rte_cuckoo_hash.h"
64 #if defined(RTE_ARCH_X86)
65 #include "rte_cuckoo_hash_x86.h"
68 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
70 static struct rte_tailq_elem rte_hash_tailq = {
73 EAL_REGISTER_TAILQ(rte_hash_tailq)
76 rte_hash_find_existing(const char *name)
78 struct rte_hash *h = NULL;
79 struct rte_tailq_entry *te;
80 struct rte_hash_list *hash_list;
82 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
84 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
85 TAILQ_FOREACH(te, hash_list, next) {
86 h = (struct rte_hash *) te->data;
87 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
90 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
99 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
101 h->cmp_jump_table_idx = KEY_CUSTOM;
102 h->rte_hash_custom_cmp_eq = func;
106 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
108 if (h->cmp_jump_table_idx == KEY_CUSTOM)
109 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
111 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
115 rte_hash_create(const struct rte_hash_parameters *params)
117 struct rte_hash *h = NULL;
118 struct rte_tailq_entry *te = NULL;
119 struct rte_hash_list *hash_list;
120 struct rte_ring *r = NULL;
121 char hash_name[RTE_HASH_NAMESIZE];
123 void *buckets = NULL;
124 char ring_name[RTE_RING_NAMESIZE];
125 unsigned num_key_slots;
126 unsigned hw_trans_mem_support = 0;
129 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
131 if (params == NULL) {
132 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
136 /* Check for valid parameters */
137 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
138 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
139 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
140 (params->key_len == 0)) {
142 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
146 /* Check extra flags field to check extra options. */
147 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
148 hw_trans_mem_support = 1;
150 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
151 if (hw_trans_mem_support)
153 * Increase number of slots by total number of indices
154 * that can be stored in the lcore caches
155 * except for the first cache
157 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
158 (LCORE_CACHE_SIZE - 1) + 1;
160 num_key_slots = params->entries + 1;
162 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
163 /* Create ring (Dummy slot index is not enqueued) */
164 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
165 params->socket_id, 0);
167 RTE_LOG(ERR, HASH, "memory allocation failed\n");
171 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
173 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
175 /* guarantee there's no existing: this is normally already checked
176 * by ring creation above */
177 TAILQ_FOREACH(te, hash_list, next) {
178 h = (struct rte_hash *) te->data;
179 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
189 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
191 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
195 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
196 RTE_CACHE_LINE_SIZE, params->socket_id);
199 RTE_LOG(ERR, HASH, "memory allocation failed\n");
203 const uint32_t num_buckets = rte_align32pow2(params->entries)
204 / RTE_HASH_BUCKET_ENTRIES;
206 buckets = rte_zmalloc_socket(NULL,
207 num_buckets * sizeof(struct rte_hash_bucket),
208 RTE_CACHE_LINE_SIZE, params->socket_id);
210 if (buckets == NULL) {
211 RTE_LOG(ERR, HASH, "memory allocation failed\n");
215 const uint32_t key_entry_size =
216 RTE_ALIGN(sizeof(struct rte_hash_key) + params->key_len,
218 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
220 k = rte_zmalloc_socket(NULL, key_tbl_size,
221 RTE_CACHE_LINE_SIZE, params->socket_id);
224 RTE_LOG(ERR, HASH, "memory allocation failed\n");
229 * If x86 architecture is used, select appropriate compare function,
230 * which may use x86 intrinsics, otherwise use memcmp
232 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
233 /* Select function to compare keys */
234 switch (params->key_len) {
236 h->cmp_jump_table_idx = KEY_16_BYTES;
239 h->cmp_jump_table_idx = KEY_32_BYTES;
242 h->cmp_jump_table_idx = KEY_48_BYTES;
245 h->cmp_jump_table_idx = KEY_64_BYTES;
248 h->cmp_jump_table_idx = KEY_80_BYTES;
251 h->cmp_jump_table_idx = KEY_96_BYTES;
254 h->cmp_jump_table_idx = KEY_112_BYTES;
257 h->cmp_jump_table_idx = KEY_128_BYTES;
260 /* If key is not multiple of 16, use generic memcmp */
261 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
264 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
267 if (hw_trans_mem_support) {
268 h->local_free_slots = rte_zmalloc_socket(NULL,
269 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
270 RTE_CACHE_LINE_SIZE, params->socket_id);
273 /* Setup hash context */
274 snprintf(h->name, sizeof(h->name), "%s", params->name);
275 h->entries = params->entries;
276 h->key_len = params->key_len;
277 h->key_entry_size = key_entry_size;
278 h->hash_func_init_val = params->hash_func_init_val;
280 h->num_buckets = num_buckets;
281 h->bucket_bitmask = h->num_buckets - 1;
282 h->buckets = buckets;
283 h->hash_func = (params->hash_func == NULL) ?
284 DEFAULT_HASH_FUNC : params->hash_func;
287 h->hw_trans_mem_support = hw_trans_mem_support;
289 #if defined(RTE_ARCH_X86)
290 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
291 h->sig_cmp_fn = RTE_HASH_COMPARE_AVX2;
292 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
293 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
296 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
298 /* Turn on multi-writer only with explicit flat from user and TM
301 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
302 if (h->hw_trans_mem_support) {
303 h->add_key = ADD_KEY_MULTIWRITER_TM;
305 h->add_key = ADD_KEY_MULTIWRITER;
306 h->multiwriter_lock = rte_malloc(NULL,
307 sizeof(rte_spinlock_t),
308 RTE_CACHE_LINE_SIZE);
309 if (h->multiwriter_lock == NULL)
312 rte_spinlock_init(h->multiwriter_lock);
315 h->add_key = ADD_KEY_SINGLEWRITER;
317 /* Populate free slots ring. Entry zero is reserved for key misses. */
318 for (i = 1; i < num_key_slots; i++)
319 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
321 te->data = (void *) h;
322 TAILQ_INSERT_TAIL(hash_list, te, next);
323 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
327 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
338 rte_hash_free(struct rte_hash *h)
340 struct rte_tailq_entry *te;
341 struct rte_hash_list *hash_list;
346 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
348 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
350 /* find out tailq entry */
351 TAILQ_FOREACH(te, hash_list, next) {
352 if (te->data == (void *) h)
357 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
361 TAILQ_REMOVE(hash_list, te, next);
363 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
365 if (h->hw_trans_mem_support)
366 rte_free(h->local_free_slots);
368 if (h->add_key == ADD_KEY_MULTIWRITER)
369 rte_free(h->multiwriter_lock);
370 rte_ring_free(h->free_slots);
371 rte_free(h->key_store);
372 rte_free(h->buckets);
378 rte_hash_hash(const struct rte_hash *h, const void *key)
380 /* calc hash result by key */
381 return h->hash_func(key, h->key_len, h->hash_func_init_val);
384 /* Calc the secondary hash value from the primary hash value of a given key */
385 static inline hash_sig_t
386 rte_hash_secondary_hash(const hash_sig_t primary_hash)
388 static const unsigned all_bits_shift = 12;
389 static const unsigned alt_bits_xor = 0x5bd1e995;
391 uint32_t tag = primary_hash >> all_bits_shift;
393 return primary_hash ^ ((tag + 1) * alt_bits_xor);
397 rte_hash_reset(struct rte_hash *h)
400 uint32_t tot_ring_cnt, i;
405 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
406 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
408 /* clear the free ring */
409 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
412 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
413 if (h->hw_trans_mem_support)
414 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
415 (LCORE_CACHE_SIZE - 1);
417 tot_ring_cnt = h->entries;
419 for (i = 1; i < tot_ring_cnt + 1; i++)
420 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
422 if (h->hw_trans_mem_support) {
423 /* Reset local caches per lcore */
424 for (i = 0; i < RTE_MAX_LCORE; i++)
425 h->local_free_slots[i].len = 0;
429 /* Search for an entry that can be pushed to its alternative location */
431 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt,
432 unsigned int *nr_pushes)
436 uint32_t next_bucket_idx;
437 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
440 * Push existing item (search for bucket with space in
441 * alternative locations) to its alternative location
443 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
444 /* Search for space in alternative locations */
445 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
446 next_bkt[i] = &h->buckets[next_bucket_idx];
447 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
448 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
452 if (j != RTE_HASH_BUCKET_ENTRIES)
456 /* Alternative location has spare room (end of recursive function) */
457 if (i != RTE_HASH_BUCKET_ENTRIES) {
458 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
459 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
460 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
464 /* Pick entry that has not been pushed yet */
465 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
466 if (bkt->flag[i] == 0)
469 /* All entries have been pushed, so entry cannot be added */
470 if (i == RTE_HASH_BUCKET_ENTRIES || ++(*nr_pushes) > RTE_HASH_MAX_PUSHES)
473 /* Set flag to indicate that this entry is going to be pushed */
476 /* Need room in alternative bucket to insert the pushed entry */
477 ret = make_space_bucket(h, next_bkt[i], nr_pushes);
479 * After recursive function.
480 * Clear flags and insert the pushed entry
481 * in its alternative location if successful,
486 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
487 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
488 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
496 * Function called to enqueue back an index in the cache/ring,
497 * as slot has not being used and it can be used in the
498 * next addition attempt.
501 enqueue_slot_back(const struct rte_hash *h,
502 struct lcore_cache *cached_free_slots,
505 if (h->hw_trans_mem_support) {
506 cached_free_slots->objs[cached_free_slots->len] = slot_id;
507 cached_free_slots->len++;
509 rte_ring_sp_enqueue(h->free_slots, slot_id);
512 static inline int32_t
513 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
514 hash_sig_t sig, void *data)
517 uint32_t prim_bucket_idx, sec_bucket_idx;
519 struct rte_hash_bucket *prim_bkt, *sec_bkt;
520 struct rte_hash_key *new_k, *k, *keys = h->key_store;
521 void *slot_id = NULL;
526 struct lcore_cache *cached_free_slots = NULL;
527 unsigned int nr_pushes = 0;
529 if (h->add_key == ADD_KEY_MULTIWRITER)
530 rte_spinlock_lock(h->multiwriter_lock);
532 prim_bucket_idx = sig & h->bucket_bitmask;
533 prim_bkt = &h->buckets[prim_bucket_idx];
534 rte_prefetch0(prim_bkt);
536 alt_hash = rte_hash_secondary_hash(sig);
537 sec_bucket_idx = alt_hash & h->bucket_bitmask;
538 sec_bkt = &h->buckets[sec_bucket_idx];
539 rte_prefetch0(sec_bkt);
541 /* Get a new slot for storing the new key */
542 if (h->hw_trans_mem_support) {
543 lcore_id = rte_lcore_id();
544 cached_free_slots = &h->local_free_slots[lcore_id];
545 /* Try to get a free slot from the local cache */
546 if (cached_free_slots->len == 0) {
547 /* Need to get another burst of free slots from global ring */
548 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
549 cached_free_slots->objs, LCORE_CACHE_SIZE);
553 cached_free_slots->len += n_slots;
556 /* Get a free slot from the local cache */
557 cached_free_slots->len--;
558 slot_id = cached_free_slots->objs[cached_free_slots->len];
560 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
564 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
565 rte_prefetch0(new_k);
566 new_idx = (uint32_t)((uintptr_t) slot_id);
568 /* Check if key is already inserted in primary location */
569 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
570 if (prim_bkt->sig_current[i] == sig &&
571 prim_bkt->sig_alt[i] == alt_hash) {
572 k = (struct rte_hash_key *) ((char *)keys +
573 prim_bkt->key_idx[i] * h->key_entry_size);
574 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
575 /* Enqueue index of free slot back in the ring. */
576 enqueue_slot_back(h, cached_free_slots, slot_id);
580 * Return index where key is stored,
581 * substracting the first dummy index
583 return prim_bkt->key_idx[i] - 1;
588 /* Check if key is already inserted in secondary location */
589 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
590 if (sec_bkt->sig_alt[i] == sig &&
591 sec_bkt->sig_current[i] == alt_hash) {
592 k = (struct rte_hash_key *) ((char *)keys +
593 sec_bkt->key_idx[i] * h->key_entry_size);
594 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
595 /* Enqueue index of free slot back in the ring. */
596 enqueue_slot_back(h, cached_free_slots, slot_id);
600 * Return index where key is stored,
601 * substracting the first dummy index
603 return sec_bkt->key_idx[i] - 1;
609 rte_memcpy(new_k->key, key, h->key_len);
612 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
613 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
614 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
615 sig, alt_hash, new_idx);
619 /* Primary bucket full, need to make space for new entry */
620 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
626 /* Also search secondary bucket to get better occupancy */
627 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
634 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
635 /* Check if slot is available */
636 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
637 prim_bkt->sig_current[i] = sig;
638 prim_bkt->sig_alt[i] = alt_hash;
639 prim_bkt->key_idx[i] = new_idx;
644 if (i != RTE_HASH_BUCKET_ENTRIES) {
645 if (h->add_key == ADD_KEY_MULTIWRITER)
646 rte_spinlock_unlock(h->multiwriter_lock);
650 /* Primary bucket full, need to make space for new entry
651 * After recursive function.
652 * Insert the new entry in the position of the pushed entry
653 * if successful or return error and
654 * store the new slot back in the ring
656 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
658 prim_bkt->sig_current[ret] = sig;
659 prim_bkt->sig_alt[ret] = alt_hash;
660 prim_bkt->key_idx[ret] = new_idx;
661 if (h->add_key == ADD_KEY_MULTIWRITER)
662 rte_spinlock_unlock(h->multiwriter_lock);
665 #if defined(RTE_ARCH_X86)
668 /* Error in addition, store new slot back in the ring and return error */
669 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
671 if (h->add_key == ADD_KEY_MULTIWRITER)
672 rte_spinlock_unlock(h->multiwriter_lock);
677 rte_hash_add_key_with_hash(const struct rte_hash *h,
678 const void *key, hash_sig_t sig)
680 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
681 return __rte_hash_add_key_with_hash(h, key, sig, 0);
685 rte_hash_add_key(const struct rte_hash *h, const void *key)
687 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
688 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
692 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
693 const void *key, hash_sig_t sig, void *data)
697 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
698 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
706 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
710 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
712 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
718 static inline int32_t
719 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
720 hash_sig_t sig, void **data)
725 struct rte_hash_bucket *bkt;
726 struct rte_hash_key *k, *keys = h->key_store;
728 bucket_idx = sig & h->bucket_bitmask;
729 bkt = &h->buckets[bucket_idx];
731 /* Check if key is in primary location */
732 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
733 if (bkt->sig_current[i] == sig &&
734 bkt->key_idx[i] != EMPTY_SLOT) {
735 k = (struct rte_hash_key *) ((char *)keys +
736 bkt->key_idx[i] * h->key_entry_size);
737 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
741 * Return index where key is stored,
742 * substracting the first dummy index
744 return bkt->key_idx[i] - 1;
749 /* Calculate secondary hash */
750 alt_hash = rte_hash_secondary_hash(sig);
751 bucket_idx = alt_hash & h->bucket_bitmask;
752 bkt = &h->buckets[bucket_idx];
754 /* Check if key is in secondary location */
755 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
756 if (bkt->sig_current[i] == alt_hash &&
757 bkt->sig_alt[i] == sig) {
758 k = (struct rte_hash_key *) ((char *)keys +
759 bkt->key_idx[i] * h->key_entry_size);
760 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
764 * Return index where key is stored,
765 * substracting the first dummy index
767 return bkt->key_idx[i] - 1;
776 rte_hash_lookup_with_hash(const struct rte_hash *h,
777 const void *key, hash_sig_t sig)
779 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
780 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
784 rte_hash_lookup(const struct rte_hash *h, const void *key)
786 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
787 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
791 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
792 const void *key, hash_sig_t sig, void **data)
794 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
795 return __rte_hash_lookup_with_hash(h, key, sig, data);
799 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
801 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
802 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
806 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
808 unsigned lcore_id, n_slots;
809 struct lcore_cache *cached_free_slots;
811 bkt->sig_current[i] = NULL_SIGNATURE;
812 bkt->sig_alt[i] = NULL_SIGNATURE;
813 if (h->hw_trans_mem_support) {
814 lcore_id = rte_lcore_id();
815 cached_free_slots = &h->local_free_slots[lcore_id];
816 /* Cache full, need to free it. */
817 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
818 /* Need to enqueue the free slots in global ring. */
819 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
820 cached_free_slots->objs,
822 cached_free_slots->len -= n_slots;
824 /* Put index of new free slot in cache. */
825 cached_free_slots->objs[cached_free_slots->len] =
826 (void *)((uintptr_t)bkt->key_idx[i]);
827 cached_free_slots->len++;
829 rte_ring_sp_enqueue(h->free_slots,
830 (void *)((uintptr_t)bkt->key_idx[i]));
834 static inline int32_t
835 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
841 struct rte_hash_bucket *bkt;
842 struct rte_hash_key *k, *keys = h->key_store;
845 bucket_idx = sig & h->bucket_bitmask;
846 bkt = &h->buckets[bucket_idx];
848 /* Check if key is in primary location */
849 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
850 if (bkt->sig_current[i] == sig &&
851 bkt->key_idx[i] != EMPTY_SLOT) {
852 k = (struct rte_hash_key *) ((char *)keys +
853 bkt->key_idx[i] * h->key_entry_size);
854 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
855 remove_entry(h, bkt, i);
858 * Return index where key is stored,
859 * substracting the first dummy index
861 ret = bkt->key_idx[i] - 1;
862 bkt->key_idx[i] = EMPTY_SLOT;
868 /* Calculate secondary hash */
869 alt_hash = rte_hash_secondary_hash(sig);
870 bucket_idx = alt_hash & h->bucket_bitmask;
871 bkt = &h->buckets[bucket_idx];
873 /* Check if key is in secondary location */
874 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
875 if (bkt->sig_current[i] == alt_hash &&
876 bkt->key_idx[i] != EMPTY_SLOT) {
877 k = (struct rte_hash_key *) ((char *)keys +
878 bkt->key_idx[i] * h->key_entry_size);
879 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
880 remove_entry(h, bkt, i);
883 * Return index where key is stored,
884 * substracting the first dummy index
886 ret = bkt->key_idx[i] - 1;
887 bkt->key_idx[i] = EMPTY_SLOT;
897 rte_hash_del_key_with_hash(const struct rte_hash *h,
898 const void *key, hash_sig_t sig)
900 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
901 return __rte_hash_del_key_with_hash(h, key, sig);
905 rte_hash_del_key(const struct rte_hash *h, const void *key)
907 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
908 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
912 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
915 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
917 struct rte_hash_key *k, *keys = h->key_store;
918 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
923 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
932 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
933 const struct rte_hash_bucket *prim_bkt,
934 const struct rte_hash_bucket *sec_bkt,
935 hash_sig_t prim_hash, hash_sig_t sec_hash,
936 enum rte_hash_sig_compare_function sig_cmp_fn)
940 switch (sig_cmp_fn) {
941 #ifdef RTE_MACHINE_CPUFLAG_AVX2
942 case RTE_HASH_COMPARE_AVX2:
943 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
945 (__m256i const *)prim_bkt->sig_current),
946 _mm256_set1_epi32(prim_hash)));
947 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
949 (__m256i const *)sec_bkt->sig_current),
950 _mm256_set1_epi32(sec_hash)));
953 #ifdef RTE_MACHINE_CPUFLAG_SSE2
954 case RTE_HASH_COMPARE_SSE:
955 /* Compare the first 4 signatures in the bucket */
956 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
958 (__m128i const *)prim_bkt->sig_current),
959 _mm_set1_epi32(prim_hash)));
960 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
962 (__m128i const *)&prim_bkt->sig_current[4]),
963 _mm_set1_epi32(prim_hash)))) << 4;
964 /* Compare the first 4 signatures in the bucket */
965 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
967 (__m128i const *)sec_bkt->sig_current),
968 _mm_set1_epi32(sec_hash)));
969 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
971 (__m128i const *)&sec_bkt->sig_current[4]),
972 _mm_set1_epi32(sec_hash)))) << 4;
976 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
977 *prim_hash_matches |=
978 ((prim_hash == prim_bkt->sig_current[i]) << i);
980 ((sec_hash == sec_bkt->sig_current[i]) << i);
986 #define PREFETCH_OFFSET 4
988 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
989 int32_t num_keys, int32_t *positions,
990 uint64_t *hit_mask, void *data[])
994 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
995 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
996 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
997 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
998 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
999 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1001 /* Prefetch first keys */
1002 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1003 rte_prefetch0(keys[i]);
1006 * Prefetch rest of the keys, calculate primary and
1007 * secondary bucket and prefetch them
1009 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1010 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1012 prim_hash[i] = rte_hash_hash(h, keys[i]);
1013 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1015 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1016 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1018 rte_prefetch0(primary_bkt[i]);
1019 rte_prefetch0(secondary_bkt[i]);
1022 /* Calculate and prefetch rest of the buckets */
1023 for (; i < num_keys; i++) {
1024 prim_hash[i] = rte_hash_hash(h, keys[i]);
1025 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1027 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1028 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1030 rte_prefetch0(primary_bkt[i]);
1031 rte_prefetch0(secondary_bkt[i]);
1034 /* Compare signatures and prefetch key slot of first hit */
1035 for (i = 0; i < num_keys; i++) {
1036 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1037 primary_bkt[i], secondary_bkt[i],
1038 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1040 if (prim_hitmask[i]) {
1041 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1042 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1043 const struct rte_hash_key *key_slot =
1044 (const struct rte_hash_key *)(
1045 (const char *)h->key_store +
1046 key_idx * h->key_entry_size);
1047 rte_prefetch0(key_slot);
1051 if (sec_hitmask[i]) {
1052 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1053 uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1054 const struct rte_hash_key *key_slot =
1055 (const struct rte_hash_key *)(
1056 (const char *)h->key_store +
1057 key_idx * h->key_entry_size);
1058 rte_prefetch0(key_slot);
1062 /* Compare keys, first hits in primary first */
1063 for (i = 0; i < num_keys; i++) {
1064 positions[i] = -ENOENT;
1065 while (prim_hitmask[i]) {
1066 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1068 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1069 const struct rte_hash_key *key_slot =
1070 (const struct rte_hash_key *)(
1071 (const char *)h->key_store +
1072 key_idx * h->key_entry_size);
1074 * If key index is 0, do not compare key,
1075 * as it is checking the dummy slot
1077 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1079 data[i] = key_slot->pdata;
1082 positions[i] = key_idx - 1;
1085 prim_hitmask[i] &= ~(1 << (hit_index));
1088 while (sec_hitmask[i]) {
1089 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1091 uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1092 const struct rte_hash_key *key_slot =
1093 (const struct rte_hash_key *)(
1094 (const char *)h->key_store +
1095 key_idx * h->key_entry_size);
1097 * If key index is 0, do not compare key,
1098 * as it is checking the dummy slot
1101 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1103 data[i] = key_slot->pdata;
1106 positions[i] = key_idx - 1;
1109 sec_hitmask[i] &= ~(1 << (hit_index));
1116 if (hit_mask != NULL)
1121 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1122 uint32_t num_keys, int32_t *positions)
1124 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1125 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1126 (positions == NULL)), -EINVAL);
1128 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1133 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1134 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1136 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1137 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1138 (hit_mask == NULL)), -EINVAL);
1140 int32_t positions[num_keys];
1142 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1144 /* Return number of hits */
1145 return __builtin_popcountl(*hit_mask);
1149 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1151 uint32_t bucket_idx, idx, position;
1152 struct rte_hash_key *next_key;
1154 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1156 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1158 if (*next >= total_entries)
1161 /* Calculate bucket and index of current iterator */
1162 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1163 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1165 /* If current position is empty, go to the next one */
1166 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1169 if (*next == total_entries)
1171 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1172 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1175 /* Get position of entry in key table */
1176 position = h->buckets[bucket_idx].key_idx[idx];
1177 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1178 position * h->key_entry_size);
1179 /* Return key and data */
1180 *key = next_key->key;
1181 *data = next_key->pdata;
1183 /* Increment iterator */
1186 return position - 1;