1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
10 #include <sys/queue.h>
12 #include <rte_common.h>
13 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
15 #include <rte_memcpy.h>
16 #include <rte_prefetch.h>
17 #include <rte_branch_prediction.h>
18 #include <rte_malloc.h>
20 #include <rte_eal_memconfig.h>
21 #include <rte_per_lcore.h>
22 #include <rte_errno.h>
23 #include <rte_string_fns.h>
24 #include <rte_cpuflags.h>
25 #include <rte_rwlock.h>
26 #include <rte_spinlock.h>
28 #include <rte_compat.h>
29 #include <rte_pause.h>
32 #include "rte_cuckoo_hash.h"
34 #if defined(RTE_ARCH_X86)
35 #include "rte_cuckoo_hash_x86.h"
38 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
40 static struct rte_tailq_elem rte_hash_tailq = {
43 EAL_REGISTER_TAILQ(rte_hash_tailq)
46 rte_hash_find_existing(const char *name)
48 struct rte_hash *h = NULL;
49 struct rte_tailq_entry *te;
50 struct rte_hash_list *hash_list;
52 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
54 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
55 TAILQ_FOREACH(te, hash_list, next) {
56 h = (struct rte_hash *) te->data;
57 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
60 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
69 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
71 h->cmp_jump_table_idx = KEY_CUSTOM;
72 h->rte_hash_custom_cmp_eq = func;
76 rte_hash_cmp_eq(const void *key1, const void *key2, const struct rte_hash *h)
78 if (h->cmp_jump_table_idx == KEY_CUSTOM)
79 return h->rte_hash_custom_cmp_eq(key1, key2, h->key_len);
81 return cmp_jump_table[h->cmp_jump_table_idx](key1, key2, h->key_len);
85 rte_hash_create(const struct rte_hash_parameters *params)
87 struct rte_hash *h = NULL;
88 struct rte_tailq_entry *te = NULL;
89 struct rte_hash_list *hash_list;
90 struct rte_ring *r = NULL;
91 char hash_name[RTE_HASH_NAMESIZE];
94 char ring_name[RTE_RING_NAMESIZE];
95 unsigned num_key_slots;
96 unsigned hw_trans_mem_support = 0;
98 rte_hash_function default_hash_func = (rte_hash_function)rte_jhash;
100 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
102 if (params == NULL) {
103 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
107 /* Check for valid parameters */
108 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
109 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
110 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
111 (params->key_len == 0)) {
113 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
117 /* Check extra flags field to check extra options. */
118 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
119 hw_trans_mem_support = 1;
121 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
122 if (hw_trans_mem_support)
124 * Increase number of slots by total number of indices
125 * that can be stored in the lcore caches
126 * except for the first cache
128 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
129 LCORE_CACHE_SIZE + 1;
131 num_key_slots = params->entries + 1;
133 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
134 /* Create ring (Dummy slot index is not enqueued) */
135 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots - 1),
136 params->socket_id, 0);
138 RTE_LOG(ERR, HASH, "memory allocation failed\n");
142 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
144 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
146 /* guarantee there's no existing: this is normally already checked
147 * by ring creation above */
148 TAILQ_FOREACH(te, hash_list, next) {
149 h = (struct rte_hash *) te->data;
150 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
160 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
162 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
166 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
167 RTE_CACHE_LINE_SIZE, params->socket_id);
170 RTE_LOG(ERR, HASH, "memory allocation failed\n");
174 const uint32_t num_buckets = rte_align32pow2(params->entries)
175 / RTE_HASH_BUCKET_ENTRIES;
177 buckets = rte_zmalloc_socket(NULL,
178 num_buckets * sizeof(struct rte_hash_bucket),
179 RTE_CACHE_LINE_SIZE, params->socket_id);
181 if (buckets == NULL) {
182 RTE_LOG(ERR, HASH, "memory allocation failed\n");
186 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
187 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
189 k = rte_zmalloc_socket(NULL, key_tbl_size,
190 RTE_CACHE_LINE_SIZE, params->socket_id);
193 RTE_LOG(ERR, HASH, "memory allocation failed\n");
198 * If x86 architecture is used, select appropriate compare function,
199 * which may use x86 intrinsics, otherwise use memcmp
201 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
202 /* Select function to compare keys */
203 switch (params->key_len) {
205 h->cmp_jump_table_idx = KEY_16_BYTES;
208 h->cmp_jump_table_idx = KEY_32_BYTES;
211 h->cmp_jump_table_idx = KEY_48_BYTES;
214 h->cmp_jump_table_idx = KEY_64_BYTES;
217 h->cmp_jump_table_idx = KEY_80_BYTES;
220 h->cmp_jump_table_idx = KEY_96_BYTES;
223 h->cmp_jump_table_idx = KEY_112_BYTES;
226 h->cmp_jump_table_idx = KEY_128_BYTES;
229 /* If key is not multiple of 16, use generic memcmp */
230 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
233 h->cmp_jump_table_idx = KEY_OTHER_BYTES;
236 if (hw_trans_mem_support) {
237 h->local_free_slots = rte_zmalloc_socket(NULL,
238 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
239 RTE_CACHE_LINE_SIZE, params->socket_id);
242 /* Default hash function */
243 #if defined(RTE_ARCH_X86)
244 default_hash_func = (rte_hash_function)rte_hash_crc;
245 #elif defined(RTE_ARCH_ARM64)
246 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
247 default_hash_func = (rte_hash_function)rte_hash_crc;
249 /* Setup hash context */
250 snprintf(h->name, sizeof(h->name), "%s", params->name);
251 h->entries = params->entries;
252 h->key_len = params->key_len;
253 h->key_entry_size = key_entry_size;
254 h->hash_func_init_val = params->hash_func_init_val;
256 h->num_buckets = num_buckets;
257 h->bucket_bitmask = h->num_buckets - 1;
258 h->buckets = buckets;
259 h->hash_func = (params->hash_func == NULL) ?
260 default_hash_func : params->hash_func;
263 h->hw_trans_mem_support = hw_trans_mem_support;
265 #if defined(RTE_ARCH_X86)
266 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_AVX2))
267 h->sig_cmp_fn = RTE_HASH_COMPARE_AVX2;
268 else if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE2))
269 h->sig_cmp_fn = RTE_HASH_COMPARE_SSE;
272 h->sig_cmp_fn = RTE_HASH_COMPARE_SCALAR;
274 /* Turn on multi-writer only with explicit flat from user and TM
277 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD) {
278 if (h->hw_trans_mem_support) {
279 h->add_key = ADD_KEY_MULTIWRITER_TM;
281 h->add_key = ADD_KEY_MULTIWRITER;
282 h->multiwriter_lock = rte_malloc(NULL,
283 sizeof(rte_spinlock_t),
285 rte_spinlock_init(h->multiwriter_lock);
288 h->add_key = ADD_KEY_SINGLEWRITER;
290 /* Populate free slots ring. Entry zero is reserved for key misses. */
291 for (i = 1; i < params->entries + 1; i++)
292 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
294 te->data = (void *) h;
295 TAILQ_INSERT_TAIL(hash_list, te, next);
296 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
300 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
311 rte_hash_free(struct rte_hash *h)
313 struct rte_tailq_entry *te;
314 struct rte_hash_list *hash_list;
319 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
321 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
323 /* find out tailq entry */
324 TAILQ_FOREACH(te, hash_list, next) {
325 if (te->data == (void *) h)
330 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
334 TAILQ_REMOVE(hash_list, te, next);
336 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
338 if (h->hw_trans_mem_support)
339 rte_free(h->local_free_slots);
341 if (h->add_key == ADD_KEY_MULTIWRITER)
342 rte_free(h->multiwriter_lock);
343 rte_ring_free(h->free_slots);
344 rte_free(h->key_store);
345 rte_free(h->buckets);
351 rte_hash_hash(const struct rte_hash *h, const void *key)
353 /* calc hash result by key */
354 return h->hash_func(key, h->key_len, h->hash_func_init_val);
357 /* Calc the secondary hash value from the primary hash value of a given key */
358 static inline hash_sig_t
359 rte_hash_secondary_hash(const hash_sig_t primary_hash)
361 static const unsigned all_bits_shift = 12;
362 static const unsigned alt_bits_xor = 0x5bd1e995;
364 uint32_t tag = primary_hash >> all_bits_shift;
366 return primary_hash ^ ((tag + 1) * alt_bits_xor);
370 rte_hash_reset(struct rte_hash *h)
378 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
379 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
381 /* clear the free ring */
382 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
385 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
386 for (i = 1; i < h->entries + 1; i++)
387 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
389 if (h->hw_trans_mem_support) {
390 /* Reset local caches per lcore */
391 for (i = 0; i < RTE_MAX_LCORE; i++)
392 h->local_free_slots[i].len = 0;
396 /* Search for an entry that can be pushed to its alternative location */
398 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt,
399 unsigned int *nr_pushes)
403 uint32_t next_bucket_idx;
404 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
407 * Push existing item (search for bucket with space in
408 * alternative locations) to its alternative location
410 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
411 /* Search for space in alternative locations */
412 next_bucket_idx = bkt->sig_alt[i] & h->bucket_bitmask;
413 next_bkt[i] = &h->buckets[next_bucket_idx];
414 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
415 if (next_bkt[i]->key_idx[j] == EMPTY_SLOT)
419 if (j != RTE_HASH_BUCKET_ENTRIES)
423 /* Alternative location has spare room (end of recursive function) */
424 if (i != RTE_HASH_BUCKET_ENTRIES) {
425 next_bkt[i]->sig_alt[j] = bkt->sig_current[i];
426 next_bkt[i]->sig_current[j] = bkt->sig_alt[i];
427 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
431 /* Pick entry that has not been pushed yet */
432 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
433 if (bkt->flag[i] == 0)
436 /* All entries have been pushed, so entry cannot be added */
437 if (i == RTE_HASH_BUCKET_ENTRIES || ++(*nr_pushes) > RTE_HASH_MAX_PUSHES)
440 /* Set flag to indicate that this entry is going to be pushed */
443 /* Need room in alternative bucket to insert the pushed entry */
444 ret = make_space_bucket(h, next_bkt[i], nr_pushes);
446 * After recursive function.
447 * Clear flags and insert the pushed entry
448 * in its alternative location if successful,
453 next_bkt[i]->sig_alt[ret] = bkt->sig_current[i];
454 next_bkt[i]->sig_current[ret] = bkt->sig_alt[i];
455 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
463 * Function called to enqueue back an index in the cache/ring,
464 * as slot has not being used and it can be used in the
465 * next addition attempt.
468 enqueue_slot_back(const struct rte_hash *h,
469 struct lcore_cache *cached_free_slots,
472 if (h->hw_trans_mem_support) {
473 cached_free_slots->objs[cached_free_slots->len] = slot_id;
474 cached_free_slots->len++;
476 rte_ring_sp_enqueue(h->free_slots, slot_id);
479 static inline int32_t
480 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
481 hash_sig_t sig, void *data)
484 uint32_t prim_bucket_idx, sec_bucket_idx;
486 struct rte_hash_bucket *prim_bkt, *sec_bkt;
487 struct rte_hash_key *new_k, *k, *keys = h->key_store;
488 void *slot_id = NULL;
493 struct lcore_cache *cached_free_slots = NULL;
494 unsigned int nr_pushes = 0;
496 if (h->add_key == ADD_KEY_MULTIWRITER)
497 rte_spinlock_lock(h->multiwriter_lock);
499 prim_bucket_idx = sig & h->bucket_bitmask;
500 prim_bkt = &h->buckets[prim_bucket_idx];
501 rte_prefetch0(prim_bkt);
503 alt_hash = rte_hash_secondary_hash(sig);
504 sec_bucket_idx = alt_hash & h->bucket_bitmask;
505 sec_bkt = &h->buckets[sec_bucket_idx];
506 rte_prefetch0(sec_bkt);
508 /* Get a new slot for storing the new key */
509 if (h->hw_trans_mem_support) {
510 lcore_id = rte_lcore_id();
511 cached_free_slots = &h->local_free_slots[lcore_id];
512 /* Try to get a free slot from the local cache */
513 if (cached_free_slots->len == 0) {
514 /* Need to get another burst of free slots from global ring */
515 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
516 cached_free_slots->objs,
517 LCORE_CACHE_SIZE, NULL);
523 cached_free_slots->len += n_slots;
526 /* Get a free slot from the local cache */
527 cached_free_slots->len--;
528 slot_id = cached_free_slots->objs[cached_free_slots->len];
530 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0) {
536 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
537 rte_prefetch0(new_k);
538 new_idx = (uint32_t)((uintptr_t) slot_id);
540 /* Check if key is already inserted in primary location */
541 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
542 if (prim_bkt->sig_current[i] == sig &&
543 prim_bkt->sig_alt[i] == alt_hash) {
544 k = (struct rte_hash_key *) ((char *)keys +
545 prim_bkt->key_idx[i] * h->key_entry_size);
546 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
547 /* Enqueue index of free slot back in the ring. */
548 enqueue_slot_back(h, cached_free_slots, slot_id);
552 * Return index where key is stored,
553 * subtracting the first dummy index
555 ret = prim_bkt->key_idx[i] - 1;
561 /* Check if key is already inserted in secondary location */
562 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
563 if (sec_bkt->sig_alt[i] == sig &&
564 sec_bkt->sig_current[i] == alt_hash) {
565 k = (struct rte_hash_key *) ((char *)keys +
566 sec_bkt->key_idx[i] * h->key_entry_size);
567 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
568 /* Enqueue index of free slot back in the ring. */
569 enqueue_slot_back(h, cached_free_slots, slot_id);
573 * Return index where key is stored,
574 * subtracting the first dummy index
576 ret = sec_bkt->key_idx[i] - 1;
583 rte_memcpy(new_k->key, key, h->key_len);
586 #if defined(RTE_ARCH_X86) /* currently only x86 support HTM */
587 if (h->add_key == ADD_KEY_MULTIWRITER_TM) {
588 ret = rte_hash_cuckoo_insert_mw_tm(prim_bkt,
589 sig, alt_hash, new_idx);
593 /* Primary bucket full, need to make space for new entry */
594 ret = rte_hash_cuckoo_make_space_mw_tm(h, prim_bkt, sig,
600 /* Also search secondary bucket to get better occupancy */
601 ret = rte_hash_cuckoo_make_space_mw_tm(h, sec_bkt, sig,
608 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
609 /* Check if slot is available */
610 if (likely(prim_bkt->key_idx[i] == EMPTY_SLOT)) {
611 prim_bkt->sig_current[i] = sig;
612 prim_bkt->sig_alt[i] = alt_hash;
613 prim_bkt->key_idx[i] = new_idx;
618 if (i != RTE_HASH_BUCKET_ENTRIES) {
619 if (h->add_key == ADD_KEY_MULTIWRITER)
620 rte_spinlock_unlock(h->multiwriter_lock);
624 /* Primary bucket full, need to make space for new entry
625 * After recursive function.
626 * Insert the new entry in the position of the pushed entry
627 * if successful or return error and
628 * store the new slot back in the ring
630 ret = make_space_bucket(h, prim_bkt, &nr_pushes);
632 prim_bkt->sig_current[ret] = sig;
633 prim_bkt->sig_alt[ret] = alt_hash;
634 prim_bkt->key_idx[ret] = new_idx;
635 if (h->add_key == ADD_KEY_MULTIWRITER)
636 rte_spinlock_unlock(h->multiwriter_lock);
639 #if defined(RTE_ARCH_X86)
642 /* Error in addition, store new slot back in the ring and return error */
643 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
646 if (h->add_key == ADD_KEY_MULTIWRITER)
647 rte_spinlock_unlock(h->multiwriter_lock);
652 rte_hash_add_key_with_hash(const struct rte_hash *h,
653 const void *key, hash_sig_t sig)
655 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
656 return __rte_hash_add_key_with_hash(h, key, sig, 0);
660 rte_hash_add_key(const struct rte_hash *h, const void *key)
662 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
663 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
667 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
668 const void *key, hash_sig_t sig, void *data)
672 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
673 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
681 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
685 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
687 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
693 static inline int32_t
694 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
695 hash_sig_t sig, void **data)
700 struct rte_hash_bucket *bkt;
701 struct rte_hash_key *k, *keys = h->key_store;
703 bucket_idx = sig & h->bucket_bitmask;
704 bkt = &h->buckets[bucket_idx];
706 /* Check if key is in primary location */
707 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
708 if (bkt->sig_current[i] == sig &&
709 bkt->key_idx[i] != EMPTY_SLOT) {
710 k = (struct rte_hash_key *) ((char *)keys +
711 bkt->key_idx[i] * h->key_entry_size);
712 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
716 * Return index where key is stored,
717 * subtracting the first dummy index
719 return bkt->key_idx[i] - 1;
724 /* Calculate secondary hash */
725 alt_hash = rte_hash_secondary_hash(sig);
726 bucket_idx = alt_hash & h->bucket_bitmask;
727 bkt = &h->buckets[bucket_idx];
729 /* Check if key is in secondary location */
730 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
731 if (bkt->sig_current[i] == alt_hash &&
732 bkt->sig_alt[i] == sig) {
733 k = (struct rte_hash_key *) ((char *)keys +
734 bkt->key_idx[i] * h->key_entry_size);
735 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
739 * Return index where key is stored,
740 * subtracting the first dummy index
742 return bkt->key_idx[i] - 1;
751 rte_hash_lookup_with_hash(const struct rte_hash *h,
752 const void *key, hash_sig_t sig)
754 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
755 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
759 rte_hash_lookup(const struct rte_hash *h, const void *key)
761 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
762 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
766 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
767 const void *key, hash_sig_t sig, void **data)
769 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
770 return __rte_hash_lookup_with_hash(h, key, sig, data);
774 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
776 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
777 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
781 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
783 unsigned lcore_id, n_slots;
784 struct lcore_cache *cached_free_slots;
786 bkt->sig_current[i] = NULL_SIGNATURE;
787 bkt->sig_alt[i] = NULL_SIGNATURE;
788 if (h->hw_trans_mem_support) {
789 lcore_id = rte_lcore_id();
790 cached_free_slots = &h->local_free_slots[lcore_id];
791 /* Cache full, need to free it. */
792 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
793 /* Need to enqueue the free slots in global ring. */
794 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
795 cached_free_slots->objs,
796 LCORE_CACHE_SIZE, NULL);
797 cached_free_slots->len -= n_slots;
799 /* Put index of new free slot in cache. */
800 cached_free_slots->objs[cached_free_slots->len] =
801 (void *)((uintptr_t)bkt->key_idx[i]);
802 cached_free_slots->len++;
804 rte_ring_sp_enqueue(h->free_slots,
805 (void *)((uintptr_t)bkt->key_idx[i]));
809 static inline int32_t
810 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
816 struct rte_hash_bucket *bkt;
817 struct rte_hash_key *k, *keys = h->key_store;
820 bucket_idx = sig & h->bucket_bitmask;
821 bkt = &h->buckets[bucket_idx];
823 /* Check if key is in primary location */
824 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
825 if (bkt->sig_current[i] == sig &&
826 bkt->key_idx[i] != EMPTY_SLOT) {
827 k = (struct rte_hash_key *) ((char *)keys +
828 bkt->key_idx[i] * h->key_entry_size);
829 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
830 remove_entry(h, bkt, i);
833 * Return index where key is stored,
834 * subtracting the first dummy index
836 ret = bkt->key_idx[i] - 1;
837 bkt->key_idx[i] = EMPTY_SLOT;
843 /* Calculate secondary hash */
844 alt_hash = rte_hash_secondary_hash(sig);
845 bucket_idx = alt_hash & h->bucket_bitmask;
846 bkt = &h->buckets[bucket_idx];
848 /* Check if key is in secondary location */
849 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
850 if (bkt->sig_current[i] == alt_hash &&
851 bkt->key_idx[i] != EMPTY_SLOT) {
852 k = (struct rte_hash_key *) ((char *)keys +
853 bkt->key_idx[i] * h->key_entry_size);
854 if (rte_hash_cmp_eq(key, k->key, h) == 0) {
855 remove_entry(h, bkt, i);
858 * Return index where key is stored,
859 * subtracting the first dummy index
861 ret = bkt->key_idx[i] - 1;
862 bkt->key_idx[i] = EMPTY_SLOT;
872 rte_hash_del_key_with_hash(const struct rte_hash *h,
873 const void *key, hash_sig_t sig)
875 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
876 return __rte_hash_del_key_with_hash(h, key, sig);
880 rte_hash_del_key(const struct rte_hash *h, const void *key)
882 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
883 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
887 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
890 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
892 struct rte_hash_key *k, *keys = h->key_store;
893 k = (struct rte_hash_key *) ((char *) keys + (position + 1) *
898 __rte_hash_lookup_with_hash(h, *key, rte_hash_hash(h, *key),
907 compare_signatures(uint32_t *prim_hash_matches, uint32_t *sec_hash_matches,
908 const struct rte_hash_bucket *prim_bkt,
909 const struct rte_hash_bucket *sec_bkt,
910 hash_sig_t prim_hash, hash_sig_t sec_hash,
911 enum rte_hash_sig_compare_function sig_cmp_fn)
915 switch (sig_cmp_fn) {
916 #ifdef RTE_MACHINE_CPUFLAG_AVX2
917 case RTE_HASH_COMPARE_AVX2:
918 *prim_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
920 (__m256i const *)prim_bkt->sig_current),
921 _mm256_set1_epi32(prim_hash)));
922 *sec_hash_matches = _mm256_movemask_ps((__m256)_mm256_cmpeq_epi32(
924 (__m256i const *)sec_bkt->sig_current),
925 _mm256_set1_epi32(sec_hash)));
928 #ifdef RTE_MACHINE_CPUFLAG_SSE2
929 case RTE_HASH_COMPARE_SSE:
930 /* Compare the first 4 signatures in the bucket */
931 *prim_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
933 (__m128i const *)prim_bkt->sig_current),
934 _mm_set1_epi32(prim_hash)));
935 *prim_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
937 (__m128i const *)&prim_bkt->sig_current[4]),
938 _mm_set1_epi32(prim_hash)))) << 4;
939 /* Compare the first 4 signatures in the bucket */
940 *sec_hash_matches = _mm_movemask_ps((__m128)_mm_cmpeq_epi16(
942 (__m128i const *)sec_bkt->sig_current),
943 _mm_set1_epi32(sec_hash)));
944 *sec_hash_matches |= (_mm_movemask_ps((__m128)_mm_cmpeq_epi16(
946 (__m128i const *)&sec_bkt->sig_current[4]),
947 _mm_set1_epi32(sec_hash)))) << 4;
951 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
952 *prim_hash_matches |=
953 ((prim_hash == prim_bkt->sig_current[i]) << i);
955 ((sec_hash == sec_bkt->sig_current[i]) << i);
961 #define PREFETCH_OFFSET 4
963 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
964 int32_t num_keys, int32_t *positions,
965 uint64_t *hit_mask, void *data[])
969 uint32_t prim_hash[RTE_HASH_LOOKUP_BULK_MAX];
970 uint32_t sec_hash[RTE_HASH_LOOKUP_BULK_MAX];
971 const struct rte_hash_bucket *primary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
972 const struct rte_hash_bucket *secondary_bkt[RTE_HASH_LOOKUP_BULK_MAX];
973 uint32_t prim_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
974 uint32_t sec_hitmask[RTE_HASH_LOOKUP_BULK_MAX] = {0};
976 /* Prefetch first keys */
977 for (i = 0; i < PREFETCH_OFFSET && i < num_keys; i++)
978 rte_prefetch0(keys[i]);
981 * Prefetch rest of the keys, calculate primary and
982 * secondary bucket and prefetch them
984 for (i = 0; i < (num_keys - PREFETCH_OFFSET); i++) {
985 rte_prefetch0(keys[i + PREFETCH_OFFSET]);
987 prim_hash[i] = rte_hash_hash(h, keys[i]);
988 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
990 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
991 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
993 rte_prefetch0(primary_bkt[i]);
994 rte_prefetch0(secondary_bkt[i]);
997 /* Calculate and prefetch rest of the buckets */
998 for (; i < num_keys; i++) {
999 prim_hash[i] = rte_hash_hash(h, keys[i]);
1000 sec_hash[i] = rte_hash_secondary_hash(prim_hash[i]);
1002 primary_bkt[i] = &h->buckets[prim_hash[i] & h->bucket_bitmask];
1003 secondary_bkt[i] = &h->buckets[sec_hash[i] & h->bucket_bitmask];
1005 rte_prefetch0(primary_bkt[i]);
1006 rte_prefetch0(secondary_bkt[i]);
1009 /* Compare signatures and prefetch key slot of first hit */
1010 for (i = 0; i < num_keys; i++) {
1011 compare_signatures(&prim_hitmask[i], &sec_hitmask[i],
1012 primary_bkt[i], secondary_bkt[i],
1013 prim_hash[i], sec_hash[i], h->sig_cmp_fn);
1015 if (prim_hitmask[i]) {
1016 uint32_t first_hit = __builtin_ctzl(prim_hitmask[i]);
1017 uint32_t key_idx = primary_bkt[i]->key_idx[first_hit];
1018 const struct rte_hash_key *key_slot =
1019 (const struct rte_hash_key *)(
1020 (const char *)h->key_store +
1021 key_idx * h->key_entry_size);
1022 rte_prefetch0(key_slot);
1026 if (sec_hitmask[i]) {
1027 uint32_t first_hit = __builtin_ctzl(sec_hitmask[i]);
1028 uint32_t key_idx = secondary_bkt[i]->key_idx[first_hit];
1029 const struct rte_hash_key *key_slot =
1030 (const struct rte_hash_key *)(
1031 (const char *)h->key_store +
1032 key_idx * h->key_entry_size);
1033 rte_prefetch0(key_slot);
1037 /* Compare keys, first hits in primary first */
1038 for (i = 0; i < num_keys; i++) {
1039 positions[i] = -ENOENT;
1040 while (prim_hitmask[i]) {
1041 uint32_t hit_index = __builtin_ctzl(prim_hitmask[i]);
1043 uint32_t key_idx = primary_bkt[i]->key_idx[hit_index];
1044 const struct rte_hash_key *key_slot =
1045 (const struct rte_hash_key *)(
1046 (const char *)h->key_store +
1047 key_idx * h->key_entry_size);
1049 * If key index is 0, do not compare key,
1050 * as it is checking the dummy slot
1052 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1054 data[i] = key_slot->pdata;
1057 positions[i] = key_idx - 1;
1060 prim_hitmask[i] &= ~(1 << (hit_index));
1063 while (sec_hitmask[i]) {
1064 uint32_t hit_index = __builtin_ctzl(sec_hitmask[i]);
1066 uint32_t key_idx = secondary_bkt[i]->key_idx[hit_index];
1067 const struct rte_hash_key *key_slot =
1068 (const struct rte_hash_key *)(
1069 (const char *)h->key_store +
1070 key_idx * h->key_entry_size);
1072 * If key index is 0, do not compare key,
1073 * as it is checking the dummy slot
1076 if (!!key_idx & !rte_hash_cmp_eq(key_slot->key, keys[i], h)) {
1078 data[i] = key_slot->pdata;
1081 positions[i] = key_idx - 1;
1084 sec_hitmask[i] &= ~(1 << (hit_index));
1091 if (hit_mask != NULL)
1096 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1097 uint32_t num_keys, int32_t *positions)
1099 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1100 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1101 (positions == NULL)), -EINVAL);
1103 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1108 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1109 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1111 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1112 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1113 (hit_mask == NULL)), -EINVAL);
1115 int32_t positions[num_keys];
1117 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1119 /* Return number of hits */
1120 return __builtin_popcountl(*hit_mask);
1124 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1126 uint32_t bucket_idx, idx, position;
1127 struct rte_hash_key *next_key;
1129 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1131 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1133 if (*next >= total_entries)
1136 /* Calculate bucket and index of current iterator */
1137 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1138 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1140 /* If current position is empty, go to the next one */
1141 while (h->buckets[bucket_idx].key_idx[idx] == EMPTY_SLOT) {
1144 if (*next == total_entries)
1146 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1147 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1150 /* Get position of entry in key table */
1151 position = h->buckets[bucket_idx].key_idx[idx];
1152 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1153 position * h->key_entry_size);
1154 /* Return key and data */
1155 *key = next_key->key;
1156 *data = next_key->pdata;
1158 /* Increment iterator */
1161 return position - 1;