forked from data61/MP-SPDZ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency-control.sh
125 lines (101 loc) · 2.12 KB
/
latency-control.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/bash
#
# latency-control.sh
# ---------
# A utility script for traffic shaping using tc
#
# Usage
# -----
# latency-control.sh start - starts the shaper
# latency-control.sh stop - stops the shaper
# latency-control.sh restart - restarts the shaper
# latency-control.sh show - shows the rules currently being shaped
#
# tc uses the following units when passed as a parameter.
# kbps: Kilobytes per second
# mbps: Megabytes per second
# kbit: Kilobits per second
# mbit: Megabits per second
# bps: Bytes per second
# Amounts of data can be specified in:
# kb or k: Kilobytes
# mb or m: Megabytes
# mbit: Megabits
# kbit: Kilobits
set -e
set -x
# Interface to shape
IF=lo
# Average to delay packets by
LATENCY=100ms
JITTER=5ms
players=4
concurrency=1
limit_port() {
echo 'limit_port' $1
# say traffic to $PORT is band 3
tc filter add dev $IF parent 1:0 \
protocol ip \
u32 match ip dport $1 0xffff \
flowid 1:3
tc filter add dev $IF parent 1:0 \
protocol ip \
u32 match ip sport $1 0xffff \
flowid 1:3
}
start() {
# Create a priority-based queue.
tc qdisc add dev $IF root handle 1: prio
# Delay everything in band 3
tc qdisc add dev $IF parent 1:3 handle 30: netem delay $LATENCY $JITTER
# http server ports
for (( port = 4000; port < $(($players + 4000)); port++ )) do
limit_port $port
done
# mpc server ports
for (( i = -1; i < $concurrency; i++ )) do
port_base=$((5000 + $(($i * 100))))
for ((port = $port_base; port < $(($players + $port_base)); port++ )) do
limit_port $port
done
done
}
stop() {
tc qdisc del dev $IF root
}
restart() {
stop
sleep 1
start
}
show() {
tc -s qdisc ls dev $IF
tc -p filter show dev lo
}
case "$1" in
start)
echo -n "Starting bandwidth shaping: "
start
echo "done"
;;
stop)
echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;
restart)
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;
show)
echo "Bandwidth shaping status for $IF:"
show
echo ""
;;
*)
pwd=$(pwd)
echo "Usage: latency-control.sh {start|stop|restart|show}"
;;
esac
exit 0