vhost: Cleanup vhost.c and net.c
[deliverable/linux.git] / 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/miscdevice.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/workqueue.h>
17 #include <linux/rcupdate.h>
18 #include <linux/file.h>
19 #include <linux/slab.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
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 /* Copy iovec entries for len bytes from iovec. */
78 static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
79 size_t len, int iovcount)
80 {
81 int seg = 0;
82 size_t size;
83
84 while (len && seg < iovcount) {
85 size = min(from->iov_len, len);
86 to->iov_base = from->iov_base;
87 to->iov_len = size;
88 len -= size;
89 ++from;
90 ++to;
91 ++seg;
92 }
93 }
94
95 /* Caller must have TX VQ lock */
96 static void tx_poll_stop(struct vhost_net *net)
97 {
98 if (likely(net->tx_poll_state != VHOST_NET_POLL_STARTED))
99 return;
100 vhost_poll_stop(net->poll + VHOST_NET_VQ_TX);
101 net->tx_poll_state = VHOST_NET_POLL_STOPPED;
102 }
103
104 /* Caller must have TX VQ lock */
105 static void tx_poll_start(struct vhost_net *net, struct socket *sock)
106 {
107 if (unlikely(net->tx_poll_state != VHOST_NET_POLL_STOPPED))
108 return;
109 vhost_poll_start(net->poll + VHOST_NET_VQ_TX, sock->file);
110 net->tx_poll_state = VHOST_NET_POLL_STARTED;
111 }
112
113 /* Expects to be always run from workqueue - which acts as
114 * read-size critical section for our kind of RCU. */
115 static void handle_tx(struct vhost_net *net)
116 {
117 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
118 unsigned out, in, s;
119 int head;
120 struct msghdr msg = {
121 .msg_name = NULL,
122 .msg_namelen = 0,
123 .msg_control = NULL,
124 .msg_controllen = 0,
125 .msg_iov = vq->iov,
126 .msg_flags = MSG_DONTWAIT,
127 };
128 size_t len, total_len = 0;
129 int err, wmem;
130 size_t hdr_size;
131 struct socket *sock;
132
133 /* TODO: check that we are running from vhost_worker? */
134 sock = rcu_dereference_check(vq->private_data, 1);
135 if (!sock)
136 return;
137
138 wmem = atomic_read(&sock->sk->sk_wmem_alloc);
139 if (wmem >= sock->sk->sk_sndbuf) {
140 mutex_lock(&vq->mutex);
141 tx_poll_start(net, sock);
142 mutex_unlock(&vq->mutex);
143 return;
144 }
145
146 mutex_lock(&vq->mutex);
147 vhost_disable_notify(vq);
148
149 if (wmem < sock->sk->sk_sndbuf / 2)
150 tx_poll_stop(net);
151 hdr_size = vq->vhost_hlen;
152
153 for (;;) {
154 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
155 ARRAY_SIZE(vq->iov),
156 &out, &in,
157 NULL, NULL);
158 /* On error, stop handling until the next kick. */
159 if (unlikely(head < 0))
160 break;
161 /* Nothing new? Wait for eventfd to tell us they refilled. */
162 if (head == vq->num) {
163 wmem = atomic_read(&sock->sk->sk_wmem_alloc);
164 if (wmem >= sock->sk->sk_sndbuf * 3 / 4) {
165 tx_poll_start(net, sock);
166 set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
167 break;
168 }
169 if (unlikely(vhost_enable_notify(vq))) {
170 vhost_disable_notify(vq);
171 continue;
172 }
173 break;
174 }
175 if (in) {
176 vq_err(vq, "Unexpected descriptor format for TX: "
177 "out %d, int %d\n", out, in);
178 break;
179 }
180 /* Skip header. TODO: support TSO. */
181 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, out);
182 msg.msg_iovlen = out;
183 len = iov_length(vq->iov, out);
184 /* Sanity check */
185 if (!len) {
186 vq_err(vq, "Unexpected header len for TX: "
187 "%zd expected %zd\n",
188 iov_length(vq->hdr, s), hdr_size);
189 break;
190 }
191 /* TODO: Check specific error and bomb out unless ENOBUFS? */
192 err = sock->ops->sendmsg(NULL, sock, &msg, len);
193 if (unlikely(err < 0)) {
194 vhost_discard_vq_desc(vq, 1);
195 tx_poll_start(net, sock);
196 break;
197 }
198 if (err != len)
199 pr_debug("Truncated TX packet: "
200 " len %d != %zd\n", err, len);
201 vhost_add_used_and_signal(&net->dev, vq, head, 0);
202 total_len += len;
203 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
204 vhost_poll_queue(&vq->poll);
205 break;
206 }
207 }
208
209 mutex_unlock(&vq->mutex);
210 }
211
212 static int peek_head_len(struct sock *sk)
213 {
214 struct sk_buff *head;
215 int len = 0;
216
217 lock_sock(sk);
218 head = skb_peek(&sk->sk_receive_queue);
219 if (head)
220 len = head->len;
221 release_sock(sk);
222 return len;
223 }
224
225 /* This is a multi-buffer version of vhost_get_desc, that works if
226 * vq has read descriptors only.
227 * @vq - the relevant virtqueue
228 * @datalen - data length we'll be reading
229 * @iovcount - returned count of io vectors we fill
230 * @log - vhost log
231 * @log_num - log offset
232 * returns number of buffer heads allocated, negative on error
233 */
234 static int get_rx_bufs(struct vhost_virtqueue *vq,
235 struct vring_used_elem *heads,
236 int datalen,
237 unsigned *iovcount,
238 struct vhost_log *log,
239 unsigned *log_num)
240 {
241 unsigned int out, in;
242 int seg = 0;
243 int headcount = 0;
244 unsigned d;
245 int r, nlogs = 0;
246
247 while (datalen > 0) {
248 if (unlikely(seg >= UIO_MAXIOV)) {
249 r = -ENOBUFS;
250 goto err;
251 }
252 d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
253 ARRAY_SIZE(vq->iov) - seg, &out,
254 &in, log, log_num);
255 if (d == vq->num) {
256 r = 0;
257 goto err;
258 }
259 if (unlikely(out || in <= 0)) {
260 vq_err(vq, "unexpected descriptor format for RX: "
261 "out %d, in %d\n", out, in);
262 r = -EINVAL;
263 goto err;
264 }
265 if (unlikely(log)) {
266 nlogs += *log_num;
267 log += *log_num;
268 }
269 heads[headcount].id = d;
270 heads[headcount].len = iov_length(vq->iov + seg, in);
271 datalen -= heads[headcount].len;
272 ++headcount;
273 seg += in;
274 }
275 heads[headcount - 1].len += datalen;
276 *iovcount = seg;
277 if (unlikely(log))
278 *log_num = nlogs;
279 return headcount;
280 err:
281 vhost_discard_vq_desc(vq, headcount);
282 return r;
283 }
284
285 /* Expects to be always run from workqueue - which acts as
286 * read-size critical section for our kind of RCU. */
287 static void handle_rx_big(struct vhost_net *net)
288 {
289 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
290 unsigned out, in, log, s;
291 int head;
292 struct vhost_log *vq_log;
293 struct msghdr msg = {
294 .msg_name = NULL,
295 .msg_namelen = 0,
296 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
297 .msg_controllen = 0,
298 .msg_iov = vq->iov,
299 .msg_flags = MSG_DONTWAIT,
300 };
301 struct virtio_net_hdr hdr = {
302 .flags = 0,
303 .gso_type = VIRTIO_NET_HDR_GSO_NONE
304 };
305 size_t len, total_len = 0;
306 int err;
307 size_t hdr_size;
308 /* TODO: check that we are running from vhost_worker? */
309 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
310
311 if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
312 return;
313
314 mutex_lock(&vq->mutex);
315 vhost_disable_notify(vq);
316 hdr_size = vq->vhost_hlen;
317
318 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
319 vq->log : NULL;
320
321 for (;;) {
322 head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
323 ARRAY_SIZE(vq->iov),
324 &out, &in,
325 vq_log, &log);
326 /* On error, stop handling until the next kick. */
327 if (unlikely(head < 0))
328 break;
329 /* OK, now we need to know about added descriptors. */
330 if (head == vq->num) {
331 if (unlikely(vhost_enable_notify(vq))) {
332 /* They have slipped one in as we were
333 * doing that: check again. */
334 vhost_disable_notify(vq);
335 continue;
336 }
337 /* Nothing new? Wait for eventfd to tell us
338 * they refilled. */
339 break;
340 }
341 /* We don't need to be notified again. */
342 if (out) {
343 vq_err(vq, "Unexpected descriptor format for RX: "
344 "out %d, int %d\n",
345 out, in);
346 break;
347 }
348 /* Skip header. TODO: support TSO/mergeable rx buffers. */
349 s = move_iovec_hdr(vq->iov, vq->hdr, hdr_size, in);
350 msg.msg_iovlen = in;
351 len = iov_length(vq->iov, in);
352 /* Sanity check */
353 if (!len) {
354 vq_err(vq, "Unexpected header len for RX: "
355 "%zd expected %zd\n",
356 iov_length(vq->hdr, s), hdr_size);
357 break;
358 }
359 err = sock->ops->recvmsg(NULL, sock, &msg,
360 len, MSG_DONTWAIT | MSG_TRUNC);
361 /* TODO: Check specific error and bomb out unless EAGAIN? */
362 if (err < 0) {
363 vhost_discard_vq_desc(vq, 1);
364 break;
365 }
366 /* TODO: Should check and handle checksum. */
367 if (err > len) {
368 pr_debug("Discarded truncated rx packet: "
369 " len %d > %zd\n", err, len);
370 vhost_discard_vq_desc(vq, 1);
371 continue;
372 }
373 len = err;
374 err = memcpy_toiovec(vq->hdr, (unsigned char *)&hdr, hdr_size);
375 if (err) {
376 vq_err(vq, "Unable to write vnet_hdr at addr %p: %d\n",
377 vq->iov->iov_base, err);
378 break;
379 }
380 len += hdr_size;
381 vhost_add_used_and_signal(&net->dev, vq, head, len);
382 if (unlikely(vq_log))
383 vhost_log_write(vq, vq_log, log, len);
384 total_len += len;
385 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
386 vhost_poll_queue(&vq->poll);
387 break;
388 }
389 }
390
391 mutex_unlock(&vq->mutex);
392 }
393
394 /* Expects to be always run from workqueue - which acts as
395 * read-size critical section for our kind of RCU. */
396 static void handle_rx_mergeable(struct vhost_net *net)
397 {
398 struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
399 unsigned uninitialized_var(in), log;
400 struct vhost_log *vq_log;
401 struct msghdr msg = {
402 .msg_name = NULL,
403 .msg_namelen = 0,
404 .msg_control = NULL, /* FIXME: get and handle RX aux data. */
405 .msg_controllen = 0,
406 .msg_iov = vq->iov,
407 .msg_flags = MSG_DONTWAIT,
408 };
409 struct virtio_net_hdr_mrg_rxbuf hdr = {
410 .hdr.flags = 0,
411 .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
412 };
413 size_t total_len = 0;
414 int err, headcount;
415 size_t vhost_hlen, sock_hlen;
416 size_t vhost_len, sock_len;
417 /* TODO: check that we are running from vhost_worker? */
418 struct socket *sock = rcu_dereference_check(vq->private_data, 1);
419
420 if (!sock || skb_queue_empty(&sock->sk->sk_receive_queue))
421 return;
422
423 mutex_lock(&vq->mutex);
424 vhost_disable_notify(vq);
425 vhost_hlen = vq->vhost_hlen;
426 sock_hlen = vq->sock_hlen;
427
428 vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
429 vq->log : NULL;
430
431 while ((sock_len = peek_head_len(sock->sk))) {
432 sock_len += sock_hlen;
433 vhost_len = sock_len + vhost_hlen;
434 headcount = get_rx_bufs(vq, vq->heads, vhost_len,
435 &in, vq_log, &log);
436 /* On error, stop handling until the next kick. */
437 if (unlikely(headcount < 0))
438 break;
439 /* OK, now we need to know about added descriptors. */
440 if (!headcount) {
441 if (unlikely(vhost_enable_notify(vq))) {
442 /* They have slipped one in as we were
443 * doing that: check again. */
444 vhost_disable_notify(vq);
445 continue;
446 }
447 /* Nothing new? Wait for eventfd to tell us
448 * they refilled. */
449 break;
450 }
451 /* We don't need to be notified again. */
452 if (unlikely((vhost_hlen)))
453 /* Skip header. TODO: support TSO. */
454 move_iovec_hdr(vq->iov, vq->hdr, vhost_hlen, in);
455 else
456 /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
457 * needed because recvmsg can modify msg_iov. */
458 copy_iovec_hdr(vq->iov, vq->hdr, sock_hlen, in);
459 msg.msg_iovlen = in;
460 err = sock->ops->recvmsg(NULL, sock, &msg,
461 sock_len, MSG_DONTWAIT | MSG_TRUNC);
462 /* Userspace might have consumed the packet meanwhile:
463 * it's not supposed to do this usually, but might be hard
464 * to prevent. Discard data we got (if any) and keep going. */
465 if (unlikely(err != sock_len)) {
466 pr_debug("Discarded rx packet: "
467 " len %d, expected %zd\n", err, sock_len);
468 vhost_discard_vq_desc(vq, headcount);
469 continue;
470 }
471 if (unlikely(vhost_hlen) &&
472 memcpy_toiovecend(vq->hdr, (unsigned char *)&hdr, 0,
473 vhost_hlen)) {
474 vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
475 vq->iov->iov_base);
476 break;
477 }
478 /* TODO: Should check and handle checksum. */
479 if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF) &&
480 memcpy_toiovecend(vq->hdr, (unsigned char *)&headcount,
481 offsetof(typeof(hdr), num_buffers),
482 sizeof hdr.num_buffers)) {
483 vq_err(vq, "Failed num_buffers write");
484 vhost_discard_vq_desc(vq, headcount);
485 break;
486 }
487 vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
488 headcount);
489 if (unlikely(vq_log))
490 vhost_log_write(vq, vq_log, log, vhost_len);
491 total_len += vhost_len;
492 if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
493 vhost_poll_queue(&vq->poll);
494 break;
495 }
496 }
497
498 mutex_unlock(&vq->mutex);
499 }
500
501 static void handle_rx(struct vhost_net *net)
502 {
503 if (vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF))
504 handle_rx_mergeable(net);
505 else
506 handle_rx_big(net);
507 }
508
509 static void handle_tx_kick(struct vhost_work *work)
510 {
511 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
512 poll.work);
513 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
514
515 handle_tx(net);
516 }
517
518 static void handle_rx_kick(struct vhost_work *work)
519 {
520 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
521 poll.work);
522 struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
523
524 handle_rx(net);
525 }
526
527 static void handle_tx_net(struct vhost_work *work)
528 {
529 struct vhost_net *net = container_of(work, struct vhost_net,
530 poll[VHOST_NET_VQ_TX].work);
531 handle_tx(net);
532 }
533
534 static void handle_rx_net(struct vhost_work *work)
535 {
536 struct vhost_net *net = container_of(work, struct vhost_net,
537 poll[VHOST_NET_VQ_RX].work);
538 handle_rx(net);
539 }
540
541 static int vhost_net_open(struct inode *inode, struct file *f)
542 {
543 struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
544 struct vhost_dev *dev;
545 int r;
546
547 if (!n)
548 return -ENOMEM;
549
550 dev = &n->dev;
551 n->vqs[VHOST_NET_VQ_TX].handle_kick = handle_tx_kick;
552 n->vqs[VHOST_NET_VQ_RX].handle_kick = handle_rx_kick;
553 r = vhost_dev_init(dev, n->vqs, VHOST_NET_VQ_MAX);
554 if (r < 0) {
555 kfree(n);
556 return r;
557 }
558
559 vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
560 vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
561 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
562
563 f->private_data = n;
564
565 return 0;
566 }
567
568 static void vhost_net_disable_vq(struct vhost_net *n,
569 struct vhost_virtqueue *vq)
570 {
571 if (!vq->private_data)
572 return;
573 if (vq == n->vqs + VHOST_NET_VQ_TX) {
574 tx_poll_stop(n);
575 n->tx_poll_state = VHOST_NET_POLL_DISABLED;
576 } else
577 vhost_poll_stop(n->poll + VHOST_NET_VQ_RX);
578 }
579
580 static void vhost_net_enable_vq(struct vhost_net *n,
581 struct vhost_virtqueue *vq)
582 {
583 struct socket *sock;
584
585 sock = rcu_dereference_protected(vq->private_data,
586 lockdep_is_held(&vq->mutex));
587 if (!sock)
588 return;
589 if (vq == n->vqs + VHOST_NET_VQ_TX) {
590 n->tx_poll_state = VHOST_NET_POLL_STOPPED;
591 tx_poll_start(n, sock);
592 } else
593 vhost_poll_start(n->poll + VHOST_NET_VQ_RX, sock->file);
594 }
595
596 static struct socket *vhost_net_stop_vq(struct vhost_net *n,
597 struct vhost_virtqueue *vq)
598 {
599 struct socket *sock;
600
601 mutex_lock(&vq->mutex);
602 sock = rcu_dereference_protected(vq->private_data,
603 lockdep_is_held(&vq->mutex));
604 vhost_net_disable_vq(n, vq);
605 rcu_assign_pointer(vq->private_data, NULL);
606 mutex_unlock(&vq->mutex);
607 return sock;
608 }
609
610 static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
611 struct socket **rx_sock)
612 {
613 *tx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_TX);
614 *rx_sock = vhost_net_stop_vq(n, n->vqs + VHOST_NET_VQ_RX);
615 }
616
617 static void vhost_net_flush_vq(struct vhost_net *n, int index)
618 {
619 vhost_poll_flush(n->poll + index);
620 vhost_poll_flush(&n->dev.vqs[index].poll);
621 }
622
623 static void vhost_net_flush(struct vhost_net *n)
624 {
625 vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
626 vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
627 }
628
629 static int vhost_net_release(struct inode *inode, struct file *f)
630 {
631 struct vhost_net *n = f->private_data;
632 struct socket *tx_sock;
633 struct socket *rx_sock;
634
635 vhost_net_stop(n, &tx_sock, &rx_sock);
636 vhost_net_flush(n);
637 vhost_dev_cleanup(&n->dev);
638 if (tx_sock)
639 fput(tx_sock->file);
640 if (rx_sock)
641 fput(rx_sock->file);
642 /* We do an extra flush before freeing memory,
643 * since jobs can re-queue themselves. */
644 vhost_net_flush(n);
645 kfree(n);
646 return 0;
647 }
648
649 static struct socket *get_raw_socket(int fd)
650 {
651 struct {
652 struct sockaddr_ll sa;
653 char buf[MAX_ADDR_LEN];
654 } uaddr;
655 int uaddr_len = sizeof uaddr, r;
656 struct socket *sock = sockfd_lookup(fd, &r);
657
658 if (!sock)
659 return ERR_PTR(-ENOTSOCK);
660
661 /* Parameter checking */
662 if (sock->sk->sk_type != SOCK_RAW) {
663 r = -ESOCKTNOSUPPORT;
664 goto err;
665 }
666
667 r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
668 &uaddr_len, 0);
669 if (r)
670 goto err;
671
672 if (uaddr.sa.sll_family != AF_PACKET) {
673 r = -EPFNOSUPPORT;
674 goto err;
675 }
676 return sock;
677 err:
678 fput(sock->file);
679 return ERR_PTR(r);
680 }
681
682 static struct socket *get_tap_socket(int fd)
683 {
684 struct file *file = fget(fd);
685 struct socket *sock;
686
687 if (!file)
688 return ERR_PTR(-EBADF);
689 sock = tun_get_socket(file);
690 if (!IS_ERR(sock))
691 return sock;
692 sock = macvtap_get_socket(file);
693 if (IS_ERR(sock))
694 fput(file);
695 return sock;
696 }
697
698 static struct socket *get_socket(int fd)
699 {
700 struct socket *sock;
701
702 /* special case to disable backend */
703 if (fd == -1)
704 return NULL;
705 sock = get_raw_socket(fd);
706 if (!IS_ERR(sock))
707 return sock;
708 sock = get_tap_socket(fd);
709 if (!IS_ERR(sock))
710 return sock;
711 return ERR_PTR(-ENOTSOCK);
712 }
713
714 static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
715 {
716 struct socket *sock, *oldsock;
717 struct vhost_virtqueue *vq;
718 int r;
719
720 mutex_lock(&n->dev.mutex);
721 r = vhost_dev_check_owner(&n->dev);
722 if (r)
723 goto err;
724
725 if (index >= VHOST_NET_VQ_MAX) {
726 r = -ENOBUFS;
727 goto err;
728 }
729 vq = n->vqs + index;
730 mutex_lock(&vq->mutex);
731
732 /* Verify that ring has been setup correctly. */
733 if (!vhost_vq_access_ok(vq)) {
734 r = -EFAULT;
735 goto err_vq;
736 }
737 sock = get_socket(fd);
738 if (IS_ERR(sock)) {
739 r = PTR_ERR(sock);
740 goto err_vq;
741 }
742
743 /* start polling new socket */
744 oldsock = rcu_dereference_protected(vq->private_data,
745 lockdep_is_held(&vq->mutex));
746 if (sock != oldsock) {
747 vhost_net_disable_vq(n, vq);
748 rcu_assign_pointer(vq->private_data, sock);
749 vhost_net_enable_vq(n, vq);
750 }
751
752 mutex_unlock(&vq->mutex);
753
754 if (oldsock) {
755 vhost_net_flush_vq(n, index);
756 fput(oldsock->file);
757 }
758
759 mutex_unlock(&n->dev.mutex);
760 return 0;
761
762 err_vq:
763 mutex_unlock(&vq->mutex);
764 err:
765 mutex_unlock(&n->dev.mutex);
766 return r;
767 }
768
769 static long vhost_net_reset_owner(struct vhost_net *n)
770 {
771 struct socket *tx_sock = NULL;
772 struct socket *rx_sock = NULL;
773 long err;
774
775 mutex_lock(&n->dev.mutex);
776 err = vhost_dev_check_owner(&n->dev);
777 if (err)
778 goto done;
779 vhost_net_stop(n, &tx_sock, &rx_sock);
780 vhost_net_flush(n);
781 err = vhost_dev_reset_owner(&n->dev);
782 done:
783 mutex_unlock(&n->dev.mutex);
784 if (tx_sock)
785 fput(tx_sock->file);
786 if (rx_sock)
787 fput(rx_sock->file);
788 return err;
789 }
790
791 static int vhost_net_set_features(struct vhost_net *n, u64 features)
792 {
793 size_t vhost_hlen, sock_hlen, hdr_len;
794 int i;
795
796 hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
797 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
798 sizeof(struct virtio_net_hdr);
799 if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
800 /* vhost provides vnet_hdr */
801 vhost_hlen = hdr_len;
802 sock_hlen = 0;
803 } else {
804 /* socket provides vnet_hdr */
805 vhost_hlen = 0;
806 sock_hlen = hdr_len;
807 }
808 mutex_lock(&n->dev.mutex);
809 if ((features & (1 << VHOST_F_LOG_ALL)) &&
810 !vhost_log_access_ok(&n->dev)) {
811 mutex_unlock(&n->dev.mutex);
812 return -EFAULT;
813 }
814 n->dev.acked_features = features;
815 smp_wmb();
816 for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
817 mutex_lock(&n->vqs[i].mutex);
818 n->vqs[i].vhost_hlen = vhost_hlen;
819 n->vqs[i].sock_hlen = sock_hlen;
820 mutex_unlock(&n->vqs[i].mutex);
821 }
822 vhost_net_flush(n);
823 mutex_unlock(&n->dev.mutex);
824 return 0;
825 }
826
827 static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
828 unsigned long arg)
829 {
830 struct vhost_net *n = f->private_data;
831 void __user *argp = (void __user *)arg;
832 u64 __user *featurep = argp;
833 struct vhost_vring_file backend;
834 u64 features;
835 int r;
836
837 switch (ioctl) {
838 case VHOST_NET_SET_BACKEND:
839 if (copy_from_user(&backend, argp, sizeof backend))
840 return -EFAULT;
841 return vhost_net_set_backend(n, backend.index, backend.fd);
842 case VHOST_GET_FEATURES:
843 features = VHOST_FEATURES;
844 if (copy_to_user(featurep, &features, sizeof features))
845 return -EFAULT;
846 return 0;
847 case VHOST_SET_FEATURES:
848 if (copy_from_user(&features, featurep, sizeof features))
849 return -EFAULT;
850 if (features & ~VHOST_FEATURES)
851 return -EOPNOTSUPP;
852 return vhost_net_set_features(n, features);
853 case VHOST_RESET_OWNER:
854 return vhost_net_reset_owner(n);
855 default:
856 mutex_lock(&n->dev.mutex);
857 r = vhost_dev_ioctl(&n->dev, ioctl, arg);
858 vhost_net_flush(n);
859 mutex_unlock(&n->dev.mutex);
860 return r;
861 }
862 }
863
864 #ifdef CONFIG_COMPAT
865 static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
866 unsigned long arg)
867 {
868 return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
869 }
870 #endif
871
872 static const struct file_operations vhost_net_fops = {
873 .owner = THIS_MODULE,
874 .release = vhost_net_release,
875 .unlocked_ioctl = vhost_net_ioctl,
876 #ifdef CONFIG_COMPAT
877 .compat_ioctl = vhost_net_compat_ioctl,
878 #endif
879 .open = vhost_net_open,
880 .llseek = noop_llseek,
881 };
882
883 static struct miscdevice vhost_net_misc = {
884 MISC_DYNAMIC_MINOR,
885 "vhost-net",
886 &vhost_net_fops,
887 };
888
889 static int vhost_net_init(void)
890 {
891 return misc_register(&vhost_net_misc);
892 }
893 module_init(vhost_net_init);
894
895 static void vhost_net_exit(void)
896 {
897 misc_deregister(&vhost_net_misc);
898 }
899 module_exit(vhost_net_exit);
900
901 MODULE_VERSION("0.0.1");
902 MODULE_LICENSE("GPL v2");
903 MODULE_AUTHOR("Michael S. Tsirkin");
904 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
This page took 0.049275 seconds and 5 git commands to generate.