1 // SPDX-License-Identifier: BSD-3-Clause
3 * Copyright 2018 Mellanox Technologies, Ltd
7 #include <rte_malloc.h>
9 #include <rte_mtr_driver.h>
12 #include "mlx5_flow.h"
15 * Find meter profile by id.
18 * Pointer to mlx5_priv.
19 * @param meter_profile_id
23 * Pointer to the profile found on success, NULL otherwise.
25 static struct mlx5_flow_meter_profile *
26 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
28 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
29 struct mlx5_flow_meter_profile *fmp;
31 TAILQ_FOREACH(fmp, fmps, next)
32 if (meter_profile_id == fmp->meter_profile_id)
38 * Calculate mantissa and exponent for cir.
41 * Value to be calculated.
43 * Pointer to the mantissa.
48 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
51 int64_t delta = INT64_MAX;
56 for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
57 for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
58 _cir = (1000000000ULL * m) >> e;
59 if (llabs(cir - _cir) <= delta) {
60 delta = llabs(cir - _cir);
71 * Calculate mantissa and exponent for xbs.
74 * Value to be calculated.
76 * Pointer to the mantissa.
81 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
86 /* Special case xbs == 0 ? both exp and matissa are 0. */
92 /* xbs = xbs_mantissa * 2^xbs_exponent */
93 _man = frexp(xbs, &_exp);
94 _man = _man * pow(2, MLX5_MAN_WIDTH);
95 _exp = _exp - MLX5_MAN_WIDTH;
96 *man = (uint8_t)ceil(_man);
101 * Fill the prm meter parameter.
104 * Pointer to meter profie to be converted.
106 * Pointer to the error structure.
109 * 0 on success, a negative errno value otherwise and rte_errno is set.
112 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
113 struct rte_mtr_error *error)
115 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
118 if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
119 return -rte_mtr_error_set(error, ENOTSUP,
120 RTE_MTR_ERROR_TYPE_METER_PROFILE,
121 NULL, "Metering algorithm not supported.");
122 /* cbs = cbs_mantissa * 2^cbs_exponent */
123 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
125 srtcm->cbs_mantissa = man;
126 srtcm->cbs_exponent = exp;
127 /* Check if cbs mantissa is too large. */
128 if (srtcm->cbs_exponent != exp)
129 return -rte_mtr_error_set(error, EINVAL,
130 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
131 "Metering profile parameter cbs is"
133 /* ebs = ebs_mantissa * 2^ebs_exponent */
134 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
136 srtcm->ebs_mantissa = man;
137 srtcm->ebs_exponent = exp;
138 /* Check if ebs mantissa is too large. */
139 if (srtcm->ebs_exponent != exp)
140 return -rte_mtr_error_set(error, EINVAL,
141 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
142 "Metering profile parameter ebs is"
144 /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
145 mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
147 srtcm->cir_mantissa = man;
148 srtcm->cir_exponent = exp;
149 /* Check if cir mantissa is too large. */
150 if (srtcm->cir_exponent != exp)
151 return -rte_mtr_error_set(error, EINVAL,
152 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
153 "Metering profile parameter cir is"
159 * Callback to get MTR capabilities.
162 * Pointer to Ethernet device.
164 * Pointer to save MTR capabilities.
166 * Pointer to the error structure.
169 * 0 on success, a negative errno value otherwise and rte_errno is set.
172 mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
173 struct rte_mtr_capabilities *cap,
174 struct rte_mtr_error *error __rte_unused)
176 struct mlx5_priv *priv = dev->data->dev_private;
177 struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
180 return -rte_mtr_error_set(error, ENOTSUP,
181 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
182 "Meter is not support");
183 memset(cap, 0, sizeof(*cap));
184 cap->n_max = 1 << qattr->log_max_flow_meter;
185 cap->n_shared_max = cap->n_max;
187 cap->shared_identical = 1;
188 cap->shared_n_flows_per_mtr_max = 4 << 20;
189 /* 2M flows can share the same meter. */
190 cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
191 cap->meter_srtcm_rfc2697_n_max = qattr->srtcm_sup ? cap->n_max : 0;
192 cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
193 cap->policer_action_drop_supported = 1;
194 cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
195 RTE_MTR_STATS_N_PKTS_DROPPED;
200 * Callback to add MTR profile.
203 * Pointer to Ethernet device.
204 * @param[in] meter_profile_id
207 * Pointer to meter profile detail.
209 * Pointer to the error structure.
212 * 0 on success, a negative errno value otherwise and rte_errno is set.
215 mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
216 uint32_t meter_profile_id,
217 struct rte_mtr_meter_profile *profile,
218 struct rte_mtr_error *error)
220 struct mlx5_priv *priv = dev->data->dev_private;
221 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
222 struct mlx5_flow_meter_profile *fmp;
226 return -rte_mtr_error_set(error, ENOTSUP,
227 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
228 "Meter is not support");
229 /* Meter profile memory allocation. */
230 fmp = rte_calloc(__func__, 1, sizeof(struct mlx5_flow_meter_profile),
231 RTE_CACHE_LINE_SIZE);
233 return -rte_mtr_error_set(error, ENOMEM,
234 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
235 NULL, "Meter profile memory "
237 /* Fill profile info. */
238 fmp->meter_profile_id = meter_profile_id;
239 fmp->profile = *profile;
240 /* Fill the flow meter parameters for the PRM. */
241 ret = mlx5_flow_meter_param_fill(fmp, error);
245 TAILQ_INSERT_TAIL(fmps, fmp, next);
253 * Callback to delete MTR profile.
256 * Pointer to Ethernet device.
257 * @param[in] meter_profile_id
260 * Pointer to the error structure.
263 * 0 on success, a negative errno value otherwise and rte_errno is set.
266 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
267 uint32_t meter_profile_id,
268 struct rte_mtr_error *error)
270 struct mlx5_priv *priv = dev->data->dev_private;
271 struct mlx5_flow_meter_profile *fmp;
274 return -rte_mtr_error_set(error, ENOTSUP,
275 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
276 "Meter is not support");
277 /* Meter profile must exist. */
278 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
280 return -rte_mtr_error_set(error, ENOENT,
281 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
283 "Meter profile id invalid.");
284 /* Check profile is unused. */
286 return -rte_mtr_error_set(error, EBUSY,
287 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
288 NULL, "Meter profile in use.");
289 /* Remove from list. */
290 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
295 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
296 .capabilities_get = mlx5_flow_mtr_cap_get,
297 .meter_profile_add = mlx5_flow_meter_profile_add,
298 .meter_profile_delete = mlx5_flow_meter_profile_delete,
301 .meter_enable = NULL,
302 .meter_disable = NULL,
303 .meter_profile_update = NULL,
304 .meter_dscp_table_update = NULL,
305 .policer_actions_update = NULL,
306 .stats_update = NULL,
311 * Get meter operations.
314 * Pointer to Ethernet device structure.
316 * Pointer to set the mtr operations.
322 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
324 *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;