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 /********************* mlx5 list ************************/
17 mlx5_list_init(struct mlx5_list_inconst *l_inconst,
18 struct mlx5_list_const *l_const,
19 struct mlx5_list_cache *gc)
21 rte_rwlock_init(&l_inconst->lock);
22 if (l_const->lcores_share) {
23 l_inconst->cache[MLX5_LIST_GLOBAL] = gc;
24 LIST_INIT(&l_inconst->cache[MLX5_LIST_GLOBAL]->h);
30 mlx5_list_create(const char *name, void *ctx, bool lcores_share,
31 mlx5_list_create_cb cb_create,
32 mlx5_list_match_cb cb_match,
33 mlx5_list_remove_cb cb_remove,
34 mlx5_list_clone_cb cb_clone,
35 mlx5_list_clone_free_cb cb_clone_free)
37 struct mlx5_list *list;
38 struct mlx5_list_cache *gc = NULL;
40 if (!cb_match || !cb_create || !cb_remove || !cb_clone ||
45 list = mlx5_malloc(MLX5_MEM_ZERO,
46 sizeof(*list) + (lcores_share ? sizeof(*gc) : 0),
52 snprintf(list->l_const.name,
53 sizeof(list->l_const.name), "%s", name);
54 list->l_const.ctx = ctx;
55 list->l_const.lcores_share = lcores_share;
56 list->l_const.cb_create = cb_create;
57 list->l_const.cb_match = cb_match;
58 list->l_const.cb_remove = cb_remove;
59 list->l_const.cb_clone = cb_clone;
60 list->l_const.cb_clone_free = cb_clone_free;
61 rte_spinlock_init(&list->l_const.lcore_lock);
63 gc = (struct mlx5_list_cache *)(list + 1);
64 if (mlx5_list_init(&list->l_inconst, &list->l_const, gc) != 0) {
68 DRV_LOG(DEBUG, "mlx5 list %s was created.", name);
72 static struct mlx5_list_entry *
73 __list_lookup(struct mlx5_list_inconst *l_inconst,
74 struct mlx5_list_const *l_const,
75 int lcore_index, void *ctx, bool reuse)
77 struct mlx5_list_entry *entry =
78 LIST_FIRST(&l_inconst->cache[lcore_index]->h);
81 while (entry != NULL) {
82 if (l_const->cb_match(l_const->ctx, entry, ctx) == 0) {
84 ret = __atomic_add_fetch(&entry->ref_cnt, 1,
85 __ATOMIC_RELAXED) - 1;
86 DRV_LOG(DEBUG, "mlx5 list %s entry %p ref: %u.",
87 l_const->name, (void *)entry,
89 } else if (lcore_index < MLX5_LIST_GLOBAL) {
90 ret = __atomic_load_n(&entry->ref_cnt,
93 if (likely(ret != 0 || lcore_index == MLX5_LIST_GLOBAL))
95 if (reuse && ret == 0)
96 entry->ref_cnt--; /* Invalid entry. */
98 entry = LIST_NEXT(entry, next);
103 static inline struct mlx5_list_entry *
104 _mlx5_list_lookup(struct mlx5_list_inconst *l_inconst,
105 struct mlx5_list_const *l_const, void *ctx)
107 struct mlx5_list_entry *entry = NULL;
110 rte_rwlock_read_lock(&l_inconst->lock);
111 for (i = 0; i < MLX5_LIST_GLOBAL; i++) {
112 if (!l_inconst->cache[i])
114 entry = __list_lookup(l_inconst, l_const, i,
119 rte_rwlock_read_unlock(&l_inconst->lock);
123 struct mlx5_list_entry *
124 mlx5_list_lookup(struct mlx5_list *list, void *ctx)
126 return _mlx5_list_lookup(&list->l_inconst, &list->l_const, ctx);
130 static struct mlx5_list_entry *
131 mlx5_list_cache_insert(struct mlx5_list_inconst *l_inconst,
132 struct mlx5_list_const *l_const, int lcore_index,
133 struct mlx5_list_entry *gentry, void *ctx)
135 struct mlx5_list_entry *lentry =
136 l_const->cb_clone(l_const->ctx, gentry, ctx);
138 if (unlikely(!lentry))
140 lentry->ref_cnt = 1u;
141 lentry->gentry = gentry;
142 lentry->lcore_idx = (uint32_t)lcore_index;
143 LIST_INSERT_HEAD(&l_inconst->cache[lcore_index]->h, lentry, next);
148 __list_cache_clean(struct mlx5_list_inconst *l_inconst,
149 struct mlx5_list_const *l_const,
152 struct mlx5_list_cache *c = l_inconst->cache[lcore_index];
153 struct mlx5_list_entry *entry = LIST_FIRST(&c->h);
154 uint32_t inv_cnt = __atomic_exchange_n(&c->inv_cnt, 0,
157 while (inv_cnt != 0 && entry != NULL) {
158 struct mlx5_list_entry *nentry = LIST_NEXT(entry, next);
160 if (__atomic_load_n(&entry->ref_cnt, __ATOMIC_RELAXED) == 0) {
161 LIST_REMOVE(entry, next);
162 if (l_const->lcores_share)
163 l_const->cb_clone_free(l_const->ctx, entry);
165 l_const->cb_remove(l_const->ctx, entry);
172 static inline struct mlx5_list_entry *
173 _mlx5_list_register(struct mlx5_list_inconst *l_inconst,
174 struct mlx5_list_const *l_const,
175 void *ctx, int lcore_index)
177 struct mlx5_list_entry *entry = NULL, *local_entry;
178 volatile uint32_t prev_gen_cnt = 0;
179 MLX5_ASSERT(l_inconst);
180 if (unlikely(!l_inconst->cache[lcore_index])) {
181 l_inconst->cache[lcore_index] = mlx5_malloc(0,
182 sizeof(struct mlx5_list_cache),
183 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
184 if (!l_inconst->cache[lcore_index]) {
188 l_inconst->cache[lcore_index]->inv_cnt = 0;
189 LIST_INIT(&l_inconst->cache[lcore_index]->h);
191 /* 0. Free entries that was invalidated by other lcores. */
192 __list_cache_clean(l_inconst, l_const, lcore_index);
193 /* 1. Lookup in local cache. */
194 local_entry = __list_lookup(l_inconst, l_const, lcore_index, ctx, true);
197 if (l_const->lcores_share) {
198 /* 2. Lookup with read lock on global list, reuse if found. */
199 rte_rwlock_read_lock(&l_inconst->lock);
200 entry = __list_lookup(l_inconst, l_const, MLX5_LIST_GLOBAL,
203 rte_rwlock_read_unlock(&l_inconst->lock);
204 return mlx5_list_cache_insert(l_inconst, l_const,
208 prev_gen_cnt = l_inconst->gen_cnt;
209 rte_rwlock_read_unlock(&l_inconst->lock);
211 /* 3. Prepare new entry for global list and for cache. */
212 entry = l_const->cb_create(l_const->ctx, ctx);
213 if (unlikely(!entry))
216 if (!l_const->lcores_share) {
217 entry->lcore_idx = (uint32_t)lcore_index;
218 LIST_INSERT_HEAD(&l_inconst->cache[lcore_index]->h,
220 __atomic_add_fetch(&l_inconst->count, 1, __ATOMIC_RELAXED);
221 DRV_LOG(DEBUG, "MLX5 list %s c%d entry %p new: %u.",
222 l_const->name, lcore_index,
223 (void *)entry, entry->ref_cnt);
226 local_entry = l_const->cb_clone(l_const->ctx, entry, ctx);
227 if (unlikely(!local_entry)) {
228 l_const->cb_remove(l_const->ctx, entry);
231 local_entry->ref_cnt = 1u;
232 local_entry->gentry = entry;
233 local_entry->lcore_idx = (uint32_t)lcore_index;
234 rte_rwlock_write_lock(&l_inconst->lock);
235 /* 4. Make sure the same entry was not created before the write lock. */
236 if (unlikely(prev_gen_cnt != l_inconst->gen_cnt)) {
237 struct mlx5_list_entry *oentry = __list_lookup(l_inconst,
242 if (unlikely(oentry)) {
243 /* 4.5. Found real race!!, reuse the old entry. */
244 rte_rwlock_write_unlock(&l_inconst->lock);
245 l_const->cb_remove(l_const->ctx, entry);
246 l_const->cb_clone_free(l_const->ctx, local_entry);
247 return mlx5_list_cache_insert(l_inconst, l_const,
252 /* 5. Update lists. */
253 LIST_INSERT_HEAD(&l_inconst->cache[MLX5_LIST_GLOBAL]->h, entry, next);
254 l_inconst->gen_cnt++;
255 rte_rwlock_write_unlock(&l_inconst->lock);
256 LIST_INSERT_HEAD(&l_inconst->cache[lcore_index]->h, local_entry, next);
257 __atomic_add_fetch(&l_inconst->count, 1, __ATOMIC_RELAXED);
258 DRV_LOG(DEBUG, "mlx5 list %s entry %p new: %u.", l_const->name,
259 (void *)entry, entry->ref_cnt);
263 struct mlx5_list_entry *
264 mlx5_list_register(struct mlx5_list *list, void *ctx)
266 struct mlx5_list_entry *entry;
267 int lcore_index = rte_lcore_index(rte_lcore_id());
269 if (unlikely(lcore_index == -1)) {
270 lcore_index = MLX5_LIST_NLCORE;
271 rte_spinlock_lock(&list->l_const.lcore_lock);
273 entry = _mlx5_list_register(&list->l_inconst, &list->l_const, ctx,
275 if (unlikely(lcore_index == MLX5_LIST_NLCORE))
276 rte_spinlock_unlock(&list->l_const.lcore_lock);
281 _mlx5_list_unregister(struct mlx5_list_inconst *l_inconst,
282 struct mlx5_list_const *l_const,
283 struct mlx5_list_entry *entry,
286 struct mlx5_list_entry *gentry = entry->gentry;
288 if (__atomic_sub_fetch(&entry->ref_cnt, 1, __ATOMIC_RELAXED) != 0)
290 if (entry->lcore_idx == (uint32_t)lcore_idx) {
291 LIST_REMOVE(entry, next);
292 if (l_const->lcores_share)
293 l_const->cb_clone_free(l_const->ctx, entry);
295 l_const->cb_remove(l_const->ctx, entry);
296 } else if (likely(lcore_idx != -1)) {
297 __atomic_add_fetch(&l_inconst->cache[entry->lcore_idx]->inv_cnt,
298 1, __ATOMIC_RELAXED);
302 if (!l_const->lcores_share) {
303 __atomic_sub_fetch(&l_inconst->count, 1, __ATOMIC_RELAXED);
304 DRV_LOG(DEBUG, "mlx5 list %s entry %p removed.",
305 l_const->name, (void *)entry);
308 if (__atomic_sub_fetch(&gentry->ref_cnt, 1, __ATOMIC_RELAXED) != 0)
310 rte_rwlock_write_lock(&l_inconst->lock);
311 if (likely(gentry->ref_cnt == 0)) {
312 LIST_REMOVE(gentry, next);
313 rte_rwlock_write_unlock(&l_inconst->lock);
314 l_const->cb_remove(l_const->ctx, gentry);
315 __atomic_sub_fetch(&l_inconst->count, 1, __ATOMIC_RELAXED);
316 DRV_LOG(DEBUG, "mlx5 list %s entry %p removed.",
317 l_const->name, (void *)gentry);
320 rte_rwlock_write_unlock(&l_inconst->lock);
325 mlx5_list_unregister(struct mlx5_list *list,
326 struct mlx5_list_entry *entry)
329 int lcore_index = rte_lcore_index(rte_lcore_id());
331 if (unlikely(lcore_index == -1)) {
332 lcore_index = MLX5_LIST_NLCORE;
333 rte_spinlock_lock(&list->l_const.lcore_lock);
335 ret = _mlx5_list_unregister(&list->l_inconst, &list->l_const, entry,
337 if (unlikely(lcore_index == MLX5_LIST_NLCORE))
338 rte_spinlock_unlock(&list->l_const.lcore_lock);
344 mlx5_list_uninit(struct mlx5_list_inconst *l_inconst,
345 struct mlx5_list_const *l_const)
347 struct mlx5_list_entry *entry;
350 MLX5_ASSERT(l_inconst);
351 for (i = 0; i < MLX5_LIST_MAX; i++) {
352 if (!l_inconst->cache[i])
354 while (!LIST_EMPTY(&l_inconst->cache[i]->h)) {
355 entry = LIST_FIRST(&l_inconst->cache[i]->h);
356 LIST_REMOVE(entry, next);
357 if (i == MLX5_LIST_GLOBAL) {
358 l_const->cb_remove(l_const->ctx, entry);
359 DRV_LOG(DEBUG, "mlx5 list %s entry %p "
360 "destroyed.", l_const->name,
363 l_const->cb_clone_free(l_const->ctx, entry);
366 if (i != MLX5_LIST_GLOBAL)
367 mlx5_free(l_inconst->cache[i]);
372 mlx5_list_destroy(struct mlx5_list *list)
374 mlx5_list_uninit(&list->l_inconst, &list->l_const);
379 mlx5_list_get_entry_num(struct mlx5_list *list)
382 return __atomic_load_n(&list->l_inconst.count, __ATOMIC_RELAXED);
385 /********************* Hash List **********************/
388 mlx5_hlist_create(const char *name, uint32_t size, bool direct_key,
389 bool lcores_share, void *ctx, mlx5_list_create_cb cb_create,
390 mlx5_list_match_cb cb_match,
391 mlx5_list_remove_cb cb_remove,
392 mlx5_list_clone_cb cb_clone,
393 mlx5_list_clone_free_cb cb_clone_free)
395 struct mlx5_hlist *h;
396 struct mlx5_list_cache *gc;
401 if (!cb_match || !cb_create || !cb_remove || !cb_clone ||
406 /* Align to the next power of 2, 32bits integer is enough now. */
407 if (!rte_is_power_of_2(size)) {
408 act_size = rte_align32pow2(size);
409 DRV_LOG(WARNING, "Size 0x%" PRIX32 " is not power of 2, will "
410 "be aligned to 0x%" PRIX32 ".", size, act_size);
414 alloc_size = sizeof(struct mlx5_hlist) +
415 sizeof(struct mlx5_hlist_bucket) * act_size;
417 alloc_size += sizeof(struct mlx5_list_cache) * act_size;
418 /* Using zmalloc, then no need to initialize the heads. */
419 h = mlx5_malloc(MLX5_MEM_ZERO, alloc_size, RTE_CACHE_LINE_SIZE,
422 DRV_LOG(ERR, "No memory for hash list %s creation",
423 name ? name : "None");
427 snprintf(h->l_const.name, sizeof(h->l_const.name), "%s", name);
428 h->l_const.ctx = ctx;
429 h->l_const.lcores_share = lcores_share;
430 h->l_const.cb_create = cb_create;
431 h->l_const.cb_match = cb_match;
432 h->l_const.cb_remove = cb_remove;
433 h->l_const.cb_clone = cb_clone;
434 h->l_const.cb_clone_free = cb_clone_free;
435 rte_spinlock_init(&h->l_const.lcore_lock);
436 h->mask = act_size - 1;
437 h->direct_key = direct_key;
438 gc = (struct mlx5_list_cache *)&h->buckets[act_size];
439 for (i = 0; i < act_size; i++) {
440 if (mlx5_list_init(&h->buckets[i].l, &h->l_const,
441 lcores_share ? &gc[i] : NULL) != 0) {
446 DRV_LOG(DEBUG, "Hash list %s with size 0x%" PRIX32 " was created.",
452 struct mlx5_list_entry *
453 mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx)
458 idx = (uint32_t)(key & h->mask);
460 idx = rte_hash_crc_8byte(key, 0) & h->mask;
461 return _mlx5_list_lookup(&h->buckets[idx].l, &h->l_const, ctx);
464 struct mlx5_list_entry*
465 mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key, void *ctx)
468 struct mlx5_list_entry *entry;
469 int lcore_index = rte_lcore_index(rte_lcore_id());
472 idx = (uint32_t)(key & h->mask);
474 idx = rte_hash_crc_8byte(key, 0) & h->mask;
475 if (unlikely(lcore_index == -1)) {
476 lcore_index = MLX5_LIST_NLCORE;
477 rte_spinlock_lock(&h->l_const.lcore_lock);
479 entry = _mlx5_list_register(&h->buckets[idx].l, &h->l_const, ctx,
482 if (h->l_const.lcores_share)
483 entry->gentry->bucket_idx = idx;
485 entry->bucket_idx = idx;
487 if (unlikely(lcore_index == MLX5_LIST_NLCORE))
488 rte_spinlock_unlock(&h->l_const.lcore_lock);
493 mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_list_entry *entry)
495 int lcore_index = rte_lcore_index(rte_lcore_id());
497 uint32_t idx = h->l_const.lcores_share ? entry->gentry->bucket_idx :
499 if (unlikely(lcore_index == -1)) {
500 lcore_index = MLX5_LIST_NLCORE;
501 rte_spinlock_lock(&h->l_const.lcore_lock);
503 ret = _mlx5_list_unregister(&h->buckets[idx].l, &h->l_const, entry,
505 if (unlikely(lcore_index == MLX5_LIST_NLCORE))
506 rte_spinlock_unlock(&h->l_const.lcore_lock);
511 mlx5_hlist_destroy(struct mlx5_hlist *h)
515 for (i = 0; i <= h->mask; i++)
516 mlx5_list_uninit(&h->buckets[i].l, &h->l_const);