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->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)
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]->signatures[ret].alt = bkt->signatures[i].current;
465 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
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->signatures[i].current == sig &&
548 prim_bkt->signatures[i].alt == 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->signatures[i].alt == sig &&
568 sec_bkt->signatures[i].current == 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->signatures[i].sig == NULL_SIGNATURE)) {
614 prim_bkt->signatures[i].current = sig;
615 prim_bkt->signatures[i].alt = 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->signatures[ret].current = sig;
636 prim_bkt->signatures[ret].alt = 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->signatures[i].current == sig &&
711 bkt->signatures[i].sig != NULL_SIGNATURE) {
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->signatures[i].current == alt_hash &&
734 bkt->signatures[i].alt == 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->signatures[i].sig = NULL_SIGNATURE;
789 if (h->hw_trans_mem_support) {
790 lcore_id = rte_lcore_id();
791 cached_free_slots = &h->local_free_slots[lcore_id];
792 /* Cache full, need to free it. */
793 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
794 /* Need to enqueue the free slots in global ring. */
795 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
796 cached_free_slots->objs,
798 cached_free_slots->len -= n_slots;
800 /* Put index of new free slot in cache. */
801 cached_free_slots->objs[cached_free_slots->len] =
802 (void *)((uintptr_t)bkt->key_idx[i]);
803 cached_free_slots->len++;
805 rte_ring_sp_enqueue(h->free_slots,
806 (void *)((uintptr_t)bkt->key_idx[i]));
810 static inline int32_t
811 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
817 struct rte_hash_bucket *bkt;
818 struct rte_hash_key *k, *keys = h->key_store;
820 bucket_idx = sig & h->bucket_bitmask;
821 bkt = &h->buckets[bucket_idx];
823 /* Check if key is in primary location */
824 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
825 if (bkt->signatures[i].current == sig &&
826 bkt->signatures[i].sig != NULL_SIGNATURE) {
827 k = (struct rte_hash_key *) ((char *)keys +
828 bkt->key_idx[i] * h->key_entry_size);
829 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
830 remove_entry(h, bkt, i);
833 * Return index where key is stored,
834 * substracting the first dummy index
836 return bkt->key_idx[i] - 1;
841 /* Calculate secondary hash */
842 alt_hash = rte_hash_secondary_hash(sig);
843 bucket_idx = alt_hash & h->bucket_bitmask;
844 bkt = &h->buckets[bucket_idx];
846 /* Check if key is in secondary location */
847 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
848 if (bkt->signatures[i].current == alt_hash &&
849 bkt->signatures[i].sig != NULL_SIGNATURE) {
850 k = (struct rte_hash_key *) ((char *)keys +
851 bkt->key_idx[i] * h->key_entry_size);
852 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
853 remove_entry(h, bkt, i);
856 * Return index where key is stored,
857 * substracting the first dummy index
859 return bkt->key_idx[i] - 1;
868 rte_hash_del_key_with_hash(const struct rte_hash *h,
869 const void *key, hash_sig_t sig)
871 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
872 return __rte_hash_del_key_with_hash(h, key, sig);
876 rte_hash_del_key(const struct rte_hash *h, const void *key)
878 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
879 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
883 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
886 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
888 struct rte_hash_key *k, *keys = h->key_store;
889 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
894 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
902 /* Lookup bulk stage 0: Prefetch input key */
904 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
905 const void * const *keys)
907 *idx = __builtin_ctzl(*lookup_mask);
908 if (*lookup_mask == 0)
911 rte_prefetch0(keys[*idx]);
912 *lookup_mask &= ~(1llu << *idx);
916 * Lookup bulk stage 1: Calculate primary/secondary hashes
917 * and prefetch primary/secondary buckets
920 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
921 const struct rte_hash_bucket **primary_bkt,
922 const struct rte_hash_bucket **secondary_bkt,
923 hash_sig_t *hash_vals, const void * const *keys,
924 const struct rte_hash *h)
926 *prim_hash = rte_hash_hash(h, keys[idx]);
927 hash_vals[idx] = *prim_hash;
928 *sec_hash = rte_hash_secondary_hash(*prim_hash);
930 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
931 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
933 rte_prefetch0(*primary_bkt);
934 rte_prefetch0(*secondary_bkt);
938 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
939 * and prefetch first key slot
942 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
943 const struct rte_hash_bucket *prim_bkt,
944 const struct rte_hash_bucket *sec_bkt,
945 const struct rte_hash_key **key_slot, int32_t *positions,
946 uint64_t *extra_hits_mask, const void *keys,
947 const struct rte_hash *h)
949 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
950 unsigned total_hash_matches;
952 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
953 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
954 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
955 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
956 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
959 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
961 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
963 total_hash_matches = (prim_hash_matches |
964 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
965 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
966 key_idx * h->key_entry_size);
968 rte_prefetch0(*key_slot);
970 * Return index where key is stored,
971 * substracting the first dummy index
973 positions[idx] = (key_idx - 1);
975 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
980 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
982 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
983 const int32_t *positions, void *data[], uint64_t *hits,
984 const struct rte_hash *h)
989 hit = !rte_hash_cmp_eq(key_slot->key, keys[idx], h);
991 data[idx] = key_slot->pdata;
993 key_idx = positions[idx] + 1;
995 * If key index is 0, force hit to be 0, in case key to be looked up
996 * is all zero (as in the dummy slot), which would result in a wrong hit
998 *hits |= (uint64_t)(hit && !!key_idx) << idx;
1002 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1003 uint32_t num_keys, int32_t *positions,
1004 uint64_t *hit_mask, void *data[])
1007 uint64_t extra_hits_mask = 0;
1008 uint64_t lookup_mask, miss_mask;
1010 const void *key_store = h->key_store;
1012 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1014 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1015 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1016 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1017 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1018 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1019 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1020 hash_sig_t primary_hash10, primary_hash11;
1021 hash_sig_t secondary_hash10, secondary_hash11;
1022 hash_sig_t primary_hash20, primary_hash21;
1023 hash_sig_t secondary_hash20, secondary_hash21;
1025 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1026 miss_mask = lookup_mask;
1028 lookup_stage0(&idx00, &lookup_mask, keys);
1029 lookup_stage0(&idx01, &lookup_mask, keys);
1031 idx10 = idx00, idx11 = idx01;
1033 lookup_stage0(&idx00, &lookup_mask, keys);
1034 lookup_stage0(&idx01, &lookup_mask, keys);
1035 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1036 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1037 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1038 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1040 primary_bkt20 = primary_bkt10;
1041 primary_bkt21 = primary_bkt11;
1042 secondary_bkt20 = secondary_bkt10;
1043 secondary_bkt21 = secondary_bkt11;
1044 primary_hash20 = primary_hash10;
1045 primary_hash21 = primary_hash11;
1046 secondary_hash20 = secondary_hash10;
1047 secondary_hash21 = secondary_hash11;
1048 idx20 = idx10, idx21 = idx11;
1049 idx10 = idx00, idx11 = idx01;
1051 lookup_stage0(&idx00, &lookup_mask, keys);
1052 lookup_stage0(&idx01, &lookup_mask, keys);
1053 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1054 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1055 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1056 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1057 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1058 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1060 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1061 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1064 while (lookup_mask) {
1065 k_slot30 = k_slot20, k_slot31 = k_slot21;
1066 idx30 = idx20, idx31 = idx21;
1067 primary_bkt20 = primary_bkt10;
1068 primary_bkt21 = primary_bkt11;
1069 secondary_bkt20 = secondary_bkt10;
1070 secondary_bkt21 = secondary_bkt11;
1071 primary_hash20 = primary_hash10;
1072 primary_hash21 = primary_hash11;
1073 secondary_hash20 = secondary_hash10;
1074 secondary_hash21 = secondary_hash11;
1075 idx20 = idx10, idx21 = idx11;
1076 idx10 = idx00, idx11 = idx01;
1078 lookup_stage0(&idx00, &lookup_mask, keys);
1079 lookup_stage0(&idx01, &lookup_mask, keys);
1080 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1081 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1082 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1083 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1084 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1085 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1086 &extra_hits_mask, key_store, h);
1087 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1088 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1089 &extra_hits_mask, key_store, h);
1090 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1091 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1094 k_slot30 = k_slot20, k_slot31 = k_slot21;
1095 idx30 = idx20, idx31 = idx21;
1096 primary_bkt20 = primary_bkt10;
1097 primary_bkt21 = primary_bkt11;
1098 secondary_bkt20 = secondary_bkt10;
1099 secondary_bkt21 = secondary_bkt11;
1100 primary_hash20 = primary_hash10;
1101 primary_hash21 = primary_hash11;
1102 secondary_hash20 = secondary_hash10;
1103 secondary_hash21 = secondary_hash11;
1104 idx20 = idx10, idx21 = idx11;
1105 idx10 = idx00, idx11 = idx01;
1107 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1108 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1109 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1110 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1111 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1112 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1114 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1115 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1117 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1118 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1120 k_slot30 = k_slot20, k_slot31 = k_slot21;
1121 idx30 = idx20, idx31 = idx21;
1122 primary_bkt20 = primary_bkt10;
1123 primary_bkt21 = primary_bkt11;
1124 secondary_bkt20 = secondary_bkt10;
1125 secondary_bkt21 = secondary_bkt11;
1126 primary_hash20 = primary_hash10;
1127 primary_hash21 = primary_hash11;
1128 secondary_hash20 = secondary_hash10;
1129 secondary_hash21 = secondary_hash11;
1130 idx20 = idx10, idx21 = idx11;
1132 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1133 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1135 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1136 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1138 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1139 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1141 k_slot30 = k_slot20, k_slot31 = k_slot21;
1142 idx30 = idx20, idx31 = idx21;
1144 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1145 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1147 /* ignore any items we have already found */
1148 extra_hits_mask &= ~hits;
1150 if (unlikely(extra_hits_mask)) {
1151 /* run a single search for each remaining item */
1153 idx = __builtin_ctzl(extra_hits_mask);
1155 ret = rte_hash_lookup_with_hash_data(h,
1156 keys[idx], hash_vals[idx], &data[idx]);
1158 hits |= 1ULL << idx;
1160 positions[idx] = rte_hash_lookup_with_hash(h,
1161 keys[idx], hash_vals[idx]);
1162 if (positions[idx] >= 0)
1163 hits |= 1llu << idx;
1165 extra_hits_mask &= ~(1llu << idx);
1166 } while (extra_hits_mask);
1170 if (unlikely(miss_mask)) {
1172 idx = __builtin_ctzl(miss_mask);
1173 positions[idx] = -ENOENT;
1174 miss_mask &= ~(1llu << idx);
1175 } while (miss_mask);
1178 if (hit_mask != NULL)
1183 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1184 uint32_t num_keys, int32_t *positions)
1186 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1187 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1188 (positions == NULL)), -EINVAL);
1190 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1195 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1196 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1198 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1199 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1200 (hit_mask == NULL)), -EINVAL);
1202 int32_t positions[num_keys];
1204 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1206 /* Return number of hits */
1207 return __builtin_popcountl(*hit_mask);
1211 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1213 uint32_t bucket_idx, idx, position;
1214 struct rte_hash_key *next_key;
1216 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1218 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1220 if (*next >= total_entries)
1223 /* Calculate bucket and index of current iterator */
1224 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1225 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1227 /* If current position is empty, go to the next one */
1228 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1231 if (*next == total_entries)
1233 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1234 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1237 /* Get position of entry in key table */
1238 position = h->buckets[bucket_idx].key_idx[idx];
1239 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1240 position * h->key_entry_size);
1241 /* Return key and data */
1242 *key = next_key->key;
1243 *data = next_key->pdata;
1245 /* Increment iterator */
1248 return position - 1;