-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlagfactory.sh
executable file
·144 lines (118 loc) · 3.67 KB
/
lagfactory.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#! /bin/bash
#
# This script uses Netem (http://www.linux-foundation.org/en/Net:Netem) to simulate
# lag and packet loss on traffic going and coming from a selected network.
#
# Customize value at start of script.
#
# Copyright (C) 2008 INL
# Written by Éric Leblond <[email protected]>
# Vincent Deffontaines <[email protected]>
# INL http://www.inl.fr/
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
IFACE="eth0 eth2" # Input and output interface to simulate lag in INPUT and OUTPUT
TARGET="0.0.0.0/0 ::/0" # Hosts or networks to apply lag on
NO_SSH_DELAY="yes" # if set to "yes" ssh will not be impacted by delay
# Default value
DELAY="3000" # delay in ms
VAR="2000" # delay in ms (packets will be delayed from DELAY +/- VAR)
BANDWITH="200kbit" # bp of simulated link
PERCENTLOSS="2%" # Percent of packet loss
# Path to commands
IPTABLES=/sbin/iptables
IP6TABLES=/sbin/ip6tables
TC=/sbin/tc
########################################
# No need to modify under this line.
########################################
if [ -n "$2" ]; then
DELAY=$2
echo "DELAY : $DELAY ms"
fi
if [ -n "$3" ]; then
PERCENTLOSS="$3%"
echo "LOSS : $PERCENTLOSS"
fi
do_start() {
modprobe sch_prio
modprobe sch_netem
for IIF in ${IFACE}; do
${TC} qdisc add dev ${IIF} root handle 1: prio bands 3
${TC} qdisc add dev ${IIF} parent 1:3 handle 30: netem \
delay ${DELAY}ms ${VAR}ms loss ${PERCENTLOSS} 33.33%
${TC} qdisc add dev ${IIF} parent 30:1 tbf rate ${BANDWITH} buffer 1600 limit 3000
${TC} filter add dev ${IIF} protocol ip parent 1:0 prio 3 handle 5000 fw flowid 1:3
${TC} filter add dev ${IIF} protocol ipv6 parent 1:0 prio 4 handle 5000 fw flowid 1:3
done;
for NET in ${TARGET}; do
if [[ ${NET} == *:* ]]; then
IPT=${IP6TABLES}
else
IPT=${IPTABLES}
fi
${IPT} -A POSTROUTING -t mangle -d ${NET} -j MARK --set-mark 5000
${IPT} -A POSTROUTING -t mangle -s ${NET} -j MARK --set-mark 5000
if [ ${NO_SSH_DELAY} = "yes" ]; then
${IPT} -A POSTROUTING -t mangle -d ${NET} -p tcp --dport 22 -j MARK --set-mark 0
${IPT} -A POSTROUTING -t mangle -s ${NET} -p tcp --sport 22 -j MARK --set-mark 0
fi
done;
}
do_stop() {
for IIF in ${IFACE}; do
${TC} qdisc del dev ${IIF} root
done;
for NET in ${TARGET}; do
if [[ ${NET} == *:* ]]; then
IPT=${IP6TABLES}
else
IPT=${IPTABLES}
fi
${IPT} -D POSTROUTING -t mangle -d ${NET} -j MARK --set-mark 5000
${IPT} -D POSTROUTING -t mangle -s ${NET} -j MARK --set-mark 5000
if [ ${NO_SSH_DELAY} = "yes" ]; then
${IPT} -D POSTROUTING -t mangle -d ${NET} -p tcp --dport 22 -j MARK --set-mark 0
${IPT} -D POSTROUTING -t mangle -s ${NET} -p tcp --sport 22 -j MARK --set-mark 0
fi
done;
rmmod sch_prio
rmmod sch_netem
}
do_status() {
for IIF in ${IFACE}; do
${TC} qdisc show dev ${IIF}
${TC} filter show dev ${IIF}
done;
${IPTABLES} -L POSTROUTING -nv -t mangle
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
status)
do_status
;;
*)
echo "Usage : lagfactory.sh start|stop [ \$DELAY [ \$LOSS ] ]"
echo " DELAY in ms, LOSS in %"
;;
esac