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;
63 * The macvtap_queue is referenced both from the chardev struct file
64 * and from the struct macvlan_dev using rcu_read_lock.
66 * We never actually update the contents of a macvtap_queue atomically
67 * with RCU but it is used for race-free destruction of a queue when
68 * either the file or the macvlan_dev goes away. Pointers back to
69 * the dev and the file are implicitly valid as long as the queue
72 * The callbacks from macvlan are always done with rcu_read_lock held
73 * already, while in the file_operations, we get it ourselves.
75 * When destroying a queue, we remove the pointers from the file and
76 * from the dev and then synchronize_rcu to make sure no thread is
77 * still using the queue. There may still be references to the struct
78 * sock inside of the queue from outbound SKBs, but these never
79 * reference back to the file or the dev. The data structure is freed
80 * through __sk_free when both our references and any pending SKBs
83 * macvtap_lock is only used to prevent multiple concurrent open()
84 * calls to assign a new vlan->tap pointer. It could be moved into
85 * the macvlan_dev itself but is extremely rarely used.
87 static DEFINE_SPINLOCK(macvtap_lock);
90 * Choose the next free queue, for now there is only one
92 static int macvtap_set_queue(struct net_device *dev, struct file *file,
93 struct macvtap_queue *q)
95 struct macvlan_dev *vlan = netdev_priv(dev);
98 spin_lock(&macvtap_lock);
99 if (rcu_dereference(vlan->tap))
104 rcu_assign_pointer(vlan->tap, q);
107 rcu_assign_pointer(file->private_data, q);
110 spin_unlock(&macvtap_lock);
115 * We must destroy each queue exactly once, when either
116 * the netdev or the file go away.
118 * Using the spinlock makes sure that we don't get
119 * to the queue again after destroying it.
121 * synchronize_rcu serializes with the packet flow
122 * that uses rcu_read_lock.
124 static void macvtap_del_queue(struct macvtap_queue **qp)
126 struct macvtap_queue *q;
128 spin_lock(&macvtap_lock);
129 q = rcu_dereference(*qp);
131 spin_unlock(&macvtap_lock);
135 rcu_assign_pointer(q->vlan->tap, NULL);
136 rcu_assign_pointer(q->file->private_data, NULL);
137 spin_unlock(&macvtap_lock);
144 * Since we only support one queue, just dereference the pointer.
146 static struct macvtap_queue *macvtap_get_queue(struct net_device *dev,
149 struct macvlan_dev *vlan = netdev_priv(dev);
151 return rcu_dereference(vlan->tap);
154 static void macvtap_del_queues(struct net_device *dev)
156 struct macvlan_dev *vlan = netdev_priv(dev);
157 macvtap_del_queue(&vlan->tap);
160 static inline struct macvtap_queue *macvtap_file_get_queue(struct file *file)
163 return rcu_dereference(file->private_data);
166 static inline void macvtap_file_put_queue(void)
168 rcu_read_unlock_bh();
172 * Forward happens for data that gets sent from one macvlan
173 * endpoint to another one in bridge mode. We just take
174 * the skb and put it into the receive queue.
176 static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
178 struct macvtap_queue *q = macvtap_get_queue(dev, skb);
182 skb_queue_tail(&q->sk.sk_receive_queue, skb);
183 wake_up(q->sk.sk_sleep);
188 * Receive is for data from the external interface (lowerdev),
189 * in case of macvtap, we can treat that the same way as
190 * forward, which macvlan cannot.
192 static int macvtap_receive(struct sk_buff *skb)
194 skb_push(skb, ETH_HLEN);
195 return macvtap_forward(skb->dev, skb);
198 static int macvtap_newlink(struct net *src_net,
199 struct net_device *dev,
201 struct nlattr *data[])
203 struct device *classdev;
207 err = macvlan_common_newlink(src_net, dev, tb, data,
208 macvtap_receive, macvtap_forward);
212 devt = MKDEV(MAJOR(macvtap_major), dev->ifindex);
214 classdev = device_create(macvtap_class, &dev->dev, devt,
215 dev, "tap%d", dev->ifindex);
216 if (IS_ERR(classdev)) {
217 err = PTR_ERR(classdev);
218 macvtap_del_queues(dev);
225 static void macvtap_dellink(struct net_device *dev,
226 struct list_head *head)
228 device_destroy(macvtap_class,
229 MKDEV(MAJOR(macvtap_major), dev->ifindex));
231 macvtap_del_queues(dev);
232 macvlan_dellink(dev, head);
235 static struct rtnl_link_ops macvtap_link_ops __read_mostly = {
237 .newlink = macvtap_newlink,
238 .dellink = macvtap_dellink,
242 static void macvtap_sock_write_space(struct sock *sk)
244 if (!sock_writeable(sk) ||
245 !test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sk->sk_socket->flags))
248 if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
249 wake_up_interruptible_sync(sk->sk_sleep);
252 static int macvtap_open(struct inode *inode, struct file *file)
254 struct net *net = current->nsproxy->net_ns;
255 struct net_device *dev = dev_get_by_index(net, iminor(inode));
256 struct macvtap_queue *q;
263 /* check if this is a macvtap device */
265 if (dev->rtnl_link_ops != &macvtap_link_ops)
269 q = (struct macvtap_queue *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL,
274 init_waitqueue_head(&q->sock.wait);
275 q->sock.type = SOCK_RAW;
276 q->sock.state = SS_CONNECTED;
277 sock_init_data(&q->sock, &q->sk);
278 q->sk.sk_allocation = GFP_ATOMIC; /* for now */
279 q->sk.sk_write_space = macvtap_sock_write_space;
281 err = macvtap_set_queue(dev, file, q);
292 static int macvtap_release(struct inode *inode, struct file *file)
294 macvtap_del_queue((struct macvtap_queue **)&file->private_data);
298 static unsigned int macvtap_poll(struct file *file, poll_table * wait)
300 struct macvtap_queue *q = macvtap_file_get_queue(file);
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;
318 macvtap_file_put_queue();
322 /* Get packet from user space buffer */
323 static ssize_t macvtap_get_user(struct macvtap_queue *q,
324 const struct iovec *iv, size_t count,
331 if (unlikely(len < ETH_HLEN))
334 skb = sock_alloc_send_skb(&q->sk, NET_IP_ALIGN + len, noblock, &err);
337 macvlan_count_rx(q->vlan, 0, false, false);
341 skb_reserve(skb, NET_IP_ALIGN);
344 if (skb_copy_datagram_from_iovec(skb, 0, iv, 0, len)) {
345 macvlan_count_rx(q->vlan, 0, false, false);
350 skb_set_network_header(skb, ETH_HLEN);
352 macvlan_start_xmit(skb, q->vlan->dev);
357 static ssize_t macvtap_aio_write(struct kiocb *iocb, const struct iovec *iv,
358 unsigned long count, loff_t pos)
360 struct file *file = iocb->ki_filp;
361 ssize_t result = -ENOLINK;
362 struct macvtap_queue *q = macvtap_file_get_queue(file);
367 result = macvtap_get_user(q, iv, iov_length(iv, count),
368 file->f_flags & O_NONBLOCK);
370 macvtap_file_put_queue();
374 /* Put packet to the user space buffer */
375 static ssize_t macvtap_put_user(struct macvtap_queue *q,
376 const struct sk_buff *skb,
377 const struct iovec *iv, int len)
379 struct macvlan_dev *vlan = q->vlan;
382 len = min_t(int, skb->len, len);
384 ret = skb_copy_datagram_const_iovec(skb, 0, iv, 0, len);
386 macvlan_count_rx(vlan, len, ret == 0, 0);
388 return ret ? ret : len;
391 static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
392 unsigned long count, loff_t pos)
394 struct file *file = iocb->ki_filp;
395 struct macvtap_queue *q = macvtap_file_get_queue(file);
397 DECLARE_WAITQUEUE(wait, current);
399 ssize_t len, ret = 0;
406 len = iov_length(iv, count);
412 add_wait_queue(q->sk.sk_sleep, &wait);
414 current->state = TASK_INTERRUPTIBLE;
416 /* Read frames from the queue */
417 skb = skb_dequeue(&q->sk.sk_receive_queue);
419 if (file->f_flags & O_NONBLOCK) {
423 if (signal_pending(current)) {
427 /* Nothing to read, let's sleep */
431 ret = macvtap_put_user(q, skb, iv, len);
436 current->state = TASK_RUNNING;
437 remove_wait_queue(q->sk.sk_sleep, &wait);
440 macvtap_file_put_queue();
445 * provide compatibility with generic tun/tap interface
447 static long macvtap_ioctl(struct file *file, unsigned int cmd,
450 struct macvtap_queue *q;
451 void __user *argp = (void __user *)arg;
452 struct ifreq __user *ifr = argp;
453 unsigned int __user *up = argp;
455 char devname[IFNAMSIZ];
459 /* ignore the name, just look at flags */
460 if (get_user(u, &ifr->ifr_flags))
462 if (u != (IFF_TAP | IFF_NO_PI))
467 q = macvtap_file_get_queue(file);
470 memcpy(devname, q->vlan->dev->name, sizeof(devname));
471 macvtap_file_put_queue();
473 if (copy_to_user(&ifr->ifr_name, q->vlan->dev->name, IFNAMSIZ) ||
474 put_user((TUN_TAP_DEV | TUN_NO_PI), &ifr->ifr_flags))
479 if (put_user((IFF_TAP | IFF_NO_PI), up))
487 q = macvtap_file_get_queue(file);
489 macvtap_file_put_queue();
493 /* let the user check for future flags */
494 if (arg & ~(TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
495 TUN_F_TSO_ECN | TUN_F_UFO))
498 /* TODO: add support for these, so far we don't
499 support any offload */
500 if (arg & (TUN_F_CSUM | TUN_F_TSO4 | TUN_F_TSO6 |
501 TUN_F_TSO_ECN | TUN_F_UFO))
512 static long macvtap_compat_ioctl(struct file *file, unsigned int cmd,
515 return macvtap_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
519 static const struct file_operations macvtap_fops = {
520 .owner = THIS_MODULE,
521 .open = macvtap_open,
522 .release = macvtap_release,
523 .aio_read = macvtap_aio_read,
524 .aio_write = macvtap_aio_write,
525 .poll = macvtap_poll,
527 .unlocked_ioctl = macvtap_ioctl,
529 .compat_ioctl = macvtap_compat_ioctl,
533 static int macvtap_init(void)
537 err = alloc_chrdev_region(&macvtap_major, 0,
538 MACVTAP_NUM_DEVS, "macvtap");
542 cdev_init(&macvtap_cdev, &macvtap_fops);
543 err = cdev_add(&macvtap_cdev, macvtap_major, MACVTAP_NUM_DEVS);
547 macvtap_class = class_create(THIS_MODULE, "macvtap");
548 if (IS_ERR(macvtap_class)) {
549 err = PTR_ERR(macvtap_class);
553 err = macvlan_link_register(&macvtap_link_ops);
560 class_unregister(macvtap_class);
562 cdev_del(&macvtap_cdev);
564 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
568 module_init(macvtap_init);
570 static void macvtap_exit(void)
572 rtnl_link_unregister(&macvtap_link_ops);
573 class_unregister(macvtap_class);
574 cdev_del(&macvtap_cdev);
575 unregister_chrdev_region(macvtap_major, MACVTAP_NUM_DEVS);
577 module_exit(macvtap_exit);
579 MODULE_ALIAS_RTNL_LINK("macvtap");
580 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
581 MODULE_LICENSE("GPL");