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,
421 unsigned int *nr_pushes)
425 uint32_t next_bucket_idx;
426 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
429 * Push existing item (search for bucket with space in
430 * alternative locations) to its alternative location
432 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
433 /* Search for space in alternative locations */
434 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
435 next_bkt[i] = &h->buckets[next_bucket_idx];
436 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
437 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
441 if (j != RTE_HASH_BUCKET_ENTRIES)
445 /* Alternative location has spare room (end of recursive function) */
446 if (i != RTE_HASH_BUCKET_ENTRIES) {
447 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
448 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
449 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
453 /* Pick entry that has not been pushed yet */
454 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
455 if (bkt->flag[i] == 0)
458 /* All entries have been pushed, so entry cannot be added */
459 if (i == RTE_HASH_BUCKET_ENTRIES || ++(*nr_pushes) > RTE_HASH_MAX_PUSHES)
462 /* Set flag to indicate that this entry is going to be pushed */
465 /* Need room in alternative bucket to insert the pushed entry */
466 ret = make_space_bucket(h, next_bkt[i], nr_pushes);
468 * After recursive function.
469 * Clear flags and insert the pushed entry
470 * in its alternative location if successful,
475 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
476 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
477 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
485 * Function called to enqueue back an index in the cache/ring,
486 * as slot has not being used and it can be used in the
487 * next addition attempt.
490 enqueue_slot_back(const struct rte_hash *h,
491 struct lcore_cache *cached_free_slots,
494 if (h->hw_trans_mem_support) {
495 cached_free_slots->objs[cached_free_slots->len] = slot_id;
496 cached_free_slots->len++;
498 rte_ring_sp_enqueue(h->free_slots, slot_id);
501 static inline int32_t
502 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
503 hash_sig_t sig, void *data)
506 uint32_t prim_bucket_idx, sec_bucket_idx;
508 struct rte_hash_bucket *prim_bkt, *sec_bkt;
509 struct rte_hash_key *new_k, *k, *keys = h->key_store;
510 void *slot_id = NULL;
515 struct lcore_cache *cached_free_slots = NULL;
516 unsigned int nr_pushes = 0;
518 if (h->add_key == ADD_KEY_MULTIWRITER)
519 rte_spinlock_lock(h->multiwriter_lock);
521 prim_bucket_idx = sig & h->bucket_bitmask;
522 prim_bkt = &h->buckets[prim_bucket_idx];
523 rte_prefetch0(prim_bkt);
525 alt_hash = rte_hash_secondary_hash(sig);
526 sec_bucket_idx = alt_hash & h->bucket_bitmask;
527 sec_bkt = &h->buckets[sec_bucket_idx];
528 rte_prefetch0(sec_bkt);
530 /* Get a new slot for storing the new key */
531 if (h->hw_trans_mem_support) {
532 lcore_id = rte_lcore_id();
533 cached_free_slots = &h->local_free_slots[lcore_id];
534 /* Try to get a free slot from the local cache */
535 if (cached_free_slots->len == 0) {
536 /* Need to get another burst of free slots from global ring */
537 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
538 cached_free_slots->objs, LCORE_CACHE_SIZE);
542 cached_free_slots->len += n_slots;
545 /* Get a free slot from the local cache */
546 cached_free_slots->len--;
547 slot_id = cached_free_slots->objs[cached_free_slots->len];
549 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
553 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
554 rte_prefetch0(new_k);
555 new_idx = (uint32_t)((uintptr_t) slot_id);
557 /* Check if key is already inserted in primary location */
558 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
559 if (prim_bkt->sig_current[i] == sig &&
560 prim_bkt->sig_alt[i] == alt_hash) {
561 k = (struct rte_hash_key *) ((char *)keys +
562 prim_bkt->key_idx[i] * h->key_entry_size);
563 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
564 /* Enqueue index of free slot back in the ring. */
565 enqueue_slot_back(h, cached_free_slots, slot_id);
569 * Return index where key is stored,
570 * substracting the first dummy index
572 return prim_bkt->key_idx[i] - 1;
577 /* Check if key is already inserted in secondary location */
578 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
579 if (sec_bkt->sig_alt[i] == sig &&
580 sec_bkt->sig_current[i] == alt_hash) {
581 k = (struct rte_hash_key *) ((char *)keys +
582 sec_bkt->key_idx[i] * h->key_entry_size);
583 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
584 /* Enqueue index of free slot back in the ring. */
585 enqueue_slot_back(h, cached_free_slots, slot_id);
589 * Return index where key is stored,
590 * substracting the first dummy index
592 return sec_bkt->key_idx[i] - 1;
598 rte_memcpy(new_k->key, key, h->key_len);
601 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
602 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
603 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
604 sig, alt_hash, new_idx);
608 /* Primary bucket full, need to make space for new entry */
609 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
615 /* Also search secondary bucket to get better occupancy */
616 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
623 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
624 /* Check if slot is available */
625 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
626 prim_bkt->sig_current[i] = sig;
627 prim_bkt->sig_alt[i] = alt_hash;
628 prim_bkt->key_idx[i] = new_idx;
633 if (i != RTE_HASH_BUCKET_ENTRIES) {
634 if (h->add_key == ADD_KEY_MULTIWRITER)
635 rte_spinlock_unlock(h->multiwriter_lock);
639 /* Primary bucket full, need to make space for new entry
640 * After recursive function.
641 * Insert the new entry in the position of the pushed entry
642 * if successful or return error and
643 * store the new slot back in the ring
645 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
647 prim_bkt->sig_current[ret] = sig;
648 prim_bkt->sig_alt[ret] = alt_hash;
649 prim_bkt->key_idx[ret] = new_idx;
650 if (h->add_key == ADD_KEY_MULTIWRITER)
651 rte_spinlock_unlock(h->multiwriter_lock);
654 #if defined(RTE_ARCH_X86)
657 /* Error in addition, store new slot back in the ring and return error */
658 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
660 if (h->add_key == ADD_KEY_MULTIWRITER)
661 rte_spinlock_unlock(h->multiwriter_lock);
666 rte_hash_add_key_with_hash(const struct rte_hash *h,
667 const void *key, hash_sig_t sig)
669 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
670 return __rte_hash_add_key_with_hash(h, key, sig, 0);
674 rte_hash_add_key(const struct rte_hash *h, const void *key)
676 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
677 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
681 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
682 const void *key, hash_sig_t sig, void *data)
686 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
687 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
695 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
699 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
701 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
707 static inline int32_t
708 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
709 hash_sig_t sig, void **data)
714 struct rte_hash_bucket *bkt;
715 struct rte_hash_key *k, *keys = h->key_store;
717 bucket_idx = sig & h->bucket_bitmask;
718 bkt = &h->buckets[bucket_idx];
720 /* Check if key is in primary location */
721 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
722 if (bkt->sig_current[i] == sig &&
723 bkt->key_idx[i] != EMPTY_SLOT) {
724 k = (struct rte_hash_key *) ((char *)keys +
725 bkt->key_idx[i] * h->key_entry_size);
726 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
730 * Return index where key is stored,
731 * substracting the first dummy index
733 return bkt->key_idx[i] - 1;
738 /* Calculate secondary hash */
739 alt_hash = rte_hash_secondary_hash(sig);
740 bucket_idx = alt_hash & h->bucket_bitmask;
741 bkt = &h->buckets[bucket_idx];
743 /* Check if key is in secondary location */
744 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
745 if (bkt->sig_current[i] == alt_hash &&
746 bkt->sig_alt[i] == sig) {
747 k = (struct rte_hash_key *) ((char *)keys +
748 bkt->key_idx[i] * h->key_entry_size);
749 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
753 * Return index where key is stored,
754 * substracting the first dummy index
756 return bkt->key_idx[i] - 1;
765 rte_hash_lookup_with_hash(const struct rte_hash *h,
766 const void *key, hash_sig_t sig)
768 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
769 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
773 rte_hash_lookup(const struct rte_hash *h, const void *key)
775 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
776 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
780 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
781 const void *key, hash_sig_t sig, void **data)
783 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
784 return __rte_hash_lookup_with_hash(h, key, sig, data);
788 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
790 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
791 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
795 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
797 unsigned lcore_id, n_slots;
798 struct lcore_cache *cached_free_slots;
800 bkt->sig_current[i] = NULL_SIGNATURE;
801 bkt->sig_alt[i] = NULL_SIGNATURE;
802 if (h->hw_trans_mem_support) {
803 lcore_id = rte_lcore_id();
804 cached_free_slots = &h->local_free_slots[lcore_id];
805 /* Cache full, need to free it. */
806 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
807 /* Need to enqueue the free slots in global ring. */
808 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
809 cached_free_slots->objs,
811 cached_free_slots->len -= n_slots;
813 /* Put index of new free slot in cache. */
814 cached_free_slots->objs[cached_free_slots->len] =
815 (void *)((uintptr_t)bkt->key_idx[i]);
816 cached_free_slots->len++;
818 rte_ring_sp_enqueue(h->free_slots,
819 (void *)((uintptr_t)bkt->key_idx[i]));
823 static inline int32_t
824 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
830 struct rte_hash_bucket *bkt;
831 struct rte_hash_key *k, *keys = h->key_store;
834 bucket_idx = sig & h->bucket_bitmask;
835 bkt = &h->buckets[bucket_idx];
837 /* Check if key is in primary location */
838 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
839 if (bkt->sig_current[i] == sig &&
840 bkt->key_idx[i] != EMPTY_SLOT) {
841 k = (struct rte_hash_key *) ((char *)keys +
842 bkt->key_idx[i] * h->key_entry_size);
843 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
844 remove_entry(h, bkt, i);
847 * Return index where key is stored,
848 * substracting the first dummy index
850 ret = bkt->key_idx[i] - 1;
851 bkt->key_idx[i] = EMPTY_SLOT;
857 /* Calculate secondary hash */
858 alt_hash = rte_hash_secondary_hash(sig);
859 bucket_idx = alt_hash & h->bucket_bitmask;
860 bkt = &h->buckets[bucket_idx];
862 /* Check if key is in secondary location */
863 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
864 if (bkt->sig_current[i] == alt_hash &&
865 bkt->key_idx[i] != EMPTY_SLOT) {
866 k = (struct rte_hash_key *) ((char *)keys +
867 bkt->key_idx[i] * h->key_entry_size);
868 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
869 remove_entry(h, bkt, i);
872 * Return index where key is stored,
873 * substracting the first dummy index
875 ret = bkt->key_idx[i] - 1;
876 bkt->key_idx[i] = EMPTY_SLOT;
886 rte_hash_del_key_with_hash(const struct rte_hash *h,
887 const void *key, hash_sig_t sig)
889 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
890 return __rte_hash_del_key_with_hash(h, key, sig);
894 rte_hash_del_key(const struct rte_hash *h, const void *key)
896 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
897 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
901 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
904 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
906 struct rte_hash_key *k, *keys = h->key_store;
907 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
912 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
921 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
922 const struct rte_hash_bucket *prim_bkt,
923 const struct rte_hash_bucket *sec_bkt,
924 hash_sig_t prim_hash, hash_sig_t sec_hash,
925 enum rte_hash_sig_compare_function sig_cmp_fn)
929 switch (sig_cmp_fn) {
930 #ifdef RTE_MACHINE_CPUFLAG_AVX2
931 case RTE_HASH_COMPARE_AVX2:
932 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
934 (__m256i const *)prim_bkt->sig_current),
935 _mm256_set1_epi32(prim_hash)));
936 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
938 (__m256i const *)sec_bkt->sig_current),
939 _mm256_set1_epi32(sec_hash)));
942 #ifdef RTE_MACHINE_CPUFLAG_SSE2
943 case RTE_HASH_COMPARE_SSE:
944 /* Compare the first 4 signatures in the bucket */
945 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
947 (__m128i const *)prim_bkt->sig_current),
948 _mm_set1_epi32(prim_hash)));
949 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
951 (__m128i const *)&prim_bkt->sig_current[4]),
952 _mm_set1_epi32(prim_hash)))) << 4;
953 /* Compare the first 4 signatures in the bucket */
954 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
956 (__m128i const *)sec_bkt->sig_current),
957 _mm_set1_epi32(sec_hash)));
958 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
960 (__m128i const *)&sec_bkt->sig_current[4]),
961 _mm_set1_epi32(sec_hash)))) << 4;
965 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
966 *prim_hash_matches |=
967 ((prim_hash == prim_bkt->sig_current[i]) << i);
969 ((sec_hash == sec_bkt->sig_current[i]) << i);
975 #define PREFETCH_OFFSET 4
977 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
978 int32_t num_keys, int32_t *positions,
979 uint64_t *hit_mask, void *data[])
983 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
984 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
985 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
986 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
987 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
988 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
990 /* Prefetch first keys */
991 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
992 rte_prefetch0(keys[i]);
995 * Prefetch rest of the keys, calculate primary and
996 * secondary bucket and prefetch them
998 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
999 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1001 prim_hash[i] = rte_hash_hash(h, keys[i]);
1002 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1004 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1005 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1007 rte_prefetch0(primary_bkt[i]);
1008 rte_prefetch0(secondary_bkt[i]);
1011 /* Calculate and prefetch rest of the buckets */
1012 for (; i < num_keys; i++) {
1013 prim_hash[i] = rte_hash_hash(h, keys[i]);
1014 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1016 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1017 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1019 rte_prefetch0(primary_bkt[i]);
1020 rte_prefetch0(secondary_bkt[i]);
1023 /* Compare signatures and prefetch key slot of first hit */
1024 for (i = 0; i < num_keys; i++) {
1025 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1026 primary_bkt[i], secondary_bkt[i],
1027 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1029 if (prim_hitmask[i]) {
1030 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1031 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1032 const struct rte_hash_key *key_slot =
1033 (const struct rte_hash_key *)(
1034 (const char *)h->key_store +
1035 key_idx * h->key_entry_size);
1036 rte_prefetch0(key_slot);
1040 if (sec_hitmask[i]) {
1041 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1042 uint32_t key_idx = secondary_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 /* Compare keys, first hits in primary first */
1052 for (i = 0; i < num_keys; i++) {
1053 positions[i] = -ENOENT;
1054 while (prim_hitmask[i]) {
1055 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1057 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1058 const struct rte_hash_key *key_slot =
1059 (const struct rte_hash_key *)(
1060 (const char *)h->key_store +
1061 key_idx * h->key_entry_size);
1063 * If key index is 0, do not compare key,
1064 * as it is checking the dummy slot
1066 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1068 data[i] = key_slot->pdata;
1071 positions[i] = key_idx - 1;
1074 prim_hitmask[i] &= ~(1 << (hit_index));
1077 while (sec_hitmask[i]) {
1078 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1080 uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1081 const struct rte_hash_key *key_slot =
1082 (const struct rte_hash_key *)(
1083 (const char *)h->key_store +
1084 key_idx * h->key_entry_size);
1086 * If key index is 0, do not compare key,
1087 * as it is checking the dummy slot
1090 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1092 data[i] = key_slot->pdata;
1095 positions[i] = key_idx - 1;
1098 sec_hitmask[i] &= ~(1 << (hit_index));
1105 if (hit_mask != NULL)
1110 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1111 uint32_t num_keys, int32_t *positions)
1113 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1114 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1115 (positions == NULL)), -EINVAL);
1117 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1122 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1123 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1125 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1126 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1127 (hit_mask == NULL)), -EINVAL);
1129 int32_t positions[num_keys];
1131 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1133 /* Return number of hits */
1134 return __builtin_popcountl(*hit_mask);
1138 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1140 uint32_t bucket_idx, idx, position;
1141 struct rte_hash_key *next_key;
1143 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1145 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1147 if (*next >= total_entries)
1150 /* Calculate bucket and index of current iterator */
1151 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1152 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1154 /* If current position is empty, go to the next one */
1155 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1158 if (*next == total_entries)
1160 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1161 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1164 /* Get position of entry in key table */
1165 position = h->buckets[bucket_idx].key_idx[idx];
1166 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1167 position * h->key_entry_size);
1168 /* Return key and data */
1169 *key = next_key->key;
1170 *data = next_key->pdata;
1172 /* Increment iterator */
1175 return position - 1;