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