blob: 0bd5a60f3bdeba16c7ec1a8fefea6c68552b3b45 [file] [log] [blame]
Asias He0ea9e1d2016-07-28 15:36:33 +01001/*
2 * virtio transport for vsock
3 *
4 * Copyright (C) 2013-2015 Red Hat, Inc.
5 * Author: Asias He <asias@redhat.com>
6 * Stefan Hajnoczi <stefanha@redhat.com>
7 *
8 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
9 * early virtio-vsock proof-of-concept bits.
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2.
12 */
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>
24
25static struct workqueue_struct *virtio_vsock_workqueue;
26static struct virtio_vsock *the_virtio_vsock;
27static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
28
29struct virtio_vsock {
30 struct virtio_device *vdev;
31 struct virtqueue *vqs[VSOCK_VQ_MAX];
32
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;
37
38 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
39 * must be accessed with tx_lock held.
40 */
41 struct mutex tx_lock;
42
43 struct work_struct send_pkt_work;
44 spinlock_t send_pkt_list_lock;
45 struct list_head send_pkt_list;
46
47 atomic_t queued_replies;
48
49 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
50 * must be accessed with rx_lock held.
51 */
52 struct mutex rx_lock;
53 int rx_buf_nr;
54 int rx_buf_max_nr;
55
56 /* The following fields are protected by event_lock.
57 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
58 */
59 struct mutex event_lock;
60 struct virtio_vsock_event event_list[8];
61
62 u32 guest_cid;
63};
64
65static struct virtio_vsock *virtio_vsock_get(void)
66{
67 return the_virtio_vsock;
68}
69
70static u32 virtio_transport_get_local_cid(void)
71{
72 struct virtio_vsock *vsock = virtio_vsock_get();
73
Stefano Garzarella5c9e72b2019-02-01 12:42:06 +010074 if (!vsock)
75 return VMADDR_CID_ANY;
76
Asias He0ea9e1d2016-07-28 15:36:33 +010077 return vsock->guest_cid;
78}
79
80static void
81virtio_transport_send_pkt_work(struct work_struct *work)
82{
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;
88
89 mutex_lock(&vsock->tx_lock);
90
91 vq = vsock->vqs[VSOCK_VQ_TX];
92
Asias He0ea9e1d2016-07-28 15:36:33 +010093 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;
98
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);
Asias He0ea9e1d2016-07-28 15:36:33 +0100102 break;
103 }
104
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);
109
110 reply = pkt->reply;
111
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;
117 }
118
119 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
Gerard Garcia21bc54f2016-08-10 17:24:34 +0200120 /* Usually this means that there is no more space available in
121 * the vq
122 */
Asias He0ea9e1d2016-07-28 15:36:33 +0100123 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);
Asias He0ea9e1d2016-07-28 15:36:33 +0100127 break;
128 }
129
130 if (reply) {
131 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
132 int val;
133
134 val = atomic_dec_return(&vsock->queued_replies);
135
136 /* Do we now have resources to resume rx processing? */
137 if (val + 1 == virtqueue_get_vring_size(rx_vq))
138 restart_rx = true;
139 }
140
141 added = true;
142 }
143
144 if (added)
145 virtqueue_kick(vq);
146
147 mutex_unlock(&vsock->tx_lock);
148
149 if (restart_rx)
150 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
151}
152
153static int
154virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
155{
156 struct virtio_vsock *vsock;
157 int len = pkt->len;
158
159 vsock = virtio_vsock_get();
160 if (!vsock) {
161 virtio_transport_free_pkt(pkt);
162 return -ENODEV;
163 }
164
165 if (pkt->reply)
166 atomic_inc(&vsock->queued_replies);
167
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);
171
172 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
173 return len;
174}
175
176static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
177{
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;
183
184 vq = vsock->vqs[VSOCK_VQ_RX];
185
186 do {
187 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
188 if (!pkt)
189 break;
190
191 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
192 if (!pkt->buf) {
193 virtio_transport_free_pkt(pkt);
194 break;
195 }
196
197 pkt->len = buf_len;
198
199 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
200 sgs[0] = &hdr;
201
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;
208 }
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);
214}
215
216static void virtio_transport_tx_work(struct work_struct *work)
217{
218 struct virtio_vsock *vsock =
219 container_of(work, struct virtio_vsock, tx_work);
220 struct virtqueue *vq;
221 bool added = false;
222
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;
228
229 virtqueue_disable_cb(vq);
230 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
231 virtio_transport_free_pkt(pkt);
232 added = true;
233 }
234 } while (!virtqueue_enable_cb(vq));
235 mutex_unlock(&vsock->tx_lock);
236
237 if (added)
238 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
239}
240
241/* Is there space left for replies to rx packets? */
242static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
243{
244 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
245 int val;
246
247 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
248 val = atomic_read(&vsock->queued_replies);
249
250 return val < virtqueue_get_vring_size(vq);
251}
252
253static void virtio_transport_rx_work(struct work_struct *work)
254{
255 struct virtio_vsock *vsock =
256 container_of(work, struct virtio_vsock, rx_work);
257 struct virtqueue *vq;
258
259 vq = vsock->vqs[VSOCK_VQ_RX];
260
261 mutex_lock(&vsock->rx_lock);
262
263 do {
264 virtqueue_disable_cb(vq);
265 for (;;) {
266 struct virtio_vsock_pkt *pkt;
267 unsigned int len;
268
269 if (!virtio_transport_more_replies(vsock)) {
270 /* Stop rx until the device processes already
271 * pending replies. Leave rx virtqueue
272 * callbacks disabled.
273 */
274 goto out;
275 }
276
277 pkt = virtqueue_get_buf(vq, &len);
278 if (!pkt) {
279 break;
280 }
281
282 vsock->rx_buf_nr--;
283
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;
289 }
290
291 pkt->len = len - sizeof(pkt->hdr);
292 virtio_transport_recv_pkt(pkt);
293 }
294 } while (!virtqueue_enable_cb(vq));
295
296out:
297 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
298 virtio_vsock_rx_fill(vsock);
299 mutex_unlock(&vsock->rx_lock);
300}
301
302/* event_lock must be held */
303static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
304 struct virtio_vsock_event *event)
305{
306 struct scatterlist sg;
307 struct virtqueue *vq;
308
309 vq = vsock->vqs[VSOCK_VQ_EVENT];
310
311 sg_init_one(&sg, event, sizeof(*event));
312
313 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
314}
315
316/* event_lock must be held */
317static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
318{
319 size_t i;
320
321 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
322 struct virtio_vsock_event *event = &vsock->event_list[i];
323
324 virtio_vsock_event_fill_one(vsock, event);
325 }
326
327 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
328}
329
330static void virtio_vsock_reset_sock(struct sock *sk)
331{
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);
337}
338
339static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
340{
341 struct virtio_device *vdev = vsock->vdev;
342 u64 guest_cid;
343
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);
347}
348
349/* event_lock must be held */
350static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
351 struct virtio_vsock_event *event)
352{
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;
358 }
359}
360
361static void virtio_transport_event_work(struct work_struct *work)
362{
363 struct virtio_vsock *vsock =
364 container_of(work, struct virtio_vsock, event_work);
365 struct virtqueue *vq;
366
367 vq = vsock->vqs[VSOCK_VQ_EVENT];
368
369 mutex_lock(&vsock->event_lock);
370
371 do {
372 struct virtio_vsock_event *event;
373 unsigned int len;
374
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);
379
380 virtio_vsock_event_fill_one(vsock, event);
381 }
382 } while (!virtqueue_enable_cb(vq));
383
384 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
385
386 mutex_unlock(&vsock->event_lock);
387}
388
389static void virtio_vsock_event_done(struct virtqueue *vq)
390{
391 struct virtio_vsock *vsock = vq->vdev->priv;
392
393 if (!vsock)
394 return;
395 queue_work(virtio_vsock_workqueue, &vsock->event_work);
396}
397
398static void virtio_vsock_tx_done(struct virtqueue *vq)
399{
400 struct virtio_vsock *vsock = vq->vdev->priv;
401
402 if (!vsock)
403 return;
404 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
405}
406
407static void virtio_vsock_rx_done(struct virtqueue *vq)
408{
409 struct virtio_vsock *vsock = vq->vdev->priv;
410
411 if (!vsock)
412 return;
413 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
414}
415
416static struct virtio_transport virtio_transport = {
417 .transport = {
418 .get_local_cid = virtio_transport_get_local_cid,
419
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,
425
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,
430
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,
438
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,
449
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,
456 },
457
458 .send_pkt = virtio_transport_send_pkt,
459};
460
461static int virtio_vsock_probe(struct virtio_device *vdev)
462{
463 vq_callback_t *callbacks[] = {
464 virtio_vsock_rx_done,
465 virtio_vsock_tx_done,
466 virtio_vsock_event_done,
467 };
468 static const char * const names[] = {
469 "rx",
470 "tx",
471 "event",
472 };
473 struct virtio_vsock *vsock = NULL;
474 int ret;
475
476 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
477 if (ret)
478 return ret;
479
480 /* Only one virtio-vsock device per guest is supported */
481 if (the_virtio_vsock) {
482 ret = -EBUSY;
483 goto out;
484 }
485
486 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
487 if (!vsock) {
488 ret = -ENOMEM;
489 goto out;
490 }
491
492 vsock->vdev = vdev;
493
494 ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
495 vsock->vqs, callbacks, names);
496 if (ret < 0)
497 goto out;
498
499 virtio_vsock_update_guest_cid(vsock);
500
Asias He0ea9e1d2016-07-28 15:36:33 +0100501 vsock->rx_buf_nr = 0;
502 vsock->rx_buf_max_nr = 0;
503 atomic_set(&vsock->queued_replies, 0);
504
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);
516
517 mutex_lock(&vsock->rx_lock);
518 virtio_vsock_rx_fill(vsock);
519 mutex_unlock(&vsock->rx_lock);
520
521 mutex_lock(&vsock->event_lock);
522 virtio_vsock_event_fill(vsock);
523 mutex_unlock(&vsock->event_lock);
524
525 mutex_unlock(&the_virtio_vsock_mutex);
526 return 0;
527
Asias He0ea9e1d2016-07-28 15:36:33 +0100528out:
529 kfree(vsock);
530 mutex_unlock(&the_virtio_vsock_mutex);
531 return ret;
532}
533
534static void virtio_vsock_remove(struct virtio_device *vdev)
535{
536 struct virtio_vsock *vsock = vdev->priv;
537 struct virtio_vsock_pkt *pkt;
538
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);
543
Stefano Garzarella4c870d32019-02-01 12:42:07 +0100544 /* Reset all connected sockets when the device disappear */
545 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
546
Asias He0ea9e1d2016-07-28 15:36:33 +0100547 vdev->config->reset(vdev);
548
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);
553
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);
558
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);
565 }
566 spin_unlock_bh(&vsock->send_pkt_list_lock);
567
568 mutex_lock(&the_virtio_vsock_mutex);
569 the_virtio_vsock = NULL;
Asias He0ea9e1d2016-07-28 15:36:33 +0100570 mutex_unlock(&the_virtio_vsock_mutex);
571
572 vdev->config->del_vqs(vdev);
573
574 kfree(vsock);
575}
576
577static struct virtio_device_id id_table[] = {
578 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
579 { 0 },
580};
581
582static unsigned int features[] = {
583};
584
585static 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,
593};
594
595static int __init virtio_vsock_init(void)
596{
597 int ret;
598
599 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
600 if (!virtio_vsock_workqueue)
601 return -ENOMEM;
Stefano Garzarella5c9e72b2019-02-01 12:42:06 +0100602
Jorge E. Moreira4cc4c592019-05-16 13:51:07 -0700603 ret = vsock_core_init(&virtio_transport.transport);
Asias He0ea9e1d2016-07-28 15:36:33 +0100604 if (ret)
Stefano Garzarella5c9e72b2019-02-01 12:42:06 +0100605 goto out_wq;
606
Jorge E. Moreira4cc4c592019-05-16 13:51:07 -0700607 ret = register_virtio_driver(&virtio_vsock_driver);
Stefano Garzarella5c9e72b2019-02-01 12:42:06 +0100608 if (ret)
Jorge E. Moreira4cc4c592019-05-16 13:51:07 -0700609 goto out_vci;
Stefano Garzarella5c9e72b2019-02-01 12:42:06 +0100610
611 return 0;
612
Jorge E. Moreira4cc4c592019-05-16 13:51:07 -0700613out_vci:
614 vsock_core_exit();
Stefano Garzarella5c9e72b2019-02-01 12:42:06 +0100615out_wq:
616 destroy_workqueue(virtio_vsock_workqueue);
Asias He0ea9e1d2016-07-28 15:36:33 +0100617 return ret;
618}
619
620static void __exit virtio_vsock_exit(void)
621{
Asias He0ea9e1d2016-07-28 15:36:33 +0100622 unregister_virtio_driver(&virtio_vsock_driver);
Jorge E. Moreira4cc4c592019-05-16 13:51:07 -0700623 vsock_core_exit();
Asias He0ea9e1d2016-07-28 15:36:33 +0100624 destroy_workqueue(virtio_vsock_workqueue);
625}
626
627module_init(virtio_vsock_init);
628module_exit(virtio_vsock_exit);
629MODULE_LICENSE("GPL v2");
630MODULE_AUTHOR("Asias He");
631MODULE_DESCRIPTION("virtio transport for vsock");
632MODULE_DEVICE_TABLE(virtio, id_table);