]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - drivers/net/macvtap.c
tg3: Fix APE mutex init and use
[linux-2.6.git] / drivers / net / macvtap.c
index ecee0fe65a9776584762f96ed364df8325e904ee..1b7082d08f334b8d1d0ab7f350bb2de5a3d41c5c 100644 (file)
@@ -51,15 +51,14 @@ 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;
 
@@ -230,6 +229,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();
@@ -272,39 +273,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);
 }
@@ -336,10 +371,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 net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
        struct macvtap_queue *q;
        int err;
 
@@ -347,11 +387,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);
@@ -366,9 +401,20 @@ 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 ((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)
                sock_put(&q->sk);
@@ -433,6 +479,78 @@ static inline struct sk_buff *macvtap_alloc_skb(struct sock *sk, size_t prepad,
        return skb;
 }
 
+/* set skb frags from iovec, this can move to core network code for reuse */
+static int zerocopy_sg_from_iovec(struct sk_buff *skb, const struct iovec *from,
+                                 int offset, size_t count)
+{
+       int len = iov_length(from, count) - offset;
+       int copy = skb_headlen(skb);
+       int size, offset1 = 0;
+       int i = 0;
+
+       /* Skip over from offset */
+       while (count && (offset >= from->iov_len)) {
+               offset -= from->iov_len;
+               ++from;
+               --count;
+       }
+
+       /* copy up to skb headlen */
+       while (count && (copy > 0)) {
+               size = min_t(unsigned int, copy, from->iov_len - offset);
+               if (copy_from_user(skb->data + offset1, from->iov_base + offset,
+                                  size))
+                       return -EFAULT;
+               if (copy > size) {
+                       ++from;
+                       --count;
+               }
+               copy -= size;
+               offset1 += size;
+               offset = 0;
+       }
+
+       if (len == offset1)
+               return 0;
+
+       while (count--) {
+               struct page *page[MAX_SKB_FRAGS];
+               int num_pages;
+               unsigned long base;
+
+               len = from->iov_len - offset1;
+               if (!len) {
+                       offset1 = 0;
+                       ++from;
+                       continue;
+               }
+               base = (unsigned long)from->iov_base + offset1;
+               size = ((base & ~PAGE_MASK) + len + ~PAGE_MASK) >> PAGE_SHIFT;
+               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 */
+                       return -EFAULT;
+               skb->data_len += len;
+               skb->len += len;
+               skb->truesize += len;
+               atomic_add(len, &skb->sk->sk_wmem_alloc);
+               while (len) {
+                       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 += size;
+                       len -= size;
+                       i++;
+               }
+               offset1 = 0;
+               ++from;
+       }
+       return 0;
+}
+
 /*
  * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
  * be shared with the tun/tap driver.
@@ -517,16 +635,18 @@ static int macvtap_skb_to_vnet_hdr(const struct sk_buff *skb,
 
 
 /* Get packet from user space buffer */
-static ssize_t macvtap_get_user(struct macvtap_queue *q,
-                               const struct iovec *iv, size_t count,
-                               int noblock)
+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)
 {
        struct sk_buff *skb;
        struct macvlan_dev *vlan;
-       size_t len = count;
+       unsigned long len = total_len;
        int err;
        struct virtio_net_hdr vnet_hdr = { 0 };
        int vnet_hdr_len = 0;
+       int copylen;
+       bool zerocopy = false;
 
        if (q->flags & IFF_VNET_HDR) {
                vnet_hdr_len = q->vnet_hdr_sz;
@@ -554,12 +674,31 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
        if (unlikely(len < ETH_HLEN))
                goto err;
 
-       skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, len, vnet_hdr.hdr_len,
-                               noblock, &err);
+       if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY))
+               zerocopy = true;
+
+       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
+               copylen = len;
+
+       skb = macvtap_alloc_skb(&q->sk, NET_IP_ALIGN, copylen,
+                               vnet_hdr.hdr_len, noblock, &err);
        if (!skb)
                goto err;
 
-       err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len, len);
+       if (zerocopy) {
+               err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
+               skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
+       } else
+               err = skb_copy_datagram_from_iovec(skb, 0, iv, vnet_hdr_len,
+                                                  len);
        if (err)
                goto err_kfree;
 
@@ -575,13 +714,16 @@ static ssize_t macvtap_get_user(struct macvtap_queue *q,
 
        rcu_read_lock_bh();
        vlan = rcu_dereference_bh(q->vlan);
+       /* copy skb_ubuf_info for callback when skb has no error */
+       if (zerocopy)
+               skb_shinfo(skb)->destructor_arg = m->msg_control;
        if (vlan)
                macvlan_start_xmit(skb, vlan->dev);
        else
                kfree_skb(skb);
        rcu_read_unlock_bh();
 
-       return count;
+       return total_len;
 
 err_kfree:
        kfree_skb(skb);
@@ -603,8 +745,8 @@ static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
        ssize_t result = -ENOLINK;
        struct macvtap_queue *q = file->private_data;
 
-       result = macvtap_get_user(q, iv, iov_length(iv, count),
-                             file->f_flags & O_NONBLOCK);
+       result = macvtap_get_user(q, NULL, iv, iov_length(iv, count), count,
+                                 file->f_flags & O_NONBLOCK);
        return result;
 }
 
@@ -817,7 +959,7 @@ static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
                           struct msghdr *m, size_t total_len)
 {
        struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
-       return macvtap_get_user(q, m->msg_iov, total_len,
+       return macvtap_get_user(q, m, m->msg_iov, total_len, m->msg_iovlen,
                            m->msg_flags & MSG_DONTWAIT);
 }
 
@@ -860,6 +1002,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;
@@ -880,12 +1068,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:
@@ -900,6 +1094,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);