DPDK patches and discussions
 help / color / mirror / Atom feed
From: alvinx.zhang@intel.com
To: dev@dpdk.org
Cc: haiyue.wang@intel.com, xiaolong.ye@intel.com,
	qi.z.zhang@intel.com, beilei.xing@intel.com,
	Alvin Zhang <alvinx.zhang@intel.com>
Subject: [dpdk-dev] [PATCH v1 12/15] net/igc: implement 2-tuple filter
Date: Mon,  9 Mar 2020 16:24:04 +0800	[thread overview]
Message-ID: <1583742247-370386-12-git-send-email-alvinx.zhang@intel.com> (raw)
In-Reply-To: <1583742247-370386-1-git-send-email-alvinx.zhang@intel.com>

From: Alvin Zhang <alvinx.zhang@intel.com>

Add L3 protocol type and L4 destination port filter.

Signed-off-by: Alvin Zhang <alvinx.zhang@intel.com>
---
 drivers/net/igc/igc_ethdev.h |  38 +++++
 drivers/net/igc/igc_filter.c | 341 +++++++++++++++++++++++++++++++++++++++++++
 drivers/net/igc/igc_filter.h |   3 +
 3 files changed, 382 insertions(+)

diff --git a/drivers/net/igc/igc_ethdev.h b/drivers/net/igc/igc_ethdev.h
index 0880380..639782c 100644
--- a/drivers/net/igc/igc_ethdev.h
+++ b/drivers/net/igc/igc_ethdev.h
@@ -99,6 +99,9 @@
 #define IGC_GET_QUEUE_FROM_ETQF(_etqf)	\
 	((uint8_t)(((_etqf) & IGC_ETQF_QUEUE_MASK) >> IGC_ETQF_QUEUE_SHIFT))
 
+#define IGC_MAX_2TUPLE_FILTERS		8
+#define IGC_2TUPLE_MAX_PRI		7
+
 /* structure for interrupt relative data */
 struct igc_interrupt {
 	uint32_t flags;
@@ -139,6 +142,40 @@ struct igc_ethertype_filter {
 	uint32_t etqf;
 };
 
+/* Structure of 2-tuple filter info. */
+struct igc_2tuple_info {
+	uint16_t dst_port;
+	uint8_t proto;           /* l4 protocol. */
+
+	/*
+	 * the packet matched above 2tuple and contain any set bit will hit
+	 * this filter.
+	 */
+	uint8_t tcp_flags;
+
+	/*
+	 * seven levels (001b-111b), 111b is highest, used when more than one
+	 * filter matches.
+	 */
+	uint8_t priority;
+	uint8_t dst_ip_mask:1,   /* if mask is 1b, do not compare dst ip. */
+		src_ip_mask:1,   /* if mask is 1b, do not compare src ip. */
+		dst_port_mask:1, /* if mask is 1b, do not compare dst port. */
+		src_port_mask:1, /* if mask is 1b, do not compare src port. */
+		proto_mask:1;    /* if mask is 1b, do not compare protocol. */
+};
+
+/* Structure of 2-tuple filter */
+struct igc_2tuple_filter {
+	RTE_STD_C11
+	union {
+		uint64_t hash_val;
+		struct igc_2tuple_info tuple2_info;
+	};
+
+	uint8_t queue;
+};
+
 /*
  * Structure to store private data for each driver instance (for each port).
  */
@@ -154,6 +191,7 @@ struct igc_adapter {
 	bool		stopped;
 
 	struct igc_ethertype_filter ethertype_filters[IGC_MAX_ETQF_FILTERS];
+	struct igc_2tuple_filter tuple2_filters[IGC_MAX_2TUPLE_FILTERS];
 };
 
 #define IGC_DEV_PRIVATE(_dev)	((_dev)->data->dev_private)
diff --git a/drivers/net/igc/igc_filter.c b/drivers/net/igc/igc_filter.c
index 231fcd4..340dbee 100644
--- a/drivers/net/igc/igc_filter.c
+++ b/drivers/net/igc/igc_filter.c
@@ -210,10 +210,347 @@
 	return ret;
 }
 
+/*
+ * Translate elements in n-tuple filter to 2-tuple filter
+ *
+ * @ntuple, n-tuple filter pointer
+ * @tuple2, 2-tuple filter pointer
+ *
+ * Return 0, or negative for error
+ */
+static int
+filter_ntuple_to_2tuple(const struct rte_eth_ntuple_filter *ntuple,
+			struct igc_2tuple_filter *tuple2)
+{
+	struct igc_2tuple_info *info;
+
+	/* check max value */
+	if (ntuple->queue >= IGC_QUEUE_PAIRS_NUM ||
+		ntuple->priority > IGC_2TUPLE_MAX_PRI ||
+		ntuple->tcp_flags > RTE_NTUPLE_TCP_FLAGS_MASK) {
+		PMD_DRV_LOG(ERR, "out of range, queue %u(max is %u), priority"
+			" %u(max is %u) tcp_flags %u(max is %u).",
+			ntuple->queue, IGC_QUEUE_PAIRS_NUM - 1,
+			ntuple->priority, IGC_2TUPLE_MAX_PRI,
+			ntuple->tcp_flags, RTE_NTUPLE_TCP_FLAGS_MASK);
+		return -EINVAL;
+	}
+
+	tuple2->queue = ntuple->queue;
+	info = &tuple2->tuple2_info;
+
+	/* port and it's mask assignment */
+	switch (ntuple->dst_port_mask) {
+	case UINT16_MAX:
+		info->dst_port_mask = 0;
+		info->dst_port = ntuple->dst_port;
+		break;
+	case 0:
+		info->dst_port_mask = 1;
+		break;
+	default:
+		PMD_DRV_LOG(ERR, "invalid dst_port mask.");
+		return -EINVAL;
+	}
+
+	/* protocol and it's mask assignment */
+	switch (ntuple->proto_mask) {
+	case UINT8_MAX:
+		info->proto_mask = 0;
+		info->proto = ntuple->proto;
+		break;
+	case 0:
+		info->proto_mask = 1;
+		break;
+	default:
+		PMD_DRV_LOG(ERR, "invalid protocol mask.");
+		return -EINVAL;
+	}
+
+	/* priority and TCP flags assignment */
+	info->priority = (uint8_t)ntuple->priority;
+	if (ntuple->flags & RTE_NTUPLE_FLAGS_TCP_FLAG)
+		info->tcp_flags = ntuple->tcp_flags;
+	else
+		info->tcp_flags = 0;
+
+	return 0;
+}
+
+/*
+ * igc_2tuple_filter_lookup - lookup 2-tuple filter
+ *
+ * @igc, IGC filter pointer
+ * @tuple2, 2-tuple pointer
+ * @empty, a place to store the index of empty entry if the item not found
+ *  it's not smaller than 0 if valid, otherwise -1 for no empty entry.
+ *  empty parameter is only valid if the return value of the function is -1
+ *
+ * Return value
+ * >= 0, item index of the filter
+ * -1, the item not been found
+ */
+static int
+igc_2tuple_filter_lookup(const struct igc_adapter *igc,
+			const struct igc_2tuple_filter *tuple2,
+			int *empty)
+{
+	int i = 0;
+
+	if (empty) {
+		/* set to invalid valid */
+		*empty = -1;
+
+		/* search the filters array */
+		for (; i < IGC_MAX_2TUPLE_FILTERS; i++) {
+			if (igc->tuple2_filters[i].hash_val) {
+				/* compare the hase value */
+				if (tuple2->hash_val ==
+					igc->tuple2_filters[i].hash_val)
+					/* filter be found, return index */
+					return i;
+			} else {
+				/* get the empty entry */
+				*empty = i;
+				i++;
+				break;
+			}
+		}
+	}
+
+	/* search the rest of filters */
+	for (; i < IGC_MAX_2TUPLE_FILTERS; i++) {
+		if (tuple2->hash_val == igc->tuple2_filters[i].hash_val)
+			/* filter be found, return index */
+			return i;
+	}
+
+	return -1;
+}
+
+static int
+igc_get_ntuple_filter(struct rte_eth_dev *dev,
+		struct rte_eth_ntuple_filter *ntuple)
+{
+	struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
+	struct igc_2tuple_filter tuple2;
+	int ret;
+
+	switch (ntuple->flags) {
+	case RTE_NTUPLE_FLAGS_DST_PORT:
+	case RTE_NTUPLE_FLAGS_DST_PORT | RTE_NTUPLE_FLAGS_TCP_FLAG:
+	case RTE_NTUPLE_FLAGS_PROTO:
+	case RTE_NTUPLE_FLAGS_PROTO | RTE_NTUPLE_FLAGS_TCP_FLAG:
+	case RTE_2TUPLE_FLAGS:
+	case RTE_2TUPLE_FLAGS | RTE_NTUPLE_FLAGS_TCP_FLAG:
+		memset(&tuple2, 0, sizeof(tuple2));
+		ret = filter_ntuple_to_2tuple(ntuple, &tuple2);
+		if (ret < 0)
+			return ret;
+
+		ret = igc_2tuple_filter_lookup(igc, &tuple2, NULL);
+		if (ret < 0) {
+			PMD_DRV_LOG(ERR, "filter doesn't exist.");
+			return -ENOENT;
+		}
+		ntuple->queue = igc->tuple2_filters[ret].queue;
+		break;
+	default:
+		PMD_DRV_LOG(ERR, "unsupported flags %u.", ntuple->flags);
+		ret = -EINVAL;
+		break;
+	}
+
+	return 0;
+}
+
+/* Set hardware register values */
+static void
+igc_enable_2tuple_filter(struct rte_eth_dev *dev,
+			const struct igc_adapter *igc, uint8_t index)
+{
+	struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
+	const struct igc_2tuple_filter *filter = &igc->tuple2_filters[index];
+	const struct igc_2tuple_info *info = &filter->tuple2_info;
+	uint32_t ttqf, imir, imir_ext = IGC_IMIREXT_SIZE_BP;
+
+	imir = info->dst_port;
+	imir |= info->priority << IGC_IMIR_PRIORITY_SHIFT;
+
+	/* 1b means not compare. */
+	if (info->dst_port_mask)
+		imir |= IGC_IMIR_PORT_BP;
+
+	ttqf = IGC_TTQF_DISABLE_MASK | IGC_TTQF_QUEUE_ENABLE;
+	ttqf |= filter->queue << IGC_TTQF_QUEUE_SHIFT;
+	ttqf |= info->proto;
+
+	if (info->proto_mask == 0)
+		ttqf &= ~IGC_TTQF_MASK_ENABLE;
+
+	/* TCP flags bits setting. */
+	if (info->tcp_flags & RTE_NTUPLE_TCP_FLAGS_MASK) {
+		if (info->tcp_flags & RTE_TCP_URG_FLAG)
+			imir_ext |= IGC_IMIREXT_CTRL_URG;
+		if (info->tcp_flags & RTE_TCP_ACK_FLAG)
+			imir_ext |= IGC_IMIREXT_CTRL_ACK;
+		if (info->tcp_flags & RTE_TCP_PSH_FLAG)
+			imir_ext |= IGC_IMIREXT_CTRL_PSH;
+		if (info->tcp_flags & RTE_TCP_RST_FLAG)
+			imir_ext |= IGC_IMIREXT_CTRL_RST;
+		if (info->tcp_flags & RTE_TCP_SYN_FLAG)
+			imir_ext |= IGC_IMIREXT_CTRL_SYN;
+		if (info->tcp_flags & RTE_TCP_FIN_FLAG)
+			imir_ext |= IGC_IMIREXT_CTRL_FIN;
+	} else {
+		imir_ext |= IGC_IMIREXT_CTRL_BP;
+	}
+
+	IGC_WRITE_REG(hw, IGC_IMIR(index), imir);
+	IGC_WRITE_REG(hw, IGC_TTQF(index), ttqf);
+	IGC_WRITE_REG(hw, IGC_IMIREXT(index), imir_ext);
+	IGC_WRITE_FLUSH(hw);
+}
+
+/* Reset hardware register values */
+static void
+igc_disable_2tuple_filter(struct rte_eth_dev *dev, uint8_t index)
+{
+	struct igc_hw *hw = IGC_DEV_PRIVATE_HW(dev);
+
+	IGC_WRITE_REG(hw, IGC_TTQF(index), IGC_TTQF_DISABLE_MASK);
+	IGC_WRITE_REG(hw, IGC_IMIR(index), 0);
+	IGC_WRITE_REG(hw, IGC_IMIREXT(index), 0);
+	IGC_WRITE_FLUSH(hw);
+}
+
+static int
+igc_add_2tuple_filter(struct rte_eth_dev *dev,
+		const struct igc_2tuple_filter *tuple2)
+{
+	struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
+	int ret, empty;
+
+	ret = igc_2tuple_filter_lookup(igc, tuple2, &empty);
+	if (ret >= 0) {
+		PMD_DRV_LOG(ERR, "filter exists.");
+		return -EEXIST;
+	}
+
+	if (empty < 0) {
+		PMD_DRV_LOG(ERR, "filter no entry.");
+		return -ENOSPC;
+	}
+
+	ret = empty;
+	memcpy(&igc->tuple2_filters[ret], tuple2, sizeof(*tuple2));
+	igc_enable_2tuple_filter(dev, igc, (uint8_t)ret);
+	return 0;
+}
+
+static int
+igc_del_2tuple_filter(struct rte_eth_dev *dev,
+		const struct igc_2tuple_filter *tuple2)
+{
+	struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
+	int ret;
+
+	ret = igc_2tuple_filter_lookup(igc, tuple2, NULL);
+	if (ret < 0) {
+		PMD_DRV_LOG(ERR, "filter not exists.");
+		return -ENOENT;
+	}
+
+	memset(&igc->tuple2_filters[ret], 0, sizeof(*tuple2));
+	igc_disable_2tuple_filter(dev, (uint8_t)ret);
+	return 0;
+}
+
+int
+igc_add_del_ntuple_filter(struct rte_eth_dev *dev,
+			const struct rte_eth_ntuple_filter *ntuple,
+			bool add)
+{
+	struct igc_2tuple_filter tuple2;
+	int ret;
+
+	switch (ntuple->flags) {
+	case RTE_NTUPLE_FLAGS_DST_PORT:
+	case RTE_NTUPLE_FLAGS_DST_PORT | RTE_NTUPLE_FLAGS_TCP_FLAG:
+	case RTE_NTUPLE_FLAGS_PROTO:
+	case RTE_NTUPLE_FLAGS_PROTO | RTE_NTUPLE_FLAGS_TCP_FLAG:
+	case RTE_2TUPLE_FLAGS:
+	case RTE_2TUPLE_FLAGS | RTE_NTUPLE_FLAGS_TCP_FLAG:
+		memset(&tuple2, 0, sizeof(tuple2));
+		ret = filter_ntuple_to_2tuple(ntuple, &tuple2);
+		if (ret < 0)
+			return ret;
+		if (add)
+			ret = igc_add_2tuple_filter(dev, &tuple2);
+		else
+			ret = igc_del_2tuple_filter(dev, &tuple2);
+		break;
+	default:
+		PMD_DRV_LOG(ERR, "unsupported flags %u.", ntuple->flags);
+		ret = -EINVAL;
+		break;
+	}
+
+	return ret;
+}
+
+/* Clear all the n-tuple filters */
+static void
+igc_clear_all_ntuple_filter(struct rte_eth_dev *dev)
+{
+	struct igc_adapter *igc = IGC_DEV_PRIVATE(dev);
+	int i;
+
+	for (i = 0; i < IGC_MAX_2TUPLE_FILTERS; i++)
+		igc_disable_2tuple_filter(dev, i);
+
+	memset(&igc->tuple2_filters, 0, sizeof(igc->tuple2_filters));
+}
+
+static int
+igc_ntuple_filter_handle(struct rte_eth_dev *dev,
+			enum rte_filter_op filter_op,
+			struct rte_eth_ntuple_filter *filter)
+{
+	int ret;
+
+	if (filter_op == RTE_ETH_FILTER_NOP)
+		return 0;
+
+	if (filter == NULL) {
+		PMD_DRV_LOG(ERR, "filter shouldn't be NULL for operation %u.",
+			filter_op);
+		return -EINVAL;
+	}
+
+	switch (filter_op) {
+	case RTE_ETH_FILTER_ADD:
+		ret = igc_add_del_ntuple_filter(dev, filter, true);
+		break;
+	case RTE_ETH_FILTER_DELETE:
+		ret = igc_add_del_ntuple_filter(dev, filter, false);
+		break;
+	case RTE_ETH_FILTER_GET:
+		ret = igc_get_ntuple_filter(dev, filter);
+		break;
+	default:
+		PMD_DRV_LOG(ERR, "unsupported operation %u.", filter_op);
+		ret = -EINVAL;
+		break;
+	}
+	return ret;
+}
+
 void
 igc_clear_all_filter(struct rte_eth_dev *dev)
 {
 	igc_clear_all_ethertype_filter(dev);
+	igc_clear_all_ntuple_filter(dev);
 }
 
 int
@@ -227,6 +564,10 @@
 		ret = igc_ethertype_filter_handle(dev, filter_op,
 			(struct rte_eth_ethertype_filter *)arg);
 		break;
+	case RTE_ETH_FILTER_NTUPLE:
+		ret = igc_ntuple_filter_handle(dev, filter_op,
+			(struct rte_eth_ntuple_filter *)arg);
+		break;
 	default:
 		PMD_DRV_LOG(WARNING, "Filter type (%d) not supported",
 							filter_type);
diff --git a/drivers/net/igc/igc_filter.h b/drivers/net/igc/igc_filter.h
index eff0e47..7c5e843 100644
--- a/drivers/net/igc/igc_filter.h
+++ b/drivers/net/igc/igc_filter.h
@@ -17,6 +17,9 @@ int igc_add_ethertype_filter(struct rte_eth_dev *dev,
 		const struct rte_eth_ethertype_filter *filter);
 int igc_del_ethertype_filter(struct rte_eth_dev *dev,
 		const struct rte_eth_ethertype_filter *filter);
+int igc_add_del_ntuple_filter(struct rte_eth_dev *dev,
+			const struct rte_eth_ntuple_filter *ntuple,
+			bool add);
 void
 igc_clear_all_filter(struct rte_eth_dev *dev);
 
-- 
1.8.3.1


  parent reply	other threads:[~2020-03-09  8:30 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-09  8:23 [dpdk-dev] [PATCH v1 01/15] net/igc: add igc PMD alvinx.zhang
2020-03-09  8:23 ` [dpdk-dev] [PATCH v1 02/15] net/igc: update base share codes alvinx.zhang
2020-03-09  8:23 ` [dpdk-dev] [PATCH v1 03/15] net/igc: device initialization alvinx.zhang
2020-03-12  4:42   ` Ye Xiaolong
2020-03-09  8:23 ` [dpdk-dev] [PATCH v1 04/15] net/igc: implement device base ops alvinx.zhang
2020-03-09  8:23 ` [dpdk-dev] [PATCH v1 05/15] net/igc: support reception and transmission of packets alvinx.zhang
2020-03-09  8:23 ` [dpdk-dev] [PATCH v1 06/15] net/igc: implement status API alvinx.zhang
2020-03-09  8:23 ` [dpdk-dev] [PATCH v1 07/15] net/igc: enable Rx queue interrupts alvinx.zhang
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 08/15] net/igc: implement flow control ops alvinx.zhang
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 09/15] net/igc: implement RSS API alvinx.zhang
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 10/15] net/igc: implement feature of VLAN alvinx.zhang
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 11/15] net/igc: implement ether-type filter alvinx.zhang
2020-03-09  8:24 ` alvinx.zhang [this message]
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 13/15] net/igc: implement TCP SYN filter alvinx.zhang
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 14/15] net/igc: implement hash filter configure alvinx.zhang
2020-03-09  8:24 ` [dpdk-dev] [PATCH v1 15/15] net/igc: implement flow API alvinx.zhang
2020-03-09  8:35 ` [dpdk-dev] [PATCH v1 01/15] net/igc: add igc PMD Ye Xiaolong
2020-03-12  3:09 ` Ye Xiaolong
2020-03-20  2:46 ` [dpdk-dev] [PATCH v2 00/14] " alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 01/14] net/igc: add " alvinx.zhang
2020-04-03 12:21     ` Ferruh Yigit
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 02/14] net/igc: support device initialization alvinx.zhang
2020-04-03 12:23     ` Ferruh Yigit
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 03/14] net/igc: implement device base ops alvinx.zhang
2020-04-03 12:24     ` Ferruh Yigit
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 04/14] net/igc: support reception and transmission of packets alvinx.zhang
2020-04-03 12:27     ` Ferruh Yigit
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 05/14] net/igc: implement status API alvinx.zhang
2020-04-03 12:24     ` Ferruh Yigit
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 06/14] net/igc: enable Rx queue interrupts alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 07/14] net/igc: implement flow control ops alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 08/14] net/igc: implement RSS API alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 09/14] net/igc: implement feature of VLAN alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 10/14] net/igc: implement ether-type filter alvinx.zhang
2020-04-03 12:26     ` Ferruh Yigit
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 11/14] net/igc: implement 2-tuple filter alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 12/14] net/igc: implement TCP SYN filter alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 13/14] net/igc: implement hash filter configure alvinx.zhang
2020-03-20  2:46   ` [dpdk-dev] [PATCH v2 14/14] net/igc: implement flow API alvinx.zhang
2020-04-03 12:26     ` Ferruh Yigit

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=1583742247-370386-12-git-send-email-alvinx.zhang@intel.com \
    --to=alvinx.zhang@intel.com \
    --cc=beilei.xing@intel.com \
    --cc=dev@dpdk.org \
    --cc=haiyue.wang@intel.com \
    --cc=qi.z.zhang@intel.com \
    --cc=xiaolong.ye@intel.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).