Patchwork [v2] device-assignment: Rework "name" of assigned pci device

login
register
mail settings
Submitter Hidetoshi Seto
Date June 29, 2010, 8:36 a.m.
Message ID <4C29B080.6030701@jp.fujitsu.com>
Download mbox | patch
Permalink /patch/57254/
State New
Headers show

Comments

Hidetoshi Seto - June 29, 2010, 8:36 a.m.
Because use of some characters in "id" is restricted recently,
assigned device start to fail having implicit "id" that uses
address string of host device, like "00:19.0" which includes
restricted character ':'.

It seems that this implicit "id" is intended to be run as "name"
that could be passed with option "-pcidevice ... ,name=..." to
specify a string to be used in log outputs.  In other words it
seems that dev->dev.qdev.id of assigned device had been used to
point such "name", that is user-defined string or address string
of "host".

The problem is that "name" for specific use is not equal to "id"
for universal use.  So it is better to remove this tricky mix up.

This patch introduces new function assigned_dev_name() that
returns proper name string for the device.
Now property "name" is explicitly defined in struct AssignedDevice.

When if the device have neither "name" nor "id", address string
like "0000:00:19.0" will be created and passed instead.
Once created, new field r_name holds the string to be reused and
to be released later.

v2: - Pass id to qemu_opts_create if "id=" is specified even it is
      an undocumented option for -pcidevice and pci_add.
    - Drop minor cleanup & unrelated changes.

Signed-off-by: Hidetoshi Seto <seto.hidetoshi@jp.fujitsu.com>
---
 hw/device-assignment.c |   52 ++++++++++++++++++++++++++++++++++++------------
 hw/device-assignment.h |    2 +
 2 files changed, 41 insertions(+), 13 deletions(-)

Patch

diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index 585162b..cda6ee3 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -62,6 +62,25 @@  static void assigned_dev_load_option_rom(AssignedDevice *dev);
 
 static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev);
 
+static const char *assigned_dev_name(AssignedDevice *dev)
+{
+    /* use user-defined "name" if specified */
+    if (dev->u_name)
+        return dev->u_name;
+    /* else use "id" if available */
+    if (dev->dev.qdev.id)
+        return dev->dev.qdev.id;
+    /* otherwise use address of host device instead */
+    if (!dev->r_name) {
+        char buf[32];
+
+        snprintf(buf, sizeof(buf), "%04x:%02x:%02x.%01x",
+                 dev->host.seg, dev->host.bus, dev->host.dev, dev->host.func);
+        dev->r_name = qemu_strdup(buf);
+    }
+    return dev->r_name;
+}
+
 static uint32_t guest_to_host_ioport(AssignedDevRegion *region, uint32_t addr)
 {
     return region->u.r_baseport + (addr - region->e_physbase);
@@ -798,6 +817,10 @@  static void free_assigned_device(AssignedDevice *dev)
             dev->real_device.config_fd = 0;
         }
 
+    if (dev->r_name) {
+        qemu_free(dev->r_name);
+    }
+
 #ifdef KVM_CAP_IRQ_ROUTING
         free_dev_irq_entries(dev);
 #endif
@@ -885,7 +908,7 @@  static int assign_device(AssignedDevice *dev)
     if (dev->use_iommu) {
         if (!kvm_check_extension(kvm_state, KVM_CAP_IOMMU)) {
             fprintf(stderr, "No IOMMU found.  Unable to assign device \"%s\"\n",
-                    dev->dev.qdev.id);
+                    assigned_dev_name(dev));
             return -ENODEV;
         }
         assigned_dev_data.flags |= KVM_DEV_ASSIGN_ENABLE_IOMMU;
@@ -897,7 +920,7 @@  static int assign_device(AssignedDevice *dev)
     r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
     if (r < 0) {
         fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
-                dev->dev.qdev.id, strerror(-r));
+                assigned_dev_name(dev), strerror(-r));
 
         switch (r) {
             case -EBUSY:
@@ -953,7 +976,7 @@  static int assign_irq(AssignedDevice *dev)
     r = kvm_assign_irq(kvm_context, &assigned_irq_data);
     if (r < 0) {
         fprintf(stderr, "Failed to assign irq for \"%s\": %s\n",
-                dev->dev.qdev.id, strerror(-r));
+                assigned_dev_name(dev), strerror(-r));
         fprintf(stderr, "Perhaps you are assigning a device "
                 "that shares an IRQ with another device?\n");
         return r;
@@ -977,7 +1000,7 @@  static void deassign_device(AssignedDevice *dev)
     r = kvm_deassign_pci_device(kvm_context, &assigned_dev_data);
     if (r < 0)
 	fprintf(stderr, "Failed to deassign device \"%s\" : %s\n",
-                dev->dev.qdev.id, strerror(-r));
+                assigned_dev_name(dev), strerror(-r));
 #endif
 }
 
@@ -1421,7 +1444,7 @@  static int assigned_initfn(struct PCIDevice *pci_dev)
     if (get_real_device(dev, dev->host.seg, dev->host.bus,
                         dev->host.dev, dev->host.func)) {
         error_report("pci-assign: Error: Couldn't get real device (%s)!",
-                     dev->dev.qdev.id);
+                     assigned_dev_name(dev));
         goto out;
     }
 
@@ -1520,6 +1543,7 @@  static PCIDeviceInfo assign_info = {
         DEFINE_PROP("host", AssignedDevice, host, qdev_prop_hostaddr, PCIHostDevice),
         DEFINE_PROP_UINT32("iommu", AssignedDevice, use_iommu, 1),
         DEFINE_PROP_STRING("configfd", AssignedDevice, configfd_name),
+        DEFINE_PROP_STRING("name", AssignedDevice, u_name),
         DEFINE_PROP_END_OF_LIST(),
     },
 };
@@ -1545,24 +1569,28 @@  device_init(assign_register_devices)
 QemuOpts *add_assigned_device(const char *arg)
 {
     QemuOpts *opts = NULL;
-    char host[64], id[64], dma[8];
+    char host[64], buf[64], dma[8], *id = NULL;
     int r;
 
+    /* "host" is mandatory */
     r = get_param_value(host, sizeof(host), "host", arg);
     if (!r)
          goto bad;
-    r = get_param_value(id, sizeof(id), "id", arg);
-    if (!r)
-        r = get_param_value(id, sizeof(id), "name", arg);
-    if (!r)
-        r = get_param_value(id, sizeof(id), "host", arg);
 
+    r = get_param_value(buf, sizeof(buf), "id", arg);
+    if (r)
+         id = buf;
     opts = qemu_opts_create(&qemu_device_opts, id, 0);
     if (!opts)
         goto bad;
     qemu_opt_set(opts, "driver", "pci-assign");
     qemu_opt_set(opts, "host", host);
 
+    /* Log outputs use "name" if specified */
+    r = get_param_value(buf, sizeof(buf), "name", arg);
+    if (r)
+        qemu_opt_set(opts, "name", buf);
+
 #ifdef KVM_CAP_IOMMU
     r = get_param_value(dma, sizeof(dma), "dma", arg);
     if (r && !strncmp(dma, "none", 4))
@@ -1574,8 +1602,6 @@  QemuOpts *add_assigned_device(const char *arg)
 bad:
     fprintf(stderr, "pcidevice argument parse error; "
             "please check the help text for usage\n");
-    if (opts)
-        qemu_opts_del(opts);
     return NULL;
 }
 
diff --git a/hw/device-assignment.h b/hw/device-assignment.h
index 4e7fe87..fb00e29 100644
--- a/hw/device-assignment.h
+++ b/hw/device-assignment.h
@@ -86,6 +86,8 @@  typedef struct AssignedDevice {
     unsigned int h_segnr;
     unsigned char h_busnr;
     unsigned int h_devfn;
+    char *u_name;
+    char *r_name;
     int irq_requested_type;
     int bound;
     struct {