8
8
from script_commands import (
16
subprocess.call('sudo tc ' + command, shell=True)
19
class StartCommand(Command):
23
parser = OptionParser()
25
'-d', '--delay', dest='delay', type='int',
26
help='Length of delay in miliseconds (each way).')
30
def run(delay=500, port=443):
31
tc('qdisc add dev lo root handle 1: prio')
32
tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
33
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
34
' dport %d 0xffff flowid 1:3' % port)
35
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
36
' sport %d 0xffff flowid 1:3' % port)
39
Command.commands['start'] = StartCommand
42
class StopCommand(Command):
46
parser = OptionParser()
51
tc('qdisc del dev lo root')
54
Command.commands['stop'] = StopCommand
16
"""Run a tc command under sudo.
18
:param tc: The remainder of the command (leaving out tc).
20
subprocess.call('sudo tc ' + command, shell=True)
23
@helps(delay='Length of delay in miliseconds (each way).',
24
port='Port to induce delay on.')
25
def start(delay=500, port=443):
26
"""Add artificial latency to the lo interface on the specified port."""
27
tc('qdisc add dev lo root handle 1: prio')
28
tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
29
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
30
' dport %d 0xffff flowid 1:3' % port)
31
tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
32
' sport %d 0xffff flowid 1:3' % port)
36
"""Remove latency from the lo."""
37
tc('qdisc del dev lo root')
58
47
if __name__ == "__main__":
60
Command.run_subcommand(sys.argv[1:])
49
run_subcommand(subcommands, sys.argv[1:])
61
50
except UserError as e:
62
51
sys.stderr.write(str(e)+'\n')