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)
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) || defined(RTE_ARCH_ARM64)
291 /* Select function to compare keys */
292 switch (params->key_len) {
294 h->rte_hash_cmp_eq = rte_hash_k16_cmp_eq;
297 h->rte_hash_cmp_eq = rte_hash_k32_cmp_eq;
300 h->rte_hash_cmp_eq = rte_hash_k48_cmp_eq;
303 h->rte_hash_cmp_eq = rte_hash_k64_cmp_eq;
306 h->rte_hash_cmp_eq = rte_hash_k80_cmp_eq;
309 h->rte_hash_cmp_eq = rte_hash_k96_cmp_eq;
312 h->rte_hash_cmp_eq = rte_hash_k112_cmp_eq;
315 h->rte_hash_cmp_eq = rte_hash_k128_cmp_eq;
318 /* If key is not multiple of 16, use generic memcmp */
319 h->rte_hash_cmp_eq = memcmp;
322 h->rte_hash_cmp_eq = memcmp;
325 snprintf(ring_name, sizeof(ring_name), "HT_%s", params->name);
326 r = rte_ring_create(ring_name, rte_align32pow2(num_key_slots),
327 params->socket_id, 0);
329 RTE_LOG(ERR, HASH, "memory allocation failed\n");
333 if (hw_trans_mem_support) {
334 h->local_free_slots = rte_zmalloc_socket(NULL,
335 sizeof(struct lcore_cache) * RTE_MAX_LCORE,
336 RTE_CACHE_LINE_SIZE, params->socket_id);
339 /* Setup hash context */
340 snprintf(h->name, sizeof(h->name), "%s", params->name);
341 h->entries = params->entries;
342 h->key_len = params->key_len;
343 h->key_entry_size = key_entry_size;
344 h->hash_func_init_val = params->hash_func_init_val;
346 h->num_buckets = num_buckets;
347 h->bucket_bitmask = h->num_buckets - 1;
348 h->buckets = buckets;
349 h->hash_func = (params->hash_func == NULL) ?
350 DEFAULT_HASH_FUNC : params->hash_func;
353 h->hw_trans_mem_support = hw_trans_mem_support;
355 /* populate the free slots ring. Entry zero is reserved for key misses */
356 for (i = 1; i < params->entries + 1; i++)
357 rte_ring_sp_enqueue(r, (void *)((uintptr_t) i));
359 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
360 te->data = (void *) h;
361 TAILQ_INSERT_TAIL(hash_list, te, next);
362 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
374 rte_hash_free(struct rte_hash *h)
376 struct rte_tailq_entry *te;
377 struct rte_hash_list *hash_list;
382 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
384 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
386 /* find out tailq entry */
387 TAILQ_FOREACH(te, hash_list, next) {
388 if (te->data == (void *) h)
393 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
397 TAILQ_REMOVE(hash_list, te, next);
399 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
401 if (h->hw_trans_mem_support)
402 rte_free(h->local_free_slots);
404 rte_ring_free(h->free_slots);
405 rte_free(h->key_store);
406 rte_free(h->buckets);
412 rte_hash_hash(const struct rte_hash *h, const void *key)
414 /* calc hash result by key */
415 return h->hash_func(key, h->key_len, h->hash_func_init_val);
418 /* Calc the secondary hash value from the primary hash value of a given key */
419 static inline hash_sig_t
420 rte_hash_secondary_hash(const hash_sig_t primary_hash)
422 static const unsigned all_bits_shift = 12;
423 static const unsigned alt_bits_xor = 0x5bd1e995;
425 uint32_t tag = primary_hash >> all_bits_shift;
427 return primary_hash ^ ((tag + 1) * alt_bits_xor);
431 rte_hash_reset(struct rte_hash *h)
439 memset(h->buckets, 0, h->num_buckets * sizeof(struct rte_hash_bucket));
440 memset(h->key_store, 0, h->key_entry_size * (h->entries + 1));
442 /* clear the free ring */
443 while (rte_ring_dequeue(h->free_slots, &ptr) == 0)
446 /* Repopulate the free slots ring. Entry zero is reserved for key misses */
447 for (i = 1; i < h->entries + 1; i++)
448 rte_ring_sp_enqueue(h->free_slots, (void *)((uintptr_t) i));
450 if (h->hw_trans_mem_support) {
451 /* Reset local caches per lcore */
452 for (i = 0; i < RTE_MAX_LCORE; i++)
453 h->local_free_slots[i].len = 0;
457 /* Search for an entry that can be pushed to its alternative location */
459 make_space_bucket(const struct rte_hash *h, struct rte_hash_bucket *bkt)
463 uint32_t next_bucket_idx;
464 struct rte_hash_bucket *next_bkt[RTE_HASH_BUCKET_ENTRIES];
467 * Push existing item (search for bucket with space in
468 * alternative locations) to its alternative location
470 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
471 /* Search for space in alternative locations */
472 next_bucket_idx = bkt->signatures[i].alt & h->bucket_bitmask;
473 next_bkt[i] = &h->buckets[next_bucket_idx];
474 for (j = 0; j < RTE_HASH_BUCKET_ENTRIES; j++) {
475 if (next_bkt[i]->signatures[j].sig == NULL_SIGNATURE)
479 if (j != RTE_HASH_BUCKET_ENTRIES)
483 /* Alternative location has spare room (end of recursive function) */
484 if (i != RTE_HASH_BUCKET_ENTRIES) {
485 next_bkt[i]->signatures[j].alt = bkt->signatures[i].current;
486 next_bkt[i]->signatures[j].current = bkt->signatures[i].alt;
487 next_bkt[i]->key_idx[j] = bkt->key_idx[i];
491 /* Pick entry that has not been pushed yet */
492 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++)
493 if (bkt->flag[i] == 0)
496 /* All entries have been pushed, so entry cannot be added */
497 if (i == RTE_HASH_BUCKET_ENTRIES)
500 /* Set flag to indicate that this entry is going to be pushed */
502 /* Need room in alternative bucket to insert the pushed entry */
503 ret = make_space_bucket(h, next_bkt[i]);
505 * After recursive function.
506 * Clear flags and insert the pushed entry
507 * in its alternative location if successful,
512 next_bkt[i]->signatures[ret].alt = bkt->signatures[i].current;
513 next_bkt[i]->signatures[ret].current = bkt->signatures[i].alt;
514 next_bkt[i]->key_idx[ret] = bkt->key_idx[i];
522 * Function called to enqueue back an index in the cache/ring,
523 * as slot has not being used and it can be used in the
524 * next addition attempt.
527 enqueue_slot_back(const struct rte_hash *h,
528 struct lcore_cache *cached_free_slots,
531 if (h->hw_trans_mem_support) {
532 cached_free_slots->objs[cached_free_slots->len] = slot_id;
533 cached_free_slots->len++;
535 rte_ring_sp_enqueue(h->free_slots, slot_id);
538 static inline int32_t
539 __rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key,
540 hash_sig_t sig, void *data)
543 uint32_t prim_bucket_idx, sec_bucket_idx;
545 struct rte_hash_bucket *prim_bkt, *sec_bkt;
546 struct rte_hash_key *new_k, *k, *keys = h->key_store;
547 void *slot_id = NULL;
552 struct lcore_cache *cached_free_slots = NULL;
554 prim_bucket_idx = sig & h->bucket_bitmask;
555 prim_bkt = &h->buckets[prim_bucket_idx];
556 rte_prefetch0(prim_bkt);
558 alt_hash = rte_hash_secondary_hash(sig);
559 sec_bucket_idx = alt_hash & h->bucket_bitmask;
560 sec_bkt = &h->buckets[sec_bucket_idx];
561 rte_prefetch0(sec_bkt);
563 /* Get a new slot for storing the new key */
564 if (h->hw_trans_mem_support) {
565 lcore_id = rte_lcore_id();
566 cached_free_slots = &h->local_free_slots[lcore_id];
567 /* Try to get a free slot from the local cache */
568 if (cached_free_slots->len == 0) {
569 /* Need to get another burst of free slots from global ring */
570 n_slots = rte_ring_mc_dequeue_burst(h->free_slots,
571 cached_free_slots->objs, LCORE_CACHE_SIZE);
575 cached_free_slots->len += n_slots;
578 /* Get a free slot from the local cache */
579 cached_free_slots->len--;
580 slot_id = cached_free_slots->objs[cached_free_slots->len];
582 if (rte_ring_sc_dequeue(h->free_slots, &slot_id) != 0)
586 new_k = RTE_PTR_ADD(keys, (uintptr_t)slot_id * h->key_entry_size);
587 rte_prefetch0(new_k);
588 new_idx = (uint32_t)((uintptr_t) slot_id);
590 /* Check if key is already inserted in primary location */
591 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
592 if (prim_bkt->signatures[i].current == sig &&
593 prim_bkt->signatures[i].alt == alt_hash) {
594 k = (struct rte_hash_key *) ((char *)keys +
595 prim_bkt->key_idx[i] * h->key_entry_size);
596 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
597 /* Enqueue index of free slot back in the ring. */
598 enqueue_slot_back(h, cached_free_slots, slot_id);
602 * Return index where key is stored,
603 * substracting the first dummy index
605 return prim_bkt->key_idx[i] - 1;
610 /* Check if key is already inserted in secondary location */
611 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
612 if (sec_bkt->signatures[i].alt == sig &&
613 sec_bkt->signatures[i].current == alt_hash) {
614 k = (struct rte_hash_key *) ((char *)keys +
615 sec_bkt->key_idx[i] * h->key_entry_size);
616 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
617 /* Enqueue index of free slot back in the ring. */
618 enqueue_slot_back(h, cached_free_slots, slot_id);
622 * Return index where key is stored,
623 * substracting the first dummy index
625 return sec_bkt->key_idx[i] - 1;
631 rte_memcpy(new_k->key, key, h->key_len);
634 /* Insert new entry is there is room in the primary bucket */
635 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
636 /* Check if slot is available */
637 if (likely(prim_bkt->signatures[i].sig == NULL_SIGNATURE)) {
638 prim_bkt->signatures[i].current = sig;
639 prim_bkt->signatures[i].alt = alt_hash;
640 prim_bkt->key_idx[i] = new_idx;
645 /* Primary bucket is full, so we need to make space for new entry */
646 ret = make_space_bucket(h, prim_bkt);
648 * After recursive function.
649 * Insert the new entry in the position of the pushed entry
650 * if successful or return error and
651 * store the new slot back in the ring
654 prim_bkt->signatures[ret].current = sig;
655 prim_bkt->signatures[ret].alt = alt_hash;
656 prim_bkt->key_idx[ret] = new_idx;
660 /* Error in addition, store new slot back in the ring and return error */
661 enqueue_slot_back(h, cached_free_slots, (void *)((uintptr_t) new_idx));
667 rte_hash_add_key_with_hash(const struct rte_hash *h,
668 const void *key, hash_sig_t sig)
670 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
671 return __rte_hash_add_key_with_hash(h, key, sig, 0);
675 rte_hash_add_key(const struct rte_hash *h, const void *key)
677 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
678 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), 0);
682 rte_hash_add_key_with_hash_data(const struct rte_hash *h,
683 const void *key, hash_sig_t sig, void *data)
687 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
688 ret = __rte_hash_add_key_with_hash(h, key, sig, data);
696 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data)
700 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
702 ret = __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key), data);
708 static inline int32_t
709 __rte_hash_lookup_with_hash(const struct rte_hash *h, const void *key,
710 hash_sig_t sig, void **data)
715 struct rte_hash_bucket *bkt;
716 struct rte_hash_key *k, *keys = h->key_store;
718 bucket_idx = sig & h->bucket_bitmask;
719 bkt = &h->buckets[bucket_idx];
721 /* Check if key is in primary location */
722 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
723 if (bkt->signatures[i].current == sig &&
724 bkt->signatures[i].sig != NULL_SIGNATURE) {
725 k = (struct rte_hash_key *) ((char *)keys +
726 bkt->key_idx[i] * h->key_entry_size);
727 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
731 * Return index where key is stored,
732 * substracting the first dummy index
734 return bkt->key_idx[i] - 1;
739 /* Calculate secondary hash */
740 alt_hash = rte_hash_secondary_hash(sig);
741 bucket_idx = alt_hash & h->bucket_bitmask;
742 bkt = &h->buckets[bucket_idx];
744 /* Check if key is in secondary location */
745 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
746 if (bkt->signatures[i].current == alt_hash &&
747 bkt->signatures[i].alt == sig) {
748 k = (struct rte_hash_key *) ((char *)keys +
749 bkt->key_idx[i] * h->key_entry_size);
750 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
754 * Return index where key is stored,
755 * substracting the first dummy index
757 return bkt->key_idx[i] - 1;
766 rte_hash_lookup_with_hash(const struct rte_hash *h,
767 const void *key, hash_sig_t sig)
769 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
770 return __rte_hash_lookup_with_hash(h, key, sig, NULL);
774 rte_hash_lookup(const struct rte_hash *h, const void *key)
776 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
777 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), NULL);
781 rte_hash_lookup_with_hash_data(const struct rte_hash *h,
782 const void *key, hash_sig_t sig, void **data)
784 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
785 return __rte_hash_lookup_with_hash(h, key, sig, data);
789 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data)
791 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
792 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key), data);
796 remove_entry(const struct rte_hash *h, struct rte_hash_bucket *bkt, unsigned i)
798 unsigned lcore_id, n_slots;
799 struct lcore_cache *cached_free_slots;
801 bkt->signatures[i].sig = NULL_SIGNATURE;
802 if (h->hw_trans_mem_support) {
803 lcore_id = rte_lcore_id();
804 cached_free_slots = &h->local_free_slots[lcore_id];
805 /* Cache full, need to free it. */
806 if (cached_free_slots->len == LCORE_CACHE_SIZE) {
807 /* Need to enqueue the free slots in global ring. */
808 n_slots = rte_ring_mp_enqueue_burst(h->free_slots,
809 cached_free_slots->objs,
811 cached_free_slots->len -= n_slots;
813 /* Put index of new free slot in cache. */
814 cached_free_slots->objs[cached_free_slots->len] =
815 (void *)((uintptr_t)bkt->key_idx[i]);
816 cached_free_slots->len++;
818 rte_ring_sp_enqueue(h->free_slots,
819 (void *)((uintptr_t)bkt->key_idx[i]));
823 static inline int32_t
824 __rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key,
830 struct rte_hash_bucket *bkt;
831 struct rte_hash_key *k, *keys = h->key_store;
833 bucket_idx = sig & h->bucket_bitmask;
834 bkt = &h->buckets[bucket_idx];
836 /* Check if key is in primary location */
837 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
838 if (bkt->signatures[i].current == sig &&
839 bkt->signatures[i].sig != NULL_SIGNATURE) {
840 k = (struct rte_hash_key *) ((char *)keys +
841 bkt->key_idx[i] * h->key_entry_size);
842 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
843 remove_entry(h, bkt, i);
846 * Return index where key is stored,
847 * substracting the first dummy index
849 return bkt->key_idx[i] - 1;
854 /* Calculate secondary hash */
855 alt_hash = rte_hash_secondary_hash(sig);
856 bucket_idx = alt_hash & h->bucket_bitmask;
857 bkt = &h->buckets[bucket_idx];
859 /* Check if key is in secondary location */
860 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
861 if (bkt->signatures[i].current == alt_hash &&
862 bkt->signatures[i].sig != NULL_SIGNATURE) {
863 k = (struct rte_hash_key *) ((char *)keys +
864 bkt->key_idx[i] * h->key_entry_size);
865 if (h->rte_hash_cmp_eq(key, k->key, h->key_len) == 0) {
866 remove_entry(h, bkt, i);
869 * Return index where key is stored,
870 * substracting the first dummy index
872 return bkt->key_idx[i] - 1;
881 rte_hash_del_key_with_hash(const struct rte_hash *h,
882 const void *key, hash_sig_t sig)
884 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
885 return __rte_hash_del_key_with_hash(h, key, sig);
889 rte_hash_del_key(const struct rte_hash *h, const void *key)
891 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
892 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
895 /* Lookup bulk stage 0: Prefetch input key */
897 lookup_stage0(unsigned *idx, uint64_t *lookup_mask,
898 const void * const *keys)
900 *idx = __builtin_ctzl(*lookup_mask);
901 if (*lookup_mask == 0)
904 rte_prefetch0(keys[*idx]);
905 *lookup_mask &= ~(1llu << *idx);
909 * Lookup bulk stage 1: Calculate primary/secondary hashes
910 * and prefetch primary/secondary buckets
913 lookup_stage1(unsigned idx, hash_sig_t *prim_hash, hash_sig_t *sec_hash,
914 const struct rte_hash_bucket **primary_bkt,
915 const struct rte_hash_bucket **secondary_bkt,
916 hash_sig_t *hash_vals, const void * const *keys,
917 const struct rte_hash *h)
919 *prim_hash = rte_hash_hash(h, keys[idx]);
920 hash_vals[idx] = *prim_hash;
921 *sec_hash = rte_hash_secondary_hash(*prim_hash);
923 *primary_bkt = &h->buckets[*prim_hash & h->bucket_bitmask];
924 *secondary_bkt = &h->buckets[*sec_hash & h->bucket_bitmask];
926 rte_prefetch0(*primary_bkt);
927 rte_prefetch0(*secondary_bkt);
931 * Lookup bulk stage 2: Search for match hashes in primary/secondary locations
932 * and prefetch first key slot
935 lookup_stage2(unsigned idx, hash_sig_t prim_hash, hash_sig_t sec_hash,
936 const struct rte_hash_bucket *prim_bkt,
937 const struct rte_hash_bucket *sec_bkt,
938 const struct rte_hash_key **key_slot, int32_t *positions,
939 uint64_t *extra_hits_mask, const void *keys,
940 const struct rte_hash *h)
942 unsigned prim_hash_matches, sec_hash_matches, key_idx, i;
943 unsigned total_hash_matches;
945 prim_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
946 sec_hash_matches = 1 << RTE_HASH_BUCKET_ENTRIES;
947 for (i = 0; i < RTE_HASH_BUCKET_ENTRIES; i++) {
948 prim_hash_matches |= ((prim_hash == prim_bkt->signatures[i].current) << i);
949 sec_hash_matches |= ((sec_hash == sec_bkt->signatures[i].current) << i);
952 key_idx = prim_bkt->key_idx[__builtin_ctzl(prim_hash_matches)];
954 key_idx = sec_bkt->key_idx[__builtin_ctzl(sec_hash_matches)];
956 total_hash_matches = (prim_hash_matches |
957 (sec_hash_matches << (RTE_HASH_BUCKET_ENTRIES + 1)));
958 *key_slot = (const struct rte_hash_key *) ((const char *)keys +
959 key_idx * h->key_entry_size);
961 rte_prefetch0(*key_slot);
963 * Return index where key is stored,
964 * substracting the first dummy index
966 positions[idx] = (key_idx - 1);
968 *extra_hits_mask |= (uint64_t)(__builtin_popcount(total_hash_matches) > 3) << idx;
973 /* Lookup bulk stage 3: Check if key matches, update hit mask and return data */
975 lookup_stage3(unsigned idx, const struct rte_hash_key *key_slot, const void * const *keys,
976 const int32_t *positions, void *data[], uint64_t *hits,
977 const struct rte_hash *h)
982 hit = !h->rte_hash_cmp_eq(key_slot->key, keys[idx], h->key_len);
984 data[idx] = key_slot->pdata;
986 key_idx = positions[idx] + 1;
988 * If key index is 0, force hit to be 0, in case key to be looked up
989 * is all zero (as in the dummy slot), which would result in a wrong hit
991 *hits |= (uint64_t)(hit && !!key_idx) << idx;
995 __rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
996 uint32_t num_keys, int32_t *positions,
997 uint64_t *hit_mask, void *data[])
1000 uint64_t extra_hits_mask = 0;
1001 uint64_t lookup_mask, miss_mask;
1003 const void *key_store = h->key_store;
1005 hash_sig_t hash_vals[RTE_HASH_LOOKUP_BULK_MAX];
1007 unsigned idx00, idx01, idx10, idx11, idx20, idx21, idx30, idx31;
1008 const struct rte_hash_bucket *primary_bkt10, *primary_bkt11;
1009 const struct rte_hash_bucket *secondary_bkt10, *secondary_bkt11;
1010 const struct rte_hash_bucket *primary_bkt20, *primary_bkt21;
1011 const struct rte_hash_bucket *secondary_bkt20, *secondary_bkt21;
1012 const struct rte_hash_key *k_slot20, *k_slot21, *k_slot30, *k_slot31;
1013 hash_sig_t primary_hash10, primary_hash11;
1014 hash_sig_t secondary_hash10, secondary_hash11;
1015 hash_sig_t primary_hash20, primary_hash21;
1016 hash_sig_t secondary_hash20, secondary_hash21;
1018 lookup_mask = (uint64_t) -1 >> (64 - num_keys);
1019 miss_mask = lookup_mask;
1021 lookup_stage0(&idx00, &lookup_mask, keys);
1022 lookup_stage0(&idx01, &lookup_mask, keys);
1024 idx10 = idx00, idx11 = idx01;
1026 lookup_stage0(&idx00, &lookup_mask, keys);
1027 lookup_stage0(&idx01, &lookup_mask, keys);
1028 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1029 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1030 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1031 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1033 primary_bkt20 = primary_bkt10;
1034 primary_bkt21 = primary_bkt11;
1035 secondary_bkt20 = secondary_bkt10;
1036 secondary_bkt21 = secondary_bkt11;
1037 primary_hash20 = primary_hash10;
1038 primary_hash21 = primary_hash11;
1039 secondary_hash20 = secondary_hash10;
1040 secondary_hash21 = secondary_hash11;
1041 idx20 = idx10, idx21 = idx11;
1042 idx10 = idx00, idx11 = idx01;
1044 lookup_stage0(&idx00, &lookup_mask, keys);
1045 lookup_stage0(&idx01, &lookup_mask, keys);
1046 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1047 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1048 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1049 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1050 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1051 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1053 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1054 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1057 while (lookup_mask) {
1058 k_slot30 = k_slot20, k_slot31 = k_slot21;
1059 idx30 = idx20, idx31 = idx21;
1060 primary_bkt20 = primary_bkt10;
1061 primary_bkt21 = primary_bkt11;
1062 secondary_bkt20 = secondary_bkt10;
1063 secondary_bkt21 = secondary_bkt11;
1064 primary_hash20 = primary_hash10;
1065 primary_hash21 = primary_hash11;
1066 secondary_hash20 = secondary_hash10;
1067 secondary_hash21 = secondary_hash11;
1068 idx20 = idx10, idx21 = idx11;
1069 idx10 = idx00, idx11 = idx01;
1071 lookup_stage0(&idx00, &lookup_mask, keys);
1072 lookup_stage0(&idx01, &lookup_mask, keys);
1073 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1074 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1075 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1076 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1077 lookup_stage2(idx20, primary_hash20, secondary_hash20,
1078 primary_bkt20, secondary_bkt20, &k_slot20, positions,
1079 &extra_hits_mask, key_store, h);
1080 lookup_stage2(idx21, primary_hash21, secondary_hash21,
1081 primary_bkt21, secondary_bkt21, &k_slot21, positions,
1082 &extra_hits_mask, key_store, h);
1083 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1084 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1087 k_slot30 = k_slot20, k_slot31 = k_slot21;
1088 idx30 = idx20, idx31 = idx21;
1089 primary_bkt20 = primary_bkt10;
1090 primary_bkt21 = primary_bkt11;
1091 secondary_bkt20 = secondary_bkt10;
1092 secondary_bkt21 = secondary_bkt11;
1093 primary_hash20 = primary_hash10;
1094 primary_hash21 = primary_hash11;
1095 secondary_hash20 = secondary_hash10;
1096 secondary_hash21 = secondary_hash11;
1097 idx20 = idx10, idx21 = idx11;
1098 idx10 = idx00, idx11 = idx01;
1100 lookup_stage1(idx10, &primary_hash10, &secondary_hash10,
1101 &primary_bkt10, &secondary_bkt10, hash_vals, keys, h);
1102 lookup_stage1(idx11, &primary_hash11, &secondary_hash11,
1103 &primary_bkt11, &secondary_bkt11, hash_vals, keys, h);
1104 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1105 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1107 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1108 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1110 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1111 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1113 k_slot30 = k_slot20, k_slot31 = k_slot21;
1114 idx30 = idx20, idx31 = idx21;
1115 primary_bkt20 = primary_bkt10;
1116 primary_bkt21 = primary_bkt11;
1117 secondary_bkt20 = secondary_bkt10;
1118 secondary_bkt21 = secondary_bkt11;
1119 primary_hash20 = primary_hash10;
1120 primary_hash21 = primary_hash11;
1121 secondary_hash20 = secondary_hash10;
1122 secondary_hash21 = secondary_hash11;
1123 idx20 = idx10, idx21 = idx11;
1125 lookup_stage2(idx20, primary_hash20, secondary_hash20, primary_bkt20,
1126 secondary_bkt20, &k_slot20, positions, &extra_hits_mask,
1128 lookup_stage2(idx21, primary_hash21, secondary_hash21, primary_bkt21,
1129 secondary_bkt21, &k_slot21, positions, &extra_hits_mask,
1131 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1132 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1134 k_slot30 = k_slot20, k_slot31 = k_slot21;
1135 idx30 = idx20, idx31 = idx21;
1137 lookup_stage3(idx30, k_slot30, keys, positions, data, &hits, h);
1138 lookup_stage3(idx31, k_slot31, keys, positions, data, &hits, h);
1140 /* ignore any items we have already found */
1141 extra_hits_mask &= ~hits;
1143 if (unlikely(extra_hits_mask)) {
1144 /* run a single search for each remaining item */
1146 idx = __builtin_ctzl(extra_hits_mask);
1148 ret = rte_hash_lookup_with_hash_data(h,
1149 keys[idx], hash_vals[idx], &data[idx]);
1151 hits |= 1ULL << idx;
1153 positions[idx] = rte_hash_lookup_with_hash(h,
1154 keys[idx], hash_vals[idx]);
1155 if (positions[idx] >= 0)
1156 hits |= 1llu << idx;
1158 extra_hits_mask &= ~(1llu << idx);
1159 } while (extra_hits_mask);
1163 if (unlikely(miss_mask)) {
1165 idx = __builtin_ctzl(miss_mask);
1166 positions[idx] = -ENOENT;
1167 miss_mask &= ~(1llu << idx);
1168 } while (miss_mask);
1171 if (hit_mask != NULL)
1176 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
1177 uint32_t num_keys, int32_t *positions)
1179 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1180 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1181 (positions == NULL)), -EINVAL);
1183 __rte_hash_lookup_bulk(h, keys, num_keys, positions, NULL, NULL);
1188 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
1189 uint32_t num_keys, uint64_t *hit_mask, void *data[])
1191 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
1192 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
1193 (hit_mask == NULL)), -EINVAL);
1195 int32_t positions[num_keys];
1197 __rte_hash_lookup_bulk(h, keys, num_keys, positions, hit_mask, data);
1199 /* Return number of hits */
1200 return __builtin_popcountl(*hit_mask);
1204 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next)
1206 uint32_t bucket_idx, idx, position;
1207 struct rte_hash_key *next_key;
1209 RETURN_IF_TRUE(((h == NULL) || (next == NULL)), -EINVAL);
1211 const uint32_t total_entries = h->num_buckets * RTE_HASH_BUCKET_ENTRIES;
1213 if (*next >= total_entries)
1216 /* Calculate bucket and index of current iterator */
1217 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1218 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1220 /* If current position is empty, go to the next one */
1221 while (h->buckets[bucket_idx].signatures[idx].sig == NULL_SIGNATURE) {
1224 if (*next == total_entries)
1226 bucket_idx = *next / RTE_HASH_BUCKET_ENTRIES;
1227 idx = *next % RTE_HASH_BUCKET_ENTRIES;
1230 /* Get position of entry in key table */
1231 position = h->buckets[bucket_idx].key_idx[idx];
1232 next_key = (struct rte_hash_key *) ((char *)h->key_store +
1233 position * h->key_entry_size);
1234 /* Return key and data */
1235 *key = next_key->key;
1236 *data = next_key->pdata;
1238 /* Increment iterator */
1241 return position - 1;