1 "Library of potentially useful topologies for Mininet"
3 from mininet.topo import Topo
4 from mininet.net import Mininet
6 # The build() method is expected to do this:
7 # pylint: disable=arguments-differ
9 class TreeTopo( Topo ):
10 "Topology for a tree network with a given depth and fanout."
12 def build( self, depth=1, fanout=2 ):
13 # Numbering: h1..N, s1..M
17 self.addTree( depth, fanout )
19 def addTree( self, depth, fanout ):
20 """Add a subtree starting with node n.
21 returns: last node added"""
24 node = self.addSwitch( 's%s' % self.switchNum )
26 for _ in range( fanout ):
27 child = self.addTree( depth - 1, fanout )
28 self.addLink( node, child )
30 node = self.addHost( 'h%s' % self.hostNum )
35 def TreeNet( depth=1, fanout=2, **kwargs ):
36 "Convenience function for creating tree networks."
37 topo = TreeTopo( depth, fanout )
38 return Mininet( topo, **kwargs )
41 class TorusTopo( Topo ):
43 WARNING: this topology has LOOPS and WILL NOT WORK
44 with the default controller or any Ethernet bridge
45 without STP turned on! It can be used with STP, e.g.:
46 # mn --topo torus,3,3 --switch lxbr,stp=1 --test pingall"""
48 def build( self, x, y, n=1 ):
49 """x: dimension of torus in x-direction
50 y: dimension of torus in y-direction
51 n: number of hosts per switch"""
53 raise Exception( 'Please use 3x3 or greater for compatibility '
56 genHostName = lambda loc, k: 'h%s' % ( loc )
58 genHostName = lambda loc, k: 'h%sx%d' % ( loc, k )
60 hosts, switches, dpid = {}, {}, 0
61 # Create and wire interior
62 for i in range( 0, x ):
63 for j in range( 0, y ):
64 loc = '%dx%d' % ( i + 1, j + 1 )
65 # dpid cannot be zero for OVS
66 dpid = ( i + 1 ) * 256 + ( j + 1 )
67 switch = switches[ i, j ] = self.addSwitch(
68 's' + loc, dpid='%x' % dpid )
69 for k in range( 0, n ):
70 host = hosts[ i, j, k ] = self.addHost(
71 genHostName( loc, k + 1 ) )
72 self.addLink( host, switch )
74 for i in range( 0, x ):
75 for j in range( 0, y ):
76 sw1 = switches[ i, j ]
77 sw2 = switches[ i, ( j + 1 ) % y ]
78 sw3 = switches[ ( i + 1 ) % x, j ]
79 self.addLink( sw1, sw2 )
80 self.addLink( sw1, sw3 )
82 # pylint: enable=arguments-differ