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