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