From 5d3e5b23ec3cac9df2af3752a08d6f172ff7d9a2 Mon Sep 17 00:00:00 2001 From: Alessandro Puccetti Date: Fri, 12 Aug 2016 18:19:03 +0200 Subject: [PATCH] plugins/traffic-control: added clear setting control This control remove any previous setting --- examples/plugins/traffic-control/report.go | 11 +++++++++++ examples/plugins/traffic-control/tc.go | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/examples/plugins/traffic-control/report.go b/examples/plugins/traffic-control/report.go index e1e0d3023..2b56b0de1 100644 --- a/examples/plugins/traffic-control/report.go +++ b/examples/plugins/traffic-control/report.go @@ -266,6 +266,17 @@ func getControls() []extControl { return DoTrafficControl(pid, "1ms") }, }, + { + control: control{ + ID: "clear", + Human: "Clear traffic control settings", + Icon: "fa-times-circle", + Rank: 23, + }, + handler: func(pid int) error { + return DoTrafficControl(pid, "") + }, + }, } } diff --git a/examples/plugins/traffic-control/tc.go b/examples/plugins/traffic-control/tc.go index b66d2ec69..d0e2cba94 100644 --- a/examples/plugins/traffic-control/tc.go +++ b/examples/plugins/traffic-control/tc.go @@ -29,9 +29,14 @@ func DoTrafficControl(pid int, latency string) error { // bandwidth. See the TODO at the beginning of the // file. - split(fmt.Sprintf("tc qdisc change dev eth0 root handle 1: netem delay %s", latency)), } - netNS := fmt.Sprintf("/proc/%d/ns/net", pid) + if latency != "" { + cmds = append(cmds, split(fmt.Sprintf("tc qdisc change dev eth0 root handle 1: netem delay %s", latency))) + } else { + cmds = append(cmds, split("tc qdisc change dev eth0 root handle 1: netem")) + } + + netNS := fmt.Sprintf("/proc/%d/ns/net", pid) err := ns.WithNetNSPath(netNS, func(hostNS ns.NetNS) error { for _, cmd := range cmds { if output, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput(); err != nil { @@ -50,7 +55,12 @@ func DoTrafficControl(pid int, latency string) error { return fmt.Errorf("failed to get network namespace ID: %v", err) } else { trafficControlStatusCache[netNSID] = trafficControlStatus{ - latency: latency, + latency: func(latency string) string { + if latency == "" { + return "-" + } + return latency + }(latency), pktLoss: "-", } }