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 * Add a key-value pair to an existing hash table.
131 * This operation is not multi-thread safe
132 * and should only be called from one thread.
135 * Hash table to add the key to.
137 * Key to add to the hash table.
139 * Data to add to the hash table.
141 * - 0 if added successfully
142 * - -EINVAL if the parameters are invalid.
143 * - -ENOSPC if there is no space in the hash for this key.
146 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
149 * Add a key-value pair with a pre-computed hash value
150 * to an existing hash table.
151 * This operation is not multi-thread safe
152 * and should only be called from one thread.
155 * Hash table to add the key to.
157 * Key to add to the hash table.
159 * Precomputed hash value for 'key'
161 * Data to add to the hash table.
163 * - 0 if added successfully
164 * - -EINVAL if the parameters are invalid.
165 * - -ENOSPC if there is no space in the hash for this key.
168 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
169 hash_sig_t sig, void *data);
172 * Add a key to an existing hash table. This operation is not multi-thread safe
173 * and should only be called from one thread.
176 * Hash table to add the key to.
178 * Key to add to the hash table.
180 * - -EINVAL if the parameters are invalid.
181 * - -ENOSPC if there is no space in the hash for this key.
182 * - A positive value that can be used by the caller as an offset into an
183 * array of user data. This value is unique for this key.
186 rte_hash_add_key(const struct rte_hash *h, const void *key);
189 * Add a key to an existing hash table.
190 * This operation is not multi-thread safe
191 * and should only be called from one thread.
194 * Hash table to add the key to.
196 * Key to add to the hash table.
198 * Precomputed hash value for 'key'.
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_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
209 * Remove a key from an existing hash table.
210 * This operation is not multi-thread safe
211 * and should only be called from one thread.
214 * Hash table to remove the key from.
216 * Key to remove from the hash table.
218 * - -EINVAL if the parameters are invalid.
219 * - -ENOENT if the key is not found.
220 * - A positive value that can be used by the caller as an offset into an
221 * array of user data. This value is unique for this key, and is the same
222 * value that was returned when the key was added.
225 rte_hash_del_key(const struct rte_hash *h, const void *key);
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.
233 * Hash table to remove the key from.
235 * Key to remove from the hash table.
237 * Precomputed hash value for 'key'.
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_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
249 * Find a key in the hash table given the position.
250 * This operation is multi-thread safe.
253 * Hash table to get the key from.
255 * Position returned when the key was inserted.
257 * Output containing a pointer to the key
259 * - 0 if retrieved successfully
260 * - EINVAL if the parameters are invalid.
261 * - ENOENT if no valid key is found in the given position.
264 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
268 * Find a key-value pair in the hash table.
269 * This operation is multi-thread safe.
272 * Hash table to look in.
276 * Output with pointer to data returned from the hash table.
278 * 0 if successful lookup
279 * - EINVAL if the parameters are invalid.
280 * - ENOENT if the key is not found.
283 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
286 * Find a key-value pair with a pre-computed hash value
287 * to an existing hash table.
288 * This operation is multi-thread safe.
291 * Hash table to look in.
295 * Precomputed hash value for 'key'
297 * Output with pointer to data returned from the hash table.
299 * 0 if successful lookup
300 * - EINVAL if the parameters are invalid.
301 * - ENOENT if the key is not found.
304 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
305 hash_sig_t sig, void **data);
308 * Find a key in the hash table.
309 * This operation is multi-thread safe.
312 * Hash table to look in.
316 * - -EINVAL if the parameters are invalid.
317 * - -ENOENT if the key is not found.
318 * - A positive value that can be used by the caller as an offset into an
319 * array of user data. This value is unique for this key, and is the same
320 * value that was returned when the key was added.
323 rte_hash_lookup(const struct rte_hash *h, const void *key);
326 * Find a key in the hash table.
327 * This operation is multi-thread safe.
330 * Hash table to look in.
334 * Precomputed hash value for 'key'.
336 * - -EINVAL if the parameters are invalid.
337 * - -ENOENT if the key is not found.
338 * - A positive value that can be used by the caller as an offset into an
339 * array of user data. This value is unique for this key, and is the same
340 * value that was returned when the key was added.
343 rte_hash_lookup_with_hash(const struct rte_hash *h,
344 const void *key, hash_sig_t sig);
347 * Calc a hash value by key.
348 * This operation is not multi-thread safe.
351 * Hash table to look in.
358 rte_hash_hash(const struct rte_hash *h, const void *key);
361 * Find multiple keys in the hash table.
362 * This operation is multi-thread safe.
365 * Hash table to look in.
367 * A pointer to a list of keys to look for.
369 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
371 * Output containing a bitmask with all successful lookups.
373 * Output containing array of data returned from all the successful lookups.
375 * -EINVAL if there's an error, otherwise number of successful lookups.
378 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
379 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
382 * Find multiple keys in the hash table.
383 * This operation is multi-thread safe.
386 * Hash table to look in.
388 * A pointer to a list of keys to look for.
390 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
392 * Output containing a list of values, corresponding to the list of keys that
393 * can be used by the caller as an offset into an array of user data. These
394 * values are unique for each key, and are the same values that were returned
395 * when each key was added. If a key in the list was not found, then -ENOENT
398 * -EINVAL if there's an error, otherwise 0.
401 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
402 uint32_t num_keys, int32_t *positions);
405 * Iterate through the hash table, returning key-value pairs.
408 * Hash table to iterate
410 * Output containing the key where current iterator
413 * Output containing the data associated with key.
414 * Returns NULL if data was not stored.
416 * Pointer to iterator. Should be 0 to start iterating the hash table.
417 * Iterator is incremented after each call of this function.
419 * Position where key was stored, if successful.
420 * - -EINVAL if the parameters are invalid.
421 * - -ENOENT if end of the hash table.
424 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
429 #endif /* _RTE_HASH_H_ */