net: dsa: Mock-up driver
Subsystems (3)
PATCH v2 1/1 net: dsa: Mock-up driver
This patch adds support for a DSA mock-up driver which essentially does the following:
- registers/unregisters 4 fixed PHYs to the slave network devices
- uses eth0 (configurable) as the master netdev
- registers the switch as a fixed MDIO device against the fixed MDIO bus at address 31
- includes dynamic debug prints for dsa_switch_ops functions that can be enabled to get call traces
This is a good way to test modular builds as well as exercise the DSA APIs without requiring access to real hardware. This does not test the data-path, although this could be added later on.
---
Changes in v2;
- removed an unnecessary change to include/linux/fixed_phy.h
drivers/net/dsa/Kconfig | 8 + drivers/net/dsa/Makefile | 2 +- drivers/net/dsa/dsa_loop.c | 329 ++++++++++++++++++++++++++++++++++++++ drivers/net/dsa/dsa_loop.h | 19 +++ drivers/net/dsa/dsa_loop_bdinfo.c | 34 ++++ 5 files changed, 391 insertions(+), 1 deletion(-) create mode 100644 drivers/net/dsa/dsa_loop.c create mode 100644 drivers/net/dsa/dsa_loop.h create mode 100644 drivers/net/dsa/dsa_loop_bdinfo.c
drivers/net/dsa/Kconfig +8 -0
@@ -34,4 +34,12 @@ config NET_DSA_QCA8K This enables support for the Qualcomm Atheros QCA8K Ethernet switch chips. +config NET_DSA_LOOP + tristate "DSA mock-up Ethernet switch chip support" + depends on NET_DSA + select FIXED_PHY + ---help--- + This enables support for a fake mock-up switch chip which + exercises the DSA APIs. + endmenu
drivers/net/dsa/Makefile +1 -1
@@ -2,6 +2,6 @@ obj-$(CONFIG_NET_DSA_MV88E6060) += mv88e6060.o obj-$(CONFIG_NET_DSA_BCM_SF2) += bcm-sf2.o bcm-sf2-objs := bcm_sf2.o bcm_sf2_cfp.o obj-$(CONFIG_NET_DSA_QCA8K) += qca8k.o - obj-y += b53/ obj-y += mv88e6xxx/ +obj-$(CONFIG_NET_DSA_LOOP) += dsa_loop.o dsa_loop_bdinfo.o
drivers/net/dsa/dsa_loop.c new file +329 -0
329 lines not on this page · raw message
drivers/net/dsa/dsa_loop.h new file +19 -0
@@ -0,0 +1,19 @@ +#ifndef __DSA_LOOP_H +#define __DSA_LOOP_H + +struct dsa_chip_data ; + +struct dsa_loop_pdata { +/* Must be first, such that dsa_register_switch() can access this +* without gory pointer manipulations +*/ +struct dsa_chip_data cd; +const char *name; +unsigned int enabled_ports; +const char *netdev; +}; + +#define DSA_LOOP_NUM_PORTS 6 +#define DSA_LOOP_CPU_PORT (DSA_LOOP_NUM_PORTS - 1) + +#endif /* __DSA_LOOP_H */
drivers/net/dsa/dsa_loop_bdinfo.c new file +34 -0
@@ -0,0 +1,34 @@ +#include <linux/kernel.h> +#include <linux/init.h> +#include <linux/phy.h> +#include <net/dsa.h> + +#include "dsa_loop.h" + +static struct dsa_loop_pdata dsa_loop_pdata = { + .cd = { + .port_names[0 ] ="lan1" , + .port_names[1 ] ="lan2" , + .port_names[2 ] ="lan3" , + .port_names[3 ] ="lan4" , + .port_names[DSA_LOOP_CPU_PORT ] ="cpu" , + }, + .name ="DSA mockup driver" , + .enabled_ports =0x1f , + .netdev ="eth0" , +}; + +static const struct mdio_board_info bdinfo = { + .bus_id ="fixed-0" , + .modalias ="dsa-loop" , + .mdio_addr =31 , + .platform_data = &dsa_loop_pdata, +}; + +static int __init dsa_loop_bdinfo_init (void ) +{ +return mdiobus_register_board_info (&bdinfo,1 ); +} +arch_initcall(dsa_loop_bdinfo_init) + +MODULE_LICENSE("GPL");
Signature · 1 line
2.9.3
Andrew Lunn
maintainerHi Florian
drivers/net/dsa/dsa_loop.c line 47
Quoted 6 lines
drivers/net/dsa/dsa_loop.c new file +6
-0+static enum dsa_tag_protocol dsa_loop_get_protocol (struct dsa_switch *ds) +{ +dev_dbg (ds->dev,"%s\n" , __func__); + +return DSA_TAG_PROTO_NONE ; +}
I'm wondering how safe this is:
static const struct dsa_device_ops none_ops = {
.xmit = dsa_slave_notag_xmit,
.rcv = NULL,
};
/*
* If the CPU connects to this switch, set the switch tree
* tagging protocol to the preferred tagging format of this
* switch.
*/
if (dst->cpu_switch == ds) {
enum dsa_tag_protocol tag_protocol;
tag_protocol = ops->get_tag_protocol(ds);
dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol);
if (IS_ERR(dst->tag_ops))
return PTR_ERR(dst->tag_ops);
dst->rcv = dst->tag_ops->rcv;
}static int dsa_switch_rcv(struct sk_buff *skb, struct net_device *dev,
struct packet_type *pt, struct net_device *orig_dev)
{
struct dsa_switch_tree *dst = dev->dsa_ptr;
if (unlikely(dst == NULL)) {
kfree_skb(skb);
return 0;
}
return dst->rcv(skb, dev, pt, orig_dev);
}static struct packet_type dsa_pack_type __read_mostly = {
.type = cpu_to_be16(ETH_P_XDSA),
.func = dsa_switch_rcv,
};
It looks like when a frame is received, we are going to dereference a NULL pointer.
Either we need a NOP rcv function, or we don't register dsa_pack_type if rcv is NULL.
Andrew
Hi Andrew,
Quoted 46 lines · Andrew Lunn
Hi Florian
drivers/net/dsa/dsa_loop.c line 47
Quoted 6 lines
drivers/net/dsa/dsa_loop.c new file +6
-0+static enum dsa_tag_protocol dsa_loop_get_protocol (struct dsa_switch *ds) +{ +dev_dbg (ds->dev,"%s\n" , __func__); + +return DSA_TAG_PROTO_NONE ; +}I'm wondering how safe this is:
static const struct
dsa_device_opsnone_ops= {
.xmit =dsa_slave_notag_xmit,
.rcv = NULL,
};/* * If the CPU connects to this switch, set the switch tree * tagging protocol to the preferred tagging format of this * switch. */ if (dst->cpu_switch == ds) { enum dsa_tag_protocol tag_protocol; tag_protocol = ops->get_tag_protocol(ds); dst->tag_ops = dsa_resolve_tag_protocol(tag_protocol); if (IS_ERR(dst->tag_ops)) return PTR_ERR(dst->tag_ops); dst->rcv = dst->tag_ops->rcv; }static int dsa_switch_rcv(struct
sk_buff*skb, structnet_device*dev,
structpacket_type*pt, structnet_device*orig_dev){ struct dsa_switch_tree *dst = dev->dsa_ptr; if (unlikely(dst == NULL)) { kfree_skb(skb); return 0; } return dst->rcv(skb, dev, pt, orig_dev); }
Quoted 7 lines
static struct
packet_typedsa_pack_type__read_mostly= {
.type =cpu_to_be16(ETH_P_XDSA),
.func =dsa_switch_rcv,
};It looks like when a frame is received, we are going to dereference a NULL pointer.
Actually we do not, because netdev_uses_dsa() returns true only when dst->rcv is different from NULL. When dst->rcv is NULL we completely bypass the DSA hook in eth_type_trans() and everything is well, the master network device is the one receiving packets.
This is actually the intended behavior for netdev_uses_dsa() because it really tells whether there is a DSA tagging protocol set-up and that is what NIC drivers (e.g: bcmsysport) would care about.
Thanks!
Signature · 1 line
Florian
Andrew Lunn
maintainerQuoted 4 lines
Actually we do not, because
netdev_uses_dsa()returns true only whendst->rcvis different from NULL. Whendst->rcvis NULL we completely bypass the DSA hook ineth_type_trans()and everything is well, the master network device is the one receiving packets.
static inline bool netdev_uses_dsa(struct net_device *dev) {
#if IS_ENABLED(CONFIG_NET_DSA)
if (dev->dsa_ptr != NULL)
return dsa_uses_tagged_protocol(dev->dsa_ptr);
#endif
return false;
}and
static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
{
return dst->rcv != NULL;
}Yep, i just needed to dig further...
Thanks
Andrew
David Miller
maintainerFrom: Florian Fainelli <[email protected]>
Date: Thu, 30 Mar 2017 18:43:21 -0700
Quoted 14 lines
This patch adds support for a DSA mock-up driver which essentially does the following:
- registers/unregisters 4 fixed PHYs to the slave network devices
- uses eth0 (configurable) as the master netdev
- registers the switch as a fixed MDIO device against the fixed MDIO bus at address 31
- includes dynamic debug prints fordsa_switch_opsfunctions that can be enabled to get call tracesThis is a good way to test modular builds as well as exercise the DSA APIs without requiring access to real hardware. This does not test the data-path, although this could be added later on.
Quoted 5 lines
Signed-off-by Florian Fainelli <[email protected]>---
Changes in v2;- removed an unnecessary change to
include/linux/fixed_phy.h
This looks extremely useful, applied, thanks!