hash: add extendable bucket feature
[dpdk.git] / lib / librte_hash / rte_hash.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation
3  */
4
5 #ifndef _RTE_HASH_H_
6 #define _RTE_HASH_H_
7
8 /**
9  * @file
10  *
11  * RTE Hash Table
12  */
13
14 #include <stdint.h>
15 #include <stddef.h>
16
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20
21 /** Maximum size of hash table that can be created. */
22 #define RTE_HASH_ENTRIES_MAX                    (1 << 30)
23
24 /** Maximum number of characters in hash name.*/
25 #define RTE_HASH_NAMESIZE                       32
26
27 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
28 #define RTE_HASH_LOOKUP_BULK_MAX                64
29 #define RTE_HASH_LOOKUP_MULTI_MAX               RTE_HASH_LOOKUP_BULK_MAX
30
31 /** Enable Hardware transactional memory support. */
32 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT  0x01
33
34 /** Default behavior of insertion, single writer/multi writer */
35 #define RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD 0x02
36
37 /** Flag to support reader writer concurrency */
38 #define RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY 0x04
39
40 /** Flag to indicate the extendabe bucket table feature should be used */
41 #define RTE_HASH_EXTRA_FLAGS_EXT_TABLE 0x08
42
43 /** Signature of key that is stored internally. */
44 typedef uint32_t hash_sig_t;
45
46 /** Type of function that can be used for calculating the hash value. */
47 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
48                                       uint32_t init_val);
49
50 /** Type of function used to compare the hash key. */
51 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
52
53 /**
54  * Parameters used when creating the hash table.
55  */
56 struct rte_hash_parameters {
57         const char *name;               /**< Name of the hash. */
58         uint32_t entries;               /**< Total hash table entries. */
59         uint32_t reserved;              /**< Unused field. Should be set to 0 */
60         uint32_t key_len;               /**< Length of hash key. */
61         rte_hash_function hash_func;    /**< Primary Hash function used to calculate hash. */
62         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
63         int socket_id;                  /**< NUMA Socket ID for memory. */
64         uint8_t extra_flag;             /**< Indicate if additional parameters are present. */
65 };
66
67 /** @internal A hash table structure. */
68 struct rte_hash;
69
70 /**
71  * Create a new hash table.
72  *
73  * @param params
74  *   Parameters used to create and initialise the hash table.
75  * @return
76  *   Pointer to hash table structure that is used in future hash table
77  *   operations, or NULL on error, with error code set in rte_errno.
78  *   Possible rte_errno errors include:
79  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
80  *    - E_RTE_SECONDARY - function was called from a secondary process instance
81  *    - ENOENT - missing entry
82  *    - EINVAL - invalid parameter passed to function
83  *    - ENOSPC - the maximum number of memzones has already been allocated
84  *    - EEXIST - a memzone with the same name already exists
85  *    - ENOMEM - no appropriate memory area found in which to create memzone
86  */
87 struct rte_hash *
88 rte_hash_create(const struct rte_hash_parameters *params);
89
90 /**
91  * Set a new hash compare function other than the default one.
92  *
93  * @note Function pointer does not work with multi-process, so do not use it
94  * in multi-process mode.
95  *
96  * @param h
97  *   Hash table for which the function is to be changed
98  * @param func
99  *   New compare function
100  */
101 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
102
103 /**
104  * Find an existing hash table object and return a pointer to it.
105  *
106  * @param name
107  *   Name of the hash table as passed to rte_hash_create()
108  * @return
109  *   Pointer to hash table or NULL if object not found
110  *   with rte_errno set appropriately. Possible rte_errno values include:
111  *    - ENOENT - value not available for return
112  */
113 struct rte_hash *
114 rte_hash_find_existing(const char *name);
115
116 /**
117  * De-allocate all memory used by hash table.
118  * @param h
119  *   Hash table to free
120  */
121 void
122 rte_hash_free(struct rte_hash *h);
123
124 /**
125  * Reset all hash structure, by zeroing all entries
126  * @param h
127  *   Hash table to reset
128  */
129 void
130 rte_hash_reset(struct rte_hash *h);
131
132 /**
133  * Return the number of keys in the hash table
134  * @param h
135  *  Hash table to query from
136  * @return
137  *   - -EINVAL if parameters are invalid
138  *   - A value indicating how many keys were inserted in the table.
139  */
140 int32_t
141 rte_hash_count(const struct rte_hash *h);
142
143 /**
144  * Add a key-value pair to an existing hash table.
145  * This operation is not multi-thread safe
146  * and should only be called from one thread by default.
147  * Thread safety can be enabled by setting flag during
148  * table creation.
149  *
150  * @param h
151  *   Hash table to add the key to.
152  * @param key
153  *   Key to add to the hash table.
154  * @param data
155  *   Data to add to the hash table.
156  * @return
157  *   - 0 if added successfully
158  *   - -EINVAL if the parameters are invalid.
159  *   - -ENOSPC if there is no space in the hash for this key.
160  */
161 int
162 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
163
164 /**
165  * Add a key-value pair with a pre-computed hash value
166  * to an existing hash table.
167  * This operation is not multi-thread safe
168  * and should only be called from one thread by default.
169  * Thread safety can be enabled by setting flag during
170  * table creation.
171  *
172  * @param h
173  *   Hash table to add the key to.
174  * @param key
175  *   Key to add to the hash table.
176  * @param sig
177  *   Precomputed hash value for 'key'
178  * @param data
179  *   Data to add to the hash table.
180  * @return
181  *   - 0 if added successfully
182  *   - -EINVAL if the parameters are invalid.
183  *   - -ENOSPC if there is no space in the hash for this key.
184  */
185 int32_t
186 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
187                                                 hash_sig_t sig, void *data);
188
189 /**
190  * Add a key to an existing hash table. This operation is not multi-thread safe
191  * and should only be called from one thread by default.
192  * Thread safety can be enabled by setting flag during
193  * table creation.
194  *
195  * @param h
196  *   Hash table to add the key to.
197  * @param key
198  *   Key to add to the hash table.
199  * @return
200  *   - -EINVAL if the parameters are invalid.
201  *   - -ENOSPC if there is no space in the hash for this key.
202  *   - A positive value that can be used by the caller as an offset into an
203  *     array of user data. This value is unique for this key.
204  */
205 int32_t
206 rte_hash_add_key(const struct rte_hash *h, const void *key);
207
208 /**
209  * Add a key to an existing hash table.
210  * This operation is not multi-thread safe
211  * and should only be called from one thread by default.
212  * Thread safety can be enabled by setting flag during
213  * table creation.
214  *
215  * @param h
216  *   Hash table to add the key to.
217  * @param key
218  *   Key to add to the hash table.
219  * @param sig
220  *   Precomputed hash value for 'key'.
221  * @return
222  *   - -EINVAL if the parameters are invalid.
223  *   - -ENOSPC if there is no space in the hash for this key.
224  *   - A positive value that can be used by the caller as an offset into an
225  *     array of user data. This value is unique for this key.
226  */
227 int32_t
228 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
229
230 /**
231  * Remove a key from an existing hash table.
232  * This operation is not multi-thread safe
233  * and should only be called from one thread by default.
234  * Thread safety can be enabled by setting flag during
235  * table creation.
236  *
237  * @param h
238  *   Hash table to remove the key from.
239  * @param key
240  *   Key to remove from the hash table.
241  * @return
242  *   - -EINVAL if the parameters are invalid.
243  *   - -ENOENT if the key is not found.
244  *   - A positive value that can be used by the caller as an offset into an
245  *     array of user data. This value is unique for this key, and is the same
246  *     value that was returned when the key was added.
247  */
248 int32_t
249 rte_hash_del_key(const struct rte_hash *h, const void *key);
250
251 /**
252  * Remove a key from an existing hash table.
253  * This operation is not multi-thread safe
254  * and should only be called from one thread by default.
255  * Thread safety can be enabled by setting flag during
256  * table creation.
257  *
258  * @param h
259  *   Hash table to remove the key from.
260  * @param key
261  *   Key to remove from the hash table.
262  * @param sig
263  *   Precomputed hash value for 'key'.
264  * @return
265  *   - -EINVAL if the parameters are invalid.
266  *   - -ENOENT if the key is not found.
267  *   - A positive value that can be used by the caller as an offset into an
268  *     array of user data. This value is unique for this key, and is the same
269  *     value that was returned when the key was added.
270  */
271 int32_t
272 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
273
274 /**
275  * Find a key in the hash table given the position.
276  * This operation is multi-thread safe with regarding to other lookup threads.
277  * Read-write concurrency can be enabled by setting flag during
278  * table creation.
279  *
280  * @param h
281  *   Hash table to get the key from.
282  * @param position
283  *   Position returned when the key was inserted.
284  * @param key
285  *   Output containing a pointer to the key
286  * @return
287  *   - 0 if retrieved successfully
288  *   - -EINVAL if the parameters are invalid.
289  *   - -ENOENT if no valid key is found in the given position.
290  */
291 int
292 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
293                                void **key);
294
295 /**
296  * Find a key-value pair in the hash table.
297  * This operation is multi-thread safe with regarding to other lookup threads.
298  * Read-write concurrency can be enabled by setting flag during
299  * table creation.
300  *
301  * @param h
302  *   Hash table to look in.
303  * @param key
304  *   Key to find.
305  * @param data
306  *   Output with pointer to data returned from the hash table.
307  * @return
308  *   - A positive value that can be used by the caller as an offset into an
309  *     array of user data. This value is unique for this key, and is the same
310  *     value that was returned when the key was added.
311  *   - -EINVAL if the parameters are invalid.
312  *   - -ENOENT if the key is not found.
313  */
314 int
315 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
316
317 /**
318  * Find a key-value pair with a pre-computed hash value
319  * to an existing hash table.
320  * This operation is multi-thread safe with regarding to other lookup threads.
321  * Read-write concurrency can be enabled by setting flag during
322  * table creation.
323  *
324  * @param h
325  *   Hash table to look in.
326  * @param key
327  *   Key to find.
328  * @param sig
329  *   Precomputed hash value for 'key'
330  * @param data
331  *   Output with pointer to data returned from the hash table.
332  * @return
333  *   - A positive value that can be used by the caller as an offset into an
334  *     array of user data. This value is unique for this key, and is the same
335  *     value that was returned when the key was added.
336  *   - -EINVAL if the parameters are invalid.
337  *   - -ENOENT if the key is not found.
338  */
339 int
340 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
341                                         hash_sig_t sig, void **data);
342
343 /**
344  * Find a key in the hash table.
345  * This operation is multi-thread safe with regarding to other lookup threads.
346  * Read-write concurrency can be enabled by setting flag during
347  * table creation.
348  *
349  * @param h
350  *   Hash table to look in.
351  * @param key
352  *   Key to find.
353  * @return
354  *   - -EINVAL if the parameters are invalid.
355  *   - -ENOENT if the key is not found.
356  *   - A positive value that can be used by the caller as an offset into an
357  *     array of user data. This value is unique for this key, and is the same
358  *     value that was returned when the key was added.
359  */
360 int32_t
361 rte_hash_lookup(const struct rte_hash *h, const void *key);
362
363 /**
364  * Find a key in the hash table.
365  * This operation is multi-thread safe with regarding to other lookup threads.
366  * Read-write concurrency can be enabled by setting flag during
367  * table creation.
368  *
369  * @param h
370  *   Hash table to look in.
371  * @param key
372  *   Key to find.
373  * @param sig
374  *   Precomputed hash value for 'key'.
375  * @return
376  *   - -EINVAL if the parameters are invalid.
377  *   - -ENOENT if the key is not found.
378  *   - A positive value that can be used by the caller as an offset into an
379  *     array of user data. This value is unique for this key, and is the same
380  *     value that was returned when the key was added.
381  */
382 int32_t
383 rte_hash_lookup_with_hash(const struct rte_hash *h,
384                                 const void *key, hash_sig_t sig);
385
386 /**
387  * Calc a hash value by key.
388  * This operation is not multi-thread safe.
389  *
390  * @param h
391  *   Hash table to look in.
392  * @param key
393  *   Key to find.
394  * @return
395  *   - hash value
396  */
397 hash_sig_t
398 rte_hash_hash(const struct rte_hash *h, const void *key);
399
400 /**
401  * Find multiple keys in the hash table.
402  * This operation is multi-thread safe with regarding to other lookup threads.
403  * Read-write concurrency can be enabled by setting flag during
404  * table creation.
405  *
406  * @param h
407  *   Hash table to look in.
408  * @param keys
409  *   A pointer to a list of keys to look for.
410  * @param num_keys
411  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
412  * @param hit_mask
413  *   Output containing a bitmask with all successful lookups.
414  * @param data
415  *   Output containing array of data returned from all the successful lookups.
416  * @return
417  *   -EINVAL if there's an error, otherwise number of successful lookups.
418  */
419 int
420 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
421                       uint32_t num_keys, uint64_t *hit_mask, void *data[]);
422
423 /**
424  * Find multiple keys in the hash table.
425  * This operation is multi-thread safe with regarding to other lookup threads.
426  * Read-write concurrency can be enabled by setting flag during
427  * table creation.
428  *
429  * @param h
430  *   Hash table to look in.
431  * @param keys
432  *   A pointer to a list of keys to look for.
433  * @param num_keys
434  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
435  * @param positions
436  *   Output containing a list of values, corresponding to the list of keys that
437  *   can be used by the caller as an offset into an array of user data. These
438  *   values are unique for each key, and are the same values that were returned
439  *   when each key was added. If a key in the list was not found, then -ENOENT
440  *   will be the value.
441  * @return
442  *   -EINVAL if there's an error, otherwise 0.
443  */
444 int
445 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
446                       uint32_t num_keys, int32_t *positions);
447
448 /**
449  * Iterate through the hash table, returning key-value pairs.
450  *
451  * @param h
452  *   Hash table to iterate
453  * @param key
454  *   Output containing the key where current iterator
455  *   was pointing at
456  * @param data
457  *   Output containing the data associated with key.
458  *   Returns NULL if data was not stored.
459  * @param next
460  *   Pointer to iterator. Should be 0 to start iterating the hash table.
461  *   Iterator is incremented after each call of this function.
462  * @return
463  *   Position where key was stored, if successful.
464  *   - -EINVAL if the parameters are invalid.
465  *   - -ENOENT if end of the hash table.
466  */
467 int32_t
468 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
469 #ifdef __cplusplus
470 }
471 #endif
472
473 #endif /* _RTE_HASH_H_ */