Skip to content

Commit 4ed5c96

Browse files
authored
Add the possibility to specify traffic control (#6)
Allowed params : -latency in ms -jitter in ms ( if latency param is set ) -loss in percentage
1 parent 34efd52 commit 4ed5c96

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
)
1919

2020
var ip, command, gateway, intf, logLevel, nsPath, mac string
21+
var latency, jitter uint
22+
var loss float64
2123
var log = logrus.New()
2224

2325
func init() {
@@ -27,6 +29,9 @@ func init() {
2729
flag.StringVar(&gateway, "gw", "", "gateway of the request (default will be the default route of the given interface)")
2830
flag.StringVar(&logLevel, "log-level", "info", "min level of logs to print")
2931
flag.StringVar(&mac, "mac", "", "mac address of the interface inside the namespace (default will be a random one)")
32+
flag.UintVar(&latency, "latency", 0, "latency added on the interface in ms")
33+
flag.UintVar(&jitter, "jitter", 0, "jitter added on the interface in ms")
34+
flag.Float64Var(&loss, "loss", 0, "loss added on the interface in percentage")
3035
flag.StringVar(
3136
&nsPath,
3237
"ns-path",
@@ -202,6 +207,28 @@ func main() {
202207
return
203208
}
204209

210+
// Check if we need to add Qdisc attributes
211+
if latency != 0 || jitter != 0 || loss != 0 {
212+
log.Debugf("Add TC : latency %d ms | jitter %d ms | loss %f", latency*1000, jitter*1000, loss)
213+
netem := netlink.NetemQdiscAttrs{
214+
Latency: uint32(latency) * 1000,
215+
Jitter: uint32(jitter) * 1000,
216+
Loss: float32(loss),
217+
}
218+
qdisc := netlink.NewNetem(
219+
netlink.QdiscAttrs{
220+
LinkIndex: link.Attrs().Index,
221+
Parent: netlink.HANDLE_ROOT,
222+
},
223+
netem,
224+
)
225+
err = netlink.QdiscAdd(qdisc)
226+
if err != nil {
227+
log.Warn("Error while setting qdisc on macVlan: ", err)
228+
return
229+
}
230+
}
231+
205232
log.Debugf("Set %s as the route", gwaddr)
206233
// ============================= Set the default route in the namespace
207234
err = netlink.RouteAdd(&netlink.Route{

0 commit comments

Comments
 (0)