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