2015-01-06 14:47:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MpLinkCharacteristics:
|
2015-01-20 11:23:57 +00:00
|
|
|
|
2016-05-08 17:48:39 +00:00
|
|
|
tcNetemParent = "1:1"
|
2016-05-08 17:08:59 +00:00
|
|
|
tcHtbClassid = "10"
|
2016-05-08 17:48:39 +00:00
|
|
|
tcNetemHandle = "1:10"
|
2015-01-20 11:23:57 +00:00
|
|
|
|
2015-12-08 15:50:49 +00:00
|
|
|
def __init__(self, id, delay, queueSize, bandwidth, loss, back_up=False):
|
2015-01-06 14:47:15 +00:00
|
|
|
self.id = id
|
|
|
|
self.delay = delay
|
|
|
|
self.queueSize = queueSize
|
|
|
|
self.bandwidth = bandwidth
|
2015-12-08 15:50:49 +00:00
|
|
|
self.loss = loss
|
2015-01-20 10:04:36 +00:00
|
|
|
self.netemAt = []
|
2015-05-18 14:54:18 +00:00
|
|
|
self.back_up = back_up
|
2015-01-20 10:04:36 +00:00
|
|
|
|
|
|
|
def addNetemAt(self, n):
|
|
|
|
if len(self.netemAt) == 0:
|
2015-01-20 11:23:57 +00:00
|
|
|
n.delta = n.at
|
2015-01-20 10:04:36 +00:00
|
|
|
self.netemAt.append(n)
|
|
|
|
else:
|
|
|
|
if n.at > self.netemAt[-1].at:
|
|
|
|
n.delta = n.at - self.netemAt[-1].at
|
|
|
|
self.netemAt.append(n)
|
|
|
|
else:
|
|
|
|
print("Do not take into account " + n.__str__() + \
|
|
|
|
"because ooo !")
|
|
|
|
pass
|
2015-02-26 16:43:45 +00:00
|
|
|
|
2015-01-20 11:23:57 +00:00
|
|
|
def buildNetemCmd(self, ifname):
|
2015-01-20 10:04:36 +00:00
|
|
|
cmd = ""
|
|
|
|
for n in self.netemAt:
|
|
|
|
cmd = cmd + "sleep " + str(n.delta)
|
2016-05-08 17:08:59 +00:00
|
|
|
cmd = cmd + " && (( tc qdisc del dev " + ifname + " root "
|
2016-05-08 17:48:39 +00:00
|
|
|
cmd = cmd + " && tc qdisc add dev " + ifname + " root handle 1: htb default 10 direct_qlen " + self.queueSize + " && tc class add dev " + ifname + " "
|
|
|
|
cmd = cmd + " parent 1:1 "
|
2016-05-08 17:08:59 +00:00
|
|
|
cmd = cmd + " classid " + MpLinkCharacteristics.tcHtbClassid
|
|
|
|
cmd = cmd + " htb rate " + self.bandwidth + "mbit"
|
|
|
|
cmd = cmd + " burst " + str(int(self.queueSize) * 1500) + ") || "
|
|
|
|
cmd = cmd + " tc class change dev " + ifname + " "
|
2015-01-20 11:23:57 +00:00
|
|
|
cmd = cmd + " parent " + MpLinkCharacteristics.tcNetemParent
|
2016-05-08 17:08:59 +00:00
|
|
|
cmd = cmd + " classid " + MpLinkCharacteristics.tcHtbClassid
|
2016-05-08 16:48:56 +00:00
|
|
|
cmd = cmd + " htb rate " + self.bandwidth + "mbit"
|
2016-05-08 17:08:59 +00:00
|
|
|
cmd = cmd + " burst " + str(int(self.queueSize) * 1500) + ") && "
|
2016-04-21 13:55:40 +00:00
|
|
|
cmd = cmd + " tc qdisc add dev " + ifname + " "
|
|
|
|
cmd = cmd + " parent " + MpLinkCharacteristics.tcNetemHandle
|
2016-05-08 17:51:45 +00:00
|
|
|
cmd = cmd + " netem " + n.cmd + " delay " + self.delay + "ms limit " + self.queueSize + " && "
|
2015-01-20 10:04:36 +00:00
|
|
|
cmd = cmd + " true &"
|
2015-01-16 02:59:52 +00:00
|
|
|
return cmd
|
2015-01-20 10:04:36 +00:00
|
|
|
|
2015-01-07 13:44:44 +00:00
|
|
|
def asDict(self):
|
|
|
|
d = {}
|
2015-01-16 02:59:52 +00:00
|
|
|
d['bw'] = float(self.bandwidth)
|
2015-01-07 13:52:43 +00:00
|
|
|
d['delay'] = self.delay + "ms"
|
2015-12-08 15:50:49 +00:00
|
|
|
d['loss'] = float(self.loss)
|
2015-01-07 13:52:43 +00:00
|
|
|
d['max_queue_size'] = int(self.queueSize)
|
2015-01-07 13:44:44 +00:00
|
|
|
return d
|
2015-01-06 14:47:15 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
s = "Link id : " + str(self.id) + "\n"
|
|
|
|
s = s + "\tDelay : " + str(self.delay) + "\n"
|
|
|
|
s = s + "\tQueue Size : " + str(self.queueSize) + "\n"
|
2015-01-20 10:04:36 +00:00
|
|
|
s = s + "\tBandwidth : " + str(self.bandwidth) + "\n"
|
2015-12-08 15:50:49 +00:00
|
|
|
s = s + "\tLoss : " + str(self.loss) + "\n"
|
2015-05-18 14:54:18 +00:00
|
|
|
s = s + "\tBack up : " + str(self.back_up) + "\n"
|
2015-01-20 10:04:36 +00:00
|
|
|
for l in self.netemAt:
|
|
|
|
s = s + "\t" + l.__str__() + "\n"
|
2015-01-06 14:47:15 +00:00
|
|
|
return s
|