qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 15/21] qom: add Device class


From: Anthony Liguori
Subject: [Qemu-devel] [PATCH 15/21] qom: add Device class
Date: Sun, 24 Jul 2011 20:44:47 -0500

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 <address@hidden>
---
 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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]