net/mlx5: support meter profile operations
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_meter.c
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright 2018 Mellanox Technologies, Ltd
4  */
5 #include <math.h>
6
7 #include <rte_malloc.h>
8 #include <rte_mtr.h>
9 #include <rte_mtr_driver.h>
10
11 #include "mlx5.h"
12 #include "mlx5_flow.h"
13
14 /**
15  * Find meter profile by id.
16  *
17  * @param priv
18  *   Pointer to mlx5_priv.
19  * @param meter_profile_id
20  *   Meter profile id.
21  *
22  * @return
23  *   Pointer to the profile found on success, NULL otherwise.
24  */
25 static struct mlx5_flow_meter_profile *
26 mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
27 {
28         struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
29         struct mlx5_flow_meter_profile *fmp;
30
31         TAILQ_FOREACH(fmp, fmps, next)
32                 if (meter_profile_id == fmp->meter_profile_id)
33                         return fmp;
34         return NULL;
35 }
36
37 /**
38  * Calculate mantissa and exponent for cir.
39  *
40  * @param[in] cir
41  *   Value to be calculated.
42  * @param[out] man
43  *   Pointer to the mantissa.
44  * @param[out] exp
45  *   Pointer to the exp.
46  */
47 static void
48 mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
49 {
50         int64_t _cir;
51         int64_t delta = INT64_MAX;
52         uint8_t _man = 0;
53         uint8_t _exp = 0;
54         uint64_t m, e;
55
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);
61                                 _man = m;
62                                 _exp = e;
63                         }
64                 }
65         }
66         *man = _man;
67         *exp = _exp;
68 }
69
70 /**
71  * Calculate mantissa and exponent for xbs.
72  *
73  * @param[in] xbs
74  *   Value to be calculated.
75  * @param[out] man
76  *   Pointer to the mantissa.
77  * @param[out] exp
78  *   Pointer to the exp.
79  */
80 static void
81 mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
82 {
83         int _exp;
84         double _man;
85
86         /* Special case xbs == 0 ? both exp and matissa are 0. */
87         if (xbs == 0) {
88                 *man = 0;
89                 *exp = 0;
90                 return;
91         }
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);
97         *exp = _exp;
98 }
99
100 /**
101  * Fill the prm meter parameter.
102  *
103  * @param[in,out] fmp
104  *   Pointer to meter profie to be converted.
105  * @param[out] error
106  *   Pointer to the error structure.
107  *
108  * @return
109  *   0 on success, a negative errno value otherwise and rte_errno is set.
110  */
111 static int
112 mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
113                           struct rte_mtr_error *error)
114 {
115         struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
116         uint8_t man, exp;
117
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,
124                                     &man, &exp);
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"
132                                           " invalid.");
133         /* ebs = ebs_mantissa * 2^ebs_exponent */
134         mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
135                                     &man, &exp);
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"
143                                           " invalid.");
144         /* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
145         mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
146                                     &man, &exp);
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"
154                                           " invalid.");
155         return 0;
156 }
157
158 /**
159  * Callback to get MTR capabilities.
160  *
161  * @param[in] dev
162  *   Pointer to Ethernet device.
163  * @param[out] cap
164  *   Pointer to save MTR capabilities.
165  * @param[out] error
166  *   Pointer to the error structure.
167  *
168  * @return
169  *   0 on success, a negative errno value otherwise and rte_errno is set.
170  */
171 static int
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)
175 {
176         struct mlx5_priv *priv = dev->data->dev_private;
177         struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
178
179         if (!priv->mtr_en)
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;
186         cap->identical = 1;
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;
196         return 0;
197 }
198
199 /**
200  * Callback to add MTR profile.
201  *
202  * @param[in] dev
203  *   Pointer to Ethernet device.
204  * @param[in] meter_profile_id
205  *   Meter profile id.
206  * @param[in] profile
207  *   Pointer to meter profile detail.
208  * @param[out] error
209  *   Pointer to the error structure.
210  *
211  * @return
212  *   0 on success, a negative errno value otherwise and rte_errno is set.
213  */
214 static int
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)
219 {
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;
223         int ret;
224
225         if (!priv->mtr_en)
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);
232         if (fmp == NULL)
233                 return -rte_mtr_error_set(error, ENOMEM,
234                                           RTE_MTR_ERROR_TYPE_UNSPECIFIED,
235                                           NULL, "Meter profile memory "
236                                           "alloc failed.");
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);
242         if (ret)
243                 goto error;
244         /* Add to list. */
245         TAILQ_INSERT_TAIL(fmps, fmp, next);
246         return 0;
247 error:
248         rte_free(fmp);
249         return ret;
250 }
251
252 /**
253  * Callback to delete MTR profile.
254  *
255  * @param[in] dev
256  *   Pointer to Ethernet device.
257  * @param[in] meter_profile_id
258  *   Meter profile id.
259  * @param[out] error
260  *   Pointer to the error structure.
261  *
262  * @return
263  *   0 on success, a negative errno value otherwise and rte_errno is set.
264  */
265 static int
266 mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
267                           uint32_t meter_profile_id,
268                           struct rte_mtr_error *error)
269 {
270         struct mlx5_priv *priv = dev->data->dev_private;
271         struct mlx5_flow_meter_profile *fmp;
272
273         if (!priv->mtr_en)
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);
279         if (fmp == NULL)
280                 return -rte_mtr_error_set(error, ENOENT,
281                                           RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
282                                           &meter_profile_id,
283                                           "Meter profile id invalid.");
284         /* Check profile is unused. */
285         if (fmp->ref_cnt)
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);
291         rte_free(fmp);
292         return 0;
293 }
294
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,
299         .create = NULL,
300         .destroy = NULL,
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,
307         .stats_read = NULL,
308 };
309
310 /**
311  * Get meter operations.
312  *
313  * @param dev
314  *   Pointer to Ethernet device structure.
315  * @param arg
316  *   Pointer to set the mtr operations.
317  *
318  * @return
319  *   Always 0.
320  */
321 int
322 mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
323 {
324         *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
325         return 0;
326 }