1 // SPDX-License-Identifier: GPL-2.0-only
3 * virtio 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 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
10 * early virtio-vsock proof-of-concept bits.
12 #include <linux/spinlock.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/atomic.h>
16 #include <linux/virtio.h>
17 #include <linux/virtio_ids.h>
18 #include <linux/virtio_config.h>
19 #include <linux/virtio_vsock.h>
21 #include <linux/mutex.h>
22 #include <net/af_vsock.h>
24 static struct workqueue_struct *virtio_vsock_workqueue;
25 static struct virtio_vsock *the_virtio_vsock;
26 static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
29 struct virtio_device *vdev;
30 struct virtqueue *vqs[VSOCK_VQ_MAX];
32 /* Virtqueue processing is deferred to a workqueue */
33 struct work_struct tx_work;
34 struct work_struct rx_work;
35 struct work_struct event_work;
37 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
38 * must be accessed with tx_lock held.
43 struct work_struct send_pkt_work;
44 spinlock_t send_pkt_list_lock;
45 struct list_head send_pkt_list;
47 struct work_struct loopback_work;
48 spinlock_t loopback_list_lock; /* protects loopback_list */
49 struct list_head loopback_list;
51 atomic_t queued_replies;
53 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
54 * must be accessed with rx_lock held.
61 /* The following fields are protected by event_lock.
62 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
64 struct mutex event_lock;
66 struct virtio_vsock_event event_list[8];
71 static u32 virtio_transport_get_local_cid(void)
73 struct virtio_vsock *vsock;
77 vsock = rcu_dereference(the_virtio_vsock);
83 ret = vsock->guest_cid;
89 static void virtio_transport_loopback_work(struct work_struct *work)
91 struct virtio_vsock *vsock =
92 container_of(work, struct virtio_vsock, loopback_work);
95 spin_lock_bh(&vsock->loopback_list_lock);
96 list_splice_init(&vsock->loopback_list, &pkts);
97 spin_unlock_bh(&vsock->loopback_list_lock);
99 mutex_lock(&vsock->rx_lock);
104 while (!list_empty(&pkts)) {
105 struct virtio_vsock_pkt *pkt;
107 pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
108 list_del_init(&pkt->list);
110 virtio_transport_recv_pkt(pkt);
113 mutex_unlock(&vsock->rx_lock);
116 static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock,
117 struct virtio_vsock_pkt *pkt)
121 spin_lock_bh(&vsock->loopback_list_lock);
122 list_add_tail(&pkt->list, &vsock->loopback_list);
123 spin_unlock_bh(&vsock->loopback_list_lock);
125 queue_work(virtio_vsock_workqueue, &vsock->loopback_work);
131 virtio_transport_send_pkt_work(struct work_struct *work)
133 struct virtio_vsock *vsock =
134 container_of(work, struct virtio_vsock, send_pkt_work);
135 struct virtqueue *vq;
137 bool restart_rx = false;
139 mutex_lock(&vsock->tx_lock);
144 vq = vsock->vqs[VSOCK_VQ_TX];
147 struct virtio_vsock_pkt *pkt;
148 struct scatterlist hdr, buf, *sgs[2];
149 int ret, in_sg = 0, out_sg = 0;
152 spin_lock_bh(&vsock->send_pkt_list_lock);
153 if (list_empty(&vsock->send_pkt_list)) {
154 spin_unlock_bh(&vsock->send_pkt_list_lock);
158 pkt = list_first_entry(&vsock->send_pkt_list,
159 struct virtio_vsock_pkt, list);
160 list_del_init(&pkt->list);
161 spin_unlock_bh(&vsock->send_pkt_list_lock);
163 virtio_transport_deliver_tap_pkt(pkt);
167 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
168 sgs[out_sg++] = &hdr;
170 sg_init_one(&buf, pkt->buf, pkt->len);
171 sgs[out_sg++] = &buf;
174 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
175 /* Usually this means that there is no more space available in
179 spin_lock_bh(&vsock->send_pkt_list_lock);
180 list_add(&pkt->list, &vsock->send_pkt_list);
181 spin_unlock_bh(&vsock->send_pkt_list_lock);
186 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
189 val = atomic_dec_return(&vsock->queued_replies);
191 /* Do we now have resources to resume rx processing? */
192 if (val + 1 == virtqueue_get_vring_size(rx_vq))
203 mutex_unlock(&vsock->tx_lock);
206 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
210 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
212 struct virtio_vsock *vsock;
216 vsock = rcu_dereference(the_virtio_vsock);
218 virtio_transport_free_pkt(pkt);
223 if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) {
224 len = virtio_transport_send_pkt_loopback(vsock, pkt);
229 atomic_inc(&vsock->queued_replies);
231 spin_lock_bh(&vsock->send_pkt_list_lock);
232 list_add_tail(&pkt->list, &vsock->send_pkt_list);
233 spin_unlock_bh(&vsock->send_pkt_list_lock);
235 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
243 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
245 struct virtio_vsock *vsock;
246 struct virtio_vsock_pkt *pkt, *n;
251 vsock = rcu_dereference(the_virtio_vsock);
257 spin_lock_bh(&vsock->send_pkt_list_lock);
258 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
261 list_move(&pkt->list, &freeme);
263 spin_unlock_bh(&vsock->send_pkt_list_lock);
265 list_for_each_entry_safe(pkt, n, &freeme, list) {
268 list_del(&pkt->list);
269 virtio_transport_free_pkt(pkt);
273 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
276 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
277 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
278 new_cnt < virtqueue_get_vring_size(rx_vq))
279 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
289 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
291 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
292 struct virtio_vsock_pkt *pkt;
293 struct scatterlist hdr, buf, *sgs[2];
294 struct virtqueue *vq;
297 vq = vsock->vqs[VSOCK_VQ_RX];
300 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
304 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
306 virtio_transport_free_pkt(pkt);
312 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
315 sg_init_one(&buf, pkt->buf, buf_len);
317 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
319 virtio_transport_free_pkt(pkt);
323 } while (vq->num_free);
324 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
325 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
329 static void virtio_transport_tx_work(struct work_struct *work)
331 struct virtio_vsock *vsock =
332 container_of(work, struct virtio_vsock, tx_work);
333 struct virtqueue *vq;
336 vq = vsock->vqs[VSOCK_VQ_TX];
337 mutex_lock(&vsock->tx_lock);
343 struct virtio_vsock_pkt *pkt;
346 virtqueue_disable_cb(vq);
347 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
348 virtio_transport_free_pkt(pkt);
351 } while (!virtqueue_enable_cb(vq));
354 mutex_unlock(&vsock->tx_lock);
357 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
360 /* Is there space left for replies to rx packets? */
361 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
363 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
366 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
367 val = atomic_read(&vsock->queued_replies);
369 return val < virtqueue_get_vring_size(vq);
372 static void virtio_transport_rx_work(struct work_struct *work)
374 struct virtio_vsock *vsock =
375 container_of(work, struct virtio_vsock, rx_work);
376 struct virtqueue *vq;
378 vq = vsock->vqs[VSOCK_VQ_RX];
380 mutex_lock(&vsock->rx_lock);
386 virtqueue_disable_cb(vq);
388 struct virtio_vsock_pkt *pkt;
391 if (!virtio_transport_more_replies(vsock)) {
392 /* Stop rx until the device processes already
393 * pending replies. Leave rx virtqueue
394 * callbacks disabled.
399 pkt = virtqueue_get_buf(vq, &len);
406 /* Drop short/long packets */
407 if (unlikely(len < sizeof(pkt->hdr) ||
408 len > sizeof(pkt->hdr) + pkt->len)) {
409 virtio_transport_free_pkt(pkt);
413 pkt->len = len - sizeof(pkt->hdr);
414 virtio_transport_deliver_tap_pkt(pkt);
415 virtio_transport_recv_pkt(pkt);
417 } while (!virtqueue_enable_cb(vq));
420 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
421 virtio_vsock_rx_fill(vsock);
422 mutex_unlock(&vsock->rx_lock);
425 /* event_lock must be held */
426 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
427 struct virtio_vsock_event *event)
429 struct scatterlist sg;
430 struct virtqueue *vq;
432 vq = vsock->vqs[VSOCK_VQ_EVENT];
434 sg_init_one(&sg, event, sizeof(*event));
436 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
439 /* event_lock must be held */
440 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
444 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
445 struct virtio_vsock_event *event = &vsock->event_list[i];
447 virtio_vsock_event_fill_one(vsock, event);
450 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
453 static void virtio_vsock_reset_sock(struct sock *sk)
456 sk->sk_state = TCP_CLOSE;
457 sk->sk_err = ECONNRESET;
458 sk->sk_error_report(sk);
462 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
464 struct virtio_device *vdev = vsock->vdev;
467 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
468 &guest_cid, sizeof(guest_cid));
469 vsock->guest_cid = le64_to_cpu(guest_cid);
472 /* event_lock must be held */
473 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
474 struct virtio_vsock_event *event)
476 switch (le32_to_cpu(event->id)) {
477 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
478 virtio_vsock_update_guest_cid(vsock);
479 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
484 static void virtio_transport_event_work(struct work_struct *work)
486 struct virtio_vsock *vsock =
487 container_of(work, struct virtio_vsock, event_work);
488 struct virtqueue *vq;
490 vq = vsock->vqs[VSOCK_VQ_EVENT];
492 mutex_lock(&vsock->event_lock);
494 if (!vsock->event_run)
498 struct virtio_vsock_event *event;
501 virtqueue_disable_cb(vq);
502 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
503 if (len == sizeof(*event))
504 virtio_vsock_event_handle(vsock, event);
506 virtio_vsock_event_fill_one(vsock, event);
508 } while (!virtqueue_enable_cb(vq));
510 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
512 mutex_unlock(&vsock->event_lock);
515 static void virtio_vsock_event_done(struct virtqueue *vq)
517 struct virtio_vsock *vsock = vq->vdev->priv;
521 queue_work(virtio_vsock_workqueue, &vsock->event_work);
524 static void virtio_vsock_tx_done(struct virtqueue *vq)
526 struct virtio_vsock *vsock = vq->vdev->priv;
530 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
533 static void virtio_vsock_rx_done(struct virtqueue *vq)
535 struct virtio_vsock *vsock = vq->vdev->priv;
539 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
542 static struct virtio_transport virtio_transport = {
544 .get_local_cid = virtio_transport_get_local_cid,
546 .init = virtio_transport_do_socket_init,
547 .destruct = virtio_transport_destruct,
548 .release = virtio_transport_release,
549 .connect = virtio_transport_connect,
550 .shutdown = virtio_transport_shutdown,
551 .cancel_pkt = virtio_transport_cancel_pkt,
553 .dgram_bind = virtio_transport_dgram_bind,
554 .dgram_dequeue = virtio_transport_dgram_dequeue,
555 .dgram_enqueue = virtio_transport_dgram_enqueue,
556 .dgram_allow = virtio_transport_dgram_allow,
558 .stream_dequeue = virtio_transport_stream_dequeue,
559 .stream_enqueue = virtio_transport_stream_enqueue,
560 .stream_has_data = virtio_transport_stream_has_data,
561 .stream_has_space = virtio_transport_stream_has_space,
562 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
563 .stream_is_active = virtio_transport_stream_is_active,
564 .stream_allow = virtio_transport_stream_allow,
566 .notify_poll_in = virtio_transport_notify_poll_in,
567 .notify_poll_out = virtio_transport_notify_poll_out,
568 .notify_recv_init = virtio_transport_notify_recv_init,
569 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
570 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
571 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
572 .notify_send_init = virtio_transport_notify_send_init,
573 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
574 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
575 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
577 .set_buffer_size = virtio_transport_set_buffer_size,
578 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
579 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
580 .get_buffer_size = virtio_transport_get_buffer_size,
581 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
582 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
585 .send_pkt = virtio_transport_send_pkt,
588 static int virtio_vsock_probe(struct virtio_device *vdev)
590 vq_callback_t *callbacks[] = {
591 virtio_vsock_rx_done,
592 virtio_vsock_tx_done,
593 virtio_vsock_event_done,
595 static const char * const names[] = {
600 struct virtio_vsock *vsock = NULL;
603 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
607 /* Only one virtio-vsock device per guest is supported */
608 if (rcu_dereference_protected(the_virtio_vsock,
609 lockdep_is_held(&the_virtio_vsock_mutex))) {
614 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
622 ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
623 vsock->vqs, callbacks, names,
628 virtio_vsock_update_guest_cid(vsock);
630 vsock->rx_buf_nr = 0;
631 vsock->rx_buf_max_nr = 0;
632 atomic_set(&vsock->queued_replies, 0);
634 mutex_init(&vsock->tx_lock);
635 mutex_init(&vsock->rx_lock);
636 mutex_init(&vsock->event_lock);
637 spin_lock_init(&vsock->send_pkt_list_lock);
638 INIT_LIST_HEAD(&vsock->send_pkt_list);
639 spin_lock_init(&vsock->loopback_list_lock);
640 INIT_LIST_HEAD(&vsock->loopback_list);
641 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
642 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
643 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
644 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
645 INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
647 mutex_lock(&vsock->tx_lock);
648 vsock->tx_run = true;
649 mutex_unlock(&vsock->tx_lock);
651 mutex_lock(&vsock->rx_lock);
652 virtio_vsock_rx_fill(vsock);
653 vsock->rx_run = true;
654 mutex_unlock(&vsock->rx_lock);
656 mutex_lock(&vsock->event_lock);
657 virtio_vsock_event_fill(vsock);
658 vsock->event_run = true;
659 mutex_unlock(&vsock->event_lock);
662 rcu_assign_pointer(the_virtio_vsock, vsock);
664 mutex_unlock(&the_virtio_vsock_mutex);
669 mutex_unlock(&the_virtio_vsock_mutex);
673 static void virtio_vsock_remove(struct virtio_device *vdev)
675 struct virtio_vsock *vsock = vdev->priv;
676 struct virtio_vsock_pkt *pkt;
678 mutex_lock(&the_virtio_vsock_mutex);
681 rcu_assign_pointer(the_virtio_vsock, NULL);
684 /* Reset all connected sockets when the device disappear */
685 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
687 /* Stop all work handlers to make sure no one is accessing the device,
688 * so we can safely call vdev->config->reset().
690 mutex_lock(&vsock->rx_lock);
691 vsock->rx_run = false;
692 mutex_unlock(&vsock->rx_lock);
694 mutex_lock(&vsock->tx_lock);
695 vsock->tx_run = false;
696 mutex_unlock(&vsock->tx_lock);
698 mutex_lock(&vsock->event_lock);
699 vsock->event_run = false;
700 mutex_unlock(&vsock->event_lock);
702 /* Flush all device writes and interrupts, device will not use any
705 vdev->config->reset(vdev);
707 mutex_lock(&vsock->rx_lock);
708 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
709 virtio_transport_free_pkt(pkt);
710 mutex_unlock(&vsock->rx_lock);
712 mutex_lock(&vsock->tx_lock);
713 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
714 virtio_transport_free_pkt(pkt);
715 mutex_unlock(&vsock->tx_lock);
717 spin_lock_bh(&vsock->send_pkt_list_lock);
718 while (!list_empty(&vsock->send_pkt_list)) {
719 pkt = list_first_entry(&vsock->send_pkt_list,
720 struct virtio_vsock_pkt, list);
721 list_del(&pkt->list);
722 virtio_transport_free_pkt(pkt);
724 spin_unlock_bh(&vsock->send_pkt_list_lock);
726 spin_lock_bh(&vsock->loopback_list_lock);
727 while (!list_empty(&vsock->loopback_list)) {
728 pkt = list_first_entry(&vsock->loopback_list,
729 struct virtio_vsock_pkt, list);
730 list_del(&pkt->list);
731 virtio_transport_free_pkt(pkt);
733 spin_unlock_bh(&vsock->loopback_list_lock);
735 /* Delete virtqueues and flush outstanding callbacks if any */
736 vdev->config->del_vqs(vdev);
738 /* Other works can be queued before 'config->del_vqs()', so we flush
739 * all works before to free the vsock object to avoid use after free.
741 flush_work(&vsock->loopback_work);
742 flush_work(&vsock->rx_work);
743 flush_work(&vsock->tx_work);
744 flush_work(&vsock->event_work);
745 flush_work(&vsock->send_pkt_work);
747 mutex_unlock(&the_virtio_vsock_mutex);
752 static struct virtio_device_id id_table[] = {
753 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
757 static unsigned int features[] = {
760 static struct virtio_driver virtio_vsock_driver = {
761 .feature_table = features,
762 .feature_table_size = ARRAY_SIZE(features),
763 .driver.name = KBUILD_MODNAME,
764 .driver.owner = THIS_MODULE,
765 .id_table = id_table,
766 .probe = virtio_vsock_probe,
767 .remove = virtio_vsock_remove,
770 static int __init virtio_vsock_init(void)
774 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
775 if (!virtio_vsock_workqueue)
778 ret = vsock_core_init(&virtio_transport.transport);
782 ret = register_virtio_driver(&virtio_vsock_driver);
791 destroy_workqueue(virtio_vsock_workqueue);
795 static void __exit virtio_vsock_exit(void)
797 unregister_virtio_driver(&virtio_vsock_driver);
799 destroy_workqueue(virtio_vsock_workqueue);
802 module_init(virtio_vsock_init);
803 module_exit(virtio_vsock_exit);
804 MODULE_LICENSE("GPL v2");
805 MODULE_AUTHOR("Asias He");
806 MODULE_DESCRIPTION("virtio transport for vsock");
807 MODULE_DEVICE_TABLE(virtio, id_table);