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"
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)
75
if (vmsg->request == VHOST_USER_ADD_MEM_REG ||
76
vmsg->request == VHOST_USER_SET_MEM_TABLE) {
80
for (i = 0; i < vmsg->fd_num; i++) {
81
qemu_socket_set_nonblock(vmsg->fds[i]);
85
static void panic_cb(VuDev *vu_dev, const char *buf)
87
error_report("vu_panic: %s", buf);
90
void vhost_user_server_inc_in_flight(VuServer *server)
92
assert(!server->wait_idle);
93
qatomic_inc(&server->in_flight);
96
void vhost_user_server_dec_in_flight(VuServer *server)
98
if (qatomic_fetch_dec(&server->in_flight) == 1) {
99
if (server->wait_idle) {
100
aio_co_wake(server->co_trip);
105
bool vhost_user_server_has_in_flight(VuServer *server)
107
return qatomic_load_acquire(&server->in_flight) > 0;
110
static bool coroutine_fn
111
vu_message_read(VuDev *vu_dev, int conn_fd, VhostUserMsg *vmsg)
114
.iov_base = (char *)vmsg,
115
.iov_len = VHOST_USER_HDR_SIZE,
117
int rc, read_bytes = 0;
118
Error *local_err = NULL;
119
const size_t max_fds = G_N_ELEMENTS(vmsg->fds);
120
VuServer *server = container_of(vu_dev, VuServer, vu_dev);
121
QIOChannel *ioc = server->ioc;
125
error_report_err(local_err);
129
assert(qemu_in_coroutine());
138
rc = qio_channel_readv_full(ioc, &iov, 1, &fds, &nfds, 0, &local_err);
140
if (rc == QIO_CHANNEL_ERR_BLOCK) {
141
assert(local_err == NULL);
143
server->in_qio_channel_yield = true;
144
qio_channel_yield(ioc, G_IO_IN);
145
server->in_qio_channel_yield = false;
151
error_report_err(local_err);
157
if (vmsg->fd_num + nfds > max_fds) {
158
error_report("A maximum of %zu fds are allowed, "
159
"however got %zu fds now",
160
max_fds, vmsg->fd_num + nfds);
164
memcpy(vmsg->fds + vmsg->fd_num, fds, nfds * sizeof(vmsg->fds[0]));
165
vmsg->fd_num += nfds;
176
} while (read_bytes != VHOST_USER_HDR_SIZE);
179
vmsg_unblock_fds(vmsg);
180
if (vmsg->size > sizeof(vmsg->payload)) {
181
error_report("Error: too big message request: %d, "
182
"size: vmsg->size: %u, "
183
"while sizeof(vmsg->payload) = %zu",
184
vmsg->request, vmsg->size, sizeof(vmsg->payload));
188
struct iovec iov_payload = {
189
.iov_base = (char *)&vmsg->payload,
190
.iov_len = vmsg->size,
193
rc = qio_channel_readv_all_eof(ioc, &iov_payload, 1, &local_err);
196
error_report_err(local_err);
205
vmsg_close_fds(vmsg);
210
static coroutine_fn void vu_client_trip(void *opaque)
212
VuServer *server = opaque;
213
VuDev *vu_dev = &server->vu_dev;
215
while (!vu_dev->broken) {
216
if (server->quiescing) {
217
server->co_trip = NULL;
222
if (!vu_dispatch(vu_dev) && server->ctx) {
227
if (vhost_user_server_has_in_flight(server)) {
229
server->wait_idle = true;
230
qemu_coroutine_yield();
231
server->wait_idle = false;
233
assert(!vhost_user_server_has_in_flight(server));
238
assert(QTAILQ_EMPTY(&server->vu_fd_watches));
240
object_unref(OBJECT(server->sioc));
243
object_unref(OBJECT(server->ioc));
246
server->co_trip = NULL;
247
if (server->restart_listener_bh) {
248
qemu_bh_schedule(server->restart_listener_bh);
260
static void kick_handler(void *opaque)
262
VuFdWatch *vu_fd_watch = opaque;
263
VuDev *vu_dev = vu_fd_watch->vu_dev;
265
vu_fd_watch->cb(vu_dev, 0, vu_fd_watch->pvt);
268
if (vu_dev->broken) {
269
VuServer *server = container_of(vu_dev, VuServer, vu_dev);
271
qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
275
static VuFdWatch *find_vu_fd_watch(VuServer *server, int fd)
278
VuFdWatch *vu_fd_watch, *next;
279
QTAILQ_FOREACH_SAFE(vu_fd_watch, &server->vu_fd_watches, next, next) {
280
if (vu_fd_watch->fd == fd) {
288
set_watch(VuDev *vu_dev, int fd, int vu_evt,
289
vu_watch_cb cb, void *pvt)
292
VuServer *server = container_of(vu_dev, VuServer, vu_dev);
297
VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
300
vu_fd_watch = g_new0(VuFdWatch, 1);
302
QTAILQ_INSERT_TAIL(&server->vu_fd_watches, vu_fd_watch, next);
304
vu_fd_watch->fd = fd;
305
vu_fd_watch->cb = cb;
306
qemu_socket_set_nonblock(fd);
307
aio_set_fd_handler(server->ctx, fd, kick_handler,
308
NULL, NULL, NULL, vu_fd_watch);
309
vu_fd_watch->vu_dev = vu_dev;
310
vu_fd_watch->pvt = pvt;
315
static void remove_watch(VuDev *vu_dev, int fd)
321
server = container_of(vu_dev, VuServer, vu_dev);
323
VuFdWatch *vu_fd_watch = find_vu_fd_watch(server, fd);
328
aio_set_fd_handler(server->ctx, fd, NULL, NULL, NULL, NULL, NULL);
330
QTAILQ_REMOVE(&server->vu_fd_watches, vu_fd_watch, next);
335
static void vu_accept(QIONetListener *listener, QIOChannelSocket *sioc,
338
VuServer *server = opaque;
341
warn_report("Only one vhost-user client is allowed to "
342
"connect the server one time");
346
if (!vu_init(&server->vu_dev, server->max_queues, sioc->fd, panic_cb,
347
vu_message_read, set_watch, remove_watch, server->vu_iface)) {
348
error_report("Failed to initialize libvhost-user");
356
qio_net_listener_set_client_func(server->listener,
365
object_ref(OBJECT(server->sioc));
366
qio_channel_set_name(QIO_CHANNEL(sioc), "vhost-user client");
367
server->ioc = QIO_CHANNEL(sioc);
368
object_ref(OBJECT(server->ioc));
371
qio_channel_set_blocking(server->ioc, false, NULL);
373
qio_channel_set_follow_coroutine_ctx(server->ioc, true);
375
vhost_user_server_attach_aio_context(server, server->ctx);
379
void vhost_user_server_stop(VuServer *server)
381
qemu_bh_delete(server->restart_listener_bh);
382
server->restart_listener_bh = NULL;
385
VuFdWatch *vu_fd_watch;
387
QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
388
aio_set_fd_handler(server->ctx, vu_fd_watch->fd,
389
NULL, NULL, NULL, NULL, vu_fd_watch);
392
qio_channel_shutdown(server->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
394
AIO_WAIT_WHILE(server->ctx, server->co_trip);
397
if (server->listener) {
398
qio_net_listener_disconnect(server->listener);
399
object_unref(OBJECT(server->listener));
407
static void restart_listener_bh(void *opaque)
409
VuServer *server = opaque;
411
qio_net_listener_set_client_func(server->listener, vu_accept, server,
416
void vhost_user_server_attach_aio_context(VuServer *server, AioContext *ctx)
418
VuFdWatch *vu_fd_watch;
426
QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
427
aio_set_fd_handler(ctx, vu_fd_watch->fd, kick_handler, NULL,
428
NULL, NULL, vu_fd_watch);
431
if (server->co_trip) {
441
AioContext *co_ctx = qemu_coroutine_get_aio_context(server->co_trip);
443
assert(!server->quiescing);
444
assert(!co_ctx || co_ctx == ctx);
446
server->co_trip = qemu_coroutine_create(vu_client_trip, server);
447
assert(!server->in_qio_channel_yield);
448
aio_co_schedule(ctx, server->co_trip);
453
void vhost_user_server_detach_aio_context(VuServer *server)
456
VuFdWatch *vu_fd_watch;
458
QTAILQ_FOREACH(vu_fd_watch, &server->vu_fd_watches, next) {
459
aio_set_fd_handler(server->ctx, vu_fd_watch->fd,
460
NULL, NULL, NULL, NULL, vu_fd_watch);
467
if (server->in_qio_channel_yield) {
469
qio_channel_wake_read(server->ioc);
474
bool vhost_user_server_start(VuServer *server,
475
SocketAddress *socket_addr,
478
const VuDevIface *vu_iface,
482
QIONetListener *listener;
484
if (socket_addr->type != SOCKET_ADDRESS_TYPE_UNIX &&
485
socket_addr->type != SOCKET_ADDRESS_TYPE_FD) {
486
error_setg(errp, "Only socket address types 'unix' and 'fd' are supported");
490
listener = qio_net_listener_new();
491
if (qio_net_listener_open_sync(listener, socket_addr, 1,
493
object_unref(OBJECT(listener));
497
bh = qemu_bh_new(restart_listener_bh, server);
500
*server = (VuServer) {
501
.listener = listener,
502
.restart_listener_bh = bh,
503
.vu_iface = vu_iface,
504
.max_queues = max_queues,
508
qio_net_listener_set_name(server->listener, "vhost-user-backend-listener");
510
qio_net_listener_set_client_func(server->listener,
515
QTAILQ_INIT(&server->vu_fd_watches);