Statistics
| Branch: | Tag: | Revision:

mininet / mininet / util.py @ a5af91d0

History | View | Annotate | Download (9.95 KB)

1
"Utility functions for Mininet."
2

    
3
from time import sleep
4
from resource import setrlimit, RLIMIT_NPROC, RLIMIT_NOFILE
5
from select import poll, POLLIN
6
from subprocess import call, check_call, Popen, PIPE, STDOUT
7
from mininet.log import output, info, error
8
import re
9

    
10
# Command execution support
11

    
12
def run( cmd ):
13
    """Simple interface to subprocess.call()
14
       cmd: list of command params"""
15
    return call( cmd.split( ' ' ) )
16

    
17
def checkRun( cmd ):
18
    """Simple interface to subprocess.check_call()
19
       cmd: list of command params"""
20
    return check_call( cmd.split( ' ' ) )
21

    
22
# pylint doesn't understand explicit type checking
23
# pylint: disable-msg=E1103
24

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

    
50

    
51
# This is a bit complicated, but it enables us to
52
# monitor commount output as it is happening
53

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

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

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

    
113
# pylint: enable-msg=E1103
114
# pylint: disable-msg=E1101,W0612
115

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

    
125
isShellBuiltin.builtIns = None
126

    
127
# pylint: enable-msg=E1101,W0612
128

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

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

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

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

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

    
190

    
191
# IP and Mac address formatting and parsing
192

    
193
def _colonHex( val, bytecount ):
194
    """Generate colon-hex string.
195
       val: input as unsigned int
196
       bytescount: number of bytes to convert
197
       returns: chStr colon-hex string"""
198
    pieces = []
199
    for i in range( bytecount - 1, -1, -1 ):
200
        piece = ( ( 0xff << ( i * 8 ) ) & val ) >> ( i * 8 )
201
        pieces.append( '%02x' % piece )
202
    chStr = ':'.join( pieces )
203
    return chStr
204

    
205
def macColonHex( mac ):
206
    """Generate MAC colon-hex string from unsigned int.
207
       mac: MAC address as unsigned int
208
       returns: macStr MAC colon-hex string"""
209
    return _colonHex( mac, 6 )
210

    
211
def ipStr( ip ):
212
    """Generate IP address string from an unsigned int.
213
       ip: unsigned int of form w << 24 | x << 16 | y << 8 | z
214
       returns: ip address string w.x.y.z, or 10.x.y.z if w==0"""
215
    w = ( ip >> 24 ) & 0xff
216
    w = 10 if w == 0 else w
217
    x = ( ip >> 16 ) & 0xff
218
    y = ( ip >> 8 ) & 0xff
219
    z = ip & 0xff
220
    return "%i.%i.%i.%i" % ( w, x, y, z )
221

    
222
def ipNum( w, x, y, z ):
223
    """Generate unsigned int from components of IP address
224
       returns: w << 24 | x << 16 | y << 8 | z"""
225
    return  ( w << 24 ) | ( x << 16 ) | ( y << 8 ) | z
226

    
227
def ipAdd( i, prefixLen=8, ipBaseNum=0x0a000000 ):
228
    """Return IP address string from ints
229
       i: int to be added to ipbase
230
       prefixLen: optional IP prefix length
231
       ipBaseNum: option base IP address as int
232
       returns IP address as string"""
233
    # Ugly but functional
234
    assert i < ( 1 << ( 32 - prefixLen ) )
235
    mask = 0xffffffff ^ ( ( 1 << prefixLen ) - 1 )
236
    ipnum = i + ( ipBaseNum & mask )
237
    return ipStr( ipnum )
238

    
239
def ipParse( ip ):
240
    "Parse an IP address and return an unsigned int."
241
    args = [ int( arg ) for arg in ip.split( '.' ) ]
242
    return ipNum( *args )
243

    
244
def netParse( ipstr ):
245
    """Parse an IP network specification, returning
246
       address and prefix len as unsigned ints"""
247
    prefixLen = 0
248
    if '/' in ipstr:
249
        ip, pf = ipstr.split( '/' )
250
        prefixLen = int( pf )
251
    return ipParse( ip ), prefixLen
252

    
253
def checkInt( s ):
254
    "Check if input string is an int"
255
    try:
256
        int( s )
257
        return True
258
    except ValueError:
259
        return False
260

    
261
def checkFloat( s ):
262
    "Check if input string is a float"
263
    try:
264
        float( s )
265
        return True
266
    except ValueError:
267
        return False
268

    
269
def makeNumeric( s ):
270
    "Convert string to int or float if numeric."
271
    if checkInt( s ):
272
        return int( s )
273
    elif checkFloat( s ):
274
        return float( s )
275
    else:
276
        return s
277

    
278

    
279
# Other stuff we use
280

    
281
def fixLimits():
282
    "Fix ridiculously small resource limits."
283
    setrlimit( RLIMIT_NPROC, ( 4096, 8192 ) )
284
    setrlimit( RLIMIT_NOFILE, ( 16384, 32768 ) )
285

    
286
def natural( text ):
287
    "To sort sanely/alphabetically: sorted( l, key=natural )"
288
    def num( s ):
289
        "Convert text segment to int if necessary"
290
        return int( s ) if s.isdigit() else s
291
    return [  num( s ) for s in re.split( r'(\d+)', text ) ]
292

    
293
def naturalSeq( t ):
294
    "Natural sort key function for sequences"
295
    return [ natural( x ) for x in t ]
296

    
297
def numCores():
298
    "Returns number of CPU cores based on /proc/cpuinfo"
299
    if hasattr( numCores, 'ncores' ):
300
        return numCores.ncores
301
    try:
302
        numCores.ncores = int( quietRun('grep -c processor /proc/cpuinfo') )
303
    except ValueError:
304
        return 0
305
    return numCores.ncores
306

    
307
def irange(start, end):
308
    """Inclusive range from start to end (vs. Python insanity.)
309
       irange(1,5) -> 1, 2, 3, 4, 5"""
310
    return range( start, end + 1 )
311

    
312
def custom( cls, **params ):
313
    "Returns customized constructor for class cls."
314
    def customized( *args, **kwargs):
315
        "Customized constructor"
316
        kwargs.update( params )
317
        return cls( *args, **kwargs )
318
    return customized