diff mbox series

[v1,12/15] net/igc: implement 2-tuple filter

Message ID 1583742247-370386-12-git-send-email-alvinx.zhang@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Ferruh Yigit
Headers show
Series [v1,01/15] net/igc: add igc PMD | expand

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Zhang, AlvinX March 9, 2020, 8:24 a.m. UTC
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 mbox series

Patch

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);