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 __rcu *the_virtio_vsock;
26 static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
27 static struct virtio_transport virtio_transport; /* forward declaration */
30 struct virtio_device *vdev;
31 struct virtqueue *vqs[VSOCK_VQ_MAX];
33 /* Virtqueue processing is deferred to a workqueue */
34 struct work_struct tx_work;
35 struct work_struct rx_work;
36 struct work_struct event_work;
38 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
39 * must be accessed with tx_lock held.
44 struct work_struct send_pkt_work;
45 struct sk_buff_head send_pkt_queue;
47 atomic_t queued_replies;
49 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
50 * must be accessed with rx_lock held.
57 /* The following fields are protected by event_lock.
58 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
60 struct mutex event_lock;
62 struct virtio_vsock_event event_list[8];
68 static u32 virtio_transport_get_local_cid(void)
70 struct virtio_vsock *vsock;
74 vsock = rcu_dereference(the_virtio_vsock);
80 ret = vsock->guest_cid;
87 virtio_transport_send_pkt_work(struct work_struct *work)
89 struct virtio_vsock *vsock =
90 container_of(work, struct virtio_vsock, send_pkt_work);
93 bool restart_rx = false;
95 mutex_lock(&vsock->tx_lock);
100 vq = vsock->vqs[VSOCK_VQ_TX];
103 struct scatterlist hdr, buf, *sgs[2];
104 int ret, in_sg = 0, out_sg = 0;
108 skb = virtio_vsock_skb_dequeue(&vsock->send_pkt_queue);
112 reply = virtio_vsock_skb_reply(skb);
114 sg_init_one(&hdr, virtio_vsock_hdr(skb), sizeof(*virtio_vsock_hdr(skb)));
115 sgs[out_sg++] = &hdr;
117 sg_init_one(&buf, skb->data, skb->len);
118 sgs[out_sg++] = &buf;
121 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, skb, GFP_KERNEL);
122 /* Usually this means that there is no more space available in
126 virtio_vsock_skb_queue_head(&vsock->send_pkt_queue, skb);
130 virtio_transport_deliver_tap_pkt(skb);
133 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
136 val = atomic_dec_return(&vsock->queued_replies);
138 /* Do we now have resources to resume rx processing? */
139 if (val + 1 == virtqueue_get_vring_size(rx_vq))
150 mutex_unlock(&vsock->tx_lock);
153 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
157 virtio_transport_send_pkt(struct sk_buff *skb)
159 struct virtio_vsock_hdr *hdr;
160 struct virtio_vsock *vsock;
163 hdr = virtio_vsock_hdr(skb);
166 vsock = rcu_dereference(the_virtio_vsock);
173 if (le64_to_cpu(hdr->dst_cid) == vsock->guest_cid) {
179 if (virtio_vsock_skb_reply(skb))
180 atomic_inc(&vsock->queued_replies);
182 virtio_vsock_skb_queue_tail(&vsock->send_pkt_queue, skb);
183 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
191 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
193 struct virtio_vsock *vsock;
197 vsock = rcu_dereference(the_virtio_vsock);
203 cnt = virtio_transport_purge_skbs(vsk, &vsock->send_pkt_queue);
206 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
209 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
210 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
211 new_cnt < virtqueue_get_vring_size(rx_vq))
212 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
222 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
224 int total_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE + VIRTIO_VSOCK_SKB_HEADROOM;
225 struct scatterlist pkt, *p;
226 struct virtqueue *vq;
230 vq = vsock->vqs[VSOCK_VQ_RX];
233 skb = virtio_vsock_alloc_skb(total_len, GFP_KERNEL);
237 memset(skb->head, 0, VIRTIO_VSOCK_SKB_HEADROOM);
238 sg_init_one(&pkt, virtio_vsock_hdr(skb), total_len);
240 ret = virtqueue_add_sgs(vq, &p, 0, 1, skb, GFP_KERNEL);
247 } while (vq->num_free);
248 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
249 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
253 static void virtio_transport_tx_work(struct work_struct *work)
255 struct virtio_vsock *vsock =
256 container_of(work, struct virtio_vsock, tx_work);
257 struct virtqueue *vq;
260 vq = vsock->vqs[VSOCK_VQ_TX];
261 mutex_lock(&vsock->tx_lock);
270 virtqueue_disable_cb(vq);
271 while ((skb = virtqueue_get_buf(vq, &len)) != NULL) {
275 } while (!virtqueue_enable_cb(vq));
278 mutex_unlock(&vsock->tx_lock);
281 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
284 /* Is there space left for replies to rx packets? */
285 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
287 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
290 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
291 val = atomic_read(&vsock->queued_replies);
293 return val < virtqueue_get_vring_size(vq);
296 /* event_lock must be held */
297 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
298 struct virtio_vsock_event *event)
300 struct scatterlist sg;
301 struct virtqueue *vq;
303 vq = vsock->vqs[VSOCK_VQ_EVENT];
305 sg_init_one(&sg, event, sizeof(*event));
307 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
310 /* event_lock must be held */
311 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
315 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
316 struct virtio_vsock_event *event = &vsock->event_list[i];
318 virtio_vsock_event_fill_one(vsock, event);
321 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
324 static void virtio_vsock_reset_sock(struct sock *sk)
326 /* vmci_transport.c doesn't take sk_lock here either. At least we're
327 * under vsock_table_lock so the sock cannot disappear while we're
331 sk->sk_state = TCP_CLOSE;
332 sk->sk_err = ECONNRESET;
336 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
338 struct virtio_device *vdev = vsock->vdev;
341 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
342 &guest_cid, sizeof(guest_cid));
343 vsock->guest_cid = le64_to_cpu(guest_cid);
346 /* event_lock must be held */
347 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
348 struct virtio_vsock_event *event)
350 switch (le32_to_cpu(event->id)) {
351 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
352 virtio_vsock_update_guest_cid(vsock);
353 vsock_for_each_connected_socket(&virtio_transport.transport,
354 virtio_vsock_reset_sock);
359 static void virtio_transport_event_work(struct work_struct *work)
361 struct virtio_vsock *vsock =
362 container_of(work, struct virtio_vsock, event_work);
363 struct virtqueue *vq;
365 vq = vsock->vqs[VSOCK_VQ_EVENT];
367 mutex_lock(&vsock->event_lock);
369 if (!vsock->event_run)
373 struct virtio_vsock_event *event;
376 virtqueue_disable_cb(vq);
377 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
378 if (len == sizeof(*event))
379 virtio_vsock_event_handle(vsock, event);
381 virtio_vsock_event_fill_one(vsock, event);
383 } while (!virtqueue_enable_cb(vq));
385 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
387 mutex_unlock(&vsock->event_lock);
390 static void virtio_vsock_event_done(struct virtqueue *vq)
392 struct virtio_vsock *vsock = vq->vdev->priv;
396 queue_work(virtio_vsock_workqueue, &vsock->event_work);
399 static void virtio_vsock_tx_done(struct virtqueue *vq)
401 struct virtio_vsock *vsock = vq->vdev->priv;
405 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
408 static void virtio_vsock_rx_done(struct virtqueue *vq)
410 struct virtio_vsock *vsock = vq->vdev->priv;
414 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
417 static bool virtio_transport_seqpacket_allow(u32 remote_cid);
419 static struct virtio_transport virtio_transport = {
421 .module = THIS_MODULE,
423 .get_local_cid = virtio_transport_get_local_cid,
425 .init = virtio_transport_do_socket_init,
426 .destruct = virtio_transport_destruct,
427 .release = virtio_transport_release,
428 .connect = virtio_transport_connect,
429 .shutdown = virtio_transport_shutdown,
430 .cancel_pkt = virtio_transport_cancel_pkt,
432 .dgram_bind = virtio_transport_dgram_bind,
433 .dgram_dequeue = virtio_transport_dgram_dequeue,
434 .dgram_enqueue = virtio_transport_dgram_enqueue,
435 .dgram_allow = virtio_transport_dgram_allow,
437 .stream_dequeue = virtio_transport_stream_dequeue,
438 .stream_enqueue = virtio_transport_stream_enqueue,
439 .stream_has_data = virtio_transport_stream_has_data,
440 .stream_has_space = virtio_transport_stream_has_space,
441 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
442 .stream_is_active = virtio_transport_stream_is_active,
443 .stream_allow = virtio_transport_stream_allow,
445 .seqpacket_dequeue = virtio_transport_seqpacket_dequeue,
446 .seqpacket_enqueue = virtio_transport_seqpacket_enqueue,
447 .seqpacket_allow = virtio_transport_seqpacket_allow,
448 .seqpacket_has_data = virtio_transport_seqpacket_has_data,
450 .notify_poll_in = virtio_transport_notify_poll_in,
451 .notify_poll_out = virtio_transport_notify_poll_out,
452 .notify_recv_init = virtio_transport_notify_recv_init,
453 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
454 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
455 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
456 .notify_send_init = virtio_transport_notify_send_init,
457 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
458 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
459 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
460 .notify_buffer_size = virtio_transport_notify_buffer_size,
463 .send_pkt = virtio_transport_send_pkt,
466 static bool virtio_transport_seqpacket_allow(u32 remote_cid)
468 struct virtio_vsock *vsock;
469 bool seqpacket_allow;
471 seqpacket_allow = false;
473 vsock = rcu_dereference(the_virtio_vsock);
475 seqpacket_allow = vsock->seqpacket_allow;
478 return seqpacket_allow;
481 static void virtio_transport_rx_work(struct work_struct *work)
483 struct virtio_vsock *vsock =
484 container_of(work, struct virtio_vsock, rx_work);
485 struct virtqueue *vq;
487 vq = vsock->vqs[VSOCK_VQ_RX];
489 mutex_lock(&vsock->rx_lock);
495 virtqueue_disable_cb(vq);
500 if (!virtio_transport_more_replies(vsock)) {
501 /* Stop rx until the device processes already
502 * pending replies. Leave rx virtqueue
503 * callbacks disabled.
508 skb = virtqueue_get_buf(vq, &len);
514 /* Drop short/long packets */
515 if (unlikely(len < sizeof(struct virtio_vsock_hdr) ||
516 len > virtio_vsock_skb_len(skb))) {
521 virtio_vsock_skb_rx_put(skb);
522 virtio_transport_deliver_tap_pkt(skb);
523 virtio_transport_recv_pkt(&virtio_transport, skb);
525 } while (!virtqueue_enable_cb(vq));
528 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
529 virtio_vsock_rx_fill(vsock);
530 mutex_unlock(&vsock->rx_lock);
533 static int virtio_vsock_vqs_init(struct virtio_vsock *vsock)
535 struct virtio_device *vdev = vsock->vdev;
536 static const char * const names[] = {
541 vq_callback_t *callbacks[] = {
542 virtio_vsock_rx_done,
543 virtio_vsock_tx_done,
544 virtio_vsock_event_done,
548 ret = virtio_find_vqs(vdev, VSOCK_VQ_MAX, vsock->vqs, callbacks, names,
553 virtio_vsock_update_guest_cid(vsock);
555 virtio_device_ready(vdev);
560 static void virtio_vsock_vqs_start(struct virtio_vsock *vsock)
562 mutex_lock(&vsock->tx_lock);
563 vsock->tx_run = true;
564 mutex_unlock(&vsock->tx_lock);
566 mutex_lock(&vsock->rx_lock);
567 virtio_vsock_rx_fill(vsock);
568 vsock->rx_run = true;
569 mutex_unlock(&vsock->rx_lock);
571 mutex_lock(&vsock->event_lock);
572 virtio_vsock_event_fill(vsock);
573 vsock->event_run = true;
574 mutex_unlock(&vsock->event_lock);
576 /* virtio_transport_send_pkt() can queue packets once
577 * the_virtio_vsock is set, but they won't be processed until
578 * vsock->tx_run is set to true. We queue vsock->send_pkt_work
579 * when initialization finishes to send those packets queued
581 * We don't need to queue the other workers (rx, event) because
582 * as long as we don't fill the queues with empty buffers, the
583 * host can't send us any notification.
585 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
588 static void virtio_vsock_vqs_del(struct virtio_vsock *vsock)
590 struct virtio_device *vdev = vsock->vdev;
593 /* Reset all connected sockets when the VQs disappear */
594 vsock_for_each_connected_socket(&virtio_transport.transport,
595 virtio_vsock_reset_sock);
597 /* Stop all work handlers to make sure no one is accessing the device,
598 * so we can safely call virtio_reset_device().
600 mutex_lock(&vsock->rx_lock);
601 vsock->rx_run = false;
602 mutex_unlock(&vsock->rx_lock);
604 mutex_lock(&vsock->tx_lock);
605 vsock->tx_run = false;
606 mutex_unlock(&vsock->tx_lock);
608 mutex_lock(&vsock->event_lock);
609 vsock->event_run = false;
610 mutex_unlock(&vsock->event_lock);
612 /* Flush all device writes and interrupts, device will not use any
615 virtio_reset_device(vdev);
617 mutex_lock(&vsock->rx_lock);
618 while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
620 mutex_unlock(&vsock->rx_lock);
622 mutex_lock(&vsock->tx_lock);
623 while ((skb = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
625 mutex_unlock(&vsock->tx_lock);
627 virtio_vsock_skb_queue_purge(&vsock->send_pkt_queue);
629 /* Delete virtqueues and flush outstanding callbacks if any */
630 vdev->config->del_vqs(vdev);
633 static int virtio_vsock_probe(struct virtio_device *vdev)
635 struct virtio_vsock *vsock = NULL;
638 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
642 /* Only one virtio-vsock device per guest is supported */
643 if (rcu_dereference_protected(the_virtio_vsock,
644 lockdep_is_held(&the_virtio_vsock_mutex))) {
649 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
657 vsock->rx_buf_nr = 0;
658 vsock->rx_buf_max_nr = 0;
659 atomic_set(&vsock->queued_replies, 0);
661 mutex_init(&vsock->tx_lock);
662 mutex_init(&vsock->rx_lock);
663 mutex_init(&vsock->event_lock);
664 skb_queue_head_init(&vsock->send_pkt_queue);
665 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
666 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
667 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
668 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
670 if (virtio_has_feature(vdev, VIRTIO_VSOCK_F_SEQPACKET))
671 vsock->seqpacket_allow = true;
675 ret = virtio_vsock_vqs_init(vsock);
679 rcu_assign_pointer(the_virtio_vsock, vsock);
680 virtio_vsock_vqs_start(vsock);
682 mutex_unlock(&the_virtio_vsock_mutex);
688 mutex_unlock(&the_virtio_vsock_mutex);
692 static void virtio_vsock_remove(struct virtio_device *vdev)
694 struct virtio_vsock *vsock = vdev->priv;
696 mutex_lock(&the_virtio_vsock_mutex);
699 rcu_assign_pointer(the_virtio_vsock, NULL);
702 virtio_vsock_vqs_del(vsock);
704 /* Other works can be queued before 'config->del_vqs()', so we flush
705 * all works before to free the vsock object to avoid use after free.
707 flush_work(&vsock->rx_work);
708 flush_work(&vsock->tx_work);
709 flush_work(&vsock->event_work);
710 flush_work(&vsock->send_pkt_work);
712 mutex_unlock(&the_virtio_vsock_mutex);
717 #ifdef CONFIG_PM_SLEEP
718 static int virtio_vsock_freeze(struct virtio_device *vdev)
720 struct virtio_vsock *vsock = vdev->priv;
722 mutex_lock(&the_virtio_vsock_mutex);
724 rcu_assign_pointer(the_virtio_vsock, NULL);
727 virtio_vsock_vqs_del(vsock);
729 mutex_unlock(&the_virtio_vsock_mutex);
734 static int virtio_vsock_restore(struct virtio_device *vdev)
736 struct virtio_vsock *vsock = vdev->priv;
739 mutex_lock(&the_virtio_vsock_mutex);
741 /* Only one virtio-vsock device per guest is supported */
742 if (rcu_dereference_protected(the_virtio_vsock,
743 lockdep_is_held(&the_virtio_vsock_mutex))) {
748 ret = virtio_vsock_vqs_init(vsock);
752 rcu_assign_pointer(the_virtio_vsock, vsock);
753 virtio_vsock_vqs_start(vsock);
756 mutex_unlock(&the_virtio_vsock_mutex);
759 #endif /* CONFIG_PM_SLEEP */
761 static struct virtio_device_id id_table[] = {
762 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
766 static unsigned int features[] = {
767 VIRTIO_VSOCK_F_SEQPACKET
770 static struct virtio_driver virtio_vsock_driver = {
771 .feature_table = features,
772 .feature_table_size = ARRAY_SIZE(features),
773 .driver.name = KBUILD_MODNAME,
774 .driver.owner = THIS_MODULE,
775 .id_table = id_table,
776 .probe = virtio_vsock_probe,
777 .remove = virtio_vsock_remove,
778 #ifdef CONFIG_PM_SLEEP
779 .freeze = virtio_vsock_freeze,
780 .restore = virtio_vsock_restore,
784 static int __init virtio_vsock_init(void)
788 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
789 if (!virtio_vsock_workqueue)
792 ret = vsock_core_register(&virtio_transport.transport,
793 VSOCK_TRANSPORT_F_G2H);
797 ret = register_virtio_driver(&virtio_vsock_driver);
804 vsock_core_unregister(&virtio_transport.transport);
806 destroy_workqueue(virtio_vsock_workqueue);
810 static void __exit virtio_vsock_exit(void)
812 unregister_virtio_driver(&virtio_vsock_driver);
813 vsock_core_unregister(&virtio_transport.transport);
814 destroy_workqueue(virtio_vsock_workqueue);
817 module_init(virtio_vsock_init);
818 module_exit(virtio_vsock_exit);
819 MODULE_LICENSE("GPL v2");
820 MODULE_AUTHOR("Asias He");
821 MODULE_DESCRIPTION("virtio transport for vsock");
822 MODULE_DEVICE_TABLE(virtio, id_table);