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_malloc.h>
49 #include <rte_eal_memconfig.h>
50 #include <rte_per_lcore.h>
51 #include <rte_errno.h>
52 #include <rte_string_fns.h>
53 #include <rte_cpuflags.h>
54 #include <rte_rwlock.h>
55 #include <rte_spinlock.h>
57 #include <rte_compat.h>
58 #include <rte_pause.h>
61 #include "rte_cuckoo_hash.h"
63 #if defined(RTE_ARCH_X86)
64 #include "rte_cuckoo_hash_x86.h"
67 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
69 static struct rte_tailq_elem rte_hash_tailq = {
72 EAL_REGISTER_TAILQ(rte_hash_tailq)
75 rte_hash_find_existing(const char *name)
77 struct rte_hash *h = NULL;
78 struct rte_tailq_entry *te;
79 struct rte_hash_list *hash_list;
81 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
83 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
84 TAILQ_FOREACH(te, hash_list, next) {
85 h = (struct rte_hash *) te->data;
86 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
89 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
98 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
100 h->cmp_jump_table_idx = KEY_CUSTOM;
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) + 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),
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 #if defined(RTE_ARCH_X86)
287 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
288 h->sig_cmp_fn = RTE_HASH_COMPARE_AVX2;
289 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
290 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
293 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
295 /* Turn on multi-writer only with explicit flat from user and TM
298 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
299 if (h->hw_trans_mem_support) {
300 h->add_key = ADD_KEY_MULTIWRITER_TM;
302 h->add_key = ADD_KEY_MULTIWRITER;
303 h->multiwriter_lock = rte_malloc(NULL,
304 sizeof(rte_spinlock_t),
305 RTE_CACHE_LINE_SIZE);
306 if (h->multiwriter_lock == NULL)
309 rte_spinlock_init(h->multiwriter_lock);
312 h->add_key = ADD_KEY_SINGLEWRITER;
314 /* Populate free slots ring. Entry zero is reserved for key misses. */
315 for (i = 1; i < num_key_slots; i++)
316 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
318 te->data = (void *) h;
319 TAILQ_INSERT_TAIL(hash_list, te, next);
320 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
324 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
335 rte_hash_free(struct rte_hash *h)
337 struct rte_tailq_entry *te;
338 struct rte_hash_list *hash_list;
343 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
345 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
347 /* find out tailq entry */
348 TAILQ_FOREACH(te, hash_list, next) {
349 if (te->data == (void *) h)
354 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
358 TAILQ_REMOVE(hash_list, te, next);
360 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
362 if (h->hw_trans_mem_support)
363 rte_free(h->local_free_slots);
365 if (h->add_key == ADD_KEY_MULTIWRITER)
366 rte_free(h->multiwriter_lock);
367 rte_ring_free(h->free_slots);
368 rte_free(h->key_store);
369 rte_free(h->buckets);
375 rte_hash_hash(const struct rte_hash *h, const void *key)
377 /* calc hash result by key */
378 return h->hash_func(key, h->key_len, h->hash_func_init_val);
381 /* Calc the secondary hash value from the primary hash value of a given key */
382 static inline hash_sig_t
383 rte_hash_secondary_hash(const hash_sig_t primary_hash)
385 static const unsigned all_bits_shift = 12;
386 static const unsigned alt_bits_xor = 0x5bd1e995;
388 uint32_t tag = primary_hash >> all_bits_shift;
390 return primary_hash ^ ((tag + 1) * alt_bits_xor);
394 rte_hash_reset(struct rte_hash *h)
397 uint32_t tot_ring_cnt, i;
402 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
403 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
405 /* clear the free ring */
406 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
409 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
410 if (h->hw_trans_mem_support)
411 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
412 (LCORE_CACHE_SIZE - 1);
414 tot_ring_cnt = h->entries;
416 for (i = 1; i < tot_ring_cnt + 1; i++)
417 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
419 if (h->hw_trans_mem_support) {
420 /* Reset local caches per lcore */
421 for (i = 0; i < RTE_MAX_LCORE; i++)
422 h->local_free_slots[i].len = 0;
426 /* Search for an entry that can be pushed to its alternative location */
428 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt,
429 unsigned int *nr_pushes)
433 uint32_t next_bucket_idx;
434 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
437 * Push existing item (search for bucket with space in
438 * alternative locations) to its alternative location
440 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
441 /* Search for space in alternative locations */
442 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
443 next_bkt[i] = &h->buckets[next_bucket_idx];
444 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
445 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
449 if (j != RTE_HASH_BUCKET_ENTRIES)
453 /* Alternative location has spare room (end of recursive function) */
454 if (i != RTE_HASH_BUCKET_ENTRIES) {
455 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
456 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
457 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
461 /* Pick entry that has not been pushed yet */
462 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
463 if (bkt->flag[i] == 0)
466 /* All entries have been pushed, so entry cannot be added */
467 if (i == RTE_HASH_BUCKET_ENTRIES || ++(*nr_pushes) > RTE_HASH_MAX_PUSHES)
470 /* Set flag to indicate that this entry is going to be pushed */
473 /* Need room in alternative bucket to insert the pushed entry */
474 ret = make_space_bucket(h, next_bkt[i], nr_pushes);
476 * After recursive function.
477 * Clear flags and insert the pushed entry
478 * in its alternative location if successful,
483 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
484 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
485 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
493 * Function called to enqueue back an index in the cache/ring,
494 * as slot has not being used and it can be used in the
495 * next addition attempt.
498 enqueue_slot_back(const struct rte_hash *h,
499 struct lcore_cache *cached_free_slots,
502 if (h->hw_trans_mem_support) {
503 cached_free_slots->objs[cached_free_slots->len] = slot_id;
504 cached_free_slots->len++;
506 rte_ring_sp_enqueue(h->free_slots, slot_id);
509 static inline int32_t
510 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
511 hash_sig_t sig, void *data)
514 uint32_t prim_bucket_idx, sec_bucket_idx;
516 struct rte_hash_bucket *prim_bkt, *sec_bkt;
517 struct rte_hash_key *new_k, *k, *keys = h->key_store;
518 void *slot_id = NULL;
523 struct lcore_cache *cached_free_slots = NULL;
524 unsigned int nr_pushes = 0;
526 if (h->add_key == ADD_KEY_MULTIWRITER)
527 rte_spinlock_lock(h->multiwriter_lock);
529 prim_bucket_idx = sig & h->bucket_bitmask;
530 prim_bkt = &h->buckets[prim_bucket_idx];
531 rte_prefetch0(prim_bkt);
533 alt_hash = rte_hash_secondary_hash(sig);
534 sec_bucket_idx = alt_hash & h->bucket_bitmask;
535 sec_bkt = &h->buckets[sec_bucket_idx];
536 rte_prefetch0(sec_bkt);
538 /* Get a new slot for storing the new key */
539 if (h->hw_trans_mem_support) {
540 lcore_id = rte_lcore_id();
541 cached_free_slots = &h->local_free_slots[lcore_id];
542 /* Try to get a free slot from the local cache */
543 if (cached_free_slots->len == 0) {
544 /* Need to get another burst of free slots from global ring */
545 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
546 cached_free_slots->objs,
547 LCORE_CACHE_SIZE, NULL);
553 cached_free_slots->len += n_slots;
556 /* Get a free slot from the local cache */
557 cached_free_slots->len--;
558 slot_id = cached_free_slots->objs[cached_free_slots->len];
560 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0) {
566 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
567 rte_prefetch0(new_k);
568 new_idx = (uint32_t)((uintptr_t) slot_id);
570 /* Check if key is already inserted in primary location */
571 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
572 if (prim_bkt->sig_current[i] == sig &&
573 prim_bkt->sig_alt[i] == alt_hash) {
574 k = (struct rte_hash_key *) ((char *)keys +
575 prim_bkt->key_idx[i] * h->key_entry_size);
576 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
577 /* Enqueue index of free slot back in the ring. */
578 enqueue_slot_back(h, cached_free_slots, slot_id);
582 * Return index where key is stored,
583 * subtracting the first dummy index
585 ret = prim_bkt->key_idx[i] - 1;
591 /* Check if key is already inserted in secondary location */
592 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
593 if (sec_bkt->sig_alt[i] == sig &&
594 sec_bkt->sig_current[i] == alt_hash) {
595 k = (struct rte_hash_key *) ((char *)keys +
596 sec_bkt->key_idx[i] * h->key_entry_size);
597 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
598 /* Enqueue index of free slot back in the ring. */
599 enqueue_slot_back(h, cached_free_slots, slot_id);
603 * Return index where key is stored,
604 * subtracting the first dummy index
606 ret = sec_bkt->key_idx[i] - 1;
613 rte_memcpy(new_k->key, key, h->key_len);
616 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
617 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
618 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
619 sig, alt_hash, new_idx);
623 /* Primary bucket full, need to make space for new entry */
624 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
630 /* Also search secondary bucket to get better occupancy */
631 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
638 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
639 /* Check if slot is available */
640 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
641 prim_bkt->sig_current[i] = sig;
642 prim_bkt->sig_alt[i] = alt_hash;
643 prim_bkt->key_idx[i] = new_idx;
648 if (i != RTE_HASH_BUCKET_ENTRIES) {
649 if (h->add_key == ADD_KEY_MULTIWRITER)
650 rte_spinlock_unlock(h->multiwriter_lock);
654 /* Primary bucket full, need to make space for new entry
655 * After recursive function.
656 * Insert the new entry in the position of the pushed entry
657 * if successful or return error and
658 * store the new slot back in the ring
660 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
662 prim_bkt->sig_current[ret] = sig;
663 prim_bkt->sig_alt[ret] = alt_hash;
664 prim_bkt->key_idx[ret] = new_idx;
665 if (h->add_key == ADD_KEY_MULTIWRITER)
666 rte_spinlock_unlock(h->multiwriter_lock);
669 #if defined(RTE_ARCH_X86)
672 /* Error in addition, store new slot back in the ring and return error */
673 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
676 if (h->add_key == ADD_KEY_MULTIWRITER)
677 rte_spinlock_unlock(h->multiwriter_lock);
682 rte_hash_add_key_with_hash(const struct rte_hash *h,
683 const void *key, hash_sig_t sig)
685 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
686 return __rte_hash_add_key_with_hash(h, key, sig, 0);
690 rte_hash_add_key(const struct rte_hash *h, const void *key)
692 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
693 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
697 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
698 const void *key, hash_sig_t sig, void *data)
702 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
703 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
711 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
715 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
717 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
723 static inline int32_t
724 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
725 hash_sig_t sig, void **data)
730 struct rte_hash_bucket *bkt;
731 struct rte_hash_key *k, *keys = h->key_store;
733 bucket_idx = sig & h->bucket_bitmask;
734 bkt = &h->buckets[bucket_idx];
736 /* Check if key is in primary location */
737 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
738 if (bkt->sig_current[i] == sig &&
739 bkt->key_idx[i] != EMPTY_SLOT) {
740 k = (struct rte_hash_key *) ((char *)keys +
741 bkt->key_idx[i] * h->key_entry_size);
742 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
746 * Return index where key is stored,
747 * subtracting the first dummy index
749 return bkt->key_idx[i] - 1;
754 /* Calculate secondary hash */
755 alt_hash = rte_hash_secondary_hash(sig);
756 bucket_idx = alt_hash & h->bucket_bitmask;
757 bkt = &h->buckets[bucket_idx];
759 /* Check if key is in secondary location */
760 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
761 if (bkt->sig_current[i] == alt_hash &&
762 bkt->sig_alt[i] == sig) {
763 k = (struct rte_hash_key *) ((char *)keys +
764 bkt->key_idx[i] * h->key_entry_size);
765 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
769 * Return index where key is stored,
770 * subtracting the first dummy index
772 return bkt->key_idx[i] - 1;
781 rte_hash_lookup_with_hash(const struct rte_hash *h,
782 const void *key, hash_sig_t sig)
784 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
785 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
789 rte_hash_lookup(const struct rte_hash *h, const void *key)
791 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
792 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
796 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
797 const void *key, hash_sig_t sig, void **data)
799 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
800 return __rte_hash_lookup_with_hash(h, key, sig, data);
804 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
806 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
807 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
811 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
813 unsigned lcore_id, n_slots;
814 struct lcore_cache *cached_free_slots;
816 bkt->sig_current[i] = NULL_SIGNATURE;
817 bkt->sig_alt[i] = NULL_SIGNATURE;
818 if (h->hw_trans_mem_support) {
819 lcore_id = rte_lcore_id();
820 cached_free_slots = &h->local_free_slots[lcore_id];
821 /* Cache full, need to free it. */
822 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
823 /* Need to enqueue the free slots in global ring. */
824 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
825 cached_free_slots->objs,
826 LCORE_CACHE_SIZE, NULL);
827 cached_free_slots->len -= n_slots;
829 /* Put index of new free slot in cache. */
830 cached_free_slots->objs[cached_free_slots->len] =
831 (void *)((uintptr_t)bkt->key_idx[i]);
832 cached_free_slots->len++;
834 rte_ring_sp_enqueue(h->free_slots,
835 (void *)((uintptr_t)bkt->key_idx[i]));
839 static inline int32_t
840 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
846 struct rte_hash_bucket *bkt;
847 struct rte_hash_key *k, *keys = h->key_store;
850 bucket_idx = sig & h->bucket_bitmask;
851 bkt = &h->buckets[bucket_idx];
853 /* Check if key is in primary location */
854 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
855 if (bkt->sig_current[i] == sig &&
856 bkt->key_idx[i] != EMPTY_SLOT) {
857 k = (struct rte_hash_key *) ((char *)keys +
858 bkt->key_idx[i] * h->key_entry_size);
859 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
860 remove_entry(h, bkt, i);
863 * Return index where key is stored,
864 * subtracting the first dummy index
866 ret = bkt->key_idx[i] - 1;
867 bkt->key_idx[i] = EMPTY_SLOT;
873 /* Calculate secondary hash */
874 alt_hash = rte_hash_secondary_hash(sig);
875 bucket_idx = alt_hash & h->bucket_bitmask;
876 bkt = &h->buckets[bucket_idx];
878 /* Check if key is in secondary location */
879 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
880 if (bkt->sig_current[i] == alt_hash &&
881 bkt->key_idx[i] != EMPTY_SLOT) {
882 k = (struct rte_hash_key *) ((char *)keys +
883 bkt->key_idx[i] * h->key_entry_size);
884 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
885 remove_entry(h, bkt, i);
888 * Return index where key is stored,
889 * subtracting the first dummy index
891 ret = bkt->key_idx[i] - 1;
892 bkt->key_idx[i] = EMPTY_SLOT;
902 rte_hash_del_key_with_hash(const struct rte_hash *h,
903 const void *key, hash_sig_t sig)
905 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
906 return __rte_hash_del_key_with_hash(h, key, sig);
910 rte_hash_del_key(const struct rte_hash *h, const void *key)
912 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
913 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
917 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
920 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
922 struct rte_hash_key *k, *keys = h->key_store;
923 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
928 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
937 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
938 const struct rte_hash_bucket *prim_bkt,
939 const struct rte_hash_bucket *sec_bkt,
940 hash_sig_t prim_hash, hash_sig_t sec_hash,
941 enum rte_hash_sig_compare_function sig_cmp_fn)
945 switch (sig_cmp_fn) {
946 #ifdef RTE_MACHINE_CPUFLAG_AVX2
947 case RTE_HASH_COMPARE_AVX2:
948 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
950 (__m256i const *)prim_bkt->sig_current),
951 _mm256_set1_epi32(prim_hash)));
952 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
954 (__m256i const *)sec_bkt->sig_current),
955 _mm256_set1_epi32(sec_hash)));
958 #ifdef RTE_MACHINE_CPUFLAG_SSE2
959 case RTE_HASH_COMPARE_SSE:
960 /* Compare the first 4 signatures in the bucket */
961 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
963 (__m128i const *)prim_bkt->sig_current),
964 _mm_set1_epi32(prim_hash)));
965 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
967 (__m128i const *)&prim_bkt->sig_current[4]),
968 _mm_set1_epi32(prim_hash)))) << 4;
969 /* Compare the first 4 signatures in the bucket */
970 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
972 (__m128i const *)sec_bkt->sig_current),
973 _mm_set1_epi32(sec_hash)));
974 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
976 (__m128i const *)&sec_bkt->sig_current[4]),
977 _mm_set1_epi32(sec_hash)))) << 4;
981 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
982 *prim_hash_matches |=
983 ((prim_hash == prim_bkt->sig_current[i]) << i);
985 ((sec_hash == sec_bkt->sig_current[i]) << i);
991 #define PREFETCH_OFFSET 4
993 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
994 int32_t num_keys, int32_t *positions,
995 uint64_t *hit_mask, void *data[])
999 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
1000 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
1001 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1002 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1003 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1004 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1006 /* Prefetch first keys */
1007 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1008 rte_prefetch0(keys[i]);
1011 * Prefetch rest of the keys, calculate primary and
1012 * secondary bucket and prefetch them
1014 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1015 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1017 prim_hash[i] = rte_hash_hash(h, keys[i]);
1018 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1020 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1021 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1023 rte_prefetch0(primary_bkt[i]);
1024 rte_prefetch0(secondary_bkt[i]);
1027 /* Calculate and prefetch rest of the buckets */
1028 for (; i < num_keys; i++) {
1029 prim_hash[i] = rte_hash_hash(h, keys[i]);
1030 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1032 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1033 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1035 rte_prefetch0(primary_bkt[i]);
1036 rte_prefetch0(secondary_bkt[i]);
1039 /* Compare signatures and prefetch key slot of first hit */
1040 for (i = 0; i < num_keys; i++) {
1041 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1042 primary_bkt[i], secondary_bkt[i],
1043 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1045 if (prim_hitmask[i]) {
1046 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1047 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1048 const struct rte_hash_key *key_slot =
1049 (const struct rte_hash_key *)(
1050 (const char *)h->key_store +
1051 key_idx * h->key_entry_size);
1052 rte_prefetch0(key_slot);
1056 if (sec_hitmask[i]) {
1057 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1058 uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1059 const struct rte_hash_key *key_slot =
1060 (const struct rte_hash_key *)(
1061 (const char *)h->key_store +
1062 key_idx * h->key_entry_size);
1063 rte_prefetch0(key_slot);
1067 /* Compare keys, first hits in primary first */
1068 for (i = 0; i < num_keys; i++) {
1069 positions[i] = -ENOENT;
1070 while (prim_hitmask[i]) {
1071 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1073 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1074 const struct rte_hash_key *key_slot =
1075 (const struct rte_hash_key *)(
1076 (const char *)h->key_store +
1077 key_idx * h->key_entry_size);
1079 * If key index is 0, do not compare key,
1080 * as it is checking the dummy slot
1082 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1084 data[i] = key_slot->pdata;
1087 positions[i] = key_idx - 1;
1090 prim_hitmask[i] &= ~(1 << (hit_index));
1093 while (sec_hitmask[i]) {
1094 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1096 uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1097 const struct rte_hash_key *key_slot =
1098 (const struct rte_hash_key *)(
1099 (const char *)h->key_store +
1100 key_idx * h->key_entry_size);
1102 * If key index is 0, do not compare key,
1103 * as it is checking the dummy slot
1106 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1108 data[i] = key_slot->pdata;
1111 positions[i] = key_idx - 1;
1114 sec_hitmask[i] &= ~(1 << (hit_index));
1121 if (hit_mask != NULL)
1126 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1127 uint32_t num_keys, int32_t *positions)
1129 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1130 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1131 (positions == NULL)), -EINVAL);
1133 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1138 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1139 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1141 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1142 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1143 (hit_mask == NULL)), -EINVAL);
1145 int32_t positions[num_keys];
1147 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1149 /* Return number of hits */
1150 return __builtin_popcountl(*hit_mask);
1154 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1156 uint32_t bucket_idx, idx, position;
1157 struct rte_hash_key *next_key;
1159 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1161 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1163 if (*next >= total_entries)
1166 /* Calculate bucket and index of current iterator */
1167 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1168 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1170 /* If current position is empty, go to the next one */
1171 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1174 if (*next == total_entries)
1176 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1177 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1180 /* Get position of entry in key table */
1181 position = h->buckets[bucket_idx].key_idx[idx];
1182 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1183 position * h->key_entry_size);
1184 /* Return key and data */
1185 *key = next_key->key;
1186 *data = next_key->pdata;
1188 /* Increment iterator */
1191 return position - 1;