1
2
3
4
5#include <math.h>
6
7#include <rte_tailq.h>
8#include <rte_malloc.h>
9#include <rte_mtr.h>
10#include <rte_mtr_driver.h>
11
12#include <mlx5_devx_cmds.h>
13#include <mlx5_malloc.h>
14
15#include "mlx5.h"
16#include "mlx5_flow.h"
17
18
19
20
21
22
23
24
25
26
27
28
29static void *
30mlx5_flow_meter_action_create(struct mlx5_priv *priv,
31 struct mlx5_flow_meter *fm)
32{
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;
38
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);
58 mtr_init.next_table =
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);
67#else
68 (void)priv;
69 (void)fm;
70 return NULL;
71#endif
72}
73
74
75
76
77
78
79
80
81
82
83
84
85static struct mlx5_flow_meter_profile *
86mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
87{
88 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
89 struct mlx5_flow_meter_profile *fmp;
90
91 TAILQ_FOREACH(fmp, fmps, next)
92 if (meter_profile_id == fmp->meter_profile_id)
93 return fmp;
94 return NULL;
95}
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112static int
113mlx5_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)
117{
118 struct mlx5_priv *priv = dev->data->dev_private;
119 struct mlx5_flow_meter_profile *fmp;
120
121
122 if (profile == NULL)
123 return -rte_mtr_error_set(error, EINVAL,
124 RTE_MTR_ERROR_TYPE_METER_PROFILE,
125 NULL, "Meter profile is null.");
126
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
132 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
133 if (fmp)
134 return -rte_mtr_error_set(error, EEXIST,
135 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
136 NULL,
137 "Meter profile already exists.");
138 if (profile->alg == RTE_MTR_SRTCM_RFC2697) {
139 if (priv->config.hca_attr.qos.srtcm_sup) {
140
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)
146 return 0;
147 else
148 return -rte_mtr_error_set
149 (error, ENOTSUP,
150 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
151 NULL,
152 profile->srtcm_rfc2697.ebs ?
153 "Metering value ebs must be 0." :
154 "Invalid metering parameters.");
155 }
156 }
157 return -rte_mtr_error_set(error, ENOTSUP,
158 RTE_MTR_ERROR_TYPE_METER_PROFILE,
159 NULL, "Metering algorithm not supported.");
160}
161
162
163
164
165
166
167
168
169
170
171
172static void
173mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
174{
175 int64_t _cir;
176 int64_t delta = INT64_MAX;
177 uint8_t _man = 0;
178 uint8_t _exp = 0;
179 uint64_t m, e;
180
181 for (m = 0; m <= 0xFF; m++) {
182 for (e = 0; e <= 0x1F; e++) {
183 _cir = (1000000000ULL * m) >> e;
184 if (llabs(cir - _cir) <= delta) {
185 delta = llabs(cir - _cir);
186 _man = m;
187 _exp = e;
188 }
189 }
190 }
191 *man = _man;
192 *exp = _exp;
193}
194
195
196
197
198
199
200
201
202
203
204
205static void
206mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
207{
208 int _exp;
209 double _man;
210
211
212 if (xbs == 0) {
213 *man = 0;
214 *exp = 0;
215 return;
216 }
217
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);
222 *exp = _exp;
223}
224
225
226
227
228
229
230
231
232
233
234
235
236static int
237mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
238 struct rte_mtr_error *error)
239{
240 struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
241 uint8_t man, exp;
242
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
248 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
249 &man, &exp);
250 srtcm->cbs_mantissa = man;
251 srtcm->cbs_exponent = exp;
252
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"
257 " invalid.");
258
259 mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
260 &man, &exp);
261 srtcm->ebs_mantissa = man;
262 srtcm->ebs_exponent = exp;
263
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"
268 " invalid.");
269
270 mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
271 &man, &exp);
272 srtcm->cir_mantissa = man;
273 srtcm->cir_exponent = exp;
274
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"
279 " invalid.");
280 return 0;
281}
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296static int
297mlx5_flow_mtr_cap_get(struct rte_eth_dev *dev,
298 struct rte_mtr_capabilities *cap,
299 struct rte_mtr_error *error __rte_unused)
300{
301 struct mlx5_priv *priv = dev->data->dev_private;
302 struct mlx5_hca_qos_attr *qattr = &priv->config.hca_attr.qos;
303
304 if (!priv->mtr_en)
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;
311 cap->identical = 1;
312 cap->shared_identical = 1;
313 cap->shared_n_flows_per_mtr_max = 4 << 20;
314
315 cap->chaining_n_mtrs_per_flow_max = 1;
316 cap->meter_srtcm_rfc2697_n_max = qattr->srtcm_sup ? cap->n_max : 0;
317 cap->meter_rate_max = 1ULL << 40;
318 cap->policer_action_drop_supported = 1;
319 cap->stats_mask = RTE_MTR_STATS_N_BYTES_DROPPED |
320 RTE_MTR_STATS_N_PKTS_DROPPED;
321 return 0;
322}
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339static int
340mlx5_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)
344{
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;
348 int ret;
349
350 if (!priv->mtr_en)
351 return -rte_mtr_error_set(error, ENOTSUP,
352 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
353 "Meter is not supported");
354
355 ret = mlx5_flow_meter_profile_validate(dev, meter_profile_id,
356 profile, error);
357 if (ret)
358 return ret;
359
360 fmp = mlx5_malloc(MLX5_MEM_ZERO, sizeof(struct mlx5_flow_meter_profile),
361 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
362 if (fmp == NULL)
363 return -rte_mtr_error_set(error, ENOMEM,
364 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
365 NULL, "Meter profile memory "
366 "alloc failed.");
367
368 fmp->meter_profile_id = meter_profile_id;
369 fmp->profile = *profile;
370
371 ret = mlx5_flow_meter_param_fill(fmp, error);
372 if (ret)
373 goto error;
374
375 TAILQ_INSERT_TAIL(fmps, fmp, next);
376 return 0;
377error:
378 mlx5_free(fmp);
379 return ret;
380}
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395static int
396mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
397 uint32_t meter_profile_id,
398 struct rte_mtr_error *error)
399{
400 struct mlx5_priv *priv = dev->data->dev_private;
401 struct mlx5_flow_meter_profile *fmp;
402
403 if (!priv->mtr_en)
404 return -rte_mtr_error_set(error, ENOTSUP,
405 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
406 "Meter is not supported");
407
408 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
409 if (fmp == NULL)
410 return -rte_mtr_error_set(error, ENOENT,
411 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
412 &meter_profile_id,
413 "Meter profile id is invalid.");
414
415 if (fmp->ref_cnt)
416 return -rte_mtr_error_set(error, EBUSY,
417 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
418 NULL, "Meter profile is in use.");
419
420 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
421 mlx5_free(fmp);
422 return 0;
423}
424
425
426
427
428
429
430
431
432
433
434static inline enum rte_mtr_error_type
435action2error(enum rte_mtr_policer_action action)
436{
437 switch (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;
444 default:
445 break;
446 }
447 return RTE_MTR_ERROR_TYPE_UNSPECIFIED;
448}
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465static int
466mlx5_flow_meter_validate(struct mlx5_priv *priv, uint32_t meter_id,
467 struct rte_mtr_params *params,
468 struct rte_mtr_error *error)
469{
470 static enum rte_mtr_policer_action
471 valid_recol_action[RTE_COLORS] = {
472 MTR_POLICER_ACTION_COLOR_GREEN,
473 MTR_POLICER_ACTION_COLOR_YELLOW,
474 MTR_POLICER_ACTION_COLOR_RED };
475 int i;
476
477
478 if (params == NULL)
479 return -rte_mtr_error_set(error, EINVAL,
480 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
481 NULL, "Meter object params null.");
482
483 if (params->use_prev_mtr_color)
484 return -rte_mtr_error_set(error, ENOTSUP,
485 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
486 NULL,
487 "Previous meter color "
488 "not supported.");
489
490 for (i = 0; i < RTE_COLORS; i++)
491 if (params->action[i] != valid_recol_action[i] &&
492 params->action[i] != MTR_POLICER_ACTION_DROP)
493 return -rte_mtr_error_set
494 (error, ENOTSUP,
495 action2error(params->action[i]), NULL,
496 "Recolor action not supported.");
497
498 if (mlx5_flow_meter_find(priv, meter_id))
499 return -rte_mtr_error_set(error, EEXIST,
500 RTE_MTR_ERROR_TYPE_MTR_ID, NULL,
501 "Meter object already exists.");
502 return 0;
503}
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521static int
522mlx5_flow_meter_action_modify(struct mlx5_priv *priv,
523 struct mlx5_flow_meter *fm,
524 const struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm,
525 uint64_t modify_bits, uint32_t active_state)
526{
527#ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
528 uint32_t in[MLX5_ST_SZ_DW(flow_meter_parameters)] = { 0 };
529 uint32_t *attr;
530 struct mlx5dv_dr_flow_meter_attr mod_attr = { 0 };
531 int ret;
532
533
534 mod_attr.reg_c_index = priv->mtr_color_reg - REG_C_0;
535 mod_attr.flow_meter_parameter = in;
536 mod_attr.flow_meter_parameter_sz = fm->mfts->fmp_size;
537 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
538 mod_attr.active = !!active_state;
539 else
540 mod_attr.active = 0;
541 attr = in;
542 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
543 MLX5_SET(flow_meter_parameters,
544 attr, cbs_exponent, srtcm->cbs_exponent);
545 MLX5_SET(flow_meter_parameters,
546 attr, cbs_mantissa, srtcm->cbs_mantissa);
547 }
548 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
549 MLX5_SET(flow_meter_parameters,
550 attr, cir_exponent, srtcm->cir_exponent);
551 MLX5_SET(flow_meter_parameters,
552 attr, cir_mantissa, srtcm->cir_mantissa);
553 }
554 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
555 MLX5_SET(flow_meter_parameters,
556 attr, ebs_exponent, srtcm->ebs_exponent);
557 MLX5_SET(flow_meter_parameters,
558 attr, ebs_mantissa, srtcm->ebs_mantissa);
559 }
560
561 if (fm->mfts->meter_action) {
562 ret = mlx5_glue->dv_modify_flow_action_meter
563 (fm->mfts->meter_action, &mod_attr,
564 rte_cpu_to_be_64(modify_bits));
565 if (ret)
566 return ret;
567 }
568
569 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_ACTIVE)
570 fm->active_state = !!active_state;
571 attr = fm->mfts->fmp;
572 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS) {
573 MLX5_SET(flow_meter_parameters,
574 attr, cbs_exponent, srtcm->cbs_exponent);
575 MLX5_SET(flow_meter_parameters,
576 attr, cbs_mantissa, srtcm->cbs_mantissa);
577 }
578 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR) {
579 MLX5_SET(flow_meter_parameters,
580 attr, cir_exponent, srtcm->cir_exponent);
581 MLX5_SET(flow_meter_parameters,
582 attr, cir_mantissa, srtcm->cir_mantissa);
583 }
584 if (modify_bits & MLX5_FLOW_METER_OBJ_MODIFY_FIELD_EBS) {
585 MLX5_SET(flow_meter_parameters,
586 attr, ebs_exponent, srtcm->ebs_exponent);
587 MLX5_SET(flow_meter_parameters,
588 attr, ebs_mantissa, srtcm->ebs_mantissa);
589 }
590
591 return 0;
592#else
593 (void)priv;
594 (void)fm;
595 (void)srtcm;
596 (void)modify_bits;
597 (void)active_state;
598 return -ENOTSUP;
599#endif
600}
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619static int
620mlx5_flow_meter_create(struct rte_eth_dev *dev, uint32_t meter_id,
621 struct rte_mtr_params *params, int shared,
622 struct rte_mtr_error *error)
623{
624 struct mlx5_priv *priv = dev->data->dev_private;
625 struct mlx5_flow_meters *fms = &priv->flow_meters;
626 struct mlx5_flow_meter_profile *fmp;
627 struct mlx5_flow_meter *fm;
628 const struct rte_flow_attr attr = {
629 .ingress = 1,
630 .egress = 1,
631 .transfer = priv->config.dv_esw_en ? 1 : 0,
632 };
633 int ret;
634 unsigned int i;
635 uint32_t idx = 0;
636
637 if (!priv->mtr_en)
638 return -rte_mtr_error_set(error, ENOTSUP,
639 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
640 "Meter is not supported");
641
642 ret = mlx5_flow_meter_validate(priv, meter_id, params, error);
643 if (ret)
644 return ret;
645
646 fmp = mlx5_flow_meter_profile_find(priv, params->meter_profile_id);
647 if (fmp == NULL)
648 return -rte_mtr_error_set(error, ENOENT,
649 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
650 NULL, "Meter profile id not valid.");
651
652 fm = mlx5_ipool_zmalloc(priv->sh->ipool[MLX5_IPOOL_MTR], &idx);
653 if (fm == NULL)
654 return -rte_mtr_error_set(error, ENOMEM,
655 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
656 "Memory alloc failed for meter.");
657 fm->idx = idx;
658
659 fm->meter_id = meter_id;
660 fm->profile = fmp;
661 memcpy(fm->action, params->action, sizeof(params->action));
662 fm->stats_mask = params->stats_mask;
663
664
665 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++) {
666 fm->policer_stats.cnt[i] = mlx5_counter_alloc(dev);
667 if (!fm->policer_stats.cnt[i])
668 goto error;
669 }
670 fm->mfts = mlx5_flow_create_mtr_tbls(dev, fm);
671 if (!fm->mfts)
672 goto error;
673 ret = mlx5_flow_create_policer_rules(dev, fm, &attr);
674 if (ret)
675 goto error;
676
677 TAILQ_INSERT_TAIL(fms, fm, next);
678 fm->active_state = 1;
679 fm->shared = !!shared;
680 fm->policer_stats.stats_mask = params->stats_mask;
681 fm->profile->ref_cnt++;
682 return 0;
683error:
684 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
685 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
686
687 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
688 if (fm->policer_stats.cnt[i])
689 mlx5_counter_free(dev, fm->policer_stats.cnt[i]);
690 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], idx);
691 return -rte_mtr_error_set(error, -ret,
692 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
693 NULL, "Failed to create devx meter.");
694}
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709static int
710mlx5_flow_meter_destroy(struct rte_eth_dev *dev, uint32_t meter_id,
711 struct rte_mtr_error *error)
712{
713 struct mlx5_priv *priv = dev->data->dev_private;
714 struct mlx5_flow_meters *fms = &priv->flow_meters;
715 struct mlx5_flow_meter_profile *fmp;
716 struct mlx5_flow_meter *fm;
717 const struct rte_flow_attr attr = {
718 .ingress = 1,
719 .egress = 1,
720 .transfer = priv->config.dv_esw_en ? 1 : 0,
721 };
722 unsigned int i;
723
724 if (!priv->mtr_en)
725 return -rte_mtr_error_set(error, ENOTSUP,
726 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
727 "Meter is not supported");
728
729 fm = mlx5_flow_meter_find(priv, meter_id);
730 if (fm == NULL)
731 return -rte_mtr_error_set(error, ENOENT,
732 RTE_MTR_ERROR_TYPE_MTR_ID,
733 NULL, "Meter object id not valid.");
734
735 if (fm->ref_cnt > 0)
736 return -rte_mtr_error_set(error, EBUSY,
737 RTE_MTR_ERROR_TYPE_UNSPECIFIED,
738 NULL, "Meter object is being used.");
739
740 fmp = fm->profile;
741 MLX5_ASSERT(fmp);
742
743 fmp->ref_cnt--;
744
745 TAILQ_REMOVE(fms, fm, next);
746
747 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
748 if (fm->policer_stats.cnt[i])
749 mlx5_counter_free(dev, fm->policer_stats.cnt[i]);
750
751 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
752 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
753 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
754 return 0;
755}
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772static int
773mlx5_flow_meter_modify_state(struct mlx5_priv *priv,
774 struct mlx5_flow_meter *fm,
775 uint32_t new_state,
776 struct rte_mtr_error *error)
777{
778 static const struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm = {
779 .cbs_exponent = 20,
780 .cbs_mantissa = 191,
781 .cir_exponent = 0,
782 .cir_mantissa = 200,
783 .ebs_exponent = 0,
784 .ebs_mantissa = 0,
785 };
786 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
787 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
788 int ret;
789
790 if (new_state == MLX5_FLOW_METER_DISABLE)
791 ret = mlx5_flow_meter_action_modify(priv, fm, &srtcm,
792 modify_bits, 0);
793 else
794 ret = mlx5_flow_meter_action_modify(priv, fm,
795 &fm->profile->srtcm_prm,
796 modify_bits, 0);
797 if (ret)
798 return -rte_mtr_error_set(error, -ret,
799 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
800 NULL,
801 new_state ?
802 "Failed to enable meter." :
803 "Failed to disable meter.");
804 return 0;
805}
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820static int
821mlx5_flow_meter_enable(struct rte_eth_dev *dev,
822 uint32_t meter_id,
823 struct rte_mtr_error *error)
824{
825 struct mlx5_priv *priv = dev->data->dev_private;
826 struct mlx5_flow_meter *fm;
827 int ret;
828
829 if (!priv->mtr_en)
830 return -rte_mtr_error_set(error, ENOTSUP,
831 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
832 "Meter is not supported");
833
834 fm = mlx5_flow_meter_find(priv, meter_id);
835 if (fm == NULL)
836 return -rte_mtr_error_set(error, ENOENT,
837 RTE_MTR_ERROR_TYPE_MTR_ID,
838 NULL, "Meter not found.");
839 if (fm->active_state == MLX5_FLOW_METER_ENABLE)
840 return 0;
841 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_ENABLE,
842 error);
843 if (!ret)
844 fm->active_state = MLX5_FLOW_METER_ENABLE;
845 return ret;
846}
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861static int
862mlx5_flow_meter_disable(struct rte_eth_dev *dev,
863 uint32_t meter_id,
864 struct rte_mtr_error *error)
865{
866 struct mlx5_priv *priv = dev->data->dev_private;
867 struct mlx5_flow_meter *fm;
868 int ret;
869
870 if (!priv->mtr_en)
871 return -rte_mtr_error_set(error, ENOTSUP,
872 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
873 "Meter is not supported");
874
875 fm = mlx5_flow_meter_find(priv, meter_id);
876 if (fm == NULL)
877 return -rte_mtr_error_set(error, ENOENT,
878 RTE_MTR_ERROR_TYPE_MTR_ID,
879 NULL, "Meter not found.");
880 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
881 return 0;
882 ret = mlx5_flow_meter_modify_state(priv, fm, MLX5_FLOW_METER_DISABLE,
883 error);
884 if (!ret)
885 fm->active_state = MLX5_FLOW_METER_DISABLE;
886 return ret;
887}
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904static int
905mlx5_flow_meter_profile_update(struct rte_eth_dev *dev,
906 uint32_t meter_id,
907 uint32_t meter_profile_id,
908 struct rte_mtr_error *error)
909{
910 struct mlx5_priv *priv = dev->data->dev_private;
911 struct mlx5_flow_meter_profile *fmp;
912 struct mlx5_flow_meter_profile *old_fmp;
913 struct mlx5_flow_meter *fm;
914 uint64_t modify_bits = MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CBS |
915 MLX5_FLOW_METER_OBJ_MODIFY_FIELD_CIR;
916 int ret;
917
918 if (!priv->mtr_en)
919 return -rte_mtr_error_set(error, ENOTSUP,
920 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
921 "Meter is not supported");
922
923 fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
924 if (fmp == NULL)
925 return -rte_mtr_error_set(error, ENOENT,
926 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
927 NULL, "Meter profile not found.");
928
929 fm = mlx5_flow_meter_find(priv, meter_id);
930 if (fm == NULL)
931 return -rte_mtr_error_set(error, ENOENT,
932 RTE_MTR_ERROR_TYPE_MTR_ID,
933 NULL, "Meter not found.");
934
935 old_fmp = fm->profile;
936 if (fmp == old_fmp)
937 return 0;
938
939 fm->profile = fmp;
940
941 if (fm->active_state == MLX5_FLOW_METER_DISABLE)
942 return 0;
943 ret = mlx5_flow_meter_action_modify(priv, fm, &fm->profile->srtcm_prm,
944 modify_bits, fm->active_state);
945 if (ret) {
946 fm->profile = old_fmp;
947 return -rte_mtr_error_set(error, -ret,
948 RTE_MTR_ERROR_TYPE_MTR_PARAMS,
949 NULL, "Failed to update meter"
950 " parmeters in hardware.");
951 }
952 old_fmp->ref_cnt--;
953 fmp->ref_cnt++;
954 return 0;
955}
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972static int
973mlx5_flow_meter_stats_update(struct rte_eth_dev *dev,
974 uint32_t meter_id,
975 uint64_t stats_mask,
976 struct rte_mtr_error *error)
977{
978 struct mlx5_priv *priv = dev->data->dev_private;
979 struct mlx5_flow_meter *fm;
980
981 if (!priv->mtr_en)
982 return -rte_mtr_error_set(error, ENOTSUP,
983 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
984 "Meter is not supported");
985
986 fm = mlx5_flow_meter_find(priv, meter_id);
987 if (fm == NULL)
988 return -rte_mtr_error_set(error, ENOENT,
989 RTE_MTR_ERROR_TYPE_MTR_ID,
990 NULL, "Meter object id not valid.");
991 fm->policer_stats.stats_mask = stats_mask;
992 return 0;
993}
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014static int
1015mlx5_flow_meter_stats_read(struct rte_eth_dev *dev,
1016 uint32_t meter_id,
1017 struct rte_mtr_stats *stats,
1018 uint64_t *stats_mask,
1019 int clear,
1020 struct rte_mtr_error *error)
1021{
1022 static uint64_t meter2mask[RTE_MTR_DROPPED + 1] = {
1023 RTE_MTR_STATS_N_PKTS_GREEN | RTE_MTR_STATS_N_BYTES_GREEN,
1024 RTE_MTR_STATS_N_PKTS_YELLOW | RTE_MTR_STATS_N_BYTES_YELLOW,
1025 RTE_MTR_STATS_N_PKTS_RED | RTE_MTR_STATS_N_BYTES_RED,
1026 RTE_MTR_STATS_N_PKTS_DROPPED | RTE_MTR_STATS_N_BYTES_DROPPED
1027 };
1028 struct mlx5_priv *priv = dev->data->dev_private;
1029 struct mlx5_flow_meter *fm;
1030 struct mlx5_flow_policer_stats *ps;
1031 uint64_t pkts_dropped = 0;
1032 uint64_t bytes_dropped = 0;
1033 uint64_t pkts;
1034 uint64_t bytes;
1035 int i;
1036 int ret = 0;
1037
1038 if (!priv->mtr_en)
1039 return -rte_mtr_error_set(error, ENOTSUP,
1040 RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
1041 "Meter is not supported");
1042
1043 fm = mlx5_flow_meter_find(priv, meter_id);
1044 if (fm == NULL)
1045 return -rte_mtr_error_set(error, ENOENT,
1046 RTE_MTR_ERROR_TYPE_MTR_ID,
1047 NULL, "Meter object id not valid.");
1048 ps = &fm->policer_stats;
1049 *stats_mask = ps->stats_mask;
1050 for (i = 0; i < RTE_MTR_DROPPED; i++) {
1051 if (*stats_mask & meter2mask[i]) {
1052 ret = mlx5_counter_query(dev, ps->cnt[i], clear, &pkts,
1053 &bytes);
1054 if (ret)
1055 goto error;
1056 if (fm->action[i] == MTR_POLICER_ACTION_DROP) {
1057 pkts_dropped += pkts;
1058 bytes_dropped += bytes;
1059 }
1060
1061 if ((1 << i) & (*stats_mask & meter2mask[i]))
1062 stats->n_pkts[i] = pkts;
1063
1064 if ((1 << (RTE_MTR_DROPPED + 1 + i)) &
1065 (*stats_mask & meter2mask[i]))
1066 stats->n_bytes[i] = bytes;
1067 }
1068 }
1069
1070 if (*stats_mask & meter2mask[i]) {
1071 ret = mlx5_counter_query(dev, ps->cnt[i], clear, &pkts,
1072 &bytes);
1073 if (ret)
1074 goto error;
1075 pkts += pkts_dropped;
1076 bytes += bytes_dropped;
1077
1078 if ((*stats_mask & meter2mask[i]) &
1079 RTE_MTR_STATS_N_PKTS_DROPPED)
1080 stats->n_pkts_dropped = pkts;
1081
1082 if ((*stats_mask & meter2mask[i]) &
1083 RTE_MTR_STATS_N_BYTES_DROPPED)
1084 stats->n_bytes_dropped = bytes;
1085 }
1086 return 0;
1087error:
1088 return -rte_mtr_error_set(error, ret, RTE_MTR_ERROR_TYPE_STATS, NULL,
1089 "Failed to read policer counters.");
1090}
1091
1092static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
1093 .capabilities_get = mlx5_flow_mtr_cap_get,
1094 .meter_profile_add = mlx5_flow_meter_profile_add,
1095 .meter_profile_delete = mlx5_flow_meter_profile_delete,
1096 .create = mlx5_flow_meter_create,
1097 .destroy = mlx5_flow_meter_destroy,
1098 .meter_enable = mlx5_flow_meter_enable,
1099 .meter_disable = mlx5_flow_meter_disable,
1100 .meter_profile_update = mlx5_flow_meter_profile_update,
1101 .meter_dscp_table_update = NULL,
1102 .policer_actions_update = NULL,
1103 .stats_update = mlx5_flow_meter_stats_update,
1104 .stats_read = mlx5_flow_meter_stats_read,
1105};
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118int
1119mlx5_flow_meter_ops_get(struct rte_eth_dev *dev __rte_unused, void *arg)
1120{
1121 *(const struct rte_mtr_ops **)arg = &mlx5_flow_mtr_ops;
1122 return 0;
1123}
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136struct mlx5_flow_meter *
1137mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id)
1138{
1139 struct mlx5_flow_meters *fms = &priv->flow_meters;
1140 struct mlx5_flow_meter *fm;
1141
1142 TAILQ_FOREACH(fm, fms, next)
1143 if (meter_id == fm->meter_id)
1144 return fm;
1145 return NULL;
1146}
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164struct mlx5_flow_meter *
1165mlx5_flow_meter_attach(struct mlx5_priv *priv, uint32_t meter_id,
1166 const struct rte_flow_attr *attr,
1167 struct rte_flow_error *error)
1168{
1169 struct mlx5_flow_meter *fm;
1170
1171 fm = mlx5_flow_meter_find(priv, meter_id);
1172 if (fm == NULL) {
1173 rte_flow_error_set(error, ENOENT,
1174 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1175 "Meter object id not valid");
1176 goto error;
1177 }
1178 if (!fm->shared && fm->ref_cnt) {
1179 DRV_LOG(ERR, "Cannot share a non-shared meter.");
1180 rte_flow_error_set(error, EINVAL,
1181 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1182 "Meter can't be shared");
1183 goto error;
1184 }
1185 if (!fm->ref_cnt++) {
1186 MLX5_ASSERT(!fm->mfts->meter_action);
1187 fm->ingress = attr->ingress;
1188 fm->egress = attr->egress;
1189 fm->transfer = attr->transfer;
1190
1191 fm->mfts->meter_action = mlx5_flow_meter_action_create(priv,
1192 fm);
1193 if (!fm->mfts->meter_action)
1194 goto error_detach;
1195 } else {
1196 MLX5_ASSERT(fm->mfts->meter_action);
1197 if (attr->transfer != fm->transfer ||
1198 attr->ingress != fm->ingress ||
1199 attr->egress != fm->egress) {
1200 DRV_LOG(ERR, "meter I/O attributes do not "
1201 "match flow I/O attributes.");
1202 goto error_detach;
1203 }
1204 }
1205 return fm;
1206error_detach:
1207 mlx5_flow_meter_detach(fm);
1208 rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
1209 fm->mfts->meter_action ? "Meter attr not match" :
1210 "Meter action create failed");
1211error:
1212 return NULL;
1213}
1214
1215
1216
1217
1218
1219
1220
1221void
1222mlx5_flow_meter_detach(struct mlx5_flow_meter *fm)
1223{
1224 MLX5_ASSERT(fm->ref_cnt);
1225 if (--fm->ref_cnt)
1226 return;
1227 if (fm->mfts->meter_action)
1228 mlx5_glue->destroy_flow_action(fm->mfts->meter_action);
1229 fm->mfts->meter_action = NULL;
1230 fm->ingress = 0;
1231 fm->egress = 0;
1232 fm->transfer = 0;
1233}
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246int
1247mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
1248{
1249 struct mlx5_priv *priv = dev->data->dev_private;
1250 struct mlx5_flow_meters *fms = &priv->flow_meters;
1251 struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
1252 struct mlx5_flow_meter_profile *fmp;
1253 struct mlx5_flow_meter *fm;
1254 const struct rte_flow_attr attr = {
1255 .ingress = 1,
1256 .egress = 1,
1257 .transfer = priv->config.dv_esw_en ? 1 : 0,
1258 };
1259 void *tmp;
1260 uint32_t i;
1261
1262 TAILQ_FOREACH_SAFE(fm, fms, next, tmp) {
1263
1264 MLX5_ASSERT(!fm->ref_cnt);
1265
1266 fmp = fm->profile;
1267 if (fmp == NULL)
1268 return -rte_mtr_error_set(error, EINVAL,
1269 RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
1270 NULL, "MTR object meter profile invalid.");
1271
1272 fmp->ref_cnt--;
1273
1274 TAILQ_REMOVE(fms, fm, next);
1275
1276 for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
1277 if (fm->policer_stats.cnt[i])
1278 mlx5_counter_free(dev,
1279 fm->policer_stats.cnt[i]);
1280
1281 mlx5_flow_destroy_policer_rules(dev, fm, &attr);
1282 mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
1283 mlx5_ipool_free(priv->sh->ipool[MLX5_IPOOL_MTR], fm->idx);
1284 }
1285 TAILQ_FOREACH_SAFE(fmp, fmps, next, tmp) {
1286
1287 MLX5_ASSERT(!fmp->ref_cnt);
1288
1289 TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
1290 mlx5_free(fmp);
1291 }
1292 return 0;
1293}
1294