doc: whitespace changes in licenses
[dpdk.git] / lib / librte_hash / rte_hash.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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 <sys/queue.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 << 26)
52
53 /** Maximum bucket size that can be created. */
54 #define RTE_HASH_BUCKET_ENTRIES_MAX             16
55
56 /** Maximum length of key that can be used. */
57 #define RTE_HASH_KEY_LENGTH_MAX                 64
58
59 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
60 #define RTE_HASH_LOOKUP_BULK_MAX                16
61 #define RTE_HASH_LOOKUP_MULTI_MAX               RTE_HASH_LOOKUP_BULK_MAX
62
63 /** Max number of characters in hash name.*/
64 #define RTE_HASH_NAMESIZE                       32
65
66 /** Signature of key that is stored internally. */
67 typedef uint32_t hash_sig_t;
68
69 /** Type of function that can be used for calculating the hash value. */
70 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
71                                       uint32_t init_val);
72
73 /**
74  * Parameters used when creating the hash table. The total table entries and
75  * bucket entries must be a power of 2.
76  */
77 struct rte_hash_parameters {
78         const char *name;               /**< Name of the hash. */
79         uint32_t entries;               /**< Total hash table entries. */
80         uint32_t bucket_entries;        /**< Bucket entries. */
81         uint32_t key_len;               /**< Length of hash key. */
82         rte_hash_function hash_func;    /**< Function used to calculate hash. */
83         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
84         int socket_id;                  /**< NUMA Socket ID for memory. */
85 };
86
87 /** A hash table structure. */
88 struct rte_hash {
89         TAILQ_ENTRY(rte_hash) next;/**< Next in list. */
90
91         char name[RTE_HASH_NAMESIZE];   /**< Name of the hash. */
92         uint32_t entries;               /**< Total table entries. */
93         uint32_t bucket_entries;        /**< Bucket entries. */
94         uint32_t key_len;               /**< Length of hash key. */
95         rte_hash_function hash_func;    /**< Function used to calculate hash. */
96         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
97         uint32_t num_buckets;           /**< Number of buckets in table. */
98         uint32_t bucket_bitmask;        /**< Bitmask for getting bucket index
99                                                         from hash signature. */
100         hash_sig_t sig_msb;     /**< MSB is always set in valid signatures. */
101         uint8_t *sig_tbl;       /**< Flat array of hash signature buckets. */
102         uint32_t sig_tbl_bucket_size;   /**< Signature buckets may be padded for
103                                            alignment reasons, and this is the
104                                            bucket size used by sig_tbl. */
105         uint8_t *key_tbl;       /**< Flat array of key value buckets. */
106         uint32_t key_tbl_key_size;      /**< Keys may be padded for alignment
107                                            reasons, and this is the key size
108                                            used by key_tbl. */
109 };
110
111 /**
112  * Create a new hash table.
113  *
114  * @param params
115  *   Parameters used to create and initialise the hash table.
116  * @return
117  *   Pointer to hash table structure that is used in future hash table
118  *   operations, or NULL on error, with error code set in rte_errno.
119  *   Possible rte_errno errors include:
120  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
121  *    - E_RTE_SECONDARY - function was called from a secondary process instance
122  *    - E_RTE_NO_TAILQ - no tailq list could be got for the hash table list
123  *    - ENOENT - missing entry
124  *    - EINVAL - invalid parameter passed to function
125  *    - ENOSPC - the maximum number of memzones has already been allocated
126  *    - EEXIST - a memzone with the same name already exists
127  *    - ENOMEM - no appropriate memory area found in which to create memzone
128  */
129 struct rte_hash *
130 rte_hash_create(const struct rte_hash_parameters *params);
131
132
133 /**
134  * Find an existing hash table object and return a pointer to it.
135  *
136  * @param name
137  *   Name of the hash table as passed to rte_hash_create()
138  * @return
139  *   Pointer to hash table or NULL if object not found
140  *   with rte_errno set appropriately. Possible rte_errno values include:
141  *    - ENOENT - value not available for return
142  */
143 struct rte_hash *
144 rte_hash_find_existing(const char *name);
145
146 /**
147  * De-allocate all memory used by hash table.
148  * @param h
149  *   Hash table to free
150  */
151 void
152 rte_hash_free(struct rte_hash *h);
153
154 /**
155  * Add a key to an existing hash table. This operation is not multi-thread safe
156  * and should only be called from one thread.
157  *
158  * @param h
159  *   Hash table to add the key to.
160  * @param key
161  *   Key to add to the hash table.
162  * @return
163  *   - -EINVAL if the parameters are invalid.
164  *   - -ENOSPC if there is no space in the hash for this key.
165  *   - A positive value that can be used by the caller as an offset into an
166  *     array of user data. This value is unique for this key.
167  */
168 int32_t
169 rte_hash_add_key(const struct rte_hash *h, const void *key);
170
171 /**
172  * Add a key to an existing hash table. This operation is not multi-thread safe
173  * and should only be called from one thread.
174  *
175  * @param h
176  *   Hash table to add the key to.
177  * @param key
178  *   Key to add to the hash table.
179  * @param sig
180  *   Hash value to add to the hash table.
181  * @return
182  *   - -EINVAL if the parameters are invalid.
183  *   - -ENOSPC if there is no space in the hash for this key.
184  *   - A positive value that can be used by the caller as an offset into an
185  *     array of user data. This value is unique for this key.
186  */
187 int32_t
188 rte_hash_add_key_with_hash(const struct rte_hash *h, 
189                                 const void *key, hash_sig_t sig);
190
191 /**
192  * Remove a key from an existing hash table. This operation is not multi-thread
193  * safe and should only be called from one thread.
194  *
195  * @param h
196  *   Hash table to remove the key from.
197  * @param key
198  *   Key to remove from the hash table.
199  * @return
200  *   - -EINVAL if the parameters are invalid.
201  *   - -ENOENT if the key is not found.
202  *   - A positive value that can be used by the caller as an offset into an
203  *     array of user data. This value is unique for this key, and is the same
204  *     value that was returned when the key was added.
205  */
206 int32_t
207 rte_hash_del_key(const struct rte_hash *h, const void *key);
208
209 /**
210  * Remove a key from an existing hash table. This operation is not multi-thread
211  * safe and should only be called from one thread.
212  *
213  * @param h
214  *   Hash table to remove the key from.
215  * @param key
216  *   Key to remove from the hash table.
217  * @param sig
218  *   Hash value to remove from the hash table.
219  * @return
220  *   - -EINVAL if the parameters are invalid.
221  *   - -ENOENT if the key is not found.
222  *   - A positive value that can be used by the caller as an offset into an
223  *     array of user data. This value is unique for this key, and is the same
224  *     value that was returned when the key was added.
225  */
226 int32_t
227 rte_hash_del_key_with_hash(const struct rte_hash *h, 
228                                 const void *key, hash_sig_t sig);
229
230
231 /**
232  * Find a key in the hash table. This operation is multi-thread safe.
233  *
234  * @param h
235  *   Hash table to look in.
236  * @param key
237  *   Key to find.
238  * @return
239  *   - -EINVAL if the parameters are invalid.
240  *   - -ENOENT if the key is not found.
241  *   - A positive value that can be used by the caller as an offset into an
242  *     array of user data. This value is unique for this key, and is the same
243  *     value that was returned when the key was added.
244  */
245 int32_t
246 rte_hash_lookup(const struct rte_hash *h, const void *key);
247
248 /**
249  * Find a key in the hash table. This operation is multi-thread safe.
250  *
251  * @param h
252  *   Hash table to look in.
253  * @param key
254  *   Key to find.
255  * @param sig
256  *   Hash value to find.
257  * @return
258  *   - -EINVAL if the parameters are invalid.
259  *   - -ENOENT if the key is not found.
260  *   - A positive value that can be used by the caller as an offset into an
261  *     array of user data. This value is unique for this key, and is the same
262  *     value that was returned when the key was added.
263  */
264 int32_t
265 rte_hash_lookup_with_hash(const struct rte_hash *h, 
266                                 const void *key, hash_sig_t sig);
267
268
269 /**
270  * Calc a hash value by key. This operation is not multi-process safe.
271  *
272  * @param h
273  *   Hash table to look in.
274  * @param key
275  *   Key to find.
276  * @return
277  *   - hash value
278  */
279 static inline hash_sig_t
280 rte_hash_hash(const struct rte_hash *h, const void *key)
281 {
282         /* calc hash result by key */
283         return h->hash_func(key, h->key_len, h->hash_func_init_val);
284 }
285
286 #define rte_hash_lookup_multi rte_hash_lookup_bulk
287 /**
288  * Find multiple keys in the hash table. This operation is multi-thread safe.
289  *
290  * @param h
291  *   Hash table to look in.
292  * @param keys
293  *   A pointer to a list of keys to look for.
294  * @param num_keys
295  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_BULK_MAX).
296  * @param positions
297  *   Output containing a list of values, corresponding to the list of keys that
298  *   can be used by the caller as an offset into an array of user data. These
299  *   values are unique for each key, and are the same values that were returned
300  *   when each key was added. If a key in the list was not found, then -ENOENT
301  *   will be the value.
302  * @return
303  *   -EINVAL if there's an error, otherwise 0.
304  */
305 int
306 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
307                       uint32_t num_keys, int32_t *positions);
308 #ifdef __cplusplus
309 }
310 #endif
311
312 #endif /* _RTE_HASH_H_ */