blob: 9c287e3e393c835a6694507b43f6915dd4a2e200 [file] [log] [blame]
Thomas Gleixner7a338472019-06-04 10:11:15 +02001// SPDX-License-Identifier: GPL-2.0-only
Asias He0ea9e1d2016-07-28 15:36:33 +01002/*
3 * virtio transport for vsock
4 *
5 * Copyright (C) 2013-2015 Red Hat, Inc.
6 * Author: Asias He <asias@redhat.com>
7 * Stefan Hajnoczi <stefanha@redhat.com>
8 *
9 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
10 * early virtio-vsock proof-of-concept bits.
Asias He0ea9e1d2016-07-28 15:36:33 +010011 */
12#include <linux/spinlock.h>
13#include <linux/module.h>
14#include <linux/list.h>
15#include <linux/atomic.h>
16#include <linux/virtio.h>
17#include <linux/virtio_ids.h>
18#include <linux/virtio_config.h>
19#include <linux/virtio_vsock.h>
20#include <net/sock.h>
21#include <linux/mutex.h>
22#include <net/af_vsock.h>
23
24static struct workqueue_struct *virtio_vsock_workqueue;
25static struct virtio_vsock *the_virtio_vsock;
26static DEFINE_MUTEX(the_virtio_vsock_mutex); /* protects the_virtio_vsock */
27
28struct virtio_vsock {
29 struct virtio_device *vdev;
30 struct virtqueue *vqs[VSOCK_VQ_MAX];
31
32 /* Virtqueue processing is deferred to a workqueue */
33 struct work_struct tx_work;
34 struct work_struct rx_work;
35 struct work_struct event_work;
36
37 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
38 * must be accessed with tx_lock held.
39 */
40 struct mutex tx_lock;
41
42 struct work_struct send_pkt_work;
43 spinlock_t send_pkt_list_lock;
44 struct list_head send_pkt_list;
45
Stefan Hajnoczib9116822016-11-21 13:56:31 +000046 struct work_struct loopback_work;
47 spinlock_t loopback_list_lock; /* protects loopback_list */
48 struct list_head loopback_list;
49
Asias He0ea9e1d2016-07-28 15:36:33 +010050 atomic_t queued_replies;
51
52 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
53 * must be accessed with rx_lock held.
54 */
55 struct mutex rx_lock;
56 int rx_buf_nr;
57 int rx_buf_max_nr;
58
59 /* The following fields are protected by event_lock.
60 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
61 */
62 struct mutex event_lock;
63 struct virtio_vsock_event event_list[8];
64
65 u32 guest_cid;
66};
67
68static struct virtio_vsock *virtio_vsock_get(void)
69{
70 return the_virtio_vsock;
71}
72
73static u32 virtio_transport_get_local_cid(void)
74{
75 struct virtio_vsock *vsock = virtio_vsock_get();
76
Stefano Garzarella22b5c0b2019-02-01 12:42:06 +010077 if (!vsock)
78 return VMADDR_CID_ANY;
79
Asias He0ea9e1d2016-07-28 15:36:33 +010080 return vsock->guest_cid;
81}
82
Stefan Hajnoczib9116822016-11-21 13:56:31 +000083static void virtio_transport_loopback_work(struct work_struct *work)
84{
85 struct virtio_vsock *vsock =
86 container_of(work, struct virtio_vsock, loopback_work);
87 LIST_HEAD(pkts);
88
89 spin_lock_bh(&vsock->loopback_list_lock);
90 list_splice_init(&vsock->loopback_list, &pkts);
91 spin_unlock_bh(&vsock->loopback_list_lock);
92
93 mutex_lock(&vsock->rx_lock);
94 while (!list_empty(&pkts)) {
95 struct virtio_vsock_pkt *pkt;
96
97 pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
98 list_del_init(&pkt->list);
99
100 virtio_transport_recv_pkt(pkt);
101 }
102 mutex_unlock(&vsock->rx_lock);
103}
104
105static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock,
106 struct virtio_vsock_pkt *pkt)
107{
108 int len = pkt->len;
109
110 spin_lock_bh(&vsock->loopback_list_lock);
111 list_add_tail(&pkt->list, &vsock->loopback_list);
112 spin_unlock_bh(&vsock->loopback_list_lock);
113
114 queue_work(virtio_vsock_workqueue, &vsock->loopback_work);
115
116 return len;
117}
118
Asias He0ea9e1d2016-07-28 15:36:33 +0100119static void
120virtio_transport_send_pkt_work(struct work_struct *work)
121{
122 struct virtio_vsock *vsock =
123 container_of(work, struct virtio_vsock, send_pkt_work);
124 struct virtqueue *vq;
125 bool added = false;
126 bool restart_rx = false;
127
128 mutex_lock(&vsock->tx_lock);
129
130 vq = vsock->vqs[VSOCK_VQ_TX];
131
Asias He0ea9e1d2016-07-28 15:36:33 +0100132 for (;;) {
133 struct virtio_vsock_pkt *pkt;
134 struct scatterlist hdr, buf, *sgs[2];
135 int ret, in_sg = 0, out_sg = 0;
136 bool reply;
137
138 spin_lock_bh(&vsock->send_pkt_list_lock);
139 if (list_empty(&vsock->send_pkt_list)) {
140 spin_unlock_bh(&vsock->send_pkt_list_lock);
Asias He0ea9e1d2016-07-28 15:36:33 +0100141 break;
142 }
143
144 pkt = list_first_entry(&vsock->send_pkt_list,
145 struct virtio_vsock_pkt, list);
146 list_del_init(&pkt->list);
147 spin_unlock_bh(&vsock->send_pkt_list_lock);
148
Gerard Garcia82dfb5402017-04-21 10:10:46 +0100149 virtio_transport_deliver_tap_pkt(pkt);
150
Asias He0ea9e1d2016-07-28 15:36:33 +0100151 reply = pkt->reply;
152
153 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
154 sgs[out_sg++] = &hdr;
155 if (pkt->buf) {
156 sg_init_one(&buf, pkt->buf, pkt->len);
157 sgs[out_sg++] = &buf;
158 }
159
160 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
Gerard Garcia21bc54f2016-08-10 17:24:34 +0200161 /* Usually this means that there is no more space available in
162 * the vq
163 */
Asias He0ea9e1d2016-07-28 15:36:33 +0100164 if (ret < 0) {
165 spin_lock_bh(&vsock->send_pkt_list_lock);
166 list_add(&pkt->list, &vsock->send_pkt_list);
167 spin_unlock_bh(&vsock->send_pkt_list_lock);
Asias He0ea9e1d2016-07-28 15:36:33 +0100168 break;
169 }
170
171 if (reply) {
172 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
173 int val;
174
175 val = atomic_dec_return(&vsock->queued_replies);
176
177 /* Do we now have resources to resume rx processing? */
178 if (val + 1 == virtqueue_get_vring_size(rx_vq))
179 restart_rx = true;
180 }
181
182 added = true;
183 }
184
185 if (added)
186 virtqueue_kick(vq);
187
188 mutex_unlock(&vsock->tx_lock);
189
190 if (restart_rx)
191 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
192}
193
194static int
195virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
196{
197 struct virtio_vsock *vsock;
198 int len = pkt->len;
199
200 vsock = virtio_vsock_get();
201 if (!vsock) {
202 virtio_transport_free_pkt(pkt);
203 return -ENODEV;
204 }
205
Claudio Imbrendae5ab5642018-06-20 15:51:51 +0200206 if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid)
Stefan Hajnoczib9116822016-11-21 13:56:31 +0000207 return virtio_transport_send_pkt_loopback(vsock, pkt);
208
Asias He0ea9e1d2016-07-28 15:36:33 +0100209 if (pkt->reply)
210 atomic_inc(&vsock->queued_replies);
211
212 spin_lock_bh(&vsock->send_pkt_list_lock);
213 list_add_tail(&pkt->list, &vsock->send_pkt_list);
214 spin_unlock_bh(&vsock->send_pkt_list_lock);
215
216 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
217 return len;
218}
219
Peng Tao073b4f22017-03-15 09:32:16 +0800220static int
221virtio_transport_cancel_pkt(struct vsock_sock *vsk)
222{
223 struct virtio_vsock *vsock;
224 struct virtio_vsock_pkt *pkt, *n;
225 int cnt = 0;
226 LIST_HEAD(freeme);
227
228 vsock = virtio_vsock_get();
229 if (!vsock) {
230 return -ENODEV;
231 }
232
233 spin_lock_bh(&vsock->send_pkt_list_lock);
234 list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
235 if (pkt->vsk != vsk)
236 continue;
237 list_move(&pkt->list, &freeme);
238 }
239 spin_unlock_bh(&vsock->send_pkt_list_lock);
240
241 list_for_each_entry_safe(pkt, n, &freeme, list) {
242 if (pkt->reply)
243 cnt++;
244 list_del(&pkt->list);
245 virtio_transport_free_pkt(pkt);
246 }
247
248 if (cnt) {
249 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
250 int new_cnt;
251
252 new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
253 if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
254 new_cnt < virtqueue_get_vring_size(rx_vq))
255 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
256 }
257
258 return 0;
259}
260
Asias He0ea9e1d2016-07-28 15:36:33 +0100261static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
262{
263 int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
264 struct virtio_vsock_pkt *pkt;
265 struct scatterlist hdr, buf, *sgs[2];
266 struct virtqueue *vq;
267 int ret;
268
269 vq = vsock->vqs[VSOCK_VQ_RX];
270
271 do {
272 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
273 if (!pkt)
274 break;
275
276 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
277 if (!pkt->buf) {
278 virtio_transport_free_pkt(pkt);
279 break;
280 }
281
282 pkt->len = buf_len;
283
284 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
285 sgs[0] = &hdr;
286
287 sg_init_one(&buf, pkt->buf, buf_len);
288 sgs[1] = &buf;
289 ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
290 if (ret) {
291 virtio_transport_free_pkt(pkt);
292 break;
293 }
294 vsock->rx_buf_nr++;
295 } while (vq->num_free);
296 if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
297 vsock->rx_buf_max_nr = vsock->rx_buf_nr;
298 virtqueue_kick(vq);
299}
300
301static void virtio_transport_tx_work(struct work_struct *work)
302{
303 struct virtio_vsock *vsock =
304 container_of(work, struct virtio_vsock, tx_work);
305 struct virtqueue *vq;
306 bool added = false;
307
308 vq = vsock->vqs[VSOCK_VQ_TX];
309 mutex_lock(&vsock->tx_lock);
310 do {
311 struct virtio_vsock_pkt *pkt;
312 unsigned int len;
313
314 virtqueue_disable_cb(vq);
315 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
316 virtio_transport_free_pkt(pkt);
317 added = true;
318 }
319 } while (!virtqueue_enable_cb(vq));
320 mutex_unlock(&vsock->tx_lock);
321
322 if (added)
323 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
324}
325
326/* Is there space left for replies to rx packets? */
327static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
328{
329 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
330 int val;
331
332 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
333 val = atomic_read(&vsock->queued_replies);
334
335 return val < virtqueue_get_vring_size(vq);
336}
337
338static void virtio_transport_rx_work(struct work_struct *work)
339{
340 struct virtio_vsock *vsock =
341 container_of(work, struct virtio_vsock, rx_work);
342 struct virtqueue *vq;
343
344 vq = vsock->vqs[VSOCK_VQ_RX];
345
346 mutex_lock(&vsock->rx_lock);
347
348 do {
349 virtqueue_disable_cb(vq);
350 for (;;) {
351 struct virtio_vsock_pkt *pkt;
352 unsigned int len;
353
354 if (!virtio_transport_more_replies(vsock)) {
355 /* Stop rx until the device processes already
356 * pending replies. Leave rx virtqueue
357 * callbacks disabled.
358 */
359 goto out;
360 }
361
362 pkt = virtqueue_get_buf(vq, &len);
363 if (!pkt) {
364 break;
365 }
366
367 vsock->rx_buf_nr--;
368
369 /* Drop short/long packets */
370 if (unlikely(len < sizeof(pkt->hdr) ||
371 len > sizeof(pkt->hdr) + pkt->len)) {
372 virtio_transport_free_pkt(pkt);
373 continue;
374 }
375
376 pkt->len = len - sizeof(pkt->hdr);
Gerard Garcia82dfb5402017-04-21 10:10:46 +0100377 virtio_transport_deliver_tap_pkt(pkt);
Asias He0ea9e1d2016-07-28 15:36:33 +0100378 virtio_transport_recv_pkt(pkt);
379 }
380 } while (!virtqueue_enable_cb(vq));
381
382out:
383 if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
384 virtio_vsock_rx_fill(vsock);
385 mutex_unlock(&vsock->rx_lock);
386}
387
388/* event_lock must be held */
389static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
390 struct virtio_vsock_event *event)
391{
392 struct scatterlist sg;
393 struct virtqueue *vq;
394
395 vq = vsock->vqs[VSOCK_VQ_EVENT];
396
397 sg_init_one(&sg, event, sizeof(*event));
398
399 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
400}
401
402/* event_lock must be held */
403static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
404{
405 size_t i;
406
407 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
408 struct virtio_vsock_event *event = &vsock->event_list[i];
409
410 virtio_vsock_event_fill_one(vsock, event);
411 }
412
413 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
414}
415
416static void virtio_vsock_reset_sock(struct sock *sk)
417{
418 lock_sock(sk);
Stefan Hajnoczi3b4477d2017-10-05 16:46:52 -0400419 sk->sk_state = TCP_CLOSE;
Asias He0ea9e1d2016-07-28 15:36:33 +0100420 sk->sk_err = ECONNRESET;
421 sk->sk_error_report(sk);
422 release_sock(sk);
423}
424
425static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
426{
427 struct virtio_device *vdev = vsock->vdev;
Michael S. Tsirkin6c7efaf2016-12-06 06:03:34 +0200428 __le64 guest_cid;
Asias He0ea9e1d2016-07-28 15:36:33 +0100429
430 vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
431 &guest_cid, sizeof(guest_cid));
432 vsock->guest_cid = le64_to_cpu(guest_cid);
433}
434
435/* event_lock must be held */
436static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
437 struct virtio_vsock_event *event)
438{
439 switch (le32_to_cpu(event->id)) {
440 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
441 virtio_vsock_update_guest_cid(vsock);
442 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
443 break;
444 }
445}
446
447static void virtio_transport_event_work(struct work_struct *work)
448{
449 struct virtio_vsock *vsock =
450 container_of(work, struct virtio_vsock, event_work);
451 struct virtqueue *vq;
452
453 vq = vsock->vqs[VSOCK_VQ_EVENT];
454
455 mutex_lock(&vsock->event_lock);
456
457 do {
458 struct virtio_vsock_event *event;
459 unsigned int len;
460
461 virtqueue_disable_cb(vq);
462 while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
463 if (len == sizeof(*event))
464 virtio_vsock_event_handle(vsock, event);
465
466 virtio_vsock_event_fill_one(vsock, event);
467 }
468 } while (!virtqueue_enable_cb(vq));
469
470 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
471
472 mutex_unlock(&vsock->event_lock);
473}
474
475static void virtio_vsock_event_done(struct virtqueue *vq)
476{
477 struct virtio_vsock *vsock = vq->vdev->priv;
478
479 if (!vsock)
480 return;
481 queue_work(virtio_vsock_workqueue, &vsock->event_work);
482}
483
484static void virtio_vsock_tx_done(struct virtqueue *vq)
485{
486 struct virtio_vsock *vsock = vq->vdev->priv;
487
488 if (!vsock)
489 return;
490 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
491}
492
493static void virtio_vsock_rx_done(struct virtqueue *vq)
494{
495 struct virtio_vsock *vsock = vq->vdev->priv;
496
497 if (!vsock)
498 return;
499 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
500}
501
502static struct virtio_transport virtio_transport = {
503 .transport = {
504 .get_local_cid = virtio_transport_get_local_cid,
505
506 .init = virtio_transport_do_socket_init,
507 .destruct = virtio_transport_destruct,
508 .release = virtio_transport_release,
509 .connect = virtio_transport_connect,
510 .shutdown = virtio_transport_shutdown,
Peng Tao073b4f22017-03-15 09:32:16 +0800511 .cancel_pkt = virtio_transport_cancel_pkt,
Asias He0ea9e1d2016-07-28 15:36:33 +0100512
513 .dgram_bind = virtio_transport_dgram_bind,
514 .dgram_dequeue = virtio_transport_dgram_dequeue,
515 .dgram_enqueue = virtio_transport_dgram_enqueue,
516 .dgram_allow = virtio_transport_dgram_allow,
517
518 .stream_dequeue = virtio_transport_stream_dequeue,
519 .stream_enqueue = virtio_transport_stream_enqueue,
520 .stream_has_data = virtio_transport_stream_has_data,
521 .stream_has_space = virtio_transport_stream_has_space,
522 .stream_rcvhiwat = virtio_transport_stream_rcvhiwat,
523 .stream_is_active = virtio_transport_stream_is_active,
524 .stream_allow = virtio_transport_stream_allow,
525
526 .notify_poll_in = virtio_transport_notify_poll_in,
527 .notify_poll_out = virtio_transport_notify_poll_out,
528 .notify_recv_init = virtio_transport_notify_recv_init,
529 .notify_recv_pre_block = virtio_transport_notify_recv_pre_block,
530 .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue,
531 .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
532 .notify_send_init = virtio_transport_notify_send_init,
533 .notify_send_pre_block = virtio_transport_notify_send_pre_block,
534 .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue,
535 .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
536
537 .set_buffer_size = virtio_transport_set_buffer_size,
538 .set_min_buffer_size = virtio_transport_set_min_buffer_size,
539 .set_max_buffer_size = virtio_transport_set_max_buffer_size,
540 .get_buffer_size = virtio_transport_get_buffer_size,
541 .get_min_buffer_size = virtio_transport_get_min_buffer_size,
542 .get_max_buffer_size = virtio_transport_get_max_buffer_size,
543 },
544
545 .send_pkt = virtio_transport_send_pkt,
546};
547
548static int virtio_vsock_probe(struct virtio_device *vdev)
549{
550 vq_callback_t *callbacks[] = {
551 virtio_vsock_rx_done,
552 virtio_vsock_tx_done,
553 virtio_vsock_event_done,
554 };
555 static const char * const names[] = {
556 "rx",
557 "tx",
558 "event",
559 };
560 struct virtio_vsock *vsock = NULL;
561 int ret;
562
563 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
564 if (ret)
565 return ret;
566
567 /* Only one virtio-vsock device per guest is supported */
568 if (the_virtio_vsock) {
569 ret = -EBUSY;
570 goto out;
571 }
572
573 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
574 if (!vsock) {
575 ret = -ENOMEM;
576 goto out;
577 }
578
579 vsock->vdev = vdev;
580
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200581 ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
582 vsock->vqs, callbacks, names,
583 NULL);
Asias He0ea9e1d2016-07-28 15:36:33 +0100584 if (ret < 0)
585 goto out;
586
587 virtio_vsock_update_guest_cid(vsock);
588
Asias He0ea9e1d2016-07-28 15:36:33 +0100589 vsock->rx_buf_nr = 0;
590 vsock->rx_buf_max_nr = 0;
591 atomic_set(&vsock->queued_replies, 0);
592
593 vdev->priv = vsock;
594 the_virtio_vsock = vsock;
595 mutex_init(&vsock->tx_lock);
596 mutex_init(&vsock->rx_lock);
597 mutex_init(&vsock->event_lock);
598 spin_lock_init(&vsock->send_pkt_list_lock);
599 INIT_LIST_HEAD(&vsock->send_pkt_list);
Stefan Hajnoczib9116822016-11-21 13:56:31 +0000600 spin_lock_init(&vsock->loopback_list_lock);
601 INIT_LIST_HEAD(&vsock->loopback_list);
Asias He0ea9e1d2016-07-28 15:36:33 +0100602 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
603 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
604 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
605 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
Stefan Hajnoczib9116822016-11-21 13:56:31 +0000606 INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
Asias He0ea9e1d2016-07-28 15:36:33 +0100607
608 mutex_lock(&vsock->rx_lock);
609 virtio_vsock_rx_fill(vsock);
610 mutex_unlock(&vsock->rx_lock);
611
612 mutex_lock(&vsock->event_lock);
613 virtio_vsock_event_fill(vsock);
614 mutex_unlock(&vsock->event_lock);
615
616 mutex_unlock(&the_virtio_vsock_mutex);
617 return 0;
618
Asias He0ea9e1d2016-07-28 15:36:33 +0100619out:
620 kfree(vsock);
621 mutex_unlock(&the_virtio_vsock_mutex);
622 return ret;
623}
624
625static void virtio_vsock_remove(struct virtio_device *vdev)
626{
627 struct virtio_vsock *vsock = vdev->priv;
628 struct virtio_vsock_pkt *pkt;
629
Stefan Hajnoczib9116822016-11-21 13:56:31 +0000630 flush_work(&vsock->loopback_work);
Asias He0ea9e1d2016-07-28 15:36:33 +0100631 flush_work(&vsock->rx_work);
632 flush_work(&vsock->tx_work);
633 flush_work(&vsock->event_work);
634 flush_work(&vsock->send_pkt_work);
635
Stefano Garzarella85965482019-02-01 12:42:07 +0100636 /* Reset all connected sockets when the device disappear */
637 vsock_for_each_connected_socket(virtio_vsock_reset_sock);
638
Asias He0ea9e1d2016-07-28 15:36:33 +0100639 vdev->config->reset(vdev);
640
641 mutex_lock(&vsock->rx_lock);
642 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
643 virtio_transport_free_pkt(pkt);
644 mutex_unlock(&vsock->rx_lock);
645
646 mutex_lock(&vsock->tx_lock);
647 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
648 virtio_transport_free_pkt(pkt);
649 mutex_unlock(&vsock->tx_lock);
650
651 spin_lock_bh(&vsock->send_pkt_list_lock);
652 while (!list_empty(&vsock->send_pkt_list)) {
653 pkt = list_first_entry(&vsock->send_pkt_list,
654 struct virtio_vsock_pkt, list);
655 list_del(&pkt->list);
656 virtio_transport_free_pkt(pkt);
657 }
658 spin_unlock_bh(&vsock->send_pkt_list_lock);
659
Stefan Hajnoczib9116822016-11-21 13:56:31 +0000660 spin_lock_bh(&vsock->loopback_list_lock);
661 while (!list_empty(&vsock->loopback_list)) {
662 pkt = list_first_entry(&vsock->loopback_list,
663 struct virtio_vsock_pkt, list);
664 list_del(&pkt->list);
665 virtio_transport_free_pkt(pkt);
666 }
667 spin_unlock_bh(&vsock->loopback_list_lock);
668
Asias He0ea9e1d2016-07-28 15:36:33 +0100669 mutex_lock(&the_virtio_vsock_mutex);
670 the_virtio_vsock = NULL;
Asias He0ea9e1d2016-07-28 15:36:33 +0100671 mutex_unlock(&the_virtio_vsock_mutex);
672
673 vdev->config->del_vqs(vdev);
674
675 kfree(vsock);
676}
677
678static struct virtio_device_id id_table[] = {
679 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
680 { 0 },
681};
682
683static unsigned int features[] = {
684};
685
686static struct virtio_driver virtio_vsock_driver = {
687 .feature_table = features,
688 .feature_table_size = ARRAY_SIZE(features),
689 .driver.name = KBUILD_MODNAME,
690 .driver.owner = THIS_MODULE,
691 .id_table = id_table,
692 .probe = virtio_vsock_probe,
693 .remove = virtio_vsock_remove,
694};
695
696static int __init virtio_vsock_init(void)
697{
698 int ret;
699
700 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
701 if (!virtio_vsock_workqueue)
702 return -ENOMEM;
Stefano Garzarella22b5c0b2019-02-01 12:42:06 +0100703
Jorge E. Moreiraba95e5df2019-05-16 13:51:07 -0700704 ret = vsock_core_init(&virtio_transport.transport);
Asias He0ea9e1d2016-07-28 15:36:33 +0100705 if (ret)
Stefano Garzarella22b5c0b2019-02-01 12:42:06 +0100706 goto out_wq;
707
Jorge E. Moreiraba95e5df2019-05-16 13:51:07 -0700708 ret = register_virtio_driver(&virtio_vsock_driver);
Stefano Garzarella22b5c0b2019-02-01 12:42:06 +0100709 if (ret)
Jorge E. Moreiraba95e5df2019-05-16 13:51:07 -0700710 goto out_vci;
Stefano Garzarella22b5c0b2019-02-01 12:42:06 +0100711
712 return 0;
713
Jorge E. Moreiraba95e5df2019-05-16 13:51:07 -0700714out_vci:
715 vsock_core_exit();
Stefano Garzarella22b5c0b2019-02-01 12:42:06 +0100716out_wq:
717 destroy_workqueue(virtio_vsock_workqueue);
Asias He0ea9e1d2016-07-28 15:36:33 +0100718 return ret;
719}
720
721static void __exit virtio_vsock_exit(void)
722{
Asias He0ea9e1d2016-07-28 15:36:33 +0100723 unregister_virtio_driver(&virtio_vsock_driver);
Jorge E. Moreiraba95e5df2019-05-16 13:51:07 -0700724 vsock_core_exit();
Asias He0ea9e1d2016-07-28 15:36:33 +0100725 destroy_workqueue(virtio_vsock_workqueue);
726}
727
728module_init(virtio_vsock_init);
729module_exit(virtio_vsock_exit);
730MODULE_LICENSE("GPL v2");
731MODULE_AUTHOR("Asias He");
732MODULE_DESCRIPTION("virtio transport for vsock");
733MODULE_DEVICE_TABLE(virtio, id_table);