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