1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 #include <sys/queue.h>
12 #include <rte_memory.h>
14 #include <rte_eal_memconfig.h>
15 #include <rte_malloc.h>
16 #include <rte_common.h>
17 #include <rte_per_lcore.h>
18 #include <rte_errno.h>
19 #include <rte_string_fns.h>
20 #include <rte_cpuflags.h>
22 #include <rte_spinlock.h>
24 #include "rte_fbk_hash.h"
26 TAILQ_HEAD(rte_fbk_hash_list, rte_tailq_entry);
28 static struct rte_tailq_elem rte_fbk_hash_tailq = {
29 .name = "RTE_FBK_HASH",
31 EAL_REGISTER_TAILQ(rte_fbk_hash_tailq)
34 * Performs a lookup for an existing hash table, and returns a pointer to
38 * Name of the hash table to find
41 * pointer to hash table structure or NULL on error.
43 struct rte_fbk_hash_table *
44 rte_fbk_hash_find_existing(const char *name)
46 struct rte_fbk_hash_table *h = NULL;
47 struct rte_tailq_entry *te;
48 struct rte_fbk_hash_list *fbk_hash_list;
50 fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
53 rte_rwlock_read_lock(RTE_EAL_TAILQ_RWLOCK);
54 TAILQ_FOREACH(te, fbk_hash_list, next) {
55 h = (struct rte_fbk_hash_table *) te->data;
56 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
59 rte_rwlock_read_unlock(RTE_EAL_TAILQ_RWLOCK);
68 * Create a new hash table for use with four byte keys.
71 * Parameters used in creation of hash table.
74 * Pointer to hash table structure that is used in future hash table
75 * operations, or NULL on error.
77 struct rte_fbk_hash_table *
78 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
80 struct rte_fbk_hash_table *ht = NULL;
81 struct rte_tailq_entry *te;
82 char hash_name[RTE_FBK_HASH_NAMESIZE];
83 const uint32_t mem_size =
84 sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
86 struct rte_fbk_hash_list *fbk_hash_list;
87 rte_fbk_hash_fn default_hash_func = (rte_fbk_hash_fn)rte_jhash_1word;
89 fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
92 /* Error checking of parameters. */
93 if ((!rte_is_power_of_2(params->entries)) ||
94 (!rte_is_power_of_2(params->entries_per_bucket)) ||
95 (params->entries == 0) ||
96 (params->entries_per_bucket == 0) ||
97 (params->entries_per_bucket > params->entries) ||
98 (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
99 (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX)){
104 snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
106 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
108 /* guarantee there's no existing */
109 TAILQ_FOREACH(te, fbk_hash_list, next) {
110 ht = (struct rte_fbk_hash_table *) te->data;
111 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
120 te = rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te), 0);
122 RTE_LOG(ERR, HASH, "Failed to allocate tailq entry\n");
126 /* Allocate memory for table. */
127 ht = rte_zmalloc_socket(hash_name, mem_size,
128 0, params->socket_id);
130 RTE_LOG(ERR, HASH, "Failed to allocate fbk hash table\n");
135 /* Default hash function */
136 #if defined(RTE_ARCH_X86)
137 default_hash_func = (rte_fbk_hash_fn)rte_hash_crc_4byte;
138 #elif defined(RTE_ARCH_ARM64)
139 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
140 default_hash_func = (rte_fbk_hash_fn)rte_hash_crc_4byte;
143 /* Set up hash table context. */
144 snprintf(ht->name, sizeof(ht->name), "%s", params->name);
145 ht->entries = params->entries;
146 ht->entries_per_bucket = params->entries_per_bucket;
147 ht->used_entries = 0;
148 ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
149 for (ht->bucket_shift = 0, i = 1;
150 (params->entries_per_bucket & i) == 0;
151 ht->bucket_shift++, i <<= 1)
152 ; /* empty loop body */
154 if (params->hash_func != NULL) {
155 ht->hash_func = params->hash_func;
156 ht->init_val = params->init_val;
159 ht->hash_func = default_hash_func;
160 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
163 te->data = (void *) ht;
165 TAILQ_INSERT_TAIL(fbk_hash_list, te, next);
168 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
174 * Free all memory used by a hash table.
177 * Hash table to deallocate.
180 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
182 struct rte_tailq_entry *te;
183 struct rte_fbk_hash_list *fbk_hash_list;
188 fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
191 rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
193 /* find out tailq entry */
194 TAILQ_FOREACH(te, fbk_hash_list, next) {
195 if (te->data == (void *) ht)
200 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
204 TAILQ_REMOVE(fbk_hash_list, te, next);
206 rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);