blob: 58230d63a718c705e9ee2196145091e7d1c6f49e [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
77 scripts = ( 'start-zk.sh', 'start-ramcloud-coordinator.sh',
78 'start-ramcloud-server.sh', 'start-onos.sh', 'start-rest.sh' )
79
Bob Lantz7ead0532013-12-17 20:41:20 -080080 def __init__( self, name, n=1, reactive=True, runAsRoot=False, **params):
Bob Lantzc7c05402013-12-16 21:22:12 -080081 """n: number of ONOS instances to run (1)
Bob Lantz7ead0532013-12-17 20:41:20 -080082 reactive: run in reactive mode (True)
83 runAsRoot: run ONOS as root (False)"""
Bob Lantzc7c05402013-12-16 21:22:12 -080084 self.check()
Bob Lantzc7c05402013-12-16 21:22:12 -080085 self.count = n
Bob Lantz7ead0532013-12-17 20:41:20 -080086 self.reactive = reactive
87 self.runAsRoot = runAsRoot
Bob Lantzc7c05402013-12-16 21:22:12 -080088 self.ids = range( 0, self.count )
89 Controller.__init__( self, name, **params )
Bob Lantz63bbe4c2014-02-06 19:29:55 -080090 self.proxies = []
Bob Lantzc7c05402013-12-16 21:22:12 -080091 # We don't need to run as root, and it can interfere
92 # with starting Zookeeper manually
Bob Lantz7ead0532013-12-17 20:41:20 -080093 self.user = None
94 if not self.runAsRoot:
95 try:
96 self.user = quietRun( 'who am i' ).split()[ 0 ]
97 self.sendCmd( 'su', self.user )
98 self.waiting = False
99 except:
100 warn( '__init__: failed to drop privileges\n' )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800101 self.cmd( 'mkdir -p', self.logDir )
Bob Lantzc7c05402013-12-16 21:22:12 -0800102 # Need to run commands from ONOS dir
103 self.cmd( 'cd', self.onosDir )
104 self.cmd( 'export PATH=$PATH:%s' % self.onosDir )
Bob Lantzc7c05402013-12-16 21:22:12 -0800105
106 def check( self ):
Bob Lantz21b41f62013-12-18 18:16:38 -0800107 "Set onosDir and check for ONOS prerequisites"
Bob Lantzc7c05402013-12-16 21:22:12 -0800108 if not quietRun( 'which java' ):
109 raise Exception( 'java not found -'
110 ' make sure it is installed and in $PATH' )
Bob Lantz21b41f62013-12-18 18:16:38 -0800111 if 'ONOS_HOME' in environ:
112 self.onosDir = environ[ 'ONOS_HOME' ]
113 else:
114 warn( '* $ONOS_HOME is not set - assuming %s\n' % self.onosDir )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800115 for script in self.scripts:
Bob Lantz21b41f62013-12-18 18:16:38 -0800116 script = path.join( self.onosDir, script )
117 if not path.exists( script ):
118 msg = '%s not found' % script
119 if 'ONOS_HOME' not in environ:
120 msg += ' (try setting $ONOS_HOME and/or sudo -E)'
121 raise Exception( msg )
Bob Lantz7ead0532013-12-17 20:41:20 -0800122
123 def waitNetstat( self, pid ):
124 """Wait for pid to show up in netstat
125 We assume that once a process is listening on some
126 port, it is ready to go!"""
127 while True:
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800128 output = self.cmd( 'sudo netstat -natup | grep %s/' % pid )
Bob Lantz7ead0532013-12-17 20:41:20 -0800129 if output:
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800130 break
Bob Lantz7ead0532013-12-17 20:41:20 -0800131 info( '.' )
Bob Lantza0930822013-12-17 21:00:41 -0800132 time.sleep( 1 )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800133 info( '\n* Process %d is listening\n' % pid )
Bob Lantz7ead0532013-12-17 20:41:20 -0800134
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800135 def waitStart( self, procname, pattern, maxWait=10 ):
136 "Wait for proces to start up and be visible to pgrep"
Bob Lantz21b41f62013-12-18 18:16:38 -0800137 # Check script exit code
138 exitCode = int( self.cmd( 'echo $?' ) )
139 if exitCode != 0:
140 raise Exception( '%s startup failed with code %d' %
141 ( procname, exitCode ) )
Bob Lantz7ead0532013-12-17 20:41:20 -0800142 info( '* Waiting for %s startup' % procname )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800143 while True:
144 result = self.cmd( 'pgrep -f %s' % pattern )
145 if result:
146 break
147 info( '.' )
148 sleep( 1 )
149 pid = int( result.split()[ 0 ] )
150 return pid
Bob Lantz7ead0532013-12-17 20:41:20 -0800151
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800152 def startRamcloud( self, cpu=.6 ):
153 """Start Ramcloud
154 cpu: CPU usage limit (in seconds/s)"""
155 # Edit configuration file
156 self.cmd( "sed -ibak -e 's/host=.*/host=127.0.0.1/' %s/conf/ramcloud.conf" %
157 self.onosDir)
158 # Create a cgroup so Ramcloud doesn't eat all of our CPU
159 ramcloud = CPULimitedHost( 'ramcloud', inNamespace=False, period_us=5000 )
160 ramcloud.setCPUFrac( cpu / numCores() )
161 ramcloud.cmd( 'export PATH=%s:$PATH' % self.onosDir )
162 ramcloud.cmd( 'export ONOS_LOGDIR=%s' % self.logDir )
163 for daemon in 'coordinator', 'server':
Bob Lantz42543db2014-02-24 16:07:34 -0800164 ramcloud.cmd( 'start-ramcloud-%s.sh stop' % daemon )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800165 ramcloud.cmd( 'start-ramcloud-%s.sh start' % daemon )
166 pid = self.waitStart( 'Ramcloud %s' % daemon, 'obj.master/' + daemon )
167 self.waitNetstat( pid )
168 status = self.cmd( 'start-ramcloud-%s.sh status' % daemon )
169 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"
175 for daemon in 'coordinator', 'server':
176 self.ramcloud.cmd( './start-ramcloud-%s.sh stop' % daemon )
177 self.ramcloud.terminate()
Bob Lantzc7c05402013-12-16 21:22:12 -0800178
179 def startZookeeper( self, initcfg=True ):
180 "Start Zookeeper"
181 # 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 )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800186 self.cmd( 'start-zk.sh stop' )
187 self.cmd( 'start-zk.sh start' )
188 pid = self.waitStart( 'Zookeeper', 'zookeeper' )
189 self.waitNetstat( pid )
Bob Lantzc7c05402013-12-16 21:22:12 -0800190 status = self.cmd( 'start-zk.sh status' )
191 if 'Error' in status:
192 raise Exception( 'Zookeeper startup failed: ' + status )
193
194 def stopZookeeper( self ):
195 "Stop Zookeeper"
196 self.cmd( 'start-zk.sh stop' )
197
Bob Lantz7ead0532013-12-17 20:41:20 -0800198 def genProperties( self, id, path='/tmp' ):
Bob Lantz42543db2014-02-24 16:07:34 -0800199 "Generate ONOS properties file and return its full pathname"
200 defaultProps = self.onosDir + '/conf/onos.properties'
201 propsFile = path + '/onos-%s.properties' % id
202 with open( propsFile, 'w' ) as f:
203 with open( defaultProps ) as d:
204 for line in d.readlines():
205 prop = line.split( ' ' )[ 0 ]
206 val = self.perNodeConfigBase.get( prop, None )
207 if val:
208 # Write updated property
209 f.write( '%s = %s\n' % ( prop, val + id) )
210 else:
211 # Write original property
212 f.write( line )
213 if prop == 'floodlight.modules' and ',\\' in line:
214 if self.reactive:
215 # Insert reactive modules into list
216 for module in self.reactiveModules:
217 f.write( '%s,\\\n' % module )
218 return propsFile
Bob Lantz7ead0532013-12-17 20:41:20 -0800219
220 def setVars( self, id, propsFile ):
221 """Set and return environment vars
222 id: ONOS instance number
223 propsFile: properties file name"""
Bob Lantz4ed6cff2013-12-17 21:43:36 -0800224 # cassdir = self.cassDir % id
Bob Lantzc7c05402013-12-16 21:22:12 -0800225 logback = self.logbackFile % id
226 jmxport = self.jmxbase + id
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800227 logdir = self.logDir
228 self.cmd( 'export ONOS_LOGDIR=%s' % logdir )
229 self.cmd( 'export ONOS_LOGBASE=onos-%d.`hostname`' % id)
Bob Lantzc7c05402013-12-16 21:22:12 -0800230 self.cmd( 'export ZOO_LOG_DIR="%s"' % logdir )
Bob Lantzc7c05402013-12-16 21:22:12 -0800231 self.cmd( 'export ONOS_LOGBACK="%s"' % logback )
232 self.cmd( 'export JMX_PORT=%s' % jmxport )
Bob Lantz7ead0532013-12-17 20:41:20 -0800233 self.cmd( 'export JVM_OPTS="-D%s=%s"' % (
234 self.proctag, id ) )
235 self.cmd( 'export ONOS_PROPS="%s"' % propsFile )
Bob Lantzc7c05402013-12-16 21:22:12 -0800236
237 def startONOS( self, id ):
238 """Start ONOS
Bob Lantz7ead0532013-12-17 20:41:20 -0800239 id: new instance number"""
Bob Lantza0930822013-12-17 21:00:41 -0800240 start = time.time()
Bob Lantz7ead0532013-12-17 20:41:20 -0800241 self.stopONOS( id )
242 propsFile = self.genProperties( id )
243 self.setVars( id, propsFile )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800244 self.cmd( 'start-onos.sh startnokill' )
Bob Lantz7ead0532013-12-17 20:41:20 -0800245 # start-onos.sh waits for ONOS startup
Bob Lantza0930822013-12-17 21:00:41 -0800246 elapsed = time.time() - start
Bob Lantz7ead0532013-12-17 20:41:20 -0800247 info( '* ONOS %s started in %.2f seconds\n' % ( id, elapsed ) )
Bob Lantzc7c05402013-12-16 21:22:12 -0800248
249 def stopONOS( self, id ):
250 """Shut down ONOS
251 id: identifier for instance"""
252 pid = self.cmd( "jps -v | grep %s=%s | awk '{print $1}'" %
253 ( self.proctag, id ) ).strip()
254 if pid:
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800255 self.cmd( 'kill', pid )
Bob Lantzc7c05402013-12-16 21:22:12 -0800256
257 def start( self, *args ):
258 "Start ONOS instances"
Bob Lantzc7c05402013-12-16 21:22:12 -0800259 info( '* Starting Zookeeper\n' )
260 self.startZookeeper()
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800261 info( '* Starting Ramcloud\n' )
262 self.startRamcloud()
Bob Lantzc7c05402013-12-16 21:22:12 -0800263 for id in self.ids:
264 info( '* Starting ONOS %s\n' % id )
265 self.startONOS( id )
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800266 self.cmd( 'start-rest.sh start' )
267 # Initialize proxies for clist()
268 self.proxies = [ RemoteController( 'onos-%d' % id, port=(self.ofbase + id ) )
269 for id in range( 0, self.count ) ]
Bob Lantzc7c05402013-12-16 21:22:12 -0800270
271 def stop( self, *args ):
272 "Stop ONOS instances"
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800273 self.cmd( 'start-rest.sh stop' )
Bob Lantzc7c05402013-12-16 21:22:12 -0800274 for id in self.ids:
275 info( '* Stopping ONOS %s\n' % id )
276 self.stopONOS( id )
Bob Lantz7ead0532013-12-17 20:41:20 -0800277 info( '* Stopping Zookeeper\n' )
Bob Lantzc7c05402013-12-16 21:22:12 -0800278 self.stopZookeeper()
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800279 info( '* Stopping Ramcloud\n' )
280 self.stopRamcloud()
281 for p in self.proxies:
282 p.stop()
283 p.proxies = []
Bob Lantzc7c05402013-12-16 21:22:12 -0800284
285 def clist( self ):
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800286 "Return list of Controller proxies for this ONOS cluster"
287 return self.proxies
Bob Lantzc7c05402013-12-16 21:22:12 -0800288
289
290class OVSSwitchONOS( OVSSwitch ):
291 "OVS switch which connects to multiple controllers"
292 def start( self, controllers ):
Bob Lantzc7c05402013-12-16 21:22:12 -0800293 assert len( controllers ) == 1
294 c0 = controllers[ 0 ]
295 assert type( c0 ) == ONOS
Bob Lantz63bbe4c2014-02-06 19:29:55 -0800296 controllers = c0.clist()
297 OVSSwitch.start( self, controllers )
Bob Lantzc7c05402013-12-16 21:22:12 -0800298
299
Bob Lantz7ead0532013-12-17 20:41:20 -0800300def waitConnected( switches ):
301 "Wait until all switches connect to controllers"
Bob Lantza0930822013-12-17 21:00:41 -0800302 start = time.time()
Bob Lantz7ead0532013-12-17 20:41:20 -0800303 info( '* Waiting for switches to connect...\n' )
304 for s in switches:
305 info( s )
306 while not s.connected():
307 info( '.' )
Bob Lantza0930822013-12-17 21:00:41 -0800308 time.sleep( 1 )
Bob Lantz7ead0532013-12-17 20:41:20 -0800309 info( ' ' )
Bob Lantza0930822013-12-17 21:00:41 -0800310 elapsed = time.time() - start
Bob Lantz7ead0532013-12-17 20:41:20 -0800311 info( '\n* Connected in %.2f seconds\n' % elapsed )
312
313
Bob Lantzc7c05402013-12-16 21:22:12 -0800314controllers = { 'onos': ONOS }
315switches = { 'ovso': OVSSwitchONOS }
316
317
318if __name__ == '__main__':
Bob Lantz7ead0532013-12-17 20:41:20 -0800319 # Simple test for ONOS() controller class
Bob Lantzc7c05402013-12-16 21:22:12 -0800320 setLogLevel( 'info' )
Bob Lantz7ead0532013-12-17 20:41:20 -0800321 size = 2 if len( argv ) != 2 else int( argv[ 1 ] )
322 net = Mininet( topo=LinearTopo( size ),
Bob Lantzc7c05402013-12-16 21:22:12 -0800323 controller=partial( ONOS, n=2 ),
324 switch=OVSSwitchONOS )
325 net.start()
Bob Lantz7ead0532013-12-17 20:41:20 -0800326 waitConnected( net.switches )
Bob Lantzc7c05402013-12-16 21:22:12 -0800327 CLI( net )
328 net.stop()