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_memcpy.h>
17 #include <rte_prefetch.h>
18 #include <rte_branch_prediction.h>
19 #include <rte_malloc.h>
21 #include <rte_eal_memconfig.h>
22 #include <rte_per_lcore.h>
23 #include <rte_errno.h>
24 #include <rte_string_fns.h>
25 #include <rte_cpuflags.h>
26 #include <rte_rwlock.h>
27 #include <rte_spinlock.h>
29 #include <rte_compat.h>
32 #include "rte_cuckoo_hash.h"
34 #define FOR_EACH_BUCKET(CURRENT_BKT, START_BUCKET) \
35 for (CURRENT_BKT = START_BUCKET; \
36 CURRENT_BKT != NULL; \
37 CURRENT_BKT = CURRENT_BKT->next)
39 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
41 static struct rte_tailq_elem rte_hash_tailq = {
44 EAL_REGISTER_TAILQ(rte_hash_tailq)
47 rte_hash_find_existing(const char *name)
49 struct rte_hash *h = NULL;
50 struct rte_tailq_entry *te;
51 struct rte_hash_list *hash_list;
53 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
55 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
56 TAILQ_FOREACH(te, hash_list, next) {
57 h = (struct rte_hash *) te->data;
58 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
61 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
70 static inline struct rte_hash_bucket *
71 rte_hash_get_last_bkt(struct rte_hash_bucket *lst_bkt)
73 while (lst_bkt->next != NULL)
74 lst_bkt = lst_bkt->next;
78 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
80 h->cmp_jump_table_idx = KEY_CUSTOM;
81 h->rte_hash_custom_cmp_eq = func;
85 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
87 if (h->cmp_jump_table_idx == KEY_CUSTOM)
88 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
90 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
94 * We use higher 16 bits of hash as the signature value stored in table.
95 * We use the lower bits for the primary bucket
96 * location. Then we XOR primary bucket location and the signature
97 * to get the secondary bucket location. This is same as
98 * proposed in Bin Fan, et al's paper
99 * "MemC3: Compact and Concurrent MemCache with Dumber Caching and
100 * Smarter Hashing". The benefit to use
101 * XOR is that one could derive the alternative bucket location
102 * by only using the current bucket location and the signature.
104 static inline uint16_t
105 get_short_sig(const hash_sig_t hash)
110 static inline uint32_t
111 get_prim_bucket_index(const struct rte_hash *h, const hash_sig_t hash)
113 return hash & h->bucket_bitmask;
116 static inline uint32_t
117 get_alt_bucket_index(const struct rte_hash *h,
118 uint32_t cur_bkt_idx, uint16_t sig)
120 return (cur_bkt_idx ^ sig) & h->bucket_bitmask;
124 rte_hash_create(const struct rte_hash_parameters *params)
126 struct rte_hash *h = NULL;
127 struct rte_tailq_entry *te = NULL;
128 struct rte_hash_list *hash_list;
129 struct rte_ring *r = NULL;
130 struct rte_ring *r_ext = NULL;
131 char hash_name[RTE_HASH_NAMESIZE];
133 void *buckets = NULL;
134 void *buckets_ext = NULL;
135 char ring_name[RTE_RING_NAMESIZE];
136 char ext_ring_name[RTE_RING_NAMESIZE];
137 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 *tbl_chng_cnt = NULL;
145 unsigned int readwrite_concur_lf_support = 0;
147 rte_hash_function default_hash_func = (rte_hash_function)rte_jhash;
149 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
151 if (params == NULL) {
152 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
156 /* Check for valid parameters */
157 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
158 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
159 (params->key_len == 0)) {
161 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
165 /* Validate correct usage of extra options */
166 if ((params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) &&
167 (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF)) {
169 RTE_LOG(ERR, HASH, "rte_hash_create: choose rw concurrency or "
170 "rw concurrency lock free\n");
174 if ((params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF) &&
175 (params->extra_flag & RTE_HASH_EXTRA_FLAGS_EXT_TABLE)) {
177 RTE_LOG(ERR, HASH, "rte_hash_create: extendable bucket "
178 "feature not supported with rw concurrency "
183 /* Check extra flags field to check extra options. */
184 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
185 hw_trans_mem_support = 1;
187 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
189 writer_takes_lock = 1;
192 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY) {
193 readwrite_concur_support = 1;
194 writer_takes_lock = 1;
197 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_EXT_TABLE)
198 ext_table_support = 1;
200 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL)
203 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF) {
204 readwrite_concur_lf_support = 1;
205 /* Enable not freeing internal memory/index on delete */
209 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
212 * Increase number of slots by total number of indices
213 * that can be stored in the lcore caches
214 * except for the first cache
216 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
217 (LCORE_CACHE_SIZE - 1) + 1;
219 num_key_slots = params->entries + 1;
221 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
222 /* Create ring (Dummy slot index is not enqueued) */
223 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
224 params->socket_id, 0);
226 RTE_LOG(ERR, HASH, "memory allocation failed\n");
230 const uint32_t num_buckets = rte_align32pow2(params->entries) /
231 RTE_HASH_BUCKET_ENTRIES;
233 /* Create ring for extendable buckets. */
234 if (ext_table_support) {
235 snprintf(ext_ring_name, sizeof(ext_ring_name), "HT_EXT_%s",
237 r_ext = rte_ring_create(ext_ring_name,
238 rte_align32pow2(num_buckets + 1),
239 params->socket_id, 0);
242 RTE_LOG(ERR, HASH, "ext buckets memory allocation "
248 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
250 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
252 /* guarantee there's no existing: this is normally already checked
253 * by ring creation above */
254 TAILQ_FOREACH(te, hash_list, next) {
255 h = (struct rte_hash *) te->data;
256 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
266 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
268 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
272 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
273 RTE_CACHE_LINE_SIZE, params->socket_id);
276 RTE_LOG(ERR, HASH, "memory allocation failed\n");
280 buckets = rte_zmalloc_socket(NULL,
281 num_buckets * sizeof(struct rte_hash_bucket),
282 RTE_CACHE_LINE_SIZE, params->socket_id);
284 if (buckets == NULL) {
285 RTE_LOG(ERR, HASH, "buckets memory allocation failed\n");
289 /* Allocate same number of extendable buckets */
290 if (ext_table_support) {
291 buckets_ext = rte_zmalloc_socket(NULL,
292 num_buckets * sizeof(struct rte_hash_bucket),
293 RTE_CACHE_LINE_SIZE, params->socket_id);
294 if (buckets_ext == NULL) {
295 RTE_LOG(ERR, HASH, "ext buckets memory allocation "
299 /* Populate ext bkt ring. We reserve 0 similar to the
300 * key-data slot, just in case in future we want to
301 * use bucket index for the linked list and 0 means NULL
304 for (i = 1; i <= num_buckets; i++)
305 rte_ring_sp_enqueue(r_ext, (void *)((uintptr_t) i));
308 const uint32_t key_entry_size =
309 RTE_ALIGN(sizeof(struct rte_hash_key) + params->key_len,
311 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
313 k = rte_zmalloc_socket(NULL, key_tbl_size,
314 RTE_CACHE_LINE_SIZE, params->socket_id);
317 RTE_LOG(ERR, HASH, "memory allocation failed\n");
321 tbl_chng_cnt = rte_zmalloc_socket(NULL, sizeof(uint32_t),
322 RTE_CACHE_LINE_SIZE, params->socket_id);
324 if (tbl_chng_cnt == NULL) {
325 RTE_LOG(ERR, HASH, "memory allocation failed\n");
330 * If x86 architecture is used, select appropriate compare function,
331 * which may use x86 intrinsics, otherwise use memcmp
333 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
334 /* Select function to compare keys */
335 switch (params->key_len) {
337 h->cmp_jump_table_idx = KEY_16_BYTES;
340 h->cmp_jump_table_idx = KEY_32_BYTES;
343 h->cmp_jump_table_idx = KEY_48_BYTES;
346 h->cmp_jump_table_idx = KEY_64_BYTES;
349 h->cmp_jump_table_idx = KEY_80_BYTES;
352 h->cmp_jump_table_idx = KEY_96_BYTES;
355 h->cmp_jump_table_idx = KEY_112_BYTES;
358 h->cmp_jump_table_idx = KEY_128_BYTES;
361 /* If key is not multiple of 16, use generic memcmp */
362 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
365 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
368 if (use_local_cache) {
369 h->local_free_slots = rte_zmalloc_socket(NULL,
370 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
371 RTE_CACHE_LINE_SIZE, params->socket_id);
374 /* Default hash function */
375 #if defined(RTE_ARCH_X86)
376 default_hash_func = (rte_hash_function)rte_hash_crc;
377 #elif defined(RTE_ARCH_ARM64)
378 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
379 default_hash_func = (rte_hash_function)rte_hash_crc;
381 /* Setup hash context */
382 snprintf(h->name, sizeof(h->name), "%s", params->name);
383 h->entries = params->entries;
384 h->key_len = params->key_len;
385 h->key_entry_size = key_entry_size;
386 h->hash_func_init_val = params->hash_func_init_val;
388 h->num_buckets = num_buckets;
389 h->bucket_bitmask = h->num_buckets - 1;
390 h->buckets = buckets;
391 h->buckets_ext = buckets_ext;
392 h->free_ext_bkts = r_ext;
393 h->hash_func = (params->hash_func == NULL) ?
394 default_hash_func : params->hash_func;
397 h->tbl_chng_cnt = tbl_chng_cnt;
398 *h->tbl_chng_cnt = 0;
399 h->hw_trans_mem_support = hw_trans_mem_support;
400 h->use_local_cache = use_local_cache;
401 h->readwrite_concur_support = readwrite_concur_support;
402 h->ext_table_support = ext_table_support;
403 h->writer_takes_lock = writer_takes_lock;
404 h->no_free_on_del = no_free_on_del;
405 h->readwrite_concur_lf_support = readwrite_concur_lf_support;
407 #if defined(RTE_ARCH_X86)
408 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
409 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
412 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
414 /* Writer threads need to take the lock when:
415 * 1) RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY is enabled OR
416 * 2) RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD is enabled
418 if (h->writer_takes_lock) {
419 h->readwrite_lock = rte_malloc(NULL, sizeof(rte_rwlock_t),
420 RTE_CACHE_LINE_SIZE);
421 if (h->readwrite_lock == NULL)
424 rte_rwlock_init(h->readwrite_lock);
427 /* Populate free slots ring. Entry zero is reserved for key misses. */
428 for (i = 1; i < num_key_slots; i++)
429 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
431 te->data = (void *) h;
432 TAILQ_INSERT_TAIL(hash_list, te, next);
433 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
437 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
440 rte_ring_free(r_ext);
444 rte_free(buckets_ext);
446 rte_free(tbl_chng_cnt);
451 rte_hash_free(struct rte_hash *h)
453 struct rte_tailq_entry *te;
454 struct rte_hash_list *hash_list;
459 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
461 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
463 /* find out tailq entry */
464 TAILQ_FOREACH(te, hash_list, next) {
465 if (te->data == (void *) h)
470 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
474 TAILQ_REMOVE(hash_list, te, next);
476 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
478 if (h->use_local_cache)
479 rte_free(h->local_free_slots);
480 if (h->writer_takes_lock)
481 rte_free(h->readwrite_lock);
482 rte_ring_free(h->free_slots);
483 rte_ring_free(h->free_ext_bkts);
484 rte_free(h->key_store);
485 rte_free(h->buckets);
486 rte_free(h->buckets_ext);
487 rte_free(h->tbl_chng_cnt);
493 rte_hash_hash(const struct rte_hash *h, const void *key)
495 /* calc hash result by key */
496 return h->hash_func(key, h->key_len, h->hash_func_init_val);
500 rte_hash_count(const struct rte_hash *h)
502 uint32_t tot_ring_cnt, cached_cnt = 0;
508 if (h->use_local_cache) {
509 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
510 (LCORE_CACHE_SIZE - 1);
511 for (i = 0; i < RTE_MAX_LCORE; i++)
512 cached_cnt += h->local_free_slots[i].len;
514 ret = tot_ring_cnt - rte_ring_count(h->free_slots) -
517 tot_ring_cnt = h->entries;
518 ret = tot_ring_cnt - rte_ring_count(h->free_slots);
523 /* Read write locks implemented using rte_rwlock */
525 __hash_rw_writer_lock(const struct rte_hash *h)
527 if (h->writer_takes_lock && h->hw_trans_mem_support)
528 rte_rwlock_write_lock_tm(h->readwrite_lock);
529 else if (h->writer_takes_lock)
530 rte_rwlock_write_lock(h->readwrite_lock);
534 __hash_rw_reader_lock(const struct rte_hash *h)
536 if (h->readwrite_concur_support && h->hw_trans_mem_support)
537 rte_rwlock_read_lock_tm(h->readwrite_lock);
538 else if (h->readwrite_concur_support)
539 rte_rwlock_read_lock(h->readwrite_lock);
543 __hash_rw_writer_unlock(const struct rte_hash *h)
545 if (h->writer_takes_lock && h->hw_trans_mem_support)
546 rte_rwlock_write_unlock_tm(h->readwrite_lock);
547 else if (h->writer_takes_lock)
548 rte_rwlock_write_unlock(h->readwrite_lock);
552 __hash_rw_reader_unlock(const struct rte_hash *h)
554 if (h->readwrite_concur_support && h->hw_trans_mem_support)
555 rte_rwlock_read_unlock_tm(h->readwrite_lock);
556 else if (h->readwrite_concur_support)
557 rte_rwlock_read_unlock(h->readwrite_lock);
561 rte_hash_reset(struct rte_hash *h)
564 uint32_t tot_ring_cnt, i;
569 __hash_rw_writer_lock(h);
570 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
571 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
572 *h->tbl_chng_cnt = 0;
574 /* clear the free ring */
575 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
578 /* clear free extendable bucket ring and memory */
579 if (h->ext_table_support) {
580 memset(h->buckets_ext, 0, h->num_buckets *
581 sizeof(struct rte_hash_bucket));
582 while (rte_ring_dequeue(h->free_ext_bkts, &ptr) == 0)
586 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
587 if (h->use_local_cache)
588 tot_ring_cnt = h->entries + (RTE_MAX_LCORE - 1) *
589 (LCORE_CACHE_SIZE - 1);
591 tot_ring_cnt = h->entries;
593 for (i = 1; i < tot_ring_cnt + 1; i++)
594 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
596 /* Repopulate the free ext bkt ring. */
597 if (h->ext_table_support) {
598 for (i = 1; i <= h->num_buckets; i++)
599 rte_ring_sp_enqueue(h->free_ext_bkts,
600 (void *)((uintptr_t) i));
603 if (h->use_local_cache) {
604 /* Reset local caches per lcore */
605 for (i = 0; i < RTE_MAX_LCORE; i++)
606 h->local_free_slots[i].len = 0;
608 __hash_rw_writer_unlock(h);
612 * Function called to enqueue back an index in the cache/ring,
613 * as slot has not being used and it can be used in the
614 * next addition attempt.
617 enqueue_slot_back(const struct rte_hash *h,
618 struct lcore_cache *cached_free_slots,
621 if (h->use_local_cache) {
622 cached_free_slots->objs[cached_free_slots->len] = slot_id;
623 cached_free_slots->len++;
625 rte_ring_sp_enqueue(h->free_slots, slot_id);
628 /* Search a key from bucket and update its data.
629 * Writer holds the lock before calling this.
631 static inline int32_t
632 search_and_update(const struct rte_hash *h, void *data, const void *key,
633 struct rte_hash_bucket *bkt, uint16_t sig)
636 struct rte_hash_key *k, *keys = h->key_store;
638 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
639 if (bkt->sig_current[i] == sig) {
640 k = (struct rte_hash_key *) ((char *)keys +
641 bkt->key_idx[i] * h->key_entry_size);
642 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
643 /* 'pdata' acts as the synchronization point
644 * when an existing hash entry is updated.
645 * Key is not updated in this case.
647 __atomic_store_n(&k->pdata,
651 * Return index where key is stored,
652 * subtracting the first dummy index
654 return bkt->key_idx[i] - 1;
661 /* Only tries to insert at one bucket (@prim_bkt) without trying to push
663 * return 1 if matching existing key, return 0 if succeeds, return -1 for no
666 static inline int32_t
667 rte_hash_cuckoo_insert_mw(const struct rte_hash *h,
668 struct rte_hash_bucket *prim_bkt,
669 struct rte_hash_bucket *sec_bkt,
670 const struct rte_hash_key *key, void *data,
671 uint16_t sig, uint32_t new_idx,
675 struct rte_hash_bucket *cur_bkt;
678 __hash_rw_writer_lock(h);
679 /* Check if key was inserted after last check but before this
680 * protected region in case of inserting duplicated keys.
682 ret = search_and_update(h, data, key, prim_bkt, sig);
684 __hash_rw_writer_unlock(h);
689 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
690 ret = search_and_update(h, data, key, cur_bkt, sig);
692 __hash_rw_writer_unlock(h);
698 /* Insert new entry if there is room in the primary
701 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
702 /* Check if slot is available */
703 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
704 prim_bkt->sig_current[i] = sig;
705 /* Key can be of arbitrary length, so it is
706 * not possible to store it atomically.
707 * Hence the new key element's memory stores
708 * (key as well as data) should be complete
709 * before it is referenced.
711 __atomic_store_n(&prim_bkt->key_idx[i],
717 __hash_rw_writer_unlock(h);
719 if (i != RTE_HASH_BUCKET_ENTRIES)
726 /* Shift buckets along provided cuckoo_path (@leaf and @leaf_slot) and fill
727 * the path head with new entry (sig, alt_hash, new_idx)
728 * return 1 if matched key found, return -1 if cuckoo path invalided and fail,
729 * return 0 if succeeds.
732 rte_hash_cuckoo_move_insert_mw(const struct rte_hash *h,
733 struct rte_hash_bucket *bkt,
734 struct rte_hash_bucket *alt_bkt,
735 const struct rte_hash_key *key, void *data,
736 struct queue_node *leaf, uint32_t leaf_slot,
737 uint16_t sig, uint32_t new_idx,
740 uint32_t prev_alt_bkt_idx;
741 struct rte_hash_bucket *cur_bkt;
742 struct queue_node *prev_node, *curr_node = leaf;
743 struct rte_hash_bucket *prev_bkt, *curr_bkt = leaf->bkt;
744 uint32_t prev_slot, curr_slot = leaf_slot;
747 __hash_rw_writer_lock(h);
749 /* In case empty slot was gone before entering protected region */
750 if (curr_bkt->key_idx[curr_slot] != EMPTY_SLOT) {
751 __hash_rw_writer_unlock(h);
755 /* Check if key was inserted after last check but before this
758 ret = search_and_update(h, data, key, bkt, sig);
760 __hash_rw_writer_unlock(h);
765 FOR_EACH_BUCKET(cur_bkt, alt_bkt) {
766 ret = search_and_update(h, data, key, cur_bkt, sig);
768 __hash_rw_writer_unlock(h);
774 while (likely(curr_node->prev != NULL)) {
775 prev_node = curr_node->prev;
776 prev_bkt = prev_node->bkt;
777 prev_slot = curr_node->prev_slot;
779 prev_alt_bkt_idx = get_alt_bucket_index(h,
780 prev_node->cur_bkt_idx,
781 prev_bkt->sig_current[prev_slot]);
783 if (unlikely(&h->buckets[prev_alt_bkt_idx]
785 /* revert it to empty, otherwise duplicated keys */
786 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
789 __hash_rw_writer_unlock(h);
793 if (h->readwrite_concur_lf_support) {
794 /* Inform the previous move. The current move need
795 * not be informed now as the current bucket entry
796 * is present in both primary and secondary.
797 * Since there is one writer, load acquires on
798 * tbl_chng_cnt are not required.
800 __atomic_store_n(h->tbl_chng_cnt,
801 *h->tbl_chng_cnt + 1,
803 /* The stores to sig_alt and sig_current should not
804 * move above the store to tbl_chng_cnt.
806 __atomic_thread_fence(__ATOMIC_RELEASE);
809 /* Need to swap current/alt sig to allow later
810 * Cuckoo insert to move elements back to its
811 * primary bucket if available
813 curr_bkt->sig_current[curr_slot] =
814 prev_bkt->sig_current[prev_slot];
815 /* Release the updated bucket entry */
816 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
817 prev_bkt->key_idx[prev_slot],
820 curr_slot = prev_slot;
821 curr_node = prev_node;
822 curr_bkt = curr_node->bkt;
825 if (h->readwrite_concur_lf_support) {
826 /* Inform the previous move. The current move need
827 * not be informed now as the current bucket entry
828 * is present in both primary and secondary.
829 * Since there is one writer, load acquires on
830 * tbl_chng_cnt are not required.
832 __atomic_store_n(h->tbl_chng_cnt,
833 *h->tbl_chng_cnt + 1,
835 /* The stores to sig_alt and sig_current should not
836 * move above the store to tbl_chng_cnt.
838 __atomic_thread_fence(__ATOMIC_RELEASE);
841 curr_bkt->sig_current[curr_slot] = sig;
842 /* Release the new bucket entry */
843 __atomic_store_n(&curr_bkt->key_idx[curr_slot],
847 __hash_rw_writer_unlock(h);
854 * Make space for new key, using bfs Cuckoo Search and Multi-Writer safe
858 rte_hash_cuckoo_make_space_mw(const struct rte_hash *h,
859 struct rte_hash_bucket *bkt,
860 struct rte_hash_bucket *sec_bkt,
861 const struct rte_hash_key *key, void *data,
862 uint16_t sig, uint32_t bucket_idx,
863 uint32_t new_idx, int32_t *ret_val)
866 struct queue_node queue[RTE_HASH_BFS_QUEUE_MAX_LEN];
867 struct queue_node *tail, *head;
868 struct rte_hash_bucket *curr_bkt, *alt_bkt;
869 uint32_t cur_idx, alt_idx;
875 tail->prev_slot = -1;
876 tail->cur_bkt_idx = bucket_idx;
878 /* Cuckoo bfs Search */
879 while (likely(tail != head && head <
880 queue + RTE_HASH_BFS_QUEUE_MAX_LEN -
881 RTE_HASH_BUCKET_ENTRIES)) {
882 curr_bkt = tail->bkt;
883 cur_idx = tail->cur_bkt_idx;
884 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
885 if (curr_bkt->key_idx[i] == EMPTY_SLOT) {
886 int32_t ret = rte_hash_cuckoo_move_insert_mw(h,
887 bkt, sec_bkt, key, data,
890 if (likely(ret != -1))
894 /* Enqueue new node and keep prev node info */
895 alt_idx = get_alt_bucket_index(h, cur_idx,
896 curr_bkt->sig_current[i]);
897 alt_bkt = &(h->buckets[alt_idx]);
899 head->cur_bkt_idx = alt_idx;
910 static inline int32_t
911 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
912 hash_sig_t sig, void *data)
915 uint32_t prim_bucket_idx, sec_bucket_idx;
916 struct rte_hash_bucket *prim_bkt, *sec_bkt, *cur_bkt;
917 struct rte_hash_key *new_k, *keys = h->key_store;
918 void *slot_id = NULL;
919 void *ext_bkt_id = NULL;
920 uint32_t new_idx, bkt_id;
925 struct lcore_cache *cached_free_slots = NULL;
927 struct rte_hash_bucket *last;
929 short_sig = get_short_sig(sig);
930 prim_bucket_idx = get_prim_bucket_index(h, sig);
931 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
932 prim_bkt = &h->buckets[prim_bucket_idx];
933 sec_bkt = &h->buckets[sec_bucket_idx];
934 rte_prefetch0(prim_bkt);
935 rte_prefetch0(sec_bkt);
937 /* Check if key is already inserted in primary location */
938 __hash_rw_writer_lock(h);
939 ret = search_and_update(h, data, key, prim_bkt, short_sig);
941 __hash_rw_writer_unlock(h);
945 /* Check if key is already inserted in secondary location */
946 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
947 ret = search_and_update(h, data, key, cur_bkt, short_sig);
949 __hash_rw_writer_unlock(h);
954 __hash_rw_writer_unlock(h);
956 /* Did not find a match, so get a new slot for storing the new key */
957 if (h->use_local_cache) {
958 lcore_id = rte_lcore_id();
959 cached_free_slots = &h->local_free_slots[lcore_id];
960 /* Try to get a free slot from the local cache */
961 if (cached_free_slots->len == 0) {
962 /* Need to get another burst of free slots from global ring */
963 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
964 cached_free_slots->objs,
965 LCORE_CACHE_SIZE, NULL);
970 cached_free_slots->len += n_slots;
973 /* Get a free slot from the local cache */
974 cached_free_slots->len--;
975 slot_id = cached_free_slots->objs[cached_free_slots->len];
977 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0) {
982 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
983 new_idx = (uint32_t)((uintptr_t) slot_id);
985 rte_memcpy(new_k->key, key, h->key_len);
986 /* Key can be of arbitrary length, so it is not possible to store
987 * it atomically. Hence the new key element's memory stores
988 * (key as well as data) should be complete before it is referenced.
989 * 'pdata' acts as the synchronization point when an existing hash
992 __atomic_store_n(&new_k->pdata,
996 /* Find an empty slot and insert */
997 ret = rte_hash_cuckoo_insert_mw(h, prim_bkt, sec_bkt, key, data,
998 short_sig, new_idx, &ret_val);
1001 else if (ret == 1) {
1002 enqueue_slot_back(h, cached_free_slots, slot_id);
1006 /* Primary bucket full, need to make space for new entry */
1007 ret = rte_hash_cuckoo_make_space_mw(h, prim_bkt, sec_bkt, key, data,
1008 short_sig, prim_bucket_idx, new_idx, &ret_val);
1011 else if (ret == 1) {
1012 enqueue_slot_back(h, cached_free_slots, slot_id);
1016 /* Also search secondary bucket to get better occupancy */
1017 ret = rte_hash_cuckoo_make_space_mw(h, sec_bkt, prim_bkt, key, data,
1018 short_sig, sec_bucket_idx, new_idx, &ret_val);
1022 else if (ret == 1) {
1023 enqueue_slot_back(h, cached_free_slots, slot_id);
1027 /* if ext table not enabled, we failed the insertion */
1028 if (!h->ext_table_support) {
1029 enqueue_slot_back(h, cached_free_slots, slot_id);
1033 /* Now we need to go through the extendable bucket. Protection is needed
1034 * to protect all extendable bucket processes.
1036 __hash_rw_writer_lock(h);
1037 /* We check for duplicates again since could be inserted before the lock */
1038 ret = search_and_update(h, data, key, prim_bkt, short_sig);
1040 enqueue_slot_back(h, cached_free_slots, slot_id);
1044 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1045 ret = search_and_update(h, data, key, cur_bkt, short_sig);
1047 enqueue_slot_back(h, cached_free_slots, slot_id);
1052 /* Search sec and ext buckets to find an empty entry to insert. */
1053 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1054 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1055 /* Check if slot is available */
1056 if (likely(cur_bkt->key_idx[i] == EMPTY_SLOT)) {
1057 cur_bkt->sig_current[i] = short_sig;
1058 cur_bkt->key_idx[i] = new_idx;
1059 __hash_rw_writer_unlock(h);
1065 /* Failed to get an empty entry from extendable buckets. Link a new
1066 * extendable bucket. We first get a free bucket from ring.
1068 if (rte_ring_sc_dequeue(h->free_ext_bkts, &ext_bkt_id) != 0) {
1073 bkt_id = (uint32_t)((uintptr_t)ext_bkt_id) - 1;
1074 /* Use the first location of the new bucket */
1075 (h->buckets_ext[bkt_id]).sig_current[0] = short_sig;
1076 (h->buckets_ext[bkt_id]).key_idx[0] = new_idx;
1077 /* Link the new bucket to sec bucket linked list */
1078 last = rte_hash_get_last_bkt(sec_bkt);
1079 last->next = &h->buckets_ext[bkt_id];
1080 __hash_rw_writer_unlock(h);
1084 __hash_rw_writer_unlock(h);
1090 rte_hash_add_key_with_hash(const struct rte_hash *h,
1091 const void *key, hash_sig_t sig)
1093 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1094 return __rte_hash_add_key_with_hash(h, key, sig, 0);
1098 rte_hash_add_key(const struct rte_hash *h, const void *key)
1100 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1101 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
1105 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
1106 const void *key, hash_sig_t sig, void *data)
1110 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1111 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
1119 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
1123 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1125 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
1132 /* Search one bucket to find the match key */
1133 static inline int32_t
1134 search_one_bucket(const struct rte_hash *h, const void *key, uint16_t sig,
1135 void **data, const struct rte_hash_bucket *bkt)
1140 struct rte_hash_key *k, *keys = h->key_store;
1142 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1143 key_idx = __atomic_load_n(&bkt->key_idx[i],
1145 if (bkt->sig_current[i] == sig && key_idx != EMPTY_SLOT) {
1146 k = (struct rte_hash_key *) ((char *)keys +
1147 key_idx * h->key_entry_size);
1148 pdata = __atomic_load_n(&k->pdata,
1151 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1155 * Return index where key is stored,
1156 * subtracting the first dummy index
1165 static inline int32_t
1166 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
1167 hash_sig_t sig, void **data)
1169 uint32_t prim_bucket_idx, sec_bucket_idx;
1170 struct rte_hash_bucket *bkt, *cur_bkt;
1171 uint32_t cnt_b, cnt_a;
1175 short_sig = get_short_sig(sig);
1176 prim_bucket_idx = get_prim_bucket_index(h, sig);
1177 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1179 __hash_rw_reader_lock(h);
1182 /* Load the table change counter before the lookup
1183 * starts. Acquire semantics will make sure that
1184 * loads in search_one_bucket are not hoisted.
1186 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1189 /* Check if key is in primary location */
1190 bkt = &h->buckets[prim_bucket_idx];
1191 ret = search_one_bucket(h, key, short_sig, data, bkt);
1193 __hash_rw_reader_unlock(h);
1196 /* Calculate secondary hash */
1197 bkt = &h->buckets[sec_bucket_idx];
1199 /* Check if key is in secondary location */
1200 FOR_EACH_BUCKET(cur_bkt, bkt) {
1201 ret = search_one_bucket(h, key, short_sig,
1204 __hash_rw_reader_unlock(h);
1209 /* The loads of sig_current in search_one_bucket
1210 * should not move below the load from tbl_chng_cnt.
1212 __atomic_thread_fence(__ATOMIC_ACQUIRE);
1213 /* Re-read the table change counter to check if the
1214 * table has changed during search. If yes, re-do
1216 * This load should not get hoisted. The load
1217 * acquires on cnt_b, key index in primary bucket
1218 * and key index in secondary bucket will make sure
1219 * that it does not get hoisted.
1221 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
1223 } while (cnt_b != cnt_a);
1225 __hash_rw_reader_unlock(h);
1231 rte_hash_lookup_with_hash(const struct rte_hash *h,
1232 const void *key, hash_sig_t sig)
1234 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1235 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
1239 rte_hash_lookup(const struct rte_hash *h, const void *key)
1241 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1242 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
1246 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
1247 const void *key, hash_sig_t sig, void **data)
1249 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1250 return __rte_hash_lookup_with_hash(h, key, sig, data);
1254 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
1256 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1257 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
1261 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
1263 unsigned lcore_id, n_slots;
1264 struct lcore_cache *cached_free_slots;
1266 if (h->use_local_cache) {
1267 lcore_id = rte_lcore_id();
1268 cached_free_slots = &h->local_free_slots[lcore_id];
1269 /* Cache full, need to free it. */
1270 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1271 /* Need to enqueue the free slots in global ring. */
1272 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
1273 cached_free_slots->objs,
1274 LCORE_CACHE_SIZE, NULL);
1275 cached_free_slots->len -= n_slots;
1277 /* Put index of new free slot in cache. */
1278 cached_free_slots->objs[cached_free_slots->len] =
1279 (void *)((uintptr_t)bkt->key_idx[i]);
1280 cached_free_slots->len++;
1282 rte_ring_sp_enqueue(h->free_slots,
1283 (void *)((uintptr_t)bkt->key_idx[i]));
1287 /* Compact the linked list by moving key from last entry in linked list to the
1291 __rte_hash_compact_ll(struct rte_hash_bucket *cur_bkt, int pos) {
1293 struct rte_hash_bucket *last_bkt;
1298 last_bkt = rte_hash_get_last_bkt(cur_bkt);
1300 for (i = RTE_HASH_BUCKET_ENTRIES - 1; i >= 0; i--) {
1301 if (last_bkt->key_idx[i] != EMPTY_SLOT) {
1302 cur_bkt->key_idx[pos] = last_bkt->key_idx[i];
1303 cur_bkt->sig_current[pos] = last_bkt->sig_current[i];
1304 last_bkt->sig_current[i] = NULL_SIGNATURE;
1305 last_bkt->key_idx[i] = EMPTY_SLOT;
1311 /* Search one bucket and remove the matched key.
1312 * Writer is expected to hold the lock while calling this
1315 static inline int32_t
1316 search_and_remove(const struct rte_hash *h, const void *key,
1317 struct rte_hash_bucket *bkt, uint16_t sig, int *pos)
1319 struct rte_hash_key *k, *keys = h->key_store;
1323 /* Check if key is in bucket */
1324 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1325 key_idx = __atomic_load_n(&bkt->key_idx[i],
1327 if (bkt->sig_current[i] == sig && key_idx != EMPTY_SLOT) {
1328 k = (struct rte_hash_key *) ((char *)keys +
1329 key_idx * h->key_entry_size);
1330 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
1331 bkt->sig_current[i] = NULL_SIGNATURE;
1332 /* Free the key store index if
1333 * no_free_on_del is disabled.
1335 if (!h->no_free_on_del)
1336 remove_entry(h, bkt, i);
1338 __atomic_store_n(&bkt->key_idx[i],
1344 * Return index where key is stored,
1345 * subtracting the first dummy index
1354 static inline int32_t
1355 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
1358 uint32_t prim_bucket_idx, sec_bucket_idx;
1359 struct rte_hash_bucket *prim_bkt, *sec_bkt, *prev_bkt, *last_bkt;
1360 struct rte_hash_bucket *cur_bkt;
1365 short_sig = get_short_sig(sig);
1366 prim_bucket_idx = get_prim_bucket_index(h, sig);
1367 sec_bucket_idx = get_alt_bucket_index(h, prim_bucket_idx, short_sig);
1368 prim_bkt = &h->buckets[prim_bucket_idx];
1370 __hash_rw_writer_lock(h);
1371 /* look for key in primary bucket */
1372 ret = search_and_remove(h, key, prim_bkt, short_sig, &pos);
1374 __rte_hash_compact_ll(prim_bkt, pos);
1375 last_bkt = prim_bkt->next;
1376 prev_bkt = prim_bkt;
1380 /* Calculate secondary hash */
1381 sec_bkt = &h->buckets[sec_bucket_idx];
1383 FOR_EACH_BUCKET(cur_bkt, sec_bkt) {
1384 ret = search_and_remove(h, key, cur_bkt, short_sig, &pos);
1386 __rte_hash_compact_ll(cur_bkt, pos);
1387 last_bkt = sec_bkt->next;
1393 __hash_rw_writer_unlock(h);
1396 /* Search last bucket to see if empty to be recycled */
1399 __hash_rw_writer_unlock(h);
1402 while (last_bkt->next) {
1403 prev_bkt = last_bkt;
1404 last_bkt = last_bkt->next;
1407 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1408 if (last_bkt->key_idx[i] != EMPTY_SLOT)
1411 /* found empty bucket and recycle */
1412 if (i == RTE_HASH_BUCKET_ENTRIES) {
1413 prev_bkt->next = last_bkt->next = NULL;
1414 uint32_t index = last_bkt - h->buckets_ext + 1;
1415 rte_ring_sp_enqueue(h->free_ext_bkts, (void *)(uintptr_t)index);
1418 __hash_rw_writer_unlock(h);
1423 rte_hash_del_key_with_hash(const struct rte_hash *h,
1424 const void *key, hash_sig_t sig)
1426 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1427 return __rte_hash_del_key_with_hash(h, key, sig);
1431 rte_hash_del_key(const struct rte_hash *h, const void *key)
1433 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1434 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
1438 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
1441 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
1443 struct rte_hash_key *k, *keys = h->key_store;
1444 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
1449 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
1457 int __rte_experimental
1458 rte_hash_free_key_with_position(const struct rte_hash *h,
1459 const int32_t position)
1461 RETURN_IF_TRUE(((h == NULL) || (position == EMPTY_SLOT)), -EINVAL);
1463 unsigned int lcore_id, n_slots;
1464 struct lcore_cache *cached_free_slots;
1465 const int32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1468 if (position >= total_entries)
1471 if (h->use_local_cache) {
1472 lcore_id = rte_lcore_id();
1473 cached_free_slots = &h->local_free_slots[lcore_id];
1474 /* Cache full, need to free it. */
1475 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
1476 /* Need to enqueue the free slots in global ring. */
1477 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
1478 cached_free_slots->objs,
1479 LCORE_CACHE_SIZE, NULL);
1480 cached_free_slots->len -= n_slots;
1482 /* Put index of new free slot in cache. */
1483 cached_free_slots->objs[cached_free_slots->len] =
1484 (void *)((uintptr_t)position);
1485 cached_free_slots->len++;
1487 rte_ring_sp_enqueue(h->free_slots,
1488 (void *)((uintptr_t)position));
1495 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
1496 const struct rte_hash_bucket *prim_bkt,
1497 const struct rte_hash_bucket *sec_bkt,
1499 enum rte_hash_sig_compare_function sig_cmp_fn)
1503 /* For match mask the first bit of every two bits indicates the match */
1504 switch (sig_cmp_fn) {
1505 #ifdef RTE_MACHINE_CPUFLAG_SSE2
1506 case RTE_HASH_COMPARE_SSE:
1507 /* Compare all signatures in the bucket */
1508 *prim_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1510 (__m128i const *)prim_bkt->sig_current),
1511 _mm_set1_epi16(sig)));
1512 /* Compare all signatures in the bucket */
1513 *sec_hash_matches = _mm_movemask_epi8(_mm_cmpeq_epi16(
1515 (__m128i const *)sec_bkt->sig_current),
1516 _mm_set1_epi16(sig)));
1520 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
1521 *prim_hash_matches |=
1522 ((sig == prim_bkt->sig_current[i]) << (i << 1));
1523 *sec_hash_matches |=
1524 ((sig == sec_bkt->sig_current[i]) << (i << 1));
1529 #define PREFETCH_OFFSET 4
1531 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1532 int32_t num_keys, int32_t *positions,
1533 uint64_t *hit_mask, void *data[])
1538 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
1539 uint32_t prim_index[RTE_HASH_LOOKUP_BULK_MAX];
1540 uint32_t sec_index[RTE_HASH_LOOKUP_BULK_MAX];
1541 uint16_t sig[RTE_HASH_LOOKUP_BULK_MAX];
1542 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1543 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
1544 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1545 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
1546 struct rte_hash_bucket *cur_bkt, *next_bkt;
1547 void *pdata[RTE_HASH_LOOKUP_BULK_MAX];
1548 uint32_t cnt_b, cnt_a;
1550 /* Prefetch first keys */
1551 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
1552 rte_prefetch0(keys[i]);
1555 * Prefetch rest of the keys, calculate primary and
1556 * secondary bucket and prefetch them
1558 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
1559 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
1561 prim_hash[i] = rte_hash_hash(h, keys[i]);
1563 sig[i] = get_short_sig(prim_hash[i]);
1564 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1565 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1567 primary_bkt[i] = &h->buckets[prim_index[i]];
1568 secondary_bkt[i] = &h->buckets[sec_index[i]];
1570 rte_prefetch0(primary_bkt[i]);
1571 rte_prefetch0(secondary_bkt[i]);
1574 /* Calculate and prefetch rest of the buckets */
1575 for (; i < num_keys; i++) {
1576 prim_hash[i] = rte_hash_hash(h, keys[i]);
1578 sig[i] = get_short_sig(prim_hash[i]);
1579 prim_index[i] = get_prim_bucket_index(h, prim_hash[i]);
1580 sec_index[i] = get_alt_bucket_index(h, prim_index[i], sig[i]);
1582 primary_bkt[i] = &h->buckets[prim_index[i]];
1583 secondary_bkt[i] = &h->buckets[sec_index[i]];
1585 rte_prefetch0(primary_bkt[i]);
1586 rte_prefetch0(secondary_bkt[i]);
1589 __hash_rw_reader_lock(h);
1591 /* Load the table change counter before the lookup
1592 * starts. Acquire semantics will make sure that
1593 * loads in compare_signatures are not hoisted.
1595 cnt_b = __atomic_load_n(h->tbl_chng_cnt,
1598 /* Compare signatures and prefetch key slot of first hit */
1599 for (i = 0; i < num_keys; i++) {
1600 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1601 primary_bkt[i], secondary_bkt[i],
1602 sig[i], h->sig_cmp_fn);
1604 if (prim_hitmask[i]) {
1605 uint32_t first_hit =
1606 __builtin_ctzl(prim_hitmask[i])
1609 primary_bkt[i]->key_idx[first_hit];
1610 const struct rte_hash_key *key_slot =
1611 (const struct rte_hash_key *)(
1612 (const char *)h->key_store +
1613 key_idx * h->key_entry_size);
1614 rte_prefetch0(key_slot);
1618 if (sec_hitmask[i]) {
1619 uint32_t first_hit =
1620 __builtin_ctzl(sec_hitmask[i])
1623 secondary_bkt[i]->key_idx[first_hit];
1624 const struct rte_hash_key *key_slot =
1625 (const struct rte_hash_key *)(
1626 (const char *)h->key_store +
1627 key_idx * h->key_entry_size);
1628 rte_prefetch0(key_slot);
1632 /* Compare keys, first hits in primary first */
1633 for (i = 0; i < num_keys; i++) {
1634 positions[i] = -ENOENT;
1635 while (prim_hitmask[i]) {
1636 uint32_t hit_index =
1637 __builtin_ctzl(prim_hitmask[i])
1641 &primary_bkt[i]->key_idx[hit_index],
1643 const struct rte_hash_key *key_slot =
1644 (const struct rte_hash_key *)(
1645 (const char *)h->key_store +
1646 key_idx * h->key_entry_size);
1648 if (key_idx != EMPTY_SLOT)
1649 pdata[i] = __atomic_load_n(
1653 * If key index is 0, do not compare key,
1654 * as it is checking the dummy slot
1658 key_slot->key, keys[i], h)) {
1663 positions[i] = key_idx - 1;
1666 prim_hitmask[i] &= ~(3ULL << (hit_index << 1));
1669 while (sec_hitmask[i]) {
1670 uint32_t hit_index =
1671 __builtin_ctzl(sec_hitmask[i])
1675 &secondary_bkt[i]->key_idx[hit_index],
1677 const struct rte_hash_key *key_slot =
1678 (const struct rte_hash_key *)(
1679 (const char *)h->key_store +
1680 key_idx * h->key_entry_size);
1682 if (key_idx != EMPTY_SLOT)
1683 pdata[i] = __atomic_load_n(
1687 * If key index is 0, do not compare key,
1688 * as it is checking the dummy slot
1693 key_slot->key, keys[i], h)) {
1698 positions[i] = key_idx - 1;
1701 sec_hitmask[i] &= ~(3ULL << (hit_index << 1));
1707 /* The loads of sig_current in compare_signatures
1708 * should not move below the load from tbl_chng_cnt.
1710 __atomic_thread_fence(__ATOMIC_ACQUIRE);
1711 /* Re-read the table change counter to check if the
1712 * table has changed during search. If yes, re-do
1714 * This load should not get hoisted. The load
1715 * acquires on cnt_b, primary key index and secondary
1716 * key index will make sure that it does not get
1719 cnt_a = __atomic_load_n(h->tbl_chng_cnt,
1721 } while (cnt_b != cnt_a);
1723 /* all found, do not need to go through ext bkt */
1724 if ((hits == ((1ULL << num_keys) - 1)) || !h->ext_table_support) {
1725 if (hit_mask != NULL)
1727 __hash_rw_reader_unlock(h);
1731 /* need to check ext buckets for match */
1732 for (i = 0; i < num_keys; i++) {
1733 if ((hits & (1ULL << i)) != 0)
1735 next_bkt = secondary_bkt[i]->next;
1736 FOR_EACH_BUCKET(cur_bkt, next_bkt) {
1738 ret = search_one_bucket(h, keys[i],
1739 sig[i], &data[i], cur_bkt);
1741 ret = search_one_bucket(h, keys[i],
1742 sig[i], NULL, cur_bkt);
1751 __hash_rw_reader_unlock(h);
1753 if (hit_mask != NULL)
1758 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1759 uint32_t num_keys, int32_t *positions)
1761 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1762 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1763 (positions == NULL)), -EINVAL);
1765 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1770 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1771 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1773 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1774 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1775 (hit_mask == NULL)), -EINVAL);
1777 int32_t positions[num_keys];
1779 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1781 /* Return number of hits */
1782 return __builtin_popcountl(*hit_mask);
1786 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1788 uint32_t bucket_idx, idx, position;
1789 struct rte_hash_key *next_key;
1791 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1793 const uint32_t total_entries_main = h->num_buckets *
1794 RTE_HASH_BUCKET_ENTRIES;
1795 const uint32_t total_entries = total_entries_main << 1;
1797 /* Out of bounds of all buckets (both main table and ext table) */
1798 if (*next >= total_entries_main)
1801 /* Calculate bucket and index of current iterator */
1802 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1803 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1805 /* If current position is empty, go to the next one */
1806 while ((position = __atomic_load_n(&h->buckets[bucket_idx].key_idx[idx],
1807 __ATOMIC_ACQUIRE)) == EMPTY_SLOT) {
1810 if (*next == total_entries_main)
1812 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1813 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1816 __hash_rw_reader_lock(h);
1817 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1818 position * h->key_entry_size);
1819 /* Return key and data */
1820 *key = next_key->key;
1821 *data = next_key->pdata;
1823 __hash_rw_reader_unlock(h);
1825 /* Increment iterator */
1828 return position - 1;
1830 /* Begin to iterate extendable buckets */
1832 /* Out of total bound or if ext bucket feature is not enabled */
1833 if (*next >= total_entries || !h->ext_table_support)
1836 bucket_idx = (*next - total_entries_main) / RTE_HASH_BUCKET_ENTRIES;
1837 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
1839 while ((position = h->buckets_ext[bucket_idx].key_idx[idx]) == EMPTY_SLOT) {
1841 if (*next == total_entries)
1843 bucket_idx = (*next - total_entries_main) /
1844 RTE_HASH_BUCKET_ENTRIES;
1845 idx = (*next - total_entries_main) % RTE_HASH_BUCKET_ENTRIES;
1847 __hash_rw_reader_lock(h);
1848 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1849 position * h->key_entry_size);
1850 /* Return key and data */
1851 *key = next_key->key;
1852 *data = next_key->pdata;
1854 __hash_rw_reader_unlock(h);
1856 /* Increment iterator */
1858 return position - 1;