]> Pileus Git - ~andy/linux/blob - drivers/vhost/net.c
vhost: replace vhost_workqueue with per-vhost kthread
[~andy/linux] / drivers / vhost / net.c
1 /* Copyright (C) 2009 Red Hat, Inc.
2  * Author: Michael S. Tsirkin <mst@redhat.com>
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2.
5  *
6  * virtio-net server in host kernel.
7  */
8
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>
21
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>
27
28 #include <net/sock.h>
29
30 #include "vhost.h"
31
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
35
36 enum {
37         VHOST_NET_VQ_RX = 0,
38         VHOST_NET_VQ_TX = 1,
39         VHOST_NET_VQ_MAX = 2,
40 };
41
42 enum vhost_net_poll_state {
43         VHOST_NET_POLL_DISABLED = 0,
44         VHOST_NET_POLL_STARTED = 1,
45         VHOST_NET_POLL_STOPPED = 2,
46 };
47
48 struct vhost_net {
49         struct vhost_dev dev;
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;
56 };
57
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)
61 {
62         int seg = 0;
63         size_t size;
64         while (len && seg < iov_count) {
65                 size = min(from->iov_len, len);
66                 to->iov_base = from->iov_base;
67                 to->iov_len = size;
68                 from->iov_len -= size;
69                 from->iov_base += size;
70                 len -= size;
71                 ++from;
72                 ++to;
73                 ++seg;
74         }
75         return seg;
76 }
77
78 /* Caller must have TX VQ lock */
79 static void tx_poll_stop(struct vhost_net *net)
80 {
81         if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED))
82                 return;
83         vhost_poll_stop(net->poll + VHOST_NET_VQ_TX);
84         net->tx_poll_state = VHOST_NET_POLL_STOPPED;
85 }
86
87 /* Caller must have TX VQ lock */
88 static void tx_poll_start(struct vhost_net *net, struct socket *sock)
89 {
90         if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED))
91                 return;
92         vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file);
93         net->tx_poll_state = VHOST_NET_POLL_STARTED;
94 }
95
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)
99 {
100         struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
101         unsigned out, in, s;
102         int head;
103         struct msghdr msg = {
104                 .msg_name = NULL,
105                 .msg_namelen = 0,
106                 .msg_control = NULL,
107                 .msg_controllen = 0,
108                 .msg_iov = vq->iov,
109                 .msg_flags = MSG_DONTWAIT,
110         };
111         size_t len, total_len = 0;
112         int err, wmem;
113         size_t hdr_size;
114         struct socket *sock = rcu_dereference(vq->private_data);
115         if (!sock)
116                 return;
117
118         wmem = atomic_read(&sock->sk->sk_wmem_alloc);
119         if (wmem >= sock->sk->sk_sndbuf) {
120                 mutex_lock(&vq->mutex);
121                 tx_poll_start(net, sock);
122                 mutex_unlock(&vq->mutex);
123                 return;
124         }
125
126         use_mm(net->dev.mm);
127         mutex_lock(&vq->mutex);
128         vhost_disable_notify(vq);
129
130         if (wmem < sock->sk->sk_sndbuf / 2)
131                 tx_poll_stop(net);
132         hdr_size = vq->hdr_size;
133
134         for (;;) {
135                 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
136                                          ARRAY_SIZE(vq->iov),
137                                          &out, &in,
138                                          NULL, NULL);
139                 /* On error, stop handling until the next kick. */
140                 if (unlikely(head < 0))
141                         break;
142                 /* Nothing new?  Wait for eventfd to tell us they refilled. */
143                 if (head == vq->num) {
144                         wmem = atomic_read(&sock->sk->sk_wmem_alloc);
145                         if (wmem >= sock->sk->sk_sndbuf * 3 / 4) {
146                                 tx_poll_start(net, sock);
147                                 set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
148                                 break;
149                         }
150                         if (unlikely(vhost_enable_notify(vq))) {
151                                 vhost_disable_notify(vq);
152                                 continue;
153                         }
154                         break;
155                 }
156                 if (in) {
157                         vq_err(vq, "Unexpected descriptor format for TX: "
158                                "out %d, int %d\n", out, in);
159                         break;
160                 }
161                 /* Skip header. TODO: support TSO. */
162                 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
163                 msg.msg_iovlen = out;
164                 len = iov_length(vq->iov, out);
165                 /* Sanity check */
166                 if (!len) {
167                         vq_err(vq, "Unexpected header len for TX: "
168                                "%zd expected %zd\n",
169                                iov_length(vq->hdr, s), hdr_size);
170                         break;
171                 }
172                 /* TODO: Check specific error and bomb out unless ENOBUFS? */
173                 err = sock->ops->sendmsg(NULL, sock, &msg, len);
174                 if (unlikely(err < 0)) {
175                         vhost_discard_vq_desc(vq);
176                         tx_poll_start(net, sock);
177                         break;
178                 }
179                 if (err != len)
180                         pr_debug("Truncated TX packet: "
181                                  " len %d != %zd\n", err, len);
182                 vhost_add_used_and_signal(&net->dev, vq, head, 0);
183                 total_len += len;
184                 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
185                         vhost_poll_queue(&vq->poll);
186                         break;
187                 }
188         }
189
190         mutex_unlock(&vq->mutex);
191         unuse_mm(net->dev.mm);
192 }
193
194 /* Expects to be always run from workqueue - which acts as
195  * read-size critical section for our kind of RCU. */
196 static void handle_rx(struct vhost_net *net)
197 {
198         struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
199         unsigned out, in, log, s;
200         int head;
201         struct vhost_log *vq_log;
202         struct msghdr msg = {
203                 .msg_name = NULL,
204                 .msg_namelen = 0,
205                 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
206                 .msg_controllen = 0,
207                 .msg_iov = vq->iov,
208                 .msg_flags = MSG_DONTWAIT,
209         };
210
211         struct virtio_net_hdr hdr = {
212                 .flags = 0,
213                 .gso_type = VIRTIO_NET_HDR_GSO_NONE
214         };
215
216         size_t len, total_len = 0;
217         int err;
218         size_t hdr_size;
219         struct socket *sock = rcu_dereference(vq->private_data);
220         if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
221                 return;
222
223         use_mm(net->dev.mm);
224         mutex_lock(&vq->mutex);
225         vhost_disable_notify(vq);
226         hdr_size = vq->hdr_size;
227
228         vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
229                 vq->log : NULL;
230
231         for (;;) {
232                 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
233                                          ARRAY_SIZE(vq->iov),
234                                          &out, &in,
235                                          vq_log, &log);
236                 /* On error, stop handling until the next kick. */
237                 if (unlikely(head < 0))
238                         break;
239                 /* OK, now we need to know about added descriptors. */
240                 if (head == vq->num) {
241                         if (unlikely(vhost_enable_notify(vq))) {
242                                 /* They have slipped one in as we were
243                                  * doing that: check again. */
244                                 vhost_disable_notify(vq);
245                                 continue;
246                         }
247                         /* Nothing new?  Wait for eventfd to tell us
248                          * they refilled. */
249                         break;
250                 }
251                 /* We don't need to be notified again. */
252                 if (out) {
253                         vq_err(vq, "Unexpected descriptor format for RX: "
254                                "out %d, int %d\n",
255                                out, in);
256                         break;
257                 }
258                 /* Skip header. TODO: support TSO/mergeable rx buffers. */
259                 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
260                 msg.msg_iovlen = in;
261                 len = iov_length(vq->iov, in);
262                 /* Sanity check */
263                 if (!len) {
264                         vq_err(vq, "Unexpected header len for RX: "
265                                "%zd expected %zd\n",
266                                iov_length(vq->hdr, s), hdr_size);
267                         break;
268                 }
269                 err = sock->ops->recvmsg(NULL, sock, &msg,
270                                          len, MSG_DONTWAIT | MSG_TRUNC);
271                 /* TODO: Check specific error and bomb out unless EAGAIN? */
272                 if (err < 0) {
273                         vhost_discard_vq_desc(vq);
274                         break;
275                 }
276                 /* TODO: Should check and handle checksum. */
277                 if (err > len) {
278                         pr_debug("Discarded truncated rx packet: "
279                                  " len %d > %zd\n", err, len);
280                         vhost_discard_vq_desc(vq);
281                         continue;
282                 }
283                 len = err;
284                 err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
285                 if (err) {
286                         vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
287                                vq->iov->iov_base, err);
288                         break;
289                 }
290                 len += hdr_size;
291                 vhost_add_used_and_signal(&net->dev, vq, head, len);
292                 if (unlikely(vq_log))
293                         vhost_log_write(vq, vq_log, log, len);
294                 total_len += len;
295                 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
296                         vhost_poll_queue(&vq->poll);
297                         break;
298                 }
299         }
300
301         mutex_unlock(&vq->mutex);
302         unuse_mm(net->dev.mm);
303 }
304
305 static void handle_tx_kick(struct vhost_work *work)
306 {
307         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
308                                                   poll.work);
309         struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
310
311         handle_tx(net);
312 }
313
314 static void handle_rx_kick(struct vhost_work *work)
315 {
316         struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
317                                                   poll.work);
318         struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
319
320         handle_rx(net);
321 }
322
323 static void handle_tx_net(struct vhost_work *work)
324 {
325         struct vhost_net *net = container_of(work, struct vhost_net,
326                                              poll[VHOST_NET_VQ_TX].work);
327         handle_tx(net);
328 }
329
330 static void handle_rx_net(struct vhost_work *work)
331 {
332         struct vhost_net *net = container_of(work, struct vhost_net,
333                                              poll[VHOST_NET_VQ_RX].work);
334         handle_rx(net);
335 }
336
337 static int vhost_net_open(struct inode *inode, struct file *f)
338 {
339         struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
340         struct vhost_dev *dev;
341         int r;
342
343         if (!n)
344                 return -ENOMEM;
345
346         dev = &n->dev;
347         n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
348         n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
349         r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
350         if (r < 0) {
351                 kfree(n);
352                 return r;
353         }
354
355         vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
356         vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
357         n->tx_poll_state = VHOST_NET_POLL_DISABLED;
358
359         f->private_data = n;
360
361         return 0;
362 }
363
364 static void vhost_net_disable_vq(struct vhost_net *n,
365                                  struct vhost_virtqueue *vq)
366 {
367         if (!vq->private_data)
368                 return;
369         if (vq == n->vqs + VHOST_NET_VQ_TX) {
370                 tx_poll_stop(n);
371                 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
372         } else
373                 vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
374 }
375
376 static void vhost_net_enable_vq(struct vhost_net *n,
377                                 struct vhost_virtqueue *vq)
378 {
379         struct socket *sock = vq->private_data;
380         if (!sock)
381                 return;
382         if (vq == n->vqs + VHOST_NET_VQ_TX) {
383                 n->tx_poll_state = VHOST_NET_POLL_STOPPED;
384                 tx_poll_start(n, sock);
385         } else
386                 vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
387 }
388
389 static struct socket *vhost_net_stop_vq(struct vhost_net *n,
390                                         struct vhost_virtqueue *vq)
391 {
392         struct socket *sock;
393
394         mutex_lock(&vq->mutex);
395         sock = vq->private_data;
396         vhost_net_disable_vq(n, vq);
397         rcu_assign_pointer(vq->private_data, NULL);
398         mutex_unlock(&vq->mutex);
399         return sock;
400 }
401
402 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
403                            struct socket **rx_sock)
404 {
405         *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
406         *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
407 }
408
409 static void vhost_net_flush_vq(struct vhost_net *n, int index)
410 {
411         vhost_poll_flush(n->poll + index);
412         vhost_poll_flush(&n->dev.vqs[index].poll);
413 }
414
415 static void vhost_net_flush(struct vhost_net *n)
416 {
417         vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
418         vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
419 }
420
421 static int vhost_net_release(struct inode *inode, struct file *f)
422 {
423         struct vhost_net *n = f->private_data;
424         struct socket *tx_sock;
425         struct socket *rx_sock;
426
427         vhost_net_stop(n, &tx_sock, &rx_sock);
428         vhost_net_flush(n);
429         vhost_dev_cleanup(&n->dev);
430         if (tx_sock)
431                 fput(tx_sock->file);
432         if (rx_sock)
433                 fput(rx_sock->file);
434         /* We do an extra flush before freeing memory,
435          * since jobs can re-queue themselves. */
436         vhost_net_flush(n);
437         kfree(n);
438         return 0;
439 }
440
441 static struct socket *get_raw_socket(int fd)
442 {
443         struct {
444                 struct sockaddr_ll sa;
445                 char  buf[MAX_ADDR_LEN];
446         } uaddr;
447         int uaddr_len = sizeof uaddr, r;
448         struct socket *sock = sockfd_lookup(fd, &r);
449         if (!sock)
450                 return ERR_PTR(-ENOTSOCK);
451
452         /* Parameter checking */
453         if (sock->sk->sk_type != SOCK_RAW) {
454                 r = -ESOCKTNOSUPPORT;
455                 goto err;
456         }
457
458         r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
459                                &uaddr_len, 0);
460         if (r)
461                 goto err;
462
463         if (uaddr.sa.sll_family != AF_PACKET) {
464                 r = -EPFNOSUPPORT;
465                 goto err;
466         }
467         return sock;
468 err:
469         fput(sock->file);
470         return ERR_PTR(r);
471 }
472
473 static struct socket *get_tap_socket(int fd)
474 {
475         struct file *file = fget(fd);
476         struct socket *sock;
477         if (!file)
478                 return ERR_PTR(-EBADF);
479         sock = tun_get_socket(file);
480         if (!IS_ERR(sock))
481                 return sock;
482         sock = macvtap_get_socket(file);
483         if (IS_ERR(sock))
484                 fput(file);
485         return sock;
486 }
487
488 static struct socket *get_socket(int fd)
489 {
490         struct socket *sock;
491         /* special case to disable backend */
492         if (fd == -1)
493                 return NULL;
494         sock = get_raw_socket(fd);
495         if (!IS_ERR(sock))
496                 return sock;
497         sock = get_tap_socket(fd);
498         if (!IS_ERR(sock))
499                 return sock;
500         return ERR_PTR(-ENOTSOCK);
501 }
502
503 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
504 {
505         struct socket *sock, *oldsock;
506         struct vhost_virtqueue *vq;
507         int r;
508
509         mutex_lock(&n->dev.mutex);
510         r = vhost_dev_check_owner(&n->dev);
511         if (r)
512                 goto err;
513
514         if (index >= VHOST_NET_VQ_MAX) {
515                 r = -ENOBUFS;
516                 goto err;
517         }
518         vq = n->vqs + index;
519         mutex_lock(&vq->mutex);
520
521         /* Verify that ring has been setup correctly. */
522         if (!vhost_vq_access_ok(vq)) {
523                 r = -EFAULT;
524                 goto err_vq;
525         }
526         sock = get_socket(fd);
527         if (IS_ERR(sock)) {
528                 r = PTR_ERR(sock);
529                 goto err_vq;
530         }
531
532         /* start polling new socket */
533         oldsock = vq->private_data;
534         if (sock != oldsock) {
535                 vhost_net_disable_vq(n, vq);
536                 rcu_assign_pointer(vq->private_data, sock);
537                 vhost_net_enable_vq(n, vq);
538         }
539
540         mutex_unlock(&vq->mutex);
541
542         if (oldsock) {
543                 vhost_net_flush_vq(n, index);
544                 fput(oldsock->file);
545         }
546
547         mutex_unlock(&n->dev.mutex);
548         return 0;
549
550 err_vq:
551         mutex_unlock(&vq->mutex);
552 err:
553         mutex_unlock(&n->dev.mutex);
554         return r;
555 }
556
557 static long vhost_net_reset_owner(struct vhost_net *n)
558 {
559         struct socket *tx_sock = NULL;
560         struct socket *rx_sock = NULL;
561         long err;
562         mutex_lock(&n->dev.mutex);
563         err = vhost_dev_check_owner(&n->dev);
564         if (err)
565                 goto done;
566         vhost_net_stop(n, &tx_sock, &rx_sock);
567         vhost_net_flush(n);
568         err = vhost_dev_reset_owner(&n->dev);
569 done:
570         mutex_unlock(&n->dev.mutex);
571         if (tx_sock)
572                 fput(tx_sock->file);
573         if (rx_sock)
574                 fput(rx_sock->file);
575         return err;
576 }
577
578 static int vhost_net_set_features(struct vhost_net *n, u64 features)
579 {
580         size_t hdr_size = features & (1 << VHOST_NET_F_VIRTIO_NET_HDR) ?
581                 sizeof(struct virtio_net_hdr) : 0;
582         int i;
583         mutex_lock(&n->dev.mutex);
584         if ((features & (1 << VHOST_F_LOG_ALL)) &&
585             !vhost_log_access_ok(&n->dev)) {
586                 mutex_unlock(&n->dev.mutex);
587                 return -EFAULT;
588         }
589         n->dev.acked_features = features;
590         smp_wmb();
591         for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
592                 mutex_lock(&n->vqs[i].mutex);
593                 n->vqs[i].hdr_size = hdr_size;
594                 mutex_unlock(&n->vqs[i].mutex);
595         }
596         vhost_net_flush(n);
597         mutex_unlock(&n->dev.mutex);
598         return 0;
599 }
600
601 static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
602                             unsigned long arg)
603 {
604         struct vhost_net *n = f->private_data;
605         void __user *argp = (void __user *)arg;
606         u64 __user *featurep = argp;
607         struct vhost_vring_file backend;
608         u64 features;
609         int r;
610         switch (ioctl) {
611         case VHOST_NET_SET_BACKEND:
612                 if (copy_from_user(&backend, argp, sizeof backend))
613                         return -EFAULT;
614                 return vhost_net_set_backend(n, backend.index, backend.fd);
615         case VHOST_GET_FEATURES:
616                 features = VHOST_FEATURES;
617                 if (copy_to_user(featurep, &features, sizeof features))
618                         return -EFAULT;
619                 return 0;
620         case VHOST_SET_FEATURES:
621                 if (copy_from_user(&features, featurep, sizeof features))
622                         return -EFAULT;
623                 if (features & ~VHOST_FEATURES)
624                         return -EOPNOTSUPP;
625                 return vhost_net_set_features(n, features);
626         case VHOST_RESET_OWNER:
627                 return vhost_net_reset_owner(n);
628         default:
629                 mutex_lock(&n->dev.mutex);
630                 r = vhost_dev_ioctl(&n->dev, ioctl, arg);
631                 vhost_net_flush(n);
632                 mutex_unlock(&n->dev.mutex);
633                 return r;
634         }
635 }
636
637 #ifdef CONFIG_COMPAT
638 static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
639                                    unsigned long arg)
640 {
641         return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
642 }
643 #endif
644
645 static const struct file_operations vhost_net_fops = {
646         .owner          = THIS_MODULE,
647         .release        = vhost_net_release,
648         .unlocked_ioctl = vhost_net_ioctl,
649 #ifdef CONFIG_COMPAT
650         .compat_ioctl   = vhost_net_compat_ioctl,
651 #endif
652         .open           = vhost_net_open,
653 };
654
655 static struct miscdevice vhost_net_misc = {
656         VHOST_NET_MINOR,
657         "vhost-net",
658         &vhost_net_fops,
659 };
660
661 static int vhost_net_init(void)
662 {
663         return misc_register(&vhost_net_misc);
664 }
665 module_init(vhost_net_init);
666
667 static void vhost_net_exit(void)
668 {
669         misc_deregister(&vhost_net_misc);
670 }
671 module_exit(vhost_net_exit);
672
673 MODULE_VERSION("0.0.1");
674 MODULE_LICENSE("GPL v2");
675 MODULE_AUTHOR("Michael S. Tsirkin");
676 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");