diff --git a/bin/mn b/bin/mn index 567e213..79941f0 100755 --- a/bin/mn +++ b/bin/mn @@ -265,12 +265,14 @@ class MininetRunner( object ): if test == 'none': pass elif test == 'all': + mn.waitConnected() mn.start() mn.ping() mn.iperf() elif test == 'cli': CLI( mn ) elif test != 'build': + mn.waitConnected() getattr( mn, test )() if self.options.post: diff --git a/examples/linearbandwidth.py b/examples/linearbandwidth.py index 3fd06c7..dee5490 100755 --- a/examples/linearbandwidth.py +++ b/examples/linearbandwidth.py @@ -24,7 +24,7 @@ of switches, this example demonstrates: """ from mininet.net import Mininet -from mininet.node import UserSwitch, OVSKernelSwitch +from mininet.node import UserSwitch, OVSKernelSwitch, Controller from mininet.topo import Topo from mininet.log import lg from mininet.util import irange @@ -76,7 +76,7 @@ def linearBandwidthTest( lengths ): print "*** testing", datapath, "datapath" Switch = switches[ datapath ] results[ datapath ] = [] - net = Mininet( topo=topo, switch=Switch ) + net = Mininet( topo=topo, switch=Switch, controller=Controller, waitConnected=True ) net.start() print "*** testing basic connectivity" for n in lengths: diff --git a/mininet/net.py b/mininet/net.py index e970bbd..be0d287 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -90,11 +90,12 @@ import os import re import select import signal +import copy from time import sleep from itertools import chain, groupby from mininet.cli import CLI -from mininet.log import info, error, debug, output +from mininet.log import info, error, debug, output, warn from mininet.node import Host, OVSKernelSwitch, Controller from mininet.link import Link, Intf from mininet.util import quietRun, fixLimits, numCores, ensureRoot @@ -112,7 +113,7 @@ class Mininet( object ): build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8', inNamespace=False, autoSetMacs=False, autoStaticArp=False, autoPinCpus=False, - listenPort=None ): + listenPort=None, waitConnected=False ): """Create Mininet object. topo: Topo (topology) object or None switch: default Switch class @@ -148,6 +149,7 @@ class Mininet( object ): self.numCores = numCores() self.nextCore = 0 # next core for pinning hosts to CPUs self.listenPort = listenPort + self.waitConn = waitConnected self.hosts = [] self.switches = [] @@ -163,6 +165,34 @@ class Mininet( object ): if topo and build: self.build() + + def waitConnected( self, timeout=None ): + """wait for each switch to connect to a controller, + up to 5 seconds + timeout: time to wait, or None to wait indefinitely + returns: True if all switches are connected""" + info( '***waiting for switches to connect\n' ) + time = 0 + remaining = copy.copy( self.switches ) + while time < timeout or timeout == None: + connected = True + for switch in remaining: + if not switch.connected(): + connected = False + else: + remaining.remove( switch ) + if connected: + break + sleep( .5 ) + time += .5 + if time >= timeout and timeout is not None: + warn( 'Timed out after %d seconds\n' % time ) + for switch in self.switches: + if not switch.connected(): + warn( 'Warning: %s is not connected to a controller\n' + % switch.name ) + return connected + def addHost( self, name, cls=None, **params ): """Add host. name: name of host to add @@ -401,6 +431,8 @@ class Mininet( object ): info( switch.name + ' ') switch.start( self.controllers ) info( '\n' ) + if self.waitConn: + self.waitConnected() def stop( self ): "Stop the controller(s), switches and hosts"