Statistics
| Branch: | Tag: | Revision:

mininet / mininet / net.py @ 6a69c3c7

History | View | Annotate | Download (36 KB)

1
"""
2

3
    Mininet: A simple networking testbed for OpenFlow/SDN!
4

5
author: Bob Lantz (rlantz@cs.stanford.edu)
6
author: Brandon Heller (brandonh@stanford.edu)
7

8
Mininet creates scalable OpenFlow test networks by using
9
process-based virtualization and network namespaces.
10

11
Simulated hosts are created as processes in separate network
12
namespaces. This allows a complete OpenFlow network to be simulated on
13
top of a single Linux kernel.
14

15
Each host has:
16

17
A virtual console (pipes to a shell)
18
A virtual interfaces (half of a veth pair)
19
A parent shell (and possibly some child processes) in a namespace
20

21
Hosts have a network interface which is configured via ifconfig/ip
22
link/etc.
23

24
This version supports both the kernel and user space datapaths
25
from the OpenFlow reference implementation (openflowswitch.org)
26
as well as OpenVSwitch (openvswitch.org.)
27

28
In kernel datapath mode, the controller and switches are simply
29
processes in the root namespace.
30

31
Kernel OpenFlow datapaths are instantiated using dpctl(8), and are
32
attached to the one side of a veth pair; the other side resides in the
33
host namespace. In this mode, switch processes can simply connect to the
34
controller via the loopback interface.
35

36
In user datapath mode, the controller and switches can be full-service
37
nodes that live in their own network namespaces and have management
38
interfaces and IP addresses on a control network (e.g. 192.168.123.1,
39
currently routed although it could be bridged.)
40

41
In addition to a management interface, user mode switches also have
42
several switch interfaces, halves of veth pairs whose other halves
43
reside in the host nodes that the switches are connected to.
44

45
Consistent, straightforward naming is important in order to easily
46
identify hosts, switches and controllers, both from the CLI and
47
from program code. Interfaces are named to make it easy to identify
48
which interfaces belong to which node.
49

50
The basic naming scheme is as follows:
51

52
    Host nodes are named h1-hN
53
    Switch nodes are named s1-sN
54
    Controller nodes are named c0-cN
55
    Interfaces are named {nodename}-eth0 .. {nodename}-ethN
56

57
Note: If the network topology is created using mininet.topo, then
58
node numbers are unique among hosts and switches (e.g. we have
59
h1..hN and SN..SN+M) and also correspond to their default IP addresses
60
of 10.x.y.z/8 where x.y.z is the base-256 representation of N for
61
hN. This mapping allows easy determination of a node's IP
62
address from its name, e.g. h1 -> 10.0.0.1, h257 -> 10.0.1.1.
63

64
Note also that 10.0.0.1 can often be written as 10.1 for short, e.g.
65
"ping 10.1" is equivalent to "ping 10.0.0.1".
66

67
Currently we wrap the entire network in a 'mininet' object, which
68
constructs a simulated network based on a network topology created
69
using a topology object (e.g. LinearTopo) from mininet.topo or
70
mininet.topolib, and a Controller which the switches will connect
71
to. Several configuration options are provided for functions such as
72
automatically setting MAC addresses, populating the ARP table, or
73
even running a set of terminals to allow direct interaction with nodes.
74

75
After the network is created, it can be started using start(), and a
76
variety of useful tasks maybe performed, including basic connectivity
77
and bandwidth tests and running the mininet CLI.
78

79
Once the network is up and running, test code can easily get access
80
to host and switch objects which can then be used for arbitrary
81
experiments, typically involving running a series of commands on the
82
hosts.
83

84
After all desired tests or activities have been completed, the stop()
85
method may be called to shut down the network.
86

87
"""
88

    
89
import os
90
import re
91
import select
92
import signal
93
import random
94

    
95
from time import sleep
96
from itertools import chain, groupby
97
from math import ceil
98

    
99
from mininet.cli import CLI
100
from mininet.log import info, error, debug, output, warn
101
from mininet.node import ( Node, Host, OVSKernelSwitch, DefaultController,
102
                           Controller )
103
from mininet.nodelib import NAT
104
from mininet.link import Link, Intf
105
from mininet.util import ( quietRun, fixLimits, numCores, ensureRoot,
106
                           macColonHex, ipStr, ipParse, netParse, ipAdd,
107
                           waitListening )
108
from mininet.term import cleanUpScreens, makeTerms
109

    
110
# Mininet version: should be consistent with README and LICENSE
111
VERSION = "2.2.1"
112

    
113
class Mininet( object ):
114
    "Network emulation with hosts spawned in network namespaces."
115

    
116
    def __init__( self, topo=None, switch=OVSKernelSwitch, host=Host,
117
                  controller=DefaultController, link=Link, intf=Intf,
118
                  build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8',
119
                  inNamespace=False,
120
                  autoSetMacs=False, autoStaticArp=False, autoPinCpus=False,
121
                  listenPort=None, waitConnected=False ):
122
        """Create Mininet object.
123
           topo: Topo (topology) object or None
124
           switch: default Switch class
125
           host: default Host class/constructor
126
           controller: default Controller class/constructor
127
           link: default Link class/constructor
128
           intf: default Intf class/constructor
129
           ipBase: base IP address for hosts,
130
           build: build now from topo?
131
           xterms: if build now, spawn xterms?
132
           cleanup: if build now, cleanup before creating?
133
           inNamespace: spawn switches and controller in net namespaces?
134
           autoSetMacs: set MAC addrs automatically like IP addresses?
135
           autoStaticArp: set all-pairs static MAC addrs?
136
           autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)?
137
           listenPort: base listening port to open; will be incremented for
138
               each additional switch in the net if inNamespace=False"""
139
        self.topo = topo
140
        self.switch = switch
141
        self.host = host
142
        self.controller = controller
143
        self.link = link
144
        self.intf = intf
145
        self.ipBase = ipBase
146
        self.ipBaseNum, self.prefixLen = netParse( self.ipBase )
147
        self.nextIP = 1  # start for address allocation
148
        self.inNamespace = inNamespace
149
        self.xterms = xterms
150
        self.cleanup = cleanup
151
        self.autoSetMacs = autoSetMacs
152
        self.autoStaticArp = autoStaticArp
153
        self.autoPinCpus = autoPinCpus
154
        self.numCores = numCores()
155
        self.nextCore = 0  # next core for pinning hosts to CPUs
156
        self.listenPort = listenPort
157
        self.waitConn = waitConnected
158

    
159
        self.hosts = []
160
        self.switches = []
161
        self.controllers = []
162
        self.links = []
163

    
164
        self.nameToNode = {}  # name to Node (Host/Switch) objects
165

    
166
        self.terms = []  # list of spawned xterm processes
167

    
168
        Mininet.init()  # Initialize Mininet if necessary
169

    
170
        self.built = False
171
        if topo and build:
172
            self.build()
173

    
174
    def waitConnected( self, timeout=None, delay=.5 ):
175
        """wait for each switch to connect to a controller,
176
           up to 5 seconds
177
           timeout: time to wait, or None to wait indefinitely
178
           delay: seconds to sleep per iteration
179
           returns: True if all switches are connected"""
180
        info( '*** Waiting for switches to connect\n' )
181
        time = 0
182
        remaining = list( self.switches )
183
        while True:
184
            for switch in tuple( remaining ):
185
                if switch.connected():
186
                    info( '%s ' % switch )
187
                    remaining.remove( switch )
188
            if not remaining:
189
                info( '\n' )
190
                return True
191
            if time > timeout and timeout is not None:
192
                break
193
            sleep( delay )
194
            time += delay
195
        warn( 'Timed out after %d seconds\n' % time )
196
        for switch in remaining:
197
            if not switch.connected():
198
                warn( 'Warning: %s is not connected to a controller\n'
199
                      % switch.name )
200
            else:
201
                remaining.remove( switch )
202
        return not remaining
203

    
204
    def addHost( self, name, cls=None, **params ):
205
        """Add host.
206
           name: name of host to add
207
           cls: custom host class/constructor (optional)
208
           params: parameters for host
209
           returns: added host"""
210
        # Default IP and MAC addresses
211
        defaults = { 'ip': ipAdd( self.nextIP,
212
                                  ipBaseNum=self.ipBaseNum,
213
                                  prefixLen=self.prefixLen ) +
214
                                  '/%s' % self.prefixLen }
215
        if self.autoSetMacs:
216
            defaults[ 'mac' ] = macColonHex( self.nextIP )
217
        if self.autoPinCpus:
218
            defaults[ 'cores' ] = self.nextCore
219
            self.nextCore = ( self.nextCore + 1 ) % self.numCores
220
        self.nextIP += 1
221
        defaults.update( params )
222
        if not cls:
223
            cls = self.host
224
        h = cls( name, **defaults )
225
        self.hosts.append( h )
226
        self.nameToNode[ name ] = h
227
        return h
228

    
229
    def addSwitch( self, name, cls=None, **params ):
230
        """Add switch.
231
           name: name of switch to add
232
           cls: custom switch class/constructor (optional)
233
           returns: added switch
234
           side effect: increments listenPort ivar ."""
235
        defaults = { 'listenPort': self.listenPort,
236
                     'inNamespace': self.inNamespace }
237
        defaults.update( params )
238
        if not cls:
239
            cls = self.switch
240
        sw = cls( name, **defaults )
241
        if not self.inNamespace and self.listenPort:
242
            self.listenPort += 1
243
        self.switches.append( sw )
244
        self.nameToNode[ name ] = sw
245
        return sw
246

    
247
    def addController( self, name='c0', controller=None, **params ):
248
        """Add controller.
249
           controller: Controller class"""
250
        # Get controller class
251
        if not controller:
252
            controller = self.controller
253
        # Construct new controller if one is not given
254
        if isinstance( name, Controller ):
255
            controller_new = name
256
            # Pylint thinks controller is a str()
257
            # pylint: disable=maybe-no-member
258
            name = controller_new.name
259
            # pylint: enable=maybe-no-member
260
        else:
261
            controller_new = controller( name, **params )
262
        # Add new controller to net
263
        if controller_new:  # allow controller-less setups
264
            self.controllers.append( controller_new )
265
            self.nameToNode[ name ] = controller_new
266
        return controller_new
267

    
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"""
276
        nat = self.addHost( name, cls=NAT, inNamespace=inNamespace,
277
                            subnet=self.ipBase, **params )
278
        # find first switch and create link
279
        if connect:
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
284
            self.addLink( nat, connect )
285
            # Set the default route on hosts
286
            natIP = nat.params[ 'ip' ].split('/')[ 0 ]
287
            for host in self.hosts:
288
                if host.inNamespace:
289
                    host.setDefaultRoute( 'via %s' % natIP )
290
        return nat
291

    
292
    # BL: We now have four ways to look up nodes
293
    # This may (should?) be cleaned up in the future.
294
    def getNodeByName( self, *args ):
295
        "Return node(s) with given name(s)"
296
        if len( args ) == 1:
297
            return self.nameToNode[ args[ 0 ] ]
298
        return [ self.nameToNode[ n ] for n in args ]
299

    
300
    def get( self, *args ):
301
        "Convenience alias for getNodeByName"
302
        return self.getNodeByName( *args )
303

    
304
    # Even more convenient syntax for node lookup and iteration
305
    def __getitem__( self, key ):
306
        """net [ name ] operator: Return node(s) with given name(s)"""
307
        return self.nameToNode[ key ]
308

    
309
    def __iter__( self ):
310
        "return iterator over node names"
311
        for node in chain( self.hosts, self.switches, self.controllers ):
312
            yield node.name
313

    
314
    def __len__( self ):
315
        "returns number of nodes in net"
316
        return ( len( self.hosts ) + len( self.switches ) +
317
                 len( self.controllers ) )
318

    
319
    def __contains__( self, item ):
320
        "returns True if net contains named node"
321
        return item in self.nameToNode
322

    
323
    def keys( self ):
324
        "return a list of all node names or net's keys"
325
        return list( self )
326

    
327
    def values( self ):
328
        "return a list of all nodes or net's values"
329
        return [ self[name] for name in self ]
330

    
331
    def items( self ):
332
        "return (key,value) tuple list for every node in net"
333
        return zip( self.keys(), self.values() )
334

    
335
    @staticmethod
336
    def randMac():
337
        "Return a random, non-multicast MAC address"
338
        return macColonHex( random.randint(1, 2**48 - 1) & 0xfeffffffffff |
339
                            0x020000000000 )
340

    
341
    def addLink( self, node1, node2, port1=None, port2=None,
342
                 cls=None, **params ):
343
        """"Add a link from node1 to node2
344
            node1: source node (or name)
345
            node2: dest node (or name)
346
            port1: source port (optional)
347
            port2: dest port (optional)
348
            cls: link class (optional)
349
            params: additional link params (optional)
350
            returns: link object"""
351
        # Accept node objects or names
352
        node1 = node1 if not isinstance( node1, basestring ) else self[ node1 ]
353
        node2 = node2 if not isinstance( node2, basestring ) else self[ node2 ]
354
        options = dict( params )
355
        # Port is optional
356
        if port1 is not None:
357
            options.setdefault( 'port1', port1 )
358
        if port2 is not None:
359
            options.setdefault( 'port2', port2 )
360
        if self.intf is not None:
361
            options.setdefault( 'intf', self.intf )
362
        # Set default MAC - this should probably be in Link
363
        options.setdefault( 'addr1', self.randMac() )
364
        options.setdefault( 'addr2', self.randMac() )
365
        cls = self.link if cls is None else cls
366
        link = cls( node1, node2, **options )
367
        self.links.append( link )
368
        return link
369

    
370
    def configHosts( self ):
371
        "Configure a set of hosts."
372
        for host in self.hosts:
373
            info( host.name + ' ' )
374
            intf = host.defaultIntf()
375
            if intf:
376
                host.configDefault()
377
            else:
378
                # Don't configure nonexistent intf
379
                host.configDefault( ip=None, mac=None )
380
            # You're low priority, dude!
381
            # BL: do we want to do this here or not?
382
            # May not make sense if we have CPU lmiting...
383
            # quietRun( 'renice +18 -p ' + repr( host.pid ) )
384
            # This may not be the right place to do this, but
385
            # it needs to be done somewhere.
386
        info( '\n' )
387

    
388
    def buildFromTopo( self, topo=None ):
389
        """Build mininet from a topology object
390
           At the end of this function, everything should be connected
391
           and up."""
392

    
393
        # Possibly we should clean up here and/or validate
394
        # the topo
395
        if self.cleanup:
396
            pass
397

    
398
        info( '*** Creating network\n' )
399

    
400
        if not self.controllers and self.controller:
401
            # Add a default controller
402
            info( '*** Adding controller\n' )
403
            classes = self.controller
404
            if not isinstance( classes, list ):
405
                classes = [ classes ]
406
            for i, cls in enumerate( classes ):
407
                # Allow Controller objects because nobody understands partial()
408
                if isinstance( cls, Controller ):
409
                    self.addController( cls )
410
                else:
411
                    self.addController( 'c%d' % i, cls )
412

    
413
        info( '*** Adding hosts:\n' )
414
        for hostName in topo.hosts():
415
            self.addHost( hostName, **topo.nodeInfo( hostName ) )
416
            info( hostName + ' ' )
417

    
418
        info( '\n*** Adding switches:\n' )
419
        for switchName in topo.switches():
420
            # A bit ugly: add batch parameter if appropriate
421
            params = topo.nodeInfo( switchName)
422
            cls = params.get( 'cls', self.switch )
423
            if hasattr( cls, 'batchStartup' ):
424
                params.setdefault( 'batch', True )
425
            self.addSwitch( switchName, **params )
426
            info( switchName + ' ' )
427

    
428
        info( '\n*** Adding links:\n' )
429
        for srcName, dstName, params in topo.links(
430
                sort=True, withInfo=True ):
431
            self.addLink( **params )
432
            info( '(%s, %s) ' % ( srcName, dstName ) )
433

    
434
        info( '\n' )
435

    
436
    def configureControlNetwork( self ):
437
        "Control net config hook: override in subclass"
438
        raise Exception( 'configureControlNetwork: '
439
                         'should be overriden in subclass', self )
440

    
441
    def build( self ):
442
        "Build mininet."
443
        if self.topo:
444
            self.buildFromTopo( self.topo )
445
        if self.inNamespace:
446
            self.configureControlNetwork()
447
        info( '*** Configuring hosts\n' )
448
        self.configHosts()
449
        if self.xterms:
450
            self.startTerms()
451
        if self.autoStaticArp:
452
            self.staticArp()
453
        self.built = True
454

    
455
    def startTerms( self ):
456
        "Start a terminal for each node."
457
        if 'DISPLAY' not in os.environ:
458
            error( "Error starting terms: Cannot connect to display\n" )
459
            return
460
        info( "*** Running terms on %s\n" % os.environ[ 'DISPLAY' ] )
461
        cleanUpScreens()
462
        self.terms += makeTerms( self.controllers, 'controller' )
463
        self.terms += makeTerms( self.switches, 'switch' )
464
        self.terms += makeTerms( self.hosts, 'host' )
465

    
466
    def stopXterms( self ):
467
        "Kill each xterm."
468
        for term in self.terms:
469
            os.kill( term.pid, signal.SIGKILL )
470
        cleanUpScreens()
471

    
472
    def staticArp( self ):
473
        "Add all-pairs ARP entries to remove the need to handle broadcast."
474
        for src in self.hosts:
475
            for dst in self.hosts:
476
                if src != dst:
477
                    src.setARP( ip=dst.IP(), mac=dst.MAC() )
478

    
479
    def start( self ):
480
        "Start controller and switches."
481
        if not self.built:
482
            self.build()
483
        info( '*** Starting controller\n' )
484
        for controller in self.controllers:
485
            info( controller.name + ' ')
486
            controller.start()
487
        info( '\n' )
488
        info( '*** Starting %s switches\n' % len( self.switches ) )
489
        for switch in self.switches:
490
            info( switch.name + ' ')
491
            switch.start( self.controllers )
492
        started = {}
493
        for swclass, switches in groupby(
494
                sorted( self.switches, key=type ), type ):
495
            switches = tuple( switches )
496
            if hasattr( swclass, 'batchStartup' ):
497
                success = swclass.batchStartup( switches )
498
                started.update( { s: s for s in success } )
499
        info( '\n' )
500
        if self.waitConn:
501
            self.waitConnected()
502

    
503
    def stop( self ):
504
        "Stop the controller(s), switches and hosts"
505
        info( '*** Stopping %i controllers\n' % len( self.controllers ) )
506
        for controller in self.controllers:
507
            info( controller.name + ' ' )
508
            controller.stop()
509
        info( '\n' )
510
        if self.terms:
511
            info( '*** Stopping %i terms\n' % len( self.terms ) )
512
            self.stopXterms()
513
        info( '*** Stopping %i links\n' % len( self.links ) )
514
        for link in self.links:
515
            info( '.' )
516
            link.stop()
517
        info( '\n' )
518
        info( '*** Stopping %i switches\n' % len( self.switches ) )
519
        stopped = {}
520
        for swclass, switches in groupby(
521
                sorted( self.switches, key=type ), type ):
522
            switches = tuple( switches )
523
            if hasattr( swclass, 'batchShutdown' ):
524
                success = swclass.batchShutdown( switches )
525
                stopped.update( { s: s for s in success } )
526
        for switch in self.switches:
527
            info( switch.name + ' ' )
528
            if switch not in stopped:
529
                switch.stop()
530
            switch.terminate()
531
        info( '\n' )
532
        info( '*** Stopping %i hosts\n' % len( self.hosts ) )
533
        for host in self.hosts:
534
            info( host.name + ' ' )
535
            host.terminate()
536
        info( '\n*** Done\n' )
537

    
538
    def run( self, test, *args, **kwargs ):
539
        "Perform a complete start/test/stop cycle."
540
        self.start()
541
        info( '*** Running test\n' )
542
        result = test( *args, **kwargs )
543
        self.stop()
544
        return result
545

    
546
    def monitor( self, hosts=None, timeoutms=-1 ):
547
        """Monitor a set of hosts (or all hosts by default),
548
           and return their output, a line at a time.
549
           hosts: (optional) set of hosts to monitor
550
           timeoutms: (optional) timeout value in ms
551
           returns: iterator which returns host, line"""
552
        if hosts is None:
553
            hosts = self.hosts
554
        poller = select.poll()
555
        h1 = hosts[ 0 ]  # so we can call class method fdToNode
556
        for host in hosts:
557
            poller.register( host.stdout )
558
        while True:
559
            ready = poller.poll( timeoutms )
560
            for fd, event in ready:
561
                host = h1.fdToNode( fd )
562
                if event & select.POLLIN:
563
                    line = host.readline()
564
                    if line is not None:
565
                        yield host, line
566
            # Return if non-blocking
567
            if not ready and timeoutms >= 0:
568
                yield None, None
569

    
570
    # XXX These test methods should be moved out of this class.
571
    # Probably we should create a tests.py for them
572

    
573
    @staticmethod
574
    def _parsePing( pingOutput ):
575
        "Parse ping output and return packets sent, received."
576
        # Check for downed link
577
        if 'connect: Network is unreachable' in pingOutput:
578
            return 1, 0
579
        r = r'(\d+) packets transmitted, (\d+) received'
580
        m = re.search( r, pingOutput )
581
        if m is None:
582
            error( '*** Error: could not parse ping output: %s\n' %
583
                   pingOutput )
584
            return 1, 0
585
        sent, received = int( m.group( 1 ) ), int( m.group( 2 ) )
586
        return sent, received
587

    
588
    def ping( self, hosts=None, timeout=None ):
589
        """Ping between all specified hosts.
590
           hosts: list of hosts
591
           timeout: time to wait for a response, as string
592
           returns: ploss packet loss percentage"""
593
        # should we check if running?
594
        packets = 0
595
        lost = 0
596
        ploss = None
597
        if not hosts:
598
            hosts = self.hosts
599
            output( '*** Ping: testing ping reachability\n' )
600
        for node in hosts:
601
            output( '%s -> ' % node.name )
602
            for dest in hosts:
603
                if node != dest:
604
                    opts = ''
605
                    if timeout:
606
                        opts = '-W %s' % timeout
607
                    if dest.intfs:
608
                        result = node.cmd( 'ping -c1 %s %s' %
609
                                           (opts, dest.IP()) )
610
                        sent, received = self._parsePing( result )
611
                    else:
612
                        sent, received = 0, 0
613
                    packets += sent
614
                    if received > sent:
615
                        error( '*** Error: received too many packets' )
616
                        error( '%s' % result )
617
                        node.cmdPrint( 'route' )
618
                        exit( 1 )
619
                    lost += sent - received
620
                    output( ( '%s ' % dest.name ) if received else 'X ' )
621
            output( '\n' )
622
        if packets > 0:
623
            ploss = 100.0 * lost / packets
624
            received = packets - lost
625
            output( "*** Results: %i%% dropped (%d/%d received)\n" %
626
                    ( ploss, received, packets ) )
627
        else:
628
            ploss = 0
629
            output( "*** Warning: No packets sent\n" )
630
        return ploss
631

    
632
    @staticmethod
633
    def _parsePingFull( pingOutput ):
634
        "Parse ping output and return all data."
635
        errorTuple = (1, 0, 0, 0, 0, 0)
636
        # Check for downed link
637
        r = r'[uU]nreachable'
638
        m = re.search( r, pingOutput )
639
        if m is not None:
640
            return errorTuple
641
        r = r'(\d+) packets transmitted, (\d+) received'
642
        m = re.search( r, pingOutput )
643
        if m is None:
644
            error( '*** Error: could not parse ping output: %s\n' %
645
                   pingOutput )
646
            return errorTuple
647
        sent, received = int( m.group( 1 ) ), int( m.group( 2 ) )
648
        r = r'rtt min/avg/max/mdev = '
649
        r += r'(\d+\.\d+)/(\d+\.\d+)/(\d+\.\d+)/(\d+\.\d+) ms'
650
        m = re.search( r, pingOutput )
651
        if m is None:
652
            if received == 0:
653
                return errorTuple
654
            error( '*** Error: could not parse ping output: %s\n' %
655
                   pingOutput )
656
            return errorTuple
657
        rttmin = float( m.group( 1 ) )
658
        rttavg = float( m.group( 2 ) )
659
        rttmax = float( m.group( 3 ) )
660
        rttdev = float( m.group( 4 ) )
661
        return sent, received, rttmin, rttavg, rttmax, rttdev
662

    
663
    def pingFull( self, hosts=None, timeout=None ):
664
        """Ping between all specified hosts and return all data.
665
           hosts: list of hosts
666
           timeout: time to wait for a response, as string
667
           returns: all ping data; see function body."""
668
        # should we check if running?
669
        # Each value is a tuple: (src, dsd, [all ping outputs])
670
        all_outputs = []
671
        if not hosts:
672
            hosts = self.hosts
673
            output( '*** Ping: testing ping reachability\n' )
674
        for node in hosts:
675
            output( '%s -> ' % node.name )
676
            for dest in hosts:
677
                if node != dest:
678
                    opts = ''
679
                    if timeout:
680
                        opts = '-W %s' % timeout
681
                    result = node.cmd( 'ping -c1 %s %s' % (opts, dest.IP()) )
682
                    outputs = self._parsePingFull( result )
683
                    sent, received, rttmin, rttavg, rttmax, rttdev = outputs
684
                    all_outputs.append( (node, dest, outputs) )
685
                    output( ( '%s ' % dest.name ) if received else 'X ' )
686
            output( '\n' )
687
        output( "*** Results: \n" )
688
        for outputs in all_outputs:
689
            src, dest, ping_outputs = outputs
690
            sent, received, rttmin, rttavg, rttmax, rttdev = ping_outputs
691
            output( " %s->%s: %s/%s, " % (src, dest, sent, received ) )
692
            output( "rtt min/avg/max/mdev %0.3f/%0.3f/%0.3f/%0.3f ms\n" %
693
                    (rttmin, rttavg, rttmax, rttdev) )
694
        return all_outputs
695

    
696
    def pingAll( self, timeout=None ):
697
        """Ping between all hosts.
698
           returns: ploss packet loss percentage"""
699
        return self.ping( timeout=timeout )
700

    
701
    def pingPair( self ):
702
        """Ping between first two hosts, useful for testing.
703
           returns: ploss packet loss percentage"""
704
        hosts = [ self.hosts[ 0 ], self.hosts[ 1 ] ]
705
        return self.ping( hosts=hosts )
706

    
707
    def pingAllFull( self ):
708
        """Ping between all hosts.
709
           returns: ploss packet loss percentage"""
710
        return self.pingFull()
711

    
712
    def pingPairFull( self ):
713
        """Ping between first two hosts, useful for testing.
714
           returns: ploss packet loss percentage"""
715
        hosts = [ self.hosts[ 0 ], self.hosts[ 1 ] ]
716
        return self.pingFull( hosts=hosts )
717

    
718
    @staticmethod
719
    def _parseIperf( iperfOutput ):
720
        """Parse iperf output and return bandwidth.
721
           iperfOutput: string
722
           returns: result string"""
723
        r = r'([\d\.]+ \w+/sec)'
724
        m = re.findall( r, iperfOutput )
725
        if m:
726
            return m[-1]
727
        else:
728
            # was: raise Exception(...)
729
            error( 'could not parse iperf output: ' + iperfOutput )
730
            return ''
731

    
732
    # XXX This should be cleaned up
733

    
734
    def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', fmt=None,
735
               seconds=5, port=5001):
736
        """Run iperf between two hosts.
737
           hosts: list of hosts; if None, uses first and last hosts
738
           l4Type: string, one of [ TCP, UDP ]
739
           udpBw: bandwidth target for UDP test
740
           fmt: iperf format argument if any
741
           seconds: iperf time to transmit
742
           port: iperf port
743
           returns: two-element array of [ server, client ] speeds
744
           note: send() is buffered, so client rate can be much higher than
745
           the actual transmission rate; on an unloaded system, server
746
           rate should be much closer to the actual receive rate"""
747
        hosts = hosts or [ self.hosts[ 0 ], self.hosts[ -1 ] ]
748
        assert len( hosts ) == 2
749
        client, server = hosts
750
        output( '*** Iperf: testing', l4Type, 'bandwidth between',
751
                client, 'and', server, '\n' )
752
        server.cmd( 'killall -9 iperf' )
753
        iperfArgs = 'iperf -p %d ' % port
754
        bwArgs = ''
755
        if l4Type == 'UDP':
756
            iperfArgs += '-u '
757
            bwArgs = '-b ' + udpBw + ' '
758
        elif l4Type != 'TCP':
759
            raise Exception( 'Unexpected l4 type: %s' % l4Type )
760
        if fmt:
761
            iperfArgs += '-f %s ' % fmt
762
        server.sendCmd( iperfArgs + '-s' )
763
        if l4Type == 'TCP':
764
            if not waitListening( client, server.IP(), port ):
765
                raise Exception( 'Could not connect to iperf on port %d'
766
                                 % port )
767
        cliout = client.cmd( iperfArgs + '-t %d -c ' % seconds +
768
                             server.IP() + ' ' + bwArgs )
769
        debug( 'Client output: %s\n' % cliout )
770
        servout = ''
771
        # We want the last *b/sec from the iperf server output
772
        # for TCP, there are two fo them because of waitListening
773
        count = 2 if l4Type == 'TCP' else 1
774
        while len( re.findall( '/sec', servout ) ) < count:
775
            servout += server.monitor( timeoutms=5000 )
776
        server.sendInt()
777
        server.waitOutput()
778
        debug( 'Server output: %s\n' % servout )
779
        result = [ self._parseIperf( servout ), self._parseIperf( cliout ) ]
780
        if l4Type == 'UDP':
781
            result.insert( 0, udpBw )
782
        output( '*** Results: %s\n' % result )
783
        return result
784

    
785
    def runCpuLimitTest( self, cpu, duration=5 ):
786
        """run CPU limit test with 'while true' processes.
787
        cpu: desired CPU fraction of each host
788
        duration: test duration in seconds (integer)
789
        returns a single list of measured CPU fractions as floats.
790
        """
791
        cores = int( quietRun( 'nproc' ) )
792
        pct = cpu * 100
793
        info( '*** Testing CPU %.0f%% bandwidth limit\n' % pct )
794
        hosts = self.hosts
795
        cores = int( quietRun( 'nproc' ) )
796
        # number of processes to run a while loop on per host
797
        num_procs = int( ceil( cores * cpu ) )
798
        pids = {}
799
        for h in hosts:
800
            pids[ h ] = []
801
            for _core in range( num_procs ):
802
                h.cmd( 'while true; do a=1; done &' )
803
                pids[ h ].append( h.cmd( 'echo $!' ).strip() )
804
        outputs = {}
805
        time = {}
806
        # get the initial cpu time for each host
807
        for host in hosts:
808
            outputs[ host ] = []
809
            with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
810
                       host, 'r' ) as f:
811
                time[ host ] = float( f.read() )
812
        for _ in range( duration ):
813
            sleep( 1 )
814
            for host in hosts:
815
                with open( '/sys/fs/cgroup/cpuacct/%s/cpuacct.usage' %
816
                           host, 'r' ) as f:
817
                    readTime = float( f.read() )
818
                outputs[ host ].append( ( ( readTime - time[ host ] )
819
                                        / 1000000000 ) / cores * 100 )
820
                time[ host ] = readTime
821
        for h, pids in pids.items():
822
            for pid in pids:
823
                h.cmd( 'kill -9 %s' % pid )
824
        cpu_fractions = []
825
        for _host, outputs in outputs.items():
826
            for pct in outputs:
827
                cpu_fractions.append( pct )
828
        output( '*** Results: %s\n' % cpu_fractions )
829
        return cpu_fractions
830

    
831
    # BL: I think this can be rewritten now that we have
832
    # a real link class.
833
    def configLinkStatus( self, src, dst, status ):
834
        """Change status of src <-> dst links.
835
           src: node name
836
           dst: node name
837
           status: string {up, down}"""
838
        if src not in self.nameToNode:
839
            error( 'src not in network: %s\n' % src )
840
        elif dst not in self.nameToNode:
841
            error( 'dst not in network: %s\n' % dst )
842
        else:
843
            if isinstance( src, basestring ):
844
                src = self.nameToNode[ src ]
845
            if isinstance( dst, basestring ):
846
                dst = self.nameToNode[ dst ]
847
            connections = src.connectionsTo( dst )
848
            if len( connections ) == 0:
849
                error( 'src and dst not connected: %s %s\n' % ( src, dst) )
850
            for srcIntf, dstIntf in connections:
851
                result = srcIntf.ifconfig( status )
852
                if result:
853
                    error( 'link src status change failed: %s\n' % result )
854
                result = dstIntf.ifconfig( status )
855
                if result:
856
                    error( 'link dst status change failed: %s\n' % result )
857

    
858
    def interact( self ):
859
        "Start network and run our simple CLI."
860
        self.start()
861
        result = CLI( self )
862
        self.stop()
863
        return result
864

    
865
    inited = False
866

    
867
    @classmethod
868
    def init( cls ):
869
        "Initialize Mininet"
870
        if cls.inited:
871
            return
872
        ensureRoot()
873
        fixLimits()
874
        cls.inited = True
875

    
876

    
877
class MininetWithControlNet( Mininet ):
878

    
879
    """Control network support:
880

881
       Create an explicit control network. Currently this is only
882
       used/usable with the user datapath.
883

884
       Notes:
885

886
       1. If the controller and switches are in the same (e.g. root)
887
          namespace, they can just use the loopback connection.
888

889
       2. If we can get unix domain sockets to work, we can use them
890
          instead of an explicit control network.
891

892
       3. Instead of routing, we could bridge or use 'in-band' control.
893

894
       4. Even if we dispense with this in general, it could still be
895
          useful for people who wish to simulate a separate control
896
          network (since real networks may need one!)
897

898
       5. Basically nobody ever used this code, so it has been moved
899
          into its own class.
900

901
       6. Ultimately we may wish to extend this to allow us to create a
902
          control network which every node's control interface is
903
          attached to."""
904

    
905
    def configureControlNetwork( self ):
906
        "Configure control network."
907
        self.configureRoutedControlNetwork()
908

    
909
    # We still need to figure out the right way to pass
910
    # in the control network location.
911

    
912
    def configureRoutedControlNetwork( self, ip='192.168.123.1',
913
                                       prefixLen=16 ):
914
        """Configure a routed control network on controller and switches.
915
           For use with the user datapath only right now."""
916
        controller = self.controllers[ 0 ]
917
        info( controller.name + ' <->' )
918
        cip = ip
919
        snum = ipParse( ip )
920
        for switch in self.switches:
921
            info( ' ' + switch.name )
922
            link = self.link( switch, controller, port1=0 )
923
            sintf, cintf = link.intf1, link.intf2
924
            switch.controlIntf = sintf
925
            snum += 1
926
            while snum & 0xff in [ 0, 255 ]:
927
                snum += 1
928
            sip = ipStr( snum )
929
            cintf.setIP( cip, prefixLen )
930
            sintf.setIP( sip, prefixLen )
931
            controller.setHostRoute( sip, cintf )
932
            switch.setHostRoute( cip, sintf )
933
        info( '\n' )
934
        info( '*** Testing control network\n' )
935
        while not cintf.isUp():
936
            info( '*** Waiting for', cintf, 'to come up\n' )
937
            sleep( 1 )
938
        for switch in self.switches:
939
            while not sintf.isUp():
940
                info( '*** Waiting for', sintf, 'to come up\n' )
941
                sleep( 1 )
942
            if self.ping( hosts=[ switch, controller ] ) != 0:
943
                error( '*** Error: control network test failed\n' )
944
                exit( 1 )
945
        info( '\n' )