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 /** Signature of key that is stored internally. */
64 typedef uint32_t hash_sig_t;
66 /** Type of function that can be used for calculating the hash value. */
67 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
70 /** Type of function used to compare the hash key. */
71 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
74 * Parameters used when creating the hash table.
76 struct rte_hash_parameters {
77 const char *name; /**< Name of the hash. */
78 uint32_t entries; /**< Total hash table entries. */
79 uint32_t reserved; /**< Unused field. Should be set to 0 */
80 uint32_t key_len; /**< Length of hash key. */
81 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
82 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
83 int socket_id; /**< NUMA Socket ID for memory. */
84 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
87 /** @internal A hash table structure. */
91 * Create a new hash table.
94 * Parameters used to create and initialise the hash table.
96 * Pointer to hash table structure that is used in future hash table
97 * operations, or NULL on error, with error code set in rte_errno.
98 * Possible rte_errno errors include:
99 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
100 * - E_RTE_SECONDARY - function was called from a secondary process instance
101 * - ENOENT - missing entry
102 * - EINVAL - invalid parameter passed to function
103 * - ENOSPC - the maximum number of memzones has already been allocated
104 * - EEXIST - a memzone with the same name already exists
105 * - ENOMEM - no appropriate memory area found in which to create memzone
108 rte_hash_create(const struct rte_hash_parameters *params);
111 * Set a new hash compare function other than the default one.
113 * @note Function pointer does not work with multi-process, so do not use it
114 * in multi-process mode.
117 * Hash table to reset
119 * New compare function
121 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
124 * Find an existing hash table object and return a pointer to it.
127 * Name of the hash table as passed to rte_hash_create()
129 * Pointer to hash table or NULL if object not found
130 * with rte_errno set appropriately. Possible rte_errno values include:
131 * - ENOENT - value not available for return
134 rte_hash_find_existing(const char *name);
137 * De-allocate all memory used by hash table.
142 rte_hash_free(struct rte_hash *h);
145 * Reset all hash structure, by zeroing all entries
147 * Hash table to reset
150 rte_hash_reset(struct rte_hash *h);
153 * Add a key-value pair to an existing hash table.
154 * This operation is not multi-thread safe
155 * and should only be called from one thread.
158 * Hash table to add the key to.
160 * Key to add to the hash table.
162 * Data to add to the hash table.
164 * - 0 if added successfully
165 * - -EINVAL if the parameters are invalid.
166 * - -ENOSPC if there is no space in the hash for this key.
169 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
172 * Add a key-value pair with a pre-computed hash value
173 * to an existing hash table.
174 * This operation is not multi-thread safe
175 * and should only be called from one thread.
178 * Hash table to add the key to.
180 * Key to add to the hash table.
182 * Precomputed hash value for 'key'
184 * Data to add to the hash table.
186 * - 0 if added successfully
187 * - -EINVAL if the parameters are invalid.
188 * - -ENOSPC if there is no space in the hash for this key.
191 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
192 hash_sig_t sig, void *data);
195 * Add a key to an existing hash table. This operation is not multi-thread safe
196 * and should only be called from one thread.
199 * Hash table to add the key to.
201 * Key to add to the hash table.
203 * - -EINVAL if the parameters are invalid.
204 * - -ENOSPC if there is no space in the hash for this key.
205 * - A positive value that can be used by the caller as an offset into an
206 * array of user data. This value is unique for this key.
209 rte_hash_add_key(const struct rte_hash *h, const void *key);
212 * Add a key to an existing hash table.
213 * This operation is not multi-thread safe
214 * and should only be called from one thread.
217 * Hash table to add the key to.
219 * Key to add to the hash table.
221 * Precomputed hash value for 'key'.
223 * - -EINVAL if the parameters are invalid.
224 * - -ENOSPC if there is no space in the hash for this key.
225 * - A positive value that can be used by the caller as an offset into an
226 * array of user data. This value is unique for this key.
229 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
232 * Remove a key from an existing hash table.
233 * This operation is not multi-thread safe
234 * and should only be called from one thread.
237 * Hash table to remove the key from.
239 * Key to remove from the hash table.
241 * - -EINVAL if the parameters are invalid.
242 * - -ENOENT if the key is not found.
243 * - A positive value that can be used by the caller as an offset into an
244 * array of user data. This value is unique for this key, and is the same
245 * value that was returned when the key was added.
248 rte_hash_del_key(const struct rte_hash *h, const void *key);
251 * Remove a key from an existing hash table.
252 * This operation is not multi-thread safe
253 * and should only be called from one thread.
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);
273 * Find a key-value pair in the hash table.
274 * This operation is multi-thread safe.
277 * Hash table to look in.
281 * Output with pointer to data returned from the hash table.
283 * 0 if successful lookup
284 * - EINVAL if the parameters are invalid.
285 * - ENOENT if the key is not found.
288 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
291 * Find a key-value pair with a pre-computed hash value
292 * to an existing hash table.
293 * This operation is multi-thread safe.
296 * Hash table to look in.
300 * Precomputed hash value for 'key'
302 * Output with pointer to data returned from the hash table.
304 * 0 if successful lookup
305 * - EINVAL if the parameters are invalid.
306 * - ENOENT if the key is not found.
309 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
310 hash_sig_t sig, void **data);
313 * Find a key in the hash table.
314 * This operation is multi-thread safe.
317 * Hash table to look in.
321 * - -EINVAL if the parameters are invalid.
322 * - -ENOENT if the key is not found.
323 * - A positive value that can be used by the caller as an offset into an
324 * array of user data. This value is unique for this key, and is the same
325 * value that was returned when the key was added.
328 rte_hash_lookup(const struct rte_hash *h, const void *key);
331 * Find a key in the hash table.
332 * This operation is multi-thread safe.
335 * Hash table to look in.
339 * Hash value to remove from the hash table.
341 * - -EINVAL if the parameters are invalid.
342 * - -ENOENT if the key is not found.
343 * - A positive value that can be used by the caller as an offset into an
344 * array of user data. This value is unique for this key, and is the same
345 * value that was returned when the key was added.
348 rte_hash_lookup_with_hash(const struct rte_hash *h,
349 const void *key, hash_sig_t sig);
352 * Calc a hash value by key.
353 * This operation is not multi-thread safe.
356 * Hash table to look in.
363 rte_hash_hash(const struct rte_hash *h, const void *key);
365 #define rte_hash_lookup_multi rte_hash_lookup_bulk
366 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
368 * Find multiple keys in the hash table.
369 * This operation is multi-thread safe.
372 * Hash table to look in.
374 * A pointer to a list of keys to look for.
376 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
378 * Output containing a bitmask with all successful lookups.
380 * Output containing array of data returned from all the successful lookups.
382 * -EINVAL if there's an error, otherwise number of successful lookups.
385 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
386 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
389 * Find multiple keys in the hash table.
390 * This operation is multi-thread safe.
393 * Hash table to look in.
395 * A pointer to a list of keys to look for.
397 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
399 * Output containing a list of values, corresponding to the list of keys that
400 * can be used by the caller as an offset into an array of user data. These
401 * values are unique for each key, and are the same values that were returned
402 * when each key was added. If a key in the list was not found, then -ENOENT
405 * -EINVAL if there's an error, otherwise 0.
408 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
409 uint32_t num_keys, int32_t *positions);
412 * Iterate through the hash table, returning key-value pairs.
415 * Hash table to iterate
417 * Output containing the key where current iterator
420 * Output containing the data associated with key.
421 * Returns NULL if data was not stored.
423 * Pointer to iterator. Should be 0 to start iterating the hash table.
424 * Iterator is incremented after each call of this function.
426 * Position where key was stored, if successful.
427 * - -EINVAL if the parameters are invalid.
428 * - -ENOENT if end of the hash table.
431 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
436 #endif /* _RTE_HASH_H_ */