1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016 Intel Corporation
6 * This file hold Cuckoo Hash private data structures to allows include from
7 * platform specific files like rte_cuckoo_hash_x86.h
10 #ifndef _RTE_CUCKOO_HASH_H_
11 #define _RTE_CUCKOO_HASH_H_
13 #if defined(RTE_ARCH_X86)
14 #include "rte_cmp_x86.h"
17 #if defined(RTE_ARCH_ARM64)
18 #include "rte_cmp_arm64.h"
21 /* Macro to enable/disable run-time checking of function parameters */
22 #if defined(RTE_LIBRTE_HASH_DEBUG)
23 #define RETURN_IF_TRUE(cond, retval) do { \
28 #define RETURN_IF_TRUE(cond, retval)
31 #include <rte_hash_crc.h>
32 #include <rte_jhash.h>
34 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
36 * All different options to select a key compare function,
37 * based on the key size and custom function.
39 enum cmp_jump_table_case {
54 * Table storing all different key compare functions
55 * (multi-process supported)
57 const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
71 * All different options to select a key compare function,
72 * based on the key size and custom function.
74 enum cmp_jump_table_case {
81 * Table storing all different key compare functions
82 * (multi-process supported)
84 const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
92 /** Number of items per bucket. */
93 #define RTE_HASH_BUCKET_ENTRIES 8
95 #if !RTE_IS_POWER_OF_2(RTE_HASH_BUCKET_ENTRIES)
96 #error RTE_HASH_BUCKET_ENTRIES must be a power of 2
99 #define NULL_SIGNATURE 0
103 #define KEY_ALIGNMENT 16
105 #define LCORE_CACHE_SIZE 64
107 #define RTE_HASH_BFS_QUEUE_MAX_LEN 1000
109 #define RTE_XABORT_CUCKOO_PATH_INVALIDED 0x4
111 #define RTE_HASH_TSX_MAX_RETRY 10
114 unsigned len; /**< Cache len */
115 void *objs[LCORE_CACHE_SIZE]; /**< Cache objects */
116 } __rte_cache_aligned;
118 /* Structure that stores key-value pair */
119 struct rte_hash_key {
124 /* Variable key size */
126 } __attribute__((aligned(KEY_ALIGNMENT)));
128 /* All different signature compare functions */
129 enum rte_hash_sig_compare_function {
130 RTE_HASH_COMPARE_SCALAR = 0,
131 RTE_HASH_COMPARE_SSE,
135 /** Bucket structure */
136 struct rte_hash_bucket {
137 uint16_t sig_current[RTE_HASH_BUCKET_ENTRIES];
139 uint32_t key_idx[RTE_HASH_BUCKET_ENTRIES];
141 uint8_t flag[RTE_HASH_BUCKET_ENTRIES];
144 } __rte_cache_aligned;
146 /** A hash table structure. */
148 char name[RTE_HASH_NAMESIZE]; /**< Name of the hash. */
149 uint32_t entries; /**< Total table entries. */
150 uint32_t num_buckets; /**< Number of buckets in table. */
152 struct rte_ring *free_slots;
153 /**< Ring that stores all indexes of the free slots in the key table */
155 struct lcore_cache *local_free_slots;
156 /**< Local cache per lcore, storing some indexes of the free slots */
158 /* Fields used in lookup */
160 uint32_t key_len __rte_cache_aligned;
161 /**< Length of hash key. */
162 uint8_t hw_trans_mem_support;
163 /**< If hardware transactional memory is used. */
164 uint8_t use_local_cache;
165 /**< If multi-writer support is enabled, use local cache
166 * to allocate key-store slots.
168 uint8_t readwrite_concur_support;
169 /**< If read-write concurrency support is enabled */
170 uint8_t ext_table_support; /**< Enable extendable bucket table */
171 uint8_t writer_takes_lock;
172 /**< Indicates if the writer threads need to take lock */
173 rte_hash_function hash_func; /**< Function used to calculate hash. */
174 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
175 rte_hash_cmp_eq_t rte_hash_custom_cmp_eq;
176 /**< Custom function used to compare keys. */
177 enum cmp_jump_table_case cmp_jump_table_idx;
178 /**< Indicates which compare function to use. */
179 enum rte_hash_sig_compare_function sig_cmp_fn;
180 /**< Indicates which signature compare function to use. */
181 uint32_t bucket_bitmask;
182 /**< Bitmask for getting bucket index from hash signature. */
183 uint32_t key_entry_size; /**< Size of each key entry. */
185 void *key_store; /**< Table storing all keys and data */
186 struct rte_hash_bucket *buckets;
187 /**< Table with buckets storing all the hash values and key indexes
190 rte_rwlock_t *readwrite_lock; /**< Read-write lock thread-safety. */
191 struct rte_hash_bucket *buckets_ext; /**< Extra buckets array */
192 struct rte_ring *free_ext_bkts; /**< Ring of indexes of free buckets */
193 } __rte_cache_aligned;
196 struct rte_hash_bucket *bkt; /* Current bucket on the bfs search */
197 uint32_t cur_bkt_idx;
199 struct queue_node *prev; /* Parent(bucket) in search path */
200 int prev_slot; /* Parent(slot) in search path */