From 00bba08d244137cc4e509588f006425677721165 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 3 Nov 2011 09:27:45 +0000 Subject: [PATCH] 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 --- src/nwfilter/nwfilter_learnipaddr.c | 1 + src/util/interface.c | 111 ---------------------------- src/util/interface.h | 6 -- src/util/virnetdev.c | 109 +++++++++++++++++++++++++++ src/util/virnetdev.h | 7 ++ 5 files changed, 117 insertions(+), 117 deletions(-) diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c index 9a51fc278a..319f317665 100644 --- a/src/nwfilter/nwfilter_learnipaddr.c +++ b/src/nwfilter/nwfilter_learnipaddr.c @@ -46,6 +46,7 @@ #include "logging.h" #include "datatypes.h" #include "interface.h" +#include "virnetdev.h" #include "virterror_internal.h" #include "threads.h" #include "conf/nwfilter_params.h" diff --git a/src/util/interface.c b/src/util/interface.c index af1def23cd..9762145f38 100644 --- a/src/util/interface.c +++ b/src/util/interface.c @@ -141,117 +141,6 @@ ifaceCheck(bool reportError ATTRIBUTE_UNUSED, #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: * @ifname: name of the interface whose IP address we want diff --git a/src/util/interface.h b/src/util/interface.h index 51d5c28e07..aa70192012 100644 --- a/src/util/interface.h +++ b/src/util/interface.h @@ -33,12 +33,6 @@ struct nlattr; int ifaceCheck(bool reportError, const char *ifname, 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 ifaceMacvtapLinkDump(bool nltarget_kernel, const char *ifname, int ifindex, diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 230dbeb324..c2a0f57fe9 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -34,6 +34,11 @@ #endif #include +#ifdef __linux__ +# include +# include +#endif + #define VIR_FROM_THIS VIR_FROM_NONE #define virNetDevError(code, ...) \ virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \ @@ -611,6 +616,110 @@ int virNetDevIsOnline(const char *ifname, #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: * @ifname: the interface name diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 84966d797d..8c1d7c6f0a 100644 --- a/src/util/virnetdev.h +++ b/src/util/virnetdev.h @@ -76,4 +76,11 @@ int virNetDevSetNamespace(const char *ifname, int pidInNs) int virNetDevSetName(const char *ifname, const char *newifname) 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__ */