[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v5 10/18] vfio-user: instantiate vfio-user context
From: |
Jagannathan Raman |
Subject: |
[PATCH v5 10/18] vfio-user: instantiate vfio-user context |
Date: |
Wed, 19 Jan 2022 16:41:59 -0500 |
create a context with the vfio-user library to run a PCI device
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 | 78 +++++++++++++++++++++++++++++++++++++++
1 file changed, 78 insertions(+)
diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
index 80757b0029..810a7c3943 100644
--- a/hw/remote/vfio-user-obj.c
+++ b/hw/remote/vfio-user-obj.c
@@ -41,6 +41,9 @@
#include "hw/remote/machine.h"
#include "qapi/error.h"
#include "qapi/qapi-visit-sockets.h"
+#include "qemu/notify.h"
+#include "sysemu/sysemu.h"
+#include "libvfio-user.h"
#define TYPE_VFU_OBJECT "x-vfio-user-server"
OBJECT_DECLARE_TYPE(VfuObject, VfuObjectClass, VFU_OBJECT)
@@ -82,13 +85,23 @@ struct VfuObject {
char *device;
Error *err;
+
+ Notifier machine_done;
+
+ vfu_ctx_t *vfu_ctx;
};
+static void vfu_object_init_ctx(VfuObject *o, Error **errp);
+
static void vfu_object_set_socket(Object *obj, Visitor *v, const char *name,
void *opaque, Error **errp)
{
VfuObject *o = VFU_OBJECT(obj);
+ if (o->vfu_ctx) {
+ return;
+ }
+
qapi_free_SocketAddress(o->socket);
o->socket = NULL;
@@ -104,17 +117,68 @@ static void vfu_object_set_socket(Object *obj, Visitor
*v, const char *name,
}
trace_vfu_prop("socket", o->socket->u.q_unix.path);
+
+ vfu_object_init_ctx(o, errp);
}
static void vfu_object_set_device(Object *obj, const char *str, Error **errp)
{
VfuObject *o = VFU_OBJECT(obj);
+ if (o->vfu_ctx) {
+ return;
+ }
+
g_free(o->device);
o->device = g_strdup(str);
trace_vfu_prop("device", str);
+
+ vfu_object_init_ctx(o, errp);
+}
+
+/*
+ * TYPE_VFU_OBJECT depends on the availability of the 'socket' and 'device'
+ * properties. It also depends on devices instantiated in QEMU. These
+ * dependencies are not available during the instance_init phase of this
+ * object's life-cycle. As such, the server is initialized after the
+ * machine is setup. machine_init_done_notifier notifies TYPE_VFU_OBJECT
+ * when the machine is setup, and the dependencies are available.
+ */
+static void vfu_object_machine_done(Notifier *notifier, void *data)
+{
+ VfuObject *o = container_of(notifier, VfuObject, machine_done);
+ Error *err = NULL;
+
+ vfu_object_init_ctx(o, &err);
+
+ if (err) {
+ error_propagate(&error_abort, err);
+ }
+}
+
+static void vfu_object_init_ctx(VfuObject *o, Error **errp)
+{
+ ERRP_GUARD();
+
+ if (o->vfu_ctx || !o->socket || !o->device ||
+ !phase_check(PHASE_MACHINE_READY)) {
+ return;
+ }
+
+ if (o->err) {
+ error_propagate(errp, o->err);
+ o->err = NULL;
+ return;
+ }
+
+ o->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, o->socket->u.q_unix.path, 0,
+ o, VFU_DEV_TYPE_PCI);
+ if (o->vfu_ctx == NULL) {
+ error_setg(errp, "vfu: Failed to create context - %s",
strerror(errno));
+ return;
+ }
}
static void vfu_object_init(Object *obj)
@@ -124,6 +188,11 @@ static void vfu_object_init(Object *obj)
k->nr_devs++;
+ if (!phase_check(PHASE_MACHINE_READY)) {
+ o->machine_done.notify = vfu_object_machine_done;
+ qemu_add_machine_init_done_notifier(&o->machine_done);
+ }
+
if (!object_dynamic_cast(OBJECT(current_machine), TYPE_REMOTE_MACHINE)) {
error_setg(&o->err, "vfu: %s only compatible with %s machine",
TYPE_VFU_OBJECT, TYPE_REMOTE_MACHINE);
@@ -142,6 +211,10 @@ static void vfu_object_finalize(Object *obj)
o->socket = NULL;
+ if (o->vfu_ctx) {
+ vfu_destroy_ctx(o->vfu_ctx);
+ }
+
g_free(o->device);
o->device = NULL;
@@ -149,6 +222,11 @@ static void vfu_object_finalize(Object *obj)
if (!k->nr_devs && k->auto_shutdown) {
qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
}
+
+ if (o->machine_done.notify) {
+ qemu_remove_machine_init_done_notifier(&o->machine_done);
+ o->machine_done.notify = NULL;
+ }
}
static void vfu_object_class_init(ObjectClass *klass, void *data)
--
2.20.1
- Re: [PATCH v5 06/18] vfio-user: add HotplugHandler for remote machine, (continued)
- [PATCH v5 04/18] pci: create and free isolated PCI buses, Jagannathan Raman, 2022/01/19
- [PATCH v5 07/18] vfio-user: set qdev bus callbacks for remote machine, Jagannathan Raman, 2022/01/19
- [PATCH v5 09/18] vfio-user: define vfio-user-server object, Jagannathan Raman, 2022/01/19
- [PATCH v5 08/18] vfio-user: build library, Jagannathan Raman, 2022/01/19
- [PATCH v5 10/18] vfio-user: instantiate vfio-user context,
Jagannathan Raman <=
- [PATCH v5 11/18] vfio-user: find and init PCI device, Jagannathan Raman, 2022/01/19
- [PATCH v5 13/18] vfio-user: handle PCI config space accesses, Jagannathan Raman, 2022/01/19
- [PATCH v5 12/18] vfio-user: run vfio-user context, Jagannathan Raman, 2022/01/19
- [PATCH v5 15/18] vfio-user: handle PCI BAR accesses, Jagannathan Raman, 2022/01/19