tailq: remove unneeded inclusions
[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
62 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
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 = NULL;
144         struct rte_tailq_entry *te;
145         struct rte_hash_list *hash_list;
146
147         /* check that we have an initialised tail queue */
148         if ((hash_list =
149                         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(te, hash_list, next) {
156                 h = (struct rte_hash *) te->data;
157                 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
158                         break;
159         }
160         rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
161
162         if (te == NULL) {
163                 rte_errno = ENOENT;
164                 return NULL;
165         }
166         return h;
167 }
168
169 struct rte_hash *
170 rte_hash_create(const struct rte_hash_parameters *params)
171 {
172         struct rte_hash *h = NULL;
173         struct rte_tailq_entry *te;
174         uint32_t num_buckets, sig_bucket_size, key_size,
175                 hash_tbl_size, sig_tbl_size, key_tbl_size, mem_size;
176         char hash_name[RTE_HASH_NAMESIZE];
177         struct rte_hash_list *hash_list;
178
179         /* check that we have an initialised tail queue */
180         if ((hash_list =
181              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
182                 rte_errno = E_RTE_NO_TAILQ;
183                 return NULL;
184         }
185
186         /* Check for valid parameters */
187         if ((params == NULL) ||
188                         (params->entries > RTE_HASH_ENTRIES_MAX) ||
189                         (params->bucket_entries > RTE_HASH_BUCKET_ENTRIES_MAX) ||
190                         (params->entries < params->bucket_entries) ||
191                         !rte_is_power_of_2(params->entries) ||
192                         !rte_is_power_of_2(params->bucket_entries) ||
193                         (params->key_len == 0) ||
194                         (params->key_len > RTE_HASH_KEY_LENGTH_MAX)) {
195                 rte_errno = EINVAL;
196                 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
197                 return NULL;
198         }
199
200         snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
201
202         /* Calculate hash dimensions */
203         num_buckets = params->entries / params->bucket_entries;
204         sig_bucket_size = align_size(params->bucket_entries *
205                                      sizeof(hash_sig_t), SIG_BUCKET_ALIGNMENT);
206         key_size =  align_size(params->key_len, KEY_ALIGNMENT);
207
208         hash_tbl_size = align_size(sizeof(struct rte_hash), RTE_CACHE_LINE_SIZE);
209         sig_tbl_size = align_size(num_buckets * sig_bucket_size,
210                                   RTE_CACHE_LINE_SIZE);
211         key_tbl_size = align_size(num_buckets * key_size *
212                                   params->bucket_entries, RTE_CACHE_LINE_SIZE);
213
214         /* Total memory required for hash context */
215         mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
216
217         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
218
219         /* guarantee there's no existing */
220         TAILQ_FOREACH(te, hash_list, next) {
221                 h = (struct rte_hash *) te->data;
222                 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
223                         break;
224         }
225         if (te != NULL)
226                 goto exit;
227
228         te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
229         if (te == NULL) {
230                 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
231                 goto exit;
232         }
233
234         h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
235                                            RTE_CACHE_LINE_SIZE, params->socket_id);
236         if (h == NULL) {
237                 RTE_LOG(ERR, HASH, "memory allocation failed\n");
238                 rte_free(te);
239                 goto exit;
240         }
241
242         /* Setup hash context */
243         snprintf(h->name, sizeof(h->name), "%s", params->name);
244         h->entries = params->entries;
245         h->bucket_entries = params->bucket_entries;
246         h->key_len = params->key_len;
247         h->hash_func_init_val = params->hash_func_init_val;
248         h->num_buckets = num_buckets;
249         h->bucket_bitmask = h->num_buckets - 1;
250         h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
251         h->sig_tbl = (uint8_t *)h + hash_tbl_size;
252         h->sig_tbl_bucket_size = sig_bucket_size;
253         h->key_tbl = h->sig_tbl + sig_tbl_size;
254         h->key_tbl_key_size = key_size;
255         h->hash_func = (params->hash_func == NULL) ?
256                 DEFAULT_HASH_FUNC : params->hash_func;
257
258         te->data = (void *) h;
259
260         TAILQ_INSERT_TAIL(hash_list, te, next);
261
262 exit:
263         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
264
265         return h;
266 }
267
268 void
269 rte_hash_free(struct rte_hash *h)
270 {
271         struct rte_tailq_entry *te;
272         struct rte_hash_list *hash_list;
273
274         if (h == NULL)
275                 return;
276
277         /* check that we have an initialised tail queue */
278         if ((hash_list =
279              RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
280                 rte_errno = E_RTE_NO_TAILQ;
281                 return;
282         }
283
284         rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
285
286         /* find out tailq entry */
287         TAILQ_FOREACH(te, hash_list, next) {
288                 if (te->data == (void *) h)
289                         break;
290         }
291
292         if (te == NULL) {
293                 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
294                 return;
295         }
296
297         TAILQ_REMOVE(hash_list, te, next);
298
299         rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
300
301         rte_free(h);
302         rte_free(te);
303 }
304
305 static inline int32_t
306 __rte_hash_add_key_with_hash(const struct rte_hash *h,
307                                 const void *key, hash_sig_t sig)
308 {
309         hash_sig_t *sig_bucket;
310         uint8_t *key_bucket;
311         uint32_t bucket_index, i;
312         int32_t pos;
313
314         /* Get the hash signature and bucket index */
315         sig |= h->sig_msb;
316         bucket_index = sig & h->bucket_bitmask;
317         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
318         key_bucket = get_key_tbl_bucket(h, bucket_index);
319
320         /* Check if key is already present in the hash */
321         for (i = 0; i < h->bucket_entries; i++) {
322                 if ((sig == sig_bucket[i]) &&
323                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
324                                   h->key_len) == 0)) {
325                         return bucket_index * h->bucket_entries + i;
326                 }
327         }
328
329         /* Check if any free slot within the bucket to add the new key */
330         pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
331
332         if (unlikely(pos < 0))
333                 return -ENOSPC;
334
335         /* Add the new key to the bucket */
336         sig_bucket[pos] = sig;
337         rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
338         return bucket_index * h->bucket_entries + pos;
339 }
340
341 int32_t
342 rte_hash_add_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_add_key_with_hash(h, key, sig);
347 }
348
349 int32_t
350 rte_hash_add_key(const struct rte_hash *h, const void *key)
351 {
352         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
353         return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
354 }
355
356 static inline int32_t
357 __rte_hash_del_key_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 = 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                         sig_bucket[i] = NULL_SIGNATURE;
376                         return bucket_index * h->bucket_entries + i;
377                 }
378         }
379
380         return -ENOENT;
381 }
382
383 int32_t
384 rte_hash_del_key_with_hash(const struct rte_hash *h,
385                                 const void *key, hash_sig_t sig)
386 {
387         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
388         return __rte_hash_del_key_with_hash(h, key, sig);
389 }
390
391 int32_t
392 rte_hash_del_key(const struct rte_hash *h, const void *key)
393 {
394         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
395         return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
396 }
397
398 static inline int32_t
399 __rte_hash_lookup_with_hash(const struct rte_hash *h,
400                         const void *key, hash_sig_t sig)
401 {
402         hash_sig_t *sig_bucket;
403         uint8_t *key_bucket;
404         uint32_t bucket_index, i;
405
406         /* Get the hash signature and bucket index */
407         sig |= h->sig_msb;
408         bucket_index = sig & h->bucket_bitmask;
409         sig_bucket = get_sig_tbl_bucket(h, bucket_index);
410         key_bucket = get_key_tbl_bucket(h, bucket_index);
411
412         /* Check if key is already present in the hash */
413         for (i = 0; i < h->bucket_entries; i++) {
414                 if ((sig == sig_bucket[i]) &&
415                     likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
416                                   h->key_len) == 0)) {
417                         return bucket_index * h->bucket_entries + i;
418                 }
419         }
420
421         return -ENOENT;
422 }
423
424 int32_t
425 rte_hash_lookup_with_hash(const struct rte_hash *h,
426                         const void *key, hash_sig_t sig)
427 {
428         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
429         return __rte_hash_lookup_with_hash(h, key, sig);
430 }
431
432 int32_t
433 rte_hash_lookup(const struct rte_hash *h, const void *key)
434 {
435         RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
436         return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
437 }
438
439 int
440 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
441                       uint32_t num_keys, int32_t *positions)
442 {
443         uint32_t i, j, bucket_index;
444         hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
445
446         RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
447                         (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
448                         (positions == NULL)), -EINVAL);
449
450         /* Get the hash signature and bucket index */
451         for (i = 0; i < num_keys; i++) {
452                 sigs[i] = h->hash_func(keys[i], h->key_len,
453                                 h->hash_func_init_val) | h->sig_msb;
454                 bucket_index = sigs[i] & h->bucket_bitmask;
455
456                 /* Pre-fetch relevant buckets */
457                 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
458                 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
459         }
460
461         /* Check if key is already present in the hash */
462         for (i = 0; i < num_keys; i++) {
463                 bucket_index = sigs[i] & h->bucket_bitmask;
464                 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
465                 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
466
467                 positions[i] = -ENOENT;
468
469                 for (j = 0; j < h->bucket_entries; j++) {
470                         if ((sigs[i] == sig_bucket[j]) &&
471                             likely(memcmp(keys[i],
472                                           get_key_from_bucket(h, key_bucket, j),
473                                           h->key_len) == 0)) {
474                                 positions[i] = bucket_index *
475                                         h->bucket_entries + j;
476                                 break;
477                         }
478                 }
479         }
480
481         return 0;
482 }