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.
41 #include <sys/queue.h>
42 #include <rte_memory.h>
43 #include <rte_memzone.h>
44 #include <rte_tailq.h>
46 #include <rte_eal_memconfig.h>
47 #include <rte_malloc.h>
48 #include <rte_common.h>
49 #include <rte_per_lcore.h>
50 #include <rte_errno.h>
51 #include <rte_string_fns.h>
52 #include <rte_cpuflags.h>
54 #include <rte_spinlock.h>
56 #include "rte_fbk_hash.h"
58 TAILQ_HEAD(rte_fbk_hash_list, rte_fbk_hash_table);
61 * Performs a lookup for an existing hash table, and returns a pointer to
65 * Name of the hash table to find
68 * pointer to hash table structure or NULL on error.
70 struct rte_fbk_hash_table *
71 rte_fbk_hash_find_existing(const char *name)
73 struct rte_fbk_hash_table *h;
74 struct rte_fbk_hash_list *fbk_hash_list;
76 /* check that we have an initialised tail queue */
78 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list)) == NULL) {
79 rte_errno = E_RTE_NO_TAILQ;
83 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
84 TAILQ_FOREACH(h, fbk_hash_list, next) {
85 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
88 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
95 * Create a new hash table for use with four byte keys.
98 * Parameters used in creation of hash table.
101 * Pointer to hash table structure that is used in future hash table
102 * operations, or NULL on error.
104 struct rte_fbk_hash_table *
105 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
107 struct rte_fbk_hash_table *ht = NULL;
108 char hash_name[RTE_FBK_HASH_NAMESIZE];
109 const uint32_t mem_size =
110 sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
112 struct rte_fbk_hash_list *fbk_hash_list;
114 /* check that we have an initialised tail queue */
116 RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list)) == NULL) {
117 rte_errno = E_RTE_NO_TAILQ;
121 /* Error checking of parameters. */
122 if ((!rte_is_power_of_2(params->entries)) ||
123 (!rte_is_power_of_2(params->entries_per_bucket)) ||
124 (params->entries == 0) ||
125 (params->entries_per_bucket == 0) ||
126 (params->entries_per_bucket > params->entries) ||
127 (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
128 (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX)){
133 rte_snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
135 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
137 /* guarantee there's no existing */
138 TAILQ_FOREACH(ht, fbk_hash_list, next) {
139 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
145 /* Allocate memory for table. */
146 ht = (struct rte_fbk_hash_table *)rte_malloc_socket(hash_name, mem_size,
147 0, params->socket_id);
151 memset(ht, 0, mem_size);
153 /* Set up hash table context. */
154 rte_snprintf(ht->name, sizeof(ht->name), "%s", params->name);
155 ht->entries = params->entries;
156 ht->entries_per_bucket = params->entries_per_bucket;
157 ht->used_entries = 0;
158 ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
159 for (ht->bucket_shift = 0, i = 1;
160 (params->entries_per_bucket & i) == 0;
161 ht->bucket_shift++, i <<= 1)
162 ; /* empty loop body */
164 if (params->hash_func != NULL) {
165 ht->hash_func = params->hash_func;
166 ht->init_val = params->init_val;
169 ht->hash_func = RTE_FBK_HASH_FUNC_DEFAULT;
170 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
173 TAILQ_INSERT_TAIL(fbk_hash_list, ht, next);
176 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
182 * Free all memory used by a hash table.
185 * Hash table to deallocate.
188 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
193 RTE_EAL_TAILQ_REMOVE(RTE_TAILQ_FBK_HASH, rte_fbk_hash_list, ht);