4 * Copyright(c) 2010-2014 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.
44 #include <sys/queue.h>
50 /** Maximum size of hash table that can be created. */
51 #define RTE_HASH_ENTRIES_MAX (1 << 26)
53 /** Maximum bucket size that can be created. */
54 #define RTE_HASH_BUCKET_ENTRIES_MAX 16
56 /** Maximum length of key that can be used. */
57 #define RTE_HASH_KEY_LENGTH_MAX 64
59 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
60 #define RTE_HASH_LOOKUP_BULK_MAX 16
61 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
63 /** Max number of characters in hash name.*/
64 #define RTE_HASH_NAMESIZE 32
66 /** Signature of key that is stored internally. */
67 typedef uint32_t hash_sig_t;
69 /** Type of function that can be used for calculating the hash value. */
70 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
74 * Parameters used when creating the hash table. The total table entries and
75 * bucket entries must be a power of 2.
77 struct rte_hash_parameters {
78 const char *name; /**< Name of the hash. */
79 uint32_t entries; /**< Total hash table entries. */
80 uint32_t bucket_entries; /**< Bucket entries. */
81 uint32_t key_len; /**< Length of hash key. */
82 rte_hash_function hash_func; /**< Function used to calculate hash. */
83 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
84 int socket_id; /**< NUMA Socket ID for memory. */
87 /** A hash table structure. */
89 char name[RTE_HASH_NAMESIZE]; /**< Name of the hash. */
90 uint32_t entries; /**< Total table entries. */
91 uint32_t bucket_entries; /**< Bucket entries. */
92 uint32_t key_len; /**< Length of hash key. */
93 rte_hash_function hash_func; /**< Function used to calculate hash. */
94 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
95 uint32_t num_buckets; /**< Number of buckets in table. */
96 uint32_t bucket_bitmask; /**< Bitmask for getting bucket index
97 from hash signature. */
98 hash_sig_t sig_msb; /**< MSB is always set in valid signatures. */
99 uint8_t *sig_tbl; /**< Flat array of hash signature buckets. */
100 uint32_t sig_tbl_bucket_size; /**< Signature buckets may be padded for
101 alignment reasons, and this is the
102 bucket size used by sig_tbl. */
103 uint8_t *key_tbl; /**< Flat array of key value buckets. */
104 uint32_t key_tbl_key_size; /**< Keys may be padded for alignment
105 reasons, and this is the key size
110 * Create a new hash table.
113 * Parameters used to create and initialise the hash table.
115 * Pointer to hash table structure that is used in future hash table
116 * operations, or NULL on error, with error code set in rte_errno.
117 * Possible rte_errno errors include:
118 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
119 * - E_RTE_SECONDARY - function was called from a secondary process instance
120 * - ENOENT - missing entry
121 * - EINVAL - invalid parameter passed to function
122 * - ENOSPC - the maximum number of memzones has already been allocated
123 * - EEXIST - a memzone with the same name already exists
124 * - ENOMEM - no appropriate memory area found in which to create memzone
127 rte_hash_create(const struct rte_hash_parameters *params);
131 * Find an existing hash table object and return a pointer to it.
134 * Name of the hash table as passed to rte_hash_create()
136 * Pointer to hash table or NULL if object not found
137 * with rte_errno set appropriately. Possible rte_errno values include:
138 * - ENOENT - value not available for return
141 rte_hash_find_existing(const char *name);
144 * De-allocate all memory used by hash table.
149 rte_hash_free(struct rte_hash *h);
152 * Add a key to an existing hash table. This operation is not multi-thread safe
153 * and should only be called from one thread.
156 * Hash table to add the key to.
158 * Key to add to the hash table.
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(const struct rte_hash *h, const void *key);
169 * Add a key to an existing hash table. This operation is not multi-thread safe
170 * and should only be called from one thread.
173 * Hash table to add the key to.
175 * Key to add to the hash table.
177 * Hash value to add to the hash table.
179 * - -EINVAL if the parameters are invalid.
180 * - -ENOSPC if there is no space in the hash for this key.
181 * - A positive value that can be used by the caller as an offset into an
182 * array of user data. This value is unique for this key.
185 rte_hash_add_key_with_hash(const struct rte_hash *h,
186 const void *key, hash_sig_t sig);
189 * Remove a key from an existing hash table. This operation is not multi-thread
190 * safe 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 * - -EINVAL if the parameters are invalid.
198 * - -ENOENT if the key is not found.
199 * - A positive value that can be used by the caller as an offset into an
200 * array of user data. This value is unique for this key, and is the same
201 * value that was returned when the key was added.
204 rte_hash_del_key(const struct rte_hash *h, const void *key);
207 * Remove a key from an existing hash table. This operation is not multi-thread
208 * safe and should only be called from one thread.
211 * Hash table to remove the key from.
213 * Key to remove from the hash table.
215 * Hash value to remove from the hash table.
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_del_key_with_hash(const struct rte_hash *h,
225 const void *key, hash_sig_t sig);
229 * Find a key in the hash table. This operation is multi-thread safe.
232 * Hash table to look in.
236 * - -EINVAL if the parameters are invalid.
237 * - -ENOENT if the key is not found.
238 * - A positive value that can be used by the caller as an offset into an
239 * array of user data. This value is unique for this key, and is the same
240 * value that was returned when the key was added.
243 rte_hash_lookup(const struct rte_hash *h, const void *key);
246 * Find a key in the hash table. This operation is multi-thread safe.
249 * Hash table to look in.
253 * Hash value to find.
255 * - -EINVAL if the parameters are invalid.
256 * - -ENOENT if the key is not found.
257 * - A positive value that can be used by the caller as an offset into an
258 * array of user data. This value is unique for this key, and is the same
259 * value that was returned when the key was added.
262 rte_hash_lookup_with_hash(const struct rte_hash *h,
263 const void *key, hash_sig_t sig);
267 * Calc a hash value by key. This operation is not multi-process safe.
270 * Hash table to look in.
276 static inline hash_sig_t
277 rte_hash_hash(const struct rte_hash *h, const void *key)
279 /* calc hash result by key */
280 return h->hash_func(key, h->key_len, h->hash_func_init_val);
283 #define rte_hash_lookup_multi rte_hash_lookup_bulk
285 * Find multiple keys in the hash table. This operation is multi-thread safe.
288 * Hash table to look in.
290 * A pointer to a list of keys to look for.
292 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
294 * Output containing a list of values, corresponding to the list of keys that
295 * can be used by the caller as an offset into an array of user data. These
296 * values are unique for each key, and are the same values that were returned
297 * when each key was added. If a key in the list was not found, then -ENOENT
300 * -EINVAL if there's an error, otherwise 0.
303 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
304 uint32_t num_keys, int32_t *positions);
309 #endif /* _RTE_HASH_H_ */