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 /** Signature of key that is stored internally. */
60 typedef uint32_t hash_sig_t;
62 /** Type of function that can be used for calculating the hash value. */
63 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
67 * Parameters used when creating the hash table.
69 struct rte_hash_parameters {
70 const char *name; /**< Name of the hash. */
71 uint32_t entries; /**< Total hash table entries. */
72 uint32_t reserved; /**< Unused field. Should be set to 0 */
73 uint32_t key_len; /**< Length of hash key. */
74 rte_hash_function hash_func; /**< Primary Hash function used to calculate hash. */
75 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
76 int socket_id; /**< NUMA Socket ID for memory. */
77 uint8_t extra_flag; /**< Indicate if additional parameters are present. */
80 /** @internal A hash table structure. */
84 * Create a new hash table.
87 * Parameters used to create and initialise the hash table.
89 * Pointer to hash table structure that is used in future hash table
90 * operations, or NULL on error, with error code set in rte_errno.
91 * Possible rte_errno errors include:
92 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
93 * - E_RTE_SECONDARY - function was called from a secondary process instance
94 * - ENOENT - missing entry
95 * - EINVAL - invalid parameter passed to function
96 * - ENOSPC - the maximum number of memzones has already been allocated
97 * - EEXIST - a memzone with the same name already exists
98 * - ENOMEM - no appropriate memory area found in which to create memzone
101 rte_hash_create(const struct rte_hash_parameters *params);
104 * Find an existing hash table object and return a pointer to it.
107 * Name of the hash table as passed to rte_hash_create()
109 * Pointer to hash table or NULL if object not found
110 * with rte_errno set appropriately. Possible rte_errno values include:
111 * - ENOENT - value not available for return
114 rte_hash_find_existing(const char *name);
117 * De-allocate all memory used by hash table.
122 rte_hash_free(struct rte_hash *h);
125 * Reset all hash structure, by zeroing all entries
127 * Hash table to reset
130 rte_hash_reset(struct rte_hash *h);
133 * Add a key-value pair to an existing hash table.
134 * This operation is not multi-thread safe
135 * and should only be called from one thread.
138 * Hash table to add the key to.
140 * Key to add to the hash table.
142 * Data to add to the hash table.
144 * - 0 if added successfully
145 * - -EINVAL if the parameters are invalid.
146 * - -ENOSPC if there is no space in the hash for this key.
149 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
152 * Add a key-value pair with a pre-computed hash value
153 * 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 * Precomputed hash value for 'key'
164 * Data to add to the hash table.
166 * - 0 if added successfully
167 * - -EINVAL if the parameters are invalid.
168 * - -ENOSPC if there is no space in the hash for this key.
171 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
172 hash_sig_t sig, void *data);
175 * Add a key to an existing hash table. This operation is not multi-thread safe
176 * and should only be called from one thread.
179 * Hash table to add the key to.
181 * Key to add to the hash table.
183 * - -EINVAL if the parameters are invalid.
184 * - -ENOSPC if there is no space in the hash for this key.
185 * - A positive value that can be used by the caller as an offset into an
186 * array of user data. This value is unique for this key.
189 rte_hash_add_key(const struct rte_hash *h, const void *key);
192 * Add a key to an existing hash table.
193 * This operation is not multi-thread safe
194 * and should only be called from one thread.
197 * Hash table to add the key to.
199 * Key to add to the hash table.
201 * Precomputed hash value for 'key'.
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_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
212 * Remove a key from 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 remove the key from.
219 * Key to remove from the hash table.
221 * - -EINVAL if the parameters are invalid.
222 * - -ENOENT if the key is not found.
223 * - A positive value that can be used by the caller as an offset into an
224 * array of user data. This value is unique for this key, and is the same
225 * value that was returned when the key was added.
228 rte_hash_del_key(const struct rte_hash *h, const void *key);
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 * Precomputed hash value for 'key'.
242 * - -EINVAL if the parameters are invalid.
243 * - -ENOENT if the key is not found.
244 * - A positive value that can be used by the caller as an offset into an
245 * array of user data. This value is unique for this key, and is the same
246 * value that was returned when the key was added.
249 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
253 * Find a key-value pair in the hash table.
254 * This operation is multi-thread safe.
257 * Hash table to look in.
261 * Output with pointer to data returned from the hash table.
263 * 0 if successful lookup
264 * - EINVAL if the parameters are invalid.
265 * - ENOENT if the key is not found.
268 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
271 * Find a key-value pair with a pre-computed hash value
272 * to an existing hash table.
273 * This operation is multi-thread safe.
276 * Hash table to look in.
280 * Precomputed hash value for 'key'
282 * Output with pointer to data returned from the hash table.
284 * 0 if successful lookup
285 * - EINVAL if the parameters are invalid.
286 * - ENOENT if the key is not found.
289 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
290 hash_sig_t sig, void **data);
293 * Find a key in the hash table.
294 * This operation is multi-thread safe.
297 * Hash table to look in.
301 * - -EINVAL if the parameters are invalid.
302 * - -ENOENT if the key is not found.
303 * - A positive value that can be used by the caller as an offset into an
304 * array of user data. This value is unique for this key, and is the same
305 * value that was returned when the key was added.
308 rte_hash_lookup(const struct rte_hash *h, const void *key);
311 * Find a key in the hash table.
312 * This operation is multi-thread safe.
315 * Hash table to look in.
319 * Hash value to remove from the hash table.
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_with_hash(const struct rte_hash *h,
329 const void *key, hash_sig_t sig);
332 * Calc a hash value by key.
333 * This operation is not multi-thread safe.
336 * Hash table to look in.
343 rte_hash_hash(const struct rte_hash *h, const void *key);
345 #define rte_hash_lookup_multi rte_hash_lookup_bulk
346 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
348 * Find multiple keys in the hash table.
349 * This operation is multi-thread safe.
352 * Hash table to look in.
354 * A pointer to a list of keys to look for.
356 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
358 * Output containing a bitmask with all successful lookups.
360 * Output containing array of data returned from all the successful lookups.
362 * -EINVAL if there's an error, otherwise number of successful lookups.
365 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
366 uint32_t num_keys, uint64_t *hit_mask, void *data[]);
369 * Find multiple keys in the hash table.
370 * This operation is multi-thread safe.
373 * Hash table to look in.
375 * A pointer to a list of keys to look for.
377 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
379 * Output containing a list of values, corresponding to the list of keys that
380 * can be used by the caller as an offset into an array of user data. These
381 * values are unique for each key, and are the same values that were returned
382 * when each key was added. If a key in the list was not found, then -ENOENT
385 * -EINVAL if there's an error, otherwise 0.
388 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
389 uint32_t num_keys, int32_t *positions);
392 * Iterate through the hash table, returning key-value pairs.
395 * Hash table to iterate
397 * Output containing the key where current iterator
400 * Output containing the data associated with key.
401 * Returns NULL if data was not stored.
403 * Pointer to iterator. Should be 0 to start iterating the hash table.
404 * Iterator is incremented after each call of this function.
406 * Position where key was stored, if successful.
407 * - -EINVAL if the parameters are invalid.
408 * - -ENOENT if end of the hash table.
411 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
416 #endif /* _RTE_HASH_H_ */