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>
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 */
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.
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.
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];
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();
79 return VMADDR_CID_ANY
;
81 return vsock
->guest_cid
;
84 static void virtio_transport_loopback_work(struct work_struct
*work
)
86 struct virtio_vsock
*vsock
=
87 container_of(work
, struct virtio_vsock
, loopback_work
);
90 spin_lock_bh(&vsock
->loopback_list_lock
);
91 list_splice_init(&vsock
->loopback_list
, &pkts
);
92 spin_unlock_bh(&vsock
->loopback_list_lock
);
94 mutex_lock(&vsock
->rx_lock
);
95 while (!list_empty(&pkts
)) {
96 struct virtio_vsock_pkt
*pkt
;
98 pkt
= list_first_entry(&pkts
, struct virtio_vsock_pkt
, list
);
99 list_del_init(&pkt
->list
);
101 virtio_transport_recv_pkt(pkt
);
103 mutex_unlock(&vsock
->rx_lock
);
106 static int virtio_transport_send_pkt_loopback(struct virtio_vsock
*vsock
,
107 struct virtio_vsock_pkt
*pkt
)
111 spin_lock_bh(&vsock
->loopback_list_lock
);
112 list_add_tail(&pkt
->list
, &vsock
->loopback_list
);
113 spin_unlock_bh(&vsock
->loopback_list_lock
);
115 queue_work(virtio_vsock_workqueue
, &vsock
->loopback_work
);
121 virtio_transport_send_pkt_work(struct work_struct
*work
)
123 struct virtio_vsock
*vsock
=
124 container_of(work
, struct virtio_vsock
, send_pkt_work
);
125 struct virtqueue
*vq
;
127 bool restart_rx
= false;
129 mutex_lock(&vsock
->tx_lock
);
131 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
134 struct virtio_vsock_pkt
*pkt
;
135 struct scatterlist hdr
, buf
, *sgs
[2];
136 int ret
, in_sg
= 0, out_sg
= 0;
139 spin_lock_bh(&vsock
->send_pkt_list_lock
);
140 if (list_empty(&vsock
->send_pkt_list
)) {
141 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
145 pkt
= list_first_entry(&vsock
->send_pkt_list
,
146 struct virtio_vsock_pkt
, list
);
147 list_del_init(&pkt
->list
);
148 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
150 virtio_transport_deliver_tap_pkt(pkt
);
154 sg_init_one(&hdr
, &pkt
->hdr
, sizeof(pkt
->hdr
));
155 sgs
[out_sg
++] = &hdr
;
157 sg_init_one(&buf
, pkt
->buf
, pkt
->len
);
158 sgs
[out_sg
++] = &buf
;
161 ret
= virtqueue_add_sgs(vq
, sgs
, out_sg
, in_sg
, pkt
, GFP_KERNEL
);
162 /* Usually this means that there is no more space available in
166 spin_lock_bh(&vsock
->send_pkt_list_lock
);
167 list_add(&pkt
->list
, &vsock
->send_pkt_list
);
168 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
173 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
176 val
= atomic_dec_return(&vsock
->queued_replies
);
178 /* Do we now have resources to resume rx processing? */
179 if (val
+ 1 == virtqueue_get_vring_size(rx_vq
))
189 mutex_unlock(&vsock
->tx_lock
);
192 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
196 virtio_transport_send_pkt(struct virtio_vsock_pkt
*pkt
)
198 struct virtio_vsock
*vsock
;
201 vsock
= virtio_vsock_get();
203 virtio_transport_free_pkt(pkt
);
207 if (le64_to_cpu(pkt
->hdr
.dst_cid
) == vsock
->guest_cid
)
208 return virtio_transport_send_pkt_loopback(vsock
, pkt
);
211 atomic_inc(&vsock
->queued_replies
);
213 spin_lock_bh(&vsock
->send_pkt_list_lock
);
214 list_add_tail(&pkt
->list
, &vsock
->send_pkt_list
);
215 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
217 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
222 virtio_transport_cancel_pkt(struct vsock_sock
*vsk
)
224 struct virtio_vsock
*vsock
;
225 struct virtio_vsock_pkt
*pkt
, *n
;
229 vsock
= virtio_vsock_get();
234 spin_lock_bh(&vsock
->send_pkt_list_lock
);
235 list_for_each_entry_safe(pkt
, n
, &vsock
->send_pkt_list
, list
) {
238 list_move(&pkt
->list
, &freeme
);
240 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
242 list_for_each_entry_safe(pkt
, n
, &freeme
, list
) {
245 list_del(&pkt
->list
);
246 virtio_transport_free_pkt(pkt
);
250 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
253 new_cnt
= atomic_sub_return(cnt
, &vsock
->queued_replies
);
254 if (new_cnt
+ cnt
>= virtqueue_get_vring_size(rx_vq
) &&
255 new_cnt
< virtqueue_get_vring_size(rx_vq
))
256 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
262 static void virtio_vsock_rx_fill(struct virtio_vsock
*vsock
)
264 int buf_len
= VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE
;
265 struct virtio_vsock_pkt
*pkt
;
266 struct scatterlist hdr
, buf
, *sgs
[2];
267 struct virtqueue
*vq
;
270 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
273 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
277 pkt
->buf
= kmalloc(buf_len
, GFP_KERNEL
);
279 virtio_transport_free_pkt(pkt
);
285 sg_init_one(&hdr
, &pkt
->hdr
, sizeof(pkt
->hdr
));
288 sg_init_one(&buf
, pkt
->buf
, buf_len
);
290 ret
= virtqueue_add_sgs(vq
, sgs
, 0, 2, pkt
, GFP_KERNEL
);
292 virtio_transport_free_pkt(pkt
);
296 } while (vq
->num_free
);
297 if (vsock
->rx_buf_nr
> vsock
->rx_buf_max_nr
)
298 vsock
->rx_buf_max_nr
= vsock
->rx_buf_nr
;
302 static void virtio_transport_tx_work(struct work_struct
*work
)
304 struct virtio_vsock
*vsock
=
305 container_of(work
, struct virtio_vsock
, tx_work
);
306 struct virtqueue
*vq
;
309 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
310 mutex_lock(&vsock
->tx_lock
);
312 struct virtio_vsock_pkt
*pkt
;
315 virtqueue_disable_cb(vq
);
316 while ((pkt
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
317 virtio_transport_free_pkt(pkt
);
320 } while (!virtqueue_enable_cb(vq
));
321 mutex_unlock(&vsock
->tx_lock
);
324 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
327 /* Is there space left for replies to rx packets? */
328 static bool virtio_transport_more_replies(struct virtio_vsock
*vsock
)
330 struct virtqueue
*vq
= vsock
->vqs
[VSOCK_VQ_RX
];
333 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
334 val
= atomic_read(&vsock
->queued_replies
);
336 return val
< virtqueue_get_vring_size(vq
);
339 static void virtio_transport_rx_work(struct work_struct
*work
)
341 struct virtio_vsock
*vsock
=
342 container_of(work
, struct virtio_vsock
, rx_work
);
343 struct virtqueue
*vq
;
345 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
347 mutex_lock(&vsock
->rx_lock
);
350 virtqueue_disable_cb(vq
);
352 struct virtio_vsock_pkt
*pkt
;
355 if (!virtio_transport_more_replies(vsock
)) {
356 /* Stop rx until the device processes already
357 * pending replies. Leave rx virtqueue
358 * callbacks disabled.
363 pkt
= virtqueue_get_buf(vq
, &len
);
370 /* Drop short/long packets */
371 if (unlikely(len
< sizeof(pkt
->hdr
) ||
372 len
> sizeof(pkt
->hdr
) + pkt
->len
)) {
373 virtio_transport_free_pkt(pkt
);
377 pkt
->len
= len
- sizeof(pkt
->hdr
);
378 virtio_transport_deliver_tap_pkt(pkt
);
379 virtio_transport_recv_pkt(pkt
);
381 } while (!virtqueue_enable_cb(vq
));
384 if (vsock
->rx_buf_nr
< vsock
->rx_buf_max_nr
/ 2)
385 virtio_vsock_rx_fill(vsock
);
386 mutex_unlock(&vsock
->rx_lock
);
389 /* event_lock must be held */
390 static int virtio_vsock_event_fill_one(struct virtio_vsock
*vsock
,
391 struct virtio_vsock_event
*event
)
393 struct scatterlist sg
;
394 struct virtqueue
*vq
;
396 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
398 sg_init_one(&sg
, event
, sizeof(*event
));
400 return virtqueue_add_inbuf(vq
, &sg
, 1, event
, GFP_KERNEL
);
403 /* event_lock must be held */
404 static void virtio_vsock_event_fill(struct virtio_vsock
*vsock
)
408 for (i
= 0; i
< ARRAY_SIZE(vsock
->event_list
); i
++) {
409 struct virtio_vsock_event
*event
= &vsock
->event_list
[i
];
411 virtio_vsock_event_fill_one(vsock
, event
);
414 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
417 static void virtio_vsock_reset_sock(struct sock
*sk
)
420 sk
->sk_state
= TCP_CLOSE
;
421 sk
->sk_err
= ECONNRESET
;
422 sk
->sk_error_report(sk
);
426 static void virtio_vsock_update_guest_cid(struct virtio_vsock
*vsock
)
428 struct virtio_device
*vdev
= vsock
->vdev
;
431 vdev
->config
->get(vdev
, offsetof(struct virtio_vsock_config
, guest_cid
),
432 &guest_cid
, sizeof(guest_cid
));
433 vsock
->guest_cid
= le64_to_cpu(guest_cid
);
436 /* event_lock must be held */
437 static void virtio_vsock_event_handle(struct virtio_vsock
*vsock
,
438 struct virtio_vsock_event
*event
)
440 switch (le32_to_cpu(event
->id
)) {
441 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
:
442 virtio_vsock_update_guest_cid(vsock
);
443 vsock_for_each_connected_socket(virtio_vsock_reset_sock
);
448 static void virtio_transport_event_work(struct work_struct
*work
)
450 struct virtio_vsock
*vsock
=
451 container_of(work
, struct virtio_vsock
, event_work
);
452 struct virtqueue
*vq
;
454 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
456 mutex_lock(&vsock
->event_lock
);
459 struct virtio_vsock_event
*event
;
462 virtqueue_disable_cb(vq
);
463 while ((event
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
464 if (len
== sizeof(*event
))
465 virtio_vsock_event_handle(vsock
, event
);
467 virtio_vsock_event_fill_one(vsock
, event
);
469 } while (!virtqueue_enable_cb(vq
));
471 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
473 mutex_unlock(&vsock
->event_lock
);
476 static void virtio_vsock_event_done(struct virtqueue
*vq
)
478 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
482 queue_work(virtio_vsock_workqueue
, &vsock
->event_work
);
485 static void virtio_vsock_tx_done(struct virtqueue
*vq
)
487 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
491 queue_work(virtio_vsock_workqueue
, &vsock
->tx_work
);
494 static void virtio_vsock_rx_done(struct virtqueue
*vq
)
496 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
500 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
503 static struct virtio_transport virtio_transport
= {
505 .get_local_cid
= virtio_transport_get_local_cid
,
507 .init
= virtio_transport_do_socket_init
,
508 .destruct
= virtio_transport_destruct
,
509 .release
= virtio_transport_release
,
510 .connect
= virtio_transport_connect
,
511 .shutdown
= virtio_transport_shutdown
,
512 .cancel_pkt
= virtio_transport_cancel_pkt
,
514 .dgram_bind
= virtio_transport_dgram_bind
,
515 .dgram_dequeue
= virtio_transport_dgram_dequeue
,
516 .dgram_enqueue
= virtio_transport_dgram_enqueue
,
517 .dgram_allow
= virtio_transport_dgram_allow
,
519 .stream_dequeue
= virtio_transport_stream_dequeue
,
520 .stream_enqueue
= virtio_transport_stream_enqueue
,
521 .stream_has_data
= virtio_transport_stream_has_data
,
522 .stream_has_space
= virtio_transport_stream_has_space
,
523 .stream_rcvhiwat
= virtio_transport_stream_rcvhiwat
,
524 .stream_is_active
= virtio_transport_stream_is_active
,
525 .stream_allow
= virtio_transport_stream_allow
,
527 .notify_poll_in
= virtio_transport_notify_poll_in
,
528 .notify_poll_out
= virtio_transport_notify_poll_out
,
529 .notify_recv_init
= virtio_transport_notify_recv_init
,
530 .notify_recv_pre_block
= virtio_transport_notify_recv_pre_block
,
531 .notify_recv_pre_dequeue
= virtio_transport_notify_recv_pre_dequeue
,
532 .notify_recv_post_dequeue
= virtio_transport_notify_recv_post_dequeue
,
533 .notify_send_init
= virtio_transport_notify_send_init
,
534 .notify_send_pre_block
= virtio_transport_notify_send_pre_block
,
535 .notify_send_pre_enqueue
= virtio_transport_notify_send_pre_enqueue
,
536 .notify_send_post_enqueue
= virtio_transport_notify_send_post_enqueue
,
538 .set_buffer_size
= virtio_transport_set_buffer_size
,
539 .set_min_buffer_size
= virtio_transport_set_min_buffer_size
,
540 .set_max_buffer_size
= virtio_transport_set_max_buffer_size
,
541 .get_buffer_size
= virtio_transport_get_buffer_size
,
542 .get_min_buffer_size
= virtio_transport_get_min_buffer_size
,
543 .get_max_buffer_size
= virtio_transport_get_max_buffer_size
,
546 .send_pkt
= virtio_transport_send_pkt
,
549 static int virtio_vsock_probe(struct virtio_device
*vdev
)
551 vq_callback_t
*callbacks
[] = {
552 virtio_vsock_rx_done
,
553 virtio_vsock_tx_done
,
554 virtio_vsock_event_done
,
556 static const char * const names
[] = {
561 struct virtio_vsock
*vsock
= NULL
;
564 ret
= mutex_lock_interruptible(&the_virtio_vsock_mutex
);
568 /* Only one virtio-vsock device per guest is supported */
569 if (the_virtio_vsock
) {
574 vsock
= kzalloc(sizeof(*vsock
), GFP_KERNEL
);
582 ret
= virtio_find_vqs(vsock
->vdev
, VSOCK_VQ_MAX
,
583 vsock
->vqs
, callbacks
, names
,
588 virtio_vsock_update_guest_cid(vsock
);
590 vsock
->rx_buf_nr
= 0;
591 vsock
->rx_buf_max_nr
= 0;
592 atomic_set(&vsock
->queued_replies
, 0);
595 the_virtio_vsock
= vsock
;
596 mutex_init(&vsock
->tx_lock
);
597 mutex_init(&vsock
->rx_lock
);
598 mutex_init(&vsock
->event_lock
);
599 spin_lock_init(&vsock
->send_pkt_list_lock
);
600 INIT_LIST_HEAD(&vsock
->send_pkt_list
);
601 spin_lock_init(&vsock
->loopback_list_lock
);
602 INIT_LIST_HEAD(&vsock
->loopback_list
);
603 INIT_WORK(&vsock
->rx_work
, virtio_transport_rx_work
);
604 INIT_WORK(&vsock
->tx_work
, virtio_transport_tx_work
);
605 INIT_WORK(&vsock
->event_work
, virtio_transport_event_work
);
606 INIT_WORK(&vsock
->send_pkt_work
, virtio_transport_send_pkt_work
);
607 INIT_WORK(&vsock
->loopback_work
, virtio_transport_loopback_work
);
609 mutex_lock(&vsock
->rx_lock
);
610 virtio_vsock_rx_fill(vsock
);
611 mutex_unlock(&vsock
->rx_lock
);
613 mutex_lock(&vsock
->event_lock
);
614 virtio_vsock_event_fill(vsock
);
615 mutex_unlock(&vsock
->event_lock
);
617 mutex_unlock(&the_virtio_vsock_mutex
);
622 mutex_unlock(&the_virtio_vsock_mutex
);
626 static void virtio_vsock_remove(struct virtio_device
*vdev
)
628 struct virtio_vsock
*vsock
= vdev
->priv
;
629 struct virtio_vsock_pkt
*pkt
;
631 flush_work(&vsock
->loopback_work
);
632 flush_work(&vsock
->rx_work
);
633 flush_work(&vsock
->tx_work
);
634 flush_work(&vsock
->event_work
);
635 flush_work(&vsock
->send_pkt_work
);
637 /* Reset all connected sockets when the device disappear */
638 vsock_for_each_connected_socket(virtio_vsock_reset_sock
);
640 vdev
->config
->reset(vdev
);
642 mutex_lock(&vsock
->rx_lock
);
643 while ((pkt
= virtqueue_detach_unused_buf(vsock
->vqs
[VSOCK_VQ_RX
])))
644 virtio_transport_free_pkt(pkt
);
645 mutex_unlock(&vsock
->rx_lock
);
647 mutex_lock(&vsock
->tx_lock
);
648 while ((pkt
= virtqueue_detach_unused_buf(vsock
->vqs
[VSOCK_VQ_TX
])))
649 virtio_transport_free_pkt(pkt
);
650 mutex_unlock(&vsock
->tx_lock
);
652 spin_lock_bh(&vsock
->send_pkt_list_lock
);
653 while (!list_empty(&vsock
->send_pkt_list
)) {
654 pkt
= list_first_entry(&vsock
->send_pkt_list
,
655 struct virtio_vsock_pkt
, list
);
656 list_del(&pkt
->list
);
657 virtio_transport_free_pkt(pkt
);
659 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
661 spin_lock_bh(&vsock
->loopback_list_lock
);
662 while (!list_empty(&vsock
->loopback_list
)) {
663 pkt
= list_first_entry(&vsock
->loopback_list
,
664 struct virtio_vsock_pkt
, list
);
665 list_del(&pkt
->list
);
666 virtio_transport_free_pkt(pkt
);
668 spin_unlock_bh(&vsock
->loopback_list_lock
);
670 mutex_lock(&the_virtio_vsock_mutex
);
671 the_virtio_vsock
= NULL
;
672 mutex_unlock(&the_virtio_vsock_mutex
);
674 vdev
->config
->del_vqs(vdev
);
679 static struct virtio_device_id id_table
[] = {
680 { VIRTIO_ID_VSOCK
, VIRTIO_DEV_ANY_ID
},
684 static unsigned int features
[] = {
687 static struct virtio_driver virtio_vsock_driver
= {
688 .feature_table
= features
,
689 .feature_table_size
= ARRAY_SIZE(features
),
690 .driver
.name
= KBUILD_MODNAME
,
691 .driver
.owner
= THIS_MODULE
,
692 .id_table
= id_table
,
693 .probe
= virtio_vsock_probe
,
694 .remove
= virtio_vsock_remove
,
697 static int __init
virtio_vsock_init(void)
701 virtio_vsock_workqueue
= alloc_workqueue("virtio_vsock", 0, 0);
702 if (!virtio_vsock_workqueue
)
705 ret
= vsock_core_init(&virtio_transport
.transport
);
709 ret
= register_virtio_driver(&virtio_vsock_driver
);
718 destroy_workqueue(virtio_vsock_workqueue
);
722 static void __exit
virtio_vsock_exit(void)
724 unregister_virtio_driver(&virtio_vsock_driver
);
726 destroy_workqueue(virtio_vsock_workqueue
);
729 module_init(virtio_vsock_init
);
730 module_exit(virtio_vsock_exit
);
731 MODULE_LICENSE("GPL v2");
732 MODULE_AUTHOR("Asias He");
733 MODULE_DESCRIPTION("virtio transport for vsock");
734 MODULE_DEVICE_TABLE(virtio
, id_table
);