All of lore.kernel.org
 help / color / mirror / Atom feed
From: Suanming Mou <suanmingm@mellanox.com>
To: Matan Azrad <matan@mellanox.com>,
	Shahaf Shuler <shahafs@mellanox.com>,
	Viacheslav Ovsiienko <viacheslavo@mellanox.com>
Cc: dev@dpdk.org
Subject: [dpdk-dev] [PATCH 04/19] net/mlx5: support meter profile operations
Date: Wed,  6 Nov 2019 17:11:13 +0200	[thread overview]
Message-ID: <1573053090-179521-5-git-send-email-suanmingm@mellanox.com> (raw)
In-Reply-To: <1573053090-179521-1-git-send-email-suanmingm@mellanox.com>

This commit add the support of meter profile add and delete operations.

New internal functions in rte_mtr_ops callback:
1. meter_profile_add()
2. meter_profile_delete()

Only RTE_MTR_SRTCM_RFC2697 algorithm is supported and can be added. To
add other algorithm will report an error.

Signed-off-by: Suanming Mou <suanmingm@mellanox.com>
---
 drivers/net/mlx5/Makefile          |   1 +
 drivers/net/mlx5/mlx5.c            |   1 +
 drivers/net/mlx5/mlx5.h            |   4 +
 drivers/net/mlx5/mlx5_flow.h       |  29 +++++
 drivers/net/mlx5/mlx5_flow_meter.c | 245 ++++++++++++++++++++++++++++++++++++-
 5 files changed, 278 insertions(+), 2 deletions(-)

diff --git a/drivers/net/mlx5/Makefile b/drivers/net/mlx5/Makefile
index c60f97c..e8d6195 100644
--- a/drivers/net/mlx5/Makefile
+++ b/drivers/net/mlx5/Makefile
@@ -63,6 +63,7 @@ LDLIBS += $(shell $(RTE_SDK)/buildtools/options-ibverbs-static.sh)
 else
 LDLIBS += -libverbs -lmlx5
 endif
+LDLIBS += -lm
 LDLIBS += -lrte_eal -lrte_mbuf -lrte_mempool -lrte_ring
 LDLIBS += -lrte_ethdev -lrte_net -lrte_kvargs
 LDLIBS += -lrte_bus_pci
diff --git a/drivers/net/mlx5/mlx5.c b/drivers/net/mlx5/mlx5.c
index ebe748b..7958067 100644
--- a/drivers/net/mlx5/mlx5.c
+++ b/drivers/net/mlx5/mlx5.c
@@ -2405,6 +2405,7 @@ struct mlx5_flow_id_pool *
 		mlx5_nl_mac_addr_sync(eth_dev);
 	TAILQ_INIT(&priv->flows);
 	TAILQ_INIT(&priv->ctrl_flows);
+	TAILQ_INIT(&priv->flow_meter_profiles);
 	/* Hint libmlx5 to use PMD allocator for data plane resources */
 	struct mlx5dv_ctx_allocators alctr = {
 		.alloc = &mlx5_alloc_verbs_buf,
diff --git a/drivers/net/mlx5/mlx5.h b/drivers/net/mlx5/mlx5.h
index 650f2d1..b1d5341 100644
--- a/drivers/net/mlx5/mlx5.h
+++ b/drivers/net/mlx5/mlx5.h
@@ -682,6 +682,9 @@ struct mlx5_proc_priv {
 	/* Table of UAR registers for each process. */
 };
 
+/* MTR profile list. */
+TAILQ_HEAD(mlx5_mtr_profiles, mlx5_flow_meter_profile);
+
 #define MLX5_PROC_PRIV(port_id) \
 	((struct mlx5_proc_priv *)rte_eth_devices[port_id].process_private)
 
@@ -752,6 +755,7 @@ struct mlx5_priv {
 	/* Hash table of Rx metadata register copy table. */
 	uint8_t mtr_sfx_reg; /* Meter prefix-suffix flow match REG_C. */
 	uint8_t mtr_color_reg; /* Meter color match REG_C. */
+	struct mlx5_mtr_profiles flow_meter_profiles; /* MTR profile list. */
 #ifndef RTE_ARCH_64
 	rte_spinlock_t uar_lock_cq; /* CQs share a common distinct UAR */
 	rte_spinlock_t uar_lock[MLX5_UAR_PAGE_NUM_MAX];
diff --git a/drivers/net/mlx5/mlx5_flow.h b/drivers/net/mlx5/mlx5_flow.h
index 1ca7dd9..922d6b6 100644
--- a/drivers/net/mlx5/mlx5_flow.h
+++ b/drivers/net/mlx5/mlx5_flow.h
@@ -23,6 +23,7 @@
 
 #include <rte_atomic.h>
 #include <rte_alarm.h>
+#include <rte_mtr.h>
 
 #include "mlx5.h"
 #include "mlx5_prm.h"
@@ -522,6 +523,34 @@ struct mlx5_flow {
 	bool external; /**< true if the flow is created external to PMD. */
 };
 
+#define MLX5_MAN_WIDTH 8
+
+/* RFC2697 parameter structure. */
+struct mlx5_flow_meter_srtcm_rfc2697_prm {
+	/* green_saturation_value = cbs_mantissa * 2^cbs_exponent */
+	uint32_t cbs_exponent:5;
+	uint32_t cbs_mantissa:8;
+	/* cir = 8G * cir_mantissa * 1/(2^cir_exponent) Bytes/Sec */
+	uint32_t cir_exponent:5;
+	uint32_t cir_mantissa:8;
+	/* yellow _saturation_value = ebs_mantissa * 2^ebs_exponent */
+	uint32_t ebs_exponent:5;
+	uint32_t ebs_mantissa:8;
+};
+
+/* Flow meter profile structure. */
+struct mlx5_flow_meter_profile {
+	TAILQ_ENTRY(mlx5_flow_meter_profile) next;
+	/**< Pointer to the next flow meter structure. */
+	uint32_t meter_profile_id; /**< Profile id. */
+	struct rte_mtr_meter_profile profile; /**< Profile detail. */
+	union {
+		struct mlx5_flow_meter_srtcm_rfc2697_prm srtcm_prm;
+		/**< srtcm_rfc2697 struct. */
+	};
+	uint32_t ref_cnt; /**< Use count. */
+};
+
 /* Flow structure. */
 struct rte_flow {
 	TAILQ_ENTRY(rte_flow) next; /**< Pointer to the next flow structure. */
diff --git a/drivers/net/mlx5/mlx5_flow_meter.c b/drivers/net/mlx5/mlx5_flow_meter.c
index 9103b6d..b11962f 100644
--- a/drivers/net/mlx5/mlx5_flow_meter.c
+++ b/drivers/net/mlx5/mlx5_flow_meter.c
@@ -4,6 +4,7 @@
  */
 #include <math.h>
 
+#include <rte_malloc.h>
 #include <rte_mtr.h>
 #include <rte_mtr_driver.h>
 
@@ -11,6 +12,150 @@
 #include "mlx5_flow.h"
 
 /**
+ * Find meter profile by id.
+ *
+ * @param priv
+ *   Pointer to mlx5_priv.
+ * @param meter_profile_id
+ *   Meter profile id.
+ *
+ * @return
+ *   Pointer to the profile found on success, NULL otherwise.
+ */
+static struct mlx5_flow_meter_profile *
+mlx5_flow_meter_profile_find(struct mlx5_priv *priv, uint32_t meter_profile_id)
+{
+	struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
+	struct mlx5_flow_meter_profile *fmp;
+
+	TAILQ_FOREACH(fmp, fmps, next)
+		if (meter_profile_id == fmp->meter_profile_id)
+			return fmp;
+	return NULL;
+}
+
+/**
+ * Calculate mantissa and exponent for cir.
+ *
+ * @param[in] cir
+ *   Value to be calculated.
+ * @param[out] man
+ *   Pointer to the mantissa.
+ * @param[out] exp
+ *   Pointer to the exp.
+ */
+static void
+mlx5_flow_meter_cir_man_exp_calc(int64_t cir, uint8_t *man, uint8_t *exp)
+{
+	int64_t _cir;
+	int64_t delta = INT64_MAX;
+	uint8_t _man = 0;
+	uint8_t _exp = 0;
+	uint64_t m, e;
+
+	for (m = 0; m <= 0xFF; m++) { /* man width 8 bit */
+		for (e = 0; e <= 0x1F; e++) { /* exp width 5bit */
+			_cir = (1000000000ULL * m) >> e;
+			if (llabs(cir - _cir) <= delta) {
+				delta = llabs(cir - _cir);
+				_man = m;
+				_exp = e;
+			}
+		}
+	}
+	*man = _man;
+	*exp = _exp;
+}
+
+/**
+ * Calculate mantissa and exponent for xbs.
+ *
+ * @param[in] xbs
+ *   Value to be calculated.
+ * @param[out] man
+ *   Pointer to the mantissa.
+ * @param[out] exp
+ *   Pointer to the exp.
+ */
+static void
+mlx5_flow_meter_xbs_man_exp_calc(uint64_t xbs, uint8_t *man, uint8_t *exp)
+{
+	int _exp;
+	double _man;
+
+	/* Special case xbs == 0 ? both exp and matissa are 0. */
+	if (xbs == 0) {
+		*man = 0;
+		*exp = 0;
+		return;
+	}
+	/* xbs = xbs_mantissa * 2^xbs_exponent */
+	_man = frexp(xbs, &_exp);
+	_man = _man * pow(2, MLX5_MAN_WIDTH);
+	_exp = _exp - MLX5_MAN_WIDTH;
+	*man = (uint8_t)ceil(_man);
+	*exp = _exp;
+}
+
+/**
+ * Fill the prm meter parameter.
+ *
+ * @param[in,out] fmp
+ *   Pointer to meter profie to be converted.
+ * @param[out] error
+ *   Pointer to the error structure.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+static int
+mlx5_flow_meter_param_fill(struct mlx5_flow_meter_profile *fmp,
+			  struct rte_mtr_error *error)
+{
+	struct mlx5_flow_meter_srtcm_rfc2697_prm *srtcm = &fmp->srtcm_prm;
+	uint8_t man, exp;
+
+	if (fmp->profile.alg != RTE_MTR_SRTCM_RFC2697)
+		return -rte_mtr_error_set(error, ENOTSUP,
+				RTE_MTR_ERROR_TYPE_METER_PROFILE,
+				NULL, "Metering algorithm not supported.");
+	 /* cbs = cbs_mantissa * 2^cbs_exponent */
+	mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.cbs,
+				    &man, &exp);
+	srtcm->cbs_mantissa = man;
+	srtcm->cbs_exponent = exp;
+	/* Check if cbs mantissa is too large. */
+	if (srtcm->cbs_exponent != exp)
+		return -rte_mtr_error_set(error, EINVAL,
+					  RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
+					  "Metering profile parameter cbs is"
+					  " invalid.");
+	/* ebs = ebs_mantissa * 2^ebs_exponent */
+	mlx5_flow_meter_xbs_man_exp_calc(fmp->profile.srtcm_rfc2697.ebs,
+				    &man, &exp);
+	srtcm->ebs_mantissa = man;
+	srtcm->ebs_exponent = exp;
+	/* Check if ebs mantissa is too large. */
+	if (srtcm->ebs_exponent != exp)
+		return -rte_mtr_error_set(error, EINVAL,
+					  RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
+					  "Metering profile parameter ebs is"
+					  " invalid.");
+	/* cir = 8G * cir_mantissa * 1/(2^cir_exponent)) Bytes/Sec */
+	mlx5_flow_meter_cir_man_exp_calc(fmp->profile.srtcm_rfc2697.cir,
+				    &man, &exp);
+	srtcm->cir_mantissa = man;
+	srtcm->cir_exponent = exp;
+	/* Check if cir mantissa is too large. */
+	if (srtcm->cir_exponent != exp)
+		return -rte_mtr_error_set(error, EINVAL,
+					  RTE_MTR_ERROR_TYPE_MTR_PARAMS, NULL,
+					  "Metering profile parameter cir is"
+					  " invalid.");
+	return 0;
+}
+
+/**
  * Callback to get MTR capabilities.
  *
  * @param[in] dev
@@ -51,10 +196,106 @@
 	return 0;
 }
 
+/**
+ * Callback to add MTR profile.
+ *
+ * @param[in] dev
+ *   Pointer to Ethernet device.
+ * @param[in] meter_profile_id
+ *   Meter profile id.
+ * @param[in] profile
+ *   Pointer to meter profile detail.
+ * @param[out] error
+ *   Pointer to the error structure.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+static int
+mlx5_flow_meter_profile_add(struct rte_eth_dev *dev,
+		       uint32_t meter_profile_id,
+		       struct rte_mtr_meter_profile *profile,
+		       struct rte_mtr_error *error)
+{
+	struct mlx5_priv *priv = dev->data->dev_private;
+	struct mlx5_mtr_profiles *fmps = &priv->flow_meter_profiles;
+	struct mlx5_flow_meter_profile *fmp;
+	int ret;
+
+	if (!priv->mtr_en)
+		return -rte_mtr_error_set(error, ENOTSUP,
+					  RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "Meter is not support");
+	/* Meter profile memory allocation. */
+	fmp = rte_calloc(__func__, 1, sizeof(struct mlx5_flow_meter_profile),
+			 RTE_CACHE_LINE_SIZE);
+	if (fmp == NULL)
+		return -rte_mtr_error_set(error, ENOMEM,
+					  RTE_MTR_ERROR_TYPE_UNSPECIFIED,
+					  NULL, "Meter profile memory "
+					  "alloc failed.");
+	/* Fill profile info. */
+	fmp->meter_profile_id = meter_profile_id;
+	fmp->profile = *profile;
+	/* Fill the flow meter parameters for the PRM. */
+	ret = mlx5_flow_meter_param_fill(fmp, error);
+	if (ret)
+		goto error;
+	/* Add to list. */
+	TAILQ_INSERT_TAIL(fmps, fmp, next);
+	return 0;
+error:
+	rte_free(fmp);
+	return ret;
+}
+
+/**
+ * Callback to delete MTR profile.
+ *
+ * @param[in] dev
+ *   Pointer to Ethernet device.
+ * @param[in] meter_profile_id
+ *   Meter profile id.
+ * @param[out] error
+ *   Pointer to the error structure.
+ *
+ * @return
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
+ */
+static int
+mlx5_flow_meter_profile_delete(struct rte_eth_dev *dev,
+			  uint32_t meter_profile_id,
+			  struct rte_mtr_error *error)
+{
+	struct mlx5_priv *priv = dev->data->dev_private;
+	struct mlx5_flow_meter_profile *fmp;
+
+	if (!priv->mtr_en)
+		return -rte_mtr_error_set(error, ENOTSUP,
+					  RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "Meter is not support");
+	/* Meter profile must exist. */
+	fmp = mlx5_flow_meter_profile_find(priv, meter_profile_id);
+	if (fmp == NULL)
+		return -rte_mtr_error_set(error, ENOENT,
+					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
+					  &meter_profile_id,
+					  "Meter profile id invalid.");
+	/* Check profile is unused. */
+	if (fmp->ref_cnt)
+		return -rte_mtr_error_set(error, EBUSY,
+					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
+					  NULL, "Meter profile in use.");
+	/* Remove from list. */
+	TAILQ_REMOVE(&priv->flow_meter_profiles, fmp, next);
+	rte_free(fmp);
+	return 0;
+}
+
 static const struct rte_mtr_ops mlx5_flow_mtr_ops = {
 	.capabilities_get = mlx5_flow_mtr_cap_get,
-	.meter_profile_add = NULL,
-	.meter_profile_delete = NULL,
+	.meter_profile_add = mlx5_flow_meter_profile_add,
+	.meter_profile_delete = mlx5_flow_meter_profile_delete,
 	.create = NULL,
 	.destroy = NULL,
 	.meter_enable = NULL,
-- 
1.8.3.1


  parent reply	other threads:[~2019-11-06 15:12 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-06 15:11 [dpdk-dev] [PATCH 00/19] net/mlx5: support meter Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 01/19] net/mlx5: add meter operation callback Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 02/19] net/mlx5: fill meter capabilities using DevX Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 03/19] net/mlx5: allocate flow meter registers Suanming Mou
2019-11-06 15:11 ` Suanming Mou [this message]
2019-11-06 15:11 ` [dpdk-dev] [PATCH 05/19] net/mlx5: validate meter profile Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 06/19] net/mlx5: prepare meter flow tables Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 07/19] net/mlx5: add policer rules operations Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 08/19] net/mlx5: support basic meter operations Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 09/19] net/mlx5: add meter action creation to the glue Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 10/19] net/mlx5: support meter modification operations Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 11/19] net/mlx5: support meter profile update Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 12/19] net/mlx5: expose flow counters management Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 13/19] net/mlx5: add count action to meter Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 14/19] net/mlx5: add meter statistics read and update Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 15/19] net/mlx5: add meter attach and detach Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 16/19] net/mlx5: support meter flow action Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 17/19] net/mlx5: split meter flow Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 18/19] net/mlx5: share tag between meter and metadata Suanming Mou
2019-11-06 15:11 ` [dpdk-dev] [PATCH 19/19] net/mlx5: clean meter resources Suanming Mou
2019-11-08  3:49 ` [dpdk-dev] [PATCH v2 00/19] net/mlx5: support meter Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 01/19] net/mlx5: add meter operation callback Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 02/19] net/mlx5: fill meter capabilities using DevX Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 03/19] net/mlx5: allocate flow meter registers Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 04/19] net/mlx5: support meter profile operations Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 05/19] net/mlx5: validate meter profile Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 06/19] net/mlx5: prepare meter flow tables Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 07/19] net/mlx5: add policer rules operations Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 08/19] net/mlx5: support basic meter operations Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 09/19] net/mlx5: add meter action creation to the glue Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 10/19] net/mlx5: support meter modification operations Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 11/19] net/mlx5: support meter profile update Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 12/19] net/mlx5: expose flow counters management Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 13/19] net/mlx5: add count action to meter Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 14/19] net/mlx5: add meter statistics read and update Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 15/19] net/mlx5: add meter attach and detach Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 16/19] net/mlx5: support meter flow action Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 17/19] net/mlx5: split meter flow Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 18/19] net/mlx5: share tag between meter and metadata Suanming Mou
2019-11-08  3:49   ` [dpdk-dev] [PATCH v2 19/19] net/mlx5: clean meter resources Suanming Mou
2019-11-08  6:20   ` [dpdk-dev] [PATCH v2 00/19] net/mlx5: support meter Matan Azrad
2019-11-08 14:22   ` Raslan Darawsheh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1573053090-179521-5-git-send-email-suanmingm@mellanox.com \
    --to=suanmingm@mellanox.com \
    --cc=dev@dpdk.org \
    --cc=matan@mellanox.com \
    --cc=shahafs@mellanox.com \
    --cc=viacheslavo@mellanox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.