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 /** @deprecated Maximum bucket size that can be created. */
53 #define RTE_HASH_BUCKET_ENTRIES_MAX 4
55 /** @deprecated Maximum length of key that can be used. */
56 #define RTE_HASH_KEY_LENGTH_MAX 64
58 /** Maximum number of characters in hash name.*/
59 #define RTE_HASH_NAMESIZE 32
61 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
62 #define RTE_HASH_LOOKUP_BULK_MAX 64
63 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
65 /** Signature of key that is stored internally. */
66 typedef uint32_t hash_sig_t;
68 /** Type of function that can be used for calculating the hash value. */
69 typedef uint32_t (*rte_hash_function)(const void *key, uint32_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 bucket_entries; /**< Bucket entries. */
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. */
85 /** @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 * Find an existing hash table object and return a pointer to it.
113 * Name of the hash table as passed to rte_hash_create()
115 * Pointer to hash table or NULL if object not found
116 * with rte_errno set appropriately. Possible rte_errno values include:
117 * - ENOENT - value not available for return
120 rte_hash_find_existing(const char *name);
123 * De-allocate all memory used by hash table.
128 rte_hash_free(struct rte_hash *h);
131 * Add a key to an existing hash table.
132 * This operation is not multi-thread safe
133 * and should only be called from one thread.
136 * Hash table to add the key to.
138 * Key to add to the hash table.
140 * - -EINVAL if the parameters are invalid.
141 * - -ENOSPC if there is no space in the hash for this key.
142 * - A positive value that can be used by the caller as an offset into an
143 * array of user data. This value is unique for this key.
146 rte_hash_add_key(const struct rte_hash *h, const void *key);
149 * Add a key to an existing hash table.
150 * This operation is not multi-thread safe
151 * and should only be called from one thread.
154 * Hash table to add the key to.
156 * Key to add to the hash table.
158 * Precomputed hash value for 'key'.
160 * - -EINVAL if the parameters are invalid.
161 * - -ENOSPC if there is no space in the hash for this key.
162 * - A positive value that can be used by the caller as an offset into an
163 * array of user data. This value is unique for this key.
166 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
169 * Remove a key from an existing hash table.
170 * This operation is not multi-thread safe
171 * and should only be called from one thread.
174 * Hash table to remove the key from.
176 * Key to remove from the hash table.
178 * - -EINVAL if the parameters are invalid.
179 * - -ENOENT if the key is not found.
180 * - A positive value that can be used by the caller as an offset into an
181 * array of user data. This value is unique for this key, and is the same
182 * value that was returned when the key was added.
185 rte_hash_del_key(const struct rte_hash *h, const void *key);
188 * Remove a key from an existing hash table.
189 * This operation is not multi-thread safe
190 * and should only be called from one thread.
193 * Hash table to remove the key from.
195 * Key to remove from the hash table.
197 * Precomputed hash value for 'key'.
199 * - -EINVAL if the parameters are invalid.
200 * - -ENOENT if the key is not found.
201 * - A positive value that can be used by the caller as an offset into an
202 * array of user data. This value is unique for this key, and is the same
203 * value that was returned when the key was added.
206 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
209 * Find a key in the hash table.
210 * This operation is multi-thread safe.
213 * Hash table to look in.
217 * - -EINVAL if the parameters are invalid.
218 * - -ENOENT if the key is not found.
219 * - A positive value that can be used by the caller as an offset into an
220 * array of user data. This value is unique for this key, and is the same
221 * value that was returned when the key was added.
224 rte_hash_lookup(const struct rte_hash *h, const void *key);
227 * Find a key in the hash table.
228 * This operation is multi-thread safe.
231 * Hash table to look in.
235 * Hash value to remove from the hash table.
237 * - -EINVAL if the parameters are invalid.
238 * - -ENOENT if the key is not found.
239 * - A positive value that can be used by the caller as an offset into an
240 * array of user data. This value is unique for this key, and is the same
241 * value that was returned when the key was added.
244 rte_hash_lookup_with_hash(const struct rte_hash *h,
245 const void *key, hash_sig_t sig);
248 * Calc a hash value by key.
249 * This operation is not multi-thread safe.
252 * Hash table to look in.
259 rte_hash_hash(const struct rte_hash *h, const void *key);
261 #define rte_hash_lookup_multi rte_hash_lookup_bulk
263 * Find multiple keys in the hash table.
264 * This operation is multi-thread safe.
267 * Hash table to look in.
269 * A pointer to a list of keys to look for.
271 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
273 * Output containing a list of values, corresponding to the list of keys that
274 * can be used by the caller as an offset into an array of user data. These
275 * values are unique for each key, and are the same values that were returned
276 * when each key was added. If a key in the list was not found, then -ENOENT
279 * -EINVAL if there's an error, otherwise 0.
282 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
283 uint32_t num_keys, int32_t *positions);
289 #endif /* _RTE_HASH_H_ */