4 * Copyright(c) 2010-2012 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.
33 * version: DPDK.L.1.2.3-3
41 #include <sys/queue.h>
43 #include <rte_common.h>
44 #include <rte_memory.h> /* for definition of CACHE_LINE_SIZE */
46 #include <rte_memcpy.h>
47 #include <rte_prefetch.h>
48 #include <rte_branch_prediction.h>
49 #include <rte_memzone.h>
50 #include <rte_malloc.h>
51 #include <rte_tailq.h>
53 #include <rte_per_lcore.h>
54 #include <rte_errno.h>
55 #include <rte_string_fns.h>
56 #include <rte_cpuflags.h>
60 #include "rte_jhash.h"
61 #include "rte_hash_crc.h"
64 TAILQ_HEAD(rte_hash_list, rte_hash);
66 /* global list of hashes (used for debug/dump) */
67 static struct rte_hash_list *hash_list;
69 /* macro to prevent duplication of list creation check code */
70 #define CHECK_HASH_LIST_CREATED() do { \
71 if (hash_list == NULL) \
72 if ((hash_list = RTE_TAILQ_RESERVE("RTE_HASH", rte_hash_list)) == NULL){ \
73 rte_errno = E_RTE_NO_TAILQ; \
78 /* Macro to enable/disable run-time checking of function parameters */
79 #if defined(RTE_LIBRTE_HASH_DEBUG)
80 #define RETURN_IF_TRUE(cond, retval) do { \
81 if (cond) return (retval); \
84 #define RETURN_IF_TRUE(cond, retval)
87 /* Hash function used if none is specified */
88 #define DEFAULT_HASH_FUNC rte_hash_crc
90 /* Signature bucket size is a multiple of this value */
91 #define SIG_BUCKET_ALIGNMENT 16
93 /* Stoered key size is a multiple of this value */
94 #define KEY_ALIGNMENT 16
96 /* The high bit is always set in real signatures */
97 #define NULL_SIGNATURE 0
99 /* Returns a pointer to the first signature in specified bucket. */
100 static inline hash_sig_t *
101 get_sig_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
103 return (hash_sig_t *)
104 &(h->sig_tbl[bucket_index * h->sig_tbl_bucket_size]);
107 /* Returns a pointer to the first key in specified bucket. */
108 static inline uint8_t *
109 get_key_tbl_bucket(const struct rte_hash *h, uint32_t bucket_index)
111 return (uint8_t *) &(h->key_tbl[bucket_index * h->bucket_entries *
112 h->key_tbl_key_size]);
115 /* Returns a pointer to a key at a specific position in a specified bucket. */
117 get_key_from_bucket(const struct rte_hash *h, uint8_t *bkt, uint32_t pos)
119 return (void *) &bkt[pos * h->key_tbl_key_size];
122 /* Does integer division with rounding-up of result. */
123 static inline uint32_t
124 div_roundup(uint32_t numerator, uint32_t denominator)
126 return (numerator + denominator - 1) / denominator;
129 /* Increases a size (if needed) to a multiple of alignment. */
130 static inline uint32_t
131 align_size(uint32_t val, uint32_t alignment)
133 return alignment * div_roundup(val, alignment);
136 /* Returns the index into the bucket of the first occurrence of a signature. */
138 find_first(uint32_t sig, const uint32_t *sig_bucket, uint32_t num_sigs)
141 for (i = 0; i < num_sigs; i++) {
142 if (sig == sig_bucket[i])
149 rte_hash_find_existing(const char *name)
153 /* check that we have an initialised tail queue */
154 CHECK_HASH_LIST_CREATED();
156 TAILQ_FOREACH(h, hash_list, next) {
157 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
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];
173 if (rte_eal_process_type() == RTE_PROC_SECONDARY){
174 rte_errno = E_RTE_SECONDARY;
178 /* check that we have an initialised tail queue */
179 CHECK_HASH_LIST_CREATED();
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 /* Allocate as a memzone, or in normal memory space */
213 #if defined(RTE_LIBRTE_HASH_USE_MEMZONE)
214 const struct rte_memzone *mz;
215 mz = rte_memzone_reserve(hash_name, mem_size, params->socket_id, 0);
217 RTE_LOG(ERR, HASH, "memzone reservation failed\n");
220 memset(mz->addr, 0, mem_size);
221 h = (struct rte_hash *)mz->addr;
223 h = (struct rte_hash *)rte_zmalloc(hash_name, mem_size,
226 RTE_LOG(ERR, HASH, "memory allocation failed\n");
231 /* Setup hash context */
232 rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
233 h->entries = params->entries;
234 h->bucket_entries = params->bucket_entries;
235 h->key_len = params->key_len;
236 h->hash_func_init_val = params->hash_func_init_val;
237 h->num_buckets = num_buckets;
238 h->bucket_bitmask = h->num_buckets - 1;
239 h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
240 h->sig_tbl = (uint8_t *)h + hash_tbl_size;
241 h->sig_tbl_bucket_size = sig_bucket_size;
242 h->key_tbl = h->sig_tbl + sig_tbl_size;
243 h->key_tbl_key_size = key_size;
244 h->hash_func = (params->hash_func == NULL) ?
245 DEFAULT_HASH_FUNC : params->hash_func;
247 if (h->hash_func == rte_hash_crc &&
248 !rte_cpu_get_flag_enabled(RTE_CPUFLAG_SSE4_2)) {
249 RTE_LOG(WARNING, HASH, "CRC32 instruction requires SSE4.2, "
250 "which is not supported on this system. "
251 "Falling back to software hash\n.");
252 h->hash_func = rte_jhash;
255 TAILQ_INSERT_TAIL(hash_list, h, next);
260 rte_hash_free(struct rte_hash *h)
264 #if !defined(RTE_LIBRTE_HASH_USE_MEMZONE)
265 TAILQ_REMOVE(hash_list, h, next);
268 /* No way to deallocate memzones */
273 rte_hash_add_key(const struct rte_hash *h, const void *key)
275 hash_sig_t sig, *sig_bucket;
277 uint32_t bucket_index, i;
280 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
282 /* Get the hash signature and bucket index */
283 sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
284 bucket_index = sig & h->bucket_bitmask;
285 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
286 key_bucket = get_key_tbl_bucket(h, bucket_index);
288 /* Check if key is already present in the hash */
289 for (i = 0; i < h->bucket_entries; i++) {
290 if ((sig == sig_bucket[i]) &&
291 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
293 return bucket_index * h->bucket_entries + i;
297 /* Check if any free slot within the bucket to add the new key */
298 pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
300 if (unlikely(pos < 0))
303 /* Add the new key to the bucket */
304 sig_bucket[pos] = sig;
305 rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
306 return bucket_index * h->bucket_entries + pos;
310 rte_hash_del_key(const struct rte_hash *h, const void *key)
312 hash_sig_t sig, *sig_bucket;
314 uint32_t bucket_index, i;
316 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
318 /* Get the hash signature and bucket index */
319 sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
320 bucket_index = sig & h->bucket_bitmask;
321 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
322 key_bucket = get_key_tbl_bucket(h, bucket_index);
324 /* Check if key is already present in the hash */
325 for (i = 0; i < h->bucket_entries; i++) {
326 if ((sig == sig_bucket[i]) &&
327 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
329 sig_bucket[i] = NULL_SIGNATURE;
330 return bucket_index * h->bucket_entries + i;
338 rte_hash_lookup(const struct rte_hash *h, const void *key)
340 hash_sig_t sig, *sig_bucket;
342 uint32_t bucket_index, i;
344 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
346 /* Get the hash signature and bucket index */
347 sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
348 bucket_index = sig & h->bucket_bitmask;
349 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
350 key_bucket = get_key_tbl_bucket(h, bucket_index);
352 /* Check if key is already present in the hash */
353 for (i = 0; i < h->bucket_entries; i++) {
354 if ((sig == sig_bucket[i]) &&
355 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
357 return bucket_index * h->bucket_entries + i;
365 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
366 uint32_t num_keys, int32_t *positions)
368 uint32_t i, j, bucket_index;
369 hash_sig_t sigs[RTE_HASH_LOOKUP_MULTI_MAX];
371 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
372 (num_keys > RTE_HASH_LOOKUP_MULTI_MAX) ||
373 (positions == NULL)), -EINVAL);
375 /* Get the hash signature and bucket index */
376 for (i = 0; i < num_keys; i++) {
377 sigs[i] = h->hash_func(keys[i], h->key_len,
378 h->hash_func_init_val) | h->sig_msb;
379 bucket_index = sigs[i] & h->bucket_bitmask;
381 /* Pre-fetch relevant buckets */
382 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
383 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
386 /* Check if key is already present in the hash */
387 for (i = 0; i < num_keys; i++) {
388 bucket_index = sigs[i] & h->bucket_bitmask;
389 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
390 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
392 positions[i] = -ENOENT;
394 for (j = 0; j < h->bucket_entries; j++) {
395 if ((sigs[i] == sig_bucket[j]) &&
396 likely(memcmp(keys[i],
397 get_key_from_bucket(h, key_bucket, j),
399 positions[i] = bucket_index *
400 h->bucket_entries + j;