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
= 1;
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 TX used buffers for outstanding zerocopy */
43 #define VHOST_MAX_PEND 128
44 #define VHOST_GOODCOPY_LEN 256
47 * For transmit, used buffer len is unused; we override it to track buffer
48 * status internally; used for zerocopy tx only.
50 /* Lower device DMA failed */
51 #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
52 /* Lower device DMA done */
53 #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
54 /* Lower device DMA in progress */
55 #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
57 #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
59 #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
62 VHOST_NET_FEATURES
= VHOST_FEATURES
|
63 (1ULL << VHOST_NET_F_VIRTIO_NET_HDR
) |
64 (1ULL << VIRTIO_NET_F_MRG_RXBUF
) |
65 (1ULL << VIRTIO_F_IOMMU_PLATFORM
)
74 struct vhost_net_ubuf_ref
{
75 /* refcount follows semantics similar to kref:
76 * 0: object is released
77 * 1: no outstanding ubufs
78 * >1: outstanding ubufs
81 wait_queue_head_t wait
;
82 struct vhost_virtqueue
*vq
;
85 struct vhost_net_virtqueue
{
86 struct vhost_virtqueue vq
;
89 /* vhost zerocopy support fields below: */
90 /* last used idx for outstanding DMA zerocopy buffers */
92 /* first used idx for DMA done zerocopy buffers */
94 /* an array of userspace buffers info */
95 struct ubuf_info
*ubuf_info
;
96 /* Reference counting for outstanding ubufs.
97 * Protected by vq mutex. Writers must also take device mutex. */
98 struct vhost_net_ubuf_ref
*ubufs
;
102 struct vhost_dev dev
;
103 struct vhost_net_virtqueue vqs
[VHOST_NET_VQ_MAX
];
104 struct vhost_poll poll
[VHOST_NET_VQ_MAX
];
105 /* Number of TX recently submitted.
106 * Protected by tx vq lock. */
108 /* Number of times zerocopy TX recently failed.
109 * Protected by tx vq lock. */
110 unsigned tx_zcopy_err
;
111 /* Flush in progress. Protected by tx vq lock. */
115 static unsigned vhost_net_zcopy_mask __read_mostly
;
117 static void vhost_net_enable_zcopy(int vq
)
119 vhost_net_zcopy_mask
|= 0x1 << vq
;
122 static struct vhost_net_ubuf_ref
*
123 vhost_net_ubuf_alloc(struct vhost_virtqueue
*vq
, bool zcopy
)
125 struct vhost_net_ubuf_ref
*ubufs
;
126 /* No zero copy backend? Nothing to count. */
129 ubufs
= kmalloc(sizeof(*ubufs
), GFP_KERNEL
);
131 return ERR_PTR(-ENOMEM
);
132 atomic_set(&ubufs
->refcount
, 1);
133 init_waitqueue_head(&ubufs
->wait
);
138 static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref
*ubufs
)
140 int r
= atomic_sub_return(1, &ubufs
->refcount
);
142 wake_up(&ubufs
->wait
);
146 static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref
*ubufs
)
148 vhost_net_ubuf_put(ubufs
);
149 wait_event(ubufs
->wait
, !atomic_read(&ubufs
->refcount
));
152 static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref
*ubufs
)
154 vhost_net_ubuf_put_and_wait(ubufs
);
158 static void vhost_net_clear_ubuf_info(struct vhost_net
*n
)
162 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
163 kfree(n
->vqs
[i
].ubuf_info
);
164 n
->vqs
[i
].ubuf_info
= NULL
;
168 static int vhost_net_set_ubuf_info(struct vhost_net
*n
)
173 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
174 zcopy
= vhost_net_zcopy_mask
& (0x1 << i
);
177 n
->vqs
[i
].ubuf_info
= kmalloc(sizeof(*n
->vqs
[i
].ubuf_info
) *
178 UIO_MAXIOV
, GFP_KERNEL
);
179 if (!n
->vqs
[i
].ubuf_info
)
185 vhost_net_clear_ubuf_info(n
);
189 static void vhost_net_vq_reset(struct vhost_net
*n
)
193 vhost_net_clear_ubuf_info(n
);
195 for (i
= 0; i
< VHOST_NET_VQ_MAX
; i
++) {
196 n
->vqs
[i
].done_idx
= 0;
197 n
->vqs
[i
].upend_idx
= 0;
198 n
->vqs
[i
].ubufs
= NULL
;
199 n
->vqs
[i
].vhost_hlen
= 0;
200 n
->vqs
[i
].sock_hlen
= 0;
205 static void vhost_net_tx_packet(struct vhost_net
*net
)
208 if (net
->tx_packets
< 1024)
211 net
->tx_zcopy_err
= 0;
214 static void vhost_net_tx_err(struct vhost_net
*net
)
219 static bool vhost_net_tx_select_zcopy(struct vhost_net
*net
)
221 /* TX flush waits for outstanding DMAs to be done.
222 * Don't start new DMAs.
224 return !net
->tx_flush
&&
225 net
->tx_packets
/ 64 >= net
->tx_zcopy_err
;
228 static bool vhost_sock_zcopy(struct socket
*sock
)
230 return unlikely(experimental_zcopytx
) &&
231 sock_flag(sock
->sk
, SOCK_ZEROCOPY
);
234 /* In case of DMA done not in order in lower device driver for some reason.
235 * upend_idx is used to track end of used idx, done_idx is used to track head
236 * of used idx. Once lower device DMA done contiguously, we will signal KVM
239 static void vhost_zerocopy_signal_used(struct vhost_net
*net
,
240 struct vhost_virtqueue
*vq
)
242 struct vhost_net_virtqueue
*nvq
=
243 container_of(vq
, struct vhost_net_virtqueue
, vq
);
247 for (i
= nvq
->done_idx
; i
!= nvq
->upend_idx
; i
= (i
+ 1) % UIO_MAXIOV
) {
248 if (vq
->heads
[i
].len
== VHOST_DMA_FAILED_LEN
)
249 vhost_net_tx_err(net
);
250 if (VHOST_DMA_IS_DONE(vq
->heads
[i
].len
)) {
251 vq
->heads
[i
].len
= VHOST_DMA_CLEAR_LEN
;
257 add
= min(UIO_MAXIOV
- nvq
->done_idx
, j
);
258 vhost_add_used_and_signal_n(vq
->dev
, vq
,
259 &vq
->heads
[nvq
->done_idx
], add
);
260 nvq
->done_idx
= (nvq
->done_idx
+ add
) % UIO_MAXIOV
;
265 static void vhost_zerocopy_callback(struct ubuf_info
*ubuf
, bool success
)
267 struct vhost_net_ubuf_ref
*ubufs
= ubuf
->ctx
;
268 struct vhost_virtqueue
*vq
= ubufs
->vq
;
273 /* set len to mark this desc buffers done DMA */
274 vq
->heads
[ubuf
->desc
].len
= success
?
275 VHOST_DMA_DONE_LEN
: VHOST_DMA_FAILED_LEN
;
276 cnt
= vhost_net_ubuf_put(ubufs
);
279 * Trigger polling thread if guest stopped submitting new buffers:
280 * in this case, the refcount after decrement will eventually reach 1.
281 * We also trigger polling periodically after each 16 packets
282 * (the value 16 here is more or less arbitrary, it's tuned to trigger
283 * less than 10% of times).
285 if (cnt
<= 1 || !(cnt
% 16))
286 vhost_poll_queue(&vq
->poll
);
288 rcu_read_unlock_bh();
291 static inline unsigned long busy_clock(void)
293 return local_clock() >> 10;
296 static bool vhost_can_busy_poll(struct vhost_dev
*dev
,
297 unsigned long endtime
)
299 return likely(!need_resched()) &&
300 likely(!time_after(busy_clock(), endtime
)) &&
301 likely(!signal_pending(current
)) &&
302 !vhost_has_work(dev
);
305 static int vhost_net_tx_get_vq_desc(struct vhost_net
*net
,
306 struct vhost_virtqueue
*vq
,
307 struct iovec iov
[], unsigned int iov_size
,
308 unsigned int *out_num
, unsigned int *in_num
)
310 unsigned long uninitialized_var(endtime
);
311 int r
= vhost_get_vq_desc(vq
, vq
->iov
, ARRAY_SIZE(vq
->iov
),
312 out_num
, in_num
, NULL
, NULL
);
314 if (r
== vq
->num
&& vq
->busyloop_timeout
) {
316 endtime
= busy_clock() + vq
->busyloop_timeout
;
317 while (vhost_can_busy_poll(vq
->dev
, endtime
) &&
318 vhost_vq_avail_empty(vq
->dev
, vq
))
319 cpu_relax_lowlatency();
321 r
= vhost_get_vq_desc(vq
, vq
->iov
, ARRAY_SIZE(vq
->iov
),
322 out_num
, in_num
, NULL
, NULL
);
328 /* Expects to be always run from workqueue - which acts as
329 * read-size critical section for our kind of RCU. */
330 static void handle_tx(struct vhost_net
*net
)
332 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_TX
];
333 struct vhost_virtqueue
*vq
= &nvq
->vq
;
336 struct msghdr msg
= {
341 .msg_flags
= MSG_DONTWAIT
,
343 size_t len
, total_len
= 0;
347 struct vhost_net_ubuf_ref
*uninitialized_var(ubufs
);
348 bool zcopy
, zcopy_used
;
350 mutex_lock(&vq
->mutex
);
351 sock
= vq
->private_data
;
355 if (!vq_iotlb_prefetch(vq
))
358 vhost_disable_notify(&net
->dev
, vq
);
360 hdr_size
= nvq
->vhost_hlen
;
364 /* Release DMAs done buffers first */
366 vhost_zerocopy_signal_used(net
, vq
);
368 /* If more outstanding DMAs, queue the work.
369 * Handle upend_idx wrap around
371 if (unlikely((nvq
->upend_idx
+ vq
->num
- VHOST_MAX_PEND
)
372 % UIO_MAXIOV
== nvq
->done_idx
))
375 head
= vhost_net_tx_get_vq_desc(net
, vq
, vq
->iov
,
378 /* On error, stop handling until the next kick. */
379 if (unlikely(head
< 0))
381 /* Nothing new? Wait for eventfd to tell us they refilled. */
382 if (head
== vq
->num
) {
383 if (unlikely(vhost_enable_notify(&net
->dev
, vq
))) {
384 vhost_disable_notify(&net
->dev
, vq
);
390 vq_err(vq
, "Unexpected descriptor format for TX: "
391 "out %d, int %d\n", out
, in
);
394 /* Skip header. TODO: support TSO. */
395 len
= iov_length(vq
->iov
, out
);
396 iov_iter_init(&msg
.msg_iter
, WRITE
, vq
->iov
, out
, len
);
397 iov_iter_advance(&msg
.msg_iter
, hdr_size
);
399 if (!msg_data_left(&msg
)) {
400 vq_err(vq
, "Unexpected header len for TX: "
401 "%zd expected %zd\n",
405 len
= msg_data_left(&msg
);
407 zcopy_used
= zcopy
&& len
>= VHOST_GOODCOPY_LEN
408 && (nvq
->upend_idx
+ 1) % UIO_MAXIOV
!=
410 && vhost_net_tx_select_zcopy(net
);
412 /* use msg_control to pass vhost zerocopy ubuf info to skb */
414 struct ubuf_info
*ubuf
;
415 ubuf
= nvq
->ubuf_info
+ nvq
->upend_idx
;
417 vq
->heads
[nvq
->upend_idx
].id
= cpu_to_vhost32(vq
, head
);
418 vq
->heads
[nvq
->upend_idx
].len
= VHOST_DMA_IN_PROGRESS
;
419 ubuf
->callback
= vhost_zerocopy_callback
;
420 ubuf
->ctx
= nvq
->ubufs
;
421 ubuf
->desc
= nvq
->upend_idx
;
422 msg
.msg_control
= ubuf
;
423 msg
.msg_controllen
= sizeof(ubuf
);
425 atomic_inc(&ubufs
->refcount
);
426 nvq
->upend_idx
= (nvq
->upend_idx
+ 1) % UIO_MAXIOV
;
428 msg
.msg_control
= NULL
;
431 /* TODO: Check specific error and bomb out unless ENOBUFS? */
432 err
= sock
->ops
->sendmsg(sock
, &msg
, len
);
433 if (unlikely(err
< 0)) {
435 vhost_net_ubuf_put(ubufs
);
436 nvq
->upend_idx
= ((unsigned)nvq
->upend_idx
- 1)
439 vhost_discard_vq_desc(vq
, 1);
443 pr_debug("Truncated TX packet: "
444 " len %d != %zd\n", err
, len
);
446 vhost_add_used_and_signal(&net
->dev
, vq
, head
, 0);
448 vhost_zerocopy_signal_used(net
, vq
);
450 vhost_net_tx_packet(net
);
451 if (unlikely(total_len
>= VHOST_NET_WEIGHT
)) {
452 vhost_poll_queue(&vq
->poll
);
457 mutex_unlock(&vq
->mutex
);
460 static int peek_head_len(struct sock
*sk
)
462 struct sk_buff
*head
;
466 spin_lock_irqsave(&sk
->sk_receive_queue
.lock
, flags
);
467 head
= skb_peek(&sk
->sk_receive_queue
);
470 if (skb_vlan_tag_present(head
))
474 spin_unlock_irqrestore(&sk
->sk_receive_queue
.lock
, flags
);
478 static int vhost_net_rx_peek_head_len(struct vhost_net
*net
, struct sock
*sk
)
480 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_TX
];
481 struct vhost_virtqueue
*vq
= &nvq
->vq
;
482 unsigned long uninitialized_var(endtime
);
483 int len
= peek_head_len(sk
);
485 if (!len
&& vq
->busyloop_timeout
) {
486 /* Both tx vq and rx socket were polled here */
487 mutex_lock(&vq
->mutex
);
488 vhost_disable_notify(&net
->dev
, vq
);
491 endtime
= busy_clock() + vq
->busyloop_timeout
;
493 while (vhost_can_busy_poll(&net
->dev
, endtime
) &&
494 skb_queue_empty(&sk
->sk_receive_queue
) &&
495 vhost_vq_avail_empty(&net
->dev
, vq
))
496 cpu_relax_lowlatency();
500 if (vhost_enable_notify(&net
->dev
, vq
))
501 vhost_poll_queue(&vq
->poll
);
502 mutex_unlock(&vq
->mutex
);
504 len
= peek_head_len(sk
);
510 /* This is a multi-buffer version of vhost_get_desc, that works if
511 * vq has read descriptors only.
512 * @vq - the relevant virtqueue
513 * @datalen - data length we'll be reading
514 * @iovcount - returned count of io vectors we fill
516 * @log_num - log offset
517 * @quota - headcount quota, 1 for big buffer
518 * returns number of buffer heads allocated, negative on error
520 static int get_rx_bufs(struct vhost_virtqueue
*vq
,
521 struct vring_used_elem
*heads
,
524 struct vhost_log
*log
,
528 unsigned int out
, in
;
533 /* len is always initialized before use since we are always called with
536 u32
uninitialized_var(len
);
538 while (datalen
> 0 && headcount
< quota
) {
539 if (unlikely(seg
>= UIO_MAXIOV
)) {
543 r
= vhost_get_vq_desc(vq
, vq
->iov
+ seg
,
544 ARRAY_SIZE(vq
->iov
) - seg
, &out
,
554 if (unlikely(out
|| in
<= 0)) {
555 vq_err(vq
, "unexpected descriptor format for RX: "
556 "out %d, in %d\n", out
, in
);
564 heads
[headcount
].id
= cpu_to_vhost32(vq
, d
);
565 len
= iov_length(vq
->iov
+ seg
, in
);
566 heads
[headcount
].len
= cpu_to_vhost32(vq
, len
);
571 heads
[headcount
- 1].len
= cpu_to_vhost32(vq
, len
+ datalen
);
577 if (unlikely(datalen
> 0)) {
583 vhost_discard_vq_desc(vq
, headcount
);
587 /* Expects to be always run from workqueue - which acts as
588 * read-size critical section for our kind of RCU. */
589 static void handle_rx(struct vhost_net
*net
)
591 struct vhost_net_virtqueue
*nvq
= &net
->vqs
[VHOST_NET_VQ_RX
];
592 struct vhost_virtqueue
*vq
= &nvq
->vq
;
593 unsigned uninitialized_var(in
), log
;
594 struct vhost_log
*vq_log
;
595 struct msghdr msg
= {
598 .msg_control
= NULL
, /* FIXME: get and handle RX aux data. */
600 .msg_flags
= MSG_DONTWAIT
,
602 struct virtio_net_hdr hdr
= {
604 .gso_type
= VIRTIO_NET_HDR_GSO_NONE
606 size_t total_len
= 0;
609 size_t vhost_hlen
, sock_hlen
;
610 size_t vhost_len
, sock_len
;
612 struct iov_iter fixup
;
613 __virtio16 num_buffers
;
615 mutex_lock(&vq
->mutex
);
616 sock
= vq
->private_data
;
620 if (!vq_iotlb_prefetch(vq
))
623 vhost_disable_notify(&net
->dev
, vq
);
625 vhost_hlen
= nvq
->vhost_hlen
;
626 sock_hlen
= nvq
->sock_hlen
;
628 vq_log
= unlikely(vhost_has_feature(vq
, VHOST_F_LOG_ALL
)) ?
630 mergeable
= vhost_has_feature(vq
, VIRTIO_NET_F_MRG_RXBUF
);
632 while ((sock_len
= vhost_net_rx_peek_head_len(net
, sock
->sk
))) {
633 sock_len
+= sock_hlen
;
634 vhost_len
= sock_len
+ vhost_hlen
;
635 headcount
= get_rx_bufs(vq
, vq
->heads
, vhost_len
,
637 likely(mergeable
) ? UIO_MAXIOV
: 1);
638 /* On error, stop handling until the next kick. */
639 if (unlikely(headcount
< 0))
641 /* On overrun, truncate and discard */
642 if (unlikely(headcount
> UIO_MAXIOV
)) {
643 iov_iter_init(&msg
.msg_iter
, READ
, vq
->iov
, 1, 1);
644 err
= sock
->ops
->recvmsg(sock
, &msg
,
645 1, MSG_DONTWAIT
| MSG_TRUNC
);
646 pr_debug("Discarded rx packet: len %zd\n", sock_len
);
649 /* OK, now we need to know about added descriptors. */
651 if (unlikely(vhost_enable_notify(&net
->dev
, vq
))) {
652 /* They have slipped one in as we were
653 * doing that: check again. */
654 vhost_disable_notify(&net
->dev
, vq
);
657 /* Nothing new? Wait for eventfd to tell us
661 /* We don't need to be notified again. */
662 iov_iter_init(&msg
.msg_iter
, READ
, vq
->iov
, in
, vhost_len
);
663 fixup
= msg
.msg_iter
;
664 if (unlikely((vhost_hlen
))) {
665 /* We will supply the header ourselves
668 iov_iter_advance(&msg
.msg_iter
, vhost_hlen
);
670 err
= sock
->ops
->recvmsg(sock
, &msg
,
671 sock_len
, MSG_DONTWAIT
| MSG_TRUNC
);
672 /* Userspace might have consumed the packet meanwhile:
673 * it's not supposed to do this usually, but might be hard
674 * to prevent. Discard data we got (if any) and keep going. */
675 if (unlikely(err
!= sock_len
)) {
676 pr_debug("Discarded rx packet: "
677 " len %d, expected %zd\n", err
, sock_len
);
678 vhost_discard_vq_desc(vq
, headcount
);
681 /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
682 if (unlikely(vhost_hlen
)) {
683 if (copy_to_iter(&hdr
, sizeof(hdr
),
684 &fixup
) != sizeof(hdr
)) {
685 vq_err(vq
, "Unable to write vnet_hdr "
686 "at addr %p\n", vq
->iov
->iov_base
);
690 /* Header came from socket; we'll need to patch
691 * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
693 iov_iter_advance(&fixup
, sizeof(hdr
));
695 /* TODO: Should check and handle checksum. */
697 num_buffers
= cpu_to_vhost16(vq
, headcount
);
698 if (likely(mergeable
) &&
699 copy_to_iter(&num_buffers
, sizeof num_buffers
,
700 &fixup
) != sizeof num_buffers
) {
701 vq_err(vq
, "Failed num_buffers write");
702 vhost_discard_vq_desc(vq
, headcount
);
705 vhost_add_used_and_signal_n(&net
->dev
, vq
, vq
->heads
,
707 if (unlikely(vq_log
))
708 vhost_log_write(vq
, vq_log
, log
, vhost_len
);
709 total_len
+= vhost_len
;
710 if (unlikely(total_len
>= VHOST_NET_WEIGHT
)) {
711 vhost_poll_queue(&vq
->poll
);
716 mutex_unlock(&vq
->mutex
);
719 static void handle_tx_kick(struct vhost_work
*work
)
721 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
723 struct vhost_net
*net
= container_of(vq
->dev
, struct vhost_net
, dev
);
728 static void handle_rx_kick(struct vhost_work
*work
)
730 struct vhost_virtqueue
*vq
= container_of(work
, struct vhost_virtqueue
,
732 struct vhost_net
*net
= container_of(vq
->dev
, struct vhost_net
, dev
);
737 static void handle_tx_net(struct vhost_work
*work
)
739 struct vhost_net
*net
= container_of(work
, struct vhost_net
,
740 poll
[VHOST_NET_VQ_TX
].work
);
744 static void handle_rx_net(struct vhost_work
*work
)
746 struct vhost_net
*net
= container_of(work
, struct vhost_net
,
747 poll
[VHOST_NET_VQ_RX
].work
);
751 static int vhost_net_open(struct inode
*inode
, struct file
*f
)
754 struct vhost_dev
*dev
;
755 struct vhost_virtqueue
**vqs
;
758 n
= kmalloc(sizeof *n
, GFP_KERNEL
| __GFP_NOWARN
| __GFP_REPEAT
);
760 n
= vmalloc(sizeof *n
);
764 vqs
= kmalloc(VHOST_NET_VQ_MAX
* sizeof(*vqs
), GFP_KERNEL
);
771 vqs
[VHOST_NET_VQ_TX
] = &n
->vqs
[VHOST_NET_VQ_TX
].vq
;
772 vqs
[VHOST_NET_VQ_RX
] = &n
->vqs
[VHOST_NET_VQ_RX
].vq
;
773 n
->vqs
[VHOST_NET_VQ_TX
].vq
.handle_kick
= handle_tx_kick
;
774 n
->vqs
[VHOST_NET_VQ_RX
].vq
.handle_kick
= handle_rx_kick
;
775 for (i
= 0; i
< VHOST_NET_VQ_MAX
; i
++) {
776 n
->vqs
[i
].ubufs
= NULL
;
777 n
->vqs
[i
].ubuf_info
= NULL
;
778 n
->vqs
[i
].upend_idx
= 0;
779 n
->vqs
[i
].done_idx
= 0;
780 n
->vqs
[i
].vhost_hlen
= 0;
781 n
->vqs
[i
].sock_hlen
= 0;
783 vhost_dev_init(dev
, vqs
, VHOST_NET_VQ_MAX
);
785 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_TX
, handle_tx_net
, POLLOUT
, dev
);
786 vhost_poll_init(n
->poll
+ VHOST_NET_VQ_RX
, handle_rx_net
, POLLIN
, dev
);
793 static void vhost_net_disable_vq(struct vhost_net
*n
,
794 struct vhost_virtqueue
*vq
)
796 struct vhost_net_virtqueue
*nvq
=
797 container_of(vq
, struct vhost_net_virtqueue
, vq
);
798 struct vhost_poll
*poll
= n
->poll
+ (nvq
- n
->vqs
);
799 if (!vq
->private_data
)
801 vhost_poll_stop(poll
);
804 static int vhost_net_enable_vq(struct vhost_net
*n
,
805 struct vhost_virtqueue
*vq
)
807 struct vhost_net_virtqueue
*nvq
=
808 container_of(vq
, struct vhost_net_virtqueue
, vq
);
809 struct vhost_poll
*poll
= n
->poll
+ (nvq
- n
->vqs
);
812 sock
= vq
->private_data
;
816 return vhost_poll_start(poll
, sock
->file
);
819 static struct socket
*vhost_net_stop_vq(struct vhost_net
*n
,
820 struct vhost_virtqueue
*vq
)
824 mutex_lock(&vq
->mutex
);
825 sock
= vq
->private_data
;
826 vhost_net_disable_vq(n
, vq
);
827 vq
->private_data
= NULL
;
828 mutex_unlock(&vq
->mutex
);
832 static void vhost_net_stop(struct vhost_net
*n
, struct socket
**tx_sock
,
833 struct socket
**rx_sock
)
835 *tx_sock
= vhost_net_stop_vq(n
, &n
->vqs
[VHOST_NET_VQ_TX
].vq
);
836 *rx_sock
= vhost_net_stop_vq(n
, &n
->vqs
[VHOST_NET_VQ_RX
].vq
);
839 static void vhost_net_flush_vq(struct vhost_net
*n
, int index
)
841 vhost_poll_flush(n
->poll
+ index
);
842 vhost_poll_flush(&n
->vqs
[index
].vq
.poll
);
845 static void vhost_net_flush(struct vhost_net
*n
)
847 vhost_net_flush_vq(n
, VHOST_NET_VQ_TX
);
848 vhost_net_flush_vq(n
, VHOST_NET_VQ_RX
);
849 if (n
->vqs
[VHOST_NET_VQ_TX
].ubufs
) {
850 mutex_lock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
852 mutex_unlock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
853 /* Wait for all lower device DMAs done. */
854 vhost_net_ubuf_put_and_wait(n
->vqs
[VHOST_NET_VQ_TX
].ubufs
);
855 mutex_lock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
857 atomic_set(&n
->vqs
[VHOST_NET_VQ_TX
].ubufs
->refcount
, 1);
858 mutex_unlock(&n
->vqs
[VHOST_NET_VQ_TX
].vq
.mutex
);
862 static int vhost_net_release(struct inode
*inode
, struct file
*f
)
864 struct vhost_net
*n
= f
->private_data
;
865 struct socket
*tx_sock
;
866 struct socket
*rx_sock
;
868 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
870 vhost_dev_stop(&n
->dev
);
871 vhost_dev_cleanup(&n
->dev
, false);
872 vhost_net_vq_reset(n
);
877 /* Make sure no callbacks are outstanding */
878 synchronize_rcu_bh();
879 /* We do an extra flush before freeing memory,
880 * since jobs can re-queue themselves. */
887 static struct socket
*get_raw_socket(int fd
)
890 struct sockaddr_ll sa
;
891 char buf
[MAX_ADDR_LEN
];
893 int uaddr_len
= sizeof uaddr
, r
;
894 struct socket
*sock
= sockfd_lookup(fd
, &r
);
897 return ERR_PTR(-ENOTSOCK
);
899 /* Parameter checking */
900 if (sock
->sk
->sk_type
!= SOCK_RAW
) {
901 r
= -ESOCKTNOSUPPORT
;
905 r
= sock
->ops
->getname(sock
, (struct sockaddr
*)&uaddr
.sa
,
910 if (uaddr
.sa
.sll_family
!= AF_PACKET
) {
920 static struct socket
*get_tap_socket(int fd
)
922 struct file
*file
= fget(fd
);
926 return ERR_PTR(-EBADF
);
927 sock
= tun_get_socket(file
);
930 sock
= macvtap_get_socket(file
);
936 static struct socket
*get_socket(int fd
)
940 /* special case to disable backend */
943 sock
= get_raw_socket(fd
);
946 sock
= get_tap_socket(fd
);
949 return ERR_PTR(-ENOTSOCK
);
952 static long vhost_net_set_backend(struct vhost_net
*n
, unsigned index
, int fd
)
954 struct socket
*sock
, *oldsock
;
955 struct vhost_virtqueue
*vq
;
956 struct vhost_net_virtqueue
*nvq
;
957 struct vhost_net_ubuf_ref
*ubufs
, *oldubufs
= NULL
;
960 mutex_lock(&n
->dev
.mutex
);
961 r
= vhost_dev_check_owner(&n
->dev
);
965 if (index
>= VHOST_NET_VQ_MAX
) {
969 vq
= &n
->vqs
[index
].vq
;
970 nvq
= &n
->vqs
[index
];
971 mutex_lock(&vq
->mutex
);
973 /* Verify that ring has been setup correctly. */
974 if (!vhost_vq_access_ok(vq
)) {
978 sock
= get_socket(fd
);
984 /* start polling new socket */
985 oldsock
= vq
->private_data
;
986 if (sock
!= oldsock
) {
987 ubufs
= vhost_net_ubuf_alloc(vq
,
988 sock
&& vhost_sock_zcopy(sock
));
994 vhost_net_disable_vq(n
, vq
);
995 vq
->private_data
= sock
;
996 r
= vhost_vq_init_access(vq
);
999 r
= vhost_net_enable_vq(n
, vq
);
1003 oldubufs
= nvq
->ubufs
;
1007 n
->tx_zcopy_err
= 0;
1008 n
->tx_flush
= false;
1011 mutex_unlock(&vq
->mutex
);
1014 vhost_net_ubuf_put_wait_and_free(oldubufs
);
1015 mutex_lock(&vq
->mutex
);
1016 vhost_zerocopy_signal_used(n
, vq
);
1017 mutex_unlock(&vq
->mutex
);
1021 vhost_net_flush_vq(n
, index
);
1022 sockfd_put(oldsock
);
1025 mutex_unlock(&n
->dev
.mutex
);
1029 vq
->private_data
= oldsock
;
1030 vhost_net_enable_vq(n
, vq
);
1032 vhost_net_ubuf_put_wait_and_free(ubufs
);
1036 mutex_unlock(&vq
->mutex
);
1038 mutex_unlock(&n
->dev
.mutex
);
1042 static long vhost_net_reset_owner(struct vhost_net
*n
)
1044 struct socket
*tx_sock
= NULL
;
1045 struct socket
*rx_sock
= NULL
;
1047 struct vhost_umem
*umem
;
1049 mutex_lock(&n
->dev
.mutex
);
1050 err
= vhost_dev_check_owner(&n
->dev
);
1053 umem
= vhost_dev_reset_owner_prepare();
1058 vhost_net_stop(n
, &tx_sock
, &rx_sock
);
1060 vhost_dev_reset_owner(&n
->dev
, umem
);
1061 vhost_net_vq_reset(n
);
1063 mutex_unlock(&n
->dev
.mutex
);
1065 sockfd_put(tx_sock
);
1067 sockfd_put(rx_sock
);
1071 static int vhost_net_set_features(struct vhost_net
*n
, u64 features
)
1073 size_t vhost_hlen
, sock_hlen
, hdr_len
;
1076 hdr_len
= (features
& ((1ULL << VIRTIO_NET_F_MRG_RXBUF
) |
1077 (1ULL << VIRTIO_F_VERSION_1
))) ?
1078 sizeof(struct virtio_net_hdr_mrg_rxbuf
) :
1079 sizeof(struct virtio_net_hdr
);
1080 if (features
& (1 << VHOST_NET_F_VIRTIO_NET_HDR
)) {
1081 /* vhost provides vnet_hdr */
1082 vhost_hlen
= hdr_len
;
1085 /* socket provides vnet_hdr */
1087 sock_hlen
= hdr_len
;
1089 mutex_lock(&n
->dev
.mutex
);
1090 if ((features
& (1 << VHOST_F_LOG_ALL
)) &&
1091 !vhost_log_access_ok(&n
->dev
))
1094 if ((features
& (1ULL << VIRTIO_F_IOMMU_PLATFORM
))) {
1095 if (vhost_init_device_iotlb(&n
->dev
, true))
1099 for (i
= 0; i
< VHOST_NET_VQ_MAX
; ++i
) {
1100 mutex_lock(&n
->vqs
[i
].vq
.mutex
);
1101 n
->vqs
[i
].vq
.acked_features
= features
;
1102 n
->vqs
[i
].vhost_hlen
= vhost_hlen
;
1103 n
->vqs
[i
].sock_hlen
= sock_hlen
;
1104 mutex_unlock(&n
->vqs
[i
].vq
.mutex
);
1106 mutex_unlock(&n
->dev
.mutex
);
1110 mutex_unlock(&n
->dev
.mutex
);
1114 static long vhost_net_set_owner(struct vhost_net
*n
)
1118 mutex_lock(&n
->dev
.mutex
);
1119 if (vhost_dev_has_owner(&n
->dev
)) {
1123 r
= vhost_net_set_ubuf_info(n
);
1126 r
= vhost_dev_set_owner(&n
->dev
);
1128 vhost_net_clear_ubuf_info(n
);
1131 mutex_unlock(&n
->dev
.mutex
);
1135 static long vhost_net_ioctl(struct file
*f
, unsigned int ioctl
,
1138 struct vhost_net
*n
= f
->private_data
;
1139 void __user
*argp
= (void __user
*)arg
;
1140 u64 __user
*featurep
= argp
;
1141 struct vhost_vring_file backend
;
1146 case VHOST_NET_SET_BACKEND
:
1147 if (copy_from_user(&backend
, argp
, sizeof backend
))
1149 return vhost_net_set_backend(n
, backend
.index
, backend
.fd
);
1150 case VHOST_GET_FEATURES
:
1151 features
= VHOST_NET_FEATURES
;
1152 if (copy_to_user(featurep
, &features
, sizeof features
))
1155 case VHOST_SET_FEATURES
:
1156 if (copy_from_user(&features
, featurep
, sizeof features
))
1158 if (features
& ~VHOST_NET_FEATURES
)
1160 return vhost_net_set_features(n
, features
);
1161 case VHOST_RESET_OWNER
:
1162 return vhost_net_reset_owner(n
);
1163 case VHOST_SET_OWNER
:
1164 return vhost_net_set_owner(n
);
1166 mutex_lock(&n
->dev
.mutex
);
1167 r
= vhost_dev_ioctl(&n
->dev
, ioctl
, argp
);
1168 if (r
== -ENOIOCTLCMD
)
1169 r
= vhost_vring_ioctl(&n
->dev
, ioctl
, argp
);
1172 mutex_unlock(&n
->dev
.mutex
);
1177 #ifdef CONFIG_COMPAT
1178 static long vhost_net_compat_ioctl(struct file
*f
, unsigned int ioctl
,
1181 return vhost_net_ioctl(f
, ioctl
, (unsigned long)compat_ptr(arg
));
1185 static ssize_t
vhost_net_chr_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1187 struct file
*file
= iocb
->ki_filp
;
1188 struct vhost_net
*n
= file
->private_data
;
1189 struct vhost_dev
*dev
= &n
->dev
;
1190 int noblock
= file
->f_flags
& O_NONBLOCK
;
1192 return vhost_chr_read_iter(dev
, to
, noblock
);
1195 static ssize_t
vhost_net_chr_write_iter(struct kiocb
*iocb
,
1196 struct iov_iter
*from
)
1198 struct file
*file
= iocb
->ki_filp
;
1199 struct vhost_net
*n
= file
->private_data
;
1200 struct vhost_dev
*dev
= &n
->dev
;
1202 return vhost_chr_write_iter(dev
, from
);
1205 static unsigned int vhost_net_chr_poll(struct file
*file
, poll_table
*wait
)
1207 struct vhost_net
*n
= file
->private_data
;
1208 struct vhost_dev
*dev
= &n
->dev
;
1210 return vhost_chr_poll(file
, dev
, wait
);
1213 static const struct file_operations vhost_net_fops
= {
1214 .owner
= THIS_MODULE
,
1215 .release
= vhost_net_release
,
1216 .read_iter
= vhost_net_chr_read_iter
,
1217 .write_iter
= vhost_net_chr_write_iter
,
1218 .poll
= vhost_net_chr_poll
,
1219 .unlocked_ioctl
= vhost_net_ioctl
,
1220 #ifdef CONFIG_COMPAT
1221 .compat_ioctl
= vhost_net_compat_ioctl
,
1223 .open
= vhost_net_open
,
1224 .llseek
= noop_llseek
,
1227 static struct miscdevice vhost_net_misc
= {
1228 .minor
= VHOST_NET_MINOR
,
1229 .name
= "vhost-net",
1230 .fops
= &vhost_net_fops
,
1233 static int vhost_net_init(void)
1235 if (experimental_zcopytx
)
1236 vhost_net_enable_zcopy(VHOST_NET_VQ_TX
);
1237 return misc_register(&vhost_net_misc
);
1239 module_init(vhost_net_init
);
1241 static void vhost_net_exit(void)
1243 misc_deregister(&vhost_net_misc
);
1245 module_exit(vhost_net_exit
);
1247 MODULE_VERSION("0.0.1");
1248 MODULE_LICENSE("GPL v2");
1249 MODULE_AUTHOR("Michael S. Tsirkin");
1250 MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
1251 MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR
);
1252 MODULE_ALIAS("devname:vhost-net");