Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2018 Mellanox Technologies, Ltd
3 : : */
4 : : #include <math.h>
5 : :
6 : : #include <rte_tailq.h>
7 : : #include <rte_malloc.h>
8 : : #include <rte_mtr.h>
9 : : #include <rte_mtr_driver.h>
10 : :
11 : : #include <mlx5_devx_cmds.h>
12 : : #include <mlx5_malloc.h>
13 : :
14 : : #include "mlx5.h"
15 : : #include "mlx5_flow.h"
16 : :
17 : : #ifdef HAVE_MLX5_HWS_SUPPORT
18 : :
19 : : static void
20 : 0 : mlx5_flow_meter_uninit_guest(struct rte_eth_dev *dev)
21 : : {
22 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
23 : :
24 [ # # ]: 0 : if (priv->hws_mpool) {
25 [ # # ]: 0 : if (priv->hws_mpool->action) {
26 : 0 : claim_zero(mlx5dr_action_destroy(priv->hws_mpool->action));
27 : 0 : priv->hws_mpool->action = NULL;
28 : : }
29 : 0 : priv->hws_mpool->devx_obj = NULL;
30 : 0 : priv->hws_mpool->idx_pool = NULL;
31 : 0 : mlx5_free(priv->hws_mpool);
32 : 0 : priv->hws_mpool = NULL;
33 : : }
34 : 0 : }
35 : :
36 : : void
37 : 0 : mlx5_flow_meter_uninit(struct rte_eth_dev *dev)
38 : : {
39 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
40 : :
41 [ # # ]: 0 : if (priv->shared_host) {
42 : 0 : mlx5_flow_meter_uninit_guest(dev);
43 : 0 : return;
44 : : }
45 [ # # ]: 0 : if (priv->mtr_policy_arr) {
46 : 0 : mlx5_free(priv->mtr_policy_arr);
47 : 0 : priv->mtr_policy_arr = NULL;
48 : : }
49 [ # # ]: 0 : if (priv->mtr_profile_arr) {
50 : 0 : mlx5_free(priv->mtr_profile_arr);
51 : 0 : priv->mtr_profile_arr = NULL;
52 : : }
53 [ # # ]: 0 : if (priv->hws_mpool) {
54 : 0 : mlx5_aso_mtr_queue_uninit(priv->sh, priv->hws_mpool, NULL);
55 : 0 : mlx5_ipool_destroy(priv->hws_mpool->idx_pool);
56 : 0 : mlx5_free(priv->hws_mpool);
57 : 0 : priv->hws_mpool = NULL;
58 : : }
59 [ # # ]: 0 : if (priv->mtr_bulk.aso) {
60 : 0 : mlx5_free(priv->mtr_bulk.aso);
61 : 0 : priv->mtr_bulk.aso = NULL;
62 : 0 : priv->mtr_bulk.size = 0;
63 : : /*
64 : : * pools_mng->sq is shared by every port on this device.
65 : : * Destroying it here would break meters still in use on
66 : : * another port. It is released with the shared context.
67 : : */
68 : : }
69 [ # # ]: 0 : if (priv->mtr_bulk.action) {
70 : 0 : mlx5dr_action_destroy(priv->mtr_bulk.action);
71 : 0 : priv->mtr_bulk.action = NULL;
72 : : }
73 [ # # ]: 0 : if (priv->mtr_bulk.devx_obj) {
74 : 0 : claim_zero(mlx5_devx_cmd_destroy(priv->mtr_bulk.devx_obj));
75 : 0 : priv->mtr_bulk.devx_obj = NULL;
76 : : }
77 : : }
78 : :
79 : : static int
80 : 0 : mlx5_flow_meter_init_guest(struct rte_eth_dev *dev)
81 : : {
82 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
83 : 0 : struct rte_eth_dev *host_dev = priv->shared_host;
84 : 0 : struct mlx5_priv *host_priv = host_dev->data->dev_private;
85 : : int reg_id = 0;
86 : : uint32_t flags;
87 : : int ret = 0;
88 : :
89 : : MLX5_ASSERT(priv->shared_host);
90 : 0 : reg_id = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, NULL);
91 [ # # ]: 0 : if (reg_id < 0) {
92 : 0 : rte_errno = ENOMEM;
93 : : ret = -rte_errno;
94 : 0 : DRV_LOG(ERR, "Meter register is not available.");
95 : 0 : goto err;
96 : : }
97 : 0 : priv->hws_mpool = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_aso_mtr_pool),
98 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
99 [ # # ]: 0 : if (!priv->hws_mpool) {
100 : 0 : rte_errno = ENOMEM;
101 : : ret = -rte_errno;
102 : 0 : DRV_LOG(ERR, "Meter ipool allocation failed.");
103 : 0 : goto err;
104 : : }
105 : : MLX5_ASSERT(host_priv->hws_mpool->idx_pool);
106 : : MLX5_ASSERT(host_priv->hws_mpool->devx_obj);
107 : 0 : priv->hws_mpool->idx_pool = host_priv->hws_mpool->idx_pool;
108 : 0 : priv->hws_mpool->devx_obj = host_priv->hws_mpool->devx_obj;
109 : : flags = MLX5DR_ACTION_FLAG_HWS_RX | MLX5DR_ACTION_FLAG_HWS_TX;
110 [ # # # # ]: 0 : if (priv->sh->config.dv_esw_en && priv->master)
111 : 0 : flags |= (is_unified_fdb(priv) ?
112 : : (MLX5DR_ACTION_FLAG_HWS_FDB_RX |
113 : : MLX5DR_ACTION_FLAG_HWS_FDB_TX |
114 [ # # ]: 0 : MLX5DR_ACTION_FLAG_HWS_FDB_UNIFIED) :
115 : : MLX5DR_ACTION_FLAG_HWS_FDB);
116 : 0 : priv->hws_mpool->action = mlx5dr_action_create_aso_meter
117 : : (priv->dr_ctx, (struct mlx5dr_devx_obj *)priv->hws_mpool->devx_obj,
118 : 0 : reg_id - REG_C_0, flags);
119 [ # # ]: 0 : if (!priv->hws_mpool->action) {
120 : 0 : rte_errno = ENOMEM;
121 : : ret = -rte_errno;
122 : 0 : DRV_LOG(ERR, "Meter action creation failed.");
123 : 0 : goto err;
124 : : }
125 : : return 0;
126 : 0 : err:
127 : 0 : mlx5_flow_meter_uninit(dev);
128 : 0 : return ret;
129 : : }
130 : :
131 : : int
132 : 0 : mlx5_flow_meter_init(struct rte_eth_dev *dev,
133 : : uint32_t nb_meters,
134 : : uint32_t nb_meter_profiles,
135 : : uint32_t nb_meter_policies,
136 : : uint32_t nb_queues)
137 : : {
138 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
139 : : struct mlx5_devx_obj *dcs = NULL;
140 : : uint32_t log_obj_size;
141 : : int ret = 0;
142 : : int reg_id;
143 : : struct mlx5_aso_mtr *aso;
144 : : uint32_t i;
145 : : struct rte_flow_error error;
146 : : uint32_t flags;
147 : : uint32_t nb_mtrs = rte_align32pow2(nb_meters);
148 : 0 : struct mlx5_indexed_pool_config cfg = {
149 : : .size = sizeof(struct mlx5_aso_mtr),
150 : : .trunk_size = 1 << 12,
151 : : .per_core_cache = 1 << 13,
152 : : .need_lock = 1,
153 : 0 : .release_mem_en = !!priv->sh->config.reclaim_mode,
154 : : .malloc = mlx5_malloc,
155 : : .max_idx = nb_meters,
156 : : .free = mlx5_free,
157 : : .type = "mlx5_hw_mtr_mark_action",
158 : : };
159 : :
160 [ # # ]: 0 : if (priv->shared_host)
161 : 0 : return mlx5_flow_meter_init_guest(dev);
162 [ # # ]: 0 : if (!nb_meters) {
163 : : ret = ENOTSUP;
164 : 0 : rte_flow_error_set(&error, ENOMEM,
165 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
166 : : NULL, "Meter configuration is invalid.");
167 : 0 : goto err;
168 : : }
169 [ # # # # ]: 0 : if (!priv->mtr_en || !priv->sh->meter_aso_en) {
170 : : ret = ENOTSUP;
171 : 0 : rte_flow_error_set(&error, ENOMEM,
172 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
173 : : NULL, "Meter ASO is not supported.");
174 : 0 : goto err;
175 : : }
176 : 0 : priv->mtr_config.nb_meters = nb_meters;
177 [ # # ]: 0 : log_obj_size = rte_log2_u32(nb_meters >> 1);
178 : 0 : dcs = mlx5_devx_cmd_create_flow_meter_aso_obj
179 : 0 : (priv->sh->cdev->ctx, priv->sh->cdev->pdn,
180 : : log_obj_size);
181 [ # # ]: 0 : if (!dcs) {
182 : : ret = ENOMEM;
183 : 0 : rte_flow_error_set(&error, ENOMEM,
184 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
185 : : NULL, "Meter ASO object allocation failed.");
186 : 0 : goto err;
187 : : }
188 : 0 : priv->mtr_bulk.devx_obj = dcs;
189 : 0 : reg_id = mlx5_flow_get_reg_id(dev, MLX5_MTR_COLOR, 0, NULL);
190 [ # # ]: 0 : if (reg_id < 0) {
191 : : ret = ENOTSUP;
192 : 0 : rte_flow_error_set(&error, ENOMEM,
193 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
194 : : NULL, "Meter register is not available.");
195 : 0 : goto err;
196 : : }
197 : : flags = MLX5DR_ACTION_FLAG_HWS_RX | MLX5DR_ACTION_FLAG_HWS_TX;
198 [ # # # # ]: 0 : if (priv->sh->config.dv_esw_en && priv->master) {
199 : 0 : flags |= ((is_unified_fdb(priv)) ?
200 : : (MLX5DR_ACTION_FLAG_HWS_FDB_RX |
201 : : MLX5DR_ACTION_FLAG_HWS_FDB_TX |
202 [ # # ]: 0 : MLX5DR_ACTION_FLAG_HWS_FDB_UNIFIED) :
203 : : MLX5DR_ACTION_FLAG_HWS_FDB);
204 : : }
205 : :
206 : 0 : priv->mtr_bulk.action = mlx5dr_action_create_aso_meter
207 : : (priv->dr_ctx, (struct mlx5dr_devx_obj *)dcs,
208 : 0 : reg_id - REG_C_0, flags);
209 [ # # ]: 0 : if (!priv->mtr_bulk.action) {
210 : : ret = ENOMEM;
211 : 0 : rte_flow_error_set(&error, ENOMEM,
212 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
213 : : NULL, "Meter action creation failed.");
214 : 0 : goto err;
215 : : }
216 : 0 : priv->mtr_bulk.aso = mlx5_malloc(MLX5_MEM_ZERO,
217 : : sizeof(struct mlx5_aso_mtr) *
218 : : nb_meters,
219 : : RTE_CACHE_LINE_SIZE,
220 : : SOCKET_ID_ANY);
221 [ # # ]: 0 : if (!priv->mtr_bulk.aso) {
222 : : ret = ENOMEM;
223 : 0 : rte_flow_error_set(&error, ENOMEM,
224 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
225 : : NULL, "Meter bulk ASO allocation failed.");
226 : 0 : goto err;
227 : : }
228 : 0 : priv->mtr_bulk.size = nb_meters;
229 : : aso = priv->mtr_bulk.aso;
230 [ # # ]: 0 : for (i = 0; i < priv->mtr_bulk.size; i++) {
231 : 0 : aso->type = ASO_METER_DIRECT;
232 : 0 : aso->state = ASO_METER_WAIT;
233 : 0 : aso->offset = i;
234 : 0 : aso++;
235 : : }
236 : 0 : priv->hws_mpool = mlx5_malloc(MLX5_MEM_ZERO,
237 : : sizeof(struct mlx5_aso_mtr_pool),
238 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
239 [ # # ]: 0 : if (!priv->hws_mpool) {
240 : : ret = ENOMEM;
241 : 0 : rte_flow_error_set(&error, ENOMEM,
242 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
243 : : NULL, "Meter ipool allocation failed.");
244 : 0 : goto err;
245 : : }
246 : 0 : priv->hws_mpool->devx_obj = priv->mtr_bulk.devx_obj;
247 : 0 : priv->hws_mpool->action = priv->mtr_bulk.action;
248 : 0 : priv->hws_mpool->nb_sq = nb_queues;
249 [ # # ]: 0 : if (mlx5_aso_mtr_queue_init(priv->sh, priv->hws_mpool,
250 : 0 : &priv->sh->mtrmng->pools_mng, nb_queues)) {
251 : : ret = ENOMEM;
252 : 0 : rte_flow_error_set(&error, ENOMEM,
253 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
254 : : NULL, "Meter ASO queue allocation failed.");
255 : 0 : goto err;
256 : : }
257 : : /*
258 : : * No need for local cache if Meter number is a small number.
259 : : * Since flow insertion rate will be very limited in that case.
260 : : * Here let's set the number to less than default trunk size 4K.
261 : : */
262 [ # # ]: 0 : if (nb_mtrs <= cfg.trunk_size) {
263 : 0 : cfg.per_core_cache = 0;
264 : 0 : cfg.trunk_size = nb_mtrs;
265 [ # # ]: 0 : } else if (nb_mtrs <= MLX5_HW_IPOOL_SIZE_THRESHOLD) {
266 : 0 : cfg.per_core_cache = MLX5_HW_IPOOL_CACHE_MIN;
267 : : }
268 : 0 : priv->hws_mpool->idx_pool = mlx5_ipool_create(&cfg);
269 [ # # ]: 0 : if (nb_meter_profiles) {
270 : 0 : priv->mtr_config.nb_meter_profiles = nb_meter_profiles;
271 : 0 : priv->mtr_profile_arr =
272 : 0 : mlx5_malloc(MLX5_MEM_ZERO,
273 : : sizeof(struct mlx5_flow_meter_profile) *
274 : : nb_meter_profiles,
275 : : RTE_CACHE_LINE_SIZE,
276 : : SOCKET_ID_ANY);
277 [ # # ]: 0 : if (!priv->mtr_profile_arr) {
278 : : ret = ENOMEM;
279 : 0 : rte_flow_error_set(&error, ENOMEM,
280 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
281 : : NULL, "Meter profile allocation failed.");
282 : 0 : goto err;
283 : : }
284 : : }
285 [ # # ]: 0 : if (nb_meter_policies) {
286 : 0 : priv->mtr_config.nb_meter_policies = nb_meter_policies;
287 : 0 : priv->mtr_policy_arr =
288 : 0 : mlx5_malloc(MLX5_MEM_ZERO,
289 : : sizeof(struct mlx5_flow_meter_policy) *
290 : : nb_meter_policies,
291 : : RTE_CACHE_LINE_SIZE,
292 : : SOCKET_ID_ANY);
293 [ # # ]: 0 : if (!priv->mtr_policy_arr) {
294 : : ret = ENOMEM;
295 : 0 : rte_flow_error_set(&error, ENOMEM,
296 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
297 : : NULL, "Meter policy allocation failed.");
298 : 0 : goto err;
299 : : }
300 : : }
301 : : return 0;
302 : 0 : err:
303 : 0 : mlx5_flow_meter_uninit(dev);
304 : 0 : return ret;
305 : : }
306 : :
307 : : #endif /* HAVE_MLX5_HWS_SUPPORT */
308 : :
309 : : static int mlx5_flow_meter_disable(struct rte_eth_dev *dev,
310 : : uint32_t meter_id, struct rte_mtr_error *error);
311 : :
312 : : /**
313 : : * Create the meter action.
314 : : *
315 : : * @param priv
316 : : * Pointer to mlx5_priv.
317 : : * @param[in] fm
318 : : * Pointer to flow meter to be converted.
319 : : *
320 : : * @return
321 : : * Pointer to the meter action on success, NULL otherwise.
322 : : */
323 : : static void *
324 : 0 : mlx5_flow_meter_action_create(struct mlx5_priv *priv,
325 : : struct mlx5_flow_meter_info *fm)
326 : : {
327 : : #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
328 : : struct mlx5dv_dr_flow_meter_attr mtr_init;
329 : : uint32_t fmp[MLX5_ST_SZ_DW(flow_meter_parameters)];
330 : : struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm =
331 : 0 : &fm->profile->srtcm_prm;
332 [ # # ]: 0 : uint32_t cbs_cir = rte_be_to_cpu_32(srtcm->cbs_cir);
333 [ # # ]: 0 : uint32_t ebs_eir = rte_be_to_cpu_32(srtcm->ebs_eir);
334 : : uint32_t val;
335 : : enum mlx5_meter_domain domain =
336 [ # # ]: 0 : fm->transfer ? MLX5_MTR_DOMAIN_TRANSFER :
337 : 0 : fm->egress ? MLX5_MTR_DOMAIN_EGRESS :
338 : : MLX5_MTR_DOMAIN_INGRESS;
339 : 0 : struct mlx5_flow_meter_def_policy *def_policy =
340 : 0 : priv->sh->mtrmng->def_policy[domain];
341 : :
342 : : memset(fmp, 0, MLX5_ST_SZ_BYTES(flow_meter_parameters));
343 : : MLX5_SET(flow_meter_parameters, fmp, valid, 1);
344 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp, bucket_overflow, 1);
345 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp,
346 : : start_color, MLX5_FLOW_COLOR_GREEN);
347 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp, both_buckets_on_green, 0);
348 : 0 : val = (cbs_cir >> ASO_DSEG_CBS_EXP_OFFSET) & ASO_DSEG_EXP_MASK;
349 : 0 : MLX5_SET(flow_meter_parameters, fmp, cbs_exponent, val);
350 : 0 : val = (cbs_cir >> ASO_DSEG_CBS_MAN_OFFSET) & ASO_DSEG_MAN_MASK;
351 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp, cbs_mantissa, val);
352 : 0 : val = (cbs_cir >> ASO_DSEG_XIR_EXP_OFFSET) & ASO_DSEG_EXP_MASK;
353 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp, cir_exponent, val);
354 : 0 : val = (cbs_cir & ASO_DSEG_MAN_MASK);
355 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp, cir_mantissa, val);
356 : 0 : val = (ebs_eir >> ASO_DSEG_EBS_EXP_OFFSET) & ASO_DSEG_EXP_MASK;
357 : 0 : MLX5_SET(flow_meter_parameters, fmp, ebs_exponent, val);
358 : 0 : val = (ebs_eir >> ASO_DSEG_EBS_MAN_OFFSET) & ASO_DSEG_MAN_MASK;
359 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, fmp, ebs_mantissa, val);
360 : 0 : mtr_init.next_table = def_policy->sub_policy.tbl_rsc->obj;
361 : 0 : mtr_init.reg_c_index = priv->sh->registers.aso_reg - REG_C_0;
362 : 0 : mtr_init.flow_meter_parameter = fmp;
363 : 0 : mtr_init.flow_meter_parameter_sz =
364 : : MLX5_ST_SZ_BYTES(flow_meter_parameters);
365 : 0 : mtr_init.active = fm->active_state;
366 : 0 : return mlx5_glue->dv_create_flow_action_meter(&mtr_init);
367 : : #else
368 : : (void)priv;
369 : : (void)fm;
370 : : return NULL;
371 : : #endif
372 : : }
373 : :
374 : : /**
375 : : * Find meter profile by id.
376 : : *
377 : : * @param priv
378 : : * Pointer to mlx5_priv.
379 : : * @param meter_profile_id
380 : : * Meter profile id.
381 : : *
382 : : * @return
383 : : * Pointer to the profile found on success, NULL otherwise.
384 : : */
385 : : static struct mlx5_flow_meter_profile *
386 : 0 : mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
387 : : {
388 : : struct mlx5_flow_meter_profile *fmp;
389 : : union mlx5_l3t_data data;
390 : : int32_t ret;
391 : :
392 [ # # ]: 0 : if (priv->mtr_profile_arr)
393 : 0 : return &priv->mtr_profile_arr[meter_profile_id];
394 [ # # # # ]: 0 : if (!priv->mtr_profile_tbl ||
395 [ # # ]: 0 : mlx5_l3t_get_entry(priv->mtr_profile_tbl, meter_profile_id, &data) || !data.ptr)
396 : 0 : return NULL;
397 : : fmp = data.ptr;
398 : : /* Remove reference taken by the mlx5_l3t_get_entry. */
399 : 0 : ret = mlx5_l3t_clear_entry(priv->mtr_profile_tbl,
400 : : meter_profile_id);
401 [ # # ]: 0 : if (!ret || ret == -1)
402 : 0 : return NULL;
403 : : return fmp;
404 : : }
405 : :
406 : : /**
407 : : * Validate the MTR profile.
408 : : *
409 : : * @param[in] dev
410 : : * Pointer to Ethernet device.
411 : : * @param[in] meter_profile_id
412 : : * Meter profile id.
413 : : * @param[in] profile
414 : : * Pointer to meter profile detail.
415 : : * @param[out] error
416 : : * Pointer to the error structure.
417 : : *
418 : : * @return
419 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
420 : : */
421 : : static int
422 : 0 : mlx5_flow_meter_profile_validate(struct rte_eth_dev *dev,
423 : : uint32_t meter_profile_id,
424 : : struct rte_mtr_meter_profile *profile,
425 : : struct rte_mtr_error *error)
426 : : {
427 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
428 : : struct mlx5_flow_meter_profile *fmp;
429 : : uint32_t ls_factor;
430 : : int ret;
431 : : uint64_t cir, cbs;
432 : : uint64_t eir, ebs;
433 : : uint64_t pir, pbs;
434 : :
435 : : /* Profile must not be NULL. */
436 [ # # ]: 0 : if (profile == NULL)
437 : 0 : return -rte_mtr_error_set(error, EINVAL,
438 : : RTE_MTR_ERROR_TYPE_METER_PROFILE,
439 : : NULL, "Meter profile is null.");
440 : : /* Meter profile ID must be valid. */
441 [ # # ]: 0 : if (priv->mtr_profile_arr) {
442 [ # # ]: 0 : if (meter_profile_id >= priv->mtr_config.nb_meter_profiles)
443 : 0 : return -rte_mtr_error_set(error, EINVAL,
444 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
445 : : NULL, "Meter profile id not valid.");
446 : 0 : fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
447 : : /* Meter profile must not exist. */
448 [ # # ]: 0 : if (fmp->initialized)
449 : 0 : return -rte_mtr_error_set(error, EEXIST,
450 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
451 : : NULL, "Meter profile already exists.");
452 : : } else {
453 [ # # ]: 0 : if (meter_profile_id == UINT32_MAX)
454 : 0 : return -rte_mtr_error_set(error, EINVAL,
455 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
456 : : NULL, "Meter profile id not valid.");
457 : 0 : fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
458 : : /* Meter profile must not exist. */
459 [ # # ]: 0 : if (fmp)
460 : 0 : return -rte_mtr_error_set(error, EEXIST,
461 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
462 : : NULL, "Meter profile already exists.");
463 : : }
464 [ # # ]: 0 : if (!priv->sh->meter_aso_en) {
465 : : /* Old version is even not supported. */
466 [ # # ]: 0 : if (!priv->sh->cdev->config.hca_attr.qos.flow_meter_old)
467 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
468 : : RTE_MTR_ERROR_TYPE_METER_PROFILE,
469 : : NULL, "Metering is not supported.");
470 : : /* Old FW metering only supports srTCM. */
471 [ # # ]: 0 : if (profile->alg != RTE_MTR_SRTCM_RFC2697) {
472 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
473 : : RTE_MTR_ERROR_TYPE_METER_PROFILE,
474 : : NULL, "Metering algorithm is not supported.");
475 [ # # ]: 0 : } else if (profile->srtcm_rfc2697.ebs) {
476 : : /* EBS is not supported for old metering. */
477 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
478 : : RTE_MTR_ERROR_TYPE_METER_PROFILE,
479 : : NULL, "EBS is not supported.");
480 : : }
481 [ # # ]: 0 : if (profile->packet_mode)
482 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
483 : : RTE_MTR_ERROR_TYPE_METER_PROFILE, NULL,
484 : : "Metering algorithm packet mode is not supported.");
485 : : }
486 [ # # ]: 0 : ls_factor = profile->packet_mode ? MLX5_MTRS_PPS_MAP_BPS_SHIFT : 0;
487 [ # # # # ]: 0 : switch (profile->alg) {
488 : 0 : case RTE_MTR_SRTCM_RFC2697:
489 : 0 : cir = profile->srtcm_rfc2697.cir << ls_factor;
490 : 0 : cbs = profile->srtcm_rfc2697.cbs << ls_factor;
491 : 0 : ebs = profile->srtcm_rfc2697.ebs << ls_factor;
492 : : /* EBS could be zero for old metering. */
493 [ # # ]: 0 : if (cir > 0 && cir <= MLX5_SRTCM_XIR_MAX &&
494 [ # # # # ]: 0 : cbs > 0 && cbs <= MLX5_SRTCM_XBS_MAX &&
495 : : ebs <= MLX5_SRTCM_XBS_MAX) {
496 : : ret = 0;
497 : : } else {
498 : : ret = -rte_mtr_error_set(error, ENOTSUP,
499 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
500 : : "Profile values out of range.");
501 : : }
502 : : break;
503 : 0 : case RTE_MTR_TRTCM_RFC2698:
504 : 0 : cir = profile->trtcm_rfc2698.cir << ls_factor;
505 : 0 : cbs = profile->trtcm_rfc2698.cbs << ls_factor;
506 : 0 : pir = profile->trtcm_rfc2698.pir << ls_factor;
507 : 0 : pbs = profile->trtcm_rfc2698.pbs << ls_factor;
508 [ # # ]: 0 : if (cir > 0 && cir <= MLX5_SRTCM_XIR_MAX &&
509 [ # # ]: 0 : cbs > 0 && cbs <= MLX5_SRTCM_XBS_MAX &&
510 [ # # ]: 0 : pir >= cir && pir <= (MLX5_SRTCM_XIR_MAX * 2) &&
511 [ # # ]: 0 : pbs >= cbs && pbs <= (MLX5_SRTCM_XBS_MAX * 2)) {
512 : : ret = 0;
513 : : } else {
514 : : ret = -rte_mtr_error_set(error, ENOTSUP,
515 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
516 : : "Profile values out of range.");
517 : : }
518 : : break;
519 : 0 : case RTE_MTR_TRTCM_RFC4115:
520 : 0 : cir = profile->trtcm_rfc4115.cir << ls_factor;
521 : 0 : cbs = profile->trtcm_rfc4115.cbs << ls_factor;
522 : 0 : eir = profile->trtcm_rfc4115.eir << ls_factor;
523 : 0 : ebs = profile->trtcm_rfc4115.ebs << ls_factor;
524 [ # # ]: 0 : if (cir > 0 && cir <= MLX5_SRTCM_XIR_MAX &&
525 [ # # ]: 0 : cbs > 0 && cbs <= MLX5_SRTCM_XBS_MAX &&
526 [ # # ]: 0 : eir <= MLX5_SRTCM_XIR_MAX && ebs <= MLX5_SRTCM_XBS_MAX) {
527 : : ret = 0;
528 : : } else {
529 : : ret = -rte_mtr_error_set(error, ENOTSUP,
530 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
531 : : "Profile values out of range.");
532 : : }
533 : : break;
534 : : default:
535 : : ret = -rte_mtr_error_set(error, ENOTSUP,
536 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
537 : : "Unknown metering algorithm.");
538 : 0 : break;
539 : : }
540 : : return ret;
541 : : }
542 : :
543 : : /*
544 : : * Calculate mantissa and exponent for cir / eir.
545 : : *
546 : : * @param[in] xir
547 : : * Value to be calculated.
548 : : * @param[out] man
549 : : * Pointer to the mantissa.
550 : : * @param[out] exp
551 : : * Pointer to the exp.
552 : : */
553 : : static inline void
554 : : mlx5_flow_meter_xir_man_exp_calc(int64_t xir, uint8_t *man, uint8_t *exp)
555 : : {
556 : : int64_t _xir;
557 : : int64_t delta = INT64_MAX;
558 : : uint8_t _man = 0;
559 : : uint8_t _exp = 0;
560 : : uint64_t m, e;
561 : :
562 : : /* Special case xir == 0 ? both exp and mantissa are 0. */
563 : 0 : if (xir == 0) {
564 : 0 : *man = 0;
565 : 0 : *exp = 0;
566 : 0 : return;
567 : : }
568 [ # # # # ]: 0 : for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
569 [ # # # # ]: 0 : for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
570 : 0 : _xir = (1000000000ULL * m) >> e;
571 [ # # # # ]: 0 : if (llabs(xir - _xir) <= delta) {
572 : : delta = llabs(xir - _xir);
573 : 0 : _man = m;
574 : 0 : _exp = e;
575 : : }
576 : : }
577 : : }
578 : 0 : *man = _man;
579 : 0 : *exp = _exp;
580 : : }
581 : :
582 : : /*
583 : : * Calculate mantissa and exponent for xbs.
584 : : *
585 : : * @param[in] xbs
586 : : * Value to be calculated.
587 : : * @param[out] man
588 : : * Pointer to the mantissa.
589 : : * @param[out] exp
590 : : * Pointer to the exp.
591 : : */
592 : : static inline void
593 : 0 : mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
594 : : {
595 : : int _exp;
596 : : double _man;
597 : :
598 : : /* Special case xbs == 0 ? both exp and mantissa are 0. */
599 [ # # ]: 0 : if (xbs == 0) {
600 : 0 : *man = 0;
601 : 0 : *exp = 0;
602 : 0 : return;
603 : : }
604 : : /* xbs = xbs_mantissa * 2^xbs_exponent */
605 : 0 : _man = frexp(xbs, &_exp);
606 [ # # ]: 0 : if (_exp >= MLX5_MAN_WIDTH) {
607 : 0 : _man = _man * pow(2, MLX5_MAN_WIDTH);
608 : 0 : _exp = _exp - MLX5_MAN_WIDTH;
609 : : }
610 : 0 : *man = (uint8_t)ceil(_man);
611 : 0 : *exp = _exp;
612 : : }
613 : :
614 : : /**
615 : : * Fill the prm meter parameter.
616 : : *
617 : : * @param[in,out] fmp
618 : : * Pointer to meter profile to be converted.
619 : : * @param[out] error
620 : : * Pointer to the error structure.
621 : : *
622 : : * @return
623 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
624 : : */
625 : : static int
626 : 0 : mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
627 : : struct rte_mtr_error *error)
628 : : {
629 : : struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
630 : : uint8_t man, exp;
631 : : uint32_t cbs_exp, cbs_man, cir_exp, cir_man;
632 : : uint32_t eir_exp, eir_man, ebs_exp, ebs_man;
633 : : uint64_t cir, cbs, eir, ebs;
634 : :
635 [ # # # # ]: 0 : switch (fmp->profile.alg) {
636 : 0 : case RTE_MTR_SRTCM_RFC2697:
637 : 0 : cir = fmp->profile.srtcm_rfc2697.cir;
638 : 0 : cbs = fmp->profile.srtcm_rfc2697.cbs;
639 : : eir = 0;
640 : 0 : ebs = fmp->profile.srtcm_rfc2697.ebs;
641 : 0 : break;
642 : 0 : case RTE_MTR_TRTCM_RFC2698:
643 : : MLX5_ASSERT(fmp->profile.trtcm_rfc2698.pir >
644 : : fmp->profile.trtcm_rfc2698.cir &&
645 : : fmp->profile.trtcm_rfc2698.pbs >
646 : : fmp->profile.trtcm_rfc2698.cbs);
647 : 0 : cir = fmp->profile.trtcm_rfc2698.cir;
648 : 0 : cbs = fmp->profile.trtcm_rfc2698.cbs;
649 : : /* EIR / EBS are filled with PIR / PBS. */
650 : 0 : eir = fmp->profile.trtcm_rfc2698.pir;
651 : 0 : ebs = fmp->profile.trtcm_rfc2698.pbs;
652 : 0 : break;
653 : 0 : case RTE_MTR_TRTCM_RFC4115:
654 : 0 : cir = fmp->profile.trtcm_rfc4115.cir;
655 : 0 : cbs = fmp->profile.trtcm_rfc4115.cbs;
656 : 0 : eir = fmp->profile.trtcm_rfc4115.eir;
657 : 0 : ebs = fmp->profile.trtcm_rfc4115.ebs;
658 : 0 : break;
659 : : default:
660 : 0 : return -rte_mtr_error_set(error, EINVAL,
661 : : RTE_MTR_ERROR_TYPE_METER_PROFILE, NULL,
662 : : "Metering algorithm mode is invalid");
663 : : }
664 : : /* Adjust the values for PPS mode. */
665 [ # # ]: 0 : if (fmp->profile.packet_mode) {
666 : 0 : cir <<= MLX5_MTRS_PPS_MAP_BPS_SHIFT;
667 : 0 : cbs <<= MLX5_MTRS_PPS_MAP_BPS_SHIFT;
668 : 0 : eir <<= MLX5_MTRS_PPS_MAP_BPS_SHIFT;
669 : 0 : ebs <<= MLX5_MTRS_PPS_MAP_BPS_SHIFT;
670 : : }
671 : : /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
672 [ # # ]: 0 : mlx5_flow_meter_xir_man_exp_calc(cir, &man, &exp);
673 : : /* Check if cir mantissa is too large. */
674 [ # # ]: 0 : if (exp > ASO_DSEG_XIR_EXP_MASK)
675 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
676 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
677 : : "meter profile parameter cir is not supported.");
678 : 0 : cir_man = man;
679 : 0 : cir_exp = exp;
680 : : /* cbs = cbs_mantissa * 2^cbs_exponent */
681 : 0 : mlx5_flow_meter_xbs_man_exp_calc(cbs, &man, &exp);
682 : : /* Check if cbs mantissa is too large. */
683 [ # # ]: 0 : if (exp > ASO_DSEG_EXP_MASK)
684 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
685 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
686 : : "meter profile parameter cbs is not supported.");
687 : 0 : cbs_man = man;
688 : 0 : cbs_exp = exp;
689 [ # # ]: 0 : srtcm->cbs_cir = rte_cpu_to_be_32(cbs_exp << ASO_DSEG_CBS_EXP_OFFSET |
690 : : cbs_man << ASO_DSEG_CBS_MAN_OFFSET |
691 : : cir_exp << ASO_DSEG_XIR_EXP_OFFSET |
692 : : cir_man);
693 [ # # ]: 0 : mlx5_flow_meter_xir_man_exp_calc(eir, &man, &exp);
694 : : /* Check if eir mantissa is too large. */
695 [ # # ]: 0 : if (exp > ASO_DSEG_XIR_EXP_MASK)
696 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
697 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
698 : : "meter profile parameter eir is not supported.");
699 : 0 : eir_man = man;
700 : 0 : eir_exp = exp;
701 : 0 : mlx5_flow_meter_xbs_man_exp_calc(ebs, &man, &exp);
702 : : /* Check if ebs mantissa is too large. */
703 [ # # ]: 0 : if (exp > ASO_DSEG_EXP_MASK)
704 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
705 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
706 : : "meter profile parameter ebs is not supported.");
707 : 0 : ebs_man = man;
708 : 0 : ebs_exp = exp;
709 [ # # ]: 0 : srtcm->ebs_eir = rte_cpu_to_be_32(ebs_exp << ASO_DSEG_EBS_EXP_OFFSET |
710 : : ebs_man << ASO_DSEG_EBS_MAN_OFFSET |
711 : : eir_exp << ASO_DSEG_XIR_EXP_OFFSET |
712 : : eir_man);
713 [ # # ]: 0 : if (srtcm->cbs_cir)
714 : 0 : fmp->g_support = 1;
715 [ # # ]: 0 : if (srtcm->ebs_eir)
716 : 0 : fmp->y_support = 1;
717 : : return 0;
718 : : }
719 : :
720 : : /**
721 : : * Callback to get MTR maximum objects number.
722 : : *
723 : : * @param[in] priv
724 : : * Pointer to Ethernet device.
725 : : *
726 : : * @return
727 : : * Max number of meters.
728 : : */
729 : : uint32_t
730 : 0 : mlx5_flow_mtr_max_get(struct mlx5_priv *priv)
731 : : {
732 : 0 : struct mlx5_hca_qos_attr *qattr = &priv->sh->cdev->config.hca_attr.qos;
733 : :
734 : : /* Max number of meters. */
735 : 0 : return ((priv->sh->meter_aso_en) ?
736 [ # # ]: 0 : 1 << (qattr->log_max_num_meter_aso + 1) :
737 : 0 : qattr->log_max_flow_meter);
738 : : }
739 : :
740 : : /**
741 : : * Callback to get MTR capabilities.
742 : : *
743 : : * @param[in] dev
744 : : * Pointer to Ethernet device.
745 : : * @param[out] cap
746 : : * Pointer to save MTR capabilities.
747 : : * @param[out] error
748 : : * Pointer to the error structure.
749 : : *
750 : : * @return
751 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
752 : : */
753 : : static int
754 : 0 : mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
755 : : struct rte_mtr_capabilities *cap,
756 : : struct rte_mtr_error *error __rte_unused)
757 : : {
758 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
759 [ # # ]: 0 : struct mlx5_hca_qos_attr *qattr = &priv->sh->cdev->config.hca_attr.qos;
760 : :
761 [ # # # # ]: 0 : if (mlx5_hws_active(dev) && !mlx5_hw_ctx_validate(dev, NULL))
762 : 0 : return -rte_mtr_error_set(error, EINVAL,
763 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
764 : : "non-template flow engine was not configured");
765 [ # # ]: 0 : if (!priv->mtr_en)
766 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
767 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
768 : : "Meter is not supported");
769 : : memset(cap, 0, sizeof(*cap));
770 : 0 : cap->n_max = mlx5_flow_mtr_max_get(priv);
771 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
772 : 0 : cap->srtcm_rfc2697_packet_mode_supported = 1;
773 : 0 : cap->trtcm_rfc2698_packet_mode_supported = 1;
774 : 0 : cap->trtcm_rfc4115_packet_mode_supported = 1;
775 : : }
776 : 0 : cap->srtcm_rfc2697_byte_mode_supported = 1;
777 : 0 : cap->trtcm_rfc2698_byte_mode_supported = 1;
778 : 0 : cap->trtcm_rfc4115_byte_mode_supported = 1;
779 : 0 : cap->n_shared_max = cap->n_max;
780 : 0 : cap->identical = 1;
781 : 0 : cap->shared_identical = 1;
782 : 0 : cap->shared_n_flows_per_mtr_max = 4 << 20;
783 : : /* 2M flows can share the same meter. */
784 : 0 : cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
785 [ # # ]: 0 : cap->meter_srtcm_rfc2697_n_max = qattr->flow_meter_old ? cap->n_max : 0;
786 : 0 : cap->meter_trtcm_rfc2698_n_max = qattr->flow_meter_old ? cap->n_max : 0;
787 : 0 : cap->meter_trtcm_rfc4115_n_max = qattr->flow_meter_old ? cap->n_max : 0;
788 : 0 : cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
789 : 0 : cap->meter_policy_n_max = cap->n_max;
790 : 0 : cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
791 : : RTE_MTR_STATS_N_PKTS_DROPPED;
792 : 0 : return 0;
793 : : }
794 : :
795 : : /**
796 : : * Callback to add MTR profile.
797 : : *
798 : : * @param[in] dev
799 : : * Pointer to Ethernet device.
800 : : * @param[in] meter_profile_id
801 : : * Meter profile id.
802 : : * @param[in] profile
803 : : * Pointer to meter profile detail.
804 : : * @param[out] error
805 : : * Pointer to the error structure.
806 : : *
807 : : * @return
808 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
809 : : */
810 : : static int
811 : 0 : mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
812 : : uint32_t meter_profile_id,
813 : : struct rte_mtr_meter_profile *profile,
814 : : struct rte_mtr_error *error)
815 : : {
816 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
817 : : struct mlx5_flow_meter_profile *fmp;
818 : : union mlx5_l3t_data data;
819 : : int ret;
820 : :
821 [ # # ]: 0 : if (!priv->mtr_en)
822 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
823 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
824 : : "Meter is not supported");
825 : : /* Check input params. */
826 : 0 : ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
827 : : profile, error);
828 [ # # ]: 0 : if (ret)
829 : : return ret;
830 : : /* Meter profile memory allocation. */
831 : 0 : fmp = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_flow_meter_profile),
832 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
833 [ # # ]: 0 : if (fmp == NULL)
834 : 0 : return -rte_mtr_error_set(error, ENOMEM,
835 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED,
836 : : NULL, "Meter profile memory "
837 : : "alloc failed.");
838 : : /* Fill profile info. */
839 : 0 : fmp->id = meter_profile_id;
840 : 0 : fmp->profile = *profile;
841 : : /* Fill the flow meter parameters for the PRM. */
842 : 0 : ret = mlx5_flow_meter_param_fill(fmp, error);
843 [ # # ]: 0 : if (ret)
844 : 0 : goto error;
845 : 0 : data.ptr = fmp;
846 : 0 : ret = mlx5_l3t_set_entry(priv->mtr_profile_tbl,
847 : : meter_profile_id, &data);
848 [ # # ]: 0 : if (ret)
849 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
850 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED,
851 : : NULL, "Meter profile insert fail.");
852 : : return 0;
853 : : error:
854 : 0 : mlx5_free(fmp);
855 : 0 : return ret;
856 : : }
857 : :
858 : : /**
859 : : * Callback to delete MTR profile.
860 : : *
861 : : * @param[in] dev
862 : : * Pointer to Ethernet device.
863 : : * @param[in] meter_profile_id
864 : : * Meter profile id.
865 : : * @param[out] error
866 : : * Pointer to the error structure.
867 : : *
868 : : * @return
869 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
870 : : */
871 : : static int
872 : 0 : mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
873 : : uint32_t meter_profile_id,
874 : : struct rte_mtr_error *error)
875 : : {
876 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
877 : : struct mlx5_flow_meter_profile *fmp;
878 : :
879 [ # # ]: 0 : if (!priv->mtr_en)
880 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
881 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
882 : : "Meter is not supported");
883 : : /* Meter profile must exist. */
884 : 0 : fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
885 [ # # ]: 0 : if (fmp == NULL)
886 : 0 : return -rte_mtr_error_set(error, ENOENT,
887 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
888 : : &meter_profile_id,
889 : : "Meter profile id is invalid.");
890 : : /* Check profile is unused. */
891 [ # # ]: 0 : if (fmp->ref_cnt)
892 : 0 : return -rte_mtr_error_set(error, EBUSY,
893 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
894 : : NULL, "Meter profile is in use.");
895 [ # # ]: 0 : if (mlx5_l3t_clear_entry(priv->mtr_profile_tbl, meter_profile_id))
896 : 0 : return -rte_mtr_error_set(error, EBUSY,
897 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
898 : : NULL, "Meter profile remove fail.");
899 : 0 : mlx5_free(fmp);
900 : 0 : return 0;
901 : : }
902 : :
903 : : /**
904 : : * Callback to get MTR profile.
905 : : *
906 : : * @param[in] dev
907 : : * Pointer to Ethernet device.
908 : : * @param[in] meter_profile_id
909 : : * Meter profile id.
910 : : * @param[out] error
911 : : * Pointer to the error structure.
912 : : *
913 : : * @return
914 : : * A valid handle in case of success, NULL otherwise.
915 : : */
916 : : static struct rte_flow_meter_profile *
917 : 0 : mlx5_flow_meter_profile_get(struct rte_eth_dev *dev,
918 : : uint32_t meter_profile_id,
919 : : struct rte_mtr_error *error)
920 : : {
921 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
922 : :
923 [ # # # # ]: 0 : if (mlx5_hws_active(dev) && !mlx5_hw_ctx_validate(dev, NULL)) {
924 : : rte_mtr_error_set(error, EINVAL,
925 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
926 : : "non-template flow engine was not configured");
927 : 0 : return NULL;
928 : : }
929 [ # # ]: 0 : if (!priv->mtr_en) {
930 : : rte_mtr_error_set(error, ENOTSUP,
931 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
932 : : "Meter is not supported");
933 : 0 : return NULL;
934 : : }
935 : 0 : return (void *)(uintptr_t)mlx5_flow_meter_profile_find(priv,
936 : : meter_profile_id);
937 : : }
938 : :
939 : : #if defined(HAVE_MLX5_HWS_SUPPORT)
940 : : /**
941 : : * Callback to add MTR profile with HWS.
942 : : *
943 : : * @param[in] dev
944 : : * Pointer to Ethernet device.
945 : : * @param[in] meter_profile_id
946 : : * Meter profile id.
947 : : * @param[in] profile
948 : : * Pointer to meter profile detail.
949 : : * @param[out] error
950 : : * Pointer to the error structure.
951 : : *
952 : : * @return
953 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
954 : : */
955 : : static int
956 : 0 : mlx5_flow_meter_profile_hws_add(struct rte_eth_dev *dev,
957 : : uint32_t meter_profile_id,
958 : : struct rte_mtr_meter_profile *profile,
959 : : struct rte_mtr_error *error)
960 : : {
961 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
962 : : struct mlx5_flow_meter_profile *fmp;
963 : : int ret;
964 : :
965 [ # # # # ]: 0 : if (mlx5_hws_active(dev) && !mlx5_hw_ctx_validate(dev, NULL))
966 : 0 : return -rte_mtr_error_set(error, EINVAL,
967 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
968 : : "non-template flow engine was not configured");
969 [ # # ]: 0 : if (priv->shared_host)
970 : 0 : return -rte_mtr_error_set(error, ENOTSUP, RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
971 : : "Meter profiles cannot be created on guest port");
972 [ # # ]: 0 : if (!priv->mtr_profile_arr)
973 : 0 : return mlx5_flow_meter_profile_add(dev, meter_profile_id, profile, error);
974 : : /* Check input params. */
975 : 0 : ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
976 : : profile, error);
977 [ # # ]: 0 : if (ret)
978 : : return ret;
979 : 0 : fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
980 : : /* Fill profile info. */
981 : 0 : fmp->id = meter_profile_id;
982 : 0 : fmp->profile = *profile;
983 : 0 : fmp->initialized = 1;
984 : : /* Fill the flow meter parameters for the PRM. */
985 : 0 : return mlx5_flow_meter_param_fill(fmp, error);
986 : : }
987 : :
988 : : /**
989 : : * Callback to delete MTR profile with HWS.
990 : : *
991 : : * @param[in] dev
992 : : * Pointer to Ethernet device.
993 : : * @param[in] meter_profile_id
994 : : * Meter profile id.
995 : : * @param[out] error
996 : : * Pointer to the error structure.
997 : : *
998 : : * @return
999 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1000 : : */
1001 : : static int
1002 : 0 : mlx5_flow_meter_profile_hws_delete(struct rte_eth_dev *dev,
1003 : : uint32_t meter_profile_id,
1004 : : struct rte_mtr_error *error)
1005 : : {
1006 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1007 : : struct mlx5_flow_meter_profile *fmp;
1008 : :
1009 [ # # ]: 0 : if (priv->shared_host)
1010 : 0 : return -rte_mtr_error_set(error, ENOTSUP, RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1011 : : "Meter profiles cannot be destroyed through guest port");
1012 [ # # ]: 0 : if (!priv->mtr_profile_arr)
1013 : 0 : return mlx5_flow_meter_profile_delete(dev, meter_profile_id, error);
1014 : : /* Meter profile must exist. */
1015 : 0 : fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
1016 [ # # ]: 0 : if (!fmp->initialized)
1017 : 0 : return -rte_mtr_error_set(error, ENOENT,
1018 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1019 : : &meter_profile_id,
1020 : : "Meter profile id is invalid.");
1021 : : /* Check profile is unused. */
1022 [ # # ]: 0 : if (fmp->ref_cnt)
1023 : 0 : return -rte_mtr_error_set(error, EBUSY,
1024 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1025 : : NULL, "Meter profile is in use.");
1026 : : memset(fmp, 0, sizeof(struct mlx5_flow_meter_profile));
1027 : 0 : return 0;
1028 : : }
1029 : : #endif
1030 : :
1031 : : /**
1032 : : * Find policy by id.
1033 : : *
1034 : : * @param[in] dev
1035 : : * Pointer to Ethernet device.
1036 : : * @param policy_id
1037 : : * Policy id.
1038 : : *
1039 : : * @return
1040 : : * Pointer to the policy found on success, NULL otherwise.
1041 : : */
1042 : : struct mlx5_flow_meter_policy *
1043 : 0 : mlx5_flow_meter_policy_find(struct rte_eth_dev *dev,
1044 : : uint32_t policy_id,
1045 : : uint32_t *policy_idx)
1046 : : {
1047 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1048 : : struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
1049 : : union mlx5_l3t_data data;
1050 : :
1051 [ # # ]: 0 : if (priv->mtr_policy_arr) {
1052 [ # # ]: 0 : if (policy_idx)
1053 : 0 : *policy_idx = policy_id;
1054 : 0 : return &priv->mtr_policy_arr[policy_id];
1055 : : }
1056 [ # # # # ]: 0 : if (policy_id > MLX5_MAX_SUB_POLICY_TBL_NUM || !priv->policy_idx_tbl)
1057 : : return NULL;
1058 [ # # ]: 0 : if (mlx5_l3t_get_entry(priv->policy_idx_tbl, policy_id, &data) ||
1059 [ # # ]: 0 : !data.dword)
1060 : : return NULL;
1061 [ # # ]: 0 : if (policy_idx)
1062 : 0 : *policy_idx = data.dword;
1063 : 0 : sub_policy = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
1064 : : data.dword);
1065 : : /* Remove reference taken by the mlx5_l3t_get_entry. */
1066 : 0 : mlx5_l3t_clear_entry(priv->policy_idx_tbl, policy_id);
1067 [ # # ]: 0 : if (sub_policy)
1068 [ # # ]: 0 : if (sub_policy->main_policy_id)
1069 : 0 : return sub_policy->main_policy;
1070 : : return NULL;
1071 : : }
1072 : :
1073 : : /**
1074 : : * Get the next meter from one meter's policy in hierarchy chain.
1075 : : * Lock free, mutex should be acquired by caller.
1076 : : *
1077 : : * @param[in] priv
1078 : : * Pointer to mlx5_priv.
1079 : : * @param[in] policy
1080 : : * Pointer to flow meter policy.
1081 : : * @param[out] mtr_idx
1082 : : * Pointer to Meter index.
1083 : : *
1084 : : * @return
1085 : : * Pointer to the next meter, or NULL when fail.
1086 : : */
1087 : : struct mlx5_flow_meter_info *
1088 : 0 : mlx5_flow_meter_hierarchy_next_meter(struct mlx5_priv *priv,
1089 : : struct mlx5_flow_meter_policy *policy,
1090 : : uint32_t *mtr_idx)
1091 : : {
1092 : : int i;
1093 : :
1094 [ # # ]: 0 : for (i = 0; i < MLX5_MTR_RTE_COLORS; i++) {
1095 [ # # ]: 0 : if (policy->act_cnt[i].fate_action == MLX5_FLOW_FATE_MTR)
1096 : 0 : return mlx5_flow_meter_find(priv,
1097 : : policy->act_cnt[i].next_mtr_id,
1098 : : mtr_idx);
1099 : : }
1100 : : return NULL;
1101 : : }
1102 : :
1103 : : /**
1104 : : * Get the last meter's policy from one meter's policy in hierarchy.
1105 : : *
1106 : : * @param[in] dev
1107 : : * Pointer to Ethernet device.
1108 : : * @param[in] policy
1109 : : * Pointer to flow meter policy.
1110 : : *
1111 : : * @return
1112 : : * Pointer to the final meter's policy, or NULL when fail.
1113 : : */
1114 : : struct mlx5_flow_meter_policy *
1115 : 0 : mlx5_flow_meter_hierarchy_get_final_policy(struct rte_eth_dev *dev,
1116 : : struct mlx5_flow_meter_policy *policy)
1117 : : {
1118 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1119 : : struct mlx5_flow_meter_info *next_fm;
1120 : : struct mlx5_flow_meter_policy *next_policy = policy;
1121 : :
1122 [ # # ]: 0 : while (next_policy->is_hierarchy) {
1123 : 0 : rte_spinlock_lock(&next_policy->sl);
1124 : 0 : next_fm = mlx5_flow_meter_hierarchy_next_meter(priv, next_policy, NULL);
1125 : : rte_spinlock_unlock(&next_policy->sl);
1126 [ # # # # ]: 0 : if (!next_fm || next_fm->def_policy)
1127 : : return NULL;
1128 : 0 : next_policy = mlx5_flow_meter_policy_find(dev,
1129 : : next_fm->policy_id, NULL);
1130 : : MLX5_ASSERT(next_policy);
1131 : : }
1132 : : return next_policy;
1133 : : }
1134 : :
1135 : : /**
1136 : : * Callback to check MTR policy action validate
1137 : : *
1138 : : * @param[in] dev
1139 : : * Pointer to Ethernet device.
1140 : : * @param[in] actions
1141 : : * Pointer to meter policy action detail.
1142 : : * @param[out] error
1143 : : * Pointer to the error structure.
1144 : : *
1145 : : * @return
1146 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1147 : : */
1148 : : static int
1149 : 0 : mlx5_flow_meter_policy_validate(struct rte_eth_dev *dev,
1150 : : struct rte_mtr_meter_policy_params *policy,
1151 : : struct rte_mtr_error *error)
1152 : : {
1153 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1154 : 0 : struct rte_flow_attr attr = { .transfer = priv->sh->config.dv_esw_en ?
1155 : 0 : 1 : 0 };
1156 : 0 : bool is_rss = false;
1157 : : uint8_t policy_mode;
1158 : : uint8_t domain_bitmap;
1159 : : int ret;
1160 : :
1161 [ # # # # ]: 0 : if (!priv->mtr_en || !priv->sh->meter_aso_en)
1162 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1163 : : RTE_MTR_ERROR_TYPE_METER_POLICY,
1164 : : NULL, "meter policy unsupported.");
1165 : 0 : ret = mlx5_flow_validate_mtr_acts(dev, policy->actions, &attr,
1166 : : &is_rss, &domain_bitmap, &policy_mode, error);
1167 [ # # ]: 0 : if (ret)
1168 : 0 : return ret;
1169 : : return 0;
1170 : : }
1171 : :
1172 : : static int
1173 : 0 : __mlx5_flow_meter_policy_delete(struct rte_eth_dev *dev,
1174 : : uint32_t policy_id,
1175 : : struct mlx5_flow_meter_policy *mtr_policy,
1176 : : struct rte_mtr_error *error,
1177 : : bool clear_l3t)
1178 : : {
1179 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1180 : : struct mlx5_flow_meter_sub_policy *sub_policy;
1181 : : uint32_t i, j;
1182 : : uint16_t sub_policy_num;
1183 : :
1184 : 0 : rte_spinlock_lock(&mtr_policy->sl);
1185 [ # # ]: 0 : if (mtr_policy->ref_cnt) {
1186 : : rte_spinlock_unlock(&mtr_policy->sl);
1187 : 0 : return -rte_mtr_error_set(error, EBUSY,
1188 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1189 : : NULL,
1190 : : "Meter policy object is being used.");
1191 : : }
1192 : 0 : mlx5_flow_destroy_policy_rules(dev, mtr_policy);
1193 : 0 : mlx5_flow_destroy_mtr_acts(dev, mtr_policy);
1194 [ # # ]: 0 : for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
1195 : 0 : sub_policy_num = (mtr_policy->sub_policy_num >>
1196 : 0 : (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
1197 : : MLX5_MTR_SUB_POLICY_NUM_MASK;
1198 [ # # ]: 0 : if (sub_policy_num) {
1199 [ # # ]: 0 : for (j = 0; j < sub_policy_num; j++) {
1200 : 0 : sub_policy = mtr_policy->sub_policys[i][j];
1201 [ # # ]: 0 : if (sub_policy)
1202 : 0 : mlx5_ipool_free
1203 : 0 : (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
1204 : 0 : sub_policy->idx);
1205 : : }
1206 : : }
1207 : : }
1208 [ # # # # ]: 0 : if (priv->policy_idx_tbl && clear_l3t) {
1209 [ # # ]: 0 : if (mlx5_l3t_clear_entry(priv->policy_idx_tbl, policy_id)) {
1210 : : rte_spinlock_unlock(&mtr_policy->sl);
1211 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1212 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
1213 : : "Fail to delete policy in index table.");
1214 : : }
1215 : : }
1216 : : rte_spinlock_unlock(&mtr_policy->sl);
1217 : 0 : return 0;
1218 : : }
1219 : :
1220 : : /**
1221 : : * Callback to add MTR policy.
1222 : : *
1223 : : * @param[in] dev
1224 : : * Pointer to Ethernet device.
1225 : : * @param[out] policy_id
1226 : : * Pointer to policy id
1227 : : * @param[in] actions
1228 : : * Pointer to meter policy action detail.
1229 : : * @param[out] error
1230 : : * Pointer to the error structure.
1231 : : *
1232 : : * @return
1233 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1234 : : */
1235 : : static int
1236 : 0 : mlx5_flow_meter_policy_add(struct rte_eth_dev *dev,
1237 : : uint32_t policy_id,
1238 : : struct rte_mtr_meter_policy_params *policy,
1239 : : struct rte_mtr_error *error)
1240 : : {
1241 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1242 : 0 : struct rte_flow_attr attr = { .transfer = priv->sh->config.dv_esw_en ?
1243 : 0 : 1 : 0 };
1244 : 0 : uint32_t sub_policy_idx = 0;
1245 : 0 : uint32_t policy_idx = 0;
1246 : : struct mlx5_flow_meter_policy *mtr_policy = NULL;
1247 : : struct mlx5_flow_meter_sub_policy *sub_policy;
1248 : 0 : bool is_rss = false;
1249 : : uint8_t policy_mode;
1250 : : uint32_t i;
1251 : : int ret;
1252 : : uint32_t policy_size = sizeof(struct mlx5_flow_meter_policy);
1253 : : uint16_t sub_policy_num;
1254 : 0 : uint8_t domain_bitmap = 0;
1255 : : union mlx5_l3t_data data;
1256 : : bool skip_rule = false;
1257 : :
1258 [ # # ]: 0 : if (!priv->mtr_en)
1259 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1260 : : RTE_MTR_ERROR_TYPE_METER_POLICY,
1261 : : NULL, "meter policy unsupported. ");
1262 [ # # ]: 0 : if (policy_id == MLX5_INVALID_POLICY_ID)
1263 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1264 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1265 : : NULL, "policy ID is invalid. ");
1266 [ # # ]: 0 : if (policy_id == priv->sh->mtrmng->def_policy_id)
1267 : 0 : return -rte_mtr_error_set(error, EEXIST,
1268 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1269 : : NULL, "default policy ID exists. ");
1270 : 0 : mtr_policy = mlx5_flow_meter_policy_find(dev, policy_id, &policy_idx);
1271 [ # # ]: 0 : if (mtr_policy)
1272 : 0 : return -rte_mtr_error_set(error, EEXIST,
1273 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1274 : : NULL, "policy ID exists. ");
1275 : 0 : ret = mlx5_flow_validate_mtr_acts(dev, policy->actions, &attr,
1276 : : &is_rss, &domain_bitmap,
1277 : : &policy_mode, error);
1278 [ # # ]: 0 : if (ret)
1279 : : return ret;
1280 [ # # ]: 0 : if (!domain_bitmap)
1281 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1282 : : RTE_MTR_ERROR_TYPE_METER_POLICY,
1283 : : NULL, "fail to find policy domain.");
1284 [ # # ]: 0 : if (policy_mode == MLX5_MTR_POLICY_MODE_DEF) {
1285 [ # # ]: 0 : if (priv->sh->mtrmng->def_policy_id != MLX5_INVALID_POLICY_ID)
1286 : 0 : return -rte_mtr_error_set(error, EEXIST,
1287 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1288 : : NULL, "a policy with similar actions "
1289 : : "is already configured");
1290 [ # # ]: 0 : if (mlx5_flow_create_def_policy(dev))
1291 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1292 : : RTE_MTR_ERROR_TYPE_METER_POLICY,
1293 : : NULL,
1294 : : "fail to create non-terminated policy.");
1295 : 0 : priv->sh->mtrmng->def_policy_id = policy_id;
1296 : 0 : return 0;
1297 : : }
1298 [ # # ]: 0 : if (!priv->sh->meter_aso_en)
1299 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1300 : : RTE_MTR_ERROR_TYPE_METER_POLICY, NULL,
1301 : : "no ASO capability to support the policy ");
1302 [ # # ]: 0 : for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
1303 [ # # ]: 0 : if (!(domain_bitmap & (1 << i)))
1304 : 0 : continue;
1305 : : /*
1306 : : * If RSS is found, it means that only the ingress domain can
1307 : : * be supported. It is invalid to support RSS for one color
1308 : : * and egress / transfer domain actions for another. Drop and
1309 : : * jump action should have no impact.
1310 : : */
1311 [ # # ]: 0 : if (is_rss) {
1312 : 0 : policy_size +=
1313 : : sizeof(struct mlx5_flow_meter_sub_policy *) *
1314 : : MLX5_MTR_RSS_MAX_SUB_POLICY;
1315 : 0 : break;
1316 : : }
1317 : 0 : policy_size += sizeof(struct mlx5_flow_meter_sub_policy *);
1318 : : }
1319 : 0 : mtr_policy = mlx5_malloc(MLX5_MEM_ZERO, policy_size,
1320 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1321 [ # # ]: 0 : if (!mtr_policy)
1322 : 0 : return -rte_mtr_error_set(error, ENOMEM,
1323 : : RTE_MTR_ERROR_TYPE_METER_POLICY, NULL,
1324 : : "Memory alloc failed for meter policy.");
1325 [ # # ]: 0 : if (policy_mode == MLX5_MTR_POLICY_MODE_OG)
1326 : 0 : mtr_policy->skip_y = 1;
1327 [ # # ]: 0 : else if (policy_mode == MLX5_MTR_POLICY_MODE_OY)
1328 : 0 : mtr_policy->skip_g = 1;
1329 : : policy_size = sizeof(struct mlx5_flow_meter_policy);
1330 [ # # ]: 0 : for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
1331 [ # # ]: 0 : if (!(domain_bitmap & (1 << i)))
1332 : 0 : continue;
1333 [ # # ]: 0 : if (i == MLX5_MTR_DOMAIN_INGRESS)
1334 : 0 : mtr_policy->ingress = 1;
1335 [ # # ]: 0 : if (i == MLX5_MTR_DOMAIN_EGRESS)
1336 : 0 : mtr_policy->egress = 1;
1337 [ # # ]: 0 : if (i == MLX5_MTR_DOMAIN_TRANSFER)
1338 : 0 : mtr_policy->transfer = 1;
1339 : 0 : sub_policy = mlx5_ipool_zmalloc
1340 : 0 : (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
1341 : : &sub_policy_idx);
1342 [ # # ]: 0 : if (!sub_policy ||
1343 [ # # ]: 0 : sub_policy_idx > MLX5_MAX_SUB_POLICY_TBL_NUM)
1344 : 0 : goto policy_add_err;
1345 : 0 : sub_policy->idx = sub_policy_idx;
1346 : 0 : sub_policy->main_policy = mtr_policy;
1347 [ # # ]: 0 : if (!policy_idx) {
1348 : 0 : policy_idx = sub_policy_idx;
1349 : 0 : sub_policy->main_policy_id = 1;
1350 : : }
1351 : 0 : mtr_policy->sub_policys[i] =
1352 : 0 : (struct mlx5_flow_meter_sub_policy **)
1353 : : ((uint8_t *)mtr_policy + policy_size);
1354 : 0 : mtr_policy->sub_policys[i][0] = sub_policy;
1355 : 0 : sub_policy_num = (mtr_policy->sub_policy_num >>
1356 : 0 : (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
1357 : : MLX5_MTR_SUB_POLICY_NUM_MASK;
1358 : 0 : sub_policy_num++;
1359 : 0 : mtr_policy->sub_policy_num &= ~(MLX5_MTR_SUB_POLICY_NUM_MASK <<
1360 : : (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i));
1361 : 0 : mtr_policy->sub_policy_num |=
1362 : 0 : (sub_policy_num & MLX5_MTR_SUB_POLICY_NUM_MASK) <<
1363 : : (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i);
1364 : : /*
1365 : : * If RSS is found, it means that only the ingress domain can
1366 : : * be supported. It is invalid to support RSS for one color
1367 : : * and egress / transfer domain actions for another. Drop and
1368 : : * jump action should have no impact.
1369 : : */
1370 [ # # ]: 0 : if (is_rss) {
1371 : 0 : mtr_policy->is_rss = 1;
1372 : 0 : break;
1373 : : }
1374 : 0 : policy_size += sizeof(struct mlx5_flow_meter_sub_policy *);
1375 : : }
1376 : : rte_spinlock_init(&mtr_policy->sl);
1377 : 0 : ret = mlx5_flow_create_mtr_acts(dev, mtr_policy,
1378 : : policy->actions, &attr, error);
1379 [ # # ]: 0 : if (ret)
1380 : 0 : goto policy_add_err;
1381 [ # # ]: 0 : if (mtr_policy->is_hierarchy) {
1382 : : struct mlx5_flow_meter_policy *final_policy;
1383 : :
1384 : : final_policy =
1385 : 0 : mlx5_flow_meter_hierarchy_get_final_policy(dev, mtr_policy);
1386 [ # # ]: 0 : if (!final_policy)
1387 : 0 : goto policy_add_err;
1388 : 0 : skip_rule = (final_policy->is_rss || final_policy->is_queue);
1389 : : }
1390 : : /*
1391 : : * If either Green or Yellow has queue / RSS action, all the policy
1392 : : * rules will be created later in the flow splitting stage.
1393 : : */
1394 [ # # # # : 0 : if (!is_rss && !mtr_policy->is_queue && !skip_rule) {
# # ]
1395 : : /* Create policy rules in HW. */
1396 : 0 : ret = mlx5_flow_create_policy_rules(dev, mtr_policy);
1397 [ # # ]: 0 : if (ret)
1398 : 0 : goto policy_add_err;
1399 : : }
1400 : 0 : data.dword = policy_idx;
1401 [ # # ]: 0 : if (!priv->policy_idx_tbl) {
1402 : 0 : priv->policy_idx_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_DWORD);
1403 [ # # ]: 0 : if (!priv->policy_idx_tbl)
1404 : 0 : goto policy_add_err;
1405 : : }
1406 [ # # ]: 0 : if (mlx5_l3t_set_entry(priv->policy_idx_tbl, policy_id, &data))
1407 : 0 : goto policy_add_err;
1408 : : return 0;
1409 : 0 : policy_add_err:
1410 : : if (mtr_policy) {
1411 : 0 : ret = __mlx5_flow_meter_policy_delete(dev, policy_id,
1412 : : mtr_policy, error, false);
1413 : 0 : mlx5_free(mtr_policy);
1414 [ # # ]: 0 : if (ret)
1415 : : return ret;
1416 : : }
1417 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1418 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1419 : : NULL, "Failed to create devx policy.");
1420 : : }
1421 : :
1422 : : /**
1423 : : * Callback to delete MTR policy.
1424 : : *
1425 : : * @param[in] dev
1426 : : * Pointer to Ethernet device.
1427 : : * @param[in] policy_id
1428 : : * Meter policy id.
1429 : : * @param[out] error
1430 : : * Pointer to the error structure.
1431 : : *
1432 : : * @return
1433 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1434 : : */
1435 : : static int
1436 : 0 : mlx5_flow_meter_policy_delete(struct rte_eth_dev *dev,
1437 : : uint32_t policy_id,
1438 : : struct rte_mtr_error *error)
1439 : : {
1440 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1441 : : struct mlx5_flow_meter_policy *mtr_policy;
1442 : : uint32_t policy_idx;
1443 : : int ret;
1444 : :
1445 [ # # ]: 0 : if (policy_id == priv->sh->mtrmng->def_policy_id) {
1446 [ # # ]: 0 : if (priv->sh->mtrmng->def_policy_ref_cnt > 0)
1447 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1448 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
1449 : : "Meter policy object is being used.");
1450 : 0 : priv->sh->mtrmng->def_policy_id = MLX5_INVALID_POLICY_ID;
1451 : 0 : return 0;
1452 : : }
1453 : 0 : mtr_policy = mlx5_flow_meter_policy_find(dev, policy_id, &policy_idx);
1454 [ # # ]: 0 : if (!mtr_policy)
1455 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1456 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
1457 : : "Meter policy id is invalid. ");
1458 : 0 : ret = __mlx5_flow_meter_policy_delete(dev, policy_id, mtr_policy,
1459 : : error, true);
1460 [ # # ]: 0 : if (ret)
1461 : : return ret;
1462 : 0 : mlx5_free(mtr_policy);
1463 : 0 : return 0;
1464 : : }
1465 : :
1466 : : /**
1467 : : * Callback to get MTR policy.
1468 : : *
1469 : : * @param[in] dev
1470 : : * Pointer to Ethernet device.
1471 : : * @param[in] policy_id
1472 : : * Meter policy id.
1473 : : * @param[out] error
1474 : : * Pointer to the error structure.
1475 : : *
1476 : : * @return
1477 : : * A valid handle in case of success, NULL otherwise.
1478 : : */
1479 : : static struct rte_flow_meter_policy *
1480 : 0 : mlx5_flow_meter_policy_get(struct rte_eth_dev *dev,
1481 : : uint32_t policy_id,
1482 : : struct rte_mtr_error *error)
1483 : : {
1484 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
1485 : : uint32_t policy_idx;
1486 : :
1487 [ # # # # ]: 0 : if (mlx5_hws_active(dev) && !mlx5_hw_ctx_validate(dev, NULL)) {
1488 : : rte_mtr_error_set(error, EINVAL,
1489 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1490 : : "non-template flow engine was not configured");
1491 : 0 : return NULL;
1492 : : }
1493 [ # # ]: 0 : if (!priv->mtr_en) {
1494 : : rte_mtr_error_set(error, ENOTSUP,
1495 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1496 : : "Meter is not supported");
1497 : 0 : return NULL;
1498 : : }
1499 : 0 : return (void *)(uintptr_t)mlx5_flow_meter_policy_find(dev, policy_id,
1500 : : &policy_idx);
1501 : : }
1502 : :
1503 : : /**
1504 : : * Check meter validation.
1505 : : *
1506 : : * @param[in] priv
1507 : : * Pointer to mlx5 private data structure.
1508 : : * @param[in] meter_id
1509 : : * Meter id.
1510 : : * @param[in] params
1511 : : * Pointer to rte meter parameters.
1512 : : * @param[out] error
1513 : : * Pointer to rte meter error structure.
1514 : : *
1515 : : * @return
1516 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1517 : : */
1518 : : static int
1519 : 0 : mlx5_flow_meter_validate(struct mlx5_priv *priv, uint32_t meter_id,
1520 : : struct rte_mtr_params *params,
1521 : : struct rte_mtr_error *error)
1522 : : {
1523 : : /* Meter must use global drop action. */
1524 [ # # ]: 0 : if (!priv->sh->dr_drop_action)
1525 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1526 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS,
1527 : : NULL,
1528 : : "No drop action ready for meter.");
1529 : : /* Meter params must not be NULL. */
1530 [ # # ]: 0 : if (params == NULL)
1531 : 0 : return -rte_mtr_error_set(error, EINVAL,
1532 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS,
1533 : : NULL, "Meter object params null.");
1534 : : /* Previous meter color is not supported. */
1535 [ # # # # ]: 0 : if (params->use_prev_mtr_color && !priv->sh->meter_aso_en)
1536 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1537 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS,
1538 : : NULL,
1539 : : "Previous meter color "
1540 : : "not supported.");
1541 [ # # ]: 0 : if (params->meter_policy_id == MLX5_INVALID_POLICY_ID)
1542 : 0 : return -rte_mtr_error_set(error, ENOENT,
1543 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1544 : : NULL, "Meter policy id not valid.");
1545 : : /* Validate meter id. */
1546 [ # # ]: 0 : if (mlx5_flow_meter_find(priv, meter_id, NULL))
1547 : 0 : return -rte_mtr_error_set(error, EEXIST,
1548 : : RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
1549 : : "Meter object already exists.");
1550 : : return 0;
1551 : : }
1552 : :
1553 : : /**
1554 : : * Modify the flow meter action.
1555 : : *
1556 : : * @param[in] priv
1557 : : * Pointer to mlx5 private data structure.
1558 : : * @param[in] fm
1559 : : * Pointer to flow meter to be modified.
1560 : : * @param[in] srtcm
1561 : : * Pointer to meter srtcm description parameter.
1562 : : * @param[in] modify_bits
1563 : : * The bit in srtcm to be updated.
1564 : : * @param[in] active_state
1565 : : * The state to be updated.
1566 : : * @return
1567 : : * 0 on success, o negative value otherwise.
1568 : : */
1569 : : static int
1570 : 0 : mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
1571 : : struct mlx5_flow_meter_info *fm,
1572 : : const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
1573 : : uint64_t modify_bits, uint32_t active_state, uint32_t is_enable)
1574 : : {
1575 : : #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
1576 : 0 : struct mlx5_dev_ctx_shared *sh = priv->sh;
1577 : 0 : uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
1578 : : uint32_t *attr;
1579 : 0 : struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
1580 : : int ret;
1581 : : struct mlx5_aso_mtr *aso_mtr = NULL;
1582 : : uint32_t cbs_cir, ebs_eir, val;
1583 : :
1584 [ # # ]: 0 : if (sh->meter_aso_en) {
1585 : 0 : fm->is_enable = !!is_enable;
1586 : 0 : aso_mtr = container_of(fm, struct mlx5_aso_mtr, fm);
1587 : 0 : aso_mtr->state = ASO_METER_WAIT;
1588 : 0 : ret = mlx5_aso_meter_update_by_wqe(priv, MLX5_HW_INV_QUEUE,
1589 : : aso_mtr, &priv->mtr_bulk,
1590 : : NULL, true);
1591 [ # # ]: 0 : if (ret)
1592 : : return ret;
1593 : 0 : ret = mlx5_aso_mtr_wait(priv, aso_mtr, false);
1594 [ # # ]: 0 : if (ret)
1595 : 0 : return ret;
1596 : : } else {
1597 : : /* Fill command parameters. */
1598 : 0 : mod_attr.reg_c_index = sh->registers.aso_reg - REG_C_0;
1599 : 0 : mod_attr.flow_meter_parameter = in;
1600 : 0 : mod_attr.flow_meter_parameter_sz =
1601 : : MLX5_ST_SZ_BYTES(flow_meter_parameters);
1602 [ # # ]: 0 : if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
1603 : 0 : mod_attr.active = !!active_state;
1604 : : else
1605 : : mod_attr.active = 0;
1606 : : attr = in;
1607 [ # # ]: 0 : cbs_cir = rte_be_to_cpu_32(srtcm->cbs_cir);
1608 [ # # ]: 0 : ebs_eir = rte_be_to_cpu_32(srtcm->ebs_eir);
1609 [ # # ]: 0 : if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
1610 : 0 : val = (cbs_cir >> ASO_DSEG_CBS_EXP_OFFSET) &
1611 : : ASO_DSEG_EXP_MASK;
1612 : 0 : MLX5_SET(flow_meter_parameters, attr,
1613 : : cbs_exponent, val);
1614 : 0 : val = (cbs_cir >> ASO_DSEG_CBS_MAN_OFFSET) &
1615 : : ASO_DSEG_MAN_MASK;
1616 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, attr,
1617 : : cbs_mantissa, val);
1618 : : }
1619 [ # # ]: 0 : if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
1620 : 0 : val = (cbs_cir >> ASO_DSEG_XIR_EXP_OFFSET) &
1621 : : ASO_DSEG_EXP_MASK;
1622 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, attr,
1623 : : cir_exponent, val);
1624 : 0 : val = cbs_cir & ASO_DSEG_MAN_MASK;
1625 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, attr,
1626 : : cir_mantissa, val);
1627 : : }
1628 [ # # ]: 0 : if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
1629 : 0 : val = (ebs_eir >> ASO_DSEG_EBS_EXP_OFFSET) &
1630 : : ASO_DSEG_EXP_MASK;
1631 : 0 : MLX5_SET(flow_meter_parameters, attr,
1632 : : ebs_exponent, val);
1633 : 0 : val = (ebs_eir >> ASO_DSEG_EBS_MAN_OFFSET) &
1634 : : ASO_DSEG_MAN_MASK;
1635 [ # # ]: 0 : MLX5_SET(flow_meter_parameters, attr,
1636 : : ebs_mantissa, val);
1637 : : }
1638 : : /* Apply modifications to meter only if it was created. */
1639 [ # # ]: 0 : if (fm->meter_action_g) {
1640 : 0 : ret = mlx5_glue->dv_modify_flow_action_meter
1641 : : (fm->meter_action_g, &mod_attr,
1642 [ # # ]: 0 : rte_cpu_to_be_64(modify_bits));
1643 [ # # ]: 0 : if (ret)
1644 : : return ret;
1645 : : }
1646 : : /* Update succeeded modify meter parameters. */
1647 [ # # ]: 0 : if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
1648 : 0 : fm->active_state = !!active_state;
1649 : : }
1650 : : return 0;
1651 : : #else
1652 : : (void)priv;
1653 : : (void)fm;
1654 : : (void)srtcm;
1655 : : (void)modify_bits;
1656 : : (void)active_state;
1657 : : (void)is_enable;
1658 : : return -ENOTSUP;
1659 : : #endif
1660 : : }
1661 : :
1662 : : static int
1663 : 0 : mlx5_flow_meter_stats_enable_update(struct rte_eth_dev *dev,
1664 : : struct mlx5_flow_meter_info *fm,
1665 : : uint64_t stats_mask)
1666 : : {
1667 : 0 : fm->bytes_dropped =
1668 : 0 : (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED) ? 1 : 0;
1669 : 0 : fm->pkts_dropped = (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED) ? 1 : 0;
1670 [ # # ]: 0 : if (fm->bytes_dropped || fm->pkts_dropped) {
1671 [ # # ]: 0 : if (!fm->drop_cnt) {
1672 : : /* Alloc policer counters. */
1673 : 0 : fm->drop_cnt = mlx5_counter_alloc(dev);
1674 [ # # ]: 0 : if (!fm->drop_cnt)
1675 : 0 : return -1;
1676 : : }
1677 : : } else {
1678 [ # # ]: 0 : if (fm->drop_cnt) {
1679 : 0 : mlx5_counter_free(dev, fm->drop_cnt);
1680 : 0 : fm->drop_cnt = 0;
1681 : : }
1682 : : }
1683 : : return 0;
1684 : : }
1685 : :
1686 : : /**
1687 : : * Create meter rules.
1688 : : *
1689 : : * @param[in] dev
1690 : : * Pointer to Ethernet device.
1691 : : * @param[in] meter_id
1692 : : * Meter id.
1693 : : * @param[in] params
1694 : : * Pointer to rte meter parameters.
1695 : : * @param[in] shared
1696 : : * Meter shared with other flow or not.
1697 : : * @param[out] error
1698 : : * Pointer to rte meter error structure.
1699 : : *
1700 : : * @return
1701 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1702 : : */
1703 : : static int
1704 : 0 : mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
1705 : : struct rte_mtr_params *params, int shared,
1706 : : struct rte_mtr_error *error)
1707 : : {
1708 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1709 : : struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
1710 : : struct mlx5_flow_meter_profile *fmp;
1711 : : struct mlx5_flow_meter_info *fm;
1712 : : /* GCC fails to infer legacy_fm is set when !priv->sh->meter_aso_en. */
1713 : : struct mlx5_legacy_flow_meter *legacy_fm = NULL;
1714 : : struct mlx5_flow_meter_policy *mtr_policy = NULL;
1715 : 0 : struct mlx5_indexed_pool_config flow_ipool_cfg = {
1716 : : .size = 0,
1717 : : .trunk_size = 64,
1718 : : .need_lock = 1,
1719 : : .type = "mlx5_flow_mtr_flow_id_pool",
1720 : : };
1721 : : struct mlx5_aso_mtr *aso_mtr;
1722 : : uint32_t mtr_idx, policy_idx;
1723 : : union mlx5_l3t_data data;
1724 : : int ret;
1725 : : uint8_t domain_bitmap;
1726 : : uint8_t mtr_id_bits;
1727 [ # # ]: 0 : uint8_t mtr_reg_bits = priv->mtr_reg_share ?
1728 : : MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
1729 : :
1730 [ # # ]: 0 : if (!priv->mtr_en)
1731 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1732 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1733 : : "Meter is not supported");
1734 : : /* Validate the parameters. */
1735 : 0 : ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
1736 [ # # ]: 0 : if (ret)
1737 : : return ret;
1738 : : /* Meter profile must exist. */
1739 : 0 : fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
1740 [ # # ]: 0 : if (fmp == NULL)
1741 : 0 : return -rte_mtr_error_set(error, ENOENT,
1742 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1743 : : NULL, "Meter profile id not valid.");
1744 : : /* Meter policy must exist. */
1745 [ # # ]: 0 : if (params->meter_policy_id == priv->sh->mtrmng->def_policy_id) {
1746 : 0 : rte_atomic_fetch_add_explicit
1747 : : (&priv->sh->mtrmng->def_policy_ref_cnt,
1748 : : 1, rte_memory_order_relaxed);
1749 : : domain_bitmap = MLX5_MTR_ALL_DOMAIN_BIT;
1750 [ # # ]: 0 : if (!priv->sh->config.dv_esw_en)
1751 : : domain_bitmap &= ~MLX5_MTR_DOMAIN_TRANSFER_BIT;
1752 : : } else {
1753 [ # # ]: 0 : if (!priv->sh->meter_aso_en)
1754 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1755 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1756 : : "Part of the policies cannot be "
1757 : : "supported without ASO ");
1758 : 0 : mtr_policy = mlx5_flow_meter_policy_find(dev,
1759 : : params->meter_policy_id, &policy_idx);
1760 [ # # ]: 0 : if (!mtr_policy)
1761 : 0 : return -rte_mtr_error_set(error, ENOENT,
1762 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1763 : : NULL, "Meter policy id not valid.");
1764 : 0 : domain_bitmap = (mtr_policy->ingress ?
1765 : 0 : MLX5_MTR_DOMAIN_INGRESS_BIT : 0) |
1766 : : (mtr_policy->egress ?
1767 : : MLX5_MTR_DOMAIN_EGRESS_BIT : 0) |
1768 : : (mtr_policy->transfer ?
1769 : : MLX5_MTR_DOMAIN_TRANSFER_BIT : 0);
1770 [ # # # # ]: 0 : if (fmp->g_support && mtr_policy->skip_g)
1771 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1772 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1773 : : NULL, "Meter green policy is empty.");
1774 [ # # # # ]: 0 : if (fmp->y_support && mtr_policy->skip_y)
1775 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1776 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1777 : : NULL, "Meter yellow policy is empty.");
1778 : : }
1779 : : /* Allocate the flow meter memory. */
1780 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
1781 : 0 : mtr_idx = mlx5_flow_mtr_alloc(dev);
1782 [ # # ]: 0 : if (!mtr_idx)
1783 : 0 : return -rte_mtr_error_set(error, ENOMEM,
1784 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1785 : : "Memory alloc failed for meter.");
1786 : 0 : aso_mtr = mlx5_aso_meter_by_idx(priv, mtr_idx);
1787 : 0 : fm = &aso_mtr->fm;
1788 : : } else {
1789 [ # # ]: 0 : if (fmp->y_support)
1790 : 0 : return -rte_mtr_error_set(error, ENOMEM,
1791 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1792 : : "Unsupported profile with yellow.");
1793 : 0 : legacy_fm = mlx5_ipool_zmalloc
1794 : : (priv->sh->ipool[MLX5_IPOOL_MTR], &mtr_idx);
1795 [ # # ]: 0 : if (legacy_fm == NULL)
1796 : 0 : return -rte_mtr_error_set(error, ENOMEM,
1797 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1798 : : "Memory alloc failed for meter.");
1799 : 0 : legacy_fm->idx = mtr_idx;
1800 : 0 : fm = &legacy_fm->fm;
1801 : : }
1802 [ # # ]: 0 : mtr_id_bits = MLX5_REG_BITS - rte_clz32(mtr_idx);
1803 [ # # ]: 0 : if ((mtr_id_bits + priv->sh->mtrmng->max_mtr_flow_bits) >
1804 : : mtr_reg_bits) {
1805 : 0 : DRV_LOG(ERR, "Meter number exceeds max limit.");
1806 : 0 : goto error;
1807 : : }
1808 [ # # ]: 0 : if (mtr_id_bits > priv->sh->mtrmng->max_mtr_bits)
1809 : 0 : priv->sh->mtrmng->max_mtr_bits = mtr_id_bits;
1810 : : /* Fill the flow meter parameters. */
1811 : 0 : fm->meter_id = meter_id;
1812 : 0 : fm->policy_id = params->meter_policy_id;
1813 : 0 : fm->profile = fmp;
1814 [ # # ]: 0 : if (mlx5_flow_meter_stats_enable_update(dev, fm, params->stats_mask))
1815 : 0 : goto error;
1816 [ # # ]: 0 : if (mlx5_flow_create_mtr_tbls(dev, fm, mtr_idx, domain_bitmap))
1817 : 0 : goto error;
1818 : : /* Add to the flow meter list. */
1819 [ # # ]: 0 : if (!priv->sh->meter_aso_en) {
1820 : : MLX5_ASSERT(legacy_fm != NULL);
1821 : 0 : TAILQ_INSERT_TAIL(fms, legacy_fm, next);
1822 : : }
1823 : : /* Add to the flow meter list. */
1824 : 0 : fm->active_state = 1; /* Config meter starts as active. */
1825 : 0 : fm->is_enable = params->meter_enable;
1826 : 0 : fm->shared = !!shared;
1827 : 0 : fm->color_aware = !!params->use_prev_mtr_color;
1828 : 0 : rte_atomic_fetch_add_explicit(&fm->profile->ref_cnt, 1, rte_memory_order_relaxed);
1829 [ # # ]: 0 : if (params->meter_policy_id == priv->sh->mtrmng->def_policy_id) {
1830 : 0 : fm->def_policy = 1;
1831 : 0 : fm->flow_ipool = mlx5_ipool_create(&flow_ipool_cfg);
1832 [ # # ]: 0 : if (!fm->flow_ipool)
1833 : 0 : goto error;
1834 : : }
1835 : : rte_spinlock_init(&fm->sl);
1836 : : /* If ASO meter supported, update ASO flow meter by wqe. */
1837 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
1838 : 0 : aso_mtr = container_of(fm, struct mlx5_aso_mtr, fm);
1839 : 0 : aso_mtr->state = ASO_METER_WAIT;
1840 : 0 : ret = mlx5_aso_meter_update_by_wqe(priv, MLX5_HW_INV_QUEUE,
1841 : : aso_mtr, &priv->mtr_bulk, NULL, true);
1842 [ # # ]: 0 : if (ret)
1843 : 0 : goto error;
1844 [ # # ]: 0 : if (!priv->mtr_idx_tbl) {
1845 : 0 : priv->mtr_idx_tbl =
1846 : 0 : mlx5_l3t_create(MLX5_L3T_TYPE_DWORD);
1847 [ # # ]: 0 : if (!priv->mtr_idx_tbl)
1848 : 0 : goto error;
1849 : : }
1850 : 0 : data.dword = mtr_idx;
1851 [ # # ]: 0 : if (mlx5_l3t_set_entry(priv->mtr_idx_tbl, meter_id, &data))
1852 : 0 : goto error;
1853 [ # # # # ]: 0 : } else if (!params->meter_enable && mlx5_flow_meter_disable(dev, meter_id, error)) {
1854 : 0 : goto error;
1855 : : }
1856 : 0 : fm->active_state = params->meter_enable;
1857 [ # # ]: 0 : if (mtr_policy)
1858 : 0 : rte_atomic_fetch_add_explicit(&mtr_policy->ref_cnt, 1, rte_memory_order_relaxed);
1859 : : return 0;
1860 : 0 : error:
1861 : 0 : mlx5_flow_destroy_mtr_tbls(dev, fm);
1862 : : /* Free policer counters. */
1863 [ # # ]: 0 : if (fm->drop_cnt)
1864 : 0 : mlx5_counter_free(dev, fm->drop_cnt);
1865 [ # # ]: 0 : if (priv->sh->meter_aso_en)
1866 : 0 : mlx5_flow_mtr_free(dev, mtr_idx);
1867 : : else
1868 : 0 : mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], mtr_idx);
1869 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1870 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1871 : : NULL, "Failed to create devx meter.");
1872 : : }
1873 : :
1874 : : static int
1875 : 0 : mlx5_flow_meter_params_flush(struct rte_eth_dev *dev,
1876 : : struct mlx5_flow_meter_info *fm,
1877 : : uint32_t mtr_idx)
1878 : : {
1879 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1880 : : struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
1881 : : struct mlx5_flow_meter_profile *fmp;
1882 : : struct mlx5_legacy_flow_meter *legacy_fm = NULL;
1883 : : struct mlx5_flow_meter_policy *mtr_policy;
1884 : :
1885 : : /* Meter object must not have any owner. */
1886 : : MLX5_ASSERT(!fm->ref_cnt);
1887 : : /* Get meter profile. */
1888 : 0 : fmp = fm->profile;
1889 [ # # ]: 0 : if (fmp == NULL)
1890 : : return -1;
1891 : : /* Update dependencies. */
1892 : 0 : rte_atomic_fetch_sub_explicit(&fmp->ref_cnt, 1, rte_memory_order_relaxed);
1893 : 0 : fm->profile = NULL;
1894 : : /* Remove from list. */
1895 [ # # ]: 0 : if (!priv->sh->meter_aso_en) {
1896 : : legacy_fm = container_of(fm,
1897 : : struct mlx5_legacy_flow_meter, fm);
1898 [ # # ]: 0 : TAILQ_REMOVE(fms, legacy_fm, next);
1899 : : }
1900 : : /* Free drop counters. */
1901 [ # # ]: 0 : if (fm->drop_cnt)
1902 : 0 : mlx5_counter_free(dev, fm->drop_cnt);
1903 : : /* Free meter flow table. */
1904 [ # # ]: 0 : if (fm->flow_ipool) {
1905 : 0 : mlx5_ipool_destroy(fm->flow_ipool);
1906 : 0 : fm->flow_ipool = 0;
1907 : : }
1908 : 0 : mlx5_flow_destroy_mtr_tbls(dev, fm);
1909 [ # # ]: 0 : if (fm->def_policy)
1910 : 0 : rte_atomic_fetch_sub_explicit(&priv->sh->mtrmng->def_policy_ref_cnt,
1911 : : 1, rte_memory_order_relaxed);
1912 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
1913 [ # # ]: 0 : if (!fm->def_policy) {
1914 : 0 : mtr_policy = mlx5_flow_meter_policy_find(dev,
1915 : : fm->policy_id, NULL);
1916 [ # # ]: 0 : if (mtr_policy)
1917 : 0 : rte_atomic_fetch_sub_explicit(&mtr_policy->ref_cnt,
1918 : : 1, rte_memory_order_relaxed);
1919 : 0 : fm->policy_id = 0;
1920 : : }
1921 : 0 : fm->def_policy = 0;
1922 [ # # ]: 0 : if (mlx5_l3t_clear_entry(priv->mtr_idx_tbl, fm->meter_id))
1923 : : return -1;
1924 : 0 : mlx5_flow_mtr_free(dev, mtr_idx);
1925 : : } else {
1926 : 0 : mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR],
1927 : : legacy_fm->idx);
1928 : : }
1929 : : return 0;
1930 : : }
1931 : :
1932 : : /**
1933 : : * Destroy meter rules.
1934 : : *
1935 : : * @param[in] dev
1936 : : * Pointer to Ethernet device.
1937 : : * @param[in] meter_id
1938 : : * Meter id.
1939 : : * @param[out] error
1940 : : * Pointer to rte meter error structure.
1941 : : *
1942 : : * @return
1943 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1944 : : */
1945 : : static int
1946 : 0 : mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
1947 : : struct rte_mtr_error *error)
1948 : : {
1949 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1950 : : struct mlx5_flow_meter_info *fm;
1951 : 0 : uint32_t mtr_idx = 0;
1952 : :
1953 [ # # ]: 0 : if (!priv->mtr_en)
1954 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
1955 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1956 : : NULL,
1957 : : "Meter is not supported");
1958 : : /* Meter object must exist. */
1959 : 0 : fm = mlx5_flow_meter_find(priv, meter_id, &mtr_idx);
1960 [ # # ]: 0 : if (fm == NULL)
1961 : 0 : return -rte_mtr_error_set(error, ENOENT,
1962 : : RTE_MTR_ERROR_TYPE_MTR_ID,
1963 : : NULL,
1964 : : "Meter object id not valid.");
1965 : : /* Meter object must not have any owner. */
1966 [ # # ]: 0 : if (fm->ref_cnt > 0)
1967 : 0 : return -rte_mtr_error_set(error, EBUSY,
1968 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1969 : : NULL,
1970 : : "Meter object is being used.");
1971 : : /* Destroy the meter profile. */
1972 [ # # ]: 0 : if (mlx5_flow_meter_params_flush(dev, fm, mtr_idx))
1973 : 0 : return -rte_mtr_error_set(error, EINVAL,
1974 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1975 : : NULL,
1976 : : "MTR object meter profile invalid.");
1977 : : return 0;
1978 : : }
1979 : :
1980 : : /**
1981 : : * Modify meter state.
1982 : : *
1983 : : * @param[in] priv
1984 : : * Pointer to mlx5 private data structure.
1985 : : * @param[in] fm
1986 : : * Pointer to flow meter.
1987 : : * @param[in] new_state
1988 : : * New state to update.
1989 : : * @param[out] error
1990 : : * Pointer to rte meter error structure.
1991 : : *
1992 : : * @return
1993 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
1994 : : */
1995 : : static int
1996 : 0 : mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
1997 : : struct mlx5_flow_meter_info *fm,
1998 : : uint32_t new_state,
1999 : : struct rte_mtr_error *error)
2000 : : {
2001 : : static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
2002 : : .cbs_cir = RTE_BE32(MLX5_IFC_FLOW_METER_DISABLE_CBS_CIR_VAL),
2003 : : .ebs_eir = 0,
2004 : : };
2005 : : uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
2006 : : MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
2007 : : int ret;
2008 : :
2009 [ # # ]: 0 : if (new_state == MLX5_FLOW_METER_DISABLE)
2010 : 0 : ret = mlx5_flow_meter_action_modify(priv, fm,
2011 : : &srtcm, modify_bits, 0, 0);
2012 : : else
2013 : 0 : ret = mlx5_flow_meter_action_modify(priv, fm,
2014 : 0 : &fm->profile->srtcm_prm,
2015 : : modify_bits, 0, 1);
2016 [ # # ]: 0 : if (ret)
2017 [ # # # # ]: 0 : return -rte_mtr_error_set(error, -ret,
2018 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS,
2019 : : NULL,
2020 : : new_state ?
2021 : : "Failed to enable meter." :
2022 : : "Failed to disable meter.");
2023 : : return 0;
2024 : : }
2025 : :
2026 : : /**
2027 : : * Callback to enable flow meter.
2028 : : *
2029 : : * @param[in] dev
2030 : : * Pointer to Ethernet device.
2031 : : * @param[in] meter_id
2032 : : * Meter id.
2033 : : * @param[out] error
2034 : : * Pointer to rte meter error structure.
2035 : : *
2036 : : * @return
2037 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2038 : : */
2039 : : static int
2040 : 0 : mlx5_flow_meter_enable(struct rte_eth_dev *dev,
2041 : : uint32_t meter_id,
2042 : : struct rte_mtr_error *error)
2043 : : {
2044 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
2045 : : struct mlx5_flow_meter_info *fm;
2046 : : int ret;
2047 : :
2048 [ # # # # ]: 0 : if (mlx5_hws_active(dev) && !mlx5_hw_ctx_validate(dev, NULL))
2049 : 0 : return -rte_mtr_error_set(error, EINVAL,
2050 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2051 : : "non-template flow engine was not configured");
2052 [ # # ]: 0 : if (!priv->mtr_en)
2053 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
2054 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2055 : : "Meter is not supported");
2056 : : /* Meter object must exist. */
2057 : 0 : fm = mlx5_flow_meter_find(priv, meter_id, NULL);
2058 [ # # ]: 0 : if (fm == NULL)
2059 : 0 : return -rte_mtr_error_set(error, ENOENT,
2060 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2061 : : NULL, "Meter not found.");
2062 [ # # ]: 0 : if (fm->active_state == MLX5_FLOW_METER_ENABLE)
2063 : : return 0;
2064 : 0 : ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
2065 : : error);
2066 [ # # ]: 0 : if (!ret)
2067 : 0 : fm->active_state = MLX5_FLOW_METER_ENABLE;
2068 : : return ret;
2069 : : }
2070 : :
2071 : : /**
2072 : : * Callback to disable flow meter.
2073 : : *
2074 : : * @param[in] dev
2075 : : * Pointer to Ethernet device.
2076 : : * @param[in] meter_id
2077 : : * Meter id.
2078 : : * @param[out] error
2079 : : * Pointer to rte meter error structure.
2080 : : *
2081 : : * @return
2082 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2083 : : */
2084 : : static int
2085 : 0 : mlx5_flow_meter_disable(struct rte_eth_dev *dev,
2086 : : uint32_t meter_id,
2087 : : struct rte_mtr_error *error)
2088 : : {
2089 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2090 : : struct mlx5_flow_meter_info *fm;
2091 : : int ret;
2092 : :
2093 [ # # ]: 0 : if (!priv->mtr_en)
2094 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
2095 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2096 : : "Meter is not supported");
2097 : : /* Meter object must exist. */
2098 : 0 : fm = mlx5_flow_meter_find(priv, meter_id, NULL);
2099 [ # # ]: 0 : if (fm == NULL)
2100 : 0 : return -rte_mtr_error_set(error, ENOENT,
2101 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2102 : : NULL, "Meter not found.");
2103 [ # # ]: 0 : if (fm->active_state == MLX5_FLOW_METER_DISABLE)
2104 : : return 0;
2105 : 0 : ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
2106 : : error);
2107 [ # # ]: 0 : if (!ret)
2108 : 0 : fm->active_state = MLX5_FLOW_METER_DISABLE;
2109 : : return ret;
2110 : : }
2111 : :
2112 : : /**
2113 : : * Callback to update meter profile.
2114 : : *
2115 : : * @param[in] dev
2116 : : * Pointer to Ethernet device.
2117 : : * @param[in] meter_id
2118 : : * Meter id.
2119 : : * @param[in] meter_profile_id
2120 : : * To be updated meter profile id.
2121 : : * @param[out] error
2122 : : * Pointer to rte meter error structure.
2123 : : *
2124 : : * @return
2125 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2126 : : */
2127 : : static int
2128 : 0 : mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
2129 : : uint32_t meter_id,
2130 : : uint32_t meter_profile_id,
2131 : : struct rte_mtr_error *error)
2132 : : {
2133 [ # # ]: 0 : struct mlx5_priv *priv = dev->data->dev_private;
2134 : : struct mlx5_flow_meter_profile *fmp;
2135 : : struct mlx5_flow_meter_profile *old_fmp;
2136 : : struct mlx5_flow_meter_info *fm;
2137 : : uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
2138 : : MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
2139 : : int ret;
2140 : :
2141 [ # # # # ]: 0 : if (mlx5_hws_active(dev) && !mlx5_hw_ctx_validate(dev, NULL))
2142 : 0 : return -rte_mtr_error_set(error, EINVAL,
2143 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2144 : : "non-template flow engine was not configured");
2145 [ # # ]: 0 : if (!priv->mtr_en)
2146 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
2147 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2148 : : "Meter is not supported");
2149 : : /* Meter profile must exist. */
2150 : 0 : fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
2151 [ # # ]: 0 : if (fmp == NULL)
2152 : 0 : return -rte_mtr_error_set(error, ENOENT,
2153 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
2154 : : NULL, "Meter profile not found.");
2155 : : /* Meter object must exist. */
2156 : 0 : fm = mlx5_flow_meter_find(priv, meter_id, NULL);
2157 [ # # ]: 0 : if (fm == NULL)
2158 : 0 : return -rte_mtr_error_set(error, ENOENT,
2159 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2160 : : NULL, "Meter not found.");
2161 : : /* MTR object already set to meter profile id. */
2162 : 0 : old_fmp = fm->profile;
2163 [ # # ]: 0 : if (fmp == old_fmp)
2164 : : return 0;
2165 : : /* Update the profile. */
2166 : 0 : fm->profile = fmp;
2167 : : /* Update meter params in HW (if not disabled). */
2168 [ # # ]: 0 : if (fm->active_state == MLX5_FLOW_METER_DISABLE)
2169 : 0 : goto dec_ref_cnt;
2170 : 0 : ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
2171 : 0 : modify_bits, fm->active_state, 1);
2172 [ # # ]: 0 : if (ret) {
2173 : 0 : fm->profile = old_fmp;
2174 [ # # ]: 0 : return -rte_mtr_error_set(error, -ret,
2175 : : RTE_MTR_ERROR_TYPE_MTR_PARAMS,
2176 : : NULL, "Failed to update meter"
2177 : : " parameters in hardware.");
2178 : : }
2179 : 0 : dec_ref_cnt:
2180 : 0 : old_fmp->ref_cnt--;
2181 : 0 : fmp->ref_cnt++;
2182 : 0 : return 0;
2183 : : }
2184 : :
2185 : : /**
2186 : : * Callback to update meter stats mask.
2187 : : *
2188 : : * @param[in] dev
2189 : : * Pointer to Ethernet device.
2190 : : * @param[in] meter_id
2191 : : * Meter id.
2192 : : * @param[in] stats_mask
2193 : : * To be updated stats_mask.
2194 : : * @param[out] error
2195 : : * Pointer to rte meter error structure.
2196 : : *
2197 : : * @return
2198 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2199 : : */
2200 : : static int
2201 : 0 : mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
2202 : : uint32_t meter_id,
2203 : : uint64_t stats_mask,
2204 : : struct rte_mtr_error *error)
2205 : : {
2206 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2207 : : struct mlx5_flow_meter_info *fm;
2208 : :
2209 [ # # ]: 0 : if (!priv->mtr_en)
2210 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
2211 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2212 : : "Meter is not supported");
2213 : : /* Meter object must exist. */
2214 : 0 : fm = mlx5_flow_meter_find(priv, meter_id, NULL);
2215 [ # # ]: 0 : if (fm == NULL)
2216 : 0 : return -rte_mtr_error_set(error, ENOENT,
2217 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2218 : : NULL, "Meter object id not valid.");
2219 [ # # ]: 0 : if (mlx5_flow_meter_stats_enable_update(dev, fm, stats_mask))
2220 : 0 : return -rte_mtr_error_set(error, ENOENT,
2221 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2222 : : NULL, "Fail to allocate "
2223 : : "counter for meter.");
2224 : : return 0;
2225 : : }
2226 : :
2227 : : /**
2228 : : * Callback to read meter statistics.
2229 : : *
2230 : : * @param[in] dev
2231 : : * Pointer to Ethernet device.
2232 : : * @param[in] meter_id
2233 : : * Meter id.
2234 : : * @param[out] stats
2235 : : * Pointer to store the statistics.
2236 : : * @param[out] stats_mask
2237 : : * Pointer to store the stats_mask.
2238 : : * @param[in] clear
2239 : : * Statistic to be cleared after read or not.
2240 : : * @param[out] error
2241 : : * Pointer to rte meter error structure.
2242 : : *
2243 : : * @return
2244 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2245 : : */
2246 : : static int
2247 : 0 : mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
2248 : : uint32_t meter_id,
2249 : : struct rte_mtr_stats *stats,
2250 : : uint64_t *stats_mask,
2251 : : int clear,
2252 : : struct rte_mtr_error *error)
2253 : : {
2254 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2255 : : struct mlx5_flow_meter_info *fm;
2256 : : uint64_t pkts;
2257 : : uint64_t bytes;
2258 : : int ret = 0;
2259 : :
2260 [ # # ]: 0 : if (!priv->mtr_en)
2261 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
2262 : : RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
2263 : : "Meter is not supported");
2264 : : /* Meter object must exist. */
2265 : 0 : fm = mlx5_flow_meter_find(priv, meter_id, NULL);
2266 [ # # ]: 0 : if (fm == NULL)
2267 : 0 : return -rte_mtr_error_set(error, ENOENT,
2268 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2269 : : NULL, "Meter object id not valid.");
2270 : 0 : *stats_mask = 0;
2271 [ # # ]: 0 : if (fm->bytes_dropped)
2272 : 0 : *stats_mask |= RTE_MTR_STATS_N_BYTES_DROPPED;
2273 [ # # ]: 0 : if (fm->pkts_dropped)
2274 : 0 : *stats_mask |= RTE_MTR_STATS_N_PKTS_DROPPED;
2275 : : memset(stats, 0, sizeof(*stats));
2276 [ # # ]: 0 : if (fm->drop_cnt) {
2277 : 0 : ret = mlx5_counter_query(dev, fm->drop_cnt, clear, &pkts,
2278 : : &bytes, NULL);
2279 [ # # ]: 0 : if (ret)
2280 [ # # ]: 0 : goto error;
2281 : : /* If need to read the packets, set it. */
2282 [ # # ]: 0 : if (fm->pkts_dropped)
2283 : 0 : stats->n_pkts_dropped = pkts;
2284 : : /* If need to read the bytes, set it. */
2285 [ # # ]: 0 : if (fm->bytes_dropped)
2286 : 0 : stats->n_bytes_dropped = bytes;
2287 : : }
2288 : : return 0;
2289 : : error:
2290 : 0 : return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
2291 : : "Failed to read meter drop counters.");
2292 : : }
2293 : :
2294 : : static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
2295 : : .capabilities_get = mlx5_flow_mtr_cap_get,
2296 : : .meter_profile_add = mlx5_flow_meter_profile_add,
2297 : : .meter_profile_delete = mlx5_flow_meter_profile_delete,
2298 : : .meter_profile_get = mlx5_flow_meter_profile_get,
2299 : : .meter_policy_validate = mlx5_flow_meter_policy_validate,
2300 : : .meter_policy_add = mlx5_flow_meter_policy_add,
2301 : : .meter_policy_delete = mlx5_flow_meter_policy_delete,
2302 : : .meter_policy_get = mlx5_flow_meter_policy_get,
2303 : : .create = mlx5_flow_meter_create,
2304 : : .destroy = mlx5_flow_meter_destroy,
2305 : : .meter_enable = mlx5_flow_meter_enable,
2306 : : .meter_disable = mlx5_flow_meter_disable,
2307 : : .meter_profile_update = mlx5_flow_meter_profile_update,
2308 : : .meter_dscp_table_update = NULL,
2309 : : .stats_update = mlx5_flow_meter_stats_update,
2310 : : .stats_read = mlx5_flow_meter_stats_read,
2311 : : };
2312 : :
2313 : : #if defined(HAVE_MLX5_HWS_SUPPORT)
2314 : : static const struct rte_mtr_ops mlx5_flow_mtr_hws_ops = {
2315 : : .capabilities_get = mlx5_flow_mtr_cap_get,
2316 : : .meter_profile_add = mlx5_flow_meter_profile_hws_add,
2317 : : .meter_profile_delete = mlx5_flow_meter_profile_hws_delete,
2318 : : .meter_profile_get = mlx5_flow_meter_profile_get,
2319 : : .meter_profile_update = mlx5_flow_meter_profile_update,
2320 : : .meter_dscp_table_update = NULL,
2321 : : .stats_update = NULL,
2322 : : .stats_read = NULL,
2323 : : };
2324 : : #endif
2325 : :
2326 : : /**
2327 : : * Get meter operations.
2328 : : *
2329 : : * @param dev
2330 : : * Pointer to Ethernet device structure.
2331 : : * @param arg
2332 : : * Pointer to set the mtr operations.
2333 : : *
2334 : : * @return
2335 : : * Always 0.
2336 : : */
2337 : : int
2338 : 0 : mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
2339 : : {
2340 : : #if defined(HAVE_MLX5_HWS_SUPPORT)
2341 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2342 : :
2343 [ # # ]: 0 : if (priv->sh->config.dv_flow_en == 2)
2344 : 0 : *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_hws_ops;
2345 : : else
2346 : 0 : *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
2347 : : #else
2348 : : *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
2349 : : #endif
2350 : 0 : return 0;
2351 : : }
2352 : :
2353 : : /**
2354 : : * Find meter by id.
2355 : : *
2356 : : * @param priv
2357 : : * Pointer to mlx5_priv.
2358 : : * @param meter_id
2359 : : * Meter id.
2360 : : * @param mtr_idx
2361 : : * Pointer to Meter index.
2362 : : *
2363 : : * @return
2364 : : * Pointer to the meter info found on success, NULL otherwise.
2365 : : */
2366 : : struct mlx5_flow_meter_info *
2367 : 0 : mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id,
2368 : : uint32_t *mtr_idx)
2369 : : {
2370 : : struct mlx5_legacy_flow_meter *legacy_fm;
2371 : : struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
2372 : : struct mlx5_aso_mtr *aso_mtr;
2373 : : struct mlx5_aso_mtr_pools_mng *pools_mng =
2374 : 0 : &priv->sh->mtrmng->pools_mng;
2375 : : union mlx5_l3t_data data;
2376 : : uint16_t n_valid;
2377 : :
2378 [ # # ]: 0 : if (priv->mtr_bulk.aso) {
2379 [ # # ]: 0 : if (mtr_idx)
2380 : 0 : *mtr_idx = meter_id;
2381 : 0 : aso_mtr = priv->mtr_bulk.aso + meter_id;
2382 : 0 : return &aso_mtr->fm;
2383 : : }
2384 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
2385 : 0 : rte_rwlock_read_lock(&pools_mng->resize_mtrwl);
2386 [ # # ]: 0 : n_valid = pools_mng->n_valid;
2387 : : rte_rwlock_read_unlock(&pools_mng->resize_mtrwl);
2388 [ # # # # : 0 : if (!n_valid || !priv->mtr_idx_tbl ||
# # ]
2389 : 0 : (mlx5_l3t_get_entry(priv->mtr_idx_tbl, meter_id, &data) ||
2390 [ # # ]: 0 : !data.dword))
2391 : 0 : return NULL;
2392 [ # # ]: 0 : if (mtr_idx)
2393 : 0 : *mtr_idx = data.dword;
2394 : 0 : aso_mtr = mlx5_aso_meter_by_idx(priv, data.dword);
2395 : : /* Remove reference taken by the mlx5_l3t_get_entry. */
2396 : 0 : mlx5_l3t_clear_entry(priv->mtr_idx_tbl, meter_id);
2397 [ # # # # ]: 0 : if (!aso_mtr || aso_mtr->state == ASO_METER_FREE)
2398 : : return NULL;
2399 : 0 : return &aso_mtr->fm;
2400 : : }
2401 [ # # ]: 0 : TAILQ_FOREACH(legacy_fm, fms, next)
2402 [ # # ]: 0 : if (meter_id == legacy_fm->fm.meter_id) {
2403 [ # # ]: 0 : if (mtr_idx)
2404 : 0 : *mtr_idx = legacy_fm->idx;
2405 : 0 : return &legacy_fm->fm;
2406 : : }
2407 : : return NULL;
2408 : : }
2409 : :
2410 : : /**
2411 : : * Find meter by index.
2412 : : *
2413 : : * @param priv
2414 : : * Pointer to mlx5_priv.
2415 : : * @param idx
2416 : : * Meter index.
2417 : : *
2418 : : * @return
2419 : : * Pointer to the meter info found on success, NULL otherwise.
2420 : : */
2421 : : struct mlx5_flow_meter_info *
2422 : 0 : mlx5_flow_dv_meter_find_by_idx(struct mlx5_priv *priv, uint32_t idx)
2423 : : {
2424 : : struct mlx5_aso_mtr *aso_mtr;
2425 : :
2426 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
2427 : 0 : aso_mtr = mlx5_aso_meter_by_idx(priv, idx);
2428 [ # # ]: 0 : if (!aso_mtr)
2429 : : return NULL;
2430 : 0 : return &aso_mtr->fm;
2431 : : } else {
2432 : 0 : return mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
2433 : : }
2434 : : }
2435 : :
2436 : : /**
2437 : : * Attach meter to flow.
2438 : : * Unidirectional Meter creation can only be done
2439 : : * when flow direction is known, i.e. when calling meter_attach.
2440 : : *
2441 : : * @param [in] priv
2442 : : * Pointer to mlx5 private data.
2443 : : * @param[in] fm
2444 : : * Pointer to flow meter.
2445 : : * @param [in] attr
2446 : : * Pointer to flow attributes.
2447 : : * @param [out] error
2448 : : * Pointer to error structure.
2449 : : *
2450 : : * @return
2451 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2452 : : */
2453 : : int
2454 : 0 : mlx5_flow_meter_attach(struct mlx5_priv *priv,
2455 : : struct mlx5_flow_meter_info *fm,
2456 : : const struct rte_flow_attr *attr,
2457 : : struct rte_flow_error *error)
2458 : : {
2459 : : int ret = 0;
2460 : :
2461 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
2462 : : struct mlx5_aso_mtr *aso_mtr;
2463 : :
2464 : 0 : aso_mtr = container_of(fm, struct mlx5_aso_mtr, fm);
2465 [ # # ]: 0 : if (mlx5_aso_mtr_wait(priv, aso_mtr, false)) {
2466 : 0 : return rte_flow_error_set(error, ENOENT,
2467 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
2468 : : NULL,
2469 : : "Timeout in meter configuration");
2470 : : }
2471 : 0 : rte_spinlock_lock(&fm->sl);
2472 [ # # # # ]: 0 : if (fm->shared || !fm->ref_cnt) {
2473 : 0 : fm->ref_cnt++;
2474 : : } else {
2475 : 0 : rte_flow_error_set(error, EINVAL,
2476 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2477 : : "Meter cannot be shared");
2478 : : ret = -1;
2479 : : }
2480 : : rte_spinlock_unlock(&fm->sl);
2481 : : } else {
2482 : 0 : rte_spinlock_lock(&fm->sl);
2483 [ # # ]: 0 : if (fm->meter_action_g) {
2484 [ # # ]: 0 : if (fm->shared &&
2485 [ # # ]: 0 : attr->transfer == fm->transfer &&
2486 [ # # ]: 0 : attr->ingress == fm->ingress &&
2487 : : attr->egress == fm->egress) {
2488 : 0 : fm->ref_cnt++;
2489 : : } else {
2490 [ # # ]: 0 : rte_flow_error_set(error, EINVAL,
2491 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2492 : : fm->shared ?
2493 : : "Meter attr not match." :
2494 : : "Meter cannot be shared.");
2495 : : ret = -1;
2496 : : }
2497 : : } else {
2498 : 0 : fm->ingress = attr->ingress;
2499 : 0 : fm->egress = attr->egress;
2500 : 0 : fm->transfer = attr->transfer;
2501 : 0 : fm->ref_cnt = 1;
2502 : : /* This also creates the meter object. */
2503 : 0 : fm->meter_action_g = mlx5_flow_meter_action_create(priv,
2504 : : fm);
2505 [ # # ]: 0 : if (!fm->meter_action_g) {
2506 : 0 : fm->ref_cnt = 0;
2507 : 0 : fm->ingress = 0;
2508 : 0 : fm->egress = 0;
2509 : 0 : fm->transfer = 0;
2510 : 0 : rte_flow_error_set(error, EINVAL,
2511 : : RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
2512 : : "Meter action create failed.");
2513 : : ret = -1;
2514 : : }
2515 : : }
2516 : : rte_spinlock_unlock(&fm->sl);
2517 : : }
2518 [ # # ]: 0 : return ret ? -rte_errno : 0;
2519 : : }
2520 : :
2521 : : /**
2522 : : * Detach meter from flow.
2523 : : *
2524 : : * @param [in] priv
2525 : : * Pointer to mlx5 private data.
2526 : : * @param [in] fm
2527 : : * Pointer to flow meter.
2528 : : */
2529 : : void
2530 : 0 : mlx5_flow_meter_detach(struct mlx5_priv *priv,
2531 : : struct mlx5_flow_meter_info *fm)
2532 : : {
2533 : : #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
2534 : 0 : rte_spinlock_lock(&fm->sl);
2535 : : MLX5_ASSERT(fm->ref_cnt);
2536 [ # # # # ]: 0 : if (--fm->ref_cnt == 0 && !priv->sh->meter_aso_en) {
2537 : 0 : mlx5_glue->destroy_flow_action(fm->meter_action_g);
2538 : 0 : fm->meter_action_g = NULL;
2539 : 0 : fm->ingress = 0;
2540 : 0 : fm->egress = 0;
2541 : 0 : fm->transfer = 0;
2542 : : }
2543 : : rte_spinlock_unlock(&fm->sl);
2544 : : #else
2545 : : (void)priv;
2546 : : (void)fm;
2547 : : #endif
2548 : 0 : }
2549 : :
2550 : : /**
2551 : : * Flush meter with Rx queue configuration.
2552 : : *
2553 : : * @param[in] dev
2554 : : * Pointer to Ethernet device.
2555 : : */
2556 : : void
2557 : 0 : mlx5_flow_meter_rxq_flush(struct rte_eth_dev *dev)
2558 : : {
2559 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2560 : : struct mlx5_flow_meter_sub_policy *sub_policy;
2561 : : struct mlx5_flow_meter_policy *mtr_policy;
2562 : : void *entry;
2563 : : uint32_t i, policy_idx;
2564 : :
2565 [ # # ]: 0 : if (!priv->mtr_en)
2566 : 0 : return;
2567 [ # # ]: 0 : if (priv->policy_idx_tbl) {
2568 [ # # ]: 0 : MLX5_L3T_FOREACH(priv->policy_idx_tbl, i, entry) {
2569 : 0 : policy_idx = *(uint32_t *)entry;
2570 : 0 : sub_policy = mlx5_ipool_get
2571 : 0 : (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
2572 : : policy_idx);
2573 [ # # # # ]: 0 : if (!sub_policy || !sub_policy->main_policy)
2574 : 0 : continue;
2575 : : mtr_policy = sub_policy->main_policy;
2576 [ # # ]: 0 : if (mtr_policy->is_queue || mtr_policy->is_rss)
2577 : 0 : mlx5_flow_destroy_sub_policy_with_rxq(dev,
2578 : : mtr_policy);
2579 : : }
2580 : : }
2581 : : }
2582 : :
2583 : : /**
2584 : : * Iterate a meter hierarchy and flush all meters and policies if possible.
2585 : : *
2586 : : * @param[in] dev
2587 : : * Pointer to Ethernet device.
2588 : : * @param[in] fm
2589 : : * Pointer to flow meter.
2590 : : * @param[in] mtr_idx
2591 : : * .Meter's index
2592 : : * @param[out] error
2593 : : * Pointer to rte meter error structure.
2594 : : *
2595 : : * @return
2596 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2597 : : */
2598 : : static int
2599 : 0 : mlx5_flow_meter_flush_hierarchy(struct rte_eth_dev *dev,
2600 : : struct mlx5_flow_meter_info *fm,
2601 : : uint32_t mtr_idx,
2602 : : struct rte_mtr_error *error)
2603 : : {
2604 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2605 : : struct mlx5_flow_meter_policy *policy;
2606 : : uint32_t policy_id;
2607 : : struct mlx5_flow_meter_info *next_fm;
2608 : : uint32_t next_mtr_idx;
2609 : : struct mlx5_flow_meter_policy *next_policy = NULL;
2610 : :
2611 : 0 : policy = mlx5_flow_meter_policy_find(dev, fm->policy_id, NULL);
2612 : : MLX5_ASSERT(policy);
2613 [ # # # # ]: 0 : while (!fm->ref_cnt && policy->is_hierarchy) {
2614 : 0 : policy_id = fm->policy_id;
2615 : 0 : next_fm = mlx5_flow_meter_hierarchy_next_meter(priv, policy, &next_mtr_idx);
2616 [ # # ]: 0 : if (next_fm) {
2617 : 0 : next_policy = mlx5_flow_meter_policy_find(dev,
2618 : : next_fm->policy_id,
2619 : : NULL);
2620 : : MLX5_ASSERT(next_policy);
2621 : : }
2622 [ # # ]: 0 : if (mlx5_flow_meter_params_flush(dev, fm, mtr_idx))
2623 : 0 : return -rte_mtr_error_set(error, ENOTSUP,
2624 : : RTE_MTR_ERROR_TYPE_MTR_ID,
2625 : : NULL,
2626 : : "Failed to flush meter.");
2627 [ # # ]: 0 : if (policy->ref_cnt)
2628 : : break;
2629 [ # # ]: 0 : if (__mlx5_flow_meter_policy_delete(dev, policy_id,
2630 : : policy, error, true))
2631 : 0 : return -rte_errno;
2632 : 0 : mlx5_free(policy);
2633 [ # # ]: 0 : if (!next_fm || !next_policy)
2634 : : break;
2635 : : fm = next_fm;
2636 : 0 : mtr_idx = next_mtr_idx;
2637 : : policy = next_policy;
2638 : : }
2639 : : return 0;
2640 : : }
2641 : :
2642 : : /**
2643 : : * Flush all the hierarchy meters and their policies.
2644 : : *
2645 : : * @param[in] dev
2646 : : * Pointer to Ethernet device.
2647 : : * @param[out] error
2648 : : * Pointer to rte meter error structure.
2649 : : *
2650 : : * @return
2651 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2652 : : */
2653 : : static int
2654 : 0 : mlx5_flow_meter_flush_all_hierarchies(struct rte_eth_dev *dev,
2655 : : struct rte_mtr_error *error)
2656 : : {
2657 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2658 : : struct mlx5_flow_meter_info *fm;
2659 : : struct mlx5_flow_meter_policy *policy;
2660 : : struct mlx5_flow_meter_sub_policy *sub_policy;
2661 : : struct mlx5_flow_meter_info *next_fm;
2662 : : struct mlx5_aso_mtr *aso_mtr;
2663 : 0 : uint32_t mtr_idx = 0;
2664 : : uint32_t i, policy_idx;
2665 : : void *entry;
2666 : :
2667 [ # # # # ]: 0 : if (!priv->mtr_idx_tbl || !priv->policy_idx_tbl)
2668 : : return 0;
2669 [ # # ]: 0 : MLX5_L3T_FOREACH(priv->mtr_idx_tbl, i, entry) {
2670 : 0 : mtr_idx = *(uint32_t *)entry;
2671 [ # # ]: 0 : if (!mtr_idx)
2672 : 0 : continue;
2673 : 0 : aso_mtr = mlx5_aso_meter_by_idx(priv, mtr_idx);
2674 : 0 : fm = &aso_mtr->fm;
2675 [ # # # # ]: 0 : if (fm->ref_cnt || fm->def_policy)
2676 : 0 : continue;
2677 [ # # ]: 0 : if (mlx5_flow_meter_flush_hierarchy(dev, fm, mtr_idx, error))
2678 : 0 : return -rte_errno;
2679 : : }
2680 [ # # ]: 0 : MLX5_L3T_FOREACH(priv->policy_idx_tbl, i, entry) {
2681 : 0 : policy_idx = *(uint32_t *)entry;
2682 : 0 : sub_policy = mlx5_ipool_get
2683 : 0 : (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
2684 : : policy_idx);
2685 [ # # ]: 0 : if (!sub_policy)
2686 : 0 : return -rte_mtr_error_set(error,
2687 : : EINVAL,
2688 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2689 : : NULL, "Meter policy invalid.");
2690 : 0 : policy = sub_policy->main_policy;
2691 [ # # # # : 0 : if (!policy || !policy->is_hierarchy || policy->ref_cnt)
# # ]
2692 : 0 : continue;
2693 : 0 : next_fm = mlx5_flow_meter_hierarchy_next_meter(priv, policy, &mtr_idx);
2694 [ # # ]: 0 : if (__mlx5_flow_meter_policy_delete(dev, i, policy,
2695 : : error, true))
2696 : 0 : return -rte_mtr_error_set(error,
2697 : : EINVAL,
2698 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2699 : : NULL, "Meter policy invalid.");
2700 : 0 : mlx5_free(policy);
2701 [ # # # # : 0 : if (!next_fm || next_fm->ref_cnt || next_fm->def_policy)
# # ]
2702 : 0 : continue;
2703 [ # # ]: 0 : if (mlx5_flow_meter_flush_hierarchy(dev, next_fm,
2704 : : mtr_idx, error))
2705 : 0 : return -rte_errno;
2706 : : }
2707 : : return 0;
2708 : : }
2709 : : /**
2710 : : * Flush meter configuration.
2711 : : *
2712 : : * @param[in] dev
2713 : : * Pointer to Ethernet device.
2714 : : * @param[out] error
2715 : : * Pointer to rte meter error structure.
2716 : : *
2717 : : * @return
2718 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2719 : : */
2720 : : int
2721 : 0 : mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
2722 : : {
2723 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
2724 : : struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
2725 : : struct mlx5_flow_meter_profile *fmp;
2726 : : struct mlx5_legacy_flow_meter *legacy_fm;
2727 : : struct mlx5_flow_meter_info *fm;
2728 : : struct mlx5_flow_meter_sub_policy *sub_policy;
2729 : : void *tmp;
2730 : : uint32_t i, mtr_idx, policy_idx;
2731 : : void *entry;
2732 : : struct mlx5_aso_mtr *aso_mtr;
2733 : :
2734 [ # # ]: 0 : if (!priv->mtr_en)
2735 : : return 0;
2736 [ # # ]: 0 : if (priv->sh->meter_aso_en) {
2737 [ # # ]: 0 : if (mlx5_flow_meter_flush_all_hierarchies(dev, error))
2738 : 0 : return -rte_errno;
2739 [ # # ]: 0 : if (priv->mtr_idx_tbl) {
2740 [ # # ]: 0 : MLX5_L3T_FOREACH(priv->mtr_idx_tbl, i, entry) {
2741 : 0 : mtr_idx = *(uint32_t *)entry;
2742 [ # # ]: 0 : if (mtr_idx) {
2743 : : aso_mtr =
2744 : 0 : mlx5_aso_meter_by_idx(priv, mtr_idx);
2745 : 0 : fm = &aso_mtr->fm;
2746 : 0 : (void)mlx5_flow_meter_params_flush(dev,
2747 : : fm, mtr_idx);
2748 : : }
2749 : : }
2750 : 0 : mlx5_l3t_destroy(priv->mtr_idx_tbl);
2751 : 0 : priv->mtr_idx_tbl = NULL;
2752 : : }
2753 : : } else {
2754 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(legacy_fm, fms, next, tmp) {
2755 : 0 : fm = &legacy_fm->fm;
2756 [ # # ]: 0 : if (mlx5_flow_meter_params_flush(dev, fm, 0))
2757 : 0 : return -rte_mtr_error_set(error, EINVAL,
2758 : : RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
2759 : : NULL, "MTR object meter profile invalid.");
2760 : : }
2761 : : }
2762 [ # # ]: 0 : if (priv->policy_idx_tbl) {
2763 [ # # ]: 0 : MLX5_L3T_FOREACH(priv->policy_idx_tbl, i, entry) {
2764 : 0 : policy_idx = *(uint32_t *)entry;
2765 : 0 : sub_policy = mlx5_ipool_get
2766 : 0 : (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
2767 : : policy_idx);
2768 [ # # ]: 0 : if (!sub_policy)
2769 : 0 : return -rte_mtr_error_set(error,
2770 : : EINVAL,
2771 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2772 : : NULL, "MTR object "
2773 : : "meter policy invalid.");
2774 [ # # ]: 0 : if (__mlx5_flow_meter_policy_delete(dev, i,
2775 : 0 : sub_policy->main_policy,
2776 : : error, true))
2777 : 0 : return -rte_mtr_error_set(error,
2778 : : EINVAL,
2779 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2780 : : NULL, "MTR object "
2781 : : "meter policy invalid.");
2782 : 0 : mlx5_free(sub_policy->main_policy);
2783 : : }
2784 : 0 : mlx5_l3t_destroy(priv->policy_idx_tbl);
2785 : 0 : priv->policy_idx_tbl = NULL;
2786 : : }
2787 [ # # ]: 0 : if (priv->mtr_profile_tbl) {
2788 [ # # ]: 0 : MLX5_L3T_FOREACH(priv->mtr_profile_tbl, i, entry) {
2789 : : fmp = entry;
2790 [ # # ]: 0 : if (mlx5_flow_meter_profile_delete(dev, fmp->id,
2791 : : error))
2792 : 0 : return -rte_mtr_error_set(error, EINVAL,
2793 : : RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2794 : : NULL, "Fail to destroy "
2795 : : "meter profile.");
2796 : : }
2797 : 0 : mlx5_l3t_destroy(priv->mtr_profile_tbl);
2798 : 0 : priv->mtr_profile_tbl = NULL;
2799 : : }
2800 : : #if defined(HAVE_MLX5_HWS_SUPPORT)
2801 [ # # ]: 0 : if (priv->mtr_profile_arr) {
2802 [ # # ]: 0 : for (i = 0; i < priv->mtr_config.nb_meter_profiles; i++) {
2803 : 0 : fmp = mlx5_flow_meter_profile_find(priv, i);
2804 [ # # ]: 0 : if (fmp->initialized) {
2805 : 0 : mlx5_flow_meter_profile_hws_delete(dev, i,
2806 : : error);
2807 : : }
2808 : : }
2809 : : }
2810 : : #endif
2811 : : /* Delete default policy table. */
2812 : 0 : mlx5_flow_destroy_def_policy(dev);
2813 [ # # ]: 0 : if (priv->sh->refcnt == 1)
2814 : 0 : mlx5_flow_destroy_mtr_drop_tbls(dev);
2815 : : #ifdef HAVE_MLX5_HWS_SUPPORT
2816 : : /* Destroy HWS configuration. */
2817 : 0 : mlx5_flow_meter_uninit(dev);
2818 : : #endif
2819 : 0 : return 0;
2820 : : }
|