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);
169 * @b EXPERIMENTAL: this API may change without prior notice
171 * Return the maximum key value ID that could possibly be returned by
172 * rte_hash_add_key function.
175 * Hash table to query from
177 * - -EINVAL if parameters are invalid
178 * - A value indicating the max key ID of key slots present in the table.
182 rte_hash_max_key_id(const struct rte_hash *h);
185 * Add a key-value pair to an existing hash table.
186 * This operation is not multi-thread safe
187 * and should only be called from one thread by default.
188 * Thread safety can be enabled by setting flag during
190 * If the key exists already in the table, this API updates its value
191 * with 'data' passed in this API. It is the responsibility of
192 * the application to manage any memory associated with the old value.
193 * The readers might still be using the old value even after this API
197 * Hash table to add the key to.
199 * Key to add to the hash table.
201 * Data to add to the hash table.
203 * - 0 if added successfully
204 * - -EINVAL if the parameters are invalid.
205 * - -ENOSPC if there is no space in the hash for this key.
208 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
211 * Add a key-value pair with a pre-computed hash value
212 * to an existing hash table.
213 * This operation is not multi-thread safe
214 * and should only be called from one thread by default.
215 * Thread safety can be enabled by setting flag during
217 * If the key exists already in the table, this API updates its value
218 * with 'data' passed in this API. It is the responsibility of
219 * the application to manage any memory associated with the old value.
220 * The readers might still be using the old value even after this API
224 * Hash table to add the key to.
226 * Key to add to the hash table.
228 * Precomputed hash value for 'key'
230 * Data to add to the hash table.
232 * - 0 if added successfully
233 * - -EINVAL if the parameters are invalid.
234 * - -ENOSPC if there is no space in the hash for this key.
237 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
238 hash_sig_t sig, void *data);
241 * Add a key to an existing hash table. This operation is not multi-thread safe
242 * and should only be called from one thread by default.
243 * Thread safety can be enabled by setting flag during
247 * Hash table to add the key to.
249 * Key to add to the hash table.
251 * - -EINVAL if the parameters are invalid.
252 * - -ENOSPC if there is no space in the hash for this key.
253 * - A positive value that can be used by the caller as an offset into an
254 * array of user data. This value is unique for this key. This
255 * unique key id may be larger than the user specified entry count
256 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
259 rte_hash_add_key(const struct rte_hash *h, const void *key);
262 * Add a key to an existing hash table.
263 * This operation is not multi-thread safe
264 * and should only be called from one thread by default.
265 * Thread safety can be enabled by setting flag during
269 * Hash table to add the key to.
271 * Key to add to the hash table.
273 * Precomputed hash value for 'key'.
275 * - -EINVAL if the parameters are invalid.
276 * - -ENOSPC if there is no space in the hash for this key.
277 * - A positive value that can be used by the caller as an offset into an
278 * array of user data. This value is unique for this key. This
279 * unique key ID may be larger than the user specified entry count
280 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
283 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
286 * Remove a key from an existing hash table.
287 * This operation is not multi-thread safe
288 * and should only be called from one thread by default.
289 * Thread safety can be enabled by setting flag during
291 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
292 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
293 * the key index returned by rte_hash_add_key_xxx APIs will not be
294 * freed by this API. rte_hash_free_key_with_position API must be called
295 * additionally to free the index associated with the key.
296 * rte_hash_free_key_with_position API should be called after all
297 * the readers have stopped referencing the entry corresponding to
298 * this key. RCU mechanisms could be used to determine such a state.
301 * Hash table to remove the key from.
303 * Key to remove from the hash table.
305 * - -EINVAL if the parameters are invalid.
306 * - -ENOENT if the key is not found.
307 * - A positive value that can be used by the caller as an offset into an
308 * array of user data. This value is unique for this key, and is the same
309 * value that was returned when the key was added.
312 rte_hash_del_key(const struct rte_hash *h, const void *key);
315 * Remove a key from an existing hash table.
316 * This operation is not multi-thread safe
317 * and should only be called from one thread by default.
318 * Thread safety can be enabled by setting flag during
320 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
321 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
322 * the key index returned by rte_hash_add_key_xxx APIs will not be
323 * freed by this API. rte_hash_free_key_with_position API must be called
324 * additionally to free the index associated with the key.
325 * rte_hash_free_key_with_position API should be called after all
326 * the readers have stopped referencing the entry corresponding to
327 * this key. RCU mechanisms could be used to determine such a state.
330 * Hash table to remove the key from.
332 * Key to remove from the hash table.
334 * Precomputed hash value for 'key'.
336 * - -EINVAL if the parameters are invalid.
337 * - -ENOENT if the key is not found.
338 * - A positive value that can be used by the caller as an offset into an
339 * array of user data. This value is unique for this key, and is the same
340 * value that was returned when the key was added.
343 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
346 * Find a key in the hash table given the position.
347 * This operation is multi-thread safe with regarding to other lookup threads.
348 * Read-write concurrency can be enabled by setting flag during
352 * Hash table to get the key from.
354 * Position returned when the key was inserted.
356 * Output containing a pointer to the key
358 * - 0 if retrieved successfully
359 * - -EINVAL if the parameters are invalid.
360 * - -ENOENT if no valid key is found in the given position.
363 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
368 * @b EXPERIMENTAL: this API may change without prior notice
370 * Free a hash key in the hash table given the position
371 * of the key. This operation is not multi-thread safe and should
372 * only be called from one thread by default. Thread safety
373 * can be enabled by setting flag during table creation.
374 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
375 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
376 * the key index returned by rte_hash_del_key_xxx APIs must be freed
377 * using this API. This API should be called after all the readers
378 * have stopped referencing the entry corresponding to this key.
379 * RCU mechanisms could be used to determine such a state.
380 * This API does not validate if the key is already freed.
383 * Hash table to free the key from.
385 * Position returned when the key was deleted.
387 * - 0 if freed successfully
388 * - -EINVAL if the parameters are invalid.
392 rte_hash_free_key_with_position(const struct rte_hash *h,
393 const int32_t position);
396 * Find a key-value pair in the hash table.
397 * This operation is multi-thread safe with regarding to other lookup threads.
398 * Read-write concurrency can be enabled by setting flag during
402 * Hash table to look in.
406 * Output with pointer to data returned from the hash table.
408 * - A positive value that can be used by the caller as an offset into an
409 * array of user data. This value is unique for this key, and is the same
410 * value that was returned when the key was added.
411 * - -EINVAL if the parameters are invalid.
412 * - -ENOENT if the key is not found.
415 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
418 * Find a key-value pair with a pre-computed hash value
419 * to an existing hash table.
420 * This operation is multi-thread safe with regarding to other lookup threads.
421 * Read-write concurrency can be enabled by setting flag during
425 * Hash table to look in.
429 * Precomputed hash value for 'key'
431 * Output with pointer to data returned from the hash table.
433 * - A positive value that can be used by the caller as an offset into an
434 * array of user data. This value is unique for this key, and is the same
435 * value that was returned when the key was added.
436 * - -EINVAL if the parameters are invalid.
437 * - -ENOENT if the key is not found.
440 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
441 hash_sig_t sig, void **data);
444 * Find a key 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.
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(const struct rte_hash *h, const void *key);
464 * Find a key in the hash table.
465 * This operation is multi-thread safe with regarding to other lookup threads.
466 * Read-write concurrency can be enabled by setting flag during
470 * Hash table to look in.
474 * Precomputed hash value for 'key'.
476 * - -EINVAL if the parameters are invalid.
477 * - -ENOENT if the key is not found.
478 * - A positive value that can be used by the caller as an offset into an
479 * array of user data. This value is unique for this key, and is the same
480 * value that was returned when the key was added.
483 rte_hash_lookup_with_hash(const struct rte_hash *h,
484 const void *key, hash_sig_t sig);
487 * Calc a hash value by key.
488 * This operation is not multi-process safe.
491 * Hash table to look in.
498 rte_hash_hash(const struct rte_hash *h, const void *key);
501 * Find multiple keys in the hash table.
502 * This operation is multi-thread safe with regarding to other lookup threads.
503 * Read-write concurrency can be enabled by setting flag during
507 * Hash table to look in.
509 * A pointer to a list of keys to look for.
511 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
513 * Output containing a bitmask with all successful lookups.
515 * Output containing array of data returned from all the successful lookups.
517 * -EINVAL if there's an error, otherwise number of successful lookups.
520 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
521 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
524 * Find multiple keys in the hash table.
525 * This operation is multi-thread safe with regarding to other lookup threads.
526 * Read-write concurrency can be enabled by setting flag during
530 * Hash table to look in.
532 * A pointer to a list of keys to look for.
534 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
536 * Output containing a list of values, corresponding to the list of keys that
537 * can be used by the caller as an offset into an array of user data. These
538 * values are unique for each key, and are the same values that were returned
539 * when each key was added. If a key in the list was not found, then -ENOENT
542 * -EINVAL if there's an error, otherwise 0.
545 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
546 uint32_t num_keys, int32_t *positions);
549 * Iterate through the hash table, returning key-value pairs.
552 * Hash table to iterate
554 * Output containing the key where current iterator
557 * Output containing the data associated with key.
558 * Returns NULL if data was not stored.
560 * Pointer to iterator. Should be 0 to start iterating the hash table.
561 * Iterator is incremented after each call of this function.
563 * Position where key was stored, if successful.
564 * - -EINVAL if the parameters are invalid.
565 * - -ENOENT if end of the hash table.
568 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
573 #endif /* _RTE_HASH_H_ */