common/mlx5: introduce common library
[dpdk.git] / drivers / net / mlx5 / mlx5_flow_meter.c
index c39e703..32d51c0 100644 (file)
@@ -4,13 +4,72 @@
  */
 #include <math.h>
 
+#include <rte_tailq.h>
 #include <rte_malloc.h>
 #include <rte_mtr.h>
 #include <rte_mtr_driver.h>
 
+#include <mlx5_devx_cmds.h>
+
 #include "mlx5.h"
 #include "mlx5_flow.h"
 
+/**
+ * Create the meter action.
+ *
+ * @param priv
+ *   Pointer to mlx5_priv.
+ * @param[in] fm
+ *   Pointer to flow meter to be converted.
+ *
+ * @return
+ *   Pointer to the meter action on success, NULL otherwise.
+ */
+static void *
+mlx5_flow_meter_action_create(struct mlx5_priv *priv,
+                             struct mlx5_flow_meter *fm)
+{
+#ifdef HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER
+       struct mlx5dv_dr_flow_meter_attr mtr_init;
+       void *attr = fm->mfts->fmp;
+       struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm =
+                                                    &fm->profile->srtcm_prm;
+
+       fm->mfts->fmp_size = MLX5_ST_SZ_BYTES(flow_meter_parameters);
+       memset(attr, 0, fm->mfts->fmp_size);
+       MLX5_SET(flow_meter_parameters, attr, valid, 1);
+       MLX5_SET(flow_meter_parameters, attr, bucket_overflow, 1);
+       MLX5_SET(flow_meter_parameters, attr,
+                start_color, MLX5_FLOW_COLOR_GREEN);
+       MLX5_SET(flow_meter_parameters, attr, both_buckets_on_green, 0);
+       MLX5_SET(flow_meter_parameters,
+                attr, cbs_exponent, srtcm->cbs_exponent);
+       MLX5_SET(flow_meter_parameters,
+                attr, cbs_mantissa, srtcm->cbs_mantissa);
+       MLX5_SET(flow_meter_parameters,
+                attr, cir_exponent, srtcm->cir_exponent);
+       MLX5_SET(flow_meter_parameters,
+                attr, cir_mantissa, srtcm->cir_mantissa);
+       MLX5_SET(flow_meter_parameters,
+                attr, ebs_exponent, srtcm->ebs_exponent);
+       MLX5_SET(flow_meter_parameters,
+                attr, ebs_mantissa, srtcm->ebs_mantissa);
+       mtr_init.next_table =
+               fm->attr.transfer ? fm->mfts->transfer.tbl->obj :
+                   fm->attr.egress ? fm->mfts->egress.tbl->obj :
+                                      fm->mfts->ingress.tbl->obj;
+       mtr_init.reg_c_index = priv->mtr_color_reg - REG_C_0;
+       mtr_init.flow_meter_parameter = fm->mfts->fmp;
+       mtr_init.flow_meter_parameter_sz = fm->mfts->fmp_size;
+       mtr_init.active = fm->active_state;
+       return mlx5_glue->dv_create_flow_action_meter(&mtr_init);
+#else
+       (void)priv;
+       (void)fm;
+       return NULL;
+#endif
+}
+
 /**
  * Find meter profile by id.
  *
@@ -1081,3 +1140,148 @@ mlx5_flow_meter_find(struct mlx5_priv *priv, uint32_t meter_id)
                        return fm;
        return NULL;
 }
+
+/**
+ * Attach meter to flow.
+ * Unidirectional Meter creation can only be done
+ * when flow direction is known, i.e. when calling meter_attach.
+ *
+ * @param [in] priv
+ *  Pointer to mlx5 private data.
+ * @param [in] meter_id
+ *  Flow meter id.
+ * @param [in] attr
+ *  Pointer to flow attributes.
+ * @param [out] error
+ *  Pointer to error structure.
+ *
+ * @return the flow meter pointer, NULL otherwise.
+ */
+struct mlx5_flow_meter *
+mlx5_flow_meter_attach(struct mlx5_priv *priv, uint32_t meter_id,
+                      const struct rte_flow_attr *attr,
+                      struct rte_flow_error *error)
+{
+       struct mlx5_flow_meter *fm;
+
+       fm = mlx5_flow_meter_find(priv, meter_id);
+       if (fm == NULL) {
+               rte_flow_error_set(error, ENOENT,
+                                  RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+                                  "Meter object id not valid");
+               goto error;
+       }
+       if (!fm->shared && fm->ref_cnt) {
+               DRV_LOG(ERR, "Cannot share a non-shared meter.");
+               rte_flow_error_set(error, EINVAL,
+                                 RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+                                 "Meter can't be shared");
+               goto error;
+       }
+       if (!fm->ref_cnt++) {
+               RTE_ASSERT(!fm->mfts->meter_action);
+               fm->attr = *attr;
+               /* This also creates the meter object. */
+               fm->mfts->meter_action = mlx5_flow_meter_action_create(priv,
+                                                                      fm);
+               if (!fm->mfts->meter_action)
+                       goto error_detach;
+       } else {
+               RTE_ASSERT(fm->mfts->meter_action);
+               if (attr->transfer != fm->attr.transfer ||
+                   attr->ingress != fm->attr.ingress ||
+                   attr->egress != fm->attr.egress) {
+                       DRV_LOG(ERR, "meter I/O attributes do not "
+                               "match flow I/O attributes.");
+                       goto error_detach;
+               }
+       }
+       return fm;
+error_detach:
+       mlx5_flow_meter_detach(fm);
+       rte_flow_error_set(error, EINVAL, RTE_FLOW_ERROR_TYPE_UNSPECIFIED, NULL,
+                         fm->mfts->meter_action ? "Meter attr not match" :
+                         "Meter action create failed");
+error:
+       return NULL;
+}
+
+/**
+ * Detach meter from flow.
+ *
+ * @param [in] fm
+ *  Pointer to flow meter.
+ */
+void
+mlx5_flow_meter_detach(struct mlx5_flow_meter *fm)
+{
+       const struct rte_flow_attr attr = { 0 };
+
+       RTE_ASSERT(fm->ref_cnt);
+       if (--fm->ref_cnt)
+               return;
+       if (fm->mfts->meter_action)
+               mlx5_glue->destroy_flow_action(fm->mfts->meter_action);
+       fm->mfts->meter_action = NULL;
+       fm->attr = attr;
+}
+
+/**
+ * Flush meter configuration.
+ *
+ * @param[in] dev
+ *   Pointer to Ethernet device.
+ * @param[out] error
+ *   Pointer to rte meter error structure.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+int
+mlx5_flow_meter_flush(struct rte_eth_dev *dev, struct rte_mtr_error *error)
+{
+       struct mlx5_priv *priv = dev->data->dev_private;
+       struct mlx5_flow_meters *fms = &priv->flow_meters;
+       struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
+       struct mlx5_flow_meter_profile *fmp;
+       struct mlx5_flow_meter *fm;
+       const struct rte_flow_attr attr = {
+                               .ingress = 1,
+                               .egress = 1,
+                               .transfer = priv->config.dv_esw_en ? 1 : 0,
+                       };
+       void *tmp;
+       uint32_t i;
+
+       TAILQ_FOREACH_SAFE(fm, fms, next, tmp) {
+               /* Meter object must not have any owner. */
+               RTE_ASSERT(!fm->ref_cnt);
+               /* Get meter profile. */
+               fmp = fm->profile;
+               if (fmp == NULL)
+                       return -rte_mtr_error_set(error, EINVAL,
+                               RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
+                               NULL, "MTR object meter profile invalid.");
+               /* Update dependencies. */
+               fmp->ref_cnt--;
+               /* Remove from list. */
+               TAILQ_REMOVE(fms, fm, next);
+               /* Free policer counters. */
+               for (i = 0; i < RTE_DIM(fm->policer_stats.cnt); i++)
+                       if (fm->policer_stats.cnt[i])
+                               mlx5_counter_free(dev,
+                                                 fm->policer_stats.cnt[i]);
+               /* Free meter flow table. */
+               mlx5_flow_destroy_policer_rules(dev, fm, &attr);
+               mlx5_flow_destroy_mtr_tbls(dev, fm->mfts);
+               rte_free(fm);
+       }
+       TAILQ_FOREACH_SAFE(fmp, fmps, next, tmp) {
+               /* Check unused. */
+               RTE_ASSERT(!fmp->ref_cnt);
+               /* Remove from list. */
+               TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
+               rte_free(fmp);
+       }
+       return 0;
+}