ff75445651ca21b77bb2108da7c29490bceec517
[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         uint8_t extra_flag;             /**< Indicate if additional parameters are present. */
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  * 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-value pair to an existing hash table.
140  * This operation is not multi-thread safe
141  * and should only be called from one thread.
142  *
143  * @param h
144  *   Hash table to add the key to.
145  * @param key
146  *   Key to add to the hash table.
147  * @param data
148  *   Data to add to the hash table.
149  * @return
150  *   - 0 if added successfully
151  *   - -EINVAL if the parameters are invalid.
152  *   - -ENOSPC if there is no space in the hash for this key.
153  */
154 int
155 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
156
157 /**
158  * Add a key-value pair with a pre-computed hash value
159  * to an existing hash table.
160  * This operation is not multi-thread safe
161  * and should only be called from one thread.
162  *
163  * @param h
164  *   Hash table to add the key to.
165  * @param key
166  *   Key to add to the hash table.
167  * @param sig
168  *   Precomputed hash value for 'key'
169  * @param data
170  *   Data to add to the hash table.
171  * @return
172  *   - 0 if added successfully
173  *   - -EINVAL if the parameters are invalid.
174  *   - -ENOSPC if there is no space in the hash for this key.
175  */
176 int32_t
177 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
178                                                 hash_sig_t sig, void *data);
179
180 /**
181  * Add a key to an existing hash table. This operation is not multi-thread safe
182  * and should only be called from one thread.
183  *
184  * @param h
185  *   Hash table to add the key to.
186  * @param key
187  *   Key to add to the hash table.
188  * @return
189  *   - -EINVAL if the parameters are invalid.
190  *   - -ENOSPC if there is no space in the hash for this key.
191  *   - A positive value that can be used by the caller as an offset into an
192  *     array of user data. This value is unique for this key.
193  */
194 int32_t
195 rte_hash_add_key(const struct rte_hash *h, const void *key);
196
197 /**
198  * Add a key to an existing hash table.
199  * This operation is not multi-thread safe
200  * and should only be called from one thread.
201  *
202  * @param h
203  *   Hash table to add the key to.
204  * @param key
205  *   Key to add to the hash table.
206  * @param sig
207  *   Precomputed hash value for 'key'.
208  * @return
209  *   - -EINVAL if the parameters are invalid.
210  *   - -ENOSPC if there is no space in the hash for this key.
211  *   - A positive value that can be used by the caller as an offset into an
212  *     array of user data. This value is unique for this key.
213  */
214 int32_t
215 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
216
217 /**
218  * Remove a key from an existing hash table.
219  * This operation is not multi-thread safe
220  * and should only be called from one thread.
221  *
222  * @param h
223  *   Hash table to remove the key from.
224  * @param key
225  *   Key to remove from the hash table.
226  * @return
227  *   - -EINVAL if the parameters are invalid.
228  *   - -ENOENT if the key is not found.
229  *   - A positive value that can be used by the caller as an offset into an
230  *     array of user data. This value is unique for this key, and is the same
231  *     value that was returned when the key was added.
232  */
233 int32_t
234 rte_hash_del_key(const struct rte_hash *h, const void *key);
235
236 /**
237  * Remove a key from an existing hash table.
238  * This operation is not multi-thread safe
239  * and should only be called from one thread.
240  *
241  * @param h
242  *   Hash table to remove the key from.
243  * @param key
244  *   Key to remove from the hash table.
245  * @param sig
246  *   Precomputed hash value for 'key'.
247  * @return
248  *   - -EINVAL if the parameters are invalid.
249  *   - -ENOENT if the key is not found.
250  *   - A positive value that can be used by the caller as an offset into an
251  *     array of user data. This value is unique for this key, and is the same
252  *     value that was returned when the key was added.
253  */
254 int32_t
255 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
256
257
258 /**
259  * Find a key-value pair in the hash table.
260  * This operation is multi-thread safe.
261  *
262  * @param h
263  *   Hash table to look in.
264  * @param key
265  *   Key to find.
266  * @param data
267  *   Output with pointer to data returned from the hash table.
268  * @return
269  *   0 if successful lookup
270  *   - EINVAL if the parameters are invalid.
271  *   - ENOENT if the key is not found.
272  */
273 int
274 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
275
276 /**
277  * Find a key-value pair with a pre-computed hash value
278  * to an existing hash table.
279  * This operation is multi-thread safe.
280  *
281  * @param h
282  *   Hash table to look in.
283  * @param key
284  *   Key to find.
285  * @param sig
286  *   Precomputed hash value for 'key'
287  * @param data
288  *   Output with pointer to data returned from the hash table.
289  * @return
290  *   0 if successful lookup
291  *   - EINVAL if the parameters are invalid.
292  *   - ENOENT if the key is not found.
293  */
294 int
295 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
296                                         hash_sig_t sig, void **data);
297
298 /**
299  * Find a key in the hash table.
300  * This operation is multi-thread safe.
301  *
302  * @param h
303  *   Hash table to look in.
304  * @param key
305  *   Key to find.
306  * @return
307  *   - -EINVAL if the parameters are invalid.
308  *   - -ENOENT if the key is not found.
309  *   - A positive value that can be used by the caller as an offset into an
310  *     array of user data. This value is unique for this key, and is the same
311  *     value that was returned when the key was added.
312  */
313 int32_t
314 rte_hash_lookup(const struct rte_hash *h, const void *key);
315
316 /**
317  * Find a key in the hash table.
318  * This operation is multi-thread safe.
319  *
320  * @param h
321  *   Hash table to look in.
322  * @param key
323  *   Key to find.
324  * @param sig
325  *   Hash value to remove from the hash table.
326  * @return
327  *   - -EINVAL if the parameters are invalid.
328  *   - -ENOENT if the key is not found.
329  *   - A positive value that can be used by the caller as an offset into an
330  *     array of user data. This value is unique for this key, and is the same
331  *     value that was returned when the key was added.
332  */
333 int32_t
334 rte_hash_lookup_with_hash(const struct rte_hash *h,
335                                 const void *key, hash_sig_t sig);
336
337 /**
338  * Calc a hash value by key.
339  * This operation is not multi-thread safe.
340  *
341  * @param h
342  *   Hash table to look in.
343  * @param key
344  *   Key to find.
345  * @return
346  *   - hash value
347  */
348 hash_sig_t
349 rte_hash_hash(const struct rte_hash *h, const void *key);
350
351 #define rte_hash_lookup_multi rte_hash_lookup_bulk
352 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
353 /**
354  * Find multiple keys in the hash table.
355  * This operation is multi-thread safe.
356  *
357  * @param h
358  *   Hash table to look in.
359  * @param keys
360  *   A pointer to a list of keys to look for.
361  * @param num_keys
362  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
363  * @param hit_mask
364  *   Output containing a bitmask with all successful lookups.
365  * @param data
366  *   Output containing array of data returned from all the successful lookups.
367  * @return
368  *   -EINVAL if there's an error, otherwise number of successful lookups.
369  */
370 int
371 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
372                       uint32_t num_keys, uint64_t *hit_mask, void *data[]);
373
374 /**
375  * Find multiple keys in the hash table.
376  * This operation is multi-thread safe.
377  *
378  * @param h
379  *   Hash table to look in.
380  * @param keys
381  *   A pointer to a list of keys to look for.
382  * @param num_keys
383  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
384  * @param positions
385  *   Output containing a list of values, corresponding to the list of keys that
386  *   can be used by the caller as an offset into an array of user data. These
387  *   values are unique for each key, and are the same values that were returned
388  *   when each key was added. If a key in the list was not found, then -ENOENT
389  *   will be the value.
390  * @return
391  *   -EINVAL if there's an error, otherwise 0.
392  */
393 int
394 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
395                       uint32_t num_keys, int32_t *positions);
396 #ifdef __cplusplus
397 }
398 #endif
399
400 #endif /* _RTE_HASH_H_ */