9898
9999from mininet .cli import CLI
100100from mininet .log import info , error , debug , output , warn
101- from mininet .node import Host , OVSKernelSwitch , DefaultController , Controller
101+ from mininet .node import ( Node , Host , OVSKernelSwitch , DefaultController ,
102+ Controller )
102103from mininet .nodelib import NAT
103104from mininet .link import Link , Intf
104105from mininet .util import quietRun , fixLimits , numCores , ensureRoot
@@ -264,14 +265,24 @@ def addController( self, name='c0', controller=None, **params ):
264265 self .nameToNode [ name ] = controller_new
265266 return controller_new
266267
267- def addNAT ( self , name = 'nat0' , connect = True , inNamespace = False , ** params ):
268+ def addNAT ( self , name = 'nat0' , connect = True , inNamespace = False ,
269+ ** params ):
270+ """Add a NAT to the Mininet network
271+ name: name of NAT node
272+ connect: switch to connect to | True (s1) | None
273+ inNamespace: create in a network namespace
274+ params: other NAT node params, notably:
275+ ip: used as default gateway address"""
268276 nat = self .addHost ( name , cls = NAT , inNamespace = inNamespace ,
269277 subnet = self .ipBase , ** params )
270278 # find first switch and create link
271279 if connect :
272- # connect the nat to the first switch
280+ if not isinstance ( connect , Node ):
281+ # Use first switch if not specified
282+ connect = self .switches [ 0 ]
283+ # Connect the nat to the switch
273284 self .addLink ( nat , self .switches [ 0 ] )
274- # set the default route on hosts
285+ # Set the default route on hosts
275286 natIP = nat .params [ 'ip' ].split ('/' )[ 0 ]
276287 for host in self .hosts :
277288 if host .inNamespace :
@@ -486,7 +497,8 @@ def stop( self ):
486497 info ( '*** Stopping %i terms\n ' % len ( self .terms ) )
487498 self .stopXterms ()
488499 info ( '*** Stopping %i switches\n ' % len ( self .switches ) )
489- for swclass , switches in groupby ( sorted ( self .switches , key = type ), type ):
500+ for swclass , switches in groupby (
501+ sorted ( self .switches , key = type ), type ):
490502 if hasattr ( swclass , 'batchShutdown' ):
491503 swclass .batchShutdown ( switches )
492504 for switch in self .switches :
@@ -521,13 +533,13 @@ def monitor( self, hosts=None, timeoutms=-1 ):
521533 if hosts is None :
522534 hosts = self .hosts
523535 poller = select .poll ()
524- Node = hosts [ 0 ] # so we can call class method fdToNode
536+ h1 = hosts [ 0 ] # so we can call class method fdToNode
525537 for host in hosts :
526538 poller .register ( host .stdout )
527539 while True :
528540 ready = poller .poll ( timeoutms )
529541 for fd , event in ready :
530- host = Node .fdToNode ( fd )
542+ host = h1 .fdToNode ( fd )
531543 if event & select .POLLIN :
532544 line = host .readline ()
533545 if line is not None :
@@ -574,7 +586,8 @@ def ping( self, hosts=None, timeout=None ):
574586 if timeout :
575587 opts = '-W %s' % timeout
576588 if dest .intfs :
577- result = node .cmd ( 'ping -c1 %s %s' % (opts , dest .IP ()) )
589+ result = node .cmd ( 'ping -c1 %s %s' %
590+ (opts , dest .IP ()) )
578591 sent , received = self ._parsePing ( result )
579592 else :
580593 sent , received = 0 , 0
@@ -699,13 +712,13 @@ def _parseIperf( iperfOutput ):
699712
700713 # XXX This should be cleaned up
701714
702- def iperf ( self , hosts = None , l4Type = 'TCP' , udpBw = '10M' , format = None ,
715+ def iperf ( self , hosts = None , l4Type = 'TCP' , udpBw = '10M' , fmt = None ,
703716 seconds = 5 ):
704717 """Run iperf between two hosts.
705- hosts: list of hosts; if None, uses opposite hosts
718+ hosts: list of hosts; if None, uses first and last hosts
706719 l4Type: string, one of [ TCP, UDP ]
707720 udpBw: bandwidth target for UDP test
708- format : iperf format argument if any
721+ fmt : iperf format argument if any
709722 seconds: iperf time to transmit
710723 returns: two-element array of [ server, client ] speeds
711724 note: send() is buffered, so client rate can be much higher than
@@ -729,8 +742,8 @@ def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', format=None,
729742 bwArgs = '-b ' + udpBw + ' '
730743 elif l4Type != 'TCP' :
731744 raise Exception ( 'Unexpected l4 type: %s' % l4Type )
732- if format :
733- iperfArgs += '-f %s ' % format
745+ if fmt :
746+ iperfArgs += '-f %s ' % fmt
734747 server .sendCmd ( iperfArgs + '-s' , printPid = True )
735748 servout = ''
736749 while server .lastPid is None :
@@ -755,7 +768,7 @@ def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', format=None,
755768 def runCpuLimitTest ( self , cpu , duration = 5 ):
756769 """run CPU limit test with 'while true' processes.
757770 cpu: desired CPU fraction of each host
758- duration: test duration in seconds
771+ duration: test duration in seconds (integer)
759772 returns a single list of measured CPU fractions as floats.
760773 """
761774 cores = int ( quietRun ( 'nproc' ) )
@@ -776,12 +789,14 @@ def runCpuLimitTest( self, cpu, duration=5 ):
776789 # get the initial cpu time for each host
777790 for host in hosts :
778791 outputs [ host ] = []
779- with open ( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' % host , 'r' ) as f :
792+ with open ( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
793+ host , 'r' ) as f :
780794 time [ host ] = float ( f .read () )
781- for _ in range ( 5 ):
795+ for _ in range ( duration ):
782796 sleep ( 1 )
783797 for host in hosts :
784- with open ( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' % host , 'r' ) as f :
798+ with open ( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
799+ host , 'r' ) as f :
785800 readTime = float ( f .read () )
786801 outputs [ host ].append ( ( ( readTime - time [ host ] )
787802 / 1000000000 ) / cores * 100 )
0 commit comments