net: dsa: Expose tagging protocol to user-space
authorFlorian Fainelli <f.fainelli@gmail.com>
Fri, 7 Sep 2018 18:09:02 +0000 (11:09 -0700)
committerDavid S. Miller <davem@davemloft.net>
Fri, 7 Sep 2018 21:49:55 +0000 (14:49 -0700)
There is no way for user-space to know what a given DSA network device's
tagging protocol is. Expose this information through a dsa/tagging
attribute which reflects the tagging protocol currently in use.

This is helpful for configuration (e.g: none behaves dramatically
different wrt. bridges) as well as for packet capture tools when there
is not a proper Ethernet type available.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Documentation/ABI/testing/sysfs-class-net-dsa [new file with mode: 0644]
net/dsa/dsa.c
net/dsa/dsa_priv.h
net/dsa/slave.c

diff --git a/Documentation/ABI/testing/sysfs-class-net-dsa b/Documentation/ABI/testing/sysfs-class-net-dsa
new file mode 100644 (file)
index 0000000..f240221
--- /dev/null
@@ -0,0 +1,7 @@
+What:          /sys/class/net/<iface>/tagging
+Date:          August 2018
+KernelVersion: 4.20
+Contact:       netdev@vger.kernel.org
+Description:
+               String indicating the type of tagging protocol used by the
+               DSA slave network device.
index 9f3209ff7ffde754230720ca9117111fdbd7c3bc..45f70859f550a2b03bbac51c9f3f3e1cec121b5a 100644 (file)
@@ -70,6 +70,49 @@ const struct dsa_device_ops *dsa_device_ops[DSA_TAG_LAST] = {
        [DSA_TAG_PROTO_NONE] = &none_ops,
 };
 
+const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops)
+{
+       const char *protocol_name[DSA_TAG_LAST] = {
+#ifdef CONFIG_NET_DSA_TAG_BRCM
+               [DSA_TAG_PROTO_BRCM] = "brcm",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND
+               [DSA_TAG_PROTO_BRCM_PREPEND] = "brcm-prepend",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_DSA
+               [DSA_TAG_PROTO_DSA] = "dsa",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_EDSA
+               [DSA_TAG_PROTO_EDSA] = "edsa",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_KSZ
+               [DSA_TAG_PROTO_KSZ] = "ksz",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_LAN9303
+               [DSA_TAG_PROTO_LAN9303] = "lan9303",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_MTK
+               [DSA_TAG_PROTO_MTK] = "mtk",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_QCA
+               [DSA_TAG_PROTO_QCA] = "qca",
+#endif
+#ifdef CONFIG_NET_DSA_TAG_TRAILER
+               [DSA_TAG_PROTO_TRAILER] = "trailer",
+#endif
+               [DSA_TAG_PROTO_NONE] = "none",
+       };
+       unsigned int i;
+
+       BUILD_BUG_ON(ARRAY_SIZE(protocol_name) != DSA_TAG_LAST);
+
+       for (i = 0; i < ARRAY_SIZE(dsa_device_ops); i++)
+               if (ops == dsa_device_ops[i])
+                       return protocol_name[i];
+
+       return protocol_name[DSA_TAG_PROTO_NONE];
+};
+
 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol)
 {
        const struct dsa_device_ops *ops;
index 3964c6f7a7c0d7c6436672c221c2d58802f7227c..2868b5bb7e7de1e1fbf4678c9f9c29b04ec58bee 100644 (file)
@@ -86,6 +86,7 @@ struct dsa_slave_priv {
 /* dsa.c */
 const struct dsa_device_ops *dsa_resolve_tag_protocol(int tag_protocol);
 bool dsa_schedule_work(struct work_struct *work);
+const char *dsa_tag_protocol_to_str(const struct dsa_device_ops *ops);
 
 /* legacy.c */
 #if IS_ENABLED(CONFIG_NET_DSA_LEGACY)
index 1c45c1d6d241dbcca8542c51eb0f25b903131756..3f840b6eea692097895449f6a50bfc2b59724d67 100644 (file)
@@ -1058,6 +1058,27 @@ static struct device_type dsa_type = {
        .name   = "dsa",
 };
 
+static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
+                           char *buf)
+{
+       struct net_device *dev = to_net_dev(d);
+       struct dsa_port *dp = dsa_slave_to_port(dev);
+
+       return sprintf(buf, "%s\n",
+                      dsa_tag_protocol_to_str(dp->cpu_dp->tag_ops));
+}
+static DEVICE_ATTR_RO(tagging);
+
+static struct attribute *dsa_slave_attrs[] = {
+       &dev_attr_tagging.attr,
+       NULL
+};
+
+static const struct attribute_group dsa_group = {
+       .name   = "dsa",
+       .attrs  = dsa_slave_attrs,
+};
+
 static void dsa_slave_phylink_validate(struct net_device *dev,
                                       unsigned long *supported,
                                       struct phylink_link_state *state)
@@ -1353,8 +1374,14 @@ int dsa_slave_create(struct dsa_port *port)
                goto out_phy;
        }
 
+       ret = sysfs_create_group(&slave_dev->dev.kobj, &dsa_group);
+       if (ret)
+               goto out_unreg;
+
        return 0;
 
+out_unreg:
+       unregister_netdev(slave_dev);
 out_phy:
        rtnl_lock();
        phylink_disconnect_phy(p->dp->pl);
@@ -1378,6 +1405,7 @@ void dsa_slave_destroy(struct net_device *slave_dev)
        rtnl_unlock();
 
        dsa_slave_notify(slave_dev, DSA_PORT_UNREGISTER);
+       sysfs_remove_group(&slave_dev->dev.kobj, &dsa_group);
        unregister_netdev(slave_dev);
        phylink_destroy(dp->pl);
        free_percpu(p->stats64);