2 * virtio transport for vsock
4 * Copyright (C) 2013-2015 Red Hat, Inc.
5 * Author: Asias He <asias@redhat.com>
6 * Stefan Hajnoczi <stefanha@redhat.com>
8 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
9 * early virtio-vsock proof-of-concept bits.
11 * This work is licensed under the terms of the GNU GPL, version 2.
13 #include <linux/spinlock.h>
14 #include <linux/module.h>
15 #include <linux/list.h>
16 #include <linux/atomic.h>
17 #include <linux/virtio.h>
18 #include <linux/virtio_ids.h>
19 #include <linux/virtio_config.h>
20 #include <linux/virtio_vsock.h>
22 #include <linux/mutex.h>
23 #include <net/af_vsock.h>
25 static struct workqueue_struct *virtio_vsock_workqueue;
26 static struct virtio_vsock *the_virtio_vsock;
27 static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
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);
121 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
122 sgs[out_sg++] = &hdr;
124 sg_init_one(&buf, pkt->buf, pkt->len);
125 sgs[out_sg++] = &buf;
128 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
129 /* Usually this means that there is no more space available in
133 spin_lock_bh(&vsock->send_pkt_list_lock);
134 list_add(&pkt->list, &vsock->send_pkt_list);
135 spin_unlock_bh(&vsock->send_pkt_list_lock);
140 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
143 val = atomic_dec_return(&vsock->queued_replies);
145 /* Do we now have resources to resume rx processing? */
146 if (val + 1 == virtqueue_get_vring_size(rx_vq))
157 mutex_unlock(&vsock->tx_lock);
160 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
164 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
166 struct virtio_vsock *vsock;
170 vsock = rcu_dereference(the_virtio_vsock);
172 virtio_transport_free_pkt(pkt);
178 atomic_inc(&vsock->queued_replies);
180 spin_lock_bh(&vsock->send_pkt_list_lock);
181 list_add_tail(&pkt->list, &vsock->send_pkt_list);
182 spin_unlock_bh(&vsock->send_pkt_list_lock);
184 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
191 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
193 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
194 struct virtio_vsock_pkt *pkt;
195 struct scatterlist hdr, buf, *sgs[2];
196 struct virtqueue *vq;
199 vq = vsock->vqs[VSOCK_VQ_RX];
202 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
206 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
208 virtio_transport_free_pkt(pkt);
214 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
217 sg_init_one(&buf, pkt->buf, buf_len);
219 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
221 virtio_transport_free_pkt(pkt);
225 } while (vq->num_free);
226 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
227 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
231 static void virtio_transport_tx_work(struct work_struct *work)
233 struct virtio_vsock *vsock =
234 container_of(work, struct virtio_vsock, tx_work);
235 struct virtqueue *vq;
238 vq = vsock->vqs[VSOCK_VQ_TX];
239 mutex_lock(&vsock->tx_lock);
245 struct virtio_vsock_pkt *pkt;
248 virtqueue_disable_cb(vq);
249 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
250 virtio_transport_free_pkt(pkt);
253 } while (!virtqueue_enable_cb(vq));
256 mutex_unlock(&vsock->tx_lock);
259 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
262 /* Is there space left for replies to rx packets? */
263 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
265 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
268 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
269 val = atomic_read(&vsock->queued_replies);
271 return val < virtqueue_get_vring_size(vq);
274 static void virtio_transport_rx_work(struct work_struct *work)
276 struct virtio_vsock *vsock =
277 container_of(work, struct virtio_vsock, rx_work);
278 struct virtqueue *vq;
280 vq = vsock->vqs[VSOCK_VQ_RX];
282 mutex_lock(&vsock->rx_lock);
288 virtqueue_disable_cb(vq);
290 struct virtio_vsock_pkt *pkt;
293 if (!virtio_transport_more_replies(vsock)) {
294 /* Stop rx until the device processes already
295 * pending replies. Leave rx virtqueue
296 * callbacks disabled.
301 pkt = virtqueue_get_buf(vq, &len);
308 /* Drop short/long packets */
309 if (unlikely(len < sizeof(pkt->hdr) ||
310 len > sizeof(pkt->hdr) + pkt->len)) {
311 virtio_transport_free_pkt(pkt);
315 pkt->len = len - sizeof(pkt->hdr);
316 virtio_transport_recv_pkt(pkt);
318 } while (!virtqueue_enable_cb(vq));
321 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
322 virtio_vsock_rx_fill(vsock);
323 mutex_unlock(&vsock->rx_lock);
326 /* event_lock must be held */
327 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
328 struct virtio_vsock_event *event)
330 struct scatterlist sg;
331 struct virtqueue *vq;
333 vq = vsock->vqs[VSOCK_VQ_EVENT];
335 sg_init_one(&sg, event, sizeof(*event));
337 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
340 /* event_lock must be held */
341 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
345 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
346 struct virtio_vsock_event *event = &vsock->event_list[i];
348 virtio_vsock_event_fill_one(vsock, event);
351 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
354 static void virtio_vsock_reset_sock(struct sock *sk)
357 sk->sk_state = SS_UNCONNECTED;
358 sk->sk_err = ECONNRESET;
359 sk->sk_error_report(sk);
363 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
365 struct virtio_device *vdev = vsock->vdev;
368 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
369 &guest_cid, sizeof(guest_cid));
370 vsock->guest_cid = le64_to_cpu(guest_cid);
373 /* event_lock must be held */
374 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
375 struct virtio_vsock_event *event)
377 switch (le32_to_cpu(event->id)) {
378 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
379 virtio_vsock_update_guest_cid(vsock);
380 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
385 static void virtio_transport_event_work(struct work_struct *work)
387 struct virtio_vsock *vsock =
388 container_of(work, struct virtio_vsock, event_work);
389 struct virtqueue *vq;
391 vq = vsock->vqs[VSOCK_VQ_EVENT];
393 mutex_lock(&vsock->event_lock);
395 if (!vsock->event_run)
399 struct virtio_vsock_event *event;
402 virtqueue_disable_cb(vq);
403 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
404 if (len == sizeof(*event))
405 virtio_vsock_event_handle(vsock, event);
407 virtio_vsock_event_fill_one(vsock, event);
409 } while (!virtqueue_enable_cb(vq));
411 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
413 mutex_unlock(&vsock->event_lock);
416 static void virtio_vsock_event_done(struct virtqueue *vq)
418 struct virtio_vsock *vsock = vq->vdev->priv;
422 queue_work(virtio_vsock_workqueue, &vsock->event_work);
425 static void virtio_vsock_tx_done(struct virtqueue *vq)
427 struct virtio_vsock *vsock = vq->vdev->priv;
431 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
434 static void virtio_vsock_rx_done(struct virtqueue *vq)
436 struct virtio_vsock *vsock = vq->vdev->priv;
440 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
443 static struct virtio_transport virtio_transport = {
445 .get_local_cid = virtio_transport_get_local_cid,
447 .init = virtio_transport_do_socket_init,
448 .destruct = virtio_transport_destruct,
449 .release = virtio_transport_release,
450 .connect = virtio_transport_connect,
451 .shutdown = virtio_transport_shutdown,
453 .dgram_bind = virtio_transport_dgram_bind,
454 .dgram_dequeue = virtio_transport_dgram_dequeue,
455 .dgram_enqueue = virtio_transport_dgram_enqueue,
456 .dgram_allow = virtio_transport_dgram_allow,
458 .stream_dequeue = virtio_transport_stream_dequeue,
459 .stream_enqueue = virtio_transport_stream_enqueue,
460 .stream_has_data = virtio_transport_stream_has_data,
461 .stream_has_space = virtio_transport_stream_has_space,
462 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
463 .stream_is_active = virtio_transport_stream_is_active,
464 .stream_allow = virtio_transport_stream_allow,
466 .notify_poll_in = virtio_transport_notify_poll_in,
467 .notify_poll_out = virtio_transport_notify_poll_out,
468 .notify_recv_init = virtio_transport_notify_recv_init,
469 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
470 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
471 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
472 .notify_send_init = virtio_transport_notify_send_init,
473 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
474 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
475 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
477 .set_buffer_size = virtio_transport_set_buffer_size,
478 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
479 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
480 .get_buffer_size = virtio_transport_get_buffer_size,
481 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
482 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
485 .send_pkt = virtio_transport_send_pkt,
488 static int virtio_vsock_probe(struct virtio_device *vdev)
490 vq_callback_t *callbacks[] = {
491 virtio_vsock_rx_done,
492 virtio_vsock_tx_done,
493 virtio_vsock_event_done,
495 static const char * const names[] = {
500 struct virtio_vsock *vsock = NULL;
503 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
507 /* Only one virtio-vsock device per guest is supported */
508 if (rcu_dereference_protected(the_virtio_vsock,
509 lockdep_is_held(&the_virtio_vsock_mutex))) {
514 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
522 ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
523 vsock->vqs, callbacks, names);
527 virtio_vsock_update_guest_cid(vsock);
529 vsock->rx_buf_nr = 0;
530 vsock->rx_buf_max_nr = 0;
531 atomic_set(&vsock->queued_replies, 0);
533 mutex_init(&vsock->tx_lock);
534 mutex_init(&vsock->rx_lock);
535 mutex_init(&vsock->event_lock);
536 spin_lock_init(&vsock->send_pkt_list_lock);
537 INIT_LIST_HEAD(&vsock->send_pkt_list);
538 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
539 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
540 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
541 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
543 mutex_lock(&vsock->tx_lock);
544 vsock->tx_run = true;
545 mutex_unlock(&vsock->tx_lock);
547 mutex_lock(&vsock->rx_lock);
548 virtio_vsock_rx_fill(vsock);
549 vsock->rx_run = true;
550 mutex_unlock(&vsock->rx_lock);
552 mutex_lock(&vsock->event_lock);
553 virtio_vsock_event_fill(vsock);
554 vsock->event_run = true;
555 mutex_unlock(&vsock->event_lock);
558 rcu_assign_pointer(the_virtio_vsock, vsock);
560 mutex_unlock(&the_virtio_vsock_mutex);
565 mutex_unlock(&the_virtio_vsock_mutex);
569 static void virtio_vsock_remove(struct virtio_device *vdev)
571 struct virtio_vsock *vsock = vdev->priv;
572 struct virtio_vsock_pkt *pkt;
574 mutex_lock(&the_virtio_vsock_mutex);
577 rcu_assign_pointer(the_virtio_vsock, NULL);
580 flush_work(&vsock->rx_work);
581 flush_work(&vsock->tx_work);
582 flush_work(&vsock->event_work);
583 flush_work(&vsock->send_pkt_work);
585 /* Reset all connected sockets when the device disappear */
586 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
588 /* Stop all work handlers to make sure no one is accessing the device,
589 * so we can safely call vdev->config->reset().
591 mutex_lock(&vsock->rx_lock);
592 vsock->rx_run = false;
593 mutex_unlock(&vsock->rx_lock);
595 mutex_lock(&vsock->tx_lock);
596 vsock->tx_run = false;
597 mutex_unlock(&vsock->tx_lock);
599 mutex_lock(&vsock->event_lock);
600 vsock->event_run = false;
601 mutex_unlock(&vsock->event_lock);
603 /* Flush all device writes and interrupts, device will not use any
606 vdev->config->reset(vdev);
608 mutex_lock(&vsock->rx_lock);
609 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
610 virtio_transport_free_pkt(pkt);
611 mutex_unlock(&vsock->rx_lock);
613 mutex_lock(&vsock->tx_lock);
614 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
615 virtio_transport_free_pkt(pkt);
616 mutex_unlock(&vsock->tx_lock);
618 spin_lock_bh(&vsock->send_pkt_list_lock);
619 while (!list_empty(&vsock->send_pkt_list)) {
620 pkt = list_first_entry(&vsock->send_pkt_list,
621 struct virtio_vsock_pkt, list);
622 list_del(&pkt->list);
623 virtio_transport_free_pkt(pkt);
625 spin_unlock_bh(&vsock->send_pkt_list_lock);
627 /* Delete virtqueues and flush outstanding callbacks if any */
628 vdev->config->del_vqs(vdev);
630 mutex_unlock(&the_virtio_vsock_mutex);
635 static struct virtio_device_id id_table[] = {
636 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
640 static unsigned int features[] = {
643 static struct virtio_driver virtio_vsock_driver = {
644 .feature_table = features,
645 .feature_table_size = ARRAY_SIZE(features),
646 .driver.name = KBUILD_MODNAME,
647 .driver.owner = THIS_MODULE,
648 .id_table = id_table,
649 .probe = virtio_vsock_probe,
650 .remove = virtio_vsock_remove,
653 static int __init virtio_vsock_init(void)
657 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
658 if (!virtio_vsock_workqueue)
661 ret = vsock_core_init(&virtio_transport.transport);
665 ret = register_virtio_driver(&virtio_vsock_driver);
674 destroy_workqueue(virtio_vsock_workqueue);
678 static void __exit virtio_vsock_exit(void)
680 unregister_virtio_driver(&virtio_vsock_driver);
682 destroy_workqueue(virtio_vsock_workqueue);
685 module_init(virtio_vsock_init);
686 module_exit(virtio_vsock_exit);
687 MODULE_LICENSE("GPL v2");
688 MODULE_AUTHOR("Asias He");
689 MODULE_DESCRIPTION("virtio transport for vsock");
690 MODULE_DEVICE_TABLE(virtio, id_table);