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 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
164 params->socket_id, 0);
166 RTE_LOG(ERR, HASH, "memory allocation failed\n");
170 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
172 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
174 /* guarantee there's no existing: this is normally already checked
175 * by ring creation above */
176 TAILQ_FOREACH(te, hash_list, next) {
177 h = (struct rte_hash *) te->data;
178 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
188 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
190 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
194 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
195 RTE_CACHE_LINE_SIZE, params->socket_id);
198 RTE_LOG(ERR, HASH, "memory allocation failed\n");
202 const uint32_t num_buckets = rte_align32pow2(params->entries)
203 / RTE_HASH_BUCKET_ENTRIES;
205 buckets = rte_zmalloc_socket(NULL,
206 num_buckets * sizeof(struct rte_hash_bucket),
207 RTE_CACHE_LINE_SIZE, params->socket_id);
209 if (buckets == NULL) {
210 RTE_LOG(ERR, HASH, "memory allocation failed\n");
214 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
215 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
217 k = rte_zmalloc_socket(NULL, key_tbl_size,
218 RTE_CACHE_LINE_SIZE, params->socket_id);
221 RTE_LOG(ERR, HASH, "memory allocation failed\n");
226 * If x86 architecture is used, select appropriate compare function,
227 * which may use x86 intrinsics, otherwise use memcmp
229 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
230 /* Select function to compare keys */
231 switch (params->key_len) {
233 h->cmp_jump_table_idx = KEY_16_BYTES;
236 h->cmp_jump_table_idx = KEY_32_BYTES;
239 h->cmp_jump_table_idx = KEY_48_BYTES;
242 h->cmp_jump_table_idx = KEY_64_BYTES;
245 h->cmp_jump_table_idx = KEY_80_BYTES;
248 h->cmp_jump_table_idx = KEY_96_BYTES;
251 h->cmp_jump_table_idx = KEY_112_BYTES;
254 h->cmp_jump_table_idx = KEY_128_BYTES;
257 /* If key is not multiple of 16, use generic memcmp */
258 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
261 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
264 if (hw_trans_mem_support) {
265 h->local_free_slots = rte_zmalloc_socket(NULL,
266 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
267 RTE_CACHE_LINE_SIZE, params->socket_id);
270 /* Setup hash context */
271 snprintf(h->name, sizeof(h->name), "%s", params->name);
272 h->entries = params->entries;
273 h->key_len = params->key_len;
274 h->key_entry_size = key_entry_size;
275 h->hash_func_init_val = params->hash_func_init_val;
277 h->num_buckets = num_buckets;
278 h->bucket_bitmask = h->num_buckets - 1;
279 h->buckets = buckets;
280 h->hash_func = (params->hash_func == NULL) ?
281 DEFAULT_HASH_FUNC : params->hash_func;
284 h->hw_trans_mem_support = hw_trans_mem_support;
286 /* Turn on multi-writer only with explicit flat from user and TM
289 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
290 if (h->hw_trans_mem_support) {
291 h->add_key = ADD_KEY_MULTIWRITER_TM;
293 h->add_key = ADD_KEY_MULTIWRITER;
294 h->multiwriter_lock = rte_malloc(NULL,
295 sizeof(rte_spinlock_t),
297 rte_spinlock_init(h->multiwriter_lock);
300 h->add_key = ADD_KEY_SINGLEWRITER;
302 /* Populate free slots ring. Entry zero is reserved for key misses. */
303 for (i = 1; i < params->entries + 1; i++)
304 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
306 te->data = (void *) h;
307 TAILQ_INSERT_TAIL(hash_list, te, next);
308 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
312 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
323 rte_hash_free(struct rte_hash *h)
325 struct rte_tailq_entry *te;
326 struct rte_hash_list *hash_list;
331 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
333 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
335 /* find out tailq entry */
336 TAILQ_FOREACH(te, hash_list, next) {
337 if (te->data == (void *) h)
342 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
346 TAILQ_REMOVE(hash_list, te, next);
348 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
350 if (h->hw_trans_mem_support)
351 rte_free(h->local_free_slots);
353 if (h->add_key == ADD_KEY_MULTIWRITER)
354 rte_free(h->multiwriter_lock);
355 rte_ring_free(h->free_slots);
356 rte_free(h->key_store);
357 rte_free(h->buckets);
363 rte_hash_hash(const struct rte_hash *h, const void *key)
365 /* calc hash result by key */
366 return h->hash_func(key, h->key_len, h->hash_func_init_val);
369 /* Calc the secondary hash value from the primary hash value of a given key */
370 static inline hash_sig_t
371 rte_hash_secondary_hash(const hash_sig_t primary_hash)
373 static const unsigned all_bits_shift = 12;
374 static const unsigned alt_bits_xor = 0x5bd1e995;
376 uint32_t tag = primary_hash >> all_bits_shift;
378 return primary_hash ^ ((tag + 1) * alt_bits_xor);
382 rte_hash_reset(struct rte_hash *h)
390 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
391 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
393 /* clear the free ring */
394 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
397 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
398 for (i = 1; i < h->entries + 1; i++)
399 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
401 if (h->hw_trans_mem_support) {
402 /* Reset local caches per lcore */
403 for (i = 0; i < RTE_MAX_LCORE; i++)
404 h->local_free_slots[i].len = 0;
408 /* Search for an entry that can be pushed to its alternative location */
410 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
414 uint32_t next_bucket_idx;
415 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
418 * Push existing item (search for bucket with space in
419 * alternative locations) to its alternative location
421 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
422 /* Search for space in alternative locations */
423 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
424 next_bkt[i] = &h->buckets[next_bucket_idx];
425 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
426 if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
430 if (j != RTE_HASH_BUCKET_ENTRIES)
434 /* Alternative location has spare room (end of recursive function) */
435 if (i != RTE_HASH_BUCKET_ENTRIES) {
436 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
437 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
438 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
442 /* Pick entry that has not been pushed yet */
443 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
444 if (bkt->flag[i] == 0)
447 /* All entries have been pushed, so entry cannot be added */
448 if (i == RTE_HASH_BUCKET_ENTRIES)
451 /* Set flag to indicate that this entry is going to be pushed */
453 /* Need room in alternative bucket to insert the pushed entry */
454 ret = make_space_bucket(h, next_bkt[i]);
456 * After recursive function.
457 * Clear flags and insert the pushed entry
458 * in its alternative location if successful,
463 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
464 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
465 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
473 * Function called to enqueue back an index in the cache/ring,
474 * as slot has not being used and it can be used in the
475 * next addition attempt.
478 enqueue_slot_back(const struct rte_hash *h,
479 struct lcore_cache *cached_free_slots,
482 if (h->hw_trans_mem_support) {
483 cached_free_slots->objs[cached_free_slots->len] = slot_id;
484 cached_free_slots->len++;
486 rte_ring_sp_enqueue(h->free_slots, slot_id);
489 static inline int32_t
490 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
491 hash_sig_t sig, void *data)
494 uint32_t prim_bucket_idx, sec_bucket_idx;
496 struct rte_hash_bucket *prim_bkt, *sec_bkt;
497 struct rte_hash_key *new_k, *k, *keys = h->key_store;
498 void *slot_id = NULL;
503 struct lcore_cache *cached_free_slots = NULL;
505 if (h->add_key == ADD_KEY_MULTIWRITER)
506 rte_spinlock_lock(h->multiwriter_lock);
508 prim_bucket_idx = sig & h->bucket_bitmask;
509 prim_bkt = &h->buckets[prim_bucket_idx];
510 rte_prefetch0(prim_bkt);
512 alt_hash = rte_hash_secondary_hash(sig);
513 sec_bucket_idx = alt_hash & h->bucket_bitmask;
514 sec_bkt = &h->buckets[sec_bucket_idx];
515 rte_prefetch0(sec_bkt);
517 /* Get a new slot for storing the new key */
518 if (h->hw_trans_mem_support) {
519 lcore_id = rte_lcore_id();
520 cached_free_slots = &h->local_free_slots[lcore_id];
521 /* Try to get a free slot from the local cache */
522 if (cached_free_slots->len == 0) {
523 /* Need to get another burst of free slots from global ring */
524 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
525 cached_free_slots->objs, LCORE_CACHE_SIZE);
529 cached_free_slots->len += n_slots;
532 /* Get a free slot from the local cache */
533 cached_free_slots->len--;
534 slot_id = cached_free_slots->objs[cached_free_slots->len];
536 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
540 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
541 rte_prefetch0(new_k);
542 new_idx = (uint32_t)((uintptr_t) slot_id);
544 /* Check if key is already inserted in primary location */
545 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
546 if (prim_bkt->signatures[i].current == sig &&
547 prim_bkt->signatures[i].alt == alt_hash) {
548 k = (struct rte_hash_key *) ((char *)keys +
549 prim_bkt->key_idx[i] * h->key_entry_size);
550 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
551 /* Enqueue index of free slot back in the ring. */
552 enqueue_slot_back(h, cached_free_slots, slot_id);
556 * Return index where key is stored,
557 * substracting the first dummy index
559 return prim_bkt->key_idx[i] - 1;
564 /* Check if key is already inserted in secondary location */
565 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
566 if (sec_bkt->signatures[i].alt == sig &&
567 sec_bkt->signatures[i].current == alt_hash) {
568 k = (struct rte_hash_key *) ((char *)keys +
569 sec_bkt->key_idx[i] * h->key_entry_size);
570 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
571 /* Enqueue index of free slot back in the ring. */
572 enqueue_slot_back(h, cached_free_slots, slot_id);
576 * Return index where key is stored,
577 * substracting the first dummy index
579 return sec_bkt->key_idx[i] - 1;
585 rte_memcpy(new_k->key, key, h->key_len);
588 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
589 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
590 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
591 sig, alt_hash, new_idx);
595 /* Primary bucket full, need to make space for new entry */
596 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
602 /* Also search secondary bucket to get better occupancy */
603 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
610 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
611 /* Check if slot is available */
612 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
613 prim_bkt->signatures[i].current = sig;
614 prim_bkt->signatures[i].alt = alt_hash;
615 prim_bkt->key_idx[i] = new_idx;
620 if (i != RTE_HASH_BUCKET_ENTRIES) {
621 if (h->add_key == ADD_KEY_MULTIWRITER)
622 rte_spinlock_unlock(h->multiwriter_lock);
626 /* Primary bucket full, need to make space for new entry
627 * After recursive function.
628 * Insert the new entry in the position of the pushed entry
629 * if successful or return error and
630 * store the new slot back in the ring
632 ret = make_space_bucket(h, prim_bkt);
634 prim_bkt->signatures[ret].current = sig;
635 prim_bkt->signatures[ret].alt = alt_hash;
636 prim_bkt->key_idx[ret] = new_idx;
637 if (h->add_key == ADD_KEY_MULTIWRITER)
638 rte_spinlock_unlock(h->multiwriter_lock);
641 #if defined(RTE_ARCH_X86)
644 /* Error in addition, store new slot back in the ring and return error */
645 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
647 if (h->add_key == ADD_KEY_MULTIWRITER)
648 rte_spinlock_unlock(h->multiwriter_lock);
653 rte_hash_add_key_with_hash(const struct rte_hash *h,
654 const void *key, hash_sig_t sig)
656 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
657 return __rte_hash_add_key_with_hash(h, key, sig, 0);
661 rte_hash_add_key(const struct rte_hash *h, const void *key)
663 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
664 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
668 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
669 const void *key, hash_sig_t sig, void *data)
673 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
674 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
682 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
686 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
688 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
694 static inline int32_t
695 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
696 hash_sig_t sig, void **data)
701 struct rte_hash_bucket *bkt;
702 struct rte_hash_key *k, *keys = h->key_store;
704 bucket_idx = sig & h->bucket_bitmask;
705 bkt = &h->buckets[bucket_idx];
707 /* Check if key is in primary location */
708 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
709 if (bkt->signatures[i].current == sig &&
710 bkt->signatures[i].sig != NULL_SIGNATURE) {
711 k = (struct rte_hash_key *) ((char *)keys +
712 bkt->key_idx[i] * h->key_entry_size);
713 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
717 * Return index where key is stored,
718 * substracting the first dummy index
720 return bkt->key_idx[i] - 1;
725 /* Calculate secondary hash */
726 alt_hash = rte_hash_secondary_hash(sig);
727 bucket_idx = alt_hash & h->bucket_bitmask;
728 bkt = &h->buckets[bucket_idx];
730 /* Check if key is in secondary location */
731 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
732 if (bkt->signatures[i].current == alt_hash &&
733 bkt->signatures[i].alt == sig) {
734 k = (struct rte_hash_key *) ((char *)keys +
735 bkt->key_idx[i] * h->key_entry_size);
736 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
740 * Return index where key is stored,
741 * substracting the first dummy index
743 return bkt->key_idx[i] - 1;
752 rte_hash_lookup_with_hash(const struct rte_hash *h,
753 const void *key, hash_sig_t sig)
755 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
756 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
760 rte_hash_lookup(const struct rte_hash *h, const void *key)
762 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
763 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
767 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
768 const void *key, hash_sig_t sig, void **data)
770 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
771 return __rte_hash_lookup_with_hash(h, key, sig, data);
775 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
777 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
778 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
782 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
784 unsigned lcore_id, n_slots;
785 struct lcore_cache *cached_free_slots;
787 bkt->signatures[i].sig = NULL_SIGNATURE;
788 if (h->hw_trans_mem_support) {
789 lcore_id = rte_lcore_id();
790 cached_free_slots = &h->local_free_slots[lcore_id];
791 /* Cache full, need to free it. */
792 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
793 /* Need to enqueue the free slots in global ring. */
794 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
795 cached_free_slots->objs,
797 cached_free_slots->len -= n_slots;
799 /* Put index of new free slot in cache. */
800 cached_free_slots->objs[cached_free_slots->len] =
801 (void *)((uintptr_t)bkt->key_idx[i]);
802 cached_free_slots->len++;
804 rte_ring_sp_enqueue(h->free_slots,
805 (void *)((uintptr_t)bkt->key_idx[i]));
809 static inline int32_t
810 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
816 struct rte_hash_bucket *bkt;
817 struct rte_hash_key *k, *keys = h->key_store;
819 bucket_idx = sig & h->bucket_bitmask;
820 bkt = &h->buckets[bucket_idx];
822 /* Check if key is in primary location */
823 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
824 if (bkt->signatures[i].current == sig &&
825 bkt->signatures[i].sig != NULL_SIGNATURE) {
826 k = (struct rte_hash_key *) ((char *)keys +
827 bkt->key_idx[i] * h->key_entry_size);
828 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
829 remove_entry(h, bkt, i);
832 * Return index where key is stored,
833 * substracting the first dummy index
835 return bkt->key_idx[i] - 1;
840 /* Calculate secondary hash */
841 alt_hash = rte_hash_secondary_hash(sig);
842 bucket_idx = alt_hash & h->bucket_bitmask;
843 bkt = &h->buckets[bucket_idx];
845 /* Check if key is in secondary location */
846 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
847 if (bkt->signatures[i].current == alt_hash &&
848 bkt->signatures[i].sig != NULL_SIGNATURE) {
849 k = (struct rte_hash_key *) ((char *)keys +
850 bkt->key_idx[i] * h->key_entry_size);
851 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
852 remove_entry(h, bkt, i);
855 * Return index where key is stored,
856 * substracting the first dummy index
858 return bkt->key_idx[i] - 1;
867 rte_hash_del_key_with_hash(const struct rte_hash *h,
868 const void *key, hash_sig_t sig)
870 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
871 return __rte_hash_del_key_with_hash(h, key, sig);
875 rte_hash_del_key(const struct rte_hash *h, const void *key)
877 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
878 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
882 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
885 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
887 struct rte_hash_key *k, *keys = h->key_store;
888 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
893 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
901 /* Lookup bulk stage 0: Prefetch input key */
903 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
904 const void * const *keys)
906 *idx = __builtin_ctzl(*lookup_mask);
907 if (*lookup_mask == 0)
910 rte_prefetch0(keys[*idx]);
911 *lookup_mask &= ~(1llu << *idx);
915 * Lookup bulk stage 1: Calculate primary/secondary hashes
916 * and prefetch primary/secondary buckets
919 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
920 const struct rte_hash_bucket **primary_bkt,
921 const struct rte_hash_bucket **secondary_bkt,
922 hash_sig_t *hash_vals, const void * const *keys,
923 const struct rte_hash *h)
925 *prim_hash = rte_hash_hash(h, keys[idx]);
926 hash_vals[idx] = *prim_hash;
927 *sec_hash = rte_hash_secondary_hash(*prim_hash);
929 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
930 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
932 rte_prefetch0(*primary_bkt);
933 rte_prefetch0(*secondary_bkt);
937 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
938 * and prefetch first key slot
941 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
942 const struct rte_hash_bucket *prim_bkt,
943 const struct rte_hash_bucket *sec_bkt,
944 const struct rte_hash_key **key_slot, int32_t *positions,
945 uint64_t *extra_hits_mask, const void *keys,
946 const struct rte_hash *h)
948 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
949 unsigned total_hash_matches;
951 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
952 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
953 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
954 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
955 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
958 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
960 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
962 total_hash_matches = (prim_hash_matches |
963 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
964 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
965 key_idx * h->key_entry_size);
967 rte_prefetch0(*key_slot);
969 * Return index where key is stored,
970 * substracting the first dummy index
972 positions[idx] = (key_idx - 1);
974 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
979 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
981 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
982 const int32_t *positions, void *data[], uint64_t *hits,
983 const struct rte_hash *h)
988 hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
990 data[idx] = key_slot->pdata;
992 key_idx = positions[idx] + 1;
994 * If key index is 0, force hit to be 0, in case key to be looked up
995 * is all zero (as in the dummy slot), which would result in a wrong hit
997 *hits |= (uint64_t)(hit && !!key_idx) << idx;
1001 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1002 uint32_t num_keys, int32_t *positions,
1003 uint64_t *hit_mask, void *data[])
1006 uint64_t extra_hits_mask = 0;
1007 uint64_t lookup_mask, miss_mask;
1009 const void *key_store = h->key_store;
1011 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1013 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1014 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1015 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1016 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1017 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1018 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1019 hash_sig_t primary_hash10, primary_hash11;
1020 hash_sig_t secondary_hash10, secondary_hash11;
1021 hash_sig_t primary_hash20, primary_hash21;
1022 hash_sig_t secondary_hash20, secondary_hash21;
1024 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1025 miss_mask = lookup_mask;
1027 lookup_stage0(&idx00, &lookup_mask, keys);
1028 lookup_stage0(&idx01, &lookup_mask, keys);
1030 idx10 = idx00, idx11 = idx01;
1032 lookup_stage0(&idx00, &lookup_mask, keys);
1033 lookup_stage0(&idx01, &lookup_mask, keys);
1034 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1035 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1036 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1037 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1039 primary_bkt20 = primary_bkt10;
1040 primary_bkt21 = primary_bkt11;
1041 secondary_bkt20 = secondary_bkt10;
1042 secondary_bkt21 = secondary_bkt11;
1043 primary_hash20 = primary_hash10;
1044 primary_hash21 = primary_hash11;
1045 secondary_hash20 = secondary_hash10;
1046 secondary_hash21 = secondary_hash11;
1047 idx20 = idx10, idx21 = idx11;
1048 idx10 = idx00, idx11 = idx01;
1050 lookup_stage0(&idx00, &lookup_mask, keys);
1051 lookup_stage0(&idx01, &lookup_mask, keys);
1052 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1053 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1054 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1055 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1056 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1057 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1059 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1060 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1063 while (lookup_mask) {
1064 k_slot30 = k_slot20, k_slot31 = k_slot21;
1065 idx30 = idx20, idx31 = idx21;
1066 primary_bkt20 = primary_bkt10;
1067 primary_bkt21 = primary_bkt11;
1068 secondary_bkt20 = secondary_bkt10;
1069 secondary_bkt21 = secondary_bkt11;
1070 primary_hash20 = primary_hash10;
1071 primary_hash21 = primary_hash11;
1072 secondary_hash20 = secondary_hash10;
1073 secondary_hash21 = secondary_hash11;
1074 idx20 = idx10, idx21 = idx11;
1075 idx10 = idx00, idx11 = idx01;
1077 lookup_stage0(&idx00, &lookup_mask, keys);
1078 lookup_stage0(&idx01, &lookup_mask, keys);
1079 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1080 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1081 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1082 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1083 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1084 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1085 &extra_hits_mask, key_store, h);
1086 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1087 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1088 &extra_hits_mask, key_store, h);
1089 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1090 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1093 k_slot30 = k_slot20, k_slot31 = k_slot21;
1094 idx30 = idx20, idx31 = idx21;
1095 primary_bkt20 = primary_bkt10;
1096 primary_bkt21 = primary_bkt11;
1097 secondary_bkt20 = secondary_bkt10;
1098 secondary_bkt21 = secondary_bkt11;
1099 primary_hash20 = primary_hash10;
1100 primary_hash21 = primary_hash11;
1101 secondary_hash20 = secondary_hash10;
1102 secondary_hash21 = secondary_hash11;
1103 idx20 = idx10, idx21 = idx11;
1104 idx10 = idx00, idx11 = idx01;
1106 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1107 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1108 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1109 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1110 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1111 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1113 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1114 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1116 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1117 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1119 k_slot30 = k_slot20, k_slot31 = k_slot21;
1120 idx30 = idx20, idx31 = idx21;
1121 primary_bkt20 = primary_bkt10;
1122 primary_bkt21 = primary_bkt11;
1123 secondary_bkt20 = secondary_bkt10;
1124 secondary_bkt21 = secondary_bkt11;
1125 primary_hash20 = primary_hash10;
1126 primary_hash21 = primary_hash11;
1127 secondary_hash20 = secondary_hash10;
1128 secondary_hash21 = secondary_hash11;
1129 idx20 = idx10, idx21 = idx11;
1131 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1132 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1134 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1135 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1137 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1138 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1140 k_slot30 = k_slot20, k_slot31 = k_slot21;
1141 idx30 = idx20, idx31 = idx21;
1143 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1144 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1146 /* ignore any items we have already found */
1147 extra_hits_mask &= ~hits;
1149 if (unlikely(extra_hits_mask)) {
1150 /* run a single search for each remaining item */
1152 idx = __builtin_ctzl(extra_hits_mask);
1154 ret = rte_hash_lookup_with_hash_data(h,
1155 keys[idx], hash_vals[idx], &data[idx]);
1157 hits |= 1ULL << idx;
1159 positions[idx] = rte_hash_lookup_with_hash(h,
1160 keys[idx], hash_vals[idx]);
1161 if (positions[idx] >= 0)
1162 hits |= 1llu << idx;
1164 extra_hits_mask &= ~(1llu << idx);
1165 } while (extra_hits_mask);
1169 if (unlikely(miss_mask)) {
1171 idx = __builtin_ctzl(miss_mask);
1172 positions[idx] = -ENOENT;
1173 miss_mask &= ~(1llu << idx);
1174 } while (miss_mask);
1177 if (hit_mask != NULL)
1182 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1183 uint32_t num_keys, int32_t *positions)
1185 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1186 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1187 (positions == NULL)), -EINVAL);
1189 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1194 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1195 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1197 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1198 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1199 (hit_mask == NULL)), -EINVAL);
1201 int32_t positions[num_keys];
1203 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1205 /* Return number of hits */
1206 return __builtin_popcountl(*hit_mask);
1210 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1212 uint32_t bucket_idx, idx, position;
1213 struct rte_hash_key *next_key;
1215 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1217 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1219 if (*next >= total_entries)
1222 /* Calculate bucket and index of current iterator */
1223 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1224 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1226 /* If current position is empty, go to the next one */
1227 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1230 if (*next == total_entries)
1232 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1233 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1236 /* Get position of entry in key table */
1237 position = h->buckets[bucket_idx].key_idx[idx];
1238 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1239 position * h->key_entry_size);
1240 /* Return key and data */
1241 *key = next_key->key;
1242 *data = next_key->pdata;
1244 /* Increment iterator */
1247 return position - 1;