1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
21 /** Maximum size of hash table that can be created. */
22 #define RTE_HASH_ENTRIES_MAX (1 << 30)
24 /** Maximum number of characters in hash name.*/
25 #define RTE_HASH_NAMESIZE 32
27 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
28 #define RTE_HASH_LOOKUP_BULK_MAX 64
29 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
31 /** Enable Hardware transactional memory support. */
32 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT 0x01
34 /** Default behavior of insertion, single writer/multi writer */
35 #define RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD 0x02
37 /** Flag to support reader writer concurrency */
38 #define RTE_HASH_EXTRA_FLAGS_RW_CONCURRENCY 0x04
40 /** Signature of key that is stored internally. */
41 typedef uint32_t hash_sig_t;
43 /** Type of function that can be used for calculating the hash value. */
44 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
47 /** Type of function used to compare the hash key. */
48 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
51 * Parameters used when creating the hash table.
53 struct rte_hash_parameters {
54 const char *name; /**< Name of the hash. */
55 uint32_t entries; /**< Total hash table entries. */
56 uint32_t reserved; /**< Unused field. Should be set to 0 */
57 uint32_t key_len; /**< Length of hash key. */
58 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
59 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
60 int socket_id; /**< NUMA Socket ID for memory. */
61 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
64 /** @internal A hash table structure. */
68 * Create a new hash table.
71 * Parameters used to create and initialise the hash table.
73 * Pointer to hash table structure that is used in future hash table
74 * operations, or NULL on error, with error code set in rte_errno.
75 * Possible rte_errno errors include:
76 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
77 * - E_RTE_SECONDARY - function was called from a secondary process instance
78 * - ENOENT - missing entry
79 * - EINVAL - invalid parameter passed to function
80 * - ENOSPC - the maximum number of memzones has already been allocated
81 * - EEXIST - a memzone with the same name already exists
82 * - ENOMEM - no appropriate memory area found in which to create memzone
85 rte_hash_create(const struct rte_hash_parameters *params);
88 * Set a new hash compare function other than the default one.
90 * @note Function pointer does not work with multi-process, so do not use it
91 * in multi-process mode.
94 * Hash table for which the function is to be changed
96 * New compare function
98 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
101 * Find an existing hash table object and return a pointer to it.
104 * Name of the hash table as passed to rte_hash_create()
106 * Pointer to hash table or NULL if object not found
107 * with rte_errno set appropriately. Possible rte_errno values include:
108 * - ENOENT - value not available for return
111 rte_hash_find_existing(const char *name);
114 * De-allocate all memory used by hash table.
119 rte_hash_free(struct rte_hash *h);
122 * Reset all hash structure, by zeroing all entries
124 * Hash table to reset
127 rte_hash_reset(struct rte_hash *h);
130 * Return the number of keys in the hash table
132 * Hash table to query from
134 * - -EINVAL if parameters are invalid
135 * - A value indicating how many keys were inserted in the table.
138 rte_hash_count(const struct rte_hash *h);
141 * Add a key-value pair to an existing hash table.
142 * This operation is not multi-thread safe
143 * and should only be called from one thread by default.
144 * Thread safety can be enabled by setting flag during
148 * Hash table to add the key to.
150 * Key to add to the hash table.
152 * Data to add to the hash table.
154 * - 0 if added successfully
155 * - -EINVAL if the parameters are invalid.
156 * - -ENOSPC if there is no space in the hash for this key.
159 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
162 * Add a key-value pair with a pre-computed hash value
163 * to an existing hash table.
164 * This operation is not multi-thread safe
165 * and should only be called from one thread by default.
166 * Thread safety can be enabled by setting flag during
170 * Hash table to add the key to.
172 * Key to add to the hash table.
174 * Precomputed hash value for 'key'
176 * Data to add to the hash table.
178 * - 0 if added successfully
179 * - -EINVAL if the parameters are invalid.
180 * - -ENOSPC if there is no space in the hash for this key.
183 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
184 hash_sig_t sig, void *data);
187 * Add a key to an existing hash table. This operation is not multi-thread safe
188 * and should only be called from one thread by default.
189 * Thread safety can be enabled by setting flag during
193 * Hash table to add the key to.
195 * Key to add to the hash table.
197 * - -EINVAL if the parameters are invalid.
198 * - -ENOSPC if there is no space in the hash for this key.
199 * - A positive value that can be used by the caller as an offset into an
200 * array of user data. This value is unique for this key.
203 rte_hash_add_key(const struct rte_hash *h, const void *key);
206 * Add a key to an existing hash table.
207 * This operation is not multi-thread safe
208 * and should only be called from one thread by default.
209 * Thread safety can be enabled by setting flag during
213 * Hash table to add the key to.
215 * Key to add to the hash table.
217 * Precomputed hash value for 'key'.
219 * - -EINVAL if the parameters are invalid.
220 * - -ENOSPC if there is no space in the hash for this key.
221 * - A positive value that can be used by the caller as an offset into an
222 * array of user data. This value is unique for this key.
225 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
228 * Remove a key from an existing hash table.
229 * This operation is not multi-thread safe
230 * and should only be called from one thread by default.
231 * Thread safety can be enabled by setting flag during
235 * Hash table to remove the key from.
237 * Key to remove from the hash table.
239 * - -EINVAL if the parameters are invalid.
240 * - -ENOENT if the key is not found.
241 * - A positive value that can be used by the caller as an offset into an
242 * array of user data. This value is unique for this key, and is the same
243 * value that was returned when the key was added.
246 rte_hash_del_key(const struct rte_hash *h, const void *key);
249 * Remove a key from an existing hash table.
250 * This operation is not multi-thread safe
251 * and should only be called from one thread by default.
252 * Thread safety can be enabled by setting flag during
256 * Hash table to remove the key from.
258 * Key to remove from the hash table.
260 * Precomputed hash value for 'key'.
262 * - -EINVAL if the parameters are invalid.
263 * - -ENOENT if the key is not found.
264 * - A positive value that can be used by the caller as an offset into an
265 * array of user data. This value is unique for this key, and is the same
266 * value that was returned when the key was added.
269 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
272 * Find a key in the hash table given the position.
273 * This operation is multi-thread safe with regarding to other lookup threads.
274 * Read-write concurrency can be enabled by setting flag during
278 * Hash table to get the key from.
280 * Position returned when the key was inserted.
282 * Output containing a pointer to the key
284 * - 0 if retrieved successfully
285 * - -EINVAL if the parameters are invalid.
286 * - -ENOENT if no valid key is found in the given position.
289 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
293 * Find a key-value pair in the hash table.
294 * This operation is multi-thread safe with regarding to other lookup threads.
295 * Read-write concurrency can be enabled by setting flag during
299 * Hash table to look in.
303 * Output with pointer to data returned from the hash table.
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.
308 * - -EINVAL if the parameters are invalid.
309 * - -ENOENT if the key is not found.
312 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
315 * Find a key-value pair with a pre-computed hash value
316 * to an existing hash table.
317 * This operation is multi-thread safe with regarding to other lookup threads.
318 * Read-write concurrency can be enabled by setting flag during
322 * Hash table to look in.
326 * Precomputed hash value for 'key'
328 * Output with pointer to data returned from the hash table.
330 * - A positive value that can be used by the caller as an offset into an
331 * array of user data. This value is unique for this key, and is the same
332 * value that was returned when the key was added.
333 * - -EINVAL if the parameters are invalid.
334 * - -ENOENT if the key is not found.
337 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
338 hash_sig_t sig, void **data);
341 * Find a key in the hash table.
342 * This operation is multi-thread safe with regarding to other lookup threads.
343 * Read-write concurrency can be enabled by setting flag during
347 * Hash table to look in.
351 * - -EINVAL if the parameters are invalid.
352 * - -ENOENT if the key is not found.
353 * - A positive value that can be used by the caller as an offset into an
354 * array of user data. This value is unique for this key, and is the same
355 * value that was returned when the key was added.
358 rte_hash_lookup(const struct rte_hash *h, const void *key);
361 * Find a key in the hash table.
362 * This operation is multi-thread safe with regarding to other lookup threads.
363 * Read-write concurrency can be enabled by setting flag during
367 * Hash table to look in.
371 * Precomputed hash value for 'key'.
373 * - -EINVAL if the parameters are invalid.
374 * - -ENOENT if the key is not found.
375 * - A positive value that can be used by the caller as an offset into an
376 * array of user data. This value is unique for this key, and is the same
377 * value that was returned when the key was added.
380 rte_hash_lookup_with_hash(const struct rte_hash *h,
381 const void *key, hash_sig_t sig);
384 * Calc a hash value by key.
385 * This operation is not multi-thread safe.
388 * Hash table to look in.
395 rte_hash_hash(const struct rte_hash *h, const void *key);
398 * Find multiple keys in the hash table.
399 * This operation is multi-thread safe with regarding to other lookup threads.
400 * Read-write concurrency can be enabled by setting flag during
404 * Hash table to look in.
406 * A pointer to a list of keys to look for.
408 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
410 * Output containing a bitmask with all successful lookups.
412 * Output containing array of data returned from all the successful lookups.
414 * -EINVAL if there's an error, otherwise number of successful lookups.
417 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
418 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
421 * Find multiple keys in the hash table.
422 * This operation is multi-thread safe with regarding to other lookup threads.
423 * Read-write concurrency can be enabled by setting flag during
427 * Hash table to look in.
429 * A pointer to a list of keys to look for.
431 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
433 * Output containing a list of values, corresponding to the list of keys that
434 * can be used by the caller as an offset into an array of user data. These
435 * values are unique for each key, and are the same values that were returned
436 * when each key was added. If a key in the list was not found, then -ENOENT
439 * -EINVAL if there's an error, otherwise 0.
442 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
443 uint32_t num_keys, int32_t *positions);
446 * Iterate through the hash table, returning key-value pairs.
449 * Hash table to iterate
451 * Output containing the key where current iterator
454 * Output containing the data associated with key.
455 * Returns NULL if data was not stored.
457 * Pointer to iterator. Should be 0 to start iterating the hash table.
458 * Iterator is incremented after each call of this function.
460 * Position where key was stored, if successful.
461 * - -EINVAL if the parameters are invalid.
462 * - -ENOENT if end of the hash table.
465 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
470 #endif /* _RTE_HASH_H_ */