[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH RFC server v2 05/11] vfio-user: run vfio-user context
From: |
Jagannathan Raman |
Subject: |
[PATCH RFC server v2 05/11] vfio-user: run vfio-user context |
Date: |
Fri, 27 Aug 2021 13:53:24 -0400 |
Setup a handler to run vfio-user context. The context is driven by
messages to the file descriptor associated with it - get the fd for
the context and hook up the handler with it
Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
---
hw/remote/vfio-user-obj.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 70 insertions(+), 1 deletion(-)
diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
index 5ae0991..0726eb9 100644
--- a/hw/remote/vfio-user-obj.c
+++ b/hw/remote/vfio-user-obj.c
@@ -35,6 +35,7 @@
#include "trace.h"
#include "sysemu/runstate.h"
#include "qemu/notify.h"
+#include "qemu/thread.h"
#include "qapi/error.h"
#include "sysemu/sysemu.h"
#include "libvfio-user.h"
@@ -65,6 +66,8 @@ struct VfuObject {
vfu_ctx_t *vfu_ctx;
PCIDevice *pci_dev;
+
+ int vfu_poll_fd;
};
static void vfu_object_set_socket(Object *obj, const char *str, Error **errp)
@@ -89,13 +92,67 @@ static void vfu_object_set_devid(Object *obj, const char
*str, Error **errp)
trace_vfu_prop("devid", str);
}
+static void vfu_object_ctx_run(void *opaque)
+{
+ VfuObject *o = opaque;
+ int ret = -1;
+
+ while (ret != 0) {
+ ret = vfu_run_ctx(o->vfu_ctx);
+ if (ret < 0) {
+ if (errno == EINTR) {
+ continue;
+ } else if (errno == ENOTCONN) {
+ qemu_set_fd_handler(o->vfu_poll_fd, NULL, NULL, NULL);
+ o->vfu_poll_fd = -1;
+ object_unparent(OBJECT(o));
+ break;
+ } else {
+ error_setg(&error_abort, "vfu: Failed to run device %s - %s",
+ o->devid, strerror(errno));
+ break;
+ }
+ }
+ }
+}
+
+static void *vfu_object_attach_ctx(void *opaque)
+{
+ VfuObject *o = opaque;
+ int ret;
+
+retry_attach:
+ ret = vfu_attach_ctx(o->vfu_ctx);
+ if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
+ goto retry_attach;
+ } else if (ret < 0) {
+ error_setg(&error_abort,
+ "vfu: Failed to attach device %s to context - %s",
+ o->devid, strerror(errno));
+ return NULL;
+ }
+
+ o->vfu_poll_fd = vfu_get_poll_fd(o->vfu_ctx);
+ if (o->vfu_poll_fd < 0) {
+ error_setg(&error_abort, "vfu: Failed to get poll fd %s", o->devid);
+ return NULL;
+ }
+
+ qemu_set_fd_handler(o->vfu_poll_fd, vfu_object_ctx_run,
+ NULL, o);
+
+ return NULL;
+}
+
static void vfu_object_machine_done(Notifier *notifier, void *data)
{
VfuObject *o = container_of(notifier, VfuObject, machine_done);
DeviceState *dev = NULL;
+ QemuThread thread;
int ret;
- o->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, o->socket, 0,
+ o->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, o->socket,
+ LIBVFIO_USER_FLAG_ATTACH_NB,
o, VFU_DEV_TYPE_PCI);
if (o->vfu_ctx == NULL) {
error_setg(&error_abort, "vfu: Failed to create context - %s",
@@ -124,6 +181,16 @@ static void vfu_object_machine_done(Notifier *notifier,
void *data)
o->devid, strerror(errno));
return;
}
+
+ ret = vfu_realize_ctx(o->vfu_ctx);
+ if (ret < 0) {
+ error_setg(&error_abort, "vfu: Failed to realize device %s- %s",
+ o->devid, strerror(errno));
+ return;
+ }
+
+ qemu_thread_create(&thread, o->socket, vfu_object_attach_ctx, o,
+ QEMU_THREAD_DETACHED);
}
static void vfu_object_init(Object *obj)
@@ -147,6 +214,8 @@ static void vfu_object_init(Object *obj)
o->machine_done.notify = vfu_object_machine_done;
qemu_add_machine_init_done_notifier(&o->machine_done);
+
+ o->vfu_poll_fd = -1;
}
static void vfu_object_finalize(Object *obj)
--
1.8.3.1
- Re: [PATCH RFC v2 01/16] vfio-user: introduce vfio-user protocol specification, (continued)
[PATCH RFC v2 13/16] vfio-user: dma map/unmap operations, Elena Ufimtseva, 2021/08/16
[PATCH RFC v2 16/16] vfio-user: migration support, Elena Ufimtseva, 2021/08/16
[PATCH RFC server v2 00/11] vfio-user server in QEMU, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 01/11] vfio-user: build library, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 02/11] vfio-user: define vfio-user object, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 03/11] vfio-user: instantiate vfio-user context, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 04/11] vfio-user: find and init PCI device, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 05/11] vfio-user: run vfio-user context,
Jagannathan Raman <=
- [PATCH RFC server v2 07/11] vfio-user: handle DMA mappings, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 06/11] vfio-user: handle PCI config space accesses, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 08/11] vfio-user: handle PCI BAR accesses, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 09/11] vfio-user: handle device interrupts, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 10/11] vfio-user: register handlers to facilitate migration, Jagannathan Raman, 2021/08/27
- [PATCH RFC server v2 11/11] vfio-user: acceptance test, Jagannathan Raman, 2021/08/27