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.

This adds a new operation into the DSA switch operations structure,
port_change_cpu_port. A driver implementing this function has the
ability to change CPU destination port of a given port.

Signed-off-by: Marek Behún <marek.be...@nic.cz>
---
 include/net/dsa.h |  6 ++++++
 net/dsa/slave.c   | 35 +++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/include/net/dsa.h b/include/net/dsa.h
index 64bd70608f2f..4f3f0032b886 100644
--- a/include/net/dsa.h
+++ b/include/net/dsa.h
@@ -545,6 +545,12 @@ struct dsa_switch_ops {
         */
        netdev_tx_t (*port_deferred_xmit)(struct dsa_switch *ds, int port,
                                          struct sk_buff *skb);
+
+       /*
+        * Multi-CPU port support
+        */
+       int     (*port_change_cpu_port)(struct dsa_switch *ds, int port,
+                                       struct dsa_port *new_cpu_dp);
 };
 
 struct dsa_switch_driver {
diff --git a/net/dsa/slave.c b/net/dsa/slave.c
index 33f41178afcc..bafaadeca912 100644
--- a/net/dsa/slave.c
+++ b/net/dsa/slave.c
@@ -64,6 +64,40 @@ 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 dsa_port *dp = dsa_slave_to_port(dev);
+       struct dsa_slave_priv *p = netdev_priv(dev);
+       struct net_device *new_cpu_dev;
+       struct dsa_port *new_cpu_dp;
+       int err;
+
+       if (!dp->ds->ops->port_change_cpu_port)
+               return -EOPNOTSUPP;
+
+       new_cpu_dev = dev_get_by_index(dev_net(dev), iflink);
+       if (!new_cpu_dev)
+               return -ENODEV;
+
+       new_cpu_dp = new_cpu_dev->dsa_ptr;
+       if (!new_cpu_dp)
+               return -EINVAL;
+
+       /* new CPU port has to be on the same switch tree */
+       if (new_cpu_dp->dst != dp->dst)
+               return -EINVAL;
+
+       err = dp->ds->ops->port_change_cpu_port(dp->ds, dp->index, new_cpu_dp);
+       if (err)
+               return err;
+
+       /* should this be done atomically? */
+       dp->cpu_dp = new_cpu_dp;
+       p->xmit = new_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);
@@ -1176,6 +1210,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.21.0

Reply via email to