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;
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 - 1),
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 = sizeof(struct rte_hash_key) + params->key_len;
216 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
218 k = rte_zmalloc_socket(NULL, key_tbl_size,
219 RTE_CACHE_LINE_SIZE, params->socket_id);
222 RTE_LOG(ERR, HASH, "memory allocation failed\n");
227 * If x86 architecture is used, select appropriate compare function,
228 * which may use x86 intrinsics, otherwise use memcmp
230 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
231 /* Select function to compare keys */
232 switch (params->key_len) {
234 h->cmp_jump_table_idx = KEY_16_BYTES;
237 h->cmp_jump_table_idx = KEY_32_BYTES;
240 h->cmp_jump_table_idx = KEY_48_BYTES;
243 h->cmp_jump_table_idx = KEY_64_BYTES;
246 h->cmp_jump_table_idx = KEY_80_BYTES;
249 h->cmp_jump_table_idx = KEY_96_BYTES;
252 h->cmp_jump_table_idx = KEY_112_BYTES;
255 h->cmp_jump_table_idx = KEY_128_BYTES;
258 /* If key is not multiple of 16, use generic memcmp */
259 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
262 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
265 if (hw_trans_mem_support) {
266 h->local_free_slots = rte_zmalloc_socket(NULL,
267 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
268 RTE_CACHE_LINE_SIZE, params->socket_id);
271 /* Setup hash context */
272 snprintf(h->name, sizeof(h->name), "%s", params->name);
273 h->entries = params->entries;
274 h->key_len = params->key_len;
275 h->key_entry_size = key_entry_size;
276 h->hash_func_init_val = params->hash_func_init_val;
278 h->num_buckets = num_buckets;
279 h->bucket_bitmask = h->num_buckets - 1;
280 h->buckets = buckets;
281 h->hash_func = (params->hash_func == NULL) ?
282 DEFAULT_HASH_FUNC : params->hash_func;
285 h->hw_trans_mem_support = hw_trans_mem_support;
287 #if defined(RTE_ARCH_X86)
288 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
289 h->sig_cmp_fn = RTE_HASH_COMPARE_AVX2;
290 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
291 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
294 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
296 /* Turn on multi-writer only with explicit flat from user and TM
299 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
300 if (h->hw_trans_mem_support) {
301 h->add_key = ADD_KEY_MULTIWRITER_TM;
303 h->add_key = ADD_KEY_MULTIWRITER;
304 h->multiwriter_lock = rte_malloc(NULL,
305 sizeof(rte_spinlock_t),
307 rte_spinlock_init(h->multiwriter_lock);
310 h->add_key = ADD_KEY_SINGLEWRITER;
312 /* Populate free slots ring. Entry zero is reserved for key misses. */
313 for (i = 1; i < params->entries + 1; i++)
314 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
316 te->data = (void *) h;
317 TAILQ_INSERT_TAIL(hash_list, te, next);
318 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
322 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
333 rte_hash_free(struct rte_hash *h)
335 struct rte_tailq_entry *te;
336 struct rte_hash_list *hash_list;
341 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
343 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
345 /* find out tailq entry */
346 TAILQ_FOREACH(te, hash_list, next) {
347 if (te->data == (void *) h)
352 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
356 TAILQ_REMOVE(hash_list, te, next);
358 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
360 if (h->hw_trans_mem_support)
361 rte_free(h->local_free_slots);
363 if (h->add_key == ADD_KEY_MULTIWRITER)
364 rte_free(h->multiwriter_lock);
365 rte_ring_free(h->free_slots);
366 rte_free(h->key_store);
367 rte_free(h->buckets);
373 rte_hash_hash(const struct rte_hash *h, const void *key)
375 /* calc hash result by key */
376 return h->hash_func(key, h->key_len, h->hash_func_init_val);
379 /* Calc the secondary hash value from the primary hash value of a given key */
380 static inline hash_sig_t
381 rte_hash_secondary_hash(const hash_sig_t primary_hash)
383 static const unsigned all_bits_shift = 12;
384 static const unsigned alt_bits_xor = 0x5bd1e995;
386 uint32_t tag = primary_hash >> all_bits_shift;
388 return primary_hash ^ ((tag + 1) * alt_bits_xor);
392 rte_hash_reset(struct rte_hash *h)
400 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
401 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
403 /* clear the free ring */
404 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
407 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
408 for (i = 1; i < h->entries + 1; i++)
409 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
411 if (h->hw_trans_mem_support) {
412 /* Reset local caches per lcore */
413 for (i = 0; i < RTE_MAX_LCORE; i++)
414 h->local_free_slots[i].len = 0;
418 /* Search for an entry that can be pushed to its alternative location */
420 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
424 uint32_t next_bucket_idx;
425 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
428 * Push existing item (search for bucket with space in
429 * alternative locations) to its alternative location
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)
440 if (j != RTE_HASH_BUCKET_ENTRIES)
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];
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)
457 /* All entries have been pushed, so entry cannot be added */
458 if (i == RTE_HASH_BUCKET_ENTRIES)
461 /* Set flag to indicate that this entry is going to be pushed */
463 /* Need room in alternative bucket to insert the pushed entry */
464 ret = make_space_bucket(h, next_bkt[i]);
466 * After recursive function.
467 * Clear flags and insert the pushed entry
468 * in its alternative location if successful,
473 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
474 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
475 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
483 * Function called to enqueue back an index in the cache/ring,
484 * as slot has not being used and it can be used in the
485 * next addition attempt.
488 enqueue_slot_back(const struct rte_hash *h,
489 struct lcore_cache *cached_free_slots,
492 if (h->hw_trans_mem_support) {
493 cached_free_slots->objs[cached_free_slots->len] = slot_id;
494 cached_free_slots->len++;
496 rte_ring_sp_enqueue(h->free_slots, slot_id);
499 static inline int32_t
500 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
501 hash_sig_t sig, void *data)
504 uint32_t prim_bucket_idx, sec_bucket_idx;
506 struct rte_hash_bucket *prim_bkt, *sec_bkt;
507 struct rte_hash_key *new_k, *k, *keys = h->key_store;
508 void *slot_id = NULL;
513 struct lcore_cache *cached_free_slots = NULL;
515 if (h->add_key == ADD_KEY_MULTIWRITER)
516 rte_spinlock_lock(h->multiwriter_lock);
518 prim_bucket_idx = sig & h->bucket_bitmask;
519 prim_bkt = &h->buckets[prim_bucket_idx];
520 rte_prefetch0(prim_bkt);
522 alt_hash = rte_hash_secondary_hash(sig);
523 sec_bucket_idx = alt_hash & h->bucket_bitmask;
524 sec_bkt = &h->buckets[sec_bucket_idx];
525 rte_prefetch0(sec_bkt);
527 /* Get a new slot for storing the new key */
528 if (h->hw_trans_mem_support) {
529 lcore_id = rte_lcore_id();
530 cached_free_slots = &h->local_free_slots[lcore_id];
531 /* Try to get a free slot from the local cache */
532 if (cached_free_slots->len == 0) {
533 /* Need to get another burst of free slots from global ring */
534 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
535 cached_free_slots->objs, LCORE_CACHE_SIZE);
539 cached_free_slots->len += n_slots;
542 /* Get a free slot from the local cache */
543 cached_free_slots->len--;
544 slot_id = cached_free_slots->objs[cached_free_slots->len];
546 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
550 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
551 rte_prefetch0(new_k);
552 new_idx = (uint32_t)((uintptr_t) slot_id);
554 /* Check if key is already inserted in primary location */
555 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
556 if (prim_bkt->sig_current[i] == sig &&
557 prim_bkt->sig_alt[i] == alt_hash) {
558 k = (struct rte_hash_key *) ((char *)keys +
559 prim_bkt->key_idx[i] * h->key_entry_size);
560 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
561 /* Enqueue index of free slot back in the ring. */
562 enqueue_slot_back(h, cached_free_slots, slot_id);
566 * Return index where key is stored,
567 * substracting the first dummy index
569 return prim_bkt->key_idx[i] - 1;
574 /* Check if key is already inserted in secondary location */
575 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
576 if (sec_bkt->sig_alt[i] == sig &&
577 sec_bkt->sig_current[i] == alt_hash) {
578 k = (struct rte_hash_key *) ((char *)keys +
579 sec_bkt->key_idx[i] * h->key_entry_size);
580 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
581 /* Enqueue index of free slot back in the ring. */
582 enqueue_slot_back(h, cached_free_slots, slot_id);
586 * Return index where key is stored,
587 * substracting the first dummy index
589 return sec_bkt->key_idx[i] - 1;
595 rte_memcpy(new_k->key, key, h->key_len);
598 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
599 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
600 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
601 sig, alt_hash, new_idx);
605 /* Primary bucket full, need to make space for new entry */
606 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
612 /* Also search secondary bucket to get better occupancy */
613 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
620 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
621 /* Check if slot is available */
622 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
623 prim_bkt->sig_current[i] = sig;
624 prim_bkt->sig_alt[i] = alt_hash;
625 prim_bkt->key_idx[i] = new_idx;
630 if (i != RTE_HASH_BUCKET_ENTRIES) {
631 if (h->add_key == ADD_KEY_MULTIWRITER)
632 rte_spinlock_unlock(h->multiwriter_lock);
636 /* Primary bucket full, need to make space for new entry
637 * After recursive function.
638 * Insert the new entry in the position of the pushed entry
639 * if successful or return error and
640 * store the new slot back in the ring
642 ret = make_space_bucket(h, prim_bkt);
644 prim_bkt->sig_current[ret] = sig;
645 prim_bkt->sig_alt[ret] = alt_hash;
646 prim_bkt->key_idx[ret] = new_idx;
647 if (h->add_key == ADD_KEY_MULTIWRITER)
648 rte_spinlock_unlock(h->multiwriter_lock);
651 #if defined(RTE_ARCH_X86)
654 /* Error in addition, store new slot back in the ring and return error */
655 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
657 if (h->add_key == ADD_KEY_MULTIWRITER)
658 rte_spinlock_unlock(h->multiwriter_lock);
663 rte_hash_add_key_with_hash(const struct rte_hash *h,
664 const void *key, hash_sig_t sig)
666 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
667 return __rte_hash_add_key_with_hash(h, key, sig, 0);
671 rte_hash_add_key(const struct rte_hash *h, const void *key)
673 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
674 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
678 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
679 const void *key, hash_sig_t sig, void *data)
683 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
684 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
692 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
696 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
698 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
704 static inline int32_t
705 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
706 hash_sig_t sig, void **data)
711 struct rte_hash_bucket *bkt;
712 struct rte_hash_key *k, *keys = h->key_store;
714 bucket_idx = sig & h->bucket_bitmask;
715 bkt = &h->buckets[bucket_idx];
717 /* Check if key is in primary location */
718 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
719 if (bkt->sig_current[i] == sig &&
720 bkt->key_idx[i] != EMPTY_SLOT) {
721 k = (struct rte_hash_key *) ((char *)keys +
722 bkt->key_idx[i] * h->key_entry_size);
723 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
727 * Return index where key is stored,
728 * substracting the first dummy index
730 return bkt->key_idx[i] - 1;
735 /* Calculate secondary hash */
736 alt_hash = rte_hash_secondary_hash(sig);
737 bucket_idx = alt_hash & h->bucket_bitmask;
738 bkt = &h->buckets[bucket_idx];
740 /* Check if key is in secondary location */
741 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
742 if (bkt->sig_current[i] == alt_hash &&
743 bkt->sig_alt[i] == sig) {
744 k = (struct rte_hash_key *) ((char *)keys +
745 bkt->key_idx[i] * h->key_entry_size);
746 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
750 * Return index where key is stored,
751 * substracting the first dummy index
753 return bkt->key_idx[i] - 1;
762 rte_hash_lookup_with_hash(const struct rte_hash *h,
763 const void *key, hash_sig_t sig)
765 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
766 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
770 rte_hash_lookup(const struct rte_hash *h, const void *key)
772 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
773 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
777 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
778 const void *key, hash_sig_t sig, void **data)
780 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
781 return __rte_hash_lookup_with_hash(h, key, sig, data);
785 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
787 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
788 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
792 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
794 unsigned lcore_id, n_slots;
795 struct lcore_cache *cached_free_slots;
797 bkt->sig_current[i] = NULL_SIGNATURE;
798 bkt->sig_alt[i] = NULL_SIGNATURE;
799 if (h->hw_trans_mem_support) {
800 lcore_id = rte_lcore_id();
801 cached_free_slots = &h->local_free_slots[lcore_id];
802 /* Cache full, need to free it. */
803 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
804 /* Need to enqueue the free slots in global ring. */
805 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
806 cached_free_slots->objs,
808 cached_free_slots->len -= n_slots;
810 /* Put index of new free slot in cache. */
811 cached_free_slots->objs[cached_free_slots->len] =
812 (void *)((uintptr_t)bkt->key_idx[i]);
813 cached_free_slots->len++;
815 rte_ring_sp_enqueue(h->free_slots,
816 (void *)((uintptr_t)bkt->key_idx[i]));
820 static inline int32_t
821 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
827 struct rte_hash_bucket *bkt;
828 struct rte_hash_key *k, *keys = h->key_store;
831 bucket_idx = sig & h->bucket_bitmask;
832 bkt = &h->buckets[bucket_idx];
834 /* Check if key is in primary location */
835 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
836 if (bkt->sig_current[i] == sig &&
837 bkt->key_idx[i] != EMPTY_SLOT) {
838 k = (struct rte_hash_key *) ((char *)keys +
839 bkt->key_idx[i] * h->key_entry_size);
840 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
841 remove_entry(h, bkt, i);
844 * Return index where key is stored,
845 * substracting the first dummy index
847 ret = bkt->key_idx[i] - 1;
848 bkt->key_idx[i] = EMPTY_SLOT;
854 /* Calculate secondary hash */
855 alt_hash = rte_hash_secondary_hash(sig);
856 bucket_idx = alt_hash & h->bucket_bitmask;
857 bkt = &h->buckets[bucket_idx];
859 /* Check if key is in secondary location */
860 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
861 if (bkt->sig_current[i] == alt_hash &&
862 bkt->key_idx[i] != EMPTY_SLOT) {
863 k = (struct rte_hash_key *) ((char *)keys +
864 bkt->key_idx[i] * h->key_entry_size);
865 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
866 remove_entry(h, bkt, i);
869 * Return index where key is stored,
870 * substracting the first dummy index
872 ret = bkt->key_idx[i] - 1;
873 bkt->key_idx[i] = EMPTY_SLOT;
883 rte_hash_del_key_with_hash(const struct rte_hash *h,
884 const void *key, hash_sig_t sig)
886 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
887 return __rte_hash_del_key_with_hash(h, key, sig);
891 rte_hash_del_key(const struct rte_hash *h, const void *key)
893 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
894 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
898 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
901 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
903 struct rte_hash_key *k, *keys = h->key_store;
904 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
909 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
917 /* Lookup bulk stage 0: Prefetch input key */
919 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
920 const void * const *keys)
922 *idx = __builtin_ctzl(*lookup_mask);
923 if (*lookup_mask == 0)
926 rte_prefetch0(keys[*idx]);
927 *lookup_mask &= ~(1llu << *idx);
931 * Lookup bulk stage 1: Calculate primary/secondary hashes
932 * and prefetch primary/secondary buckets
935 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
936 const struct rte_hash_bucket **primary_bkt,
937 const struct rte_hash_bucket **secondary_bkt,
938 hash_sig_t *hash_vals, const void * const *keys,
939 const struct rte_hash *h)
941 *prim_hash = rte_hash_hash(h, keys[idx]);
942 hash_vals[idx] = *prim_hash;
943 *sec_hash = rte_hash_secondary_hash(*prim_hash);
945 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
946 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
948 rte_prefetch0(*primary_bkt);
949 rte_prefetch0(*secondary_bkt);
953 compare_signatures(unsigned int *prim_hash_matches,
954 unsigned int *sec_hash_matches,
955 const struct rte_hash_bucket *prim_bkt,
956 const struct rte_hash_bucket *sec_bkt,
957 hash_sig_t prim_hash, hash_sig_t sec_hash,
958 enum rte_hash_sig_compare_function sig_cmp_fn)
962 switch (sig_cmp_fn) {
963 #ifdef RTE_MACHINE_CPUFLAG_AVX2
964 case RTE_HASH_COMPARE_AVX2:
965 *prim_hash_matches |= _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
967 (__m256i const *)prim_bkt->sig_current),
968 _mm256_set1_epi32(prim_hash)));
969 *sec_hash_matches |= _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
971 (__m256i const *)sec_bkt->sig_current),
972 _mm256_set1_epi32(sec_hash)));
975 #ifdef RTE_MACHINE_CPUFLAG_SSE2
976 case RTE_HASH_COMPARE_SSE:
977 /* Compare the first 4 signatures in the bucket */
978 *prim_hash_matches |= _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
980 (__m128i const *)prim_bkt->sig_current),
981 _mm_set1_epi32(prim_hash)));
982 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
984 (__m128i const *)&prim_bkt->sig_current[4]),
985 _mm_set1_epi32(prim_hash)))) << 4;
986 /* Compare the first 4 signatures in the bucket */
987 *sec_hash_matches |= _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
989 (__m128i const *)sec_bkt->sig_current),
990 _mm_set1_epi32(sec_hash)));
991 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
993 (__m128i const *)&sec_bkt->sig_current[4]),
994 _mm_set1_epi32(sec_hash)))) << 4;
998 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
999 *prim_hash_matches |=
1000 ((prim_hash == prim_bkt->sig_current[i]) << i);
1001 *sec_hash_matches |=
1002 ((sec_hash == sec_bkt->sig_current[i]) << i);
1009 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
1010 * and prefetch first key slot
1013 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
1014 const struct rte_hash_bucket *prim_bkt,
1015 const struct rte_hash_bucket *sec_bkt,
1016 const struct rte_hash_key **key_slot, int32_t *positions,
1017 uint64_t *extra_hits_mask, const void *keys,
1018 const struct rte_hash *h)
1020 unsigned int prim_hash_matches, sec_hash_matches, key_idx;
1021 unsigned int total_hash_matches;
1023 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
1024 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
1026 compare_signatures(&prim_hash_matches, &sec_hash_matches, prim_bkt,
1027 sec_bkt, prim_hash, sec_hash, h->sig_cmp_fn);
1029 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
1031 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
1033 total_hash_matches = (prim_hash_matches |
1034 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
1035 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
1036 key_idx * h->key_entry_size);
1038 rte_prefetch0(*key_slot);
1040 * Return index where key is stored,
1041 * substracting the first dummy index
1043 positions[idx] = (key_idx - 1);
1045 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
1050 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
1052 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
1053 const int32_t *positions, void *data[], uint64_t *hits,
1054 const struct rte_hash *h)
1059 hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
1061 data[idx] = key_slot->pdata;
1063 key_idx = positions[idx] + 1;
1065 * If key index is 0, force hit to be 0, in case key to be looked up
1066 * is all zero (as in the dummy slot), which would result in a wrong hit
1068 *hits |= (uint64_t)(hit && !!key_idx) << idx;
1072 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1073 uint32_t num_keys, int32_t *positions,
1074 uint64_t *hit_mask, void *data[])
1077 uint64_t extra_hits_mask = 0;
1078 uint64_t lookup_mask, miss_mask;
1080 const void *key_store = h->key_store;
1082 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1084 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1085 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1086 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1087 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1088 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1089 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1090 hash_sig_t primary_hash10, primary_hash11;
1091 hash_sig_t secondary_hash10, secondary_hash11;
1092 hash_sig_t primary_hash20, primary_hash21;
1093 hash_sig_t secondary_hash20, secondary_hash21;
1095 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1096 miss_mask = lookup_mask;
1098 lookup_stage0(&idx00, &lookup_mask, keys);
1099 lookup_stage0(&idx01, &lookup_mask, keys);
1101 idx10 = idx00, idx11 = idx01;
1103 lookup_stage0(&idx00, &lookup_mask, keys);
1104 lookup_stage0(&idx01, &lookup_mask, keys);
1105 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1106 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1107 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1108 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1110 primary_bkt20 = primary_bkt10;
1111 primary_bkt21 = primary_bkt11;
1112 secondary_bkt20 = secondary_bkt10;
1113 secondary_bkt21 = secondary_bkt11;
1114 primary_hash20 = primary_hash10;
1115 primary_hash21 = primary_hash11;
1116 secondary_hash20 = secondary_hash10;
1117 secondary_hash21 = secondary_hash11;
1118 idx20 = idx10, idx21 = idx11;
1119 idx10 = idx00, idx11 = idx01;
1121 lookup_stage0(&idx00, &lookup_mask, keys);
1122 lookup_stage0(&idx01, &lookup_mask, keys);
1123 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1124 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1125 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1126 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1127 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1128 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1130 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1131 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1134 while (lookup_mask) {
1135 k_slot30 = k_slot20, k_slot31 = k_slot21;
1136 idx30 = idx20, idx31 = idx21;
1137 primary_bkt20 = primary_bkt10;
1138 primary_bkt21 = primary_bkt11;
1139 secondary_bkt20 = secondary_bkt10;
1140 secondary_bkt21 = secondary_bkt11;
1141 primary_hash20 = primary_hash10;
1142 primary_hash21 = primary_hash11;
1143 secondary_hash20 = secondary_hash10;
1144 secondary_hash21 = secondary_hash11;
1145 idx20 = idx10, idx21 = idx11;
1146 idx10 = idx00, idx11 = idx01;
1148 lookup_stage0(&idx00, &lookup_mask, keys);
1149 lookup_stage0(&idx01, &lookup_mask, keys);
1150 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1151 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1152 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1153 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1154 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1155 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1156 &extra_hits_mask, key_store, h);
1157 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1158 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1159 &extra_hits_mask, key_store, h);
1160 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1161 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1164 k_slot30 = k_slot20, k_slot31 = k_slot21;
1165 idx30 = idx20, idx31 = idx21;
1166 primary_bkt20 = primary_bkt10;
1167 primary_bkt21 = primary_bkt11;
1168 secondary_bkt20 = secondary_bkt10;
1169 secondary_bkt21 = secondary_bkt11;
1170 primary_hash20 = primary_hash10;
1171 primary_hash21 = primary_hash11;
1172 secondary_hash20 = secondary_hash10;
1173 secondary_hash21 = secondary_hash11;
1174 idx20 = idx10, idx21 = idx11;
1175 idx10 = idx00, idx11 = idx01;
1177 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1178 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1179 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1180 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1181 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1182 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1184 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1185 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1187 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1188 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1190 k_slot30 = k_slot20, k_slot31 = k_slot21;
1191 idx30 = idx20, idx31 = idx21;
1192 primary_bkt20 = primary_bkt10;
1193 primary_bkt21 = primary_bkt11;
1194 secondary_bkt20 = secondary_bkt10;
1195 secondary_bkt21 = secondary_bkt11;
1196 primary_hash20 = primary_hash10;
1197 primary_hash21 = primary_hash11;
1198 secondary_hash20 = secondary_hash10;
1199 secondary_hash21 = secondary_hash11;
1200 idx20 = idx10, idx21 = idx11;
1202 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1203 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1205 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1206 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1208 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1209 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1211 k_slot30 = k_slot20, k_slot31 = k_slot21;
1212 idx30 = idx20, idx31 = idx21;
1214 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1215 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1217 /* ignore any items we have already found */
1218 extra_hits_mask &= ~hits;
1220 if (unlikely(extra_hits_mask)) {
1221 /* run a single search for each remaining item */
1223 idx = __builtin_ctzl(extra_hits_mask);
1225 ret = rte_hash_lookup_with_hash_data(h,
1226 keys[idx], hash_vals[idx], &data[idx]);
1228 hits |= 1ULL << idx;
1230 positions[idx] = rte_hash_lookup_with_hash(h,
1231 keys[idx], hash_vals[idx]);
1232 if (positions[idx] >= 0)
1233 hits |= 1llu << idx;
1235 extra_hits_mask &= ~(1llu << idx);
1236 } while (extra_hits_mask);
1240 if (unlikely(miss_mask)) {
1242 idx = __builtin_ctzl(miss_mask);
1243 positions[idx] = -ENOENT;
1244 miss_mask &= ~(1llu << idx);
1245 } while (miss_mask);
1248 if (hit_mask != NULL)
1253 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1254 uint32_t num_keys, int32_t *positions)
1256 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1257 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1258 (positions == NULL)), -EINVAL);
1260 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1265 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1266 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1268 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1269 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1270 (hit_mask == NULL)), -EINVAL);
1272 int32_t positions[num_keys];
1274 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1276 /* Return number of hits */
1277 return __builtin_popcountl(*hit_mask);
1281 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1283 uint32_t bucket_idx, idx, position;
1284 struct rte_hash_key *next_key;
1286 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1288 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1290 if (*next >= total_entries)
1293 /* Calculate bucket and index of current iterator */
1294 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1295 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1297 /* If current position is empty, go to the next one */
1298 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1301 if (*next == total_entries)
1303 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1304 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1307 /* Get position of entry in key table */
1308 position = h->buckets[bucket_idx].key_idx[idx];
1309 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1310 position * h->key_entry_size);
1311 /* Return key and data */
1312 *key = next_key->key;
1313 *data = next_key->pdata;
1315 /* Increment iterator */
1318 return position - 1;