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.
49 /** Maximum size of hash table that can be created. */
50 #define RTE_HASH_ENTRIES_MAX (1 << 30)
52 /** Maximum number of characters in hash name.*/
53 #define RTE_HASH_NAMESIZE 32
55 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
56 #define RTE_HASH_LOOKUP_BULK_MAX 64
57 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
59 /** Enable Hardware transactional memory support. */
60 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT 0x01
62 /** Signature of key that is stored internally. */
63 typedef uint32_t hash_sig_t;
65 /** Type of function that can be used for calculating the hash value. */
66 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
69 /** Type of function used to compare the hash key. */
70 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
73 * Parameters used when creating the hash table.
75 struct rte_hash_parameters {
76 const char *name; /**< Name of the hash. */
77 uint32_t entries; /**< Total hash table entries. */
78 uint32_t reserved; /**< Unused field. Should be set to 0 */
79 uint32_t key_len; /**< Length of hash key. */
80 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
81 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
82 int socket_id; /**< NUMA Socket ID for memory. */
83 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
86 /** @internal A hash table structure. */
90 * Create a new hash table.
93 * Parameters used to create and initialise the hash table.
95 * Pointer to hash table structure that is used in future hash table
96 * operations, or NULL on error, with error code set in rte_errno.
97 * Possible rte_errno errors include:
98 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
99 * - E_RTE_SECONDARY - function was called from a secondary process instance
100 * - ENOENT - missing entry
101 * - EINVAL - invalid parameter passed to function
102 * - ENOSPC - the maximum number of memzones has already been allocated
103 * - EEXIST - a memzone with the same name already exists
104 * - ENOMEM - no appropriate memory area found in which to create memzone
107 rte_hash_create(const struct rte_hash_parameters *params);
110 * Set a new hash compare function other than the default one.
112 * @note Function pointer does not work with multi-process, so do not use it
113 * in multi-process mode.
116 * Hash table to reset
118 * New compare function
120 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
123 * Find an existing hash table object and return a pointer to it.
126 * Name of the hash table as passed to rte_hash_create()
128 * Pointer to hash table or NULL if object not found
129 * with rte_errno set appropriately. Possible rte_errno values include:
130 * - ENOENT - value not available for return
133 rte_hash_find_existing(const char *name);
136 * De-allocate all memory used by hash table.
141 rte_hash_free(struct rte_hash *h);
144 * Reset all hash structure, by zeroing all entries
146 * Hash table to reset
149 rte_hash_reset(struct rte_hash *h);
152 * Add a key-value pair to an existing hash table.
153 * This operation is not multi-thread safe
154 * and should only be called from one thread.
157 * Hash table to add the key to.
159 * Key to add to the hash table.
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_data(const struct rte_hash *h, const void *key, void *data);
171 * Add a key-value pair with a pre-computed hash value
172 * to an existing hash table.
173 * This operation is not multi-thread safe
174 * and should only be called from one thread.
177 * Hash table to add the key to.
179 * Key to add to the hash table.
181 * Precomputed hash value for 'key'
183 * Data to add to the hash table.
185 * - 0 if added successfully
186 * - -EINVAL if the parameters are invalid.
187 * - -ENOSPC if there is no space in the hash for this key.
190 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
191 hash_sig_t sig, void *data);
194 * Add a key to an existing hash table. This operation is not multi-thread safe
195 * and should only be called from one thread.
198 * Hash table to add the key to.
200 * Key to add to the hash table.
202 * - -EINVAL if the parameters are invalid.
203 * - -ENOSPC if there is no space in the hash for this key.
204 * - A positive value that can be used by the caller as an offset into an
205 * array of user data. This value is unique for this key.
208 rte_hash_add_key(const struct rte_hash *h, const void *key);
211 * Add a key to an existing hash table.
212 * This operation is not multi-thread safe
213 * and should only be called from one thread.
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.
236 * Hash table to remove the key from.
238 * Key to remove from the hash table.
240 * - -EINVAL if the parameters are invalid.
241 * - -ENOENT if the key is not found.
242 * - A positive value that can be used by the caller as an offset into an
243 * array of user data. This value is unique for this key, and is the same
244 * value that was returned when the key was added.
247 rte_hash_del_key(const struct rte_hash *h, const void *key);
250 * Remove a key from an existing hash table.
251 * This operation is not multi-thread safe
252 * and should only be called from one thread.
255 * Hash table to remove the key from.
257 * Key to remove from the hash table.
259 * Precomputed hash value for 'key'.
261 * - -EINVAL if the parameters are invalid.
262 * - -ENOENT if the key is not found.
263 * - A positive value that can be used by the caller as an offset into an
264 * array of user data. This value is unique for this key, and is the same
265 * value that was returned when the key was added.
268 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
272 * Find a key-value pair in the hash table.
273 * This operation is multi-thread safe.
276 * Hash table to look in.
280 * Output with pointer to data returned from the hash table.
282 * 0 if successful lookup
283 * - EINVAL if the parameters are invalid.
284 * - ENOENT if the key is not found.
287 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
290 * Find a key-value pair with a pre-computed hash value
291 * to an existing hash table.
292 * This operation is multi-thread safe.
295 * Hash table to look in.
299 * Precomputed hash value for 'key'
301 * Output with pointer to data returned from the hash table.
303 * 0 if successful lookup
304 * - EINVAL if the parameters are invalid.
305 * - ENOENT if the key is not found.
308 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
309 hash_sig_t sig, void **data);
312 * Find a key in the hash table.
313 * This operation is multi-thread safe.
316 * Hash table to look in.
320 * - -EINVAL if the parameters are invalid.
321 * - -ENOENT if the key is not found.
322 * - A positive value that can be used by the caller as an offset into an
323 * array of user data. This value is unique for this key, and is the same
324 * value that was returned when the key was added.
327 rte_hash_lookup(const struct rte_hash *h, const void *key);
330 * Find a key in the hash table.
331 * This operation is multi-thread safe.
334 * Hash table to look in.
338 * Hash value to remove from the hash table.
340 * - -EINVAL if the parameters are invalid.
341 * - -ENOENT if the key is not found.
342 * - A positive value that can be used by the caller as an offset into an
343 * array of user data. This value is unique for this key, and is the same
344 * value that was returned when the key was added.
347 rte_hash_lookup_with_hash(const struct rte_hash *h,
348 const void *key, hash_sig_t sig);
351 * Calc a hash value by key.
352 * This operation is not multi-thread safe.
355 * Hash table to look in.
362 rte_hash_hash(const struct rte_hash *h, const void *key);
364 #define rte_hash_lookup_multi rte_hash_lookup_bulk
365 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
367 * Find multiple keys in the hash table.
368 * This operation is multi-thread safe.
371 * Hash table to look in.
373 * A pointer to a list of keys to look for.
375 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
377 * Output containing a bitmask with all successful lookups.
379 * Output containing array of data returned from all the successful lookups.
381 * -EINVAL if there's an error, otherwise number of successful lookups.
384 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
385 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
388 * Find multiple keys in the hash table.
389 * This operation is multi-thread safe.
392 * Hash table to look in.
394 * A pointer to a list of keys to look for.
396 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
398 * Output containing a list of values, corresponding to the list of keys that
399 * can be used by the caller as an offset into an array of user data. These
400 * values are unique for each key, and are the same values that were returned
401 * when each key was added. If a key in the list was not found, then -ENOENT
404 * -EINVAL if there's an error, otherwise 0.
407 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
408 uint32_t num_keys, int32_t *positions);
411 * Iterate through the hash table, returning key-value pairs.
414 * Hash table to iterate
416 * Output containing the key where current iterator
419 * Output containing the data associated with key.
420 * Returns NULL if data was not stored.
422 * Pointer to iterator. Should be 0 to start iterating the hash table.
423 * Iterator is incremented after each call of this function.
425 * Position where key was stored, if successful.
426 * - -EINVAL if the parameters are invalid.
427 * - -ENOENT if end of the hash table.
430 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
435 #endif /* _RTE_HASH_H_ */