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 | #!/usr/bin/python """ Run Mininet network using tree topology per remote controller. """ from mininet.cli import CLI from mininet.log import info, setLogLevel from mininet.net import Mininet from mininet.node import Host, OVSKernelSwitch, RemoteController from mininet.topo import Topo TreeDepth = 2 FanOut = 2 ControllerAddress = ["172.31.2.70", "172.31.2.72"] class MultiTreeTopo(Topo): """Topology for multiple tree network using remote controllers. A tree network is assigned to a remote controller.""" def __init__(self): Topo.__init__(self) self.hostSize = 1 self.switchSize = 1 self.treeSwitches = prev = None for cidx in range(len(ControllerAddress)): switches = self.treeSwitches.append(switches) root = self.addTree(switches, TreeDepth, FanOut) if prev: self.addLink(prev, root) prev = root def addTree(self, switches, depth, fanout): """Add a tree node.""" if depth > 0: node = self.addSwitch('s%u' % self.switchSize) self.switchSize += 1 switches.append(node) for i in range(fanout): child = self.addTree(switches, depth - 1, fanout) self.addLink(node, child) else: node = self.addHost('h%u' % self.hostSize) self.hostSize += 1 return node def start(self, net): """Start all controllers and switches in the network.""" cidx = 0 for c in net.controllers: info("*** Starting controller: %s\n" % c) info("+ Starting switches ... ") switches = self.treeSwitches[cidx] for sname in switches: s = net.getNodeByName(sname) info(" %s" % s) s.start([c]) cidx += 1 info("\n") self.treeSwitches = None class MultiTreeNet(Mininet): """Mininet network environment with multiple tree network using remote controllers.""" def __init__(self, **args): args['topo'] = MultiTreeTopo args['switch'] = OVSKernelSwitch args['controller'] = RemoteController args['build'] = False Mininet.__init__(self, **args) idx = 1 for addr in ControllerAddress: name = 'c%d' % idx info('*** Creating remote controller: %s (%s)\n' % (name, addr)) self.addController(name, ip=addr, port=6633) idx = idx + 1 def start(self): "Start controller and switches." if not self.built: self.build self.topo.start(self) if __name__ == '__main__': setLogLevel('info')# for CLI output net = MultiTreeNet net.build print "*** Starting network" net.start print "*** Running CLI" CLI(net) print "*** Stopping network" net.stop |
欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/) | Powered by Discuz! X3 |