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: 105867 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o5E5pPZQ030903 for ; Mon, 14 Jun 2010 05:51:34 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752160Ab0FNFvV (ORCPT ); Mon, 14 Jun 2010 01:51:21 -0400 Received: from qmta01.emeryville.ca.mail.comcast.net ([76.96.30.16]:55390 "EHLO qmta01.emeryville.ca.mail.comcast.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752049Ab0FNFvV (ORCPT ); Mon, 14 Jun 2010 01:51:21 -0400 Received: from omta02.emeryville.ca.mail.comcast.net ([76.96.30.19]) by qmta01.emeryville.ca.mail.comcast.net with comcast id Vhmb1e0010QkzPwA1hrLL4; 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 Subject: [RFC PATCH 1/5] qdev: Create qdev_get_dev_path() To: qemu-devel@nongnu.org Cc: kvm@vger.kernel.org, avi@redhat.com, anthony@codemonkey.ws, paul@codesourcery.com, kraxel@redhat.com, chrisw@redhat.com, alex.williamson@redhat.com 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 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 14 Jun 2010 05:51:41 +0000 (UTC) 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;