Statistics
| Branch: | Tag: | Revision:

mininet / mininet / node.py @ 3f61ea71

History | View | Annotate | Download (34.5 KB)

1
"""
2
Node objects for Mininet.
3

4
Nodes provide a simple abstraction for interacting with hosts, switches
5
and controllers. Local nodes are simply one or more processes on the local
6
machine.
7

8
Node: superclass for all (primarily local) network nodes.
9

10
Host: a virtual host. By default, a host is simply a shell; commands
11
    may be sent using Cmd (which waits for output), or using sendCmd(),
12
    which returns immediately, allowing subsequent monitoring using
13
    monitor(). Examples of how to run experiments using this
14
    functionality are provided in the examples/ directory.
15

16
CPULimitedHost: a virtual host whose CPU bandwidth is limited by
17
    RT or CFS bandwidth limiting.
18

19
Switch: superclass for switch nodes.
20

21
UserSwitch: a switch using the user-space switch from the OpenFlow
22
    reference implementation.
23

24
KernelSwitch: a switch using the kernel switch from the OpenFlow reference
25
    implementation.
26

27
OVSSwitch: a switch using the OpenVSwitch OpenFlow-compatible switch
28
    implementation (openvswitch.org).
29

30
Controller: superclass for OpenFlow controllers. The default controller
31
    is controller(8) from the reference implementation.
32

33
NOXController: a controller node using NOX (noxrepo.org).
34

35
RemoteController: a remote controller node, which may use any
36
    arbitrary OpenFlow-compatible controller, and which is not
37
    created or managed by mininet.
38

39
Future enhancements:
40

41
- Possibly make Node, Switch and Controller more abstract so that
42
  they can be used for both local and remote nodes
43

44
- Create proxy objects for remote nodes (Mininet: Cluster Edition)
45
"""
46

    
47
import os
48
import re
49
import signal
50
import select
51
from subprocess import Popen, PIPE, STDOUT
52

    
53
from mininet.log import info, error, warn, debug
54
from mininet.util import quietRun, errRun, errFail, moveIntf, isShellBuiltin
55
from mininet.util import numCores, retry
56
from mininet.moduledeps import moduleDeps, pathCheck, OVS_KMOD, OF_KMOD, TUN
57
from mininet.link import Link, Intf, TCIntf
58

    
59
class Node( object ):
60
    """A virtual network node is simply a shell in a network namespace.
61
       We communicate with it using pipes."""
62

    
63
    portBase = 0  # Nodes always start with eth0/port0, even in OF 1.0
64

    
65
    def __init__( self, name, inNamespace=True, **params ):
66
        """name: name of node
67
           inNamespace: in network namespace?
68
           params: Node parameters (see config() for details)"""
69

    
70
        # Make sure class actually works
71
        self.checkSetup()
72

    
73
        self.name = name
74
        self.inNamespace = inNamespace
75

    
76
        # Stash configuration parameters for future reference
77
        self.params = params
78

    
79
        self.intfs = {}  # dict of port numbers to interfaces
80
        self.ports = {}  # dict of interfaces to port numbers
81
                         # replace with Port objects, eventually ?
82
        self.nameToIntf = {}  # dict of interface names to Intfs
83

    
84
        # Make pylint happy
85
        ( self.shell, self.execed, self.pid, self.stdin, self.stdout,
86
          self.lastPid, self.lastCmd, self.pollOut ) = (
87
            None, None, None, None, None, None, None, None )
88
        self.waiting = False
89
        self.readbuf = ''
90

    
91
        # Start command interpreter shell
92
        self.startShell()
93

    
94
    # File descriptor to node mapping support
95
    # Class variables and methods
96

    
97
    inToNode = {}  # mapping of input fds to nodes
98
    outToNode = {}  # mapping of output fds to nodes
99

    
100
    @classmethod
101
    def fdToNode( cls, fd ):
102
        """Return node corresponding to given file descriptor.
103
           fd: file descriptor
104
           returns: node"""
105
        node = cls.outToNode.get( fd )
106
        return node or cls.inToNode.get( fd )
107

    
108
    # Command support via shell process in namespace
109

    
110
    def startShell( self ):
111
        "Start a shell process for running commands"
112
        if self.shell:
113
            error( "%s: shell is already running" )
114
            return
115
        # mnexec: (c)lose descriptors, (d)etach from tty,
116
        # (p)rint pid, and run in (n)amespace
117
        opts = '-cdp'
118
        if self.inNamespace:
119
            opts += 'n'
120
        # bash -m: enable job control
121
        cmd = [ 'mnexec', opts, 'bash', '-m' ]
122
        self.shell = Popen( cmd, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
123
            close_fds=True )
124
        self.stdin = self.shell.stdin
125
        self.stdout = self.shell.stdout
126
        self.pid = self.shell.pid
127
        self.pollOut = select.poll()
128
        self.pollOut.register( self.stdout )
129
        # Maintain mapping between file descriptors and nodes
130
        # This is useful for monitoring multiple nodes
131
        # using select.poll()
132
        self.outToNode[ self.stdout.fileno() ] = self
133
        self.inToNode[ self.stdin.fileno() ] = self
134
        self.execed = False
135
        self.lastCmd = None
136
        self.lastPid = None
137
        self.readbuf = ''
138
        self.waiting = False
139

    
140
    def cleanup( self ):
141
        "Help python collect its garbage."
142
        if not self.inNamespace:
143
            for intfName in self.intfNames():
144
                if self.name in intfName:
145
                    quietRun( 'ip link del ' + intfName )
146
        self.shell = None
147

    
148
    def read( self, maxbytes=1024 ):
149
        """Buffered read from node, non-blocking.
150
           maxbytes: maximum number of bytes to return"""
151
        count = len( self.readbuf )
152
        if count < maxbytes:
153
            data = os.read( self.stdout.fileno(), maxbytes - count )
154
            self.readbuf += data
155
        if maxbytes >= len( self.readbuf ):
156
            result = self.readbuf
157
            self.readbuf = ''
158
        else:
159
            result = self.readbuf[ :maxbytes ]
160
            self.readbuf = self.readbuf[ maxbytes: ]
161
        return result
162

    
163
    def readline( self ):
164
        """Buffered readline from node, non-blocking.
165
           returns: line (minus newline) or None"""
166
        self.readbuf += self.read( 1024 )
167
        if '\n' not in self.readbuf:
168
            return None
169
        pos = self.readbuf.find( '\n' )
170
        line = self.readbuf[ 0 : pos ]
171
        self.readbuf = self.readbuf[ pos + 1: ]
172
        return line
173

    
174
    def write( self, data ):
175
        """Write data to node.
176
           data: string"""
177
        os.write( self.stdin.fileno(), data )
178

    
179
    def terminate( self ):
180
        "Send kill signal to Node and clean up after it."
181
        os.kill( self.pid, signal.SIGKILL )
182
        self.cleanup()
183

    
184
    def stop( self ):
185
        "Stop node."
186
        self.terminate()
187

    
188
    def waitReadable( self, timeoutms=None ):
189
        """Wait until node's output is readable.
190
           timeoutms: timeout in ms or None to wait indefinitely."""
191
        if len( self.readbuf ) == 0:
192
            self.pollOut.poll( timeoutms )
193

    
194
    def sendCmd( self, *args, **kwargs ):
195
        """Send a command, followed by a command to echo a sentinel,
196
           and return without waiting for the command to complete.
197
           args: command and arguments, or string
198
           printPid: print command's PID?"""
199
        assert not self.waiting
200
        printPid = kwargs.get( 'printPid', True )
201
        # Allow sendCmd( [ list ] )
202
        if len( args ) == 1 and type( args[ 0 ] ) is list:
203
            cmd = args[ 0 ]
204
        # Allow sendCmd( cmd, arg1, arg2... )
205
        elif len( args ) > 0:
206
            cmd = args
207
        # Convert to string
208
        if not isinstance( cmd, str ):
209
            cmd = ' '.join( [ str( c ) for c in cmd ] )
210
        if not re.search( r'\w', cmd ):
211
            # Replace empty commands with something harmless
212
            cmd = 'echo -n'
213
        self.lastCmd = cmd
214
        printPid = printPid and not isShellBuiltin( cmd )
215
        if len( cmd ) > 0 and cmd[ -1 ] == '&':
216
            # print ^A{pid}\n{sentinel}
217
            cmd += ' printf "\\001%d\n\\177" $! \n'
218
        else:
219
            # print sentinel
220
            cmd += '; printf "\\177"'
221
            if printPid and not isShellBuiltin( cmd ):
222
                cmd = 'mnexec -p ' + cmd
223
        self.write( cmd + '\n' )
224
        self.lastPid = None
225
        self.waiting = True
226

    
227
    def sendInt( self, sig=signal.SIGINT ):
228
        "Interrupt running command."
229
        if self.lastPid:
230
            try:
231
                os.kill( self.lastPid, sig )
232
            except OSError:
233
                pass
234

    
235
    def monitor( self, timeoutms=None ):
236
        """Monitor and return the output of a command.
237
           Set self.waiting to False if command has completed.
238
           timeoutms: timeout in ms or None to wait indefinitely."""
239
        self.waitReadable( timeoutms )
240
        data = self.read( 1024 )
241
        # Look for PID
242
        marker = chr( 1 ) + r'\d+\n'
243
        if chr( 1 ) in data:
244
            markers = re.findall( marker, data )
245
            if markers:
246
                self.lastPid = int( markers[ 0 ][ 1: ] )
247
                data = re.sub( marker, '', data )
248
        # Look for sentinel/EOF
249
        if len( data ) > 0 and data[ -1 ] == chr( 127 ):
250
            self.waiting = False
251
            data = data[ :-1 ]
252
        elif chr( 127 ) in data:
253
            self.waiting = False
254
            data = data.replace( chr( 127 ), '' )
255
        return data
256

    
257
    def waitOutput( self, verbose=False ):
258
        """Wait for a command to complete.
259
           Completion is signaled by a sentinel character, ASCII(127)
260
           appearing in the output stream.  Wait for the sentinel and return
261
           the output, including trailing newline.
262
           verbose: print output interactively"""
263
        log = info if verbose else debug
264
        output = ''
265
        while self.waiting:
266
            data = self.monitor()
267
            output += data
268
            log( data )
269
        return output
270

    
271
    def cmd( self, *args, **kwargs ):
272
        """Send a command, wait for output, and return it.
273
           cmd: string"""
274
        verbose = kwargs.get( 'verbose', False )
275
        log = info if verbose else debug
276
        log( '*** %s : %s\n' % ( self.name, args ) )
277
        self.sendCmd( *args, **kwargs )
278
        return self.waitOutput( verbose )
279

    
280
    def cmdPrint( self, *args):
281
        """Call cmd and printing its output
282
           cmd: string"""
283
        return self.cmd( *args, **{ 'verbose': True } )
284

    
285
    # Interface management, configuration, and routing
286

    
287
    # BL notes: This might be a bit redundant or over-complicated.
288
    # However, it does allow a bit of specialization, including
289
    # changing the canonical interface names. It's also tricky since
290
    # the real interfaces are created as veth pairs, so we can't
291
    # make a single interface at a time.
292

    
293
    def newPort( self ):
294
        "Return the next port number to allocate."
295
        if len( self.ports ) > 0:
296
            return max( self.ports.values() ) + 1
297
        return self.portBase
298

    
299
    def addIntf( self, intf, port=None ):
300
        """Add an interface.
301
           intf: interface
302
           port: port number (optional, typically OpenFlow port number)"""
303
        if port is None:
304
            port = self.newPort()
305
        self.intfs[ port ] = intf
306
        self.ports[ intf ] = port
307
        self.nameToIntf[ intf.name ] = intf
308
        debug( '\n' )
309
        debug( 'added intf %s:%d to node %s\n' % ( intf, port, self.name ) )
310
        if self.inNamespace:
311
            debug( 'moving', intf, 'into namespace for', self.name, '\n' )
312
            moveIntf( intf.name, self )
313

    
314
    def defaultIntf( self ):
315
        "Return interface for lowest port"
316
        ports = self.intfs.keys()
317
        if ports:
318
            return self.intfs[ min( ports ) ]
319
        else:
320
            warn( '*** defaultIntf: warning:', self.name,
321
                 'has no interfaces\n' )
322

    
323
    def intf( self, intf='' ):
324
        """Return our interface object with given name,
325
           or default intf if name is empty"""
326
        if not intf:
327
            return self.defaultIntf()
328
        elif type( intf) is str:
329
            return self.nameToIntf[ intf ]
330
        else:
331
            return None
332

    
333
    def connectionsTo( self, node):
334
        "Return [ intf1, intf2... ] for all intfs that connect self to node."
335
        # We could optimize this if it is important
336
        connections = []
337
        for intf in self.intfList():
338
            link = intf.link
339
            if link:
340
                node1, node2 = link.intf1.node, link.intf2.node
341
                if node1 == self and node2 == node:
342
                    connections += [ ( intf, link.intf2 ) ]
343
                elif node1 == node and node2 == self:
344
                    connections += [ ( intf, link.intf1 ) ]
345
        return connections
346

    
347
    def deleteIntfs( self ):
348
        "Delete all of our interfaces."
349
        # In theory the interfaces should go away after we shut down.
350
        # However, this takes time, so we're better off removing them
351
        # explicitly so that we won't get errors if we run before they
352
        # have been removed by the kernel. Unfortunately this is very slow,
353
        # at least with Linux kernels before 2.6.33
354
        for intf in self.intfs.values():
355
            intf.delete()
356
            info( '.' )
357

    
358
    # Routing support
359

    
360
    def setARP( self, ip, mac ):
361
        """Add an ARP entry.
362
           ip: IP address as string
363
           mac: MAC address as string"""
364
        result = self.cmd( 'arp', '-s', ip, mac )
365
        return result
366

    
367
    def setHostRoute( self, ip, intf ):
368
        """Add route to host.
369
           ip: IP address as dotted decimal
370
           intf: string, interface name"""
371
        return self.cmd( 'route add -host', ip, 'dev', intf )
372

    
373
    def setDefaultRoute( self, intf=None ):
374
        """Set the default route to go through intf.
375
           intf: string, interface name"""
376
        if not intf:
377
            intf = self.defaultIntf()
378
        self.cmd( 'ip route flush root 0/0' )
379
        return self.cmd( 'route add default %s' % intf )
380

    
381
    # Convenience and configuration methods
382

    
383
    def setMAC( self, mac, intf=None ):
384
        """Set the MAC address for an interface.
385
           intf: intf or intf name
386
           mac: MAC address as string"""
387
        return self.intf( intf ).setMAC( mac )
388

    
389
    def setIP( self, ip, prefixLen=8, intf=None ):
390
        """Set the IP address for an interface.
391
           intf: interface name
392
           ip: IP address as a string
393
           prefixLen: prefix length, e.g. 8 for /8 or 16M addrs"""
394
        # This should probably be rethought
395
        if '/' not in ip:
396
            ip = '%s/%s' % ( ip, prefixLen )
397
        return self.intf( intf ).setIP( ip )
398

    
399
    def IP( self, intf=None ):
400
        "Return IP address of a node or specific interface."
401
        return self.intf( intf ).IP()
402

    
403
    def MAC( self, intf=None ):
404
        "Return MAC address of a node or specific interface."
405
        return self.intf( intf ).IP()
406

    
407
    def intfIsUp( self, intf=None ):
408
        "Check if an interface is up."
409
        return self.intf( intf ).isUp()
410

    
411
    # The reason why we configure things in this way is so
412
    # That the parameters can be listed and documented in
413
    # the config method.
414
    # Dealing with subclasses and superclasses is slightly
415
    # annoying, but at least the information is there!
416

    
417
    def setParam( self, results, method, **param ):
418
        """Internal method: configure a *single* parameter
419
           results: dict of results to update
420
           method: config method name
421
           param: arg=value (ignore if value=None)
422
           value may also be list or dict"""
423
        name, value = param.items()[ 0 ]
424
        f = getattr( self, method, None )
425
        if not f or value is None:
426
            return
427
        if type( value ) is list:
428
            result = f( *value )
429
        elif type( value ) is dict:
430
            result = f( **value )
431
        else:
432
            result = f( value )
433
        results[ name ] = result
434
        return result
435

    
436
    def config( self, mac=None, ip=None,
437
                defaultRoute=None, lo='up', **_params ):
438
        """Configure Node according to (optional) parameters:
439
           mac: MAC address for default interface
440
           ip: IP address for default interface
441
           ifconfig: arbitrary interface configuration
442
           Subclasses should override this method and call
443
           the parent class's config(**params)"""
444
        # If we were overriding this method, we would call
445
        # the superclass config method here as follows:
446
        # r = Parent.config( **_params )
447
        r = {}
448
        self.setParam( r, 'setMAC', mac=mac )
449
        self.setParam( r, 'setIP', ip=ip )
450
        self.setParam( r, 'defaultRoute', defaultRoute=defaultRoute )
451
        # This should be examined
452
        self.cmd( 'ifconfig lo ' + lo )
453
        return r
454

    
455
    def configDefault( self, **moreParams ):
456
        "Configure with default parameters"
457
        self.params.update( moreParams )
458
        self.config( **self.params )
459

    
460
    # This is here for backward compatibility
461
    def linkTo( self, node, link=Link ):
462
        """(Deprecated) Link to another node
463
           replace with Link( node1, node2)"""
464
        return link( self, node )
465

    
466
    # Other methods
467

    
468
    def intfList( self ):
469
        "List of our interfaces sorted by port number"
470
        return [ self.intfs[ p ] for p in sorted( self.intfs.iterkeys() ) ]
471

    
472
    def intfNames( self ):
473
        "The names of our interfaces sorted by port number"
474
        return [ str( i ) for i in self.intfList() ]
475

    
476
    def __repr__( self ):
477
        "More informative string representation"
478
        intfs = ( ','.join( [ '%s:%s' % ( i.name, i.IP() )
479
                        for i in self.intfList() ] ) )
480
        return '<%s %s: %s pid=%s> ' % (
481
                self.__class__.__name__, self.name, intfs, self.pid )
482

    
483
    def __str__( self ):
484
        "Abbreviated string representation"
485
        return self.name
486

    
487
    # Automatic class setup support
488

    
489
    isSetup = False
490

    
491
    @classmethod
492
    def checkSetup( cls ):
493
        "Make sure our class and superclasses are set up"
494
        while cls and not getattr( cls, 'isSetup', True ):
495
            cls.setup()
496
            cls.isSetup = True
497
            # Make pylint happy
498
            cls = getattr( type( cls ), '__base__', None )
499

    
500
    @classmethod
501
    def setup( cls ):
502
        "Make sure our class dependencies are available"
503
        pathCheck( 'mnexec', 'ifconfig', moduleName='Mininet')
504

    
505

    
506
class Host( Node ):
507
    "A host is simply a Node"
508
    pass
509

    
510

    
511
class CPULimitedHost( Host ):
512

    
513
    "CPU limited host"
514

    
515
    def __init__( self, name, sched='cfs', **kwargs ):
516
        Host.__init__( self, name, **kwargs )
517
        # Create a cgroup and move shell into it
518
        self.cgroup = 'cpu,cpuacct:/' + self.name
519
        errFail( 'cgcreate -g ' + self.cgroup )
520
        errFail( 'cgclassify -g %s %s' % ( self.cgroup, self.pid ) )
521
        # BL: Setting the correct period/quota is tricky, particularly
522
        # for RT. RT allows very small quotas, but the overhead
523
        # seems to be high. CFS has a mininimum quota of 1 ms, but
524
        # still does better with larger period values.
525
        self.period_us = kwargs.get( 'period_us', 100000 )
526
        self.sched = sched
527

    
528
    def cgroupSet( self, param, value, resource='cpu' ):
529
        "Set a cgroup parameter and return its value"
530
        cmd = 'cgset -r %s.%s=%s /%s' % (
531
            resource, param, value, self.name )
532
        quietRun( cmd )
533
        nvalue = int( self.cgroupGet( param, resource ) )
534
        if nvalue != value:
535
            error( '*** error: cgroupSet: %s set to %s instead of %s\n'
536
                   % ( param, nvalue, value ) )
537
        return nvalue
538

    
539
    def cgroupGet( self, param, resource='cpu' ):
540
        "Return value of cgroup parameter"
541
        cmd = 'cgget -r %s.%s /%s' % (
542
            resource, param, self.name )
543
        return quietRun( cmd ).split()[ -1 ]
544

    
545
    def cgroupDel( self ):
546
        "Clean up our cgroup"
547
        # info( '*** deleting cgroup', self.cgroup, '\n' )
548
        _out, _err, exitcode = errRun( 'cgdelete -r ' + self.cgroup )
549
        return exitcode != 0
550

    
551
    def cleanup( self ):
552
        "Clean up our cgroup"
553
        retry( retries=3, delaySecs=1, fn=self.cgroupDel )
554

    
555
    def chrt( self, prio=20 ):
556
        "Set RT scheduling priority"
557
        quietRun( 'chrt -p %s %s' % ( prio, self.pid ) )
558
        result = quietRun( 'chrt -p %s' % self.pid )
559
        firstline = result.split( '\n' )[ 0 ]
560
        lastword = firstline.split( ' ' )[ -1 ]
561
        if lastword != 'SCHED_RR':
562
            error( '*** error: could not assign SCHED_RR to %s\n' % self.name )
563
        return lastword
564

    
565
    def rtInfo( self, f ):
566
        "Internal method: return parameters for RT bandwidth"
567
        pstr, qstr = 'rt_period_us', 'rt_runtime_us'
568
        # RT uses wall clock time for period and quota
569
        quota = int( self.period_us * f * numCores() )
570
        return pstr, qstr, self.period_us, quota
571

    
572
    def cfsInfo( self, f):
573
        "Internal method: return parameters for CFS bandwidth"
574
        pstr, qstr = 'cfs_period_us', 'cfs_quota_us'
575
        # CFS uses wall clock time for period and CPU time for quota.
576
        quota = int( self.period_us * f * numCores() )
577
        period = self.period_us
578
        if f > 0 and quota < 1000:
579
            debug( '(cfsInfo: increasing default period) ' )
580
            quota = 1000
581
            period = int( quota / f / numCores() )
582
        return pstr, qstr, period, quota
583

    
584
    # BL comment:
585
    # This may not be the right API,
586
    # since it doesn't specify CPU bandwidth in "absolute"
587
    # units the way link bandwidth is specified.
588
    # We should use MIPS or SPECINT or something instead.
589
    # Alternatively, we should change from system fraction
590
    # to CPU seconds per second, essentially assuming that
591
    # all CPUs are the same.
592

    
593
    def setCPUFrac( self, f=-1, sched=None):
594
        """Set overall CPU fraction for this host
595
           f: CPU bandwidth limit (fraction)
596
           sched: 'rt' or 'cfs'
597
           Note 'cfs' requires CONFIG_CFS_BANDWIDTH"""
598
        if not f:
599
            return
600
        if not sched:
601
            sched = self.sched
602
        if sched == 'rt':
603
            pstr, qstr, period, quota = self.rtInfo( f )
604
        elif sched == 'cfs':
605
            pstr, qstr, period, quota = self.cfsInfo( f )
606
        else:
607
            return
608
        if quota < 0:
609
            # Reset to unlimited
610
            quota = -1
611
        # Set cgroup's period and quota
612
        self.cgroupSet( pstr, period )
613
        self.cgroupSet( qstr, quota )
614
        if sched == 'rt':
615
            # Set RT priority if necessary
616
            self.chrt( prio=20 )
617
        info( '(%s %d/%dus) ' % ( sched, quota, period ) )
618

    
619
    def config( self, cpu=None, **params ):
620
        """cpu: desired overall system CPU fraction
621
           params: parameters for Node.config()"""
622
        r = Node.config( self, **params )
623
        # Was considering cpu={'cpu': cpu , 'sched': sched}, but
624
        # that seems redundant
625
        self.setParam( r, 'setCPUFrac', cpu=cpu )
626
        return r
627

    
628
# Some important things to note:
629
#
630
# The "IP" address which setIP() assigns to the switch is not
631
# an "IP address for the switch" in the sense of IP routing.
632
# Rather, it is the IP address for the control interface,
633
# on the control network, and it is only relevant to the
634
# controller. If you are running in the root namespace
635
# (which is the only way to run OVS at the moment), the
636
# control interface is the loopback interface, and you
637
# normally never want to change its IP address!
638
#
639
# In general, you NEVER want to attempt to use Linux's
640
# network stack (i.e. ifconfig) to "assign" an IP address or
641
# MAC address to a switch data port. Instead, you "assign"
642
# the IP and MAC addresses in the controller by specifying
643
# packets that you want to receive or send. The "MAC" address
644
# reported by ifconfig for a switch data port is essentially
645
# meaningless. It is important to understand this if you
646
# want to create a functional router using OpenFlow.
647

    
648
class Switch( Node ):
649
    """A Switch is a Node that is running (or has execed?)
650
       an OpenFlow switch."""
651

    
652
    portBase = 1  # Switches start with port 1 in OpenFlow
653

    
654
    def __init__( self, name, dpid=None, opts='', listenPort=None, **params):
655
        """dpid: dpid for switch (or None for default)
656
           opts: additional switch options
657
           listenPort: port to listen on for dpctl connections"""
658
        Node.__init__( self, name, **params )
659
        self.dpid = dpid if dpid else self.defaultDpid()
660
        self.opts = opts
661
        self.listenPort = listenPort
662
        if not self.inNamespace:
663
            self.controlIntf = Intf( 'lo', self, port=0 )
664

    
665
    def defaultDpid( self ):
666
        "Derive dpid from switch name, s1 -> 1"
667
        dpid = int( re.findall( '\d+', self.name )[ 0 ] )
668
        dpid = hex( dpid )[ 2: ]
669
        dpid = '0' * ( 16 - len( dpid ) ) + dpid
670
        return dpid
671

    
672
    def defaultIntf( self ):
673
        "Return control interface"
674
        if self.controlIntf:
675
            return self.controlIntf
676
        else:
677
            return Node.defaultIntf( self )
678

    
679
    def sendCmd( self, *cmd, **kwargs ):
680
        """Send command to Node.
681
           cmd: string"""
682
        kwargs.setdefault( 'printPid', False )
683
        if not self.execed:
684
            return Node.sendCmd( self, *cmd, **kwargs )
685
        else:
686
            error( '*** Error: %s has execed and cannot accept commands' %
687
                     self.name )
688

    
689
    def __repr__( self ):
690
        "More informative string representation"
691
        intfs = ( ','.join( [ '%s:%s' % ( i.name, i.IP() )
692
                        for i in self.intfList() ] ) )
693
        return '<%s %s: %s pid=%s> ' % (
694
                self.__class__.__name__, self.name, intfs, self.pid )
695

    
696
class UserSwitch( Switch ):
697
    "User-space switch."
698

    
699
    def __init__( self, name, **kwargs ):
700
        """Init.
701
           name: name for the switch"""
702
        Switch.__init__( self, name, **kwargs )
703
        pathCheck( 'ofdatapath', 'ofprotocol',
704
            moduleName='the OpenFlow reference user switch (openflow.org)' )
705
        if self.listenPort:
706
            self.opts += ' --listen=ptcp:%i ' % self.listenPort
707

    
708
    @classmethod
709
    def setup( cls ):
710
        "Ensure any dependencies are loaded; if not, try to load them."
711
        if not os.path.exists( '/dev/net/tun' ):
712
            moduleDeps( add=TUN )
713

    
714
    def dpctl( self, *args ):
715
        "Run dpctl command"
716
        if not self.listenPort:
717
            return "can't run dpctl without passive listening port"
718
        return self.cmd( 'dpctl ' + ' '.join( args ) +
719
                         ' tcp:127.0.0.1:%i' % self.listenPort )
720

    
721
    def start( self, controllers ):
722
        """Start OpenFlow reference user datapath.
723
           Log to /tmp/sN-{ofd,ofp}.log.
724
           controllers: list of controller objects"""
725
        controller = controllers[ 0 ]
726
        ofdlog = '/tmp/' + self.name + '-ofd.log'
727
        ofplog = '/tmp/' + self.name + '-ofp.log'
728
        self.cmd( 'ifconfig lo up' )
729
        intfs = [ str( i ) for i in self.intfList() if not i.IP() ]
730
        self.cmd( 'ofdatapath -i ' + ','.join( intfs ) +
731
            ' punix:/tmp/' + self.name + ' -d ' + self.dpid +
732
            ' --no-slicing ' +
733
            ' 1> ' + ofdlog + ' 2> ' + ofdlog + ' &' )
734
        self.cmd( 'ofprotocol unix:/tmp/' + self.name +
735
            ' tcp:%s:%d' % ( controller.IP(), controller.port ) +
736
            ' --fail=closed ' + self.opts +
737
            ' 1> ' + ofplog + ' 2>' + ofplog + ' &' )
738

    
739
    def stop( self ):
740
        "Stop OpenFlow reference user datapath."
741
        self.cmd( 'kill %ofdatapath' )
742
        self.cmd( 'kill %ofprotocol' )
743
        self.deleteIntfs()
744

    
745

    
746
class OVSLegacyKernelSwitch( Switch ):
747
    """Open VSwitch legacy kernel-space switch using ovs-openflowd.
748
       Currently only works in the root namespace."""
749

    
750
    def __init__( self, name, dp=None, **kwargs ):
751
        """Init.
752
           name: name for switch
753
           dp: netlink id (0, 1, 2, ...)
754
           defaultMAC: default MAC as unsigned int; random value if None"""
755
        Switch.__init__( self, name, **kwargs )
756
        self.dp = dp if dp else self.name
757
        self.intf = self.dp
758
        if self.inNamespace:
759
            error( "OVSKernelSwitch currently only works"
760
                " in the root namespace.\n" )
761
            exit( 1 )
762

    
763
    @classmethod
764
    def setup( cls ):
765
        "Ensure any dependencies are loaded; if not, try to load them."
766
        pathCheck( 'ovs-dpctl', 'ovs-openflowd',
767
            moduleName='Open vSwitch (openvswitch.org)')
768
        moduleDeps( subtract=OF_KMOD, add=OVS_KMOD )
769

    
770
    def start( self, controllers ):
771
        "Start up kernel datapath."
772
        ofplog = '/tmp/' + self.name + '-ofp.log'
773
        quietRun( 'ifconfig lo up' )
774
        # Delete local datapath if it exists;
775
        # then create a new one monitoring the given interfaces
776
        self.cmd( 'ovs-dpctl del-dp ' + self.dp )
777
        self.cmd( 'ovs-dpctl add-dp ' + self.dp )
778
        intfs = [ str( i ) for i in self.intfList() if not i.IP() ]
779
        self.cmd( 'ovs-dpctl', 'add-if', self.dp, ' '.join( intfs ) )
780
        # Run protocol daemon
781
        controller = controllers[ 0 ]
782
        self.cmd( 'ovs-openflowd ' + self.dp +
783
            ' tcp:%s:%d' % ( controller.IP(), controller.port ) +
784
            ' --fail=secure ' + self.opts +
785
            ' --datapath-id=' + self.dpid +
786
            ' 1>' + ofplog + ' 2>' + ofplog + '&' )
787
        self.execed = False
788

    
789
    def stop( self ):
790
        "Terminate kernel datapath."
791
        quietRun( 'ovs-dpctl del-dp ' + self.dp )
792
        self.cmd( 'kill %ovs-openflowd' )
793
        self.deleteIntfs()
794

    
795

    
796
class OVSSwitch( Switch ):
797
    "Open vSwitch switch. Depends on ovs-vsctl."
798

    
799
    def __init__( self, name, **params ):
800
        """Init.
801
           name: name for switch
802
           defaultMAC: default MAC as unsigned int; random value if None"""
803
        Switch.__init__( self, name, **params )
804

    
805
    @classmethod
806
    def setup( cls ):
807
        "Make sure Open vSwitch is installed and working"
808
        pathCheck( 'ovs-vsctl',
809
            moduleName='Open vSwitch (openvswitch.org)')
810
        moduleDeps( subtract=OF_KMOD, add=OVS_KMOD )
811
        out, err, exitcode = errRun( 'ovs-vsctl -t 1 show' )
812
        if exitcode:
813
            error( out + err +
814
                   'ovs-vsctl exited with code %d\n' % exitcode +
815
                   '*** Error connecting to ovs-db with ovs-vsctl\n'
816
                   'Make sure that Open vSwitch is installed, '
817
                   'that ovsdb-server is running, and that\n'
818
                   '"ovs-vsctl show" works correctly.\n'
819
                   'You may wish to try '
820
                   '"service openvswitch-switch start".\n' )
821
            exit( 1 )
822

    
823
    def dpctl( self, *args ):
824
        "Run ovs-dpctl command"
825
        return self.cmd( 'ovs-dpctl', args[ 0 ], self, *args[ 1: ] )
826

    
827
    @staticmethod
828
    def TCReapply( intf ):
829
        """Unfortunately OVS and Mininet are fighting
830
           over tc queuing disciplines. As a quick hack/
831
           workaround, we clear OVS's and reapply our own."""
832
        if type( intf ) is TCIntf:
833
            intf.config( **intf.params )
834

    
835
    def attach( self, intf ):
836
        "Connect a data port"
837
        self.cmd( 'ovs-vsctl add-port', self, intf )
838
        self.cmd( 'ifconfig', intf, 'up' )
839
        self.TCReapply( intf )
840

    
841
    def detach( self, intf ):
842
        "Disconnect a data port"
843
        self.cmd( 'ovs-vsctl del-port', self, intf )
844

    
845
    def start( self, controllers ):
846
        "Start up a new OVS OpenFlow switch using ovs-vsctl"
847
        if self.inNamespace:
848
            raise Exception(
849
                'OVS kernel switch does not work in a namespace' )
850
        # We should probably call config instead, but this
851
        # requires some rethinking...
852
        self.cmd( 'ifconfig lo up' )
853
        # Annoyingly, --if-exists option seems not to work
854
        self.cmd( 'ovs-vsctl del-br', self )
855
        self.cmd( 'ovs-vsctl add-br', self )
856
        self.cmd( 'ovs-vsctl set-fail-mode', self, 'secure' )
857
        for intf in self.intfList():
858
            if not intf.IP():
859
                self.attach( intf )
860
        # Add controllers
861
        clist = ','.join( [ 'tcp:%s:%d' % ( c.IP(), c.port )
862
                            for c in controllers ] )
863
        self.cmd( 'ovs-vsctl set-controller', self, clist )
864

    
865
    def stop( self ):
866
        "Terminate OVS switch."
867
        self.cmd( 'ovs-vsctl del-br', self )
868
        self.deleteIntfs()
869

    
870
OVSKernelSwitch = OVSSwitch
871

    
872

    
873
class Controller( Node ):
874
    """A Controller is a Node that is running (or has execed?) an
875
       OpenFlow controller."""
876

    
877
    def __init__( self, name, inNamespace=False, command='controller',
878
                 cargs='-v ptcp:%d', cdir=None, ip="127.0.0.1",
879
                 port=6633, **params ):
880
        self.command = command
881
        self.cargs = cargs
882
        self.cdir = cdir
883
        self.ip = ip
884
        self.port = port
885
        Node.__init__( self, name, inNamespace=inNamespace,
886
            ip=ip, **params  )
887

    
888
    def start( self ):
889
        """Start <controller> <args> on controller.
890
           Log to /tmp/cN.log"""
891
        pathCheck( self.command )
892
        cout = '/tmp/' + self.name + '.log'
893
        if self.cdir is not None:
894
            self.cmd( 'cd ' + self.cdir )
895
        self.cmd( self.command + ' ' + self.cargs % self.port +
896
            ' 1>' + cout + ' 2>' + cout + '&' )
897
        self.execed = False
898

    
899
    def stop( self ):
900
        "Stop controller."
901
        self.cmd( 'kill %' + self.command )
902
        self.terminate()
903

    
904
    def IP( self, intf=None ):
905
        "Return IP address of the Controller"
906
        if self.intfs:
907
            ip = Node.IP( self, intf )
908
        else:
909
            ip = self.ip
910
        return ip
911

    
912
    def __repr__( self ):
913
        "More informative string representation"
914
        return '<%s %s: %s:%s pid=%s> ' % (
915
                self.__class__.__name__, self.name,
916
                self.IP(), self.port, self.pid )
917

    
918

    
919
class OVSController( Controller ):
920
    "Open vSwitch controller"
921
    def __init__( self, name, command='ovs-controller', **kwargs ):
922
        Controller.__init__( self, name, command=command, **kwargs )
923

    
924

    
925
class NOX( Controller ):
926
    "Controller to run a NOX application."
927

    
928
    def __init__( self, name, *noxArgs, **kwargs ):
929
        """Init.
930
           name: name to give controller
931
           noxArgs: arguments (strings) to pass to NOX"""
932
        if not noxArgs:
933
            warn( 'warning: no NOX modules specified; '
934
                  'running packetdump only\n' )
935
            noxArgs = [ 'packetdump' ]
936
        elif type( noxArgs ) not in ( list, tuple ):
937
            noxArgs = [ noxArgs ]
938

    
939
        if 'NOX_CORE_DIR' not in os.environ:
940
            exit( 'exiting; please set missing NOX_CORE_DIR env var' )
941
        noxCoreDir = os.environ[ 'NOX_CORE_DIR' ]
942

    
943
        Controller.__init__( self, name,
944
            command=noxCoreDir + '/nox_core',
945
            cargs='--libdir=/usr/local/lib -v -i ptcp:%s ' +
946
                    ' '.join( noxArgs ),
947
            cdir=noxCoreDir, **kwargs )
948

    
949

    
950
class RemoteController( Controller ):
951
    "Controller running outside of Mininet's control."
952

    
953
    def __init__( self, name, defaultIP='127.0.0.1',
954
                 port=6633, **kwargs):
955
        """Init.
956
           name: name to give controller
957
           defaultIP: the IP address where the remote controller is
958
           listening
959
           port: the port where the remote controller is listening"""
960
        Controller.__init__( self, name, defaultIP=defaultIP, port=port,
961
            **kwargs )
962

    
963
    def start( self ):
964
        "Overridden to do nothing."
965
        return
966

    
967
    def stop( self ):
968
        "Overridden to do nothing."
969
        return