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,
70 * Parameters used when creating the hash table.
72 struct rte_hash_parameters {
73 const char *name; /**< Name of the hash. */
74 uint32_t entries; /**< Total hash table entries. */
75 uint32_t reserved; /**< Unused field. Should be set to 0 */
76 uint32_t key_len; /**< Length of hash key. */
77 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
78 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
79 int socket_id; /**< NUMA Socket ID for memory. */
80 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
83 /** @internal A hash table structure. */
87 * Create a new hash table.
90 * Parameters used to create and initialise the hash table.
92 * Pointer to hash table structure that is used in future hash table
93 * operations, or NULL on error, with error code set in rte_errno.
94 * Possible rte_errno errors include:
95 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
96 * - E_RTE_SECONDARY - function was called from a secondary process instance
97 * - ENOENT - missing entry
98 * - EINVAL - invalid parameter passed to function
99 * - ENOSPC - the maximum number of memzones has already been allocated
100 * - EEXIST - a memzone with the same name already exists
101 * - ENOMEM - no appropriate memory area found in which to create memzone
104 rte_hash_create(const struct rte_hash_parameters *params);
107 * Find an existing hash table object and return a pointer to it.
110 * Name of the hash table as passed to rte_hash_create()
112 * Pointer to hash table or NULL if object not found
113 * with rte_errno set appropriately. Possible rte_errno values include:
114 * - ENOENT - value not available for return
117 rte_hash_find_existing(const char *name);
120 * De-allocate all memory used by hash table.
125 rte_hash_free(struct rte_hash *h);
128 * Reset all hash structure, by zeroing all entries
130 * Hash table to reset
133 rte_hash_reset(struct rte_hash *h);
136 * Add a key-value pair to an existing hash table.
137 * This operation is not multi-thread safe
138 * and should only be called from one thread.
141 * Hash table to add the key to.
143 * Key to add to the hash table.
145 * Data to add to the hash table.
147 * - 0 if added successfully
148 * - -EINVAL if the parameters are invalid.
149 * - -ENOSPC if there is no space in the hash for this key.
152 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
155 * Add a key-value pair with a pre-computed hash value
156 * 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 * Precomputed hash value for 'key'
167 * Data to add to the hash table.
169 * - 0 if added successfully
170 * - -EINVAL if the parameters are invalid.
171 * - -ENOSPC if there is no space in the hash for this key.
174 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
175 hash_sig_t sig, void *data);
178 * Add a key to an existing hash table. This operation is not multi-thread safe
179 * and should only be called from one thread.
182 * Hash table to add the key to.
184 * Key to add to the hash table.
186 * - -EINVAL if the parameters are invalid.
187 * - -ENOSPC if there is no space in the hash for this key.
188 * - A positive value that can be used by the caller as an offset into an
189 * array of user data. This value is unique for this key.
192 rte_hash_add_key(const struct rte_hash *h, const void *key);
195 * Add a key to an existing hash table.
196 * This operation is not multi-thread safe
197 * and should only be called from one thread.
200 * Hash table to add the key to.
202 * Key to add to the hash table.
204 * Precomputed hash value for 'key'.
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_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
215 * Remove a key from 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 remove the key from.
222 * Key to remove from the hash table.
224 * - -EINVAL if the parameters are invalid.
225 * - -ENOENT if the key is not found.
226 * - A positive value that can be used by the caller as an offset into an
227 * array of user data. This value is unique for this key, and is the same
228 * value that was returned when the key was added.
231 rte_hash_del_key(const struct rte_hash *h, const void *key);
234 * Remove a key from an existing hash table.
235 * This operation is not multi-thread safe
236 * and should only be called from one thread.
239 * Hash table to remove the key from.
241 * Key to remove from the hash table.
243 * Precomputed hash value for 'key'.
245 * - -EINVAL if the parameters are invalid.
246 * - -ENOENT if the key is not found.
247 * - A positive value that can be used by the caller as an offset into an
248 * array of user data. This value is unique for this key, and is the same
249 * value that was returned when the key was added.
252 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
256 * Find a key-value pair in the hash table.
257 * This operation is multi-thread safe.
260 * Hash table to look in.
264 * Output with pointer to data returned from the hash table.
266 * 0 if successful lookup
267 * - EINVAL if the parameters are invalid.
268 * - ENOENT if the key is not found.
271 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
274 * Find a key-value pair with a pre-computed hash value
275 * to an existing hash table.
276 * This operation is multi-thread safe.
279 * Hash table to look in.
283 * Precomputed hash value for 'key'
285 * Output with pointer to data returned from the hash table.
287 * 0 if successful lookup
288 * - EINVAL if the parameters are invalid.
289 * - ENOENT if the key is not found.
292 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
293 hash_sig_t sig, void **data);
296 * Find a key in the hash table.
297 * This operation is multi-thread safe.
300 * Hash table to look in.
304 * - -EINVAL if the parameters are invalid.
305 * - -ENOENT if the key is not found.
306 * - A positive value that can be used by the caller as an offset into an
307 * array of user data. This value is unique for this key, and is the same
308 * value that was returned when the key was added.
311 rte_hash_lookup(const struct rte_hash *h, const void *key);
314 * Find a key in the hash table.
315 * This operation is multi-thread safe.
318 * Hash table to look in.
322 * Hash value to remove from the hash table.
324 * - -EINVAL if the parameters are invalid.
325 * - -ENOENT if the key is not found.
326 * - A positive value that can be used by the caller as an offset into an
327 * array of user data. This value is unique for this key, and is the same
328 * value that was returned when the key was added.
331 rte_hash_lookup_with_hash(const struct rte_hash *h,
332 const void *key, hash_sig_t sig);
335 * Calc a hash value by key.
336 * This operation is not multi-thread safe.
339 * Hash table to look in.
346 rte_hash_hash(const struct rte_hash *h, const void *key);
348 #define rte_hash_lookup_multi rte_hash_lookup_bulk
349 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
351 * Find multiple keys in the hash table.
352 * This operation is multi-thread safe.
355 * Hash table to look in.
357 * A pointer to a list of keys to look for.
359 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
361 * Output containing a bitmask with all successful lookups.
363 * Output containing array of data returned from all the successful lookups.
365 * -EINVAL if there's an error, otherwise number of successful lookups.
368 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
369 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
372 * Find multiple keys in the hash table.
373 * This operation is multi-thread safe.
376 * Hash table to look in.
378 * A pointer to a list of keys to look for.
380 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
382 * Output containing a list of values, corresponding to the list of keys that
383 * can be used by the caller as an offset into an array of user data. These
384 * values are unique for each key, and are the same values that were returned
385 * when each key was added. If a key in the list was not found, then -ENOENT
388 * -EINVAL if there's an error, otherwise 0.
391 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
392 uint32_t num_keys, int32_t *positions);
395 * Iterate through the hash table, returning key-value pairs.
398 * Hash table to iterate
400 * Output containing the key where current iterator
403 * Output containing the data associated with key.
404 * Returns NULL if data was not stored.
406 * Pointer to iterator. Should be 0 to start iterating the hash table.
407 * Iterator is incremented after each call of this function.
409 * Position where key was stored, if successful.
410 * - -EINVAL if the parameters are invalid.
411 * - -ENOENT if end of the hash table.
414 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
419 #endif /* _RTE_HASH_H_ */