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>
62 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
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); \
70 #define RETURN_IF_TRUE(cond, retval)
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
78 #include <rte_jhash.h>
79 #define DEFAULT_HASH_FUNC rte_jhash
82 /* Signature bucket size is a multiple of this value */
83 #define SIG_BUCKET_ALIGNMENT 16
85 /* Stoered key size is a multiple of this value */
86 #define KEY_ALIGNMENT 16
88 /* The high bit is always set in real signatures */
89 #define NULL_SIGNATURE 0
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)
96 &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
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)
103 return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
104 h->key_tbl_key_size]);
107 /* Returns a pointer to a key at a specific position in a specified bucket. */
109 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
111 return (void *) &bkt[pos * h->key_tbl_key_size];
114 /* Does integer division with rounding-up of result. */
115 static inline uint32_t
116 div_roundup(uint32_t numerator, uint32_t denominator)
118 return (numerator + denominator - 1) / denominator;
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)
125 return alignment * div_roundup(val, alignment);
128 /* Returns the index into the bucket of the first occurrence of a signature. */
130 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
133 for (i = 0; i < num_sigs; i++) {
134 if (sig == sig_bucket[i])
141 rte_hash_find_existing(const char *name)
143 struct rte_hash *h = NULL;
144 struct rte_tailq_entry *te;
145 struct rte_hash_list *hash_list;
147 /* check that we have an initialised tail queue */
149 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
150 rte_errno = E_RTE_NO_TAILQ;
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)
160 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
170 rte_hash_create(const struct rte_hash_parameters *params)
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;
179 /* check that we have an initialised tail queue */
181 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
182 rte_errno = E_RTE_NO_TAILQ;
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)) {
196 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
200 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
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);
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);
214 /* Total memory required for hash context */
215 mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
217 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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)
228 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
230 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
234 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
235 RTE_CACHE_LINE_SIZE, params->socket_id);
237 RTE_LOG(ERR, HASH, "memory allocation failed\n");
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;
258 te->data = (void *) h;
260 TAILQ_INSERT_TAIL(hash_list, te, next);
263 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
269 rte_hash_free(struct rte_hash *h)
271 struct rte_tailq_entry *te;
272 struct rte_hash_list *hash_list;
277 /* check that we have an initialised tail queue */
279 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
280 rte_errno = E_RTE_NO_TAILQ;
284 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
286 /* find out tailq entry */
287 TAILQ_FOREACH(te, hash_list, next) {
288 if (te->data == (void *) h)
293 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
297 TAILQ_REMOVE(hash_list, te, next);
299 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
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)
309 hash_sig_t *sig_bucket;
311 uint32_t bucket_index, i;
314 /* Get the hash signature and bucket index */
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);
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),
325 return bucket_index * h->bucket_entries + i;
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);
332 if (unlikely(pos < 0))
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;
342 rte_hash_add_key_with_hash(const struct rte_hash *h,
343 const void *key, hash_sig_t sig)
345 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
346 return __rte_hash_add_key_with_hash(h, key, sig);
350 rte_hash_add_key(const struct rte_hash *h, const void *key)
352 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
353 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
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)
360 hash_sig_t *sig_bucket;
362 uint32_t bucket_index, i;
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);
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),
375 sig_bucket[i] = NULL_SIGNATURE;
376 return bucket_index * h->bucket_entries + i;
384 rte_hash_del_key_with_hash(const struct rte_hash *h,
385 const void *key, hash_sig_t sig)
387 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
388 return __rte_hash_del_key_with_hash(h, key, sig);
392 rte_hash_del_key(const struct rte_hash *h, const void *key)
394 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
395 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
398 static inline int32_t
399 __rte_hash_lookup_with_hash(const struct rte_hash *h,
400 const void *key, hash_sig_t sig)
402 hash_sig_t *sig_bucket;
404 uint32_t bucket_index, i;
406 /* Get the hash signature and bucket index */
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);
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),
417 return bucket_index * h->bucket_entries + i;
425 rte_hash_lookup_with_hash(const struct rte_hash *h,
426 const void *key, hash_sig_t sig)
428 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
429 return __rte_hash_lookup_with_hash(h, key, sig);
433 rte_hash_lookup(const struct rte_hash *h, const void *key)
435 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
436 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
440 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
441 uint32_t num_keys, int32_t *positions)
443 uint32_t i, j, bucket_index;
444 hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
446 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
447 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
448 (positions == NULL)), -EINVAL);
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;
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));
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);
467 positions[i] = -ENOENT;
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),
474 positions[i] = bucket_index *
475 h->bucket_entries + j;