diff --git a/po/POTFILES.in b/po/POTFILES.in index b0a1ed401becfc8730fd14287231a4d0c8fbf1d3..e66bb7a3abcc2e5554c23750df5610fc145c5d89 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -242,7 +242,6 @@ src/util/virscsivhost.c src/util/virsecret.c src/util/virsexpr.c src/util/virsocketaddr.c -src/util/virstats.c src/util/virstorageencryption.c src/util/virstoragefile.c src/util/virstring.c diff --git a/src/Makefile.am b/src/Makefile.am index f87fd473b13c69a85c695f69010acfca45c67a2e..9f808bef7b22212082438d2a426da3dcdb9d6ae2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -169,7 +169,6 @@ UTIL_SOURCES = \ util/virsecret.c util/virsecret.h \ util/virsexpr.c util/virsexpr.h \ util/virsocketaddr.h util/virsocketaddr.c \ - util/virstats.c util/virstats.h \ util/virstorageencryption.c util/virstorageencryption.h \ util/virstoragefile.c util/virstoragefile.h \ util/virstring.h util/virstring.c \ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 46051a1048afcbccdbd7db42a125c1e52ae7dfdb..867acdb2cf31a03bb8d40cbc570136308f860f8c 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2058,6 +2058,7 @@ virNetDevTapCreateInBridgePort; virNetDevTapDelete; virNetDevTapGetName; virNetDevTapGetRealDeviceName; +virNetDevTapInterfaceStats; # util/virnetdevveth.h @@ -2396,9 +2397,6 @@ virSocketAddrSetIPv6Addr; virSocketAddrSetIPv6AddrNetOrder; virSocketAddrSetPort; -# util/virstats.h -virNetDevTapInterfaceStats; - # util/virstorageencryption.h virStorageEncryptionFormat; virStorageEncryptionFree; diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index e0cb14740a0622811231f38cafecc8e2a3ee5e81..b19b17e2c452354fae422989649784f944dcb4be 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -61,7 +61,7 @@ #include "virhostdev.h" #include "network/bridge_driver.h" #include "locking/domain_lock.h" -#include "virstats.h" +#include "virnetdevtap.h" #include "cpu/cpu.h" #define VIR_FROM_THIS VIR_FROM_LIBXL diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 64ff83dd307d5e6dd27d60fd6d505cf26e84e735..a7bc9f06b4fdf862579f83b7a9bf10d6c4333432 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -61,7 +61,6 @@ #include "virhostcpu.h" #include "virhostmem.h" #include "viruuid.h" -#include "virstats.h" #include "virhook.h" #include "virfile.h" #include "virpidfile.h" diff --git a/src/openvz/openvz_driver.c b/src/openvz/openvz_driver.c index cdf39275b50af8792178b7f8c7a3b6c775f0ff9c..eaa9ef68c57671678c9f650dd63d20213bc48332 100644 --- a/src/openvz/openvz_driver.c +++ b/src/openvz/openvz_driver.c @@ -57,7 +57,7 @@ #include "virlog.h" #include "vircommand.h" #include "viruri.h" -#include "virstats.h" +#include "virnetdevtap.h" #include "virstring.h" #define VIR_FROM_THIS VIR_FROM_OPENVZ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 54841a074daee2be0b748010c04673a33a1035b9..842de0a484f8c4f93ac46286536084170292eda7 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -65,7 +65,7 @@ #include "nodeinfo.h" #include "virhostcpu.h" #include "virhostmem.h" -#include "virstats.h" +#include "virnetdevtap.h" #include "virnetdevopenvswitch.h" #include "capabilities.h" #include "viralloc.h" diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c index ad89e3ef7ff80aee141bd70fa72892f1d6400d54..f0c0ad35a8beec04f13ead3186315ea8e501cb2e 100644 --- a/src/uml/uml_driver.c +++ b/src/uml/uml_driver.c @@ -48,7 +48,6 @@ #include "nodeinfo.h" #include "virhostcpu.h" #include "virhostmem.h" -#include "virstats.h" #include "capabilities.h" #include "viralloc.h" #include "viruuid.h" diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c index 7488a4c0f16232247440a8ff22b1558cb33cf567..85c0045ea87b34e245a535377b86760b6eaa3d6e 100644 --- a/src/util/virnetdevtap.c +++ b/src/util/virnetdevtap.c @@ -33,7 +33,13 @@ #include "viralloc.h" #include "virlog.h" #include "virstring.h" +#include "datatypes.h" +#include +#include +#include +#include +#include #include #include #include @@ -44,6 +50,9 @@ #elif defined(__FreeBSD__) # include #endif +#if defined(HAVE_GETIFADDRS) && defined(AF_LINK) +# include +#endif #define VIR_FROM_THIS VIR_FROM_NONE @@ -601,3 +610,137 @@ int virNetDevTapCreateInBridgePort(const char *brname, return -1; } + +/*-------------------- interface stats --------------------*/ +/* Just reads the named interface, so not Xen or QEMU-specific. + * NB. Caller must check that libvirt user is trying to query + * the interface of a domain they own. We do no such checking. + */ +#ifdef __linux__ +int +virNetDevTapInterfaceStats(const char *ifname, + virDomainInterfaceStatsPtr stats) +{ + int ifname_len; + FILE *fp; + char line[256], *colon; + + fp = fopen("/proc/net/dev", "r"); + if (!fp) { + virReportSystemError(errno, "%s", + _("Could not open /proc/net/dev")); + return -1; + } + + ifname_len = strlen(ifname); + + while (fgets(line, sizeof(line), fp)) { + long long dummy; + long long rx_bytes; + long long rx_packets; + long long rx_errs; + long long rx_drop; + long long tx_bytes; + long long tx_packets; + long long tx_errs; + long long tx_drop; + + /* The line looks like: + * " eth0:..." + * Split it at the colon. + */ + colon = strchr(line, ':'); + if (!colon) continue; + *colon = '\0'; + if (colon-ifname_len >= line && + STREQ(colon-ifname_len, ifname)) { + /* IMPORTANT NOTE! + * /proc/net/dev vif.nn sees the network from the point + * of view of dom0 / hypervisor. So bytes TRANSMITTED by dom0 + * are bytes RECEIVED by the domain. That's why the TX/RX fields + * appear to be swapped here. + */ + if (sscanf(colon+1, + "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld", + &tx_bytes, &tx_packets, &tx_errs, &tx_drop, + &dummy, &dummy, &dummy, &dummy, + &rx_bytes, &rx_packets, &rx_errs, &rx_drop, + &dummy, &dummy, &dummy, &dummy) != 16) + continue; + + stats->rx_bytes = rx_bytes; + stats->rx_packets = rx_packets; + stats->rx_errs = rx_errs; + stats->rx_drop = rx_drop; + stats->tx_bytes = tx_bytes; + stats->tx_packets = tx_packets; + stats->tx_errs = tx_errs; + stats->tx_drop = tx_drop; + VIR_FORCE_FCLOSE(fp); + + return 0; + } + } + VIR_FORCE_FCLOSE(fp); + + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("/proc/net/dev: Interface not found")); + return -1; +} +#elif defined(HAVE_GETIFADDRS) && defined(AF_LINK) +int +virNetDevTapInterfaceStats(const char *ifname, + virDomainInterfaceStatsPtr stats) +{ + struct ifaddrs *ifap, *ifa; + struct if_data *ifd; + int ret = -1; + + if (getifaddrs(&ifap) < 0) { + virReportSystemError(errno, "%s", + _("Could not get interface list")); + return -1; + } + + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { + if (ifa->ifa_addr->sa_family != AF_LINK) + continue; + + if (STREQ(ifa->ifa_name, ifname)) { + ifd = (struct if_data *)ifa->ifa_data; + stats->tx_bytes = ifd->ifi_ibytes; + stats->tx_packets = ifd->ifi_ipackets; + stats->tx_errs = ifd->ifi_ierrors; + stats->tx_drop = ifd->ifi_iqdrops; + stats->rx_bytes = ifd->ifi_obytes; + stats->rx_packets = ifd->ifi_opackets; + stats->rx_errs = ifd->ifi_oerrors; +# ifdef HAVE_STRUCT_IF_DATA_IFI_OQDROPS + stats->rx_drop = ifd->ifi_oqdrops; +# else + stats->rx_drop = 0; +# endif + + ret = 0; + break; + } + } + + if (ret < 0) + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Interface not found")); + + freeifaddrs(ifap); + return ret; +} +#else +int +virNetDevTapInterfaceStats(const char *ifname ATTRIBUTE_UNUSED, + virDomainInterfaceStatsPtr stats ATTRIBUTE_UNUSED) +{ + virReportError(VIR_ERR_OPERATION_INVALID, "%s", + _("interface stats not implemented on this platform")); + return -1; +} + +#endif /* __linux__ */ diff --git a/src/util/virnetdevtap.h b/src/util/virnetdevtap.h index 20dec58b7f73468170ff785cc885c9c5de9a3195..259e7c9c2024890ff7468105fa1e28169d7e9921 100644 --- a/src/util/virnetdevtap.h +++ b/src/util/virnetdevtap.h @@ -75,5 +75,8 @@ int virNetDevTapCreateInBridgePort(const char *brname, ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK; +int virNetDevTapInterfaceStats(const char *ifname, + virDomainInterfaceStatsPtr stats) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; #endif /* __VIR_NETDEV_TAP_H__ */ diff --git a/src/util/virstats.c b/src/util/virstats.c deleted file mode 100644 index eadb3c97881d33e46fe7b2376d7ca2a6de506987..0000000000000000000000000000000000000000 --- a/src/util/virstats.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * virstats.c: Block and network stats. - * - * Copyright (C) 2007-2010, 2014 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - * - * Richard W.M. Jones - */ - -#include - -#include -#include -#include -#include -#include -#include - -#if defined(HAVE_GETIFADDRS) && defined(AF_LINK) -# include -# include -#endif - -#include "virerror.h" -#include "datatypes.h" -#include "virstats.h" -#include "viralloc.h" -#include "virfile.h" - -#define VIR_FROM_THIS VIR_FROM_STATS_LINUX - - -/*-------------------- interface stats --------------------*/ -/* Just reads the named interface, so not Xen or QEMU-specific. - * NB. Caller must check that libvirt user is trying to query - * the interface of a domain they own. We do no such checking. - */ -#ifdef __linux__ -int -virNetDevTapInterfaceStats(const char *ifname, - virDomainInterfaceStatsPtr stats) -{ - int ifname_len; - FILE *fp; - char line[256], *colon; - - fp = fopen("/proc/net/dev", "r"); - if (!fp) { - virReportSystemError(errno, "%s", - _("Could not open /proc/net/dev")); - return -1; - } - - ifname_len = strlen(ifname); - - while (fgets(line, sizeof(line), fp)) { - long long dummy; - long long rx_bytes; - long long rx_packets; - long long rx_errs; - long long rx_drop; - long long tx_bytes; - long long tx_packets; - long long tx_errs; - long long tx_drop; - - /* The line looks like: - * " eth0:..." - * Split it at the colon. - */ - colon = strchr(line, ':'); - if (!colon) continue; - *colon = '\0'; - if (colon - ifname_len >= line && - STREQ(colon - ifname_len, ifname)) { - /* IMPORTANT NOTE! - * /proc/net/dev vif.nn sees the network from the point - * of view of dom0 / hypervisor. So bytes TRANSMITTED by dom0 - * are bytes RECEIVED by the domain. That's why the TX/RX fields - * appear to be swapped here. - */ - if (sscanf(colon + 1, - "%lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld", - &tx_bytes, &tx_packets, &tx_errs, &tx_drop, - &dummy, &dummy, &dummy, &dummy, - &rx_bytes, &rx_packets, &rx_errs, &rx_drop, - &dummy, &dummy, &dummy, &dummy) != 16) - continue; - - stats->rx_bytes = rx_bytes; - stats->rx_packets = rx_packets; - stats->rx_errs = rx_errs; - stats->rx_drop = rx_drop; - stats->tx_bytes = tx_bytes; - stats->tx_packets = tx_packets; - stats->tx_errs = tx_errs; - stats->tx_drop = tx_drop; - VIR_FORCE_FCLOSE(fp); - - return 0; - } - } - VIR_FORCE_FCLOSE(fp); - - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("/proc/net/dev: Interface not found")); - return -1; -} -#elif defined(HAVE_GETIFADDRS) && defined(AF_LINK) -int -virNetDevTapInterfaceStats(const char *ifname, - virDomainInterfaceStatsPtr stats) -{ - struct ifaddrs *ifap, *ifa; - struct if_data *ifd; - int ret = -1; - - if (getifaddrs(&ifap) < 0) { - virReportSystemError(errno, "%s", - _("Could not get interface list")); - return -1; - } - - for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr->sa_family != AF_LINK) - continue; - - if (STREQ(ifa->ifa_name, ifname)) { - ifd = (struct if_data *)ifa->ifa_data; - stats->tx_bytes = ifd->ifi_ibytes; - stats->tx_packets = ifd->ifi_ipackets; - stats->tx_errs = ifd->ifi_ierrors; - stats->tx_drop = ifd->ifi_iqdrops; - stats->rx_bytes = ifd->ifi_obytes; - stats->rx_packets = ifd->ifi_opackets; - stats->rx_errs = ifd->ifi_oerrors; -# ifdef HAVE_STRUCT_IF_DATA_IFI_OQDROPS - stats->rx_drop = ifd->ifi_oqdrops; -# else - stats->rx_drop = 0; -# endif - - ret = 0; - break; - } - } - - if (ret < 0) - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Interface not found")); - - freeifaddrs(ifap); - return ret; -} -#else -int -virNetDevTapInterfaceStats(const char *ifname ATTRIBUTE_UNUSED, - virDomainInterfaceStatsPtr stats ATTRIBUTE_UNUSED) -{ - virReportError(VIR_ERR_OPERATION_INVALID, "%s", - _("interface stats not implemented on this platform")); - return -1; -} - -#endif /* __linux__ */ diff --git a/src/util/virstats.h b/src/util/virstats.h deleted file mode 100644 index 5b771976bd3866fba8c30fd2def9c302828e9197..0000000000000000000000000000000000000000 --- a/src/util/virstats.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * virstats.h: Block and network stats. - * - * Copyright (C) 2007 Red Hat, Inc. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library. If not, see - * . - * - * Richard W.M. Jones - */ - -#ifndef __STATS_LINUX_H__ -# define __STATS_LINUX_H__ - -# include "internal.h" - -int virNetDevTapInterfaceStats(const char *ifname, - virDomainInterfaceStatsPtr stats) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; - -#endif /* __STATS_LINUX_H__ */ diff --git a/src/xen/xen_hypervisor.c b/src/xen/xen_hypervisor.c index efe5a8f977a38de145e40e8e3da39952e40a963d..bce7b5612397145e931337409d6140d60cec40fe 100644 --- a/src/xen/xen_hypervisor.c +++ b/src/xen/xen_hypervisor.c @@ -68,7 +68,7 @@ #include "xen_driver.h" #include "xen_hypervisor.h" #include "xs_internal.h" -#include "virstats.h" +#include "virnetdevtap.h" #include "block_stats.h" #include "xend_internal.h" #include "virbuffer.h"