macvtap: read vnet_hdr_size once
[pandora-kernel.git] / drivers / net / macvtap.c
index ab96c31..1c3db94 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/cdev.h>
 #include <linux/fs.h>
 
+#include <net/ipv6.h>
 #include <net/net_namespace.h>
 #include <net/rtnetlink.h>
 #include <net/sock.h>
@@ -51,15 +52,13 @@ static struct proto macvtap_proto = {
 };
 
 /*
- * Minor number matches netdev->ifindex, so need a potentially
- * large value. This also makes it possible to split the
- * tap functionality out again in the future by offering it
- * from other drivers besides macvtap. As long as every device
- * only has one tap, the interface numbers assure that the
- * device nodes are unique.
+ * Variables for dealing with macvtaps device numbers.
  */
 static dev_t macvtap_major;
-#define MACVTAP_NUM_DEVS 65536
+#define MACVTAP_NUM_DEVS (1U << MINORBITS)
+static DEFINE_MUTEX(minor_lock);
+static DEFINE_IDR(minor_idr);
+
 #define GOODCOPY_LEN 128
 static struct class *macvtap_class;
 static struct cdev macvtap_cdev;
@@ -231,6 +230,8 @@ static void macvtap_del_queues(struct net_device *dev)
                }
        }
        BUG_ON(vlan->numvtaps != 0);
+       /* guarantee that any future macvtap_set_queue will fail */
+       vlan->numvtaps = MAX_MACVTAP_QUEUES;
        spin_unlock(&macvtap_lock);
 
        synchronize_rcu();
@@ -273,39 +274,73 @@ static int macvtap_receive(struct sk_buff *skb)
        return macvtap_forward(skb->dev, skb);
 }
 
-static int macvtap_newlink(struct net *src_net,
-                          struct net_device *dev,
-                          struct nlattr *tb[],
-                          struct nlattr *data[])
+static int macvtap_get_minor(struct macvlan_dev *vlan)
 {
-       struct device *classdev;
-       dev_t devt;
-       int err;
+       int retval = -ENOMEM;
+       int id;
+
+       mutex_lock(&minor_lock);
+       if (idr_pre_get(&minor_idr, GFP_KERNEL) == 0)
+               goto exit;
+
+       retval = idr_get_new_above(&minor_idr, vlan, 1, &id);
+       if (retval < 0) {
+               if (retval == -EAGAIN)
+                       retval = -ENOMEM;
+               goto exit;
+       }
+       if (id < MACVTAP_NUM_DEVS) {
+               vlan->minor = id;
+       } else {
+               printk(KERN_ERR "too many macvtap devices\n");
+               retval = -EINVAL;
+               idr_remove(&minor_idr, id);
+       }
+exit:
+       mutex_unlock(&minor_lock);
+       return retval;
+}
 
-       err = macvlan_common_newlink(src_net, dev, tb, data,
-                                    macvtap_receive, macvtap_forward);
-       if (err)
-               goto out;
+static void macvtap_free_minor(struct macvlan_dev *vlan)
+{
+       mutex_lock(&minor_lock);
+       if (vlan->minor) {
+               idr_remove(&minor_idr, vlan->minor);
+               vlan->minor = 0;
+       }
+       mutex_unlock(&minor_lock);
+}
 
-       devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
+static struct net_device *dev_get_by_macvtap_minor(int minor)
+{
+       struct net_device *dev = NULL;
+       struct macvlan_dev *vlan;
 
-       classdev = device_create(macvtap_class, &dev->dev, devt,
-                                dev, "tap%d", dev->ifindex);
-       if (IS_ERR(classdev)) {
-               err = PTR_ERR(classdev);
-               macvtap_del_queues(dev);
+       mutex_lock(&minor_lock);
+       vlan = idr_find(&minor_idr, minor);
+       if (vlan) {
+               dev = vlan->dev;
+               dev_hold(dev);
        }
+       mutex_unlock(&minor_lock);
+       return dev;
+}
 
-out:
-       return err;
+static int macvtap_newlink(struct net *src_net,
+                          struct net_device *dev,
+                          struct nlattr *tb[],
+                          struct nlattr *data[])
+{
+       /* Don't put anything that may fail after macvlan_common_newlink
+        * because we can't undo what it does.
+        */
+       return macvlan_common_newlink(src_net, dev, tb, data,
+                                     macvtap_receive, macvtap_forward);
 }
 
 static void macvtap_dellink(struct net_device *dev,
                            struct list_head *head)
 {
-       device_destroy(macvtap_class,
-                      MKDEV(MAJOR(macvtap_major), dev->ifindex));
-
        macvtap_del_queues(dev);
        macvlan_dellink(dev, head);
 }
@@ -337,11 +372,15 @@ static void macvtap_sock_write_space(struct sock *sk)
                wake_up_interruptible_poll(wqueue, POLLOUT | POLLWRNORM | POLLWRBAND);
 }
 
+static void macvtap_sock_destruct(struct sock *sk)
+{
+       skb_queue_purge(&sk->sk_receive_queue);
+}
+
 static int macvtap_open(struct inode *inode, struct file *file)
 {
        struct net *net = current->nsproxy->net_ns;
-       struct net_device *dev = dev_get_by_index(net, iminor(inode));
-       struct macvlan_dev *vlan = netdev_priv(dev);
+       struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
        struct macvtap_queue *q;
        int err;
 
@@ -349,11 +388,6 @@ static int macvtap_open(struct inode *inode, struct file *file)
        if (!dev)
                goto out;
 
-       /* check if this is a macvtap device */
-       err = -EINVAL;
-       if (dev->rtnl_link_ops != &macvtap_link_ops)
-               goto out;
-
        err = -ENOMEM;
        q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
                                             &macvtap_proto);
@@ -368,18 +402,19 @@ static int macvtap_open(struct inode *inode, struct file *file)
        q->sock.ops = &macvtap_socket_ops;
        sock_init_data(&q->sock, &q->sk);
        q->sk.sk_write_space = macvtap_sock_write_space;
+       q->sk.sk_destruct = macvtap_sock_destruct;
        q->flags = IFF_VNET_HDR | IFF_NO_PI | IFF_TAP;
        q->vnet_hdr_sz = sizeof(struct virtio_net_hdr);
 
        /*
         * so far only KVM virtio_net uses macvtap, enable zero copy between
         * guest kernel and host kernel when lower device supports zerocopy
+        *
+        * The macvlan supports zerocopy iff the lower device supports zero
+        * copy so we don't have to look at the lower device directly.
         */
-       if (vlan) {
-               if ((vlan->lowerdev->features & NETIF_F_HIGHDMA) &&
-                   (vlan->lowerdev->features & NETIF_F_SG))
-                       sock_set_flag(&q->sk, SOCK_ZEROCOPY);
-       }
+       if ((dev->features & NETIF_F_HIGHDMA) && (dev->features & NETIF_F_SG))
+               sock_set_flag(&q->sk, SOCK_ZEROCOPY);
 
        err = macvtap_set_queue(dev, file, q);
        if (err)
@@ -453,7 +488,6 @@ static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
        int copy = skb_headlen(skb);
        int size, offset1 = 0;
        int i = 0;
-       skb_frag_t *f;
 
        /* Skip over from offset */
        while (count && (offset >= from->iov_len)) {
@@ -471,10 +505,11 @@ static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
                if (copy > size) {
                        ++from;
                        --count;
-               }
+                       offset = 0;
+               } else
+                       offset += size;
                copy -= size;
                offset1 += size;
-               offset = 0;
        }
 
        if (len == offset1)
@@ -484,36 +519,42 @@ static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
                struct page *page[MAX_SKB_FRAGS];
                int num_pages;
                unsigned long base;
+               unsigned long truesize;
 
-               len = from->iov_len - offset1;
+               len = from->iov_len - offset;
                if (!len) {
-                       offset1 = 0;
+                       offset = 0;
                        ++from;
                        continue;
                }
-               base = (unsigned long)from->iov_base + offset1;
+               base = (unsigned long)from->iov_base + offset;
                size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
+               if (i + size > MAX_SKB_FRAGS)
+                       return -EMSGSIZE;
                num_pages = get_user_pages_fast(base, size, 0, &page[i]);
-               if ((num_pages != size) ||
-                   (num_pages > MAX_SKB_FRAGS - skb_shinfo(skb)->nr_frags))
-                       /* put_page is in skb free */
+               if (num_pages != size) {
+                       int j;
+
+                       for (j = 0; j < num_pages; j++)
+                               put_page(page[i + j]);
                        return -EFAULT;
+               }
+               truesize = size * PAGE_SIZE;
                skb->data_len += len;
                skb->len += len;
-               skb->truesize += len;
-               atomic_add(len, &skb->sk->sk_wmem_alloc);
+               skb->truesize += truesize;
+               atomic_add(truesize, &skb->sk->sk_wmem_alloc);
                while (len) {
-                       f = &skb_shinfo(skb)->frags[i];
-                       f->page = page[i];
-                       f->page_offset = base & ~PAGE_MASK;
-                       f->size = min_t(int, len, PAGE_SIZE - f->page_offset);
+                       int off = base & ~PAGE_MASK;
+                       int size = min_t(int, len, PAGE_SIZE - off);
+                       __skb_fill_page_desc(skb, i, page[i], off, size);
                        skb_shinfo(skb)->nr_frags++;
                        /* increase sk_wmem_alloc */
-                       base += f->size;
-                       len -= f->size;
+                       base += size;
+                       len -= size;
                        i++;
                }
-               offset1 = 0;
+               offset = 0;
                ++from;
        }
        return 0;
@@ -537,6 +578,8 @@ static int macvtap_skb_from_vnet_hdr(struct sk_buff *skb,
                        break;
                case VIRTIO_NET_HDR_GSO_UDP:
                        gso_type = SKB_GSO_UDP;
+                       if (skb->protocol == htons(ETH_P_IPV6))
+                               ipv6_proxy_select_ident(skb);
                        break;
                default:
                        return -EINVAL;
@@ -601,23 +644,50 @@ static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
        return 0;
 }
 
+static unsigned long iov_pages(const struct iovec *iv, int offset,
+                              unsigned long nr_segs)
+{
+       unsigned long seg, base;
+       int pages = 0, len, size;
+
+       while (nr_segs && (offset >= iv->iov_len)) {
+               offset -= iv->iov_len;
+               ++iv;
+               --nr_segs;
+       }
+
+       for (seg = 0; seg < nr_segs; seg++) {
+               base = (unsigned long)iv[seg].iov_base + offset;
+               len = iv[seg].iov_len - offset;
+               size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
+               pages += size;
+               offset = 0;
+       }
+
+       return pages;
+}
+
+/* Neighbour code has some assumptions on HH_DATA_MOD alignment */
+#define MACVTAP_RESERVE HH_DATA_OFF(ETH_HLEN)
 
 /* Get packet from user space buffer */
 static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
                                const struct iovec *iv, unsigned long total_len,
                                size_t count, int noblock)
 {
+       int good_linear = SKB_MAX_HEAD(MACVTAP_RESERVE);
        struct sk_buff *skb;
        struct macvlan_dev *vlan;
        unsigned long len = total_len;
        int err;
        struct virtio_net_hdr vnet_hdr = { 0 };
        int vnet_hdr_len = 0;
-       int copylen;
+       int copylen = 0;
        bool zerocopy = false;
+       size_t linear;
 
        if (q->flags & IFF_VNET_HDR) {
-               vnet_hdr_len = q->vnet_hdr_sz;
+               vnet_hdr_len = ACCESS_ONCE(q->vnet_hdr_sz);
 
                err = -EINVAL;
                if (len < vnet_hdr_len)
@@ -642,31 +712,47 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
        if (unlikely(len < ETH_HLEN))
                goto err;
 
-       if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
-               zerocopy = true;
+       err = -EMSGSIZE;
+       if (unlikely(count > UIO_MAXIOV))
+               goto err;
 
-       if (zerocopy) {
-               /* There are 256 bytes to be copied in skb, so there is enough
-                * room for skb expand head in case it is used.
-                * The rest buffer is mapped from userspace.
-                */
-               copylen = vnet_hdr.hdr_len;
-               if (!copylen)
-                       copylen = GOODCOPY_LEN;
-       } else
+       if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
+               copylen = vnet_hdr.hdr_len ? vnet_hdr.hdr_len : GOODCOPY_LEN;
+               if (copylen > good_linear)
+                       copylen = good_linear;
+               else if (copylen < ETH_HLEN)
+                       copylen = ETH_HLEN;
+               linear = copylen;
+               if (iov_pages(iv, vnet_hdr_len + copylen, count)
+                   <= MAX_SKB_FRAGS)
+                       zerocopy = true;
+       }
+
+       if (!zerocopy) {
                copylen = len;
+               linear = vnet_hdr.hdr_len;
+               if (linear > good_linear)
+                       linear = good_linear;
+               else if (linear < ETH_HLEN)
+                       linear = ETH_HLEN;
+       }
 
-       skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
-                               vnet_hdr.hdr_len, noblock, &err);
+       skb = macvtap_alloc_skb(&q->sk, MACVTAP_RESERVE, copylen,
+                               linear, noblock, &err);
        if (!skb)
                goto err;
 
-       if (zerocopy) {
+       if (zerocopy)
                err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
-               skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
-       } else
+       else {
                err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
                                                   len);
+               if (!err && m && m->msg_control) {
+                       struct ubuf_info *uarg = m->msg_control;
+                       uarg->callback(uarg);
+               }
+       }
+
        if (err)
                goto err_kfree;
 
@@ -683,8 +769,10 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q, struct msghdr *m,
        rcu_read_lock_bh();
        vlan = rcu_dereference_bh(q->vlan);
        /* copy skb_ubuf_info for callback when skb has no error */
-       if (zerocopy)
+       if (zerocopy) {
                skb_shinfo(skb)->destructor_arg = m->msg_control;
+               skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
+       }
        if (vlan)
                macvlan_start_xmit(skb, vlan->dev);
        else
@@ -729,7 +817,7 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q,
 
        if (q->flags & IFF_VNET_HDR) {
                struct virtio_net_hdr vnet_hdr;
-               vnet_hdr_len = q->vnet_hdr_sz;
+               vnet_hdr_len = ACCESS_ONCE(q->vnet_hdr_sz);
                if ((len -= vnet_hdr_len) < 0)
                        return -EINVAL;
 
@@ -970,6 +1058,52 @@ struct socket *macvtap_get_socket(struct file *file)
 }
 EXPORT_SYMBOL_GPL(macvtap_get_socket);
 
+static int macvtap_device_event(struct notifier_block *unused,
+                               unsigned long event, void *ptr)
+{
+       struct net_device *dev = ptr;
+       struct macvlan_dev *vlan;
+       struct device *classdev;
+       dev_t devt;
+       int err;
+
+       if (dev->rtnl_link_ops != &macvtap_link_ops)
+               return NOTIFY_DONE;
+
+       vlan = netdev_priv(dev);
+
+       switch (event) {
+       case NETDEV_REGISTER:
+               /* Create the device node here after the network device has
+                * been registered but before register_netdevice has
+                * finished running.
+                */
+               err = macvtap_get_minor(vlan);
+               if (err)
+                       return notifier_from_errno(err);
+
+               devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
+               classdev = device_create(macvtap_class, &dev->dev, devt,
+                                        dev, "tap%d", dev->ifindex);
+               if (IS_ERR(classdev)) {
+                       macvtap_free_minor(vlan);
+                       return notifier_from_errno(PTR_ERR(classdev));
+               }
+               break;
+       case NETDEV_UNREGISTER:
+               devt = MKDEV(MAJOR(macvtap_major), vlan->minor);
+               device_destroy(macvtap_class, devt);
+               macvtap_free_minor(vlan);
+               break;
+       }
+
+       return NOTIFY_DONE;
+}
+
+static struct notifier_block macvtap_notifier_block __read_mostly = {
+       .notifier_call  = macvtap_device_event,
+};
+
 static int macvtap_init(void)
 {
        int err;
@@ -990,12 +1124,18 @@ static int macvtap_init(void)
                goto out3;
        }
 
-       err = macvlan_link_register(&macvtap_link_ops);
+       err = register_netdevice_notifier(&macvtap_notifier_block);
        if (err)
                goto out4;
 
+       err = macvlan_link_register(&macvtap_link_ops);
+       if (err)
+               goto out5;
+
        return 0;
 
+out5:
+       unregister_netdevice_notifier(&macvtap_notifier_block);
 out4:
        class_unregister(macvtap_class);
 out3:
@@ -1010,6 +1150,7 @@ module_init(macvtap_init);
 static void macvtap_exit(void)
 {
        rtnl_link_unregister(&macvtap_link_ops);
+       unregister_netdevice_notifier(&macvtap_notifier_block);
        class_unregister(macvtap_class);
        cdev_del(&macvtap_cdev);
        unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);