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>
27 #include <rte_ring_elem.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;
139 unsigned int hw_trans_mem_support = 0, use_local_cache = 0;
140 unsigned int ext_table_support = 0;
141 unsigned int readwrite_concur_support = 0;
142 unsigned int writer_takes_lock = 0;
143 unsigned int no_free_on_del = 0;
144 uint32_t *ext_bkt_to_free = NULL;
145 uint32_t *tbl_chng_cnt = NULL;
146 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_elem(ring_name, sizeof(uint32_t),
217 rte_align32pow2(num_key_slots), 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_elem(ext_ring_name, sizeof(uint32_t),
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_elem(r_ext, &i, sizeof(uint32_t));
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_elem(r, &i, sizeof(uint32_t));
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_max_key_id(const struct rte_hash *h)
512 RETURN_IF_TRUE((h == NULL), -EINVAL);
513 if (h->use_local_cache)
515 * Increase number of slots by total number of indices
516 * that can be stored in the lcore caches
518 return (h->entries + ((RTE_MAX_LCORE - 1) *
519 (LCORE_CACHE_SIZE - 1)));
525 rte_hash_count(const struct rte_hash *h)
527 uint32_t tot_ring_cnt, cached_cnt = 0;
533 if (h->use_local_cache) {
534 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
535 (LCORE_CACHE_SIZE - 1);
536 for (i = 0; i < RTE_MAX_LCORE; i++)
537 cached_cnt += h->local_free_slots[i].len;
539 ret = tot_ring_cnt - rte_ring_count(h->free_slots) -
542 tot_ring_cnt = h->entries;
543 ret = tot_ring_cnt - rte_ring_count(h->free_slots);
548 /* Read write locks implemented using rte_rwlock */
550 __hash_rw_writer_lock(const struct rte_hash *h)
552 if (h->writer_takes_lock && h->hw_trans_mem_support)
553 rte_rwlock_write_lock_tm(h->readwrite_lock);
554 else if (h->writer_takes_lock)
555 rte_rwlock_write_lock(h->readwrite_lock);
559 __hash_rw_reader_lock(const struct rte_hash *h)
561 if (h->readwrite_concur_support && h->hw_trans_mem_support)
562 rte_rwlock_read_lock_tm(h->readwrite_lock);
563 else if (h->readwrite_concur_support)
564 rte_rwlock_read_lock(h->readwrite_lock);
568 __hash_rw_writer_unlock(const struct rte_hash *h)
570 if (h->writer_takes_lock && h->hw_trans_mem_support)
571 rte_rwlock_write_unlock_tm(h->readwrite_lock);
572 else if (h->writer_takes_lock)
573 rte_rwlock_write_unlock(h->readwrite_lock);
577 __hash_rw_reader_unlock(const struct rte_hash *h)
579 if (h->readwrite_concur_support && h->hw_trans_mem_support)
580 rte_rwlock_read_unlock_tm(h->readwrite_lock);
581 else if (h->readwrite_concur_support)
582 rte_rwlock_read_unlock(h->readwrite_lock);
586 rte_hash_reset(struct rte_hash *h)
588 uint32_t tot_ring_cnt, i;
593 __hash_rw_writer_lock(h);
594 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
595 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
596 *h->tbl_chng_cnt = 0;
598 /* reset the free ring */
599 rte_ring_reset(h->free_slots);
601 /* flush free extendable bucket ring and memory */
602 if (h->ext_table_support) {
603 memset(h->buckets_ext, 0, h->num_buckets *
604 sizeof(struct rte_hash_bucket));
605 rte_ring_reset(h->free_ext_bkts);
608 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
609 if (h->use_local_cache)
610 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
611 (LCORE_CACHE_SIZE - 1);
613 tot_ring_cnt = h->entries;
615 for (i = 1; i < tot_ring_cnt + 1; i++)
616 rte_ring_sp_enqueue_elem(h->free_slots, &i, sizeof(uint32_t));
618 /* Repopulate the free ext bkt ring. */
619 if (h->ext_table_support) {
620 for (i = 1; i <= h->num_buckets; i++)
621 rte_ring_sp_enqueue_elem(h->free_ext_bkts, &i,
625 if (h->use_local_cache) {
626 /* Reset local caches per lcore */
627 for (i = 0; i < RTE_MAX_LCORE; i++)
628 h->local_free_slots[i].len = 0;
630 __hash_rw_writer_unlock(h);
634 * Function called to enqueue back an index in the cache/ring,
635 * as slot has not being used and it can be used in the
636 * next addition attempt.
639 enqueue_slot_back(const struct rte_hash *h,
640 struct lcore_cache *cached_free_slots,
643 if (h->use_local_cache) {
644 cached_free_slots->objs[cached_free_slots->len] = slot_id;
645 cached_free_slots->len++;
647 rte_ring_sp_enqueue_elem(h->free_slots, &slot_id,
651 /* Search a key from bucket and update its data.
652 * Writer holds the lock before calling this.
654 static inline int32_t
655 search_and_update(const struct rte_hash *h, void *data, const void *key,
656 struct rte_hash_bucket *bkt, uint16_t sig)
659 struct rte_hash_key *k, *keys = h->key_store;
661 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
662 if (bkt->sig_current[i] == sig) {
663 k = (struct rte_hash_key *) ((char *)keys +
664 bkt->key_idx[i] * h->key_entry_size);
665 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
666 /* The store to application data at *data
667 * should not leak after the store to pdata
668 * in the key store. i.e. pdata is the guard
669 * variable. Release the application data
672 __atomic_store_n(&k->pdata,
676 * Return index where key is stored,
677 * subtracting the first dummy index
679 return bkt->key_idx[i] - 1;
686 /* Only tries to insert at one bucket (@prim_bkt) without trying to push
688 * return 1 if matching existing key, return 0 if succeeds, return -1 for no
691 static inline int32_t
692 rte_hash_cuckoo_insert_mw(const struct rte_hash *h,
693 struct rte_hash_bucket *prim_bkt,
694 struct rte_hash_bucket *sec_bkt,
695 const struct rte_hash_key *key, void *data,
696 uint16_t sig, uint32_t new_idx,
700 struct rte_hash_bucket *cur_bkt;
703 __hash_rw_writer_lock(h);
704 /* Check if key was inserted after last check but before this
705 * protected region in case of inserting duplicated keys.
707 ret = search_and_update(h, data, key, prim_bkt, sig);
709 __hash_rw_writer_unlock(h);
714 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
715 ret = search_and_update(h, data, key, cur_bkt, sig);
717 __hash_rw_writer_unlock(h);
723 /* Insert new entry if there is room in the primary
726 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
727 /* Check if slot is available */
728 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
729 prim_bkt->sig_current[i] = sig;
730 /* Store to signature and key should not
731 * leak after the store to key_idx. i.e.
732 * key_idx is the guard variable for signature
735 __atomic_store_n(&prim_bkt->key_idx[i],
741 __hash_rw_writer_unlock(h);
743 if (i != RTE_HASH_BUCKET_ENTRIES)
750 /* Shift buckets along provided cuckoo_path (@leaf and @leaf_slot) and fill
751 * the path head with new entry (sig, alt_hash, new_idx)
752 * return 1 if matched key found, return -1 if cuckoo path invalided and fail,
753 * return 0 if succeeds.
756 rte_hash_cuckoo_move_insert_mw(const struct rte_hash *h,
757 struct rte_hash_bucket *bkt,
758 struct rte_hash_bucket *alt_bkt,
759 const struct rte_hash_key *key, void *data,
760 struct queue_node *leaf, uint32_t leaf_slot,
761 uint16_t sig, uint32_t new_idx,
764 uint32_t prev_alt_bkt_idx;
765 struct rte_hash_bucket *cur_bkt;
766 struct queue_node *prev_node, *curr_node = leaf;
767 struct rte_hash_bucket *prev_bkt, *curr_bkt = leaf->bkt;
768 uint32_t prev_slot, curr_slot = leaf_slot;
771 __hash_rw_writer_lock(h);
773 /* In case empty slot was gone before entering protected region */
774 if (curr_bkt->key_idx[curr_slot] != EMPTY_SLOT) {
775 __hash_rw_writer_unlock(h);
779 /* Check if key was inserted after last check but before this
782 ret = search_and_update(h, data, key, bkt, sig);
784 __hash_rw_writer_unlock(h);
789 FOR_EACH_BUCKET(cur_bkt, alt_bkt) {
790 ret = search_and_update(h, data, key, cur_bkt, sig);
792 __hash_rw_writer_unlock(h);
798 while (likely(curr_node->prev != NULL)) {
799 prev_node = curr_node->prev;
800 prev_bkt = prev_node->bkt;
801 prev_slot = curr_node->prev_slot;
803 prev_alt_bkt_idx = get_alt_bucket_index(h,
804 prev_node->cur_bkt_idx,
805 prev_bkt->sig_current[prev_slot]);
807 if (unlikely(&h->buckets[prev_alt_bkt_idx]
809 /* revert it to empty, otherwise duplicated keys */
810 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
813 __hash_rw_writer_unlock(h);
817 if (h->readwrite_concur_lf_support) {
818 /* Inform the previous move. The current move need
819 * not be informed now as the current bucket entry
820 * is present in both primary and secondary.
821 * Since there is one writer, load acquires on
822 * tbl_chng_cnt are not required.
824 __atomic_store_n(h->tbl_chng_cnt,
825 *h->tbl_chng_cnt + 1,
827 /* The store to sig_current should not
828 * move above the store to tbl_chng_cnt.
830 __atomic_thread_fence(__ATOMIC_RELEASE);
833 /* Need to swap current/alt sig to allow later
834 * Cuckoo insert to move elements back to its
835 * primary bucket if available
837 curr_bkt->sig_current[curr_slot] =
838 prev_bkt->sig_current[prev_slot];
839 /* Release the updated bucket entry */
840 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
841 prev_bkt->key_idx[prev_slot],
844 curr_slot = prev_slot;
845 curr_node = prev_node;
846 curr_bkt = curr_node->bkt;
849 if (h->readwrite_concur_lf_support) {
850 /* Inform the previous move. The current move need
851 * not be informed now as the current bucket entry
852 * is present in both primary and secondary.
853 * Since there is one writer, load acquires on
854 * tbl_chng_cnt are not required.
856 __atomic_store_n(h->tbl_chng_cnt,
857 *h->tbl_chng_cnt + 1,
859 /* The store to sig_current should not
860 * move above the store to tbl_chng_cnt.
862 __atomic_thread_fence(__ATOMIC_RELEASE);
865 curr_bkt->sig_current[curr_slot] = sig;
866 /* Release the new bucket entry */
867 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
871 __hash_rw_writer_unlock(h);
878 * Make space for new key, using bfs Cuckoo Search and Multi-Writer safe
882 rte_hash_cuckoo_make_space_mw(const struct rte_hash *h,
883 struct rte_hash_bucket *bkt,
884 struct rte_hash_bucket *sec_bkt,
885 const struct rte_hash_key *key, void *data,
886 uint16_t sig, uint32_t bucket_idx,
887 uint32_t new_idx, int32_t *ret_val)
890 struct queue_node queue[RTE_HASH_BFS_QUEUE_MAX_LEN];
891 struct queue_node *tail, *head;
892 struct rte_hash_bucket *curr_bkt, *alt_bkt;
893 uint32_t cur_idx, alt_idx;
899 tail->prev_slot = -1;
900 tail->cur_bkt_idx = bucket_idx;
902 /* Cuckoo bfs Search */
903 while (likely(tail != head && head <
904 queue + RTE_HASH_BFS_QUEUE_MAX_LEN -
905 RTE_HASH_BUCKET_ENTRIES)) {
906 curr_bkt = tail->bkt;
907 cur_idx = tail->cur_bkt_idx;
908 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
909 if (curr_bkt->key_idx[i] == EMPTY_SLOT) {
910 int32_t ret = rte_hash_cuckoo_move_insert_mw(h,
911 bkt, sec_bkt, key, data,
914 if (likely(ret != -1))
918 /* Enqueue new node and keep prev node info */
919 alt_idx = get_alt_bucket_index(h, cur_idx,
920 curr_bkt->sig_current[i]);
921 alt_bkt = &(h->buckets[alt_idx]);
923 head->cur_bkt_idx = alt_idx;
934 static inline int32_t
935 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
936 hash_sig_t sig, void *data)
939 uint32_t prim_bucket_idx, sec_bucket_idx;
940 struct rte_hash_bucket *prim_bkt, *sec_bkt, *cur_bkt;
941 struct rte_hash_key *new_k, *keys = h->key_store;
942 uint32_t ext_bkt_id = 0;
948 struct lcore_cache *cached_free_slots = NULL;
950 struct rte_hash_bucket *last;
952 short_sig = get_short_sig(sig);
953 prim_bucket_idx = get_prim_bucket_index(h, sig);
954 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
955 prim_bkt = &h->buckets[prim_bucket_idx];
956 sec_bkt = &h->buckets[sec_bucket_idx];
957 rte_prefetch0(prim_bkt);
958 rte_prefetch0(sec_bkt);
960 /* Check if key is already inserted in primary location */
961 __hash_rw_writer_lock(h);
962 ret = search_and_update(h, data, key, prim_bkt, short_sig);
964 __hash_rw_writer_unlock(h);
968 /* Check if key is already inserted in secondary location */
969 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
970 ret = search_and_update(h, data, key, cur_bkt, short_sig);
972 __hash_rw_writer_unlock(h);
977 __hash_rw_writer_unlock(h);
979 /* Did not find a match, so get a new slot for storing the new key */
980 if (h->use_local_cache) {
981 lcore_id = rte_lcore_id();
982 cached_free_slots = &h->local_free_slots[lcore_id];
983 /* Try to get a free slot from the local cache */
984 if (cached_free_slots->len == 0) {
985 /* Need to get another burst of free slots from global ring */
986 n_slots = rte_ring_mc_dequeue_burst_elem(h->free_slots,
987 cached_free_slots->objs,
989 LCORE_CACHE_SIZE, NULL);
994 cached_free_slots->len += n_slots;
997 /* Get a free slot from the local cache */
998 cached_free_slots->len--;
999 slot_id = cached_free_slots->objs[cached_free_slots->len];
1001 if (rte_ring_sc_dequeue_elem(h->free_slots, &slot_id,
1002 sizeof(uint32_t)) != 0) {
1007 new_k = RTE_PTR_ADD(keys, slot_id * h->key_entry_size);
1008 /* The store to application data (by the application) at *data should
1009 * not leak after the store of pdata in the key store. i.e. pdata is
1010 * the guard variable. Release the application data to the readers.
1012 __atomic_store_n(&new_k->pdata,
1016 memcpy(new_k->key, key, h->key_len);
1018 /* Find an empty slot and insert */
1019 ret = rte_hash_cuckoo_insert_mw(h, prim_bkt, sec_bkt, key, data,
1020 short_sig, slot_id, &ret_val);
1023 else if (ret == 1) {
1024 enqueue_slot_back(h, cached_free_slots, slot_id);
1028 /* Primary bucket full, need to make space for new entry */
1029 ret = rte_hash_cuckoo_make_space_mw(h, prim_bkt, sec_bkt, key, data,
1030 short_sig, prim_bucket_idx, slot_id, &ret_val);
1033 else if (ret == 1) {
1034 enqueue_slot_back(h, cached_free_slots, slot_id);
1038 /* Also search secondary bucket to get better occupancy */
1039 ret = rte_hash_cuckoo_make_space_mw(h, sec_bkt, prim_bkt, key, data,
1040 short_sig, sec_bucket_idx, slot_id, &ret_val);
1044 else if (ret == 1) {
1045 enqueue_slot_back(h, cached_free_slots, slot_id);
1049 /* if ext table not enabled, we failed the insertion */
1050 if (!h->ext_table_support) {
1051 enqueue_slot_back(h, cached_free_slots, slot_id);
1055 /* Now we need to go through the extendable bucket. Protection is needed
1056 * to protect all extendable bucket processes.
1058 __hash_rw_writer_lock(h);
1059 /* We check for duplicates again since could be inserted before the lock */
1060 ret = search_and_update(h, data, key, prim_bkt, short_sig);
1062 enqueue_slot_back(h, cached_free_slots, slot_id);
1066 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1067 ret = search_and_update(h, data, key, cur_bkt, short_sig);
1069 enqueue_slot_back(h, cached_free_slots, slot_id);
1074 /* Search sec and ext buckets to find an empty entry to insert. */
1075 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1076 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1077 /* Check if slot is available */
1078 if (likely(cur_bkt->key_idx[i] == EMPTY_SLOT)) {
1079 cur_bkt->sig_current[i] = short_sig;
1080 /* Store to signature and key should not
1081 * leak after the store to key_idx. i.e.
1082 * key_idx is the guard variable for signature
1085 __atomic_store_n(&cur_bkt->key_idx[i],
1088 __hash_rw_writer_unlock(h);
1094 /* Failed to get an empty entry from extendable buckets. Link a new
1095 * extendable bucket. We first get a free bucket from ring.
1097 if (rte_ring_sc_dequeue_elem(h->free_ext_bkts, &ext_bkt_id,
1098 sizeof(uint32_t)) != 0 ||
1104 /* Use the first location of the new bucket */
1105 (h->buckets_ext[ext_bkt_id - 1]).sig_current[0] = short_sig;
1106 /* Store to signature and key should not leak after
1107 * the store to key_idx. i.e. key_idx is the guard variable
1108 * for signature and key.
1110 __atomic_store_n(&(h->buckets_ext[ext_bkt_id - 1]).key_idx[0],
1113 /* Link the new bucket to sec bucket linked list */
1114 last = rte_hash_get_last_bkt(sec_bkt);
1115 last->next = &h->buckets_ext[ext_bkt_id - 1];
1116 __hash_rw_writer_unlock(h);
1120 __hash_rw_writer_unlock(h);
1126 rte_hash_add_key_with_hash(const struct rte_hash *h,
1127 const void *key, hash_sig_t sig)
1129 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1130 return __rte_hash_add_key_with_hash(h, key, sig, 0);
1134 rte_hash_add_key(const struct rte_hash *h, const void *key)
1136 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1137 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
1141 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
1142 const void *key, hash_sig_t sig, void *data)
1146 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1147 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
1155 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
1159 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1161 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
1168 /* Search one bucket to find the match key - uses rw lock */
1169 static inline int32_t
1170 search_one_bucket_l(const struct rte_hash *h, const void *key,
1171 uint16_t sig, void **data,
1172 const struct rte_hash_bucket *bkt)
1175 struct rte_hash_key *k, *keys = h->key_store;
1177 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1178 if (bkt->sig_current[i] == sig &&
1179 bkt->key_idx[i] != EMPTY_SLOT) {
1180 k = (struct rte_hash_key *) ((char *)keys +
1181 bkt->key_idx[i] * h->key_entry_size);
1183 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1187 * Return index where key is stored,
1188 * subtracting the first dummy index
1190 return bkt->key_idx[i] - 1;
1197 /* Search one bucket to find the match key */
1198 static inline int32_t
1199 search_one_bucket_lf(const struct rte_hash *h, const void *key, uint16_t sig,
1200 void **data, const struct rte_hash_bucket *bkt)
1204 struct rte_hash_key *k, *keys = h->key_store;
1206 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1207 /* Signature comparison is done before the acquire-load
1208 * of the key index to achieve better performance.
1209 * This can result in the reader loading old signature
1210 * (which matches), while the key_idx is updated to a
1211 * value that belongs to a new key. However, the full
1212 * key comparison will ensure that the lookup fails.
1214 if (bkt->sig_current[i] == sig) {
1215 key_idx = __atomic_load_n(&bkt->key_idx[i],
1217 if (key_idx != EMPTY_SLOT) {
1218 k = (struct rte_hash_key *) ((char *)keys +
1219 key_idx * h->key_entry_size);
1221 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1223 *data = __atomic_load_n(
1228 * Return index where key is stored,
1229 * subtracting the first dummy index
1239 static inline int32_t
1240 __rte_hash_lookup_with_hash_l(const struct rte_hash *h, const void *key,
1241 hash_sig_t sig, void **data)
1243 uint32_t prim_bucket_idx, sec_bucket_idx;
1244 struct rte_hash_bucket *bkt, *cur_bkt;
1248 short_sig = get_short_sig(sig);
1249 prim_bucket_idx = get_prim_bucket_index(h, sig);
1250 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1252 bkt = &h->buckets[prim_bucket_idx];
1254 __hash_rw_reader_lock(h);
1256 /* Check if key is in primary location */
1257 ret = search_one_bucket_l(h, key, short_sig, data, bkt);
1259 __hash_rw_reader_unlock(h);
1262 /* Calculate secondary hash */
1263 bkt = &h->buckets[sec_bucket_idx];
1265 /* Check if key is in secondary location */
1266 FOR_EACH_BUCKET(cur_bkt, bkt) {
1267 ret = search_one_bucket_l(h, key, short_sig,
1270 __hash_rw_reader_unlock(h);
1275 __hash_rw_reader_unlock(h);
1280 static inline int32_t
1281 __rte_hash_lookup_with_hash_lf(const struct rte_hash *h, const void *key,
1282 hash_sig_t sig, void **data)
1284 uint32_t prim_bucket_idx, sec_bucket_idx;
1285 struct rte_hash_bucket *bkt, *cur_bkt;
1286 uint32_t cnt_b, cnt_a;
1290 short_sig = get_short_sig(sig);
1291 prim_bucket_idx = get_prim_bucket_index(h, sig);
1292 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1295 /* Load the table change counter before the lookup
1296 * starts. Acquire semantics will make sure that
1297 * loads in search_one_bucket are not hoisted.
1299 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1302 /* Check if key is in primary location */
1303 bkt = &h->buckets[prim_bucket_idx];
1304 ret = search_one_bucket_lf(h, key, short_sig, data, bkt);
1307 /* Calculate secondary hash */
1308 bkt = &h->buckets[sec_bucket_idx];
1310 /* Check if key is in secondary location */
1311 FOR_EACH_BUCKET(cur_bkt, bkt) {
1312 ret = search_one_bucket_lf(h, key, short_sig,
1318 /* The loads of sig_current in search_one_bucket
1319 * should not move below the load from tbl_chng_cnt.
1321 __atomic_thread_fence(__ATOMIC_ACQUIRE);
1322 /* Re-read the table change counter to check if the
1323 * table has changed during search. If yes, re-do
1325 * This load should not get hoisted. The load
1326 * acquires on cnt_b, key index in primary bucket
1327 * and key index in secondary bucket will make sure
1328 * that it does not get hoisted.
1330 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
1332 } while (cnt_b != cnt_a);
1337 static inline int32_t
1338 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
1339 hash_sig_t sig, void **data)
1341 if (h->readwrite_concur_lf_support)
1342 return __rte_hash_lookup_with_hash_lf(h, key, sig, data);
1344 return __rte_hash_lookup_with_hash_l(h, key, sig, data);
1348 rte_hash_lookup_with_hash(const struct rte_hash *h,
1349 const void *key, hash_sig_t sig)
1351 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1352 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
1356 rte_hash_lookup(const struct rte_hash *h, const void *key)
1358 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1359 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
1363 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
1364 const void *key, hash_sig_t sig, void **data)
1366 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1367 return __rte_hash_lookup_with_hash(h, key, sig, data);
1371 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
1373 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1374 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
1378 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
1380 unsigned lcore_id, n_slots;
1381 struct lcore_cache *cached_free_slots;
1383 if (h->use_local_cache) {
1384 lcore_id = rte_lcore_id();
1385 cached_free_slots = &h->local_free_slots[lcore_id];
1386 /* Cache full, need to free it. */
1387 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1388 /* Need to enqueue the free slots in global ring. */
1389 n_slots = rte_ring_mp_enqueue_burst_elem(h->free_slots,
1390 cached_free_slots->objs,
1392 LCORE_CACHE_SIZE, NULL);
1393 ERR_IF_TRUE((n_slots == 0),
1394 "%s: could not enqueue free slots in global ring\n",
1396 cached_free_slots->len -= n_slots;
1398 /* Put index of new free slot in cache. */
1399 cached_free_slots->objs[cached_free_slots->len] =
1401 cached_free_slots->len++;
1403 rte_ring_sp_enqueue_elem(h->free_slots,
1404 &bkt->key_idx[i], sizeof(uint32_t));
1408 /* Compact the linked list by moving key from last entry in linked list to the
1412 __rte_hash_compact_ll(const struct rte_hash *h,
1413 struct rte_hash_bucket *cur_bkt, int pos) {
1415 struct rte_hash_bucket *last_bkt;
1420 last_bkt = rte_hash_get_last_bkt(cur_bkt);
1422 for (i = RTE_HASH_BUCKET_ENTRIES - 1; i >= 0; i--) {
1423 if (last_bkt->key_idx[i] != EMPTY_SLOT) {
1424 cur_bkt->sig_current[pos] = last_bkt->sig_current[i];
1425 __atomic_store_n(&cur_bkt->key_idx[pos],
1426 last_bkt->key_idx[i],
1428 if (h->readwrite_concur_lf_support) {
1429 /* Inform the readers that the table has changed
1430 * Since there is one writer, load acquire on
1431 * tbl_chng_cnt is not required.
1433 __atomic_store_n(h->tbl_chng_cnt,
1434 *h->tbl_chng_cnt + 1,
1436 /* The store to sig_current should
1437 * not move above the store to tbl_chng_cnt.
1439 __atomic_thread_fence(__ATOMIC_RELEASE);
1441 last_bkt->sig_current[i] = NULL_SIGNATURE;
1442 __atomic_store_n(&last_bkt->key_idx[i],
1450 /* Search one bucket and remove the matched key.
1451 * Writer is expected to hold the lock while calling this
1454 static inline int32_t
1455 search_and_remove(const struct rte_hash *h, const void *key,
1456 struct rte_hash_bucket *bkt, uint16_t sig, int *pos)
1458 struct rte_hash_key *k, *keys = h->key_store;
1462 /* Check if key is in bucket */
1463 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1464 key_idx = __atomic_load_n(&bkt->key_idx[i],
1466 if (bkt->sig_current[i] == sig && key_idx != EMPTY_SLOT) {
1467 k = (struct rte_hash_key *) ((char *)keys +
1468 key_idx * h->key_entry_size);
1469 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1470 bkt->sig_current[i] = NULL_SIGNATURE;
1471 /* Free the key store index if
1472 * no_free_on_del is disabled.
1474 if (!h->no_free_on_del)
1475 remove_entry(h, bkt, i);
1477 __atomic_store_n(&bkt->key_idx[i],
1483 * Return index where key is stored,
1484 * subtracting the first dummy index
1493 static inline int32_t
1494 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
1497 uint32_t prim_bucket_idx, sec_bucket_idx;
1498 struct rte_hash_bucket *prim_bkt, *sec_bkt, *prev_bkt, *last_bkt;
1499 struct rte_hash_bucket *cur_bkt;
1504 short_sig = get_short_sig(sig);
1505 prim_bucket_idx = get_prim_bucket_index(h, sig);
1506 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1507 prim_bkt = &h->buckets[prim_bucket_idx];
1509 __hash_rw_writer_lock(h);
1510 /* look for key in primary bucket */
1511 ret = search_and_remove(h, key, prim_bkt, short_sig, &pos);
1513 __rte_hash_compact_ll(h, prim_bkt, pos);
1514 last_bkt = prim_bkt->next;
1515 prev_bkt = prim_bkt;
1519 /* Calculate secondary hash */
1520 sec_bkt = &h->buckets[sec_bucket_idx];
1522 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1523 ret = search_and_remove(h, key, cur_bkt, short_sig, &pos);
1525 __rte_hash_compact_ll(h, cur_bkt, pos);
1526 last_bkt = sec_bkt->next;
1532 __hash_rw_writer_unlock(h);
1535 /* Search last bucket to see if empty to be recycled */
1538 __hash_rw_writer_unlock(h);
1541 while (last_bkt->next) {
1542 prev_bkt = last_bkt;
1543 last_bkt = last_bkt->next;
1546 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1547 if (last_bkt->key_idx[i] != EMPTY_SLOT)
1550 /* found empty bucket and recycle */
1551 if (i == RTE_HASH_BUCKET_ENTRIES) {
1552 prev_bkt->next = NULL;
1553 uint32_t index = last_bkt - h->buckets_ext + 1;
1554 /* Recycle the empty bkt if
1555 * no_free_on_del is disabled.
1557 if (h->no_free_on_del)
1558 /* Store index of an empty ext bkt to be recycled
1559 * on calling rte_hash_del_xxx APIs.
1560 * When lock free read-write concurrency is enabled,
1561 * an empty ext bkt cannot be put into free list
1562 * immediately (as readers might be using it still).
1563 * Hence freeing of the ext bkt is piggy-backed to
1564 * freeing of the key index.
1566 h->ext_bkt_to_free[ret] = index;
1568 rte_ring_sp_enqueue_elem(h->free_ext_bkts, &index,
1571 __hash_rw_writer_unlock(h);
1576 rte_hash_del_key_with_hash(const struct rte_hash *h,
1577 const void *key, hash_sig_t sig)
1579 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1580 return __rte_hash_del_key_with_hash(h, key, sig);
1584 rte_hash_del_key(const struct rte_hash *h, const void *key)
1586 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1587 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
1591 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
1594 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1596 struct rte_hash_key *k, *keys = h->key_store;
1597 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
1602 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
1611 rte_hash_free_key_with_position(const struct rte_hash *h,
1612 const int32_t position)
1614 /* Key index where key is stored, adding the first dummy index */
1615 uint32_t key_idx = position + 1;
1617 RETURN_IF_TRUE(((h == NULL) || (key_idx == EMPTY_SLOT)), -EINVAL);
1619 unsigned int lcore_id, n_slots;
1620 struct lcore_cache *cached_free_slots;
1621 const uint32_t total_entries = h->use_local_cache ?
1622 h->entries + (RTE_MAX_LCORE - 1) * (LCORE_CACHE_SIZE - 1) + 1
1626 if (key_idx >= total_entries)
1628 if (h->ext_table_support && h->readwrite_concur_lf_support) {
1629 uint32_t index = h->ext_bkt_to_free[position];
1631 /* Recycle empty ext bkt to free list. */
1632 rte_ring_sp_enqueue_elem(h->free_ext_bkts, &index,
1634 h->ext_bkt_to_free[position] = 0;
1638 if (h->use_local_cache) {
1639 lcore_id = rte_lcore_id();
1640 cached_free_slots = &h->local_free_slots[lcore_id];
1641 /* Cache full, need to free it. */
1642 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1643 /* Need to enqueue the free slots in global ring. */
1644 n_slots = rte_ring_mp_enqueue_burst_elem(h->free_slots,
1645 cached_free_slots->objs,
1647 LCORE_CACHE_SIZE, NULL);
1648 RETURN_IF_TRUE((n_slots == 0), -EFAULT);
1649 cached_free_slots->len -= n_slots;
1651 /* Put index of new free slot in cache. */
1652 cached_free_slots->objs[cached_free_slots->len] = key_idx;
1653 cached_free_slots->len++;
1655 rte_ring_sp_enqueue_elem(h->free_slots, &key_idx,
1663 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
1664 const struct rte_hash_bucket *prim_bkt,
1665 const struct rte_hash_bucket *sec_bkt,
1667 enum rte_hash_sig_compare_function sig_cmp_fn)
1671 /* For match mask the first bit of every two bits indicates the match */
1672 switch (sig_cmp_fn) {
1673 #if defined(RTE_MACHINE_CPUFLAG_SSE2)
1674 case RTE_HASH_COMPARE_SSE:
1675 /* Compare all signatures in the bucket */
1676 *prim_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1678 (__m128i const *)prim_bkt->sig_current),
1679 _mm_set1_epi16(sig)));
1680 /* Compare all signatures in the bucket */
1681 *sec_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1683 (__m128i const *)sec_bkt->sig_current),
1684 _mm_set1_epi16(sig)));
1686 #elif defined(RTE_MACHINE_CPUFLAG_NEON)
1687 case RTE_HASH_COMPARE_NEON: {
1688 uint16x8_t vmat, vsig, x;
1689 int16x8_t shift = {-15, -13, -11, -9, -7, -5, -3, -1};
1691 vsig = vld1q_dup_u16((uint16_t const *)&sig);
1692 /* Compare all signatures in the primary bucket */
1693 vmat = vceqq_u16(vsig,
1694 vld1q_u16((uint16_t const *)prim_bkt->sig_current));
1695 x = vshlq_u16(vandq_u16(vmat, vdupq_n_u16(0x8000)), shift);
1696 *prim_hash_matches = (uint32_t)(vaddvq_u16(x));
1697 /* Compare all signatures in the secondary bucket */
1698 vmat = vceqq_u16(vsig,
1699 vld1q_u16((uint16_t const *)sec_bkt->sig_current));
1700 x = vshlq_u16(vandq_u16(vmat, vdupq_n_u16(0x8000)), shift);
1701 *sec_hash_matches = (uint32_t)(vaddvq_u16(x));
1706 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1707 *prim_hash_matches |=
1708 ((sig == prim_bkt->sig_current[i]) << (i << 1));
1709 *sec_hash_matches |=
1710 ((sig == sec_bkt->sig_current[i]) << (i << 1));
1716 __bulk_lookup_l(const struct rte_hash *h, const void **keys,
1717 const struct rte_hash_bucket **primary_bkt,
1718 const struct rte_hash_bucket **secondary_bkt,
1719 uint16_t *sig, int32_t num_keys, int32_t *positions,
1720 uint64_t *hit_mask, void *data[])
1725 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1726 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1727 struct rte_hash_bucket *cur_bkt, *next_bkt;
1729 __hash_rw_reader_lock(h);
1731 /* Compare signatures and prefetch key slot of first hit */
1732 for (i = 0; i < num_keys; i++) {
1733 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1734 primary_bkt[i], secondary_bkt[i],
1735 sig[i], h->sig_cmp_fn);
1737 if (prim_hitmask[i]) {
1738 uint32_t first_hit =
1739 __builtin_ctzl(prim_hitmask[i])
1742 primary_bkt[i]->key_idx[first_hit];
1743 const struct rte_hash_key *key_slot =
1744 (const struct rte_hash_key *)(
1745 (const char *)h->key_store +
1746 key_idx * h->key_entry_size);
1747 rte_prefetch0(key_slot);
1751 if (sec_hitmask[i]) {
1752 uint32_t first_hit =
1753 __builtin_ctzl(sec_hitmask[i])
1756 secondary_bkt[i]->key_idx[first_hit];
1757 const struct rte_hash_key *key_slot =
1758 (const struct rte_hash_key *)(
1759 (const char *)h->key_store +
1760 key_idx * h->key_entry_size);
1761 rte_prefetch0(key_slot);
1765 /* Compare keys, first hits in primary first */
1766 for (i = 0; i < num_keys; i++) {
1767 positions[i] = -ENOENT;
1768 while (prim_hitmask[i]) {
1769 uint32_t hit_index =
1770 __builtin_ctzl(prim_hitmask[i])
1773 primary_bkt[i]->key_idx[hit_index];
1774 const struct rte_hash_key *key_slot =
1775 (const struct rte_hash_key *)(
1776 (const char *)h->key_store +
1777 key_idx * h->key_entry_size);
1780 * If key index is 0, do not compare key,
1781 * as it is checking the dummy slot
1785 key_slot->key, keys[i], h)) {
1787 data[i] = key_slot->pdata;
1790 positions[i] = key_idx - 1;
1793 prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
1796 while (sec_hitmask[i]) {
1797 uint32_t hit_index =
1798 __builtin_ctzl(sec_hitmask[i])
1801 secondary_bkt[i]->key_idx[hit_index];
1802 const struct rte_hash_key *key_slot =
1803 (const struct rte_hash_key *)(
1804 (const char *)h->key_store +
1805 key_idx * h->key_entry_size);
1808 * If key index is 0, do not compare key,
1809 * as it is checking the dummy slot
1814 key_slot->key, keys[i], h)) {
1816 data[i] = key_slot->pdata;
1819 positions[i] = key_idx - 1;
1822 sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
1828 /* all found, do not need to go through ext bkt */
1829 if ((hits == ((1ULL << num_keys) - 1)) || !h->ext_table_support) {
1830 if (hit_mask != NULL)
1832 __hash_rw_reader_unlock(h);
1836 /* need to check ext buckets for match */
1837 for (i = 0; i < num_keys; i++) {
1838 if ((hits & (1ULL << i)) != 0)
1840 next_bkt = secondary_bkt[i]->next;
1841 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
1843 ret = search_one_bucket_l(h, keys[i],
1844 sig[i], &data[i], cur_bkt);
1846 ret = search_one_bucket_l(h, keys[i],
1847 sig[i], NULL, cur_bkt);
1856 __hash_rw_reader_unlock(h);
1858 if (hit_mask != NULL)
1863 __bulk_lookup_lf(const struct rte_hash *h, const void **keys,
1864 const struct rte_hash_bucket **primary_bkt,
1865 const struct rte_hash_bucket **secondary_bkt,
1866 uint16_t *sig, int32_t num_keys, int32_t *positions,
1867 uint64_t *hit_mask, void *data[])
1872 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1873 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1874 struct rte_hash_bucket *cur_bkt, *next_bkt;
1875 uint32_t cnt_b, cnt_a;
1877 for (i = 0; i < num_keys; i++)
1878 positions[i] = -ENOENT;
1881 /* Load the table change counter before the lookup
1882 * starts. Acquire semantics will make sure that
1883 * loads in compare_signatures are not hoisted.
1885 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1888 /* Compare signatures and prefetch key slot of first hit */
1889 for (i = 0; i < num_keys; i++) {
1890 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1891 primary_bkt[i], secondary_bkt[i],
1892 sig[i], h->sig_cmp_fn);
1894 if (prim_hitmask[i]) {
1895 uint32_t first_hit =
1896 __builtin_ctzl(prim_hitmask[i])
1899 primary_bkt[i]->key_idx[first_hit];
1900 const struct rte_hash_key *key_slot =
1901 (const struct rte_hash_key *)(
1902 (const char *)h->key_store +
1903 key_idx * h->key_entry_size);
1904 rte_prefetch0(key_slot);
1908 if (sec_hitmask[i]) {
1909 uint32_t first_hit =
1910 __builtin_ctzl(sec_hitmask[i])
1913 secondary_bkt[i]->key_idx[first_hit];
1914 const struct rte_hash_key *key_slot =
1915 (const struct rte_hash_key *)(
1916 (const char *)h->key_store +
1917 key_idx * h->key_entry_size);
1918 rte_prefetch0(key_slot);
1922 /* Compare keys, first hits in primary first */
1923 for (i = 0; i < num_keys; i++) {
1924 while (prim_hitmask[i]) {
1925 uint32_t hit_index =
1926 __builtin_ctzl(prim_hitmask[i])
1930 &primary_bkt[i]->key_idx[hit_index],
1932 const struct rte_hash_key *key_slot =
1933 (const struct rte_hash_key *)(
1934 (const char *)h->key_store +
1935 key_idx * h->key_entry_size);
1938 * If key index is 0, do not compare key,
1939 * as it is checking the dummy slot
1943 key_slot->key, keys[i], h)) {
1945 data[i] = __atomic_load_n(
1950 positions[i] = key_idx - 1;
1953 prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
1956 while (sec_hitmask[i]) {
1957 uint32_t hit_index =
1958 __builtin_ctzl(sec_hitmask[i])
1962 &secondary_bkt[i]->key_idx[hit_index],
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);
1970 * If key index is 0, do not compare key,
1971 * as it is checking the dummy slot
1976 key_slot->key, keys[i], h)) {
1978 data[i] = __atomic_load_n(
1983 positions[i] = key_idx - 1;
1986 sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
1992 /* all found, do not need to go through ext bkt */
1993 if (hits == ((1ULL << num_keys) - 1)) {
1994 if (hit_mask != NULL)
1998 /* need to check ext buckets for match */
1999 if (h->ext_table_support) {
2000 for (i = 0; i < num_keys; i++) {
2001 if ((hits & (1ULL << i)) != 0)
2003 next_bkt = secondary_bkt[i]->next;
2004 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
2006 ret = search_one_bucket_lf(h,
2010 ret = search_one_bucket_lf(h,
2021 /* The loads of sig_current in compare_signatures
2022 * should not move below the load from tbl_chng_cnt.
2024 __atomic_thread_fence(__ATOMIC_ACQUIRE);
2025 /* Re-read the table change counter to check if the
2026 * table has changed during search. If yes, re-do
2028 * This load should not get hoisted. The load
2029 * acquires on cnt_b, primary key index and secondary
2030 * key index will make sure that it does not get
2033 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
2035 } while (cnt_b != cnt_a);
2037 if (hit_mask != NULL)
2041 #define PREFETCH_OFFSET 4
2043 __bulk_lookup_prefetching_loop(const struct rte_hash *h,
2044 const void **keys, int32_t num_keys,
2046 const struct rte_hash_bucket **primary_bkt,
2047 const struct rte_hash_bucket **secondary_bkt)
2050 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
2051 uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
2052 uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
2054 /* Prefetch first keys */
2055 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
2056 rte_prefetch0(keys[i]);
2059 * Prefetch rest of the keys, calculate primary and
2060 * secondary bucket and prefetch them
2062 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
2063 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
2065 prim_hash[i] = rte_hash_hash(h, keys[i]);
2067 sig[i] = get_short_sig(prim_hash[i]);
2068 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
2069 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
2071 primary_bkt[i] = &h->buckets[prim_index[i]];
2072 secondary_bkt[i] = &h->buckets[sec_index[i]];
2074 rte_prefetch0(primary_bkt[i]);
2075 rte_prefetch0(secondary_bkt[i]);
2078 /* Calculate and prefetch rest of the buckets */
2079 for (; i < num_keys; i++) {
2080 prim_hash[i] = rte_hash_hash(h, keys[i]);
2082 sig[i] = get_short_sig(prim_hash[i]);
2083 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
2084 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
2086 primary_bkt[i] = &h->buckets[prim_index[i]];
2087 secondary_bkt[i] = &h->buckets[sec_index[i]];
2089 rte_prefetch0(primary_bkt[i]);
2090 rte_prefetch0(secondary_bkt[i]);
2096 __rte_hash_lookup_bulk_l(const struct rte_hash *h, const void **keys,
2097 int32_t num_keys, int32_t *positions,
2098 uint64_t *hit_mask, void *data[])
2100 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
2101 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2102 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2104 __bulk_lookup_prefetching_loop(h, keys, num_keys, sig,
2105 primary_bkt, secondary_bkt);
2107 __bulk_lookup_l(h, keys, primary_bkt, secondary_bkt, sig, num_keys,
2108 positions, hit_mask, data);
2112 __rte_hash_lookup_bulk_lf(const struct rte_hash *h, const void **keys,
2113 int32_t num_keys, int32_t *positions,
2114 uint64_t *hit_mask, void *data[])
2116 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
2117 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2118 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2120 __bulk_lookup_prefetching_loop(h, keys, num_keys, sig,
2121 primary_bkt, secondary_bkt);
2123 __bulk_lookup_lf(h, keys, primary_bkt, secondary_bkt, sig, num_keys,
2124 positions, hit_mask, data);
2128 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
2129 int32_t num_keys, int32_t *positions,
2130 uint64_t *hit_mask, void *data[])
2132 if (h->readwrite_concur_lf_support)
2133 __rte_hash_lookup_bulk_lf(h, keys, num_keys, positions,
2136 __rte_hash_lookup_bulk_l(h, keys, num_keys, positions,
2141 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
2142 uint32_t num_keys, int32_t *positions)
2144 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
2145 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2146 (positions == NULL)), -EINVAL);
2148 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
2153 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
2154 uint32_t num_keys, uint64_t *hit_mask, void *data[])
2156 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
2157 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2158 (hit_mask == NULL)), -EINVAL);
2160 int32_t positions[num_keys];
2162 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
2164 /* Return number of hits */
2165 return __builtin_popcountl(*hit_mask);
2170 __rte_hash_lookup_with_hash_bulk_l(const struct rte_hash *h,
2171 const void **keys, hash_sig_t *prim_hash,
2172 int32_t num_keys, int32_t *positions,
2173 uint64_t *hit_mask, void *data[])
2176 uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
2177 uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
2178 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
2179 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2180 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2183 * Prefetch keys, calculate primary and
2184 * secondary bucket and prefetch them
2186 for (i = 0; i < num_keys; i++) {
2187 rte_prefetch0(keys[i]);
2189 sig[i] = get_short_sig(prim_hash[i]);
2190 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
2191 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
2193 primary_bkt[i] = &h->buckets[prim_index[i]];
2194 secondary_bkt[i] = &h->buckets[sec_index[i]];
2196 rte_prefetch0(primary_bkt[i]);
2197 rte_prefetch0(secondary_bkt[i]);
2200 __bulk_lookup_l(h, keys, primary_bkt, secondary_bkt, sig, num_keys,
2201 positions, hit_mask, data);
2205 __rte_hash_lookup_with_hash_bulk_lf(const struct rte_hash *h,
2206 const void **keys, hash_sig_t *prim_hash,
2207 int32_t num_keys, int32_t *positions,
2208 uint64_t *hit_mask, void *data[])
2211 uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
2212 uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
2213 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
2214 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2215 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
2218 * Prefetch keys, calculate primary and
2219 * secondary bucket and prefetch them
2221 for (i = 0; i < num_keys; i++) {
2222 rte_prefetch0(keys[i]);
2224 sig[i] = get_short_sig(prim_hash[i]);
2225 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
2226 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
2228 primary_bkt[i] = &h->buckets[prim_index[i]];
2229 secondary_bkt[i] = &h->buckets[sec_index[i]];
2231 rte_prefetch0(primary_bkt[i]);
2232 rte_prefetch0(secondary_bkt[i]);
2235 __bulk_lookup_lf(h, keys, primary_bkt, secondary_bkt, sig, num_keys,
2236 positions, hit_mask, data);
2240 __rte_hash_lookup_with_hash_bulk(const struct rte_hash *h, const void **keys,
2241 hash_sig_t *prim_hash, int32_t num_keys,
2242 int32_t *positions, uint64_t *hit_mask, void *data[])
2244 if (h->readwrite_concur_lf_support)
2245 __rte_hash_lookup_with_hash_bulk_lf(h, keys, prim_hash,
2246 num_keys, positions, hit_mask, data);
2248 __rte_hash_lookup_with_hash_bulk_l(h, keys, prim_hash,
2249 num_keys, positions, hit_mask, data);
2253 rte_hash_lookup_with_hash_bulk(const struct rte_hash *h, const void **keys,
2254 hash_sig_t *sig, uint32_t num_keys, int32_t *positions)
2256 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) ||
2257 (sig == NULL) || (num_keys == 0) ||
2258 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2259 (positions == NULL)), -EINVAL);
2261 __rte_hash_lookup_with_hash_bulk(h, keys, sig, num_keys,
2262 positions, NULL, NULL);
2267 rte_hash_lookup_with_hash_bulk_data(const struct rte_hash *h,
2268 const void **keys, hash_sig_t *sig,
2269 uint32_t num_keys, uint64_t *hit_mask, void *data[])
2271 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) ||
2272 (sig == NULL) || (num_keys == 0) ||
2273 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
2274 (hit_mask == NULL)), -EINVAL);
2276 int32_t positions[num_keys];
2278 __rte_hash_lookup_with_hash_bulk(h, keys, sig, num_keys,
2279 positions, hit_mask, data);
2281 /* Return number of hits */
2282 return __builtin_popcountl(*hit_mask);
2286 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
2288 uint32_t bucket_idx, idx, position;
2289 struct rte_hash_key *next_key;
2291 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
2293 const uint32_t total_entries_main = h->num_buckets *
2294 RTE_HASH_BUCKET_ENTRIES;
2295 const uint32_t total_entries = total_entries_main << 1;
2297 /* Out of bounds of all buckets (both main table and ext table) */
2298 if (*next >= total_entries_main)
2301 /* Calculate bucket and index of current iterator */
2302 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
2303 idx = *next % RTE_HASH_BUCKET_ENTRIES;
2305 /* If current position is empty, go to the next one */
2306 while ((position = __atomic_load_n(&h->buckets[bucket_idx].key_idx[idx],
2307 __ATOMIC_ACQUIRE)) == EMPTY_SLOT) {
2310 if (*next == total_entries_main)
2312 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
2313 idx = *next % RTE_HASH_BUCKET_ENTRIES;
2316 __hash_rw_reader_lock(h);
2317 next_key = (struct rte_hash_key *) ((char *)h->key_store +
2318 position * h->key_entry_size);
2319 /* Return key and data */
2320 *key = next_key->key;
2321 *data = next_key->pdata;
2323 __hash_rw_reader_unlock(h);
2325 /* Increment iterator */
2328 return position - 1;
2330 /* Begin to iterate extendable buckets */
2332 /* Out of total bound or if ext bucket feature is not enabled */
2333 if (*next >= total_entries || !h->ext_table_support)
2336 bucket_idx = (*next - total_entries_main) / RTE_HASH_BUCKET_ENTRIES;
2337 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
2339 while ((position = h->buckets_ext[bucket_idx].key_idx[idx]) == EMPTY_SLOT) {
2341 if (*next == total_entries)
2343 bucket_idx = (*next - total_entries_main) /
2344 RTE_HASH_BUCKET_ENTRIES;
2345 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
2347 __hash_rw_reader_lock(h);
2348 next_key = (struct rte_hash_key *) ((char *)h->key_store +
2349 position * h->key_entry_size);
2350 /* Return key and data */
2351 *key = next_key->key;
2352 *data = next_key->pdata;
2354 __hash_rw_reader_unlock(h);
2356 /* Increment iterator */
2358 return position - 1;