]>
Commit | Line | Data |
---|---|---|
0ea9e1d3 AH |
1 | /* |
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 | ||
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 */ | |
28 | ||
29 | struct 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 | ||
b9116823 SH |
47 | struct work_struct loopback_work; |
48 | spinlock_t loopback_list_lock; /* protects loopback_list */ | |
49 | struct list_head loopback_list; | |
50 | ||
0ea9e1d3 AH |
51 | atomic_t queued_replies; |
52 | ||
53 | /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX] | |
54 | * must be accessed with rx_lock held. | |
55 | */ | |
56 | struct mutex rx_lock; | |
57 | int rx_buf_nr; | |
58 | int rx_buf_max_nr; | |
59 | ||
60 | /* The following fields are protected by event_lock. | |
61 | * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held. | |
62 | */ | |
63 | struct mutex event_lock; | |
64 | struct virtio_vsock_event event_list[8]; | |
65 | ||
66 | u32 guest_cid; | |
67 | }; | |
68 | ||
69 | static struct virtio_vsock *virtio_vsock_get(void) | |
70 | { | |
71 | return the_virtio_vsock; | |
72 | } | |
73 | ||
74 | static u32 virtio_transport_get_local_cid(void) | |
75 | { | |
76 | struct virtio_vsock *vsock = virtio_vsock_get(); | |
77 | ||
78 | return vsock->guest_cid; | |
79 | } | |
80 | ||
b9116823 SH |
81 | static void virtio_transport_loopback_work(struct work_struct *work) |
82 | { | |
83 | struct virtio_vsock *vsock = | |
84 | container_of(work, struct virtio_vsock, loopback_work); | |
85 | LIST_HEAD(pkts); | |
86 | ||
87 | spin_lock_bh(&vsock->loopback_list_lock); | |
88 | list_splice_init(&vsock->loopback_list, &pkts); | |
89 | spin_unlock_bh(&vsock->loopback_list_lock); | |
90 | ||
91 | mutex_lock(&vsock->rx_lock); | |
92 | while (!list_empty(&pkts)) { | |
93 | struct virtio_vsock_pkt *pkt; | |
94 | ||
95 | pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list); | |
96 | list_del_init(&pkt->list); | |
97 | ||
98 | virtio_transport_recv_pkt(pkt); | |
99 | } | |
100 | mutex_unlock(&vsock->rx_lock); | |
101 | } | |
102 | ||
103 | static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock, | |
104 | struct virtio_vsock_pkt *pkt) | |
105 | { | |
106 | int len = pkt->len; | |
107 | ||
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); | |
111 | ||
112 | queue_work(virtio_vsock_workqueue, &vsock->loopback_work); | |
113 | ||
114 | return len; | |
115 | } | |
116 | ||
0ea9e1d3 AH |
117 | static void |
118 | virtio_transport_send_pkt_work(struct work_struct *work) | |
119 | { | |
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; | |
125 | ||
126 | mutex_lock(&vsock->tx_lock); | |
127 | ||
128 | vq = vsock->vqs[VSOCK_VQ_TX]; | |
129 | ||
0ea9e1d3 AH |
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; | |
135 | ||
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); | |
0ea9e1d3 AH |
139 | break; |
140 | } | |
141 | ||
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); | |
146 | ||
147 | reply = pkt->reply; | |
148 | ||
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; | |
154 | } | |
155 | ||
156 | ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL); | |
21bc54fc GG |
157 | /* Usually this means that there is no more space available in |
158 | * the vq | |
159 | */ | |
0ea9e1d3 AH |
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); | |
0ea9e1d3 AH |
164 | break; |
165 | } | |
166 | ||
167 | if (reply) { | |
168 | struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX]; | |
169 | int val; | |
170 | ||
171 | val = atomic_dec_return(&vsock->queued_replies); | |
172 | ||
173 | /* Do we now have resources to resume rx processing? */ | |
174 | if (val + 1 == virtqueue_get_vring_size(rx_vq)) | |
175 | restart_rx = true; | |
176 | } | |
177 | ||
178 | added = true; | |
179 | } | |
180 | ||
181 | if (added) | |
182 | virtqueue_kick(vq); | |
183 | ||
184 | mutex_unlock(&vsock->tx_lock); | |
185 | ||
186 | if (restart_rx) | |
187 | queue_work(virtio_vsock_workqueue, &vsock->rx_work); | |
188 | } | |
189 | ||
190 | static int | |
191 | virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt) | |
192 | { | |
193 | struct virtio_vsock *vsock; | |
194 | int len = pkt->len; | |
195 | ||
196 | vsock = virtio_vsock_get(); | |
197 | if (!vsock) { | |
198 | virtio_transport_free_pkt(pkt); | |
199 | return -ENODEV; | |
200 | } | |
201 | ||
b9116823 SH |
202 | if (le32_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) |
203 | return virtio_transport_send_pkt_loopback(vsock, pkt); | |
204 | ||
0ea9e1d3 AH |
205 | if (pkt->reply) |
206 | atomic_inc(&vsock->queued_replies); | |
207 | ||
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); | |
211 | ||
212 | queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work); | |
213 | return len; | |
214 | } | |
215 | ||
216 | static void virtio_vsock_rx_fill(struct virtio_vsock *vsock) | |
217 | { | |
218 | int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE; | |
219 | struct virtio_vsock_pkt *pkt; | |
220 | struct scatterlist hdr, buf, *sgs[2]; | |
221 | struct virtqueue *vq; | |
222 | int ret; | |
223 | ||
224 | vq = vsock->vqs[VSOCK_VQ_RX]; | |
225 | ||
226 | do { | |
227 | pkt = kzalloc(sizeof(*pkt), GFP_KERNEL); | |
228 | if (!pkt) | |
229 | break; | |
230 | ||
231 | pkt->buf = kmalloc(buf_len, GFP_KERNEL); | |
232 | if (!pkt->buf) { | |
233 | virtio_transport_free_pkt(pkt); | |
234 | break; | |
235 | } | |
236 | ||
237 | pkt->len = buf_len; | |
238 | ||
239 | sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr)); | |
240 | sgs[0] = &hdr; | |
241 | ||
242 | sg_init_one(&buf, pkt->buf, buf_len); | |
243 | sgs[1] = &buf; | |
244 | ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL); | |
245 | if (ret) { | |
246 | virtio_transport_free_pkt(pkt); | |
247 | break; | |
248 | } | |
249 | vsock->rx_buf_nr++; | |
250 | } while (vq->num_free); | |
251 | if (vsock->rx_buf_nr > vsock->rx_buf_max_nr) | |
252 | vsock->rx_buf_max_nr = vsock->rx_buf_nr; | |
253 | virtqueue_kick(vq); | |
254 | } | |
255 | ||
256 | static void virtio_transport_tx_work(struct work_struct *work) | |
257 | { | |
258 | struct virtio_vsock *vsock = | |
259 | container_of(work, struct virtio_vsock, tx_work); | |
260 | struct virtqueue *vq; | |
261 | bool added = false; | |
262 | ||
263 | vq = vsock->vqs[VSOCK_VQ_TX]; | |
264 | mutex_lock(&vsock->tx_lock); | |
265 | do { | |
266 | struct virtio_vsock_pkt *pkt; | |
267 | unsigned int len; | |
268 | ||
269 | virtqueue_disable_cb(vq); | |
270 | while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) { | |
271 | virtio_transport_free_pkt(pkt); | |
272 | added = true; | |
273 | } | |
274 | } while (!virtqueue_enable_cb(vq)); | |
275 | mutex_unlock(&vsock->tx_lock); | |
276 | ||
277 | if (added) | |
278 | queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work); | |
279 | } | |
280 | ||
281 | /* Is there space left for replies to rx packets? */ | |
282 | static bool virtio_transport_more_replies(struct virtio_vsock *vsock) | |
283 | { | |
284 | struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX]; | |
285 | int val; | |
286 | ||
287 | smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */ | |
288 | val = atomic_read(&vsock->queued_replies); | |
289 | ||
290 | return val < virtqueue_get_vring_size(vq); | |
291 | } | |
292 | ||
293 | static void virtio_transport_rx_work(struct work_struct *work) | |
294 | { | |
295 | struct virtio_vsock *vsock = | |
296 | container_of(work, struct virtio_vsock, rx_work); | |
297 | struct virtqueue *vq; | |
298 | ||
299 | vq = vsock->vqs[VSOCK_VQ_RX]; | |
300 | ||
301 | mutex_lock(&vsock->rx_lock); | |
302 | ||
303 | do { | |
304 | virtqueue_disable_cb(vq); | |
305 | for (;;) { | |
306 | struct virtio_vsock_pkt *pkt; | |
307 | unsigned int len; | |
308 | ||
309 | if (!virtio_transport_more_replies(vsock)) { | |
310 | /* Stop rx until the device processes already | |
311 | * pending replies. Leave rx virtqueue | |
312 | * callbacks disabled. | |
313 | */ | |
314 | goto out; | |
315 | } | |
316 | ||
317 | pkt = virtqueue_get_buf(vq, &len); | |
318 | if (!pkt) { | |
319 | break; | |
320 | } | |
321 | ||
322 | vsock->rx_buf_nr--; | |
323 | ||
324 | /* Drop short/long packets */ | |
325 | if (unlikely(len < sizeof(pkt->hdr) || | |
326 | len > sizeof(pkt->hdr) + pkt->len)) { | |
327 | virtio_transport_free_pkt(pkt); | |
328 | continue; | |
329 | } | |
330 | ||
331 | pkt->len = len - sizeof(pkt->hdr); | |
332 | virtio_transport_recv_pkt(pkt); | |
333 | } | |
334 | } while (!virtqueue_enable_cb(vq)); | |
335 | ||
336 | out: | |
337 | if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2) | |
338 | virtio_vsock_rx_fill(vsock); | |
339 | mutex_unlock(&vsock->rx_lock); | |
340 | } | |
341 | ||
342 | /* event_lock must be held */ | |
343 | static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock, | |
344 | struct virtio_vsock_event *event) | |
345 | { | |
346 | struct scatterlist sg; | |
347 | struct virtqueue *vq; | |
348 | ||
349 | vq = vsock->vqs[VSOCK_VQ_EVENT]; | |
350 | ||
351 | sg_init_one(&sg, event, sizeof(*event)); | |
352 | ||
353 | return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL); | |
354 | } | |
355 | ||
356 | /* event_lock must be held */ | |
357 | static void virtio_vsock_event_fill(struct virtio_vsock *vsock) | |
358 | { | |
359 | size_t i; | |
360 | ||
361 | for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) { | |
362 | struct virtio_vsock_event *event = &vsock->event_list[i]; | |
363 | ||
364 | virtio_vsock_event_fill_one(vsock, event); | |
365 | } | |
366 | ||
367 | virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]); | |
368 | } | |
369 | ||
370 | static void virtio_vsock_reset_sock(struct sock *sk) | |
371 | { | |
372 | lock_sock(sk); | |
373 | sk->sk_state = SS_UNCONNECTED; | |
374 | sk->sk_err = ECONNRESET; | |
375 | sk->sk_error_report(sk); | |
376 | release_sock(sk); | |
377 | } | |
378 | ||
379 | static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock) | |
380 | { | |
381 | struct virtio_device *vdev = vsock->vdev; | |
382 | u64 guest_cid; | |
383 | ||
384 | vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid), | |
385 | &guest_cid, sizeof(guest_cid)); | |
386 | vsock->guest_cid = le64_to_cpu(guest_cid); | |
387 | } | |
388 | ||
389 | /* event_lock must be held */ | |
390 | static void virtio_vsock_event_handle(struct virtio_vsock *vsock, | |
391 | struct virtio_vsock_event *event) | |
392 | { | |
393 | switch (le32_to_cpu(event->id)) { | |
394 | case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET: | |
395 | virtio_vsock_update_guest_cid(vsock); | |
396 | vsock_for_each_connected_socket(virtio_vsock_reset_sock); | |
397 | break; | |
398 | } | |
399 | } | |
400 | ||
401 | static void virtio_transport_event_work(struct work_struct *work) | |
402 | { | |
403 | struct virtio_vsock *vsock = | |
404 | container_of(work, struct virtio_vsock, event_work); | |
405 | struct virtqueue *vq; | |
406 | ||
407 | vq = vsock->vqs[VSOCK_VQ_EVENT]; | |
408 | ||
409 | mutex_lock(&vsock->event_lock); | |
410 | ||
411 | do { | |
412 | struct virtio_vsock_event *event; | |
413 | unsigned int len; | |
414 | ||
415 | virtqueue_disable_cb(vq); | |
416 | while ((event = virtqueue_get_buf(vq, &len)) != NULL) { | |
417 | if (len == sizeof(*event)) | |
418 | virtio_vsock_event_handle(vsock, event); | |
419 | ||
420 | virtio_vsock_event_fill_one(vsock, event); | |
421 | } | |
422 | } while (!virtqueue_enable_cb(vq)); | |
423 | ||
424 | virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]); | |
425 | ||
426 | mutex_unlock(&vsock->event_lock); | |
427 | } | |
428 | ||
429 | static void virtio_vsock_event_done(struct virtqueue *vq) | |
430 | { | |
431 | struct virtio_vsock *vsock = vq->vdev->priv; | |
432 | ||
433 | if (!vsock) | |
434 | return; | |
435 | queue_work(virtio_vsock_workqueue, &vsock->event_work); | |
436 | } | |
437 | ||
438 | static void virtio_vsock_tx_done(struct virtqueue *vq) | |
439 | { | |
440 | struct virtio_vsock *vsock = vq->vdev->priv; | |
441 | ||
442 | if (!vsock) | |
443 | return; | |
444 | queue_work(virtio_vsock_workqueue, &vsock->tx_work); | |
445 | } | |
446 | ||
447 | static void virtio_vsock_rx_done(struct virtqueue *vq) | |
448 | { | |
449 | struct virtio_vsock *vsock = vq->vdev->priv; | |
450 | ||
451 | if (!vsock) | |
452 | return; | |
453 | queue_work(virtio_vsock_workqueue, &vsock->rx_work); | |
454 | } | |
455 | ||
456 | static struct virtio_transport virtio_transport = { | |
457 | .transport = { | |
458 | .get_local_cid = virtio_transport_get_local_cid, | |
459 | ||
460 | .init = virtio_transport_do_socket_init, | |
461 | .destruct = virtio_transport_destruct, | |
462 | .release = virtio_transport_release, | |
463 | .connect = virtio_transport_connect, | |
464 | .shutdown = virtio_transport_shutdown, | |
465 | ||
466 | .dgram_bind = virtio_transport_dgram_bind, | |
467 | .dgram_dequeue = virtio_transport_dgram_dequeue, | |
468 | .dgram_enqueue = virtio_transport_dgram_enqueue, | |
469 | .dgram_allow = virtio_transport_dgram_allow, | |
470 | ||
471 | .stream_dequeue = virtio_transport_stream_dequeue, | |
472 | .stream_enqueue = virtio_transport_stream_enqueue, | |
473 | .stream_has_data = virtio_transport_stream_has_data, | |
474 | .stream_has_space = virtio_transport_stream_has_space, | |
475 | .stream_rcvhiwat = virtio_transport_stream_rcvhiwat, | |
476 | .stream_is_active = virtio_transport_stream_is_active, | |
477 | .stream_allow = virtio_transport_stream_allow, | |
478 | ||
479 | .notify_poll_in = virtio_transport_notify_poll_in, | |
480 | .notify_poll_out = virtio_transport_notify_poll_out, | |
481 | .notify_recv_init = virtio_transport_notify_recv_init, | |
482 | .notify_recv_pre_block = virtio_transport_notify_recv_pre_block, | |
483 | .notify_recv_pre_dequeue = virtio_transport_notify_recv_pre_dequeue, | |
484 | .notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue, | |
485 | .notify_send_init = virtio_transport_notify_send_init, | |
486 | .notify_send_pre_block = virtio_transport_notify_send_pre_block, | |
487 | .notify_send_pre_enqueue = virtio_transport_notify_send_pre_enqueue, | |
488 | .notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue, | |
489 | ||
490 | .set_buffer_size = virtio_transport_set_buffer_size, | |
491 | .set_min_buffer_size = virtio_transport_set_min_buffer_size, | |
492 | .set_max_buffer_size = virtio_transport_set_max_buffer_size, | |
493 | .get_buffer_size = virtio_transport_get_buffer_size, | |
494 | .get_min_buffer_size = virtio_transport_get_min_buffer_size, | |
495 | .get_max_buffer_size = virtio_transport_get_max_buffer_size, | |
496 | }, | |
497 | ||
498 | .send_pkt = virtio_transport_send_pkt, | |
499 | }; | |
500 | ||
501 | static int virtio_vsock_probe(struct virtio_device *vdev) | |
502 | { | |
503 | vq_callback_t *callbacks[] = { | |
504 | virtio_vsock_rx_done, | |
505 | virtio_vsock_tx_done, | |
506 | virtio_vsock_event_done, | |
507 | }; | |
508 | static const char * const names[] = { | |
509 | "rx", | |
510 | "tx", | |
511 | "event", | |
512 | }; | |
513 | struct virtio_vsock *vsock = NULL; | |
514 | int ret; | |
515 | ||
516 | ret = mutex_lock_interruptible(&the_virtio_vsock_mutex); | |
517 | if (ret) | |
518 | return ret; | |
519 | ||
520 | /* Only one virtio-vsock device per guest is supported */ | |
521 | if (the_virtio_vsock) { | |
522 | ret = -EBUSY; | |
523 | goto out; | |
524 | } | |
525 | ||
526 | vsock = kzalloc(sizeof(*vsock), GFP_KERNEL); | |
527 | if (!vsock) { | |
528 | ret = -ENOMEM; | |
529 | goto out; | |
530 | } | |
531 | ||
532 | vsock->vdev = vdev; | |
533 | ||
534 | ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX, | |
535 | vsock->vqs, callbacks, names); | |
536 | if (ret < 0) | |
537 | goto out; | |
538 | ||
539 | virtio_vsock_update_guest_cid(vsock); | |
540 | ||
541 | ret = vsock_core_init(&virtio_transport.transport); | |
542 | if (ret < 0) | |
543 | goto out_vqs; | |
544 | ||
545 | vsock->rx_buf_nr = 0; | |
546 | vsock->rx_buf_max_nr = 0; | |
547 | atomic_set(&vsock->queued_replies, 0); | |
548 | ||
549 | vdev->priv = vsock; | |
550 | the_virtio_vsock = vsock; | |
551 | mutex_init(&vsock->tx_lock); | |
552 | mutex_init(&vsock->rx_lock); | |
553 | mutex_init(&vsock->event_lock); | |
554 | spin_lock_init(&vsock->send_pkt_list_lock); | |
555 | INIT_LIST_HEAD(&vsock->send_pkt_list); | |
b9116823 SH |
556 | spin_lock_init(&vsock->loopback_list_lock); |
557 | INIT_LIST_HEAD(&vsock->loopback_list); | |
0ea9e1d3 AH |
558 | INIT_WORK(&vsock->rx_work, virtio_transport_rx_work); |
559 | INIT_WORK(&vsock->tx_work, virtio_transport_tx_work); | |
560 | INIT_WORK(&vsock->event_work, virtio_transport_event_work); | |
561 | INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work); | |
b9116823 | 562 | INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work); |
0ea9e1d3 AH |
563 | |
564 | mutex_lock(&vsock->rx_lock); | |
565 | virtio_vsock_rx_fill(vsock); | |
566 | mutex_unlock(&vsock->rx_lock); | |
567 | ||
568 | mutex_lock(&vsock->event_lock); | |
569 | virtio_vsock_event_fill(vsock); | |
570 | mutex_unlock(&vsock->event_lock); | |
571 | ||
572 | mutex_unlock(&the_virtio_vsock_mutex); | |
573 | return 0; | |
574 | ||
575 | out_vqs: | |
576 | vsock->vdev->config->del_vqs(vsock->vdev); | |
577 | out: | |
578 | kfree(vsock); | |
579 | mutex_unlock(&the_virtio_vsock_mutex); | |
580 | return ret; | |
581 | } | |
582 | ||
583 | static void virtio_vsock_remove(struct virtio_device *vdev) | |
584 | { | |
585 | struct virtio_vsock *vsock = vdev->priv; | |
586 | struct virtio_vsock_pkt *pkt; | |
587 | ||
b9116823 | 588 | flush_work(&vsock->loopback_work); |
0ea9e1d3 AH |
589 | flush_work(&vsock->rx_work); |
590 | flush_work(&vsock->tx_work); | |
591 | flush_work(&vsock->event_work); | |
592 | flush_work(&vsock->send_pkt_work); | |
593 | ||
594 | vdev->config->reset(vdev); | |
595 | ||
596 | mutex_lock(&vsock->rx_lock); | |
597 | while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX]))) | |
598 | virtio_transport_free_pkt(pkt); | |
599 | mutex_unlock(&vsock->rx_lock); | |
600 | ||
601 | mutex_lock(&vsock->tx_lock); | |
602 | while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX]))) | |
603 | virtio_transport_free_pkt(pkt); | |
604 | mutex_unlock(&vsock->tx_lock); | |
605 | ||
606 | spin_lock_bh(&vsock->send_pkt_list_lock); | |
607 | while (!list_empty(&vsock->send_pkt_list)) { | |
608 | pkt = list_first_entry(&vsock->send_pkt_list, | |
609 | struct virtio_vsock_pkt, list); | |
610 | list_del(&pkt->list); | |
611 | virtio_transport_free_pkt(pkt); | |
612 | } | |
613 | spin_unlock_bh(&vsock->send_pkt_list_lock); | |
614 | ||
b9116823 SH |
615 | spin_lock_bh(&vsock->loopback_list_lock); |
616 | while (!list_empty(&vsock->loopback_list)) { | |
617 | pkt = list_first_entry(&vsock->loopback_list, | |
618 | struct virtio_vsock_pkt, list); | |
619 | list_del(&pkt->list); | |
620 | virtio_transport_free_pkt(pkt); | |
621 | } | |
622 | spin_unlock_bh(&vsock->loopback_list_lock); | |
623 | ||
0ea9e1d3 AH |
624 | mutex_lock(&the_virtio_vsock_mutex); |
625 | the_virtio_vsock = NULL; | |
626 | vsock_core_exit(); | |
627 | mutex_unlock(&the_virtio_vsock_mutex); | |
628 | ||
629 | vdev->config->del_vqs(vdev); | |
630 | ||
631 | kfree(vsock); | |
632 | } | |
633 | ||
634 | static struct virtio_device_id id_table[] = { | |
635 | { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID }, | |
636 | { 0 }, | |
637 | }; | |
638 | ||
639 | static unsigned int features[] = { | |
640 | }; | |
641 | ||
642 | static struct virtio_driver virtio_vsock_driver = { | |
643 | .feature_table = features, | |
644 | .feature_table_size = ARRAY_SIZE(features), | |
645 | .driver.name = KBUILD_MODNAME, | |
646 | .driver.owner = THIS_MODULE, | |
647 | .id_table = id_table, | |
648 | .probe = virtio_vsock_probe, | |
649 | .remove = virtio_vsock_remove, | |
650 | }; | |
651 | ||
652 | static int __init virtio_vsock_init(void) | |
653 | { | |
654 | int ret; | |
655 | ||
656 | virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0); | |
657 | if (!virtio_vsock_workqueue) | |
658 | return -ENOMEM; | |
659 | ret = register_virtio_driver(&virtio_vsock_driver); | |
660 | if (ret) | |
661 | destroy_workqueue(virtio_vsock_workqueue); | |
662 | return ret; | |
663 | } | |
664 | ||
665 | static void __exit virtio_vsock_exit(void) | |
666 | { | |
667 | unregister_virtio_driver(&virtio_vsock_driver); | |
668 | destroy_workqueue(virtio_vsock_workqueue); | |
669 | } | |
670 | ||
671 | module_init(virtio_vsock_init); | |
672 | module_exit(virtio_vsock_exit); | |
673 | MODULE_LICENSE("GPL v2"); | |
674 | MODULE_AUTHOR("Asias He"); | |
675 | MODULE_DESCRIPTION("virtio transport for vsock"); | |
676 | MODULE_DEVICE_TABLE(virtio, id_table); |