Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2019 Mellanox Technologies, Ltd
3 : : */
4 : :
5 : : #include <rte_malloc.h>
6 : :
7 : : #include <mlx5_malloc.h>
8 : :
9 : : #include "mlx5_utils.h"
10 : :
11 : : /********************* Indexed pool **********************/
12 : :
13 : : static inline void
14 : : mlx5_ipool_lock(struct mlx5_indexed_pool *pool)
15 : : {
16 [ # # # # : 0 : if (pool->cfg.need_lock)
# # ]
17 : 0 : rte_spinlock_lock(&pool->rsz_lock);
18 : : }
19 : :
20 : : static inline void
21 : : mlx5_ipool_unlock(struct mlx5_indexed_pool *pool)
22 : : {
23 [ # # # # : 0 : if (pool->cfg.need_lock)
# # # # #
# ]
24 : 0 : rte_spinlock_unlock(&pool->rsz_lock);
25 : : }
26 : :
27 : : static inline uint32_t
28 : 0 : mlx5_trunk_idx_get(struct mlx5_indexed_pool *pool, uint32_t entry_idx)
29 : : {
30 : : struct mlx5_indexed_pool_config *cfg = &pool->cfg;
31 : : uint32_t trunk_idx = 0;
32 : : uint32_t i;
33 : :
34 [ # # ]: 0 : if (!cfg->grow_trunk)
35 : 0 : return entry_idx / cfg->trunk_size;
36 [ # # ]: 0 : if (entry_idx >= pool->grow_tbl[cfg->grow_trunk - 1]) {
37 : 0 : trunk_idx = (entry_idx - pool->grow_tbl[cfg->grow_trunk - 1]) /
38 : 0 : (cfg->trunk_size << (cfg->grow_shift *
39 : 0 : cfg->grow_trunk)) + cfg->grow_trunk;
40 : : } else {
41 [ # # ]: 0 : for (i = 0; i < cfg->grow_trunk; i++) {
42 [ # # ]: 0 : if (entry_idx < pool->grow_tbl[i])
43 : : break;
44 : : }
45 : : trunk_idx = i;
46 : : }
47 : : return trunk_idx;
48 : : }
49 : :
50 : : static inline uint32_t
51 : : mlx5_trunk_size_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
52 : : {
53 : : struct mlx5_indexed_pool_config *cfg = &pool->cfg;
54 : :
55 : 0 : return cfg->trunk_size << (cfg->grow_shift *
56 : 0 : (trunk_idx > cfg->grow_trunk ? cfg->grow_trunk : trunk_idx));
57 : : }
58 : :
59 : : static inline uint32_t
60 : 0 : mlx5_trunk_idx_offset_get(struct mlx5_indexed_pool *pool, uint32_t trunk_idx)
61 : : {
62 : : struct mlx5_indexed_pool_config *cfg = &pool->cfg;
63 : : uint32_t offset = 0;
64 : :
65 [ # # ]: 0 : if (!trunk_idx)
66 : : return 0;
67 [ # # # # : 0 : if (!cfg->grow_trunk)
# # ]
68 : 0 : return cfg->trunk_size * trunk_idx;
69 [ # # ]: 0 : if (trunk_idx < cfg->grow_trunk)
70 : 0 : offset = pool->grow_tbl[trunk_idx - 1];
71 : : else
72 : 0 : offset = pool->grow_tbl[cfg->grow_trunk - 1] +
73 : 0 : (cfg->trunk_size << (cfg->grow_shift *
74 : 0 : cfg->grow_trunk)) * (trunk_idx - cfg->grow_trunk);
75 : : return offset;
76 : : }
77 : :
78 : : struct mlx5_indexed_pool *
79 : 0 : mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
80 : : {
81 : : struct mlx5_indexed_pool *pool;
82 : : uint32_t i;
83 : :
84 [ # # # # ]: 0 : if (!cfg || (!cfg->malloc ^ !cfg->free) ||
85 [ # # # # ]: 0 : (cfg->per_core_cache && cfg->release_mem_en) ||
86 [ # # # # : 0 : (cfg->trunk_size && ((cfg->trunk_size & (cfg->trunk_size - 1)) ||
# # ]
87 : : ((__builtin_ffs(cfg->trunk_size) + TRUNK_IDX_BITS) > 32))))
88 : : return NULL;
89 : 0 : pool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*pool) + cfg->grow_trunk *
90 : : sizeof(pool->grow_tbl[0]), RTE_CACHE_LINE_SIZE,
91 : : SOCKET_ID_ANY);
92 [ # # ]: 0 : if (!pool)
93 : : return NULL;
94 : 0 : pool->cfg = *cfg;
95 [ # # ]: 0 : if (!pool->cfg.trunk_size)
96 : 0 : pool->cfg.trunk_size = MLX5_IPOOL_DEFAULT_TRUNK_SIZE;
97 [ # # # # ]: 0 : if (!cfg->malloc && !cfg->free) {
98 : 0 : pool->cfg.malloc = mlx5_malloc;
99 : 0 : pool->cfg.free = mlx5_free;
100 : : }
101 [ # # ]: 0 : if (pool->cfg.need_lock)
102 : : rte_spinlock_init(&pool->rsz_lock);
103 : : /*
104 : : * Initialize the dynamic grow trunk size lookup table to have a quick
105 : : * lookup for the trunk entry index offset.
106 : : */
107 [ # # ]: 0 : for (i = 0; i < cfg->grow_trunk; i++) {
108 : 0 : pool->grow_tbl[i] = cfg->trunk_size << (cfg->grow_shift * i);
109 [ # # ]: 0 : if (i > 0)
110 : 0 : pool->grow_tbl[i] += pool->grow_tbl[i - 1];
111 : : }
112 [ # # ]: 0 : if (!pool->cfg.max_idx)
113 : 0 : pool->cfg.max_idx =
114 : : mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1);
115 [ # # ]: 0 : if (!cfg->per_core_cache)
116 : 0 : pool->free_list = TRUNK_INVALID;
117 : : rte_spinlock_init(&pool->lcore_lock);
118 : 0 : return pool;
119 : : }
120 : :
121 : : static int
122 : 0 : mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
123 : : {
124 : : struct mlx5_indexed_trunk *trunk;
125 : : struct mlx5_indexed_trunk **trunk_tmp;
126 : : struct mlx5_indexed_trunk **p;
127 : : size_t trunk_size = 0;
128 : : size_t data_size;
129 : : size_t bmp_size;
130 : : uint32_t idx, cur_max_idx, i;
131 : :
132 : 0 : cur_max_idx = mlx5_trunk_idx_offset_get(pool, pool->n_trunk_valid);
133 [ # # ]: 0 : if (pool->n_trunk_valid == TRUNK_MAX_IDX ||
134 [ # # ]: 0 : cur_max_idx >= pool->cfg.max_idx)
135 : : return -ENOMEM;
136 [ # # ]: 0 : if (pool->n_trunk_valid == pool->n_trunk) {
137 : : /* No free trunk flags, expand trunk list. */
138 [ # # ]: 0 : int n_grow = pool->n_trunk_valid ? pool->n_trunk :
139 : : RTE_CACHE_LINE_SIZE / sizeof(void *);
140 : :
141 : 0 : p = pool->cfg.malloc(0, (pool->n_trunk_valid + n_grow) *
142 : : sizeof(struct mlx5_indexed_trunk *),
143 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
144 [ # # ]: 0 : if (!p)
145 : : return -ENOMEM;
146 [ # # ]: 0 : if (pool->trunks)
147 : 0 : memcpy(p, pool->trunks, pool->n_trunk_valid *
148 : : sizeof(struct mlx5_indexed_trunk *));
149 [ # # ]: 0 : memset(RTE_PTR_ADD(p, pool->n_trunk_valid * sizeof(void *)), 0,
150 : : n_grow * sizeof(void *));
151 : 0 : trunk_tmp = pool->trunks;
152 : 0 : pool->trunks = p;
153 [ # # ]: 0 : if (trunk_tmp)
154 : 0 : pool->cfg.free(trunk_tmp);
155 : 0 : pool->n_trunk += n_grow;
156 : : }
157 [ # # ]: 0 : if (!pool->cfg.release_mem_en) {
158 : 0 : idx = pool->n_trunk_valid;
159 : : } else {
160 : : /* Find the first available slot in trunk list */
161 [ # # ]: 0 : for (idx = 0; idx < pool->n_trunk; idx++)
162 [ # # ]: 0 : if (pool->trunks[idx] == NULL)
163 : : break;
164 : : }
165 : : trunk_size += sizeof(*trunk);
166 : 0 : data_size = mlx5_trunk_size_get(pool, idx);
167 : 0 : bmp_size = rte_bitmap_get_memory_footprint(data_size);
168 : : /* rte_bitmap requires memory cacheline aligned. */
169 : 0 : trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
170 : 0 : trunk_size += bmp_size;
171 : 0 : trunk = pool->cfg.malloc(0, trunk_size,
172 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
173 [ # # ]: 0 : if (!trunk)
174 : : return -ENOMEM;
175 : 0 : pool->trunks[idx] = trunk;
176 : 0 : trunk->idx = idx;
177 : 0 : trunk->free = data_size;
178 : 0 : trunk->prev = TRUNK_INVALID;
179 : 0 : trunk->next = TRUNK_INVALID;
180 : : MLX5_ASSERT(pool->free_list == TRUNK_INVALID);
181 : 0 : pool->free_list = idx;
182 : : /* Mark all entries as available. */
183 : 0 : trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
184 : 0 : [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
185 : : bmp_size);
186 : : /* Clear the overhead bits in the trunk if it happens. */
187 [ # # ]: 0 : if (cur_max_idx + data_size > pool->cfg.max_idx) {
188 [ # # ]: 0 : for (i = pool->cfg.max_idx - cur_max_idx; i < data_size; i++)
189 : 0 : rte_bitmap_clear(trunk->bmp, i);
190 : : }
191 : : MLX5_ASSERT(trunk->bmp);
192 : 0 : pool->n_trunk_valid++;
193 : : #ifdef POOL_DEBUG
194 : : pool->trunk_new++;
195 : : pool->trunk_avail++;
196 : : #endif
197 : 0 : return 0;
198 : : }
199 : :
200 : : static inline struct mlx5_indexed_cache *
201 : 0 : mlx5_ipool_update_global_cache(struct mlx5_indexed_pool *pool, int cidx)
202 : : {
203 : : struct mlx5_indexed_cache *gc, *lc, *olc = NULL;
204 : :
205 : 0 : lc = pool->cache[cidx]->lc;
206 : 0 : gc = rte_atomic_load_explicit(&pool->gc, rte_memory_order_relaxed);
207 [ # # ]: 0 : if (gc && lc != gc) {
208 : : mlx5_ipool_lock(pool);
209 [ # # # # ]: 0 : if (lc && !(--lc->ref_cnt))
210 : : olc = lc;
211 : 0 : lc = pool->gc;
212 : 0 : lc->ref_cnt++;
213 [ # # ]: 0 : pool->cache[cidx]->lc = lc;
214 : : mlx5_ipool_unlock(pool);
215 [ # # ]: 0 : if (olc)
216 : 0 : pool->cfg.free(olc);
217 : : }
218 : 0 : return lc;
219 : : }
220 : :
221 : : static uint32_t
222 : 0 : mlx5_ipool_allocate_from_global(struct mlx5_indexed_pool *pool, int cidx)
223 : : {
224 : : struct mlx5_indexed_trunk *trunk;
225 : : struct mlx5_indexed_cache *p, *lc, *olc = NULL;
226 : : size_t trunk_size = 0;
227 : : size_t data_size;
228 : : uint32_t cur_max_idx, trunk_idx, trunk_n;
229 : : uint32_t fetch_size, ts_idx, i;
230 : : int n_grow;
231 : :
232 : 0 : check_again:
233 : : p = NULL;
234 : : fetch_size = 0;
235 : : /*
236 : : * Fetch new index from global if possible. First round local
237 : : * cache will be NULL.
238 : : */
239 [ # # ]: 0 : lc = pool->cache[cidx]->lc;
240 : : mlx5_ipool_lock(pool);
241 : : /* Try to update local cache first. */
242 [ # # ]: 0 : if (likely(pool->gc)) {
243 [ # # ]: 0 : if (lc != pool->gc) {
244 [ # # # # ]: 0 : if (lc && !(--lc->ref_cnt))
245 : : olc = lc;
246 : : lc = pool->gc;
247 : 0 : lc->ref_cnt++;
248 : 0 : pool->cache[cidx]->lc = lc;
249 : : }
250 [ # # ]: 0 : if (lc->len) {
251 : : /* Use the updated local cache to fetch index. */
252 : 0 : fetch_size = pool->cfg.per_core_cache >> 2;
253 : : if (lc->len < fetch_size)
254 : : fetch_size = lc->len;
255 : 0 : lc->len -= fetch_size;
256 : 0 : memcpy(pool->cache[cidx]->idx, &lc->idx[lc->len],
257 : : sizeof(uint32_t) * fetch_size);
258 : : }
259 : : }
260 : : mlx5_ipool_unlock(pool);
261 [ # # ]: 0 : if (unlikely(olc)) {
262 : 0 : pool->cfg.free(olc);
263 : : olc = NULL;
264 : : }
265 [ # # ]: 0 : if (fetch_size) {
266 : 0 : pool->cache[cidx]->len = fetch_size - 1;
267 : 0 : return pool->cache[cidx]->idx[pool->cache[cidx]->len];
268 : : }
269 : 0 : trunk_idx = lc ? rte_atomic_load_explicit(&lc->n_trunk_valid,
270 [ # # ]: 0 : rte_memory_order_acquire) : 0;
271 [ # # ]: 0 : trunk_n = lc ? lc->n_trunk : 0;
272 : 0 : cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx);
273 : : /* Check if index reach maximum. */
274 [ # # ]: 0 : if (trunk_idx == TRUNK_MAX_IDX ||
275 [ # # ]: 0 : cur_max_idx >= pool->cfg.max_idx)
276 : : return 0;
277 : : /* No enough space in trunk array, resize the trunks array. */
278 [ # # ]: 0 : if (trunk_idx == trunk_n) {
279 [ # # ]: 0 : n_grow = trunk_idx ? trunk_idx :
280 : : RTE_CACHE_LINE_SIZE / sizeof(void *);
281 : 0 : cur_max_idx = mlx5_trunk_idx_offset_get(pool, trunk_n + n_grow);
282 : : /* Resize the trunk array. */
283 : 0 : p = pool->cfg.malloc(0, ((trunk_idx + n_grow) *
284 : 0 : sizeof(struct mlx5_indexed_trunk *)) +
285 : 0 : (cur_max_idx * sizeof(uint32_t)) + sizeof(*p),
286 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
287 [ # # ]: 0 : if (!p)
288 : : return 0;
289 : 0 : p->trunks = (struct mlx5_indexed_trunk **)&p->idx[cur_max_idx];
290 [ # # ]: 0 : if (lc)
291 : 0 : memcpy(p->trunks, lc->trunks, trunk_idx *
292 : : sizeof(struct mlx5_indexed_trunk *));
293 : : #ifdef RTE_LIBRTE_MLX5_DEBUG
294 : : memset(RTE_PTR_ADD(p->trunks, trunk_idx * sizeof(void *)), 0,
295 : : n_grow * sizeof(void *));
296 : : #endif
297 : 0 : p->n_trunk_valid = trunk_idx;
298 : 0 : p->n_trunk = trunk_n + n_grow;
299 : 0 : p->len = 0;
300 : : }
301 : : /* Prepare the new trunk. */
302 : : trunk_size = sizeof(*trunk);
303 : 0 : data_size = mlx5_trunk_size_get(pool, trunk_idx);
304 : 0 : trunk_size += RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size);
305 : 0 : trunk = pool->cfg.malloc(0, trunk_size,
306 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
307 [ # # ]: 0 : if (unlikely(!trunk)) {
308 : 0 : pool->cfg.free(p);
309 : 0 : return 0;
310 : : }
311 : 0 : trunk->idx = trunk_idx;
312 [ # # ]: 0 : trunk->free = data_size;
313 : : mlx5_ipool_lock(pool);
314 : : /*
315 : : * Double check if trunks has been updated or have available index.
316 : : * During the new trunk allocate, index may still be flushed to the
317 : : * global cache. So also need to check the pool->gc->len.
318 : : */
319 [ # # # # ]: 0 : if (pool->gc && (lc != pool->gc ||
320 [ # # ]: 0 : lc->n_trunk_valid != trunk_idx ||
321 [ # # ]: 0 : pool->gc->len)) {
322 : : mlx5_ipool_unlock(pool);
323 [ # # ]: 0 : if (p)
324 : 0 : pool->cfg.free(p);
325 : 0 : pool->cfg.free(trunk);
326 : 0 : goto check_again;
327 : : }
328 : : /* Resize the trunk array and update local cache first. */
329 [ # # ]: 0 : if (p) {
330 [ # # # # ]: 0 : if (lc && !(--lc->ref_cnt))
331 : : olc = lc;
332 : : lc = p;
333 : 0 : lc->ref_cnt = 1;
334 : 0 : pool->cache[cidx]->lc = lc;
335 : 0 : rte_atomic_store_explicit(&pool->gc, p, rte_memory_order_relaxed);
336 : : }
337 : : /* Add trunk to trunks array. */
338 : 0 : lc->trunks[trunk_idx] = trunk;
339 : 0 : rte_atomic_fetch_add_explicit(&lc->n_trunk_valid, 1, rte_memory_order_relaxed);
340 : : /* Enqueue half of the index to global. */
341 : 0 : ts_idx = mlx5_trunk_idx_offset_get(pool, trunk_idx) + 1;
342 : 0 : fetch_size = trunk->free >> 1;
343 [ # # ]: 0 : if (fetch_size > pool->cfg.per_core_cache)
344 : 0 : fetch_size = trunk->free - pool->cfg.per_core_cache;
345 [ # # ]: 0 : for (i = 0; i < fetch_size; i++)
346 : 0 : lc->idx[i] = ts_idx + i;
347 [ # # ]: 0 : lc->len = fetch_size;
348 : : mlx5_ipool_unlock(pool);
349 : : /* Copy left half - 1 to local cache index array. */
350 : 0 : pool->cache[cidx]->len = trunk->free - fetch_size - 1;
351 : 0 : ts_idx += fetch_size;
352 [ # # ]: 0 : for (i = 0; i < pool->cache[cidx]->len; i++)
353 : 0 : pool->cache[cidx]->idx[i] = ts_idx + i;
354 [ # # ]: 0 : if (olc)
355 : 0 : pool->cfg.free(olc);
356 : 0 : return ts_idx + i;
357 : : }
358 : :
359 : : static void *
360 : 0 : _mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, int cidx, uint32_t idx)
361 : : {
362 : : struct mlx5_indexed_trunk *trunk;
363 : : struct mlx5_indexed_cache *lc;
364 : : uint32_t trunk_idx;
365 : : uint32_t entry_idx;
366 : :
367 : : MLX5_ASSERT(idx);
368 [ # # ]: 0 : if (unlikely(!pool->cache[cidx])) {
369 : 0 : pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
370 : 0 : sizeof(struct mlx5_ipool_per_lcore) +
371 : 0 : (pool->cfg.per_core_cache * sizeof(uint32_t)),
372 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
373 [ # # ]: 0 : if (!pool->cache[cidx]) {
374 : 0 : DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
375 : 0 : return NULL;
376 : : }
377 : : }
378 : 0 : lc = mlx5_ipool_update_global_cache(pool, cidx);
379 : 0 : idx -= 1;
380 : 0 : trunk_idx = mlx5_trunk_idx_get(pool, idx);
381 : 0 : trunk = lc->trunks[trunk_idx];
382 [ # # ]: 0 : if (!trunk)
383 : : return NULL;
384 : 0 : entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk_idx);
385 : 0 : return &trunk->data[entry_idx * pool->cfg.size];
386 : : }
387 : :
388 : : static void *
389 : 0 : mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
390 : : {
391 : : void *entry;
392 : : int cidx;
393 : :
394 : 0 : cidx = rte_lcore_index(rte_lcore_id());
395 [ # # ]: 0 : if (unlikely(cidx == -1)) {
396 : : cidx = RTE_MAX_LCORE;
397 : 0 : rte_spinlock_lock(&pool->lcore_lock);
398 : : }
399 : 0 : entry = _mlx5_ipool_get_cache(pool, cidx, idx);
400 [ # # ]: 0 : if (unlikely(cidx == RTE_MAX_LCORE))
401 : 0 : rte_spinlock_unlock(&pool->lcore_lock);
402 : 0 : return entry;
403 : : }
404 : :
405 : :
406 : : static void *
407 : 0 : _mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, int cidx,
408 : : uint32_t *idx)
409 : : {
410 [ # # ]: 0 : if (unlikely(!pool->cache[cidx])) {
411 : 0 : pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
412 : 0 : sizeof(struct mlx5_ipool_per_lcore) +
413 : 0 : (pool->cfg.per_core_cache * sizeof(uint32_t)),
414 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
415 [ # # ]: 0 : if (!pool->cache[cidx]) {
416 : 0 : DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
417 : 0 : return NULL;
418 : : }
419 [ # # ]: 0 : } else if (pool->cache[cidx]->len) {
420 : 0 : pool->cache[cidx]->len--;
421 : 0 : *idx = pool->cache[cidx]->idx[pool->cache[cidx]->len];
422 : 0 : return _mlx5_ipool_get_cache(pool, cidx, *idx);
423 : : }
424 : : /* Not enough idx in global cache. Keep fetching from global. */
425 : 0 : *idx = mlx5_ipool_allocate_from_global(pool, cidx);
426 [ # # ]: 0 : if (unlikely(!(*idx)))
427 : : return NULL;
428 : 0 : return _mlx5_ipool_get_cache(pool, cidx, *idx);
429 : : }
430 : :
431 : : static void *
432 : 0 : mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, uint32_t *idx)
433 : : {
434 : : void *entry;
435 : : int cidx;
436 : :
437 : 0 : cidx = rte_lcore_index(rte_lcore_id());
438 [ # # ]: 0 : if (unlikely(cidx == -1)) {
439 : : cidx = RTE_MAX_LCORE;
440 : 0 : rte_spinlock_lock(&pool->lcore_lock);
441 : : }
442 : 0 : entry = _mlx5_ipool_malloc_cache(pool, cidx, idx);
443 [ # # ]: 0 : if (unlikely(cidx == RTE_MAX_LCORE))
444 : 0 : rte_spinlock_unlock(&pool->lcore_lock);
445 : 0 : return entry;
446 : : }
447 : :
448 : : static void
449 : 0 : _mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, int cidx, uint32_t idx)
450 : : {
451 : : struct mlx5_ipool_per_lcore *ilc;
452 : : struct mlx5_indexed_cache *gc, *olc = NULL;
453 : : uint32_t reclaim_num = 0;
454 : :
455 : : MLX5_ASSERT(idx);
456 : : /*
457 : : * When index was allocated on core A but freed on core B. In this
458 : : * case check if local cache on core B was allocated before.
459 : : */
460 [ # # ]: 0 : if (unlikely(!pool->cache[cidx])) {
461 : 0 : pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
462 : 0 : sizeof(struct mlx5_ipool_per_lcore) +
463 : 0 : (pool->cfg.per_core_cache * sizeof(uint32_t)),
464 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
465 [ # # ]: 0 : if (!pool->cache[cidx]) {
466 : 0 : DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
467 : 0 : return;
468 : : }
469 : : }
470 : : /* Try to enqueue to local index cache. */
471 [ # # ]: 0 : if (pool->cache[cidx]->len < pool->cfg.per_core_cache) {
472 : 0 : pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
473 : 0 : pool->cache[cidx]->len++;
474 : 0 : return;
475 : : }
476 : : ilc = pool->cache[cidx];
477 : 0 : reclaim_num = pool->cfg.per_core_cache >> 2;
478 [ # # ]: 0 : ilc->len -= reclaim_num;
479 : : /* Local index cache full, try with global index cache. */
480 : : mlx5_ipool_lock(pool);
481 : 0 : gc = pool->gc;
482 [ # # ]: 0 : if (ilc->lc != gc) {
483 [ # # # # ]: 0 : if (ilc->lc && !(--ilc->lc->ref_cnt))
484 : : olc = ilc->lc;
485 : 0 : gc->ref_cnt++;
486 : 0 : ilc->lc = gc;
487 : : }
488 [ # # ]: 0 : memcpy(&gc->idx[gc->len], &ilc->idx[ilc->len],
489 : : reclaim_num * sizeof(uint32_t));
490 [ # # ]: 0 : gc->len += reclaim_num;
491 : : mlx5_ipool_unlock(pool);
492 [ # # ]: 0 : if (olc)
493 : 0 : pool->cfg.free(olc);
494 : 0 : pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
495 : 0 : pool->cache[cidx]->len++;
496 : : }
497 : :
498 : : static void
499 : 0 : mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
500 : : {
501 : : int cidx;
502 : :
503 : 0 : cidx = rte_lcore_index(rte_lcore_id());
504 [ # # ]: 0 : if (unlikely(cidx == -1)) {
505 : : cidx = RTE_MAX_LCORE;
506 : 0 : rte_spinlock_lock(&pool->lcore_lock);
507 : : }
508 : 0 : _mlx5_ipool_free_cache(pool, cidx, idx);
509 [ # # ]: 0 : if (unlikely(cidx == RTE_MAX_LCORE))
510 : 0 : rte_spinlock_unlock(&pool->lcore_lock);
511 : 0 : }
512 : :
513 : : void *
514 : 0 : mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
515 : : {
516 : : struct mlx5_indexed_trunk *trunk;
517 : 0 : uint64_t slab = 0;
518 : 0 : uint32_t iidx = 0;
519 : : void *p;
520 : :
521 [ # # ]: 0 : if (pool->cfg.per_core_cache)
522 : 0 : return mlx5_ipool_malloc_cache(pool, idx);
523 : : mlx5_ipool_lock(pool);
524 [ # # ]: 0 : if (pool->free_list == TRUNK_INVALID) {
525 : : /* If no available trunks, grow new. */
526 [ # # ]: 0 : if (mlx5_ipool_grow(pool)) {
527 : : mlx5_ipool_unlock(pool);
528 : 0 : return NULL;
529 : : }
530 : : }
531 : : MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
532 : 0 : trunk = pool->trunks[pool->free_list];
533 : : MLX5_ASSERT(trunk->free);
534 [ # # ]: 0 : if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
535 : : mlx5_ipool_unlock(pool);
536 : 0 : return NULL;
537 : : }
538 : : MLX5_ASSERT(slab);
539 : 0 : iidx += rte_ctz64(slab);
540 : : MLX5_ASSERT(iidx != UINT32_MAX);
541 : : MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
542 : 0 : rte_bitmap_clear(trunk->bmp, iidx);
543 : 0 : p = &trunk->data[iidx * pool->cfg.size];
544 : : /*
545 : : * The ipool index should grow continually from small to big,
546 : : * some features as metering only accept limited bits of index.
547 : : * Random index with MSB set may be rejected.
548 : : */
549 : 0 : iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
550 : 0 : iidx += 1; /* non-zero index. */
551 : 0 : trunk->free--;
552 : : #ifdef POOL_DEBUG
553 : : pool->n_entry++;
554 : : #endif
555 [ # # ]: 0 : if (!trunk->free) {
556 : : /* Full trunk will be removed from free list in imalloc. */
557 : : MLX5_ASSERT(pool->free_list == trunk->idx);
558 : 0 : pool->free_list = trunk->next;
559 [ # # ]: 0 : if (trunk->next != TRUNK_INVALID)
560 : 0 : pool->trunks[trunk->next]->prev = TRUNK_INVALID;
561 : 0 : trunk->prev = TRUNK_INVALID;
562 : 0 : trunk->next = TRUNK_INVALID;
563 : : #ifdef POOL_DEBUG
564 : : pool->trunk_empty++;
565 : : pool->trunk_avail--;
566 : : #endif
567 : : }
568 [ # # ]: 0 : *idx = iidx;
569 : : mlx5_ipool_unlock(pool);
570 : : return p;
571 : : }
572 : :
573 : : void *
574 : 0 : mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
575 : : {
576 : 0 : void *entry = mlx5_ipool_malloc(pool, idx);
577 : :
578 [ # # # # ]: 0 : if (entry && pool->cfg.size)
579 : 0 : memset(entry, 0, pool->cfg.size);
580 : 0 : return entry;
581 : : }
582 : :
583 : : void
584 : 0 : mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
585 : : {
586 : : struct mlx5_indexed_trunk *trunk;
587 : : uint32_t trunk_idx;
588 : : uint32_t entry_idx;
589 : :
590 [ # # ]: 0 : if (!idx)
591 : : return;
592 [ # # ]: 0 : if (pool->cfg.per_core_cache) {
593 : 0 : mlx5_ipool_free_cache(pool, idx);
594 : 0 : return;
595 : : }
596 [ # # ]: 0 : idx -= 1;
597 : : mlx5_ipool_lock(pool);
598 : 0 : trunk_idx = mlx5_trunk_idx_get(pool, idx);
599 [ # # # # : 0 : if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
# # ]
600 [ # # ]: 0 : (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
601 : 0 : goto out;
602 : 0 : trunk = pool->trunks[trunk_idx];
603 [ # # ]: 0 : if (!trunk)
604 : 0 : goto out;
605 : 0 : entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
606 [ # # # # ]: 0 : if (trunk_idx != trunk->idx ||
607 [ # # ]: 0 : rte_bitmap_get(trunk->bmp, entry_idx))
608 : 0 : goto out;
609 : 0 : rte_bitmap_set(trunk->bmp, entry_idx);
610 : 0 : trunk->free++;
611 [ # # # # ]: 0 : if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
612 : : (pool, trunk->idx)) {
613 [ # # ]: 0 : if (pool->free_list == trunk->idx)
614 : 0 : pool->free_list = trunk->next;
615 [ # # ]: 0 : if (trunk->next != TRUNK_INVALID)
616 : 0 : pool->trunks[trunk->next]->prev = trunk->prev;
617 [ # # ]: 0 : if (trunk->prev != TRUNK_INVALID)
618 : 0 : pool->trunks[trunk->prev]->next = trunk->next;
619 : 0 : pool->cfg.free(trunk);
620 : 0 : pool->trunks[trunk_idx] = NULL;
621 : 0 : pool->n_trunk_valid--;
622 : : #ifdef POOL_DEBUG
623 : : pool->trunk_avail--;
624 : : pool->trunk_free++;
625 : : #endif
626 [ # # ]: 0 : if (pool->n_trunk_valid == 0) {
627 : 0 : pool->cfg.free(pool->trunks);
628 : 0 : pool->trunks = NULL;
629 : 0 : pool->n_trunk = 0;
630 : : }
631 [ # # ]: 0 : } else if (trunk->free == 1) {
632 : : /* Put into free trunk list head. */
633 : : MLX5_ASSERT(pool->free_list != trunk->idx);
634 : 0 : trunk->next = pool->free_list;
635 : 0 : trunk->prev = TRUNK_INVALID;
636 [ # # ]: 0 : if (pool->free_list != TRUNK_INVALID)
637 : 0 : pool->trunks[pool->free_list]->prev = trunk->idx;
638 : 0 : pool->free_list = trunk->idx;
639 : : #ifdef POOL_DEBUG
640 : : pool->trunk_empty--;
641 : : pool->trunk_avail++;
642 : : #endif
643 : : }
644 : : #ifdef POOL_DEBUG
645 : : pool->n_entry--;
646 : : #endif
647 [ # # ]: 0 : out:
648 : : mlx5_ipool_unlock(pool);
649 : : }
650 : :
651 : : void *
652 : 0 : mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
653 : : {
654 : : struct mlx5_indexed_trunk *trunk;
655 : : void *p = NULL;
656 : : uint32_t trunk_idx;
657 : : uint32_t entry_idx;
658 : :
659 [ # # ]: 0 : if (!idx)
660 : : return NULL;
661 [ # # ]: 0 : if (pool->cfg.per_core_cache)
662 : 0 : return mlx5_ipool_get_cache(pool, idx);
663 [ # # ]: 0 : idx -= 1;
664 : : mlx5_ipool_lock(pool);
665 : 0 : trunk_idx = mlx5_trunk_idx_get(pool, idx);
666 [ # # # # : 0 : if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
# # ]
667 [ # # ]: 0 : (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
668 : 0 : goto out;
669 : 0 : trunk = pool->trunks[trunk_idx];
670 [ # # ]: 0 : if (!trunk)
671 : 0 : goto out;
672 : 0 : entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
673 [ # # # # ]: 0 : if (trunk_idx != trunk->idx ||
674 [ # # ]: 0 : rte_bitmap_get(trunk->bmp, entry_idx))
675 : 0 : goto out;
676 : 0 : p = &trunk->data[entry_idx * pool->cfg.size];
677 [ # # ]: 0 : out:
678 : : mlx5_ipool_unlock(pool);
679 : : return p;
680 : : }
681 : :
682 : : int
683 : 0 : mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
684 : : {
685 : : struct mlx5_indexed_trunk **trunks = NULL;
686 [ # # ]: 0 : struct mlx5_indexed_cache *gc = pool->gc;
687 : : uint32_t i, n_trunk_valid = 0;
688 : :
689 : : MLX5_ASSERT(pool);
690 : : mlx5_ipool_lock(pool);
691 [ # # ]: 0 : if (pool->cfg.per_core_cache) {
692 [ # # ]: 0 : for (i = 0; i <= RTE_MAX_LCORE; i++) {
693 : : /*
694 : : * Free only old global cache. Pool gc will be
695 : : * freed at last.
696 : : */
697 [ # # ]: 0 : if (pool->cache[i]) {
698 [ # # ]: 0 : if (pool->cache[i]->lc &&
699 [ # # ]: 0 : pool->cache[i]->lc != pool->gc &&
700 [ # # ]: 0 : (!(--pool->cache[i]->lc->ref_cnt)))
701 : 0 : pool->cfg.free(pool->cache[i]->lc);
702 : 0 : pool->cfg.free(pool->cache[i]);
703 : : }
704 : : }
705 [ # # ]: 0 : if (gc) {
706 : 0 : trunks = gc->trunks;
707 : 0 : n_trunk_valid = gc->n_trunk_valid;
708 : : }
709 : : } else {
710 : : gc = NULL;
711 : 0 : trunks = pool->trunks;
712 : 0 : n_trunk_valid = pool->n_trunk_valid;
713 : : }
714 [ # # ]: 0 : for (i = 0; i < n_trunk_valid; i++) {
715 [ # # ]: 0 : if (trunks[i])
716 : 0 : pool->cfg.free(trunks[i]);
717 : : }
718 [ # # ]: 0 : if (!gc && trunks)
719 : 0 : pool->cfg.free(trunks);
720 [ # # ]: 0 : if (gc)
721 : 0 : pool->cfg.free(gc);
722 : : mlx5_ipool_unlock(pool);
723 : 0 : mlx5_free(pool);
724 : 0 : return 0;
725 : : }
726 : :
727 : : void
728 : 0 : mlx5_ipool_flush_cache(struct mlx5_indexed_pool *pool)
729 : : {
730 : : uint32_t i, j;
731 : : struct mlx5_indexed_cache *gc;
732 : : struct rte_bitmap *ibmp;
733 : : uint32_t bmp_num, mem_size;
734 : :
735 [ # # ]: 0 : if (!pool->cfg.per_core_cache)
736 : : return;
737 : 0 : gc = pool->gc;
738 [ # # ]: 0 : if (!gc)
739 : : return;
740 : : /* Reset bmp. */
741 : 0 : bmp_num = mlx5_trunk_idx_offset_get(pool, gc->n_trunk_valid);
742 : 0 : mem_size = rte_bitmap_get_memory_footprint(bmp_num);
743 : 0 : pool->bmp_mem = pool->cfg.malloc(MLX5_MEM_ZERO, mem_size,
744 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
745 [ # # ]: 0 : if (!pool->bmp_mem) {
746 : 0 : DRV_LOG(ERR, "Ipool bitmap mem allocate failed.\n");
747 : 0 : return;
748 : : }
749 : 0 : ibmp = rte_bitmap_init_with_all_set(bmp_num, pool->bmp_mem, mem_size);
750 [ # # ]: 0 : if (!ibmp) {
751 : 0 : pool->cfg.free(pool->bmp_mem);
752 : 0 : pool->bmp_mem = NULL;
753 : 0 : DRV_LOG(ERR, "Ipool bitmap create failed.\n");
754 : 0 : return;
755 : : }
756 : 0 : pool->ibmp = ibmp;
757 : : /* Clear global cache. */
758 [ # # ]: 0 : for (i = 0; i < gc->len; i++)
759 : 0 : rte_bitmap_clear(ibmp, gc->idx[i] - 1);
760 : : /* Clear core cache. */
761 [ # # ]: 0 : for (i = 0; i < RTE_MAX_LCORE + 1; i++) {
762 : 0 : struct mlx5_ipool_per_lcore *ilc = pool->cache[i];
763 : :
764 [ # # ]: 0 : if (!ilc)
765 : 0 : continue;
766 [ # # ]: 0 : for (j = 0; j < ilc->len; j++)
767 : 0 : rte_bitmap_clear(ibmp, ilc->idx[j] - 1);
768 : : }
769 : : }
770 : :
771 : : static void *
772 : 0 : mlx5_ipool_get_next_cache(struct mlx5_indexed_pool *pool, uint32_t *pos)
773 : : {
774 : : struct rte_bitmap *ibmp;
775 : 0 : uint64_t slab = 0;
776 : 0 : uint32_t iidx = *pos;
777 : :
778 : 0 : ibmp = pool->ibmp;
779 [ # # # # ]: 0 : if (!ibmp || !rte_bitmap_scan(ibmp, &iidx, &slab)) {
780 [ # # ]: 0 : if (pool->bmp_mem) {
781 : 0 : pool->cfg.free(pool->bmp_mem);
782 : 0 : pool->bmp_mem = NULL;
783 : 0 : pool->ibmp = NULL;
784 : : }
785 : 0 : return NULL;
786 : : }
787 : 0 : iidx += rte_ctz64(slab);
788 : 0 : rte_bitmap_clear(ibmp, iidx);
789 : 0 : iidx++;
790 : 0 : *pos = iidx;
791 : 0 : return mlx5_ipool_get_cache(pool, iidx);
792 : : }
793 : :
794 : : void *
795 : 0 : mlx5_ipool_get_next(struct mlx5_indexed_pool *pool, uint32_t *pos)
796 : : {
797 : 0 : uint32_t idx = *pos;
798 : : void *entry;
799 : :
800 [ # # ]: 0 : if (pool->cfg.per_core_cache)
801 : 0 : return mlx5_ipool_get_next_cache(pool, pos);
802 [ # # ]: 0 : while (idx <= mlx5_trunk_idx_offset_get(pool, pool->n_trunk)) {
803 : 0 : entry = mlx5_ipool_get(pool, idx);
804 [ # # ]: 0 : if (entry) {
805 : 0 : *pos = idx;
806 : 0 : return entry;
807 : : }
808 : 0 : idx++;
809 : : }
810 : : return NULL;
811 : : }
812 : :
813 : : int
814 : 0 : mlx5_ipool_resize(struct mlx5_indexed_pool *pool, uint32_t num_entries,
815 : : struct rte_flow_error *error)
816 : : {
817 [ # # ]: 0 : if (num_entries == pool->cfg.max_idx)
818 : : return 0;
819 [ # # ]: 0 : else if (num_entries < pool->cfg.max_idx)
820 : 0 : return rte_flow_error_set(error, EINVAL,
821 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
822 : : NULL, "cannot decrease pool size");
823 [ # # ]: 0 : if (num_entries % pool->cfg.trunk_size)
824 : 0 : return rte_flow_error_set(error, EINVAL,
825 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
826 : : NULL, "number of entries in pool must be trunk size multiplication");
827 [ # # ]: 0 : if (num_entries >= mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1))
828 : 0 : return rte_flow_error_set(error, EINVAL,
829 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
830 : : NULL, "requested number of entries exceeds pool limit");
831 : : mlx5_ipool_lock(pool);
832 [ # # ]: 0 : pool->cfg.max_idx = num_entries;
833 : : mlx5_ipool_unlock(pool);
834 : : return 0;
835 : : }
836 : :
837 : : void
838 : 0 : mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
839 : : {
840 : 0 : printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
841 : : "total: %d\n",
842 : : pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
843 : 0 : pool->cfg.trunk_size, pool->n_trunk_valid);
844 : : #ifdef POOL_DEBUG
845 : : printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
846 : : "available %u free %u\n",
847 : : pool->cfg.type, pool->n_entry, pool->trunk_new,
848 : : pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
849 : : #endif
850 : 0 : }
851 : :
852 : : struct mlx5_l3t_tbl *
853 : 0 : mlx5_l3t_create(enum mlx5_l3t_type type)
854 : : {
855 : : struct mlx5_l3t_tbl *tbl;
856 : 0 : struct mlx5_indexed_pool_config l3t_ip_cfg = {
857 : : .trunk_size = 16,
858 : : .grow_trunk = 6,
859 : : .grow_shift = 1,
860 : : .need_lock = 0,
861 : : .release_mem_en = 1,
862 : : .malloc = mlx5_malloc,
863 : : .free = mlx5_free,
864 : : };
865 : :
866 [ # # ]: 0 : if (type >= MLX5_L3T_TYPE_MAX) {
867 : 0 : rte_errno = EINVAL;
868 : 0 : return NULL;
869 : : }
870 : 0 : tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
871 : : SOCKET_ID_ANY);
872 [ # # ]: 0 : if (!tbl) {
873 : 0 : rte_errno = ENOMEM;
874 : 0 : return NULL;
875 : : }
876 : 0 : tbl->type = type;
877 [ # # # # ]: 0 : switch (type) {
878 : 0 : case MLX5_L3T_TYPE_WORD:
879 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
880 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
881 : 0 : break;
882 : 0 : case MLX5_L3T_TYPE_DWORD:
883 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
884 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
885 : 0 : break;
886 : 0 : case MLX5_L3T_TYPE_QWORD:
887 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
888 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
889 : 0 : break;
890 : 0 : default:
891 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
892 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
893 : 0 : break;
894 : : }
895 : : rte_spinlock_init(&tbl->sl);
896 : 0 : tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
897 [ # # ]: 0 : if (!tbl->eip) {
898 : 0 : rte_errno = ENOMEM;
899 : 0 : mlx5_free(tbl);
900 : : tbl = NULL;
901 : : }
902 : : return tbl;
903 : : }
904 : :
905 : : void
906 : 0 : mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
907 : : {
908 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
909 : : uint32_t i, j;
910 : :
911 [ # # ]: 0 : if (!tbl)
912 : : return;
913 : 0 : g_tbl = tbl->tbl;
914 [ # # ]: 0 : if (g_tbl) {
915 [ # # ]: 0 : for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
916 : 0 : m_tbl = g_tbl->tbl[i];
917 [ # # ]: 0 : if (!m_tbl)
918 : 0 : continue;
919 [ # # ]: 0 : for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
920 [ # # ]: 0 : if (!m_tbl->tbl[j])
921 : 0 : continue;
922 : : MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
923 : : m_tbl->tbl[j])->ref_cnt);
924 : 0 : mlx5_ipool_free(tbl->eip,
925 : : ((struct mlx5_l3t_entry_word *)
926 : : m_tbl->tbl[j])->idx);
927 : 0 : m_tbl->tbl[j] = 0;
928 [ # # ]: 0 : if (!(--m_tbl->ref_cnt))
929 : : break;
930 : : }
931 : : MLX5_ASSERT(!m_tbl->ref_cnt);
932 : 0 : mlx5_free(g_tbl->tbl[i]);
933 : 0 : g_tbl->tbl[i] = 0;
934 [ # # ]: 0 : if (!(--g_tbl->ref_cnt))
935 : : break;
936 : : }
937 : : MLX5_ASSERT(!g_tbl->ref_cnt);
938 : 0 : mlx5_free(tbl->tbl);
939 : 0 : tbl->tbl = 0;
940 : : }
941 : 0 : mlx5_ipool_destroy(tbl->eip);
942 : 0 : mlx5_free(tbl);
943 : : }
944 : :
945 : : static int32_t
946 : 0 : __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
947 : : union mlx5_l3t_data *data)
948 : : {
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;
954 : : void *e_tbl;
955 : : uint32_t entry_idx;
956 : :
957 : 0 : g_tbl = tbl->tbl;
958 [ # # ]: 0 : if (!g_tbl)
959 : : return -1;
960 : 0 : m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
961 [ # # ]: 0 : if (!m_tbl)
962 : : return -1;
963 : 0 : e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
964 [ # # ]: 0 : if (!e_tbl)
965 : : return -1;
966 : 0 : entry_idx = idx & MLX5_L3T_ET_MASK;
967 [ # # # # ]: 0 : switch (tbl->type) {
968 : 0 : case MLX5_L3T_TYPE_WORD:
969 : : w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
970 : 0 : data->word = w_e_tbl->entry[entry_idx].data;
971 [ # # ]: 0 : if (w_e_tbl->entry[entry_idx].data)
972 : 0 : w_e_tbl->entry[entry_idx].ref_cnt++;
973 : : break;
974 : 0 : case MLX5_L3T_TYPE_DWORD:
975 : : dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
976 : 0 : data->dword = dw_e_tbl->entry[entry_idx].data;
977 [ # # ]: 0 : if (dw_e_tbl->entry[entry_idx].data)
978 : 0 : dw_e_tbl->entry[entry_idx].ref_cnt++;
979 : : break;
980 : 0 : case MLX5_L3T_TYPE_QWORD:
981 : : qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
982 : 0 : data->qword = qw_e_tbl->entry[entry_idx].data;
983 [ # # ]: 0 : if (qw_e_tbl->entry[entry_idx].data)
984 : 0 : qw_e_tbl->entry[entry_idx].ref_cnt++;
985 : : break;
986 : 0 : default:
987 : : ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
988 : 0 : data->ptr = ptr_e_tbl->entry[entry_idx].data;
989 [ # # ]: 0 : if (ptr_e_tbl->entry[entry_idx].data)
990 : 0 : ptr_e_tbl->entry[entry_idx].ref_cnt++;
991 : : break;
992 : : }
993 : : return 0;
994 : : }
995 : :
996 : : int32_t
997 : 0 : mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
998 : : union mlx5_l3t_data *data)
999 : : {
1000 : : int ret;
1001 : :
1002 : 0 : rte_spinlock_lock(&tbl->sl);
1003 : 0 : ret = __l3t_get_entry(tbl, idx, data);
1004 : : rte_spinlock_unlock(&tbl->sl);
1005 : 0 : return ret;
1006 : : }
1007 : :
1008 : : int32_t
1009 : 0 : mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
1010 : : {
1011 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1012 : : struct mlx5_l3t_entry_word *w_e_tbl;
1013 : : struct mlx5_l3t_entry_dword *dw_e_tbl;
1014 : : struct mlx5_l3t_entry_qword *qw_e_tbl;
1015 : : struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1016 : : void *e_tbl;
1017 : : uint32_t entry_idx;
1018 : : uint64_t ref_cnt;
1019 : : int32_t ret = -1;
1020 : :
1021 : 0 : rte_spinlock_lock(&tbl->sl);
1022 : 0 : g_tbl = tbl->tbl;
1023 [ # # ]: 0 : if (!g_tbl)
1024 : 0 : goto out;
1025 : 0 : m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1026 [ # # ]: 0 : if (!m_tbl)
1027 : 0 : goto out;
1028 : 0 : e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1029 [ # # ]: 0 : if (!e_tbl)
1030 : 0 : goto out;
1031 : 0 : entry_idx = idx & MLX5_L3T_ET_MASK;
1032 [ # # # # ]: 0 : switch (tbl->type) {
1033 : 0 : case MLX5_L3T_TYPE_WORD:
1034 : : w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1035 : : MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
1036 : 0 : ret = --w_e_tbl->entry[entry_idx].ref_cnt;
1037 [ # # ]: 0 : if (ret)
1038 : 0 : goto out;
1039 : 0 : w_e_tbl->entry[entry_idx].data = 0;
1040 : 0 : ref_cnt = --w_e_tbl->ref_cnt;
1041 : 0 : break;
1042 : 0 : case MLX5_L3T_TYPE_DWORD:
1043 : : dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1044 : : MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
1045 : 0 : ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
1046 [ # # ]: 0 : if (ret)
1047 : 0 : goto out;
1048 : 0 : dw_e_tbl->entry[entry_idx].data = 0;
1049 : 0 : ref_cnt = --dw_e_tbl->ref_cnt;
1050 : 0 : break;
1051 : 0 : case MLX5_L3T_TYPE_QWORD:
1052 : : qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1053 : : MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
1054 : 0 : ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
1055 [ # # ]: 0 : if (ret)
1056 : 0 : goto out;
1057 : 0 : qw_e_tbl->entry[entry_idx].data = 0;
1058 : 0 : ref_cnt = --qw_e_tbl->ref_cnt;
1059 : 0 : break;
1060 : 0 : default:
1061 : : ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1062 : : MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
1063 : 0 : ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
1064 [ # # ]: 0 : if (ret)
1065 : 0 : goto out;
1066 : 0 : ptr_e_tbl->entry[entry_idx].data = NULL;
1067 : 0 : ref_cnt = --ptr_e_tbl->ref_cnt;
1068 : 0 : break;
1069 : : }
1070 [ # # ]: 0 : if (!ref_cnt) {
1071 : 0 : mlx5_ipool_free(tbl->eip,
1072 : : ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
1073 : 0 : m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1074 : : NULL;
1075 [ # # ]: 0 : if (!(--m_tbl->ref_cnt)) {
1076 : 0 : mlx5_free(m_tbl);
1077 : : g_tbl->tbl
1078 : 0 : [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
1079 [ # # ]: 0 : if (!(--g_tbl->ref_cnt)) {
1080 : 0 : mlx5_free(g_tbl);
1081 : 0 : tbl->tbl = 0;
1082 : : }
1083 : : }
1084 : : }
1085 : 0 : out:
1086 : : rte_spinlock_unlock(&tbl->sl);
1087 : 0 : return ret;
1088 : : }
1089 : :
1090 : : static int32_t
1091 : 0 : __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1092 : : union mlx5_l3t_data *data)
1093 : : {
1094 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1095 : : struct mlx5_l3t_entry_word *w_e_tbl;
1096 : : struct mlx5_l3t_entry_dword *dw_e_tbl;
1097 : : struct mlx5_l3t_entry_qword *qw_e_tbl;
1098 : : struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1099 : : void *e_tbl;
1100 : 0 : uint32_t entry_idx, tbl_idx = 0;
1101 : :
1102 : : /* Check the global table, create it if empty. */
1103 : 0 : g_tbl = tbl->tbl;
1104 [ # # ]: 0 : if (!g_tbl) {
1105 : 0 : g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1106 : : sizeof(struct mlx5_l3t_level_tbl) +
1107 : : sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1108 : : SOCKET_ID_ANY);
1109 [ # # ]: 0 : if (!g_tbl) {
1110 : 0 : rte_errno = ENOMEM;
1111 : 0 : return -1;
1112 : : }
1113 : 0 : tbl->tbl = g_tbl;
1114 : : }
1115 : : /*
1116 : : * Check the middle table, create it if empty. Ref_cnt will be
1117 : : * increased if new sub table created.
1118 : : */
1119 : 0 : m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1120 [ # # ]: 0 : if (!m_tbl) {
1121 : 0 : m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1122 : : sizeof(struct mlx5_l3t_level_tbl) +
1123 : : sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1124 : : SOCKET_ID_ANY);
1125 [ # # ]: 0 : if (!m_tbl) {
1126 : 0 : rte_errno = ENOMEM;
1127 : 0 : return -1;
1128 : : }
1129 : 0 : g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1130 : : m_tbl;
1131 : 0 : g_tbl->ref_cnt++;
1132 : : }
1133 : : /*
1134 : : * Check the entry table, create it if empty. Ref_cnt will be
1135 : : * increased if new sub entry table created.
1136 : : */
1137 : 0 : e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1138 [ # # ]: 0 : if (!e_tbl) {
1139 : 0 : e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1140 [ # # ]: 0 : if (!e_tbl) {
1141 : 0 : rte_errno = ENOMEM;
1142 : 0 : return -1;
1143 : : }
1144 : 0 : ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1145 : 0 : m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1146 : : e_tbl;
1147 : 0 : m_tbl->ref_cnt++;
1148 : : }
1149 : 0 : entry_idx = idx & MLX5_L3T_ET_MASK;
1150 [ # # # # ]: 0 : switch (tbl->type) {
1151 : 0 : case MLX5_L3T_TYPE_WORD:
1152 : : w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1153 [ # # ]: 0 : if (w_e_tbl->entry[entry_idx].data) {
1154 : 0 : data->word = w_e_tbl->entry[entry_idx].data;
1155 : 0 : w_e_tbl->entry[entry_idx].ref_cnt++;
1156 : 0 : rte_errno = EEXIST;
1157 : 0 : return -1;
1158 : : }
1159 : 0 : w_e_tbl->entry[entry_idx].data = data->word;
1160 : 0 : w_e_tbl->entry[entry_idx].ref_cnt = 1;
1161 : 0 : w_e_tbl->ref_cnt++;
1162 : 0 : break;
1163 : 0 : case MLX5_L3T_TYPE_DWORD:
1164 : : dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1165 [ # # ]: 0 : if (dw_e_tbl->entry[entry_idx].data) {
1166 : 0 : data->dword = dw_e_tbl->entry[entry_idx].data;
1167 : 0 : dw_e_tbl->entry[entry_idx].ref_cnt++;
1168 : 0 : rte_errno = EEXIST;
1169 : 0 : return -1;
1170 : : }
1171 : 0 : dw_e_tbl->entry[entry_idx].data = data->dword;
1172 : 0 : dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1173 : 0 : dw_e_tbl->ref_cnt++;
1174 : 0 : break;
1175 : 0 : case MLX5_L3T_TYPE_QWORD:
1176 : : qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1177 [ # # ]: 0 : if (qw_e_tbl->entry[entry_idx].data) {
1178 : 0 : data->qword = qw_e_tbl->entry[entry_idx].data;
1179 : 0 : qw_e_tbl->entry[entry_idx].ref_cnt++;
1180 : 0 : rte_errno = EEXIST;
1181 : 0 : return -1;
1182 : : }
1183 : 0 : qw_e_tbl->entry[entry_idx].data = data->qword;
1184 : 0 : qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1185 : 0 : qw_e_tbl->ref_cnt++;
1186 : 0 : break;
1187 : 0 : default:
1188 : : ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1189 [ # # ]: 0 : if (ptr_e_tbl->entry[entry_idx].data) {
1190 : 0 : data->ptr = ptr_e_tbl->entry[entry_idx].data;
1191 : 0 : ptr_e_tbl->entry[entry_idx].ref_cnt++;
1192 : 0 : rte_errno = EEXIST;
1193 : 0 : return -1;
1194 : : }
1195 : 0 : ptr_e_tbl->entry[entry_idx].data = data->ptr;
1196 : 0 : ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1197 : 0 : ptr_e_tbl->ref_cnt++;
1198 : 0 : break;
1199 : : }
1200 : : return 0;
1201 : : }
1202 : :
1203 : : int32_t
1204 : 0 : mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1205 : : union mlx5_l3t_data *data)
1206 : : {
1207 : : int ret;
1208 : :
1209 : 0 : rte_spinlock_lock(&tbl->sl);
1210 : 0 : ret = __l3t_set_entry(tbl, idx, data);
1211 : : rte_spinlock_unlock(&tbl->sl);
1212 : 0 : return ret;
1213 : : }
|