-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjft-shaper
executable file
·59 lines (49 loc) · 1.46 KB
/
jft-shaper
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
#!/bin/sh
### Usage: jft-shaper -h
### Usage: jft-shaper -i INTERVAL # interval in seconds
### Usage: jft-shaper -t <adsl|institution|datacenter> # shapes this machine to traces of the type
### Usage: jft-shaper -b MAXBW # max bandwith of the target machine in kilobits per second
usage()
{
perl -ne '/^### Usage:/ && do { s/^### ?//; print }' < $0
exit 1
}
help()
{
perl -ne '/^###/ && do { s/^### ?//; print }' < $0
exit 0
}
# Parse arguments
eval set -- $(getopt -n $0 -o hs:t:i:b: -- ${1+"$@"})
[ $? = 0 ] || usage
for arg; do
case $arg in
-i) INTVAL="$2"; shift; shift ;;
-b) MAXBW="$2"; shift; shift ;;
-t) TYPE="$2"; shift; shift ;;
-h) help ;;
--) shift; break ;;
-*) usage ;;
esac
done
[ $# = 0 ] || usage
[ X"$TYPE" = X ] && usage
[ -f /data/cfg/traces/$TYPE ] ||
{ echo "Traces for $TYPE not found in /data/state"; exit 1; }
[ X"$INTVAL" = X ] && usage
[ X"$MAXBW" = X ] && usage
# set to default rate upon exit
trap "exit 0" SIGTERM SIGINT
trap "sudo /sbin/tc qdisc del dev seth0 root tbf" EXIT
set -e
while true; do
# loop reads traces and sets the rate at every INTVAL interval seconds
cat /data/cfg/traces/$TYPE | while read p; do
r=$(echo "scale=0; (1.0 - $p) * $MAXBW / 1.0" | bc)
echo rate is $r and perc available is 1.0 - $p and MAXBW is $MAXBW
(sudo /sbin/tc qdisc add dev seth0 root tbf rate ${r}kbit latency 50ms burst 1540)
sleep $INTVAL
(sudo /sbin/tc qdisc del dev seth0 root tbf)
done
done
exit 0