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 spinlock_t send_pkt_list_lock;
46 struct list_head send_pkt_list;
48 atomic_t queued_replies;
50 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
51 * must be accessed with rx_lock held.
58 /* The following fields are protected by event_lock.
59 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
61 struct mutex event_lock;
63 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 virtio_vsock_pkt *pkt;
104 struct scatterlist hdr, buf, *sgs[2];
105 int ret, in_sg = 0, out_sg = 0;
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);
114 pkt = list_first_entry(&vsock->send_pkt_list,
115 struct virtio_vsock_pkt, list);
116 list_del_init(&pkt->list);
117 spin_unlock_bh(&vsock->send_pkt_list_lock);
119 virtio_transport_deliver_tap_pkt(pkt);
123 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
124 sgs[out_sg++] = &hdr;
126 sg_init_one(&buf, pkt->buf, pkt->len);
127 sgs[out_sg++] = &buf;
130 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
131 /* Usually this means that there is no more space available in
135 spin_lock_bh(&vsock->send_pkt_list_lock);
136 list_add(&pkt->list, &vsock->send_pkt_list);
137 spin_unlock_bh(&vsock->send_pkt_list_lock);
142 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
145 val = atomic_dec_return(&vsock->queued_replies);
147 /* Do we now have resources to resume rx processing? */
148 if (val + 1 == virtqueue_get_vring_size(rx_vq))
159 mutex_unlock(&vsock->tx_lock);
162 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
166 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
168 struct virtio_vsock *vsock;
172 vsock = rcu_dereference(the_virtio_vsock);
174 virtio_transport_free_pkt(pkt);
179 if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) {
180 virtio_transport_free_pkt(pkt);
186 atomic_inc(&vsock->queued_replies);
188 spin_lock_bh(&vsock->send_pkt_list_lock);
189 list_add_tail(&pkt->list, &vsock->send_pkt_list);
190 spin_unlock_bh(&vsock->send_pkt_list_lock);
192 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
200 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
202 struct virtio_vsock *vsock;
203 struct virtio_vsock_pkt *pkt, *n;
208 vsock = rcu_dereference(the_virtio_vsock);
214 spin_lock_bh(&vsock->send_pkt_list_lock);
215 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
218 list_move(&pkt->list, &freeme);
220 spin_unlock_bh(&vsock->send_pkt_list_lock);
222 list_for_each_entry_safe(pkt, n, &freeme, list) {
225 list_del(&pkt->list);
226 virtio_transport_free_pkt(pkt);
230 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
233 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
234 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
235 new_cnt < virtqueue_get_vring_size(rx_vq))
236 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
246 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
248 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
249 struct virtio_vsock_pkt *pkt;
250 struct scatterlist hdr, buf, *sgs[2];
251 struct virtqueue *vq;
254 vq = vsock->vqs[VSOCK_VQ_RX];
257 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
261 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
263 virtio_transport_free_pkt(pkt);
267 pkt->buf_len = buf_len;
270 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
273 sg_init_one(&buf, pkt->buf, buf_len);
275 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
277 virtio_transport_free_pkt(pkt);
281 } while (vq->num_free);
282 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
283 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
287 static void virtio_transport_tx_work(struct work_struct *work)
289 struct virtio_vsock *vsock =
290 container_of(work, struct virtio_vsock, tx_work);
291 struct virtqueue *vq;
294 vq = vsock->vqs[VSOCK_VQ_TX];
295 mutex_lock(&vsock->tx_lock);
301 struct virtio_vsock_pkt *pkt;
304 virtqueue_disable_cb(vq);
305 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
306 virtio_transport_free_pkt(pkt);
309 } while (!virtqueue_enable_cb(vq));
312 mutex_unlock(&vsock->tx_lock);
315 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
318 /* Is there space left for replies to rx packets? */
319 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
321 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
324 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
325 val = atomic_read(&vsock->queued_replies);
327 return val < virtqueue_get_vring_size(vq);
330 /* event_lock must be held */
331 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
332 struct virtio_vsock_event *event)
334 struct scatterlist sg;
335 struct virtqueue *vq;
337 vq = vsock->vqs[VSOCK_VQ_EVENT];
339 sg_init_one(&sg, event, sizeof(*event));
341 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
344 /* event_lock must be held */
345 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
349 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
350 struct virtio_vsock_event *event = &vsock->event_list[i];
352 virtio_vsock_event_fill_one(vsock, event);
355 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
358 static void virtio_vsock_reset_sock(struct sock *sk)
360 /* vmci_transport.c doesn't take sk_lock here either. At least we're
361 * under vsock_table_lock so the sock cannot disappear while we're
365 sk->sk_state = TCP_CLOSE;
366 sk->sk_err = ECONNRESET;
367 sk->sk_error_report(sk);
370 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
372 struct virtio_device *vdev = vsock->vdev;
375 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
376 &guest_cid, sizeof(guest_cid));
377 vsock->guest_cid = le64_to_cpu(guest_cid);
380 /* event_lock must be held */
381 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
382 struct virtio_vsock_event *event)
384 switch (le32_to_cpu(event->id)) {
385 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
386 virtio_vsock_update_guest_cid(vsock);
387 vsock_for_each_connected_socket(&virtio_transport.transport,
388 virtio_vsock_reset_sock);
393 static void virtio_transport_event_work(struct work_struct *work)
395 struct virtio_vsock *vsock =
396 container_of(work, struct virtio_vsock, event_work);
397 struct virtqueue *vq;
399 vq = vsock->vqs[VSOCK_VQ_EVENT];
401 mutex_lock(&vsock->event_lock);
403 if (!vsock->event_run)
407 struct virtio_vsock_event *event;
410 virtqueue_disable_cb(vq);
411 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
412 if (len == sizeof(*event))
413 virtio_vsock_event_handle(vsock, event);
415 virtio_vsock_event_fill_one(vsock, event);
417 } while (!virtqueue_enable_cb(vq));
419 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
421 mutex_unlock(&vsock->event_lock);
424 static void virtio_vsock_event_done(struct virtqueue *vq)
426 struct virtio_vsock *vsock = vq->vdev->priv;
430 queue_work(virtio_vsock_workqueue, &vsock->event_work);
433 static void virtio_vsock_tx_done(struct virtqueue *vq)
435 struct virtio_vsock *vsock = vq->vdev->priv;
439 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
442 static void virtio_vsock_rx_done(struct virtqueue *vq)
444 struct virtio_vsock *vsock = vq->vdev->priv;
448 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
451 static struct virtio_transport virtio_transport = {
453 .module = THIS_MODULE,
455 .get_local_cid = virtio_transport_get_local_cid,
457 .init = virtio_transport_do_socket_init,
458 .destruct = virtio_transport_destruct,
459 .release = virtio_transport_release,
460 .connect = virtio_transport_connect,
461 .shutdown = virtio_transport_shutdown,
462 .cancel_pkt = virtio_transport_cancel_pkt,
464 .dgram_bind = virtio_transport_dgram_bind,
465 .dgram_dequeue = virtio_transport_dgram_dequeue,
466 .dgram_enqueue = virtio_transport_dgram_enqueue,
467 .dgram_allow = virtio_transport_dgram_allow,
469 .stream_dequeue = virtio_transport_stream_dequeue,
470 .stream_enqueue = virtio_transport_stream_enqueue,
471 .stream_has_data = virtio_transport_stream_has_data,
472 .stream_has_space = virtio_transport_stream_has_space,
473 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
474 .stream_is_active = virtio_transport_stream_is_active,
475 .stream_allow = virtio_transport_stream_allow,
477 .notify_poll_in = virtio_transport_notify_poll_in,
478 .notify_poll_out = virtio_transport_notify_poll_out,
479 .notify_recv_init = virtio_transport_notify_recv_init,
480 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
481 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
482 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
483 .notify_send_init = virtio_transport_notify_send_init,
484 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
485 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
486 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
487 .notify_buffer_size = virtio_transport_notify_buffer_size,
490 .send_pkt = virtio_transport_send_pkt,
493 static void virtio_transport_rx_work(struct work_struct *work)
495 struct virtio_vsock *vsock =
496 container_of(work, struct virtio_vsock, rx_work);
497 struct virtqueue *vq;
499 vq = vsock->vqs[VSOCK_VQ_RX];
501 mutex_lock(&vsock->rx_lock);
507 virtqueue_disable_cb(vq);
509 struct virtio_vsock_pkt *pkt;
512 if (!virtio_transport_more_replies(vsock)) {
513 /* Stop rx until the device processes already
514 * pending replies. Leave rx virtqueue
515 * callbacks disabled.
520 pkt = virtqueue_get_buf(vq, &len);
527 /* Drop short/long packets */
528 if (unlikely(len < sizeof(pkt->hdr) ||
529 len > sizeof(pkt->hdr) + pkt->len)) {
530 virtio_transport_free_pkt(pkt);
534 pkt->len = len - sizeof(pkt->hdr);
535 virtio_transport_deliver_tap_pkt(pkt);
536 virtio_transport_recv_pkt(&virtio_transport, pkt);
538 } while (!virtqueue_enable_cb(vq));
541 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
542 virtio_vsock_rx_fill(vsock);
543 mutex_unlock(&vsock->rx_lock);
546 static int virtio_vsock_probe(struct virtio_device *vdev)
548 vq_callback_t *callbacks[] = {
549 virtio_vsock_rx_done,
550 virtio_vsock_tx_done,
551 virtio_vsock_event_done,
553 static const char * const names[] = {
558 struct virtio_vsock *vsock = NULL;
561 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
565 /* Only one virtio-vsock device per guest is supported */
566 if (rcu_dereference_protected(the_virtio_vsock,
567 lockdep_is_held(&the_virtio_vsock_mutex))) {
572 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
580 ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
581 vsock->vqs, callbacks, names,
586 virtio_vsock_update_guest_cid(vsock);
588 vsock->rx_buf_nr = 0;
589 vsock->rx_buf_max_nr = 0;
590 atomic_set(&vsock->queued_replies, 0);
592 mutex_init(&vsock->tx_lock);
593 mutex_init(&vsock->rx_lock);
594 mutex_init(&vsock->event_lock);
595 spin_lock_init(&vsock->send_pkt_list_lock);
596 INIT_LIST_HEAD(&vsock->send_pkt_list);
597 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
598 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
599 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
600 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
602 mutex_lock(&vsock->tx_lock);
603 vsock->tx_run = true;
604 mutex_unlock(&vsock->tx_lock);
606 mutex_lock(&vsock->rx_lock);
607 virtio_vsock_rx_fill(vsock);
608 vsock->rx_run = true;
609 mutex_unlock(&vsock->rx_lock);
611 mutex_lock(&vsock->event_lock);
612 virtio_vsock_event_fill(vsock);
613 vsock->event_run = true;
614 mutex_unlock(&vsock->event_lock);
617 rcu_assign_pointer(the_virtio_vsock, vsock);
619 mutex_unlock(&the_virtio_vsock_mutex);
624 mutex_unlock(&the_virtio_vsock_mutex);
628 static void virtio_vsock_remove(struct virtio_device *vdev)
630 struct virtio_vsock *vsock = vdev->priv;
631 struct virtio_vsock_pkt *pkt;
633 mutex_lock(&the_virtio_vsock_mutex);
636 rcu_assign_pointer(the_virtio_vsock, NULL);
639 /* Reset all connected sockets when the device disappear */
640 vsock_for_each_connected_socket(&virtio_transport.transport,
641 virtio_vsock_reset_sock);
643 /* Stop all work handlers to make sure no one is accessing the device,
644 * so we can safely call vdev->config->reset().
646 mutex_lock(&vsock->rx_lock);
647 vsock->rx_run = false;
648 mutex_unlock(&vsock->rx_lock);
650 mutex_lock(&vsock->tx_lock);
651 vsock->tx_run = false;
652 mutex_unlock(&vsock->tx_lock);
654 mutex_lock(&vsock->event_lock);
655 vsock->event_run = false;
656 mutex_unlock(&vsock->event_lock);
658 /* Flush all device writes and interrupts, device will not use any
661 vdev->config->reset(vdev);
663 mutex_lock(&vsock->rx_lock);
664 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
665 virtio_transport_free_pkt(pkt);
666 mutex_unlock(&vsock->rx_lock);
668 mutex_lock(&vsock->tx_lock);
669 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
670 virtio_transport_free_pkt(pkt);
671 mutex_unlock(&vsock->tx_lock);
673 spin_lock_bh(&vsock->send_pkt_list_lock);
674 while (!list_empty(&vsock->send_pkt_list)) {
675 pkt = list_first_entry(&vsock->send_pkt_list,
676 struct virtio_vsock_pkt, list);
677 list_del(&pkt->list);
678 virtio_transport_free_pkt(pkt);
680 spin_unlock_bh(&vsock->send_pkt_list_lock);
682 /* Delete virtqueues and flush outstanding callbacks if any */
683 vdev->config->del_vqs(vdev);
685 /* Other works can be queued before 'config->del_vqs()', so we flush
686 * all works before to free the vsock object to avoid use after free.
688 flush_work(&vsock->rx_work);
689 flush_work(&vsock->tx_work);
690 flush_work(&vsock->event_work);
691 flush_work(&vsock->send_pkt_work);
693 mutex_unlock(&the_virtio_vsock_mutex);
698 static struct virtio_device_id id_table[] = {
699 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
703 static unsigned int features[] = {
706 static struct virtio_driver virtio_vsock_driver = {
707 .feature_table = features,
708 .feature_table_size = ARRAY_SIZE(features),
709 .driver.name = KBUILD_MODNAME,
710 .driver.owner = THIS_MODULE,
711 .id_table = id_table,
712 .probe = virtio_vsock_probe,
713 .remove = virtio_vsock_remove,
716 static int __init virtio_vsock_init(void)
720 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
721 if (!virtio_vsock_workqueue)
724 ret = vsock_core_register(&virtio_transport.transport,
725 VSOCK_TRANSPORT_F_G2H);
729 ret = register_virtio_driver(&virtio_vsock_driver);
736 vsock_core_unregister(&virtio_transport.transport);
738 destroy_workqueue(virtio_vsock_workqueue);
742 static void __exit virtio_vsock_exit(void)
744 unregister_virtio_driver(&virtio_vsock_driver);
745 vsock_core_unregister(&virtio_transport.transport);
746 destroy_workqueue(virtio_vsock_workqueue);
749 module_init(virtio_vsock_init);
750 module_exit(virtio_vsock_exit);
751 MODULE_LICENSE("GPL v2");
752 MODULE_AUTHOR("Asias He");
753 MODULE_DESCRIPTION("virtio transport for vsock");
754 MODULE_DEVICE_TABLE(virtio, id_table);