From patchwork Wed Feb 1 19:50:52 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anthony Liguori X-Patchwork-Id: 139010 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 69A25104785 for ; Thu, 2 Feb 2012 08:09:58 +1100 (EST) Received: from localhost ([::1]:58066 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsgEC-0001zB-SV for incoming@patchwork.ozlabs.org; Wed, 01 Feb 2012 14:52:24 -0500 Received: from eggs.gnu.org ([140.186.70.92]:37632) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsgDr-0001O5-Kw for qemu-devel@nongnu.org; Wed, 01 Feb 2012 14:52:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RsgDj-00068E-NB for qemu-devel@nongnu.org; Wed, 01 Feb 2012 14:52:02 -0500 Received: from cpe-70-123-132-139.austin.res.rr.com ([70.123.132.139]:52910 helo=localhost6.localdomain6) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RsgDj-00067h-E4 for qemu-devel@nongnu.org; Wed, 01 Feb 2012 14:51:55 -0500 Received: from localhost6.localdomain6 (localhost.localdomain [127.0.0.1]) by localhost6.localdomain6 (8.14.4/8.14.4/Debian-2ubuntu1) with ESMTP id q11Jpm5S006378; Wed, 1 Feb 2012 13:51:49 -0600 Received: (from anthony@localhost) by localhost6.localdomain6 (8.14.4/8.14.4/Submit) id q11JplMn006377; Wed, 1 Feb 2012 13:51:47 -0600 From: Anthony Liguori To: qemu-devel@nongnu.org Date: Wed, 1 Feb 2012 13:50:52 -0600 Message-Id: <1328125863-6203-12-git-send-email-aliguori@us.ibm.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1328125863-6203-1-git-send-email-aliguori@us.ibm.com> References: <1328125863-6203-1-git-send-email-aliguori@us.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 70.123.132.139 Cc: Paolo Bonzini , Anthony Liguori , Andreas Faerber , Peter Maydell Subject: [Qemu-devel] [PATCH 11/22] qdev: remove baked in notion of aliases (v2) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org 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 --- 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(-) 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)