remove version in all files
[dpdk.git] / lib / librte_hash / rte_fbk_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_FBK_HASH_H_
36 #define _RTE_FBK_HASH_H_
37
38 /**
39  * @file
40  *
41  * This is a hash table implementation for four byte keys (fbk).
42  *
43  * Note that the return value of the add function should always be checked as,
44  * if a bucket is full, the key is not added even if there is space in other
45  * buckets. This keeps the lookup function very simple and therefore fast.
46  */
47
48 #include <stdint.h>
49 #include <errno.h>
50 #include <sys/queue.h>
51 #include <rte_hash_crc.h>
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 #include <string.h>
58
59 #ifndef RTE_FBK_HASH_FUNC_DEFAULT
60 /** Default four-byte key hash function if none is specified. */
61 #define RTE_FBK_HASH_FUNC_DEFAULT               rte_hash_crc_4byte
62 #endif
63
64 #ifndef RTE_FBK_HASH_INIT_VAL_DEFAULT
65 /** Initialising value used when calculating hash. */
66 #define RTE_FBK_HASH_INIT_VAL_DEFAULT           0xFFFFFFFF
67 #endif
68
69 /** The maximum number of entries in the hash table that is supported. */
70 #define RTE_FBK_HASH_ENTRIES_MAX                (1 << 20)
71
72 /** The maximum number of entries in each bucket that is supported. */
73 #define RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX     256
74
75 /** Maximum size of string for naming the hash. */
76 #define RTE_FBK_HASH_NAMESIZE                   32
77
78 /** Type of function that can be used for calculating the hash value. */
79 typedef uint32_t (*rte_fbk_hash_fn)(uint32_t key, uint32_t init_val);
80
81 /** Parameters used when creating four-byte key hash table. */
82 struct rte_fbk_hash_params {
83         const char *name;               /**< Name of the hash table. */
84         uint32_t entries;               /**< Total number of entries. */
85         uint32_t entries_per_bucket;    /**< Number of entries in a bucket. */
86         int socket_id;                  /**< Socket to allocate memory on. */
87         rte_fbk_hash_fn hash_func;      /**< The hash function. */
88         uint32_t init_val;              /**< For initialising hash function. */
89 };
90
91 /** Individual entry in the four-byte key hash table. */
92 union rte_fbk_hash_entry {
93         uint64_t whole_entry;           /**< For accessing entire entry. */
94         struct {
95                 uint16_t is_entry;      /**< Non-zero if entry is active. */
96                 uint16_t value;         /**< Value returned by lookup. */
97                 uint32_t key;           /**< Key used to find value. */
98         } entry;                        /**< For accessing each entry part. */
99 } ;
100
101
102
103 /** The four-byte key hash table structure. */
104 struct rte_fbk_hash_table {
105         TAILQ_ENTRY(rte_fbk_hash_table) next;   /**< Linked list. */
106
107         char name[RTE_FBK_HASH_NAMESIZE];       /**< Name of the hash. */
108         uint32_t entries;               /**< Total number of entries. */
109         uint32_t entries_per_bucket;    /**< Number of entries in a bucket. */
110         uint32_t used_entries;          /**< How many entries are used. */
111         uint32_t bucket_mask;           /**< To find which bucket the key is in. */
112         uint32_t bucket_shift;          /**< Convert bucket to table offset. */
113         rte_fbk_hash_fn hash_func;      /**< The hash function. */
114         uint32_t init_val;              /**< For initialising hash function. */
115
116         /** A flat table of all buckets. */
117         union rte_fbk_hash_entry t[0];
118 };
119
120 /**
121  * Find the offset into hash table of the bucket containing a particular key.
122  *
123  * @param ht
124  *   Pointer to hash table.
125  * @param key
126  *   Key to calculate bucket for.
127  * @return
128  *   Offset into hash table.
129  */
130 static inline uint32_t
131 rte_fbk_hash_get_bucket(const struct rte_fbk_hash_table *ht, uint32_t key)
132 {
133         return (ht->hash_func(key, ht->init_val) & ht->bucket_mask) <<
134                         ht->bucket_shift;
135 }
136
137
138 /**
139  * Add a key to an existing hash table. This operation is not multi-thread safe
140  * and should only be called from one thread.
141  *
142  * @param ht
143  *   Hash table to add the key to.
144  * @param key
145  *   Key to add to the hash table.
146  * @param value
147  *   Value to associate with key.
148  * @return
149  *   0 if ok, or negative value on error.
150  */
151 static inline int
152 rte_fbk_hash_add_key(struct rte_fbk_hash_table *ht,
153                         uint32_t key, uint16_t value)
154 {
155         /*
156          * The writing of a new value to the hash table is done as a single
157          * 64bit operation. This should help prevent individual entries being
158          * corrupted due to race conditions, but it's still possible to
159          * overwrite entries that have just been made valid.
160          */
161         const uint64_t new_entry = ((uint64_t)(key) << 32) |
162                         ((uint64_t)(value) << 16) |
163                         1;  /* 1 = is_entry bit. */
164         const uint32_t bucket = rte_fbk_hash_get_bucket(ht, key);
165         uint32_t i;
166
167         for (i = 0; i < ht->entries_per_bucket; i++) {
168                 /* Set entry if unused. */
169                 if (! ht->t[bucket + i].entry.is_entry) {
170                         ht->t[bucket + i].whole_entry = new_entry;
171                         ht->used_entries++;
172                         return 0;
173                 }
174                 /* Change value if key already exists. */
175                 if (ht->t[bucket + i].entry.key == key) {
176                         ht->t[bucket + i].entry.value = value;
177                         return 0;
178                 }
179         }
180
181         return -ENOSPC; /* No space in bucket. */
182 }
183
184 /**
185  * Remove a key from an existing hash table. This operation is not multi-thread
186  * safe and should only be called from one thread.
187  *
188  * @param ht
189  *   Hash table to remove the key from.
190  * @param key
191  *   Key to remove from the hash table.
192  * @return
193  *   0 if ok, or negative value on error.
194  */
195 static inline int
196 rte_fbk_hash_delete_key(struct rte_fbk_hash_table *ht, uint32_t key)
197 {
198         const uint32_t bucket = rte_fbk_hash_get_bucket(ht, key);
199         uint32_t last_entry = ht->entries_per_bucket - 1;
200         uint32_t i, j;
201
202         for (i = 0; i < ht->entries_per_bucket; i++) {
203                 if (ht->t[bucket + i].entry.key == key) {
204                         /* Find last key in bucket. */
205                         for (j = ht->entries_per_bucket - 1; j > i; j-- ) {
206                                 if (! ht->t[bucket + j].entry.is_entry) {
207                                         last_entry = j - 1;
208                                 }
209                         }
210                         /*
211                          * Move the last key to the deleted key's position, and
212                          * delete the last key. lastEntry and i may be same but
213                          * it doesn't matter.
214                          */
215                         ht->t[bucket + i].whole_entry =
216                                         ht->t[bucket + last_entry].whole_entry;
217                         ht->t[bucket + last_entry].whole_entry = 0;
218
219                         ht->used_entries--;
220                         return 0;
221                 }
222         }
223
224         return -ENOENT; /* Key didn't exist. */
225 }
226
227 /**
228  * Find a key in the hash table. This operation is multi-thread safe.
229  *
230  * @param ht
231  *   Hash table to look in.
232  * @param key
233  *   Key to find.
234  * @return
235  *   The value that was associated with the key, or negative value on error.
236  */
237 static inline int
238 rte_fbk_hash_lookup(const struct rte_fbk_hash_table *ht, uint32_t key)
239 {
240         const uint32_t bucket = rte_fbk_hash_get_bucket(ht, key);
241         union rte_fbk_hash_entry current_entry;
242         uint32_t i;
243
244         for (i = 0; i < ht->entries_per_bucket; i++) {
245                 /* Single read of entry, which should be atomic. */
246                 current_entry.whole_entry = ht->t[bucket + i].whole_entry;
247                 if (! current_entry.entry.is_entry) {
248                         return -ENOENT; /* Error once we hit an empty field. */
249                 }
250                 if (current_entry.entry.key == key) {
251                         return current_entry.entry.value;
252                 }
253         }
254         return -ENOENT; /* Key didn't exist. */
255 }
256
257 /**
258  * Delete all entries in a hash table. This operation is not multi-thread
259  * safe and should only be called from one thread.
260  *
261  * @param ht
262  *   Hash table to delete entries in.
263  */
264 static inline void
265 rte_fbk_hash_clear_all(struct rte_fbk_hash_table *ht)
266 {
267         memset(ht->t, 0, sizeof(ht->t[0]) * ht->entries);
268         ht->used_entries = 0;
269 }
270
271 /**
272  * Find what fraction of entries are being used.
273  *
274  * @param ht
275  *   Hash table to find how many entries are being used in.
276  * @return
277  *   Load factor of the hash table, or negative value on error.
278  */
279 static inline double
280 rte_fbk_hash_get_load_factor(struct rte_fbk_hash_table *ht)
281 {
282         return (double)ht->used_entries / (double)ht->entries;
283 }
284
285 /**
286  * Performs a lookup for an existing hash table, and returns a pointer to
287  * the table if found.
288  *
289  * @param name
290  *   Name of the hash table to find
291  *
292  * @return
293  *   pointer to hash table structure or NULL on error with rte_errno
294  *   set appropriately. Possible rte_errno values include:
295  *    - ENOENT - required entry not available to return.
296  */
297 struct rte_fbk_hash_table *rte_fbk_hash_find_existing(const char *name);
298
299 /**
300  * Create a new hash table for use with four byte keys.
301  *
302  * @param params
303  *   Parameters used in creation of hash table.
304  *
305  * @return
306  *   Pointer to hash table structure that is used in future hash table
307  *   operations, or NULL on error with rte_errno set appropriately.
308  *   Possible rte_errno error values include:
309  *    - E_RTE_NO_CONFIG - function could not get pointer to rte_config structure
310  *    - E_RTE_SECONDARY - function was called from a secondary process instance
311  *    - E_RTE_NO_TAILQ - no tailq list could be got for the fbk hash table list
312  *    - EINVAL - invalid parameter value passed to function
313  *    - ENOSPC - the maximum number of memzones has already been allocated
314  *    - EEXIST - a memzone with the same name already exists
315  *    - ENOMEM - no appropriate memory area found in which to create memzone
316  */
317 struct rte_fbk_hash_table * \
318 rte_fbk_hash_create(const struct rte_fbk_hash_params *params);
319
320 /**
321  * Free all memory used by a hash table.
322  * Has no effect on hash tables allocated in memory zones
323  *
324  * @param ht
325  *   Hash table to deallocate.
326  */
327 void rte_fbk_hash_free(struct rte_fbk_hash_table *ht);
328
329 #ifdef __cplusplus
330 }
331 #endif
332
333 #endif /* _RTE_FBK_HASH_H_ */