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