1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
3 * Copyright(c) 2018 Arm Limited
11 #include <sys/queue.h>
13 #include <rte_common.h>
14 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
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>
30 #include <rte_tailq.h>
33 #include "rte_cuckoo_hash.h"
35 #define FOR_EACH_BUCKET(CURRENT_BKT, START_BUCKET) \
36 for (CURRENT_BKT = START_BUCKET; \
37 CURRENT_BKT != NULL; \
38 CURRENT_BKT = CURRENT_BKT->next)
40 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
42 static struct rte_tailq_elem rte_hash_tailq = {
45 EAL_REGISTER_TAILQ(rte_hash_tailq)
48 rte_hash_find_existing(const char *name)
50 struct rte_hash *h = NULL;
51 struct rte_tailq_entry *te;
52 struct rte_hash_list *hash_list;
54 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
56 rte_mcfg_tailq_read_lock();
57 TAILQ_FOREACH(te, hash_list, next) {
58 h = (struct rte_hash *) te->data;
59 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
62 rte_mcfg_tailq_read_unlock();
71 static inline struct rte_hash_bucket *
72 rte_hash_get_last_bkt(struct rte_hash_bucket *lst_bkt)
74 while (lst_bkt->next != NULL)
75 lst_bkt = lst_bkt->next;
79 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
81 h->cmp_jump_table_idx = KEY_CUSTOM;
82 h->rte_hash_custom_cmp_eq = func;
86 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
88 if (h->cmp_jump_table_idx == KEY_CUSTOM)
89 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
91 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
95 * We use higher 16 bits of hash as the signature value stored in table.
96 * We use the lower bits for the primary bucket
97 * location. Then we XOR primary bucket location and the signature
98 * to get the secondary bucket location. This is same as
99 * proposed in Bin Fan, et al's paper
100 * "MemC3: Compact and Concurrent MemCache with Dumber Caching and
101 * Smarter Hashing". The benefit to use
102 * XOR is that one could derive the alternative bucket location
103 * by only using the current bucket location and the signature.
105 static inline uint16_t
106 get_short_sig(const hash_sig_t hash)
111 static inline uint32_t
112 get_prim_bucket_index(const struct rte_hash *h, const hash_sig_t hash)
114 return hash & h->bucket_bitmask;
117 static inline uint32_t
118 get_alt_bucket_index(const struct rte_hash *h,
119 uint32_t cur_bkt_idx, uint16_t sig)
121 return (cur_bkt_idx ^ sig) & h->bucket_bitmask;
125 rte_hash_create(const struct rte_hash_parameters *params)
127 struct rte_hash *h = NULL;
128 struct rte_tailq_entry *te = NULL;
129 struct rte_hash_list *hash_list;
130 struct rte_ring *r = NULL;
131 struct rte_ring *r_ext = NULL;
132 char hash_name[RTE_HASH_NAMESIZE];
134 void *buckets = NULL;
135 void *buckets_ext = NULL;
136 char ring_name[RTE_RING_NAMESIZE];
137 char ext_ring_name[RTE_RING_NAMESIZE];
138 unsigned num_key_slots;
140 unsigned int hw_trans_mem_support = 0, use_local_cache = 0;
141 unsigned int ext_table_support = 0;
142 unsigned int readwrite_concur_support = 0;
143 unsigned int writer_takes_lock = 0;
144 unsigned int no_free_on_del = 0;
145 uint32_t *ext_bkt_to_free = NULL;
146 uint32_t *tbl_chng_cnt = NULL;
147 unsigned int readwrite_concur_lf_support = 0;
149 rte_hash_function default_hash_func = (rte_hash_function)rte_jhash;
151 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
153 if (params == NULL) {
154 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
158 /* Check for valid parameters */
159 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
160 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
161 (params->key_len == 0)) {
163 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
167 /* Validate correct usage of extra options */
168 if ((params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) &&
169 (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF)) {
171 RTE_LOG(ERR, HASH, "rte_hash_create: choose rw concurrency or "
172 "rw concurrency lock free\n");
176 /* Check extra flags field to check extra options. */
177 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
178 hw_trans_mem_support = 1;
180 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
182 writer_takes_lock = 1;
185 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) {
186 readwrite_concur_support = 1;
187 writer_takes_lock = 1;
190 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_EXT_TABLE)
191 ext_table_support = 1;
193 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL)
196 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF) {
197 readwrite_concur_lf_support = 1;
198 /* Enable not freeing internal memory/index on delete */
202 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
205 * Increase number of slots by total number of indices
206 * that can be stored in the lcore caches
207 * except for the first cache
209 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
210 (LCORE_CACHE_SIZE - 1) + 1;
212 num_key_slots = params->entries + 1;
214 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
215 /* Create ring (Dummy slot index is not enqueued) */
216 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
217 params->socket_id, 0);
219 RTE_LOG(ERR, HASH, "memory allocation failed\n");
223 const uint32_t num_buckets = rte_align32pow2(params->entries) /
224 RTE_HASH_BUCKET_ENTRIES;
226 /* Create ring for extendable buckets. */
227 if (ext_table_support) {
228 snprintf(ext_ring_name, sizeof(ext_ring_name), "HT_EXT_%s",
230 r_ext = rte_ring_create(ext_ring_name,
231 rte_align32pow2(num_buckets + 1),
232 params->socket_id, 0);
235 RTE_LOG(ERR, HASH, "ext buckets memory allocation "
241 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
243 rte_mcfg_tailq_write_lock();
245 /* guarantee there's no existing: this is normally already checked
246 * by ring creation above */
247 TAILQ_FOREACH(te, hash_list, next) {
248 h = (struct rte_hash *) te->data;
249 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
259 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
261 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
265 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
266 RTE_CACHE_LINE_SIZE, params->socket_id);
269 RTE_LOG(ERR, HASH, "memory allocation failed\n");
273 buckets = rte_zmalloc_socket(NULL,
274 num_buckets * sizeof(struct rte_hash_bucket),
275 RTE_CACHE_LINE_SIZE, params->socket_id);
277 if (buckets == NULL) {
278 RTE_LOG(ERR, HASH, "buckets memory allocation failed\n");
282 /* Allocate same number of extendable buckets */
283 if (ext_table_support) {
284 buckets_ext = rte_zmalloc_socket(NULL,
285 num_buckets * sizeof(struct rte_hash_bucket),
286 RTE_CACHE_LINE_SIZE, params->socket_id);
287 if (buckets_ext == NULL) {
288 RTE_LOG(ERR, HASH, "ext buckets memory allocation "
292 /* Populate ext bkt ring. We reserve 0 similar to the
293 * key-data slot, just in case in future we want to
294 * use bucket index for the linked list and 0 means NULL
297 for (i = 1; i <= num_buckets; i++)
298 rte_ring_sp_enqueue(r_ext, (void *)((uintptr_t) i));
300 if (readwrite_concur_lf_support) {
301 ext_bkt_to_free = rte_zmalloc(NULL, sizeof(uint32_t) *
303 if (ext_bkt_to_free == NULL) {
304 RTE_LOG(ERR, HASH, "ext bkt to free memory allocation "
311 const uint32_t key_entry_size =
312 RTE_ALIGN(sizeof(struct rte_hash_key) + params->key_len,
314 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
316 k = rte_zmalloc_socket(NULL, key_tbl_size,
317 RTE_CACHE_LINE_SIZE, params->socket_id);
320 RTE_LOG(ERR, HASH, "memory allocation failed\n");
324 tbl_chng_cnt = rte_zmalloc_socket(NULL, sizeof(uint32_t),
325 RTE_CACHE_LINE_SIZE, params->socket_id);
327 if (tbl_chng_cnt == NULL) {
328 RTE_LOG(ERR, HASH, "memory allocation failed\n");
333 * If x86 architecture is used, select appropriate compare function,
334 * which may use x86 intrinsics, otherwise use memcmp
336 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
337 /* Select function to compare keys */
338 switch (params->key_len) {
340 h->cmp_jump_table_idx = KEY_16_BYTES;
343 h->cmp_jump_table_idx = KEY_32_BYTES;
346 h->cmp_jump_table_idx = KEY_48_BYTES;
349 h->cmp_jump_table_idx = KEY_64_BYTES;
352 h->cmp_jump_table_idx = KEY_80_BYTES;
355 h->cmp_jump_table_idx = KEY_96_BYTES;
358 h->cmp_jump_table_idx = KEY_112_BYTES;
361 h->cmp_jump_table_idx = KEY_128_BYTES;
364 /* If key is not multiple of 16, use generic memcmp */
365 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
368 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
371 if (use_local_cache) {
372 h->local_free_slots = rte_zmalloc_socket(NULL,
373 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
374 RTE_CACHE_LINE_SIZE, params->socket_id);
377 /* Default hash function */
378 #if defined(RTE_ARCH_X86)
379 default_hash_func = (rte_hash_function)rte_hash_crc;
380 #elif defined(RTE_ARCH_ARM64)
381 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
382 default_hash_func = (rte_hash_function)rte_hash_crc;
384 /* Setup hash context */
385 strlcpy(h->name, params->name, sizeof(h->name));
386 h->entries = params->entries;
387 h->key_len = params->key_len;
388 h->key_entry_size = key_entry_size;
389 h->hash_func_init_val = params->hash_func_init_val;
391 h->num_buckets = num_buckets;
392 h->bucket_bitmask = h->num_buckets - 1;
393 h->buckets = buckets;
394 h->buckets_ext = buckets_ext;
395 h->free_ext_bkts = r_ext;
396 h->hash_func = (params->hash_func == NULL) ?
397 default_hash_func : params->hash_func;
400 h->ext_bkt_to_free = ext_bkt_to_free;
401 h->tbl_chng_cnt = tbl_chng_cnt;
402 *h->tbl_chng_cnt = 0;
403 h->hw_trans_mem_support = hw_trans_mem_support;
404 h->use_local_cache = use_local_cache;
405 h->readwrite_concur_support = readwrite_concur_support;
406 h->ext_table_support = ext_table_support;
407 h->writer_takes_lock = writer_takes_lock;
408 h->no_free_on_del = no_free_on_del;
409 h->readwrite_concur_lf_support = readwrite_concur_lf_support;
411 #if defined(RTE_ARCH_X86)
412 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
413 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
415 #elif defined(RTE_ARCH_ARM64)
416 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
417 h->sig_cmp_fn = RTE_HASH_COMPARE_NEON;
420 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
422 /* Writer threads need to take the lock when:
423 * 1) RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY is enabled OR
424 * 2) RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD is enabled
426 if (h->writer_takes_lock) {
427 h->readwrite_lock = rte_malloc(NULL, sizeof(rte_rwlock_t),
428 RTE_CACHE_LINE_SIZE);
429 if (h->readwrite_lock == NULL)
432 rte_rwlock_init(h->readwrite_lock);
435 /* Populate free slots ring. Entry zero is reserved for key misses. */
436 for (i = 1; i < num_key_slots; i++)
437 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
439 te->data = (void *) h;
440 TAILQ_INSERT_TAIL(hash_list, te, next);
441 rte_mcfg_tailq_write_unlock();
445 rte_mcfg_tailq_write_unlock();
448 rte_ring_free(r_ext);
452 rte_free(buckets_ext);
454 rte_free(tbl_chng_cnt);
455 rte_free(ext_bkt_to_free);
460 rte_hash_free(struct rte_hash *h)
462 struct rte_tailq_entry *te;
463 struct rte_hash_list *hash_list;
468 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
470 rte_mcfg_tailq_write_lock();
472 /* find out tailq entry */
473 TAILQ_FOREACH(te, hash_list, next) {
474 if (te->data == (void *) h)
479 rte_mcfg_tailq_write_unlock();
483 TAILQ_REMOVE(hash_list, te, next);
485 rte_mcfg_tailq_write_unlock();
487 if (h->use_local_cache)
488 rte_free(h->local_free_slots);
489 if (h->writer_takes_lock)
490 rte_free(h->readwrite_lock);
491 rte_ring_free(h->free_slots);
492 rte_ring_free(h->free_ext_bkts);
493 rte_free(h->key_store);
494 rte_free(h->buckets);
495 rte_free(h->buckets_ext);
496 rte_free(h->tbl_chng_cnt);
497 rte_free(h->ext_bkt_to_free);
503 rte_hash_hash(const struct rte_hash *h, const void *key)
505 /* calc hash result by key */
506 return h->hash_func(key, h->key_len, h->hash_func_init_val);
510 rte_hash_count(const struct rte_hash *h)
512 uint32_t tot_ring_cnt, cached_cnt = 0;
518 if (h->use_local_cache) {
519 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
520 (LCORE_CACHE_SIZE - 1);
521 for (i = 0; i < RTE_MAX_LCORE; i++)
522 cached_cnt += h->local_free_slots[i].len;
524 ret = tot_ring_cnt - rte_ring_count(h->free_slots) -
527 tot_ring_cnt = h->entries;
528 ret = tot_ring_cnt - rte_ring_count(h->free_slots);
533 /* Read write locks implemented using rte_rwlock */
535 __hash_rw_writer_lock(const struct rte_hash *h)
537 if (h->writer_takes_lock && h->hw_trans_mem_support)
538 rte_rwlock_write_lock_tm(h->readwrite_lock);
539 else if (h->writer_takes_lock)
540 rte_rwlock_write_lock(h->readwrite_lock);
544 __hash_rw_reader_lock(const struct rte_hash *h)
546 if (h->readwrite_concur_support && h->hw_trans_mem_support)
547 rte_rwlock_read_lock_tm(h->readwrite_lock);
548 else if (h->readwrite_concur_support)
549 rte_rwlock_read_lock(h->readwrite_lock);
553 __hash_rw_writer_unlock(const struct rte_hash *h)
555 if (h->writer_takes_lock && h->hw_trans_mem_support)
556 rte_rwlock_write_unlock_tm(h->readwrite_lock);
557 else if (h->writer_takes_lock)
558 rte_rwlock_write_unlock(h->readwrite_lock);
562 __hash_rw_reader_unlock(const struct rte_hash *h)
564 if (h->readwrite_concur_support && h->hw_trans_mem_support)
565 rte_rwlock_read_unlock_tm(h->readwrite_lock);
566 else if (h->readwrite_concur_support)
567 rte_rwlock_read_unlock(h->readwrite_lock);
571 rte_hash_reset(struct rte_hash *h)
574 uint32_t tot_ring_cnt, i;
579 __hash_rw_writer_lock(h);
580 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
581 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
582 *h->tbl_chng_cnt = 0;
584 /* clear the free ring */
585 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
588 /* clear free extendable bucket ring and memory */
589 if (h->ext_table_support) {
590 memset(h->buckets_ext, 0, h->num_buckets *
591 sizeof(struct rte_hash_bucket));
592 while (rte_ring_dequeue(h->free_ext_bkts, &ptr) == 0)
596 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
597 if (h->use_local_cache)
598 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
599 (LCORE_CACHE_SIZE - 1);
601 tot_ring_cnt = h->entries;
603 for (i = 1; i < tot_ring_cnt + 1; i++)
604 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
606 /* Repopulate the free ext bkt ring. */
607 if (h->ext_table_support) {
608 for (i = 1; i <= h->num_buckets; i++)
609 rte_ring_sp_enqueue(h->free_ext_bkts,
610 (void *)((uintptr_t) i));
613 if (h->use_local_cache) {
614 /* Reset local caches per lcore */
615 for (i = 0; i < RTE_MAX_LCORE; i++)
616 h->local_free_slots[i].len = 0;
618 __hash_rw_writer_unlock(h);
622 * Function called to enqueue back an index in the cache/ring,
623 * as slot has not being used and it can be used in the
624 * next addition attempt.
627 enqueue_slot_back(const struct rte_hash *h,
628 struct lcore_cache *cached_free_slots,
631 if (h->use_local_cache) {
632 cached_free_slots->objs[cached_free_slots->len] = slot_id;
633 cached_free_slots->len++;
635 rte_ring_sp_enqueue(h->free_slots, slot_id);
638 /* Search a key from bucket and update its data.
639 * Writer holds the lock before calling this.
641 static inline int32_t
642 search_and_update(const struct rte_hash *h, void *data, const void *key,
643 struct rte_hash_bucket *bkt, uint16_t sig)
646 struct rte_hash_key *k, *keys = h->key_store;
648 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
649 if (bkt->sig_current[i] == sig) {
650 k = (struct rte_hash_key *) ((char *)keys +
651 bkt->key_idx[i] * h->key_entry_size);
652 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
653 /* 'pdata' acts as the synchronization point
654 * when an existing hash entry is updated.
655 * Key is not updated in this case.
657 __atomic_store_n(&k->pdata,
661 * Return index where key is stored,
662 * subtracting the first dummy index
664 return bkt->key_idx[i] - 1;
671 /* Only tries to insert at one bucket (@prim_bkt) without trying to push
673 * return 1 if matching existing key, return 0 if succeeds, return -1 for no
676 static inline int32_t
677 rte_hash_cuckoo_insert_mw(const struct rte_hash *h,
678 struct rte_hash_bucket *prim_bkt,
679 struct rte_hash_bucket *sec_bkt,
680 const struct rte_hash_key *key, void *data,
681 uint16_t sig, uint32_t new_idx,
685 struct rte_hash_bucket *cur_bkt;
688 __hash_rw_writer_lock(h);
689 /* Check if key was inserted after last check but before this
690 * protected region in case of inserting duplicated keys.
692 ret = search_and_update(h, data, key, prim_bkt, sig);
694 __hash_rw_writer_unlock(h);
699 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
700 ret = search_and_update(h, data, key, cur_bkt, sig);
702 __hash_rw_writer_unlock(h);
708 /* Insert new entry if there is room in the primary
711 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
712 /* Check if slot is available */
713 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
714 prim_bkt->sig_current[i] = sig;
715 /* Key can be of arbitrary length, so it is
716 * not possible to store it atomically.
717 * Hence the new key element's memory stores
718 * (key as well as data) should be complete
719 * before it is referenced.
721 __atomic_store_n(&prim_bkt->key_idx[i],
727 __hash_rw_writer_unlock(h);
729 if (i != RTE_HASH_BUCKET_ENTRIES)
736 /* Shift buckets along provided cuckoo_path (@leaf and @leaf_slot) and fill
737 * the path head with new entry (sig, alt_hash, new_idx)
738 * return 1 if matched key found, return -1 if cuckoo path invalided and fail,
739 * return 0 if succeeds.
742 rte_hash_cuckoo_move_insert_mw(const struct rte_hash *h,
743 struct rte_hash_bucket *bkt,
744 struct rte_hash_bucket *alt_bkt,
745 const struct rte_hash_key *key, void *data,
746 struct queue_node *leaf, uint32_t leaf_slot,
747 uint16_t sig, uint32_t new_idx,
750 uint32_t prev_alt_bkt_idx;
751 struct rte_hash_bucket *cur_bkt;
752 struct queue_node *prev_node, *curr_node = leaf;
753 struct rte_hash_bucket *prev_bkt, *curr_bkt = leaf->bkt;
754 uint32_t prev_slot, curr_slot = leaf_slot;
757 __hash_rw_writer_lock(h);
759 /* In case empty slot was gone before entering protected region */
760 if (curr_bkt->key_idx[curr_slot] != EMPTY_SLOT) {
761 __hash_rw_writer_unlock(h);
765 /* Check if key was inserted after last check but before this
768 ret = search_and_update(h, data, key, bkt, sig);
770 __hash_rw_writer_unlock(h);
775 FOR_EACH_BUCKET(cur_bkt, alt_bkt) {
776 ret = search_and_update(h, data, key, cur_bkt, sig);
778 __hash_rw_writer_unlock(h);
784 while (likely(curr_node->prev != NULL)) {
785 prev_node = curr_node->prev;
786 prev_bkt = prev_node->bkt;
787 prev_slot = curr_node->prev_slot;
789 prev_alt_bkt_idx = get_alt_bucket_index(h,
790 prev_node->cur_bkt_idx,
791 prev_bkt->sig_current[prev_slot]);
793 if (unlikely(&h->buckets[prev_alt_bkt_idx]
795 /* revert it to empty, otherwise duplicated keys */
796 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
799 __hash_rw_writer_unlock(h);
803 if (h->readwrite_concur_lf_support) {
804 /* Inform the previous move. The current move need
805 * not be informed now as the current bucket entry
806 * is present in both primary and secondary.
807 * Since there is one writer, load acquires on
808 * tbl_chng_cnt are not required.
810 __atomic_store_n(h->tbl_chng_cnt,
811 *h->tbl_chng_cnt + 1,
813 /* The store to sig_current should not
814 * move above the store to tbl_chng_cnt.
816 __atomic_thread_fence(__ATOMIC_RELEASE);
819 /* Need to swap current/alt sig to allow later
820 * Cuckoo insert to move elements back to its
821 * primary bucket if available
823 curr_bkt->sig_current[curr_slot] =
824 prev_bkt->sig_current[prev_slot];
825 /* Release the updated bucket entry */
826 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
827 prev_bkt->key_idx[prev_slot],
830 curr_slot = prev_slot;
831 curr_node = prev_node;
832 curr_bkt = curr_node->bkt;
835 if (h->readwrite_concur_lf_support) {
836 /* Inform the previous move. The current move need
837 * not be informed now as the current bucket entry
838 * is present in both primary and secondary.
839 * Since there is one writer, load acquires on
840 * tbl_chng_cnt are not required.
842 __atomic_store_n(h->tbl_chng_cnt,
843 *h->tbl_chng_cnt + 1,
845 /* The store to sig_current should not
846 * move above the store to tbl_chng_cnt.
848 __atomic_thread_fence(__ATOMIC_RELEASE);
851 curr_bkt->sig_current[curr_slot] = sig;
852 /* Release the new bucket entry */
853 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
857 __hash_rw_writer_unlock(h);
864 * Make space for new key, using bfs Cuckoo Search and Multi-Writer safe
868 rte_hash_cuckoo_make_space_mw(const struct rte_hash *h,
869 struct rte_hash_bucket *bkt,
870 struct rte_hash_bucket *sec_bkt,
871 const struct rte_hash_key *key, void *data,
872 uint16_t sig, uint32_t bucket_idx,
873 uint32_t new_idx, int32_t *ret_val)
876 struct queue_node queue[RTE_HASH_BFS_QUEUE_MAX_LEN];
877 struct queue_node *tail, *head;
878 struct rte_hash_bucket *curr_bkt, *alt_bkt;
879 uint32_t cur_idx, alt_idx;
885 tail->prev_slot = -1;
886 tail->cur_bkt_idx = bucket_idx;
888 /* Cuckoo bfs Search */
889 while (likely(tail != head && head <
890 queue + RTE_HASH_BFS_QUEUE_MAX_LEN -
891 RTE_HASH_BUCKET_ENTRIES)) {
892 curr_bkt = tail->bkt;
893 cur_idx = tail->cur_bkt_idx;
894 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
895 if (curr_bkt->key_idx[i] == EMPTY_SLOT) {
896 int32_t ret = rte_hash_cuckoo_move_insert_mw(h,
897 bkt, sec_bkt, key, data,
900 if (likely(ret != -1))
904 /* Enqueue new node and keep prev node info */
905 alt_idx = get_alt_bucket_index(h, cur_idx,
906 curr_bkt->sig_current[i]);
907 alt_bkt = &(h->buckets[alt_idx]);
909 head->cur_bkt_idx = alt_idx;
920 static inline int32_t
921 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
922 hash_sig_t sig, void *data)
925 uint32_t prim_bucket_idx, sec_bucket_idx;
926 struct rte_hash_bucket *prim_bkt, *sec_bkt, *cur_bkt;
927 struct rte_hash_key *new_k, *keys = h->key_store;
928 void *slot_id = NULL;
929 void *ext_bkt_id = NULL;
930 uint32_t new_idx, bkt_id;
935 struct lcore_cache *cached_free_slots = NULL;
937 struct rte_hash_bucket *last;
939 short_sig = get_short_sig(sig);
940 prim_bucket_idx = get_prim_bucket_index(h, sig);
941 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
942 prim_bkt = &h->buckets[prim_bucket_idx];
943 sec_bkt = &h->buckets[sec_bucket_idx];
944 rte_prefetch0(prim_bkt);
945 rte_prefetch0(sec_bkt);
947 /* Check if key is already inserted in primary location */
948 __hash_rw_writer_lock(h);
949 ret = search_and_update(h, data, key, prim_bkt, short_sig);
951 __hash_rw_writer_unlock(h);
955 /* Check if key is already inserted in secondary location */
956 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
957 ret = search_and_update(h, data, key, cur_bkt, short_sig);
959 __hash_rw_writer_unlock(h);
964 __hash_rw_writer_unlock(h);
966 /* Did not find a match, so get a new slot for storing the new key */
967 if (h->use_local_cache) {
968 lcore_id = rte_lcore_id();
969 cached_free_slots = &h->local_free_slots[lcore_id];
970 /* Try to get a free slot from the local cache */
971 if (cached_free_slots->len == 0) {
972 /* Need to get another burst of free slots from global ring */
973 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
974 cached_free_slots->objs,
975 LCORE_CACHE_SIZE, NULL);
980 cached_free_slots->len += n_slots;
983 /* Get a free slot from the local cache */
984 cached_free_slots->len--;
985 slot_id = cached_free_slots->objs[cached_free_slots->len];
987 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0) {
992 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
993 new_idx = (uint32_t)((uintptr_t) slot_id);
995 memcpy(new_k->key, key, h->key_len);
996 /* Key can be of arbitrary length, so it is not possible to store
997 * it atomically. Hence the new key element's memory stores
998 * (key as well as data) should be complete before it is referenced.
999 * 'pdata' acts as the synchronization point when an existing hash
1002 __atomic_store_n(&new_k->pdata,
1006 /* Find an empty slot and insert */
1007 ret = rte_hash_cuckoo_insert_mw(h, prim_bkt, sec_bkt, key, data,
1008 short_sig, new_idx, &ret_val);
1011 else if (ret == 1) {
1012 enqueue_slot_back(h, cached_free_slots, slot_id);
1016 /* Primary bucket full, need to make space for new entry */
1017 ret = rte_hash_cuckoo_make_space_mw(h, prim_bkt, sec_bkt, key, data,
1018 short_sig, prim_bucket_idx, new_idx, &ret_val);
1021 else if (ret == 1) {
1022 enqueue_slot_back(h, cached_free_slots, slot_id);
1026 /* Also search secondary bucket to get better occupancy */
1027 ret = rte_hash_cuckoo_make_space_mw(h, sec_bkt, prim_bkt, key, data,
1028 short_sig, sec_bucket_idx, new_idx, &ret_val);
1032 else if (ret == 1) {
1033 enqueue_slot_back(h, cached_free_slots, slot_id);
1037 /* if ext table not enabled, we failed the insertion */
1038 if (!h->ext_table_support) {
1039 enqueue_slot_back(h, cached_free_slots, slot_id);
1043 /* Now we need to go through the extendable bucket. Protection is needed
1044 * to protect all extendable bucket processes.
1046 __hash_rw_writer_lock(h);
1047 /* We check for duplicates again since could be inserted before the lock */
1048 ret = search_and_update(h, data, key, prim_bkt, short_sig);
1050 enqueue_slot_back(h, cached_free_slots, slot_id);
1054 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1055 ret = search_and_update(h, data, key, cur_bkt, short_sig);
1057 enqueue_slot_back(h, cached_free_slots, slot_id);
1062 /* Search sec and ext buckets to find an empty entry to insert. */
1063 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1064 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1065 /* Check if slot is available */
1066 if (likely(cur_bkt->key_idx[i] == EMPTY_SLOT)) {
1067 cur_bkt->sig_current[i] = short_sig;
1068 /* Store to signature should not leak after
1069 * the store to key_idx
1071 __atomic_store_n(&cur_bkt->key_idx[i],
1074 __hash_rw_writer_unlock(h);
1080 /* Failed to get an empty entry from extendable buckets. Link a new
1081 * extendable bucket. We first get a free bucket from ring.
1083 if (rte_ring_sc_dequeue(h->free_ext_bkts, &ext_bkt_id) != 0) {
1088 bkt_id = (uint32_t)((uintptr_t)ext_bkt_id) - 1;
1089 /* Use the first location of the new bucket */
1090 (h->buckets_ext[bkt_id]).sig_current[0] = short_sig;
1091 /* Store to signature should not leak after
1092 * the store to key_idx
1094 __atomic_store_n(&(h->buckets_ext[bkt_id]).key_idx[0],
1097 /* Link the new bucket to sec bucket linked list */
1098 last = rte_hash_get_last_bkt(sec_bkt);
1099 last->next = &h->buckets_ext[bkt_id];
1100 __hash_rw_writer_unlock(h);
1104 __hash_rw_writer_unlock(h);
1110 rte_hash_add_key_with_hash(const struct rte_hash *h,
1111 const void *key, hash_sig_t sig)
1113 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1114 return __rte_hash_add_key_with_hash(h, key, sig, 0);
1118 rte_hash_add_key(const struct rte_hash *h, const void *key)
1120 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1121 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
1125 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
1126 const void *key, hash_sig_t sig, void *data)
1130 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1131 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
1139 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
1143 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1145 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
1152 /* Search one bucket to find the match key - uses rw lock */
1153 static inline int32_t
1154 search_one_bucket_l(const struct rte_hash *h, const void *key,
1155 uint16_t sig, void **data,
1156 const struct rte_hash_bucket *bkt)
1159 struct rte_hash_key *k, *keys = h->key_store;
1161 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1162 if (bkt->sig_current[i] == sig &&
1163 bkt->key_idx[i] != EMPTY_SLOT) {
1164 k = (struct rte_hash_key *) ((char *)keys +
1165 bkt->key_idx[i] * h->key_entry_size);
1167 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1171 * Return index where key is stored,
1172 * subtracting the first dummy index
1174 return bkt->key_idx[i] - 1;
1181 /* Search one bucket to find the match key */
1182 static inline int32_t
1183 search_one_bucket_lf(const struct rte_hash *h, const void *key, uint16_t sig,
1184 void **data, const struct rte_hash_bucket *bkt)
1189 struct rte_hash_key *k, *keys = h->key_store;
1191 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1192 key_idx = __atomic_load_n(&bkt->key_idx[i],
1194 if (bkt->sig_current[i] == sig && key_idx != EMPTY_SLOT) {
1195 k = (struct rte_hash_key *) ((char *)keys +
1196 key_idx * h->key_entry_size);
1197 pdata = __atomic_load_n(&k->pdata,
1200 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1204 * Return index where key is stored,
1205 * subtracting the first dummy index
1214 static inline int32_t
1215 __rte_hash_lookup_with_hash_l(const struct rte_hash *h, const void *key,
1216 hash_sig_t sig, void **data)
1218 uint32_t prim_bucket_idx, sec_bucket_idx;
1219 struct rte_hash_bucket *bkt, *cur_bkt;
1223 short_sig = get_short_sig(sig);
1224 prim_bucket_idx = get_prim_bucket_index(h, sig);
1225 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1227 bkt = &h->buckets[prim_bucket_idx];
1229 __hash_rw_reader_lock(h);
1231 /* Check if key is in primary location */
1232 ret = search_one_bucket_l(h, key, short_sig, data, bkt);
1234 __hash_rw_reader_unlock(h);
1237 /* Calculate secondary hash */
1238 bkt = &h->buckets[sec_bucket_idx];
1240 /* Check if key is in secondary location */
1241 FOR_EACH_BUCKET(cur_bkt, bkt) {
1242 ret = search_one_bucket_l(h, key, short_sig,
1245 __hash_rw_reader_unlock(h);
1250 __hash_rw_reader_unlock(h);
1255 static inline int32_t
1256 __rte_hash_lookup_with_hash_lf(const struct rte_hash *h, const void *key,
1257 hash_sig_t sig, void **data)
1259 uint32_t prim_bucket_idx, sec_bucket_idx;
1260 struct rte_hash_bucket *bkt, *cur_bkt;
1261 uint32_t cnt_b, cnt_a;
1265 short_sig = get_short_sig(sig);
1266 prim_bucket_idx = get_prim_bucket_index(h, sig);
1267 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1270 /* Load the table change counter before the lookup
1271 * starts. Acquire semantics will make sure that
1272 * loads in search_one_bucket are not hoisted.
1274 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1277 /* Check if key is in primary location */
1278 bkt = &h->buckets[prim_bucket_idx];
1279 ret = search_one_bucket_lf(h, key, short_sig, data, bkt);
1281 __hash_rw_reader_unlock(h);
1284 /* Calculate secondary hash */
1285 bkt = &h->buckets[sec_bucket_idx];
1287 /* Check if key is in secondary location */
1288 FOR_EACH_BUCKET(cur_bkt, bkt) {
1289 ret = search_one_bucket_lf(h, key, short_sig,
1292 __hash_rw_reader_unlock(h);
1297 /* The loads of sig_current in search_one_bucket
1298 * should not move below the load from tbl_chng_cnt.
1300 __atomic_thread_fence(__ATOMIC_ACQUIRE);
1301 /* Re-read the table change counter to check if the
1302 * table has changed during search. If yes, re-do
1304 * This load should not get hoisted. The load
1305 * acquires on cnt_b, key index in primary bucket
1306 * and key index in secondary bucket will make sure
1307 * that it does not get hoisted.
1309 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
1311 } while (cnt_b != cnt_a);
1316 static inline int32_t
1317 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
1318 hash_sig_t sig, void **data)
1320 if (h->readwrite_concur_lf_support)
1321 return __rte_hash_lookup_with_hash_lf(h, key, sig, data);
1323 return __rte_hash_lookup_with_hash_l(h, key, sig, data);
1327 rte_hash_lookup_with_hash(const struct rte_hash *h,
1328 const void *key, hash_sig_t sig)
1330 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1331 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
1335 rte_hash_lookup(const struct rte_hash *h, const void *key)
1337 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1338 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
1342 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
1343 const void *key, hash_sig_t sig, void **data)
1345 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1346 return __rte_hash_lookup_with_hash(h, key, sig, data);
1350 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
1352 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1353 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
1357 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
1359 unsigned lcore_id, n_slots;
1360 struct lcore_cache *cached_free_slots;
1362 if (h->use_local_cache) {
1363 lcore_id = rte_lcore_id();
1364 cached_free_slots = &h->local_free_slots[lcore_id];
1365 /* Cache full, need to free it. */
1366 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1367 /* Need to enqueue the free slots in global ring. */
1368 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
1369 cached_free_slots->objs,
1370 LCORE_CACHE_SIZE, NULL);
1371 ERR_IF_TRUE((n_slots == 0),
1372 "%s: could not enqueue free slots in global ring\n",
1374 cached_free_slots->len -= n_slots;
1376 /* Put index of new free slot in cache. */
1377 cached_free_slots->objs[cached_free_slots->len] =
1378 (void *)((uintptr_t)bkt->key_idx[i]);
1379 cached_free_slots->len++;
1381 rte_ring_sp_enqueue(h->free_slots,
1382 (void *)((uintptr_t)bkt->key_idx[i]));
1386 /* Compact the linked list by moving key from last entry in linked list to the
1390 __rte_hash_compact_ll(const struct rte_hash *h,
1391 struct rte_hash_bucket *cur_bkt, int pos) {
1393 struct rte_hash_bucket *last_bkt;
1398 last_bkt = rte_hash_get_last_bkt(cur_bkt);
1400 for (i = RTE_HASH_BUCKET_ENTRIES - 1; i >= 0; i--) {
1401 if (last_bkt->key_idx[i] != EMPTY_SLOT) {
1402 cur_bkt->sig_current[pos] = last_bkt->sig_current[i];
1403 __atomic_store_n(&cur_bkt->key_idx[pos],
1404 last_bkt->key_idx[i],
1406 if (h->readwrite_concur_lf_support) {
1407 /* Inform the readers that the table has changed
1408 * Since there is one writer, load acquire on
1409 * tbl_chng_cnt is not required.
1411 __atomic_store_n(h->tbl_chng_cnt,
1412 *h->tbl_chng_cnt + 1,
1414 /* The store to sig_current should
1415 * not move above the store to tbl_chng_cnt.
1417 __atomic_thread_fence(__ATOMIC_RELEASE);
1419 last_bkt->sig_current[i] = NULL_SIGNATURE;
1420 __atomic_store_n(&last_bkt->key_idx[i],
1428 /* Search one bucket and remove the matched key.
1429 * Writer is expected to hold the lock while calling this
1432 static inline int32_t
1433 search_and_remove(const struct rte_hash *h, const void *key,
1434 struct rte_hash_bucket *bkt, uint16_t sig, int *pos)
1436 struct rte_hash_key *k, *keys = h->key_store;
1440 /* Check if key is in bucket */
1441 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1442 key_idx = __atomic_load_n(&bkt->key_idx[i],
1444 if (bkt->sig_current[i] == sig && key_idx != EMPTY_SLOT) {
1445 k = (struct rte_hash_key *) ((char *)keys +
1446 key_idx * h->key_entry_size);
1447 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1448 bkt->sig_current[i] = NULL_SIGNATURE;
1449 /* Free the key store index if
1450 * no_free_on_del is disabled.
1452 if (!h->no_free_on_del)
1453 remove_entry(h, bkt, i);
1455 __atomic_store_n(&bkt->key_idx[i],
1461 * Return index where key is stored,
1462 * subtracting the first dummy index
1471 static inline int32_t
1472 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
1475 uint32_t prim_bucket_idx, sec_bucket_idx;
1476 struct rte_hash_bucket *prim_bkt, *sec_bkt, *prev_bkt, *last_bkt;
1477 struct rte_hash_bucket *cur_bkt;
1482 short_sig = get_short_sig(sig);
1483 prim_bucket_idx = get_prim_bucket_index(h, sig);
1484 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1485 prim_bkt = &h->buckets[prim_bucket_idx];
1487 __hash_rw_writer_lock(h);
1488 /* look for key in primary bucket */
1489 ret = search_and_remove(h, key, prim_bkt, short_sig, &pos);
1491 __rte_hash_compact_ll(h, prim_bkt, pos);
1492 last_bkt = prim_bkt->next;
1493 prev_bkt = prim_bkt;
1497 /* Calculate secondary hash */
1498 sec_bkt = &h->buckets[sec_bucket_idx];
1500 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1501 ret = search_and_remove(h, key, cur_bkt, short_sig, &pos);
1503 __rte_hash_compact_ll(h, cur_bkt, pos);
1504 last_bkt = sec_bkt->next;
1510 __hash_rw_writer_unlock(h);
1513 /* Search last bucket to see if empty to be recycled */
1516 __hash_rw_writer_unlock(h);
1519 while (last_bkt->next) {
1520 prev_bkt = last_bkt;
1521 last_bkt = last_bkt->next;
1524 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1525 if (last_bkt->key_idx[i] != EMPTY_SLOT)
1528 /* found empty bucket and recycle */
1529 if (i == RTE_HASH_BUCKET_ENTRIES) {
1530 prev_bkt->next = NULL;
1531 uint32_t index = last_bkt - h->buckets_ext + 1;
1532 /* Recycle the empty bkt if
1533 * no_free_on_del is disabled.
1535 if (h->no_free_on_del)
1536 /* Store index of an empty ext bkt to be recycled
1537 * on calling rte_hash_del_xxx APIs.
1538 * When lock free read-write concurrency is enabled,
1539 * an empty ext bkt cannot be put into free list
1540 * immediately (as readers might be using it still).
1541 * Hence freeing of the ext bkt is piggy-backed to
1542 * freeing of the key index.
1544 h->ext_bkt_to_free[ret] = index;
1546 rte_ring_sp_enqueue(h->free_ext_bkts, (void *)(uintptr_t)index);
1548 __hash_rw_writer_unlock(h);
1553 rte_hash_del_key_with_hash(const struct rte_hash *h,
1554 const void *key, hash_sig_t sig)
1556 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1557 return __rte_hash_del_key_with_hash(h, key, sig);
1561 rte_hash_del_key(const struct rte_hash *h, const void *key)
1563 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1564 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
1568 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
1571 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1573 struct rte_hash_key *k, *keys = h->key_store;
1574 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
1579 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
1588 rte_hash_free_key_with_position(const struct rte_hash *h,
1589 const int32_t position)
1591 /* Key index where key is stored, adding the first dummy index */
1592 uint32_t key_idx = position + 1;
1594 RETURN_IF_TRUE(((h == NULL) || (key_idx == EMPTY_SLOT)), -EINVAL);
1596 unsigned int lcore_id, n_slots;
1597 struct lcore_cache *cached_free_slots;
1598 const uint32_t total_entries = h->use_local_cache ?
1599 h->entries + (RTE_MAX_LCORE - 1) * (LCORE_CACHE_SIZE - 1) + 1
1603 if (key_idx >= total_entries)
1605 if (h->ext_table_support && h->readwrite_concur_lf_support) {
1606 uint32_t index = h->ext_bkt_to_free[position];
1608 /* Recycle empty ext bkt to free list. */
1609 rte_ring_sp_enqueue(h->free_ext_bkts, (void *)(uintptr_t)index);
1610 h->ext_bkt_to_free[position] = 0;
1614 if (h->use_local_cache) {
1615 lcore_id = rte_lcore_id();
1616 cached_free_slots = &h->local_free_slots[lcore_id];
1617 /* Cache full, need to free it. */
1618 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1619 /* Need to enqueue the free slots in global ring. */
1620 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
1621 cached_free_slots->objs,
1622 LCORE_CACHE_SIZE, NULL);
1623 RETURN_IF_TRUE((n_slots == 0), -EFAULT);
1624 cached_free_slots->len -= n_slots;
1626 /* Put index of new free slot in cache. */
1627 cached_free_slots->objs[cached_free_slots->len] =
1628 (void *)((uintptr_t)key_idx);
1629 cached_free_slots->len++;
1631 rte_ring_sp_enqueue(h->free_slots,
1632 (void *)((uintptr_t)key_idx));
1639 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
1640 const struct rte_hash_bucket *prim_bkt,
1641 const struct rte_hash_bucket *sec_bkt,
1643 enum rte_hash_sig_compare_function sig_cmp_fn)
1647 /* For match mask the first bit of every two bits indicates the match */
1648 switch (sig_cmp_fn) {
1649 #if defined(RTE_MACHINE_CPUFLAG_SSE2)
1650 case RTE_HASH_COMPARE_SSE:
1651 /* Compare all signatures in the bucket */
1652 *prim_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1654 (__m128i const *)prim_bkt->sig_current),
1655 _mm_set1_epi16(sig)));
1656 /* Compare all signatures in the bucket */
1657 *sec_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1659 (__m128i const *)sec_bkt->sig_current),
1660 _mm_set1_epi16(sig)));
1662 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
1663 case RTE_HASH_COMPARE_NEON: {
1664 uint16x8_t vmat, vsig, x;
1665 int16x8_t shift = {-15, -13, -11, -9, -7, -5, -3, -1};
1667 vsig = vld1q_dup_u16((uint16_t const *)&sig);
1668 /* Compare all signatures in the primary bucket */
1669 vmat = vceqq_u16(vsig,
1670 vld1q_u16((uint16_t const *)prim_bkt->sig_current));
1671 x = vshlq_u16(vandq_u16(vmat, vdupq_n_u16(0x8000)), shift);
1672 *prim_hash_matches = (uint32_t)(vaddvq_u16(x));
1673 /* Compare all signatures in the secondary bucket */
1674 vmat = vceqq_u16(vsig,
1675 vld1q_u16((uint16_t const *)sec_bkt->sig_current));
1676 x = vshlq_u16(vandq_u16(vmat, vdupq_n_u16(0x8000)), shift);
1677 *sec_hash_matches = (uint32_t)(vaddvq_u16(x));
1682 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1683 *prim_hash_matches |=
1684 ((sig == prim_bkt->sig_current[i]) << (i << 1));
1685 *sec_hash_matches |=
1686 ((sig == sec_bkt->sig_current[i]) << (i << 1));
1691 #define PREFETCH_OFFSET 4
1693 __rte_hash_lookup_bulk_l(const struct rte_hash *h, const void **keys,
1694 int32_t num_keys, int32_t *positions,
1695 uint64_t *hit_mask, void *data[])
1700 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
1701 uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
1702 uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
1703 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
1704 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1705 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1706 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1707 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1708 struct rte_hash_bucket *cur_bkt, *next_bkt;
1710 /* Prefetch first keys */
1711 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1712 rte_prefetch0(keys[i]);
1715 * Prefetch rest of the keys, calculate primary and
1716 * secondary bucket and prefetch them
1718 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1719 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1721 prim_hash[i] = rte_hash_hash(h, keys[i]);
1723 sig[i] = get_short_sig(prim_hash[i]);
1724 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1725 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1727 primary_bkt[i] = &h->buckets[prim_index[i]];
1728 secondary_bkt[i] = &h->buckets[sec_index[i]];
1730 rte_prefetch0(primary_bkt[i]);
1731 rte_prefetch0(secondary_bkt[i]);
1734 /* Calculate and prefetch rest of the buckets */
1735 for (; i < num_keys; i++) {
1736 prim_hash[i] = rte_hash_hash(h, keys[i]);
1738 sig[i] = get_short_sig(prim_hash[i]);
1739 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1740 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1742 primary_bkt[i] = &h->buckets[prim_index[i]];
1743 secondary_bkt[i] = &h->buckets[sec_index[i]];
1745 rte_prefetch0(primary_bkt[i]);
1746 rte_prefetch0(secondary_bkt[i]);
1749 __hash_rw_reader_lock(h);
1751 /* Compare signatures and prefetch key slot of first hit */
1752 for (i = 0; i < num_keys; i++) {
1753 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1754 primary_bkt[i], secondary_bkt[i],
1755 sig[i], h->sig_cmp_fn);
1757 if (prim_hitmask[i]) {
1758 uint32_t first_hit =
1759 __builtin_ctzl(prim_hitmask[i])
1762 primary_bkt[i]->key_idx[first_hit];
1763 const struct rte_hash_key *key_slot =
1764 (const struct rte_hash_key *)(
1765 (const char *)h->key_store +
1766 key_idx * h->key_entry_size);
1767 rte_prefetch0(key_slot);
1771 if (sec_hitmask[i]) {
1772 uint32_t first_hit =
1773 __builtin_ctzl(sec_hitmask[i])
1776 secondary_bkt[i]->key_idx[first_hit];
1777 const struct rte_hash_key *key_slot =
1778 (const struct rte_hash_key *)(
1779 (const char *)h->key_store +
1780 key_idx * h->key_entry_size);
1781 rte_prefetch0(key_slot);
1785 /* Compare keys, first hits in primary first */
1786 for (i = 0; i < num_keys; i++) {
1787 positions[i] = -ENOENT;
1788 while (prim_hitmask[i]) {
1789 uint32_t hit_index =
1790 __builtin_ctzl(prim_hitmask[i])
1793 primary_bkt[i]->key_idx[hit_index];
1794 const struct rte_hash_key *key_slot =
1795 (const struct rte_hash_key *)(
1796 (const char *)h->key_store +
1797 key_idx * h->key_entry_size);
1800 * If key index is 0, do not compare key,
1801 * as it is checking the dummy slot
1805 key_slot->key, keys[i], h)) {
1807 data[i] = key_slot->pdata;
1810 positions[i] = key_idx - 1;
1813 prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
1816 while (sec_hitmask[i]) {
1817 uint32_t hit_index =
1818 __builtin_ctzl(sec_hitmask[i])
1821 secondary_bkt[i]->key_idx[hit_index];
1822 const struct rte_hash_key *key_slot =
1823 (const struct rte_hash_key *)(
1824 (const char *)h->key_store +
1825 key_idx * h->key_entry_size);
1828 * If key index is 0, do not compare key,
1829 * as it is checking the dummy slot
1834 key_slot->key, keys[i], h)) {
1836 data[i] = key_slot->pdata;
1839 positions[i] = key_idx - 1;
1842 sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
1848 /* all found, do not need to go through ext bkt */
1849 if ((hits == ((1ULL << num_keys) - 1)) || !h->ext_table_support) {
1850 if (hit_mask != NULL)
1852 __hash_rw_reader_unlock(h);
1856 /* need to check ext buckets for match */
1857 for (i = 0; i < num_keys; i++) {
1858 if ((hits & (1ULL << i)) != 0)
1860 next_bkt = secondary_bkt[i]->next;
1861 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
1863 ret = search_one_bucket_l(h, keys[i],
1864 sig[i], &data[i], cur_bkt);
1866 ret = search_one_bucket_l(h, keys[i],
1867 sig[i], NULL, cur_bkt);
1876 __hash_rw_reader_unlock(h);
1878 if (hit_mask != NULL)
1883 __rte_hash_lookup_bulk_lf(const struct rte_hash *h, const void **keys,
1884 int32_t num_keys, int32_t *positions,
1885 uint64_t *hit_mask, void *data[])
1890 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
1891 uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
1892 uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
1893 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
1894 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1895 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1896 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1897 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1898 struct rte_hash_bucket *cur_bkt, *next_bkt;
1899 void *pdata[RTE_HASH_LOOKUP_BULK_MAX];
1900 uint32_t cnt_b, cnt_a;
1902 /* Prefetch first keys */
1903 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1904 rte_prefetch0(keys[i]);
1907 * Prefetch rest of the keys, calculate primary and
1908 * secondary bucket and prefetch them
1910 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1911 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1913 prim_hash[i] = rte_hash_hash(h, keys[i]);
1915 sig[i] = get_short_sig(prim_hash[i]);
1916 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1917 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1919 primary_bkt[i] = &h->buckets[prim_index[i]];
1920 secondary_bkt[i] = &h->buckets[sec_index[i]];
1922 rte_prefetch0(primary_bkt[i]);
1923 rte_prefetch0(secondary_bkt[i]);
1926 /* Calculate and prefetch rest of the buckets */
1927 for (; i < num_keys; i++) {
1928 prim_hash[i] = rte_hash_hash(h, keys[i]);
1930 sig[i] = get_short_sig(prim_hash[i]);
1931 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1932 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1934 primary_bkt[i] = &h->buckets[prim_index[i]];
1935 secondary_bkt[i] = &h->buckets[sec_index[i]];
1937 rte_prefetch0(primary_bkt[i]);
1938 rte_prefetch0(secondary_bkt[i]);
1941 for (i = 0; i < num_keys; i++)
1942 positions[i] = -ENOENT;
1945 /* Load the table change counter before the lookup
1946 * starts. Acquire semantics will make sure that
1947 * loads in compare_signatures are not hoisted.
1949 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1952 /* Compare signatures and prefetch key slot of first hit */
1953 for (i = 0; i < num_keys; i++) {
1954 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1955 primary_bkt[i], secondary_bkt[i],
1956 sig[i], h->sig_cmp_fn);
1958 if (prim_hitmask[i]) {
1959 uint32_t first_hit =
1960 __builtin_ctzl(prim_hitmask[i])
1963 primary_bkt[i]->key_idx[first_hit];
1964 const struct rte_hash_key *key_slot =
1965 (const struct rte_hash_key *)(
1966 (const char *)h->key_store +
1967 key_idx * h->key_entry_size);
1968 rte_prefetch0(key_slot);
1972 if (sec_hitmask[i]) {
1973 uint32_t first_hit =
1974 __builtin_ctzl(sec_hitmask[i])
1977 secondary_bkt[i]->key_idx[first_hit];
1978 const struct rte_hash_key *key_slot =
1979 (const struct rte_hash_key *)(
1980 (const char *)h->key_store +
1981 key_idx * h->key_entry_size);
1982 rte_prefetch0(key_slot);
1986 /* Compare keys, first hits in primary first */
1987 for (i = 0; i < num_keys; i++) {
1988 while (prim_hitmask[i]) {
1989 uint32_t hit_index =
1990 __builtin_ctzl(prim_hitmask[i])
1994 &primary_bkt[i]->key_idx[hit_index],
1996 const struct rte_hash_key *key_slot =
1997 (const struct rte_hash_key *)(
1998 (const char *)h->key_store +
1999 key_idx * h->key_entry_size);
2001 if (key_idx != EMPTY_SLOT)
2002 pdata[i] = __atomic_load_n(
2006 * If key index is 0, do not compare key,
2007 * as it is checking the dummy slot
2011 key_slot->key, keys[i], h)) {
2016 positions[i] = key_idx - 1;
2019 prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
2022 while (sec_hitmask[i]) {
2023 uint32_t hit_index =
2024 __builtin_ctzl(sec_hitmask[i])
2028 &secondary_bkt[i]->key_idx[hit_index],
2030 const struct rte_hash_key *key_slot =
2031 (const struct rte_hash_key *)(
2032 (const char *)h->key_store +
2033 key_idx * h->key_entry_size);
2035 if (key_idx != EMPTY_SLOT)
2036 pdata[i] = __atomic_load_n(
2040 * If key index is 0, do not compare key,
2041 * as it is checking the dummy slot
2046 key_slot->key, keys[i], h)) {
2051 positions[i] = key_idx - 1;
2054 sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
2060 /* all found, do not need to go through ext bkt */
2061 if (hits == ((1ULL << num_keys) - 1)) {
2062 if (hit_mask != NULL)
2066 /* need to check ext buckets for match */
2067 if (h->ext_table_support) {
2068 for (i = 0; i < num_keys; i++) {
2069 if ((hits & (1ULL << i)) != 0)
2071 next_bkt = secondary_bkt[i]->next;
2072 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
2074 ret = search_one_bucket_lf(h,
2078 ret = search_one_bucket_lf(h,
2089 /* The loads of sig_current in compare_signatures
2090 * should not move below the load from tbl_chng_cnt.
2092 __atomic_thread_fence(__ATOMIC_ACQUIRE);
2093 /* Re-read the table change counter to check if the
2094 * table has changed during search. If yes, re-do
2096 * This load should not get hoisted. The load
2097 * acquires on cnt_b, primary key index and secondary
2098 * key index will make sure that it does not get
2101 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
2103 } while (cnt_b != cnt_a);
2105 if (hit_mask != NULL)
2110 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
2111 int32_t num_keys, int32_t *positions,
2112 uint64_t *hit_mask, void *data[])
2114 if (h->readwrite_concur_lf_support)
2115 __rte_hash_lookup_bulk_lf(h, keys, num_keys, positions,
2118 __rte_hash_lookup_bulk_l(h, keys, num_keys, positions,
2123 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
2124 uint32_t num_keys, int32_t *positions)
2126 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
2127 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2128 (positions == NULL)), -EINVAL);
2130 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
2135 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
2136 uint32_t num_keys, uint64_t *hit_mask, void *data[])
2138 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
2139 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2140 (hit_mask == NULL)), -EINVAL);
2142 int32_t positions[num_keys];
2144 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
2146 /* Return number of hits */
2147 return __builtin_popcountl(*hit_mask);
2151 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
2153 uint32_t bucket_idx, idx, position;
2154 struct rte_hash_key *next_key;
2156 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
2158 const uint32_t total_entries_main = h->num_buckets *
2159 RTE_HASH_BUCKET_ENTRIES;
2160 const uint32_t total_entries = total_entries_main << 1;
2162 /* Out of bounds of all buckets (both main table and ext table) */
2163 if (*next >= total_entries_main)
2166 /* Calculate bucket and index of current iterator */
2167 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
2168 idx = *next % RTE_HASH_BUCKET_ENTRIES;
2170 /* If current position is empty, go to the next one */
2171 while ((position = __atomic_load_n(&h->buckets[bucket_idx].key_idx[idx],
2172 __ATOMIC_ACQUIRE)) == EMPTY_SLOT) {
2175 if (*next == total_entries_main)
2177 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
2178 idx = *next % RTE_HASH_BUCKET_ENTRIES;
2181 __hash_rw_reader_lock(h);
2182 next_key = (struct rte_hash_key *) ((char *)h->key_store +
2183 position * h->key_entry_size);
2184 /* Return key and data */
2185 *key = next_key->key;
2186 *data = next_key->pdata;
2188 __hash_rw_reader_unlock(h);
2190 /* Increment iterator */
2193 return position - 1;
2195 /* Begin to iterate extendable buckets */
2197 /* Out of total bound or if ext bucket feature is not enabled */
2198 if (*next >= total_entries || !h->ext_table_support)
2201 bucket_idx = (*next - total_entries_main) / RTE_HASH_BUCKET_ENTRIES;
2202 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
2204 while ((position = h->buckets_ext[bucket_idx].key_idx[idx]) == EMPTY_SLOT) {
2206 if (*next == total_entries)
2208 bucket_idx = (*next - total_entries_main) /
2209 RTE_HASH_BUCKET_ENTRIES;
2210 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
2212 __hash_rw_reader_lock(h);
2213 next_key = (struct rte_hash_key *) ((char *)h->key_store +
2214 position * h->key_entry_size);
2215 /* Return key and data */
2216 *key = next_key->key;
2217 *data = next_key->pdata;
2219 __hash_rw_reader_unlock(h);
2221 /* Increment iterator */
2223 return position - 1;