Statistics
| Branch: | Tag: | Revision:

mininet / mininet / topolib.py @ b1ec912d

History | View | Annotate | Download (2.54 KB)

1
"Library of potentially useful topologies for Mininet"
2

    
3
from mininet.topo import Topo
4
from mininet.net import Mininet
5

    
6
# The build() method is expected to do both of these things:
7
# pylint: disable=attribute-defined-outside-init, arguments-differ
8

    
9
class TreeTopo( Topo ):
10
    "Topology for a tree network with a given depth and fanout."
11

    
12
    def build( self, depth=1, fanout=2 ):
13
        # Numbering:  h1..N, s1..M
14
        self.hostNum = 1
15
        self.switchNum = 1
16
        # Build topology
17
        self.addTree( depth, fanout )
18

    
19
    def addTree( self, depth, fanout ):
20
        """Add a subtree starting with node n.
21
           returns: last node added"""
22
        isSwitch = depth > 0
23
        if isSwitch:
24
            node = self.addSwitch( 's%s' % self.switchNum )
25
            self.switchNum += 1
26
            for _ in range( fanout ):
27
                child = self.addTree( depth - 1, fanout )
28
                self.addLink( node, child )
29
        else:
30
            node = self.addHost( 'h%s' % self.hostNum )
31
            self.hostNum += 1
32
        return node
33

    
34

    
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

    
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

    
48
    def build( self, x, y ):
49
        if x < 3 or y < 3:
50
            raise Exception( 'Please use 3x3 or greater for compatibility '
51
                            'with 2.1' )
52
        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
                switch = switches[ i, j ] = self.addSwitch(
60
                    's' + loc, dpid='%016x' % dpid )
61
                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
# pylint: enable=attribute-defined-outside-init, arguments-differ