Revision b1ec912d mininet/net.py

View differences:

mininet/net.py
98 98

  
99 99
from mininet.cli import CLI
100 100
from 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 )
102 103
from mininet.nodelib import NAT
103 104
from mininet.link import Link, Intf
104 105
from mininet.util import quietRun, fixLimits, numCores, ensureRoot
......
264 265
            self.nameToNode[ name ] = controller_new
265 266
        return controller_new
266 267

  
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"""
268 276
        nat = self.addHost( name, cls=NAT, inNamespace=inNamespace,
269 277
                            subnet=self.ipBase, **params )
270 278
        # find first switch and create link
271 279
        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
273 284
            self.addLink( nat, self.switches[ 0 ] )
274
            # set the default route on hosts
285
            # Set the default route on hosts
275 286
            natIP = nat.params[ 'ip' ].split('/')[ 0 ]
276 287
            for host in self.hosts:
277 288
                if host.inNamespace:
......
486 497
            info( '*** Stopping %i terms\n' % len( self.terms ) )
487 498
            self.stopXterms()
488 499
        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 ):
490 502
            if hasattr( swclass, 'batchShutdown' ):
491 503
                swclass.batchShutdown( switches )
492 504
        for switch in self.switches:
......
521 533
        if hosts is None:
522 534
            hosts = self.hosts
523 535
        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
525 537
        for host in hosts:
526 538
            poller.register( host.stdout )
527 539
        while True:
528 540
            ready = poller.poll( timeoutms )
529 541
            for fd, event in ready:
530
                host = Node.fdToNode( fd )
542
                host = h1.fdToNode( fd )
531 543
                if event & select.POLLIN:
532 544
                    line = host.readline()
533 545
                    if line is not None:
......
574 586
                    if timeout:
575 587
                        opts = '-W %s' % timeout
576 588
                    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()) )
578 591
                        sent, received = self._parsePing( result )
579 592
                    else:
580 593
                        sent, received = 0, 0
......
699 712

  
700 713
    # XXX This should be cleaned up
701 714

  
702
    def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', format=None,
715
    def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', fmt=None,
703 716
               seconds=5):
704 717
        """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
706 719
           l4Type: string, one of [ TCP, UDP ]
707 720
           udpBw: bandwidth target for UDP test
708
           format: iperf format argument if any
721
           fmt: iperf format argument if any
709 722
           seconds: iperf time to transmit
710 723
           returns: two-element array of [ server, client ] speeds
711 724
           note: send() is buffered, so client rate can be much higher than
......
729 742
            bwArgs = '-b ' + udpBw + ' '
730 743
        elif l4Type != 'TCP':
731 744
            raise Exception( 'Unexpected l4 type: %s' % l4Type )
732
        if format:
733
          iperfArgs += '-f %s ' %format
745
        if fmt:
746
            iperfArgs += '-f %s ' % fmt
734 747
        server.sendCmd( iperfArgs + '-s', printPid=True )
735 748
        servout = ''
736 749
        while server.lastPid is None:
......
755 768
    def runCpuLimitTest( self, cpu, duration=5 ):
756 769
        """run CPU limit test with 'while true' processes.
757 770
        cpu: desired CPU fraction of each host
758
        duration: test duration in seconds
771
        duration: test duration in seconds (integer)
759 772
        returns a single list of measured CPU fractions as floats.
760 773
        """
761 774
        cores = int( quietRun( 'nproc' ) )
......
776 789
        # get the initial cpu time for each host
777 790
        for host in hosts:
778 791
            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:
780 794
                time[ host ] = float( f.read() )
781
        for _ in range( 5 ):
795
        for _ in range( duration ):
782 796
            sleep( 1 )
783 797
            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:
785 800
                    readTime = float( f.read() )
786 801
                outputs[ host ].append( ( ( readTime - time[ host ] )
787 802
                                        / 1000000000 ) / cores * 100 )

Also available in: Unified diff