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.
40 #include <sys/queue.h>
42 #include <rte_common.h>
43 #include <rte_memory.h> /* for definition of CACHE_LINE_SIZE */
45 #include <rte_memcpy.h>
46 #include <rte_prefetch.h>
47 #include <rte_branch_prediction.h>
48 #include <rte_memzone.h>
49 #include <rte_malloc.h>
50 #include <rte_tailq.h>
52 #include <rte_eal_memconfig.h>
53 #include <rte_per_lcore.h>
54 #include <rte_errno.h>
55 #include <rte_string_fns.h>
56 #include <rte_cpuflags.h>
62 TAILQ_HEAD(rte_hash_list, rte_hash);
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)
144 struct rte_hash_list *hash_list;
146 /* check that we have an initialised tail queue */
147 if ((hash_list = RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
148 rte_errno = E_RTE_NO_TAILQ;
152 TAILQ_FOREACH(h, hash_list, next) {
153 if (strncmp(name, h->name, RTE_HASH_NAMESIZE) == 0)
162 rte_hash_create(const struct rte_hash_parameters *params)
164 struct rte_hash *h = NULL;
165 uint32_t num_buckets, sig_bucket_size, key_size,
166 hash_tbl_size, sig_tbl_size, key_tbl_size, mem_size;
167 char hash_name[RTE_HASH_NAMESIZE];
168 struct rte_hash_list *hash_list;
170 /* check that we have an initialised tail queue */
172 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_HASH, rte_hash_list)) == NULL) {
173 rte_errno = E_RTE_NO_TAILQ;
177 /* Check for valid parameters */
178 if ((params == NULL) ||
179 (params->entries > RTE_HASH_ENTRIES_MAX) ||
180 (params->bucket_entries > RTE_HASH_BUCKET_ENTRIES_MAX) ||
181 (params->entries < params->bucket_entries) ||
182 !rte_is_power_of_2(params->entries) ||
183 !rte_is_power_of_2(params->bucket_entries) ||
184 (params->key_len == 0) ||
185 (params->key_len > RTE_HASH_KEY_LENGTH_MAX)) {
187 RTE_LOG(ERR, HASH, "rte_hash_create has invalid parameters\n");
191 rte_snprintf(hash_name, sizeof(hash_name), "HT_%s", params->name);
193 /* Calculate hash dimensions */
194 num_buckets = params->entries / params->bucket_entries;
195 sig_bucket_size = align_size(params->bucket_entries *
196 sizeof(hash_sig_t), SIG_BUCKET_ALIGNMENT);
197 key_size = align_size(params->key_len, KEY_ALIGNMENT);
199 hash_tbl_size = align_size(sizeof(struct rte_hash), CACHE_LINE_SIZE);
200 sig_tbl_size = align_size(num_buckets * sig_bucket_size,
202 key_tbl_size = align_size(num_buckets * key_size *
203 params->bucket_entries, CACHE_LINE_SIZE);
205 /* Total memory required for hash context */
206 mem_size = hash_tbl_size + sig_tbl_size + key_tbl_size;
208 /* guarantee there's no existing */
209 TAILQ_FOREACH(h, hash_list, next) {
210 if (strncmp(params->name, h->name, RTE_HASH_NAMESIZE) == 0)
216 h = (struct rte_hash *)rte_zmalloc_socket(hash_name, mem_size,
217 CACHE_LINE_SIZE, params->socket_id);
219 RTE_LOG(ERR, HASH, "memory allocation failed\n");
223 /* Setup hash context */
224 rte_snprintf(h->name, sizeof(h->name), "%s", params->name);
225 h->entries = params->entries;
226 h->bucket_entries = params->bucket_entries;
227 h->key_len = params->key_len;
228 h->hash_func_init_val = params->hash_func_init_val;
229 h->num_buckets = num_buckets;
230 h->bucket_bitmask = h->num_buckets - 1;
231 h->sig_msb = 1 << (sizeof(hash_sig_t) * 8 - 1);
232 h->sig_tbl = (uint8_t *)h + hash_tbl_size;
233 h->sig_tbl_bucket_size = sig_bucket_size;
234 h->key_tbl = h->sig_tbl + sig_tbl_size;
235 h->key_tbl_key_size = key_size;
236 h->hash_func = (params->hash_func == NULL) ?
237 DEFAULT_HASH_FUNC : params->hash_func;
239 TAILQ_INSERT_TAIL(hash_list, h, next);
244 rte_hash_free(struct rte_hash *h)
249 RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_HASH, rte_hash_list, h);
254 rte_hash_add_key(const struct rte_hash *h, const void *key)
256 hash_sig_t sig, *sig_bucket;
258 uint32_t bucket_index, i;
261 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
263 /* Get the hash signature and bucket index */
264 sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
265 bucket_index = sig & h->bucket_bitmask;
266 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
267 key_bucket = get_key_tbl_bucket(h, bucket_index);
269 /* Check if key is already present in the hash */
270 for (i = 0; i < h->bucket_entries; i++) {
271 if ((sig == sig_bucket[i]) &&
272 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
274 return bucket_index * h->bucket_entries + i;
278 /* Check if any free slot within the bucket to add the new key */
279 pos = find_first(NULL_SIGNATURE, sig_bucket, h->bucket_entries);
281 if (unlikely(pos < 0))
284 /* Add the new key to the bucket */
285 sig_bucket[pos] = sig;
286 rte_memcpy(get_key_from_bucket(h, key_bucket, pos), key, h->key_len);
287 return bucket_index * h->bucket_entries + pos;
291 rte_hash_del_key(const struct rte_hash *h, const void *key)
293 hash_sig_t sig, *sig_bucket;
295 uint32_t bucket_index, i;
297 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
299 /* Get the hash signature and bucket index */
300 sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
301 bucket_index = sig & h->bucket_bitmask;
302 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
303 key_bucket = get_key_tbl_bucket(h, bucket_index);
305 /* Check if key is already present in the hash */
306 for (i = 0; i < h->bucket_entries; i++) {
307 if ((sig == sig_bucket[i]) &&
308 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
310 sig_bucket[i] = NULL_SIGNATURE;
311 return bucket_index * h->bucket_entries + i;
319 rte_hash_lookup(const struct rte_hash *h, const void *key)
321 hash_sig_t sig, *sig_bucket;
323 uint32_t bucket_index, i;
325 RETURN_IF_TRUE(((h == NULL) || (key == NULL)), -EINVAL);
327 /* Get the hash signature and bucket index */
328 sig = h->hash_func(key, h->key_len, h->hash_func_init_val) | h->sig_msb;
329 bucket_index = sig & h->bucket_bitmask;
330 sig_bucket = get_sig_tbl_bucket(h, bucket_index);
331 key_bucket = get_key_tbl_bucket(h, bucket_index);
333 /* Check if key is already present in the hash */
334 for (i = 0; i < h->bucket_entries; i++) {
335 if ((sig == sig_bucket[i]) &&
336 likely(memcmp(key, get_key_from_bucket(h, key_bucket, i),
338 return bucket_index * h->bucket_entries + i;
346 rte_hash_lookup_multi(const struct rte_hash *h, const void **keys,
347 uint32_t num_keys, int32_t *positions)
349 uint32_t i, j, bucket_index;
350 hash_sig_t sigs[RTE_HASH_LOOKUP_MULTI_MAX];
352 RETURN_IF_TRUE(((h == NULL) || (keys == NULL) || (num_keys == 0) ||
353 (num_keys > RTE_HASH_LOOKUP_MULTI_MAX) ||
354 (positions == NULL)), -EINVAL);
356 /* Get the hash signature and bucket index */
357 for (i = 0; i < num_keys; i++) {
358 sigs[i] = h->hash_func(keys[i], h->key_len,
359 h->hash_func_init_val) | h->sig_msb;
360 bucket_index = sigs[i] & h->bucket_bitmask;
362 /* Pre-fetch relevant buckets */
363 rte_prefetch1((void *) get_sig_tbl_bucket(h, bucket_index));
364 rte_prefetch1((void *) get_key_tbl_bucket(h, bucket_index));
367 /* Check if key is already present in the hash */
368 for (i = 0; i < num_keys; i++) {
369 bucket_index = sigs[i] & h->bucket_bitmask;
370 hash_sig_t *sig_bucket = get_sig_tbl_bucket(h, bucket_index);
371 uint8_t *key_bucket = get_key_tbl_bucket(h, bucket_index);
373 positions[i] = -ENOENT;
375 for (j = 0; j < h->bucket_entries; j++) {
376 if ((sigs[i] == sig_bucket[j]) &&
377 likely(memcmp(keys[i],
378 get_key_from_bucket(h, key_bucket, j),
380 positions[i] = bucket_index *
381 h->bucket_entries + j;