1 #include <linux/etherdevice.h>
2 #include <linux/if_macvlan.h>
3 #include <linux/interrupt.h>
4 #include <linux/nsproxy.h>
5 #include <linux/compat.h>
6 #include <linux/if_tun.h>
7 #include <linux/module.h>
8 #include <linux/skbuff.h>
9 #include <linux/cache.h>
10 #include <linux/sched.h>
11 #include <linux/types.h>
12 #include <linux/init.h>
13 #include <linux/wait.h>
14 #include <linux/cdev.h>
17 #include <net/net_namespace.h>
18 #include <net/rtnetlink.h>
22 * A macvtap queue is the central object of this driver, it connects
23 * an open character device to a macvlan interface. There can be
24 * multiple queues on one interface, which map back to queues
25 * implemented in hardware on the underlying device.
27 * macvtap_proto is used to allocate queues through the sock allocation
30 * TODO: multiqueue support is currently not implemented, even though
31 * macvtap is basically prepared for that. We will need to add this
32 * here as well as in virtio-net and qemu to get line rate on 10gbit
33 * adapters from a guest.
35 struct macvtap_queue {
38 struct macvlan_dev *vlan;
42 static struct proto macvtap_proto = {
45 .obj_size = sizeof (struct macvtap_queue),
49 * Minor number matches netdev->ifindex, so need a potentially
50 * large value. This also makes it possible to split the
51 * tap functionality out again in the future by offering it
52 * from other drivers besides macvtap. As long as every device
53 * only has one tap, the interface numbers assure that the
54 * device nodes are unique.
56 static unsigned int macvtap_major;
57 #define MACVTAP_NUM_DEVS 65536
58 static struct class *macvtap_class;
59 static struct cdev macvtap_cdev;
61 static const struct proto_ops macvtap_socket_ops;
65 * The macvtap_queue and the macvlan_dev are loosely coupled, the
66 * pointers from one to the other can only be read while rcu_read_lock
67 * or macvtap_lock is held.
69 * Both the file and the macvlan_dev hold a reference on the macvtap_queue
70 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
71 * q->vlan becomes inaccessible. When the files gets closed,
72 * macvtap_get_queue() fails.
74 * There may still be references to the struct sock inside of the
75 * queue from outbound SKBs, but these never reference back to the
76 * file or the dev. The data structure is freed through __sk_free
77 * when both our references and any pending SKBs are gone.
79 static DEFINE_SPINLOCK(macvtap_lock);
82 * Choose the next free queue, for now there is only one
84 static int macvtap_set_queue(struct net_device *dev, struct file *file,
85 struct macvtap_queue *q)
87 struct macvlan_dev *vlan = netdev_priv(dev);
90 spin_lock(&macvtap_lock);
91 if (rcu_dereference(vlan->tap))
95 rcu_assign_pointer(q->vlan, vlan);
96 rcu_assign_pointer(vlan->tap, q);
100 file->private_data = q;
103 spin_unlock(&macvtap_lock);
108 * The file owning the queue got closed, give up both
109 * the reference that the files holds as well as the
110 * one from the macvlan_dev if that still exists.
112 * Using the spinlock makes sure that we don't get
113 * to the queue again after destroying it.
115 static void macvtap_put_queue(struct macvtap_queue *q)
117 struct macvlan_dev *vlan;
119 spin_lock(&macvtap_lock);
120 vlan = rcu_dereference(q->vlan);
122 rcu_assign_pointer(vlan->tap, NULL);
123 rcu_assign_pointer(q->vlan, NULL);
127 spin_unlock(&macvtap_lock);
134 * Since we only support one queue, just dereference the pointer.
136 static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
139 struct macvlan_dev *vlan = netdev_priv(dev);
141 return rcu_dereference(vlan->tap);
145 * The net_device is going away, give up the reference
146 * that it holds on the queue (all the queues one day)
147 * and safely set the pointer from the queues to NULL.
149 static void macvtap_del_queues(struct net_device *dev)
151 struct macvlan_dev *vlan = netdev_priv(dev);
152 struct macvtap_queue *q;
154 spin_lock(&macvtap_lock);
155 q = rcu_dereference(vlan->tap);
157 spin_unlock(&macvtap_lock);
161 rcu_assign_pointer(vlan->tap, NULL);
162 rcu_assign_pointer(q->vlan, NULL);
163 spin_unlock(&macvtap_lock);
170 * Forward happens for data that gets sent from one macvlan
171 * endpoint to another one in bridge mode. We just take
172 * the skb and put it into the receive queue.
174 static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
176 struct macvtap_queue *q = macvtap_get_queue(dev, skb);
180 skb_queue_tail(&q->sk.sk_receive_queue, skb);
181 wake_up_interruptible_poll(q->sk.sk_sleep, POLLIN | POLLRDNORM | POLLRDBAND);
186 * Receive is for data from the external interface (lowerdev),
187 * in case of macvtap, we can treat that the same way as
188 * forward, which macvlan cannot.
190 static int macvtap_receive(struct sk_buff *skb)
192 skb_push(skb, ETH_HLEN);
193 return macvtap_forward(skb->dev, skb);
196 static int macvtap_newlink(struct net *src_net,
197 struct net_device *dev,
199 struct nlattr *data[])
201 struct device *classdev;
205 err = macvlan_common_newlink(src_net, dev, tb, data,
206 macvtap_receive, macvtap_forward);
210 devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
212 classdev = device_create(macvtap_class, &dev->dev, devt,
213 dev, "tap%d", dev->ifindex);
214 if (IS_ERR(classdev)) {
215 err = PTR_ERR(classdev);
216 macvtap_del_queues(dev);
223 static void macvtap_dellink(struct net_device *dev,
224 struct list_head *head)
226 device_destroy(macvtap_class,
227 MKDEV(MAJOR(macvtap_major), dev->ifindex));
229 macvtap_del_queues(dev);
230 macvlan_dellink(dev, head);
233 static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
235 .newlink = macvtap_newlink,
236 .dellink = macvtap_dellink,
240 static void macvtap_sock_write_space(struct sock *sk)
242 if (!sock_writeable(sk) ||
243 !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
246 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
247 wake_up_interruptible_poll(sk->sk_sleep, POLLOUT | POLLWRNORM | POLLWRBAND);
250 static int macvtap_open(struct inode *inode, struct file *file)
252 struct net *net = current->nsproxy->net_ns;
253 struct net_device *dev = dev_get_by_index(net, iminor(inode));
254 struct macvtap_queue *q;
261 /* check if this is a macvtap device */
263 if (dev->rtnl_link_ops != &macvtap_link_ops)
267 q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
272 init_waitqueue_head(&q->sock.wait);
273 q->sock.type = SOCK_RAW;
274 q->sock.state = SS_CONNECTED;
276 q->sock.ops = &macvtap_socket_ops;
277 sock_init_data(&q->sock, &q->sk);
278 q->sk.sk_write_space = macvtap_sock_write_space;
280 err = macvtap_set_queue(dev, file, q);
291 static int macvtap_release(struct inode *inode, struct file *file)
293 struct macvtap_queue *q = file->private_data;
294 macvtap_put_queue(q);
298 static unsigned int macvtap_poll(struct file *file, poll_table * wait)
300 struct macvtap_queue *q = file->private_data;
301 unsigned int mask = POLLERR;
307 poll_wait(file, &q->sock.wait, wait);
309 if (!skb_queue_empty(&q->sk.sk_receive_queue))
310 mask |= POLLIN | POLLRDNORM;
312 if (sock_writeable(&q->sk) ||
313 (!test_and_set_bit(SOCK_ASYNC_NOSPACE, &q->sock.flags) &&
314 sock_writeable(&q->sk)))
315 mask |= POLLOUT | POLLWRNORM;
321 /* Get packet from user space buffer */
322 static ssize_t macvtap_get_user(struct macvtap_queue *q,
323 const struct iovec *iv, size_t count,
327 struct macvlan_dev *vlan;
331 if (unlikely(len < ETH_HLEN))
334 skb = sock_alloc_send_skb(&q->sk, NET_IP_ALIGN + len, noblock, &err);
338 skb_reserve(skb, NET_IP_ALIGN);
341 err = skb_copy_datagram_from_iovec(skb, 0, iv, 0, len);
345 skb_set_network_header(skb, ETH_HLEN);
347 vlan = rcu_dereference(q->vlan);
349 macvlan_start_xmit(skb, vlan->dev);
352 rcu_read_unlock_bh();
358 vlan = rcu_dereference(q->vlan);
360 macvlan_count_rx(q->vlan, 0, false, false);
361 rcu_read_unlock_bh();
368 static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
369 unsigned long count, loff_t pos)
371 struct file *file = iocb->ki_filp;
372 ssize_t result = -ENOLINK;
373 struct macvtap_queue *q = file->private_data;
375 result = macvtap_get_user(q, iv, iov_length(iv, count),
376 file->f_flags & O_NONBLOCK);
380 /* Put packet to the user space buffer */
381 static ssize_t macvtap_put_user(struct macvtap_queue *q,
382 const struct sk_buff *skb,
383 const struct iovec *iv, int len)
385 struct macvlan_dev *vlan;
388 len = min_t(int, skb->len, len);
390 ret = skb_copy_datagram_const_iovec(skb, 0, iv, 0, len);
393 vlan = rcu_dereference(q->vlan);
395 macvlan_count_rx(vlan, len, ret == 0, 0);
396 rcu_read_unlock_bh();
398 return ret ? ret : len;
401 static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
402 const struct iovec *iv, unsigned long len,
405 DECLARE_WAITQUEUE(wait, current);
409 add_wait_queue(q->sk.sk_sleep, &wait);
411 current->state = TASK_INTERRUPTIBLE;
413 /* Read frames from the queue */
414 skb = skb_dequeue(&q->sk.sk_receive_queue);
420 if (signal_pending(current)) {
424 /* Nothing to read, let's sleep */
428 ret = macvtap_put_user(q, skb, iv, len);
433 current->state = TASK_RUNNING;
434 remove_wait_queue(q->sk.sk_sleep, &wait);
438 static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
439 unsigned long count, loff_t pos)
441 struct file *file = iocb->ki_filp;
442 struct macvtap_queue *q = file->private_data;
443 ssize_t len, ret = 0;
445 len = iov_length(iv, count);
451 ret = macvtap_do_read(q, iocb, iv, len, file->f_flags & O_NONBLOCK);
452 ret = min_t(ssize_t, ret, len); /* XXX copied from tun.c. Why? */
458 * provide compatibility with generic tun/tap interface
460 static long macvtap_ioctl(struct file *file, unsigned int cmd,
463 struct macvtap_queue *q = file->private_data;
464 struct macvlan_dev *vlan;
465 void __user *argp = (void __user *)arg;
466 struct ifreq __user *ifr = argp;
467 unsigned int __user *up = argp;
473 /* ignore the name, just look at flags */
474 if (get_user(u, &ifr->ifr_flags))
476 if (u != (IFF_TAP | IFF_NO_PI))
482 vlan = rcu_dereference(q->vlan);
485 rcu_read_unlock_bh();
491 if (copy_to_user(&ifr->ifr_name, q->vlan->dev->name, IFNAMSIZ) ||
492 put_user((TUN_TAP_DEV | TUN_NO_PI), &ifr->ifr_flags))
498 if (put_user((IFF_TAP | IFF_NO_PI), up))
510 /* let the user check for future flags */
511 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
512 TUN_F_TSO_ECN | TUN_F_UFO))
515 /* TODO: add support for these, so far we don't
516 support any offload */
517 if (arg & (TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
518 TUN_F_TSO_ECN | TUN_F_UFO))
529 static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
532 return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
536 static const struct file_operations macvtap_fops = {
537 .owner = THIS_MODULE,
538 .open = macvtap_open,
539 .release = macvtap_release,
540 .aio_read = macvtap_aio_read,
541 .aio_write = macvtap_aio_write,
542 .poll = macvtap_poll,
544 .unlocked_ioctl = macvtap_ioctl,
546 .compat_ioctl = macvtap_compat_ioctl,
550 static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
551 struct msghdr *m, size_t total_len)
553 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
554 return macvtap_get_user(q, m->msg_iov, total_len,
555 m->msg_flags & MSG_DONTWAIT);
558 static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
559 struct msghdr *m, size_t total_len,
562 struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
564 if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
566 ret = macvtap_do_read(q, iocb, m->msg_iov, total_len,
567 flags & MSG_DONTWAIT);
568 if (ret > total_len) {
569 m->msg_flags |= MSG_TRUNC;
570 ret = flags & MSG_TRUNC ? ret : total_len;
575 /* Ops structure to mimic raw sockets with tun */
576 static const struct proto_ops macvtap_socket_ops = {
577 .sendmsg = macvtap_sendmsg,
578 .recvmsg = macvtap_recvmsg,
581 /* Get an underlying socket object from tun file. Returns error unless file is
582 * attached to a device. The returned object works like a packet socket, it
583 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
584 * holding a reference to the file for as long as the socket is in use. */
585 struct socket *macvtap_get_socket(struct file *file)
587 struct macvtap_queue *q;
588 if (file->f_op != &macvtap_fops)
589 return ERR_PTR(-EINVAL);
590 q = file->private_data;
592 return ERR_PTR(-EBADFD);
595 EXPORT_SYMBOL_GPL(macvtap_get_socket);
597 static int macvtap_init(void)
601 err = alloc_chrdev_region(&macvtap_major, 0,
602 MACVTAP_NUM_DEVS, "macvtap");
606 cdev_init(&macvtap_cdev, &macvtap_fops);
607 err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
611 macvtap_class = class_create(THIS_MODULE, "macvtap");
612 if (IS_ERR(macvtap_class)) {
613 err = PTR_ERR(macvtap_class);
617 err = macvlan_link_register(&macvtap_link_ops);
624 class_unregister(macvtap_class);
626 cdev_del(&macvtap_cdev);
628 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
632 module_init(macvtap_init);
634 static void macvtap_exit(void)
636 rtnl_link_unregister(&macvtap_link_ops);
637 class_unregister(macvtap_class);
638 cdev_del(&macvtap_cdev);
639 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
641 module_exit(macvtap_exit);
643 MODULE_ALIAS_RTNL_LINK("macvtap");
644 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
645 MODULE_LICENSE("GPL");