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 5/5] -object/object-add: use custom default object location if provided
Date: Wed,  8 Jan 2014 17:09:42 +0100	[thread overview]
Message-ID: <1389197382-25085-6-git-send-email-imammedo@redhat.com> (raw)
In-Reply-To: <1389197382-25085-1-git-send-email-imammedo@redhat.com>

Add a optional OBJECT_PATH_INTERFACE, that will allow to set custom
container path if object's type implements this interface.

Signed-off-by: Igor Mammedov <imammedo@redhat.com>
---
it will be used for memdev backend to so that
 -object membackendX,id=memdevY
could create object with path /backends/memdev/memdevY,
grouping similar objects in one place and not clattering "/objects"
container.

it also could be used for iothread backend, which could use -object
instead of custom -iothread option.
---
 include/qom/object_interfaces.h |   43 +++++++++++++++++++++++++++++++++++++++
 qmp.c                           |   14 ++++++++++--
 qom/object_interfaces.c         |   24 +++++++++++++++++++++
 vl.c                            |    5 +++-
 4 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/include/qom/object_interfaces.h b/include/qom/object_interfaces.h
index 8a42d46..9c4b4b3 100644
--- a/include/qom/object_interfaces.h
+++ b/include/qom/object_interfaces.h
@@ -50,4 +50,47 @@ typedef struct ObjectRealizeInterfaceClass {
  * implements OBJECT_REALIZE_INTERFACE, otherwise the call does nothing.
  */
 void call_object_realize_interface(Object *obj, Error **errp);
+
+
+#define TYPE_OBJECT_PATH_INTERFACE "object-path-interface"
+
+#define OBJECT_PATH_INTERFACE_CLASS(klass) \
+     OBJECT_CLASS_CHECK(ObjectPathInterfaceClass, (klass), \
+                        TYPE_OBJECT_PATH_INTERFACE)
+#define OBJECT_PATH_INTERFACE_GET_CLASS(obj) \
+     OBJECT_GET_CLASS(ObjectPathInterfaceClass, (obj), \
+                      TYPE_OBJECT_PATH_INTERFACE)
+#define OBJECT_PATH_INTERFACE(obj) \
+     INTERFACE_CHECK(ObjectPathInterface, (obj), \
+                     TYPE_OBJECT_PATH_INTERFACE)
+
+typedef struct ObjectPathInterface {
+    /* <private> */
+    Object Parent;
+} ObjectPathInterface;
+
+/**
+ * ObjectPathInterfaceClass:
+ * @parent_class: the base class
+ * @default_parent: returns default parent path
+ *
+ * Interface is designed to work with -object/object-add/object_add
+ * commands and provides an optional ability for object to
+ * provide its default location in QOM tree.
+ */
+typedef struct ObjectPathInterfaceClass {
+    /* <private> */
+    InterfaceClass parent_class;
+
+    const char *(*default_parent)(ObjectPathInterface *obj);
+} ObjectPathInterfaceClass;
+
+/**
+ * object_default_parent_path:
+ * @obj: the object default_parent() method is called
+ *
+ * Wrapper to call default_parent() method if type implements
+ * OBJECT_PATH_INTERFACE, returns default parent path.
+ */
+const char *object_default_parent_path(Object *obj);
 #endif
diff --git a/qmp.c b/qmp.c
index 3ed78cd..53ee026 100644
--- a/qmp.c
+++ b/qmp.c
@@ -539,6 +539,7 @@ void object_add(const char *type, const char *id, const QDict *qdict,
     Object *obj;
     const QDictEntry *e;
     Error *local_err = NULL;
+    const char *default_path;
 
     if (!object_class_by_name(type)) {
         error_setg(errp, "invalid class name");
@@ -560,7 +561,9 @@ void object_add(const char *type, const char *id, const QDict *qdict,
         goto out;
     }
 
-    object_property_add_child(container_get(object_get_root(), "/objects"),
+    default_path = object_default_parent_path(obj);
+    default_path = default_path ? default_path : "/objects";
+    object_property_add_child(container_get(object_get_root(), default_path),
                               id, obj, &local_err);
 out:
     if (local_err) {
@@ -603,13 +606,18 @@ out:
 void qmp_object_del(const char *id, Error **errp)
 {
     Object *container;
+    bool ambiguous;
     Object *obj;
 
-    container = container_get(object_get_root(), "/objects");
-    obj = object_resolve_path_component(container, id);
+    obj = object_resolve_path(id, &ambiguous);
     if (!obj) {
         error_setg(errp, "object id not found");
         return;
     }
+    if (ambiguous) {
+        error_setg(errp, "object id is ambiguous, use absolute"
+                         " QOM path to specify the object");
+        return;
+    }
     object_unparent(obj);
 }
diff --git a/qom/object_interfaces.c b/qom/object_interfaces.c
index d2aa722..66eb052 100644
--- a/qom/object_interfaces.c
+++ b/qom/object_interfaces.c
@@ -18,6 +18,23 @@ void call_object_realize_interface(Object *obj, Error **errp)
     }
 }
 
+const char *object_default_parent_path(Object *obj)
+{
+    ObjectPathInterface *opi;
+    ObjectPathInterfaceClass *opic;
+
+    opi = (ObjectPathInterface *)
+        object_dynamic_cast(obj, TYPE_OBJECT_PATH_INTERFACE);
+    if (!opi) {
+        return NULL;
+    }
+
+    opic = OBJECT_PATH_INTERFACE_GET_CLASS(opi);
+    if (opic->default_parent) {
+        return opic->default_parent(opi);
+    }
+    return NULL;
+}
 
 static void register_types(void)
 {
@@ -27,7 +44,14 @@ static void register_types(void)
         .class_size = sizeof(ObjectRealizeInterfaceClass),
     };
 
+    static const TypeInfo path_interface_info = {
+        .name          = TYPE_OBJECT_PATH_INTERFACE,
+        .parent        = TYPE_INTERFACE,
+        .class_size = sizeof(ObjectPathInterfaceClass),
+    };
+
     type_register_static(&realize_interface_info);
+    type_register_static(&path_interface_info);
 }
 
 type_init(register_types)
diff --git a/vl.c b/vl.c
index 84cb7a8..fc2a2ca 100644
--- a/vl.c
+++ b/vl.c
@@ -2799,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);
+    const char *default_path;
     Error *local_err = NULL;
     Object *obj;
 
@@ -2820,7 +2821,9 @@ static int object_create(QemuOpts *opts, void *opaque)
         goto out;
     }
 
-    object_property_add_child(container_get(object_get_root(), "/objects"),
+    default_path = object_default_parent_path(obj);
+    default_path = default_path ? default_path : "/objects";
+    object_property_add_child(container_get(object_get_root(), default_path),
                               id, obj, &local_err);
 
 out:
-- 
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 ` [Qemu-devel] [PATCH 2/5] add optional 2nd stage initialization to -object/object-add/object_add commands Igor Mammedov
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 ` Igor Mammedov [this message]
2014-01-09  4:35   ` [Qemu-devel] [PATCH 5/5] -object/object-add: use custom default object location if provided 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-6-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).