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;
159 num_key_slots = params->entries + 1;
161 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
162 /* Create ring (Dummy slot index is not enqueued) */
163 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots - 1),
164 params->socket_id, 0);
166 RTE_LOG(ERR, HASH, "memory allocation failed\n");
170 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
172 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
174 /* guarantee there's no existing: this is normally already checked
175 * by ring creation above */
176 TAILQ_FOREACH(te, hash_list, next) {
177 h = (struct rte_hash *) te->data;
178 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
188 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
190 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
194 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
195 RTE_CACHE_LINE_SIZE, params->socket_id);
198 RTE_LOG(ERR, HASH, "memory allocation failed\n");
202 const uint32_t num_buckets = rte_align32pow2(params->entries)
203 / RTE_HASH_BUCKET_ENTRIES;
205 buckets = rte_zmalloc_socket(NULL,
206 num_buckets * sizeof(struct rte_hash_bucket),
207 RTE_CACHE_LINE_SIZE, params->socket_id);
209 if (buckets == NULL) {
210 RTE_LOG(ERR, HASH, "memory allocation failed\n");
214 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
215 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
217 k = rte_zmalloc_socket(NULL, key_tbl_size,
218 RTE_CACHE_LINE_SIZE, params->socket_id);
221 RTE_LOG(ERR, HASH, "memory allocation failed\n");
226 * If x86 architecture is used, select appropriate compare function,
227 * which may use x86 intrinsics, otherwise use memcmp
229 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
230 /* Select function to compare keys */
231 switch (params->key_len) {
233 h->cmp_jump_table_idx = KEY_16_BYTES;
236 h->cmp_jump_table_idx = KEY_32_BYTES;
239 h->cmp_jump_table_idx = KEY_48_BYTES;
242 h->cmp_jump_table_idx = KEY_64_BYTES;
245 h->cmp_jump_table_idx = KEY_80_BYTES;
248 h->cmp_jump_table_idx = KEY_96_BYTES;
251 h->cmp_jump_table_idx = KEY_112_BYTES;
254 h->cmp_jump_table_idx = KEY_128_BYTES;
257 /* If key is not multiple of 16, use generic memcmp */
258 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
261 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
264 if (hw_trans_mem_support) {
265 h->local_free_slots = rte_zmalloc_socket(NULL,
266 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
267 RTE_CACHE_LINE_SIZE, params->socket_id);
270 /* Setup hash context */
271 snprintf(h->name, sizeof(h->name), "%s", params->name);
272 h->entries = params->entries;
273 h->key_len = params->key_len;
274 h->key_entry_size = key_entry_size;
275 h->hash_func_init_val = params->hash_func_init_val;
277 h->num_buckets = num_buckets;
278 h->bucket_bitmask = h->num_buckets - 1;
279 h->buckets = buckets;
280 h->hash_func = (params->hash_func == NULL) ?
281 DEFAULT_HASH_FUNC : params->hash_func;
284 h->hw_trans_mem_support = hw_trans_mem_support;
286 #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),
306 rte_spinlock_init(h->multiwriter_lock);
309 h->add_key = ADD_KEY_SINGLEWRITER;
311 /* Populate free slots ring. Entry zero is reserved for key misses. */
312 for (i = 1; i < params->entries + 1; i++)
313 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
315 te->data = (void *) h;
316 TAILQ_INSERT_TAIL(hash_list, te, next);
317 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
321 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
332 rte_hash_free(struct rte_hash *h)
334 struct rte_tailq_entry *te;
335 struct rte_hash_list *hash_list;
340 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
342 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
344 /* find out tailq entry */
345 TAILQ_FOREACH(te, hash_list, next) {
346 if (te->data == (void *) h)
351 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
355 TAILQ_REMOVE(hash_list, te, next);
357 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
359 if (h->hw_trans_mem_support)
360 rte_free(h->local_free_slots);
362 if (h->add_key == ADD_KEY_MULTIWRITER)
363 rte_free(h->multiwriter_lock);
364 rte_ring_free(h->free_slots);
365 rte_free(h->key_store);
366 rte_free(h->buckets);
372 rte_hash_hash(const struct rte_hash *h, const void *key)
374 /* calc hash result by key */
375 return h->hash_func(key, h->key_len, h->hash_func_init_val);
378 /* Calc the secondary hash value from the primary hash value of a given key */
379 static inline hash_sig_t
380 rte_hash_secondary_hash(const hash_sig_t primary_hash)
382 static const unsigned all_bits_shift = 12;
383 static const unsigned alt_bits_xor = 0x5bd1e995;
385 uint32_t tag = primary_hash >> all_bits_shift;
387 return primary_hash ^ ((tag + 1) * alt_bits_xor);
391 rte_hash_reset(struct rte_hash *h)
399 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
400 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
402 /* clear the free ring */
403 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
406 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
407 for (i = 1; i < h->entries + 1; i++)
408 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
410 if (h->hw_trans_mem_support) {
411 /* Reset local caches per lcore */
412 for (i = 0; i < RTE_MAX_LCORE; i++)
413 h->local_free_slots[i].len = 0;
417 /* Search for an entry that can be pushed to its alternative location */
419 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt,
420 unsigned int *nr_pushes)
424 uint32_t next_bucket_idx;
425 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
428 * Push existing item (search for bucket with space in
429 * alternative locations) to its alternative location
431 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
432 /* Search for space in alternative locations */
433 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
434 next_bkt[i] = &h->buckets[next_bucket_idx];
435 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
436 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
440 if (j != RTE_HASH_BUCKET_ENTRIES)
444 /* Alternative location has spare room (end of recursive function) */
445 if (i != RTE_HASH_BUCKET_ENTRIES) {
446 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
447 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
448 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
452 /* Pick entry that has not been pushed yet */
453 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
454 if (bkt->flag[i] == 0)
457 /* All entries have been pushed, so entry cannot be added */
458 if (i == RTE_HASH_BUCKET_ENTRIES || ++(*nr_pushes) > RTE_HASH_MAX_PUSHES)
461 /* Set flag to indicate that this entry is going to be pushed */
464 /* Need room in alternative bucket to insert the pushed entry */
465 ret = make_space_bucket(h, next_bkt[i], nr_pushes);
467 * After recursive function.
468 * Clear flags and insert the pushed entry
469 * in its alternative location if successful,
474 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
475 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
476 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
484 * Function called to enqueue back an index in the cache/ring,
485 * as slot has not being used and it can be used in the
486 * next addition attempt.
489 enqueue_slot_back(const struct rte_hash *h,
490 struct lcore_cache *cached_free_slots,
493 if (h->hw_trans_mem_support) {
494 cached_free_slots->objs[cached_free_slots->len] = slot_id;
495 cached_free_slots->len++;
497 rte_ring_sp_enqueue(h->free_slots, slot_id);
500 static inline int32_t
501 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
502 hash_sig_t sig, void *data)
505 uint32_t prim_bucket_idx, sec_bucket_idx;
507 struct rte_hash_bucket *prim_bkt, *sec_bkt;
508 struct rte_hash_key *new_k, *k, *keys = h->key_store;
509 void *slot_id = NULL;
514 struct lcore_cache *cached_free_slots = NULL;
515 unsigned int nr_pushes = 0;
517 if (h->add_key == ADD_KEY_MULTIWRITER)
518 rte_spinlock_lock(h->multiwriter_lock);
520 prim_bucket_idx = sig & h->bucket_bitmask;
521 prim_bkt = &h->buckets[prim_bucket_idx];
522 rte_prefetch0(prim_bkt);
524 alt_hash = rte_hash_secondary_hash(sig);
525 sec_bucket_idx = alt_hash & h->bucket_bitmask;
526 sec_bkt = &h->buckets[sec_bucket_idx];
527 rte_prefetch0(sec_bkt);
529 /* Get a new slot for storing the new key */
530 if (h->hw_trans_mem_support) {
531 lcore_id = rte_lcore_id();
532 cached_free_slots = &h->local_free_slots[lcore_id];
533 /* Try to get a free slot from the local cache */
534 if (cached_free_slots->len == 0) {
535 /* Need to get another burst of free slots from global ring */
536 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
537 cached_free_slots->objs,
538 LCORE_CACHE_SIZE, NULL);
544 cached_free_slots->len += n_slots;
547 /* Get a free slot from the local cache */
548 cached_free_slots->len--;
549 slot_id = cached_free_slots->objs[cached_free_slots->len];
551 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0) {
557 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
558 rte_prefetch0(new_k);
559 new_idx = (uint32_t)((uintptr_t) slot_id);
561 /* Check if key is already inserted in primary location */
562 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
563 if (prim_bkt->sig_current[i] == sig &&
564 prim_bkt->sig_alt[i] == alt_hash) {
565 k = (struct rte_hash_key *) ((char *)keys +
566 prim_bkt->key_idx[i] * h->key_entry_size);
567 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
568 /* Enqueue index of free slot back in the ring. */
569 enqueue_slot_back(h, cached_free_slots, slot_id);
573 * Return index where key is stored,
574 * subtracting the first dummy index
576 ret = prim_bkt->key_idx[i] - 1;
582 /* Check if key is already inserted in secondary location */
583 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
584 if (sec_bkt->sig_alt[i] == sig &&
585 sec_bkt->sig_current[i] == alt_hash) {
586 k = (struct rte_hash_key *) ((char *)keys +
587 sec_bkt->key_idx[i] * h->key_entry_size);
588 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
589 /* Enqueue index of free slot back in the ring. */
590 enqueue_slot_back(h, cached_free_slots, slot_id);
594 * Return index where key is stored,
595 * subtracting the first dummy index
597 ret = sec_bkt->key_idx[i] - 1;
604 rte_memcpy(new_k->key, key, h->key_len);
607 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
608 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
609 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
610 sig, alt_hash, new_idx);
614 /* Primary bucket full, need to make space for new entry */
615 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
621 /* Also search secondary bucket to get better occupancy */
622 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
629 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
630 /* Check if slot is available */
631 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
632 prim_bkt->sig_current[i] = sig;
633 prim_bkt->sig_alt[i] = alt_hash;
634 prim_bkt->key_idx[i] = new_idx;
639 if (i != RTE_HASH_BUCKET_ENTRIES) {
640 if (h->add_key == ADD_KEY_MULTIWRITER)
641 rte_spinlock_unlock(h->multiwriter_lock);
645 /* Primary bucket full, need to make space for new entry
646 * After recursive function.
647 * Insert the new entry in the position of the pushed entry
648 * if successful or return error and
649 * store the new slot back in the ring
651 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
653 prim_bkt->sig_current[ret] = sig;
654 prim_bkt->sig_alt[ret] = alt_hash;
655 prim_bkt->key_idx[ret] = new_idx;
656 if (h->add_key == ADD_KEY_MULTIWRITER)
657 rte_spinlock_unlock(h->multiwriter_lock);
660 #if defined(RTE_ARCH_X86)
663 /* Error in addition, store new slot back in the ring and return error */
664 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
667 if (h->add_key == ADD_KEY_MULTIWRITER)
668 rte_spinlock_unlock(h->multiwriter_lock);
673 rte_hash_add_key_with_hash(const struct rte_hash *h,
674 const void *key, hash_sig_t sig)
676 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
677 return __rte_hash_add_key_with_hash(h, key, sig, 0);
681 rte_hash_add_key(const struct rte_hash *h, const void *key)
683 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
684 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
688 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
689 const void *key, hash_sig_t sig, void *data)
693 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
694 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
702 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
706 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
708 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
714 static inline int32_t
715 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
716 hash_sig_t sig, void **data)
721 struct rte_hash_bucket *bkt;
722 struct rte_hash_key *k, *keys = h->key_store;
724 bucket_idx = sig & h->bucket_bitmask;
725 bkt = &h->buckets[bucket_idx];
727 /* Check if key is in primary location */
728 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
729 if (bkt->sig_current[i] == sig &&
730 bkt->key_idx[i] != EMPTY_SLOT) {
731 k = (struct rte_hash_key *) ((char *)keys +
732 bkt->key_idx[i] * h->key_entry_size);
733 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
737 * Return index where key is stored,
738 * subtracting the first dummy index
740 return bkt->key_idx[i] - 1;
745 /* Calculate secondary hash */
746 alt_hash = rte_hash_secondary_hash(sig);
747 bucket_idx = alt_hash & h->bucket_bitmask;
748 bkt = &h->buckets[bucket_idx];
750 /* Check if key is in secondary location */
751 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
752 if (bkt->sig_current[i] == alt_hash &&
753 bkt->sig_alt[i] == sig) {
754 k = (struct rte_hash_key *) ((char *)keys +
755 bkt->key_idx[i] * h->key_entry_size);
756 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
760 * Return index where key is stored,
761 * subtracting the first dummy index
763 return bkt->key_idx[i] - 1;
772 rte_hash_lookup_with_hash(const struct rte_hash *h,
773 const void *key, hash_sig_t sig)
775 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
776 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
780 rte_hash_lookup(const struct rte_hash *h, const void *key)
782 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
783 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
787 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
788 const void *key, hash_sig_t sig, void **data)
790 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
791 return __rte_hash_lookup_with_hash(h, key, sig, data);
795 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
797 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
798 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
802 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
804 unsigned lcore_id, n_slots;
805 struct lcore_cache *cached_free_slots;
807 bkt->sig_current[i] = NULL_SIGNATURE;
808 bkt->sig_alt[i] = NULL_SIGNATURE;
809 if (h->hw_trans_mem_support) {
810 lcore_id = rte_lcore_id();
811 cached_free_slots = &h->local_free_slots[lcore_id];
812 /* Cache full, need to free it. */
813 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
814 /* Need to enqueue the free slots in global ring. */
815 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
816 cached_free_slots->objs,
817 LCORE_CACHE_SIZE, NULL);
818 cached_free_slots->len -= n_slots;
820 /* Put index of new free slot in cache. */
821 cached_free_slots->objs[cached_free_slots->len] =
822 (void *)((uintptr_t)bkt->key_idx[i]);
823 cached_free_slots->len++;
825 rte_ring_sp_enqueue(h->free_slots,
826 (void *)((uintptr_t)bkt->key_idx[i]));
830 static inline int32_t
831 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
837 struct rte_hash_bucket *bkt;
838 struct rte_hash_key *k, *keys = h->key_store;
841 bucket_idx = sig & h->bucket_bitmask;
842 bkt = &h->buckets[bucket_idx];
844 /* Check if key is in primary location */
845 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
846 if (bkt->sig_current[i] == sig &&
847 bkt->key_idx[i] != EMPTY_SLOT) {
848 k = (struct rte_hash_key *) ((char *)keys +
849 bkt->key_idx[i] * h->key_entry_size);
850 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
851 remove_entry(h, bkt, i);
854 * Return index where key is stored,
855 * subtracting the first dummy index
857 ret = bkt->key_idx[i] - 1;
858 bkt->key_idx[i] = EMPTY_SLOT;
864 /* Calculate secondary hash */
865 alt_hash = rte_hash_secondary_hash(sig);
866 bucket_idx = alt_hash & h->bucket_bitmask;
867 bkt = &h->buckets[bucket_idx];
869 /* Check if key is in secondary location */
870 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
871 if (bkt->sig_current[i] == alt_hash &&
872 bkt->key_idx[i] != EMPTY_SLOT) {
873 k = (struct rte_hash_key *) ((char *)keys +
874 bkt->key_idx[i] * h->key_entry_size);
875 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
876 remove_entry(h, bkt, i);
879 * Return index where key is stored,
880 * subtracting the first dummy index
882 ret = bkt->key_idx[i] - 1;
883 bkt->key_idx[i] = EMPTY_SLOT;
893 rte_hash_del_key_with_hash(const struct rte_hash *h,
894 const void *key, hash_sig_t sig)
896 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
897 return __rte_hash_del_key_with_hash(h, key, sig);
901 rte_hash_del_key(const struct rte_hash *h, const void *key)
903 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
904 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
908 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
911 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
913 struct rte_hash_key *k, *keys = h->key_store;
914 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
919 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
928 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
929 const struct rte_hash_bucket *prim_bkt,
930 const struct rte_hash_bucket *sec_bkt,
931 hash_sig_t prim_hash, hash_sig_t sec_hash,
932 enum rte_hash_sig_compare_function sig_cmp_fn)
936 switch (sig_cmp_fn) {
937 #ifdef RTE_MACHINE_CPUFLAG_AVX2
938 case RTE_HASH_COMPARE_AVX2:
939 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
941 (__m256i const *)prim_bkt->sig_current),
942 _mm256_set1_epi32(prim_hash)));
943 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
945 (__m256i const *)sec_bkt->sig_current),
946 _mm256_set1_epi32(sec_hash)));
949 #ifdef RTE_MACHINE_CPUFLAG_SSE2
950 case RTE_HASH_COMPARE_SSE:
951 /* Compare the first 4 signatures in the bucket */
952 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
954 (__m128i const *)prim_bkt->sig_current),
955 _mm_set1_epi32(prim_hash)));
956 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
958 (__m128i const *)&prim_bkt->sig_current[4]),
959 _mm_set1_epi32(prim_hash)))) << 4;
960 /* Compare the first 4 signatures in the bucket */
961 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
963 (__m128i const *)sec_bkt->sig_current),
964 _mm_set1_epi32(sec_hash)));
965 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
967 (__m128i const *)&sec_bkt->sig_current[4]),
968 _mm_set1_epi32(sec_hash)))) << 4;
972 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
973 *prim_hash_matches |=
974 ((prim_hash == prim_bkt->sig_current[i]) << i);
976 ((sec_hash == sec_bkt->sig_current[i]) << i);
982 #define PREFETCH_OFFSET 4
984 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
985 int32_t num_keys, int32_t *positions,
986 uint64_t *hit_mask, void *data[])
990 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
991 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
992 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
993 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
994 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
995 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
997 /* Prefetch first keys */
998 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
999 rte_prefetch0(keys[i]);
1002 * Prefetch rest of the keys, calculate primary and
1003 * secondary bucket and prefetch them
1005 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1006 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1008 prim_hash[i] = rte_hash_hash(h, keys[i]);
1009 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1011 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1012 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1014 rte_prefetch0(primary_bkt[i]);
1015 rte_prefetch0(secondary_bkt[i]);
1018 /* Calculate and prefetch rest of the buckets */
1019 for (; i < num_keys; i++) {
1020 prim_hash[i] = rte_hash_hash(h, keys[i]);
1021 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1023 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1024 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1026 rte_prefetch0(primary_bkt[i]);
1027 rte_prefetch0(secondary_bkt[i]);
1030 /* Compare signatures and prefetch key slot of first hit */
1031 for (i = 0; i < num_keys; i++) {
1032 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1033 primary_bkt[i], secondary_bkt[i],
1034 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1036 if (prim_hitmask[i]) {
1037 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1038 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1039 const struct rte_hash_key *key_slot =
1040 (const struct rte_hash_key *)(
1041 (const char *)h->key_store +
1042 key_idx * h->key_entry_size);
1043 rte_prefetch0(key_slot);
1047 if (sec_hitmask[i]) {
1048 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1049 uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1050 const struct rte_hash_key *key_slot =
1051 (const struct rte_hash_key *)(
1052 (const char *)h->key_store +
1053 key_idx * h->key_entry_size);
1054 rte_prefetch0(key_slot);
1058 /* Compare keys, first hits in primary first */
1059 for (i = 0; i < num_keys; i++) {
1060 positions[i] = -ENOENT;
1061 while (prim_hitmask[i]) {
1062 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1064 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1065 const struct rte_hash_key *key_slot =
1066 (const struct rte_hash_key *)(
1067 (const char *)h->key_store +
1068 key_idx * h->key_entry_size);
1070 * If key index is 0, do not compare key,
1071 * as it is checking the dummy slot
1073 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1075 data[i] = key_slot->pdata;
1078 positions[i] = key_idx - 1;
1081 prim_hitmask[i] &= ~(1 << (hit_index));
1084 while (sec_hitmask[i]) {
1085 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1087 uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1088 const struct rte_hash_key *key_slot =
1089 (const struct rte_hash_key *)(
1090 (const char *)h->key_store +
1091 key_idx * h->key_entry_size);
1093 * If key index is 0, do not compare key,
1094 * as it is checking the dummy slot
1097 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1099 data[i] = key_slot->pdata;
1102 positions[i] = key_idx - 1;
1105 sec_hitmask[i] &= ~(1 << (hit_index));
1112 if (hit_mask != NULL)
1117 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1118 uint32_t num_keys, int32_t *positions)
1120 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1121 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1122 (positions == NULL)), -EINVAL);
1124 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1129 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1130 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1132 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1133 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1134 (hit_mask == NULL)), -EINVAL);
1136 int32_t positions[num_keys];
1138 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1140 /* Return number of hits */
1141 return __builtin_popcountl(*hit_mask);
1145 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1147 uint32_t bucket_idx, idx, position;
1148 struct rte_hash_key *next_key;
1150 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1152 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1154 if (*next >= total_entries)
1157 /* Calculate bucket and index of current iterator */
1158 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1159 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1161 /* If current position is empty, go to the next one */
1162 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1165 if (*next == total_entries)
1167 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1168 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1171 /* Get position of entry in key table */
1172 position = h->buckets[bucket_idx].key_idx[idx];
1173 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1174 position * h->key_entry_size);
1175 /* Return key and data */
1176 *key = next_key->key;
1177 *data = next_key->pdata;
1179 /* Increment iterator */
1182 return position - 1;