libpod: sum per-interface network stats for FreeBSD

This sums the metric values from all interfaces similar to the Linux
version.

[NO NEW TESTS NEEDED]

Signed-off-by: Doug Rabson <dfr@rabson.org>
This commit is contained in:
Doug Rabson 2023-08-21 15:59:19 +01:00
parent 375eb045ca
commit 3d00744d29
1 changed files with 21 additions and 14 deletions

View File

@ -216,6 +216,8 @@ func (r *Runtime) teardownNetNS(ctr *Container) error {
return nil return nil
} }
// TODO (5.0): return the statistics per network interface
// This would allow better compat with docker.
func getContainerNetIO(ctr *Container) (*LinkStatistics64, error) { func getContainerNetIO(ctr *Container) (*LinkStatistics64, error) {
if ctr.state.NetNS == "" { if ctr.state.NetNS == "" {
// If NetNS is nil, it was set as none, and no netNS // If NetNS is nil, it was set as none, and no netNS
@ -224,8 +226,7 @@ func getContainerNetIO(ctr *Container) (*LinkStatistics64, error) {
return nil, nil return nil, nil
} }
// FIXME get the interface from the container netstatus cmd := exec.Command("jexec", ctr.state.NetNS, "netstat", "-bi", "--libxo", "json")
cmd := exec.Command("jexec", ctr.state.NetNS, "netstat", "-bI", "eth0", "--libxo", "json")
out, err := cmd.Output() out, err := cmd.Output()
if err != nil { if err != nil {
return nil, err return nil, err
@ -235,23 +236,29 @@ func getContainerNetIO(ctr *Container) (*LinkStatistics64, error) {
return nil, err return nil, err
} }
// Find the link stats // Sum all the interface stats - in practice only Tx/TxBytes are needed
res := &LinkStatistics64{}
for _, ifaddr := range stats.Statistics.Interface { for _, ifaddr := range stats.Statistics.Interface {
// Each interface has two records, one for link-layer which has
// an MTU field and one for IP which doesn't. We only want the
// link-layer stats.
//
// It's not clear if we should include loopback stats here but
// if we move to per-interface stats in future, this can be
// reported separately.
if ifaddr.Mtu > 0 { if ifaddr.Mtu > 0 {
return &LinkStatistics64{ res.RxPackets += ifaddr.ReceivedPackets
RxPackets: ifaddr.ReceivedPackets, res.TxPackets += ifaddr.SentPackets
TxPackets: ifaddr.SentPackets, res.RxBytes += ifaddr.ReceivedBytes
RxBytes: ifaddr.ReceivedBytes, res.TxBytes += ifaddr.SentBytes
TxBytes: ifaddr.SentBytes, res.RxErrors += ifaddr.ReceivedErrors
RxErrors: ifaddr.ReceivedErrors, res.TxErrors += ifaddr.SentErrors
TxErrors: ifaddr.SentErrors, res.RxDropped += ifaddr.DroppedPackets
RxDropped: ifaddr.DroppedPackets, res.Collisions += ifaddr.Collisions
Collisions: ifaddr.Collisions,
}, nil
} }
} }
return &LinkStatistics64{}, nil return res, nil
} }
func (c *Container) joinedNetworkNSPath() (string, bool) { func (c *Container) joinedNetworkNSPath() (string, bool) {