[PATCH RFC net-next 3/3] net: dsa: implement ndo_set_netlink for chaning port's CPU port

From: Ansuel Smith
Date: Sun Apr 11 2021 - 11:04:11 EST


Implement ndo_set_iflink for DSA slave device. In multi-CPU port setup
this should be used to change to which CPU destination port a given port
should be connected. On CPU port change, the mac address is updated with
the new value, if not set to a custom value.

Signed-off-by: Marek Behún <marek.behun@xxxxxx>
Signed-off-by: Ansuel Smith <ansuelsmth@xxxxxxxxx>
---
net/dsa/slave.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)

diff --git a/net/dsa/slave.c b/net/dsa/slave.c
index 992fcab4b552..c68dbd3ab21a 100644
--- a/net/dsa/slave.c
+++ b/net/dsa/slave.c
@@ -63,6 +63,36 @@ static int dsa_slave_get_iflink(const struct net_device *dev)
return dsa_slave_to_master(dev)->ifindex;
}

+static int dsa_slave_set_iflink(struct net_device *dev, int iflink)
+{
+ struct net_device *master = dsa_slave_to_master(dev);
+ struct dsa_port *dp = dsa_slave_to_port(dev);
+ struct dsa_slave_priv *p = netdev_priv(dev);
+ struct net_device *cpu_dev;
+ struct dsa_port *cpu_dp;
+
+ cpu_dev = dev_get_by_index(dev_net(dev), iflink);
+ if (!cpu_dev)
+ return -ENODEV;
+
+ cpu_dp = cpu_dev->dsa_ptr;
+ if (!cpu_dp)
+ return -EINVAL;
+
+ /* new CPU port has to be on the same switch tree */
+ if (cpu_dp->dst != dp->cpu_dp->dst)
+ return -EINVAL;
+
+ if (ether_addr_equal(dev->dev_addr, master->dev_addr))
+ eth_hw_addr_inherit(dev, cpu_dev);
+
+ /* should this be done atomically? */
+ dp->cpu_dp = cpu_dp;
+ p->xmit = cpu_dp->tag_ops->xmit;
+
+ return 0;
+}
+
static int dsa_slave_open(struct net_device *dev)
{
struct net_device *master = dsa_slave_to_master(dev);
@@ -1666,6 +1696,7 @@ static const struct net_device_ops dsa_slave_netdev_ops = {
.ndo_fdb_dump = dsa_slave_fdb_dump,
.ndo_do_ioctl = dsa_slave_ioctl,
.ndo_get_iflink = dsa_slave_get_iflink,
+ .ndo_set_iflink = dsa_slave_set_iflink,
#ifdef CONFIG_NET_POLL_CONTROLLER
.ndo_netpoll_setup = dsa_slave_netpoll_setup,
.ndo_netpoll_cleanup = dsa_slave_netpoll_cleanup,
--
2.30.2