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