mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-24 14:45:24 +00:00
Move functions for dealing with physical/virtual devices
Move virNetDevIsVirtualFunction, virNetDevGetVirtualFunctionIndex and virNetDevGetPhysicalFunction to virnetdev.c * src/util/interface.c, src/util/interface.h, src/util/virnetdev.c, src/util/virnetdev.h: Move APIs
This commit is contained in:
parent
8f688c85af
commit
74b32b6297
@ -45,7 +45,6 @@
|
||||
#include "virfile.h"
|
||||
#include "memory.h"
|
||||
#include "netlink.h"
|
||||
#include "pci.h"
|
||||
#include "logging.h"
|
||||
#include "virnetdev.h"
|
||||
|
||||
@ -295,148 +294,3 @@ ifaceGetNthParent(int ifindex ATTRIBUTE_UNUSED,
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __linux__
|
||||
static int
|
||||
virNetDevSysfsFile(char **pf_sysfs_device_link, const char *ifname,
|
||||
const char *file)
|
||||
{
|
||||
|
||||
if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/%s",
|
||||
ifname, file) < 0) {
|
||||
virReportOOMError();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, const char *ifname,
|
||||
const char *file)
|
||||
{
|
||||
|
||||
if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/device/%s",
|
||||
ifname, file) < 0) {
|
||||
virReportOOMError();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* virNetDevIsVirtualFunction:
|
||||
* @ifname : name of the interface
|
||||
*
|
||||
* Checks if an interface is a SRIOV virtual function.
|
||||
*
|
||||
* Returns 1 if interface is SRIOV virtual function, 0 if not and -1 if error
|
||||
*
|
||||
*/
|
||||
int
|
||||
virNetDevIsVirtualFunction(const char *ifname)
|
||||
{
|
||||
char *if_sysfs_device_link = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (virNetDevSysfsFile(&if_sysfs_device_link, ifname, "device") < 0)
|
||||
return ret;
|
||||
|
||||
ret = pciDeviceIsVirtualFunction(if_sysfs_device_link);
|
||||
|
||||
VIR_FREE(if_sysfs_device_link);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* virNetDevGetVirtualFunctionIndex
|
||||
*
|
||||
* @pfname : name of the physical function interface name
|
||||
* @vfname : name of the virtual function interface name
|
||||
* @vf_index : Pointer to int. Contains vf index of interface upon successful
|
||||
* return
|
||||
*
|
||||
* Returns 0 on success, -1 on failure
|
||||
*
|
||||
*/
|
||||
int
|
||||
virNetDevGetVirtualFunctionIndex(const char *pfname, const char *vfname,
|
||||
int *vf_index)
|
||||
{
|
||||
char *pf_sysfs_device_link = NULL, *vf_sysfs_device_link = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (virNetDevSysfsFile(&pf_sysfs_device_link, pfname, "device") < 0)
|
||||
return ret;
|
||||
|
||||
if (virNetDevSysfsFile(&vf_sysfs_device_link, vfname, "device") < 0) {
|
||||
VIR_FREE(pf_sysfs_device_link);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = pciGetVirtualFunctionIndex(pf_sysfs_device_link,
|
||||
vf_sysfs_device_link,
|
||||
vf_index);
|
||||
|
||||
VIR_FREE(pf_sysfs_device_link);
|
||||
VIR_FREE(vf_sysfs_device_link);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* virNetDevGetPhysicalFunction
|
||||
*
|
||||
* @ifname : name of the physical function interface name
|
||||
* @pfname : Contains sriov physical function for interface ifname
|
||||
* upon successful return
|
||||
*
|
||||
* Returns 0 on success, -1 on failure
|
||||
*
|
||||
*/
|
||||
int
|
||||
virNetDevGetPhysicalFunction(const char *ifname, char **pfname)
|
||||
{
|
||||
char *physfn_sysfs_path = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (virNetDevSysfsDeviceFile(&physfn_sysfs_path, ifname, "physfn") < 0)
|
||||
return ret;
|
||||
|
||||
ret = pciDeviceNetName(physfn_sysfs_path, pfname);
|
||||
|
||||
VIR_FREE(physfn_sysfs_path);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#else /* !__linux__ */
|
||||
int
|
||||
virNetDevIsVirtualFunction(const char *ifname ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virReportSystemError(ENOSYS, "%s",
|
||||
_("Unable to check virtual function status on this platfornm"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
virNetDevGetVirtualFunctionIndex(const char *pfname ATTRIBUTE_UNUSED,
|
||||
const char *vfname ATTRIBUTE_UNUSED,
|
||||
int *vf_index ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virReportSystemError(ENOSYS, "%s",
|
||||
_("Unable to get virtual function index on this platfornm"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
virNetDevGetPhysicalFunction(const char *ifname ATTRIBUTE_UNUSED,
|
||||
char **pfname ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virReportSystemError(ENOSYS, "%s",
|
||||
_("Unable to get physical function status on this platfornm"));
|
||||
return -1;
|
||||
}
|
||||
#endif /* !__linux__ */
|
||||
|
@ -27,8 +27,6 @@ struct nlattr;
|
||||
# include "datatypes.h"
|
||||
# include "virsocketaddr.h"
|
||||
|
||||
# define NET_SYSFS "/sys/class/net/"
|
||||
|
||||
|
||||
int ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex,
|
||||
struct nlattr **tb, unsigned char **recvbuf,
|
||||
@ -40,15 +38,4 @@ int ifaceGetNthParent(int ifindex, const char *ifname, unsigned int nthParent,
|
||||
ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(5)
|
||||
ATTRIBUTE_NONNULL(6);
|
||||
|
||||
int virNetDevIsVirtualFunction(const char *ifname)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
int virNetDevGetVirtualFunctionIndex(const char *pfname, const char *vfname,
|
||||
int *vf_index)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
||||
ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
int virNetDevGetPhysicalFunction(const char *ifname, char **pfname)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
#endif /* __VIR_INTERFACE_H__ */
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "virterror_internal.h"
|
||||
#include "command.h"
|
||||
#include "memory.h"
|
||||
#include "pci.h"
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#ifdef HAVE_NET_IF_H
|
||||
@ -933,3 +934,150 @@ int virNetDevValidateConfig(const char *ifname ATTRIBUTE_UNUSED,
|
||||
return -1;
|
||||
}
|
||||
#endif /* ! __linux__ */
|
||||
|
||||
|
||||
#ifdef __linux__
|
||||
# define NET_SYSFS "/sys/class/net/"
|
||||
|
||||
static int
|
||||
virNetDevSysfsFile(char **pf_sysfs_device_link, const char *ifname,
|
||||
const char *file)
|
||||
{
|
||||
|
||||
if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/%s",
|
||||
ifname, file) < 0) {
|
||||
virReportOOMError();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
virNetDevSysfsDeviceFile(char **pf_sysfs_device_link, const char *ifname,
|
||||
const char *file)
|
||||
{
|
||||
|
||||
if (virAsprintf(pf_sysfs_device_link, NET_SYSFS "%s/device/%s",
|
||||
ifname, file) < 0) {
|
||||
virReportOOMError();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* virNetDevIsVirtualFunction:
|
||||
* @ifname : name of the interface
|
||||
*
|
||||
* Checks if an interface is a SRIOV virtual function.
|
||||
*
|
||||
* Returns 1 if interface is SRIOV virtual function, 0 if not and -1 if error
|
||||
*
|
||||
*/
|
||||
int
|
||||
virNetDevIsVirtualFunction(const char *ifname)
|
||||
{
|
||||
char *if_sysfs_device_link = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (virNetDevSysfsFile(&if_sysfs_device_link, ifname, "device") < 0)
|
||||
return ret;
|
||||
|
||||
ret = pciDeviceIsVirtualFunction(if_sysfs_device_link);
|
||||
|
||||
VIR_FREE(if_sysfs_device_link);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* virNetDevGetVirtualFunctionIndex
|
||||
*
|
||||
* @pfname : name of the physical function interface name
|
||||
* @vfname : name of the virtual function interface name
|
||||
* @vf_index : Pointer to int. Contains vf index of interface upon successful
|
||||
* return
|
||||
*
|
||||
* Returns 0 on success, -1 on failure
|
||||
*
|
||||
*/
|
||||
int
|
||||
virNetDevGetVirtualFunctionIndex(const char *pfname, const char *vfname,
|
||||
int *vf_index)
|
||||
{
|
||||
char *pf_sysfs_device_link = NULL, *vf_sysfs_device_link = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (virNetDevSysfsFile(&pf_sysfs_device_link, pfname, "device") < 0)
|
||||
return ret;
|
||||
|
||||
if (virNetDevSysfsFile(&vf_sysfs_device_link, vfname, "device") < 0) {
|
||||
VIR_FREE(pf_sysfs_device_link);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = pciGetVirtualFunctionIndex(pf_sysfs_device_link,
|
||||
vf_sysfs_device_link,
|
||||
vf_index);
|
||||
|
||||
VIR_FREE(pf_sysfs_device_link);
|
||||
VIR_FREE(vf_sysfs_device_link);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* virNetDevGetPhysicalFunction
|
||||
*
|
||||
* @ifname : name of the physical function interface name
|
||||
* @pfname : Contains sriov physical function for interface ifname
|
||||
* upon successful return
|
||||
*
|
||||
* Returns 0 on success, -1 on failure
|
||||
*
|
||||
*/
|
||||
int
|
||||
virNetDevGetPhysicalFunction(const char *ifname, char **pfname)
|
||||
{
|
||||
char *physfn_sysfs_path = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if (virNetDevSysfsDeviceFile(&physfn_sysfs_path, ifname, "physfn") < 0)
|
||||
return ret;
|
||||
|
||||
ret = pciDeviceNetName(physfn_sysfs_path, pfname);
|
||||
|
||||
VIR_FREE(physfn_sysfs_path);
|
||||
|
||||
return ret;
|
||||
}
|
||||
#else /* !__linux__ */
|
||||
int
|
||||
virNetDevIsVirtualFunction(const char *ifname ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virReportSystemError(ENOSYS, "%s",
|
||||
_("Unable to check virtual function status on this platfornm"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
virNetDevGetVirtualFunctionIndex(const char *pfname ATTRIBUTE_UNUSED,
|
||||
const char *vfname ATTRIBUTE_UNUSED,
|
||||
int *vf_index ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virReportSystemError(ENOSYS, "%s",
|
||||
_("Unable to get virtual function index on this platfornm"));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int
|
||||
virNetDevGetPhysicalFunction(const char *ifname ATTRIBUTE_UNUSED,
|
||||
char **pfname ATTRIBUTE_UNUSED)
|
||||
{
|
||||
virReportSystemError(ENOSYS, "%s",
|
||||
_("Unable to get physical function status on this platfornm"));
|
||||
return -1;
|
||||
}
|
||||
#endif /* !__linux__ */
|
||||
|
@ -88,5 +88,15 @@ int virNetDevValidateConfig(const char *ifname,
|
||||
const unsigned char *macaddr, int ifindex)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
int virNetDevIsVirtualFunction(const char *ifname)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
int virNetDevGetVirtualFunctionIndex(const char *pfname, const char *vfname,
|
||||
int *vf_index)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3)
|
||||
ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
int virNetDevGetPhysicalFunction(const char *ifname, char **pfname)
|
||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
||||
|
||||
#endif /* __VIR_NETDEV_H__ */
|
||||
|
Loading…
Reference in New Issue
Block a user