mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-27 11:01:19 +00:00
Move virNetDevGetIndex & virNetDevGetVLanID to virnetdev.c
Move virNetDevGetIndex & virNetDevGetVLanID to virnetdev.c to suit their functional purpose * util/interface.c, util/interface.h: Remove virNetDevGetIndex & virNetDevGetVLanID * util/virnetdev.c, util/virnetdev.h: Add virNetDevGetIndex & virNetDevGetVLanID
This commit is contained in:
parent
ebbb6bd11f
commit
00bba08d24
@ -46,6 +46,7 @@
|
|||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
#include "datatypes.h"
|
#include "datatypes.h"
|
||||||
#include "interface.h"
|
#include "interface.h"
|
||||||
|
#include "virnetdev.h"
|
||||||
#include "virterror_internal.h"
|
#include "virterror_internal.h"
|
||||||
#include "threads.h"
|
#include "threads.h"
|
||||||
#include "conf/nwfilter_params.h"
|
#include "conf/nwfilter_params.h"
|
||||||
|
@ -141,117 +141,6 @@ ifaceCheck(bool reportError ATTRIBUTE_UNUSED,
|
|||||||
#endif /* __linux__ */
|
#endif /* __linux__ */
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* virNetDevGetIndex
|
|
||||||
* @ifname : Name of the interface whose index is to be found
|
|
||||||
* @ifindex: Pointer to int where the index will be written into
|
|
||||||
*
|
|
||||||
* Get the index of an interface given its name.
|
|
||||||
*
|
|
||||||
* Returns 0 on success, -1 on failure
|
|
||||||
*/
|
|
||||||
#ifdef __linux__
|
|
||||||
int
|
|
||||||
virNetDevGetIndex(const char *ifname, int *ifindex)
|
|
||||||
{
|
|
||||||
int ret = -1;
|
|
||||||
struct ifreq ifreq;
|
|
||||||
int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
|
|
||||||
|
|
||||||
if (fd < 0) {
|
|
||||||
virReportSystemError(errno, "%s",
|
|
||||||
_("Unable to open control socket"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
memset(&ifreq, 0, sizeof(ifreq));
|
|
||||||
|
|
||||||
if (virStrncpy(ifreq.ifr_name, ifname, strlen(ifname),
|
|
||||||
sizeof(ifreq.ifr_name)) == NULL) {
|
|
||||||
virReportSystemError(ERANGE,
|
|
||||||
_("invalid interface name %s"),
|
|
||||||
ifname);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioctl(fd, SIOCGIFINDEX, &ifreq) < 0) {
|
|
||||||
virReportSystemError(errno,
|
|
||||||
_("Unable to get index for interface %s"), ifname);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
*ifindex = ifreq.ifr_ifindex;
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
int
|
|
||||||
virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
|
|
||||||
int *ifindex ATTRIBUTE_UNUSED)
|
|
||||||
{
|
|
||||||
virReportSystemError(ENOSYS, "%s",
|
|
||||||
_("Unable to get interface index on this platform"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif /* __linux__ */
|
|
||||||
|
|
||||||
#ifdef __linux__
|
|
||||||
int
|
|
||||||
virNetDevGetVLanID(const char *ifname, int *vlanid)
|
|
||||||
{
|
|
||||||
struct vlan_ioctl_args vlanargs = {
|
|
||||||
.cmd = GET_VLAN_VID_CMD,
|
|
||||||
};
|
|
||||||
int ret = -1;
|
|
||||||
int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
|
|
||||||
|
|
||||||
if (fd < 0) {
|
|
||||||
virReportSystemError(errno, "%s",
|
|
||||||
_("Unable to open control socket"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (virStrcpyStatic(vlanargs.device1, ifname) == NULL) {
|
|
||||||
virReportSystemError(ERANGE,
|
|
||||||
_("invalid interface name %s"),
|
|
||||||
ifname);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ioctl(fd, SIOCGIFVLAN, &vlanargs) != 0) {
|
|
||||||
virReportSystemError(errno,
|
|
||||||
_("Unable to get VLAN for interface %s"), ifname);
|
|
||||||
goto cleanup;
|
|
||||||
}
|
|
||||||
|
|
||||||
*vlanid = vlanargs.u.VID;
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
cleanup:
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
int
|
|
||||||
virNetDevGetVLanID(const char *ifname ATTRIBUTE_UNUSED,
|
|
||||||
int *vlanid ATTRIBUTE_UNUSED)
|
|
||||||
{
|
|
||||||
virReportSystemError(ENOSYS, "%s",
|
|
||||||
_("Unable to get VLAN on this platform"));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
#endif /* __linux__ */
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ifaceGetIPAddress:
|
* ifaceGetIPAddress:
|
||||||
* @ifname: name of the interface whose IP address we want
|
* @ifname: name of the interface whose IP address we want
|
||||||
|
@ -33,12 +33,6 @@ struct nlattr;
|
|||||||
int ifaceCheck(bool reportError, const char *ifname,
|
int ifaceCheck(bool reportError, const char *ifname,
|
||||||
const unsigned char *macaddr, int ifindex);
|
const unsigned char *macaddr, int ifindex);
|
||||||
|
|
||||||
int virNetDevGetIndex(const char *ifname, int *ifindex)
|
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
|
|
||||||
int virNetDevGetVLanID(const char *ifname, int *vlanid)
|
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
|
||||||
|
|
||||||
int ifaceGetIPAddress(const char *ifname, virSocketAddrPtr addr);
|
int ifaceGetIPAddress(const char *ifname, virSocketAddrPtr addr);
|
||||||
|
|
||||||
int ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex,
|
int ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex,
|
||||||
|
@ -34,6 +34,11 @@
|
|||||||
#endif
|
#endif
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
# include <linux/sockios.h>
|
||||||
|
# include <linux/if_vlan.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||||
#define virNetDevError(code, ...) \
|
#define virNetDevError(code, ...) \
|
||||||
virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \
|
virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \
|
||||||
@ -611,6 +616,110 @@ int virNetDevIsOnline(const char *ifname,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* virNetDevGetIndex:
|
||||||
|
* @ifname : Name of the interface whose index is to be found
|
||||||
|
* @ifindex: Pointer to int where the index will be written into
|
||||||
|
*
|
||||||
|
* Get the index of an interface given its name.
|
||||||
|
*
|
||||||
|
* Returns 0 on success, -1 on failure
|
||||||
|
*/
|
||||||
|
#ifdef __linux__
|
||||||
|
int virNetDevGetIndex(const char *ifname, int *ifindex)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
struct ifreq ifreq;
|
||||||
|
int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
|
if (fd < 0) {
|
||||||
|
virReportSystemError(errno, "%s",
|
||||||
|
_("Unable to open control socket"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(&ifreq, 0, sizeof(ifreq));
|
||||||
|
|
||||||
|
if (virStrncpy(ifreq.ifr_name, ifname, strlen(ifname),
|
||||||
|
sizeof(ifreq.ifr_name)) == NULL) {
|
||||||
|
virReportSystemError(ERANGE,
|
||||||
|
_("invalid interface name %s"),
|
||||||
|
ifname);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ioctl(fd, SIOCGIFINDEX, &ifreq) < 0) {
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("Unable to get index for interface %s"), ifname);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
*ifindex = ifreq.ifr_ifindex;
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#else /* ! __linux__ */
|
||||||
|
int virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED,
|
||||||
|
int *ifindex ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
virReportSystemError(ENOSYS, "%s",
|
||||||
|
_("Unable to get interface index on this platform"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif /* ! __linux__ */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __linux__
|
||||||
|
int virNetDevGetVLanID(const char *ifname, int *vlanid)
|
||||||
|
{
|
||||||
|
struct vlan_ioctl_args vlanargs = {
|
||||||
|
.cmd = GET_VLAN_VID_CMD,
|
||||||
|
};
|
||||||
|
int ret = -1;
|
||||||
|
int fd = socket(PF_PACKET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
|
if (fd < 0) {
|
||||||
|
virReportSystemError(errno, "%s",
|
||||||
|
_("Unable to open control socket"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (virStrcpyStatic(vlanargs.device1, ifname) == NULL) {
|
||||||
|
virReportSystemError(ERANGE,
|
||||||
|
_("invalid interface name %s"),
|
||||||
|
ifname);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ioctl(fd, SIOCGIFVLAN, &vlanargs) != 0) {
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("Unable to get VLAN for interface %s"), ifname);
|
||||||
|
goto cleanup;
|
||||||
|
}
|
||||||
|
|
||||||
|
*vlanid = vlanargs.u.VID;
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
cleanup:
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
#else /* ! __linux__ */
|
||||||
|
int virNetDevGetVLanID(const char *ifname ATTRIBUTE_UNUSED,
|
||||||
|
int *vlanid ATTRIBUTE_UNUSED)
|
||||||
|
{
|
||||||
|
virReportSystemError(ENOSYS, "%s",
|
||||||
|
_("Unable to get VLAN on this platform"));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
#endif /* ! __linux__ */
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* virNetDevSetIPv4Address:
|
* virNetDevSetIPv4Address:
|
||||||
* @ifname: the interface name
|
* @ifname: the interface name
|
||||||
|
@ -76,4 +76,11 @@ int virNetDevSetNamespace(const char *ifname, int pidInNs)
|
|||||||
int virNetDevSetName(const char *ifname, const char *newifname)
|
int virNetDevSetName(const char *ifname, const char *newifname)
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
int virNetDevGetIndex(const char *ifname, int *ifindex)
|
||||||
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
int virNetDevGetVLanID(const char *ifname, int *vlanid)
|
||||||
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
|
||||||
|
|
||||||
#endif /* __VIR_NETDEV_H__ */
|
#endif /* __VIR_NETDEV_H__ */
|
||||||
|
Loading…
Reference in New Issue
Block a user