1 /* Copyright (C) 2009 Red Hat, Inc.
2 * Author: Michael S. Tsirkin <mst@redhat.com>
4 * This work is licensed under the terms of the GNU GPL, version 2.
6 * virtio-net server in host kernel.
9 #include <linux/compat.h>
10 #include <linux/eventfd.h>
11 #include <linux/vhost.h>
12 #include <linux/virtio_net.h>
13 #include <linux/mmu_context.h>
14 #include <linux/miscdevice.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/workqueue.h>
18 #include <linux/rcupdate.h>
19 #include <linux/file.h>
21 #include <linux/net.h>
22 #include <linux/if_packet.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_tun.h>
25 #include <linux/if_macvlan.h>
31 /* Max number of bytes transferred before requeueing the job.
32 * Using this limit prevents one virtqueue from starving others. */
33 #define VHOST_NET_WEIGHT 0x80000
41 enum vhost_net_poll_state
{
42 VHOST_NET_POLL_DISABLED
= 0,
43 VHOST_NET_POLL_STARTED
= 1,
44 VHOST_NET_POLL_STOPPED
= 2,
49 struct vhost_virtqueue vqs
[VHOST_NET_VQ_MAX
];
50 struct vhost_poll poll
[VHOST_NET_VQ_MAX
];
51 /* Tells us whether we are polling a socket for TX.
52 * We only do this when socket buffer fills up.
53 * Protected by tx vq lock. */
54 enum vhost_net_poll_state tx_poll_state
;
57 /* Pop first len bytes from iovec. Return number of segments used. */
58 static int move_iovec_hdr(struct iovec
*from
, struct iovec
*to
,
59 size_t len
, int iov_count
)
63 while (len
&& seg
< iov_count
) {
64 size
= min(from
->iov_len
, len
);
65 to
->iov_base
= from
->iov_base
;
67 from
->iov_len
-= size
;
68 from
->iov_base
+= size
;
77 /* Caller must have TX VQ lock */
78 static void tx_poll_stop(struct vhost_net
*net
)
80 if (likely(net
->tx_poll_state
!= VHOST_NET_POLL_STARTED
))
82 vhost_poll_stop(net
->poll
+ VHOST_NET_VQ_TX
);
83 net
->tx_poll_state
= VHOST_NET_POLL_STOPPED
;
86 /* Caller must have TX VQ lock */
87 static void tx_poll_start(struct vhost_net
*net
, struct socket
*sock
)
89 if (unlikely(net
->tx_poll_state
!= VHOST_NET_POLL_STOPPED
))
91 vhost_poll_start(net
->poll
+ VHOST_NET_VQ_TX
, sock
->file
);
92 net
->tx_poll_state
= VHOST_NET_POLL_STARTED
;
95 /* Expects to be always run from workqueue - which acts as
96 * read-size critical section for our kind of RCU. */
97 static void handle_tx(struct vhost_net
*net
)
99 struct vhost_virtqueue
*vq
= &net
->dev
.vqs
[VHOST_NET_VQ_TX
];
100 unsigned head
, out
, in
, s
;
101 struct msghdr msg
= {
107 .msg_flags
= MSG_DONTWAIT
,
109 size_t len
, total_len
= 0;
112 struct socket
*sock
= rcu_dereference(vq
->private_data
);
116 wmem
= atomic_read(&sock
->sk
->sk_wmem_alloc
);
117 if (wmem
>= sock
->sk
->sk_sndbuf
) {
118 mutex_lock(&vq
->mutex
);
119 tx_poll_start(net
, sock
);
120 mutex_unlock(&vq
->mutex
);
125 mutex_lock(&vq
->mutex
);
126 vhost_disable_notify(vq
);
128 if (wmem
< sock
->sk
->sk_sndbuf
/ 2)
130 hdr_size
= vq
->hdr_size
;
133 head
= vhost_get_vq_desc(&net
->dev
, vq
, vq
->iov
,
137 /* Nothing new? Wait for eventfd to tell us they refilled. */
138 if (head
== vq
->num
) {
139 wmem
= atomic_read(&sock
->sk
->sk_wmem_alloc
);
140 if (wmem
>= sock
->sk
->sk_sndbuf
* 3 / 4) {
141 tx_poll_start(net
, sock
);
142 set_bit(SOCK_ASYNC_NOSPACE
, &sock
->flags
);
145 if (unlikely(vhost_enable_notify(vq
))) {
146 vhost_disable_notify(vq
);
152 vq_err(vq
, "Unexpected descriptor format for TX: "
153 "out %d, int %d\n", out
, in
);
156 /* Skip header. TODO: support TSO. */
157 s
= move_iovec_hdr(vq
->iov
, vq
->hdr
, hdr_size
, out
);
158 msg
.msg_iovlen
= out
;
159 len
= iov_length(vq
->iov
, out
);
162 vq_err(vq
, "Unexpected header len for TX: "
163 "%zd expected %zd\n",
164 iov_length(vq
->hdr
, s
), hdr_size
);
167 /* TODO: Check specific error and bomb out unless ENOBUFS? */
168 err
= sock
->ops
->sendmsg(NULL
, sock
, &msg
, len
);
169 if (unlikely(err
< 0)) {
170 vhost_discard_vq_desc(vq
);
171 tx_poll_start(net
, sock
);
175 pr_err("Truncated TX packet: "
176 " len %d != %zd\n", err
, len
);
177 vhost_add_used_and_signal(&net
->dev
, vq
, head
, 0);
179 if (unlikely(total_len
>= VHOST_NET_WEIGHT
)) {
180 vhost_poll_queue(&vq
->poll
);
185 mutex_unlock(&vq
->mutex
);
186 unuse_mm(net
->dev
.mm
);
189 /* Expects to be always run from workqueue - which acts as
190 * read-size critical section for our kind of RCU. */
191 static void handle_rx(struct vhost_net
*net
)
193 struct vhost_virtqueue
*vq
= &net
->dev
.vqs
[VHOST_NET_VQ_RX
];
194 unsigned head
, out
, in
, log
, s
;
195 struct vhost_log
*vq_log
;
196 struct msghdr msg
= {
199 .msg_control
= NULL
, /* FIXME: get and handle RX aux data. */
202 .msg_flags
= MSG_DONTWAIT
,
205 struct virtio_net_hdr hdr
= {
207 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
210 size_t len
, total_len
= 0;
213 struct socket
*sock
= rcu_dereference(vq
->private_data
);
214 if (!sock
|| skb_queue_empty(&sock
->sk
->sk_receive_queue
))
218 mutex_lock(&vq
->mutex
);
219 vhost_disable_notify(vq
);
220 hdr_size
= vq
->hdr_size
;
222 vq_log
= unlikely(vhost_has_feature(&net
->dev
, VHOST_F_LOG_ALL
)) ?
226 head
= vhost_get_vq_desc(&net
->dev
, vq
, vq
->iov
,
230 /* OK, now we need to know about added descriptors. */
231 if (head
== vq
->num
) {
232 if (unlikely(vhost_enable_notify(vq
))) {
233 /* They have slipped one in as we were
234 * doing that: check again. */
235 vhost_disable_notify(vq
);
238 /* Nothing new? Wait for eventfd to tell us
242 /* We don't need to be notified again. */
244 vq_err(vq
, "Unexpected descriptor format for RX: "
249 /* Skip header. TODO: support TSO/mergeable rx buffers. */
250 s
= move_iovec_hdr(vq
->iov
, vq
->hdr
, hdr_size
, in
);
252 len
= iov_length(vq
->iov
, in
);
255 vq_err(vq
, "Unexpected header len for RX: "
256 "%zd expected %zd\n",
257 iov_length(vq
->hdr
, s
), hdr_size
);
260 err
= sock
->ops
->recvmsg(NULL
, sock
, &msg
,
261 len
, MSG_DONTWAIT
| MSG_TRUNC
);
262 /* TODO: Check specific error and bomb out unless EAGAIN? */
264 vhost_discard_vq_desc(vq
);
267 /* TODO: Should check and handle checksum. */
269 pr_err("Discarded truncated rx packet: "
270 " len %d > %zd\n", err
, len
);
271 vhost_discard_vq_desc(vq
);
275 err
= memcpy_toiovec(vq
->hdr
, (unsigned char *)&hdr
, hdr_size
);
277 vq_err(vq
, "Unable to write vnet_hdr at addr %p: %d\n",
278 vq
->iov
->iov_base
, err
);
282 vhost_add_used_and_signal(&net
->dev
, vq
, head
, len
);
283 if (unlikely(vq_log
))
284 vhost_log_write(vq
, vq_log
, log
, len
);
286 if (unlikely(total_len
>= VHOST_NET_WEIGHT
)) {
287 vhost_poll_queue(&vq
->poll
);
292 mutex_unlock(&vq
->mutex
);
293 unuse_mm(net
->dev
.mm
);
296 static void handle_tx_kick(struct work_struct
*work
)
298 struct vhost_virtqueue
*vq
;
299 struct vhost_net
*net
;
300 vq
= container_of(work
, struct vhost_virtqueue
, poll
.work
);
301 net
= container_of(vq
->dev
, struct vhost_net
, dev
);
305 static void handle_rx_kick(struct work_struct
*work
)
307 struct vhost_virtqueue
*vq
;
308 struct vhost_net
*net
;
309 vq
= container_of(work
, struct vhost_virtqueue
, poll
.work
);
310 net
= container_of(vq
->dev
, struct vhost_net
, dev
);
314 static void handle_tx_net(struct work_struct
*work
)
316 struct vhost_net
*net
;
317 net
= container_of(work
, struct vhost_net
, poll
[VHOST_NET_VQ_TX
].work
);
321 static void handle_rx_net(struct work_struct
*work
)
323 struct vhost_net
*net
;
324 net
= container_of(work
, struct vhost_net
, poll
[VHOST_NET_VQ_RX
].work
);
328 static int vhost_net_open(struct inode
*inode
, struct file
*f
)
330 struct vhost_net
*n
= kmalloc(sizeof *n
, GFP_KERNEL
);
334 n
->vqs
[VHOST_NET_VQ_TX
].handle_kick
= handle_tx_kick
;
335 n
->vqs
[VHOST_NET_VQ_RX
].handle_kick
= handle_rx_kick
;
336 r
= vhost_dev_init(&n
->dev
, n
->vqs
, VHOST_NET_VQ_MAX
);
342 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_TX
, handle_tx_net
, POLLOUT
);
343 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_RX
, handle_rx_net
, POLLIN
);
344 n
->tx_poll_state
= VHOST_NET_POLL_DISABLED
;
351 static void vhost_net_disable_vq(struct vhost_net
*n
,
352 struct vhost_virtqueue
*vq
)
354 if (!vq
->private_data
)
356 if (vq
== n
->vqs
+ VHOST_NET_VQ_TX
) {
358 n
->tx_poll_state
= VHOST_NET_POLL_DISABLED
;
360 vhost_poll_stop(n
->poll
+ VHOST_NET_VQ_RX
);
363 static void vhost_net_enable_vq(struct vhost_net
*n
,
364 struct vhost_virtqueue
*vq
)
366 struct socket
*sock
= vq
->private_data
;
369 if (vq
== n
->vqs
+ VHOST_NET_VQ_TX
) {
370 n
->tx_poll_state
= VHOST_NET_POLL_STOPPED
;
371 tx_poll_start(n
, sock
);
373 vhost_poll_start(n
->poll
+ VHOST_NET_VQ_RX
, sock
->file
);
376 static struct socket
*vhost_net_stop_vq(struct vhost_net
*n
,
377 struct vhost_virtqueue
*vq
)
381 mutex_lock(&vq
->mutex
);
382 sock
= vq
->private_data
;
383 vhost_net_disable_vq(n
, vq
);
384 rcu_assign_pointer(vq
->private_data
, NULL
);
385 mutex_unlock(&vq
->mutex
);
389 static void vhost_net_stop(struct vhost_net
*n
, struct socket
**tx_sock
,
390 struct socket
**rx_sock
)
392 *tx_sock
= vhost_net_stop_vq(n
, n
->vqs
+ VHOST_NET_VQ_TX
);
393 *rx_sock
= vhost_net_stop_vq(n
, n
->vqs
+ VHOST_NET_VQ_RX
);
396 static void vhost_net_flush_vq(struct vhost_net
*n
, int index
)
398 vhost_poll_flush(n
->poll
+ index
);
399 vhost_poll_flush(&n
->dev
.vqs
[index
].poll
);
402 static void vhost_net_flush(struct vhost_net
*n
)
404 vhost_net_flush_vq(n
, VHOST_NET_VQ_TX
);
405 vhost_net_flush_vq(n
, VHOST_NET_VQ_RX
);
408 static int vhost_net_release(struct inode
*inode
, struct file
*f
)
410 struct vhost_net
*n
= f
->private_data
;
411 struct socket
*tx_sock
;
412 struct socket
*rx_sock
;
414 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
416 vhost_dev_cleanup(&n
->dev
);
421 /* We do an extra flush before freeing memory,
422 * since jobs can re-queue themselves. */
428 static struct socket
*get_raw_socket(int fd
)
431 struct sockaddr_ll sa
;
432 char buf
[MAX_ADDR_LEN
];
434 int uaddr_len
= sizeof uaddr
, r
;
435 struct socket
*sock
= sockfd_lookup(fd
, &r
);
437 return ERR_PTR(-ENOTSOCK
);
439 /* Parameter checking */
440 if (sock
->sk
->sk_type
!= SOCK_RAW
) {
441 r
= -ESOCKTNOSUPPORT
;
445 r
= sock
->ops
->getname(sock
, (struct sockaddr
*)&uaddr
.sa
,
450 if (uaddr
.sa
.sll_family
!= AF_PACKET
) {
460 static struct socket
*get_tap_socket(int fd
)
462 struct file
*file
= fget(fd
);
465 return ERR_PTR(-EBADF
);
466 sock
= tun_get_socket(file
);
469 sock
= macvtap_get_socket(file
);
475 static struct socket
*get_socket(int fd
)
478 /* special case to disable backend */
481 sock
= get_raw_socket(fd
);
484 sock
= get_tap_socket(fd
);
487 return ERR_PTR(-ENOTSOCK
);
490 static long vhost_net_set_backend(struct vhost_net
*n
, unsigned index
, int fd
)
492 struct socket
*sock
, *oldsock
;
493 struct vhost_virtqueue
*vq
;
496 mutex_lock(&n
->dev
.mutex
);
497 r
= vhost_dev_check_owner(&n
->dev
);
501 if (index
>= VHOST_NET_VQ_MAX
) {
506 mutex_lock(&vq
->mutex
);
508 /* Verify that ring has been setup correctly. */
509 if (!vhost_vq_access_ok(vq
)) {
513 sock
= get_socket(fd
);
519 /* start polling new socket */
520 oldsock
= vq
->private_data
;
524 vhost_net_disable_vq(n
, vq
);
525 rcu_assign_pointer(vq
->private_data
, sock
);
526 vhost_net_enable_vq(n
, vq
);
529 vhost_net_flush_vq(n
, index
);
534 mutex_unlock(&vq
->mutex
);
536 mutex_unlock(&n
->dev
.mutex
);
540 static long vhost_net_reset_owner(struct vhost_net
*n
)
542 struct socket
*tx_sock
= NULL
;
543 struct socket
*rx_sock
= NULL
;
545 mutex_lock(&n
->dev
.mutex
);
546 err
= vhost_dev_check_owner(&n
->dev
);
549 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
551 err
= vhost_dev_reset_owner(&n
->dev
);
553 mutex_unlock(&n
->dev
.mutex
);
561 static int vhost_net_set_features(struct vhost_net
*n
, u64 features
)
563 size_t hdr_size
= features
& (1 << VHOST_NET_F_VIRTIO_NET_HDR
) ?
564 sizeof(struct virtio_net_hdr
) : 0;
566 mutex_lock(&n
->dev
.mutex
);
567 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
568 !vhost_log_access_ok(&n
->dev
)) {
569 mutex_unlock(&n
->dev
.mutex
);
572 n
->dev
.acked_features
= features
;
574 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
575 mutex_lock(&n
->vqs
[i
].mutex
);
576 n
->vqs
[i
].hdr_size
= hdr_size
;
577 mutex_unlock(&n
->vqs
[i
].mutex
);
580 mutex_unlock(&n
->dev
.mutex
);
584 static long vhost_net_ioctl(struct file
*f
, unsigned int ioctl
,
587 struct vhost_net
*n
= f
->private_data
;
588 void __user
*argp
= (void __user
*)arg
;
589 u64 __user
*featurep
= argp
;
590 struct vhost_vring_file backend
;
594 case VHOST_NET_SET_BACKEND
:
595 r
= copy_from_user(&backend
, argp
, sizeof backend
);
598 return vhost_net_set_backend(n
, backend
.index
, backend
.fd
);
599 case VHOST_GET_FEATURES
:
600 features
= VHOST_FEATURES
;
601 return copy_to_user(featurep
, &features
, sizeof features
);
602 case VHOST_SET_FEATURES
:
603 r
= copy_from_user(&features
, featurep
, sizeof features
);
606 if (features
& ~VHOST_FEATURES
)
608 return vhost_net_set_features(n
, features
);
609 case VHOST_RESET_OWNER
:
610 return vhost_net_reset_owner(n
);
612 mutex_lock(&n
->dev
.mutex
);
613 r
= vhost_dev_ioctl(&n
->dev
, ioctl
, arg
);
615 mutex_unlock(&n
->dev
.mutex
);
621 static long vhost_net_compat_ioctl(struct file
*f
, unsigned int ioctl
,
624 return vhost_net_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
628 const static struct file_operations vhost_net_fops
= {
629 .owner
= THIS_MODULE
,
630 .release
= vhost_net_release
,
631 .unlocked_ioctl
= vhost_net_ioctl
,
633 .compat_ioctl
= vhost_net_compat_ioctl
,
635 .open
= vhost_net_open
,
638 static struct miscdevice vhost_net_misc
= {
644 int vhost_net_init(void)
646 int r
= vhost_init();
649 r
= misc_register(&vhost_net_misc
);
659 module_init(vhost_net_init
);
661 void vhost_net_exit(void)
663 misc_deregister(&vhost_net_misc
);
666 module_exit(vhost_net_exit
);
668 MODULE_VERSION("0.0.1");
669 MODULE_LICENSE("GPL v2");
670 MODULE_AUTHOR("Michael S. Tsirkin");
671 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");