[17/27] net/cnxk: support ops to create meter profile

Message ID 20210906075450.1452123-17-skori@marvell.com (mailing list archive)
State Superseded, archived
Delegated to: Jerin Jacob
Headers
Series [01/27] common/cnxk: update policer MBOX APIs and HW definitions |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Sunil Kumar Kori Sept. 6, 2021, 7:54 a.m. UTC
  From: Sunil Kumar Kori <skori@marvell.com>

Implement API to add meter profile for CN10K platform.

Signed-off-by: Sunil Kumar Kori <skori@marvell.com>
---
 drivers/net/cnxk/cn10k_ethdev_mtr.c | 104 ++++++++++++++++++++++++++++
 drivers/net/cnxk/cnxk_ethdev.h      |  13 ++++
 2 files changed, 117 insertions(+)
  

Patch

diff --git a/drivers/net/cnxk/cn10k_ethdev_mtr.c b/drivers/net/cnxk/cn10k_ethdev_mtr.c
index bf97bf4a71..404431fb8c 100644
--- a/drivers/net/cnxk/cn10k_ethdev_mtr.c
+++ b/drivers/net/cnxk/cn10k_ethdev_mtr.c
@@ -37,6 +37,71 @@  static struct rte_mtr_capabilities mtr_capa = {
 		      RTE_MTR_STATS_N_BYTES_YELLOW | RTE_MTR_STATS_N_BYTES_RED |
 		      RTE_MTR_STATS_N_BYTES_DROPPED};
 
+static struct cn10k_flow_meter_profile *
+nix_mtr_profile_find(struct cnxk_eth_dev *dev, uint32_t profile_id)
+{
+	struct cn10k_mtr_profiles *fmps = &dev->mtr_profiles;
+	struct cn10k_flow_meter_profile *fmp;
+
+	TAILQ_FOREACH(fmp, fmps, next)
+		if (profile_id == fmp->id)
+			return fmp;
+
+	return NULL;
+}
+
+static int
+nix_mtr_profile_validate(struct cnxk_eth_dev *dev, uint32_t profile_id,
+			 struct rte_mtr_meter_profile *profile,
+			 struct rte_mtr_error *error)
+{
+	int rc = 0;
+
+	PLT_SET_USED(dev);
+
+	if (profile == NULL)
+		return -rte_mtr_error_set(error, EINVAL,
+					  RTE_MTR_ERROR_TYPE_METER_PROFILE,
+					  NULL, "Meter profile is null.");
+
+	if (profile_id == UINT32_MAX)
+		return -rte_mtr_error_set(error, EINVAL,
+					  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID,
+					  NULL, "Meter profile id not valid.");
+
+	switch (profile->alg) {
+	case RTE_MTR_SRTCM_RFC2697:
+		if (profile->srtcm_rfc2697.cir > mtr_capa.meter_rate_max)
+			rc = -rte_mtr_error_set(error, EINVAL,
+				RTE_MTR_ERROR_TYPE_METER_PROFILE, NULL,
+				"CIR exceeds max meter rate");
+		break;
+
+	case RTE_MTR_TRTCM_RFC2698:
+		if (profile->trtcm_rfc2698.pir > mtr_capa.meter_rate_max)
+			rc = -rte_mtr_error_set(error, EINVAL,
+				RTE_MTR_ERROR_TYPE_METER_PROFILE, NULL,
+				"PIR exceeds max meter rate");
+		break;
+
+	case RTE_MTR_TRTCM_RFC4115:
+		if ((profile->trtcm_rfc4115.cir + profile->trtcm_rfc4115.eir) >
+		    mtr_capa.meter_rate_max)
+			rc = -rte_mtr_error_set(error, EINVAL,
+				RTE_MTR_ERROR_TYPE_METER_PROFILE, NULL,
+				"PIR + EIR exceeds max rate");
+		break;
+
+	default:
+		rc = -rte_mtr_error_set(error, EINVAL,
+					RTE_MTR_ERROR_TYPE_METER_PROFILE, NULL,
+					"alg is invalid");
+		break;
+	}
+
+	return rc;
+}
+
 static int
 cn10k_nix_mtr_capabilities_get(struct rte_eth_dev *dev,
 			       struct rte_mtr_capabilities *capa,
@@ -52,8 +117,47 @@  cn10k_nix_mtr_capabilities_get(struct rte_eth_dev *dev,
 	return 0;
 }
 
+static int
+cn10k_nix_mtr_profile_add(struct rte_eth_dev *eth_dev, uint32_t profile_id,
+			  struct rte_mtr_meter_profile *profile,
+			  struct rte_mtr_error *error)
+{
+	struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
+	struct cn10k_mtr_profiles *fmps = &dev->mtr_profiles;
+	struct cn10k_flow_meter_profile *fmp;
+	int ret;
+
+	/* Check input params. */
+	ret = nix_mtr_profile_validate(dev, profile_id, profile, error);
+	if (ret)
+		return ret;
+
+	fmp = nix_mtr_profile_find(dev, profile_id);
+	if (fmp) {
+		rte_mtr_error_set(error, EEXIST,
+				  RTE_MTR_ERROR_TYPE_METER_PROFILE_ID, NULL,
+				  "Profile already exist");
+		return 0;
+	}
+
+	fmp = plt_zmalloc(sizeof(struct cn10k_flow_meter_profile), ROC_ALIGN);
+	if (fmp == NULL)
+		return -rte_mtr_error_set(error, ENOMEM,
+					  RTE_MTR_ERROR_TYPE_UNSPECIFIED, NULL,
+					  "Meter profile memory "
+					  "alloc failed.");
+
+	fmp->id = profile_id;
+	fmp->profile = *profile;
+
+	TAILQ_INSERT_TAIL(fmps, fmp, next);
+
+	return 0;
+}
+
 const struct rte_mtr_ops nix_mtr_ops = {
 	.capabilities_get = cn10k_nix_mtr_capabilities_get,
+	.meter_profile_add = cn10k_nix_mtr_profile_add,
 };
 
 int
diff --git a/drivers/net/cnxk/cnxk_ethdev.h b/drivers/net/cnxk/cnxk_ethdev.h
index 2528b3cdaa..5d7cf58a24 100644
--- a/drivers/net/cnxk/cnxk_ethdev.h
+++ b/drivers/net/cnxk/cnxk_ethdev.h
@@ -13,6 +13,7 @@ 
 #include <rte_mbuf.h>
 #include <rte_mbuf_pool_ops.h>
 #include <rte_mempool.h>
+#include <rte_mtr_driver.h>
 #include <rte_time.h>
 
 #include "roc_api.h"
@@ -144,6 +145,15 @@  struct cnxk_timesync_info {
 	uint64_t *tx_tstamp;
 } __plt_cache_aligned;
 
+struct cn10k_flow_meter_profile {
+	TAILQ_ENTRY(cn10k_flow_meter_profile) next;
+	struct rte_mtr_meter_profile profile; /**< Profile detail. */
+	uint32_t ref_cnt;		      /**< Use count. */
+	uint32_t id;			      /**< Profile id. */
+};
+
+TAILQ_HEAD(cn10k_mtr_profiles, cn10k_flow_meter_profile);
+
 struct cnxk_eth_dev {
 	/* ROC NIX */
 	struct roc_nix nix;
@@ -211,6 +221,9 @@  struct cnxk_eth_dev {
 	double clk_freq_mult;
 	uint64_t clk_delta;
 
+	/* Ingress policer */
+	struct cn10k_mtr_profiles mtr_profiles;
+
 	/* Rx burst for cleanup(Only Primary) */
 	eth_rx_burst_t rx_pkt_burst_no_offload;