From a9c1febcf43544fb9b9e59e8b45161d11260d9aa Mon Sep 17 00:00:00 2001 From: Jonathon Jongsma Date: Wed, 7 Jul 2021 15:01:20 -0500 Subject: nodedev: fix xml output for mdev parents in test suite Commit 51fbbfdce8 attempted to get the proper nodedev name for the parent of an defined mdev by traversing the filesystem and looking for a device that had the appropriate sysfs path. This works, but it would be cleaner to to avoid mucking around in the filesystem and instead just just examine the list of devices we have in memory. We already had a function nodeDeviceFindAddressByName() which constructs an address for parent device in a format that can be used with mdevctl. So if we refactor this function into a a function that simply formats an address for an arbitrary virNodeDeviceObj*, then we can use this function as a predicate for our new virNodeDeviceObjListFind() function from the previous commit. This will search our list of devices for one whose address matches the address we get from mdevctl. One nice benefit of this approach is that our test cases will now display xml output with the proper parent name for mdevs (assuming that we've added the appropriate mock parent devices to the test driver). Previously they just displayed 'computer' for the parent because the alternative would have required specially constructing a mock filesystem environment with a sysfs that mapped to the appropriate parent. Signed-off-by: Jonathon Jongsma Reviewed-by: Michal Privoznik --- src/node_device/node_device_driver.c | 55 +++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'src/node_device') diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c index 65c163f519..6820ec413b 100644 --- a/src/node_device/node_device_driver.c +++ b/src/node_device/node_device_driver.c @@ -648,17 +648,11 @@ nodeDeviceDefToMdevctlConfig(virNodeDeviceDef *def, char **buf) static char * -nodeDeviceFindAddressByName(const char *name) +nodeDeviceObjFormatAddress(virNodeDeviceObj *obj) { - virNodeDeviceDef *def = NULL; virNodeDevCapsDef *caps = NULL; char *addr = NULL; - virNodeDeviceObj *dev = virNodeDeviceObjListFindByName(driver->devs, name); - - if (!dev) - return NULL; - - def = virNodeDeviceObjGetDef(dev); + virNodeDeviceDef *def = virNodeDeviceObjGetDef(obj); for (caps = def->caps; caps != NULL; caps = caps->next) { switch (caps->data.type) { case VIR_NODE_DEV_CAP_PCI_DEV: { @@ -714,8 +708,6 @@ nodeDeviceFindAddressByName(const char *name) break; } - virNodeDeviceObjEndAPI(&dev); - return addr; } @@ -730,6 +722,7 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, g_autoptr(virCommand) cmd = NULL; const char *subcommand = virMdevctlCommandTypeToString(cmd_type); g_autofree char *inbuf = NULL; + virNodeDeviceObj *parent_obj = NULL; switch (cmd_type) { case MDEVCTL_CMD_CREATE: @@ -754,7 +747,10 @@ nodeDeviceGetMdevctlCommand(virNodeDeviceDef *def, switch (cmd_type) { case MDEVCTL_CMD_CREATE: case MDEVCTL_CMD_DEFINE: - parent_addr = nodeDeviceFindAddressByName(def->parent); + if ((parent_obj = nodeDeviceObjFindByName(def->parent))) { + parent_addr = nodeDeviceObjFormatAddress(parent_obj); + virNodeDeviceObjEndAPI(&parent_obj); + } if (!parent_addr) { virReportError(VIR_ERR_INTERNAL_ERROR, @@ -1042,6 +1038,21 @@ static void mdevGenerateDeviceName(virNodeDeviceDef *dev) } +static bool +matchDeviceAddress(virNodeDeviceObj *obj, + const void *opaque) +{ + g_autofree char *addr = NULL; + bool want = false; + + virObjectLock(obj); + addr = nodeDeviceObjFormatAddress(obj); + want = STREQ_NULLABLE(addr, opaque); + virObjectUnlock(obj); + return want; +} + + static virNodeDeviceDef* nodeDeviceParseMdevctlChildDevice(const char *parent, virJSONValue *json) @@ -1051,7 +1062,7 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, virJSONValue *props; virJSONValue *attrs; g_autoptr(virNodeDeviceDef) child = g_new0(virNodeDeviceDef, 1); - g_autofree char *parent_sysfs_path = NULL; + virNodeDeviceObj *parent_obj; /* the child object should have a single key equal to its uuid. * The value is an object describing the properties of the mdev */ @@ -1064,20 +1075,12 @@ nodeDeviceParseMdevctlChildDevice(const char *parent, /* Look up id of parent device. mdevctl supports defining mdevs for parent * devices that are not present on the system (to support starting mdevs on * hotplug, etc) so the parent may not actually exist. */ - parent_sysfs_path = g_strdup_printf("/sys/class/mdev_bus/%s", parent); - if (virFileExists(parent_sysfs_path)) { - g_autofree char *canon_syspath = virFileCanonicalizePath(parent_sysfs_path); - virNodeDeviceObj *parentobj = NULL; - - if ((parentobj = virNodeDeviceObjListFindBySysfsPath(driver->devs, - canon_syspath))) { - virNodeDeviceDef *parentdef = virNodeDeviceObjGetDef(parentobj); - child->parent = g_strdup(parentdef->name); - virNodeDeviceObjEndAPI(&parentobj); - - child->parent_sysfs_path = g_steal_pointer(&canon_syspath); - } - } + if ((parent_obj = virNodeDeviceObjListFind(driver->devs, matchDeviceAddress, + (void *)parent))) { + virNodeDeviceDef *parentdef = virNodeDeviceObjGetDef(parent_obj); + child->parent = g_strdup(parentdef->name); + virNodeDeviceObjEndAPI(&parent_obj); + }; if (!child->parent) child->parent = g_strdup("computer"); child->caps = g_new0(virNodeDevCapsDef, 1); -- cgit v1.2.1