Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 1 | /* |
| 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 Huth | 5feed38 | 2023-02-10 12:19:31 +0100 | [diff] [blame] | 11 | #include "qemu/error-report.h" |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 12 | #include "qemu/main-loop.h" |
Stefan Hajnoczi | 80a06cc | 2020-09-24 16:15:48 +0100 | [diff] [blame] | 13 | #include "qemu/vhost-user-server.h" |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 14 | #include "block/aio-wait.h" |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 15 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 16 | /* |
| 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 57 | static 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 | |
| 65 | static void vmsg_unblock_fds(VhostUserMsg *vmsg) |
| 66 | { |
| 67 | int i; |
| 68 | for (i = 0; i < vmsg->fd_num; i++) { |
Marc-André Lureau | ff5927b | 2022-04-25 17:33:47 +0400 | [diff] [blame] | 69 | qemu_socket_set_nonblock(vmsg->fds[i]); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 73 | static void panic_cb(VuDev *vu_dev, const char *buf) |
| 74 | { |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 75 | error_report("vu_panic: %s", buf); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 76 | } |
| 77 | |
Stefan Hajnoczi | 75d33e8 | 2023-05-16 15:02:23 -0400 | [diff] [blame] | 78 | void vhost_user_server_inc_in_flight(VuServer *server) |
Kevin Wolf | 520d8b4 | 2022-01-25 16:14:35 +0100 | [diff] [blame] | 79 | { |
| 80 | assert(!server->wait_idle); |
Stefan Hajnoczi | 8f5e9a8 | 2023-05-16 15:02:24 -0400 | [diff] [blame] | 81 | qatomic_inc(&server->in_flight); |
Kevin Wolf | 520d8b4 | 2022-01-25 16:14:35 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Stefan Hajnoczi | 75d33e8 | 2023-05-16 15:02:23 -0400 | [diff] [blame] | 84 | void vhost_user_server_dec_in_flight(VuServer *server) |
Kevin Wolf | 520d8b4 | 2022-01-25 16:14:35 +0100 | [diff] [blame] | 85 | { |
Stefan Hajnoczi | 8f5e9a8 | 2023-05-16 15:02:24 -0400 | [diff] [blame] | 86 | if (qatomic_fetch_dec(&server->in_flight) == 1) { |
| 87 | if (server->wait_idle) { |
| 88 | aio_co_wake(server->co_trip); |
| 89 | } |
Kevin Wolf | 520d8b4 | 2022-01-25 16:14:35 +0100 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Stefan Hajnoczi | 8f5e9a8 | 2023-05-16 15:02:24 -0400 | [diff] [blame] | 93 | bool vhost_user_server_has_in_flight(VuServer *server) |
| 94 | { |
| 95 | return qatomic_load_acquire(&server->in_flight) > 0; |
| 96 | } |
| 97 | |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 98 | static bool coroutine_fn |
| 99 | vu_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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 107 | const size_t max_fds = G_N_ELEMENTS(vmsg->fds); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 108 | VuServer *server = container_of(vu_dev, VuServer, vu_dev); |
| 109 | QIOChannel *ioc = server->ioc; |
| 110 | |
Stefan Hajnoczi | 8c7f7cb | 2020-09-24 16:15:43 +0100 | [diff] [blame] | 111 | vmsg->fd_num = 0; |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 112 | if (!ioc) { |
| 113 | error_report_err(local_err); |
| 114 | goto fail; |
| 115 | } |
| 116 | |
| 117 | assert(qemu_in_coroutine()); |
| 118 | do { |
Stefan Hajnoczi | 8c7f7cb | 2020-09-24 16:15:43 +0100 | [diff] [blame] | 119 | size_t nfds = 0; |
| 120 | int *fds = NULL; |
| 121 | |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 122 | /* |
| 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.mishra | 84615a1 | 2022-12-20 18:44:17 +0000 | [diff] [blame] | 126 | rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, 0, &local_err); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 127 | if (rc < 0) { |
| 128 | if (rc == QIO_CHANNEL_ERR_BLOCK) { |
Stefan Hajnoczi | 8c7f7cb | 2020-09-24 16:15:43 +0100 | [diff] [blame] | 129 | assert(local_err == NULL); |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 130 | 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 138 | continue; |
| 139 | } else { |
| 140 | error_report_err(local_err); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 141 | goto fail; |
| 142 | } |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 143 | } |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 144 | |
Stefan Hajnoczi | 8c7f7cb | 2020-09-24 16:15:43 +0100 | [diff] [blame] | 145 | 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 167 | /* 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 Hajnoczi | edaf620 | 2020-09-24 16:15:44 +0100 | [diff] [blame] | 183 | if (rc != 1) { |
| 184 | if (local_err) { |
| 185 | error_report_err(local_err); |
| 186 | } |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 187 | goto fail; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return true; |
| 192 | |
| 193 | fail: |
| 194 | vmsg_close_fds(vmsg); |
| 195 | |
| 196 | return false; |
| 197 | } |
| 198 | |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 199 | static coroutine_fn void vu_client_trip(void *opaque) |
| 200 | { |
| 201 | VuServer *server = opaque; |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 202 | VuDev *vu_dev = &server->vu_dev; |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 203 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 204 | while (!vu_dev->broken && vu_dispatch(vu_dev)) { |
| 205 | /* Keep running */ |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 206 | } |
| 207 | |
Stefan Hajnoczi | 8f5e9a8 | 2023-05-16 15:02:24 -0400 | [diff] [blame] | 208 | if (vhost_user_server_has_in_flight(server)) { |
Kevin Wolf | 520d8b4 | 2022-01-25 16:14:35 +0100 | [diff] [blame] | 209 | /* 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 Hajnoczi | 8f5e9a8 | 2023-05-16 15:02:24 -0400 | [diff] [blame] | 214 | assert(!vhost_user_server_has_in_flight(server)); |
Kevin Wolf | 520d8b4 | 2022-01-25 16:14:35 +0100 | [diff] [blame] | 215 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 216 | vu_deinit(vu_dev); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 217 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 218 | /* 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 232 | } |
| 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 | */ |
| 241 | static void kick_handler(void *opaque) |
| 242 | { |
| 243 | VuFdWatch *vu_fd_watch = opaque; |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 244 | VuDev *vu_dev = vu_fd_watch->vu_dev; |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 245 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 246 | 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 255 | |
| 256 | static 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 | |
| 268 | static void |
| 269 | set_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é | fbf58f2 | 2023-09-04 18:12:29 +0200 | [diff] [blame] | 281 | vu_fd_watch = g_new0(VuFdWatch, 1); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 282 | |
| 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é Lureau | ff5927b | 2022-04-25 17:33:47 +0400 | [diff] [blame] | 287 | qemu_socket_set_nonblock(fd); |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 288 | aio_set_fd_handler(server->ctx, fd, kick_handler, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 289 | NULL, NULL, NULL, vu_fd_watch); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 290 | vu_fd_watch->vu_dev = vu_dev; |
| 291 | vu_fd_watch->pvt = pvt; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | |
| 296 | static 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 Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 309 | aio_set_fd_handler(server->ctx, fd, NULL, NULL, NULL, NULL, NULL); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 310 | |
| 311 | QTAILQ_REMOVE(&server->vu_fd_watches, vu_fd_watch, next); |
| 312 | g_free(vu_fd_watch); |
| 313 | } |
| 314 | |
| 315 | |
| 316 | static 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 350 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 351 | /* TODO vu_message_write() spins if non-blocking! */ |
| 352 | qio_channel_set_blocking(server->ioc, false, NULL); |
| 353 | |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 354 | qio_channel_set_follow_coroutine_ctx(server->ioc, true); |
| 355 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 356 | 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 362 | |
Stefan Hajnoczi | 2957dc4 | 2023-03-23 10:58:53 -0400 | [diff] [blame] | 363 | /* server->ctx acquired by caller */ |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 364 | void vhost_user_server_stop(VuServer *server) |
| 365 | { |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 366 | qemu_bh_delete(server->restart_listener_bh); |
| 367 | server->restart_listener_bh = NULL; |
| 368 | |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 369 | if (server->sioc) { |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 370 | VuFdWatch *vu_fd_watch; |
| 371 | |
| 372 | QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) { |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 373 | aio_set_fd_handler(server->ctx, vu_fd_watch->fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 374 | NULL, NULL, NULL, NULL, vu_fd_watch); |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL); |
| 378 | |
| 379 | AIO_WAIT_WHILE(server->ctx, server->co_trip); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (server->listener) { |
| 383 | qio_net_listener_disconnect(server->listener); |
| 384 | object_unref(OBJECT(server->listener)); |
| 385 | } |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 386 | } |
| 387 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 388 | /* |
| 389 | * Allow the next client to connect to the server. Called from a BH in the main |
| 390 | * loop. |
| 391 | */ |
| 392 | static void restart_listener_bh(void *opaque) |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 393 | { |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 394 | VuServer *server = opaque; |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 395 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 396 | qio_net_listener_set_client_func(server->listener, vu_accept, server, |
| 397 | NULL); |
| 398 | } |
| 399 | |
| 400 | /* Called with ctx acquired */ |
| 401 | void vhost_user_server_attach_aio_context(VuServer *server, AioContext *ctx) |
| 402 | { |
| 403 | VuFdWatch *vu_fd_watch; |
| 404 | |
| 405 | server->ctx = ctx; |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 406 | |
| 407 | if (!server->sioc) { |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 408 | return; |
| 409 | } |
| 410 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 411 | QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) { |
Stefan Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 412 | aio_set_fd_handler(ctx, vu_fd_watch->fd, kick_handler, NULL, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 413 | NULL, NULL, vu_fd_watch); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 414 | } |
| 415 | |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 416 | assert(!server->in_qio_channel_yield); |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 417 | aio_co_schedule(ctx, server->co_trip); |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 418 | } |
| 419 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 420 | /* Called with server->ctx acquired */ |
| 421 | void 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 Hajnoczi | 60f782b | 2023-05-16 15:02:38 -0400 | [diff] [blame] | 427 | aio_set_fd_handler(server->ctx, vu_fd_watch->fd, |
Stefan Hajnoczi | 826cc32 | 2021-12-07 13:23:31 +0000 | [diff] [blame] | 428 | NULL, NULL, NULL, NULL, vu_fd_watch); |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 429 | } |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | server->ctx = NULL; |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 433 | |
| 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 Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 440 | } |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 441 | |
| 442 | bool vhost_user_server_start(VuServer *server, |
| 443 | SocketAddress *socket_addr, |
| 444 | AioContext *ctx, |
| 445 | uint16_t max_queues, |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 446 | const VuDevIface *vu_iface, |
| 447 | Error **errp) |
| 448 | { |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 449 | QEMUBH *bh; |
Stefan Hajnoczi | 90fc91d | 2020-09-24 16:15:47 +0100 | [diff] [blame] | 450 | 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 Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 459 | if (qio_net_listener_open_sync(listener, socket_addr, 1, |
| 460 | errp) < 0) { |
| 461 | object_unref(OBJECT(listener)); |
| 462 | return false; |
| 463 | } |
| 464 | |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 465 | bh = qemu_bh_new(restart_listener_bh, server); |
| 466 | |
Stefan Hajnoczi | 1d78745 | 2020-09-24 16:15:38 +0100 | [diff] [blame] | 467 | /* zero out unspecified fields */ |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 468 | *server = (VuServer) { |
| 469 | .listener = listener, |
Stefan Hajnoczi | 7185c85 | 2020-09-24 16:15:45 +0100 | [diff] [blame] | 470 | .restart_listener_bh = bh, |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 471 | .vu_iface = vu_iface, |
| 472 | .max_queues = max_queues, |
| 473 | .ctx = ctx, |
Coiby Xu | 70eb2c0 | 2020-09-18 16:09:08 +0800 | [diff] [blame] | 474 | }; |
| 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 | } |