qed: Deprecate VF multiple queue-stop
[linux-2.6/btrfs-unstable.git] / net / vmw_vsock / virtio_transport.c
blob9d24c0e958b18e614e30b24c0fcfbbe2152941f3
1 /*
2 * virtio transport for vsock
4 * Copyright (C) 2013-2015 Red Hat, Inc.
5 * Author: Asias He <asias@redhat.com>
6 * Stefan Hajnoczi <stefanha@redhat.com>
8 * Some of the code is take from Gerd Hoffmann <kraxel@redhat.com>'s
9 * early virtio-vsock proof-of-concept bits.
11 * This work is licensed under the terms of the GNU GPL, version 2.
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>
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 */
29 struct virtio_vsock {
30 struct virtio_device *vdev;
31 struct virtqueue *vqs[VSOCK_VQ_MAX];
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;
38 /* The following fields are protected by tx_lock. vqs[VSOCK_VQ_TX]
39 * must be accessed with tx_lock held.
41 struct mutex tx_lock;
43 struct work_struct send_pkt_work;
44 spinlock_t send_pkt_list_lock;
45 struct list_head send_pkt_list;
47 struct work_struct loopback_work;
48 spinlock_t loopback_list_lock; /* protects loopback_list */
49 struct list_head loopback_list;
51 atomic_t queued_replies;
53 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
54 * must be accessed with rx_lock held.
56 struct mutex rx_lock;
57 int rx_buf_nr;
58 int rx_buf_max_nr;
60 /* The following fields are protected by event_lock.
61 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
63 struct mutex event_lock;
64 struct virtio_vsock_event event_list[8];
66 u32 guest_cid;
69 static struct virtio_vsock *virtio_vsock_get(void)
71 return the_virtio_vsock;
74 static u32 virtio_transport_get_local_cid(void)
76 struct virtio_vsock *vsock = virtio_vsock_get();
78 return vsock->guest_cid;
81 static void virtio_transport_loopback_work(struct work_struct *work)
83 struct virtio_vsock *vsock =
84 container_of(work, struct virtio_vsock, loopback_work);
85 LIST_HEAD(pkts);
87 spin_lock_bh(&vsock->loopback_list_lock);
88 list_splice_init(&vsock->loopback_list, &pkts);
89 spin_unlock_bh(&vsock->loopback_list_lock);
91 mutex_lock(&vsock->rx_lock);
92 while (!list_empty(&pkts)) {
93 struct virtio_vsock_pkt *pkt;
95 pkt = list_first_entry(&pkts, struct virtio_vsock_pkt, list);
96 list_del_init(&pkt->list);
98 virtio_transport_recv_pkt(pkt);
100 mutex_unlock(&vsock->rx_lock);
103 static int virtio_transport_send_pkt_loopback(struct virtio_vsock *vsock,
104 struct virtio_vsock_pkt *pkt)
106 int len = pkt->len;
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);
112 queue_work(virtio_vsock_workqueue, &vsock->loopback_work);
114 return len;
117 static void
118 virtio_transport_send_pkt_work(struct work_struct *work)
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;
126 mutex_lock(&vsock->tx_lock);
128 vq = vsock->vqs[VSOCK_VQ_TX];
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;
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);
139 break;
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);
147 reply = pkt->reply;
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;
156 ret = virtqueue_add_sgs(vq, sgs, out_sg, in_sg, pkt, GFP_KERNEL);
157 /* Usually this means that there is no more space available in
158 * the vq
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);
164 break;
167 if (reply) {
168 struct virtqueue *rx_vq = vsock->vqs[VSOCK_VQ_RX];
169 int val;
171 val = atomic_dec_return(&vsock->queued_replies);
173 /* Do we now have resources to resume rx processing? */
174 if (val + 1 == virtqueue_get_vring_size(rx_vq))
175 restart_rx = true;
178 added = true;
181 if (added)
182 virtqueue_kick(vq);
184 mutex_unlock(&vsock->tx_lock);
186 if (restart_rx)
187 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
190 static int
191 virtio_transport_send_pkt(struct virtio_vsock_pkt *pkt)
193 struct virtio_vsock *vsock;
194 int len = pkt->len;
196 vsock = virtio_vsock_get();
197 if (!vsock) {
198 virtio_transport_free_pkt(pkt);
199 return -ENODEV;
202 if (le32_to_cpu(pkt->hdr.dst_cid) == vsock->guest_cid)
203 return virtio_transport_send_pkt_loopback(vsock, pkt);
205 if (pkt->reply)
206 atomic_inc(&vsock->queued_replies);
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);
212 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
213 return len;
216 static void virtio_vsock_rx_fill(struct virtio_vsock *vsock)
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;
224 vq = vsock->vqs[VSOCK_VQ_RX];
226 do {
227 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
228 if (!pkt)
229 break;
231 pkt->buf = kmalloc(buf_len, GFP_KERNEL);
232 if (!pkt->buf) {
233 virtio_transport_free_pkt(pkt);
234 break;
237 pkt->len = buf_len;
239 sg_init_one(&hdr, &pkt->hdr, sizeof(pkt->hdr));
240 sgs[0] = &hdr;
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;
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);
256 static void virtio_transport_tx_work(struct work_struct *work)
258 struct virtio_vsock *vsock =
259 container_of(work, struct virtio_vsock, tx_work);
260 struct virtqueue *vq;
261 bool added = false;
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;
269 virtqueue_disable_cb(vq);
270 while ((pkt = virtqueue_get_buf(vq, &len)) != NULL) {
271 virtio_transport_free_pkt(pkt);
272 added = true;
274 } while (!virtqueue_enable_cb(vq));
275 mutex_unlock(&vsock->tx_lock);
277 if (added)
278 queue_work(virtio_vsock_workqueue, &vsock->send_pkt_work);
281 /* Is there space left for replies to rx packets? */
282 static bool virtio_transport_more_replies(struct virtio_vsock *vsock)
284 struct virtqueue *vq = vsock->vqs[VSOCK_VQ_RX];
285 int val;
287 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
288 val = atomic_read(&vsock->queued_replies);
290 return val < virtqueue_get_vring_size(vq);
293 static void virtio_transport_rx_work(struct work_struct *work)
295 struct virtio_vsock *vsock =
296 container_of(work, struct virtio_vsock, rx_work);
297 struct virtqueue *vq;
299 vq = vsock->vqs[VSOCK_VQ_RX];
301 mutex_lock(&vsock->rx_lock);
303 do {
304 virtqueue_disable_cb(vq);
305 for (;;) {
306 struct virtio_vsock_pkt *pkt;
307 unsigned int len;
309 if (!virtio_transport_more_replies(vsock)) {
310 /* Stop rx until the device processes already
311 * pending replies. Leave rx virtqueue
312 * callbacks disabled.
314 goto out;
317 pkt = virtqueue_get_buf(vq, &len);
318 if (!pkt) {
319 break;
322 vsock->rx_buf_nr--;
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;
331 pkt->len = len - sizeof(pkt->hdr);
332 virtio_transport_recv_pkt(pkt);
334 } while (!virtqueue_enable_cb(vq));
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);
342 /* event_lock must be held */
343 static int virtio_vsock_event_fill_one(struct virtio_vsock *vsock,
344 struct virtio_vsock_event *event)
346 struct scatterlist sg;
347 struct virtqueue *vq;
349 vq = vsock->vqs[VSOCK_VQ_EVENT];
351 sg_init_one(&sg, event, sizeof(*event));
353 return virtqueue_add_inbuf(vq, &sg, 1, event, GFP_KERNEL);
356 /* event_lock must be held */
357 static void virtio_vsock_event_fill(struct virtio_vsock *vsock)
359 size_t i;
361 for (i = 0; i < ARRAY_SIZE(vsock->event_list); i++) {
362 struct virtio_vsock_event *event = &vsock->event_list[i];
364 virtio_vsock_event_fill_one(vsock, event);
367 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
370 static void virtio_vsock_reset_sock(struct sock *sk)
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);
379 static void virtio_vsock_update_guest_cid(struct virtio_vsock *vsock)
381 struct virtio_device *vdev = vsock->vdev;
382 __le64 guest_cid;
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);
389 /* event_lock must be held */
390 static void virtio_vsock_event_handle(struct virtio_vsock *vsock,
391 struct virtio_vsock_event *event)
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;
401 static void virtio_transport_event_work(struct work_struct *work)
403 struct virtio_vsock *vsock =
404 container_of(work, struct virtio_vsock, event_work);
405 struct virtqueue *vq;
407 vq = vsock->vqs[VSOCK_VQ_EVENT];
409 mutex_lock(&vsock->event_lock);
411 do {
412 struct virtio_vsock_event *event;
413 unsigned int len;
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);
420 virtio_vsock_event_fill_one(vsock, event);
422 } while (!virtqueue_enable_cb(vq));
424 virtqueue_kick(vsock->vqs[VSOCK_VQ_EVENT]);
426 mutex_unlock(&vsock->event_lock);
429 static void virtio_vsock_event_done(struct virtqueue *vq)
431 struct virtio_vsock *vsock = vq->vdev->priv;
433 if (!vsock)
434 return;
435 queue_work(virtio_vsock_workqueue, &vsock->event_work);
438 static void virtio_vsock_tx_done(struct virtqueue *vq)
440 struct virtio_vsock *vsock = vq->vdev->priv;
442 if (!vsock)
443 return;
444 queue_work(virtio_vsock_workqueue, &vsock->tx_work);
447 static void virtio_vsock_rx_done(struct virtqueue *vq)
449 struct virtio_vsock *vsock = vq->vdev->priv;
451 if (!vsock)
452 return;
453 queue_work(virtio_vsock_workqueue, &vsock->rx_work);
456 static struct virtio_transport virtio_transport = {
457 .transport = {
458 .get_local_cid = virtio_transport_get_local_cid,
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,
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,
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,
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,
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,
498 .send_pkt = virtio_transport_send_pkt,
501 static int virtio_vsock_probe(struct virtio_device *vdev)
503 vq_callback_t *callbacks[] = {
504 virtio_vsock_rx_done,
505 virtio_vsock_tx_done,
506 virtio_vsock_event_done,
508 static const char * const names[] = {
509 "rx",
510 "tx",
511 "event",
513 struct virtio_vsock *vsock = NULL;
514 int ret;
516 ret = mutex_lock_interruptible(&the_virtio_vsock_mutex);
517 if (ret)
518 return ret;
520 /* Only one virtio-vsock device per guest is supported */
521 if (the_virtio_vsock) {
522 ret = -EBUSY;
523 goto out;
526 vsock = kzalloc(sizeof(*vsock), GFP_KERNEL);
527 if (!vsock) {
528 ret = -ENOMEM;
529 goto out;
532 vsock->vdev = vdev;
534 ret = vsock->vdev->config->find_vqs(vsock->vdev, VSOCK_VQ_MAX,
535 vsock->vqs, callbacks, names,
536 NULL);
537 if (ret < 0)
538 goto out;
540 virtio_vsock_update_guest_cid(vsock);
542 ret = vsock_core_init(&virtio_transport.transport);
543 if (ret < 0)
544 goto out_vqs;
546 vsock->rx_buf_nr = 0;
547 vsock->rx_buf_max_nr = 0;
548 atomic_set(&vsock->queued_replies, 0);
550 vdev->priv = vsock;
551 the_virtio_vsock = vsock;
552 mutex_init(&vsock->tx_lock);
553 mutex_init(&vsock->rx_lock);
554 mutex_init(&vsock->event_lock);
555 spin_lock_init(&vsock->send_pkt_list_lock);
556 INIT_LIST_HEAD(&vsock->send_pkt_list);
557 spin_lock_init(&vsock->loopback_list_lock);
558 INIT_LIST_HEAD(&vsock->loopback_list);
559 INIT_WORK(&vsock->rx_work, virtio_transport_rx_work);
560 INIT_WORK(&vsock->tx_work, virtio_transport_tx_work);
561 INIT_WORK(&vsock->event_work, virtio_transport_event_work);
562 INIT_WORK(&vsock->send_pkt_work, virtio_transport_send_pkt_work);
563 INIT_WORK(&vsock->loopback_work, virtio_transport_loopback_work);
565 mutex_lock(&vsock->rx_lock);
566 virtio_vsock_rx_fill(vsock);
567 mutex_unlock(&vsock->rx_lock);
569 mutex_lock(&vsock->event_lock);
570 virtio_vsock_event_fill(vsock);
571 mutex_unlock(&vsock->event_lock);
573 mutex_unlock(&the_virtio_vsock_mutex);
574 return 0;
576 out_vqs:
577 vsock->vdev->config->del_vqs(vsock->vdev);
578 out:
579 kfree(vsock);
580 mutex_unlock(&the_virtio_vsock_mutex);
581 return ret;
584 static void virtio_vsock_remove(struct virtio_device *vdev)
586 struct virtio_vsock *vsock = vdev->priv;
587 struct virtio_vsock_pkt *pkt;
589 flush_work(&vsock->loopback_work);
590 flush_work(&vsock->rx_work);
591 flush_work(&vsock->tx_work);
592 flush_work(&vsock->event_work);
593 flush_work(&vsock->send_pkt_work);
595 vdev->config->reset(vdev);
597 mutex_lock(&vsock->rx_lock);
598 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_RX])))
599 virtio_transport_free_pkt(pkt);
600 mutex_unlock(&vsock->rx_lock);
602 mutex_lock(&vsock->tx_lock);
603 while ((pkt = virtqueue_detach_unused_buf(vsock->vqs[VSOCK_VQ_TX])))
604 virtio_transport_free_pkt(pkt);
605 mutex_unlock(&vsock->tx_lock);
607 spin_lock_bh(&vsock->send_pkt_list_lock);
608 while (!list_empty(&vsock->send_pkt_list)) {
609 pkt = list_first_entry(&vsock->send_pkt_list,
610 struct virtio_vsock_pkt, list);
611 list_del(&pkt->list);
612 virtio_transport_free_pkt(pkt);
614 spin_unlock_bh(&vsock->send_pkt_list_lock);
616 spin_lock_bh(&vsock->loopback_list_lock);
617 while (!list_empty(&vsock->loopback_list)) {
618 pkt = list_first_entry(&vsock->loopback_list,
619 struct virtio_vsock_pkt, list);
620 list_del(&pkt->list);
621 virtio_transport_free_pkt(pkt);
623 spin_unlock_bh(&vsock->loopback_list_lock);
625 mutex_lock(&the_virtio_vsock_mutex);
626 the_virtio_vsock = NULL;
627 vsock_core_exit();
628 mutex_unlock(&the_virtio_vsock_mutex);
630 vdev->config->del_vqs(vdev);
632 kfree(vsock);
635 static struct virtio_device_id id_table[] = {
636 { VIRTIO_ID_VSOCK, VIRTIO_DEV_ANY_ID },
637 { 0 },
640 static unsigned int features[] = {
643 static struct virtio_driver virtio_vsock_driver = {
644 .feature_table = features,
645 .feature_table_size = ARRAY_SIZE(features),
646 .driver.name = KBUILD_MODNAME,
647 .driver.owner = THIS_MODULE,
648 .id_table = id_table,
649 .probe = virtio_vsock_probe,
650 .remove = virtio_vsock_remove,
653 static int __init virtio_vsock_init(void)
655 int ret;
657 virtio_vsock_workqueue = alloc_workqueue("virtio_vsock", 0, 0);
658 if (!virtio_vsock_workqueue)
659 return -ENOMEM;
660 ret = register_virtio_driver(&virtio_vsock_driver);
661 if (ret)
662 destroy_workqueue(virtio_vsock_workqueue);
663 return ret;
666 static void __exit virtio_vsock_exit(void)
668 unregister_virtio_driver(&virtio_vsock_driver);
669 destroy_workqueue(virtio_vsock_workqueue);
672 module_init(virtio_vsock_init);
673 module_exit(virtio_vsock_exit);
674 MODULE_LICENSE("GPL v2");
675 MODULE_AUTHOR("Asias He");
676 MODULE_DESCRIPTION("virtio transport for vsock");
677 MODULE_DEVICE_TABLE(virtio, id_table);