4 * Copyright(c) 2010-2013 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.
45 #include <sys/queue.h>
51 /** Maximum size of hash table that can be created. */
52 #define RTE_HASH_ENTRIES_MAX (1 << 26)
54 /** Maximum bucket size that can be created. */
55 #define RTE_HASH_BUCKET_ENTRIES_MAX 16
57 /** Maximum length of key that can be used. */
58 #define RTE_HASH_KEY_LENGTH_MAX 64
60 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
61 #define RTE_HASH_LOOKUP_BULK_MAX 16
62 #define RTE_HASH_LOOKUP_MULTI_MAX RTE_HASH_LOOKUP_BULK_MAX
64 /** Max number of characters in hash name.*/
65 #define RTE_HASH_NAMESIZE 32
67 /** Signature of key that is stored internally. */
68 typedef uint32_t hash_sig_t;
70 /** Type of function that can be used for calculating the hash value. */
71 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
75 * Parameters used when creating the hash table. The total table entries and
76 * bucket entries must be a power of 2.
78 struct rte_hash_parameters {
79 const char *name; /**< Name of the hash. */
80 uint32_t entries; /**< Total hash table entries. */
81 uint32_t bucket_entries; /**< Bucket entries. */
82 uint32_t key_len; /**< Length of hash key. */
83 rte_hash_function hash_func; /**< Function used to calculate hash. */
84 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
85 int socket_id; /**< NUMA Socket ID for memory. */
88 /** A hash table structure. */
90 TAILQ_ENTRY(rte_hash) next;/**< Next in list. */
92 char name[RTE_HASH_NAMESIZE]; /**< Name of the hash. */
93 uint32_t entries; /**< Total table entries. */
94 uint32_t bucket_entries; /**< Bucket entries. */
95 uint32_t key_len; /**< Length of hash key. */
96 rte_hash_function hash_func; /**< Function used to calculate hash. */
97 uint32_t hash_func_init_val; /**< Init value used by hash_func. */
98 uint32_t num_buckets; /**< Number of buckets in table. */
99 uint32_t bucket_bitmask; /**< Bitmask for getting bucket index
100 from hash signature. */
101 hash_sig_t sig_msb; /**< MSB is always set in valid signatures. */
102 uint8_t *sig_tbl; /**< Flat array of hash signature buckets. */
103 uint32_t sig_tbl_bucket_size; /**< Signature buckets may be padded for
104 alignment reasons, and this is the
105 bucket size used by sig_tbl. */
106 uint8_t *key_tbl; /**< Flat array of key value buckets. */
107 uint32_t key_tbl_key_size; /**< Keys may be padded for alignment
108 reasons, and this is the key size
113 * Create a new hash table.
116 * Parameters used to create and initialise the hash table.
118 * Pointer to hash table structure that is used in future hash table
119 * operations, or NULL on error, with error code set in rte_errno.
120 * Possible rte_errno errors include:
121 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
122 * - E_RTE_SECONDARY - function was called from a secondary process instance
123 * - E_RTE_NO_TAILQ - no tailq list could be got for the hash table list
124 * - ENOENT - missing entry
125 * - EINVAL - invalid parameter passed to function
126 * - ENOSPC - the maximum number of memzones has already been allocated
127 * - EEXIST - a memzone with the same name already exists
128 * - ENOMEM - no appropriate memory area found in which to create memzone
131 rte_hash_create(const struct rte_hash_parameters *params);
135 * Find an existing hash table object and return a pointer to it.
138 * Name of the hash table as passed to rte_hash_create()
140 * Pointer to hash table or NULL if object not found
141 * with rte_errno set appropriately. Possible rte_errno values include:
142 * - ENOENT - value not available for return
145 rte_hash_find_existing(const char *name);
148 * De-allocate all memory used by hash table.
153 rte_hash_free(struct rte_hash *h);
156 * Add a key to an existing hash table. This operation is not multi-thread safe
157 * and should only be called from one thread.
160 * Hash table to add the key to.
162 * Key to add to the hash table.
164 * - -EINVAL if the parameters are invalid.
165 * - -ENOSPC if there is no space in the hash for this key.
166 * - A positive value that can be used by the caller as an offset into an
167 * array of user data. This value is unique for this key.
170 rte_hash_add_key(const struct rte_hash *h, const void *key);
173 * Add a key to an existing hash table. 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 * Hash value 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_with_hash(const struct rte_hash *h,
190 const void *key, hash_sig_t sig);
193 * Remove a key from an existing hash table. This operation is not multi-thread
194 * safe and should only be called from one thread.
197 * Hash table to remove the key from.
199 * Key to remove from the hash table.
201 * - -EINVAL if the parameters are invalid.
202 * - -ENOENT if the key is not found.
203 * - A positive value that can be used by the caller as an offset into an
204 * array of user data. This value is unique for this key, and is the same
205 * value that was returned when the key was added.
208 rte_hash_del_key(const struct rte_hash *h, const void *key);
211 * Remove a key from an existing hash table. This operation is not multi-thread
212 * safe and should only be called from one thread.
215 * Hash table to remove the key from.
217 * Key to remove from the hash table.
219 * Hash value 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_with_hash(const struct rte_hash *h,
229 const void *key, hash_sig_t sig);
233 * Find a key in the hash table. This operation is multi-thread safe.
236 * Hash table to look in.
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_lookup(const struct rte_hash *h, const void *key);
250 * Find a key in the hash table. This operation is multi-thread safe.
253 * Hash table to look in.
257 * Hash value to find.
259 * - -EINVAL if the parameters are invalid.
260 * - -ENOENT if the key is not found.
261 * - A positive value that can be used by the caller as an offset into an
262 * array of user data. This value is unique for this key, and is the same
263 * value that was returned when the key was added.
266 rte_hash_lookup_with_hash(const struct rte_hash *h,
267 const void *key, hash_sig_t sig);
271 * Calc a hash value by key. This operation is not multi-process safe.
274 * Hash table to look in.
280 static inline hash_sig_t
281 rte_hash_hash(const struct rte_hash *h, const void *key)
283 /* calc hash result by key */
284 return h->hash_func(key, h->key_len, h->hash_func_init_val);
287 #define rte_hash_lookup_multi rte_hash_lookup_bulk
289 * Find multiple keys in the hash table. This operation is multi-thread safe.
292 * Hash table to look in.
294 * A pointer to a list of keys to look for.
296 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
298 * Output containing a list of values, corresponding to the list of keys that
299 * can be used by the caller as an offset into an array of user data. These
300 * values are unique for each key, and are the same values that were returned
301 * when each key was added. If a key in the list was not found, then -ENOENT
304 * -EINVAL if there's an error, otherwise 0.
307 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
308 uint32_t num_keys, int32_t *positions);
313 #endif /* _RTE_HASH_H_ */