Revision 5a8bb489 mininet/net.py

View differences:

mininet/net.py
97 97
from mininet.node import Host, OVSKernelSwitch, Controller
98 98
from mininet.link import Link
99 99
from mininet.util import quietRun, fixLimits
100
from mininet.util import macColonHex, ipStr, ipParse, netParse
100
from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd
101 101
from mininet.term import cleanUpScreens, makeTerms
102 102

  
103 103
class Mininet( object ):
......
131 131
        self.link = link
132 132
        self.intf = intf
133 133
        self.ipBase = ipBase
134
        self.ipBaseNum, self.prefixLen = netParse( self.ipBase )
135
        self.nextIP = 1  # start for address allocation
134 136
        self.inNamespace = inNamespace
135 137
        self.xterms = xterms
136 138
        self.cleanup = cleanup
......
143 145
        self.controllers = []
144 146

  
145 147
        self.nameToNode = {}  # name to Node (Host/Switch) objects
146
        self.idToNode = {}  # dpid to Node (Host/Switch) objects
147 148

  
148 149
        self.terms = []  # list of spawned xterm processes
149 150

  
......
153 154
        if topo and build:
154 155
            self.build()
155 156

  
156
    # BL Note:
157
    # The specific items for host/switch/etc. should probably be
158
    # handled in the node classes rather than here!!
159

  
160
    def addHost( self, name, host=None, **params ):
157
    def addHost( self, name, cls=None, **params ):
161 158
        """Add host.
162 159
           name: name of host to add
163
           host: custom host constructor (optional)
160
           cls: custom host class/constructor (optional)
164 161
           params: parameters for host
165 162
           returns: added host"""
166
        if not host:
167
            host = self.host
168
        h = host( name, **params)
163
        # Default IP and MAC addresses
164
        defaults = { 'ip': ipAdd( self.nextIP,
165
                                  ipBaseNum=self.ipBaseNum,
166
                                  prefixLen=self.prefixLen ) }
167
        if self.autoSetMacs:
168
            defaults[ 'mac'] = macColonHex( self.nextIP )
169
        self.nextIP += 1
170
        defaults.update( params )
171
        if not cls:
172
            cls  = self.host
173
        h = cls( name, **defaults )
169 174
        self.hosts.append( h )
170 175
        self.nameToNode[ name ] = h
171 176
        return h
172 177

  
173
    def addSwitch( self, name, switch=None, **params ):
178
    def addSwitch( self, name, cls=None, **params ):
174 179
        """Add switch.
175 180
           name: name of switch to add
176
           switch: custom switch constructor (optional)
181
           cls: custom switch class/constructor (optional)
177 182
           returns: added switch
178 183
           side effect: increments listenPort ivar ."""
179 184
        defaults = { 'listenPort': self.listenPort,
180 185
                     'inNamespace': self.inNamespace }
181 186
        defaults.update( params )
182
        if not switch:
183
            switch = self.switch
184
        sw = self.switch( name, **defaults )
187
        if not cls:
188
            cls = self.switch
189
        sw = cls( name, **defaults )
185 190
        if not self.inNamespace and self.listenPort:
186 191
            self.listenPort += 1
187 192
        self.switches.append( sw )
......
199 204
            self.nameToNode[ name ] = controller_new
200 205
        return controller_new
201 206

  
207
    def addLink( self, src, dst, srcPort=None, dstPort=None, 
208
                 cls=None, **params ):
209
        "Add a link from topo"
210
        if self.intf and not 'intf' in params:
211
            params[ 'intf' ] = self.intf
212
        if not cls:
213
            cls = self.link
214
        return cls( src, dst, srcPort, dstPort, **params )
215

  
202 216
    def configHosts( self ):
203 217
        "Configure a set of hosts."
204 218
        for host in self.hosts:
......
215 229
           At the end of this function, everything should be connected
216 230
           and up."""
217 231

  
218
        ipBaseNum, prefixLen = netParse( self.ipBase )
219

  
220
        if not topo:
221
            topo = self.topo()
222

  
223
        def addNode( prefix, addMethod, nodeId ):
224
            "Add a host or a switch from topo"
225
            name = prefix + topo.name( nodeId )
226
            ni = topo.nodeInfo( nodeId )
227
            # Default IP and MAC addresses
228
            defaults = { 'ip': topo.ip( nodeId,
229
                                        ipBaseNum=ipBaseNum,
230
                                        prefixLen=prefixLen ) }
231
            if self.autoSetMacs:
232
                defaults[ 'mac'] = macColonHex( nodeId )
233
            defaults.update( ni.params )
234
            node = addMethod( name, cls=ni.cls, **defaults )
235
            self.idToNode[ nodeId ] = node
236
            info( name + ' ' )
237

  
238
        def addLink( srcId, dstId, link=None ):
239
            "Add a link from topo"
240
            src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ]
241
            srcPort, dstPort = topo.port( srcId, dstId )
242
            ei = topo.edgeInfo( srcId, dstId )
243
            link = getattr( ei, 'cls', link )
244
            params = ei.params
245
            if self.intf and not 'intf' in params:
246
                params[ 'intf' ] = self.intf
247
            if not link:
248
                link = self.link
249
            info( '(%s, %s) ' % ( src.name, dst.name ) )
250
            link( src, dst, srcPort, dstPort, **params )
251

  
252 232
        # Possibly we should clean up here and/or validate
253 233
        # the topo
254 234
        if self.cleanup:
......
262 242
            self.addController( 'c0' )
263 243

  
264 244
        info( '*** Adding hosts:\n' )
265
        for hostId in sorted( topo.hosts() ):
266
            addNode( 'h', self.addHost, hostId )
245
        for hostName in topo.hosts():
246
            self.addHost( hostName, **topo.nodeInfo( hostName ) )
247
            info( hostName + ' ' )
267 248

  
268 249
        info( '\n*** Adding switches:\n' )
269
        for switchId in sorted( topo.switches() ):
270
            addNode( 's', self.addSwitch, switchId )
250
        for switchName in topo.switches():
251
            self.addSwitch( switchName, **topo.nodeInfo( switchName) )
252
            info( switchName + ' ' )
271 253

  
272 254
        info( '\n*** Adding links:\n' )
273
        for srcId, dstId in sorted( topo.edges() ):
274
            addLink( srcId, dstId )
255
        for srcName, dstName in topo.links(sort=True):
256
            src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ]
257
            srcPort, dstPort = topo.port( srcName, dstName )
258
            self.addLink( src, dst, srcPort, dstPort,
259
                          **topo.linkInfo( srcName, dstName ) )
260
            info( '(%s, %s) ' % ( src.name, dst.name ) )
275 261

  
276 262
        info( '\n' )
277 263

  

Also available in: Unified diff