1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
7 #include <mlx5_malloc.h>
9 #include "mlx5_utils.h"
12 /********************* Cache list ************************/
14 static struct mlx5_cache_entry *
15 mlx5_clist_default_create_cb(struct mlx5_cache_list *list,
16 struct mlx5_cache_entry *entry __rte_unused,
17 void *ctx __rte_unused)
19 return mlx5_malloc(MLX5_MEM_ZERO, list->entry_sz, 0, SOCKET_ID_ANY);
23 mlx5_clist_default_remove_cb(struct mlx5_cache_list *list __rte_unused,
24 struct mlx5_cache_entry *entry)
30 mlx5_cache_list_init(struct mlx5_cache_list *list, const char *name,
31 uint32_t entry_size, void *ctx,
32 mlx5_cache_create_cb cb_create,
33 mlx5_cache_match_cb cb_match,
34 mlx5_cache_remove_cb cb_remove)
37 if (!cb_match || (!cb_create ^ !cb_remove))
40 snprintf(list->name, sizeof(list->name), "%s", name);
41 list->entry_sz = entry_size;
43 list->cb_create = cb_create ? cb_create : mlx5_clist_default_create_cb;
44 list->cb_match = cb_match;
45 list->cb_remove = cb_remove ? cb_remove : mlx5_clist_default_remove_cb;
46 rte_rwlock_init(&list->lock);
47 DRV_LOG(DEBUG, "Cache list %s initialized.", list->name);
48 LIST_INIT(&list->head);
52 static struct mlx5_cache_entry *
53 __cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
55 struct mlx5_cache_entry *entry;
57 LIST_FOREACH(entry, &list->head, next) {
58 if (list->cb_match(list, entry, ctx))
61 __atomic_add_fetch(&entry->ref_cnt, 1,
63 DRV_LOG(DEBUG, "Cache list %s entry %p ref++: %u.",
64 list->name, (void *)entry, entry->ref_cnt);
71 static struct mlx5_cache_entry *
72 cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
74 struct mlx5_cache_entry *entry;
76 rte_rwlock_read_lock(&list->lock);
77 entry = __cache_lookup(list, ctx, reuse);
78 rte_rwlock_read_unlock(&list->lock);
82 struct mlx5_cache_entry *
83 mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
85 return cache_lookup(list, ctx, false);
88 struct mlx5_cache_entry *
89 mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
91 struct mlx5_cache_entry *entry;
92 uint32_t prev_gen_cnt = 0;
95 prev_gen_cnt = __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE);
96 /* Lookup with read lock, reuse if found. */
97 entry = cache_lookup(list, ctx, true);
100 /* Not found, append with write lock - block read from other threads. */
101 rte_rwlock_write_lock(&list->lock);
102 /* If list changed by other threads before lock, search again. */
103 if (prev_gen_cnt != __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE)) {
104 /* Lookup and reuse w/o read lock. */
105 entry = __cache_lookup(list, ctx, true);
109 entry = list->cb_create(list, entry, ctx);
111 DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
112 list->name, (void *)entry);
116 LIST_INSERT_HEAD(&list->head, entry, next);
117 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_RELEASE);
118 __atomic_add_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
119 DRV_LOG(DEBUG, "Cache list %s entry %p new: %u.",
120 list->name, (void *)entry, entry->ref_cnt);
122 rte_rwlock_write_unlock(&list->lock);
127 mlx5_cache_unregister(struct mlx5_cache_list *list,
128 struct mlx5_cache_entry *entry)
130 rte_rwlock_write_lock(&list->lock);
131 MLX5_ASSERT(entry && entry->next.le_prev);
132 DRV_LOG(DEBUG, "Cache list %s entry %p ref--: %u.",
133 list->name, (void *)entry, entry->ref_cnt);
134 if (--entry->ref_cnt) {
135 rte_rwlock_write_unlock(&list->lock);
138 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_ACQUIRE);
139 __atomic_sub_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
140 LIST_REMOVE(entry, next);
141 list->cb_remove(list, entry);
142 rte_rwlock_write_unlock(&list->lock);
143 DRV_LOG(DEBUG, "Cache list %s entry %p removed.",
144 list->name, (void *)entry);
149 mlx5_cache_list_destroy(struct mlx5_cache_list *list)
151 struct mlx5_cache_entry *entry;
154 /* no LIST_FOREACH_SAFE, using while instead */
155 while (!LIST_EMPTY(&list->head)) {
156 entry = LIST_FIRST(&list->head);
157 LIST_REMOVE(entry, next);
158 list->cb_remove(list, entry);
159 DRV_LOG(DEBUG, "Cache list %s entry %p destroyed.",
160 list->name, (void *)entry);
162 memset(list, 0, sizeof(*list));
166 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list)
169 return __atomic_load_n(&list->count, __ATOMIC_RELAXED);
172 /********************* Indexed pool **********************/
175 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
177 if (pool->cfg.need_lock)
178 rte_spinlock_lock(&pool->rsz_lock);
182 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
184 if (pool->cfg.need_lock)
185 rte_spinlock_unlock(&pool->rsz_lock);
188 static inline uint32_t
189 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
191 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
192 uint32_t trunk_idx = 0;
195 if (!cfg->grow_trunk)
196 return entry_idx / cfg->trunk_size;
197 if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
198 trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
199 (cfg->trunk_size << (cfg->grow_shift *
200 cfg->grow_trunk)) + cfg->grow_trunk;
202 for (i = 0; i < cfg->grow_trunk; i++) {
203 if (entry_idx < pool->grow_tbl[i])
211 static inline uint32_t
212 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
214 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
216 return cfg->trunk_size << (cfg->grow_shift *
217 (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
220 static inline uint32_t
221 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
223 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
228 if (!cfg->grow_trunk)
229 return cfg->trunk_size * trunk_idx;
230 if (trunk_idx < cfg->grow_trunk)
231 offset = pool->grow_tbl[trunk_idx - 1];
233 offset = pool->grow_tbl[cfg->grow_trunk - 1] +
234 (cfg->trunk_size << (cfg->grow_shift *
235 cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
239 struct mlx5_indexed_pool *
240 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
242 struct mlx5_indexed_pool *pool;
245 if (!cfg || (!cfg->malloc ^ !cfg->free) ||
246 (cfg->per_core_cache && cfg->release_mem_en) ||
247 (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
248 ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
250 pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
251 sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
256 if (!pool->cfg.trunk_size)
257 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
258 if (!cfg->malloc && !cfg->free) {
259 pool->cfg.malloc = mlx5_malloc;
260 pool->cfg.free = mlx5_free;
262 if (pool->cfg.need_lock)
263 rte_spinlock_init(&pool->rsz_lock);
265 * Initialize the dynamic grow trunk size lookup table to have a quick
266 * lookup for the trunk entry index offset.
268 for (i = 0; i < cfg->grow_trunk; i++) {
269 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
271 pool->grow_tbl[i] += pool->grow_tbl[i - 1];
273 if (!pool->cfg.max_idx)
275 mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1);
276 if (!cfg->per_core_cache)
277 pool->free_list = TRUNK_INVALID;
282 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
284 struct mlx5_indexed_trunk *trunk;
285 struct mlx5_indexed_trunk **trunk_tmp;
286 struct mlx5_indexed_trunk **p;
287 size_t trunk_size = 0;
290 uint32_t idx, cur_max_idx, i;
292 cur_max_idx = mlx5_trunk_idx_offset_get(pool, pool->n_trunk_valid);
293 if (pool->n_trunk_valid == TRUNK_MAX_IDX ||
294 cur_max_idx >= pool->cfg.max_idx)
296 if (pool->n_trunk_valid == pool->n_trunk) {
297 /* No free trunk flags, expand trunk list. */
298 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
299 RTE_CACHE_LINE_SIZE / sizeof(void *);
301 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
302 sizeof(struct mlx5_indexed_trunk *),
303 RTE_CACHE_LINE_SIZE, rte_socket_id());
307 memcpy(p, pool->trunks, pool->n_trunk_valid *
308 sizeof(struct mlx5_indexed_trunk *));
309 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
310 n_grow * sizeof(void *));
311 trunk_tmp = pool->trunks;
314 pool->cfg.free(trunk_tmp);
315 pool->n_trunk += n_grow;
317 if (!pool->cfg.release_mem_en) {
318 idx = pool->n_trunk_valid;
320 /* Find the first available slot in trunk list */
321 for (idx = 0; idx < pool->n_trunk; idx++)
322 if (pool->trunks[idx] == NULL)
325 trunk_size += sizeof(*trunk);
326 data_size = mlx5_trunk_size_get(pool, idx);
327 bmp_size = rte_bitmap_get_memory_footprint(data_size);
328 /* rte_bitmap requires memory cacheline aligned. */
329 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
330 trunk_size += bmp_size;
331 trunk = pool->cfg.malloc(0, trunk_size,
332 RTE_CACHE_LINE_SIZE, rte_socket_id());
335 pool->trunks[idx] = trunk;
337 trunk->free = data_size;
338 trunk->prev = TRUNK_INVALID;
339 trunk->next = TRUNK_INVALID;
340 MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
341 pool->free_list = idx;
342 /* Mark all entries as available. */
343 trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
344 [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
346 /* Clear the overhead bits in the trunk if it happens. */
347 if (cur_max_idx + data_size > pool->cfg.max_idx) {
348 for (i = pool->cfg.max_idx - cur_max_idx; i < data_size; i++)
349 rte_bitmap_clear(trunk->bmp, i);
351 MLX5_ASSERT(trunk->bmp);
352 pool->n_trunk_valid++;
360 static inline struct mlx5_indexed_cache *
361 mlx5_ipool_update_global_cache(struct mlx5_indexed_pool *pool, int cidx)
363 struct mlx5_indexed_cache *gc, *lc, *olc = NULL;
365 lc = pool->cache[cidx]->lc;
366 gc = __atomic_load_n(&pool->gc, __ATOMIC_RELAXED);
367 if (gc && lc != gc) {
368 mlx5_ipool_lock(pool);
369 if (lc && !(--lc->ref_cnt))
373 pool->cache[cidx]->lc = lc;
374 mlx5_ipool_unlock(pool);
382 mlx5_ipool_allocate_from_global(struct mlx5_indexed_pool *pool, int cidx)
384 struct mlx5_indexed_trunk *trunk;
385 struct mlx5_indexed_cache *p, *lc, *olc = NULL;
386 size_t trunk_size = 0;
388 uint32_t cur_max_idx, trunk_idx, trunk_n;
389 uint32_t fetch_size, ts_idx, i;
396 * Fetch new index from global if possible. First round local
397 * cache will be NULL.
399 lc = pool->cache[cidx]->lc;
400 mlx5_ipool_lock(pool);
401 /* Try to update local cache first. */
402 if (likely(pool->gc)) {
403 if (lc != pool->gc) {
404 if (lc && !(--lc->ref_cnt))
408 pool->cache[cidx]->lc = lc;
411 /* Use the updated local cache to fetch index. */
412 fetch_size = pool->cfg.per_core_cache >> 2;
413 if (lc->len < fetch_size)
414 fetch_size = lc->len;
415 lc->len -= fetch_size;
416 memcpy(pool->cache[cidx]->idx, &lc->idx[lc->len],
417 sizeof(uint32_t) * fetch_size);
420 mlx5_ipool_unlock(pool);
426 pool->cache[cidx]->len = fetch_size - 1;
427 return pool->cache[cidx]->idx[pool->cache[cidx]->len];
429 trunk_idx = lc ? __atomic_load_n(&lc->n_trunk_valid,
430 __ATOMIC_ACQUIRE) : 0;
431 trunk_n = lc ? lc->n_trunk : 0;
432 cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx);
433 /* Check if index reach maximum. */
434 if (trunk_idx == TRUNK_MAX_IDX ||
435 cur_max_idx >= pool->cfg.max_idx)
437 /* No enough space in trunk array, resize the trunks array. */
438 if (trunk_idx == trunk_n) {
439 n_grow = trunk_idx ? trunk_idx :
440 RTE_CACHE_LINE_SIZE / sizeof(void *);
441 cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_n + n_grow);
442 /* Resize the trunk array. */
443 p = pool->cfg.malloc(0, ((trunk_idx + n_grow) *
444 sizeof(struct mlx5_indexed_trunk *)) +
445 (cur_max_idx * sizeof(uint32_t)) + sizeof(*p),
446 RTE_CACHE_LINE_SIZE, rte_socket_id());
449 p->trunks = (struct mlx5_indexed_trunk **)&p->idx[cur_max_idx];
451 memcpy(p->trunks, lc->trunks, trunk_idx *
452 sizeof(struct mlx5_indexed_trunk *));
453 #ifdef RTE_LIBRTE_MLX5_DEBUG
454 memset(RTE_PTR_ADD(p->trunks, trunk_idx * sizeof(void *)), 0,
455 n_grow * sizeof(void *));
457 p->n_trunk_valid = trunk_idx;
458 p->n_trunk = trunk_n + n_grow;
461 /* Prepare the new trunk. */
462 trunk_size = sizeof(*trunk);
463 data_size = mlx5_trunk_size_get(pool, trunk_idx);
464 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
465 trunk = pool->cfg.malloc(0, trunk_size,
466 RTE_CACHE_LINE_SIZE, rte_socket_id());
467 if (unlikely(!trunk)) {
471 trunk->idx = trunk_idx;
472 trunk->free = data_size;
473 mlx5_ipool_lock(pool);
475 * Double check if trunks has been updated or have available index.
476 * During the new trunk allocate, index may still be flushed to the
477 * global cache. So also need to check the pool->gc->len.
479 if (pool->gc && (lc != pool->gc ||
480 lc->n_trunk_valid != trunk_idx ||
482 mlx5_ipool_unlock(pool);
485 pool->cfg.free(trunk);
488 /* Resize the trunk array and update local cache first. */
490 if (lc && !(--lc->ref_cnt))
494 pool->cache[cidx]->lc = lc;
495 __atomic_store_n(&pool->gc, p, __ATOMIC_RELAXED);
497 /* Add trunk to trunks array. */
498 lc->trunks[trunk_idx] = trunk;
499 __atomic_fetch_add(&lc->n_trunk_valid, 1, __ATOMIC_RELAXED);
500 /* Enqueue half of the index to global. */
501 ts_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx) + 1;
502 fetch_size = trunk->free >> 1;
503 for (i = 0; i < fetch_size; i++)
504 lc->idx[i] = ts_idx + i;
505 lc->len = fetch_size;
506 mlx5_ipool_unlock(pool);
507 /* Copy left half - 1 to local cache index array. */
508 pool->cache[cidx]->len = trunk->free - fetch_size - 1;
509 ts_idx += fetch_size;
510 for (i = 0; i < pool->cache[cidx]->len; i++)
511 pool->cache[cidx]->idx[i] = ts_idx + i;
518 mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
520 struct mlx5_indexed_trunk *trunk;
521 struct mlx5_indexed_cache *lc;
527 cidx = rte_lcore_index(rte_lcore_id());
528 if (unlikely(cidx == -1)) {
532 lc = mlx5_ipool_update_global_cache(pool, cidx);
534 trunk_idx = mlx5_trunk_idx_get(pool, idx);
535 trunk = lc->trunks[trunk_idx];
537 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk_idx);
538 return &trunk->data[entry_idx * pool->cfg.size];
542 mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, uint32_t *idx)
546 cidx = rte_lcore_index(rte_lcore_id());
547 if (unlikely(cidx == -1)) {
551 if (unlikely(!pool->cache[cidx])) {
552 pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
553 sizeof(struct mlx5_ipool_per_lcore) +
554 (pool->cfg.per_core_cache * sizeof(uint32_t)),
555 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
556 if (!pool->cache[cidx]) {
557 DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
560 } else if (pool->cache[cidx]->len) {
561 pool->cache[cidx]->len--;
562 *idx = pool->cache[cidx]->idx[pool->cache[cidx]->len];
563 return mlx5_ipool_get_cache(pool, *idx);
565 /* Not enough idx in global cache. Keep fetching from global. */
566 *idx = mlx5_ipool_allocate_from_global(pool, cidx);
567 if (unlikely(!(*idx)))
569 return mlx5_ipool_get_cache(pool, *idx);
573 mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
576 struct mlx5_ipool_per_lcore *ilc;
577 struct mlx5_indexed_cache *gc, *olc = NULL;
578 uint32_t reclaim_num = 0;
581 cidx = rte_lcore_index(rte_lcore_id());
582 if (unlikely(cidx == -1)) {
587 * When index was allocated on core A but freed on core B. In this
588 * case check if local cache on core B was allocated before.
590 if (unlikely(!pool->cache[cidx])) {
591 pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
592 sizeof(struct mlx5_ipool_per_lcore) +
593 (pool->cfg.per_core_cache * sizeof(uint32_t)),
594 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
595 if (!pool->cache[cidx]) {
596 DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
600 /* Try to enqueue to local index cache. */
601 if (pool->cache[cidx]->len < pool->cfg.per_core_cache) {
602 pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
603 pool->cache[cidx]->len++;
606 ilc = pool->cache[cidx];
607 reclaim_num = pool->cfg.per_core_cache >> 2;
608 ilc->len -= reclaim_num;
609 /* Local index cache full, try with global index cache. */
610 mlx5_ipool_lock(pool);
613 if (!(--ilc->lc->ref_cnt))
618 memcpy(&gc->idx[gc->len], &ilc->idx[ilc->len],
619 reclaim_num * sizeof(uint32_t));
620 gc->len += reclaim_num;
621 mlx5_ipool_unlock(pool);
624 pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
625 pool->cache[cidx]->len++;
629 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
631 struct mlx5_indexed_trunk *trunk;
636 if (pool->cfg.per_core_cache)
637 return mlx5_ipool_malloc_cache(pool, idx);
638 mlx5_ipool_lock(pool);
639 if (pool->free_list == TRUNK_INVALID) {
640 /* If no available trunks, grow new. */
641 if (mlx5_ipool_grow(pool)) {
642 mlx5_ipool_unlock(pool);
646 MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
647 trunk = pool->trunks[pool->free_list];
648 MLX5_ASSERT(trunk->free);
649 if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
650 mlx5_ipool_unlock(pool);
654 iidx += __builtin_ctzll(slab);
655 MLX5_ASSERT(iidx != UINT32_MAX);
656 MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
657 rte_bitmap_clear(trunk->bmp, iidx);
658 p = &trunk->data[iidx * pool->cfg.size];
660 * The ipool index should grow continually from small to big,
661 * some features as metering only accept limited bits of index.
662 * Random index with MSB set may be rejected.
664 iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
665 iidx += 1; /* non-zero index. */
671 /* Full trunk will be removed from free list in imalloc. */
672 MLX5_ASSERT(pool->free_list == trunk->idx);
673 pool->free_list = trunk->next;
674 if (trunk->next != TRUNK_INVALID)
675 pool->trunks[trunk->next]->prev = TRUNK_INVALID;
676 trunk->prev = TRUNK_INVALID;
677 trunk->next = TRUNK_INVALID;
684 mlx5_ipool_unlock(pool);
689 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
691 void *entry = mlx5_ipool_malloc(pool, idx);
693 if (entry && pool->cfg.size)
694 memset(entry, 0, pool->cfg.size);
699 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
701 struct mlx5_indexed_trunk *trunk;
707 if (pool->cfg.per_core_cache) {
708 mlx5_ipool_free_cache(pool, idx);
712 mlx5_ipool_lock(pool);
713 trunk_idx = mlx5_trunk_idx_get(pool, idx);
714 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
715 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
717 trunk = pool->trunks[trunk_idx];
720 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
721 if (trunk_idx != trunk->idx ||
722 rte_bitmap_get(trunk->bmp, entry_idx))
724 rte_bitmap_set(trunk->bmp, entry_idx);
726 if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
727 (pool, trunk->idx)) {
728 if (pool->free_list == trunk->idx)
729 pool->free_list = trunk->next;
730 if (trunk->next != TRUNK_INVALID)
731 pool->trunks[trunk->next]->prev = trunk->prev;
732 if (trunk->prev != TRUNK_INVALID)
733 pool->trunks[trunk->prev]->next = trunk->next;
734 pool->cfg.free(trunk);
735 pool->trunks[trunk_idx] = NULL;
736 pool->n_trunk_valid--;
741 if (pool->n_trunk_valid == 0) {
742 pool->cfg.free(pool->trunks);
746 } else if (trunk->free == 1) {
747 /* Put into free trunk list head. */
748 MLX5_ASSERT(pool->free_list != trunk->idx);
749 trunk->next = pool->free_list;
750 trunk->prev = TRUNK_INVALID;
751 if (pool->free_list != TRUNK_INVALID)
752 pool->trunks[pool->free_list]->prev = trunk->idx;
753 pool->free_list = trunk->idx;
763 mlx5_ipool_unlock(pool);
767 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
769 struct mlx5_indexed_trunk *trunk;
776 if (pool->cfg.per_core_cache)
777 return mlx5_ipool_get_cache(pool, idx);
779 mlx5_ipool_lock(pool);
780 trunk_idx = mlx5_trunk_idx_get(pool, idx);
781 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
782 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
784 trunk = pool->trunks[trunk_idx];
787 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
788 if (trunk_idx != trunk->idx ||
789 rte_bitmap_get(trunk->bmp, entry_idx))
791 p = &trunk->data[entry_idx * pool->cfg.size];
793 mlx5_ipool_unlock(pool);
798 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
800 struct mlx5_indexed_trunk **trunks = NULL;
801 struct mlx5_indexed_cache *gc = pool->gc;
802 uint32_t i, n_trunk_valid = 0;
805 mlx5_ipool_lock(pool);
806 if (pool->cfg.per_core_cache) {
807 for (i = 0; i < RTE_MAX_LCORE; i++) {
809 * Free only old global cache. Pool gc will be
812 if (pool->cache[i]) {
813 if (pool->cache[i]->lc &&
814 pool->cache[i]->lc != pool->gc &&
815 (!(--pool->cache[i]->lc->ref_cnt)))
816 pool->cfg.free(pool->cache[i]->lc);
817 pool->cfg.free(pool->cache[i]);
822 n_trunk_valid = gc->n_trunk_valid;
826 trunks = pool->trunks;
827 n_trunk_valid = pool->n_trunk_valid;
829 for (i = 0; i < n_trunk_valid; i++) {
831 pool->cfg.free(trunks[i]);
834 pool->cfg.free(trunks);
837 mlx5_ipool_unlock(pool);
843 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
845 printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
847 pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
848 pool->cfg.trunk_size, pool->n_trunk_valid);
850 printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
851 "available %u free %u\n",
852 pool->cfg.type, pool->n_entry, pool->trunk_new,
853 pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
857 struct mlx5_l3t_tbl *
858 mlx5_l3t_create(enum mlx5_l3t_type type)
860 struct mlx5_l3t_tbl *tbl;
861 struct mlx5_indexed_pool_config l3t_ip_cfg = {
867 .malloc = mlx5_malloc,
871 if (type >= MLX5_L3T_TYPE_MAX) {
875 tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
883 case MLX5_L3T_TYPE_WORD:
884 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
885 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
887 case MLX5_L3T_TYPE_DWORD:
888 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
889 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
891 case MLX5_L3T_TYPE_QWORD:
892 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
893 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
896 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
897 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
900 rte_spinlock_init(&tbl->sl);
901 tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
911 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
913 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
920 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
921 m_tbl = g_tbl->tbl[i];
924 for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
927 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
928 m_tbl->tbl[j])->ref_cnt);
929 mlx5_ipool_free(tbl->eip,
930 ((struct mlx5_l3t_entry_word *)
931 m_tbl->tbl[j])->idx);
933 if (!(--m_tbl->ref_cnt))
936 MLX5_ASSERT(!m_tbl->ref_cnt);
937 mlx5_free(g_tbl->tbl[i]);
939 if (!(--g_tbl->ref_cnt))
942 MLX5_ASSERT(!g_tbl->ref_cnt);
946 mlx5_ipool_destroy(tbl->eip);
951 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
952 union mlx5_l3t_data *data)
954 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
955 struct mlx5_l3t_entry_word *w_e_tbl;
956 struct mlx5_l3t_entry_dword *dw_e_tbl;
957 struct mlx5_l3t_entry_qword *qw_e_tbl;
958 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
965 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
968 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
971 entry_idx = idx & MLX5_L3T_ET_MASK;
973 case MLX5_L3T_TYPE_WORD:
974 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
975 data->word = w_e_tbl->entry[entry_idx].data;
976 if (w_e_tbl->entry[entry_idx].data)
977 w_e_tbl->entry[entry_idx].ref_cnt++;
979 case MLX5_L3T_TYPE_DWORD:
980 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
981 data->dword = dw_e_tbl->entry[entry_idx].data;
982 if (dw_e_tbl->entry[entry_idx].data)
983 dw_e_tbl->entry[entry_idx].ref_cnt++;
985 case MLX5_L3T_TYPE_QWORD:
986 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
987 data->qword = qw_e_tbl->entry[entry_idx].data;
988 if (qw_e_tbl->entry[entry_idx].data)
989 qw_e_tbl->entry[entry_idx].ref_cnt++;
992 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
993 data->ptr = ptr_e_tbl->entry[entry_idx].data;
994 if (ptr_e_tbl->entry[entry_idx].data)
995 ptr_e_tbl->entry[entry_idx].ref_cnt++;
1002 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1003 union mlx5_l3t_data *data)
1007 rte_spinlock_lock(&tbl->sl);
1008 ret = __l3t_get_entry(tbl, idx, data);
1009 rte_spinlock_unlock(&tbl->sl);
1014 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
1016 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1017 struct mlx5_l3t_entry_word *w_e_tbl;
1018 struct mlx5_l3t_entry_dword *dw_e_tbl;
1019 struct mlx5_l3t_entry_qword *qw_e_tbl;
1020 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1026 rte_spinlock_lock(&tbl->sl);
1030 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1033 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1036 entry_idx = idx & MLX5_L3T_ET_MASK;
1037 switch (tbl->type) {
1038 case MLX5_L3T_TYPE_WORD:
1039 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1040 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
1041 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
1044 w_e_tbl->entry[entry_idx].data = 0;
1045 ref_cnt = --w_e_tbl->ref_cnt;
1047 case MLX5_L3T_TYPE_DWORD:
1048 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1049 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
1050 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
1053 dw_e_tbl->entry[entry_idx].data = 0;
1054 ref_cnt = --dw_e_tbl->ref_cnt;
1056 case MLX5_L3T_TYPE_QWORD:
1057 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1058 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
1059 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
1062 qw_e_tbl->entry[entry_idx].data = 0;
1063 ref_cnt = --qw_e_tbl->ref_cnt;
1066 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1067 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
1068 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
1071 ptr_e_tbl->entry[entry_idx].data = NULL;
1072 ref_cnt = --ptr_e_tbl->ref_cnt;
1076 mlx5_ipool_free(tbl->eip,
1077 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
1078 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1080 if (!(--m_tbl->ref_cnt)) {
1083 [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
1084 if (!(--g_tbl->ref_cnt)) {
1091 rte_spinlock_unlock(&tbl->sl);
1096 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1097 union mlx5_l3t_data *data)
1099 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1100 struct mlx5_l3t_entry_word *w_e_tbl;
1101 struct mlx5_l3t_entry_dword *dw_e_tbl;
1102 struct mlx5_l3t_entry_qword *qw_e_tbl;
1103 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1105 uint32_t entry_idx, tbl_idx = 0;
1107 /* Check the global table, create it if empty. */
1110 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1111 sizeof(struct mlx5_l3t_level_tbl) +
1112 sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1121 * Check the middle table, create it if empty. Ref_cnt will be
1122 * increased if new sub table created.
1124 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1126 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1127 sizeof(struct mlx5_l3t_level_tbl) +
1128 sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1134 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1139 * Check the entry table, create it if empty. Ref_cnt will be
1140 * increased if new sub entry table created.
1142 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1144 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1149 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1150 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1154 entry_idx = idx & MLX5_L3T_ET_MASK;
1155 switch (tbl->type) {
1156 case MLX5_L3T_TYPE_WORD:
1157 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1158 if (w_e_tbl->entry[entry_idx].data) {
1159 data->word = w_e_tbl->entry[entry_idx].data;
1160 w_e_tbl->entry[entry_idx].ref_cnt++;
1164 w_e_tbl->entry[entry_idx].data = data->word;
1165 w_e_tbl->entry[entry_idx].ref_cnt = 1;
1168 case MLX5_L3T_TYPE_DWORD:
1169 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1170 if (dw_e_tbl->entry[entry_idx].data) {
1171 data->dword = dw_e_tbl->entry[entry_idx].data;
1172 dw_e_tbl->entry[entry_idx].ref_cnt++;
1176 dw_e_tbl->entry[entry_idx].data = data->dword;
1177 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1178 dw_e_tbl->ref_cnt++;
1180 case MLX5_L3T_TYPE_QWORD:
1181 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1182 if (qw_e_tbl->entry[entry_idx].data) {
1183 data->qword = qw_e_tbl->entry[entry_idx].data;
1184 qw_e_tbl->entry[entry_idx].ref_cnt++;
1188 qw_e_tbl->entry[entry_idx].data = data->qword;
1189 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1190 qw_e_tbl->ref_cnt++;
1193 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1194 if (ptr_e_tbl->entry[entry_idx].data) {
1195 data->ptr = ptr_e_tbl->entry[entry_idx].data;
1196 ptr_e_tbl->entry[entry_idx].ref_cnt++;
1200 ptr_e_tbl->entry[entry_idx].data = data->ptr;
1201 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1202 ptr_e_tbl->ref_cnt++;
1209 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1210 union mlx5_l3t_data *data)
1214 rte_spinlock_lock(&tbl->sl);
1215 ret = __l3t_set_entry(tbl, idx, data);
1216 rte_spinlock_unlock(&tbl->sl);
1221 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1222 union mlx5_l3t_data *data,
1223 mlx5_l3t_alloc_callback_fn cb, void *ctx)
1227 rte_spinlock_lock(&tbl->sl);
1228 /* Check if entry data is ready. */
1229 ret = __l3t_get_entry(tbl, idx, data);
1231 switch (tbl->type) {
1232 case MLX5_L3T_TYPE_WORD:
1236 case MLX5_L3T_TYPE_DWORD:
1240 case MLX5_L3T_TYPE_QWORD:
1250 /* Entry data is not ready, use user callback to create it. */
1251 ret = cb(ctx, data);
1254 /* Save the new allocated data to entry. */
1255 ret = __l3t_set_entry(tbl, idx, data);
1257 rte_spinlock_unlock(&tbl->sl);