util: add virNetDevGetPhysPortName

This commit add virNetDevGetPhysPortName to read netdevice
phys_port_name from sysfs. It also refactor the code so
virNetDevGetPhysPortName and virNetDevGetPhysPortID will use
same method to read the netdevice sysfs.

Signed-off-by: Moshe Levi <moshele@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
This commit is contained in:
Moshe Levi 2021-01-21 14:15:21 +02:00 committed by Laine Stump
parent 36aabc86d5
commit 97ebb98245
2 changed files with 59 additions and 12 deletions

View File

@ -1147,6 +1147,29 @@ virNetDevGetPCIDevice(const char *devName)
# endif
/* A wrapper to get content of file from ifname SYSFS_NET_DIR
*/
static int
virNetDevGetSysfsFileValue(const char *ifname,
const char *fileName,
char **sysfsFileData)
{
g_autofree char *sysfsFile = NULL;
*sysfsFileData = NULL;
if (virNetDevSysfsFile(&sysfsFile, ifname, fileName) < 0)
return -1;
/* a failure to read just means the driver doesn't support
* <fileName>, so set success now and ignore the return from
* virFileReadAllQuiet().
*/
ignore_value(virFileReadAllQuiet(sysfsFile, 1024, sysfsFileData));
return 0;
}
/**
* virNetDevGetPhysPortID:
*
@ -1165,20 +1188,29 @@ int
virNetDevGetPhysPortID(const char *ifname,
char **physPortID)
{
g_autofree char *physPortIDFile = NULL;
return virNetDevGetSysfsFileValue(ifname, "phys_port_id", physPortID);
}
*physPortID = NULL;
if (virNetDevSysfsFile(&physPortIDFile, ifname, "phys_port_id") < 0)
return -1;
/* a failure to read just means the driver doesn't support
* phys_port_id, so set success now and ignore the return from
* virFileReadAllQuiet().
*/
ignore_value(virFileReadAllQuiet(physPortIDFile, 1024, physPortID));
return 0;
/**
* virNetDevGetPhysPortName:
*
* @ifname: name of a netdev
*
* @physPortName: pointer to char* that will receive @ifname's
* phys_port_name from sysfs (null terminated
* string). Could be NULL if @ifname's net driver doesn't
* support phys_port_name (most netdev drivers
* don't). Caller is responsible for freeing the string
* when finished.
*
* Returns 0 on success or -1 on failure.
*/
int
virNetDevGetPhysPortName(const char *ifname,
char **physPortName)
{
return virNetDevGetSysfsFileValue(ifname, "phys_port_name", physPortName);
}
@ -1433,6 +1465,17 @@ virNetDevGetPhysPortID(const char *ifname G_GNUC_UNUSED,
return 0;
}
int
virNetDevGetPhysPortName(const char *ifname G_GNUC_UNUSED,
char **physPortName)
{
/* this actually should never be called, and is just here to
* satisfy the linker.
*/
*physPortName = NULL;
return 0;
}
int
virNetDevGetVirtualFunctions(const char *pfname G_GNUC_UNUSED,
char ***vfname G_GNUC_UNUSED,

View File

@ -250,6 +250,10 @@ int virNetDevGetPhysPortID(const char *ifname,
char **physPortID)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
G_GNUC_WARN_UNUSED_RESULT;
int virNetDevGetPhysPortName(const char *ifname,
char **physPortName)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2)
G_GNUC_WARN_UNUSED_RESULT;
int virNetDevGetVirtualFunctions(const char *pfname,
char ***vfname,