first public release
[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  *  version: DPDK.L.1.2.3-3
34  */
35
36 #include <string.h>
37 #include <stdint.h>
38 #include <errno.h>
39 #include <stdio.h>
40 #include <stdarg.h>
41 #include <sys/queue.h>
42
43 #include <rte_common.h>
44 #include <rte_memory.h>         /* for definition of CACHE_LINE_SIZE */
45 #include <rte_log.h>
46 #include <rte_memcpy.h>
47 #include <rte_prefetch.h>
48 #include <rte_branch_prediction.h>
49 #include <rte_memzone.h>
50 #include <rte_malloc.h>
51 #include <rte_tailq.h>
52 #include <rte_eal.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 /* global list of hashes (used for debug/dump) */
67 static struct rte_hash_list *hash_list;
68
69 /* macro to prevent duplication of list creation check code */
70 #define CHECK_HASH_LIST_CREATED() do { \
71         if (hash_list == NULL) \
72                 if ((hash_list = RTE_TAILQ_RESERVE("RTE_HASH", rte_hash_list)) == NULL){ \
73                         rte_errno = E_RTE_NO_TAILQ; \
74                         return NULL; \
75                 } \
76 } while (0)
77
78 /* Macro to enable/disable run-time checking of function parameters */
79 #if defined(RTE_LIBRTE_HASH_DEBUG)
80 #define RETURN_IF_TRUE(cond, retval) do { \
81         if (cond) return (retval); \
82 } while (0)
83 #else
84 #define RETURN_IF_TRUE(cond, retval)
85 #endif
86
87 /* Hash function used if none is specified */
88 #define DEFAULT_HASH_FUNC       rte_hash_crc
89
90 /* Signature bucket size is a multiple of this value */
91 #define SIG_BUCKET_ALIGNMENT    16
92
93 /* Stoered key size is a multiple of this value */
94 #define KEY_ALIGNMENT           16
95
96 /* The high bit is always set in real signatures */
97 #define NULL_SIGNATURE          0
98
99 /* Returns a pointer to the first signature in specified bucket. */
100 static inline hash_sig_t *
101 get_sig_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
102 {
103         return (hash_sig_t *)
104                         &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
105 }
106
107 /* Returns a pointer to the first key in specified bucket. */
108 static inline uint8_t *
109 get_key_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
110 {
111         return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
112                                      h->key_tbl_key_size]);
113 }
114
115 /* Returns a pointer to a key at a specific position in a specified bucket. */
116 static inline void *
117 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
118 {
119         return (void *) &bkt[pos * h->key_tbl_key_size];
120 }
121
122 /* Does integer division with rounding-up of result. */
123 static inline uint32_t
124 div_roundup(uint32_t numerator, uint32_t denominator)
125 {
126         return (numerator + denominator - 1) / denominator;
127 }
128
129 /* Increases a size (if needed) to a multiple of alignment. */
130 static inline uint32_t
131 align_size(uint32_t val, uint32_t alignment)
132 {
133         return alignment * div_roundup(val, alignment);
134 }
135
136 /* Returns the index into the bucket of the first occurrence of a signature. */
137 static inline int
138 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
139 {
140         uint32_t i;
141         for (i = 0; i < num_sigs; i++) {
142                 if (sig == sig_bucket[i])
143                         return i;
144         }
145         return -1;
146 }
147
148 struct rte_hash *
149 rte_hash_find_existing(const char *name)
150 {
151         struct rte_hash *h;
152
153         /* check that we have an initialised tail queue */
154         CHECK_HASH_LIST_CREATED();
155
156         TAILQ_FOREACH(h, hash_list, next) {
157                 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
158                         break;
159         }
160         if (h == NULL)
161                 rte_errno = ENOENT;
162         return h;
163 }
164
165 struct rte_hash *
166 rte_hash_create(const struct rte_hash_parameters *params)
167 {
168         struct rte_hash *h = NULL;
169         uint32_t num_buckets, sig_bucket_size, key_size,
170                 hash_tbl_size, sig_tbl_size, key_tbl_size, mem_size;
171         char hash_name[RTE_HASH_NAMESIZE];
172
173         if (rte_eal_process_type() == RTE_PROC_SECONDARY){
174                 rte_errno = E_RTE_SECONDARY;
175                 return NULL;
176         }
177
178         /* check that we have an initialised tail queue */
179         CHECK_HASH_LIST_CREATED();
180
181         /* Check for valid parameters */
182         if ((params == NULL) ||
183                         (params->entries > RTE_HASH_ENTRIES_MAX) ||
184                         (params->bucket_entries > RTE_HASH_BUCKET_ENTRIES_MAX) ||
185                         (params->entries < params->bucket_entries) ||
186                         !rte_is_power_of_2(params->entries) ||
187                         !rte_is_power_of_2(params->bucket_entries) ||
188                         (params->key_len == 0) ||
189                         (params->key_len > RTE_HASH_KEY_LENGTH_MAX)) {
190                 rte_errno = EINVAL;
191                 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
192                 return NULL;
193         }
194
195         rte_snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
196
197         /* Calculate hash dimensions */
198         num_buckets = params->entries / params->bucket_entries;
199         sig_bucket_size = align_size(params->bucket_entries *
200                                      sizeof(hash_sig_t), SIG_BUCKET_ALIGNMENT);
201         key_size =  align_size(params->key_len, KEY_ALIGNMENT);
202
203         hash_tbl_size = align_size(sizeof(struct rte_hash), CACHE_LINE_SIZE);
204         sig_tbl_size = align_size(num_buckets * sig_bucket_size,
205                                   CACHE_LINE_SIZE);
206         key_tbl_size = align_size(num_buckets * key_size *
207                                   params->bucket_entries, CACHE_LINE_SIZE);
208
209         /* Total memory required for hash context */
210         mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
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 #if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
265         TAILQ_REMOVE(hash_list, h, next);
266         rte_free(h);
267 #endif
268         /* No way to deallocate memzones */
269         return;
270 }
271
272 int32_t
273 rte_hash_add_key(const struct rte_hash *h, const void *key)
274 {
275         hash_sig_t sig, *sig_bucket;
276         uint8_t *key_bucket;
277         uint32_t bucket_index, i;
278         int32_t pos;
279
280         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
281
282         /* Get the hash signature and bucket index */
283         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
284         bucket_index = sig & h->bucket_bitmask;
285         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
286         key_bucket = get_key_tbl_bucket(h, bucket_index);
287
288         /* Check if key is already present in the hash */
289         for (i = 0; i < h->bucket_entries; i++) {
290                 if ((sig == sig_bucket[i]) &&
291                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
292                                   h->key_len) == 0)) {
293                         return bucket_index * h->bucket_entries + i;
294                 }
295         }
296
297         /* Check if any free slot within the bucket to add the new key */
298         pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
299
300         if (unlikely(pos < 0))
301                 return -ENOSPC;
302
303         /* Add the new key to the bucket */
304         sig_bucket[pos] = sig;
305         rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
306         return bucket_index * h->bucket_entries + pos;
307 }
308
309 int32_t
310 rte_hash_del_key(const struct rte_hash *h, const void *key)
311 {
312         hash_sig_t sig, *sig_bucket;
313         uint8_t *key_bucket;
314         uint32_t bucket_index, i;
315
316         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
317
318         /* Get the hash signature and bucket index */
319         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
320         bucket_index = sig & h->bucket_bitmask;
321         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
322         key_bucket = get_key_tbl_bucket(h, bucket_index);
323
324         /* Check if key is already present in the hash */
325         for (i = 0; i < h->bucket_entries; i++) {
326                 if ((sig == sig_bucket[i]) &&
327                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
328                                   h->key_len) == 0)) {
329                         sig_bucket[i] = NULL_SIGNATURE;
330                         return bucket_index * h->bucket_entries + i;
331                 }
332         }
333
334         return -ENOENT;
335 }
336
337 int32_t
338 rte_hash_lookup(const struct rte_hash *h, const void *key)
339 {
340         hash_sig_t sig, *sig_bucket;
341         uint8_t *key_bucket;
342         uint32_t bucket_index, i;
343
344         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
345
346         /* Get the hash signature and bucket index */
347         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
348         bucket_index = sig & h->bucket_bitmask;
349         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
350         key_bucket = get_key_tbl_bucket(h, bucket_index);
351
352         /* Check if key is already present in the hash */
353         for (i = 0; i < h->bucket_entries; i++) {
354                 if ((sig == sig_bucket[i]) &&
355                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
356                                   h->key_len) == 0)) {
357                         return bucket_index * h->bucket_entries + i;
358                 }
359         }
360
361         return -ENOENT;
362 }
363
364 int
365 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
366                       uint32_t num_keys, int32_t *positions)
367 {
368         uint32_t i, j, bucket_index;
369         hash_sig_t sigs[RTE_HASH_LOOKUP_MULTI_MAX];
370
371         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
372                         (num_keys > RTE_HASH_LOOKUP_MULTI_MAX) ||
373                         (positions == NULL)), -EINVAL);
374
375         /* Get the hash signature and bucket index */
376         for (i = 0; i < num_keys; i++) {
377                 sigs[i] = h->hash_func(keys[i], h->key_len,
378                                 h->hash_func_init_val) | h->sig_msb;
379                 bucket_index = sigs[i] & h->bucket_bitmask;
380
381                 /* Pre-fetch relevant buckets */
382                 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
383                 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
384         }
385
386         /* Check if key is already present in the hash */
387         for (i = 0; i < num_keys; i++) {
388                 bucket_index = sigs[i] & h->bucket_bitmask;
389                 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
390                 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
391
392                 positions[i] = -ENOENT;
393
394                 for (j = 0; j < h->bucket_entries; j++) {
395                         if ((sigs[i] == sig_bucket[j]) &&
396                             likely(memcmp(keys[i],
397                                           get_key_from_bucket(h, key_bucket, j),
398                                           h->key_len) == 0)) {
399                                 positions[i] = bucket_index *
400                                         h->bucket_entries + j;
401                                 break;
402                         }
403                 }
404         }
405
406         return 0;
407 }