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 atomic_t queued_replies
;
49 /* The following fields are protected by rx_lock. vqs[VSOCK_VQ_RX]
50 * must be accessed with rx_lock held.
56 /* The following fields are protected by event_lock.
57 * vqs[VSOCK_VQ_EVENT] must be accessed with event_lock held.
59 struct mutex event_lock
;
60 struct virtio_vsock_event event_list
[8];
65 static struct virtio_vsock
*virtio_vsock_get(void)
67 return the_virtio_vsock
;
70 static u32
virtio_transport_get_local_cid(void)
72 struct virtio_vsock
*vsock
= virtio_vsock_get();
75 return VMADDR_CID_ANY
;
77 return vsock
->guest_cid
;
81 virtio_transport_send_pkt_work(struct work_struct
*work
)
83 struct virtio_vsock
*vsock
=
84 container_of(work
, struct virtio_vsock
, send_pkt_work
);
87 bool restart_rx
= false;
89 mutex_lock(&vsock
->tx_lock
);
91 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
94 struct virtio_vsock_pkt
*pkt
;
95 struct scatterlist hdr
, buf
, *sgs
[2];
96 int ret
, in_sg
= 0, out_sg
= 0;
99 spin_lock_bh(&vsock
->send_pkt_list_lock
);
100 if (list_empty(&vsock
->send_pkt_list
)) {
101 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
105 pkt
= list_first_entry(&vsock
->send_pkt_list
,
106 struct virtio_vsock_pkt
, list
);
107 list_del_init(&pkt
->list
);
108 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
112 sg_init_one(&hdr
, &pkt
->hdr
, sizeof(pkt
->hdr
));
113 sgs
[out_sg
++] = &hdr
;
115 sg_init_one(&buf
, pkt
->buf
, pkt
->len
);
116 sgs
[out_sg
++] = &buf
;
119 ret
= virtqueue_add_sgs(vq
, sgs
, out_sg
, in_sg
, pkt
, GFP_KERNEL
);
120 /* Usually this means that there is no more space available in
124 spin_lock_bh(&vsock
->send_pkt_list_lock
);
125 list_add(&pkt
->list
, &vsock
->send_pkt_list
);
126 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
131 struct virtqueue
*rx_vq
= vsock
->vqs
[VSOCK_VQ_RX
];
134 val
= atomic_dec_return(&vsock
->queued_replies
);
136 /* Do we now have resources to resume rx processing? */
137 if (val
+ 1 == virtqueue_get_vring_size(rx_vq
))
147 mutex_unlock(&vsock
->tx_lock
);
150 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
154 virtio_transport_send_pkt(struct virtio_vsock_pkt
*pkt
)
156 struct virtio_vsock
*vsock
;
159 vsock
= virtio_vsock_get();
161 virtio_transport_free_pkt(pkt
);
166 atomic_inc(&vsock
->queued_replies
);
168 spin_lock_bh(&vsock
->send_pkt_list_lock
);
169 list_add_tail(&pkt
->list
, &vsock
->send_pkt_list
);
170 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
172 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
176 static void virtio_vsock_rx_fill(struct virtio_vsock
*vsock
)
178 int buf_len
= VIRTIO_VSOCK_DEFAULT_RX_BUF_SIZE
;
179 struct virtio_vsock_pkt
*pkt
;
180 struct scatterlist hdr
, buf
, *sgs
[2];
181 struct virtqueue
*vq
;
184 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
187 pkt
= kzalloc(sizeof(*pkt
), GFP_KERNEL
);
191 pkt
->buf
= kmalloc(buf_len
, GFP_KERNEL
);
193 virtio_transport_free_pkt(pkt
);
199 sg_init_one(&hdr
, &pkt
->hdr
, sizeof(pkt
->hdr
));
202 sg_init_one(&buf
, pkt
->buf
, buf_len
);
204 ret
= virtqueue_add_sgs(vq
, sgs
, 0, 2, pkt
, GFP_KERNEL
);
206 virtio_transport_free_pkt(pkt
);
210 } while (vq
->num_free
);
211 if (vsock
->rx_buf_nr
> vsock
->rx_buf_max_nr
)
212 vsock
->rx_buf_max_nr
= vsock
->rx_buf_nr
;
216 static void virtio_transport_tx_work(struct work_struct
*work
)
218 struct virtio_vsock
*vsock
=
219 container_of(work
, struct virtio_vsock
, tx_work
);
220 struct virtqueue
*vq
;
223 vq
= vsock
->vqs
[VSOCK_VQ_TX
];
224 mutex_lock(&vsock
->tx_lock
);
226 struct virtio_vsock_pkt
*pkt
;
229 virtqueue_disable_cb(vq
);
230 while ((pkt
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
231 virtio_transport_free_pkt(pkt
);
234 } while (!virtqueue_enable_cb(vq
));
235 mutex_unlock(&vsock
->tx_lock
);
238 queue_work(virtio_vsock_workqueue
, &vsock
->send_pkt_work
);
241 /* Is there space left for replies to rx packets? */
242 static bool virtio_transport_more_replies(struct virtio_vsock
*vsock
)
244 struct virtqueue
*vq
= vsock
->vqs
[VSOCK_VQ_RX
];
247 smp_rmb(); /* paired with atomic_inc() and atomic_dec_return() */
248 val
= atomic_read(&vsock
->queued_replies
);
250 return val
< virtqueue_get_vring_size(vq
);
253 static void virtio_transport_rx_work(struct work_struct
*work
)
255 struct virtio_vsock
*vsock
=
256 container_of(work
, struct virtio_vsock
, rx_work
);
257 struct virtqueue
*vq
;
259 vq
= vsock
->vqs
[VSOCK_VQ_RX
];
261 mutex_lock(&vsock
->rx_lock
);
264 virtqueue_disable_cb(vq
);
266 struct virtio_vsock_pkt
*pkt
;
269 if (!virtio_transport_more_replies(vsock
)) {
270 /* Stop rx until the device processes already
271 * pending replies. Leave rx virtqueue
272 * callbacks disabled.
277 pkt
= virtqueue_get_buf(vq
, &len
);
284 /* Drop short/long packets */
285 if (unlikely(len
< sizeof(pkt
->hdr
) ||
286 len
> sizeof(pkt
->hdr
) + pkt
->len
)) {
287 virtio_transport_free_pkt(pkt
);
291 pkt
->len
= len
- sizeof(pkt
->hdr
);
292 virtio_transport_recv_pkt(pkt
);
294 } while (!virtqueue_enable_cb(vq
));
297 if (vsock
->rx_buf_nr
< vsock
->rx_buf_max_nr
/ 2)
298 virtio_vsock_rx_fill(vsock
);
299 mutex_unlock(&vsock
->rx_lock
);
302 /* event_lock must be held */
303 static int virtio_vsock_event_fill_one(struct virtio_vsock
*vsock
,
304 struct virtio_vsock_event
*event
)
306 struct scatterlist sg
;
307 struct virtqueue
*vq
;
309 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
311 sg_init_one(&sg
, event
, sizeof(*event
));
313 return virtqueue_add_inbuf(vq
, &sg
, 1, event
, GFP_KERNEL
);
316 /* event_lock must be held */
317 static void virtio_vsock_event_fill(struct virtio_vsock
*vsock
)
321 for (i
= 0; i
< ARRAY_SIZE(vsock
->event_list
); i
++) {
322 struct virtio_vsock_event
*event
= &vsock
->event_list
[i
];
324 virtio_vsock_event_fill_one(vsock
, event
);
327 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
330 static void virtio_vsock_reset_sock(struct sock
*sk
)
333 sk
->sk_state
= SS_UNCONNECTED
;
334 sk
->sk_err
= ECONNRESET
;
335 sk
->sk_error_report(sk
);
339 static void virtio_vsock_update_guest_cid(struct virtio_vsock
*vsock
)
341 struct virtio_device
*vdev
= vsock
->vdev
;
344 vdev
->config
->get(vdev
, offsetof(struct virtio_vsock_config
, guest_cid
),
345 &guest_cid
, sizeof(guest_cid
));
346 vsock
->guest_cid
= le64_to_cpu(guest_cid
);
349 /* event_lock must be held */
350 static void virtio_vsock_event_handle(struct virtio_vsock
*vsock
,
351 struct virtio_vsock_event
*event
)
353 switch (le32_to_cpu(event
->id
)) {
354 case VIRTIO_VSOCK_EVENT_TRANSPORT_RESET
:
355 virtio_vsock_update_guest_cid(vsock
);
356 vsock_for_each_connected_socket(virtio_vsock_reset_sock
);
361 static void virtio_transport_event_work(struct work_struct
*work
)
363 struct virtio_vsock
*vsock
=
364 container_of(work
, struct virtio_vsock
, event_work
);
365 struct virtqueue
*vq
;
367 vq
= vsock
->vqs
[VSOCK_VQ_EVENT
];
369 mutex_lock(&vsock
->event_lock
);
372 struct virtio_vsock_event
*event
;
375 virtqueue_disable_cb(vq
);
376 while ((event
= virtqueue_get_buf(vq
, &len
)) != NULL
) {
377 if (len
== sizeof(*event
))
378 virtio_vsock_event_handle(vsock
, event
);
380 virtio_vsock_event_fill_one(vsock
, event
);
382 } while (!virtqueue_enable_cb(vq
));
384 virtqueue_kick(vsock
->vqs
[VSOCK_VQ_EVENT
]);
386 mutex_unlock(&vsock
->event_lock
);
389 static void virtio_vsock_event_done(struct virtqueue
*vq
)
391 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
395 queue_work(virtio_vsock_workqueue
, &vsock
->event_work
);
398 static void virtio_vsock_tx_done(struct virtqueue
*vq
)
400 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
404 queue_work(virtio_vsock_workqueue
, &vsock
->tx_work
);
407 static void virtio_vsock_rx_done(struct virtqueue
*vq
)
409 struct virtio_vsock
*vsock
= vq
->vdev
->priv
;
413 queue_work(virtio_vsock_workqueue
, &vsock
->rx_work
);
416 static struct virtio_transport virtio_transport
= {
418 .get_local_cid
= virtio_transport_get_local_cid
,
420 .init
= virtio_transport_do_socket_init
,
421 .destruct
= virtio_transport_destruct
,
422 .release
= virtio_transport_release
,
423 .connect
= virtio_transport_connect
,
424 .shutdown
= virtio_transport_shutdown
,
426 .dgram_bind
= virtio_transport_dgram_bind
,
427 .dgram_dequeue
= virtio_transport_dgram_dequeue
,
428 .dgram_enqueue
= virtio_transport_dgram_enqueue
,
429 .dgram_allow
= virtio_transport_dgram_allow
,
431 .stream_dequeue
= virtio_transport_stream_dequeue
,
432 .stream_enqueue
= virtio_transport_stream_enqueue
,
433 .stream_has_data
= virtio_transport_stream_has_data
,
434 .stream_has_space
= virtio_transport_stream_has_space
,
435 .stream_rcvhiwat
= virtio_transport_stream_rcvhiwat
,
436 .stream_is_active
= virtio_transport_stream_is_active
,
437 .stream_allow
= virtio_transport_stream_allow
,
439 .notify_poll_in
= virtio_transport_notify_poll_in
,
440 .notify_poll_out
= virtio_transport_notify_poll_out
,
441 .notify_recv_init
= virtio_transport_notify_recv_init
,
442 .notify_recv_pre_block
= virtio_transport_notify_recv_pre_block
,
443 .notify_recv_pre_dequeue
= virtio_transport_notify_recv_pre_dequeue
,
444 .notify_recv_post_dequeue
= virtio_transport_notify_recv_post_dequeue
,
445 .notify_send_init
= virtio_transport_notify_send_init
,
446 .notify_send_pre_block
= virtio_transport_notify_send_pre_block
,
447 .notify_send_pre_enqueue
= virtio_transport_notify_send_pre_enqueue
,
448 .notify_send_post_enqueue
= virtio_transport_notify_send_post_enqueue
,
450 .set_buffer_size
= virtio_transport_set_buffer_size
,
451 .set_min_buffer_size
= virtio_transport_set_min_buffer_size
,
452 .set_max_buffer_size
= virtio_transport_set_max_buffer_size
,
453 .get_buffer_size
= virtio_transport_get_buffer_size
,
454 .get_min_buffer_size
= virtio_transport_get_min_buffer_size
,
455 .get_max_buffer_size
= virtio_transport_get_max_buffer_size
,
458 .send_pkt
= virtio_transport_send_pkt
,
461 static int virtio_vsock_probe(struct virtio_device
*vdev
)
463 vq_callback_t
*callbacks
[] = {
464 virtio_vsock_rx_done
,
465 virtio_vsock_tx_done
,
466 virtio_vsock_event_done
,
468 static const char * const names
[] = {
473 struct virtio_vsock
*vsock
= NULL
;
476 ret
= mutex_lock_interruptible(&the_virtio_vsock_mutex
);
480 /* Only one virtio-vsock device per guest is supported */
481 if (the_virtio_vsock
) {
486 vsock
= kzalloc(sizeof(*vsock
), GFP_KERNEL
);
494 ret
= vsock
->vdev
->config
->find_vqs(vsock
->vdev
, VSOCK_VQ_MAX
,
495 vsock
->vqs
, callbacks
, names
);
499 virtio_vsock_update_guest_cid(vsock
);
501 vsock
->rx_buf_nr
= 0;
502 vsock
->rx_buf_max_nr
= 0;
503 atomic_set(&vsock
->queued_replies
, 0);
506 the_virtio_vsock
= vsock
;
507 mutex_init(&vsock
->tx_lock
);
508 mutex_init(&vsock
->rx_lock
);
509 mutex_init(&vsock
->event_lock
);
510 spin_lock_init(&vsock
->send_pkt_list_lock
);
511 INIT_LIST_HEAD(&vsock
->send_pkt_list
);
512 INIT_WORK(&vsock
->rx_work
, virtio_transport_rx_work
);
513 INIT_WORK(&vsock
->tx_work
, virtio_transport_tx_work
);
514 INIT_WORK(&vsock
->event_work
, virtio_transport_event_work
);
515 INIT_WORK(&vsock
->send_pkt_work
, virtio_transport_send_pkt_work
);
517 mutex_lock(&vsock
->rx_lock
);
518 virtio_vsock_rx_fill(vsock
);
519 mutex_unlock(&vsock
->rx_lock
);
521 mutex_lock(&vsock
->event_lock
);
522 virtio_vsock_event_fill(vsock
);
523 mutex_unlock(&vsock
->event_lock
);
525 mutex_unlock(&the_virtio_vsock_mutex
);
530 mutex_unlock(&the_virtio_vsock_mutex
);
534 static void virtio_vsock_remove(struct virtio_device
*vdev
)
536 struct virtio_vsock
*vsock
= vdev
->priv
;
537 struct virtio_vsock_pkt
*pkt
;
539 flush_work(&vsock
->rx_work
);
540 flush_work(&vsock
->tx_work
);
541 flush_work(&vsock
->event_work
);
542 flush_work(&vsock
->send_pkt_work
);
544 /* Reset all connected sockets when the device disappear */
545 vsock_for_each_connected_socket(virtio_vsock_reset_sock
);
547 vdev
->config
->reset(vdev
);
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
);
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
);
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
);
566 spin_unlock_bh(&vsock
->send_pkt_list_lock
);
568 mutex_lock(&the_virtio_vsock_mutex
);
569 the_virtio_vsock
= NULL
;
570 mutex_unlock(&the_virtio_vsock_mutex
);
572 vdev
->config
->del_vqs(vdev
);
577 static struct virtio_device_id id_table
[] = {
578 { VIRTIO_ID_VSOCK
, VIRTIO_DEV_ANY_ID
},
582 static unsigned int features
[] = {
585 static struct virtio_driver virtio_vsock_driver
= {
586 .feature_table
= features
,
587 .feature_table_size
= ARRAY_SIZE(features
),
588 .driver
.name
= KBUILD_MODNAME
,
589 .driver
.owner
= THIS_MODULE
,
590 .id_table
= id_table
,
591 .probe
= virtio_vsock_probe
,
592 .remove
= virtio_vsock_remove
,
595 static int __init
virtio_vsock_init(void)
599 virtio_vsock_workqueue
= alloc_workqueue("virtio_vsock", 0, 0);
600 if (!virtio_vsock_workqueue
)
603 ret
= register_virtio_driver(&virtio_vsock_driver
);
607 ret
= vsock_core_init(&virtio_transport
.transport
);
614 unregister_virtio_driver(&virtio_vsock_driver
);
616 destroy_workqueue(virtio_vsock_workqueue
);
621 static void __exit
virtio_vsock_exit(void)
624 unregister_virtio_driver(&virtio_vsock_driver
);
625 destroy_workqueue(virtio_vsock_workqueue
);
628 module_init(virtio_vsock_init
);
629 module_exit(virtio_vsock_exit
);
630 MODULE_LICENSE("GPL v2");
631 MODULE_AUTHOR("Asias He");
632 MODULE_DESCRIPTION("virtio transport for vsock");
633 MODULE_DEVICE_TABLE(virtio
, id_table
);