Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | """ |
| 4 | onos.py: ONOS cluster and control network in Mininet |
| 5 | |
| 6 | With onos.py, you can use Mininet to create a complete |
| 7 | ONOS network, including an ONOS cluster with a modeled |
| 8 | control network as well as the usual data nework. |
| 9 | |
| 10 | This is intended to be useful for distributed ONOS |
| 11 | development and testing in the case that you require |
| 12 | a modeled control network. |
| 13 | |
| 14 | Invocation (using OVS as default switch): |
| 15 | |
| 16 | mn --custom onos.py --controller onos,3 --topo torus,4,4 |
| 17 | |
| 18 | Or with the user switch (or CPqD if installed): |
| 19 | |
| 20 | mn --custom onos.py --controller onos,3 \ |
| 21 | --switch onosuser --topo torus,4,4 |
| 22 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 23 | Currently you meed to use a custom switch class |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 24 | because Mininet's Switch() class does't (yet?) handle |
| 25 | controllers with multiple IP addresses directly. |
| 26 | |
| 27 | The classes may also be imported and used via Mininet's |
| 28 | python API. |
| 29 | |
| 30 | Bugs/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 | |
| 43 | from mininet.node import Controller, OVSSwitch, UserSwitch |
| 44 | from mininet.nodelib import LinuxBridge |
| 45 | from mininet.net import Mininet |
| 46 | from mininet.topo import SingleSwitchTopo, Topo |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 47 | from mininet.log import setLogLevel, info, warn, error, debug |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 48 | from mininet.cli import CLI |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 49 | from mininet.util import quietRun, specialClass |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 50 | from mininet.examples.controlnet import MininetFacade |
| 51 | |
| 52 | from os import environ |
| 53 | from os.path import dirname, join, isfile |
| 54 | from sys import argv |
| 55 | from glob import glob |
| 56 | import time |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 57 | from functools import partial |
Bob Lantz | 92d8e05 | 2016-06-17 16:03:58 -0700 | [diff] [blame] | 58 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 59 | |
| 60 | ### ONOS Environment |
| 61 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 62 | KarafPort = 8101 # ssh port indicating karaf is running |
| 63 | GUIPort = 8181 # GUI/REST port |
| 64 | OpenFlowPort = 6653 # OpenFlow port |
Jon Hall | a43d033 | 2016-08-04 15:02:23 -0700 | [diff] [blame] | 65 | CopycatPort = 9876 # Copycat port |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 66 | |
| 67 | def 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 Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 77 | # Module vars, initialized below |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 78 | HOME = ONOS_ROOT = ONOS_USER = None |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 79 | ONOS_APPS = ONOS_WEB_USER = ONOS_WEB_PASS = ONOS_TAR = None |
| 80 | |
| 81 | def 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 88 | global HOME, ONOS_ROOT, ONOS_USER |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 89 | 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 95 | assert environ[ 'HOME' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 96 | HOME = sd( 'HOME', environ[ 'HOME' ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 97 | ONOS_ROOT = sd( 'ONOS_ROOT', join( HOME, 'onos' ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 98 | 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 | |
| 109 | def 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 | |
| 127 | tarDefaultPath = 'buck-out/gen/tools/package/onos-package/onos.tar.gz' |
| 128 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 129 | def unpackONOS( destDir='/tmp', run=quietRun ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 130 | "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 Lantz | a2ccaa5 | 2016-06-29 18:26:06 -0700 | [diff] [blame] | 139 | 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 Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 146 | # 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 Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 151 | # Add symlink to log file |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 152 | run( 'cd %s; ln -s onos*/apache* karaf;' |
| 153 | 'ln -s karaf/data/log/karaf.log log' % destDir, |
| 154 | shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 155 | return onosDir |
| 156 | |
| 157 | |
Bob Lantz | 9ba19dc | 2016-06-13 20:22:07 -0700 | [diff] [blame] | 158 | def 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 Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 170 | info( '.' ) |
| 171 | if callback: |
| 172 | callback() |
| 173 | time.sleep( sleepSecs ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 174 | |
| 175 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 176 | ### Mininet classes |
| 177 | |
| 178 | def 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 | |
| 213 | class ONOSNode( Controller ): |
| 214 | "ONOS cluster node" |
| 215 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 216 | def __init__( self, name, **kwargs ): |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 217 | "alertAction: exception|ignore|warn|exit (exception)" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 218 | kwargs.update( inNamespace=True ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 219 | self.alertAction = kwargs.pop( 'alertAction', 'exception' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 220 | Controller.__init__( self, name, **kwargs ) |
| 221 | self.dir = '/tmp/%s' % self.name |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 222 | self.client = self.dir + '/karaf/bin/client' |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 223 | self.ONOS_HOME = '/tmp' |
Bob Lantz | 55562ea | 2016-06-17 18:19:03 -0700 | [diff] [blame] | 224 | self.cmd( 'rm -rf', self.dir ) |
| 225 | self.ONOS_HOME = unpackONOS( self.dir, run=self.ucmd ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 226 | |
| 227 | # pylint: disable=arguments-differ |
| 228 | |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 229 | def start( self, env, nodes=() ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 230 | """Start ONOS on node |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 231 | env: environment var dict |
| 232 | nodes: all nodes in cluster""" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 233 | env = dict( env ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 234 | 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 Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 240 | self.ucmd( 'mkdir -p config && ' |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 241 | 'onos-gen-partitions config/cluster.json', |
| 242 | ' '.join( node.IP() for node in nodes ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 243 | info( '(starting %s)' % self ) |
| 244 | service = join( self.ONOS_HOME, 'bin/onos-service' ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 245 | self.ucmd( service, 'server 1>../onos.log 2>../onos.log' |
| 246 | ' & echo $! > onos.pid; ln -s `pwd`/onos.pid ..' ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 247 | self.onosPid = int( self.cmd( 'cat onos.pid' ).strip() ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 248 | self.warningCount = 0 |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 249 | |
| 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 Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 257 | 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 269 | def isRunning( self ): |
| 270 | "Is our ONOS process still running?" |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 271 | cmd = ( 'ps -p %d >/dev/null 2>&1 && echo "running" ||' |
| 272 | 'echo "not running"' ) |
| 273 | return self.cmd( cmd % self.onosPid ).strip() == 'running' |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 274 | |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 275 | def checkLog( self ): |
| 276 | "Return log file errors and warnings" |
| 277 | log = join( self.dir, 'log' ) |
Bob Lantz | c96e258 | 2016-06-13 18:57:04 -0700 | [diff] [blame] | 278 | errors, warnings = [], [] |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 279 | 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 301 | if not self.isRunning(): |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 302 | 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 317 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 318 | def waitStarted( self ): |
| 319 | "Wait until we've really started" |
| 320 | info( '(checking: karaf' ) |
| 321 | while True: |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 322 | status = self.ucmd( 'karaf status' ).lower() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 323 | if 'running' in status and 'not running' not in status: |
| 324 | break |
| 325 | info( '.' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 326 | self.sanityCheck() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 327 | time.sleep( 1 ) |
| 328 | info( ' ssh-port' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 329 | waitListening( server=self, port=KarafPort, callback=self.sanityCheck ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 330 | info( ' openflow-port' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 331 | waitListening( server=self, port=OpenFlowPort, |
| 332 | callback=self.sanityCheck ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 333 | info( ' client' ) |
| 334 | while True: |
Bob Lantz | 9ba19dc | 2016-06-13 20:22:07 -0700 | [diff] [blame] | 335 | result = quietRun( '%s -h %s "apps -a"' % |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 336 | ( self.client, self.IP() ), shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 337 | if 'openflow' in result: |
| 338 | break |
| 339 | info( '.' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 340 | self.sanityCheck() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 341 | time.sleep( 1 ) |
Jon Hall | a43d033 | 2016-08-04 15:02:23 -0700 | [diff] [blame] | 342 | 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 Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 353 | info( ')\n' ) |
| 354 | |
| 355 | def updateEnv( self, envDict ): |
| 356 | "Update environment variables" |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 357 | cmd = ';'.join( ( 'export %s="%s"' % ( var, val ) |
| 358 | if val else 'unset %s' % var ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 359 | for var, val in envDict.iteritems() ) |
| 360 | self.cmd( cmd ) |
| 361 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 362 | 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 Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 369 | |
| 370 | class 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 Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 376 | forward: default port forwarding list, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 377 | topo: topology class or instance |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 378 | nodeOpts: ONOSNode options |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 379 | **kwargs: additional topology parameters""" |
| 380 | args = list( args ) |
| 381 | name = args.pop( 0 ) |
| 382 | topo = kwargs.pop( 'topo', None ) |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 383 | self.nat = kwargs.pop( 'nat', 'nat0' ) |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 384 | nodeOpts = kwargs.pop( 'nodeOpts', {} ) |
Jon Hall | 9b238ae | 2016-08-09 13:47:43 -0700 | [diff] [blame] | 385 | # 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 Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 390 | # 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 Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 397 | self.ipBase = kwargs.pop( 'ipBase', '192.168.123.0/24' ) |
| 398 | self.forward = kwargs.pop( 'forward', |
| 399 | [ KarafPort, GUIPort, OpenFlowPort ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 400 | super( ONOSCluster, self ).__init__( name, inNamespace=False ) |
| 401 | fixIPTables() |
| 402 | self.env = initONOSEnv() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 403 | self.net = Mininet( topo=topo, ipBase=self.ipBase, |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 404 | host=partial( ONOSNode, **nodeOpts ), |
| 405 | switch=LinuxBridge, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 406 | controller=None ) |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 407 | if self.nat: |
| 408 | self.net.addNAT( self.nat ).configDefault() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 409 | updateNodeIPs( self.env, self.nodes() ) |
| 410 | self._remoteControllers = [] |
| 411 | |
| 412 | def start( self ): |
| 413 | "Start up ONOS cluster" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 414 | info( '*** ONOS_APPS = %s\n' % ONOS_APPS ) |
| 415 | self.net.start() |
| 416 | for node in self.nodes(): |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 417 | node.start( self.env, self.nodes() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 418 | info( '\n' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 419 | self.configPortForwarding( ports=self.forward, action='A' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 420 | 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 Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 429 | info( '*** Waited %.2f seconds for ONOS startup' % |
| 430 | ( time.time() - startTime ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 431 | |
| 432 | def stop( self ): |
| 433 | "Shut down ONOS cluster" |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 434 | self.configPortForwarding( ports=self.forward, action='D' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 435 | 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 Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 443 | 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 Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 446 | action: A=add/start, D=delete/stop (default: A)""" |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 447 | self.cmd( 'iptables -' + action, 'FORWARD -d', self.ipBase, |
| 448 | '-j ACCEPT' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 449 | 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 Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 453 | self.cmd( 'iptables -t nat -' + action, |
| 454 | 'PREROUTING -t nat -p tcp --dport', inport, |
| 455 | '-j DNAT --to-destination %s:%s' % ( ip, port ) ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 456 | |
Bob Lantz | 930138e | 2016-06-23 18:53:19 -0700 | [diff] [blame] | 457 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 458 | class 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 | |
| 468 | class ONOSOVSSwitch( ONOSSwitchMixin, OVSSwitch ): |
| 469 | "OVSSwitch that can connect to an ONOSCluster" |
| 470 | pass |
| 471 | |
| 472 | class ONOSUserSwitch( ONOSSwitchMixin, UserSwitch): |
| 473 | "UserSwitch that can connect to an ONOSCluster" |
| 474 | pass |
| 475 | |
| 476 | |
| 477 | ### Ugly utility routines |
| 478 | |
| 479 | def 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 | |
| 487 | def 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 | |
| 501 | OldCLI = CLI |
| 502 | |
| 503 | class 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 514 | def onos1( self ): |
| 515 | "Helper function: return default ONOS node" |
| 516 | return self.mn.controllers[ 0 ].net.hosts[ 0 ] |
| 517 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 518 | 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 525 | onos1 = self.onos1().name |
| 526 | if line: |
| 527 | line = '"%s"' % line |
| 528 | cmd = '%s client -h %s %s' % ( onos1, onos1, line ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 529 | 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 Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 542 | "Run tail -f /tmp/onos1/log; press control-C to stop" |
Bob Lantz | 9ba19dc | 2016-06-13 20:22:07 -0700 | [diff] [blame] | 543 | self.default( '%s tail -f /tmp/%s/log' % |
| 544 | ( self.onos1(), self.onos1() ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 545 | |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 546 | 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 Lantz | c96e258 | 2016-06-13 18:57:04 -0700 | [diff] [blame] | 563 | 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 Lantz | c3de515 | 2016-06-17 17:13:57 -0700 | [diff] [blame] | 571 | if '-U' not in quietRun( 'arping -h', shell=True ): |
| 572 | warn( 'Please install iputils-arping.\n' ) |
Bob Lantz | c96e258 | 2016-06-13 18:57:04 -0700 | [diff] [blame] | 573 | 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 Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 589 | |
| 590 | # For interactive use, exit on error |
| 591 | exitOnError = dict( nodeOpts={ 'alertAction': 'exit' } ) |
| 592 | ONOSClusterInteractive = specialClass( ONOSCluster, defaults=exitOnError ) |
| 593 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 594 | |
| 595 | ### Exports for bin/mn |
| 596 | |
| 597 | CLI = ONOSCLI |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 598 | controllers = { 'onos': ONOSClusterInteractive, |
| 599 | 'default': ONOSClusterInteractive } |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 600 | |
| 601 | # XXX Hack to change default controller as above doesn't work |
Bob Lantz | 6438242 | 2016-06-03 22:51:39 -0700 | [diff] [blame] | 602 | findController = lambda: controllers[ 'default' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 603 | |
| 604 | switches = { 'onos': ONOSOVSSwitch, |
| 605 | 'onosovs': ONOSOVSSwitch, |
| 606 | 'onosuser': ONOSUserSwitch, |
| 607 | 'default': ONOSOVSSwitch } |
| 608 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 609 | # Null topology so we can control an external/hardware network |
| 610 | topos = { 'none': Topo } |
| 611 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 612 | if __name__ == '__main__': |
| 613 | if len( argv ) != 2: |
| 614 | test( 3 ) |
| 615 | else: |
| 616 | test( int( argv[ 1 ] ) ) |