blob: 5ccc6d24a0c05ffdf7c18769c2fe1cbef4d4f6b7 [file] [log] [blame]
Coiby Xu70eb2c02020-09-18 16:09:08 +08001/*
2 * Sharing QEMU devices via vhost-user protocol
3 *
4 * Copyright (c) Coiby Xu <coiby.xu@gmail.com>.
5 * Copyright (c) 2020 Red Hat, Inc.
6 *
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.
9 */
10#include "qemu/osdep.h"
Thomas Huth5feed382023-02-10 12:19:31 +010011#include "qemu/error-report.h"
Coiby Xu70eb2c02020-09-18 16:09:08 +080012#include "qemu/main-loop.h"
Stefan Hajnoczi80a06cc2020-09-24 16:15:48 +010013#include "qemu/vhost-user-server.h"
Stefan Hajnoczi7185c852020-09-24 16:15:45 +010014#include "block/aio-wait.h"
Coiby Xu70eb2c02020-09-18 16:09:08 +080015
Stefan Hajnoczi7185c852020-09-24 16:15:45 +010016/*
17 * Theory of operation:
18 *
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.
23 *
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.
28 *
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.
31 *
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
38 * processing.
39 *
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.
42 *
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.
46 *
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
54 * new AioContext.
55 */
56
Coiby Xu70eb2c02020-09-18 16:09:08 +080057static void vmsg_close_fds(VhostUserMsg *vmsg)
58{
59 int i;
60 for (i = 0; i < vmsg->fd_num; i++) {
61 close(vmsg->fds[i]);
62 }
63}
64
65static void vmsg_unblock_fds(VhostUserMsg *vmsg)
66{
67 int i;
68 for (i = 0; i < vmsg->fd_num; i++) {
Marc-André Lureauff5927b2022-04-25 17:33:47 +040069 qemu_socket_set_nonblock(vmsg->fds[i]);
Coiby Xu70eb2c02020-09-18 16:09:08 +080070 }
71}
72
Coiby Xu70eb2c02020-09-18 16:09:08 +080073static void panic_cb(VuDev *vu_dev, const char *buf)
74{
Stefan Hajnoczi7185c852020-09-24 16:15:45 +010075 error_report("vu_panic: %s", buf);
Coiby Xu70eb2c02020-09-18 16:09:08 +080076}
77
Stefan Hajnoczi75d33e82023-05-16 15:02:23 -040078void vhost_user_server_inc_in_flight(VuServer *server)
Kevin Wolf520d8b42022-01-25 16:14:35 +010079{
80 assert(!server->wait_idle);
Stefan Hajnoczi8f5e9a82023-05-16 15:02:24 -040081 qatomic_inc(&server->in_flight);
Kevin Wolf520d8b42022-01-25 16:14:35 +010082}
83
Stefan Hajnoczi75d33e82023-05-16 15:02:23 -040084void vhost_user_server_dec_in_flight(VuServer *server)
Kevin Wolf520d8b42022-01-25 16:14:35 +010085{
Stefan Hajnoczi8f5e9a82023-05-16 15:02:24 -040086 if (qatomic_fetch_dec(&server->in_flight) == 1) {
87 if (server->wait_idle) {
88 aio_co_wake(server->co_trip);
89 }
Kevin Wolf520d8b42022-01-25 16:14:35 +010090 }
91}
92
Stefan Hajnoczi8f5e9a82023-05-16 15:02:24 -040093bool vhost_user_server_has_in_flight(VuServer *server)
94{
95 return qatomic_load_acquire(&server->in_flight) > 0;
96}
97
Coiby Xu70eb2c02020-09-18 16:09:08 +080098static bool coroutine_fn
99vu_message_read(VuDev *vu_dev, int conn_fd, VhostUserMsg *vmsg)
100{
101 struct iovec iov = {
102 .iov_base = (char *)vmsg,
103 .iov_len = VHOST_USER_HDR_SIZE,
104 };
105 int rc, read_bytes = 0;
106 Error *local_err = NULL;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800107 const size_t max_fds = G_N_ELEMENTS(vmsg->fds);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800108 VuServer *server = container_of(vu_dev, VuServer, vu_dev);
109 QIOChannel *ioc = server->ioc;
110
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100111 vmsg->fd_num = 0;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800112 if (!ioc) {
113 error_report_err(local_err);
114 goto fail;
115 }
116
117 assert(qemu_in_coroutine());
118 do {
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100119 size_t nfds = 0;
120 int *fds = NULL;
121
Coiby Xu70eb2c02020-09-18 16:09:08 +0800122 /*
123 * qio_channel_readv_full may have short reads, keeping calling it
124 * until getting VHOST_USER_HDR_SIZE or 0 bytes in total
125 */
manish.mishra84615a12022-12-20 18:44:17 +0000126 rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, 0, &local_err);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800127 if (rc < 0) {
128 if (rc == QIO_CHANNEL_ERR_BLOCK) {
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100129 assert(local_err == NULL);
Stefan Hajnoczi06e0f092023-08-30 18:48:02 -0400130 if (server->ctx) {
131 server->in_qio_channel_yield = true;
132 qio_channel_yield(ioc, G_IO_IN);
133 server->in_qio_channel_yield = false;
134 } else {
135 /* Wait until attached to an AioContext again */
136 qemu_coroutine_yield();
137 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800138 continue;
139 } else {
140 error_report_err(local_err);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800141 goto fail;
142 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800143 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800144
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100145 if (nfds > 0) {
146 if (vmsg->fd_num + nfds > max_fds) {
147 error_report("A maximum of %zu fds are allowed, "
148 "however got %zu fds now",
149 max_fds, vmsg->fd_num + nfds);
150 g_free(fds);
151 goto fail;
152 }
153 memcpy(vmsg->fds + vmsg->fd_num, fds, nfds * sizeof(vmsg->fds[0]));
154 vmsg->fd_num += nfds;
155 g_free(fds);
156 }
157
158 if (rc == 0) { /* socket closed */
159 goto fail;
160 }
161
162 iov.iov_base += rc;
163 iov.iov_len -= rc;
164 read_bytes += rc;
165 } while (read_bytes != VHOST_USER_HDR_SIZE);
166
Coiby Xu70eb2c02020-09-18 16:09:08 +0800167 /* qio_channel_readv_full will make socket fds blocking, unblock them */
168 vmsg_unblock_fds(vmsg);
169 if (vmsg->size > sizeof(vmsg->payload)) {
170 error_report("Error: too big message request: %d, "
171 "size: vmsg->size: %u, "
172 "while sizeof(vmsg->payload) = %zu",
173 vmsg->request, vmsg->size, sizeof(vmsg->payload));
174 goto fail;
175 }
176
177 struct iovec iov_payload = {
178 .iov_base = (char *)&vmsg->payload,
179 .iov_len = vmsg->size,
180 };
181 if (vmsg->size) {
182 rc = qio_channel_readv_all_eof(ioc, &iov_payload, 1, &local_err);
Stefan Hajnocziedaf6202020-09-24 16:15:44 +0100183 if (rc != 1) {
184 if (local_err) {
185 error_report_err(local_err);
186 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800187 goto fail;
188 }
189 }
190
191 return true;
192
193fail:
194 vmsg_close_fds(vmsg);
195
196 return false;
197}
198
Coiby Xu70eb2c02020-09-18 16:09:08 +0800199static coroutine_fn void vu_client_trip(void *opaque)
200{
201 VuServer *server = opaque;
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100202 VuDev *vu_dev = &server->vu_dev;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800203
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100204 while (!vu_dev->broken && vu_dispatch(vu_dev)) {
205 /* Keep running */
Coiby Xu70eb2c02020-09-18 16:09:08 +0800206 }
207
Stefan Hajnoczi8f5e9a82023-05-16 15:02:24 -0400208 if (vhost_user_server_has_in_flight(server)) {
Kevin Wolf520d8b42022-01-25 16:14:35 +0100209 /* Wait for requests to complete before we can unmap the memory */
210 server->wait_idle = true;
211 qemu_coroutine_yield();
212 server->wait_idle = false;
213 }
Stefan Hajnoczi8f5e9a82023-05-16 15:02:24 -0400214 assert(!vhost_user_server_has_in_flight(server));
Kevin Wolf520d8b42022-01-25 16:14:35 +0100215
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100216 vu_deinit(vu_dev);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800217
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100218 /* vu_deinit() should have called remove_watch() */
219 assert(QTAILQ_EMPTY(&server->vu_fd_watches));
220
221 object_unref(OBJECT(server->sioc));
222 server->sioc = NULL;
223
224 object_unref(OBJECT(server->ioc));
225 server->ioc = NULL;
226
227 server->co_trip = NULL;
228 if (server->restart_listener_bh) {
229 qemu_bh_schedule(server->restart_listener_bh);
230 }
231 aio_wait_kick();
Coiby Xu70eb2c02020-09-18 16:09:08 +0800232}
233
234/*
235 * a wrapper for vu_kick_cb
236 *
237 * since aio_dispatch can only pass one user data pointer to the
238 * callback function, pack VuDev and pvt into a struct. Then unpack it
239 * and pass them to vu_kick_cb
240 */
241static void kick_handler(void *opaque)
242{
243 VuFdWatch *vu_fd_watch = opaque;
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100244 VuDev *vu_dev = vu_fd_watch->vu_dev;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800245
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100246 vu_fd_watch->cb(vu_dev, 0, vu_fd_watch->pvt);
247
248 /* Stop vu_client_trip() if an error occurred in vu_fd_watch->cb() */
249 if (vu_dev->broken) {
250 VuServer *server = container_of(vu_dev, VuServer, vu_dev);
251
252 qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
253 }
254}
Coiby Xu70eb2c02020-09-18 16:09:08 +0800255
256static VuFdWatch *find_vu_fd_watch(VuServer *server, int fd)
257{
258
259 VuFdWatch *vu_fd_watch, *next;
260 QTAILQ_FOREACH_SAFE(vu_fd_watch, &server->vu_fd_watches, next, next) {
261 if (vu_fd_watch->fd == fd) {
262 return vu_fd_watch;
263 }
264 }
265 return NULL;
266}
267
268static void
269set_watch(VuDev *vu_dev, int fd, int vu_evt,
270 vu_watch_cb cb, void *pvt)
271{
272
273 VuServer *server = container_of(vu_dev, VuServer, vu_dev);
274 g_assert(vu_dev);
275 g_assert(fd >= 0);
276 g_assert(cb);
277
278 VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
279
280 if (!vu_fd_watch) {
Philippe Mathieu-Daudéfbf58f22023-09-04 18:12:29 +0200281 vu_fd_watch = g_new0(VuFdWatch, 1);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800282
283 QTAILQ_INSERT_TAIL(&server->vu_fd_watches, vu_fd_watch, next);
284
285 vu_fd_watch->fd = fd;
286 vu_fd_watch->cb = cb;
Marc-André Lureauff5927b2022-04-25 17:33:47 +0400287 qemu_socket_set_nonblock(fd);
Stefan Hajnoczi06e0f092023-08-30 18:48:02 -0400288 aio_set_fd_handler(server->ctx, fd, kick_handler,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000289 NULL, NULL, NULL, vu_fd_watch);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800290 vu_fd_watch->vu_dev = vu_dev;
291 vu_fd_watch->pvt = pvt;
292 }
293}
294
295
296static void remove_watch(VuDev *vu_dev, int fd)
297{
298 VuServer *server;
299 g_assert(vu_dev);
300 g_assert(fd >= 0);
301
302 server = container_of(vu_dev, VuServer, vu_dev);
303
304 VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
305
306 if (!vu_fd_watch) {
307 return;
308 }
Stefan Hajnoczi06e0f092023-08-30 18:48:02 -0400309 aio_set_fd_handler(server->ctx, fd, NULL, NULL, NULL, NULL, NULL);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800310
311 QTAILQ_REMOVE(&server->vu_fd_watches, vu_fd_watch, next);
312 g_free(vu_fd_watch);
313}
314
315
316static void vu_accept(QIONetListener *listener, QIOChannelSocket *sioc,
317 gpointer opaque)
318{
319 VuServer *server = opaque;
320
321 if (server->sioc) {
322 warn_report("Only one vhost-user client is allowed to "
323 "connect the server one time");
324 return;
325 }
326
327 if (!vu_init(&server->vu_dev, server->max_queues, sioc->fd, panic_cb,
328 vu_message_read, set_watch, remove_watch, server->vu_iface)) {
329 error_report("Failed to initialize libvhost-user");
330 return;
331 }
332
333 /*
334 * Unset the callback function for network listener to make another
335 * vhost-user client keeping waiting until this client disconnects
336 */
337 qio_net_listener_set_client_func(server->listener,
338 NULL,
339 NULL,
340 NULL);
341 server->sioc = sioc;
342 /*
343 * Increase the object reference, so sioc will not freed by
344 * qio_net_listener_channel_func which will call object_unref(OBJECT(sioc))
345 */
346 object_ref(OBJECT(server->sioc));
347 qio_channel_set_name(QIO_CHANNEL(sioc), "vhost-user client");
348 server->ioc = QIO_CHANNEL(sioc);
349 object_ref(OBJECT(server->ioc));
Coiby Xu70eb2c02020-09-18 16:09:08 +0800350
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100351 /* TODO vu_message_write() spins if non-blocking! */
352 qio_channel_set_blocking(server->ioc, false, NULL);
353
Stefan Hajnoczi06e0f092023-08-30 18:48:02 -0400354 qio_channel_set_follow_coroutine_ctx(server->ioc, true);
355
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100356 server->co_trip = qemu_coroutine_create(vu_client_trip, server);
357
358 aio_context_acquire(server->ctx);
359 vhost_user_server_attach_aio_context(server, server->ctx);
360 aio_context_release(server->ctx);
361}
Coiby Xu70eb2c02020-09-18 16:09:08 +0800362
Stefan Hajnoczi2957dc42023-03-23 10:58:53 -0400363/* server->ctx acquired by caller */
Coiby Xu70eb2c02020-09-18 16:09:08 +0800364void vhost_user_server_stop(VuServer *server)
365{
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100366 qemu_bh_delete(server->restart_listener_bh);
367 server->restart_listener_bh = NULL;
368
Coiby Xu70eb2c02020-09-18 16:09:08 +0800369 if (server->sioc) {
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100370 VuFdWatch *vu_fd_watch;
371
372 QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400373 aio_set_fd_handler(server->ctx, vu_fd_watch->fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000374 NULL, NULL, NULL, NULL, vu_fd_watch);
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100375 }
376
377 qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
378
379 AIO_WAIT_WHILE(server->ctx, server->co_trip);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800380 }
381
382 if (server->listener) {
383 qio_net_listener_disconnect(server->listener);
384 object_unref(OBJECT(server->listener));
385 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800386}
387
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100388/*
389 * Allow the next client to connect to the server. Called from a BH in the main
390 * loop.
391 */
392static void restart_listener_bh(void *opaque)
Coiby Xu70eb2c02020-09-18 16:09:08 +0800393{
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100394 VuServer *server = opaque;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800395
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100396 qio_net_listener_set_client_func(server->listener, vu_accept, server,
397 NULL);
398}
399
400/* Called with ctx acquired */
401void vhost_user_server_attach_aio_context(VuServer *server, AioContext *ctx)
402{
403 VuFdWatch *vu_fd_watch;
404
405 server->ctx = ctx;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800406
407 if (!server->sioc) {
Coiby Xu70eb2c02020-09-18 16:09:08 +0800408 return;
409 }
410
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100411 QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400412 aio_set_fd_handler(ctx, vu_fd_watch->fd, kick_handler, NULL,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000413 NULL, NULL, vu_fd_watch);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800414 }
415
Stefan Hajnoczi06e0f092023-08-30 18:48:02 -0400416 assert(!server->in_qio_channel_yield);
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100417 aio_co_schedule(ctx, server->co_trip);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800418}
419
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100420/* Called with server->ctx acquired */
421void vhost_user_server_detach_aio_context(VuServer *server)
422{
423 if (server->sioc) {
424 VuFdWatch *vu_fd_watch;
425
426 QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400427 aio_set_fd_handler(server->ctx, vu_fd_watch->fd,
Stefan Hajnoczi826cc322021-12-07 13:23:31 +0000428 NULL, NULL, NULL, NULL, vu_fd_watch);
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100429 }
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100430 }
431
432 server->ctx = NULL;
Stefan Hajnoczi06e0f092023-08-30 18:48:02 -0400433
434 if (server->ioc) {
435 if (server->in_qio_channel_yield) {
436 /* Stop receiving the next vhost-user message */
437 qio_channel_wake_read(server->ioc);
438 }
439 }
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100440}
Coiby Xu70eb2c02020-09-18 16:09:08 +0800441
442bool vhost_user_server_start(VuServer *server,
443 SocketAddress *socket_addr,
444 AioContext *ctx,
445 uint16_t max_queues,
Coiby Xu70eb2c02020-09-18 16:09:08 +0800446 const VuDevIface *vu_iface,
447 Error **errp)
448{
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100449 QEMUBH *bh;
Stefan Hajnoczi90fc91d2020-09-24 16:15:47 +0100450 QIONetListener *listener;
451
452 if (socket_addr->type != SOCKET_ADDRESS_TYPE_UNIX &&
453 socket_addr->type != SOCKET_ADDRESS_TYPE_FD) {
454 error_setg(errp, "Only socket address types 'unix' and 'fd' are supported");
455 return false;
456 }
457
458 listener = qio_net_listener_new();
Coiby Xu70eb2c02020-09-18 16:09:08 +0800459 if (qio_net_listener_open_sync(listener, socket_addr, 1,
460 errp) < 0) {
461 object_unref(OBJECT(listener));
462 return false;
463 }
464
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100465 bh = qemu_bh_new(restart_listener_bh, server);
466
Stefan Hajnoczi1d787452020-09-24 16:15:38 +0100467 /* zero out unspecified fields */
Coiby Xu70eb2c02020-09-18 16:09:08 +0800468 *server = (VuServer) {
469 .listener = listener,
Stefan Hajnoczi7185c852020-09-24 16:15:45 +0100470 .restart_listener_bh = bh,
Coiby Xu70eb2c02020-09-18 16:09:08 +0800471 .vu_iface = vu_iface,
472 .max_queues = max_queues,
473 .ctx = ctx,
Coiby Xu70eb2c02020-09-18 16:09:08 +0800474 };
475
476 qio_net_listener_set_name(server->listener, "vhost-user-backend-listener");
477
478 qio_net_listener_set_client_func(server->listener,
479 vu_accept,
480 server,
481 NULL);
482
483 QTAILQ_INIT(&server->vu_fd_watches);
484 return true;
485}