-
Notifications
You must be signed in to change notification settings - Fork 1
/
disruption.py
241 lines (219 loc) · 11 KB
/
disruption.py
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# @Time : 2/19/22 4:49 PM
# @Author : Ruizhi Cheng
# @FileName: disruption.py
# @Email : [email protected]
import os
import time
import sys
import argparse
import subprocess
#bandwidth unit: kbps, which means we constraint the bandwidth from 3Mbps to 0.1 Mbps
uplink_bandwidth = [1500,1200,1000,700,500,300]
#downlink_bandwidth = [2000,1000,700,500,300,200,100,50]
downlink_bandwidth = [1000,700,500,300,200,100]
uplink_latency = [100,200,300,350,400,500]
downlink_latency=[100,200,300,350,400,500]
uplink_packet_loss = [1,3,5,7,10,20]
downlink_packet_loss = [1,3,5,7,10,20]
last_time = 40
#drop
#flag = u uplink
#flag = d downlink
def bandwidth_wondershaper(args):
if not (args.u or args.d):
print("Please indicate uplink (u) or downlink (d)")
return 0
adapter = args.a
flag = 'u' if args.u else 'd'
bandwidth=uplink_bandwidth if flag=='u' else downlink_bandwidth
subprocess.run("sudo wondershaper -c -a {0}".format(adapter),shell=True)
for step in bandwidth:
cur_step = step
subprocess.run("sudo wondershaper -a {0} -{1} {2}".format(adapter,flag,cur_step),shell=True)
print("sudo wondershaper -a {0} -{1} {2}".format(adapter,flag,cur_step))
time.sleep(last_time)
subprocess.run("sudo wondershaper -c -a {0}".format(adapter),shell=True)
#subprocess.run("sudo wondershaper -s {0}".format(adapter))
subprocess.run("sudo wondershaper -c -a {0}".format(adapter),shell=True)
time.sleep(last_time)
print('done')
def latency(args):
adapter = args.a
if args.d:
subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter),shell=True)
subprocess.run('modprobe ifb',shell=True)
subprocess.run('ip link set dev ifb0 up',shell=True)
subprocess.run('tc qdisc add dev {0} ingress'.format(adapter),shell=True)
subprocess.run('tc filter add dev {0} parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0'.format(adapter),shell=True)
count = 0
for step in downlink_latency:
if count==0:
subprocess.run('tc qdisc add dev ifb0 root netem delay {0}ms'.format(step),shell=True)
print('tc qdisc add dev ifb0 root netem delay {0}ms'.format(step))
time.sleep(last_time)
count+=1
else:
subprocess.run('tc qdisc change dev ifb0 root netem delay {0}ms'.format(step),shell=True)
print(('tc qdisc change dev ifb0 root netem delay {0}ms'.format(step)))
time.sleep(last_time)
# subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter))
# subprocess.run('modprobe -r ifb')
subprocess.run('tc qdisc del dev {0} root 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev {0} ingress 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev ifb root 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev ifb ingress 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter),shell=True)
subprocess.run('modprobe -r ifb',shell=True)
time.sleep(last_time)
print('done')
elif args.u:
count = 0
for step in uplink_latency:
if count == 0:
subprocess.run("tcset {0} --delay {1}ms".format(adapter,step),shell=True)
print(' tcset {0} --delay {1}ms'.format(adapter,step))
time.sleep(last_time)
count+=1
else:
subprocess.run('tcset {0} --change --delay {1}ms'.format(adapter, step),shell=True)
print('tcset {0} --change --delay {1}ms'.format(adapter, step))
time.sleep(last_time)
subprocess.run('tcdel {0} --all'.format(adapter),shell=True)
time.sleep(last_time)
print('done')
else:
print("Please indicate uplink (u) or downlink (d)")
def packet_loss(args):
adapter = args.a
if args.d:
subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter),shell=True)
subprocess.run('modprobe ifb',shell=True)
subprocess.run('ip link set dev ifb0 up',shell=True)
subprocess.run('tc qdisc add dev {0} ingress'.format(adapter),shell=True)
subprocess.run(
'tc filter add dev {0} parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0'.format(
adapter),shell=True)
count = 0
for step in downlink_packet_loss:
if count == 0:
subprocess.run('tc qdisc add dev ifb0 root netem loss {0}%'.format(step),shell=True)
print('tc qdisc add dev ifb0 root netem loss {0}%'.format(step))
time.sleep(last_time)
count += 1
else:
subprocess.run('tc qdisc change dev ifb0 root netem loss {0}%'.format(step),shell=True)
print('tc qdisc change dev ifb0 root netem loss {0}%'.format(step))
time.sleep(last_time)
subprocess.run('tc qdisc del dev {0} root 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev {0} ingress 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev ifb root 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev ifb ingress 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter),shell=True)
subprocess.run('modprobe -r ifb',shell=True)
time.sleep(last_time)
print('done')
elif args.u:
count = 0
for step in uplink_packet_loss:
if count == 0:
subprocess.run('tcset {0} --loss {1}%'.format(adapter, step),shell=True)
print('tcset {0} --loss {1}%'.format(adapter, step))
count += 1
time.sleep(last_time)
else:
subprocess.run('tcset {0} --change --loss {1}%'.format(adapter, step),shell=True)
print('tcset {0} --change --loss {1}%'.format(adapter, step))
time.sleep(last_time)
subprocess.run('tcdel {0} --all'.format(adapter),shell=True)
time.sleep(last_time)
print('done')
else:
print("Please indicate uplink (u) or downlink (d)")
def bandwidth_tc(args):
adapter = args.a
if args.d:
subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter),shell=True)
subprocess.run('modprobe ifb',shell=True)
subprocess.run('ip link set dev ifb0 up',shell=True)
subprocess.run('tc qdisc add dev {0} ingress'.format(adapter),shell=True)
subprocess.run('tc filter add dev {0} parent ffff: protocol ip u32 match u32 0 0 flowid 1:1 action mirred egress redirect dev ifb0'.format(adapter),shell=True)
#subprocess.run('tc qdisc add dev ifb0 root netem rate 128kbit')
count = 0
time.sleep(last_time)
for step in downlink_bandwidth:
if count==0:
subprocess.run('tc qdisc add dev ifb0 root netem rate {0}kbit'.format(step),shell=True)
print('tc qdisc add dev ifb0 root netem rate {0}kbit'.format(step))
time.sleep(last_time)
count+=1
else:
#subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter))
subprocess.run('tc qdisc change dev ifb0 root netem rate {0}kbit'.format(step),shell=True)
print('tc qdisc change dev ifb0 root netem rate {0}kbit'.format(step))
time.sleep(last_time)
subprocess.run('tc qdisc del dev {0} root 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev {0} ingress 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev ifb root 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev ifb ingress 2> /dev/null > /dev/null;'.format(adapter),shell=True)
subprocess.run('tc qdisc del dev {0} handle ffff: ingress'.format(adapter))
subprocess.run('modprobe -r ifb')
time.sleep(last_time)
print('done')
elif args.u:
count = 0
for step in uplink_bandwidth:
if count == 0:
subprocess.run('tcset {0} --rate {1}kbps'.format(adapter, step),shell=True)
#subprocess.run('tcset {0} --rate {1}kbps'.format(adapter, step))
print('tcset {0} --rate {1}kbps'.format(adapter, step))
count += 1
time.sleep(last_time)
else:
subprocess.run('tcset {0} --change --rate {1}kbps'.format(adapter, step), shell=True)
#subprocess.run('tcset {0} --change --rate {1}kbps'.format(adapter, step))
print('tcset {0} --change --rate {1}kbps'.format(adapter, step))
time.sleep(last_time)
subprocess.run('tcdel {0} --all'.format(adapter),shell=True)
#subprocess.run('tcdel {0} --all'.format(adapter))
time.sleep(last_time)
print('done')
else:
print("Please indicate uplink (u) or downlink (d)")
def latency_loss(args):
adapter = args.a
subprocess.run('tcset {0} --delay 5s --port 443'.format(adapter),shell=True)
print('tcset {0} --delay 5s --port 443'.format(adapter))
time.sleep(60)
subprocess.run('tcset {0} --change --delay 10s --port 443'.format(adapter),shell=True)
print('tcset {0} --change --delay 10s --port 443'.format(adapter))
time.sleep(60)
subprocess.run('tcset {0} --change --delay 15s --port 443'.format(adapter),shell=True)
print('tcset {0} --change --delay 15s --port 443'.format(adapter))
time.sleep(60)
subprocess.run('tcset {0} --change --loss 100% --port 443'.format(adapter),shell=True)
print('tcset {0} --change --loss 100% --port 443'.format(adapter))
time.sleep(60)
subprocess.run('tcdel {0} --all'.format(adapter),shell=True)
time.sleep(60)
print('done')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("-a", help='set the adapter', required=True)
parser.add_argument("-b", help='bandwidth',action = 'store_true')
parser.add_argument("-l", help='latency', action='store_true')
parser.add_argument("-d", help='downlink',action='store_true')
parser.add_argument("-u", help='uplink',action='store_true')
parser.add_argument("-p", help='packet loss', action='store_true')
parser.add_argument("-lp", help='latency+loss', action='store_true')
args = parser.parse_args()
if args.b:
bandwidth_tc(args)
elif args.l:
latency(args)
elif args.p:
packet_loss(args)
elif args.lp:
latency_loss(args)
else:
print("Please indicate bandwidth(b), E2Elatency (l),or packet loss (p)")
#main()