blob: 119df36e3cc65ae5cf0865a84fa7bee48e6b6fe9 [file] [log] [blame]
Bob Lantzc7c05402013-12-16 21:22:12 -08001#!/usr/bin/env python
2
3"""
Bob Lantz7ead0532013-12-17 20:41:20 -08004onos.py: A basic (?) ONOS Controller() subclass for Mininet
Bob Lantzc7c05402013-12-16 21:22:12 -08005
6We implement the following classes:
7
8ONOSController: a custom Controller() subclass to start ONOS
9OVSSwitchONOS: a custom OVSSwitch() switch that connects to multiple controllers.
10
Bob Lantz63bbe4c2014-02-06 19:29:55 -080011We use single Zookeeper and Ramcloud instances for now.
Bob Lantzc7c05402013-12-16 21:22:12 -080012
13As a custom file, exports:
14
15--controller onos
16--switch ovso
17
18Usage:
19
Bob Lantz21b41f62013-12-18 18:16:38 -080020$ sudo -E ./onos.py
Bob Lantzc7c05402013-12-16 21:22:12 -080021
22This will start up a simple 2-host, 2 ONOS network
23
Bob Lantz21b41f62013-12-18 18:16:38 -080024$ sudo -E mn --custom onos.py --controller onos,2 --switch ovso
Bob Lantzc7c05402013-12-16 21:22:12 -080025"""
26
Bob Lantz63bbe4c2014-02-06 19:29:55 -080027from mininet.node import Controller, OVSSwitch, CPULimitedHost, RemoteController
Bob Lantzc7c05402013-12-16 21:22:12 -080028from mininet.net import Mininet
29from mininet.cli import CLI
Bob Lantz7ead0532013-12-17 20:41:20 -080030from mininet.topo import LinearTopo
31from mininet.log import setLogLevel, info, warn
Bob Lantz63bbe4c2014-02-06 19:29:55 -080032from mininet.util import quietRun, numCores
Bob Lantzc7c05402013-12-16 21:22:12 -080033
Bob Lantza0930822013-12-17 21:00:41 -080034# This should be cleaned up to avoid interfering with mn
Bob Lantzc7c05402013-12-16 21:22:12 -080035from shutil import copyfile
Bob Lantz21b41f62013-12-18 18:16:38 -080036from os import environ, path
Bob Lantzc7c05402013-12-16 21:22:12 -080037from functools import partial
Bob Lantza0930822013-12-17 21:00:41 -080038import time
Bob Lantz7ead0532013-12-17 20:41:20 -080039from sys import argv
Bob Lantz63bbe4c2014-02-06 19:29:55 -080040from time import sleep
Bob Lantzc7c05402013-12-16 21:22:12 -080041
42class ONOS( Controller ):
43 "Custom controller class for ONOS"
44
45 # Directories and configuration templates
46 home = environ[ 'HOME' ]
47 onosDir = home + "/ONOS"
48 zookeeperDir = home + "/zookeeper-3.4.5"
49 dirBase = '/tmp'
Bob Lantz63bbe4c2014-02-06 19:29:55 -080050 logDir = dirBase + '/onos-logs'
Bob Lantz4ed6cff2013-12-17 21:43:36 -080051 logbackFile = dirBase + '/onos-%s.logback.xml'
Bob Lantz7ead0532013-12-17 20:41:20 -080052
Bob Lantz7ead0532013-12-17 20:41:20 -080053 # Additions for reactive forwarding
54 reactiveModules = (
Jonathan Hart0961fe82014-04-03 09:56:25 -070055 'net.onrc.onos.apps.proxyarp.ProxyArpManager',
Jonathan Hart51f6f5b2014-04-03 10:32:10 -070056 'net.onrc.onos.core.main.config.DefaultConfiguration',
Jonathan Hart0961fe82014-04-03 09:56:25 -070057 'net.onrc.onos.apps.forwarding.Forwarding'
Bob Lantz7ead0532013-12-17 20:41:20 -080058 )
59
60 # Module parameters
Bob Lantzc7c05402013-12-16 21:22:12 -080061 ofbase = 6633
Bob Lantz7ead0532013-12-17 20:41:20 -080062 restbase = 8080
63 jmxbase = 7189
Bob Lantzc7c05402013-12-16 21:22:12 -080064
Bob Lantzc7c05402013-12-16 21:22:12 -080065 fc = 'net.floodlightcontroller.'
Bob Lantzc7c05402013-12-16 21:22:12 -080066
Bob Lantz21b41f62013-12-18 18:16:38 -080067 # Things that vary per ONOS id
Bob Lantz7ead0532013-12-17 20:41:20 -080068 perNodeConfigBase = {
69 fc + 'core.FloodlightProvider.openflowport': ofbase,
70 fc + 'restserver.RestApiServer.port': restbase,
71 fc + 'core.FloodlightProvider.controllerid': 0
72 }
73
Bob Lantz7ead0532013-12-17 20:41:20 -080074 proctag = 'mn-onos-id'
Bob Lantzc7c05402013-12-16 21:22:12 -080075
Bob Lantz63bbe4c2014-02-06 19:29:55 -080076 # List of scripts that we need/use
Naoki Shiotabe433a12014-04-07 12:03:49 -070077# scripts = ( 'start-zk.sh', 'start-ramcloud-coordinator.sh',
78# 'start-ramcloud-server.sh', 'start-onos.sh', 'start-rest.sh' )
79 scripts = ( 'onos.sh', 'start-rest.sh' )
Bob Lantz63bbe4c2014-02-06 19:29:55 -080080
Bob Lantz7ead0532013-12-17 20:41:20 -080081 def __init__( self, name, n=1, reactive=True, runAsRoot=False, **params):
Bob Lantzc7c05402013-12-16 21:22:12 -080082 """n: number of ONOS instances to run (1)
Bob Lantz7ead0532013-12-17 20:41:20 -080083 reactive: run in reactive mode (True)
84 runAsRoot: run ONOS as root (False)"""
Bob Lantzc7c05402013-12-16 21:22:12 -080085 self.check()
Bob Lantzc7c05402013-12-16 21:22:12 -080086 self.count = n
Bob Lantz7ead0532013-12-17 20:41:20 -080087 self.reactive = reactive
88 self.runAsRoot = runAsRoot
Bob Lantzc7c05402013-12-16 21:22:12 -080089 self.ids = range( 0, self.count )
90 Controller.__init__( self, name, **params )
Bob Lantz63bbe4c2014-02-06 19:29:55 -080091 self.proxies = []
Bob Lantzc7c05402013-12-16 21:22:12 -080092 # We don't need to run as root, and it can interfere
93 # with starting Zookeeper manually
Bob Lantz7ead0532013-12-17 20:41:20 -080094 self.user = None
95 if not self.runAsRoot:
96 try:
97 self.user = quietRun( 'who am i' ).split()[ 0 ]
98 self.sendCmd( 'su', self.user )
99 self.waiting = False
100 except:
101 warn( '__init__: failed to drop privileges\n' )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800102 self.cmd( 'mkdir -p', self.logDir )
Bob Lantzc7c05402013-12-16 21:22:12 -0800103 # Need to run commands from ONOS dir
104 self.cmd( 'cd', self.onosDir )
105 self.cmd( 'export PATH=$PATH:%s' % self.onosDir )
Bob Lantzc7c05402013-12-16 21:22:12 -0800106
107 def check( self ):
Bob Lantz21b41f62013-12-18 18:16:38 -0800108 "Set onosDir and check for ONOS prerequisites"
Bob Lantzc7c05402013-12-16 21:22:12 -0800109 if not quietRun( 'which java' ):
110 raise Exception( 'java not found -'
111 ' make sure it is installed and in $PATH' )
Bob Lantz21b41f62013-12-18 18:16:38 -0800112 if 'ONOS_HOME' in environ:
113 self.onosDir = environ[ 'ONOS_HOME' ]
114 else:
115 warn( '* $ONOS_HOME is not set - assuming %s\n' % self.onosDir )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800116 for script in self.scripts:
Bob Lantz21b41f62013-12-18 18:16:38 -0800117 script = path.join( self.onosDir, script )
118 if not path.exists( script ):
119 msg = '%s not found' % script
120 if 'ONOS_HOME' not in environ:
121 msg += ' (try setting $ONOS_HOME and/or sudo -E)'
122 raise Exception( msg )
Bob Lantz7ead0532013-12-17 20:41:20 -0800123
124 def waitNetstat( self, pid ):
125 """Wait for pid to show up in netstat
126 We assume that once a process is listening on some
127 port, it is ready to go!"""
128 while True:
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800129 output = self.cmd( 'sudo netstat -natup | grep %s/' % pid )
Bob Lantz7ead0532013-12-17 20:41:20 -0800130 if output:
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800131 break
Bob Lantz7ead0532013-12-17 20:41:20 -0800132 info( '.' )
Bob Lantza0930822013-12-17 21:00:41 -0800133 time.sleep( 1 )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800134 info( '\n* Process %d is listening\n' % pid )
Bob Lantz7ead0532013-12-17 20:41:20 -0800135
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800136 def waitStart( self, procname, pattern, maxWait=10 ):
137 "Wait for proces to start up and be visible to pgrep"
Bob Lantz21b41f62013-12-18 18:16:38 -0800138 # Check script exit code
139 exitCode = int( self.cmd( 'echo $?' ) )
140 if exitCode != 0:
141 raise Exception( '%s startup failed with code %d' %
142 ( procname, exitCode ) )
Bob Lantz7ead0532013-12-17 20:41:20 -0800143 info( '* Waiting for %s startup' % procname )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800144 while True:
145 result = self.cmd( 'pgrep -f %s' % pattern )
146 if result:
147 break
148 info( '.' )
149 sleep( 1 )
150 pid = int( result.split()[ 0 ] )
151 return pid
Bob Lantz7ead0532013-12-17 20:41:20 -0800152
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800153 def startRamcloud( self, cpu=.6 ):
154 """Start Ramcloud
155 cpu: CPU usage limit (in seconds/s)"""
156 # Edit configuration file
Naoki Shiotabe433a12014-04-07 12:03:49 -0700157# self.cmd( "sed -ibak -e 's/host=.*/host=127.0.0.1/' %s/conf/ramcloud.conf" %
158# self.onosDir)
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800159 # Create a cgroup so Ramcloud doesn't eat all of our CPU
160 ramcloud = CPULimitedHost( 'ramcloud', inNamespace=False, period_us=5000 )
161 ramcloud.setCPUFrac( cpu / numCores() )
162 ramcloud.cmd( 'export PATH=%s:$PATH' % self.onosDir )
163 ramcloud.cmd( 'export ONOS_LOGDIR=%s' % self.logDir )
Naoki Shiotabe433a12014-04-07 12:03:49 -0700164 for daemon in 'coord', 'server':
165 ramcloud.cmd( 'onos.sh rc-%s start' % daemon )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800166 pid = self.waitStart( 'Ramcloud %s' % daemon, 'obj.master/' + daemon )
167 self.waitNetstat( pid )
Naoki Shiotabe433a12014-04-07 12:03:49 -0700168 status = self.cmd( 'onos.sh rc-%s.sh status' % daemon )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800169 if 'running' not in status:
170 raise Exception( 'Ramcloud %s startup failed: ' % daemon + status )
171 self.ramcloud = ramcloud
Bob Lantzc7c05402013-12-16 21:22:12 -0800172
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800173 def stopRamcloud( self ):
174 "Stop Ramcloud"
Naoki Shiotabe433a12014-04-07 12:03:49 -0700175 for daemon in 'coord', 'server':
176 self.ramcloud.cmd( './onos.sh rc-%s stop' % daemon )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800177 self.ramcloud.terminate()
Bob Lantzc7c05402013-12-16 21:22:12 -0800178
179 def startZookeeper( self, initcfg=True ):
180 "Start Zookeeper"
Naoki Shiotabe433a12014-04-07 12:03:49 -0700181# # Reinitialize configuration file
182# if initcfg:
183# cfg = self.zookeeperDir + '/conf/zoo.cfg'
184# template = self.zookeeperDir + '/conf/zoo_sample.cfg'
185# copyfile( template, cfg )
186 self.cmd( 'onos.sh zk start' )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800187 pid = self.waitStart( 'Zookeeper', 'zookeeper' )
188 self.waitNetstat( pid )
Naoki Shiotabe433a12014-04-07 12:03:49 -0700189 status = self.cmd( 'onos.sh zk status' )
Bob Lantzc7c05402013-12-16 21:22:12 -0800190 if 'Error' in status:
191 raise Exception( 'Zookeeper startup failed: ' + status )
192
193 def stopZookeeper( self ):
194 "Stop Zookeeper"
Naoki Shiotabe433a12014-04-07 12:03:49 -0700195 self.cmd( 'onos.sh zk stop' )
Bob Lantzc7c05402013-12-16 21:22:12 -0800196
Bob Lantz7ead0532013-12-17 20:41:20 -0800197 def genProperties( self, id, path='/tmp' ):
Bob Lantz42543db2014-02-24 16:07:34 -0800198 "Generate ONOS properties file and return its full pathname"
199 defaultProps = self.onosDir + '/conf/onos.properties'
200 propsFile = path + '/onos-%s.properties' % id
201 with open( propsFile, 'w' ) as f:
202 with open( defaultProps ) as d:
203 for line in d.readlines():
204 prop = line.split( ' ' )[ 0 ]
205 val = self.perNodeConfigBase.get( prop, None )
206 if val:
207 # Write updated property
208 f.write( '%s = %s\n' % ( prop, val + id) )
209 else:
210 # Write original property
211 f.write( line )
212 if prop == 'floodlight.modules' and ',\\' in line:
213 if self.reactive:
214 # Insert reactive modules into list
215 for module in self.reactiveModules:
216 f.write( '%s,\\\n' % module )
217 return propsFile
Bob Lantz7ead0532013-12-17 20:41:20 -0800218
219 def setVars( self, id, propsFile ):
220 """Set and return environment vars
221 id: ONOS instance number
222 propsFile: properties file name"""
Bob Lantz4ed6cff2013-12-17 21:43:36 -0800223 # cassdir = self.cassDir % id
Bob Lantzc7c05402013-12-16 21:22:12 -0800224 logback = self.logbackFile % id
225 jmxport = self.jmxbase + id
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800226 logdir = self.logDir
227 self.cmd( 'export ONOS_LOGDIR=%s' % logdir )
228 self.cmd( 'export ONOS_LOGBASE=onos-%d.`hostname`' % id)
Bob Lantzc7c05402013-12-16 21:22:12 -0800229 self.cmd( 'export ZOO_LOG_DIR="%s"' % logdir )
Bob Lantzc7c05402013-12-16 21:22:12 -0800230 self.cmd( 'export ONOS_LOGBACK="%s"' % logback )
231 self.cmd( 'export JMX_PORT=%s' % jmxport )
Bob Lantz7ead0532013-12-17 20:41:20 -0800232 self.cmd( 'export JVM_OPTS="-D%s=%s"' % (
233 self.proctag, id ) )
234 self.cmd( 'export ONOS_PROPS="%s"' % propsFile )
Bob Lantzc7c05402013-12-16 21:22:12 -0800235
236 def startONOS( self, id ):
237 """Start ONOS
Bob Lantz7ead0532013-12-17 20:41:20 -0800238 id: new instance number"""
Bob Lantza0930822013-12-17 21:00:41 -0800239 start = time.time()
Bob Lantz7ead0532013-12-17 20:41:20 -0800240 self.stopONOS( id )
241 propsFile = self.genProperties( id )
242 self.setVars( id, propsFile )
Naoki Shiotabe433a12014-04-07 12:03:49 -0700243 self.cmd( 'onos.sh core startnokill' )
Bob Lantz7ead0532013-12-17 20:41:20 -0800244 # start-onos.sh waits for ONOS startup
Bob Lantza0930822013-12-17 21:00:41 -0800245 elapsed = time.time() - start
Bob Lantz7ead0532013-12-17 20:41:20 -0800246 info( '* ONOS %s started in %.2f seconds\n' % ( id, elapsed ) )
Bob Lantzc7c05402013-12-16 21:22:12 -0800247
248 def stopONOS( self, id ):
249 """Shut down ONOS
250 id: identifier for instance"""
251 pid = self.cmd( "jps -v | grep %s=%s | awk '{print $1}'" %
252 ( self.proctag, id ) ).strip()
253 if pid:
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800254 self.cmd( 'kill', pid )
Bob Lantzc7c05402013-12-16 21:22:12 -0800255
256 def start( self, *args ):
257 "Start ONOS instances"
Bob Lantzc7c05402013-12-16 21:22:12 -0800258 info( '* Starting Zookeeper\n' )
259 self.startZookeeper()
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800260 info( '* Starting Ramcloud\n' )
261 self.startRamcloud()
Bob Lantzc7c05402013-12-16 21:22:12 -0800262 for id in self.ids:
263 info( '* Starting ONOS %s\n' % id )
264 self.startONOS( id )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800265 self.cmd( 'start-rest.sh start' )
266 # Initialize proxies for clist()
267 self.proxies = [ RemoteController( 'onos-%d' % id, port=(self.ofbase + id ) )
268 for id in range( 0, self.count ) ]
Bob Lantzc7c05402013-12-16 21:22:12 -0800269
270 def stop( self, *args ):
271 "Stop ONOS instances"
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800272 self.cmd( 'start-rest.sh stop' )
Bob Lantzc7c05402013-12-16 21:22:12 -0800273 for id in self.ids:
274 info( '* Stopping ONOS %s\n' % id )
275 self.stopONOS( id )
Bob Lantz7ead0532013-12-17 20:41:20 -0800276 info( '* Stopping Zookeeper\n' )
Bob Lantzc7c05402013-12-16 21:22:12 -0800277 self.stopZookeeper()
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800278 info( '* Stopping Ramcloud\n' )
279 self.stopRamcloud()
280 for p in self.proxies:
281 p.stop()
282 p.proxies = []
Bob Lantzc7c05402013-12-16 21:22:12 -0800283
284 def clist( self ):
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800285 "Return list of Controller proxies for this ONOS cluster"
286 return self.proxies
Bob Lantzc7c05402013-12-16 21:22:12 -0800287
288
289class OVSSwitchONOS( OVSSwitch ):
290 "OVS switch which connects to multiple controllers"
291 def start( self, controllers ):
Bob Lantzc7c05402013-12-16 21:22:12 -0800292 assert len( controllers ) == 1
293 c0 = controllers[ 0 ]
294 assert type( c0 ) == ONOS
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800295 controllers = c0.clist()
296 OVSSwitch.start( self, controllers )
Bob Lantzc7c05402013-12-16 21:22:12 -0800297
298
Bob Lantz7ead0532013-12-17 20:41:20 -0800299def waitConnected( switches ):
300 "Wait until all switches connect to controllers"
Bob Lantza0930822013-12-17 21:00:41 -0800301 start = time.time()
Bob Lantz7ead0532013-12-17 20:41:20 -0800302 info( '* Waiting for switches to connect...\n' )
303 for s in switches:
304 info( s )
305 while not s.connected():
306 info( '.' )
Bob Lantza0930822013-12-17 21:00:41 -0800307 time.sleep( 1 )
Bob Lantz7ead0532013-12-17 20:41:20 -0800308 info( ' ' )
Bob Lantza0930822013-12-17 21:00:41 -0800309 elapsed = time.time() - start
Bob Lantz7ead0532013-12-17 20:41:20 -0800310 info( '\n* Connected in %.2f seconds\n' % elapsed )
311
312
Bob Lantzc7c05402013-12-16 21:22:12 -0800313controllers = { 'onos': ONOS }
314switches = { 'ovso': OVSSwitchONOS }
315
316
317if __name__ == '__main__':
Bob Lantz7ead0532013-12-17 20:41:20 -0800318 # Simple test for ONOS() controller class
Bob Lantzc7c05402013-12-16 21:22:12 -0800319 setLogLevel( 'info' )
Bob Lantz7ead0532013-12-17 20:41:20 -0800320 size = 2 if len( argv ) != 2 else int( argv[ 1 ] )
321 net = Mininet( topo=LinearTopo( size ),
Bob Lantzc7c05402013-12-16 21:22:12 -0800322 controller=partial( ONOS, n=2 ),
323 switch=OVSSwitchONOS )
324 net.start()
Bob Lantz7ead0532013-12-17 20:41:20 -0800325 waitConnected( net.switches )
Bob Lantzc7c05402013-12-16 21:22:12 -0800326 CLI( net )
327 net.stop()