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.
43 struct work_struct send_pkt_work;
44 spinlock_t send_pkt_list_lock;
45 struct list_head send_pkt_list;
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.
56 /* The following fields are protected by event_lock.
57 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
59 struct mutex event_lock;
60 struct virtio_vsock_event event_list[8];
65 static struct virtio_vsock *virtio_vsock_get(void)
67 return the_virtio_vsock;
70 static u32 virtio_transport_get_local_cid(void)
72 struct virtio_vsock *vsock = virtio_vsock_get();
74 return vsock->guest_cid;
78 virtio_transport_send_pkt_work(struct work_struct *work)
80 struct virtio_vsock *vsock =
81 container_of(work, struct virtio_vsock, send_pkt_work);
84 bool restart_rx = false;
86 mutex_lock(&vsock->tx_lock);
88 vq = vsock->vqs[VSOCK_VQ_TX];
90 /* Avoid unnecessary interrupts while we're processing the ring */
91 virtqueue_disable_cb(vq);
94 struct virtio_vsock_pkt *pkt;
95 struct scatterlist hdr, buf, *sgs[2];
96 int ret, in_sg = 0, out_sg = 0;
99 spin_lock_bh(&vsock->send_pkt_list_lock);
100 if (list_empty(&vsock->send_pkt_list)) {
101 spin_unlock_bh(&vsock->send_pkt_list_lock);
102 virtqueue_enable_cb(vq);
106 pkt = list_first_entry(&vsock->send_pkt_list,
107 struct virtio_vsock_pkt, list);
108 list_del_init(&pkt->list);
109 spin_unlock_bh(&vsock->send_pkt_list_lock);
113 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
114 sgs[out_sg++] = &hdr;
116 sg_init_one(&buf, pkt->buf, pkt->len);
117 sgs[out_sg++] = &buf;
120 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
122 spin_lock_bh(&vsock->send_pkt_list_lock);
123 list_add(&pkt->list, &vsock->send_pkt_list);
124 spin_unlock_bh(&vsock->send_pkt_list_lock);
126 if (!virtqueue_enable_cb(vq) && ret == -ENOSPC)
127 continue; /* retry now that we have more space */
132 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
135 val = atomic_dec_return(&vsock->queued_replies);
137 /* Do we now have resources to resume rx processing? */
138 if (val + 1 == virtqueue_get_vring_size(rx_vq))
148 mutex_unlock(&vsock->tx_lock);
151 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
155 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
157 struct virtio_vsock *vsock;
160 vsock = virtio_vsock_get();
162 virtio_transport_free_pkt(pkt);
167 atomic_inc(&vsock->queued_replies);
169 spin_lock_bh(&vsock->send_pkt_list_lock);
170 list_add_tail(&pkt->list, &vsock->send_pkt_list);
171 spin_unlock_bh(&vsock->send_pkt_list_lock);
173 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
177 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
179 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
180 struct virtio_vsock_pkt *pkt;
181 struct scatterlist hdr, buf, *sgs[2];
182 struct virtqueue *vq;
185 vq = vsock->vqs[VSOCK_VQ_RX];
188 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
192 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
194 virtio_transport_free_pkt(pkt);
200 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
203 sg_init_one(&buf, pkt->buf, buf_len);
205 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
207 virtio_transport_free_pkt(pkt);
211 } while (vq->num_free);
212 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
213 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
217 static void virtio_transport_tx_work(struct work_struct *work)
219 struct virtio_vsock *vsock =
220 container_of(work, struct virtio_vsock, tx_work);
221 struct virtqueue *vq;
224 vq = vsock->vqs[VSOCK_VQ_TX];
225 mutex_lock(&vsock->tx_lock);
227 struct virtio_vsock_pkt *pkt;
230 virtqueue_disable_cb(vq);
231 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
232 virtio_transport_free_pkt(pkt);
235 } while (!virtqueue_enable_cb(vq));
236 mutex_unlock(&vsock->tx_lock);
239 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
242 /* Is there space left for replies to rx packets? */
243 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
245 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
248 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
249 val = atomic_read(&vsock->queued_replies);
251 return val < virtqueue_get_vring_size(vq);
254 static void virtio_transport_rx_work(struct work_struct *work)
256 struct virtio_vsock *vsock =
257 container_of(work, struct virtio_vsock, rx_work);
258 struct virtqueue *vq;
260 vq = vsock->vqs[VSOCK_VQ_RX];
262 mutex_lock(&vsock->rx_lock);
265 virtqueue_disable_cb(vq);
267 struct virtio_vsock_pkt *pkt;
270 if (!virtio_transport_more_replies(vsock)) {
271 /* Stop rx until the device processes already
272 * pending replies. Leave rx virtqueue
273 * callbacks disabled.
278 pkt = virtqueue_get_buf(vq, &len);
285 /* Drop short/long packets */
286 if (unlikely(len < sizeof(pkt->hdr) ||
287 len > sizeof(pkt->hdr) + pkt->len)) {
288 virtio_transport_free_pkt(pkt);
292 pkt->len = len - sizeof(pkt->hdr);
293 virtio_transport_recv_pkt(pkt);
295 } while (!virtqueue_enable_cb(vq));
298 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
299 virtio_vsock_rx_fill(vsock);
300 mutex_unlock(&vsock->rx_lock);
303 /* event_lock must be held */
304 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
305 struct virtio_vsock_event *event)
307 struct scatterlist sg;
308 struct virtqueue *vq;
310 vq = vsock->vqs[VSOCK_VQ_EVENT];
312 sg_init_one(&sg, event, sizeof(*event));
314 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
317 /* event_lock must be held */
318 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
322 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
323 struct virtio_vsock_event *event = &vsock->event_list[i];
325 virtio_vsock_event_fill_one(vsock, event);
328 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
331 static void virtio_vsock_reset_sock(struct sock *sk)
334 sk->sk_state = SS_UNCONNECTED;
335 sk->sk_err = ECONNRESET;
336 sk->sk_error_report(sk);
340 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
342 struct virtio_device *vdev = vsock->vdev;
345 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
346 &guest_cid, sizeof(guest_cid));
347 vsock->guest_cid = le64_to_cpu(guest_cid);
350 /* event_lock must be held */
351 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
352 struct virtio_vsock_event *event)
354 switch (le32_to_cpu(event->id)) {
355 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
356 virtio_vsock_update_guest_cid(vsock);
357 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
362 static void virtio_transport_event_work(struct work_struct *work)
364 struct virtio_vsock *vsock =
365 container_of(work, struct virtio_vsock, event_work);
366 struct virtqueue *vq;
368 vq = vsock->vqs[VSOCK_VQ_EVENT];
370 mutex_lock(&vsock->event_lock);
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 struct virtio_transport virtio_transport = {
419 .get_local_cid = virtio_transport_get_local_cid,
421 .init = virtio_transport_do_socket_init,
422 .destruct = virtio_transport_destruct,
423 .release = virtio_transport_release,
424 .connect = virtio_transport_connect,
425 .shutdown = virtio_transport_shutdown,
427 .dgram_bind = virtio_transport_dgram_bind,
428 .dgram_dequeue = virtio_transport_dgram_dequeue,
429 .dgram_enqueue = virtio_transport_dgram_enqueue,
430 .dgram_allow = virtio_transport_dgram_allow,
432 .stream_dequeue = virtio_transport_stream_dequeue,
433 .stream_enqueue = virtio_transport_stream_enqueue,
434 .stream_has_data = virtio_transport_stream_has_data,
435 .stream_has_space = virtio_transport_stream_has_space,
436 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
437 .stream_is_active = virtio_transport_stream_is_active,
438 .stream_allow = virtio_transport_stream_allow,
440 .notify_poll_in = virtio_transport_notify_poll_in,
441 .notify_poll_out = virtio_transport_notify_poll_out,
442 .notify_recv_init = virtio_transport_notify_recv_init,
443 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
444 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
445 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
446 .notify_send_init = virtio_transport_notify_send_init,
447 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
448 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
449 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
451 .set_buffer_size = virtio_transport_set_buffer_size,
452 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
453 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
454 .get_buffer_size = virtio_transport_get_buffer_size,
455 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
456 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
459 .send_pkt = virtio_transport_send_pkt,
462 static int virtio_vsock_probe(struct virtio_device *vdev)
464 vq_callback_t *callbacks[] = {
465 virtio_vsock_rx_done,
466 virtio_vsock_tx_done,
467 virtio_vsock_event_done,
469 static const char * const names[] = {
474 struct virtio_vsock *vsock = NULL;
477 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
481 /* Only one virtio-vsock device per guest is supported */
482 if (the_virtio_vsock) {
487 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
495 ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
496 vsock->vqs, callbacks, names);
500 virtio_vsock_update_guest_cid(vsock);
502 ret = vsock_core_init(&virtio_transport.transport);
506 vsock->rx_buf_nr = 0;
507 vsock->rx_buf_max_nr = 0;
508 atomic_set(&vsock->queued_replies, 0);
511 the_virtio_vsock = vsock;
512 mutex_init(&vsock->tx_lock);
513 mutex_init(&vsock->rx_lock);
514 mutex_init(&vsock->event_lock);
515 spin_lock_init(&vsock->send_pkt_list_lock);
516 INIT_LIST_HEAD(&vsock->send_pkt_list);
517 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
518 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
519 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
520 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
522 mutex_lock(&vsock->rx_lock);
523 virtio_vsock_rx_fill(vsock);
524 mutex_unlock(&vsock->rx_lock);
526 mutex_lock(&vsock->event_lock);
527 virtio_vsock_event_fill(vsock);
528 mutex_unlock(&vsock->event_lock);
530 mutex_unlock(&the_virtio_vsock_mutex);
534 vsock->vdev->config->del_vqs(vsock->vdev);
537 mutex_unlock(&the_virtio_vsock_mutex);
541 static void virtio_vsock_remove(struct virtio_device *vdev)
543 struct virtio_vsock *vsock = vdev->priv;
544 struct virtio_vsock_pkt *pkt;
546 flush_work(&vsock->rx_work);
547 flush_work(&vsock->tx_work);
548 flush_work(&vsock->event_work);
549 flush_work(&vsock->send_pkt_work);
551 vdev->config->reset(vdev);
553 mutex_lock(&vsock->rx_lock);
554 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
555 virtio_transport_free_pkt(pkt);
556 mutex_unlock(&vsock->rx_lock);
558 mutex_lock(&vsock->tx_lock);
559 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
560 virtio_transport_free_pkt(pkt);
561 mutex_unlock(&vsock->tx_lock);
563 spin_lock_bh(&vsock->send_pkt_list_lock);
564 while (!list_empty(&vsock->send_pkt_list)) {
565 pkt = list_first_entry(&vsock->send_pkt_list,
566 struct virtio_vsock_pkt, list);
567 list_del(&pkt->list);
568 virtio_transport_free_pkt(pkt);
570 spin_unlock_bh(&vsock->send_pkt_list_lock);
572 mutex_lock(&the_virtio_vsock_mutex);
573 the_virtio_vsock = NULL;
575 mutex_unlock(&the_virtio_vsock_mutex);
577 vdev->config->del_vqs(vdev);
582 static struct virtio_device_id id_table[] = {
583 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
587 static unsigned int features[] = {
590 static struct virtio_driver virtio_vsock_driver = {
591 .feature_table = features,
592 .feature_table_size = ARRAY_SIZE(features),
593 .driver.name = KBUILD_MODNAME,
594 .driver.owner = THIS_MODULE,
595 .id_table = id_table,
596 .probe = virtio_vsock_probe,
597 .remove = virtio_vsock_remove,
600 static int __init virtio_vsock_init(void)
604 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
605 if (!virtio_vsock_workqueue)
607 ret = register_virtio_driver(&virtio_vsock_driver);
609 destroy_workqueue(virtio_vsock_workqueue);
613 static void __exit virtio_vsock_exit(void)
615 unregister_virtio_driver(&virtio_vsock_driver);
616 destroy_workqueue(virtio_vsock_workqueue);
619 module_init(virtio_vsock_init);
620 module_exit(virtio_vsock_exit);
621 MODULE_LICENSE("GPL v2");
622 MODULE_AUTHOR("Asias He");
623 MODULE_DESCRIPTION("virtio transport for vsock");
624 MODULE_DEVICE_TABLE(virtio, id_table);