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>
13 #include <mlx5_malloc.h>
16 #include "mlx5_flow.h"
19 * Create the meter action.
22 * Pointer to mlx5_priv.
24 * Pointer to flow meter to be converted.
27 * Pointer to the meter action on success, NULL otherwise.
30 mlx5_flow_meter_action_create(struct mlx5_priv *priv,
31 struct mlx5_flow_meter *fm)
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;
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);
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);
75 * Find meter profile by id.
78 * Pointer to mlx5_priv.
79 * @param meter_profile_id
83 * Pointer to the profile found on success, NULL otherwise.
85 static struct mlx5_flow_meter_profile *
86 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
88 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
89 struct mlx5_flow_meter_profile *fmp;
91 TAILQ_FOREACH(fmp, fmps, next)
92 if (meter_profile_id == fmp->meter_profile_id)
98 * Validate the MTR profile.
101 * Pointer to Ethernet device.
102 * @param[in] meter_profile_id
105 * Pointer to meter profile detail.
107 * Pointer to the error structure.
110 * 0 on success, a negative errno value otherwise and rte_errno is set.
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)
118 struct mlx5_priv *priv = dev->data->dev_private;
119 struct mlx5_flow_meter_profile *fmp;
121 /* Profile must not be 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);
134 return -rte_mtr_error_set(error, EEXIST,
135 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
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)
148 return -rte_mtr_error_set
150 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
152 profile->srtcm_rfc2697.ebs ?
153 "Metering value ebs must be 0." :
154 "Invalid metering parameters.");
157 return -rte_mtr_error_set(error, ENOTSUP,
158 RTE_MTR_ERROR_TYPE_METER_PROFILE,
159 NULL, "Metering algorithm not supported.");
163 * Calculate mantissa and exponent for cir.
166 * Value to be calculated.
168 * Pointer to the mantissa.
170 * Pointer to the exp.
173 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
176 int64_t delta = INT64_MAX;
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);
196 * Calculate mantissa and exponent for xbs.
199 * Value to be calculated.
201 * Pointer to the mantissa.
203 * Pointer to the exp.
206 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
211 /* Special case xbs == 0 ? both exp and matissa are 0. */
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);
226 * Fill the prm meter parameter.
229 * Pointer to meter profie to be converted.
231 * Pointer to the error structure.
234 * 0 on success, a negative errno value otherwise and rte_errno is set.
237 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
238 struct rte_mtr_error *error)
240 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
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,
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"
258 /* ebs = ebs_mantissa * 2^ebs_exponent */
259 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
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"
269 /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
270 mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
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"
284 * Callback to get MTR capabilities.
287 * Pointer to Ethernet device.
289 * Pointer to save MTR capabilities.
291 * Pointer to the error structure.
294 * 0 on success, a negative errno value otherwise and rte_errno is set.
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)
301 struct mlx5_priv *priv = dev->data->dev_private;
302 struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
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;
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;
325 * Callback to add MTR profile.
328 * Pointer to Ethernet device.
329 * @param[in] meter_profile_id
332 * Pointer to meter profile detail.
334 * Pointer to the error structure.
337 * 0 on success, a negative errno value otherwise and rte_errno is set.
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)
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;
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,
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);
363 return -rte_mtr_error_set(error, ENOMEM,
364 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
365 NULL, "Meter profile memory "
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);
375 TAILQ_INSERT_TAIL(fmps, fmp, next);
383 * Callback to delete MTR profile.
386 * Pointer to Ethernet device.
387 * @param[in] meter_profile_id
390 * Pointer to the error structure.
393 * 0 on success, a negative errno value otherwise and rte_errno is set.
396 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
397 uint32_t meter_profile_id,
398 struct rte_mtr_error *error)
400 struct mlx5_priv *priv = dev->data->dev_private;
401 struct mlx5_flow_meter_profile *fmp;
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);
410 return -rte_mtr_error_set(error, ENOENT,
411 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
413 "Meter profile id is invalid.");
414 /* Check profile is unused. */
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);
426 * Convert wrong color setting action to verbose error.
429 * Policy color action.
432 * Verbose meter color error type.
434 static inline enum rte_mtr_error_type
435 action2error(enum rte_mtr_policer_action 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;
447 return RTE_MTR_ERROR_TYPE_UNSPECIFIED;
451 * Check meter validation.
454 * Pointer to mlx5 private data structure.
455 * @param[in] meter_id
458 * Pointer to rte meter parameters.
460 * Pointer to rte meter error structure.
463 * 0 on success, a negative errno value otherwise and rte_errno is set.
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)
470 /* Meter must use global drop action. */
471 if (!priv->sh->dr_drop_action)
472 return -rte_mtr_error_set(error, ENOTSUP,
473 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
475 "No drop action ready for meter.");
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 if (params->action[RTE_COLOR_RED] != MTR_POLICER_ACTION_DROP)
490 return -rte_mtr_error_set
492 action2error(params->action[RTE_COLOR_RED]),
494 "Red color only supports drop action.");
495 if (params->action[RTE_COLOR_GREEN] != MTR_POLICER_ACTION_COLOR_GREEN)
496 return -rte_mtr_error_set
498 action2error(params->action[RTE_COLOR_GREEN]),
500 "Green color only supports recolor green action.");
501 /* Validate meter id. */
502 if (mlx5_flow_meter_find(priv, meter_id))
503 return -rte_mtr_error_set(error, EEXIST,
504 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
505 "Meter object already exists.");
510 * Modify the flow meter action.
513 * Pointer to mlx5 private data structure.
515 * Pointer to flow meter to be modified.
517 * Pointer to meter srtcm description parameter.
518 * @param[in] modify_bits
519 * The bit in srtcm to be updated.
520 * @param[in] active_state
521 * The state to be updated.
523 * 0 on success, o negative value otherwise.
526 mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
527 struct mlx5_flow_meter *fm,
528 const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
529 uint64_t modify_bits, uint32_t active_state)
531 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
532 uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
534 struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
537 /* Fill command parameters. */
538 mod_attr.reg_c_index = priv->mtr_color_reg - REG_C_0;
539 mod_attr.flow_meter_parameter = in;
540 mod_attr.flow_meter_parameter_sz = fm->mfts->fmp_size;
541 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
542 mod_attr.active = !!active_state;
546 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
547 MLX5_SET(flow_meter_parameters,
548 attr, cbs_exponent, srtcm->cbs_exponent);
549 MLX5_SET(flow_meter_parameters,
550 attr, cbs_mantissa, srtcm->cbs_mantissa);
552 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
553 MLX5_SET(flow_meter_parameters,
554 attr, cir_exponent, srtcm->cir_exponent);
555 MLX5_SET(flow_meter_parameters,
556 attr, cir_mantissa, srtcm->cir_mantissa);
558 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
559 MLX5_SET(flow_meter_parameters,
560 attr, ebs_exponent, srtcm->ebs_exponent);
561 MLX5_SET(flow_meter_parameters,
562 attr, ebs_mantissa, srtcm->ebs_mantissa);
564 /* Apply modifications to meter only if it was created. */
565 if (fm->mfts->meter_action) {
566 ret = mlx5_glue->dv_modify_flow_action_meter
567 (fm->mfts->meter_action, &mod_attr,
568 rte_cpu_to_be_64(modify_bits));
572 /* Update succeedded modify meter parameters. */
573 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
574 fm->active_state = !!active_state;
575 attr = fm->mfts->fmp;
576 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
577 MLX5_SET(flow_meter_parameters,
578 attr, cbs_exponent, srtcm->cbs_exponent);
579 MLX5_SET(flow_meter_parameters,
580 attr, cbs_mantissa, srtcm->cbs_mantissa);
582 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
583 MLX5_SET(flow_meter_parameters,
584 attr, cir_exponent, srtcm->cir_exponent);
585 MLX5_SET(flow_meter_parameters,
586 attr, cir_mantissa, srtcm->cir_mantissa);
588 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
589 MLX5_SET(flow_meter_parameters,
590 attr, ebs_exponent, srtcm->ebs_exponent);
591 MLX5_SET(flow_meter_parameters,
592 attr, ebs_mantissa, srtcm->ebs_mantissa);
607 mlx5_flow_meter_stats_enable_update(struct mlx5_flow_meter *fm,
610 fm->green_bytes = (stats_mask & RTE_MTR_STATS_N_BYTES_GREEN) ? 1 : 0;
611 fm->green_pkts = (stats_mask & RTE_MTR_STATS_N_PKTS_GREEN) ? 1 : 0;
612 fm->red_bytes = (stats_mask & RTE_MTR_STATS_N_BYTES_RED) ? 1 : 0;
613 fm->red_pkts = (stats_mask & RTE_MTR_STATS_N_PKTS_RED) ? 1 : 0;
615 (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED) ? 1 : 0;
616 fm->pkts_dropped = (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED) ? 1 : 0;
620 * Create meter rules.
623 * Pointer to Ethernet device.
624 * @param[in] meter_id
627 * Pointer to rte meter parameters.
629 * Meter shared with other flow or not.
631 * Pointer to rte meter error structure.
634 * 0 on success, a negative errno value otherwise and rte_errno is set.
637 mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
638 struct rte_mtr_params *params, int shared,
639 struct rte_mtr_error *error)
641 struct mlx5_priv *priv = dev->data->dev_private;
642 struct mlx5_flow_meters *fms = &priv->flow_meters;
643 struct mlx5_flow_meter_profile *fmp;
644 struct mlx5_flow_meter *fm;
645 const struct rte_flow_attr attr = {
648 .transfer = priv->config.dv_esw_en ? 1 : 0,
650 struct mlx5_indexed_pool_config flow_ipool_cfg = {
654 .type = "mlx5_flow_mtr_flow_id_pool",
659 uint8_t mtr_reg_bits = priv->mtr_reg_share ?
660 MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
663 return -rte_mtr_error_set(error, ENOTSUP,
664 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
665 "Meter is not supported");
666 /* Validate the parameters. */
667 ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
670 /* Meter profile must exist. */
671 fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
673 return -rte_mtr_error_set(error, ENOENT,
674 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
675 NULL, "Meter profile id not valid.");
676 /* Allocate the flow meter memory. */
677 fm = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MTR], &idx);
679 return -rte_mtr_error_set(error, ENOMEM,
680 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
681 "Memory alloc failed for meter.");
682 mtr_id_bits = MLX5_REG_BITS - __builtin_clz(idx);
683 if ((mtr_id_bits + priv->max_mtr_flow_bits) > mtr_reg_bits) {
684 DRV_LOG(ERR, "Meter number exceeds max limit.");
687 if (mtr_id_bits > priv->max_mtr_bits)
688 priv->max_mtr_bits = mtr_id_bits;
690 /* Fill the flow meter parameters. */
691 fm->meter_id = meter_id;
693 memcpy(fm->action, params->action, sizeof(params->action));
694 mlx5_flow_meter_stats_enable_update(fm, params->stats_mask);
696 /* Alloc policer counters. */
697 if (fm->green_bytes || fm->green_pkts) {
698 fm->policer_stats.pass_cnt = mlx5_counter_alloc(dev);
699 if (!fm->policer_stats.pass_cnt)
702 if (fm->red_bytes || fm->red_pkts ||
703 fm->bytes_dropped || fm->pkts_dropped) {
704 fm->policer_stats.drop_cnt = mlx5_counter_alloc(dev);
705 if (!fm->policer_stats.drop_cnt)
708 fm->mfts = mlx5_flow_create_mtr_tbls(dev);
711 ret = mlx5_flow_prepare_policer_rules(dev, fm, &attr);
714 /* Add to the flow meter list. */
715 TAILQ_INSERT_TAIL(fms, fm, next);
716 fm->active_state = 1; /* Config meter starts as active. */
717 fm->shared = !!shared;
718 fm->profile->ref_cnt++;
719 fm->flow_ipool = mlx5_ipool_create(&flow_ipool_cfg);
722 rte_spinlock_init(&fm->sl);
725 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
726 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
727 /* Free policer counters. */
728 if (fm->policer_stats.pass_cnt)
729 mlx5_counter_free(dev, fm->policer_stats.pass_cnt);
730 if (fm->policer_stats.drop_cnt)
731 mlx5_counter_free(dev, fm->policer_stats.drop_cnt);
732 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
733 return -rte_mtr_error_set(error, -ret,
734 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
735 NULL, "Failed to create devx meter.");
739 * Destroy meter rules.
742 * Pointer to Ethernet device.
743 * @param[in] meter_id
746 * Pointer to rte meter error structure.
749 * 0 on success, a negative errno value otherwise and rte_errno is set.
752 mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
753 struct rte_mtr_error *error)
755 struct mlx5_priv *priv = dev->data->dev_private;
756 struct mlx5_flow_meters *fms = &priv->flow_meters;
757 struct mlx5_flow_meter_profile *fmp;
758 struct mlx5_flow_meter *fm;
759 const struct rte_flow_attr attr = {
762 .transfer = priv->config.dv_esw_en ? 1 : 0,
766 return -rte_mtr_error_set(error, ENOTSUP,
767 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
768 "Meter is not supported");
769 /* Meter object must exist. */
770 fm = mlx5_flow_meter_find(priv, meter_id);
772 return -rte_mtr_error_set(error, ENOENT,
773 RTE_MTR_ERROR_TYPE_MTR_ID,
774 NULL, "Meter object id not valid.");
775 /* Meter object must not have any owner. */
777 return -rte_mtr_error_set(error, EBUSY,
778 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
779 NULL, "Meter object is being used.");
780 /* Get the meter profile. */
783 /* Update dependencies. */
785 /* Remove from the flow meter list. */
786 TAILQ_REMOVE(fms, fm, next);
787 /* Free policer counters. */
788 if (fm->policer_stats.pass_cnt)
789 mlx5_counter_free(dev, fm->policer_stats.pass_cnt);
790 if (fm->policer_stats.drop_cnt)
791 mlx5_counter_free(dev, fm->policer_stats.drop_cnt);
792 /* Free meter flow table */
794 mlx5_ipool_destroy(fm->flow_ipool);
795 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
796 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
797 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
802 * Modify meter state.
805 * Pointer to mlx5 private data structure.
807 * Pointer to flow meter.
808 * @param[in] new_state
809 * New state to update.
811 * Pointer to rte meter error structure.
814 * 0 on success, a negative errno value otherwise and rte_errno is set.
817 mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
818 struct mlx5_flow_meter *fm,
820 struct rte_mtr_error *error)
822 static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
830 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
831 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
834 if (new_state == MLX5_FLOW_METER_DISABLE)
835 ret = mlx5_flow_meter_action_modify(priv, fm, &srtcm,
838 ret = mlx5_flow_meter_action_modify(priv, fm,
839 &fm->profile->srtcm_prm,
842 return -rte_mtr_error_set(error, -ret,
843 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
846 "Failed to enable meter." :
847 "Failed to disable meter.");
852 * Callback to enable flow meter.
855 * Pointer to Ethernet device.
856 * @param[in] meter_id
859 * Pointer to rte meter error structure.
862 * 0 on success, a negative errno value otherwise and rte_errno is set.
865 mlx5_flow_meter_enable(struct rte_eth_dev *dev,
867 struct rte_mtr_error *error)
869 struct mlx5_priv *priv = dev->data->dev_private;
870 struct mlx5_flow_meter *fm;
874 return -rte_mtr_error_set(error, ENOTSUP,
875 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
876 "Meter is not supported");
877 /* Meter object must exist. */
878 fm = mlx5_flow_meter_find(priv, meter_id);
880 return -rte_mtr_error_set(error, ENOENT,
881 RTE_MTR_ERROR_TYPE_MTR_ID,
882 NULL, "Meter not found.");
883 if (fm->active_state == MLX5_FLOW_METER_ENABLE)
885 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
888 fm->active_state = MLX5_FLOW_METER_ENABLE;
893 * Callback to disable flow meter.
896 * Pointer to Ethernet device.
897 * @param[in] meter_id
900 * Pointer to rte meter error structure.
903 * 0 on success, a negative errno value otherwise and rte_errno is set.
906 mlx5_flow_meter_disable(struct rte_eth_dev *dev,
908 struct rte_mtr_error *error)
910 struct mlx5_priv *priv = dev->data->dev_private;
911 struct mlx5_flow_meter *fm;
915 return -rte_mtr_error_set(error, ENOTSUP,
916 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
917 "Meter is not supported");
918 /* Meter object must exist. */
919 fm = mlx5_flow_meter_find(priv, meter_id);
921 return -rte_mtr_error_set(error, ENOENT,
922 RTE_MTR_ERROR_TYPE_MTR_ID,
923 NULL, "Meter not found.");
924 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
926 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
929 fm->active_state = MLX5_FLOW_METER_DISABLE;
934 * Callback to update meter profile.
937 * Pointer to Ethernet device.
938 * @param[in] meter_id
940 * @param[in] meter_profile_id
941 * To be updated meter profile id.
943 * Pointer to rte meter error structure.
946 * 0 on success, a negative errno value otherwise and rte_errno is set.
949 mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
951 uint32_t meter_profile_id,
952 struct rte_mtr_error *error)
954 struct mlx5_priv *priv = dev->data->dev_private;
955 struct mlx5_flow_meter_profile *fmp;
956 struct mlx5_flow_meter_profile *old_fmp;
957 struct mlx5_flow_meter *fm;
958 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
959 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
963 return -rte_mtr_error_set(error, ENOTSUP,
964 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
965 "Meter is not supported");
966 /* Meter profile must exist. */
967 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
969 return -rte_mtr_error_set(error, ENOENT,
970 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
971 NULL, "Meter profile not found.");
972 /* Meter object must exist. */
973 fm = mlx5_flow_meter_find(priv, meter_id);
975 return -rte_mtr_error_set(error, ENOENT,
976 RTE_MTR_ERROR_TYPE_MTR_ID,
977 NULL, "Meter not found.");
978 /* MTR object already set to meter profile id. */
979 old_fmp = fm->profile;
982 /* Update the profile. */
984 /* Update meter params in HW (if not disabled). */
985 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
987 ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
988 modify_bits, fm->active_state);
990 fm->profile = old_fmp;
991 return -rte_mtr_error_set(error, -ret,
992 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
993 NULL, "Failed to update meter"
994 " parmeters in hardware.");
1002 * Callback to update meter stats mask.
1005 * Pointer to Ethernet device.
1006 * @param[in] meter_id
1008 * @param[in] stats_mask
1009 * To be updated stats_mask.
1011 * Pointer to rte meter error structure.
1014 * 0 on success, a negative errno value otherwise and rte_errno is set.
1017 mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
1019 uint64_t stats_mask,
1020 struct rte_mtr_error *error)
1022 struct mlx5_priv *priv = dev->data->dev_private;
1023 struct mlx5_flow_meter *fm;
1024 const struct rte_flow_attr attr = {
1027 .transfer = priv->config.dv_esw_en ? 1 : 0,
1029 bool need_updated = false;
1030 struct mlx5_flow_policer_stats old_policer_stats;
1033 return -rte_mtr_error_set(error, ENOTSUP,
1034 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1035 "Meter is not supported");
1036 /* Meter object must exist. */
1037 fm = mlx5_flow_meter_find(priv, meter_id);
1039 return -rte_mtr_error_set(error, ENOENT,
1040 RTE_MTR_ERROR_TYPE_MTR_ID,
1041 NULL, "Meter object id not valid.");
1042 old_policer_stats.pass_cnt = 0;
1043 old_policer_stats.drop_cnt = 0;
1044 if (!!((RTE_MTR_STATS_N_PKTS_GREEN |
1045 RTE_MTR_STATS_N_BYTES_GREEN) & stats_mask) !=
1046 !!fm->policer_stats.pass_cnt) {
1047 need_updated = true;
1048 if (fm->policer_stats.pass_cnt) {
1049 old_policer_stats.pass_cnt = fm->policer_stats.pass_cnt;
1050 fm->policer_stats.pass_cnt = 0;
1052 fm->policer_stats.pass_cnt =
1053 mlx5_counter_alloc(dev);
1054 if (!fm->policer_stats.pass_cnt)
1055 return -rte_mtr_error_set(error, ENOMEM,
1056 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1057 "Counter alloc failed for meter.");
1060 if (!!((RTE_MTR_STATS_N_PKTS_RED | RTE_MTR_STATS_N_BYTES_RED |
1061 RTE_MTR_STATS_N_PKTS_DROPPED | RTE_MTR_STATS_N_BYTES_DROPPED) &
1063 !!fm->policer_stats.drop_cnt) {
1064 need_updated = true;
1065 if (fm->policer_stats.drop_cnt) {
1066 old_policer_stats.drop_cnt = fm->policer_stats.drop_cnt;
1067 fm->policer_stats.drop_cnt = 0;
1069 fm->policer_stats.drop_cnt =
1070 mlx5_counter_alloc(dev);
1071 if (!fm->policer_stats.drop_cnt)
1072 return -rte_mtr_error_set(error, ENOMEM,
1073 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1074 "Counter alloc failed for meter.");
1078 if (mlx5_flow_prepare_policer_rules(dev, fm, &attr)) {
1079 if (fm->policer_stats.pass_cnt &&
1080 fm->policer_stats.pass_cnt !=
1081 old_policer_stats.pass_cnt)
1082 mlx5_counter_free(dev,
1083 fm->policer_stats.pass_cnt);
1084 fm->policer_stats.pass_cnt =
1085 old_policer_stats.pass_cnt;
1086 if (fm->policer_stats.drop_cnt &&
1087 fm->policer_stats.drop_cnt !=
1088 old_policer_stats.drop_cnt)
1089 mlx5_counter_free(dev,
1090 fm->policer_stats.drop_cnt);
1091 fm->policer_stats.pass_cnt =
1092 old_policer_stats.pass_cnt;
1093 return -rte_mtr_error_set(error, ENOTSUP,
1094 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1095 NULL, "Failed to create meter policer rules.");
1097 /* Free old policer counters. */
1098 if (old_policer_stats.pass_cnt)
1099 mlx5_counter_free(dev,
1100 old_policer_stats.pass_cnt);
1101 if (old_policer_stats.drop_cnt)
1102 mlx5_counter_free(dev,
1103 old_policer_stats.drop_cnt);
1105 mlx5_flow_meter_stats_enable_update(fm, stats_mask);
1110 * Callback to read meter statistics.
1113 * Pointer to Ethernet device.
1114 * @param[in] meter_id
1117 * Pointer to store the statistics.
1118 * @param[out] stats_mask
1119 * Pointer to store the stats_mask.
1121 * Statistic to be cleared after read or not.
1123 * Pointer to rte meter error structure.
1126 * 0 on success, a negative errno value otherwise and rte_errno is set.
1129 mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
1131 struct rte_mtr_stats *stats,
1132 uint64_t *stats_mask,
1134 struct rte_mtr_error *error)
1136 struct mlx5_priv *priv = dev->data->dev_private;
1137 struct mlx5_flow_meter *fm;
1138 struct mlx5_flow_policer_stats *ps;
1144 return -rte_mtr_error_set(error, ENOTSUP,
1145 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1146 "Meter is not supported");
1147 /* Meter object must exist. */
1148 fm = mlx5_flow_meter_find(priv, meter_id);
1150 return -rte_mtr_error_set(error, ENOENT,
1151 RTE_MTR_ERROR_TYPE_MTR_ID,
1152 NULL, "Meter object id not valid.");
1153 ps = &fm->policer_stats;
1155 if (fm->green_bytes)
1156 *stats_mask |= RTE_MTR_STATS_N_BYTES_GREEN;
1158 *stats_mask |= RTE_MTR_STATS_N_PKTS_GREEN;
1160 *stats_mask |= RTE_MTR_STATS_N_BYTES_RED;
1162 *stats_mask |= RTE_MTR_STATS_N_PKTS_RED;
1163 if (fm->bytes_dropped)
1164 *stats_mask |= RTE_MTR_STATS_N_BYTES_DROPPED;
1165 if (fm->pkts_dropped)
1166 *stats_mask |= RTE_MTR_STATS_N_PKTS_DROPPED;
1167 memset(stats, 0, sizeof(*stats));
1169 ret = mlx5_counter_query(dev, ps->pass_cnt, clear, &pkts,
1173 /* If need to read the packets, set it. */
1175 stats->n_pkts[RTE_COLOR_GREEN] = pkts;
1176 /* If need to read the bytes, set it. */
1177 if (fm->green_bytes)
1178 stats->n_bytes[RTE_COLOR_GREEN] = bytes;
1181 ret = mlx5_counter_query(dev, ps->drop_cnt, clear, &pkts,
1185 /* If need to read the packets, set it. */
1186 if (fm->pkts_dropped)
1187 stats->n_pkts_dropped = pkts;
1188 /* If need to read the bytes, set it. */
1189 if (fm->bytes_dropped)
1190 stats->n_bytes_dropped = bytes;
1194 return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
1195 "Failed to read policer counters.");
1198 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
1199 .capabilities_get = mlx5_flow_mtr_cap_get,
1200 .meter_profile_add = mlx5_flow_meter_profile_add,
1201 .meter_profile_delete = mlx5_flow_meter_profile_delete,
1202 .create = mlx5_flow_meter_create,
1203 .destroy = mlx5_flow_meter_destroy,
1204 .meter_enable = mlx5_flow_meter_enable,
1205 .meter_disable = mlx5_flow_meter_disable,
1206 .meter_profile_update = mlx5_flow_meter_profile_update,
1207 .meter_dscp_table_update = NULL,
1208 .policer_actions_update = NULL,
1209 .stats_update = mlx5_flow_meter_stats_update,
1210 .stats_read = mlx5_flow_meter_stats_read,
1214 * Get meter operations.
1217 * Pointer to Ethernet device structure.
1219 * Pointer to set the mtr operations.
1225 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
1227 *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
1235 * Pointer to mlx5_priv.
1240 * Pointer to the profile found on success, NULL otherwise.
1242 struct mlx5_flow_meter *
1243 mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id)
1245 struct mlx5_flow_meters *fms = &priv->flow_meters;
1246 struct mlx5_flow_meter *fm;
1248 TAILQ_FOREACH(fm, fms, next)
1249 if (meter_id == fm->meter_id)
1255 * Attach meter to flow.
1256 * Unidirectional Meter creation can only be done
1257 * when flow direction is known, i.e. when calling meter_attach.
1260 * Pointer to mlx5 private data.
1262 * Pointer to flow meter.
1264 * Pointer to flow attributes.
1265 * @param [out] error
1266 * Pointer to error structure.
1269 * 0 on success, a negative errno value otherwise and rte_errno is set.
1272 mlx5_flow_meter_attach(struct mlx5_priv *priv,
1273 struct mlx5_flow_meter *fm,
1274 const struct rte_flow_attr *attr,
1275 struct rte_flow_error *error)
1279 rte_spinlock_lock(&fm->sl);
1280 if (fm->mfts->meter_action) {
1282 attr->transfer == fm->transfer &&
1283 attr->ingress == fm->ingress &&
1284 attr->egress == fm->egress)
1289 fm->ingress = attr->ingress;
1290 fm->egress = attr->egress;
1291 fm->transfer = attr->transfer;
1293 /* This also creates the meter object. */
1294 fm->mfts->meter_action = mlx5_flow_meter_action_create(priv,
1296 if (!fm->mfts->meter_action) {
1302 DRV_LOG(ERR, "Meter action create failed.");
1305 rte_spinlock_unlock(&fm->sl);
1307 rte_flow_error_set(error, EINVAL,
1308 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1309 fm->mfts->meter_action ?
1310 "Meter attr not match" :
1311 "Meter action create failed");
1312 return ret ? -rte_errno : 0;
1316 * Detach meter from flow.
1319 * Pointer to flow meter.
1322 mlx5_flow_meter_detach(struct mlx5_flow_meter *fm)
1324 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
1325 rte_spinlock_lock(&fm->sl);
1326 MLX5_ASSERT(fm->ref_cnt);
1327 if (--fm->ref_cnt == 0) {
1328 mlx5_glue->destroy_flow_action(fm->mfts->meter_action);
1329 fm->mfts->meter_action = NULL;
1334 rte_spinlock_unlock(&fm->sl);
1341 * Flush meter configuration.
1344 * Pointer to Ethernet device.
1346 * Pointer to rte meter error structure.
1349 * 0 on success, a negative errno value otherwise and rte_errno is set.
1352 mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
1354 struct mlx5_priv *priv = dev->data->dev_private;
1355 struct mlx5_flow_meters *fms = &priv->flow_meters;
1356 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
1357 struct mlx5_flow_meter_profile *fmp;
1358 struct mlx5_flow_meter *fm;
1359 const struct rte_flow_attr attr = {
1362 .transfer = priv->config.dv_esw_en ? 1 : 0,
1366 TAILQ_FOREACH_SAFE(fm, fms, next, tmp) {
1367 /* Meter object must not have any owner. */
1368 MLX5_ASSERT(!fm->ref_cnt);
1369 /* Get meter profile. */
1372 return -rte_mtr_error_set(error, EINVAL,
1373 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1374 NULL, "MTR object meter profile invalid.");
1375 /* Update dependencies. */
1377 /* Remove from list. */
1378 TAILQ_REMOVE(fms, fm, next);
1379 /* Free policer counters. */
1380 if (fm->policer_stats.pass_cnt)
1381 mlx5_counter_free(dev, fm->policer_stats.pass_cnt);
1382 if (fm->policer_stats.drop_cnt)
1383 mlx5_counter_free(dev, fm->policer_stats.drop_cnt);
1384 /* Free meter flow table. */
1385 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
1386 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
1387 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
1389 TAILQ_FOREACH_SAFE(fmp, fmps, next, tmp) {
1391 MLX5_ASSERT(!fmp->ref_cnt);
1392 /* Remove from list. */
1393 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);