bf39d330891938501ae4ec83db4f2d21a0144218
[dpdk.git] / lib / librte_hash / rte_hash.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34
35 #ifndef _RTE_HASH_H_
36 #define _RTE_HASH_H_
37
38 /**
39  * @file
40  *
41  * RTE Hash Table
42  */
43
44 #include <stdint.h>
45 #include <sys/queue.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /** Maximum size of hash table that can be created. */
52 #define RTE_HASH_ENTRIES_MAX                    (1 << 26)
53
54 /** Maximum bucket size that can be created. */
55 #define RTE_HASH_BUCKET_ENTRIES_MAX             16
56
57 /** Maximum length of key that can be used. */
58 #define RTE_HASH_KEY_LENGTH_MAX                 64
59
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
63
64 /** Max number of characters in hash name.*/
65 #define RTE_HASH_NAMESIZE                       32
66
67 /** Signature of key that is stored internally. */
68 typedef uint32_t hash_sig_t;
69
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,
72                                       uint32_t init_val);
73
74 /**
75  * Parameters used when creating the hash table. The total table entries and
76  * bucket entries must be a power of 2.
77  */
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. */
86 };
87
88 /** A hash table structure. */
89 struct rte_hash {
90         TAILQ_ENTRY(rte_hash) next;/**< Next in list. */
91
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
109                                            used by key_tbl. */
110 };
111
112 /**
113  * Create a new hash table.
114  *
115  * @param params
116  *   Parameters used to create and initialise the hash table.
117  * @return
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
129  */
130 struct rte_hash *
131 rte_hash_create(const struct rte_hash_parameters *params);
132
133
134 /**
135  * Find an existing hash table object and return a pointer to it.
136  *
137  * @param name
138  *   Name of the hash table as passed to rte_hash_create()
139  * @return
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
143  */
144 struct rte_hash *
145 rte_hash_find_existing(const char *name);
146
147 /**
148  * De-allocate all memory used by hash table.
149  * @param h
150  *   Hash table to free
151  */
152 void
153 rte_hash_free(struct rte_hash *h);
154
155 /**
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.
158  *
159  * @param h
160  *   Hash table to add the key to.
161  * @param key
162  *   Key to add to the hash table.
163  * @return
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.
168  */
169 int32_t
170 rte_hash_add_key(const struct rte_hash *h, const void *key);
171
172 /**
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.
175  *
176  * @param h
177  *   Hash table to add the key to.
178  * @param key
179  *   Key to add to the hash table.
180  * @param sig
181  *   Hash value to add to the hash table.
182  * @return
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.
187  */
188 int32_t
189 rte_hash_add_key_with_hash(const struct rte_hash *h, 
190                                 const void *key, hash_sig_t sig);
191
192 /**
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.
195  *
196  * @param h
197  *   Hash table to remove the key from.
198  * @param key
199  *   Key to remove from the hash table.
200  * @return
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.
206  */
207 int32_t
208 rte_hash_del_key(const struct rte_hash *h, const void *key);
209
210 /**
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.
213  *
214  * @param h
215  *   Hash table to remove the key from.
216  * @param key
217  *   Key to remove from the hash table.
218  * @param sig
219  *   Hash value to remove from the hash table.
220  * @return
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.
226  */
227 int32_t
228 rte_hash_del_key_with_hash(const struct rte_hash *h, 
229                                 const void *key, hash_sig_t sig);
230
231
232 /**
233  * Find a key in the hash table. This operation is multi-thread safe.
234  *
235  * @param h
236  *   Hash table to look in.
237  * @param key
238  *   Key to find.
239  * @return
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.
245  */
246 int32_t
247 rte_hash_lookup(const struct rte_hash *h, const void *key);
248
249 /**
250  * Find a key in the hash table. This operation is multi-thread safe.
251  *
252  * @param h
253  *   Hash table to look in.
254  * @param key
255  *   Key to find.
256  * @param sig
257  *   Hash value to find.
258  * @return
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.
264  */
265 int32_t
266 rte_hash_lookup_with_hash(const struct rte_hash *h, 
267                                 const void *key, hash_sig_t sig);
268
269
270 /**
271  * Calc a hash value by key. This operation is not multi-process safe.
272  *
273  * @param h
274  *   Hash table to look in.
275  * @param key
276  *   Key to find.
277  * @return
278  *   - hash value
279  */
280 static inline hash_sig_t
281 rte_hash_hash(const struct rte_hash *h, const void *key)
282 {
283         /* calc hash result by key */
284         return h->hash_func(key, h->key_len, h->hash_func_init_val);
285 }
286
287 #define rte_hash_lookup_multi rte_hash_lookup_bulk
288 /**
289  * Find multiple keys in the hash table. This operation is multi-thread safe.
290  *
291  * @param h
292  *   Hash table to look in.
293  * @param keys
294  *   A pointer to a list of keys to look for.
295  * @param num_keys
296  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
297  * @param positions
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
302  *   will be the value.
303  * @return
304  *   -EINVAL if there's an error, otherwise 0.
305  */
306 int
307 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
308                       uint32_t num_keys, int32_t *positions);
309 #ifdef __cplusplus
310 }
311 #endif
312
313 #endif /* _RTE_HASH_H_ */