plugins/traffic-control: separate function for clear settings

Make easier to combine latency and packet loss settings.
This commit is contained in:
Alessandro Puccetti
2016-09-06 11:53:36 +02:00
parent 176cb5f99f
commit 79453519b2
2 changed files with 47 additions and 1 deletions
+1 -1
View File
@@ -285,7 +285,7 @@ func getControls() []extControl {
Rank: 24,
},
handler: func(pid int) error {
return DoTrafficControl(pid, "", "")
return ClearTrafficControlSettings(pid)
},
},
}
+46
View File
@@ -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