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 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
68 static struct rte_tailq_elem rte_hash_tailq = {
71 EAL_REGISTER_TAILQ(rte_hash_tailq)
73 /* Macro to enable/disable run-time checking of function parameters */
74 #if defined(RTE_LIBRTE_HASH_DEBUG)
75 #define RETURN_IF_TRUE(cond, retval) do { \
80 #define RETURN_IF_TRUE(cond, retval)
83 /* Hash function used if none is specified */
84 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
85 #include <rte_hash_crc.h>
86 #define DEFAULT_HASH_FUNC rte_hash_crc
88 #include <rte_jhash.h>
89 #define DEFAULT_HASH_FUNC rte_jhash
92 /** Number of items per bucket. */
93 #define RTE_HASH_BUCKET_ENTRIES 4
95 #define NULL_SIGNATURE 0
97 #define KEY_ALIGNMENT 16
99 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
101 /** A hash table structure. */
103 char name[RTE_HASH_NAMESIZE]; /**< Name of the hash. */
104 uint32_t entries; /**< Total table entries. */
105 uint32_t num_buckets; /**< Number of buckets in table. */
106 uint32_t key_len; /**< Length of hash key. */
107 rte_hash_function hash_func; /**< Function used to calculate hash. */
108 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
109 rte_hash_cmp_eq_t rte_hash_cmp_eq; /**< Function used to compare keys. */
110 uint32_t bucket_bitmask; /**< Bitmask for getting bucket index
111 from hash signature. */
112 uint32_t key_entry_size; /**< Size of each key entry. */
114 struct rte_ring *free_slots; /**< Ring that stores all indexes
115 of the free slots in the key table */
116 void *key_store; /**< Table storing all keys and data */
117 struct rte_hash_bucket *buckets; /**< Table with buckets storing all the
118 hash values and key indexes
120 } __rte_cache_aligned;
122 /* Structure storing both primary and secondary hashes */
123 struct rte_hash_signatures {
133 /* Structure that stores key-value pair */
134 struct rte_hash_key {
139 /* Variable key size */
141 } __attribute__((aligned(KEY_ALIGNMENT)));
143 /** Bucket structure */
144 struct rte_hash_bucket {
145 struct rte_hash_signatures signatures[RTE_HASH_BUCKET_ENTRIES];
146 /* Includes dummy key index that always contains index 0 */
147 uint32_t key_idx[RTE_HASH_BUCKET_ENTRIES + 1];
148 uint8_t flag[RTE_HASH_BUCKET_ENTRIES];
149 } __rte_cache_aligned;
152 rte_hash_find_existing(const char *name)
154 struct rte_hash *h = NULL;
155 struct rte_tailq_entry *te;
156 struct rte_hash_list *hash_list;
158 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
160 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
161 TAILQ_FOREACH(te, hash_list, next) {
162 h = (struct rte_hash *) te->data;
163 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
166 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
176 rte_hash_create(const struct rte_hash_parameters *params)
178 struct rte_hash *h = NULL;
179 struct rte_tailq_entry *te = NULL;
180 struct rte_hash_list *hash_list;
181 struct rte_ring *r = NULL;
182 char hash_name[RTE_HASH_NAMESIZE];
183 void *ptr, *k = NULL;
184 void *buckets = NULL;
185 char ring_name[RTE_RING_NAMESIZE];
188 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
190 if (params == NULL) {
191 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
195 /* Check for valid parameters */
196 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
197 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
198 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
199 (params->key_len == 0)) {
201 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
205 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
207 /* Guarantee there's no existing */
208 h = rte_hash_find_existing(params->name);
212 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
214 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
218 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
219 RTE_CACHE_LINE_SIZE, params->socket_id);
222 RTE_LOG(ERR, HASH, "memory allocation failed\n");
226 const uint32_t num_buckets = rte_align32pow2(params->entries)
227 / RTE_HASH_BUCKET_ENTRIES;
229 buckets = rte_zmalloc_socket(NULL,
230 num_buckets * sizeof(struct rte_hash_bucket),
231 RTE_CACHE_LINE_SIZE, params->socket_id);
233 if (buckets == NULL) {
234 RTE_LOG(ERR, HASH, "memory allocation failed\n");
238 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
240 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
241 const uint64_t key_tbl_size = (uint64_t) key_entry_size * (params->entries + 1);
243 k = rte_zmalloc_socket(NULL, key_tbl_size,
244 RTE_CACHE_LINE_SIZE, params->socket_id);
247 RTE_LOG(ERR, HASH, "memory allocation failed\n");
252 * If x86 architecture is used, select appropriate compare function,
253 * which may use x86 instrinsics, otherwise use memcmp
255 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686) || defined(RTE_ARCH_X86_X32)
256 /* Select function to compare keys */
257 switch (params->key_len) {
259 h->rte_hash_cmp_eq = rte_hash_k16_cmp_eq;
262 h->rte_hash_cmp_eq = rte_hash_k32_cmp_eq;
265 h->rte_hash_cmp_eq = rte_hash_k48_cmp_eq;
268 h->rte_hash_cmp_eq = rte_hash_k64_cmp_eq;
271 h->rte_hash_cmp_eq = rte_hash_k80_cmp_eq;
274 h->rte_hash_cmp_eq = rte_hash_k96_cmp_eq;
277 h->rte_hash_cmp_eq = rte_hash_k112_cmp_eq;
280 h->rte_hash_cmp_eq = rte_hash_k128_cmp_eq;
283 /* If key is not multiple of 16, use generic memcmp */
284 h->rte_hash_cmp_eq = memcmp;
287 h->rte_hash_cmp_eq = memcmp;
290 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
291 r = rte_ring_lookup(ring_name);
293 /* clear the free ring */
294 while (rte_ring_dequeue(r, &ptr) == 0)
297 r = rte_ring_create(ring_name, rte_align32pow2(params->entries + 1),
298 params->socket_id, 0);
300 RTE_LOG(ERR, HASH, "memory allocation failed\n");
304 /* Setup hash context */
305 snprintf(h->name, sizeof(h->name), "%s", params->name);
306 h->entries = params->entries;
307 h->key_len = params->key_len;
308 h->key_entry_size = key_entry_size;
309 h->hash_func_init_val = params->hash_func_init_val;
311 h->num_buckets = num_buckets;
312 h->bucket_bitmask = h->num_buckets - 1;
313 h->buckets = buckets;
314 h->hash_func = (params->hash_func == NULL) ?
315 DEFAULT_HASH_FUNC : params->hash_func;
320 /* populate the free slots ring. Entry zero is reserved for key misses */
321 for (i = 1; i < params->entries + 1; i++)
322 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
324 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
325 te->data = (void *) h;
326 TAILQ_INSERT_TAIL(hash_list, te, next);
327 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
339 rte_hash_free(struct rte_hash *h)
341 struct rte_tailq_entry *te;
342 struct rte_hash_list *hash_list;
347 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
349 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
351 /* find out tailq entry */
352 TAILQ_FOREACH(te, hash_list, next) {
353 if (te->data == (void *) h)
358 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
362 TAILQ_REMOVE(hash_list, te, next);
364 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
366 rte_free(h->key_store);
367 rte_free(h->buckets);
373 rte_hash_hash(const struct rte_hash *h, const void *key)
375 /* calc hash result by key */
376 return h->hash_func(key, h->key_len, h->hash_func_init_val);
379 /* Calc the secondary hash value from the primary hash value of a given key */
380 static inline hash_sig_t
381 rte_hash_secondary_hash(const hash_sig_t primary_hash)
383 static const unsigned all_bits_shift = 12;
384 static const unsigned alt_bits_xor = 0x5bd1e995;
386 uint32_t tag = primary_hash >> all_bits_shift;
388 return (primary_hash ^ ((tag + 1) * alt_bits_xor));
392 rte_hash_reset(struct rte_hash *h)
400 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
401 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
403 /* clear the free ring */
404 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
407 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
408 for (i = 1; i < h->entries + 1; i++)
409 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
412 /* Search for an entry that can be pushed to its alternative location */
414 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
418 uint32_t next_bucket_idx;
419 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
422 * Push existing item (search for bucket with space in
423 * alternative locations) to its alternative location
425 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
426 /* Search for space in alternative locations */
427 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
428 next_bkt[i] = &h->buckets[next_bucket_idx];
429 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
430 if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
434 if (j != RTE_HASH_BUCKET_ENTRIES)
438 /* Alternative location has spare room (end of recursive function) */
439 if (i != RTE_HASH_BUCKET_ENTRIES) {
440 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
441 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
442 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
446 /* Pick entry that has not been pushed yet */
447 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
448 if (bkt->flag[i] == 0)
451 /* All entries have been pushed, so entry cannot be added */
452 if (i == RTE_HASH_BUCKET_ENTRIES)
455 /* Set flag to indicate that this entry is going to be pushed */
457 /* Need room in alternative bucket to insert the pushed entry */
458 ret = make_space_bucket(h, next_bkt[i]);
460 * After recursive function.
461 * Clear flags and insert the pushed entry
462 * in its alternative location if successful,
467 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
468 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
469 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
476 static inline int32_t
477 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
478 hash_sig_t sig, void *data)
481 uint32_t prim_bucket_idx, sec_bucket_idx;
483 struct rte_hash_bucket *prim_bkt, *sec_bkt;
484 struct rte_hash_key *new_k, *k, *keys = h->key_store;
489 prim_bucket_idx = sig & h->bucket_bitmask;
490 prim_bkt = &h->buckets[prim_bucket_idx];
491 rte_prefetch0(prim_bkt);
493 alt_hash = rte_hash_secondary_hash(sig);
494 sec_bucket_idx = alt_hash & h->bucket_bitmask;
495 sec_bkt = &h->buckets[sec_bucket_idx];
496 rte_prefetch0(sec_bkt);
498 /* Get a new slot for storing the new key */
499 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
501 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
502 rte_prefetch0(new_k);
503 new_idx = (uint32_t)((uintptr_t) slot_id);
505 /* Check if key is already inserted in primary location */
506 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
507 if (prim_bkt->signatures[i].current == sig &&
508 prim_bkt->signatures[i].alt == alt_hash) {
509 k = (struct rte_hash_key *) ((char *)keys +
510 prim_bkt->key_idx[i] * h->key_entry_size);
511 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
512 rte_ring_sp_enqueue(h->free_slots, slot_id);
516 * Return index where key is stored,
517 * substracting the first dummy index
519 return (prim_bkt->key_idx[i] - 1);
524 /* Check if key is already inserted in secondary location */
525 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
526 if (sec_bkt->signatures[i].alt == sig &&
527 sec_bkt->signatures[i].current == alt_hash) {
528 k = (struct rte_hash_key *) ((char *)keys +
529 sec_bkt->key_idx[i] * h->key_entry_size);
530 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
531 rte_ring_sp_enqueue(h->free_slots, slot_id);
535 * Return index where key is stored,
536 * substracting the first dummy index
538 return (sec_bkt->key_idx[i] - 1);
544 rte_memcpy(new_k->key, key, h->key_len);
547 /* Insert new entry is there is room in the primary bucket */
548 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
549 /* Check if slot is available */
550 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
551 prim_bkt->signatures[i].current = sig;
552 prim_bkt->signatures[i].alt = alt_hash;
553 prim_bkt->key_idx[i] = new_idx;
558 /* Primary bucket is full, so we need to make space for new entry */
559 ret = make_space_bucket(h, prim_bkt);
561 * After recursive function.
562 * Insert the new entry in the position of the pushed entry
563 * if successful or return error and
564 * store the new slot back in the ring
567 prim_bkt->signatures[ret].current = sig;
568 prim_bkt->signatures[ret].alt = alt_hash;
569 prim_bkt->key_idx[ret] = new_idx;
570 return (new_idx - 1);
573 /* Error in addition, store new slot back in the ring and return error */
574 rte_ring_sp_enqueue(h->free_slots,
575 (void *)((uintptr_t) new_idx));
581 rte_hash_add_key_with_hash(const struct rte_hash *h,
582 const void *key, hash_sig_t sig)
584 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
585 return __rte_hash_add_key_with_hash(h, key, sig, 0);
589 rte_hash_add_key(const struct rte_hash *h, const void *key)
591 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
592 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
596 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
597 const void *key, hash_sig_t sig, void *data)
601 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
602 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
610 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
614 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
616 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
622 static inline int32_t
623 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
624 hash_sig_t sig, void **data)
629 struct rte_hash_bucket *bkt;
630 struct rte_hash_key *k, *keys = h->key_store;
632 bucket_idx = sig & h->bucket_bitmask;
633 bkt = &h->buckets[bucket_idx];
635 /* Check if key is in primary location */
636 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
637 if (bkt->signatures[i].current == sig &&
638 bkt->signatures[i].sig != NULL_SIGNATURE) {
639 k = (struct rte_hash_key *) ((char *)keys +
640 bkt->key_idx[i] * h->key_entry_size);
641 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
645 * Return index where key is stored,
646 * substracting the first dummy index
648 return (bkt->key_idx[i] - 1);
653 /* Calculate secondary hash */
654 alt_hash = rte_hash_secondary_hash(sig);
655 bucket_idx = alt_hash & h->bucket_bitmask;
656 bkt = &h->buckets[bucket_idx];
658 /* Check if key is in secondary location */
659 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
660 if (bkt->signatures[i].current == alt_hash &&
661 bkt->signatures[i].alt == sig) {
662 k = (struct rte_hash_key *) ((char *)keys +
663 bkt->key_idx[i] * h->key_entry_size);
664 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
668 * Return index where key is stored,
669 * substracting the first dummy index
671 return (bkt->key_idx[i] - 1);
680 rte_hash_lookup_with_hash(const struct rte_hash *h,
681 const void *key, hash_sig_t sig)
683 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
684 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
688 rte_hash_lookup(const struct rte_hash *h, const void *key)
690 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
691 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
695 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
696 const void *key, hash_sig_t sig, void **data)
698 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
699 return __rte_hash_lookup_with_hash(h, key, sig, data);
703 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
705 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
706 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
709 static inline int32_t
710 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
716 struct rte_hash_bucket *bkt;
717 struct rte_hash_key *k, *keys = h->key_store;
719 bucket_idx = sig & h->bucket_bitmask;
720 bkt = &h->buckets[bucket_idx];
722 /* Check if key is in primary location */
723 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
724 if (bkt->signatures[i].current == sig &&
725 bkt->signatures[i].sig != NULL_SIGNATURE) {
726 k = (struct rte_hash_key *) ((char *)keys +
727 bkt->key_idx[i] * h->key_entry_size);
728 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
729 bkt->signatures[i].sig = NULL_SIGNATURE;
730 rte_ring_sp_enqueue(h->free_slots,
731 (void *)((uintptr_t)bkt->key_idx[i]));
733 * Return index where key is stored,
734 * substracting the first dummy index
736 return (bkt->key_idx[i] - 1);
741 /* Calculate secondary hash */
742 alt_hash = rte_hash_secondary_hash(sig);
743 bucket_idx = alt_hash & h->bucket_bitmask;
744 bkt = &h->buckets[bucket_idx];
746 /* Check if key is in secondary location */
747 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
748 if (bkt->signatures[i].current == alt_hash &&
749 bkt->signatures[i].sig != NULL_SIGNATURE) {
750 k = (struct rte_hash_key *) ((char *)keys +
751 bkt->key_idx[i] * h->key_entry_size);
752 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
753 bkt->signatures[i].sig = NULL_SIGNATURE;
754 rte_ring_sp_enqueue(h->free_slots,
755 (void *)((uintptr_t)bkt->key_idx[i]));
757 * Return index where key is stored,
758 * substracting the first dummy index
760 return (bkt->key_idx[i] - 1);
769 rte_hash_del_key_with_hash(const struct rte_hash *h,
770 const void *key, hash_sig_t sig)
772 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
773 return __rte_hash_del_key_with_hash(h, key, sig);
777 rte_hash_del_key(const struct rte_hash *h, const void *key)
779 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
780 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
783 /* Lookup bulk stage 0: Prefetch input key */
785 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
786 const void * const *keys)
788 *idx = __builtin_ctzl(*lookup_mask);
789 if (*lookup_mask == 0)
792 rte_prefetch0(keys[*idx]);
793 *lookup_mask &= ~(1llu << *idx);
797 * Lookup bulk stage 1: Calculate primary/secondary hashes
798 * and prefetch primary/secondary buckets
801 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
802 const struct rte_hash_bucket **primary_bkt,
803 const struct rte_hash_bucket **secondary_bkt,
804 hash_sig_t *hash_vals, const void * const *keys,
805 const struct rte_hash *h)
807 *prim_hash = rte_hash_hash(h, keys[idx]);
808 hash_vals[idx] = *prim_hash;
809 *sec_hash = rte_hash_secondary_hash(*prim_hash);
811 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
812 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
814 rte_prefetch0(*primary_bkt);
815 rte_prefetch0(*secondary_bkt);
819 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
820 * and prefetch first key slot
823 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
824 const struct rte_hash_bucket *prim_bkt,
825 const struct rte_hash_bucket *sec_bkt,
826 const struct rte_hash_key **key_slot, int32_t *positions,
827 uint64_t *extra_hits_mask, const void *keys,
828 const struct rte_hash *h)
830 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
831 unsigned total_hash_matches;
833 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
834 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
835 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
836 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
837 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
840 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
842 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
844 total_hash_matches = (prim_hash_matches |
845 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
846 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
847 key_idx * h->key_entry_size);
849 rte_prefetch0(*key_slot);
851 * Return index where key is stored,
852 * substracting the first dummy index
854 positions[idx] = (key_idx - 1);
856 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
861 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
863 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
864 void *data[], uint64_t *hits, const struct rte_hash *h)
868 hit = !h->rte_hash_cmp_eq(key_slot->key, keys[idx], h->key_len);
870 data[idx] = key_slot->pdata;
872 *hits |= (uint64_t)(hit) << idx;
876 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
877 uint32_t num_keys, int32_t *positions,
878 uint64_t *hit_mask, void *data[])
881 uint64_t extra_hits_mask = 0;
882 uint64_t lookup_mask, miss_mask;
884 const void *key_store = h->key_store;
886 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
888 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
889 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
890 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
891 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
892 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
893 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
894 hash_sig_t primary_hash10, primary_hash11;
895 hash_sig_t secondary_hash10, secondary_hash11;
896 hash_sig_t primary_hash20, primary_hash21;
897 hash_sig_t secondary_hash20, secondary_hash21;
899 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
900 miss_mask = lookup_mask;
902 lookup_stage0(&idx00, &lookup_mask, keys);
903 lookup_stage0(&idx01, &lookup_mask, keys);
905 idx10 = idx00, idx11 = idx01;
907 lookup_stage0(&idx00, &lookup_mask, keys);
908 lookup_stage0(&idx01, &lookup_mask, keys);
909 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
910 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
911 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
912 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
914 primary_bkt20 = primary_bkt10;
915 primary_bkt21 = primary_bkt11;
916 secondary_bkt20 = secondary_bkt10;
917 secondary_bkt21 = secondary_bkt11;
918 primary_hash20 = primary_hash10;
919 primary_hash21 = primary_hash11;
920 secondary_hash20 = secondary_hash10;
921 secondary_hash21 = secondary_hash11;
922 idx20 = idx10, idx21 = idx11;
923 idx10 = idx00, idx11 = idx01;
925 lookup_stage0(&idx00, &lookup_mask, keys);
926 lookup_stage0(&idx01, &lookup_mask, keys);
927 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
928 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
929 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
930 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
931 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
932 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
934 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
935 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
938 while (lookup_mask) {
939 k_slot30 = k_slot20, k_slot31 = k_slot21;
940 idx30 = idx20, idx31 = idx21;
941 primary_bkt20 = primary_bkt10;
942 primary_bkt21 = primary_bkt11;
943 secondary_bkt20 = secondary_bkt10;
944 secondary_bkt21 = secondary_bkt11;
945 primary_hash20 = primary_hash10;
946 primary_hash21 = primary_hash11;
947 secondary_hash20 = secondary_hash10;
948 secondary_hash21 = secondary_hash11;
949 idx20 = idx10, idx21 = idx11;
950 idx10 = idx00, idx11 = idx01;
952 lookup_stage0(&idx00, &lookup_mask, keys);
953 lookup_stage0(&idx01, &lookup_mask, keys);
954 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
955 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
956 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
957 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
958 lookup_stage2(idx20, primary_hash20, secondary_hash20,
959 primary_bkt20, secondary_bkt20, &k_slot20, positions,
960 &extra_hits_mask, key_store, h);
961 lookup_stage2(idx21, primary_hash21, secondary_hash21,
962 primary_bkt21, secondary_bkt21, &k_slot21, positions,
963 &extra_hits_mask, key_store, h);
964 lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
965 lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
968 k_slot30 = k_slot20, k_slot31 = k_slot21;
969 idx30 = idx20, idx31 = idx21;
970 primary_bkt20 = primary_bkt10;
971 primary_bkt21 = primary_bkt11;
972 secondary_bkt20 = secondary_bkt10;
973 secondary_bkt21 = secondary_bkt11;
974 primary_hash20 = primary_hash10;
975 primary_hash21 = primary_hash11;
976 secondary_hash20 = secondary_hash10;
977 secondary_hash21 = secondary_hash11;
978 idx20 = idx10, idx21 = idx11;
979 idx10 = idx00, idx11 = idx01;
981 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
982 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
983 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
984 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
985 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
986 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
988 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
989 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
991 lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
992 lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
994 k_slot30 = k_slot20, k_slot31 = k_slot21;
995 idx30 = idx20, idx31 = idx21;
996 primary_bkt20 = primary_bkt10;
997 primary_bkt21 = primary_bkt11;
998 secondary_bkt20 = secondary_bkt10;
999 secondary_bkt21 = secondary_bkt11;
1000 primary_hash20 = primary_hash10;
1001 primary_hash21 = primary_hash11;
1002 secondary_hash20 = secondary_hash10;
1003 secondary_hash21 = secondary_hash11;
1004 idx20 = idx10, idx21 = idx11;
1006 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1007 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1009 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1010 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1012 lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
1013 lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
1015 k_slot30 = k_slot20, k_slot31 = k_slot21;
1016 idx30 = idx20, idx31 = idx21;
1018 lookup_stage3(idx30, k_slot30, keys, data, &hits, h);
1019 lookup_stage3(idx31, k_slot31, keys, data, &hits, h);
1021 /* ignore any items we have already found */
1022 extra_hits_mask &= ~hits;
1024 if (unlikely(extra_hits_mask)) {
1025 /* run a single search for each remaining item */
1027 idx = __builtin_ctzl(extra_hits_mask);
1029 ret = rte_hash_lookup_with_hash_data(h,
1030 keys[idx], hash_vals[idx], &data[idx]);
1032 hits |= 1ULL << idx;
1034 positions[idx] = rte_hash_lookup_with_hash(h,
1035 keys[idx], hash_vals[idx]);
1036 if (positions[idx] >= 0)
1037 hits |= 1llu << idx;
1039 extra_hits_mask &= ~(1llu << idx);
1040 } while (extra_hits_mask);
1044 if (unlikely(miss_mask)) {
1046 idx = __builtin_ctzl(miss_mask);
1047 positions[idx] = -ENOENT;
1048 miss_mask &= ~(1llu << idx);
1049 } while (miss_mask);
1052 if (hit_mask != NULL)
1057 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1058 uint32_t num_keys, int32_t *positions)
1060 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1061 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1062 (positions == NULL)), -EINVAL);
1064 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1069 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1070 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1072 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1073 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1074 (hit_mask == NULL)), -EINVAL);
1076 int32_t positions[num_keys];
1078 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1080 /* Return number of hits */
1081 return __builtin_popcountl(*hit_mask);
1085 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1087 uint32_t bucket_idx, idx, position;
1088 struct rte_hash_key *next_key;
1090 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1092 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1094 if (*next >= total_entries)
1097 /* Calculate bucket and index of current iterator */
1098 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1099 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1101 /* If current position is empty, go to the next one */
1102 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1105 if (*next == total_entries)
1107 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1108 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1111 /* Get position of entry in key table */
1112 position = h->buckets[bucket_idx].key_idx[idx];
1113 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1114 position * h->key_entry_size);
1115 /* Return key and data */
1116 *key = next_key->key;
1117 *data = next_key->pdata;
1119 /* Increment iterator */
1122 return (position - 1);