qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Igor Mammedov <imammedo@redhat.com>
To: qemu-devel@nongnu.org
Cc: stefanha@redhat.com, sw@weilnetz.de, mjt@tls.msk.ru,
	lcapitulino@redhat.com, blauwirbel@gmail.com,
	aliguori@amazon.com, pbonzini@redhat.com, afaerber@suse.de,
	rth@twiddle.net
Subject: [Qemu-devel] [PATCH 2/5] add optional 2nd stage initialization to -object/object-add/object_add commands
Date: Wed,  8 Jan 2014 17:09:39 +0100	[thread overview]
Message-ID: <1389197382-25085-3-git-send-email-imammedo@redhat.com> (raw)
In-Reply-To: <1389197382-25085-1-git-send-email-imammedo@redhat.com>

Provides an ability to do an optional second stage initialization
of an object created with -object/object-add/object_add commands.

Patch adds interface that provides realize() callback, which is
called after the object properties were set upon completion of
-object/object-add/object_add command, if the type implements
OBJECT_REALIZE_INTERFACE.

It allows to:
 * generalize second stage backend initialization instead of adding
   custom APIs to perform it
 * early error detection of backend initialization at -object/
   object-add/object_add time rather than through a proxy DEVICE
   object that tries to use backend.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
Next patch will convert virtio_rng to a new interface as an example.
The same interface will be useful for memory backend.
---
 include/qom/object_interfaces.h |   53 +++++++++++++++++++++++++++++++++++++++
 qmp.c                           |    6 ++++
 qom/Makefile.objs               |    1 +
 qom/object_interfaces.c         |   33 ++++++++++++++++++++++++
 vl.c                            |   14 ++++++++++
 5 files changed, 107 insertions(+), 0 deletions(-)
 create mode 100644 include/qom/object_interfaces.h
 create mode 100644 qom/object_interfaces.c

diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
new file mode 100644
index 0000000..8a42d46
--- /dev/null
+++ b/include/qom/object_interfaces.h
@@ -0,0 +1,53 @@
+#ifndef OBJECT_INTERFACES_H
+#define OBJECT_INTERFACES_H
+
+#include "qom/object.h"
+
+#define TYPE_OBJECT_REALIZE_INTERFACE "object-realize-interface"
+
+#define OBJECT_REALIZE_INTERFACE_CLASS(klass) \
+     OBJECT_CLASS_CHECK(ObjectRealizeInterfaceClass, (klass), \
+                        TYPE_OBJECT_REALIZE_INTERFACE)
+#define OBJECT_REALIZE_INTERFACE_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(ObjectRealizeInterfaceClass, (obj), \
+                      TYPE_OBJECT_REALIZE_INTERFACE)
+#define OBJECT_REALIZE_INTERFACE(obj) \
+     INTERFACE_CHECK(ObjectRealizeInterface, (obj), \
+                     TYPE_OBJECT_REALIZE_INTERFACE)
+
+
+typedef struct ObjectRealizeInterface {
+    /* <private> */
+    Object Parent;
+} ObjectRealizeInterface;
+
+/**
+ * ObjectRealizeInterfaceClass:
+ * @parent_class: the base class
+ * @realize: callback to be called after @obj's properties are set.
+ *
+ * Interface is designed to work with -object/object-add/object_add
+ * commands and provides an optional ability to do the second stage
+ * initialization of the object after its properties were set.
+ *
+ * For objects created without using -object/object-add/object_add,
+ * @call_object_realize_interface should be called manually if object's
+ * type implements OBJECT_REALIZE_INTERFACE.
+ */
+typedef struct ObjectRealizeInterfaceClass {
+    /* <private> */
+    InterfaceClass parent_class;
+
+    void (*realize)(ObjectRealizeInterface *obj, Error **errp);
+} ObjectRealizeInterfaceClass;
+
+/**
+ * call_object_realize_interface:
+ * @obj: the object whose realize() method is called
+ * @errp: if an error occurs, a pointer to an area to store the error
+ *
+ * Wrapper to call realize() method if one of obj's types
+ * implements OBJECT_REALIZE_INTERFACE, otherwise the call does nothing.
+ */
+void call_object_realize_interface(Object *obj, Error **errp);
+#endif
diff --git a/qmp.c b/qmp.c
index a67e0c4..3ed78cd 100644
--- a/qmp.c
+++ b/qmp.c
@@ -27,6 +27,7 @@
 #include "qapi/qmp/qobject.h"
 #include "qapi/qmp-input-visitor.h"
 #include "hw/boards.h"
+#include "qom/object_interfaces.h"
 
 NameInfo *qmp_query_name(Error **errp)
 {
@@ -554,6 +555,11 @@ void object_add(const char *type, const char *id, const QDict *qdict,
         }
     }
 
+    call_object_realize_interface(obj, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
     object_property_add_child(container_get(object_get_root(), "/objects"),
                               id, obj, &local_err);
 out:
diff --git a/qom/Makefile.objs b/qom/Makefile.objs
index 6a93ac7..985003b 100644
--- a/qom/Makefile.objs
+++ b/qom/Makefile.objs
@@ -1,2 +1,3 @@
 common-obj-y = object.o container.o qom-qobject.o
 common-obj-y += cpu.o
+common-obj-y += object_interfaces.o
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
new file mode 100644
index 0000000..d2aa722
--- /dev/null
+++ b/qom/object_interfaces.c
@@ -0,0 +1,33 @@
+#include "qom/object_interfaces.h"
+#include "qemu/module.h"
+
+void call_object_realize_interface(Object *obj, Error **errp)
+{
+    ObjectRealizeInterface *ori;
+    ObjectRealizeInterfaceClass *oric;
+
+    ori = (ObjectRealizeInterface *)
+        object_dynamic_cast(obj, TYPE_OBJECT_REALIZE_INTERFACE);
+    if (!ori) {
+        return;
+    }
+
+    oric = OBJECT_REALIZE_INTERFACE_GET_CLASS(ori);
+    if (oric->realize) {
+        oric->realize(ori, errp);
+    }
+}
+
+
+static void register_types(void)
+{
+    static const TypeInfo realize_interface_info = {
+        .name          = TYPE_OBJECT_REALIZE_INTERFACE,
+        .parent        = TYPE_INTERFACE,
+        .class_size = sizeof(ObjectRealizeInterfaceClass),
+    };
+
+    type_register_static(&realize_interface_info);
+}
+
+type_init(register_types)
diff --git a/vl.c b/vl.c
index 45f9177..1620393 100644
--- a/vl.c
+++ b/vl.c
@@ -170,6 +170,7 @@ int main(int argc, char **argv)
 
 #include "ui/qemu-spice.h"
 #include "qapi/string-input-visitor.h"
+#include "qom/object_interfaces.h"
 
 //#define DEBUG_NET
 //#define DEBUG_SLIRP
@@ -2798,6 +2799,7 @@ static int object_create(QemuOpts *opts, void *opaque)
 {
     const char *type = qemu_opt_get(opts, "qom-type");
     const char *id = qemu_opts_id(opts);
+    Error *local_err = NULL;
     Object *obj;
 
     g_assert(type != NULL);
@@ -2813,9 +2815,21 @@ static int object_create(QemuOpts *opts, void *opaque)
         return -1;
     }
 
+    call_object_realize_interface(obj, &local_err);
+    if (local_err) {
+        goto out;
+    }
+
     object_property_add_child(container_get(object_get_root(), "/objects"),
                               id, obj, NULL);
+
+out:
     object_unref(obj);
+    if (local_err) {
+        qerror_report_err(local_err);
+        error_free(local_err);
+        return -1;
+    }
     return 0;
 }
 
-- 
1.7.1

  parent reply	other threads:[~2014-01-08 16:14 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-08 16:09 [Qemu-devel] [RFC 0/5] -object/object-add support custom location and 2nd stage initialization Igor Mammedov
2014-01-08 16:09 ` [Qemu-devel] [PATCH 1/5] object_add: consolidate error handling Igor Mammedov
2014-01-08 16:09 ` Igor Mammedov [this message]
2014-01-08 16:09 ` [Qemu-devel] [PATCH 3/5] virtio_rng: use object_realize interface instead of calling backend API Igor Mammedov
2014-01-08 16:09 ` [Qemu-devel] [PATCH 4/5] vl.c: -object: handle duplicate 'id' properly Igor Mammedov
2014-01-08 16:09 ` [Qemu-devel] [PATCH 5/5] -object/object-add: use custom default object location if provided Igor Mammedov
2014-01-09  4:35   ` Stefan Hajnoczi
2014-01-10 10:59     ` Igor Mammedov
2014-01-08 16:24 ` [Qemu-devel] [RFC 0/5] -object/object-add support custom location and 2nd stage initialization Paolo Bonzini
2014-01-08 16:45   ` Andreas Färber
2014-01-08 17:00     ` Igor Mammedov
2014-01-08 16:51   ` Igor Mammedov
2014-01-08 17:33     ` Paolo Bonzini
2014-01-10 11:28       ` Igor Mammedov
2014-01-10 11:38         ` Paolo Bonzini
2014-01-10 14:44           ` Igor Mammedov
2014-01-10 15:40             ` Paolo Bonzini
2014-01-10 15:31         ` Igor Mammedov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1389197382-25085-3-git-send-email-imammedo@redhat.com \
    --to=imammedo@redhat.com \
    --cc=afaerber@suse.de \
    --cc=aliguori@amazon.com \
    --cc=blauwirbel@gmail.com \
    --cc=lcapitulino@redhat.com \
    --cc=mjt@tls.msk.ru \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=stefanha@redhat.com \
    --cc=sw@weilnetz.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).