Statistics
| Branch: | Tag: | Revision:

## mininet / mininet / topolib.py @ master

1 2 3 11a6d400 Bob Lantz ```"Library of potentially useful topologies for Mininet" ``` 5a8bb489 Bob Lantz ```from mininet.topo import Topo ``` cd745748 Bob Lantz ```from mininet.net import Mininet ``` 11a6d400 Bob Lantz 061598f0 Bob Lantz ```# The build() method is expected to do this: ``` ```# pylint: disable=arguments-differ ``` b1ec912d Bob Lantz 11a6d400 Bob Lantz ```class TreeTopo( Topo ): ``` ``` "Topology for a tree network with a given depth and fanout." ``` 1b2c7a31 Bob Lantz ``` def build( self, depth=1, fanout=2 ): ``` 5a8bb489 Bob Lantz ``` # Numbering: h1..N, s1..M ``` 06b8210e Bob Lantz ``` self.hostNum = 1 ``` 5a8bb489 Bob Lantz ``` self.switchNum = 1 ``` 11a6d400 Bob Lantz ``` # Build topology ``` 06b8210e Bob Lantz ``` self.addTree( depth, fanout ) ``` 11a6d400 Bob Lantz 06b8210e Bob Lantz ``` def addTree( self, depth, fanout ): ``` 11a6d400 Bob Lantz ``` """Add a subtree starting with node n. ``` ``` returns: last node added""" ``` ``` isSwitch = depth > 0 ``` 06b8210e Bob Lantz ``` if isSwitch: ``` ce15c4f6 Bob Lantz ``` node = self.addSwitch( 's%s' % self.switchNum ) ``` 06b8210e Bob Lantz ``` self.switchNum += 1 ``` 5a8bb489 Bob Lantz ``` for _ in range( fanout ): ``` ``` child = self.addTree( depth - 1, fanout ) ``` ce15c4f6 Bob Lantz ``` self.addLink( node, child ) ``` 06b8210e Bob Lantz ``` else: ``` ce15c4f6 Bob Lantz ``` node = self.addHost( 'h%s' % self.hostNum ) ``` 06b8210e Bob Lantz ``` self.hostNum += 1 ``` 5a8bb489 Bob Lantz ``` return node ``` 11a6d400 Bob Lantz cd745748 Bob Lantz ```def TreeNet( depth=1, fanout=2, **kwargs ): ``` ``` "Convenience function for creating tree networks." ``` ``` topo = TreeTopo( depth, fanout ) ``` ``` return Mininet( topo, **kwargs ) ``` b5962e8e Bob Lantz ```class TorusTopo( Topo ): ``` ``` """2-D Torus topology ``` ``` WARNING: this topology has LOOPS and WILL NOT WORK ``` ``` with the default controller or any Ethernet bridge ``` ``` without STP turned on! It can be used with STP, e.g.: ``` ``` # mn --topo torus,3,3 --switch lxbr,stp=1 --test pingall""" ``` 5a530af1 Bob Lantz b27cce08 Brian O'Connor ``` def build( self, x, y, n=1 ): ``` ``` """x: dimension of torus in x-direction ``` ``` y: dimension of torus in y-direction ``` ``` n: number of hosts per switch""" ``` b5962e8e Bob Lantz ``` if x < 3 or y < 3: ``` ``` raise Exception( 'Please use 3x3 or greater for compatibility ' ``` 7a3159c9 Bob Lantz ``` 'with 2.1' ) ``` b27cce08 Brian O'Connor ``` if n == 1: ``` ``` genHostName = lambda loc, k: 'h%s' % ( loc ) ``` ``` else: ``` ``` genHostName = lambda loc, k: 'h%sx%d' % ( loc, k ) ``` b5962e8e Bob Lantz ``` hosts, switches, dpid = {}, {}, 0 ``` ``` # Create and wire interior ``` ``` for i in range( 0, x ): ``` ``` for j in range( 0, y ): ``` ``` loc = '%dx%d' % ( i + 1, j + 1 ) ``` ``` # dpid cannot be zero for OVS ``` ``` dpid = ( i + 1 ) * 256 + ( j + 1 ) ``` b1ec912d Bob Lantz ``` switch = switches[ i, j ] = self.addSwitch( ``` bb35d041 Bob Lantz ``` 's' + loc, dpid='%x' % dpid ) ``` b27cce08 Brian O'Connor ``` for k in range( 0, n ): ``` ``` host = hosts[ i, j, k ] = self.addHost( genHostName( loc, k + 1 ) ) ``` ``` self.addLink( host, switch ) ``` b5962e8e Bob Lantz ``` # Connect switches ``` ``` for i in range( 0, x ): ``` ``` for j in range( 0, y ): ``` ``` sw1 = switches[ i, j ] ``` ``` sw2 = switches[ i, ( j + 1 ) % y ] ``` ``` sw3 = switches[ ( i + 1 ) % x, j ] ``` ``` self.addLink( sw1, sw2 ) ``` ``` self.addLink( sw1, sw3 ) ``` 061598f0 Bob Lantz `# pylint: enable=arguments-differ`