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 /** Flag to indicate the extendabe bucket table feature should be used */
41 #define RTE_HASH_EXTRA_FLAGS_EXT_TABLE 0x08
43 /** Signature of key that is stored internally. */
44 typedef uint32_t hash_sig_t;
46 /** Type of function that can be used for calculating the hash value. */
47 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
50 /** Type of function used to compare the hash key. */
51 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
54 * Parameters used when creating the hash table.
56 struct rte_hash_parameters {
57 const char *name; /**< Name of the hash. */
58 uint32_t entries; /**< Total hash table entries. */
59 uint32_t reserved; /**< Unused field. Should be set to 0 */
60 uint32_t key_len; /**< Length of hash key. */
61 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
62 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
63 int socket_id; /**< NUMA Socket ID for memory. */
64 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
67 /** @internal A hash table structure. */
71 * Create a new hash table.
74 * Parameters used to create and initialise the hash table.
76 * Pointer to hash table structure that is used in future hash table
77 * operations, or NULL on error, with error code set in rte_errno.
78 * Possible rte_errno errors include:
79 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
80 * - E_RTE_SECONDARY - function was called from a secondary process instance
81 * - ENOENT - missing entry
82 * - EINVAL - invalid parameter passed to function
83 * - ENOSPC - the maximum number of memzones has already been allocated
84 * - EEXIST - a memzone with the same name already exists
85 * - ENOMEM - no appropriate memory area found in which to create memzone
88 rte_hash_create(const struct rte_hash_parameters *params);
91 * Set a new hash compare function other than the default one.
93 * @note Function pointer does not work with multi-process, so do not use it
94 * in multi-process mode.
97 * Hash table for which the function is to be changed
99 * New compare function
101 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
104 * Find an existing hash table object and return a pointer to it.
107 * Name of the hash table as passed to rte_hash_create()
109 * Pointer to hash table or NULL if object not found
110 * with rte_errno set appropriately. Possible rte_errno values include:
111 * - ENOENT - value not available for return
114 rte_hash_find_existing(const char *name);
117 * De-allocate all memory used by hash table.
122 rte_hash_free(struct rte_hash *h);
125 * Reset all hash structure, by zeroing all entries
127 * Hash table to reset
130 rte_hash_reset(struct rte_hash *h);
133 * Return the number of keys in the hash table
135 * Hash table to query from
137 * - -EINVAL if parameters are invalid
138 * - A value indicating how many keys were inserted in the table.
141 rte_hash_count(const struct rte_hash *h);
144 * Add a key-value pair to an existing hash table.
145 * This operation is not multi-thread safe
146 * and should only be called from one thread by default.
147 * Thread safety can be enabled by setting flag during
151 * Hash table to add the key to.
153 * Key to add to the hash table.
155 * Data to add to the hash table.
157 * - 0 if added successfully
158 * - -EINVAL if the parameters are invalid.
159 * - -ENOSPC if there is no space in the hash for this key.
162 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
165 * Add a key-value pair with a pre-computed hash value
166 * to an existing hash table.
167 * This operation is not multi-thread safe
168 * and should only be called from one thread by default.
169 * Thread safety can be enabled by setting flag during
173 * Hash table to add the key to.
175 * Key to add to the hash table.
177 * Precomputed hash value for 'key'
179 * Data to add to the hash table.
181 * - 0 if added successfully
182 * - -EINVAL if the parameters are invalid.
183 * - -ENOSPC if there is no space in the hash for this key.
186 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
187 hash_sig_t sig, void *data);
190 * Add a key to an existing hash table. This operation is not multi-thread safe
191 * and should only be called from one thread by default.
192 * Thread safety can be enabled by setting flag during
196 * Hash table to add the key to.
198 * Key to add to the hash table.
200 * - -EINVAL if the parameters are invalid.
201 * - -ENOSPC if there is no space in the hash for this key.
202 * - A positive value that can be used by the caller as an offset into an
203 * array of user data. This value is unique for this key.
206 rte_hash_add_key(const struct rte_hash *h, const void *key);
209 * Add a key to an existing hash table.
210 * This operation is not multi-thread safe
211 * and should only be called from one thread by default.
212 * Thread safety can be enabled by setting flag during
216 * Hash table to add the key to.
218 * Key to add to the hash table.
220 * Precomputed hash value for 'key'.
222 * - -EINVAL if the parameters are invalid.
223 * - -ENOSPC if there is no space in the hash for this key.
224 * - A positive value that can be used by the caller as an offset into an
225 * array of user data. This value is unique for this key.
228 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
231 * Remove a key from an existing hash table.
232 * This operation is not multi-thread safe
233 * and should only be called from one thread by default.
234 * Thread safety can be enabled by setting flag during
238 * Hash table to remove the key from.
240 * Key to remove from the hash table.
242 * - -EINVAL if the parameters are invalid.
243 * - -ENOENT if the key is not found.
244 * - A positive value that can be used by the caller as an offset into an
245 * array of user data. This value is unique for this key, and is the same
246 * value that was returned when the key was added.
249 rte_hash_del_key(const struct rte_hash *h, const void *key);
252 * Remove a key from an existing hash table.
253 * This operation is not multi-thread safe
254 * and should only be called from one thread by default.
255 * Thread safety can be enabled by setting flag during
259 * Hash table to remove the key from.
261 * Key to remove from the hash table.
263 * Precomputed hash value for 'key'.
265 * - -EINVAL if the parameters are invalid.
266 * - -ENOENT if the key is not found.
267 * - A positive value that can be used by the caller as an offset into an
268 * array of user data. This value is unique for this key, and is the same
269 * value that was returned when the key was added.
272 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
275 * Find a key in the hash table given the position.
276 * This operation is multi-thread safe with regarding to other lookup threads.
277 * Read-write concurrency can be enabled by setting flag during
281 * Hash table to get the key from.
283 * Position returned when the key was inserted.
285 * Output containing a pointer to the key
287 * - 0 if retrieved successfully
288 * - -EINVAL if the parameters are invalid.
289 * - -ENOENT if no valid key is found in the given position.
292 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
296 * Find a key-value pair in the hash table.
297 * This operation is multi-thread safe with regarding to other lookup threads.
298 * Read-write concurrency can be enabled by setting flag during
302 * Hash table to look in.
306 * Output with pointer to data returned from the hash table.
308 * - A positive value that can be used by the caller as an offset into an
309 * array of user data. This value is unique for this key, and is the same
310 * value that was returned when the key was added.
311 * - -EINVAL if the parameters are invalid.
312 * - -ENOENT if the key is not found.
315 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
318 * Find a key-value pair with a pre-computed hash value
319 * to an existing hash table.
320 * This operation is multi-thread safe with regarding to other lookup threads.
321 * Read-write concurrency can be enabled by setting flag during
325 * Hash table to look in.
329 * Precomputed hash value for 'key'
331 * Output with pointer to data returned from the hash table.
333 * - A positive value that can be used by the caller as an offset into an
334 * array of user data. This value is unique for this key, and is the same
335 * value that was returned when the key was added.
336 * - -EINVAL if the parameters are invalid.
337 * - -ENOENT if the key is not found.
340 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
341 hash_sig_t sig, void **data);
344 * Find a key in the hash table.
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 look in.
354 * - -EINVAL if the parameters are invalid.
355 * - -ENOENT if the key is not found.
356 * - A positive value that can be used by the caller as an offset into an
357 * array of user data. This value is unique for this key, and is the same
358 * value that was returned when the key was added.
361 rte_hash_lookup(const struct rte_hash *h, const void *key);
364 * Find a key in the hash table.
365 * This operation is multi-thread safe with regarding to other lookup threads.
366 * Read-write concurrency can be enabled by setting flag during
370 * Hash table to look in.
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_lookup_with_hash(const struct rte_hash *h,
384 const void *key, hash_sig_t sig);
387 * Calc a hash value by key.
388 * This operation is not multi-thread safe.
391 * Hash table to look in.
398 rte_hash_hash(const struct rte_hash *h, const void *key);
401 * Find multiple keys in the hash table.
402 * This operation is multi-thread safe with regarding to other lookup threads.
403 * Read-write concurrency can be enabled by setting flag during
407 * Hash table to look in.
409 * A pointer to a list of keys to look for.
411 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
413 * Output containing a bitmask with all successful lookups.
415 * Output containing array of data returned from all the successful lookups.
417 * -EINVAL if there's an error, otherwise number of successful lookups.
420 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
421 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
424 * Find multiple keys in the hash table.
425 * This operation is multi-thread safe with regarding to other lookup threads.
426 * Read-write concurrency can be enabled by setting flag during
430 * Hash table to look in.
432 * A pointer to a list of keys to look for.
434 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
436 * Output containing a list of values, corresponding to the list of keys that
437 * can be used by the caller as an offset into an array of user data. These
438 * values are unique for each key, and are the same values that were returned
439 * when each key was added. If a key in the list was not found, then -ENOENT
442 * -EINVAL if there's an error, otherwise 0.
445 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
446 uint32_t num_keys, int32_t *positions);
449 * Iterate through the hash table, returning key-value pairs.
452 * Hash table to iterate
454 * Output containing the key where current iterator
457 * Output containing the data associated with key.
458 * Returns NULL if data was not stored.
460 * Pointer to iterator. Should be 0 to start iterating the hash table.
461 * Iterator is incremented after each call of this function.
463 * Position where key was stored, if successful.
464 * - -EINVAL if the parameters are invalid.
465 * - -ENOENT if end of the hash table.
468 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
473 #endif /* _RTE_HASH_H_ */