remove version in all files
[dpdk.git] / lib / librte_hash / rte_hash.h
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #ifndef _RTE_HASH_H_
36 #define _RTE_HASH_H_
37
38 /**
39  * @file
40  *
41  * RTE Hash Table
42  */
43
44 #include <stdint.h>
45 #include <sys/queue.h>
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /** Maximum size of hash table that can be created. */
52 #define RTE_HASH_ENTRIES_MAX                    (1 << 26)
53
54 /** Maximum bucket size that can be created. */
55 #define RTE_HASH_BUCKET_ENTRIES_MAX             16
56
57 /** Maximum length of key that can be used. */
58 #define RTE_HASH_KEY_LENGTH_MAX                 64
59
60 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
61 #define RTE_HASH_LOOKUP_MULTI_MAX               16
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. If RTE_LIBRTE_HASH_USE_MEMZONE is defined, then
113  * the hash table is allocated in a memzone on a specific NUMA socket ID,
114  * otherwise it is allocated in the heap.
115  *
116  * @param params
117  *   Parameters used to create and initialise the hash table.
118  * @return
119  *   Pointer to hash table structure that is used in future hash table
120  *   operations, or NULL on error, with error code set in rte_errno.
121  *   Possible rte_errno errors include:
122  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
123  *    - E_RTE_SECONDARY - function was called from a secondary process instance
124  *    - E_RTE_NO_TAILQ - no tailq list could be got for the hash table list
125  *    - ENOENT - missing entry
126  *    - EINVAL - invalid parameter passed to function
127  *    - ENOSPC - the maximum number of memzones has already been allocated
128  *    - EEXIST - a memzone with the same name already exists
129  *    - ENOMEM - no appropriate memory area found in which to create memzone
130  */
131 struct rte_hash *
132 rte_hash_create(const struct rte_hash_parameters *params);
133
134
135 /**
136  * Find an existing hash table object and return a pointer to it.
137  *
138  * @param name
139  *   Name of the hash table as passed to rte_hash_create()
140  * @return
141  *   Pointer to hash table or NULL if object not found
142  *   with rte_errno set appropriately. Possible rte_errno values include:
143  *    - ENOENT - value not available for return
144  */
145 struct rte_hash *
146 rte_hash_find_existing(const char *name);
147
148 /**
149  * De-allocate all memory used by hash table. If RTE_LIBRTE_HASH_USE_MEMZONE
150  * is defined, then this has no effect.
151  * @param h
152  *   Hash table to free
153  */
154 void
155 rte_hash_free(struct rte_hash *h);
156
157 /**
158  * Add a key to an existing hash table. This operation is not multi-thread safe
159  * and should only be called from one thread.
160  *
161  * @param h
162  *   Hash table to add the key to.
163  * @param key
164  *   Key to add to the hash table.
165  * @return
166  *   - -EINVAL if the parameters are invalid.
167  *   - -ENOSPC if there is no space in the hash for this key.
168  *   - A positive value that can be used by the caller as an offset into an
169  *     array of user data. This value is unique for this key.
170  */
171 int32_t
172 rte_hash_add_key(const struct rte_hash *h, const void *key);
173
174 /**
175  * Remove a key from an existing hash table. This operation is not multi-thread
176  * safe and should only be called from one thread.
177  *
178  * @param h
179  *   Hash table to remove the key from.
180  * @param key
181  *   Key to remove from the hash table.
182  * @return
183  *   - -EINVAL if the parameters are invalid.
184  *   - -ENOENT if the key is not found.
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, and is the same
187  *     value that was returned when the key was added.
188  */
189 int32_t
190 rte_hash_del_key(const struct rte_hash *h, const void *key);
191
192 /**
193  * Find a key in the hash table. This operation is multi-thread safe.
194  *
195  * @param h
196  *   Hash table to look in.
197  * @param key
198  *   Key to find.
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_lookup(const struct rte_hash *h, const void *key);
208
209 /**
210  * Find multiple keys in the hash table. This operation is multi-thread safe.
211  *
212  * @param h
213  *   Hash table to look in.
214  * @param keys
215  *   A pointer to a list of keys to look for.
216  * @param num_keys
217  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_MULTI_MAX).
218  * @param positions
219  *   Output containing a list of values, corresponding to the list of keys that
220  *   can be used by the caller as an offset into an array of user data. These
221  *   values are unique for each key, and are the same values that were returned
222  *   when each key was added. If a key in the list was not found, then -ENOENT
223  *   will be the value.
224  * @return
225  *   -EINVAL if there's an error, otherwise 0.
226  */
227 int
228 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
229                       uint32_t num_keys, int32_t *positions);
230
231 #ifdef __cplusplus
232 }
233 #endif
234
235 #endif /* _RTE_HASH_H_ */