Message ID | 66bb8c18bb5372f72aaa66d62f09258ec7775100.1642626515.git.jag.raman@oracle.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | vfio-user server in QEMU | expand |
On Wed, Jan 19, 2022 at 04:41:59PM -0500, Jagannathan Raman wrote: > 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; > + } No error? > + > 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; > + } No error? > + > 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 >
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)