hash: add reset function
[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 << 30)
51
52 /** @deprecated Maximum bucket size that can be created. */
53 #define RTE_HASH_BUCKET_ENTRIES_MAX             4
54
55 /** @deprecated Maximum length of key that can be used. */
56 #define RTE_HASH_KEY_LENGTH_MAX                 64
57
58 /** Maximum number of characters in hash name.*/
59 #define RTE_HASH_NAMESIZE                       32
60
61 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
62 #define RTE_HASH_LOOKUP_BULK_MAX                64
63 #define RTE_HASH_LOOKUP_MULTI_MAX               RTE_HASH_LOOKUP_BULK_MAX
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.
74  */
75 struct rte_hash_parameters {
76         const char *name;               /**< Name of the hash. */
77         uint32_t entries;               /**< Total hash table entries. */
78         uint32_t bucket_entries;        /**< Bucket entries. */
79         uint32_t key_len;               /**< Length of hash key. */
80         rte_hash_function hash_func;    /**< Primary Hash function used to calculate hash. */
81         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
82         int socket_id;                  /**< NUMA Socket ID for memory. */
83 };
84
85 /** @internal A hash table structure. */
86 struct rte_hash;
87
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  * Find an existing hash table object and return a pointer to it.
111  *
112  * @param name
113  *   Name of the hash table as passed to rte_hash_create()
114  * @return
115  *   Pointer to hash table or NULL if object not found
116  *   with rte_errno set appropriately. Possible rte_errno values include:
117  *    - ENOENT - value not available for return
118  */
119 struct rte_hash *
120 rte_hash_find_existing(const char *name);
121
122 /**
123  * De-allocate all memory used by hash table.
124  * @param h
125  *   Hash table to free
126  */
127 void
128 rte_hash_free(struct rte_hash *h);
129
130 /**
131  * Reset all hash structure, by zeroing all entries
132  * @param h
133  *   Hash table to reset
134  */
135 void
136 rte_hash_reset(struct rte_hash *h);
137
138 /**
139  * Add a key to an existing hash table. This operation is not multi-thread safe
140  * and should only be called from one thread.
141  *
142  * @param h
143  *   Hash table to add the key to.
144  * @param key
145  *   Key to add to the hash table.
146  * @return
147  *   - -EINVAL if the parameters are invalid.
148  *   - -ENOSPC if there is no space in the hash for this key.
149  *   - A positive value that can be used by the caller as an offset into an
150  *     array of user data. This value is unique for this key.
151  */
152 int32_t
153 rte_hash_add_key(const struct rte_hash *h, const void *key);
154
155 /**
156  * Add a key to an existing hash table.
157  * This operation is not multi-thread safe
158  * and should only be called from one thread.
159  *
160  * @param h
161  *   Hash table to add the key to.
162  * @param key
163  *   Key to add to the hash table.
164  * @param sig
165  *   Precomputed hash value for 'key'.
166  * @return
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.
171  */
172 int32_t
173 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
174
175 /**
176  * Remove a key from an existing hash table.
177  * This operation is not multi-thread safe
178  * and should only be called from one thread.
179  *
180  * @param h
181  *   Hash table to remove the key from.
182  * @param key
183  *   Key to remove from the hash table.
184  * @return
185  *   - -EINVAL if the parameters are invalid.
186  *   - -ENOENT if the key is not found.
187  *   - A positive value that can be used by the caller as an offset into an
188  *     array of user data. This value is unique for this key, and is the same
189  *     value that was returned when the key was added.
190  */
191 int32_t
192 rte_hash_del_key(const struct rte_hash *h, const void *key);
193
194 /**
195  * Remove a key from an existing hash table.
196  * This operation is not multi-thread safe
197  * and should only be called from one thread.
198  *
199  * @param h
200  *   Hash table to remove the key from.
201  * @param key
202  *   Key to remove from the hash table.
203  * @param sig
204  *   Precomputed hash value for 'key'.
205  * @return
206  *   - -EINVAL if the parameters are invalid.
207  *   - -ENOENT if the key is not found.
208  *   - A positive value that can be used by the caller as an offset into an
209  *     array of user data. This value is unique for this key, and is the same
210  *     value that was returned when the key was added.
211  */
212 int32_t
213 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
214
215 /**
216  * Find a key in the hash table.
217  * This operation is multi-thread safe.
218  *
219  * @param h
220  *   Hash table to look in.
221  * @param key
222  *   Key to find.
223  * @return
224  *   - -EINVAL if the parameters are invalid.
225  *   - -ENOENT if the key is not found.
226  *   - A positive value that can be used by the caller as an offset into an
227  *     array of user data. This value is unique for this key, and is the same
228  *     value that was returned when the key was added.
229  */
230 int32_t
231 rte_hash_lookup(const struct rte_hash *h, const void *key);
232
233 /**
234  * Find a key in the hash table.
235  * This operation is multi-thread safe.
236  *
237  * @param h
238  *   Hash table to look in.
239  * @param key
240  *   Key to find.
241  * @param sig
242  *   Hash value to remove from the hash table.
243  * @return
244  *   - -EINVAL if the parameters are invalid.
245  *   - -ENOENT if the key is not found.
246  *   - A positive value that can be used by the caller as an offset into an
247  *     array of user data. This value is unique for this key, and is the same
248  *     value that was returned when the key was added.
249  */
250 int32_t
251 rte_hash_lookup_with_hash(const struct rte_hash *h,
252                                 const void *key, hash_sig_t sig);
253
254 /**
255  * Calc a hash value by key.
256  * This operation is not multi-thread safe.
257  *
258  * @param h
259  *   Hash table to look in.
260  * @param key
261  *   Key to find.
262  * @return
263  *   - hash value
264  */
265 hash_sig_t
266 rte_hash_hash(const struct rte_hash *h, const void *key);
267
268 #define rte_hash_lookup_multi rte_hash_lookup_bulk
269 /**
270  * Find multiple keys in the hash table.
271  * This operation is multi-thread safe.
272  *
273  * @param h
274  *   Hash table to look in.
275  * @param keys
276  *   A pointer to a list of keys to look for.
277  * @param num_keys
278  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
279  * @param positions
280  *   Output containing a list of values, corresponding to the list of keys that
281  *   can be used by the caller as an offset into an array of user data. These
282  *   values are unique for each key, and are the same values that were returned
283  *   when each key was added. If a key in the list was not found, then -ENOENT
284  *   will be the value.
285  * @return
286  *   -EINVAL if there's an error, otherwise 0.
287  */
288 int
289 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
290                       uint32_t num_keys, int32_t *positions);
291
292 #ifdef __cplusplus
293 }
294 #endif
295
296 #endif /* _RTE_HASH_H_ */