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 * Validate the MTR profile.
41 * Pointer to Ethernet device.
42 * @param[in] meter_profile_id
45 * Pointer to meter profile detail.
47 * Pointer to the error structure.
50 * 0 on success, a negative errno value otherwise and rte_errno is set.
53 mlx5_flow_meter_profile_validate(struct rte_eth_dev *dev,
54 uint32_t meter_profile_id,
55 struct rte_mtr_meter_profile *profile,
56 struct rte_mtr_error *error)
58 struct mlx5_priv *priv = dev->data->dev_private;
59 struct mlx5_flow_meter_profile *fmp;
61 /* Profile must not be NULL. */
63 return -rte_mtr_error_set(error, EINVAL,
64 RTE_MTR_ERROR_TYPE_METER_PROFILE,
65 NULL, "Meter profile is null.");
66 /* Meter profile ID must be valid. */
67 if (meter_profile_id == UINT32_MAX)
68 return -rte_mtr_error_set(error, EINVAL,
69 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
70 NULL, "Meter profile id not valid.");
71 /* Meter profile must not exist. */
72 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
74 return -rte_mtr_error_set(error, EEXIST,
75 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
77 "Meter profile already exists.");
78 if (profile->alg == RTE_MTR_SRTCM_RFC2697) {
79 if (priv->config.hca_attr.qos.srtcm_sup) {
80 /* Verify support for flow meter parameters. */
81 if (profile->srtcm_rfc2697.cir > 0 &&
82 profile->srtcm_rfc2697.cir <= MLX5_SRTCM_CIR_MAX &&
83 profile->srtcm_rfc2697.cbs > 0 &&
84 profile->srtcm_rfc2697.cbs <= MLX5_SRTCM_CBS_MAX &&
85 profile->srtcm_rfc2697.ebs <= MLX5_SRTCM_EBS_MAX)
88 return -rte_mtr_error_set
90 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
92 profile->srtcm_rfc2697.ebs ?
93 "Metering value ebs must be 0." :
94 "Invalid metering parameters.");
97 return -rte_mtr_error_set(error, ENOTSUP,
98 RTE_MTR_ERROR_TYPE_METER_PROFILE,
99 NULL, "Metering algorithm not supported.");
103 * Calculate mantissa and exponent for cir.
106 * Value to be calculated.
108 * Pointer to the mantissa.
110 * Pointer to the exp.
113 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
116 int64_t delta = INT64_MAX;
121 for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
122 for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
123 _cir = (1000000000ULL * m) >> e;
124 if (llabs(cir - _cir) <= delta) {
125 delta = llabs(cir - _cir);
136 * Calculate mantissa and exponent for xbs.
139 * Value to be calculated.
141 * Pointer to the mantissa.
143 * Pointer to the exp.
146 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
151 /* Special case xbs == 0 ? both exp and matissa are 0. */
157 /* xbs = xbs_mantissa * 2^xbs_exponent */
158 _man = frexp(xbs, &_exp);
159 _man = _man * pow(2, MLX5_MAN_WIDTH);
160 _exp = _exp - MLX5_MAN_WIDTH;
161 *man = (uint8_t)ceil(_man);
166 * Fill the prm meter parameter.
169 * Pointer to meter profie to be converted.
171 * Pointer to the error structure.
174 * 0 on success, a negative errno value otherwise and rte_errno is set.
177 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
178 struct rte_mtr_error *error)
180 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
183 if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
184 return -rte_mtr_error_set(error, ENOTSUP,
185 RTE_MTR_ERROR_TYPE_METER_PROFILE,
186 NULL, "Metering algorithm not supported.");
187 /* cbs = cbs_mantissa * 2^cbs_exponent */
188 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
190 srtcm->cbs_mantissa = man;
191 srtcm->cbs_exponent = exp;
192 /* Check if cbs mantissa is too large. */
193 if (srtcm->cbs_exponent != exp)
194 return -rte_mtr_error_set(error, EINVAL,
195 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
196 "Metering profile parameter cbs is"
198 /* ebs = ebs_mantissa * 2^ebs_exponent */
199 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
201 srtcm->ebs_mantissa = man;
202 srtcm->ebs_exponent = exp;
203 /* Check if ebs mantissa is too large. */
204 if (srtcm->ebs_exponent != exp)
205 return -rte_mtr_error_set(error, EINVAL,
206 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
207 "Metering profile parameter ebs is"
209 /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
210 mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
212 srtcm->cir_mantissa = man;
213 srtcm->cir_exponent = exp;
214 /* Check if cir mantissa is too large. */
215 if (srtcm->cir_exponent != exp)
216 return -rte_mtr_error_set(error, EINVAL,
217 RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
218 "Metering profile parameter cir is"
224 * Callback to get MTR capabilities.
227 * Pointer to Ethernet device.
229 * Pointer to save MTR capabilities.
231 * Pointer to the error structure.
234 * 0 on success, a negative errno value otherwise and rte_errno is set.
237 mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
238 struct rte_mtr_capabilities *cap,
239 struct rte_mtr_error *error __rte_unused)
241 struct mlx5_priv *priv = dev->data->dev_private;
242 struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
245 return -rte_mtr_error_set(error, ENOTSUP,
246 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
247 "Meter is not support");
248 memset(cap, 0, sizeof(*cap));
249 cap->n_max = 1 << qattr->log_max_flow_meter;
250 cap->n_shared_max = cap->n_max;
252 cap->shared_identical = 1;
253 cap->shared_n_flows_per_mtr_max = 4 << 20;
254 /* 2M flows can share the same meter. */
255 cap->chaining_n_mtrs_per_flow_max = 1; /* Chaining is not supported. */
256 cap->meter_srtcm_rfc2697_n_max = qattr->srtcm_sup ? cap->n_max : 0;
257 cap->meter_rate_max = 1ULL << 40; /* 1 Tera tokens per sec. */
258 cap->policer_action_drop_supported = 1;
259 cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
260 RTE_MTR_STATS_N_PKTS_DROPPED;
265 * Callback to add MTR profile.
268 * Pointer to Ethernet device.
269 * @param[in] meter_profile_id
272 * Pointer to meter profile detail.
274 * Pointer to the error structure.
277 * 0 on success, a negative errno value otherwise and rte_errno is set.
280 mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
281 uint32_t meter_profile_id,
282 struct rte_mtr_meter_profile *profile,
283 struct rte_mtr_error *error)
285 struct mlx5_priv *priv = dev->data->dev_private;
286 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
287 struct mlx5_flow_meter_profile *fmp;
291 return -rte_mtr_error_set(error, ENOTSUP,
292 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
293 "Meter is not support");
294 /* Check input params. */
295 ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
299 /* Meter profile memory allocation. */
300 fmp = rte_calloc(__func__, 1, sizeof(struct mlx5_flow_meter_profile),
301 RTE_CACHE_LINE_SIZE);
303 return -rte_mtr_error_set(error, ENOMEM,
304 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
305 NULL, "Meter profile memory "
307 /* Fill profile info. */
308 fmp->meter_profile_id = meter_profile_id;
309 fmp->profile = *profile;
310 /* Fill the flow meter parameters for the PRM. */
311 ret = mlx5_flow_meter_param_fill(fmp, error);
315 TAILQ_INSERT_TAIL(fmps, fmp, next);
323 * Callback to delete MTR profile.
326 * Pointer to Ethernet device.
327 * @param[in] meter_profile_id
330 * Pointer to the error structure.
333 * 0 on success, a negative errno value otherwise and rte_errno is set.
336 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
337 uint32_t meter_profile_id,
338 struct rte_mtr_error *error)
340 struct mlx5_priv *priv = dev->data->dev_private;
341 struct mlx5_flow_meter_profile *fmp;
344 return -rte_mtr_error_set(error, ENOTSUP,
345 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
346 "Meter is not support");
347 /* Meter profile must exist. */
348 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
350 return -rte_mtr_error_set(error, ENOENT,
351 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
353 "Meter profile id invalid.");
354 /* Check profile is unused. */
356 return -rte_mtr_error_set(error, EBUSY,
357 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
358 NULL, "Meter profile in use.");
359 /* Remove from list. */
360 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
365 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
366 .capabilities_get = mlx5_flow_mtr_cap_get,
367 .meter_profile_add = mlx5_flow_meter_profile_add,
368 .meter_profile_delete = mlx5_flow_meter_profile_delete,
371 .meter_enable = NULL,
372 .meter_disable = NULL,
373 .meter_profile_update = NULL,
374 .meter_dscp_table_update = NULL,
375 .policer_actions_update = NULL,
376 .stats_update = NULL,
381 * Get meter operations.
384 * Pointer to Ethernet device structure.
386 * Pointer to set the mtr operations.
392 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
394 *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;