1 // SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2018 Mellanox Technologies, Ltd
8 #include <rte_malloc.h>
10 #include <rte_mtr_driver.h>
12 #include <mlx5_devx_cmds.h>
15 #include "mlx5_flow.h"
18 * Create the meter action.
21 * Pointer to mlx5_priv.
23 * Pointer to flow meter to be converted.
26 * Pointer to the meter action on success, NULL otherwise.
29 mlx5_flow_meter_action_create(struct mlx5_priv *priv,
30 struct mlx5_flow_meter *fm)
32 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
33 struct mlx5dv_dr_flow_meter_attr mtr_init;
34 void *attr = fm->mfts->fmp;
35 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm =
36 &fm->profile->srtcm_prm;
38 fm->mfts->fmp_size = MLX5_ST_SZ_BYTES(flow_meter_parameters);
39 memset(attr, 0, fm->mfts->fmp_size);
40 MLX5_SET(flow_meter_parameters, attr, valid, 1);
41 MLX5_SET(flow_meter_parameters, attr, bucket_overflow, 1);
42 MLX5_SET(flow_meter_parameters, attr,
43 start_color, MLX5_FLOW_COLOR_GREEN);
44 MLX5_SET(flow_meter_parameters, attr, both_buckets_on_green, 0);
45 MLX5_SET(flow_meter_parameters,
46 attr, cbs_exponent, srtcm->cbs_exponent);
47 MLX5_SET(flow_meter_parameters,
48 attr, cbs_mantissa, srtcm->cbs_mantissa);
49 MLX5_SET(flow_meter_parameters,
50 attr, cir_exponent, srtcm->cir_exponent);
51 MLX5_SET(flow_meter_parameters,
52 attr, cir_mantissa, srtcm->cir_mantissa);
53 MLX5_SET(flow_meter_parameters,
54 attr, ebs_exponent, srtcm->ebs_exponent);
55 MLX5_SET(flow_meter_parameters,
56 attr, ebs_mantissa, srtcm->ebs_mantissa);
58 fm->transfer ? fm->mfts->transfer.tbl->obj :
59 fm->egress ? fm->mfts->egress.tbl->obj :
60 fm->mfts->ingress.tbl->obj;
61 mtr_init.reg_c_index = priv->mtr_color_reg - REG_C_0;
62 mtr_init.flow_meter_parameter = fm->mfts->fmp;
63 mtr_init.flow_meter_parameter_sz = fm->mfts->fmp_size;
64 mtr_init.active = fm->active_state;
65 return mlx5_glue->dv_create_flow_action_meter(&mtr_init);
74 * Find meter profile by id.
77 * Pointer to mlx5_priv.
78 * @param meter_profile_id
82 * Pointer to the profile found on success, NULL otherwise.
84 static struct mlx5_flow_meter_profile *
85 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
87 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
88 struct mlx5_flow_meter_profile *fmp;
90 TAILQ_FOREACH(fmp, fmps, next)
91 if (meter_profile_id == fmp->meter_profile_id)
97 * Validate the MTR profile.
100 * Pointer to Ethernet device.
101 * @param[in] meter_profile_id
104 * Pointer to meter profile detail.
106 * Pointer to the error structure.
109 * 0 on success, a negative errno value otherwise and rte_errno is set.
112 mlx5_flow_meter_profile_validate(struct rte_eth_dev *dev,
113 uint32_t meter_profile_id,
114 struct rte_mtr_meter_profile *profile,
115 struct rte_mtr_error *error)
117 struct mlx5_priv *priv = dev->data->dev_private;
118 struct mlx5_flow_meter_profile *fmp;
120 /* Profile must not be NULL. */
122 return -rte_mtr_error_set(error, EINVAL,
123 RTE_MTR_ERROR_TYPE_METER_PROFILE,
124 NULL, "Meter profile is null.");
125 /* Meter profile ID must be valid. */
126 if (meter_profile_id == UINT32_MAX)
127 return -rte_mtr_error_set(error, EINVAL,
128 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
129 NULL, "Meter profile id not valid.");
130 /* Meter profile must not exist. */
131 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
133 return -rte_mtr_error_set(error, EEXIST,
134 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
136 "Meter profile already exists.");
137 if (profile->alg == RTE_MTR_SRTCM_RFC2697) {
138 if (priv->config.hca_attr.qos.srtcm_sup) {
139 /* Verify support for flow meter parameters. */
140 if (profile->srtcm_rfc2697.cir > 0 &&
141 profile->srtcm_rfc2697.cir <= MLX5_SRTCM_CIR_MAX &&
142 profile->srtcm_rfc2697.cbs > 0 &&
143 profile->srtcm_rfc2697.cbs <= MLX5_SRTCM_CBS_MAX &&
144 profile->srtcm_rfc2697.ebs <= MLX5_SRTCM_EBS_MAX)
147 return -rte_mtr_error_set
149 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
151 profile->srtcm_rfc2697.ebs ?
152 "Metering value ebs must be 0." :
153 "Invalid metering parameters.");
156 return -rte_mtr_error_set(error, ENOTSUP,
157 RTE_MTR_ERROR_TYPE_METER_PROFILE,
158 NULL, "Metering algorithm not supported.");
162 * Calculate mantissa and exponent for cir.
165 * Value to be calculated.
167 * Pointer to the mantissa.
169 * Pointer to the exp.
172 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
175 int64_t delta = INT64_MAX;
180 for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
181 for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
182 _cir = (1000000000ULL * m) >> e;
183 if (llabs(cir - _cir) <= delta) {
184 delta = llabs(cir - _cir);
195 * Calculate mantissa and exponent for xbs.
198 * Value to be calculated.
200 * Pointer to the mantissa.
202 * Pointer to the exp.
205 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
210 /* Special case xbs == 0 ? both exp and matissa are 0. */
216 /* xbs = xbs_mantissa * 2^xbs_exponent */
217 _man = frexp(xbs, &_exp);
218 _man = _man * pow(2, MLX5_MAN_WIDTH);
219 _exp = _exp - MLX5_MAN_WIDTH;
220 *man = (uint8_t)ceil(_man);
225 * Fill the prm meter parameter.
228 * Pointer to meter profie to be converted.
230 * Pointer to the error structure.
233 * 0 on success, a negative errno value otherwise and rte_errno is set.
236 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
237 struct rte_mtr_error *error)
239 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
242 if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
243 return -rte_mtr_error_set(error, ENOTSUP,
244 RTE_MTR_ERROR_TYPE_METER_PROFILE,
245 NULL, "Metering algorithm not supported.");
246 /* cbs = cbs_mantissa * 2^cbs_exponent */
247 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
249 srtcm->cbs_mantissa = man;
250 srtcm->cbs_exponent = exp;
251 /* Check if cbs mantissa is too large. */
252 if (srtcm->cbs_exponent != exp)
253 return -rte_mtr_error_set(error, EINVAL,
254 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
255 "Metering profile parameter cbs is"
257 /* ebs = ebs_mantissa * 2^ebs_exponent */
258 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
260 srtcm->ebs_mantissa = man;
261 srtcm->ebs_exponent = exp;
262 /* Check if ebs mantissa is too large. */
263 if (srtcm->ebs_exponent != exp)
264 return -rte_mtr_error_set(error, EINVAL,
265 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
266 "Metering profile parameter ebs is"
268 /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
269 mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
271 srtcm->cir_mantissa = man;
272 srtcm->cir_exponent = exp;
273 /* Check if cir mantissa is too large. */
274 if (srtcm->cir_exponent != exp)
275 return -rte_mtr_error_set(error, EINVAL,
276 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
277 "Metering profile parameter cir is"
283 * Callback to get MTR capabilities.
286 * Pointer to Ethernet device.
288 * Pointer to save MTR capabilities.
290 * Pointer to the error structure.
293 * 0 on success, a negative errno value otherwise and rte_errno is set.
296 mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
297 struct rte_mtr_capabilities *cap,
298 struct rte_mtr_error *error __rte_unused)
300 struct mlx5_priv *priv = dev->data->dev_private;
301 struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
304 return -rte_mtr_error_set(error, ENOTSUP,
305 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
306 "Meter is not support");
307 memset(cap, 0, sizeof(*cap));
308 cap->n_max = 1 << qattr->log_max_flow_meter;
309 cap->n_shared_max = cap->n_max;
311 cap->shared_identical = 1;
312 cap->shared_n_flows_per_mtr_max = 4 << 20;
313 /* 2M flows can share the same meter. */
314 cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
315 cap->meter_srtcm_rfc2697_n_max = qattr->srtcm_sup ? cap->n_max : 0;
316 cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
317 cap->policer_action_drop_supported = 1;
318 cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
319 RTE_MTR_STATS_N_PKTS_DROPPED;
324 * Callback to add MTR profile.
327 * Pointer to Ethernet device.
328 * @param[in] meter_profile_id
331 * Pointer to meter profile detail.
333 * Pointer to the error structure.
336 * 0 on success, a negative errno value otherwise and rte_errno is set.
339 mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
340 uint32_t meter_profile_id,
341 struct rte_mtr_meter_profile *profile,
342 struct rte_mtr_error *error)
344 struct mlx5_priv *priv = dev->data->dev_private;
345 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
346 struct mlx5_flow_meter_profile *fmp;
350 return -rte_mtr_error_set(error, ENOTSUP,
351 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
352 "Meter is not support");
353 /* Check input params. */
354 ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
358 /* Meter profile memory allocation. */
359 fmp = rte_calloc(__func__, 1, sizeof(struct mlx5_flow_meter_profile),
360 RTE_CACHE_LINE_SIZE);
362 return -rte_mtr_error_set(error, ENOMEM,
363 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
364 NULL, "Meter profile memory "
366 /* Fill profile info. */
367 fmp->meter_profile_id = meter_profile_id;
368 fmp->profile = *profile;
369 /* Fill the flow meter parameters for the PRM. */
370 ret = mlx5_flow_meter_param_fill(fmp, error);
374 TAILQ_INSERT_TAIL(fmps, fmp, next);
382 * Callback to delete MTR profile.
385 * Pointer to Ethernet device.
386 * @param[in] meter_profile_id
389 * Pointer to the error structure.
392 * 0 on success, a negative errno value otherwise and rte_errno is set.
395 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
396 uint32_t meter_profile_id,
397 struct rte_mtr_error *error)
399 struct mlx5_priv *priv = dev->data->dev_private;
400 struct mlx5_flow_meter_profile *fmp;
403 return -rte_mtr_error_set(error, ENOTSUP,
404 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
405 "Meter is not support");
406 /* Meter profile must exist. */
407 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
409 return -rte_mtr_error_set(error, ENOENT,
410 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
412 "Meter profile id invalid.");
413 /* Check profile is unused. */
415 return -rte_mtr_error_set(error, EBUSY,
416 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
417 NULL, "Meter profile in use.");
418 /* Remove from list. */
419 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
425 * Convert wrong color setting action to verbose error.
428 * Policy color action.
431 * Verbose meter color error type.
433 static inline enum rte_mtr_error_type
434 action2error(enum rte_mtr_policer_action action)
437 case MTR_POLICER_ACTION_COLOR_GREEN:
438 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_GREEN;
439 case MTR_POLICER_ACTION_COLOR_YELLOW:
440 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_YELLOW;
441 case MTR_POLICER_ACTION_COLOR_RED:
442 return RTE_MTR_ERROR_TYPE_POLICER_ACTION_RED;
446 return RTE_MTR_ERROR_TYPE_UNSPECIFIED;
450 * Check meter validation.
453 * Pointer to mlx5 private data structure.
454 * @param[in] meter_id
457 * Pointer to rte meter parameters.
459 * Pointer to rte meter error structure.
462 * 0 on success, a negative errno value otherwise and rte_errno is set.
465 mlx5_flow_meter_validate(struct mlx5_priv *priv, uint32_t meter_id,
466 struct rte_mtr_params *params,
467 struct rte_mtr_error *error)
469 static enum rte_mtr_policer_action
470 valid_recol_action[RTE_COLORS] = {
471 MTR_POLICER_ACTION_COLOR_GREEN,
472 MTR_POLICER_ACTION_COLOR_YELLOW,
473 MTR_POLICER_ACTION_COLOR_RED };
476 /* Meter params must not be NULL. */
478 return -rte_mtr_error_set(error, EINVAL,
479 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
480 NULL, "Meter object params null.");
481 /* Previous meter color is not supported. */
482 if (params->use_prev_mtr_color)
483 return -rte_mtr_error_set(error, ENOTSUP,
484 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
486 "Previous meter color "
488 /* Validate policer settings. */
489 for (i = 0; i < RTE_COLORS; i++)
490 if (params->action[i] != valid_recol_action[i] &&
491 params->action[i] != MTR_POLICER_ACTION_DROP)
492 return -rte_mtr_error_set
494 action2error(params->action[i]), NULL,
495 "Recolor action not supported.");
496 /* Validate meter id. */
497 if (mlx5_flow_meter_find(priv, meter_id))
498 return -rte_mtr_error_set(error, EEXIST,
499 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
500 "Meter object already exists.");
505 * Modify the flow meter action.
508 * Pointer to mlx5 private data structure.
510 * Pointer to flow meter to be modified.
512 * Pointer to meter srtcm description parameter.
513 * @param[in] modify_bits
514 * The bit in srtcm to be updated.
515 * @param[in] active_state
516 * The state to be updated.
518 * 0 on success, o negative value otherwise.
521 mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
522 struct mlx5_flow_meter *fm,
523 const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
524 uint64_t modify_bits, uint32_t active_state)
526 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
527 uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
529 struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
532 /* Fill command parameters. */
533 mod_attr.reg_c_index = priv->mtr_color_reg - REG_C_0;
534 mod_attr.flow_meter_parameter = in;
535 mod_attr.flow_meter_parameter_sz = fm->mfts->fmp_size;
536 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
537 mod_attr.active = !!active_state;
541 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
542 MLX5_SET(flow_meter_parameters,
543 attr, cbs_exponent, srtcm->cbs_exponent);
544 MLX5_SET(flow_meter_parameters,
545 attr, cbs_mantissa, srtcm->cbs_mantissa);
547 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
548 MLX5_SET(flow_meter_parameters,
549 attr, cir_exponent, srtcm->cir_exponent);
550 MLX5_SET(flow_meter_parameters,
551 attr, cir_mantissa, srtcm->cir_mantissa);
553 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
554 MLX5_SET(flow_meter_parameters,
555 attr, ebs_exponent, srtcm->ebs_exponent);
556 MLX5_SET(flow_meter_parameters,
557 attr, ebs_mantissa, srtcm->ebs_mantissa);
559 /* Apply modifications to meter only if it was created. */
560 if (fm->mfts->meter_action) {
561 ret = mlx5_glue->dv_modify_flow_action_meter
562 (fm->mfts->meter_action, &mod_attr,
563 rte_cpu_to_be_64(modify_bits));
567 /* Update succeedded modify meter parameters. */
568 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
569 fm->active_state = !!active_state;
570 attr = fm->mfts->fmp;
571 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
572 MLX5_SET(flow_meter_parameters,
573 attr, cbs_exponent, srtcm->cbs_exponent);
574 MLX5_SET(flow_meter_parameters,
575 attr, cbs_mantissa, srtcm->cbs_mantissa);
577 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
578 MLX5_SET(flow_meter_parameters,
579 attr, cir_exponent, srtcm->cir_exponent);
580 MLX5_SET(flow_meter_parameters,
581 attr, cir_mantissa, srtcm->cir_mantissa);
583 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
584 MLX5_SET(flow_meter_parameters,
585 attr, ebs_exponent, srtcm->ebs_exponent);
586 MLX5_SET(flow_meter_parameters,
587 attr, ebs_mantissa, srtcm->ebs_mantissa);
602 * Create meter rules.
605 * Pointer to Ethernet device.
606 * @param[in] meter_id
609 * Pointer to rte meter parameters.
611 * Meter shared with other flow or not.
613 * Pointer to rte meter error structure.
616 * 0 on success, a negative errno value otherwise and rte_errno is set.
619 mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
620 struct rte_mtr_params *params, int shared,
621 struct rte_mtr_error *error)
623 struct mlx5_priv *priv = dev->data->dev_private;
624 struct mlx5_flow_meters *fms = &priv->flow_meters;
625 struct mlx5_flow_meter_profile *fmp;
626 struct mlx5_flow_meter *fm;
627 const struct rte_flow_attr attr = {
630 .transfer = priv->config.dv_esw_en ? 1 : 0,
637 return -rte_mtr_error_set(error, ENOTSUP,
638 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
639 "Meter is not support");
640 /* Validate the parameters. */
641 ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
644 /* Meter profile must exist. */
645 fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
647 return -rte_mtr_error_set(error, ENOENT,
648 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
649 NULL, "Meter profile id not valid.");
650 /* Allocate the flow meter memory. */
651 fm = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MTR], &idx);
653 return -rte_mtr_error_set(error, ENOMEM,
654 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
655 "Memory alloc failed for meter.");
657 /* Fill the flow meter parameters. */
658 fm->meter_id = meter_id;
660 memcpy(fm->action, params->action, sizeof(params->action));
661 fm->stats_mask = params->stats_mask;
663 /* Alloc policer counters. */
664 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++) {
665 fm->policer_stats.cnt[i] = mlx5_counter_alloc(dev);
666 if (!fm->policer_stats.cnt[i])
669 fm->mfts = mlx5_flow_create_mtr_tbls(dev, fm);
672 ret = mlx5_flow_create_policer_rules(dev, fm, &attr);
675 /* Add to the flow meter list. */
676 TAILQ_INSERT_TAIL(fms, fm, next);
677 fm->active_state = 1; /* Config meter starts as active. */
678 fm->shared = !!shared;
679 fm->policer_stats.stats_mask = params->stats_mask;
680 fm->profile->ref_cnt++;
683 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
684 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
685 /* Free policer counters. */
686 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
687 if (fm->policer_stats.cnt[i])
688 mlx5_counter_free(dev, fm->policer_stats.cnt[i]);
689 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
690 return -rte_mtr_error_set(error, -ret,
691 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
692 NULL, "Failed to create devx meter.");
696 * Destroy meter rules.
699 * Pointer to Ethernet device.
700 * @param[in] meter_id
703 * Pointer to rte meter error structure.
706 * 0 on success, a negative errno value otherwise and rte_errno is set.
709 mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
710 struct rte_mtr_error *error)
712 struct mlx5_priv *priv = dev->data->dev_private;
713 struct mlx5_flow_meters *fms = &priv->flow_meters;
714 struct mlx5_flow_meter_profile *fmp;
715 struct mlx5_flow_meter *fm;
716 const struct rte_flow_attr attr = {
719 .transfer = priv->config.dv_esw_en ? 1 : 0,
724 return -rte_mtr_error_set(error, ENOTSUP,
725 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
726 "Meter is not support");
727 /* Meter object must exist. */
728 fm = mlx5_flow_meter_find(priv, meter_id);
730 return -rte_mtr_error_set(error, ENOENT,
731 RTE_MTR_ERROR_TYPE_MTR_ID,
732 NULL, "Meter object id not valid.");
733 /* Meter object must not have any owner. */
735 return -rte_mtr_error_set(error, EBUSY,
736 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
737 NULL, "Meter object is being used.");
738 /* Get the meter profile. */
741 /* Update dependencies. */
743 /* Remove from the flow meter list. */
744 TAILQ_REMOVE(fms, fm, next);
745 /* Free policer counters. */
746 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
747 if (fm->policer_stats.cnt[i])
748 mlx5_counter_free(dev, fm->policer_stats.cnt[i]);
749 /* Free meter flow table */
750 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
751 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
752 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
757 * Modify meter state.
760 * Pointer to mlx5 private data structure.
762 * Pointer to flow meter.
763 * @param[in] new_state
764 * New state to update.
766 * Pointer to rte meter error structure.
769 * 0 on success, a negative errno value otherwise and rte_errno is set.
772 mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
773 struct mlx5_flow_meter *fm,
775 struct rte_mtr_error *error)
777 static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
785 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
786 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
789 if (new_state == MLX5_FLOW_METER_DISABLE)
790 ret = mlx5_flow_meter_action_modify(priv, fm, &srtcm,
793 ret = mlx5_flow_meter_action_modify(priv, fm,
794 &fm->profile->srtcm_prm,
797 return -rte_mtr_error_set(error, -ret,
798 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
801 "Failed to enable meter." :
802 "Failed to disable meter.");
807 * Callback to enable flow meter.
810 * Pointer to Ethernet device.
811 * @param[in] meter_id
814 * Pointer to rte meter error structure.
817 * 0 on success, a negative errno value otherwise and rte_errno is set.
820 mlx5_flow_meter_enable(struct rte_eth_dev *dev,
822 struct rte_mtr_error *error)
824 struct mlx5_priv *priv = dev->data->dev_private;
825 struct mlx5_flow_meter *fm;
829 return -rte_mtr_error_set(error, ENOTSUP,
830 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
831 "Meter is not support");
832 /* Meter object must exist. */
833 fm = mlx5_flow_meter_find(priv, meter_id);
835 return -rte_mtr_error_set(error, ENOENT,
836 RTE_MTR_ERROR_TYPE_MTR_ID,
837 NULL, "Meter not found.");
838 if (fm->active_state == MLX5_FLOW_METER_ENABLE)
840 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
843 fm->active_state = MLX5_FLOW_METER_ENABLE;
848 * Callback to disable flow meter.
851 * Pointer to Ethernet device.
852 * @param[in] meter_id
855 * Pointer to rte meter error structure.
858 * 0 on success, a negative errno value otherwise and rte_errno is set.
861 mlx5_flow_meter_disable(struct rte_eth_dev *dev,
863 struct rte_mtr_error *error)
865 struct mlx5_priv *priv = dev->data->dev_private;
866 struct mlx5_flow_meter *fm;
870 return -rte_mtr_error_set(error, ENOTSUP,
871 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
872 "Meter is not support");
873 /* Meter object must exist. */
874 fm = mlx5_flow_meter_find(priv, meter_id);
876 return -rte_mtr_error_set(error, ENOENT,
877 RTE_MTR_ERROR_TYPE_MTR_ID,
878 NULL, "Meter not found.");
879 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
881 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
884 fm->active_state = MLX5_FLOW_METER_DISABLE;
889 * Callback to update meter profile.
892 * Pointer to Ethernet device.
893 * @param[in] meter_id
895 * @param[in] meter_profile_id
896 * To be updated meter profile id.
898 * Pointer to rte meter error structure.
901 * 0 on success, a negative errno value otherwise and rte_errno is set.
904 mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
906 uint32_t meter_profile_id,
907 struct rte_mtr_error *error)
909 struct mlx5_priv *priv = dev->data->dev_private;
910 struct mlx5_flow_meter_profile *fmp;
911 struct mlx5_flow_meter_profile *old_fmp;
912 struct mlx5_flow_meter *fm;
913 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
914 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
918 return -rte_mtr_error_set(error, ENOTSUP,
919 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
920 "Meter is not support");
921 /* Meter profile must exist. */
922 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
924 return -rte_mtr_error_set(error, ENOENT,
925 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
926 NULL, "Meter profile not found.");
927 /* Meter object must exist. */
928 fm = mlx5_flow_meter_find(priv, meter_id);
930 return -rte_mtr_error_set(error, ENOENT,
931 RTE_MTR_ERROR_TYPE_MTR_ID,
932 NULL, "Meter not found.");
933 /* MTR object already set to meter profile id. */
934 old_fmp = fm->profile;
937 /* Update the profile. */
939 /* Update meter params in HW (if not disabled). */
940 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
942 ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
943 modify_bits, fm->active_state);
945 fm->profile = old_fmp;
946 return -rte_mtr_error_set(error, -ret,
947 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
948 NULL, "Failed to update meter"
949 " parmeters in hardware.");
957 * Callback to update meter stats mask.
960 * Pointer to Ethernet device.
961 * @param[in] meter_id
963 * @param[in] stats_mask
964 * To be updated stats_mask.
966 * Pointer to rte meter error structure.
969 * 0 on success, a negative errno value otherwise and rte_errno is set.
972 mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
975 struct rte_mtr_error *error)
977 struct mlx5_priv *priv = dev->data->dev_private;
978 struct mlx5_flow_meter *fm;
981 return -rte_mtr_error_set(error, ENOTSUP,
982 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
983 "Meter is not support");
984 /* Meter object must exist. */
985 fm = mlx5_flow_meter_find(priv, meter_id);
987 return -rte_mtr_error_set(error, ENOENT,
988 RTE_MTR_ERROR_TYPE_MTR_ID,
989 NULL, "Meter object id not valid.");
990 fm->policer_stats.stats_mask = stats_mask;
995 * Callback to read meter statistics.
998 * Pointer to Ethernet device.
999 * @param[in] meter_id
1002 * Pointer to store the statistics.
1003 * @param[out] stats_mask
1004 * Pointer to store the stats_mask.
1006 * Statistic to be cleared after read or not.
1008 * Pointer to rte meter error structure.
1011 * 0 on success, a negative errno value otherwise and rte_errno is set.
1014 mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
1016 struct rte_mtr_stats *stats,
1017 uint64_t *stats_mask,
1019 struct rte_mtr_error *error)
1021 static uint64_t meter2mask[RTE_MTR_DROPPED + 1] = {
1022 RTE_MTR_STATS_N_PKTS_GREEN | RTE_MTR_STATS_N_BYTES_GREEN,
1023 RTE_MTR_STATS_N_PKTS_YELLOW | RTE_MTR_STATS_N_BYTES_YELLOW,
1024 RTE_MTR_STATS_N_PKTS_RED | RTE_MTR_STATS_N_BYTES_RED,
1025 RTE_MTR_STATS_N_PKTS_DROPPED | RTE_MTR_STATS_N_BYTES_DROPPED
1027 struct mlx5_priv *priv = dev->data->dev_private;
1028 struct mlx5_flow_meter *fm;
1029 struct mlx5_flow_policer_stats *ps;
1030 uint64_t pkts_dropped = 0;
1031 uint64_t bytes_dropped = 0;
1038 return -rte_mtr_error_set(error, ENOTSUP,
1039 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1040 "Meter is not support");
1041 /* Meter object must exist. */
1042 fm = mlx5_flow_meter_find(priv, meter_id);
1044 return -rte_mtr_error_set(error, ENOENT,
1045 RTE_MTR_ERROR_TYPE_MTR_ID,
1046 NULL, "Meter object id not valid.");
1047 ps = &fm->policer_stats;
1048 *stats_mask = ps->stats_mask;
1049 for (i = 0; i < RTE_MTR_DROPPED; i++) {
1050 if (*stats_mask & meter2mask[i]) {
1051 ret = mlx5_counter_query(dev, ps->cnt[i], clear, &pkts,
1055 if (fm->action[i] == MTR_POLICER_ACTION_DROP) {
1056 pkts_dropped += pkts;
1057 bytes_dropped += bytes;
1059 /* If need to read the packets, set it. */
1060 if ((1 << i) & (*stats_mask & meter2mask[i]))
1061 stats->n_pkts[i] = pkts;
1062 /* If need to read the bytes, set it. */
1063 if ((1 << (RTE_MTR_DROPPED + 1 + i)) &
1064 (*stats_mask & meter2mask[i]))
1065 stats->n_bytes[i] = bytes;
1068 /* Dropped packets/bytes are treated differently. */
1069 if (*stats_mask & meter2mask[i]) {
1070 ret = mlx5_counter_query(dev, ps->cnt[i], clear, &pkts,
1074 pkts += pkts_dropped;
1075 bytes += bytes_dropped;
1076 /* If need to read the packets, set it. */
1077 if ((*stats_mask & meter2mask[i]) &
1078 RTE_MTR_STATS_N_PKTS_DROPPED)
1079 stats->n_pkts_dropped = pkts;
1080 /* If need to read the bytes, set it. */
1081 if ((*stats_mask & meter2mask[i]) &
1082 RTE_MTR_STATS_N_BYTES_DROPPED)
1083 stats->n_bytes_dropped = bytes;
1087 return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
1088 "Failed to read policer counters.");
1091 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
1092 .capabilities_get = mlx5_flow_mtr_cap_get,
1093 .meter_profile_add = mlx5_flow_meter_profile_add,
1094 .meter_profile_delete = mlx5_flow_meter_profile_delete,
1095 .create = mlx5_flow_meter_create,
1096 .destroy = mlx5_flow_meter_destroy,
1097 .meter_enable = mlx5_flow_meter_enable,
1098 .meter_disable = mlx5_flow_meter_disable,
1099 .meter_profile_update = mlx5_flow_meter_profile_update,
1100 .meter_dscp_table_update = NULL,
1101 .policer_actions_update = NULL,
1102 .stats_update = mlx5_flow_meter_stats_update,
1103 .stats_read = mlx5_flow_meter_stats_read,
1107 * Get meter operations.
1110 * Pointer to Ethernet device structure.
1112 * Pointer to set the mtr operations.
1118 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
1120 *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
1128 * Pointer to mlx5_priv.
1133 * Pointer to the profile found on success, NULL otherwise.
1135 struct mlx5_flow_meter *
1136 mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id)
1138 struct mlx5_flow_meters *fms = &priv->flow_meters;
1139 struct mlx5_flow_meter *fm;
1141 TAILQ_FOREACH(fm, fms, next)
1142 if (meter_id == fm->meter_id)
1148 * Attach meter to flow.
1149 * Unidirectional Meter creation can only be done
1150 * when flow direction is known, i.e. when calling meter_attach.
1153 * Pointer to mlx5 private data.
1154 * @param [in] meter_id
1157 * Pointer to flow attributes.
1158 * @param [out] error
1159 * Pointer to error structure.
1161 * @return the flow meter pointer, NULL otherwise.
1163 struct mlx5_flow_meter *
1164 mlx5_flow_meter_attach(struct mlx5_priv *priv, uint32_t meter_id,
1165 const struct rte_flow_attr *attr,
1166 struct rte_flow_error *error)
1168 struct mlx5_flow_meter *fm;
1170 fm = mlx5_flow_meter_find(priv, meter_id);
1172 rte_flow_error_set(error, ENOENT,
1173 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1174 "Meter object id not valid");
1177 if (!fm->shared && fm->ref_cnt) {
1178 DRV_LOG(ERR, "Cannot share a non-shared meter.");
1179 rte_flow_error_set(error, EINVAL,
1180 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1181 "Meter can't be shared");
1184 if (!fm->ref_cnt++) {
1185 MLX5_ASSERT(!fm->mfts->meter_action);
1186 fm->ingress = attr->ingress;
1187 fm->egress = attr->egress;
1188 fm->transfer = attr->transfer;
1189 /* This also creates the meter object. */
1190 fm->mfts->meter_action = mlx5_flow_meter_action_create(priv,
1192 if (!fm->mfts->meter_action)
1195 MLX5_ASSERT(fm->mfts->meter_action);
1196 if (attr->transfer != fm->transfer ||
1197 attr->ingress != fm->ingress ||
1198 attr->egress != fm->egress) {
1199 DRV_LOG(ERR, "meter I/O attributes do not "
1200 "match flow I/O attributes.");
1206 mlx5_flow_meter_detach(fm);
1207 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1208 fm->mfts->meter_action ? "Meter attr not match" :
1209 "Meter action create failed");
1215 * Detach meter from flow.
1218 * Pointer to flow meter.
1221 mlx5_flow_meter_detach(struct mlx5_flow_meter *fm)
1223 MLX5_ASSERT(fm->ref_cnt);
1226 if (fm->mfts->meter_action)
1227 mlx5_glue->destroy_flow_action(fm->mfts->meter_action);
1228 fm->mfts->meter_action = NULL;
1235 * Flush meter configuration.
1238 * Pointer to Ethernet device.
1240 * Pointer to rte meter error structure.
1243 * 0 on success, a negative errno value otherwise and rte_errno is set.
1246 mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
1248 struct mlx5_priv *priv = dev->data->dev_private;
1249 struct mlx5_flow_meters *fms = &priv->flow_meters;
1250 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
1251 struct mlx5_flow_meter_profile *fmp;
1252 struct mlx5_flow_meter *fm;
1253 const struct rte_flow_attr attr = {
1256 .transfer = priv->config.dv_esw_en ? 1 : 0,
1261 TAILQ_FOREACH_SAFE(fm, fms, next, tmp) {
1262 /* Meter object must not have any owner. */
1263 MLX5_ASSERT(!fm->ref_cnt);
1264 /* Get meter profile. */
1267 return -rte_mtr_error_set(error, EINVAL,
1268 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1269 NULL, "MTR object meter profile invalid.");
1270 /* Update dependencies. */
1272 /* Remove from list. */
1273 TAILQ_REMOVE(fms, fm, next);
1274 /* Free policer counters. */
1275 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
1276 if (fm->policer_stats.cnt[i])
1277 mlx5_counter_free(dev,
1278 fm->policer_stats.cnt[i]);
1279 /* Free meter flow table. */
1280 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
1281 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
1282 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
1284 TAILQ_FOREACH_SAFE(fmp, fmps, next, tmp) {
1286 MLX5_ASSERT(!fmp->ref_cnt);
1287 /* Remove from list. */
1288 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);