-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstress_net.sh
executable file
·117 lines (102 loc) · 2.78 KB
/
stress_net.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
#!/bin/bash
my_file="$(readlink -e "$0")"
my_dir="$(dirname $my_file)"
function usage(){
echo -e "Shapes egress traffic\n" \
"Options:\n" \
"--nic <nic> # interface name (mandatory)\n" \
"[--rate 1] # rate (default value 1 and unit is mbit, could be specified with unit like 100kbit)\n" \
"[--delay 100] # delay in ms (default 100, could be specified with unit like 1s)\n" \
"[--loss 10] # packet loss in % (default 0)\n" \
"[--corrupt 10] # corrupt level in % (default 0)\n" \
"[--duplicate 10] # duplicate level in % (default 0)\n" \
"[--cleanup] # remove all rules\n"
}
nic=''
rate='1'
delay='100'
loss='0'
corrupt='0'
duplicate='0'
cleanup=''
while [[ -n "$1" ]] ; do
case $1 in
'--nic')
nic="$2"
;;
'--rate')
rate="$2"
;;
'--delay')
delay="$2"
;;
'--loss')
loss="$2"
;;
'--corrupt')
corrupt="$2"
;;
'--duplicate')
duplicate="$2"
;;
'--cleanup')
cleanup="true"
shift 1
continue
;;
'--help')
usage
exit
;;
*)
echo "ERROR: unknown options '$1'"
usage
exit -1
;;
esac
shift 2
done
# add measuruements units if not pointed
[[ "${delay//[^0-9]/}" == "$delay" ]] && delay+='ms'
[[ "${rate//[^0-9]/}" == "$rate" ]] && rate+='mbit'
if [ -z "$nic" ] ; then
echo "ERORO: nic options is reqruied"
usage
exit -1
fi
# remove all rules if any
echo "INFO: remove all rules"
sudo tc qdisc del dev $nic root >/dev/null 2>&1 || true
if [ -n "$cleanup" ] ; then
sudo tc qdisc show dev $nic
exit
fi
function add_opt() {
local dst_name=$1
local opt=$2
local val=$3
[[ -n "$val" && "${val//[^0-9]/}" != 0 ]] && printf -v $dst_name "${!dst_name//\%/%%} %s %s" $opt $val
}
netem_opts=''
add_opt netem_opts delay "${delay}"
add_opt netem_opts loss "${loss}%"
add_opt netem_opts corrupt "${corrupt}%"
add_opt netem_opts duplicate "${duplicate}%"
rate_opts=''
if [ -n "$rate" ] ; then
add_opt rate_opts rate "${rate}"
add_opt rate_opts burst 32kbit
add_opt rate_opts limit 3000
fi
if [[ -z "$netem_opts" && -z "$rate_opts" ]] ; then
echo "ERROR: neither net delay/loss/corrupt/duplicate nor rate provided"
usage
exit -1
fi
# add delay (egress traffic)
sudo tc qdisc add dev $nic root handle 1:0 netem $netem_opts
# add rate filter if eny (egress traffic)
if [ -n "$rate_opts" ] ; then
sudo tc qdisc add dev $nic parent 1:1 handle 10: tbf $rate_opts
fi
sudo tc qdisc show dev $nic