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_create(const char *name, uint32_t size, uint32_t entry_size,
30 uint32_t flags, mlx5_hlist_create_cb cb_create,
31 mlx5_hlist_match_cb cb_match, mlx5_hlist_remove_cb cb_remove)
38 if (!size || !cb_match || (!cb_create ^ !cb_remove))
40 /* Align to the next power of 2, 32bits integer is enough now. */
41 if (!rte_is_power_of_2(size)) {
42 act_size = rte_align32pow2(size);
43 DRV_LOG(WARNING, "Size 0x%" PRIX32 " is not power of 2, will "
44 "be aligned to 0x%" PRIX32 ".", size, act_size);
48 alloc_size = sizeof(struct mlx5_hlist) +
49 sizeof(struct mlx5_hlist_bucket) * act_size;
50 /* Using zmalloc, then no need to initialize the heads. */
51 h = mlx5_malloc(MLX5_MEM_ZERO, alloc_size, RTE_CACHE_LINE_SIZE,
54 DRV_LOG(ERR, "No memory for hash list %s creation",
55 name ? name : "None");
59 snprintf(h->name, MLX5_HLIST_NAMESIZE, "%s", name);
60 h->table_sz = act_size;
61 h->mask = act_size - 1;
62 h->entry_sz = entry_size;
63 h->direct_key = !!(flags & MLX5_HLIST_DIRECT_KEY);
64 h->write_most = !!(flags & MLX5_HLIST_WRITE_MOST);
65 h->cb_create = cb_create ? cb_create : mlx5_hlist_default_create_cb;
66 h->cb_match = cb_match;
67 h->cb_remove = cb_remove ? cb_remove : mlx5_hlist_default_remove_cb;
68 for (i = 0; i < act_size; i++)
69 rte_rwlock_init(&h->buckets[i].lock);
70 DRV_LOG(DEBUG, "Hash list with %s size 0x%" PRIX32 " is created.",
75 static struct mlx5_hlist_entry *
76 __hlist_lookup(struct mlx5_hlist *h, uint64_t key, uint32_t idx,
77 void *ctx, bool reuse)
79 struct mlx5_hlist_head *first;
80 struct mlx5_hlist_entry *node;
83 first = &h->buckets[idx].head;
84 LIST_FOREACH(node, first, next) {
85 if (!h->cb_match(h, node, key, ctx)) {
87 __atomic_add_fetch(&node->ref_cnt, 1,
89 DRV_LOG(DEBUG, "Hash list %s entry %p "
91 h->name, (void *)node, node->ref_cnt);
99 static struct mlx5_hlist_entry *
100 hlist_lookup(struct mlx5_hlist *h, uint64_t key, uint32_t idx,
101 void *ctx, bool reuse)
103 struct mlx5_hlist_entry *node;
106 rte_rwlock_read_lock(&h->buckets[idx].lock);
107 node = __hlist_lookup(h, key, idx, ctx, reuse);
108 rte_rwlock_read_unlock(&h->buckets[idx].lock);
112 struct mlx5_hlist_entry *
113 mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key, void *ctx)
118 idx = (uint32_t)(key & h->mask);
120 idx = rte_hash_crc_8byte(key, 0) & h->mask;
121 return hlist_lookup(h, key, idx, ctx, false);
124 struct mlx5_hlist_entry*
125 mlx5_hlist_register(struct mlx5_hlist *h, uint64_t key, void *ctx)
128 struct mlx5_hlist_head *first;
129 struct mlx5_hlist_bucket *b;
130 struct mlx5_hlist_entry *entry;
131 uint32_t prev_gen_cnt = 0;
134 idx = (uint32_t)(key & h->mask);
136 idx = rte_hash_crc_8byte(key, 0) & h->mask;
138 b = &h->buckets[idx];
139 /* Use write lock directly for write-most list. */
140 if (!h->write_most) {
141 prev_gen_cnt = __atomic_load_n(&b->gen_cnt, __ATOMIC_ACQUIRE);
142 entry = hlist_lookup(h, key, idx, ctx, true);
146 rte_rwlock_write_lock(&b->lock);
147 /* Check if the list changed by other threads. */
149 prev_gen_cnt != __atomic_load_n(&b->gen_cnt, __ATOMIC_ACQUIRE)) {
150 entry = __hlist_lookup(h, key, idx, ctx, true);
155 entry = h->cb_create(h, key, ctx);
158 DRV_LOG(DEBUG, "Can't allocate hash list %s entry.", h->name);
163 LIST_INSERT_HEAD(first, entry, next);
164 __atomic_add_fetch(&b->gen_cnt, 1, __ATOMIC_ACQ_REL);
165 DRV_LOG(DEBUG, "Hash list %s entry %p new: %u.",
166 h->name, (void *)entry, entry->ref_cnt);
168 rte_rwlock_write_unlock(&b->lock);
173 mlx5_hlist_unregister(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry)
175 uint32_t idx = entry->idx;
177 rte_rwlock_write_lock(&h->buckets[idx].lock);
178 MLX5_ASSERT(entry && entry->ref_cnt && entry->next.le_prev);
179 DRV_LOG(DEBUG, "Hash list %s entry %p deref: %u.",
180 h->name, (void *)entry, entry->ref_cnt);
181 if (--entry->ref_cnt) {
182 rte_rwlock_write_unlock(&h->buckets[idx].lock);
185 LIST_REMOVE(entry, next);
186 /* Set to NULL to get rid of removing action for more than once. */
187 entry->next.le_prev = NULL;
188 h->cb_remove(h, entry);
189 rte_rwlock_write_unlock(&h->buckets[idx].lock);
190 DRV_LOG(DEBUG, "Hash list %s entry %p removed.",
191 h->name, (void *)entry);
196 mlx5_hlist_destroy(struct mlx5_hlist *h)
199 struct mlx5_hlist_entry *entry;
202 for (idx = 0; idx < h->table_sz; ++idx) {
203 /* No LIST_FOREACH_SAFE, using while instead. */
204 while (!LIST_EMPTY(&h->buckets[idx].head)) {
205 entry = LIST_FIRST(&h->buckets[idx].head);
206 LIST_REMOVE(entry, next);
208 * The owner of whole element which contains data entry
209 * is the user, so it's the user's duty to do the clean
210 * up and the free work because someone may not put the
211 * hlist entry at the beginning(suggested to locate at
212 * the beginning). Or else the default free function
215 h->cb_remove(h, entry);
221 /********************* Cache list ************************/
223 static struct mlx5_cache_entry *
224 mlx5_clist_default_create_cb(struct mlx5_cache_list *list,
225 struct mlx5_cache_entry *entry __rte_unused,
226 void *ctx __rte_unused)
228 return mlx5_malloc(MLX5_MEM_ZERO, list->entry_sz, 0, SOCKET_ID_ANY);
232 mlx5_clist_default_remove_cb(struct mlx5_cache_list *list __rte_unused,
233 struct mlx5_cache_entry *entry)
239 mlx5_cache_list_init(struct mlx5_cache_list *list, const char *name,
240 uint32_t entry_size, void *ctx,
241 mlx5_cache_create_cb cb_create,
242 mlx5_cache_match_cb cb_match,
243 mlx5_cache_remove_cb cb_remove)
246 if (!cb_match || (!cb_create ^ !cb_remove))
249 snprintf(list->name, sizeof(list->name), "%s", name);
250 list->entry_sz = entry_size;
252 list->cb_create = cb_create ? cb_create : mlx5_clist_default_create_cb;
253 list->cb_match = cb_match;
254 list->cb_remove = cb_remove ? cb_remove : mlx5_clist_default_remove_cb;
255 rte_rwlock_init(&list->lock);
256 DRV_LOG(DEBUG, "Cache list %s initialized.", list->name);
257 LIST_INIT(&list->head);
261 static struct mlx5_cache_entry *
262 __cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
264 struct mlx5_cache_entry *entry;
266 LIST_FOREACH(entry, &list->head, next) {
267 if (list->cb_match(list, entry, ctx))
270 __atomic_add_fetch(&entry->ref_cnt, 1,
272 DRV_LOG(DEBUG, "Cache list %s entry %p ref++: %u.",
273 list->name, (void *)entry, entry->ref_cnt);
280 static struct mlx5_cache_entry *
281 cache_lookup(struct mlx5_cache_list *list, void *ctx, bool reuse)
283 struct mlx5_cache_entry *entry;
285 rte_rwlock_read_lock(&list->lock);
286 entry = __cache_lookup(list, ctx, reuse);
287 rte_rwlock_read_unlock(&list->lock);
291 struct mlx5_cache_entry *
292 mlx5_cache_lookup(struct mlx5_cache_list *list, void *ctx)
294 return cache_lookup(list, ctx, false);
297 struct mlx5_cache_entry *
298 mlx5_cache_register(struct mlx5_cache_list *list, void *ctx)
300 struct mlx5_cache_entry *entry;
301 uint32_t prev_gen_cnt = 0;
304 prev_gen_cnt = __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE);
305 /* Lookup with read lock, reuse if found. */
306 entry = cache_lookup(list, ctx, true);
309 /* Not found, append with write lock - block read from other threads. */
310 rte_rwlock_write_lock(&list->lock);
311 /* If list changed by other threads before lock, search again. */
312 if (prev_gen_cnt != __atomic_load_n(&list->gen_cnt, __ATOMIC_ACQUIRE)) {
313 /* Lookup and reuse w/o read lock. */
314 entry = __cache_lookup(list, ctx, true);
318 entry = list->cb_create(list, entry, ctx);
320 DRV_LOG(ERR, "Failed to init cache list %s entry %p.",
321 list->name, (void *)entry);
325 LIST_INSERT_HEAD(&list->head, entry, next);
326 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_RELEASE);
327 __atomic_add_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
328 DRV_LOG(DEBUG, "Cache list %s entry %p new: %u.",
329 list->name, (void *)entry, entry->ref_cnt);
331 rte_rwlock_write_unlock(&list->lock);
336 mlx5_cache_unregister(struct mlx5_cache_list *list,
337 struct mlx5_cache_entry *entry)
339 rte_rwlock_write_lock(&list->lock);
340 MLX5_ASSERT(entry && entry->next.le_prev);
341 DRV_LOG(DEBUG, "Cache list %s entry %p ref--: %u.",
342 list->name, (void *)entry, entry->ref_cnt);
343 if (--entry->ref_cnt) {
344 rte_rwlock_write_unlock(&list->lock);
347 __atomic_add_fetch(&list->gen_cnt, 1, __ATOMIC_ACQUIRE);
348 __atomic_sub_fetch(&list->count, 1, __ATOMIC_ACQUIRE);
349 LIST_REMOVE(entry, next);
350 list->cb_remove(list, entry);
351 rte_rwlock_write_unlock(&list->lock);
352 DRV_LOG(DEBUG, "Cache list %s entry %p removed.",
353 list->name, (void *)entry);
358 mlx5_cache_list_destroy(struct mlx5_cache_list *list)
360 struct mlx5_cache_entry *entry;
363 /* no LIST_FOREACH_SAFE, using while instead */
364 while (!LIST_EMPTY(&list->head)) {
365 entry = LIST_FIRST(&list->head);
366 LIST_REMOVE(entry, next);
367 list->cb_remove(list, entry);
368 DRV_LOG(DEBUG, "Cache list %s entry %p destroyed.",
369 list->name, (void *)entry);
371 memset(list, 0, sizeof(*list));
375 mlx5_cache_list_get_entry_num(struct mlx5_cache_list *list)
378 return __atomic_load_n(&list->count, __ATOMIC_RELAXED);
381 /********************* Indexed pool **********************/
384 mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
386 if (pool->cfg.need_lock)
387 rte_spinlock_lock(&pool->lock);
391 mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
393 if (pool->cfg.need_lock)
394 rte_spinlock_unlock(&pool->lock);
397 static inline uint32_t
398 mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
400 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
401 uint32_t trunk_idx = 0;
404 if (!cfg->grow_trunk)
405 return entry_idx / cfg->trunk_size;
406 if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
407 trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
408 (cfg->trunk_size << (cfg->grow_shift *
409 cfg->grow_trunk)) + cfg->grow_trunk;
411 for (i = 0; i < cfg->grow_trunk; i++) {
412 if (entry_idx < pool->grow_tbl[i])
420 static inline uint32_t
421 mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
423 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
425 return cfg->trunk_size << (cfg->grow_shift *
426 (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
429 static inline uint32_t
430 mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
432 struct mlx5_indexed_pool_config *cfg = &pool->cfg;
437 if (!cfg->grow_trunk)
438 return cfg->trunk_size * trunk_idx;
439 if (trunk_idx < cfg->grow_trunk)
440 offset = pool->grow_tbl[trunk_idx - 1];
442 offset = pool->grow_tbl[cfg->grow_trunk - 1] +
443 (cfg->trunk_size << (cfg->grow_shift *
444 cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
448 struct mlx5_indexed_pool *
449 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
451 struct mlx5_indexed_pool *pool;
454 if (!cfg || (!cfg->malloc ^ !cfg->free) ||
455 (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
456 ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
458 pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
459 sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
464 if (!pool->cfg.trunk_size)
465 pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
466 if (!cfg->malloc && !cfg->free) {
467 pool->cfg.malloc = mlx5_malloc;
468 pool->cfg.free = mlx5_free;
470 pool->free_list = TRUNK_INVALID;
471 if (pool->cfg.need_lock)
472 rte_spinlock_init(&pool->lock);
474 * Initialize the dynamic grow trunk size lookup table to have a quick
475 * lookup for the trunk entry index offset.
477 for (i = 0; i < cfg->grow_trunk; i++) {
478 pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
480 pool->grow_tbl[i] += pool->grow_tbl[i - 1];
486 mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
488 struct mlx5_indexed_trunk *trunk;
489 struct mlx5_indexed_trunk **trunk_tmp;
490 struct mlx5_indexed_trunk **p;
491 size_t trunk_size = 0;
496 if (pool->n_trunk_valid == TRUNK_MAX_IDX)
498 if (pool->n_trunk_valid == pool->n_trunk) {
499 /* No free trunk flags, expand trunk list. */
500 int n_grow = pool->n_trunk_valid ? pool->n_trunk :
501 RTE_CACHE_LINE_SIZE / sizeof(void *);
503 p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
504 sizeof(struct mlx5_indexed_trunk *),
505 RTE_CACHE_LINE_SIZE, rte_socket_id());
509 memcpy(p, pool->trunks, pool->n_trunk_valid *
510 sizeof(struct mlx5_indexed_trunk *));
511 memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
512 n_grow * sizeof(void *));
513 trunk_tmp = pool->trunks;
516 pool->cfg.free(trunk_tmp);
517 pool->n_trunk += n_grow;
519 if (!pool->cfg.release_mem_en) {
520 idx = pool->n_trunk_valid;
522 /* Find the first available slot in trunk list */
523 for (idx = 0; idx < pool->n_trunk; idx++)
524 if (pool->trunks[idx] == NULL)
527 trunk_size += sizeof(*trunk);
528 data_size = mlx5_trunk_size_get(pool, idx);
529 bmp_size = rte_bitmap_get_memory_footprint(data_size);
530 /* rte_bitmap requires memory cacheline aligned. */
531 trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
532 trunk_size += bmp_size;
533 trunk = pool->cfg.malloc(0, trunk_size,
534 RTE_CACHE_LINE_SIZE, rte_socket_id());
537 pool->trunks[idx] = trunk;
539 trunk->free = data_size;
540 trunk->prev = TRUNK_INVALID;
541 trunk->next = TRUNK_INVALID;
542 MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
543 pool->free_list = idx;
544 /* Mark all entries as available. */
545 trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
546 [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
548 MLX5_ASSERT(trunk->bmp);
549 pool->n_trunk_valid++;
558 mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
560 struct mlx5_indexed_trunk *trunk;
565 mlx5_ipool_lock(pool);
566 if (pool->free_list == TRUNK_INVALID) {
567 /* If no available trunks, grow new. */
568 if (mlx5_ipool_grow(pool)) {
569 mlx5_ipool_unlock(pool);
573 MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
574 trunk = pool->trunks[pool->free_list];
575 MLX5_ASSERT(trunk->free);
576 if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
577 mlx5_ipool_unlock(pool);
581 iidx += __builtin_ctzll(slab);
582 MLX5_ASSERT(iidx != UINT32_MAX);
583 MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
584 rte_bitmap_clear(trunk->bmp, iidx);
585 p = &trunk->data[iidx * pool->cfg.size];
587 * The ipool index should grow continually from small to big,
588 * some features as metering only accept limited bits of index.
589 * Random index with MSB set may be rejected.
591 iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
592 iidx += 1; /* non-zero index. */
598 /* Full trunk will be removed from free list in imalloc. */
599 MLX5_ASSERT(pool->free_list == trunk->idx);
600 pool->free_list = trunk->next;
601 if (trunk->next != TRUNK_INVALID)
602 pool->trunks[trunk->next]->prev = TRUNK_INVALID;
603 trunk->prev = TRUNK_INVALID;
604 trunk->next = TRUNK_INVALID;
611 mlx5_ipool_unlock(pool);
616 mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
618 void *entry = mlx5_ipool_malloc(pool, idx);
620 if (entry && pool->cfg.size)
621 memset(entry, 0, pool->cfg.size);
626 mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
628 struct mlx5_indexed_trunk *trunk;
635 mlx5_ipool_lock(pool);
636 trunk_idx = mlx5_trunk_idx_get(pool, idx);
637 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
638 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
640 trunk = pool->trunks[trunk_idx];
643 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
644 if (trunk_idx != trunk->idx ||
645 rte_bitmap_get(trunk->bmp, entry_idx))
647 rte_bitmap_set(trunk->bmp, entry_idx);
649 if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
650 (pool, trunk->idx)) {
651 if (pool->free_list == trunk->idx)
652 pool->free_list = trunk->next;
653 if (trunk->next != TRUNK_INVALID)
654 pool->trunks[trunk->next]->prev = trunk->prev;
655 if (trunk->prev != TRUNK_INVALID)
656 pool->trunks[trunk->prev]->next = trunk->next;
657 pool->cfg.free(trunk);
658 pool->trunks[trunk_idx] = NULL;
659 pool->n_trunk_valid--;
664 if (pool->n_trunk_valid == 0) {
665 pool->cfg.free(pool->trunks);
669 } else if (trunk->free == 1) {
670 /* Put into free trunk list head. */
671 MLX5_ASSERT(pool->free_list != trunk->idx);
672 trunk->next = pool->free_list;
673 trunk->prev = TRUNK_INVALID;
674 if (pool->free_list != TRUNK_INVALID)
675 pool->trunks[pool->free_list]->prev = trunk->idx;
676 pool->free_list = trunk->idx;
686 mlx5_ipool_unlock(pool);
690 mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
692 struct mlx5_indexed_trunk *trunk;
700 mlx5_ipool_lock(pool);
701 trunk_idx = mlx5_trunk_idx_get(pool, idx);
702 if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
703 (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
705 trunk = pool->trunks[trunk_idx];
708 entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
709 if (trunk_idx != trunk->idx ||
710 rte_bitmap_get(trunk->bmp, entry_idx))
712 p = &trunk->data[entry_idx * pool->cfg.size];
714 mlx5_ipool_unlock(pool);
719 mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
721 struct mlx5_indexed_trunk **trunks;
725 mlx5_ipool_lock(pool);
726 trunks = pool->trunks;
727 for (i = 0; i < pool->n_trunk; i++) {
729 pool->cfg.free(trunks[i]);
732 pool->cfg.free(pool->trunks);
733 mlx5_ipool_unlock(pool);
739 mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
741 printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
743 pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
744 pool->cfg.trunk_size, pool->n_trunk_valid);
746 printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
747 "available %u free %u\n",
748 pool->cfg.type, pool->n_entry, pool->trunk_new,
749 pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
753 struct mlx5_l3t_tbl *
754 mlx5_l3t_create(enum mlx5_l3t_type type)
756 struct mlx5_l3t_tbl *tbl;
757 struct mlx5_indexed_pool_config l3t_ip_cfg = {
763 .malloc = mlx5_malloc,
767 if (type >= MLX5_L3T_TYPE_MAX) {
771 tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
779 case MLX5_L3T_TYPE_WORD:
780 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
781 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
783 case MLX5_L3T_TYPE_DWORD:
784 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
785 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
787 case MLX5_L3T_TYPE_QWORD:
788 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
789 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
792 l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
793 l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
796 rte_spinlock_init(&tbl->sl);
797 tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
807 mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
809 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
816 for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
817 m_tbl = g_tbl->tbl[i];
820 for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
823 MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
824 m_tbl->tbl[j])->ref_cnt);
825 mlx5_ipool_free(tbl->eip,
826 ((struct mlx5_l3t_entry_word *)
827 m_tbl->tbl[j])->idx);
829 if (!(--m_tbl->ref_cnt))
832 MLX5_ASSERT(!m_tbl->ref_cnt);
833 mlx5_free(g_tbl->tbl[i]);
835 if (!(--g_tbl->ref_cnt))
838 MLX5_ASSERT(!g_tbl->ref_cnt);
842 mlx5_ipool_destroy(tbl->eip);
847 __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
848 union mlx5_l3t_data *data)
850 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
851 struct mlx5_l3t_entry_word *w_e_tbl;
852 struct mlx5_l3t_entry_dword *dw_e_tbl;
853 struct mlx5_l3t_entry_qword *qw_e_tbl;
854 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
861 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
864 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
867 entry_idx = idx & MLX5_L3T_ET_MASK;
869 case MLX5_L3T_TYPE_WORD:
870 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
871 data->word = w_e_tbl->entry[entry_idx].data;
872 if (w_e_tbl->entry[entry_idx].data)
873 w_e_tbl->entry[entry_idx].ref_cnt++;
875 case MLX5_L3T_TYPE_DWORD:
876 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
877 data->dword = dw_e_tbl->entry[entry_idx].data;
878 if (dw_e_tbl->entry[entry_idx].data)
879 dw_e_tbl->entry[entry_idx].ref_cnt++;
881 case MLX5_L3T_TYPE_QWORD:
882 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
883 data->qword = qw_e_tbl->entry[entry_idx].data;
884 if (qw_e_tbl->entry[entry_idx].data)
885 qw_e_tbl->entry[entry_idx].ref_cnt++;
888 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
889 data->ptr = ptr_e_tbl->entry[entry_idx].data;
890 if (ptr_e_tbl->entry[entry_idx].data)
891 ptr_e_tbl->entry[entry_idx].ref_cnt++;
898 mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
899 union mlx5_l3t_data *data)
903 rte_spinlock_lock(&tbl->sl);
904 ret = __l3t_get_entry(tbl, idx, data);
905 rte_spinlock_unlock(&tbl->sl);
910 mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
912 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
913 struct mlx5_l3t_entry_word *w_e_tbl;
914 struct mlx5_l3t_entry_dword *dw_e_tbl;
915 struct mlx5_l3t_entry_qword *qw_e_tbl;
916 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
922 rte_spinlock_lock(&tbl->sl);
926 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
929 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
932 entry_idx = idx & MLX5_L3T_ET_MASK;
934 case MLX5_L3T_TYPE_WORD:
935 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
936 MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
937 ret = --w_e_tbl->entry[entry_idx].ref_cnt;
940 w_e_tbl->entry[entry_idx].data = 0;
941 ref_cnt = --w_e_tbl->ref_cnt;
943 case MLX5_L3T_TYPE_DWORD:
944 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
945 MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
946 ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
949 dw_e_tbl->entry[entry_idx].data = 0;
950 ref_cnt = --dw_e_tbl->ref_cnt;
952 case MLX5_L3T_TYPE_QWORD:
953 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
954 MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
955 ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
958 qw_e_tbl->entry[entry_idx].data = 0;
959 ref_cnt = --qw_e_tbl->ref_cnt;
962 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
963 MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
964 ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
967 ptr_e_tbl->entry[entry_idx].data = NULL;
968 ref_cnt = --ptr_e_tbl->ref_cnt;
972 mlx5_ipool_free(tbl->eip,
973 ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
974 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
976 if (!(--m_tbl->ref_cnt)) {
979 [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
980 if (!(--g_tbl->ref_cnt)) {
987 rte_spinlock_unlock(&tbl->sl);
992 __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
993 union mlx5_l3t_data *data)
995 struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
996 struct mlx5_l3t_entry_word *w_e_tbl;
997 struct mlx5_l3t_entry_dword *dw_e_tbl;
998 struct mlx5_l3t_entry_qword *qw_e_tbl;
999 struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1001 uint32_t entry_idx, tbl_idx = 0;
1003 /* Check the global table, create it if empty. */
1006 g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1007 sizeof(struct mlx5_l3t_level_tbl) +
1008 sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1017 * Check the middle table, create it if empty. Ref_cnt will be
1018 * increased if new sub table created.
1020 m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1022 m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1023 sizeof(struct mlx5_l3t_level_tbl) +
1024 sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1030 g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1035 * Check the entry table, create it if empty. Ref_cnt will be
1036 * increased if new sub entry table created.
1038 e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1040 e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1045 ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1046 m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1050 entry_idx = idx & MLX5_L3T_ET_MASK;
1051 switch (tbl->type) {
1052 case MLX5_L3T_TYPE_WORD:
1053 w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1054 if (w_e_tbl->entry[entry_idx].data) {
1055 data->word = w_e_tbl->entry[entry_idx].data;
1056 w_e_tbl->entry[entry_idx].ref_cnt++;
1060 w_e_tbl->entry[entry_idx].data = data->word;
1061 w_e_tbl->entry[entry_idx].ref_cnt = 1;
1064 case MLX5_L3T_TYPE_DWORD:
1065 dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1066 if (dw_e_tbl->entry[entry_idx].data) {
1067 data->dword = dw_e_tbl->entry[entry_idx].data;
1068 dw_e_tbl->entry[entry_idx].ref_cnt++;
1072 dw_e_tbl->entry[entry_idx].data = data->dword;
1073 dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1074 dw_e_tbl->ref_cnt++;
1076 case MLX5_L3T_TYPE_QWORD:
1077 qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1078 if (qw_e_tbl->entry[entry_idx].data) {
1079 data->qword = qw_e_tbl->entry[entry_idx].data;
1080 qw_e_tbl->entry[entry_idx].ref_cnt++;
1084 qw_e_tbl->entry[entry_idx].data = data->qword;
1085 qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1086 qw_e_tbl->ref_cnt++;
1089 ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1090 if (ptr_e_tbl->entry[entry_idx].data) {
1091 data->ptr = ptr_e_tbl->entry[entry_idx].data;
1092 ptr_e_tbl->entry[entry_idx].ref_cnt++;
1096 ptr_e_tbl->entry[entry_idx].data = data->ptr;
1097 ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1098 ptr_e_tbl->ref_cnt++;
1105 mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1106 union mlx5_l3t_data *data)
1110 rte_spinlock_lock(&tbl->sl);
1111 ret = __l3t_set_entry(tbl, idx, data);
1112 rte_spinlock_unlock(&tbl->sl);
1117 mlx5_l3t_prepare_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1118 union mlx5_l3t_data *data,
1119 mlx5_l3t_alloc_callback_fn cb, void *ctx)
1123 rte_spinlock_lock(&tbl->sl);
1124 /* Check if entry data is ready. */
1125 ret = __l3t_get_entry(tbl, idx, data);
1127 switch (tbl->type) {
1128 case MLX5_L3T_TYPE_WORD:
1132 case MLX5_L3T_TYPE_DWORD:
1136 case MLX5_L3T_TYPE_QWORD:
1146 /* Entry data is not ready, use user callback to create it. */
1147 ret = cb(ctx, data);
1150 /* Save the new allocated data to entry. */
1151 ret = __l3t_set_entry(tbl, idx, data);
1153 rte_spinlock_unlock(&tbl->sl);