4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
46 #include <sys/queue.h>
52 /** Maximum size of hash table that can be created. */
53 #define RTE_HASH_ENTRIES_MAX (1 << 26)
55 /** Maximum bucket size that can be created. */
56 #define RTE_HASH_BUCKET_ENTRIES_MAX 16
58 /** Maximum length of key that can be used. */
59 #define RTE_HASH_KEY_LENGTH_MAX 64
61 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
62 #define RTE_HASH_LOOKUP_MULTI_MAX 16
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. If RTE_LIBRTE_HASH_USE_MEMZONE is defined, then
114 * the hash table is allocated in a memzone on a specific NUMA socket ID,
115 * otherwise it is allocated in the heap.
118 * Parameters used to create and initialise the hash table.
120 * Pointer to hash table structure that is used in future hash table
121 * operations, or NULL on error, with error code set in rte_errno.
122 * Possible rte_errno errors include:
123 * - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
124 * - E_RTE_SECONDARY - function was called from a secondary process instance
125 * - E_RTE_NO_TAILQ - no tailq list could be got for the hash table list
126 * - ENOENT - missing entry
127 * - EINVAL - invalid parameter passed to function
128 * - ENOSPC - the maximum number of memzones has already been allocated
129 * - EEXIST - a memzone with the same name already exists
130 * - ENOMEM - no appropriate memory area found in which to create memzone
133 rte_hash_create(const struct rte_hash_parameters *params);
137 * Find an existing hash table object and return a pointer to it.
140 * Name of the hash table as passed to rte_hash_create()
142 * Pointer to hash table or NULL if object not found
143 * with rte_errno set appropriately. Possible rte_errno values include:
144 * - ENOENT - value not available for return
147 rte_hash_find_existing(const char *name);
150 * De-allocate all memory used by hash table. If RTE_LIBRTE_HASH_USE_MEMZONE
151 * is defined, then this has no effect.
156 rte_hash_free(struct rte_hash *h);
159 * Add a key to an existing hash table. This operation is not multi-thread safe
160 * and should only be called from one thread.
163 * Hash table to add the key to.
165 * Key to add to the hash table.
167 * - -EINVAL if the parameters are invalid.
168 * - -ENOSPC if there is no space in the hash for this key.
169 * - A positive value that can be used by the caller as an offset into an
170 * array of user data. This value is unique for this key.
173 rte_hash_add_key(const struct rte_hash *h, const void *key);
176 * Remove a key from an existing hash table. This operation is not multi-thread
177 * safe and should only be called from one thread.
180 * Hash table to remove the key from.
182 * Key to remove from the hash table.
184 * - -EINVAL if the parameters are invalid.
185 * - -ENOENT if the key is not found.
186 * - A positive value that can be used by the caller as an offset into an
187 * array of user data. This value is unique for this key, and is the same
188 * value that was returned when the key was added.
191 rte_hash_del_key(const struct rte_hash *h, const void *key);
194 * Find a key in the hash table. This operation is multi-thread safe.
197 * Hash table to look in.
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_lookup(const struct rte_hash *h, const void *key);
211 * Find multiple keys in the hash table. This operation is multi-thread safe.
214 * Hash table to look in.
216 * A pointer to a list of keys to look for.
218 * How many keys are in the keys list (less than RTE_HASH_LOOKUP_MULTI_MAX).
220 * Output containing a list of values, corresponding to the list of keys that
221 * can be used by the caller as an offset into an array of user data. These
222 * values are unique for each key, and are the same values that were returned
223 * when each key was added. If a key in the list was not found, then -ENOENT
226 * -EINVAL if there's an error, otherwise 0.
229 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
230 uint32_t num_keys, int32_t *positions);
236 #endif /* _RTE_HASH_H_ */