1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_common.h>
13 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
15 #include <rte_memcpy.h>
16 #include <rte_prefetch.h>
17 #include <rte_branch_prediction.h>
18 #include <rte_malloc.h>
20 #include <rte_eal_memconfig.h>
21 #include <rte_per_lcore.h>
22 #include <rte_errno.h>
23 #include <rte_string_fns.h>
24 #include <rte_cpuflags.h>
25 #include <rte_rwlock.h>
26 #include <rte_spinlock.h>
28 #include <rte_compat.h>
29 #include <rte_pause.h>
32 #include "rte_cuckoo_hash.h"
34 #if defined(RTE_ARCH_X86)
35 #include "rte_cuckoo_hash_x86.h"
38 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
40 static struct rte_tailq_elem rte_hash_tailq = {
43 EAL_REGISTER_TAILQ(rte_hash_tailq)
46 rte_hash_find_existing(const char *name)
48 struct rte_hash *h = NULL;
49 struct rte_tailq_entry *te;
50 struct rte_hash_list *hash_list;
52 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
54 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
55 TAILQ_FOREACH(te, hash_list, next) {
56 h = (struct rte_hash *) te->data;
57 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
60 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
69 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
71 h->cmp_jump_table_idx = KEY_CUSTOM;
72 h->rte_hash_custom_cmp_eq = func;
76 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
78 if (h->cmp_jump_table_idx == KEY_CUSTOM)
79 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
81 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
85 rte_hash_create(const struct rte_hash_parameters *params)
87 struct rte_hash *h = NULL;
88 struct rte_tailq_entry *te = NULL;
89 struct rte_hash_list *hash_list;
90 struct rte_ring *r = NULL;
91 char hash_name[RTE_HASH_NAMESIZE];
94 char ring_name[RTE_RING_NAMESIZE];
95 unsigned num_key_slots;
96 unsigned hw_trans_mem_support = 0;
98 rte_hash_function default_hash_func = (rte_hash_function)rte_jhash;
100 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
102 if (params == NULL) {
103 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
107 /* Check for valid parameters */
108 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
109 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
110 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
111 (params->key_len == 0)) {
113 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
117 /* Check extra flags field to check extra options. */
118 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
119 hw_trans_mem_support = 1;
121 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
122 if (hw_trans_mem_support)
124 * Increase number of slots by total number of indices
125 * that can be stored in the lcore caches
126 * except for the first cache
128 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
129 LCORE_CACHE_SIZE + 1;
131 num_key_slots = params->entries + 1;
133 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
134 /* Create ring (Dummy slot index is not enqueued) */
135 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots - 1),
136 params->socket_id, 0);
138 RTE_LOG(ERR, HASH, "memory allocation failed\n");
142 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
144 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
146 /* guarantee there's no existing: this is normally already checked
147 * by ring creation above */
148 TAILQ_FOREACH(te, hash_list, next) {
149 h = (struct rte_hash *) te->data;
150 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
160 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
162 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
166 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
167 RTE_CACHE_LINE_SIZE, params->socket_id);
170 RTE_LOG(ERR, HASH, "memory allocation failed\n");
174 const uint32_t num_buckets = rte_align32pow2(params->entries)
175 / RTE_HASH_BUCKET_ENTRIES;
177 buckets = rte_zmalloc_socket(NULL,
178 num_buckets * sizeof(struct rte_hash_bucket),
179 RTE_CACHE_LINE_SIZE, params->socket_id);
181 if (buckets == NULL) {
182 RTE_LOG(ERR, HASH, "memory allocation failed\n");
186 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
187 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
189 k = rte_zmalloc_socket(NULL, key_tbl_size,
190 RTE_CACHE_LINE_SIZE, params->socket_id);
193 RTE_LOG(ERR, HASH, "memory allocation failed\n");
198 * If x86 architecture is used, select appropriate compare function,
199 * which may use x86 intrinsics, otherwise use memcmp
201 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
202 /* Select function to compare keys */
203 switch (params->key_len) {
205 h->cmp_jump_table_idx = KEY_16_BYTES;
208 h->cmp_jump_table_idx = KEY_32_BYTES;
211 h->cmp_jump_table_idx = KEY_48_BYTES;
214 h->cmp_jump_table_idx = KEY_64_BYTES;
217 h->cmp_jump_table_idx = KEY_80_BYTES;
220 h->cmp_jump_table_idx = KEY_96_BYTES;
223 h->cmp_jump_table_idx = KEY_112_BYTES;
226 h->cmp_jump_table_idx = KEY_128_BYTES;
229 /* If key is not multiple of 16, use generic memcmp */
230 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
233 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
236 if (hw_trans_mem_support) {
237 h->local_free_slots = rte_zmalloc_socket(NULL,
238 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
239 RTE_CACHE_LINE_SIZE, params->socket_id);
242 /* Default hash function */
243 #if defined(RTE_ARCH_X86)
244 default_hash_func = (rte_hash_function)rte_hash_crc;
245 #elif defined(RTE_ARCH_ARM64)
246 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
247 default_hash_func = (rte_hash_function)rte_hash_crc;
249 /* Setup hash context */
250 snprintf(h->name, sizeof(h->name), "%s", params->name);
251 h->entries = params->entries;
252 h->key_len = params->key_len;
253 h->key_entry_size = key_entry_size;
254 h->hash_func_init_val = params->hash_func_init_val;
256 h->num_buckets = num_buckets;
257 h->bucket_bitmask = h->num_buckets - 1;
258 h->buckets = buckets;
259 h->hash_func = (params->hash_func == NULL) ?
260 default_hash_func : params->hash_func;
263 h->hw_trans_mem_support = hw_trans_mem_support;
265 #if defined(RTE_ARCH_X86)
266 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
267 h->sig_cmp_fn = RTE_HASH_COMPARE_AVX2;
268 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
269 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
272 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
274 /* Turn on multi-writer only with explicit flat from user and TM
277 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
278 if (h->hw_trans_mem_support) {
279 h->add_key = ADD_KEY_MULTIWRITER_TM;
281 h->add_key = ADD_KEY_MULTIWRITER;
282 h->multiwriter_lock = rte_malloc(NULL,
283 sizeof(rte_spinlock_t),
285 rte_spinlock_init(h->multiwriter_lock);
288 h->add_key = ADD_KEY_SINGLEWRITER;
290 /* Populate free slots ring. Entry zero is reserved for key misses. */
291 for (i = 1; i < params->entries + 1; i++)
292 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
294 te->data = (void *) h;
295 TAILQ_INSERT_TAIL(hash_list, te, next);
296 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
300 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
311 rte_hash_free(struct rte_hash *h)
313 struct rte_tailq_entry *te;
314 struct rte_hash_list *hash_list;
319 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
321 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
323 /* find out tailq entry */
324 TAILQ_FOREACH(te, hash_list, next) {
325 if (te->data == (void *) h)
330 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
334 TAILQ_REMOVE(hash_list, te, next);
336 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
338 if (h->hw_trans_mem_support)
339 rte_free(h->local_free_slots);
341 if (h->add_key == ADD_KEY_MULTIWRITER)
342 rte_free(h->multiwriter_lock);
343 rte_ring_free(h->free_slots);
344 rte_free(h->key_store);
345 rte_free(h->buckets);
351 rte_hash_hash(const struct rte_hash *h, const void *key)
353 /* calc hash result by key */
354 return h->hash_func(key, h->key_len, h->hash_func_init_val);
357 /* Calc the secondary hash value from the primary hash value of a given key */
358 static inline hash_sig_t
359 rte_hash_secondary_hash(const hash_sig_t primary_hash)
361 static const unsigned all_bits_shift = 12;
362 static const unsigned alt_bits_xor = 0x5bd1e995;
364 uint32_t tag = primary_hash >> all_bits_shift;
366 return primary_hash ^ ((tag + 1) * alt_bits_xor);
370 rte_hash_reset(struct rte_hash *h)
378 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
379 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
381 /* clear the free ring */
382 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
385 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
386 for (i = 1; i < h->entries + 1; i++)
387 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
389 if (h->hw_trans_mem_support) {
390 /* Reset local caches per lcore */
391 for (i = 0; i < RTE_MAX_LCORE; i++)
392 h->local_free_slots[i].len = 0;
396 /* Search for an entry that can be pushed to its alternative location */
398 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt,
399 unsigned int *nr_pushes)
403 uint32_t next_bucket_idx;
404 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
407 * Push existing item (search for bucket with space in
408 * alternative locations) to its alternative location
410 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
411 /* Search for space in alternative locations */
412 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
413 next_bkt[i] = &h->buckets[next_bucket_idx];
414 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
415 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
419 if (j != RTE_HASH_BUCKET_ENTRIES)
423 /* Alternative location has spare room (end of recursive function) */
424 if (i != RTE_HASH_BUCKET_ENTRIES) {
425 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
426 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
427 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
431 /* Pick entry that has not been pushed yet */
432 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
433 if (bkt->flag[i] == 0)
436 /* All entries have been pushed, so entry cannot be added */
437 if (i == RTE_HASH_BUCKET_ENTRIES || ++(*nr_pushes) > RTE_HASH_MAX_PUSHES)
440 /* Set flag to indicate that this entry is going to be pushed */
443 /* Need room in alternative bucket to insert the pushed entry */
444 ret = make_space_bucket(h, next_bkt[i], nr_pushes);
446 * After recursive function.
447 * Clear flags and insert the pushed entry
448 * in its alternative location if successful,
453 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
454 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
455 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
463 * Function called to enqueue back an index in the cache/ring,
464 * as slot has not being used and it can be used in the
465 * next addition attempt.
468 enqueue_slot_back(const struct rte_hash *h,
469 struct lcore_cache *cached_free_slots,
472 if (h->hw_trans_mem_support) {
473 cached_free_slots->objs[cached_free_slots->len] = slot_id;
474 cached_free_slots->len++;
476 rte_ring_sp_enqueue(h->free_slots, slot_id);
479 static inline int32_t
480 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
481 hash_sig_t sig, void *data)
484 uint32_t prim_bucket_idx, sec_bucket_idx;
486 struct rte_hash_bucket *prim_bkt, *sec_bkt;
487 struct rte_hash_key *new_k, *k, *keys = h->key_store;
488 void *slot_id = NULL;
493 struct lcore_cache *cached_free_slots = NULL;
494 unsigned int nr_pushes = 0;
496 if (h->add_key == ADD_KEY_MULTIWRITER)
497 rte_spinlock_lock(h->multiwriter_lock);
499 prim_bucket_idx = sig & h->bucket_bitmask;
500 prim_bkt = &h->buckets[prim_bucket_idx];
501 rte_prefetch0(prim_bkt);
503 alt_hash = rte_hash_secondary_hash(sig);
504 sec_bucket_idx = alt_hash & h->bucket_bitmask;
505 sec_bkt = &h->buckets[sec_bucket_idx];
506 rte_prefetch0(sec_bkt);
508 /* Get a new slot for storing the new key */
509 if (h->hw_trans_mem_support) {
510 lcore_id = rte_lcore_id();
511 cached_free_slots = &h->local_free_slots[lcore_id];
512 /* Try to get a free slot from the local cache */
513 if (cached_free_slots->len == 0) {
514 /* Need to get another burst of free slots from global ring */
515 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
516 cached_free_slots->objs,
517 LCORE_CACHE_SIZE, NULL);
523 cached_free_slots->len += n_slots;
526 /* Get a free slot from the local cache */
527 cached_free_slots->len--;
528 slot_id = cached_free_slots->objs[cached_free_slots->len];
530 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0) {
536 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
537 rte_prefetch0(new_k);
538 new_idx = (uint32_t)((uintptr_t) slot_id);
540 /* Check if key is already inserted in primary location */
541 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
542 if (prim_bkt->sig_current[i] == sig &&
543 prim_bkt->sig_alt[i] == alt_hash) {
544 k = (struct rte_hash_key *) ((char *)keys +
545 prim_bkt->key_idx[i] * h->key_entry_size);
546 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
547 /* Enqueue index of free slot back in the ring. */
548 enqueue_slot_back(h, cached_free_slots, slot_id);
552 * Return index where key is stored,
553 * subtracting the first dummy index
555 return prim_bkt->key_idx[i] - 1;
560 /* Check if key is already inserted in secondary location */
561 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
562 if (sec_bkt->sig_alt[i] == sig &&
563 sec_bkt->sig_current[i] == alt_hash) {
564 k = (struct rte_hash_key *) ((char *)keys +
565 sec_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 sec_bkt->key_idx[i] - 1;
581 rte_memcpy(new_k->key, key, h->key_len);
584 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
585 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
586 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
587 sig, alt_hash, new_idx);
591 /* Primary bucket full, need to make space for new entry */
592 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
598 /* Also search secondary bucket to get better occupancy */
599 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
606 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
607 /* Check if slot is available */
608 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
609 prim_bkt->sig_current[i] = sig;
610 prim_bkt->sig_alt[i] = alt_hash;
611 prim_bkt->key_idx[i] = new_idx;
616 if (i != RTE_HASH_BUCKET_ENTRIES) {
617 if (h->add_key == ADD_KEY_MULTIWRITER)
618 rte_spinlock_unlock(h->multiwriter_lock);
622 /* Primary bucket full, need to make space for new entry
623 * After recursive function.
624 * Insert the new entry in the position of the pushed entry
625 * if successful or return error and
626 * store the new slot back in the ring
628 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
630 prim_bkt->sig_current[ret] = sig;
631 prim_bkt->sig_alt[ret] = alt_hash;
632 prim_bkt->key_idx[ret] = new_idx;
633 if (h->add_key == ADD_KEY_MULTIWRITER)
634 rte_spinlock_unlock(h->multiwriter_lock);
637 #if defined(RTE_ARCH_X86)
640 /* Error in addition, store new slot back in the ring and return error */
641 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
644 if (h->add_key == ADD_KEY_MULTIWRITER)
645 rte_spinlock_unlock(h->multiwriter_lock);
650 rte_hash_add_key_with_hash(const struct rte_hash *h,
651 const void *key, hash_sig_t sig)
653 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
654 return __rte_hash_add_key_with_hash(h, key, sig, 0);
658 rte_hash_add_key(const struct rte_hash *h, const void *key)
660 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
661 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
665 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
666 const void *key, hash_sig_t sig, void *data)
670 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
671 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
679 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
683 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
685 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
691 static inline int32_t
692 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
693 hash_sig_t sig, void **data)
698 struct rte_hash_bucket *bkt;
699 struct rte_hash_key *k, *keys = h->key_store;
701 bucket_idx = sig & h->bucket_bitmask;
702 bkt = &h->buckets[bucket_idx];
704 /* Check if key is in primary location */
705 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
706 if (bkt->sig_current[i] == sig &&
707 bkt->key_idx[i] != EMPTY_SLOT) {
708 k = (struct rte_hash_key *) ((char *)keys +
709 bkt->key_idx[i] * h->key_entry_size);
710 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
714 * Return index where key is stored,
715 * subtracting the first dummy index
717 return bkt->key_idx[i] - 1;
722 /* Calculate secondary hash */
723 alt_hash = rte_hash_secondary_hash(sig);
724 bucket_idx = alt_hash & h->bucket_bitmask;
725 bkt = &h->buckets[bucket_idx];
727 /* Check if key is in secondary location */
728 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
729 if (bkt->sig_current[i] == alt_hash &&
730 bkt->sig_alt[i] == sig) {
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;
749 rte_hash_lookup_with_hash(const struct rte_hash *h,
750 const void *key, hash_sig_t sig)
752 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
753 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
757 rte_hash_lookup(const struct rte_hash *h, const void *key)
759 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
760 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
764 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
765 const void *key, hash_sig_t sig, void **data)
767 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
768 return __rte_hash_lookup_with_hash(h, key, sig, data);
772 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
774 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
775 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
779 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
781 unsigned lcore_id, n_slots;
782 struct lcore_cache *cached_free_slots;
784 bkt->sig_current[i] = NULL_SIGNATURE;
785 bkt->sig_alt[i] = NULL_SIGNATURE;
786 if (h->hw_trans_mem_support) {
787 lcore_id = rte_lcore_id();
788 cached_free_slots = &h->local_free_slots[lcore_id];
789 /* Cache full, need to free it. */
790 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
791 /* Need to enqueue the free slots in global ring. */
792 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
793 cached_free_slots->objs,
794 LCORE_CACHE_SIZE, NULL);
795 cached_free_slots->len -= n_slots;
797 /* Put index of new free slot in cache. */
798 cached_free_slots->objs[cached_free_slots->len] =
799 (void *)((uintptr_t)bkt->key_idx[i]);
800 cached_free_slots->len++;
802 rte_ring_sp_enqueue(h->free_slots,
803 (void *)((uintptr_t)bkt->key_idx[i]));
807 static inline int32_t
808 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
814 struct rte_hash_bucket *bkt;
815 struct rte_hash_key *k, *keys = h->key_store;
818 bucket_idx = sig & h->bucket_bitmask;
819 bkt = &h->buckets[bucket_idx];
821 /* Check if key is in primary location */
822 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
823 if (bkt->sig_current[i] == sig &&
824 bkt->key_idx[i] != EMPTY_SLOT) {
825 k = (struct rte_hash_key *) ((char *)keys +
826 bkt->key_idx[i] * h->key_entry_size);
827 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
828 remove_entry(h, bkt, i);
831 * Return index where key is stored,
832 * subtracting the first dummy index
834 ret = bkt->key_idx[i] - 1;
835 bkt->key_idx[i] = EMPTY_SLOT;
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->sig_current[i] == alt_hash &&
849 bkt->key_idx[i] != EMPTY_SLOT) {
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 * subtracting the first dummy index
859 ret = bkt->key_idx[i] - 1;
860 bkt->key_idx[i] = EMPTY_SLOT;
870 rte_hash_del_key_with_hash(const struct rte_hash *h,
871 const void *key, hash_sig_t sig)
873 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
874 return __rte_hash_del_key_with_hash(h, key, sig);
878 rte_hash_del_key(const struct rte_hash *h, const void *key)
880 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
881 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
885 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
888 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
890 struct rte_hash_key *k, *keys = h->key_store;
891 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
896 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
905 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
906 const struct rte_hash_bucket *prim_bkt,
907 const struct rte_hash_bucket *sec_bkt,
908 hash_sig_t prim_hash, hash_sig_t sec_hash,
909 enum rte_hash_sig_compare_function sig_cmp_fn)
913 switch (sig_cmp_fn) {
914 #ifdef RTE_MACHINE_CPUFLAG_AVX2
915 case RTE_HASH_COMPARE_AVX2:
916 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
918 (__m256i const *)prim_bkt->sig_current),
919 _mm256_set1_epi32(prim_hash)));
920 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
922 (__m256i const *)sec_bkt->sig_current),
923 _mm256_set1_epi32(sec_hash)));
926 #ifdef RTE_MACHINE_CPUFLAG_SSE2
927 case RTE_HASH_COMPARE_SSE:
928 /* Compare the first 4 signatures in the bucket */
929 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
931 (__m128i const *)prim_bkt->sig_current),
932 _mm_set1_epi32(prim_hash)));
933 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
935 (__m128i const *)&prim_bkt->sig_current[4]),
936 _mm_set1_epi32(prim_hash)))) << 4;
937 /* Compare the first 4 signatures in the bucket */
938 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
940 (__m128i const *)sec_bkt->sig_current),
941 _mm_set1_epi32(sec_hash)));
942 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
944 (__m128i const *)&sec_bkt->sig_current[4]),
945 _mm_set1_epi32(sec_hash)))) << 4;
949 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
950 *prim_hash_matches |=
951 ((prim_hash == prim_bkt->sig_current[i]) << i);
953 ((sec_hash == sec_bkt->sig_current[i]) << i);
959 #define PREFETCH_OFFSET 4
961 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
962 int32_t num_keys, int32_t *positions,
963 uint64_t *hit_mask, void *data[])
967 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
968 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
969 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
970 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
971 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
972 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
974 /* Prefetch first keys */
975 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
976 rte_prefetch0(keys[i]);
979 * Prefetch rest of the keys, calculate primary and
980 * secondary bucket and prefetch them
982 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
983 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
985 prim_hash[i] = rte_hash_hash(h, keys[i]);
986 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
988 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
989 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
991 rte_prefetch0(primary_bkt[i]);
992 rte_prefetch0(secondary_bkt[i]);
995 /* Calculate and prefetch rest of the buckets */
996 for (; i < num_keys; i++) {
997 prim_hash[i] = rte_hash_hash(h, keys[i]);
998 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1000 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1001 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1003 rte_prefetch0(primary_bkt[i]);
1004 rte_prefetch0(secondary_bkt[i]);
1007 /* Compare signatures and prefetch key slot of first hit */
1008 for (i = 0; i < num_keys; i++) {
1009 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1010 primary_bkt[i], secondary_bkt[i],
1011 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1013 if (prim_hitmask[i]) {
1014 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1015 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1016 const struct rte_hash_key *key_slot =
1017 (const struct rte_hash_key *)(
1018 (const char *)h->key_store +
1019 key_idx * h->key_entry_size);
1020 rte_prefetch0(key_slot);
1024 if (sec_hitmask[i]) {
1025 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1026 uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1027 const struct rte_hash_key *key_slot =
1028 (const struct rte_hash_key *)(
1029 (const char *)h->key_store +
1030 key_idx * h->key_entry_size);
1031 rte_prefetch0(key_slot);
1035 /* Compare keys, first hits in primary first */
1036 for (i = 0; i < num_keys; i++) {
1037 positions[i] = -ENOENT;
1038 while (prim_hitmask[i]) {
1039 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1041 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1042 const struct rte_hash_key *key_slot =
1043 (const struct rte_hash_key *)(
1044 (const char *)h->key_store +
1045 key_idx * h->key_entry_size);
1047 * If key index is 0, do not compare key,
1048 * as it is checking the dummy slot
1050 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1052 data[i] = key_slot->pdata;
1055 positions[i] = key_idx - 1;
1058 prim_hitmask[i] &= ~(1 << (hit_index));
1061 while (sec_hitmask[i]) {
1062 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1064 uint32_t key_idx = secondary_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
1074 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1076 data[i] = key_slot->pdata;
1079 positions[i] = key_idx - 1;
1082 sec_hitmask[i] &= ~(1 << (hit_index));
1089 if (hit_mask != NULL)
1094 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1095 uint32_t num_keys, int32_t *positions)
1097 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1098 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1099 (positions == NULL)), -EINVAL);
1101 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1106 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1107 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1109 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1110 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1111 (hit_mask == NULL)), -EINVAL);
1113 int32_t positions[num_keys];
1115 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1117 /* Return number of hits */
1118 return __builtin_popcountl(*hit_mask);
1122 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1124 uint32_t bucket_idx, idx, position;
1125 struct rte_hash_key *next_key;
1127 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1129 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1131 if (*next >= total_entries)
1134 /* Calculate bucket and index of current iterator */
1135 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1136 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1138 /* If current position is empty, go to the next one */
1139 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1142 if (*next == total_entries)
1144 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1145 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1148 /* Get position of entry in key table */
1149 position = h->buckets[bucket_idx].key_idx[idx];
1150 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1151 position * h->key_entry_size);
1152 /* Return key and data */
1153 *key = next_key->key;
1154 *data = next_key->pdata;
1156 /* Increment iterator */
1159 return position - 1;