Patchwork [RFC,v5,5/6] virtio-device : Refactor virtio-device.

login
register
mail settings
Submitter fred.konrad@greensocs.com
Date Dec. 4, 2012, 2:35 p.m.
Message ID <1354631742-4693-6-git-send-email-fred.konrad@greensocs.com>
Download mbox | patch
Permalink /patch/203665/
State New
Headers show

Comments

fred.konrad@greensocs.com - Dec. 4, 2012, 2:35 p.m.
From: KONRAD Frederic <fred.konrad@greensocs.com>

Create the virtio-device which is abstract. All the virtio-device can extend
this class.

Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
---
 hw/virtio.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/virtio.h | 29 +++++++++++++++++++++++++++++
 2 files changed, 85 insertions(+)
Peter Maydell - Dec. 4, 2012, 2:55 p.m.
On 4 December 2012 14:35,  <fred.konrad@greensocs.com> wrote:
> From: KONRAD Frederic <fred.konrad@greensocs.com>
>
> Create the virtio-device which is abstract. All the virtio-device can extend
> this class.
>
> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
> ---
>  hw/virtio.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  hw/virtio.h | 29 +++++++++++++++++++++++++++++
>  2 files changed, 85 insertions(+)
>
> diff --git a/hw/virtio.c b/hw/virtio.c
> index f40a8c5..cd46af1 100644
> --- a/hw/virtio.c
> +++ b/hw/virtio.c
> @@ -16,6 +16,7 @@
>  #include "trace.h"
>  #include "qemu-error.h"
>  #include "virtio.h"
> +#include "virtio-bus.h"
>  #include "qemu-barrier.h"
>
>  /* The alignment to use between consumer and producer parts of vring.
> @@ -934,6 +935,38 @@ VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
>      return vdev;
>  }
>
> +/*
> + * The same initialization as above without allocating the structure.
> + */
> +void virtio_common_init_(VirtIODevice *vdev, const char *name,
> +                         uint16_t device_id, size_t config_size,
> +                         size_t struct_size)

If you find yourself cut-and-pasting 25 lines of code, think again.
In this case, just make virtio_common_init() a wrapper that does
a malloc and calls your non-allocation init.

Also, find a better function name than "just add a random underscore".

-- PMM
fred.konrad@greensocs.com - Dec. 4, 2012, 3:55 p.m.
On 04/12/2012 15:55, Peter Maydell wrote:
> On 4 December 2012 14:35,  <fred.konrad@greensocs.com> wrote:
>> From: KONRAD Frederic <fred.konrad@greensocs.com>
>>
>> Create the virtio-device which is abstract. All the virtio-device can extend
>> this class.
>>
>> Signed-off-by: KONRAD Frederic <fred.konrad@greensocs.com>
>> ---
>>   hw/virtio.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>   hw/virtio.h | 29 +++++++++++++++++++++++++++++
>>   2 files changed, 85 insertions(+)
>>
>> diff --git a/hw/virtio.c b/hw/virtio.c
>> index f40a8c5..cd46af1 100644
>> --- a/hw/virtio.c
>> +++ b/hw/virtio.c
>> @@ -16,6 +16,7 @@
>>   #include "trace.h"
>>   #include "qemu-error.h"
>>   #include "virtio.h"
>> +#include "virtio-bus.h"
>>   #include "qemu-barrier.h"
>>
>>   /* The alignment to use between consumer and producer parts of vring.
>> @@ -934,6 +935,38 @@ VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
>>       return vdev;
>>   }
>>
>> +/*
>> + * The same initialization as above without allocating the structure.
>> + */
>> +void virtio_common_init_(VirtIODevice *vdev, const char *name,
>> +                         uint16_t device_id, size_t config_size,
>> +                         size_t struct_size)
> If you find yourself cut-and-pasting 25 lines of code, think again.
> In this case, just make virtio_common_init() a wrapper that does
> a malloc and calls your non-allocation init.
I didn't think about it, I'll do it.

Thanks,

Fred.

Patch

diff --git a/hw/virtio.c b/hw/virtio.c
index f40a8c5..cd46af1 100644
--- a/hw/virtio.c
+++ b/hw/virtio.c
@@ -16,6 +16,7 @@ 
 #include "trace.h"
 #include "qemu-error.h"
 #include "virtio.h"
+#include "virtio-bus.h"
 #include "qemu-barrier.h"
 
 /* The alignment to use between consumer and producer parts of vring.
@@ -934,6 +935,38 @@  VirtIODevice *virtio_common_init(const char *name, uint16_t device_id,
     return vdev;
 }
 
+/*
+ * The same initialization as above without allocating the structure.
+ */
+void virtio_common_init_(VirtIODevice *vdev, const char *name,
+                         uint16_t device_id, size_t config_size,
+                         size_t struct_size)
+{
+    int i;
+
+    vdev->device_id = device_id;
+    vdev->status = 0;
+    vdev->isr = 0;
+    vdev->queue_sel = 0;
+    vdev->config_vector = VIRTIO_NO_VECTOR;
+    vdev->vq = g_malloc0(sizeof(VirtQueue) * VIRTIO_PCI_QUEUE_MAX);
+    vdev->vm_running = runstate_is_running();
+    for (i = 0; i < VIRTIO_PCI_QUEUE_MAX; i++) {
+        vdev->vq[i].vector = VIRTIO_NO_VECTOR;
+        vdev->vq[i].vdev = vdev;
+    }
+
+    vdev->name = name;
+    vdev->config_len = config_size;
+    if (vdev->config_len) {
+        vdev->config = g_malloc0(config_size);
+    } else {
+        vdev->config = NULL;
+    }
+    vdev->vmstate = qemu_add_vm_change_state_handler(virtio_vmstate_change,
+                                                     vdev);
+}
+
 void virtio_bind_device(VirtIODevice *vdev, const VirtIOBindings *binding,
                         void *opaque)
 {
@@ -1056,3 +1089,26 @@  EventNotifier *virtio_queue_get_host_notifier(VirtQueue *vq)
 {
     return &vq->host_notifier;
 }
+
+static void virtio_device_class_init(ObjectClass *klass, void *data)
+{
+    /* Set the default value here. */
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->bus_type = TYPE_VIRTIO_BUS;
+}
+
+static const TypeInfo virtio_device_info = {
+    .name = TYPE_VIRTIO_DEVICE,
+    .parent = TYPE_DEVICE,
+    .instance_size = sizeof(VirtIODevice),
+    .class_init = virtio_device_class_init,
+    .abstract = true,
+    .class_size = sizeof(VirtioDeviceClass),
+};
+
+static void virtio_register_types(void)
+{
+    type_register_static(&virtio_device_info);
+}
+
+type_init(virtio_register_types)
diff --git a/hw/virtio.h b/hw/virtio.h
index 7c17f7b..361ad95 100644
--- a/hw/virtio.h
+++ b/hw/virtio.h
@@ -108,8 +108,17 @@  typedef struct {
 
 #define VIRTIO_NO_VECTOR 0xffff
 
+#define TYPE_VIRTIO_DEVICE "virtio-device"
+#define VIRTIO_DEVICE_GET_CLASS(obj) \
+        OBJECT_GET_CLASS(VirtioDeviceClass, obj, TYPE_VIRTIO_DEVICE)
+#define VIRTIO_DEVICE_CLASS(klass) \
+        OBJECT_CLASS_CHECK(VirtioDeviceClass, klass, TYPE_VIRTIO_DEVICE)
+#define VIRTIO_DEVICE(obj) \
+        OBJECT_CHECK(VirtIODevice, (obj), TYPE_VIRTIO_DEVICE)
+
 struct VirtIODevice
 {
+    DeviceState parent_obj;
     const char *name;
     uint8_t status;
     uint8_t isr;
@@ -119,6 +128,9 @@  struct VirtIODevice
     void *config;
     uint16_t config_vector;
     int nvectors;
+    /*
+     * Must be removed as we have it in VirtioDeviceClass.
+     */
     uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
     uint32_t (*bad_features)(VirtIODevice *vdev);
     void (*set_features)(VirtIODevice *vdev, uint32_t val);
@@ -126,6 +138,7 @@  struct VirtIODevice
     void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
     void (*reset)(VirtIODevice *vdev);
     void (*set_status)(VirtIODevice *vdev, uint8_t val);
+    /***/
     VirtQueue *vq;
     const VirtIOBindings *binding;
     void *binding_opaque;
@@ -134,6 +147,22 @@  struct VirtIODevice
     VMChangeStateEntry *vmstate;
 };
 
+typedef struct {
+    /* This is what a VirtioDevice must implement */
+    DeviceClass parent;
+    uint32_t (*get_features)(VirtIODevice *vdev, uint32_t requested_features);
+    uint32_t (*bad_features)(VirtIODevice *vdev);
+    void (*set_features)(VirtIODevice *vdev, uint32_t val);
+    void (*get_config)(VirtIODevice *vdev, uint8_t *config);
+    void (*set_config)(VirtIODevice *vdev, const uint8_t *config);
+    void (*reset)(VirtIODevice *vdev);
+    void (*set_status)(VirtIODevice *vdev, uint8_t val);
+} VirtioDeviceClass;
+
+void virtio_common_init_(VirtIODevice *vdev, const char *name,
+                         uint16_t device_id, size_t config_size,
+                         size_t struct_size);
+
 VirtQueue *virtio_add_queue(VirtIODevice *vdev, int queue_size,
                             void (*handle_output)(VirtIODevice *,
                                                   VirtQueue *));