Skip to content

Commit 501c774

Browse files
arndbdavem330
authored andcommitted
net/macvtap: add vhost support
This adds support for passing a macvtap file descriptor into vhost-net, much like we already do for tun/tap. Most of the new code is taken from the respective patch in the tun driver and may get consolidated in the future. Signed-off-by: Arnd Bergmann <arnd@arndb.de> Acked-by: Sridhar Samudrala <sri@us.ibm.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 02df55d commit 501c774

4 files changed

Lines changed: 97 additions & 24 deletions

File tree

‎drivers/net/macvtap.c‎

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ static unsigned int macvtap_major;
5858
static struct class *macvtap_class;
5959
static struct cdev macvtap_cdev;
6060

61+
static const struct proto_ops macvtap_socket_ops;
62+
6163
/*
6264
* RCU usage:
6365
* The macvtap_queue and the macvlan_dev are loosely coupled, the
@@ -176,7 +178,7 @@ static int macvtap_forward(struct net_device *dev, struct sk_buff *skb)
176178
return -ENOLINK;
177179

178180
skb_queue_tail(&q->sk.sk_receive_queue, skb);
179-
wake_up(q->sk.sk_sleep);
181+
wake_up_interruptible_poll(q->sk.sk_sleep, POLLIN | POLLRDNORM | POLLRDBAND);
180182
return 0;
181183
}
182184

@@ -242,7 +244,7 @@ static void macvtap_sock_write_space(struct sock *sk)
242244
return;
243245

244246
if (sk->sk_sleep && waitqueue_active(sk->sk_sleep))
245-
wake_up_interruptible_sync(sk->sk_sleep);
247+
wake_up_interruptible_poll(sk->sk_sleep, POLLOUT | POLLWRNORM | POLLWRBAND);
246248
}
247249

248250
static int macvtap_open(struct inode *inode, struct file *file)
@@ -270,6 +272,8 @@ static int macvtap_open(struct inode *inode, struct file *file)
270272
init_waitqueue_head(&q->sock.wait);
271273
q->sock.type = SOCK_RAW;
272274
q->sock.state = SS_CONNECTED;
275+
q->sock.file = file;
276+
q->sock.ops = &macvtap_socket_ops;
273277
sock_init_data(&q->sock, &q->sk);
274278
q->sk.sk_write_space = macvtap_sock_write_space;
275279

@@ -387,32 +391,20 @@ static ssize_t macvtap_put_user(struct macvtap_queue *q,
387391

388392
rcu_read_lock_bh();
389393
vlan = rcu_dereference(q->vlan);
390-
macvlan_count_rx(vlan, len, ret == 0, 0);
394+
if (vlan)
395+
macvlan_count_rx(vlan, len, ret == 0, 0);
391396
rcu_read_unlock_bh();
392397

393398
return ret ? ret : len;
394399
}
395400

396-
static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
397-
unsigned long count, loff_t pos)
401+
static ssize_t macvtap_do_read(struct macvtap_queue *q, struct kiocb *iocb,
402+
const struct iovec *iv, unsigned long len,
403+
int noblock)
398404
{
399-
struct file *file = iocb->ki_filp;
400-
struct macvtap_queue *q = file->private_data;
401-
402405
DECLARE_WAITQUEUE(wait, current);
403406
struct sk_buff *skb;
404-
ssize_t len, ret = 0;
405-
406-
if (!q) {
407-
ret = -ENOLINK;
408-
goto out;
409-
}
410-
411-
len = iov_length(iv, count);
412-
if (len < 0) {
413-
ret = -EINVAL;
414-
goto out;
415-
}
407+
ssize_t ret = 0;
416408

417409
add_wait_queue(q->sk.sk_sleep, &wait);
418410
while (len) {
@@ -421,7 +413,7 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
421413
/* Read frames from the queue */
422414
skb = skb_dequeue(&q->sk.sk_receive_queue);
423415
if (!skb) {
424-
if (file->f_flags & O_NONBLOCK) {
416+
if (noblock) {
425417
ret = -EAGAIN;
426418
break;
427419
}
@@ -440,7 +432,24 @@ static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
440432

441433
current->state = TASK_RUNNING;
442434
remove_wait_queue(q->sk.sk_sleep, &wait);
435+
return ret;
436+
}
437+
438+
static ssize_t macvtap_aio_read(struct kiocb *iocb, const struct iovec *iv,
439+
unsigned long count, loff_t pos)
440+
{
441+
struct file *file = iocb->ki_filp;
442+
struct macvtap_queue *q = file->private_data;
443+
ssize_t len, ret = 0;
443444

445+
len = iov_length(iv, count);
446+
if (len < 0) {
447+
ret = -EINVAL;
448+
goto out;
449+
}
450+
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? */
444453
out:
445454
return ret;
446455
}
@@ -538,6 +547,53 @@ static const struct file_operations macvtap_fops = {
538547
#endif
539548
};
540549

550+
static int macvtap_sendmsg(struct kiocb *iocb, struct socket *sock,
551+
struct msghdr *m, size_t total_len)
552+
{
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);
556+
}
557+
558+
static int macvtap_recvmsg(struct kiocb *iocb, struct socket *sock,
559+
struct msghdr *m, size_t total_len,
560+
int flags)
561+
{
562+
struct macvtap_queue *q = container_of(sock, struct macvtap_queue, sock);
563+
int ret;
564+
if (flags & ~(MSG_DONTWAIT|MSG_TRUNC))
565+
return -EINVAL;
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;
571+
}
572+
return ret;
573+
}
574+
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,
579+
};
580+
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)
586+
{
587+
struct macvtap_queue *q;
588+
if (file->f_op != &macvtap_fops)
589+
return ERR_PTR(-EINVAL);
590+
q = file->private_data;
591+
if (!q)
592+
return ERR_PTR(-EBADFD);
593+
return &q->sock;
594+
}
595+
EXPORT_SYMBOL_GPL(macvtap_get_socket);
596+
541597
static int macvtap_init(void)
542598
{
543599
int err;

‎drivers/vhost/Kconfig‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
config VHOST_NET
22
tristate "Host kernel accelerator for virtio net (EXPERIMENTAL)"
3-
depends on NET && EVENTFD && (TUN || !TUN) && EXPERIMENTAL
3+
depends on NET && EVENTFD && (TUN || !TUN) && (MACVTAP || !MACVTAP) && EXPERIMENTAL
44
---help---
55
This kernel module can be loaded in host kernel to accelerate
66
guest networking with virtio_net. Not to be confused with virtio_net

‎drivers/vhost/net.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/if_packet.h>
2323
#include <linux/if_arp.h>
2424
#include <linux/if_tun.h>
25+
#include <linux/if_macvlan.h>
2526

2627
#include <net/sock.h>
2728

@@ -452,13 +453,16 @@ static struct socket *get_raw_socket(int fd)
452453
return ERR_PTR(r);
453454
}
454455

455-
static struct socket *get_tun_socket(int fd)
456+
static struct socket *get_tap_socket(int fd)
456457
{
457458
struct file *file = fget(fd);
458459
struct socket *sock;
459460
if (!file)
460461
return ERR_PTR(-EBADF);
461462
sock = tun_get_socket(file);
463+
if (!IS_ERR(sock))
464+
return sock;
465+
sock = macvtap_get_socket(file);
462466
if (IS_ERR(sock))
463467
fput(file);
464468
return sock;
@@ -473,7 +477,7 @@ static struct socket *get_socket(int fd)
473477
sock = get_raw_socket(fd);
474478
if (!IS_ERR(sock))
475479
return sock;
476-
sock = get_tun_socket(fd);
480+
sock = get_tap_socket(fd);
477481
if (!IS_ERR(sock))
478482
return sock;
479483
return ERR_PTR(-ENOTSOCK);

‎include/linux/if_macvlan.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77
#include <linux/netlink.h>
88
#include <net/netlink.h>
99

10+
#if defined(CONFIG_MACVTAP) || defined(CONFIG_MACVTAP_MODULE)
11+
struct socket *macvtap_get_socket(struct file *);
12+
#else
13+
#include <linux/err.h>
14+
#include <linux/errno.h>
15+
struct file;
16+
struct socket;
17+
static inline struct socket *macvtap_get_socket(struct file *f)
18+
{
19+
return ERR_PTR(-EINVAL);
20+
}
21+
#endif /* CONFIG_MACVTAP */
22+
1023
struct macvlan_port;
1124
struct macvtap_queue;
1225

0 commit comments

Comments
 (0)