Commit 3e8a72d1 authored by Florian Fainelli's avatar Florian Fainelli Committed by David S. Miller

net: dsa: reduce number of protocol hooks

DSA is currently registering one packet_type function per EtherType it
needs to intercept in the receive path of a DSA-enabled Ethernet device.
Right now we have three of them: trailer, DSA and eDSA, and there might
be more in the future, this will not scale to the addition of new
protocols.

This patch proceeds with adding a new layer of abstraction and two new
functions:

dsa_switch_rcv() which will dispatch into the tag-protocol specific
receive function implemented by net/dsa/tag_*.c

dsa_slave_xmit() which will dispatch into the tag-protocol specific
transmit function implemented by net/dsa/tag_*.c

When we do create the per-port slave network devices, we iterate over
the switch protocol to assign the DSA-specific receive and transmit
operations.

A new fake ethertype value is used: ETH_P_XDSA to illustrate the fact
that this is no longer going to look like ETH_P_DSA or ETH_P_TRAILER
like it used to be.

This allows us to greatly simplify the check in eth_type_trans() and
always override the skb->protocol with ETH_P_XDSA for Ethernet switches
tagged protocol, while also reducing the number repetitive slave
netdevice_ops assignments.
Signed-off-by: default avatarFlorian Fainelli <f.fainelli@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 8663dc20
...@@ -1781,24 +1781,13 @@ void dev_net_set(struct net_device *dev, struct net *net) ...@@ -1781,24 +1781,13 @@ void dev_net_set(struct net_device *dev, struct net *net)
#endif #endif
} }
static inline bool netdev_uses_dsa_tags(struct net_device *dev) static inline bool netdev_uses_dsa(struct net_device *dev)
{ {
#ifdef CONFIG_NET_DSA_TAG_DSA #ifdef CONFIG_NET_DSA
if (dev->dsa_ptr != NULL) return dev->dsa_ptr != NULL;
return dsa_uses_dsa_tags(dev->dsa_ptr); #else
#endif return false;
return 0;
}
static inline bool netdev_uses_trailer_tags(struct net_device *dev)
{
#ifdef CONFIG_NET_DSA_TAG_TRAILER
if (dev->dsa_ptr != NULL)
return dsa_uses_trailer_tags(dev->dsa_ptr);
#endif #endif
return 0;
} }
/** /**
...@@ -1933,6 +1922,13 @@ struct udp_offload { ...@@ -1933,6 +1922,13 @@ struct udp_offload {
struct offload_callbacks callbacks; struct offload_callbacks callbacks;
}; };
struct dsa_device_ops {
netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
int (*rcv)(struct sk_buff *skb, struct net_device *dev,
struct packet_type *pt, struct net_device *orig_dev);
};
/* often modified stats are per cpu, other are shared (netdev->stats) */ /* often modified stats are per cpu, other are shared (netdev->stats) */
struct pcpu_sw_netstats { struct pcpu_sw_netstats {
u64 rx_packets; u64 rx_packets;
......
...@@ -59,6 +59,8 @@ struct dsa_platform_data { ...@@ -59,6 +59,8 @@ struct dsa_platform_data {
struct dsa_chip_data *chip; struct dsa_chip_data *chip;
}; };
struct dsa_device_ops;
struct dsa_switch_tree { struct dsa_switch_tree {
/* /*
* Configuration data for the platform device that owns * Configuration data for the platform device that owns
...@@ -71,6 +73,7 @@ struct dsa_switch_tree { ...@@ -71,6 +73,7 @@ struct dsa_switch_tree {
* protocol to use. * protocol to use.
*/ */
struct net_device *master_netdev; struct net_device *master_netdev;
const struct dsa_device_ops *ops;
__be16 tag_protocol; __be16 tag_protocol;
/* /*
...@@ -186,21 +189,4 @@ static inline void *ds_to_priv(struct dsa_switch *ds) ...@@ -186,21 +189,4 @@ static inline void *ds_to_priv(struct dsa_switch *ds)
return (void *)(ds + 1); return (void *)(ds + 1);
} }
/*
* The original DSA tag format and some other tag formats have no
* ethertype, which means that we need to add a little hack to the
* networking receive path to make sure that received frames get
* the right ->protocol assigned to them when one of those tag
* formats is in use.
*/
static inline bool dsa_uses_dsa_tags(struct dsa_switch_tree *dst)
{
return !!(dst->tag_protocol == htons(ETH_P_DSA));
}
static inline bool dsa_uses_trailer_tags(struct dsa_switch_tree *dst)
{
return !!(dst->tag_protocol == htons(ETH_P_TRAILER));
}
#endif #endif
...@@ -128,6 +128,7 @@ ...@@ -128,6 +128,7 @@
#define ETH_P_PHONET 0x00F5 /* Nokia Phonet frames */ #define ETH_P_PHONET 0x00F5 /* Nokia Phonet frames */
#define ETH_P_IEEE802154 0x00F6 /* IEEE802.15.4 frame */ #define ETH_P_IEEE802154 0x00F6 /* IEEE802.15.4 frame */
#define ETH_P_CAIF 0x00F7 /* ST-Ericsson CAIF protocol */ #define ETH_P_CAIF 0x00F7 /* ST-Ericsson CAIF protocol */
#define ETH_P_XDSA 0x00F8 /* Multiplexed DSA protocol */
/* /*
* This is an Ethernet frame header. * This is an Ethernet frame header.
......
...@@ -608,6 +608,24 @@ static void dsa_shutdown(struct platform_device *pdev) ...@@ -608,6 +608,24 @@ static void dsa_shutdown(struct platform_device *pdev)
{ {
} }
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->ops->rcv(skb, dev, pt, orig_dev);
}
struct packet_type dsa_pack_type __read_mostly = {
.type = cpu_to_be16(ETH_P_XDSA),
.func = dsa_switch_rcv,
};
static const struct of_device_id dsa_of_match_table[] = { static const struct of_device_id dsa_of_match_table[] = {
{ .compatible = "marvell,dsa", }, { .compatible = "marvell,dsa", },
{} {}
...@@ -633,30 +651,15 @@ static int __init dsa_init_module(void) ...@@ -633,30 +651,15 @@ static int __init dsa_init_module(void)
if (rc) if (rc)
return rc; return rc;
#ifdef CONFIG_NET_DSA_TAG_DSA dev_add_pack(&dsa_pack_type);
dev_add_pack(&dsa_packet_type);
#endif
#ifdef CONFIG_NET_DSA_TAG_EDSA
dev_add_pack(&edsa_packet_type);
#endif
#ifdef CONFIG_NET_DSA_TAG_TRAILER
dev_add_pack(&trailer_packet_type);
#endif
return 0; return 0;
} }
module_init(dsa_init_module); module_init(dsa_init_module);
static void __exit dsa_cleanup_module(void) static void __exit dsa_cleanup_module(void)
{ {
#ifdef CONFIG_NET_DSA_TAG_TRAILER dev_remove_pack(&dsa_pack_type);
dev_remove_pack(&trailer_packet_type);
#endif
#ifdef CONFIG_NET_DSA_TAG_EDSA
dev_remove_pack(&edsa_packet_type);
#endif
#ifdef CONFIG_NET_DSA_TAG_DSA
dev_remove_pack(&dsa_packet_type);
#endif
platform_driver_unregister(&dsa_driver); platform_driver_unregister(&dsa_driver);
} }
module_exit(dsa_cleanup_module); module_exit(dsa_cleanup_module);
......
...@@ -45,16 +45,13 @@ struct net_device *dsa_slave_create(struct dsa_switch *ds, ...@@ -45,16 +45,13 @@ struct net_device *dsa_slave_create(struct dsa_switch *ds,
int port, char *name); int port, char *name);
/* tag_dsa.c */ /* tag_dsa.c */
netdev_tx_t dsa_xmit(struct sk_buff *skb, struct net_device *dev); extern const struct dsa_device_ops dsa_netdev_ops;
extern struct packet_type dsa_packet_type;
/* tag_edsa.c */ /* tag_edsa.c */
netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev); extern const struct dsa_device_ops edsa_netdev_ops;
extern struct packet_type edsa_packet_type;
/* tag_trailer.c */ /* tag_trailer.c */
netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev); extern const struct dsa_device_ops trailer_netdev_ops;
extern struct packet_type trailer_packet_type;
#endif #endif
...@@ -171,6 +171,14 @@ static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) ...@@ -171,6 +171,14 @@ static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
return -EOPNOTSUPP; return -EOPNOTSUPP;
} }
static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct dsa_slave_priv *p = netdev_priv(dev);
struct dsa_switch_tree *dst = p->parent->dst;
return dst->ops->xmit(skb, dev);
}
/* ethtool operations *******************************************************/ /* ethtool operations *******************************************************/
static int static int
...@@ -293,42 +301,16 @@ static const struct ethtool_ops dsa_slave_ethtool_ops = { ...@@ -293,42 +301,16 @@ static const struct ethtool_ops dsa_slave_ethtool_ops = {
.get_sset_count = dsa_slave_get_sset_count, .get_sset_count = dsa_slave_get_sset_count,
}; };
#ifdef CONFIG_NET_DSA_TAG_DSA static const struct net_device_ops dsa_slave_netdev_ops = {
static const struct net_device_ops dsa_netdev_ops = {
.ndo_init = dsa_slave_init, .ndo_init = dsa_slave_init,
.ndo_open = dsa_slave_open, .ndo_open = dsa_slave_open,
.ndo_stop = dsa_slave_close, .ndo_stop = dsa_slave_close,
.ndo_start_xmit = dsa_xmit, .ndo_start_xmit = dsa_slave_xmit,
.ndo_change_rx_flags = dsa_slave_change_rx_flags, .ndo_change_rx_flags = dsa_slave_change_rx_flags,
.ndo_set_rx_mode = dsa_slave_set_rx_mode, .ndo_set_rx_mode = dsa_slave_set_rx_mode,
.ndo_set_mac_address = dsa_slave_set_mac_address, .ndo_set_mac_address = dsa_slave_set_mac_address,
.ndo_do_ioctl = dsa_slave_ioctl, .ndo_do_ioctl = dsa_slave_ioctl,
}; };
#endif
#ifdef CONFIG_NET_DSA_TAG_EDSA
static const struct net_device_ops edsa_netdev_ops = {
.ndo_init = dsa_slave_init,
.ndo_open = dsa_slave_open,
.ndo_stop = dsa_slave_close,
.ndo_start_xmit = edsa_xmit,
.ndo_change_rx_flags = dsa_slave_change_rx_flags,
.ndo_set_rx_mode = dsa_slave_set_rx_mode,
.ndo_set_mac_address = dsa_slave_set_mac_address,
.ndo_do_ioctl = dsa_slave_ioctl,
};
#endif
#ifdef CONFIG_NET_DSA_TAG_TRAILER
static const struct net_device_ops trailer_netdev_ops = {
.ndo_init = dsa_slave_init,
.ndo_open = dsa_slave_open,
.ndo_stop = dsa_slave_close,
.ndo_start_xmit = trailer_xmit,
.ndo_change_rx_flags = dsa_slave_change_rx_flags,
.ndo_set_rx_mode = dsa_slave_set_rx_mode,
.ndo_set_mac_address = dsa_slave_set_mac_address,
.ndo_do_ioctl = dsa_slave_ioctl,
};
#endif
/* slave device setup *******************************************************/ /* slave device setup *******************************************************/
struct net_device * struct net_device *
...@@ -349,21 +331,22 @@ dsa_slave_create(struct dsa_switch *ds, struct device *parent, ...@@ -349,21 +331,22 @@ dsa_slave_create(struct dsa_switch *ds, struct device *parent,
slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
eth_hw_addr_inherit(slave_dev, master); eth_hw_addr_inherit(slave_dev, master);
slave_dev->tx_queue_len = 0; slave_dev->tx_queue_len = 0;
slave_dev->netdev_ops = &dsa_slave_netdev_ops;
switch (ds->dst->tag_protocol) { switch (ds->dst->tag_protocol) {
#ifdef CONFIG_NET_DSA_TAG_DSA #ifdef CONFIG_NET_DSA_TAG_DSA
case htons(ETH_P_DSA): case htons(ETH_P_DSA):
slave_dev->netdev_ops = &dsa_netdev_ops; ds->dst->ops = &dsa_netdev_ops;
break; break;
#endif #endif
#ifdef CONFIG_NET_DSA_TAG_EDSA #ifdef CONFIG_NET_DSA_TAG_EDSA
case htons(ETH_P_EDSA): case htons(ETH_P_EDSA):
slave_dev->netdev_ops = &edsa_netdev_ops; ds->dst->ops = &edsa_netdev_ops;
break; break;
#endif #endif
#ifdef CONFIG_NET_DSA_TAG_TRAILER #ifdef CONFIG_NET_DSA_TAG_TRAILER
case htons(ETH_P_TRAILER): case htons(ETH_P_TRAILER):
slave_dev->netdev_ops = &trailer_netdev_ops; ds->dst->ops = &trailer_netdev_ops;
break; break;
#endif #endif
default: default:
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#define DSA_HLEN 4 #define DSA_HLEN 4
netdev_tx_t dsa_xmit(struct sk_buff *skb, struct net_device *dev) static netdev_tx_t dsa_xmit(struct sk_buff *skb, struct net_device *dev)
{ {
struct dsa_slave_priv *p = netdev_priv(dev); struct dsa_slave_priv *p = netdev_priv(dev);
u8 *dsa_header; u8 *dsa_header;
...@@ -186,7 +186,7 @@ out: ...@@ -186,7 +186,7 @@ out:
return 0; return 0;
} }
struct packet_type dsa_packet_type __read_mostly = { const struct dsa_device_ops dsa_netdev_ops = {
.type = cpu_to_be16(ETH_P_DSA), .xmit = dsa_xmit,
.func = dsa_rcv, .rcv = dsa_rcv,
}; };
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#define DSA_HLEN 4 #define DSA_HLEN 4
#define EDSA_HLEN 8 #define EDSA_HLEN 8
netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev) static netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev)
{ {
struct dsa_slave_priv *p = netdev_priv(dev); struct dsa_slave_priv *p = netdev_priv(dev);
u8 *edsa_header; u8 *edsa_header;
...@@ -205,7 +205,7 @@ out: ...@@ -205,7 +205,7 @@ out:
return 0; return 0;
} }
struct packet_type edsa_packet_type __read_mostly = { const struct dsa_device_ops edsa_netdev_ops = {
.type = cpu_to_be16(ETH_P_EDSA), .xmit = edsa_xmit,
.func = edsa_rcv, .rcv = edsa_rcv,
}; };
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include "dsa_priv.h" #include "dsa_priv.h"
netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev) static netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
{ {
struct dsa_slave_priv *p = netdev_priv(dev); struct dsa_slave_priv *p = netdev_priv(dev);
struct sk_buff *nskb; struct sk_buff *nskb;
...@@ -114,7 +114,7 @@ out: ...@@ -114,7 +114,7 @@ out:
return 0; return 0;
} }
struct packet_type trailer_packet_type __read_mostly = { const struct dsa_device_ops trailer_netdev_ops = {
.type = cpu_to_be16(ETH_P_TRAILER), .xmit = trailer_xmit,
.func = trailer_rcv, .rcv = trailer_rcv,
}; };
...@@ -181,11 +181,8 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev) ...@@ -181,11 +181,8 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
* variants has been configured on the receiving interface, * variants has been configured on the receiving interface,
* and if so, set skb->protocol without looking at the packet. * and if so, set skb->protocol without looking at the packet.
*/ */
if (unlikely(netdev_uses_dsa_tags(dev))) if (unlikely(netdev_uses_dsa(dev)))
return htons(ETH_P_DSA); return htons(ETH_P_XDSA);
if (unlikely(netdev_uses_trailer_tags(dev)))
return htons(ETH_P_TRAILER);
if (likely(ntohs(eth->h_proto) >= ETH_P_802_3_MIN)) if (likely(ntohs(eth->h_proto) >= ETH_P_802_3_MIN))
return eth->h_proto; return eth->h_proto;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment