4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <sys/queue.h>
41 #include <rte_common.h>
42 #include <rte_memory.h> /* for definition of RTE_CACHE_LINE_SIZE */
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>
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>
56 #include <rte_rwlock.h>
57 #include <rte_spinlock.h>
61 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
63 static struct rte_tailq_elem rte_hash_tailq = {
66 EAL_REGISTER_TAILQ(rte_hash_tailq)
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); \
74 #define RETURN_IF_TRUE(cond, retval)
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
82 #include <rte_jhash.h>
83 #define DEFAULT_HASH_FUNC rte_jhash
86 /* Signature bucket size is a multiple of this value */
87 #define SIG_BUCKET_ALIGNMENT 16
89 /* Stoered key size is a multiple of this value */
90 #define KEY_ALIGNMENT 16
92 /* The high bit is always set in real signatures */
93 #define NULL_SIGNATURE 0
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)
99 return RTE_PTR_ADD(h->sig_tbl, (bucket_index *
100 h->sig_tbl_bucket_size));
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)
107 return RTE_PTR_ADD(h->key_tbl, (bucket_index * h->bucket_entries *
108 h->key_tbl_key_size));
111 /* Returns a pointer to a key at a specific position in a specified bucket. */
113 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
115 return RTE_PTR_ADD(bkt, pos * h->key_tbl_key_size);
118 /* Does integer division with rounding-up of result. */
119 static inline uint32_t
120 div_roundup(uint32_t numerator, uint32_t denominator)
122 return (numerator + denominator - 1) / denominator;
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)
129 return alignment * div_roundup(val, alignment);
132 /* Returns the index into the bucket of the first occurrence of a signature. */
134 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
137 for (i = 0; i < num_sigs; i++) {
138 if (sig == sig_bucket[i])
145 rte_hash_find_existing(const char *name)
147 struct rte_hash *h = NULL;
148 struct rte_tailq_entry *te;
149 struct rte_hash_list *hash_list;
151 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
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)
159 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
169 rte_hash_create(const struct rte_hash_parameters *params)
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;
178 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
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)) {
190 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
194 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
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);
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);
208 /* Total memory required for hash context */
209 mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
211 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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)
222 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
224 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
228 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
229 RTE_CACHE_LINE_SIZE, params->socket_id);
231 RTE_LOG(ERR, HASH, "memory allocation failed\n");
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;
252 te->data = (void *) h;
254 TAILQ_INSERT_TAIL(hash_list, te, next);
257 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
263 rte_hash_free(struct rte_hash *h)
265 struct rte_tailq_entry *te;
266 struct rte_hash_list *hash_list;
271 hash_list = RTE_TAILQ_CAST(rte_hash_tailq.head, rte_hash_list);
273 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
275 /* find out tailq entry */
276 TAILQ_FOREACH(te, hash_list, next) {
277 if (te->data == (void *) h)
282 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
286 TAILQ_REMOVE(hash_list, te, next);
288 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
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)
298 hash_sig_t *sig_bucket;
300 uint32_t bucket_index, i;
303 /* Get the hash signature and bucket index */
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);
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),
314 return bucket_index * h->bucket_entries + i;
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);
321 if (unlikely(pos < 0))
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;
331 rte_hash_add_key_with_hash(const struct rte_hash *h,
332 const void *key, hash_sig_t sig)
334 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
335 return __rte_hash_add_key_with_hash(h, key, sig);
339 rte_hash_add_key(const struct rte_hash *h, const void *key)
341 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
342 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
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)
349 hash_sig_t *sig_bucket;
351 uint32_t bucket_index, i;
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);
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),
364 sig_bucket[i] = NULL_SIGNATURE;
365 return bucket_index * h->bucket_entries + i;
373 rte_hash_del_key_with_hash(const struct rte_hash *h,
374 const void *key, hash_sig_t sig)
376 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
377 return __rte_hash_del_key_with_hash(h, key, sig);
381 rte_hash_del_key(const struct rte_hash *h, const void *key)
383 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
384 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
387 static inline int32_t
388 __rte_hash_lookup_with_hash(const struct rte_hash *h,
389 const void *key, hash_sig_t sig)
391 hash_sig_t *sig_bucket;
393 uint32_t bucket_index, i;
395 /* Get the hash signature and bucket index */
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);
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),
406 return bucket_index * h->bucket_entries + i;
414 rte_hash_lookup_with_hash(const struct rte_hash *h,
415 const void *key, hash_sig_t sig)
417 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
418 return __rte_hash_lookup_with_hash(h, key, sig);
422 rte_hash_lookup(const struct rte_hash *h, const void *key)
424 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
425 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
429 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
430 uint32_t num_keys, int32_t *positions)
432 uint32_t i, j, bucket_index;
433 hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
435 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
436 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
437 (positions == NULL)), -EINVAL);
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;
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));
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);
456 positions[i] = -ENOENT;
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),
463 positions[i] = bucket_index *
464 h->bucket_entries + j;