From 4d381f0b58499dad18c4ac4cabc1e759db72efa9 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Sat, 19 Jul 2014 02:05:37 -0700 Subject: [PATCH 1/6] testing link stuff --- bin/mn | 1 - mininet/link.py | 26 ++++++++++++++++++++++---- mininet/net.py | 9 ++++++++- mininet/util.py | 8 +++++--- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/bin/mn b/bin/mn index 71c76ec..75166b7 100755 --- a/bin/mn +++ b/bin/mn @@ -272,7 +272,6 @@ class MininetRunner( object ): if self.options.post: CLI( mn, script=self.options.post ) - mn.stop() elapsed = float( time.time() - start ) info( 'completed in %0.3f seconds\n' % elapsed ) diff --git a/mininet/link.py b/mininet/link.py index be73d52..3e32538 100644 --- a/mininet/link.py +++ b/mininet/link.py @@ -86,6 +86,7 @@ class Intf( object ): def updateMAC( self ): "Return updated MAC address based on ifconfig" + #heres where we send the unecessary ifconfigs ifconfig = self.ifconfig() macs = self._macMatchRegex.findall( ifconfig ) self.mac = macs[ 0 ] if macs else None @@ -102,8 +103,13 @@ class Intf( object ): def isUp( self, setUp=False ): "Return whether interface is up" if setUp: - self.ifconfig( 'up' ) - return "UP" in self.ifconfig() + r = self.ifconfig( 'up' ) + if r: + return False + else: + return True + else: + return "UP" in self.ifconfig() def rename( self, newname ): "Rename interface" @@ -138,6 +144,16 @@ class Intf( object ): results[ name ] = result return result + def updateAddr( self ): + "instead of updating ip and mac separately, use one ifconfig call to do it simultaneously" + ifconfig = self.ifconfig() + print ifconfig + ips = self._ipMatchRegex.findall( ifconfig ) + macs = self._macMatchRegex.findall( ifconfig ) + self.ip = ips[ 0 ] if ips else None + self.mac = macs[ 0 ] if macs else None + return self.ip, self.mac + def config( self, mac=None, ip=None, ifconfig=None, up=True, **_params ): """Configure Node according to (optional) parameters: @@ -154,8 +170,10 @@ class Intf( object ): self.setParam( r, 'setIP', ip=ip ) self.setParam( r, 'isUp', up=up ) self.setParam( r, 'ifconfig', ifconfig=ifconfig ) - self.updateIP() - self.updateMAC() + #should combine these next two operations into one. this is unecessary + #self.updateAddr() + #self.updateIP() + #self.updateMAC() return r def delete( self ): diff --git a/mininet/net.py b/mininet/net.py index 8edaee3..135807b 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -100,6 +100,7 @@ from mininet.link import Link, Intf from mininet.util import quietRun, fixLimits, numCores, ensureRoot from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd from mininet.term import cleanUpScreens, makeTerms +from multiprocessing.pool import ThreadPool # Mininet version: should be consistent with README and LICENSE VERSION = "2.1.0+" @@ -157,6 +158,8 @@ class Mininet( object ): self.terms = [] # list of spawned xterm processes + self.pool = ThreadPool( 64 ) + Mininet.init() # Initialize Mininet if necessary self.built = False @@ -337,13 +340,17 @@ class Mininet( object ): info( switchName + ' ' ) info( '\n*** Adding links:\n' ) + # need to 'asynchronize' this too for srcName, dstName in topo.links(sort=True): src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ] params = topo.linkInfo( srcName, dstName ) srcPort, dstPort = topo.port( srcName, dstName ) self.addLink( src, dst, srcPort, dstPort, **params ) + #self.pool.apply( self.addLink, ( src, dst, srcPort, dstPort, params, ) ) + #self.pool.apply_async( self.addLink, args = ( src, dst )+ params, kwds = { 'Port1':srcPort, 'Port2':dstPort } ) info( '(%s, %s) ' % ( src.name, dst.name ) ) - + #self.pool.close() + #self.pool.join() info( '\n' ) def configureControlNetwork( self ): diff --git a/mininet/util.py b/mininet/util.py index 5cb27f4..b4d213a 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -82,6 +82,8 @@ def errRun( *cmd, **kwargs ): poller.register( popen.stdout, POLLIN ) fdtofile = { popen.stdout.fileno(): popen.stdout } outDone, errDone = False, True + #bookmark: rearrange this for aynch startup. shouldnt have to keep + # maybe we dont rearrange this. we really just need a method to call a command and NOT poll for output if popen.stderr: fdtofile[ popen.stderr.fileno() ] = popen.stderr poller.register( popen.stderr, POLLIN ) @@ -185,10 +187,10 @@ def moveIntfNoRetry( intf, dstNode, srcNode=None, printError=False ): intf = str( intf ) cmd = 'ip link set %s netns %s' % ( intf, dstNode.pid ) if srcNode: - srcNode.cmd( cmd ) + output = srcNode.cmd( cmd ) else: - quietRun( cmd ) - if ( ' %s:' % intf ) not in dstNode.cmd( 'ip link show', intf ): + output = quietRun( cmd ) + if output: if printError: error( '*** Error: moveIntf: ' + intf + ' not successfully moved to ' + dstNode.name + '\n' ) From 5cd6b553a52be57d170b5d48810460d1930298d0 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Tue, 22 Jul 2014 19:27:45 -0700 Subject: [PATCH 2/6] removed many of the commands being run to maximize startup performance --- mininet/link.py | 21 +++++++++++---------- mininet/net.py | 27 +++++++++++++++++++++++---- mininet/node.py | 12 ++++++------ mininet/util.py | 8 ++++++-- 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/mininet/link.py b/mininet/link.py index 3e32538..e9df6cc 100644 --- a/mininet/link.py +++ b/mininet/link.py @@ -32,7 +32,7 @@ class Intf( object ): "Basic interface object that can configure itself." - def __init__( self, name, node=None, port=None, link=None, **params ): + def __init__( self, name, node=None, port=None, link=None, mac=None, **params ): """name: interface name (e.g. h1-eth0) node: owning node (where this intf most likely lives) link: parent link if we're part of a link @@ -40,7 +40,10 @@ class Intf( object ): self.node = node self.name = name self.link = link - self.mac, self.ip, self.prefixLen = None, None, None + self.mac = mac + self.ip, self.prefixLen = None, None + if self.name == 'lo': + self.ip = '127.0.0.1' # Add to node (and move ourselves if necessary ) node.addIntf( self, port=port ) # Save params for future reference @@ -147,7 +150,6 @@ class Intf( object ): def updateAddr( self ): "instead of updating ip and mac separately, use one ifconfig call to do it simultaneously" ifconfig = self.ifconfig() - print ifconfig ips = self._ipMatchRegex.findall( ifconfig ) macs = self._macMatchRegex.findall( ifconfig ) self.ip = ips[ 0 ] if ips else None @@ -170,7 +172,6 @@ class Intf( object ): self.setParam( r, 'setIP', ip=ip ) self.setParam( r, 'isUp', up=up ) self.setParam( r, 'ifconfig', ifconfig=ifconfig ) - #should combine these next two operations into one. this is unecessary #self.updateAddr() #self.updateIP() #self.updateMAC() @@ -339,7 +340,7 @@ class Link( object ): Other types of links could be tunnels, link emulators, etc..""" def __init__( self, node1, node2, port1=None, port2=None, - intfName1=None, intfName2=None, + intfName1=None, intfName2=None, addr1=None, addr2=None, intf=Intf, cls1=None, cls2=None, params1=None, params2=None ): """Create veth link to another node, making two new interfaces. @@ -365,7 +366,7 @@ class Link( object ): if not intfName2: intfName2 = self.intfName( node2, port2 ) - self.makeIntfPair( intfName1, intfName2 ) + self.makeIntfPair( intfName1, intfName2, addr1, addr2 ) if not cls1: cls1 = intf @@ -377,9 +378,9 @@ class Link( object ): params2 = {} intf1 = cls1( name=intfName1, node=node1, port=port1, - link=self, **params1 ) + link=self, mac=addr1, **params1 ) intf2 = cls2( name=intfName2, node=node2, port=port2, - link=self, **params2 ) + link=self, mac=addr2, **params2 ) # All we are is dust in the wind, and our two interfaces self.intf1, self.intf2 = intf1, intf2 @@ -390,13 +391,13 @@ class Link( object ): return node.name + '-eth' + repr( n ) @classmethod - def makeIntfPair( cls, intf1, intf2 ): + def makeIntfPair( cls, intf1, intf2, addr1=None, addr2=None ): """Create pair of interfaces intf1: name of interface 1 intf2: name of interface 2 (override this class method [and possibly delete()] to change link type)""" - makeIntfPair( intf1, intf2 ) + makeIntfPair( intf1, intf2, addr1, addr2 ) def delete( self ): "Delete this link" diff --git a/mininet/net.py b/mininet/net.py index 4224b97..5be34f8 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -90,6 +90,7 @@ import os import re import select import signal +import random import copy from time import sleep from itertools import chain, groupby @@ -101,7 +102,7 @@ from mininet.link import Link, Intf from mininet.util import quietRun, fixLimits, numCores, ensureRoot from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd from mininet.term import cleanUpScreens, makeTerms -from multiprocessing.pool import ThreadPool +from multiprocessing import Process # Mininet version: should be consistent with README and LICENSE VERSION = "2.1.0+" @@ -160,7 +161,7 @@ class Mininet( object ): self.terms = [] # list of spawned xterm processes - self.pool = ThreadPool( 64 ) + #self.pool = Pool( 64 ) Mininet.init() # Initialize Mininet if necessary @@ -306,6 +307,21 @@ class Mininet( object ): "return (key,value) tuple list for every node in net" return zip( self.keys(), self.values() ) + def generateMac( self ): + newMac = True + while True: + macList = [ 0x00 ] + for i in xrange ( 0, 5 ): + macList.append( random.randint( 0x00, 0xff ) ) + mac = ':'.join( map(lambda x: "%02x" % x, macList ) ) + for node in self.switches + self.hosts: + for intf in node.ports: + if intf.mac == mac: + newMac = False + break + if newMac: + return mac + def addLink( self, node1, node2, port1=None, port2=None, cls=None, **params ): """"Add a link from node1 to node2 @@ -314,8 +330,12 @@ class Mininet( object ): port1: source port port2: dest port returns: link object""" + mac1 = self.generateMac() + mac2 = self.generateMac() defaults = { 'port1': port1, 'port2': port2, + 'addr1': mac1, + 'addr2': mac2, 'intf': self.intf } defaults.update( params ) if not cls: @@ -383,8 +403,7 @@ class Mininet( object ): params = topo.linkInfo( srcName, dstName ) srcPort, dstPort = topo.port( srcName, dstName ) self.addLink( src, dst, srcPort, dstPort, **params ) - #self.pool.apply( self.addLink, ( src, dst, srcPort, dstPort, params, ) ) - #self.pool.apply_async( self.addLink, args = ( src, dst )+ params, kwds = { 'Port1':srcPort, 'Port2':dstPort } ) + #self.pool.apply_async( self.addLink, ( src, dst, srcPort, dstPort, params ) ) info( '(%s, %s) ' % ( src.name, dst.name ) ) #self.pool.close() #self.pool.join() diff --git a/mininet/node.py b/mininet/node.py index d9052fa..bf1afb9 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -1082,11 +1082,9 @@ class OVSSwitch( Switch ): if self.inNamespace: raise Exception( 'OVS kernel switch does not work in a namespace' ) - # We should probably call config instead, but this - # requires some rethinking... - self.cmd( 'ifconfig lo up' ) # Annoyingly, --if-exists option seems not to work - self.cmd( 'ovs-vsctl del-br', self ) + self.sendCmd( 'ovs-vsctl del-br', self ) + self.waiting = False int( self.dpid, 16 ) # DPID must be a hex string # Interfaces and controllers intfs = ' '.join( '-- add-port %s %s ' % ( self, intf ) + @@ -1107,10 +1105,12 @@ class OVSSwitch( Switch ): '-- set-controller %s %s ' % ( self, clist ) ) # Construct ovs-vsctl commands for old versions of OVS else: - self.cmd( 'ovs-vsctl add-br', self ) + self.sendCmd( 'ovs-vsctl add-br', self ) + self.waiting = False for intf in self.intfList(): if not intf.IP(): - self.cmd( 'ovs-vsctl add-port', self, intf ) + self.sendCmd( 'ovs-vsctl add-port', self, intf ) + self.waiting = False cmd = ( 'ovs-vsctl set Bridge %s ' % self + 'other_config:datapath-id=%s ' % self.dpid + '-- set-fail-mode %s %s ' % ( self, self.failMode ) + diff --git a/mininet/util.py b/mininet/util.py index b4d213a..1a866fa 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -147,7 +147,7 @@ isShellBuiltin.builtIns = None # live in the root namespace and thus do not have to be # explicitly moved. -def makeIntfPair( intf1, intf2 ): +def makeIntfPair( intf1, intf2, addr1=None, addr2=None ): """Make a veth pair connecting intf1 and intf2. intf1: string, interface intf2: string, interface @@ -156,7 +156,11 @@ def makeIntfPair( intf1, intf2 ): quietRun( 'ip link del ' + intf1 ) quietRun( 'ip link del ' + intf2 ) # Create new pair - cmd = 'ip link add name ' + intf1 + ' type veth peer name ' + intf2 + if addr1 is None and addr2 is None: + cmd = 'ip link add name ' + intf1 + ' type veth peer name ' + intf2 + else: + cmd = ( 'ip link add name ' + intf1 + ' address ' + addr1 + + ' type veth peer name ' + intf2 + ' address ' + addr2 ) cmdOutput = quietRun( cmd ) if cmdOutput == '': return True From 6bc9d6848529367144a51ebbe149997668c00f61 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Tue, 22 Jul 2014 23:00:43 -0700 Subject: [PATCH 3/6] removed comments and cleaned up code. --- bin/mn | 3 ++- mininet/link.py | 29 ++++++++++++++--------------- mininet/net.py | 8 -------- mininet/util.py | 6 ++---- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/bin/mn b/bin/mn index fbe1f05..f96511c 100755 --- a/bin/mn +++ b/bin/mn @@ -281,7 +281,8 @@ class MininetRunner( object ): if self.options.post: CLI( mn, script=self.options.post ) - + mn.stop() + elapsed = float( time.time() - start ) info( 'completed in %0.3f seconds\n' % elapsed ) diff --git a/mininet/link.py b/mininet/link.py index e9df6cc..2e6e171 100644 --- a/mininet/link.py +++ b/mininet/link.py @@ -89,12 +89,22 @@ class Intf( object ): def updateMAC( self ): "Return updated MAC address based on ifconfig" - #heres where we send the unecessary ifconfigs ifconfig = self.ifconfig() macs = self._macMatchRegex.findall( ifconfig ) self.mac = macs[ 0 ] if macs else None return self.mac + def updateAddr( self ): + """Return IP address and MAC address based on ifconfig. + instead of updating ip and mac separately, + use one ifconfig call to do it simultaneously"""" + ifconfig = self.ifconfig() + ips = self._ipMatchRegex.findall( ifconfig ) + macs = self._macMatchRegex.findall( ifconfig ) + self.ip = ips[ 0 ] if ips else None + self.mac = macs[ 0 ] if macs else None + return self.ip, self.mac + def IP( self ): "Return IP address" return self.ip @@ -106,8 +116,9 @@ class Intf( object ): def isUp( self, setUp=False ): "Return whether interface is up" if setUp: - r = self.ifconfig( 'up' ) - if r: + cmdOutput = self.ifconfig( 'up' ) + if cmdOutput: + error( "Error setting %s up: %s " % ( self.name, cmdOutput ) return False else: return True @@ -147,15 +158,6 @@ class Intf( object ): results[ name ] = result return result - def updateAddr( self ): - "instead of updating ip and mac separately, use one ifconfig call to do it simultaneously" - ifconfig = self.ifconfig() - ips = self._ipMatchRegex.findall( ifconfig ) - macs = self._macMatchRegex.findall( ifconfig ) - self.ip = ips[ 0 ] if ips else None - self.mac = macs[ 0 ] if macs else None - return self.ip, self.mac - def config( self, mac=None, ip=None, ifconfig=None, up=True, **_params ): """Configure Node according to (optional) parameters: @@ -172,9 +174,6 @@ class Intf( object ): self.setParam( r, 'setIP', ip=ip ) self.setParam( r, 'isUp', up=up ) self.setParam( r, 'ifconfig', ifconfig=ifconfig ) - #self.updateAddr() - #self.updateIP() - #self.updateMAC() return r def delete( self ): diff --git a/mininet/net.py b/mininet/net.py index 5be34f8..30feb8f 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -102,7 +102,6 @@ from mininet.link import Link, Intf from mininet.util import quietRun, fixLimits, numCores, ensureRoot from mininet.util import macColonHex, ipStr, ipParse, netParse, ipAdd from mininet.term import cleanUpScreens, makeTerms -from multiprocessing import Process # Mininet version: should be consistent with README and LICENSE VERSION = "2.1.0+" @@ -161,15 +160,12 @@ class Mininet( object ): self.terms = [] # list of spawned xterm processes - #self.pool = Pool( 64 ) - Mininet.init() # Initialize Mininet if necessary self.built = False if topo and build: self.build() - def waitConnected( self, timeout=None, delay=.5 ): """wait for each switch to connect to a controller, up to 5 seconds @@ -397,16 +393,12 @@ class Mininet( object ): info( switchName + ' ' ) info( '\n*** Adding links:\n' ) - # need to 'asynchronize' this too for srcName, dstName in topo.links(sort=True): src, dst = self.nameToNode[ srcName ], self.nameToNode[ dstName ] params = topo.linkInfo( srcName, dstName ) srcPort, dstPort = topo.port( srcName, dstName ) self.addLink( src, dst, srcPort, dstPort, **params ) - #self.pool.apply_async( self.addLink, ( src, dst, srcPort, dstPort, params ) ) info( '(%s, %s) ' % ( src.name, dst.name ) ) - #self.pool.close() - #self.pool.join() info( '\n' ) def configureControlNetwork( self ): diff --git a/mininet/util.py b/mininet/util.py index 1a866fa..32160be 100644 --- a/mininet/util.py +++ b/mininet/util.py @@ -82,8 +82,6 @@ def errRun( *cmd, **kwargs ): poller.register( popen.stdout, POLLIN ) fdtofile = { popen.stdout.fileno(): popen.stdout } outDone, errDone = False, True - #bookmark: rearrange this for aynch startup. shouldnt have to keep - # maybe we dont rearrange this. we really just need a method to call a command and NOT poll for output if popen.stderr: fdtofile[ popen.stderr.fileno() ] = popen.stderr poller.register( popen.stderr, POLLIN ) @@ -191,9 +189,9 @@ def moveIntfNoRetry( intf, dstNode, srcNode=None, printError=False ): intf = str( intf ) cmd = 'ip link set %s netns %s' % ( intf, dstNode.pid ) if srcNode: - output = srcNode.cmd( cmd ) + cmdOutput = srcNode.cmd( cmd ) else: - output = quietRun( cmd ) + cmdOutput = quietRun( cmd ) if output: if printError: error( '*** Error: moveIntf: ' + intf + From f11dbe81d2d75e647a07798a44752e37d5b970c1 Mon Sep 17 00:00:00 2001 From: cody burkard Date: Thu, 24 Jul 2014 15:59:38 -0700 Subject: [PATCH 4/6] few small fixes to syntax errors --- bin/mn | 2 +- mininet/link.py | 4 ++-- mininet/net.py | 19 ++----------------- mininet/node.py | 1 - mininet/util.py | 22 ++++++++++++++++++++-- 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/bin/mn b/bin/mn index f96511c..edd4d98 100755 --- a/bin/mn +++ b/bin/mn @@ -282,7 +282,7 @@ class MininetRunner( object ): CLI( mn, script=self.options.post ) mn.stop() - + elapsed = float( time.time() - start ) info( 'completed in %0.3f seconds\n' % elapsed ) diff --git a/mininet/link.py b/mininet/link.py index 2e6e171..c7e5c72 100644 --- a/mininet/link.py +++ b/mininet/link.py @@ -97,7 +97,7 @@ class Intf( object ): def updateAddr( self ): """Return IP address and MAC address based on ifconfig. instead of updating ip and mac separately, - use one ifconfig call to do it simultaneously"""" + use one ifconfig call to do it simultaneously""" ifconfig = self.ifconfig() ips = self._ipMatchRegex.findall( ifconfig ) macs = self._macMatchRegex.findall( ifconfig ) @@ -118,7 +118,7 @@ class Intf( object ): if setUp: cmdOutput = self.ifconfig( 'up' ) if cmdOutput: - error( "Error setting %s up: %s " % ( self.name, cmdOutput ) + error( "Error setting %s up: %s " % ( self.name, cmdOutput ) ) return False else: return True diff --git a/mininet/net.py b/mininet/net.py index 30feb8f..3b822f4 100755 --- a/mininet/net.py +++ b/mininet/net.py @@ -303,21 +303,6 @@ class Mininet( object ): "return (key,value) tuple list for every node in net" return zip( self.keys(), self.values() ) - def generateMac( self ): - newMac = True - while True: - macList = [ 0x00 ] - for i in xrange ( 0, 5 ): - macList.append( random.randint( 0x00, 0xff ) ) - mac = ':'.join( map(lambda x: "%02x" % x, macList ) ) - for node in self.switches + self.hosts: - for intf in node.ports: - if intf.mac == mac: - newMac = False - break - if newMac: - return mac - def addLink( self, node1, node2, port1=None, port2=None, cls=None, **params ): """"Add a link from node1 to node2 @@ -326,8 +311,8 @@ class Mininet( object ): port1: source port port2: dest port returns: link object""" - mac1 = self.generateMac() - mac2 = self.generateMac() + mac1 = macColonHex( random.randint( 1, (2**24 - 1) ) ) + mac2 = macColonHex( random.randint( 1, (2**24 - 1) ) ) defaults = { 'port1': port1, 'port2': port2, 'addr1': mac1, diff --git a/mininet/node.py b/mininet/node.py index bf1afb9..92ffcc1 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -1186,7 +1186,6 @@ class IVSSwitch(Switch): args.append( self.opts ) logfile = '/tmp/ivs.%s.log' % self.name - self.cmd( 'ifconfig lo up' ) self.cmd( ' '.join(args) + ' >' + logfile + ' 2>&1 Date: Thu, 24 Jul 2014 16:30:48 -0700 Subject: [PATCH 5/6] adding old changes --- mininet/cli.py | 15 ++++++++----- mininet/node.py | 60 ++++++++++++++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/mininet/cli.py b/mininet/cli.py index b432f7c..42a4d3d 100644 --- a/mininet/cli.py +++ b/mininet/cli.py @@ -55,7 +55,7 @@ class CLI( Cmd ): Cmd.__init__( self ) info( '*** Starting CLI:\n' ) - # Setup history if readline is available + # Set up history if readline is available try: import readline except ImportError: @@ -77,7 +77,7 @@ class CLI( Cmd ): node.sendInt() node.monitor() if self.isatty(): - quietRun( 'stty sane' ) + quietRun( 'stty echo sane intr "^C"' ) self.cmdloop() break except KeyboardInterrupt: @@ -352,8 +352,7 @@ class CLI( Cmd ): for arg in rest ] rest = ' '.join( rest ) # Run cmd on node: - builtin = isShellBuiltin( first ) - node.sendCmd( rest, printPid=( not builtin ) ) + node.sendCmd( rest ) self.waitForNode( node ) else: error( '*** Unknown command: %s\n' % line ) @@ -361,7 +360,7 @@ class CLI( Cmd ): # pylint: enable-msg=R0201 def waitForNode( self, node ): - "Wait for a node to finish, and print its output." + "Wait for a node to finish, and print its output." # Pollers nodePoller = poll() nodePoller.register( node.stdout ) @@ -379,7 +378,7 @@ class CLI( Cmd ): if False and self.inputFile: key = self.inputFile.read( 1 ) if key is not '': - node.write(key) + node.write( key ) else: self.inputFile = None if isReadable( self.inPoller ): @@ -391,8 +390,12 @@ class CLI( Cmd ): if not node.waiting: break except KeyboardInterrupt: + # There is an at least one race condition here, since + # it's possible to interrupt ourselves after we've + # read data but before it has been printed. node.sendInt() + # Helper functions def isReadable( poller ): diff --git a/mininet/node.py b/mininet/node.py index 92ffcc1..48424e7 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -50,6 +50,7 @@ Future enhancements: """ import os +import pty import re import signal import select @@ -123,16 +124,22 @@ class Node( object ): return # mnexec: (c)lose descriptors, (d)etach from tty, # (p)rint pid, and run in (n)amespace - opts = '-cdp' + opts = '-cd' if self.inNamespace: opts += 'n' - # bash -m: enable job control + # bash -m: enable job control, i: force interactive # -s: pass $* to shell, and make process easy to find in ps - cmd = [ 'mnexec', opts, 'bash', '-ms', 'mininet:' + self.name ] - self.shell = Popen( cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT, - close_fds=True ) - self.stdin = self.shell.stdin - self.stdout = self.shell.stdout + # prompt is set to sentinel chr( 127 ) + os.environ[ 'PS1' ] = chr( 127 ) + cmd = [ 'mnexec', opts, 'bash', '--norc', '-mis', 'mininet:' + self.name ] + # Spawn a shell subprocess in a pseudo-tty, to disable buffering + # in the subprocess and insulate it from signals (e.g. SIGINT) + # received by the parent + master, slave = pty.openpty() + self.shell = Popen( cmd, stdin=slave, stdout=slave, stderr=slave, + close_fds=False ) + self.stdin = os.fdopen( master ) + self.stdout = self.stdin self.pid = self.shell.pid self.pollOut = select.poll() self.pollOut.register( self.stdout ) @@ -145,7 +152,14 @@ class Node( object ): self.lastCmd = None self.lastPid = None self.readbuf = '' + # Wait for prompt + while True: + data = self.read( 1024 ) + if data[ -1 ] == chr( 127 ): + break + self.pollOut.poll() self.waiting = False + self.cmd( 'stty -echo' ) def cleanup( self ): "Help python collect its garbage." @@ -224,36 +238,29 @@ class Node( object ): # Replace empty commands with something harmless cmd = 'echo -n' self.lastCmd = cmd - printPid = printPid and not isShellBuiltin( cmd ) - if len( cmd ) > 0 and cmd[ -1 ] == '&': - # print ^A{pid}\n{sentinel} - cmd += ' printf "\\001%d\n\\177" $! \n' - else: - # print sentinel - cmd += '; printf "\\177"' - if printPid and not isShellBuiltin( cmd ): + if printPid and not isShellBuiltin( cmd ): + if len( cmd ) > 0 and cmd[ -1 ] == '&': + # print ^A{pid}\n so monitor() can set lastPid + cmd += ' printf "\\001%d\n" $! \n' + else: cmd = 'mnexec -p ' + cmd self.write( cmd + '\n' ) self.lastPid = None self.waiting = True - def sendInt( self, sig=signal.SIGINT ): + def sendInt( self, intr=chr( 3 ) ): "Interrupt running command." - if self.lastPid: - try: - os.kill( self.lastPid, sig ) - except OSError: - pass + self.write( intr ) - def monitor( self, timeoutms=None ): + def monitor( self, timeoutms=None, findPid=True ): """Monitor and return the output of a command. Set self.waiting to False if command has completed. timeoutms: timeout in ms or None to wait indefinitely.""" self.waitReadable( timeoutms ) data = self.read( 1024 ) # Look for PID - marker = chr( 1 ) + r'\d+\n' - if chr( 1 ) in data: + marker = chr( 1 ) + r'\d+\r\n' + if findPid and chr( 1 ) in data: markers = re.findall( marker, data ) if markers: self.lastPid = int( markers[ 0 ][ 1: ] ) @@ -322,7 +329,10 @@ class Node( object ): # Shell requires a string, not a list! if defaults.get( 'shell', False ): cmd = ' '.join( cmd ) - return Popen( cmd, **defaults ) + old = signal.signal( signal.SIGINT, signal.SIG_IGN ) + popen = Popen( cmd, **defaults ) + signal.signal( signal.SIGINT, old ) + return popen def pexec( self, *args, **kwargs ): """Execute a command using popen From 859bfea502b41296ebf1ca1ce48350818f9cea8d Mon Sep 17 00:00:00 2001 From: cody burkard Date: Fri, 25 Jul 2014 03:41:13 -0700 Subject: [PATCH 6/6] fixed issue with regex matching --- mininet/node.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mininet/node.py b/mininet/node.py index 48424e7..0b435fc 100644 --- a/mininet/node.py +++ b/mininet/node.py @@ -261,6 +261,8 @@ class Node( object ): # Look for PID marker = chr( 1 ) + r'\d+\r\n' if findPid and chr( 1 ) in data: + while not re.findall( marker, data ): + data += self.read( 1024 ) markers = re.findall( marker, data ) if markers: self.lastPid = int( markers[ 0 ][ 1: ] )