Statistics
| Branch: | Tag: | Revision:

mininet / mininet / topolib.py @ b1ec912d

History | View | Annotate | Download (2.54 KB)

1 11a6d400 Bob Lantz
"Library of potentially useful topologies for Mininet"
2
3 5a8bb489 Bob Lantz
from mininet.topo import Topo
4 cd745748 Bob Lantz
from mininet.net import Mininet
5 11a6d400 Bob Lantz
6 b1ec912d Bob Lantz
# The build() method is expected to do both of these things:
7
# pylint: disable=attribute-defined-outside-init, arguments-differ
8
9 11a6d400 Bob Lantz
class TreeTopo( Topo ):
10
    "Topology for a tree network with a given depth and fanout."
11
12 1b2c7a31 Bob Lantz
    def build( self, depth=1, fanout=2 ):
13 5a8bb489 Bob Lantz
        # Numbering:  h1..N, s1..M
14 06b8210e Bob Lantz
        self.hostNum = 1
15 5a8bb489 Bob Lantz
        self.switchNum = 1
16 11a6d400 Bob Lantz
        # Build topology
17 06b8210e Bob Lantz
        self.addTree( depth, fanout )
18 11a6d400 Bob Lantz
19 06b8210e Bob Lantz
    def addTree( self, depth, fanout ):
20 11a6d400 Bob Lantz
        """Add a subtree starting with node n.
21
           returns: last node added"""
22
        isSwitch = depth > 0
23 06b8210e Bob Lantz
        if isSwitch:
24 ce15c4f6 Bob Lantz
            node = self.addSwitch( 's%s' % self.switchNum )
25 06b8210e Bob Lantz
            self.switchNum += 1
26 5a8bb489 Bob Lantz
            for _ in range( fanout ):
27
                child = self.addTree( depth - 1, fanout )
28 ce15c4f6 Bob Lantz
                self.addLink( node, child )
29 06b8210e Bob Lantz
        else:
30 ce15c4f6 Bob Lantz
            node = self.addHost( 'h%s' % self.hostNum )
31 06b8210e Bob Lantz
            self.hostNum += 1
32 5a8bb489 Bob Lantz
        return node
33 11a6d400 Bob Lantz
34 cd745748 Bob Lantz
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 )
39 b5962e8e Bob Lantz
40
41
class TorusTopo( Topo ):
42
    """2-D Torus topology
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"""
47 5a530af1 Bob Lantz
48 1b2c7a31 Bob Lantz
    def build( self, x, y ):
49 b5962e8e Bob Lantz
        if x < 3 or y < 3:
50
            raise Exception( 'Please use 3x3 or greater for compatibility '
51 2a08dec6 Bob Lantz
                            'with 2.1' )
52 b5962e8e Bob Lantz
        hosts, switches, dpid = {}, {}, 0
53
        # Create and wire interior
54
        for i in range( 0, x ):
55
            for j in range( 0, y ):
56
                loc = '%dx%d' % ( i + 1, j + 1 )
57
                # dpid cannot be zero for OVS
58
                dpid = ( i + 1 ) * 256 + ( j + 1 )
59 b1ec912d Bob Lantz
                switch = switches[ i, j ] = self.addSwitch(
60
                    's' + loc, dpid='%016x' % dpid )
61 b5962e8e Bob Lantz
                host = hosts[ i, j ] = self.addHost( 'h' + loc )
62
                self.addLink( host, switch )
63
        # Connect switches
64
        for i in range( 0, x ):
65
            for j in range( 0, y ):
66
                sw1 = switches[ i, j ]
67
                sw2 = switches[ i, ( j + 1 ) % y ]
68
                sw3 = switches[ ( i + 1 ) % x, j ]
69
                self.addLink( sw1, sw2 )
70
                self.addLink( sw1, sw3 )
71
72 b1ec912d Bob Lantz
# pylint: enable=attribute-defined-outside-init, arguments-differ