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
106 unsigned len; /**< Cache len */
107 void *objs[LCORE_CACHE_SIZE]; /**< Cache objects */
108 } __rte_cache_aligned;
110 /** A hash table structure. */
112 char name[RTE_HASH_NAMESIZE]; /**< Name of the hash. */
113 uint32_t entries; /**< Total table entries. */
114 uint32_t num_buckets; /**< Number of buckets in table. */
115 uint32_t key_len; /**< Length of hash key. */
116 rte_hash_function hash_func; /**< Function used to calculate hash. */
117 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
118 rte_hash_cmp_eq_t rte_hash_cmp_eq; /**< Function used to compare keys. */
119 uint32_t bucket_bitmask; /**< Bitmask for getting bucket index
120 from hash signature. */
121 uint32_t key_entry_size; /**< Size of each key entry. */
123 struct rte_ring *free_slots; /**< Ring that stores all indexes
124 of the free slots in the key table */
125 void *key_store; /**< Table storing all keys and data */
126 struct rte_hash_bucket *buckets; /**< Table with buckets storing all the
127 hash values and key indexes
129 uint8_t hw_trans_mem_support; /**< Hardware transactional
131 struct lcore_cache *local_free_slots;
132 /**< Local cache per lcore, storing some indexes of the free slots */
133 } __rte_cache_aligned;
135 /* Structure storing both primary and secondary hashes */
136 struct rte_hash_signatures {
146 /* Structure that stores key-value pair */
147 struct rte_hash_key {
152 /* Variable key size */
154 } __attribute__((aligned(KEY_ALIGNMENT)));
156 /** Bucket structure */
157 struct rte_hash_bucket {
158 struct rte_hash_signatures signatures[RTE_HASH_BUCKET_ENTRIES];
159 /* Includes dummy key index that always contains index 0 */
160 uint32_t key_idx[RTE_HASH_BUCKET_ENTRIES + 1];
161 uint8_t flag[RTE_HASH_BUCKET_ENTRIES];
162 } __rte_cache_aligned;
165 rte_hash_find_existing(const char *name)
167 struct rte_hash *h = NULL;
168 struct rte_tailq_entry *te;
169 struct rte_hash_list *hash_list;
171 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
173 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
174 TAILQ_FOREACH(te, hash_list, next) {
175 h = (struct rte_hash *) te->data;
176 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
179 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
188 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func)
190 h->rte_hash_cmp_eq = func;
194 rte_hash_create(const struct rte_hash_parameters *params)
196 struct rte_hash *h = NULL;
197 struct rte_tailq_entry *te = NULL;
198 struct rte_hash_list *hash_list;
199 struct rte_ring *r = NULL;
200 char hash_name[RTE_HASH_NAMESIZE];
202 void *buckets = NULL;
203 char ring_name[RTE_RING_NAMESIZE];
204 unsigned num_key_slots;
205 unsigned hw_trans_mem_support = 0;
208 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
210 if (params == NULL) {
211 RTE_LOG(ERR, HASH, "rte_hash_create has no parameters\n");
215 /* Check for valid parameters */
216 if ((params->entries > RTE_HASH_ENTRIES_MAX) ||
217 (params->entries < RTE_HASH_BUCKET_ENTRIES) ||
218 !rte_is_power_of_2(RTE_HASH_BUCKET_ENTRIES) ||
219 (params->key_len == 0)) {
221 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
225 /* Check extra flags field to check extra options. */
226 if (params->extra_flag & RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT)
227 hw_trans_mem_support = 1;
229 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
231 /* Guarantee there's no existing */
232 h = rte_hash_find_existing(params->name);
236 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
238 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
242 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, sizeof(struct rte_hash),
243 RTE_CACHE_LINE_SIZE, params->socket_id);
246 RTE_LOG(ERR, HASH, "memory allocation failed\n");
250 const uint32_t num_buckets = rte_align32pow2(params->entries)
251 / RTE_HASH_BUCKET_ENTRIES;
253 buckets = rte_zmalloc_socket(NULL,
254 num_buckets * sizeof(struct rte_hash_bucket),
255 RTE_CACHE_LINE_SIZE, params->socket_id);
257 if (buckets == NULL) {
258 RTE_LOG(ERR, HASH, "memory allocation failed\n");
262 const uint32_t key_entry_size = sizeof(struct rte_hash_key) + params->key_len;
264 /* Store all keys and leave the first entry as a dummy entry for lookup_bulk */
265 if (hw_trans_mem_support)
267 * Increase number of slots by total number of indices
268 * that can be stored in the lcore caches
269 * except for the first cache
271 num_key_slots = params->entries + (RTE_MAX_LCORE - 1) *
272 LCORE_CACHE_SIZE + 1;
274 num_key_slots = params->entries + 1;
276 const uint64_t key_tbl_size = (uint64_t) key_entry_size * num_key_slots;
278 k = rte_zmalloc_socket(NULL, key_tbl_size,
279 RTE_CACHE_LINE_SIZE, params->socket_id);
282 RTE_LOG(ERR, HASH, "memory allocation failed\n");
287 * If x86 architecture is used, select appropriate compare function,
288 * which may use x86 instrinsics, otherwise use memcmp
290 #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_I686) ||\
291 defined(RTE_ARCH_X86_X32) || defined(RTE_ARCH_ARM64)
292 /* Select function to compare keys */
293 switch (params->key_len) {
295 h->rte_hash_cmp_eq = rte_hash_k16_cmp_eq;
298 h->rte_hash_cmp_eq = rte_hash_k32_cmp_eq;
301 h->rte_hash_cmp_eq = rte_hash_k48_cmp_eq;
304 h->rte_hash_cmp_eq = rte_hash_k64_cmp_eq;
307 h->rte_hash_cmp_eq = rte_hash_k80_cmp_eq;
310 h->rte_hash_cmp_eq = rte_hash_k96_cmp_eq;
313 h->rte_hash_cmp_eq = rte_hash_k112_cmp_eq;
316 h->rte_hash_cmp_eq = rte_hash_k128_cmp_eq;
319 /* If key is not multiple of 16, use generic memcmp */
320 h->rte_hash_cmp_eq = memcmp;
323 h->rte_hash_cmp_eq = memcmp;
326 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
327 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
328 params->socket_id, 0);
330 RTE_LOG(ERR, HASH, "memory allocation failed\n");
334 if (hw_trans_mem_support) {
335 h->local_free_slots = rte_zmalloc_socket(NULL,
336 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
337 RTE_CACHE_LINE_SIZE, params->socket_id);
340 /* Setup hash context */
341 snprintf(h->name, sizeof(h->name), "%s", params->name);
342 h->entries = params->entries;
343 h->key_len = params->key_len;
344 h->key_entry_size = key_entry_size;
345 h->hash_func_init_val = params->hash_func_init_val;
347 h->num_buckets = num_buckets;
348 h->bucket_bitmask = h->num_buckets - 1;
349 h->buckets = buckets;
350 h->hash_func = (params->hash_func == NULL) ?
351 DEFAULT_HASH_FUNC : params->hash_func;
354 h->hw_trans_mem_support = hw_trans_mem_support;
356 /* populate the free slots ring. Entry zero is reserved for key misses */
357 for (i = 1; i < params->entries + 1; i++)
358 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
360 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
361 te->data = (void *) h;
362 TAILQ_INSERT_TAIL(hash_list, te, next);
363 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
375 rte_hash_free(struct rte_hash *h)
377 struct rte_tailq_entry *te;
378 struct rte_hash_list *hash_list;
383 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
385 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
387 /* find out tailq entry */
388 TAILQ_FOREACH(te, hash_list, next) {
389 if (te->data == (void *) h)
394 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
398 TAILQ_REMOVE(hash_list, te, next);
400 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
402 if (h->hw_trans_mem_support)
403 rte_free(h->local_free_slots);
405 rte_ring_free(h->free_slots);
406 rte_free(h->key_store);
407 rte_free(h->buckets);
413 rte_hash_hash(const struct rte_hash *h, const void *key)
415 /* calc hash result by key */
416 return h->hash_func(key, h->key_len, h->hash_func_init_val);
419 /* Calc the secondary hash value from the primary hash value of a given key */
420 static inline hash_sig_t
421 rte_hash_secondary_hash(const hash_sig_t primary_hash)
423 static const unsigned all_bits_shift = 12;
424 static const unsigned alt_bits_xor = 0x5bd1e995;
426 uint32_t tag = primary_hash >> all_bits_shift;
428 return primary_hash ^ ((tag + 1) * alt_bits_xor);
432 rte_hash_reset(struct rte_hash *h)
440 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
441 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
443 /* clear the free ring */
444 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
447 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
448 for (i = 1; i < h->entries + 1; i++)
449 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
451 if (h->hw_trans_mem_support) {
452 /* Reset local caches per lcore */
453 for (i = 0; i < RTE_MAX_LCORE; i++)
454 h->local_free_slots[i].len = 0;
458 /* Search for an entry that can be pushed to its alternative location */
460 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
464 uint32_t next_bucket_idx;
465 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
468 * Push existing item (search for bucket with space in
469 * alternative locations) to its alternative location
471 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
472 /* Search for space in alternative locations */
473 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
474 next_bkt[i] = &h->buckets[next_bucket_idx];
475 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
476 if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
480 if (j != RTE_HASH_BUCKET_ENTRIES)
484 /* Alternative location has spare room (end of recursive function) */
485 if (i != RTE_HASH_BUCKET_ENTRIES) {
486 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
487 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
488 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
492 /* Pick entry that has not been pushed yet */
493 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
494 if (bkt->flag[i] == 0)
497 /* All entries have been pushed, so entry cannot be added */
498 if (i == RTE_HASH_BUCKET_ENTRIES)
501 /* Set flag to indicate that this entry is going to be pushed */
503 /* Need room in alternative bucket to insert the pushed entry */
504 ret = make_space_bucket(h, next_bkt[i]);
506 * After recursive function.
507 * Clear flags and insert the pushed entry
508 * in its alternative location if successful,
513 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
514 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
515 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
523 * Function called to enqueue back an index in the cache/ring,
524 * as slot has not being used and it can be used in the
525 * next addition attempt.
528 enqueue_slot_back(const struct rte_hash *h,
529 struct lcore_cache *cached_free_slots,
532 if (h->hw_trans_mem_support) {
533 cached_free_slots->objs[cached_free_slots->len] = slot_id;
534 cached_free_slots->len++;
536 rte_ring_sp_enqueue(h->free_slots, slot_id);
539 static inline int32_t
540 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
541 hash_sig_t sig, void *data)
544 uint32_t prim_bucket_idx, sec_bucket_idx;
546 struct rte_hash_bucket *prim_bkt, *sec_bkt;
547 struct rte_hash_key *new_k, *k, *keys = h->key_store;
548 void *slot_id = NULL;
553 struct lcore_cache *cached_free_slots = NULL;
555 prim_bucket_idx = sig & h->bucket_bitmask;
556 prim_bkt = &h->buckets[prim_bucket_idx];
557 rte_prefetch0(prim_bkt);
559 alt_hash = rte_hash_secondary_hash(sig);
560 sec_bucket_idx = alt_hash & h->bucket_bitmask;
561 sec_bkt = &h->buckets[sec_bucket_idx];
562 rte_prefetch0(sec_bkt);
564 /* Get a new slot for storing the new key */
565 if (h->hw_trans_mem_support) {
566 lcore_id = rte_lcore_id();
567 cached_free_slots = &h->local_free_slots[lcore_id];
568 /* Try to get a free slot from the local cache */
569 if (cached_free_slots->len == 0) {
570 /* Need to get another burst of free slots from global ring */
571 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
572 cached_free_slots->objs, LCORE_CACHE_SIZE);
576 cached_free_slots->len += n_slots;
579 /* Get a free slot from the local cache */
580 cached_free_slots->len--;
581 slot_id = cached_free_slots->objs[cached_free_slots->len];
583 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
587 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
588 rte_prefetch0(new_k);
589 new_idx = (uint32_t)((uintptr_t) slot_id);
591 /* Check if key is already inserted in primary location */
592 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
593 if (prim_bkt->signatures[i].current == sig &&
594 prim_bkt->signatures[i].alt == alt_hash) {
595 k = (struct rte_hash_key *) ((char *)keys +
596 prim_bkt->key_idx[i] * h->key_entry_size);
597 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
598 /* Enqueue index of free slot back in the ring. */
599 enqueue_slot_back(h, cached_free_slots, slot_id);
603 * Return index where key is stored,
604 * substracting the first dummy index
606 return prim_bkt->key_idx[i] - 1;
611 /* Check if key is already inserted in secondary location */
612 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
613 if (sec_bkt->signatures[i].alt == sig &&
614 sec_bkt->signatures[i].current == alt_hash) {
615 k = (struct rte_hash_key *) ((char *)keys +
616 sec_bkt->key_idx[i] * h->key_entry_size);
617 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
618 /* Enqueue index of free slot back in the ring. */
619 enqueue_slot_back(h, cached_free_slots, slot_id);
623 * Return index where key is stored,
624 * substracting the first dummy index
626 return sec_bkt->key_idx[i] - 1;
632 rte_memcpy(new_k->key, key, h->key_len);
635 /* Insert new entry is there is room in the primary bucket */
636 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
637 /* Check if slot is available */
638 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
639 prim_bkt->signatures[i].current = sig;
640 prim_bkt->signatures[i].alt = alt_hash;
641 prim_bkt->key_idx[i] = new_idx;
646 /* Primary bucket is full, so we need to make space for new entry */
647 ret = make_space_bucket(h, prim_bkt);
649 * After recursive function.
650 * Insert the new entry in the position of the pushed entry
651 * if successful or return error and
652 * store the new slot back in the ring
655 prim_bkt->signatures[ret].current = sig;
656 prim_bkt->signatures[ret].alt = alt_hash;
657 prim_bkt->key_idx[ret] = new_idx;
661 /* Error in addition, store new slot back in the ring and return error */
662 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
668 rte_hash_add_key_with_hash(const struct rte_hash *h,
669 const void *key, hash_sig_t sig)
671 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
672 return __rte_hash_add_key_with_hash(h, key, sig, 0);
676 rte_hash_add_key(const struct rte_hash *h, const void *key)
678 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
679 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
683 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
684 const void *key, hash_sig_t sig, void *data)
688 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
689 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
697 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
701 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
703 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
709 static inline int32_t
710 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
711 hash_sig_t sig, void **data)
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) {
732 * Return index where key is stored,
733 * substracting the first dummy index
735 return bkt->key_idx[i] - 1;
740 /* Calculate secondary hash */
741 alt_hash = rte_hash_secondary_hash(sig);
742 bucket_idx = alt_hash & h->bucket_bitmask;
743 bkt = &h->buckets[bucket_idx];
745 /* Check if key is in secondary location */
746 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
747 if (bkt->signatures[i].current == alt_hash &&
748 bkt->signatures[i].alt == sig) {
749 k = (struct rte_hash_key *) ((char *)keys +
750 bkt->key_idx[i] * h->key_entry_size);
751 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
755 * Return index where key is stored,
756 * substracting the first dummy index
758 return bkt->key_idx[i] - 1;
767 rte_hash_lookup_with_hash(const struct rte_hash *h,
768 const void *key, hash_sig_t sig)
770 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
771 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
775 rte_hash_lookup(const struct rte_hash *h, const void *key)
777 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
778 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
782 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
783 const void *key, hash_sig_t sig, void **data)
785 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
786 return __rte_hash_lookup_with_hash(h, key, sig, data);
790 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
792 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
793 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
797 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
799 unsigned lcore_id, n_slots;
800 struct lcore_cache *cached_free_slots;
802 bkt->signatures[i].sig = NULL_SIGNATURE;
803 if (h->hw_trans_mem_support) {
804 lcore_id = rte_lcore_id();
805 cached_free_slots = &h->local_free_slots[lcore_id];
806 /* Cache full, need to free it. */
807 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
808 /* Need to enqueue the free slots in global ring. */
809 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
810 cached_free_slots->objs,
812 cached_free_slots->len -= n_slots;
814 /* Put index of new free slot in cache. */
815 cached_free_slots->objs[cached_free_slots->len] =
816 (void *)((uintptr_t)bkt->key_idx[i]);
817 cached_free_slots->len++;
819 rte_ring_sp_enqueue(h->free_slots,
820 (void *)((uintptr_t)bkt->key_idx[i]));
824 static inline int32_t
825 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
831 struct rte_hash_bucket *bkt;
832 struct rte_hash_key *k, *keys = h->key_store;
834 bucket_idx = sig & h->bucket_bitmask;
835 bkt = &h->buckets[bucket_idx];
837 /* Check if key is in primary location */
838 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
839 if (bkt->signatures[i].current == sig &&
840 bkt->signatures[i].sig != NULL_SIGNATURE) {
841 k = (struct rte_hash_key *) ((char *)keys +
842 bkt->key_idx[i] * h->key_entry_size);
843 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
844 remove_entry(h, bkt, i);
847 * Return index where key is stored,
848 * substracting the first dummy index
850 return bkt->key_idx[i] - 1;
855 /* Calculate secondary hash */
856 alt_hash = rte_hash_secondary_hash(sig);
857 bucket_idx = alt_hash & h->bucket_bitmask;
858 bkt = &h->buckets[bucket_idx];
860 /* Check if key is in secondary location */
861 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
862 if (bkt->signatures[i].current == alt_hash &&
863 bkt->signatures[i].sig != NULL_SIGNATURE) {
864 k = (struct rte_hash_key *) ((char *)keys +
865 bkt->key_idx[i] * h->key_entry_size);
866 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
867 remove_entry(h, bkt, i);
870 * Return index where key is stored,
871 * substracting the first dummy index
873 return bkt->key_idx[i] - 1;
882 rte_hash_del_key_with_hash(const struct rte_hash *h,
883 const void *key, hash_sig_t sig)
885 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
886 return __rte_hash_del_key_with_hash(h, key, sig);
890 rte_hash_del_key(const struct rte_hash *h, const void *key)
892 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
893 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
896 /* Lookup bulk stage 0: Prefetch input key */
898 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
899 const void * const *keys)
901 *idx = __builtin_ctzl(*lookup_mask);
902 if (*lookup_mask == 0)
905 rte_prefetch0(keys[*idx]);
906 *lookup_mask &= ~(1llu << *idx);
910 * Lookup bulk stage 1: Calculate primary/secondary hashes
911 * and prefetch primary/secondary buckets
914 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
915 const struct rte_hash_bucket **primary_bkt,
916 const struct rte_hash_bucket **secondary_bkt,
917 hash_sig_t *hash_vals, const void * const *keys,
918 const struct rte_hash *h)
920 *prim_hash = rte_hash_hash(h, keys[idx]);
921 hash_vals[idx] = *prim_hash;
922 *sec_hash = rte_hash_secondary_hash(*prim_hash);
924 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
925 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
927 rte_prefetch0(*primary_bkt);
928 rte_prefetch0(*secondary_bkt);
932 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
933 * and prefetch first key slot
936 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
937 const struct rte_hash_bucket *prim_bkt,
938 const struct rte_hash_bucket *sec_bkt,
939 const struct rte_hash_key **key_slot, int32_t *positions,
940 uint64_t *extra_hits_mask, const void *keys,
941 const struct rte_hash *h)
943 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
944 unsigned total_hash_matches;
946 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
947 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
948 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
949 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
950 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
953 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
955 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
957 total_hash_matches = (prim_hash_matches |
958 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
959 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
960 key_idx * h->key_entry_size);
962 rte_prefetch0(*key_slot);
964 * Return index where key is stored,
965 * substracting the first dummy index
967 positions[idx] = (key_idx - 1);
969 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
974 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
976 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
977 const int32_t *positions, void *data[], uint64_t *hits,
978 const struct rte_hash *h)
983 hit = !h->rte_hash_cmp_eq(key_slot->key, keys[idx], h->key_len);
985 data[idx] = key_slot->pdata;
987 key_idx = positions[idx] + 1;
989 * If key index is 0, force hit to be 0, in case key to be looked up
990 * is all zero (as in the dummy slot), which would result in a wrong hit
992 *hits |= (uint64_t)(hit && !!key_idx) << idx;
996 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
997 uint32_t num_keys, int32_t *positions,
998 uint64_t *hit_mask, void *data[])
1001 uint64_t extra_hits_mask = 0;
1002 uint64_t lookup_mask, miss_mask;
1004 const void *key_store = h->key_store;
1006 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1008 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1009 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1010 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1011 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1012 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1013 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1014 hash_sig_t primary_hash10, primary_hash11;
1015 hash_sig_t secondary_hash10, secondary_hash11;
1016 hash_sig_t primary_hash20, primary_hash21;
1017 hash_sig_t secondary_hash20, secondary_hash21;
1019 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1020 miss_mask = lookup_mask;
1022 lookup_stage0(&idx00, &lookup_mask, keys);
1023 lookup_stage0(&idx01, &lookup_mask, keys);
1025 idx10 = idx00, idx11 = idx01;
1027 lookup_stage0(&idx00, &lookup_mask, keys);
1028 lookup_stage0(&idx01, &lookup_mask, keys);
1029 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1030 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1031 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1032 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1034 primary_bkt20 = primary_bkt10;
1035 primary_bkt21 = primary_bkt11;
1036 secondary_bkt20 = secondary_bkt10;
1037 secondary_bkt21 = secondary_bkt11;
1038 primary_hash20 = primary_hash10;
1039 primary_hash21 = primary_hash11;
1040 secondary_hash20 = secondary_hash10;
1041 secondary_hash21 = secondary_hash11;
1042 idx20 = idx10, idx21 = idx11;
1043 idx10 = idx00, idx11 = idx01;
1045 lookup_stage0(&idx00, &lookup_mask, keys);
1046 lookup_stage0(&idx01, &lookup_mask, keys);
1047 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1048 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1049 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1050 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1051 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1052 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1054 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1055 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1058 while (lookup_mask) {
1059 k_slot30 = k_slot20, k_slot31 = k_slot21;
1060 idx30 = idx20, idx31 = idx21;
1061 primary_bkt20 = primary_bkt10;
1062 primary_bkt21 = primary_bkt11;
1063 secondary_bkt20 = secondary_bkt10;
1064 secondary_bkt21 = secondary_bkt11;
1065 primary_hash20 = primary_hash10;
1066 primary_hash21 = primary_hash11;
1067 secondary_hash20 = secondary_hash10;
1068 secondary_hash21 = secondary_hash11;
1069 idx20 = idx10, idx21 = idx11;
1070 idx10 = idx00, idx11 = idx01;
1072 lookup_stage0(&idx00, &lookup_mask, keys);
1073 lookup_stage0(&idx01, &lookup_mask, keys);
1074 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1075 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1076 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1077 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1078 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1079 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1080 &extra_hits_mask, key_store, h);
1081 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1082 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1083 &extra_hits_mask, key_store, h);
1084 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1085 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1088 k_slot30 = k_slot20, k_slot31 = k_slot21;
1089 idx30 = idx20, idx31 = idx21;
1090 primary_bkt20 = primary_bkt10;
1091 primary_bkt21 = primary_bkt11;
1092 secondary_bkt20 = secondary_bkt10;
1093 secondary_bkt21 = secondary_bkt11;
1094 primary_hash20 = primary_hash10;
1095 primary_hash21 = primary_hash11;
1096 secondary_hash20 = secondary_hash10;
1097 secondary_hash21 = secondary_hash11;
1098 idx20 = idx10, idx21 = idx11;
1099 idx10 = idx00, idx11 = idx01;
1101 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1102 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1103 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1104 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1105 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1106 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1108 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1109 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1111 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1112 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1114 k_slot30 = k_slot20, k_slot31 = k_slot21;
1115 idx30 = idx20, idx31 = idx21;
1116 primary_bkt20 = primary_bkt10;
1117 primary_bkt21 = primary_bkt11;
1118 secondary_bkt20 = secondary_bkt10;
1119 secondary_bkt21 = secondary_bkt11;
1120 primary_hash20 = primary_hash10;
1121 primary_hash21 = primary_hash11;
1122 secondary_hash20 = secondary_hash10;
1123 secondary_hash21 = secondary_hash11;
1124 idx20 = idx10, idx21 = idx11;
1126 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1127 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1129 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1130 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1132 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1133 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1135 k_slot30 = k_slot20, k_slot31 = k_slot21;
1136 idx30 = idx20, idx31 = idx21;
1138 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1139 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1141 /* ignore any items we have already found */
1142 extra_hits_mask &= ~hits;
1144 if (unlikely(extra_hits_mask)) {
1145 /* run a single search for each remaining item */
1147 idx = __builtin_ctzl(extra_hits_mask);
1149 ret = rte_hash_lookup_with_hash_data(h,
1150 keys[idx], hash_vals[idx], &data[idx]);
1152 hits |= 1ULL << idx;
1154 positions[idx] = rte_hash_lookup_with_hash(h,
1155 keys[idx], hash_vals[idx]);
1156 if (positions[idx] >= 0)
1157 hits |= 1llu << idx;
1159 extra_hits_mask &= ~(1llu << idx);
1160 } while (extra_hits_mask);
1164 if (unlikely(miss_mask)) {
1166 idx = __builtin_ctzl(miss_mask);
1167 positions[idx] = -ENOENT;
1168 miss_mask &= ~(1llu << idx);
1169 } while (miss_mask);
1172 if (hit_mask != NULL)
1177 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1178 uint32_t num_keys, int32_t *positions)
1180 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1181 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1182 (positions == NULL)), -EINVAL);
1184 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1189 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1190 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1192 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1193 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1194 (hit_mask == NULL)), -EINVAL);
1196 int32_t positions[num_keys];
1198 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1200 /* Return number of hits */
1201 return __builtin_popcountl(*hit_mask);
1205 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1207 uint32_t bucket_idx, idx, position;
1208 struct rte_hash_key *next_key;
1210 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1212 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1214 if (*next >= total_entries)
1217 /* Calculate bucket and index of current iterator */
1218 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1219 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1221 /* If current position is empty, go to the next one */
1222 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1225 if (*next == total_entries)
1227 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1228 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1231 /* Get position of entry in key table */
1232 position = h->buckets[bucket_idx].key_idx[idx];
1233 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1234 position * h->key_entry_size);
1235 /* Return key and data */
1236 *key = next_key->key;
1237 *data = next_key->pdata;
1239 /* Increment iterator */
1242 return position - 1;