table: rework variable size key lru hash table
[dpdk.git] / lib / librte_table / rte_table_hash.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
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
16  *       distribution.
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.
20  *
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.
32  */
33
34 #ifndef __INCLUDE_RTE_TABLE_HASH_H__
35 #define __INCLUDE_RTE_TABLE_HASH_H__
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 /**
42  * @file
43  * RTE Table Hash
44  *
45  * These tables use the exact match criterion to uniquely associate data to
46  * lookup keys.
47  *
48  * Use-cases: Flow classification table, Address Resolution Protocol (ARP) table
49  *
50  * Hash table types:
51  * 1. Entry add strategy on bucket full:
52  *     a. Least Recently Used (LRU): One of the existing keys in the bucket is
53  *        deleted and the new key is added in its place. The number of keys in
54  *        each bucket never grows bigger than 4. The logic to pick the key to
55  *        be dropped from the bucket is LRU. The hash table lookup operation
56  *        maintains the order in which the keys in the same bucket are hit, so
57  *        every time a key is hit, it becomes the new Most Recently Used (MRU)
58  *        key, i.e. the most unlikely candidate for drop. When a key is added
59  *        to the bucket, it also becomes the new MRU key. When a key needs to
60  *        be picked and dropped, the most likely candidate for drop, i.e. the
61  *        current LRU key, is always picked. The LRU logic requires maintaining
62  *        specific data structures per each bucket.
63  *     b. Extendible bucket (ext): The bucket is extended with space for 4 more
64  *        keys. This is done by allocating additional memory at table init time,
65  *        which is used to create a pool of free keys (the size of this pool is
66  *        configurable and always a multiple of 4). On key add operation, the
67  *        allocation of a group of 4 keys only happens successfully within the
68  *        limit of free keys, otherwise the key add operation fails. On key
69  *        delete operation, a group of 4 keys is freed back to the pool of free
70  *        keys when the key to be deleted is the only key that was used within
71  *        its group of 4 keys at that time. On key lookup operation, if the
72  *        current bucket is in extended state and a match is not found in the
73  *        first group of 4 keys, the search continues beyond the first group of
74  *        4 keys, potentially until all keys in this bucket are examined. The
75  *        extendible bucket logic requires maintaining specific data structures
76  *        per table and per each bucket.
77  * 2. Key signature computation:
78  *     a. Pre-computed key signature: The key lookup operation is split between
79  *        two CPU cores. The first CPU core (typically the CPU core performing
80  *        packet RX) extracts the key from the input packet, computes the key
81  *        signature and saves both the key and the key signature in the packet
82  *        buffer as packet meta-data. The second CPU core reads both the key and
83  *        the key signature from the packet meta-data and performs the bucket
84  *        search step of the key lookup operation.
85  *     b. Key signature computed on lookup (do-sig): The same CPU core reads
86  *        the key from the packet meta-data, uses it to compute the key
87  *        signature and also performs the bucket search step of the key lookup
88  *        operation.
89  * 3. Key size:
90  *     a. Configurable key size
91  *     b. Single key size (8-byte, 16-byte or 32-byte key size)
92  *
93  ***/
94 #include <stdint.h>
95
96 #include "rte_table.h"
97
98 /** Hash function */
99 typedef uint64_t (*rte_table_hash_op_hash)(
100         void *key,
101         void *key_mask,
102         uint32_t key_size,
103         uint64_t seed);
104
105 /** Hash table parameters */
106 struct rte_table_hash_params {
107         /** Name */
108         const char *name;
109
110         /** Key size (number of bytes) */
111         uint32_t key_size;
112
113         /** Byte offset within packet meta-data where the key is located */
114         uint32_t key_offset;
115
116         /** Key mask */
117         uint8_t *key_mask;
118
119         /** Number of keys */
120         uint32_t n_keys;
121
122         /** Number of buckets */
123         uint32_t n_buckets;
124
125         /** Hash function */
126         rte_table_hash_op_hash f_hash;
127
128         /** Seed value for the hash function */
129         uint64_t seed;
130 };
131
132 /** Hash function */
133 typedef uint64_t (*rte_table_hash_op_hash_nomask)(
134         void *key,
135         uint32_t key_size,
136         uint64_t seed);
137
138 extern struct rte_table_ops rte_table_hash_ext_ops;
139
140 extern struct rte_table_ops rte_table_hash_lru_ops;
141
142 /**
143  * 8-byte key hash tables
144  *
145  */
146 /** LRU hash table parameters */
147 struct rte_table_hash_key8_lru_params {
148         /** Maximum number of entries (and keys) in the table */
149         uint32_t n_entries;
150
151         /** Hash function */
152         rte_table_hash_op_hash_nomask f_hash;
153
154         /** Seed for the hash function */
155         uint64_t seed;
156
157         /** Byte offset within packet meta-data where the 4-byte key signature
158         is located. Valid for pre-computed key signature tables, ignored for
159         do-sig tables. */
160         uint32_t signature_offset;
161
162         /** Byte offset within packet meta-data where the key is located */
163         uint32_t key_offset;
164
165         /** Bit-mask to be AND-ed to the key on lookup */
166         uint8_t *key_mask;
167 };
168
169 extern struct rte_table_ops rte_table_hash_key8_lru_ops;
170
171 /** Extendible bucket hash table parameters */
172 struct rte_table_hash_key8_ext_params {
173         /** Maximum number of entries (and keys) in the table */
174         uint32_t n_entries;
175
176         /** Number of entries (and keys) for hash table bucket extensions. Each
177                 bucket is extended in increments of 4 keys. */
178         uint32_t n_entries_ext;
179
180         /** Hash function */
181         rte_table_hash_op_hash_nomask f_hash;
182
183         /** Seed for the hash function */
184         uint64_t seed;
185
186         /** Byte offset within packet meta-data where the 4-byte key signature
187         is located. Valid for pre-computed key signature tables, ignored for
188         do-sig tables. */
189         uint32_t signature_offset;
190
191         /** Byte offset within packet meta-data where the key is located */
192         uint32_t key_offset;
193
194         /** Bit-mask to be AND-ed to the key on lookup */
195         uint8_t *key_mask;
196 };
197
198 extern struct rte_table_ops rte_table_hash_key8_ext_ops;
199
200 /**
201  * 16-byte key hash tables
202  *
203  */
204 /** LRU hash table parameters */
205 struct rte_table_hash_key16_lru_params {
206         /** Maximum number of entries (and keys) in the table */
207         uint32_t n_entries;
208
209         /** Hash function */
210         rte_table_hash_op_hash_nomask f_hash;
211
212         /** Seed for the hash function */
213         uint64_t seed;
214
215         /** Byte offset within packet meta-data where the 4-byte key signature
216         is located. Valid for pre-computed key signature tables, ignored for
217         do-sig tables. */
218         uint32_t signature_offset;
219
220         /** Byte offset within packet meta-data where the key is located */
221         uint32_t key_offset;
222
223         /** Bit-mask to be AND-ed to the key on lookup */
224         uint8_t *key_mask;
225 };
226
227 extern struct rte_table_ops rte_table_hash_key16_lru_ops;
228
229 /** Extendible bucket hash table parameters */
230 struct rte_table_hash_key16_ext_params {
231         /** Maximum number of entries (and keys) in the table */
232         uint32_t n_entries;
233
234         /** Number of entries (and keys) for hash table bucket extensions. Each
235         bucket is extended in increments of 4 keys. */
236         uint32_t n_entries_ext;
237
238         /** Hash function */
239         rte_table_hash_op_hash_nomask f_hash;
240
241         /** Seed for the hash function */
242         uint64_t seed;
243
244         /** Byte offset within packet meta-data where the 4-byte key signature
245         is located. Valid for pre-computed key signature tables, ignored for
246         do-sig tables. */
247         uint32_t signature_offset;
248
249         /** Byte offset within packet meta-data where the key is located */
250         uint32_t key_offset;
251
252         /** Bit-mask to be AND-ed to the key on lookup */
253         uint8_t *key_mask;
254 };
255
256 extern struct rte_table_ops rte_table_hash_key16_ext_ops;
257
258 /**
259  * 32-byte key hash tables
260  *
261  */
262 /** LRU hash table parameters */
263 struct rte_table_hash_key32_lru_params {
264         /** Maximum number of entries (and keys) in the table */
265         uint32_t n_entries;
266
267         /** Hash function */
268         rte_table_hash_op_hash_nomask f_hash;
269
270         /** Seed for the hash function */
271         uint64_t seed;
272
273         /** Byte offset within packet meta-data where the 4-byte key signature
274         is located. Valid for pre-computed key signature tables, ignored for
275         do-sig tables. */
276         uint32_t signature_offset;
277
278         /** Byte offset within packet meta-data where the key is located */
279         uint32_t key_offset;
280 };
281
282 /** LRU hash table operations for pre-computed key signature */
283 extern struct rte_table_ops rte_table_hash_key32_lru_ops;
284
285 /** Extendible bucket hash table parameters */
286 struct rte_table_hash_key32_ext_params {
287         /** Maximum number of entries (and keys) in the table */
288         uint32_t n_entries;
289
290         /** Number of entries (and keys) for hash table bucket extensions. Each
291                 bucket is extended in increments of 4 keys. */
292         uint32_t n_entries_ext;
293
294         /** Hash function */
295         rte_table_hash_op_hash_nomask f_hash;
296
297         /** Seed for the hash function */
298         uint64_t seed;
299
300         /** Byte offset within packet meta-data where the 4-byte key signature
301         is located. Valid for pre-computed key signature tables, ignored for
302         do-sig tables. */
303         uint32_t signature_offset;
304
305         /** Byte offset within packet meta-data where the key is located */
306         uint32_t key_offset;
307 };
308
309 /** Extendible bucket hash table operations */
310 extern struct rte_table_ops rte_table_hash_key32_ext_ops;
311
312 /** Cuckoo hash table parameters */
313 struct rte_table_hash_cuckoo_params {
314     /** Key size (number of bytes */
315                 uint32_t key_size;
316
317         /** Maximum number of hash table entries */
318         uint32_t n_keys;
319
320         /** Hash function used to calculate hash */
321         rte_table_hash_op_hash_nomask f_hash;
322
323         /** Seed value or Init value used by f_hash */
324         uint32_t seed;
325
326         /** Byte offset within packet meta-data where the 4-byte key signature
327         is located. Valid for pre-computed key signature tables, ignored for
328         do-sig tables. */
329         uint32_t signature_offset;
330
331         /** Byte offset within packet meta-data where the key is located */
332         uint32_t key_offset;
333
334         /** Hash table name */
335         const char *name;
336 };
337
338 extern struct rte_table_ops rte_table_hash_cuckoo_ops;
339
340 #ifdef __cplusplus
341 }
342 #endif
343
344 #endif