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_info *fm)
33 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
34 struct mlx5dv_dr_flow_meter_attr mtr_init;
35 uint32_t fmp[MLX5_ST_SZ_DW(flow_meter_parameters)];
36 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm =
37 &fm->profile->srtcm_prm;
38 uint32_t cbs_cir = rte_be_to_cpu_32(srtcm->cbs_cir);
39 uint32_t ebs_eir = rte_be_to_cpu_32(srtcm->ebs_eir);
41 enum mlx5_meter_domain domain =
42 fm->transfer ? MLX5_MTR_DOMAIN_TRANSFER :
43 fm->egress ? MLX5_MTR_DOMAIN_EGRESS :
44 MLX5_MTR_DOMAIN_INGRESS;
45 struct mlx5_flow_meter_def_policy *def_policy =
46 priv->sh->mtrmng->def_policy[domain];
48 memset(fmp, 0, MLX5_ST_SZ_BYTES(flow_meter_parameters));
49 MLX5_SET(flow_meter_parameters, fmp, valid, 1);
50 MLX5_SET(flow_meter_parameters, fmp, bucket_overflow, 1);
51 MLX5_SET(flow_meter_parameters, fmp,
52 start_color, MLX5_FLOW_COLOR_GREEN);
53 MLX5_SET(flow_meter_parameters, fmp, both_buckets_on_green, 0);
54 val = (cbs_cir >> ASO_DSEG_CBS_EXP_OFFSET) & ASO_DSEG_EXP_MASK;
55 MLX5_SET(flow_meter_parameters, fmp, cbs_exponent, val);
56 val = (cbs_cir >> ASO_DSEG_CBS_MAN_OFFSET) & ASO_DSEG_MAN_MASK;
57 MLX5_SET(flow_meter_parameters, fmp, cbs_mantissa, val);
58 val = (cbs_cir >> ASO_DSEG_CIR_EXP_OFFSET) & ASO_DSEG_EXP_MASK;
59 MLX5_SET(flow_meter_parameters, fmp, cir_exponent, val);
60 val = (cbs_cir & ASO_DSEG_MAN_MASK);
61 MLX5_SET(flow_meter_parameters, fmp, cir_mantissa, val);
62 val = (ebs_eir >> ASO_DSEG_EBS_EXP_OFFSET) & ASO_DSEG_EXP_MASK;
63 MLX5_SET(flow_meter_parameters, fmp, ebs_exponent, val);
64 val = (ebs_eir >> ASO_DSEG_EBS_MAN_OFFSET) & ASO_DSEG_MAN_MASK;
65 MLX5_SET(flow_meter_parameters, fmp, ebs_mantissa, val);
66 mtr_init.next_table = def_policy->sub_policy.tbl_rsc->obj;
67 mtr_init.reg_c_index = priv->mtr_color_reg - REG_C_0;
68 mtr_init.flow_meter_parameter = fmp;
69 mtr_init.flow_meter_parameter_sz =
70 MLX5_ST_SZ_BYTES(flow_meter_parameters);
71 mtr_init.active = fm->active_state;
72 return mlx5_glue->dv_create_flow_action_meter(&mtr_init);
81 * Find meter profile by id.
84 * Pointer to mlx5_priv.
85 * @param meter_profile_id
89 * Pointer to the profile found on success, NULL otherwise.
91 static struct mlx5_flow_meter_profile *
92 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
94 struct mlx5_flow_meter_profile *fmp;
95 union mlx5_l3t_data data;
98 if (mlx5_l3t_get_entry(priv->mtr_profile_tbl,
99 meter_profile_id, &data) || !data.ptr)
102 /* Remove reference taken by the mlx5_l3t_get_entry. */
103 ret = mlx5_l3t_clear_entry(priv->mtr_profile_tbl,
105 if (!ret || ret == -1)
111 * Validate the MTR profile.
114 * Pointer to Ethernet device.
115 * @param[in] meter_profile_id
118 * Pointer to meter profile detail.
120 * Pointer to the error structure.
123 * 0 on success, a negative errno value otherwise and rte_errno is set.
126 mlx5_flow_meter_profile_validate(struct rte_eth_dev *dev,
127 uint32_t meter_profile_id,
128 struct rte_mtr_meter_profile *profile,
129 struct rte_mtr_error *error)
131 struct mlx5_priv *priv = dev->data->dev_private;
132 struct mlx5_flow_meter_profile *fmp;
134 /* Profile must not be NULL. */
136 return -rte_mtr_error_set(error, EINVAL,
137 RTE_MTR_ERROR_TYPE_METER_PROFILE,
138 NULL, "Meter profile is null.");
139 /* Meter profile ID must be valid. */
140 if (meter_profile_id == UINT32_MAX)
141 return -rte_mtr_error_set(error, EINVAL,
142 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
143 NULL, "Meter profile id not valid.");
144 /* Meter profile must not exist. */
145 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
147 return -rte_mtr_error_set(error, EEXIST,
148 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
150 "Meter profile already exists.");
151 if (profile->alg == RTE_MTR_SRTCM_RFC2697) {
152 if (priv->config.hca_attr.qos.flow_meter_old) {
153 /* Verify support for flow meter parameters. */
154 if (priv->sh->meter_aso_en && profile->packet_mode) {
155 if (profile->srtcm_rfc2697.cir > 0 &&
156 (profile->srtcm_rfc2697.cir <<
157 MLX5_MTRS_PPS_MAP_BPS_SHIFT)
158 <= MLX5_SRTCM_CIR_MAX &&
159 profile->srtcm_rfc2697.cbs > 0 &&
160 (profile->srtcm_rfc2697.cbs <<
161 MLX5_MTRS_PPS_MAP_BPS_SHIFT)
162 <= MLX5_SRTCM_CBS_MAX &&
163 (profile->srtcm_rfc2697.ebs <<
164 MLX5_MTRS_PPS_MAP_BPS_SHIFT)
165 <= MLX5_SRTCM_EBS_MAX)
167 return -rte_mtr_error_set
169 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
171 profile->srtcm_rfc2697.ebs ?
172 "Metering value ebs must be 0." :
173 "Invalid metering parameters.");
175 if (profile->srtcm_rfc2697.cir > 0 &&
176 profile->srtcm_rfc2697.cir <=
177 MLX5_SRTCM_CIR_MAX &&
178 profile->srtcm_rfc2697.cbs > 0 &&
179 profile->srtcm_rfc2697.cbs <=
180 MLX5_SRTCM_CBS_MAX &&
181 profile->srtcm_rfc2697.ebs <=
184 return -rte_mtr_error_set(error, ENOTSUP,
185 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
187 profile->srtcm_rfc2697.ebs ?
188 "Metering value ebs must be 0." :
189 "Invalid metering parameters.");
192 return -rte_mtr_error_set(error, ENOTSUP,
193 RTE_MTR_ERROR_TYPE_METER_PROFILE,
194 NULL, "Metering algorithm not supported.");
198 * Calculate mantissa and exponent for cir.
201 * Value to be calculated.
203 * Pointer to the mantissa.
205 * Pointer to the exp.
208 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
211 int64_t delta = INT64_MAX;
216 for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
217 for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
218 _cir = (1000000000ULL * m) >> e;
219 if (llabs(cir - _cir) <= delta) {
220 delta = llabs(cir - _cir);
231 * Calculate mantissa and exponent for xbs.
234 * Value to be calculated.
236 * Pointer to the mantissa.
238 * Pointer to the exp.
241 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
246 /* Special case xbs == 0 ? both exp and matissa are 0. */
252 /* xbs = xbs_mantissa * 2^xbs_exponent */
253 _man = frexp(xbs, &_exp);
254 _man = _man * pow(2, MLX5_MAN_WIDTH);
255 _exp = _exp - MLX5_MAN_WIDTH;
256 *man = (uint8_t)ceil(_man);
261 * Fill the prm meter parameter.
264 * Pointer to meter profie to be converted.
266 * Pointer to the error structure.
269 * 0 on success, a negative errno value otherwise and rte_errno is set.
272 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
273 struct mlx5_priv *priv, struct rte_mtr_error *error)
275 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
277 uint32_t cbs_exp, cbs_man, cir_exp, cir_man;
278 uint32_t ebs_exp, ebs_man;
279 uint64_t cir, cbs, ebs;
281 if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
282 return -rte_mtr_error_set(error, ENOTSUP,
283 RTE_MTR_ERROR_TYPE_METER_PROFILE,
284 NULL, "Metering algorithm not supported.");
285 if (!priv->sh->meter_aso_en && fmp->profile.packet_mode)
286 return -rte_mtr_error_set(error, ENOTSUP,
287 RTE_MTR_ERROR_TYPE_METER_PROFILE,
288 NULL, "Metering algorithm packet mode not supported.");
289 if (priv->sh->meter_aso_en && fmp->profile.packet_mode) {
290 cir = fmp->profile.srtcm_rfc2697.cir <<
291 MLX5_MTRS_PPS_MAP_BPS_SHIFT;
292 cbs = fmp->profile.srtcm_rfc2697.cbs <<
293 MLX5_MTRS_PPS_MAP_BPS_SHIFT;
294 ebs = fmp->profile.srtcm_rfc2697.ebs <<
295 MLX5_MTRS_PPS_MAP_BPS_SHIFT;
297 cir = fmp->profile.srtcm_rfc2697.cir;
298 cbs = fmp->profile.srtcm_rfc2697.cbs;
299 ebs = fmp->profile.srtcm_rfc2697.ebs;
301 /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
302 mlx5_flow_meter_cir_man_exp_calc(cir, &man, &exp);
303 /* Check if cir mantissa is too large. */
304 if (exp > ASO_DSEG_CIR_EXP_MASK)
305 return -rte_mtr_error_set(error, ENOTSUP,
306 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
307 "meter profile parameter cir is"
311 /* cbs = cbs_mantissa * 2^cbs_exponent */
312 mlx5_flow_meter_xbs_man_exp_calc(cbs, &man, &exp);
313 /* Check if cbs mantissa is too large. */
314 if (exp > ASO_DSEG_EXP_MASK)
315 return -rte_mtr_error_set(error, ENOTSUP,
316 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
317 "meter profile parameter cbs is"
321 srtcm->cbs_cir = rte_cpu_to_be_32(cbs_exp << ASO_DSEG_CBS_EXP_OFFSET |
322 cbs_man << ASO_DSEG_CBS_MAN_OFFSET |
323 cir_exp << ASO_DSEG_CIR_EXP_OFFSET |
325 mlx5_flow_meter_xbs_man_exp_calc(ebs, &man, &exp);
326 /* Check if ebs mantissa is too large. */
327 if (exp > ASO_DSEG_EXP_MASK)
328 return -rte_mtr_error_set(error, ENOTSUP,
329 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
330 "meter profile parameter ebs is"
334 srtcm->ebs_eir = rte_cpu_to_be_32(ebs_exp << ASO_DSEG_EBS_EXP_OFFSET |
335 ebs_man << ASO_DSEG_EBS_MAN_OFFSET);
340 * Callback to get MTR capabilities.
343 * Pointer to Ethernet device.
345 * Pointer to save MTR capabilities.
347 * Pointer to the error structure.
350 * 0 on success, a negative errno value otherwise and rte_errno is set.
353 mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
354 struct rte_mtr_capabilities *cap,
355 struct rte_mtr_error *error __rte_unused)
357 struct mlx5_priv *priv = dev->data->dev_private;
358 struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
361 return -rte_mtr_error_set(error, ENOTSUP,
362 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
363 "Meter is not supported");
364 memset(cap, 0, sizeof(*cap));
365 if (priv->sh->meter_aso_en) {
366 /* 2 meters per one ASO cache line. */
367 cap->n_max = 1 << (qattr->log_max_num_meter_aso + 1);
368 cap->srtcm_rfc2697_packet_mode_supported = 1;
370 cap->n_max = 1 << qattr->log_max_flow_meter;
371 cap->srtcm_rfc2697_packet_mode_supported = 0;
373 cap->srtcm_rfc2697_byte_mode_supported = 1;
374 cap->n_shared_max = cap->n_max;
376 cap->shared_identical = 1;
377 cap->shared_n_flows_per_mtr_max = 4 << 20;
378 /* 2M flows can share the same meter. */
379 cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
380 cap->meter_srtcm_rfc2697_n_max = qattr->flow_meter_old ? cap->n_max : 0;
381 cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
382 cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
383 RTE_MTR_STATS_N_PKTS_DROPPED;
388 * Callback to add MTR profile.
391 * Pointer to Ethernet device.
392 * @param[in] meter_profile_id
395 * Pointer to meter profile detail.
397 * Pointer to the error structure.
400 * 0 on success, a negative errno value otherwise and rte_errno is set.
403 mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
404 uint32_t meter_profile_id,
405 struct rte_mtr_meter_profile *profile,
406 struct rte_mtr_error *error)
408 struct mlx5_priv *priv = dev->data->dev_private;
409 struct mlx5_flow_meter_profile *fmp;
410 union mlx5_l3t_data data;
414 return -rte_mtr_error_set(error, ENOTSUP,
415 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
416 "Meter is not supported");
417 /* Check input params. */
418 ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
422 /* Meter profile memory allocation. */
423 fmp = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_flow_meter_profile),
424 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
426 return -rte_mtr_error_set(error, ENOMEM,
427 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
428 NULL, "Meter profile memory "
430 /* Fill profile info. */
431 fmp->id = meter_profile_id;
432 fmp->profile = *profile;
433 /* Fill the flow meter parameters for the PRM. */
434 ret = mlx5_flow_meter_param_fill(fmp, priv, error);
438 ret = mlx5_l3t_set_entry(priv->mtr_profile_tbl,
439 meter_profile_id, &data);
441 return -rte_mtr_error_set(error, ENOTSUP,
442 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
443 NULL, "Meter profile insert fail.");
451 * Callback to delete MTR profile.
454 * Pointer to Ethernet device.
455 * @param[in] meter_profile_id
458 * Pointer to the error structure.
461 * 0 on success, a negative errno value otherwise and rte_errno is set.
464 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
465 uint32_t meter_profile_id,
466 struct rte_mtr_error *error)
468 struct mlx5_priv *priv = dev->data->dev_private;
469 struct mlx5_flow_meter_profile *fmp;
472 return -rte_mtr_error_set(error, ENOTSUP,
473 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
474 "Meter is not supported");
475 /* Meter profile must exist. */
476 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
478 return -rte_mtr_error_set(error, ENOENT,
479 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
481 "Meter profile id is invalid.");
482 /* Check profile is unused. */
484 return -rte_mtr_error_set(error, EBUSY,
485 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
486 NULL, "Meter profile is in use.");
487 if (mlx5_l3t_clear_entry(priv->mtr_profile_tbl, meter_profile_id))
488 return -rte_mtr_error_set(error, EBUSY,
489 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
490 NULL, "Meter profile remove fail.");
499 * Pointer to Ethernet device.
504 * Pointer to the policy found on success, NULL otherwise.
506 struct mlx5_flow_meter_policy *
507 mlx5_flow_meter_policy_find(struct rte_eth_dev *dev,
509 uint32_t *policy_idx)
511 struct mlx5_priv *priv = dev->data->dev_private;
512 struct mlx5_flow_meter_sub_policy *sub_policy = NULL;
513 union mlx5_l3t_data data;
515 if (policy_id > MLX5_MAX_SUB_POLICY_TBL_NUM || !priv->policy_idx_tbl)
517 if (mlx5_l3t_get_entry(priv->policy_idx_tbl, policy_id, &data) ||
521 *policy_idx = data.dword;
522 sub_policy = mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
524 /* Remove reference taken by the mlx5_l3t_get_entry. */
525 mlx5_l3t_clear_entry(priv->policy_idx_tbl, policy_id);
527 if (sub_policy->main_policy_id)
528 return sub_policy->main_policy;
533 * Get the last meter's policy from one meter's policy in hierarchy.
536 * Pointer to Ethernet device.
538 * Pointer to flow meter policy.
541 * Pointer to the final meter's policy, or NULL when fail.
543 struct mlx5_flow_meter_policy *
544 mlx5_flow_meter_hierarchy_get_final_policy(struct rte_eth_dev *dev,
545 struct mlx5_flow_meter_policy *policy)
547 struct mlx5_priv *priv = dev->data->dev_private;
548 struct mlx5_flow_meter_info *next_fm;
549 struct mlx5_flow_meter_policy *next_policy = policy;
551 while (next_policy->is_hierarchy) {
552 next_fm = mlx5_flow_meter_find(priv,
553 next_policy->act_cnt[RTE_COLOR_GREEN].next_mtr_id, NULL);
554 if (!next_fm || next_fm->def_policy)
556 next_policy = mlx5_flow_meter_policy_find(dev,
557 next_fm->policy_id, NULL);
558 MLX5_ASSERT(next_policy);
564 * Callback to check MTR policy action validate
567 * Pointer to Ethernet device.
569 * Pointer to meter policy action detail.
571 * Pointer to the error structure.
574 * 0 on success, a negative errno value otherwise and rte_errno is set.
577 mlx5_flow_meter_policy_validate(struct rte_eth_dev *dev,
578 struct rte_mtr_meter_policy_params *policy,
579 struct rte_mtr_error *error)
581 struct mlx5_priv *priv = dev->data->dev_private;
582 struct rte_flow_attr attr = { .transfer =
583 priv->config.dv_esw_en ? 1 : 0};
585 bool is_def_policy = false;
586 uint8_t domain_bitmap;
589 if (!priv->mtr_en || !priv->sh->meter_aso_en)
590 return -rte_mtr_error_set(error, ENOTSUP,
591 RTE_MTR_ERROR_TYPE_METER_POLICY,
592 NULL, "meter policy unsupported.");
593 ret = mlx5_flow_validate_mtr_acts(dev, policy->actions, &attr,
594 &is_rss, &domain_bitmap, &is_def_policy, error);
601 __mlx5_flow_meter_policy_delete(struct rte_eth_dev *dev,
603 struct mlx5_flow_meter_policy *mtr_policy,
604 struct rte_mtr_error *error,
607 struct mlx5_priv *priv = dev->data->dev_private;
608 struct mlx5_flow_meter_sub_policy *sub_policy;
610 uint16_t sub_policy_num;
612 rte_spinlock_lock(&mtr_policy->sl);
613 if (mtr_policy->ref_cnt) {
614 rte_spinlock_unlock(&mtr_policy->sl);
615 return -rte_mtr_error_set(error, EBUSY,
616 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
618 "Meter policy object is being used.");
620 mlx5_flow_destroy_policy_rules(dev, mtr_policy);
621 mlx5_flow_destroy_mtr_acts(dev, mtr_policy);
622 for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
623 sub_policy_num = (mtr_policy->sub_policy_num >>
624 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
625 MLX5_MTR_SUB_POLICY_NUM_MASK;
626 if (sub_policy_num) {
627 for (j = 0; j < sub_policy_num; j++) {
628 sub_policy = mtr_policy->sub_policys[i][j];
631 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
636 if (priv->policy_idx_tbl && clear_l3t) {
637 if (mlx5_l3t_clear_entry(priv->policy_idx_tbl, policy_id)) {
638 rte_spinlock_unlock(&mtr_policy->sl);
639 return -rte_mtr_error_set(error, ENOTSUP,
640 RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
641 "Fail to delete policy in index table.");
644 rte_spinlock_unlock(&mtr_policy->sl);
649 * Callback to add MTR policy.
652 * Pointer to Ethernet device.
653 * @param[out] policy_id
654 * Pointer to policy id
656 * Pointer to meter policy action detail.
658 * Pointer to the error structure.
661 * 0 on success, a negative errno value otherwise and rte_errno is set.
664 mlx5_flow_meter_policy_add(struct rte_eth_dev *dev,
666 struct rte_mtr_meter_policy_params *policy,
667 struct rte_mtr_error *error)
669 struct mlx5_priv *priv = dev->data->dev_private;
670 struct rte_flow_attr attr = { .transfer =
671 priv->config.dv_esw_en ? 1 : 0};
672 uint32_t sub_policy_idx = 0;
673 uint32_t policy_idx = 0;
674 struct mlx5_flow_meter_policy *mtr_policy = NULL;
675 struct mlx5_flow_meter_sub_policy *sub_policy;
677 bool is_def_policy = false;
680 uint32_t policy_size = sizeof(struct mlx5_flow_meter_policy);
681 uint16_t sub_policy_num;
682 uint8_t domain_bitmap = 0;
683 union mlx5_l3t_data data;
684 bool skip_rule = false;
687 return -rte_mtr_error_set(error, ENOTSUP,
688 RTE_MTR_ERROR_TYPE_METER_POLICY,
689 NULL, "meter policy unsupported.");
690 if (policy_id == MLX5_INVALID_POLICY_ID)
691 return -rte_mtr_error_set(error, ENOTSUP,
692 RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
693 "policy ID is invalid. ");
694 if (policy_id == priv->sh->mtrmng->def_policy_id)
695 return -rte_mtr_error_set(error, EEXIST,
696 RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
697 "policy ID exists. ");
698 mtr_policy = mlx5_flow_meter_policy_find(dev, policy_id,
701 return -rte_mtr_error_set(error, EEXIST,
702 RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
703 "policy ID exists. ");
704 ret = mlx5_flow_validate_mtr_acts(dev, policy->actions, &attr,
705 &is_rss, &domain_bitmap, &is_def_policy, error);
709 return -rte_mtr_error_set(error, ENOTSUP,
710 RTE_MTR_ERROR_TYPE_METER_POLICY,
711 NULL, "fail to find policy domain.");
713 if (priv->sh->mtrmng->def_policy_id != MLX5_INVALID_POLICY_ID)
714 return -rte_mtr_error_set(error, EEXIST,
715 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
716 NULL, "a policy with similar actions "
717 "is already configured");
718 if (mlx5_flow_create_def_policy(dev))
719 return -rte_mtr_error_set(error, ENOTSUP,
720 RTE_MTR_ERROR_TYPE_METER_POLICY,
722 "fail to create non-terminated policy.");
723 priv->sh->mtrmng->def_policy_id = policy_id;
726 if (!priv->sh->meter_aso_en)
727 return -rte_mtr_error_set(error, ENOTSUP,
728 RTE_MTR_ERROR_TYPE_METER_POLICY, NULL,
729 "no ASO capability to support the policy ");
730 for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
731 if (!(domain_bitmap & (1 << i)))
735 sizeof(struct mlx5_flow_meter_sub_policy *) *
736 MLX5_MTR_RSS_MAX_SUB_POLICY;
739 policy_size += sizeof(struct mlx5_flow_meter_sub_policy *);
741 mtr_policy = mlx5_malloc(MLX5_MEM_ZERO, policy_size,
742 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
744 return -rte_mtr_error_set(error, ENOMEM,
745 RTE_MTR_ERROR_TYPE_METER_POLICY, NULL,
746 "Memory alloc failed for meter policy.");
747 policy_size = sizeof(struct mlx5_flow_meter_policy);
748 for (i = 0; i < MLX5_MTR_DOMAIN_MAX; i++) {
749 if (!(domain_bitmap & (1 << i)))
751 if (i == MLX5_MTR_DOMAIN_INGRESS)
752 mtr_policy->ingress = 1;
753 if (i == MLX5_MTR_DOMAIN_EGRESS)
754 mtr_policy->egress = 1;
755 if (i == MLX5_MTR_DOMAIN_TRANSFER)
756 mtr_policy->transfer = 1;
757 sub_policy = mlx5_ipool_zmalloc
758 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
762 if (sub_policy_idx > MLX5_MAX_SUB_POLICY_TBL_NUM)
764 sub_policy->idx = sub_policy_idx;
765 sub_policy->main_policy = mtr_policy;
767 policy_idx = sub_policy_idx;
768 sub_policy->main_policy_id = 1;
770 mtr_policy->sub_policys[i] =
771 (struct mlx5_flow_meter_sub_policy **)
772 ((uint8_t *)mtr_policy + policy_size);
773 mtr_policy->sub_policys[i][0] = sub_policy;
774 sub_policy_num = (mtr_policy->sub_policy_num >>
775 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i)) &
776 MLX5_MTR_SUB_POLICY_NUM_MASK;
778 mtr_policy->sub_policy_num &= ~(MLX5_MTR_SUB_POLICY_NUM_MASK <<
779 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i));
780 mtr_policy->sub_policy_num |=
781 (sub_policy_num & MLX5_MTR_SUB_POLICY_NUM_MASK) <<
782 (MLX5_MTR_SUB_POLICY_NUM_SHIFT * i);
784 mtr_policy->is_rss = 1;
787 policy_size += sizeof(struct mlx5_flow_meter_sub_policy *);
789 rte_spinlock_init(&mtr_policy->sl);
790 ret = mlx5_flow_create_mtr_acts(dev, mtr_policy,
791 policy->actions, error);
794 if (mtr_policy->is_hierarchy) {
795 struct mlx5_flow_meter_policy *final_policy;
798 mlx5_flow_meter_hierarchy_get_final_policy(dev, mtr_policy);
801 skip_rule = (final_policy->is_rss || final_policy->is_queue);
803 if (!is_rss && !mtr_policy->is_queue && !skip_rule) {
804 /* Create policy rules in HW. */
805 ret = mlx5_flow_create_policy_rules(dev, mtr_policy);
809 data.dword = policy_idx;
810 if (!priv->policy_idx_tbl) {
811 priv->policy_idx_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_DWORD);
812 if (!priv->policy_idx_tbl)
815 if (mlx5_l3t_set_entry(priv->policy_idx_tbl, policy_id, &data))
820 ret = __mlx5_flow_meter_policy_delete(dev, policy_id,
821 mtr_policy, error, false);
822 mlx5_free(mtr_policy);
826 return -rte_mtr_error_set(error, ENOTSUP,
827 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
828 NULL, "Failed to create devx policy.");
832 * Callback to delete MTR policy.
835 * Pointer to Ethernet device.
836 * @param[in] policy_id
839 * Pointer to the error structure.
842 * 0 on success, a negative errno value otherwise and rte_errno is set.
845 mlx5_flow_meter_policy_delete(struct rte_eth_dev *dev,
847 struct rte_mtr_error *error)
849 struct mlx5_priv *priv = dev->data->dev_private;
850 struct mlx5_flow_meter_policy *mtr_policy;
854 if (policy_id == priv->sh->mtrmng->def_policy_id) {
855 if (priv->sh->mtrmng->def_policy_ref_cnt > 0)
856 return -rte_mtr_error_set(error, ENOTSUP,
857 RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
858 "Meter policy object is being used.");
859 priv->sh->mtrmng->def_policy_id = MLX5_INVALID_POLICY_ID;
862 mtr_policy = mlx5_flow_meter_policy_find(dev, policy_id, &policy_idx);
864 return -rte_mtr_error_set(error, ENOTSUP,
865 RTE_MTR_ERROR_TYPE_METER_POLICY_ID, NULL,
866 "Meter policy id is invalid. ");
867 ret = __mlx5_flow_meter_policy_delete(dev, policy_id, mtr_policy,
871 mlx5_free(mtr_policy);
876 * Check meter validation.
879 * Pointer to mlx5 private data structure.
880 * @param[in] meter_id
883 * Pointer to rte meter parameters.
885 * Pointer to rte meter error structure.
888 * 0 on success, a negative errno value otherwise and rte_errno is set.
891 mlx5_flow_meter_validate(struct mlx5_priv *priv, uint32_t meter_id,
892 struct rte_mtr_params *params,
893 struct rte_mtr_error *error)
895 /* Meter must use global drop action. */
896 if (!priv->sh->dr_drop_action)
897 return -rte_mtr_error_set(error, ENOTSUP,
898 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
900 "No drop action ready for meter.");
901 /* Meter params must not be NULL. */
903 return -rte_mtr_error_set(error, EINVAL,
904 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
905 NULL, "Meter object params null.");
906 /* Previous meter color is not supported. */
907 if (params->use_prev_mtr_color)
908 return -rte_mtr_error_set(error, ENOTSUP,
909 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
911 "Previous meter color "
913 if (params->meter_policy_id == MLX5_INVALID_POLICY_ID)
914 return -rte_mtr_error_set(error, ENOENT,
915 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
916 NULL, "Meter policy id not valid.");
917 /* Validate meter id. */
918 if (mlx5_flow_meter_find(priv, meter_id, NULL))
919 return -rte_mtr_error_set(error, EEXIST,
920 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
921 "Meter object already exists.");
926 * Modify the flow meter action.
929 * Pointer to mlx5 private data structure.
931 * Pointer to flow meter to be modified.
933 * Pointer to meter srtcm description parameter.
934 * @param[in] modify_bits
935 * The bit in srtcm to be updated.
936 * @param[in] active_state
937 * The state to be updated.
939 * 0 on success, o negative value otherwise.
942 mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
943 struct mlx5_flow_meter_info *fm,
944 const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
945 uint64_t modify_bits, uint32_t active_state, uint32_t is_enable)
947 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
948 uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
950 struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
952 struct mlx5_aso_mtr *aso_mtr = NULL;
953 uint32_t cbs_cir, ebs_eir, val;
955 if (priv->sh->meter_aso_en) {
956 fm->is_enable = !!is_enable;
957 aso_mtr = container_of(fm, struct mlx5_aso_mtr, fm);
958 ret = mlx5_aso_meter_update_by_wqe(priv->sh, aso_mtr);
961 ret = mlx5_aso_mtr_wait(priv->sh, aso_mtr);
965 /* Fill command parameters. */
966 mod_attr.reg_c_index = priv->mtr_color_reg - REG_C_0;
967 mod_attr.flow_meter_parameter = in;
968 mod_attr.flow_meter_parameter_sz =
969 MLX5_ST_SZ_BYTES(flow_meter_parameters);
970 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
971 mod_attr.active = !!active_state;
975 cbs_cir = rte_be_to_cpu_32(srtcm->cbs_cir);
976 ebs_eir = rte_be_to_cpu_32(srtcm->ebs_eir);
977 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
978 val = (cbs_cir >> ASO_DSEG_CBS_EXP_OFFSET) &
980 MLX5_SET(flow_meter_parameters, attr,
982 val = (cbs_cir >> ASO_DSEG_CBS_MAN_OFFSET) &
984 MLX5_SET(flow_meter_parameters, attr,
987 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
988 val = (cbs_cir >> ASO_DSEG_CIR_EXP_OFFSET) &
990 MLX5_SET(flow_meter_parameters, attr,
992 val = cbs_cir & ASO_DSEG_MAN_MASK;
993 MLX5_SET(flow_meter_parameters, attr,
996 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
997 val = (ebs_eir >> ASO_DSEG_EBS_EXP_OFFSET) &
999 MLX5_SET(flow_meter_parameters, attr,
1001 val = (ebs_eir >> ASO_DSEG_EBS_MAN_OFFSET) &
1003 MLX5_SET(flow_meter_parameters, attr,
1006 /* Apply modifications to meter only if it was created. */
1007 if (fm->meter_action) {
1008 ret = mlx5_glue->dv_modify_flow_action_meter
1009 (fm->meter_action, &mod_attr,
1010 rte_cpu_to_be_64(modify_bits));
1014 /* Update succeedded modify meter parameters. */
1015 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
1016 fm->active_state = !!active_state;
1031 mlx5_flow_meter_stats_enable_update(struct rte_eth_dev *dev,
1032 struct mlx5_flow_meter_info *fm,
1033 uint64_t stats_mask)
1036 (stats_mask & RTE_MTR_STATS_N_BYTES_DROPPED) ? 1 : 0;
1037 fm->pkts_dropped = (stats_mask & RTE_MTR_STATS_N_PKTS_DROPPED) ? 1 : 0;
1038 if (fm->bytes_dropped || fm->pkts_dropped) {
1039 if (!fm->drop_cnt) {
1040 /* Alloc policer counters. */
1041 fm->drop_cnt = mlx5_counter_alloc(dev);
1047 mlx5_counter_free(dev, fm->drop_cnt);
1055 * Create meter rules.
1058 * Pointer to Ethernet device.
1059 * @param[in] meter_id
1062 * Pointer to rte meter parameters.
1064 * Meter shared with other flow or not.
1066 * Pointer to rte meter error structure.
1069 * 0 on success, a negative errno value otherwise and rte_errno is set.
1072 mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
1073 struct rte_mtr_params *params, int shared,
1074 struct rte_mtr_error *error)
1076 struct mlx5_priv *priv = dev->data->dev_private;
1077 struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
1078 struct mlx5_flow_meter_profile *fmp;
1079 struct mlx5_flow_meter_info *fm;
1080 struct mlx5_legacy_flow_meter *legacy_fm;
1081 struct mlx5_flow_meter_policy *mtr_policy = NULL;
1082 struct mlx5_indexed_pool_config flow_ipool_cfg = {
1086 .type = "mlx5_flow_mtr_flow_id_pool",
1088 struct mlx5_aso_mtr *aso_mtr;
1089 uint32_t mtr_idx, policy_idx;
1090 union mlx5_l3t_data data;
1092 uint8_t domain_bitmap;
1093 uint8_t mtr_id_bits;
1094 uint8_t mtr_reg_bits = priv->mtr_reg_share ?
1095 MLX5_MTR_IDLE_BITS_IN_COLOR_REG : MLX5_REG_BITS;
1098 return -rte_mtr_error_set(error, ENOTSUP,
1099 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1100 "Meter is not supported");
1101 /* Validate the parameters. */
1102 ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
1105 /* Meter profile must exist. */
1106 fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
1108 return -rte_mtr_error_set(error, ENOENT,
1109 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1110 NULL, "Meter profile id not valid.");
1111 /* Meter policy must exist. */
1112 if (params->meter_policy_id == priv->sh->mtrmng->def_policy_id) {
1114 (&priv->sh->mtrmng->def_policy_ref_cnt,
1115 1, __ATOMIC_RELAXED);
1116 domain_bitmap = MLX5_MTR_ALL_DOMAIN_BIT;
1117 if (!priv->config.dv_esw_en)
1118 domain_bitmap &= ~MLX5_MTR_DOMAIN_TRANSFER_BIT;
1120 mtr_policy = mlx5_flow_meter_policy_find(dev,
1121 params->meter_policy_id, &policy_idx);
1122 if (!priv->sh->meter_aso_en)
1123 return -rte_mtr_error_set(error, ENOTSUP,
1124 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1125 "Part of the policies cannot be "
1126 "supported without ASO ");
1128 return -rte_mtr_error_set(error, ENOENT,
1129 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
1130 NULL, "Meter policy id not valid.");
1131 domain_bitmap = (mtr_policy->ingress ?
1132 MLX5_MTR_DOMAIN_INGRESS_BIT : 0) |
1133 (mtr_policy->egress ?
1134 MLX5_MTR_DOMAIN_EGRESS_BIT : 0) |
1135 (mtr_policy->transfer ?
1136 MLX5_MTR_DOMAIN_TRANSFER_BIT : 0);
1138 /* Allocate the flow meter memory. */
1139 if (priv->sh->meter_aso_en) {
1140 mtr_idx = mlx5_flow_mtr_alloc(dev);
1142 return -rte_mtr_error_set(error, ENOMEM,
1143 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1144 "Memory alloc failed for meter.");
1145 aso_mtr = mlx5_aso_meter_by_idx(priv, mtr_idx);
1148 legacy_fm = mlx5_ipool_zmalloc
1149 (priv->sh->ipool[MLX5_IPOOL_MTR], &mtr_idx);
1150 if (legacy_fm == NULL)
1151 return -rte_mtr_error_set(error, ENOMEM,
1152 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1153 "Memory alloc failed for meter.");
1154 legacy_fm->idx = mtr_idx;
1155 fm = &legacy_fm->fm;
1157 mtr_id_bits = MLX5_REG_BITS - __builtin_clz(mtr_idx);
1158 if ((mtr_id_bits + priv->sh->mtrmng->max_mtr_flow_bits) >
1160 DRV_LOG(ERR, "Meter number exceeds max limit.");
1163 if (mtr_id_bits > priv->sh->mtrmng->max_mtr_bits)
1164 priv->sh->mtrmng->max_mtr_bits = mtr_id_bits;
1165 /* Fill the flow meter parameters. */
1166 fm->meter_id = meter_id;
1167 fm->policy_id = params->meter_policy_id;
1169 if (mlx5_flow_meter_stats_enable_update(dev, fm, params->stats_mask))
1171 if (mlx5_flow_create_mtr_tbls(dev, fm, mtr_idx, domain_bitmap))
1173 /* Add to the flow meter list. */
1174 if (!priv->sh->meter_aso_en)
1175 TAILQ_INSERT_TAIL(fms, legacy_fm, next);
1176 /* Add to the flow meter list. */
1177 fm->active_state = 1; /* Config meter starts as active. */
1179 fm->shared = !!shared;
1180 __atomic_add_fetch(&fm->profile->ref_cnt, 1, __ATOMIC_RELAXED);
1181 if (params->meter_policy_id == priv->sh->mtrmng->def_policy_id) {
1183 fm->flow_ipool = mlx5_ipool_create(&flow_ipool_cfg);
1184 if (!fm->flow_ipool)
1187 rte_spinlock_init(&fm->sl);
1188 /* If ASO meter supported, update ASO flow meter by wqe. */
1189 if (priv->sh->meter_aso_en) {
1190 aso_mtr = container_of(fm, struct mlx5_aso_mtr, fm);
1191 ret = mlx5_aso_meter_update_by_wqe(priv->sh, aso_mtr);
1194 if (!priv->mtr_idx_tbl) {
1196 mlx5_l3t_create(MLX5_L3T_TYPE_DWORD);
1197 if (!priv->mtr_idx_tbl)
1200 data.dword = mtr_idx;
1201 if (mlx5_l3t_set_entry(priv->mtr_idx_tbl, meter_id, &data))
1205 __atomic_add_fetch(&mtr_policy->ref_cnt, 1, __ATOMIC_RELAXED);
1208 mlx5_flow_destroy_mtr_tbls(dev, fm);
1209 /* Free policer counters. */
1211 mlx5_counter_free(dev, fm->drop_cnt);
1212 if (priv->sh->meter_aso_en)
1213 mlx5_flow_mtr_free(dev, mtr_idx);
1215 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], mtr_idx);
1216 return -rte_mtr_error_set(error, ENOTSUP,
1217 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1218 NULL, "Failed to create devx meter.");
1222 mlx5_flow_meter_params_flush(struct rte_eth_dev *dev,
1223 struct mlx5_flow_meter_info *fm,
1226 struct mlx5_priv *priv = dev->data->dev_private;
1227 struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
1228 struct mlx5_flow_meter_profile *fmp;
1229 struct mlx5_legacy_flow_meter *legacy_fm = NULL;
1230 struct mlx5_flow_meter_policy *mtr_policy;
1232 /* Meter object must not have any owner. */
1233 MLX5_ASSERT(!fm->ref_cnt);
1234 /* Get meter profile. */
1238 /* Update dependencies. */
1239 __atomic_sub_fetch(&fmp->ref_cnt, 1, __ATOMIC_RELAXED);
1241 /* Remove from list. */
1242 if (!priv->sh->meter_aso_en) {
1243 legacy_fm = container_of(fm,
1244 struct mlx5_legacy_flow_meter, fm);
1245 TAILQ_REMOVE(fms, legacy_fm, next);
1247 /* Free drop counters. */
1249 mlx5_counter_free(dev, fm->drop_cnt);
1250 /* Free meter flow table. */
1251 if (fm->flow_ipool) {
1252 mlx5_ipool_destroy(fm->flow_ipool);
1255 mlx5_flow_destroy_mtr_tbls(dev, fm);
1257 __atomic_sub_fetch(&priv->sh->mtrmng->def_policy_ref_cnt,
1258 1, __ATOMIC_RELAXED);
1259 if (priv->sh->meter_aso_en) {
1260 if (!fm->def_policy) {
1261 mtr_policy = mlx5_flow_meter_policy_find(dev,
1262 fm->policy_id, NULL);
1264 __atomic_sub_fetch(&mtr_policy->ref_cnt,
1265 1, __ATOMIC_RELAXED);
1269 if (mlx5_l3t_clear_entry(priv->mtr_idx_tbl, fm->meter_id))
1271 mlx5_flow_mtr_free(dev, mtr_idx);
1273 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR],
1280 * Destroy meter rules.
1283 * Pointer to Ethernet device.
1284 * @param[in] meter_id
1287 * Pointer to rte meter error structure.
1290 * 0 on success, a negative errno value otherwise and rte_errno is set.
1293 mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
1294 struct rte_mtr_error *error)
1296 struct mlx5_priv *priv = dev->data->dev_private;
1297 struct mlx5_flow_meter_info *fm;
1298 uint32_t mtr_idx = 0;
1301 return -rte_mtr_error_set(error, ENOTSUP,
1302 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1304 "Meter is not supported");
1305 /* Meter object must exist. */
1306 fm = mlx5_flow_meter_find(priv, meter_id, &mtr_idx);
1308 return -rte_mtr_error_set(error, ENOENT,
1309 RTE_MTR_ERROR_TYPE_MTR_ID,
1311 "Meter object id not valid.");
1312 /* Meter object must not have any owner. */
1313 if (fm->ref_cnt > 0)
1314 return -rte_mtr_error_set(error, EBUSY,
1315 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
1317 "Meter object is being used.");
1318 /* Destroy the meter profile. */
1319 if (mlx5_flow_meter_params_flush(dev, fm, mtr_idx))
1320 return -rte_mtr_error_set(error, EINVAL,
1321 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1323 "MTR object meter profile invalid.");
1328 * Modify meter state.
1331 * Pointer to mlx5 private data structure.
1333 * Pointer to flow meter.
1334 * @param[in] new_state
1335 * New state to update.
1337 * Pointer to rte meter error structure.
1340 * 0 on success, a negative errno value otherwise and rte_errno is set.
1343 mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
1344 struct mlx5_flow_meter_info *fm,
1346 struct rte_mtr_error *error)
1348 static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
1349 .cbs_cir = RTE_BE32(MLX5_IFC_FLOW_METER_DISABLE_CBS_CIR_VAL),
1352 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
1353 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
1356 if (new_state == MLX5_FLOW_METER_DISABLE)
1357 ret = mlx5_flow_meter_action_modify(priv, fm,
1358 &srtcm, modify_bits, 0, 0);
1360 ret = mlx5_flow_meter_action_modify(priv, fm,
1361 &fm->profile->srtcm_prm,
1364 return -rte_mtr_error_set(error, -ret,
1365 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
1368 "Failed to enable meter." :
1369 "Failed to disable meter.");
1374 * Callback to enable flow meter.
1377 * Pointer to Ethernet device.
1378 * @param[in] meter_id
1381 * Pointer to rte meter error structure.
1384 * 0 on success, a negative errno value otherwise and rte_errno is set.
1387 mlx5_flow_meter_enable(struct rte_eth_dev *dev,
1389 struct rte_mtr_error *error)
1391 struct mlx5_priv *priv = dev->data->dev_private;
1392 struct mlx5_flow_meter_info *fm;
1396 return -rte_mtr_error_set(error, ENOTSUP,
1397 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1398 "Meter is not supported");
1399 /* Meter object must exist. */
1400 fm = mlx5_flow_meter_find(priv, meter_id, NULL);
1402 return -rte_mtr_error_set(error, ENOENT,
1403 RTE_MTR_ERROR_TYPE_MTR_ID,
1404 NULL, "Meter not found.");
1405 if (fm->active_state == MLX5_FLOW_METER_ENABLE)
1407 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
1410 fm->active_state = MLX5_FLOW_METER_ENABLE;
1415 * Callback to disable flow meter.
1418 * Pointer to Ethernet device.
1419 * @param[in] meter_id
1422 * Pointer to rte meter error structure.
1425 * 0 on success, a negative errno value otherwise and rte_errno is set.
1428 mlx5_flow_meter_disable(struct rte_eth_dev *dev,
1430 struct rte_mtr_error *error)
1432 struct mlx5_priv *priv = dev->data->dev_private;
1433 struct mlx5_flow_meter_info *fm;
1437 return -rte_mtr_error_set(error, ENOTSUP,
1438 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1439 "Meter is not supported");
1440 /* Meter object must exist. */
1441 fm = mlx5_flow_meter_find(priv, meter_id, NULL);
1443 return -rte_mtr_error_set(error, ENOENT,
1444 RTE_MTR_ERROR_TYPE_MTR_ID,
1445 NULL, "Meter not found.");
1446 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
1448 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
1451 fm->active_state = MLX5_FLOW_METER_DISABLE;
1456 * Callback to update meter profile.
1459 * Pointer to Ethernet device.
1460 * @param[in] meter_id
1462 * @param[in] meter_profile_id
1463 * To be updated meter profile id.
1465 * Pointer to rte meter error structure.
1468 * 0 on success, a negative errno value otherwise and rte_errno is set.
1471 mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
1473 uint32_t meter_profile_id,
1474 struct rte_mtr_error *error)
1476 struct mlx5_priv *priv = dev->data->dev_private;
1477 struct mlx5_flow_meter_profile *fmp;
1478 struct mlx5_flow_meter_profile *old_fmp;
1479 struct mlx5_flow_meter_info *fm;
1480 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
1481 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
1485 return -rte_mtr_error_set(error, ENOTSUP,
1486 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1487 "Meter is not supported");
1488 /* Meter profile must exist. */
1489 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
1491 return -rte_mtr_error_set(error, ENOENT,
1492 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1493 NULL, "Meter profile not found.");
1494 /* Meter object must exist. */
1495 fm = mlx5_flow_meter_find(priv, meter_id, NULL);
1497 return -rte_mtr_error_set(error, ENOENT,
1498 RTE_MTR_ERROR_TYPE_MTR_ID,
1499 NULL, "Meter not found.");
1500 /* MTR object already set to meter profile id. */
1501 old_fmp = fm->profile;
1504 /* Update the profile. */
1506 /* Update meter params in HW (if not disabled). */
1507 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
1509 ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
1510 modify_bits, fm->active_state, 1);
1512 fm->profile = old_fmp;
1513 return -rte_mtr_error_set(error, -ret,
1514 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
1515 NULL, "Failed to update meter"
1516 " parmeters in hardware.");
1524 * Callback to update meter stats mask.
1527 * Pointer to Ethernet device.
1528 * @param[in] meter_id
1530 * @param[in] stats_mask
1531 * To be updated stats_mask.
1533 * Pointer to rte meter error structure.
1536 * 0 on success, a negative errno value otherwise and rte_errno is set.
1539 mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
1541 uint64_t stats_mask,
1542 struct rte_mtr_error *error)
1544 struct mlx5_priv *priv = dev->data->dev_private;
1545 struct mlx5_flow_meter_info *fm;
1548 return -rte_mtr_error_set(error, ENOTSUP,
1549 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1550 "Meter is not supported");
1551 /* Meter object must exist. */
1552 fm = mlx5_flow_meter_find(priv, meter_id, NULL);
1554 return -rte_mtr_error_set(error, ENOENT,
1555 RTE_MTR_ERROR_TYPE_MTR_ID,
1556 NULL, "Meter object id not valid.");
1557 if (mlx5_flow_meter_stats_enable_update(dev, fm, stats_mask))
1558 return -rte_mtr_error_set(error, ENOENT,
1559 RTE_MTR_ERROR_TYPE_MTR_ID,
1560 NULL, "Fail to allocate "
1561 "counter for meter.");
1566 * Callback to read meter statistics.
1569 * Pointer to Ethernet device.
1570 * @param[in] meter_id
1573 * Pointer to store the statistics.
1574 * @param[out] stats_mask
1575 * Pointer to store the stats_mask.
1577 * Statistic to be cleared after read or not.
1579 * Pointer to rte meter error structure.
1582 * 0 on success, a negative errno value otherwise and rte_errno is set.
1585 mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
1587 struct rte_mtr_stats *stats,
1588 uint64_t *stats_mask,
1590 struct rte_mtr_error *error)
1592 struct mlx5_priv *priv = dev->data->dev_private;
1593 struct mlx5_flow_meter_info *fm;
1599 return -rte_mtr_error_set(error, ENOTSUP,
1600 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1601 "Meter is not supported");
1602 /* Meter object must exist. */
1603 fm = mlx5_flow_meter_find(priv, meter_id, NULL);
1605 return -rte_mtr_error_set(error, ENOENT,
1606 RTE_MTR_ERROR_TYPE_MTR_ID,
1607 NULL, "Meter object id not valid.");
1609 if (fm->bytes_dropped)
1610 *stats_mask |= RTE_MTR_STATS_N_BYTES_DROPPED;
1611 if (fm->pkts_dropped)
1612 *stats_mask |= RTE_MTR_STATS_N_PKTS_DROPPED;
1613 memset(stats, 0, sizeof(*stats));
1615 ret = mlx5_counter_query(dev, fm->drop_cnt, clear, &pkts,
1619 /* If need to read the packets, set it. */
1620 if (fm->pkts_dropped)
1621 stats->n_pkts_dropped = pkts;
1622 /* If need to read the bytes, set it. */
1623 if (fm->bytes_dropped)
1624 stats->n_bytes_dropped = bytes;
1628 return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
1629 "Failed to read meter drop counters.");
1632 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
1633 .capabilities_get = mlx5_flow_mtr_cap_get,
1634 .meter_profile_add = mlx5_flow_meter_profile_add,
1635 .meter_profile_delete = mlx5_flow_meter_profile_delete,
1636 .meter_policy_validate = mlx5_flow_meter_policy_validate,
1637 .meter_policy_add = mlx5_flow_meter_policy_add,
1638 .meter_policy_delete = mlx5_flow_meter_policy_delete,
1639 .create = mlx5_flow_meter_create,
1640 .destroy = mlx5_flow_meter_destroy,
1641 .meter_enable = mlx5_flow_meter_enable,
1642 .meter_disable = mlx5_flow_meter_disable,
1643 .meter_profile_update = mlx5_flow_meter_profile_update,
1644 .meter_dscp_table_update = NULL,
1645 .stats_update = mlx5_flow_meter_stats_update,
1646 .stats_read = mlx5_flow_meter_stats_read,
1650 * Get meter operations.
1653 * Pointer to Ethernet device structure.
1655 * Pointer to set the mtr operations.
1661 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
1663 *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
1671 * Pointer to mlx5_priv.
1675 * Pointer to Meter index.
1678 * Pointer to the meter info found on success, NULL otherwise.
1680 struct mlx5_flow_meter_info *
1681 mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id,
1684 struct mlx5_legacy_flow_meter *legacy_fm;
1685 struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
1686 struct mlx5_aso_mtr *aso_mtr;
1687 struct mlx5_aso_mtr_pools_mng *pools_mng =
1688 &priv->sh->mtrmng->pools_mng;
1689 union mlx5_l3t_data data;
1691 if (priv->sh->meter_aso_en) {
1692 rte_spinlock_lock(&pools_mng->mtrsl);
1693 if (!pools_mng->n_valid || !priv->mtr_idx_tbl) {
1694 rte_spinlock_unlock(&pools_mng->mtrsl);
1697 if (mlx5_l3t_get_entry(priv->mtr_idx_tbl, meter_id, &data) ||
1699 rte_spinlock_unlock(&pools_mng->mtrsl);
1703 *mtr_idx = data.dword;
1704 aso_mtr = mlx5_aso_meter_by_idx(priv, data.dword);
1705 /* Remove reference taken by the mlx5_l3t_get_entry. */
1706 mlx5_l3t_clear_entry(priv->mtr_idx_tbl, meter_id);
1707 rte_spinlock_unlock(&pools_mng->mtrsl);
1708 if (!aso_mtr || aso_mtr->state == ASO_METER_FREE)
1710 return &aso_mtr->fm;
1712 TAILQ_FOREACH(legacy_fm, fms, next)
1713 if (meter_id == legacy_fm->fm.meter_id) {
1715 *mtr_idx = legacy_fm->idx;
1716 return &legacy_fm->fm;
1722 * Find meter by index.
1725 * Pointer to mlx5_priv.
1730 * Pointer to the meter info found on success, NULL otherwise.
1732 struct mlx5_flow_meter_info *
1733 flow_dv_meter_find_by_idx(struct mlx5_priv *priv, uint32_t idx)
1735 struct mlx5_aso_mtr *aso_mtr;
1737 if (priv->sh->meter_aso_en) {
1738 aso_mtr = mlx5_aso_meter_by_idx(priv, idx);
1741 return &aso_mtr->fm;
1743 return mlx5_ipool_get(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
1748 * Attach meter to flow.
1749 * Unidirectional Meter creation can only be done
1750 * when flow direction is known, i.e. when calling meter_attach.
1753 * Pointer to mlx5 private data.
1755 * Pointer to flow meter.
1757 * Pointer to flow attributes.
1758 * @param [out] error
1759 * Pointer to error structure.
1762 * 0 on success, a negative errno value otherwise and rte_errno is set.
1765 mlx5_flow_meter_attach(struct mlx5_priv *priv,
1766 struct mlx5_flow_meter_info *fm,
1767 const struct rte_flow_attr *attr,
1768 struct rte_flow_error *error)
1772 if (priv->sh->meter_aso_en) {
1773 struct mlx5_aso_mtr *aso_mtr;
1775 aso_mtr = container_of(fm, struct mlx5_aso_mtr, fm);
1776 if (mlx5_aso_mtr_wait(priv->sh, aso_mtr)) {
1777 return rte_flow_error_set(error, ENOENT,
1778 RTE_FLOW_ERROR_TYPE_UNSPECIFIED,
1780 "Timeout in meter configuration");
1782 rte_spinlock_lock(&fm->sl);
1783 if (fm->shared || !fm->ref_cnt) {
1786 rte_flow_error_set(error, EINVAL,
1787 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1788 "Meter cannot be shared");
1791 rte_spinlock_unlock(&fm->sl);
1793 rte_spinlock_lock(&fm->sl);
1794 if (fm->meter_action) {
1796 attr->transfer == fm->transfer &&
1797 attr->ingress == fm->ingress &&
1798 attr->egress == fm->egress) {
1801 rte_flow_error_set(error, EINVAL,
1802 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1804 "Meter attr not match." :
1805 "Meter cannot be shared.");
1809 fm->ingress = attr->ingress;
1810 fm->egress = attr->egress;
1811 fm->transfer = attr->transfer;
1813 /* This also creates the meter object. */
1814 fm->meter_action = mlx5_flow_meter_action_create(priv,
1816 if (!fm->meter_action) {
1821 rte_flow_error_set(error, EINVAL,
1822 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1823 "Meter action create failed.");
1827 rte_spinlock_unlock(&fm->sl);
1829 return ret ? -rte_errno : 0;
1833 * Detach meter from flow.
1836 * Pointer to mlx5 private data.
1838 * Pointer to flow meter.
1841 mlx5_flow_meter_detach(struct mlx5_priv *priv,
1842 struct mlx5_flow_meter_info *fm)
1844 #ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
1845 rte_spinlock_lock(&fm->sl);
1846 MLX5_ASSERT(fm->ref_cnt);
1847 if (--fm->ref_cnt == 0 && !priv->sh->meter_aso_en) {
1848 mlx5_glue->destroy_flow_action(fm->meter_action);
1849 fm->meter_action = NULL;
1854 rte_spinlock_unlock(&fm->sl);
1862 * Flush meter with Rx queue configuration.
1865 * Pointer to Ethernet device.
1868 mlx5_flow_meter_rxq_flush(struct rte_eth_dev *dev)
1870 struct mlx5_priv *priv = dev->data->dev_private;
1871 struct mlx5_flow_meter_sub_policy *sub_policy;
1872 struct mlx5_flow_meter_policy *mtr_policy;
1874 uint32_t i, policy_idx;
1878 if (priv->policy_idx_tbl) {
1879 MLX5_L3T_FOREACH(priv->policy_idx_tbl, i, entry) {
1880 policy_idx = *(uint32_t *)entry;
1881 sub_policy = mlx5_ipool_get
1882 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
1884 if (!sub_policy || !sub_policy->main_policy)
1886 mtr_policy = sub_policy->main_policy;
1887 if (mtr_policy->is_queue || mtr_policy->is_rss)
1888 mlx5_flow_destroy_sub_policy_with_rxq(dev,
1895 * Iterate a meter hierarchy and flush all meters and policies if possible.
1898 * Pointer to Ethernet device.
1900 * Pointer to flow meter.
1901 * @param[in] mtr_idx
1904 * Pointer to rte meter error structure.
1907 * 0 on success, a negative errno value otherwise and rte_errno is set.
1910 mlx5_flow_meter_flush_hierarchy(struct rte_eth_dev *dev,
1911 struct mlx5_flow_meter_info *fm,
1913 struct rte_mtr_error *error)
1915 struct mlx5_priv *priv = dev->data->dev_private;
1916 struct mlx5_flow_meter_policy *policy;
1918 struct mlx5_flow_meter_info *next_fm;
1919 uint32_t next_mtr_idx;
1920 struct mlx5_flow_meter_policy *next_policy = NULL;
1922 policy = mlx5_flow_meter_policy_find(dev, fm->policy_id, NULL);
1923 MLX5_ASSERT(policy);
1924 while (!fm->ref_cnt && policy->is_hierarchy) {
1925 policy_id = fm->policy_id;
1926 next_fm = mlx5_flow_meter_find(priv,
1927 policy->act_cnt[RTE_COLOR_GREEN].next_mtr_id,
1930 next_policy = mlx5_flow_meter_policy_find(dev,
1933 MLX5_ASSERT(next_policy);
1935 if (mlx5_flow_meter_params_flush(dev, fm, mtr_idx))
1936 return -rte_mtr_error_set(error, ENOTSUP,
1937 RTE_MTR_ERROR_TYPE_MTR_ID,
1939 "Failed to flush meter.");
1940 if (policy->ref_cnt)
1942 if (__mlx5_flow_meter_policy_delete(dev, policy_id,
1943 policy, error, true))
1946 if (!next_fm || !next_policy)
1949 mtr_idx = next_mtr_idx;
1950 policy = next_policy;
1956 * Flush all the hierarchy meters and their policies.
1959 * Pointer to Ethernet device.
1961 * Pointer to rte meter error structure.
1964 * 0 on success, a negative errno value otherwise and rte_errno is set.
1967 mlx5_flow_meter_flush_all_hierarchies(struct rte_eth_dev *dev,
1968 struct rte_mtr_error *error)
1970 struct mlx5_priv *priv = dev->data->dev_private;
1971 struct mlx5_flow_meter_info *fm;
1972 struct mlx5_flow_meter_policy *policy;
1973 struct mlx5_flow_meter_sub_policy *sub_policy;
1974 struct mlx5_flow_meter_info *next_fm;
1975 struct mlx5_aso_mtr *aso_mtr;
1976 uint32_t mtr_idx = 0;
1977 uint32_t i, policy_idx;
1980 if (!priv->mtr_idx_tbl || !priv->policy_idx_tbl)
1982 MLX5_L3T_FOREACH(priv->mtr_idx_tbl, i, entry) {
1983 mtr_idx = *(uint32_t *)entry;
1986 aso_mtr = mlx5_aso_meter_by_idx(priv, mtr_idx);
1988 if (fm->ref_cnt || fm->def_policy)
1990 if (mlx5_flow_meter_flush_hierarchy(dev, fm, mtr_idx, error))
1993 MLX5_L3T_FOREACH(priv->policy_idx_tbl, i, entry) {
1994 policy_idx = *(uint32_t *)entry;
1995 sub_policy = mlx5_ipool_get
1996 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
1999 return -rte_mtr_error_set(error,
2001 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2002 NULL, "Meter policy invalid.");
2003 policy = sub_policy->main_policy;
2004 if (!policy || !policy->is_hierarchy || policy->ref_cnt)
2006 next_fm = mlx5_flow_meter_find(priv,
2007 policy->act_cnt[RTE_COLOR_GREEN].next_mtr_id,
2009 if (__mlx5_flow_meter_policy_delete(dev, i, policy,
2011 return -rte_mtr_error_set(error,
2013 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2014 NULL, "Meter policy invalid.");
2016 if (!next_fm || next_fm->ref_cnt || next_fm->def_policy)
2018 if (mlx5_flow_meter_flush_hierarchy(dev, next_fm,
2025 * Flush meter configuration.
2028 * Pointer to Ethernet device.
2030 * Pointer to rte meter error structure.
2033 * 0 on success, a negative errno value otherwise and rte_errno is set.
2036 mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
2038 struct mlx5_priv *priv = dev->data->dev_private;
2039 struct mlx5_legacy_flow_meters *fms = &priv->flow_meters;
2040 struct mlx5_flow_meter_profile *fmp;
2041 struct mlx5_legacy_flow_meter *legacy_fm;
2042 struct mlx5_flow_meter_info *fm;
2043 struct mlx5_flow_meter_sub_policy *sub_policy;
2045 uint32_t i, mtr_idx, policy_idx;
2047 struct mlx5_aso_mtr *aso_mtr;
2051 if (priv->sh->meter_aso_en) {
2052 if (mlx5_flow_meter_flush_all_hierarchies(dev, error))
2054 if (priv->mtr_idx_tbl) {
2055 MLX5_L3T_FOREACH(priv->mtr_idx_tbl, i, entry) {
2056 mtr_idx = *(uint32_t *)entry;
2059 mlx5_aso_meter_by_idx(priv, mtr_idx);
2061 (void)mlx5_flow_meter_params_flush(dev,
2065 mlx5_l3t_destroy(priv->mtr_idx_tbl);
2066 priv->mtr_idx_tbl = NULL;
2069 TAILQ_FOREACH_SAFE(legacy_fm, fms, next, tmp) {
2070 fm = &legacy_fm->fm;
2071 if (mlx5_flow_meter_params_flush(dev, fm, 0))
2072 return -rte_mtr_error_set(error, EINVAL,
2073 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
2074 NULL, "MTR object meter profile invalid.");
2077 if (priv->policy_idx_tbl) {
2078 MLX5_L3T_FOREACH(priv->policy_idx_tbl, i, entry) {
2079 policy_idx = *(uint32_t *)entry;
2080 sub_policy = mlx5_ipool_get
2081 (priv->sh->ipool[MLX5_IPOOL_MTR_POLICY],
2084 return -rte_mtr_error_set(error,
2086 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2088 "meter policy invalid.");
2089 if (__mlx5_flow_meter_policy_delete(dev, i,
2090 sub_policy->main_policy,
2092 return -rte_mtr_error_set(error,
2094 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2096 "meter policy invalid.");
2097 mlx5_free(sub_policy->main_policy);
2099 mlx5_l3t_destroy(priv->policy_idx_tbl);
2100 priv->policy_idx_tbl = NULL;
2102 if (priv->mtr_profile_tbl) {
2103 MLX5_L3T_FOREACH(priv->mtr_profile_tbl, i, entry) {
2105 if (mlx5_flow_meter_profile_delete(dev, fmp->id,
2107 return -rte_mtr_error_set(error, EINVAL,
2108 RTE_MTR_ERROR_TYPE_METER_POLICY_ID,
2109 NULL, "Fail to destroy "
2112 mlx5_l3t_destroy(priv->mtr_profile_tbl);
2113 priv->mtr_profile_tbl = NULL;
2115 /* Delete default policy table. */
2116 mlx5_flow_destroy_def_policy(dev);
2117 if (priv->sh->refcnt == 1)
2118 mlx5_flow_destroy_mtr_drop_tbls(dev);