• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	bool tx_run;
43 
44 	struct work_struct send_pkt_work;
45 	spinlock_t send_pkt_list_lock;
46 	struct list_head send_pkt_list;
47 
48 	struct work_struct loopback_work;
49 	spinlock_t loopback_list_lock; /* protects loopback_list */
50 	struct list_head loopback_list;
51 
52 	atomic_t queued_replies;
53 
54 	/* The following fields are protected by rx_lock.  vqs[VSOCK_VQ_RX]
55 	 * must be accessed with rx_lock held.
56 	 */
57 	struct mutex rx_lock;
58 	bool rx_run;
59 	int rx_buf_nr;
60 	int rx_buf_max_nr;
61 
62 	/* The following fields are protected by event_lock.
63 	 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
64 	 */
65 	struct mutex event_lock;
66 	bool event_run;
67 	struct virtio_vsock_event event_list[8];
68 
69 	u32 guest_cid;
70 };
71 
virtio_transport_get_local_cid(void)72 static u32 virtio_transport_get_local_cid(void)
73 {
74 	struct virtio_vsock *vsock;
75 	u32 ret;
76 
77 	rcu_read_lock();
78 	vsock = rcu_dereference(the_virtio_vsock);
79 	if (!vsock) {
80 		ret = VMADDR_CID_ANY;
81 		goto out_rcu;
82 	}
83 
84 	ret = vsock->guest_cid;
85 out_rcu:
86 	rcu_read_unlock();
87 	return ret;
88 }
89 
virtio_transport_send_pkt_loopback(struct virtio_vsock * vsock,struct virtio_vsock_pkt * pkt)90 static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock,
91 					      struct virtio_vsock_pkt *pkt)
92 {
93 	int len = pkt->len;
94 
95 	spin_lock_bh(&vsock->loopback_list_lock);
96 	list_add_tail(&pkt->list, &vsock->loopback_list);
97 	spin_unlock_bh(&vsock->loopback_list_lock);
98 
99 	queue_work(virtio_vsock_workqueue, &vsock->loopback_work);
100 
101 	return len;
102 }
103 
104 static void
virtio_transport_send_pkt_work(struct work_struct * work)105 virtio_transport_send_pkt_work(struct work_struct *work)
106 {
107 	struct virtio_vsock *vsock =
108 		container_of(work, struct virtio_vsock, send_pkt_work);
109 	struct virtqueue *vq;
110 	bool added = false;
111 	bool restart_rx = false;
112 
113 	mutex_lock(&vsock->tx_lock);
114 
115 	if (!vsock->tx_run)
116 		goto out;
117 
118 	vq = vsock->vqs[VSOCK_VQ_TX];
119 
120 	for (;;) {
121 		struct virtio_vsock_pkt *pkt;
122 		struct scatterlist hdr, buf, *sgs[2];
123 		int ret, in_sg = 0, out_sg = 0;
124 		bool reply;
125 
126 		spin_lock_bh(&vsock->send_pkt_list_lock);
127 		if (list_empty(&vsock->send_pkt_list)) {
128 			spin_unlock_bh(&vsock->send_pkt_list_lock);
129 			break;
130 		}
131 
132 		pkt = list_first_entry(&vsock->send_pkt_list,
133 				       struct virtio_vsock_pkt, list);
134 		list_del_init(&pkt->list);
135 		spin_unlock_bh(&vsock->send_pkt_list_lock);
136 
137 		virtio_transport_deliver_tap_pkt(pkt);
138 
139 		reply = pkt->reply;
140 
141 		sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
142 		sgs[out_sg++] = &hdr;
143 		if (pkt->buf) {
144 			sg_init_one(&buf, pkt->buf, pkt->len);
145 			sgs[out_sg++] = &buf;
146 		}
147 
148 		ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
149 		/* Usually this means that there is no more space available in
150 		 * the vq
151 		 */
152 		if (ret < 0) {
153 			spin_lock_bh(&vsock->send_pkt_list_lock);
154 			list_add(&pkt->list, &vsock->send_pkt_list);
155 			spin_unlock_bh(&vsock->send_pkt_list_lock);
156 			break;
157 		}
158 
159 		if (reply) {
160 			struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
161 			int val;
162 
163 			val = atomic_dec_return(&vsock->queued_replies);
164 
165 			/* Do we now have resources to resume rx processing? */
166 			if (val + 1 == virtqueue_get_vring_size(rx_vq))
167 				restart_rx = true;
168 		}
169 
170 		added = true;
171 	}
172 
173 	if (added)
174 		virtqueue_kick(vq);
175 
176 out:
177 	mutex_unlock(&vsock->tx_lock);
178 
179 	if (restart_rx)
180 		queue_work(virtio_vsock_workqueue, &vsock->rx_work);
181 }
182 
183 static int
virtio_transport_send_pkt(struct virtio_vsock_pkt * pkt)184 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
185 {
186 	struct virtio_vsock *vsock;
187 	int len = pkt->len;
188 
189 	rcu_read_lock();
190 	vsock = rcu_dereference(the_virtio_vsock);
191 	if (!vsock) {
192 		virtio_transport_free_pkt(pkt);
193 		len = -ENODEV;
194 		goto out_rcu;
195 	}
196 
197 	if (le64_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid) {
198 		len = virtio_transport_send_pkt_loopback(vsock, pkt);
199 		goto out_rcu;
200 	}
201 
202 	if (pkt->reply)
203 		atomic_inc(&vsock->queued_replies);
204 
205 	spin_lock_bh(&vsock->send_pkt_list_lock);
206 	list_add_tail(&pkt->list, &vsock->send_pkt_list);
207 	spin_unlock_bh(&vsock->send_pkt_list_lock);
208 
209 	queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
210 
211 out_rcu:
212 	rcu_read_unlock();
213 	return len;
214 }
215 
216 static int
virtio_transport_cancel_pkt(struct vsock_sock * vsk)217 virtio_transport_cancel_pkt(struct vsock_sock *vsk)
218 {
219 	struct virtio_vsock *vsock;
220 	struct virtio_vsock_pkt *pkt, *n;
221 	int cnt = 0, ret;
222 	LIST_HEAD(freeme);
223 
224 	rcu_read_lock();
225 	vsock = rcu_dereference(the_virtio_vsock);
226 	if (!vsock) {
227 		ret = -ENODEV;
228 		goto out_rcu;
229 	}
230 
231 	spin_lock_bh(&vsock->send_pkt_list_lock);
232 	list_for_each_entry_safe(pkt, n, &vsock->send_pkt_list, list) {
233 		if (pkt->vsk != vsk)
234 			continue;
235 		list_move(&pkt->list, &freeme);
236 	}
237 	spin_unlock_bh(&vsock->send_pkt_list_lock);
238 
239 	list_for_each_entry_safe(pkt, n, &freeme, list) {
240 		if (pkt->reply)
241 			cnt++;
242 		list_del(&pkt->list);
243 		virtio_transport_free_pkt(pkt);
244 	}
245 
246 	if (cnt) {
247 		struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
248 		int new_cnt;
249 
250 		new_cnt = atomic_sub_return(cnt, &vsock->queued_replies);
251 		if (new_cnt + cnt >= virtqueue_get_vring_size(rx_vq) &&
252 		    new_cnt < virtqueue_get_vring_size(rx_vq))
253 			queue_work(virtio_vsock_workqueue, &vsock->rx_work);
254 	}
255 
256 	ret = 0;
257 
258 out_rcu:
259 	rcu_read_unlock();
260 	return ret;
261 }
262 
virtio_vsock_rx_fill(struct virtio_vsock * vsock)263 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
264 {
265 	int buf_len = VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE;
266 	struct virtio_vsock_pkt *pkt;
267 	struct scatterlist hdr, buf, *sgs[2];
268 	struct virtqueue *vq;
269 	int ret;
270 
271 	vq = vsock->vqs[VSOCK_VQ_RX];
272 
273 	do {
274 		pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
275 		if (!pkt)
276 			break;
277 
278 		pkt->buf = kmalloc(buf_len, GFP_KERNEL);
279 		if (!pkt->buf) {
280 			virtio_transport_free_pkt(pkt);
281 			break;
282 		}
283 
284 		pkt->len = buf_len;
285 
286 		sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
287 		sgs[0] = &hdr;
288 
289 		sg_init_one(&buf, pkt->buf, buf_len);
290 		sgs[1] = &buf;
291 		ret = virtqueue_add_sgs(vq, sgs, 0, 2, pkt, GFP_KERNEL);
292 		if (ret) {
293 			virtio_transport_free_pkt(pkt);
294 			break;
295 		}
296 		vsock->rx_buf_nr++;
297 	} while (vq->num_free);
298 	if (vsock->rx_buf_nr > vsock->rx_buf_max_nr)
299 		vsock->rx_buf_max_nr = vsock->rx_buf_nr;
300 	virtqueue_kick(vq);
301 }
302 
virtio_transport_tx_work(struct work_struct * work)303 static void virtio_transport_tx_work(struct work_struct *work)
304 {
305 	struct virtio_vsock *vsock =
306 		container_of(work, struct virtio_vsock, tx_work);
307 	struct virtqueue *vq;
308 	bool added = false;
309 
310 	vq = vsock->vqs[VSOCK_VQ_TX];
311 	mutex_lock(&vsock->tx_lock);
312 
313 	if (!vsock->tx_run)
314 		goto out;
315 
316 	do {
317 		struct virtio_vsock_pkt *pkt;
318 		unsigned int len;
319 
320 		virtqueue_disable_cb(vq);
321 		while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
322 			virtio_transport_free_pkt(pkt);
323 			added = true;
324 		}
325 	} while (!virtqueue_enable_cb(vq));
326 
327 out:
328 	mutex_unlock(&vsock->tx_lock);
329 
330 	if (added)
331 		queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
332 }
333 
334 /* Is there space left for replies to rx packets? */
virtio_transport_more_replies(struct virtio_vsock * vsock)335 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
336 {
337 	struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
338 	int val;
339 
340 	smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
341 	val = atomic_read(&vsock->queued_replies);
342 
343 	return val < virtqueue_get_vring_size(vq);
344 }
345 
346 /* event_lock must be held */
virtio_vsock_event_fill_one(struct virtio_vsock * vsock,struct virtio_vsock_event * event)347 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
348 				       struct virtio_vsock_event *event)
349 {
350 	struct scatterlist sg;
351 	struct virtqueue *vq;
352 
353 	vq = vsock->vqs[VSOCK_VQ_EVENT];
354 
355 	sg_init_one(&sg, event, sizeof(*event));
356 
357 	return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
358 }
359 
360 /* event_lock must be held */
virtio_vsock_event_fill(struct virtio_vsock * vsock)361 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
362 {
363 	size_t i;
364 
365 	for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
366 		struct virtio_vsock_event *event = &vsock->event_list[i];
367 
368 		virtio_vsock_event_fill_one(vsock, event);
369 	}
370 
371 	virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
372 }
373 
virtio_vsock_reset_sock(struct sock * sk)374 static void virtio_vsock_reset_sock(struct sock *sk)
375 {
376 	lock_sock(sk);
377 	sk->sk_state = TCP_CLOSE;
378 	sk->sk_err = ECONNRESET;
379 	sk->sk_error_report(sk);
380 	release_sock(sk);
381 }
382 
virtio_vsock_update_guest_cid(struct virtio_vsock * vsock)383 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
384 {
385 	struct virtio_device *vdev = vsock->vdev;
386 	__le64 guest_cid;
387 
388 	vdev->config->get(vdev, offsetof(struct virtio_vsock_config, guest_cid),
389 			  &guest_cid, sizeof(guest_cid));
390 	vsock->guest_cid = le64_to_cpu(guest_cid);
391 }
392 
393 /* event_lock must be held */
virtio_vsock_event_handle(struct virtio_vsock * vsock,struct virtio_vsock_event * event)394 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
395 				      struct virtio_vsock_event *event)
396 {
397 	switch (le32_to_cpu(event->id)) {
398 	case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET:
399 		virtio_vsock_update_guest_cid(vsock);
400 		vsock_for_each_connected_socket(virtio_vsock_reset_sock);
401 		break;
402 	}
403 }
404 
virtio_transport_event_work(struct work_struct * work)405 static void virtio_transport_event_work(struct work_struct *work)
406 {
407 	struct virtio_vsock *vsock =
408 		container_of(work, struct virtio_vsock, event_work);
409 	struct virtqueue *vq;
410 
411 	vq = vsock->vqs[VSOCK_VQ_EVENT];
412 
413 	mutex_lock(&vsock->event_lock);
414 
415 	if (!vsock->event_run)
416 		goto out;
417 
418 	do {
419 		struct virtio_vsock_event *event;
420 		unsigned int len;
421 
422 		virtqueue_disable_cb(vq);
423 		while ((event = virtqueue_get_buf(vq, &len)) != NULL) {
424 			if (len == sizeof(*event))
425 				virtio_vsock_event_handle(vsock, event);
426 
427 			virtio_vsock_event_fill_one(vsock, event);
428 		}
429 	} while (!virtqueue_enable_cb(vq));
430 
431 	virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
432 out:
433 	mutex_unlock(&vsock->event_lock);
434 }
435 
virtio_vsock_event_done(struct virtqueue * vq)436 static void virtio_vsock_event_done(struct virtqueue *vq)
437 {
438 	struct virtio_vsock *vsock = vq->vdev->priv;
439 
440 	if (!vsock)
441 		return;
442 	queue_work(virtio_vsock_workqueue, &vsock->event_work);
443 }
444 
virtio_vsock_tx_done(struct virtqueue * vq)445 static void virtio_vsock_tx_done(struct virtqueue *vq)
446 {
447 	struct virtio_vsock *vsock = vq->vdev->priv;
448 
449 	if (!vsock)
450 		return;
451 	queue_work(virtio_vsock_workqueue, &vsock->tx_work);
452 }
453 
virtio_vsock_rx_done(struct virtqueue * vq)454 static void virtio_vsock_rx_done(struct virtqueue *vq)
455 {
456 	struct virtio_vsock *vsock = vq->vdev->priv;
457 
458 	if (!vsock)
459 		return;
460 	queue_work(virtio_vsock_workqueue, &vsock->rx_work);
461 }
462 
463 static struct virtio_transport virtio_transport = {
464 	.transport = {
465 		.get_local_cid            = virtio_transport_get_local_cid,
466 
467 		.init                     = virtio_transport_do_socket_init,
468 		.destruct                 = virtio_transport_destruct,
469 		.release                  = virtio_transport_release,
470 		.connect                  = virtio_transport_connect,
471 		.shutdown                 = virtio_transport_shutdown,
472 		.cancel_pkt               = virtio_transport_cancel_pkt,
473 
474 		.dgram_bind               = virtio_transport_dgram_bind,
475 		.dgram_dequeue            = virtio_transport_dgram_dequeue,
476 		.dgram_enqueue            = virtio_transport_dgram_enqueue,
477 		.dgram_allow              = virtio_transport_dgram_allow,
478 
479 		.stream_dequeue           = virtio_transport_stream_dequeue,
480 		.stream_enqueue           = virtio_transport_stream_enqueue,
481 		.stream_has_data          = virtio_transport_stream_has_data,
482 		.stream_has_space         = virtio_transport_stream_has_space,
483 		.stream_rcvhiwat          = virtio_transport_stream_rcvhiwat,
484 		.stream_is_active         = virtio_transport_stream_is_active,
485 		.stream_allow             = virtio_transport_stream_allow,
486 
487 		.notify_poll_in           = virtio_transport_notify_poll_in,
488 		.notify_poll_out          = virtio_transport_notify_poll_out,
489 		.notify_recv_init         = virtio_transport_notify_recv_init,
490 		.notify_recv_pre_block    = virtio_transport_notify_recv_pre_block,
491 		.notify_recv_pre_dequeue  = virtio_transport_notify_recv_pre_dequeue,
492 		.notify_recv_post_dequeue = virtio_transport_notify_recv_post_dequeue,
493 		.notify_send_init         = virtio_transport_notify_send_init,
494 		.notify_send_pre_block    = virtio_transport_notify_send_pre_block,
495 		.notify_send_pre_enqueue  = virtio_transport_notify_send_pre_enqueue,
496 		.notify_send_post_enqueue = virtio_transport_notify_send_post_enqueue,
497 
498 		.set_buffer_size          = virtio_transport_set_buffer_size,
499 		.set_min_buffer_size      = virtio_transport_set_min_buffer_size,
500 		.set_max_buffer_size      = virtio_transport_set_max_buffer_size,
501 		.get_buffer_size          = virtio_transport_get_buffer_size,
502 		.get_min_buffer_size      = virtio_transport_get_min_buffer_size,
503 		.get_max_buffer_size      = virtio_transport_get_max_buffer_size,
504 	},
505 
506 	.send_pkt = virtio_transport_send_pkt,
507 };
508 
virtio_transport_loopback_work(struct work_struct * work)509 static void virtio_transport_loopback_work(struct work_struct *work)
510 {
511 	struct virtio_vsock *vsock =
512 		container_of(work, struct virtio_vsock, loopback_work);
513 	LIST_HEAD(pkts);
514 
515 	spin_lock_bh(&vsock->loopback_list_lock);
516 	list_splice_init(&vsock->loopback_list, &pkts);
517 	spin_unlock_bh(&vsock->loopback_list_lock);
518 
519 	mutex_lock(&vsock->rx_lock);
520 
521 	if (!vsock->rx_run)
522 		goto out;
523 
524 	while (!list_empty(&pkts)) {
525 		struct virtio_vsock_pkt *pkt;
526 
527 		pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
528 		list_del_init(&pkt->list);
529 
530 		virtio_transport_recv_pkt(&virtio_transport, pkt);
531 	}
532 out:
533 	mutex_unlock(&vsock->rx_lock);
534 }
535 
virtio_transport_rx_work(struct work_struct * work)536 static void virtio_transport_rx_work(struct work_struct *work)
537 {
538 	struct virtio_vsock *vsock =
539 		container_of(work, struct virtio_vsock, rx_work);
540 	struct virtqueue *vq;
541 
542 	vq = vsock->vqs[VSOCK_VQ_RX];
543 
544 	mutex_lock(&vsock->rx_lock);
545 
546 	if (!vsock->rx_run)
547 		goto out;
548 
549 	do {
550 		virtqueue_disable_cb(vq);
551 		for (;;) {
552 			struct virtio_vsock_pkt *pkt;
553 			unsigned int len;
554 
555 			if (!virtio_transport_more_replies(vsock)) {
556 				/* Stop rx until the device processes already
557 				 * pending replies.  Leave rx virtqueue
558 				 * callbacks disabled.
559 				 */
560 				goto out;
561 			}
562 
563 			pkt = virtqueue_get_buf(vq, &len);
564 			if (!pkt) {
565 				break;
566 			}
567 
568 			vsock->rx_buf_nr--;
569 
570 			/* Drop short/long packets */
571 			if (unlikely(len < sizeof(pkt->hdr) ||
572 				     len > sizeof(pkt->hdr) + pkt->len)) {
573 				virtio_transport_free_pkt(pkt);
574 				continue;
575 			}
576 
577 			pkt->len = len - sizeof(pkt->hdr);
578 			virtio_transport_deliver_tap_pkt(pkt);
579 			virtio_transport_recv_pkt(&virtio_transport, pkt);
580 		}
581 	} while (!virtqueue_enable_cb(vq));
582 
583 out:
584 	if (vsock->rx_buf_nr < vsock->rx_buf_max_nr / 2)
585 		virtio_vsock_rx_fill(vsock);
586 	mutex_unlock(&vsock->rx_lock);
587 }
588 
virtio_vsock_probe(struct virtio_device * vdev)589 static int virtio_vsock_probe(struct virtio_device *vdev)
590 {
591 	vq_callback_t *callbacks[] = {
592 		virtio_vsock_rx_done,
593 		virtio_vsock_tx_done,
594 		virtio_vsock_event_done,
595 	};
596 	static const char * const names[] = {
597 		"rx",
598 		"tx",
599 		"event",
600 	};
601 	struct virtio_vsock *vsock = NULL;
602 	int ret;
603 
604 	ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
605 	if (ret)
606 		return ret;
607 
608 	/* Only one virtio-vsock device per guest is supported */
609 	if (rcu_dereference_protected(the_virtio_vsock,
610 				lockdep_is_held(&the_virtio_vsock_mutex))) {
611 		ret = -EBUSY;
612 		goto out;
613 	}
614 
615 	vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
616 	if (!vsock) {
617 		ret = -ENOMEM;
618 		goto out;
619 	}
620 
621 	vsock->vdev = vdev;
622 
623 	ret = virtio_find_vqs(vsock->vdev, VSOCK_VQ_MAX,
624 			      vsock->vqs, callbacks, names,
625 			      NULL);
626 	if (ret < 0)
627 		goto out;
628 
629 	virtio_vsock_update_guest_cid(vsock);
630 
631 	vsock->rx_buf_nr = 0;
632 	vsock->rx_buf_max_nr = 0;
633 	atomic_set(&vsock->queued_replies, 0);
634 
635 	mutex_init(&vsock->tx_lock);
636 	mutex_init(&vsock->rx_lock);
637 	mutex_init(&vsock->event_lock);
638 	spin_lock_init(&vsock->send_pkt_list_lock);
639 	INIT_LIST_HEAD(&vsock->send_pkt_list);
640 	spin_lock_init(&vsock->loopback_list_lock);
641 	INIT_LIST_HEAD(&vsock->loopback_list);
642 	INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
643 	INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
644 	INIT_WORK(&vsock->event_work, virtio_transport_event_work);
645 	INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
646 	INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
647 
648 	mutex_lock(&vsock->tx_lock);
649 	vsock->tx_run = true;
650 	mutex_unlock(&vsock->tx_lock);
651 
652 	mutex_lock(&vsock->rx_lock);
653 	virtio_vsock_rx_fill(vsock);
654 	vsock->rx_run = true;
655 	mutex_unlock(&vsock->rx_lock);
656 
657 	mutex_lock(&vsock->event_lock);
658 	virtio_vsock_event_fill(vsock);
659 	vsock->event_run = true;
660 	mutex_unlock(&vsock->event_lock);
661 
662 	vdev->priv = vsock;
663 	rcu_assign_pointer(the_virtio_vsock, vsock);
664 
665 	mutex_unlock(&the_virtio_vsock_mutex);
666 	return 0;
667 
668 out:
669 	kfree(vsock);
670 	mutex_unlock(&the_virtio_vsock_mutex);
671 	return ret;
672 }
673 
virtio_vsock_remove(struct virtio_device * vdev)674 static void virtio_vsock_remove(struct virtio_device *vdev)
675 {
676 	struct virtio_vsock *vsock = vdev->priv;
677 	struct virtio_vsock_pkt *pkt;
678 
679 	mutex_lock(&the_virtio_vsock_mutex);
680 
681 	vdev->priv = NULL;
682 	rcu_assign_pointer(the_virtio_vsock, NULL);
683 	synchronize_rcu();
684 
685 	flush_work(&vsock->loopback_work);
686 	flush_work(&vsock->rx_work);
687 	flush_work(&vsock->tx_work);
688 	flush_work(&vsock->event_work);
689 	flush_work(&vsock->send_pkt_work);
690 
691 	/* Reset all connected sockets when the device disappear */
692 	vsock_for_each_connected_socket(virtio_vsock_reset_sock);
693 
694 	/* Stop all work handlers to make sure no one is accessing the device,
695 	 * so we can safely call vdev->config->reset().
696 	 */
697 	mutex_lock(&vsock->rx_lock);
698 	vsock->rx_run = false;
699 	mutex_unlock(&vsock->rx_lock);
700 
701 	mutex_lock(&vsock->tx_lock);
702 	vsock->tx_run = false;
703 	mutex_unlock(&vsock->tx_lock);
704 
705 	mutex_lock(&vsock->event_lock);
706 	vsock->event_run = false;
707 	mutex_unlock(&vsock->event_lock);
708 
709 	/* Flush all device writes and interrupts, device will not use any
710 	 * more buffers.
711 	 */
712 	vdev->config->reset(vdev);
713 
714 	mutex_lock(&vsock->rx_lock);
715 	while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
716 		virtio_transport_free_pkt(pkt);
717 	mutex_unlock(&vsock->rx_lock);
718 
719 	mutex_lock(&vsock->tx_lock);
720 	while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
721 		virtio_transport_free_pkt(pkt);
722 	mutex_unlock(&vsock->tx_lock);
723 
724 	spin_lock_bh(&vsock->send_pkt_list_lock);
725 	while (!list_empty(&vsock->send_pkt_list)) {
726 		pkt = list_first_entry(&vsock->send_pkt_list,
727 				       struct virtio_vsock_pkt, list);
728 		list_del(&pkt->list);
729 		virtio_transport_free_pkt(pkt);
730 	}
731 	spin_unlock_bh(&vsock->send_pkt_list_lock);
732 
733 	spin_lock_bh(&vsock->loopback_list_lock);
734 	while (!list_empty(&vsock->loopback_list)) {
735 		pkt = list_first_entry(&vsock->loopback_list,
736 				       struct virtio_vsock_pkt, list);
737 		list_del(&pkt->list);
738 		virtio_transport_free_pkt(pkt);
739 	}
740 	spin_unlock_bh(&vsock->loopback_list_lock);
741 
742 	/* Delete virtqueues and flush outstanding callbacks if any */
743 	vdev->config->del_vqs(vdev);
744 
745 	mutex_unlock(&the_virtio_vsock_mutex);
746 
747 	kfree(vsock);
748 }
749 
750 static struct virtio_device_id id_table[] = {
751 	{ VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
752 	{ 0 },
753 };
754 
755 static unsigned int features[] = {
756 };
757 
758 static struct virtio_driver virtio_vsock_driver = {
759 	.feature_table = features,
760 	.feature_table_size = ARRAY_SIZE(features),
761 	.driver.name = KBUILD_MODNAME,
762 	.driver.owner = THIS_MODULE,
763 	.id_table = id_table,
764 	.probe = virtio_vsock_probe,
765 	.remove = virtio_vsock_remove,
766 };
767 
virtio_vsock_init(void)768 static int __init virtio_vsock_init(void)
769 {
770 	int ret;
771 
772 	virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
773 	if (!virtio_vsock_workqueue)
774 		return -ENOMEM;
775 
776 	ret = vsock_core_init(&virtio_transport.transport);
777 	if (ret)
778 		goto out_wq;
779 
780 	ret = register_virtio_driver(&virtio_vsock_driver);
781 	if (ret)
782 		goto out_vci;
783 
784 	return 0;
785 
786 out_vci:
787 	vsock_core_exit();
788 out_wq:
789 	destroy_workqueue(virtio_vsock_workqueue);
790 	return ret;
791 }
792 
virtio_vsock_exit(void)793 static void __exit virtio_vsock_exit(void)
794 {
795 	unregister_virtio_driver(&virtio_vsock_driver);
796 	vsock_core_exit();
797 	destroy_workqueue(virtio_vsock_workqueue);
798 }
799 
800 module_init(virtio_vsock_init);
801 module_exit(virtio_vsock_exit);
802 MODULE_LICENSE("GPL v2");
803 MODULE_AUTHOR("Asias He");
804 MODULE_DESCRIPTION("virtio transport for vsock");
805 MODULE_DEVICE_TABLE(virtio, id_table);
806