forked from evilkant/mpquic_exp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp_setup.py
410 lines (312 loc) · 11.9 KB
/
exp_setup.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import sys, getopt
import math
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI
from subprocess import Popen,PIPE
topoParamFile = None
def parseArgs(argv):
global topoParamFile
try:
opts,args=getopt.getopt(argv,"t:",["topoParam="])
except getopt.GetoptError:
sys.exit(1)
for opt,arg in opts:
if opt in ("-t",'--topoParam'):
topoParamFile=arg
if topoParamFile is None:
print("Missing the topo...")
sys.exit(1)
class attributed_link:
def __init__(self,id,delay,queueSize,bandwidth,loss,back_up=False):
self.id=id
self.delay=delay
self.queueSize=queueSize
self.bandwidth=bandwidth
self.loss=loss
#self.queuingDelay
self.back_up=back_up
def asDict(self):
d={}
d['bw']=float(self.bandwidth)
d['delay']=self.delay+"ms"
d['loss']=float(self.loss)
d['max_queue_size']=int(self.queueSize)
return d
class MpParamTopo:
# this object holds paramDic and linkAttrs
LSUBNET="leftSubnet"
RSUBNET="rightSubnet"
netemAt="netemAt_"
changeNetem="changeNetem"
defaultValue={}
defaultValue[LSUBNET]="10.1."
defaultValue[RSUBNET]="10.2."
defaultValue[changeNetem]="false"
def __init__(self,paramFile):
self.paramDic={}
self.loadParamFile(paramFile) #flesh out the paramDic
#for k,v in self.paramDic.items():
# print("("+k+":"+v+")")
self.linkAttrs=[]
self.loadLinkAttrs()
#print(len(self.linkAttrs))
def loadParamFile(self,paramFile):
file=open(paramFile)
i=0
for line in file:
i=i+1
if line.startswith("#"): #ignore comments
continue
tab=line.split(":")
if len(tab)==2:
k=tab[0]
v=tab[1][:-1] #[:-1] is used to get rid of \n
if k in self.paramDic:
if not isinstance(self.paramDic[k],list):
self.paramDic[k]=[self.paramDic[k]]
self.paramDic[k].append(v)
else:
self.paramDic[k]=v
else:
print("Ignored line "+str(i) + "In file "+ paramFile)
file.close()
def loadLinkAttrs(self):
i=0
for k in sorted(self.paramDic): # i do not see why we need to sort it
if k.startswith("path"):
tab=self.paramDic[k].split(",")
backup=False
loss="0.0"
if len(tab)==5:
loss=tab[3]
backup=(tab[4]=='True')
if len(tab)==4:
try:
loss=float(tab[3])
loss=tab[3]
except ValueError:
backup=(tab[3]=='True')
if len(tab)==3 or len(tab)==4 or len(tab)==5:
path=attributed_link(i,tab[0],tab[1],tab[2],loss,backup)
#id delay queueSize bandwidth loss back_up
self.linkAttrs.append(path)
i=i+1
else:
print("Ignored path "+self.paramDic[k])
def getParamFromTopoParamDic(self,k):
if k in self.paramDic:
return self.paramDic[k]
return None
class MpTopo:
#static properties
clientName="Client"
serverName="Server"
routerName="Router"
switchNamePrefix="s"
routerNamePrefix="r"
def __init__(self,topoBuilder,topoParam):
self.topoBuilder=topoBuilder
self.topoParam=topoParam
self.client=self.addHost(self.clientName)
self.server=self.addHost(self.serverName)
self.router=self.addHost(self.routerName)
self.switch=[]
for link in self.topoParam.linkAttrs:
self.switch.append(self.addOneSwitchPerLink(link))
self.addLink(self.client,self.switch[-1])
self.addLink(self.switch[-1],self.router,**link.asDict())
self.addLink(self.router,self.server)
def addOneSwitchPerLink(self,link):
return self.addSwitch(self.switchNamePrefix+str(link.id))
def getAttributedLinks(self):
return self.topoParam.linkAttrs
def commandTo(self,who,cmd):
return self.topoBuilder.commandTo(who,cmd)
def getHost(self,who):
return self.topoBuilder.getHost(who)
def addHost(self,who):
return self.topoBuilder.addHost(who)
def addSwitch(self,who):
return self.topoBuilder.addSwitch(who)
def addLink(self,fromA,toB,**kwargs):
self.topoBuilder.addLink(fromA,toB,**kwargs)
def getCLI(self):
self.topoBuilder.getCLI()
def startNetwork(self):
self.topoBuilder.startNetwork()
def stopNetwork(self):
self.topoBuilder.stopNetwork()
class MpConfig:
def __init__(self,topo,param):
self.topo=topo
self.param=param
def configureNetwork(self):
self.configureInterfaces()
self.configureRoute()
def interfaceUpCommand(self,interfaceName,ip,subnet):
s="ifconfig "+ interfaceName+" "+ip+" netmask "+ subnet
return s
def configureInterfaces(self): #we did not configure switch though
self.client = self.topo.getHost(MpTopo.clientName)
self.server = self.topo.getHost(MpTopo.serverName)
self.router = self.topo.getHost(MpTopo.routerName)
i = 0
netmask = "255.255.255.0" #why do we need the netmask
links = self.topo.getAttributedLinks()
for l in self.topo.switch:
cmd = self.interfaceUpCommand( self.getClientInterface(i), self.getClientIP(i), netmask)
self.topo.commandTo(self.client, cmd)
clientIntfMac = self.client.intf(self.getClientInterface(i)).MAC()
self.topo.commandTo(self.router, "arp -s " + self.getClientIP(i) + " " + clientIntfMac)
#if(links[i].back_up):
# cmd = self.interfaceBUPCommand(
# self.getClientInterface(i))
# self.topo.commandTo(self.client, cmd)
cmd = self.interfaceUpCommand(
self.getRouterInterfaceSwitch(i),
self.getRouterIPSwitch(i), netmask)
self.topo.commandTo(self.router, cmd)
routerIntfMac = self.router.intf(self.getRouterInterfaceSwitch(i)).MAC()
self.topo.commandTo(self.client, "arp -s " + self.getRouterIPSwitch(i) + " " + routerIntfMac)
print(str(links[i]))
i = i + 1
cmd = self.interfaceUpCommand(self.getRouterInterfaceServer(),
self.getRouterIPServer(), netmask)
self.topo.commandTo(self.router, cmd)
routerIntfMac = self.router.intf(self.getRouterInterfaceServer()).MAC()
self.topo.commandTo(self.server, "arp -s " + self.getRouterIPServer() + " " + routerIntfMac)
cmd = self.interfaceUpCommand(self.getServerInterface(),
self.getServerIP(), netmask)
self.topo.commandTo(self.server, cmd)
serverIntfMac = self.server.intf(self.getServerInterface()).MAC()
self.topo.commandTo(self.router, "arp -s " + self.getServerIP() + " " + serverIntfMac)
def configureRoute(self):
i=0
for l in self.topo.switch:
cmd=self.addRouteTableCommand(self.getClientIP(i),i)
self.topo.commandTo(self.client,cmd)
cmd=self.addRouteScopeLinkCommand( #route scope, maybe related to subnet
self.getClientSubnet(i),
self.getClientInterface(i),
i
)
self.topo.commandTo(self.client,cmd)
self.addRouteDefaultCommand(self.getRouterIPSwitch(i),i)
self.topo.commandTo(self.client,cmd)
i=i+1
cmd=self.addRouteDefaultGlobalCommand(self.getRouterIPSwitch(0),
self.getClientInterface(0))
self.topo.commandTo(self.client,cmd)
cmd=self.addRouteDefaultSimple(self.getRouterIPServer())
self.topo.commandTo(self.server,cmd)
def getClientIP(self,interfaceID):
lSubnet=self.param.getParamFromTopoParamDic(MpParamTopo.LSUBNET)
clientIP=lSubnet+str(interfaceID)+".1"
return clientIP
def getClientSubnet(self,interfaceID):
lSubnet=self.param.getParamFromTopoParamDic(MpParamTopo.LSUBNET)
clinetSubnet=lSubnet+str(interfaceID)+".0/24"
return clinetSubnet
def getRouterIPSwitch(self,interfaceID):
lSubnet=self.param.getParamFromTopoParamDic(MpParamTopo.LSUBNET)
routerIP=lSubnet+str(interfaceID)+".2"
return routerIP
def getRouterIPServer(self):
rSubnet=self.param.getParamFromTopoParamDic(MpParamTopo.RSUBNET)
routerIP=rSubnet+"0.2"
return routerIP
def getServerIP(self):
rSubnet=self.param.getParamFromTopoParamDic(MpParamTopo.RSUBNET)
serverIP=rSubnet+"0.1"
return serverIP
def getClientInterfaceCount(self):
return len(self.topo.switch)
def getRouterInterfaceServer(self):
return self.getRouterInterfaceSwitch(len(self.topo.switch))
def getClientInterface(self, interfaceID):
return MpTopo.clientName + "-eth" + str(interfaceID)
def getRouterInterfaceSwitch(self, interfaceID):
return MpTopo.routerName + "-eth" + str(interfaceID)
def getServerInterface(self):
return MpTopo.serverName + "-eth0"
def getMidLeftName(self, id):
return MpTopo.switchNamePrefix + str(id)
def getMidRightName(self, id):
return MpTopo.routerName
def getMidL2RInterface(self, id):
return self.getMidLeftName(id) + "-eth2"
def getMidR2LInterface(self, id):
return self.getMidRightName(id) + "-eth" + str(id)
def addRouteTableCommand(self, fromIP, id):
s = "ip rule add from " + fromIP + " table " + str(id + 1)
print(s)
return s
def addRouteScopeLinkCommand(self, network, interfaceName, id):
s = "ip route add " + network + " dev " + interfaceName + \
" scope link table " + str(id + 1)
print(s)
return s
def addRouteDefaultCommand(self, via, id):
s = "ip route add default via " + via + " table " + str(id + 1)
print(s)
return s
def addRouteDefaultGlobalCommand(self, via, interfaceName):
s = "ip route add default scope global nexthop via " + via + \
" dev " + interfaceName
print(s)
return s
def arpCommand(self, ip, mac):
s = "arp -s " + ip + " " + mac
print(s)
return s
def addRouteDefaultSimple(self, via):
s = "ip route add default via " + via
print(s)
return s
#this class inherits from mininet class Topo
class MininetBuilder(Topo):
def __init__(self):
Topo.__init__(self)
self.net=None
def commandTo(self,who,cmd):
return who.cmd(cmd)
def startNetwork(self):
self.net=Mininet(topo=self,link=TCLink)
self.net.start()
def getCLI(self):
if self.net is None:
print("can not get the CLI")
else:
CLI(self.net)
def getHost(self,who):
if self.net is None:
print("network not available...")
raise Exception("network not ready")
else:
return self.net.getNodeByName(who)
def stopNetwork(self):
if self.net is None:
print("could not stop network... nothing to stop")
else:
self.net.stop()
class MpXpRunner:
def __init__(self,topoParamFile):
self.topoParamObj=MpParamTopo(topoParamFile)
# so far so good
self.topoBuilder=MininetBuilder()
#should be no problem up to this point
self.mpTopo=MpTopo(self.topoBuilder,self.topoParamObj)
self.mpTopoConfig=MpConfig(self.mpTopo,self.topoParamObj)
#start topo
self.mpTopo.startNetwork()
self.mpTopoConfig.configureNetwork()
#strat experiment
#MpExperienceQUIC()
self.mpTopo.getCLI()
#stop topo
#self.mpTopo.stopNetwork()
if __name__=='__main__':
parseArgs(sys.argv[1:])
MpXpRunner(topoParamFile)