Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2020 Mellanox Technologies, Ltd
3 : : */
4 : :
5 : : #include <stdint.h>
6 : : #include <rte_malloc.h>
7 : : #include <mlx5_malloc.h>
8 : : #include <rte_ring.h>
9 : : #include <mlx5_devx_cmds.h>
10 : : #include <rte_cycles.h>
11 : : #include <rte_eal_paging.h>
12 : : #include <rte_thread.h>
13 : :
14 : : #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
15 : :
16 : : #include "mlx5_utils.h"
17 : : #include "mlx5_hws_cnt.h"
18 : :
19 : : #define HWS_CNT_CACHE_SZ_DEFAULT 511
20 : : #define HWS_CNT_CACHE_PRELOAD_DEFAULT 254
21 : : #define HWS_CNT_CACHE_FETCH_DEFAULT 254
22 : : #define HWS_CNT_CACHE_THRESHOLD_DEFAULT 254
23 : : #define HWS_CNT_ALLOC_FACTOR_DEFAULT 20
24 : :
25 : : static void
26 [ # # ]: 0 : __hws_cnt_id_load(struct mlx5_hws_cnt_pool *cpool)
27 : : {
28 : : uint32_t cnt_num = mlx5_hws_cnt_pool_get_size(cpool);
29 : : uint32_t iidx;
30 : :
31 : : /*
32 : : * Counter ID order is important for tracking the max number of in used
33 : : * counter for querying, which means counter internal index order must
34 : : * be from zero to the number user configured, i.e: 0 - 8000000.
35 : : * Need to load counter ID in this order into the cache firstly,
36 : : * and then the global free list.
37 : : * In the end, user fetch the counter from minimal to the maximum.
38 : : */
39 [ # # ]: 0 : for (iidx = 0; iidx < cnt_num; iidx++) {
40 : 0 : cnt_id_t cnt_id = mlx5_hws_cnt_id_gen(cpool, iidx);
41 : :
42 [ # # # # : 0 : rte_ring_enqueue_elem(cpool->free_list, &cnt_id,
# ]
43 : : sizeof(cnt_id));
44 : : }
45 : 0 : }
46 : :
47 : : static void
48 : 0 : __mlx5_hws_cnt_svc(struct mlx5_dev_ctx_shared *sh,
49 : : struct mlx5_hws_cnt_pool *cpool)
50 : : {
51 : 0 : struct rte_ring *reset_list = cpool->wait_reset_list;
52 : 0 : struct rte_ring *reuse_list = cpool->reuse_list;
53 : : uint32_t reset_cnt_num;
54 : : struct rte_ring_zc_data zcdr = {0};
55 : : struct rte_ring_zc_data zcdu = {0};
56 : : uint32_t ret __rte_unused;
57 : :
58 : : reset_cnt_num = rte_ring_count(reset_list);
59 : : do {
60 : 0 : cpool->query_gen++;
61 : 0 : mlx5_aso_cnt_query(sh, cpool);
62 : : zcdr.n1 = 0;
63 : : zcdu.n1 = 0;
64 : : ret = rte_ring_enqueue_zc_burst_elem_start(reuse_list,
65 : : sizeof(cnt_id_t),
66 : : reset_cnt_num, &zcdu,
67 : : NULL);
68 : : MLX5_ASSERT(ret == reset_cnt_num);
69 : : ret = rte_ring_dequeue_zc_burst_elem_start(reset_list,
70 : : sizeof(cnt_id_t),
71 : : reset_cnt_num, &zcdr,
72 : : NULL);
73 : : MLX5_ASSERT(ret == reset_cnt_num);
74 : : __hws_cnt_r2rcpy(&zcdu, &zcdr, reset_cnt_num);
75 : : rte_ring_dequeue_zc_elem_finish(reset_list, reset_cnt_num);
76 : : rte_ring_enqueue_zc_elem_finish(reuse_list, reset_cnt_num);
77 : : reset_cnt_num = rte_ring_count(reset_list);
78 [ # # ]: 0 : } while (reset_cnt_num > 0);
79 : 0 : }
80 : :
81 : : /**
82 : : * Release AGE parameter.
83 : : *
84 : : * @param priv
85 : : * Pointer to the port private data structure.
86 : : * @param own_cnt_index
87 : : * Counter ID to created only for this AGE to release.
88 : : * Zero means there is no such counter.
89 : : * @param age_ipool
90 : : * Pointer to AGE parameter indexed pool.
91 : : * @param idx
92 : : * Index of AGE parameter in the indexed pool.
93 : : */
94 : : static void
95 : 0 : mlx5_hws_age_param_free(struct mlx5_priv *priv, cnt_id_t own_cnt_index,
96 : : struct mlx5_indexed_pool *age_ipool, uint32_t idx)
97 : : {
98 [ # # ]: 0 : if (own_cnt_index) {
99 [ # # ]: 0 : struct mlx5_hws_cnt_pool *cpool = priv->hws_cpool;
100 : :
101 : : MLX5_ASSERT(mlx5_hws_cnt_is_shared(cpool, own_cnt_index));
102 : : mlx5_hws_cnt_shared_put(cpool, &own_cnt_index);
103 : : }
104 : 0 : mlx5_ipool_free(age_ipool, idx);
105 : 0 : }
106 : :
107 : : /**
108 : : * Check and callback event for new aged flow in the HWS counter pool.
109 : : *
110 : : * @param[in] priv
111 : : * Pointer to port private object.
112 : : * @param[in] cpool
113 : : * Pointer to current counter pool.
114 : : */
115 : : static void
116 : 0 : mlx5_hws_aging_check(struct mlx5_priv *priv, struct mlx5_hws_cnt_pool *cpool)
117 : : {
118 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
119 : 0 : struct flow_counter_stats *stats = cpool->raw_mng->raw;
120 : : struct mlx5_hws_age_param *param;
121 : : struct rte_ring *r;
122 : 0 : const uint64_t curr_time = MLX5_CURR_TIME_SEC;
123 [ # # ]: 0 : const uint32_t time_delta = curr_time - cpool->time_of_last_age_check;
124 : : uint32_t nb_alloc_cnts = mlx5_hws_cnt_pool_get_size(cpool);
125 : : uint16_t expected1 = HWS_AGE_CANDIDATE;
126 : : uint16_t expected2 = HWS_AGE_CANDIDATE_INSIDE_RING;
127 : : uint32_t i;
128 : :
129 : 0 : cpool->time_of_last_age_check = curr_time;
130 [ # # ]: 0 : for (i = 0; i < nb_alloc_cnts; ++i) {
131 : 0 : uint32_t age_idx = cpool->pool[i].age_idx;
132 : : uint64_t hits;
133 : :
134 [ # # # # ]: 0 : if (!cpool->pool[i].in_used || age_idx == 0)
135 : 0 : continue;
136 : 0 : param = mlx5_ipool_get(age_info->ages_ipool, age_idx);
137 [ # # ]: 0 : if (unlikely(param == NULL)) {
138 : : /*
139 : : * When AGE which used indirect counter it is user
140 : : * responsibility not using this indirect counter
141 : : * without this AGE.
142 : : * If this counter is used after the AGE was freed, the
143 : : * AGE index is invalid and using it here will cause a
144 : : * segmentation fault.
145 : : */
146 : 0 : DRV_LOG(WARNING,
147 : : "Counter %u is lost his AGE, it is unused.", i);
148 : 0 : continue;
149 : : }
150 [ # # ]: 0 : if (param->timeout == 0)
151 : 0 : continue;
152 [ # # # ]: 0 : switch (__atomic_load_n(¶m->state, __ATOMIC_RELAXED)) {
153 : 0 : case HWS_AGE_AGED_OUT_NOT_REPORTED:
154 : : case HWS_AGE_AGED_OUT_REPORTED:
155 : : /* Already aged-out, no action is needed. */
156 : 0 : continue;
157 : : case HWS_AGE_CANDIDATE:
158 : : case HWS_AGE_CANDIDATE_INSIDE_RING:
159 : : /* This AGE candidate to be aged-out, go to checking. */
160 : : break;
161 : 0 : case HWS_AGE_FREE:
162 : : /*
163 : : * AGE parameter with state "FREE" couldn't be pointed
164 : : * by any counter since counter is destroyed first.
165 : : * Fall-through.
166 : : */
167 : : default:
168 : : MLX5_ASSERT(0);
169 : 0 : continue;
170 : : }
171 [ # # ]: 0 : hits = rte_be_to_cpu_64(stats[i].hits);
172 [ # # ]: 0 : if (param->nb_cnts == 1) {
173 [ # # ]: 0 : if (hits != param->accumulator_last_hits) {
174 : 0 : __atomic_store_n(¶m->sec_since_last_hit, 0,
175 : : __ATOMIC_RELAXED);
176 : 0 : param->accumulator_last_hits = hits;
177 : 0 : continue;
178 : : }
179 : : } else {
180 : 0 : param->accumulator_hits += hits;
181 : 0 : param->accumulator_cnt++;
182 [ # # ]: 0 : if (param->accumulator_cnt < param->nb_cnts)
183 : 0 : continue;
184 : 0 : param->accumulator_cnt = 0;
185 [ # # ]: 0 : if (param->accumulator_last_hits !=
186 : : param->accumulator_hits) {
187 : 0 : __atomic_store_n(¶m->sec_since_last_hit,
188 : : 0, __ATOMIC_RELAXED);
189 : 0 : param->accumulator_last_hits =
190 : 0 : param->accumulator_hits;
191 : 0 : param->accumulator_hits = 0;
192 : 0 : continue;
193 : : }
194 : 0 : param->accumulator_hits = 0;
195 : : }
196 : 0 : if (__atomic_fetch_add(¶m->sec_since_last_hit, time_delta,
197 : 0 : __ATOMIC_RELAXED) + time_delta <=
198 [ # # ]: 0 : __atomic_load_n(¶m->timeout, __ATOMIC_RELAXED))
199 : 0 : continue;
200 : : /* Prepare the relevant ring for this AGE parameter */
201 [ # # ]: 0 : if (priv->hws_strict_queue)
202 : 0 : r = age_info->hw_q_age->aged_lists[param->queue_id];
203 : : else
204 : 0 : r = age_info->hw_age.aged_list;
205 : : /* Changing the state atomically and insert it into the ring. */
206 [ # # ]: 0 : if (__atomic_compare_exchange_n(¶m->state, &expected1,
207 : : HWS_AGE_AGED_OUT_NOT_REPORTED,
208 : : false, __ATOMIC_RELAXED,
209 : : __ATOMIC_RELAXED)) {
210 : : int ret = rte_ring_enqueue_burst_elem(r, &age_idx,
211 : : sizeof(uint32_t),
212 : : 1, NULL);
213 : :
214 : : /*
215 : : * The ring doesn't have enough room for this entry,
216 : : * it replace back the state for the next second.
217 : : *
218 : : * FIXME: if until next sec it get traffic, we are going
219 : : * to lose this "aged out", will be fixed later
220 : : * when optimise it to fill ring in bulks.
221 : : */
222 : : expected2 = HWS_AGE_AGED_OUT_NOT_REPORTED;
223 [ # # # # ]: 0 : if (ret == 0 &&
224 : 0 : !__atomic_compare_exchange_n(¶m->state,
225 : : &expected2, expected1,
226 : : false,
227 : : __ATOMIC_RELAXED,
228 [ # # ]: 0 : __ATOMIC_RELAXED) &&
229 : : expected2 == HWS_AGE_FREE)
230 : 0 : mlx5_hws_age_param_free(priv,
231 : : param->own_cnt_index,
232 : : age_info->ages_ipool,
233 : : age_idx);
234 : : /* The event is irrelevant in strict queue mode. */
235 [ # # ]: 0 : if (!priv->hws_strict_queue)
236 : 0 : MLX5_AGE_SET(age_info, MLX5_AGE_EVENT_NEW);
237 : : } else {
238 : 0 : __atomic_compare_exchange_n(¶m->state, &expected2,
239 : : HWS_AGE_AGED_OUT_NOT_REPORTED,
240 : : false, __ATOMIC_RELAXED,
241 : : __ATOMIC_RELAXED);
242 : : }
243 : : }
244 : : /* The event is irrelevant in strict queue mode. */
245 [ # # ]: 0 : if (!priv->hws_strict_queue)
246 : 0 : mlx5_age_event_prepare(priv->sh);
247 : 0 : }
248 : :
249 : : static void
250 : 0 : mlx5_hws_cnt_raw_data_free(struct mlx5_dev_ctx_shared *sh,
251 : : struct mlx5_hws_cnt_raw_data_mng *mng)
252 : : {
253 [ # # ]: 0 : if (mng == NULL)
254 : : return;
255 : 0 : sh->cdev->mr_scache.dereg_mr_cb(&mng->mr);
256 : 0 : mlx5_free(mng->raw);
257 : 0 : mlx5_free(mng);
258 : : }
259 : :
260 : : __rte_unused
261 : : static struct mlx5_hws_cnt_raw_data_mng *
262 : 0 : mlx5_hws_cnt_raw_data_alloc(struct mlx5_dev_ctx_shared *sh, uint32_t n)
263 : : {
264 : : struct mlx5_hws_cnt_raw_data_mng *mng = NULL;
265 : : int ret;
266 : 0 : size_t sz = n * sizeof(struct flow_counter_stats);
267 : 0 : size_t pgsz = rte_mem_page_size();
268 : :
269 : : MLX5_ASSERT(pgsz > 0);
270 : 0 : mng = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO, sizeof(*mng), 0,
271 : : SOCKET_ID_ANY);
272 [ # # ]: 0 : if (mng == NULL)
273 : 0 : goto error;
274 : 0 : mng->raw = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO, sz, pgsz,
275 : : SOCKET_ID_ANY);
276 [ # # ]: 0 : if (mng->raw == NULL)
277 : 0 : goto error;
278 : 0 : ret = sh->cdev->mr_scache.reg_mr_cb(sh->cdev->pd, mng->raw, sz,
279 : : &mng->mr);
280 [ # # ]: 0 : if (ret) {
281 : 0 : rte_errno = errno;
282 : 0 : goto error;
283 : : }
284 : : return mng;
285 : 0 : error:
286 : 0 : mlx5_hws_cnt_raw_data_free(sh, mng);
287 : 0 : return NULL;
288 : : }
289 : :
290 : : static uint32_t
291 : 0 : mlx5_hws_cnt_svc(void *opaque)
292 : : {
293 : : struct mlx5_dev_ctx_shared *sh =
294 : : (struct mlx5_dev_ctx_shared *)opaque;
295 : 0 : uint64_t interval =
296 : 0 : (uint64_t)sh->cnt_svc->query_interval * (US_PER_S / MS_PER_S);
297 : : struct mlx5_hws_cnt_pool *hws_cpool;
298 : : uint64_t start_cycle, query_cycle = 0;
299 : : uint64_t query_us;
300 : : uint64_t sleep_us;
301 : :
302 [ # # ]: 0 : while (sh->cnt_svc->svc_running != 0) {
303 [ # # ]: 0 : if (rte_spinlock_trylock(&sh->cpool_lock) == 0)
304 : 0 : continue;
305 : : start_cycle = rte_rdtsc();
306 : : /* 200ms for 16M counters. */
307 [ # # ]: 0 : LIST_FOREACH(hws_cpool, &sh->hws_cpool_list, next) {
308 : 0 : struct mlx5_priv *opriv = hws_cpool->priv;
309 : :
310 : 0 : __mlx5_hws_cnt_svc(sh, hws_cpool);
311 [ # # ]: 0 : if (opriv->hws_age_req)
312 : 0 : mlx5_hws_aging_check(opriv, hws_cpool);
313 : : }
314 : 0 : query_cycle = rte_rdtsc() - start_cycle;
315 : : rte_spinlock_unlock(&sh->cpool_lock);
316 : 0 : query_us = query_cycle / (rte_get_timer_hz() / US_PER_S);
317 : 0 : sleep_us = interval - query_us;
318 [ # # ]: 0 : if (interval > query_us)
319 : 0 : rte_delay_us_sleep(sleep_us);
320 : : }
321 : 0 : return 0;
322 : : }
323 : :
324 : : static void
325 : 0 : mlx5_hws_cnt_pool_deinit(struct mlx5_hws_cnt_pool * const cntp)
326 : : {
327 : : uint32_t qidx = 0;
328 [ # # ]: 0 : if (cntp == NULL)
329 : : return;
330 : 0 : rte_ring_free(cntp->free_list);
331 : 0 : rte_ring_free(cntp->wait_reset_list);
332 : 0 : rte_ring_free(cntp->reuse_list);
333 [ # # ]: 0 : if (cntp->cache) {
334 [ # # ]: 0 : for (qidx = 0; qidx < cntp->cache->q_num; qidx++)
335 : 0 : rte_ring_free(cntp->cache->qcache[qidx]);
336 : : }
337 : 0 : mlx5_free(cntp->cache);
338 : 0 : mlx5_free(cntp->raw_mng);
339 : 0 : mlx5_free(cntp->pool);
340 : 0 : mlx5_free(cntp);
341 : : }
342 : :
343 : : static struct mlx5_hws_cnt_pool *
344 : 0 : mlx5_hws_cnt_pool_init(struct mlx5_dev_ctx_shared *sh,
345 : : const struct mlx5_hws_cnt_pool_cfg *pcfg,
346 : : const struct mlx5_hws_cache_param *ccfg)
347 : : {
348 : : char mz_name[RTE_MEMZONE_NAMESIZE];
349 : : struct mlx5_hws_cnt_pool *cntp;
350 : : uint64_t cnt_num = 0;
351 : : uint32_t qidx;
352 : :
353 : : MLX5_ASSERT(pcfg);
354 : : MLX5_ASSERT(ccfg);
355 : 0 : cntp = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO, sizeof(*cntp), 0,
356 : : SOCKET_ID_ANY);
357 [ # # ]: 0 : if (cntp == NULL)
358 : : return NULL;
359 : :
360 : 0 : cntp->cfg = *pcfg;
361 [ # # ]: 0 : if (cntp->cfg.host_cpool)
362 : : return cntp;
363 : 0 : cntp->cache = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
364 : 0 : sizeof(*cntp->cache) +
365 : : sizeof(((struct mlx5_hws_cnt_pool_caches *)0)->qcache[0])
366 : 0 : * ccfg->q_num, 0, SOCKET_ID_ANY);
367 [ # # ]: 0 : if (cntp->cache == NULL)
368 : 0 : goto error;
369 : : /* store the necessary cache parameters. */
370 : 0 : cntp->cache->fetch_sz = ccfg->fetch_sz;
371 : 0 : cntp->cache->preload_sz = ccfg->preload_sz;
372 : 0 : cntp->cache->threshold = ccfg->threshold;
373 : 0 : cntp->cache->q_num = ccfg->q_num;
374 [ # # ]: 0 : if (pcfg->request_num > sh->hws_max_nb_counters) {
375 : 0 : DRV_LOG(ERR, "Counter number %u "
376 : : "is greater than the maximum supported (%u).",
377 : : pcfg->request_num, sh->hws_max_nb_counters);
378 : 0 : goto error;
379 : : }
380 : 0 : cnt_num = pcfg->request_num * (100 + pcfg->alloc_factor) / 100;
381 : : if (cnt_num > UINT32_MAX) {
382 : : DRV_LOG(ERR, "counter number %"PRIu64" is out of 32bit range",
383 : : cnt_num);
384 : : goto error;
385 : : }
386 : : /*
387 : : * When counter request number is supported, but the factor takes it
388 : : * out of size, the factor is reduced.
389 : : */
390 : 0 : cnt_num = RTE_MIN((uint32_t)cnt_num, sh->hws_max_nb_counters);
391 : 0 : cntp->pool = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
392 : : sizeof(struct mlx5_hws_cnt) * cnt_num,
393 : : 0, SOCKET_ID_ANY);
394 [ # # ]: 0 : if (cntp->pool == NULL)
395 : 0 : goto error;
396 : 0 : snprintf(mz_name, sizeof(mz_name), "%s_F_RING", pcfg->name);
397 : 0 : cntp->free_list = rte_ring_create_elem(mz_name, sizeof(cnt_id_t),
398 : : (uint32_t)cnt_num, SOCKET_ID_ANY,
399 : : RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ |
400 : : RING_F_EXACT_SZ);
401 [ # # ]: 0 : if (cntp->free_list == NULL) {
402 : 0 : DRV_LOG(ERR, "failed to create free list ring");
403 : 0 : goto error;
404 : : }
405 : 0 : snprintf(mz_name, sizeof(mz_name), "%s_R_RING", pcfg->name);
406 : 0 : cntp->wait_reset_list = rte_ring_create_elem(mz_name, sizeof(cnt_id_t),
407 : : (uint32_t)cnt_num, SOCKET_ID_ANY,
408 : : RING_F_MP_HTS_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ);
409 [ # # ]: 0 : if (cntp->wait_reset_list == NULL) {
410 : 0 : DRV_LOG(ERR, "failed to create free list ring");
411 : 0 : goto error;
412 : : }
413 : 0 : snprintf(mz_name, sizeof(mz_name), "%s_U_RING", pcfg->name);
414 : 0 : cntp->reuse_list = rte_ring_create_elem(mz_name, sizeof(cnt_id_t),
415 : : (uint32_t)cnt_num, SOCKET_ID_ANY,
416 : : RING_F_MP_HTS_ENQ | RING_F_MC_HTS_DEQ | RING_F_EXACT_SZ);
417 [ # # ]: 0 : if (cntp->reuse_list == NULL) {
418 : 0 : DRV_LOG(ERR, "failed to create reuse list ring");
419 : 0 : goto error;
420 : : }
421 [ # # ]: 0 : for (qidx = 0; qidx < ccfg->q_num; qidx++) {
422 : 0 : snprintf(mz_name, sizeof(mz_name), "%s_qc/%x", pcfg->name, qidx);
423 : 0 : cntp->cache->qcache[qidx] = rte_ring_create(mz_name, ccfg->size,
424 : : SOCKET_ID_ANY,
425 : : RING_F_SP_ENQ | RING_F_SC_DEQ |
426 : : RING_F_EXACT_SZ);
427 [ # # ]: 0 : if (cntp->cache->qcache[qidx] == NULL)
428 : 0 : goto error;
429 : : }
430 : : /* Initialize the time for aging-out calculation. */
431 : 0 : cntp->time_of_last_age_check = MLX5_CURR_TIME_SEC;
432 : 0 : return cntp;
433 : 0 : error:
434 : 0 : mlx5_hws_cnt_pool_deinit(cntp);
435 : 0 : return NULL;
436 : : }
437 : :
438 : : int
439 : 0 : mlx5_hws_cnt_service_thread_create(struct mlx5_dev_ctx_shared *sh)
440 : : {
441 : : char name[RTE_THREAD_INTERNAL_NAME_SIZE];
442 : : rte_thread_attr_t attr;
443 : : int ret;
444 : 0 : uint32_t service_core = sh->cnt_svc->service_core;
445 : :
446 : 0 : ret = rte_thread_attr_init(&attr);
447 [ # # ]: 0 : if (ret != 0)
448 : 0 : goto error;
449 [ # # ]: 0 : CPU_SET(service_core, &attr.cpuset);
450 : 0 : sh->cnt_svc->svc_running = 1;
451 : 0 : ret = rte_thread_create(&sh->cnt_svc->service_thread,
452 : : &attr, mlx5_hws_cnt_svc, sh);
453 [ # # ]: 0 : if (ret != 0)
454 : 0 : goto error;
455 : : snprintf(name, sizeof(name), "mlx5-cn%d", service_core);
456 : 0 : rte_thread_set_prefixed_name(sh->cnt_svc->service_thread, name);
457 : :
458 : 0 : return 0;
459 : 0 : error:
460 : 0 : DRV_LOG(ERR, "Failed to create HW steering's counter service thread.");
461 : 0 : return ret;
462 : : }
463 : :
464 : : void
465 : 0 : mlx5_hws_cnt_service_thread_destroy(struct mlx5_dev_ctx_shared *sh)
466 : : {
467 [ # # ]: 0 : if (sh->cnt_svc->service_thread.opaque_id == 0)
468 : : return;
469 : 0 : sh->cnt_svc->svc_running = 0;
470 : 0 : rte_thread_join(sh->cnt_svc->service_thread, NULL);
471 : 0 : sh->cnt_svc->service_thread.opaque_id = 0;
472 : : }
473 : :
474 : : static int
475 : 0 : mlx5_hws_cnt_pool_dcs_alloc(struct mlx5_dev_ctx_shared *sh,
476 : : struct mlx5_hws_cnt_pool *cpool)
477 : : {
478 : 0 : struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
479 [ # # ]: 0 : uint32_t max_log_bulk_sz = sh->hws_max_log_bulk_sz;
480 : : uint32_t log_bulk_sz;
481 : : uint32_t idx, alloc_candidate, alloced = 0;
482 : : unsigned int cnt_num = mlx5_hws_cnt_pool_get_size(cpool);
483 : 0 : struct mlx5_devx_counter_attr attr = {0};
484 : : struct mlx5_devx_obj *dcs;
485 : :
486 : : MLX5_ASSERT(cpool->cfg.host_cpool == NULL);
487 [ # # ]: 0 : if (hca_attr->flow_counter_bulk_log_max_alloc == 0) {
488 : 0 : DRV_LOG(ERR, "Fw doesn't support bulk log max alloc");
489 : 0 : return -1;
490 : : }
491 [ # # ]: 0 : cnt_num = RTE_ALIGN_CEIL(cnt_num, 4); /* minimal 4 counter in bulk. */
492 : 0 : log_bulk_sz = RTE_MIN(max_log_bulk_sz, rte_log2_u32(cnt_num));
493 : 0 : attr.pd = sh->cdev->pdn;
494 : 0 : attr.pd_valid = 1;
495 : 0 : attr.bulk_log_max_alloc = 1;
496 : 0 : attr.flow_counter_bulk_log_size = log_bulk_sz;
497 : : idx = 0;
498 : 0 : dcs = mlx5_devx_cmd_flow_counter_alloc_general(sh->cdev->ctx, &attr);
499 [ # # ]: 0 : if (dcs == NULL)
500 : 0 : goto error;
501 : 0 : cpool->dcs_mng.dcs[idx].obj = dcs;
502 : 0 : cpool->dcs_mng.dcs[idx].batch_sz = (1 << log_bulk_sz);
503 : 0 : cpool->dcs_mng.batch_total++;
504 : : idx++;
505 : 0 : cpool->dcs_mng.dcs[0].iidx = 0;
506 : : alloced = cpool->dcs_mng.dcs[0].batch_sz;
507 [ # # ]: 0 : if (cnt_num > cpool->dcs_mng.dcs[0].batch_sz) {
508 [ # # ]: 0 : while (idx < MLX5_HWS_CNT_DCS_NUM) {
509 : 0 : attr.flow_counter_bulk_log_size = --max_log_bulk_sz;
510 : 0 : alloc_candidate = RTE_BIT32(max_log_bulk_sz);
511 [ # # ]: 0 : if (alloced + alloc_candidate > sh->hws_max_nb_counters)
512 : 0 : continue;
513 : 0 : dcs = mlx5_devx_cmd_flow_counter_alloc_general
514 : 0 : (sh->cdev->ctx, &attr);
515 [ # # ]: 0 : if (dcs == NULL)
516 : 0 : goto error;
517 : 0 : cpool->dcs_mng.dcs[idx].obj = dcs;
518 : 0 : cpool->dcs_mng.dcs[idx].batch_sz = alloc_candidate;
519 : 0 : cpool->dcs_mng.dcs[idx].iidx = alloced;
520 : : alloced += cpool->dcs_mng.dcs[idx].batch_sz;
521 : 0 : cpool->dcs_mng.batch_total++;
522 [ # # ]: 0 : if (alloced >= cnt_num)
523 : : break;
524 : 0 : idx++;
525 : : }
526 : : }
527 : : return 0;
528 : 0 : error:
529 : 0 : DRV_LOG(DEBUG,
530 : : "Cannot alloc device counter, allocated[%" PRIu32 "] request[%" PRIu32 "]",
531 : : alloced, cnt_num);
532 [ # # ]: 0 : for (idx = 0; idx < cpool->dcs_mng.batch_total; idx++) {
533 : 0 : mlx5_devx_cmd_destroy(cpool->dcs_mng.dcs[idx].obj);
534 : 0 : cpool->dcs_mng.dcs[idx].obj = NULL;
535 : 0 : cpool->dcs_mng.dcs[idx].batch_sz = 0;
536 : 0 : cpool->dcs_mng.dcs[idx].iidx = 0;
537 : : }
538 : 0 : cpool->dcs_mng.batch_total = 0;
539 : 0 : return -1;
540 : : }
541 : :
542 : : static void
543 : 0 : mlx5_hws_cnt_pool_dcs_free(struct mlx5_dev_ctx_shared *sh,
544 : : struct mlx5_hws_cnt_pool *cpool)
545 : : {
546 : : uint32_t idx;
547 : :
548 [ # # ]: 0 : if (cpool == NULL)
549 : : return;
550 [ # # ]: 0 : for (idx = 0; idx < MLX5_HWS_CNT_DCS_NUM; idx++)
551 : 0 : mlx5_devx_cmd_destroy(cpool->dcs_mng.dcs[idx].obj);
552 [ # # ]: 0 : if (cpool->raw_mng) {
553 : 0 : mlx5_hws_cnt_raw_data_free(sh, cpool->raw_mng);
554 : 0 : cpool->raw_mng = NULL;
555 : : }
556 : : }
557 : :
558 : : static void
559 : 0 : mlx5_hws_cnt_pool_action_destroy(struct mlx5_hws_cnt_pool *cpool)
560 : : {
561 : : uint32_t idx;
562 : :
563 [ # # ]: 0 : for (idx = 0; idx < cpool->dcs_mng.batch_total; idx++) {
564 : : struct mlx5_hws_cnt_dcs *dcs = &cpool->dcs_mng.dcs[idx];
565 : :
566 [ # # ]: 0 : if (dcs->dr_action != NULL) {
567 : 0 : mlx5dr_action_destroy(dcs->dr_action);
568 : 0 : dcs->dr_action = NULL;
569 : : }
570 : : }
571 : 0 : }
572 : :
573 : : static int
574 [ # # ]: 0 : mlx5_hws_cnt_pool_action_create(struct mlx5_priv *priv,
575 : : struct mlx5_hws_cnt_pool *cpool)
576 : : {
577 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
578 : : uint32_t idx;
579 : : int ret = 0;
580 : : uint32_t flags;
581 : :
582 : : flags = MLX5DR_ACTION_FLAG_HWS_RX | MLX5DR_ACTION_FLAG_HWS_TX;
583 [ # # # # ]: 0 : if (priv->sh->config.dv_esw_en && priv->master)
584 : : flags |= MLX5DR_ACTION_FLAG_HWS_FDB;
585 [ # # ]: 0 : for (idx = 0; idx < hpool->dcs_mng.batch_total; idx++) {
586 : : struct mlx5_hws_cnt_dcs *hdcs = &hpool->dcs_mng.dcs[idx];
587 : : struct mlx5_hws_cnt_dcs *dcs = &cpool->dcs_mng.dcs[idx];
588 : :
589 : 0 : dcs->dr_action = mlx5dr_action_create_counter(priv->dr_ctx,
590 : 0 : (struct mlx5dr_devx_obj *)hdcs->obj,
591 : : flags);
592 [ # # ]: 0 : if (dcs->dr_action == NULL) {
593 : 0 : mlx5_hws_cnt_pool_action_destroy(cpool);
594 : : ret = -ENOSYS;
595 : 0 : break;
596 : : }
597 : : }
598 : 0 : return ret;
599 : : }
600 : :
601 : : struct mlx5_hws_cnt_pool *
602 : 0 : mlx5_hws_cnt_pool_create(struct rte_eth_dev *dev,
603 : : const struct rte_flow_port_attr *pattr, uint16_t nb_queue)
604 : : {
605 : : struct mlx5_hws_cnt_pool *cpool = NULL;
606 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
607 : 0 : struct mlx5_hws_cache_param cparam = {0};
608 : 0 : struct mlx5_hws_cnt_pool_cfg pcfg = {0};
609 : : char *mp_name;
610 : : int ret = 0;
611 : : size_t sz;
612 : :
613 : 0 : mp_name = mlx5_malloc(MLX5_MEM_ZERO, RTE_MEMZONE_NAMESIZE, 0, SOCKET_ID_ANY);
614 [ # # ]: 0 : if (mp_name == NULL)
615 : 0 : goto error;
616 [ # # ]: 0 : snprintf(mp_name, RTE_MEMZONE_NAMESIZE, "MLX5_HWS_CNT_P_%x", dev->data->port_id);
617 : 0 : pcfg.name = mp_name;
618 : 0 : pcfg.request_num = pattr->nb_counters;
619 : 0 : pcfg.alloc_factor = HWS_CNT_ALLOC_FACTOR_DEFAULT;
620 [ # # ]: 0 : if (pattr->flags & RTE_FLOW_PORT_FLAG_SHARE_INDIRECT) {
621 : 0 : struct mlx5_priv *host_priv =
622 : 0 : priv->shared_host->data->dev_private;
623 : 0 : struct mlx5_hws_cnt_pool *chost = host_priv->hws_cpool;
624 : :
625 : 0 : pcfg.host_cpool = chost;
626 : 0 : cpool = mlx5_hws_cnt_pool_init(priv->sh, &pcfg, &cparam);
627 [ # # ]: 0 : if (cpool == NULL)
628 : 0 : goto error;
629 : 0 : ret = mlx5_hws_cnt_pool_action_create(priv, cpool);
630 [ # # ]: 0 : if (ret != 0)
631 : 0 : goto error;
632 : : return cpool;
633 : : }
634 : : /* init cnt service if not. */
635 [ # # ]: 0 : if (priv->sh->cnt_svc == NULL) {
636 : 0 : ret = mlx5_hws_cnt_svc_init(priv->sh);
637 [ # # ]: 0 : if (ret != 0)
638 : : return NULL;
639 : : }
640 : 0 : cparam.fetch_sz = HWS_CNT_CACHE_FETCH_DEFAULT;
641 : 0 : cparam.preload_sz = HWS_CNT_CACHE_PRELOAD_DEFAULT;
642 : 0 : cparam.q_num = nb_queue;
643 : 0 : cparam.threshold = HWS_CNT_CACHE_THRESHOLD_DEFAULT;
644 : 0 : cparam.size = HWS_CNT_CACHE_SZ_DEFAULT;
645 : 0 : cpool = mlx5_hws_cnt_pool_init(priv->sh, &pcfg, &cparam);
646 [ # # ]: 0 : if (cpool == NULL)
647 : 0 : goto error;
648 : 0 : ret = mlx5_hws_cnt_pool_dcs_alloc(priv->sh, cpool);
649 [ # # ]: 0 : if (ret != 0)
650 : 0 : goto error;
651 : 0 : sz = RTE_ALIGN_CEIL(mlx5_hws_cnt_pool_get_size(cpool), 4);
652 : 0 : cpool->raw_mng = mlx5_hws_cnt_raw_data_alloc(priv->sh, sz);
653 [ # # ]: 0 : if (cpool->raw_mng == NULL)
654 : 0 : goto error;
655 : 0 : __hws_cnt_id_load(cpool);
656 : : /*
657 : : * Bump query gen right after pool create so the
658 : : * pre-loaded counters can be used directly
659 : : * because they already have init value no need
660 : : * to wait for query.
661 : : */
662 : 0 : cpool->query_gen = 1;
663 : 0 : ret = mlx5_hws_cnt_pool_action_create(priv, cpool);
664 [ # # ]: 0 : if (ret != 0)
665 : 0 : goto error;
666 : 0 : priv->sh->cnt_svc->refcnt++;
667 : 0 : cpool->priv = priv;
668 : 0 : rte_spinlock_lock(&priv->sh->cpool_lock);
669 [ # # ]: 0 : LIST_INSERT_HEAD(&priv->sh->hws_cpool_list, cpool, next);
670 : 0 : rte_spinlock_unlock(&priv->sh->cpool_lock);
671 : 0 : return cpool;
672 : 0 : error:
673 : 0 : mlx5_hws_cnt_pool_destroy(priv->sh, cpool);
674 : 0 : return NULL;
675 : : }
676 : :
677 : : void
678 : 0 : mlx5_hws_cnt_pool_destroy(struct mlx5_dev_ctx_shared *sh,
679 : : struct mlx5_hws_cnt_pool *cpool)
680 : : {
681 [ # # ]: 0 : if (cpool == NULL)
682 : : return;
683 : : /*
684 : : * 16M counter consumes 200ms to finish the query.
685 : : * Maybe blocked for at most 200ms here.
686 : : */
687 : 0 : rte_spinlock_lock(&sh->cpool_lock);
688 [ # # ]: 0 : LIST_REMOVE(cpool, next);
689 : : rte_spinlock_unlock(&sh->cpool_lock);
690 [ # # ]: 0 : if (cpool->cfg.host_cpool == NULL) {
691 [ # # ]: 0 : if (--sh->cnt_svc->refcnt == 0)
692 : 0 : mlx5_hws_cnt_svc_deinit(sh);
693 : : }
694 : 0 : mlx5_hws_cnt_pool_action_destroy(cpool);
695 [ # # ]: 0 : if (cpool->cfg.host_cpool == NULL) {
696 : 0 : mlx5_hws_cnt_pool_dcs_free(sh, cpool);
697 : 0 : mlx5_hws_cnt_raw_data_free(sh, cpool->raw_mng);
698 : : }
699 : 0 : mlx5_free((void *)cpool->cfg.name);
700 : 0 : mlx5_hws_cnt_pool_deinit(cpool);
701 : : }
702 : :
703 : : int
704 : 0 : mlx5_hws_cnt_svc_init(struct mlx5_dev_ctx_shared *sh)
705 : : {
706 : : int ret;
707 : :
708 : 0 : sh->cnt_svc = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
709 : : sizeof(*sh->cnt_svc), 0, SOCKET_ID_ANY);
710 [ # # ]: 0 : if (sh->cnt_svc == NULL)
711 : : return -1;
712 : 0 : sh->cnt_svc->query_interval = sh->config.cnt_svc.cycle_time;
713 : 0 : sh->cnt_svc->service_core = sh->config.cnt_svc.service_core;
714 : 0 : ret = mlx5_aso_cnt_queue_init(sh);
715 [ # # ]: 0 : if (ret != 0) {
716 : 0 : mlx5_free(sh->cnt_svc);
717 : 0 : sh->cnt_svc = NULL;
718 : 0 : return -1;
719 : : }
720 : 0 : ret = mlx5_hws_cnt_service_thread_create(sh);
721 [ # # ]: 0 : if (ret != 0) {
722 : 0 : mlx5_aso_cnt_queue_uninit(sh);
723 : 0 : mlx5_free(sh->cnt_svc);
724 : 0 : sh->cnt_svc = NULL;
725 : : }
726 : : return 0;
727 : : }
728 : :
729 : : void
730 : 0 : mlx5_hws_cnt_svc_deinit(struct mlx5_dev_ctx_shared *sh)
731 : : {
732 [ # # ]: 0 : if (sh->cnt_svc == NULL)
733 : : return;
734 : 0 : mlx5_hws_cnt_service_thread_destroy(sh);
735 : 0 : mlx5_aso_cnt_queue_uninit(sh);
736 : 0 : mlx5_free(sh->cnt_svc);
737 : 0 : sh->cnt_svc = NULL;
738 : : }
739 : :
740 : : /**
741 : : * Destroy AGE action.
742 : : *
743 : : * @param priv
744 : : * Pointer to the port private data structure.
745 : : * @param idx
746 : : * Index of AGE parameter.
747 : : * @param error
748 : : * Pointer to error structure.
749 : : *
750 : : * @return
751 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
752 : : */
753 : : int
754 : 0 : mlx5_hws_age_action_destroy(struct mlx5_priv *priv, uint32_t idx,
755 : : struct rte_flow_error *error)
756 : : {
757 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
758 : 0 : struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
759 : 0 : struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, idx);
760 : :
761 [ # # ]: 0 : if (param == NULL)
762 : 0 : return rte_flow_error_set(error, EINVAL,
763 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
764 : : "invalid AGE parameter index");
765 [ # # # ]: 0 : switch (__atomic_exchange_n(¶m->state, HWS_AGE_FREE,
766 : : __ATOMIC_RELAXED)) {
767 : 0 : case HWS_AGE_CANDIDATE:
768 : : case HWS_AGE_AGED_OUT_REPORTED:
769 : 0 : mlx5_hws_age_param_free(priv, param->own_cnt_index, ipool, idx);
770 : 0 : break;
771 : : case HWS_AGE_AGED_OUT_NOT_REPORTED:
772 : : case HWS_AGE_CANDIDATE_INSIDE_RING:
773 : : /*
774 : : * In both cases AGE is inside the ring. Change the state here
775 : : * and destroy it later when it is taken out of ring.
776 : : */
777 : : break;
778 : 0 : case HWS_AGE_FREE:
779 : : /*
780 : : * If index is valid and state is FREE, it says this AGE has
781 : : * been freed for the user but not for the PMD since it is
782 : : * inside the ring.
783 : : */
784 : 0 : return rte_flow_error_set(error, EINVAL,
785 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
786 : : "this AGE has already been released");
787 : : default:
788 : : MLX5_ASSERT(0);
789 : : break;
790 : : }
791 : : return 0;
792 : : }
793 : :
794 : : /**
795 : : * Create AGE action parameter.
796 : : *
797 : : * @param[in] priv
798 : : * Pointer to the port private data structure.
799 : : * @param[in] queue_id
800 : : * Which HWS queue to be used.
801 : : * @param[in] shared
802 : : * Whether it indirect AGE action.
803 : : * @param[in] flow_idx
804 : : * Flow index from indexed pool.
805 : : * For indirect AGE action it doesn't affect.
806 : : * @param[in] age
807 : : * Pointer to the aging action configuration.
808 : : * @param[out] error
809 : : * Pointer to error structure.
810 : : *
811 : : * @return
812 : : * Index to AGE action parameter on success, 0 otherwise.
813 : : */
814 : : uint32_t
815 : 0 : mlx5_hws_age_action_create(struct mlx5_priv *priv, uint32_t queue_id,
816 : : bool shared, const struct rte_flow_action_age *age,
817 : : uint32_t flow_idx, struct rte_flow_error *error)
818 : : {
819 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
820 : 0 : struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
821 : : struct mlx5_hws_age_param *param;
822 : : uint32_t age_idx;
823 : :
824 : 0 : param = mlx5_ipool_malloc(ipool, &age_idx);
825 [ # # ]: 0 : if (param == NULL) {
826 : 0 : rte_flow_error_set(error, ENOMEM,
827 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
828 : : "cannot allocate AGE parameter");
829 : 0 : return 0;
830 : : }
831 : : MLX5_ASSERT(__atomic_load_n(¶m->state,
832 : : __ATOMIC_RELAXED) == HWS_AGE_FREE);
833 [ # # ]: 0 : if (shared) {
834 : 0 : param->nb_cnts = 0;
835 : 0 : param->accumulator_hits = 0;
836 : 0 : param->accumulator_cnt = 0;
837 : 0 : flow_idx = age_idx;
838 : : } else {
839 : 0 : param->nb_cnts = 1;
840 : : }
841 [ # # ]: 0 : param->context = age->context ? age->context :
842 : 0 : (void *)(uintptr_t)flow_idx;
843 : 0 : param->timeout = age->timeout;
844 : 0 : param->queue_id = queue_id;
845 : 0 : param->accumulator_last_hits = 0;
846 : 0 : param->own_cnt_index = 0;
847 : 0 : param->sec_since_last_hit = 0;
848 : 0 : param->state = HWS_AGE_CANDIDATE;
849 : 0 : return age_idx;
850 : : }
851 : :
852 : : /**
853 : : * Update indirect AGE action parameter.
854 : : *
855 : : * @param[in] priv
856 : : * Pointer to the port private data structure.
857 : : * @param[in] idx
858 : : * Index of AGE parameter.
859 : : * @param[in] update
860 : : * Update value.
861 : : * @param[out] error
862 : : * Pointer to error structure.
863 : : *
864 : : * @return
865 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
866 : : */
867 : : int
868 : 0 : mlx5_hws_age_action_update(struct mlx5_priv *priv, uint32_t idx,
869 : : const void *update, struct rte_flow_error *error)
870 : : {
871 : : const struct rte_flow_update_age *update_ade = update;
872 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
873 : 0 : struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
874 : 0 : struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, idx);
875 : : bool sec_since_last_hit_reset = false;
876 : : bool state_update = false;
877 : :
878 [ # # ]: 0 : if (param == NULL)
879 : 0 : return rte_flow_error_set(error, EINVAL,
880 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
881 : : "invalid AGE parameter index");
882 [ # # ]: 0 : if (update_ade->timeout_valid) {
883 : 0 : uint32_t old_timeout = __atomic_exchange_n(¶m->timeout,
884 : 0 : update_ade->timeout,
885 : : __ATOMIC_RELAXED);
886 : :
887 [ # # ]: 0 : if (old_timeout == 0)
888 : : sec_since_last_hit_reset = true;
889 [ # # ]: 0 : else if (old_timeout < update_ade->timeout ||
890 [ # # ]: 0 : update_ade->timeout == 0)
891 : : /*
892 : : * When timeout is increased, aged-out flows might be
893 : : * active again and state should be updated accordingly.
894 : : * When new timeout is 0, we update the state for not
895 : : * reporting aged-out stopped.
896 : : */
897 : : state_update = true;
898 : : }
899 [ # # ]: 0 : if (update_ade->touch) {
900 : : sec_since_last_hit_reset = true;
901 : : state_update = true;
902 : : }
903 [ # # ]: 0 : if (sec_since_last_hit_reset)
904 : 0 : __atomic_store_n(¶m->sec_since_last_hit, 0,
905 : : __ATOMIC_RELAXED);
906 [ # # ]: 0 : if (state_update) {
907 : : uint16_t expected = HWS_AGE_AGED_OUT_NOT_REPORTED;
908 : :
909 : : /*
910 : : * Change states of aged-out flows to active:
911 : : * - AGED_OUT_NOT_REPORTED -> CANDIDATE_INSIDE_RING
912 : : * - AGED_OUT_REPORTED -> CANDIDATE
913 : : */
914 [ # # ]: 0 : if (!__atomic_compare_exchange_n(¶m->state, &expected,
915 : : HWS_AGE_CANDIDATE_INSIDE_RING,
916 : : false, __ATOMIC_RELAXED,
917 [ # # ]: 0 : __ATOMIC_RELAXED) &&
918 : : expected == HWS_AGE_AGED_OUT_REPORTED)
919 : 0 : __atomic_store_n(¶m->state, HWS_AGE_CANDIDATE,
920 : : __ATOMIC_RELAXED);
921 : : }
922 : : return 0;
923 : : }
924 : :
925 : : /**
926 : : * Get the AGE context if the aged-out index is still valid.
927 : : *
928 : : * @param priv
929 : : * Pointer to the port private data structure.
930 : : * @param idx
931 : : * Index of AGE parameter.
932 : : *
933 : : * @return
934 : : * AGE context if the index is still aged-out, NULL otherwise.
935 : : */
936 : : void *
937 : 0 : mlx5_hws_age_context_get(struct mlx5_priv *priv, uint32_t idx)
938 : : {
939 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
940 : 0 : struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
941 : 0 : struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, idx);
942 : : uint16_t expected = HWS_AGE_AGED_OUT_NOT_REPORTED;
943 : :
944 : : MLX5_ASSERT(param != NULL);
945 [ # # ]: 0 : if (__atomic_compare_exchange_n(¶m->state, &expected,
946 : : HWS_AGE_AGED_OUT_REPORTED, false,
947 : : __ATOMIC_RELAXED, __ATOMIC_RELAXED))
948 : 0 : return param->context;
949 [ # # # ]: 0 : switch (expected) {
950 : 0 : case HWS_AGE_FREE:
951 : : /*
952 : : * This AGE couldn't have been destroyed since it was inside
953 : : * the ring. Its state has updated, and now it is actually
954 : : * destroyed.
955 : : */
956 : 0 : mlx5_hws_age_param_free(priv, param->own_cnt_index, ipool, idx);
957 : 0 : break;
958 : 0 : case HWS_AGE_CANDIDATE_INSIDE_RING:
959 : 0 : __atomic_store_n(¶m->state, HWS_AGE_CANDIDATE,
960 : : __ATOMIC_RELAXED);
961 : 0 : break;
962 : : case HWS_AGE_CANDIDATE:
963 : : /*
964 : : * Only BG thread pushes to ring and it never pushes this state.
965 : : * When AGE inside the ring becomes candidate, it has a special
966 : : * state called HWS_AGE_CANDIDATE_INSIDE_RING.
967 : : * Fall-through.
968 : : */
969 : : case HWS_AGE_AGED_OUT_REPORTED:
970 : : /*
971 : : * Only this thread (doing query) may write this state, and it
972 : : * happens only after the query thread takes it out of the ring.
973 : : * Fall-through.
974 : : */
975 : : case HWS_AGE_AGED_OUT_NOT_REPORTED:
976 : : /*
977 : : * In this case the compare return true and function return
978 : : * the context immediately.
979 : : * Fall-through.
980 : : */
981 : : default:
982 : : MLX5_ASSERT(0);
983 : : break;
984 : : }
985 : : return NULL;
986 : : }
987 : :
988 : : #ifdef RTE_ARCH_64
989 : : #define MLX5_HWS_AGED_OUT_RING_SIZE_MAX UINT32_MAX
990 : : #else
991 : : #define MLX5_HWS_AGED_OUT_RING_SIZE_MAX RTE_BIT32(8)
992 : : #endif
993 : :
994 : : /**
995 : : * Get the size of aged out ring list for each queue.
996 : : *
997 : : * The size is one percent of nb_counters divided by nb_queues.
998 : : * The ring size must be power of 2, so it align up to power of 2.
999 : : * In 32 bit systems, the size is limited by 256.
1000 : : *
1001 : : * This function is called when RTE_FLOW_PORT_FLAG_STRICT_QUEUE is on.
1002 : : *
1003 : : * @param nb_counters
1004 : : * Final number of allocated counter in the pool.
1005 : : * @param nb_queues
1006 : : * Number of HWS queues in this port.
1007 : : *
1008 : : * @return
1009 : : * Size of aged out ring per queue.
1010 : : */
1011 : : static __rte_always_inline uint32_t
1012 : : mlx5_hws_aged_out_q_ring_size_get(uint32_t nb_counters, uint32_t nb_queues)
1013 : : {
1014 : 0 : uint32_t size = rte_align32pow2((nb_counters / 100) / nb_queues);
1015 : : uint32_t max_size = MLX5_HWS_AGED_OUT_RING_SIZE_MAX;
1016 : :
1017 : : return RTE_MIN(size, max_size);
1018 : : }
1019 : :
1020 : : /**
1021 : : * Get the size of the aged out ring list.
1022 : : *
1023 : : * The size is one percent of nb_counters.
1024 : : * The ring size must be power of 2, so it align up to power of 2.
1025 : : * In 32 bit systems, the size is limited by 256.
1026 : : *
1027 : : * This function is called when RTE_FLOW_PORT_FLAG_STRICT_QUEUE is off.
1028 : : *
1029 : : * @param nb_counters
1030 : : * Final number of allocated counter in the pool.
1031 : : *
1032 : : * @return
1033 : : * Size of the aged out ring list.
1034 : : */
1035 : : static __rte_always_inline uint32_t
1036 : : mlx5_hws_aged_out_ring_size_get(uint32_t nb_counters)
1037 : : {
1038 : 0 : uint32_t size = rte_align32pow2(nb_counters / 100);
1039 : : uint32_t max_size = MLX5_HWS_AGED_OUT_RING_SIZE_MAX;
1040 : :
1041 : : return RTE_MIN(size, max_size);
1042 : : }
1043 : :
1044 : : /**
1045 : : * Initialize the shared aging list information per port.
1046 : : *
1047 : : * @param dev
1048 : : * Pointer to the rte_eth_dev structure.
1049 : : * @param nb_queues
1050 : : * Number of HWS queues.
1051 : : * @param strict_queue
1052 : : * Indicator whether is strict_queue mode.
1053 : : * @param ring_size
1054 : : * Size of aged-out ring for creation.
1055 : : *
1056 : : * @return
1057 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1058 : : */
1059 : : static int
1060 : 0 : mlx5_hws_age_info_init(struct rte_eth_dev *dev, uint16_t nb_queues,
1061 : : bool strict_queue, uint32_t ring_size)
1062 : : {
1063 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1064 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
1065 : : uint32_t flags = RING_F_SP_ENQ | RING_F_SC_DEQ | RING_F_EXACT_SZ;
1066 : : char mz_name[RTE_MEMZONE_NAMESIZE];
1067 : : struct rte_ring *r = NULL;
1068 : : uint32_t qidx;
1069 : :
1070 : 0 : age_info->flags = 0;
1071 [ # # ]: 0 : if (strict_queue) {
1072 : 0 : size_t size = sizeof(*age_info->hw_q_age) +
1073 : : sizeof(struct rte_ring *) * nb_queues;
1074 : :
1075 : 0 : age_info->hw_q_age = mlx5_malloc(MLX5_MEM_ANY | MLX5_MEM_ZERO,
1076 : : size, 0, SOCKET_ID_ANY);
1077 [ # # ]: 0 : if (age_info->hw_q_age == NULL)
1078 : : return -ENOMEM;
1079 [ # # ]: 0 : for (qidx = 0; qidx < nb_queues; ++qidx) {
1080 : 0 : snprintf(mz_name, sizeof(mz_name),
1081 : : "port_%u_queue_%u_aged_out_ring",
1082 : 0 : dev->data->port_id, qidx);
1083 : 0 : r = rte_ring_create(mz_name, ring_size, SOCKET_ID_ANY,
1084 : : flags);
1085 [ # # ]: 0 : if (r == NULL) {
1086 : 0 : DRV_LOG(ERR, "\"%s\" creation failed: %s",
1087 : : mz_name, rte_strerror(rte_errno));
1088 : 0 : goto error;
1089 : : }
1090 : 0 : age_info->hw_q_age->aged_lists[qidx] = r;
1091 : 0 : DRV_LOG(DEBUG,
1092 : : "\"%s\" is successfully created (size=%u).",
1093 : : mz_name, ring_size);
1094 : : }
1095 : 0 : age_info->hw_q_age->nb_rings = nb_queues;
1096 : : } else {
1097 : 0 : snprintf(mz_name, sizeof(mz_name), "port_%u_aged_out_ring",
1098 : 0 : dev->data->port_id);
1099 : 0 : r = rte_ring_create(mz_name, ring_size, SOCKET_ID_ANY, flags);
1100 [ # # ]: 0 : if (r == NULL) {
1101 : 0 : DRV_LOG(ERR, "\"%s\" creation failed: %s", mz_name,
1102 : : rte_strerror(rte_errno));
1103 : 0 : return -rte_errno;
1104 : : }
1105 : 0 : age_info->hw_age.aged_list = r;
1106 : 0 : DRV_LOG(DEBUG, "\"%s\" is successfully created (size=%u).",
1107 : : mz_name, ring_size);
1108 : : /* In non "strict_queue" mode, initialize the event. */
1109 : 0 : MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
1110 : : }
1111 : : return 0;
1112 : : error:
1113 : : MLX5_ASSERT(strict_queue);
1114 [ # # ]: 0 : while (qidx--)
1115 : 0 : rte_ring_free(age_info->hw_q_age->aged_lists[qidx]);
1116 : 0 : mlx5_free(age_info->hw_q_age);
1117 : 0 : return -1;
1118 : : }
1119 : :
1120 : : /**
1121 : : * Cleanup aged-out ring before destroying.
1122 : : *
1123 : : * @param priv
1124 : : * Pointer to port private object.
1125 : : * @param r
1126 : : * Pointer to aged-out ring object.
1127 : : */
1128 : : static void
1129 : 0 : mlx5_hws_aged_out_ring_cleanup(struct mlx5_priv *priv, struct rte_ring *r)
1130 : : {
1131 : 0 : int ring_size = rte_ring_count(r);
1132 : :
1133 [ # # ]: 0 : while (ring_size > 0) {
1134 [ # # # # : 0 : uint32_t age_idx = 0;
# ]
1135 : :
1136 : : if (rte_ring_dequeue_elem(r, &age_idx, sizeof(uint32_t)) < 0)
1137 : : break;
1138 : : /* get the AGE context if the aged-out index is still valid. */
1139 : 0 : mlx5_hws_age_context_get(priv, age_idx);
1140 : 0 : ring_size--;
1141 : : }
1142 : 0 : rte_ring_free(r);
1143 : 0 : }
1144 : :
1145 : : /**
1146 : : * Destroy the shared aging list information per port.
1147 : : *
1148 : : * @param priv
1149 : : * Pointer to port private object.
1150 : : */
1151 : : static void
1152 : 0 : mlx5_hws_age_info_destroy(struct mlx5_priv *priv)
1153 : : {
1154 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
1155 : 0 : uint16_t nb_queues = age_info->hw_q_age->nb_rings;
1156 : : struct rte_ring *r;
1157 : :
1158 [ # # ]: 0 : if (priv->hws_strict_queue) {
1159 : : uint32_t qidx;
1160 : :
1161 [ # # ]: 0 : for (qidx = 0; qidx < nb_queues; ++qidx) {
1162 : 0 : r = age_info->hw_q_age->aged_lists[qidx];
1163 : 0 : mlx5_hws_aged_out_ring_cleanup(priv, r);
1164 : : }
1165 : 0 : mlx5_free(age_info->hw_q_age);
1166 : : } else {
1167 : : r = age_info->hw_age.aged_list;
1168 : 0 : mlx5_hws_aged_out_ring_cleanup(priv, r);
1169 : : }
1170 : 0 : }
1171 : :
1172 : : /**
1173 : : * Initialize the aging mechanism per port.
1174 : : *
1175 : : * @param dev
1176 : : * Pointer to the rte_eth_dev structure.
1177 : : * @param attr
1178 : : * Port configuration attributes.
1179 : : * @param nb_queues
1180 : : * Number of HWS queues.
1181 : : *
1182 : : * @return
1183 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1184 : : */
1185 : : int
1186 : 0 : mlx5_hws_age_pool_init(struct rte_eth_dev *dev,
1187 : : const struct rte_flow_port_attr *attr,
1188 : : uint16_t nb_queues)
1189 : : {
1190 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1191 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
1192 : 0 : struct mlx5_indexed_pool_config cfg = {
1193 : : .size =
1194 : : RTE_CACHE_LINE_ROUNDUP(sizeof(struct mlx5_hws_age_param)),
1195 : : .trunk_size = 1 << 12,
1196 : : .per_core_cache = 1 << 13,
1197 : : .need_lock = 1,
1198 : 0 : .release_mem_en = !!priv->sh->config.reclaim_mode,
1199 : : .malloc = mlx5_malloc,
1200 : : .free = mlx5_free,
1201 : : .type = "mlx5_hws_age_pool",
1202 : : };
1203 : : bool strict_queue = false;
1204 : : uint32_t nb_alloc_cnts;
1205 : : uint32_t rsize;
1206 : : uint32_t nb_ages_updated;
1207 : : int ret;
1208 : :
1209 : 0 : strict_queue = !!(attr->flags & RTE_FLOW_PORT_FLAG_STRICT_QUEUE);
1210 : : MLX5_ASSERT(priv->hws_cpool);
1211 [ # # ]: 0 : if (attr->flags & RTE_FLOW_PORT_FLAG_SHARE_INDIRECT) {
1212 : 0 : DRV_LOG(ERR, "Aging sn not supported "
1213 : : "in cross vHCA sharing mode");
1214 : 0 : rte_errno = ENOTSUP;
1215 : 0 : return -ENOTSUP;
1216 : : }
1217 [ # # ]: 0 : nb_alloc_cnts = mlx5_hws_cnt_pool_get_size(priv->hws_cpool);
1218 [ # # ]: 0 : if (strict_queue) {
1219 : 0 : rsize = mlx5_hws_aged_out_q_ring_size_get(nb_alloc_cnts,
1220 : : nb_queues);
1221 : 0 : nb_ages_updated = rsize * nb_queues + attr->nb_aging_objects;
1222 : : } else {
1223 : : rsize = mlx5_hws_aged_out_ring_size_get(nb_alloc_cnts);
1224 : 0 : nb_ages_updated = rsize + attr->nb_aging_objects;
1225 : : }
1226 : 0 : ret = mlx5_hws_age_info_init(dev, nb_queues, strict_queue, rsize);
1227 [ # # ]: 0 : if (ret < 0)
1228 : : return ret;
1229 : 0 : cfg.max_idx = rte_align32pow2(nb_ages_updated);
1230 [ # # ]: 0 : if (cfg.max_idx <= cfg.trunk_size) {
1231 : 0 : cfg.per_core_cache = 0;
1232 : 0 : cfg.trunk_size = cfg.max_idx;
1233 [ # # ]: 0 : } else if (cfg.max_idx <= MLX5_HW_IPOOL_SIZE_THRESHOLD) {
1234 : 0 : cfg.per_core_cache = MLX5_HW_IPOOL_CACHE_MIN;
1235 : : }
1236 : 0 : age_info->ages_ipool = mlx5_ipool_create(&cfg);
1237 [ # # ]: 0 : if (age_info->ages_ipool == NULL) {
1238 : 0 : mlx5_hws_age_info_destroy(priv);
1239 : 0 : rte_errno = ENOMEM;
1240 : 0 : return -rte_errno;
1241 : : }
1242 : 0 : priv->hws_age_req = 1;
1243 : 0 : return 0;
1244 : : }
1245 : :
1246 : : /**
1247 : : * Cleanup all aging resources per port.
1248 : : *
1249 : : * @param priv
1250 : : * Pointer to port private object.
1251 : : */
1252 : : void
1253 : 0 : mlx5_hws_age_pool_destroy(struct mlx5_priv *priv)
1254 : : {
1255 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
1256 : :
1257 : 0 : rte_spinlock_lock(&priv->sh->cpool_lock);
1258 : : MLX5_ASSERT(priv->hws_age_req);
1259 : 0 : mlx5_hws_age_info_destroy(priv);
1260 : 0 : mlx5_ipool_destroy(age_info->ages_ipool);
1261 : 0 : age_info->ages_ipool = NULL;
1262 : 0 : priv->hws_age_req = 0;
1263 : 0 : rte_spinlock_unlock(&priv->sh->cpool_lock);
1264 : 0 : }
1265 : :
1266 : : #endif
|