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 extendable 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.
47 * This is enabled by default when RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF
50 #define RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL 0x10
52 /** Flag to support lock free reader writer concurrency. Both single writer
53 * and multi writer use cases are supported.
54 * Currently, extendable bucket table feature is not supported with
57 #define RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF 0x20
60 * The type of hash value of a key.
61 * It should be a value of at least 32bit with fully random pattern.
63 typedef uint32_t hash_sig_t;
65 /** Type of function that can be used for calculating the hash value. */
66 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
69 /** Type of function used to compare the hash key. */
70 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
73 * Parameters used when creating the hash table.
75 struct rte_hash_parameters {
76 const char *name; /**< Name of the hash. */
77 uint32_t entries; /**< Total hash table entries. */
78 uint32_t reserved; /**< Unused field. Should be set to 0 */
79 uint32_t key_len; /**< Length of hash key. */
80 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
81 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
82 int socket_id; /**< NUMA Socket ID for memory. */
83 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
86 /** @internal A hash table structure. */
90 * Create a new hash table.
93 * Parameters used to create and initialise the hash table.
95 * Pointer to hash table structure that is used in future hash table
96 * operations, or NULL on error, with error code set in rte_errno.
97 * Possible rte_errno errors include:
98 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
99 * - E_RTE_SECONDARY - function was called from a secondary process instance
100 * - ENOENT - missing entry
101 * - EINVAL - invalid parameter passed to function
102 * - ENOSPC - the maximum number of memzones has already been allocated
103 * - EEXIST - a memzone with the same name already exists
104 * - ENOMEM - no appropriate memory area found in which to create memzone
107 rte_hash_create(const struct rte_hash_parameters *params);
110 * Set a new hash compare function other than the default one.
112 * @note Function pointer does not work with multi-process, so do not use it
113 * in multi-process mode.
116 * Hash table for which the function is to be changed
118 * New compare function
120 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
123 * Find an existing hash table object and return a pointer to it.
126 * Name of the hash table as passed to rte_hash_create()
128 * Pointer to hash table or NULL if object not found
129 * with rte_errno set appropriately. Possible rte_errno values include:
130 * - ENOENT - value not available for return
133 rte_hash_find_existing(const char *name);
136 * De-allocate all memory used by hash table.
141 rte_hash_free(struct rte_hash *h);
144 * Reset all hash structure, by zeroing all entries.
145 * When RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
146 * it is application's responsibility to make sure that
147 * none of the readers are referencing the hash table
148 * while calling this API.
151 * Hash table to reset
154 rte_hash_reset(struct rte_hash *h);
157 * Return the number of keys in the hash table
159 * Hash table to query from
161 * - -EINVAL if parameters are invalid
162 * - A value indicating how many keys were inserted in the table.
165 rte_hash_count(const struct rte_hash *h);
168 * Add a key-value pair to an existing hash table.
169 * This operation is not multi-thread safe
170 * and should only be called from one thread by default.
171 * Thread safety can be enabled by setting flag during
173 * If the key exists already in the table, this API updates its value
174 * with 'data' passed in this API. It is the responsibility of
175 * the application to manage any memory associated with the old value.
176 * The readers might still be using the old value even after this API
180 * Hash table to add the key to.
182 * Key to add to the hash table.
184 * Data to add to the hash table.
186 * - 0 if added successfully
187 * - -EINVAL if the parameters are invalid.
188 * - -ENOSPC if there is no space in the hash for this key.
191 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
194 * Add a key-value pair with a pre-computed hash value
195 * to an existing hash table.
196 * This operation is not multi-thread safe
197 * and should only be called from one thread by default.
198 * Thread safety can be enabled by setting flag during
200 * If the key exists already in the table, this API updates its value
201 * with 'data' passed in this API. It is the responsibility of
202 * the application to manage any memory associated with the old value.
203 * The readers might still be using the old value even after this API
207 * Hash table to add the key to.
209 * Key to add to the hash table.
211 * Precomputed hash value for 'key'
213 * Data to add to the hash table.
215 * - 0 if added successfully
216 * - -EINVAL if the parameters are invalid.
217 * - -ENOSPC if there is no space in the hash for this key.
220 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
221 hash_sig_t sig, void *data);
224 * Add a key to an existing hash table. This operation is not multi-thread safe
225 * and should only be called from one thread by default.
226 * Thread safety can be enabled by setting flag during
230 * Hash table to add the key to.
232 * Key to add to the hash table.
234 * - -EINVAL if the parameters are invalid.
235 * - -ENOSPC if there is no space in the hash for this key.
236 * - A positive value that can be used by the caller as an offset into an
237 * array of user data. This value is unique for this key.
240 rte_hash_add_key(const struct rte_hash *h, const void *key);
243 * Add a key to an existing hash table.
244 * This operation is not multi-thread safe
245 * and should only be called from one thread by default.
246 * Thread safety can be enabled by setting flag during
250 * Hash table to add the key to.
252 * Key to add to the hash table.
254 * Precomputed hash value for 'key'.
256 * - -EINVAL if the parameters are invalid.
257 * - -ENOSPC if there is no space in the hash for this key.
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.
262 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
265 * Remove a key from an existing hash table.
266 * This operation is not multi-thread safe
267 * and should only be called from one thread by default.
268 * Thread safety can be enabled by setting flag during
270 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
271 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF 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.
275 * rte_hash_free_key_with_position API should be called after all
276 * the readers have stopped referencing the entry corresponding to
277 * this key. RCU mechanisms could be used to determine such a state.
280 * Hash table to remove the key from.
282 * Key to remove from the hash table.
284 * - -EINVAL if the parameters are invalid.
285 * - -ENOENT if the key is not found.
286 * - A positive value that can be used by the caller as an offset into an
287 * array of user data. This value is unique for this key, and is the same
288 * value that was returned when the key was added.
291 rte_hash_del_key(const struct rte_hash *h, const void *key);
294 * Remove a key from an existing hash table.
295 * This operation is not multi-thread safe
296 * and should only be called from one thread by default.
297 * Thread safety can be enabled by setting flag during
299 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
300 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
301 * the key index returned by rte_hash_add_key_xxx APIs will not be
302 * freed by this API. rte_hash_free_key_with_position API must be called
303 * additionally to free the index associated with the key.
304 * rte_hash_free_key_with_position API should be called after all
305 * the readers have stopped referencing the entry corresponding to
306 * this key. RCU mechanisms could be used to determine such a state.
309 * Hash table to remove the key from.
311 * Key to remove from the hash table.
313 * Precomputed hash value for 'key'.
315 * - -EINVAL if the parameters are invalid.
316 * - -ENOENT if the key is not found.
317 * - A positive value that can be used by the caller as an offset into an
318 * array of user data. This value is unique for this key, and is the same
319 * value that was returned when the key was added.
322 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
325 * Find a key in the hash table given the position.
326 * This operation is multi-thread safe with regarding to other lookup threads.
327 * Read-write concurrency can be enabled by setting flag during
331 * Hash table to get the key from.
333 * Position returned when the key was inserted.
335 * Output containing a pointer to the key
337 * - 0 if retrieved successfully
338 * - -EINVAL if the parameters are invalid.
339 * - -ENOENT if no valid key is found in the given position.
342 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
347 * @b EXPERIMENTAL: this API may change without prior notice
349 * Free a hash key in the hash table given the position
350 * of the key. This operation is not multi-thread safe and should
351 * only be called from one thread by default. Thread safety
352 * can be enabled by setting flag during table creation.
353 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
354 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
355 * the key index returned by rte_hash_del_key_xxx APIs must be freed
356 * using this API. This API should be called after all the readers
357 * have stopped referencing the entry corresponding to this key.
358 * RCU mechanisms could be used to determine such a state.
359 * This API does not validate if the key is already freed.
362 * Hash table to free the key from.
364 * Position returned when the key was deleted.
366 * - 0 if freed successfully
367 * - -EINVAL if the parameters are invalid.
369 int __rte_experimental
370 rte_hash_free_key_with_position(const struct rte_hash *h,
371 const int32_t position);
374 * Find a key-value pair in the hash table.
375 * This operation is multi-thread safe with regarding to other lookup threads.
376 * Read-write concurrency can be enabled by setting flag during
380 * Hash table to look in.
384 * Output with pointer to data returned from the hash table.
386 * - A positive value that can be used by the caller as an offset into an
387 * array of user data. This value is unique for this key, and is the same
388 * value that was returned when the key was added.
389 * - -EINVAL if the parameters are invalid.
390 * - -ENOENT if the key is not found.
393 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
396 * Find a key-value pair with a pre-computed hash value
397 * to an existing hash table.
398 * This operation is multi-thread safe with regarding to other lookup threads.
399 * Read-write concurrency can be enabled by setting flag during
403 * Hash table to look in.
407 * Precomputed hash value for 'key'
409 * Output with pointer to data returned from the hash table.
411 * - A positive value that can be used by the caller as an offset into an
412 * array of user data. This value is unique for this key, and is the same
413 * value that was returned when the key was added.
414 * - -EINVAL if the parameters are invalid.
415 * - -ENOENT if the key is not found.
418 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
419 hash_sig_t sig, void **data);
422 * Find a key in the hash table.
423 * This operation is multi-thread safe with regarding to other lookup threads.
424 * Read-write concurrency can be enabled by setting flag during
428 * Hash table to look in.
432 * - -EINVAL if the parameters are invalid.
433 * - -ENOENT if the key is not found.
434 * - A positive value that can be used by the caller as an offset into an
435 * array of user data. This value is unique for this key, and is the same
436 * value that was returned when the key was added.
439 rte_hash_lookup(const struct rte_hash *h, const void *key);
442 * Find a key in the hash table.
443 * This operation is multi-thread safe with regarding to other lookup threads.
444 * Read-write concurrency can be enabled by setting flag during
448 * Hash table to look in.
452 * Precomputed hash value for 'key'.
454 * - -EINVAL if the parameters are invalid.
455 * - -ENOENT if the key is not found.
456 * - A positive value that can be used by the caller as an offset into an
457 * array of user data. This value is unique for this key, and is the same
458 * value that was returned when the key was added.
461 rte_hash_lookup_with_hash(const struct rte_hash *h,
462 const void *key, hash_sig_t sig);
465 * Calc a hash value by key.
466 * This operation is not multi-process safe.
469 * Hash table to look in.
476 rte_hash_hash(const struct rte_hash *h, const void *key);
479 * Find multiple keys in the hash table.
480 * This operation is multi-thread safe with regarding to other lookup threads.
481 * Read-write concurrency can be enabled by setting flag during
485 * Hash table to look in.
487 * A pointer to a list of keys to look for.
489 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
491 * Output containing a bitmask with all successful lookups.
493 * Output containing array of data returned from all the successful lookups.
495 * -EINVAL if there's an error, otherwise number of successful lookups.
498 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
499 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
502 * Find multiple keys in the hash table.
503 * This operation is multi-thread safe with regarding to other lookup threads.
504 * Read-write concurrency can be enabled by setting flag during
508 * Hash table to look in.
510 * A pointer to a list of keys to look for.
512 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
514 * Output containing a list of values, corresponding to the list of keys that
515 * can be used by the caller as an offset into an array of user data. These
516 * values are unique for each key, and are the same values that were returned
517 * when each key was added. If a key in the list was not found, then -ENOENT
520 * -EINVAL if there's an error, otherwise 0.
523 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
524 uint32_t num_keys, int32_t *positions);
527 * Iterate through the hash table, returning key-value pairs.
530 * Hash table to iterate
532 * Output containing the key where current iterator
535 * Output containing the data associated with key.
536 * Returns NULL if data was not stored.
538 * Pointer to iterator. Should be 0 to start iterating the hash table.
539 * Iterator is incremented after each call of this function.
541 * Position where key was stored, if successful.
542 * - -EINVAL if the parameters are invalid.
543 * - -ENOENT if end of the hash table.
546 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
551 #endif /* _RTE_HASH_H_ */