hash: remove deprecated function and macros
[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 /** Maximum number of characters in hash name.*/
53 #define RTE_HASH_NAMESIZE                       32
54
55 /** Maximum number of keys that can be searched for using rte_hash_lookup_bulk. */
56 #define RTE_HASH_LOOKUP_BULK_MAX                64
57 #define RTE_HASH_LOOKUP_MULTI_MAX               RTE_HASH_LOOKUP_BULK_MAX
58
59 /** Signature of key that is stored internally. */
60 typedef uint32_t hash_sig_t;
61
62 /** Type of function that can be used for calculating the hash value. */
63 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
64                                       uint32_t init_val);
65
66 /**
67  * Parameters used when creating the hash table.
68  */
69 struct rte_hash_parameters {
70         const char *name;               /**< Name of the hash. */
71         uint32_t entries;               /**< Total hash table entries. */
72         uint32_t reserved;              /**< Unused field. Should be set to 0 */
73         uint32_t key_len;               /**< Length of hash key. */
74         rte_hash_function hash_func;    /**< Primary Hash function used to calculate hash. */
75         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
76         int socket_id;                  /**< NUMA Socket ID for memory. */
77         uint8_t extra_flag;             /**< Indicate if additional parameters are present. */
78 };
79
80 /** @internal A hash table structure. */
81 struct rte_hash;
82
83 /**
84  * Create a new hash table.
85  *
86  * @param params
87  *   Parameters used to create and initialise the hash table.
88  * @return
89  *   Pointer to hash table structure that is used in future hash table
90  *   operations, or NULL on error, with error code set in rte_errno.
91  *   Possible rte_errno errors include:
92  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
93  *    - E_RTE_SECONDARY - function was called from a secondary process instance
94  *    - ENOENT - missing entry
95  *    - EINVAL - invalid parameter passed to function
96  *    - ENOSPC - the maximum number of memzones has already been allocated
97  *    - EEXIST - a memzone with the same name already exists
98  *    - ENOMEM - no appropriate memory area found in which to create memzone
99  */
100 struct rte_hash *
101 rte_hash_create(const struct rte_hash_parameters *params);
102
103 /**
104  * Find an existing hash table object and return a pointer to it.
105  *
106  * @param name
107  *   Name of the hash table as passed to rte_hash_create()
108  * @return
109  *   Pointer to hash table or NULL if object not found
110  *   with rte_errno set appropriately. Possible rte_errno values include:
111  *    - ENOENT - value not available for return
112  */
113 struct rte_hash *
114 rte_hash_find_existing(const char *name);
115
116 /**
117  * De-allocate all memory used by hash table.
118  * @param h
119  *   Hash table to free
120  */
121 void
122 rte_hash_free(struct rte_hash *h);
123
124 /**
125  * Reset all hash structure, by zeroing all entries
126  * @param h
127  *   Hash table to reset
128  */
129 void
130 rte_hash_reset(struct rte_hash *h);
131
132 /**
133  * Add a key-value pair to an existing hash table.
134  * This operation is not multi-thread safe
135  * and should only be called from one thread.
136  *
137  * @param h
138  *   Hash table to add the key to.
139  * @param key
140  *   Key to add to the hash table.
141  * @param data
142  *   Data to add to the hash table.
143  * @return
144  *   - 0 if added successfully
145  *   - -EINVAL if the parameters are invalid.
146  *   - -ENOSPC if there is no space in the hash for this key.
147  */
148 int
149 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
150
151 /**
152  * Add a key-value pair with a pre-computed hash value
153  * to an existing hash table.
154  * This operation is not multi-thread safe
155  * and should only be called from one thread.
156  *
157  * @param h
158  *   Hash table to add the key to.
159  * @param key
160  *   Key to add to the hash table.
161  * @param sig
162  *   Precomputed hash value for 'key'
163  * @param data
164  *   Data to add to the hash table.
165  * @return
166  *   - 0 if added successfully
167  *   - -EINVAL if the parameters are invalid.
168  *   - -ENOSPC if there is no space in the hash for this key.
169  */
170 int32_t
171 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
172                                                 hash_sig_t sig, void *data);
173
174 /**
175  * Add a key to an existing hash table. This operation is not multi-thread safe
176  * and should only be called from one thread.
177  *
178  * @param h
179  *   Hash table to add the key to.
180  * @param key
181  *   Key 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(const struct rte_hash *h, const void *key);
190
191 /**
192  * Add a key to an existing hash table.
193  * This operation is not multi-thread safe
194  * and should only be called from one thread.
195  *
196  * @param h
197  *   Hash table to add the key to.
198  * @param key
199  *   Key to add to the hash table.
200  * @param sig
201  *   Precomputed hash value for 'key'.
202  * @return
203  *   - -EINVAL if the parameters are invalid.
204  *   - -ENOSPC if there is no space in the hash for this key.
205  *   - A positive value that can be used by the caller as an offset into an
206  *     array of user data. This value is unique for this key.
207  */
208 int32_t
209 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
210
211 /**
212  * Remove a key from an existing hash table.
213  * This operation is not multi-thread safe
214  * and should only be called from one thread.
215  *
216  * @param h
217  *   Hash table to remove the key from.
218  * @param key
219  *   Key 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(const struct rte_hash *h, const void *key);
229
230 /**
231  * Remove a key from an existing hash table.
232  * This operation is not multi-thread safe
233  * and should only be called from one thread.
234  *
235  * @param h
236  *   Hash table to remove the key from.
237  * @param key
238  *   Key to remove from the hash table.
239  * @param sig
240  *   Precomputed hash value for 'key'.
241  * @return
242  *   - -EINVAL if the parameters are invalid.
243  *   - -ENOENT if the key is not found.
244  *   - A positive value that can be used by the caller as an offset into an
245  *     array of user data. This value is unique for this key, and is the same
246  *     value that was returned when the key was added.
247  */
248 int32_t
249 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
250
251
252 /**
253  * Find a key-value pair in the hash table.
254  * This operation is multi-thread safe.
255  *
256  * @param h
257  *   Hash table to look in.
258  * @param key
259  *   Key to find.
260  * @param data
261  *   Output with pointer to data returned from the hash table.
262  * @return
263  *   0 if successful lookup
264  *   - EINVAL if the parameters are invalid.
265  *   - ENOENT if the key is not found.
266  */
267 int
268 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
269
270 /**
271  * Find a key-value pair with a pre-computed hash value
272  * to an existing hash table.
273  * This operation is multi-thread safe.
274  *
275  * @param h
276  *   Hash table to look in.
277  * @param key
278  *   Key to find.
279  * @param sig
280  *   Precomputed hash value for 'key'
281  * @param data
282  *   Output with pointer to data returned from the hash table.
283  * @return
284  *   0 if successful lookup
285  *   - EINVAL if the parameters are invalid.
286  *   - ENOENT if the key is not found.
287  */
288 int
289 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
290                                         hash_sig_t sig, void **data);
291
292 /**
293  * Find a key in the hash table.
294  * This operation is multi-thread safe.
295  *
296  * @param h
297  *   Hash table to look in.
298  * @param key
299  *   Key to find.
300  * @return
301  *   - -EINVAL if the parameters are invalid.
302  *   - -ENOENT if the key is not found.
303  *   - A positive value that can be used by the caller as an offset into an
304  *     array of user data. This value is unique for this key, and is the same
305  *     value that was returned when the key was added.
306  */
307 int32_t
308 rte_hash_lookup(const struct rte_hash *h, const void *key);
309
310 /**
311  * Find a key in the hash table.
312  * This operation is multi-thread safe.
313  *
314  * @param h
315  *   Hash table to look in.
316  * @param key
317  *   Key to find.
318  * @param sig
319  *   Hash value to remove from the hash table.
320  * @return
321  *   - -EINVAL if the parameters are invalid.
322  *   - -ENOENT if the key is not found.
323  *   - A positive value that can be used by the caller as an offset into an
324  *     array of user data. This value is unique for this key, and is the same
325  *     value that was returned when the key was added.
326  */
327 int32_t
328 rte_hash_lookup_with_hash(const struct rte_hash *h,
329                                 const void *key, hash_sig_t sig);
330
331 /**
332  * Calc a hash value by key.
333  * This operation is not multi-thread safe.
334  *
335  * @param h
336  *   Hash table to look in.
337  * @param key
338  *   Key to find.
339  * @return
340  *   - hash value
341  */
342 hash_sig_t
343 rte_hash_hash(const struct rte_hash *h, const void *key);
344
345 #define rte_hash_lookup_multi rte_hash_lookup_bulk
346 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
347 /**
348  * Find multiple keys in the hash table.
349  * This operation is multi-thread safe.
350  *
351  * @param h
352  *   Hash table to look in.
353  * @param keys
354  *   A pointer to a list of keys to look for.
355  * @param num_keys
356  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
357  * @param hit_mask
358  *   Output containing a bitmask with all successful lookups.
359  * @param data
360  *   Output containing array of data returned from all the successful lookups.
361  * @return
362  *   -EINVAL if there's an error, otherwise number of successful lookups.
363  */
364 int
365 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
366                       uint32_t num_keys, uint64_t *hit_mask, void *data[]);
367
368 /**
369  * Find multiple keys in the hash table.
370  * This operation is multi-thread safe.
371  *
372  * @param h
373  *   Hash table to look in.
374  * @param keys
375  *   A pointer to a list of keys to look for.
376  * @param num_keys
377  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
378  * @param positions
379  *   Output containing a list of values, corresponding to the list of keys that
380  *   can be used by the caller as an offset into an array of user data. These
381  *   values are unique for each key, and are the same values that were returned
382  *   when each key was added. If a key in the list was not found, then -ENOENT
383  *   will be the value.
384  * @return
385  *   -EINVAL if there's an error, otherwise 0.
386  */
387 int
388 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
389                       uint32_t num_keys, int32_t *positions);
390
391 /**
392  * Iterate through the hash table, returning key-value pairs.
393  *
394  * @param h
395  *   Hash table to iterate
396  * @param key
397  *   Output containing the key where current iterator
398  *   was pointing at
399  * @param data
400  *   Output containing the data associated with key.
401  *   Returns NULL if data was not stored.
402  * @param next
403  *   Pointer to iterator. Should be 0 to start iterating the hash table.
404  *   Iterator is incremented after each call of this function.
405  * @return
406  *   Position where key was stored, if successful.
407  *   - -EINVAL if the parameters are invalid.
408  *   - -ENOENT if end of the hash table.
409  */
410 int32_t
411 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
412 #ifdef __cplusplus
413 }
414 #endif
415
416 #endif /* _RTE_HASH_H_ */