The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxd/pull/4853

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
The new logic introduced in 3.3 was both slower due to having to do
multiple file operations and also wrong as forknet doesn't actually have
the container's /sys view.

The result was the expected list of interfaces and addresses but
statistics coming from the host's device, if a matching one existed.

Closes #4851

Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
From 48b8b97c4a330b2de7ad2434f2dd8ddf998de042 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@ubuntu.com>
Date: Mon, 30 Jul 2018 14:49:47 -0400
Subject: [PATCH] networks: Fix packet stats logic for containers
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The new logic introduced in 3.3 was both slower due to having to do
multiple file operations and also wrong as forknet doesn't actually have
the container's /sys view.

The result was the expected list of interfaces and addresses but
statistics coming from the host's device, if a matching one existed.

Closes #4851

Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
---
 lxd/networks_utils.go | 48 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 8 deletions(-)

diff --git a/lxd/networks_utils.go b/lxd/networks_utils.go
index 4169ff74e..7ee6cfd83 100644
--- a/lxd/networks_utils.go
+++ b/lxd/networks_utils.go
@@ -1031,6 +1031,7 @@ func networkGetState(netIf net.Interface) 
api.NetworkState {
                Type:      netType,
        }
 
+       // Get address information
        addrs, err := netIf.Addrs()
        if err == nil {
                for _, addr := range addrs {
@@ -1071,17 +1072,48 @@ func networkGetState(netIf net.Interface) 
api.NetworkState {
                }
        }
 
-       network.Counters.BytesSent, _ = shared.ParseNumberFromFile(
-               fmt.Sprintf("/sys/class/net/%s/statistics/tx_bytes", 
netIf.Name))
+       // Get counters
+       content, err := ioutil.ReadFile("/proc/net/dev")
+       if err == nil {
+               for _, line := range strings.Split(string(content), "\n") {
+                       fields := strings.Fields(line)
+
+                       if len(fields) != 17 {
+                               continue
+                       }
+
+                       intName := strings.TrimSuffix(fields[0], ":")
+                       if intName != netIf.Name {
+                               continue
+                       }
+
+                       rxBytes, err := strconv.ParseInt(fields[1], 10, 64)
+                       if err != nil {
+                               continue
+                       }
+
+                       rxPackets, err := strconv.ParseInt(fields[2], 10, 64)
+                       if err != nil {
+                               continue
+                       }
 
-       network.Counters.BytesReceived, _ = shared.ParseNumberFromFile(
-               fmt.Sprintf("/sys/class/net/%s/statistics/rx_bytes", 
netIf.Name))
+                       txBytes, err := strconv.ParseInt(fields[9], 10, 64)
+                       if err != nil {
+                               continue
+                       }
 
-       network.Counters.PacketsSent, _ = shared.ParseNumberFromFile(
-               fmt.Sprintf("/sys/class/net/%s/statistics/tx_packets", 
netIf.Name))
+                       txPackets, err := strconv.ParseInt(fields[10], 10, 64)
+                       if err != nil {
+                               continue
+                       }
 
-       network.Counters.PacketsReceived, _ = shared.ParseNumberFromFile(
-               fmt.Sprintf("/sys/class/net/%s/statistics/rx_packets", 
netIf.Name))
+                       network.Counters.BytesSent = txBytes
+                       network.Counters.BytesReceived = rxBytes
+                       network.Counters.PacketsSent = txPackets
+                       network.Counters.PacketsReceived = rxPackets
+                       break
+               }
+       }
 
        return network
 }
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to