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 /********************* Indexed pool **********************/
261 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
263 if (pool->cfg.need_lock)
264 rte_spinlock_lock(&pool->lock);
268 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
270 if (pool->cfg.need_lock)
271 rte_spinlock_unlock(&pool->lock);
274 static inline uint32_t
275 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
277 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
278 uint32_t trunk_idx = 0;
281 if (!cfg->grow_trunk)
282 return entry_idx / cfg->trunk_size;
283 if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
284 trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
285 (cfg->trunk_size << (cfg->grow_shift *
286 cfg->grow_trunk)) + cfg->grow_trunk;
288 for (i = 0; i < cfg->grow_trunk; i++) {
289 if (entry_idx < pool->grow_tbl[i])
297 static inline uint32_t
298 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
300 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
302 return cfg->trunk_size << (cfg->grow_shift *
303 (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
306 static inline uint32_t
307 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
309 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
314 if (!cfg->grow_trunk)
315 return cfg->trunk_size * trunk_idx;
316 if (trunk_idx < cfg->grow_trunk)
317 offset = pool->grow_tbl[trunk_idx - 1];
319 offset = pool->grow_tbl[cfg->grow_trunk - 1] +
320 (cfg->trunk_size << (cfg->grow_shift *
321 cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
325 struct mlx5_indexed_pool *
326 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
328 struct mlx5_indexed_pool *pool;
331 if (!cfg || (!cfg->malloc ^ !cfg->free) ||
332 (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
333 ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
335 pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
336 sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
341 if (!pool->cfg.trunk_size)
342 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
343 if (!cfg->malloc && !cfg->free) {
344 pool->cfg.malloc = mlx5_malloc;
345 pool->cfg.free = mlx5_free;
347 pool->free_list = TRUNK_INVALID;
348 if (pool->cfg.need_lock)
349 rte_spinlock_init(&pool->lock);
351 * Initialize the dynamic grow trunk size lookup table to have a quick
352 * lookup for the trunk entry index offset.
354 for (i = 0; i < cfg->grow_trunk; i++) {
355 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
357 pool->grow_tbl[i] += pool->grow_tbl[i - 1];
363 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
365 struct mlx5_indexed_trunk *trunk;
366 struct mlx5_indexed_trunk **trunk_tmp;
367 struct mlx5_indexed_trunk **p;
368 size_t trunk_size = 0;
373 if (pool->n_trunk_valid == TRUNK_MAX_IDX)
375 if (pool->n_trunk_valid == pool->n_trunk) {
376 /* No free trunk flags, expand trunk list. */
377 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
378 RTE_CACHE_LINE_SIZE / sizeof(void *);
380 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
381 sizeof(struct mlx5_indexed_trunk *),
382 RTE_CACHE_LINE_SIZE, rte_socket_id());
386 memcpy(p, pool->trunks, pool->n_trunk_valid *
387 sizeof(struct mlx5_indexed_trunk *));
388 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
389 n_grow * sizeof(void *));
390 trunk_tmp = pool->trunks;
393 pool->cfg.free(trunk_tmp);
394 pool->n_trunk += n_grow;
396 if (!pool->cfg.release_mem_en) {
397 idx = pool->n_trunk_valid;
399 /* Find the first available slot in trunk list */
400 for (idx = 0; idx < pool->n_trunk; idx++)
401 if (pool->trunks[idx] == NULL)
404 trunk_size += sizeof(*trunk);
405 data_size = mlx5_trunk_size_get(pool, idx);
406 bmp_size = rte_bitmap_get_memory_footprint(data_size);
407 /* rte_bitmap requires memory cacheline aligned. */
408 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
409 trunk_size += bmp_size;
410 trunk = pool->cfg.malloc(0, trunk_size,
411 RTE_CACHE_LINE_SIZE, rte_socket_id());
414 pool->trunks[idx] = trunk;
416 trunk->free = data_size;
417 trunk->prev = TRUNK_INVALID;
418 trunk->next = TRUNK_INVALID;
419 MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
420 pool->free_list = idx;
421 /* Mark all entries as available. */
422 trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
423 [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
425 MLX5_ASSERT(trunk->bmp);
426 pool->n_trunk_valid++;
435 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
437 struct mlx5_indexed_trunk *trunk;
442 mlx5_ipool_lock(pool);
443 if (pool->free_list == TRUNK_INVALID) {
444 /* If no available trunks, grow new. */
445 if (mlx5_ipool_grow(pool)) {
446 mlx5_ipool_unlock(pool);
450 MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
451 trunk = pool->trunks[pool->free_list];
452 MLX5_ASSERT(trunk->free);
453 if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
454 mlx5_ipool_unlock(pool);
458 iidx += __builtin_ctzll(slab);
459 MLX5_ASSERT(iidx != UINT32_MAX);
460 MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
461 rte_bitmap_clear(trunk->bmp, iidx);
462 p = &trunk->data[iidx * pool->cfg.size];
464 * The ipool index should grow continually from small to big,
465 * some features as metering only accept limited bits of index.
466 * Random index with MSB set may be rejected.
468 iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
469 iidx += 1; /* non-zero index. */
475 /* Full trunk will be removed from free list in imalloc. */
476 MLX5_ASSERT(pool->free_list == trunk->idx);
477 pool->free_list = trunk->next;
478 if (trunk->next != TRUNK_INVALID)
479 pool->trunks[trunk->next]->prev = TRUNK_INVALID;
480 trunk->prev = TRUNK_INVALID;
481 trunk->next = TRUNK_INVALID;
488 mlx5_ipool_unlock(pool);
493 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
495 void *entry = mlx5_ipool_malloc(pool, idx);
497 if (entry && pool->cfg.size)
498 memset(entry, 0, pool->cfg.size);
503 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
505 struct mlx5_indexed_trunk *trunk;
512 mlx5_ipool_lock(pool);
513 trunk_idx = mlx5_trunk_idx_get(pool, idx);
514 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
515 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
517 trunk = pool->trunks[trunk_idx];
520 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
521 if (trunk_idx != trunk->idx ||
522 rte_bitmap_get(trunk->bmp, entry_idx))
524 rte_bitmap_set(trunk->bmp, entry_idx);
526 if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
527 (pool, trunk->idx)) {
528 if (pool->free_list == trunk->idx)
529 pool->free_list = trunk->next;
530 if (trunk->next != TRUNK_INVALID)
531 pool->trunks[trunk->next]->prev = trunk->prev;
532 if (trunk->prev != TRUNK_INVALID)
533 pool->trunks[trunk->prev]->next = trunk->next;
534 pool->cfg.free(trunk);
535 pool->trunks[trunk_idx] = NULL;
536 pool->n_trunk_valid--;
541 if (pool->n_trunk_valid == 0) {
542 pool->cfg.free(pool->trunks);
546 } else if (trunk->free == 1) {
547 /* Put into free trunk list head. */
548 MLX5_ASSERT(pool->free_list != trunk->idx);
549 trunk->next = pool->free_list;
550 trunk->prev = TRUNK_INVALID;
551 if (pool->free_list != TRUNK_INVALID)
552 pool->trunks[pool->free_list]->prev = trunk->idx;
553 pool->free_list = trunk->idx;
563 mlx5_ipool_unlock(pool);
567 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
569 struct mlx5_indexed_trunk *trunk;
577 mlx5_ipool_lock(pool);
578 trunk_idx = mlx5_trunk_idx_get(pool, idx);
579 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
580 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
582 trunk = pool->trunks[trunk_idx];
585 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
586 if (trunk_idx != trunk->idx ||
587 rte_bitmap_get(trunk->bmp, entry_idx))
589 p = &trunk->data[entry_idx * pool->cfg.size];
591 mlx5_ipool_unlock(pool);
596 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
598 struct mlx5_indexed_trunk **trunks;
602 mlx5_ipool_lock(pool);
603 trunks = pool->trunks;
604 for (i = 0; i < pool->n_trunk; i++) {
606 pool->cfg.free(trunks[i]);
609 pool->cfg.free(pool->trunks);
610 mlx5_ipool_unlock(pool);
616 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
618 printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
620 pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
621 pool->cfg.trunk_size, pool->n_trunk_valid);
623 printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
624 "available %u free %u\n",
625 pool->cfg.type, pool->n_entry, pool->trunk_new,
626 pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
630 struct mlx5_l3t_tbl *
631 mlx5_l3t_create(enum mlx5_l3t_type type)
633 struct mlx5_l3t_tbl *tbl;
634 struct mlx5_indexed_pool_config l3t_ip_cfg = {
640 .malloc = mlx5_malloc,
644 if (type >= MLX5_L3T_TYPE_MAX) {
648 tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
656 case MLX5_L3T_TYPE_WORD:
657 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
658 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
660 case MLX5_L3T_TYPE_DWORD:
661 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
662 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
664 case MLX5_L3T_TYPE_QWORD:
665 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
666 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
669 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
670 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
673 rte_spinlock_init(&tbl->sl);
674 tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
684 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
686 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
693 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
694 m_tbl = g_tbl->tbl[i];
697 for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
700 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
701 m_tbl->tbl[j])->ref_cnt);
702 mlx5_ipool_free(tbl->eip,
703 ((struct mlx5_l3t_entry_word *)
704 m_tbl->tbl[j])->idx);
706 if (!(--m_tbl->ref_cnt))
709 MLX5_ASSERT(!m_tbl->ref_cnt);
710 mlx5_free(g_tbl->tbl[i]);
712 if (!(--g_tbl->ref_cnt))
715 MLX5_ASSERT(!g_tbl->ref_cnt);
719 mlx5_ipool_destroy(tbl->eip);
724 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
725 union mlx5_l3t_data *data)
727 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
728 struct mlx5_l3t_entry_word *w_e_tbl;
729 struct mlx5_l3t_entry_dword *dw_e_tbl;
730 struct mlx5_l3t_entry_qword *qw_e_tbl;
731 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
738 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
741 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
744 entry_idx = idx & MLX5_L3T_ET_MASK;
746 case MLX5_L3T_TYPE_WORD:
747 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
748 data->word = w_e_tbl->entry[entry_idx].data;
749 if (w_e_tbl->entry[entry_idx].data)
750 w_e_tbl->entry[entry_idx].ref_cnt++;
752 case MLX5_L3T_TYPE_DWORD:
753 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
754 data->dword = dw_e_tbl->entry[entry_idx].data;
755 if (dw_e_tbl->entry[entry_idx].data)
756 dw_e_tbl->entry[entry_idx].ref_cnt++;
758 case MLX5_L3T_TYPE_QWORD:
759 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
760 data->qword = qw_e_tbl->entry[entry_idx].data;
761 if (qw_e_tbl->entry[entry_idx].data)
762 qw_e_tbl->entry[entry_idx].ref_cnt++;
765 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
766 data->ptr = ptr_e_tbl->entry[entry_idx].data;
767 if (ptr_e_tbl->entry[entry_idx].data)
768 ptr_e_tbl->entry[entry_idx].ref_cnt++;
775 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
776 union mlx5_l3t_data *data)
780 rte_spinlock_lock(&tbl->sl);
781 ret = __l3t_get_entry(tbl, idx, data);
782 rte_spinlock_unlock(&tbl->sl);
787 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
789 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
790 struct mlx5_l3t_entry_word *w_e_tbl;
791 struct mlx5_l3t_entry_dword *dw_e_tbl;
792 struct mlx5_l3t_entry_qword *qw_e_tbl;
793 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
799 rte_spinlock_lock(&tbl->sl);
803 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
806 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
809 entry_idx = idx & MLX5_L3T_ET_MASK;
811 case MLX5_L3T_TYPE_WORD:
812 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
813 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
814 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
817 w_e_tbl->entry[entry_idx].data = 0;
818 ref_cnt = --w_e_tbl->ref_cnt;
820 case MLX5_L3T_TYPE_DWORD:
821 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
822 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
823 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
826 dw_e_tbl->entry[entry_idx].data = 0;
827 ref_cnt = --dw_e_tbl->ref_cnt;
829 case MLX5_L3T_TYPE_QWORD:
830 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
831 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
832 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
835 qw_e_tbl->entry[entry_idx].data = 0;
836 ref_cnt = --qw_e_tbl->ref_cnt;
839 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
840 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
841 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
844 ptr_e_tbl->entry[entry_idx].data = NULL;
845 ref_cnt = --ptr_e_tbl->ref_cnt;
849 mlx5_ipool_free(tbl->eip,
850 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
851 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
853 if (!(--m_tbl->ref_cnt)) {
856 [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
857 if (!(--g_tbl->ref_cnt)) {
864 rte_spinlock_unlock(&tbl->sl);
869 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
870 union mlx5_l3t_data *data)
872 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
873 struct mlx5_l3t_entry_word *w_e_tbl;
874 struct mlx5_l3t_entry_dword *dw_e_tbl;
875 struct mlx5_l3t_entry_qword *qw_e_tbl;
876 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
878 uint32_t entry_idx, tbl_idx = 0;
880 /* Check the global table, create it if empty. */
883 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
884 sizeof(struct mlx5_l3t_level_tbl) +
885 sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
894 * Check the middle table, create it if empty. Ref_cnt will be
895 * increased if new sub table created.
897 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
899 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
900 sizeof(struct mlx5_l3t_level_tbl) +
901 sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
907 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
912 * Check the entry table, create it if empty. Ref_cnt will be
913 * increased if new sub entry table created.
915 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
917 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
922 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
923 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
927 entry_idx = idx & MLX5_L3T_ET_MASK;
929 case MLX5_L3T_TYPE_WORD:
930 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
931 if (w_e_tbl->entry[entry_idx].data) {
932 data->word = w_e_tbl->entry[entry_idx].data;
933 w_e_tbl->entry[entry_idx].ref_cnt++;
937 w_e_tbl->entry[entry_idx].data = data->word;
938 w_e_tbl->entry[entry_idx].ref_cnt = 1;
941 case MLX5_L3T_TYPE_DWORD:
942 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
943 if (dw_e_tbl->entry[entry_idx].data) {
944 data->dword = dw_e_tbl->entry[entry_idx].data;
945 dw_e_tbl->entry[entry_idx].ref_cnt++;
949 dw_e_tbl->entry[entry_idx].data = data->dword;
950 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
953 case MLX5_L3T_TYPE_QWORD:
954 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
955 if (qw_e_tbl->entry[entry_idx].data) {
956 data->qword = qw_e_tbl->entry[entry_idx].data;
957 qw_e_tbl->entry[entry_idx].ref_cnt++;
961 qw_e_tbl->entry[entry_idx].data = data->qword;
962 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
966 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
967 if (ptr_e_tbl->entry[entry_idx].data) {
968 data->ptr = ptr_e_tbl->entry[entry_idx].data;
969 ptr_e_tbl->entry[entry_idx].ref_cnt++;
973 ptr_e_tbl->entry[entry_idx].data = data->ptr;
974 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
975 ptr_e_tbl->ref_cnt++;
982 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
983 union mlx5_l3t_data *data)
987 rte_spinlock_lock(&tbl->sl);
988 ret = __l3t_set_entry(tbl, idx, data);
989 rte_spinlock_unlock(&tbl->sl);
994 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
995 union mlx5_l3t_data *data,
996 mlx5_l3t_alloc_callback_fn cb, void *ctx)
1000 rte_spinlock_lock(&tbl->sl);
1001 /* Check if entry data is ready. */
1002 ret = __l3t_get_entry(tbl, idx, data);
1004 switch (tbl->type) {
1005 case MLX5_L3T_TYPE_WORD:
1009 case MLX5_L3T_TYPE_DWORD:
1013 case MLX5_L3T_TYPE_QWORD:
1023 /* Entry data is not ready, use user callback to create it. */
1024 ret = cb(ctx, data);
1027 /* Save the new allocated data to entry. */
1028 ret = __l3t_set_entry(tbl, idx, data);
1030 rte_spinlock_unlock(&tbl->sl);