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>
20 #include <linux/slab.h>
22 #include <linux/net.h>
23 #include <linux/if_packet.h>
24 #include <linux/if_arp.h>
25 #include <linux/if_tun.h>
26 #include <linux/if_macvlan.h>
32 /* Max number of bytes transferred before requeueing the job.
33 * Using this limit prevents one virtqueue from starving others. */
34 #define VHOST_NET_WEIGHT 0x80000
42 enum vhost_net_poll_state {
43 VHOST_NET_POLL_DISABLED = 0,
44 VHOST_NET_POLL_STARTED = 1,
45 VHOST_NET_POLL_STOPPED = 2,
50 struct vhost_virtqueue vqs[VHOST_NET_VQ_MAX];
51 struct vhost_poll poll[VHOST_NET_VQ_MAX];
52 /* Tells us whether we are polling a socket for TX.
53 * We only do this when socket buffer fills up.
54 * Protected by tx vq lock. */
55 enum vhost_net_poll_state tx_poll_state;
58 /* Pop first len bytes from iovec. Return number of segments used. */
59 static int move_iovec_hdr(struct iovec *from, struct iovec *to,
60 size_t len, int iov_count)
64 while (len && seg < iov_count) {
65 size = min(from->iov_len, len);
66 to->iov_base = from->iov_base;
68 from->iov_len -= size;
69 from->iov_base += size;
78 /* Caller must have TX VQ lock */
79 static void tx_poll_stop(struct vhost_net *net)
81 if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED))
83 vhost_poll_stop(net->poll + VHOST_NET_VQ_TX);
84 net->tx_poll_state = VHOST_NET_POLL_STOPPED;
87 /* Caller must have TX VQ lock */
88 static void tx_poll_start(struct vhost_net *net, struct socket *sock)
90 if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED))
92 vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file);
93 net->tx_poll_state = VHOST_NET_POLL_STARTED;
96 /* Expects to be always run from workqueue - which acts as
97 * read-size critical section for our kind of RCU. */
98 static void handle_tx(struct vhost_net *net)
100 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
101 unsigned head, out, in, s;
102 struct msghdr msg = {
108 .msg_flags = MSG_DONTWAIT,
110 size_t len, total_len = 0;
113 struct socket *sock = rcu_dereference(vq->private_data);
117 wmem = atomic_read(&sock->sk->sk_wmem_alloc);
118 if (wmem >= sock->sk->sk_sndbuf) {
119 mutex_lock(&vq->mutex);
120 tx_poll_start(net, sock);
121 mutex_unlock(&vq->mutex);
126 mutex_lock(&vq->mutex);
127 vhost_disable_notify(vq);
129 if (wmem < sock->sk->sk_sndbuf / 2)
131 hdr_size = vq->hdr_size;
134 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
138 /* Nothing new? Wait for eventfd to tell us they refilled. */
139 if (head == vq->num) {
140 wmem = atomic_read(&sock->sk->sk_wmem_alloc);
141 if (wmem >= sock->sk->sk_sndbuf * 3 / 4) {
142 tx_poll_start(net, sock);
143 set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
146 if (unlikely(vhost_enable_notify(vq))) {
147 vhost_disable_notify(vq);
153 vq_err(vq, "Unexpected descriptor format for TX: "
154 "out %d, int %d\n", out, in);
157 /* Skip header. TODO: support TSO. */
158 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
159 msg.msg_iovlen = out;
160 len = iov_length(vq->iov, out);
163 vq_err(vq, "Unexpected header len for TX: "
164 "%zd expected %zd\n",
165 iov_length(vq->hdr, s), hdr_size);
168 /* TODO: Check specific error and bomb out unless ENOBUFS? */
169 err = sock->ops->sendmsg(NULL, sock, &msg, len);
170 if (unlikely(err < 0)) {
171 vhost_discard_vq_desc(vq);
172 tx_poll_start(net, sock);
176 pr_err("Truncated TX packet: "
177 " len %d != %zd\n", err, len);
178 vhost_add_used_and_signal(&net->dev, vq, head, 0);
180 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
181 vhost_poll_queue(&vq->poll);
186 mutex_unlock(&vq->mutex);
187 unuse_mm(net->dev.mm);
190 /* Expects to be always run from workqueue - which acts as
191 * read-size critical section for our kind of RCU. */
192 static void handle_rx(struct vhost_net *net)
194 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
195 unsigned head, out, in, log, s;
196 struct vhost_log *vq_log;
197 struct msghdr msg = {
200 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
203 .msg_flags = MSG_DONTWAIT,
206 struct virtio_net_hdr hdr = {
208 .gso_type = VIRTIO_NET_HDR_GSO_NONE
211 size_t len, total_len = 0;
214 struct socket *sock = rcu_dereference(vq->private_data);
215 if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
219 mutex_lock(&vq->mutex);
220 vhost_disable_notify(vq);
221 hdr_size = vq->hdr_size;
223 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
227 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
231 /* OK, now we need to know about added descriptors. */
232 if (head == vq->num) {
233 if (unlikely(vhost_enable_notify(vq))) {
234 /* They have slipped one in as we were
235 * doing that: check again. */
236 vhost_disable_notify(vq);
239 /* Nothing new? Wait for eventfd to tell us
243 /* We don't need to be notified again. */
245 vq_err(vq, "Unexpected descriptor format for RX: "
250 /* Skip header. TODO: support TSO/mergeable rx buffers. */
251 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
253 len = iov_length(vq->iov, in);
256 vq_err(vq, "Unexpected header len for RX: "
257 "%zd expected %zd\n",
258 iov_length(vq->hdr, s), hdr_size);
261 err = sock->ops->recvmsg(NULL, sock, &msg,
262 len, MSG_DONTWAIT | MSG_TRUNC);
263 /* TODO: Check specific error and bomb out unless EAGAIN? */
265 vhost_discard_vq_desc(vq);
268 /* TODO: Should check and handle checksum. */
270 pr_err("Discarded truncated rx packet: "
271 " len %d > %zd\n", err, len);
272 vhost_discard_vq_desc(vq);
276 err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
278 vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
279 vq->iov->iov_base, err);
283 vhost_add_used_and_signal(&net->dev, vq, head, len);
284 if (unlikely(vq_log))
285 vhost_log_write(vq, vq_log, log, len);
287 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
288 vhost_poll_queue(&vq->poll);
293 mutex_unlock(&vq->mutex);
294 unuse_mm(net->dev.mm);
297 static void handle_tx_kick(struct work_struct *work)
299 struct vhost_virtqueue *vq;
300 struct vhost_net *net;
301 vq = container_of(work, struct vhost_virtqueue, poll.work);
302 net = container_of(vq->dev, struct vhost_net, dev);
306 static void handle_rx_kick(struct work_struct *work)
308 struct vhost_virtqueue *vq;
309 struct vhost_net *net;
310 vq = container_of(work, struct vhost_virtqueue, poll.work);
311 net = container_of(vq->dev, struct vhost_net, dev);
315 static void handle_tx_net(struct work_struct *work)
317 struct vhost_net *net;
318 net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_TX].work);
322 static void handle_rx_net(struct work_struct *work)
324 struct vhost_net *net;
325 net = container_of(work, struct vhost_net, poll[VHOST_NET_VQ_RX].work);
329 static int vhost_net_open(struct inode *inode, struct file *f)
331 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
335 n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
336 n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
337 r = vhost_dev_init(&n->dev, n->vqs, VHOST_NET_VQ_MAX);
343 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT);
344 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN);
345 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
352 static void vhost_net_disable_vq(struct vhost_net *n,
353 struct vhost_virtqueue *vq)
355 if (!vq->private_data)
357 if (vq == n->vqs + VHOST_NET_VQ_TX) {
359 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
361 vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
364 static void vhost_net_enable_vq(struct vhost_net *n,
365 struct vhost_virtqueue *vq)
367 struct socket *sock = vq->private_data;
370 if (vq == n->vqs + VHOST_NET_VQ_TX) {
371 n->tx_poll_state = VHOST_NET_POLL_STOPPED;
372 tx_poll_start(n, sock);
374 vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
377 static struct socket *vhost_net_stop_vq(struct vhost_net *n,
378 struct vhost_virtqueue *vq)
382 mutex_lock(&vq->mutex);
383 sock = vq->private_data;
384 vhost_net_disable_vq(n, vq);
385 rcu_assign_pointer(vq->private_data, NULL);
386 mutex_unlock(&vq->mutex);
390 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
391 struct socket **rx_sock)
393 *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
394 *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
397 static void vhost_net_flush_vq(struct vhost_net *n, int index)
399 vhost_poll_flush(n->poll + index);
400 vhost_poll_flush(&n->dev.vqs[index].poll);
403 static void vhost_net_flush(struct vhost_net *n)
405 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
406 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
409 static int vhost_net_release(struct inode *inode, struct file *f)
411 struct vhost_net *n = f->private_data;
412 struct socket *tx_sock;
413 struct socket *rx_sock;
415 vhost_net_stop(n, &tx_sock, &rx_sock);
417 vhost_dev_cleanup(&n->dev);
422 /* We do an extra flush before freeing memory,
423 * since jobs can re-queue themselves. */
429 static struct socket *get_raw_socket(int fd)
432 struct sockaddr_ll sa;
433 char buf[MAX_ADDR_LEN];
435 int uaddr_len = sizeof uaddr, r;
436 struct socket *sock = sockfd_lookup(fd, &r);
438 return ERR_PTR(-ENOTSOCK);
440 /* Parameter checking */
441 if (sock->sk->sk_type != SOCK_RAW) {
442 r = -ESOCKTNOSUPPORT;
446 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
451 if (uaddr.sa.sll_family != AF_PACKET) {
461 static struct socket *get_tap_socket(int fd)
463 struct file *file = fget(fd);
466 return ERR_PTR(-EBADF);
467 sock = tun_get_socket(file);
470 sock = macvtap_get_socket(file);
476 static struct socket *get_socket(int fd)
479 /* special case to disable backend */
482 sock = get_raw_socket(fd);
485 sock = get_tap_socket(fd);
488 return ERR_PTR(-ENOTSOCK);
491 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
493 struct socket *sock, *oldsock;
494 struct vhost_virtqueue *vq;
497 mutex_lock(&n->dev.mutex);
498 r = vhost_dev_check_owner(&n->dev);
502 if (index >= VHOST_NET_VQ_MAX) {
507 mutex_lock(&vq->mutex);
509 /* Verify that ring has been setup correctly. */
510 if (!vhost_vq_access_ok(vq)) {
514 sock = get_socket(fd);
520 /* start polling new socket */
521 oldsock = vq->private_data;
525 vhost_net_disable_vq(n, vq);
526 rcu_assign_pointer(vq->private_data, sock);
527 vhost_net_enable_vq(n, vq);
530 vhost_net_flush_vq(n, index);
535 mutex_unlock(&vq->mutex);
537 mutex_unlock(&n->dev.mutex);
541 static long vhost_net_reset_owner(struct vhost_net *n)
543 struct socket *tx_sock = NULL;
544 struct socket *rx_sock = NULL;
546 mutex_lock(&n->dev.mutex);
547 err = vhost_dev_check_owner(&n->dev);
550 vhost_net_stop(n, &tx_sock, &rx_sock);
552 err = vhost_dev_reset_owner(&n->dev);
554 mutex_unlock(&n->dev.mutex);
562 static int vhost_net_set_features(struct vhost_net *n, u64 features)
564 size_t hdr_size = features & (1 << VHOST_NET_F_VIRTIO_NET_HDR) ?
565 sizeof(struct virtio_net_hdr) : 0;
567 mutex_lock(&n->dev.mutex);
568 if ((features & (1 << VHOST_F_LOG_ALL)) &&
569 !vhost_log_access_ok(&n->dev)) {
570 mutex_unlock(&n->dev.mutex);
573 n->dev.acked_features = features;
575 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
576 mutex_lock(&n->vqs[i].mutex);
577 n->vqs[i].hdr_size = hdr_size;
578 mutex_unlock(&n->vqs[i].mutex);
581 mutex_unlock(&n->dev.mutex);
585 static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
588 struct vhost_net *n = f->private_data;
589 void __user *argp = (void __user *)arg;
590 u64 __user *featurep = argp;
591 struct vhost_vring_file backend;
595 case VHOST_NET_SET_BACKEND:
596 if (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 if (copy_to_user(featurep, &features, sizeof features))
604 case VHOST_SET_FEATURES:
605 if (copy_from_user(&features, featurep, sizeof features))
607 if (features & ~VHOST_FEATURES)
609 return vhost_net_set_features(n, features);
610 case VHOST_RESET_OWNER:
611 return vhost_net_reset_owner(n);
613 mutex_lock(&n->dev.mutex);
614 r = vhost_dev_ioctl(&n->dev, ioctl, arg);
616 mutex_unlock(&n->dev.mutex);
622 static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
625 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
629 const static struct file_operations vhost_net_fops = {
630 .owner = THIS_MODULE,
631 .release = vhost_net_release,
632 .unlocked_ioctl = vhost_net_ioctl,
634 .compat_ioctl = vhost_net_compat_ioctl,
636 .open = vhost_net_open,
639 static struct miscdevice vhost_net_misc = {
645 static int vhost_net_init(void)
647 int r = vhost_init();
650 r = misc_register(&vhost_net_misc);
660 module_init(vhost_net_init);
662 static void vhost_net_exit(void)
664 misc_deregister(&vhost_net_misc);
667 module_exit(vhost_net_exit);
669 MODULE_VERSION("0.0.1");
670 MODULE_LICENSE("GPL v2");
671 MODULE_AUTHOR("Michael S. Tsirkin");
672 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");