[libvirt PATCH] nodedev: support 'mtty' device for testing

Jonathon Jongsma posted 1 patch 1 year, 8 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20220719163356.337159-1-jjongsma@redhat.com
src/conf/node_device_conf.c          | 41 +++++++++++++++++++++++++++-
src/conf/node_device_conf.h          | 13 +++++++++
src/libvirt_private.syms             |  2 ++
src/node_device/node_device_driver.c |  5 +++-
src/node_device/node_device_udev.c   | 25 +++++++++++++++++
src/util/virmdev.c                   | 28 +++++++++++++++++++
src/util/virmdev.h                   |  4 +++
7 files changed, 116 insertions(+), 2 deletions(-)
[libvirt PATCH] nodedev: support 'mtty' device for testing
Posted by Jonathon Jongsma 1 year, 8 months ago
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>
---
 src/conf/node_device_conf.c          | 41 +++++++++++++++++++++++++++-
 src/conf/node_device_conf.h          | 13 +++++++++
 src/libvirt_private.syms             |  2 ++
 src/node_device/node_device_driver.c |  5 +++-
 src/node_device/node_device_udev.c   | 25 +++++++++++++++++
 src/util/virmdev.c                   | 28 +++++++++++++++++++
 src/util/virmdev.h                   |  4 +++
 7 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/src/conf/node_device_conf.c b/src/conf/node_device_conf.c
index 364bb489bd..d5bfc098b2 100644
--- a/src/conf/node_device_conf.c
+++ b/src/conf/node_device_conf.c
@@ -772,6 +772,10 @@ virNodeDeviceDefFormat(const virNodeDeviceDef *def)
                                                 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:
@@ -2674,6 +2678,11 @@ virNodeDevCapsDefFree(virNodeDevCapsDef *caps)
         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:
@@ -2729,6 +2738,11 @@ virNodeDeviceUpdateCaps(virNodeDeviceDef *def)
                                                     &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.
@@ -2742,7 +2756,6 @@ virNodeDeviceUpdateCaps(virNodeDeviceDef *def)
         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:
@@ -3193,6 +3206,24 @@ virNodeDeviceGetAPMatrixDynamicCaps(const char *sysfsPath,
     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
@@ -3229,4 +3260,12 @@ virNodeDeviceGetAPMatrixDynamicCaps(const char *sysfsPath G_GNUC_UNUSED,
     return -1;
 }
 
+int
+virNodeDeviceGetMdevParentDynamicCaps(const char *sysfsPath G_GNUC_UNUSED,
+                                      virNodeDevCapMdevParent *mdev_parent G_GNUC_UNUSED)
+{
+    return -1;
+}
+
+
 #endif /* __linux__ */
diff --git a/src/conf/node_device_conf.h b/src/conf/node_device_conf.h
index a234b4147b..21622c62ac 100644
--- a/src/conf/node_device_conf.h
+++ b/src/conf/node_device_conf.h
@@ -306,6 +306,14 @@ struct _virNodeDevCapAPMatrix {
     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;
@@ -327,6 +335,7 @@ struct _virNodeDevCapData {
         virNodeDevCapAPCard ap_card;
         virNodeDevCapAPQueue ap_queue;
         virNodeDevCapAPMatrix ap_matrix;
+        virNodeDevCapMdevParent mdev_parent;
     };
 };
 
@@ -453,6 +462,10 @@ int
 virNodeDeviceGetAPMatrixDynamicCaps(const char *sysfsPath,
                                     virNodeDevCapAPMatrix *ap_matrix);
 
+int
+virNodeDeviceGetMdevParentDynamicCaps(const char *sysfsPath,
+                                      virNodeDevCapMdevParent *mdev_parent);
+
 int
 virNodeDeviceUpdateCaps(virNodeDeviceDef *def);
 
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index f4732f1742..1a5fce22e5 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -866,6 +866,7 @@ virNodeDeviceDefParseNode;
 virNodeDeviceDefParseString;
 virNodeDeviceGetAPMatrixDynamicCaps;
 virNodeDeviceGetCSSDynamicCaps;
+virNodeDeviceGetMdevParentDynamicCaps;
 virNodeDeviceGetPCIDynamicCaps;
 virNodeDeviceGetSCSIHostCaps;
 virNodeDeviceGetSCSITargetCaps;
@@ -2723,6 +2724,7 @@ virMediatedDeviceListStealIndex;
 virMediatedDeviceModelTypeFromString;
 virMediatedDeviceModelTypeToString;
 virMediatedDeviceNew;
+virMediatedDeviceParentGetAddress;
 virMediatedDeviceSetUsedBy;
 virMediatedDeviceTypeFree;
 virMediatedDeviceTypeReadAttrs;
diff --git a/src/node_device/node_device_driver.c b/src/node_device/node_device_driver.c
index 0178ae8b66..fa3cfcf24c 100644
--- a/src/node_device/node_device_driver.c
+++ b/src/node_device/node_device_driver.c
@@ -672,6 +672,10 @@ nodeDeviceObjFormatAddress(virNodeDeviceObj *obj)
             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:
@@ -684,7 +688,6 @@ nodeDeviceObjFormatAddress(virNodeDeviceObj *obj)
         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:
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 3d69bdedae..e76c1a36b8 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -457,6 +457,28 @@ udevProcessPCI(struct udev_device *device,
 }
 
 
+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)
 {
@@ -1349,6 +1371,8 @@ udevGetDeviceType(struct udev_device *device,
             *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);
     }
@@ -1404,6 +1428,7 @@ udevGetDeviceDetails(struct udev_device *device,
     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:
diff --git a/src/util/virmdev.c b/src/util/virmdev.c
index eb566982b1..41d4cef8b9 100644
--- a/src/util/virmdev.c
+++ b/src/util/virmdev.c
@@ -520,6 +520,34 @@ void virMediatedDeviceAttrFree(virMediatedDeviceAttr *attr)
 }
 
 
+#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
diff --git a/src/util/virmdev.h b/src/util/virmdev.h
index 4d3655a091..bc8306d0e1 100644
--- a/src/util/virmdev.h
+++ b/src/util/virmdev.h
@@ -149,5 +149,9 @@ virMediatedDeviceGetMdevTypes(const char *sysfspath,
                               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);
-- 
2.35.3
Re: [libvirt PATCH] nodedev: support 'mtty' device for testing
Posted by Michal Prívozník 1 year, 8 months ago
On 7/19/22 18:33, Jonathon Jongsma wrote:
> 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>
> ---
>  src/conf/node_device_conf.c          | 41 +++++++++++++++++++++++++++-
>  src/conf/node_device_conf.h          | 13 +++++++++
>  src/libvirt_private.syms             |  2 ++
>  src/node_device/node_device_driver.c |  5 +++-
>  src/node_device/node_device_udev.c   | 25 +++++++++++++++++
>  src/util/virmdev.c                   | 28 +++++++++++++++++++
>  src/util/virmdev.h                   |  4 +++
>  7 files changed, 116 insertions(+), 2 deletions(-)

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal