Skip to contentSkip to conversation

vhost-user: another try at supporting any POSIX system

PATCH In review Tim Pambor 2 participants 11 messages Oct 30 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

Tim Pambor

author
#0 lore raw

v2 cover vhost-user: another try at supporting any POSIX system

This series continues the work started by Stefano [1] to add support for any POSIX system to vhost-user.

Three patches from his original series were never applied due to failing tests [2]. I was able to reproduce these test failures on current master.

Patch 6 contains a fix for this problem. Patches 1-5 fix further issues I came across while testing, mostly on macOS. Patches 7-9 are the three patches from Stefano's series; patch 7 drops VHOST_FILE_UNBIND as discussed in v1, the other two are unmodified.

I have tested this patch series on Ubuntu 24.04, macOS 15, FreeBSD 14.2 and OpenBSD 7.7. On all platforms the tests are now passing. In addition, on these platforms I have tested successfully booting a Fedora image with

  qemu-storage-daemon \
    --blockdev file,filename=Fedora-Cloud-Base-Generic-44-1.7.x86_64.qcow2,node-name=file \
    --blockdev qcow2,file=file,node-name=qcow2 \
    --export vhost-user-blk,addr.type=unix,addr.path=/tmp/vhost.socket,id=vub,num-queues=1,node-name=qcow2,writable=on

  qemu-system-x86_64 -smp 2 -M q35,memory-backend=mem \
    -object memory-backend-shm,id=mem,size="512M" \
    -device vhost-user-blk-pci,num-queues=1,chardev=char0 \
    -chardev socket,id=char0,path=/tmp/vhost.socket

Full CI run: https://gitlab.com/tpambor/qemu/-/pipelines/3007880563

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/suvpzkb3ppdodjdyo5zcpngz5mwrrlq3nfowemh4tqjghbc4si@hzrsr23fganv/

---
Changes in v2:
- Moved "libvhost-user: detect memfd support with a link test" to the
front of the series.
- Added "libvhost-user: detect eventfd support with a link test".
- Replaced the library-owned self-kick notifier in libvhost-user with a
per-ring flag; the handlers of started rings now run at the end of
vu_dispatch(), after the reply has been sent.
- Dropped "io/channel-socket: only clear O_NONBLOCK on received fds if
set" and instead reimplemented qemu_set_blocking() with fcntl(), so
F_SETFL is only issued when O_NONBLOCK actually changes.
- vhost_net.c: replaced VHOST_FILE_UNBIND with -1 and dropped the
Linux header include instead of defining the constant for other
systems.
- Added a comment and an assert to "vhost-user: kick via
event_notifier_set()".
- New CI run: https://gitlab.com/tpambor/qemu/-/pipelines/3007880563
- Link to v1: https://lore.kernel.org/qemu-devel/[email protected]

To: [email protected]
Cc: "Michael S. Tsirkin" <[email protected]>
Cc: Stefano Garzarella <[email protected]>
Cc: "Daniel P. Berrangé" <[email protected]>
Cc: Marc-André Lureau <[email protected]>
Cc: Paolo Bonzini <[email protected]>
Cc: Kevin Wolf <[email protected]>
Cc: Hanna Reitz <[email protected]>
Cc: Jason Wang <[email protected]>
Cc: Pierrick Bouvier <[email protected]>
Cc: [email protected]
Cc: Raphael Norwitz <[email protected]>
Cc: Brad Smith <[email protected]>
Cc: [email protected]
Cc: Philippe Mathieu-Daudé <[email protected]>
Cc: Stefan Hajnoczi <[email protected]>

---
Stefano Garzarella (2):
libvhost-user: enable it on any POSIX system contrib/vhost-user-blk: enable it on any POSIX system

Tim Pambor (7):
libvhost-user: detect memfd support with a link test
libvhost-user: detect eventfd support with a link test
vhost-user: kick via event_notifier_set()
libvhost-user: run handlers of started rings after the reply util/oslib-posix: implement qemu_set_blocking() with fcntl() chardev/char-socket: detect peer close portably
vhost-user: enable frontends on any POSIX system

 chardev/char-socket.c                     |  78 +++++++++++++++-----
 contrib/vhost-user-blk/vhost-user-blk.c   |  14 ++++
 hw/block/Kconfig                          |   2 +-
 hw/net/vhost_net.c                        |   3 +-
 hw/virtio/vhost-user.c                    |  19 ++---
 meson.build                               |   5 +-
 subprojects/libvhost-user/libvhost-user.c | 118 ++++++++++++++++++++++++------
 subprojects/libvhost-user/libvhost-user.h |   3 +-
 subprojects/libvhost-user/meson.build     |  17 ++++-
 util/meson.build                          |   4 +-
 util/oslib-posix.c                        |  26 +++++--
 11 files changed, 221 insertions(+), 68 deletions(-)

---
base-commit: a19f59a72d96056d9a8f48cf6c77f5787491e414
change-id: 20261022-vhost-user-posix-e7b999aa751d

Best regards,

Signature · 1 line
Tim Pambor <[email protected]>
author
#1 lore raw

PATCH v2 1/9 libvhost-user: detect memfd support with a link test

The inflight fd code is guarded by #ifdef MFD_ALLOW_SEALING, which only tells us that the headers define the flag, not that memfd_create() is available in the C library. There are environments where the flag is defined but the link fails:

ld: error: libvhost-user.a(libvhost-user.c.o): undefined symbol: memfd_create

Probe for memfd_create() with MFD_ALLOW_SEALING in the subproject's meson.build, using the same test QEMU's top-level meson.build uses for CONFIG_MEMFD, and guard the code with CONFIG_MEMFD instead.

When memfd is not available, VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD is masked out as before, so the frontend never sends VHOST_USER_GET_INFLIGHT_FD.

---
 subprojects/libvhost-user/libvhost-user.c | 15 +++++++--------
 subprojects/libvhost-user/meson.build     | 11 ++++++++++-
 2 files changed, 17 insertions(+), 9 deletions(-)
subprojects/libvhost-user/libvhost-user.c +7 -8
index 2fed792e85..adda700630 100644
@@ -1800,13 +1800,12 @@ vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg)
         features |= dev->iface->get_protocol_features(dev);
     }
 
-#ifndef MFD_ALLOW_SEALING
+#ifndef CONFIG_MEMFD
     /*
-     * If MFD_ALLOW_SEALING is not defined, we are not able to handle
-     * VHOST_USER_GET_INFLIGHT_FD messages, since we can't create a memfd.
-     * Those messages are used only if VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD
-     * is negotiated. A device implementation can enable it, so let's mask
-     * it to avoid a runtime panic.
+     * Without memfd we are not able to handle VHOST_USER_GET_INFLIGHT_FD
+     * messages. Those messages are used only if
+     * VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD is negotiated. A device
+     * implementation can enable it, so let's mask it to avoid a runtime panic.
      */
     features &= ~(1ULL << VHOST_USER_PROTOCOL_F_INFLIGHT_SHMFD);
 #endif
@@ -2001,7 +2000,7 @@ vu_inflight_queue_size(uint16_t queue_size)
            sizeof(uint16_t), INFLIGHT_ALIGNMENT);
 }
 
-#ifdef MFD_ALLOW_SEALING
+#ifdef CONFIG_MEMFD
 static void *
 memfd_alloc(const char *name, size_t size, unsigned int flags, int *fd)
 {
@@ -2068,7 +2067,7 @@ vu_get_inflight_fd(VuDev *dev, VhostUserMsg *vmsg)
 
     mmap_size = vu_inflight_queue_size(queue_size) * num_queues;
 
-#ifdef MFD_ALLOW_SEALING
+#ifdef CONFIG_MEMFD
     addr = memfd_alloc("vhost-inflight", mmap_size,
                        F_SEAL_GROW | F_SEAL_SHRINK | F_SEAL_SEAL,
                        &fd);
subprojects/libvhost-user/meson.build +10 -1
index a18014e7f2..aa5894a642 100644
@@ -11,10 +11,19 @@ add_project_arguments(cc.get_supported_arguments('-Wsign-compare',
 threads = dependency('threads')
 glib = dependency('glib-2.0')
 
+vhost_user_flags = ['-D_GNU_SOURCE']
+
+if cc.links('''
+    #define _GNU_SOURCE
+    #include <sys/mman.h>
+    int main(void) { return memfd_create("foo", MFD_ALLOW_SEALING); }''')
+  vhost_user_flags += '-DCONFIG_MEMFD'
+endif
+
 vhost_user = static_library('vhost-user',
                             files('libvhost-user.c'),
                             dependencies: threads,
-                            c_args: '-D_GNU_SOURCE')
+                            c_args: vhost_user_flags)
 
 executable('link-test', files('link-test.c'),
            link_whole: vhost_user)
Signature · 1 line
2.55.0
author
#2 lore raw

PATCH v2 2/9 libvhost-user: detect eventfd support with a link test

In reply to Tim Pambor

libvhost-user includes <sys/eventfd.h> unconditionally and uses eventfd_read() and eventfd_write(). The header is not available on every POSIX system, e.g. macOS, so the build fails there:

libvhost-user.c:31:10: fatal error: 'sys/eventfd.h' file not found

Probe for eventfd() in the subproject's meson.build, using the same test QEMU's top-level meson.build uses for CONFIG_EVENTFD, and only include the header when CONFIG_EVENTFD is set.

Without eventfd the front-end falls back to pipes, so provide eventfd_read() and eventfd_write() replacements with plain read() and write().

---
 subprojects/libvhost-user/libvhost-user.c | 17 ++++++++++++++++-
 subprojects/libvhost-user/meson.build     |  6 ++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
subprojects/libvhost-user/libvhost-user.c +16 -1
index adda700630..ece2bfcdd7 100644
@@ -28,7 +28,6 @@
 #include <inttypes.h>
 #include <sys/types.h>
 #include <sys/socket.h>
-#include <sys/eventfd.h>
 #include <sys/mman.h>
 #include <endian.h>
 
@@ -52,6 +51,22 @@
 
 #endif
 
+#ifdef CONFIG_EVENTFD
+#include <sys/eventfd.h>
+#else
+#define eventfd_t uint64_t
+
+int eventfd_write(int fd, eventfd_t value)
+{
+    return (write(fd, &value, sizeof(value)) == sizeof(value)) ? 0 : -1;
+}
+
+int eventfd_read(int fd, eventfd_t *value)
+{
+    return (read(fd, value, sizeof(*value)) == sizeof(*value)) ? 0 : -1;
+}
+#endif
+
 #include "include/atomic.h"
 
 #include "libvhost-user.h"
subprojects/libvhost-user/meson.build +6 -0
index aa5894a642..2f9aedb208 100644
@@ -20,6 +20,12 @@ if cc.links('''
   vhost_user_flags += '-DCONFIG_MEMFD'
 endif
 
+if cc.links('''
+    #include <sys/eventfd.h>
+    int main(void) { return eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); }''')
+  vhost_user_flags += '-DCONFIG_EVENTFD'
+endif
+
 vhost_user = static_library('vhost-user',
                             files('libvhost-user.c'),
                             dependencies: threads,
Signature · 1 line
2.55.0
author
#3 lore raw

PATCH v2 3/9 vhost-user: kick via event_notifier_set()

In reply to Tim Pambor

vhost-user injects a kick by writing to the fd it has just passed to the back-end. With an eventfd that fd is writable, but when pipes are used it is the read end and the write fails.

Instead, kick via event_notifier_set() on the queue's host notifier, which writes to the correct end in both cases.

---
Changes in v2:
- Add comment about eventfd vs pipe
- Add assert file->fd == event_notifier_get_fd()

---
 hw/virtio/vhost-user.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
hw/virtio/vhost-user.c +10 -9
index 65889b0776..868b3d25ca 100644
@@ -1527,16 +1527,17 @@ static int vhost_user_set_vring_kick(struct vhost_dev *dev,
      * receiving a kick. The spec suggests this to improve compatibility.
      */
     if (file->fd != -1) {
-        uint64_t val = 1;
-        ssize_t nwritten;
-
-        do {
-            nwritten = write(file->fd, &val, sizeof(val));
-        } while (nwritten < 0 && errno == EINTR);
+        /*
+         * With an eventfd that fd is writable, but when pipes are used it is
+         * the read end and the write fails. Instead, kick via
+         * event_notifier_set() on the queue's host notifier, which writes
+         * to the correct end in both cases.
+         */
+        VirtQueue *vq = virtio_get_queue(dev->vdev, file->index);
+        EventNotifier *n = virtio_queue_get_host_notifier(vq);
 
-        if (nwritten < 0 && errno != EAGAIN /* back-end can already read */) {
-            return -errno;
-        }
+        assert(file->fd == event_notifier_get_fd(n));
+        return event_notifier_set(n);
     }
 
     return 0;
Signature · 1 line
2.55.0
author
#4 lore raw

PATCH v2 4/9 libvhost-user: run handlers of started rings after the reply

In reply to Tim Pambor

When a ring is started, it may already hold requests: ones the driver queued while the back-end was away and ones the previous instance left in flight. libvhost-user handled them by writing to the kick fd it had just received, which only works when that fd is an eventfd. With pipes it is the read end, so the back-end was left relying on the front-end sending a kick after SET_VRING_KICK.

Instead, flag the ring in vu_set_vring_kick_exec() and run the handler of every flagged ring at the end of vu_dispatch(), once the reply has gone out. This needs no writable fd and works the same with eventfds and pipes.

---
 subprojects/libvhost-user/libvhost-user.c | 44 ++++++++++++++++++++++---------
 subprojects/libvhost-user/libvhost-user.h |  1 +
 2 files changed, 32 insertions(+), 13 deletions(-)
subprojects/libvhost-user/libvhost-user.c +31 -13
index ece2bfcdd7..5f649a3d52 100644
@@ -1320,6 +1320,7 @@ vu_get_vring_base_exec(VuDev *dev, VhostUserMsg *vmsg)
     vmsg->size = sizeof(vmsg->payload.state);
 
     dev->vq[index].started = false;
+    dev->vq[index].kick_pending = false;
     if (dev->iface->queue_set_started) {
         dev->iface->queue_set_started(dev, index, false);
     }
@@ -1493,19 +1494,12 @@ vu_set_vring_kick_exec(VuDev *dev, VhostUserMsg *vmsg)
         vu_panic(dev, "Failed to check inflights for vq: %d\n", index);
     }
 
-    /* Inject a kick to look for available vq buffers */
-    if (dev->vq[index].kick_fd != -1) {
-        int ret;
-
-        do {
-            ret = eventfd_write(dev->vq[index].kick_fd, 1);
-        } while (ret != 0 && errno == EINTR);
-
-        if (ret != 0 && errno != EAGAIN /* already readable */) {
-            vu_panic(dev, "Failed to inject kick during SET_VRING_KICK "
-                     "on vq: %d with error: %m\n", index);
-        }
-    }
+    /*
+     * The ring may already hold requests: ones the driver queued while the
+     * back-end was away and ones the previous instance left in flight. Run
+     * the handler once the reply to this message has gone out.
+     */
+    dev->vq[index].kick_pending = true;
 
     return false;
 }
@@ -2313,6 +2307,25 @@ vu_process_message(VuDev *dev, VhostUserMsg *vmsg)
     return false;
 }
 
+static void
+vu_run_pending_handlers(VuDev *dev)
+{
+    unsigned int i;
+
+    for (i = 0; i < dev->max_queues && !dev->broken; i++) {
+        VuVirtq *vq = &dev->vq[i];
+
+        /* Without a handler the request stays pending until one is set */
+        if (!vq->kick_pending || !vq->handler) {
+            continue;
+        }
+        vq->kick_pending = false;
+
+        DPRINT("Running handler for pending requests on vq: %u\n", i);
+        vq->handler(dev, i);
+    }
+}
+
 bool
 vu_dispatch(VuDev *dev)
 {
@@ -2345,6 +2358,11 @@ vu_dispatch(VuDev *dev)
 
 end:
     free(vmsg.data);
+
+    if (success) {
+        vu_run_pending_handlers(dev);
+    }
+
     return success;
 }
 
subprojects/libvhost-user/libvhost-user.h +1 -0
index a534d70771..aedb1da96d 100644
@@ -392,6 +392,7 @@ typedef struct VuVirtq {
     int err_fd;
     unsigned int enable;
     bool started;
+    bool kick_pending;
 
     /* Guest addresses of our ring */
     struct vhost_vring_addr vra;
Signature · 1 line
2.55.0
author
#5 lore raw

PATCH v2 5/9 util/oslib-posix: implement qemu_set_blocking() with fcntl()

In reply to Tim Pambor

qemu_set_blocking() relies on g_unix_set_fd_nonblocking(), which always issues fcntl(F_SETFL) even if O_NONBLOCK already has the requested value. macOS rejects F_SETFL on POSIX shared memory descriptors. The socket channel treats this like a read error and disconnects. Those descriptors are passed in blocking mode anyway, so there is nothing to change on them. Query the flags with F_GETFL first and only issue F_SETFL when O_NONBLOCK is actually set.

---
 util/oslib-posix.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)
util/oslib-posix.c +19 -7
index dc001da66d..3d5bb16ca6 100644
@@ -252,17 +252,29 @@ void qemu_anon_ram_free(void *ptr, size_t size)
 
 bool qemu_set_blocking(int fd, bool block, Error **errp)
 {
-    g_autoptr(GError) err = NULL;
+    int flags, new_flags;
 
-    if (!g_unix_set_fd_nonblocking(fd, !block, &err)) {
-        error_setg_errno(errp, errno,
-                         "Can't set file descriptor %d %s: %s", fd,
-                         block ? "blocking" : "non-blocking",
-                         err->message);
-        return false;
+    flags = fcntl(fd, F_GETFL);
+    if (flags < 0) {
+        goto fail;
+    }
+
+    if (block) {
+        new_flags = flags & ~O_NONBLOCK;
+    } else {
+        new_flags = flags | O_NONBLOCK;
+    }
+
+    if (new_flags != flags && fcntl(fd, F_SETFL, new_flags) < 0) {
+        goto fail;
     }
 
     return true;
+
+fail:
+    error_setg_errno(errp, errno, "Can't set file descriptor %d %s", fd,
+                     block ? "blocking" : "non-blocking");
+    return false;
 }
 
 int socket_set_fast_reuse(int fd)
Signature · 1 line
2.55.0
author
#6 lore raw

PATCH v2 6/9 chardev/char-socket: detect peer close portably

In reply to Tim Pambor

A hang-up watch for G_IO_HUP alone is not a portable disconnect detector. Linux wakes such a poll() when the peer closes, but FreeBSD registers a wakeup only for read/write interest and macOS drops its one-shot read filter on any non-EOF event, so as long as nobody polls the socket for readability the hang-up is not noticed until something unrelated wakes the main loop. Frontends that never read from the chardev, such as vhost-user, are the ones without a G_IO_IN watch, and that is what makes the vhost-user/reconnect qtest fail under load on FreeBSD with "wait_for_fds: assertion failed: (s->fds_num)".

While the frontend has no read handler at all, also ask for G_IO_IN on the hang-up watch and treat EOF as a hang-up.

---
 chardev/char-socket.c | 78 ++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 59 insertions(+), 19 deletions(-)
chardev/char-socket.c +59 -19
index 81bac42a15..62e8871c51 100644
@@ -36,11 +36,14 @@
 #include "trace.h"
 
 #include "chardev/char-io.h"
+#include "chardev/char-fe.h"
 #include "chardev/char-socket.h"
 
 static gboolean socket_reconnect_timeout(gpointer opaque);
 static void tcp_chr_telnet_init(Chardev *chr);
 static char *qemu_chr_compute_filename(SocketChardev *s);
+static gboolean tcp_chr_hup(QIOChannel *channel, GIOCondition cond,
+                            void *opaque);
 
 static void tcp_chr_change_state(SocketChardev *s, TCPChardevState state)
 {
@@ -345,6 +348,33 @@ static void remove_hup_source(SocketChardev *s)
     }
 }
 
+static void add_hup_source(SocketChardev *s, GIOCondition cond)
+{
+    Chardev *chr = CHARDEV(s);
+
+    remove_hup_source(s);
+    s->hup_source = qio_channel_create_watch(s->ioc, cond);
+    /*
+     * poll() is liable to return POLLHUP even when there is
+     * still incoming data available to read on the FD. If
+     * we have the hup_source at the same priority as the
+     * main io_add_watch_poll GSource, then we might end up
+     * processing the POLLHUP event first, closing the FD,
+     * and as a result silently discard data we should have
+     * read.
+     *
+     * By setting the hup_source to G_PRIORITY_DEFAULT + 1,
+     * we ensure that io_add_watch_poll GSource will always
+     * be dispatched first, thus guaranteeing we will be
+     * able to process all incoming data before closing the
+     * FD
+     */
+    g_source_set_priority(s->hup_source, G_PRIORITY_DEFAULT + 1);
+    g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
+                          chr, NULL);
+    g_source_attach(s->hup_source, chr->gcontext);
+}
+
 static void char_socket_yank_iochannel(void *opaque)
 {
     QIOChannel *ioc = QIO_CHANNEL(opaque);
@@ -520,6 +550,30 @@ static gboolean tcp_chr_hup(QIOChannel *channel,
                                void *opaque)
 {
     Chardev *chr = CHARDEV(opaque);
+    SocketChardev *s = SOCKET_CHARDEV(opaque);
+
+    if (!(cond & G_IO_HUP)) {
+        /* Readable while nobody reads: only EOF counts as a hang-up. */
+        int flags = MSG_PEEK;
+        ssize_t n;
+        char c;
+
+#ifdef MSG_DONTWAIT
+        flags |= MSG_DONTWAIT;
+#endif
+
+        n = recv(s->sioc->fd, &c, 1, flags);
+
+        if (n < 0 && (errno == EAGAIN || errno == EINTR)) {
+            return G_SOURCE_CONTINUE;
+        }
+
+        if (n > 0) {
+            /* Data nobody will read: stop watching for it, or we spin. */
+            add_hup_source(s, G_IO_HUP);
+            return G_SOURCE_REMOVE;
+        }
+    }
     trace_chr_socket_hangup(chr, chr->label);
     tcp_chr_disconnect(chr);
     return G_SOURCE_REMOVE;
@@ -611,27 +665,13 @@ static void update_ioc_handlers(SocketChardev *s)
                                      tcp_chr_read, chr,
                                      chr->gcontext);
 
-    remove_hup_source(s);
-    s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
     /*
-     * poll() is liable to return POLLHUP even when there is
-     * still incoming data available to read on the FD. If
-     * we have the hup_source at the same priority as the
-     * main io_add_watch_poll GSource, then we might end up
-     * processing the POLLHUP event first, closing the FD,
-     * and as a result silently discard data we should have
-     * read.
-     *
-     * By setting the hup_source to G_PRIORITY_DEFAULT + 1,
-     * we ensure that io_add_watch_poll GSource will always
-     * be dispatched first, thus guaranteeing we will be
-     * able to process all incoming data before closing the
-     * FD
+     * A G_IO_HUP-only poll() is not woken by a peer close on FreeBSD
+     * nor reliably on macOS.  Therefore, while nobody reads, ask for
+     * G_IO_IN too and let tcp_chr_hup tell EOF from data.
      */
-    g_source_set_priority(s->hup_source, G_PRIORITY_DEFAULT + 1);
-    g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
-                          chr, NULL);
-    g_source_attach(s->hup_source, chr->gcontext);
+    add_hup_source(s, G_IO_HUP |
+                   (chr->fe && chr->fe->chr_can_read ? 0 : G_IO_IN));
 }
 
 static void tcp_chr_connect(void *opaque)
Signature · 1 line
2.55.0
author
#7 lore raw

PATCH v2 7/9 vhost-user: enable frontends on any POSIX system

In reply to Tim Pambor

The vhost-user protocol is not really Linux-specific so let's enable vhost-user frontends for any POSIX system.

vhost_net.c uses VHOST_FILE_UNBIND, which is defined in a Linux specific header. The kernel's VHOST_NET_SET_BACKEND documentation asks to pass fd -1 to unbind, so replace the macro with -1 and drop the include, nothing else in the file needs it. Long-term a constant could be introduced and used for it.

---
Changes in v2:
- Replaced VHOST_FILE_UNBIND with -1 and dropped the include

---
 hw/block/Kconfig   | 2 +-
 hw/net/vhost_net.c | 3 +--
 meson.build        | 1 -
 3 files changed, 2 insertions(+), 4 deletions(-)
hw/block/Kconfig +1 -1
index 737dbcdb3e..7edcd7a8f6 100644
@@ -28,7 +28,7 @@ config VHOST_USER_BLK
     bool
     # Only PCI devices are provided for now
     default y if VIRTIO_PCI
-    depends on VIRTIO && VHOST_USER && LINUX
+    depends on VIRTIO && VHOST_USER
 
 config SWIM
     bool
hw/net/vhost_net.c +1 -2
index 6e05c995f1..d0f1014ce1 100644
@@ -33,7 +33,6 @@
 #include "standard-headers/linux/virtio_ring.h"
 #include "hw/virtio/vhost.h"
 #include "hw/virtio/virtio-bus.h"
-#include "linux-headers/linux/vhost.h"
 
 void vhost_net_get_features_ex(struct vhost_net *net, uint64_t *features)
 {
@@ -681,7 +680,7 @@ err_start:
     error_report("Error when restarting the queue.");
 
     if (net->nc->info->type == NET_CLIENT_DRIVER_TAP) {
-        file.fd = VHOST_FILE_UNBIND;
+        file.fd = -1;
         file.index = idx;
         int ret = vhost_net_set_backend(&net->dev, &file);
         assert(ret >= 0);
meson.build +0 -1
index b67a97ab45..eb296346a8 100644
@@ -221,7 +221,6 @@ have_tpm = get_option('tpm') \
 
 # vhost
 have_vhost_user = get_option('vhost_user') \
-  .disable_auto_if(host_os != 'linux') \
   .require(host_os != 'windows',
            error_message: 'vhost-user is not available on Windows').allowed()
 have_vhost_vdpa = get_option('vhost_vdpa') \
Signature · 1 line
2.55.0
Reviewed-by on v2 7/9
#8 lore raw

Re: [PATCH v2 7/9] vhost-user: enable frontends on any POSIX system

Quoted 11 lines · Tim Pambor

The vhost-user protocol is not really Linux-specific so let's enable vhost-user frontends for any POSIX system.

vhost_net.c uses VHOST_FILE_UNBIND, which is defined in a Linux specific header. The kernel's VHOST_NET_SET_BACKEND documentation asks to pass fd -1 to unbind, so replace the macro with -1 and drop the include, nothing else in the file needs it. Long-term a constant could be introduced and used for it.

Quoted 8 lines

---
Changes in v2:
- Replaced VHOST_FILE_UNBIND with -1 and dropped the include

---
 hw/block/Kconfig   | 2 +-
 hw/net/vhost_net.c | 3 +--
 meson.build        | 1 -
 3 files changed, 2 insertions(+), 4 deletions(-)
author
#9 lore raw

PATCH v2 8/9 libvhost-user: enable it on any POSIX system

In reply to Tim Pambor

From: Stefano Garzarella <[email protected]>

The vhost-user protocol is not really Linux-specific so let's enable libvhost-user for any POSIX system.

Compiling it on macOS and FreeBSD some problems came up:
- avoid to include linux/vhost.h which is available only on Linux (vhost_types.h contains many of the things we need)
- macOS doesn't provide sys/endian.h, so let's define them (note: libvhost-user doesn't include QEMU's headers, so we can't use use "qemu/bswap.h")
- copy SEAL defines from include/qemu/memfd.h to make the code works on FreeBSD where MFD_ALLOW_SEALING is defined
- define MAP_NORESERVE if it's not defined (e.g. on FreeBSD)

---
 meson.build                               |  2 +-
 subprojects/libvhost-user/libvhost-user.c | 42 ++++++++++++++++++++++++++++++-
 subprojects/libvhost-user/libvhost-user.h |  2 +-
 3 files changed, 43 insertions(+), 3 deletions(-)
meson.build +1 -1
index eb296346a8..4f4068e7a7 100644
@@ -3468,7 +3468,7 @@ if have_system and vfio_user_server_allowed
 endif
 
 vhost_user = not_found
-if host_os == 'linux' and have_vhost_user
+if have_vhost_user
   libvhost_user = subproject('libvhost-user')
   vhost_user = libvhost_user.get_variable('vhost_user_dep')
 endif
subprojects/libvhost-user/libvhost-user.c +41 -1

66 lines not on this page · raw message

subprojects/libvhost-user/libvhost-user.h +1 -1
index aedb1da96d..a2f0add832 100644
@@ -18,9 +18,9 @@
 #include <stdbool.h>
 #include <stddef.h>
 #include <poll.h>
-#include <linux/vhost.h>
 #include <pthread.h>
 #include "standard-headers/linux/virtio_ring.h"
+#include "standard-headers/linux/vhost_types.h"
 
 /* Based on qemu/hw/virtio/vhost-user.c */
 #define VHOST_USER_F_PROTOCOL_FEATURES 30
Signature · 1 line
2.55.0
author
#10 lore raw

PATCH v2 9/9 contrib/vhost-user-blk: enable it on any POSIX system

In reply to Tim Pambor

From: Stefano Garzarella <[email protected]>

Let's make the code more portable by adding defines from block/file-posix.c to support O_DIRECT in other systems (e.g. macOS).

vhost-user-server.c is a dependency, let's enable it for any POSIX system.

---
 contrib/vhost-user-blk/vhost-user-blk.c | 14 ++++++++++++++
 meson.build                             |  2 --
 util/meson.build                        |  4 +++-
 3 files changed, 17 insertions(+), 3 deletions(-)
contrib/vhost-user-blk/vhost-user-blk.c +14 -0
index 6cc18a1c04..419ad2a10d 100644
@@ -25,6 +25,20 @@
 #include <sys/ioctl.h>
 #endif
 
+/* OS X does not have O_DSYNC */
+#ifndef O_DSYNC
+#ifdef O_SYNC
+#define O_DSYNC O_SYNC
+#elif defined(O_FSYNC)
+#define O_DSYNC O_FSYNC
+#endif
+#endif
+
+/* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
+#ifndef O_DIRECT
+#define O_DIRECT O_DSYNC
+#endif
+
 enum {
     VHOST_USER_BLK_MAX_QUEUES = 8,
 };
meson.build +0 -2
index 4f4068e7a7..1250d467a0 100644
@@ -2177,8 +2177,6 @@ osdep_prefix = '''
 '''
 
 have_vhost_user_blk_server = get_option('vhost_user_blk_server') \
-  .require(host_os == 'linux',
-           error_message: 'vhost_user_blk_server requires linux') \
   .require(have_vhost_user,
            error_message: 'vhost_user_blk_server requires vhost-user support') \
   .disable_auto_if(not have_tools and not have_system) \
util/meson.build +3 -1
index fa174c07a5..54ca587362 100644
@@ -105,10 +105,12 @@ if have_block
     util_ss.add(files('filemonitor-stub.c'))
   endif
   if host_os == 'linux'
-    util_ss.add(files('vhost-user-server.c'), vhost_user)
     util_ss.add(files('vfio-helpers.c'))
     util_ss.add(files('chardev_open.c'))
   endif
+  if host_os != 'windows'
+    util_ss.add(files('vhost-user-server.c'), vhost_user)
+  endif
   util_ss.add(files('yank.c'))
 endif
 
Signature · 1 line
2.55.0