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.
42 struct work_struct send_pkt_work
;
43 spinlock_t send_pkt_list_lock
;
44 struct list_head send_pkt_list
;
46 struct work_struct loopback_work
;
47 spinlock_t loopback_list_lock
; /* protects loopback_list */
48 struct list_head loopback_list
;
50 atomic_t queued_replies
;
52 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
53 * must be accessed with rx_lock held.
59 /* The following fields are protected by event_lock.
60 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
62 struct mutex event_lock
;
63 struct virtio_vsock_event event_list
[8];
68 static struct virtio_vsock
*virtio_vsock_get(void)
70 return the_virtio_vsock
;
73 static u32
virtio_transport_get_local_cid(void)
75 struct virtio_vsock
*vsock
= virtio_vsock_get();
78 return VMADDR_CID_ANY
;
80 return vsock
->guest_cid
;
83 static void virtio_transport_loopback_work(struct work_struct
*work
)
85 struct virtio_vsock
*vsock
=
86 container_of(work
, struct virtio_vsock
, loopback_work
);
89 spin_lock_bh(&vsock
->loopback_list_lock
);
90 list_splice_init(&vsock
->loopback_list
, &pkts
);
91 spin_unlock_bh(&vsock
->loopback_list_lock
);
93 mutex_lock(&vsock
->rx_lock
);
94 while (!list_empty(&pkts
)) {
95 struct virtio_vsock_pkt
*pkt
;
97 pkt
= list_first_entry(&pkts
, struct virtio_vsock_pkt
, list
);
98 list_del_init(&pkt
->list
);
100 virtio_transport_recv_pkt(pkt
);
102 mutex_unlock(&vsock
->rx_lock
);
105 static int virtio_transport_send_pkt_loopback(struct virtio_vsock
*vsock
,
106 struct virtio_vsock_pkt
*pkt
)
110 spin_lock_bh(&vsock
->loopback_list_lock
);
111 list_add_tail(&pkt
->list
, &vsock
->loopback_list
);
112 spin_unlock_bh(&vsock
->loopback_list_lock
);
114 queue_work(virtio_vsock_workqueue
, &vsock
->loopback_work
);
120 virtio_transport_send_pkt_work(struct work_struct
*work
)
122 struct virtio_vsock
*vsock
=
123 container_of(work
, struct virtio_vsock
, send_pkt_work
);
124 struct virtqueue
*vq
;
126 bool restart_rx
= false;
128 mutex_lock(&vsock
->tx_lock
);
130 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
133 struct virtio_vsock_pkt
*pkt
;
134 struct scatterlist hdr
, buf
, *sgs
[2];
135 int ret
, in_sg
= 0, out_sg
= 0;
138 spin_lock_bh(&vsock
->send_pkt_list_lock
);
139 if (list_empty(&vsock
->send_pkt_list
)) {
140 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
144 pkt
= list_first_entry(&vsock
->send_pkt_list
,
145 struct virtio_vsock_pkt
, list
);
146 list_del_init(&pkt
->list
);
147 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
149 virtio_transport_deliver_tap_pkt(pkt
);
153 sg_init_one(&hdr
, &pkt
->hdr
, sizeof(pkt
->hdr
));
154 sgs
[out_sg
++] = &hdr
;
156 sg_init_one(&buf
, pkt
->buf
, pkt
->len
);
157 sgs
[out_sg
++] = &buf
;
160 ret
= virtqueue_add_sgs(vq
, sgs
, out_sg
, in_sg
, pkt
, GFP_KERNEL
);
161 /* Usually this means that there is no more space available in
165 spin_lock_bh(&vsock
->send_pkt_list_lock
);
166 list_add(&pkt
->list
, &vsock
->send_pkt_list
);
167 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
172 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
175 val
= atomic_dec_return(&vsock
->queued_replies
);
177 /* Do we now have resources to resume rx processing? */
178 if (val
+ 1 == virtqueue_get_vring_size(rx_vq
))
188 mutex_unlock(&vsock
->tx_lock
);
191 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
195 virtio_transport_send_pkt(struct virtio_vsock_pkt
*pkt
)
197 struct virtio_vsock
*vsock
;
200 vsock
= virtio_vsock_get();
202 virtio_transport_free_pkt(pkt
);
206 if (le64_to_cpu(pkt
->hdr
.dst_cid
) == vsock
->guest_cid
)
207 return virtio_transport_send_pkt_loopback(vsock
, pkt
);
210 atomic_inc(&vsock
->queued_replies
);
212 spin_lock_bh(&vsock
->send_pkt_list_lock
);
213 list_add_tail(&pkt
->list
, &vsock
->send_pkt_list
);
214 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
216 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
221 virtio_transport_cancel_pkt(struct vsock_sock
*vsk
)
223 struct virtio_vsock
*vsock
;
224 struct virtio_vsock_pkt
*pkt
, *n
;
228 vsock
= virtio_vsock_get();
233 spin_lock_bh(&vsock
->send_pkt_list_lock
);
234 list_for_each_entry_safe(pkt
, n
, &vsock
->send_pkt_list
, list
) {
237 list_move(&pkt
->list
, &freeme
);
239 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
241 list_for_each_entry_safe(pkt
, n
, &freeme
, list
) {
244 list_del(&pkt
->list
);
245 virtio_transport_free_pkt(pkt
);
249 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
252 new_cnt
= atomic_sub_return(cnt
, &vsock
->queued_replies
);
253 if (new_cnt
+ cnt
>= virtqueue_get_vring_size(rx_vq
) &&
254 new_cnt
< virtqueue_get_vring_size(rx_vq
))
255 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
261 static void virtio_vsock_rx_fill(struct virtio_vsock
*vsock
)
263 int buf_len
= VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE
;
264 struct virtio_vsock_pkt
*pkt
;
265 struct scatterlist hdr
, buf
, *sgs
[2];
266 struct virtqueue
*vq
;
269 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
272 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
276 pkt
->buf
= kmalloc(buf_len
, GFP_KERNEL
);
278 virtio_transport_free_pkt(pkt
);
284 sg_init_one(&hdr
, &pkt
->hdr
, sizeof(pkt
->hdr
));
287 sg_init_one(&buf
, pkt
->buf
, buf_len
);
289 ret
= virtqueue_add_sgs(vq
, sgs
, 0, 2, pkt
, GFP_KERNEL
);
291 virtio_transport_free_pkt(pkt
);
295 } while (vq
->num_free
);
296 if (vsock
->rx_buf_nr
> vsock
->rx_buf_max_nr
)
297 vsock
->rx_buf_max_nr
= vsock
->rx_buf_nr
;
301 static void virtio_transport_tx_work(struct work_struct
*work
)
303 struct virtio_vsock
*vsock
=
304 container_of(work
, struct virtio_vsock
, tx_work
);
305 struct virtqueue
*vq
;
308 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
309 mutex_lock(&vsock
->tx_lock
);
311 struct virtio_vsock_pkt
*pkt
;
314 virtqueue_disable_cb(vq
);
315 while ((pkt
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
316 virtio_transport_free_pkt(pkt
);
319 } while (!virtqueue_enable_cb(vq
));
320 mutex_unlock(&vsock
->tx_lock
);
323 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
326 /* Is there space left for replies to rx packets? */
327 static bool virtio_transport_more_replies(struct virtio_vsock
*vsock
)
329 struct virtqueue
*vq
= vsock
->vqs
[VSOCK_VQ_RX
];
332 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
333 val
= atomic_read(&vsock
->queued_replies
);
335 return val
< virtqueue_get_vring_size(vq
);
338 static void virtio_transport_rx_work(struct work_struct
*work
)
340 struct virtio_vsock
*vsock
=
341 container_of(work
, struct virtio_vsock
, rx_work
);
342 struct virtqueue
*vq
;
344 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
346 mutex_lock(&vsock
->rx_lock
);
349 virtqueue_disable_cb(vq
);
351 struct virtio_vsock_pkt
*pkt
;
354 if (!virtio_transport_more_replies(vsock
)) {
355 /* Stop rx until the device processes already
356 * pending replies. Leave rx virtqueue
357 * callbacks disabled.
362 pkt
= virtqueue_get_buf(vq
, &len
);
369 /* Drop short/long packets */
370 if (unlikely(len
< sizeof(pkt
->hdr
) ||
371 len
> sizeof(pkt
->hdr
) + pkt
->len
)) {
372 virtio_transport_free_pkt(pkt
);
376 pkt
->len
= len
- sizeof(pkt
->hdr
);
377 virtio_transport_deliver_tap_pkt(pkt
);
378 virtio_transport_recv_pkt(pkt
);
380 } while (!virtqueue_enable_cb(vq
));
383 if (vsock
->rx_buf_nr
< vsock
->rx_buf_max_nr
/ 2)
384 virtio_vsock_rx_fill(vsock
);
385 mutex_unlock(&vsock
->rx_lock
);
388 /* event_lock must be held */
389 static int virtio_vsock_event_fill_one(struct virtio_vsock
*vsock
,
390 struct virtio_vsock_event
*event
)
392 struct scatterlist sg
;
393 struct virtqueue
*vq
;
395 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
397 sg_init_one(&sg
, event
, sizeof(*event
));
399 return virtqueue_add_inbuf(vq
, &sg
, 1, event
, GFP_KERNEL
);
402 /* event_lock must be held */
403 static void virtio_vsock_event_fill(struct virtio_vsock
*vsock
)
407 for (i
= 0; i
< ARRAY_SIZE(vsock
->event_list
); i
++) {
408 struct virtio_vsock_event
*event
= &vsock
->event_list
[i
];
410 virtio_vsock_event_fill_one(vsock
, event
);
413 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
416 static void virtio_vsock_reset_sock(struct sock
*sk
)
419 sk
->sk_state
= TCP_CLOSE
;
420 sk
->sk_err
= ECONNRESET
;
421 sk
->sk_error_report(sk
);
425 static void virtio_vsock_update_guest_cid(struct virtio_vsock
*vsock
)
427 struct virtio_device
*vdev
= vsock
->vdev
;
430 vdev
->config
->get(vdev
, offsetof(struct virtio_vsock_config
, guest_cid
),
431 &guest_cid
, sizeof(guest_cid
));
432 vsock
->guest_cid
= le64_to_cpu(guest_cid
);
435 /* event_lock must be held */
436 static void virtio_vsock_event_handle(struct virtio_vsock
*vsock
,
437 struct virtio_vsock_event
*event
)
439 switch (le32_to_cpu(event
->id
)) {
440 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
:
441 virtio_vsock_update_guest_cid(vsock
);
442 vsock_for_each_connected_socket(virtio_vsock_reset_sock
);
447 static void virtio_transport_event_work(struct work_struct
*work
)
449 struct virtio_vsock
*vsock
=
450 container_of(work
, struct virtio_vsock
, event_work
);
451 struct virtqueue
*vq
;
453 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
455 mutex_lock(&vsock
->event_lock
);
458 struct virtio_vsock_event
*event
;
461 virtqueue_disable_cb(vq
);
462 while ((event
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
463 if (len
== sizeof(*event
))
464 virtio_vsock_event_handle(vsock
, event
);
466 virtio_vsock_event_fill_one(vsock
, event
);
468 } while (!virtqueue_enable_cb(vq
));
470 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
472 mutex_unlock(&vsock
->event_lock
);
475 static void virtio_vsock_event_done(struct virtqueue
*vq
)
477 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
481 queue_work(virtio_vsock_workqueue
, &vsock
->event_work
);
484 static void virtio_vsock_tx_done(struct virtqueue
*vq
)
486 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
490 queue_work(virtio_vsock_workqueue
, &vsock
->tx_work
);
493 static void virtio_vsock_rx_done(struct virtqueue
*vq
)
495 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
499 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
502 static struct virtio_transport virtio_transport
= {
504 .get_local_cid
= virtio_transport_get_local_cid
,
506 .init
= virtio_transport_do_socket_init
,
507 .destruct
= virtio_transport_destruct
,
508 .release
= virtio_transport_release
,
509 .connect
= virtio_transport_connect
,
510 .shutdown
= virtio_transport_shutdown
,
511 .cancel_pkt
= virtio_transport_cancel_pkt
,
513 .dgram_bind
= virtio_transport_dgram_bind
,
514 .dgram_dequeue
= virtio_transport_dgram_dequeue
,
515 .dgram_enqueue
= virtio_transport_dgram_enqueue
,
516 .dgram_allow
= virtio_transport_dgram_allow
,
518 .stream_dequeue
= virtio_transport_stream_dequeue
,
519 .stream_enqueue
= virtio_transport_stream_enqueue
,
520 .stream_has_data
= virtio_transport_stream_has_data
,
521 .stream_has_space
= virtio_transport_stream_has_space
,
522 .stream_rcvhiwat
= virtio_transport_stream_rcvhiwat
,
523 .stream_is_active
= virtio_transport_stream_is_active
,
524 .stream_allow
= virtio_transport_stream_allow
,
526 .notify_poll_in
= virtio_transport_notify_poll_in
,
527 .notify_poll_out
= virtio_transport_notify_poll_out
,
528 .notify_recv_init
= virtio_transport_notify_recv_init
,
529 .notify_recv_pre_block
= virtio_transport_notify_recv_pre_block
,
530 .notify_recv_pre_dequeue
= virtio_transport_notify_recv_pre_dequeue
,
531 .notify_recv_post_dequeue
= virtio_transport_notify_recv_post_dequeue
,
532 .notify_send_init
= virtio_transport_notify_send_init
,
533 .notify_send_pre_block
= virtio_transport_notify_send_pre_block
,
534 .notify_send_pre_enqueue
= virtio_transport_notify_send_pre_enqueue
,
535 .notify_send_post_enqueue
= virtio_transport_notify_send_post_enqueue
,
537 .set_buffer_size
= virtio_transport_set_buffer_size
,
538 .set_min_buffer_size
= virtio_transport_set_min_buffer_size
,
539 .set_max_buffer_size
= virtio_transport_set_max_buffer_size
,
540 .get_buffer_size
= virtio_transport_get_buffer_size
,
541 .get_min_buffer_size
= virtio_transport_get_min_buffer_size
,
542 .get_max_buffer_size
= virtio_transport_get_max_buffer_size
,
545 .send_pkt
= virtio_transport_send_pkt
,
548 static int virtio_vsock_probe(struct virtio_device
*vdev
)
550 vq_callback_t
*callbacks
[] = {
551 virtio_vsock_rx_done
,
552 virtio_vsock_tx_done
,
553 virtio_vsock_event_done
,
555 static const char * const names
[] = {
560 struct virtio_vsock
*vsock
= NULL
;
563 ret
= mutex_lock_interruptible(&the_virtio_vsock_mutex
);
567 /* Only one virtio-vsock device per guest is supported */
568 if (the_virtio_vsock
) {
573 vsock
= kzalloc(sizeof(*vsock
), GFP_KERNEL
);
581 ret
= virtio_find_vqs(vsock
->vdev
, VSOCK_VQ_MAX
,
582 vsock
->vqs
, callbacks
, names
,
587 virtio_vsock_update_guest_cid(vsock
);
589 vsock
->rx_buf_nr
= 0;
590 vsock
->rx_buf_max_nr
= 0;
591 atomic_set(&vsock
->queued_replies
, 0);
594 the_virtio_vsock
= vsock
;
595 mutex_init(&vsock
->tx_lock
);
596 mutex_init(&vsock
->rx_lock
);
597 mutex_init(&vsock
->event_lock
);
598 spin_lock_init(&vsock
->send_pkt_list_lock
);
599 INIT_LIST_HEAD(&vsock
->send_pkt_list
);
600 spin_lock_init(&vsock
->loopback_list_lock
);
601 INIT_LIST_HEAD(&vsock
->loopback_list
);
602 INIT_WORK(&vsock
->rx_work
, virtio_transport_rx_work
);
603 INIT_WORK(&vsock
->tx_work
, virtio_transport_tx_work
);
604 INIT_WORK(&vsock
->event_work
, virtio_transport_event_work
);
605 INIT_WORK(&vsock
->send_pkt_work
, virtio_transport_send_pkt_work
);
606 INIT_WORK(&vsock
->loopback_work
, virtio_transport_loopback_work
);
608 mutex_lock(&vsock
->rx_lock
);
609 virtio_vsock_rx_fill(vsock
);
610 mutex_unlock(&vsock
->rx_lock
);
612 mutex_lock(&vsock
->event_lock
);
613 virtio_vsock_event_fill(vsock
);
614 mutex_unlock(&vsock
->event_lock
);
616 mutex_unlock(&the_virtio_vsock_mutex
);
621 mutex_unlock(&the_virtio_vsock_mutex
);
625 static void virtio_vsock_remove(struct virtio_device
*vdev
)
627 struct virtio_vsock
*vsock
= vdev
->priv
;
628 struct virtio_vsock_pkt
*pkt
;
630 flush_work(&vsock
->loopback_work
);
631 flush_work(&vsock
->rx_work
);
632 flush_work(&vsock
->tx_work
);
633 flush_work(&vsock
->event_work
);
634 flush_work(&vsock
->send_pkt_work
);
636 /* Reset all connected sockets when the device disappear */
637 vsock_for_each_connected_socket(virtio_vsock_reset_sock
);
639 vdev
->config
->reset(vdev
);
641 mutex_lock(&vsock
->rx_lock
);
642 while ((pkt
= virtqueue_detach_unused_buf(vsock
->vqs
[VSOCK_VQ_RX
])))
643 virtio_transport_free_pkt(pkt
);
644 mutex_unlock(&vsock
->rx_lock
);
646 mutex_lock(&vsock
->tx_lock
);
647 while ((pkt
= virtqueue_detach_unused_buf(vsock
->vqs
[VSOCK_VQ_TX
])))
648 virtio_transport_free_pkt(pkt
);
649 mutex_unlock(&vsock
->tx_lock
);
651 spin_lock_bh(&vsock
->send_pkt_list_lock
);
652 while (!list_empty(&vsock
->send_pkt_list
)) {
653 pkt
= list_first_entry(&vsock
->send_pkt_list
,
654 struct virtio_vsock_pkt
, list
);
655 list_del(&pkt
->list
);
656 virtio_transport_free_pkt(pkt
);
658 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
660 spin_lock_bh(&vsock
->loopback_list_lock
);
661 while (!list_empty(&vsock
->loopback_list
)) {
662 pkt
= list_first_entry(&vsock
->loopback_list
,
663 struct virtio_vsock_pkt
, list
);
664 list_del(&pkt
->list
);
665 virtio_transport_free_pkt(pkt
);
667 spin_unlock_bh(&vsock
->loopback_list_lock
);
669 mutex_lock(&the_virtio_vsock_mutex
);
670 the_virtio_vsock
= NULL
;
671 mutex_unlock(&the_virtio_vsock_mutex
);
673 vdev
->config
->del_vqs(vdev
);
678 static struct virtio_device_id id_table
[] = {
679 { VIRTIO_ID_VSOCK
, VIRTIO_DEV_ANY_ID
},
683 static unsigned int features
[] = {
686 static struct virtio_driver virtio_vsock_driver
= {
687 .feature_table
= features
,
688 .feature_table_size
= ARRAY_SIZE(features
),
689 .driver
.name
= KBUILD_MODNAME
,
690 .driver
.owner
= THIS_MODULE
,
691 .id_table
= id_table
,
692 .probe
= virtio_vsock_probe
,
693 .remove
= virtio_vsock_remove
,
696 static int __init
virtio_vsock_init(void)
700 virtio_vsock_workqueue
= alloc_workqueue("virtio_vsock", 0, 0);
701 if (!virtio_vsock_workqueue
)
704 ret
= vsock_core_init(&virtio_transport
.transport
);
708 ret
= register_virtio_driver(&virtio_vsock_driver
);
717 destroy_workqueue(virtio_vsock_workqueue
);
721 static void __exit
virtio_vsock_exit(void)
723 unregister_virtio_driver(&virtio_vsock_driver
);
725 destroy_workqueue(virtio_vsock_workqueue
);
728 module_init(virtio_vsock_init
);
729 module_exit(virtio_vsock_exit
);
730 MODULE_LICENSE("GPL v2");
731 MODULE_AUTHOR("Asias He");
732 MODULE_DESCRIPTION("virtio transport for vsock");
733 MODULE_DEVICE_TABLE(virtio
, id_table
);