Skip to contentSkip to conversation

common/cnxk: add RTE Flow support for SPI to SA index

PATCH Superseded [email protected] 2 participants 7 messages Dec 1, 2022 – Jan 11, 2023 Network drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 common/cnxk: add RTE Flow support for SPI to SA index

From: Satheesh Paul <[email protected]>

In case of IPsec, the inbound SPI can be random. HW supports mapping SPI to an arbitrary SA index. SPI to SA index is done using a lookup in NPC cam entry with key as SPI, MATCH_ID, LFID. Adding Mbox API changes to configure the match table. And adding RTE FLow changes to program the match table.

---
 drivers/common/cnxk/roc_mbox.h      | 28 +++++++++-
 drivers/common/cnxk/roc_npc.c       | 87 ++++++++++++++++++++++++-----
 drivers/common/cnxk/roc_npc.h       | 19 +++++++
 drivers/common/cnxk/roc_npc_parse.c |  4 ++
 4 files changed, 123 insertions(+), 15 deletions(-)
drivers/common/cnxk/roc_mbox.h +27 -1
index 8b0384c737..0989bddc3b 100644
@@ -268,7 +268,11 @@ struct mbox_msghdr {
 	M(NIX_READ_INLINE_IPSEC_CFG, 0x8023, nix_read_inline_ipsec_cfg,        \
 	  msg_req, nix_inline_ipsec_cfg)				       \
 	M(NIX_LF_INLINE_RQ_CFG, 0x8024, nix_lf_inline_rq_cfg,                  \
-	  nix_rq_cpt_field_mask_cfg_req, msg_rsp)
+	  nix_rq_cpt_field_mask_cfg_req, msg_rsp)                              \
+	M(NIX_SPI_TO_SA_ADD, 0x8026, nix_spi_to_sa_add, nix_spi_to_sa_add_req, \
+	  nix_spi_to_sa_add_rsp)                                               \
+	M(NIX_SPI_TO_SA_DELETE, 0x8027, nix_spi_to_sa_delete,                  \
+	  nix_spi_to_sa_delete_req, msg_rsp)
 
 /* Messages initiated by AF (range 0xC00 - 0xDFF) */
 #define MBOX_UP_CGX_MESSAGES                                                   \
@@ -2048,4 +2052,26 @@ struct sdp_chan_info_msg {
 	struct sdp_node_info info;
 };
 
+/* For SPI to SA index add */
+struct nix_spi_to_sa_add_req {
+	struct mbox_msghdr hdr;
+	uint32_t __io sa_index;
+	uint32_t __io spi_index;
+	uint16_t __io match_id;
+	bool __io valid;
+};
+
+struct nix_spi_to_sa_add_rsp {
+	struct mbox_msghdr hdr;
+	uint16_t __io hash_index;
+	uint8_t __io way;
+	uint8_t __io is_duplicate;
+};
+
+/* To free SPI to SA index */
+struct nix_spi_to_sa_delete_req {
+	struct mbox_msghdr hdr;
+	uint16_t __io hash_index;
+	uint8_t __io way;
+};
 #endif /* __ROC_MBOX_H__ */
drivers/common/cnxk/roc_npc.c +73 -14

151 lines not on this page · raw message

drivers/common/cnxk/roc_npc.h +19 -0
index 1b4e5521cb..80d70dd4c4 100644
@@ -191,6 +191,14 @@ struct roc_npc_action_port_id {
 	uint32_t id;		/**< port ID. */
 };
 
+/**
+ * ESP Header
+ */
+struct roc_npc_item_esp_hdr {
+	uint32_t spi; /**< Security Parameters Index */
+	uint32_t seq; /**< packet sequence number */
+};
+
 struct roc_npc_action_queue {
 	uint16_t index; /**< Queue index to use. */
 };
@@ -242,6 +250,14 @@ struct roc_npc_flow_dump_data {
 	uint16_t ltype;
 };
 
+struct roc_npc_msns_action_info {
+	uint32_t spi;
+	uint32_t hash_index;
+	uint8_t way;
+	bool duplicate;
+	bool has_action;
+};
+
 struct roc_npc_flow {
 	uint8_t nix_intf;
 	uint8_t enable;
@@ -261,6 +277,9 @@ struct roc_npc_flow {
 #define ROC_NPC_MAX_FLOW_PATTERNS 32
 	struct roc_npc_flow_dump_data dump_data[ROC_NPC_MAX_FLOW_PATTERNS];
 	uint16_t num_patterns;
+	struct roc_npc_msns_action_info msns_info;
+	bool is_validate;
+	uint16_t match_id;
 
 	TAILQ_ENTRY(roc_npc_flow) next;
 };
drivers/common/cnxk/roc_npc_parse.c +4 -0
index 947e1ec53d..746a79a836 100644
@@ -731,6 +731,7 @@ int
 npc_parse_le(struct npc_parse_state *pst)
 {
 	const struct roc_npc_item_info *pattern = pst->pattern;
+	const struct roc_npc_item_esp_hdr *esp = NULL;
 	char hw_mask[NPC_MAX_EXTRACT_HW_LEN];
 	struct npc_parse_item_info info;
 	int lid, lt, lflags;
@@ -787,6 +788,9 @@ npc_parse_le(struct npc_parse_state *pst)
 	case ROC_NPC_ITEM_TYPE_ESP:
 		lt = NPC_LT_LE_ESP;
 		info.len = pst->pattern->size;
+		esp = (const struct roc_npc_item_esp_hdr *)pattern->spec;
+		if (esp)
+			pst->flow->msns_info.spi = esp->spi;
 		break;
 	default:
 		return 0;
Signature · 1 line
2.35.3
#1 lore raw

PATCH 2/2 net/cnxk: add validate flag for RTE flow for cnxk

From: Kiran Kumar K <[email protected]>

Add a flag to check flow validate. In case of MSNS don't need to configure the API to SA translation while validating the flow. Skip this with a check.

---
 drivers/net/cnxk/cnxk_flow.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/cnxk/cnxk_flow.c +5 -4
index 6d155d924c..bf4c55f00a 100644
@@ -299,12 +299,13 @@ cnxk_flow_validate(struct rte_eth_dev *eth_dev,
 	int rc;
 
 	memset(&flow, 0, sizeof(flow));
+	flow.is_validate = true;
 
-	rc = cnxk_map_flow_data(eth_dev, attr, pattern, actions, &in_attr,
-				in_pattern, in_actions, &flowkey_cfg);
+	rc = cnxk_map_flow_data(eth_dev, attr, pattern, actions, &in_attr, in_pattern, in_actions,
+				&flowkey_cfg);
 	if (rc) {
-		rte_flow_error_set(error, 0, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
-				   NULL, "Failed to map flow data");
+		rte_flow_error_set(error, 0, RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
+				   "Failed to map flow data");
 		return rc;
 	}
 
Signature · 1 line
2.35.3
#2 lore raw

PATCH v2 1/4 common/cnxk: support SPI to SA index

In reply to [email protected]

From: Satheesh Paul <[email protected]>

In case of IPsec, the inbound SPI can be random. HW supports mapping SPI to an arbitrary SA index. SPI to SA index is done using a lookup in NPC cam entry with key as SPI, MATCH_ID, LFID. Adding mailbox API changes to configure the match table and adding rte flow driver changes to program the match table.

---
v2:
* Addressed naming related comment.
* Added dependent patches in this series.

 drivers/common/cnxk/roc_mbox.h      | 28 ++++++++-
 drivers/common/cnxk/roc_npc.c       | 93 +++++++++++++++++++++++------
 drivers/common/cnxk/roc_npc.h       | 19 ++++++
 drivers/common/cnxk/roc_npc_parse.c |  4 ++
 4 files changed, 126 insertions(+), 18 deletions(-)
drivers/common/cnxk/roc_mbox.h +27 -1
index 8b0384c737..0989bddc3b 100644
@@ -268,7 +268,11 @@ struct mbox_msghdr {
 	M(NIX_READ_INLINE_IPSEC_CFG, 0x8023, nix_read_inline_ipsec_cfg,        \
 	  msg_req, nix_inline_ipsec_cfg)				       \
 	M(NIX_LF_INLINE_RQ_CFG, 0x8024, nix_lf_inline_rq_cfg,                  \
-	  nix_rq_cpt_field_mask_cfg_req, msg_rsp)
+	  nix_rq_cpt_field_mask_cfg_req, msg_rsp)                              \
+	M(NIX_SPI_TO_SA_ADD, 0x8026, nix_spi_to_sa_add, nix_spi_to_sa_add_req, \
+	  nix_spi_to_sa_add_rsp)                                               \
+	M(NIX_SPI_TO_SA_DELETE, 0x8027, nix_spi_to_sa_delete,                  \
+	  nix_spi_to_sa_delete_req, msg_rsp)
 
 /* Messages initiated by AF (range 0xC00 - 0xDFF) */
 #define MBOX_UP_CGX_MESSAGES                                                   \
@@ -2048,4 +2052,26 @@ struct sdp_chan_info_msg {
 	struct sdp_node_info info;
 };
 
+/* For SPI to SA index add */
+struct nix_spi_to_sa_add_req {
+	struct mbox_msghdr hdr;
+	uint32_t __io sa_index;
+	uint32_t __io spi_index;
+	uint16_t __io match_id;
+	bool __io valid;
+};
+
+struct nix_spi_to_sa_add_rsp {
+	struct mbox_msghdr hdr;
+	uint16_t __io hash_index;
+	uint8_t __io way;
+	uint8_t __io is_duplicate;
+};
+
+/* To free SPI to SA index */
+struct nix_spi_to_sa_delete_req {
+	struct mbox_msghdr hdr;
+	uint16_t __io hash_index;
+	uint8_t __io way;
+};
 #endif /* __ROC_MBOX_H__ */
drivers/common/cnxk/roc_npc.c +76 -17

170 lines not on this page · raw message

drivers/common/cnxk/roc_npc.h +19 -0
index 1b4e5521cb..da5639e812 100644
@@ -191,6 +191,14 @@ struct roc_npc_action_port_id {
 	uint32_t id;		/**< port ID. */
 };
 
+/**
+ * ESP Header
+ */
+struct roc_npc_item_esp_hdr {
+	uint32_t spi; /**< Security Parameters Index */
+	uint32_t seq; /**< packet sequence number */
+};
+
 struct roc_npc_action_queue {
 	uint16_t index; /**< Queue index to use. */
 };
@@ -242,6 +250,14 @@ struct roc_npc_flow_dump_data {
 	uint16_t ltype;
 };
 
+struct roc_npc_spi_to_sa_action_info {
+	uint32_t spi;
+	uint32_t hash_index;
+	uint8_t way;
+	bool duplicate;
+	bool has_action;
+};
+
 struct roc_npc_flow {
 	uint8_t nix_intf;
 	uint8_t enable;
@@ -261,6 +277,9 @@ struct roc_npc_flow {
 #define ROC_NPC_MAX_FLOW_PATTERNS 32
 	struct roc_npc_flow_dump_data dump_data[ROC_NPC_MAX_FLOW_PATTERNS];
 	uint16_t num_patterns;
+	struct roc_npc_spi_to_sa_action_info spi_to_sa_info;
+	bool is_validate;
+	uint16_t match_id;
 
 	TAILQ_ENTRY(roc_npc_flow) next;
 };
drivers/common/cnxk/roc_npc_parse.c +4 -0
index 947e1ec53d..c1af5f3087 100644
@@ -731,6 +731,7 @@ int
 npc_parse_le(struct npc_parse_state *pst)
 {
 	const struct roc_npc_item_info *pattern = pst->pattern;
+	const struct roc_npc_item_esp_hdr *esp = NULL;
 	char hw_mask[NPC_MAX_EXTRACT_HW_LEN];
 	struct npc_parse_item_info info;
 	int lid, lt, lflags;
@@ -787,6 +788,9 @@ npc_parse_le(struct npc_parse_state *pst)
 	case ROC_NPC_ITEM_TYPE_ESP:
 		lt = NPC_LT_LE_ESP;
 		info.len = pst->pattern->size;
+		esp = (const struct roc_npc_item_esp_hdr *)pattern->spec;
+		if (esp)
+			pst->flow->spi_to_sa_info.spi = esp->spi;
 		break;
 	default:
 		return 0;
Signature · 1 line
2.35.3
#3 lore raw

PATCH v2 2/4 net/cnxk: introduce flag for flow validate

From: Kiran Kumar K <[email protected]>

Add a flag to check flow validate. In case of MSNS don't need to configure the API to SA translation while validating the flow. Skip this with a check.

---
 drivers/net/cnxk/cnxk_flow.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/cnxk/cnxk_flow.c +5 -4
index 6d155d924c..bf4c55f00a 100644
@@ -299,12 +299,13 @@ cnxk_flow_validate(struct rte_eth_dev *eth_dev,
 	int rc;
 
 	memset(&flow, 0, sizeof(flow));
+	flow.is_validate = true;
 
-	rc = cnxk_map_flow_data(eth_dev, attr, pattern, actions, &in_attr,
-				in_pattern, in_actions, &flowkey_cfg);
+	rc = cnxk_map_flow_data(eth_dev, attr, pattern, actions, &in_attr, in_pattern, in_actions,
+				&flowkey_cfg);
 	if (rc) {
-		rte_flow_error_set(error, 0, RTE_FLOW_ERROR_TYPE_ACTION_NUM,
-				   NULL, "Failed to map flow data");
+		rte_flow_error_set(error, 0, RTE_FLOW_ERROR_TYPE_ACTION_NUM, NULL,
+				   "Failed to map flow data");
 		return rc;
 	}
 
Signature · 1 line
2.35.3
#4 lore raw

PATCH v2 3/4 drivers: add IPsec rule reservation scheme for cnxk

In reply to [email protected]

From: Kiran Kumar K <[email protected]>

For inline IPsec device, reserve number of rules specified by ``max_ipsec_rules`` and use them while installing rules with action as security. Rule priority should be 0. If specified number of rules not available, then only available number of rules will be allocated and used. If application try to insert more then allocated rules, flow creation will fail.

---
 doc/guides/nics/cnxk.rst               |  18 +++++
 drivers/common/cnxk/roc_nix_inl.h      |   1 +
 drivers/common/cnxk/roc_nix_inl_dev.c  |  35 +++++++-
 drivers/common/cnxk/roc_nix_inl_priv.h |   6 ++
 drivers/common/cnxk/roc_npc.c          | 107 +++++++++++++++++++++----
 drivers/common/cnxk/roc_npc.h          |   4 +
 drivers/common/cnxk/roc_npc_mcam.c     |  92 +++++++++++++--------
 drivers/common/cnxk/roc_npc_priv.h     |  15 ++--
 drivers/common/cnxk/version.map        |   2 +
 drivers/net/cnxk/cnxk_ethdev_sec.c     |  23 +++++-
 drivers/net/cnxk/cnxk_flow.c           |  13 ++-
 11 files changed, 257 insertions(+), 59 deletions(-)
doc/guides/nics/cnxk.rst +18 -0
index be176b53a2..c7fb05850a 100644
@@ -348,6 +348,24 @@ Runtime Config Options
    set with this custom mask, inbound encrypted traffic from all ports with
    matching channel number pattern will be directed to the inline IPSec device.
 
+- ``Inline IPsec device flow rules`` (default ``none``)
+
+   For inline IPsec device, reserve number of rules specified by ``max_ipsec_rules``
+   and use them while installing rules with action as security. Rule priority should
+   be 0. If specified number of rules not available, then only available number
+   of rules will be allocated and used. If application try to insert more then
+   allocated rules, flow creation will fail.
+
+   For example::
+
+      -a 0002:1d:00.0,max_ipsec_rules=100
+
+   With the above configuration, 100 rules will be allocated from 0-99 if available
+   and will be used for rules with action security. If 100 rules are not available,
+   and only 50 are available, then only 50 rules will be allocated and used for
+   flow rule creation. If application try to add more than 50 rules, the flow
+   create will fail.
+
 - ``SDP device channel and mask`` (default ``none``)
    Set channel and channel mask configuration for the SDP device. This
    will be used when creating flow rules on the SDP device.
drivers/common/cnxk/roc_nix_inl.h +1 -0
index c537262819..99d1281169 100644
@@ -186,6 +186,7 @@ struct roc_nix_inl_dev {
 	uint32_t soft_exp_poll_freq; /* Polling disabled if 0 */
 	uint32_t nb_meta_bufs;
 	uint32_t meta_buf_sz;
+	uint32_t max_ipsec_rules;
 	/* End of input parameters */
 
 #define ROC_NIX_INL_MEM_SZ (1280)
drivers/common/cnxk/roc_nix_inl_dev.c +34 -1
index 4ab4209dba..0578afc3b7 100644
@@ -807,7 +807,9 @@ roc_nix_inl_dev_init(struct roc_nix_inl_dev *roc_inl_dev)
 	struct plt_pci_device *pci_dev;
 	struct nix_inl_dev *inl_dev;
 	struct idev_cfg *idev;
-	int rc;
+	int start_index;
+	int resp_count;
+	int rc, i;
 
 	pci_dev = roc_inl_dev->pci_dev;
 
@@ -890,6 +892,30 @@ roc_nix_inl_dev_init(struct roc_nix_inl_dev *roc_inl_dev)
 		if (rc)
 			goto cpt_release;
 	}
+	inl_dev->max_ipsec_rules = roc_inl_dev->max_ipsec_rules;
+
+	if (inl_dev->max_ipsec_rules && roc_inl_dev->is_multi_channel) {
+		inl_dev->ipsec_index =
+			plt_zmalloc(sizeof(int) * inl_dev->max_ipsec_rules, PLT_CACHE_LINE_SIZE);
+		if (inl_dev->ipsec_index == NULL) {
+			rc = NPC_ERR_NO_MEM;
+			goto cpt_release;
+		}
+		rc = npc_mcam_alloc_entries(inl_dev->dev.mbox, inl_dev->max_ipsec_rules,
+					    inl_dev->ipsec_index, inl_dev->max_ipsec_rules,
+					    NPC_MCAM_HIGHER_PRIO, &resp_count, 1);
+		if (rc) {
+			plt_free(inl_dev->ipsec_index);
+			goto cpt_release;
+		}
+
+		start_index = inl_dev->ipsec_index[0];
+		for (i = 0; i < resp_count; i++)
+			inl_dev->ipsec_index[i] = start_index + i;
+
+		inl_dev->curr_ipsec_idx = 0;
+		inl_dev->alloc_ipsec_rules = resp_count;
+	}
 
 	idev->nix_inl_dev = inl_dev;
 
@@ -914,6 +940,7 @@ roc_nix_inl_dev_fini(struct roc_nix_inl_dev *roc_inl_dev)
 	struct plt_pci_device *pci_dev;
 	struct nix_inl_dev *inl_dev;
 	struct idev_cfg *idev;
+	uint32_t i;
 	int rc;
 
 	idev = idev_get_cfg();
@@ -927,6 +954,12 @@ roc_nix_inl_dev_fini(struct roc_nix_inl_dev *roc_inl_dev)
 	inl_dev = idev->nix_inl_dev;
 	pci_dev = inl_dev->pci_dev;
 
+	if (inl_dev->ipsec_index && roc_inl_dev->is_multi_channel) {
+		for (i = inl_dev->curr_ipsec_idx; i < inl_dev->alloc_ipsec_rules; i++)
+			npc_mcam_free_entry(inl_dev->dev.mbox, inl_dev->ipsec_index[i]);
+		plt_free(inl_dev->ipsec_index);
+	}
+
 	if (inl_dev->set_soft_exp_poll) {
 		soft_exp_poll_thread_exit = true;
 		pthread_join(inl_dev->soft_exp_poll_thread, NULL);
drivers/common/cnxk/roc_nix_inl_priv.h +6 -0
index ccd2adf982..528d2db365 100644
@@ -90,6 +90,12 @@ struct nix_inl_dev {
 	bool ts_ena;
 	uint32_t nb_meta_bufs;
 	uint32_t meta_buf_sz;
+
+	/* NPC */
+	int *ipsec_index;
+	uint32_t curr_ipsec_idx;
+	uint32_t max_ipsec_rules;
+	uint32_t alloc_ipsec_rules;
 };
 
 int nix_inl_sso_register_irqs(struct nix_inl_dev *inl_dev);
drivers/common/cnxk/roc_npc.c +93 -14

189 lines not on this page · raw message

drivers/common/cnxk/roc_npc.h +4 -0
index da5639e812..c7e8b4ac92 100644
@@ -280,6 +280,8 @@ struct roc_npc_flow {
 	struct roc_npc_spi_to_sa_action_info spi_to_sa_info;
 	bool is_validate;
 	uint16_t match_id;
+	uint8_t is_inline_dev;
+	bool use_pre_alloc;
 
 	TAILQ_ENTRY(roc_npc_flow) next;
 };
@@ -378,6 +380,8 @@ int __roc_api roc_npc_mcam_read_counter(struct roc_npc *roc_npc,
 					uint32_t ctr_id, uint64_t *count);
 int __roc_api roc_npc_mcam_clear_counter(struct roc_npc *roc_npc,
 					 uint32_t ctr_id);
+int __roc_api roc_npc_inl_mcam_read_counter(uint32_t ctr_id, uint64_t *count);
+int __roc_api roc_npc_inl_mcam_clear_counter(uint32_t ctr_id);
 int __roc_api roc_npc_mcam_free_all_resources(struct roc_npc *roc_npc);
 void __roc_api roc_npc_flow_dump(FILE *file, struct roc_npc *roc_npc);
 void __roc_api roc_npc_flow_mcam_dump(FILE *file, struct roc_npc *roc_npc,
drivers/common/cnxk/roc_npc_mcam.c +59 -33

220 lines not on this page · raw message

drivers/common/cnxk/roc_npc_priv.h +8 -7
index 09a727b13e..fe19329a7f 100644
@@ -396,6 +396,7 @@ struct npc {
 	struct npc_flow_list *flow_list;
 	struct npc_prio_flow_list_head *prio_flow_list;
 	struct plt_bitmap *rss_grp_entries;
+	struct npc_flow_list ipsec_list;
 };
 
 static inline struct npc *
@@ -404,22 +405,22 @@ roc_npc_to_npc_priv(struct roc_npc *npc)
 	return (struct npc *)npc->reserved;
 }
 
-int npc_mcam_free_counter(struct npc *npc, uint16_t ctr_id);
-int npc_mcam_read_counter(struct npc *npc, uint32_t ctr_id, uint64_t *count);
-int npc_mcam_clear_counter(struct npc *npc, uint32_t ctr_id);
-int npc_mcam_free_entry(struct npc *npc, uint32_t entry);
+int npc_mcam_free_counter(struct mbox *mbox, uint16_t ctr_id);
+int npc_mcam_read_counter(struct mbox *mbox, uint32_t ctr_id, uint64_t *count);
+int npc_mcam_clear_counter(struct mbox *mbox, uint32_t ctr_id);
+int npc_mcam_free_entry(struct mbox *mbox, uint32_t entry);
 int npc_mcam_free_all_entries(struct npc *npc);
 int npc_mcam_alloc_and_write(struct npc *npc, struct roc_npc_flow *flow,
 			     struct npc_parse_state *pst);
 int npc_mcam_alloc_entry(struct npc *npc, struct roc_npc_flow *mcam,
 			 struct roc_npc_flow *ref_mcam, int prio,
 			 int *resp_count);
-int npc_mcam_alloc_entries(struct npc *npc, int ref_mcam, int *alloc_entry,
-			   int req_count, int prio, int *resp_count);
+int npc_mcam_alloc_entries(struct mbox *mbox, int ref_mcam, int *alloc_entry, int req_count,
+			   int prio, int *resp_count, bool is_conti);
 
 int npc_mcam_ena_dis_entry(struct npc *npc, struct roc_npc_flow *mcam,
 			   bool enable);
-int npc_mcam_write_entry(struct npc *npc, struct roc_npc_flow *mcam);
+int npc_mcam_write_entry(struct mbox *mbox, struct roc_npc_flow *mcam);
 int npc_flow_enable_all_entries(struct npc *npc, bool enable);
 int npc_update_parse_state(struct npc_parse_state *pst,
 			   struct npc_parse_item_info *info, int lid, int lt,
drivers/common/cnxk/version.map +2 -0
index 63fe9deb72..4d500a5ddb 100644
@@ -344,6 +344,7 @@ INTERNAL {
 	roc_npc_mcam_alloc_entries;
 	roc_npc_mcam_alloc_entry;
 	roc_npc_mcam_clear_counter;
+	roc_npc_inl_mcam_clear_counter;
 	roc_npc_mcam_enable_all_entries;
 	roc_npc_mcam_ena_dis_entry;
 	roc_npc_mcam_free_all_resources;
@@ -355,6 +356,7 @@ INTERNAL {
 	roc_npc_mcam_merge_base_steering_rule;
 	roc_npc_mcam_write_entry;
 	roc_npc_mcam_read_counter;
+	roc_npc_inl_mcam_read_counter;
 	roc_npc_profile_name_get;
 	roc_npc_validate_portid_action;
 	roc_ot_ipsec_inb_sa_init;
drivers/net/cnxk/cnxk_ethdev_sec.c +22 -1
index 8bec9acb54..6c71f9554b 100644
@@ -13,6 +13,7 @@
 #define CNXK_NIX_INL_NB_META_BUFS     "nb_meta_bufs"
 #define CNXK_NIX_INL_META_BUF_SZ      "meta_buf_sz"
 #define CNXK_NIX_SOFT_EXP_POLL_FREQ   "soft_exp_poll_freq"
+#define CNXK_MAX_IPSEC_RULES	"max_ipsec_rules"
 
 /* Default soft expiry poll freq in usec */
 #define CNXK_NIX_SOFT_EXP_POLL_FREQ_DFLT 100
@@ -115,6 +116,22 @@ cnxk_nix_inl_meta_pool_cb(uint64_t *aura_handle, uint32_t buf_sz, uint32_t nb_bu
 	return rc;
 }
 
+static int
+parse_max_ipsec_rules(const char *key, const char *value, void *extra_args)
+{
+	RTE_SET_USED(key);
+	uint32_t val;
+
+	val = atoi(value);
+
+	if (val < 1 || val > 4095)
+		return -EINVAL;
+
+	*(uint32_t *)extra_args = val;
+
+	return 0;
+}
+
 int
 cnxk_eth_outb_sa_idx_get(struct cnxk_eth_dev *dev, uint32_t *idx_p,
 			 uint32_t spi)
@@ -271,6 +288,7 @@ nix_inl_parse_devargs(struct rte_devargs *devargs,
 	uint32_t ipsec_in_max_spi = BIT(8) - 1;
 	uint32_t ipsec_in_min_spi = 0;
 	struct inl_cpt_channel cpt_channel;
+	uint32_t max_ipsec_rules = 0;
 	struct rte_kvargs *kvlist;
 	uint32_t nb_meta_bufs = 0;
 	uint32_t meta_buf_sz = 0;
@@ -299,6 +317,7 @@ nix_inl_parse_devargs(struct rte_devargs *devargs,
 			   &meta_buf_sz);
 	rte_kvargs_process(kvlist, CNXK_NIX_SOFT_EXP_POLL_FREQ,
 			   &parse_val_u32, &soft_exp_poll_freq);
+	rte_kvargs_process(kvlist, CNXK_MAX_IPSEC_RULES, &parse_max_ipsec_rules, &max_ipsec_rules);
 	rte_kvargs_free(kvlist);
 
 null_devargs:
@@ -311,6 +330,7 @@ nix_inl_parse_devargs(struct rte_devargs *devargs,
 	inl_dev->nb_meta_bufs = nb_meta_bufs;
 	inl_dev->meta_buf_sz = meta_buf_sz;
 	inl_dev->soft_exp_poll_freq = soft_exp_poll_freq;
+	inl_dev->max_ipsec_rules = max_ipsec_rules;
 	return 0;
 exit:
 	return -EINVAL;
@@ -437,4 +457,5 @@ RTE_PMD_REGISTER_PARAM_STRING(cnxk_nix_inl,
 			      CNXK_INL_CPT_CHANNEL "=<1-4095>/<1-4095>"
 			      CNXK_NIX_INL_NB_META_BUFS "=<1-U32_MAX>"
 			      CNXK_NIX_INL_META_BUF_SZ "=<1-U32_MAX>"
-			      CNXK_NIX_SOFT_EXP_POLL_FREQ "=<0-U32_MAX>");
+			      CNXK_NIX_SOFT_EXP_POLL_FREQ "=<0-U32_MAX>"
+			      CNXK_MAX_IPSEC_RULES "=<1-4095>");
drivers/net/cnxk/cnxk_flow.c +10 -3
index bf4c55f00a..f13d8e5582 100644
@@ -409,7 +409,10 @@ cnxk_flow_query(struct rte_eth_dev *eth_dev, struct rte_flow *flow,
 		goto err_exit;
 	}
 
-	rc = roc_npc_mcam_read_counter(npc, in_flow->ctr_id, &query->hits);
+	if (in_flow->use_pre_alloc)
+		rc = roc_npc_inl_mcam_read_counter(in_flow->ctr_id, &query->hits);
+	else
+		rc = roc_npc_mcam_read_counter(npc, in_flow->ctr_id, &query->hits);
 	if (rc != 0) {
 		errcode = EIO;
 		errmsg = "Error reading flow counter";
@@ -418,8 +421,12 @@ cnxk_flow_query(struct rte_eth_dev *eth_dev, struct rte_flow *flow,
 	query->hits_set = 1;
 	query->bytes_set = 0;
 
-	if (query->reset)
-		rc = roc_npc_mcam_clear_counter(npc, in_flow->ctr_id);
+	if (query->reset) {
+		if (in_flow->use_pre_alloc)
+			rc = roc_npc_inl_mcam_clear_counter(in_flow->ctr_id);
+		else
+			rc = roc_npc_mcam_clear_counter(npc, in_flow->ctr_id);
+	}
 	if (rc != 0) {
 		errcode = EIO;
 		errmsg = "Error clearing flow counter";
Signature · 1 line
2.35.3
#5 lore raw

PATCH v2 4/4 common/cnxk: fix dual VLAN parsing issue

In reply to [email protected]

From: Satheesh Paul <[email protected]>

Flow parsing was not handling pattern matching on the fields of inner VLAN even though MKEX profile could be extracting inner VLAN fields. Code has been modified to handle matching fields on two VLAN tags.

Fixes c34ea71b878 ("common/cnxk: add NPC parsing API")
---
 drivers/common/cnxk/roc_npc_parse.c | 240 +++++++++++++++++++++-------
 drivers/common/cnxk/roc_npc_priv.h  |  16 +-
 drivers/common/cnxk/roc_npc_utils.c |   2 +-
 3 files changed, 186 insertions(+), 72 deletions(-)
drivers/common/cnxk/roc_npc_parse.c +178 -62
index c1af5f3087..d8f9271fa8 100644
@@ -239,10 +239,185 @@ npc_parse_la(struct npc_parse_state *pst)
 
 #define NPC_MAX_SUPPORTED_VLANS 3
 
+static int
+npc_parse_vlan_count(const struct roc_npc_item_info *pattern,
+		     const struct roc_npc_item_info **pattern_list,
+		     const struct roc_npc_flow_item_vlan **vlan_items, int *vlan_count)
+{
+	*vlan_count = 0;
+	while (pattern->type == ROC_NPC_ITEM_TYPE_VLAN) {
+		if (*vlan_count > NPC_MAX_SUPPORTED_VLANS - 1)
+			return NPC_ERR_PATTERN_NOTSUP;
+
+		/* Don't support ranges */
+		if (pattern->last != NULL)
+			return NPC_ERR_INVALID_RANGE;
+
+		/* If spec is NULL, both mask and last must be NULL, this
+		 * makes it to match ANY value (eq to mask = 0).
+		 * Setting either mask or last without spec is an error
+		 */
+		if (pattern->spec == NULL) {
+			if (pattern->last != NULL && pattern->mask != NULL)
+				return NPC_ERR_INVALID_SPEC;
+		}
+
+		pattern_list[*vlan_count] = pattern;
+		vlan_items[*vlan_count] = pattern->spec;
+		(*vlan_count)++;
+
+		pattern++;
+		pattern = npc_parse_skip_void_and_any_items(pattern);
+	}
+
+	return 0;
+}
+
+static int
+npc_parse_vlan_ltype_get(struct npc_parse_state *pst,
+			 const struct roc_npc_flow_item_vlan **vlan_item, int vlan_count,
+			 int *ltype, int *lflags)
+{
+	switch (vlan_count) {
+	case 1:
+		*ltype = NPC_LT_LB_CTAG;
+		if (vlan_item[0] && vlan_item[0]->has_more_vlan)
+			*ltype = NPC_LT_LB_STAG_QINQ;
+		break;
+	case 2:
+		if (vlan_item[1] && vlan_item[1]->has_more_vlan) {
+			if (!(pst->npc->keyx_supp_nmask[pst->nix_intf] &
+			      0x3ULL << NPC_LFLAG_LB_OFFSET))
+				return NPC_ERR_PATTERN_NOTSUP;
+
+			/* This lflag value will match either one of
+			 * NPC_F_LB_L_WITH_STAG_STAG,
+			 * NPC_F_LB_L_WITH_QINQ_CTAG,
+			 * NPC_F_LB_L_WITH_QINQ_QINQ and
+			 * NPC_F_LB_L_WITH_ITAG (0b0100 to 0b0111). For
+			 * NPC_F_LB_L_WITH_ITAG, ltype is NPC_LT_LB_ETAG
+			 * hence will not match.
+			 */
+
+			*lflags = NPC_F_LB_L_WITH_QINQ_CTAG & NPC_F_LB_L_WITH_QINQ_QINQ &
+				  NPC_F_LB_L_WITH_STAG_STAG;
+		}
+		*ltype = NPC_LT_LB_STAG_QINQ;
+		break;
+	case 3:
+		if (vlan_item[2] && vlan_item[2]->has_more_vlan)
+			return NPC_ERR_PATTERN_NOTSUP;
+		if (!(pst->npc->keyx_supp_nmask[pst->nix_intf] & 0x3ULL << NPC_LFLAG_LB_OFFSET))
+			return NPC_ERR_PATTERN_NOTSUP;
+		*ltype = NPC_LT_LB_STAG_QINQ;
+		*lflags = NPC_F_STAG_STAG_CTAG;
+		break;
+	default:
+		return NPC_ERR_PATTERN_NOTSUP;
+	}
+
+	return 0;
+}
+
+static int
+npc_update_vlan_parse_state(struct npc_parse_state *pst, const struct roc_npc_item_info *pattern,
+			    int lid, int lt, uint8_t lflags, int vlan_count)
+{
+	uint8_t vlan_spec[NPC_MAX_SUPPORTED_VLANS * sizeof(struct roc_vlan_hdr)];
+	uint8_t vlan_mask[NPC_MAX_SUPPORTED_VLANS * sizeof(struct roc_vlan_hdr)];
+	int rc = 0, i, offset = NPC_TPID_LENGTH;
+	struct npc_parse_item_info parse_info;
+	char hw_mask[NPC_MAX_EXTRACT_HW_LEN];
+
+	memset(vlan_spec, 0, sizeof(struct roc_vlan_hdr) * NPC_MAX_SUPPORTED_VLANS);
+	memset(vlan_mask, 0, sizeof(struct roc_vlan_hdr) * NPC_MAX_SUPPORTED_VLANS);
+	memset(&parse_info, 0, sizeof(parse_info));
+
+	if (vlan_count > 2)
+		vlan_count = 2;
+
+	for (i = 0; i < vlan_count; i++) {
+		if (pattern[i].spec)
+			memcpy(vlan_spec + offset, pattern[i].spec, sizeof(struct roc_vlan_hdr));
+		if (pattern[i].mask)
+			memcpy(vlan_mask + offset, pattern[i].mask, sizeof(struct roc_vlan_hdr));
+
+		offset += 4;
+	}
+
+	parse_info.def_mask = NULL;
+	parse_info.spec = vlan_spec;
+	parse_info.mask = vlan_mask;
+	parse_info.def_mask = NULL;
+	parse_info.hw_hdr_len = 0;
+
+	lid = NPC_LID_LB;
+	parse_info.hw_mask = hw_mask;
+
+	if (lt == NPC_LT_LB_CTAG)
+		parse_info.len = sizeof(struct roc_vlan_hdr) + NPC_TPID_LENGTH;
+
+	if (lt == NPC_LT_LB_STAG_QINQ)
+		parse_info.len = sizeof(struct roc_vlan_hdr) * 2 + NPC_TPID_LENGTH;
+
+	memset(hw_mask, 0, sizeof(hw_mask));
+
+	parse_info.hw_mask = &hw_mask;
+	npc_get_hw_supp_mask(pst, &parse_info, lid, lt);
+
+	rc = npc_mask_is_supported(parse_info.mask, parse_info.hw_mask, parse_info.len);
+	if (!rc)
+		return NPC_ERR_INVALID_MASK;
+
+	/* Point pattern to last item consumed */
+	pst->pattern = pattern;
+	return npc_update_parse_state(pst, &parse_info, lid, lt, lflags);
+}
+
+static int
+npc_parse_lb_vlan(struct npc_parse_state *pst)
+{
+	const struct roc_npc_flow_item_vlan *vlan_items[NPC_MAX_SUPPORTED_VLANS];
+	const struct roc_npc_item_info *pattern_list[NPC_MAX_SUPPORTED_VLANS];
+	const struct roc_npc_item_info *last_pattern;
+	int vlan_count = 0, rc = 0;
+	int lid, lt, lflags;
+
+	lid = NPC_LID_LB;
+	lflags = 0;
+	last_pattern = pst->pattern;
+
+	rc = npc_parse_vlan_count(pst->pattern, pattern_list, vlan_items, &vlan_count);
+	if (rc)
+		return rc;
+
+	rc = npc_parse_vlan_ltype_get(pst, vlan_items, vlan_count, &lt, &lflags);
+	if (rc)
+		return rc;
+
+	if (vlan_count == 3) {
+		if (pattern_list[2]->spec != NULL && pattern_list[2]->mask != NULL &&
+		    pattern_list[2]->last != NULL)
+			return NPC_ERR_PATTERN_NOTSUP;
+
+		/* Matching can be done only for two tags. */
+		vlan_count = 2;
+		last_pattern++;
+	}
+
+	rc = npc_update_vlan_parse_state(pst, pattern_list[0], lid, lt, lflags, vlan_count);
+	if (rc)
+		return rc;
+
+	if (vlan_count > 1)
+		pst->pattern = last_pattern + vlan_count;
+
+	return 0;
+}
+
 int
 npc_parse_lb(struct npc_parse_state *pst)
 {
-	const struct roc_npc_flow_item_vlan *vlan_item[NPC_MAX_SUPPORTED_VLANS];
 	const struct roc_npc_item_info *pattern = pst->pattern;
 	const struct roc_npc_item_info *last_pattern;
 	const struct roc_npc_flow_item_raw *raw_spec;
@@ -251,7 +426,6 @@ npc_parse_lb(struct npc_parse_state *pst)
 	char hw_mask[NPC_MAX_EXTRACT_HW_LEN];
 	struct npc_parse_item_info info;
 	int lid, lt, lflags, len = 0;
-	int nr_vlans = 0;
 	int rc;
 
 	info.def_mask = NULL;
@@ -268,68 +442,10 @@ npc_parse_lb(struct npc_parse_state *pst)
 		/* RTE vlan is either 802.1q or 802.1ad,
 		 * this maps to either CTAG/STAG. We need to decide
 		 * based on number of VLANS present. Matching is
-		 * supported on first tag only.
+		 * supported on first two tags.
 		 */
-		info.hw_mask = NULL;
-		info.len = sizeof(vlan_item[0]->hdr);
-
-		pattern = pst->pattern;
-		while (pattern->type == ROC_NPC_ITEM_TYPE_VLAN) {
-			if (nr_vlans > NPC_MAX_SUPPORTED_VLANS - 1)
-				return NPC_ERR_PATTERN_NOTSUP;
-
-			vlan_item[nr_vlans] = pattern->spec;
-			nr_vlans++;
 
-			/* Basic validation of Second/Third vlan item */
-			if (nr_vlans > 1) {
-				rc = npc_parse_item_basic(pattern, &info);
-				if (rc != 0)
-					return rc;
-			}
-			last_pattern = pattern;
-			pattern++;
-			pattern = npc_parse_skip_void_and_any_items(pattern);
-		}
-
-		switch (nr_vlans) {
-		case 1:
-			lt = NPC_LT_LB_CTAG;
-			if (vlan_item[0] && vlan_item[0]->has_more_vlan)
-				lt = NPC_LT_LB_STAG_QINQ;
-			break;
-		case 2:
-			if (vlan_item[1] && vlan_item[1]->has_more_vlan) {
-				if (!(pst->npc->keyx_supp_nmask[pst->nix_intf] &
-				      0x3ULL << NPC_LFLAG_LB_OFFSET))
-					return NPC_ERR_PATTERN_NOTSUP;
-
-				/* This lflag value will match either one of
-				 * NPC_F_LB_L_WITH_STAG_STAG,
-				 * NPC_F_LB_L_WITH_QINQ_CTAG,
-				 * NPC_F_LB_L_WITH_QINQ_QINQ and
-				 * NPC_F_LB_L_WITH_ITAG (0b0100 to 0b0111). For
-				 * NPC_F_LB_L_WITH_ITAG, ltype is NPC_LT_LB_ETAG
-				 * hence will not match.
-				 */
-
-				lflags = NPC_F_LB_L_WITH_QINQ_CTAG &
-					 NPC_F_LB_L_WITH_QINQ_QINQ &
-					 NPC_F_LB_L_WITH_STAG_STAG;
-			} else {
-				lflags = NPC_F_LB_L_WITH_CTAG;
-			}
-			lt = NPC_LT_LB_STAG_QINQ;
-			break;
-		case 3:
-			if (vlan_item[2] && vlan_item[2]->has_more_vlan)
-				return NPC_ERR_PATTERN_NOTSUP;
-			lt = NPC_LT_LB_STAG_QINQ;
-			lflags = NPC_F_STAG_STAG_CTAG;
-			break;
-		default:
-			return NPC_ERR_PATTERN_NOTSUP;
-		}
+		return npc_parse_lb_vlan(pst);
 	} else if (pst->pattern->type == ROC_NPC_ITEM_TYPE_E_TAG) {
 		/* we can support ETAG and match a subsequent CTAG
 		 * without any matching support.
drivers/common/cnxk/roc_npc_priv.h +7 -9
index fe19329a7f..2a7d3137fb 100644
@@ -418,17 +418,15 @@ int npc_mcam_alloc_entry(struct npc *npc, struct roc_npc_flow *mcam,
 int npc_mcam_alloc_entries(struct mbox *mbox, int ref_mcam, int *alloc_entry, int req_count,
 			   int prio, int *resp_count, bool is_conti);
 
-int npc_mcam_ena_dis_entry(struct npc *npc, struct roc_npc_flow *mcam,
-			   bool enable);
+int npc_mcam_ena_dis_entry(struct npc *npc, struct roc_npc_flow *mcam, bool enable);
 int npc_mcam_write_entry(struct mbox *mbox, struct roc_npc_flow *mcam);
 int npc_flow_enable_all_entries(struct npc *npc, bool enable);
-int npc_update_parse_state(struct npc_parse_state *pst,
-			   struct npc_parse_item_info *info, int lid, int lt,
-			   uint8_t flags);
-void npc_get_hw_supp_mask(struct npc_parse_state *pst,
-			  struct npc_parse_item_info *info, int lid, int lt);
-int npc_parse_item_basic(const struct roc_npc_item_info *item,
-			 struct npc_parse_item_info *info);
+int npc_update_parse_state(struct npc_parse_state *pst, struct npc_parse_item_info *info, int lid,
+			   int lt, uint8_t flags);
+void npc_get_hw_supp_mask(struct npc_parse_state *pst, struct npc_parse_item_info *info, int lid,
+			  int lt);
+int npc_mask_is_supported(const char *mask, const char *hw_mask, int len);
+int npc_parse_item_basic(const struct roc_npc_item_info *item, struct npc_parse_item_info *info);
 int npc_parse_meta_items(struct npc_parse_state *pst);
 int npc_parse_mark_item(struct npc_parse_state *pst);
 int npc_parse_pre_l2(struct npc_parse_state *pst);
drivers/common/cnxk/roc_npc_utils.c +1 -1
index 8bdabc116d..fda3073cba 100644
@@ -88,7 +88,7 @@ npc_get_hw_supp_mask(struct npc_parse_state *pst,
 	}
 }
 
-static inline int
+inline int
 npc_mask_is_supported(const char *mask, const char *hw_mask, int len)
 {
 	/*
Signature · 1 line
2.35.3
#6 lore raw

Re: [dpdk-dev] [PATCH v2 4/4] common/cnxk: fix dual VLAN parsing issue

Quoted 8 lines · On Wed, Jan 11, 2023 at 11:09 AM <[email protected]> wrote:

From: Satheesh Paul <[email protected]>

Flow parsing was not handling pattern matching on the fields of inner VLAN even though MKEX profile could be extracting inner VLAN fields. Code has been modified to handle matching fields on two VLAN tags.

Quoted 5 lines
Fixes c34ea71b878 ("common/cnxk: add NPC parsing API")

Series applied to dpdk-next-net-mrvl/for-next-net. Thanks

Quoted 321 lines
---
 drivers/common/cnxk/roc_npc_parse.c | 240 +++++++++++++++++++++-------
 drivers/common/cnxk/roc_npc_priv.h  |  16 +-
 drivers/common/cnxk/roc_npc_utils.c |   2 +-
 3 files changed, 186 insertions(+), 72 deletions(-)
drivers/common/cnxk/roc_npc_parse.c +178 -62
index c1af5f3087..d8f9271fa8 100644
@@ -239,10 +239,185 @@ npc_parse_la(struct npc_parse_state *pst)
 
 #define NPC_MAX_SUPPORTED_VLANS 3
 
+static int
+npc_parse_vlan_count(const struct roc_npc_item_info *pattern,
+                    const struct roc_npc_item_info **pattern_list,
+                    const struct roc_npc_flow_item_vlan **vlan_items, int *vlan_count)
+{
+       *vlan_count = 0;
+       while (pattern->type == ROC_NPC_ITEM_TYPE_VLAN) {
+               if (*vlan_count > NPC_MAX_SUPPORTED_VLANS - 1)
+                       return NPC_ERR_PATTERN_NOTSUP;
+
+               /* Don't support ranges */
+               if (pattern->last != NULL)
+                       return NPC_ERR_INVALID_RANGE;
+
+               /* If spec is NULL, both mask and last must be NULL, this
+                * makes it to match ANY value (eq to mask = 0).
+                * Setting either mask or last without spec is an error
+                */
+               if (pattern->spec == NULL) {
+                       if (pattern->last != NULL && pattern->mask != NULL)
+                               return NPC_ERR_INVALID_SPEC;
+               }
+
+               pattern_list[*vlan_count] = pattern;
+               vlan_items[*vlan_count] = pattern->spec;
+               (*vlan_count)++;
+
+               pattern++;
+               pattern = npc_parse_skip_void_and_any_items(pattern);
+       }
+
+       return 0;
+}
+
+static int
+npc_parse_vlan_ltype_get(struct npc_parse_state *pst,
+                        const struct roc_npc_flow_item_vlan **vlan_item, int vlan_count,
+                        int *ltype, int *lflags)
+{
+       switch (vlan_count) {
+       case 1:
+               *ltype = NPC_LT_LB_CTAG;
+               if (vlan_item[0] && vlan_item[0]->has_more_vlan)
+                       *ltype = NPC_LT_LB_STAG_QINQ;
+               break;
+       case 2:
+               if (vlan_item[1] && vlan_item[1]->has_more_vlan) {
+                       if (!(pst->npc->keyx_supp_nmask[pst->nix_intf] &
+                             0x3ULL << NPC_LFLAG_LB_OFFSET))
+                               return NPC_ERR_PATTERN_NOTSUP;
+
+                       /* This lflag value will match either one of
+                        * NPC_F_LB_L_WITH_STAG_STAG,
+                        * NPC_F_LB_L_WITH_QINQ_CTAG,
+                        * NPC_F_LB_L_WITH_QINQ_QINQ and
+                        * NPC_F_LB_L_WITH_ITAG (0b0100 to 0b0111). For
+                        * NPC_F_LB_L_WITH_ITAG, ltype is NPC_LT_LB_ETAG
+                        * hence will not match.
+                        */
+
+                       *lflags = NPC_F_LB_L_WITH_QINQ_CTAG & NPC_F_LB_L_WITH_QINQ_QINQ &
+                                 NPC_F_LB_L_WITH_STAG_STAG;
+               }
+               *ltype = NPC_LT_LB_STAG_QINQ;
+               break;
+       case 3:
+               if (vlan_item[2] && vlan_item[2]->has_more_vlan)
+                       return NPC_ERR_PATTERN_NOTSUP;
+               if (!(pst->npc->keyx_supp_nmask[pst->nix_intf] & 0x3ULL << NPC_LFLAG_LB_OFFSET))
+                       return NPC_ERR_PATTERN_NOTSUP;
+               *ltype = NPC_LT_LB_STAG_QINQ;
+               *lflags = NPC_F_STAG_STAG_CTAG;
+               break;
+       default:
+               return NPC_ERR_PATTERN_NOTSUP;
+       }
+
+       return 0;
+}
+
+static int
+npc_update_vlan_parse_state(struct npc_parse_state *pst, const struct roc_npc_item_info *pattern,
+                           int lid, int lt, uint8_t lflags, int vlan_count)
+{
+       uint8_t vlan_spec[NPC_MAX_SUPPORTED_VLANS * sizeof(struct roc_vlan_hdr)];
+       uint8_t vlan_mask[NPC_MAX_SUPPORTED_VLANS * sizeof(struct roc_vlan_hdr)];
+       int rc = 0, i, offset = NPC_TPID_LENGTH;
+       struct npc_parse_item_info parse_info;
+       char hw_mask[NPC_MAX_EXTRACT_HW_LEN];
+
+       memset(vlan_spec, 0, sizeof(struct roc_vlan_hdr) * NPC_MAX_SUPPORTED_VLANS);
+       memset(vlan_mask, 0, sizeof(struct roc_vlan_hdr) * NPC_MAX_SUPPORTED_VLANS);
+       memset(&parse_info, 0, sizeof(parse_info));
+
+       if (vlan_count > 2)
+               vlan_count = 2;
+
+       for (i = 0; i < vlan_count; i++) {
+               if (pattern[i].spec)
+                       memcpy(vlan_spec + offset, pattern[i].spec, sizeof(struct roc_vlan_hdr));
+               if (pattern[i].mask)
+                       memcpy(vlan_mask + offset, pattern[i].mask, sizeof(struct roc_vlan_hdr));
+
+               offset += 4;
+       }
+
+       parse_info.def_mask = NULL;
+       parse_info.spec = vlan_spec;
+       parse_info.mask = vlan_mask;
+       parse_info.def_mask = NULL;
+       parse_info.hw_hdr_len = 0;
+
+       lid = NPC_LID_LB;
+       parse_info.hw_mask = hw_mask;
+
+       if (lt == NPC_LT_LB_CTAG)
+               parse_info.len = sizeof(struct roc_vlan_hdr) + NPC_TPID_LENGTH;
+
+       if (lt == NPC_LT_LB_STAG_QINQ)
+               parse_info.len = sizeof(struct roc_vlan_hdr) * 2 + NPC_TPID_LENGTH;
+
+       memset(hw_mask, 0, sizeof(hw_mask));
+
+       parse_info.hw_mask = &hw_mask;
+       npc_get_hw_supp_mask(pst, &parse_info, lid, lt);
+
+       rc = npc_mask_is_supported(parse_info.mask, parse_info.hw_mask, parse_info.len);
+       if (!rc)
+               return NPC_ERR_INVALID_MASK;
+
+       /* Point pattern to last item consumed */
+       pst->pattern = pattern;
+       return npc_update_parse_state(pst, &parse_info, lid, lt, lflags);
+}
+
+static int
+npc_parse_lb_vlan(struct npc_parse_state *pst)
+{
+       const struct roc_npc_flow_item_vlan *vlan_items[NPC_MAX_SUPPORTED_VLANS];
+       const struct roc_npc_item_info *pattern_list[NPC_MAX_SUPPORTED_VLANS];
+       const struct roc_npc_item_info *last_pattern;
+       int vlan_count = 0, rc = 0;
+       int lid, lt, lflags;
+
+       lid = NPC_LID_LB;
+       lflags = 0;
+       last_pattern = pst->pattern;
+
+       rc = npc_parse_vlan_count(pst->pattern, pattern_list, vlan_items, &vlan_count);
+       if (rc)
+               return rc;
+
+       rc = npc_parse_vlan_ltype_get(pst, vlan_items, vlan_count, &lt, &lflags);
+       if (rc)
+               return rc;
+
+       if (vlan_count == 3) {
+               if (pattern_list[2]->spec != NULL && pattern_list[2]->mask != NULL &&
+                   pattern_list[2]->last != NULL)
+                       return NPC_ERR_PATTERN_NOTSUP;
+
+               /* Matching can be done only for two tags. */
+               vlan_count = 2;
+               last_pattern++;
+       }
+
+       rc = npc_update_vlan_parse_state(pst, pattern_list[0], lid, lt, lflags, vlan_count);
+       if (rc)
+               return rc;
+
+       if (vlan_count > 1)
+               pst->pattern = last_pattern + vlan_count;
+
+       return 0;
+}
+
 int
 npc_parse_lb(struct npc_parse_state *pst)
 {
-       const struct roc_npc_flow_item_vlan *vlan_item[NPC_MAX_SUPPORTED_VLANS];
        const struct roc_npc_item_info *pattern = pst->pattern;
        const struct roc_npc_item_info *last_pattern;
        const struct roc_npc_flow_item_raw *raw_spec;
@@ -251,7 +426,6 @@ npc_parse_lb(struct npc_parse_state *pst)
        char hw_mask[NPC_MAX_EXTRACT_HW_LEN];
        struct npc_parse_item_info info;
        int lid, lt, lflags, len = 0;
-       int nr_vlans = 0;
        int rc;
 
        info.def_mask = NULL;
@@ -268,68 +442,10 @@ npc_parse_lb(struct npc_parse_state *pst)
                /* RTE vlan is either 802.1q or 802.1ad,
                 * this maps to either CTAG/STAG. We need to decide
                 * based on number of VLANS present. Matching is
-                * supported on first tag only.
+                * supported on first two tags.
                 */
-               info.hw_mask = NULL;
-               info.len = sizeof(vlan_item[0]->hdr);
-
-               pattern = pst->pattern;
-               while (pattern->type == ROC_NPC_ITEM_TYPE_VLAN) {
-                       if (nr_vlans > NPC_MAX_SUPPORTED_VLANS - 1)
-                               return NPC_ERR_PATTERN_NOTSUP;
-
-                       vlan_item[nr_vlans] = pattern->spec;
-                       nr_vlans++;
 
-                       /* Basic validation of Second/Third vlan item */
-                       if (nr_vlans > 1) {
-                               rc = npc_parse_item_basic(pattern, &info);
-                               if (rc != 0)
-                                       return rc;
-                       }
-                       last_pattern = pattern;
-                       pattern++;
-                       pattern = npc_parse_skip_void_and_any_items(pattern);
-               }
-
-               switch (nr_vlans) {
-               case 1:
-                       lt = NPC_LT_LB_CTAG;
-                       if (vlan_item[0] && vlan_item[0]->has_more_vlan)
-                               lt = NPC_LT_LB_STAG_QINQ;
-                       break;
-               case 2:
-                       if (vlan_item[1] && vlan_item[1]->has_more_vlan) {
-                               if (!(pst->npc->keyx_supp_nmask[pst->nix_intf] &
-                                     0x3ULL << NPC_LFLAG_LB_OFFSET))
-                                       return NPC_ERR_PATTERN_NOTSUP;
-
-                               /* This lflag value will match either one of
-                                * NPC_F_LB_L_WITH_STAG_STAG,
-                                * NPC_F_LB_L_WITH_QINQ_CTAG,
-                                * NPC_F_LB_L_WITH_QINQ_QINQ and
-                                * NPC_F_LB_L_WITH_ITAG (0b0100 to 0b0111). For
-                                * NPC_F_LB_L_WITH_ITAG, ltype is NPC_LT_LB_ETAG
-                                * hence will not match.
-                                */
-
-                               lflags = NPC_F_LB_L_WITH_QINQ_CTAG &
-                                        NPC_F_LB_L_WITH_QINQ_QINQ &
-                                        NPC_F_LB_L_WITH_STAG_STAG;
-                       } else {
-                               lflags = NPC_F_LB_L_WITH_CTAG;
-                       }
-                       lt = NPC_LT_LB_STAG_QINQ;
-                       break;
-               case 3:
-                       if (vlan_item[2] && vlan_item[2]->has_more_vlan)
-                               return NPC_ERR_PATTERN_NOTSUP;
-                       lt = NPC_LT_LB_STAG_QINQ;
-                       lflags = NPC_F_STAG_STAG_CTAG;
-                       break;
-               default:
-                       return NPC_ERR_PATTERN_NOTSUP;
-               }
+               return npc_parse_lb_vlan(pst);
        } else if (pst->pattern->type == ROC_NPC_ITEM_TYPE_E_TAG) {
                /* we can support ETAG and match a subsequent CTAG
                 * without any matching support.
drivers/common/cnxk/roc_npc_priv.h +7 -9
index fe19329a7f..2a7d3137fb 100644
@@ -418,17 +418,15 @@ int npc_mcam_alloc_entry(struct npc *npc, struct roc_npc_flow *mcam,
 int npc_mcam_alloc_entries(struct mbox *mbox, int ref_mcam, int *alloc_entry, int req_count,
                           int prio, int *resp_count, bool is_conti);
 
-int npc_mcam_ena_dis_entry(struct npc *npc, struct roc_npc_flow *mcam,
-                          bool enable);
+int npc_mcam_ena_dis_entry(struct npc *npc, struct roc_npc_flow *mcam, bool enable);
 int npc_mcam_write_entry(struct mbox *mbox, struct roc_npc_flow *mcam);
 int npc_flow_enable_all_entries(struct npc *npc, bool enable);
-int npc_update_parse_state(struct npc_parse_state *pst,
-                          struct npc_parse_item_info *info, int lid, int lt,
-                          uint8_t flags);
-void npc_get_hw_supp_mask(struct npc_parse_state *pst,
-                         struct npc_parse_item_info *info, int lid, int lt);
-int npc_parse_item_basic(const struct roc_npc_item_info *item,
-                        struct npc_parse_item_info *info);
+int npc_update_parse_state(struct npc_parse_state *pst, struct npc_parse_item_info *info, int lid,
+                          int lt, uint8_t flags);
+void npc_get_hw_supp_mask(struct npc_parse_state *pst, struct npc_parse_item_info *info, int lid,
+                         int lt);
+int npc_mask_is_supported(const char *mask, const char *hw_mask, int len);
+int npc_parse_item_basic(const struct roc_npc_item_info *item, struct npc_parse_item_info *info);
 int npc_parse_meta_items(struct npc_parse_state *pst);
 int npc_parse_mark_item(struct npc_parse_state *pst);
 int npc_parse_pre_l2(struct npc_parse_state *pst);
drivers/common/cnxk/roc_npc_utils.c +1 -2
index 8bdabc116d..fda3073cba 100644
@@ -88,7 +88,7 @@ npc_get_hw_supp_mask(struct npc_parse_state *pst,
        }
 }
 
-static inline int
+inline int
 npc_mask_is_supported(const char *mask, const char *hw_mask, int len)
 {
        /*
--

2.35.3