[PATCH v5 1/2] net: dsa: Add protocol support for 802.1AD when adding or deleting vlan for dsa switch port

From: hongbo . wang
Date: Fri Aug 07 2020 - 07:11:46 EST


From: "hongbo.wang" <hongbo.wang@xxxxxxx>

the following command will be supported:

Set bridge's vlan protocol:
ip link set br0 type bridge vlan_protocol 802.1ad
Add VLAN:
ip link add link swp1 name swp1.100 type vlan protocol 802.1ad id 100
Delete VLAN:
ip link del link swp1 name swp1.100

Signed-off-by: hongbo.wang <hongbo.wang@xxxxxxx>
---
include/net/switchdev.h | 1 +
net/bridge/br_switchdev.c | 22 ++++++++++++++++
net/dsa/dsa_priv.h | 4 +--
net/dsa/port.c | 6 +++--
net/dsa/slave.c | 53 ++++++++++++++++++++++++++-------------
net/dsa/tag_8021q.c | 4 +--
6 files changed, 66 insertions(+), 24 deletions(-)

diff --git a/include/net/switchdev.h b/include/net/switchdev.h
index ff2246914301..7594ea82879f 100644
--- a/include/net/switchdev.h
+++ b/include/net/switchdev.h
@@ -97,6 +97,7 @@ struct switchdev_obj_port_vlan {
u16 flags;
u16 vid_begin;
u16 vid_end;
+ u16 proto;
};

#define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
diff --git a/net/bridge/br_switchdev.c b/net/bridge/br_switchdev.c
index 015209bf44aa..bcfa00d6d5eb 100644
--- a/net/bridge/br_switchdev.c
+++ b/net/bridge/br_switchdev.c
@@ -146,6 +146,26 @@ br_switchdev_fdb_notify(const struct net_bridge_fdb_entry *fdb, int type)
}
}

+static u16 br_switchdev_get_bridge_vlan_proto(struct net_device *dev)
+{
+ u16 vlan_proto = ETH_P_8021Q;
+ struct net_device *br = NULL;
+ struct net_bridge_port *p;
+
+ if (netif_is_bridge_master(dev)) {
+ br = dev;
+ } else if (netif_is_bridge_port(dev)) {
+ p = br_port_get_rcu(dev);
+ if (p && p->br)
+ br = p->br->dev;
+ }
+
+ if (br)
+ br_vlan_get_proto(br, &vlan_proto);
+
+ return vlan_proto;
+}
+
int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
struct netlink_ext_ack *extack)
{
@@ -157,6 +177,7 @@ int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
.vid_end = vid,
};

+ v.proto = br_switchdev_get_bridge_vlan_proto(dev);
return switchdev_port_obj_add(dev, &v.obj, extack);
}

@@ -169,5 +190,6 @@ int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
.vid_end = vid,
};

+ v.proto = br_switchdev_get_bridge_vlan_proto(dev);
return switchdev_port_obj_del(dev, &v.obj);
}
diff --git a/net/dsa/dsa_priv.h b/net/dsa/dsa_priv.h
index 1653e3377cb3..52685b9875e5 100644
--- a/net/dsa/dsa_priv.h
+++ b/net/dsa/dsa_priv.h
@@ -164,8 +164,8 @@ int dsa_port_vlan_add(struct dsa_port *dp,
struct switchdev_trans *trans);
int dsa_port_vlan_del(struct dsa_port *dp,
const struct switchdev_obj_port_vlan *vlan);
-int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags);
-int dsa_port_vid_del(struct dsa_port *dp, u16 vid);
+int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 proto, u16 flags);
+int dsa_port_vid_del(struct dsa_port *dp, u16 vid, u16 proto);
int dsa_port_link_register_of(struct dsa_port *dp);
void dsa_port_link_unregister_of(struct dsa_port *dp);
extern const struct phylink_mac_ops dsa_port_phylink_mac_ops;
diff --git a/net/dsa/port.c b/net/dsa/port.c
index e23ece229c7e..c98bbac3980a 100644
--- a/net/dsa/port.c
+++ b/net/dsa/port.c
@@ -433,13 +433,14 @@ int dsa_port_vlan_del(struct dsa_port *dp,
return dsa_port_notify(dp, DSA_NOTIFIER_VLAN_DEL, &info);
}

-int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
+int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 proto, u16 flags)
{
struct switchdev_obj_port_vlan vlan = {
.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
.flags = flags,
.vid_begin = vid,
.vid_end = vid,
+ .proto = proto,
};
struct switchdev_trans trans;
int err;
@@ -454,12 +455,13 @@ int dsa_port_vid_add(struct dsa_port *dp, u16 vid, u16 flags)
}
EXPORT_SYMBOL(dsa_port_vid_add);

-int dsa_port_vid_del(struct dsa_port *dp, u16 vid)
+int dsa_port_vid_del(struct dsa_port *dp, u16 vid, u16 proto)
{
struct switchdev_obj_port_vlan vlan = {
.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
.vid_begin = vid,
.vid_end = vid,
+ .proto = proto,
};

return dsa_port_vlan_del(dp, &vlan);
diff --git a/net/dsa/slave.c b/net/dsa/slave.c
index 41d60eeefdbd..f01deda00492 100644
--- a/net/dsa/slave.c
+++ b/net/dsa/slave.c
@@ -328,6 +328,7 @@ static int dsa_slave_vlan_add(struct net_device *dev,
* it doesn't make sense to program a PVID, so clear this flag.
*/
vlan.flags &= ~BRIDGE_VLAN_INFO_PVID;
+ vlan.proto = ETH_P_8021Q;

err = dsa_port_vlan_add(dp->cpu_dp, &vlan, trans);
if (err)
@@ -1229,11 +1230,38 @@ static int dsa_slave_get_ts_info(struct net_device *dev,
return ds->ops->get_ts_info(ds, p->dp->index, ts);
}

+static bool dsa_slave_skip_vlan_configuration(struct dsa_port *dp,
+ u16 vlan_proto, u16 vid)
+{
+ struct bridge_vlan_info info;
+ bool change_proto = false;
+ u16 br_proto = 0;
+ int ret;
+
+ /* when changing bridge's vlan protocol, it will change bridge
+ * port's protocol firstly, then set bridge's protocol. if it's
+ * changing vlan protocol, should not return -EBUSY.
+ */
+ ret = br_vlan_get_proto(dp->bridge_dev, &br_proto);
+ if (ret == 0 && br_proto != vlan_proto)
+ change_proto = true;
+
+ /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
+ * device, respectively the VID is not found, returning
+ * 0 means success, which is a failure for us here.
+ */
+ ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
+ if (ret == 0 && !change_proto)
+ return true;
+ else
+ return false;
+}
+
static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
u16 vid)
{
struct dsa_port *dp = dsa_slave_to_port(dev);
- struct bridge_vlan_info info;
+ u16 vlan_proto = ntohs(proto);
int ret;

/* Check for a possible bridge VLAN entry now since there is no
@@ -1243,20 +1271,15 @@ static int dsa_slave_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
if (dsa_port_skip_vlan_configuration(dp))
return 0;

- /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
- * device, respectively the VID is not found, returning
- * 0 means success, which is a failure for us here.
- */
- ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
- if (ret == 0)
+ if (dsa_slave_skip_vlan_configuration(dp, vlan_proto, vid))
return -EBUSY;
}

- ret = dsa_port_vid_add(dp, vid, 0);
+ ret = dsa_port_vid_add(dp, vid, vlan_proto, 0);
if (ret)
return ret;

- ret = dsa_port_vid_add(dp->cpu_dp, vid, 0);
+ ret = dsa_port_vid_add(dp->cpu_dp, vid, ETH_P_8021Q, 0);
if (ret)
return ret;

@@ -1267,8 +1290,7 @@ static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
u16 vid)
{
struct dsa_port *dp = dsa_slave_to_port(dev);
- struct bridge_vlan_info info;
- int ret;
+ u16 vlan_proto = ntohs(proto);

/* Check for a possible bridge VLAN entry now since there is no
* need to emulate the switchdev prepare + commit phase.
@@ -1277,19 +1299,14 @@ static int dsa_slave_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
if (dsa_port_skip_vlan_configuration(dp))
return 0;

- /* br_vlan_get_info() returns -EINVAL or -ENOENT if the
- * device, respectively the VID is not found, returning
- * 0 means success, which is a failure for us here.
- */
- ret = br_vlan_get_info(dp->bridge_dev, vid, &info);
- if (ret == 0)
+ if (dsa_slave_skip_vlan_configuration(dp, vlan_proto, vid))
return -EBUSY;
}

/* Do not deprogram the CPU port as it may be shared with other user
* ports which can be members of this VLAN as well.
*/
- return dsa_port_vid_del(dp, vid);
+ return dsa_port_vid_del(dp, vid, vlan_proto);
}

struct dsa_hw_port {
diff --git a/net/dsa/tag_8021q.c b/net/dsa/tag_8021q.c
index 780b2a15ac9b..4c93988e61a3 100644
--- a/net/dsa/tag_8021q.c
+++ b/net/dsa/tag_8021q.c
@@ -152,9 +152,9 @@ static int dsa_8021q_vid_apply(struct dsa_switch *ds, int port, u16 vid,
struct dsa_port *dp = dsa_to_port(ds, port);

if (enabled)
- return dsa_port_vid_add(dp, vid, flags);
+ return dsa_port_vid_add(dp, vid, ETH_P_8021Q, flags);

- return dsa_port_vid_del(dp, vid);
+ return dsa_port_vid_del(dp, vid, ETH_P_8021Q);
}

/* RX VLAN tagging (left) and TX VLAN tagging (right) setup shown for a single
--
2.17.1