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>
23 #include <rte_tailq.h>
25 #include "rte_fbk_hash.h"
27 TAILQ_HEAD(rte_fbk_hash_list
, rte_tailq_entry
);
29 static struct rte_tailq_elem rte_fbk_hash_tailq
= {
30 .name
= "RTE_FBK_HASH",
32 EAL_REGISTER_TAILQ(rte_fbk_hash_tailq
)
35 * Performs a lookup for an existing hash table, and returns a pointer to
39 * Name of the hash table to find
42 * pointer to hash table structure or NULL on error.
44 struct rte_fbk_hash_table
*
45 rte_fbk_hash_find_existing(const char *name
)
47 struct rte_fbk_hash_table
*h
= NULL
;
48 struct rte_tailq_entry
*te
;
49 struct rte_fbk_hash_list
*fbk_hash_list
;
51 fbk_hash_list
= RTE_TAILQ_CAST(rte_fbk_hash_tailq
.head
,
54 rte_mcfg_tailq_read_lock();
55 TAILQ_FOREACH(te
, fbk_hash_list
, next
) {
56 h
= (struct rte_fbk_hash_table
*) te
->data
;
57 if (strncmp(name
, h
->name
, RTE_FBK_HASH_NAMESIZE
) == 0)
60 rte_mcfg_tailq_read_unlock();
69 * Create a new hash table for use with four byte keys.
72 * Parameters used in creation of hash table.
75 * Pointer to hash table structure that is used in future hash table
76 * operations, or NULL on error.
78 struct rte_fbk_hash_table
*
79 rte_fbk_hash_create(const struct rte_fbk_hash_params
*params
)
81 struct rte_fbk_hash_table
*ht
= NULL
;
82 struct rte_tailq_entry
*te
;
83 char hash_name
[RTE_FBK_HASH_NAMESIZE
];
84 const uint32_t mem_size
=
85 sizeof(*ht
) + (sizeof(ht
->t
[0]) * params
->entries
);
87 struct rte_fbk_hash_list
*fbk_hash_list
;
88 rte_fbk_hash_fn default_hash_func
= (rte_fbk_hash_fn
)rte_jhash_1word
;
90 fbk_hash_list
= RTE_TAILQ_CAST(rte_fbk_hash_tailq
.head
,
93 /* Error checking of parameters. */
94 if ((!rte_is_power_of_2(params
->entries
)) ||
95 (!rte_is_power_of_2(params
->entries_per_bucket
)) ||
96 (params
->entries
== 0) ||
97 (params
->entries_per_bucket
== 0) ||
98 (params
->entries_per_bucket
> params
->entries
) ||
99 (params
->entries
> RTE_FBK_HASH_ENTRIES_MAX
) ||
100 (params
->entries_per_bucket
> RTE_FBK_HASH_ENTRIES_PER_BUCKET_MAX
)){
105 snprintf(hash_name
, sizeof(hash_name
), "FBK_%s", params
->name
);
107 rte_mcfg_tailq_write_lock();
109 /* guarantee there's no existing */
110 TAILQ_FOREACH(te
, fbk_hash_list
, next
) {
111 ht
= (struct rte_fbk_hash_table
*) te
->data
;
112 if (strncmp(params
->name
, ht
->name
, RTE_FBK_HASH_NAMESIZE
) == 0)
121 te
= rte_zmalloc("FBK_HASH_TAILQ_ENTRY", sizeof(*te
), 0);
123 RTE_LOG(ERR
, HASH
, "Failed to allocate tailq entry\n");
127 /* Allocate memory for table. */
128 ht
= rte_zmalloc_socket(hash_name
, mem_size
,
129 0, params
->socket_id
);
131 RTE_LOG(ERR
, HASH
, "Failed to allocate fbk hash table\n");
136 /* Default hash function */
137 #if defined(RTE_ARCH_X86)
138 default_hash_func
= (rte_fbk_hash_fn
)rte_hash_crc_4byte
;
139 #elif defined(RTE_ARCH_ARM64)
140 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_CRC32
))
141 default_hash_func
= (rte_fbk_hash_fn
)rte_hash_crc_4byte
;
144 /* Set up hash table context. */
145 strlcpy(ht
->name
, params
->name
, sizeof(ht
->name
));
146 ht
->entries
= params
->entries
;
147 ht
->entries_per_bucket
= params
->entries_per_bucket
;
148 ht
->used_entries
= 0;
149 ht
->bucket_mask
= (params
->entries
/ params
->entries_per_bucket
) - 1;
150 for (ht
->bucket_shift
= 0, i
= 1;
151 (params
->entries_per_bucket
& i
) == 0;
152 ht
->bucket_shift
++, i
<<= 1)
153 ; /* empty loop body */
155 if (params
->hash_func
!= NULL
) {
156 ht
->hash_func
= params
->hash_func
;
157 ht
->init_val
= params
->init_val
;
160 ht
->hash_func
= default_hash_func
;
161 ht
->init_val
= RTE_FBK_HASH_INIT_VAL_DEFAULT
;
164 te
->data
= (void *) ht
;
166 TAILQ_INSERT_TAIL(fbk_hash_list
, te
, next
);
169 rte_mcfg_tailq_write_unlock();
175 * Free all memory used by a hash table.
178 * Hash table to deallocate.
181 rte_fbk_hash_free(struct rte_fbk_hash_table
*ht
)
183 struct rte_tailq_entry
*te
;
184 struct rte_fbk_hash_list
*fbk_hash_list
;
189 fbk_hash_list
= RTE_TAILQ_CAST(rte_fbk_hash_tailq
.head
,
192 rte_mcfg_tailq_write_lock();
194 /* find out tailq entry */
195 TAILQ_FOREACH(te
, fbk_hash_list
, next
) {
196 if (te
->data
== (void *) ht
)
201 rte_mcfg_tailq_write_unlock();
205 TAILQ_REMOVE(fbk_hash_list
, te
, next
);
207 rte_mcfg_tailq_write_unlock();