3 #include <linux/if_vlan.h>
7 #include <linux/if_tun.h>
8 #include <linux/module.h>
11 #include <linux/sched.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
15 #include <linux/wait.h>
49 static struct proto macvtap_proto = {
58 static dev_t macvtap_major;
59 #define MACVTAP_NUM_DEVS (1U << MINORBITS)
63 #define GOODCOPY_LEN 128
64 static struct class *macvtap_class;
65 static struct cdev macvtap_cdev;
67 static const struct proto_ops macvtap_socket_ops;
98 lockdep_is_held(&macvtap_lock)) == q)
113 spin_lock(&macvtap_lock);
114 if (vlan->
numvtaps == MAX_MACVTAP_QUEUES)
118 index = get_slot(vlan,
NULL);
129 spin_unlock(&macvtap_lock);
145 spin_lock(&macvtap_lock);
147 lockdep_is_held(&macvtap_lock));
149 int index = get_slot(vlan, q);
157 spin_unlock(&macvtap_lock);
182 rxq = skb_get_rxhash(skb);
189 if (
likely(skb_rx_queue_recorded(skb))) {
190 rxq = skb_get_rx_queue(skb);
216 static void macvtap_del_queues(
struct net_device *dev)
223 spin_lock(&macvtap_lock);
224 for (i = 0; i < MAX_MACVTAP_QUEUES && vlan->
numvtaps; i++) {
226 lockdep_is_held(&macvtap_lock));
237 spin_unlock(&macvtap_lock);
241 for (--j; j >= 0; j--)
242 sock_put(&qlist[j]->
sk);
273 static int macvtap_receive(
struct sk_buff *skb)
276 return macvtap_forward(skb->
dev, skb);
279 static int macvtap_get_minor(
struct macvlan_dev *vlan)
306 static void macvtap_free_minor(
struct macvlan_dev *vlan)
316 static struct net_device *dev_get_by_macvtap_minor(
int minor)
331 static int macvtap_newlink(
struct net *src_net,
340 macvtap_receive, macvtap_forward);
343 static void macvtap_dellink(
struct net_device *dev,
346 macvtap_del_queues(dev);
350 static void macvtap_setup(
struct net_device *dev)
358 .setup = macvtap_setup,
359 .newlink = macvtap_newlink,
360 .dellink = macvtap_dellink,
364 static void macvtap_sock_write_space(
struct sock *
sk)
368 if (!sock_writeable(sk) ||
372 wqueue = sk_sleep(sk);
373 if (wqueue && waitqueue_active(wqueue))
377 static void macvtap_sock_destruct(
struct sock *sk)
382 static int macvtap_open(
struct inode *
inode,
struct file *file)
385 struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
404 q->
sock.ops = &macvtap_socket_ops;
406 q->
sk.sk_write_space = macvtap_sock_write_space;
407 q->
sk.sk_destruct = macvtap_sock_destruct;
421 err = macvtap_set_queue(dev, file, q);
432 static int macvtap_release(
struct inode *inode,
struct file *file)
435 macvtap_put_queue(q);
439 static unsigned int macvtap_poll(
struct file *file,
poll_table *
wait)
448 poll_wait(file, &q->
wq.wait, wait);
450 if (!skb_queue_empty(&q->
sk.sk_receive_queue))
453 if (sock_writeable(&q->
sk) ||
455 sock_writeable(&q->
sk)))
462 static inline struct sk_buff *macvtap_alloc_skb(
struct sock *sk,
size_t prepad,
464 int noblock,
int *err)
477 skb_reserve(skb, prepad);
486 static int zerocopy_sg_from_iovec(
struct sk_buff *skb,
const struct iovec *
from,
489 int len = iov_length(from, count) -
offset;
490 int copy = skb_headlen(skb);
495 while (count && (offset >= from->
iov_len)) {
502 while (count && (copy > 0)) {
503 size =
min_t(
unsigned int, copy, from->
iov_len - offset);
524 unsigned long truesize;
537 if (num_pages != size) {
549 int size =
min_t(
int, len, PAGE_SIZE - off);
550 __skb_fill_page_desc(skb, i, page[i], off, size);
551 skb_shinfo(skb)->nr_frags++;
567 static int macvtap_skb_from_vnet_hdr(
struct sk_buff *skb,
570 unsigned short gso_type = 0;
600 skb_shinfo(skb)->gso_size = vnet_hdr->
gso_size;
601 skb_shinfo(skb)->gso_type = gso_type;
605 skb_shinfo(skb)->gso_segs = 0;
610 static int macvtap_skb_to_vnet_hdr(
const struct sk_buff *skb,
613 memset(vnet_hdr, 0,
sizeof(*vnet_hdr));
615 if (skb_is_gso(skb)) {
619 vnet_hdr->
hdr_len = skb_headlen(skb);
636 vnet_hdr->
csum_start = skb_checksum_start_offset(skb);
649 size_t count,
int noblock)
656 int vnet_hdr_len = 0;
658 bool zerocopy =
false;
664 if (len < vnet_hdr_len)
700 if (copylen < vnet_hdr_len)
703 copylen -= vnet_hdr_len;
709 if (copylen < vnet_hdr.
hdr_len)
717 vnet_hdr.
hdr_len, noblock, &err);
722 err = zerocopy_sg_from_iovec(skb, iv, vnet_hdr_len, count);
729 skb_set_network_header(skb,
ETH_HLEN);
730 skb_reset_mac_header(skb);
731 skb->
protocol = eth_hdr(skb)->h_proto;
734 err = macvtap_skb_from_vnet_hdr(skb, &vnet_hdr);
750 rcu_read_unlock_bh();
761 vlan->
dev->stats.tx_dropped++;
762 rcu_read_unlock_bh();
768 unsigned long count, loff_t
pos)
770 struct file *file = iocb->
ki_filp;
774 result = macvtap_get_user(q,
NULL, iv, iov_length(iv, count), count,
782 const struct iovec *iv,
int len)
786 int vnet_hdr_len = 0;
793 if ((len -= vnet_hdr_len) < 0)
796 ret = macvtap_skb_to_vnet_hdr(skb, &vnet_hdr);
803 copied = vnet_hdr_len;
819 copy =
min_t(
int, vlan_offset, len);
826 copy =
min_t(
int,
sizeof(veth), len);
841 macvlan_count_rx(vlan, copied - vnet_hdr_len, ret == 0, 0);
842 rcu_read_unlock_bh();
844 return ret ? ret : copied;
848 const struct iovec *iv,
unsigned long len,
873 ret = macvtap_put_user(q, skb, iv, len);
883 unsigned long count, loff_t pos)
885 struct file *file = iocb->
ki_filp;
889 len = iov_length(iv, count);
904 static long macvtap_ioctl(
struct file *file,
unsigned int cmd,
936 rcu_read_unlock_bh();
993 static long macvtap_compat_ioctl(
struct file *file,
unsigned int cmd,
996 return macvtap_ioctl(file, cmd, (
unsigned long)compat_ptr(arg));
1002 .open = macvtap_open,
1003 .release = macvtap_release,
1004 .aio_read = macvtap_aio_read,
1005 .aio_write = macvtap_aio_write,
1006 .poll = macvtap_poll,
1008 .unlocked_ioctl = macvtap_ioctl,
1009 #ifdef CONFIG_COMPAT
1010 .compat_ioctl = macvtap_compat_ioctl,
1015 struct msghdr *m,
size_t total_len)
1023 struct msghdr *m,
size_t total_len,
1030 ret = macvtap_do_read(q, iocb, m->
msg_iov, total_len,
1032 if (ret > total_len) {
1040 static const struct proto_ops macvtap_socket_ops = {
1041 .sendmsg = macvtap_sendmsg,
1042 .recvmsg = macvtap_recvmsg,
1052 if (file->
f_op != &macvtap_fops)
1073 vlan = netdev_priv(dev);
1081 err = macvtap_get_minor(vlan);
1083 return notifier_from_errno(err);
1088 if (IS_ERR(classdev)) {
1089 macvtap_free_minor(vlan);
1090 return notifier_from_errno(PTR_ERR(classdev));
1096 macvtap_free_minor(vlan);
1103 static struct notifier_block macvtap_notifier_block __read_mostly = {
1104 .notifier_call = macvtap_device_event,
1107 static int macvtap_init(
void)
1116 cdev_init(&macvtap_cdev, &macvtap_fops);
1122 if (IS_ERR(macvtap_class)) {
1123 err = PTR_ERR(macvtap_class);
1150 static void macvtap_exit(
void)