update Intel copyright years to 2014
[dpdk.git] / lib / librte_hash / rte_hash.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 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 #include <string.h>
35 #include <stdint.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdarg.h>
39 #include <sys/queue.h>
40
41 #include <rte_common.h>
42 #include <rte_memory.h>         /* for definition of CACHE_LINE_SIZE */
43 #include <rte_log.h>
44 #include <rte_memcpy.h>
45 #include <rte_prefetch.h>
46 #include <rte_branch_prediction.h>
47 #include <rte_memzone.h>
48 #include <rte_malloc.h>
49 #include <rte_tailq.h>
50 #include <rte_eal.h>
51 #include <rte_eal_memconfig.h>
52 #include <rte_per_lcore.h>
53 #include <rte_errno.h>
54 #include <rte_string_fns.h>
55 #include <rte_cpuflags.h>
56 #include <rte_log.h>
57 #include <rte_rwlock.h>
58 #include <rte_spinlock.h>
59
60 #include "rte_hash.h"
61
62
63 TAILQ_HEAD(rte_hash_list, rte_hash);
64
65 /* Macro to enable/disable run-time checking of function parameters */
66 #if defined(RTE_LIBRTE_HASH_DEBUG)
67 #define RETURN_IF_TRUE(cond, retval) do { \
68         if (cond) return (retval); \
69 } while (0)
70 #else
71 #define RETURN_IF_TRUE(cond, retval)
72 #endif
73
74 /* Hash function used if none is specified */
75 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
76 #include <rte_hash_crc.h>
77 #define DEFAULT_HASH_FUNC       rte_hash_crc
78 #else
79 #include <rte_jhash.h>
80 #define DEFAULT_HASH_FUNC       rte_jhash
81 #endif
82
83 /* Signature bucket size is a multiple of this value */
84 #define SIG_BUCKET_ALIGNMENT    16
85
86 /* Stoered key size is a multiple of this value */
87 #define KEY_ALIGNMENT           16
88
89 /* The high bit is always set in real signatures */
90 #define NULL_SIGNATURE          0
91
92 /* Returns a pointer to the first signature in specified bucket. */
93 static inline hash_sig_t *
94 get_sig_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
95 {
96         return (hash_sig_t *)
97                         &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
98 }
99
100 /* Returns a pointer to the first key in specified bucket. */
101 static inline uint8_t *
102 get_key_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
103 {
104         return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
105                                      h->key_tbl_key_size]);
106 }
107
108 /* Returns a pointer to a key at a specific position in a specified bucket. */
109 static inline void *
110 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
111 {
112         return (void *) &bkt[pos * h->key_tbl_key_size];
113 }
114
115 /* Does integer division with rounding-up of result. */
116 static inline uint32_t
117 div_roundup(uint32_t numerator, uint32_t denominator)
118 {
119         return (numerator + denominator - 1) / denominator;
120 }
121
122 /* Increases a size (if needed) to a multiple of alignment. */
123 static inline uint32_t
124 align_size(uint32_t val, uint32_t alignment)
125 {
126         return alignment * div_roundup(val, alignment);
127 }
128
129 /* Returns the index into the bucket of the first occurrence of a signature. */
130 static inline int
131 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
132 {
133         uint32_t i;
134         for (i = 0; i < num_sigs; i++) {
135                 if (sig == sig_bucket[i])
136                         return i;
137         }
138         return -1;
139 }
140
141 struct rte_hash *
142 rte_hash_find_existing(const char *name)
143 {
144         struct rte_hash *h;
145         struct rte_hash_list *hash_list;
146
147         /* check that we have an initialised tail queue */
148         if ((hash_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
149                 rte_errno = E_RTE_NO_TAILQ;
150                 return NULL;
151         }
152
153         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
154         TAILQ_FOREACH(h, hash_list, next) {
155                 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
156                         break;
157         }
158         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
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         struct rte_hash_list *hash_list;
173
174         /* check that we have an initialised tail queue */
175         if ((hash_list = 
176              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
177                 rte_errno = E_RTE_NO_TAILQ;
178                 return NULL;    
179         }
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         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
213
214         /* guarantee there's no existing */
215         TAILQ_FOREACH(h, hash_list, next) {
216                 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
217                         break;
218         }
219         if (h != NULL)
220                 goto exit;
221
222         h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
223                                            CACHE_LINE_SIZE, params->socket_id);
224         if (h == NULL) {
225                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
226                 goto exit;
227         }
228
229         /* Setup hash context */
230         rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
231         h->entries = params->entries;
232         h->bucket_entries = params->bucket_entries;
233         h->key_len = params->key_len;
234         h->hash_func_init_val = params->hash_func_init_val;
235         h->num_buckets = num_buckets;
236         h->bucket_bitmask = h->num_buckets - 1;
237         h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
238         h->sig_tbl = (uint8_t *)h + hash_tbl_size;
239         h->sig_tbl_bucket_size = sig_bucket_size;
240         h->key_tbl = h->sig_tbl + sig_tbl_size;
241         h->key_tbl_key_size = key_size;
242         h->hash_func = (params->hash_func == NULL) ?
243                 DEFAULT_HASH_FUNC : params->hash_func;
244
245         TAILQ_INSERT_TAIL(hash_list, h, next);
246
247 exit:
248         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
249
250         return h;
251 }
252
253 void
254 rte_hash_free(struct rte_hash *h)
255 {
256         if (h == NULL)
257                 return;
258
259         RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_HASH, rte_hash_list, h);
260         rte_free(h);
261 }
262
263 static inline int32_t
264 __rte_hash_add_key_with_hash(const struct rte_hash *h, 
265                                 const void *key, hash_sig_t sig)
266 {
267         hash_sig_t *sig_bucket;
268         uint8_t *key_bucket;
269         uint32_t bucket_index, i;
270         int32_t pos;
271
272         /* Get the hash signature and bucket index */
273         sig |= h->sig_msb;
274         bucket_index = sig & h->bucket_bitmask;
275         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
276         key_bucket = get_key_tbl_bucket(h, bucket_index);
277
278         /* Check if key is already present in the hash */
279         for (i = 0; i < h->bucket_entries; i++) {
280                 if ((sig == sig_bucket[i]) &&
281                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
282                                   h->key_len) == 0)) {
283                         return bucket_index * h->bucket_entries + i;
284                 }
285         }
286
287         /* Check if any free slot within the bucket to add the new key */
288         pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
289
290         if (unlikely(pos < 0))
291                 return -ENOSPC;
292
293         /* Add the new key to the bucket */
294         sig_bucket[pos] = sig;
295         rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
296         return bucket_index * h->bucket_entries + pos;
297 }
298
299 int32_t
300 rte_hash_add_key_with_hash(const struct rte_hash *h, 
301                                 const void *key, hash_sig_t sig)
302 {
303         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
304         return __rte_hash_add_key_with_hash(h, key, sig);
305 }
306
307 int32_t
308 rte_hash_add_key(const struct rte_hash *h, const void *key)
309 {
310         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
311         return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
312 }
313
314 static inline int32_t
315 __rte_hash_del_key_with_hash(const struct rte_hash *h, 
316                                 const void *key, hash_sig_t sig)
317 {
318         hash_sig_t *sig_bucket;
319         uint8_t *key_bucket;
320         uint32_t bucket_index, i;
321
322         /* Get the hash signature and bucket index */
323         sig = sig | h->sig_msb;
324         bucket_index = sig & h->bucket_bitmask;
325         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
326         key_bucket = get_key_tbl_bucket(h, bucket_index);
327
328         /* Check if key is already present in the hash */
329         for (i = 0; i < h->bucket_entries; i++) {
330                 if ((sig == sig_bucket[i]) &&
331                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
332                                   h->key_len) == 0)) {
333                         sig_bucket[i] = NULL_SIGNATURE;
334                         return bucket_index * h->bucket_entries + i;
335                 }
336         }
337
338         return -ENOENT;
339 }
340
341 int32_t
342 rte_hash_del_key_with_hash(const struct rte_hash *h, 
343                                 const void *key, hash_sig_t sig)
344 {
345         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
346         return __rte_hash_del_key_with_hash(h, key, sig);
347 }
348
349 int32_t
350 rte_hash_del_key(const struct rte_hash *h, const void *key)
351 {
352         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
353         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
354 }
355
356 static inline int32_t
357 __rte_hash_lookup_with_hash(const struct rte_hash *h, 
358                         const void *key, hash_sig_t sig)
359 {
360         hash_sig_t *sig_bucket;
361         uint8_t *key_bucket;
362         uint32_t bucket_index, i;
363
364         /* Get the hash signature and bucket index */
365         sig |= h->sig_msb;
366         bucket_index = sig & h->bucket_bitmask;
367         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
368         key_bucket = get_key_tbl_bucket(h, bucket_index);
369
370         /* Check if key is already present in the hash */
371         for (i = 0; i < h->bucket_entries; i++) {
372                 if ((sig == sig_bucket[i]) &&
373                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
374                                   h->key_len) == 0)) {
375                         return bucket_index * h->bucket_entries + i;
376                 }
377         }
378
379         return -ENOENT;
380 }
381
382 int32_t
383 rte_hash_lookup_with_hash(const struct rte_hash *h, 
384                         const void *key, hash_sig_t sig)
385 {
386         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
387         return __rte_hash_lookup_with_hash(h, key, sig);
388 }
389
390 int32_t
391 rte_hash_lookup(const struct rte_hash *h, const void *key)
392 {
393         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
394         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
395 }
396
397 int
398 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
399                       uint32_t num_keys, int32_t *positions)
400 {
401         uint32_t i, j, bucket_index;
402         hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
403
404         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
405                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
406                         (positions == NULL)), -EINVAL);
407
408         /* Get the hash signature and bucket index */
409         for (i = 0; i < num_keys; i++) {
410                 sigs[i] = h->hash_func(keys[i], h->key_len,
411                                 h->hash_func_init_val) | h->sig_msb;
412                 bucket_index = sigs[i] & h->bucket_bitmask;
413
414                 /* Pre-fetch relevant buckets */
415                 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
416                 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
417         }
418
419         /* Check if key is already present in the hash */
420         for (i = 0; i < num_keys; i++) {
421                 bucket_index = sigs[i] & h->bucket_bitmask;
422                 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
423                 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
424
425                 positions[i] = -ENOENT;
426
427                 for (j = 0; j < h->bucket_entries; j++) {
428                         if ((sigs[i] == sig_bucket[j]) &&
429                             likely(memcmp(keys[i],
430                                           get_key_from_bucket(h, key_bucket, j),
431                                           h->key_len) == 0)) {
432                                 positions[i] = bucket_index *
433                                         h->bucket_entries + j;
434                                 break;
435                         }
436                 }
437         }
438
439         return 0;
440 }