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