Patchwork [11/22] qdev: remove baked in notion of aliases (v2)

login
register
mail settings
Submitter Anthony Liguori
Date Feb. 1, 2012, 7:50 p.m.
Message ID <1328125863-6203-12-git-send-email-aliguori@us.ibm.com>
Download mbox | patch
Permalink /patch/139010/
State New
Headers show

Comments

Anthony Liguori - Feb. 1, 2012, 7:50 p.m.
Limit them to the device_add functionality.  Device aliases were a hack based
on the fact that virtio was modeled the wrong way.  The mechanism for aliasing
is very limited in that only one alias can exist for any device.

We have to support it for the purposes of compatibility but we only need to
support it in device_add so restrict it to that piece of code.

Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
---
v1 -> v2
 - Use a table for aliases (Paolo)
---
 hw/ide/ich.c         |    2 -
 hw/lsi53c895a.c      |    2 -
 hw/qdev.c            |   93 ++++++++++++++++++++++++++++++++++++++++++++++---
 hw/qdev.h            |    1 -
 hw/s390-virtio-bus.c |    3 --
 hw/virtio-pci.c      |    8 ----
 6 files changed, 87 insertions(+), 22 deletions(-)

Patch

diff --git a/hw/ide/ich.c b/hw/ide/ich.c
index 0e819f6..5cdaa99 100644
--- a/hw/ide/ich.c
+++ b/hw/ide/ich.c
@@ -158,7 +158,6 @@  static void ich_ahci_class_init(ObjectClass *klass, void *data)
     k->device_id = PCI_DEVICE_ID_INTEL_82801IR;
     k->revision = 0x02;
     k->class_id = PCI_CLASS_STORAGE_SATA;
-    dc->alias = "ahci";
     dc->vmsd = &vmstate_ahci;
 }
 
@@ -172,6 +171,5 @@  static TypeInfo ich_ahci_info = {
 static void ich_ahci_register(void)
 {
     type_register_static(&ich_ahci_info);
-    type_register_static_alias(&ich_ahci_info, "ahci");
 }
 device_init(ich_ahci_register);
diff --git a/hw/lsi53c895a.c b/hw/lsi53c895a.c
index 638332e..9a7ffe3 100644
--- a/hw/lsi53c895a.c
+++ b/hw/lsi53c895a.c
@@ -2131,7 +2131,6 @@  static void lsi_class_init(ObjectClass *klass, void *data)
     k->device_id = PCI_DEVICE_ID_LSI_53C895A;
     k->class_id = PCI_CLASS_STORAGE_SCSI;
     k->subsystem_id = 0x1000;
-    dc->alias = "lsi";
     dc->reset = lsi_scsi_reset;
     dc->vmsd = &vmstate_lsi_scsi;
 }
@@ -2146,7 +2145,6 @@  static TypeInfo lsi_info = {
 static void lsi53c895a_register_devices(void)
 {
     type_register_static(&lsi_info);
-    type_register_static_alias(&lsi_info, "lsi");
 }
 
 device_init(lsi53c895a_register_devices);
diff --git a/hw/qdev.c b/hw/qdev.c
index 4f6c3a7..5830bef 100644
--- a/hw/qdev.c
+++ b/hw/qdev.c
@@ -61,14 +61,56 @@  Property *qdev_get_props(DeviceState *dev)
     return dc->props;
 }
 
+/*
+ * Aliases were a bad idea from the start.  Let's keep them
+ * from spreading further.
+ */
+typedef struct QDevAlias
+{
+    const char *typename;
+    const char *alias;
+} QDevAlias;
+
+static const QDevAlias qdev_alias_table[] = {
+    { "virtio-blk-pci", "virtio-blk" },
+    { "virtio-net-pci", "virtio-net" },
+    { "virtio-serial-pci", "virtio-serial" },
+    { "virtio-balloon-pci", "virtio-balloon" },
+    { "virtio-blk-s390", "virtio-blk" },
+    { "virtio-net-s390", "virtio-net" },
+    { "virtio-serial-s390", "virtio-serial" },
+    { "lsi53c895a", "lsi" },
+    { "ich9-ahci", "ahci" },
+    { }
+};
+
+static const char *qdev_class_get_alias(DeviceClass *dc)
+{
+    const char *typename = object_class_get_name(OBJECT_CLASS(dc));
+    int i;
+
+    for (i = 0; qdev_alias_table[i].typename; i++) {
+        if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
+            return qdev_alias_table[i].alias;
+        }
+    }
+
+    return NULL;
+}
+
+static bool qdev_class_has_alias(DeviceClass *dc)
+{
+    return (qdev_class_get_alias(dc) != NULL);
+}
+
 const char *qdev_fw_name(DeviceState *dev)
 {
     DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
     if (dc->fw_name) {
         return dc->fw_name;
-    } else if (dc->alias) {
-        return dc->alias;
+    } else if (qdev_class_has_alias(dc)) {
+        return qdev_class_get_alias(dc);
     }
 
     return object_get_typename(OBJECT(dev));
@@ -161,8 +203,8 @@  static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
     if (dc->bus_info) {
         error_printf(", bus %s", dc->bus_info->name);
     }
-    if (dc->alias) {
-        error_printf(", alias \"%s\"", dc->alias);
+    if (qdev_class_has_alias(dc)) {
+        error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
     }
     if (dc->desc) {
         error_printf(", desc \"%s\"", dc->desc);
@@ -188,6 +230,19 @@  static int set_property(const char *name, const char *value, void *opaque)
     return 0;
 }
 
+static const char *find_typename_by_alias(const char *alias)
+{
+    int i;
+
+    for (i = 0; qdev_alias_table[i].alias; i++) {
+        if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
+            return qdev_alias_table[i].typename;
+        }
+    }
+
+    return NULL;
+}
+
 int qdev_device_help(QemuOpts *opts)
 {
     const char *driver;
@@ -208,6 +263,15 @@  int qdev_device_help(QemuOpts *opts)
 
     klass = object_class_by_name(driver);
     if (!klass) {
+        const char *typename = find_typename_by_alias(driver);
+
+        if (typename) {
+            driver = typename;
+            klass = object_class_by_name(driver);
+        }
+    }
+
+    if (!klass) {
         return 0;
     }
     info = DEVICE_CLASS(klass);
@@ -263,6 +327,7 @@  static DeviceState *qdev_get_peripheral_anon(void)
 
 DeviceState *qdev_device_add(QemuOpts *opts)
 {
+    ObjectClass *obj;
     DeviceClass *k;
     const char *driver, *path, *id;
     DeviceState *qdev;
@@ -275,7 +340,22 @@  DeviceState *qdev_device_add(QemuOpts *opts)
     }
 
     /* find driver */
-    k = DEVICE_CLASS(object_class_by_name(driver));
+    obj = object_class_by_name(driver);
+    if (!obj) {
+        const char *typename = find_typename_by_alias(driver);
+
+        if (typename) {
+            driver = typename;
+            obj = object_class_by_name(driver);
+        }
+    }
+
+    if (!obj) {
+        qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
+        return NULL;
+    }
+
+    k = DEVICE_CLASS(obj);
 
     /* find bus */
     path = qemu_opt_get(opts, "bus");
@@ -753,7 +833,8 @@  static DeviceState *qbus_find_dev(BusState *bus, char *elem)
     QTAILQ_FOREACH(dev, &bus->children, sibling) {
         DeviceClass *dc = DEVICE_GET_CLASS(dev);
 
-        if (dc->alias && strcmp(dc->alias, elem) == 0) {
+        if (qdev_class_has_alias(dc) &&
+            strcmp(qdev_class_get_alias(dc), elem) == 0) {
             return dev;
         }
     }
diff --git a/hw/qdev.h b/hw/qdev.h
index 29f6daa..5aea4bf 100644
--- a/hw/qdev.h
+++ b/hw/qdev.h
@@ -78,7 +78,6 @@  typedef struct DeviceClass {
     ObjectClass parent_class;
 
     const char *fw_name;
-    const char *alias;
     const char *desc;
     Property *props;
     int no_user;
diff --git a/hw/s390-virtio-bus.c b/hw/s390-virtio-bus.c
index 4e1e888..b66ef68 100644
--- a/hw/s390-virtio-bus.c
+++ b/hw/s390-virtio-bus.c
@@ -354,7 +354,6 @@  static void s390_virtio_net_class_init(ObjectClass *klass, void *data)
 
     k->init = s390_virtio_net_init;
     dc->props = s390_virtio_net_properties;
-    dc->alias = "virtio-net";
 }
 
 static TypeInfo s390_virtio_net = {
@@ -377,7 +376,6 @@  static void s390_virtio_blk_class_init(ObjectClass *klass, void *data)
 
     k->init = s390_virtio_blk_init;
     dc->props = s390_virtio_blk_properties;
-    dc->alias = "virtio-blk";
 }
 
 static TypeInfo s390_virtio_blk = {
@@ -400,7 +398,6 @@  static void s390_virtio_serial_class_init(ObjectClass *klass, void *data)
 
     k->init = s390_virtio_serial_init;
     dc->props = s390_virtio_serial_properties;
-    dc->alias = "virtio-serial";
 }
 
 static TypeInfo s390_virtio_serial = {
diff --git a/hw/virtio-pci.c b/hw/virtio-pci.c
index bc96552..93fff54 100644
--- a/hw/virtio-pci.c
+++ b/hw/virtio-pci.c
@@ -827,7 +827,6 @@  static void virtio_blk_class_init(ObjectClass *klass, void *data)
     k->device_id = PCI_DEVICE_ID_VIRTIO_BLOCK;
     k->revision = VIRTIO_PCI_ABI_VERSION;
     k->class_id = PCI_CLASS_STORAGE_SCSI;
-    dc->alias = "virtio-blk";
     dc->reset = virtio_pci_reset;
     dc->props = virtio_blk_properties;
 }
@@ -862,7 +861,6 @@  static void virtio_net_class_init(ObjectClass *klass, void *data)
     k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
     k->revision = VIRTIO_PCI_ABI_VERSION;
     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
-    dc->alias = "virtio-net";
     dc->reset = virtio_pci_reset;
     dc->props = virtio_net_properties;
 }
@@ -894,7 +892,6 @@  static void virtio_serial_class_init(ObjectClass *klass, void *data)
     k->device_id = PCI_DEVICE_ID_VIRTIO_CONSOLE;
     k->revision = VIRTIO_PCI_ABI_VERSION;
     k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
-    dc->alias = "virtio-serial";
     dc->reset = virtio_pci_reset;
     dc->props = virtio_serial_properties;
 }
@@ -922,7 +919,6 @@  static void virtio_balloon_class_init(ObjectClass *klass, void *data)
     k->device_id = PCI_DEVICE_ID_VIRTIO_BALLOON;
     k->revision = VIRTIO_PCI_ABI_VERSION;
     k->class_id = PCI_CLASS_MEMORY_RAM;
-    dc->alias = "virtio-balloon";
     dc->reset = virtio_pci_reset;
     dc->props = virtio_balloon_properties;
 }
@@ -937,13 +933,9 @@  static TypeInfo virtio_balloon_info = {
 static void virtio_pci_register_devices(void)
 {
     type_register_static(&virtio_blk_info);
-    type_register_static_alias(&virtio_blk_info, "virtio-blk");
     type_register_static(&virtio_net_info);
-    type_register_static_alias(&virtio_net_info, "virtio-net");
     type_register_static(&virtio_serial_info);
-    type_register_static_alias(&virtio_serial_info, "virtio-serial");
     type_register_static(&virtio_balloon_info);
-    type_register_static_alias(&virtio_balloon_info, "virtio-balloon");
 }
 
 device_init(virtio_pci_register_devices)