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/slab.h>
13 #include <linux/init.h>
14 #include <linux/wait.h>
15 #include <linux/cdev.h>
18 #include <net/net_namespace.h>
19 #include <net/rtnetlink.h>
21 #include <linux/virtio_net.h>
24 * A macvtap queue is the central object of this driver, it connects
25 * an open character device to a macvlan interface. There can be
26 * multiple queues on one interface, which map back to queues
27 * implemented in hardware on the underlying device.
29 * macvtap_proto is used to allocate queues through the sock allocation
32 * TODO: multiqueue support is currently not implemented, even though
33 * macvtap is basically prepared for that. We will need to add this
34 * here as well as in virtio-net and qemu to get line rate on 10gbit
35 * adapters from a guest.
37 struct macvtap_queue
{
40 struct macvlan_dev
*vlan
;
45 static struct proto macvtap_proto
= {
48 .obj_size
= sizeof (struct macvtap_queue
),
52 * Minor number matches netdev->ifindex, so need a potentially
53 * large value. This also makes it possible to split the
54 * tap functionality out again in the future by offering it
55 * from other drivers besides macvtap. As long as every device
56 * only has one tap, the interface numbers assure that the
57 * device nodes are unique.
59 static unsigned int macvtap_major
;
60 #define MACVTAP_NUM_DEVS 65536
61 static struct class *macvtap_class
;
62 static struct cdev macvtap_cdev
;
64 static const struct proto_ops macvtap_socket_ops
;
68 * The macvtap_queue and the macvlan_dev are loosely coupled, the
69 * pointers from one to the other can only be read while rcu_read_lock
70 * or macvtap_lock is held.
72 * Both the file and the macvlan_dev hold a reference on the macvtap_queue
73 * through sock_hold(&q->sk). When the macvlan_dev goes away first,
74 * q->vlan becomes inaccessible. When the files gets closed,
75 * macvtap_get_queue() fails.
77 * There may still be references to the struct sock inside of the
78 * queue from outbound SKBs, but these never reference back to the
79 * file or the dev. The data structure is freed through __sk_free
80 * when both our references and any pending SKBs are gone.
82 static DEFINE_SPINLOCK(macvtap_lock
);
85 * Choose the next free queue, for now there is only one
87 static int macvtap_set_queue(struct net_device
*dev
, struct file
*file
,
88 struct macvtap_queue
*q
)
90 struct macvlan_dev
*vlan
= netdev_priv(dev
);
93 spin_lock(&macvtap_lock
);
94 if (rcu_dereference(vlan
->tap
))
98 rcu_assign_pointer(q
->vlan
, vlan
);
99 rcu_assign_pointer(vlan
->tap
, q
);
103 file
->private_data
= q
;
106 spin_unlock(&macvtap_lock
);
111 * The file owning the queue got closed, give up both
112 * the reference that the files holds as well as the
113 * one from the macvlan_dev if that still exists.
115 * Using the spinlock makes sure that we don't get
116 * to the queue again after destroying it.
118 static void macvtap_put_queue(struct macvtap_queue
*q
)
120 struct macvlan_dev
*vlan
;
122 spin_lock(&macvtap_lock
);
123 vlan
= rcu_dereference(q
->vlan
);
125 rcu_assign_pointer(vlan
->tap
, NULL
);
126 rcu_assign_pointer(q
->vlan
, NULL
);
130 spin_unlock(&macvtap_lock
);
137 * Since we only support one queue, just dereference the pointer.
139 static struct macvtap_queue
*macvtap_get_queue(struct net_device
*dev
,
142 struct macvlan_dev
*vlan
= netdev_priv(dev
);
144 return rcu_dereference(vlan
->tap
);
148 * The net_device is going away, give up the reference
149 * that it holds on the queue (all the queues one day)
150 * and safely set the pointer from the queues to NULL.
152 static void macvtap_del_queues(struct net_device
*dev
)
154 struct macvlan_dev
*vlan
= netdev_priv(dev
);
155 struct macvtap_queue
*q
;
157 spin_lock(&macvtap_lock
);
158 q
= rcu_dereference(vlan
->tap
);
160 spin_unlock(&macvtap_lock
);
164 rcu_assign_pointer(vlan
->tap
, NULL
);
165 rcu_assign_pointer(q
->vlan
, NULL
);
166 spin_unlock(&macvtap_lock
);
173 * Forward happens for data that gets sent from one macvlan
174 * endpoint to another one in bridge mode. We just take
175 * the skb and put it into the receive queue.
177 static int macvtap_forward(struct net_device
*dev
, struct sk_buff
*skb
)
179 struct macvtap_queue
*q
= macvtap_get_queue(dev
, skb
);
183 skb_queue_tail(&q
->sk
.sk_receive_queue
, skb
);
184 wake_up_interruptible_poll(q
->sk
.sk_sleep
, POLLIN
| POLLRDNORM
| POLLRDBAND
);
189 * Receive is for data from the external interface (lowerdev),
190 * in case of macvtap, we can treat that the same way as
191 * forward, which macvlan cannot.
193 static int macvtap_receive(struct sk_buff
*skb
)
195 skb_push(skb
, ETH_HLEN
);
196 return macvtap_forward(skb
->dev
, skb
);
199 static int macvtap_newlink(struct net
*src_net
,
200 struct net_device
*dev
,
202 struct nlattr
*data
[])
204 struct device
*classdev
;
208 err
= macvlan_common_newlink(src_net
, dev
, tb
, data
,
209 macvtap_receive
, macvtap_forward
);
213 devt
= MKDEV(MAJOR(macvtap_major
), dev
->ifindex
);
215 classdev
= device_create(macvtap_class
, &dev
->dev
, devt
,
216 dev
, "tap%d", dev
->ifindex
);
217 if (IS_ERR(classdev
)) {
218 err
= PTR_ERR(classdev
);
219 macvtap_del_queues(dev
);
226 static void macvtap_dellink(struct net_device
*dev
,
227 struct list_head
*head
)
229 device_destroy(macvtap_class
,
230 MKDEV(MAJOR(macvtap_major
), dev
->ifindex
));
232 macvtap_del_queues(dev
);
233 macvlan_dellink(dev
, head
);
236 static struct rtnl_link_ops macvtap_link_ops __read_mostly
= {
238 .newlink
= macvtap_newlink
,
239 .dellink
= macvtap_dellink
,
243 static void macvtap_sock_write_space(struct sock
*sk
)
245 if (!sock_writeable(sk
) ||
246 !test_and_clear_bit(SOCK_ASYNC_NOSPACE
, &sk
->sk_socket
->flags
))
249 if (sk
->sk_sleep
&& waitqueue_active(sk
->sk_sleep
))
250 wake_up_interruptible_poll(sk
->sk_sleep
, POLLOUT
| POLLWRNORM
| POLLWRBAND
);
253 static int macvtap_open(struct inode
*inode
, struct file
*file
)
255 struct net
*net
= current
->nsproxy
->net_ns
;
256 struct net_device
*dev
= dev_get_by_index(net
, iminor(inode
));
257 struct macvtap_queue
*q
;
264 /* check if this is a macvtap device */
266 if (dev
->rtnl_link_ops
!= &macvtap_link_ops
)
270 q
= (struct macvtap_queue
*)sk_alloc(net
, AF_UNSPEC
, GFP_KERNEL
,
275 init_waitqueue_head(&q
->sock
.wait
);
276 q
->sock
.type
= SOCK_RAW
;
277 q
->sock
.state
= SS_CONNECTED
;
279 q
->sock
.ops
= &macvtap_socket_ops
;
280 sock_init_data(&q
->sock
, &q
->sk
);
281 q
->sk
.sk_write_space
= macvtap_sock_write_space
;
282 q
->flags
= IFF_VNET_HDR
| IFF_NO_PI
| IFF_TAP
;
284 err
= macvtap_set_queue(dev
, file
, q
);
295 static int macvtap_release(struct inode
*inode
, struct file
*file
)
297 struct macvtap_queue
*q
= file
->private_data
;
298 macvtap_put_queue(q
);
302 static unsigned int macvtap_poll(struct file
*file
, poll_table
* wait
)
304 struct macvtap_queue
*q
= file
->private_data
;
305 unsigned int mask
= POLLERR
;
311 poll_wait(file
, &q
->sock
.wait
, wait
);
313 if (!skb_queue_empty(&q
->sk
.sk_receive_queue
))
314 mask
|= POLLIN
| POLLRDNORM
;
316 if (sock_writeable(&q
->sk
) ||
317 (!test_and_set_bit(SOCK_ASYNC_NOSPACE
, &q
->sock
.flags
) &&
318 sock_writeable(&q
->sk
)))
319 mask
|= POLLOUT
| POLLWRNORM
;
325 static inline struct sk_buff
*macvtap_alloc_skb(struct sock
*sk
, size_t prepad
,
326 size_t len
, size_t linear
,
327 int noblock
, int *err
)
331 /* Under a page? Don't bother with paged skb. */
332 if (prepad
+ len
< PAGE_SIZE
|| !linear
)
335 skb
= sock_alloc_send_pskb(sk
, prepad
+ linear
, len
- linear
, noblock
,
340 skb_reserve(skb
, prepad
);
341 skb_put(skb
, linear
);
342 skb
->data_len
= len
- linear
;
343 skb
->len
+= len
- linear
;
349 * macvtap_skb_from_vnet_hdr and macvtap_skb_to_vnet_hdr should
350 * be shared with the tun/tap driver.
352 static int macvtap_skb_from_vnet_hdr(struct sk_buff
*skb
,
353 struct virtio_net_hdr
*vnet_hdr
)
355 unsigned short gso_type
= 0;
356 if (vnet_hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
357 switch (vnet_hdr
->gso_type
& ~VIRTIO_NET_HDR_GSO_ECN
) {
358 case VIRTIO_NET_HDR_GSO_TCPV4
:
359 gso_type
= SKB_GSO_TCPV4
;
361 case VIRTIO_NET_HDR_GSO_TCPV6
:
362 gso_type
= SKB_GSO_TCPV6
;
364 case VIRTIO_NET_HDR_GSO_UDP
:
365 gso_type
= SKB_GSO_UDP
;
371 if (vnet_hdr
->gso_type
& VIRTIO_NET_HDR_GSO_ECN
)
372 gso_type
|= SKB_GSO_TCP_ECN
;
374 if (vnet_hdr
->gso_size
== 0)
378 if (vnet_hdr
->flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) {
379 if (!skb_partial_csum_set(skb
, vnet_hdr
->csum_start
,
380 vnet_hdr
->csum_offset
))
384 if (vnet_hdr
->gso_type
!= VIRTIO_NET_HDR_GSO_NONE
) {
385 skb_shinfo(skb
)->gso_size
= vnet_hdr
->gso_size
;
386 skb_shinfo(skb
)->gso_type
= gso_type
;
388 /* Header must be checked, and gso_segs computed. */
389 skb_shinfo(skb
)->gso_type
|= SKB_GSO_DODGY
;
390 skb_shinfo(skb
)->gso_segs
= 0;
395 static int macvtap_skb_to_vnet_hdr(const struct sk_buff
*skb
,
396 struct virtio_net_hdr
*vnet_hdr
)
398 memset(vnet_hdr
, 0, sizeof(*vnet_hdr
));
400 if (skb_is_gso(skb
)) {
401 struct skb_shared_info
*sinfo
= skb_shinfo(skb
);
403 /* This is a hint as to how much should be linear. */
404 vnet_hdr
->hdr_len
= skb_headlen(skb
);
405 vnet_hdr
->gso_size
= sinfo
->gso_size
;
406 if (sinfo
->gso_type
& SKB_GSO_TCPV4
)
407 vnet_hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV4
;
408 else if (sinfo
->gso_type
& SKB_GSO_TCPV6
)
409 vnet_hdr
->gso_type
= VIRTIO_NET_HDR_GSO_TCPV6
;
410 else if (sinfo
->gso_type
& SKB_GSO_UDP
)
411 vnet_hdr
->gso_type
= VIRTIO_NET_HDR_GSO_UDP
;
414 if (sinfo
->gso_type
& SKB_GSO_TCP_ECN
)
415 vnet_hdr
->gso_type
|= VIRTIO_NET_HDR_GSO_ECN
;
417 vnet_hdr
->gso_type
= VIRTIO_NET_HDR_GSO_NONE
;
419 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
420 vnet_hdr
->flags
= VIRTIO_NET_HDR_F_NEEDS_CSUM
;
421 vnet_hdr
->csum_start
= skb
->csum_start
-
423 vnet_hdr
->csum_offset
= skb
->csum_offset
;
424 } /* else everything is zero */
430 /* Get packet from user space buffer */
431 static ssize_t
macvtap_get_user(struct macvtap_queue
*q
,
432 const struct iovec
*iv
, size_t count
,
436 struct macvlan_dev
*vlan
;
439 struct virtio_net_hdr vnet_hdr
= { 0 };
440 int vnet_hdr_len
= 0;
442 if (q
->flags
& IFF_VNET_HDR
) {
443 vnet_hdr_len
= sizeof(vnet_hdr
);
446 if ((len
-= vnet_hdr_len
) < 0)
449 err
= memcpy_fromiovecend((void *)&vnet_hdr
, iv
, 0,
453 if ((vnet_hdr
.flags
& VIRTIO_NET_HDR_F_NEEDS_CSUM
) &&
454 vnet_hdr
.csum_start
+ vnet_hdr
.csum_offset
+ 2 >
456 vnet_hdr
.hdr_len
= vnet_hdr
.csum_start
+
457 vnet_hdr
.csum_offset
+ 2;
459 if (vnet_hdr
.hdr_len
> len
)
464 if (unlikely(len
< ETH_HLEN
))
467 skb
= macvtap_alloc_skb(&q
->sk
, NET_IP_ALIGN
, len
, vnet_hdr
.hdr_len
,
472 err
= skb_copy_datagram_from_iovec(skb
, 0, iv
, vnet_hdr_len
, len
);
476 skb_set_network_header(skb
, ETH_HLEN
);
477 skb_reset_mac_header(skb
);
478 skb
->protocol
= eth_hdr(skb
)->h_proto
;
481 err
= macvtap_skb_from_vnet_hdr(skb
, &vnet_hdr
);
487 vlan
= rcu_dereference(q
->vlan
);
489 macvlan_start_xmit(skb
, vlan
->dev
);
492 rcu_read_unlock_bh();
501 vlan
= rcu_dereference(q
->vlan
);
503 netdev_get_tx_queue(vlan
->dev
, 0)->tx_dropped
++;
504 rcu_read_unlock_bh();
509 static ssize_t
macvtap_aio_write(struct kiocb
*iocb
, const struct iovec
*iv
,
510 unsigned long count
, loff_t pos
)
512 struct file
*file
= iocb
->ki_filp
;
513 ssize_t result
= -ENOLINK
;
514 struct macvtap_queue
*q
= file
->private_data
;
516 result
= macvtap_get_user(q
, iv
, iov_length(iv
, count
),
517 file
->f_flags
& O_NONBLOCK
);
521 /* Put packet to the user space buffer */
522 static ssize_t
macvtap_put_user(struct macvtap_queue
*q
,
523 const struct sk_buff
*skb
,
524 const struct iovec
*iv
, int len
)
526 struct macvlan_dev
*vlan
;
528 int vnet_hdr_len
= 0;
530 if (q
->flags
& IFF_VNET_HDR
) {
531 struct virtio_net_hdr vnet_hdr
;
532 vnet_hdr_len
= sizeof (vnet_hdr
);
533 if ((len
-= vnet_hdr_len
) < 0)
536 ret
= macvtap_skb_to_vnet_hdr(skb
, &vnet_hdr
);
540 if (memcpy_toiovecend(iv
, (void *)&vnet_hdr
, 0, vnet_hdr_len
))
544 len
= min_t(int, skb
->len
, len
);
546 ret
= skb_copy_datagram_const_iovec(skb
, 0, iv
, vnet_hdr_len
, len
);
549 vlan
= rcu_dereference(q
->vlan
);
551 macvlan_count_rx(vlan
, len
, ret
== 0, 0);
552 rcu_read_unlock_bh();
554 return ret
? ret
: (len
+ vnet_hdr_len
);
557 static ssize_t
macvtap_do_read(struct macvtap_queue
*q
, struct kiocb
*iocb
,
558 const struct iovec
*iv
, unsigned long len
,
561 DECLARE_WAITQUEUE(wait
, current
);
565 add_wait_queue(q
->sk
.sk_sleep
, &wait
);
567 current
->state
= TASK_INTERRUPTIBLE
;
569 /* Read frames from the queue */
570 skb
= skb_dequeue(&q
->sk
.sk_receive_queue
);
576 if (signal_pending(current
)) {
580 /* Nothing to read, let's sleep */
584 ret
= macvtap_put_user(q
, skb
, iv
, len
);
589 current
->state
= TASK_RUNNING
;
590 remove_wait_queue(q
->sk
.sk_sleep
, &wait
);
594 static ssize_t
macvtap_aio_read(struct kiocb
*iocb
, const struct iovec
*iv
,
595 unsigned long count
, loff_t pos
)
597 struct file
*file
= iocb
->ki_filp
;
598 struct macvtap_queue
*q
= file
->private_data
;
599 ssize_t len
, ret
= 0;
601 len
= iov_length(iv
, count
);
607 ret
= macvtap_do_read(q
, iocb
, iv
, len
, file
->f_flags
& O_NONBLOCK
);
608 ret
= min_t(ssize_t
, ret
, len
); /* XXX copied from tun.c. Why? */
614 * provide compatibility with generic tun/tap interface
616 static long macvtap_ioctl(struct file
*file
, unsigned int cmd
,
619 struct macvtap_queue
*q
= file
->private_data
;
620 struct macvlan_dev
*vlan
;
621 void __user
*argp
= (void __user
*)arg
;
622 struct ifreq __user
*ifr
= argp
;
623 unsigned int __user
*up
= argp
;
629 /* ignore the name, just look at flags */
630 if (get_user(u
, &ifr
->ifr_flags
))
634 if ((u
& ~IFF_VNET_HDR
) != (IFF_NO_PI
| IFF_TAP
))
643 vlan
= rcu_dereference(q
->vlan
);
646 rcu_read_unlock_bh();
652 if (copy_to_user(&ifr
->ifr_name
, q
->vlan
->dev
->name
, IFNAMSIZ
) ||
653 put_user(q
->flags
, &ifr
->ifr_flags
))
659 if (put_user(IFF_TAP
| IFF_NO_PI
| IFF_VNET_HDR
, up
))
671 /* let the user check for future flags */
672 if (arg
& ~(TUN_F_CSUM
| TUN_F_TSO4
| TUN_F_TSO6
|
673 TUN_F_TSO_ECN
| TUN_F_UFO
))
676 /* TODO: only accept frames with the features that
677 got enabled for forwarded frames */
678 if (!(q
->flags
& IFF_VNET_HDR
))
688 static long macvtap_compat_ioctl(struct file
*file
, unsigned int cmd
,
691 return macvtap_ioctl(file
, cmd
, (unsigned long)compat_ptr(arg
));
695 static const struct file_operations macvtap_fops
= {
696 .owner
= THIS_MODULE
,
697 .open
= macvtap_open
,
698 .release
= macvtap_release
,
699 .aio_read
= macvtap_aio_read
,
700 .aio_write
= macvtap_aio_write
,
701 .poll
= macvtap_poll
,
703 .unlocked_ioctl
= macvtap_ioctl
,
705 .compat_ioctl
= macvtap_compat_ioctl
,
709 static int macvtap_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
710 struct msghdr
*m
, size_t total_len
)
712 struct macvtap_queue
*q
= container_of(sock
, struct macvtap_queue
, sock
);
713 return macvtap_get_user(q
, m
->msg_iov
, total_len
,
714 m
->msg_flags
& MSG_DONTWAIT
);
717 static int macvtap_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
718 struct msghdr
*m
, size_t total_len
,
721 struct macvtap_queue
*q
= container_of(sock
, struct macvtap_queue
, sock
);
723 if (flags
& ~(MSG_DONTWAIT
|MSG_TRUNC
))
725 ret
= macvtap_do_read(q
, iocb
, m
->msg_iov
, total_len
,
726 flags
& MSG_DONTWAIT
);
727 if (ret
> total_len
) {
728 m
->msg_flags
|= MSG_TRUNC
;
729 ret
= flags
& MSG_TRUNC
? ret
: total_len
;
734 /* Ops structure to mimic raw sockets with tun */
735 static const struct proto_ops macvtap_socket_ops
= {
736 .sendmsg
= macvtap_sendmsg
,
737 .recvmsg
= macvtap_recvmsg
,
740 /* Get an underlying socket object from tun file. Returns error unless file is
741 * attached to a device. The returned object works like a packet socket, it
742 * can be used for sock_sendmsg/sock_recvmsg. The caller is responsible for
743 * holding a reference to the file for as long as the socket is in use. */
744 struct socket
*macvtap_get_socket(struct file
*file
)
746 struct macvtap_queue
*q
;
747 if (file
->f_op
!= &macvtap_fops
)
748 return ERR_PTR(-EINVAL
);
749 q
= file
->private_data
;
751 return ERR_PTR(-EBADFD
);
754 EXPORT_SYMBOL_GPL(macvtap_get_socket
);
756 static int macvtap_init(void)
760 err
= alloc_chrdev_region(&macvtap_major
, 0,
761 MACVTAP_NUM_DEVS
, "macvtap");
765 cdev_init(&macvtap_cdev
, &macvtap_fops
);
766 err
= cdev_add(&macvtap_cdev
, macvtap_major
, MACVTAP_NUM_DEVS
);
770 macvtap_class
= class_create(THIS_MODULE
, "macvtap");
771 if (IS_ERR(macvtap_class
)) {
772 err
= PTR_ERR(macvtap_class
);
776 err
= macvlan_link_register(&macvtap_link_ops
);
783 class_unregister(macvtap_class
);
785 cdev_del(&macvtap_cdev
);
787 unregister_chrdev_region(macvtap_major
, MACVTAP_NUM_DEVS
);
791 module_init(macvtap_init
);
793 static void macvtap_exit(void)
795 rtnl_link_unregister(&macvtap_link_ops
);
796 class_unregister(macvtap_class
);
797 cdev_del(&macvtap_cdev
);
798 unregister_chrdev_region(macvtap_major
, MACVTAP_NUM_DEVS
);
800 module_exit(macvtap_exit
);
802 MODULE_ALIAS_RTNL_LINK("macvtap");
803 MODULE_AUTHOR("Arnd Bergmann <arnd@arndb.de>");
804 MODULE_LICENSE("GPL");