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->rte_hash_custom_cmp_eq = func;
105 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
107 if (h->cmp_jump_table_idx == KEY_CUSTOM)
108 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
110 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
114 rte_hash_create(const struct rte_hash_parameters *params)
116 struct rte_hash *h = NULL;
117 struct rte_tailq_entry *te = NULL;
118 struct rte_hash_list *hash_list;
119 struct rte_ring *r = NULL;
120 char hash_name[RTE_HASH_NAMESIZE];
122 void *buckets = NULL;
123 char ring_name[RTE_RING_NAMESIZE];
124 unsigned num_key_slots;
125 unsigned hw_trans_mem_support = 0;
128 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
130 if (params == NULL) {
131 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
135 /* Check for valid parameters */
136 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
137 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
138 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
139 (params->key_len == 0)) {
141 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
145 /* Check extra flags field to check extra options. */
146 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
147 hw_trans_mem_support = 1;
149 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
150 if (hw_trans_mem_support)
152 * Increase number of slots by total number of indices
153 * that can be stored in the lcore caches
154 * except for the first cache
156 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
157 LCORE_CACHE_SIZE + 1;
159 num_key_slots = params->entries + 1;
161 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
162 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
163 params->socket_id, 0);
165 RTE_LOG(ERR, HASH, "memory allocation failed\n");
169 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
171 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
173 /* guarantee there's no existing: this is normally already checked
174 * by ring creation above */
175 TAILQ_FOREACH(te, hash_list, next) {
176 h = (struct rte_hash *) te->data;
177 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
187 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
189 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
193 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
194 RTE_CACHE_LINE_SIZE, params->socket_id);
197 RTE_LOG(ERR, HASH, "memory allocation failed\n");
201 const uint32_t num_buckets = rte_align32pow2(params->entries)
202 / RTE_HASH_BUCKET_ENTRIES;
204 buckets = rte_zmalloc_socket(NULL,
205 num_buckets * sizeof(struct rte_hash_bucket),
206 RTE_CACHE_LINE_SIZE, params->socket_id);
208 if (buckets == NULL) {
209 RTE_LOG(ERR, HASH, "memory allocation failed\n");
213 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
214 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
216 k = rte_zmalloc_socket(NULL, key_tbl_size,
217 RTE_CACHE_LINE_SIZE, params->socket_id);
220 RTE_LOG(ERR, HASH, "memory allocation failed\n");
225 * If x86 architecture is used, select appropriate compare function,
226 * which may use x86 intrinsics, otherwise use memcmp
228 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
229 /* Select function to compare keys */
230 switch (params->key_len) {
232 h->cmp_jump_table_idx = KEY_16_BYTES;
235 h->cmp_jump_table_idx = KEY_32_BYTES;
238 h->cmp_jump_table_idx = KEY_48_BYTES;
241 h->cmp_jump_table_idx = KEY_64_BYTES;
244 h->cmp_jump_table_idx = KEY_80_BYTES;
247 h->cmp_jump_table_idx = KEY_96_BYTES;
250 h->cmp_jump_table_idx = KEY_112_BYTES;
253 h->cmp_jump_table_idx = KEY_128_BYTES;
256 /* If key is not multiple of 16, use generic memcmp */
257 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
260 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
263 if (hw_trans_mem_support) {
264 h->local_free_slots = rte_zmalloc_socket(NULL,
265 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
266 RTE_CACHE_LINE_SIZE, params->socket_id);
269 /* Setup hash context */
270 snprintf(h->name, sizeof(h->name), "%s", params->name);
271 h->entries = params->entries;
272 h->key_len = params->key_len;
273 h->key_entry_size = key_entry_size;
274 h->hash_func_init_val = params->hash_func_init_val;
276 h->num_buckets = num_buckets;
277 h->bucket_bitmask = h->num_buckets - 1;
278 h->buckets = buckets;
279 h->hash_func = (params->hash_func == NULL) ?
280 DEFAULT_HASH_FUNC : params->hash_func;
283 h->hw_trans_mem_support = hw_trans_mem_support;
285 /* Turn on multi-writer only with explicit flat from user and TM
288 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
289 if (h->hw_trans_mem_support) {
290 h->add_key = ADD_KEY_MULTIWRITER_TM;
292 h->add_key = ADD_KEY_MULTIWRITER;
293 h->multiwriter_lock = rte_malloc(NULL,
294 sizeof(rte_spinlock_t),
296 rte_spinlock_init(h->multiwriter_lock);
299 h->add_key = ADD_KEY_SINGLEWRITER;
301 /* Populate free slots ring. Entry zero is reserved for key misses. */
302 for (i = 1; i < params->entries + 1; i++)
303 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
305 te->data = (void *) h;
306 TAILQ_INSERT_TAIL(hash_list, te, next);
307 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
311 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
322 rte_hash_free(struct rte_hash *h)
324 struct rte_tailq_entry *te;
325 struct rte_hash_list *hash_list;
330 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
332 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
334 /* find out tailq entry */
335 TAILQ_FOREACH(te, hash_list, next) {
336 if (te->data == (void *) h)
341 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
345 TAILQ_REMOVE(hash_list, te, next);
347 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
349 if (h->hw_trans_mem_support)
350 rte_free(h->local_free_slots);
352 if (h->add_key == ADD_KEY_MULTIWRITER)
353 rte_free(h->multiwriter_lock);
354 rte_ring_free(h->free_slots);
355 rte_free(h->key_store);
356 rte_free(h->buckets);
362 rte_hash_hash(const struct rte_hash *h, const void *key)
364 /* calc hash result by key */
365 return h->hash_func(key, h->key_len, h->hash_func_init_val);
368 /* Calc the secondary hash value from the primary hash value of a given key */
369 static inline hash_sig_t
370 rte_hash_secondary_hash(const hash_sig_t primary_hash)
372 static const unsigned all_bits_shift = 12;
373 static const unsigned alt_bits_xor = 0x5bd1e995;
375 uint32_t tag = primary_hash >> all_bits_shift;
377 return primary_hash ^ ((tag + 1) * alt_bits_xor);
381 rte_hash_reset(struct rte_hash *h)
389 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
390 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
392 /* clear the free ring */
393 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
396 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
397 for (i = 1; i < h->entries + 1; i++)
398 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
400 if (h->hw_trans_mem_support) {
401 /* Reset local caches per lcore */
402 for (i = 0; i < RTE_MAX_LCORE; i++)
403 h->local_free_slots[i].len = 0;
407 /* Search for an entry that can be pushed to its alternative location */
409 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
413 uint32_t next_bucket_idx;
414 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
417 * Push existing item (search for bucket with space in
418 * alternative locations) to its alternative location
420 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
421 /* Search for space in alternative locations */
422 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
423 next_bkt[i] = &h->buckets[next_bucket_idx];
424 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
425 if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
429 if (j != RTE_HASH_BUCKET_ENTRIES)
433 /* Alternative location has spare room (end of recursive function) */
434 if (i != RTE_HASH_BUCKET_ENTRIES) {
435 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
436 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
437 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
441 /* Pick entry that has not been pushed yet */
442 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
443 if (bkt->flag[i] == 0)
446 /* All entries have been pushed, so entry cannot be added */
447 if (i == RTE_HASH_BUCKET_ENTRIES)
450 /* Set flag to indicate that this entry is going to be pushed */
452 /* Need room in alternative bucket to insert the pushed entry */
453 ret = make_space_bucket(h, next_bkt[i]);
455 * After recursive function.
456 * Clear flags and insert the pushed entry
457 * in its alternative location if successful,
462 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
463 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
464 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
472 * Function called to enqueue back an index in the cache/ring,
473 * as slot has not being used and it can be used in the
474 * next addition attempt.
477 enqueue_slot_back(const struct rte_hash *h,
478 struct lcore_cache *cached_free_slots,
481 if (h->hw_trans_mem_support) {
482 cached_free_slots->objs[cached_free_slots->len] = slot_id;
483 cached_free_slots->len++;
485 rte_ring_sp_enqueue(h->free_slots, slot_id);
488 static inline int32_t
489 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
490 hash_sig_t sig, void *data)
493 uint32_t prim_bucket_idx, sec_bucket_idx;
495 struct rte_hash_bucket *prim_bkt, *sec_bkt;
496 struct rte_hash_key *new_k, *k, *keys = h->key_store;
497 void *slot_id = NULL;
502 struct lcore_cache *cached_free_slots = NULL;
504 if (h->add_key == ADD_KEY_MULTIWRITER)
505 rte_spinlock_lock(h->multiwriter_lock);
507 prim_bucket_idx = sig & h->bucket_bitmask;
508 prim_bkt = &h->buckets[prim_bucket_idx];
509 rte_prefetch0(prim_bkt);
511 alt_hash = rte_hash_secondary_hash(sig);
512 sec_bucket_idx = alt_hash & h->bucket_bitmask;
513 sec_bkt = &h->buckets[sec_bucket_idx];
514 rte_prefetch0(sec_bkt);
516 /* Get a new slot for storing the new key */
517 if (h->hw_trans_mem_support) {
518 lcore_id = rte_lcore_id();
519 cached_free_slots = &h->local_free_slots[lcore_id];
520 /* Try to get a free slot from the local cache */
521 if (cached_free_slots->len == 0) {
522 /* Need to get another burst of free slots from global ring */
523 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
524 cached_free_slots->objs, LCORE_CACHE_SIZE);
528 cached_free_slots->len += n_slots;
531 /* Get a free slot from the local cache */
532 cached_free_slots->len--;
533 slot_id = cached_free_slots->objs[cached_free_slots->len];
535 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
539 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
540 rte_prefetch0(new_k);
541 new_idx = (uint32_t)((uintptr_t) slot_id);
543 /* Check if key is already inserted in primary location */
544 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
545 if (prim_bkt->signatures[i].current == sig &&
546 prim_bkt->signatures[i].alt == alt_hash) {
547 k = (struct rte_hash_key *) ((char *)keys +
548 prim_bkt->key_idx[i] * h->key_entry_size);
549 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
550 /* Enqueue index of free slot back in the ring. */
551 enqueue_slot_back(h, cached_free_slots, slot_id);
555 * Return index where key is stored,
556 * substracting the first dummy index
558 return prim_bkt->key_idx[i] - 1;
563 /* Check if key is already inserted in secondary location */
564 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
565 if (sec_bkt->signatures[i].alt == sig &&
566 sec_bkt->signatures[i].current == alt_hash) {
567 k = (struct rte_hash_key *) ((char *)keys +
568 sec_bkt->key_idx[i] * h->key_entry_size);
569 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
570 /* Enqueue index of free slot back in the ring. */
571 enqueue_slot_back(h, cached_free_slots, slot_id);
575 * Return index where key is stored,
576 * substracting the first dummy index
578 return sec_bkt->key_idx[i] - 1;
584 rte_memcpy(new_k->key, key, h->key_len);
587 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
588 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
589 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
590 sig, alt_hash, new_idx);
594 /* Primary bucket full, need to make space for new entry */
595 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
601 /* Also search secondary bucket to get better occupancy */
602 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
609 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
610 /* Check if slot is available */
611 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
612 prim_bkt->signatures[i].current = sig;
613 prim_bkt->signatures[i].alt = alt_hash;
614 prim_bkt->key_idx[i] = new_idx;
619 if (i != RTE_HASH_BUCKET_ENTRIES) {
620 if (h->add_key == ADD_KEY_MULTIWRITER)
621 rte_spinlock_unlock(h->multiwriter_lock);
625 /* Primary bucket full, need to make space for new entry
626 * After recursive function.
627 * Insert the new entry in the position of the pushed entry
628 * if successful or return error and
629 * store the new slot back in the ring
631 ret = make_space_bucket(h, prim_bkt);
633 prim_bkt->signatures[ret].current = sig;
634 prim_bkt->signatures[ret].alt = alt_hash;
635 prim_bkt->key_idx[ret] = new_idx;
636 if (h->add_key == ADD_KEY_MULTIWRITER)
637 rte_spinlock_unlock(h->multiwriter_lock);
640 #if defined(RTE_ARCH_X86)
643 /* Error in addition, store new slot back in the ring and return error */
644 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
646 if (h->add_key == ADD_KEY_MULTIWRITER)
647 rte_spinlock_unlock(h->multiwriter_lock);
652 rte_hash_add_key_with_hash(const struct rte_hash *h,
653 const void *key, hash_sig_t sig)
655 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
656 return __rte_hash_add_key_with_hash(h, key, sig, 0);
660 rte_hash_add_key(const struct rte_hash *h, const void *key)
662 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
663 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
667 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
668 const void *key, hash_sig_t sig, void *data)
672 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
673 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
681 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
685 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
687 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
693 static inline int32_t
694 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
695 hash_sig_t sig, void **data)
700 struct rte_hash_bucket *bkt;
701 struct rte_hash_key *k, *keys = h->key_store;
703 bucket_idx = sig & h->bucket_bitmask;
704 bkt = &h->buckets[bucket_idx];
706 /* Check if key is in primary location */
707 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
708 if (bkt->signatures[i].current == sig &&
709 bkt->signatures[i].sig != NULL_SIGNATURE) {
710 k = (struct rte_hash_key *) ((char *)keys +
711 bkt->key_idx[i] * h->key_entry_size);
712 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
716 * Return index where key is stored,
717 * substracting the first dummy index
719 return bkt->key_idx[i] - 1;
724 /* Calculate secondary hash */
725 alt_hash = rte_hash_secondary_hash(sig);
726 bucket_idx = alt_hash & h->bucket_bitmask;
727 bkt = &h->buckets[bucket_idx];
729 /* Check if key is in secondary location */
730 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
731 if (bkt->signatures[i].current == alt_hash &&
732 bkt->signatures[i].alt == sig) {
733 k = (struct rte_hash_key *) ((char *)keys +
734 bkt->key_idx[i] * h->key_entry_size);
735 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
739 * Return index where key is stored,
740 * substracting the first dummy index
742 return bkt->key_idx[i] - 1;
751 rte_hash_lookup_with_hash(const struct rte_hash *h,
752 const void *key, hash_sig_t sig)
754 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
755 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
759 rte_hash_lookup(const struct rte_hash *h, const void *key)
761 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
762 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
766 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
767 const void *key, hash_sig_t sig, void **data)
769 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
770 return __rte_hash_lookup_with_hash(h, key, sig, data);
774 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
776 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
777 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
781 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
783 unsigned lcore_id, n_slots;
784 struct lcore_cache *cached_free_slots;
786 bkt->signatures[i].sig = NULL_SIGNATURE;
787 if (h->hw_trans_mem_support) {
788 lcore_id = rte_lcore_id();
789 cached_free_slots = &h->local_free_slots[lcore_id];
790 /* Cache full, need to free it. */
791 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
792 /* Need to enqueue the free slots in global ring. */
793 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
794 cached_free_slots->objs,
796 cached_free_slots->len -= n_slots;
798 /* Put index of new free slot in cache. */
799 cached_free_slots->objs[cached_free_slots->len] =
800 (void *)((uintptr_t)bkt->key_idx[i]);
801 cached_free_slots->len++;
803 rte_ring_sp_enqueue(h->free_slots,
804 (void *)((uintptr_t)bkt->key_idx[i]));
808 static inline int32_t
809 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
815 struct rte_hash_bucket *bkt;
816 struct rte_hash_key *k, *keys = h->key_store;
818 bucket_idx = sig & h->bucket_bitmask;
819 bkt = &h->buckets[bucket_idx];
821 /* Check if key is in primary location */
822 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
823 if (bkt->signatures[i].current == sig &&
824 bkt->signatures[i].sig != NULL_SIGNATURE) {
825 k = (struct rte_hash_key *) ((char *)keys +
826 bkt->key_idx[i] * h->key_entry_size);
827 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
828 remove_entry(h, bkt, i);
831 * Return index where key is stored,
832 * substracting the first dummy index
834 return bkt->key_idx[i] - 1;
839 /* Calculate secondary hash */
840 alt_hash = rte_hash_secondary_hash(sig);
841 bucket_idx = alt_hash & h->bucket_bitmask;
842 bkt = &h->buckets[bucket_idx];
844 /* Check if key is in secondary location */
845 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
846 if (bkt->signatures[i].current == alt_hash &&
847 bkt->signatures[i].sig != NULL_SIGNATURE) {
848 k = (struct rte_hash_key *) ((char *)keys +
849 bkt->key_idx[i] * h->key_entry_size);
850 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
851 remove_entry(h, bkt, i);
854 * Return index where key is stored,
855 * substracting the first dummy index
857 return bkt->key_idx[i] - 1;
866 rte_hash_del_key_with_hash(const struct rte_hash *h,
867 const void *key, hash_sig_t sig)
869 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
870 return __rte_hash_del_key_with_hash(h, key, sig);
874 rte_hash_del_key(const struct rte_hash *h, const void *key)
876 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
877 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
881 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
884 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
886 struct rte_hash_key *k, *keys = h->key_store;
887 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
892 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
900 /* Lookup bulk stage 0: Prefetch input key */
902 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
903 const void * const *keys)
905 *idx = __builtin_ctzl(*lookup_mask);
906 if (*lookup_mask == 0)
909 rte_prefetch0(keys[*idx]);
910 *lookup_mask &= ~(1llu << *idx);
914 * Lookup bulk stage 1: Calculate primary/secondary hashes
915 * and prefetch primary/secondary buckets
918 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
919 const struct rte_hash_bucket **primary_bkt,
920 const struct rte_hash_bucket **secondary_bkt,
921 hash_sig_t *hash_vals, const void * const *keys,
922 const struct rte_hash *h)
924 *prim_hash = rte_hash_hash(h, keys[idx]);
925 hash_vals[idx] = *prim_hash;
926 *sec_hash = rte_hash_secondary_hash(*prim_hash);
928 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
929 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
931 rte_prefetch0(*primary_bkt);
932 rte_prefetch0(*secondary_bkt);
936 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
937 * and prefetch first key slot
940 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
941 const struct rte_hash_bucket *prim_bkt,
942 const struct rte_hash_bucket *sec_bkt,
943 const struct rte_hash_key **key_slot, int32_t *positions,
944 uint64_t *extra_hits_mask, const void *keys,
945 const struct rte_hash *h)
947 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
948 unsigned total_hash_matches;
950 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
951 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
952 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
953 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
954 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
957 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
959 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
961 total_hash_matches = (prim_hash_matches |
962 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
963 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
964 key_idx * h->key_entry_size);
966 rte_prefetch0(*key_slot);
968 * Return index where key is stored,
969 * substracting the first dummy index
971 positions[idx] = (key_idx - 1);
973 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
978 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
980 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
981 const int32_t *positions, void *data[], uint64_t *hits,
982 const struct rte_hash *h)
987 hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
989 data[idx] = key_slot->pdata;
991 key_idx = positions[idx] + 1;
993 * If key index is 0, force hit to be 0, in case key to be looked up
994 * is all zero (as in the dummy slot), which would result in a wrong hit
996 *hits |= (uint64_t)(hit && !!key_idx) << idx;
1000 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1001 uint32_t num_keys, int32_t *positions,
1002 uint64_t *hit_mask, void *data[])
1005 uint64_t extra_hits_mask = 0;
1006 uint64_t lookup_mask, miss_mask;
1008 const void *key_store = h->key_store;
1010 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1012 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1013 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1014 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1015 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1016 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1017 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1018 hash_sig_t primary_hash10, primary_hash11;
1019 hash_sig_t secondary_hash10, secondary_hash11;
1020 hash_sig_t primary_hash20, primary_hash21;
1021 hash_sig_t secondary_hash20, secondary_hash21;
1023 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1024 miss_mask = lookup_mask;
1026 lookup_stage0(&idx00, &lookup_mask, keys);
1027 lookup_stage0(&idx01, &lookup_mask, keys);
1029 idx10 = idx00, idx11 = idx01;
1031 lookup_stage0(&idx00, &lookup_mask, keys);
1032 lookup_stage0(&idx01, &lookup_mask, keys);
1033 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1034 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1035 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1036 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1038 primary_bkt20 = primary_bkt10;
1039 primary_bkt21 = primary_bkt11;
1040 secondary_bkt20 = secondary_bkt10;
1041 secondary_bkt21 = secondary_bkt11;
1042 primary_hash20 = primary_hash10;
1043 primary_hash21 = primary_hash11;
1044 secondary_hash20 = secondary_hash10;
1045 secondary_hash21 = secondary_hash11;
1046 idx20 = idx10, idx21 = idx11;
1047 idx10 = idx00, idx11 = idx01;
1049 lookup_stage0(&idx00, &lookup_mask, keys);
1050 lookup_stage0(&idx01, &lookup_mask, keys);
1051 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1052 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1053 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1054 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1055 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1056 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1058 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1059 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1062 while (lookup_mask) {
1063 k_slot30 = k_slot20, k_slot31 = k_slot21;
1064 idx30 = idx20, idx31 = idx21;
1065 primary_bkt20 = primary_bkt10;
1066 primary_bkt21 = primary_bkt11;
1067 secondary_bkt20 = secondary_bkt10;
1068 secondary_bkt21 = secondary_bkt11;
1069 primary_hash20 = primary_hash10;
1070 primary_hash21 = primary_hash11;
1071 secondary_hash20 = secondary_hash10;
1072 secondary_hash21 = secondary_hash11;
1073 idx20 = idx10, idx21 = idx11;
1074 idx10 = idx00, idx11 = idx01;
1076 lookup_stage0(&idx00, &lookup_mask, keys);
1077 lookup_stage0(&idx01, &lookup_mask, keys);
1078 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1079 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1080 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1081 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1082 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1083 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1084 &extra_hits_mask, key_store, h);
1085 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1086 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1087 &extra_hits_mask, key_store, h);
1088 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1089 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1092 k_slot30 = k_slot20, k_slot31 = k_slot21;
1093 idx30 = idx20, idx31 = idx21;
1094 primary_bkt20 = primary_bkt10;
1095 primary_bkt21 = primary_bkt11;
1096 secondary_bkt20 = secondary_bkt10;
1097 secondary_bkt21 = secondary_bkt11;
1098 primary_hash20 = primary_hash10;
1099 primary_hash21 = primary_hash11;
1100 secondary_hash20 = secondary_hash10;
1101 secondary_hash21 = secondary_hash11;
1102 idx20 = idx10, idx21 = idx11;
1103 idx10 = idx00, idx11 = idx01;
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);
1109 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1110 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1112 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1113 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1115 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1116 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1118 k_slot30 = k_slot20, k_slot31 = k_slot21;
1119 idx30 = idx20, idx31 = idx21;
1120 primary_bkt20 = primary_bkt10;
1121 primary_bkt21 = primary_bkt11;
1122 secondary_bkt20 = secondary_bkt10;
1123 secondary_bkt21 = secondary_bkt11;
1124 primary_hash20 = primary_hash10;
1125 primary_hash21 = primary_hash11;
1126 secondary_hash20 = secondary_hash10;
1127 secondary_hash21 = secondary_hash11;
1128 idx20 = idx10, idx21 = idx11;
1130 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1131 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1133 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1134 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1136 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1137 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1139 k_slot30 = k_slot20, k_slot31 = k_slot21;
1140 idx30 = idx20, idx31 = idx21;
1142 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1143 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1145 /* ignore any items we have already found */
1146 extra_hits_mask &= ~hits;
1148 if (unlikely(extra_hits_mask)) {
1149 /* run a single search for each remaining item */
1151 idx = __builtin_ctzl(extra_hits_mask);
1153 ret = rte_hash_lookup_with_hash_data(h,
1154 keys[idx], hash_vals[idx], &data[idx]);
1156 hits |= 1ULL << idx;
1158 positions[idx] = rte_hash_lookup_with_hash(h,
1159 keys[idx], hash_vals[idx]);
1160 if (positions[idx] >= 0)
1161 hits |= 1llu << idx;
1163 extra_hits_mask &= ~(1llu << idx);
1164 } while (extra_hits_mask);
1168 if (unlikely(miss_mask)) {
1170 idx = __builtin_ctzl(miss_mask);
1171 positions[idx] = -ENOENT;
1172 miss_mask &= ~(1llu << idx);
1173 } while (miss_mask);
1176 if (hit_mask != NULL)
1181 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1182 uint32_t num_keys, int32_t *positions)
1184 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1185 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1186 (positions == NULL)), -EINVAL);
1188 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1193 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1194 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1196 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1197 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1198 (hit_mask == NULL)), -EINVAL);
1200 int32_t positions[num_keys];
1202 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1204 /* Return number of hits */
1205 return __builtin_popcountl(*hit_mask);
1209 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1211 uint32_t bucket_idx, idx, position;
1212 struct rte_hash_key *next_key;
1214 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1216 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1218 if (*next >= total_entries)
1221 /* Calculate bucket and index of current iterator */
1222 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1223 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1225 /* If current position is empty, go to the next one */
1226 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1229 if (*next == total_entries)
1231 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1232 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1235 /* Get position of entry in key table */
1236 position = h->buckets[bucket_idx].key_idx[idx];
1237 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1238 position * h->key_entry_size);
1239 /* Return key and data */
1240 *key = next_key->key;
1241 *data = next_key->pdata;
1243 /* Increment iterator */
1246 return position - 1;