vhost-user-server.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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"
  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"
  15. /*
  16. * Theory of operation:
  17. *
  18. * VuServer is started and stopped by vhost_user_server_start() and
  19. * vhost_user_server_stop() from the main loop thread. Starting the server
  20. * opens a vhost-user UNIX domain socket and listens for incoming connections.
  21. * Only one connection is allowed at a time.
  22. *
  23. * The connection is handled by the vu_client_trip() coroutine in the
  24. * VuServer->ctx AioContext. The coroutine consists of a vu_dispatch() loop
  25. * where libvhost-user calls vu_message_read() to receive the next vhost-user
  26. * protocol messages over the UNIX domain socket.
  27. *
  28. * When virtqueues are set up libvhost-user calls set_watch() to monitor kick
  29. * fds. These fds are also handled in the VuServer->ctx AioContext.
  30. *
  31. * Both vu_client_trip() and kick fd monitoring can be stopped by shutting down
  32. * the socket connection. Shutting down the socket connection causes
  33. * vu_message_read() to fail since no more data can be received from the socket.
  34. * After vu_dispatch() fails, vu_client_trip() calls vu_deinit() to stop
  35. * libvhost-user before terminating the coroutine. vu_deinit() calls
  36. * remove_watch() to stop monitoring kick fds and this stops virtqueue
  37. * processing.
  38. *
  39. * When vu_client_trip() has finished cleaning up it schedules a BH in the main
  40. * loop thread to accept the next client connection.
  41. *
  42. * When libvhost-user detects an error it calls panic_cb() and sets the
  43. * dev->broken flag. Both vu_client_trip() and kick fd processing stop when
  44. * the dev->broken flag is set.
  45. *
  46. * It is possible to switch AioContexts using
  47. * vhost_user_server_detach_aio_context() and
  48. * vhost_user_server_attach_aio_context(). They stop monitoring fds in the old
  49. * AioContext and resume monitoring in the new AioContext. The vu_client_trip()
  50. * coroutine remains in a yielded state during the switch. This is made
  51. * possible by QIOChannel's support for spurious coroutine re-entry in
  52. * qio_channel_yield(). The coroutine will restart I/O when re-entered from the
  53. * new AioContext.
  54. */
  55. static void vmsg_close_fds(VhostUserMsg *vmsg)
  56. {
  57. int i;
  58. for (i = 0; i < vmsg->fd_num; i++) {
  59. close(vmsg->fds[i]);
  60. }
  61. }
  62. static void vmsg_unblock_fds(VhostUserMsg *vmsg)
  63. {
  64. int i;
  65. for (i = 0; i < vmsg->fd_num; i++) {
  66. qemu_socket_set_nonblock(vmsg->fds[i]);
  67. }
  68. }
  69. static void panic_cb(VuDev *vu_dev, const char *buf)
  70. {
  71. error_report("vu_panic: %s", buf);
  72. }
  73. void vhost_user_server_ref(VuServer *server)
  74. {
  75. assert(!server->wait_idle);
  76. server->refcount++;
  77. }
  78. void vhost_user_server_unref(VuServer *server)
  79. {
  80. server->refcount--;
  81. if (server->wait_idle && !server->refcount) {
  82. aio_co_wake(server->co_trip);
  83. }
  84. }
  85. static bool coroutine_fn
  86. vu_message_read(VuDev *vu_dev, int conn_fd, VhostUserMsg *vmsg)
  87. {
  88. struct iovec iov = {
  89. .iov_base = (char *)vmsg,
  90. .iov_len = VHOST_USER_HDR_SIZE,
  91. };
  92. int rc, read_bytes = 0;
  93. Error *local_err = NULL;
  94. const size_t max_fds = G_N_ELEMENTS(vmsg->fds);
  95. VuServer *server = container_of(vu_dev, VuServer, vu_dev);
  96. QIOChannel *ioc = server->ioc;
  97. vmsg->fd_num = 0;
  98. if (!ioc) {
  99. error_report_err(local_err);
  100. goto fail;
  101. }
  102. assert(qemu_in_coroutine());
  103. do {
  104. size_t nfds = 0;
  105. int *fds = NULL;
  106. /*
  107. * qio_channel_readv_full may have short reads, keeping calling it
  108. * until getting VHOST_USER_HDR_SIZE or 0 bytes in total
  109. */
  110. rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, 0, &local_err);
  111. if (rc < 0) {
  112. if (rc == QIO_CHANNEL_ERR_BLOCK) {
  113. assert(local_err == NULL);
  114. qio_channel_yield(ioc, G_IO_IN);
  115. continue;
  116. } else {
  117. error_report_err(local_err);
  118. goto fail;
  119. }
  120. }
  121. if (nfds > 0) {
  122. if (vmsg->fd_num + nfds > max_fds) {
  123. error_report("A maximum of %zu fds are allowed, "
  124. "however got %zu fds now",
  125. max_fds, vmsg->fd_num + nfds);
  126. g_free(fds);
  127. goto fail;
  128. }
  129. memcpy(vmsg->fds + vmsg->fd_num, fds, nfds * sizeof(vmsg->fds[0]));
  130. vmsg->fd_num += nfds;
  131. g_free(fds);
  132. }
  133. if (rc == 0) { /* socket closed */
  134. goto fail;
  135. }
  136. iov.iov_base += rc;
  137. iov.iov_len -= rc;
  138. read_bytes += rc;
  139. } while (read_bytes != VHOST_USER_HDR_SIZE);
  140. /* qio_channel_readv_full will make socket fds blocking, unblock them */
  141. vmsg_unblock_fds(vmsg);
  142. if (vmsg->size > sizeof(vmsg->payload)) {
  143. error_report("Error: too big message request: %d, "
  144. "size: vmsg->size: %u, "
  145. "while sizeof(vmsg->payload) = %zu",
  146. vmsg->request, vmsg->size, sizeof(vmsg->payload));
  147. goto fail;
  148. }
  149. struct iovec iov_payload = {
  150. .iov_base = (char *)&vmsg->payload,
  151. .iov_len = vmsg->size,
  152. };
  153. if (vmsg->size) {
  154. rc = qio_channel_readv_all_eof(ioc, &iov_payload, 1, &local_err);
  155. if (rc != 1) {
  156. if (local_err) {
  157. error_report_err(local_err);
  158. }
  159. goto fail;
  160. }
  161. }
  162. return true;
  163. fail:
  164. vmsg_close_fds(vmsg);
  165. return false;
  166. }
  167. static coroutine_fn void vu_client_trip(void *opaque)
  168. {
  169. VuServer *server = opaque;
  170. VuDev *vu_dev = &server->vu_dev;
  171. while (!vu_dev->broken && vu_dispatch(vu_dev)) {
  172. /* Keep running */
  173. }
  174. if (server->refcount) {
  175. /* Wait for requests to complete before we can unmap the memory */
  176. server->wait_idle = true;
  177. qemu_coroutine_yield();
  178. server->wait_idle = false;
  179. }
  180. assert(server->refcount == 0);
  181. vu_deinit(vu_dev);
  182. /* vu_deinit() should have called remove_watch() */
  183. assert(QTAILQ_EMPTY(&server->vu_fd_watches));
  184. object_unref(OBJECT(server->sioc));
  185. server->sioc = NULL;
  186. object_unref(OBJECT(server->ioc));
  187. server->ioc = NULL;
  188. server->co_trip = NULL;
  189. if (server->restart_listener_bh) {
  190. qemu_bh_schedule(server->restart_listener_bh);
  191. }
  192. aio_wait_kick();
  193. }
  194. /*
  195. * a wrapper for vu_kick_cb
  196. *
  197. * since aio_dispatch can only pass one user data pointer to the
  198. * callback function, pack VuDev and pvt into a struct. Then unpack it
  199. * and pass them to vu_kick_cb
  200. */
  201. static void kick_handler(void *opaque)
  202. {
  203. VuFdWatch *vu_fd_watch = opaque;
  204. VuDev *vu_dev = vu_fd_watch->vu_dev;
  205. vu_fd_watch->cb(vu_dev, 0, vu_fd_watch->pvt);
  206. /* Stop vu_client_trip() if an error occurred in vu_fd_watch->cb() */
  207. if (vu_dev->broken) {
  208. VuServer *server = container_of(vu_dev, VuServer, vu_dev);
  209. qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
  210. }
  211. }
  212. static VuFdWatch *find_vu_fd_watch(VuServer *server, int fd)
  213. {
  214. VuFdWatch *vu_fd_watch, *next;
  215. QTAILQ_FOREACH_SAFE(vu_fd_watch, &server->vu_fd_watches, next, next) {
  216. if (vu_fd_watch->fd == fd) {
  217. return vu_fd_watch;
  218. }
  219. }
  220. return NULL;
  221. }
  222. static void
  223. set_watch(VuDev *vu_dev, int fd, int vu_evt,
  224. vu_watch_cb cb, void *pvt)
  225. {
  226. VuServer *server = container_of(vu_dev, VuServer, vu_dev);
  227. g_assert(vu_dev);
  228. g_assert(fd >= 0);
  229. g_assert(cb);
  230. VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
  231. if (!vu_fd_watch) {
  232. VuFdWatch *vu_fd_watch = g_new0(VuFdWatch, 1);
  233. QTAILQ_INSERT_TAIL(&server->vu_fd_watches, vu_fd_watch, next);
  234. vu_fd_watch->fd = fd;
  235. vu_fd_watch->cb = cb;
  236. qemu_socket_set_nonblock(fd);
  237. aio_set_fd_handler(server->ioc->ctx, fd, true, kick_handler,
  238. NULL, NULL, NULL, vu_fd_watch);
  239. vu_fd_watch->vu_dev = vu_dev;
  240. vu_fd_watch->pvt = pvt;
  241. }
  242. }
  243. static void remove_watch(VuDev *vu_dev, int fd)
  244. {
  245. VuServer *server;
  246. g_assert(vu_dev);
  247. g_assert(fd >= 0);
  248. server = container_of(vu_dev, VuServer, vu_dev);
  249. VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
  250. if (!vu_fd_watch) {
  251. return;
  252. }
  253. aio_set_fd_handler(server->ioc->ctx, fd, true,
  254. NULL, NULL, NULL, NULL, NULL);
  255. QTAILQ_REMOVE(&server->vu_fd_watches, vu_fd_watch, next);
  256. g_free(vu_fd_watch);
  257. }
  258. static void vu_accept(QIONetListener *listener, QIOChannelSocket *sioc,
  259. gpointer opaque)
  260. {
  261. VuServer *server = opaque;
  262. if (server->sioc) {
  263. warn_report("Only one vhost-user client is allowed to "
  264. "connect the server one time");
  265. return;
  266. }
  267. if (!vu_init(&server->vu_dev, server->max_queues, sioc->fd, panic_cb,
  268. vu_message_read, set_watch, remove_watch, server->vu_iface)) {
  269. error_report("Failed to initialize libvhost-user");
  270. return;
  271. }
  272. /*
  273. * Unset the callback function for network listener to make another
  274. * vhost-user client keeping waiting until this client disconnects
  275. */
  276. qio_net_listener_set_client_func(server->listener,
  277. NULL,
  278. NULL,
  279. NULL);
  280. server->sioc = sioc;
  281. /*
  282. * Increase the object reference, so sioc will not freed by
  283. * qio_net_listener_channel_func which will call object_unref(OBJECT(sioc))
  284. */
  285. object_ref(OBJECT(server->sioc));
  286. qio_channel_set_name(QIO_CHANNEL(sioc), "vhost-user client");
  287. server->ioc = QIO_CHANNEL(sioc);
  288. object_ref(OBJECT(server->ioc));
  289. /* TODO vu_message_write() spins if non-blocking! */
  290. qio_channel_set_blocking(server->ioc, false, NULL);
  291. server->co_trip = qemu_coroutine_create(vu_client_trip, server);
  292. aio_context_acquire(server->ctx);
  293. vhost_user_server_attach_aio_context(server, server->ctx);
  294. aio_context_release(server->ctx);
  295. }
  296. /* server->ctx acquired by caller */
  297. void vhost_user_server_stop(VuServer *server)
  298. {
  299. qemu_bh_delete(server->restart_listener_bh);
  300. server->restart_listener_bh = NULL;
  301. if (server->sioc) {
  302. VuFdWatch *vu_fd_watch;
  303. QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
  304. aio_set_fd_handler(server->ctx, vu_fd_watch->fd, true,
  305. NULL, NULL, NULL, NULL, vu_fd_watch);
  306. }
  307. qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
  308. AIO_WAIT_WHILE(server->ctx, server->co_trip);
  309. }
  310. if (server->listener) {
  311. qio_net_listener_disconnect(server->listener);
  312. object_unref(OBJECT(server->listener));
  313. }
  314. }
  315. /*
  316. * Allow the next client to connect to the server. Called from a BH in the main
  317. * loop.
  318. */
  319. static void restart_listener_bh(void *opaque)
  320. {
  321. VuServer *server = opaque;
  322. qio_net_listener_set_client_func(server->listener, vu_accept, server,
  323. NULL);
  324. }
  325. /* Called with ctx acquired */
  326. void vhost_user_server_attach_aio_context(VuServer *server, AioContext *ctx)
  327. {
  328. VuFdWatch *vu_fd_watch;
  329. server->ctx = ctx;
  330. if (!server->sioc) {
  331. return;
  332. }
  333. qio_channel_attach_aio_context(server->ioc, ctx);
  334. QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
  335. aio_set_fd_handler(ctx, vu_fd_watch->fd, true, kick_handler, NULL,
  336. NULL, NULL, vu_fd_watch);
  337. }
  338. aio_co_schedule(ctx, server->co_trip);
  339. }
  340. /* Called with server->ctx acquired */
  341. void vhost_user_server_detach_aio_context(VuServer *server)
  342. {
  343. if (server->sioc) {
  344. VuFdWatch *vu_fd_watch;
  345. QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
  346. aio_set_fd_handler(server->ctx, vu_fd_watch->fd, true,
  347. NULL, NULL, NULL, NULL, vu_fd_watch);
  348. }
  349. qio_channel_detach_aio_context(server->ioc);
  350. }
  351. server->ctx = NULL;
  352. }
  353. bool vhost_user_server_start(VuServer *server,
  354. SocketAddress *socket_addr,
  355. AioContext *ctx,
  356. uint16_t max_queues,
  357. const VuDevIface *vu_iface,
  358. Error **errp)
  359. {
  360. QEMUBH *bh;
  361. QIONetListener *listener;
  362. if (socket_addr->type != SOCKET_ADDRESS_TYPE_UNIX &&
  363. socket_addr->type != SOCKET_ADDRESS_TYPE_FD) {
  364. error_setg(errp, "Only socket address types 'unix' and 'fd' are supported");
  365. return false;
  366. }
  367. listener = qio_net_listener_new();
  368. if (qio_net_listener_open_sync(listener, socket_addr, 1,
  369. errp) < 0) {
  370. object_unref(OBJECT(listener));
  371. return false;
  372. }
  373. bh = qemu_bh_new(restart_listener_bh, server);
  374. /* zero out unspecified fields */
  375. *server = (VuServer) {
  376. .listener = listener,
  377. .restart_listener_bh = bh,
  378. .vu_iface = vu_iface,
  379. .max_queues = max_queues,
  380. .ctx = ctx,
  381. };
  382. qio_net_listener_set_name(server->listener, "vhost-user-backend-listener");
  383. qio_net_listener_set_client_func(server->listener,
  384. vu_accept,
  385. server,
  386. NULL);
  387. QTAILQ_INIT(&server->vu_fd_watches);
  388. return true;
  389. }