hash: don't use memzone for allocations
[dpdk.git] / lib / librte_hash / rte_hash.c
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 #include <string.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <sys/queue.h>
41
42 #include <rte_common.h>
43 #include <rte_memory.h>         /* for definition of CACHE_LINE_SIZE */
44 #include <rte_log.h>
45 #include <rte_memcpy.h>
46 #include <rte_prefetch.h>
47 #include <rte_branch_prediction.h>
48 #include <rte_memzone.h>
49 #include <rte_malloc.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_eal_memconfig.h>
53 #include <rte_per_lcore.h>
54 #include <rte_errno.h>
55 #include <rte_string_fns.h>
56 #include <rte_cpuflags.h>
57 #include <rte_log.h>
58
59 #include "rte_hash.h"
60
61
62 TAILQ_HEAD(rte_hash_list, rte_hash);
63
64 /* Macro to enable/disable run-time checking of function parameters */
65 #if defined(RTE_LIBRTE_HASH_DEBUG)
66 #define RETURN_IF_TRUE(cond, retval) do { \
67         if (cond) return (retval); \
68 } while (0)
69 #else
70 #define RETURN_IF_TRUE(cond, retval)
71 #endif
72
73 /* Hash function used if none is specified */
74 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
75 #include <rte_hash_crc.h>
76 #define DEFAULT_HASH_FUNC       rte_hash_crc
77 #else
78 #include <rte_jhash.h>
79 #define DEFAULT_HASH_FUNC       rte_jhash
80 #endif
81
82 /* Signature bucket size is a multiple of this value */
83 #define SIG_BUCKET_ALIGNMENT    16
84
85 /* Stoered key size is a multiple of this value */
86 #define KEY_ALIGNMENT           16
87
88 /* The high bit is always set in real signatures */
89 #define NULL_SIGNATURE          0
90
91 /* Returns a pointer to the first signature in specified bucket. */
92 static inline hash_sig_t *
93 get_sig_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
94 {
95         return (hash_sig_t *)
96                         &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
97 }
98
99 /* Returns a pointer to the first key in specified bucket. */
100 static inline uint8_t *
101 get_key_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
102 {
103         return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
104                                      h->key_tbl_key_size]);
105 }
106
107 /* Returns a pointer to a key at a specific position in a specified bucket. */
108 static inline void *
109 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
110 {
111         return (void *) &bkt[pos * h->key_tbl_key_size];
112 }
113
114 /* Does integer division with rounding-up of result. */
115 static inline uint32_t
116 div_roundup(uint32_t numerator, uint32_t denominator)
117 {
118         return (numerator + denominator - 1) / denominator;
119 }
120
121 /* Increases a size (if needed) to a multiple of alignment. */
122 static inline uint32_t
123 align_size(uint32_t val, uint32_t alignment)
124 {
125         return alignment * div_roundup(val, alignment);
126 }
127
128 /* Returns the index into the bucket of the first occurrence of a signature. */
129 static inline int
130 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
131 {
132         uint32_t i;
133         for (i = 0; i < num_sigs; i++) {
134                 if (sig == sig_bucket[i])
135                         return i;
136         }
137         return -1;
138 }
139
140 struct rte_hash *
141 rte_hash_find_existing(const char *name)
142 {
143         struct rte_hash *h;
144         struct rte_hash_list *hash_list;
145
146         /* check that we have an initialised tail queue */
147         if ((hash_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
148                 rte_errno = E_RTE_NO_TAILQ;
149                 return NULL;
150         }
151
152         TAILQ_FOREACH(h, hash_list, next) {
153                 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
154                         break;
155         }
156         if (h == NULL)
157                 rte_errno = ENOENT;
158         return h;
159 }
160
161 struct rte_hash *
162 rte_hash_create(const struct rte_hash_parameters *params)
163 {
164         struct rte_hash *h = NULL;
165         uint32_t num_buckets, sig_bucket_size, key_size,
166                 hash_tbl_size, sig_tbl_size, key_tbl_size, mem_size;
167         char hash_name[RTE_HASH_NAMESIZE];
168         struct rte_hash_list *hash_list;
169
170         /* check that we have an initialised tail queue */
171         if ((hash_list = 
172              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
173                 rte_errno = E_RTE_NO_TAILQ;
174                 return NULL;    
175         }
176
177         /* Check for valid parameters */
178         if ((params == NULL) ||
179                         (params->entries > RTE_HASH_ENTRIES_MAX) ||
180                         (params->bucket_entries > RTE_HASH_BUCKET_ENTRIES_MAX) ||
181                         (params->entries < params->bucket_entries) ||
182                         !rte_is_power_of_2(params->entries) ||
183                         !rte_is_power_of_2(params->bucket_entries) ||
184                         (params->key_len == 0) ||
185                         (params->key_len > RTE_HASH_KEY_LENGTH_MAX)) {
186                 rte_errno = EINVAL;
187                 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
188                 return NULL;
189         }
190
191         rte_snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
192
193         /* Calculate hash dimensions */
194         num_buckets = params->entries / params->bucket_entries;
195         sig_bucket_size = align_size(params->bucket_entries *
196                                      sizeof(hash_sig_t), SIG_BUCKET_ALIGNMENT);
197         key_size =  align_size(params->key_len, KEY_ALIGNMENT);
198
199         hash_tbl_size = align_size(sizeof(struct rte_hash), CACHE_LINE_SIZE);
200         sig_tbl_size = align_size(num_buckets * sig_bucket_size,
201                                   CACHE_LINE_SIZE);
202         key_tbl_size = align_size(num_buckets * key_size *
203                                   params->bucket_entries, CACHE_LINE_SIZE);
204         
205         /* Total memory required for hash context */
206         mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
207
208         /* guarantee there's no existing */
209         TAILQ_FOREACH(h, hash_list, next) {
210                 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
211                         break;
212         }
213         if (h != NULL)
214                 return NULL;
215
216         h = (struct rte_hash *)rte_zmalloc(hash_name, mem_size,
217                                            CACHE_LINE_SIZE);
218         if (h == NULL) {
219                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
220                 return NULL;
221         }
222
223         /* Setup hash context */
224         rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
225         h->entries = params->entries;
226         h->bucket_entries = params->bucket_entries;
227         h->key_len = params->key_len;
228         h->hash_func_init_val = params->hash_func_init_val;
229         h->num_buckets = num_buckets;
230         h->bucket_bitmask = h->num_buckets - 1;
231         h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
232         h->sig_tbl = (uint8_t *)h + hash_tbl_size;
233         h->sig_tbl_bucket_size = sig_bucket_size;
234         h->key_tbl = h->sig_tbl + sig_tbl_size;
235         h->key_tbl_key_size = key_size;
236         h->hash_func = (params->hash_func == NULL) ?
237                 DEFAULT_HASH_FUNC : params->hash_func;
238
239         TAILQ_INSERT_TAIL(hash_list, h, next);
240         return h;
241 }
242
243 void
244 rte_hash_free(struct rte_hash *h)
245 {
246         if (h == NULL)
247                 return;
248
249         RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_HASH, rte_hash_list, h);
250         rte_free(h);
251 }
252
253 int32_t
254 rte_hash_add_key(const struct rte_hash *h, const void *key)
255 {
256         hash_sig_t sig, *sig_bucket;
257         uint8_t *key_bucket;
258         uint32_t bucket_index, i;
259         int32_t pos;
260
261         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
262
263         /* Get the hash signature and bucket index */
264         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
265         bucket_index = sig & h->bucket_bitmask;
266         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
267         key_bucket = get_key_tbl_bucket(h, bucket_index);
268
269         /* Check if key is already present in the hash */
270         for (i = 0; i < h->bucket_entries; i++) {
271                 if ((sig == sig_bucket[i]) &&
272                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
273                                   h->key_len) == 0)) {
274                         return bucket_index * h->bucket_entries + i;
275                 }
276         }
277
278         /* Check if any free slot within the bucket to add the new key */
279         pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
280
281         if (unlikely(pos < 0))
282                 return -ENOSPC;
283
284         /* Add the new key to the bucket */
285         sig_bucket[pos] = sig;
286         rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
287         return bucket_index * h->bucket_entries + pos;
288 }
289
290 int32_t
291 rte_hash_del_key(const struct rte_hash *h, const void *key)
292 {
293         hash_sig_t sig, *sig_bucket;
294         uint8_t *key_bucket;
295         uint32_t bucket_index, i;
296
297         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
298
299         /* Get the hash signature and bucket index */
300         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
301         bucket_index = sig & h->bucket_bitmask;
302         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
303         key_bucket = get_key_tbl_bucket(h, bucket_index);
304
305         /* Check if key is already present in the hash */
306         for (i = 0; i < h->bucket_entries; i++) {
307                 if ((sig == sig_bucket[i]) &&
308                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
309                                   h->key_len) == 0)) {
310                         sig_bucket[i] = NULL_SIGNATURE;
311                         return bucket_index * h->bucket_entries + i;
312                 }
313         }
314
315         return -ENOENT;
316 }
317
318 int32_t
319 rte_hash_lookup(const struct rte_hash *h, const void *key)
320 {
321         hash_sig_t sig, *sig_bucket;
322         uint8_t *key_bucket;
323         uint32_t bucket_index, i;
324
325         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
326
327         /* Get the hash signature and bucket index */
328         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
329         bucket_index = sig & h->bucket_bitmask;
330         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
331         key_bucket = get_key_tbl_bucket(h, bucket_index);
332
333         /* Check if key is already present in the hash */
334         for (i = 0; i < h->bucket_entries; i++) {
335                 if ((sig == sig_bucket[i]) &&
336                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
337                                   h->key_len) == 0)) {
338                         return bucket_index * h->bucket_entries + i;
339                 }
340         }
341
342         return -ENOENT;
343 }
344
345 int
346 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
347                       uint32_t num_keys, int32_t *positions)
348 {
349         uint32_t i, j, bucket_index;
350         hash_sig_t sigs[RTE_HASH_LOOKUP_MULTI_MAX];
351
352         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
353                         (num_keys > RTE_HASH_LOOKUP_MULTI_MAX) ||
354                         (positions == NULL)), -EINVAL);
355
356         /* Get the hash signature and bucket index */
357         for (i = 0; i < num_keys; i++) {
358                 sigs[i] = h->hash_func(keys[i], h->key_len,
359                                 h->hash_func_init_val) | h->sig_msb;
360                 bucket_index = sigs[i] & h->bucket_bitmask;
361
362                 /* Pre-fetch relevant buckets */
363                 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
364                 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
365         }
366
367         /* Check if key is already present in the hash */
368         for (i = 0; i < num_keys; i++) {
369                 bucket_index = sigs[i] & h->bucket_bitmask;
370                 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
371                 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
372
373                 positions[i] = -ENOENT;
374
375                 for (j = 0; j < h->bucket_entries; j++) {
376                         if ((sigs[i] == sig_bucket[j]) &&
377                             likely(memcmp(keys[i],
378                                           get_key_from_bucket(h, key_bucket, j),
379                                           h->key_len) == 0)) {
380                                 positions[i] = bucket_index *
381                                         h->bucket_entries + j;
382                                 break;
383                         }
384                 }
385         }
386
387         return 0;
388 }