Statistics
| Branch: | Tag: | Revision:

mininet / mininet / util.py @ 50cebe67

History | View | Annotate | Download (12.6 KB)

1
"Utility functions for Mininet."
2

    
3
from mininet.log import output, info, error
4

    
5
from time import sleep
6
from resource import setrlimit, RLIMIT_NPROC, RLIMIT_NOFILE
7
from select import poll, POLLIN
8
from subprocess import call, check_call, Popen, PIPE, STDOUT
9
import re
10
from fcntl import fcntl, F_GETFL, F_SETFL
11
from os import O_NONBLOCK
12

    
13
# Command execution support
14

    
15
def run( cmd ):
16
    """Simple interface to subprocess.call()
17
       cmd: list of command params"""
18
    return call( cmd.split( ' ' ) )
19

    
20
def checkRun( cmd ):
21
    """Simple interface to subprocess.check_call()
22
       cmd: list of command params"""
23
    return check_call( cmd.split( ' ' ) )
24

    
25
# pylint doesn't understand explicit type checking
26
# pylint: disable-msg=E1103
27

    
28
def oldQuietRun( *cmd ):
29
    """Run a command, routing stderr to stdout, and return the output.
30
       cmd: list of command params"""
31
    if len( cmd ) == 1:
32
        cmd = cmd[ 0 ]
33
        if isinstance( cmd, str ):
34
            cmd = cmd.split( ' ' )
35
    popen = Popen( cmd, stdout=PIPE, stderr=STDOUT )
36
    # We can't use Popen.communicate() because it uses
37
    # select(), which can't handle
38
    # high file descriptor numbers! poll() can, however.
39
    out = ''
40
    readable = poll()
41
    readable.register( popen.stdout )
42
    while True:
43
        while readable.poll():
44
            data = popen.stdout.read( 1024 )
45
            if len( data ) == 0:
46
                break
47
            out += data
48
        popen.poll()
49
        if popen.returncode != None:
50
            break
51
    return out
52

    
53

    
54
# This is a bit complicated, but it enables us to
55
# monitor commount output as it is happening
56

    
57
def errRun( *cmd, **kwargs ):
58
    """Run a command and return stdout, stderr and return code
59
       cmd: string or list of command and args
60
       stderr: STDOUT to merge stderr with stdout
61
       shell: run command using shell
62
       echo: monitor output to console"""
63
    # Allow passing in a list or a string
64
    if len( cmd ) == 1:
65
        cmd = cmd[ 0 ]
66
        if isinstance( cmd, str ):
67
            cmd = cmd.split( ' ' )
68
    cmd = [ str( arg ) for arg in cmd ]
69
    # By default we separate stderr, don't run in a shell, and don't echo
70
    stderr = kwargs.get( 'stderr', PIPE )
71
    shell = kwargs.get( 'shell', False )
72
    echo = kwargs.get( 'echo', False )
73
    if echo:
74
        # cmd goes to stderr, output goes to stdout
75
        info( cmd, '\n' )
76
    popen = Popen( cmd, stdout=PIPE, stderr=stderr, shell=shell )
77
    # We use poll() because select() doesn't work with large fd numbers
78
    out, err = '', ''
79
    poller = poll()
80
    poller.register( popen.stdout, POLLIN )
81
    fdtofile = { popen.stdout.fileno(): popen.stdout }
82
    if popen.stderr:
83
        fdtofile[ popen.stderr.fileno() ] = popen.stderr
84
        poller.register( popen.stderr, POLLIN )
85
    while True:
86
        readable = poller.poll()
87
        # Tell pylint to ignore unused variable event
88
        # pylint: disable-msg=W0612
89
        for fd, event in readable:
90
            # pylint: enable-msg=W0612
91
            f = fdtofile[ fd ]
92
            data = f.read( 1024 )
93
            if echo:
94
                output( data )
95
            if f == popen.stdout:
96
                out += data
97
            elif f == popen.stderr:
98
                err += data
99
        returncode = popen.poll()
100
        if returncode is not None:
101
            break
102
    return out, err, returncode
103

    
104
def errFail( *cmd, **kwargs ):
105
    "Run a command using errRun and raise exception on nonzero exit"
106
    out, err, ret = errRun( *cmd, **kwargs )
107
    if ret:
108
        raise Exception( "errFail: %s failed with return code %s: %s"
109
                         % ( cmd, ret, err ) )
110
    return out, err, ret
111

    
112
def quietRun( cmd, **kwargs ):
113
    "Run a command and return merged stdout and stderr"
114
    return errRun( cmd, stderr=STDOUT, **kwargs )[ 0 ]
115

    
116
# pylint: enable-msg=E1103
117
# pylint: disable-msg=E1101,W0612
118

    
119
def isShellBuiltin( cmd ):
120
    "Return True if cmd is a bash builtin."
121
    if isShellBuiltin.builtIns is None:
122
        isShellBuiltin.builtIns = quietRun( 'bash -c enable' )
123
    space = cmd.find( ' ' )
124
    if space > 0:
125
        cmd = cmd[ :space]
126
    return cmd in isShellBuiltin.builtIns
127

    
128
isShellBuiltin.builtIns = None
129

    
130
# pylint: enable-msg=E1101,W0612
131

    
132
# Interface management
133
#
134
# Interfaces are managed as strings which are simply the
135
# interface names, of the form 'nodeN-ethM'.
136
#
137
# To connect nodes, we create a pair of veth interfaces, and then place them
138
# in the pair of nodes that we want to communicate. We then update the node's
139
# list of interfaces and connectivity map.
140
#
141
# For the kernel datapath, switch interfaces
142
# live in the root namespace and thus do not have to be
143
# explicitly moved.
144

    
145
def makeIntfPair( intf1, intf2 ):
146
    """Make a veth pair connecting intf1 and intf2.
147
       intf1: string, interface
148
       intf2: string, interface
149
       returns: success boolean"""
150
    # Delete any old interfaces with the same names
151
    quietRun( 'ip link del ' + intf1 )
152
    quietRun( 'ip link del ' + intf2 )
153
    # Create new pair
154
    cmd = 'ip link add name ' + intf1 + ' type veth peer name ' + intf2
155
    return quietRun( cmd )
156

    
157
def retry( retries, delaySecs, fn, *args, **keywords ):
158
    """Try something several times before giving up.
159
       n: number of times to retry
160
       delaySecs: wait this long between tries
161
       fn: function to call
162
       args: args to apply to function call"""
163
    tries = 0
164
    while not fn( *args, **keywords ) and tries < retries:
165
        sleep( delaySecs )
166
        tries += 1
167
    if tries >= retries:
168
        error( "*** gave up after %i retries\n" % tries )
169
        exit( 1 )
170

    
171
def moveIntfNoRetry( intf, node, printError=False ):
172
    """Move interface to node, without retrying.
173
       intf: string, interface
174
       node: Node object
175
       printError: if true, print error"""
176
    cmd = 'ip link set ' + intf + ' netns ' + repr( node.pid )
177
    quietRun( cmd )
178
    links = node.cmd( 'ip link show' )
179
    if not ( ' %s:' % intf ) in links:
180
        if printError:
181
            error( '*** Error: moveIntf: ' + intf +
182
                ' not successfully moved to ' + node.name + '\n' )
183
        return False
184
    return True
185

    
186
def moveIntf( intf, node, printError=False, retries=3, delaySecs=0.001 ):
187
    """Move interface to node, retrying on failure.
188
       intf: string, interface
189
       node: Node object
190
       printError: if true, print error"""
191
    retry( retries, delaySecs, moveIntfNoRetry, intf, node, printError )
192

    
193
# Support for dumping network
194

    
195
def dumpNodeConnections( nodes ):
196
    "Dump connections to/from nodes."
197

    
198
    def dumpConnections( node ):
199
        "Helper function: dump connections to node"
200
        for intf in node.intfList():
201
            output( ' %s:' % intf )
202
            if intf.link:
203
                intfs = [ intf.link.intf1, intf.link.intf2 ]
204
                intfs.remove( intf )
205
                output( intfs[ 0 ] )
206
            else:
207
                output( ' ' )
208

    
209
    for node in nodes:
210
        output( node.name )
211
        dumpConnections( node )
212
        output( '\n' )
213

    
214
def dumpNetConnections( net ):
215
    "Dump connections in network"
216
    nodes = net.controllers + net.switches + net.hosts
217
    dumpNodeConnections( nodes )
218

    
219
# IP and Mac address formatting and parsing
220

    
221
def _colonHex( val, bytecount ):
222
    """Generate colon-hex string.
223
       val: input as unsigned int
224
       bytescount: number of bytes to convert
225
       returns: chStr colon-hex string"""
226
    pieces = []
227
    for i in range( bytecount - 1, -1, -1 ):
228
        piece = ( ( 0xff << ( i * 8 ) ) & val ) >> ( i * 8 )
229
        pieces.append( '%02x' % piece )
230
    chStr = ':'.join( pieces )
231
    return chStr
232

    
233
def macColonHex( mac ):
234
    """Generate MAC colon-hex string from unsigned int.
235
       mac: MAC address as unsigned int
236
       returns: macStr MAC colon-hex string"""
237
    return _colonHex( mac, 6 )
238

    
239
def ipStr( ip ):
240
    """Generate IP address string from an unsigned int.
241
       ip: unsigned int of form w << 24 | x << 16 | y << 8 | z
242
       returns: ip address string w.x.y.z, or 10.x.y.z if w==0"""
243
    w = ( ip >> 24 ) & 0xff
244
    w = 10 if w == 0 else w
245
    x = ( ip >> 16 ) & 0xff
246
    y = ( ip >> 8 ) & 0xff
247
    z = ip & 0xff
248
    return "%i.%i.%i.%i" % ( w, x, y, z )
249

    
250
def ipNum( w, x, y, z ):
251
    """Generate unsigned int from components of IP address
252
       returns: w << 24 | x << 16 | y << 8 | z"""
253
    return  ( w << 24 ) | ( x << 16 ) | ( y << 8 ) | z
254

    
255
def ipAdd( i, prefixLen=8, ipBaseNum=0x0a000000 ):
256
    """Return IP address string from ints
257
       i: int to be added to ipbase
258
       prefixLen: optional IP prefix length
259
       ipBaseNum: option base IP address as int
260
       returns IP address as string"""
261
    # Ugly but functional
262
    assert i < ( 1 << ( 32 - prefixLen ) )
263
    mask = 0xffffffff ^ ( ( 1 << prefixLen ) - 1 )
264
    ipnum = i + ( ipBaseNum & mask )
265
    return ipStr( ipnum )
266

    
267
def ipParse( ip ):
268
    "Parse an IP address and return an unsigned int."
269
    args = [ int( arg ) for arg in ip.split( '.' ) ]
270
    return ipNum( *args )
271

    
272
def netParse( ipstr ):
273
    """Parse an IP network specification, returning
274
       address and prefix len as unsigned ints"""
275
    prefixLen = 0
276
    if '/' in ipstr:
277
        ip, pf = ipstr.split( '/' )
278
        prefixLen = int( pf )
279
    return ipParse( ip ), prefixLen
280

    
281
def checkInt( s ):
282
    "Check if input string is an int"
283
    try:
284
        int( s )
285
        return True
286
    except ValueError:
287
        return False
288

    
289
def checkFloat( s ):
290
    "Check if input string is a float"
291
    try:
292
        float( s )
293
        return True
294
    except ValueError:
295
        return False
296

    
297
def makeNumeric( s ):
298
    "Convert string to int or float if numeric."
299
    if checkInt( s ):
300
        return int( s )
301
    elif checkFloat( s ):
302
        return float( s )
303
    else:
304
        return s
305

    
306
# Popen support
307

    
308
def pmonitor(popens, timeoutms=500, readline=True,
309
             readmax=1024 ):
310
    """Monitor dict of hosts to popen objects
311
       a line at a time
312
       timeoutms: timeout for poll()
313
       readline: return single line of output
314
       yields: host, line/output (if any)
315
       terminates: when all EOFs received"""
316
    poller = poll()
317
    fdToHost = {}
318
    for host, popen in popens.iteritems():
319
        fd = popen.stdout.fileno()
320
        fdToHost[ fd ] = host
321
        poller.register( fd, POLLIN )
322
        if not readline:
323
            # Use non-blocking reads
324
            flags = fcntl( fd, F_GETFL )
325
            fcntl( fd, F_SETFL, flags | O_NONBLOCK )
326
    while True:
327
        fds = poller.poll( timeoutms )
328
        if fds:
329
            for fd, _event in fds:
330
                host = fdToHost[ fd ]
331
                popen = popens[ host ]
332
                if readline:
333
                    # Attempt to read a line of output
334
                    # This blocks until we receive a newline!
335
                    line = popen.stdout.readline()
336
                else:
337
                    line = popen.stdout.read( readmax )
338
                yield host, line
339
                # Check for EOF
340
                if not line:
341
                    popen.poll()
342
                    if popen.returncode is not None:
343
                        poller.unregister( fd )
344
                        del popens[ host ]
345
                        if not popens:
346
                            return
347
        else:
348
            yield None, ''
349

    
350
# Other stuff we use
351

    
352
def fixLimits():
353
    "Fix ridiculously small resource limits."
354
    setrlimit( RLIMIT_NPROC, ( 8192, 8192 ) )
355
    setrlimit( RLIMIT_NOFILE, ( 16384, 16384 ) )
356

    
357
def mountCgroups():
358
    "Make sure cgroups file system is mounted"
359
    mounts = quietRun( 'mount' )
360
    cgdir = '/sys/fs/cgroup'
361
    csdir = cgdir + '/cpuset'
362
    if 'cgroups on %s' % cgdir not in mounts:
363
        raise Exception( "cgroups not mounted on " + cgdir )
364
    if 'cpuset on %s' % csdir not in mounts:
365
        errRun( 'mkdir -p ' + csdir )
366
        errRun( 'mount -t cgroup -ocpuset cpuset ' + csdir )
367

    
368
def natural( text ):
369
    "To sort sanely/alphabetically: sorted( l, key=natural )"
370
    def num( s ):
371
        "Convert text segment to int if necessary"
372
        return int( s ) if s.isdigit() else s
373
    return [  num( s ) for s in re.split( r'(\d+)', text ) ]
374

    
375
def naturalSeq( t ):
376
    "Natural sort key function for sequences"
377
    return [ natural( x ) for x in t ]
378

    
379
def numCores():
380
    "Returns number of CPU cores based on /proc/cpuinfo"
381
    if hasattr( numCores, 'ncores' ):
382
        return numCores.ncores
383
    try:
384
        numCores.ncores = int( quietRun('grep -c processor /proc/cpuinfo') )
385
    except ValueError:
386
        return 0
387
    return numCores.ncores
388

    
389
def irange(start, end):
390
    """Inclusive range from start to end (vs. Python insanity.)
391
       irange(1,5) -> 1, 2, 3, 4, 5"""
392
    return range( start, end + 1 )
393

    
394
def custom( cls, **params ):
395
    "Returns customized constructor for class cls."
396
    def customized( *args, **kwargs):
397
        "Customized constructor"
398
        kwargs.update( params )
399
        return cls( *args, **kwargs )
400
    return customized