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;
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 len = iov_length(&vq->iov[out], in);
151 iov_iter_init(&iov_iter, READ, &vq->iov[out], in, len);
153 nbytes = copy_to_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
154 if (nbytes != sizeof(pkt->hdr)) {
155 virtio_transport_free_pkt(pkt);
156 vq_err(vq, "Faulted on copying pkt hdr\n");
160 nbytes = copy_to_iter(pkt->buf, pkt->len, &iov_iter);
161 if (nbytes != pkt->len) {
162 virtio_transport_free_pkt(pkt);
163 vq_err(vq, "Faulted on copying pkt buf\n");
167 vhost_add_used(vq, head, sizeof(pkt->hdr) + pkt->len);
173 val = atomic_dec_return(&vsock->queued_replies);
175 /* Do we have resources to resume tx processing? */
176 if (val + 1 == tx_vq->num)
180 /* Deliver to monitoring devices all correctly transmitted
183 virtio_transport_deliver_tap_pkt(pkt);
185 total_len += pkt->len;
186 virtio_transport_free_pkt(pkt);
187 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
189 vhost_signal(&vsock->dev, vq);
192 mutex_unlock(&vq->mutex);
195 vhost_poll_queue(&tx_vq->poll);
198 static void vhost_transport_send_pkt_work(struct vhost_work *work)
200 struct vhost_virtqueue *vq;
201 struct vhost_vsock *vsock;
203 vsock = container_of(work, struct vhost_vsock, send_pkt_work);
204 vq = &vsock->vqs[VSOCK_VQ_RX];
206 vhost_transport_do_send_pkt(vsock, vq);
210 vhost_transport_send_pkt(struct virtio_vsock_pkt *pkt)
212 struct vhost_vsock *vsock;
217 /* Find the vhost_vsock according to guest context id */
218 vsock = vhost_vsock_get(le64_to_cpu(pkt->hdr.dst_cid));
221 virtio_transport_free_pkt(pkt);
226 atomic_inc(&vsock->queued_replies);
228 spin_lock_bh(&vsock->send_pkt_list_lock);
229 list_add_tail(&pkt->list, &vsock->send_pkt_list);
230 spin_unlock_bh(&vsock->send_pkt_list_lock);
232 vhost_work_queue(&vsock->dev, &vsock->send_pkt_work);
239 vhost_transport_cancel_pkt(struct vsock_sock *vsk)
241 struct vhost_vsock *vsock;
242 struct virtio_vsock_pkt *pkt, *n;
249 /* Find the vhost_vsock according to guest context id */
250 vsock = vhost_vsock_get(vsk->remote_addr.svm_cid);
254 spin_lock_bh(&vsock->send_pkt_list_lock);
255 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
258 list_move(&pkt->list, &freeme);
260 spin_unlock_bh(&vsock->send_pkt_list_lock);
262 list_for_each_entry_safe(pkt, n, &freeme, list) {
265 list_del(&pkt->list);
266 virtio_transport_free_pkt(pkt);
270 struct vhost_virtqueue *tx_vq = &vsock->vqs[VSOCK_VQ_TX];
273 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
274 if (new_cnt + cnt >= tx_vq->num && new_cnt < tx_vq->num)
275 vhost_poll_queue(&tx_vq->poll);
284 static struct virtio_vsock_pkt *
285 vhost_vsock_alloc_pkt(struct vhost_virtqueue *vq,
286 unsigned int out, unsigned int in)
288 struct virtio_vsock_pkt *pkt;
289 struct iov_iter iov_iter;
294 vq_err(vq, "Expected 0 input buffers, got %u\n", in);
298 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
302 len = iov_length(vq->iov, out);
303 iov_iter_init(&iov_iter, WRITE, vq->iov, out, len);
305 nbytes = copy_from_iter(&pkt->hdr, sizeof(pkt->hdr), &iov_iter);
306 if (nbytes != sizeof(pkt->hdr)) {
307 vq_err(vq, "Expected %zu bytes for pkt->hdr, got %zu bytes\n",
308 sizeof(pkt->hdr), nbytes);
313 if (le16_to_cpu(pkt->hdr.type) == VIRTIO_VSOCK_TYPE_STREAM)
314 pkt->len = le32_to_cpu(pkt->hdr.len);
320 /* The pkt is too big */
321 if (pkt->len > VIRTIO_VSOCK_MAX_PKT_BUF_SIZE) {
326 pkt->buf = kmalloc(pkt->len, GFP_KERNEL);
332 nbytes = copy_from_iter(pkt->buf, pkt->len, &iov_iter);
333 if (nbytes != pkt->len) {
334 vq_err(vq, "Expected %u byte payload, got %zu bytes\n",
336 virtio_transport_free_pkt(pkt);
343 /* Is there space left for replies to rx packets? */
344 static bool vhost_vsock_more_replies(struct vhost_vsock *vsock)
346 struct vhost_virtqueue *vq = &vsock->vqs[VSOCK_VQ_TX];
349 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
350 val = atomic_read(&vsock->queued_replies);
352 return val < vq->num;
355 static void vhost_vsock_handle_tx_kick(struct vhost_work *work)
357 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
359 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
361 struct virtio_vsock_pkt *pkt;
362 int head, pkts = 0, total_len = 0;
363 unsigned int out, in;
366 mutex_lock(&vq->mutex);
368 if (!vq->private_data)
371 vhost_disable_notify(&vsock->dev, vq);
375 if (!vhost_vsock_more_replies(vsock)) {
376 /* Stop tx until the device processes already
377 * pending replies. Leave tx virtqueue
378 * callbacks disabled.
380 goto no_more_replies;
383 head = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
384 &out, &in, NULL, NULL);
388 if (head == vq->num) {
389 if (unlikely(vhost_enable_notify(&vsock->dev, vq))) {
390 vhost_disable_notify(&vsock->dev, vq);
396 pkt = vhost_vsock_alloc_pkt(vq, out, in);
398 vq_err(vq, "Faulted on pkt\n");
404 /* Deliver to monitoring devices all received packets */
405 virtio_transport_deliver_tap_pkt(pkt);
407 /* Only accept correctly addressed packets */
408 if (le64_to_cpu(pkt->hdr.src_cid) == vsock->guest_cid)
409 virtio_transport_recv_pkt(pkt);
411 virtio_transport_free_pkt(pkt);
413 len += sizeof(pkt->hdr);
414 vhost_add_used(vq, head, len);
417 } while(likely(!vhost_exceeds_weight(vq, ++pkts, total_len)));
421 vhost_signal(&vsock->dev, vq);
424 mutex_unlock(&vq->mutex);
427 static void vhost_vsock_handle_rx_kick(struct vhost_work *work)
429 struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
431 struct vhost_vsock *vsock = container_of(vq->dev, struct vhost_vsock,
434 vhost_transport_do_send_pkt(vsock, vq);
437 static int vhost_vsock_start(struct vhost_vsock *vsock)
439 struct vhost_virtqueue *vq;
443 mutex_lock(&vsock->dev.mutex);
445 ret = vhost_dev_check_owner(&vsock->dev);
449 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
452 mutex_lock(&vq->mutex);
454 if (!vhost_vq_access_ok(vq)) {
459 if (!vq->private_data) {
460 vq->private_data = vsock;
461 ret = vhost_vq_init_access(vq);
466 mutex_unlock(&vq->mutex);
469 mutex_unlock(&vsock->dev.mutex);
473 vq->private_data = NULL;
474 mutex_unlock(&vq->mutex);
476 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
479 mutex_lock(&vq->mutex);
480 vq->private_data = NULL;
481 mutex_unlock(&vq->mutex);
484 mutex_unlock(&vsock->dev.mutex);
488 static int vhost_vsock_stop(struct vhost_vsock *vsock)
493 mutex_lock(&vsock->dev.mutex);
495 ret = vhost_dev_check_owner(&vsock->dev);
499 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
500 struct vhost_virtqueue *vq = &vsock->vqs[i];
502 mutex_lock(&vq->mutex);
503 vq->private_data = NULL;
504 mutex_unlock(&vq->mutex);
508 mutex_unlock(&vsock->dev.mutex);
512 static void vhost_vsock_free(struct vhost_vsock *vsock)
517 static int vhost_vsock_dev_open(struct inode *inode, struct file *file)
519 struct vhost_virtqueue **vqs;
520 struct vhost_vsock *vsock;
523 /* This struct is large and allocation could fail, fall back to vmalloc
524 * if there is no other way.
526 vsock = kvmalloc(sizeof(*vsock), GFP_KERNEL | __GFP_RETRY_MAYFAIL);
530 vqs = kmalloc_array(ARRAY_SIZE(vsock->vqs), sizeof(*vqs), GFP_KERNEL);
536 vsock->guest_cid = 0; /* no CID assigned yet */
538 atomic_set(&vsock->queued_replies, 0);
540 vqs[VSOCK_VQ_TX] = &vsock->vqs[VSOCK_VQ_TX];
541 vqs[VSOCK_VQ_RX] = &vsock->vqs[VSOCK_VQ_RX];
542 vsock->vqs[VSOCK_VQ_TX].handle_kick = vhost_vsock_handle_tx_kick;
543 vsock->vqs[VSOCK_VQ_RX].handle_kick = vhost_vsock_handle_rx_kick;
545 vhost_dev_init(&vsock->dev, vqs, ARRAY_SIZE(vsock->vqs),
546 UIO_MAXIOV, VHOST_VSOCK_PKT_WEIGHT,
549 file->private_data = vsock;
550 spin_lock_init(&vsock->send_pkt_list_lock);
551 INIT_LIST_HEAD(&vsock->send_pkt_list);
552 vhost_work_init(&vsock->send_pkt_work, vhost_transport_send_pkt_work);
556 vhost_vsock_free(vsock);
560 static void vhost_vsock_flush(struct vhost_vsock *vsock)
564 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++)
565 if (vsock->vqs[i].handle_kick)
566 vhost_poll_flush(&vsock->vqs[i].poll);
567 vhost_work_flush(&vsock->dev, &vsock->send_pkt_work);
570 static void vhost_vsock_reset_orphans(struct sock *sk)
572 struct vsock_sock *vsk = vsock_sk(sk);
574 /* vmci_transport.c doesn't take sk_lock here either. At least we're
575 * under vsock_table_lock so the sock cannot disappear while we're
579 /* If the peer is still valid, no need to reset connection */
580 if (vhost_vsock_get(vsk->remote_addr.svm_cid))
583 /* If the close timeout is pending, let it expire. This avoids races
584 * with the timeout callback.
586 if (vsk->close_work_scheduled)
589 sock_set_flag(sk, SOCK_DONE);
590 vsk->peer_shutdown = SHUTDOWN_MASK;
591 sk->sk_state = SS_UNCONNECTED;
592 sk->sk_err = ECONNRESET;
593 sk->sk_error_report(sk);
596 static int vhost_vsock_dev_release(struct inode *inode, struct file *file)
598 struct vhost_vsock *vsock = file->private_data;
600 mutex_lock(&vhost_vsock_mutex);
601 if (vsock->guest_cid)
602 hash_del_rcu(&vsock->hash);
603 mutex_unlock(&vhost_vsock_mutex);
605 /* Wait for other CPUs to finish using vsock */
608 /* Iterating over all connections for all CIDs to find orphans is
609 * inefficient. Room for improvement here. */
610 vsock_for_each_connected_socket(vhost_vsock_reset_orphans);
612 vhost_vsock_stop(vsock);
613 vhost_vsock_flush(vsock);
614 vhost_dev_stop(&vsock->dev);
616 spin_lock_bh(&vsock->send_pkt_list_lock);
617 while (!list_empty(&vsock->send_pkt_list)) {
618 struct virtio_vsock_pkt *pkt;
620 pkt = list_first_entry(&vsock->send_pkt_list,
621 struct virtio_vsock_pkt, list);
622 list_del_init(&pkt->list);
623 virtio_transport_free_pkt(pkt);
625 spin_unlock_bh(&vsock->send_pkt_list_lock);
627 vhost_dev_cleanup(&vsock->dev);
628 kfree(vsock->dev.vqs);
629 vhost_vsock_free(vsock);
633 static int vhost_vsock_set_cid(struct vhost_vsock *vsock, u64 guest_cid)
635 struct vhost_vsock *other;
637 /* Refuse reserved CIDs */
638 if (guest_cid <= VMADDR_CID_HOST ||
639 guest_cid == U32_MAX)
642 /* 64-bit CIDs are not yet supported */
643 if (guest_cid > U32_MAX)
646 /* Refuse if CID is already in use */
647 mutex_lock(&vhost_vsock_mutex);
648 other = vhost_vsock_get(guest_cid);
649 if (other && other != vsock) {
650 mutex_unlock(&vhost_vsock_mutex);
654 if (vsock->guest_cid)
655 hash_del_rcu(&vsock->hash);
657 vsock->guest_cid = guest_cid;
658 hash_add_rcu(vhost_vsock_hash, &vsock->hash, vsock->guest_cid);
659 mutex_unlock(&vhost_vsock_mutex);
664 static int vhost_vsock_set_features(struct vhost_vsock *vsock, u64 features)
666 struct vhost_virtqueue *vq;
669 if (features & ~VHOST_VSOCK_FEATURES)
672 mutex_lock(&vsock->dev.mutex);
673 if ((features & (1 << VHOST_F_LOG_ALL)) &&
674 !vhost_log_access_ok(&vsock->dev)) {
675 mutex_unlock(&vsock->dev.mutex);
679 for (i = 0; i < ARRAY_SIZE(vsock->vqs); i++) {
681 mutex_lock(&vq->mutex);
682 vq->acked_features = features;
683 mutex_unlock(&vq->mutex);
685 mutex_unlock(&vsock->dev.mutex);
689 static long vhost_vsock_dev_ioctl(struct file *f, unsigned int ioctl,
692 struct vhost_vsock *vsock = f->private_data;
693 void __user *argp = (void __user *)arg;
700 case VHOST_VSOCK_SET_GUEST_CID:
701 if (copy_from_user(&guest_cid, argp, sizeof(guest_cid)))
703 return vhost_vsock_set_cid(vsock, guest_cid);
704 case VHOST_VSOCK_SET_RUNNING:
705 if (copy_from_user(&start, argp, sizeof(start)))
708 return vhost_vsock_start(vsock);
710 return vhost_vsock_stop(vsock);
711 case VHOST_GET_FEATURES:
712 features = VHOST_VSOCK_FEATURES;
713 if (copy_to_user(argp, &features, sizeof(features)))
716 case VHOST_SET_FEATURES:
717 if (copy_from_user(&features, argp, sizeof(features)))
719 return vhost_vsock_set_features(vsock, features);
721 mutex_lock(&vsock->dev.mutex);
722 r = vhost_dev_ioctl(&vsock->dev, ioctl, argp);
723 if (r == -ENOIOCTLCMD)
724 r = vhost_vring_ioctl(&vsock->dev, ioctl, argp);
726 vhost_vsock_flush(vsock);
727 mutex_unlock(&vsock->dev.mutex);
733 static long vhost_vsock_dev_compat_ioctl(struct file *f, unsigned int ioctl,
736 return vhost_vsock_dev_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
740 static const struct file_operations vhost_vsock_fops = {
741 .owner = THIS_MODULE,
742 .open = vhost_vsock_dev_open,
743 .release = vhost_vsock_dev_release,
744 .llseek = noop_llseek,
745 .unlocked_ioctl = vhost_vsock_dev_ioctl,
747 .compat_ioctl = vhost_vsock_dev_compat_ioctl,
751 static struct miscdevice vhost_vsock_misc = {
752 .minor = VHOST_VSOCK_MINOR,
753 .name = "vhost-vsock",
754 .fops = &vhost_vsock_fops,
757 static struct virtio_transport vhost_transport = {
759 .get_local_cid = vhost_transport_get_local_cid,
761 .init = virtio_transport_do_socket_init,
762 .destruct = virtio_transport_destruct,
763 .release = virtio_transport_release,
764 .connect = virtio_transport_connect,
765 .shutdown = virtio_transport_shutdown,
766 .cancel_pkt = vhost_transport_cancel_pkt,
768 .dgram_enqueue = virtio_transport_dgram_enqueue,
769 .dgram_dequeue = virtio_transport_dgram_dequeue,
770 .dgram_bind = virtio_transport_dgram_bind,
771 .dgram_allow = virtio_transport_dgram_allow,
773 .stream_enqueue = virtio_transport_stream_enqueue,
774 .stream_dequeue = virtio_transport_stream_dequeue,
775 .stream_has_data = virtio_transport_stream_has_data,
776 .stream_has_space = virtio_transport_stream_has_space,
777 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
778 .stream_is_active = virtio_transport_stream_is_active,
779 .stream_allow = virtio_transport_stream_allow,
781 .notify_poll_in = virtio_transport_notify_poll_in,
782 .notify_poll_out = virtio_transport_notify_poll_out,
783 .notify_recv_init = virtio_transport_notify_recv_init,
784 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
785 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
786 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
787 .notify_send_init = virtio_transport_notify_send_init,
788 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
789 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
790 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
792 .set_buffer_size = virtio_transport_set_buffer_size,
793 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
794 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
795 .get_buffer_size = virtio_transport_get_buffer_size,
796 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
797 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
800 .send_pkt = vhost_transport_send_pkt,
803 static int __init vhost_vsock_init(void)
807 ret = vsock_core_init(&vhost_transport.transport);
810 return misc_register(&vhost_vsock_misc);
813 static void __exit vhost_vsock_exit(void)
815 misc_deregister(&vhost_vsock_misc);
819 module_init(vhost_vsock_init);
820 module_exit(vhost_vsock_exit);
821 MODULE_LICENSE("GPL v2");
822 MODULE_AUTHOR("Asias He");
823 MODULE_DESCRIPTION("vhost transport for vsock ");
824 MODULE_ALIAS_MISCDEV(VHOST_VSOCK_MINOR);
825 MODULE_ALIAS("devname:vhost-vsock");