util: refactor mdev_types method from PCI to mdev

Extract virPCIGetMdevTypes from PCI as virMediatedDeviceGetMdevTypes
into mdev for later reuse.

Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Reviewed-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Ján Tomko <jtomko@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Boris Fiuczynski 2020-10-23 19:31:44 +02:00 committed by Ján Tomko
parent 689ff3c6c6
commit 65c1f47760
6 changed files with 71 additions and 64 deletions

View File

@ -2598,7 +2598,7 @@ virNodeDeviceGetPCIMdevTypesCaps(const char *sysfspath,
pci_dev->nmdev_types = 0;
pci_dev->flags &= ~VIR_NODE_DEV_CAP_FLAG_PCI_MDEV;
rc = virPCIGetMdevTypes(sysfspath, &types);
rc = virMediatedDeviceGetMdevTypes(sysfspath, &types);
if (rc <= 0)
return rc;

View File

@ -2509,6 +2509,7 @@ virMediatedDeviceAttrNew;
virMediatedDeviceFree;
virMediatedDeviceGetIOMMUGroupDev;
virMediatedDeviceGetIOMMUGroupNum;
virMediatedDeviceGetMdevTypes;
virMediatedDeviceGetSysfsPath;
virMediatedDeviceGetUsedBy;
virMediatedDeviceIsUsed;
@ -2845,7 +2846,6 @@ virPCIELinkSpeedTypeFromString;
virPCIELinkSpeedTypeToString;
virPCIGetDeviceAddressFromSysfsLink;
virPCIGetHeaderType;
virPCIGetMdevTypes;
virPCIGetNetName;
virPCIGetPhysicalFunction;
virPCIGetVirtualFunctionIndex;

View File

@ -522,3 +522,68 @@ void virMediatedDeviceAttrFree(virMediatedDeviceAttrPtr attr)
g_free(attr->value);
g_free(attr);
}
#ifdef __linux__
ssize_t
virMediatedDeviceGetMdevTypes(const char *sysfspath,
virMediatedDeviceTypePtr **types)
{
ssize_t ret = -1;
int dirret = -1;
g_autoptr(DIR) dir = NULL;
struct dirent *entry;
g_autofree char *types_path = NULL;
g_autoptr(virMediatedDeviceType) mdev_type = NULL;
virMediatedDeviceTypePtr *mdev_types = NULL;
size_t ntypes = 0;
size_t i;
types_path = g_strdup_printf("%s/mdev_supported_types", sysfspath);
if ((dirret = virDirOpenIfExists(&dir, types_path)) < 0)
goto cleanup;
if (dirret == 0) {
ret = 0;
goto cleanup;
}
while ((dirret = virDirRead(dir, &entry, types_path)) > 0) {
g_autofree char *tmppath = NULL;
/* append the type id to the path and read the attributes from there */
tmppath = g_strdup_printf("%s/%s", types_path, entry->d_name);
if (virMediatedDeviceTypeReadAttrs(tmppath, &mdev_type) < 0)
goto cleanup;
if (VIR_APPEND_ELEMENT(mdev_types, ntypes, mdev_type) < 0)
goto cleanup;
}
if (dirret < 0)
goto cleanup;
*types = g_steal_pointer(&mdev_types);
ret = ntypes;
ntypes = 0;
cleanup:
for (i = 0; i < ntypes; i++)
virMediatedDeviceTypeFree(mdev_types[i]);
VIR_FREE(mdev_types);
return ret;
}
#else
static const char *unsupported = N_("not supported on non-linux platforms");
ssize_t
virMediatedDeviceGetMdevTypes(const char *sysfspath G_GNUC_UNUSED,
virMediatedDeviceTypePtr **types G_GNUC_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
}
#endif /* __linux__ */

View File

@ -149,5 +149,9 @@ int
virMediatedDeviceTypeReadAttrs(const char *sysfspath,
virMediatedDeviceTypePtr *type);
ssize_t
virMediatedDeviceGetMdevTypes(const char *sysfspath,
virMediatedDeviceTypePtr **types);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDevice, virMediatedDeviceFree);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virMediatedDeviceType, virMediatedDeviceTypeFree);

View File

@ -2506,56 +2506,6 @@ virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
return 0;
}
ssize_t
virPCIGetMdevTypes(const char *sysfspath,
virMediatedDeviceTypePtr **types)
{
ssize_t ret = -1;
int dirret = -1;
g_autoptr(DIR) dir = NULL;
struct dirent *entry;
g_autofree char *types_path = NULL;
g_autoptr(virMediatedDeviceType) mdev_type = NULL;
virMediatedDeviceTypePtr *mdev_types = NULL;
size_t ntypes = 0;
size_t i;
types_path = g_strdup_printf("%s/mdev_supported_types", sysfspath);
if ((dirret = virDirOpenIfExists(&dir, types_path)) < 0)
goto cleanup;
if (dirret == 0) {
ret = 0;
goto cleanup;
}
while ((dirret = virDirRead(dir, &entry, types_path)) > 0) {
g_autofree char *tmppath = NULL;
/* append the type id to the path and read the attributes from there */
tmppath = g_strdup_printf("%s/%s", types_path, entry->d_name);
if (virMediatedDeviceTypeReadAttrs(tmppath, &mdev_type) < 0)
goto cleanup;
if (VIR_APPEND_ELEMENT(mdev_types, ntypes, mdev_type) < 0)
goto cleanup;
}
if (dirret < 0)
goto cleanup;
*types = g_steal_pointer(&mdev_types);
ret = ntypes;
ntypes = 0;
cleanup:
for (i = 0; i < ntypes; i++)
virMediatedDeviceTypeFree(mdev_types[i]);
VIR_FREE(mdev_types);
return ret;
}
#else
static const char *unsupported = N_("not supported on non-linux platforms");
@ -2630,15 +2580,6 @@ virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path G_GNUC_UNUSED,
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
}
ssize_t
virPCIGetMdevTypes(const char *sysfspath G_GNUC_UNUSED,
virMediatedDeviceTypePtr **types G_GNUC_UNUSED)
{
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
return -1;
}
#endif /* __linux__ */
int

View File

@ -275,9 +275,6 @@ int virPCIGetHeaderType(virPCIDevicePtr dev, int *hdrType);
void virPCIEDeviceInfoFree(virPCIEDeviceInfoPtr dev);
ssize_t virPCIGetMdevTypes(const char *sysfspath,
virMediatedDeviceType ***types);
void virPCIDeviceAddressFree(virPCIDeviceAddressPtr address);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virPCIDevice, virPCIDeviceFree);