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>
60 #include <rte_pause.h>
63 #include "rte_cuckoo_hash.h"
65 #if defined(RTE_ARCH_X86)
66 #include "rte_cuckoo_hash_x86.h"
69 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
71 static struct rte_tailq_elem rte_hash_tailq = {
74 EAL_REGISTER_TAILQ(rte_hash_tailq)
77 rte_hash_find_existing(const char *name)
79 struct rte_hash *h = NULL;
80 struct rte_tailq_entry *te;
81 struct rte_hash_list *hash_list;
83 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
85 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
86 TAILQ_FOREACH(te, hash_list, next) {
87 h = (struct rte_hash *) te->data;
88 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
91 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
100 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
102 h->cmp_jump_table_idx = KEY_CUSTOM;
103 h->rte_hash_custom_cmp_eq = func;
107 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
109 if (h->cmp_jump_table_idx == KEY_CUSTOM)
110 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
112 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
116 rte_hash_create(const struct rte_hash_parameters *params)
118 struct rte_hash *h = NULL;
119 struct rte_tailq_entry *te = NULL;
120 struct rte_hash_list *hash_list;
121 struct rte_ring *r = NULL;
122 char hash_name[RTE_HASH_NAMESIZE];
124 void *buckets = NULL;
125 char ring_name[RTE_RING_NAMESIZE];
126 unsigned num_key_slots;
127 unsigned hw_trans_mem_support = 0;
130 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
132 if (params == NULL) {
133 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
137 /* Check for valid parameters */
138 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
139 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
140 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
141 (params->key_len == 0)) {
143 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
147 /* Check extra flags field to check extra options. */
148 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
149 hw_trans_mem_support = 1;
151 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
152 if (hw_trans_mem_support)
154 * Increase number of slots by total number of indices
155 * that can be stored in the lcore caches
156 * except for the first cache
158 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
159 LCORE_CACHE_SIZE + 1;
161 num_key_slots = params->entries + 1;
163 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
164 /* Create ring (Dummy slot index is not enqueued) */
165 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots - 1),
166 params->socket_id, 0);
168 RTE_LOG(ERR, HASH, "memory allocation failed\n");
172 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
174 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
176 /* guarantee there's no existing: this is normally already checked
177 * by ring creation above */
178 TAILQ_FOREACH(te, hash_list, next) {
179 h = (struct rte_hash *) te->data;
180 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
190 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
192 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
196 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
197 RTE_CACHE_LINE_SIZE, params->socket_id);
200 RTE_LOG(ERR, HASH, "memory allocation failed\n");
204 const uint32_t num_buckets = rte_align32pow2(params->entries)
205 / RTE_HASH_BUCKET_ENTRIES;
207 buckets = rte_zmalloc_socket(NULL,
208 num_buckets * sizeof(struct rte_hash_bucket),
209 RTE_CACHE_LINE_SIZE, params->socket_id);
211 if (buckets == NULL) {
212 RTE_LOG(ERR, HASH, "memory allocation failed\n");
216 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
217 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
219 k = rte_zmalloc_socket(NULL, key_tbl_size,
220 RTE_CACHE_LINE_SIZE, params->socket_id);
223 RTE_LOG(ERR, HASH, "memory allocation failed\n");
228 * If x86 architecture is used, select appropriate compare function,
229 * which may use x86 intrinsics, otherwise use memcmp
231 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
232 /* Select function to compare keys */
233 switch (params->key_len) {
235 h->cmp_jump_table_idx = KEY_16_BYTES;
238 h->cmp_jump_table_idx = KEY_32_BYTES;
241 h->cmp_jump_table_idx = KEY_48_BYTES;
244 h->cmp_jump_table_idx = KEY_64_BYTES;
247 h->cmp_jump_table_idx = KEY_80_BYTES;
250 h->cmp_jump_table_idx = KEY_96_BYTES;
253 h->cmp_jump_table_idx = KEY_112_BYTES;
256 h->cmp_jump_table_idx = KEY_128_BYTES;
259 /* If key is not multiple of 16, use generic memcmp */
260 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
263 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
266 if (hw_trans_mem_support) {
267 h->local_free_slots = rte_zmalloc_socket(NULL,
268 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
269 RTE_CACHE_LINE_SIZE, params->socket_id);
272 /* Setup hash context */
273 snprintf(h->name, sizeof(h->name), "%s", params->name);
274 h->entries = params->entries;
275 h->key_len = params->key_len;
276 h->key_entry_size = key_entry_size;
277 h->hash_func_init_val = params->hash_func_init_val;
279 h->num_buckets = num_buckets;
280 h->bucket_bitmask = h->num_buckets - 1;
281 h->buckets = buckets;
282 h->hash_func = (params->hash_func == NULL) ?
283 DEFAULT_HASH_FUNC : params->hash_func;
286 h->hw_trans_mem_support = hw_trans_mem_support;
288 #if defined(RTE_ARCH_X86)
289 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
290 h->sig_cmp_fn = RTE_HASH_COMPARE_AVX2;
291 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
292 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
295 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
297 /* Turn on multi-writer only with explicit flat from user and TM
300 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
301 if (h->hw_trans_mem_support) {
302 h->add_key = ADD_KEY_MULTIWRITER_TM;
304 h->add_key = ADD_KEY_MULTIWRITER;
305 h->multiwriter_lock = rte_malloc(NULL,
306 sizeof(rte_spinlock_t),
308 rte_spinlock_init(h->multiwriter_lock);
311 h->add_key = ADD_KEY_SINGLEWRITER;
313 /* Populate free slots ring. Entry zero is reserved for key misses. */
314 for (i = 1; i < params->entries + 1; i++)
315 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
317 te->data = (void *) h;
318 TAILQ_INSERT_TAIL(hash_list, te, next);
319 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
323 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
334 rte_hash_free(struct rte_hash *h)
336 struct rte_tailq_entry *te;
337 struct rte_hash_list *hash_list;
342 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
344 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
346 /* find out tailq entry */
347 TAILQ_FOREACH(te, hash_list, next) {
348 if (te->data == (void *) h)
353 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
357 TAILQ_REMOVE(hash_list, te, next);
359 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
361 if (h->hw_trans_mem_support)
362 rte_free(h->local_free_slots);
364 if (h->add_key == ADD_KEY_MULTIWRITER)
365 rte_free(h->multiwriter_lock);
366 rte_ring_free(h->free_slots);
367 rte_free(h->key_store);
368 rte_free(h->buckets);
374 rte_hash_hash(const struct rte_hash *h, const void *key)
376 /* calc hash result by key */
377 return h->hash_func(key, h->key_len, h->hash_func_init_val);
380 /* Calc the secondary hash value from the primary hash value of a given key */
381 static inline hash_sig_t
382 rte_hash_secondary_hash(const hash_sig_t primary_hash)
384 static const unsigned all_bits_shift = 12;
385 static const unsigned alt_bits_xor = 0x5bd1e995;
387 uint32_t tag = primary_hash >> all_bits_shift;
389 return primary_hash ^ ((tag + 1) * alt_bits_xor);
393 rte_hash_reset(struct rte_hash *h)
401 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
402 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
404 /* clear the free ring */
405 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
408 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
409 for (i = 1; i < h->entries + 1; i++)
410 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
412 if (h->hw_trans_mem_support) {
413 /* Reset local caches per lcore */
414 for (i = 0; i < RTE_MAX_LCORE; i++)
415 h->local_free_slots[i].len = 0;
419 /* Search for an entry that can be pushed to its alternative location */
421 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
423 static unsigned int nr_pushes;
426 uint32_t next_bucket_idx;
427 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
430 * Push existing item (search for bucket with space in
431 * alternative locations) to its alternative location
433 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
434 /* Search for space in alternative locations */
435 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
436 next_bkt[i] = &h->buckets[next_bucket_idx];
437 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
438 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
442 if (j != RTE_HASH_BUCKET_ENTRIES)
446 /* Alternative location has spare room (end of recursive function) */
447 if (i != RTE_HASH_BUCKET_ENTRIES) {
448 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
449 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
450 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
454 /* Pick entry that has not been pushed yet */
455 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
456 if (bkt->flag[i] == 0)
459 /* All entries have been pushed, so entry cannot be added */
460 if (i == RTE_HASH_BUCKET_ENTRIES || nr_pushes > RTE_HASH_MAX_PUSHES)
463 /* Set flag to indicate that this entry is going to be pushed */
467 /* Need room in alternative bucket to insert the pushed entry */
468 ret = make_space_bucket(h, next_bkt[i]);
470 * After recursive function.
471 * Clear flags and insert the pushed entry
472 * in its alternative location if successful,
478 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
479 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
480 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
488 * Function called to enqueue back an index in the cache/ring,
489 * as slot has not being used and it can be used in the
490 * next addition attempt.
493 enqueue_slot_back(const struct rte_hash *h,
494 struct lcore_cache *cached_free_slots,
497 if (h->hw_trans_mem_support) {
498 cached_free_slots->objs[cached_free_slots->len] = slot_id;
499 cached_free_slots->len++;
501 rte_ring_sp_enqueue(h->free_slots, slot_id);
504 static inline int32_t
505 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
506 hash_sig_t sig, void *data)
509 uint32_t prim_bucket_idx, sec_bucket_idx;
511 struct rte_hash_bucket *prim_bkt, *sec_bkt;
512 struct rte_hash_key *new_k, *k, *keys = h->key_store;
513 void *slot_id = NULL;
518 struct lcore_cache *cached_free_slots = NULL;
520 if (h->add_key == ADD_KEY_MULTIWRITER)
521 rte_spinlock_lock(h->multiwriter_lock);
523 prim_bucket_idx = sig & h->bucket_bitmask;
524 prim_bkt = &h->buckets[prim_bucket_idx];
525 rte_prefetch0(prim_bkt);
527 alt_hash = rte_hash_secondary_hash(sig);
528 sec_bucket_idx = alt_hash & h->bucket_bitmask;
529 sec_bkt = &h->buckets[sec_bucket_idx];
530 rte_prefetch0(sec_bkt);
532 /* Get a new slot for storing the new key */
533 if (h->hw_trans_mem_support) {
534 lcore_id = rte_lcore_id();
535 cached_free_slots = &h->local_free_slots[lcore_id];
536 /* Try to get a free slot from the local cache */
537 if (cached_free_slots->len == 0) {
538 /* Need to get another burst of free slots from global ring */
539 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
540 cached_free_slots->objs,
541 LCORE_CACHE_SIZE, NULL);
545 cached_free_slots->len += n_slots;
548 /* Get a free slot from the local cache */
549 cached_free_slots->len--;
550 slot_id = cached_free_slots->objs[cached_free_slots->len];
552 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
556 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
557 rte_prefetch0(new_k);
558 new_idx = (uint32_t)((uintptr_t) slot_id);
560 /* Check if key is already inserted in primary location */
561 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
562 if (prim_bkt->sig_current[i] == sig &&
563 prim_bkt->sig_alt[i] == alt_hash) {
564 k = (struct rte_hash_key *) ((char *)keys +
565 prim_bkt->key_idx[i] * h->key_entry_size);
566 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
567 /* Enqueue index of free slot back in the ring. */
568 enqueue_slot_back(h, cached_free_slots, slot_id);
572 * Return index where key is stored,
573 * subtracting the first dummy index
575 return prim_bkt->key_idx[i] - 1;
580 /* Check if key is already inserted in secondary location */
581 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
582 if (sec_bkt->sig_alt[i] == sig &&
583 sec_bkt->sig_current[i] == alt_hash) {
584 k = (struct rte_hash_key *) ((char *)keys +
585 sec_bkt->key_idx[i] * h->key_entry_size);
586 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
587 /* Enqueue index of free slot back in the ring. */
588 enqueue_slot_back(h, cached_free_slots, slot_id);
592 * Return index where key is stored,
593 * subtracting the first dummy index
595 return sec_bkt->key_idx[i] - 1;
601 rte_memcpy(new_k->key, key, h->key_len);
604 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
605 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
606 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
607 sig, alt_hash, new_idx);
611 /* Primary bucket full, need to make space for new entry */
612 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
618 /* Also search secondary bucket to get better occupancy */
619 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
626 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
627 /* Check if slot is available */
628 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
629 prim_bkt->sig_current[i] = sig;
630 prim_bkt->sig_alt[i] = alt_hash;
631 prim_bkt->key_idx[i] = new_idx;
636 if (i != RTE_HASH_BUCKET_ENTRIES) {
637 if (h->add_key == ADD_KEY_MULTIWRITER)
638 rte_spinlock_unlock(h->multiwriter_lock);
642 /* Primary bucket full, need to make space for new entry
643 * After recursive function.
644 * Insert the new entry in the position of the pushed entry
645 * if successful or return error and
646 * store the new slot back in the ring
648 ret = make_space_bucket(h, prim_bkt);
650 prim_bkt->sig_current[ret] = sig;
651 prim_bkt->sig_alt[ret] = alt_hash;
652 prim_bkt->key_idx[ret] = new_idx;
653 if (h->add_key == ADD_KEY_MULTIWRITER)
654 rte_spinlock_unlock(h->multiwriter_lock);
657 #if defined(RTE_ARCH_X86)
660 /* Error in addition, store new slot back in the ring and return error */
661 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
663 if (h->add_key == ADD_KEY_MULTIWRITER)
664 rte_spinlock_unlock(h->multiwriter_lock);
669 rte_hash_add_key_with_hash(const struct rte_hash *h,
670 const void *key, hash_sig_t sig)
672 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
673 return __rte_hash_add_key_with_hash(h, key, sig, 0);
677 rte_hash_add_key(const struct rte_hash *h, const void *key)
679 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
680 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
684 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
685 const void *key, hash_sig_t sig, void *data)
689 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
690 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
698 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
702 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
704 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
710 static inline int32_t
711 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
712 hash_sig_t sig, void **data)
717 struct rte_hash_bucket *bkt;
718 struct rte_hash_key *k, *keys = h->key_store;
720 bucket_idx = sig & h->bucket_bitmask;
721 bkt = &h->buckets[bucket_idx];
723 /* Check if key is in primary location */
724 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
725 if (bkt->sig_current[i] == sig &&
726 bkt->key_idx[i] != EMPTY_SLOT) {
727 k = (struct rte_hash_key *) ((char *)keys +
728 bkt->key_idx[i] * h->key_entry_size);
729 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
733 * Return index where key is stored,
734 * subtracting the first dummy index
736 return bkt->key_idx[i] - 1;
741 /* Calculate secondary hash */
742 alt_hash = rte_hash_secondary_hash(sig);
743 bucket_idx = alt_hash & h->bucket_bitmask;
744 bkt = &h->buckets[bucket_idx];
746 /* Check if key is in secondary location */
747 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
748 if (bkt->sig_current[i] == alt_hash &&
749 bkt->sig_alt[i] == sig) {
750 k = (struct rte_hash_key *) ((char *)keys +
751 bkt->key_idx[i] * h->key_entry_size);
752 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
756 * Return index where key is stored,
757 * subtracting the first dummy index
759 return bkt->key_idx[i] - 1;
768 rte_hash_lookup_with_hash(const struct rte_hash *h,
769 const void *key, hash_sig_t sig)
771 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
772 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
776 rte_hash_lookup(const struct rte_hash *h, const void *key)
778 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
779 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
783 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
784 const void *key, hash_sig_t sig, void **data)
786 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
787 return __rte_hash_lookup_with_hash(h, key, sig, data);
791 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
793 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
794 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
798 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
800 unsigned lcore_id, n_slots;
801 struct lcore_cache *cached_free_slots;
803 bkt->sig_current[i] = NULL_SIGNATURE;
804 bkt->sig_alt[i] = NULL_SIGNATURE;
805 if (h->hw_trans_mem_support) {
806 lcore_id = rte_lcore_id();
807 cached_free_slots = &h->local_free_slots[lcore_id];
808 /* Cache full, need to free it. */
809 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
810 /* Need to enqueue the free slots in global ring. */
811 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
812 cached_free_slots->objs,
813 LCORE_CACHE_SIZE, NULL);
814 cached_free_slots->len -= n_slots;
816 /* Put index of new free slot in cache. */
817 cached_free_slots->objs[cached_free_slots->len] =
818 (void *)((uintptr_t)bkt->key_idx[i]);
819 cached_free_slots->len++;
821 rte_ring_sp_enqueue(h->free_slots,
822 (void *)((uintptr_t)bkt->key_idx[i]));
826 static inline int32_t
827 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
833 struct rte_hash_bucket *bkt;
834 struct rte_hash_key *k, *keys = h->key_store;
837 bucket_idx = sig & h->bucket_bitmask;
838 bkt = &h->buckets[bucket_idx];
840 /* Check if key is in primary location */
841 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
842 if (bkt->sig_current[i] == sig &&
843 bkt->key_idx[i] != EMPTY_SLOT) {
844 k = (struct rte_hash_key *) ((char *)keys +
845 bkt->key_idx[i] * h->key_entry_size);
846 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
847 remove_entry(h, bkt, i);
850 * Return index where key is stored,
851 * subtracting the first dummy index
853 ret = bkt->key_idx[i] - 1;
854 bkt->key_idx[i] = EMPTY_SLOT;
860 /* Calculate secondary hash */
861 alt_hash = rte_hash_secondary_hash(sig);
862 bucket_idx = alt_hash & h->bucket_bitmask;
863 bkt = &h->buckets[bucket_idx];
865 /* Check if key is in secondary location */
866 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
867 if (bkt->sig_current[i] == alt_hash &&
868 bkt->key_idx[i] != EMPTY_SLOT) {
869 k = (struct rte_hash_key *) ((char *)keys +
870 bkt->key_idx[i] * h->key_entry_size);
871 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
872 remove_entry(h, bkt, i);
875 * Return index where key is stored,
876 * subtracting the first dummy index
878 ret = bkt->key_idx[i] - 1;
879 bkt->key_idx[i] = EMPTY_SLOT;
889 rte_hash_del_key_with_hash(const struct rte_hash *h,
890 const void *key, hash_sig_t sig)
892 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
893 return __rte_hash_del_key_with_hash(h, key, sig);
897 rte_hash_del_key(const struct rte_hash *h, const void *key)
899 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
900 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
904 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
907 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
909 struct rte_hash_key *k, *keys = h->key_store;
910 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
915 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
924 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
925 const struct rte_hash_bucket *prim_bkt,
926 const struct rte_hash_bucket *sec_bkt,
927 hash_sig_t prim_hash, hash_sig_t sec_hash,
928 enum rte_hash_sig_compare_function sig_cmp_fn)
932 switch (sig_cmp_fn) {
933 #ifdef RTE_MACHINE_CPUFLAG_AVX2
934 case RTE_HASH_COMPARE_AVX2:
935 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
937 (__m256i const *)prim_bkt->sig_current),
938 _mm256_set1_epi32(prim_hash)));
939 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
941 (__m256i const *)sec_bkt->sig_current),
942 _mm256_set1_epi32(sec_hash)));
945 #ifdef RTE_MACHINE_CPUFLAG_SSE2
946 case RTE_HASH_COMPARE_SSE:
947 /* Compare the first 4 signatures in the bucket */
948 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
950 (__m128i const *)prim_bkt->sig_current),
951 _mm_set1_epi32(prim_hash)));
952 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
954 (__m128i const *)&prim_bkt->sig_current[4]),
955 _mm_set1_epi32(prim_hash)))) << 4;
956 /* Compare the first 4 signatures in the bucket */
957 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
959 (__m128i const *)sec_bkt->sig_current),
960 _mm_set1_epi32(sec_hash)));
961 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
963 (__m128i const *)&sec_bkt->sig_current[4]),
964 _mm_set1_epi32(sec_hash)))) << 4;
968 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
969 *prim_hash_matches |=
970 ((prim_hash == prim_bkt->sig_current[i]) << i);
972 ((sec_hash == sec_bkt->sig_current[i]) << i);
978 #define PREFETCH_OFFSET 4
980 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
981 int32_t num_keys, int32_t *positions,
982 uint64_t *hit_mask, void *data[])
986 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
987 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
988 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
989 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
990 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
991 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
993 /* Prefetch first keys */
994 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
995 rte_prefetch0(keys[i]);
998 * Prefetch rest of the keys, calculate primary and
999 * secondary bucket and prefetch them
1001 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1002 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1004 prim_hash[i] = rte_hash_hash(h, keys[i]);
1005 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1007 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1008 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1010 rte_prefetch0(primary_bkt[i]);
1011 rte_prefetch0(secondary_bkt[i]);
1014 /* Calculate and prefetch rest of the buckets */
1015 for (; i < num_keys; i++) {
1016 prim_hash[i] = rte_hash_hash(h, keys[i]);
1017 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1019 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1020 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1022 rte_prefetch0(primary_bkt[i]);
1023 rte_prefetch0(secondary_bkt[i]);
1026 /* Compare signatures and prefetch key slot of first hit */
1027 for (i = 0; i < num_keys; i++) {
1028 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1029 primary_bkt[i], secondary_bkt[i],
1030 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1032 if (prim_hitmask[i]) {
1033 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1034 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1035 const struct rte_hash_key *key_slot =
1036 (const struct rte_hash_key *)(
1037 (const char *)h->key_store +
1038 key_idx * h->key_entry_size);
1039 rte_prefetch0(key_slot);
1043 if (sec_hitmask[i]) {
1044 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1045 uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1046 const struct rte_hash_key *key_slot =
1047 (const struct rte_hash_key *)(
1048 (const char *)h->key_store +
1049 key_idx * h->key_entry_size);
1050 rte_prefetch0(key_slot);
1054 /* Compare keys, first hits in primary first */
1055 for (i = 0; i < num_keys; i++) {
1056 positions[i] = -ENOENT;
1057 while (prim_hitmask[i]) {
1058 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1060 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1061 const struct rte_hash_key *key_slot =
1062 (const struct rte_hash_key *)(
1063 (const char *)h->key_store +
1064 key_idx * h->key_entry_size);
1066 * If key index is 0, do not compare key,
1067 * as it is checking the dummy slot
1069 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1071 data[i] = key_slot->pdata;
1074 positions[i] = key_idx - 1;
1077 prim_hitmask[i] &= ~(1 << (hit_index));
1080 while (sec_hitmask[i]) {
1081 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1083 uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1084 const struct rte_hash_key *key_slot =
1085 (const struct rte_hash_key *)(
1086 (const char *)h->key_store +
1087 key_idx * h->key_entry_size);
1089 * If key index is 0, do not compare key,
1090 * as it is checking the dummy slot
1093 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1095 data[i] = key_slot->pdata;
1098 positions[i] = key_idx - 1;
1101 sec_hitmask[i] &= ~(1 << (hit_index));
1108 if (hit_mask != NULL)
1113 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1114 uint32_t num_keys, int32_t *positions)
1116 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1117 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1118 (positions == NULL)), -EINVAL);
1120 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1125 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1126 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1128 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1129 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1130 (hit_mask == NULL)), -EINVAL);
1132 int32_t positions[num_keys];
1134 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1136 /* Return number of hits */
1137 return __builtin_popcountl(*hit_mask);
1141 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1143 uint32_t bucket_idx, idx, position;
1144 struct rte_hash_key *next_key;
1146 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1148 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1150 if (*next >= total_entries)
1153 /* Calculate bucket and index of current iterator */
1154 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1155 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1157 /* If current position is empty, go to the next one */
1158 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1161 if (*next == total_entries)
1163 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1164 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1167 /* Get position of entry in key table */
1168 position = h->buckets[bucket_idx].key_idx[idx];
1169 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1170 position * h->key_entry_size);
1171 /* Return key and data */
1172 *key = next_key->key;
1173 *data = next_key->pdata;
1175 /* Increment iterator */
1178 return position - 1;