4 * Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 /** Maximum size of hash table that can be created. */
51 #define RTE_HASH_ENTRIES_MAX (1 << 30)
53 /** Maximum number of characters in hash name.*/
54 #define RTE_HASH_NAMESIZE 32
56 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
57 #define RTE_HASH_LOOKUP_BULK_MAX 64
58 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
60 /** Enable Hardware transactional memory support. */
61 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT 0x01
63 /** Default behavior of insertion, single writer/multi writer */
64 #define RTE_HASH_EXTRA_FLAGS_MULTI_WRITER_ADD 0x02
66 /** Signature of key that is stored internally. */
67 typedef uint32_t hash_sig_t;
69 /** Type of function that can be used for calculating the hash value. */
70 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
73 /** Type of function used to compare the hash key. */
74 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
77 * Parameters used when creating the hash table.
79 struct rte_hash_parameters {
80 const char *name; /**< Name of the hash. */
81 uint32_t entries; /**< Total hash table entries. */
82 uint32_t reserved; /**< Unused field. Should be set to 0 */
83 uint32_t key_len; /**< Length of hash key. */
84 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
85 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
86 int socket_id; /**< NUMA Socket ID for memory. */
87 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
90 /** @internal A hash table structure. */
94 * Create a new hash table.
97 * Parameters used to create and initialise the hash table.
99 * Pointer to hash table structure that is used in future hash table
100 * operations, or NULL on error, with error code set in rte_errno.
101 * Possible rte_errno errors include:
102 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
103 * - E_RTE_SECONDARY - function was called from a secondary process instance
104 * - ENOENT - missing entry
105 * - EINVAL - invalid parameter passed to function
106 * - ENOSPC - the maximum number of memzones has already been allocated
107 * - EEXIST - a memzone with the same name already exists
108 * - ENOMEM - no appropriate memory area found in which to create memzone
111 rte_hash_create(const struct rte_hash_parameters *params);
114 * Set a new hash compare function other than the default one.
116 * @note Function pointer does not work with multi-process, so do not use it
117 * in multi-process mode.
120 * Hash table for which the function is to be changed
122 * New compare function
124 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
127 * Find an existing hash table object and return a pointer to it.
130 * Name of the hash table as passed to rte_hash_create()
132 * Pointer to hash table or NULL if object not found
133 * with rte_errno set appropriately. Possible rte_errno values include:
134 * - ENOENT - value not available for return
137 rte_hash_find_existing(const char *name);
140 * De-allocate all memory used by hash table.
145 rte_hash_free(struct rte_hash *h);
148 * Reset all hash structure, by zeroing all entries
150 * Hash table to reset
153 rte_hash_reset(struct rte_hash *h);
156 * Add a key-value pair to an existing hash table.
157 * This operation is not multi-thread safe
158 * and should only be called from one thread.
161 * Hash table to add the key to.
163 * Key to add to the hash table.
165 * Data to add to the hash table.
167 * - 0 if added successfully
168 * - -EINVAL if the parameters are invalid.
169 * - -ENOSPC if there is no space in the hash for this key.
172 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
175 * Add a key-value pair with a pre-computed hash value
176 * to an existing hash table.
177 * This operation is not multi-thread safe
178 * and should only be called from one thread.
181 * Hash table to add the key to.
183 * Key to add to the hash table.
185 * Precomputed hash value for 'key'
187 * Data to add to the hash table.
189 * - 0 if added successfully
190 * - -EINVAL if the parameters are invalid.
191 * - -ENOSPC if there is no space in the hash for this key.
194 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
195 hash_sig_t sig, void *data);
198 * Add a key to an existing hash table. This operation is not multi-thread safe
199 * and should only be called from one thread.
202 * Hash table to add the key to.
204 * Key to add to the hash table.
206 * - -EINVAL if the parameters are invalid.
207 * - -ENOSPC if there is no space in the hash for this key.
208 * - A positive value that can be used by the caller as an offset into an
209 * array of user data. This value is unique for this key.
212 rte_hash_add_key(const struct rte_hash *h, const void *key);
215 * Add a key to an existing hash table.
216 * This operation is not multi-thread safe
217 * and should only be called from one thread.
220 * Hash table to add the key to.
222 * Key to add to the hash table.
224 * Precomputed hash value for 'key'.
226 * - -EINVAL if the parameters are invalid.
227 * - -ENOSPC if there is no space in the hash for this key.
228 * - A positive value that can be used by the caller as an offset into an
229 * array of user data. This value is unique for this key.
232 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
235 * Remove a key from an existing hash table.
236 * This operation is not multi-thread safe
237 * and should only be called from one thread.
240 * Hash table to remove the key from.
242 * Key to remove from the hash table.
244 * - -EINVAL if the parameters are invalid.
245 * - -ENOENT if the key is not found.
246 * - A positive value that can be used by the caller as an offset into an
247 * array of user data. This value is unique for this key, and is the same
248 * value that was returned when the key was added.
251 rte_hash_del_key(const struct rte_hash *h, const void *key);
254 * Remove a key from an existing hash table.
255 * This operation is not multi-thread safe
256 * and should only be called from one thread.
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.
279 * Hash table to get the key from.
281 * Position returned when the key was inserted.
283 * Output containing a pointer to the key
285 * - 0 if retrieved successfully
286 * - -EINVAL if the parameters are invalid.
287 * - -ENOENT if no valid key is found in the given position.
290 rte_hash_get_key_with_position(const struct rte_hash *h, const int32_t position,
294 * Find a key-value pair in the hash table.
295 * This operation is multi-thread safe.
298 * Hash table to look in.
302 * Output with pointer to data returned from the hash table.
304 * - A positive value that can be used by the caller as an offset into an
305 * array of user data. This value is unique for this key, and is the same
306 * value that was returned when the key was added.
307 * - -EINVAL if the parameters are invalid.
308 * - -ENOENT if the key is not found.
311 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
314 * Find a key-value pair with a pre-computed hash value
315 * to an existing hash table.
316 * This operation is multi-thread safe.
319 * Hash table to look in.
323 * Precomputed hash value for 'key'
325 * Output with pointer to data returned from the hash table.
327 * - A positive value that can be used by the caller as an offset into an
328 * array of user data. This value is unique for this key, and is the same
329 * value that was returned when the key was added.
330 * - -EINVAL if the parameters are invalid.
331 * - -ENOENT if the key is not found.
334 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
335 hash_sig_t sig, void **data);
338 * Find a key in the hash table.
339 * This operation is multi-thread safe.
342 * Hash table to look in.
346 * - -EINVAL if the parameters are invalid.
347 * - -ENOENT if the key is not found.
348 * - A positive value that can be used by the caller as an offset into an
349 * array of user data. This value is unique for this key, and is the same
350 * value that was returned when the key was added.
353 rte_hash_lookup(const struct rte_hash *h, const void *key);
356 * Find a key in the hash table.
357 * This operation is multi-thread safe.
360 * Hash table to look in.
364 * Hash value to remove from the hash table.
366 * - -EINVAL if the parameters are invalid.
367 * - -ENOENT if the key is not found.
368 * - A positive value that can be used by the caller as an offset into an
369 * array of user data. This value is unique for this key, and is the same
370 * value that was returned when the key was added.
373 rte_hash_lookup_with_hash(const struct rte_hash *h,
374 const void *key, hash_sig_t sig);
377 * Calc a hash value by key.
378 * This operation is not multi-thread safe.
381 * Hash table to look in.
388 rte_hash_hash(const struct rte_hash *h, const void *key);
391 * Find multiple keys in the hash table.
392 * This operation is multi-thread safe.
395 * Hash table to look in.
397 * A pointer to a list of keys to look for.
399 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
401 * Output containing a bitmask with all successful lookups.
403 * Output containing array of data returned from all the successful lookups.
405 * -EINVAL if there's an error, otherwise number of successful lookups.
408 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
409 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
412 * Find multiple keys in the hash table.
413 * This operation is multi-thread safe.
416 * Hash table to look in.
418 * A pointer to a list of keys to look for.
420 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
422 * Output containing a list of values, corresponding to the list of keys that
423 * can be used by the caller as an offset into an array of user data. These
424 * values are unique for each key, and are the same values that were returned
425 * when each key was added. If a key in the list was not found, then -ENOENT
428 * -EINVAL if there's an error, otherwise 0.
431 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
432 uint32_t num_keys, int32_t *positions);
435 * Iterate through the hash table, returning key-value pairs.
438 * Hash table to iterate
440 * Output containing the key where current iterator
443 * Output containing the data associated with key.
444 * Returns NULL if data was not stored.
446 * Pointer to iterator. Should be 0 to start iterating the hash table.
447 * Iterator is incremented after each call of this function.
449 * Position where key was stored, if successful.
450 * - -EINVAL if the parameters are invalid.
451 * - -ENOENT if end of the hash table.
454 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
459 #endif /* _RTE_HASH_H_ */