diff --git a/examples/plugins/traffic-control/report.go b/examples/plugins/traffic-control/report.go index 2b56b0de1..247292f4a 100644 --- a/examples/plugins/traffic-control/report.go +++ b/examples/plugins/traffic-control/report.go @@ -241,7 +241,7 @@ func getControls() []extControl { Rank: 20, }, handler: func(pid int) error { - return DoTrafficControl(pid, "2000ms") + return DoTrafficControl(pid, "2000ms", "") }, }, { @@ -252,7 +252,7 @@ func getControls() []extControl { Rank: 21, }, handler: func(pid int) error { - return DoTrafficControl(pid, "300ms") + return DoTrafficControl(pid, "300ms", "") }, }, { @@ -263,7 +263,18 @@ func getControls() []extControl { Rank: 22, }, handler: func(pid int) error { - return DoTrafficControl(pid, "1ms") + return DoTrafficControl(pid, "1ms", "") + }, + }, + { + control: control{ + ID: "pkt-drop-low", + Human: "Packet drop: low", + Icon: "fa-cut", + Rank: 23, + }, + handler: func(pid int) error { + return DoTrafficControl(pid, "", "10%") }, }, { @@ -271,10 +282,10 @@ func getControls() []extControl { ID: "clear", Human: "Clear traffic control settings", Icon: "fa-times-circle", - Rank: 23, + Rank: 24, }, handler: func(pid int) error { - return DoTrafficControl(pid, "") + return DoTrafficControl(pid, "", "") }, }, } diff --git a/examples/plugins/traffic-control/tc.go b/examples/plugins/traffic-control/tc.go index d0e2cba94..3127909e8 100644 --- a/examples/plugins/traffic-control/tc.go +++ b/examples/plugins/traffic-control/tc.go @@ -10,7 +10,7 @@ import ( "github.com/containernetworking/cni/pkg/ns" ) -func DoTrafficControl(pid int, latency string) error { +func DoTrafficControl(pid int, latency string, pktLoss string) error { cmds := [][]string{ split("tc qdisc replace dev eth0 root handle 1: netem"), @@ -30,13 +30,18 @@ func DoTrafficControl(pid int, latency string) error { // file. } + cmd := split("tc qdisc change dev eth0 root handle 1: netem") 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")) + cmd = append(cmd, "delay") + cmd = append(cmd, latency) } + if pktLoss != "" { + cmd = append(cmd, "loss") + cmd = append(cmd, pktLoss) + } + cmds = append(cmds, cmd) - netNS := fmt.Sprintf("/proc/%d/ns/net", pid) + 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 { @@ -61,7 +66,12 @@ func DoTrafficControl(pid int, latency string) error { } return latency }(latency), - pktLoss: "-", + pktLoss: func(pktLoss string) string { + if pktLoss == "" { + return "-" + } + return pktLoss + }(pktLoss), } } return nil