From 454ac0288659184aaca3f4d641164baa45090f83 Mon Sep 17 00:00:00 2001 From: Benjamin Hesmans Date: Wed, 7 Jan 2015 14:44:44 +0100 Subject: [PATCH] wip Signed-off-by: Benjamin Hesmans --- src/conf/topo/1_para_3 | 4 +- src/mpConfig.py | 38 ++++++++++++++ src/mpLinkCharacteristics.py | 7 +++ src/mpMininetBuilder.py | 31 +++++++++++ src/mpMultiInterfaceConfig.py | 99 +++++++++++++++++++++++++++++++++++ src/mpMultiInterfaceTopo.py | 14 +++-- src/mpParamTopo.py | 8 +++ src/mpPerf.py | 7 ++- src/mpTopo.py | 29 ++++++---- 9 files changed, 219 insertions(+), 18 deletions(-) create mode 100644 src/mpConfig.py create mode 100644 src/mpMultiInterfaceConfig.py diff --git a/src/conf/topo/1_para_3 b/src/conf/topo/1_para_3 index c1e03eb..8927f90 100644 --- a/src/conf/topo/1_para_3 +++ b/src/conf/topo/1_para_3 @@ -1,6 +1,6 @@ desc:Simple configuration with two para link -leftSubnet:xxx -rightSubnet:yyy +leftSubnet:5.5. +rightSubnet:6.6. midSubnet:zzz #path_x:delay,queueSize(may be calc),bw error diff --git a/src/mpConfig.py b/src/mpConfig.py new file mode 100644 index 0000000..d0b7832 --- /dev/null +++ b/src/mpConfig.py @@ -0,0 +1,38 @@ +class MpConfig: + def __init__(self, topo, param): + self.topo = topo + self.param = param + + def interfaceUpCommand(self, interfaceName, ip, subnet): + s = "ifconfig " + interfaceName + " " + ip + " netmask " + \ + subnet + print(s) + return s + + 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 addRouteDefaultSimple(self, via): + s = "ip route add default via " + via + print(s) + return s + diff --git a/src/mpLinkCharacteristics.py b/src/mpLinkCharacteristics.py index 4f6dc0b..fe3d1e8 100644 --- a/src/mpLinkCharacteristics.py +++ b/src/mpLinkCharacteristics.py @@ -7,6 +7,13 @@ class MpLinkCharacteristics: self.delay = delay self.queueSize = queueSize self.bandwidth = bandwidth + + def asDict(self): + d = {} + d['delay'] = self.delay + d['queueSize'] = self.queueSize + d['bandwidth'] = self.bandwidth + return d def __str__(self): s = "Link id : " + str(self.id) + "\n" diff --git a/src/mpMininetBuilder.py b/src/mpMininetBuilder.py index 08be38d..0b5945b 100644 --- a/src/mpMininetBuilder.py +++ b/src/mpMininetBuilder.py @@ -1,5 +1,36 @@ from mininet.topo import Topo +from mininet.net import Mininet +from mininet.link import TCLink +from mininet.cli import CLI class MpMininetBuilder(Topo): def __init__(self): Topo.__init__( self ) + self.net = None + + def commandTo(self, who, cmd): + 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() + diff --git a/src/mpMultiInterfaceConfig.py b/src/mpMultiInterfaceConfig.py new file mode 100644 index 0000000..4de1a39 --- /dev/null +++ b/src/mpMultiInterfaceConfig.py @@ -0,0 +1,99 @@ +from mpConfig import MpConfig +from mpMultiInterfaceTopo import MpMultiInterfaceTopo +from mpParamTopo import MpParamTopo +from mpTopo import MpTopo + +class MpMultiInterfaceConfig(MpConfig): + def __init__(self, topo, param): + MpConfig.__init__(self, topo, param) + + def configureNetwork(self): + self.configureInterfaces() + self.configureRoute() + + 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( + self.getClientSubnet(i), + self.getClientInterface(i), i) + self.topo.commandTo(self.client, cmd) + + 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 configureInterfaces(self): + 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" + for l in self.topo.switch: + cmd = self.interfaceUpCommand( + self.getClientInterface(i), + self.getClientIP(i), netmask) + self.topo.commandTo(self.client, cmd) + + cmd = self.interfaceUpCommand( + self.getRouterInterfaceSwitch(i), + self.getRouterIPSwitch(i), netmask) + self.topo.commandTo(self.router, cmd) + i = i + 1 + + cmd = self.interfaceUpCommand(self.getRouterInterfaceServer(), + self.getRouterIPServer(), netmask) + self.topo.commandTo(self.router, cmd) + + cmd = self.interfaceUpCommand(self.getServerInterface(), + self.getServerIP(), netmask) + self.topo.commandTo(self.server, cmd) + + def getClientIP(self, interfaceID): + lSubnet = self.param.getParam(MpParamTopo.LSUBNET) + clientIP = lSubnet + str(interfaceID) + ".1" + return clientIP + + def getClientSubnet(self, interfaceID): + lSubnet = self.param.getParam(MpParamTopo.LSUBNET) + clientSubnet = lSubnet + str(interfaceID) + ".0/24" + return clientSubnet + + def getRouterIPSwitch(self, interfaceID): + lSubnet = self.param.getParam(MpParamTopo.LSUBNET) + routerIP = lSubnet + str(interfaceID) + ".2" + return routerIP + + def getRouterIPServer(self): + rSubnet = self.param.getParam(MpParamTopo.RSUBNET) + routerIP = rSubnet + "0.2" + return routerIP + + def getServerIP(self): + rSubnet = self.param.getParam(MpParamTopo.RSUBNET) + serverIP = rSubnet + "0.1" + return serverIP + + 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" diff --git a/src/mpMultiInterfaceTopo.py b/src/mpMultiInterfaceTopo.py index 227dcf3..c11b73d 100644 --- a/src/mpMultiInterfaceTopo.py +++ b/src/mpMultiInterfaceTopo.py @@ -4,13 +4,19 @@ class MpMultiInterfaceTopo(MpTopo): def __init__(self, topoBuilder, parameterFile): MpTopo.__init__(self,topoBuilder, parameterFile) print("Hello from topo multi if") - self.addHost("Client") - self.addHost("Server") + self.client = self.addHost(MpTopo.clientName) + self.server = self.addHost(MpTopo.serverName) + self.router = self.addHost(MpTopo.routerName) + self.switch = [] for l in self.topoParam.linkCharacteristics: - self.addOneSwitchPerLink(l) + self.switch.append(self.addOneSwitchPerLink(l)) + self.addLink(self.client,self.switch[-1]) + self.addLink(self.switch[-1],self.router, **l.asDict()) + self.addLink(self.router, self.server) def addOneSwitchPerLink(self, link): - self.addSwitch("sw" + str(link.id)) + return self.addSwitch(MpMultiInterfaceTopo.switchNamePrefix + + str(link.id)) def __str__(self): s = "Simple multiple interface topolgy \n" diff --git a/src/mpParamTopo.py b/src/mpParamTopo.py index d9e00c4..0daa592 100644 --- a/src/mpParamTopo.py +++ b/src/mpParamTopo.py @@ -2,6 +2,12 @@ from mpLinkCharacteristics import MpLinkCharacteristics class MpParamTopo: + LSUBNET = "leftSubnet" + RSUBNET = "rightSubnet" + defaultValue = {} + defaultValue[LSUBNET] = "10.1." + defaultValue[RSUBNET] = "10.2." + def __init__(self, paramFile): self.paramDic = {} self.linkCharacteristics = [] @@ -43,6 +49,8 @@ class MpParamTopo: def getParam(self, key): if key in self.paramDic: return self.paramDic[key] + elif key in MpParamTopo.defaultValue: + return MpParamTopo[key] else: raise Exception("Param not found " + key) diff --git a/src/mpPerf.py b/src/mpPerf.py index d87a17e..1a6387b 100755 --- a/src/mpPerf.py +++ b/src/mpPerf.py @@ -3,6 +3,7 @@ import sys, getopt from mpParamTopo import MpParamTopo from mpMultiInterfaceTopo import MpMultiInterfaceTopo +from mpMultiInterfaceConfig import MpMultiInterfaceConfig from mpMininetBuilder import MpMininetBuilder topoParamFile = None @@ -38,7 +39,11 @@ if __name__ == '__main__': if topoType == "mininet": if param.getParam('topoType') == "MultiIf": mpTopo = MpMultiInterfaceTopo(MpMininetBuilder(), param) - + mpConfig = MpMultiInterfaceConfig(mpTopo, param) + mpTopo.startNetwork() + mpConfig.configureNetwork() + mpTopo.getCLI() + mpTopo.stopNetwork() else: print("Unrecognized topo type") print(mpTopo) diff --git a/src/mpTopo.py b/src/mpTopo.py index f8c6ad4..7069379 100644 --- a/src/mpTopo.py +++ b/src/mpTopo.py @@ -1,28 +1,35 @@ class MpTopo: + switchNamePrefix = "s" + clientName = "Client" + serverName = "Server" + routerName = "Router" + """Simple MpTopo""" def __init__(self, topoBuilder, topoParam): self.topoBuilder = topoBuilder self.topoParam = topoParam def commandTo(self, who, cmd): - pass + self.topoBuilder.commandTo(who, cmd) def getHost(self, who): - pass + return self.topoBuilder.getHost(who) def addHost(self, host): - print("TODO, add host " + host) - self.topoBuilder.addHost(host) - pass + return self.topoBuilder.addHost(host) def addSwitch(self, switch): - print("TODO, add switchi " + switch) - self.topoBuilder.addSwitch(switch) - pass + return self.topoBuilder.addSwitch(switch) def addLink(self, fromA, toB, **kwargs): - #todo check syntax for **kwargs + print(kwargs) self.topoBuilder.addLink(fromA,toB,**kwargs) - pass - + def getCLI(self): + self.topoBuilder.getCLI() + + def startNetwork(self): + self.topoBuilder.startNetwork() + + def stopNetwork(self): + self.topoBuilder.stopNetwork()