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.
55 #define RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF 0x20
58 * The type of hash value of a key.
59 * It should be a value of at least 32bit with fully random pattern.
61 typedef uint32_t hash_sig_t;
63 /** Type of function that can be used for calculating the hash value. */
64 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
67 /** Type of function used to compare the hash key. */
68 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
71 * Parameters used when creating the hash table.
73 struct rte_hash_parameters {
74 const char *name; /**< Name of the hash. */
75 uint32_t entries; /**< Total hash table entries. */
76 uint32_t reserved; /**< Unused field. Should be set to 0 */
77 uint32_t key_len; /**< Length of hash key. */
78 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
79 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
80 int socket_id; /**< NUMA Socket ID for memory. */
81 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
84 /** @internal A hash table structure. */
88 * Create a new hash table.
91 * Parameters used to create and initialise the hash table.
93 * Pointer to hash table structure that is used in future hash table
94 * operations, or NULL on error, with error code set in rte_errno.
95 * Possible rte_errno errors include:
96 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
97 * - E_RTE_SECONDARY - function was called from a secondary process instance
98 * - ENOENT - missing entry
99 * - EINVAL - invalid parameter passed to function
100 * - ENOSPC - the maximum number of memzones has already been allocated
101 * - EEXIST - a memzone with the same name already exists
102 * - ENOMEM - no appropriate memory area found in which to create memzone
105 rte_hash_create(const struct rte_hash_parameters *params);
108 * Set a new hash compare function other than the default one.
110 * @note Function pointer does not work with multi-process, so do not use it
111 * in multi-process mode.
114 * Hash table for which the function is to be changed
116 * New compare function
118 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
121 * Find an existing hash table object and return a pointer to it.
124 * Name of the hash table as passed to rte_hash_create()
126 * Pointer to hash table or NULL if object not found
127 * with rte_errno set appropriately. Possible rte_errno values include:
128 * - ENOENT - value not available for return
131 rte_hash_find_existing(const char *name);
134 * De-allocate all memory used by hash table.
139 rte_hash_free(struct rte_hash *h);
142 * Reset all hash structure, by zeroing all entries.
143 * When RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
144 * it is application's responsibility to make sure that
145 * none of the readers are referencing the hash table
146 * while calling this API.
149 * Hash table to reset
152 rte_hash_reset(struct rte_hash *h);
155 * Return the number of keys in the hash table
157 * Hash table to query from
159 * - -EINVAL if parameters are invalid
160 * - A value indicating how many keys were inserted in the table.
163 rte_hash_count(const struct rte_hash *h);
167 * @b EXPERIMENTAL: this API may change without prior notice
169 * Return the maximum key value ID that could possibly be returned by
170 * rte_hash_add_key function.
173 * Hash table to query from
175 * - -EINVAL if parameters are invalid
176 * - A value indicating the max key ID of key slots present in the table.
180 rte_hash_max_key_id(const struct rte_hash *h);
183 * Add a key-value pair to an existing hash table.
184 * This operation is not multi-thread safe
185 * and should only be called from one thread by default.
186 * Thread safety can be enabled by setting flag during
188 * If the key exists already in the table, this API updates its value
189 * with 'data' passed in this API. It is the responsibility of
190 * the application to manage any memory associated with the old value.
191 * The readers might still be using the old value even after this API
195 * Hash table to add the key to.
197 * Key to add to the hash table.
199 * Data to add to the hash table.
201 * - 0 if added successfully
202 * - -EINVAL if the parameters are invalid.
203 * - -ENOSPC if there is no space in the hash for this key.
206 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
209 * Add a key-value pair with a pre-computed hash value
210 * to an existing hash table.
211 * This operation is not multi-thread safe
212 * and should only be called from one thread by default.
213 * Thread safety can be enabled by setting flag during
215 * If the key exists already in the table, this API updates its value
216 * with 'data' passed in this API. It is the responsibility of
217 * the application to manage any memory associated with the old value.
218 * The readers might still be using the old value even after this API
222 * Hash table to add the key to.
224 * Key to add to the hash table.
226 * Precomputed hash value for 'key'
228 * Data to add to the hash table.
230 * - 0 if added successfully
231 * - -EINVAL if the parameters are invalid.
232 * - -ENOSPC if there is no space in the hash for this key.
235 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
236 hash_sig_t sig, void *data);
239 * Add a key to an existing hash table. This operation is not multi-thread safe
240 * and should only be called from one thread by default.
241 * Thread safety can be enabled by setting flag during
245 * Hash table to add the key to.
247 * Key to add to the hash table.
249 * - -EINVAL if the parameters are invalid.
250 * - -ENOSPC if there is no space in the hash for this key.
251 * - A positive value that can be used by the caller as an offset into an
252 * array of user data. This value is unique for this key. This
253 * unique key id may be larger than the user specified entry count
254 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
257 rte_hash_add_key(const struct rte_hash *h, const void *key);
260 * Add a key to an existing hash table.
261 * This operation is not multi-thread safe
262 * and should only be called from one thread by default.
263 * Thread safety can be enabled by setting flag during
267 * Hash table to add the key to.
269 * Key to add to the hash table.
271 * Precomputed hash value for 'key'.
273 * - -EINVAL if the parameters are invalid.
274 * - -ENOSPC if there is no space in the hash for this key.
275 * - A positive value that can be used by the caller as an offset into an
276 * array of user data. This value is unique for this key. This
277 * unique key ID may be larger than the user specified entry count
278 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
281 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
284 * Remove a key from an existing hash table.
285 * This operation is not multi-thread safe
286 * and should only be called from one thread by default.
287 * Thread safety can be enabled by setting flag during
289 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
290 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
291 * the key index returned by rte_hash_add_key_xxx APIs will not be
292 * freed by this API. rte_hash_free_key_with_position API must be called
293 * additionally to free the index associated with the key.
294 * rte_hash_free_key_with_position API should be called after all
295 * the readers have stopped referencing the entry corresponding to
296 * this key. RCU mechanisms could be used to determine such a state.
299 * Hash table to remove the key from.
301 * Key to remove from the hash table.
303 * - -EINVAL if the parameters are invalid.
304 * - -ENOENT if the key is not found.
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.
310 rte_hash_del_key(const struct rte_hash *h, const void *key);
313 * Remove a key from an existing hash table.
314 * This operation is not multi-thread safe
315 * and should only be called from one thread by default.
316 * Thread safety can be enabled by setting flag during
318 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
319 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
320 * the key index returned by rte_hash_add_key_xxx APIs will not be
321 * freed by this API. rte_hash_free_key_with_position API must be called
322 * additionally to free the index associated with the key.
323 * rte_hash_free_key_with_position API should be called after all
324 * the readers have stopped referencing the entry corresponding to
325 * this key. RCU mechanisms could be used to determine such a state.
328 * Hash table to remove the key from.
330 * Key to remove from the hash table.
332 * Precomputed hash value for 'key'.
334 * - -EINVAL if the parameters are invalid.
335 * - -ENOENT if the key is not found.
336 * - A positive value that can be used by the caller as an offset into an
337 * array of user data. This value is unique for this key, and is the same
338 * value that was returned when the key was added.
341 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
344 * Find a key in the hash table given the position.
345 * This operation is multi-thread safe with regarding to other lookup threads.
346 * Read-write concurrency can be enabled by setting flag during
350 * Hash table to get the key from.
352 * Position returned when the key was inserted.
354 * Output containing a pointer to the key
356 * - 0 if retrieved successfully
357 * - -EINVAL if the parameters are invalid.
358 * - -ENOENT if no valid key is found in the given position.
361 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
366 * @b EXPERIMENTAL: this API may change without prior notice
368 * Free a hash key in the hash table given the position
369 * of the key. This operation is not multi-thread safe and should
370 * only be called from one thread by default. Thread safety
371 * can be enabled by setting flag during table creation.
372 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
373 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
374 * the key index returned by rte_hash_del_key_xxx APIs must be freed
375 * using this API. This API should be called after all the readers
376 * have stopped referencing the entry corresponding to this key.
377 * RCU mechanisms could be used to determine such a state.
378 * This API does not validate if the key is already freed.
381 * Hash table to free the key from.
383 * Position returned when the key was deleted.
385 * - 0 if freed successfully
386 * - -EINVAL if the parameters are invalid.
390 rte_hash_free_key_with_position(const struct rte_hash *h,
391 const int32_t position);
394 * Find a key-value pair in the hash table.
395 * This operation is multi-thread safe with regarding to other lookup threads.
396 * Read-write concurrency can be enabled by setting flag during
400 * Hash table to look in.
404 * Output with pointer to data returned from the hash table.
406 * - A positive value that can be used by the caller as an offset into an
407 * array of user data. This value is unique for this key, and is the same
408 * value that was returned when the key was added.
409 * - -EINVAL if the parameters are invalid.
410 * - -ENOENT if the key is not found.
413 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
416 * Find a key-value pair with a pre-computed hash value
417 * to an existing hash table.
418 * This operation is multi-thread safe with regarding to other lookup threads.
419 * Read-write concurrency can be enabled by setting flag during
423 * Hash table to look in.
427 * Precomputed hash value for 'key'
429 * Output with pointer to data returned from the hash table.
431 * - A positive value that can be used by the caller as an offset into an
432 * array of user data. This value is unique for this key, and is the same
433 * value that was returned when the key was added.
434 * - -EINVAL if the parameters are invalid.
435 * - -ENOENT if the key is not found.
438 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
439 hash_sig_t sig, void **data);
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 * - -EINVAL if the parameters are invalid.
453 * - -ENOENT if the key is not found.
454 * - A positive value that can be used by the caller as an offset into an
455 * array of user data. This value is unique for this key, and is the same
456 * value that was returned when the key was added.
459 rte_hash_lookup(const struct rte_hash *h, const void *key);
462 * Find a key in the hash table.
463 * This operation is multi-thread safe with regarding to other lookup threads.
464 * Read-write concurrency can be enabled by setting flag during
468 * Hash table to look in.
472 * Precomputed hash value for 'key'.
474 * - -EINVAL if the parameters are invalid.
475 * - -ENOENT if the key is not found.
476 * - A positive value that can be used by the caller as an offset into an
477 * array of user data. This value is unique for this key, and is the same
478 * value that was returned when the key was added.
481 rte_hash_lookup_with_hash(const struct rte_hash *h,
482 const void *key, hash_sig_t sig);
485 * Calc a hash value by key.
486 * This operation is not multi-process safe.
489 * Hash table to look in.
496 rte_hash_hash(const struct rte_hash *h, const void *key);
499 * Find multiple keys in the hash table.
500 * This operation is multi-thread safe with regarding to other lookup threads.
501 * Read-write concurrency can be enabled by setting flag during
505 * Hash table to look in.
507 * A pointer to a list of keys to look for.
509 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
511 * Output containing a bitmask with all successful lookups.
513 * Output containing array of data returned from all the successful lookups.
515 * -EINVAL if there's an error, otherwise number of successful lookups.
518 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
519 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
523 * @b EXPERIMENTAL: this API may change without prior notice
525 * Find multiple keys in the hash table with precomputed hash value array.
526 * This operation is multi-thread safe with regarding to other lookup threads.
527 * Read-write concurrency can be enabled by setting flag during
531 * Hash table to look in.
533 * A pointer to a list of keys to look for.
535 * A pointer to a list of precomputed hash values for keys.
537 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
539 * Output containing a list of values, corresponding to the list of keys that
540 * can be used by the caller as an offset into an array of user data. These
541 * values are unique for each key, and are the same values that were returned
542 * when each key was added. If a key in the list was not found, then -ENOENT
545 * -EINVAL if there's an error, otherwise 0.
549 rte_hash_lookup_with_hash_bulk(const struct rte_hash *h, const void **keys,
550 hash_sig_t *sig, uint32_t num_keys, int32_t *positions);
554 * @b EXPERIMENTAL: this API may change without prior notice
556 * Find multiple keys in the hash table with precomputed hash value array.
557 * This operation is multi-thread safe with regarding to other lookup threads.
558 * Read-write concurrency can be enabled by setting flag during
562 * Hash table to look in.
564 * A pointer to a list of keys to look for.
566 * A pointer to a list of precomputed hash values for keys.
568 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
570 * Output containing a bitmask with all successful lookups.
572 * Output containing array of data returned from all the successful lookups.
574 * -EINVAL if there's an error, otherwise number of successful lookups.
578 rte_hash_lookup_with_hash_bulk_data(const struct rte_hash *h,
579 const void **keys, hash_sig_t *sig,
580 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
583 * Find multiple keys in the hash table.
584 * This operation is multi-thread safe with regarding to other lookup threads.
585 * Read-write concurrency can be enabled by setting flag during
589 * Hash table to look in.
591 * A pointer to a list of keys to look for.
593 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
595 * Output containing a list of values, corresponding to the list of keys that
596 * can be used by the caller as an offset into an array of user data. These
597 * values are unique for each key, and are the same values that were returned
598 * when each key was added. If a key in the list was not found, then -ENOENT
601 * -EINVAL if there's an error, otherwise 0.
604 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
605 uint32_t num_keys, int32_t *positions);
608 * Iterate through the hash table, returning key-value pairs.
611 * Hash table to iterate
613 * Output containing the key where current iterator
616 * Output containing the data associated with key.
617 * Returns NULL if data was not stored.
619 * Pointer to iterator. Should be 0 to start iterating the hash table.
620 * Iterator is incremented after each call of this function.
622 * Position where key was stored, if successful.
623 * - -EINVAL if the parameters are invalid.
624 * - -ENOENT if end of the hash table.
627 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
632 #endif /* _RTE_HASH_H_ */