1 // SPDX-License-Identifier: GPL-2.0-only
3 * vhost transport for vsock
5 * Copyright (C) 2013-2015 Red Hat, Inc.
6 * Author: Asias He <asias@redhat.com>
7 * Stefan Hajnoczi <stefanha@redhat.com>
9 #include <linux/miscdevice.h>
10 #include <linux/atomic.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/vmalloc.h>
15 #include <linux/virtio_vsock.h>
16 #include <linux/vhost.h>
17 #include <linux/hashtable.h>
19 #include <net/af_vsock.h>
22 #define VHOST_VSOCK_DEFAULT_HOST_CID 2
23 /* Max number of bytes transferred before requeueing the job.
24 * Using this limit prevents one virtqueue from starving others. */
25 #define VHOST_VSOCK_WEIGHT 0x80000
26 /* Max number of packets transferred before requeueing the job.
27 * Using this limit prevents one virtqueue from starving others with
30 #define VHOST_VSOCK_PKT_WEIGHT 256
33 VHOST_VSOCK_FEATURES = VHOST_FEATURES,
36 /* Used to track all the vhost_vsock instances on the system. */
37 static DEFINE_MUTEX(vhost_vsock_mutex);
38 static DEFINE_READ_MOSTLY_HASHTABLE(vhost_vsock_hash, 8);
42 struct vhost_virtqueue vqs[2];
44 /* Link to global vhost_vsock_hash, writes use vhost_vsock_mutex */
45 struct hlist_node hash;
47 struct vhost_work send_pkt_work;
48 spinlock_t send_pkt_list_lock;
49 struct list_head send_pkt_list; /* host->guest pending packets */
51 atomic_t queued_replies;
56 static u32 vhost_transport_get_local_cid(void)
58 return VHOST_VSOCK_DEFAULT_HOST_CID;
61 /* Callers that dereference the return value must hold vhost_vsock_mutex or the
64 static struct vhost_vsock *vhost_vsock_get(u32 guest_cid)
66 struct vhost_vsock *vsock;
68 hash_for_each_possible_rcu(vhost_vsock_hash, vsock, hash, guest_cid) {
69 u32 other_cid = vsock->guest_cid;
71 /* Skip instances that have no CID yet */
75 if (other_cid == guest_cid)
84 vhost_transport_do_send_pkt(struct vhost_vsock *vsock,
85 struct vhost_virtqueue *vq)
87 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
88 int pkts = 0, total_len = 0;
90 bool restart_tx = false;
92 mutex_lock(&vq->mutex);
94 if (!vq->private_data)
97 /* Avoid further vmexits, we're already processing the virtqueue */
98 vhost_disable_notify(&vsock->dev, vq);
101 struct virtio_vsock_pkt *pkt;
102 struct iov_iter iov_iter;
105 size_t iov_len, payload_len;
108 spin_lock_bh(&vsock->send_pkt_list_lock);
109 if (list_empty(&vsock->send_pkt_list)) {
110 spin_unlock_bh(&vsock->send_pkt_list_lock);
111 vhost_enable_notify(&vsock->dev, vq);
115 pkt = list_first_entry(&vsock->send_pkt_list,
116 struct virtio_vsock_pkt, list);
117 list_del_init(&pkt->list);
118 spin_unlock_bh(&vsock->send_pkt_list_lock);
120 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
121 &out, &in, NULL, NULL);
123 spin_lock_bh(&vsock->send_pkt_list_lock);
124 list_add(&pkt->list, &vsock->send_pkt_list);
125 spin_unlock_bh(&vsock->send_pkt_list_lock);
129 if (head == vq->num) {
130 spin_lock_bh(&vsock->send_pkt_list_lock);
131 list_add(&pkt->list, &vsock->send_pkt_list);
132 spin_unlock_bh(&vsock->send_pkt_list_lock);
134 /* We cannot finish yet if more buffers snuck in while
135 * re-enabling notify.
137 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
138 vhost_disable_notify(&vsock->dev, vq);
145 virtio_transport_free_pkt(pkt);
146 vq_err(vq, "Expected 0 output buffers, got %u\n", out);
150 iov_len = iov_length(&vq->iov[out], in);
151 if (iov_len < sizeof(pkt->hdr)) {
152 virtio_transport_free_pkt(pkt);
153 vq_err(vq, "Buffer len [%zu] too small\n", iov_len);
157 iov_iter_init(&iov_iter, READ, &vq->iov[out], in, iov_len);
158 payload_len = pkt->len - pkt->off;
160 /* If the packet is greater than the space available in the
161 * buffer, we split it using multiple buffers.
163 if (payload_len > iov_len - sizeof(pkt->hdr))
164 payload_len = iov_len - sizeof(pkt->hdr);
166 /* Set the correct length in the header */
167 pkt->hdr.len = cpu_to_le32(payload_len);
169 nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
170 if (nbytes != sizeof(pkt->hdr)) {
171 virtio_transport_free_pkt(pkt);
172 vq_err(vq, "Faulted on copying pkt hdr\n");
176 nbytes = copy_to_iter(pkt->buf + pkt->off, payload_len,
178 if (nbytes != payload_len) {
179 virtio_transport_free_pkt(pkt);
180 vq_err(vq, "Faulted on copying pkt buf\n");
184 vhost_add_used(vq, head, sizeof(pkt->hdr) + payload_len);
187 /* Deliver to monitoring devices all correctly transmitted
190 virtio_transport_deliver_tap_pkt(pkt);
192 pkt->off += payload_len;
193 total_len += payload_len;
195 /* If we didn't send all the payload we can requeue the packet
196 * to send it with the next available buffer.
198 if (pkt->off < pkt->len) {
199 spin_lock_bh(&vsock->send_pkt_list_lock);
200 list_add(&pkt->list, &vsock->send_pkt_list);
201 spin_unlock_bh(&vsock->send_pkt_list_lock);
206 val = atomic_dec_return(&vsock->queued_replies);
208 /* Do we have resources to resume tx
211 if (val + 1 == tx_vq->num)
215 virtio_transport_free_pkt(pkt);
217 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
219 vhost_signal(&vsock->dev, vq);
222 mutex_unlock(&vq->mutex);
225 vhost_poll_queue(&tx_vq->poll);
228 static void vhost_transport_send_pkt_work(struct vhost_work *work)
230 struct vhost_virtqueue *vq;
231 struct vhost_vsock *vsock;
233 vsock = container_of(work, struct vhost_vsock, send_pkt_work);
234 vq = &vsock->vqs[VSOCK_VQ_RX];
236 vhost_transport_do_send_pkt(vsock, vq);
240 vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
242 struct vhost_vsock *vsock;
247 /* Find the vhost_vsock according to guest context id */
248 vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
251 virtio_transport_free_pkt(pkt);
256 atomic_inc(&vsock->queued_replies);
258 spin_lock_bh(&vsock->send_pkt_list_lock);
259 list_add_tail(&pkt->list, &vsock->send_pkt_list);
260 spin_unlock_bh(&vsock->send_pkt_list_lock);
262 vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
269 vhost_transport_cancel_pkt(struct vsock_sock *vsk)
271 struct vhost_vsock *vsock;
272 struct virtio_vsock_pkt *pkt, *n;
279 /* Find the vhost_vsock according to guest context id */
280 vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
284 spin_lock_bh(&vsock->send_pkt_list_lock);
285 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
288 list_move(&pkt->list, &freeme);
290 spin_unlock_bh(&vsock->send_pkt_list_lock);
292 list_for_each_entry_safe(pkt, n, &freeme, list) {
295 list_del(&pkt->list);
296 virtio_transport_free_pkt(pkt);
300 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
303 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
304 if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
305 vhost_poll_queue(&tx_vq->poll);
314 static struct virtio_vsock_pkt *
315 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
316 unsigned int out, unsigned int in)
318 struct virtio_vsock_pkt *pkt;
319 struct iov_iter iov_iter;
324 vq_err(vq, "Expected 0 input buffers, got %u\n", in);
328 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
332 len = iov_length(vq->iov, out);
333 iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
335 nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
336 if (nbytes != sizeof(pkt->hdr)) {
337 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
338 sizeof(pkt->hdr), nbytes);
343 if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
344 pkt->len = le32_to_cpu(pkt->hdr.len);
350 /* The pkt is too big */
351 if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
356 pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
362 pkt->buf_len = pkt->len;
364 nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
365 if (nbytes != pkt->len) {
366 vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
368 virtio_transport_free_pkt(pkt);
375 /* Is there space left for replies to rx packets? */
376 static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
378 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
381 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
382 val = atomic_read(&vsock->queued_replies);
384 return val < vq->num;
387 static struct virtio_transport vhost_transport = {
389 .get_local_cid = vhost_transport_get_local_cid,
391 .init = virtio_transport_do_socket_init,
392 .destruct = virtio_transport_destruct,
393 .release = virtio_transport_release,
394 .connect = virtio_transport_connect,
395 .shutdown = virtio_transport_shutdown,
396 .cancel_pkt = vhost_transport_cancel_pkt,
398 .dgram_enqueue = virtio_transport_dgram_enqueue,
399 .dgram_dequeue = virtio_transport_dgram_dequeue,
400 .dgram_bind = virtio_transport_dgram_bind,
401 .dgram_allow = virtio_transport_dgram_allow,
403 .stream_enqueue = virtio_transport_stream_enqueue,
404 .stream_dequeue = virtio_transport_stream_dequeue,
405 .stream_has_data = virtio_transport_stream_has_data,
406 .stream_has_space = virtio_transport_stream_has_space,
407 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
408 .stream_is_active = virtio_transport_stream_is_active,
409 .stream_allow = virtio_transport_stream_allow,
411 .notify_poll_in = virtio_transport_notify_poll_in,
412 .notify_poll_out = virtio_transport_notify_poll_out,
413 .notify_recv_init = virtio_transport_notify_recv_init,
414 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
415 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
416 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
417 .notify_send_init = virtio_transport_notify_send_init,
418 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
419 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
420 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
421 .notify_buffer_size = virtio_transport_notify_buffer_size,
425 .send_pkt = vhost_transport_send_pkt,
428 static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
430 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
432 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
434 struct virtio_vsock_pkt *pkt;
435 int head, pkts = 0, total_len = 0;
436 unsigned int out, in;
439 mutex_lock(&vq->mutex);
441 if (!vq->private_data)
444 vhost_disable_notify(&vsock->dev, vq);
448 if (!vhost_vsock_more_replies(vsock)) {
449 /* Stop tx until the device processes already
450 * pending replies. Leave tx virtqueue
451 * callbacks disabled.
453 goto no_more_replies;
456 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
457 &out, &in, NULL, NULL);
461 if (head == vq->num) {
462 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
463 vhost_disable_notify(&vsock->dev, vq);
469 pkt = vhost_vsock_alloc_pkt(vq, out, in);
471 vq_err(vq, "Faulted on pkt\n");
477 /* Deliver to monitoring devices all received packets */
478 virtio_transport_deliver_tap_pkt(pkt);
480 /* Only accept correctly addressed packets */
481 if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
482 virtio_transport_recv_pkt(&vhost_transport, pkt);
484 virtio_transport_free_pkt(pkt);
486 len += sizeof(pkt->hdr);
487 vhost_add_used(vq, head, len);
490 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
494 vhost_signal(&vsock->dev, vq);
497 mutex_unlock(&vq->mutex);
500 static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
502 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
504 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
507 vhost_transport_do_send_pkt(vsock, vq);
510 static int vhost_vsock_start(struct vhost_vsock *vsock)
512 struct vhost_virtqueue *vq;
516 mutex_lock(&vsock->dev.mutex);
518 ret = vhost_dev_check_owner(&vsock->dev);
522 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
525 mutex_lock(&vq->mutex);
527 if (!vhost_vq_access_ok(vq)) {
532 if (!vq->private_data) {
533 vq->private_data = vsock;
534 ret = vhost_vq_init_access(vq);
539 mutex_unlock(&vq->mutex);
542 mutex_unlock(&vsock->dev.mutex);
546 vq->private_data = NULL;
547 mutex_unlock(&vq->mutex);
549 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
552 mutex_lock(&vq->mutex);
553 vq->private_data = NULL;
554 mutex_unlock(&vq->mutex);
557 mutex_unlock(&vsock->dev.mutex);
561 static int vhost_vsock_stop(struct vhost_vsock *vsock)
566 mutex_lock(&vsock->dev.mutex);
568 ret = vhost_dev_check_owner(&vsock->dev);
572 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
573 struct vhost_virtqueue *vq = &vsock->vqs[i];
575 mutex_lock(&vq->mutex);
576 vq->private_data = NULL;
577 mutex_unlock(&vq->mutex);
581 mutex_unlock(&vsock->dev.mutex);
585 static void vhost_vsock_free(struct vhost_vsock *vsock)
590 static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
592 struct vhost_virtqueue **vqs;
593 struct vhost_vsock *vsock;
596 /* This struct is large and allocation could fail, fall back to vmalloc
597 * if there is no other way.
599 vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
603 vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
609 vsock->guest_cid = 0; /* no CID assigned yet */
611 atomic_set(&vsock->queued_replies, 0);
613 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
614 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
615 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
616 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
618 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
619 UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,
622 file->private_data = vsock;
623 spin_lock_init(&vsock->send_pkt_list_lock);
624 INIT_LIST_HEAD(&vsock->send_pkt_list);
625 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
629 vhost_vsock_free(vsock);
633 static void vhost_vsock_flush(struct vhost_vsock *vsock)
637 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
638 if (vsock->vqs[i].handle_kick)
639 vhost_poll_flush(&vsock->vqs[i].poll);
640 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
643 static void vhost_vsock_reset_orphans(struct sock *sk)
645 struct vsock_sock *vsk = vsock_sk(sk);
647 /* vmci_transport.c doesn't take sk_lock here either. At least we're
648 * under vsock_table_lock so the sock cannot disappear while we're
652 /* If the peer is still valid, no need to reset connection */
653 if (vhost_vsock_get(vsk->remote_addr.svm_cid))
656 /* If the close timeout is pending, let it expire. This avoids races
657 * with the timeout callback.
659 if (vsk->close_work_scheduled)
662 sock_set_flag(sk, SOCK_DONE);
663 vsk->peer_shutdown = SHUTDOWN_MASK;
664 sk->sk_state = SS_UNCONNECTED;
665 sk->sk_err = ECONNRESET;
666 sk->sk_error_report(sk);
669 static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
671 struct vhost_vsock *vsock = file->private_data;
673 mutex_lock(&vhost_vsock_mutex);
674 if (vsock->guest_cid)
675 hash_del_rcu(&vsock->hash);
676 mutex_unlock(&vhost_vsock_mutex);
678 /* Wait for other CPUs to finish using vsock */
681 /* Iterating over all connections for all CIDs to find orphans is
682 * inefficient. Room for improvement here. */
683 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
685 vhost_vsock_stop(vsock);
686 vhost_vsock_flush(vsock);
687 vhost_dev_stop(&vsock->dev);
689 spin_lock_bh(&vsock->send_pkt_list_lock);
690 while (!list_empty(&vsock->send_pkt_list)) {
691 struct virtio_vsock_pkt *pkt;
693 pkt = list_first_entry(&vsock->send_pkt_list,
694 struct virtio_vsock_pkt, list);
695 list_del_init(&pkt->list);
696 virtio_transport_free_pkt(pkt);
698 spin_unlock_bh(&vsock->send_pkt_list_lock);
700 vhost_dev_cleanup(&vsock->dev);
701 kfree(vsock->dev.vqs);
702 vhost_vsock_free(vsock);
706 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
708 struct vhost_vsock *other;
710 /* Refuse reserved CIDs */
711 if (guest_cid <= VMADDR_CID_HOST ||
712 guest_cid == U32_MAX)
715 /* 64-bit CIDs are not yet supported */
716 if (guest_cid > U32_MAX)
719 /* Refuse if CID is already in use */
720 mutex_lock(&vhost_vsock_mutex);
721 other = vhost_vsock_get(guest_cid);
722 if (other && other != vsock) {
723 mutex_unlock(&vhost_vsock_mutex);
727 if (vsock->guest_cid)
728 hash_del_rcu(&vsock->hash);
730 vsock->guest_cid = guest_cid;
731 hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
732 mutex_unlock(&vhost_vsock_mutex);
737 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
739 struct vhost_virtqueue *vq;
742 if (features & ~VHOST_VSOCK_FEATURES)
745 mutex_lock(&vsock->dev.mutex);
746 if ((features & (1 << VHOST_F_LOG_ALL)) &&
747 !vhost_log_access_ok(&vsock->dev)) {
748 mutex_unlock(&vsock->dev.mutex);
752 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
754 mutex_lock(&vq->mutex);
755 vq->acked_features = features;
756 mutex_unlock(&vq->mutex);
758 mutex_unlock(&vsock->dev.mutex);
762 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
765 struct vhost_vsock *vsock = f->private_data;
766 void __user *argp = (void __user *)arg;
773 case VHOST_VSOCK_SET_GUEST_CID:
774 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
776 return vhost_vsock_set_cid(vsock, guest_cid);
777 case VHOST_VSOCK_SET_RUNNING:
778 if (copy_from_user(&start, argp, sizeof(start)))
781 return vhost_vsock_start(vsock);
783 return vhost_vsock_stop(vsock);
784 case VHOST_GET_FEATURES:
785 features = VHOST_VSOCK_FEATURES;
786 if (copy_to_user(argp, &features, sizeof(features)))
789 case VHOST_SET_FEATURES:
790 if (copy_from_user(&features, argp, sizeof(features)))
792 return vhost_vsock_set_features(vsock, features);
794 mutex_lock(&vsock->dev.mutex);
795 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
796 if (r == -ENOIOCTLCMD)
797 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
799 vhost_vsock_flush(vsock);
800 mutex_unlock(&vsock->dev.mutex);
806 static long vhost_vsock_dev_compat_ioctl(struct file *f, unsigned int ioctl,
809 return vhost_vsock_dev_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
813 static const struct file_operations vhost_vsock_fops = {
814 .owner = THIS_MODULE,
815 .open = vhost_vsock_dev_open,
816 .release = vhost_vsock_dev_release,
817 .llseek = noop_llseek,
818 .unlocked_ioctl = vhost_vsock_dev_ioctl,
820 .compat_ioctl = vhost_vsock_dev_compat_ioctl,
824 static struct miscdevice vhost_vsock_misc = {
825 .minor = VHOST_VSOCK_MINOR,
826 .name = "vhost-vsock",
827 .fops = &vhost_vsock_fops,
830 static int __init vhost_vsock_init(void)
834 ret = vsock_core_init(&vhost_transport.transport);
837 return misc_register(&vhost_vsock_misc);
840 static void __exit vhost_vsock_exit(void)
842 misc_deregister(&vhost_vsock_misc);
846 module_init(vhost_vsock_init);
847 module_exit(vhost_vsock_exit);
848 MODULE_LICENSE("GPL v2");
849 MODULE_AUTHOR("Asias He");
850 MODULE_DESCRIPTION("vhost transport for vsock ");
851 MODULE_ALIAS_MISCDEV(VHOST_VSOCK_MINOR);
852 MODULE_ALIAS("devname:vhost-vsock");