ixgb and e1000: Use new function for copybreak tests
[safe/jmp/linux-2.6] / drivers / net / virtio_net.c
index 9d8984a..b0a85d0 100644 (file)
@@ -25,6 +25,7 @@
 #include <linux/virtio_net.h>
 #include <linux/scatterlist.h>
 #include <linux/if_vlan.h>
+#include <linux/slab.h>
 
 static int napi_weight = 128;
 module_param(napi_weight, int, 0444);
@@ -39,8 +40,7 @@ module_param(gso, bool, 0444);
 
 #define VIRTNET_SEND_COMMAND_SG_MAX    2
 
-struct virtnet_info
-{
+struct virtnet_info {
        struct virtio_device *vdev;
        struct virtqueue *rvq, *svq, *cvq;
        struct net_device *dev;
@@ -56,14 +56,15 @@ struct virtnet_info
        /* Host will merge rx buffers for big packets (shake it! shake it!) */
        bool mergeable_rx_bufs;
 
-       /* Send queue. */
-       struct sk_buff_head send;
-
        /* Work struct for refilling if we run low on memory. */
        struct delayed_work refill;
 
        /* Chain pages by the private ptr. */
        struct page *pages;
+
+       /* fragments + linear part + virtio header */
+       struct scatterlist rx_sg[MAX_SKB_FRAGS + 2];
+       struct scatterlist tx_sg[MAX_SKB_FRAGS + 2];
 };
 
 struct skb_vnet_hdr {
@@ -326,7 +327,6 @@ static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
 {
        struct sk_buff *skb;
        struct skb_vnet_hdr *hdr;
-       struct scatterlist sg[2];
        int err;
 
        skb = netdev_alloc_skb_ip_align(vi->dev, MAX_PACKET_LEN);
@@ -336,11 +336,11 @@ static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
        skb_put(skb, MAX_PACKET_LEN);
 
        hdr = skb_vnet_hdr(skb);
-       sg_set_buf(sg, &hdr->hdr, sizeof hdr->hdr);
+       sg_set_buf(vi->rx_sg, &hdr->hdr, sizeof hdr->hdr);
 
-       skb_to_sgvec(skb, sg + 1, 0, skb->len);
+       skb_to_sgvec(skb, vi->rx_sg + 1, 0, skb->len);
 
-       err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 2, skb);
+       err = vi->rvq->vq_ops->add_buf(vi->rvq, vi->rx_sg, 0, 2, skb);
        if (err < 0)
                dev_kfree_skb(skb);
 
@@ -349,12 +349,11 @@ static int add_recvbuf_small(struct virtnet_info *vi, gfp_t gfp)
 
 static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
 {
-       struct scatterlist sg[MAX_SKB_FRAGS + 2];
        struct page *first, *list = NULL;
        char *p;
        int i, err, offset;
 
-       /* page in sg[MAX_SKB_FRAGS + 1] is list tail */
+       /* page in vi->rx_sg[MAX_SKB_FRAGS + 1] is list tail */
        for (i = MAX_SKB_FRAGS + 1; i > 1; --i) {
                first = get_a_page(vi, gfp);
                if (!first) {
@@ -362,7 +361,7 @@ static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
                                give_pages(vi, list);
                        return -ENOMEM;
                }
-               sg_set_buf(&sg[i], page_address(first), PAGE_SIZE);
+               sg_set_buf(&vi->rx_sg[i], page_address(first), PAGE_SIZE);
 
                /* chain new page in list head to match sg */
                first->private = (unsigned long)list;
@@ -376,17 +375,17 @@ static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
        }
        p = page_address(first);
 
-       /* sg[0], sg[1] share the same page */
-       /* a separated sg[0] for  virtio_net_hdr only during to QEMU bug*/
-       sg_set_buf(&sg[0], p, sizeof(struct virtio_net_hdr));
+       /* vi->rx_sg[0], vi->rx_sg[1] share the same page */
+       /* a separated vi->rx_sg[0] for virtio_net_hdr only due to QEMU bug */
+       sg_set_buf(&vi->rx_sg[0], p, sizeof(struct virtio_net_hdr));
 
-       /* sg[1] for data packet, from offset */
+       /* vi->rx_sg[1] for data packet, from offset */
        offset = sizeof(struct padded_vnet_hdr);
-       sg_set_buf(&sg[1], p + offset, PAGE_SIZE - offset);
+       sg_set_buf(&vi->rx_sg[1], p + offset, PAGE_SIZE - offset);
 
        /* chain first in list head */
        first->private = (unsigned long)list;
-       err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, MAX_SKB_FRAGS + 2,
+       err = vi->rvq->vq_ops->add_buf(vi->rvq, vi->rx_sg, 0, MAX_SKB_FRAGS + 2,
                                       first);
        if (err < 0)
                give_pages(vi, first);
@@ -397,16 +396,15 @@ static int add_recvbuf_big(struct virtnet_info *vi, gfp_t gfp)
 static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
 {
        struct page *page;
-       struct scatterlist sg;
        int err;
 
        page = get_a_page(vi, gfp);
        if (!page)
                return -ENOMEM;
 
-       sg_init_one(&sg, page_address(page), PAGE_SIZE);
+       sg_init_one(vi->rx_sg, page_address(page), PAGE_SIZE);
 
-       err = vi->rvq->vq_ops->add_buf(vi->rvq, &sg, 0, 1, page);
+       err = vi->rvq->vq_ops->add_buf(vi->rvq, vi->rx_sg, 0, 1, page);
        if (err < 0)
                give_pages(vi, page);
 
@@ -505,7 +503,6 @@ static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
 
        while ((skb = vi->svq->vq_ops->get_buf(vi->svq, &len)) != NULL) {
                pr_debug("Sent skb %p\n", skb);
-               __skb_unlink(skb, &vi->send);
                vi->dev->stats.tx_bytes += skb->len;
                vi->dev->stats.tx_packets++;
                tot_sgs += skb_vnet_hdr(skb)->num_sg;
@@ -516,12 +513,9 @@ static unsigned int free_old_xmit_skbs(struct virtnet_info *vi)
 
 static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
 {
-       struct scatterlist sg[2+MAX_SKB_FRAGS];
        struct skb_vnet_hdr *hdr = skb_vnet_hdr(skb);
        const unsigned char *dest = ((struct ethhdr *)skb->data)->h_dest;
 
-       sg_init_table(sg, 2+MAX_SKB_FRAGS);
-
        pr_debug("%s: xmit %p %pM\n", vi->dev->name, skb, dest);
 
        if (skb->ip_summed == CHECKSUM_PARTIAL) {
@@ -555,12 +549,13 @@ static int xmit_skb(struct virtnet_info *vi, struct sk_buff *skb)
 
        /* Encode metadata header at front. */
        if (vi->mergeable_rx_bufs)
-               sg_set_buf(sg, &hdr->mhdr, sizeof hdr->mhdr);
+               sg_set_buf(vi->tx_sg, &hdr->mhdr, sizeof hdr->mhdr);
        else
-               sg_set_buf(sg, &hdr->hdr, sizeof hdr->hdr);
+               sg_set_buf(vi->tx_sg, &hdr->hdr, sizeof hdr->hdr);
 
-       hdr->num_sg = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1;
-       return vi->svq->vq_ops->add_buf(vi->svq, sg, hdr->num_sg, 0, skb);
+       hdr->num_sg = skb_to_sgvec(skb, vi->tx_sg + 1, 0, skb->len) + 1;
+       return vi->svq->vq_ops->add_buf(vi->svq, vi->tx_sg, hdr->num_sg,
+                                       0, skb);
 }
 
 static netdev_tx_t start_xmit(struct sk_buff *skb, struct net_device *dev)
@@ -588,15 +583,6 @@ again:
        }
        vi->svq->vq_ops->kick(vi->svq);
 
-       /*
-        * Put new one in send queue.  You'd expect we'd need this before
-        * xmit_skb calls add_buf(), since the callback can be triggered
-        * immediately after that.  But since the callback just triggers
-        * another call back here, normal network xmit locking prevents the
-        * race.
-        */
-       __skb_queue_head(&vi->send, skb);
-
        /* Don't wait up for transmitted skbs to be freed. */
        skb_orphan(skb);
        nf_reset(skb);
@@ -732,9 +718,9 @@ static void virtnet_set_rx_mode(struct net_device *dev)
        struct scatterlist sg[2];
        u8 promisc, allmulti;
        struct virtio_net_ctrl_mac *mac_data;
-       struct dev_addr_list *addr;
        struct netdev_hw_addr *ha;
        int uc_count;
+       int mc_count;
        void *buf;
        int i;
 
@@ -762,9 +748,11 @@ static void virtnet_set_rx_mode(struct net_device *dev)
                         allmulti ? "en" : "dis");
 
        uc_count = netdev_uc_count(dev);
+       mc_count = netdev_mc_count(dev);
        /* MAC filter - use one buffer for both lists */
-       mac_data = buf = kzalloc(((uc_count + dev->mc_count) * ETH_ALEN) +
-                                (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
+       buf = kzalloc(((uc_count + mc_count) * ETH_ALEN) +
+                     (2 * sizeof(mac_data->entries)), GFP_ATOMIC);
+       mac_data = buf;
        if (!buf) {
                dev_warn(&dev->dev, "No memory for MAC address buffer\n");
                return;
@@ -784,13 +772,13 @@ static void virtnet_set_rx_mode(struct net_device *dev)
        /* multicast list and count fill the end */
        mac_data = (void *)&mac_data->macs[uc_count][0];
 
-       mac_data->entries = dev->mc_count;
-       addr = dev->mc_list;
-       for (i = 0; i < dev->mc_count; i++, addr = addr->next)
-               memcpy(&mac_data->macs[i][0], addr->da_addr, ETH_ALEN);
+       mac_data->entries = mc_count;
+       i = 0;
+       netdev_for_each_mc_addr(ha, dev)
+               memcpy(&mac_data->macs[i++][0], ha->addr, ETH_ALEN);
 
        sg_set_buf(&sg[1], mac_data,
-                  sizeof(mac_data->entries) + (dev->mc_count * ETH_ALEN));
+                  sizeof(mac_data->entries) + (mc_count * ETH_ALEN));
 
        if (!virtnet_send_command(vi, VIRTIO_NET_CTRL_MAC,
                                  VIRTIO_NET_CTRL_MAC_TABLE_SET,
@@ -949,6 +937,8 @@ static int virtnet_probe(struct virtio_device *vdev)
        vdev->priv = vi;
        vi->pages = NULL;
        INIT_DELAYED_WORK(&vi->refill, refill_work);
+       sg_init_table(vi->rx_sg, ARRAY_SIZE(vi->rx_sg));
+       sg_init_table(vi->tx_sg, ARRAY_SIZE(vi->tx_sg));
 
        /* If we can receive ANY GSO packets, we must allocate large ones. */
        if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) ||
@@ -977,9 +967,6 @@ static int virtnet_probe(struct virtio_device *vdev)
                        dev->features |= NETIF_F_HW_VLAN_FILTER;
        }
 
-       /* Initialize our empty send queue. */
-       skb_queue_head_init(&vi->send);
-
        err = register_netdev(dev);
        if (err) {
                pr_debug("virtio_net: registering device failed\n");
@@ -1016,6 +1003,12 @@ static void free_unused_bufs(struct virtnet_info *vi)
 {
        void *buf;
        while (1) {
+               buf = vi->svq->vq_ops->detach_unused_buf(vi->svq);
+               if (!buf)
+                       break;
+               dev_kfree_skb(buf);
+       }
+       while (1) {
                buf = vi->rvq->vq_ops->detach_unused_buf(vi->rvq);
                if (!buf)
                        break;
@@ -1035,11 +1028,11 @@ static void __devexit virtnet_remove(struct virtio_device *vdev)
        /* Stop all the virtqueues. */
        vdev->config->reset(vdev);
 
-       /* Free our skbs in send queue, if any. */
-       __skb_queue_purge(&vi->send);
 
        unregister_netdev(vi->dev);
        cancel_delayed_work_sync(&vi->refill);
+
+       /* Free unused buffers in both send and recv, if any. */
        free_unused_bufs(vi);
 
        vdev->config->del_vqs(vi->vdev);