da0a00ae39d602a3a4a8c11805747b08bab84571
[dpdk.git] / lib / librte_hash / rte_hash.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 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
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 /** Maximum size of hash table that can be created. */
50 #define RTE_HASH_ENTRIES_MAX                    (1 << 26)
51
52 /** Maximum bucket size that can be created. */
53 #define RTE_HASH_BUCKET_ENTRIES_MAX             16
54
55 /** Maximum length of key that can be used. */
56 #define RTE_HASH_KEY_LENGTH_MAX                 64
57
58 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
59 #define RTE_HASH_LOOKUP_BULK_MAX                16
60 #define RTE_HASH_LOOKUP_MULTI_MAX               RTE_HASH_LOOKUP_BULK_MAX
61
62 /** Max number of characters in hash name.*/
63 #define RTE_HASH_NAMESIZE                       32
64
65 /** Signature of key that is stored internally. */
66 typedef uint32_t hash_sig_t;
67
68 /** Type of function that can be used for calculating the hash value. */
69 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
70                                       uint32_t init_val);
71
72 /**
73  * Parameters used when creating the hash table. The total table entries and
74  * bucket entries must be a power of 2.
75  */
76 struct rte_hash_parameters {
77         const char *name;               /**< Name of the hash. */
78         uint32_t entries;               /**< Total hash table entries. */
79         uint32_t bucket_entries;        /**< Bucket entries. */
80         uint32_t key_len;               /**< Length of hash key. */
81         rte_hash_function hash_func;    /**< Function used to calculate hash. */
82         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
83         int socket_id;                  /**< NUMA Socket ID for memory. */
84 };
85
86 /** @internal A hash table structure. */
87 struct rte_hash;
88
89 /**
90  * Create a new hash table.
91  *
92  * @param params
93  *   Parameters used to create and initialise the hash table.
94  * @return
95  *   Pointer to hash table structure that is used in future hash table
96  *   operations, or NULL on error, with error code set in rte_errno.
97  *   Possible rte_errno errors include:
98  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
99  *    - E_RTE_SECONDARY - function was called from a secondary process instance
100  *    - ENOENT - missing entry
101  *    - EINVAL - invalid parameter passed to function
102  *    - ENOSPC - the maximum number of memzones has already been allocated
103  *    - EEXIST - a memzone with the same name already exists
104  *    - ENOMEM - no appropriate memory area found in which to create memzone
105  */
106 struct rte_hash *
107 rte_hash_create(const struct rte_hash_parameters *params);
108
109
110 /**
111  * Find an existing hash table object and return a pointer to it.
112  *
113  * @param name
114  *   Name of the hash table as passed to rte_hash_create()
115  * @return
116  *   Pointer to hash table or NULL if object not found
117  *   with rte_errno set appropriately. Possible rte_errno values include:
118  *    - ENOENT - value not available for return
119  */
120 struct rte_hash *
121 rte_hash_find_existing(const char *name);
122
123 /**
124  * De-allocate all memory used by hash table.
125  * @param h
126  *   Hash table to free
127  */
128 void
129 rte_hash_free(struct rte_hash *h);
130
131 /**
132  * Add a key to an existing hash table. This operation is not multi-thread safe
133  * and should only be called from one thread.
134  *
135  * @param h
136  *   Hash table to add the key to.
137  * @param key
138  *   Key to add to the hash table.
139  * @return
140  *   - -EINVAL if the parameters are invalid.
141  *   - -ENOSPC if there is no space in the hash for this key.
142  *   - A positive value that can be used by the caller as an offset into an
143  *     array of user data. This value is unique for this key.
144  */
145 int32_t
146 rte_hash_add_key(const struct rte_hash *h, const void *key);
147
148 /**
149  * Add a key to an existing hash table. This operation is not multi-thread safe
150  * and should only be called from one thread.
151  *
152  * @param h
153  *   Hash table to add the key to.
154  * @param key
155  *   Key to add to the hash table.
156  * @param sig
157  *   Hash value to add to the hash table.
158  * @return
159  *   - -EINVAL if the parameters are invalid.
160  *   - -ENOSPC if there is no space in the hash for this key.
161  *   - A positive value that can be used by the caller as an offset into an
162  *     array of user data. This value is unique for this key.
163  */
164 int32_t
165 rte_hash_add_key_with_hash(const struct rte_hash *h,
166                                 const void *key, hash_sig_t sig);
167
168 /**
169  * Remove a key from an existing hash table. This operation is not multi-thread
170  * safe and should only be called from one thread.
171  *
172  * @param h
173  *   Hash table to remove the key from.
174  * @param key
175  *   Key to remove from the hash table.
176  * @return
177  *   - -EINVAL if the parameters are invalid.
178  *   - -ENOENT if the key is not found.
179  *   - A positive value that can be used by the caller as an offset into an
180  *     array of user data. This value is unique for this key, and is the same
181  *     value that was returned when the key was added.
182  */
183 int32_t
184 rte_hash_del_key(const struct rte_hash *h, const void *key);
185
186 /**
187  * Remove a key from an existing hash table. This operation is not multi-thread
188  * safe and should only be called from one thread.
189  *
190  * @param h
191  *   Hash table to remove the key from.
192  * @param key
193  *   Key to remove from the hash table.
194  * @param sig
195  *   Hash value to remove from the hash table.
196  * @return
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.
202  */
203 int32_t
204 rte_hash_del_key_with_hash(const struct rte_hash *h,
205                                 const void *key, hash_sig_t sig);
206
207
208 /**
209  * Find a key in the hash table. This operation is multi-thread safe.
210  *
211  * @param h
212  *   Hash table to look in.
213  * @param key
214  *   Key to find.
215  * @return
216  *   - -EINVAL if the parameters are invalid.
217  *   - -ENOENT if the key is not found.
218  *   - A positive value that can be used by the caller as an offset into an
219  *     array of user data. This value is unique for this key, and is the same
220  *     value that was returned when the key was added.
221  */
222 int32_t
223 rte_hash_lookup(const struct rte_hash *h, const void *key);
224
225 /**
226  * Find a key in the hash table. This operation is multi-thread safe.
227  *
228  * @param h
229  *   Hash table to look in.
230  * @param key
231  *   Key to find.
232  * @param sig
233  *   Hash value to find.
234  * @return
235  *   - -EINVAL if the parameters are invalid.
236  *   - -ENOENT if the key is not found.
237  *   - A positive value that can be used by the caller as an offset into an
238  *     array of user data. This value is unique for this key, and is the same
239  *     value that was returned when the key was added.
240  */
241 int32_t
242 rte_hash_lookup_with_hash(const struct rte_hash *h,
243                                 const void *key, hash_sig_t sig);
244
245 /**
246  * Calc a hash value by key. This operation is not multi-process safe.
247  *
248  * @param h
249  *   Hash table to look in.
250  * @param key
251  *   Key to find.
252  * @return
253  *   - hash value
254  */
255 hash_sig_t
256 rte_hash_hash(const struct rte_hash *h, const void *key);
257
258 #define rte_hash_lookup_multi rte_hash_lookup_bulk
259 /**
260  * Find multiple keys in the hash table. This operation is multi-thread safe.
261  *
262  * @param h
263  *   Hash table to look in.
264  * @param keys
265  *   A pointer to a list of keys to look for.
266  * @param num_keys
267  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
268  * @param positions
269  *   Output containing a list of values, corresponding to the list of keys that
270  *   can be used by the caller as an offset into an array of user data. These
271  *   values are unique for each key, and are the same values that were returned
272  *   when each key was added. If a key in the list was not found, then -ENOENT
273  *   will be the value.
274  * @return
275  *   -EINVAL if there's an error, otherwise 0.
276  */
277 int
278 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
279                       uint32_t num_keys, int32_t *positions);
280 #ifdef __cplusplus
281 }
282 #endif
283
284 #endif /* _RTE_HASH_H_ */