mininet / mininet / node.py @ 686a9993
History | View | Annotate | Download (53 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 |
HostWithPrivateDirs: a virtual host that has user-specified private
|
20 |
directories. These may be temporary directories stored as a tmpfs,
|
21 |
or persistent directories that are mounted from another directory in
|
22 |
the root filesystem.
|
23 |
|
24 |
Switch: superclass for switch nodes.
|
25 |
|
26 |
UserSwitch: a switch using the user-space switch from the OpenFlow
|
27 |
reference implementation.
|
28 |
|
29 |
KernelSwitch: a switch using the kernel switch from the OpenFlow reference
|
30 |
implementation.
|
31 |
|
32 |
OVSSwitch: a switch using the OpenVSwitch OpenFlow-compatible switch
|
33 |
implementation (openvswitch.org).
|
34 |
|
35 |
Controller: superclass for OpenFlow controllers. The default controller
|
36 |
is controller(8) from the reference implementation.
|
37 |
|
38 |
NOXController: a controller node using NOX (noxrepo.org).
|
39 |
|
40 |
RemoteController: a remote controller node, which may use any
|
41 |
arbitrary OpenFlow-compatible controller, and which is not
|
42 |
created or managed by mininet.
|
43 |
|
44 |
Future enhancements:
|
45 |
|
46 |
- Possibly make Node, Switch and Controller more abstract so that
|
47 |
they can be used for both local and remote nodes
|
48 |
|
49 |
- Create proxy objects for remote nodes (Mininet: Cluster Edition)
|
50 |
"""
|
51 |
|
52 |
import os |
53 |
import pty |
54 |
import re |
55 |
import signal |
56 |
import select |
57 |
from subprocess import Popen, PIPE, STDOUT |
58 |
from operator import or_ |
59 |
from time import sleep |
60 |
|
61 |
from mininet.log import info, error, warn, debug |
62 |
from mininet.util import ( quietRun, errRun, errFail, moveIntf, isShellBuiltin, |
63 |
numCores, retry, mountCgroups ) |
64 |
from mininet.moduledeps import moduleDeps, pathCheck, OVS_KMOD, OF_KMOD, TUN |
65 |
from mininet.link import Link, Intf, TCIntf |
66 |
from re import findall |
67 |
from distutils.version import StrictVersion |
68 |
|
69 |
class Node( object ): |
70 |
"""A virtual network node is simply a shell in a network namespace.
|
71 |
We communicate with it using pipes."""
|
72 |
|
73 |
portBase = 0 # Nodes always start with eth0/port0, even in OF 1.0 |
74 |
|
75 |
def __init__( self, name, inNamespace=True, **params ): |
76 |
"""name: name of node
|
77 |
inNamespace: in network namespace?
|
78 |
params: Node parameters (see config() for details)"""
|
79 |
|
80 |
# Make sure class actually works
|
81 |
self.checkSetup()
|
82 |
|
83 |
self.name = params.get( 'name', name ) |
84 |
self.inNamespace = params.get( 'inNamespace', inNamespace ) |
85 |
|
86 |
# Stash configuration parameters for future reference
|
87 |
self.params = params
|
88 |
|
89 |
self.intfs = {} # dict of port numbers to interfaces |
90 |
self.ports = {} # dict of interfaces to port numbers |
91 |
# replace with Port objects, eventually ?
|
92 |
self.nameToIntf = {} # dict of interface names to Intfs |
93 |
|
94 |
# Make pylint happy
|
95 |
( self.shell, self.execed, self.pid, self.stdin, self.stdout, |
96 |
self.lastPid, self.lastCmd, self.pollOut ) = ( |
97 |
None, None, None, None, None, None, None, None ) |
98 |
self.waiting = False |
99 |
self.readbuf = '' |
100 |
|
101 |
# Start command interpreter shell
|
102 |
self.startShell()
|
103 |
|
104 |
# File descriptor to node mapping support
|
105 |
# Class variables and methods
|
106 |
|
107 |
inToNode = {} # mapping of input fds to nodes
|
108 |
outToNode = {} # mapping of output fds to nodes
|
109 |
|
110 |
@classmethod
|
111 |
def fdToNode( cls, fd ): |
112 |
"""Return node corresponding to given file descriptor.
|
113 |
fd: file descriptor
|
114 |
returns: node"""
|
115 |
node = cls.outToNode.get( fd ) |
116 |
return node or cls.inToNode.get( fd ) |
117 |
|
118 |
# Command support via shell process in namespace
|
119 |
def startShell( self, mnopts=None ): |
120 |
"Start a shell process for running commands"
|
121 |
if self.shell: |
122 |
error( "%s: shell is already running\n" % self.name ) |
123 |
return
|
124 |
# mnexec: (c)lose descriptors, (d)etach from tty,
|
125 |
# (p)rint pid, and run in (n)amespace
|
126 |
opts = '-cd' if mnopts is None else mnopts |
127 |
if self.inNamespace: |
128 |
opts += 'n'
|
129 |
# bash -m: enable job control, i: force interactive
|
130 |
# -s: pass $* to shell, and make process easy to find in ps
|
131 |
# prompt is set to sentinel chr( 127 )
|
132 |
cmd = [ 'mnexec', opts, 'env', 'PS1=' + chr( 127 ), |
133 |
'bash', '--norc', '-mis', 'mininet:' + self.name ] |
134 |
# Spawn a shell subprocess in a pseudo-tty, to disable buffering
|
135 |
# in the subprocess and insulate it from signals (e.g. SIGINT)
|
136 |
# received by the parent
|
137 |
master, slave = pty.openpty() |
138 |
self.shell = self._popen( cmd, stdin=slave, stdout=slave, stderr=slave, |
139 |
close_fds=False )
|
140 |
self.stdin = os.fdopen( master, 'rw' ) |
141 |
self.stdout = self.stdin |
142 |
self.pid = self.shell.pid |
143 |
self.pollOut = select.poll()
|
144 |
self.pollOut.register( self.stdout ) |
145 |
# Maintain mapping between file descriptors and nodes
|
146 |
# This is useful for monitoring multiple nodes
|
147 |
# using select.poll()
|
148 |
self.outToNode[ self.stdout.fileno() ] = self |
149 |
self.inToNode[ self.stdin.fileno() ] = self |
150 |
self.execed = False |
151 |
self.lastCmd = None |
152 |
self.lastPid = None |
153 |
self.readbuf = '' |
154 |
# Wait for prompt
|
155 |
while True: |
156 |
data = self.read( 1024 ) |
157 |
if data[ -1 ] == chr( 127 ): |
158 |
break
|
159 |
self.pollOut.poll()
|
160 |
self.waiting = False |
161 |
self.cmd( 'stty -echo' ) |
162 |
self.cmd( 'set +m' ) |
163 |
|
164 |
def _popen( self, cmd, **params ): |
165 |
"""Internal method: spawn and return a process
|
166 |
cmd: command to run (list)
|
167 |
params: parameters to Popen()"""
|
168 |
return Popen( cmd, **params )
|
169 |
|
170 |
def cleanup( self ): |
171 |
"Help python collect its garbage."
|
172 |
# Intfs may end up in root NS
|
173 |
for intfName in self.intfNames(): |
174 |
if self.name in intfName: |
175 |
quietRun( 'ip link del ' + intfName )
|
176 |
self.shell = None |
177 |
|
178 |
# Subshell I/O, commands and control
|
179 |
|
180 |
def read( self, maxbytes=1024 ): |
181 |
"""Buffered read from node, non-blocking.
|
182 |
maxbytes: maximum number of bytes to return"""
|
183 |
count = len( self.readbuf ) |
184 |
if count < maxbytes:
|
185 |
data = os.read( self.stdout.fileno(), maxbytes - count )
|
186 |
self.readbuf += data
|
187 |
if maxbytes >= len( self.readbuf ): |
188 |
result = self.readbuf
|
189 |
self.readbuf = '' |
190 |
else:
|
191 |
result = self.readbuf[ :maxbytes ]
|
192 |
self.readbuf = self.readbuf[ maxbytes: ] |
193 |
return result
|
194 |
|
195 |
def readline( self ): |
196 |
"""Buffered readline from node, non-blocking.
|
197 |
returns: line (minus newline) or None"""
|
198 |
self.readbuf += self.read( 1024 ) |
199 |
if '\n' not in self.readbuf: |
200 |
return None |
201 |
pos = self.readbuf.find( '\n' ) |
202 |
line = self.readbuf[ 0: pos ] |
203 |
self.readbuf = self.readbuf[ pos + 1: ] |
204 |
return line
|
205 |
|
206 |
def write( self, data ): |
207 |
"""Write data to node.
|
208 |
data: string"""
|
209 |
os.write( self.stdin.fileno(), data )
|
210 |
|
211 |
def terminate( self ): |
212 |
"Send kill signal to Node and clean up after it."
|
213 |
if self.shell: |
214 |
if self.shell.poll() is None: |
215 |
os.killpg( self.shell.pid, signal.SIGHUP )
|
216 |
self.cleanup()
|
217 |
|
218 |
def stop( self ): |
219 |
"Stop node."
|
220 |
self.terminate()
|
221 |
|
222 |
def waitReadable( self, timeoutms=None ): |
223 |
"""Wait until node's output is readable.
|
224 |
timeoutms: timeout in ms or None to wait indefinitely."""
|
225 |
if len( self.readbuf ) == 0: |
226 |
self.pollOut.poll( timeoutms )
|
227 |
|
228 |
def sendCmd( self, *args, **kwargs ): |
229 |
"""Send a command, followed by a command to echo a sentinel,
|
230 |
and return without waiting for the command to complete.
|
231 |
args: command and arguments, or string
|
232 |
printPid: print command's PID?"""
|
233 |
assert not self.waiting |
234 |
printPid = kwargs.get( 'printPid', True ) |
235 |
# Allow sendCmd( [ list ] )
|
236 |
if len( args ) == 1 and type( args[ 0 ] ) is list: |
237 |
cmd = args[ 0 ]
|
238 |
# Allow sendCmd( cmd, arg1, arg2... )
|
239 |
elif len( args ) > 0: |
240 |
cmd = args |
241 |
# Convert to string
|
242 |
if not isinstance( cmd, str ): |
243 |
cmd = ' '.join( [ str( c ) for c in cmd ] ) |
244 |
if not re.search( r'\w', cmd ): |
245 |
# Replace empty commands with something harmless
|
246 |
cmd = 'echo -n'
|
247 |
self.lastCmd = cmd
|
248 |
# if a builtin command is backgrounded, it still yields a PID
|
249 |
if len( cmd ) > 0 and cmd[ -1 ] == '&': |
250 |
# print ^A{pid}\n so monitor() can set lastPid
|
251 |
cmd += ' printf "\\001%d\\012" $! '
|
252 |
elif printPid and not isShellBuiltin( cmd ): |
253 |
cmd = 'mnexec -p ' + cmd
|
254 |
self.write( cmd + '\n' ) |
255 |
self.lastPid = None |
256 |
self.waiting = True |
257 |
|
258 |
def sendInt( self, intr=chr( 3 ) ): |
259 |
"Interrupt running command."
|
260 |
debug( 'sendInt: writing chr(%d)\n' % ord( intr ) ) |
261 |
self.write( intr )
|
262 |
|
263 |
def monitor( self, timeoutms=None, findPid=True ): |
264 |
"""Monitor and return the output of a command.
|
265 |
Set self.waiting to False if command has completed.
|
266 |
timeoutms: timeout in ms or None to wait indefinitely
|
267 |
findPid: look for PID from mnexec -p"""
|
268 |
self.waitReadable( timeoutms )
|
269 |
data = self.read( 1024 ) |
270 |
pidre = r'\[\d+\] \d+\r\n'
|
271 |
# Look for PID
|
272 |
marker = chr( 1 ) + r'\d+\r\n' |
273 |
if findPid and chr( 1 ) in data: |
274 |
# suppress the job and PID of a backgrounded command
|
275 |
if re.findall( pidre, data ):
|
276 |
data = re.sub( pidre, '', data )
|
277 |
# Marker can be read in chunks; continue until all of it is read
|
278 |
while not re.findall( marker, data ): |
279 |
data += self.read( 1024 ) |
280 |
markers = re.findall( marker, data ) |
281 |
if markers:
|
282 |
self.lastPid = int( markers[ 0 ][ 1: ] ) |
283 |
data = re.sub( marker, '', data )
|
284 |
# Look for sentinel/EOF
|
285 |
if len( data ) > 0 and data[ -1 ] == chr( 127 ): |
286 |
self.waiting = False |
287 |
data = data[ :-1 ]
|
288 |
elif chr( 127 ) in data: |
289 |
self.waiting = False |
290 |
data = data.replace( chr( 127 ), '' ) |
291 |
return data
|
292 |
|
293 |
def waitOutput( self, verbose=False, findPid=True ): |
294 |
"""Wait for a command to complete.
|
295 |
Completion is signaled by a sentinel character, ASCII(127)
|
296 |
appearing in the output stream. Wait for the sentinel and return
|
297 |
the output, including trailing newline.
|
298 |
verbose: print output interactively"""
|
299 |
log = info if verbose else debug |
300 |
output = ''
|
301 |
while self.waiting: |
302 |
data = self.monitor()
|
303 |
output += data |
304 |
log( data ) |
305 |
return output
|
306 |
|
307 |
def cmd( self, *args, **kwargs ): |
308 |
"""Send a command, wait for output, and return it.
|
309 |
cmd: string"""
|
310 |
verbose = kwargs.get( 'verbose', False ) |
311 |
log = info if verbose else debug |
312 |
log( '*** %s : %s\n' % ( self.name, args ) ) |
313 |
self.sendCmd( *args, **kwargs )
|
314 |
return self.waitOutput( verbose ) |
315 |
|
316 |
def cmdPrint( self, *args): |
317 |
"""Call cmd and printing its output
|
318 |
cmd: string"""
|
319 |
return self.cmd( *args, **{ 'verbose': True } ) |
320 |
|
321 |
def popen( self, *args, **kwargs ): |
322 |
"""Return a Popen() object in our namespace
|
323 |
args: Popen() args, single list, or string
|
324 |
kwargs: Popen() keyword args"""
|
325 |
defaults = { 'stdout': PIPE, 'stderr': PIPE, |
326 |
'mncmd':
|
327 |
[ 'mnexec', '-da', str( self.pid ) ] } |
328 |
defaults.update( kwargs ) |
329 |
if len( args ) == 1: |
330 |
if type( args[ 0 ] ) is list: |
331 |
# popen([cmd, arg1, arg2...])
|
332 |
cmd = args[ 0 ]
|
333 |
elif type( args[ 0 ] ) is str: |
334 |
# popen("cmd arg1 arg2...")
|
335 |
cmd = args[ 0 ].split()
|
336 |
else:
|
337 |
raise Exception( 'popen() requires a string or list' ) |
338 |
elif len( args ) > 0: |
339 |
# popen( cmd, arg1, arg2... )
|
340 |
cmd = list( args )
|
341 |
# Attach to our namespace using mnexec -a
|
342 |
cmd = defaults.pop( 'mncmd' ) + cmd
|
343 |
# Shell requires a string, not a list!
|
344 |
if defaults.get( 'shell', False ): |
345 |
cmd = ' '.join( cmd )
|
346 |
popen = self._popen( cmd, **defaults )
|
347 |
return popen
|
348 |
|
349 |
def pexec( self, *args, **kwargs ): |
350 |
"""Execute a command using popen
|
351 |
returns: out, err, exitcode"""
|
352 |
popen = self.popen( *args, stdin=PIPE, stdout=PIPE, stderr=PIPE,
|
353 |
**kwargs ) |
354 |
# Warning: this can fail with large numbers of fds!
|
355 |
out, err = popen.communicate() |
356 |
exitcode = popen.wait() |
357 |
return out, err, exitcode
|
358 |
|
359 |
# Interface management, configuration, and routing
|
360 |
|
361 |
# BL notes: This might be a bit redundant or over-complicated.
|
362 |
# However, it does allow a bit of specialization, including
|
363 |
# changing the canonical interface names. It's also tricky since
|
364 |
# the real interfaces are created as veth pairs, so we can't
|
365 |
# make a single interface at a time.
|
366 |
|
367 |
def newPort( self ): |
368 |
"Return the next port number to allocate."
|
369 |
if len( self.ports ) > 0: |
370 |
return max( self.ports.values() ) + 1 |
371 |
return self.portBase |
372 |
|
373 |
def addIntf( self, intf, port=None, moveIntfFn=moveIntf ): |
374 |
"""Add an interface.
|
375 |
intf: interface
|
376 |
port: port number (optional, typically OpenFlow port number)
|
377 |
moveIntfFn: function to move interface (optional)"""
|
378 |
if port is None: |
379 |
port = self.newPort()
|
380 |
self.intfs[ port ] = intf
|
381 |
self.ports[ intf ] = port
|
382 |
self.nameToIntf[ intf.name ] = intf
|
383 |
debug( '\n' )
|
384 |
debug( 'added intf %s (%d) to node %s\n' % (
|
385 |
intf, port, self.name ) )
|
386 |
if self.inNamespace: |
387 |
debug( 'moving', intf, 'into namespace for', self.name, '\n' ) |
388 |
moveIntfFn( intf.name, self )
|
389 |
|
390 |
def defaultIntf( self ): |
391 |
"Return interface for lowest port"
|
392 |
ports = self.intfs.keys()
|
393 |
if ports:
|
394 |
return self.intfs[ min( ports ) ] |
395 |
else:
|
396 |
warn( '*** defaultIntf: warning:', self.name, |
397 |
'has no interfaces\n' )
|
398 |
|
399 |
def intf( self, intf='' ): |
400 |
"""Return our interface object with given string name,
|
401 |
default intf if name is falsy (None, empty string, etc).
|
402 |
or the input intf arg.
|
403 |
|
404 |
Having this fcn return its arg for Intf objects makes it
|
405 |
easier to construct functions with flexible input args for
|
406 |
interfaces (those that accept both string names and Intf objects).
|
407 |
"""
|
408 |
if not intf: |
409 |
return self.defaultIntf() |
410 |
elif type( intf ) is str: |
411 |
return self.nameToIntf[ intf ] |
412 |
else:
|
413 |
return intf
|
414 |
|
415 |
def connectionsTo( self, node): |
416 |
"Return [ intf1, intf2... ] for all intfs that connect self to node."
|
417 |
# We could optimize this if it is important
|
418 |
connections = [] |
419 |
for intf in self.intfList(): |
420 |
link = intf.link |
421 |
if link:
|
422 |
node1, node2 = link.intf1.node, link.intf2.node |
423 |
if node1 == self and node2 == node: |
424 |
connections += [ ( intf, link.intf2 ) ] |
425 |
elif node1 == node and node2 == self: |
426 |
connections += [ ( intf, link.intf1 ) ] |
427 |
return connections
|
428 |
|
429 |
def deleteIntfs( self, checkName=True ): |
430 |
"""Delete all of our interfaces.
|
431 |
checkName: only delete interfaces that contain our name"""
|
432 |
# In theory the interfaces should go away after we shut down.
|
433 |
# However, this takes time, so we're better off removing them
|
434 |
# explicitly so that we won't get errors if we run before they
|
435 |
# have been removed by the kernel. Unfortunately this is very slow,
|
436 |
# at least with Linux kernels before 2.6.33
|
437 |
for intf in self.intfs.values(): |
438 |
# Protect against deleting hardware interfaces
|
439 |
if ( self.name in intf.name ) or ( not checkName ): |
440 |
intf.delete() |
441 |
info( '.' )
|
442 |
|
443 |
# Routing support
|
444 |
|
445 |
def setARP( self, ip, mac ): |
446 |
"""Add an ARP entry.
|
447 |
ip: IP address as string
|
448 |
mac: MAC address as string"""
|
449 |
result = self.cmd( 'arp', '-s', ip, mac ) |
450 |
return result
|
451 |
|
452 |
def setHostRoute( self, ip, intf ): |
453 |
"""Add route to host.
|
454 |
ip: IP address as dotted decimal
|
455 |
intf: string, interface name"""
|
456 |
return self.cmd( 'route add -host', ip, 'dev', intf ) |
457 |
|
458 |
def setDefaultRoute( self, intf=None ): |
459 |
"""Set the default route to go through intf.
|
460 |
intf: Intf or {dev <intfname> via <gw-ip> ...}"""
|
461 |
# Note setParam won't call us if intf is none
|
462 |
if type( intf ) is str and ' ' in intf: |
463 |
params = intf |
464 |
else:
|
465 |
params = 'dev %s' % intf
|
466 |
self.cmd( 'ip route del default' ) |
467 |
return self.cmd( 'ip route add default', params ) |
468 |
|
469 |
# Convenience and configuration methods
|
470 |
|
471 |
def setMAC( self, mac, intf=None ): |
472 |
"""Set the MAC address for an interface.
|
473 |
intf: intf or intf name
|
474 |
mac: MAC address as string"""
|
475 |
return self.intf( intf ).setMAC( mac ) |
476 |
|
477 |
def setIP( self, ip, prefixLen=8, intf=None ): |
478 |
"""Set the IP address for an interface.
|
479 |
intf: intf or intf name
|
480 |
ip: IP address as a string
|
481 |
prefixLen: prefix length, e.g. 8 for /8 or 16M addrs"""
|
482 |
# This should probably be rethought
|
483 |
if '/' not in ip: |
484 |
ip = '%s/%s' % ( ip, prefixLen )
|
485 |
return self.intf( intf ).setIP( ip ) |
486 |
|
487 |
def IP( self, intf=None ): |
488 |
"Return IP address of a node or specific interface."
|
489 |
return self.intf( intf ).IP() |
490 |
|
491 |
def MAC( self, intf=None ): |
492 |
"Return MAC address of a node or specific interface."
|
493 |
return self.intf( intf ).MAC() |
494 |
|
495 |
def intfIsUp( self, intf=None ): |
496 |
"Check if an interface is up."
|
497 |
return self.intf( intf ).isUp() |
498 |
|
499 |
# The reason why we configure things in this way is so
|
500 |
# That the parameters can be listed and documented in
|
501 |
# the config method.
|
502 |
# Dealing with subclasses and superclasses is slightly
|
503 |
# annoying, but at least the information is there!
|
504 |
|
505 |
def setParam( self, results, method, **param ): |
506 |
"""Internal method: configure a *single* parameter
|
507 |
results: dict of results to update
|
508 |
method: config method name
|
509 |
param: arg=value (ignore if value=None)
|
510 |
value may also be list or dict"""
|
511 |
name, value = param.items()[ 0 ]
|
512 |
f = getattr( self, method, None ) |
513 |
if not f or value is None: |
514 |
return
|
515 |
if type( value ) is list: |
516 |
result = f( *value ) |
517 |
elif type( value ) is dict: |
518 |
result = f( **value ) |
519 |
else:
|
520 |
result = f( value ) |
521 |
results[ name ] = result |
522 |
return result
|
523 |
|
524 |
def config( self, mac=None, ip=None, |
525 |
defaultRoute=None, lo='up', **_params ): |
526 |
"""Configure Node according to (optional) parameters:
|
527 |
mac: MAC address for default interface
|
528 |
ip: IP address for default interface
|
529 |
ifconfig: arbitrary interface configuration
|
530 |
Subclasses should override this method and call
|
531 |
the parent class's config(**params)"""
|
532 |
# If we were overriding this method, we would call
|
533 |
# the superclass config method here as follows:
|
534 |
# r = Parent.config( **_params )
|
535 |
r = {} |
536 |
self.setParam( r, 'setMAC', mac=mac ) |
537 |
self.setParam( r, 'setIP', ip=ip ) |
538 |
self.setParam( r, 'setDefaultRoute', defaultRoute=defaultRoute ) |
539 |
# This should be examined
|
540 |
self.cmd( 'ifconfig lo ' + lo ) |
541 |
return r
|
542 |
|
543 |
def configDefault( self, **moreParams ): |
544 |
"Configure with default parameters"
|
545 |
self.params.update( moreParams )
|
546 |
self.config( **self.params ) |
547 |
|
548 |
# This is here for backward compatibility
|
549 |
def linkTo( self, node, link=Link ): |
550 |
"""(Deprecated) Link to another node
|
551 |
replace with Link( node1, node2)"""
|
552 |
return link( self, node ) |
553 |
|
554 |
# Other methods
|
555 |
|
556 |
def intfList( self ): |
557 |
"List of our interfaces sorted by port number"
|
558 |
return [ self.intfs[ p ] for p in sorted( self.intfs.iterkeys() ) ] |
559 |
|
560 |
def intfNames( self ): |
561 |
"The names of our interfaces sorted by port number"
|
562 |
return [ str( i ) for i in self.intfList() ] |
563 |
|
564 |
def __repr__( self ): |
565 |
"More informative string representation"
|
566 |
intfs = ( ','.join( [ '%s:%s' % ( i.name, i.IP() ) |
567 |
for i in self.intfList() ] ) ) |
568 |
return '<%s %s: %s pid=%s> ' % ( |
569 |
self.__class__.__name__, self.name, intfs, self.pid ) |
570 |
|
571 |
def __str__( self ): |
572 |
"Abbreviated string representation"
|
573 |
return self.name |
574 |
|
575 |
# Automatic class setup support
|
576 |
|
577 |
isSetup = False
|
578 |
|
579 |
@classmethod
|
580 |
def checkSetup( cls ): |
581 |
"Make sure our class and superclasses are set up"
|
582 |
while cls and not getattr( cls, 'isSetup', True ): |
583 |
cls.setup() |
584 |
cls.isSetup = True
|
585 |
# Make pylint happy
|
586 |
cls = getattr( type( cls ), '__base__', None ) |
587 |
|
588 |
@classmethod
|
589 |
def setup( cls ): |
590 |
"Make sure our class dependencies are available"
|
591 |
pathCheck( 'mnexec', 'ifconfig', moduleName='Mininet') |
592 |
|
593 |
|
594 |
class Host( Node ): |
595 |
"A host is simply a Node"
|
596 |
pass
|
597 |
|
598 |
|
599 |
class CPULimitedHost( Host ): |
600 |
|
601 |
"CPU limited host"
|
602 |
|
603 |
def __init__( self, name, sched='cfs', **kwargs ): |
604 |
Host.__init__( self, name, **kwargs )
|
605 |
# Initialize class if necessary
|
606 |
if not CPULimitedHost.inited: |
607 |
CPULimitedHost.init() |
608 |
# Create a cgroup and move shell into it
|
609 |
self.cgroup = 'cpu,cpuacct,cpuset:/' + self.name |
610 |
errFail( 'cgcreate -g ' + self.cgroup ) |
611 |
# We don't add ourselves to a cpuset because you must
|
612 |
# specify the cpu and memory placement first
|
613 |
errFail( 'cgclassify -g cpu,cpuacct:/%s %s' % ( self.name, self.pid ) ) |
614 |
# BL: Setting the correct period/quota is tricky, particularly
|
615 |
# for RT. RT allows very small quotas, but the overhead
|
616 |
# seems to be high. CFS has a mininimum quota of 1 ms, but
|
617 |
# still does better with larger period values.
|
618 |
self.period_us = kwargs.get( 'period_us', 100000 ) |
619 |
self.sched = sched
|
620 |
if self.sched == 'rt': |
621 |
release = quietRun( 'uname -r' ).strip('\r\n') |
622 |
output = quietRun( 'grep CONFIG_RT_GROUP_SCHED /boot/config-%s' % release )
|
623 |
if output == '# CONFIG_RT_GROUP_SCHED is not set\n': |
624 |
error( '\n*** error: please enable RT_GROUP_SCHED in your kernel\n' )
|
625 |
exit( 1 ) |
626 |
self.rtprio = 20 |
627 |
|
628 |
def cgroupSet( self, param, value, resource='cpu' ): |
629 |
"Set a cgroup parameter and return its value"
|
630 |
cmd = 'cgset -r %s.%s=%s /%s' % (
|
631 |
resource, param, value, self.name )
|
632 |
quietRun( cmd ) |
633 |
nvalue = int( self.cgroupGet( param, resource ) ) |
634 |
if nvalue != value:
|
635 |
error( '*** error: cgroupSet: %s set to %s instead of %s\n'
|
636 |
% ( param, nvalue, value ) ) |
637 |
return nvalue
|
638 |
|
639 |
def cgroupGet( self, param, resource='cpu' ): |
640 |
"Return value of cgroup parameter"
|
641 |
cmd = 'cgget -r %s.%s /%s' % (
|
642 |
resource, param, self.name )
|
643 |
return int( quietRun( cmd ).split()[ -1 ] ) |
644 |
|
645 |
def cgroupDel( self ): |
646 |
"Clean up our cgroup"
|
647 |
# info( '*** deleting cgroup', self.cgroup, '\n' )
|
648 |
_out, _err, exitcode = errRun( 'cgdelete -r ' + self.cgroup ) |
649 |
return exitcode != 0 |
650 |
|
651 |
def popen( self, *args, **kwargs ): |
652 |
"""Return a Popen() object in node's namespace
|
653 |
args: Popen() args, single list, or string
|
654 |
kwargs: Popen() keyword args"""
|
655 |
# Tell mnexec to execute command in our cgroup
|
656 |
mncmd = [ 'mnexec', '-da', str( self.pid ), |
657 |
'-g', self.name ] |
658 |
if self.sched == 'rt': |
659 |
mncmd += [ '-r', str( self.rtprio ) ] |
660 |
return Host.popen( self, *args, mncmd=mncmd, **kwargs ) |
661 |
|
662 |
def cleanup( self ): |
663 |
"Clean up Node, then clean up our cgroup"
|
664 |
super( CPULimitedHost, self ).cleanup() |
665 |
retry( retries=3, delaySecs=1, fn=self.cgroupDel ) |
666 |
|
667 |
def chrt( self ): |
668 |
"Set RT scheduling priority"
|
669 |
quietRun( 'chrt -p %s %s' % ( self.rtprio, self.pid ) ) |
670 |
result = quietRun( 'chrt -p %s' % self.pid ) |
671 |
firstline = result.split( '\n' )[ 0 ] |
672 |
lastword = firstline.split( ' ' )[ -1 ] |
673 |
if lastword != 'SCHED_RR': |
674 |
error( '*** error: could not assign SCHED_RR to %s\n' % self.name ) |
675 |
return lastword
|
676 |
|
677 |
def rtInfo( self, f ): |
678 |
"Internal method: return parameters for RT bandwidth"
|
679 |
pstr, qstr = 'rt_period_us', 'rt_runtime_us' |
680 |
# RT uses wall clock time for period and quota
|
681 |
quota = int( self.period_us * f ) |
682 |
return pstr, qstr, self.period_us, quota |
683 |
|
684 |
def cfsInfo( self, f): |
685 |
"Internal method: return parameters for CFS bandwidth"
|
686 |
pstr, qstr = 'cfs_period_us', 'cfs_quota_us' |
687 |
# CFS uses wall clock time for period and CPU time for quota.
|
688 |
quota = int( self.period_us * f * numCores() ) |
689 |
period = self.period_us
|
690 |
if f > 0 and quota < 1000: |
691 |
debug( '(cfsInfo: increasing default period) ' )
|
692 |
quota = 1000
|
693 |
period = int( quota / f / numCores() )
|
694 |
return pstr, qstr, period, quota
|
695 |
|
696 |
# BL comment:
|
697 |
# This may not be the right API,
|
698 |
# since it doesn't specify CPU bandwidth in "absolute"
|
699 |
# units the way link bandwidth is specified.
|
700 |
# We should use MIPS or SPECINT or something instead.
|
701 |
# Alternatively, we should change from system fraction
|
702 |
# to CPU seconds per second, essentially assuming that
|
703 |
# all CPUs are the same.
|
704 |
|
705 |
def setCPUFrac( self, f=-1, sched=None): |
706 |
"""Set overall CPU fraction for this host
|
707 |
f: CPU bandwidth limit (fraction)
|
708 |
sched: 'rt' or 'cfs'
|
709 |
Note 'cfs' requires CONFIG_CFS_BANDWIDTH"""
|
710 |
if not f: |
711 |
return
|
712 |
if not sched: |
713 |
sched = self.sched
|
714 |
if sched == 'rt': |
715 |
pstr, qstr, period, quota = self.rtInfo( f )
|
716 |
elif sched == 'cfs': |
717 |
pstr, qstr, period, quota = self.cfsInfo( f )
|
718 |
else:
|
719 |
return
|
720 |
if quota < 0: |
721 |
# Reset to unlimited
|
722 |
quota = -1
|
723 |
# Set cgroup's period and quota
|
724 |
self.cgroupSet( pstr, period )
|
725 |
self.cgroupSet( qstr, quota )
|
726 |
if sched == 'rt': |
727 |
# Set RT priority if necessary
|
728 |
self.chrt()
|
729 |
info( '(%s %d/%dus) ' % ( sched, quota, period ) )
|
730 |
|
731 |
def setCPUs( self, cores, mems=0 ): |
732 |
"Specify (real) cores that our cgroup can run on"
|
733 |
if type( cores ) is list: |
734 |
cores = ','.join( [ str( c ) for c in cores ] ) |
735 |
self.cgroupSet( resource='cpuset', param='cpus', |
736 |
value=cores ) |
737 |
# Memory placement is probably not relevant, but we
|
738 |
# must specify it anyway
|
739 |
self.cgroupSet( resource='cpuset', param='mems', |
740 |
value=mems) |
741 |
# We have to do this here after we've specified
|
742 |
# cpus and mems
|
743 |
errFail( 'cgclassify -g cpuset:/%s %s' % (
|
744 |
self.name, self.pid ) ) |
745 |
|
746 |
def config( self, cpu=None, cores=None, **params ): |
747 |
"""cpu: desired overall system CPU fraction
|
748 |
cores: (real) core(s) this host can run on
|
749 |
params: parameters for Node.config()"""
|
750 |
r = Node.config( self, **params )
|
751 |
# Was considering cpu={'cpu': cpu , 'sched': sched}, but
|
752 |
# that seems redundant
|
753 |
self.setParam( r, 'setCPUFrac', cpu=cpu ) |
754 |
self.setParam( r, 'setCPUs', cores=cores ) |
755 |
return r
|
756 |
|
757 |
inited = False
|
758 |
|
759 |
@classmethod
|
760 |
def init( cls ): |
761 |
"Initialization for CPULimitedHost class"
|
762 |
mountCgroups() |
763 |
cls.inited = True
|
764 |
|
765 |
class HostWithPrivateDirs( Host ): |
766 |
"Host with private directories"
|
767 |
|
768 |
def __init__( self, name, *args, **kwargs ): |
769 |
"privateDirs: list of private directory strings or tuples"
|
770 |
self.name = name
|
771 |
self.privateDirs = kwargs.pop( 'privateDirs', [] ) |
772 |
Host.__init__( self, name, *args, **kwargs )
|
773 |
self.mountPrivateDirs()
|
774 |
|
775 |
def mountPrivateDirs( self ): |
776 |
"mount private directories"
|
777 |
for directory in self.privateDirs: |
778 |
if isinstance( directory, tuple ): |
779 |
# mount given private directory
|
780 |
privateDir = directory[ 1 ] % self.__dict__ |
781 |
mountPoint = directory[ 0 ]
|
782 |
self.cmd( 'mkdir -p %s' % privateDir ) |
783 |
self.cmd( 'mkdir -p %s' % mountPoint ) |
784 |
self.cmd( 'mount --bind %s %s' % |
785 |
( privateDir, mountPoint ) ) |
786 |
else:
|
787 |
# mount temporary filesystem on directory
|
788 |
self.cmd( 'mkdir -p %s' % directory ) |
789 |
self.cmd( 'mount -n -t tmpfs tmpfs %s' % directory ) |
790 |
|
791 |
|
792 |
|
793 |
# Some important things to note:
|
794 |
#
|
795 |
# The "IP" address which setIP() assigns to the switch is not
|
796 |
# an "IP address for the switch" in the sense of IP routing.
|
797 |
# Rather, it is the IP address for the control interface,
|
798 |
# on the control network, and it is only relevant to the
|
799 |
# controller. If you are running in the root namespace
|
800 |
# (which is the only way to run OVS at the moment), the
|
801 |
# control interface is the loopback interface, and you
|
802 |
# normally never want to change its IP address!
|
803 |
#
|
804 |
# In general, you NEVER want to attempt to use Linux's
|
805 |
# network stack (i.e. ifconfig) to "assign" an IP address or
|
806 |
# MAC address to a switch data port. Instead, you "assign"
|
807 |
# the IP and MAC addresses in the controller by specifying
|
808 |
# packets that you want to receive or send. The "MAC" address
|
809 |
# reported by ifconfig for a switch data port is essentially
|
810 |
# meaningless. It is important to understand this if you
|
811 |
# want to create a functional router using OpenFlow.
|
812 |
|
813 |
class Switch( Node ): |
814 |
"""A Switch is a Node that is running (or has execed?)
|
815 |
an OpenFlow switch."""
|
816 |
|
817 |
portBase = 1 # Switches start with port 1 in OpenFlow |
818 |
dpidLen = 16 # digits in dpid passed to switch |
819 |
|
820 |
def __init__( self, name, dpid=None, opts='', listenPort=None, **params): |
821 |
"""dpid: dpid hex string (or None to derive from name, e.g. s1 -> 1)
|
822 |
opts: additional switch options
|
823 |
listenPort: port to listen on for dpctl connections"""
|
824 |
Node.__init__( self, name, **params )
|
825 |
self.dpid = self.defaultDpid( dpid ) |
826 |
self.opts = opts
|
827 |
self.listenPort = listenPort
|
828 |
if not self.inNamespace: |
829 |
self.controlIntf = Intf( 'lo', self, port=0 ) |
830 |
|
831 |
def defaultDpid( self, dpid=None ): |
832 |
"Return correctly formatted dpid from dpid or switch name (s1 -> 1)"
|
833 |
if dpid:
|
834 |
# Remove any colons and make sure it's a good hex number
|
835 |
dpid = dpid.translate( None, ':' ) |
836 |
assert len( dpid ) <= self.dpidLen and int( dpid, 16 ) >= 0 |
837 |
else:
|
838 |
# Use hex of the first number in the switch name
|
839 |
nums = re.findall( r'\d+', self.name ) |
840 |
if nums:
|
841 |
dpid = hex( int( nums[ 0 ] ) )[ 2: ] |
842 |
else:
|
843 |
raise Exception( 'Unable to derive default datapath ID - ' |
844 |
'please either specify a dpid or use a '
|
845 |
'canonical switch name such as s23.' )
|
846 |
return '0' * ( self.dpidLen - len( dpid ) ) + dpid |
847 |
|
848 |
def defaultIntf( self ): |
849 |
"Return control interface"
|
850 |
if self.controlIntf: |
851 |
return self.controlIntf |
852 |
else:
|
853 |
return Node.defaultIntf( self ) |
854 |
|
855 |
def sendCmd( self, *cmd, **kwargs ): |
856 |
"""Send command to Node.
|
857 |
cmd: string"""
|
858 |
kwargs.setdefault( 'printPid', False ) |
859 |
if not self.execed: |
860 |
return Node.sendCmd( self, *cmd, **kwargs ) |
861 |
else:
|
862 |
error( '*** Error: %s has execed and cannot accept commands' %
|
863 |
self.name )
|
864 |
|
865 |
def connected( self ): |
866 |
"Is the switch connected to a controller? (override this method)"
|
867 |
return False and self # satisfy pylint |
868 |
|
869 |
def __repr__( self ): |
870 |
"More informative string representation"
|
871 |
intfs = ( ','.join( [ '%s:%s' % ( i.name, i.IP() ) |
872 |
for i in self.intfList() ] ) ) |
873 |
return '<%s %s: %s pid=%s> ' % ( |
874 |
self.__class__.__name__, self.name, intfs, self.pid ) |
875 |
|
876 |
class UserSwitch( Switch ): |
877 |
"User-space switch."
|
878 |
|
879 |
dpidLen = 12
|
880 |
|
881 |
def __init__( self, name, dpopts='--no-slicing', **kwargs ): |
882 |
"""Init.
|
883 |
name: name for the switch
|
884 |
dpopts: additional arguments to ofdatapath (--no-slicing)"""
|
885 |
Switch.__init__( self, name, **kwargs )
|
886 |
pathCheck( 'ofdatapath', 'ofprotocol', |
887 |
moduleName='the OpenFlow reference user switch' +
|
888 |
'(openflow.org)' )
|
889 |
if self.listenPort: |
890 |
self.opts += ' --listen=ptcp:%i ' % self.listenPort |
891 |
else:
|
892 |
self.opts += ' --listen=punix:/tmp/%s.listen' % self.name |
893 |
self.dpopts = dpopts
|
894 |
|
895 |
@classmethod
|
896 |
def setup( cls ): |
897 |
"Ensure any dependencies are loaded; if not, try to load them."
|
898 |
if not os.path.exists( '/dev/net/tun' ): |
899 |
moduleDeps( add=TUN ) |
900 |
|
901 |
def dpctl( self, *args ): |
902 |
"Run dpctl command"
|
903 |
listenAddr = None
|
904 |
if not self.listenPort: |
905 |
listenAddr = 'unix:/tmp/%s.listen' % self.name |
906 |
else:
|
907 |
listenAddr = 'tcp:127.0.0.1:%i' % self.listenPort |
908 |
return self.cmd( 'dpctl ' + ' '.join( args ) + |
909 |
' ' + listenAddr )
|
910 |
|
911 |
def connected( self ): |
912 |
"Is the switch connected to a controller?"
|
913 |
status = self.dpctl( 'status' ) |
914 |
return ( 'remote.is-connected=true' in status and |
915 |
'local.is-connected=true' in status ) |
916 |
|
917 |
@staticmethod
|
918 |
def TCReapply( intf ): |
919 |
"""Unfortunately user switch and Mininet are fighting
|
920 |
over tc queuing disciplines. To resolve the conflict,
|
921 |
we re-create the user switch's configuration, but as a
|
922 |
leaf of the TCIntf-created configuration."""
|
923 |
if type( intf ) is TCIntf: |
924 |
ifspeed = 10000000000 # 10 Gbps |
925 |
minspeed = ifspeed * 0.001
|
926 |
|
927 |
res = intf.config( **intf.params ) |
928 |
|
929 |
if res is None: # link may not have TC parameters |
930 |
return
|
931 |
|
932 |
# Re-add qdisc, root, and default classes user switch created, but
|
933 |
# with new parent, as setup by Mininet's TCIntf
|
934 |
parent = res['parent']
|
935 |
intf.tc( "%s qdisc add dev %s " + parent +
|
936 |
" handle 1: htb default 0xfffe" )
|
937 |
intf.tc( "%s class add dev %s classid 1:0xffff parent 1: htb rate "
|
938 |
+ str(ifspeed) )
|
939 |
intf.tc( "%s class add dev %s classid 1:0xfffe parent 1:0xffff " +
|
940 |
"htb rate " + str(minspeed) + " ceil " + str(ifspeed) ) |
941 |
|
942 |
def start( self, controllers ): |
943 |
"""Start OpenFlow reference user datapath.
|
944 |
Log to /tmp/sN-{ofd,ofp}.log.
|
945 |
controllers: list of controller objects"""
|
946 |
# Add controllers
|
947 |
clist = ','.join( [ 'tcp:%s:%d' % ( c.IP(), c.port ) |
948 |
for c in controllers ] ) |
949 |
ofdlog = '/tmp/' + self.name + '-ofd.log' |
950 |
ofplog = '/tmp/' + self.name + '-ofp.log' |
951 |
intfs = [ str( i ) for i in self.intfList() if not i.IP() ] |
952 |
self.cmd( 'ofdatapath -i ' + ','.join( intfs ) + |
953 |
' punix:/tmp/' + self.name + ' -d %s ' % self.dpid + |
954 |
self.dpopts +
|
955 |
' 1> ' + ofdlog + ' 2> ' + ofdlog + ' &' ) |
956 |
self.cmd( 'ofprotocol unix:/tmp/' + self.name + |
957 |
' ' + clist +
|
958 |
' --fail=closed ' + self.opts + |
959 |
' 1> ' + ofplog + ' 2>' + ofplog + ' &' ) |
960 |
if "no-slicing" not in self.dpopts: |
961 |
# Only TCReapply if slicing is enable
|
962 |
sleep(1) # Allow ofdatapath to start before re-arranging qdisc's |
963 |
for intf in self.intfList(): |
964 |
if not intf.IP(): |
965 |
self.TCReapply( intf )
|
966 |
|
967 |
def stop( self ): |
968 |
"Stop OpenFlow reference user datapath."
|
969 |
self.cmd( 'kill %ofdatapath' ) |
970 |
self.cmd( 'kill %ofprotocol' ) |
971 |
self.deleteIntfs()
|
972 |
|
973 |
|
974 |
class OVSLegacyKernelSwitch( Switch ): |
975 |
"""Open VSwitch legacy kernel-space switch using ovs-openflowd.
|
976 |
Currently only works in the root namespace."""
|
977 |
|
978 |
def __init__( self, name, dp=None, **kwargs ): |
979 |
"""Init.
|
980 |
name: name for switch
|
981 |
dp: netlink id (0, 1, 2, ...)
|
982 |
defaultMAC: default MAC as unsigned int; random value if None"""
|
983 |
Switch.__init__( self, name, **kwargs )
|
984 |
self.dp = dp if dp else self.name |
985 |
self.intf = self.dp |
986 |
if self.inNamespace: |
987 |
error( "OVSKernelSwitch currently only works"
|
988 |
" in the root namespace.\n" )
|
989 |
exit( 1 ) |
990 |
|
991 |
@classmethod
|
992 |
def setup( cls ): |
993 |
"Ensure any dependencies are loaded; if not, try to load them."
|
994 |
pathCheck( 'ovs-dpctl', 'ovs-openflowd', |
995 |
moduleName='Open vSwitch (openvswitch.org)')
|
996 |
moduleDeps( subtract=OF_KMOD, add=OVS_KMOD ) |
997 |
|
998 |
def start( self, controllers ): |
999 |
"Start up kernel datapath."
|
1000 |
ofplog = '/tmp/' + self.name + '-ofp.log' |
1001 |
# Delete local datapath if it exists;
|
1002 |
# then create a new one monitoring the given interfaces
|
1003 |
self.cmd( 'ovs-dpctl del-dp ' + self.dp ) |
1004 |
self.cmd( 'ovs-dpctl add-dp ' + self.dp ) |
1005 |
intfs = [ str( i ) for i in self.intfList() if not i.IP() ] |
1006 |
self.cmd( 'ovs-dpctl', 'add-if', self.dp, ' '.join( intfs ) ) |
1007 |
# Run protocol daemon
|
1008 |
clist = ','.join( [ 'tcp:%s:%d' % ( c.IP(), c.port ) |
1009 |
for c in controllers ] ) |
1010 |
self.cmd( 'ovs-openflowd ' + self.dp + |
1011 |
' ' + clist +
|
1012 |
' --fail=secure ' + self.opts + |
1013 |
' --datapath-id=' + self.dpid + |
1014 |
' 1>' + ofplog + ' 2>' + ofplog + '&' ) |
1015 |
self.execed = False |
1016 |
|
1017 |
def stop( self ): |
1018 |
"Terminate kernel datapath."
|
1019 |
quietRun( 'ovs-dpctl del-dp ' + self.dp ) |
1020 |
self.cmd( 'kill %ovs-openflowd' ) |
1021 |
self.deleteIntfs()
|
1022 |
|
1023 |
|
1024 |
class OVSSwitch( Switch ): |
1025 |
"Open vSwitch switch. Depends on ovs-vsctl."
|
1026 |
|
1027 |
def __init__( self, name, failMode='secure', datapath='kernel', |
1028 |
inband=False, protocols=None, **params ): |
1029 |
"""Init.
|
1030 |
name: name for switch
|
1031 |
failMode: controller loss behavior (secure|open)
|
1032 |
datapath: userspace or kernel mode (kernel|user)
|
1033 |
inband: use in-band control (False)"""
|
1034 |
Switch.__init__( self, name, **params )
|
1035 |
self.failMode = failMode
|
1036 |
self.datapath = datapath
|
1037 |
self.inband = inband
|
1038 |
self.protocols = protocols
|
1039 |
|
1040 |
@classmethod
|
1041 |
def setup( cls ): |
1042 |
"Make sure Open vSwitch is installed and working"
|
1043 |
pathCheck( 'ovs-vsctl',
|
1044 |
moduleName='Open vSwitch (openvswitch.org)')
|
1045 |
# This should no longer be needed, and it breaks
|
1046 |
# with OVS 1.7 which has renamed the kernel module:
|
1047 |
# moduleDeps( subtract=OF_KMOD, add=OVS_KMOD )
|
1048 |
out, err, exitcode = errRun( 'ovs-vsctl -t 1 show' )
|
1049 |
if exitcode:
|
1050 |
error( out + err + |
1051 |
'ovs-vsctl exited with code %d\n' % exitcode +
|
1052 |
'*** Error connecting to ovs-db with ovs-vsctl\n'
|
1053 |
'Make sure that Open vSwitch is installed, '
|
1054 |
'that ovsdb-server is running, and that\n'
|
1055 |
'"ovs-vsctl show" works correctly.\n'
|
1056 |
'You may wish to try '
|
1057 |
'"service openvswitch-switch start".\n' )
|
1058 |
exit( 1 ) |
1059 |
info = quietRun( 'ovs-vsctl --version' )
|
1060 |
cls.OVSVersion = findall( '\d+\.\d+', info )[ 0 ] |
1061 |
|
1062 |
@classmethod
|
1063 |
def isOldOVS( cls ): |
1064 |
return ( StrictVersion( cls.OVSVersion ) <
|
1065 |
StrictVersion( '1.10' ) )
|
1066 |
|
1067 |
@classmethod
|
1068 |
def batchShutdown( cls, switches ): |
1069 |
"Call ovs-vsctl del-br on all OVSSwitches in a list"
|
1070 |
quietRun( 'ovs-vsctl ' +
|
1071 |
' -- '.join( '--if-exists del-br %s' % s |
1072 |
for s in switches ) ) |
1073 |
|
1074 |
def dpctl( self, *args ): |
1075 |
"Run ovs-ofctl command"
|
1076 |
return self.cmd( 'ovs-ofctl', args[ 0 ], self, *args[ 1: ] ) |
1077 |
|
1078 |
@staticmethod
|
1079 |
def TCReapply( intf ): |
1080 |
"""Unfortunately OVS and Mininet are fighting
|
1081 |
over tc queuing disciplines. As a quick hack/
|
1082 |
workaround, we clear OVS's and reapply our own."""
|
1083 |
if type( intf ) is TCIntf: |
1084 |
intf.config( **intf.params ) |
1085 |
|
1086 |
def attach( self, intf ): |
1087 |
"Connect a data port"
|
1088 |
self.cmd( 'ovs-vsctl add-port', self, intf ) |
1089 |
self.cmd( 'ifconfig', intf, 'up' ) |
1090 |
self.TCReapply( intf )
|
1091 |
|
1092 |
def detach( self, intf ): |
1093 |
"Disconnect a data port"
|
1094 |
self.cmd( 'ovs-vsctl del-port', self, intf ) |
1095 |
|
1096 |
def controllerUUIDs( self ): |
1097 |
"Return ovsdb UUIDs for our controllers"
|
1098 |
uuids = [] |
1099 |
controllers = self.cmd( 'ovs-vsctl -- get Bridge', self, |
1100 |
'Controller' ).strip()
|
1101 |
if controllers.startswith( '[' ) and controllers.endswith( ']' ): |
1102 |
controllers = controllers[ 1 : -1 ] |
1103 |
uuids = [ c.strip() for c in controllers.split( ',' ) ] |
1104 |
return uuids
|
1105 |
|
1106 |
def connected( self ): |
1107 |
"Are we connected to at least one of our controllers?"
|
1108 |
results = [ 'true' in self.cmd( 'ovs-vsctl -- get Controller', |
1109 |
uuid, 'is_connected' )
|
1110 |
for uuid in self.controllerUUIDs() ] |
1111 |
return reduce( or_, results, False ) |
1112 |
|
1113 |
def start( self, controllers ): |
1114 |
"Start up a new OVS OpenFlow switch using ovs-vsctl"
|
1115 |
if self.inNamespace: |
1116 |
raise Exception( |
1117 |
'OVS kernel switch does not work in a namespace' )
|
1118 |
# Annoyingly, --if-exists option seems not to work
|
1119 |
self.cmd( 'ovs-vsctl del-br', self ) |
1120 |
int( self.dpid, 16 ) # DPID must be a hex string |
1121 |
# Interfaces and controllers
|
1122 |
intfs = ' '.join( '-- add-port %s %s ' % ( self, intf ) + |
1123 |
'-- set Interface %s ' % intf +
|
1124 |
'ofport_request=%s ' % self.ports[ intf ] |
1125 |
for intf in self.intfList() |
1126 |
if self.ports[ intf ] and not intf.IP() ) |
1127 |
clist = ' '.join( '%s:%s:%d' % ( c.protocol, c.IP(), c.port ) |
1128 |
for c in controllers ) |
1129 |
if self.listenPort: |
1130 |
clist += ' ptcp:%s' % self.listenPort |
1131 |
# Construct big ovs-vsctl command for new versions of OVS
|
1132 |
if not self.isOldOVS(): |
1133 |
cmd = ( 'ovs-vsctl add-br %s ' % self + |
1134 |
'-- set Bridge %s ' % self + |
1135 |
'other_config:datapath-id=%s ' % self.dpid + |
1136 |
'-- set-fail-mode %s %s ' % ( self, self.failMode ) + |
1137 |
intfs + |
1138 |
'-- set-controller %s %s ' % ( self, clist ) ) |
1139 |
# Construct ovs-vsctl commands for old versions of OVS
|
1140 |
else:
|
1141 |
self.cmd( 'ovs-vsctl add-br', self ) |
1142 |
for intf in self.intfList(): |
1143 |
if not intf.IP(): |
1144 |
self.cmd( 'ovs-vsctl add-port', self, intf ) |
1145 |
cmd = ( 'ovs-vsctl set Bridge %s ' % self + |
1146 |
'other_config:datapath-id=%s ' % self.dpid + |
1147 |
'-- set-fail-mode %s %s ' % ( self, self.failMode ) + |
1148 |
'-- set-controller %s %s ' % ( self, clist ) ) |
1149 |
if not self.inband: |
1150 |
cmd += ( '-- set bridge %s '
|
1151 |
'other-config:disable-in-band=true ' % self ) |
1152 |
if self.datapath == 'user': |
1153 |
cmd += '-- set bridge %s datapath_type=netdev ' % self |
1154 |
if self.protocols: |
1155 |
cmd += '-- set bridge %s protocols=%s' % ( self, self.protocols ) |
1156 |
# Reconnect quickly to controllers (1s vs. 15s max_backoff)
|
1157 |
for uuid in self.controllerUUIDs(): |
1158 |
if uuid.count( '-' ) != 4: |
1159 |
# Doesn't look like a UUID
|
1160 |
continue
|
1161 |
uuid = uuid.strip() |
1162 |
cmd += '-- set Controller %smax_backoff=1000 ' % uuid
|
1163 |
# Do it!!
|
1164 |
self.cmd( cmd )
|
1165 |
for intf in self.intfList(): |
1166 |
self.TCReapply( intf )
|
1167 |
|
1168 |
|
1169 |
def stop( self ): |
1170 |
"Terminate OVS switch."
|
1171 |
self.cmd( 'ovs-vsctl del-br', self ) |
1172 |
if self.datapath == 'user': |
1173 |
self.cmd( 'ip link del', self ) |
1174 |
self.deleteIntfs()
|
1175 |
|
1176 |
OVSKernelSwitch = OVSSwitch |
1177 |
|
1178 |
|
1179 |
class IVSSwitch(Switch): |
1180 |
"""IVS virtual switch"""
|
1181 |
|
1182 |
def __init__( self, name, verbose=True, **kwargs ): |
1183 |
Switch.__init__( self, name, **kwargs )
|
1184 |
self.verbose = verbose
|
1185 |
|
1186 |
@classmethod
|
1187 |
def setup( cls ): |
1188 |
"Make sure IVS is installed"
|
1189 |
pathCheck( 'ivs-ctl', 'ivs', |
1190 |
moduleName="Indigo Virtual Switch (projectfloodlight.org)" )
|
1191 |
out, err, exitcode = errRun( 'ivs-ctl show' )
|
1192 |
if exitcode:
|
1193 |
error( out + err + |
1194 |
'ivs-ctl exited with code %d\n' % exitcode +
|
1195 |
'*** The openvswitch kernel module might '
|
1196 |
'not be loaded. Try modprobe openvswitch.\n' )
|
1197 |
exit( 1 ) |
1198 |
|
1199 |
@classmethod
|
1200 |
def batchShutdown( cls, switches ): |
1201 |
"Kill each IVS switch, to be waited on later in stop()"
|
1202 |
for switch in switches: |
1203 |
switch.cmd( 'kill %ivs' )
|
1204 |
|
1205 |
def start( self, controllers ): |
1206 |
"Start up a new IVS switch"
|
1207 |
args = ['ivs']
|
1208 |
args.extend( ['--name', self.name] ) |
1209 |
args.extend( ['--dpid', self.dpid] ) |
1210 |
if self.verbose: |
1211 |
args.extend( ['--verbose'] )
|
1212 |
for intf in self.intfs.values(): |
1213 |
if not intf.IP(): |
1214 |
args.extend( ['-i', intf.name] )
|
1215 |
for c in controllers: |
1216 |
args.extend( ['-c', '%s:%d' % (c.IP(), c.port)] ) |
1217 |
if self.listenPort: |
1218 |
args.extend( ['--listen', '127.0.0.1:%i' % self.listenPort] ) |
1219 |
args.append( self.opts )
|
1220 |
|
1221 |
logfile = '/tmp/ivs.%s.log' % self.name |
1222 |
|
1223 |
self.cmd( ' '.join(args) + ' >' + logfile + ' 2>&1 </dev/null &' ) |
1224 |
|
1225 |
def stop( self ): |
1226 |
"Terminate IVS switch."
|
1227 |
self.cmd( 'kill %ivs' ) |
1228 |
self.cmd( 'wait' ) |
1229 |
self.deleteIntfs()
|
1230 |
|
1231 |
def attach( self, intf ): |
1232 |
"Connect a data port"
|
1233 |
self.cmd( 'ivs-ctl', 'add-port', '--datapath', self.name, intf ) |
1234 |
|
1235 |
def detach( self, intf ): |
1236 |
"Disconnect a data port"
|
1237 |
self.cmd( 'ivs-ctl', 'del-port', '--datapath', self.name, intf ) |
1238 |
|
1239 |
def dpctl( self, *args ): |
1240 |
"Run dpctl command"
|
1241 |
if not self.listenPort: |
1242 |
return "can't run dpctl without passive listening port" |
1243 |
return self.cmd( 'ovs-ofctl ' + ' '.join( args ) + |
1244 |
' tcp:127.0.0.1:%i' % self.listenPort ) |
1245 |
|
1246 |
|
1247 |
class Controller( Node ): |
1248 |
"""A Controller is a Node that is running (or has execed?) an
|
1249 |
OpenFlow controller."""
|
1250 |
|
1251 |
def __init__( self, name, inNamespace=False, command='controller', |
1252 |
cargs='-v ptcp:%d', cdir=None, ip="127.0.0.1", |
1253 |
port=6633, protocol='tcp', **params ): |
1254 |
self.command = command
|
1255 |
self.cargs = cargs
|
1256 |
self.cdir = cdir
|
1257 |
self.ip = ip
|
1258 |
self.port = port
|
1259 |
self.protocol = protocol
|
1260 |
Node.__init__( self, name, inNamespace=inNamespace,
|
1261 |
ip=ip, **params ) |
1262 |
self.checkListening()
|
1263 |
|
1264 |
def checkListening( self ): |
1265 |
"Make sure no controllers are running on our port"
|
1266 |
# Verify that Telnet is installed first:
|
1267 |
out, _err, returnCode = errRun( "which telnet" )
|
1268 |
if 'telnet' not in out or returnCode != 0: |
1269 |
raise Exception( "Error running telnet to check for listening " |
1270 |
"controllers; please check that it is "
|
1271 |
"installed." )
|
1272 |
listening = self.cmd( "echo A | telnet -e A %s %d" % |
1273 |
( self.ip, self.port ) ) |
1274 |
if 'Connected' in listening: |
1275 |
servers = self.cmd( 'netstat -natp' ).split( '\n' ) |
1276 |
pstr = ':%d ' % self.port |
1277 |
clist = servers[ 0:1 ] + [ s for s in servers if pstr in s ] |
1278 |
raise Exception( "Please shut down the controller which is" |
1279 |
" running on port %d:\n" % self.port + |
1280 |
'\n'.join( clist ) )
|
1281 |
|
1282 |
def start( self ): |
1283 |
"""Start <controller> <args> on controller.
|
1284 |
Log to /tmp/cN.log"""
|
1285 |
pathCheck( self.command )
|
1286 |
cout = '/tmp/' + self.name + '.log' |
1287 |
if self.cdir is not None: |
1288 |
self.cmd( 'cd ' + self.cdir ) |
1289 |
self.cmd( self.command + ' ' + self.cargs % self.port + |
1290 |
' 1>' + cout + ' 2>' + cout + ' &' ) |
1291 |
self.execed = False |
1292 |
|
1293 |
def stop( self ): |
1294 |
"Stop controller."
|
1295 |
self.cmd( 'kill %' + self.command ) |
1296 |
self.terminate()
|
1297 |
|
1298 |
def IP( self, intf=None ): |
1299 |
"Return IP address of the Controller"
|
1300 |
if self.intfs: |
1301 |
ip = Node.IP( self, intf )
|
1302 |
else:
|
1303 |
ip = self.ip
|
1304 |
return ip
|
1305 |
|
1306 |
def __repr__( self ): |
1307 |
"More informative string representation"
|
1308 |
return '<%s %s: %s:%s pid=%s> ' % ( |
1309 |
self.__class__.__name__, self.name, |
1310 |
self.IP(), self.port, self.pid ) |
1311 |
@classmethod
|
1312 |
def isAvailable( self ): |
1313 |
return quietRun( 'which controller' ) |
1314 |
|
1315 |
class OVSController( Controller ): |
1316 |
"Open vSwitch controller"
|
1317 |
def __init__( self, name, command='ovs-controller', **kwargs ): |
1318 |
if quietRun( 'which test-controller' ): |
1319 |
command = 'test-controller'
|
1320 |
Controller.__init__( self, name, command=command, **kwargs )
|
1321 |
@classmethod
|
1322 |
def isAvailable( self ): |
1323 |
return quietRun( 'which ovs-controller' ) or quietRun( 'which test-controller' ) |
1324 |
|
1325 |
class NOX( Controller ): |
1326 |
"Controller to run a NOX application."
|
1327 |
|
1328 |
def __init__( self, name, *noxArgs, **kwargs ): |
1329 |
"""Init.
|
1330 |
name: name to give controller
|
1331 |
noxArgs: arguments (strings) to pass to NOX"""
|
1332 |
if not noxArgs: |
1333 |
warn( 'warning: no NOX modules specified; '
|
1334 |
'running packetdump only\n' )
|
1335 |
noxArgs = [ 'packetdump' ]
|
1336 |
elif type( noxArgs ) not in ( list, tuple ): |
1337 |
noxArgs = [ noxArgs ] |
1338 |
|
1339 |
if 'NOX_CORE_DIR' not in os.environ: |
1340 |
exit( 'exiting; please set missing NOX_CORE_DIR env var' ) |
1341 |
noxCoreDir = os.environ[ 'NOX_CORE_DIR' ]
|
1342 |
|
1343 |
Controller.__init__( self, name,
|
1344 |
command=noxCoreDir + '/nox_core',
|
1345 |
cargs='--libdir=/usr/local/lib -v -i ptcp:%s ' +
|
1346 |
' '.join( noxArgs ),
|
1347 |
cdir=noxCoreDir, |
1348 |
**kwargs ) |
1349 |
|
1350 |
class RYU( Controller ): |
1351 |
"Controller to run Ryu application"
|
1352 |
def __init__( self, name, *ryuArgs, **kwargs ): |
1353 |
"""Init.
|
1354 |
name: name to give controller.
|
1355 |
ryuArgs: arguments and modules to pass to Ryu"""
|
1356 |
homeDir = quietRun( 'printenv HOME' ).strip( '\r\n' ) |
1357 |
ryuCoreDir = '%s/ryu/ryu/app/' % homeDir
|
1358 |
if not ryuArgs: |
1359 |
warn( 'warning: no Ryu modules specified; '
|
1360 |
'running simple_switch only\n' )
|
1361 |
ryuArgs = [ ryuCoreDir + 'simple_switch.py' ]
|
1362 |
elif type( ryuArgs ) not in ( list, tuple ): |
1363 |
ryuArgs = [ ryuArgs ] |
1364 |
|
1365 |
Controller.__init__( self, name,
|
1366 |
command='ryu-manager',
|
1367 |
cargs='--ofp-tcp-listen-port %s ' +
|
1368 |
' '.join( ryuArgs ),
|
1369 |
cdir=ryuCoreDir, |
1370 |
**kwargs ) |
1371 |
|
1372 |
class RemoteController( Controller ): |
1373 |
"Controller running outside of Mininet's control."
|
1374 |
|
1375 |
def __init__( self, name, ip='127.0.0.1', |
1376 |
port=6633, **kwargs):
|
1377 |
"""Init.
|
1378 |
name: name to give controller
|
1379 |
ip: the IP address where the remote controller is
|
1380 |
listening
|
1381 |
port: the port where the remote controller is listening"""
|
1382 |
Controller.__init__( self, name, ip=ip, port=port, **kwargs )
|
1383 |
|
1384 |
def start( self ): |
1385 |
"Overridden to do nothing."
|
1386 |
return
|
1387 |
|
1388 |
def stop( self ): |
1389 |
"Overridden to do nothing."
|
1390 |
return
|
1391 |
|
1392 |
def checkListening( self ): |
1393 |
"Warn if remote controller is not accessible"
|
1394 |
listening = self.cmd( "echo A | telnet -e A %s %d" % |
1395 |
( self.ip, self.port ) ) |
1396 |
if 'Connected' not in listening: |
1397 |
warn( "Unable to contact the remote controller"
|
1398 |
" at %s:%d\n" % ( self.ip, self.port ) ) |
1399 |
|
1400 |
def DefaultController( name, order=[ Controller, OVSController ], **kwargs ): |
1401 |
"find any controller that is available and run it"
|
1402 |
for controller in order: |
1403 |
if controller.isAvailable():
|
1404 |
return controller( name, **kwargs )
|