Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2022 Mellanox Technologies, Ltd
3 : : */
4 : :
5 : : #ifndef _MLX5_HWS_CNT_H_
6 : : #define _MLX5_HWS_CNT_H_
7 : :
8 : : #include <rte_memzone.h>
9 : : #include <rte_ring.h>
10 : : #include "mlx5_utils.h"
11 : : #include "mlx5_flow.h"
12 : :
13 : : /*
14 : : * HWS COUNTER ID's layout
15 : : * 3 2 1 0
16 : : * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
17 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18 : : * | T | | D | |
19 : : * ~ Y | | C | IDX ~
20 : : * | P | | S | |
21 : : * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 : : *
23 : : * Bit 31:29 = TYPE = MLX5_INDIRECT_ACTION_TYPE_COUNT = b'10
24 : : * Bit 25:24 = DCS index
25 : : * Bit 23:00 = IDX in this counter belonged DCS bulk.
26 : : */
27 : :
28 : : #define MLX5_HWS_CNT_DCS_IDX_OFFSET 24
29 : : #define MLX5_HWS_CNT_DCS_IDX_MASK 0x3
30 : : #define MLX5_HWS_CNT_IDX_MASK ((1UL << MLX5_HWS_CNT_DCS_IDX_OFFSET) - 1)
31 : :
32 : : #define MLX5_HWS_AGE_IDX_MASK (RTE_BIT32(MLX5_INDIRECT_ACTION_TYPE_OFFSET) - 1)
33 : :
34 : : struct mlx5_hws_cnt_dcs {
35 : : struct mlx5dr_action *root_action; /* mlx5dr action used on root groups. */
36 : : struct mlx5dr_action *hws_action; /* mlx5dr action used on non-root groups. */
37 : : uint32_t batch_sz;
38 : : uint32_t iidx; /* internal index of first counter in this bulk. */
39 : : struct mlx5_devx_obj *obj;
40 : : };
41 : :
42 : : struct mlx5_hws_cnt_dcs_mng {
43 : : uint32_t batch_total;
44 : : struct mlx5_hws_cnt_dcs dcs[MLX5_HWS_CNT_DCS_NUM];
45 : : };
46 : :
47 : : union mlx5_hws_cnt_state {
48 : : RTE_ATOMIC(uint32_t) data;
49 : : struct {
50 : : uint32_t in_used:1;
51 : : /* Indicator whether this counter in used or in pool. */
52 : : uint32_t share:1;
53 : : /*
54 : : * share will be set to 1 when this counter is used as
55 : : * indirect action.
56 : : */
57 : : uint32_t age_idx:24;
58 : : /*
59 : : * When this counter uses for aging, it stores the index
60 : : * of AGE parameter. Otherwise, this index is zero.
61 : : */
62 : : };
63 : : };
64 : :
65 : : struct mlx5_hws_cnt {
66 : : struct flow_counter_stats reset;
67 : : union mlx5_hws_cnt_state cnt_state;
68 : : /* This struct is only meaningful when user own this counter. */
69 : : RTE_ATOMIC(uint32_t) query_gen_when_free;
70 : : /*
71 : : * When PMD own this counter (user put back counter to PMD
72 : : * counter pool, i.e), this field recorded value of counter
73 : : * pools query generation at time user release the counter.
74 : : */
75 : : };
76 : :
77 : : struct mlx5_hws_cnt_raw_data_mng {
78 : : struct flow_counter_stats *raw;
79 : : struct mlx5_pmd_mr mr;
80 : : };
81 : :
82 : : struct mlx5_hws_cache_param {
83 : : uint32_t size;
84 : : uint32_t q_num;
85 : : uint32_t fetch_sz;
86 : : uint32_t threshold;
87 : : uint32_t preload_sz;
88 : : };
89 : :
90 : : struct mlx5_hws_cnt_pool_cfg {
91 : : char name[RTE_MEMZONE_NAMESIZE];
92 : : uint32_t request_num;
93 : : uint32_t alloc_factor;
94 : : struct mlx5_hws_cnt_pool *host_cpool;
95 : : };
96 : :
97 : : struct mlx5_hws_cnt_pool_caches {
98 : : uint32_t fetch_sz;
99 : : uint32_t threshold;
100 : : uint32_t preload_sz;
101 : : uint32_t q_num;
102 : : struct rte_ring *qcache[];
103 : : };
104 : :
105 : : struct __rte_cache_aligned mlx5_hws_cnt_pool {
106 : : LIST_ENTRY(mlx5_hws_cnt_pool) next;
107 : : alignas(RTE_CACHE_LINE_SIZE) struct mlx5_hws_cnt_pool_cfg cfg;
108 : : alignas(RTE_CACHE_LINE_SIZE) struct mlx5_hws_cnt_dcs_mng dcs_mng;
109 : : alignas(RTE_CACHE_LINE_SIZE) RTE_ATOMIC(uint32_t) query_gen;
110 : : struct mlx5_hws_cnt *pool;
111 : : struct mlx5_hws_cnt_raw_data_mng *raw_mng;
112 : : struct rte_ring *reuse_list;
113 : : struct rte_ring *free_list;
114 : : struct rte_ring *wait_reset_list;
115 : : struct mlx5_hws_cnt_pool_caches *cache;
116 : : uint64_t time_of_last_age_check;
117 : : struct mlx5_priv *priv;
118 : : };
119 : :
120 : : /* HWS AGE status. */
121 : : enum {
122 : : HWS_AGE_FREE, /* Initialized state. */
123 : : HWS_AGE_CANDIDATE, /* AGE assigned to flows. */
124 : : HWS_AGE_CANDIDATE_INSIDE_RING,
125 : : /*
126 : : * AGE assigned to flows but it still in ring. It was aged-out but the
127 : : * timeout was changed, so it in ring but still candidate.
128 : : */
129 : : HWS_AGE_AGED_OUT_REPORTED,
130 : : /*
131 : : * Aged-out, reported by rte_flow_get_q_aged_flows and wait for destroy.
132 : : */
133 : : HWS_AGE_AGED_OUT_NOT_REPORTED,
134 : : /*
135 : : * Aged-out, inside the aged-out ring.
136 : : * wait for rte_flow_get_q_aged_flows and destroy.
137 : : */
138 : : };
139 : :
140 : : /* HWS counter age parameter. */
141 : : struct __rte_cache_aligned __rte_packed_begin mlx5_hws_age_param {
142 : : RTE_ATOMIC(uint32_t) timeout; /* Aging timeout in seconds (atomically accessed). */
143 : : RTE_ATOMIC(uint32_t) sec_since_last_hit;
144 : : /* Time in seconds since last hit (atomically accessed). */
145 : : RTE_ATOMIC(uint16_t) state; /* AGE state (atomically accessed). */
146 : : uint64_t accumulator_last_hits;
147 : : /* Last total value of hits for comparing. */
148 : : uint64_t accumulator_hits;
149 : : /* Accumulator for hits coming from several counters. */
150 : : uint32_t accumulator_cnt;
151 : : /* Number counters which already updated the accumulator in this sec. */
152 : : uint32_t nb_cnts; /* Number counters used by this AGE. */
153 : : uint32_t queue_id; /* Queue id of the counter. */
154 : : cnt_id_t own_cnt_index;
155 : : /* Counter action created specifically for this AGE action. */
156 : : void *context; /* Flow AGE context. */
157 : : } __rte_packed_end;
158 : :
159 : :
160 : : /**
161 : : * Return the actual counter pool should be used in cross vHCA sharing mode.
162 : : * as index of raw/cnt pool.
163 : : *
164 : : * @param cnt_id
165 : : * The external counter id
166 : : * @return
167 : : * Internal index
168 : : */
169 : : static __rte_always_inline struct mlx5_hws_cnt_pool *
170 : : mlx5_hws_cnt_host_pool(struct mlx5_hws_cnt_pool *cpool)
171 : : {
172 [ # # # # : 0 : return cpool->cfg.host_cpool ? cpool->cfg.host_cpool : cpool;
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
173 : : }
174 : :
175 : : /**
176 : : * Translate counter id into internal index (start from 0), which can be used
177 : : * as index of raw/cnt pool.
178 : : *
179 : : * @param cnt_id
180 : : * The external counter id
181 : : * @return
182 : : * Internal index
183 : : */
184 : : static __rte_always_inline uint32_t
185 : : mlx5_hws_cnt_iidx(struct mlx5_hws_cnt_pool *cpool, cnt_id_t cnt_id)
186 : : {
187 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
188 : 0 : uint8_t dcs_idx = cnt_id >> MLX5_HWS_CNT_DCS_IDX_OFFSET;
189 : 0 : uint32_t offset = cnt_id & MLX5_HWS_CNT_IDX_MASK;
190 : :
191 : 0 : dcs_idx &= MLX5_HWS_CNT_DCS_IDX_MASK;
192 : 0 : return (hpool->dcs_mng.dcs[dcs_idx].iidx + offset);
193 : : }
194 : :
195 : : /**
196 : : * Check if it's valid counter id.
197 : : */
198 : : static __rte_always_inline bool
199 : : mlx5_hws_cnt_id_valid(cnt_id_t cnt_id)
200 : : {
201 [ # # # # : 0 : return (cnt_id >> MLX5_INDIRECT_ACTION_TYPE_OFFSET) ==
# # # # #
# # # #
# ]
202 : : MLX5_INDIRECT_ACTION_TYPE_COUNT ? true : false;
203 : : }
204 : :
205 : : static __rte_always_inline void
206 : : mlx5_hws_cnt_set_age_idx(struct mlx5_hws_cnt *cnt, uint32_t value)
207 : : {
208 : : union mlx5_hws_cnt_state cnt_state;
209 : :
210 : 0 : cnt_state.data = rte_atomic_load_explicit(&cnt->cnt_state.data, rte_memory_order_acquire);
211 : 0 : cnt_state.age_idx = value;
212 : 0 : rte_atomic_store_explicit(&cnt->cnt_state.data, cnt_state.data, rte_memory_order_release);
213 : : }
214 : :
215 : : static __rte_always_inline void
216 : : mlx5_hws_cnt_set_all(struct mlx5_hws_cnt *cnt, uint32_t in_used, uint32_t share, uint32_t age_idx)
217 : : {
218 : : union mlx5_hws_cnt_state cnt_state;
219 : :
220 : 0 : cnt_state.in_used = !!in_used;
221 : 0 : cnt_state.share = !!share;
222 : 0 : cnt_state.age_idx = age_idx;
223 : 0 : rte_atomic_store_explicit(&cnt->cnt_state.data, cnt_state.data, rte_memory_order_relaxed);
224 : : }
225 : :
226 : : static __rte_always_inline void
227 : : mlx5_hws_cnt_get_all(struct mlx5_hws_cnt *cnt, uint32_t *in_used, uint32_t *share,
228 : : uint32_t *age_idx)
229 : : {
230 : : union mlx5_hws_cnt_state cnt_state;
231 : :
232 : 0 : cnt_state.data = rte_atomic_load_explicit(&cnt->cnt_state.data, rte_memory_order_acquire);
233 : : if (in_used != NULL)
234 : 0 : *in_used = cnt_state.in_used;
235 : : if (share != NULL)
236 : 0 : *share = cnt_state.share;
237 : : if (age_idx != NULL)
238 [ # # ]: 0 : *age_idx = cnt_state.age_idx;
239 : : }
240 : :
241 : : /**
242 : : * Generate Counter id from internal index.
243 : : *
244 : : * @param cpool
245 : : * The pointer to counter pool
246 : : * @param iidx
247 : : * The internal counter index.
248 : : *
249 : : * @return
250 : : * Counter id
251 : : */
252 : : static __rte_always_inline cnt_id_t
253 : : mlx5_hws_cnt_id_gen(struct mlx5_hws_cnt_pool *cpool, uint32_t iidx)
254 : : {
255 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
256 : : struct mlx5_hws_cnt_dcs_mng *dcs_mng = &hpool->dcs_mng;
257 : : uint32_t idx;
258 : : uint32_t offset;
259 : : cnt_id_t cnt_id;
260 : :
261 [ # # ]: 0 : for (idx = 0, offset = iidx; idx < dcs_mng->batch_total; idx++) {
262 [ # # ]: 0 : if (dcs_mng->dcs[idx].batch_sz <= offset)
263 : 0 : offset -= dcs_mng->dcs[idx].batch_sz;
264 : : else
265 : : break;
266 : : }
267 : : cnt_id = offset;
268 : 0 : cnt_id |= (idx << MLX5_HWS_CNT_DCS_IDX_OFFSET);
269 : : return (MLX5_INDIRECT_ACTION_TYPE_COUNT <<
270 : 0 : MLX5_INDIRECT_ACTION_TYPE_OFFSET) | cnt_id;
271 : : }
272 : :
273 : : static __rte_always_inline void
274 : : __hws_cnt_query_raw(struct mlx5_hws_cnt_pool *cpool, cnt_id_t cnt_id,
275 : : uint64_t *raw_pkts, uint64_t *raw_bytes)
276 : : {
277 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
278 : 0 : struct mlx5_hws_cnt_raw_data_mng *raw_mng = hpool->raw_mng;
279 : : struct flow_counter_stats s[2];
280 : : uint8_t i = 0x1;
281 : : size_t stat_sz = sizeof(s[0]);
282 : : uint32_t iidx = mlx5_hws_cnt_iidx(cpool, cnt_id);
283 : :
284 : 0 : memcpy(&s[0], &raw_mng->raw[iidx], stat_sz);
285 : : do {
286 [ # # # # : 0 : memcpy(&s[i & 1], &raw_mng->raw[iidx], stat_sz);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
287 [ # # # # : 0 : if (memcmp(&s[0], &s[1], stat_sz) == 0) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
288 [ # # # # : 0 : *raw_pkts = rte_be_to_cpu_64(s[0].hits);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
289 [ # # # # : 0 : *raw_bytes = rte_be_to_cpu_64(s[0].bytes);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # ]
290 : : break;
291 : : }
292 : 0 : i = ~i;
293 : : } while (1);
294 : : }
295 : :
296 : : /**
297 : : * Copy elements from one zero-copy ring to zero-copy ring in place.
298 : : *
299 : : * The input is a rte ring zero-copy data struct, which has two pointer.
300 : : * in case of the wrapper happened, the ptr2 will be meaningful.
301 : : *
302 : : * So this routine needs to consider the situation that the address given by
303 : : * source and destination could be both wrapped.
304 : : * First, calculate the first number of element needs to be copied until wrapped
305 : : * address, which could be in source or destination.
306 : : * Second, copy left number of element until second wrapped address. If in first
307 : : * step the wrapped address is source, then this time it must be in destination.
308 : : * and vice-versa.
309 : : * Third, copy all left number of element.
310 : : *
311 : : * In worst case, we need copy three pieces of continuous memory.
312 : : *
313 : : * @param zcdd
314 : : * A pointer to zero-copy data of destination ring.
315 : : * @param zcds
316 : : * A pointer to zero-copy data of source ring.
317 : : * @param n
318 : : * Number of elements to copy.
319 : : */
320 : : static __rte_always_inline void
321 : : __hws_cnt_r2rcpy(struct rte_ring_zc_data *zcdd, struct rte_ring_zc_data *zcds,
322 : : unsigned int n)
323 : : {
324 : : unsigned int n1, n2, n3;
325 : : void *s1, *s2, *s3;
326 : : void *d1, *d2, *d3;
327 : :
328 : : s1 = zcds->ptr1;
329 : : d1 = zcdd->ptr1;
330 : 0 : n1 = RTE_MIN(zcdd->n1, zcds->n1);
331 [ # # # # : 0 : if (zcds->n1 > n1) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
332 : 0 : n2 = zcds->n1 - n1;
333 : 0 : s2 = RTE_PTR_ADD(zcds->ptr1, sizeof(cnt_id_t) * n1);
334 : : d2 = zcdd->ptr2;
335 : 0 : n3 = n - n1 - n2;
336 : : s3 = zcds->ptr2;
337 : 0 : d3 = RTE_PTR_ADD(zcdd->ptr2, sizeof(cnt_id_t) * n2);
338 : : } else {
339 : 0 : n2 = zcdd->n1 - n1;
340 : : s2 = zcds->ptr2;
341 : 0 : d2 = RTE_PTR_ADD(zcdd->ptr1, sizeof(cnt_id_t) * n1);
342 : 0 : n3 = n - n1 - n2;
343 : 0 : s3 = RTE_PTR_ADD(zcds->ptr2, sizeof(cnt_id_t) * n2);
344 : : d3 = zcdd->ptr2;
345 : : }
346 [ # # # # : 0 : memcpy(d1, s1, n1 * sizeof(cnt_id_t));
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
347 [ # # # # : 0 : if (n2 != 0)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
348 : 0 : memcpy(d2, s2, n2 * sizeof(cnt_id_t));
349 [ # # # # : 0 : if (n3 != 0)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ]
350 : 0 : memcpy(d3, s3, n3 * sizeof(cnt_id_t));
351 : : }
352 : :
353 : : static __rte_always_inline int
354 : : mlx5_hws_cnt_pool_cache_flush(struct mlx5_hws_cnt_pool *cpool,
355 : : uint32_t queue_id)
356 : : {
357 : : unsigned int ret __rte_unused;
358 : : struct rte_ring_zc_data zcdr = {0};
359 : : struct rte_ring_zc_data zcdc = {0};
360 : : struct rte_ring *reset_list = NULL;
361 : 0 : struct rte_ring *qcache = cpool->cache->qcache[queue_id];
362 : 0 : uint32_t ring_size = rte_ring_count(qcache);
363 : :
364 : : ret = rte_ring_dequeue_zc_burst_elem_start(qcache, sizeof(cnt_id_t),
365 : : ring_size, &zcdc, NULL);
366 : : MLX5_ASSERT(ret == ring_size);
367 [ # # # # : 0 : reset_list = cpool->wait_reset_list;
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
368 : : ret = rte_ring_enqueue_zc_burst_elem_start(reset_list, sizeof(cnt_id_t),
369 : : ring_size, &zcdr, NULL);
370 : : MLX5_ASSERT(ret == ring_size);
371 : : __hws_cnt_r2rcpy(&zcdr, &zcdc, ring_size);
372 : : rte_ring_enqueue_zc_elem_finish(reset_list, ring_size);
373 : : rte_ring_dequeue_zc_elem_finish(qcache, ring_size);
374 : : return 0;
375 : : }
376 : :
377 : : static __rte_always_inline int
378 : : mlx5_hws_cnt_pool_cache_fetch(struct mlx5_hws_cnt_pool *cpool,
379 : : uint32_t queue_id)
380 : : {
381 : 0 : struct rte_ring *qcache = cpool->cache->qcache[queue_id];
382 : : struct rte_ring *free_list = NULL;
383 : : struct rte_ring *reuse_list = NULL;
384 : : struct rte_ring *list = NULL;
385 : : struct rte_ring_zc_data zcdf = {0};
386 : : struct rte_ring_zc_data zcdc = {0};
387 : : struct rte_ring_zc_data zcdu = {0};
388 : : struct rte_ring_zc_data zcds = {0};
389 : : struct mlx5_hws_cnt_pool_caches *cache = cpool->cache;
390 : : unsigned int ret, actual_fetch_size __rte_unused;
391 : :
392 : 0 : reuse_list = cpool->reuse_list;
393 : 0 : ret = rte_ring_dequeue_zc_burst_elem_start(reuse_list,
394 : : sizeof(cnt_id_t), cache->fetch_sz, &zcdu, NULL);
395 : : zcds = zcdu;
396 : : list = reuse_list;
397 [ # # # # : 0 : if (unlikely(ret == 0)) { /* no reuse counter. */
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
398 : : rte_ring_dequeue_zc_elem_finish(reuse_list, 0);
399 : 0 : free_list = cpool->free_list;
400 [ # # # # : 0 : ret = rte_ring_dequeue_zc_burst_elem_start(free_list,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
401 : : sizeof(cnt_id_t),
402 : : cache->fetch_sz,
403 : : &zcdf, NULL);
404 : : zcds = zcdf;
405 : : list = free_list;
406 [ # # # # : 0 : if (unlikely(ret == 0)) { /* no free counter. */
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
407 : : rte_ring_dequeue_zc_elem_finish(free_list, 0);
408 [ # # # # : 0 : if (rte_ring_count(cpool->wait_reset_list))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
409 : : return -EAGAIN;
410 : 0 : return -ENOENT;
411 : : }
412 : : }
413 : : actual_fetch_size = ret;
414 : : ret = rte_ring_enqueue_zc_burst_elem_start(qcache, sizeof(cnt_id_t),
415 : : ret, &zcdc, NULL);
416 : : MLX5_ASSERT(ret == actual_fetch_size);
417 : : __hws_cnt_r2rcpy(&zcdc, &zcds, ret);
418 : : rte_ring_dequeue_zc_elem_finish(list, ret);
419 : : rte_ring_enqueue_zc_elem_finish(qcache, ret);
420 : : return 0;
421 : : }
422 : :
423 : : static __rte_always_inline int
424 : : __mlx5_hws_cnt_pool_enqueue_revert(struct rte_ring *r, unsigned int n,
425 : : struct rte_ring_zc_data *zcd)
426 : : {
427 : : uint32_t current_head = 0;
428 : : uint32_t revert2head = 0;
429 : :
430 : : MLX5_ASSERT(r->prod.sync_type == RTE_RING_SYNC_ST);
431 : : MLX5_ASSERT(r->cons.sync_type == RTE_RING_SYNC_ST);
432 : 0 : current_head = rte_atomic_load_explicit(&r->prod.head, rte_memory_order_relaxed);
433 : : MLX5_ASSERT(n <= r->capacity);
434 : : MLX5_ASSERT(n <= rte_ring_count(r));
435 : 0 : revert2head = current_head - n;
436 : 0 : r->prod.head = revert2head; /* This ring should be SP. */
437 : : __rte_ring_get_elem_addr(r, revert2head, sizeof(cnt_id_t), n,
438 : : &zcd->ptr1, &zcd->n1, &zcd->ptr2);
439 : : /* Update tail */
440 [ # # # ]: 0 : rte_atomic_store_explicit(&r->prod.tail, revert2head, rte_memory_order_release);
441 : : return n;
442 : : }
443 : :
444 : : /**
445 : : * Put one counter back in the mempool.
446 : : *
447 : : * @param cpool
448 : : * A pointer to the counter pool structure.
449 : : * @param queue
450 : : * A pointer to HWS queue. If null, it means put into common pool.
451 : : * @param cnt_id
452 : : * A counter id to be added.
453 : : */
454 : : static __rte_always_inline void
455 : : mlx5_hws_cnt_pool_put(struct mlx5_hws_cnt_pool *cpool, uint32_t *queue,
456 : : cnt_id_t *cnt_id)
457 : : {
458 : : unsigned int ret = 0;
459 : : struct mlx5_hws_cnt_pool *hpool;
460 : : struct rte_ring_zc_data zcdc = {0};
461 : : struct rte_ring_zc_data zcdr = {0};
462 : : struct rte_ring *qcache = NULL;
463 : : unsigned int wb_num = 0; /* cache write-back number. */
464 : : uint32_t iidx;
465 : :
466 : : hpool = mlx5_hws_cnt_host_pool(cpool);
467 [ # # # # ]: 0 : iidx = mlx5_hws_cnt_iidx(hpool, *cnt_id);
468 : 0 : mlx5_hws_cnt_set_all(&hpool->pool[iidx], 0, 0, 0);
469 : 0 : rte_atomic_store_explicit(&hpool->pool[iidx].query_gen_when_free,
470 : : rte_atomic_load_explicit(&hpool->query_gen, rte_memory_order_relaxed),
471 : : rte_memory_order_relaxed);
472 [ # # # # ]: 0 : if (likely(queue != NULL) && cpool->cfg.host_cpool == NULL)
473 : 0 : qcache = hpool->cache->qcache[*queue];
474 [ # # ]: 0 : if (unlikely(qcache == NULL)) {
475 [ # # # # : 0 : ret = rte_ring_enqueue_elem(hpool->wait_reset_list, cnt_id,
# # # # #
# # # # #
# ]
476 : : sizeof(cnt_id_t));
477 : : MLX5_ASSERT(ret == 0);
478 : : return;
479 : : }
480 : : ret = rte_ring_enqueue_burst_elem(qcache, cnt_id, sizeof(cnt_id_t), 1,
481 : : NULL);
482 [ # # ]: 0 : if (unlikely(ret == 0)) { /* cache is full. */
483 [ # # ]: 0 : struct rte_ring *reset_list = cpool->wait_reset_list;
484 : :
485 [ # # ]: 0 : wb_num = rte_ring_count(qcache) - cpool->cache->threshold;
486 : : MLX5_ASSERT(wb_num < rte_ring_count(qcache));
487 : : __mlx5_hws_cnt_pool_enqueue_revert(qcache, wb_num, &zcdc);
488 : : ret = rte_ring_enqueue_zc_burst_elem_start(reset_list,
489 : : sizeof(cnt_id_t),
490 : : wb_num, &zcdr, NULL);
491 : : MLX5_ASSERT(ret == wb_num);
492 : : __hws_cnt_r2rcpy(&zcdr, &zcdc, ret);
493 : : rte_ring_enqueue_zc_elem_finish(reset_list, ret);
494 : : /* write-back THIS counter too */
495 : : ret = rte_ring_enqueue_burst_elem(reset_list, cnt_id,
496 : : sizeof(cnt_id_t), 1, NULL);
497 : : }
498 : : MLX5_ASSERT(ret == 1);
499 : : }
500 : :
501 : : /**
502 : : * Get one counter from the pool.
503 : : *
504 : : * If @param queue is not null, objects will be retrieved first from queue's
505 : : * cache, subsequently from the common pool. Note that it can return -ENOENT
506 : : * when the local cache and common pool are empty, even if cache from other
507 : : * queue are full.
508 : : *
509 : : * @param cntp
510 : : * A pointer to the counter pool structure.
511 : : * @param queue
512 : : * A pointer to HWS queue. If null, it means fetch from common pool.
513 : : * @param cnt_id
514 : : * A pointer to a cnt_id_t * pointer (counter id) that will be filled.
515 : : * @param age_idx
516 : : * Index of AGE parameter using this counter, zero means there is no such AGE.
517 : : *
518 : : * @return
519 : : * - 0: Success; objects taken.
520 : : * - -ENOENT: Not enough entries in the mempool; no object is retrieved.
521 : : * - -EAGAIN: counter is not ready; try again.
522 : : */
523 : : static __rte_always_inline int
524 : : mlx5_hws_cnt_pool_get(struct mlx5_hws_cnt_pool *cpool, uint32_t *queue,
525 : : cnt_id_t *cnt_id, uint32_t age_idx, uint32_t shared)
526 : : {
527 : : unsigned int ret;
528 : : struct rte_ring_zc_data zcdc = {0};
529 : : struct rte_ring *qcache = NULL;
530 : : uint32_t iidx, query_gen = 0;
531 [ # # # # : 0 : cnt_id_t tmp_cid = 0;
# # # # #
# # # #
# ]
532 : :
533 [ # # # # : 0 : if (likely(queue != NULL && cpool->cfg.host_cpool == NULL))
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
534 : 0 : qcache = cpool->cache->qcache[*queue];
535 [ # # # # : 0 : if (unlikely(qcache == NULL)) {
# # # # #
# # # # #
# # # # #
# ]
536 : : cpool = mlx5_hws_cnt_host_pool(cpool);
537 [ # # # # : 0 : ret = rte_ring_dequeue_elem(cpool->reuse_list, &tmp_cid,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
538 : : sizeof(cnt_id_t));
539 [ # # # # : 0 : if (unlikely(ret != 0)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
540 [ # # # # : 0 : ret = rte_ring_dequeue_elem(cpool->free_list, &tmp_cid,
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
541 : : sizeof(cnt_id_t));
542 [ # # # # : 0 : if (unlikely(ret != 0)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
543 [ # # # # : 0 : if (rte_ring_count(cpool->wait_reset_list))
# # # # #
# ]
544 : : return -EAGAIN;
545 : : return -ENOENT;
546 : : }
547 : : }
548 [ # # # # : 0 : *cnt_id = tmp_cid;
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
549 : : iidx = mlx5_hws_cnt_iidx(cpool, *cnt_id);
550 : : __hws_cnt_query_raw(cpool, *cnt_id,
551 : : &cpool->pool[iidx].reset.hits,
552 : 0 : &cpool->pool[iidx].reset.bytes);
553 : : mlx5_hws_cnt_set_all(&cpool->pool[iidx], 1, shared, age_idx);
554 : : return 0;
555 : : }
556 : : ret = rte_ring_dequeue_zc_burst_elem_start(qcache, sizeof(cnt_id_t), 1,
557 : : &zcdc, NULL);
558 [ # # # # : 0 : if (unlikely(ret == 0)) { /* local cache is empty. */
# # # # #
# # # # #
# # # # #
# ]
559 : : rte_ring_dequeue_zc_elem_finish(qcache, 0);
560 : : /* let's fetch from global free list. */
561 [ # # # # : 0 : ret = mlx5_hws_cnt_pool_cache_fetch(cpool, *queue);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
562 [ # # # # : 0 : if (unlikely(ret != 0))
# # # # #
# # # # #
# # # # #
# ]
563 : : return ret;
564 : : ret = rte_ring_dequeue_zc_burst_elem_start(qcache,
565 : : sizeof(cnt_id_t), 1,
566 : : &zcdc, NULL);
567 : : MLX5_ASSERT(ret == 1);
568 : : }
569 : : /* get one from local cache. */
570 [ # # # # : 0 : *cnt_id = (*(cnt_id_t *)zcdc.ptr1);
# # # # #
# # # # #
# # # # #
# ]
571 : : iidx = mlx5_hws_cnt_iidx(cpool, *cnt_id);
572 : 0 : query_gen = rte_atomic_load_explicit(&cpool->pool[iidx].query_gen_when_free,
573 : : rte_memory_order_relaxed);
574 : : /* counter is waiting to reset. */
575 [ # # # # : 0 : if (rte_atomic_load_explicit(&cpool->query_gen, rte_memory_order_relaxed) == query_gen) {
# # # # #
# # # # #
# # # # #
# ]
576 : : rte_ring_dequeue_zc_elem_finish(qcache, 0);
577 : : /* write-back counter to reset list. */
578 : 0 : mlx5_hws_cnt_pool_cache_flush(cpool, *queue);
579 : : /* let's fetch from global free list. */
580 [ # # # # : 0 : ret = mlx5_hws_cnt_pool_cache_fetch(cpool, *queue);
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
581 [ # # # # : 0 : if (unlikely(ret != 0))
# # # # #
# # # # #
# # # # #
# ]
582 : : return ret;
583 : : ret = rte_ring_dequeue_zc_burst_elem_start(qcache,
584 : : sizeof(cnt_id_t), 1,
585 : : &zcdc, NULL);
586 : : MLX5_ASSERT(ret == 1);
587 [ # # # # : 0 : *cnt_id = *(cnt_id_t *)zcdc.ptr1;
# # # # #
# # # # #
# # # # #
# ]
588 : : iidx = mlx5_hws_cnt_iidx(cpool, *cnt_id);
589 : : }
590 : : __hws_cnt_query_raw(cpool, *cnt_id, &cpool->pool[iidx].reset.hits,
591 [ # # # # : 0 : &cpool->pool[iidx].reset.bytes);
# # # # #
# # # # #
# # # # #
# ]
592 : : rte_ring_dequeue_zc_elem_finish(qcache, 1);
593 : 0 : mlx5_hws_cnt_set_all(&cpool->pool[iidx], 1, shared, age_idx);
594 : : return 0;
595 : : }
596 : :
597 : : /**
598 : : * Decide if the given queue can be used to perform counter allocation/deallcation
599 : : * based on counter configuration
600 : : *
601 : : * @param[in] priv
602 : : * Pointer to the port private data structure.
603 : : * @param[in] queue
604 : : * Pointer to the queue index.
605 : : *
606 : : * @return
607 : : * @p queue if cache related to the queue can be used. NULL otherwise.
608 : : */
609 : : static __rte_always_inline uint32_t *
610 : : mlx5_hws_cnt_get_queue(struct mlx5_priv *priv, uint32_t *queue)
611 : : {
612 [ # # # # : 0 : if (priv && priv->hws_cpool) {
# # # # #
# # # # #
# # # # #
# # # ]
613 : : /* Do not use queue cache if counter pool is shared. */
614 [ # # # # : 0 : if (priv->shared_refcnt || priv->hws_cpool->cfg.host_cpool != NULL)
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # #
# ]
615 : : return NULL;
616 : : /* Do not use queue cache if counter cache is disabled. */
617 [ # # # # : 0 : if (priv->hws_cpool->cache == NULL)
# # # # #
# # # # #
# # # # #
# # # ]
618 : : return NULL;
619 : 0 : return queue;
620 : : }
621 : : /* This case should not be reached if counter pool was successfully configured. */
622 : : MLX5_ASSERT(false);
623 : : return NULL;
624 : : }
625 : :
626 : : static __rte_always_inline unsigned int
627 : : mlx5_hws_cnt_pool_get_size(struct mlx5_hws_cnt_pool *cpool)
628 : : {
629 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
630 : :
631 [ # # # # ]: 0 : return rte_ring_get_capacity(hpool->free_list);
632 : : }
633 : :
634 : : static __rte_always_inline int
635 : : mlx5_hws_cnt_pool_get_action_offset(struct mlx5_hws_cnt_pool *cpool,
636 : : cnt_id_t cnt_id, struct mlx5dr_action **action,
637 : : uint32_t *offset, bool is_root)
638 : : {
639 : 0 : uint8_t idx = cnt_id >> MLX5_HWS_CNT_DCS_IDX_OFFSET;
640 : :
641 : 0 : idx &= MLX5_HWS_CNT_DCS_IDX_MASK;
642 [ # # # # : 0 : if (likely(!is_root)) {
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # # #
# ]
643 : 0 : *action = cpool->dcs_mng.dcs[idx].hws_action;
644 : : } else {
645 : : /*
646 : : * Any table using counter on root group should be rejected on validation
647 : : * when counter on root is not supported.
648 : : */
649 : : MLX5_ASSERT(cpool->dcs_mng.dcs[idx].root_action != NULL);
650 : 0 : *action = cpool->dcs_mng.dcs[idx].root_action;
651 : : }
652 : 0 : *offset = cnt_id & MLX5_HWS_CNT_IDX_MASK;
653 : : return 0;
654 : : }
655 : :
656 : : static __rte_always_inline int
657 : : mlx5_hws_cnt_shared_get(struct mlx5_hws_cnt_pool *cpool, cnt_id_t *cnt_id,
658 : : uint32_t age_idx)
659 : : {
660 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
661 : :
662 : : return mlx5_hws_cnt_pool_get(hpool, NULL, cnt_id, age_idx, 1);
663 : : }
664 : :
665 : : static __rte_always_inline void
666 : : mlx5_hws_cnt_shared_put(struct mlx5_hws_cnt_pool *cpool, cnt_id_t *cnt_id)
667 : : {
668 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
669 : :
670 : : mlx5_hws_cnt_pool_put(hpool, NULL, cnt_id);
671 : : }
672 : :
673 : : static __rte_always_inline bool
674 : : mlx5_hws_cnt_is_shared(struct mlx5_hws_cnt_pool *cpool, cnt_id_t cnt_id)
675 : : {
676 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
677 : : uint32_t iidx = mlx5_hws_cnt_iidx(hpool, cnt_id);
678 : : uint32_t share;
679 : :
680 [ # # ]: 0 : mlx5_hws_cnt_get_all(&hpool->pool[iidx], NULL, &share, NULL);
681 : : return !!share;
682 : : }
683 : :
684 : : static __rte_always_inline void
685 : : mlx5_hws_cnt_age_set(struct mlx5_hws_cnt_pool *cpool, cnt_id_t cnt_id,
686 : : uint32_t age_idx)
687 : : {
688 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
689 : : uint32_t iidx = mlx5_hws_cnt_iidx(hpool, cnt_id);
690 : :
691 : : MLX5_ASSERT(hpool->pool[iidx].cnt_state.share);
692 : 0 : mlx5_hws_cnt_set_age_idx(&hpool->pool[iidx], age_idx);
693 : 0 : }
694 : :
695 : : static __rte_always_inline uint32_t
696 : : mlx5_hws_cnt_age_get(struct mlx5_hws_cnt_pool *cpool, cnt_id_t cnt_id)
697 : : {
698 : : struct mlx5_hws_cnt_pool *hpool = mlx5_hws_cnt_host_pool(cpool);
699 : : uint32_t iidx = mlx5_hws_cnt_iidx(hpool, cnt_id);
700 : : uint32_t age_idx, share;
701 : :
702 [ # # # # : 0 : mlx5_hws_cnt_get_all(&hpool->pool[iidx], NULL, &share, &age_idx);
# # # # #
# # # ]
703 : : MLX5_ASSERT(share);
704 : : return age_idx;
705 : : }
706 : :
707 : : static __rte_always_inline cnt_id_t
708 : : mlx5_hws_age_cnt_get(struct mlx5_priv *priv, struct mlx5_hws_age_param *param,
709 : : uint32_t age_idx)
710 : : {
711 [ # # # # : 0 : if (!param->own_cnt_index) {
# # # # #
# ]
712 : : /* Create indirect counter one for internal usage. */
713 [ # # # # : 0 : if (mlx5_hws_cnt_shared_get(priv->hws_cpool,
# # # # #
# ]
714 : : ¶m->own_cnt_index, age_idx) < 0)
715 : : return 0;
716 : 0 : param->nb_cnts++;
717 : : }
718 [ # # # # : 0 : return param->own_cnt_index;
# # # # #
# ]
719 : : }
720 : :
721 : : static __rte_always_inline void
722 : : mlx5_hws_age_nb_cnt_increase(struct mlx5_priv *priv, uint32_t age_idx)
723 : : {
724 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
725 : 0 : struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
726 : 0 : struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, age_idx);
727 : :
728 : : MLX5_ASSERT(param != NULL);
729 : 0 : param->nb_cnts++;
730 : 0 : }
731 : :
732 : : static __rte_always_inline void
733 : : mlx5_hws_age_nb_cnt_decrease(struct mlx5_priv *priv, uint32_t age_idx)
734 : : {
735 : 0 : struct mlx5_age_info *age_info = GET_PORT_AGE_INFO(priv);
736 : 0 : struct mlx5_indexed_pool *ipool = age_info->ages_ipool;
737 : 0 : struct mlx5_hws_age_param *param = mlx5_ipool_get(ipool, age_idx);
738 : :
739 [ # # # # ]: 0 : if (param != NULL)
740 : 0 : param->nb_cnts--;
741 : : }
742 : :
743 : : static __rte_always_inline bool
744 : : mlx5_hws_age_is_indirect(uint32_t age_idx)
745 : : {
746 [ # # # # ]: 0 : return (age_idx >> MLX5_INDIRECT_ACTION_TYPE_OFFSET) ==
747 : : MLX5_INDIRECT_ACTION_TYPE_AGE ? true : false;
748 : : }
749 : :
750 : : /* init HWS counter pool. */
751 : : int
752 : : mlx5_hws_cnt_service_thread_create(struct mlx5_dev_ctx_shared *sh);
753 : :
754 : : void
755 : : mlx5_hws_cnt_service_thread_destroy(struct mlx5_dev_ctx_shared *sh);
756 : :
757 : : int
758 : : mlx5_hws_cnt_pool_create(struct rte_eth_dev *dev,
759 : : uint32_t nb_counters, uint16_t nb_queue,
760 : : struct mlx5_hws_cnt_pool *chost, struct rte_flow_error *error);
761 : :
762 : : void
763 : : mlx5_hws_cnt_pool_destroy(struct mlx5_dev_ctx_shared *sh,
764 : : struct mlx5_hws_cnt_pool *cpool);
765 : :
766 : : int
767 : : mlx5_hws_cnt_svc_init(struct mlx5_dev_ctx_shared *sh,
768 : : struct rte_flow_error *error);
769 : :
770 : : void
771 : : mlx5_hws_cnt_svc_deinit(struct mlx5_dev_ctx_shared *sh);
772 : :
773 : : int
774 : : mlx5_hws_age_action_destroy(struct mlx5_priv *priv, uint32_t idx,
775 : : struct rte_flow_error *error);
776 : :
777 : : uint32_t
778 : : mlx5_hws_age_action_create(struct mlx5_priv *priv, uint32_t queue_id,
779 : : bool shared, const struct rte_flow_action_age *age,
780 : : uint32_t flow_idx, struct rte_flow_error *error);
781 : :
782 : : int
783 : : mlx5_hws_age_action_update(struct mlx5_priv *priv, uint32_t idx,
784 : : const void *update, struct rte_flow_error *error);
785 : :
786 : : void *
787 : : mlx5_hws_age_context_get(struct mlx5_priv *priv, uint32_t idx);
788 : :
789 : : int
790 : : mlx5_hws_age_pool_init(struct rte_eth_dev *dev,
791 : : uint32_t nb_aging_objects,
792 : : uint16_t nb_queues,
793 : : bool strict_queue);
794 : :
795 : : void
796 : : mlx5_hws_age_pool_destroy(struct mlx5_priv *priv);
797 : :
798 : : #endif /* _MLX5_HWS_CNT_H_ */
|