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 /* Create ring (Dummy slot index is not enqueued) */
163 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots - 1),
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)
412 static unsigned int nr_pushes;
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->signatures[i].alt & 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]->signatures[j].sig == NULL_SIGNATURE)
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]->signatures[j].alt = bkt->signatures[i].current;
438 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
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 || nr_pushes > RTE_HASH_MAX_PUSHES)
452 /* Set flag to indicate that this entry is going to be pushed */
456 /* Need room in alternative bucket to insert the pushed entry */
457 ret = make_space_bucket(h, next_bkt[i]);
459 * After recursive function.
460 * Clear flags and insert the pushed entry
461 * in its alternative location if successful,
467 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
468 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
469 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
477 * Function called to enqueue back an index in the cache/ring,
478 * as slot has not being used and it can be used in the
479 * next addition attempt.
482 enqueue_slot_back(const struct rte_hash *h,
483 struct lcore_cache *cached_free_slots,
486 if (h->hw_trans_mem_support) {
487 cached_free_slots->objs[cached_free_slots->len] = slot_id;
488 cached_free_slots->len++;
490 rte_ring_sp_enqueue(h->free_slots, slot_id);
493 static inline int32_t
494 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
495 hash_sig_t sig, void *data)
498 uint32_t prim_bucket_idx, sec_bucket_idx;
500 struct rte_hash_bucket *prim_bkt, *sec_bkt;
501 struct rte_hash_key *new_k, *k, *keys = h->key_store;
502 void *slot_id = NULL;
507 struct lcore_cache *cached_free_slots = NULL;
509 if (h->add_key == ADD_KEY_MULTIWRITER)
510 rte_spinlock_lock(h->multiwriter_lock);
512 prim_bucket_idx = sig & h->bucket_bitmask;
513 prim_bkt = &h->buckets[prim_bucket_idx];
514 rte_prefetch0(prim_bkt);
516 alt_hash = rte_hash_secondary_hash(sig);
517 sec_bucket_idx = alt_hash & h->bucket_bitmask;
518 sec_bkt = &h->buckets[sec_bucket_idx];
519 rte_prefetch0(sec_bkt);
521 /* Get a new slot for storing the new key */
522 if (h->hw_trans_mem_support) {
523 lcore_id = rte_lcore_id();
524 cached_free_slots = &h->local_free_slots[lcore_id];
525 /* Try to get a free slot from the local cache */
526 if (cached_free_slots->len == 0) {
527 /* Need to get another burst of free slots from global ring */
528 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
529 cached_free_slots->objs, LCORE_CACHE_SIZE);
533 cached_free_slots->len += n_slots;
536 /* Get a free slot from the local cache */
537 cached_free_slots->len--;
538 slot_id = cached_free_slots->objs[cached_free_slots->len];
540 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
544 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
545 rte_prefetch0(new_k);
546 new_idx = (uint32_t)((uintptr_t) slot_id);
548 /* Check if key is already inserted in primary location */
549 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
550 if (prim_bkt->signatures[i].current == sig &&
551 prim_bkt->signatures[i].alt == alt_hash) {
552 k = (struct rte_hash_key *) ((char *)keys +
553 prim_bkt->key_idx[i] * h->key_entry_size);
554 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
555 /* Enqueue index of free slot back in the ring. */
556 enqueue_slot_back(h, cached_free_slots, slot_id);
560 * Return index where key is stored,
561 * substracting the first dummy index
563 return prim_bkt->key_idx[i] - 1;
568 /* Check if key is already inserted in secondary location */
569 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
570 if (sec_bkt->signatures[i].alt == sig &&
571 sec_bkt->signatures[i].current == alt_hash) {
572 k = (struct rte_hash_key *) ((char *)keys +
573 sec_bkt->key_idx[i] * h->key_entry_size);
574 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
575 /* Enqueue index of free slot back in the ring. */
576 enqueue_slot_back(h, cached_free_slots, slot_id);
580 * Return index where key is stored,
581 * substracting the first dummy index
583 return sec_bkt->key_idx[i] - 1;
589 rte_memcpy(new_k->key, key, h->key_len);
592 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
593 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
594 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
595 sig, alt_hash, new_idx);
599 /* Primary bucket full, need to make space for new entry */
600 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
606 /* Also search secondary bucket to get better occupancy */
607 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
614 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
615 /* Check if slot is available */
616 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
617 prim_bkt->signatures[i].current = sig;
618 prim_bkt->signatures[i].alt = alt_hash;
619 prim_bkt->key_idx[i] = new_idx;
624 if (i != RTE_HASH_BUCKET_ENTRIES) {
625 if (h->add_key == ADD_KEY_MULTIWRITER)
626 rte_spinlock_unlock(h->multiwriter_lock);
630 /* Primary bucket full, need to make space for new entry
631 * After recursive function.
632 * Insert the new entry in the position of the pushed entry
633 * if successful or return error and
634 * store the new slot back in the ring
636 ret = make_space_bucket(h, prim_bkt);
638 prim_bkt->signatures[ret].current = sig;
639 prim_bkt->signatures[ret].alt = alt_hash;
640 prim_bkt->key_idx[ret] = new_idx;
641 if (h->add_key == ADD_KEY_MULTIWRITER)
642 rte_spinlock_unlock(h->multiwriter_lock);
645 #if defined(RTE_ARCH_X86)
648 /* Error in addition, store new slot back in the ring and return error */
649 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
651 if (h->add_key == ADD_KEY_MULTIWRITER)
652 rte_spinlock_unlock(h->multiwriter_lock);
657 rte_hash_add_key_with_hash(const struct rte_hash *h,
658 const void *key, hash_sig_t sig)
660 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
661 return __rte_hash_add_key_with_hash(h, key, sig, 0);
665 rte_hash_add_key(const struct rte_hash *h, const void *key)
667 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
668 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
672 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
673 const void *key, hash_sig_t sig, void *data)
677 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
678 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
686 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
690 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
692 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
698 static inline int32_t
699 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
700 hash_sig_t sig, void **data)
705 struct rte_hash_bucket *bkt;
706 struct rte_hash_key *k, *keys = h->key_store;
708 bucket_idx = sig & h->bucket_bitmask;
709 bkt = &h->buckets[bucket_idx];
711 /* Check if key is in primary location */
712 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
713 if (bkt->signatures[i].current == sig &&
714 bkt->signatures[i].sig != NULL_SIGNATURE) {
715 k = (struct rte_hash_key *) ((char *)keys +
716 bkt->key_idx[i] * h->key_entry_size);
717 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
721 * Return index where key is stored,
722 * substracting the first dummy index
724 return bkt->key_idx[i] - 1;
729 /* Calculate secondary hash */
730 alt_hash = rte_hash_secondary_hash(sig);
731 bucket_idx = alt_hash & h->bucket_bitmask;
732 bkt = &h->buckets[bucket_idx];
734 /* Check if key is in secondary location */
735 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
736 if (bkt->signatures[i].current == alt_hash &&
737 bkt->signatures[i].alt == sig) {
738 k = (struct rte_hash_key *) ((char *)keys +
739 bkt->key_idx[i] * h->key_entry_size);
740 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
744 * Return index where key is stored,
745 * substracting the first dummy index
747 return bkt->key_idx[i] - 1;
756 rte_hash_lookup_with_hash(const struct rte_hash *h,
757 const void *key, hash_sig_t sig)
759 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
760 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
764 rte_hash_lookup(const struct rte_hash *h, const void *key)
766 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
767 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
771 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
772 const void *key, hash_sig_t sig, void **data)
774 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
775 return __rte_hash_lookup_with_hash(h, key, sig, data);
779 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
781 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
782 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
786 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
788 unsigned lcore_id, n_slots;
789 struct lcore_cache *cached_free_slots;
791 bkt->signatures[i].sig = NULL_SIGNATURE;
792 if (h->hw_trans_mem_support) {
793 lcore_id = rte_lcore_id();
794 cached_free_slots = &h->local_free_slots[lcore_id];
795 /* Cache full, need to free it. */
796 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
797 /* Need to enqueue the free slots in global ring. */
798 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
799 cached_free_slots->objs,
801 cached_free_slots->len -= n_slots;
803 /* Put index of new free slot in cache. */
804 cached_free_slots->objs[cached_free_slots->len] =
805 (void *)((uintptr_t)bkt->key_idx[i]);
806 cached_free_slots->len++;
808 rte_ring_sp_enqueue(h->free_slots,
809 (void *)((uintptr_t)bkt->key_idx[i]));
813 static inline int32_t
814 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
820 struct rte_hash_bucket *bkt;
821 struct rte_hash_key *k, *keys = h->key_store;
824 bucket_idx = sig & h->bucket_bitmask;
825 bkt = &h->buckets[bucket_idx];
827 /* Check if key is in primary location */
828 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
829 if (bkt->signatures[i].current == sig &&
830 bkt->signatures[i].sig != NULL_SIGNATURE) {
831 k = (struct rte_hash_key *) ((char *)keys +
832 bkt->key_idx[i] * h->key_entry_size);
833 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
834 remove_entry(h, bkt, i);
837 * Return index where key is stored,
838 * substracting the first dummy index
840 ret = bkt->key_idx[i] - 1;
847 /* Calculate secondary hash */
848 alt_hash = rte_hash_secondary_hash(sig);
849 bucket_idx = alt_hash & h->bucket_bitmask;
850 bkt = &h->buckets[bucket_idx];
852 /* Check if key is in secondary location */
853 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
854 if (bkt->signatures[i].current == alt_hash &&
855 bkt->signatures[i].sig != NULL_SIGNATURE) {
856 k = (struct rte_hash_key *) ((char *)keys +
857 bkt->key_idx[i] * h->key_entry_size);
858 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
859 remove_entry(h, bkt, i);
862 * Return index where key is stored,
863 * substracting the first dummy index
865 ret = bkt->key_idx[i] - 1;
876 rte_hash_del_key_with_hash(const struct rte_hash *h,
877 const void *key, hash_sig_t sig)
879 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
880 return __rte_hash_del_key_with_hash(h, key, sig);
884 rte_hash_del_key(const struct rte_hash *h, const void *key)
886 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
887 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
891 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
894 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
896 struct rte_hash_key *k, *keys = h->key_store;
897 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
902 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
910 /* Lookup bulk stage 0: Prefetch input key */
912 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
913 const void * const *keys)
915 *idx = __builtin_ctzl(*lookup_mask);
916 if (*lookup_mask == 0)
919 rte_prefetch0(keys[*idx]);
920 *lookup_mask &= ~(1llu << *idx);
924 * Lookup bulk stage 1: Calculate primary/secondary hashes
925 * and prefetch primary/secondary buckets
928 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
929 const struct rte_hash_bucket **primary_bkt,
930 const struct rte_hash_bucket **secondary_bkt,
931 hash_sig_t *hash_vals, const void * const *keys,
932 const struct rte_hash *h)
934 *prim_hash = rte_hash_hash(h, keys[idx]);
935 hash_vals[idx] = *prim_hash;
936 *sec_hash = rte_hash_secondary_hash(*prim_hash);
938 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
939 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
941 rte_prefetch0(*primary_bkt);
942 rte_prefetch0(*secondary_bkt);
946 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
947 * and prefetch first key slot
950 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
951 const struct rte_hash_bucket *prim_bkt,
952 const struct rte_hash_bucket *sec_bkt,
953 const struct rte_hash_key **key_slot, int32_t *positions,
954 uint64_t *extra_hits_mask, const void *keys,
955 const struct rte_hash *h)
957 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
958 unsigned total_hash_matches;
960 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
961 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
962 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
963 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
964 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
967 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
969 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
971 total_hash_matches = (prim_hash_matches |
972 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
973 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
974 key_idx * h->key_entry_size);
976 rte_prefetch0(*key_slot);
978 * Return index where key is stored,
979 * substracting the first dummy index
981 positions[idx] = (key_idx - 1);
983 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
988 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
990 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
991 const int32_t *positions, void *data[], uint64_t *hits,
992 const struct rte_hash *h)
997 hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
999 data[idx] = key_slot->pdata;
1001 key_idx = positions[idx] + 1;
1003 * If key index is 0, force hit to be 0, in case key to be looked up
1004 * is all zero (as in the dummy slot), which would result in a wrong hit
1006 *hits |= (uint64_t)(hit && !!key_idx) << idx;
1010 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1011 uint32_t num_keys, int32_t *positions,
1012 uint64_t *hit_mask, void *data[])
1015 uint64_t extra_hits_mask = 0;
1016 uint64_t lookup_mask, miss_mask;
1018 const void *key_store = h->key_store;
1020 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1022 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1023 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1024 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1025 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1026 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1027 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1028 hash_sig_t primary_hash10, primary_hash11;
1029 hash_sig_t secondary_hash10, secondary_hash11;
1030 hash_sig_t primary_hash20, primary_hash21;
1031 hash_sig_t secondary_hash20, secondary_hash21;
1033 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1034 miss_mask = lookup_mask;
1036 lookup_stage0(&idx00, &lookup_mask, keys);
1037 lookup_stage0(&idx01, &lookup_mask, keys);
1039 idx10 = idx00, idx11 = idx01;
1041 lookup_stage0(&idx00, &lookup_mask, keys);
1042 lookup_stage0(&idx01, &lookup_mask, keys);
1043 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1044 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1045 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1046 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1048 primary_bkt20 = primary_bkt10;
1049 primary_bkt21 = primary_bkt11;
1050 secondary_bkt20 = secondary_bkt10;
1051 secondary_bkt21 = secondary_bkt11;
1052 primary_hash20 = primary_hash10;
1053 primary_hash21 = primary_hash11;
1054 secondary_hash20 = secondary_hash10;
1055 secondary_hash21 = secondary_hash11;
1056 idx20 = idx10, idx21 = idx11;
1057 idx10 = idx00, idx11 = idx01;
1059 lookup_stage0(&idx00, &lookup_mask, keys);
1060 lookup_stage0(&idx01, &lookup_mask, keys);
1061 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1062 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1063 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1064 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1065 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1066 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1068 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1069 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1072 while (lookup_mask) {
1073 k_slot30 = k_slot20, k_slot31 = k_slot21;
1074 idx30 = idx20, idx31 = idx21;
1075 primary_bkt20 = primary_bkt10;
1076 primary_bkt21 = primary_bkt11;
1077 secondary_bkt20 = secondary_bkt10;
1078 secondary_bkt21 = secondary_bkt11;
1079 primary_hash20 = primary_hash10;
1080 primary_hash21 = primary_hash11;
1081 secondary_hash20 = secondary_hash10;
1082 secondary_hash21 = secondary_hash11;
1083 idx20 = idx10, idx21 = idx11;
1084 idx10 = idx00, idx11 = idx01;
1086 lookup_stage0(&idx00, &lookup_mask, keys);
1087 lookup_stage0(&idx01, &lookup_mask, keys);
1088 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1089 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1090 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1091 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1092 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1093 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1094 &extra_hits_mask, key_store, h);
1095 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1096 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1097 &extra_hits_mask, key_store, h);
1098 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1099 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1102 k_slot30 = k_slot20, k_slot31 = k_slot21;
1103 idx30 = idx20, idx31 = idx21;
1104 primary_bkt20 = primary_bkt10;
1105 primary_bkt21 = primary_bkt11;
1106 secondary_bkt20 = secondary_bkt10;
1107 secondary_bkt21 = secondary_bkt11;
1108 primary_hash20 = primary_hash10;
1109 primary_hash21 = primary_hash11;
1110 secondary_hash20 = secondary_hash10;
1111 secondary_hash21 = secondary_hash11;
1112 idx20 = idx10, idx21 = idx11;
1113 idx10 = idx00, idx11 = idx01;
1115 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1116 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1117 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1118 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1119 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1120 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1122 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1123 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1125 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1126 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1128 k_slot30 = k_slot20, k_slot31 = k_slot21;
1129 idx30 = idx20, idx31 = idx21;
1130 primary_bkt20 = primary_bkt10;
1131 primary_bkt21 = primary_bkt11;
1132 secondary_bkt20 = secondary_bkt10;
1133 secondary_bkt21 = secondary_bkt11;
1134 primary_hash20 = primary_hash10;
1135 primary_hash21 = primary_hash11;
1136 secondary_hash20 = secondary_hash10;
1137 secondary_hash21 = secondary_hash11;
1138 idx20 = idx10, idx21 = idx11;
1140 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1141 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1143 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1144 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1146 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1147 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1149 k_slot30 = k_slot20, k_slot31 = k_slot21;
1150 idx30 = idx20, idx31 = idx21;
1152 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1153 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1155 /* ignore any items we have already found */
1156 extra_hits_mask &= ~hits;
1158 if (unlikely(extra_hits_mask)) {
1159 /* run a single search for each remaining item */
1161 idx = __builtin_ctzl(extra_hits_mask);
1163 ret = rte_hash_lookup_with_hash_data(h,
1164 keys[idx], hash_vals[idx], &data[idx]);
1166 hits |= 1ULL << idx;
1168 positions[idx] = rte_hash_lookup_with_hash(h,
1169 keys[idx], hash_vals[idx]);
1170 if (positions[idx] >= 0)
1171 hits |= 1llu << idx;
1173 extra_hits_mask &= ~(1llu << idx);
1174 } while (extra_hits_mask);
1178 if (unlikely(miss_mask)) {
1180 idx = __builtin_ctzl(miss_mask);
1181 positions[idx] = -ENOENT;
1182 miss_mask &= ~(1llu << idx);
1183 } while (miss_mask);
1186 if (hit_mask != NULL)
1191 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1192 uint32_t num_keys, int32_t *positions)
1194 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1195 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1196 (positions == NULL)), -EINVAL);
1198 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1203 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1204 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1206 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1207 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1208 (hit_mask == NULL)), -EINVAL);
1210 int32_t positions[num_keys];
1212 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1214 /* Return number of hits */
1215 return __builtin_popcountl(*hit_mask);
1219 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1221 uint32_t bucket_idx, idx, position;
1222 struct rte_hash_key *next_key;
1224 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1226 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1228 if (*next >= total_entries)
1231 /* Calculate bucket and index of current iterator */
1232 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1233 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1235 /* If current position is empty, go to the next one */
1236 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1239 if (*next == total_entries)
1241 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1242 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1245 /* Get position of entry in key table */
1246 position = h->buckets[bucket_idx].key_idx[idx];
1247 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1248 position * h->key_entry_size);
1249 /* Return key and data */
1250 *key = next_key->key;
1251 *data = next_key->pdata;
1253 /* Increment iterator */
1256 return position - 1;