Linux 4.11-rc6
[linux/fpc-iii.git] / net / vmw_vsock / virtio_transport.c
blob68675a151f22b8b63c02b25a67b833d9a6046d84
1 /*
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>
21 #include <net/sock.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 */
29 struct 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.
41 struct mutex tx_lock;
43 struct work_struct send_pkt_work;
44 spinlock_t send_pkt_list_lock;
45 struct list_head send_pkt_list;
47 struct work_struct loopback_work;
48 spinlock_t loopback_list_lock; /* protects loopback_list */
49 struct list_head loopback_list;
51 atomic_t queued_replies;
53 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
54 * must be accessed with rx_lock held.
56 struct mutex rx_lock;
57 int rx_buf_nr;
58 int rx_buf_max_nr;
60 /* The following fields are protected by event_lock.
61 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
63 struct mutex event_lock;
64 struct virtio_vsock_event event_list[8];
66 u32 guest_cid;
69 static struct virtio_vsock *virtio_vsock_get(void)
71 return the_virtio_vsock;
74 static u32 virtio_transport_get_local_cid(void)
76 struct virtio_vsock *vsock = virtio_vsock_get();
78 return vsock->guest_cid;
81 static void virtio_transport_loopback_work(struct work_struct *work)
83 struct virtio_vsock *vsock =
84 container_of(work, struct virtio_vsock, loopback_work);
85 LIST_HEAD(pkts);
87 spin_lock_bh(&vsock->loopback_list_lock);
88 list_splice_init(&vsock->loopback_list, &pkts);
89 spin_unlock_bh(&vsock->loopback_list_lock);
91 mutex_lock(&vsock->rx_lock);
92 while (!list_empty(&pkts)) {
93 struct virtio_vsock_pkt *pkt;
95 pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
96 list_del_init(&pkt->list);
98 virtio_transport_recv_pkt(pkt);
100 mutex_unlock(&vsock->rx_lock);
103 static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock,
104 struct virtio_vsock_pkt *pkt)
106 int len = pkt->len;
108 spin_lock_bh(&vsock->loopback_list_lock);
109 list_add_tail(&pkt->list, &vsock->loopback_list);
110 spin_unlock_bh(&vsock->loopback_list_lock);
112 queue_work(virtio_vsock_workqueue, &vsock->loopback_work);
114 return len;
117 static void
118 virtio_transport_send_pkt_work(struct work_struct *work)
120 struct virtio_vsock *vsock =
121 container_of(work, struct virtio_vsock, send_pkt_work);
122 struct virtqueue *vq;
123 bool added = false;
124 bool restart_rx = false;
126 mutex_lock(&vsock->tx_lock);
128 vq = vsock->vqs[VSOCK_VQ_TX];
130 for (;;) {
131 struct virtio_vsock_pkt *pkt;
132 struct scatterlist hdr, buf, *sgs[2];
133 int ret, in_sg = 0, out_sg = 0;
134 bool reply;
136 spin_lock_bh(&vsock->send_pkt_list_lock);
137 if (list_empty(&vsock->send_pkt_list)) {
138 spin_unlock_bh(&vsock->send_pkt_list_lock);
139 break;
142 pkt = list_first_entry(&vsock->send_pkt_list,
143 struct virtio_vsock_pkt, list);
144 list_del_init(&pkt->list);
145 spin_unlock_bh(&vsock->send_pkt_list_lock);
147 reply = pkt->reply;
149 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
150 sgs[out_sg++] = &hdr;
151 if (pkt->buf) {
152 sg_init_one(&buf, pkt->buf, pkt->len);
153 sgs[out_sg++] = &buf;
156 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
157 /* Usually this means that there is no more space available in
158 * the vq
160 if (ret < 0) {
161 spin_lock_bh(&vsock->send_pkt_list_lock);
162 list_add(&pkt->list, &vsock->send_pkt_list);
163 spin_unlock_bh(&vsock->send_pkt_list_lock);
164 break;
167 if (reply) {
168 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
169 int val;
171 val = atomic_dec_return(&vsock->queued_replies);
173 /* Do we now have resources to resume rx processing? */
174 if (val + 1 == virtqueue_get_vring_size(rx_vq))
175 restart_rx = true;
178 added = true;
181 if (added)
182 virtqueue_kick(vq);
184 mutex_unlock(&vsock->tx_lock);
186 if (restart_rx)
187 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
190 static int
191 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
193 struct virtio_vsock *vsock;
194 int len = pkt->len;
196 vsock = virtio_vsock_get();
197 if (!vsock) {
198 virtio_transport_free_pkt(pkt);
199 return -ENODEV;
202 if (le32_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid)
203 return virtio_transport_send_pkt_loopback(vsock, pkt);
205 if (pkt->reply)
206 atomic_inc(&vsock->queued_replies);
208 spin_lock_bh(&vsock->send_pkt_list_lock);
209 list_add_tail(&pkt->list, &vsock->send_pkt_list);
210 spin_unlock_bh(&vsock->send_pkt_list_lock);
212 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
213 return len;
216 static int
217 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
219 struct virtio_vsock *vsock;
220 struct virtio_vsock_pkt *pkt, *n;
221 int cnt = 0;
222 LIST_HEAD(freeme);
224 vsock = virtio_vsock_get();
225 if (!vsock) {
226 return -ENODEV;
229 spin_lock_bh(&vsock->send_pkt_list_lock);
230 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
231 if (pkt->vsk != vsk)
232 continue;
233 list_move(&pkt->list, &freeme);
235 spin_unlock_bh(&vsock->send_pkt_list_lock);
237 list_for_each_entry_safe(pkt, n, &freeme, list) {
238 if (pkt->reply)
239 cnt++;
240 list_del(&pkt->list);
241 virtio_transport_free_pkt(pkt);
244 if (cnt) {
245 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
246 int new_cnt;
248 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
249 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
250 new_cnt < virtqueue_get_vring_size(rx_vq))
251 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
254 return 0;
257 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
259 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
260 struct virtio_vsock_pkt *pkt;
261 struct scatterlist hdr, buf, *sgs[2];
262 struct virtqueue *vq;
263 int ret;
265 vq = vsock->vqs[VSOCK_VQ_RX];
267 do {
268 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
269 if (!pkt)
270 break;
272 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
273 if (!pkt->buf) {
274 virtio_transport_free_pkt(pkt);
275 break;
278 pkt->len = buf_len;
280 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
281 sgs[0] = &hdr;
283 sg_init_one(&buf, pkt->buf, buf_len);
284 sgs[1] = &buf;
285 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
286 if (ret) {
287 virtio_transport_free_pkt(pkt);
288 break;
290 vsock->rx_buf_nr++;
291 } while (vq->num_free);
292 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
293 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
294 virtqueue_kick(vq);
297 static void virtio_transport_tx_work(struct work_struct *work)
299 struct virtio_vsock *vsock =
300 container_of(work, struct virtio_vsock, tx_work);
301 struct virtqueue *vq;
302 bool added = false;
304 vq = vsock->vqs[VSOCK_VQ_TX];
305 mutex_lock(&vsock->tx_lock);
306 do {
307 struct virtio_vsock_pkt *pkt;
308 unsigned int len;
310 virtqueue_disable_cb(vq);
311 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
312 virtio_transport_free_pkt(pkt);
313 added = true;
315 } while (!virtqueue_enable_cb(vq));
316 mutex_unlock(&vsock->tx_lock);
318 if (added)
319 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
322 /* Is there space left for replies to rx packets? */
323 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
325 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
326 int val;
328 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
329 val = atomic_read(&vsock->queued_replies);
331 return val < virtqueue_get_vring_size(vq);
334 static void virtio_transport_rx_work(struct work_struct *work)
336 struct virtio_vsock *vsock =
337 container_of(work, struct virtio_vsock, rx_work);
338 struct virtqueue *vq;
340 vq = vsock->vqs[VSOCK_VQ_RX];
342 mutex_lock(&vsock->rx_lock);
344 do {
345 virtqueue_disable_cb(vq);
346 for (;;) {
347 struct virtio_vsock_pkt *pkt;
348 unsigned int len;
350 if (!virtio_transport_more_replies(vsock)) {
351 /* Stop rx until the device processes already
352 * pending replies. Leave rx virtqueue
353 * callbacks disabled.
355 goto out;
358 pkt = virtqueue_get_buf(vq, &len);
359 if (!pkt) {
360 break;
363 vsock->rx_buf_nr--;
365 /* Drop short/long packets */
366 if (unlikely(len < sizeof(pkt->hdr) ||
367 len > sizeof(pkt->hdr) + pkt->len)) {
368 virtio_transport_free_pkt(pkt);
369 continue;
372 pkt->len = len - sizeof(pkt->hdr);
373 virtio_transport_recv_pkt(pkt);
375 } while (!virtqueue_enable_cb(vq));
377 out:
378 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
379 virtio_vsock_rx_fill(vsock);
380 mutex_unlock(&vsock->rx_lock);
383 /* event_lock must be held */
384 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
385 struct virtio_vsock_event *event)
387 struct scatterlist sg;
388 struct virtqueue *vq;
390 vq = vsock->vqs[VSOCK_VQ_EVENT];
392 sg_init_one(&sg, event, sizeof(*event));
394 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
397 /* event_lock must be held */
398 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
400 size_t i;
402 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
403 struct virtio_vsock_event *event = &vsock->event_list[i];
405 virtio_vsock_event_fill_one(vsock, event);
408 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
411 static void virtio_vsock_reset_sock(struct sock *sk)
413 lock_sock(sk);
414 sk->sk_state = SS_UNCONNECTED;
415 sk->sk_err = ECONNRESET;
416 sk->sk_error_report(sk);
417 release_sock(sk);
420 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
422 struct virtio_device *vdev = vsock->vdev;
423 __le64 guest_cid;
425 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
426 &guest_cid, sizeof(guest_cid));
427 vsock->guest_cid = le64_to_cpu(guest_cid);
430 /* event_lock must be held */
431 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
432 struct virtio_vsock_event *event)
434 switch (le32_to_cpu(event->id)) {
435 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
436 virtio_vsock_update_guest_cid(vsock);
437 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
438 break;
442 static void virtio_transport_event_work(struct work_struct *work)
444 struct virtio_vsock *vsock =
445 container_of(work, struct virtio_vsock, event_work);
446 struct virtqueue *vq;
448 vq = vsock->vqs[VSOCK_VQ_EVENT];
450 mutex_lock(&vsock->event_lock);
452 do {
453 struct virtio_vsock_event *event;
454 unsigned int len;
456 virtqueue_disable_cb(vq);
457 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
458 if (len == sizeof(*event))
459 virtio_vsock_event_handle(vsock, event);
461 virtio_vsock_event_fill_one(vsock, event);
463 } while (!virtqueue_enable_cb(vq));
465 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
467 mutex_unlock(&vsock->event_lock);
470 static void virtio_vsock_event_done(struct virtqueue *vq)
472 struct virtio_vsock *vsock = vq->vdev->priv;
474 if (!vsock)
475 return;
476 queue_work(virtio_vsock_workqueue, &vsock->event_work);
479 static void virtio_vsock_tx_done(struct virtqueue *vq)
481 struct virtio_vsock *vsock = vq->vdev->priv;
483 if (!vsock)
484 return;
485 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
488 static void virtio_vsock_rx_done(struct virtqueue *vq)
490 struct virtio_vsock *vsock = vq->vdev->priv;
492 if (!vsock)
493 return;
494 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
497 static struct virtio_transport virtio_transport = {
498 .transport = {
499 .get_local_cid = virtio_transport_get_local_cid,
501 .init = virtio_transport_do_socket_init,
502 .destruct = virtio_transport_destruct,
503 .release = virtio_transport_release,
504 .connect = virtio_transport_connect,
505 .shutdown = virtio_transport_shutdown,
506 .cancel_pkt = virtio_transport_cancel_pkt,
508 .dgram_bind = virtio_transport_dgram_bind,
509 .dgram_dequeue = virtio_transport_dgram_dequeue,
510 .dgram_enqueue = virtio_transport_dgram_enqueue,
511 .dgram_allow = virtio_transport_dgram_allow,
513 .stream_dequeue = virtio_transport_stream_dequeue,
514 .stream_enqueue = virtio_transport_stream_enqueue,
515 .stream_has_data = virtio_transport_stream_has_data,
516 .stream_has_space = virtio_transport_stream_has_space,
517 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
518 .stream_is_active = virtio_transport_stream_is_active,
519 .stream_allow = virtio_transport_stream_allow,
521 .notify_poll_in = virtio_transport_notify_poll_in,
522 .notify_poll_out = virtio_transport_notify_poll_out,
523 .notify_recv_init = virtio_transport_notify_recv_init,
524 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
525 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
526 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
527 .notify_send_init = virtio_transport_notify_send_init,
528 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
529 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
530 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
532 .set_buffer_size = virtio_transport_set_buffer_size,
533 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
534 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
535 .get_buffer_size = virtio_transport_get_buffer_size,
536 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
537 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
540 .send_pkt = virtio_transport_send_pkt,
543 static int virtio_vsock_probe(struct virtio_device *vdev)
545 vq_callback_t *callbacks[] = {
546 virtio_vsock_rx_done,
547 virtio_vsock_tx_done,
548 virtio_vsock_event_done,
550 static const char * const names[] = {
551 "rx",
552 "tx",
553 "event",
555 struct virtio_vsock *vsock = NULL;
556 int ret;
558 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
559 if (ret)
560 return ret;
562 /* Only one virtio-vsock device per guest is supported */
563 if (the_virtio_vsock) {
564 ret = -EBUSY;
565 goto out;
568 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
569 if (!vsock) {
570 ret = -ENOMEM;
571 goto out;
574 vsock->vdev = vdev;
576 ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
577 vsock->vqs, callbacks, names,
578 NULL);
579 if (ret < 0)
580 goto out;
582 virtio_vsock_update_guest_cid(vsock);
584 ret = vsock_core_init(&virtio_transport.transport);
585 if (ret < 0)
586 goto out_vqs;
588 vsock->rx_buf_nr = 0;
589 vsock->rx_buf_max_nr = 0;
590 atomic_set(&vsock->queued_replies, 0);
592 vdev->priv = vsock;
593 the_virtio_vsock = vsock;
594 mutex_init(&vsock->tx_lock);
595 mutex_init(&vsock->rx_lock);
596 mutex_init(&vsock->event_lock);
597 spin_lock_init(&vsock->send_pkt_list_lock);
598 INIT_LIST_HEAD(&vsock->send_pkt_list);
599 spin_lock_init(&vsock->loopback_list_lock);
600 INIT_LIST_HEAD(&vsock->loopback_list);
601 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
602 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
603 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
604 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
605 INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
607 mutex_lock(&vsock->rx_lock);
608 virtio_vsock_rx_fill(vsock);
609 mutex_unlock(&vsock->rx_lock);
611 mutex_lock(&vsock->event_lock);
612 virtio_vsock_event_fill(vsock);
613 mutex_unlock(&vsock->event_lock);
615 mutex_unlock(&the_virtio_vsock_mutex);
616 return 0;
618 out_vqs:
619 vsock->vdev->config->del_vqs(vsock->vdev);
620 out:
621 kfree(vsock);
622 mutex_unlock(&the_virtio_vsock_mutex);
623 return ret;
626 static void virtio_vsock_remove(struct virtio_device *vdev)
628 struct virtio_vsock *vsock = vdev->priv;
629 struct virtio_vsock_pkt *pkt;
631 flush_work(&vsock->loopback_work);
632 flush_work(&vsock->rx_work);
633 flush_work(&vsock->tx_work);
634 flush_work(&vsock->event_work);
635 flush_work(&vsock->send_pkt_work);
637 vdev->config->reset(vdev);
639 mutex_lock(&vsock->rx_lock);
640 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
641 virtio_transport_free_pkt(pkt);
642 mutex_unlock(&vsock->rx_lock);
644 mutex_lock(&vsock->tx_lock);
645 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
646 virtio_transport_free_pkt(pkt);
647 mutex_unlock(&vsock->tx_lock);
649 spin_lock_bh(&vsock->send_pkt_list_lock);
650 while (!list_empty(&vsock->send_pkt_list)) {
651 pkt = list_first_entry(&vsock->send_pkt_list,
652 struct virtio_vsock_pkt, list);
653 list_del(&pkt->list);
654 virtio_transport_free_pkt(pkt);
656 spin_unlock_bh(&vsock->send_pkt_list_lock);
658 spin_lock_bh(&vsock->loopback_list_lock);
659 while (!list_empty(&vsock->loopback_list)) {
660 pkt = list_first_entry(&vsock->loopback_list,
661 struct virtio_vsock_pkt, list);
662 list_del(&pkt->list);
663 virtio_transport_free_pkt(pkt);
665 spin_unlock_bh(&vsock->loopback_list_lock);
667 mutex_lock(&the_virtio_vsock_mutex);
668 the_virtio_vsock = NULL;
669 vsock_core_exit();
670 mutex_unlock(&the_virtio_vsock_mutex);
672 vdev->config->del_vqs(vdev);
674 kfree(vsock);
677 static struct virtio_device_id id_table[] = {
678 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
679 { 0 },
682 static unsigned int features[] = {
685 static struct virtio_driver virtio_vsock_driver = {
686 .feature_table = features,
687 .feature_table_size = ARRAY_SIZE(features),
688 .driver.name = KBUILD_MODNAME,
689 .driver.owner = THIS_MODULE,
690 .id_table = id_table,
691 .probe = virtio_vsock_probe,
692 .remove = virtio_vsock_remove,
695 static int __init virtio_vsock_init(void)
697 int ret;
699 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
700 if (!virtio_vsock_workqueue)
701 return -ENOMEM;
702 ret = register_virtio_driver(&virtio_vsock_driver);
703 if (ret)
704 destroy_workqueue(virtio_vsock_workqueue);
705 return ret;
708 static void __exit virtio_vsock_exit(void)
710 unregister_virtio_driver(&virtio_vsock_driver);
711 destroy_workqueue(virtio_vsock_workqueue);
714 module_init(virtio_vsock_init);
715 module_exit(virtio_vsock_exit);
716 MODULE_LICENSE("GPL v2");
717 MODULE_AUTHOR("Asias He");
718 MODULE_DESCRIPTION("virtio transport for vsock");
719 MODULE_DEVICE_TABLE(virtio, id_table);