Statistics
| Branch: | Tag: | Revision:

mininet / mininet / topolib.py @ master

History | View | Annotate | Download (2.82 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 this:
7
# pylint: disable=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, 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"""
52
        if x < 3 or y < 3:
53
            raise Exception( 'Please use 3x3 or greater for compatibility '
54
                             'with 2.1' )
55
        if n == 1:
56
            genHostName = lambda loc, k: 'h%s' % ( loc )
57
        else:
58
            genHostName = lambda loc, k: 'h%sx%d' % ( loc, k )
59

    
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( genHostName( loc, k + 1 ) )
71
                    self.addLink( host, switch )
72
        # Connect switches
73
        for i in range( 0, x ):
74
            for j in range( 0, y ):
75
                sw1 = switches[ i, j ]
76
                sw2 = switches[ i, ( j + 1 ) % y ]
77
                sw3 = switches[ ( i + 1 ) % x, j ]
78
                self.addLink( sw1, sw2 )
79
                self.addLink( sw1, sw3 )
80

    
81
# pylint: enable=arguments-differ