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_hash);
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)
145 struct rte_hash_list *hash_list;
147 /* check that we have an initialised tail queue */
148 if ((hash_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
149 rte_errno = E_RTE_NO_TAILQ;
153 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
154 TAILQ_FOREACH(h, hash_list, next) {
155 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
158 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
166 rte_hash_create(const struct rte_hash_parameters *params)
168 struct rte_hash *h = NULL;
169 uint32_t num_buckets, sig_bucket_size, key_size,
170 hash_tbl_size, sig_tbl_size, key_tbl_size, mem_size;
171 char hash_name[RTE_HASH_NAMESIZE];
172 struct rte_hash_list *hash_list;
174 /* check that we have an initialised tail queue */
176 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
177 rte_errno = E_RTE_NO_TAILQ;
181 /* Check for valid parameters */
182 if ((params == NULL) ||
183 (params->entries > RTE_HASH_ENTRIES_MAX) ||
184 (params->bucket_entries > RTE_HASH_BUCKET_ENTRIES_MAX) ||
185 (params->entries < params->bucket_entries) ||
186 !rte_is_power_of_2(params->entries) ||
187 !rte_is_power_of_2(params->bucket_entries) ||
188 (params->key_len == 0) ||
189 (params->key_len > RTE_HASH_KEY_LENGTH_MAX)) {
191 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
195 rte_snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
197 /* Calculate hash dimensions */
198 num_buckets = params->entries / params->bucket_entries;
199 sig_bucket_size = align_size(params->bucket_entries *
200 sizeof(hash_sig_t), SIG_BUCKET_ALIGNMENT);
201 key_size = align_size(params->key_len, KEY_ALIGNMENT);
203 hash_tbl_size = align_size(sizeof(struct rte_hash), CACHE_LINE_SIZE);
204 sig_tbl_size = align_size(num_buckets * sig_bucket_size,
206 key_tbl_size = align_size(num_buckets * key_size *
207 params->bucket_entries, CACHE_LINE_SIZE);
209 /* Total memory required for hash context */
210 mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
212 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
214 /* guarantee there's no existing */
215 TAILQ_FOREACH(h, hash_list, next) {
216 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
222 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
223 CACHE_LINE_SIZE, params->socket_id);
225 RTE_LOG(ERR, HASH, "memory allocation failed\n");
229 /* Setup hash context */
230 rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
231 h->entries = params->entries;
232 h->bucket_entries = params->bucket_entries;
233 h->key_len = params->key_len;
234 h->hash_func_init_val = params->hash_func_init_val;
235 h->num_buckets = num_buckets;
236 h->bucket_bitmask = h->num_buckets - 1;
237 h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
238 h->sig_tbl = (uint8_t *)h + hash_tbl_size;
239 h->sig_tbl_bucket_size = sig_bucket_size;
240 h->key_tbl = h->sig_tbl + sig_tbl_size;
241 h->key_tbl_key_size = key_size;
242 h->hash_func = (params->hash_func == NULL) ?
243 DEFAULT_HASH_FUNC : params->hash_func;
245 TAILQ_INSERT_TAIL(hash_list, h, next);
248 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
254 rte_hash_free(struct rte_hash *h)
259 RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_HASH, rte_hash_list, h);
263 static inline int32_t
264 __rte_hash_add_key_with_hash(const struct rte_hash *h,
265 const void *key, hash_sig_t sig)
267 hash_sig_t *sig_bucket;
269 uint32_t bucket_index, i;
272 /* Get the hash signature and bucket index */
274 bucket_index = sig & h->bucket_bitmask;
275 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
276 key_bucket = get_key_tbl_bucket(h, bucket_index);
278 /* Check if key is already present in the hash */
279 for (i = 0; i < h->bucket_entries; i++) {
280 if ((sig == sig_bucket[i]) &&
281 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
283 return bucket_index * h->bucket_entries + i;
287 /* Check if any free slot within the bucket to add the new key */
288 pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
290 if (unlikely(pos < 0))
293 /* Add the new key to the bucket */
294 sig_bucket[pos] = sig;
295 rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
296 return bucket_index * h->bucket_entries + pos;
300 rte_hash_add_key_with_hash(const struct rte_hash *h,
301 const void *key, hash_sig_t sig)
303 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
304 return __rte_hash_add_key_with_hash(h, key, sig);
308 rte_hash_add_key(const struct rte_hash *h, const void *key)
310 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
311 return __rte_hash_add_key_with_hash(h, key, rte_hash_hash(h, key));
314 static inline int32_t
315 __rte_hash_del_key_with_hash(const struct rte_hash *h,
316 const void *key, hash_sig_t sig)
318 hash_sig_t *sig_bucket;
320 uint32_t bucket_index, i;
322 /* Get the hash signature and bucket index */
323 sig = sig | h->sig_msb;
324 bucket_index = sig & h->bucket_bitmask;
325 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
326 key_bucket = get_key_tbl_bucket(h, bucket_index);
328 /* Check if key is already present in the hash */
329 for (i = 0; i < h->bucket_entries; i++) {
330 if ((sig == sig_bucket[i]) &&
331 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
333 sig_bucket[i] = NULL_SIGNATURE;
334 return bucket_index * h->bucket_entries + i;
342 rte_hash_del_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_del_key_with_hash(h, key, sig);
350 rte_hash_del_key(const struct rte_hash *h, const void *key)
352 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
353 return __rte_hash_del_key_with_hash(h, key, rte_hash_hash(h, key));
356 static inline int32_t
357 __rte_hash_lookup_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 */
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 return bucket_index * h->bucket_entries + i;
383 rte_hash_lookup_with_hash(const struct rte_hash *h,
384 const void *key, hash_sig_t sig)
386 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
387 return __rte_hash_lookup_with_hash(h, key, sig);
391 rte_hash_lookup(const struct rte_hash *h, const void *key)
393 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
394 return __rte_hash_lookup_with_hash(h, key, rte_hash_hash(h, key));
398 rte_hash_lookup_bulk(const struct rte_hash *h, const void **keys,
399 uint32_t num_keys, int32_t *positions)
401 uint32_t i, j, bucket_index;
402 hash_sig_t sigs[RTE_HASH_LOOKUP_BULK_MAX];
404 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
405 (num_keys > RTE_HASH_LOOKUP_BULK_MAX) ||
406 (positions == NULL)), -EINVAL);
408 /* Get the hash signature and bucket index */
409 for (i = 0; i < num_keys; i++) {
410 sigs[i] = h->hash_func(keys[i], h->key_len,
411 h->hash_func_init_val) | h->sig_msb;
412 bucket_index = sigs[i] & h->bucket_bitmask;
414 /* Pre-fetch relevant buckets */
415 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
416 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
419 /* Check if key is already present in the hash */
420 for (i = 0; i < num_keys; i++) {
421 bucket_index = sigs[i] & h->bucket_bitmask;
422 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
423 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
425 positions[i] = -ENOENT;
427 for (j = 0; j < h->bucket_entries; j++) {
428 if ((sigs[i] == sig_bucket[j]) &&
429 likely(memcmp(keys[i],
430 get_key_from_bucket(h, key_bucket, j),
432 positions[i] = bucket_index *
433 h->bucket_entries + j;