hash: fix doxygen of return values
[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  * Return the number of keys in the hash table
131  * @param h
132  *  Hash table to query from
133  * @return
134  *   - -EINVAL if parameters are invalid
135  *   - A value indicating how many keys were inserted in the table.
136  */
137 int32_t
138 rte_hash_count(const struct rte_hash *h);
139
140 /**
141  * Add a key-value pair to an existing hash table.
142  * This operation is not multi-thread safe
143  * and should only be called from one thread.
144  *
145  * @param h
146  *   Hash table to add the key to.
147  * @param key
148  *   Key to add to the hash table.
149  * @param data
150  *   Data to add to the hash table.
151  * @return
152  *   - 0 if added successfully
153  *   - -EINVAL if the parameters are invalid.
154  *   - -ENOSPC if there is no space in the hash for this key.
155  */
156 int
157 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
158
159 /**
160  * Add a key-value pair with a pre-computed hash value
161  * to an existing hash table.
162  * This operation is not multi-thread safe
163  * and should only be called from one thread.
164  *
165  * @param h
166  *   Hash table to add the key to.
167  * @param key
168  *   Key to add to the hash table.
169  * @param sig
170  *   Precomputed hash value for 'key'
171  * @param data
172  *   Data to add to the hash table.
173  * @return
174  *   - 0 if added successfully
175  *   - -EINVAL if the parameters are invalid.
176  *   - -ENOSPC if there is no space in the hash for this key.
177  */
178 int32_t
179 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
180                                                 hash_sig_t sig, void *data);
181
182 /**
183  * Add a key to an existing hash table. This operation is not multi-thread safe
184  * and should only be called from one thread.
185  *
186  * @param h
187  *   Hash table to add the key to.
188  * @param key
189  *   Key to add to the hash table.
190  * @return
191  *   - -EINVAL if the parameters are invalid.
192  *   - -ENOSPC if there is no space in the hash for this key.
193  *   - A positive value that can be used by the caller as an offset into an
194  *     array of user data. This value is unique for this key.
195  */
196 int32_t
197 rte_hash_add_key(const struct rte_hash *h, const void *key);
198
199 /**
200  * Add a key to an existing hash table.
201  * This operation is not multi-thread safe
202  * and should only be called from one thread.
203  *
204  * @param h
205  *   Hash table to add the key to.
206  * @param key
207  *   Key to add to the hash table.
208  * @param sig
209  *   Precomputed hash value for 'key'.
210  * @return
211  *   - -EINVAL if the parameters are invalid.
212  *   - -ENOSPC if there is no space in the hash for this key.
213  *   - A positive value that can be used by the caller as an offset into an
214  *     array of user data. This value is unique for this key.
215  */
216 int32_t
217 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
218
219 /**
220  * Remove a key from an existing hash table.
221  * This operation is not multi-thread safe
222  * and should only be called from one thread.
223  *
224  * @param h
225  *   Hash table to remove the key from.
226  * @param key
227  *   Key to remove from the hash table.
228  * @return
229  *   - -EINVAL if the parameters are invalid.
230  *   - -ENOENT if the key is not found.
231  *   - A positive value that can be used by the caller as an offset into an
232  *     array of user data. This value is unique for this key, and is the same
233  *     value that was returned when the key was added.
234  */
235 int32_t
236 rte_hash_del_key(const struct rte_hash *h, const void *key);
237
238 /**
239  * Remove a key from an existing hash table.
240  * This operation is not multi-thread safe
241  * and should only be called from one thread.
242  *
243  * @param h
244  *   Hash table to remove the key from.
245  * @param key
246  *   Key to remove from the hash table.
247  * @param sig
248  *   Precomputed hash value for 'key'.
249  * @return
250  *   - -EINVAL if the parameters are invalid.
251  *   - -ENOENT if the key is not found.
252  *   - A positive value that can be used by the caller as an offset into an
253  *     array of user data. This value is unique for this key, and is the same
254  *     value that was returned when the key was added.
255  */
256 int32_t
257 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
258
259 /**
260  * Find a key in the hash table given the position.
261  * This operation is multi-thread safe.
262  *
263  * @param h
264  *   Hash table to get the key from.
265  * @param position
266  *   Position returned when the key was inserted.
267  * @param key
268  *   Output containing a pointer to the key
269  * @return
270  *   - 0 if retrieved successfully
271  *   - -EINVAL if the parameters are invalid.
272  *   - -ENOENT if no valid key is found in the given position.
273  */
274 int
275 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
276                                void **key);
277
278 /**
279  * Find a key-value pair in the hash table.
280  * This operation is multi-thread safe.
281  *
282  * @param h
283  *   Hash table to look in.
284  * @param key
285  *   Key to find.
286  * @param data
287  *   Output with pointer to data returned from the hash table.
288  * @return
289  *   - A positive value that can be used by the caller as an offset into an
290  *     array of user data. This value is unique for this key, and is the same
291  *     value that was returned when the key was added.
292  *   - -EINVAL if the parameters are invalid.
293  *   - -ENOENT if the key is not found.
294  */
295 int
296 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
297
298 /**
299  * Find a key-value pair with a pre-computed hash value
300  * to an existing hash table.
301  * This operation is multi-thread safe.
302  *
303  * @param h
304  *   Hash table to look in.
305  * @param key
306  *   Key to find.
307  * @param sig
308  *   Precomputed hash value for 'key'
309  * @param data
310  *   Output with pointer to data returned from the hash table.
311  * @return
312  *   - A positive value that can be used by the caller as an offset into an
313  *     array of user data. This value is unique for this key, and is the same
314  *     value that was returned when the key was added.
315  *   - -EINVAL if the parameters are invalid.
316  *   - -ENOENT if the key is not found.
317  */
318 int
319 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
320                                         hash_sig_t sig, void **data);
321
322 /**
323  * Find a key in the hash table.
324  * This operation is multi-thread safe.
325  *
326  * @param h
327  *   Hash table to look in.
328  * @param key
329  *   Key to find.
330  * @return
331  *   - -EINVAL if the parameters are invalid.
332  *   - -ENOENT if the key is not found.
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  */
337 int32_t
338 rte_hash_lookup(const struct rte_hash *h, const void *key);
339
340 /**
341  * Find a key in the hash table.
342  * This operation is multi-thread safe.
343  *
344  * @param h
345  *   Hash table to look in.
346  * @param key
347  *   Key to find.
348  * @param sig
349  *   Precomputed hash value for 'key'.
350  * @return
351  *   - -EINVAL if the parameters are invalid.
352  *   - -ENOENT if the key is not found.
353  *   - A positive value that can be used by the caller as an offset into an
354  *     array of user data. This value is unique for this key, and is the same
355  *     value that was returned when the key was added.
356  */
357 int32_t
358 rte_hash_lookup_with_hash(const struct rte_hash *h,
359                                 const void *key, hash_sig_t sig);
360
361 /**
362  * Calc a hash value by key.
363  * This operation is not multi-thread safe.
364  *
365  * @param h
366  *   Hash table to look in.
367  * @param key
368  *   Key to find.
369  * @return
370  *   - hash value
371  */
372 hash_sig_t
373 rte_hash_hash(const struct rte_hash *h, const void *key);
374
375 /**
376  * Find multiple keys in the hash table.
377  * This operation is multi-thread safe.
378  *
379  * @param h
380  *   Hash table to look in.
381  * @param keys
382  *   A pointer to a list of keys to look for.
383  * @param num_keys
384  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
385  * @param hit_mask
386  *   Output containing a bitmask with all successful lookups.
387  * @param data
388  *   Output containing array of data returned from all the successful lookups.
389  * @return
390  *   -EINVAL if there's an error, otherwise number of successful lookups.
391  */
392 int
393 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
394                       uint32_t num_keys, uint64_t *hit_mask, void *data[]);
395
396 /**
397  * Find multiple keys in the hash table.
398  * This operation is multi-thread safe.
399  *
400  * @param h
401  *   Hash table to look in.
402  * @param keys
403  *   A pointer to a list of keys to look for.
404  * @param num_keys
405  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
406  * @param positions
407  *   Output containing a list of values, corresponding to the list of keys that
408  *   can be used by the caller as an offset into an array of user data. These
409  *   values are unique for each key, and are the same values that were returned
410  *   when each key was added. If a key in the list was not found, then -ENOENT
411  *   will be the value.
412  * @return
413  *   -EINVAL if there's an error, otherwise 0.
414  */
415 int
416 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
417                       uint32_t num_keys, int32_t *positions);
418
419 /**
420  * Iterate through the hash table, returning key-value pairs.
421  *
422  * @param h
423  *   Hash table to iterate
424  * @param key
425  *   Output containing the key where current iterator
426  *   was pointing at
427  * @param data
428  *   Output containing the data associated with key.
429  *   Returns NULL if data was not stored.
430  * @param next
431  *   Pointer to iterator. Should be 0 to start iterating the hash table.
432  *   Iterator is incremented after each call of this function.
433  * @return
434  *   Position where key was stored, if successful.
435  *   - -EINVAL if the parameters are invalid.
436  *   - -ENOENT if end of the hash table.
437  */
438 int32_t
439 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
440 #ifdef __cplusplus
441 }
442 #endif
443
444 #endif /* _RTE_HASH_H_ */