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