Linux 4.9.163
[linux/fpc-iii.git] / net / vmw_vsock / virtio_transport.c
blobf66a6010ae0757d94def0f88fc0884f50b287aa2
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 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.
52 struct mutex rx_lock;
53 int rx_buf_nr;
54 int rx_buf_max_nr;
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];
62 u32 guest_cid;
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 if (!vsock)
75 return VMADDR_CID_ANY;
77 return vsock->guest_cid;
80 static void
81 virtio_transport_send_pkt_work(struct work_struct *work)
83 struct virtio_vsock *vsock =
84 container_of(work, struct virtio_vsock, send_pkt_work);
85 struct virtqueue *vq;
86 bool added = false;
87 bool restart_rx = false;
89 mutex_lock(&vsock->tx_lock);
91 vq = vsock->vqs[VSOCK_VQ_TX];
93 for (;;) {
94 struct virtio_vsock_pkt *pkt;
95 struct scatterlist hdr, buf, *sgs[2];
96 int ret, in_sg = 0, out_sg = 0;
97 bool reply;
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 break;
105 pkt = list_first_entry(&vsock->send_pkt_list,
106 struct virtio_vsock_pkt, list);
107 list_del_init(&pkt->list);
108 spin_unlock_bh(&vsock->send_pkt_list_lock);
110 reply = pkt->reply;
112 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
113 sgs[out_sg++] = &hdr;
114 if (pkt->buf) {
115 sg_init_one(&buf, pkt->buf, pkt->len);
116 sgs[out_sg++] = &buf;
119 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
120 /* Usually this means that there is no more space available in
121 * the vq
123 if (ret < 0) {
124 spin_lock_bh(&vsock->send_pkt_list_lock);
125 list_add(&pkt->list, &vsock->send_pkt_list);
126 spin_unlock_bh(&vsock->send_pkt_list_lock);
127 break;
130 if (reply) {
131 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
132 int val;
134 val = atomic_dec_return(&vsock->queued_replies);
136 /* Do we now have resources to resume rx processing? */
137 if (val + 1 == virtqueue_get_vring_size(rx_vq))
138 restart_rx = true;
141 added = true;
144 if (added)
145 virtqueue_kick(vq);
147 mutex_unlock(&vsock->tx_lock);
149 if (restart_rx)
150 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
153 static int
154 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
156 struct virtio_vsock *vsock;
157 int len = pkt->len;
159 vsock = virtio_vsock_get();
160 if (!vsock) {
161 virtio_transport_free_pkt(pkt);
162 return -ENODEV;
165 if (pkt->reply)
166 atomic_inc(&vsock->queued_replies);
168 spin_lock_bh(&vsock->send_pkt_list_lock);
169 list_add_tail(&pkt->list, &vsock->send_pkt_list);
170 spin_unlock_bh(&vsock->send_pkt_list_lock);
172 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
173 return len;
176 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
178 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
179 struct virtio_vsock_pkt *pkt;
180 struct scatterlist hdr, buf, *sgs[2];
181 struct virtqueue *vq;
182 int ret;
184 vq = vsock->vqs[VSOCK_VQ_RX];
186 do {
187 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
188 if (!pkt)
189 break;
191 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
192 if (!pkt->buf) {
193 virtio_transport_free_pkt(pkt);
194 break;
197 pkt->len = buf_len;
199 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
200 sgs[0] = &hdr;
202 sg_init_one(&buf, pkt->buf, buf_len);
203 sgs[1] = &buf;
204 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
205 if (ret) {
206 virtio_transport_free_pkt(pkt);
207 break;
209 vsock->rx_buf_nr++;
210 } while (vq->num_free);
211 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
212 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
213 virtqueue_kick(vq);
216 static void virtio_transport_tx_work(struct work_struct *work)
218 struct virtio_vsock *vsock =
219 container_of(work, struct virtio_vsock, tx_work);
220 struct virtqueue *vq;
221 bool added = false;
223 vq = vsock->vqs[VSOCK_VQ_TX];
224 mutex_lock(&vsock->tx_lock);
225 do {
226 struct virtio_vsock_pkt *pkt;
227 unsigned int len;
229 virtqueue_disable_cb(vq);
230 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
231 virtio_transport_free_pkt(pkt);
232 added = true;
234 } while (!virtqueue_enable_cb(vq));
235 mutex_unlock(&vsock->tx_lock);
237 if (added)
238 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
241 /* Is there space left for replies to rx packets? */
242 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
244 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
245 int val;
247 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
248 val = atomic_read(&vsock->queued_replies);
250 return val < virtqueue_get_vring_size(vq);
253 static void virtio_transport_rx_work(struct work_struct *work)
255 struct virtio_vsock *vsock =
256 container_of(work, struct virtio_vsock, rx_work);
257 struct virtqueue *vq;
259 vq = vsock->vqs[VSOCK_VQ_RX];
261 mutex_lock(&vsock->rx_lock);
263 do {
264 virtqueue_disable_cb(vq);
265 for (;;) {
266 struct virtio_vsock_pkt *pkt;
267 unsigned int len;
269 if (!virtio_transport_more_replies(vsock)) {
270 /* Stop rx until the device processes already
271 * pending replies. Leave rx virtqueue
272 * callbacks disabled.
274 goto out;
277 pkt = virtqueue_get_buf(vq, &len);
278 if (!pkt) {
279 break;
282 vsock->rx_buf_nr--;
284 /* Drop short/long packets */
285 if (unlikely(len < sizeof(pkt->hdr) ||
286 len > sizeof(pkt->hdr) + pkt->len)) {
287 virtio_transport_free_pkt(pkt);
288 continue;
291 pkt->len = len - sizeof(pkt->hdr);
292 virtio_transport_recv_pkt(pkt);
294 } while (!virtqueue_enable_cb(vq));
296 out:
297 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
298 virtio_vsock_rx_fill(vsock);
299 mutex_unlock(&vsock->rx_lock);
302 /* event_lock must be held */
303 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
304 struct virtio_vsock_event *event)
306 struct scatterlist sg;
307 struct virtqueue *vq;
309 vq = vsock->vqs[VSOCK_VQ_EVENT];
311 sg_init_one(&sg, event, sizeof(*event));
313 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
316 /* event_lock must be held */
317 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
319 size_t i;
321 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
322 struct virtio_vsock_event *event = &vsock->event_list[i];
324 virtio_vsock_event_fill_one(vsock, event);
327 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
330 static void virtio_vsock_reset_sock(struct sock *sk)
332 lock_sock(sk);
333 sk->sk_state = SS_UNCONNECTED;
334 sk->sk_err = ECONNRESET;
335 sk->sk_error_report(sk);
336 release_sock(sk);
339 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
341 struct virtio_device *vdev = vsock->vdev;
342 u64 guest_cid;
344 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
345 &guest_cid, sizeof(guest_cid));
346 vsock->guest_cid = le64_to_cpu(guest_cid);
349 /* event_lock must be held */
350 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
351 struct virtio_vsock_event *event)
353 switch (le32_to_cpu(event->id)) {
354 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
355 virtio_vsock_update_guest_cid(vsock);
356 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
357 break;
361 static void virtio_transport_event_work(struct work_struct *work)
363 struct virtio_vsock *vsock =
364 container_of(work, struct virtio_vsock, event_work);
365 struct virtqueue *vq;
367 vq = vsock->vqs[VSOCK_VQ_EVENT];
369 mutex_lock(&vsock->event_lock);
371 do {
372 struct virtio_vsock_event *event;
373 unsigned int len;
375 virtqueue_disable_cb(vq);
376 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
377 if (len == sizeof(*event))
378 virtio_vsock_event_handle(vsock, event);
380 virtio_vsock_event_fill_one(vsock, event);
382 } while (!virtqueue_enable_cb(vq));
384 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
386 mutex_unlock(&vsock->event_lock);
389 static void virtio_vsock_event_done(struct virtqueue *vq)
391 struct virtio_vsock *vsock = vq->vdev->priv;
393 if (!vsock)
394 return;
395 queue_work(virtio_vsock_workqueue, &vsock->event_work);
398 static void virtio_vsock_tx_done(struct virtqueue *vq)
400 struct virtio_vsock *vsock = vq->vdev->priv;
402 if (!vsock)
403 return;
404 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
407 static void virtio_vsock_rx_done(struct virtqueue *vq)
409 struct virtio_vsock *vsock = vq->vdev->priv;
411 if (!vsock)
412 return;
413 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
416 static struct virtio_transport virtio_transport = {
417 .transport = {
418 .get_local_cid = virtio_transport_get_local_cid,
420 .init = virtio_transport_do_socket_init,
421 .destruct = virtio_transport_destruct,
422 .release = virtio_transport_release,
423 .connect = virtio_transport_connect,
424 .shutdown = virtio_transport_shutdown,
426 .dgram_bind = virtio_transport_dgram_bind,
427 .dgram_dequeue = virtio_transport_dgram_dequeue,
428 .dgram_enqueue = virtio_transport_dgram_enqueue,
429 .dgram_allow = virtio_transport_dgram_allow,
431 .stream_dequeue = virtio_transport_stream_dequeue,
432 .stream_enqueue = virtio_transport_stream_enqueue,
433 .stream_has_data = virtio_transport_stream_has_data,
434 .stream_has_space = virtio_transport_stream_has_space,
435 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
436 .stream_is_active = virtio_transport_stream_is_active,
437 .stream_allow = virtio_transport_stream_allow,
439 .notify_poll_in = virtio_transport_notify_poll_in,
440 .notify_poll_out = virtio_transport_notify_poll_out,
441 .notify_recv_init = virtio_transport_notify_recv_init,
442 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
443 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
444 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
445 .notify_send_init = virtio_transport_notify_send_init,
446 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
447 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
448 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
450 .set_buffer_size = virtio_transport_set_buffer_size,
451 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
452 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
453 .get_buffer_size = virtio_transport_get_buffer_size,
454 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
455 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
458 .send_pkt = virtio_transport_send_pkt,
461 static int virtio_vsock_probe(struct virtio_device *vdev)
463 vq_callback_t *callbacks[] = {
464 virtio_vsock_rx_done,
465 virtio_vsock_tx_done,
466 virtio_vsock_event_done,
468 static const char * const names[] = {
469 "rx",
470 "tx",
471 "event",
473 struct virtio_vsock *vsock = NULL;
474 int ret;
476 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
477 if (ret)
478 return ret;
480 /* Only one virtio-vsock device per guest is supported */
481 if (the_virtio_vsock) {
482 ret = -EBUSY;
483 goto out;
486 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
487 if (!vsock) {
488 ret = -ENOMEM;
489 goto out;
492 vsock->vdev = vdev;
494 ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
495 vsock->vqs, callbacks, names);
496 if (ret < 0)
497 goto out;
499 virtio_vsock_update_guest_cid(vsock);
501 vsock->rx_buf_nr = 0;
502 vsock->rx_buf_max_nr = 0;
503 atomic_set(&vsock->queued_replies, 0);
505 vdev->priv = vsock;
506 the_virtio_vsock = vsock;
507 mutex_init(&vsock->tx_lock);
508 mutex_init(&vsock->rx_lock);
509 mutex_init(&vsock->event_lock);
510 spin_lock_init(&vsock->send_pkt_list_lock);
511 INIT_LIST_HEAD(&vsock->send_pkt_list);
512 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
513 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
514 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
515 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
517 mutex_lock(&vsock->rx_lock);
518 virtio_vsock_rx_fill(vsock);
519 mutex_unlock(&vsock->rx_lock);
521 mutex_lock(&vsock->event_lock);
522 virtio_vsock_event_fill(vsock);
523 mutex_unlock(&vsock->event_lock);
525 mutex_unlock(&the_virtio_vsock_mutex);
526 return 0;
528 out:
529 kfree(vsock);
530 mutex_unlock(&the_virtio_vsock_mutex);
531 return ret;
534 static void virtio_vsock_remove(struct virtio_device *vdev)
536 struct virtio_vsock *vsock = vdev->priv;
537 struct virtio_vsock_pkt *pkt;
539 flush_work(&vsock->rx_work);
540 flush_work(&vsock->tx_work);
541 flush_work(&vsock->event_work);
542 flush_work(&vsock->send_pkt_work);
544 /* Reset all connected sockets when the device disappear */
545 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
547 vdev->config->reset(vdev);
549 mutex_lock(&vsock->rx_lock);
550 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
551 virtio_transport_free_pkt(pkt);
552 mutex_unlock(&vsock->rx_lock);
554 mutex_lock(&vsock->tx_lock);
555 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
556 virtio_transport_free_pkt(pkt);
557 mutex_unlock(&vsock->tx_lock);
559 spin_lock_bh(&vsock->send_pkt_list_lock);
560 while (!list_empty(&vsock->send_pkt_list)) {
561 pkt = list_first_entry(&vsock->send_pkt_list,
562 struct virtio_vsock_pkt, list);
563 list_del(&pkt->list);
564 virtio_transport_free_pkt(pkt);
566 spin_unlock_bh(&vsock->send_pkt_list_lock);
568 mutex_lock(&the_virtio_vsock_mutex);
569 the_virtio_vsock = NULL;
570 mutex_unlock(&the_virtio_vsock_mutex);
572 vdev->config->del_vqs(vdev);
574 kfree(vsock);
577 static struct virtio_device_id id_table[] = {
578 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
579 { 0 },
582 static unsigned int features[] = {
585 static struct virtio_driver virtio_vsock_driver = {
586 .feature_table = features,
587 .feature_table_size = ARRAY_SIZE(features),
588 .driver.name = KBUILD_MODNAME,
589 .driver.owner = THIS_MODULE,
590 .id_table = id_table,
591 .probe = virtio_vsock_probe,
592 .remove = virtio_vsock_remove,
595 static int __init virtio_vsock_init(void)
597 int ret;
599 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
600 if (!virtio_vsock_workqueue)
601 return -ENOMEM;
603 ret = register_virtio_driver(&virtio_vsock_driver);
604 if (ret)
605 goto out_wq;
607 ret = vsock_core_init(&virtio_transport.transport);
608 if (ret)
609 goto out_vdr;
611 return 0;
613 out_vdr:
614 unregister_virtio_driver(&virtio_vsock_driver);
615 out_wq:
616 destroy_workqueue(virtio_vsock_workqueue);
617 return ret;
621 static void __exit virtio_vsock_exit(void)
623 vsock_core_exit();
624 unregister_virtio_driver(&virtio_vsock_driver);
625 destroy_workqueue(virtio_vsock_workqueue);
628 module_init(virtio_vsock_init);
629 module_exit(virtio_vsock_exit);
630 MODULE_LICENSE("GPL v2");
631 MODULE_AUTHOR("Asias He");
632 MODULE_DESCRIPTION("virtio transport for vsock");
633 MODULE_DEVICE_TABLE(virtio, id_table);