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.
181 rte_hash_free(struct rte_hash *h);
184 * Reset all hash structure, by zeroing all entries.
185 * When RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled,
186 * it is application's responsibility to make sure that
187 * none of the readers are referencing the hash table
188 * while calling this API.
191 * Hash table to reset
194 rte_hash_reset(struct rte_hash *h);
197 * Return the number of keys in the hash table
199 * Hash table to query from
201 * - -EINVAL if parameters are invalid
202 * - A value indicating how many keys were inserted in the table.
205 rte_hash_count(const struct rte_hash *h);
208 * Return the maximum key value ID that could possibly be returned by
209 * rte_hash_add_key function.
212 * Hash table to query from
214 * - -EINVAL if parameters are invalid
215 * - A value indicating the max key ID of key slots present in the table.
218 rte_hash_max_key_id(const struct rte_hash *h);
221 * Add a key-value pair to an existing hash table.
222 * This operation is not multi-thread safe
223 * and should only be called from one thread by default.
224 * Thread safety can be enabled by setting flag during
226 * If the key exists already in the table, this API updates its value
227 * with 'data' passed in this API. It is the responsibility of
228 * the application to manage any memory associated with the old value.
229 * The readers might still be using the old value even after this API
233 * Hash table to add the key to.
235 * Key to add to the hash table.
237 * Data to add to the hash table.
239 * - 0 if added successfully
240 * - -EINVAL if the parameters are invalid.
241 * - -ENOSPC if there is no space in the hash for this key.
244 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
247 * Add a key-value pair with a pre-computed hash value
248 * to an existing hash table.
249 * This operation is not multi-thread safe
250 * and should only be called from one thread by default.
251 * Thread safety can be enabled by setting flag during
253 * If the key exists already in the table, this API updates its value
254 * with 'data' passed in this API. It is the responsibility of
255 * the application to manage any memory associated with the old value.
256 * The readers might still be using the old value even after this API
260 * Hash table to add the key to.
262 * Key to add to the hash table.
264 * Precomputed hash value for 'key'
266 * Data to add to the hash table.
268 * - 0 if added successfully
269 * - -EINVAL if the parameters are invalid.
270 * - -ENOSPC if there is no space in the hash for this key.
273 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
274 hash_sig_t sig, void *data);
277 * Add a key to an existing hash table. This operation is not multi-thread safe
278 * and should only be called from one thread by default.
279 * Thread safety can be enabled by setting flag during
283 * Hash table to add the key to.
285 * Key to add to the hash table.
287 * - -EINVAL if the parameters are invalid.
288 * - -ENOSPC if there is no space in the hash for this key.
289 * - A positive value that can be used by the caller as an offset into an
290 * array of user data. This value is unique for this key. This
291 * unique key id may be larger than the user specified entry count
292 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
295 rte_hash_add_key(const struct rte_hash *h, const void *key);
298 * Add a key to an existing hash table.
299 * This operation is not multi-thread safe
300 * and should only be called from one thread by default.
301 * Thread safety can be enabled by setting flag during
305 * Hash table to add the key to.
307 * Key to add to the hash table.
309 * Precomputed hash value for 'key'.
311 * - -EINVAL if the parameters are invalid.
312 * - -ENOSPC if there is no space in the hash for this key.
313 * - A positive value that can be used by the caller as an offset into an
314 * array of user data. This value is unique for this key. This
315 * unique key ID may be larger than the user specified entry count
316 * when RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD flag is set.
319 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
322 * Remove a key from an existing hash table.
323 * This operation is not multi-thread safe
324 * and should only be called from one thread by default.
325 * Thread safety can be enabled by setting flag during
327 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
328 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled and
329 * internal RCU is NOT enabled,
330 * the key index returned by rte_hash_add_key_xxx APIs will not be
331 * freed by this API. rte_hash_free_key_with_position API must be called
332 * additionally to free the index associated with the key.
333 * rte_hash_free_key_with_position API should be called after all
334 * the readers have stopped referencing the entry corresponding to
335 * this key. RCU mechanisms could be used to determine such a state.
338 * Hash table to remove the key from.
340 * Key to remove from the hash table.
342 * - -EINVAL if the parameters are invalid.
343 * - -ENOENT if the key is not found.
344 * - A positive value that can be used by the caller as an offset into an
345 * array of user data. This value is unique for this key, and is the same
346 * value that was returned when the key was added.
349 rte_hash_del_key(const struct rte_hash *h, const void *key);
352 * Remove a key from an existing hash table.
353 * This operation is not multi-thread safe
354 * and should only be called from one thread by default.
355 * Thread safety can be enabled by setting flag during
357 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
358 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled and
359 * internal RCU is NOT enabled,
360 * the key index returned by rte_hash_add_key_xxx APIs will not be
361 * freed by this API. rte_hash_free_key_with_position API must be called
362 * additionally to free the index associated with the key.
363 * rte_hash_free_key_with_position API should be called after all
364 * the readers have stopped referencing the entry corresponding to
365 * this key. RCU mechanisms could be used to determine such a state.
368 * Hash table to remove the key from.
370 * Key to remove from the hash table.
372 * Precomputed hash value for 'key'.
374 * - -EINVAL if the parameters are invalid.
375 * - -ENOENT if the key is not found.
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.
381 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
384 * Find a key in the hash table given the position.
385 * This operation is multi-thread safe with regarding to other lookup threads.
386 * Read-write concurrency can be enabled by setting flag during
390 * Hash table to get the key from.
392 * Position returned when the key was inserted.
394 * Output containing a pointer to the key
396 * - 0 if retrieved successfully
397 * - -EINVAL if the parameters are invalid.
398 * - -ENOENT if no valid key is found in the given position.
401 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
405 * Free a hash key in the hash table given the position
406 * of the key. This operation is not multi-thread safe and should
407 * only be called from one thread by default. Thread safety
408 * can be enabled by setting flag during table creation.
409 * If RTE_HASH_EXTRA_FLAGS_NO_FREE_ON_DEL or
410 * RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY_LF is enabled and
411 * internal RCU is NOT enabled,
412 * the key index returned by rte_hash_del_key_xxx APIs must be freed
413 * using this API. This API should be called after all the readers
414 * have stopped referencing the entry corresponding to this key.
415 * RCU mechanisms could be used to determine such a state.
416 * This API does not validate if the key is already freed.
419 * Hash table to free the key from.
421 * Position returned when the key was deleted.
423 * - 0 if freed successfully
424 * - -EINVAL if the parameters are invalid.
427 rte_hash_free_key_with_position(const struct rte_hash *h,
428 const int32_t position);
431 * Find a key-value pair in the hash table.
432 * This operation is multi-thread safe with regarding to other lookup threads.
433 * Read-write concurrency can be enabled by setting flag during
437 * Hash table to look in.
441 * Output with pointer to data returned from the hash table.
443 * - A positive value that can be used by the caller as an offset into an
444 * array of user data. This value is unique for this key, and is the same
445 * value that was returned when the key was added.
446 * - -EINVAL if the parameters are invalid.
447 * - -ENOENT if the key is not found.
450 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
453 * Find a key-value pair with a pre-computed hash value
454 * to an existing hash table.
455 * This operation is multi-thread safe with regarding to other lookup threads.
456 * Read-write concurrency can be enabled by setting flag during
460 * Hash table to look in.
464 * Precomputed hash value for 'key'
466 * Output with pointer to data returned from the hash table.
468 * - A positive value that can be used by the caller as an offset into an
469 * array of user data. This value is unique for this key, and is the same
470 * value that was returned when the key was added.
471 * - -EINVAL if the parameters are invalid.
472 * - -ENOENT if the key is not found.
475 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
476 hash_sig_t sig, void **data);
479 * Find a key 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.
489 * - -EINVAL if the parameters are invalid.
490 * - -ENOENT if the key is not found.
491 * - A positive value that can be used by the caller as an offset into an
492 * array of user data. This value is unique for this key, and is the same
493 * value that was returned when the key was added.
496 rte_hash_lookup(const struct rte_hash *h, const void *key);
499 * Find a key 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.
509 * Precomputed hash value for 'key'.
511 * - -EINVAL if the parameters are invalid.
512 * - -ENOENT if the key is not found.
513 * - A positive value that can be used by the caller as an offset into an
514 * array of user data. This value is unique for this key, and is the same
515 * value that was returned when the key was added.
518 rte_hash_lookup_with_hash(const struct rte_hash *h,
519 const void *key, hash_sig_t sig);
522 * Calc a hash value by key.
523 * This operation is not multi-process safe.
526 * Hash table to look in.
533 rte_hash_hash(const struct rte_hash *h, const void *key);
536 * Find multiple keys in the hash table.
537 * This operation is multi-thread safe with regarding to other lookup threads.
538 * Read-write concurrency can be enabled by setting flag during
542 * Hash table to look in.
544 * A pointer to a list of keys to look for.
546 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
548 * Output containing a bitmask with all successful lookups.
550 * Output containing array of data returned from all the successful lookups.
552 * -EINVAL if there's an error, otherwise number of successful lookups.
555 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
556 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
559 * Find multiple keys in the hash table with precomputed hash value array.
560 * This operation is multi-thread safe with regarding to other lookup threads.
561 * Read-write concurrency can be enabled by setting flag during
565 * Hash table to look in.
567 * A pointer to a list of keys to look for.
569 * A pointer to a list of precomputed hash values for keys.
571 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
573 * Output containing a list of values, corresponding to the list of keys that
574 * can be used by the caller as an offset into an array of user data. These
575 * values are unique for each key, and are the same values that were returned
576 * when each key was added. If a key in the list was not found, then -ENOENT
579 * -EINVAL if there's an error, otherwise 0.
582 rte_hash_lookup_with_hash_bulk(const struct rte_hash *h, const void **keys,
583 hash_sig_t *sig, uint32_t num_keys, int32_t *positions);
586 * Find multiple keys in the hash table with precomputed hash value array.
587 * This operation is multi-thread safe with regarding to other lookup threads.
588 * Read-write concurrency can be enabled by setting flag during
592 * Hash table to look in.
594 * A pointer to a list of keys to look for.
596 * A pointer to a list of precomputed hash values for keys.
598 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
600 * Output containing a bitmask with all successful lookups.
602 * Output containing array of data returned from all the successful lookups.
604 * -EINVAL if there's an error, otherwise number of successful lookups.
607 rte_hash_lookup_with_hash_bulk_data(const struct rte_hash *h,
608 const void **keys, hash_sig_t *sig,
609 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
612 * Find multiple keys in the hash table.
613 * This operation is multi-thread safe with regarding to other lookup threads.
614 * Read-write concurrency can be enabled by setting flag during
618 * Hash table to look in.
620 * A pointer to a list of keys to look for.
622 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
624 * Output containing a list of values, corresponding to the list of keys that
625 * can be used by the caller as an offset into an array of user data. These
626 * values are unique for each key, and are the same values that were returned
627 * when each key was added. If a key in the list was not found, then -ENOENT
630 * -EINVAL if there's an error, otherwise 0.
633 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
634 uint32_t num_keys, int32_t *positions);
637 * Iterate through the hash table, returning key-value pairs.
640 * Hash table to iterate
642 * Output containing the key where current iterator
645 * Output containing the data associated with key.
646 * Returns NULL if data was not stored.
648 * Pointer to iterator. Should be 0 to start iterating the hash table.
649 * Iterator is incremented after each call of this function.
651 * Position where key was stored, if successful.
652 * - -EINVAL if the parameters are invalid.
653 * - -ENOENT if end of the hash table.
656 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
659 * Associate RCU QSBR variable with a Hash object.
660 * This API should be called to enable the integrated RCU QSBR support and
661 * should be called immediately after creating the Hash object.
664 * the hash object to add RCU QSBR
666 * RCU QSBR configuration
669 * On error - 1 with error code set in rte_errno.
670 * Possible rte_errno codes are:
671 * - EINVAL - invalid pointer
672 * - EEXIST - already added QSBR
673 * - ENOMEM - memory allocation failure
675 int rte_hash_rcu_qsbr_add(struct rte_hash *h, struct rte_hash_rcu_config *cfg);
681 #endif /* _RTE_HASH_H_ */