Statistics
| Branch: | Tag: | Revision:

mininet / mininet / net.py @ d44a5843

History | View | Annotate | Download (18.2 KB)

1
"""
2

3
    Mininet: A simple networking testbed for OpenFlow!
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. 10.0.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
Currently we wrap the entire network in a 'mininet' object, which
65
constructs a simulated network based on a network topology created
66
using a topology object (e.g. LinearTopo) from mininet.topo or
67
mininet.topolib, and a Controller which the switches will connect
68
to. Several configuration options are provided for functions such as
69
automatically setting MAC addresses, populating the ARP table, or
70
even running a set of xterms to allow direct interaction with nodes.
71

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

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

81
After all desired tests or activities have been completed, the stop()
82
method may be called to shut down the network.
83

84
"""
85

    
86
import os
87
import re
88
import signal
89
from time import sleep
90

    
91
from mininet.cli import CLI
92
from mininet.log import info, error, debug
93
from mininet.node import Host, UserSwitch, KernelSwitch, Controller
94
from mininet.node import ControllerParams
95
from mininet.util import quietRun, fixLimits
96
from mininet.util import createLink, macColonHex, ipStr, ipParse
97
from mininet.xterm import cleanUpScreens, makeXterms
98

    
99
DATAPATHS = [ 'kernel' ] #[ 'user', 'kernel' ]
100

    
101
def init():
102
    "Initialize Mininet."
103
    if os.getuid() != 0:
104
        # Note: this script must be run as root
105
        # Perhaps we should do so automatically!
106
        print "*** Mininet must run as root."
107
        exit( 1 )
108
    # If which produces no output, then netns is not in the path.
109
    # May want to loosen this to handle netns in the current dir.
110
    if not quietRun( [ 'which', 'netns' ] ):
111
        raise Exception( "Could not find netns; see INSTALL" )
112
    fixLimits()
113

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

    
117
    def __init__( self, topo, switch=KernelSwitch, host=Host,
118
                 controller=Controller,
119
                 cparams=ControllerParams( '10.0.0.0', 8 ),
120
                 build=True, xterms=False, cleanup=False,
121
                 inNamespace=False,
122
                 autoSetMacs=False, autoStaticArp=False ):
123
        """Create Mininet object.
124
           topo: Topo (topology) object or None
125
           switch: Switch class
126
           host: Host class
127
           controller: Controller class
128
           cparams: ControllerParams object
129
           now: build now from topo?
130
           xterms: if build now, spawn xterms?
131
           cleanup: if build now, cleanup before creating?
132
           inNamespace: spawn switches and controller in net namespaces?
133
           autoSetMacs: set MAC addrs from topo?
134
           autoStaticArp: set all-pairs static MAC addrs?"""
135
        self.switch = switch
136
        self.host = host
137
        self.controller = controller
138
        self.cparams = cparams
139
        self.topo = topo
140
        self.inNamespace = inNamespace
141
        self.xterms = xterms
142
        self.cleanup = cleanup
143
        self.autoSetMacs = autoSetMacs
144
        self.autoStaticArp = autoStaticArp
145

    
146
        self.hosts = []
147
        self.switches = []
148
        self.controllers = []
149
        self.nameToNode = {} # name to Node (Host/Switch) objects
150
        self.idToNode = {} # dpid to Node (Host/Switch) objects
151
        self.dps = 0 # number of created kernel datapaths
152
        self.terms = [] # list of spawned xterm processes
153

    
154
        if build:
155
            self.build()
156

    
157
    def addHost( self, name, mac=None, ip=None ):
158
        """Add host.
159
           name: name of host to add
160
           mac: default MAC address for intf 0
161
           ip: default IP address for intf 0
162
           returns: added host"""
163
        host = self.host( name, defaultMAC=mac, defaultIP=ip )
164
        self.hosts.append( host )
165
        self.nameToNode[ name ] = host
166
        return host
167

    
168
    def addSwitch( self, name, mac=None, ip=None ):
169
        """Add switch.
170
           name: name of switch to add
171
           mac: default MAC address for kernel/OVS switch intf 0
172
           returns: added switch"""
173
        if self.switch == UserSwitch:
174
            sw = self.switch( name, defaultMAC=mac, defaultIP=ip,
175
                inNamespace=self.inNamespace )
176
        else:
177
            sw = self.switch( name, defaultMAC=mac, defaultIP=ip, dp=self.dps,
178
                inNamespace=self.inNamespace )
179
        self.dps += 1
180
        self.switches.append( sw )
181
        self.nameToNode[ name ] = sw
182
        return sw
183

    
184
    def addController( self, controller ):
185
        """Add controller.
186
           controller: Controller class"""
187
        controller = self.controller( 'c0', self.inNamespace )
188
        if controller: # allow controller-less setups
189
            self.controllers.append( controller )
190
            self.nameToNode[ 'c0' ] = controller
191

    
192
    # Control network support:
193
    #
194
    # Create an explicit control network. Currently this is only
195
    # used by the user datapath configuration.
196
    #
197
    # Notes:
198
    #
199
    # 1. If the controller and switches are in the same (e.g. root)
200
    #    namespace, they can just use the loopback connection.
201
    #
202
    # 2. If we can get unix domain sockets to work, we can use them
203
    #    instead of an explicit control network.
204
    #
205
    # 3. Instead of routing, we could bridge or use 'in-band' control.
206
    #
207
    # 4. Even if we dispense with this in general, it could still be
208
    #    useful for people who wish to simulate a separate control
209
    #    network (since real networks may need one!)
210

    
211
    def configureControlNetwork( self ):
212
        "Configure control network."
213
        self.configureRoutedControlNetwork()
214

    
215
    # We still need to figure out the right way to pass
216
    # in the control network location.
217

    
218
    def configureRoutedControlNetwork( self, ip='192.168.123.1',
219
        prefixLen=16 ):
220
        """Configure a routed control network on controller and switches.
221
           For use with the user datapath only right now.
222
           """
223
        controller = self.controllers[ 0 ]
224
        info( controller.name + ' <->' )
225
        cip = ip
226
        snum = ipParse( ip )
227
        for switch in self.switches:
228
            info( ' ' + switch.name )
229
            sintf, cintf = createLink( switch, controller )
230
            snum += 1
231
            while snum & 0xff in [ 0, 255 ]:
232
                snum += 1
233
            sip = ipStr( snum )
234
            controller.setIP( cintf, cip, prefixLen )
235
            switch.setIP( sintf, sip, prefixLen )
236
            controller.setHostRoute( sip, cintf )
237
            switch.setHostRoute( cip, sintf )
238
        info( '\n' )
239
        info( '*** Testing control network\n' )
240
        while not controller.intfIsUp( cintf ):
241
            info( '*** Waiting for', cintf, 'to come up\n' )
242
            sleep( 1 )
243
        for switch in self.switches:
244
            while not switch.intfIsUp( sintf ):
245
                info( '*** Waiting for', sintf, 'to come up\n' )
246
                sleep( 1 )
247
            if self.ping( hosts=[ switch, controller ] ) != 0:
248
                error( '*** Error: control network test failed\n' )
249
                exit( 1 )
250
        info( '\n' )
251

    
252
    def configHosts( self ):
253
        "Configure a set of hosts."
254
        # params were: hosts, ips
255
        for host in self.hosts:
256
            hintf = host.intfs[ 0 ]
257
            host.setIP( hintf, host.defaultIP, self.cparams.prefixLen )
258
            host.setDefaultRoute( hintf )
259
            # You're low priority, dude!
260
            quietRun( 'renice +18 -p ' + repr( host.pid ) )
261
            info( host.name + ' ' )
262
        info( '\n' )
263

    
264
    def buildFromTopo( self, topo ):
265
        """Build mininet from a topology object
266
           At the end of this function, everything should be connected
267
           and up."""
268

    
269
        def addNode( prefix, addMethod, nodeId ):
270
            "Add a host or a switch."
271
            name = prefix + topo.name( nodeId )
272
            mac = macColonHex( nodeId ) if self.setMacs else None
273
            ip = topo.ip( nodeId )
274
            node = addMethod( name, mac=mac, ip=ip )
275
            self.idToNode[ nodeId ] = node
276
            info( name + ' ' )
277

    
278
        # Possibly we should clean up here and/or validate
279
        # the topo
280
        if self.cleanup:
281
            pass
282

    
283
        info( '*** Adding controller\n' )
284
        self.addController( self.controller )
285
        info( '*** Creating network\n' )
286
        info( '*** Adding hosts:\n' )
287
        for hostId in sorted( topo.hosts() ):
288
            addNode( 'h', self.addHost, hostId )
289
        info( '\n*** Adding switches:\n' )
290
        for switchId in sorted( topo.switches() ):
291
            addNode( 's', self.addSwitch, switchId )
292
        info( '\n*** Adding edges:\n' )
293
        for srcId, dstId in sorted( topo.edges() ):
294
            src, dst = self.idToNode[ srcId ], self.idToNode[ dstId ]
295
            srcPort, dstPort = topo.port( srcId, dstId )
296
            createLink( src, dst, srcPort, dstPort )
297
            info( '(%s, %s) ' % ( src.name, dst.name ) )
298
        info( '\n' )
299

    
300
    def build( self ):
301
        "Build mininet."
302
        if self.topo:
303
            self.buildFromTopo( self.topo )
304
        if self.inNamespace:
305
            info( '*** Configuring control network\n' )
306
            self.configureControlNetwork()
307
        info( '*** Configuring hosts\n' )
308
        self.configHosts()
309
        if self.xterms:
310
            self.startXterms()
311
        if self.autoSetMacs:
312
            self.setMacs()
313
        if self.autoStaticArp:
314
            self.staticArp()
315

    
316
    def startXterms( self ):
317
        "Start an xterm for each node."
318
        info( "*** Running xterms on %s\n" % os.environ[ 'DISPLAY' ] )
319
        cleanUpScreens()
320
        self.terms += makeXterms( self.controllers, 'controller' )
321
        self.terms += makeXterms( self.switches, 'switch' )
322
        self.terms += makeXterms( self.hosts, 'host' )
323

    
324
    def stopXterms( self ):
325
        "Kill each xterm."
326
        # Kill xterms
327
        for term in self.terms:
328
            os.kill( term.pid, signal.SIGKILL )
329
        cleanUpScreens()
330

    
331
    def setMacs( self ):
332
        """Set MAC addrs to correspond to default MACs on hosts.
333
           Assume that the host only has one interface."""
334
        for host in self.hosts:
335
            host.setMAC( host.intfs[ 0 ], host.defaultMAC )
336

    
337
    def staticArp( self ):
338
        "Add all-pairs ARP entries to remove the need to handle broadcast."
339
        for src in self.hosts:
340
            for dst in self.hosts:
341
                if src != dst:
342
                    src.setARP( ip=dst.IP(), mac=dst.MAC() )
343

    
344
    def start( self ):
345
        "Start controller and switches"
346
        info( '*** Starting controller\n' )
347
        for controller in self.controllers:
348
            controller.start()
349
        info( '*** Starting %s switches\n' % len( self.switches ) )
350
        for switch in self.switches:
351
            info( switch.name + ' ')
352
            switch.start( self.controllers )
353
        info( '\n' )
354

    
355
    def stop( self ):
356
        "Stop the controller(s), switches and hosts"
357
        if self.terms:
358
            info( '*** Stopping %i terms\n' % len( self.terms ) )
359
            self.stopXterms()
360
        info( '*** Stopping %i hosts\n' % len( self.hosts ) )
361
        for host in self.hosts:
362
            info( '%s ' % host.name )
363
            host.terminate()
364
        info( '\n' )
365
        info( '*** Stopping %i switches\n' % len( self.switches ) )
366
        for switch in self.switches:
367
            info( switch.name )
368
            switch.stop()
369
        info( '\n' )
370
        info( '*** Stopping %i controllers\n' % len( self.controllers ) )
371
        for controller in self.controllers:
372
            controller.stop()
373
        info( '*** Test complete\n' )
374

    
375
    def run( self, test, **params ):
376
        "Perform a complete start/test/stop cycle."
377
        self.start()
378
        info( '*** Running test\n' )
379
        result = getattr( self, test )( **params )
380
        self.stop()
381
        return result
382

    
383
    @staticmethod
384
    def _parsePing( pingOutput ):
385
        "Parse ping output and return packets sent, received."
386
        r = r'(\d+) packets transmitted, (\d+) received'
387
        m = re.search( r, pingOutput )
388
        if m == None:
389
            error( '*** Error: could not parse ping output: %s\n' %
390
                     pingOutput )
391
            exit( 1 )
392
        sent, received = int( m.group( 1 ) ), int( m.group( 2 ) )
393
        return sent, received
394

    
395
    def ping( self, hosts=None ):
396
        """Ping between all specified hosts.
397
           hosts: list of hosts
398
           returns: ploss packet loss percentage"""
399
        # should we check if running?
400
        packets = 0
401
        lost = 0
402
        ploss = None
403
        if not hosts:
404
            hosts = self.hosts
405
            info( '*** Ping: testing ping reachability\n' )
406
        for node in hosts:
407
            info( '%s -> ' % node.name )
408
            for dest in hosts:
409
                if node != dest:
410
                    result = node.cmd( 'ping -c1 ' + dest.IP() )
411
                    sent, received = self._parsePing( result )
412
                    packets += sent
413
                    if received > sent:
414
                        error( '*** Error: received too many packets' )
415
                        error( '%s' % result )
416
                        node.cmdPrint( 'route' )
417
                        exit( 1 )
418
                    lost += sent - received
419
                    info( ( '%s ' % dest.name ) if received else 'X ' )
420
            info( '\n' )
421
            ploss = 100 * lost / packets
422
        info( "*** Results: %i%% dropped (%d/%d lost)\n" %
423
                ( ploss, lost, packets ) )
424
        return ploss
425

    
426
    def pingAll( self ):
427
        """Ping between all hosts.
428
           returns: ploss packet loss percentage"""
429
        return self.ping()
430

    
431
    def pingPair( self ):
432
        """Ping between first two hosts, useful for testing.
433
           returns: ploss packet loss percentage"""
434
        hosts = [ self.hosts[ 0 ], self.hosts[ 1 ] ]
435
        return self.ping( hosts=hosts )
436

    
437
    @staticmethod
438
    def _parseIperf( iperfOutput ):
439
        """Parse iperf output and return bandwidth.
440
           iperfOutput: string
441
           returns: result string"""
442
        r = r'([\d\.]+ \w+/sec)'
443
        m = re.search( r, iperfOutput )
444
        if m:
445
            return m.group( 1 )
446
        else:
447
            raise Exception( 'could not parse iperf output: ' + iperfOutput )
448

    
449
    def iperf( self, hosts=None, l4Type='TCP', udpBw='10M',
450
              verbose=False ):
451
        """Run iperf between two hosts.
452
           hosts: list of hosts; if None, uses opposite hosts
453
           l4Type: string, one of [ TCP, UDP ]
454
           verbose: verbose printing
455
           returns: results two-element array of server and client speeds"""
456
        log = info if verbose else debug
457
        if not hosts:
458
            hosts = [ self.hosts[ 0 ], self.hosts[ -1 ] ]
459
        else:
460
            assert len( hosts ) == 2
461
        host0, host1 = hosts
462
        log( '*** Iperf: testing ' + l4Type + ' bandwidth between ' )
463
        log( "%s and %s\n" % ( host0.name, host1.name ) )
464
        host0.cmd( 'killall -9 iperf' )
465
        iperfArgs = 'iperf '
466
        bwArgs = ''
467
        if l4Type == 'UDP':
468
            iperfArgs += '-u '
469
            bwArgs = '-b ' + udpBw + ' '
470
        elif l4Type != 'TCP':
471
            raise Exception( 'Unexpected l4 type: %s' % l4Type )
472
        server = host0.cmd( iperfArgs + '-s &' )
473
        log( '%s\n' % server )
474
        client = host1.cmd( iperfArgs + '-t 5 -c ' + host0.IP() + ' ' +
475
                           bwArgs )
476
        log( '%s\n' % client )
477
        server = host0.cmd( 'killall -9 iperf' )
478
        log( '%s\n' % server )
479
        result = [ self._parseIperf( server ), self._parseIperf( client ) ]
480
        if l4Type == 'UDP':
481
            result.insert( 0, udpBw )
482
        log( '*** Results: %s\n' % result )
483
        return result
484

    
485
    def iperfUdp( self, udpBw='10M' ):
486
        "Run iperf UDP test."
487
        return self.iperf( l4Type='UDP', udpBw=udpBw )
488

    
489
    def interact( self ):
490
        "Start network and run our simple CLI."
491
        self.start()
492
        result = CLI( self )
493
        self.stop()
494
        return result