It would be nice to be able to test the mediated device capabilities
without having physical hardware which supports it. The 'mtty' kernel
module presents a virtual parent device which is capable of creating
'fake' mediated devices, and as such it would be useful for testing.
However, the 'mtty' device is not part of an existing device subsystem
(e.g. PCI, etc), so libvirt ignores it and it does not get added to the
node device list. And because it does not get added to the node device
list, it cannot be used to create child mdevs using `virsh
nodedev-create`.
There is already a node device type capability
VIR_NODE_DEV_CAP_MDEV_TYPES that indicates whether a device supports
creating child mediated devices, but libvirt assumes that this is a
nested capability (in other words, it assumes that the primary
capability of a device is something like PCI). If we allow this
MDEV_TYPES capability to be a primary device capability, then we can
support virtual devices like 'mtty' as a parent for mediated devices.
See https://bugzilla.redhat.com/show_bug.cgi?id=
2107031
Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
data->ap_matrix.nmdev_types);
break;
case VIR_NODE_DEV_CAP_MDEV_TYPES:
+ virNodeDeviceCapMdevTypesFormat(&buf,
+ data->mdev_parent.mdev_types,
+ data->mdev_parent.nmdev_types);
+ break;
case VIR_NODE_DEV_CAP_FC_HOST:
case VIR_NODE_DEV_CAP_VPORTS:
case VIR_NODE_DEV_CAP_VPD:
g_free(data->ap_matrix.mdev_types);
break;
case VIR_NODE_DEV_CAP_MDEV_TYPES:
+ for (i = 0; i < data->mdev_parent.nmdev_types; i++)
+ virMediatedDeviceTypeFree(data->mdev_parent.mdev_types[i]);
+ g_free(data->mdev_parent.mdev_types);
+ g_free(data->mdev_parent.address);
+ break;
case VIR_NODE_DEV_CAP_DRM:
case VIR_NODE_DEV_CAP_FC_HOST:
case VIR_NODE_DEV_CAP_VPORTS:
&cap->data.ap_matrix) < 0)
return -1;
break;
+ case VIR_NODE_DEV_CAP_MDEV_TYPES:
+ if (virNodeDeviceGetMdevParentDynamicCaps(def->sysfs_path,
+ &cap->data.mdev_parent) < 0)
+ return -1;
+ break;
/* all types that (supposedly) don't require any updates
* relative to what's in the cache.
case VIR_NODE_DEV_CAP_FC_HOST:
case VIR_NODE_DEV_CAP_VPORTS:
case VIR_NODE_DEV_CAP_SCSI_GENERIC:
- case VIR_NODE_DEV_CAP_MDEV_TYPES:
case VIR_NODE_DEV_CAP_MDEV:
case VIR_NODE_DEV_CAP_CCW_DEV:
case VIR_NODE_DEV_CAP_VDPA:
return 0;
}
+/* virNodeDeviceGetMdevParentDynamicCaps() get info that is stored in sysfs
+ * about devices related to this device, i.e. things that can change
+ * without this device itself changing. These must be refreshed
+ * anytime full XML of the device is requested, because they can
+ * change with no corresponding notification from the kernel/udev.
+ */
+int
+virNodeDeviceGetMdevParentDynamicCaps(const char *sysfsPath,
+ virNodeDevCapMdevParent *mdev_parent)
+{
+ if (virNodeDeviceGetMdevTypesCaps(sysfsPath,
+ &mdev_parent->mdev_types,
+ &mdev_parent->nmdev_types) < 0)
+ return -1;
+ return 0;
+}
+
+
#else
int
return -1;
}
+int
+virNodeDeviceGetMdevParentDynamicCaps(const char *sysfsPath G_GNUC_UNUSED,
+ virNodeDevCapMdevParent *mdev_parent G_GNUC_UNUSED)
+{
+ return -1;
+}
+
+
#endif /* __linux__ */
size_t nmdev_types;
};
+
+typedef struct _virNodeDevCapMdevParent virNodeDevCapMdevParent;
+struct _virNodeDevCapMdevParent {
+ virMediatedDeviceType **mdev_types;
+ size_t nmdev_types;
+ char *address;
+};
+
typedef struct _virNodeDevCapData virNodeDevCapData;
struct _virNodeDevCapData {
virNodeDevCapType type;
virNodeDevCapAPCard ap_card;
virNodeDevCapAPQueue ap_queue;
virNodeDevCapAPMatrix ap_matrix;
+ virNodeDevCapMdevParent mdev_parent;
};
};
virNodeDeviceGetAPMatrixDynamicCaps(const char *sysfsPath,
virNodeDevCapAPMatrix *ap_matrix);
+int
+virNodeDeviceGetMdevParentDynamicCaps(const char *sysfsPath,
+ virNodeDevCapMdevParent *mdev_parent);
+
int
virNodeDeviceUpdateCaps(virNodeDeviceDef *def);
virNodeDeviceDefParseString;
virNodeDeviceGetAPMatrixDynamicCaps;
virNodeDeviceGetCSSDynamicCaps;
+virNodeDeviceGetMdevParentDynamicCaps;
virNodeDeviceGetPCIDynamicCaps;
virNodeDeviceGetSCSIHostCaps;
virNodeDeviceGetSCSITargetCaps;
virMediatedDeviceModelTypeFromString;
virMediatedDeviceModelTypeToString;
virMediatedDeviceNew;
+virMediatedDeviceParentGetAddress;
virMediatedDeviceSetUsedBy;
virMediatedDeviceTypeFree;
virMediatedDeviceTypeReadAttrs;
addr = g_strdup(caps->data.ap_matrix.addr);
break;
+ case VIR_NODE_DEV_CAP_MDEV_TYPES:
+ addr = g_strdup(caps->data.mdev_parent.address);
+ break;
+
case VIR_NODE_DEV_CAP_SYSTEM:
case VIR_NODE_DEV_CAP_USB_DEV:
case VIR_NODE_DEV_CAP_USB_INTERFACE:
case VIR_NODE_DEV_CAP_VPORTS:
case VIR_NODE_DEV_CAP_SCSI_GENERIC:
case VIR_NODE_DEV_CAP_DRM:
- case VIR_NODE_DEV_CAP_MDEV_TYPES:
case VIR_NODE_DEV_CAP_MDEV:
case VIR_NODE_DEV_CAP_CCW_DEV:
case VIR_NODE_DEV_CAP_VDPA:
}
+static int
+udevProcessMdevParent(struct udev_device *device,
+ virNodeDeviceDef *def)
+{
+ virNodeDevCapMdevParent *mdev_parent = &def->caps->data.mdev_parent;
+
+ udevGenerateDeviceName(device, def, NULL);
+
+ if (virMediatedDeviceParentGetAddress(def->sysfs_path, &mdev_parent->address) < 0) {
+ virReportError(VIR_ERR_INTERNAL_ERROR,
+ _("Unable to find address for mdev parent device '%s'"),
+ def->name);
+ return -1;
+ }
+
+ if (virNodeDeviceGetMdevParentDynamicCaps(def->sysfs_path, mdev_parent) < 0)
+ return -1;
+
+ return 0;
+}
+
+
static int
drmGetMinorType(int minor)
{
*type = VIR_NODE_DEV_CAP_VDPA;
else if (STREQ_NULLABLE(subsystem, "matrix"))
*type = VIR_NODE_DEV_CAP_AP_MATRIX;
+ else if (STREQ_NULLABLE(subsystem, "mtty"))
+ *type = VIR_NODE_DEV_CAP_MDEV_TYPES;
VIR_FREE(subsystem);
}
case VIR_NODE_DEV_CAP_AP_MATRIX:
return udevProcessAPMatrix(device, def);
case VIR_NODE_DEV_CAP_MDEV_TYPES:
+ return udevProcessMdevParent(device, def);
case VIR_NODE_DEV_CAP_VPD:
case VIR_NODE_DEV_CAP_SYSTEM:
case VIR_NODE_DEV_CAP_FC_HOST:
}
+#define MDEV_BUS_DIR "/sys/class/mdev_bus"
+
+
+int
+virMediatedDeviceParentGetAddress(const char *sysfspath,
+ char **address)
+{
+ g_autoptr(DIR) dir = NULL;
+ struct dirent *entry;
+ if (virDirOpen(&dir, MDEV_BUS_DIR) < 0)
+ return -1;
+
+ /* check if one of the links in /sys/class/mdev_bus/ points at the sysfs
+ * path for this device. If so, the link name is treated as the 'address'
+ * for the mdev parent */
+ while (virDirRead(dir, &entry, MDEV_BUS_DIR) > 0) {
+ g_autofree char *tmppath = g_strdup_printf("%s/%s", MDEV_BUS_DIR,
+ entry->d_name);
+
+ if (virFileLinkPointsTo(tmppath, sysfspath)) {
+ *address = g_strdup(entry->d_name);
+ return 0;
+ }
+ }
+
+ return -1;
+}
+
#ifdef __linux__
ssize_t
virMediatedDeviceType ***types,
size_t *ntypes);
+int
+virMediatedDeviceParentGetAddress(const char *sysfspath,
+ char **address);
+
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDevice, virMediatedDeviceFree);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDeviceType, virMediatedDeviceTypeFree);