summaryrefslogtreecommitdiff
path: root/src/node_device
diff options
context:
space:
mode:
authorJonathon Jongsma <jjongsma@redhat.com>2021-07-07 15:01:20 -0500
committerJonathon Jongsma <jjongsma@redhat.com>2021-08-06 15:02:23 -0500
commita9c1febcf43544fb9b9e59e8b45161d11260d9aa (patch)
treee01baff80e858b22c12e5344f0e5cfeeb4c7eb00 /src/node_device
parentfdfe4b2837c3408edc57f6d3bf393dc16cd0ac64 (diff)
downloadlibvirt-a9c1febcf43544fb9b9e59e8b45161d11260d9aa.tar.gz
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 <jjongsma@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
Diffstat (limited to 'src/node_device')
-rw-r--r--src/node_device/node_device_driver.c55
1 files changed, 29 insertions, 26 deletions
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);