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