net: dsa: Refactor transmit path to eliminate duplication
authorFlorian Fainelli <f.fainelli@gmail.com>
Fri, 31 Jul 2015 18:42:56 +0000 (11:42 -0700)
committerDavid S. Miller <davem@davemloft.net>
Fri, 31 Jul 2015 22:45:37 +0000 (15:45 -0700)
All tagging protocols do the same thing: increment device statistics,
make room for the tag to be inserted, create the tag, invoke the parent
network device transmit function.

In order to prepare for adding netpoll support, which requires the tag
creation, but not using the parent network device transmit function, do
some little refactoring which eliminates duplication between the 4
tagging protocols supported.

We need to return a sk_buff pointer back to the caller because the tag
specific transmit function may have to reallocate the original skb (e.g:
tag_trailer.c) and this is the one we should be transmitting, not the
original sk_buff we were passed.

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/dsa/dsa_priv.h
net/dsa/slave.c
net/dsa/tag_brcm.c
net/dsa/tag_dsa.c
net/dsa/tag_edsa.c
net/dsa/tag_trailer.c

index d5f1f9b..eeade90 100644 (file)
@@ -15,7 +15,7 @@
 #include <linux/netdevice.h>
 
 struct dsa_device_ops {
-       netdev_tx_t (*xmit)(struct sk_buff *skb, struct net_device *dev);
+       struct sk_buff *(*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);
 };
@@ -26,7 +26,7 @@ struct dsa_slave_priv {
         * switch port.
         */
        struct net_device       *dev;
-       netdev_tx_t             (*xmit)(struct sk_buff *skb,
+       struct sk_buff *        (*xmit)(struct sk_buff *skb,
                                        struct net_device *dev);
 
        /*
index 0917123..5fc87ee 100644 (file)
@@ -421,21 +421,32 @@ static int dsa_slave_port_attr_get(struct net_device *dev,
 static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct dsa_slave_priv *p = netdev_priv(dev);
+       struct sk_buff *nskb;
 
-       return p->xmit(skb, dev);
-}
+       dev->stats.tx_packets++;
+       dev->stats.tx_bytes += skb->len;
 
-static netdev_tx_t dsa_slave_notag_xmit(struct sk_buff *skb,
-                                       struct net_device *dev)
-{
-       struct dsa_slave_priv *p = netdev_priv(dev);
+       /* Transmit function may have to reallocate the original SKB */
+       nskb = p->xmit(skb, dev);
+       if (!nskb)
+               return NETDEV_TX_OK;
 
-       skb->dev = p->parent->dst->master_netdev;
-       dev_queue_xmit(skb);
+       /* Queue the SKB for transmission on the parent interface, but
+        * do not modify its EtherType
+        */
+       nskb->dev = p->parent->dst->master_netdev;
+       dev_queue_xmit(nskb);
 
        return NETDEV_TX_OK;
 }
 
+static struct sk_buff *dsa_slave_notag_xmit(struct sk_buff *skb,
+                                           struct net_device *dev)
+{
+       /* Just return the original SKB */
+       return skb;
+}
+
 
 /* ethtool operations *******************************************************/
 static int
index 83d3572..e2aadb7 100644 (file)
 #define BRCM_EG_TC_MASK                0x7
 #define BRCM_EG_PID_MASK       0x1f
 
-static netdev_tx_t brcm_tag_xmit(struct sk_buff *skb, struct net_device *dev)
+static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct dsa_slave_priv *p = netdev_priv(dev);
        u8 *brcm_tag;
 
-       dev->stats.tx_packets++;
-       dev->stats.tx_bytes += skb->len;
-
        if (skb_cow_head(skb, BRCM_TAG_LEN) < 0)
                goto out_free;
 
@@ -87,17 +84,11 @@ static netdev_tx_t brcm_tag_xmit(struct sk_buff *skb, struct net_device *dev)
                brcm_tag[2] = BRCM_IG_DSTMAP2_MASK;
        brcm_tag[3] = (1 << p->port) & BRCM_IG_DSTMAP1_MASK;
 
-       /* Queue the SKB for transmission on the parent interface, but
-        * do not modify its EtherType
-        */
-       skb->dev = p->parent->dst->master_netdev;
-       dev_queue_xmit(skb);
-
-       return NETDEV_TX_OK;
+       return skb;
 
 out_free:
        kfree_skb(skb);
-       return NETDEV_TX_OK;
+       return NULL;
 }
 
 static int brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev,
index 2dab270..aa780e4 100644 (file)
 
 #define DSA_HLEN       4
 
-static netdev_tx_t dsa_xmit(struct sk_buff *skb, struct net_device *dev)
+static struct sk_buff *dsa_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct dsa_slave_priv *p = netdev_priv(dev);
        u8 *dsa_header;
 
-       dev->stats.tx_packets++;
-       dev->stats.tx_bytes += skb->len;
-
        /*
         * Convert the outermost 802.1q tag to a DSA tag for tagged
         * packets, or insert a DSA tag between the addresses and
@@ -63,14 +60,11 @@ static netdev_tx_t dsa_xmit(struct sk_buff *skb, struct net_device *dev)
                dsa_header[3] = 0x00;
        }
 
-       skb->dev = p->parent->dst->master_netdev;
-       dev_queue_xmit(skb);
-
-       return NETDEV_TX_OK;
+       return skb;
 
 out_free:
        kfree_skb(skb);
-       return NETDEV_TX_OK;
+       return NULL;
 }
 
 static int dsa_rcv(struct sk_buff *skb, struct net_device *dev,
index 9aeda59..2288c80 100644 (file)
 #define DSA_HLEN       4
 #define EDSA_HLEN      8
 
-static netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev)
+static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct dsa_slave_priv *p = netdev_priv(dev);
        u8 *edsa_header;
 
-       dev->stats.tx_packets++;
-       dev->stats.tx_bytes += skb->len;
-
        /*
         * Convert the outermost 802.1q tag to a DSA tag and prepend
         * a DSA ethertype field is the packet is tagged, or insert
@@ -76,14 +73,11 @@ static netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev)
                edsa_header[7] = 0x00;
        }
 
-       skb->dev = p->parent->dst->master_netdev;
-       dev_queue_xmit(skb);
-
-       return NETDEV_TX_OK;
+       return skb;
 
 out_free:
        kfree_skb(skb);
-       return NETDEV_TX_OK;
+       return NULL;
 }
 
 static int edsa_rcv(struct sk_buff *skb, struct net_device *dev,
index e268f9d..d25efc9 100644 (file)
 #include <linux/slab.h>
 #include "dsa_priv.h"
 
-static netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
+static struct sk_buff *trailer_xmit(struct sk_buff *skb, struct net_device *dev)
 {
        struct dsa_slave_priv *p = netdev_priv(dev);
        struct sk_buff *nskb;
        int padlen;
        u8 *trailer;
 
-       dev->stats.tx_packets++;
-       dev->stats.tx_bytes += skb->len;
-
        /*
         * We have to make sure that the trailer ends up as the very
         * last 4 bytes of the packet.  This means that we have to pad
@@ -36,7 +33,7 @@ static netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
        nskb = alloc_skb(NET_IP_ALIGN + skb->len + padlen + 4, GFP_ATOMIC);
        if (nskb == NULL) {
                kfree_skb(skb);
-               return NETDEV_TX_OK;
+               return NULL;
        }
        skb_reserve(nskb, NET_IP_ALIGN);
 
@@ -57,10 +54,7 @@ static netdev_tx_t trailer_xmit(struct sk_buff *skb, struct net_device *dev)
        trailer[2] = 0x10;
        trailer[3] = 0x00;
 
-       nskb->dev = p->parent->dst->master_netdev;
-       dev_queue_xmit(nskb);
-
-       return NETDEV_TX_OK;
+       return nskb;
 }
 
 static int trailer_rcv(struct sk_buff *skb, struct net_device *dev,