2 * Sharing QEMU devices via vhost-user protocol
4 * Copyright (c) Coiby Xu <coiby.xu@gmail.com>.
5 * Copyright (c) 2020 Red Hat, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or
8 * later. See the COPYING file in the top-level directory.
10 #include "qemu/osdep.h"
11 #include "qemu/error-report.h"
12 #include "qemu/main-loop.h"
13 #include "qemu/vhost-user-server.h"
14 #include "block/aio-wait.h"
17 * Theory of operation:
19 * VuServer is started and stopped by vhost_user_server_start() and
20 * vhost_user_server_stop() from the main loop thread. Starting the server
21 * opens a vhost-user UNIX domain socket and listens for incoming connections.
22 * Only one connection is allowed at a time.
24 * The connection is handled by the vu_client_trip() coroutine in the
25 * VuServer->ctx AioContext. The coroutine consists of a vu_dispatch() loop
26 * where libvhost-user calls vu_message_read() to receive the next vhost-user
27 * protocol messages over the UNIX domain socket.
29 * When virtqueues are set up libvhost-user calls set_watch() to monitor kick
30 * fds. These fds are also handled in the VuServer->ctx AioContext.
32 * Both vu_client_trip() and kick fd monitoring can be stopped by shutting down
33 * the socket connection. Shutting down the socket connection causes
34 * vu_message_read() to fail since no more data can be received from the socket.
35 * After vu_dispatch() fails, vu_client_trip() calls vu_deinit() to stop
36 * libvhost-user before terminating the coroutine. vu_deinit() calls
37 * remove_watch() to stop monitoring kick fds and this stops virtqueue
40 * When vu_client_trip() has finished cleaning up it schedules a BH in the main
41 * loop thread to accept the next client connection.
43 * When libvhost-user detects an error it calls panic_cb() and sets the
44 * dev->broken flag. Both vu_client_trip() and kick fd processing stop when
45 * the dev->broken flag is set.
47 * It is possible to switch AioContexts using
48 * vhost_user_server_detach_aio_context() and
49 * vhost_user_server_attach_aio_context(). They stop monitoring fds in the old
50 * AioContext and resume monitoring in the new AioContext. The vu_client_trip()
51 * coroutine remains in a yielded state during the switch. This is made
52 * possible by QIOChannel's support for spurious coroutine re-entry in
53 * qio_channel_yield(). The coroutine will restart I/O when re-entered from the
57 static void vmsg_close_fds(VhostUserMsg
*vmsg
)
60 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
65 static void vmsg_unblock_fds(VhostUserMsg
*vmsg
)
68 for (i
= 0; i
< vmsg
->fd_num
; i
++) {
69 qemu_socket_set_nonblock(vmsg
->fds
[i
]);
73 static void panic_cb(VuDev
*vu_dev
, const char *buf
)
75 error_report("vu_panic: %s", buf
);
78 void vhost_user_server_ref(VuServer
*server
)
80 assert(!server
->wait_idle
);
84 void vhost_user_server_unref(VuServer
*server
)
87 if (server
->wait_idle
&& !server
->refcount
) {
88 aio_co_wake(server
->co_trip
);
92 static bool coroutine_fn
93 vu_message_read(VuDev
*vu_dev
, int conn_fd
, VhostUserMsg
*vmsg
)
96 .iov_base
= (char *)vmsg
,
97 .iov_len
= VHOST_USER_HDR_SIZE
,
99 int rc
, read_bytes
= 0;
100 Error
*local_err
= NULL
;
101 const size_t max_fds
= G_N_ELEMENTS(vmsg
->fds
);
102 VuServer
*server
= container_of(vu_dev
, VuServer
, vu_dev
);
103 QIOChannel
*ioc
= server
->ioc
;
107 error_report_err(local_err
);
111 assert(qemu_in_coroutine());
117 * qio_channel_readv_full may have short reads, keeping calling it
118 * until getting VHOST_USER_HDR_SIZE or 0 bytes in total
120 rc
= qio_channel_readv_full(ioc
, &iov
, 1, &fds
, &nfds
, 0, &local_err
);
122 if (rc
== QIO_CHANNEL_ERR_BLOCK
) {
123 assert(local_err
== NULL
);
124 qio_channel_yield(ioc
, G_IO_IN
);
127 error_report_err(local_err
);
133 if (vmsg
->fd_num
+ nfds
> max_fds
) {
134 error_report("A maximum of %zu fds are allowed, "
135 "however got %zu fds now",
136 max_fds
, vmsg
->fd_num
+ nfds
);
140 memcpy(vmsg
->fds
+ vmsg
->fd_num
, fds
, nfds
* sizeof(vmsg
->fds
[0]));
141 vmsg
->fd_num
+= nfds
;
145 if (rc
== 0) { /* socket closed */
152 } while (read_bytes
!= VHOST_USER_HDR_SIZE
);
154 /* qio_channel_readv_full will make socket fds blocking, unblock them */
155 vmsg_unblock_fds(vmsg
);
156 if (vmsg
->size
> sizeof(vmsg
->payload
)) {
157 error_report("Error: too big message request: %d, "
158 "size: vmsg->size: %u, "
159 "while sizeof(vmsg->payload) = %zu",
160 vmsg
->request
, vmsg
->size
, sizeof(vmsg
->payload
));
164 struct iovec iov_payload
= {
165 .iov_base
= (char *)&vmsg
->payload
,
166 .iov_len
= vmsg
->size
,
169 rc
= qio_channel_readv_all_eof(ioc
, &iov_payload
, 1, &local_err
);
172 error_report_err(local_err
);
181 vmsg_close_fds(vmsg
);
186 static coroutine_fn
void vu_client_trip(void *opaque
)
188 VuServer
*server
= opaque
;
189 VuDev
*vu_dev
= &server
->vu_dev
;
191 while (!vu_dev
->broken
&& vu_dispatch(vu_dev
)) {
195 if (server
->refcount
) {
196 /* Wait for requests to complete before we can unmap the memory */
197 server
->wait_idle
= true;
198 qemu_coroutine_yield();
199 server
->wait_idle
= false;
201 assert(server
->refcount
== 0);
205 /* vu_deinit() should have called remove_watch() */
206 assert(QTAILQ_EMPTY(&server
->vu_fd_watches
));
208 object_unref(OBJECT(server
->sioc
));
211 object_unref(OBJECT(server
->ioc
));
214 server
->co_trip
= NULL
;
215 if (server
->restart_listener_bh
) {
216 qemu_bh_schedule(server
->restart_listener_bh
);
222 * a wrapper for vu_kick_cb
224 * since aio_dispatch can only pass one user data pointer to the
225 * callback function, pack VuDev and pvt into a struct. Then unpack it
226 * and pass them to vu_kick_cb
228 static void kick_handler(void *opaque
)
230 VuFdWatch
*vu_fd_watch
= opaque
;
231 VuDev
*vu_dev
= vu_fd_watch
->vu_dev
;
233 vu_fd_watch
->cb(vu_dev
, 0, vu_fd_watch
->pvt
);
235 /* Stop vu_client_trip() if an error occurred in vu_fd_watch->cb() */
236 if (vu_dev
->broken
) {
237 VuServer
*server
= container_of(vu_dev
, VuServer
, vu_dev
);
239 qio_channel_shutdown(server
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
243 static VuFdWatch
*find_vu_fd_watch(VuServer
*server
, int fd
)
246 VuFdWatch
*vu_fd_watch
, *next
;
247 QTAILQ_FOREACH_SAFE(vu_fd_watch
, &server
->vu_fd_watches
, next
, next
) {
248 if (vu_fd_watch
->fd
== fd
) {
256 set_watch(VuDev
*vu_dev
, int fd
, int vu_evt
,
257 vu_watch_cb cb
, void *pvt
)
260 VuServer
*server
= container_of(vu_dev
, VuServer
, vu_dev
);
265 VuFdWatch
*vu_fd_watch
= find_vu_fd_watch(server
, fd
);
268 VuFdWatch
*vu_fd_watch
= g_new0(VuFdWatch
, 1);
270 QTAILQ_INSERT_TAIL(&server
->vu_fd_watches
, vu_fd_watch
, next
);
272 vu_fd_watch
->fd
= fd
;
273 vu_fd_watch
->cb
= cb
;
274 qemu_socket_set_nonblock(fd
);
275 aio_set_fd_handler(server
->ioc
->ctx
, fd
, true, kick_handler
,
276 NULL
, NULL
, NULL
, vu_fd_watch
);
277 vu_fd_watch
->vu_dev
= vu_dev
;
278 vu_fd_watch
->pvt
= pvt
;
283 static void remove_watch(VuDev
*vu_dev
, int fd
)
289 server
= container_of(vu_dev
, VuServer
, vu_dev
);
291 VuFdWatch
*vu_fd_watch
= find_vu_fd_watch(server
, fd
);
296 aio_set_fd_handler(server
->ioc
->ctx
, fd
, true,
297 NULL
, NULL
, NULL
, NULL
, NULL
);
299 QTAILQ_REMOVE(&server
->vu_fd_watches
, vu_fd_watch
, next
);
304 static void vu_accept(QIONetListener
*listener
, QIOChannelSocket
*sioc
,
307 VuServer
*server
= opaque
;
310 warn_report("Only one vhost-user client is allowed to "
311 "connect the server one time");
315 if (!vu_init(&server
->vu_dev
, server
->max_queues
, sioc
->fd
, panic_cb
,
316 vu_message_read
, set_watch
, remove_watch
, server
->vu_iface
)) {
317 error_report("Failed to initialize libvhost-user");
322 * Unset the callback function for network listener to make another
323 * vhost-user client keeping waiting until this client disconnects
325 qio_net_listener_set_client_func(server
->listener
,
331 * Increase the object reference, so sioc will not freed by
332 * qio_net_listener_channel_func which will call object_unref(OBJECT(sioc))
334 object_ref(OBJECT(server
->sioc
));
335 qio_channel_set_name(QIO_CHANNEL(sioc
), "vhost-user client");
336 server
->ioc
= QIO_CHANNEL(sioc
);
337 object_ref(OBJECT(server
->ioc
));
339 /* TODO vu_message_write() spins if non-blocking! */
340 qio_channel_set_blocking(server
->ioc
, false, NULL
);
342 server
->co_trip
= qemu_coroutine_create(vu_client_trip
, server
);
344 aio_context_acquire(server
->ctx
);
345 vhost_user_server_attach_aio_context(server
, server
->ctx
);
346 aio_context_release(server
->ctx
);
349 /* server->ctx acquired by caller */
350 void vhost_user_server_stop(VuServer
*server
)
352 qemu_bh_delete(server
->restart_listener_bh
);
353 server
->restart_listener_bh
= NULL
;
356 VuFdWatch
*vu_fd_watch
;
358 QTAILQ_FOREACH(vu_fd_watch
, &server
->vu_fd_watches
, next
) {
359 aio_set_fd_handler(server
->ctx
, vu_fd_watch
->fd
, true,
360 NULL
, NULL
, NULL
, NULL
, vu_fd_watch
);
363 qio_channel_shutdown(server
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
365 AIO_WAIT_WHILE(server
->ctx
, server
->co_trip
);
368 if (server
->listener
) {
369 qio_net_listener_disconnect(server
->listener
);
370 object_unref(OBJECT(server
->listener
));
375 * Allow the next client to connect to the server. Called from a BH in the main
378 static void restart_listener_bh(void *opaque
)
380 VuServer
*server
= opaque
;
382 qio_net_listener_set_client_func(server
->listener
, vu_accept
, server
,
386 /* Called with ctx acquired */
387 void vhost_user_server_attach_aio_context(VuServer
*server
, AioContext
*ctx
)
389 VuFdWatch
*vu_fd_watch
;
397 qio_channel_attach_aio_context(server
->ioc
, ctx
);
399 QTAILQ_FOREACH(vu_fd_watch
, &server
->vu_fd_watches
, next
) {
400 aio_set_fd_handler(ctx
, vu_fd_watch
->fd
, true, kick_handler
, NULL
,
401 NULL
, NULL
, vu_fd_watch
);
404 aio_co_schedule(ctx
, server
->co_trip
);
407 /* Called with server->ctx acquired */
408 void vhost_user_server_detach_aio_context(VuServer
*server
)
411 VuFdWatch
*vu_fd_watch
;
413 QTAILQ_FOREACH(vu_fd_watch
, &server
->vu_fd_watches
, next
) {
414 aio_set_fd_handler(server
->ctx
, vu_fd_watch
->fd
, true,
415 NULL
, NULL
, NULL
, NULL
, vu_fd_watch
);
418 qio_channel_detach_aio_context(server
->ioc
);
424 bool vhost_user_server_start(VuServer
*server
,
425 SocketAddress
*socket_addr
,
428 const VuDevIface
*vu_iface
,
432 QIONetListener
*listener
;
434 if (socket_addr
->type
!= SOCKET_ADDRESS_TYPE_UNIX
&&
435 socket_addr
->type
!= SOCKET_ADDRESS_TYPE_FD
) {
436 error_setg(errp
, "Only socket address types 'unix' and 'fd' are supported");
440 listener
= qio_net_listener_new();
441 if (qio_net_listener_open_sync(listener
, socket_addr
, 1,
443 object_unref(OBJECT(listener
));
447 bh
= qemu_bh_new(restart_listener_bh
, server
);
449 /* zero out unspecified fields */
450 *server
= (VuServer
) {
451 .listener
= listener
,
452 .restart_listener_bh
= bh
,
453 .vu_iface
= vu_iface
,
454 .max_queues
= max_queues
,
458 qio_net_listener_set_name(server
->listener
, "vhost-user-backend-listener");
460 qio_net_listener_set_client_func(server
->listener
,
465 QTAILQ_INIT(&server
->vu_fd_watches
);