1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
5 #include <rte_malloc.h>
6 #include <rte_hash_crc.h>
8 #include <mlx5_malloc.h>
10 #include "mlx5_utils.h"
12 /********************* Hash List **********************/
14 static struct mlx5_hlist_entry *
15 mlx5_hlist_default_create_cb(struct mlx5_hlist *h, uint64_t key __rte_unused,
16 void *ctx __rte_unused)
18 return mlx5_malloc(MLX5_MEM_ZERO, h->entry_sz, 0, SOCKET_ID_ANY);
22 mlx5_hlist_default_remove_cb(struct mlx5_hlist *h __rte_unused,
23 struct mlx5_hlist_entry *entry)
29 mlx5_hlist_default_match_cb(struct mlx5_hlist *h __rte_unused,
30 struct mlx5_hlist_entry *entry,
31 uint64_t key, void *ctx __rte_unused)
33 return entry->key != key;
37 mlx5_hlist_create(const char *name, uint32_t size, uint32_t entry_size,
38 uint32_t flags, mlx5_hlist_create_cb cb_create,
39 mlx5_hlist_match_cb cb_match, mlx5_hlist_remove_cb cb_remove)
45 if (!size || (!cb_create ^ !cb_remove))
47 /* Align to the next power of 2, 32bits integer is enough now. */
48 if (!rte_is_power_of_2(size)) {
49 act_size = rte_align32pow2(size);
50 DRV_LOG(WARNING, "Size 0x%" PRIX32 " is not power of 2, will "
51 "be aligned to 0x%" PRIX32 ".", size, act_size);
55 alloc_size = sizeof(struct mlx5_hlist) +
56 sizeof(struct mlx5_hlist_head) * act_size;
57 /* Using zmalloc, then no need to initialize the heads. */
58 h = mlx5_malloc(MLX5_MEM_ZERO, alloc_size, RTE_CACHE_LINE_SIZE,
61 DRV_LOG(ERR, "No memory for hash list %s creation",
62 name ? name : "None");
66 snprintf(h->name, MLX5_HLIST_NAMESIZE, "%s", name);
67 h->table_sz = act_size;
68 h->mask = act_size - 1;
69 h->entry_sz = entry_size;
70 h->direct_key = !!(flags & MLX5_HLIST_DIRECT_KEY);
71 h->write_most = !!(flags & MLX5_HLIST_WRITE_MOST);
72 h->cb_create = cb_create ? cb_create : mlx5_hlist_default_create_cb;
73 h->cb_match = cb_match ? cb_match : mlx5_hlist_default_match_cb;
74 h->cb_remove = cb_remove ? cb_remove : mlx5_hlist_default_remove_cb;
75 rte_rwlock_init(&h->lock);
76 DRV_LOG(DEBUG, "Hash list with %s size 0x%" PRIX32 " is created.",
81 static struct mlx5_hlist_entry *
82 __hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx, bool reuse)
85 struct mlx5_hlist_head *first;
86 struct mlx5_hlist_entry *node;
90 idx = (uint32_t)(key & h->mask);
92 idx = rte_hash_crc_8byte(key, 0) & h->mask;
93 first = &h->heads[idx];
94 LIST_FOREACH(node, first, next) {
95 if (!h->cb_match(h, node, key, ctx)) {
97 __atomic_add_fetch(&node->ref_cnt, 1,
99 DRV_LOG(DEBUG, "Hash list %s entry %p "
101 h->name, (void *)node, node->ref_cnt);
109 static struct mlx5_hlist_entry *
110 hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx, bool reuse)
112 struct mlx5_hlist_entry *node;
115 rte_rwlock_read_lock(&h->lock);
116 node = __hlist_lookup(h, key, ctx, reuse);
117 rte_rwlock_read_unlock(&h->lock);
121 struct mlx5_hlist_entry *
122 mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx)
124 return hlist_lookup(h, key, ctx, false);
127 struct mlx5_hlist_entry*
128 mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key, void *ctx)
131 struct mlx5_hlist_head *first;
132 struct mlx5_hlist_entry *entry;
133 uint32_t prev_gen_cnt = 0;
135 MLX5_ASSERT(h && entry);
136 /* Use write lock directly for write-most list. */
137 if (!h->write_most) {
138 prev_gen_cnt = __atomic_load_n(&h->gen_cnt, __ATOMIC_ACQUIRE);
139 entry = hlist_lookup(h, key, ctx, true);
143 rte_rwlock_write_lock(&h->lock);
144 /* Check if the list changed by other threads. */
146 prev_gen_cnt != __atomic_load_n(&h->gen_cnt, __ATOMIC_ACQUIRE)) {
147 entry = __hlist_lookup(h, key, ctx, true);
152 idx = (uint32_t)(key & h->mask);
154 idx = rte_hash_crc_8byte(key, 0) & h->mask;
155 first = &h->heads[idx];
156 entry = h->cb_create(h, key, ctx);
159 DRV_LOG(ERR, "Can't allocate hash list %s entry.", h->name);
164 LIST_INSERT_HEAD(first, entry, next);
165 __atomic_add_fetch(&h->gen_cnt, 1, __ATOMIC_ACQ_REL);
166 DRV_LOG(DEBUG, "Hash list %s entry %p new: %u.",
167 h->name, (void *)entry, entry->ref_cnt);
169 rte_rwlock_write_unlock(&h->lock);
173 struct mlx5_hlist_entry *
174 mlx5_hlist_lookup_ex(struct mlx5_hlist *h, uint64_t key,
175 mlx5_hlist_match_callback_fn cb, void *ctx)
178 struct mlx5_hlist_head *first;
179 struct mlx5_hlist_entry *node;
181 MLX5_ASSERT(h && cb && ctx);
182 idx = rte_hash_crc_8byte(key, 0) & h->mask;
183 first = &h->heads[idx];
184 LIST_FOREACH(node, first, next) {
192 mlx5_hlist_insert_ex(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry,
193 mlx5_hlist_match_callback_fn cb, void *ctx)
196 struct mlx5_hlist_head *first;
197 struct mlx5_hlist_entry *node;
199 MLX5_ASSERT(h && entry && cb && ctx);
200 idx = rte_hash_crc_8byte(entry->key, 0) & h->mask;
201 first = &h->heads[idx];
202 /* No need to reuse the lookup function. */
203 LIST_FOREACH(node, first, next) {
207 LIST_INSERT_HEAD(first, entry, next);
212 mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry)
214 rte_rwlock_write_lock(&h->lock);
215 MLX5_ASSERT(entry && entry->ref_cnt && entry->next.le_prev);
216 DRV_LOG(DEBUG, "Hash list %s entry %p deref: %u.",
217 h->name, (void *)entry, entry->ref_cnt);
218 if (--entry->ref_cnt) {
219 rte_rwlock_write_unlock(&h->lock);
222 LIST_REMOVE(entry, next);
223 /* Set to NULL to get rid of removing action for more than once. */
224 entry->next.le_prev = NULL;
225 h->cb_remove(h, entry);
226 rte_rwlock_write_unlock(&h->lock);
227 DRV_LOG(DEBUG, "Hash list %s entry %p removed.",
228 h->name, (void *)entry);
233 mlx5_hlist_destroy(struct mlx5_hlist *h)
236 struct mlx5_hlist_entry *entry;
239 for (idx = 0; idx < h->table_sz; ++idx) {
240 /* No LIST_FOREACH_SAFE, using while instead. */
241 while (!LIST_EMPTY(&h->heads[idx])) {
242 entry = LIST_FIRST(&h->heads[idx]);
243 LIST_REMOVE(entry, next);
245 * The owner of whole element which contains data entry
246 * is the user, so it's the user's duty to do the clean
247 * up and the free work because someone may not put the
248 * hlist entry at the beginning(suggested to locate at
249 * the beginning). Or else the default free function
252 h->cb_remove(h, entry);
258 /********************* Cache list ************************/
260 static struct mlx5_cache_entry *
261 mlx5_clist_default_create_cb(struct mlx5_cache_list *list,
262 struct mlx5_cache_entry *entry __rte_unused,
263 void *ctx __rte_unused)
265 return mlx5_malloc(MLX5_MEM_ZERO, list->entry_sz, 0, SOCKET_ID_ANY);
269 mlx5_clist_default_remove_cb(struct mlx5_cache_list *list __rte_unused,
270 struct mlx5_cache_entry *entry)
276 mlx5_cache_list_init(struct mlx5_cache_list *list, const char *name,
277 uint32_t entry_size, void *ctx,
278 mlx5_cache_create_cb cb_create,
279 mlx5_cache_match_cb cb_match,
280 mlx5_cache_remove_cb cb_remove)
283 if (!cb_match || (!cb_create ^ !cb_remove))
286 snprintf(list->name, sizeof(list->name), "%s", name);
287 list->entry_sz = entry_size;
289 list->cb_create = cb_create ? cb_create : mlx5_clist_default_create_cb;
290 list->cb_match = cb_match;
291 list->cb_remove = cb_remove ? cb_remove : mlx5_clist_default_remove_cb;
292 rte_rwlock_init(&list->lock);
293 DRV_LOG(DEBUG, "Cache list %s initialized.", list->name);
294 LIST_INIT(&list->head);
298 static struct mlx5_cache_entry *
299 __cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
301 struct mlx5_cache_entry *entry;
303 LIST_FOREACH(entry, &list->head, next) {
304 if (list->cb_match(list, entry, ctx))
307 __atomic_add_fetch(&entry->ref_cnt, 1,
309 DRV_LOG(DEBUG, "Cache list %s entry %p ref++: %u.",
310 list->name, (void *)entry, entry->ref_cnt);
317 static struct mlx5_cache_entry *
318 cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
320 struct mlx5_cache_entry *entry;
322 rte_rwlock_read_lock(&list->lock);
323 entry = __cache_lookup(list, ctx, reuse);
324 rte_rwlock_read_unlock(&list->lock);
328 struct mlx5_cache_entry *
329 mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
331 return cache_lookup(list, ctx, false);
334 struct mlx5_cache_entry *
335 mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
337 struct mlx5_cache_entry *entry;
338 uint32_t prev_gen_cnt = 0;
341 prev_gen_cnt = __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE);
342 /* Lookup with read lock, reuse if found. */
343 entry = cache_lookup(list, ctx, true);
346 /* Not found, append with write lock - block read from other threads. */
347 rte_rwlock_write_lock(&list->lock);
348 /* If list changed by other threads before lock, search again. */
349 if (prev_gen_cnt != __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE)) {
350 /* Lookup and reuse w/o read lock. */
351 entry = __cache_lookup(list, ctx, true);
355 entry = list->cb_create(list, entry, ctx);
357 DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
358 list->name, (void *)entry);
362 LIST_INSERT_HEAD(&list->head, entry, next);
363 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_RELEASE);
364 __atomic_add_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
365 DRV_LOG(DEBUG, "Cache list %s entry %p new: %u.",
366 list->name, (void *)entry, entry->ref_cnt);
368 rte_rwlock_write_unlock(&list->lock);
373 mlx5_cache_unregister(struct mlx5_cache_list *list,
374 struct mlx5_cache_entry *entry)
376 rte_rwlock_write_lock(&list->lock);
377 MLX5_ASSERT(entry && entry->next.le_prev);
378 DRV_LOG(DEBUG, "Cache list %s entry %p ref--: %u.",
379 list->name, (void *)entry, entry->ref_cnt);
380 if (--entry->ref_cnt) {
381 rte_rwlock_write_unlock(&list->lock);
384 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_ACQUIRE);
385 __atomic_sub_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
386 LIST_REMOVE(entry, next);
387 list->cb_remove(list, entry);
388 rte_rwlock_write_unlock(&list->lock);
389 DRV_LOG(DEBUG, "Cache list %s entry %p removed.",
390 list->name, (void *)entry);
395 mlx5_cache_list_destroy(struct mlx5_cache_list *list)
397 struct mlx5_cache_entry *entry;
400 /* no LIST_FOREACH_SAFE, using while instead */
401 while (!LIST_EMPTY(&list->head)) {
402 entry = LIST_FIRST(&list->head);
403 LIST_REMOVE(entry, next);
404 list->cb_remove(list, entry);
405 DRV_LOG(DEBUG, "Cache list %s entry %p destroyed.",
406 list->name, (void *)entry);
408 memset(list, 0, sizeof(*list));
412 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list)
415 return __atomic_load_n(&list->count, __ATOMIC_RELAXED);
418 /********************* Indexed pool **********************/
421 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
423 if (pool->cfg.need_lock)
424 rte_spinlock_lock(&pool->lock);
428 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
430 if (pool->cfg.need_lock)
431 rte_spinlock_unlock(&pool->lock);
434 static inline uint32_t
435 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
437 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
438 uint32_t trunk_idx = 0;
441 if (!cfg->grow_trunk)
442 return entry_idx / cfg->trunk_size;
443 if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
444 trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
445 (cfg->trunk_size << (cfg->grow_shift *
446 cfg->grow_trunk)) + cfg->grow_trunk;
448 for (i = 0; i < cfg->grow_trunk; i++) {
449 if (entry_idx < pool->grow_tbl[i])
457 static inline uint32_t
458 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
460 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
462 return cfg->trunk_size << (cfg->grow_shift *
463 (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
466 static inline uint32_t
467 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
469 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
474 if (!cfg->grow_trunk)
475 return cfg->trunk_size * trunk_idx;
476 if (trunk_idx < cfg->grow_trunk)
477 offset = pool->grow_tbl[trunk_idx - 1];
479 offset = pool->grow_tbl[cfg->grow_trunk - 1] +
480 (cfg->trunk_size << (cfg->grow_shift *
481 cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
485 struct mlx5_indexed_pool *
486 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
488 struct mlx5_indexed_pool *pool;
491 if (!cfg || (!cfg->malloc ^ !cfg->free) ||
492 (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
493 ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
495 pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
496 sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
501 if (!pool->cfg.trunk_size)
502 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
503 if (!cfg->malloc && !cfg->free) {
504 pool->cfg.malloc = mlx5_malloc;
505 pool->cfg.free = mlx5_free;
507 pool->free_list = TRUNK_INVALID;
508 if (pool->cfg.need_lock)
509 rte_spinlock_init(&pool->lock);
511 * Initialize the dynamic grow trunk size lookup table to have a quick
512 * lookup for the trunk entry index offset.
514 for (i = 0; i < cfg->grow_trunk; i++) {
515 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
517 pool->grow_tbl[i] += pool->grow_tbl[i - 1];
523 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
525 struct mlx5_indexed_trunk *trunk;
526 struct mlx5_indexed_trunk **trunk_tmp;
527 struct mlx5_indexed_trunk **p;
528 size_t trunk_size = 0;
533 if (pool->n_trunk_valid == TRUNK_MAX_IDX)
535 if (pool->n_trunk_valid == pool->n_trunk) {
536 /* No free trunk flags, expand trunk list. */
537 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
538 RTE_CACHE_LINE_SIZE / sizeof(void *);
540 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
541 sizeof(struct mlx5_indexed_trunk *),
542 RTE_CACHE_LINE_SIZE, rte_socket_id());
546 memcpy(p, pool->trunks, pool->n_trunk_valid *
547 sizeof(struct mlx5_indexed_trunk *));
548 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
549 n_grow * sizeof(void *));
550 trunk_tmp = pool->trunks;
553 pool->cfg.free(trunk_tmp);
554 pool->n_trunk += n_grow;
556 if (!pool->cfg.release_mem_en) {
557 idx = pool->n_trunk_valid;
559 /* Find the first available slot in trunk list */
560 for (idx = 0; idx < pool->n_trunk; idx++)
561 if (pool->trunks[idx] == NULL)
564 trunk_size += sizeof(*trunk);
565 data_size = mlx5_trunk_size_get(pool, idx);
566 bmp_size = rte_bitmap_get_memory_footprint(data_size);
567 /* rte_bitmap requires memory cacheline aligned. */
568 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
569 trunk_size += bmp_size;
570 trunk = pool->cfg.malloc(0, trunk_size,
571 RTE_CACHE_LINE_SIZE, rte_socket_id());
574 pool->trunks[idx] = trunk;
576 trunk->free = data_size;
577 trunk->prev = TRUNK_INVALID;
578 trunk->next = TRUNK_INVALID;
579 MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
580 pool->free_list = idx;
581 /* Mark all entries as available. */
582 trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
583 [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
585 MLX5_ASSERT(trunk->bmp);
586 pool->n_trunk_valid++;
595 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
597 struct mlx5_indexed_trunk *trunk;
602 mlx5_ipool_lock(pool);
603 if (pool->free_list == TRUNK_INVALID) {
604 /* If no available trunks, grow new. */
605 if (mlx5_ipool_grow(pool)) {
606 mlx5_ipool_unlock(pool);
610 MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
611 trunk = pool->trunks[pool->free_list];
612 MLX5_ASSERT(trunk->free);
613 if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
614 mlx5_ipool_unlock(pool);
618 iidx += __builtin_ctzll(slab);
619 MLX5_ASSERT(iidx != UINT32_MAX);
620 MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
621 rte_bitmap_clear(trunk->bmp, iidx);
622 p = &trunk->data[iidx * pool->cfg.size];
624 * The ipool index should grow continually from small to big,
625 * some features as metering only accept limited bits of index.
626 * Random index with MSB set may be rejected.
628 iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
629 iidx += 1; /* non-zero index. */
635 /* Full trunk will be removed from free list in imalloc. */
636 MLX5_ASSERT(pool->free_list == trunk->idx);
637 pool->free_list = trunk->next;
638 if (trunk->next != TRUNK_INVALID)
639 pool->trunks[trunk->next]->prev = TRUNK_INVALID;
640 trunk->prev = TRUNK_INVALID;
641 trunk->next = TRUNK_INVALID;
648 mlx5_ipool_unlock(pool);
653 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
655 void *entry = mlx5_ipool_malloc(pool, idx);
657 if (entry && pool->cfg.size)
658 memset(entry, 0, pool->cfg.size);
663 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
665 struct mlx5_indexed_trunk *trunk;
672 mlx5_ipool_lock(pool);
673 trunk_idx = mlx5_trunk_idx_get(pool, idx);
674 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
675 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
677 trunk = pool->trunks[trunk_idx];
680 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
681 if (trunk_idx != trunk->idx ||
682 rte_bitmap_get(trunk->bmp, entry_idx))
684 rte_bitmap_set(trunk->bmp, entry_idx);
686 if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
687 (pool, trunk->idx)) {
688 if (pool->free_list == trunk->idx)
689 pool->free_list = trunk->next;
690 if (trunk->next != TRUNK_INVALID)
691 pool->trunks[trunk->next]->prev = trunk->prev;
692 if (trunk->prev != TRUNK_INVALID)
693 pool->trunks[trunk->prev]->next = trunk->next;
694 pool->cfg.free(trunk);
695 pool->trunks[trunk_idx] = NULL;
696 pool->n_trunk_valid--;
701 if (pool->n_trunk_valid == 0) {
702 pool->cfg.free(pool->trunks);
706 } else if (trunk->free == 1) {
707 /* Put into free trunk list head. */
708 MLX5_ASSERT(pool->free_list != trunk->idx);
709 trunk->next = pool->free_list;
710 trunk->prev = TRUNK_INVALID;
711 if (pool->free_list != TRUNK_INVALID)
712 pool->trunks[pool->free_list]->prev = trunk->idx;
713 pool->free_list = trunk->idx;
723 mlx5_ipool_unlock(pool);
727 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
729 struct mlx5_indexed_trunk *trunk;
737 mlx5_ipool_lock(pool);
738 trunk_idx = mlx5_trunk_idx_get(pool, idx);
739 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
740 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
742 trunk = pool->trunks[trunk_idx];
745 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
746 if (trunk_idx != trunk->idx ||
747 rte_bitmap_get(trunk->bmp, entry_idx))
749 p = &trunk->data[entry_idx * pool->cfg.size];
751 mlx5_ipool_unlock(pool);
756 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
758 struct mlx5_indexed_trunk **trunks;
762 mlx5_ipool_lock(pool);
763 trunks = pool->trunks;
764 for (i = 0; i < pool->n_trunk; i++) {
766 pool->cfg.free(trunks[i]);
769 pool->cfg.free(pool->trunks);
770 mlx5_ipool_unlock(pool);
776 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
778 printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
780 pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
781 pool->cfg.trunk_size, pool->n_trunk_valid);
783 printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
784 "available %u free %u\n",
785 pool->cfg.type, pool->n_entry, pool->trunk_new,
786 pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
790 struct mlx5_l3t_tbl *
791 mlx5_l3t_create(enum mlx5_l3t_type type)
793 struct mlx5_l3t_tbl *tbl;
794 struct mlx5_indexed_pool_config l3t_ip_cfg = {
800 .malloc = mlx5_malloc,
804 if (type >= MLX5_L3T_TYPE_MAX) {
808 tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
816 case MLX5_L3T_TYPE_WORD:
817 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
818 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
820 case MLX5_L3T_TYPE_DWORD:
821 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
822 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
824 case MLX5_L3T_TYPE_QWORD:
825 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
826 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
829 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
830 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
833 rte_spinlock_init(&tbl->sl);
834 tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
844 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
846 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
853 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
854 m_tbl = g_tbl->tbl[i];
857 for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
860 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
861 m_tbl->tbl[j])->ref_cnt);
862 mlx5_ipool_free(tbl->eip,
863 ((struct mlx5_l3t_entry_word *)
864 m_tbl->tbl[j])->idx);
866 if (!(--m_tbl->ref_cnt))
869 MLX5_ASSERT(!m_tbl->ref_cnt);
870 mlx5_free(g_tbl->tbl[i]);
872 if (!(--g_tbl->ref_cnt))
875 MLX5_ASSERT(!g_tbl->ref_cnt);
879 mlx5_ipool_destroy(tbl->eip);
884 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
885 union mlx5_l3t_data *data)
887 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
888 struct mlx5_l3t_entry_word *w_e_tbl;
889 struct mlx5_l3t_entry_dword *dw_e_tbl;
890 struct mlx5_l3t_entry_qword *qw_e_tbl;
891 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
898 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
901 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
904 entry_idx = idx & MLX5_L3T_ET_MASK;
906 case MLX5_L3T_TYPE_WORD:
907 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
908 data->word = w_e_tbl->entry[entry_idx].data;
909 if (w_e_tbl->entry[entry_idx].data)
910 w_e_tbl->entry[entry_idx].ref_cnt++;
912 case MLX5_L3T_TYPE_DWORD:
913 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
914 data->dword = dw_e_tbl->entry[entry_idx].data;
915 if (dw_e_tbl->entry[entry_idx].data)
916 dw_e_tbl->entry[entry_idx].ref_cnt++;
918 case MLX5_L3T_TYPE_QWORD:
919 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
920 data->qword = qw_e_tbl->entry[entry_idx].data;
921 if (qw_e_tbl->entry[entry_idx].data)
922 qw_e_tbl->entry[entry_idx].ref_cnt++;
925 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
926 data->ptr = ptr_e_tbl->entry[entry_idx].data;
927 if (ptr_e_tbl->entry[entry_idx].data)
928 ptr_e_tbl->entry[entry_idx].ref_cnt++;
935 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
936 union mlx5_l3t_data *data)
940 rte_spinlock_lock(&tbl->sl);
941 ret = __l3t_get_entry(tbl, idx, data);
942 rte_spinlock_unlock(&tbl->sl);
947 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
949 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
950 struct mlx5_l3t_entry_word *w_e_tbl;
951 struct mlx5_l3t_entry_dword *dw_e_tbl;
952 struct mlx5_l3t_entry_qword *qw_e_tbl;
953 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
959 rte_spinlock_lock(&tbl->sl);
963 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
966 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
969 entry_idx = idx & MLX5_L3T_ET_MASK;
971 case MLX5_L3T_TYPE_WORD:
972 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
973 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
974 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
977 w_e_tbl->entry[entry_idx].data = 0;
978 ref_cnt = --w_e_tbl->ref_cnt;
980 case MLX5_L3T_TYPE_DWORD:
981 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
982 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
983 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
986 dw_e_tbl->entry[entry_idx].data = 0;
987 ref_cnt = --dw_e_tbl->ref_cnt;
989 case MLX5_L3T_TYPE_QWORD:
990 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
991 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
992 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
995 qw_e_tbl->entry[entry_idx].data = 0;
996 ref_cnt = --qw_e_tbl->ref_cnt;
999 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1000 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
1001 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
1004 ptr_e_tbl->entry[entry_idx].data = NULL;
1005 ref_cnt = --ptr_e_tbl->ref_cnt;
1009 mlx5_ipool_free(tbl->eip,
1010 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
1011 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1013 if (!(--m_tbl->ref_cnt)) {
1016 [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
1017 if (!(--g_tbl->ref_cnt)) {
1024 rte_spinlock_unlock(&tbl->sl);
1029 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1030 union mlx5_l3t_data *data)
1032 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1033 struct mlx5_l3t_entry_word *w_e_tbl;
1034 struct mlx5_l3t_entry_dword *dw_e_tbl;
1035 struct mlx5_l3t_entry_qword *qw_e_tbl;
1036 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1038 uint32_t entry_idx, tbl_idx = 0;
1040 /* Check the global table, create it if empty. */
1043 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1044 sizeof(struct mlx5_l3t_level_tbl) +
1045 sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1054 * Check the middle table, create it if empty. Ref_cnt will be
1055 * increased if new sub table created.
1057 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1059 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1060 sizeof(struct mlx5_l3t_level_tbl) +
1061 sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1067 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1072 * Check the entry table, create it if empty. Ref_cnt will be
1073 * increased if new sub entry table created.
1075 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1077 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1082 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1083 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1087 entry_idx = idx & MLX5_L3T_ET_MASK;
1088 switch (tbl->type) {
1089 case MLX5_L3T_TYPE_WORD:
1090 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1091 if (w_e_tbl->entry[entry_idx].data) {
1092 data->word = w_e_tbl->entry[entry_idx].data;
1093 w_e_tbl->entry[entry_idx].ref_cnt++;
1097 w_e_tbl->entry[entry_idx].data = data->word;
1098 w_e_tbl->entry[entry_idx].ref_cnt = 1;
1101 case MLX5_L3T_TYPE_DWORD:
1102 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1103 if (dw_e_tbl->entry[entry_idx].data) {
1104 data->dword = dw_e_tbl->entry[entry_idx].data;
1105 dw_e_tbl->entry[entry_idx].ref_cnt++;
1109 dw_e_tbl->entry[entry_idx].data = data->dword;
1110 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1111 dw_e_tbl->ref_cnt++;
1113 case MLX5_L3T_TYPE_QWORD:
1114 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1115 if (qw_e_tbl->entry[entry_idx].data) {
1116 data->qword = qw_e_tbl->entry[entry_idx].data;
1117 qw_e_tbl->entry[entry_idx].ref_cnt++;
1121 qw_e_tbl->entry[entry_idx].data = data->qword;
1122 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1123 qw_e_tbl->ref_cnt++;
1126 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1127 if (ptr_e_tbl->entry[entry_idx].data) {
1128 data->ptr = ptr_e_tbl->entry[entry_idx].data;
1129 ptr_e_tbl->entry[entry_idx].ref_cnt++;
1133 ptr_e_tbl->entry[entry_idx].data = data->ptr;
1134 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1135 ptr_e_tbl->ref_cnt++;
1142 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1143 union mlx5_l3t_data *data)
1147 rte_spinlock_lock(&tbl->sl);
1148 ret = __l3t_set_entry(tbl, idx, data);
1149 rte_spinlock_unlock(&tbl->sl);
1154 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1155 union mlx5_l3t_data *data,
1156 mlx5_l3t_alloc_callback_fn cb, void *ctx)
1160 rte_spinlock_lock(&tbl->sl);
1161 /* Check if entry data is ready. */
1162 ret = __l3t_get_entry(tbl, idx, data);
1164 switch (tbl->type) {
1165 case MLX5_L3T_TYPE_WORD:
1169 case MLX5_L3T_TYPE_DWORD:
1173 case MLX5_L3T_TYPE_QWORD:
1183 /* Entry data is not ready, use user callback to create it. */
1184 ret = cb(ctx, data);
1187 /* Save the new allocated data to entry. */
1188 ret = __l3t_set_entry(tbl, idx, data);
1190 rte_spinlock_unlock(&tbl->sl);