Revision d44a5843 mininet/net.py

View differences:

mininet/net.py
90 90

  
91 91
from mininet.cli import CLI
92 92
from mininet.log import info, error, debug
93
from mininet.node import Host, KernelSwitch, OVSKernelSwitch, Controller
93
from mininet.node import Host, UserSwitch, KernelSwitch, Controller
94 94
from mininet.node import ControllerParams
95 95
from mininet.util import quietRun, fixLimits
96
from mininet.util import createLink, macColonHex
96
from mininet.util import createLink, macColonHex, ipStr, ipParse
97 97
from mininet.xterm import cleanUpScreens, makeXterms
98 98

  
99 99
DATAPATHS = [ 'kernel' ] #[ 'user', 'kernel' ]
......
130 130
           xterms: if build now, spawn xterms?
131 131
           cleanup: if build now, cleanup before creating?
132 132
           inNamespace: spawn switches and controller in net namespaces?
133
           autoSetMacs: set MAC addrs to DPIDs?
133
           autoSetMacs: set MAC addrs from topo?
134 134
           autoStaticArp: set all-pairs static MAC addrs?"""
135 135
        self.switch = switch
136 136
        self.host = host
......
151 151
        self.dps = 0 # number of created kernel datapaths
152 152
        self.terms = [] # list of spawned xterm processes
153 153

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

  
157 157
    def addHost( self, name, mac=None, ip=None ):
158 158
        """Add host.
......
165 165
        self.nameToNode[ name ] = host
166 166
        return host
167 167

  
168
    def addSwitch( self, name, mac=None ):
168
    def addSwitch( self, name, mac=None, ip=None ):
169 169
        """Add switch.
170 170
           name: name of switch to add
171 171
           mac: default MAC address for kernel/OVS switch intf 0
172 172
           returns: added switch"""
173
        if self.switch is KernelSwitch or self.switch is OVSKernelSwitch:
174
            sw = self.switch( name, dp=self.dps, defaultMAC=mac )
175
            self.dps += 1
173
        if self.switch == UserSwitch:
174
            sw = self.switch( name, defaultMAC=mac, defaultIP=ip,
175
                inNamespace=self.inNamespace )
176 176
        else:
177
            sw = self.switch( name )
177
            sw = self.switch( name, defaultMAC=mac, defaultIP=ip, dp=self.dps,
178
                inNamespace=self.inNamespace )
179
        self.dps += 1
178 180
        self.switches.append( sw )
179 181
        self.nameToNode[ name ] = sw
180 182
        return sw
......
206 208
    #    useful for people who wish to simulate a separate control
207 209
    #    network (since real networks may need one!)
208 210

  
209
    def _configureControlNetwork( self ):
211
    def configureControlNetwork( self ):
210 212
        "Configure control network."
211
        self._configureRoutedControlNetwork()
213
        self.configureRoutedControlNetwork()
214

  
215
    # We still need to figure out the right way to pass
216
    # in the control network location.
212 217

  
213
    def _configureRoutedControlNetwork( self ):
218
    def configureRoutedControlNetwork( self, ip='192.168.123.1',
219
        prefixLen=16 ):
214 220
        """Configure a routed control network on controller and switches.
215 221
           For use with the user datapath only right now.
216
           TODO( brandonh ) test this code!
217 222
           """
218
        # params were: controller, switches, ips
219

  
220 223
        controller = self.controllers[ 0 ]
221
        info( '%s <-> ' % controller.name )
224
        info( controller.name + ' <->' )
225
        cip = ip
226
        snum = ipParse( ip )
222 227
        for switch in self.switches:
223
            info( '%s ' % switch.name )
224
            sip = switch.defaultIP
225
            sintf = switch.intfs[ 0 ]
226
            node, cintf = switch.connection[ sintf ]
227
            if node != controller:
228
                error( '*** Error: switch %s not connected to correct'
229
                         'controller' %
230
                         switch.name )
231
                exit( 1 )
232
            controller.setIP( cintf, self.cparams.ip, self.cparams.prefixLen )
233
            switch.setIP( sintf, sip, self.cparams.prefixLen )
228
            info( ' ' + switch.name )
229
            sintf, cintf = createLink( switch, controller )
230
            snum += 1
231
            while snum & 0xff in [ 0, 255 ]:
232
                snum += 1
233
            sip = ipStr( snum )
234
            controller.setIP( cintf, cip, prefixLen )
235
            switch.setIP( sintf, sip, prefixLen )
234 236
            controller.setHostRoute( sip, cintf )
235
            switch.setHostRoute( self.cparams.ip, sintf )
237
            switch.setHostRoute( cip, sintf )
236 238
        info( '\n' )
237 239
        info( '*** Testing control network\n' )
238
        while not controller.intfIsUp( controller.intfs[ 0 ] ):
239
            info( '*** Waiting for %s to come up\n',
240
                controller.intfs[ 0 ] )
240
        while not controller.intfIsUp( cintf ):
241
            info( '*** Waiting for', cintf, 'to come up\n' )
241 242
            sleep( 1 )
242 243
        for switch in self.switches:
243
            while not switch.intfIsUp( switch.intfs[ 0 ] ):
244
                info( '*** Waiting for %s to come up\n' %
245
                    switch.intfs[ 0 ] )
244
            while not switch.intfIsUp( sintf ):
245
                info( '*** Waiting for', sintf, 'to come up\n' )
246 246
                sleep( 1 )
247 247
            if self.ping( hosts=[ switch, controller ] ) != 0:
248 248
                error( '*** Error: control network test failed\n' )
......
265 265
        """Build mininet from a topology object
266 266
           At the end of this function, everything should be connected
267 267
           and up."""
268

  
269
        def addNode( prefix, addMethod, nodeId ):
270
            "Add a host or a switch."
271
            name = prefix + topo.name( nodeId )
272
            mac = macColonHex( nodeId ) if self.setMacs else None
273
            ip = topo.ip( nodeId )
274
            node = addMethod( name, mac=mac, ip=ip )
275
            self.idToNode[ nodeId ] = node
276
            info( name + ' ' )
277

  
278
        # Possibly we should clean up here and/or validate
279
        # the topo
268 280
        if self.cleanup:
269
            pass # cleanup
270
        # validate topo?
281
            pass
282

  
271 283
        info( '*** Adding controller\n' )
272 284
        self.addController( self.controller )
273 285
        info( '*** Creating network\n' )
274 286
        info( '*** Adding hosts:\n' )
275 287
        for hostId in sorted( topo.hosts() ):
276
            name = 'h' + topo.name( hostId )
277
            mac = macColonHex( hostId ) if self.setMacs else None
278
            ip = topo.ip( hostId )
279
            host = self.addHost( name, ip=ip, mac=mac )
280
            self.idToNode[ hostId ] = host
281
            info( name + ' ' )
288
            addNode( 'h', self.addHost, hostId )
282 289
        info( '\n*** Adding switches:\n' )
283 290
        for switchId in sorted( topo.switches() ):
284
            name = 's' + topo.name( switchId )
285
            mac = macColonHex( switchId) if self.setMacs else None
286
            switch = self.addSwitch( name, mac=mac )
287
            self.idToNode[ switchId ] = switch
288
            info( name + ' ' )
291
            addNode( 's', self.addSwitch, switchId )
289 292
        info( '\n*** Adding edges:\n' )
290 293
        for srcId, dstId in sorted( topo.edges() ):
291 294
            src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ]
292 295
            srcPort, dstPort = topo.port( srcId, dstId )
293
            createLink( src, srcPort, dst, dstPort )
296
            createLink( src, dst, srcPort, dstPort )
294 297
            info( '(%s, %s) ' % ( src.name, dst.name ) )
295 298
        info( '\n' )
296 299

  
300
    def build( self ):
301
        "Build mininet."
302
        if self.topo:
303
            self.buildFromTopo( self.topo )
297 304
        if self.inNamespace:
298 305
            info( '*** Configuring control network\n' )
299
            self._configureControlNetwork()
300

  
306
            self.configureControlNetwork()
301 307
        info( '*** Configuring hosts\n' )
302 308
        self.configHosts()
303

  
304 309
        if self.xterms:
305 310
            self.startXterms()
306 311
        if self.autoSetMacs:
......
391 396
        """Ping between all specified hosts.
392 397
           hosts: list of hosts
393 398
           returns: ploss packet loss percentage"""
394
        #self.start()
395
        # check if running - only then, start?
399
        # should we check if running?
396 400
        packets = 0
397 401
        lost = 0
398 402
        ploss = None

Also available in: Unified diff