forked from giangnvbk1989/cs244-pa3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·243 lines (200 loc) · 7.47 KB
/
run.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
242
243
#!/usr/bin/python
# Written by CJ Cullen and Stephen Barber for CS 244 at Stanford, Spring 2014
# Partially based on code from AMI ID ami-9e7ce9ae from an earlier quarter
import os
import signal
import sys
from subprocess import Popen, PIPE
from time import sleep
import termcolor as T
import argparse
from mininet.net import Mininet
from mininet.log import lg
from mininet.node import OVSKernelSwitch as Switch
from mininet.link import Link, TCLink
from mininet.util import makeNumeric, custom
from topo import Wifi3GTopo
from argparse import ArgumentParser
parser = ArgumentParser(description="MPTCP WiFi/3G Latency tests")
parser.add_argument('--bw-wifi',
dest="bw_wifi",
type=float,
action="store",
help="Bandwidth of WiFi link (Mbps)",
default=30)
parser.add_argument('--bw-3g',
dest="bw_3g",
type=float,
action="store",
help="Bandwidth of 3G link (Mbps)",
default=3)
parser.add_argument('--latency-wifi',
dest="latency_wifi",
type=float,
action="store",
help="Latency of WiFi link (ms)",
default=5)
parser.add_argument('--latency-3g',
dest="latency_3g",
type=float,
action="store",
help="Latency of 3G link (ms)",
default=30)
parser.add_argument('--loss-wifi',
dest="loss_wifi",
type=float,
action="store",
help="Loss rate of WiFi link (0.0-1.0)",
default=0.75)
parser.add_argument('--loss-3g',
dest="loss_3g",
type=float,
action="store",
help="Loss rate of 3G link (0.0-1.0)",
default=0.1)
parser.add_argument('--jitter-wifi',
dest="jitter_wifi",
type=float,
action="store",
help="Jitter of WiFi link (ms)",
default=0.0)
parser.add_argument('--jitter-3g',
dest="jitter_3g",
type=float,
action="store",
help="Jitter of 3G link (ms)",
default=100.0)
args = parser.parse_args()
net = None
signames = dict((k, v) for v, k in signal.__dict__.iteritems() if v.startswith('SIG'))
def signal_cleanup(signum, frame):
global signames
lg.info("Received signal: %s\n" % signames[signum])
if net is not None:
net.stop()
sys.exit(1)
def sysctl_set(key, value):
"""Issue systcl for given param to given value and check for error."""
p = Popen("sysctl -w %s=%s" % (key, value), shell=True, stdout=PIPE,
stderr=PIPE)
# Output should be empty; otherwise, we have an issue.
stdout, stderr = p.communicate()
stdout_expected = "%s = %s\n" % (key, value)
if stdout != stdout_expected:
raise Exception("Popen returned unexpected stdout: %s != %s" %
(stdout, stdout_expected))
if stderr:
raise Exception("Popen returned unexpected stderr: %s" % stderr)
def set_mptcp_enabled(enabled):
"""Enable MPTCP if true, disable if false"""
e = 1 if enabled else 0
lg.info("setting MPTCP enabled to %s\n" % e)
sysctl_set('net.mptcp.mptcp_enabled', e)
def set_optimizations_enabled(enabled):
"""Enable MPTCP optimizations if true, disable if false"""
e = 1 if enabled else 0
lg.info("setting MPTCP opts to %s\n" % e)
sysctl_set('net.mptcp.mptcp_rbuf_opti', e)
sysctl_set('net.mptcp.mptcp_rbuf_penal', e)
sysctl_set('net.mptcp.mptcp_rbuf_retr', e)
def setup(mptcp, optimizations):
set_mptcp_enabled(mptcp)
set_optimizations_enabled(optimizations)
def run(mptcp, net, type):
if type == 'wifi':
log = 'logfile_wifi'
ip = '10.0.0.4'
elif type == '3g':
log = 'logfile_3g'
ip = '10.0.1.4'
else:
if type == 'mptcp_noopt':
log = 'logfile_mptcp_noopt'
else:
log = 'logfile_mptcp'
ip = '10.0.0.4'
h1 = net.getNodeByName('h1')
h2 = net.getNodeByName('h2')
for i in range(2):
# Setup IPs:
h1.cmdPrint('ifconfig h1-eth%i 10.0.%i.3 netmask 255.255.255.0' % (i, i))
h2.cmdPrint('ifconfig h2-eth%i 10.0.%i.4 netmask 255.255.255.0' % (i, i))
h1.cmdPrint('ifconfig h1-eth%i txqueuelen 50' % (i, ))
h2.cmdPrint('ifconfig h2-eth%i txqueuelen 50' % (i, ))
if mptcp:
lg.info("configuring source-specific routing tables for MPTCP\n")
# This creates two different routing tables, that we use based on the
# source-address.
dev = 'h1-eth%i' % i
table = '%s' % (i + 1)
h1.cmdPrint('ip rule add from 10.0.%i.3 table %s' % (i, table))
h1.cmdPrint('ip route add 10.0.%i.0/24 dev %s scope link table %s' % (i, dev, table))
h1.cmdPrint('ip route add default via 10.0.%i.1 dev %s table %s' % (i, dev, table))
# verify connectivity with a ping test.
lg.info("pinging each destination interface\n")
for i in range(2):
h2_out = h2.cmd('ping -c 3 10.0.%i.3' % i)
lg.info("ping test output: %s\n" % h2_out)
lg.info("%s: starting server and client\n" % type)
server = h2.popen('./server', log)
client = h1.popen('./client', ip)
retcode = client.wait()
if retcode != 0:
return False
sleep(2)
server.terminate()
lg.info("%s run completed\n" % type)
return True
def end():
set_mptcp_enabled(False)
def genericTest(topo, setup, run, end, type):
global net
if type == 'wifi' or type == '3g':
mptcp = False
else:
mptcp = True
if type == 'mptcp_noopt':
optimizations = False
else:
optimizations = True
net = Mininet(topo=topo, link=TCLink)
setup(mptcp, optimizations)
net.start()
success = run(mptcp, net, type)
net.stop()
end()
return success
def main():
lg.setLogLevel('info')
if os.geteuid() != 0:
lg.info("This script uses Mininet, and must be run as root.\n")
sys.exit(1)
topo = Wifi3GTopo(bw_wifi=args.bw_wifi,
bw_3g=args.bw_3g,
latency_wifi='%fms' % (args.latency_wifi,),
latency_3g='%fms' % (args.latency_3g,),
loss_wifi=args.loss_wifi,
loss_3g=args.loss_3g,
jitter_wifi='%fms' % (args.jitter_wifi,),
jitter_3g='%fms' % (args.jitter_3g,))
# Compile client and server
os.system('gcc server.c -o server')
os.system('gcc client.c -o client')
signal.signal(signal.SIGABRT, signal_cleanup)
signal.signal(signal.SIGHUP, signal_cleanup)
signal.signal(signal.SIGINT, signal_cleanup)
signal.signal(signal.SIGTERM, signal_cleanup)
types = [ 'wifi', '3g', 'mptcp', 'mptcp_noopt' ]
trylimit = 3
for type in types:
lg.info("Running %s\n" % type)
success = False
tries = 0
while not success and tries < trylimit:
success = genericTest(topo, setup, run, end, type)
tries += 1
if not success:
lg.info("Test failed to run, retrying (%d tries remaining)\n" % (trylimit - tries,))
return 0
if __name__ == '__main__':
main()