Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libpod: report slirp4netns network stats #13101

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-stats.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ ID NAME MEM USAGE / LIMIT
6eae9e25a564 clever_bassi 3.031MB / 16.7GB
```

Note: When using a slirp4netns network, the traffic send via the port forwarding will be accounted
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should mention the rootlesskit port handler, with --network slirp4netns:port_handler=slirp4netns it should be counted correctly on tap0 AFAIK

to the `lo` device. Traffic accounted to `lo` is not accounted in the stats output.


## SEE ALSO
**[podman(1)](podman.1.md)**

Expand Down
37 changes: 23 additions & 14 deletions libpod/networking_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,21 +834,25 @@ func (r *Runtime) teardownNetNS(ctr *Container) error {
return nil
}

func getContainerNetNS(ctr *Container) (string, error) {
func getContainerNetNS(ctr *Container) (string, *Container, error) {
if ctr.state.NetNS != nil {
return ctr.state.NetNS.Path(), nil
return ctr.state.NetNS.Path(), nil, nil
}
if ctr.config.NetNsCtr != "" {
c, err := ctr.runtime.GetContainer(ctr.config.NetNsCtr)
if err != nil {
return "", err
return "", nil, err
}
if err = c.syncContainer(); err != nil {
return "", err
return "", c, err
}
return getContainerNetNS(c)
netNs, c2, err := getContainerNetNS(c)
if c2 != nil {
c = c2
}
return netNs, c, err
}
return "", nil
return "", nil, nil
}

// isBridgeNetMode checks if the given network mode is bridge.
Expand Down Expand Up @@ -919,12 +923,8 @@ func (r *Runtime) reloadContainerNetwork(ctr *Container) (map[string]types.Statu

func getContainerNetIO(ctr *Container) (*netlink.LinkStatistics, error) {
var netStats *netlink.LinkStatistics
// With slirp4netns, we can't collect statistics at present.
// For now, we allow stats to at least run by returning nil
if rootless.IsRootless() || ctr.config.NetMode.IsSlirp4netns() {
return netStats, nil
}
netNSPath, netPathErr := getContainerNetNS(ctr)

netNSPath, otherCtr, netPathErr := getContainerNetNS(ctr)
if netPathErr != nil {
return nil, netPathErr
}
Expand All @@ -933,9 +933,18 @@ func getContainerNetIO(ctr *Container) (*netlink.LinkStatistics, error) {
// this is a valid state and thus return no error, nor any statistics
return nil, nil
}

// FIXME get the interface from the container netstatus
dev := "eth0"
netMode := ctr.config.NetMode
if otherCtr != nil {
netMode = otherCtr.config.NetMode
}
if netMode.IsSlirp4netns() {
dev = "tap0"
}
err := ns.WithNetNSPath(netNSPath, func(_ ns.NetNS) error {
// FIXME get the interface from the container netstatus
link, err := netlink.LinkByName("eth0")
link, err := netlink.LinkByName(dev)
if err != nil {
return err
}
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,19 @@ var _ = Describe("Podman stats", func() {
Expect(session).Should(Exit(0))
})

It("podman reads slirp4netns network stats", func() {
session := podmanTest.Podman([]string{"run", "-d", "--network", "slirp4netns", ALPINE, "top"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

cid := session.OutputToString()

stats := podmanTest.Podman([]string{"stats", "--format", "'{{.NetIO}}'", "--no-stream", cid})
stats.WaitWithDefaultTimeout()
Expect(stats).Should(Exit(0))
Expect(stats.OutputToString()).To(Not(ContainSubstring("-- / --")))
})

// Regression test for #8265
It("podman stats with custom memory limits", func() {
// Run three containers. One with a memory limit. Make sure
Expand Down