1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
17 #include <rte_compat.h>
23 /** Maximum size of hash table that can be created. */
24 #define RTE_HASH_ENTRIES_MAX (1 << 30)
26 /** Maximum number of characters in hash name.*/
27 #define RTE_HASH_NAMESIZE 32
29 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
30 #define RTE_HASH_LOOKUP_BULK_MAX 64
31 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
33 /** Enable Hardware transactional memory support. */
34 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT 0x01
36 /** Default behavior of insertion, single writer/multi writer */
37 #define RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD 0x02
39 /** Flag to support reader writer concurrency */
40 #define RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY 0x04
42 /** Flag to indicate the extendabe bucket table feature should be used */
43 #define RTE_HASH_EXTRA_FLAGS_EXT_TABLE 0x08
45 /** Flag to disable freeing of key index on hash delete.
46 * Refer to rte_hash_del_xxx APIs for more details.
48 #define RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL 0x10
51 * The type of hash value of a key.
52 * It should be a value of at least 32bit with fully random pattern.
54 typedef uint32_t hash_sig_t;
56 /** Type of function that can be used for calculating the hash value. */
57 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
60 /** Type of function used to compare the hash key. */
61 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
64 * Parameters used when creating the hash table.
66 struct rte_hash_parameters {
67 const char *name; /**< Name of the hash. */
68 uint32_t entries; /**< Total hash table entries. */
69 uint32_t reserved; /**< Unused field. Should be set to 0 */
70 uint32_t key_len; /**< Length of hash key. */
71 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
72 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
73 int socket_id; /**< NUMA Socket ID for memory. */
74 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
77 /** @internal A hash table structure. */
81 * Create a new hash table.
84 * Parameters used to create and initialise the hash table.
86 * Pointer to hash table structure that is used in future hash table
87 * operations, or NULL on error, with error code set in rte_errno.
88 * Possible rte_errno errors include:
89 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
90 * - E_RTE_SECONDARY - function was called from a secondary process instance
91 * - ENOENT - missing entry
92 * - EINVAL - invalid parameter passed to function
93 * - ENOSPC - the maximum number of memzones has already been allocated
94 * - EEXIST - a memzone with the same name already exists
95 * - ENOMEM - no appropriate memory area found in which to create memzone
98 rte_hash_create(const struct rte_hash_parameters *params);
101 * Set a new hash compare function other than the default one.
103 * @note Function pointer does not work with multi-process, so do not use it
104 * in multi-process mode.
107 * Hash table for which the function is to be changed
109 * New compare function
111 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
114 * Find an existing hash table object and return a pointer to it.
117 * Name of the hash table as passed to rte_hash_create()
119 * Pointer to hash table or NULL if object not found
120 * with rte_errno set appropriately. Possible rte_errno values include:
121 * - ENOENT - value not available for return
124 rte_hash_find_existing(const char *name);
127 * De-allocate all memory used by hash table.
132 rte_hash_free(struct rte_hash *h);
135 * Reset all hash structure, by zeroing all entries
137 * Hash table to reset
140 rte_hash_reset(struct rte_hash *h);
143 * Return the number of keys in the hash table
145 * Hash table to query from
147 * - -EINVAL if parameters are invalid
148 * - A value indicating how many keys were inserted in the table.
151 rte_hash_count(const struct rte_hash *h);
154 * Add a key-value pair to an existing hash table.
155 * This operation is not multi-thread safe
156 * and should only be called from one thread by default.
157 * Thread safety can be enabled by setting flag during
161 * Hash table to add the key to.
163 * Key to add to the hash table.
165 * Data to add to the hash table.
167 * - 0 if added successfully
168 * - -EINVAL if the parameters are invalid.
169 * - -ENOSPC if there is no space in the hash for this key.
172 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
175 * Add a key-value pair with a pre-computed hash value
176 * to an existing hash table.
177 * This operation is not multi-thread safe
178 * and should only be called from one thread by default.
179 * Thread safety can be enabled by setting flag during
183 * Hash table to add the key to.
185 * Key to add to the hash table.
187 * Precomputed hash value for 'key'
189 * Data to add to the hash table.
191 * - 0 if added successfully
192 * - -EINVAL if the parameters are invalid.
193 * - -ENOSPC if there is no space in the hash for this key.
196 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
197 hash_sig_t sig, void *data);
200 * Add a key to an existing hash table. This operation is not multi-thread safe
201 * and should only be called from one thread by default.
202 * Thread safety can be enabled by setting flag during
206 * Hash table to add the key to.
208 * Key to add to the hash table.
210 * - -EINVAL if the parameters are invalid.
211 * - -ENOSPC if there is no space in the hash for this key.
212 * - A positive value that can be used by the caller as an offset into an
213 * array of user data. This value is unique for this key.
216 rte_hash_add_key(const struct rte_hash *h, const void *key);
219 * Add a key to an existing hash table.
220 * This operation is not multi-thread safe
221 * and should only be called from one thread by default.
222 * Thread safety can be enabled by setting flag during
226 * Hash table to add the key to.
228 * Key to add to the hash table.
230 * Precomputed hash value for 'key'.
232 * - -EINVAL if the parameters are invalid.
233 * - -ENOSPC if there is no space in the hash for this key.
234 * - A positive value that can be used by the caller as an offset into an
235 * array of user data. This value is unique for this key.
238 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
241 * Remove a key from an existing hash table.
242 * This operation is not multi-thread safe
243 * and should only be called from one thread by default.
244 * Thread safety can be enabled by setting flag during
246 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL is enabled,
247 * the key index returned by rte_hash_add_key_xxx APIs will not be
248 * freed by this API. rte_hash_free_key_with_position API must be called
249 * additionally to free the index associated with the key.
252 * Hash table to remove the key from.
254 * Key to remove from the hash table.
256 * - -EINVAL if the parameters are invalid.
257 * - -ENOENT if the key is not found.
258 * - A positive value that can be used by the caller as an offset into an
259 * array of user data. This value is unique for this key, and is the same
260 * value that was returned when the key was added.
263 rte_hash_del_key(const struct rte_hash *h, const void *key);
266 * Remove a key from an existing hash table.
267 * This operation is not multi-thread safe
268 * and should only be called from one thread by default.
269 * Thread safety can be enabled by setting flag during
271 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL is enabled,
272 * the key index returned by rte_hash_add_key_xxx APIs will not be
273 * freed by this API. rte_hash_free_key_with_position API must be called
274 * additionally to free the index associated with the key.
277 * Hash table to remove the key from.
279 * Key to remove from the hash table.
281 * Precomputed hash value for 'key'.
283 * - -EINVAL if the parameters are invalid.
284 * - -ENOENT if the key is not found.
285 * - A positive value that can be used by the caller as an offset into an
286 * array of user data. This value is unique for this key, and is the same
287 * value that was returned when the key was added.
290 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
293 * Find a key in the hash table given the position.
294 * This operation is multi-thread safe with regarding to other lookup threads.
295 * Read-write concurrency can be enabled by setting flag during
299 * Hash table to get the key from.
301 * Position returned when the key was inserted.
303 * Output containing a pointer to the key
305 * - 0 if retrieved successfully
306 * - -EINVAL if the parameters are invalid.
307 * - -ENOENT if no valid key is found in the given position.
310 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
315 * @b EXPERIMENTAL: this API may change without prior notice
317 * Free a hash key in the hash table given the position
318 * of the key. This operation is not multi-thread safe and should
319 * only be called from one thread by default. Thread safety
320 * can be enabled by setting flag during table creation.
321 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL is enabled,
322 * this API must be called, with the key index returned by rte_hash_add_key_xxx
323 * APIs, after the key is deleted using rte_hash_del_key_xxx APIs.
324 * This API does not validate if the key is already freed.
327 * Hash table to free the key from.
329 * Position returned when the key was deleted.
331 * - 0 if freed successfully
332 * - -EINVAL if the parameters are invalid.
334 int __rte_experimental
335 rte_hash_free_key_with_position(const struct rte_hash *h,
336 const int32_t position);
339 * Find a key-value pair in the hash table.
340 * This operation is multi-thread safe with regarding to other lookup threads.
341 * Read-write concurrency can be enabled by setting flag during
345 * Hash table to look in.
349 * Output with pointer to data returned from the hash table.
351 * - A positive value that can be used by the caller as an offset into an
352 * array of user data. This value is unique for this key, and is the same
353 * value that was returned when the key was added.
354 * - -EINVAL if the parameters are invalid.
355 * - -ENOENT if the key is not found.
358 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
361 * Find a key-value pair with a pre-computed hash value
362 * to an existing hash table.
363 * This operation is multi-thread safe with regarding to other lookup threads.
364 * Read-write concurrency can be enabled by setting flag during
368 * Hash table to look in.
372 * Precomputed hash value for 'key'
374 * Output with pointer to data returned from the hash table.
376 * - A positive value that can be used by the caller as an offset into an
377 * array of user data. This value is unique for this key, and is the same
378 * value that was returned when the key was added.
379 * - -EINVAL if the parameters are invalid.
380 * - -ENOENT if the key is not found.
383 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
384 hash_sig_t sig, void **data);
387 * Find a key in the hash table.
388 * This operation is multi-thread safe with regarding to other lookup threads.
389 * Read-write concurrency can be enabled by setting flag during
393 * Hash table to look in.
397 * - -EINVAL if the parameters are invalid.
398 * - -ENOENT if the key is not found.
399 * - A positive value that can be used by the caller as an offset into an
400 * array of user data. This value is unique for this key, and is the same
401 * value that was returned when the key was added.
404 rte_hash_lookup(const struct rte_hash *h, const void *key);
407 * Find a key in the hash table.
408 * This operation is multi-thread safe with regarding to other lookup threads.
409 * Read-write concurrency can be enabled by setting flag during
413 * Hash table to look in.
417 * Precomputed hash value for 'key'.
419 * - -EINVAL if the parameters are invalid.
420 * - -ENOENT if the key is not found.
421 * - A positive value that can be used by the caller as an offset into an
422 * array of user data. This value is unique for this key, and is the same
423 * value that was returned when the key was added.
426 rte_hash_lookup_with_hash(const struct rte_hash *h,
427 const void *key, hash_sig_t sig);
430 * Calc a hash value by key.
431 * This operation is not multi-thread safe.
434 * Hash table to look in.
441 rte_hash_hash(const struct rte_hash *h, const void *key);
444 * Find multiple keys in the hash table.
445 * This operation is multi-thread safe with regarding to other lookup threads.
446 * Read-write concurrency can be enabled by setting flag during
450 * Hash table to look in.
452 * A pointer to a list of keys to look for.
454 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
456 * Output containing a bitmask with all successful lookups.
458 * Output containing array of data returned from all the successful lookups.
460 * -EINVAL if there's an error, otherwise number of successful lookups.
463 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
464 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
467 * Find multiple keys in the hash table.
468 * This operation is multi-thread safe with regarding to other lookup threads.
469 * Read-write concurrency can be enabled by setting flag during
473 * Hash table to look in.
475 * A pointer to a list of keys to look for.
477 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
479 * Output containing a list of values, corresponding to the list of keys that
480 * can be used by the caller as an offset into an array of user data. These
481 * values are unique for each key, and are the same values that were returned
482 * when each key was added. If a key in the list was not found, then -ENOENT
485 * -EINVAL if there's an error, otherwise 0.
488 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
489 uint32_t num_keys, int32_t *positions);
492 * Iterate through the hash table, returning key-value pairs.
495 * Hash table to iterate
497 * Output containing the key where current iterator
500 * Output containing the data associated with key.
501 * Returns NULL if data was not stored.
503 * Pointer to iterator. Should be 0 to start iterating the hash table.
504 * Iterator is incremented after each call of this function.
506 * Position where key was stored, if successful.
507 * - -EINVAL if the parameters are invalid.
508 * - -ENOENT if end of the hash table.
511 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
516 #endif /* _RTE_HASH_H_ */