4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <sys/queue.h>
41 #include <rte_common.h>
42 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
44 #include <rte_memcpy.h>
45 #include <rte_prefetch.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
50 #include <rte_eal_memconfig.h>
51 #include <rte_per_lcore.h>
52 #include <rte_errno.h>
53 #include <rte_string_fns.h>
54 #include <rte_cpuflags.h>
56 #include <rte_rwlock.h>
57 #include <rte_spinlock.h>
59 #include <rte_compat.h>
62 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686) || defined(RTE_ARCH_X86_X32)
63 #include "rte_cmp_x86.h"
66 #if defined(RTE_ARCH_ARM64)
67 #include "rte_cmp_arm64.h"
70 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
72 static struct rte_tailq_elem rte_hash_tailq = {
75 EAL_REGISTER_TAILQ(rte_hash_tailq)
77 /* Macro to enable/disable run-time checking of function parameters */
78 #if defined(RTE_LIBRTE_HASH_DEBUG)
79 #define RETURN_IF_TRUE(cond, retval) do { \
84 #define RETURN_IF_TRUE(cond, retval)
87 /* Hash function used if none is specified */
88 #if defined(RTE_MACHINE_CPUFLAG_SSE4_2) || defined(RTE_MACHINE_CPUFLAG_CRC32)
89 #include <rte_hash_crc.h>
90 #define DEFAULT_HASH_FUNC rte_hash_crc
92 #include <rte_jhash.h>
93 #define DEFAULT_HASH_FUNC rte_jhash
96 /** Number of items per bucket. */
97 #define RTE_HASH_BUCKET_ENTRIES 4
99 #define NULL_SIGNATURE 0
101 #define KEY_ALIGNMENT 16
103 #define LCORE_CACHE_SIZE 8
105 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
108 unsigned len; /**< Cache len */
109 void *objs[LCORE_CACHE_SIZE]; /**< Cache objects */
110 } __rte_cache_aligned;
112 /** A hash table structure. */
114 char name[RTE_HASH_NAMESIZE]; /**< Name of the hash. */
115 uint32_t entries; /**< Total table entries. */
116 uint32_t num_buckets; /**< Number of buckets in table. */
117 uint32_t key_len; /**< Length of hash key. */
118 rte_hash_function hash_func; /**< Function used to calculate hash. */
119 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
120 rte_hash_cmp_eq_t rte_hash_cmp_eq; /**< Function used to compare keys. */
121 uint32_t bucket_bitmask; /**< Bitmask for getting bucket index
122 from hash signature. */
123 uint32_t key_entry_size; /**< Size of each key entry. */
125 struct rte_ring *free_slots; /**< Ring that stores all indexes
126 of the free slots in the key table */
127 void *key_store; /**< Table storing all keys and data */
128 struct rte_hash_bucket *buckets; /**< Table with buckets storing all the
129 hash values and key indexes
131 uint8_t hw_trans_mem_support; /**< Hardware transactional
133 struct lcore_cache *local_free_slots;
134 /**< Local cache per lcore, storing some indexes of the free slots */
135 } __rte_cache_aligned;
137 /* Structure storing both primary and secondary hashes */
138 struct rte_hash_signatures {
148 /* Structure that stores key-value pair */
149 struct rte_hash_key {
154 /* Variable key size */
156 } __attribute__((aligned(KEY_ALIGNMENT)));
158 /** Bucket structure */
159 struct rte_hash_bucket {
160 struct rte_hash_signatures signatures[RTE_HASH_BUCKET_ENTRIES];
161 /* Includes dummy key index that always contains index 0 */
162 uint32_t key_idx[RTE_HASH_BUCKET_ENTRIES + 1];
163 uint8_t flag[RTE_HASH_BUCKET_ENTRIES];
164 } __rte_cache_aligned;
167 rte_hash_find_existing(const char *name)
169 struct rte_hash *h = NULL;
170 struct rte_tailq_entry *te;
171 struct rte_hash_list *hash_list;
173 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
175 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
176 TAILQ_FOREACH(te, hash_list, next) {
177 h = (struct rte_hash *) te->data;
178 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
181 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
191 rte_hash_create(const struct rte_hash_parameters *params)
193 struct rte_hash *h = NULL;
194 struct rte_tailq_entry *te = NULL;
195 struct rte_hash_list *hash_list;
196 struct rte_ring *r = NULL;
197 char hash_name[RTE_HASH_NAMESIZE];
199 void *buckets = NULL;
200 char ring_name[RTE_RING_NAMESIZE];
201 unsigned num_key_slots;
202 unsigned hw_trans_mem_support = 0;
205 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
207 if (params == NULL) {
208 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
212 /* Check for valid parameters */
213 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
214 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
215 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
216 (params->key_len == 0)) {
218 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
222 /* Check extra flags field to check extra options. */
223 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
224 hw_trans_mem_support = 1;
226 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
228 /* Guarantee there's no existing */
229 h = rte_hash_find_existing(params->name);
233 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
235 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
239 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
240 RTE_CACHE_LINE_SIZE, params->socket_id);
243 RTE_LOG(ERR, HASH, "memory allocation failed\n");
247 const uint32_t num_buckets = rte_align32pow2(params->entries)
248 / RTE_HASH_BUCKET_ENTRIES;
250 buckets = rte_zmalloc_socket(NULL,
251 num_buckets * sizeof(struct rte_hash_bucket),
252 RTE_CACHE_LINE_SIZE, params->socket_id);
254 if (buckets == NULL) {
255 RTE_LOG(ERR, HASH, "memory allocation failed\n");
259 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
261 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
262 if (hw_trans_mem_support)
264 * Increase number of slots by total number of indices
265 * that can be stored in the lcore caches
266 * except for the first cache
268 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
269 LCORE_CACHE_SIZE + 1;
271 num_key_slots = params->entries + 1;
273 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
275 k = rte_zmalloc_socket(NULL, key_tbl_size,
276 RTE_CACHE_LINE_SIZE, params->socket_id);
279 RTE_LOG(ERR, HASH, "memory allocation failed\n");
284 * If x86 architecture is used, select appropriate compare function,
285 * which may use x86 instrinsics, otherwise use memcmp
287 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686) ||\
288 defined(RTE_ARCH_X86_X32) || defined(RTE_ARCH_ARM64)
289 /* Select function to compare keys */
290 switch (params->key_len) {
292 h->rte_hash_cmp_eq = rte_hash_k16_cmp_eq;
295 h->rte_hash_cmp_eq = rte_hash_k32_cmp_eq;
298 h->rte_hash_cmp_eq = rte_hash_k48_cmp_eq;
301 h->rte_hash_cmp_eq = rte_hash_k64_cmp_eq;
304 h->rte_hash_cmp_eq = rte_hash_k80_cmp_eq;
307 h->rte_hash_cmp_eq = rte_hash_k96_cmp_eq;
310 h->rte_hash_cmp_eq = rte_hash_k112_cmp_eq;
313 h->rte_hash_cmp_eq = rte_hash_k128_cmp_eq;
316 /* If key is not multiple of 16, use generic memcmp */
317 h->rte_hash_cmp_eq = memcmp;
320 h->rte_hash_cmp_eq = memcmp;
323 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
324 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
325 params->socket_id, 0);
327 RTE_LOG(ERR, HASH, "memory allocation failed\n");
331 if (hw_trans_mem_support) {
332 h->local_free_slots = rte_zmalloc_socket(NULL,
333 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
334 RTE_CACHE_LINE_SIZE, params->socket_id);
337 /* Setup hash context */
338 snprintf(h->name, sizeof(h->name), "%s", params->name);
339 h->entries = params->entries;
340 h->key_len = params->key_len;
341 h->key_entry_size = key_entry_size;
342 h->hash_func_init_val = params->hash_func_init_val;
344 h->num_buckets = num_buckets;
345 h->bucket_bitmask = h->num_buckets - 1;
346 h->buckets = buckets;
347 h->hash_func = (params->hash_func == NULL) ?
348 DEFAULT_HASH_FUNC : params->hash_func;
351 h->hw_trans_mem_support = hw_trans_mem_support;
353 /* populate the free slots ring. Entry zero is reserved for key misses */
354 for (i = 1; i < params->entries + 1; i++)
355 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
357 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
358 te->data = (void *) h;
359 TAILQ_INSERT_TAIL(hash_list, te, next);
360 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
372 rte_hash_free(struct rte_hash *h)
374 struct rte_tailq_entry *te;
375 struct rte_hash_list *hash_list;
380 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
382 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
384 /* find out tailq entry */
385 TAILQ_FOREACH(te, hash_list, next) {
386 if (te->data == (void *) h)
391 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
395 TAILQ_REMOVE(hash_list, te, next);
397 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
399 if (h->hw_trans_mem_support)
400 rte_free(h->local_free_slots);
402 rte_ring_free(h->free_slots);
403 rte_free(h->key_store);
404 rte_free(h->buckets);
410 rte_hash_hash(const struct rte_hash *h, const void *key)
412 /* calc hash result by key */
413 return h->hash_func(key, h->key_len, h->hash_func_init_val);
416 /* Calc the secondary hash value from the primary hash value of a given key */
417 static inline hash_sig_t
418 rte_hash_secondary_hash(const hash_sig_t primary_hash)
420 static const unsigned all_bits_shift = 12;
421 static const unsigned alt_bits_xor = 0x5bd1e995;
423 uint32_t tag = primary_hash >> all_bits_shift;
425 return (primary_hash ^ ((tag + 1) * alt_bits_xor));
429 rte_hash_reset(struct rte_hash *h)
437 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
438 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
440 /* clear the free ring */
441 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
444 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
445 for (i = 1; i < h->entries + 1; i++)
446 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
448 if (h->hw_trans_mem_support) {
449 /* Reset local caches per lcore */
450 for (i = 0; i < RTE_MAX_LCORE; i++)
451 h->local_free_slots[i].len = 0;
455 /* Search for an entry that can be pushed to its alternative location */
457 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
461 uint32_t next_bucket_idx;
462 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
465 * Push existing item (search for bucket with space in
466 * alternative locations) to its alternative location
468 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
469 /* Search for space in alternative locations */
470 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
471 next_bkt[i] = &h->buckets[next_bucket_idx];
472 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
473 if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
477 if (j != RTE_HASH_BUCKET_ENTRIES)
481 /* Alternative location has spare room (end of recursive function) */
482 if (i != RTE_HASH_BUCKET_ENTRIES) {
483 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
484 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
485 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
489 /* Pick entry that has not been pushed yet */
490 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
491 if (bkt->flag[i] == 0)
494 /* All entries have been pushed, so entry cannot be added */
495 if (i == RTE_HASH_BUCKET_ENTRIES)
498 /* Set flag to indicate that this entry is going to be pushed */
500 /* Need room in alternative bucket to insert the pushed entry */
501 ret = make_space_bucket(h, next_bkt[i]);
503 * After recursive function.
504 * Clear flags and insert the pushed entry
505 * in its alternative location if successful,
510 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
511 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
512 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
520 * Function called to enqueue back an index in the cache/ring,
521 * as slot has not being used and it can be used in the
522 * next addition attempt.
525 enqueue_slot_back(const struct rte_hash *h,
526 struct lcore_cache *cached_free_slots,
529 if (h->hw_trans_mem_support) {
530 cached_free_slots->objs[cached_free_slots->len] = slot_id;
531 cached_free_slots->len++;
533 rte_ring_sp_enqueue(h->free_slots, slot_id);
536 static inline int32_t
537 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
538 hash_sig_t sig, void *data)
541 uint32_t prim_bucket_idx, sec_bucket_idx;
543 struct rte_hash_bucket *prim_bkt, *sec_bkt;
544 struct rte_hash_key *new_k, *k, *keys = h->key_store;
545 void *slot_id = NULL;
550 struct lcore_cache *cached_free_slots = NULL;
552 prim_bucket_idx = sig & h->bucket_bitmask;
553 prim_bkt = &h->buckets[prim_bucket_idx];
554 rte_prefetch0(prim_bkt);
556 alt_hash = rte_hash_secondary_hash(sig);
557 sec_bucket_idx = alt_hash & h->bucket_bitmask;
558 sec_bkt = &h->buckets[sec_bucket_idx];
559 rte_prefetch0(sec_bkt);
561 /* Get a new slot for storing the new key */
562 if (h->hw_trans_mem_support) {
563 lcore_id = rte_lcore_id();
564 cached_free_slots = &h->local_free_slots[lcore_id];
565 /* Try to get a free slot from the local cache */
566 if (cached_free_slots->len == 0) {
567 /* Need to get another burst of free slots from global ring */
568 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
569 cached_free_slots->objs, LCORE_CACHE_SIZE);
573 cached_free_slots->len += n_slots;
576 /* Get a free slot from the local cache */
577 cached_free_slots->len--;
578 slot_id = cached_free_slots->objs[cached_free_slots->len];
580 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
584 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
585 rte_prefetch0(new_k);
586 new_idx = (uint32_t)((uintptr_t) slot_id);
588 /* Check if key is already inserted in primary location */
589 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
590 if (prim_bkt->signatures[i].current == sig &&
591 prim_bkt->signatures[i].alt == alt_hash) {
592 k = (struct rte_hash_key *) ((char *)keys +
593 prim_bkt->key_idx[i] * h->key_entry_size);
594 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
595 /* Enqueue index of free slot back in the ring. */
596 enqueue_slot_back(h, cached_free_slots, slot_id);
600 * Return index where key is stored,
601 * substracting the first dummy index
603 return (prim_bkt->key_idx[i] - 1);
608 /* Check if key is already inserted in secondary location */
609 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
610 if (sec_bkt->signatures[i].alt == sig &&
611 sec_bkt->signatures[i].current == alt_hash) {
612 k = (struct rte_hash_key *) ((char *)keys +
613 sec_bkt->key_idx[i] * h->key_entry_size);
614 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
615 /* Enqueue index of free slot back in the ring. */
616 enqueue_slot_back(h, cached_free_slots, slot_id);
620 * Return index where key is stored,
621 * substracting the first dummy index
623 return (sec_bkt->key_idx[i] - 1);
629 rte_memcpy(new_k->key, key, h->key_len);
632 /* Insert new entry is there is room in the primary bucket */
633 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
634 /* Check if slot is available */
635 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
636 prim_bkt->signatures[i].current = sig;
637 prim_bkt->signatures[i].alt = alt_hash;
638 prim_bkt->key_idx[i] = new_idx;
643 /* Primary bucket is full, so we need to make space for new entry */
644 ret = make_space_bucket(h, prim_bkt);
646 * After recursive function.
647 * Insert the new entry in the position of the pushed entry
648 * if successful or return error and
649 * store the new slot back in the ring
652 prim_bkt->signatures[ret].current = sig;
653 prim_bkt->signatures[ret].alt = alt_hash;
654 prim_bkt->key_idx[ret] = new_idx;
655 return (new_idx - 1);
658 /* Error in addition, store new slot back in the ring and return error */
659 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
665 rte_hash_add_key_with_hash(const struct rte_hash *h,
666 const void *key, hash_sig_t sig)
668 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
669 return __rte_hash_add_key_with_hash(h, key, sig, 0);
673 rte_hash_add_key(const struct rte_hash *h, const void *key)
675 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
676 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
680 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
681 const void *key, hash_sig_t sig, void *data)
685 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
686 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
694 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
698 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
700 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
706 static inline int32_t
707 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
708 hash_sig_t sig, void **data)
713 struct rte_hash_bucket *bkt;
714 struct rte_hash_key *k, *keys = h->key_store;
716 bucket_idx = sig & h->bucket_bitmask;
717 bkt = &h->buckets[bucket_idx];
719 /* Check if key is in primary location */
720 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
721 if (bkt->signatures[i].current == sig &&
722 bkt->signatures[i].sig != NULL_SIGNATURE) {
723 k = (struct rte_hash_key *) ((char *)keys +
724 bkt->key_idx[i] * h->key_entry_size);
725 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
729 * Return index where key is stored,
730 * substracting the first dummy index
732 return (bkt->key_idx[i] - 1);
737 /* Calculate secondary hash */
738 alt_hash = rte_hash_secondary_hash(sig);
739 bucket_idx = alt_hash & h->bucket_bitmask;
740 bkt = &h->buckets[bucket_idx];
742 /* Check if key is in secondary location */
743 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
744 if (bkt->signatures[i].current == alt_hash &&
745 bkt->signatures[i].alt == sig) {
746 k = (struct rte_hash_key *) ((char *)keys +
747 bkt->key_idx[i] * h->key_entry_size);
748 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
752 * Return index where key is stored,
753 * substracting the first dummy index
755 return (bkt->key_idx[i] - 1);
764 rte_hash_lookup_with_hash(const struct rte_hash *h,
765 const void *key, hash_sig_t sig)
767 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
768 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
772 rte_hash_lookup(const struct rte_hash *h, const void *key)
774 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
775 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
779 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
780 const void *key, hash_sig_t sig, void **data)
782 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
783 return __rte_hash_lookup_with_hash(h, key, sig, data);
787 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
789 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
790 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
794 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
796 unsigned lcore_id, n_slots;
797 struct lcore_cache *cached_free_slots;
799 bkt->signatures[i].sig = NULL_SIGNATURE;
800 if (h->hw_trans_mem_support) {
801 lcore_id = rte_lcore_id();
802 cached_free_slots = &h->local_free_slots[lcore_id];
803 /* Cache full, need to free it. */
804 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
805 /* Need to enqueue the free slots in global ring. */
806 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
807 cached_free_slots->objs,
809 cached_free_slots->len -= n_slots;
811 /* Put index of new free slot in cache. */
812 cached_free_slots->objs[cached_free_slots->len] =
813 (void *)((uintptr_t)bkt->key_idx[i]);
814 cached_free_slots->len++;
816 rte_ring_sp_enqueue(h->free_slots,
817 (void *)((uintptr_t)bkt->key_idx[i]));
821 static inline int32_t
822 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
828 struct rte_hash_bucket *bkt;
829 struct rte_hash_key *k, *keys = h->key_store;
831 bucket_idx = sig & h->bucket_bitmask;
832 bkt = &h->buckets[bucket_idx];
834 /* Check if key is in primary location */
835 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
836 if (bkt->signatures[i].current == sig &&
837 bkt->signatures[i].sig != NULL_SIGNATURE) {
838 k = (struct rte_hash_key *) ((char *)keys +
839 bkt->key_idx[i] * h->key_entry_size);
840 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
841 remove_entry(h, bkt, i);
844 * Return index where key is stored,
845 * substracting the first dummy index
847 return (bkt->key_idx[i] - 1);
852 /* Calculate secondary hash */
853 alt_hash = rte_hash_secondary_hash(sig);
854 bucket_idx = alt_hash & h->bucket_bitmask;
855 bkt = &h->buckets[bucket_idx];
857 /* Check if key is in secondary location */
858 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
859 if (bkt->signatures[i].current == alt_hash &&
860 bkt->signatures[i].sig != NULL_SIGNATURE) {
861 k = (struct rte_hash_key *) ((char *)keys +
862 bkt->key_idx[i] * h->key_entry_size);
863 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
864 remove_entry(h, bkt, i);
867 * Return index where key is stored,
868 * substracting the first dummy index
870 return (bkt->key_idx[i] - 1);
879 rte_hash_del_key_with_hash(const struct rte_hash *h,
880 const void *key, hash_sig_t sig)
882 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
883 return __rte_hash_del_key_with_hash(h, key, sig);
887 rte_hash_del_key(const struct rte_hash *h, const void *key)
889 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
890 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
893 /* Lookup bulk stage 0: Prefetch input key */
895 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
896 const void * const *keys)
898 *idx = __builtin_ctzl(*lookup_mask);
899 if (*lookup_mask == 0)
902 rte_prefetch0(keys[*idx]);
903 *lookup_mask &= ~(1llu << *idx);
907 * Lookup bulk stage 1: Calculate primary/secondary hashes
908 * and prefetch primary/secondary buckets
911 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
912 const struct rte_hash_bucket **primary_bkt,
913 const struct rte_hash_bucket **secondary_bkt,
914 hash_sig_t *hash_vals, const void * const *keys,
915 const struct rte_hash *h)
917 *prim_hash = rte_hash_hash(h, keys[idx]);
918 hash_vals[idx] = *prim_hash;
919 *sec_hash = rte_hash_secondary_hash(*prim_hash);
921 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
922 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
924 rte_prefetch0(*primary_bkt);
925 rte_prefetch0(*secondary_bkt);
929 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
930 * and prefetch first key slot
933 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
934 const struct rte_hash_bucket *prim_bkt,
935 const struct rte_hash_bucket *sec_bkt,
936 const struct rte_hash_key **key_slot, int32_t *positions,
937 uint64_t *extra_hits_mask, const void *keys,
938 const struct rte_hash *h)
940 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
941 unsigned total_hash_matches;
943 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
944 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
945 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
946 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
947 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
950 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
952 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
954 total_hash_matches = (prim_hash_matches |
955 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
956 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
957 key_idx * h->key_entry_size);
959 rte_prefetch0(*key_slot);
961 * Return index where key is stored,
962 * substracting the first dummy index
964 positions[idx] = (key_idx - 1);
966 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
971 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
973 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
974 const int32_t *positions, void *data[], uint64_t *hits,
975 const struct rte_hash *h)
980 hit = !h->rte_hash_cmp_eq(key_slot->key, keys[idx], h->key_len);
982 data[idx] = key_slot->pdata;
984 key_idx = positions[idx] + 1;
986 * If key index is 0, force hit to be 0, in case key to be looked up
987 * is all zero (as in the dummy slot), which would result in a wrong hit
989 *hits |= (uint64_t)(hit && !!key_idx) << idx;
993 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
994 uint32_t num_keys, int32_t *positions,
995 uint64_t *hit_mask, void *data[])
998 uint64_t extra_hits_mask = 0;
999 uint64_t lookup_mask, miss_mask;
1001 const void *key_store = h->key_store;
1003 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1005 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1006 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1007 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1008 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1009 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1010 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1011 hash_sig_t primary_hash10, primary_hash11;
1012 hash_sig_t secondary_hash10, secondary_hash11;
1013 hash_sig_t primary_hash20, primary_hash21;
1014 hash_sig_t secondary_hash20, secondary_hash21;
1016 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1017 miss_mask = lookup_mask;
1019 lookup_stage0(&idx00, &lookup_mask, keys);
1020 lookup_stage0(&idx01, &lookup_mask, keys);
1022 idx10 = idx00, idx11 = idx01;
1024 lookup_stage0(&idx00, &lookup_mask, keys);
1025 lookup_stage0(&idx01, &lookup_mask, keys);
1026 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1027 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1028 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1029 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1031 primary_bkt20 = primary_bkt10;
1032 primary_bkt21 = primary_bkt11;
1033 secondary_bkt20 = secondary_bkt10;
1034 secondary_bkt21 = secondary_bkt11;
1035 primary_hash20 = primary_hash10;
1036 primary_hash21 = primary_hash11;
1037 secondary_hash20 = secondary_hash10;
1038 secondary_hash21 = secondary_hash11;
1039 idx20 = idx10, idx21 = idx11;
1040 idx10 = idx00, idx11 = idx01;
1042 lookup_stage0(&idx00, &lookup_mask, keys);
1043 lookup_stage0(&idx01, &lookup_mask, keys);
1044 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1045 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1046 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1047 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1048 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1049 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1051 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1052 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1055 while (lookup_mask) {
1056 k_slot30 = k_slot20, k_slot31 = k_slot21;
1057 idx30 = idx20, idx31 = idx21;
1058 primary_bkt20 = primary_bkt10;
1059 primary_bkt21 = primary_bkt11;
1060 secondary_bkt20 = secondary_bkt10;
1061 secondary_bkt21 = secondary_bkt11;
1062 primary_hash20 = primary_hash10;
1063 primary_hash21 = primary_hash11;
1064 secondary_hash20 = secondary_hash10;
1065 secondary_hash21 = secondary_hash11;
1066 idx20 = idx10, idx21 = idx11;
1067 idx10 = idx00, idx11 = idx01;
1069 lookup_stage0(&idx00, &lookup_mask, keys);
1070 lookup_stage0(&idx01, &lookup_mask, keys);
1071 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1072 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1073 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1074 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1075 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1076 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1077 &extra_hits_mask, key_store, h);
1078 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1079 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1080 &extra_hits_mask, key_store, h);
1081 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1082 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1085 k_slot30 = k_slot20, k_slot31 = k_slot21;
1086 idx30 = idx20, idx31 = idx21;
1087 primary_bkt20 = primary_bkt10;
1088 primary_bkt21 = primary_bkt11;
1089 secondary_bkt20 = secondary_bkt10;
1090 secondary_bkt21 = secondary_bkt11;
1091 primary_hash20 = primary_hash10;
1092 primary_hash21 = primary_hash11;
1093 secondary_hash20 = secondary_hash10;
1094 secondary_hash21 = secondary_hash11;
1095 idx20 = idx10, idx21 = idx11;
1096 idx10 = idx00, idx11 = idx01;
1098 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1099 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1100 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1101 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1102 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1103 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1105 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1106 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1108 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1109 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1111 k_slot30 = k_slot20, k_slot31 = k_slot21;
1112 idx30 = idx20, idx31 = idx21;
1113 primary_bkt20 = primary_bkt10;
1114 primary_bkt21 = primary_bkt11;
1115 secondary_bkt20 = secondary_bkt10;
1116 secondary_bkt21 = secondary_bkt11;
1117 primary_hash20 = primary_hash10;
1118 primary_hash21 = primary_hash11;
1119 secondary_hash20 = secondary_hash10;
1120 secondary_hash21 = secondary_hash11;
1121 idx20 = idx10, idx21 = idx11;
1123 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1124 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1126 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1127 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1129 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1130 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1132 k_slot30 = k_slot20, k_slot31 = k_slot21;
1133 idx30 = idx20, idx31 = idx21;
1135 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1136 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1138 /* ignore any items we have already found */
1139 extra_hits_mask &= ~hits;
1141 if (unlikely(extra_hits_mask)) {
1142 /* run a single search for each remaining item */
1144 idx = __builtin_ctzl(extra_hits_mask);
1146 ret = rte_hash_lookup_with_hash_data(h,
1147 keys[idx], hash_vals[idx], &data[idx]);
1149 hits |= 1ULL << idx;
1151 positions[idx] = rte_hash_lookup_with_hash(h,
1152 keys[idx], hash_vals[idx]);
1153 if (positions[idx] >= 0)
1154 hits |= 1llu << idx;
1156 extra_hits_mask &= ~(1llu << idx);
1157 } while (extra_hits_mask);
1161 if (unlikely(miss_mask)) {
1163 idx = __builtin_ctzl(miss_mask);
1164 positions[idx] = -ENOENT;
1165 miss_mask &= ~(1llu << idx);
1166 } while (miss_mask);
1169 if (hit_mask != NULL)
1174 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1175 uint32_t num_keys, int32_t *positions)
1177 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1178 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1179 (positions == NULL)), -EINVAL);
1181 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1186 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1187 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1189 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1190 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1191 (hit_mask == NULL)), -EINVAL);
1193 int32_t positions[num_keys];
1195 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1197 /* Return number of hits */
1198 return __builtin_popcountl(*hit_mask);
1202 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1204 uint32_t bucket_idx, idx, position;
1205 struct rte_hash_key *next_key;
1207 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1209 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1211 if (*next >= total_entries)
1214 /* Calculate bucket and index of current iterator */
1215 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1216 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1218 /* If current position is empty, go to the next one */
1219 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1222 if (*next == total_entries)
1224 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1225 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1228 /* Get position of entry in key table */
1229 position = h->buckets[bucket_idx].key_idx[idx];
1230 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1231 position * h->key_entry_size);
1232 /* Return key and data */
1233 *key = next_key->key;
1234 *data = next_key->pdata;
1236 /* Increment iterator */
1239 return (position - 1);