1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <sys/queue.h>
11 #include <rte_eal_memconfig.h>
12 #include <rte_malloc.h>
13 #include <rte_common.h>
14 #include <rte_errno.h>
15 #include <rte_string_fns.h>
17 #include <rte_tailq.h>
19 #include "rte_fbk_hash.h"
21 TAILQ_HEAD(rte_fbk_hash_list, rte_tailq_entry);
23 static struct rte_tailq_elem rte_fbk_hash_tailq = {
24 .name = "RTE_FBK_HASH",
26 EAL_REGISTER_TAILQ(rte_fbk_hash_tailq)
29 * Performs a lookup for an existing hash table, and returns a pointer to
33 * Name of the hash table to find
36 * pointer to hash table structure or NULL on error.
38 struct rte_fbk_hash_table *
39 rte_fbk_hash_find_existing(const char *name)
41 struct rte_fbk_hash_table *h = NULL;
42 struct rte_tailq_entry *te;
43 struct rte_fbk_hash_list *fbk_hash_list;
45 fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
48 rte_mcfg_tailq_read_lock();
49 TAILQ_FOREACH(te, fbk_hash_list, next) {
50 h = (struct rte_fbk_hash_table *) te->data;
51 if (strncmp(name, h->name, RTE_FBK_HASH_NAMESIZE) == 0)
54 rte_mcfg_tailq_read_unlock();
63 * Create a new hash table for use with four byte keys.
66 * Parameters used in creation of hash table.
69 * Pointer to hash table structure that is used in future hash table
70 * operations, or NULL on error.
72 struct rte_fbk_hash_table *
73 rte_fbk_hash_create(const struct rte_fbk_hash_params *params)
75 struct rte_fbk_hash_table *ht = NULL;
76 struct rte_tailq_entry *te;
77 char hash_name[RTE_FBK_HASH_NAMESIZE];
78 const uint32_t mem_size =
79 sizeof(*ht) + (sizeof(ht->t[0]) * params->entries);
81 struct rte_fbk_hash_list *fbk_hash_list;
82 rte_fbk_hash_fn default_hash_func = (rte_fbk_hash_fn)rte_jhash_1word;
84 fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
87 /* Error checking of parameters. */
88 if ((!rte_is_power_of_2(params->entries)) ||
89 (!rte_is_power_of_2(params->entries_per_bucket)) ||
90 (params->entries == 0) ||
91 (params->entries_per_bucket == 0) ||
92 (params->entries_per_bucket > params->entries) ||
93 (params->entries > RTE_FBK_HASH_ENTRIES_MAX) ||
94 (params->entries_per_bucket > RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX)){
99 snprintf(hash_name, sizeof(hash_name), "FBK_%s", params->name);
101 rte_mcfg_tailq_write_lock();
103 /* guarantee there's no existing */
104 TAILQ_FOREACH(te, fbk_hash_list, next) {
105 ht = (struct rte_fbk_hash_table *) te->data;
106 if (strncmp(params->name, ht->name, RTE_FBK_HASH_NAMESIZE) == 0)
115 te = rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te), 0);
117 RTE_LOG(ERR, HASH, "Failed to allocate tailq entry\n");
121 /* Allocate memory for table. */
122 ht = rte_zmalloc_socket(hash_name, mem_size,
123 0, params->socket_id);
125 RTE_LOG(ERR, HASH, "Failed to allocate fbk hash table\n");
130 /* Default hash function */
131 #if defined(RTE_ARCH_X86)
132 default_hash_func = (rte_fbk_hash_fn)rte_hash_crc_4byte;
133 #elif defined(RTE_ARCH_ARM64)
134 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32))
135 default_hash_func = (rte_fbk_hash_fn)rte_hash_crc_4byte;
138 /* Set up hash table context. */
139 strlcpy(ht->name, params->name, sizeof(ht->name));
140 ht->entries = params->entries;
141 ht->entries_per_bucket = params->entries_per_bucket;
142 ht->used_entries = 0;
143 ht->bucket_mask = (params->entries / params->entries_per_bucket) - 1;
144 for (ht->bucket_shift = 0, i = 1;
145 (params->entries_per_bucket & i) == 0;
146 ht->bucket_shift++, i <<= 1)
147 ; /* empty loop body */
149 if (params->hash_func != NULL) {
150 ht->hash_func = params->hash_func;
151 ht->init_val = params->init_val;
154 ht->hash_func = default_hash_func;
155 ht->init_val = RTE_FBK_HASH_INIT_VAL_DEFAULT;
158 te->data = (void *) ht;
160 TAILQ_INSERT_TAIL(fbk_hash_list, te, next);
163 rte_mcfg_tailq_write_unlock();
169 * Free all memory used by a hash table.
172 * Hash table to deallocate.
175 rte_fbk_hash_free(struct rte_fbk_hash_table *ht)
177 struct rte_tailq_entry *te;
178 struct rte_fbk_hash_list *fbk_hash_list;
183 fbk_hash_list = RTE_TAILQ_CAST(rte_fbk_hash_tailq.head,
186 rte_mcfg_tailq_write_lock();
188 /* find out tailq entry */
189 TAILQ_FOREACH(te, fbk_hash_list, next) {
190 if (te->data == (void *) ht)
195 rte_mcfg_tailq_write_unlock();
199 TAILQ_REMOVE(fbk_hash_list, te, next);
201 rte_mcfg_tailq_write_unlock();