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)
46 if (!size || (!cb_create ^ !cb_remove))
48 /* Align to the next power of 2, 32bits integer is enough now. */
49 if (!rte_is_power_of_2(size)) {
50 act_size = rte_align32pow2(size);
51 DRV_LOG(WARNING, "Size 0x%" PRIX32 " is not power of 2, will "
52 "be aligned to 0x%" PRIX32 ".", size, act_size);
56 alloc_size = sizeof(struct mlx5_hlist) +
57 sizeof(struct mlx5_hlist_bucket) * act_size;
58 /* Using zmalloc, then no need to initialize the heads. */
59 h = mlx5_malloc(MLX5_MEM_ZERO, alloc_size, RTE_CACHE_LINE_SIZE,
62 DRV_LOG(ERR, "No memory for hash list %s creation",
63 name ? name : "None");
67 snprintf(h->name, MLX5_HLIST_NAMESIZE, "%s", name);
68 h->table_sz = act_size;
69 h->mask = act_size - 1;
70 h->entry_sz = entry_size;
71 h->direct_key = !!(flags & MLX5_HLIST_DIRECT_KEY);
72 h->write_most = !!(flags & MLX5_HLIST_WRITE_MOST);
73 h->cb_create = cb_create ? cb_create : mlx5_hlist_default_create_cb;
74 h->cb_match = cb_match ? cb_match : mlx5_hlist_default_match_cb;
75 h->cb_remove = cb_remove ? cb_remove : mlx5_hlist_default_remove_cb;
76 for (i = 0; i < act_size; i++)
77 rte_rwlock_init(&h->buckets[i].lock);
78 DRV_LOG(DEBUG, "Hash list with %s size 0x%" PRIX32 " is created.",
83 static struct mlx5_hlist_entry *
84 __hlist_lookup(struct mlx5_hlist *h, uint64_t key, uint32_t idx,
85 void *ctx, bool reuse)
87 struct mlx5_hlist_head *first;
88 struct mlx5_hlist_entry *node;
91 first = &h->buckets[idx].head;
92 LIST_FOREACH(node, first, next) {
93 if (!h->cb_match(h, node, key, ctx)) {
95 __atomic_add_fetch(&node->ref_cnt, 1,
97 DRV_LOG(DEBUG, "Hash list %s entry %p "
99 h->name, (void *)node, node->ref_cnt);
107 static struct mlx5_hlist_entry *
108 hlist_lookup(struct mlx5_hlist *h, uint64_t key, uint32_t idx,
109 void *ctx, bool reuse)
111 struct mlx5_hlist_entry *node;
114 rte_rwlock_read_lock(&h->buckets[idx].lock);
115 node = __hlist_lookup(h, key, idx, ctx, reuse);
116 rte_rwlock_read_unlock(&h->buckets[idx].lock);
120 struct mlx5_hlist_entry *
121 mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx)
126 idx = (uint32_t)(key & h->mask);
128 idx = rte_hash_crc_8byte(key, 0) & h->mask;
129 return hlist_lookup(h, key, idx, ctx, false);
132 struct mlx5_hlist_entry*
133 mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key, void *ctx)
136 struct mlx5_hlist_head *first;
137 struct mlx5_hlist_bucket *b;
138 struct mlx5_hlist_entry *entry;
139 uint32_t prev_gen_cnt = 0;
142 idx = (uint32_t)(key & h->mask);
144 idx = rte_hash_crc_8byte(key, 0) & h->mask;
146 b = &h->buckets[idx];
147 /* Use write lock directly for write-most list. */
148 if (!h->write_most) {
149 prev_gen_cnt = __atomic_load_n(&b->gen_cnt, __ATOMIC_ACQUIRE);
150 entry = hlist_lookup(h, key, idx, ctx, true);
154 rte_rwlock_write_lock(&b->lock);
155 /* Check if the list changed by other threads. */
157 prev_gen_cnt != __atomic_load_n(&b->gen_cnt, __ATOMIC_ACQUIRE)) {
158 entry = __hlist_lookup(h, key, idx, ctx, true);
163 entry = h->cb_create(h, key, ctx);
166 DRV_LOG(DEBUG, "Can't allocate hash list %s entry.", h->name);
171 LIST_INSERT_HEAD(first, entry, next);
172 __atomic_add_fetch(&b->gen_cnt, 1, __ATOMIC_ACQ_REL);
173 DRV_LOG(DEBUG, "Hash list %s entry %p new: %u.",
174 h->name, (void *)entry, entry->ref_cnt);
176 rte_rwlock_write_unlock(&b->lock);
181 mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry)
186 idx = (uint32_t)(entry->key & h->mask);
188 idx = rte_hash_crc_8byte(entry->key, 0) & h->mask;
190 rte_rwlock_write_lock(&h->buckets[idx].lock);
191 MLX5_ASSERT(entry && entry->ref_cnt && entry->next.le_prev);
192 DRV_LOG(DEBUG, "Hash list %s entry %p deref: %u.",
193 h->name, (void *)entry, entry->ref_cnt);
194 if (--entry->ref_cnt) {
195 rte_rwlock_write_unlock(&h->buckets[idx].lock);
198 LIST_REMOVE(entry, next);
199 /* Set to NULL to get rid of removing action for more than once. */
200 entry->next.le_prev = NULL;
201 h->cb_remove(h, entry);
202 rte_rwlock_write_unlock(&h->buckets[idx].lock);
203 DRV_LOG(DEBUG, "Hash list %s entry %p removed.",
204 h->name, (void *)entry);
209 mlx5_hlist_destroy(struct mlx5_hlist *h)
212 struct mlx5_hlist_entry *entry;
215 for (idx = 0; idx < h->table_sz; ++idx) {
216 /* No LIST_FOREACH_SAFE, using while instead. */
217 while (!LIST_EMPTY(&h->buckets[idx].head)) {
218 entry = LIST_FIRST(&h->buckets[idx].head);
219 LIST_REMOVE(entry, next);
221 * The owner of whole element which contains data entry
222 * is the user, so it's the user's duty to do the clean
223 * up and the free work because someone may not put the
224 * hlist entry at the beginning(suggested to locate at
225 * the beginning). Or else the default free function
228 h->cb_remove(h, entry);
234 /********************* Cache list ************************/
236 static struct mlx5_cache_entry *
237 mlx5_clist_default_create_cb(struct mlx5_cache_list *list,
238 struct mlx5_cache_entry *entry __rte_unused,
239 void *ctx __rte_unused)
241 return mlx5_malloc(MLX5_MEM_ZERO, list->entry_sz, 0, SOCKET_ID_ANY);
245 mlx5_clist_default_remove_cb(struct mlx5_cache_list *list __rte_unused,
246 struct mlx5_cache_entry *entry)
252 mlx5_cache_list_init(struct mlx5_cache_list *list, const char *name,
253 uint32_t entry_size, void *ctx,
254 mlx5_cache_create_cb cb_create,
255 mlx5_cache_match_cb cb_match,
256 mlx5_cache_remove_cb cb_remove)
259 if (!cb_match || (!cb_create ^ !cb_remove))
262 snprintf(list->name, sizeof(list->name), "%s", name);
263 list->entry_sz = entry_size;
265 list->cb_create = cb_create ? cb_create : mlx5_clist_default_create_cb;
266 list->cb_match = cb_match;
267 list->cb_remove = cb_remove ? cb_remove : mlx5_clist_default_remove_cb;
268 rte_rwlock_init(&list->lock);
269 DRV_LOG(DEBUG, "Cache list %s initialized.", list->name);
270 LIST_INIT(&list->head);
274 static struct mlx5_cache_entry *
275 __cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
277 struct mlx5_cache_entry *entry;
279 LIST_FOREACH(entry, &list->head, next) {
280 if (list->cb_match(list, entry, ctx))
283 __atomic_add_fetch(&entry->ref_cnt, 1,
285 DRV_LOG(DEBUG, "Cache list %s entry %p ref++: %u.",
286 list->name, (void *)entry, entry->ref_cnt);
293 static struct mlx5_cache_entry *
294 cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
296 struct mlx5_cache_entry *entry;
298 rte_rwlock_read_lock(&list->lock);
299 entry = __cache_lookup(list, ctx, reuse);
300 rte_rwlock_read_unlock(&list->lock);
304 struct mlx5_cache_entry *
305 mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
307 return cache_lookup(list, ctx, false);
310 struct mlx5_cache_entry *
311 mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
313 struct mlx5_cache_entry *entry;
314 uint32_t prev_gen_cnt = 0;
317 prev_gen_cnt = __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE);
318 /* Lookup with read lock, reuse if found. */
319 entry = cache_lookup(list, ctx, true);
322 /* Not found, append with write lock - block read from other threads. */
323 rte_rwlock_write_lock(&list->lock);
324 /* If list changed by other threads before lock, search again. */
325 if (prev_gen_cnt != __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE)) {
326 /* Lookup and reuse w/o read lock. */
327 entry = __cache_lookup(list, ctx, true);
331 entry = list->cb_create(list, entry, ctx);
333 DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
334 list->name, (void *)entry);
338 LIST_INSERT_HEAD(&list->head, entry, next);
339 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_RELEASE);
340 __atomic_add_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
341 DRV_LOG(DEBUG, "Cache list %s entry %p new: %u.",
342 list->name, (void *)entry, entry->ref_cnt);
344 rte_rwlock_write_unlock(&list->lock);
349 mlx5_cache_unregister(struct mlx5_cache_list *list,
350 struct mlx5_cache_entry *entry)
352 rte_rwlock_write_lock(&list->lock);
353 MLX5_ASSERT(entry && entry->next.le_prev);
354 DRV_LOG(DEBUG, "Cache list %s entry %p ref--: %u.",
355 list->name, (void *)entry, entry->ref_cnt);
356 if (--entry->ref_cnt) {
357 rte_rwlock_write_unlock(&list->lock);
360 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_ACQUIRE);
361 __atomic_sub_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
362 LIST_REMOVE(entry, next);
363 list->cb_remove(list, entry);
364 rte_rwlock_write_unlock(&list->lock);
365 DRV_LOG(DEBUG, "Cache list %s entry %p removed.",
366 list->name, (void *)entry);
371 mlx5_cache_list_destroy(struct mlx5_cache_list *list)
373 struct mlx5_cache_entry *entry;
376 /* no LIST_FOREACH_SAFE, using while instead */
377 while (!LIST_EMPTY(&list->head)) {
378 entry = LIST_FIRST(&list->head);
379 LIST_REMOVE(entry, next);
380 list->cb_remove(list, entry);
381 DRV_LOG(DEBUG, "Cache list %s entry %p destroyed.",
382 list->name, (void *)entry);
384 memset(list, 0, sizeof(*list));
388 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list)
391 return __atomic_load_n(&list->count, __ATOMIC_RELAXED);
394 /********************* Indexed pool **********************/
397 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
399 if (pool->cfg.need_lock)
400 rte_spinlock_lock(&pool->lock);
404 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
406 if (pool->cfg.need_lock)
407 rte_spinlock_unlock(&pool->lock);
410 static inline uint32_t
411 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
413 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
414 uint32_t trunk_idx = 0;
417 if (!cfg->grow_trunk)
418 return entry_idx / cfg->trunk_size;
419 if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
420 trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
421 (cfg->trunk_size << (cfg->grow_shift *
422 cfg->grow_trunk)) + cfg->grow_trunk;
424 for (i = 0; i < cfg->grow_trunk; i++) {
425 if (entry_idx < pool->grow_tbl[i])
433 static inline uint32_t
434 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
436 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
438 return cfg->trunk_size << (cfg->grow_shift *
439 (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
442 static inline uint32_t
443 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
445 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
450 if (!cfg->grow_trunk)
451 return cfg->trunk_size * trunk_idx;
452 if (trunk_idx < cfg->grow_trunk)
453 offset = pool->grow_tbl[trunk_idx - 1];
455 offset = pool->grow_tbl[cfg->grow_trunk - 1] +
456 (cfg->trunk_size << (cfg->grow_shift *
457 cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
461 struct mlx5_indexed_pool *
462 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
464 struct mlx5_indexed_pool *pool;
467 if (!cfg || (!cfg->malloc ^ !cfg->free) ||
468 (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
469 ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
471 pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
472 sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
477 if (!pool->cfg.trunk_size)
478 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
479 if (!cfg->malloc && !cfg->free) {
480 pool->cfg.malloc = mlx5_malloc;
481 pool->cfg.free = mlx5_free;
483 pool->free_list = TRUNK_INVALID;
484 if (pool->cfg.need_lock)
485 rte_spinlock_init(&pool->lock);
487 * Initialize the dynamic grow trunk size lookup table to have a quick
488 * lookup for the trunk entry index offset.
490 for (i = 0; i < cfg->grow_trunk; i++) {
491 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
493 pool->grow_tbl[i] += pool->grow_tbl[i - 1];
499 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
501 struct mlx5_indexed_trunk *trunk;
502 struct mlx5_indexed_trunk **trunk_tmp;
503 struct mlx5_indexed_trunk **p;
504 size_t trunk_size = 0;
509 if (pool->n_trunk_valid == TRUNK_MAX_IDX)
511 if (pool->n_trunk_valid == pool->n_trunk) {
512 /* No free trunk flags, expand trunk list. */
513 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
514 RTE_CACHE_LINE_SIZE / sizeof(void *);
516 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
517 sizeof(struct mlx5_indexed_trunk *),
518 RTE_CACHE_LINE_SIZE, rte_socket_id());
522 memcpy(p, pool->trunks, pool->n_trunk_valid *
523 sizeof(struct mlx5_indexed_trunk *));
524 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
525 n_grow * sizeof(void *));
526 trunk_tmp = pool->trunks;
529 pool->cfg.free(trunk_tmp);
530 pool->n_trunk += n_grow;
532 if (!pool->cfg.release_mem_en) {
533 idx = pool->n_trunk_valid;
535 /* Find the first available slot in trunk list */
536 for (idx = 0; idx < pool->n_trunk; idx++)
537 if (pool->trunks[idx] == NULL)
540 trunk_size += sizeof(*trunk);
541 data_size = mlx5_trunk_size_get(pool, idx);
542 bmp_size = rte_bitmap_get_memory_footprint(data_size);
543 /* rte_bitmap requires memory cacheline aligned. */
544 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
545 trunk_size += bmp_size;
546 trunk = pool->cfg.malloc(0, trunk_size,
547 RTE_CACHE_LINE_SIZE, rte_socket_id());
550 pool->trunks[idx] = trunk;
552 trunk->free = data_size;
553 trunk->prev = TRUNK_INVALID;
554 trunk->next = TRUNK_INVALID;
555 MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
556 pool->free_list = idx;
557 /* Mark all entries as available. */
558 trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
559 [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
561 MLX5_ASSERT(trunk->bmp);
562 pool->n_trunk_valid++;
571 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
573 struct mlx5_indexed_trunk *trunk;
578 mlx5_ipool_lock(pool);
579 if (pool->free_list == TRUNK_INVALID) {
580 /* If no available trunks, grow new. */
581 if (mlx5_ipool_grow(pool)) {
582 mlx5_ipool_unlock(pool);
586 MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
587 trunk = pool->trunks[pool->free_list];
588 MLX5_ASSERT(trunk->free);
589 if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
590 mlx5_ipool_unlock(pool);
594 iidx += __builtin_ctzll(slab);
595 MLX5_ASSERT(iidx != UINT32_MAX);
596 MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
597 rte_bitmap_clear(trunk->bmp, iidx);
598 p = &trunk->data[iidx * pool->cfg.size];
600 * The ipool index should grow continually from small to big,
601 * some features as metering only accept limited bits of index.
602 * Random index with MSB set may be rejected.
604 iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
605 iidx += 1; /* non-zero index. */
611 /* Full trunk will be removed from free list in imalloc. */
612 MLX5_ASSERT(pool->free_list == trunk->idx);
613 pool->free_list = trunk->next;
614 if (trunk->next != TRUNK_INVALID)
615 pool->trunks[trunk->next]->prev = TRUNK_INVALID;
616 trunk->prev = TRUNK_INVALID;
617 trunk->next = TRUNK_INVALID;
624 mlx5_ipool_unlock(pool);
629 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
631 void *entry = mlx5_ipool_malloc(pool, idx);
633 if (entry && pool->cfg.size)
634 memset(entry, 0, pool->cfg.size);
639 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
641 struct mlx5_indexed_trunk *trunk;
648 mlx5_ipool_lock(pool);
649 trunk_idx = mlx5_trunk_idx_get(pool, idx);
650 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
651 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
653 trunk = pool->trunks[trunk_idx];
656 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
657 if (trunk_idx != trunk->idx ||
658 rte_bitmap_get(trunk->bmp, entry_idx))
660 rte_bitmap_set(trunk->bmp, entry_idx);
662 if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
663 (pool, trunk->idx)) {
664 if (pool->free_list == trunk->idx)
665 pool->free_list = trunk->next;
666 if (trunk->next != TRUNK_INVALID)
667 pool->trunks[trunk->next]->prev = trunk->prev;
668 if (trunk->prev != TRUNK_INVALID)
669 pool->trunks[trunk->prev]->next = trunk->next;
670 pool->cfg.free(trunk);
671 pool->trunks[trunk_idx] = NULL;
672 pool->n_trunk_valid--;
677 if (pool->n_trunk_valid == 0) {
678 pool->cfg.free(pool->trunks);
682 } else if (trunk->free == 1) {
683 /* Put into free trunk list head. */
684 MLX5_ASSERT(pool->free_list != trunk->idx);
685 trunk->next = pool->free_list;
686 trunk->prev = TRUNK_INVALID;
687 if (pool->free_list != TRUNK_INVALID)
688 pool->trunks[pool->free_list]->prev = trunk->idx;
689 pool->free_list = trunk->idx;
699 mlx5_ipool_unlock(pool);
703 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
705 struct mlx5_indexed_trunk *trunk;
713 mlx5_ipool_lock(pool);
714 trunk_idx = mlx5_trunk_idx_get(pool, idx);
715 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
716 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
718 trunk = pool->trunks[trunk_idx];
721 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
722 if (trunk_idx != trunk->idx ||
723 rte_bitmap_get(trunk->bmp, entry_idx))
725 p = &trunk->data[entry_idx * pool->cfg.size];
727 mlx5_ipool_unlock(pool);
732 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
734 struct mlx5_indexed_trunk **trunks;
738 mlx5_ipool_lock(pool);
739 trunks = pool->trunks;
740 for (i = 0; i < pool->n_trunk; i++) {
742 pool->cfg.free(trunks[i]);
745 pool->cfg.free(pool->trunks);
746 mlx5_ipool_unlock(pool);
752 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
754 printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
756 pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
757 pool->cfg.trunk_size, pool->n_trunk_valid);
759 printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
760 "available %u free %u\n",
761 pool->cfg.type, pool->n_entry, pool->trunk_new,
762 pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
766 struct mlx5_l3t_tbl *
767 mlx5_l3t_create(enum mlx5_l3t_type type)
769 struct mlx5_l3t_tbl *tbl;
770 struct mlx5_indexed_pool_config l3t_ip_cfg = {
776 .malloc = mlx5_malloc,
780 if (type >= MLX5_L3T_TYPE_MAX) {
784 tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
792 case MLX5_L3T_TYPE_WORD:
793 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
794 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
796 case MLX5_L3T_TYPE_DWORD:
797 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
798 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
800 case MLX5_L3T_TYPE_QWORD:
801 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
802 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
805 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
806 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
809 rte_spinlock_init(&tbl->sl);
810 tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
820 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
822 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
829 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
830 m_tbl = g_tbl->tbl[i];
833 for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
836 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
837 m_tbl->tbl[j])->ref_cnt);
838 mlx5_ipool_free(tbl->eip,
839 ((struct mlx5_l3t_entry_word *)
840 m_tbl->tbl[j])->idx);
842 if (!(--m_tbl->ref_cnt))
845 MLX5_ASSERT(!m_tbl->ref_cnt);
846 mlx5_free(g_tbl->tbl[i]);
848 if (!(--g_tbl->ref_cnt))
851 MLX5_ASSERT(!g_tbl->ref_cnt);
855 mlx5_ipool_destroy(tbl->eip);
860 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
861 union mlx5_l3t_data *data)
863 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
864 struct mlx5_l3t_entry_word *w_e_tbl;
865 struct mlx5_l3t_entry_dword *dw_e_tbl;
866 struct mlx5_l3t_entry_qword *qw_e_tbl;
867 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
874 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
877 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
880 entry_idx = idx & MLX5_L3T_ET_MASK;
882 case MLX5_L3T_TYPE_WORD:
883 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
884 data->word = w_e_tbl->entry[entry_idx].data;
885 if (w_e_tbl->entry[entry_idx].data)
886 w_e_tbl->entry[entry_idx].ref_cnt++;
888 case MLX5_L3T_TYPE_DWORD:
889 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
890 data->dword = dw_e_tbl->entry[entry_idx].data;
891 if (dw_e_tbl->entry[entry_idx].data)
892 dw_e_tbl->entry[entry_idx].ref_cnt++;
894 case MLX5_L3T_TYPE_QWORD:
895 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
896 data->qword = qw_e_tbl->entry[entry_idx].data;
897 if (qw_e_tbl->entry[entry_idx].data)
898 qw_e_tbl->entry[entry_idx].ref_cnt++;
901 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
902 data->ptr = ptr_e_tbl->entry[entry_idx].data;
903 if (ptr_e_tbl->entry[entry_idx].data)
904 ptr_e_tbl->entry[entry_idx].ref_cnt++;
911 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
912 union mlx5_l3t_data *data)
916 rte_spinlock_lock(&tbl->sl);
917 ret = __l3t_get_entry(tbl, idx, data);
918 rte_spinlock_unlock(&tbl->sl);
923 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
925 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
926 struct mlx5_l3t_entry_word *w_e_tbl;
927 struct mlx5_l3t_entry_dword *dw_e_tbl;
928 struct mlx5_l3t_entry_qword *qw_e_tbl;
929 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
935 rte_spinlock_lock(&tbl->sl);
939 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
942 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
945 entry_idx = idx & MLX5_L3T_ET_MASK;
947 case MLX5_L3T_TYPE_WORD:
948 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
949 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
950 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
953 w_e_tbl->entry[entry_idx].data = 0;
954 ref_cnt = --w_e_tbl->ref_cnt;
956 case MLX5_L3T_TYPE_DWORD:
957 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
958 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
959 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
962 dw_e_tbl->entry[entry_idx].data = 0;
963 ref_cnt = --dw_e_tbl->ref_cnt;
965 case MLX5_L3T_TYPE_QWORD:
966 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
967 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
968 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
971 qw_e_tbl->entry[entry_idx].data = 0;
972 ref_cnt = --qw_e_tbl->ref_cnt;
975 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
976 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
977 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
980 ptr_e_tbl->entry[entry_idx].data = NULL;
981 ref_cnt = --ptr_e_tbl->ref_cnt;
985 mlx5_ipool_free(tbl->eip,
986 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
987 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
989 if (!(--m_tbl->ref_cnt)) {
992 [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
993 if (!(--g_tbl->ref_cnt)) {
1000 rte_spinlock_unlock(&tbl->sl);
1005 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1006 union mlx5_l3t_data *data)
1008 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1009 struct mlx5_l3t_entry_word *w_e_tbl;
1010 struct mlx5_l3t_entry_dword *dw_e_tbl;
1011 struct mlx5_l3t_entry_qword *qw_e_tbl;
1012 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1014 uint32_t entry_idx, tbl_idx = 0;
1016 /* Check the global table, create it if empty. */
1019 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1020 sizeof(struct mlx5_l3t_level_tbl) +
1021 sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1030 * Check the middle table, create it if empty. Ref_cnt will be
1031 * increased if new sub table created.
1033 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1035 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1036 sizeof(struct mlx5_l3t_level_tbl) +
1037 sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1043 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1048 * Check the entry table, create it if empty. Ref_cnt will be
1049 * increased if new sub entry table created.
1051 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1053 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1058 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1059 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1063 entry_idx = idx & MLX5_L3T_ET_MASK;
1064 switch (tbl->type) {
1065 case MLX5_L3T_TYPE_WORD:
1066 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1067 if (w_e_tbl->entry[entry_idx].data) {
1068 data->word = w_e_tbl->entry[entry_idx].data;
1069 w_e_tbl->entry[entry_idx].ref_cnt++;
1073 w_e_tbl->entry[entry_idx].data = data->word;
1074 w_e_tbl->entry[entry_idx].ref_cnt = 1;
1077 case MLX5_L3T_TYPE_DWORD:
1078 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1079 if (dw_e_tbl->entry[entry_idx].data) {
1080 data->dword = dw_e_tbl->entry[entry_idx].data;
1081 dw_e_tbl->entry[entry_idx].ref_cnt++;
1085 dw_e_tbl->entry[entry_idx].data = data->dword;
1086 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1087 dw_e_tbl->ref_cnt++;
1089 case MLX5_L3T_TYPE_QWORD:
1090 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1091 if (qw_e_tbl->entry[entry_idx].data) {
1092 data->qword = qw_e_tbl->entry[entry_idx].data;
1093 qw_e_tbl->entry[entry_idx].ref_cnt++;
1097 qw_e_tbl->entry[entry_idx].data = data->qword;
1098 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1099 qw_e_tbl->ref_cnt++;
1102 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1103 if (ptr_e_tbl->entry[entry_idx].data) {
1104 data->ptr = ptr_e_tbl->entry[entry_idx].data;
1105 ptr_e_tbl->entry[entry_idx].ref_cnt++;
1109 ptr_e_tbl->entry[entry_idx].data = data->ptr;
1110 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1111 ptr_e_tbl->ref_cnt++;
1118 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1119 union mlx5_l3t_data *data)
1123 rte_spinlock_lock(&tbl->sl);
1124 ret = __l3t_set_entry(tbl, idx, data);
1125 rte_spinlock_unlock(&tbl->sl);
1130 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1131 union mlx5_l3t_data *data,
1132 mlx5_l3t_alloc_callback_fn cb, void *ctx)
1136 rte_spinlock_lock(&tbl->sl);
1137 /* Check if entry data is ready. */
1138 ret = __l3t_get_entry(tbl, idx, data);
1140 switch (tbl->type) {
1141 case MLX5_L3T_TYPE_WORD:
1145 case MLX5_L3T_TYPE_DWORD:
1149 case MLX5_L3T_TYPE_QWORD:
1159 /* Entry data is not ready, use user callback to create it. */
1160 ret = cb(ctx, data);
1163 /* Save the new allocated data to entry. */
1164 ret = __l3t_set_entry(tbl, idx, data);
1166 rte_spinlock_unlock(&tbl->sl);