blob: 981908fef090fe9add0d1fa63be82a962ead27aa [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"
11#include "qemu/main-loop.h"
12#include "vhost-user-server.h"
13
14static void vmsg_close_fds(VhostUserMsg *vmsg)
15{
16 int i;
17 for (i = 0; i < vmsg->fd_num; i++) {
18 close(vmsg->fds[i]);
19 }
20}
21
22static void vmsg_unblock_fds(VhostUserMsg *vmsg)
23{
24 int i;
25 for (i = 0; i < vmsg->fd_num; i++) {
26 qemu_set_nonblock(vmsg->fds[i]);
27 }
28}
29
30static void vu_accept(QIONetListener *listener, QIOChannelSocket *sioc,
31 gpointer opaque);
32
33static void close_client(VuServer *server)
34{
35 /*
36 * Before closing the client
37 *
38 * 1. Let vu_client_trip stop processing new vhost-user msg
39 *
40 * 2. remove kick_handler
41 *
42 * 3. wait for the kick handler to be finished
43 *
44 * 4. wait for the current vhost-user msg to be finished processing
45 */
46
47 QIOChannelSocket *sioc = server->sioc;
48 /* When this is set vu_client_trip will stop new processing vhost-user message */
49 server->sioc = NULL;
50
Coiby Xu70eb2c02020-09-18 16:09:08 +080051 while (server->processing_msg) {
52 if (server->ioc->read_coroutine) {
53 server->ioc->read_coroutine = NULL;
54 qio_channel_set_aio_fd_handler(server->ioc, server->ioc->ctx, NULL,
55 NULL, server->ioc);
56 server->processing_msg = false;
57 }
58 }
59
60 vu_deinit(&server->vu_dev);
Stefan Hajnoczidad4f192020-09-24 16:15:40 +010061
62 /* vu_deinit() should have called remove_watch() */
63 assert(QTAILQ_EMPTY(&server->vu_fd_watches));
64
Coiby Xu70eb2c02020-09-18 16:09:08 +080065 object_unref(OBJECT(sioc));
66 object_unref(OBJECT(server->ioc));
67}
68
69static void panic_cb(VuDev *vu_dev, const char *buf)
70{
71 VuServer *server = container_of(vu_dev, VuServer, vu_dev);
72
73 /* avoid while loop in close_client */
74 server->processing_msg = false;
75
76 if (buf) {
77 error_report("vu_panic: %s", buf);
78 }
79
80 if (server->sioc) {
81 close_client(server);
82 }
83
Coiby Xu70eb2c02020-09-18 16:09:08 +080084 /*
85 * Set the callback function for network listener so another
86 * vhost-user client can connect to this server
87 */
88 qio_net_listener_set_client_func(server->listener,
89 vu_accept,
90 server,
91 NULL);
92}
93
94static bool coroutine_fn
95vu_message_read(VuDev *vu_dev, int conn_fd, VhostUserMsg *vmsg)
96{
97 struct iovec iov = {
98 .iov_base = (char *)vmsg,
99 .iov_len = VHOST_USER_HDR_SIZE,
100 };
101 int rc, read_bytes = 0;
102 Error *local_err = NULL;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800103 const size_t max_fds = G_N_ELEMENTS(vmsg->fds);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800104 VuServer *server = container_of(vu_dev, VuServer, vu_dev);
105 QIOChannel *ioc = server->ioc;
106
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100107 vmsg->fd_num = 0;
Coiby Xu70eb2c02020-09-18 16:09:08 +0800108 if (!ioc) {
109 error_report_err(local_err);
110 goto fail;
111 }
112
113 assert(qemu_in_coroutine());
114 do {
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100115 size_t nfds = 0;
116 int *fds = NULL;
117
Coiby Xu70eb2c02020-09-18 16:09:08 +0800118 /*
119 * qio_channel_readv_full may have short reads, keeping calling it
120 * until getting VHOST_USER_HDR_SIZE or 0 bytes in total
121 */
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100122 rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, &local_err);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800123 if (rc < 0) {
124 if (rc == QIO_CHANNEL_ERR_BLOCK) {
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100125 assert(local_err == NULL);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800126 qio_channel_yield(ioc, G_IO_IN);
127 continue;
128 } else {
129 error_report_err(local_err);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800130 goto fail;
131 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800132 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800133
Stefan Hajnoczi8c7f7cb2020-09-24 16:15:43 +0100134 if (nfds > 0) {
135 if (vmsg->fd_num + nfds > max_fds) {
136 error_report("A maximum of %zu fds are allowed, "
137 "however got %zu fds now",
138 max_fds, vmsg->fd_num + nfds);
139 g_free(fds);
140 goto fail;
141 }
142 memcpy(vmsg->fds + vmsg->fd_num, fds, nfds * sizeof(vmsg->fds[0]));
143 vmsg->fd_num += nfds;
144 g_free(fds);
145 }
146
147 if (rc == 0) { /* socket closed */
148 goto fail;
149 }
150
151 iov.iov_base += rc;
152 iov.iov_len -= rc;
153 read_bytes += rc;
154 } while (read_bytes != VHOST_USER_HDR_SIZE);
155
Coiby Xu70eb2c02020-09-18 16:09:08 +0800156 /* qio_channel_readv_full will make socket fds blocking, unblock them */
157 vmsg_unblock_fds(vmsg);
158 if (vmsg->size > sizeof(vmsg->payload)) {
159 error_report("Error: too big message request: %d, "
160 "size: vmsg->size: %u, "
161 "while sizeof(vmsg->payload) = %zu",
162 vmsg->request, vmsg->size, sizeof(vmsg->payload));
163 goto fail;
164 }
165
166 struct iovec iov_payload = {
167 .iov_base = (char *)&vmsg->payload,
168 .iov_len = vmsg->size,
169 };
170 if (vmsg->size) {
171 rc = qio_channel_readv_all_eof(ioc, &iov_payload, 1, &local_err);
Stefan Hajnocziedaf6202020-09-24 16:15:44 +0100172 if (rc != 1) {
173 if (local_err) {
174 error_report_err(local_err);
175 }
Coiby Xu70eb2c02020-09-18 16:09:08 +0800176 goto fail;
177 }
178 }
179
180 return true;
181
182fail:
183 vmsg_close_fds(vmsg);
184
185 return false;
186}
187
188
189static void vu_client_start(VuServer *server);
190static coroutine_fn void vu_client_trip(void *opaque)
191{
192 VuServer *server = opaque;
193
194 while (!server->aio_context_changed && server->sioc) {
195 server->processing_msg = true;
196 vu_dispatch(&server->vu_dev);
197 server->processing_msg = false;
198 }
199
200 if (server->aio_context_changed && server->sioc) {
201 server->aio_context_changed = false;
202 vu_client_start(server);
203 }
204}
205
206static void vu_client_start(VuServer *server)
207{
208 server->co_trip = qemu_coroutine_create(vu_client_trip, server);
209 aio_co_enter(server->ctx, server->co_trip);
210}
211
212/*
213 * a wrapper for vu_kick_cb
214 *
215 * since aio_dispatch can only pass one user data pointer to the
216 * callback function, pack VuDev and pvt into a struct. Then unpack it
217 * and pass them to vu_kick_cb
218 */
219static void kick_handler(void *opaque)
220{
221 VuFdWatch *vu_fd_watch = opaque;
222 vu_fd_watch->processing = true;
223 vu_fd_watch->cb(vu_fd_watch->vu_dev, 0, vu_fd_watch->pvt);
224 vu_fd_watch->processing = false;
225}
226
227
228static VuFdWatch *find_vu_fd_watch(VuServer *server, int fd)
229{
230
231 VuFdWatch *vu_fd_watch, *next;
232 QTAILQ_FOREACH_SAFE(vu_fd_watch, &server->vu_fd_watches, next, next) {
233 if (vu_fd_watch->fd == fd) {
234 return vu_fd_watch;
235 }
236 }
237 return NULL;
238}
239
240static void
241set_watch(VuDev *vu_dev, int fd, int vu_evt,
242 vu_watch_cb cb, void *pvt)
243{
244
245 VuServer *server = container_of(vu_dev, VuServer, vu_dev);
246 g_assert(vu_dev);
247 g_assert(fd >= 0);
248 g_assert(cb);
249
250 VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
251
252 if (!vu_fd_watch) {
253 VuFdWatch *vu_fd_watch = g_new0(VuFdWatch, 1);
254
255 QTAILQ_INSERT_TAIL(&server->vu_fd_watches, vu_fd_watch, next);
256
257 vu_fd_watch->fd = fd;
258 vu_fd_watch->cb = cb;
259 qemu_set_nonblock(fd);
260 aio_set_fd_handler(server->ioc->ctx, fd, true, kick_handler,
261 NULL, NULL, vu_fd_watch);
262 vu_fd_watch->vu_dev = vu_dev;
263 vu_fd_watch->pvt = pvt;
264 }
265}
266
267
268static void remove_watch(VuDev *vu_dev, int fd)
269{
270 VuServer *server;
271 g_assert(vu_dev);
272 g_assert(fd >= 0);
273
274 server = container_of(vu_dev, VuServer, vu_dev);
275
276 VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
277
278 if (!vu_fd_watch) {
279 return;
280 }
281 aio_set_fd_handler(server->ioc->ctx, fd, true, NULL, NULL, NULL, NULL);
282
283 QTAILQ_REMOVE(&server->vu_fd_watches, vu_fd_watch, next);
284 g_free(vu_fd_watch);
285}
286
287
288static void vu_accept(QIONetListener *listener, QIOChannelSocket *sioc,
289 gpointer opaque)
290{
291 VuServer *server = opaque;
292
293 if (server->sioc) {
294 warn_report("Only one vhost-user client is allowed to "
295 "connect the server one time");
296 return;
297 }
298
299 if (!vu_init(&server->vu_dev, server->max_queues, sioc->fd, panic_cb,
300 vu_message_read, set_watch, remove_watch, server->vu_iface)) {
301 error_report("Failed to initialize libvhost-user");
302 return;
303 }
304
305 /*
306 * Unset the callback function for network listener to make another
307 * vhost-user client keeping waiting until this client disconnects
308 */
309 qio_net_listener_set_client_func(server->listener,
310 NULL,
311 NULL,
312 NULL);
313 server->sioc = sioc;
314 /*
315 * Increase the object reference, so sioc will not freed by
316 * qio_net_listener_channel_func which will call object_unref(OBJECT(sioc))
317 */
318 object_ref(OBJECT(server->sioc));
319 qio_channel_set_name(QIO_CHANNEL(sioc), "vhost-user client");
320 server->ioc = QIO_CHANNEL(sioc);
321 object_ref(OBJECT(server->ioc));
322 qio_channel_attach_aio_context(server->ioc, server->ctx);
Stefan Hajnoczi46a096c2020-09-24 16:15:39 +0100323 qio_channel_set_blocking(server->ioc, false, NULL);
Coiby Xu70eb2c02020-09-18 16:09:08 +0800324 vu_client_start(server);
325}
326
327
328void vhost_user_server_stop(VuServer *server)
329{
330 if (server->sioc) {
331 close_client(server);
332 }
333
334 if (server->listener) {
335 qio_net_listener_disconnect(server->listener);
336 object_unref(OBJECT(server->listener));
337 }
338
339}
340
341void vhost_user_server_set_aio_context(VuServer *server, AioContext *ctx)
342{
343 VuFdWatch *vu_fd_watch, *next;
344 void *opaque = NULL;
345 IOHandler *io_read = NULL;
346 bool attach;
347
348 server->ctx = ctx ? ctx : qemu_get_aio_context();
349
350 if (!server->sioc) {
351 /* not yet serving any client*/
352 return;
353 }
354
355 if (ctx) {
356 qio_channel_attach_aio_context(server->ioc, ctx);
357 server->aio_context_changed = true;
358 io_read = kick_handler;
359 attach = true;
360 } else {
361 qio_channel_detach_aio_context(server->ioc);
362 /* server->ioc->ctx keeps the old AioConext */
363 ctx = server->ioc->ctx;
364 attach = false;
365 }
366
367 QTAILQ_FOREACH_SAFE(vu_fd_watch, &server->vu_fd_watches, next, next) {
368 if (vu_fd_watch->cb) {
369 opaque = attach ? vu_fd_watch : NULL;
370 aio_set_fd_handler(ctx, vu_fd_watch->fd, true,
371 io_read, NULL, NULL,
372 opaque);
373 }
374 }
375}
376
377
378bool vhost_user_server_start(VuServer *server,
379 SocketAddress *socket_addr,
380 AioContext *ctx,
381 uint16_t max_queues,
Coiby Xu70eb2c02020-09-18 16:09:08 +0800382 const VuDevIface *vu_iface,
383 Error **errp)
384{
385 QIONetListener *listener = qio_net_listener_new();
386 if (qio_net_listener_open_sync(listener, socket_addr, 1,
387 errp) < 0) {
388 object_unref(OBJECT(listener));
389 return false;
390 }
391
Stefan Hajnoczi1d787452020-09-24 16:15:38 +0100392 /* zero out unspecified fields */
Coiby Xu70eb2c02020-09-18 16:09:08 +0800393 *server = (VuServer) {
394 .listener = listener,
395 .vu_iface = vu_iface,
396 .max_queues = max_queues,
397 .ctx = ctx,
Coiby Xu70eb2c02020-09-18 16:09:08 +0800398 };
399
400 qio_net_listener_set_name(server->listener, "vhost-user-backend-listener");
401
402 qio_net_listener_set_client_func(server->listener,
403 vu_accept,
404 server,
405 NULL);
406
407 QTAILQ_INIT(&server->vu_fd_watches);
408 return true;
409}