[RFC PATCH 1/5] qdev: Create qdev_get_dev_path()

Alex Williamson
2010-06-14T05:51:21Z

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 <alex.williamson@redhat.com>
---

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;