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/miscdevice.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/mutex.h>
17 #include <linux/workqueue.h>
18 #include <linux/file.h>
19 #include <linux/slab.h>
20 #include <linux/vmalloc.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>
27 #include <linux/if_vlan.h>
33 static int experimental_zcopytx
= 0;
34 module_param(experimental_zcopytx
, int, 0444);
35 MODULE_PARM_DESC(experimental_zcopytx
, "Enable Zero Copy TX;"
36 " 1 -Enable; 0 - Disable");
38 /* Max number of bytes transferred before requeueing the job.
39 * Using this limit prevents one virtqueue from starving others. */
40 #define VHOST_NET_WEIGHT 0x80000
42 /* Max number of packets transferred before requeueing the job.
43 * Using this limit prevents one virtqueue from starving others with small
46 #define VHOST_NET_PKT_WEIGHT 256
48 /* MAX number of TX used buffers for outstanding zerocopy */
49 #define VHOST_MAX_PEND 128
50 #define VHOST_GOODCOPY_LEN 256
53 * For transmit, used buffer len is unused; we override it to track buffer
54 * status internally; used for zerocopy tx only.
56 /* Lower device DMA failed */
57 #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
58 /* Lower device DMA done */
59 #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
60 /* Lower device DMA in progress */
61 #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
63 #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
65 #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
68 VHOST_NET_FEATURES
= VHOST_FEATURES
|
69 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
) |
70 (1ULL << VIRTIO_NET_F_MRG_RXBUF
)
79 struct vhost_net_ubuf_ref
{
80 /* refcount follows semantics similar to kref:
81 * 0: object is released
82 * 1: no outstanding ubufs
83 * >1: outstanding ubufs
86 wait_queue_head_t wait
;
87 struct vhost_virtqueue
*vq
;
90 struct vhost_net_virtqueue
{
91 struct vhost_virtqueue vq
;
94 /* vhost zerocopy support fields below: */
95 /* last used idx for outstanding DMA zerocopy buffers */
97 /* first used idx for DMA done zerocopy buffers */
99 /* an array of userspace buffers info */
100 struct ubuf_info
*ubuf_info
;
101 /* Reference counting for outstanding ubufs.
102 * Protected by vq mutex. Writers must also take device mutex. */
103 struct vhost_net_ubuf_ref
*ubufs
;
107 struct vhost_dev dev
;
108 struct vhost_net_virtqueue vqs
[VHOST_NET_VQ_MAX
];
109 struct vhost_poll poll
[VHOST_NET_VQ_MAX
];
110 /* Number of TX recently submitted.
111 * Protected by tx vq lock. */
113 /* Number of times zerocopy TX recently failed.
114 * Protected by tx vq lock. */
115 unsigned tx_zcopy_err
;
116 /* Flush in progress. Protected by tx vq lock. */
120 static unsigned vhost_net_zcopy_mask __read_mostly
;
122 static void vhost_net_enable_zcopy(int vq
)
124 vhost_net_zcopy_mask
|= 0x1 << vq
;
127 static struct vhost_net_ubuf_ref
*
128 vhost_net_ubuf_alloc(struct vhost_virtqueue
*vq
, bool zcopy
)
130 struct vhost_net_ubuf_ref
*ubufs
;
131 /* No zero copy backend? Nothing to count. */
134 ubufs
= kmalloc(sizeof(*ubufs
), GFP_KERNEL
);
136 return ERR_PTR(-ENOMEM
);
137 atomic_set(&ubufs
->refcount
, 1);
138 init_waitqueue_head(&ubufs
->wait
);
143 static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref
*ubufs
)
145 int r
= atomic_sub_return(1, &ubufs
->refcount
);
147 wake_up(&ubufs
->wait
);
151 static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref
*ubufs
)
153 vhost_net_ubuf_put(ubufs
);
154 wait_event(ubufs
->wait
, !atomic_read(&ubufs
->refcount
));
157 static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref
*ubufs
)
159 vhost_net_ubuf_put_and_wait(ubufs
);
163 static void vhost_net_clear_ubuf_info(struct vhost_net
*n
)
167 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
168 kfree(n
->vqs
[i
].ubuf_info
);
169 n
->vqs
[i
].ubuf_info
= NULL
;
173 static int vhost_net_set_ubuf_info(struct vhost_net
*n
)
178 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
179 zcopy
= vhost_net_zcopy_mask
& (0x1 << i
);
182 n
->vqs
[i
].ubuf_info
= kmalloc(sizeof(*n
->vqs
[i
].ubuf_info
) *
183 UIO_MAXIOV
, GFP_KERNEL
);
184 if (!n
->vqs
[i
].ubuf_info
)
190 vhost_net_clear_ubuf_info(n
);
194 static void vhost_net_vq_reset(struct vhost_net
*n
)
198 vhost_net_clear_ubuf_info(n
);
200 for (i
= 0; i
< VHOST_NET_VQ_MAX
; i
++) {
201 n
->vqs
[i
].done_idx
= 0;
202 n
->vqs
[i
].upend_idx
= 0;
203 n
->vqs
[i
].ubufs
= NULL
;
204 n
->vqs
[i
].vhost_hlen
= 0;
205 n
->vqs
[i
].sock_hlen
= 0;
210 static void vhost_net_tx_packet(struct vhost_net
*net
)
213 if (net
->tx_packets
< 1024)
216 net
->tx_zcopy_err
= 0;
219 static void vhost_net_tx_err(struct vhost_net
*net
)
224 static bool vhost_net_tx_select_zcopy(struct vhost_net
*net
)
226 /* TX flush waits for outstanding DMAs to be done.
227 * Don't start new DMAs.
229 return !net
->tx_flush
&&
230 net
->tx_packets
/ 64 >= net
->tx_zcopy_err
;
233 static bool vhost_sock_zcopy(struct socket
*sock
)
235 return unlikely(experimental_zcopytx
) &&
236 sock_flag(sock
->sk
, SOCK_ZEROCOPY
);
239 /* In case of DMA done not in order in lower device driver for some reason.
240 * upend_idx is used to track end of used idx, done_idx is used to track head
241 * of used idx. Once lower device DMA done contiguously, we will signal KVM
244 static void vhost_zerocopy_signal_used(struct vhost_net
*net
,
245 struct vhost_virtqueue
*vq
)
247 struct vhost_net_virtqueue
*nvq
=
248 container_of(vq
, struct vhost_net_virtqueue
, vq
);
252 for (i
= nvq
->done_idx
; i
!= nvq
->upend_idx
; i
= (i
+ 1) % UIO_MAXIOV
) {
253 if (vq
->heads
[i
].len
== VHOST_DMA_FAILED_LEN
)
254 vhost_net_tx_err(net
);
255 if (VHOST_DMA_IS_DONE(vq
->heads
[i
].len
)) {
256 vq
->heads
[i
].len
= VHOST_DMA_CLEAR_LEN
;
262 add
= min(UIO_MAXIOV
- nvq
->done_idx
, j
);
263 vhost_add_used_and_signal_n(vq
->dev
, vq
,
264 &vq
->heads
[nvq
->done_idx
], add
);
265 nvq
->done_idx
= (nvq
->done_idx
+ add
) % UIO_MAXIOV
;
270 static void vhost_zerocopy_callback(struct ubuf_info
*ubuf
, bool success
)
272 struct vhost_net_ubuf_ref
*ubufs
= ubuf
->ctx
;
273 struct vhost_virtqueue
*vq
= ubufs
->vq
;
278 /* set len to mark this desc buffers done DMA */
279 vq
->heads
[ubuf
->desc
].len
= success
?
280 VHOST_DMA_DONE_LEN
: VHOST_DMA_FAILED_LEN
;
281 cnt
= vhost_net_ubuf_put(ubufs
);
284 * Trigger polling thread if guest stopped submitting new buffers:
285 * in this case, the refcount after decrement will eventually reach 1.
286 * We also trigger polling periodically after each 16 packets
287 * (the value 16 here is more or less arbitrary, it's tuned to trigger
288 * less than 10% of times).
290 if (cnt
<= 1 || !(cnt
% 16))
291 vhost_poll_queue(&vq
->poll
);
293 rcu_read_unlock_bh();
296 /* Expects to be always run from workqueue - which acts as
297 * read-size critical section for our kind of RCU. */
298 static void handle_tx(struct vhost_net
*net
)
300 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_TX
];
301 struct vhost_virtqueue
*vq
= &nvq
->vq
;
304 struct msghdr msg
= {
309 .msg_flags
= MSG_DONTWAIT
,
311 size_t len
, total_len
= 0;
315 struct vhost_net_ubuf_ref
*uninitialized_var(ubufs
);
316 bool zcopy
, zcopy_used
;
319 mutex_lock(&vq
->mutex
);
320 sock
= vq
->private_data
;
324 vhost_disable_notify(&net
->dev
, vq
);
326 hdr_size
= nvq
->vhost_hlen
;
330 /* Release DMAs done buffers first */
332 vhost_zerocopy_signal_used(net
, vq
);
334 /* If more outstanding DMAs, queue the work.
335 * Handle upend_idx wrap around
337 if (unlikely((nvq
->upend_idx
+ vq
->num
- VHOST_MAX_PEND
)
338 % UIO_MAXIOV
== nvq
->done_idx
))
341 head
= vhost_get_vq_desc(vq
, vq
->iov
,
345 /* On error, stop handling until the next kick. */
346 if (unlikely(head
< 0))
348 /* Nothing new? Wait for eventfd to tell us they refilled. */
349 if (head
== vq
->num
) {
350 if (unlikely(vhost_enable_notify(&net
->dev
, vq
))) {
351 vhost_disable_notify(&net
->dev
, vq
);
357 vq_err(vq
, "Unexpected descriptor format for TX: "
358 "out %d, int %d\n", out
, in
);
361 /* Skip header. TODO: support TSO. */
362 len
= iov_length(vq
->iov
, out
);
363 iov_iter_init(&msg
.msg_iter
, WRITE
, vq
->iov
, out
, len
);
364 iov_iter_advance(&msg
.msg_iter
, hdr_size
);
366 if (!msg_data_left(&msg
)) {
367 vq_err(vq
, "Unexpected header len for TX: "
368 "%zd expected %zd\n",
372 len
= msg_data_left(&msg
);
374 zcopy_used
= zcopy
&& len
>= VHOST_GOODCOPY_LEN
375 && (nvq
->upend_idx
+ 1) % UIO_MAXIOV
!=
377 && vhost_net_tx_select_zcopy(net
);
379 /* use msg_control to pass vhost zerocopy ubuf info to skb */
381 struct ubuf_info
*ubuf
;
382 ubuf
= nvq
->ubuf_info
+ nvq
->upend_idx
;
384 vq
->heads
[nvq
->upend_idx
].id
= cpu_to_vhost32(vq
, head
);
385 vq
->heads
[nvq
->upend_idx
].len
= VHOST_DMA_IN_PROGRESS
;
386 ubuf
->callback
= vhost_zerocopy_callback
;
387 ubuf
->ctx
= nvq
->ubufs
;
388 ubuf
->desc
= nvq
->upend_idx
;
389 msg
.msg_control
= ubuf
;
390 msg
.msg_controllen
= sizeof(ubuf
);
392 atomic_inc(&ubufs
->refcount
);
393 nvq
->upend_idx
= (nvq
->upend_idx
+ 1) % UIO_MAXIOV
;
395 msg
.msg_control
= NULL
;
398 /* TODO: Check specific error and bomb out unless ENOBUFS? */
399 err
= sock
->ops
->sendmsg(sock
, &msg
, len
);
400 if (unlikely(err
< 0)) {
402 vhost_net_ubuf_put(ubufs
);
403 nvq
->upend_idx
= ((unsigned)nvq
->upend_idx
- 1)
406 vhost_discard_vq_desc(vq
, 1);
410 pr_debug("Truncated TX packet: "
411 " len %d != %zd\n", err
, len
);
413 vhost_add_used_and_signal(&net
->dev
, vq
, head
, 0);
415 vhost_zerocopy_signal_used(net
, vq
);
417 vhost_net_tx_packet(net
);
418 } while (likely(!vhost_exceeds_weight(vq
, ++sent_pkts
, total_len
)));
420 mutex_unlock(&vq
->mutex
);
423 static int peek_head_len(struct sock
*sk
)
425 struct sk_buff
*head
;
429 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, flags
);
430 head
= skb_peek(&sk
->sk_receive_queue
);
433 if (skb_vlan_tag_present(head
))
437 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, flags
);
441 /* This is a multi-buffer version of vhost_get_desc, that works if
442 * vq has read descriptors only.
443 * @vq - the relevant virtqueue
444 * @datalen - data length we'll be reading
445 * @iovcount - returned count of io vectors we fill
447 * @log_num - log offset
448 * @quota - headcount quota, 1 for big buffer
449 * returns number of buffer heads allocated, negative on error
451 static int get_rx_bufs(struct vhost_virtqueue
*vq
,
452 struct vring_used_elem
*heads
,
455 struct vhost_log
*log
,
459 unsigned int out
, in
;
464 /* len is always initialized before use since we are always called with
467 u32
uninitialized_var(len
);
469 while (datalen
> 0 && headcount
< quota
) {
470 if (unlikely(seg
>= UIO_MAXIOV
)) {
474 r
= vhost_get_vq_desc(vq
, vq
->iov
+ seg
,
475 ARRAY_SIZE(vq
->iov
) - seg
, &out
,
485 if (unlikely(out
|| in
<= 0)) {
486 vq_err(vq
, "unexpected descriptor format for RX: "
487 "out %d, in %d\n", out
, in
);
495 heads
[headcount
].id
= cpu_to_vhost32(vq
, d
);
496 len
= iov_length(vq
->iov
+ seg
, in
);
497 heads
[headcount
].len
= cpu_to_vhost32(vq
, len
);
502 heads
[headcount
- 1].len
= cpu_to_vhost32(vq
, len
+ datalen
);
508 if (unlikely(datalen
> 0)) {
514 vhost_discard_vq_desc(vq
, headcount
);
518 /* Expects to be always run from workqueue - which acts as
519 * read-size critical section for our kind of RCU. */
520 static void handle_rx(struct vhost_net
*net
)
522 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_RX
];
523 struct vhost_virtqueue
*vq
= &nvq
->vq
;
524 unsigned uninitialized_var(in
), log
;
525 struct vhost_log
*vq_log
;
526 struct msghdr msg
= {
529 .msg_control
= NULL
, /* FIXME: get and handle RX aux data. */
531 .msg_flags
= MSG_DONTWAIT
,
533 struct virtio_net_hdr hdr
= {
535 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
537 size_t total_len
= 0;
540 size_t vhost_hlen
, sock_hlen
;
541 size_t vhost_len
, sock_len
;
543 struct iov_iter fixup
;
544 __virtio16 num_buffers
;
547 mutex_lock(&vq
->mutex
);
548 sock
= vq
->private_data
;
551 vhost_disable_notify(&net
->dev
, vq
);
553 vhost_hlen
= nvq
->vhost_hlen
;
554 sock_hlen
= nvq
->sock_hlen
;
556 vq_log
= unlikely(vhost_has_feature(vq
, VHOST_F_LOG_ALL
)) ?
558 mergeable
= vhost_has_feature(vq
, VIRTIO_NET_F_MRG_RXBUF
);
561 sock_len
= peek_head_len(sock
->sk
);
564 sock_len
+= sock_hlen
;
565 vhost_len
= sock_len
+ vhost_hlen
;
566 headcount
= get_rx_bufs(vq
, vq
->heads
, vhost_len
,
568 likely(mergeable
) ? UIO_MAXIOV
: 1);
569 /* On error, stop handling until the next kick. */
570 if (unlikely(headcount
< 0))
572 /* On overrun, truncate and discard */
573 if (unlikely(headcount
> UIO_MAXIOV
)) {
574 iov_iter_init(&msg
.msg_iter
, READ
, vq
->iov
, 1, 1);
575 err
= sock
->ops
->recvmsg(sock
, &msg
,
576 1, MSG_DONTWAIT
| MSG_TRUNC
);
577 pr_debug("Discarded rx packet: len %zd\n", sock_len
);
580 /* OK, now we need to know about added descriptors. */
582 if (unlikely(vhost_enable_notify(&net
->dev
, vq
))) {
583 /* They have slipped one in as we were
584 * doing that: check again. */
585 vhost_disable_notify(&net
->dev
, vq
);
588 /* Nothing new? Wait for eventfd to tell us
592 /* We don't need to be notified again. */
593 iov_iter_init(&msg
.msg_iter
, READ
, vq
->iov
, in
, vhost_len
);
594 fixup
= msg
.msg_iter
;
595 if (unlikely((vhost_hlen
))) {
596 /* We will supply the header ourselves
599 iov_iter_advance(&msg
.msg_iter
, vhost_hlen
);
601 err
= sock
->ops
->recvmsg(sock
, &msg
,
602 sock_len
, MSG_DONTWAIT
| MSG_TRUNC
);
603 /* Userspace might have consumed the packet meanwhile:
604 * it's not supposed to do this usually, but might be hard
605 * to prevent. Discard data we got (if any) and keep going. */
606 if (unlikely(err
!= sock_len
)) {
607 pr_debug("Discarded rx packet: "
608 " len %d, expected %zd\n", err
, sock_len
);
609 vhost_discard_vq_desc(vq
, headcount
);
612 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
613 if (unlikely(vhost_hlen
)) {
614 if (copy_to_iter(&hdr
, sizeof(hdr
),
615 &fixup
) != sizeof(hdr
)) {
616 vq_err(vq
, "Unable to write vnet_hdr "
617 "at addr %p\n", vq
->iov
->iov_base
);
621 /* Header came from socket; we'll need to patch
622 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
624 iov_iter_advance(&fixup
, sizeof(hdr
));
626 /* TODO: Should check and handle checksum. */
628 num_buffers
= cpu_to_vhost16(vq
, headcount
);
629 if (likely(mergeable
) &&
630 copy_to_iter(&num_buffers
, sizeof num_buffers
,
631 &fixup
) != sizeof num_buffers
) {
632 vq_err(vq
, "Failed num_buffers write");
633 vhost_discard_vq_desc(vq
, headcount
);
636 vhost_add_used_and_signal_n(&net
->dev
, vq
, vq
->heads
,
638 if (unlikely(vq_log
))
639 vhost_log_write(vq
, vq_log
, log
, vhost_len
);
640 total_len
+= vhost_len
;
641 } while (likely(!vhost_exceeds_weight(vq
, ++recv_pkts
, total_len
)));
644 mutex_unlock(&vq
->mutex
);
647 static void handle_tx_kick(struct vhost_work
*work
)
649 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
651 struct vhost_net
*net
= container_of(vq
->dev
, struct vhost_net
, dev
);
656 static void handle_rx_kick(struct vhost_work
*work
)
658 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
660 struct vhost_net
*net
= container_of(vq
->dev
, struct vhost_net
, dev
);
665 static void handle_tx_net(struct vhost_work
*work
)
667 struct vhost_net
*net
= container_of(work
, struct vhost_net
,
668 poll
[VHOST_NET_VQ_TX
].work
);
672 static void handle_rx_net(struct vhost_work
*work
)
674 struct vhost_net
*net
= container_of(work
, struct vhost_net
,
675 poll
[VHOST_NET_VQ_RX
].work
);
679 static int vhost_net_open(struct inode
*inode
, struct file
*f
)
682 struct vhost_dev
*dev
;
683 struct vhost_virtqueue
**vqs
;
686 n
= kmalloc(sizeof *n
, GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
688 n
= vmalloc(sizeof *n
);
692 vqs
= kmalloc(VHOST_NET_VQ_MAX
* sizeof(*vqs
), GFP_KERNEL
);
699 vqs
[VHOST_NET_VQ_TX
] = &n
->vqs
[VHOST_NET_VQ_TX
].vq
;
700 vqs
[VHOST_NET_VQ_RX
] = &n
->vqs
[VHOST_NET_VQ_RX
].vq
;
701 n
->vqs
[VHOST_NET_VQ_TX
].vq
.handle_kick
= handle_tx_kick
;
702 n
->vqs
[VHOST_NET_VQ_RX
].vq
.handle_kick
= handle_rx_kick
;
703 for (i
= 0; i
< VHOST_NET_VQ_MAX
; i
++) {
704 n
->vqs
[i
].ubufs
= NULL
;
705 n
->vqs
[i
].ubuf_info
= NULL
;
706 n
->vqs
[i
].upend_idx
= 0;
707 n
->vqs
[i
].done_idx
= 0;
708 n
->vqs
[i
].vhost_hlen
= 0;
709 n
->vqs
[i
].sock_hlen
= 0;
711 vhost_dev_init(dev
, vqs
, VHOST_NET_VQ_MAX
,
712 VHOST_NET_PKT_WEIGHT
, VHOST_NET_WEIGHT
);
714 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_TX
, handle_tx_net
, POLLOUT
, dev
);
715 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_RX
, handle_rx_net
, POLLIN
, dev
);
722 static void vhost_net_disable_vq(struct vhost_net
*n
,
723 struct vhost_virtqueue
*vq
)
725 struct vhost_net_virtqueue
*nvq
=
726 container_of(vq
, struct vhost_net_virtqueue
, vq
);
727 struct vhost_poll
*poll
= n
->poll
+ (nvq
- n
->vqs
);
728 if (!vq
->private_data
)
730 vhost_poll_stop(poll
);
733 static int vhost_net_enable_vq(struct vhost_net
*n
,
734 struct vhost_virtqueue
*vq
)
736 struct vhost_net_virtqueue
*nvq
=
737 container_of(vq
, struct vhost_net_virtqueue
, vq
);
738 struct vhost_poll
*poll
= n
->poll
+ (nvq
- n
->vqs
);
741 sock
= vq
->private_data
;
745 return vhost_poll_start(poll
, sock
->file
);
748 static struct socket
*vhost_net_stop_vq(struct vhost_net
*n
,
749 struct vhost_virtqueue
*vq
)
753 mutex_lock(&vq
->mutex
);
754 sock
= vq
->private_data
;
755 vhost_net_disable_vq(n
, vq
);
756 vq
->private_data
= NULL
;
757 mutex_unlock(&vq
->mutex
);
761 static void vhost_net_stop(struct vhost_net
*n
, struct socket
**tx_sock
,
762 struct socket
**rx_sock
)
764 *tx_sock
= vhost_net_stop_vq(n
, &n
->vqs
[VHOST_NET_VQ_TX
].vq
);
765 *rx_sock
= vhost_net_stop_vq(n
, &n
->vqs
[VHOST_NET_VQ_RX
].vq
);
768 static void vhost_net_flush_vq(struct vhost_net
*n
, int index
)
770 vhost_poll_flush(n
->poll
+ index
);
771 vhost_poll_flush(&n
->vqs
[index
].vq
.poll
);
774 static void vhost_net_flush(struct vhost_net
*n
)
776 vhost_net_flush_vq(n
, VHOST_NET_VQ_TX
);
777 vhost_net_flush_vq(n
, VHOST_NET_VQ_RX
);
778 if (n
->vqs
[VHOST_NET_VQ_TX
].ubufs
) {
779 mutex_lock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
781 mutex_unlock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
782 /* Wait for all lower device DMAs done. */
783 vhost_net_ubuf_put_and_wait(n
->vqs
[VHOST_NET_VQ_TX
].ubufs
);
784 mutex_lock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
786 atomic_set(&n
->vqs
[VHOST_NET_VQ_TX
].ubufs
->refcount
, 1);
787 mutex_unlock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
791 static int vhost_net_release(struct inode
*inode
, struct file
*f
)
793 struct vhost_net
*n
= f
->private_data
;
794 struct socket
*tx_sock
;
795 struct socket
*rx_sock
;
797 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
799 vhost_dev_stop(&n
->dev
);
800 vhost_dev_cleanup(&n
->dev
, false);
801 vhost_net_vq_reset(n
);
806 /* Make sure no callbacks are outstanding */
807 synchronize_rcu_bh();
808 /* We do an extra flush before freeing memory,
809 * since jobs can re-queue themselves. */
816 static struct socket
*get_raw_socket(int fd
)
819 struct socket
*sock
= sockfd_lookup(fd
, &r
);
822 return ERR_PTR(-ENOTSOCK
);
824 /* Parameter checking */
825 if (sock
->sk
->sk_type
!= SOCK_RAW
) {
826 r
= -ESOCKTNOSUPPORT
;
830 if (sock
->sk
->sk_family
!= AF_PACKET
) {
840 static struct socket
*get_tap_socket(int fd
)
842 struct file
*file
= fget(fd
);
846 return ERR_PTR(-EBADF
);
847 sock
= tun_get_socket(file
);
850 sock
= macvtap_get_socket(file
);
856 static struct socket
*get_socket(int fd
)
860 /* special case to disable backend */
863 sock
= get_raw_socket(fd
);
866 sock
= get_tap_socket(fd
);
869 return ERR_PTR(-ENOTSOCK
);
872 static long vhost_net_set_backend(struct vhost_net
*n
, unsigned index
, int fd
)
874 struct socket
*sock
, *oldsock
;
875 struct vhost_virtqueue
*vq
;
876 struct vhost_net_virtqueue
*nvq
;
877 struct vhost_net_ubuf_ref
*ubufs
, *oldubufs
= NULL
;
880 mutex_lock(&n
->dev
.mutex
);
881 r
= vhost_dev_check_owner(&n
->dev
);
885 if (index
>= VHOST_NET_VQ_MAX
) {
889 vq
= &n
->vqs
[index
].vq
;
890 nvq
= &n
->vqs
[index
];
891 mutex_lock(&vq
->mutex
);
893 /* Verify that ring has been setup correctly. */
894 if (!vhost_vq_access_ok(vq
)) {
898 sock
= get_socket(fd
);
904 /* start polling new socket */
905 oldsock
= vq
->private_data
;
906 if (sock
!= oldsock
) {
907 ubufs
= vhost_net_ubuf_alloc(vq
,
908 sock
&& vhost_sock_zcopy(sock
));
914 vhost_net_disable_vq(n
, vq
);
915 vq
->private_data
= sock
;
916 r
= vhost_init_used(vq
);
919 r
= vhost_net_enable_vq(n
, vq
);
923 oldubufs
= nvq
->ubufs
;
931 mutex_unlock(&vq
->mutex
);
934 vhost_net_ubuf_put_wait_and_free(oldubufs
);
935 mutex_lock(&vq
->mutex
);
936 vhost_zerocopy_signal_used(n
, vq
);
937 mutex_unlock(&vq
->mutex
);
941 vhost_net_flush_vq(n
, index
);
945 mutex_unlock(&n
->dev
.mutex
);
949 vq
->private_data
= oldsock
;
950 vhost_net_enable_vq(n
, vq
);
952 vhost_net_ubuf_put_wait_and_free(ubufs
);
957 mutex_unlock(&vq
->mutex
);
959 mutex_unlock(&n
->dev
.mutex
);
963 static long vhost_net_reset_owner(struct vhost_net
*n
)
965 struct socket
*tx_sock
= NULL
;
966 struct socket
*rx_sock
= NULL
;
968 struct vhost_memory
*memory
;
970 mutex_lock(&n
->dev
.mutex
);
971 err
= vhost_dev_check_owner(&n
->dev
);
974 memory
= vhost_dev_reset_owner_prepare();
979 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
981 vhost_dev_stop(&n
->dev
);
982 vhost_dev_reset_owner(&n
->dev
, memory
);
983 vhost_net_vq_reset(n
);
985 mutex_unlock(&n
->dev
.mutex
);
993 static int vhost_net_set_features(struct vhost_net
*n
, u64 features
)
995 size_t vhost_hlen
, sock_hlen
, hdr_len
;
998 hdr_len
= (features
& ((1ULL << VIRTIO_NET_F_MRG_RXBUF
) |
999 (1ULL << VIRTIO_F_VERSION_1
))) ?
1000 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
1001 sizeof(struct virtio_net_hdr
);
1002 if (features
& (1 << VHOST_NET_F_VIRTIO_NET_HDR
)) {
1003 /* vhost provides vnet_hdr */
1004 vhost_hlen
= hdr_len
;
1007 /* socket provides vnet_hdr */
1009 sock_hlen
= hdr_len
;
1011 mutex_lock(&n
->dev
.mutex
);
1012 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1013 !vhost_log_access_ok(&n
->dev
)) {
1014 mutex_unlock(&n
->dev
.mutex
);
1017 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
1018 mutex_lock(&n
->vqs
[i
].vq
.mutex
);
1019 n
->vqs
[i
].vq
.acked_features
= features
;
1020 n
->vqs
[i
].vhost_hlen
= vhost_hlen
;
1021 n
->vqs
[i
].sock_hlen
= sock_hlen
;
1022 mutex_unlock(&n
->vqs
[i
].vq
.mutex
);
1024 mutex_unlock(&n
->dev
.mutex
);
1028 static long vhost_net_set_owner(struct vhost_net
*n
)
1032 mutex_lock(&n
->dev
.mutex
);
1033 if (vhost_dev_has_owner(&n
->dev
)) {
1037 r
= vhost_net_set_ubuf_info(n
);
1040 r
= vhost_dev_set_owner(&n
->dev
);
1042 vhost_net_clear_ubuf_info(n
);
1045 mutex_unlock(&n
->dev
.mutex
);
1049 static long vhost_net_ioctl(struct file
*f
, unsigned int ioctl
,
1052 struct vhost_net
*n
= f
->private_data
;
1053 void __user
*argp
= (void __user
*)arg
;
1054 u64 __user
*featurep
= argp
;
1055 struct vhost_vring_file backend
;
1060 case VHOST_NET_SET_BACKEND
:
1061 if (copy_from_user(&backend
, argp
, sizeof backend
))
1063 return vhost_net_set_backend(n
, backend
.index
, backend
.fd
);
1064 case VHOST_GET_FEATURES
:
1065 features
= VHOST_NET_FEATURES
;
1066 if (copy_to_user(featurep
, &features
, sizeof features
))
1069 case VHOST_SET_FEATURES
:
1070 if (copy_from_user(&features
, featurep
, sizeof features
))
1072 if (features
& ~VHOST_NET_FEATURES
)
1074 return vhost_net_set_features(n
, features
);
1075 case VHOST_RESET_OWNER
:
1076 return vhost_net_reset_owner(n
);
1077 case VHOST_SET_OWNER
:
1078 return vhost_net_set_owner(n
);
1080 mutex_lock(&n
->dev
.mutex
);
1081 r
= vhost_dev_ioctl(&n
->dev
, ioctl
, argp
);
1082 if (r
== -ENOIOCTLCMD
)
1083 r
= vhost_vring_ioctl(&n
->dev
, ioctl
, argp
);
1086 mutex_unlock(&n
->dev
.mutex
);
1091 #ifdef CONFIG_COMPAT
1092 static long vhost_net_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1095 return vhost_net_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1099 static const struct file_operations vhost_net_fops
= {
1100 .owner
= THIS_MODULE
,
1101 .release
= vhost_net_release
,
1102 .unlocked_ioctl
= vhost_net_ioctl
,
1103 #ifdef CONFIG_COMPAT
1104 .compat_ioctl
= vhost_net_compat_ioctl
,
1106 .open
= vhost_net_open
,
1107 .llseek
= noop_llseek
,
1110 static struct miscdevice vhost_net_misc
= {
1111 .minor
= VHOST_NET_MINOR
,
1112 .name
= "vhost-net",
1113 .fops
= &vhost_net_fops
,
1116 static int vhost_net_init(void)
1118 if (experimental_zcopytx
)
1119 vhost_net_enable_zcopy(VHOST_NET_VQ_TX
);
1120 return misc_register(&vhost_net_misc
);
1122 module_init(vhost_net_init
);
1124 static void vhost_net_exit(void)
1126 misc_deregister(&vhost_net_misc
);
1128 module_exit(vhost_net_exit
);
1130 MODULE_VERSION("0.0.1");
1131 MODULE_LICENSE("GPL v2");
1132 MODULE_AUTHOR("Michael S. Tsirkin");
1133 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
1134 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR
);
1135 MODULE_ALIAS("devname:vhost-net");