~launchpad-pqm/launchpad/devel

13588.1.1 by Aaron Bentley
Add basic local-latency script.
1
#!/usr/bin/env python
13588.1.3 by Aaron Bentley
Allow specifying delay.
2
3
__metaclass__ = type
4
13588.1.1 by Aaron Bentley
Add basic local-latency script.
5
import sys
6
import subprocess
7
13588.1.4 by Aaron Bentley
Extract common functionality to script_commands.
8
from script_commands import (
13588.1.8 by Aaron Bentley
Remove Command.
9
    helps,
10
    run_subcommand,
13588.1.4 by Aaron Bentley
Extract common functionality to script_commands.
11
    UserError,
13588.1.7 by Aaron Bentley
Switch to command-from-function approach.
12
    types,
13588.1.4 by Aaron Bentley
Extract common functionality to script_commands.
13
    )
13588.1.1 by Aaron Bentley
Add basic local-latency script.
14
13588.1.3 by Aaron Bentley
Allow specifying delay.
15
13588.1.2 by Aaron Bentley
Port-specific version.
16
def tc(command):
13588.1.12 by Aaron Bentley
Update docs.
17
    """Run a tc command under sudo.
18
19
    :param tc: The remainder of the command (leaving out tc).
20
    """
13588.1.13 by Aaron Bentley
Update from review.
21
    subprocess.call('sudo tc ' + command, shell=True)
13588.1.2 by Aaron Bentley
Port-specific version.
22
13588.1.3 by Aaron Bentley
Allow specifying delay.
23
13588.1.7 by Aaron Bentley
Switch to command-from-function approach.
24
@helps(delay='Length of delay in miliseconds (each way).',
25
       port='Port to induce delay on.')
26
def start(delay=500, port=443):
13588.1.12 by Aaron Bentley
Update docs.
27
    """Add artificial latency to the lo interface on the specified port."""
13588.1.7 by Aaron Bentley
Switch to command-from-function approach.
28
    tc('qdisc add dev lo root handle 1: prio')
29
    tc('qdisc add dev lo parent 1:3 handle 30: netem delay %dms' % delay)
30
    tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
31
       ' dport %d 0xffff flowid 1:3' % port)
32
    tc('filter add dev lo protocol ip parent 1:0 prio 3 u32 match ip'
33
       ' sport %d 0xffff flowid 1:3' % port)
34
35
36
def stop():
13588.1.16 by Aaron Bentley
Updates from review.
37
    """Remove latency from the lo."""
13588.1.7 by Aaron Bentley
Switch to command-from-function approach.
38
    tc('qdisc del dev lo root')
39
40
13588.1.8 by Aaron Bentley
Remove Command.
41
subcommands = {
42
    'start': start,
43
    'stop': stop,
44
    }
13588.1.4 by Aaron Bentley
Extract common functionality to script_commands.
45
13588.1.1 by Aaron Bentley
Add basic local-latency script.
46
47
48
if __name__ == "__main__":
49
    try:
13588.1.8 by Aaron Bentley
Remove Command.
50
        run_subcommand(subcommands, sys.argv[1:])
13588.1.4 by Aaron Bentley
Extract common functionality to script_commands.
51
    except UserError as e:
13588.1.1 by Aaron Bentley
Add basic local-latency script.
52
        sys.stderr.write(str(e)+'\n')