1f1a276df7b92a595a680fc8d7362f09ef3bc314
[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  *   0 if successful lookup
290  *   - EINVAL if the parameters are invalid.
291  *   - ENOENT if the key is not found.
292  */
293 int
294 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
295
296 /**
297  * Find a key-value pair with a pre-computed hash value
298  * to an existing hash table.
299  * This operation is multi-thread safe.
300  *
301  * @param h
302  *   Hash table to look in.
303  * @param key
304  *   Key to find.
305  * @param sig
306  *   Precomputed hash value for 'key'
307  * @param data
308  *   Output with pointer to data returned from the hash table.
309  * @return
310  *   0 if successful lookup
311  *   - EINVAL if the parameters are invalid.
312  *   - ENOENT if the key is not found.
313  */
314 int
315 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
316                                         hash_sig_t sig, void **data);
317
318 /**
319  * Find a key in the hash table.
320  * This operation is multi-thread safe.
321  *
322  * @param h
323  *   Hash table to look in.
324  * @param key
325  *   Key to find.
326  * @return
327  *   - -EINVAL if the parameters are invalid.
328  *   - -ENOENT if the key is not found.
329  *   - A positive value that can be used by the caller as an offset into an
330  *     array of user data. This value is unique for this key, and is the same
331  *     value that was returned when the key was added.
332  */
333 int32_t
334 rte_hash_lookup(const struct rte_hash *h, const void *key);
335
336 /**
337  * Find a key in the hash table.
338  * This operation is multi-thread safe.
339  *
340  * @param h
341  *   Hash table to look in.
342  * @param key
343  *   Key to find.
344  * @param sig
345  *   Precomputed hash value for 'key'.
346  * @return
347  *   - -EINVAL if the parameters are invalid.
348  *   - -ENOENT if the key is not found.
349  *   - A positive value that can be used by the caller as an offset into an
350  *     array of user data. This value is unique for this key, and is the same
351  *     value that was returned when the key was added.
352  */
353 int32_t
354 rte_hash_lookup_with_hash(const struct rte_hash *h,
355                                 const void *key, hash_sig_t sig);
356
357 /**
358  * Calc a hash value by key.
359  * This operation is not multi-thread safe.
360  *
361  * @param h
362  *   Hash table to look in.
363  * @param key
364  *   Key to find.
365  * @return
366  *   - hash value
367  */
368 hash_sig_t
369 rte_hash_hash(const struct rte_hash *h, const void *key);
370
371 /**
372  * Find multiple keys in the hash table.
373  * This operation is multi-thread safe.
374  *
375  * @param h
376  *   Hash table to look in.
377  * @param keys
378  *   A pointer to a list of keys to look for.
379  * @param num_keys
380  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
381  * @param hit_mask
382  *   Output containing a bitmask with all successful lookups.
383  * @param data
384  *   Output containing array of data returned from all the successful lookups.
385  * @return
386  *   -EINVAL if there's an error, otherwise number of successful lookups.
387  */
388 int
389 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
390                       uint32_t num_keys, uint64_t *hit_mask, void *data[]);
391
392 /**
393  * Find multiple keys in the hash table.
394  * This operation is multi-thread safe.
395  *
396  * @param h
397  *   Hash table to look in.
398  * @param keys
399  *   A pointer to a list of keys to look for.
400  * @param num_keys
401  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
402  * @param positions
403  *   Output containing a list of values, corresponding to the list of keys that
404  *   can be used by the caller as an offset into an array of user data. These
405  *   values are unique for each key, and are the same values that were returned
406  *   when each key was added. If a key in the list was not found, then -ENOENT
407  *   will be the value.
408  * @return
409  *   -EINVAL if there's an error, otherwise 0.
410  */
411 int
412 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
413                       uint32_t num_keys, int32_t *positions);
414
415 /**
416  * Iterate through the hash table, returning key-value pairs.
417  *
418  * @param h
419  *   Hash table to iterate
420  * @param key
421  *   Output containing the key where current iterator
422  *   was pointing at
423  * @param data
424  *   Output containing the data associated with key.
425  *   Returns NULL if data was not stored.
426  * @param next
427  *   Pointer to iterator. Should be 0 to start iterating the hash table.
428  *   Iterator is incremented after each call of this function.
429  * @return
430  *   Position where key was stored, if successful.
431  *   - -EINVAL if the parameters are invalid.
432  *   - -ENOENT if end of the hash table.
433  */
434 int32_t
435 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
436 #ifdef __cplusplus
437 }
438 #endif
439
440 #endif /* _RTE_HASH_H_ */