2ecaf1ad1994c072a8067b8cd03209ded4c4f84e
[dpdk.git] / lib / librte_hash / rte_hash.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #ifndef _RTE_HASH_H_
35 #define _RTE_HASH_H_
36
37 /**
38  * @file
39  *
40  * RTE Hash Table
41  */
42
43 #include <stdint.h>
44 #include <sys/queue.h>
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 /** Maximum size of hash table that can be created. */
51 #define RTE_HASH_ENTRIES_MAX                    (1 << 26)
52
53 /** Maximum bucket size that can be created. */
54 #define RTE_HASH_BUCKET_ENTRIES_MAX             16
55
56 /** Maximum length of key that can be used. */
57 #define RTE_HASH_KEY_LENGTH_MAX                 64
58
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
62
63 /** Max number of characters in hash name.*/
64 #define RTE_HASH_NAMESIZE                       32
65
66 /** Signature of key that is stored internally. */
67 typedef uint32_t hash_sig_t;
68
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,
71                                       uint32_t init_val);
72
73 /**
74  * Parameters used when creating the hash table. The total table entries and
75  * bucket entries must be a power of 2.
76  */
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. */
85 };
86
87 /** A hash table structure. */
88 struct rte_hash {
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
106                                            used by key_tbl. */
107 };
108
109 /**
110  * Create a new hash table.
111  *
112  * @param params
113  *   Parameters used to create and initialise the hash table.
114  * @return
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  *    - E_RTE_NO_TAILQ - no tailq list could be got for the hash table list
121  *    - ENOENT - missing entry
122  *    - EINVAL - invalid parameter passed to function
123  *    - ENOSPC - the maximum number of memzones has already been allocated
124  *    - EEXIST - a memzone with the same name already exists
125  *    - ENOMEM - no appropriate memory area found in which to create memzone
126  */
127 struct rte_hash *
128 rte_hash_create(const struct rte_hash_parameters *params);
129
130
131 /**
132  * Find an existing hash table object and return a pointer to it.
133  *
134  * @param name
135  *   Name of the hash table as passed to rte_hash_create()
136  * @return
137  *   Pointer to hash table or NULL if object not found
138  *   with rte_errno set appropriately. Possible rte_errno values include:
139  *    - ENOENT - value not available for return
140  */
141 struct rte_hash *
142 rte_hash_find_existing(const char *name);
143
144 /**
145  * De-allocate all memory used by hash table.
146  * @param h
147  *   Hash table to free
148  */
149 void
150 rte_hash_free(struct rte_hash *h);
151
152 /**
153  * Add a key to an existing hash table. This operation is not multi-thread safe
154  * and should only be called from one thread.
155  *
156  * @param h
157  *   Hash table to add the key to.
158  * @param key
159  *   Key to add to the hash table.
160  * @return
161  *   - -EINVAL if the parameters are invalid.
162  *   - -ENOSPC if there is no space in the hash for this key.
163  *   - A positive value that can be used by the caller as an offset into an
164  *     array of user data. This value is unique for this key.
165  */
166 int32_t
167 rte_hash_add_key(const struct rte_hash *h, const void *key);
168
169 /**
170  * Add a key to an existing hash table. This operation is not multi-thread safe
171  * and should only be called from one thread.
172  *
173  * @param h
174  *   Hash table to add the key to.
175  * @param key
176  *   Key to add to the hash table.
177  * @param sig
178  *   Hash value to add to the hash table.
179  * @return
180  *   - -EINVAL if the parameters are invalid.
181  *   - -ENOSPC if there is no space in the hash for this key.
182  *   - A positive value that can be used by the caller as an offset into an
183  *     array of user data. This value is unique for this key.
184  */
185 int32_t
186 rte_hash_add_key_with_hash(const struct rte_hash *h,
187                                 const void *key, hash_sig_t sig);
188
189 /**
190  * Remove a key from an existing hash table. This operation is not multi-thread
191  * safe and should only be called from one thread.
192  *
193  * @param h
194  *   Hash table to remove the key from.
195  * @param key
196  *   Key to remove from the hash table.
197  * @return
198  *   - -EINVAL if the parameters are invalid.
199  *   - -ENOENT if the key is not found.
200  *   - A positive value that can be used by the caller as an offset into an
201  *     array of user data. This value is unique for this key, and is the same
202  *     value that was returned when the key was added.
203  */
204 int32_t
205 rte_hash_del_key(const struct rte_hash *h, const void *key);
206
207 /**
208  * Remove a key from an existing hash table. This operation is not multi-thread
209  * safe and should only be called from one thread.
210  *
211  * @param h
212  *   Hash table to remove the key from.
213  * @param key
214  *   Key to remove from the hash table.
215  * @param sig
216  *   Hash value to remove from the hash table.
217  * @return
218  *   - -EINVAL if the parameters are invalid.
219  *   - -ENOENT if the key is not found.
220  *   - A positive value that can be used by the caller as an offset into an
221  *     array of user data. This value is unique for this key, and is the same
222  *     value that was returned when the key was added.
223  */
224 int32_t
225 rte_hash_del_key_with_hash(const struct rte_hash *h,
226                                 const void *key, hash_sig_t sig);
227
228
229 /**
230  * Find a key in the hash table. This operation is multi-thread safe.
231  *
232  * @param h
233  *   Hash table to look in.
234  * @param key
235  *   Key to find.
236  * @return
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.
242  */
243 int32_t
244 rte_hash_lookup(const struct rte_hash *h, const void *key);
245
246 /**
247  * Find a key in the hash table. This operation is multi-thread safe.
248  *
249  * @param h
250  *   Hash table to look in.
251  * @param key
252  *   Key to find.
253  * @param sig
254  *   Hash value to find.
255  * @return
256  *   - -EINVAL if the parameters are invalid.
257  *   - -ENOENT if the key is not found.
258  *   - A positive value that can be used by the caller as an offset into an
259  *     array of user data. This value is unique for this key, and is the same
260  *     value that was returned when the key was added.
261  */
262 int32_t
263 rte_hash_lookup_with_hash(const struct rte_hash *h,
264                                 const void *key, hash_sig_t sig);
265
266
267 /**
268  * Calc a hash value by key. This operation is not multi-process safe.
269  *
270  * @param h
271  *   Hash table to look in.
272  * @param key
273  *   Key to find.
274  * @return
275  *   - hash value
276  */
277 static inline hash_sig_t
278 rte_hash_hash(const struct rte_hash *h, const void *key)
279 {
280         /* calc hash result by key */
281         return h->hash_func(key, h->key_len, h->hash_func_init_val);
282 }
283
284 #define rte_hash_lookup_multi rte_hash_lookup_bulk
285 /**
286  * Find multiple keys in the hash table. This operation is multi-thread safe.
287  *
288  * @param h
289  *   Hash table to look in.
290  * @param keys
291  *   A pointer to a list of keys to look for.
292  * @param num_keys
293  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
294  * @param positions
295  *   Output containing a list of values, corresponding to the list of keys that
296  *   can be used by the caller as an offset into an array of user data. These
297  *   values are unique for each key, and are the same values that were returned
298  *   when each key was added. If a key in the list was not found, then -ENOENT
299  *   will be the value.
300  * @return
301  *   -EINVAL if there's an error, otherwise 0.
302  */
303 int
304 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
305                       uint32_t num_keys, int32_t *positions);
306 #ifdef __cplusplus
307 }
308 #endif
309
310 #endif /* _RTE_HASH_H_ */