hash: select default hash by looking at SSE flags
[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         /* Allocate as a memzone, or in normal memory space */
217 #if defined(RTE_LIBRTE_HASH_USE_MEMZONE)
218         const struct rte_memzone *mz;
219         mz = rte_memzone_reserve(hash_name, mem_size, params->socket_id, 0);
220         if (mz == NULL) {
221                 RTE_LOG(ERR, HASH, "memzone reservation failed\n");
222                 return NULL;
223         }
224         memset(mz->addr, 0, mem_size);
225         h = (struct rte_hash *)mz->addr;
226 #else
227         h = (struct rte_hash *)rte_zmalloc(hash_name, mem_size,
228                                            CACHE_LINE_SIZE);
229         if (h == NULL) {
230                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
231                 return NULL;
232         }
233 #endif
234
235         /* Setup hash context */
236         rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
237         h->entries = params->entries;
238         h->bucket_entries = params->bucket_entries;
239         h->key_len = params->key_len;
240         h->hash_func_init_val = params->hash_func_init_val;
241         h->num_buckets = num_buckets;
242         h->bucket_bitmask = h->num_buckets - 1;
243         h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
244         h->sig_tbl = (uint8_t *)h + hash_tbl_size;
245         h->sig_tbl_bucket_size = sig_bucket_size;
246         h->key_tbl = h->sig_tbl + sig_tbl_size;
247         h->key_tbl_key_size = key_size;
248         h->hash_func = (params->hash_func == NULL) ?
249                 DEFAULT_HASH_FUNC : params->hash_func;
250
251         TAILQ_INSERT_TAIL(hash_list, h, next);
252         return h;
253 }
254
255 void
256 rte_hash_free(struct rte_hash *h)
257 {
258         if (h == NULL)
259                 return;
260
261 #if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
262         RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_HASH, rte_hash_list, h);
263         rte_free(h);
264 #endif
265         /* No way to deallocate memzones */
266         return;
267 }
268
269 int32_t
270 rte_hash_add_key(const struct rte_hash *h, const void *key)
271 {
272         hash_sig_t sig, *sig_bucket;
273         uint8_t *key_bucket;
274         uint32_t bucket_index, i;
275         int32_t pos;
276
277         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
278
279         /* Get the hash signature and bucket index */
280         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
281         bucket_index = sig & h->bucket_bitmask;
282         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
283         key_bucket = get_key_tbl_bucket(h, bucket_index);
284
285         /* Check if key is already present in the hash */
286         for (i = 0; i < h->bucket_entries; i++) {
287                 if ((sig == sig_bucket[i]) &&
288                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
289                                   h->key_len) == 0)) {
290                         return bucket_index * h->bucket_entries + i;
291                 }
292         }
293
294         /* Check if any free slot within the bucket to add the new key */
295         pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
296
297         if (unlikely(pos < 0))
298                 return -ENOSPC;
299
300         /* Add the new key to the bucket */
301         sig_bucket[pos] = sig;
302         rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
303         return bucket_index * h->bucket_entries + pos;
304 }
305
306 int32_t
307 rte_hash_del_key(const struct rte_hash *h, const void *key)
308 {
309         hash_sig_t sig, *sig_bucket;
310         uint8_t *key_bucket;
311         uint32_t bucket_index, i;
312
313         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
314
315         /* Get the hash signature and bucket index */
316         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
317         bucket_index = sig & h->bucket_bitmask;
318         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
319         key_bucket = get_key_tbl_bucket(h, bucket_index);
320
321         /* Check if key is already present in the hash */
322         for (i = 0; i < h->bucket_entries; i++) {
323                 if ((sig == sig_bucket[i]) &&
324                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
325                                   h->key_len) == 0)) {
326                         sig_bucket[i] = NULL_SIGNATURE;
327                         return bucket_index * h->bucket_entries + i;
328                 }
329         }
330
331         return -ENOENT;
332 }
333
334 int32_t
335 rte_hash_lookup(const struct rte_hash *h, const void *key)
336 {
337         hash_sig_t sig, *sig_bucket;
338         uint8_t *key_bucket;
339         uint32_t bucket_index, i;
340
341         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
342
343         /* Get the hash signature and bucket index */
344         sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
345         bucket_index = sig & h->bucket_bitmask;
346         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
347         key_bucket = get_key_tbl_bucket(h, bucket_index);
348
349         /* Check if key is already present in the hash */
350         for (i = 0; i < h->bucket_entries; i++) {
351                 if ((sig == sig_bucket[i]) &&
352                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
353                                   h->key_len) == 0)) {
354                         return bucket_index * h->bucket_entries + i;
355                 }
356         }
357
358         return -ENOENT;
359 }
360
361 int
362 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
363                       uint32_t num_keys, int32_t *positions)
364 {
365         uint32_t i, j, bucket_index;
366         hash_sig_t sigs[RTE_HASH_LOOKUP_MULTI_MAX];
367
368         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
369                         (num_keys > RTE_HASH_LOOKUP_MULTI_MAX) ||
370                         (positions == NULL)), -EINVAL);
371
372         /* Get the hash signature and bucket index */
373         for (i = 0; i < num_keys; i++) {
374                 sigs[i] = h->hash_func(keys[i], h->key_len,
375                                 h->hash_func_init_val) | h->sig_msb;
376                 bucket_index = sigs[i] & h->bucket_bitmask;
377
378                 /* Pre-fetch relevant buckets */
379                 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
380                 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
381         }
382
383         /* Check if key is already present in the hash */
384         for (i = 0; i < num_keys; i++) {
385                 bucket_index = sigs[i] & h->bucket_bitmask;
386                 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
387                 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
388
389                 positions[i] = -ENOENT;
390
391                 for (j = 0; j < h->bucket_entries; j++) {
392                         if ((sigs[i] == sig_bucket[j]) &&
393                             likely(memcmp(keys[i],
394                                           get_key_from_bucket(h, key_bucket, j),
395                                           h->key_len) == 0)) {
396                                 positions[i] = bucket_index *
397                                         h->bucket_entries + j;
398                                 break;
399                         }
400                 }
401         }
402
403         return 0;
404 }