All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Liguori <aliguori@us.ibm.com>
To: qemu-devel@nongnu.org
Cc: Anthony Liguori <aliguori@us.ibm.com>
Subject: [Qemu-devel] [PATCH 15/21] qom: add Device class
Date: Sun, 24 Jul 2011 20:44:47 -0500	[thread overview]
Message-ID: <1311558293-5855-16-git-send-email-aliguori@us.ibm.com> (raw)
In-Reply-To: <1311558293-5855-1-git-send-email-aliguori@us.ibm.com>

Device is meant to replace DeviceState as the root class for the device model.
This is included here merely as a RFC.  Device adds a couple of useful features.

1) Default hard reset.  Device will literally call finalize on the object and
   then reinitialize it in place.  This means that most devices don't have to
   worry about implementing reset logic.

   Reset preserves the current state of properties which makes it equivalent to
   taking the properties of the current device and then initializing a new
   object using those properties.

2) Full object serialization as a property.  The 'state' property is special in
   that it will invoke a visit method that's intended to be overridden by a
   subclass.  The visit method will visit the full object.

   This exists to enable live migration.  Notice that there is no mention of
   compatibility, versioning, subsections, etc.  The idea behind supporting
   robust migration is that the device model is only responsible for generating
   the state of the current device model, the structure of the device model, and
   the current set of properties.

   The expectation is that another layer will perform transformations of the
   resulting tree to preserve migration compatibility.  Tree transformation is
   a powerful mechanism and even with a totally different device model, this
   should enable us to support migration compatibility even to the current
   VMState based migration code.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
 Makefile.qom          |    3 +
 Qconfig               |    1 +
 configure             |    2 +-
 devices/Makefile      |    1 +
 devices/Qconfig       |    6 ++
 devices/device.c      |  125 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/qemu/device.h |   28 +++++++++++
 7 files changed, 165 insertions(+), 1 deletions(-)
 create mode 100644 devices/Makefile
 create mode 100644 devices/Qconfig
 create mode 100644 devices/device.c
 create mode 100644 include/qemu/device.h

diff --git a/Makefile.qom b/Makefile.qom
index 34f1f91..c694cbb 100644
--- a/Makefile.qom
+++ b/Makefile.qom
@@ -13,3 +13,6 @@ common-obj-y += $(addprefix qapi/,$(qapi-obj-y))
 include $(SRC_PATH)/qom/Makefile
 common-obj-y += $(addprefix qom/,$(qom-obj-y))
 
+include $(SRC_PATH)/devices/Makefile
+common-obj-y += $(addprefix devices/,$(devices-obj-y))
+
diff --git a/Qconfig b/Qconfig
index 889dfa6..03f2a87 100644
--- a/Qconfig
+++ b/Qconfig
@@ -1,2 +1,3 @@
 source qapi/Qconfig
 source qom/Qconfig
+source devices/Qconfig
diff --git a/configure b/configure
index 93e5e97..6ec1020 100755
--- a/configure
+++ b/configure
@@ -3516,7 +3516,7 @@ DIRS="$DIRS pc-bios/spapr-rtas"
 DIRS="$DIRS roms/seabios roms/vgabios"
 DIRS="$DIRS fsdev ui"
 DIRS="$DIRS qapi"
-DIRS="$DIRS qga qom"
+DIRS="$DIRS qga qom devices"
 FILES="Makefile tests/Makefile"
 FILES="$FILES tests/cris/Makefile tests/cris/.gdbinit"
 FILES="$FILES pc-bios/optionrom/Makefile pc-bios/keymaps"
diff --git a/devices/Makefile b/devices/Makefile
new file mode 100644
index 0000000..fbb0b82
--- /dev/null
+++ b/devices/Makefile
@@ -0,0 +1 @@
+devices-obj-$(CONFIG_DEVICE) := device.o
diff --git a/devices/Qconfig b/devices/Qconfig
new file mode 100644
index 0000000..6a06417
--- /dev/null
+++ b/devices/Qconfig
@@ -0,0 +1,6 @@
+config DEVICE
+       bool "QOM based device model"
+       default y
+       depends on QOM && QAPI_QMP
+       help
+         Device model
diff --git a/devices/device.c b/devices/device.c
new file mode 100644
index 0000000..5d289fc
--- /dev/null
+++ b/devices/device.c
@@ -0,0 +1,125 @@
+#include "qemu/device.h"
+#include "qapi/qmp-output-visitor.h"
+#include "qapi/qmp-input-visitor.h"
+
+static void device_state_accessor(Plug *plug, const char *name, Visitor *v, void *opaque, Error **errp)
+{
+    DeviceClass *class = DEVICE_GET_CLASS(DEVICE(plug));
+    return class->visit(DEVICE(plug), v, name, errp);
+}
+
+static void device_initfn(TypeInstance *obj)
+{
+    Device *device = DEVICE(obj);
+
+    plug_add_property_full(PLUG(device), "state",
+                           device_state_accessor,
+                           device_state_accessor,
+                           NULL,
+                           NULL,
+                           type_get_type(obj),
+                           PROP_F_READWRITE);
+}
+
+void device_visit(Device *device, Visitor *v, const char *name, Error **errp)
+{
+    visit_start_struct(v, (void **)&device, "Device", name, 0, errp);
+    visit_end_struct(v, errp);
+}
+
+static void device_visit_properties(Device *device, bool is_input, const char *name, Visitor *v, Error **errp);
+
+typedef struct DeviceVisitPropertyData
+{
+    Visitor *v;
+    Error **errp;
+    bool is_input;
+} DeviceVisitPropertyData;
+
+static void device_visit_property(Plug *plug, const char *name, const char *typename, int flags, void *opaque)
+{
+    DeviceVisitPropertyData *data = opaque;
+
+    if (strcmp(name, "state") == 0 || strcmp(name, "realized") == 0) {
+        return;
+    }
+
+    if (strstart(typename, "plug<", NULL)) {
+        Device *value = DEVICE(plug_get_property_plug(plug, NULL, name));
+        device_visit_properties(value, data->is_input, name, data->v, data->errp);
+    } else if (data->is_input) {
+        if ((flags & PROP_F_READ) && (flags & PROP_F_WRITE)) {
+            plug_set_property(plug, name, data->v, data->errp);
+        }
+    } else {
+        if (flags & PROP_F_READ) {
+            plug_get_property(plug, name, data->v, data->errp);
+        }
+    }
+}
+
+static void device_visit_properties(Device *device, bool is_input, const char *name, Visitor *v, Error **errp)
+{
+    DeviceVisitPropertyData data = {
+        .v = v,
+        .errp = errp,
+        .is_input = is_input,
+    };
+
+    visit_start_struct(v, (void **)&device, "Device", name, sizeof(Device), errp);
+    plug_foreach_property(PLUG(device), device_visit_property, &data);
+    visit_end_struct(v, errp);
+}
+
+static void device_unrealize(Plug *plug)
+{
+    Device *device = DEVICE(plug);
+    const char *typename;
+    char id[MAX_ID];
+    QmpOutputVisitor *qov;
+    QmpInputVisitor *qiv;
+    Error *local_err = NULL; // FIXME
+
+    snprintf(id, sizeof(id), "%s", type_get_id(TYPE_INSTANCE(device)));
+    typename = type_get_type(TYPE_INSTANCE(device));
+
+    qov = qmp_output_visitor_new();
+
+    device_visit_properties(device, false, id, qmp_output_get_visitor(qov), &local_err);
+
+    type_finalize(device);
+    type_initialize(device, typename, id);
+
+    qiv = qmp_input_visitor_new(qmp_output_get_qobject(qov));
+
+    device_visit_properties(device, true, id, qmp_input_get_visitor(qiv), &local_err);
+
+    qmp_input_visitor_cleanup(qiv);
+    qmp_output_visitor_cleanup(qov);
+}
+
+static void device_class_initfn(TypeClass *type_class)
+{
+    PlugClass *plug_class = PLUG_CLASS(type_class);
+    DeviceClass *class = DEVICE_CLASS(type_class);
+
+    plug_class->unrealize = device_unrealize;
+    class->visit = device_visit;
+}
+
+static const TypeInfo device_type_info = {
+    .name = TYPE_DEVICE,
+    .parent = TYPE_PLUG,
+    .instance_size = sizeof(Device),
+    .class_size = sizeof(DeviceClass),
+    .class_init = device_class_initfn,
+    .instance_init = device_initfn,
+    .abstract = true,
+};
+
+static void register_devices(void)
+{
+    type_register_static(&device_type_info);
+}
+
+device_init(register_devices);
diff --git a/include/qemu/device.h b/include/qemu/device.h
new file mode 100644
index 0000000..8e15232
--- /dev/null
+++ b/include/qemu/device.h
@@ -0,0 +1,28 @@
+#ifndef DEVICE_H
+#define DEVICE_H
+
+#include "qemu/plug.h"
+
+typedef struct Device
+{
+    Plug parent;
+} Device;
+
+typedef void (DeviceVisitor)(Device *device, Visitor *v, const char *name, Error **errp);
+
+typedef struct DeviceClass
+{
+    PlugClass parent_class;
+
+    /* public */
+    DeviceVisitor *visit;
+} DeviceClass;
+
+#define TYPE_DEVICE "device"
+#define DEVICE(obj) TYPE_CHECK(Device, obj, TYPE_DEVICE)
+#define DEVICE_CLASS(class) TYPE_CLASS_CHECK(DeviceClass, class, TYPE_DEVICE)
+#define DEVICE_GET_CLASS(obj) TYPE_GET_CLASS(DeviceClass, obj, TYPE_DEVICE)
+
+void device_visit(Device *device, Visitor *v, const char *name, Error **errp);
+
+#endif
-- 
1.7.4.1

  parent reply	other threads:[~2011-07-25  1:45 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-25  1:44 [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 01/21] qom: add make infrastructure Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 02/21] qom: convert QAPI to use Qconfig build system Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 03/21] qom: Add core type system Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 04/21] qom: add Plug class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 05/21] plug: add Plug property type Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 06/21] plug: add socket " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 07/21] plug: add generated property types Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 08/21] qom: add plug_create QMP command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 09/21] qom: add plug_list " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 10/21] qom: add plug_get " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 11/21] qom: add plug_set " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 12/21] qom: add plug_list_props " Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 13/21] qom: add plug_destroy command Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 14/21] qom: add example qsh command Anthony Liguori
2011-07-25  1:44 ` Anthony Liguori [this message]
2011-07-27 15:10   ` [Qemu-devel] [PATCH 15/21] qom: add Device class Peter Maydell
2011-07-27 16:07     ` Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 16/21] qom-devices: add a Pin class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 17/21] qom: add CharDriver class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 18/21] qom-chrdrv: add memory character driver Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 19/21] qom-chrdrv: add Socket base class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 20/21] qom-chrdrv: add TCPServer class Anthony Liguori
2011-07-25  1:44 ` [Qemu-devel] [PATCH 21/21] qom-chrdrv: add UnixServer Anthony Liguori
2011-07-25 11:21 ` [Qemu-devel] [RFC][PATCH 0/21] QEMU Object Model Kevin Wolf
2011-07-25 12:45   ` Anthony Liguori
2011-07-25 13:08     ` Kevin Wolf
2011-07-25 13:10       ` Anthony Liguori
2011-07-26 12:59 ` Paolo Bonzini
2011-07-26 14:02   ` Anthony Liguori
2011-07-26 14:35     ` Paolo Bonzini
2011-07-26 15:34       ` Anthony Liguori
2011-07-26 18:26         ` Paolo Bonzini
2011-07-26 19:23           ` Anthony Liguori
2011-07-27  8:55             ` Paolo Bonzini
2011-07-27 12:48               ` Anthony Liguori
2011-07-27 15:33                 ` Paolo Bonzini
2011-07-27 16:19                   ` Anthony Liguori
2011-07-27 16:28                   ` Anthony Liguori
2011-07-27 18:51                     ` Paolo Bonzini
2011-07-27 20:01                       ` Anthony Liguori
2011-07-28  7:36                         ` Paolo Bonzini
2011-07-28 12:46                           ` Anthony Liguori
2011-07-28 13:50                             ` Paolo Bonzini
2011-07-28 14:03                               ` Anthony Liguori
2011-07-28 14:41                                 ` Paolo Bonzini
2011-07-28 15:04                                   ` Anthony Liguori
2011-07-28 15:47                                     ` Paolo Bonzini
2011-07-28 17:59                                       ` Anthony Liguori
2011-07-29  7:19                                         ` Paolo Bonzini
2011-07-27 21:33     ` Peter Maydell
2011-07-27 22:31       ` Anthony Liguori

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=1311558293-5855-16-git-send-email-aliguori@us.ibm.com \
    --to=aliguori@us.ibm.com \
    --cc=qemu-devel@nongnu.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.