net/mlx5: fix meter statistics
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_meter.c
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5 #include <math.h>
6
7 #include <rte_tailq.h>
8 #include <rte_malloc.h>
9 #include <rte_mtr.h>
10 #include <rte_mtr_driver.h>
11
12 #include <mlx5_devx_cmds.h>
13 #include <mlx5_malloc.h>
14
15 #include "mlx5.h"
16 #include "mlx5_flow.h"
17
18 /**
19  * Create the meter action.
20  *
21  * @param priv
22  *   Pointer to mlx5_priv.
23  * @param[in] fm
24  *   Pointer to flow meter to be converted.
25  *
26  * @return
27  *   Pointer to the meter action on success, NULL otherwise.
28  */
29 static void *
30 mlx5_flow_meter_action_create(struct mlx5_priv *priv,
31                               struct mlx5_flow_meter *fm)
32 {
33 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
34         struct mlx5dv_dr_flow_meter_attr mtr_init;
35         void *attr = fm->mfts->fmp;
36         struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm =
37                                                      &fm->profile->srtcm_prm;
38
39         fm->mfts->fmp_size = MLX5_ST_SZ_BYTES(flow_meter_parameters);
40         memset(attr, 0, fm->mfts->fmp_size);
41         MLX5_SET(flow_meter_parameters, attr, valid, 1);
42         MLX5_SET(flow_meter_parameters, attr, bucket_overflow, 1);
43         MLX5_SET(flow_meter_parameters, attr,
44                  start_color, MLX5_FLOW_COLOR_GREEN);
45         MLX5_SET(flow_meter_parameters, attr, both_buckets_on_green, 0);
46         MLX5_SET(flow_meter_parameters,
47                  attr, cbs_exponent, srtcm->cbs_exponent);
48         MLX5_SET(flow_meter_parameters,
49                  attr, cbs_mantissa, srtcm->cbs_mantissa);
50         MLX5_SET(flow_meter_parameters,
51                  attr, cir_exponent, srtcm->cir_exponent);
52         MLX5_SET(flow_meter_parameters,
53                  attr, cir_mantissa, srtcm->cir_mantissa);
54         MLX5_SET(flow_meter_parameters,
55                  attr, ebs_exponent, srtcm->ebs_exponent);
56         MLX5_SET(flow_meter_parameters,
57                  attr, ebs_mantissa, srtcm->ebs_mantissa);
58         mtr_init.next_table =
59                 fm->transfer ? fm->mfts->transfer.tbl->obj :
60                     fm->egress ? fm->mfts->egress.tbl->obj :
61                                        fm->mfts->ingress.tbl->obj;
62         mtr_init.reg_c_index = priv->mtr_color_reg - REG_C_0;
63         mtr_init.flow_meter_parameter = fm->mfts->fmp;
64         mtr_init.flow_meter_parameter_sz = fm->mfts->fmp_size;
65         mtr_init.active = fm->active_state;
66         return mlx5_glue->dv_create_flow_action_meter(&mtr_init);
67 #else
68         (void)priv;
69         (void)fm;
70         return NULL;
71 #endif
72 }
73
74 /**
75  * Find meter profile by id.
76  *
77  * @param priv
78  *   Pointer to mlx5_priv.
79  * @param meter_profile_id
80  *   Meter profile id.
81  *
82  * @return
83  *   Pointer to the profile found on success, NULL otherwise.
84  */
85 static struct mlx5_flow_meter_profile *
86 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
87 {
88         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
89         struct mlx5_flow_meter_profile *fmp;
90
91         TAILQ_FOREACH(fmp, fmps, next)
92                 if (meter_profile_id == fmp->meter_profile_id)
93                         return fmp;
94         return NULL;
95 }
96
97 /**
98  * Validate the MTR profile.
99  *
100  * @param[in] dev
101  *   Pointer to Ethernet device.
102  * @param[in] meter_profile_id
103  *   Meter profile id.
104  * @param[in] profile
105  *   Pointer to meter profile detail.
106  * @param[out] error
107  *   Pointer to the error structure.
108  *
109  * @return
110  *   0 on success, a negative errno value otherwise and rte_errno is set.
111  */
112 static int
113 mlx5_flow_meter_profile_validate(struct rte_eth_dev *dev,
114                                  uint32_t meter_profile_id,
115                                  struct rte_mtr_meter_profile *profile,
116                                  struct rte_mtr_error *error)
117 {
118         struct mlx5_priv *priv = dev->data->dev_private;
119         struct mlx5_flow_meter_profile *fmp;
120
121         /* Profile must not be NULL. */
122         if (profile == NULL)
123                 return -rte_mtr_error_set(error, EINVAL,
124                                           RTE_MTR_ERROR_TYPE_METER_PROFILE,
125                                           NULL, "Meter profile is null.");
126         /* Meter profile ID must be valid. */
127         if (meter_profile_id == UINT32_MAX)
128                 return -rte_mtr_error_set(error, EINVAL,
129                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
130                                           NULL, "Meter profile id not valid.");
131         /* Meter profile must not exist. */
132         fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
133         if (fmp)
134                 return -rte_mtr_error_set(error, EEXIST,
135                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
136                                           NULL,
137                                           "Meter profile already exists.");
138         if (profile->alg == RTE_MTR_SRTCM_RFC2697) {
139                 if (priv->config.hca_attr.qos.flow_meter_old) {
140                         /* Verify support for flow meter parameters. */
141                         if (profile->srtcm_rfc2697.cir > 0 &&
142                             profile->srtcm_rfc2697.cir <= MLX5_SRTCM_CIR_MAX &&
143                             profile->srtcm_rfc2697.cbs > 0 &&
144                             profile->srtcm_rfc2697.cbs <= MLX5_SRTCM_CBS_MAX &&
145                             profile->srtcm_rfc2697.ebs <= MLX5_SRTCM_EBS_MAX)
146                                 return 0;
147                         else
148                                 return -rte_mtr_error_set
149                                              (error, ENOTSUP,
150                                               RTE_MTR_ERROR_TYPE_MTR_PARAMS,
151                                               NULL,
152                                               profile->srtcm_rfc2697.ebs ?
153                                               "Metering value ebs must be 0." :
154                                               "Invalid metering parameters.");
155                 }
156         }
157         return -rte_mtr_error_set(error, ENOTSUP,
158                                   RTE_MTR_ERROR_TYPE_METER_PROFILE,
159                                   NULL, "Metering algorithm not supported.");
160 }
161
162 /**
163  * Calculate mantissa and exponent for cir.
164  *
165  * @param[in] cir
166  *   Value to be calculated.
167  * @param[out] man
168  *   Pointer to the mantissa.
169  * @param[out] exp
170  *   Pointer to the exp.
171  */
172 static void
173 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
174 {
175         int64_t _cir;
176         int64_t delta = INT64_MAX;
177         uint8_t _man = 0;
178         uint8_t _exp = 0;
179         uint64_t m, e;
180
181         for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
182                 for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
183                         _cir = (1000000000ULL * m) >> e;
184                         if (llabs(cir - _cir) <= delta) {
185                                 delta = llabs(cir - _cir);
186                                 _man = m;
187                                 _exp = e;
188                         }
189                 }
190         }
191         *man = _man;
192         *exp = _exp;
193 }
194
195 /**
196  * Calculate mantissa and exponent for xbs.
197  *
198  * @param[in] xbs
199  *   Value to be calculated.
200  * @param[out] man
201  *   Pointer to the mantissa.
202  * @param[out] exp
203  *   Pointer to the exp.
204  */
205 static void
206 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
207 {
208         int _exp;
209         double _man;
210
211         /* Special case xbs == 0 ? both exp and matissa are 0. */
212         if (xbs == 0) {
213                 *man = 0;
214                 *exp = 0;
215                 return;
216         }
217         /* xbs = xbs_mantissa * 2^xbs_exponent */
218         _man = frexp(xbs, &_exp);
219         _man = _man * pow(2, MLX5_MAN_WIDTH);
220         _exp = _exp - MLX5_MAN_WIDTH;
221         *man = (uint8_t)ceil(_man);
222         *exp = _exp;
223 }
224
225 /**
226  * Fill the prm meter parameter.
227  *
228  * @param[in,out] fmp
229  *   Pointer to meter profie to be converted.
230  * @param[out] error
231  *   Pointer to the error structure.
232  *
233  * @return
234  *   0 on success, a negative errno value otherwise and rte_errno is set.
235  */
236 static int
237 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
238                           struct rte_mtr_error *error)
239 {
240         struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
241         uint8_t man, exp;
242
243         if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
244                 return -rte_mtr_error_set(error, ENOTSUP,
245                                 RTE_MTR_ERROR_TYPE_METER_PROFILE,
246                                 NULL, "Metering algorithm not supported.");
247          /* cbs = cbs_mantissa * 2^cbs_exponent */
248         mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
249                                     &man, &exp);
250         srtcm->cbs_mantissa = man;
251         srtcm->cbs_exponent = exp;
252         /* Check if cbs mantissa is too large. */
253         if (srtcm->cbs_exponent != exp)
254                 return -rte_mtr_error_set(error, EINVAL,
255                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
256                                           "Metering profile parameter cbs is"
257                                           " invalid.");
258         /* ebs = ebs_mantissa * 2^ebs_exponent */
259         mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
260                                     &man, &exp);
261         srtcm->ebs_mantissa = man;
262         srtcm->ebs_exponent = exp;
263         /* Check if ebs mantissa is too large. */
264         if (srtcm->ebs_exponent != exp)
265                 return -rte_mtr_error_set(error, EINVAL,
266                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
267                                           "Metering profile parameter ebs is"
268                                           " invalid.");
269         /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
270         mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
271                                     &man, &exp);
272         srtcm->cir_mantissa = man;
273         srtcm->cir_exponent = exp;
274         /* Check if cir mantissa is too large. */
275         if (srtcm->cir_exponent != exp)
276                 return -rte_mtr_error_set(error, EINVAL,
277                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
278                                           "Metering profile parameter cir is"
279                                           " invalid.");
280         return 0;
281 }
282
283 /**
284  * Callback to get MTR capabilities.
285  *
286  * @param[in] dev
287  *   Pointer to Ethernet device.
288  * @param[out] cap
289  *   Pointer to save MTR capabilities.
290  * @param[out] error
291  *   Pointer to the error structure.
292  *
293  * @return
294  *   0 on success, a negative errno value otherwise and rte_errno is set.
295  */
296 static int
297 mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
298                  struct rte_mtr_capabilities *cap,
299                  struct rte_mtr_error *error __rte_unused)
300 {
301         struct mlx5_priv *priv = dev->data->dev_private;
302         struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
303
304         if (!priv->mtr_en)
305                 return -rte_mtr_error_set(error, ENOTSUP,
306                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
307                                           "Meter is not supported");
308         memset(cap, 0, sizeof(*cap));
309         cap->n_max = 1 << qattr->log_max_flow_meter;
310         cap->n_shared_max = cap->n_max;
311         cap->identical = 1;
312         cap->shared_identical = 1;
313         cap->shared_n_flows_per_mtr_max = 4 << 20;
314         /* 2M flows can share the same meter. */
315         cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
316         cap->meter_srtcm_rfc2697_n_max = qattr->flow_meter_old ? cap->n_max : 0;
317         cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
318         cap->policer_action_drop_supported = 1;
319         cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
320                           RTE_MTR_STATS_N_PKTS_DROPPED;
321         return 0;
322 }
323
324 /**
325  * Callback to add MTR profile.
326  *
327  * @param[in] dev
328  *   Pointer to Ethernet device.
329  * @param[in] meter_profile_id
330  *   Meter profile id.
331  * @param[in] profile
332  *   Pointer to meter profile detail.
333  * @param[out] error
334  *   Pointer to the error structure.
335  *
336  * @return
337  *   0 on success, a negative errno value otherwise and rte_errno is set.
338  */
339 static int
340 mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
341                        uint32_t meter_profile_id,
342                        struct rte_mtr_meter_profile *profile,
343                        struct rte_mtr_error *error)
344 {
345         struct mlx5_priv *priv = dev->data->dev_private;
346         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
347         struct mlx5_flow_meter_profile *fmp;
348         int ret;
349
350         if (!priv->mtr_en)
351                 return -rte_mtr_error_set(error, ENOTSUP,
352                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
353                                           "Meter is not supported");
354         /* Check input params. */
355         ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
356                                                profile, error);
357         if (ret)
358                 return ret;
359         /* Meter profile memory allocation. */
360         fmp = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_flow_meter_profile),
361                          RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
362         if (fmp == NULL)
363                 return -rte_mtr_error_set(error, ENOMEM,
364                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED,
365                                           NULL, "Meter profile memory "
366                                           "alloc failed.");
367         /* Fill profile info. */
368         fmp->meter_profile_id = meter_profile_id;
369         fmp->profile = *profile;
370         /* Fill the flow meter parameters for the PRM. */
371         ret = mlx5_flow_meter_param_fill(fmp, error);
372         if (ret)
373                 goto error;
374         /* Add to list. */
375         TAILQ_INSERT_TAIL(fmps, fmp, next);
376         return 0;
377 error:
378         mlx5_free(fmp);
379         return ret;
380 }
381
382 /**
383  * Callback to delete MTR profile.
384  *
385  * @param[in] dev
386  *   Pointer to Ethernet device.
387  * @param[in] meter_profile_id
388  *   Meter profile id.
389  * @param[out] error
390  *   Pointer to the error structure.
391  *
392  * @return
393  *   0 on success, a negative errno value otherwise and rte_errno is set.
394  */
395 static int
396 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
397                           uint32_t meter_profile_id,
398                           struct rte_mtr_error *error)
399 {
400         struct mlx5_priv *priv = dev->data->dev_private;
401         struct mlx5_flow_meter_profile *fmp;
402
403         if (!priv->mtr_en)
404                 return -rte_mtr_error_set(error, ENOTSUP,
405                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
406                                           "Meter is not supported");
407         /* Meter profile must exist. */
408         fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
409         if (fmp == NULL)
410                 return -rte_mtr_error_set(error, ENOENT,
411                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
412                                           &meter_profile_id,
413                                           "Meter profile id is invalid.");
414         /* Check profile is unused. */
415         if (fmp->ref_cnt)
416                 return -rte_mtr_error_set(error, EBUSY,
417                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
418                                           NULL, "Meter profile is in use.");
419         /* Remove from list. */
420         TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
421         mlx5_free(fmp);
422         return 0;
423 }
424
425 /**
426  * Convert wrong color setting action to verbose error.
427  *
428  * @param[in] action
429  *   Policy color action.
430  *
431  * @return
432  *   Verbose meter color error type.
433  */
434 static inline enum rte_mtr_error_type
435 action2error(enum rte_mtr_policer_action action)
436 {
437         switch (action) {
438         case MTR_POLICER_ACTION_COLOR_GREEN:
439                 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_GREEN;
440         case MTR_POLICER_ACTION_COLOR_YELLOW:
441                 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_YELLOW;
442         case MTR_POLICER_ACTION_COLOR_RED:
443                 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_RED;
444         default:
445                 break;
446         }
447         return RTE_MTR_ERROR_TYPE_UNSPECIFIED;
448 }
449
450 /**
451  * Check meter validation.
452  *
453  * @param[in] priv
454  *   Pointer to mlx5 private data structure.
455  * @param[in] meter_id
456  *   Meter id.
457  * @param[in] params
458  *   Pointer to rte meter parameters.
459  * @param[out] error
460  *   Pointer to rte meter error structure.
461  *
462  * @return
463  *   0 on success, a negative errno value otherwise and rte_errno is set.
464  */
465 static int
466 mlx5_flow_meter_validate(struct mlx5_priv *priv, uint32_t meter_id,
467                          struct rte_mtr_params *params,
468                          struct rte_mtr_error *error)
469 {
470         static enum rte_mtr_policer_action
471                                 valid_recol_action[RTE_COLORS] = {
472                                                MTR_POLICER_ACTION_COLOR_GREEN,
473                                                MTR_POLICER_ACTION_COLOR_YELLOW,
474                                                MTR_POLICER_ACTION_COLOR_RED };
475         int i;
476
477         /* Meter must use global drop action. */
478         if (!priv->sh->dr_drop_action)
479                 return -rte_mtr_error_set(error, ENOTSUP,
480                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
481                                           NULL,
482                                           "No drop action ready for meter.");
483         /* Meter params must not be NULL. */
484         if (params == NULL)
485                 return -rte_mtr_error_set(error, EINVAL,
486                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
487                                           NULL, "Meter object params null.");
488         /* Previous meter color is not supported. */
489         if (params->use_prev_mtr_color)
490                 return -rte_mtr_error_set(error, ENOTSUP,
491                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
492                                           NULL,
493                                           "Previous meter color "
494                                           "not supported.");
495         /* Validate policer settings. */
496         for (i = 0; i < RTE_COLORS; i++)
497                 if (params->action[i] != valid_recol_action[i] &&
498                     params->action[i] != MTR_POLICER_ACTION_DROP)
499                         return -rte_mtr_error_set
500                                         (error, ENOTSUP,
501                                          action2error(params->action[i]), NULL,
502                                          "Recolor action not supported.");
503         /* Validate meter id. */
504         if (mlx5_flow_meter_find(priv, meter_id))
505                 return -rte_mtr_error_set(error, EEXIST,
506                                           RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
507                                           "Meter object already exists.");
508         return 0;
509 }
510
511 /**
512  * Modify the flow meter action.
513  *
514  * @param[in] priv
515  *   Pointer to mlx5 private data structure.
516  * @param[in] fm
517  *   Pointer to flow meter to be modified.
518  * @param[in] srtcm
519  *   Pointer to meter srtcm description parameter.
520  * @param[in] modify_bits
521  *   The bit in srtcm to be updated.
522  * @param[in] active_state
523  *   The state to be updated.
524  * @return
525  *   0 on success, o negative value otherwise.
526  */
527 static int
528 mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
529                 struct mlx5_flow_meter *fm,
530                 const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
531                 uint64_t modify_bits, uint32_t active_state)
532 {
533 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
534         uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
535         uint32_t *attr;
536         struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
537         int ret;
538
539         /* Fill command parameters. */
540         mod_attr.reg_c_index = priv->mtr_color_reg - REG_C_0;
541         mod_attr.flow_meter_parameter = in;
542         mod_attr.flow_meter_parameter_sz = fm->mfts->fmp_size;
543         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
544                 mod_attr.active = !!active_state;
545         else
546                 mod_attr.active = 0;
547         attr = in;
548         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
549                 MLX5_SET(flow_meter_parameters,
550                          attr, cbs_exponent, srtcm->cbs_exponent);
551                 MLX5_SET(flow_meter_parameters,
552                          attr, cbs_mantissa, srtcm->cbs_mantissa);
553         }
554         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
555                 MLX5_SET(flow_meter_parameters,
556                          attr, cir_exponent, srtcm->cir_exponent);
557                 MLX5_SET(flow_meter_parameters,
558                          attr, cir_mantissa, srtcm->cir_mantissa);
559         }
560         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
561                 MLX5_SET(flow_meter_parameters,
562                          attr, ebs_exponent, srtcm->ebs_exponent);
563                 MLX5_SET(flow_meter_parameters,
564                          attr, ebs_mantissa, srtcm->ebs_mantissa);
565         }
566         /* Apply modifications to meter only if it was created. */
567         if (fm->mfts->meter_action) {
568                 ret = mlx5_glue->dv_modify_flow_action_meter
569                                         (fm->mfts->meter_action, &mod_attr,
570                                         rte_cpu_to_be_64(modify_bits));
571                 if (ret)
572                         return ret;
573         }
574         /* Update succeedded modify meter parameters. */
575         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
576                 fm->active_state = !!active_state;
577         attr = fm->mfts->fmp;
578         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
579                 MLX5_SET(flow_meter_parameters,
580                          attr, cbs_exponent, srtcm->cbs_exponent);
581                 MLX5_SET(flow_meter_parameters,
582                          attr, cbs_mantissa, srtcm->cbs_mantissa);
583         }
584         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
585                 MLX5_SET(flow_meter_parameters,
586                          attr, cir_exponent, srtcm->cir_exponent);
587                 MLX5_SET(flow_meter_parameters,
588                          attr, cir_mantissa, srtcm->cir_mantissa);
589         }
590         if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
591                 MLX5_SET(flow_meter_parameters,
592                          attr, ebs_exponent, srtcm->ebs_exponent);
593                 MLX5_SET(flow_meter_parameters,
594                          attr, ebs_mantissa, srtcm->ebs_mantissa);
595         }
596
597         return 0;
598 #else
599         (void)priv;
600         (void)fm;
601         (void)srtcm;
602         (void)modify_bits;
603         (void)active_state;
604         return -ENOTSUP;
605 #endif
606 }
607
608 /**
609  * Create meter rules.
610  *
611  * @param[in] dev
612  *   Pointer to Ethernet device.
613  * @param[in] meter_id
614  *   Meter id.
615  * @param[in] params
616  *   Pointer to rte meter parameters.
617  * @param[in] shared
618  *   Meter shared with other flow or not.
619  * @param[out] error
620  *   Pointer to rte meter error structure.
621  *
622  * @return
623  *   0 on success, a negative errno value otherwise and rte_errno is set.
624  */
625 static int
626 mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
627                        struct rte_mtr_params *params, int shared,
628                        struct rte_mtr_error *error)
629 {
630         struct mlx5_priv *priv = dev->data->dev_private;
631         struct mlx5_flow_meters *fms = &priv->flow_meters;
632         struct mlx5_flow_meter_profile *fmp;
633         struct mlx5_flow_meter *fm;
634         const struct rte_flow_attr attr = {
635                                 .ingress = 1,
636                                 .egress = 1,
637                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
638                         };
639         struct mlx5_indexed_pool_config flow_ipool_cfg = {
640                 .size = 0,
641                 .trunk_size = 64,
642                 .need_lock = 1,
643                 .type = "mlx5_flow_mtr_flow_id_pool",
644         };
645         int ret;
646         unsigned int i;
647         uint32_t idx = 0;
648         uint8_t mtr_id_bits;
649         uint8_t mtr_reg_bits = priv->mtr_reg_share ?
650                                 MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
651
652         if (!priv->mtr_en)
653                 return -rte_mtr_error_set(error, ENOTSUP,
654                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
655                                           "Meter is not supported");
656         /* Validate the parameters. */
657         ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
658         if (ret)
659                 return ret;
660         /* Meter profile must exist. */
661         fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
662         if (fmp == NULL)
663                 return -rte_mtr_error_set(error, ENOENT,
664                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
665                                           NULL, "Meter profile id not valid.");
666         /* Allocate the flow meter memory. */
667         fm = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MTR], &idx);
668         if (fm == NULL)
669                 return -rte_mtr_error_set(error, ENOMEM,
670                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
671                                           "Memory alloc failed for meter.");
672         mtr_id_bits = MLX5_REG_BITS - __builtin_clz(idx);
673         if ((mtr_id_bits + priv->max_mtr_flow_bits) > mtr_reg_bits) {
674                 DRV_LOG(ERR, "Meter number exceeds max limit.");
675                 goto error;
676         }
677         if (mtr_id_bits > priv->max_mtr_bits)
678                 priv->max_mtr_bits = mtr_id_bits;
679         fm->idx = idx;
680         /* Fill the flow meter parameters. */
681         fm->meter_id = meter_id;
682         fm->profile = fmp;
683         memcpy(fm->action, params->action, sizeof(params->action));
684         fm->stats_mask = params->stats_mask;
685
686         /* Alloc policer counters. */
687         for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++) {
688                 fm->policer_stats.cnt[i] = mlx5_counter_alloc(dev);
689                 if (!fm->policer_stats.cnt[i])
690                         goto error;
691         }
692         fm->mfts = mlx5_flow_create_mtr_tbls(dev);
693         if (!fm->mfts)
694                 goto error;
695         ret = mlx5_flow_prepare_policer_rules(dev, fm, &attr);
696         if (ret)
697                 goto error;
698         /* Add to the flow meter list. */
699         TAILQ_INSERT_TAIL(fms, fm, next);
700         fm->active_state = 1; /* Config meter starts as active. */
701         fm->shared = !!shared;
702         fm->policer_stats.stats_mask = params->stats_mask;
703         fm->profile->ref_cnt++;
704         fm->flow_ipool = mlx5_ipool_create(&flow_ipool_cfg);
705         if (!fm->flow_ipool)
706                 goto error;
707         rte_spinlock_init(&fm->sl);
708         return 0;
709 error:
710         mlx5_flow_destroy_policer_rules(dev, fm, &attr);
711         mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
712         /* Free policer counters. */
713         for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
714                 if (fm->policer_stats.cnt[i])
715                         mlx5_counter_free(dev, fm->policer_stats.cnt[i]);
716         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
717         return -rte_mtr_error_set(error, -ret,
718                                   RTE_MTR_ERROR_TYPE_UNSPECIFIED,
719                                   NULL, "Failed to create devx meter.");
720 }
721
722 /**
723  * Destroy meter rules.
724  *
725  * @param[in] dev
726  *   Pointer to Ethernet device.
727  * @param[in] meter_id
728  *   Meter id.
729  * @param[out] error
730  *   Pointer to rte meter error structure.
731  *
732  * @return
733  *   0 on success, a negative errno value otherwise and rte_errno is set.
734  */
735 static int
736 mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
737                         struct rte_mtr_error *error)
738 {
739         struct mlx5_priv *priv = dev->data->dev_private;
740         struct mlx5_flow_meters *fms = &priv->flow_meters;
741         struct mlx5_flow_meter_profile *fmp;
742         struct mlx5_flow_meter *fm;
743         const struct rte_flow_attr attr = {
744                                 .ingress = 1,
745                                 .egress = 1,
746                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
747                         };
748         unsigned int i;
749
750         if (!priv->mtr_en)
751                 return -rte_mtr_error_set(error, ENOTSUP,
752                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
753                                           "Meter is not supported");
754         /* Meter object must exist. */
755         fm = mlx5_flow_meter_find(priv, meter_id);
756         if (fm == NULL)
757                 return -rte_mtr_error_set(error, ENOENT,
758                                           RTE_MTR_ERROR_TYPE_MTR_ID,
759                                           NULL, "Meter object id not valid.");
760         /* Meter object must not have any owner. */
761         if (fm->ref_cnt > 0)
762                 return -rte_mtr_error_set(error, EBUSY,
763                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED,
764                                           NULL, "Meter object is being used.");
765         /* Get the meter profile. */
766         fmp = fm->profile;
767         MLX5_ASSERT(fmp);
768         /* Update dependencies. */
769         fmp->ref_cnt--;
770         /* Remove from the flow meter list. */
771         TAILQ_REMOVE(fms, fm, next);
772         /* Free policer counters. */
773         for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
774                 if (fm->policer_stats.cnt[i])
775                         mlx5_counter_free(dev, fm->policer_stats.cnt[i]);
776         /* Free meter flow table */
777         if (fm->flow_ipool)
778                 mlx5_ipool_destroy(fm->flow_ipool);
779         mlx5_flow_destroy_policer_rules(dev, fm, &attr);
780         mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
781         mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
782         return 0;
783 }
784
785 /**
786  * Modify meter state.
787  *
788  * @param[in] priv
789  *   Pointer to mlx5 private data structure.
790  * @param[in] fm
791  *   Pointer to flow meter.
792  * @param[in] new_state
793  *   New state to update.
794  * @param[out] error
795  *   Pointer to rte meter error structure.
796  *
797  * @return
798  *   0 on success, a negative errno value otherwise and rte_errno is set.
799  */
800 static int
801 mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
802                              struct mlx5_flow_meter *fm,
803                              uint32_t new_state,
804                              struct rte_mtr_error *error)
805 {
806         static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
807                 .cbs_exponent = 20,
808                 .cbs_mantissa = 191,
809                 .cir_exponent = 0,
810                 .cir_mantissa = 200,
811                 .ebs_exponent = 0,
812                 .ebs_mantissa = 0,
813         };
814         uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
815                                MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
816         int ret;
817
818         if (new_state == MLX5_FLOW_METER_DISABLE)
819                 ret = mlx5_flow_meter_action_modify(priv, fm, &srtcm,
820                                                     modify_bits, 0);
821         else
822                 ret = mlx5_flow_meter_action_modify(priv, fm,
823                                                    &fm->profile->srtcm_prm,
824                                                     modify_bits, 0);
825         if (ret)
826                 return -rte_mtr_error_set(error, -ret,
827                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
828                                           NULL,
829                                           new_state ?
830                                           "Failed to enable meter." :
831                                           "Failed to disable meter.");
832         return 0;
833 }
834
835 /**
836  * Callback to enable flow meter.
837  *
838  * @param[in] dev
839  *   Pointer to Ethernet device.
840  * @param[in] meter_id
841  *   Meter id.
842  * @param[out] error
843  *   Pointer to rte meter error structure.
844  *
845  * @return
846  *   0 on success, a negative errno value otherwise and rte_errno is set.
847  */
848 static int
849 mlx5_flow_meter_enable(struct rte_eth_dev *dev,
850                        uint32_t meter_id,
851                        struct rte_mtr_error *error)
852 {
853         struct mlx5_priv *priv = dev->data->dev_private;
854         struct mlx5_flow_meter *fm;
855         int ret;
856
857         if (!priv->mtr_en)
858                 return -rte_mtr_error_set(error, ENOTSUP,
859                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
860                                           "Meter is not supported");
861         /* Meter object must exist. */
862         fm = mlx5_flow_meter_find(priv, meter_id);
863         if (fm == NULL)
864                 return -rte_mtr_error_set(error, ENOENT,
865                                           RTE_MTR_ERROR_TYPE_MTR_ID,
866                                           NULL, "Meter not found.");
867         if (fm->active_state == MLX5_FLOW_METER_ENABLE)
868                 return 0;
869         ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
870                                            error);
871         if (!ret)
872                 fm->active_state = MLX5_FLOW_METER_ENABLE;
873         return ret;
874 }
875
876 /**
877  * Callback to disable flow meter.
878  *
879  * @param[in] dev
880  *   Pointer to Ethernet device.
881  * @param[in] meter_id
882  *   Meter id.
883  * @param[out] error
884  *   Pointer to rte meter error structure.
885  *
886  * @return
887  *   0 on success, a negative errno value otherwise and rte_errno is set.
888  */
889 static int
890 mlx5_flow_meter_disable(struct rte_eth_dev *dev,
891                         uint32_t meter_id,
892                         struct rte_mtr_error *error)
893 {
894         struct mlx5_priv *priv = dev->data->dev_private;
895         struct mlx5_flow_meter *fm;
896         int ret;
897
898         if (!priv->mtr_en)
899                 return -rte_mtr_error_set(error, ENOTSUP,
900                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
901                                           "Meter is not supported");
902         /* Meter object must exist. */
903         fm = mlx5_flow_meter_find(priv, meter_id);
904         if (fm == NULL)
905                 return -rte_mtr_error_set(error, ENOENT,
906                                           RTE_MTR_ERROR_TYPE_MTR_ID,
907                                           NULL, "Meter not found.");
908         if (fm->active_state == MLX5_FLOW_METER_DISABLE)
909                 return 0;
910         ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
911                                            error);
912         if (!ret)
913                 fm->active_state = MLX5_FLOW_METER_DISABLE;
914         return ret;
915 }
916
917 /**
918  * Callback to update meter profile.
919  *
920  * @param[in] dev
921  *   Pointer to Ethernet device.
922  * @param[in] meter_id
923  *   Meter id.
924  * @param[in] meter_profile_id
925  *   To be updated meter profile id.
926  * @param[out] error
927  *   Pointer to rte meter error structure.
928  *
929  * @return
930  *   0 on success, a negative errno value otherwise and rte_errno is set.
931  */
932 static int
933 mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
934                                uint32_t meter_id,
935                                uint32_t meter_profile_id,
936                                struct rte_mtr_error *error)
937 {
938         struct mlx5_priv *priv = dev->data->dev_private;
939         struct mlx5_flow_meter_profile *fmp;
940         struct mlx5_flow_meter_profile *old_fmp;
941         struct mlx5_flow_meter *fm;
942         uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
943                                MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
944         int ret;
945
946         if (!priv->mtr_en)
947                 return -rte_mtr_error_set(error, ENOTSUP,
948                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
949                                           "Meter is not supported");
950         /* Meter profile must exist. */
951         fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
952         if (fmp == NULL)
953                 return -rte_mtr_error_set(error, ENOENT,
954                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
955                                           NULL, "Meter profile not found.");
956         /* Meter object must exist. */
957         fm = mlx5_flow_meter_find(priv, meter_id);
958         if (fm == NULL)
959                 return -rte_mtr_error_set(error, ENOENT,
960                                           RTE_MTR_ERROR_TYPE_MTR_ID,
961                                           NULL, "Meter not found.");
962         /* MTR object already set to meter profile id. */
963         old_fmp = fm->profile;
964         if (fmp == old_fmp)
965                 return 0;
966         /* Update the profile. */
967         fm->profile = fmp;
968         /* Update meter params in HW (if not disabled). */
969         if (fm->active_state == MLX5_FLOW_METER_DISABLE)
970                 return 0;
971         ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
972                                               modify_bits, fm->active_state);
973         if (ret) {
974                 fm->profile = old_fmp;
975                 return -rte_mtr_error_set(error, -ret,
976                                           RTE_MTR_ERROR_TYPE_MTR_PARAMS,
977                                           NULL, "Failed to update meter"
978                                           " parmeters in hardware.");
979         }
980         old_fmp->ref_cnt--;
981         fmp->ref_cnt++;
982         return 0;
983 }
984
985 /**
986  * Callback to update meter stats mask.
987  *
988  * @param[in] dev
989  *   Pointer to Ethernet device.
990  * @param[in] meter_id
991  *   Meter id.
992  * @param[in] stats_mask
993  *   To be updated stats_mask.
994  * @param[out] error
995  *   Pointer to rte meter error structure.
996  *
997  * @return
998  *   0 on success, a negative errno value otherwise and rte_errno is set.
999  */
1000 static int
1001 mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
1002                              uint32_t meter_id,
1003                              uint64_t stats_mask,
1004                              struct rte_mtr_error *error)
1005 {
1006         struct mlx5_priv *priv = dev->data->dev_private;
1007         struct mlx5_flow_meter *fm;
1008
1009         if (!priv->mtr_en)
1010                 return -rte_mtr_error_set(error, ENOTSUP,
1011                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1012                                           "Meter is not supported");
1013         /* Meter object must exist. */
1014         fm = mlx5_flow_meter_find(priv, meter_id);
1015         if (fm == NULL)
1016                 return -rte_mtr_error_set(error, ENOENT,
1017                                           RTE_MTR_ERROR_TYPE_MTR_ID,
1018                                           NULL, "Meter object id not valid.");
1019         fm->policer_stats.stats_mask = stats_mask;
1020         return 0;
1021 }
1022
1023 /**
1024  * Callback to read meter statistics.
1025  *
1026  * @param[in] dev
1027  *   Pointer to Ethernet device.
1028  * @param[in] meter_id
1029  *   Meter id.
1030  * @param[out] stats
1031  *   Pointer to store the statistics.
1032  * @param[out] stats_mask
1033  *   Pointer to store the stats_mask.
1034  * @param[in] clear
1035  *   Statistic to be cleared after read or not.
1036  * @param[out] error
1037  *   Pointer to rte meter error structure.
1038  *
1039  * @return
1040  *   0 on success, a negative errno value otherwise and rte_errno is set.
1041  */
1042 static int
1043 mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
1044                            uint32_t meter_id,
1045                            struct rte_mtr_stats *stats,
1046                            uint64_t *stats_mask,
1047                            int clear,
1048                            struct rte_mtr_error *error)
1049 {
1050         static uint64_t meter2mask[RTE_MTR_DROPPED + 1] = {
1051                 RTE_MTR_STATS_N_PKTS_GREEN | RTE_MTR_STATS_N_BYTES_GREEN,
1052                 RTE_MTR_STATS_N_PKTS_YELLOW | RTE_MTR_STATS_N_BYTES_YELLOW,
1053                 RTE_MTR_STATS_N_PKTS_RED | RTE_MTR_STATS_N_BYTES_RED,
1054                 RTE_MTR_STATS_N_PKTS_DROPPED | RTE_MTR_STATS_N_BYTES_DROPPED
1055         };
1056         struct mlx5_priv *priv = dev->data->dev_private;
1057         struct mlx5_flow_meter *fm;
1058         struct mlx5_flow_policer_stats *ps;
1059         uint64_t pkts_dropped = 0;
1060         uint64_t bytes_dropped = 0;
1061         uint64_t pkts;
1062         uint64_t bytes;
1063         int i;
1064         int ret = 0;
1065
1066         if (!priv->mtr_en)
1067                 return -rte_mtr_error_set(error, ENOTSUP,
1068                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1069                                           "Meter is not supported");
1070         /* Meter object must exist. */
1071         fm = mlx5_flow_meter_find(priv, meter_id);
1072         if (fm == NULL)
1073                 return -rte_mtr_error_set(error, ENOENT,
1074                                           RTE_MTR_ERROR_TYPE_MTR_ID,
1075                                           NULL, "Meter object id not valid.");
1076         ps = &fm->policer_stats;
1077         *stats_mask = ps->stats_mask;
1078         for (i = 0; i < RTE_MTR_DROPPED; i++) {
1079                 if (*stats_mask & meter2mask[i]) {
1080                         ret = mlx5_counter_query(dev, ps->cnt[i], clear, &pkts,
1081                                                  &bytes);
1082                         if (ret)
1083                                 goto error;
1084                         if (fm->action[i] == MTR_POLICER_ACTION_DROP) {
1085                                 pkts_dropped += pkts;
1086                                 bytes_dropped += bytes;
1087                         }
1088                         /* If need to read the packets, set it. */
1089                         if ((1 << i) & (*stats_mask & meter2mask[i]))
1090                                 stats->n_pkts[i] = pkts;
1091                         /* If need to read the bytes, set it. */
1092                         if ((1 << (RTE_MTR_DROPPED + 1 + i)) &
1093                            (*stats_mask & meter2mask[i]))
1094                                 stats->n_bytes[i] = bytes;
1095                 }
1096         }
1097         /* Dropped packets/bytes are treated differently. */
1098         if (*stats_mask & meter2mask[i]) {
1099                 ret = mlx5_counter_query(dev, ps->cnt[i], clear, &pkts,
1100                                          &bytes);
1101                 if (ret)
1102                         goto error;
1103                 pkts += pkts_dropped;
1104                 bytes += bytes_dropped;
1105                 /* If need to read the packets, set it. */
1106                 if ((*stats_mask & meter2mask[i]) &
1107                    RTE_MTR_STATS_N_PKTS_DROPPED)
1108                         stats->n_pkts_dropped = pkts;
1109                 /* If need to read the bytes, set it. */
1110                 if ((*stats_mask & meter2mask[i]) &
1111                    RTE_MTR_STATS_N_BYTES_DROPPED)
1112                         stats->n_bytes_dropped = bytes;
1113         }
1114         return 0;
1115 error:
1116         return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
1117                                  "Failed to read policer counters.");
1118 }
1119
1120 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
1121         .capabilities_get = mlx5_flow_mtr_cap_get,
1122         .meter_profile_add = mlx5_flow_meter_profile_add,
1123         .meter_profile_delete = mlx5_flow_meter_profile_delete,
1124         .create = mlx5_flow_meter_create,
1125         .destroy = mlx5_flow_meter_destroy,
1126         .meter_enable = mlx5_flow_meter_enable,
1127         .meter_disable = mlx5_flow_meter_disable,
1128         .meter_profile_update = mlx5_flow_meter_profile_update,
1129         .meter_dscp_table_update = NULL,
1130         .policer_actions_update = NULL,
1131         .stats_update = mlx5_flow_meter_stats_update,
1132         .stats_read = mlx5_flow_meter_stats_read,
1133 };
1134
1135 /**
1136  * Get meter operations.
1137  *
1138  * @param dev
1139  *   Pointer to Ethernet device structure.
1140  * @param arg
1141  *   Pointer to set the mtr operations.
1142  *
1143  * @return
1144  *   Always 0.
1145  */
1146 int
1147 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
1148 {
1149         *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
1150         return 0;
1151 }
1152
1153 /**
1154  * Find meter by id.
1155  *
1156  * @param priv
1157  *   Pointer to mlx5_priv.
1158  * @param meter_id
1159  *   Meter id.
1160  *
1161  * @return
1162  *   Pointer to the profile found on success, NULL otherwise.
1163  */
1164 struct mlx5_flow_meter *
1165 mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id)
1166 {
1167         struct mlx5_flow_meters *fms = &priv->flow_meters;
1168         struct mlx5_flow_meter *fm;
1169
1170         TAILQ_FOREACH(fm, fms, next)
1171                 if (meter_id == fm->meter_id)
1172                         return fm;
1173         return NULL;
1174 }
1175
1176 /**
1177  * Attach meter to flow.
1178  * Unidirectional Meter creation can only be done
1179  * when flow direction is known, i.e. when calling meter_attach.
1180  *
1181  * @param [in] priv
1182  *  Pointer to mlx5 private data.
1183  * @param[in] fm
1184  *   Pointer to flow meter.
1185  * @param [in] attr
1186  *  Pointer to flow attributes.
1187  * @param [out] error
1188  *  Pointer to error structure.
1189  *
1190  * @return
1191  *   0 on success, a negative errno value otherwise and rte_errno is set.
1192  */
1193 int
1194 mlx5_flow_meter_attach(struct mlx5_priv *priv,
1195                        struct mlx5_flow_meter *fm,
1196                        const struct rte_flow_attr *attr,
1197                        struct rte_flow_error *error)
1198 {
1199         int ret = 0;
1200
1201         rte_spinlock_lock(&fm->sl);
1202         if (fm->mfts->meter_action) {
1203                 if (fm->shared &&
1204                     attr->transfer == fm->transfer &&
1205                     attr->ingress == fm->ingress &&
1206                     attr->egress == fm->egress)
1207                         fm->ref_cnt++;
1208                 else
1209                         ret = -1;
1210         } else {
1211                 fm->ingress = attr->ingress;
1212                 fm->egress = attr->egress;
1213                 fm->transfer = attr->transfer;
1214                  fm->ref_cnt = 1;
1215                 /* This also creates the meter object. */
1216                 fm->mfts->meter_action = mlx5_flow_meter_action_create(priv,
1217                                                                        fm);
1218                 if (!fm->mfts->meter_action) {
1219                         fm->ref_cnt = 0;
1220                         fm->ingress = 0;
1221                         fm->egress = 0;
1222                         fm->transfer = 0;
1223                         ret = -1;
1224                         DRV_LOG(ERR, "Meter action create failed.");
1225                 }
1226         }
1227         rte_spinlock_unlock(&fm->sl);
1228         if (ret)
1229                 rte_flow_error_set(error, EINVAL,
1230                                    RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1231                                    fm->mfts->meter_action ?
1232                                    "Meter attr not match" :
1233                                    "Meter action create failed");
1234         return ret ? -rte_errno : 0;
1235 }
1236
1237 /**
1238  * Detach meter from flow.
1239  *
1240  * @param [in] fm
1241  *  Pointer to flow meter.
1242  */
1243 void
1244 mlx5_flow_meter_detach(struct mlx5_flow_meter *fm)
1245 {
1246 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
1247         rte_spinlock_lock(&fm->sl);
1248         MLX5_ASSERT(fm->ref_cnt);
1249         if (--fm->ref_cnt == 0) {
1250                 mlx5_glue->destroy_flow_action(fm->mfts->meter_action);
1251                 fm->mfts->meter_action = NULL;
1252                 fm->ingress = 0;
1253                 fm->egress = 0;
1254                 fm->transfer = 0;
1255         }
1256         rte_spinlock_unlock(&fm->sl);
1257 #else
1258         (void)fm;
1259 #endif
1260 }
1261
1262 /**
1263  * Flush meter configuration.
1264  *
1265  * @param[in] dev
1266  *   Pointer to Ethernet device.
1267  * @param[out] error
1268  *   Pointer to rte meter error structure.
1269  *
1270  * @return
1271  *   0 on success, a negative errno value otherwise and rte_errno is set.
1272  */
1273 int
1274 mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
1275 {
1276         struct mlx5_priv *priv = dev->data->dev_private;
1277         struct mlx5_flow_meters *fms = &priv->flow_meters;
1278         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
1279         struct mlx5_flow_meter_profile *fmp;
1280         struct mlx5_flow_meter *fm;
1281         const struct rte_flow_attr attr = {
1282                                 .ingress = 1,
1283                                 .egress = 1,
1284                                 .transfer = priv->config.dv_esw_en ? 1 : 0,
1285                         };
1286         void *tmp;
1287         uint32_t i;
1288
1289         TAILQ_FOREACH_SAFE(fm, fms, next, tmp) {
1290                 /* Meter object must not have any owner. */
1291                 MLX5_ASSERT(!fm->ref_cnt);
1292                 /* Get meter profile. */
1293                 fmp = fm->profile;
1294                 if (fmp == NULL)
1295                         return -rte_mtr_error_set(error, EINVAL,
1296                                 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1297                                 NULL, "MTR object meter profile invalid.");
1298                 /* Update dependencies. */
1299                 fmp->ref_cnt--;
1300                 /* Remove from list. */
1301                 TAILQ_REMOVE(fms, fm, next);
1302                 /* Free policer counters. */
1303                 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
1304                         if (fm->policer_stats.cnt[i])
1305                                 mlx5_counter_free(dev,
1306                                                   fm->policer_stats.cnt[i]);
1307                 /* Free meter flow table. */
1308                 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
1309                 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
1310                 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
1311         }
1312         TAILQ_FOREACH_SAFE(fmp, fmps, next, tmp) {
1313                 /* Check unused. */
1314                 MLX5_ASSERT(!fmp->ref_cnt);
1315                 /* Remove from list. */
1316                 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
1317                 mlx5_free(fmp);
1318         }
1319         return 0;
1320 }