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 |
| 47 | from mininet.log import setLogLevel, info |
| 48 | from mininet.cli import CLI |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 49 | from mininet.util import quietRun, waitListening |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 50 | from mininet.clean import killprocs |
| 51 | from mininet.examples.controlnet import MininetFacade |
| 52 | |
| 53 | from os import environ |
| 54 | from os.path import dirname, join, isfile |
| 55 | from sys import argv |
| 56 | from glob import glob |
| 57 | import time |
| 58 | |
| 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 |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 65 | |
| 66 | def defaultUser(): |
| 67 | "Return a reasonable default user" |
| 68 | if 'SUDO_USER' in environ: |
| 69 | return environ[ 'SUDO_USER' ] |
| 70 | try: |
| 71 | user = quietRun( 'who am i' ).split()[ 0 ] |
| 72 | except: |
| 73 | user = 'nobody' |
| 74 | return user |
| 75 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 76 | # Module vars, initialized below |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 77 | HOME = ONOS_ROOT = ONOS_USER = None |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 78 | ONOS_APPS = ONOS_WEB_USER = ONOS_WEB_PASS = ONOS_TAR = None |
| 79 | |
| 80 | def initONOSEnv(): |
| 81 | """Initialize ONOS environment (and module) variables |
| 82 | This is ugly and painful, but they have to be set correctly |
| 83 | in order for the onos-setup-karaf script to work. |
| 84 | nodes: list of ONOS nodes |
| 85 | returns: ONOS environment variable dict""" |
| 86 | # pylint: disable=global-statement |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 87 | global HOME, ONOS_ROOT, ONOS_USER |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 88 | global ONOS_APPS, ONOS_WEB_USER, ONOS_WEB_PASS |
| 89 | env = {} |
| 90 | def sd( var, val ): |
| 91 | "Set default value for environment variable" |
| 92 | env[ var ] = environ.setdefault( var, val ) |
| 93 | return env[ var ] |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 94 | assert environ[ 'HOME' ] |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 95 | HOME = sd( 'HOME', environ[ 'HOME' ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 96 | ONOS_ROOT = sd( 'ONOS_ROOT', join( HOME, 'onos' ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 97 | environ[ 'ONOS_USER' ] = defaultUser() |
| 98 | ONOS_USER = sd( 'ONOS_USER', defaultUser() ) |
| 99 | ONOS_APPS = sd( 'ONOS_APPS', |
| 100 | 'drivers,openflow,fwd,proxyarp,mobility' ) |
| 101 | # ONOS_WEB_{USER,PASS} isn't respected by onos-karaf: |
| 102 | environ.update( ONOS_WEB_USER='karaf', ONOS_WEB_PASS='karaf' ) |
| 103 | ONOS_WEB_USER = sd( 'ONOS_WEB_USER', 'karaf' ) |
| 104 | ONOS_WEB_PASS = sd( 'ONOS_WEB_PASS', 'karaf' ) |
| 105 | return env |
| 106 | |
| 107 | |
| 108 | def updateNodeIPs( env, nodes ): |
| 109 | "Update env dict and environ with node IPs" |
| 110 | # Get rid of stale junk |
| 111 | for var in 'ONOS_NIC', 'ONOS_CELL', 'ONOS_INSTANCES': |
| 112 | env[ var ] = '' |
| 113 | for var in environ.keys(): |
| 114 | if var.startswith( 'OC' ): |
| 115 | env[ var ] = '' |
| 116 | for index, node in enumerate( nodes, 1 ): |
| 117 | var = 'OC%d' % index |
| 118 | env[ var ] = node.IP() |
| 119 | env[ 'OCI' ] = env[ 'OCN' ] = env[ 'OC1' ] |
| 120 | env[ 'ONOS_INSTANCES' ] = '\n'.join( |
| 121 | node.IP() for node in nodes ) |
| 122 | environ.update( env ) |
| 123 | return env |
| 124 | |
| 125 | |
| 126 | tarDefaultPath = 'buck-out/gen/tools/package/onos-package/onos.tar.gz' |
| 127 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 128 | def unpackONOS( destDir='/tmp', run=quietRun ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 129 | "Unpack ONOS and return its location" |
| 130 | global ONOS_TAR |
| 131 | environ.setdefault( 'ONOS_TAR', join( ONOS_ROOT, tarDefaultPath ) ) |
| 132 | ONOS_TAR = environ[ 'ONOS_TAR' ] |
| 133 | tarPath = ONOS_TAR |
| 134 | if not isfile( tarPath ): |
| 135 | raise Exception( 'Missing ONOS tarball %s - run buck build onos?' |
| 136 | % tarPath ) |
| 137 | info( '(unpacking %s)' % destDir) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 138 | cmds = ( 'mkdir -p "%s" && cd "%s" && tar xzf "%s"' |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 139 | % ( destDir, destDir, tarPath) ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 140 | run( cmds, shell=True, verbose=True ) |
| 141 | # We can use quietRun for this usually |
| 142 | tarOutput = quietRun( 'tar tzf "%s" | head -1' % tarPath, shell=True) |
| 143 | tarOutput = tarOutput.split()[ 0 ].strip() |
| 144 | assert '/' in tarOutput |
| 145 | onosDir = join( destDir, dirname( tarOutput ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 146 | # Add symlink to log file |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 147 | run( 'cd %s; ln -s onos*/apache* karaf;' |
| 148 | 'ln -s karaf/data/log/karaf.log log' % destDir, |
| 149 | shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 150 | return onosDir |
| 151 | |
| 152 | |
| 153 | ### Mininet classes |
| 154 | |
| 155 | def RenamedTopo( topo, *args, **kwargs ): |
| 156 | """Return specialized topo with renamed hosts |
| 157 | topo: topo class/class name to specialize |
| 158 | args, kwargs: topo args |
| 159 | sold: old switch name prefix (default 's') |
| 160 | snew: new switch name prefix |
| 161 | hold: old host name prefix (default 'h') |
| 162 | hnew: new host name prefix |
| 163 | This may be used from the mn command, e.g. |
| 164 | mn --topo renamed,single,spref=sw,hpref=host""" |
| 165 | sold = kwargs.pop( 'sold', 's' ) |
| 166 | hold = kwargs.pop( 'hold', 'h' ) |
| 167 | snew = kwargs.pop( 'snew', 'cs' ) |
| 168 | hnew = kwargs.pop( 'hnew' ,'ch' ) |
| 169 | topos = {} # TODO: use global TOPOS dict |
| 170 | if isinstance( topo, str ): |
| 171 | # Look up in topo directory - this allows us to |
| 172 | # use RenamedTopo from the command line! |
| 173 | if topo in topos: |
| 174 | topo = topos.get( topo ) |
| 175 | else: |
| 176 | raise Exception( 'Unknown topo name: %s' % topo ) |
| 177 | # pylint: disable=no-init |
| 178 | class RenamedTopoCls( topo ): |
| 179 | "Topo subclass with renamed nodes" |
| 180 | def addNode( self, name, *args, **kwargs ): |
| 181 | "Add a node, renaming if necessary" |
| 182 | if name.startswith( sold ): |
| 183 | name = snew + name[ len( sold ): ] |
| 184 | elif name.startswith( hold ): |
| 185 | name = hnew + name[ len( hold ): ] |
| 186 | return topo.addNode( self, name, *args, **kwargs ) |
| 187 | return RenamedTopoCls( *args, **kwargs ) |
| 188 | |
| 189 | |
| 190 | class ONOSNode( Controller ): |
| 191 | "ONOS cluster node" |
| 192 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 193 | def __init__( self, name, **kwargs ): |
| 194 | kwargs.update( inNamespace=True ) |
| 195 | Controller.__init__( self, name, **kwargs ) |
| 196 | self.dir = '/tmp/%s' % self.name |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 197 | self.client = self.dir + '/karaf/bin/client' |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 198 | self.ONOS_HOME = '/tmp' |
| 199 | |
| 200 | # pylint: disable=arguments-differ |
| 201 | |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 202 | def start( self, env, nodes=() ): |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 203 | """Start ONOS on node |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 204 | env: environment var dict |
| 205 | nodes: all nodes in cluster""" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 206 | env = dict( env ) |
| 207 | self.cmd( 'rm -rf', self.dir ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 208 | self.ONOS_HOME = unpackONOS( self.dir, run=self.ucmd ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 209 | env.update( ONOS_HOME=self.ONOS_HOME ) |
| 210 | self.updateEnv( env ) |
| 211 | karafbin = glob( '%s/apache*/bin' % self.ONOS_HOME )[ 0 ] |
| 212 | onosbin = join( ONOS_ROOT, 'tools/test/bin' ) |
| 213 | self.cmd( 'export PATH=%s:%s:$PATH' % ( onosbin, karafbin ) ) |
| 214 | self.cmd( 'cd', self.ONOS_HOME ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 215 | self.ucmd( 'mkdir -p config && ' |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 216 | 'onos-gen-partitions config/cluster.json', |
| 217 | ' '.join( node.IP() for node in nodes ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 218 | info( '(starting %s)' % self ) |
| 219 | service = join( self.ONOS_HOME, 'bin/onos-service' ) |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 220 | self.ucmd( service, 'server 1>../onos.log 2>../onos.log' |
| 221 | ' & echo $! > onos.pid; ln -s `pwd`/onos.pid ..' ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 222 | self.onosPid = int( self.cmd( 'cat onos.pid' ).strip() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 223 | |
| 224 | # pylint: enable=arguments-differ |
| 225 | |
| 226 | def stop( self ): |
| 227 | # XXX This will kill all karafs - too bad! |
| 228 | self.cmd( 'pkill -HUP -f karaf.jar && wait' ) |
| 229 | self.cmd( 'rm -rf', self.dir ) |
| 230 | |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 231 | def isRunning( self ): |
| 232 | "Is our ONOS process still running?" |
| 233 | cmd = 'ps -p %d >/dev/null 2>&1 && echo "running" || echo "not running"' |
| 234 | return self.cmd( cmd % self.onosPid ) == 'running' |
| 235 | |
| 236 | def sanityCheck( self ): |
| 237 | "Check whether we've quit or are running out of memory" |
| 238 | if not self.isRunning(): |
| 239 | raise Exception( 'ONOS node %s has died' % self.name ) |
| 240 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 241 | def waitStarted( self ): |
| 242 | "Wait until we've really started" |
| 243 | info( '(checking: karaf' ) |
| 244 | while True: |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 245 | status = self.ucmd( 'karaf status' ).lower() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 246 | if 'running' in status and 'not running' not in status: |
| 247 | break |
| 248 | info( '.' ) |
| 249 | time.sleep( 1 ) |
| 250 | info( ' ssh-port' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 251 | waitListening( server=self, port=KarafPort ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 252 | info( ' openflow-port' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 253 | waitListening( server=self, port=OpenFlowPort ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 254 | info( ' client' ) |
| 255 | while True: |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 256 | result = quietRun( 'echo apps -a | %s -h %s' % |
| 257 | ( self.client, self.IP() ), shell=True ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 258 | if 'openflow' in result: |
| 259 | break |
| 260 | info( '.' ) |
| 261 | time.sleep( 1 ) |
| 262 | info( ')\n' ) |
| 263 | |
| 264 | def updateEnv( self, envDict ): |
| 265 | "Update environment variables" |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 266 | cmd = ';'.join( ( 'export %s="%s"' % ( var, val ) |
| 267 | if val else 'unset %s' % var ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 268 | for var, val in envDict.iteritems() ) |
| 269 | self.cmd( cmd ) |
| 270 | |
Bob Lantz | 1451d72 | 2016-05-17 14:40:07 -0700 | [diff] [blame] | 271 | def ucmd( self, *args, **_kwargs ): |
| 272 | "Run command as $ONOS_USER using sudo -E -u" |
| 273 | if ONOS_USER != 'root': # don't bother with sudo |
| 274 | args = [ "sudo -E -u $ONOS_USER PATH=$PATH " |
| 275 | "bash -c '%s'" % ' '.join( args ) ] |
| 276 | return self.cmd( *args ) |
| 277 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 278 | |
| 279 | class ONOSCluster( Controller ): |
| 280 | "ONOS Cluster" |
| 281 | def __init__( self, *args, **kwargs ): |
| 282 | """name: (first parameter) |
| 283 | *args: topology class parameters |
| 284 | ipBase: IP range for ONOS nodes |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 285 | forward: default port forwarding list, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 286 | topo: topology class or instance |
| 287 | **kwargs: additional topology parameters""" |
| 288 | args = list( args ) |
| 289 | name = args.pop( 0 ) |
| 290 | topo = kwargs.pop( 'topo', None ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 291 | nat = kwargs.pop( 'nat', 'nat0' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 292 | # Default: single switch with 1 ONOS node |
| 293 | if not topo: |
| 294 | topo = SingleSwitchTopo |
| 295 | if not args: |
| 296 | args = ( 1, ) |
| 297 | if not isinstance( topo, Topo ): |
| 298 | topo = RenamedTopo( topo, *args, hnew='onos', **kwargs ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 299 | self.ipBase = kwargs.pop( 'ipBase', '192.168.123.0/24' ) |
| 300 | self.forward = kwargs.pop( 'forward', |
| 301 | [ KarafPort, GUIPort, OpenFlowPort ] ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 302 | super( ONOSCluster, self ).__init__( name, inNamespace=False ) |
| 303 | fixIPTables() |
| 304 | self.env = initONOSEnv() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 305 | self.net = Mininet( topo=topo, ipBase=self.ipBase, |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 306 | host=ONOSNode, switch=LinuxBridge, |
| 307 | controller=None ) |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 308 | if nat: |
| 309 | self.net.addNAT( nat ).configDefault() |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 310 | updateNodeIPs( self.env, self.nodes() ) |
| 311 | self._remoteControllers = [] |
| 312 | |
| 313 | def start( self ): |
| 314 | "Start up ONOS cluster" |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 315 | info( '*** ONOS_APPS = %s\n' % ONOS_APPS ) |
| 316 | self.net.start() |
| 317 | for node in self.nodes(): |
Bob Lantz | 569bbec | 2016-06-03 18:51:16 -0700 | [diff] [blame] | 318 | node.start( self.env, self.nodes() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 319 | info( '\n' ) |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 320 | self.configPortForwarding( ports=self.forward, action='A' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 321 | self.waitStarted() |
| 322 | return |
| 323 | |
| 324 | def waitStarted( self ): |
| 325 | "Wait until all nodes have started" |
| 326 | startTime = time.time() |
| 327 | for node in self.nodes(): |
| 328 | info( node ) |
| 329 | node.waitStarted() |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 330 | info( '*** Waited %.2f seconds for ONOS startup' % |
| 331 | ( time.time() - startTime ) ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 332 | |
| 333 | def stop( self ): |
| 334 | "Shut down ONOS cluster" |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 335 | self.configPortForwarding( ports=self.forward, action='D' ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 336 | for node in self.nodes(): |
| 337 | node.stop() |
| 338 | self.net.stop() |
| 339 | |
| 340 | def nodes( self ): |
| 341 | "Return list of ONOS nodes" |
| 342 | return [ h for h in self.net.hosts if isinstance( h, ONOSNode ) ] |
| 343 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 344 | def configPortForwarding( self, ports=[], intf='eth0', action='A' ): |
| 345 | """Start or stop ports on intf to all nodes |
| 346 | action: A=add/start, D=delete/stop (default: A)""" |
| 347 | for port in ports: |
| 348 | for index, node in enumerate( self.nodes() ): |
| 349 | ip, inport = node.IP(), port + index |
| 350 | # Configure a destination NAT rule |
| 351 | cmd = ( 'iptables -t nat -{action} PREROUTING -t nat ' |
| 352 | '-i {intf} -p tcp --dport {inport} ' |
| 353 | '-j DNAT --to-destination {ip}:{port}' ) |
| 354 | self.cmd( cmd.format( **locals() ) ) |
| 355 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 356 | |
| 357 | class ONOSSwitchMixin( object ): |
| 358 | "Mixin for switches that connect to an ONOSCluster" |
| 359 | def start( self, controllers ): |
| 360 | "Connect to ONOSCluster" |
| 361 | self.controllers = controllers |
| 362 | assert ( len( controllers ) is 1 and |
| 363 | isinstance( controllers[ 0 ], ONOSCluster ) ) |
| 364 | clist = controllers[ 0 ].nodes() |
| 365 | return super( ONOSSwitchMixin, self ).start( clist ) |
| 366 | |
| 367 | class ONOSOVSSwitch( ONOSSwitchMixin, OVSSwitch ): |
| 368 | "OVSSwitch that can connect to an ONOSCluster" |
| 369 | pass |
| 370 | |
| 371 | class ONOSUserSwitch( ONOSSwitchMixin, UserSwitch): |
| 372 | "UserSwitch that can connect to an ONOSCluster" |
| 373 | pass |
| 374 | |
| 375 | |
| 376 | ### Ugly utility routines |
| 377 | |
| 378 | def fixIPTables(): |
| 379 | "Fix LinuxBridge warning" |
| 380 | for s in 'arp', 'ip', 'ip6': |
| 381 | quietRun( 'sysctl net.bridge.bridge-nf-call-%stables=0' % s ) |
| 382 | |
| 383 | |
| 384 | ### Test code |
| 385 | |
| 386 | def test( serverCount ): |
| 387 | "Test this setup" |
| 388 | setLogLevel( 'info' ) |
| 389 | net = Mininet( topo=SingleSwitchTopo( 3 ), |
| 390 | controller=[ ONOSCluster( 'c0', serverCount ) ], |
| 391 | switch=ONOSOVSSwitch ) |
| 392 | net.start() |
| 393 | net.waitConnected() |
| 394 | CLI( net ) |
| 395 | net.stop() |
| 396 | |
| 397 | |
| 398 | ### CLI Extensions |
| 399 | |
| 400 | OldCLI = CLI |
| 401 | |
| 402 | class ONOSCLI( OldCLI ): |
| 403 | "CLI Extensions for ONOS" |
| 404 | |
| 405 | prompt = 'mininet-onos> ' |
| 406 | |
| 407 | def __init__( self, net, **kwargs ): |
| 408 | c0 = net.controllers[ 0 ] |
| 409 | if isinstance( c0, ONOSCluster ): |
| 410 | net = MininetFacade( net, cnet=c0.net ) |
| 411 | OldCLI.__init__( self, net, **kwargs ) |
| 412 | |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 413 | def onos1( self ): |
| 414 | "Helper function: return default ONOS node" |
| 415 | return self.mn.controllers[ 0 ].net.hosts[ 0 ] |
| 416 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 417 | def do_onos( self, line ): |
| 418 | "Send command to ONOS CLI" |
| 419 | c0 = self.mn.controllers[ 0 ] |
| 420 | if isinstance( c0, ONOSCluster ): |
| 421 | # cmdLoop strips off command name 'onos' |
| 422 | if line.startswith( ':' ): |
| 423 | line = 'onos' + line |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 424 | onos1 = self.onos1().name |
| 425 | if line: |
| 426 | line = '"%s"' % line |
| 427 | cmd = '%s client -h %s %s' % ( onos1, onos1, line ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 428 | quietRun( 'stty -echo' ) |
| 429 | self.default( cmd ) |
| 430 | quietRun( 'stty echo' ) |
| 431 | |
| 432 | def do_wait( self, line ): |
| 433 | "Wait for switches to connect" |
| 434 | self.mn.waitConnected() |
| 435 | |
| 436 | def do_balance( self, line ): |
| 437 | "Balance switch mastership" |
| 438 | self.do_onos( ':balance-masters' ) |
| 439 | |
| 440 | def do_log( self, line ): |
Bob Lantz | 4b51d5c | 2016-05-27 14:47:38 -0700 | [diff] [blame] | 441 | "Run tail -f /tmp/onos1/log; press control-C to stop" |
| 442 | self.default( self.onos1().name, 'tail -f /tmp/%s/log' % self.onos1() ) |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 443 | |
| 444 | |
| 445 | ### Exports for bin/mn |
| 446 | |
| 447 | CLI = ONOSCLI |
| 448 | |
| 449 | controllers = { 'onos': ONOSCluster, 'default': ONOSCluster } |
| 450 | |
| 451 | # XXX Hack to change default controller as above doesn't work |
| 452 | findController = lambda: ONOSCluster |
| 453 | |
| 454 | switches = { 'onos': ONOSOVSSwitch, |
| 455 | 'onosovs': ONOSOVSSwitch, |
| 456 | 'onosuser': ONOSUserSwitch, |
| 457 | 'default': ONOSOVSSwitch } |
| 458 | |
Bob Lantz | bb37d87 | 2016-05-16 16:26:13 -0700 | [diff] [blame] | 459 | # Null topology so we can control an external/hardware network |
| 460 | topos = { 'none': Topo } |
| 461 | |
Bob Lantz | 087b5d9 | 2016-05-06 11:39:04 -0700 | [diff] [blame] | 462 | if __name__ == '__main__': |
| 463 | if len( argv ) != 2: |
| 464 | test( 3 ) |
| 465 | else: |
| 466 | test( int( argv[ 1 ] ) ) |