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 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>
49 #include <rte_tailq.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>
57 #include <rte_rwlock.h>
58 #include <rte_spinlock.h>
63 TAILQ_HEAD(rte_hash_list, rte_tailq_entry);
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); \
71 #define RETURN_IF_TRUE(cond, retval)
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
79 #include <rte_jhash.h>
80 #define DEFAULT_HASH_FUNC rte_jhash
83 /* Signature bucket size is a multiple of this value */
84 #define SIG_BUCKET_ALIGNMENT 16
86 /* Stoered key size is a multiple of this value */
87 #define KEY_ALIGNMENT 16
89 /* The high bit is always set in real signatures */
90 #define NULL_SIGNATURE 0
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)
97 &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
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)
104 return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
105 h->key_tbl_key_size]);
108 /* Returns a pointer to a key at a specific position in a specified bucket. */
110 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
112 return (void *) &bkt[pos * h->key_tbl_key_size];
115 /* Does integer division with rounding-up of result. */
116 static inline uint32_t
117 div_roundup(uint32_t numerator, uint32_t denominator)
119 return (numerator + denominator - 1) / denominator;
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)
126 return alignment * div_roundup(val, alignment);
129 /* Returns the index into the bucket of the first occurrence of a signature. */
131 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
134 for (i = 0; i < num_sigs; i++) {
135 if (sig == sig_bucket[i])
142 rte_hash_find_existing(const char *name)
144 struct rte_hash *h = NULL;
145 struct rte_tailq_entry *te;
146 struct rte_hash_list *hash_list;
148 /* check that we have an initialised tail queue */
150 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
151 rte_errno = E_RTE_NO_TAILQ;
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)
161 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
171 rte_hash_create(const struct rte_hash_parameters *params)
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;
180 /* check that we have an initialised tail queue */
182 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
183 rte_errno = E_RTE_NO_TAILQ;
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)) {
197 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
201 snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
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);
209 hash_tbl_size = align_size(sizeof(struct rte_hash), CACHE_LINE_SIZE);
210 sig_tbl_size = align_size(num_buckets * sig_bucket_size,
212 key_tbl_size = align_size(num_buckets * key_size *
213 params->bucket_entries, CACHE_LINE_SIZE);
215 /* Total memory required for hash context */
216 mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
218 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
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)
229 te = rte_zmalloc("HASH_TAILQ_ENTRY", sizeof(*te), 0);
231 RTE_LOG(ERR, HASH, "tailq entry allocation failed\n");
235 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
236 CACHE_LINE_SIZE, params->socket_id);
238 RTE_LOG(ERR, HASH, "memory allocation failed\n");
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;
259 te->data = (void *) h;
261 TAILQ_INSERT_TAIL(hash_list, te, next);
264 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
270 rte_hash_free(struct rte_hash *h)
272 struct rte_tailq_entry *te;
273 struct rte_hash_list *hash_list;
278 /* check that we have an initialised tail queue */
280 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
281 rte_errno = E_RTE_NO_TAILQ;
285 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
287 /* find out tailq entry */
288 TAILQ_FOREACH(te, hash_list, next) {
289 if (te->data == (void *) h)
294 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
298 TAILQ_REMOVE(hash_list, te, next);
300 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
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)
310 hash_sig_t *sig_bucket;
312 uint32_t bucket_index, i;
315 /* Get the hash signature and bucket index */
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);
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),
326 return bucket_index * h->bucket_entries + i;
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);
333 if (unlikely(pos < 0))
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;
343 rte_hash_add_key_with_hash(const struct rte_hash *h,
344 const void *key, hash_sig_t sig)
346 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
347 return __rte_hash_add_key_with_hash(h, key, sig);
351 rte_hash_add_key(const struct rte_hash *h, const void *key)
353 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
354 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
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)
361 hash_sig_t *sig_bucket;
363 uint32_t bucket_index, i;
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);
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),
376 sig_bucket[i] = NULL_SIGNATURE;
377 return bucket_index * h->bucket_entries + i;
385 rte_hash_del_key_with_hash(const struct rte_hash *h,
386 const void *key, hash_sig_t sig)
388 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
389 return __rte_hash_del_key_with_hash(h, key, sig);
393 rte_hash_del_key(const struct rte_hash *h, const void *key)
395 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
396 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
399 static inline int32_t
400 __rte_hash_lookup_with_hash(const struct rte_hash *h,
401 const void *key, hash_sig_t sig)
403 hash_sig_t *sig_bucket;
405 uint32_t bucket_index, i;
407 /* Get the hash signature and bucket index */
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);
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),
418 return bucket_index * h->bucket_entries + i;
426 rte_hash_lookup_with_hash(const struct rte_hash *h,
427 const void *key, hash_sig_t sig)
429 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
430 return __rte_hash_lookup_with_hash(h, key, sig);
434 rte_hash_lookup(const struct rte_hash *h, const void *key)
436 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
437 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
441 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
442 uint32_t num_keys, int32_t *positions)
444 uint32_t i, j, bucket_index;
445 hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
447 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
448 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
449 (positions == NULL)), -EINVAL);
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;
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));
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);
468 positions[i] = -ENOENT;
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),
475 positions[i] = bucket_index *
476 h->bucket_entries + j;