blob: d170caae44057a96714e5b92a9a66faa8584436e [file] [log] [blame]
Bob Lantz087b5d92016-05-06 11:39:04 -07001#!/usr/bin/python
2
3"""
4onos.py: ONOS cluster and control network in Mininet
5
6With onos.py, you can use Mininet to create a complete
7ONOS network, including an ONOS cluster with a modeled
8control network as well as the usual data nework.
9
10This is intended to be useful for distributed ONOS
11development and testing in the case that you require
12a modeled control network.
13
14Invocation (using OVS as default switch):
15
16mn --custom onos.py --controller onos,3 --topo torus,4,4
17
18Or with the user switch (or CPqD if installed):
19
20mn --custom onos.py --controller onos,3 \
21 --switch onosuser --topo torus,4,4
22
Bob Lantzbb37d872016-05-16 16:26:13 -070023Currently you meed to use a custom switch class
Bob Lantz087b5d92016-05-06 11:39:04 -070024because Mininet's Switch() class does't (yet?) handle
25controllers with multiple IP addresses directly.
26
27The classes may also be imported and used via Mininet's
28python API.
29
30Bugs/Gripes:
31- We need --switch onosuser for the user switch because
32 Switch() doesn't currently handle Controller objects
33 with multiple IP addresses.
34- ONOS startup and configuration is painful/undocumented.
35- Too many ONOS environment vars - do we need them all?
36- ONOS cluster startup is very, very slow. If Linux can
37 boot in 4 seconds, why can't ONOS?
38- It's a pain to mess with the control network from the
39 CLI
40- Setting a default controller for Mininet should be easier
41"""
42
43from mininet.node import Controller, OVSSwitch, UserSwitch
44from mininet.nodelib import LinuxBridge
45from mininet.net import Mininet
46from mininet.topo import SingleSwitchTopo, Topo
Bob Lantz64382422016-06-03 22:51:39 -070047from mininet.log import setLogLevel, info, warn, error, debug
Bob Lantz087b5d92016-05-06 11:39:04 -070048from mininet.cli import CLI
Bob Lantz64382422016-06-03 22:51:39 -070049from mininet.util import quietRun, specialClass
Bob Lantz087b5d92016-05-06 11:39:04 -070050from mininet.examples.controlnet import MininetFacade
51
52from os import environ
53from os.path import dirname, join, isfile
54from sys import argv
55from glob import glob
56import time
Bob Lantz64382422016-06-03 22:51:39 -070057from functools import partial
Bob Lantz92d8e052016-06-17 16:03:58 -070058
Bob Lantz087b5d92016-05-06 11:39:04 -070059
60### ONOS Environment
61
Bob Lantzbb37d872016-05-16 16:26:13 -070062KarafPort = 8101 # ssh port indicating karaf is running
63GUIPort = 8181 # GUI/REST port
64OpenFlowPort = 6653 # OpenFlow port
Jon Halla43d0332016-08-04 15:02:23 -070065CopycatPort = 9876 # Copycat port
Bob Lantz087b5d92016-05-06 11:39:04 -070066
67def defaultUser():
68 "Return a reasonable default user"
69 if 'SUDO_USER' in environ:
70 return environ[ 'SUDO_USER' ]
71 try:
72 user = quietRun( 'who am i' ).split()[ 0 ]
73 except:
74 user = 'nobody'
75 return user
76
Bob Lantz087b5d92016-05-06 11:39:04 -070077# Module vars, initialized below
Bob Lantz4b51d5c2016-05-27 14:47:38 -070078HOME = ONOS_ROOT = ONOS_USER = None
Bob Lantz087b5d92016-05-06 11:39:04 -070079ONOS_APPS = ONOS_WEB_USER = ONOS_WEB_PASS = ONOS_TAR = None
80
81def initONOSEnv():
82 """Initialize ONOS environment (and module) variables
83 This is ugly and painful, but they have to be set correctly
84 in order for the onos-setup-karaf script to work.
85 nodes: list of ONOS nodes
86 returns: ONOS environment variable dict"""
87 # pylint: disable=global-statement
Bob Lantz4b51d5c2016-05-27 14:47:38 -070088 global HOME, ONOS_ROOT, ONOS_USER
Bob Lantz087b5d92016-05-06 11:39:04 -070089 global ONOS_APPS, ONOS_WEB_USER, ONOS_WEB_PASS
90 env = {}
91 def sd( var, val ):
92 "Set default value for environment variable"
93 env[ var ] = environ.setdefault( var, val )
94 return env[ var ]
Bob Lantz4b51d5c2016-05-27 14:47:38 -070095 assert environ[ 'HOME' ]
Bob Lantz087b5d92016-05-06 11:39:04 -070096 HOME = sd( 'HOME', environ[ 'HOME' ] )
Bob Lantz087b5d92016-05-06 11:39:04 -070097 ONOS_ROOT = sd( 'ONOS_ROOT', join( HOME, 'onos' ) )
Bob Lantz087b5d92016-05-06 11:39:04 -070098 environ[ 'ONOS_USER' ] = defaultUser()
99 ONOS_USER = sd( 'ONOS_USER', defaultUser() )
100 ONOS_APPS = sd( 'ONOS_APPS',
101 'drivers,openflow,fwd,proxyarp,mobility' )
102 # ONOS_WEB_{USER,PASS} isn't respected by onos-karaf:
103 environ.update( ONOS_WEB_USER='karaf', ONOS_WEB_PASS='karaf' )
104 ONOS_WEB_USER = sd( 'ONOS_WEB_USER', 'karaf' )
105 ONOS_WEB_PASS = sd( 'ONOS_WEB_PASS', 'karaf' )
106 return env
107
108
109def updateNodeIPs( env, nodes ):
110 "Update env dict and environ with node IPs"
111 # Get rid of stale junk
112 for var in 'ONOS_NIC', 'ONOS_CELL', 'ONOS_INSTANCES':
113 env[ var ] = ''
114 for var in environ.keys():
115 if var.startswith( 'OC' ):
116 env[ var ] = ''
117 for index, node in enumerate( nodes, 1 ):
118 var = 'OC%d' % index
119 env[ var ] = node.IP()
120 env[ 'OCI' ] = env[ 'OCN' ] = env[ 'OC1' ]
121 env[ 'ONOS_INSTANCES' ] = '\n'.join(
122 node.IP() for node in nodes )
123 environ.update( env )
124 return env
125
126
127tarDefaultPath = 'buck-out/gen/tools/package/onos-package/onos.tar.gz'
128
Bob Lantz1451d722016-05-17 14:40:07 -0700129def unpackONOS( destDir='/tmp', run=quietRun ):
Bob Lantz087b5d92016-05-06 11:39:04 -0700130 "Unpack ONOS and return its location"
131 global ONOS_TAR
132 environ.setdefault( 'ONOS_TAR', join( ONOS_ROOT, tarDefaultPath ) )
133 ONOS_TAR = environ[ 'ONOS_TAR' ]
134 tarPath = ONOS_TAR
135 if not isfile( tarPath ):
136 raise Exception( 'Missing ONOS tarball %s - run buck build onos?'
137 % tarPath )
138 info( '(unpacking %s)' % destDir)
Bob Lantza2ccaa52016-06-29 18:26:06 -0700139 success = '*** SUCCESS ***'
140 cmds = ( 'mkdir -p "%s" && cd "%s" && tar xzf "%s" && echo "%s"'
141 % ( destDir, destDir, tarPath, success ) )
142 result = run( cmds, shell=True, verbose=True )
143 if success not in result:
144 raise Exception( 'Failed to unpack ONOS archive %s in %s:\n%s\n' %
145 ( tarPath, destDir, result ) )
Bob Lantz1451d722016-05-17 14:40:07 -0700146 # We can use quietRun for this usually
147 tarOutput = quietRun( 'tar tzf "%s" | head -1' % tarPath, shell=True)
148 tarOutput = tarOutput.split()[ 0 ].strip()
149 assert '/' in tarOutput
150 onosDir = join( destDir, dirname( tarOutput ) )
Bob Lantz087b5d92016-05-06 11:39:04 -0700151 # Add symlink to log file
Bob Lantz1451d722016-05-17 14:40:07 -0700152 run( 'cd %s; ln -s onos*/apache* karaf;'
153 'ln -s karaf/data/log/karaf.log log' % destDir,
154 shell=True )
Bob Lantz087b5d92016-05-06 11:39:04 -0700155 return onosDir
156
157
Bob Lantz9ba19dc2016-06-13 20:22:07 -0700158def waitListening( server, port=80, callback=None, sleepSecs=.5,
159 proc='java' ):
160 "Simplified netstat version of waitListening"
161 while True:
162 lines = server.cmd( 'netstat -natp' ).strip().split( '\n' )
163 entries = [ line.split() for line in lines ]
164 portstr = ':%s' % port
165 listening = [ entry for entry in entries
166 if len( entry ) > 6 and portstr in entry[ 3 ]
167 and proc in entry[ 6 ] ]
168 if listening:
169 break
Bob Lantz64382422016-06-03 22:51:39 -0700170 info( '.' )
171 if callback:
172 callback()
173 time.sleep( sleepSecs )
Bob Lantz64382422016-06-03 22:51:39 -0700174
175
Bob Lantz087b5d92016-05-06 11:39:04 -0700176### Mininet classes
177
178def RenamedTopo( topo, *args, **kwargs ):
179 """Return specialized topo with renamed hosts
180 topo: topo class/class name to specialize
181 args, kwargs: topo args
182 sold: old switch name prefix (default 's')
183 snew: new switch name prefix
184 hold: old host name prefix (default 'h')
185 hnew: new host name prefix
186 This may be used from the mn command, e.g.
187 mn --topo renamed,single,spref=sw,hpref=host"""
188 sold = kwargs.pop( 'sold', 's' )
189 hold = kwargs.pop( 'hold', 'h' )
190 snew = kwargs.pop( 'snew', 'cs' )
191 hnew = kwargs.pop( 'hnew' ,'ch' )
192 topos = {} # TODO: use global TOPOS dict
193 if isinstance( topo, str ):
194 # Look up in topo directory - this allows us to
195 # use RenamedTopo from the command line!
196 if topo in topos:
197 topo = topos.get( topo )
198 else:
199 raise Exception( 'Unknown topo name: %s' % topo )
200 # pylint: disable=no-init
201 class RenamedTopoCls( topo ):
202 "Topo subclass with renamed nodes"
203 def addNode( self, name, *args, **kwargs ):
204 "Add a node, renaming if necessary"
205 if name.startswith( sold ):
206 name = snew + name[ len( sold ): ]
207 elif name.startswith( hold ):
208 name = hnew + name[ len( hold ): ]
209 return topo.addNode( self, name, *args, **kwargs )
210 return RenamedTopoCls( *args, **kwargs )
211
212
213class ONOSNode( Controller ):
214 "ONOS cluster node"
215
Bob Lantz087b5d92016-05-06 11:39:04 -0700216 def __init__( self, name, **kwargs ):
Bob Lantz64382422016-06-03 22:51:39 -0700217 "alertAction: exception|ignore|warn|exit (exception)"
Bob Lantz087b5d92016-05-06 11:39:04 -0700218 kwargs.update( inNamespace=True )
Bob Lantz64382422016-06-03 22:51:39 -0700219 self.alertAction = kwargs.pop( 'alertAction', 'exception' )
Bob Lantz087b5d92016-05-06 11:39:04 -0700220 Controller.__init__( self, name, **kwargs )
221 self.dir = '/tmp/%s' % self.name
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700222 self.client = self.dir + '/karaf/bin/client'
Bob Lantz087b5d92016-05-06 11:39:04 -0700223 self.ONOS_HOME = '/tmp'
Bob Lantz55562ea2016-06-17 18:19:03 -0700224 self.cmd( 'rm -rf', self.dir )
225 self.ONOS_HOME = unpackONOS( self.dir, run=self.ucmd )
Bob Lantz087b5d92016-05-06 11:39:04 -0700226
227 # pylint: disable=arguments-differ
228
Bob Lantz569bbec2016-06-03 18:51:16 -0700229 def start( self, env, nodes=() ):
Bob Lantz087b5d92016-05-06 11:39:04 -0700230 """Start ONOS on node
Bob Lantz569bbec2016-06-03 18:51:16 -0700231 env: environment var dict
232 nodes: all nodes in cluster"""
Bob Lantz087b5d92016-05-06 11:39:04 -0700233 env = dict( env )
Bob Lantz087b5d92016-05-06 11:39:04 -0700234 env.update( ONOS_HOME=self.ONOS_HOME )
235 self.updateEnv( env )
236 karafbin = glob( '%s/apache*/bin' % self.ONOS_HOME )[ 0 ]
237 onosbin = join( ONOS_ROOT, 'tools/test/bin' )
238 self.cmd( 'export PATH=%s:%s:$PATH' % ( onosbin, karafbin ) )
239 self.cmd( 'cd', self.ONOS_HOME )
Bob Lantz1451d722016-05-17 14:40:07 -0700240 self.ucmd( 'mkdir -p config && '
Bob Lantz569bbec2016-06-03 18:51:16 -0700241 'onos-gen-partitions config/cluster.json',
242 ' '.join( node.IP() for node in nodes ) )
Bob Lantz087b5d92016-05-06 11:39:04 -0700243 info( '(starting %s)' % self )
244 service = join( self.ONOS_HOME, 'bin/onos-service' )
Bob Lantz1451d722016-05-17 14:40:07 -0700245 self.ucmd( service, 'server 1>../onos.log 2>../onos.log'
246 ' & echo $! > onos.pid; ln -s `pwd`/onos.pid ..' )
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700247 self.onosPid = int( self.cmd( 'cat onos.pid' ).strip() )
Bob Lantz64382422016-06-03 22:51:39 -0700248 self.warningCount = 0
Bob Lantz087b5d92016-05-06 11:39:04 -0700249
250 # pylint: enable=arguments-differ
251
252 def stop( self ):
253 # XXX This will kill all karafs - too bad!
254 self.cmd( 'pkill -HUP -f karaf.jar && wait' )
255 self.cmd( 'rm -rf', self.dir )
256
Bob Lantz64382422016-06-03 22:51:39 -0700257 def sanityAlert( self, *args ):
258 "Alert to raise on sanityCheck failure"
259 info( '\n' )
260 if self.alertAction == 'exception':
261 raise Exception( *args )
262 if self.alertAction == 'warn':
263 warn( *args + ( '\n', ) )
264 elif self.alertAction == 'exit':
265 error( '***', *args +
266 ( '\nExiting. Run "sudo mn -c" to clean up.\n', ) )
267 exit( 1 )
268
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700269 def isRunning( self ):
270 "Is our ONOS process still running?"
Bob Lantz64382422016-06-03 22:51:39 -0700271 cmd = ( 'ps -p %d >/dev/null 2>&1 && echo "running" ||'
272 'echo "not running"' )
273 return self.cmd( cmd % self.onosPid ).strip() == 'running'
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700274
Bob Lantz64382422016-06-03 22:51:39 -0700275 def checkLog( self ):
276 "Return log file errors and warnings"
277 log = join( self.dir, 'log' )
Bob Lantzc96e2582016-06-13 18:57:04 -0700278 errors, warnings = [], []
Bob Lantz64382422016-06-03 22:51:39 -0700279 if isfile( log ):
280 lines = open( log ).read().split( '\n' )
281 errors = [ line for line in lines if 'ERROR' in line ]
282 warnings = [ line for line in lines if 'WARN'in line ]
283 return errors, warnings
284
285 def memAvailable( self ):
286 "Return available memory in KB (or -1 if we can't tell)"
287 lines = open( '/proc/meminfo' ).read().strip().split( '\n' )
288 entries = map( str.split, lines )
289 index = { entry[ 0 ]: entry for entry in entries }
290 # Check MemAvailable if present
291 default = ( None, '-1', 'kB' )
292 _name, count, unit = index.get( 'MemAvailable:', default )
293 if unit.lower() == 'kb':
294 return int( count )
295 return -1
296
297 def sanityCheck( self, lowMem=100000 ):
298 """Check whether we've quit or are running out of memory
299 lowMem: low memory threshold in KB (100000)"""
300 # Are we still running?
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700301 if not self.isRunning():
Bob Lantz64382422016-06-03 22:51:39 -0700302 self.sanityAlert( 'ONOS node %s has died' % self.name )
303 # Are there errors in the log file?
304 errors, warnings = self.checkLog()
305 if errors:
306 self.sanityAlert( 'ONOS startup errors:\n<<%s>>' %
307 '\n'.join( errors ) )
308 warningCount = len( warnings )
309 if warnings and warningCount > self.warningCount:
310 warn( '(%d warnings)' % len( warnings ) )
311 self.warningCount = warningCount
312 # Are we running out of memory?
313 mem = self.memAvailable()
314 if mem > 0 and mem < lowMem:
315 self.sanityAlert( 'Running out of memory (only %d KB available)'
316 % mem )
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700317
Bob Lantz087b5d92016-05-06 11:39:04 -0700318 def waitStarted( self ):
319 "Wait until we've really started"
320 info( '(checking: karaf' )
321 while True:
Bob Lantz1451d722016-05-17 14:40:07 -0700322 status = self.ucmd( 'karaf status' ).lower()
Bob Lantz087b5d92016-05-06 11:39:04 -0700323 if 'running' in status and 'not running' not in status:
324 break
325 info( '.' )
Bob Lantz64382422016-06-03 22:51:39 -0700326 self.sanityCheck()
Bob Lantz087b5d92016-05-06 11:39:04 -0700327 time.sleep( 1 )
328 info( ' ssh-port' )
Bob Lantz64382422016-06-03 22:51:39 -0700329 waitListening( server=self, port=KarafPort, callback=self.sanityCheck )
Bob Lantz087b5d92016-05-06 11:39:04 -0700330 info( ' openflow-port' )
Bob Lantz64382422016-06-03 22:51:39 -0700331 waitListening( server=self, port=OpenFlowPort,
332 callback=self.sanityCheck )
Bob Lantz087b5d92016-05-06 11:39:04 -0700333 info( ' client' )
334 while True:
Bob Lantz9ba19dc2016-06-13 20:22:07 -0700335 result = quietRun( '%s -h %s "apps -a"' %
Bob Lantzbb37d872016-05-16 16:26:13 -0700336 ( self.client, self.IP() ), shell=True )
Bob Lantz087b5d92016-05-06 11:39:04 -0700337 if 'openflow' in result:
338 break
339 info( '.' )
Bob Lantz64382422016-06-03 22:51:39 -0700340 self.sanityCheck()
Bob Lantz087b5d92016-05-06 11:39:04 -0700341 time.sleep( 1 )
Jon Halla43d0332016-08-04 15:02:23 -0700342 info( ' node-status' )
343 while True:
344 result = quietRun( '%s -h %s "nodes"' %
345 ( self.client, self.IP() ), shell=True )
346 nodeStr = 'id=%s, address=%s:%s, state=READY, updated' %\
347 ( self.IP(), self.IP(), CopycatPort )
348 if nodeStr in result:
349 break
350 info( '.' )
351 self.sanityCheck()
352 time.sleep( 1 )
Bob Lantz087b5d92016-05-06 11:39:04 -0700353 info( ')\n' )
354
355 def updateEnv( self, envDict ):
356 "Update environment variables"
Bob Lantz569bbec2016-06-03 18:51:16 -0700357 cmd = ';'.join( ( 'export %s="%s"' % ( var, val )
358 if val else 'unset %s' % var )
Bob Lantz087b5d92016-05-06 11:39:04 -0700359 for var, val in envDict.iteritems() )
360 self.cmd( cmd )
361
Bob Lantz1451d722016-05-17 14:40:07 -0700362 def ucmd( self, *args, **_kwargs ):
363 "Run command as $ONOS_USER using sudo -E -u"
364 if ONOS_USER != 'root': # don't bother with sudo
365 args = [ "sudo -E -u $ONOS_USER PATH=$PATH "
366 "bash -c '%s'" % ' '.join( args ) ]
367 return self.cmd( *args )
368
Bob Lantz087b5d92016-05-06 11:39:04 -0700369
370class ONOSCluster( Controller ):
371 "ONOS Cluster"
372 def __init__( self, *args, **kwargs ):
373 """name: (first parameter)
374 *args: topology class parameters
375 ipBase: IP range for ONOS nodes
Bob Lantzbb37d872016-05-16 16:26:13 -0700376 forward: default port forwarding list,
Bob Lantz087b5d92016-05-06 11:39:04 -0700377 topo: topology class or instance
Bob Lantz64382422016-06-03 22:51:39 -0700378 nodeOpts: ONOSNode options
Bob Lantz087b5d92016-05-06 11:39:04 -0700379 **kwargs: additional topology parameters"""
380 args = list( args )
381 name = args.pop( 0 )
382 topo = kwargs.pop( 'topo', None )
Bob Lantz930138e2016-06-23 18:53:19 -0700383 self.nat = kwargs.pop( 'nat', 'nat0' )
Bob Lantz64382422016-06-03 22:51:39 -0700384 nodeOpts = kwargs.pop( 'nodeOpts', {} )
Jon Hall9b238ae2016-08-09 13:47:43 -0700385 # Pass in kwargs to the ONOSNodes instead of the cluster
386 "alertAction: exception|ignore|warn|exit (exception)"
387 alertAction = kwargs.pop( 'alertAction', None )
388 if alertAction:
389 nodeOpts[ 'alertAction'] = alertAction
Bob Lantz087b5d92016-05-06 11:39:04 -0700390 # Default: single switch with 1 ONOS node
391 if not topo:
392 topo = SingleSwitchTopo
393 if not args:
394 args = ( 1, )
395 if not isinstance( topo, Topo ):
396 topo = RenamedTopo( topo, *args, hnew='onos', **kwargs )
Bob Lantzbb37d872016-05-16 16:26:13 -0700397 self.ipBase = kwargs.pop( 'ipBase', '192.168.123.0/24' )
398 self.forward = kwargs.pop( 'forward',
399 [ KarafPort, GUIPort, OpenFlowPort ] )
Bob Lantz087b5d92016-05-06 11:39:04 -0700400 super( ONOSCluster, self ).__init__( name, inNamespace=False )
401 fixIPTables()
402 self.env = initONOSEnv()
Bob Lantzbb37d872016-05-16 16:26:13 -0700403 self.net = Mininet( topo=topo, ipBase=self.ipBase,
Bob Lantz64382422016-06-03 22:51:39 -0700404 host=partial( ONOSNode, **nodeOpts ),
405 switch=LinuxBridge,
Bob Lantz087b5d92016-05-06 11:39:04 -0700406 controller=None )
Bob Lantz930138e2016-06-23 18:53:19 -0700407 if self.nat:
408 self.net.addNAT( self.nat ).configDefault()
Bob Lantz087b5d92016-05-06 11:39:04 -0700409 updateNodeIPs( self.env, self.nodes() )
410 self._remoteControllers = []
411
412 def start( self ):
413 "Start up ONOS cluster"
Bob Lantz087b5d92016-05-06 11:39:04 -0700414 info( '*** ONOS_APPS = %s\n' % ONOS_APPS )
415 self.net.start()
416 for node in self.nodes():
Bob Lantz569bbec2016-06-03 18:51:16 -0700417 node.start( self.env, self.nodes() )
Bob Lantz087b5d92016-05-06 11:39:04 -0700418 info( '\n' )
Bob Lantzbb37d872016-05-16 16:26:13 -0700419 self.configPortForwarding( ports=self.forward, action='A' )
Bob Lantz087b5d92016-05-06 11:39:04 -0700420 self.waitStarted()
421 return
422
423 def waitStarted( self ):
424 "Wait until all nodes have started"
425 startTime = time.time()
426 for node in self.nodes():
427 info( node )
428 node.waitStarted()
Bob Lantzbb37d872016-05-16 16:26:13 -0700429 info( '*** Waited %.2f seconds for ONOS startup' %
430 ( time.time() - startTime ) )
Bob Lantz087b5d92016-05-06 11:39:04 -0700431
432 def stop( self ):
433 "Shut down ONOS cluster"
Bob Lantzbb37d872016-05-16 16:26:13 -0700434 self.configPortForwarding( ports=self.forward, action='D' )
Bob Lantz087b5d92016-05-06 11:39:04 -0700435 for node in self.nodes():
436 node.stop()
437 self.net.stop()
438
439 def nodes( self ):
440 "Return list of ONOS nodes"
441 return [ h for h in self.net.hosts if isinstance( h, ONOSNode ) ]
442
Bob Lantz930138e2016-06-23 18:53:19 -0700443 def configPortForwarding( self, ports=[], action='A' ):
444 """Start or stop port forwarding (any intf) for all nodes
445 ports: list of ports to forward
Bob Lantzbb37d872016-05-16 16:26:13 -0700446 action: A=add/start, D=delete/stop (default: A)"""
Bob Lantz930138e2016-06-23 18:53:19 -0700447 self.cmd( 'iptables -' + action, 'FORWARD -d', self.ipBase,
448 '-j ACCEPT' )
Bob Lantzbb37d872016-05-16 16:26:13 -0700449 for port in ports:
450 for index, node in enumerate( self.nodes() ):
451 ip, inport = node.IP(), port + index
452 # Configure a destination NAT rule
Bob Lantz930138e2016-06-23 18:53:19 -0700453 self.cmd( 'iptables -t nat -' + action,
454 'PREROUTING -t nat -p tcp --dport', inport,
455 '-j DNAT --to-destination %s:%s' % ( ip, port ) )
Bob Lantzbb37d872016-05-16 16:26:13 -0700456
Bob Lantz930138e2016-06-23 18:53:19 -0700457
Bob Lantz087b5d92016-05-06 11:39:04 -0700458class ONOSSwitchMixin( object ):
459 "Mixin for switches that connect to an ONOSCluster"
460 def start( self, controllers ):
461 "Connect to ONOSCluster"
462 self.controllers = controllers
463 assert ( len( controllers ) is 1 and
464 isinstance( controllers[ 0 ], ONOSCluster ) )
465 clist = controllers[ 0 ].nodes()
466 return super( ONOSSwitchMixin, self ).start( clist )
467
468class ONOSOVSSwitch( ONOSSwitchMixin, OVSSwitch ):
469 "OVSSwitch that can connect to an ONOSCluster"
470 pass
471
472class ONOSUserSwitch( ONOSSwitchMixin, UserSwitch):
473 "UserSwitch that can connect to an ONOSCluster"
474 pass
475
476
477### Ugly utility routines
478
479def fixIPTables():
480 "Fix LinuxBridge warning"
481 for s in 'arp', 'ip', 'ip6':
482 quietRun( 'sysctl net.bridge.bridge-nf-call-%stables=0' % s )
483
484
485### Test code
486
487def test( serverCount ):
488 "Test this setup"
489 setLogLevel( 'info' )
490 net = Mininet( topo=SingleSwitchTopo( 3 ),
491 controller=[ ONOSCluster( 'c0', serverCount ) ],
492 switch=ONOSOVSSwitch )
493 net.start()
494 net.waitConnected()
495 CLI( net )
496 net.stop()
497
498
499### CLI Extensions
500
501OldCLI = CLI
502
503class ONOSCLI( OldCLI ):
504 "CLI Extensions for ONOS"
505
506 prompt = 'mininet-onos> '
507
508 def __init__( self, net, **kwargs ):
509 c0 = net.controllers[ 0 ]
510 if isinstance( c0, ONOSCluster ):
511 net = MininetFacade( net, cnet=c0.net )
512 OldCLI.__init__( self, net, **kwargs )
513
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700514 def onos1( self ):
515 "Helper function: return default ONOS node"
516 return self.mn.controllers[ 0 ].net.hosts[ 0 ]
517
Bob Lantz087b5d92016-05-06 11:39:04 -0700518 def do_onos( self, line ):
519 "Send command to ONOS CLI"
520 c0 = self.mn.controllers[ 0 ]
521 if isinstance( c0, ONOSCluster ):
522 # cmdLoop strips off command name 'onos'
523 if line.startswith( ':' ):
524 line = 'onos' + line
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700525 onos1 = self.onos1().name
526 if line:
527 line = '"%s"' % line
528 cmd = '%s client -h %s %s' % ( onos1, onos1, line )
Bob Lantz087b5d92016-05-06 11:39:04 -0700529 quietRun( 'stty -echo' )
530 self.default( cmd )
531 quietRun( 'stty echo' )
532
533 def do_wait( self, line ):
534 "Wait for switches to connect"
535 self.mn.waitConnected()
536
537 def do_balance( self, line ):
538 "Balance switch mastership"
539 self.do_onos( ':balance-masters' )
540
541 def do_log( self, line ):
Bob Lantz4b51d5c2016-05-27 14:47:38 -0700542 "Run tail -f /tmp/onos1/log; press control-C to stop"
Bob Lantz9ba19dc2016-06-13 20:22:07 -0700543 self.default( '%s tail -f /tmp/%s/log' %
544 ( self.onos1(), self.onos1() ) )
Bob Lantz087b5d92016-05-06 11:39:04 -0700545
Bob Lantz64382422016-06-03 22:51:39 -0700546 def do_status( self, line ):
547 "Return status of ONOS cluster(s)"
548 for c in self.mn.controllers:
549 if isinstance( c, ONOSCluster ):
550 for node in c.net.hosts:
551 if isinstance( node, ONOSNode ):
552 errors, warnings = node.checkLog()
553 running = ( 'Running' if node.isRunning()
554 else 'Exited' )
555 status = ''
556 if errors:
557 status += '%d ERRORS ' % len( errors )
558 if warnings:
559 status += '%d warnings' % len( warnings )
560 status = status if status else 'OK'
561 info( node, '\t', running, '\t', status, '\n' )
562
Bob Lantzc96e2582016-06-13 18:57:04 -0700563 def do_arp( self, line ):
564 "Send gratuitous arps from all data network hosts"
565 startTime = time.time()
566 try:
567 count = int( line )
568 except:
569 count = 1
570 # Technically this check should be on the host
Bob Lantzc3de5152016-06-17 17:13:57 -0700571 if '-U' not in quietRun( 'arping -h', shell=True ):
572 warn( 'Please install iputils-arping.\n' )
Bob Lantzc96e2582016-06-13 18:57:04 -0700573 return
574 # This is much faster if we do it in parallel
575 for host in self.mn.net.hosts:
576 intf = host.defaultIntf()
577 # -b: keep using broadcasts; -f: quit after 1 reply
578 # -U: gratuitous ARP update
579 host.sendCmd( 'arping -bf -c', count, '-U -I',
580 intf.name, intf.IP() )
581 for host in self.mn.net.hosts:
582 # We could check the output here if desired
583 host.waitOutput()
584 info( '.' )
585 info( '\n' )
586 elapsed = time.time() - startTime
587 debug( 'Completed in %.2f seconds\n' % elapsed )
588
Bob Lantz64382422016-06-03 22:51:39 -0700589
590# For interactive use, exit on error
591exitOnError = dict( nodeOpts={ 'alertAction': 'exit' } )
592ONOSClusterInteractive = specialClass( ONOSCluster, defaults=exitOnError )
593
Bob Lantz087b5d92016-05-06 11:39:04 -0700594
595### Exports for bin/mn
596
597CLI = ONOSCLI
Bob Lantz64382422016-06-03 22:51:39 -0700598controllers = { 'onos': ONOSClusterInteractive,
599 'default': ONOSClusterInteractive }
Bob Lantz087b5d92016-05-06 11:39:04 -0700600
601# XXX Hack to change default controller as above doesn't work
Bob Lantz64382422016-06-03 22:51:39 -0700602findController = lambda: controllers[ 'default' ]
Bob Lantz087b5d92016-05-06 11:39:04 -0700603
604switches = { 'onos': ONOSOVSSwitch,
605 'onosovs': ONOSOVSSwitch,
606 'onosuser': ONOSUserSwitch,
607 'default': ONOSOVSSwitch }
608
Bob Lantzbb37d872016-05-16 16:26:13 -0700609# Null topology so we can control an external/hardware network
610topos = { 'none': Topo }
611
Bob Lantz087b5d92016-05-06 11:39:04 -0700612if __name__ == '__main__':
613 if len( argv ) != 2:
614 test( 3 )
615 else:
616 test( int( argv[ 1 ] ) )