]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: virstatslinux: make more generic
authorRoman Bogorodskiy <bogorodskiy@gmail.com>
Sat, 5 Jul 2014 15:34:39 +0000 (19:34 +0400)
committerRoman Bogorodskiy <bogorodskiy@gmail.com>
Tue, 15 Jul 2014 18:00:59 +0000 (22:00 +0400)
Rename linuxDomainInterfaceStats to virNetInterfaceStats in order
to allow adding platform specific implementations without
making consumer worrying about specific implementation to be used.

Also, rename util/virstatslinux.c to util/virstats.c so placing
other platform specific implementations into this file don't
look unexpected from the file name.

14 files changed:
po/POTFILES.in
src/Makefile.am
src/libvirt_linux.syms
src/libvirt_private.syms
src/lxc/lxc_driver.c
src/openvz/openvz_driver.c
src/qemu/qemu_driver.c
src/uml/uml_driver.c
src/util/virstats.c [new file with mode: 0644]
src/util/virstats.h [new file with mode: 0644]
src/util/virstatslinux.c [deleted file]
src/util/virstatslinux.h [deleted file]
src/xen/xen_hypervisor.c
tests/statstest.c

index 64a987ecabe3ab719b0d74d6c590299ae206d48f..fd4b56ec08c4c2d875c21ff314114783561e2e14 100644 (file)
@@ -198,7 +198,7 @@ src/util/virrandom.c
 src/util/virsexpr.c
 src/util/virscsi.c
 src/util/virsocketaddr.c
-src/util/virstatslinux.c
+src/util/virstats.c
 src/util/virstorageencryption.c
 src/util/virstoragefile.c
 src/util/virstring.c
index e2f76a7496d71eaa5e5dd39970a2c0ffa5a4f80a..a40e63fe65132700f952acf0961988e5d8b0d767 100644 (file)
@@ -149,7 +149,7 @@ UTIL_SOURCES =                                                      \
                util/virseclabel.c util/virseclabel.h           \
                util/virsexpr.c util/virsexpr.h                 \
                util/virsocketaddr.h util/virsocketaddr.c       \
-               util/virstatslinux.c util/virstatslinux.h       \
+               util/virstats.c util/virstats.h \
                util/virstorageencryption.c util/virstorageencryption.h \
                util/virstoragefile.c util/virstoragefile.h     \
                util/virstring.h util/virstring.c               \
index b3b23841fe62d93e595c0ede155145e44f880afe..1a7f263fba731d86d684100b07d21b28750024b4 100644 (file)
@@ -6,9 +6,6 @@
 linuxNodeGetCPUStats;
 linuxNodeInfoCPUPopulate;
 
-# util/virstatslinux.h
-linuxDomainInterfaceStats;
-
 # Let emacs know we want case-insensitive sorting
 # Local Variables:
 # sort-fold-case: t
index e59ea4c02c2da2e3b278272dff20e3c1ff912472..a184268bc6c694267a6b1600b5bbe94079419778 100644 (file)
@@ -1886,6 +1886,8 @@ virSocketAddrPrefixToNetmask;
 virSocketAddrSetIPv4Addr;
 virSocketAddrSetPort;
 
+# util/virstats.h
+virNetInterfaceStats;
 
 # util/virstorageencryption.h
 virStorageEncryptionFormat;
index f04b543c7acc08396505196b4679b9de64747012..b7b4b02dd3c02ca90e5c0686dca99dd4139af123 100644 (file)
@@ -51,7 +51,7 @@
 #include "virnetdevveth.h"
 #include "nodeinfo.h"
 #include "viruuid.h"
-#include "virstatslinux.h"
+#include "virstats.h"
 #include "virhook.h"
 #include "virfile.h"
 #include "virpidfile.h"
@@ -3132,7 +3132,7 @@ lxcDomainInterfaceStats(virDomainPtr dom,
     }
 
     if (ret == 0)
-        ret = linuxDomainInterfaceStats(path, stats);
+        ret = virNetInterfaceStats(path, stats);
     else
         virReportError(VIR_ERR_INVALID_ARG,
                        _("Invalid path, '%s' is not a known interface"), path);
index baa725629f04fbcf6fcadcc0ca4bfccdee1cf799..851ed303712972807184d611a7f9d4f091c1a962 100644 (file)
@@ -56,7 +56,7 @@
 #include "virlog.h"
 #include "vircommand.h"
 #include "viruri.h"
-#include "virstatslinux.h"
+#include "virstats.h"
 #include "virstring.h"
 
 #define VIR_FROM_THIS VIR_FROM_OPENVZ
@@ -2010,7 +2010,7 @@ openvzDomainInterfaceStats(virDomainPtr dom,
     }
 
     if (ret == 0)
-        ret = linuxDomainInterfaceStats(path, stats);
+        ret = virNetInterfaceStats(path, stats);
     else
         virReportError(VIR_ERR_INVALID_ARG,
                        _("invalid path, '%s' is not a known interface"), path);
index c0ad446aadab794efb392027ef581b32513454c6..62b7f079f485cdf702a9dbdb279ecef01d70abbf 100644 (file)
@@ -61,7 +61,7 @@
 #include "datatypes.h"
 #include "virbuffer.h"
 #include "nodeinfo.h"
-#include "virstatslinux.h"
+#include "virstats.h"
 #include "capabilities.h"
 #include "viralloc.h"
 #include "viruuid.h"
@@ -9765,7 +9765,6 @@ qemuDomainBlockStatsFlags(virDomainPtr dom,
     return ret;
 }
 
-#ifdef __linux__
 static int
 qemuDomainInterfaceStats(virDomainPtr dom,
                          const char *path,
@@ -9797,7 +9796,7 @@ qemuDomainInterfaceStats(virDomainPtr dom,
     }
 
     if (ret == 0)
-        ret = linuxDomainInterfaceStats(path, stats);
+        ret = virNetInterfaceStats(path, stats);
     else
         virReportError(VIR_ERR_INVALID_ARG,
                        _("invalid path, '%s' is not a known interface"), path);
@@ -9807,17 +9806,6 @@ qemuDomainInterfaceStats(virDomainPtr dom,
         virObjectUnlock(vm);
     return ret;
 }
-#else
-static int
-qemuDomainInterfaceStats(virDomainPtr dom ATTRIBUTE_UNUSED,
-                         const char *path ATTRIBUTE_UNUSED,
-                         struct _virDomainInterfaceStats *stats ATTRIBUTE_UNUSED)
-{
-    virReportError(VIR_ERR_OPERATION_INVALID, "%s",
-                   _("interface stats not implemented on this platform"));
-    return -1;
-}
-#endif
 
 static int
 qemuDomainSetInterfaceParameters(virDomainPtr dom,
index 62d1afe2e27ff16fcddeccc94689b4dc25862ed5..7039afc8526c683e09e9dabfd84c6eead5db1f5b 100644 (file)
@@ -47,7 +47,7 @@
 #include "uml_conf.h"
 #include "virbuffer.h"
 #include "nodeinfo.h"
-#include "virstatslinux.h"
+#include "virstats.h"
 #include "capabilities.h"
 #include "viralloc.h"
 #include "viruuid.h"
diff --git a/src/util/virstats.c b/src/util/virstats.c
new file mode 100644 (file)
index 0000000..17ef5b6
--- /dev/null
@@ -0,0 +1,127 @@
+/*
+ * virstats.c: Block and network stats.
+ *
+ * Copyright (C) 2007-2010 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
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Richard W.M. Jones <rjones@redhat.com>
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <regex.h>
+
+#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
+virNetInterfaceStats(const char *path,
+                     struct _virDomainInterfaceStats *stats)
+{
+    int path_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;
+    }
+
+    path_len = strlen(path);
+
+    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-path_len >= line &&
+            STREQ(colon-path_len, path)) {
+            /* IMPORTANT NOTE!
+             * /proc/net/dev vif<domid>.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;
+}
+#else
+int
+virNetInterfaceStats(const char *path ATTRIBUTE_UNUSED,
+                     struct _virDomainInterfaceStats *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
new file mode 100644 (file)
index 0000000..9724d8e
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Richard W.M. Jones <rjones@redhat.com>
+ */
+
+#ifndef __STATS_LINUX_H__
+# define __STATS_LINUX_H__
+
+# include "internal.h"
+
+extern int virNetInterfaceStats(const char *path,
+                                struct _virDomainInterfaceStats *stats);
+
+#endif /* __STATS_LINUX_H__ */
diff --git a/src/util/virstatslinux.c b/src/util/virstatslinux.c
deleted file mode 100644 (file)
index 60b72dc..0000000
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
- * virstatslinux.c: Linux block and network stats.
- *
- * Copyright (C) 2007-2010 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
- * <http://www.gnu.org/licenses/>.
- *
- * Richard W.M. Jones <rjones@redhat.com>
- */
-
-#include <config.h>
-
-/* This file only applies on Linux. */
-#ifdef __linux__
-
-# include <stdio.h>
-# include <stdlib.h>
-# include <fcntl.h>
-# include <string.h>
-# include <unistd.h>
-# include <regex.h>
-
-# include "virerror.h"
-# include "datatypes.h"
-# include "virstatslinux.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.
- */
-
-int
-linuxDomainInterfaceStats(const char *path,
-                          struct _virDomainInterfaceStats *stats)
-{
-    int path_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;
-    }
-
-    path_len = strlen(path);
-
-    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-path_len >= line &&
-            STREQ(colon-path_len, path)) {
-            /* IMPORTANT NOTE!
-             * /proc/net/dev vif<domid>.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;
-}
-
-#endif /* __linux__ */
diff --git a/src/util/virstatslinux.h b/src/util/virstatslinux.h
deleted file mode 100644 (file)
index d5900ed..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * virstatslinux.h: Linux 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
- * <http://www.gnu.org/licenses/>.
- *
- * Richard W.M. Jones <rjones@redhat.com>
- */
-
-#ifndef __STATS_LINUX_H__
-# define __STATS_LINUX_H__
-
-# ifdef __linux__
-
-#  include "internal.h"
-
-extern int linuxDomainInterfaceStats(const char *path,
-                                     struct _virDomainInterfaceStats *stats);
-
-# endif /* __linux__ */
-
-#endif /* __STATS_LINUX_H__ */
index b52c9375594edf299088a2585f3138d65931084c..09aa1aac141ea89fad2a071ebaf0f1f44c81cca6 100644 (file)
@@ -68,7 +68,7 @@
 #include "xen_driver.h"
 #include "xen_hypervisor.h"
 #include "xs_internal.h"
-#include "virstatslinux.h"
+#include "virstats.h"
 #include "block_stats.h"
 #include "xend_internal.h"
 #include "virbuffer.h"
@@ -1470,7 +1470,7 @@ xenHypervisorDomainInterfaceStats(virDomainDefPtr def,
         return -1;
     }
 
-    return linuxDomainInterfaceStats(path, stats);
+    return virNetInterfaceStats(path, stats);
 #else
     virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                    _("/proc/net/dev: Interface not found"));
index ce1567cf1350926b95896eea55cedefd37b28c79..f9b45738dcfb7be31161eeb40b4b003283982e77 100644 (file)
@@ -5,7 +5,7 @@
 #include <string.h>
 #include <sys/utsname.h>
 
-#include "virstatslinux.h"
+#include "virstats.h"
 #include "internal.h"
 #include "xen/block_stats.h"
 #include "testutils.h"