Statistics
| Branch: | Tag: | Revision:

mininet / mininet / topo.py @ c273f490

History | View | Annotate | Download (11.9 KB)

1
#!/usr/bin/env python
2
"""@package topo
3

4
Network topology creation.
5

6
@author Brandon Heller (brandonh@stanford.edu)
7

8
This package includes code to represent network topologies.
9

10
A Topo object can be a topology database for NOX, can represent a physical
11
setup for testing, and can even be emulated with the Mininet package.
12
"""
13

    
14
from mininet.util import irange, natural, naturalSeq
15

    
16
class MultiGraph( object ):
17
    "Utility class to track nodes and edges - replaces networkx.MultiGraph"
18

    
19
    def __init__( self ):
20
        self.node = {}
21
        self.edge = {}
22

    
23
    def add_node( self, node, attr_dict=None, **attrs):
24
        """Add node to graph
25
           attr_dict: attribute dict (optional)
26
           attrs: more attributes (optional)
27
           warning: updates attr_dict with attrs"""
28
        attr_dict = {} if attr_dict is None else attr_dict
29
        attr_dict.update( attrs )
30
        self.node[ node ] = attr_dict
31

    
32
    def add_edge( self, src, dst, key=None, attr_dict=None, **attrs ):
33
        """Add edge to graph
34
           key: optional key
35
           attr_dict: optional attribute dict
36
           attrs: more attributes
37
           warning: udpates attr_dict with attrs"""
38
        attr_dict = {} if attr_dict is None else attr_dict
39
        attr_dict.update( attrs )
40
        self.node.setdefault( src, {} )
41
        self.node.setdefault( dst, {} )
42
        self.edge.setdefault( src, {} )
43
        self.edge.setdefault( dst, {} )
44
        self.edge[ src ].setdefault( dst, {} )
45
        entry = self.edge[ dst ][ src ] = self.edge[ src ][ dst ]
46
        # If no key, pick next ordinal number
47
        if key is None:
48
            keys = [ k for k in entry.keys() if isinstance( k, int ) ]
49
            key = max( [ 0 ] + keys ) + 1
50
        entry[ key ] = attr_dict
51
        return key
52

    
53
    def nodes( self, data=False):
54
        """Return list of graph nodes
55
           data: return list of ( node, attrs)"""
56
        return self.node.items() if data else self.node.keys()
57

    
58
    def edges_iter( self, data=False, keys=False ):
59
        "Iterator: return graph edges"
60
        for src, entry in self.edge.iteritems():
61
            for dst, keys in entry.iteritems():
62
                if src > dst:
63
                    # Skip duplicate edges
64
                    continue
65
                for k, attrs in keys.iteritems():
66
                    if data:
67
                        if keys:
68
                            yield( src, dst, k, attrs )
69
                        else:
70
                            yield( src, dst, attrs )
71
                    else:
72
                        if keys:
73
                            yield( src, dst, k )
74
                        else:
75
                            yield( src, dst )
76

    
77
    def edges( self, data=False, keys=False ):
78
        "Return list of graph edges"
79
        return list( self.edges_iter( data=data, keys=keys ) )
80

    
81

    
82
    def __getitem__( self, node ):
83
        "Return link dict for given src node"
84
        return self.edge[ node ]
85

    
86
    def __len__( self ):
87
        "Return the number of nodes"
88
        return len( self.node )
89

    
90
    def convertTo( self, cls, data=False, keys=False ):
91
        """Convert to a new object of networkx.MultiGraph-like class cls
92
           data: include node and edge data
93
           keys: include edge keys as well as edge data"""
94
        g = cls()
95
        g.add_nodes_from( self.nodes( data=data ) )
96
        g.add_edges_from( self.edges( data=( data or keys ), keys=keys ) )
97
        return g
98

    
99

    
100
class Topo( object ):
101
    "Data center network representation for structured multi-trees."
102

    
103
    def __init__( self, *args, **params ):
104
        """Topo object. 
105
           Optional named parameters:
106
           hinfo: default host options
107
           sopts: default switch options
108
           lopts: default link options
109
           calls build()"""
110
        self.g = MultiGraph()
111
        self.hopts = params.pop( 'hopts', {} )
112
        self.sopts = params.pop( 'sopts', {} )
113
        self.lopts = params.pop( 'lopts', {} )
114
        self.ports = {}  # ports[src][dst][sport] is port on dst that connects to src
115
        self.build( *args, **params )
116

    
117
    def build( self, *args, **params ):
118
        "Override this method to build your topology."
119
        pass
120

    
121
    def addNode( self, name, **opts ):
122
        """Add Node to graph.
123
           name: name
124
           opts: node options
125
           returns: node name"""
126
        self.g.add_node( name, **opts )
127
        return name
128

    
129
    def addHost( self, name, **opts ):
130
        """Convenience method: Add host to graph.
131
           name: host name
132
           opts: host options
133
           returns: host name"""
134
        if not opts and self.hopts:
135
            opts = self.hopts
136
        return self.addNode( name, **opts )
137

    
138
    def addSwitch( self, name, **opts ):
139
        """Convenience method: Add switch to graph.
140
           name: switch name
141
           opts: switch options
142
           returns: switch name"""
143
        if not opts and self.sopts:
144
            opts = self.sopts
145
        result = self.addNode( name, isSwitch=True, **opts )
146
        return result
147

    
148
    def addLink( self, node1, node2, port1=None, port2=None,
149
                key=None, **opts ):
150
        """node1, node2: nodes to link together
151
           port1, port2: ports (optional)
152
           opts: link options (optional)
153
           returns: link info key"""
154
        if not opts and self.lopts:
155
            opts = self.lopts
156
        port1, port2 = self.addPort( node1, node2, port1, port2 )
157
        opts = dict( opts )
158
        opts.update( node1=node1, node2=node2, port1=port1, port2=port2 )
159
        self.g.add_edge(node1, node2, key, opts )
160
        return key
161

    
162
    def nodes( self, sort=True ):
163
        "Return nodes in graph"
164
        if sort:
165
            return self.sorted( self.g.nodes() )
166
        else:
167
            return self.g.nodes()
168

    
169
    def isSwitch( self, n ):
170
        "Returns true if node is a switch."
171
        return self.g.node[ n ].get( 'isSwitch', False )
172

    
173
    def switches( self, sort=True ):
174
        """Return switches.
175
           sort: sort switches alphabetically
176
           returns: dpids list of dpids"""
177
        return [ n for n in self.nodes( sort ) if self.isSwitch( n ) ]
178

    
179
    def hosts( self, sort=True ):
180
        """Return hosts.
181
           sort: sort hosts alphabetically
182
           returns: list of hosts"""
183
        return [ n for n in self.nodes( sort ) if not self.isSwitch( n ) ]
184

    
185
    def iterLinks( self, withKeys=False, withInfo=False ):
186
        """Return links (iterator)
187
           withKeys: return link keys
188
           withInfo: return link info
189
           returns: list of ( src, dst [,key, info ] )"""
190
        for src, dst, key, info in self.g.edges_iter( data=True, keys=True ):
191
            node1, node2 = info[ 'node1' ], info[ 'node2' ]
192
            if withKeys:
193
                if withInfo:
194
                    yield( node1, node2, key, info )
195
                else:
196
                    yield( node1, node2, key )
197
            else:
198
                if withInfo:
199
                    yield( node1, node2, info )
200
                else:
201
                    yield( node1, node2 )
202

    
203
    def links( self, sort=False, withKeys=False, withInfo=False ):
204
        """Return links
205
           sort: sort links alphabetically, preserving (src, dst) order
206
           withKeys: return link keys
207
           withInfo: return link info
208
           returns: list of ( src, dst [,key, info ] )"""
209
        links = list( self.iterLinks( withKeys, withInfo ) )
210
        if not sorted:
211
            return links
212
        # Ignore info when sorting
213
        tupleSize = 3 if withKeys else 2
214
        return sorted( links, key=( lambda l: naturalSeq( l[ :tupleSize ] ) ) )
215

    
216
    # This legacy port management mechanism is clunky and will probably
217
    # be removed at some point.
218

    
219
    def addPort( self, src, dst, sport=None, dport=None ):
220
        """Generate port mapping for new edge.
221
            src: source switch name
222
            dst: destination switch name"""
223
        # Initialize if necessary
224
        ports = self.ports
225
        ports.setdefault( src, {} )
226
        ports.setdefault( dst, {} )
227
        # New port: number of outlinks + base
228
        if sport is None:
229
            src_base = 1 if self.isSwitch( src ) else 0
230
            sport = len( ports[ src ] ) + src_base
231
        if dport is None:
232
            dst_base = 1 if self.isSwitch( dst ) else 0
233
            dport = len( ports[ dst ] ) + dst_base
234
        ports[ src ][ sport ] = ( dst, dport )
235
        ports[ dst ][ dport ] = ( src, sport )
236
        return sport, dport
237

    
238
    def port( self, src, dst ):
239
        """Get port numbers.
240
            src: source switch name
241
            dst: destination switch name
242
            sport: optional source port (otherwise use lowest src port)
243
            returns: tuple (sport, dport), where
244
                sport = port on source switch leading to the destination switch
245
                dport = port on destination switch leading to the source switch
246
            Note that you can also look up ports using linkInfo()"""
247
        # A bit ugly and slow vs. single-link implementation ;-(
248
        ports = [ ( sport, entry[ 1 ] )
249
                  for sport, entry in self.ports[ src ].items()
250
                  if entry[ 0 ] == dst ]
251
        return ports if len( ports ) != 1 else ports[ 0 ]
252

    
253
    def _linkEntry( self, src, dst, key=None ):
254
        "Helper function: return link entry and key"
255
        entry = self.g[ src ][ dst ]
256
        if key is None:
257
            key = min( entry )
258
        return entry, key
259
    
260
    def linkInfo( self, src, dst, key=None ):
261
        "Return link metadata dict"
262
        entry, key = self._linkEntry( src, dst, key )
263
        return entry[ key ]
264

    
265
    def setlinkInfo( self, src, dst, info, key=None ):
266
        "Set link metadata dict"
267
        entry, key = self._linkEntry( src, dst, key )
268
        entry[ key ] = info
269

    
270
    def nodeInfo( self, name ):
271
        "Return metadata (dict) for node"
272
        return self.g.node[ name ]
273

    
274
    def setNodeInfo( self, name, info ):
275
        "Set metadata (dict) for node"
276
        self.g.node[ name ] = info
277

    
278
    def convertTo( self, cls, data=True, keys=True ):
279
        """Convert to a new object of networkx.MultiGraph-like class cls
280
           data: include node and edge data (default True)
281
           keys: include edge keys as well as edge data (default True)"""
282
        return self.g.convertTo( cls, data=data, keys=keys )
283

    
284
    @staticmethod
285
    def sorted( items ):
286
        "Items sorted in natural (i.e. alphabetical) order"
287
        return sorted( items, key=natural )
288

    
289

    
290
class SingleSwitchTopo( Topo ):
291
    "Single switch connected to k hosts."
292

    
293
    def build( self, k=2, **opts ):
294
        "k: number of hosts"
295
        self.k = k
296
        switch = self.addSwitch( 's1' )
297
        for h in irange( 1, k ):
298
            host = self.addHost( 'h%s' % h )
299
            self.addLink( host, switch )
300

    
301

    
302
class SingleSwitchReversedTopo( Topo ):
303
    """Single switch connected to k hosts, with reversed ports.
304
       The lowest-numbered host is connected to the highest-numbered port.
305
       Useful to verify that Mininet properly handles custom port numberings."""
306

    
307
    def build( self, k=2 ):
308
        "k: number of hosts"
309
        self.k = k
310
        switch = self.addSwitch( 's1' )
311
        for h in irange( 1, k ):
312
            host = self.addHost( 'h%s' % h )
313
            self.addLink( host, switch,
314
                          port1=0, port2=( k - h + 1 ) )
315

    
316

    
317
class LinearTopo( Topo ):
318
    "Linear topology of k switches, with n hosts per switch."
319

    
320
    def build( self, k=2, n=1, **opts):
321
        """k: number of switches
322
           n: number of hosts per switch"""
323
        self.k = k
324
        self.n = n
325

    
326
        if n == 1:
327
            genHostName = lambda i, j: 'h%s' % i
328
        else:
329
            genHostName = lambda i, j: 'h%ss%d' % ( j, i )
330

    
331
        lastSwitch = None
332
        for i in irange( 1, k ):
333
            # Add switch
334
            switch = self.addSwitch( 's%s' % i )
335
            # Add hosts to switch
336
            for j in irange( 1, n ):
337
                host = self.addHost( genHostName( i, j ) )
338
                self.addLink( host, switch )
339
            # Connect switch to previous
340
            if lastSwitch:
341
                self.addLink( switch, lastSwitch )
342
            lastSwitch = switch