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 /* Turn on multi-writer only with explicit flat from user and TM
290 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
291 if (h->hw_trans_mem_support) {
292 h->add_key = ADD_KEY_MULTIWRITER_TM;
294 h->add_key = ADD_KEY_MULTIWRITER;
295 h->multiwriter_lock = rte_malloc(NULL,
296 sizeof(rte_spinlock_t),
298 rte_spinlock_init(h->multiwriter_lock);
301 h->add_key = ADD_KEY_SINGLEWRITER;
303 /* Populate free slots ring. Entry zero is reserved for key misses. */
304 for (i = 1; i < params->entries + 1; i++)
305 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
307 te->data = (void *) h;
308 TAILQ_INSERT_TAIL(hash_list, te, next);
309 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
313 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
324 rte_hash_free(struct rte_hash *h)
326 struct rte_tailq_entry *te;
327 struct rte_hash_list *hash_list;
332 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
334 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
336 /* find out tailq entry */
337 TAILQ_FOREACH(te, hash_list, next) {
338 if (te->data == (void *) h)
343 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
347 TAILQ_REMOVE(hash_list, te, next);
349 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
351 if (h->hw_trans_mem_support)
352 rte_free(h->local_free_slots);
354 if (h->add_key == ADD_KEY_MULTIWRITER)
355 rte_free(h->multiwriter_lock);
356 rte_ring_free(h->free_slots);
357 rte_free(h->key_store);
358 rte_free(h->buckets);
364 rte_hash_hash(const struct rte_hash *h, const void *key)
366 /* calc hash result by key */
367 return h->hash_func(key, h->key_len, h->hash_func_init_val);
370 /* Calc the secondary hash value from the primary hash value of a given key */
371 static inline hash_sig_t
372 rte_hash_secondary_hash(const hash_sig_t primary_hash)
374 static const unsigned all_bits_shift = 12;
375 static const unsigned alt_bits_xor = 0x5bd1e995;
377 uint32_t tag = primary_hash >> all_bits_shift;
379 return primary_hash ^ ((tag + 1) * alt_bits_xor);
383 rte_hash_reset(struct rte_hash *h)
391 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
392 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
394 /* clear the free ring */
395 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
398 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
399 for (i = 1; i < h->entries + 1; i++)
400 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
402 if (h->hw_trans_mem_support) {
403 /* Reset local caches per lcore */
404 for (i = 0; i < RTE_MAX_LCORE; i++)
405 h->local_free_slots[i].len = 0;
409 /* Search for an entry that can be pushed to its alternative location */
411 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
415 uint32_t next_bucket_idx;
416 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
419 * Push existing item (search for bucket with space in
420 * alternative locations) to its alternative location
422 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
423 /* Search for space in alternative locations */
424 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
425 next_bkt[i] = &h->buckets[next_bucket_idx];
426 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
427 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
431 if (j != RTE_HASH_BUCKET_ENTRIES)
435 /* Alternative location has spare room (end of recursive function) */
436 if (i != RTE_HASH_BUCKET_ENTRIES) {
437 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
438 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
439 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
443 /* Pick entry that has not been pushed yet */
444 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
445 if (bkt->flag[i] == 0)
448 /* All entries have been pushed, so entry cannot be added */
449 if (i == RTE_HASH_BUCKET_ENTRIES)
452 /* Set flag to indicate that this entry is going to be pushed */
454 /* Need room in alternative bucket to insert the pushed entry */
455 ret = make_space_bucket(h, next_bkt[i]);
457 * After recursive function.
458 * Clear flags and insert the pushed entry
459 * in its alternative location if successful,
464 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
465 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
466 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
474 * Function called to enqueue back an index in the cache/ring,
475 * as slot has not being used and it can be used in the
476 * next addition attempt.
479 enqueue_slot_back(const struct rte_hash *h,
480 struct lcore_cache *cached_free_slots,
483 if (h->hw_trans_mem_support) {
484 cached_free_slots->objs[cached_free_slots->len] = slot_id;
485 cached_free_slots->len++;
487 rte_ring_sp_enqueue(h->free_slots, slot_id);
490 static inline int32_t
491 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
492 hash_sig_t sig, void *data)
495 uint32_t prim_bucket_idx, sec_bucket_idx;
497 struct rte_hash_bucket *prim_bkt, *sec_bkt;
498 struct rte_hash_key *new_k, *k, *keys = h->key_store;
499 void *slot_id = NULL;
504 struct lcore_cache *cached_free_slots = NULL;
506 if (h->add_key == ADD_KEY_MULTIWRITER)
507 rte_spinlock_lock(h->multiwriter_lock);
509 prim_bucket_idx = sig & h->bucket_bitmask;
510 prim_bkt = &h->buckets[prim_bucket_idx];
511 rte_prefetch0(prim_bkt);
513 alt_hash = rte_hash_secondary_hash(sig);
514 sec_bucket_idx = alt_hash & h->bucket_bitmask;
515 sec_bkt = &h->buckets[sec_bucket_idx];
516 rte_prefetch0(sec_bkt);
518 /* Get a new slot for storing the new key */
519 if (h->hw_trans_mem_support) {
520 lcore_id = rte_lcore_id();
521 cached_free_slots = &h->local_free_slots[lcore_id];
522 /* Try to get a free slot from the local cache */
523 if (cached_free_slots->len == 0) {
524 /* Need to get another burst of free slots from global ring */
525 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
526 cached_free_slots->objs, LCORE_CACHE_SIZE);
530 cached_free_slots->len += n_slots;
533 /* Get a free slot from the local cache */
534 cached_free_slots->len--;
535 slot_id = cached_free_slots->objs[cached_free_slots->len];
537 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
541 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
542 rte_prefetch0(new_k);
543 new_idx = (uint32_t)((uintptr_t) slot_id);
545 /* Check if key is already inserted in primary location */
546 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
547 if (prim_bkt->sig_current[i] == sig &&
548 prim_bkt->sig_alt[i] == alt_hash) {
549 k = (struct rte_hash_key *) ((char *)keys +
550 prim_bkt->key_idx[i] * h->key_entry_size);
551 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
552 /* Enqueue index of free slot back in the ring. */
553 enqueue_slot_back(h, cached_free_slots, slot_id);
557 * Return index where key is stored,
558 * substracting the first dummy index
560 return prim_bkt->key_idx[i] - 1;
565 /* Check if key is already inserted in secondary location */
566 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
567 if (sec_bkt->sig_alt[i] == sig &&
568 sec_bkt->sig_current[i] == alt_hash) {
569 k = (struct rte_hash_key *) ((char *)keys +
570 sec_bkt->key_idx[i] * h->key_entry_size);
571 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
572 /* Enqueue index of free slot back in the ring. */
573 enqueue_slot_back(h, cached_free_slots, slot_id);
577 * Return index where key is stored,
578 * substracting the first dummy index
580 return sec_bkt->key_idx[i] - 1;
586 rte_memcpy(new_k->key, key, h->key_len);
589 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
590 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
591 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
592 sig, alt_hash, new_idx);
596 /* Primary bucket full, need to make space for new entry */
597 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
603 /* Also search secondary bucket to get better occupancy */
604 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
611 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
612 /* Check if slot is available */
613 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
614 prim_bkt->sig_current[i] = sig;
615 prim_bkt->sig_alt[i] = alt_hash;
616 prim_bkt->key_idx[i] = new_idx;
621 if (i != RTE_HASH_BUCKET_ENTRIES) {
622 if (h->add_key == ADD_KEY_MULTIWRITER)
623 rte_spinlock_unlock(h->multiwriter_lock);
627 /* Primary bucket full, need to make space for new entry
628 * After recursive function.
629 * Insert the new entry in the position of the pushed entry
630 * if successful or return error and
631 * store the new slot back in the ring
633 ret = make_space_bucket(h, prim_bkt);
635 prim_bkt->sig_current[ret] = sig;
636 prim_bkt->sig_alt[ret] = alt_hash;
637 prim_bkt->key_idx[ret] = new_idx;
638 if (h->add_key == ADD_KEY_MULTIWRITER)
639 rte_spinlock_unlock(h->multiwriter_lock);
642 #if defined(RTE_ARCH_X86)
645 /* Error in addition, store new slot back in the ring and return error */
646 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
648 if (h->add_key == ADD_KEY_MULTIWRITER)
649 rte_spinlock_unlock(h->multiwriter_lock);
654 rte_hash_add_key_with_hash(const struct rte_hash *h,
655 const void *key, hash_sig_t sig)
657 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
658 return __rte_hash_add_key_with_hash(h, key, sig, 0);
662 rte_hash_add_key(const struct rte_hash *h, const void *key)
664 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
665 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
669 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
670 const void *key, hash_sig_t sig, void *data)
674 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
675 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
683 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
687 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
689 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
695 static inline int32_t
696 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
697 hash_sig_t sig, void **data)
702 struct rte_hash_bucket *bkt;
703 struct rte_hash_key *k, *keys = h->key_store;
705 bucket_idx = sig & h->bucket_bitmask;
706 bkt = &h->buckets[bucket_idx];
708 /* Check if key is in primary location */
709 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
710 if (bkt->sig_current[i] == sig &&
711 bkt->key_idx[i] != EMPTY_SLOT) {
712 k = (struct rte_hash_key *) ((char *)keys +
713 bkt->key_idx[i] * h->key_entry_size);
714 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
718 * Return index where key is stored,
719 * substracting the first dummy index
721 return bkt->key_idx[i] - 1;
726 /* Calculate secondary hash */
727 alt_hash = rte_hash_secondary_hash(sig);
728 bucket_idx = alt_hash & h->bucket_bitmask;
729 bkt = &h->buckets[bucket_idx];
731 /* Check if key is in secondary location */
732 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
733 if (bkt->sig_current[i] == alt_hash &&
734 bkt->sig_alt[i] == sig) {
735 k = (struct rte_hash_key *) ((char *)keys +
736 bkt->key_idx[i] * h->key_entry_size);
737 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
741 * Return index where key is stored,
742 * substracting the first dummy index
744 return bkt->key_idx[i] - 1;
753 rte_hash_lookup_with_hash(const struct rte_hash *h,
754 const void *key, hash_sig_t sig)
756 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
757 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
761 rte_hash_lookup(const struct rte_hash *h, const void *key)
763 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
764 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
768 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
769 const void *key, hash_sig_t sig, void **data)
771 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
772 return __rte_hash_lookup_with_hash(h, key, sig, data);
776 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
778 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
779 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
783 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
785 unsigned lcore_id, n_slots;
786 struct lcore_cache *cached_free_slots;
788 bkt->sig_current[i] = NULL_SIGNATURE;
789 bkt->sig_alt[i] = NULL_SIGNATURE;
790 if (h->hw_trans_mem_support) {
791 lcore_id = rte_lcore_id();
792 cached_free_slots = &h->local_free_slots[lcore_id];
793 /* Cache full, need to free it. */
794 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
795 /* Need to enqueue the free slots in global ring. */
796 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
797 cached_free_slots->objs,
799 cached_free_slots->len -= n_slots;
801 /* Put index of new free slot in cache. */
802 cached_free_slots->objs[cached_free_slots->len] =
803 (void *)((uintptr_t)bkt->key_idx[i]);
804 cached_free_slots->len++;
806 rte_ring_sp_enqueue(h->free_slots,
807 (void *)((uintptr_t)bkt->key_idx[i]));
811 static inline int32_t
812 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
818 struct rte_hash_bucket *bkt;
819 struct rte_hash_key *k, *keys = h->key_store;
822 bucket_idx = sig & h->bucket_bitmask;
823 bkt = &h->buckets[bucket_idx];
825 /* Check if key is in primary location */
826 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
827 if (bkt->sig_current[i] == sig &&
828 bkt->key_idx[i] != EMPTY_SLOT) {
829 k = (struct rte_hash_key *) ((char *)keys +
830 bkt->key_idx[i] * h->key_entry_size);
831 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
832 remove_entry(h, bkt, i);
835 * Return index where key is stored,
836 * substracting the first dummy index
838 ret = bkt->key_idx[i] - 1;
839 bkt->key_idx[i] = EMPTY_SLOT;
845 /* Calculate secondary hash */
846 alt_hash = rte_hash_secondary_hash(sig);
847 bucket_idx = alt_hash & h->bucket_bitmask;
848 bkt = &h->buckets[bucket_idx];
850 /* Check if key is in secondary location */
851 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
852 if (bkt->sig_current[i] == alt_hash &&
853 bkt->key_idx[i] != EMPTY_SLOT) {
854 k = (struct rte_hash_key *) ((char *)keys +
855 bkt->key_idx[i] * h->key_entry_size);
856 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
857 remove_entry(h, bkt, i);
860 * Return index where key is stored,
861 * substracting the first dummy index
863 ret = bkt->key_idx[i] - 1;
864 bkt->key_idx[i] = EMPTY_SLOT;
874 rte_hash_del_key_with_hash(const struct rte_hash *h,
875 const void *key, hash_sig_t sig)
877 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
878 return __rte_hash_del_key_with_hash(h, key, sig);
882 rte_hash_del_key(const struct rte_hash *h, const void *key)
884 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
885 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
889 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
892 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
894 struct rte_hash_key *k, *keys = h->key_store;
895 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
900 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
908 /* Lookup bulk stage 0: Prefetch input key */
910 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
911 const void * const *keys)
913 *idx = __builtin_ctzl(*lookup_mask);
914 if (*lookup_mask == 0)
917 rte_prefetch0(keys[*idx]);
918 *lookup_mask &= ~(1llu << *idx);
922 * Lookup bulk stage 1: Calculate primary/secondary hashes
923 * and prefetch primary/secondary buckets
926 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
927 const struct rte_hash_bucket **primary_bkt,
928 const struct rte_hash_bucket **secondary_bkt,
929 hash_sig_t *hash_vals, const void * const *keys,
930 const struct rte_hash *h)
932 *prim_hash = rte_hash_hash(h, keys[idx]);
933 hash_vals[idx] = *prim_hash;
934 *sec_hash = rte_hash_secondary_hash(*prim_hash);
936 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
937 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
939 rte_prefetch0(*primary_bkt);
940 rte_prefetch0(*secondary_bkt);
944 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
945 * and prefetch first key slot
948 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
949 const struct rte_hash_bucket *prim_bkt,
950 const struct rte_hash_bucket *sec_bkt,
951 const struct rte_hash_key **key_slot, int32_t *positions,
952 uint64_t *extra_hits_mask, const void *keys,
953 const struct rte_hash *h)
955 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
956 unsigned total_hash_matches;
958 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
959 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
960 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
961 prim_hash_matches |= ((prim_hash == prim_bkt->sig_current[i]) << i);
962 sec_hash_matches |= ((sec_hash == sec_bkt->sig_current[i]) << i);
965 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
967 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
969 total_hash_matches = (prim_hash_matches |
970 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
971 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
972 key_idx * h->key_entry_size);
974 rte_prefetch0(*key_slot);
976 * Return index where key is stored,
977 * substracting the first dummy index
979 positions[idx] = (key_idx - 1);
981 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
986 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
988 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
989 const int32_t *positions, void *data[], uint64_t *hits,
990 const struct rte_hash *h)
995 hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
997 data[idx] = key_slot->pdata;
999 key_idx = positions[idx] + 1;
1001 * If key index is 0, force hit to be 0, in case key to be looked up
1002 * is all zero (as in the dummy slot), which would result in a wrong hit
1004 *hits |= (uint64_t)(hit && !!key_idx) << idx;
1008 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1009 uint32_t num_keys, int32_t *positions,
1010 uint64_t *hit_mask, void *data[])
1013 uint64_t extra_hits_mask = 0;
1014 uint64_t lookup_mask, miss_mask;
1016 const void *key_store = h->key_store;
1018 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1020 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1021 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1022 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1023 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1024 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1025 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1026 hash_sig_t primary_hash10, primary_hash11;
1027 hash_sig_t secondary_hash10, secondary_hash11;
1028 hash_sig_t primary_hash20, primary_hash21;
1029 hash_sig_t secondary_hash20, secondary_hash21;
1031 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1032 miss_mask = lookup_mask;
1034 lookup_stage0(&idx00, &lookup_mask, keys);
1035 lookup_stage0(&idx01, &lookup_mask, keys);
1037 idx10 = idx00, idx11 = idx01;
1039 lookup_stage0(&idx00, &lookup_mask, keys);
1040 lookup_stage0(&idx01, &lookup_mask, keys);
1041 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1042 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1043 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1044 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1046 primary_bkt20 = primary_bkt10;
1047 primary_bkt21 = primary_bkt11;
1048 secondary_bkt20 = secondary_bkt10;
1049 secondary_bkt21 = secondary_bkt11;
1050 primary_hash20 = primary_hash10;
1051 primary_hash21 = primary_hash11;
1052 secondary_hash20 = secondary_hash10;
1053 secondary_hash21 = secondary_hash11;
1054 idx20 = idx10, idx21 = idx11;
1055 idx10 = idx00, idx11 = idx01;
1057 lookup_stage0(&idx00, &lookup_mask, keys);
1058 lookup_stage0(&idx01, &lookup_mask, keys);
1059 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1060 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1061 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1062 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1063 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1064 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1066 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1067 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1070 while (lookup_mask) {
1071 k_slot30 = k_slot20, k_slot31 = k_slot21;
1072 idx30 = idx20, idx31 = idx21;
1073 primary_bkt20 = primary_bkt10;
1074 primary_bkt21 = primary_bkt11;
1075 secondary_bkt20 = secondary_bkt10;
1076 secondary_bkt21 = secondary_bkt11;
1077 primary_hash20 = primary_hash10;
1078 primary_hash21 = primary_hash11;
1079 secondary_hash20 = secondary_hash10;
1080 secondary_hash21 = secondary_hash11;
1081 idx20 = idx10, idx21 = idx11;
1082 idx10 = idx00, idx11 = idx01;
1084 lookup_stage0(&idx00, &lookup_mask, keys);
1085 lookup_stage0(&idx01, &lookup_mask, keys);
1086 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1087 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1088 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1089 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1090 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1091 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1092 &extra_hits_mask, key_store, h);
1093 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1094 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1095 &extra_hits_mask, key_store, h);
1096 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1097 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1100 k_slot30 = k_slot20, k_slot31 = k_slot21;
1101 idx30 = idx20, idx31 = idx21;
1102 primary_bkt20 = primary_bkt10;
1103 primary_bkt21 = primary_bkt11;
1104 secondary_bkt20 = secondary_bkt10;
1105 secondary_bkt21 = secondary_bkt11;
1106 primary_hash20 = primary_hash10;
1107 primary_hash21 = primary_hash11;
1108 secondary_hash20 = secondary_hash10;
1109 secondary_hash21 = secondary_hash11;
1110 idx20 = idx10, idx21 = idx11;
1111 idx10 = idx00, idx11 = idx01;
1113 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1114 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1115 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1116 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1117 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1118 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1120 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1121 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1123 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1124 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1126 k_slot30 = k_slot20, k_slot31 = k_slot21;
1127 idx30 = idx20, idx31 = idx21;
1128 primary_bkt20 = primary_bkt10;
1129 primary_bkt21 = primary_bkt11;
1130 secondary_bkt20 = secondary_bkt10;
1131 secondary_bkt21 = secondary_bkt11;
1132 primary_hash20 = primary_hash10;
1133 primary_hash21 = primary_hash11;
1134 secondary_hash20 = secondary_hash10;
1135 secondary_hash21 = secondary_hash11;
1136 idx20 = idx10, idx21 = idx11;
1138 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1139 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1141 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1142 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1144 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1145 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1147 k_slot30 = k_slot20, k_slot31 = k_slot21;
1148 idx30 = idx20, idx31 = idx21;
1150 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1151 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1153 /* ignore any items we have already found */
1154 extra_hits_mask &= ~hits;
1156 if (unlikely(extra_hits_mask)) {
1157 /* run a single search for each remaining item */
1159 idx = __builtin_ctzl(extra_hits_mask);
1161 ret = rte_hash_lookup_with_hash_data(h,
1162 keys[idx], hash_vals[idx], &data[idx]);
1164 hits |= 1ULL << idx;
1166 positions[idx] = rte_hash_lookup_with_hash(h,
1167 keys[idx], hash_vals[idx]);
1168 if (positions[idx] >= 0)
1169 hits |= 1llu << idx;
1171 extra_hits_mask &= ~(1llu << idx);
1172 } while (extra_hits_mask);
1176 if (unlikely(miss_mask)) {
1178 idx = __builtin_ctzl(miss_mask);
1179 positions[idx] = -ENOENT;
1180 miss_mask &= ~(1llu << idx);
1181 } while (miss_mask);
1184 if (hit_mask != NULL)
1189 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1190 uint32_t num_keys, int32_t *positions)
1192 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1193 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1194 (positions == NULL)), -EINVAL);
1196 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1201 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1202 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1204 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1205 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1206 (hit_mask == NULL)), -EINVAL);
1208 int32_t positions[num_keys];
1210 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1212 /* Return number of hits */
1213 return __builtin_popcountl(*hit_mask);
1217 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1219 uint32_t bucket_idx, idx, position;
1220 struct rte_hash_key *next_key;
1222 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1224 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1226 if (*next >= total_entries)
1229 /* Calculate bucket and index of current iterator */
1230 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1231 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1233 /* If current position is empty, go to the next one */
1234 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1237 if (*next == total_entries)
1239 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1240 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1243 /* Get position of entry in key table */
1244 position = h->buckets[bucket_idx].key_idx[idx];
1245 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1246 position * h->key_entry_size);
1247 /* Return key and data */
1248 *key = next_key->key;
1249 *data = next_key->pdata;
1251 /* Increment iterator */
1254 return position - 1;