first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #ifndef _RTE_HASH_H_
37 #define _RTE_HASH_H_
38
39 /**
40  * @file
41  *
42  * RTE Hash Table
43  */
44
45 #include <stdint.h>
46 #include <sys/queue.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 /** Maximum size of hash table that can be created. */
53 #define RTE_HASH_ENTRIES_MAX                    (1 << 26)
54
55 /** Maximum bucket size that can be created. */
56 #define RTE_HASH_BUCKET_ENTRIES_MAX             16
57
58 /** Maximum length of key that can be used. */
59 #define RTE_HASH_KEY_LENGTH_MAX                 64
60
61 /** Max number of keys that can be searched for using rte_hash_lookup_multi. */
62 #define RTE_HASH_LOOKUP_MULTI_MAX               16
63
64 /** Max number of characters in hash name.*/
65 #define RTE_HASH_NAMESIZE                       32
66
67 /** Signature of key that is stored internally. */
68 typedef uint32_t hash_sig_t;
69
70 /** Type of function that can be used for calculating the hash value. */
71 typedef uint32_t (*rte_hash_function)(const void *key, uint32_t key_len,
72                                       uint32_t init_val);
73
74 /**
75  * Parameters used when creating the hash table. The total table entries and
76  * bucket entries must be a power of 2.
77  */
78 struct rte_hash_parameters {
79         const char *name;               /**< Name of the hash. */
80         uint32_t entries;               /**< Total hash table entries. */
81         uint32_t bucket_entries;        /**< Bucket entries. */
82         uint32_t key_len;               /**< Length of hash key. */
83         rte_hash_function hash_func;    /**< Function used to calculate hash. */
84         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
85         int socket_id;                  /**< NUMA Socket ID for memory. */
86 };
87
88 /** A hash table structure. */
89 struct rte_hash {
90         TAILQ_ENTRY(rte_hash) next;/**< Next in list. */
91
92         char name[RTE_HASH_NAMESIZE];   /**< Name of the hash. */
93         uint32_t entries;               /**< Total table entries. */
94         uint32_t bucket_entries;        /**< Bucket entries. */
95         uint32_t key_len;               /**< Length of hash key. */
96         rte_hash_function hash_func;    /**< Function used to calculate hash. */
97         uint32_t hash_func_init_val;    /**< Init value used by hash_func. */
98         uint32_t num_buckets;           /**< Number of buckets in table. */
99         uint32_t bucket_bitmask;        /**< Bitmask for getting bucket index
100                                                         from hash signature. */
101         hash_sig_t sig_msb;     /**< MSB is always set in valid signatures. */
102         uint8_t *sig_tbl;       /**< Flat array of hash signature buckets. */
103         uint32_t sig_tbl_bucket_size;   /**< Signature buckets may be padded for
104                                            alignment reasons, and this is the
105                                            bucket size used by sig_tbl. */
106         uint8_t *key_tbl;       /**< Flat array of key value buckets. */
107         uint32_t key_tbl_key_size;      /**< Keys may be padded for alignment
108                                            reasons, and this is the key size
109                                            used by key_tbl. */
110 };
111
112 /**
113  * Create a new hash table. If RTE_LIBRTE_HASH_USE_MEMZONE is defined, then
114  * the hash table is allocated in a memzone on a specific NUMA socket ID,
115  * otherwise it is allocated in the heap.
116  *
117  * @param params
118  *   Parameters used to create and initialise the hash table.
119  * @return
120  *   Pointer to hash table structure that is used in future hash table
121  *   operations, or NULL on error, with error code set in rte_errno.
122  *   Possible rte_errno errors include:
123  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
124  *    - E_RTE_SECONDARY - function was called from a secondary process instance
125  *    - E_RTE_NO_TAILQ - no tailq list could be got for the hash table list
126  *    - ENOENT - missing entry
127  *    - EINVAL - invalid parameter passed to function
128  *    - ENOSPC - the maximum number of memzones has already been allocated
129  *    - EEXIST - a memzone with the same name already exists
130  *    - ENOMEM - no appropriate memory area found in which to create memzone
131  */
132 struct rte_hash *
133 rte_hash_create(const struct rte_hash_parameters *params);
134
135
136 /**
137  * Find an existing hash table object and return a pointer to it.
138  *
139  * @param name
140  *   Name of the hash table as passed to rte_hash_create()
141  * @return
142  *   Pointer to hash table or NULL if object not found
143  *   with rte_errno set appropriately. Possible rte_errno values include:
144  *    - ENOENT - value not available for return
145  */
146 struct rte_hash *
147 rte_hash_find_existing(const char *name);
148
149 /**
150  * De-allocate all memory used by hash table. If RTE_LIBRTE_HASH_USE_MEMZONE
151  * is defined, then this has no effect.
152  * @param h
153  *   Hash table to free
154  */
155 void
156 rte_hash_free(struct rte_hash *h);
157
158 /**
159  * Add a key to an existing hash table. This operation is not multi-thread safe
160  * and should only be called from one thread.
161  *
162  * @param h
163  *   Hash table to add the key to.
164  * @param key
165  *   Key to add to the hash table.
166  * @return
167  *   - -EINVAL if the parameters are invalid.
168  *   - -ENOSPC if there is no space in the hash for this key.
169  *   - A positive value that can be used by the caller as an offset into an
170  *     array of user data. This value is unique for this key.
171  */
172 int32_t
173 rte_hash_add_key(const struct rte_hash *h, const void *key);
174
175 /**
176  * Remove a key from an existing hash table. This operation is not multi-thread
177  * safe and should only be called from one thread.
178  *
179  * @param h
180  *   Hash table to remove the key from.
181  * @param key
182  *   Key to remove from the hash table.
183  * @return
184  *   - -EINVAL if the parameters are invalid.
185  *   - -ENOENT if the key is not found.
186  *   - A positive value that can be used by the caller as an offset into an
187  *     array of user data. This value is unique for this key, and is the same
188  *     value that was returned when the key was added.
189  */
190 int32_t
191 rte_hash_del_key(const struct rte_hash *h, const void *key);
192
193 /**
194  * Find a key in the hash table. This operation is multi-thread safe.
195  *
196  * @param h
197  *   Hash table to look in.
198  * @param key
199  *   Key to find.
200  * @return
201  *   - -EINVAL if the parameters are invalid.
202  *   - -ENOENT if the key is not found.
203  *   - A positive value that can be used by the caller as an offset into an
204  *     array of user data. This value is unique for this key, and is the same
205  *     value that was returned when the key was added.
206  */
207 int32_t
208 rte_hash_lookup(const struct rte_hash *h, const void *key);
209
210 /**
211  * Find multiple keys in the hash table. This operation is multi-thread safe.
212  *
213  * @param h
214  *   Hash table to look in.
215  * @param keys
216  *   A pointer to a list of keys to look for.
217  * @param num_keys
218  *   How many keys are in the keys list (less than RTE_HASH_LOOKUP_MULTI_MAX).
219  * @param positions
220  *   Output containing a list of values, corresponding to the list of keys that
221  *   can be used by the caller as an offset into an array of user data. These
222  *   values are unique for each key, and are the same values that were returned
223  *   when each key was added. If a key in the list was not found, then -ENOENT
224  *   will be the value.
225  * @return
226  *   -EINVAL if there's an error, otherwise 0.
227  */
228 int
229 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
230                       uint32_t num_keys, int32_t *positions);
231
232 #ifdef __cplusplus
233 }
234 #endif
235
236 #endif /* _RTE_HASH_H_ */