tailq: move to dynamic tailq
[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 RTE_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_eal.h>
50 #include <rte_eal_memconfig.h>
51 #include <rte_per_lcore.h>
52 #include <rte_errno.h>
53 #include <rte_string_fns.h>
54 #include <rte_cpuflags.h>
55 #include <rte_log.h>
56 #include <rte_rwlock.h>
57 #include <rte_spinlock.h>
58
59 #include "rte_hash.h"
60
61 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
62
63 static struct rte_tailq_elem rte_hash_tailq = {
64         .name = "RTE_HASH",
65 };
66 EAL_REGISTER_TAILQ(rte_hash_tailq)
67
68 /* Macro to enable/disable run-time checking of function parameters */
69 #if defined(RTE_LIBRTE_HASH_DEBUG)
70 #define RETURN_IF_TRUE(cond, retval) do { \
71         if (cond) return (retval); \
72 } while (0)
73 #else
74 #define RETURN_IF_TRUE(cond, retval)
75 #endif
76
77 /* Hash function used if none is specified */
78 #ifdef RTE_MACHINE_CPUFLAG_SSE4_2
79 #include <rte_hash_crc.h>
80 #define DEFAULT_HASH_FUNC       rte_hash_crc
81 #else
82 #include <rte_jhash.h>
83 #define DEFAULT_HASH_FUNC       rte_jhash
84 #endif
85
86 /* Signature bucket size is a multiple of this value */
87 #define SIG_BUCKET_ALIGNMENT    16
88
89 /* Stoered key size is a multiple of this value */
90 #define KEY_ALIGNMENT           16
91
92 /* The high bit is always set in real signatures */
93 #define NULL_SIGNATURE          0
94
95 /* Returns a pointer to the first signature in specified bucket. */
96 static inline hash_sig_t *
97 get_sig_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
98 {
99         return (hash_sig_t *)
100                         &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
101 }
102
103 /* Returns a pointer to the first key in specified bucket. */
104 static inline uint8_t *
105 get_key_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
106 {
107         return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
108                                      h->key_tbl_key_size]);
109 }
110
111 /* Returns a pointer to a key at a specific position in a specified bucket. */
112 static inline void *
113 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
114 {
115         return (void *) &bkt[pos * h->key_tbl_key_size];
116 }
117
118 /* Does integer division with rounding-up of result. */
119 static inline uint32_t
120 div_roundup(uint32_t numerator, uint32_t denominator)
121 {
122         return (numerator + denominator - 1) / denominator;
123 }
124
125 /* Increases a size (if needed) to a multiple of alignment. */
126 static inline uint32_t
127 align_size(uint32_t val, uint32_t alignment)
128 {
129         return alignment * div_roundup(val, alignment);
130 }
131
132 /* Returns the index into the bucket of the first occurrence of a signature. */
133 static inline int
134 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
135 {
136         uint32_t i;
137         for (i = 0; i < num_sigs; i++) {
138                 if (sig == sig_bucket[i])
139                         return i;
140         }
141         return -1;
142 }
143
144 struct rte_hash *
145 rte_hash_find_existing(const char *name)
146 {
147         struct rte_hash *h = NULL;
148         struct rte_tailq_entry *te;
149         struct rte_hash_list *hash_list;
150
151         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
152
153         rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
154         TAILQ_FOREACH(te, hash_list, next) {
155                 h = (struct rte_hash *) te->data;
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 (te == NULL) {
162                 rte_errno = ENOENT;
163                 return NULL;
164         }
165         return h;
166 }
167
168 struct rte_hash *
169 rte_hash_create(const struct rte_hash_parameters *params)
170 {
171         struct rte_hash *h = NULL;
172         struct rte_tailq_entry *te;
173         uint32_t num_buckets, sig_bucket_size, key_size,
174                 hash_tbl_size, sig_tbl_size, key_tbl_size, mem_size;
175         char hash_name[RTE_HASH_NAMESIZE];
176         struct rte_hash_list *hash_list;
177
178         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
179
180         /* Check for valid parameters */
181         if ((params == NULL) ||
182                         (params->entries > RTE_HASH_ENTRIES_MAX) ||
183                         (params->bucket_entries > RTE_HASH_BUCKET_ENTRIES_MAX) ||
184                         (params->entries < params->bucket_entries) ||
185                         !rte_is_power_of_2(params->entries) ||
186                         !rte_is_power_of_2(params->bucket_entries) ||
187                         (params->key_len == 0) ||
188                         (params->key_len > RTE_HASH_KEY_LENGTH_MAX)) {
189                 rte_errno = EINVAL;
190                 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
191                 return NULL;
192         }
193
194         snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
195
196         /* Calculate hash dimensions */
197         num_buckets = params->entries / params->bucket_entries;
198         sig_bucket_size = align_size(params->bucket_entries *
199                                      sizeof(hash_sig_t), SIG_BUCKET_ALIGNMENT);
200         key_size =  align_size(params->key_len, KEY_ALIGNMENT);
201
202         hash_tbl_size = align_size(sizeof(struct rte_hash), RTE_CACHE_LINE_SIZE);
203         sig_tbl_size = align_size(num_buckets * sig_bucket_size,
204                                   RTE_CACHE_LINE_SIZE);
205         key_tbl_size = align_size(num_buckets * key_size *
206                                   params->bucket_entries, RTE_CACHE_LINE_SIZE);
207
208         /* Total memory required for hash context */
209         mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
210
211         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
212
213         /* guarantee there's no existing */
214         TAILQ_FOREACH(te, hash_list, next) {
215                 h = (struct rte_hash *) te->data;
216                 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
217                         break;
218         }
219         if (te != NULL)
220                 goto exit;
221
222         te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
223         if (te == NULL) {
224                 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
225                 goto exit;
226         }
227
228         h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
229                                            RTE_CACHE_LINE_SIZE, params->socket_id);
230         if (h == NULL) {
231                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
232                 rte_free(te);
233                 goto exit;
234         }
235
236         /* Setup hash context */
237         snprintf(h->name, sizeof(h->name), "%s", params->name);
238         h->entries = params->entries;
239         h->bucket_entries = params->bucket_entries;
240         h->key_len = params->key_len;
241         h->hash_func_init_val = params->hash_func_init_val;
242         h->num_buckets = num_buckets;
243         h->bucket_bitmask = h->num_buckets - 1;
244         h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
245         h->sig_tbl = (uint8_t *)h + hash_tbl_size;
246         h->sig_tbl_bucket_size = sig_bucket_size;
247         h->key_tbl = h->sig_tbl + sig_tbl_size;
248         h->key_tbl_key_size = key_size;
249         h->hash_func = (params->hash_func == NULL) ?
250                 DEFAULT_HASH_FUNC : params->hash_func;
251
252         te->data = (void *) h;
253
254         TAILQ_INSERT_TAIL(hash_list, te, next);
255
256 exit:
257         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
258
259         return h;
260 }
261
262 void
263 rte_hash_free(struct rte_hash *h)
264 {
265         struct rte_tailq_entry *te;
266         struct rte_hash_list *hash_list;
267
268         if (h == NULL)
269                 return;
270
271         hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
272
273         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
274
275         /* find out tailq entry */
276         TAILQ_FOREACH(te, hash_list, next) {
277                 if (te->data == (void *) h)
278                         break;
279         }
280
281         if (te == NULL) {
282                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
283                 return;
284         }
285
286         TAILQ_REMOVE(hash_list, te, next);
287
288         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
289
290         rte_free(h);
291         rte_free(te);
292 }
293
294 static inline int32_t
295 __rte_hash_add_key_with_hash(const struct rte_hash *h,
296                                 const void *key, hash_sig_t sig)
297 {
298         hash_sig_t *sig_bucket;
299         uint8_t *key_bucket;
300         uint32_t bucket_index, i;
301         int32_t pos;
302
303         /* Get the hash signature and bucket index */
304         sig |= h->sig_msb;
305         bucket_index = sig & h->bucket_bitmask;
306         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
307         key_bucket = get_key_tbl_bucket(h, bucket_index);
308
309         /* Check if key is already present in the hash */
310         for (i = 0; i < h->bucket_entries; i++) {
311                 if ((sig == sig_bucket[i]) &&
312                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
313                                   h->key_len) == 0)) {
314                         return bucket_index * h->bucket_entries + i;
315                 }
316         }
317
318         /* Check if any free slot within the bucket to add the new key */
319         pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
320
321         if (unlikely(pos < 0))
322                 return -ENOSPC;
323
324         /* Add the new key to the bucket */
325         sig_bucket[pos] = sig;
326         rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
327         return bucket_index * h->bucket_entries + pos;
328 }
329
330 int32_t
331 rte_hash_add_key_with_hash(const struct rte_hash *h,
332                                 const void *key, hash_sig_t sig)
333 {
334         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
335         return __rte_hash_add_key_with_hash(h, key, sig);
336 }
337
338 int32_t
339 rte_hash_add_key(const struct rte_hash *h, const void *key)
340 {
341         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
342         return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
343 }
344
345 static inline int32_t
346 __rte_hash_del_key_with_hash(const struct rte_hash *h,
347                                 const void *key, hash_sig_t sig)
348 {
349         hash_sig_t *sig_bucket;
350         uint8_t *key_bucket;
351         uint32_t bucket_index, i;
352
353         /* Get the hash signature and bucket index */
354         sig = sig | h->sig_msb;
355         bucket_index = sig & h->bucket_bitmask;
356         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
357         key_bucket = get_key_tbl_bucket(h, bucket_index);
358
359         /* Check if key is already present in the hash */
360         for (i = 0; i < h->bucket_entries; i++) {
361                 if ((sig == sig_bucket[i]) &&
362                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
363                                   h->key_len) == 0)) {
364                         sig_bucket[i] = NULL_SIGNATURE;
365                         return bucket_index * h->bucket_entries + i;
366                 }
367         }
368
369         return -ENOENT;
370 }
371
372 int32_t
373 rte_hash_del_key_with_hash(const struct rte_hash *h,
374                                 const void *key, hash_sig_t sig)
375 {
376         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
377         return __rte_hash_del_key_with_hash(h, key, sig);
378 }
379
380 int32_t
381 rte_hash_del_key(const struct rte_hash *h, const void *key)
382 {
383         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
384         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
385 }
386
387 static inline int32_t
388 __rte_hash_lookup_with_hash(const struct rte_hash *h,
389                         const void *key, hash_sig_t sig)
390 {
391         hash_sig_t *sig_bucket;
392         uint8_t *key_bucket;
393         uint32_t bucket_index, i;
394
395         /* Get the hash signature and bucket index */
396         sig |= h->sig_msb;
397         bucket_index = sig & h->bucket_bitmask;
398         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
399         key_bucket = get_key_tbl_bucket(h, bucket_index);
400
401         /* Check if key is already present in the hash */
402         for (i = 0; i < h->bucket_entries; i++) {
403                 if ((sig == sig_bucket[i]) &&
404                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
405                                   h->key_len) == 0)) {
406                         return bucket_index * h->bucket_entries + i;
407                 }
408         }
409
410         return -ENOENT;
411 }
412
413 int32_t
414 rte_hash_lookup_with_hash(const struct rte_hash *h,
415                         const void *key, hash_sig_t sig)
416 {
417         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
418         return __rte_hash_lookup_with_hash(h, key, sig);
419 }
420
421 int32_t
422 rte_hash_lookup(const struct rte_hash *h, const void *key)
423 {
424         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
425         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
426 }
427
428 int
429 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
430                       uint32_t num_keys, int32_t *positions)
431 {
432         uint32_t i, j, bucket_index;
433         hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
434
435         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
436                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
437                         (positions == NULL)), -EINVAL);
438
439         /* Get the hash signature and bucket index */
440         for (i = 0; i < num_keys; i++) {
441                 sigs[i] = h->hash_func(keys[i], h->key_len,
442                                 h->hash_func_init_val) | h->sig_msb;
443                 bucket_index = sigs[i] & h->bucket_bitmask;
444
445                 /* Pre-fetch relevant buckets */
446                 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
447                 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
448         }
449
450         /* Check if key is already present in the hash */
451         for (i = 0; i < num_keys; i++) {
452                 bucket_index = sigs[i] & h->bucket_bitmask;
453                 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
454                 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
455
456                 positions[i] = -ENOENT;
457
458                 for (j = 0; j < h->bucket_entries; j++) {
459                         if ((sigs[i] == sig_bucket[j]) &&
460                             likely(memcmp(keys[i],
461                                           get_key_from_bucket(h, key_bucket, j),
462                                           h->key_len) == 0)) {
463                                 positions[i] = bucket_index *
464                                         h->bucket_entries + j;
465                                 break;
466                         }
467                 }
468         }
469
470         return 0;
471 }