1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
17 #include <rte_compat.h>
18 #include <rte_rcu_qsbr.h>
24 /** Maximum size of hash table that can be created. */
25 #define RTE_HASH_ENTRIES_MAX (1 << 30)
27 /** Maximum number of characters in hash name.*/
28 #define RTE_HASH_NAMESIZE 32
30 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
31 #define RTE_HASH_LOOKUP_BULK_MAX 64
32 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
34 /** Enable Hardware transactional memory support. */
35 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT 0x01
37 /** Default behavior of insertion, single writer/multi writer */
38 #define RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD 0x02
40 /** Flag to support reader writer concurrency */
41 #define RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY 0x04
43 /** Flag to indicate the extendable bucket table feature should be used */
44 #define RTE_HASH_EXTRA_FLAGS_EXT_TABLE 0x08
46 /** Flag to disable freeing of key index on hash delete.
47 * Refer to rte_hash_del_xxx APIs for more details.
48 * This is enabled by default when RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF
49 * is enabled. However, if internal RCU is enabled, freeing of internal
50 * memory/index is done on delete
52 #define RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL 0x10
54 /** Flag to support lock free reader writer concurrency. Both single writer
55 * and multi writer use cases are supported.
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 * Type of function used to free data stored in the key.
74 * Required when using internal RCU to allow application to free key-data once
75 * the key is returned to the ring of free key-slots.
77 typedef void (*rte_hash_free_key_data)(void *p, void *key_data);
80 * Parameters used when creating the hash table.
82 struct rte_hash_parameters {
83 const char *name; /**< Name of the hash. */
84 uint32_t entries; /**< Total hash table entries. */
85 uint32_t reserved; /**< Unused field. Should be set to 0 */
86 uint32_t key_len; /**< Length of hash key. */
87 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
88 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
89 int socket_id; /**< NUMA Socket ID for memory. */
90 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
93 /** RCU reclamation modes */
94 enum rte_hash_qsbr_mode {
95 /** Create defer queue for reclaim. */
96 RTE_HASH_QSBR_MODE_DQ = 0,
97 /** Use blocking mode reclaim. No defer queue created. */
98 RTE_HASH_QSBR_MODE_SYNC
101 /** HASH RCU QSBR configuration structure. */
102 struct rte_hash_rcu_config {
103 struct rte_rcu_qsbr *v; /**< RCU QSBR variable. */
104 enum rte_hash_qsbr_mode mode;
105 /**< Mode of RCU QSBR. RTE_HASH_QSBR_MODE_xxx
106 * '0' for default: create defer queue for reclaim.
109 /**< RCU defer queue size.
110 * default: total hash table entries.
112 uint32_t trigger_reclaim_limit; /**< Threshold to trigger auto reclaim. */
113 uint32_t max_reclaim_size;
114 /**< Max entries to reclaim in one go.
115 * default: RTE_HASH_RCU_DQ_RECLAIM_MAX.
118 /**< Pointer passed to the free function. Typically, this is the
119 * pointer to the data structure to which the resource to free
120 * (key-data) belongs. This can be NULL.
122 rte_hash_free_key_data free_key_data_func;
123 /**< Function to call to free the resource (key-data). */
126 /** @internal A hash table structure. */
130 * Create a new hash table.
133 * Parameters used to create and initialise the hash table.
135 * Pointer to hash table structure that is used in future hash table
136 * operations, or NULL on error, with error code set in rte_errno.
137 * Possible rte_errno errors include:
138 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
139 * - E_RTE_SECONDARY - function was called from a secondary process instance
140 * - ENOENT - missing entry
141 * - EINVAL - invalid parameter passed to function
142 * - ENOSPC - the maximum number of memzones has already been allocated
143 * - EEXIST - a memzone with the same name already exists
144 * - ENOMEM - no appropriate memory area found in which to create memzone
147 rte_hash_create(const struct rte_hash_parameters *params);
150 * Set a new hash compare function other than the default one.
152 * @note Function pointer does not work with multi-process, so do not use it
153 * in multi-process mode.
156 * Hash table for which the function is to be changed
158 * New compare function
160 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
163 * Find an existing hash table object and return a pointer to it.
166 * Name of the hash table as passed to rte_hash_create()
168 * Pointer to hash table or NULL if object not found
169 * with rte_errno set appropriately. Possible rte_errno values include:
170 * - ENOENT - value not available for return
173 rte_hash_find_existing(const char *name);
176 * De-allocate all memory used by hash table.
179 * Hash table to free, if NULL, the function does nothing.
183 rte_hash_free(struct rte_hash *h);
186 * Reset all hash structure, by zeroing all entries.
187 * When RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
188 * it is application's responsibility to make sure that
189 * none of the readers are referencing the hash table
190 * while calling this API.
193 * Hash table to reset
196 rte_hash_reset(struct rte_hash *h);
199 * Return the number of keys in the hash table
201 * Hash table to query from
203 * - -EINVAL if parameters are invalid
204 * - A value indicating how many keys were inserted in the table.
207 rte_hash_count(const struct rte_hash *h);
210 * Return the maximum key value ID that could possibly be returned by
211 * rte_hash_add_key function.
214 * Hash table to query from
216 * - -EINVAL if parameters are invalid
217 * - A value indicating the max key ID of key slots present in the table.
220 rte_hash_max_key_id(const struct rte_hash *h);
223 * Add a key-value pair to an existing hash table.
224 * 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
228 * If the key exists already in the table, this API updates its value
229 * with 'data' passed in this API. It is the responsibility of
230 * the application to manage any memory associated with the old value.
231 * The readers might still be using the old value even after this API
235 * Hash table to add the key to.
237 * Key to add to the hash table.
239 * Data to add to the hash table.
241 * - 0 if added successfully
242 * - -EINVAL if the parameters are invalid.
243 * - -ENOSPC if there is no space in the hash for this key.
246 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
249 * Add a key-value pair with a pre-computed hash value
250 * to an existing hash table.
251 * This operation is not multi-thread safe
252 * and should only be called from one thread by default.
253 * Thread safety can be enabled by setting flag during
255 * If the key exists already in the table, this API updates its value
256 * with 'data' passed in this API. It is the responsibility of
257 * the application to manage any memory associated with the old value.
258 * The readers might still be using the old value even after this API
262 * Hash table to add the key to.
264 * Key to add to the hash table.
266 * Precomputed hash value for 'key'
268 * Data to add to the hash table.
270 * - 0 if added successfully
271 * - -EINVAL if the parameters are invalid.
272 * - -ENOSPC if there is no space in the hash for this key.
275 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
276 hash_sig_t sig, void *data);
279 * Add a key to an existing hash table. This operation is not multi-thread safe
280 * and should only be called from one thread by default.
281 * Thread safety can be enabled by setting flag during
285 * Hash table to add the key to.
287 * Key to add to the hash table.
289 * - -EINVAL if the parameters are invalid.
290 * - -ENOSPC if there is no space in the hash for this key.
291 * - A positive value that can be used by the caller as an offset into an
292 * array of user data. This value is unique for this key. This
293 * unique key id may be larger than the user specified entry count
294 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
297 rte_hash_add_key(const struct rte_hash *h, const void *key);
300 * Add a key to an existing hash table.
301 * This operation is not multi-thread safe
302 * and should only be called from one thread by default.
303 * Thread safety can be enabled by setting flag during
307 * Hash table to add the key to.
309 * Key to add to the hash table.
311 * Precomputed hash value for 'key'.
313 * - -EINVAL if the parameters are invalid.
314 * - -ENOSPC if there is no space in the hash for this key.
315 * - A positive value that can be used by the caller as an offset into an
316 * array of user data. This value is unique for this key. This
317 * unique key ID may be larger than the user specified entry count
318 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
321 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
324 * Remove a key from an existing hash table.
325 * This operation is not multi-thread safe
326 * and should only be called from one thread by default.
327 * Thread safety can be enabled by setting flag during
329 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
330 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled and
331 * internal RCU is NOT enabled,
332 * the key index returned by rte_hash_add_key_xxx APIs will not be
333 * freed by this API. rte_hash_free_key_with_position API must be called
334 * additionally to free the index associated with the key.
335 * rte_hash_free_key_with_position API should be called after all
336 * the readers have stopped referencing the entry corresponding to
337 * this key. RCU mechanisms could be used to determine such a state.
340 * Hash table to remove the key from.
342 * Key to remove from the hash table.
344 * - -EINVAL if the parameters are invalid.
345 * - -ENOENT if the key is not found.
346 * - A positive value that can be used by the caller as an offset into an
347 * array of user data. This value is unique for this key, and is the same
348 * value that was returned when the key was added.
351 rte_hash_del_key(const struct rte_hash *h, const void *key);
354 * Remove a key from an existing hash table.
355 * This operation is not multi-thread safe
356 * and should only be called from one thread by default.
357 * Thread safety can be enabled by setting flag during
359 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
360 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled and
361 * internal RCU is NOT enabled,
362 * the key index returned by rte_hash_add_key_xxx APIs will not be
363 * freed by this API. rte_hash_free_key_with_position API must be called
364 * additionally to free the index associated with the key.
365 * rte_hash_free_key_with_position API should be called after all
366 * the readers have stopped referencing the entry corresponding to
367 * this key. RCU mechanisms could be used to determine such a state.
370 * Hash table to remove the key from.
372 * Key to remove from the hash table.
374 * Precomputed hash value for 'key'.
376 * - -EINVAL if the parameters are invalid.
377 * - -ENOENT if the key is not found.
378 * - A positive value that can be used by the caller as an offset into an
379 * array of user data. This value is unique for this key, and is the same
380 * value that was returned when the key was added.
383 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
386 * Find a key in the hash table given the position.
387 * This operation is multi-thread safe with regarding to other lookup threads.
388 * Read-write concurrency can be enabled by setting flag during
392 * Hash table to get the key from.
394 * Position returned when the key was inserted.
396 * Output containing a pointer to the key
398 * - 0 if retrieved successfully
399 * - -EINVAL if the parameters are invalid.
400 * - -ENOENT if no valid key is found in the given position.
403 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
407 * Free a hash key in the hash table given the position
408 * of the key. This operation is not multi-thread safe and should
409 * only be called from one thread by default. Thread safety
410 * can be enabled by setting flag during table creation.
411 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
412 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled and
413 * internal RCU is NOT enabled,
414 * the key index returned by rte_hash_del_key_xxx APIs must be freed
415 * using this API. This API should be called after all the readers
416 * have stopped referencing the entry corresponding to this key.
417 * RCU mechanisms could be used to determine such a state.
418 * This API does not validate if the key is already freed.
421 * Hash table to free the key from.
423 * Position returned when the key was deleted.
425 * - 0 if freed successfully
426 * - -EINVAL if the parameters are invalid.
429 rte_hash_free_key_with_position(const struct rte_hash *h,
430 const int32_t position);
433 * Find a key-value pair in the hash table.
434 * This operation is multi-thread safe with regarding to other lookup threads.
435 * Read-write concurrency can be enabled by setting flag during
439 * Hash table to look in.
443 * Output with pointer to data returned from the hash table.
445 * - A positive value that can be used by the caller as an offset into an
446 * array of user data. This value is unique for this key, and is the same
447 * value that was returned when the key was added.
448 * - -EINVAL if the parameters are invalid.
449 * - -ENOENT if the key is not found.
452 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
455 * Find a key-value pair with a pre-computed hash value
456 * to an existing hash table.
457 * This operation is multi-thread safe with regarding to other lookup threads.
458 * Read-write concurrency can be enabled by setting flag during
462 * Hash table to look in.
466 * Precomputed hash value for 'key'
468 * Output with pointer to data returned from the hash table.
470 * - A positive value that can be used by the caller as an offset into an
471 * array of user data. This value is unique for this key, and is the same
472 * value that was returned when the key was added.
473 * - -EINVAL if the parameters are invalid.
474 * - -ENOENT if the key is not found.
477 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
478 hash_sig_t sig, void **data);
481 * Find a key in the hash table.
482 * This operation is multi-thread safe with regarding to other lookup threads.
483 * Read-write concurrency can be enabled by setting flag during
487 * Hash table to look in.
491 * - -EINVAL if the parameters are invalid.
492 * - -ENOENT if the key is not found.
493 * - A positive value that can be used by the caller as an offset into an
494 * array of user data. This value is unique for this key, and is the same
495 * value that was returned when the key was added.
498 rte_hash_lookup(const struct rte_hash *h, const void *key);
501 * Find a key 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.
511 * Precomputed hash value for 'key'.
513 * - -EINVAL if the parameters are invalid.
514 * - -ENOENT if the key is not found.
515 * - A positive value that can be used by the caller as an offset into an
516 * array of user data. This value is unique for this key, and is the same
517 * value that was returned when the key was added.
520 rte_hash_lookup_with_hash(const struct rte_hash *h,
521 const void *key, hash_sig_t sig);
524 * Calc a hash value by key.
525 * This operation is not multi-process safe.
528 * Hash table to look in.
535 rte_hash_hash(const struct rte_hash *h, const void *key);
538 * Find multiple keys in the hash table.
539 * This operation is multi-thread safe with regarding to other lookup threads.
540 * Read-write concurrency can be enabled by setting flag during
544 * Hash table to look in.
546 * A pointer to a list of keys to look for.
548 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
550 * Output containing a bitmask with all successful lookups.
552 * Output containing array of data returned from all the successful lookups.
554 * -EINVAL if there's an error, otherwise number of successful lookups.
557 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
558 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
561 * Find multiple keys in the hash table with precomputed hash value array.
562 * This operation is multi-thread safe with regarding to other lookup threads.
563 * Read-write concurrency can be enabled by setting flag during
567 * Hash table to look in.
569 * A pointer to a list of keys to look for.
571 * A pointer to a list of precomputed hash values for keys.
573 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
575 * Output containing a list of values, corresponding to the list of keys that
576 * can be used by the caller as an offset into an array of user data. These
577 * values are unique for each key, and are the same values that were returned
578 * when each key was added. If a key in the list was not found, then -ENOENT
581 * -EINVAL if there's an error, otherwise 0.
584 rte_hash_lookup_with_hash_bulk(const struct rte_hash *h, const void **keys,
585 hash_sig_t *sig, uint32_t num_keys, int32_t *positions);
588 * Find multiple keys in the hash table with precomputed hash value array.
589 * This operation is multi-thread safe with regarding to other lookup threads.
590 * Read-write concurrency can be enabled by setting flag during
594 * Hash table to look in.
596 * A pointer to a list of keys to look for.
598 * A pointer to a list of precomputed hash values for keys.
600 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
602 * Output containing a bitmask with all successful lookups.
604 * Output containing array of data returned from all the successful lookups.
606 * -EINVAL if there's an error, otherwise number of successful lookups.
609 rte_hash_lookup_with_hash_bulk_data(const struct rte_hash *h,
610 const void **keys, hash_sig_t *sig,
611 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
614 * Find multiple keys in the hash table.
615 * This operation is multi-thread safe with regarding to other lookup threads.
616 * Read-write concurrency can be enabled by setting flag during
620 * Hash table to look in.
622 * A pointer to a list of keys to look for.
624 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
626 * Output containing a list of values, corresponding to the list of keys that
627 * can be used by the caller as an offset into an array of user data. These
628 * values are unique for each key, and are the same values that were returned
629 * when each key was added. If a key in the list was not found, then -ENOENT
632 * -EINVAL if there's an error, otherwise 0.
635 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
636 uint32_t num_keys, int32_t *positions);
639 * Iterate through the hash table, returning key-value pairs.
642 * Hash table to iterate
644 * Output containing the key where current iterator
647 * Output containing the data associated with key.
648 * Returns NULL if data was not stored.
650 * Pointer to iterator. Should be 0 to start iterating the hash table.
651 * Iterator is incremented after each call of this function.
653 * Position where key was stored, if successful.
654 * - -EINVAL if the parameters are invalid.
655 * - -ENOENT if end of the hash table.
658 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
661 * Associate RCU QSBR variable with a Hash object.
662 * This API should be called to enable the integrated RCU QSBR support and
663 * should be called immediately after creating the Hash object.
666 * the hash object to add RCU QSBR
668 * RCU QSBR configuration
671 * On error - 1 with error code set in rte_errno.
672 * Possible rte_errno codes are:
673 * - EINVAL - invalid pointer
674 * - EEXIST - already added QSBR
675 * - ENOMEM - memory allocation failure
677 int rte_hash_rcu_qsbr_add(struct rte_hash *h, struct rte_hash_rcu_config *cfg);
683 #endif /* _RTE_HASH_H_ */