[dpdk-dev] [PATCH v1 04/15] net/hinic: add VLAN filter and offload

Ziyang Xuan xuanziyang2 at huawei.com
Fri Sep 6 14:51:18 CEST 2019


From: Xiaoyun Wang <cloud.wangxiaoyun at huawei.com>

This patch adds VLAN filter and VLAN offload.

Signed-off-by: Ziyang Xuan <xuanziyang2 at huawei.com>
---
 drivers/net/hinic/base/hinic_pmd_cmd.h    |   1 +
 drivers/net/hinic/base/hinic_pmd_niccfg.c | 119 ++++++++++++++++++
 drivers/net/hinic/base/hinic_pmd_niccfg.h |  31 +++++
 drivers/net/hinic/hinic_pmd_ethdev.c      | 196 +++++++++++++++++++++++++++++-
 drivers/net/hinic/hinic_pmd_ethdev.h      |   7 +-
 5 files changed, 352 insertions(+), 2 deletions(-)

diff --git a/drivers/net/hinic/base/hinic_pmd_cmd.h b/drivers/net/hinic/base/hinic_pmd_cmd.h
index 73f96b1..d945694 100644
--- a/drivers/net/hinic/base/hinic_pmd_cmd.h
+++ b/drivers/net/hinic/base/hinic_pmd_cmd.h
@@ -140,6 +140,7 @@ enum hinic_port_cmd {
 
 	HINIC_PORT_CMD_SET_VHD_CFG		= 0xF7,
 	HINIC_PORT_CMD_SET_LINK_FOLLOW		= 0xF8,
+	HINIC_PORT_CMD_SET_VLAN_FILTER		= 0xFF
 };
 
 /* cmd of mgmt CPU message for HW module */
diff --git a/drivers/net/hinic/base/hinic_pmd_niccfg.c b/drivers/net/hinic/base/hinic_pmd_niccfg.c
index d245315..257015e 100644
--- a/drivers/net/hinic/base/hinic_pmd_niccfg.c
+++ b/drivers/net/hinic/base/hinic_pmd_niccfg.c
@@ -237,6 +237,125 @@ int hinic_set_port_mtu(void *hwdev, u32 new_mtu)
 	return 0;
 }
 
+int hinic_add_vlan(void *hwdev, u16 vlan_id, u16 func_id)
+{
+	struct hinic_vlan_config vlan_info;
+	u16 out_size = sizeof(vlan_info);
+	int err;
+
+	if (!hwdev) {
+		PMD_DRV_LOG(ERR, "Hwdev is NULL");
+		return -EINVAL;
+	}
+
+	memset(&vlan_info, 0, sizeof(vlan_info));
+	vlan_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
+	vlan_info.func_id = func_id;
+	vlan_info.vlan_id = vlan_id;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_ADD_VLAN,
+			&vlan_info, sizeof(vlan_info), &vlan_info, &out_size);
+	if (err || !out_size || vlan_info.mgmt_msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			"Failed to add vlan, err: %d, status: 0x%x, out size: 0x%x\n",
+			err, vlan_info.mgmt_msg_head.status, out_size);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+int hinic_del_vlan(void *hwdev, u16 vlan_id, u16 func_id)
+{
+	struct hinic_vlan_config vlan_info;
+	u16 out_size = sizeof(vlan_info);
+	int err;
+
+	if (!hwdev) {
+		PMD_DRV_LOG(ERR, "Hwdev is NULL");
+		return -EINVAL;
+	}
+
+	memset(&vlan_info, 0, sizeof(vlan_info));
+	vlan_info.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
+	vlan_info.func_id = func_id;
+	vlan_info.vlan_id = vlan_id;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_DEL_VLAN,
+			&vlan_info, sizeof(vlan_info), &vlan_info, &out_size);
+	if (err || !out_size || vlan_info.mgmt_msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			"Failed to delete vlan, err: %d, status: 0x%x, out size: 0x%x\n",
+			err, vlan_info.mgmt_msg_head.status, out_size);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+int hinic_config_vlan_filter(void *hwdev, u32 vlan_filter_ctrl)
+{
+	struct hinic_hwdev *nic_hwdev = (struct hinic_hwdev *)hwdev;
+	struct hinic_vlan_filter vlan_filter;
+	u16 out_size = sizeof(vlan_filter);
+	int err;
+
+	if (!hwdev)
+		return -EINVAL;
+
+	memset(&vlan_filter, 0, sizeof(vlan_filter));
+	vlan_filter.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
+	vlan_filter.func_id = hinic_global_func_id(nic_hwdev);
+	vlan_filter.vlan_filter_ctrl = vlan_filter_ctrl;
+
+	err = l2nic_msg_to_mgmt_sync(nic_hwdev, HINIC_PORT_CMD_SET_VLAN_FILTER,
+				     &vlan_filter, sizeof(vlan_filter),
+				     &vlan_filter, &out_size);
+	if (vlan_filter.mgmt_msg_head.status == HINIC_MGMT_CMD_UNSUPPORTED) {
+		err = HINIC_MGMT_CMD_UNSUPPORTED;
+	} else if ((err == HINIC_MBOX_VF_CMD_ERROR) &&
+		(HINIC_IS_VF(nic_hwdev))) {
+		err = HINIC_MGMT_CMD_UNSUPPORTED;
+	} else if (err || !out_size || vlan_filter.mgmt_msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			"Failed to config vlan filter, vlan_filter_ctrl: 0x%x, err: %d, status: 0x%x, out size: 0x%x\n",
+			vlan_filter_ctrl, err,
+			vlan_filter.mgmt_msg_head.status, out_size);
+		err = -EINVAL;
+	}
+
+	return err;
+}
+
+int hinic_set_rx_vlan_offload(void *hwdev, u8 en)
+{
+	struct hinic_vlan_offload vlan_cfg;
+	u16 out_size = sizeof(vlan_cfg);
+	int err;
+
+	if (!hwdev) {
+		PMD_DRV_LOG(ERR, "Hwdev is NULL");
+		return -EINVAL;
+	}
+
+	memset(&vlan_cfg, 0, sizeof(vlan_cfg));
+	vlan_cfg.mgmt_msg_head.resp_aeq_num = HINIC_AEQ1;
+	vlan_cfg.func_id = hinic_global_func_id(hwdev);
+	vlan_cfg.vlan_rx_offload = en;
+
+	err = l2nic_msg_to_mgmt_sync(hwdev, HINIC_PORT_CMD_SET_RX_VLAN_OFFLOAD,
+					&vlan_cfg, sizeof(vlan_cfg),
+					&vlan_cfg, &out_size);
+	if (err || !out_size || vlan_cfg.mgmt_msg_head.status) {
+		PMD_DRV_LOG(ERR,
+			"Failed to set rx vlan offload, err: %d, status: 0x%x, out size: 0x%x\n",
+			err, vlan_cfg.mgmt_msg_head.status, out_size);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
 int hinic_get_link_status(void *hwdev, u8 *link_state)
 {
 	struct hinic_get_link get_link;
diff --git a/drivers/net/hinic/base/hinic_pmd_niccfg.h b/drivers/net/hinic/base/hinic_pmd_niccfg.h
index 253dbf4..0f0b078 100644
--- a/drivers/net/hinic/base/hinic_pmd_niccfg.h
+++ b/drivers/net/hinic/base/hinic_pmd_niccfg.h
@@ -380,6 +380,29 @@ struct hinic_mtu {
 	u32	mtu;
 };
 
+struct hinic_vlan_config {
+	struct hinic_mgmt_msg_head mgmt_msg_head;
+
+	u16	func_id;
+	u16	vlan_id;
+};
+
+struct hinic_vlan_filter {
+	struct hinic_mgmt_msg_head mgmt_msg_head;
+
+	u16	func_id;
+	u8	rsvd1[2];
+	u32	vlan_filter_ctrl;
+};
+
+struct hinic_vlan_offload {
+	struct hinic_mgmt_msg_head mgmt_msg_head;
+
+	u16	func_id;
+	u8	vlan_rx_offload;
+	u8	rsvd1[5];
+};
+
 struct hinic_get_link {
 	struct hinic_mgmt_msg_head mgmt_msg_head;
 
@@ -597,6 +620,14 @@ int hinic_update_mac(void *hwdev, u8 *old_mac, u8 *new_mac, u16 vlan_id,
 
 int hinic_set_port_mtu(void *hwdev, u32 new_mtu);
 
+int hinic_add_vlan(void *hwdev, u16 vlan_id, u16 func_id);
+
+int hinic_del_vlan(void *hwdev, u16 vlan_id, u16 func_id);
+
+int hinic_config_vlan_filter(void *hwdev, u32 vlan_filter_ctrl);
+
+int hinic_set_rx_vlan_offload(void *hwdev, u8 en);
+
 int hinic_set_vport_enable(void *hwdev, bool enable);
 
 int hinic_set_port_enable(void *hwdev, bool enable);
diff --git a/drivers/net/hinic/hinic_pmd_ethdev.c b/drivers/net/hinic/hinic_pmd_ethdev.c
index 90ebc16..9f0c204 100644
--- a/drivers/net/hinic/hinic_pmd_ethdev.c
+++ b/drivers/net/hinic/hinic_pmd_ethdev.c
@@ -10,6 +10,7 @@
 #include <rte_memcpy.h>
 #include <rte_mempool.h>
 #include <rte_errno.h>
+#include <rte_ether.h>
 
 #include "base/hinic_compat.h"
 #include "base/hinic_pmd_hwdev.h"
@@ -46,6 +47,17 @@
 #define HINIC_MIN_RX_BUF_SIZE		1024
 #define HINIC_MAX_MAC_ADDRS		1
 
+/*
+ * vlan_id is a 12 bit number.
+ * The VFTA array is actually a 4096 bit array, 128 of 32bit elements.
+ * 2^5 = 32. The val of lower 5 bits specifies the bit in the 32bit element.
+ * The higher 7 bit val specifies VFTA array index.
+ */
+#define HINIC_VFTA_BIT(vlan_id)    (1 << ((vlan_id) & 0x1F))
+#define HINIC_VFTA_IDX(vlan_id)    ((vlan_id) >> 5)
+
+#define HINIC_VLAN_FILTER_EN		(1U << 0)
+
 /** Driver-specific log messages type. */
 int hinic_logtype;
 
@@ -224,6 +236,7 @@ static int hinic_xstats_calc_num(struct hinic_nic_dev *nic_dev)
 	.nb_align = HINIC_TXD_ALIGN,
 };
 
+static int hinic_init_vlan(struct rte_eth_dev *dev);
 
 /**
  * Interrupt handler triggered by NIC  for handling
@@ -307,6 +320,14 @@ static int hinic_dev_configure(struct rte_eth_dev *dev)
 		return err;
 	}
 
+	/* init vlan offoad */
+	err = hinic_init_vlan(dev);
+	if (err) {
+		PMD_DRV_LOG(ERR, "Initialize vlan filter and strip failed\n");
+		(void)hinic_config_mq_mode(dev, FALSE);
+		return err;
+	}
+
 	return HINIC_OK;
 }
 
@@ -689,7 +710,8 @@ static void hinic_get_speed_capa(struct rte_eth_dev *dev, uint32_t *speed_capa)
 	info->rx_offload_capa = DEV_RX_OFFLOAD_VLAN_STRIP |
 				DEV_RX_OFFLOAD_IPV4_CKSUM |
 				DEV_RX_OFFLOAD_UDP_CKSUM |
-				DEV_RX_OFFLOAD_TCP_CKSUM;
+				DEV_RX_OFFLOAD_TCP_CKSUM |
+				DEV_RX_OFFLOAD_VLAN_FILTER;
 
 	info->tx_queue_offload_capa = 0;
 	info->tx_offload_capa = DEV_TX_OFFLOAD_VLAN_INSERT |
@@ -1329,6 +1351,173 @@ static void hinic_deinit_mac_addr(struct rte_eth_dev *eth_dev)
 			    eth_dev->data->name);
 }
 
+static void hinic_store_vlan_filter(struct hinic_nic_dev *nic_dev,
+					u16 vlan_id, bool on)
+{
+	u32 vid_idx, vid_bit;
+
+	vid_idx = HINIC_VFTA_IDX(vlan_id);
+	vid_bit = HINIC_VFTA_BIT(vlan_id);
+
+	if (on)
+		nic_dev->vfta[vid_idx] |= vid_bit;
+	else
+		nic_dev->vfta[vid_idx] &= ~vid_bit;
+}
+
+static bool hinic_find_vlan_filter(struct hinic_nic_dev *nic_dev,
+				uint16_t vlan_id)
+{
+	u32 vid_idx, vid_bit;
+
+	vid_idx = HINIC_VFTA_IDX(vlan_id);
+	vid_bit = HINIC_VFTA_BIT(vlan_id);
+
+	return (nic_dev->vfta[vid_idx] & vid_bit) ? TRUE : FALSE;
+}
+
+/**
+ * DPDK callback to set vlan filter.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ * @param vlan_id
+ *   vlan id is used to filter vlan packets
+ * @param enable
+ *   enable disable or enable vlan filter function
+ */
+static int hinic_vlan_filter_set(struct rte_eth_dev *dev,
+				uint16_t vlan_id, int enable)
+{
+	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
+	int err = 0;
+	u16 func_id;
+
+	if (vlan_id > RTE_ETHER_MAX_VLAN_ID)
+		return -EINVAL;
+
+	func_id = hinic_global_func_id(nic_dev->hwdev);
+
+	if (enable) {
+		/* If vlanid is already set, just return */
+		if (hinic_find_vlan_filter(nic_dev, vlan_id)) {
+			PMD_DRV_LOG(INFO, "Vlan %u has been added, device: %s",
+				  vlan_id, nic_dev->proc_dev_name);
+			return 0;
+		}
+
+		err = hinic_add_vlan(nic_dev->hwdev, vlan_id, func_id);
+	} else {
+		/* If vlanid can't be found, just return */
+		if (!hinic_find_vlan_filter(nic_dev, vlan_id)) {
+			PMD_DRV_LOG(INFO, "Vlan %u does not in the vlan filter list, device: %s",
+				  vlan_id, nic_dev->proc_dev_name);
+			return 0;
+		}
+
+		err = hinic_del_vlan(nic_dev->hwdev, vlan_id, func_id);
+	}
+
+	if (err) {
+		PMD_DRV_LOG(ERR, "%s vlan failed, func_id: %d, vlan_id: %d",
+		      enable ? "Add" : "Remove", func_id, vlan_id);
+		return err;
+	}
+
+	hinic_store_vlan_filter(nic_dev, vlan_id, enable);
+
+	PMD_DRV_LOG(INFO, "%s vlan %u succeed, device: %s",
+		  enable ? "Add" : "Remove", vlan_id, nic_dev->proc_dev_name);
+	return 0;
+}
+
+/**
+ * DPDK callback to enable or disable vlan offload.
+ *
+ * @param dev
+ *   Pointer to Ethernet device structure.
+ * @param mask
+ *   Definitions used for VLAN setting
+ */
+static int hinic_vlan_offload_set(struct rte_eth_dev *dev, int mask)
+{
+	struct hinic_nic_dev *nic_dev = HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(dev);
+	struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
+	bool on;
+	int err;
+
+	/* Enable or disable VLAN filter */
+	if (mask & ETH_VLAN_FILTER_MASK) {
+		on = (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_FILTER) ?
+			TRUE : FALSE;
+		err = hinic_config_vlan_filter(nic_dev->hwdev, on);
+		if (err == HINIC_MGMT_CMD_UNSUPPORTED) {
+			PMD_DRV_LOG(WARNING,
+				"Current matching version does not support vlan filter configuration, device: %s, port_id: %d",
+				  nic_dev->proc_dev_name, dev->data->port_id);
+		} else if (err) {
+			PMD_DRV_LOG(ERR, "Failed to %s vlan filter, device: %s, port_id: %d",
+				  on ? "enable" : "disable",
+				  nic_dev->proc_dev_name, dev->data->port_id);
+			return err;
+		}
+
+		PMD_DRV_LOG(INFO, "%s vlan filter succeed, device: %s, port_id: %d",
+			  on ? "Enable" : "Disable",
+			  nic_dev->proc_dev_name, dev->data->port_id);
+	}
+
+	/* Enable or disable VLAN stripping */
+	if (mask & ETH_VLAN_STRIP_MASK) {
+		on = (rxmode->offloads & DEV_RX_OFFLOAD_VLAN_STRIP) ?
+			TRUE : FALSE;
+		err = hinic_set_rx_vlan_offload(nic_dev->hwdev, on);
+		if (err) {
+			PMD_DRV_LOG(ERR, "Failed to %s vlan strip, device: %s, port_id: %d",
+				  on ? "enable" : "disable",
+				  nic_dev->proc_dev_name, dev->data->port_id);
+			return err;
+		}
+
+		PMD_DRV_LOG(INFO, "%s vlan strip succeed, device: %s, port_id: %d",
+			  on ? "Enable" : "Disable",
+			  nic_dev->proc_dev_name, dev->data->port_id);
+	}
+
+	if (mask & ETH_VLAN_EXTEND_MASK) {
+		PMD_DRV_LOG(WARNING, "Don't support vlan qinq, device: %s, port_id: %d",
+			  nic_dev->proc_dev_name, dev->data->port_id);
+		return -ENOTSUP;
+	}
+
+	return 0;
+}
+
+static int hinic_init_vlan(struct rte_eth_dev *dev)
+{
+	int mask = 0;
+
+	mask = ETH_VLAN_STRIP_MASK | ETH_VLAN_FILTER_MASK;
+	return hinic_vlan_offload_set(dev, mask);
+}
+
+static void hinic_remove_all_vlanid(struct rte_eth_dev *eth_dev)
+{
+	struct hinic_nic_dev *nic_dev =
+		HINIC_ETH_DEV_TO_PRIVATE_NIC_DEV(eth_dev);
+	u16 func_id;
+	int i;
+
+	func_id = hinic_global_func_id(nic_dev->hwdev);
+	for (i = 0; i <= RTE_ETHER_MAX_VLAN_ID; i++) {
+		/* If can't find it, continue */
+		if (!hinic_find_vlan_filter(nic_dev, i))
+			continue;
+
+		(void)hinic_del_vlan(nic_dev->hwdev, i, func_id);
+		hinic_store_vlan_filter(nic_dev, i, false);
+	}
+}
 /**
  * DPDK callback to enable promiscuous mode.
  *
@@ -2178,6 +2367,7 @@ static void hinic_dev_close(struct rte_eth_dev *dev)
 
 	/* deinit mac vlan tbl */
 	hinic_deinit_mac_addr(dev);
+	hinic_remove_all_vlanid(dev);
 
 	/* disable hardware and uio interrupt */
 	hinic_disable_interrupt(dev);
@@ -2197,6 +2387,8 @@ static void hinic_dev_close(struct rte_eth_dev *dev)
 	.tx_queue_release              = hinic_tx_queue_release,
 	.dev_stop                      = hinic_dev_stop,
 	.dev_close                     = hinic_dev_close,
+	.vlan_filter_set               = hinic_vlan_filter_set,
+	.vlan_offload_set              = hinic_vlan_offload_set,
 	.promiscuous_enable            = hinic_dev_promiscuous_enable,
 	.promiscuous_disable           = hinic_dev_promiscuous_disable,
 	.rss_hash_update               = hinic_rss_hash_update,
@@ -2221,6 +2413,8 @@ static void hinic_dev_close(struct rte_eth_dev *dev)
 	.tx_queue_release              = hinic_tx_queue_release,
 	.dev_stop                      = hinic_dev_stop,
 	.dev_close                     = hinic_dev_close,
+	.vlan_filter_set               = hinic_vlan_filter_set,
+	.vlan_offload_set              = hinic_vlan_offload_set,
 	.rss_hash_update               = hinic_rss_hash_update,
 	.rss_hash_conf_get             = hinic_rss_conf_get,
 	.reta_update                   = hinic_rss_indirtbl_update,
diff --git a/drivers/net/hinic/hinic_pmd_ethdev.h b/drivers/net/hinic/hinic_pmd_ethdev.h
index 4aeddc2..cc689f4 100644
--- a/drivers/net/hinic/hinic_pmd_ethdev.h
+++ b/drivers/net/hinic/hinic_pmd_ethdev.h
@@ -31,6 +31,9 @@
 #define HINIC_TXD_ALIGN                 1
 #define HINIC_RXD_ALIGN                 1
 
+#define HINIC_UINT32_BIT_SIZE      (CHAR_BIT * sizeof(uint32_t))
+#define HINIC_VFTA_SIZE            (4096 / HINIC_UINT32_BIT_SIZE)
+
 enum hinic_dev_status {
 	HINIC_DEV_INIT,
 	HINIC_DEV_CLOSE,
@@ -54,10 +57,12 @@ struct hinic_nic_dev {
 	u8 num_rss;
 	u8 rx_queue_list[HINIC_MAX_RX_QUEUES];
 
+	u32 vfta[HINIC_VFTA_SIZE];	/* VLAN bitmap */
+
 	/* info */
 	unsigned int flags;
 	struct nic_service_cap nic_cap;
-	u32 rx_mode_status;	/* promisc allmulticast */
+	u32 rx_mode_status;	/* promisc or allmulticast */
 	unsigned long dev_status;
 
 	/* dpdk only */
-- 
1.8.3.1



More information about the dev mailing list