From patchwork Mon Jun 14 05:51:19 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 55470 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 2B8221007D2 for ; Mon, 14 Jun 2010 15:53:31 +1000 (EST) Received: from localhost ([127.0.0.1]:48613 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OO2bg-00045C-5g for incoming@patchwork.ozlabs.org; Mon, 14 Jun 2010 01:53:12 -0400 Received: from [140.186.70.92] (port=49860 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OO2Zv-00042Z-7l for qemu-devel@nongnu.org; Mon, 14 Jun 2010 01:51:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OO2Zt-00083y-Vv for qemu-devel@nongnu.org; Mon, 14 Jun 2010 01:51:23 -0400 Received: from qmta02.emeryville.ca.mail.comcast.net ([76.96.30.24]:60307) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OO2Zt-00083r-N1 for qemu-devel@nongnu.org; Mon, 14 Jun 2010 01:51:21 -0400 Received: from omta02.emeryville.ca.mail.comcast.net ([76.96.30.19]) by qmta02.emeryville.ca.mail.comcast.net with comcast id Vhdc1e0030QkzPwA2hrLo6; Mon, 14 Jun 2010 05:51:20 +0000 Received: from localhost.localdomain ([75.71.122.219]) by omta02.emeryville.ca.mail.comcast.net with comcast id VhrK1e0024k7Kz78NhrKCx; Mon, 14 Jun 2010 05:51:20 +0000 From: Alex Williamson To: qemu-devel@nongnu.org Date: Sun, 13 Jun 2010 23:51:19 -0600 Message-ID: <20100614055119.879.92321.stgit@localhost.localdomain> In-Reply-To: <20100614054923.879.33717.stgit@localhost.localdomain> References: <20100614054923.879.33717.stgit@localhost.localdomain> User-Agent: StGIT/0.14.3 MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: chrisw@redhat.com, kvm@vger.kernel.org, paul@codesourcery.com, alex.williamson@redhat.com, kraxel@redhat.com, avi@redhat.com Subject: [Qemu-devel] [RFC PATCH 1/5] qdev: Create qdev_get_dev_path() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org qdev_get_dev_path() is intended to be the canonical utility for creating a string representing the qdev hierarchy of a device. The path consists of bus and device names as well as identified properties of the immediate parent bus and device. This results in strings like: "/main-system-bus/pci.0,addr=00.0/i440FX" "/main-system-bus/pci.0,addr=01.0/PIIX3" "/main-system-bus/pci.0,addr=02.0/cirrus-vga" "/main-system-bus/pci.0/isa.0/mc146818rtc" "/main-system-bus/pci.0/isa.0/isa-serial" "/main-system-bus/pci.0/isa.0/i8042" "/main-system-bus/pci.0/isa.0/isa-fdc" "/main-system-bus/pci.0,addr=03.0/i82551,mac=52:54:00:12:34:56" "/main-system-bus/pci.0,addr=04.0/virtio-net-pci,mac=52:54:00:12:34:57" "/main-system-bus/pci.0,addr=05.0/e1000,mac=52:54:00:12:34:58" "/main-system-bus/pci.0,addr=06.0/rtl8139,mac=52:54:00:12:34:59" "/main-system-bus/pci.0,addr=07.0/pcnet,mac=52:54:00:12:34:5a" "/main-system-bus/pci.0,addr=01.1/piix3-ide" "/main-system-bus/pci.0,addr=01.3/PIIX4_PM" "/main-system-bus/pci.0,addr=08.0/lsi53c895a" "/main-system-bus/pci.0,addr=09.0/virtio-blk-pci" There are two primary targets for these strings. The first is vmsave, where we currently use a device provided string plus instance number to track SaveStateEntries. This fails when we introduce device hotplug, particularly in a case were we have gaps in the instance numbers that cannot easily be reproduced on a migration target. The second is for naming RAMBlocks. For these, we would like a string that matches the vmstate string. Signed-off-by: Alex Williamson Signed-off-by: Alex Williamson --- hw/qdev-properties.c | 2 ++ hw/qdev.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ hw/qdev.h | 5 ++++ 3 files changed, 64 insertions(+), 0 deletions(-) diff --git a/hw/qdev-properties.c b/hw/qdev-properties.c index 9ffdba7..e30df88 100644 --- a/hw/qdev-properties.c +++ b/hw/qdev-properties.c @@ -453,6 +453,7 @@ PropertyInfo qdev_prop_macaddr = { .name = "macaddr", .type = PROP_TYPE_MACADDR, .size = sizeof(MACAddr), + .flags = PROP_FLAG_PATH, .parse = parse_mac, .print = print_mac, }; @@ -496,6 +497,7 @@ PropertyInfo qdev_prop_pci_devfn = { .name = "pci-devfn", .type = PROP_TYPE_UINT32, .size = sizeof(uint32_t), + .flags = PROP_FLAG_PATH, .parse = parse_pci_devfn, .print = print_pci_devfn, }; diff --git a/hw/qdev.c b/hw/qdev.c index 36f29ea..dea44fe 100644 --- a/hw/qdev.c +++ b/hw/qdev.c @@ -120,6 +120,63 @@ DeviceState *qdev_create(BusState *bus, const char *name) return qdev_create_from_info(bus, info); } +static int qdev_strprint_parent_path(DeviceState *dev, char *buf, size_t len) +{ + int offset = 0; + + if (dev->parent_bus && dev->parent_bus->parent) + offset = qdev_strprint_parent_path(dev->parent_bus->parent, buf, len); + + offset += snprintf(buf + offset, len - offset, + "/%s", dev->parent_bus->name); + return offset; +} + +static int qdev_strprint_path_props(DeviceState *dev, Property *props, + char *buf, size_t len) +{ + int offset = 0; + char pbuf[64]; + + if (!props) + return 0; + + while (props->name) { + if (props->info->flags & PROP_FLAG_PATH) { + if (props->info->print) { + props->info->print(dev, props, pbuf, sizeof(pbuf)); + offset += snprintf(buf + offset, len - offset, ",%s=%s", + props->name, pbuf); + } + } + props++; + } + return offset; +} + +char *qdev_get_dev_path(DeviceState *dev) +{ + char buf[256] = ""; + int offset; + + if (!dev) + return NULL; + + offset = qdev_strprint_parent_path(dev, buf, sizeof(buf)); + + offset += qdev_strprint_path_props(dev, dev->parent_bus->info->props, + buf + offset, sizeof(buf) - offset); + + offset += snprintf(buf + offset, sizeof(buf) - offset, "/%s", + dev->info->name); + if (dev->id) + offset += snprintf(buf + offset, sizeof(buf) - offset, "-%s", dev->id); + + offset += qdev_strprint_path_props(dev, dev->info->props, + buf + offset, sizeof(buf) - offset); + return qemu_strdup(buf); +} + static void qdev_print_devinfo(DeviceInfo *info) { error_printf("name \"%s\", bus %s", diff --git a/hw/qdev.h b/hw/qdev.h index a44060e..2702384 100644 --- a/hw/qdev.h +++ b/hw/qdev.h @@ -96,6 +96,7 @@ struct PropertyInfo { const char *name; size_t size; enum PropertyType type; + int flags; int (*parse)(DeviceState *dev, Property *prop, const char *str); int (*print)(DeviceState *dev, Property *prop, char *dest, size_t len); }; @@ -201,6 +202,8 @@ extern PropertyInfo qdev_prop_netdev; extern PropertyInfo qdev_prop_vlan; extern PropertyInfo qdev_prop_pci_devfn; +#define PROP_FLAG_PATH (1<<0) + #define DEFINE_PROP(_name, _state, _field, _prop, _type) { \ .name = (_name), \ .info = &(_prop), \ @@ -280,6 +283,8 @@ void qdev_prop_set_defaults(DeviceState *dev, Property *props); void qdev_prop_register_global_list(GlobalProperty *props); void qdev_prop_set_globals(DeviceState *dev); +char *qdev_get_dev_path(DeviceState *dev); + /* This is a nasty hack to allow passing a NULL bus to qdev_create. */ extern struct BusInfo system_bus_info;