hash: customize compare 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 /** 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 /** Enable Hardware transactional memory support. */
60 #define RTE_HASH_EXTRA_FLAGS_TRANS_MEM_SUPPORT  0x01
61
62 /** Signature of key that is stored internally. */
63 typedef uint32_t hash_sig_t;
64
65 /** Type of function that can be used for calculating the hash value. */
66 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
67                                       uint32_t init_val);
68
69 /** Type of function used to compare the hash key. */
70 typedef int (*rte_hash_cmp_eq_t)(const void *key1, const void *key2, size_t key_len);
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 reserved;              /**< Unused field. Should be set to 0 */
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  * Set a new hash compare function other than the default one.
111  *
112  * @note Function pointer does not work with multi-process, so do not use it
113  * in multi-process mode.
114  *
115  * @param h
116  *   Hash table to reset
117  * @param func
118  *   New compare function
119  */
120 void rte_hash_set_cmp_func(struct rte_hash *h, rte_hash_cmp_eq_t func);
121
122 /**
123  * Find an existing hash table object and return a pointer to it.
124  *
125  * @param name
126  *   Name of the hash table as passed to rte_hash_create()
127  * @return
128  *   Pointer to hash table or NULL if object not found
129  *   with rte_errno set appropriately. Possible rte_errno values include:
130  *    - ENOENT - value not available for return
131  */
132 struct rte_hash *
133 rte_hash_find_existing(const char *name);
134
135 /**
136  * De-allocate all memory used by hash table.
137  * @param h
138  *   Hash table to free
139  */
140 void
141 rte_hash_free(struct rte_hash *h);
142
143 /**
144  * Reset all hash structure, by zeroing all entries
145  * @param h
146  *   Hash table to reset
147  */
148 void
149 rte_hash_reset(struct rte_hash *h);
150
151 /**
152  * Add a key-value pair to an existing hash table.
153  * 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  * @param data
161  *   Data to add to the hash table.
162  * @return
163  *   - 0 if added successfully
164  *   - -EINVAL if the parameters are invalid.
165  *   - -ENOSPC if there is no space in the hash for this key.
166  */
167 int
168 rte_hash_add_key_data(const struct rte_hash *h, const void *key, void *data);
169
170 /**
171  * Add a key-value pair with a pre-computed hash value
172  * to an existing hash table.
173  * This operation is not multi-thread safe
174  * and should only be called from one thread.
175  *
176  * @param h
177  *   Hash table to add the key to.
178  * @param key
179  *   Key to add to the hash table.
180  * @param sig
181  *   Precomputed hash value for 'key'
182  * @param data
183  *   Data to add to the hash table.
184  * @return
185  *   - 0 if added successfully
186  *   - -EINVAL if the parameters are invalid.
187  *   - -ENOSPC if there is no space in the hash for this key.
188  */
189 int32_t
190 rte_hash_add_key_with_hash_data(const struct rte_hash *h, const void *key,
191                                                 hash_sig_t sig, void *data);
192
193 /**
194  * Add a key to an existing hash table. This operation is not multi-thread safe
195  * and should only be called from one thread.
196  *
197  * @param h
198  *   Hash table to add the key to.
199  * @param key
200  *   Key to add to the hash table.
201  * @return
202  *   - -EINVAL if the parameters are invalid.
203  *   - -ENOSPC if there is no space in the hash for this key.
204  *   - A positive value that can be used by the caller as an offset into an
205  *     array of user data. This value is unique for this key.
206  */
207 int32_t
208 rte_hash_add_key(const struct rte_hash *h, const void *key);
209
210 /**
211  * Add a key to an existing hash table.
212  * This operation is not multi-thread safe
213  * and should only be called from one thread.
214  *
215  * @param h
216  *   Hash table to add the key to.
217  * @param key
218  *   Key to add to the hash table.
219  * @param sig
220  *   Precomputed hash value for 'key'.
221  * @return
222  *   - -EINVAL if the parameters are invalid.
223  *   - -ENOSPC if there is no space in the hash for this key.
224  *   - A positive value that can be used by the caller as an offset into an
225  *     array of user data. This value is unique for this key.
226  */
227 int32_t
228 rte_hash_add_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
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  * @return
240  *   - -EINVAL if the parameters are invalid.
241  *   - -ENOENT if the key is not found.
242  *   - A positive value that can be used by the caller as an offset into an
243  *     array of user data. This value is unique for this key, and is the same
244  *     value that was returned when the key was added.
245  */
246 int32_t
247 rte_hash_del_key(const struct rte_hash *h, const void *key);
248
249 /**
250  * Remove a key from an existing hash table.
251  * This operation is not multi-thread safe
252  * and should only be called from one thread.
253  *
254  * @param h
255  *   Hash table to remove the key from.
256  * @param key
257  *   Key to remove from the hash table.
258  * @param sig
259  *   Precomputed hash value for 'key'.
260  * @return
261  *   - -EINVAL if the parameters are invalid.
262  *   - -ENOENT if the key is not found.
263  *   - A positive value that can be used by the caller as an offset into an
264  *     array of user data. This value is unique for this key, and is the same
265  *     value that was returned when the key was added.
266  */
267 int32_t
268 rte_hash_del_key_with_hash(const struct rte_hash *h, const void *key, hash_sig_t sig);
269
270
271 /**
272  * Find a key-value pair in the 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 data
280  *   Output with pointer to data returned from the hash table.
281  * @return
282  *   0 if successful lookup
283  *   - EINVAL if the parameters are invalid.
284  *   - ENOENT if the key is not found.
285  */
286 int
287 rte_hash_lookup_data(const struct rte_hash *h, const void *key, void **data);
288
289 /**
290  * Find a key-value pair with a pre-computed hash value
291  * to an existing hash table.
292  * This operation is multi-thread safe.
293  *
294  * @param h
295  *   Hash table to look in.
296  * @param key
297  *   Key to find.
298  * @param sig
299  *   Precomputed hash value for 'key'
300  * @param data
301  *   Output with pointer to data returned from the hash table.
302  * @return
303  *   0 if successful lookup
304  *   - EINVAL if the parameters are invalid.
305  *   - ENOENT if the key is not found.
306  */
307 int
308 rte_hash_lookup_with_hash_data(const struct rte_hash *h, const void *key,
309                                         hash_sig_t sig, void **data);
310
311 /**
312  * Find a key in the hash table.
313  * This operation is multi-thread safe.
314  *
315  * @param h
316  *   Hash table to look in.
317  * @param key
318  *   Key to find.
319  * @return
320  *   - -EINVAL if the parameters are invalid.
321  *   - -ENOENT if the key is not found.
322  *   - A positive value that can be used by the caller as an offset into an
323  *     array of user data. This value is unique for this key, and is the same
324  *     value that was returned when the key was added.
325  */
326 int32_t
327 rte_hash_lookup(const struct rte_hash *h, const void *key);
328
329 /**
330  * Find a key in the hash table.
331  * This operation is multi-thread safe.
332  *
333  * @param h
334  *   Hash table to look in.
335  * @param key
336  *   Key to find.
337  * @param sig
338  *   Hash value to remove from the hash table.
339  * @return
340  *   - -EINVAL if the parameters are invalid.
341  *   - -ENOENT if the key is not found.
342  *   - A positive value that can be used by the caller as an offset into an
343  *     array of user data. This value is unique for this key, and is the same
344  *     value that was returned when the key was added.
345  */
346 int32_t
347 rte_hash_lookup_with_hash(const struct rte_hash *h,
348                                 const void *key, hash_sig_t sig);
349
350 /**
351  * Calc a hash value by key.
352  * This operation is not multi-thread safe.
353  *
354  * @param h
355  *   Hash table to look in.
356  * @param key
357  *   Key to find.
358  * @return
359  *   - hash value
360  */
361 hash_sig_t
362 rte_hash_hash(const struct rte_hash *h, const void *key);
363
364 #define rte_hash_lookup_multi rte_hash_lookup_bulk
365 #define rte_hash_lookup_multi_data rte_hash_lookup_bulk_data
366 /**
367  * Find multiple keys in the hash table.
368  * This operation is multi-thread safe.
369  *
370  * @param h
371  *   Hash table to look in.
372  * @param keys
373  *   A pointer to a list of keys to look for.
374  * @param num_keys
375  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
376  * @param hit_mask
377  *   Output containing a bitmask with all successful lookups.
378  * @param data
379  *   Output containing array of data returned from all the successful lookups.
380  * @return
381  *   -EINVAL if there's an error, otherwise number of successful lookups.
382  */
383 int
384 rte_hash_lookup_bulk_data(const struct rte_hash *h, const void **keys,
385                       uint32_t num_keys, uint64_t *hit_mask, void *data[]);
386
387 /**
388  * Find multiple keys in the hash table.
389  * This operation is multi-thread safe.
390  *
391  * @param h
392  *   Hash table to look in.
393  * @param keys
394  *   A pointer to a list of keys to look for.
395  * @param num_keys
396  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
397  * @param positions
398  *   Output containing a list of values, corresponding to the list of keys that
399  *   can be used by the caller as an offset into an array of user data. These
400  *   values are unique for each key, and are the same values that were returned
401  *   when each key was added. If a key in the list was not found, then -ENOENT
402  *   will be the value.
403  * @return
404  *   -EINVAL if there's an error, otherwise 0.
405  */
406 int
407 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
408                       uint32_t num_keys, int32_t *positions);
409
410 /**
411  * Iterate through the hash table, returning key-value pairs.
412  *
413  * @param h
414  *   Hash table to iterate
415  * @param key
416  *   Output containing the key where current iterator
417  *   was pointing at
418  * @param data
419  *   Output containing the data associated with key.
420  *   Returns NULL if data was not stored.
421  * @param next
422  *   Pointer to iterator. Should be 0 to start iterating the hash table.
423  *   Iterator is incremented after each call of this function.
424  * @return
425  *   Position where key was stored, if successful.
426  *   - -EINVAL if the parameters are invalid.
427  *   - -ENOENT if end of the hash table.
428  */
429 int32_t
430 rte_hash_iterate(const struct rte_hash *h, const void **key, void **data, uint32_t *next);
431 #ifdef __cplusplus
432 }
433 #endif
434
435 #endif /* _RTE_HASH_H_ */