net: dsa: keep a copy of the tagging protocol in the DSA switch tree
authorVladimir Oltean <vladimir.oltean@nxp.com>
Fri, 29 Jan 2021 01:00:05 +0000 (03:00 +0200)
committerJakub Kicinski <kuba@kernel.org>
Sat, 30 Jan 2021 05:24:31 +0000 (21:24 -0800)
Cascading DSA switches can be done multiple ways. There is the brute
force approach / tag stacking, where one upstream switch, located
between leaf switches and the host Ethernet controller, will just
happily transport the DSA header of those leaf switches as payload.
For this kind of setups, DSA works without any special kind of treatment
compared to a single switch - they just aren't aware of each other.
Then there's the approach where the upstream switch understands the tags
it transports from its leaves below, as it doesn't push a tag of its own,
but it routes based on the source port & switch id information present
in that tag (as opposed to DMAC & VID) and it strips the tag when
egressing a front-facing port. Currently only Marvell implements the
latter, and Marvell DSA trees contain only Marvell switches.

So it is safe to say that DSA trees already have a single tag protocol
shared by all switches, and in fact this is what makes the switches able
to understand each other. This fact is also implied by the fact that
currently, the tagging protocol is reported as part of a sysfs installed
on the DSA master and not per port, so it must be the same for all the
ports connected to that DSA master regardless of the switch that they
belong to.

It's time to make this official and enforce it (yes, this also means we
won't have any "switch understands tag to some extent but is not able to
speak it" hardware oddities that we'll support in the future).

This is needed due to the imminent introduction of the dsa_switch_ops::
change_tag_protocol driver API. When that is introduced, we'll have
to notify switches of the tagging protocol that they're configured to
use. Currently the tag_ops structure pointer is held only for CPU ports.
But there are switches which don't have CPU ports and nonetheless still
need to be configured. These would be Marvell leaf switches whose
upstream port is just a DSA link. How do we inform these of their
tagging protocol setup/deletion?

One answer to the above would be: iterate through the DSA switch tree's
ports once, list the CPU ports, get their tag_ops, then iterate again
now that we have it, and notify everybody of that tag_ops. But what to
do if conflicts appear between one cpu_dp->tag_ops and another? There's
no escaping the fact that conflict resolution needs to be done, so we
can be upfront about it.

Ease our work and just keep the master copy of the tag_ops inside the
struct dsa_switch_tree. Reference counting is now moved to be per-tree
too, instead of per-CPU port.

There are many places in the data path that access master->dsa_ptr->tag_ops
and we would introduce unnecessary performance penalty going through yet
another indirection, so keep those right where they are.

Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/dsa.h
net/dsa/dsa2.c

index 2f5435d..b8af1d6 100644 (file)
@@ -140,6 +140,9 @@ struct dsa_switch_tree {
        /* Has this tree been applied to the hardware? */
        bool setup;
 
+       /* Tagging protocol operations */
+       const struct dsa_device_ops *tag_ops;
+
        /*
         * Configuration data for the platform device that owns
         * this dsa switch tree instance.
@@ -225,7 +228,9 @@ struct dsa_port {
                struct net_device *slave;
        };
 
-       /* CPU port tagging operations used by master or slave devices */
+       /* Copy of the tagging protocol operations, for quicker access
+        * in the data path. Valid only for the CPU ports.
+        */
        const struct dsa_device_ops *tag_ops;
 
        /* Copies for faster access in master receive hot path */
index 2953d0c..63035a8 100644 (file)
@@ -179,6 +179,8 @@ static struct dsa_switch_tree *dsa_tree_alloc(int index)
 
 static void dsa_tree_free(struct dsa_switch_tree *dst)
 {
+       if (dst->tag_ops)
+               dsa_tag_driver_put(dst->tag_ops);
        list_del(&dst->list);
        kfree(dst);
 }
@@ -467,7 +469,6 @@ static void dsa_port_teardown(struct dsa_port *dp)
                break;
        case DSA_PORT_TYPE_CPU:
                dsa_port_disable(dp);
-               dsa_tag_driver_put(dp->tag_ops);
                dsa_port_link_unregister_of(dp);
                break;
        case DSA_PORT_TYPE_DSA:
@@ -1011,24 +1012,35 @@ static int dsa_port_parse_cpu(struct dsa_port *dp, struct net_device *master)
 {
        struct dsa_switch *ds = dp->ds;
        struct dsa_switch_tree *dst = ds->dst;
-       const struct dsa_device_ops *tag_ops;
        enum dsa_tag_protocol tag_protocol;
 
        tag_protocol = dsa_get_tag_protocol(dp, master);
-       tag_ops = dsa_tag_driver_get(tag_protocol);
-       if (IS_ERR(tag_ops)) {
-               if (PTR_ERR(tag_ops) == -ENOPROTOOPT)
-                       return -EPROBE_DEFER;
-               dev_warn(ds->dev, "No tagger for this switch\n");
-               dp->master = NULL;
-               return PTR_ERR(tag_ops);
+       if (dst->tag_ops) {
+               if (dst->tag_ops->proto != tag_protocol) {
+                       dev_err(ds->dev,
+                               "A DSA switch tree can have only one tagging protocol\n");
+                       return -EINVAL;
+               }
+               /* In the case of multiple CPU ports per switch, the tagging
+                * protocol is still reference-counted only per switch tree, so
+                * nothing to do here.
+                */
+       } else {
+               dst->tag_ops = dsa_tag_driver_get(tag_protocol);
+               if (IS_ERR(dst->tag_ops)) {
+                       if (PTR_ERR(dst->tag_ops) == -ENOPROTOOPT)
+                               return -EPROBE_DEFER;
+                       dev_warn(ds->dev, "No tagger for this switch\n");
+                       dp->master = NULL;
+                       return PTR_ERR(dst->tag_ops);
+               }
        }
 
        dp->master = master;
        dp->type = DSA_PORT_TYPE_CPU;
-       dp->filter = tag_ops->filter;
-       dp->rcv = tag_ops->rcv;
-       dp->tag_ops = tag_ops;
+       dp->filter = dst->tag_ops->filter;
+       dp->rcv = dst->tag_ops->rcv;
+       dp->tag_ops = dst->tag_ops;
        dp->dst = dst;
 
        return 0;