From: "Daniel P. Berrange" <berrange@xxxxxxxxxx> 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 9a51fc2..319f317 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 af1def2..9762145 100644 --- a/src/util/interface.c +++ b/src/util/interface.c @@ -142,117 +142,6 @@ ifaceCheck(bool reportError ATTRIBUTE_UNUSED, /** - * 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 * @macaddr: MAC address (VIR_MAC_BUFLEN in size) diff --git a/src/util/interface.h b/src/util/interface.h index 51d5c28..aa70192 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 230dbeb..c2a0f57 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -34,6 +34,11 @@ #endif #include <fcntl.h> +#ifdef __linux__ +# include <linux/sockios.h> +# include <linux/if_vlan.h> +#endif + #define VIR_FROM_THIS VIR_FROM_NONE #define virNetDevError(code, ...) \ virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \ @@ -612,6 +617,110 @@ int virNetDevIsOnline(const char *ifname, /** + * 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 * @addr: the IP address (IPv4 or IPv6) diff --git a/src/util/virnetdev.h b/src/util/virnetdev.h index 84966d7..8c1d7c6 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__ */ -- 1.7.6.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list