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_tap.h>
28 #include <linux/if_vlan.h>
34 static int experimental_zcopytx
= 1;
35 module_param(experimental_zcopytx
, int, 0444);
36 MODULE_PARM_DESC(experimental_zcopytx
, "Enable Zero Copy TX;"
37 " 1 -Enable; 0 - Disable");
39 /* Max number of bytes transferred before requeueing the job.
40 * Using this limit prevents one virtqueue from starving others. */
41 #define VHOST_NET_WEIGHT 0x80000
43 /* MAX number of TX used buffers for outstanding zerocopy */
44 #define VHOST_MAX_PEND 128
45 #define VHOST_GOODCOPY_LEN 256
48 * For transmit, used buffer len is unused; we override it to track buffer
49 * status internally; used for zerocopy tx only.
51 /* Lower device DMA failed */
52 #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
53 /* Lower device DMA done */
54 #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
55 /* Lower device DMA in progress */
56 #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
58 #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
60 #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
63 VHOST_NET_FEATURES
= VHOST_FEATURES
|
64 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
) |
65 (1ULL << VIRTIO_NET_F_MRG_RXBUF
) |
66 (1ULL << VIRTIO_F_IOMMU_PLATFORM
)
75 struct vhost_net_ubuf_ref
{
76 /* refcount follows semantics similar to kref:
77 * 0: object is released
78 * 1: no outstanding ubufs
79 * >1: outstanding ubufs
82 wait_queue_head_t wait
;
83 struct vhost_virtqueue
*vq
;
86 struct vhost_net_virtqueue
{
87 struct vhost_virtqueue vq
;
90 /* vhost zerocopy support fields below: */
91 /* last used idx for outstanding DMA zerocopy buffers */
93 /* first used idx for DMA done zerocopy buffers */
95 /* an array of userspace buffers info */
96 struct ubuf_info
*ubuf_info
;
97 /* Reference counting for outstanding ubufs.
98 * Protected by vq mutex. Writers must also take device mutex. */
99 struct vhost_net_ubuf_ref
*ubufs
;
103 struct vhost_dev dev
;
104 struct vhost_net_virtqueue vqs
[VHOST_NET_VQ_MAX
];
105 struct vhost_poll poll
[VHOST_NET_VQ_MAX
];
106 /* Number of TX recently submitted.
107 * Protected by tx vq lock. */
109 /* Number of times zerocopy TX recently failed.
110 * Protected by tx vq lock. */
111 unsigned tx_zcopy_err
;
112 /* Flush in progress. Protected by tx vq lock. */
116 static unsigned vhost_net_zcopy_mask __read_mostly
;
118 static void vhost_net_enable_zcopy(int vq
)
120 vhost_net_zcopy_mask
|= 0x1 << vq
;
123 static struct vhost_net_ubuf_ref
*
124 vhost_net_ubuf_alloc(struct vhost_virtqueue
*vq
, bool zcopy
)
126 struct vhost_net_ubuf_ref
*ubufs
;
127 /* No zero copy backend? Nothing to count. */
130 ubufs
= kmalloc(sizeof(*ubufs
), GFP_KERNEL
);
132 return ERR_PTR(-ENOMEM
);
133 atomic_set(&ubufs
->refcount
, 1);
134 init_waitqueue_head(&ubufs
->wait
);
139 static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref
*ubufs
)
141 int r
= atomic_sub_return(1, &ubufs
->refcount
);
143 wake_up(&ubufs
->wait
);
147 static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref
*ubufs
)
149 vhost_net_ubuf_put(ubufs
);
150 wait_event(ubufs
->wait
, !atomic_read(&ubufs
->refcount
));
153 static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref
*ubufs
)
155 vhost_net_ubuf_put_and_wait(ubufs
);
159 static void vhost_net_clear_ubuf_info(struct vhost_net
*n
)
163 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
164 kfree(n
->vqs
[i
].ubuf_info
);
165 n
->vqs
[i
].ubuf_info
= NULL
;
169 static int vhost_net_set_ubuf_info(struct vhost_net
*n
)
174 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
175 zcopy
= vhost_net_zcopy_mask
& (0x1 << i
);
178 n
->vqs
[i
].ubuf_info
= kmalloc(sizeof(*n
->vqs
[i
].ubuf_info
) *
179 UIO_MAXIOV
, GFP_KERNEL
);
180 if (!n
->vqs
[i
].ubuf_info
)
186 vhost_net_clear_ubuf_info(n
);
190 static void vhost_net_vq_reset(struct vhost_net
*n
)
194 vhost_net_clear_ubuf_info(n
);
196 for (i
= 0; i
< VHOST_NET_VQ_MAX
; i
++) {
197 n
->vqs
[i
].done_idx
= 0;
198 n
->vqs
[i
].upend_idx
= 0;
199 n
->vqs
[i
].ubufs
= NULL
;
200 n
->vqs
[i
].vhost_hlen
= 0;
201 n
->vqs
[i
].sock_hlen
= 0;
206 static void vhost_net_tx_packet(struct vhost_net
*net
)
209 if (net
->tx_packets
< 1024)
212 net
->tx_zcopy_err
= 0;
215 static void vhost_net_tx_err(struct vhost_net
*net
)
220 static bool vhost_net_tx_select_zcopy(struct vhost_net
*net
)
222 /* TX flush waits for outstanding DMAs to be done.
223 * Don't start new DMAs.
225 return !net
->tx_flush
&&
226 net
->tx_packets
/ 64 >= net
->tx_zcopy_err
;
229 static bool vhost_sock_zcopy(struct socket
*sock
)
231 return unlikely(experimental_zcopytx
) &&
232 sock_flag(sock
->sk
, SOCK_ZEROCOPY
);
235 /* In case of DMA done not in order in lower device driver for some reason.
236 * upend_idx is used to track end of used idx, done_idx is used to track head
237 * of used idx. Once lower device DMA done contiguously, we will signal KVM
240 static void vhost_zerocopy_signal_used(struct vhost_net
*net
,
241 struct vhost_virtqueue
*vq
)
243 struct vhost_net_virtqueue
*nvq
=
244 container_of(vq
, struct vhost_net_virtqueue
, vq
);
248 for (i
= nvq
->done_idx
; i
!= nvq
->upend_idx
; i
= (i
+ 1) % UIO_MAXIOV
) {
249 if (vq
->heads
[i
].len
== VHOST_DMA_FAILED_LEN
)
250 vhost_net_tx_err(net
);
251 if (VHOST_DMA_IS_DONE(vq
->heads
[i
].len
)) {
252 vq
->heads
[i
].len
= VHOST_DMA_CLEAR_LEN
;
258 add
= min(UIO_MAXIOV
- nvq
->done_idx
, j
);
259 vhost_add_used_and_signal_n(vq
->dev
, vq
,
260 &vq
->heads
[nvq
->done_idx
], add
);
261 nvq
->done_idx
= (nvq
->done_idx
+ add
) % UIO_MAXIOV
;
266 static void vhost_zerocopy_callback(struct ubuf_info
*ubuf
, bool success
)
268 struct vhost_net_ubuf_ref
*ubufs
= ubuf
->ctx
;
269 struct vhost_virtqueue
*vq
= ubufs
->vq
;
274 /* set len to mark this desc buffers done DMA */
275 vq
->heads
[ubuf
->desc
].len
= success
?
276 VHOST_DMA_DONE_LEN
: VHOST_DMA_FAILED_LEN
;
277 cnt
= vhost_net_ubuf_put(ubufs
);
280 * Trigger polling thread if guest stopped submitting new buffers:
281 * in this case, the refcount after decrement will eventually reach 1.
282 * We also trigger polling periodically after each 16 packets
283 * (the value 16 here is more or less arbitrary, it's tuned to trigger
284 * less than 10% of times).
286 if (cnt
<= 1 || !(cnt
% 16))
287 vhost_poll_queue(&vq
->poll
);
289 rcu_read_unlock_bh();
292 static inline unsigned long busy_clock(void)
294 return local_clock() >> 10;
297 static bool vhost_can_busy_poll(struct vhost_dev
*dev
,
298 unsigned long endtime
)
300 return likely(!need_resched()) &&
301 likely(!time_after(busy_clock(), endtime
)) &&
302 likely(!signal_pending(current
)) &&
303 !vhost_has_work(dev
);
306 static void vhost_net_disable_vq(struct vhost_net
*n
,
307 struct vhost_virtqueue
*vq
)
309 struct vhost_net_virtqueue
*nvq
=
310 container_of(vq
, struct vhost_net_virtqueue
, vq
);
311 struct vhost_poll
*poll
= n
->poll
+ (nvq
- n
->vqs
);
312 if (!vq
->private_data
)
314 vhost_poll_stop(poll
);
317 static int vhost_net_enable_vq(struct vhost_net
*n
,
318 struct vhost_virtqueue
*vq
)
320 struct vhost_net_virtqueue
*nvq
=
321 container_of(vq
, struct vhost_net_virtqueue
, vq
);
322 struct vhost_poll
*poll
= n
->poll
+ (nvq
- n
->vqs
);
325 sock
= vq
->private_data
;
329 return vhost_poll_start(poll
, sock
->file
);
332 static int vhost_net_tx_get_vq_desc(struct vhost_net
*net
,
333 struct vhost_virtqueue
*vq
,
334 struct iovec iov
[], unsigned int iov_size
,
335 unsigned int *out_num
, unsigned int *in_num
)
337 unsigned long uninitialized_var(endtime
);
338 int r
= vhost_get_vq_desc(vq
, vq
->iov
, ARRAY_SIZE(vq
->iov
),
339 out_num
, in_num
, NULL
, NULL
);
341 if (r
== vq
->num
&& vq
->busyloop_timeout
) {
343 endtime
= busy_clock() + vq
->busyloop_timeout
;
344 while (vhost_can_busy_poll(vq
->dev
, endtime
) &&
345 vhost_vq_avail_empty(vq
->dev
, vq
))
348 r
= vhost_get_vq_desc(vq
, vq
->iov
, ARRAY_SIZE(vq
->iov
),
349 out_num
, in_num
, NULL
, NULL
);
355 static bool vhost_exceeds_maxpend(struct vhost_net
*net
)
357 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_TX
];
358 struct vhost_virtqueue
*vq
= &nvq
->vq
;
360 return (nvq
->upend_idx
+ vq
->num
- VHOST_MAX_PEND
) % UIO_MAXIOV
364 /* Expects to be always run from workqueue - which acts as
365 * read-size critical section for our kind of RCU. */
366 static void handle_tx(struct vhost_net
*net
)
368 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_TX
];
369 struct vhost_virtqueue
*vq
= &nvq
->vq
;
372 struct msghdr msg
= {
377 .msg_flags
= MSG_DONTWAIT
,
379 size_t len
, total_len
= 0;
383 struct vhost_net_ubuf_ref
*uninitialized_var(ubufs
);
384 bool zcopy
, zcopy_used
;
386 mutex_lock(&vq
->mutex
);
387 sock
= vq
->private_data
;
391 if (!vq_iotlb_prefetch(vq
))
394 vhost_disable_notify(&net
->dev
, vq
);
396 hdr_size
= nvq
->vhost_hlen
;
400 /* Release DMAs done buffers first */
402 vhost_zerocopy_signal_used(net
, vq
);
404 /* If more outstanding DMAs, queue the work.
405 * Handle upend_idx wrap around
407 if (unlikely(vhost_exceeds_maxpend(net
)))
410 head
= vhost_net_tx_get_vq_desc(net
, vq
, vq
->iov
,
413 /* On error, stop handling until the next kick. */
414 if (unlikely(head
< 0))
416 /* Nothing new? Wait for eventfd to tell us they refilled. */
417 if (head
== vq
->num
) {
418 if (unlikely(vhost_enable_notify(&net
->dev
, vq
))) {
419 vhost_disable_notify(&net
->dev
, vq
);
425 vq_err(vq
, "Unexpected descriptor format for TX: "
426 "out %d, int %d\n", out
, in
);
429 /* Skip header. TODO: support TSO. */
430 len
= iov_length(vq
->iov
, out
);
431 iov_iter_init(&msg
.msg_iter
, WRITE
, vq
->iov
, out
, len
);
432 iov_iter_advance(&msg
.msg_iter
, hdr_size
);
434 if (!msg_data_left(&msg
)) {
435 vq_err(vq
, "Unexpected header len for TX: "
436 "%zd expected %zd\n",
440 len
= msg_data_left(&msg
);
442 zcopy_used
= zcopy
&& len
>= VHOST_GOODCOPY_LEN
443 && (nvq
->upend_idx
+ 1) % UIO_MAXIOV
!=
445 && vhost_net_tx_select_zcopy(net
);
447 /* use msg_control to pass vhost zerocopy ubuf info to skb */
449 struct ubuf_info
*ubuf
;
450 ubuf
= nvq
->ubuf_info
+ nvq
->upend_idx
;
452 vq
->heads
[nvq
->upend_idx
].id
= cpu_to_vhost32(vq
, head
);
453 vq
->heads
[nvq
->upend_idx
].len
= VHOST_DMA_IN_PROGRESS
;
454 ubuf
->callback
= vhost_zerocopy_callback
;
455 ubuf
->ctx
= nvq
->ubufs
;
456 ubuf
->desc
= nvq
->upend_idx
;
457 msg
.msg_control
= ubuf
;
458 msg
.msg_controllen
= sizeof(ubuf
);
460 atomic_inc(&ubufs
->refcount
);
461 nvq
->upend_idx
= (nvq
->upend_idx
+ 1) % UIO_MAXIOV
;
463 msg
.msg_control
= NULL
;
468 if (total_len
< VHOST_NET_WEIGHT
&&
469 !vhost_vq_avail_empty(&net
->dev
, vq
) &&
470 likely(!vhost_exceeds_maxpend(net
))) {
471 msg
.msg_flags
|= MSG_MORE
;
473 msg
.msg_flags
&= ~MSG_MORE
;
476 /* TODO: Check specific error and bomb out unless ENOBUFS? */
477 err
= sock
->ops
->sendmsg(sock
, &msg
, len
);
478 if (unlikely(err
< 0)) {
480 vhost_net_ubuf_put(ubufs
);
481 nvq
->upend_idx
= ((unsigned)nvq
->upend_idx
- 1)
484 vhost_discard_vq_desc(vq
, 1);
488 pr_debug("Truncated TX packet: "
489 " len %d != %zd\n", err
, len
);
491 vhost_add_used_and_signal(&net
->dev
, vq
, head
, 0);
493 vhost_zerocopy_signal_used(net
, vq
);
494 vhost_net_tx_packet(net
);
495 if (unlikely(total_len
>= VHOST_NET_WEIGHT
)) {
496 vhost_poll_queue(&vq
->poll
);
501 mutex_unlock(&vq
->mutex
);
504 static int peek_head_len(struct sock
*sk
)
506 struct socket
*sock
= sk
->sk_socket
;
507 struct sk_buff
*head
;
511 if (sock
->ops
->peek_len
)
512 return sock
->ops
->peek_len(sock
);
514 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, flags
);
515 head
= skb_peek(&sk
->sk_receive_queue
);
518 if (skb_vlan_tag_present(head
))
522 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, flags
);
526 static int sk_has_rx_data(struct sock
*sk
)
528 struct socket
*sock
= sk
->sk_socket
;
530 if (sock
->ops
->peek_len
)
531 return sock
->ops
->peek_len(sock
);
533 return skb_queue_empty(&sk
->sk_receive_queue
);
536 static int vhost_net_rx_peek_head_len(struct vhost_net
*net
, struct sock
*sk
)
538 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_TX
];
539 struct vhost_virtqueue
*vq
= &nvq
->vq
;
540 unsigned long uninitialized_var(endtime
);
541 int len
= peek_head_len(sk
);
543 if (!len
&& vq
->busyloop_timeout
) {
544 /* Both tx vq and rx socket were polled here */
545 mutex_lock(&vq
->mutex
);
546 vhost_disable_notify(&net
->dev
, vq
);
549 endtime
= busy_clock() + vq
->busyloop_timeout
;
551 while (vhost_can_busy_poll(&net
->dev
, endtime
) &&
552 !sk_has_rx_data(sk
) &&
553 vhost_vq_avail_empty(&net
->dev
, vq
))
558 if (vhost_enable_notify(&net
->dev
, vq
))
559 vhost_poll_queue(&vq
->poll
);
560 mutex_unlock(&vq
->mutex
);
562 len
= peek_head_len(sk
);
568 /* This is a multi-buffer version of vhost_get_desc, that works if
569 * vq has read descriptors only.
570 * @vq - the relevant virtqueue
571 * @datalen - data length we'll be reading
572 * @iovcount - returned count of io vectors we fill
574 * @log_num - log offset
575 * @quota - headcount quota, 1 for big buffer
576 * returns number of buffer heads allocated, negative on error
578 static int get_rx_bufs(struct vhost_virtqueue
*vq
,
579 struct vring_used_elem
*heads
,
582 struct vhost_log
*log
,
586 unsigned int out
, in
;
591 /* len is always initialized before use since we are always called with
594 u32
uninitialized_var(len
);
596 while (datalen
> 0 && headcount
< quota
) {
597 if (unlikely(seg
>= UIO_MAXIOV
)) {
601 r
= vhost_get_vq_desc(vq
, vq
->iov
+ seg
,
602 ARRAY_SIZE(vq
->iov
) - seg
, &out
,
612 if (unlikely(out
|| in
<= 0)) {
613 vq_err(vq
, "unexpected descriptor format for RX: "
614 "out %d, in %d\n", out
, in
);
622 heads
[headcount
].id
= cpu_to_vhost32(vq
, d
);
623 len
= iov_length(vq
->iov
+ seg
, in
);
624 heads
[headcount
].len
= cpu_to_vhost32(vq
, len
);
629 heads
[headcount
- 1].len
= cpu_to_vhost32(vq
, len
+ datalen
);
635 if (unlikely(datalen
> 0)) {
641 vhost_discard_vq_desc(vq
, headcount
);
645 /* Expects to be always run from workqueue - which acts as
646 * read-size critical section for our kind of RCU. */
647 static void handle_rx(struct vhost_net
*net
)
649 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_RX
];
650 struct vhost_virtqueue
*vq
= &nvq
->vq
;
651 unsigned uninitialized_var(in
), log
;
652 struct vhost_log
*vq_log
;
653 struct msghdr msg
= {
656 .msg_control
= NULL
, /* FIXME: get and handle RX aux data. */
658 .msg_flags
= MSG_DONTWAIT
,
660 struct virtio_net_hdr hdr
= {
662 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
664 size_t total_len
= 0;
667 size_t vhost_hlen
, sock_hlen
;
668 size_t vhost_len
, sock_len
;
670 struct iov_iter fixup
;
671 __virtio16 num_buffers
;
673 mutex_lock(&vq
->mutex
);
674 sock
= vq
->private_data
;
678 if (!vq_iotlb_prefetch(vq
))
681 vhost_disable_notify(&net
->dev
, vq
);
682 vhost_net_disable_vq(net
, vq
);
684 vhost_hlen
= nvq
->vhost_hlen
;
685 sock_hlen
= nvq
->sock_hlen
;
687 vq_log
= unlikely(vhost_has_feature(vq
, VHOST_F_LOG_ALL
)) ?
689 mergeable
= vhost_has_feature(vq
, VIRTIO_NET_F_MRG_RXBUF
);
691 while ((sock_len
= vhost_net_rx_peek_head_len(net
, sock
->sk
))) {
692 sock_len
+= sock_hlen
;
693 vhost_len
= sock_len
+ vhost_hlen
;
694 headcount
= get_rx_bufs(vq
, vq
->heads
, vhost_len
,
696 likely(mergeable
) ? UIO_MAXIOV
: 1);
697 /* On error, stop handling until the next kick. */
698 if (unlikely(headcount
< 0))
700 /* On overrun, truncate and discard */
701 if (unlikely(headcount
> UIO_MAXIOV
)) {
702 iov_iter_init(&msg
.msg_iter
, READ
, vq
->iov
, 1, 1);
703 err
= sock
->ops
->recvmsg(sock
, &msg
,
704 1, MSG_DONTWAIT
| MSG_TRUNC
);
705 pr_debug("Discarded rx packet: len %zd\n", sock_len
);
708 /* OK, now we need to know about added descriptors. */
710 if (unlikely(vhost_enable_notify(&net
->dev
, vq
))) {
711 /* They have slipped one in as we were
712 * doing that: check again. */
713 vhost_disable_notify(&net
->dev
, vq
);
716 /* Nothing new? Wait for eventfd to tell us
720 /* We don't need to be notified again. */
721 iov_iter_init(&msg
.msg_iter
, READ
, vq
->iov
, in
, vhost_len
);
722 fixup
= msg
.msg_iter
;
723 if (unlikely((vhost_hlen
))) {
724 /* We will supply the header ourselves
727 iov_iter_advance(&msg
.msg_iter
, vhost_hlen
);
729 err
= sock
->ops
->recvmsg(sock
, &msg
,
730 sock_len
, MSG_DONTWAIT
| MSG_TRUNC
);
731 /* Userspace might have consumed the packet meanwhile:
732 * it's not supposed to do this usually, but might be hard
733 * to prevent. Discard data we got (if any) and keep going. */
734 if (unlikely(err
!= sock_len
)) {
735 pr_debug("Discarded rx packet: "
736 " len %d, expected %zd\n", err
, sock_len
);
737 vhost_discard_vq_desc(vq
, headcount
);
740 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
741 if (unlikely(vhost_hlen
)) {
742 if (copy_to_iter(&hdr
, sizeof(hdr
),
743 &fixup
) != sizeof(hdr
)) {
744 vq_err(vq
, "Unable to write vnet_hdr "
745 "at addr %p\n", vq
->iov
->iov_base
);
749 /* Header came from socket; we'll need to patch
750 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
752 iov_iter_advance(&fixup
, sizeof(hdr
));
754 /* TODO: Should check and handle checksum. */
756 num_buffers
= cpu_to_vhost16(vq
, headcount
);
757 if (likely(mergeable
) &&
758 copy_to_iter(&num_buffers
, sizeof num_buffers
,
759 &fixup
) != sizeof num_buffers
) {
760 vq_err(vq
, "Failed num_buffers write");
761 vhost_discard_vq_desc(vq
, headcount
);
764 vhost_add_used_and_signal_n(&net
->dev
, vq
, vq
->heads
,
766 if (unlikely(vq_log
))
767 vhost_log_write(vq
, vq_log
, log
, vhost_len
);
768 total_len
+= vhost_len
;
769 if (unlikely(total_len
>= VHOST_NET_WEIGHT
)) {
770 vhost_poll_queue(&vq
->poll
);
774 vhost_net_enable_vq(net
, vq
);
776 mutex_unlock(&vq
->mutex
);
779 static void handle_tx_kick(struct vhost_work
*work
)
781 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
783 struct vhost_net
*net
= container_of(vq
->dev
, struct vhost_net
, dev
);
788 static void handle_rx_kick(struct vhost_work
*work
)
790 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
792 struct vhost_net
*net
= container_of(vq
->dev
, struct vhost_net
, dev
);
797 static void handle_tx_net(struct vhost_work
*work
)
799 struct vhost_net
*net
= container_of(work
, struct vhost_net
,
800 poll
[VHOST_NET_VQ_TX
].work
);
804 static void handle_rx_net(struct vhost_work
*work
)
806 struct vhost_net
*net
= container_of(work
, struct vhost_net
,
807 poll
[VHOST_NET_VQ_RX
].work
);
811 static int vhost_net_open(struct inode
*inode
, struct file
*f
)
814 struct vhost_dev
*dev
;
815 struct vhost_virtqueue
**vqs
;
818 n
= kmalloc(sizeof *n
, GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
820 n
= vmalloc(sizeof *n
);
824 vqs
= kmalloc(VHOST_NET_VQ_MAX
* sizeof(*vqs
), GFP_KERNEL
);
831 vqs
[VHOST_NET_VQ_TX
] = &n
->vqs
[VHOST_NET_VQ_TX
].vq
;
832 vqs
[VHOST_NET_VQ_RX
] = &n
->vqs
[VHOST_NET_VQ_RX
].vq
;
833 n
->vqs
[VHOST_NET_VQ_TX
].vq
.handle_kick
= handle_tx_kick
;
834 n
->vqs
[VHOST_NET_VQ_RX
].vq
.handle_kick
= handle_rx_kick
;
835 for (i
= 0; i
< VHOST_NET_VQ_MAX
; i
++) {
836 n
->vqs
[i
].ubufs
= NULL
;
837 n
->vqs
[i
].ubuf_info
= NULL
;
838 n
->vqs
[i
].upend_idx
= 0;
839 n
->vqs
[i
].done_idx
= 0;
840 n
->vqs
[i
].vhost_hlen
= 0;
841 n
->vqs
[i
].sock_hlen
= 0;
843 vhost_dev_init(dev
, vqs
, VHOST_NET_VQ_MAX
);
845 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_TX
, handle_tx_net
, POLLOUT
, dev
);
846 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_RX
, handle_rx_net
, POLLIN
, dev
);
853 static struct socket
*vhost_net_stop_vq(struct vhost_net
*n
,
854 struct vhost_virtqueue
*vq
)
858 mutex_lock(&vq
->mutex
);
859 sock
= vq
->private_data
;
860 vhost_net_disable_vq(n
, vq
);
861 vq
->private_data
= NULL
;
862 mutex_unlock(&vq
->mutex
);
866 static void vhost_net_stop(struct vhost_net
*n
, struct socket
**tx_sock
,
867 struct socket
**rx_sock
)
869 *tx_sock
= vhost_net_stop_vq(n
, &n
->vqs
[VHOST_NET_VQ_TX
].vq
);
870 *rx_sock
= vhost_net_stop_vq(n
, &n
->vqs
[VHOST_NET_VQ_RX
].vq
);
873 static void vhost_net_flush_vq(struct vhost_net
*n
, int index
)
875 vhost_poll_flush(n
->poll
+ index
);
876 vhost_poll_flush(&n
->vqs
[index
].vq
.poll
);
879 static void vhost_net_flush(struct vhost_net
*n
)
881 vhost_net_flush_vq(n
, VHOST_NET_VQ_TX
);
882 vhost_net_flush_vq(n
, VHOST_NET_VQ_RX
);
883 if (n
->vqs
[VHOST_NET_VQ_TX
].ubufs
) {
884 mutex_lock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
886 mutex_unlock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
887 /* Wait for all lower device DMAs done. */
888 vhost_net_ubuf_put_and_wait(n
->vqs
[VHOST_NET_VQ_TX
].ubufs
);
889 mutex_lock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
891 atomic_set(&n
->vqs
[VHOST_NET_VQ_TX
].ubufs
->refcount
, 1);
892 mutex_unlock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
896 static int vhost_net_release(struct inode
*inode
, struct file
*f
)
898 struct vhost_net
*n
= f
->private_data
;
899 struct socket
*tx_sock
;
900 struct socket
*rx_sock
;
902 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
904 vhost_dev_stop(&n
->dev
);
905 vhost_dev_cleanup(&n
->dev
, false);
906 vhost_net_vq_reset(n
);
911 /* Make sure no callbacks are outstanding */
912 synchronize_rcu_bh();
913 /* We do an extra flush before freeing memory,
914 * since jobs can re-queue themselves. */
921 static struct socket
*get_raw_socket(int fd
)
924 struct sockaddr_ll sa
;
925 char buf
[MAX_ADDR_LEN
];
927 int uaddr_len
= sizeof uaddr
, r
;
928 struct socket
*sock
= sockfd_lookup(fd
, &r
);
931 return ERR_PTR(-ENOTSOCK
);
933 /* Parameter checking */
934 if (sock
->sk
->sk_type
!= SOCK_RAW
) {
935 r
= -ESOCKTNOSUPPORT
;
939 r
= sock
->ops
->getname(sock
, (struct sockaddr
*)&uaddr
.sa
,
944 if (uaddr
.sa
.sll_family
!= AF_PACKET
) {
954 static struct socket
*get_tap_socket(int fd
)
956 struct file
*file
= fget(fd
);
960 return ERR_PTR(-EBADF
);
961 sock
= tun_get_socket(file
);
964 sock
= tap_get_socket(file
);
970 static struct socket
*get_socket(int fd
)
974 /* special case to disable backend */
977 sock
= get_raw_socket(fd
);
980 sock
= get_tap_socket(fd
);
983 return ERR_PTR(-ENOTSOCK
);
986 static long vhost_net_set_backend(struct vhost_net
*n
, unsigned index
, int fd
)
988 struct socket
*sock
, *oldsock
;
989 struct vhost_virtqueue
*vq
;
990 struct vhost_net_virtqueue
*nvq
;
991 struct vhost_net_ubuf_ref
*ubufs
, *oldubufs
= NULL
;
994 mutex_lock(&n
->dev
.mutex
);
995 r
= vhost_dev_check_owner(&n
->dev
);
999 if (index
>= VHOST_NET_VQ_MAX
) {
1003 vq
= &n
->vqs
[index
].vq
;
1004 nvq
= &n
->vqs
[index
];
1005 mutex_lock(&vq
->mutex
);
1007 /* Verify that ring has been setup correctly. */
1008 if (!vhost_vq_access_ok(vq
)) {
1012 sock
= get_socket(fd
);
1018 /* start polling new socket */
1019 oldsock
= vq
->private_data
;
1020 if (sock
!= oldsock
) {
1021 ubufs
= vhost_net_ubuf_alloc(vq
,
1022 sock
&& vhost_sock_zcopy(sock
));
1023 if (IS_ERR(ubufs
)) {
1028 vhost_net_disable_vq(n
, vq
);
1029 vq
->private_data
= sock
;
1030 r
= vhost_vq_init_access(vq
);
1033 r
= vhost_net_enable_vq(n
, vq
);
1037 oldubufs
= nvq
->ubufs
;
1041 n
->tx_zcopy_err
= 0;
1042 n
->tx_flush
= false;
1045 mutex_unlock(&vq
->mutex
);
1048 vhost_net_ubuf_put_wait_and_free(oldubufs
);
1049 mutex_lock(&vq
->mutex
);
1050 vhost_zerocopy_signal_used(n
, vq
);
1051 mutex_unlock(&vq
->mutex
);
1055 vhost_net_flush_vq(n
, index
);
1056 sockfd_put(oldsock
);
1059 mutex_unlock(&n
->dev
.mutex
);
1063 vq
->private_data
= oldsock
;
1064 vhost_net_enable_vq(n
, vq
);
1066 vhost_net_ubuf_put_wait_and_free(ubufs
);
1070 mutex_unlock(&vq
->mutex
);
1072 mutex_unlock(&n
->dev
.mutex
);
1076 static long vhost_net_reset_owner(struct vhost_net
*n
)
1078 struct socket
*tx_sock
= NULL
;
1079 struct socket
*rx_sock
= NULL
;
1081 struct vhost_umem
*umem
;
1083 mutex_lock(&n
->dev
.mutex
);
1084 err
= vhost_dev_check_owner(&n
->dev
);
1087 umem
= vhost_dev_reset_owner_prepare();
1092 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
1094 vhost_dev_reset_owner(&n
->dev
, umem
);
1095 vhost_net_vq_reset(n
);
1097 mutex_unlock(&n
->dev
.mutex
);
1099 sockfd_put(tx_sock
);
1101 sockfd_put(rx_sock
);
1105 static int vhost_net_set_features(struct vhost_net
*n
, u64 features
)
1107 size_t vhost_hlen
, sock_hlen
, hdr_len
;
1110 hdr_len
= (features
& ((1ULL << VIRTIO_NET_F_MRG_RXBUF
) |
1111 (1ULL << VIRTIO_F_VERSION_1
))) ?
1112 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
1113 sizeof(struct virtio_net_hdr
);
1114 if (features
& (1 << VHOST_NET_F_VIRTIO_NET_HDR
)) {
1115 /* vhost provides vnet_hdr */
1116 vhost_hlen
= hdr_len
;
1119 /* socket provides vnet_hdr */
1121 sock_hlen
= hdr_len
;
1123 mutex_lock(&n
->dev
.mutex
);
1124 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1125 !vhost_log_access_ok(&n
->dev
))
1128 if ((features
& (1ULL << VIRTIO_F_IOMMU_PLATFORM
))) {
1129 if (vhost_init_device_iotlb(&n
->dev
, true))
1133 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
1134 mutex_lock(&n
->vqs
[i
].vq
.mutex
);
1135 n
->vqs
[i
].vq
.acked_features
= features
;
1136 n
->vqs
[i
].vhost_hlen
= vhost_hlen
;
1137 n
->vqs
[i
].sock_hlen
= sock_hlen
;
1138 mutex_unlock(&n
->vqs
[i
].vq
.mutex
);
1140 mutex_unlock(&n
->dev
.mutex
);
1144 mutex_unlock(&n
->dev
.mutex
);
1148 static long vhost_net_set_owner(struct vhost_net
*n
)
1152 mutex_lock(&n
->dev
.mutex
);
1153 if (vhost_dev_has_owner(&n
->dev
)) {
1157 r
= vhost_net_set_ubuf_info(n
);
1160 r
= vhost_dev_set_owner(&n
->dev
);
1162 vhost_net_clear_ubuf_info(n
);
1165 mutex_unlock(&n
->dev
.mutex
);
1169 static long vhost_net_ioctl(struct file
*f
, unsigned int ioctl
,
1172 struct vhost_net
*n
= f
->private_data
;
1173 void __user
*argp
= (void __user
*)arg
;
1174 u64 __user
*featurep
= argp
;
1175 struct vhost_vring_file backend
;
1180 case VHOST_NET_SET_BACKEND
:
1181 if (copy_from_user(&backend
, argp
, sizeof backend
))
1183 return vhost_net_set_backend(n
, backend
.index
, backend
.fd
);
1184 case VHOST_GET_FEATURES
:
1185 features
= VHOST_NET_FEATURES
;
1186 if (copy_to_user(featurep
, &features
, sizeof features
))
1189 case VHOST_SET_FEATURES
:
1190 if (copy_from_user(&features
, featurep
, sizeof features
))
1192 if (features
& ~VHOST_NET_FEATURES
)
1194 return vhost_net_set_features(n
, features
);
1195 case VHOST_RESET_OWNER
:
1196 return vhost_net_reset_owner(n
);
1197 case VHOST_SET_OWNER
:
1198 return vhost_net_set_owner(n
);
1200 mutex_lock(&n
->dev
.mutex
);
1201 r
= vhost_dev_ioctl(&n
->dev
, ioctl
, argp
);
1202 if (r
== -ENOIOCTLCMD
)
1203 r
= vhost_vring_ioctl(&n
->dev
, ioctl
, argp
);
1206 mutex_unlock(&n
->dev
.mutex
);
1211 #ifdef CONFIG_COMPAT
1212 static long vhost_net_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1215 return vhost_net_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1219 static ssize_t
vhost_net_chr_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1221 struct file
*file
= iocb
->ki_filp
;
1222 struct vhost_net
*n
= file
->private_data
;
1223 struct vhost_dev
*dev
= &n
->dev
;
1224 int noblock
= file
->f_flags
& O_NONBLOCK
;
1226 return vhost_chr_read_iter(dev
, to
, noblock
);
1229 static ssize_t
vhost_net_chr_write_iter(struct kiocb
*iocb
,
1230 struct iov_iter
*from
)
1232 struct file
*file
= iocb
->ki_filp
;
1233 struct vhost_net
*n
= file
->private_data
;
1234 struct vhost_dev
*dev
= &n
->dev
;
1236 return vhost_chr_write_iter(dev
, from
);
1239 static unsigned int vhost_net_chr_poll(struct file
*file
, poll_table
*wait
)
1241 struct vhost_net
*n
= file
->private_data
;
1242 struct vhost_dev
*dev
= &n
->dev
;
1244 return vhost_chr_poll(file
, dev
, wait
);
1247 static const struct file_operations vhost_net_fops
= {
1248 .owner
= THIS_MODULE
,
1249 .release
= vhost_net_release
,
1250 .read_iter
= vhost_net_chr_read_iter
,
1251 .write_iter
= vhost_net_chr_write_iter
,
1252 .poll
= vhost_net_chr_poll
,
1253 .unlocked_ioctl
= vhost_net_ioctl
,
1254 #ifdef CONFIG_COMPAT
1255 .compat_ioctl
= vhost_net_compat_ioctl
,
1257 .open
= vhost_net_open
,
1258 .llseek
= noop_llseek
,
1261 static struct miscdevice vhost_net_misc
= {
1262 .minor
= VHOST_NET_MINOR
,
1263 .name
= "vhost-net",
1264 .fops
= &vhost_net_fops
,
1267 static int vhost_net_init(void)
1269 if (experimental_zcopytx
)
1270 vhost_net_enable_zcopy(VHOST_NET_VQ_TX
);
1271 return misc_register(&vhost_net_misc
);
1273 module_init(vhost_net_init
);
1275 static void vhost_net_exit(void)
1277 misc_deregister(&vhost_net_misc
);
1279 module_exit(vhost_net_exit
);
1281 MODULE_VERSION("0.0.1");
1282 MODULE_LICENSE("GPL v2");
1283 MODULE_AUTHOR("Michael S. Tsirkin");
1284 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
1285 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR
);
1286 MODULE_ALIAS("devname:vhost-net");