forked from JoshGelua/Bufferbloat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_bufferbloat.py
282 lines (232 loc) · 9.85 KB
/
old_bufferbloat.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/python
"CS244 Spring 2015 Assignment 1: Bufferbloat"
from mininet.topo import Topo
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.net import Mininet
from mininet.log import lg, info
from mininet.util import dumpNodeConnections
from mininet.cli import CLI
from mininet.clean import cleanup
from subprocess import Popen, PIPE
from time import sleep, time
from multiprocessing import Process
from argparse import ArgumentParser
from helper import avg, stdev
from monitor import monitor_qlen
import termcolor as T
import sys
import os
import math
# TODO: Don't just read the TODO sections in this code. Remember that
# one of the goals of this assignment is for you to learn how to use
# Mininet. :-)
parser = ArgumentParser(description="Bufferbloat tests")
parser.add_argument('--bw-host', '-B',
type=float,
help="Bandwidth of host links (Mb/s)",
default=1000)
parser.add_argument('--bw-net', '-b',
type=float,
help="Bandwidth of bottleneck (network) link (Mb/s)",
required=True)
parser.add_argument('--delay',
type=float,
help="Link propagation delay (ms)",
required=True)
parser.add_argument('--dir', '-d',
help="Directory to store outputs",
required=True)
parser.add_argument('--time', '-t',
help="Duration (sec) to run the experiment",
type=int,
default=10)
parser.add_argument('--maxq',
type=int,
help="Max buffer size of network interface in packets",
default=100)
parser.add_argument('--hosts', '-ho',
type=int,
help="Number of hosts as non-servers",
default=4)
# Linux uses CUBIC-TCP by default that doesn't have the usual sawtooth
# behaviour. For those who are curious, invoke this script with
# --cong cubic and see what happens...
# sysctl -a | grep cong should list some interesting parameters.
parser.add_argument('--cong',
help="Congestion control algorithm to use",
default="reno")
# Expt parameters
args = parser.parse_args()
class BBTopo(Topo):
"Simple topology for bufferbloat experiment."
def build(self, n=2):
# Here are two hosts
hosts = []
for i in range(1,5):
hosts.append(self.addHost('h%d'%(i)))
# Here I have created a switch. If you change its name, its
# interface names will change from s0-eth1 to newname-eth1.
switch = self.addSwitch('s0')
switch2 = self.addSwitch('s1')
# TODO: Add links with appropriate characteristics
for i in range(0, n):
if i == 0:
self.addLink(hosts[i], switch, bw=args.bw_host, delay=args.delay,
max_queue_size=args.maxq)
else:
self.addLink(hosts[i], switch, bw=args.bw_net, delay=args.delay,
max_queue_size=args.maxq)
self.addLink(hosts[2], switch2, bw=args.bw_host, delay=args.delay,
max_queue_size=args.maxq)
self.addLink(hosts[3], switch2, bw=args.bw_host, delay=args.delay,
max_queue_size=args.maxq)
bottleneck = self.addLink(switch, switch2, bw=args.bw_host, delay=args.delay,
max_queue_size=args.maxq)
#sleep(2)
#self.addLink(switch, switch2, bw=args.bw_host, delay=args.delay,
#max_queue_size=args.maxq-10)
return
# Simple wrappers around monitoring utilities. You are welcome to
# contribute neatly written (using classes) monitoring scripts for
# Mininet!
# tcp_probe is a kernel module which records cwnd over time. In linux >= 4.16
# it has been replaced by the tcp:tcp_probe kernel tracepoint.
class DBTopo(Topo):
"Dumbell topology for qbuffer experiment"
def build(self, n=args.hosts):
hosts = []
for i in range(1,n+1):
hosts.append(self.addHost('h%d'%(i)))
switches = []
switches.append(self.addSwitch('s0'))
switches.append(self.addSwitch('s1'))
self.addLink(hosts[1], switches[0], bw=args.bw_net, delay=args.delay,
max_queue_size=args.maxq)
self.addLink(hosts[0], switches[1], bw=args.bw_net, delay=args.delay,
max_queue_size=args.maxq)
self.addLink(hosts[2], switches[0], bw=args.bw_net, delay=args.delay,
max_queue_size=args.maxq)
self.addLink(hosts[3], switches[1], bw=args.bw_net, delay=args.delay,
max_queue_size=args.maxq)
self.addLink(switches[0], switches[1], bw=args.bw_net, delay=args.delay,
max_queue_size=args.maxq)
return
def start_tcpprobe(outfile="cwnd.txt"):
os.system("rmmod tcp_probe; modprobe tcp_probe full=1;")
Popen("cat /proc/net/tcpprobe > %s/%s" % (args.dir, outfile),
shell=True)
def stop_tcpprobe():
Popen("killall -9 cat", shell=True).wait()
def start_qmon(iface, interval_sec, outfile):
monitor = Process(target=monitor_qlen,
args=(iface, interval_sec, outfile))
monitor.start()
return monitor
def start_iperf(net):
h2 = net.get('h2')
print ("Starting iperf server...")
# For those who are curious about the -w 16m parameter, it ensures
# that the TCP flow is not receiver window limited. If it is,
# there is a chance that the router buffer may not get filled up.
server = h2.popen("iperf -s -w 16m")
# TODO: Start the iperf client on h1. Ensure that you create a
# long lived TCP flow. You may need to redirect iperf's stdout to avoid blocking.
h1 = net.get('h1')
h1.popen("iperf -c %s -t %s > %s/iperf.out" % (h2.IP(), args.time, args.dir), shell=True)
def start_webserver(net):
h1 = net.get('h1')
proc = h1.popen("python http/webserver.py", shell=True)
sleep(1)
return [proc]
def start_ping(net):
# TODO: Start a ping train from h1 to h2 (or h2 to h1, does it
# matter?) Measure RTTs every 0.1 second. Read the ping man page
# to see how to do this.
# Hint: Use host.popen(cmd, shell=True). If you pass shell=True
# to popen, you can redirect cmd's output using shell syntax.
# i.e. ping ... > /path/to/ping.txt
# Note that if the command prints out a lot of text to stdout, it will block
# until stdout is read. You can avoid this by runnning popen.communicate() or
# redirecting stdout
h1 = net.get('h1')
h2 = net.get('h2')
popen = h1.popen("ping -i 0.1 %s > %s/ping.txt"%(h2.IP(), args.dir), shell=True)
def bufferbloat():
if not os.path.exists(args.dir):
os.makedirs(args.dir)
os.system("sysctl -w net.ipv4.tcp_congestion_control=%s" % args.cong)
# Cleanup any leftovers from previous mininet runs
cleanup()
topo = BBTopo()
net = Mininet(topo=topo, link=TCLink) #, host=CPULimitedHost, link=TCLink
net.start()
# This dumps the topology and how nodes are interconnected through
# links.
dumpNodeConnections(net.hosts)
# This performs a basic all pairs ping test.
net.pingAll()
# Start all the monitoring processes
#start_tcpprobe("cwnd.txt")
start_ping(net)
# TODO: Start monitoring the queue sizes. Since the switch I
# created is "s0", I monitor one of the interfaces. Which
# interface? The interface numbering starts with 1 and increases.
# Depending on the order you add links to your network, this
# number may be 1 or 2. Ensure you use the correct number.
#
qmon = start_qmon(iface='s0-eth2', interval_sec=0.01,
outfile='%s/q.txt' % (args.dir))
# TODO: Start iperf, webservers, etc.
start_iperf(net)
start_webserver(net)
# Hint: The command below invokes a CLI which you can use to
# debug. It allows you to run arbitrary commands inside your
# emulated hosts h1 and h2.
#
# CLI(net)
# TODO: measure the time it takes to complete webpage transfer
# from h1 to h2 (say) 3 times. Hint: check what the following
# command does: curl -o /dev/null -s -w %{time_total} google.com
# Now use the curl command to fetch webpage from the webserver you
# spawned on host h1 (not from google!)
# Hint: have a separate function to do this and you may find the
# loop below useful.
start_time = time()
time_measures = []
while True:
# do the measurement (say) 3 times.
now = time()
delta = now - start_time
queueSize = args.maxq
if delta > args.time:
break
print ("%.1fs left..." % (args.time - delta))
h1 = net.get('h1')
h2 = net.get('h2')
for i in range(3):
webpage_time = h2.popen('curl -o /dev/null -s -w %%{time_total} %s/http/index.html' %
h1.IP()).communicate()[0]
time_measures.append(float(webpage_time))
sleep(5)
queueSize -= 10
switch1 = net.get('s0')
switch2 = net.get('s1')
net.delLinkBetween(switch1, switch2)
net.addLink(switch1, switch2, bw=args.bw_net, delay=args.delay,
max_queue_size=queueSize)
net.pingAll()
# TODO: compute average (and standard deviation) of the fetch
# times. You don't need to plot them. Just note it in your
# README and explain.
with open('%s/avgsd.txt'%(args.dir), 'w') as f:
f.write(str(time_measures))
#stop_tcpprobe()
if qmon is not None:
qmon.terminate()
net.stop()
# Ensure that all processes you create within Mininet are killed.
# Sometimes they require manual killing.
Popen("pgrep -f webserver.py | xargs kill -9", shell=True).wait()
if __name__ == "__main__":
bufferbloat()