hash: support no free on delete
[dpdk.git] / lib / librte_hash / rte_cuckoo_hash.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 /* rte_cuckoo_hash.h
6  * This file hold Cuckoo Hash private data structures to allows include from
7  * platform specific files like rte_cuckoo_hash_x86.h
8  */
9
10 #ifndef _RTE_CUCKOO_HASH_H_
11 #define _RTE_CUCKOO_HASH_H_
12
13 #if defined(RTE_ARCH_X86)
14 #include "rte_cmp_x86.h"
15 #endif
16
17 #if defined(RTE_ARCH_ARM64)
18 #include "rte_cmp_arm64.h"
19 #endif
20
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 { \
24         if (cond) \
25                 return retval; \
26 } while (0)
27 #else
28 #define RETURN_IF_TRUE(cond, retval)
29 #endif
30
31 #include <rte_hash_crc.h>
32 #include <rte_jhash.h>
33
34 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
35 /*
36  * All different options to select a key compare function,
37  * based on the key size and custom function.
38  */
39 enum cmp_jump_table_case {
40         KEY_CUSTOM = 0,
41         KEY_16_BYTES,
42         KEY_32_BYTES,
43         KEY_48_BYTES,
44         KEY_64_BYTES,
45         KEY_80_BYTES,
46         KEY_96_BYTES,
47         KEY_112_BYTES,
48         KEY_128_BYTES,
49         KEY_OTHER_BYTES,
50         NUM_KEY_CMP_CASES,
51 };
52
53 /*
54  * Table storing all different key compare functions
55  * (multi-process supported)
56  */
57 const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
58         NULL,
59         rte_hash_k16_cmp_eq,
60         rte_hash_k32_cmp_eq,
61         rte_hash_k48_cmp_eq,
62         rte_hash_k64_cmp_eq,
63         rte_hash_k80_cmp_eq,
64         rte_hash_k96_cmp_eq,
65         rte_hash_k112_cmp_eq,
66         rte_hash_k128_cmp_eq,
67         memcmp
68 };
69 #else
70 /*
71  * All different options to select a key compare function,
72  * based on the key size and custom function.
73  */
74 enum cmp_jump_table_case {
75         KEY_CUSTOM = 0,
76         KEY_OTHER_BYTES,
77         NUM_KEY_CMP_CASES,
78 };
79
80 /*
81  * Table storing all different key compare functions
82  * (multi-process supported)
83  */
84 const rte_hash_cmp_eq_t cmp_jump_table[NUM_KEY_CMP_CASES] = {
85         NULL,
86         memcmp
87 };
88
89 #endif
90
91
92 /** Number of items per bucket. */
93 #define RTE_HASH_BUCKET_ENTRIES         8
94
95 #if !RTE_IS_POWER_OF_2(RTE_HASH_BUCKET_ENTRIES)
96 #error RTE_HASH_BUCKET_ENTRIES must be a power of 2
97 #endif
98
99 #define NULL_SIGNATURE                  0
100
101 #define EMPTY_SLOT                      0
102
103 #define KEY_ALIGNMENT                   16
104
105 #define LCORE_CACHE_SIZE                64
106
107 #define RTE_HASH_BFS_QUEUE_MAX_LEN       1000
108
109 #define RTE_XABORT_CUCKOO_PATH_INVALIDED 0x4
110
111 #define RTE_HASH_TSX_MAX_RETRY  10
112
113 struct lcore_cache {
114         unsigned len; /**< Cache len */
115         void *objs[LCORE_CACHE_SIZE]; /**< Cache objects */
116 } __rte_cache_aligned;
117
118 /* Structure that stores key-value pair */
119 struct rte_hash_key {
120         union {
121                 uintptr_t idata;
122                 void *pdata;
123         };
124         /* Variable key size */
125         char key[0];
126 } __attribute__((aligned(KEY_ALIGNMENT)));
127
128 /* All different signature compare functions */
129 enum rte_hash_sig_compare_function {
130         RTE_HASH_COMPARE_SCALAR = 0,
131         RTE_HASH_COMPARE_SSE,
132         RTE_HASH_COMPARE_NUM
133 };
134
135 /** Bucket structure */
136 struct rte_hash_bucket {
137         uint16_t sig_current[RTE_HASH_BUCKET_ENTRIES];
138
139         uint32_t key_idx[RTE_HASH_BUCKET_ENTRIES];
140
141         uint8_t flag[RTE_HASH_BUCKET_ENTRIES];
142
143         void *next;
144 } __rte_cache_aligned;
145
146 /** A hash table structure. */
147 struct rte_hash {
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. */
151
152         struct rte_ring *free_slots;
153         /**< Ring that stores all indexes of the free slots in the key table */
154
155         struct lcore_cache *local_free_slots;
156         /**< Local cache per lcore, storing some indexes of the free slots */
157
158         /* Fields used in lookup */
159
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.
167          */
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 no_free_on_del;
172         /**< If key index should be freed on calling rte_hash_del_xxx APIs.
173          * If this is set, rte_hash_free_key_with_position must be called to
174          * free the key index associated with the deleted entry.
175          * This flag is enabled by default.
176          */
177         uint8_t writer_takes_lock;
178         /**< Indicates if the writer threads need to take lock */
179         rte_hash_function hash_func;    /**< Function used to calculate hash. */
180         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
181         rte_hash_cmp_eq_t rte_hash_custom_cmp_eq;
182         /**< Custom function used to compare keys. */
183         enum cmp_jump_table_case cmp_jump_table_idx;
184         /**< Indicates which compare function to use. */
185         enum rte_hash_sig_compare_function sig_cmp_fn;
186         /**< Indicates which signature compare function to use. */
187         uint32_t bucket_bitmask;
188         /**< Bitmask for getting bucket index from hash signature. */
189         uint32_t key_entry_size;         /**< Size of each key entry. */
190
191         void *key_store;                /**< Table storing all keys and data */
192         struct rte_hash_bucket *buckets;
193         /**< Table with buckets storing all the hash values and key indexes
194          * to the key table.
195          */
196         rte_rwlock_t *readwrite_lock; /**< Read-write lock thread-safety. */
197         struct rte_hash_bucket *buckets_ext; /**< Extra buckets array */
198         struct rte_ring *free_ext_bkts; /**< Ring of indexes of free buckets */
199 } __rte_cache_aligned;
200
201 struct queue_node {
202         struct rte_hash_bucket *bkt; /* Current bucket on the bfs search */
203         uint32_t cur_bkt_idx;
204
205         struct queue_node *prev;     /* Parent(bucket) in search path */
206         int prev_slot;               /* Parent(slot) in search path */
207 };
208
209 #endif