1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
6 #include <rte_hash_crc.h>
9 #include <mlx5_malloc.h>
11 #include "mlx5_common_utils.h"
12 #include "mlx5_common_log.h"
14 /********************* Hash List **********************/
16 static struct mlx5_hlist_entry *
17 mlx5_hlist_default_create_cb(struct mlx5_hlist *h, uint64_t key __rte_unused,
18 void *ctx __rte_unused)
20 return mlx5_malloc(MLX5_MEM_ZERO, h->entry_sz, 0, SOCKET_ID_ANY);
24 mlx5_hlist_default_remove_cb(struct mlx5_hlist *h __rte_unused,
25 struct mlx5_hlist_entry *entry)
31 mlx5_hlist_create(const char *name, uint32_t size, uint32_t entry_size,
32 uint32_t flags, mlx5_hlist_create_cb cb_create,
33 mlx5_hlist_match_cb cb_match, mlx5_hlist_remove_cb cb_remove)
40 if (!size || !cb_match || (!cb_create ^ !cb_remove))
42 /* Align to the next power of 2, 32bits integer is enough now. */
43 if (!rte_is_power_of_2(size)) {
44 act_size = rte_align32pow2(size);
45 DRV_LOG(DEBUG, "Size 0x%" PRIX32 " is not power of 2, "
46 "will be aligned to 0x%" PRIX32 ".", size, act_size);
50 alloc_size = sizeof(struct mlx5_hlist) +
51 sizeof(struct mlx5_hlist_bucket) * act_size;
52 /* Using zmalloc, then no need to initialize the heads. */
53 h = mlx5_malloc(MLX5_MEM_ZERO, alloc_size, RTE_CACHE_LINE_SIZE,
56 DRV_LOG(ERR, "No memory for hash list %s creation",
57 name ? name : "None");
61 snprintf(h->name, MLX5_HLIST_NAMESIZE, "%s", name);
62 h->table_sz = act_size;
63 h->mask = act_size - 1;
64 h->entry_sz = entry_size;
65 h->direct_key = !!(flags & MLX5_HLIST_DIRECT_KEY);
66 h->write_most = !!(flags & MLX5_HLIST_WRITE_MOST);
67 h->cb_create = cb_create ? cb_create : mlx5_hlist_default_create_cb;
68 h->cb_match = cb_match;
69 h->cb_remove = cb_remove ? cb_remove : mlx5_hlist_default_remove_cb;
70 for (i = 0; i < act_size; i++)
71 rte_rwlock_init(&h->buckets[i].lock);
72 DRV_LOG(DEBUG, "Hash list with %s size 0x%" PRIX32 " is created.",
77 static struct mlx5_hlist_entry *
78 __hlist_lookup(struct mlx5_hlist *h, uint64_t key, uint32_t idx,
79 void *ctx, bool reuse)
81 struct mlx5_hlist_head *first;
82 struct mlx5_hlist_entry *node;
85 first = &h->buckets[idx].head;
86 LIST_FOREACH(node, first, next) {
87 if (!h->cb_match(h, node, key, ctx)) {
89 __atomic_add_fetch(&node->ref_cnt, 1,
91 DRV_LOG(DEBUG, "Hash list %s entry %p "
93 h->name, (void *)node, node->ref_cnt);
101 static struct mlx5_hlist_entry *
102 hlist_lookup(struct mlx5_hlist *h, uint64_t key, uint32_t idx,
103 void *ctx, bool reuse)
105 struct mlx5_hlist_entry *node;
108 rte_rwlock_read_lock(&h->buckets[idx].lock);
109 node = __hlist_lookup(h, key, idx, ctx, reuse);
110 rte_rwlock_read_unlock(&h->buckets[idx].lock);
114 struct mlx5_hlist_entry *
115 mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx)
120 idx = (uint32_t)(key & h->mask);
122 idx = rte_hash_crc_8byte(key, 0) & h->mask;
123 return hlist_lookup(h, key, idx, ctx, false);
126 struct mlx5_hlist_entry*
127 mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key, void *ctx)
130 struct mlx5_hlist_head *first;
131 struct mlx5_hlist_bucket *b;
132 struct mlx5_hlist_entry *entry;
133 uint32_t prev_gen_cnt = 0;
136 idx = (uint32_t)(key & h->mask);
138 idx = rte_hash_crc_8byte(key, 0) & h->mask;
140 b = &h->buckets[idx];
141 /* Use write lock directly for write-most list. */
142 if (!h->write_most) {
143 prev_gen_cnt = __atomic_load_n(&b->gen_cnt, __ATOMIC_ACQUIRE);
144 entry = hlist_lookup(h, key, idx, ctx, true);
148 rte_rwlock_write_lock(&b->lock);
149 /* Check if the list changed by other threads. */
151 prev_gen_cnt != __atomic_load_n(&b->gen_cnt, __ATOMIC_ACQUIRE)) {
152 entry = __hlist_lookup(h, key, idx, ctx, true);
157 entry = h->cb_create(h, key, ctx);
160 DRV_LOG(DEBUG, "Can't allocate hash list %s entry.", h->name);
165 LIST_INSERT_HEAD(first, entry, next);
166 __atomic_add_fetch(&b->gen_cnt, 1, __ATOMIC_ACQ_REL);
167 DRV_LOG(DEBUG, "Hash list %s entry %p new: %u.",
168 h->name, (void *)entry, entry->ref_cnt);
170 rte_rwlock_write_unlock(&b->lock);
175 mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry)
177 uint32_t idx = entry->idx;
179 rte_rwlock_write_lock(&h->buckets[idx].lock);
180 MLX5_ASSERT(entry && entry->ref_cnt && entry->next.le_prev);
181 DRV_LOG(DEBUG, "Hash list %s entry %p deref: %u.",
182 h->name, (void *)entry, entry->ref_cnt);
183 if (--entry->ref_cnt) {
184 rte_rwlock_write_unlock(&h->buckets[idx].lock);
187 LIST_REMOVE(entry, next);
188 /* Set to NULL to get rid of removing action for more than once. */
189 entry->next.le_prev = NULL;
190 h->cb_remove(h, entry);
191 rte_rwlock_write_unlock(&h->buckets[idx].lock);
192 DRV_LOG(DEBUG, "Hash list %s entry %p removed.",
193 h->name, (void *)entry);
198 mlx5_hlist_destroy(struct mlx5_hlist *h)
201 struct mlx5_hlist_entry *entry;
204 for (idx = 0; idx < h->table_sz; ++idx) {
205 /* No LIST_FOREACH_SAFE, using while instead. */
206 while (!LIST_EMPTY(&h->buckets[idx].head)) {
207 entry = LIST_FIRST(&h->buckets[idx].head);
208 LIST_REMOVE(entry, next);
210 * The owner of whole element which contains data entry
211 * is the user, so it's the user's duty to do the clean
212 * up and the free work because someone may not put the
213 * hlist entry at the beginning(suggested to locate at
214 * the beginning). Or else the default free function
217 h->cb_remove(h, entry);