From 79453519b2a1e9f19addb2dc419469d951209f39 Mon Sep 17 00:00:00 2001 From: Alessandro Puccetti Date: Mon, 15 Aug 2016 09:29:42 +0200 Subject: [PATCH] plugins/traffic-control: separate function for clear settings Make easier to combine latency and packet loss settings. --- examples/plugins/traffic-control/report.go | 2 +- examples/plugins/traffic-control/tc.go | 46 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/examples/plugins/traffic-control/report.go b/examples/plugins/traffic-control/report.go index 247292f4a..91b261c37 100644 --- a/examples/plugins/traffic-control/report.go +++ b/examples/plugins/traffic-control/report.go @@ -285,7 +285,7 @@ func getControls() []extControl { Rank: 24, }, handler: func(pid int) error { - return DoTrafficControl(pid, "", "") + return ClearTrafficControlSettings(pid) }, }, } diff --git a/examples/plugins/traffic-control/tc.go b/examples/plugins/traffic-control/tc.go index 3127909e8..9b3f0f726 100644 --- a/examples/plugins/traffic-control/tc.go +++ b/examples/plugins/traffic-control/tc.go @@ -77,6 +77,52 @@ func DoTrafficControl(pid int, latency string, pktLoss string) error { return nil } +func ClearTrafficControlSettings(pid int) error { + cmds := [][]string{ + split("tc qdisc replace dev eth0 root handle 1: netem"), + + // These steps are not required, since we don't do + // ingress traffic control, only egress, see the TODO + // at the beginning of the file. + + //split("ip link add ifb0 type ifb"), + //split("ip link set ifb0 up"), + //split("tc qdisc add dev eth0 handle ffff: ingress"), + //split("tc filter add dev eth0 parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0"), + //split("tc qdisc replace dev ifb0 handle 1:0 root netem"), + + // Add "loss %d%% rate %dkbit" when we add the + // possibility to control the packet loss and + // bandwidth. See the TODO at the beginning of the + // file. + + } + 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 { + log.Error(string(output)) + return fmt.Errorf("failed to execute command: %v", err) + } + } + return nil + }) + if err != nil { + return fmt.Errorf("failed to perform traffic control: %v", err) + } + // clear cached parameters + if netNSID, err := getNSID(netNS); err != nil { + log.Error(netNSID) + return fmt.Errorf("failed to get network namespace ID: %v", err) + } else { + //trafficControlStatusCache[netNSID] = trafficControlStatus{ + // latency: "-", + // pktLoss: "-", + delete(trafficControlStatusCache, netNSID) + } + return nil +} + func getLatency(pid int) (string, error) { var status *trafficControlStatus var err error