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 = __atomic_load_n(&pool->gc, __ATOMIC_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 ? __atomic_load_n(&lc->n_trunk_valid,
270 [ # # ]: 0 : __ATOMIC_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 : __atomic_store_n(&pool->gc, p, __ATOMIC_RELAXED);
336 : : }
337 : : /* Add trunk to trunks array. */
338 : 0 : lc->trunks[trunk_idx] = trunk;
339 : 0 : __atomic_fetch_add(&lc->n_trunk_valid, 1, __ATOMIC_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 : : MLX5_ASSERT(trunk);
383 : 0 : entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk_idx);
384 : 0 : return &trunk->data[entry_idx * pool->cfg.size];
385 : : }
386 : :
387 : : static void *
388 : 0 : mlx5_ipool_get_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
389 : : {
390 : : void *entry;
391 : : int cidx;
392 : :
393 : 0 : cidx = rte_lcore_index(rte_lcore_id());
394 [ # # ]: 0 : if (unlikely(cidx == -1)) {
395 : : cidx = RTE_MAX_LCORE;
396 : 0 : rte_spinlock_lock(&pool->lcore_lock);
397 : : }
398 : 0 : entry = _mlx5_ipool_get_cache(pool, cidx, idx);
399 [ # # ]: 0 : if (unlikely(cidx == RTE_MAX_LCORE))
400 : 0 : rte_spinlock_unlock(&pool->lcore_lock);
401 : 0 : return entry;
402 : : }
403 : :
404 : :
405 : : static void *
406 : 0 : _mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, int cidx,
407 : : uint32_t *idx)
408 : : {
409 [ # # ]: 0 : if (unlikely(!pool->cache[cidx])) {
410 : 0 : pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
411 : 0 : sizeof(struct mlx5_ipool_per_lcore) +
412 : 0 : (pool->cfg.per_core_cache * sizeof(uint32_t)),
413 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
414 [ # # ]: 0 : if (!pool->cache[cidx]) {
415 : 0 : DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
416 : 0 : return NULL;
417 : : }
418 [ # # ]: 0 : } else if (pool->cache[cidx]->len) {
419 : 0 : pool->cache[cidx]->len--;
420 : 0 : *idx = pool->cache[cidx]->idx[pool->cache[cidx]->len];
421 : 0 : return _mlx5_ipool_get_cache(pool, cidx, *idx);
422 : : }
423 : : /* Not enough idx in global cache. Keep fetching from global. */
424 : 0 : *idx = mlx5_ipool_allocate_from_global(pool, cidx);
425 [ # # ]: 0 : if (unlikely(!(*idx)))
426 : : return NULL;
427 : 0 : return _mlx5_ipool_get_cache(pool, cidx, *idx);
428 : : }
429 : :
430 : : static void *
431 : 0 : mlx5_ipool_malloc_cache(struct mlx5_indexed_pool *pool, uint32_t *idx)
432 : : {
433 : : void *entry;
434 : : int cidx;
435 : :
436 : 0 : cidx = rte_lcore_index(rte_lcore_id());
437 [ # # ]: 0 : if (unlikely(cidx == -1)) {
438 : : cidx = RTE_MAX_LCORE;
439 : 0 : rte_spinlock_lock(&pool->lcore_lock);
440 : : }
441 : 0 : entry = _mlx5_ipool_malloc_cache(pool, cidx, idx);
442 [ # # ]: 0 : if (unlikely(cidx == RTE_MAX_LCORE))
443 : 0 : rte_spinlock_unlock(&pool->lcore_lock);
444 : 0 : return entry;
445 : : }
446 : :
447 : : static void
448 : 0 : _mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, int cidx, uint32_t idx)
449 : : {
450 : : struct mlx5_ipool_per_lcore *ilc;
451 : : struct mlx5_indexed_cache *gc, *olc = NULL;
452 : : uint32_t reclaim_num = 0;
453 : :
454 : : MLX5_ASSERT(idx);
455 : : /*
456 : : * When index was allocated on core A but freed on core B. In this
457 : : * case check if local cache on core B was allocated before.
458 : : */
459 [ # # ]: 0 : if (unlikely(!pool->cache[cidx])) {
460 : 0 : pool->cache[cidx] = pool->cfg.malloc(MLX5_MEM_ZERO,
461 : 0 : sizeof(struct mlx5_ipool_per_lcore) +
462 : 0 : (pool->cfg.per_core_cache * sizeof(uint32_t)),
463 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
464 [ # # ]: 0 : if (!pool->cache[cidx]) {
465 : 0 : DRV_LOG(ERR, "Ipool cache%d allocate failed\n", cidx);
466 : 0 : return;
467 : : }
468 : : }
469 : : /* Try to enqueue to local index cache. */
470 [ # # ]: 0 : if (pool->cache[cidx]->len < pool->cfg.per_core_cache) {
471 : 0 : pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
472 : 0 : pool->cache[cidx]->len++;
473 : 0 : return;
474 : : }
475 : : ilc = pool->cache[cidx];
476 : 0 : reclaim_num = pool->cfg.per_core_cache >> 2;
477 [ # # ]: 0 : ilc->len -= reclaim_num;
478 : : /* Local index cache full, try with global index cache. */
479 : : mlx5_ipool_lock(pool);
480 : 0 : gc = pool->gc;
481 [ # # ]: 0 : if (ilc->lc != gc) {
482 [ # # # # ]: 0 : if (ilc->lc && !(--ilc->lc->ref_cnt))
483 : : olc = ilc->lc;
484 : 0 : gc->ref_cnt++;
485 : 0 : ilc->lc = gc;
486 : : }
487 [ # # ]: 0 : memcpy(&gc->idx[gc->len], &ilc->idx[ilc->len],
488 : : reclaim_num * sizeof(uint32_t));
489 [ # # ]: 0 : gc->len += reclaim_num;
490 : : mlx5_ipool_unlock(pool);
491 [ # # ]: 0 : if (olc)
492 : 0 : pool->cfg.free(olc);
493 : 0 : pool->cache[cidx]->idx[pool->cache[cidx]->len] = idx;
494 : 0 : pool->cache[cidx]->len++;
495 : : }
496 : :
497 : : static void
498 : 0 : mlx5_ipool_free_cache(struct mlx5_indexed_pool *pool, uint32_t idx)
499 : : {
500 : : int cidx;
501 : :
502 : 0 : cidx = rte_lcore_index(rte_lcore_id());
503 [ # # ]: 0 : if (unlikely(cidx == -1)) {
504 : : cidx = RTE_MAX_LCORE;
505 : 0 : rte_spinlock_lock(&pool->lcore_lock);
506 : : }
507 : 0 : _mlx5_ipool_free_cache(pool, cidx, idx);
508 [ # # ]: 0 : if (unlikely(cidx == RTE_MAX_LCORE))
509 : 0 : rte_spinlock_unlock(&pool->lcore_lock);
510 : 0 : }
511 : :
512 : : void *
513 : 0 : mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
514 : : {
515 : : struct mlx5_indexed_trunk *trunk;
516 : 0 : uint64_t slab = 0;
517 : 0 : uint32_t iidx = 0;
518 : : void *p;
519 : :
520 [ # # ]: 0 : if (pool->cfg.per_core_cache)
521 : 0 : return mlx5_ipool_malloc_cache(pool, idx);
522 : : mlx5_ipool_lock(pool);
523 [ # # ]: 0 : if (pool->free_list == TRUNK_INVALID) {
524 : : /* If no available trunks, grow new. */
525 [ # # ]: 0 : if (mlx5_ipool_grow(pool)) {
526 : : mlx5_ipool_unlock(pool);
527 : 0 : return NULL;
528 : : }
529 : : }
530 : : MLX5_ASSERT(pool->free_list != TRUNK_INVALID);
531 : 0 : trunk = pool->trunks[pool->free_list];
532 : : MLX5_ASSERT(trunk->free);
533 [ # # ]: 0 : if (!rte_bitmap_scan(trunk->bmp, &iidx, &slab)) {
534 : : mlx5_ipool_unlock(pool);
535 : 0 : return NULL;
536 : : }
537 : : MLX5_ASSERT(slab);
538 : 0 : iidx += rte_ctz64(slab);
539 : : MLX5_ASSERT(iidx != UINT32_MAX);
540 : : MLX5_ASSERT(iidx < mlx5_trunk_size_get(pool, trunk->idx));
541 : 0 : rte_bitmap_clear(trunk->bmp, iidx);
542 : 0 : p = &trunk->data[iidx * pool->cfg.size];
543 : : /*
544 : : * The ipool index should grow continually from small to big,
545 : : * some features as metering only accept limited bits of index.
546 : : * Random index with MSB set may be rejected.
547 : : */
548 : 0 : iidx += mlx5_trunk_idx_offset_get(pool, trunk->idx);
549 : 0 : iidx += 1; /* non-zero index. */
550 : 0 : trunk->free--;
551 : : #ifdef POOL_DEBUG
552 : : pool->n_entry++;
553 : : #endif
554 [ # # ]: 0 : if (!trunk->free) {
555 : : /* Full trunk will be removed from free list in imalloc. */
556 : : MLX5_ASSERT(pool->free_list == trunk->idx);
557 : 0 : pool->free_list = trunk->next;
558 [ # # ]: 0 : if (trunk->next != TRUNK_INVALID)
559 : 0 : pool->trunks[trunk->next]->prev = TRUNK_INVALID;
560 : 0 : trunk->prev = TRUNK_INVALID;
561 : 0 : trunk->next = TRUNK_INVALID;
562 : : #ifdef POOL_DEBUG
563 : : pool->trunk_empty++;
564 : : pool->trunk_avail--;
565 : : #endif
566 : : }
567 [ # # ]: 0 : *idx = iidx;
568 : : mlx5_ipool_unlock(pool);
569 : : return p;
570 : : }
571 : :
572 : : void *
573 : 0 : mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx)
574 : : {
575 : 0 : void *entry = mlx5_ipool_malloc(pool, idx);
576 : :
577 [ # # # # ]: 0 : if (entry && pool->cfg.size)
578 : 0 : memset(entry, 0, pool->cfg.size);
579 : 0 : return entry;
580 : : }
581 : :
582 : : void
583 : 0 : mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx)
584 : : {
585 : : struct mlx5_indexed_trunk *trunk;
586 : : uint32_t trunk_idx;
587 : : uint32_t entry_idx;
588 : :
589 [ # # ]: 0 : if (!idx)
590 : : return;
591 [ # # ]: 0 : if (pool->cfg.per_core_cache) {
592 : 0 : mlx5_ipool_free_cache(pool, idx);
593 : 0 : return;
594 : : }
595 [ # # ]: 0 : idx -= 1;
596 : : mlx5_ipool_lock(pool);
597 : 0 : trunk_idx = mlx5_trunk_idx_get(pool, idx);
598 [ # # # # : 0 : if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
# # ]
599 [ # # ]: 0 : (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
600 : 0 : goto out;
601 : 0 : trunk = pool->trunks[trunk_idx];
602 [ # # ]: 0 : if (!trunk)
603 : 0 : goto out;
604 : 0 : entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
605 [ # # # # ]: 0 : if (trunk_idx != trunk->idx ||
606 [ # # ]: 0 : rte_bitmap_get(trunk->bmp, entry_idx))
607 : 0 : goto out;
608 : 0 : rte_bitmap_set(trunk->bmp, entry_idx);
609 : 0 : trunk->free++;
610 [ # # # # ]: 0 : if (pool->cfg.release_mem_en && trunk->free == mlx5_trunk_size_get
611 : : (pool, trunk->idx)) {
612 [ # # ]: 0 : if (pool->free_list == trunk->idx)
613 : 0 : pool->free_list = trunk->next;
614 [ # # ]: 0 : if (trunk->next != TRUNK_INVALID)
615 : 0 : pool->trunks[trunk->next]->prev = trunk->prev;
616 [ # # ]: 0 : if (trunk->prev != TRUNK_INVALID)
617 : 0 : pool->trunks[trunk->prev]->next = trunk->next;
618 : 0 : pool->cfg.free(trunk);
619 : 0 : pool->trunks[trunk_idx] = NULL;
620 : 0 : pool->n_trunk_valid--;
621 : : #ifdef POOL_DEBUG
622 : : pool->trunk_avail--;
623 : : pool->trunk_free++;
624 : : #endif
625 [ # # ]: 0 : if (pool->n_trunk_valid == 0) {
626 : 0 : pool->cfg.free(pool->trunks);
627 : 0 : pool->trunks = NULL;
628 : 0 : pool->n_trunk = 0;
629 : : }
630 [ # # ]: 0 : } else if (trunk->free == 1) {
631 : : /* Put into free trunk list head. */
632 : : MLX5_ASSERT(pool->free_list != trunk->idx);
633 : 0 : trunk->next = pool->free_list;
634 : 0 : trunk->prev = TRUNK_INVALID;
635 [ # # ]: 0 : if (pool->free_list != TRUNK_INVALID)
636 : 0 : pool->trunks[pool->free_list]->prev = trunk->idx;
637 : 0 : pool->free_list = trunk->idx;
638 : : #ifdef POOL_DEBUG
639 : : pool->trunk_empty--;
640 : : pool->trunk_avail++;
641 : : #endif
642 : : }
643 : : #ifdef POOL_DEBUG
644 : : pool->n_entry--;
645 : : #endif
646 [ # # ]: 0 : out:
647 : : mlx5_ipool_unlock(pool);
648 : : }
649 : :
650 : : void *
651 : 0 : mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx)
652 : : {
653 : : struct mlx5_indexed_trunk *trunk;
654 : : void *p = NULL;
655 : : uint32_t trunk_idx;
656 : : uint32_t entry_idx;
657 : :
658 [ # # ]: 0 : if (!idx)
659 : : return NULL;
660 [ # # ]: 0 : if (pool->cfg.per_core_cache)
661 : 0 : return mlx5_ipool_get_cache(pool, idx);
662 [ # # ]: 0 : idx -= 1;
663 : : mlx5_ipool_lock(pool);
664 : 0 : trunk_idx = mlx5_trunk_idx_get(pool, idx);
665 [ # # # # : 0 : if ((!pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk_valid) ||
# # ]
666 [ # # ]: 0 : (pool->cfg.release_mem_en && trunk_idx >= pool->n_trunk))
667 : 0 : goto out;
668 : 0 : trunk = pool->trunks[trunk_idx];
669 [ # # ]: 0 : if (!trunk)
670 : 0 : goto out;
671 : 0 : entry_idx = idx - mlx5_trunk_idx_offset_get(pool, trunk->idx);
672 [ # # # # ]: 0 : if (trunk_idx != trunk->idx ||
673 [ # # ]: 0 : rte_bitmap_get(trunk->bmp, entry_idx))
674 : 0 : goto out;
675 : 0 : p = &trunk->data[entry_idx * pool->cfg.size];
676 [ # # ]: 0 : out:
677 : : mlx5_ipool_unlock(pool);
678 : : return p;
679 : : }
680 : :
681 : : int
682 : 0 : mlx5_ipool_destroy(struct mlx5_indexed_pool *pool)
683 : : {
684 : : struct mlx5_indexed_trunk **trunks = NULL;
685 [ # # ]: 0 : struct mlx5_indexed_cache *gc = pool->gc;
686 : : uint32_t i, n_trunk_valid = 0;
687 : :
688 : : MLX5_ASSERT(pool);
689 : : mlx5_ipool_lock(pool);
690 [ # # ]: 0 : if (pool->cfg.per_core_cache) {
691 [ # # ]: 0 : for (i = 0; i <= RTE_MAX_LCORE; i++) {
692 : : /*
693 : : * Free only old global cache. Pool gc will be
694 : : * freed at last.
695 : : */
696 [ # # ]: 0 : if (pool->cache[i]) {
697 [ # # ]: 0 : if (pool->cache[i]->lc &&
698 [ # # ]: 0 : pool->cache[i]->lc != pool->gc &&
699 [ # # ]: 0 : (!(--pool->cache[i]->lc->ref_cnt)))
700 : 0 : pool->cfg.free(pool->cache[i]->lc);
701 : 0 : pool->cfg.free(pool->cache[i]);
702 : : }
703 : : }
704 [ # # ]: 0 : if (gc) {
705 : 0 : trunks = gc->trunks;
706 : 0 : n_trunk_valid = gc->n_trunk_valid;
707 : : }
708 : : } else {
709 : : gc = NULL;
710 : 0 : trunks = pool->trunks;
711 : 0 : n_trunk_valid = pool->n_trunk_valid;
712 : : }
713 [ # # ]: 0 : for (i = 0; i < n_trunk_valid; i++) {
714 [ # # ]: 0 : if (trunks[i])
715 : 0 : pool->cfg.free(trunks[i]);
716 : : }
717 [ # # ]: 0 : if (!gc && trunks)
718 : 0 : pool->cfg.free(trunks);
719 [ # # ]: 0 : if (gc)
720 : 0 : pool->cfg.free(gc);
721 : : mlx5_ipool_unlock(pool);
722 : 0 : mlx5_free(pool);
723 : 0 : return 0;
724 : : }
725 : :
726 : : void
727 : 0 : mlx5_ipool_flush_cache(struct mlx5_indexed_pool *pool)
728 : : {
729 : : uint32_t i, j;
730 : : struct mlx5_indexed_cache *gc;
731 : : struct rte_bitmap *ibmp;
732 : : uint32_t bmp_num, mem_size;
733 : :
734 [ # # ]: 0 : if (!pool->cfg.per_core_cache)
735 : : return;
736 : 0 : gc = pool->gc;
737 [ # # ]: 0 : if (!gc)
738 : : return;
739 : : /* Reset bmp. */
740 : 0 : bmp_num = mlx5_trunk_idx_offset_get(pool, gc->n_trunk_valid);
741 : 0 : mem_size = rte_bitmap_get_memory_footprint(bmp_num);
742 : 0 : pool->bmp_mem = pool->cfg.malloc(MLX5_MEM_ZERO, mem_size,
743 : 0 : RTE_CACHE_LINE_SIZE, rte_socket_id());
744 [ # # ]: 0 : if (!pool->bmp_mem) {
745 : 0 : DRV_LOG(ERR, "Ipool bitmap mem allocate failed.\n");
746 : 0 : return;
747 : : }
748 : 0 : ibmp = rte_bitmap_init_with_all_set(bmp_num, pool->bmp_mem, mem_size);
749 [ # # ]: 0 : if (!ibmp) {
750 : 0 : pool->cfg.free(pool->bmp_mem);
751 : 0 : pool->bmp_mem = NULL;
752 : 0 : DRV_LOG(ERR, "Ipool bitmap create failed.\n");
753 : 0 : return;
754 : : }
755 : 0 : pool->ibmp = ibmp;
756 : : /* Clear global cache. */
757 [ # # ]: 0 : for (i = 0; i < gc->len; i++)
758 : 0 : rte_bitmap_clear(ibmp, gc->idx[i] - 1);
759 : : /* Clear core cache. */
760 [ # # ]: 0 : for (i = 0; i < RTE_MAX_LCORE + 1; i++) {
761 : 0 : struct mlx5_ipool_per_lcore *ilc = pool->cache[i];
762 : :
763 [ # # ]: 0 : if (!ilc)
764 : 0 : continue;
765 [ # # ]: 0 : for (j = 0; j < ilc->len; j++)
766 : 0 : rte_bitmap_clear(ibmp, ilc->idx[j] - 1);
767 : : }
768 : : }
769 : :
770 : : static void *
771 : 0 : mlx5_ipool_get_next_cache(struct mlx5_indexed_pool *pool, uint32_t *pos)
772 : : {
773 : : struct rte_bitmap *ibmp;
774 : 0 : uint64_t slab = 0;
775 : 0 : uint32_t iidx = *pos;
776 : :
777 : 0 : ibmp = pool->ibmp;
778 [ # # # # ]: 0 : if (!ibmp || !rte_bitmap_scan(ibmp, &iidx, &slab)) {
779 [ # # ]: 0 : if (pool->bmp_mem) {
780 : 0 : pool->cfg.free(pool->bmp_mem);
781 : 0 : pool->bmp_mem = NULL;
782 : 0 : pool->ibmp = NULL;
783 : : }
784 : 0 : return NULL;
785 : : }
786 : 0 : iidx += rte_ctz64(slab);
787 : 0 : rte_bitmap_clear(ibmp, iidx);
788 : 0 : iidx++;
789 : 0 : *pos = iidx;
790 : 0 : return mlx5_ipool_get_cache(pool, iidx);
791 : : }
792 : :
793 : : void *
794 : 0 : mlx5_ipool_get_next(struct mlx5_indexed_pool *pool, uint32_t *pos)
795 : : {
796 : 0 : uint32_t idx = *pos;
797 : : void *entry;
798 : :
799 [ # # ]: 0 : if (pool->cfg.per_core_cache)
800 : 0 : return mlx5_ipool_get_next_cache(pool, pos);
801 [ # # ]: 0 : while (idx <= mlx5_trunk_idx_offset_get(pool, pool->n_trunk)) {
802 : 0 : entry = mlx5_ipool_get(pool, idx);
803 [ # # ]: 0 : if (entry) {
804 : 0 : *pos = idx;
805 : 0 : return entry;
806 : : }
807 : 0 : idx++;
808 : : }
809 : : return NULL;
810 : : }
811 : :
812 : : void
813 : 0 : mlx5_ipool_dump(struct mlx5_indexed_pool *pool)
814 : : {
815 : 0 : printf("Pool %s entry size %u, trunks %u, %d entry per trunk, "
816 : : "total: %d\n",
817 : : pool->cfg.type, pool->cfg.size, pool->n_trunk_valid,
818 : 0 : pool->cfg.trunk_size, pool->n_trunk_valid);
819 : : #ifdef POOL_DEBUG
820 : : printf("Pool %s entry %u, trunk alloc %u, empty: %u, "
821 : : "available %u free %u\n",
822 : : pool->cfg.type, pool->n_entry, pool->trunk_new,
823 : : pool->trunk_empty, pool->trunk_avail, pool->trunk_free);
824 : : #endif
825 : 0 : }
826 : :
827 : : struct mlx5_l3t_tbl *
828 : 0 : mlx5_l3t_create(enum mlx5_l3t_type type)
829 : : {
830 : : struct mlx5_l3t_tbl *tbl;
831 : 0 : struct mlx5_indexed_pool_config l3t_ip_cfg = {
832 : : .trunk_size = 16,
833 : : .grow_trunk = 6,
834 : : .grow_shift = 1,
835 : : .need_lock = 0,
836 : : .release_mem_en = 1,
837 : : .malloc = mlx5_malloc,
838 : : .free = mlx5_free,
839 : : };
840 : :
841 [ # # ]: 0 : if (type >= MLX5_L3T_TYPE_MAX) {
842 : 0 : rte_errno = EINVAL;
843 : 0 : return NULL;
844 : : }
845 : 0 : tbl = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_l3t_tbl), 1,
846 : : SOCKET_ID_ANY);
847 [ # # ]: 0 : if (!tbl) {
848 : 0 : rte_errno = ENOMEM;
849 : 0 : return NULL;
850 : : }
851 : 0 : tbl->type = type;
852 [ # # # # ]: 0 : switch (type) {
853 : 0 : case MLX5_L3T_TYPE_WORD:
854 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_word);
855 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_w";
856 : 0 : break;
857 : 0 : case MLX5_L3T_TYPE_DWORD:
858 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_dword);
859 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_dw";
860 : 0 : break;
861 : 0 : case MLX5_L3T_TYPE_QWORD:
862 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_qword);
863 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_qw";
864 : 0 : break;
865 : 0 : default:
866 : 0 : l3t_ip_cfg.size = sizeof(struct mlx5_l3t_entry_ptr);
867 : 0 : l3t_ip_cfg.type = "mlx5_l3t_e_tbl_tpr";
868 : 0 : break;
869 : : }
870 : : rte_spinlock_init(&tbl->sl);
871 : 0 : tbl->eip = mlx5_ipool_create(&l3t_ip_cfg);
872 [ # # ]: 0 : if (!tbl->eip) {
873 : 0 : rte_errno = ENOMEM;
874 : 0 : mlx5_free(tbl);
875 : : tbl = NULL;
876 : : }
877 : : return tbl;
878 : : }
879 : :
880 : : void
881 : 0 : mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl)
882 : : {
883 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
884 : : uint32_t i, j;
885 : :
886 [ # # ]: 0 : if (!tbl)
887 : : return;
888 : 0 : g_tbl = tbl->tbl;
889 [ # # ]: 0 : if (g_tbl) {
890 [ # # ]: 0 : for (i = 0; i < MLX5_L3T_GT_SIZE; i++) {
891 : 0 : m_tbl = g_tbl->tbl[i];
892 [ # # ]: 0 : if (!m_tbl)
893 : 0 : continue;
894 [ # # ]: 0 : for (j = 0; j < MLX5_L3T_MT_SIZE; j++) {
895 [ # # ]: 0 : if (!m_tbl->tbl[j])
896 : 0 : continue;
897 : : MLX5_ASSERT(!((struct mlx5_l3t_entry_word *)
898 : : m_tbl->tbl[j])->ref_cnt);
899 : 0 : mlx5_ipool_free(tbl->eip,
900 : : ((struct mlx5_l3t_entry_word *)
901 : : m_tbl->tbl[j])->idx);
902 : 0 : m_tbl->tbl[j] = 0;
903 [ # # ]: 0 : if (!(--m_tbl->ref_cnt))
904 : : break;
905 : : }
906 : : MLX5_ASSERT(!m_tbl->ref_cnt);
907 : 0 : mlx5_free(g_tbl->tbl[i]);
908 : 0 : g_tbl->tbl[i] = 0;
909 [ # # ]: 0 : if (!(--g_tbl->ref_cnt))
910 : : break;
911 : : }
912 : : MLX5_ASSERT(!g_tbl->ref_cnt);
913 : 0 : mlx5_free(tbl->tbl);
914 : 0 : tbl->tbl = 0;
915 : : }
916 : 0 : mlx5_ipool_destroy(tbl->eip);
917 : 0 : mlx5_free(tbl);
918 : : }
919 : :
920 : : static int32_t
921 : 0 : __l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
922 : : union mlx5_l3t_data *data)
923 : : {
924 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
925 : : struct mlx5_l3t_entry_word *w_e_tbl;
926 : : struct mlx5_l3t_entry_dword *dw_e_tbl;
927 : : struct mlx5_l3t_entry_qword *qw_e_tbl;
928 : : struct mlx5_l3t_entry_ptr *ptr_e_tbl;
929 : : void *e_tbl;
930 : : uint32_t entry_idx;
931 : :
932 : 0 : g_tbl = tbl->tbl;
933 [ # # ]: 0 : if (!g_tbl)
934 : : return -1;
935 : 0 : m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
936 [ # # ]: 0 : if (!m_tbl)
937 : : return -1;
938 : 0 : e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
939 [ # # ]: 0 : if (!e_tbl)
940 : : return -1;
941 : 0 : entry_idx = idx & MLX5_L3T_ET_MASK;
942 [ # # # # ]: 0 : switch (tbl->type) {
943 : 0 : case MLX5_L3T_TYPE_WORD:
944 : : w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
945 : 0 : data->word = w_e_tbl->entry[entry_idx].data;
946 [ # # ]: 0 : if (w_e_tbl->entry[entry_idx].data)
947 : 0 : w_e_tbl->entry[entry_idx].ref_cnt++;
948 : : break;
949 : 0 : case MLX5_L3T_TYPE_DWORD:
950 : : dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
951 : 0 : data->dword = dw_e_tbl->entry[entry_idx].data;
952 [ # # ]: 0 : if (dw_e_tbl->entry[entry_idx].data)
953 : 0 : dw_e_tbl->entry[entry_idx].ref_cnt++;
954 : : break;
955 : 0 : case MLX5_L3T_TYPE_QWORD:
956 : : qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
957 : 0 : data->qword = qw_e_tbl->entry[entry_idx].data;
958 [ # # ]: 0 : if (qw_e_tbl->entry[entry_idx].data)
959 : 0 : qw_e_tbl->entry[entry_idx].ref_cnt++;
960 : : break;
961 : 0 : default:
962 : : ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
963 : 0 : data->ptr = ptr_e_tbl->entry[entry_idx].data;
964 [ # # ]: 0 : if (ptr_e_tbl->entry[entry_idx].data)
965 : 0 : ptr_e_tbl->entry[entry_idx].ref_cnt++;
966 : : break;
967 : : }
968 : : return 0;
969 : : }
970 : :
971 : : int32_t
972 : 0 : mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
973 : : union mlx5_l3t_data *data)
974 : : {
975 : : int ret;
976 : :
977 : 0 : rte_spinlock_lock(&tbl->sl);
978 : 0 : ret = __l3t_get_entry(tbl, idx, data);
979 : : rte_spinlock_unlock(&tbl->sl);
980 : 0 : return ret;
981 : : }
982 : :
983 : : int32_t
984 : 0 : mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx)
985 : : {
986 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
987 : : struct mlx5_l3t_entry_word *w_e_tbl;
988 : : struct mlx5_l3t_entry_dword *dw_e_tbl;
989 : : struct mlx5_l3t_entry_qword *qw_e_tbl;
990 : : struct mlx5_l3t_entry_ptr *ptr_e_tbl;
991 : : void *e_tbl;
992 : : uint32_t entry_idx;
993 : : uint64_t ref_cnt;
994 : : int32_t ret = -1;
995 : :
996 : 0 : rte_spinlock_lock(&tbl->sl);
997 : 0 : g_tbl = tbl->tbl;
998 [ # # ]: 0 : if (!g_tbl)
999 : 0 : goto out;
1000 : 0 : m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1001 [ # # ]: 0 : if (!m_tbl)
1002 : 0 : goto out;
1003 : 0 : e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1004 [ # # ]: 0 : if (!e_tbl)
1005 : 0 : goto out;
1006 : 0 : entry_idx = idx & MLX5_L3T_ET_MASK;
1007 [ # # # # ]: 0 : switch (tbl->type) {
1008 : 0 : case MLX5_L3T_TYPE_WORD:
1009 : : w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1010 : : MLX5_ASSERT(w_e_tbl->entry[entry_idx].ref_cnt);
1011 : 0 : ret = --w_e_tbl->entry[entry_idx].ref_cnt;
1012 [ # # ]: 0 : if (ret)
1013 : 0 : goto out;
1014 : 0 : w_e_tbl->entry[entry_idx].data = 0;
1015 : 0 : ref_cnt = --w_e_tbl->ref_cnt;
1016 : 0 : break;
1017 : 0 : case MLX5_L3T_TYPE_DWORD:
1018 : : dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1019 : : MLX5_ASSERT(dw_e_tbl->entry[entry_idx].ref_cnt);
1020 : 0 : ret = --dw_e_tbl->entry[entry_idx].ref_cnt;
1021 [ # # ]: 0 : if (ret)
1022 : 0 : goto out;
1023 : 0 : dw_e_tbl->entry[entry_idx].data = 0;
1024 : 0 : ref_cnt = --dw_e_tbl->ref_cnt;
1025 : 0 : break;
1026 : 0 : case MLX5_L3T_TYPE_QWORD:
1027 : : qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1028 : : MLX5_ASSERT(qw_e_tbl->entry[entry_idx].ref_cnt);
1029 : 0 : ret = --qw_e_tbl->entry[entry_idx].ref_cnt;
1030 [ # # ]: 0 : if (ret)
1031 : 0 : goto out;
1032 : 0 : qw_e_tbl->entry[entry_idx].data = 0;
1033 : 0 : ref_cnt = --qw_e_tbl->ref_cnt;
1034 : 0 : break;
1035 : 0 : default:
1036 : : ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1037 : : MLX5_ASSERT(ptr_e_tbl->entry[entry_idx].ref_cnt);
1038 : 0 : ret = --ptr_e_tbl->entry[entry_idx].ref_cnt;
1039 [ # # ]: 0 : if (ret)
1040 : 0 : goto out;
1041 : 0 : ptr_e_tbl->entry[entry_idx].data = NULL;
1042 : 0 : ref_cnt = --ptr_e_tbl->ref_cnt;
1043 : 0 : break;
1044 : : }
1045 [ # # ]: 0 : if (!ref_cnt) {
1046 : 0 : mlx5_ipool_free(tbl->eip,
1047 : : ((struct mlx5_l3t_entry_word *)e_tbl)->idx);
1048 : 0 : m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1049 : : NULL;
1050 [ # # ]: 0 : if (!(--m_tbl->ref_cnt)) {
1051 : 0 : mlx5_free(m_tbl);
1052 : : g_tbl->tbl
1053 : 0 : [(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] = NULL;
1054 [ # # ]: 0 : if (!(--g_tbl->ref_cnt)) {
1055 : 0 : mlx5_free(g_tbl);
1056 : 0 : tbl->tbl = 0;
1057 : : }
1058 : : }
1059 : : }
1060 : 0 : out:
1061 : : rte_spinlock_unlock(&tbl->sl);
1062 : 0 : return ret;
1063 : : }
1064 : :
1065 : : static int32_t
1066 : 0 : __l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1067 : : union mlx5_l3t_data *data)
1068 : : {
1069 : : struct mlx5_l3t_level_tbl *g_tbl, *m_tbl;
1070 : : struct mlx5_l3t_entry_word *w_e_tbl;
1071 : : struct mlx5_l3t_entry_dword *dw_e_tbl;
1072 : : struct mlx5_l3t_entry_qword *qw_e_tbl;
1073 : : struct mlx5_l3t_entry_ptr *ptr_e_tbl;
1074 : : void *e_tbl;
1075 : 0 : uint32_t entry_idx, tbl_idx = 0;
1076 : :
1077 : : /* Check the global table, create it if empty. */
1078 : 0 : g_tbl = tbl->tbl;
1079 [ # # ]: 0 : if (!g_tbl) {
1080 : 0 : g_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1081 : : sizeof(struct mlx5_l3t_level_tbl) +
1082 : : sizeof(void *) * MLX5_L3T_GT_SIZE, 1,
1083 : : SOCKET_ID_ANY);
1084 [ # # ]: 0 : if (!g_tbl) {
1085 : 0 : rte_errno = ENOMEM;
1086 : 0 : return -1;
1087 : : }
1088 : 0 : tbl->tbl = g_tbl;
1089 : : }
1090 : : /*
1091 : : * Check the middle table, create it if empty. Ref_cnt will be
1092 : : * increased if new sub table created.
1093 : : */
1094 : 0 : m_tbl = g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK];
1095 [ # # ]: 0 : if (!m_tbl) {
1096 : 0 : m_tbl = mlx5_malloc(MLX5_MEM_ZERO,
1097 : : sizeof(struct mlx5_l3t_level_tbl) +
1098 : : sizeof(void *) * MLX5_L3T_MT_SIZE, 1,
1099 : : SOCKET_ID_ANY);
1100 [ # # ]: 0 : if (!m_tbl) {
1101 : 0 : rte_errno = ENOMEM;
1102 : 0 : return -1;
1103 : : }
1104 : 0 : g_tbl->tbl[(idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK] =
1105 : : m_tbl;
1106 : 0 : g_tbl->ref_cnt++;
1107 : : }
1108 : : /*
1109 : : * Check the entry table, create it if empty. Ref_cnt will be
1110 : : * increased if new sub entry table created.
1111 : : */
1112 : 0 : e_tbl = m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK];
1113 [ # # ]: 0 : if (!e_tbl) {
1114 : 0 : e_tbl = mlx5_ipool_zmalloc(tbl->eip, &tbl_idx);
1115 [ # # ]: 0 : if (!e_tbl) {
1116 : 0 : rte_errno = ENOMEM;
1117 : 0 : return -1;
1118 : : }
1119 : 0 : ((struct mlx5_l3t_entry_word *)e_tbl)->idx = tbl_idx;
1120 : 0 : m_tbl->tbl[(idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK] =
1121 : : e_tbl;
1122 : 0 : m_tbl->ref_cnt++;
1123 : : }
1124 : 0 : entry_idx = idx & MLX5_L3T_ET_MASK;
1125 [ # # # # ]: 0 : switch (tbl->type) {
1126 : 0 : case MLX5_L3T_TYPE_WORD:
1127 : : w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl;
1128 [ # # ]: 0 : if (w_e_tbl->entry[entry_idx].data) {
1129 : 0 : data->word = w_e_tbl->entry[entry_idx].data;
1130 : 0 : w_e_tbl->entry[entry_idx].ref_cnt++;
1131 : 0 : rte_errno = EEXIST;
1132 : 0 : return -1;
1133 : : }
1134 : 0 : w_e_tbl->entry[entry_idx].data = data->word;
1135 : 0 : w_e_tbl->entry[entry_idx].ref_cnt = 1;
1136 : 0 : w_e_tbl->ref_cnt++;
1137 : 0 : break;
1138 : 0 : case MLX5_L3T_TYPE_DWORD:
1139 : : dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl;
1140 [ # # ]: 0 : if (dw_e_tbl->entry[entry_idx].data) {
1141 : 0 : data->dword = dw_e_tbl->entry[entry_idx].data;
1142 : 0 : dw_e_tbl->entry[entry_idx].ref_cnt++;
1143 : 0 : rte_errno = EEXIST;
1144 : 0 : return -1;
1145 : : }
1146 : 0 : dw_e_tbl->entry[entry_idx].data = data->dword;
1147 : 0 : dw_e_tbl->entry[entry_idx].ref_cnt = 1;
1148 : 0 : dw_e_tbl->ref_cnt++;
1149 : 0 : break;
1150 : 0 : case MLX5_L3T_TYPE_QWORD:
1151 : : qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl;
1152 [ # # ]: 0 : if (qw_e_tbl->entry[entry_idx].data) {
1153 : 0 : data->qword = qw_e_tbl->entry[entry_idx].data;
1154 : 0 : qw_e_tbl->entry[entry_idx].ref_cnt++;
1155 : 0 : rte_errno = EEXIST;
1156 : 0 : return -1;
1157 : : }
1158 : 0 : qw_e_tbl->entry[entry_idx].data = data->qword;
1159 : 0 : qw_e_tbl->entry[entry_idx].ref_cnt = 1;
1160 : 0 : qw_e_tbl->ref_cnt++;
1161 : 0 : break;
1162 : 0 : default:
1163 : : ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl;
1164 [ # # ]: 0 : if (ptr_e_tbl->entry[entry_idx].data) {
1165 : 0 : data->ptr = ptr_e_tbl->entry[entry_idx].data;
1166 : 0 : ptr_e_tbl->entry[entry_idx].ref_cnt++;
1167 : 0 : rte_errno = EEXIST;
1168 : 0 : return -1;
1169 : : }
1170 : 0 : ptr_e_tbl->entry[entry_idx].data = data->ptr;
1171 : 0 : ptr_e_tbl->entry[entry_idx].ref_cnt = 1;
1172 : 0 : ptr_e_tbl->ref_cnt++;
1173 : 0 : break;
1174 : : }
1175 : : return 0;
1176 : : }
1177 : :
1178 : : int32_t
1179 : 0 : mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx,
1180 : : union mlx5_l3t_data *data)
1181 : : {
1182 : : int ret;
1183 : :
1184 : 0 : rte_spinlock_lock(&tbl->sl);
1185 : 0 : ret = __l3t_set_entry(tbl, idx, data);
1186 : : rte_spinlock_unlock(&tbl->sl);
1187 : 0 : return ret;
1188 : : }
|