admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Oct-2012 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2016 by ON.Lab |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 14 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 19 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 20 | You should have received a copy of the GNU General Public License |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 22 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 23 | MininetCliDriver is the basic driver which will handle the Mininet functions |
| 24 | |
| 25 | Some functions rely on a modified version of Mininet. These functions |
| 26 | should all be noted in the comments. To get this MN version run these commands |
| 27 | from within your Mininet folder: |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 28 | git remote add jhall11 https://github.com/jhall11/mininet.git |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 29 | git fetch jhall11 |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 30 | git checkout -b dynamic_topo remotes/jhall11/dynamic_topo |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 31 | git pull |
| 32 | |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 33 | |
| 34 | Note that you may need to run 'sudo make develop' if your mnexec.c file |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 35 | changed when switching branches.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 36 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 37 | import re |
| 38 | import sys |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 39 | import types |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 40 | import os |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 41 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 42 | from drivers.common.cli.emulatordriver import Emulator |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 43 | from core.graph import Graph |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 45 | |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 46 | class MininetCliDriver( Emulator ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 47 | |
| 48 | """ |
| 49 | MininetCliDriver is the basic driver which will handle |
| 50 | the Mininet functions""" |
| 51 | def __init__( self ): |
| 52 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | self.handle = self |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 54 | self.name = None |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 55 | self.home = None |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 56 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | self.flag = 0 |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 58 | # TODO: Refactor driver to use these everywhere |
| 59 | self.mnPrompt = "mininet>" |
| 60 | self.hostPrompt = "~#" |
| 61 | self.bashPrompt = "\$" |
| 62 | self.scapyPrompt = ">>>" |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 63 | self.graph = Graph() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 65 | def connect( self, **connectargs ): |
| 66 | """ |
| 67 | Here the main is the TestON instance after creating |
| 68 | all the log handles.""" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 69 | try: |
| 70 | for key in connectargs: |
| 71 | vars( self )[ key ] = connectargs[ key ] |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 72 | self.home = "~/mininet" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 73 | self.name = self.options[ 'name' ] |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 74 | for key in self.options: |
| 75 | if key == "home": |
| 76 | self.home = self.options[ 'home' ] |
| 77 | break |
| 78 | if self.home is None or self.home == "": |
| 79 | self.home = "~/mininet" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 80 | |
| 81 | try: |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 82 | if os.getenv( str( self.ip_address ) ) is not None: |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 83 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 84 | else: |
| 85 | main.log.info( self.name + |
| 86 | ": Trying to connect to " + |
| 87 | self.ip_address ) |
| 88 | |
| 89 | except KeyError: |
| 90 | main.log.info( "Invalid host name," + |
| 91 | " connecting to local host instead" ) |
| 92 | self.ip_address = 'localhost' |
| 93 | except Exception as inst: |
| 94 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 95 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 96 | self.handle = super( |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 97 | MininetCliDriver, |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 98 | self ).connect( |
| 99 | user_name=self.user_name, |
| 100 | ip_address=self.ip_address, |
| 101 | port=None, |
| 102 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 103 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 104 | if self.handle: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 105 | main.log.info( "Connection successful to the host " + |
| 106 | self.user_name + |
| 107 | "@" + |
| 108 | self.ip_address ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 109 | return main.TRUE |
| 110 | else: |
| 111 | main.log.error( "Connection failed to the host " + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 112 | self.user_name + |
| 113 | "@" + |
| 114 | self.ip_address ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 115 | main.log.error( "Failed to connect to the Mininet CLI" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 116 | return main.FALSE |
| 117 | except pexpect.EOF: |
| 118 | main.log.error( self.name + ": EOF exception found" ) |
| 119 | main.log.error( self.name + ": " + self.handle.before ) |
| 120 | main.cleanup() |
| 121 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 122 | except Exception: |
| 123 | main.log.exception( self.name + ": Uncaught exception!" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 124 | main.cleanup() |
| 125 | main.exit() |
| 126 | |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 127 | def startNet( self, topoFile='', args='', mnCmd='', timeout=120 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 128 | """ |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 129 | Description: |
| 130 | Starts Mininet accepts a topology(.py) file and/or an optional |
| 131 | argument, to start the mininet, as a parameter. |
| 132 | Can also send regular mininet command to load up desired topology. |
alison | 12f34c3 | 2016-06-10 14:39:21 -0700 | [diff] [blame] | 133 | Eg. Pass in a string 'mn --topo=tree,3,3' to mnCmd |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 134 | Options: |
| 135 | topoFile = file path for topology file (.py) |
| 136 | args = extra option added when starting the topology from the file |
| 137 | mnCmd = Mininet command use to start topology |
| 138 | Returns: |
| 139 | main.TRUE if the mininet starts successfully, main.FALSE |
| 140 | otherwise |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 141 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 142 | try: |
| 143 | if self.handle: |
| 144 | # make sure old networks are cleaned up |
| 145 | main.log.info( self.name + |
| 146 | ": Clearing any residual state or processes" ) |
| 147 | self.handle.sendline( "sudo mn -c" ) |
| 148 | i = self.handle.expect( [ 'password\sfor\s', |
| 149 | 'Cleanup\scomplete', |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 150 | pexpect.EOF, |
| 151 | pexpect.TIMEOUT ], |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 152 | timeout ) |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 153 | if i == 0: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 154 | # Sudo asking for password |
| 155 | main.log.info( self.name + ": Sending sudo password" ) |
| 156 | self.handle.sendline( self.pwd ) |
| 157 | i = self.handle.expect( [ '%s:' % self.user, |
| 158 | '\$', |
| 159 | pexpect.EOF, |
| 160 | pexpect.TIMEOUT ], |
| 161 | timeout ) |
| 162 | if i == 1: |
| 163 | main.log.info( self.name + ": Clean" ) |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 164 | elif i == 2: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 165 | main.log.error( self.name + ": Connection terminated" ) |
| 166 | elif i == 3: # timeout |
| 167 | main.log.error( self.name + ": Something while cleaning " + |
| 168 | "Mininet took too long... " ) |
| 169 | # Craft the string to start mininet |
| 170 | cmdString = "sudo " |
| 171 | if not mnCmd: |
| 172 | if topoFile is None or topoFile == '': # If no file is given |
| 173 | main.log.info( self.name + ": building fresh Mininet" ) |
| 174 | cmdString += "mn " |
| 175 | if args is None or args == '': |
| 176 | # If no args given, use args from .topo file |
| 177 | args = self.options[ 'arg1' ] +\ |
| 178 | " " + self.options[ 'arg2' ] +\ |
| 179 | " --mac --controller " +\ |
| 180 | self.options[ 'controller' ] + " " +\ |
| 181 | self.options[ 'arg3' ] |
| 182 | else: # else only use given args |
| 183 | pass |
| 184 | # TODO: allow use of topo args and method args? |
| 185 | else: # Use given topology file |
| 186 | main.log.info( |
| 187 | "Starting Mininet from topo file " + |
| 188 | topoFile ) |
| 189 | cmdString += "-E python " + topoFile + " " |
| 190 | if args is None: |
| 191 | args = '' |
| 192 | # TODO: allow use of args from .topo file? |
| 193 | cmdString += args |
| 194 | else: |
| 195 | main.log.info( "Starting Mininet topology using '" + mnCmd + |
| 196 | "' command" ) |
| 197 | cmdString += mnCmd |
| 198 | # Send the command and check if network started |
| 199 | self.handle.sendline( "" ) |
| 200 | self.handle.expect( '\$' ) |
| 201 | main.log.info( "Sending '" + cmdString + "' to " + self.name ) |
| 202 | self.handle.sendline( cmdString ) |
| 203 | while True: |
| 204 | i = self.handle.expect( [ 'mininet>', |
| 205 | 'Exception', |
| 206 | '\*\*\*', |
| 207 | pexpect.EOF, |
| 208 | pexpect.TIMEOUT ], |
| 209 | timeout ) |
| 210 | if i == 0: |
| 211 | main.log.info( self.name + ": Mininet built" ) |
| 212 | return main.TRUE |
| 213 | elif i == 1: |
| 214 | response = str( self.handle.before + |
| 215 | self.handle.after ) |
| 216 | self.handle.expect( '\$' ) |
| 217 | response += str( self.handle.before + |
| 218 | self.handle.after ) |
| 219 | main.log.error( |
| 220 | self.name + |
| 221 | ": Launching Mininet failed: " + response ) |
| 222 | return main.FALSE |
| 223 | elif i == 2: |
| 224 | self.handle.expect( [ "\n", |
| 225 | pexpect.EOF, |
| 226 | pexpect.TIMEOUT ], |
| 227 | timeout ) |
| 228 | main.log.info( self.handle.before ) |
| 229 | elif i == 3: |
| 230 | main.log.error( self.name + ": Connection timeout" ) |
| 231 | return main.FALSE |
| 232 | elif i == 4: # timeout |
| 233 | main.log.error( |
| 234 | self.name + |
| 235 | ": Something took too long... " ) |
| 236 | return main.FALSE |
| 237 | # Why did we hit this part? |
| 238 | main.log.error( "startNet did not return correctly" ) |
| 239 | return main.FASLE |
| 240 | else: # if no handle |
| 241 | main.log.error( self.name + ": Connection failed to the host " + |
| 242 | self.user_name + "@" + self.ip_address ) |
| 243 | main.log.error( self.name + ": Failed to connect to the Mininet" ) |
| 244 | return main.FALSE |
| 245 | except pexpect.TIMEOUT: |
| 246 | main.log.exception( self.name + ": TIMEOUT exception found while starting Mininet" ) |
| 247 | main.log.error( self.name + ": " + self.handle.before ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 248 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 249 | except pexpect.EOF: |
| 250 | main.log.error( self.name + ": EOF exception found" ) |
| 251 | main.log.error( self.name + ": " + self.handle.before ) |
| 252 | main.cleanup() |
| 253 | main.exit() |
| 254 | except Exception: |
| 255 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 256 | main.cleanup() |
| 257 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 258 | |
kelvin-onlab | fccaafa | 2015-01-20 13:50:44 -0800 | [diff] [blame] | 259 | def numSwitchesNlinks( self, topoType, depth, fanout ): |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 260 | try: |
| 261 | if topoType == 'tree': |
| 262 | # In tree topology, if fanout arg is not given, by default it is 2 |
| 263 | if fanout is None: |
| 264 | fanout = 2 |
| 265 | k = 0 |
| 266 | count = 0 |
| 267 | while( k <= depth - 1 ): |
| 268 | count = count + pow( fanout, k ) |
| 269 | k = k + 1 |
| 270 | numSwitches = count |
| 271 | while( k <= depth - 2 ): |
| 272 | # depth-2 gives you only core links and not considering |
| 273 | # edge links as seen by ONOS. If all the links including |
| 274 | # edge links are required, do depth-1 |
| 275 | count = count + pow( fanout, k ) |
| 276 | k = k + 1 |
| 277 | numLinks = count * fanout |
| 278 | # print "num_switches for %s(%d,%d) = %d and links=%d" %( |
| 279 | # topoType,depth,fanout,numSwitches,numLinks ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 280 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 281 | elif topoType == 'linear': |
| 282 | # In linear topology, if fanout or numHostsPerSw is not given, |
| 283 | # by default it is 1 |
| 284 | if fanout is None: |
| 285 | fanout = 1 |
| 286 | numSwitches = depth |
| 287 | numHostsPerSw = fanout |
| 288 | totalNumHosts = numSwitches * numHostsPerSw |
| 289 | numLinks = totalNumHosts + ( numSwitches - 1 ) |
| 290 | print "num_switches for %s(%d,%d) = %d and links=%d" %\ |
| 291 | ( topoType, depth, fanout, numSwitches, numLinks ) |
| 292 | topoDict = { "num_switches": int( numSwitches ), |
| 293 | "num_corelinks": int( numLinks ) } |
| 294 | return topoDict |
| 295 | except Exception: |
| 296 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 297 | main.cleanup() |
| 298 | main.exit() |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 299 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 300 | def calculateSwAndLinks( self ): |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 301 | """ |
| 302 | Calculate the number of switches and links in a topo.""" |
| 303 | # TODO: combine this function and numSwitchesNlinks |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 304 | try: |
| 305 | argList = self.options[ 'arg1' ].split( "," ) |
| 306 | topoArgList = argList[ 0 ].split( " " ) |
| 307 | argList = map( int, argList[ 1: ] ) |
| 308 | topoArgList = topoArgList[ 1: ] + argList |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 309 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 310 | topoDict = self.numSwitchesNlinks( *topoArgList ) |
| 311 | return topoDict |
| 312 | except Exception: |
| 313 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 314 | main.cleanup() |
| 315 | main.exit() |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 316 | |
GlennRC | f07c44a | 2015-09-18 13:33:46 -0700 | [diff] [blame] | 317 | def pingall( self, protocol="IPv4", timeout=300, shortCircuit=False, acceptableFailed=0 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 318 | """ |
| 319 | Verifies the reachability of the hosts using pingall command. |
| 320 | Optional parameter timeout allows you to specify how long to |
| 321 | wait for pingall to complete |
kelvin-onlab | fbcd82f | 2015-04-02 12:06:00 -0700 | [diff] [blame] | 322 | Optional: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 323 | timeout( seconds ) - How long to wait before breaking the pingall |
kelvin-onlab | fbcd82f | 2015-04-02 12:06:00 -0700 | [diff] [blame] | 324 | shortCircuit - Break the pingall based on the number of failed hosts |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 325 | ping |
| 326 | acceptableFailed - Set the number of acceptable failed pings for the |
| 327 | function to still return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 328 | Returns: |
| 329 | main.TRUE if pingall completes with no pings dropped |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 330 | otherwise main.FALSE |
| 331 | """ |
| 332 | import time |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 333 | try: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 334 | timeout = int( timeout ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 335 | if self.handle: |
| 336 | main.log.info( |
| 337 | self.name + |
| 338 | ": Checking reachabilty to the hosts using pingall" ) |
| 339 | response = "" |
| 340 | failedPings = 0 |
| 341 | returnValue = main.TRUE |
GlennRC | f07c44a | 2015-09-18 13:33:46 -0700 | [diff] [blame] | 342 | cmd = "pingall" |
| 343 | if protocol == "IPv6": |
| 344 | cmd = "py net.pingAll6()" |
| 345 | self.handle.sendline( cmd ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 346 | startTime = time.time() |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 347 | while True: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 348 | i = self.handle.expect( [ "mininet>", "X", |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 349 | pexpect.EOF, |
| 350 | pexpect.TIMEOUT ], |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 351 | timeout ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 352 | if i == 0: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 353 | main.log.info( self.name + ": pingall finished" ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 354 | response += self.handle.before |
| 355 | break |
| 356 | elif i == 1: |
| 357 | response += self.handle.before + self.handle.after |
| 358 | failedPings = failedPings + 1 |
kelvin-onlab | d26a374 | 2015-04-06 15:31:16 -0700 | [diff] [blame] | 359 | if failedPings > acceptableFailed: |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 360 | returnValue = main.FALSE |
| 361 | if shortCircuit: |
| 362 | main.log.error( self.name + |
| 363 | ": Aborting pingall - " |
| 364 | + str( failedPings ) + |
| 365 | " pings failed" ) |
| 366 | break |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 367 | if ( time.time() - startTime ) > timeout: |
| 368 | returnValue = main.FALSE |
| 369 | main.log.error( self.name + |
| 370 | ": Aborting pingall - " + |
| 371 | "Function took too long " ) |
| 372 | break |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 373 | elif i == 2: |
| 374 | main.log.error( self.name + |
| 375 | ": EOF exception found" ) |
| 376 | main.log.error( self.name + ": " + |
| 377 | self.handle.before ) |
| 378 | main.cleanup() |
| 379 | main.exit() |
| 380 | elif i == 3: |
| 381 | response += self.handle.before |
| 382 | main.log.error( self.name + |
| 383 | ": TIMEOUT exception found" ) |
| 384 | main.log.error( self.name + |
| 385 | ": " + |
| 386 | str( response ) ) |
| 387 | # NOTE: Send ctrl-c to make sure pingall is done |
You Wang | af68431 | 2016-01-27 14:44:38 -0800 | [diff] [blame] | 388 | self.handle.send( "\x03" ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 389 | self.handle.expect( "Interrupt" ) |
| 390 | self.handle.expect( "mininet>" ) |
| 391 | break |
| 392 | pattern = "Results\:" |
| 393 | main.log.info( "Pingall output: " + str( response ) ) |
| 394 | if re.search( pattern, response ): |
| 395 | main.log.info( self.name + ": Pingall finished with " |
| 396 | + str( failedPings ) + " failed pings" ) |
| 397 | return returnValue |
| 398 | else: |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 399 | # NOTE: Send ctrl-c to make sure pingall is done |
You Wang | af68431 | 2016-01-27 14:44:38 -0800 | [diff] [blame] | 400 | self.handle.send( "\x03" ) |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 401 | self.handle.expect( "Interrupt" ) |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 402 | self.handle.expect( "mininet>" ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 403 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 404 | else: |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 405 | main.log.error( self.name + ": Connection failed to the host" ) |
| 406 | main.cleanup() |
| 407 | main.exit() |
| 408 | except pexpect.TIMEOUT: |
| 409 | if response: |
| 410 | main.log.info( "Pingall output: " + str( response ) ) |
| 411 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 412 | return main.FALSE |
| 413 | except pexpect.EOF: |
| 414 | main.log.error( self.name + ": EOF exception found" ) |
| 415 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 416 | main.cleanup() |
| 417 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 418 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 419 | def fpingHost( self, **pingParams ): |
| 420 | """ |
| 421 | Uses the fping package for faster pinging... |
| 422 | *requires fping to be installed on machine running mininet""" |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 423 | try: |
| 424 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
| 425 | command = args[ "SRC" ] + \ |
| 426 | " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ] |
| 427 | self.handle.sendline( command ) |
| 428 | self.handle.expect( |
| 429 | [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] ) |
| 430 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 431 | response = self.handle.before |
| 432 | if re.search( ":\s-", response ): |
| 433 | main.log.info( self.name + ": Ping fail" ) |
| 434 | return main.FALSE |
| 435 | elif re.search( ":\s\d{1,2}\.\d\d", response ): |
| 436 | main.log.info( self.name + ": Ping good!" ) |
| 437 | return main.TRUE |
| 438 | main.log.info( self.name + ": Install fping on mininet machine... " ) |
| 439 | main.log.info( self.name + ": \n---\n" + response ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 440 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 441 | except Exception: |
| 442 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 443 | main.cleanup() |
| 444 | main.exit() |
| 445 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 446 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 447 | def pingallHosts( self, hostList, wait=1 ): |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 448 | """ |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 449 | Ping all specified IPv4 hosts |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 450 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 451 | Acceptable hostList: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 452 | - [ 'h1','h2','h3','h4' ] |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 453 | |
| 454 | Returns main.TRUE if all hosts specified can reach |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 455 | each other |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 456 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 457 | Returns main.FALSE if one or more of hosts specified |
| 458 | cannot reach each other""" |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 459 | wait = int( wait ) |
| 460 | cmd = " ping -c 1 -i 1 -W " + str( wait ) + " " |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 461 | |
| 462 | try: |
| 463 | main.log.info( "Testing reachability between specified hosts" ) |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 464 | |
andrew@onlab.us | defe38c | 2015-05-14 19:18:18 -0400 | [diff] [blame] | 465 | isReachable = main.TRUE |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 466 | pingResponse = "IPv4 ping across specified hosts\n" |
| 467 | failedPings = 0 |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 468 | for host in hostList: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 469 | listIndex = hostList.index( host ) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 470 | # List of hosts to ping other than itself |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 471 | pingList = hostList[ :listIndex ] + \ |
| 472 | hostList[ ( listIndex + 1 ): ] |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 473 | |
| 474 | pingResponse += str(str(host) + " -> ") |
| 475 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 476 | for temp in pingList: |
| 477 | # Current host pings all other hosts specified |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 478 | pingCmd = str( host ) + cmd + str( temp ) |
Jon Hall | 934576d | 2015-10-09 10:12:22 -0700 | [diff] [blame] | 479 | self.handle.sendline( pingCmd ) |
| 480 | self.handle.expect( "mininet>", timeout=wait + 1 ) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 481 | response = self.handle.before |
| 482 | if re.search( ',\s0\%\spacket\sloss', response ): |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 483 | pingResponse += str(" h" + str( temp[1:] )) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 484 | else: |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 485 | pingResponse += " X" |
andrew@onlab.us | defe38c | 2015-05-14 19:18:18 -0400 | [diff] [blame] | 486 | # One of the host to host pair is unreachable |
| 487 | isReachable = main.FALSE |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 488 | failedPings += 1 |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 489 | pingResponse += "\n" |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 490 | main.log.info( pingResponse + "Failed pings: " + str(failedPings) ) |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 491 | return isReachable |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 492 | except pexpect.TIMEOUT: |
| 493 | main.log.exception( self.name + ": TIMEOUT exception" ) |
Hari Krishna | 4223dbd | 2015-08-13 16:29:53 -0700 | [diff] [blame] | 494 | return main.FALSE |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 495 | except pexpect.EOF: |
| 496 | main.log.error( self.name + ": EOF exception found" ) |
| 497 | main.log.error( self.name + ": " + self.handle.before ) |
| 498 | main.cleanup() |
| 499 | main.exit() |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 500 | except Exception: |
| 501 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 502 | main.cleanup() |
| 503 | main.exit() |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 504 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 505 | def pingIpv6Hosts( self, hostList, prefix='1000::', wait=1 ): |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 506 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 507 | IPv6 ping all hosts in hostList. If no prefix passed this will use |
| 508 | default prefix of 1000:: |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 509 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 510 | Returns main.TRUE if all hosts specified can reach each other |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 511 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 512 | Returns main.FALSE if one or more of hosts specified cannot reach each other |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 513 | """ |
| 514 | try: |
| 515 | main.log.info( "Testing reachability between specified IPv6 hosts" ) |
| 516 | isReachable = main.TRUE |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 517 | wait = int( wait ) |
| 518 | cmd = " ping6 -c 1 -i 1 -W " + str( wait ) + " " |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 519 | pingResponse = "IPv6 Pingall output:\n" |
| 520 | failedPings = 0 |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 521 | for host in hostList: |
| 522 | listIndex = hostList.index( host ) |
| 523 | # List of hosts to ping other than itself |
| 524 | pingList = hostList[ :listIndex ] + \ |
| 525 | hostList[ ( listIndex + 1 ): ] |
| 526 | |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 527 | pingResponse += str(str(host) + " -> ") |
| 528 | |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 529 | for temp in pingList: |
| 530 | # Current host pings all other hosts specified |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 531 | pingCmd = str( host ) + cmd + str( self.getIPAddress(temp,proto='IPv6') ) |
Jon Hall | 934576d | 2015-10-09 10:12:22 -0700 | [diff] [blame] | 532 | self.handle.sendline( pingCmd ) |
| 533 | self.handle.expect( "mininet>", timeout=wait + 1 ) |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 534 | response = self.handle.before |
| 535 | if re.search( ',\s0\%\spacket\sloss', response ): |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 536 | pingResponse += str(" h" + str( temp[1:] )) |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 537 | else: |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 538 | pingResponse += " X" |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 539 | # One of the host to host pair is unreachable |
| 540 | isReachable = main.FALSE |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 541 | failedPings += 1 |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 542 | pingResponse += "\n" |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 543 | main.log.info( pingResponse + "Failed pings: " + str(failedPings) ) |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 544 | return isReachable |
| 545 | |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 546 | except pexpect.TIMEOUT: |
| 547 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 548 | return main.FALSE |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 549 | except pexpect.EOF: |
| 550 | main.log.error( self.name + ": EOF exception found" ) |
| 551 | main.log.error( self.name + ": " + self.handle.before ) |
| 552 | main.cleanup() |
| 553 | main.exit() |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 554 | except Exception: |
| 555 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 556 | main.cleanup() |
| 557 | main.exit() |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 558 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 559 | def pingHost( self, **pingParams ): |
| 560 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 561 | Ping from one mininet host to another |
| 562 | Currently the only supported Params: SRC, TARGET, and WAIT |
| 563 | """ |
| 564 | args = utilities.parse_args( [ "SRC", "TARGET", 'WAIT' ], **pingParams ) |
| 565 | wait = args['WAIT'] |
| 566 | wait = int( wait if wait else 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 567 | command = args[ "SRC" ] + " ping " + \ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 568 | args[ "TARGET" ] + " -c 1 -i 1 -W " + str( wait ) + " " |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 569 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 570 | main.log.info( "Sending: " + command ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 571 | self.handle.sendline( command ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 572 | i = self.handle.expect( [ command, pexpect.TIMEOUT ], |
| 573 | timeout=wait + 1 ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 574 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 575 | main.log.error( |
| 576 | self.name + |
| 577 | ": timeout when waiting for response from mininet" ) |
| 578 | main.log.error( "response: " + str( self.handle.before ) ) |
| 579 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 580 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 581 | main.log.error( |
| 582 | self.name + |
| 583 | ": timeout when waiting for response from mininet" ) |
| 584 | main.log.error( "response: " + str( self.handle.before ) ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 585 | response = self.handle.before |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 586 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 587 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
| 588 | return main.TRUE |
| 589 | else: |
| 590 | main.log.error( |
| 591 | self.name + |
| 592 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 593 | return main.FALSE |
| 594 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 595 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 596 | main.log.error( self.name + ": EOF exception found" ) |
| 597 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 598 | main.cleanup() |
| 599 | main.exit() |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 600 | except Exception: |
| 601 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 602 | main.cleanup() |
| 603 | main.exit() |
| 604 | |
| 605 | def ping6pair( self, **pingParams ): |
| 606 | """ |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 607 | IPv6 Ping between a pair of mininet hosts |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 608 | Currently the only supported Params are: SRC, TARGET, and WAIT |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 609 | FLOWLABEL and -I (src interface) will be added later after running some tests. |
| 610 | Example: main.Mininet1.ping6pair( src="h1", target="1000::2" ) |
| 611 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 612 | args = utilities.parse_args( [ "SRC", "TARGET", 'WAIT' ], **pingParams ) |
| 613 | wait = args['WAIT'] |
| 614 | wait = int( wait if wait else 1 ) |
Subhash Kumar Singh | bcc1c79 | 2015-11-07 04:52:11 +0530 | [diff] [blame] | 615 | command = args[ "SRC" ] + " ping6 " + \ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 616 | args[ "TARGET" ] + " -c 1 -i 1 -W " + str( wait ) + " " |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 617 | try: |
| 618 | main.log.info( "Sending: " + command ) |
| 619 | self.handle.sendline( command ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 620 | i = self.handle.expect( [ command, pexpect.TIMEOUT ], |
| 621 | timeout=wait + 1 ) |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 622 | if i == 1: |
| 623 | main.log.error( |
| 624 | self.name + |
| 625 | ": timeout when waiting for response from mininet" ) |
| 626 | main.log.error( "response: " + str( self.handle.before ) ) |
| 627 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
| 628 | if i == 1: |
| 629 | main.log.error( |
| 630 | self.name + |
| 631 | ": timeout when waiting for response from mininet" ) |
| 632 | main.log.error( "response: " + str( self.handle.before ) ) |
| 633 | response = self.handle.before |
| 634 | main.log.info( self.name + ": Ping Response: " + response ) |
| 635 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 636 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 637 | return main.TRUE |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 638 | else: |
| 639 | main.log.error( |
| 640 | self.name + |
| 641 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 642 | return main.FALSE |
| 643 | |
| 644 | except pexpect.EOF: |
| 645 | main.log.error( self.name + ": EOF exception found" ) |
| 646 | main.log.error( self.name + ": " + self.handle.before ) |
| 647 | main.cleanup() |
| 648 | main.exit() |
| 649 | except Exception: |
| 650 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 651 | main.cleanup() |
| 652 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 653 | |
You Wang | db927a5 | 2016-02-26 11:03:28 -0800 | [diff] [blame] | 654 | def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ): |
| 655 | """ |
| 656 | Description: |
| 657 | Ping a set of destination host from host CLI. |
| 658 | Logging into a Mininet host CLI is required before calling this funtion. |
| 659 | Params: |
| 660 | dstIPList is a list of destination ip addresses |
| 661 | Returns: |
| 662 | main.TRUE if the destination host is reachable |
| 663 | main.FALSE otherwise |
| 664 | """ |
| 665 | isReachable = main.TRUE |
| 666 | wait = int( wait ) |
| 667 | cmd = "ping" |
| 668 | if IPv6: |
| 669 | cmd = cmd + "6" |
| 670 | cmd = cmd + " -c 1 -i 1 -W " + str( wait ) |
| 671 | try: |
| 672 | for dstIP in dstIPList: |
| 673 | pingCmd = cmd + " " + dstIP |
| 674 | self.handle.sendline( pingCmd ) |
| 675 | i = self.handle.expect( [ self.hostPrompt, |
| 676 | '\*\*\* Unknown command: ' + pingCmd, |
| 677 | pexpect.TIMEOUT ], |
| 678 | timeout=wait + 1 ) |
| 679 | if i == 0: |
| 680 | response = self.handle.before |
| 681 | if not re.search( ',\s0\%\spacket\sloss', response ): |
| 682 | main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) ) |
| 683 | isReachable = main.FALSE |
| 684 | elif i == 1: |
| 685 | main.log.error( self.name + ": function should be called from host CLI instead of Mininet CLI" ) |
| 686 | main.cleanup() |
| 687 | main.exit() |
| 688 | elif i == 2: |
| 689 | main.log.error( self.name + ": timeout when waiting for response" ) |
| 690 | isReachable = main.FALSE |
| 691 | else: |
| 692 | main.log.error( self.name + ": unknown response: " + self.handle.before ) |
| 693 | isReachable = main.FALSE |
| 694 | except pexpect.TIMEOUT: |
| 695 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 696 | isReachable = main.FALSE |
| 697 | except pexpect.EOF: |
| 698 | main.log.error( self.name + ": EOF exception found" ) |
| 699 | main.log.error( self.name + ": " + self.handle.before ) |
| 700 | main.cleanup() |
| 701 | main.exit() |
| 702 | except Exception: |
| 703 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 704 | main.cleanup() |
| 705 | main.exit() |
| 706 | return isReachable |
| 707 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 708 | def checkIP( self, host ): |
| 709 | """ |
| 710 | Verifies the host's ip configured or not.""" |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 711 | try: |
| 712 | if self.handle: |
| 713 | try: |
| 714 | response = self.execute( |
| 715 | cmd=host + |
| 716 | " ifconfig", |
| 717 | prompt="mininet>", |
| 718 | timeout=10 ) |
| 719 | except pexpect.EOF: |
| 720 | main.log.error( self.name + ": EOF exception found" ) |
| 721 | main.log.error( self.name + ": " + self.handle.before ) |
| 722 | main.cleanup() |
| 723 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 724 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 725 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\ |
| 726 | "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\ |
| 727 | "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\ |
| 728 | "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\ |
| 729 | "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\ |
| 730 | "[0-9]|25[0-5]|[0-9]{1,2})" |
| 731 | # pattern = "inet addr:10.0.0.6" |
| 732 | if re.search( pattern, response ): |
| 733 | main.log.info( self.name + ": Host Ip configured properly" ) |
| 734 | return main.TRUE |
| 735 | else: |
| 736 | main.log.error( self.name + ": Host IP not found" ) |
| 737 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 738 | else: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 739 | main.log.error( self.name + ": Connection failed to the host" ) |
| 740 | except Exception: |
| 741 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 742 | main.cleanup() |
| 743 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 744 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 745 | def verifySSH( self, **connectargs ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 746 | # FIXME: Who uses this and what is the purpose? seems very specific |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 747 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 748 | response = self.execute( |
| 749 | cmd="h1 /usr/sbin/sshd -D&", |
| 750 | prompt="mininet>", |
| 751 | timeout=10 ) |
| 752 | response = self.execute( |
| 753 | cmd="h4 /usr/sbin/sshd -D&", |
| 754 | prompt="mininet>", |
| 755 | timeout=10 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 756 | for key in connectargs: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 757 | vars( self )[ key ] = connectargs[ key ] |
| 758 | response = self.execute( |
| 759 | cmd="xterm h1 h4 ", |
| 760 | prompt="mininet>", |
| 761 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 762 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 763 | main.log.error( self.name + ": EOF exception found" ) |
| 764 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 765 | main.cleanup() |
| 766 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 767 | import time |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 768 | time.sleep( 20 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 769 | if self.flag == 0: |
| 770 | self.flag = 1 |
| 771 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 772 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 773 | return main.TRUE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 774 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 775 | def moveHost( self, host, oldSw, newSw ): |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 776 | """ |
| 777 | Moves a host from one switch to another on the fly |
| 778 | Note: The intf between host and oldSw when detached |
| 779 | using detach(), will still show up in the 'net' |
| 780 | cmd, because switch.detach() doesn't affect switch.intfs[] |
| 781 | ( which is correct behavior since the interfaces |
| 782 | haven't moved ). |
| 783 | """ |
| 784 | if self.handle: |
| 785 | try: |
| 786 | # Bring link between oldSw-host down |
| 787 | cmd = "py net.configLinkStatus('" + oldSw + "'," + "'" + host +\ |
| 788 | "'," + "'down')" |
| 789 | print "cmd1= ", cmd |
| 790 | response = self.execute( cmd=cmd, |
| 791 | prompt="mininet>", |
| 792 | timeout=10 ) |
| 793 | |
| 794 | # Determine hostintf and Oldswitchintf |
| 795 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\ |
| 796 | ")[0]" |
| 797 | print "cmd2= ", cmd |
| 798 | self.handle.sendline( cmd ) |
| 799 | self.handle.expect( "mininet>" ) |
| 800 | |
| 801 | # Determine ip and mac address of the host-oldSw interface |
| 802 | cmd = "px ipaddr = hintf.IP()" |
| 803 | print "cmd3= ", cmd |
| 804 | self.handle.sendline( cmd ) |
| 805 | self.handle.expect( "mininet>" ) |
| 806 | |
| 807 | cmd = "px macaddr = hintf.MAC()" |
| 808 | print "cmd3= ", cmd |
| 809 | self.handle.sendline( cmd ) |
| 810 | self.handle.expect( "mininet>" ) |
| 811 | |
| 812 | # Detach interface between oldSw-host |
| 813 | cmd = "px " + oldSw + ".detach( sintf )" |
| 814 | print "cmd4= ", cmd |
| 815 | self.handle.sendline( cmd ) |
| 816 | self.handle.expect( "mininet>" ) |
| 817 | |
| 818 | # Add link between host-newSw |
| 819 | cmd = "py net.addLink(" + host + "," + newSw + ")" |
| 820 | print "cmd5= ", cmd |
| 821 | self.handle.sendline( cmd ) |
| 822 | self.handle.expect( "mininet>" ) |
| 823 | |
| 824 | # Determine hostintf and Newswitchintf |
| 825 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\ |
| 826 | ")[0]" |
| 827 | print "cmd6= ", cmd |
| 828 | self.handle.sendline( cmd ) |
| 829 | self.handle.expect( "mininet>" ) |
| 830 | |
| 831 | # Attach interface between newSw-host |
| 832 | cmd = "px " + newSw + ".attach( sintf )" |
| 833 | print "cmd3= ", cmd |
| 834 | self.handle.sendline( cmd ) |
| 835 | self.handle.expect( "mininet>" ) |
| 836 | |
| 837 | # Set ipaddress of the host-newSw interface |
| 838 | cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)" |
| 839 | print "cmd7 = ", cmd |
| 840 | self.handle.sendline( cmd ) |
| 841 | self.handle.expect( "mininet>" ) |
| 842 | |
| 843 | # Set macaddress of the host-newSw interface |
| 844 | cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)" |
| 845 | print "cmd8 = ", cmd |
| 846 | self.handle.sendline( cmd ) |
| 847 | self.handle.expect( "mininet>" ) |
| 848 | |
| 849 | cmd = "net" |
| 850 | print "cmd9 = ", cmd |
| 851 | self.handle.sendline( cmd ) |
| 852 | self.handle.expect( "mininet>" ) |
| 853 | print "output = ", self.handle.before |
| 854 | |
| 855 | # Determine ipaddress of the host-newSw interface |
| 856 | cmd = host + " ifconfig" |
| 857 | print "cmd10= ", cmd |
| 858 | self.handle.sendline( cmd ) |
| 859 | self.handle.expect( "mininet>" ) |
| 860 | print "ifconfig o/p = ", self.handle.before |
| 861 | |
| 862 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 863 | |
| 864 | except pexpect.TIMEOUT: |
| 865 | main.log.error(self.name + ": TIMEOUT exception found") |
| 866 | main.log.error(self.name + ": " + self.handle.before) |
| 867 | main.cleanup() |
| 868 | main.exit() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 869 | except pexpect.EOF: |
| 870 | main.log.error( self.name + ": EOF exception found" ) |
| 871 | main.log.error( self.name + ": " + self.handle.before ) |
| 872 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 873 | except Exception: |
| 874 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 875 | return main.FALSE |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 876 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 877 | def moveHostv6( self, host, oldSw, newSw ): |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 878 | """ |
| 879 | Moves a host from one switch to another on the fly |
| 880 | Note: The intf between host and oldSw when detached |
| 881 | using detach(), will still show up in the 'net' |
| 882 | cmd, because switch.detach() doesn't affect switch.intfs[] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 883 | ( which is correct behavior since the interfaces |
| 884 | haven't moved ). |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 885 | """ |
| 886 | if self.handle: |
| 887 | try: |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 888 | IP = str( self.getIPAddress( host, proto='IPV6' ) ) + "/64" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 889 | # Bring link between oldSw-host down |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 890 | cmd = "py net.configLinkStatus('" + oldSw + "'," + "'" + host +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 891 | "'," + "'down')" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 892 | print "cmd1= ", cmd |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 893 | response = self.execute( cmd=cmd, |
| 894 | prompt="mininet>", |
| 895 | timeout=10 ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 896 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 897 | # Determine hostintf and Oldswitchintf |
| 898 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 899 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 900 | print "cmd2= ", cmd |
| 901 | self.handle.sendline( cmd ) |
| 902 | self.handle.expect( "mininet>" ) |
| 903 | |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 904 | # Determine ip and mac address of the host-oldSw interface |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 905 | cmd = "px ipaddr = " + str(IP) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 906 | print "cmd3= ", cmd |
| 907 | self.handle.sendline( cmd ) |
| 908 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 909 | |
| 910 | cmd = "px macaddr = hintf.MAC()" |
| 911 | print "cmd3= ", cmd |
| 912 | self.handle.sendline( cmd ) |
| 913 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 914 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 915 | # Detach interface between oldSw-host |
| 916 | cmd = "px " + oldSw + ".detach( sintf )" |
| 917 | print "cmd4= ", cmd |
| 918 | self.handle.sendline( cmd ) |
| 919 | self.handle.expect( "mininet>" ) |
| 920 | |
| 921 | # Add link between host-newSw |
| 922 | cmd = "py net.addLink(" + host + "," + newSw + ")" |
| 923 | print "cmd5= ", cmd |
| 924 | self.handle.sendline( cmd ) |
| 925 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 926 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 927 | # Determine hostintf and Newswitchintf |
| 928 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 929 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 930 | print "cmd6= ", cmd |
| 931 | self.handle.sendline( cmd ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 932 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 933 | |
| 934 | # Attach interface between newSw-host |
| 935 | cmd = "px " + newSw + ".attach( sintf )" |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 936 | print "cmd6= ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 937 | self.handle.sendline( cmd ) |
| 938 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 939 | |
| 940 | # Set macaddress of the host-newSw interface |
| 941 | cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)" |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 942 | print "cmd7 = ", cmd |
| 943 | self.handle.sendline( cmd ) |
| 944 | self.handle.expect( "mininet>" ) |
| 945 | |
| 946 | # Set ipaddress of the host-newSw interface |
| 947 | cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)" |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 948 | print "cmd8 = ", cmd |
| 949 | self.handle.sendline( cmd ) |
| 950 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 951 | |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 952 | cmd = host + " ifconfig" |
| 953 | print "cmd9 =",cmd |
| 954 | response = self.execute( cmd = cmd, prompt="mininet>" ,timeout=10 ) |
| 955 | print response |
| 956 | pattern = "h\d-eth([\w])" |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 957 | ipAddressSearch = re.search( pattern, response ) |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 958 | print ipAddressSearch.group(1) |
| 959 | intf= host + "-eth" + str(ipAddressSearch.group(1)) |
| 960 | cmd = host + " ip -6 addr add %s dev %s" % ( IP, intf ) |
| 961 | print "cmd10 = ", cmd |
| 962 | self.handle.sendline( cmd ) |
| 963 | self.handle.expect( "mininet>" ) |
| 964 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 965 | cmd = "net" |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 966 | print "cmd11 = ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 967 | self.handle.sendline( cmd ) |
| 968 | self.handle.expect( "mininet>" ) |
| 969 | print "output = ", self.handle.before |
| 970 | |
| 971 | # Determine ipaddress of the host-newSw interface |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 972 | cmd = host + " ifconfig" |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 973 | print "cmd12= ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 974 | self.handle.sendline( cmd ) |
| 975 | self.handle.expect( "mininet>" ) |
| 976 | print "ifconfig o/p = ", self.handle.before |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 977 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 978 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 979 | except pexpect.TIMEOUT: |
| 980 | main.log.error(self.name + ": TIMEOUT exception found") |
| 981 | main.log.error(self.name + ": " + self.handle.before) |
| 982 | main.cleanup() |
| 983 | main.exit() |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 984 | except pexpect.EOF: |
| 985 | main.log.error( self.name + ": EOF exception found" ) |
| 986 | main.log.error( self.name + ": " + self.handle.before ) |
| 987 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 988 | except Exception: |
| 989 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 990 | return main.FALSE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 991 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 992 | def changeIP( self, host, intf, newIP, newNetmask ): |
| 993 | """ |
| 994 | Changes the ip address of a host on the fly |
| 995 | Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 996 | if self.handle: |
| 997 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 998 | cmd = host + " ifconfig " + intf + " " + \ |
| 999 | newIP + " " + 'netmask' + " " + newNetmask |
| 1000 | self.handle.sendline( cmd ) |
| 1001 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1002 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1003 | main.log.info( "response = " + response ) |
| 1004 | main.log.info( |
| 1005 | "Ip of host " + |
| 1006 | host + |
| 1007 | " changed to new IP " + |
| 1008 | newIP ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1009 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1010 | except pexpect.TIMEOUT: |
| 1011 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1012 | main.log.error(self.name + ": " + self.handle.before) |
| 1013 | main.cleanup() |
| 1014 | main.exit() |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1015 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1016 | main.log.error( self.name + ": EOF exception found" ) |
| 1017 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1018 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1019 | except Exception: |
| 1020 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1021 | main.cleanup() |
| 1022 | main.exit() |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1023 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1024 | def changeDefaultGateway( self, host, newGW ): |
| 1025 | """ |
| 1026 | Changes the default gateway of a host |
| 1027 | Ex: h1 route add default gw 10.0.1.2""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1028 | if self.handle: |
| 1029 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1030 | cmd = host + " route add default gw " + newGW |
| 1031 | self.handle.sendline( cmd ) |
| 1032 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1033 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1034 | main.log.info( "response = " + response ) |
| 1035 | main.log.info( |
| 1036 | "Default gateway of host " + |
| 1037 | host + |
| 1038 | " changed to " + |
| 1039 | newGW ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1040 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1041 | except pexpect.TIMEOUT: |
| 1042 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1043 | main.log.error(self.name + ": " + self.handle.before) |
| 1044 | main.cleanup() |
| 1045 | main.exit() |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1046 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1047 | main.log.error( self.name + ": EOF exception found" ) |
| 1048 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1049 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1050 | except Exception: |
| 1051 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1052 | main.cleanup() |
| 1053 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1054 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1055 | def addStaticMACAddress( self, host, GW, macaddr ): |
| 1056 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1057 | Changes the mac address of a gateway host""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1058 | if self.handle: |
| 1059 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1060 | # h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
| 1061 | cmd = host + " arp -s " + GW + " " + macaddr |
| 1062 | self.handle.sendline( cmd ) |
| 1063 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1064 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1065 | main.log.info( "response = " + response ) |
| 1066 | main.log.info( |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1067 | "Mac address of gateway " + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1068 | GW + |
| 1069 | " changed to " + |
| 1070 | macaddr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1071 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1072 | except pexpect.TIMEOUT: |
| 1073 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1074 | main.log.error(self.name + ": " + self.handle.before) |
| 1075 | main.cleanup() |
| 1076 | main.exit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1077 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1078 | main.log.error( self.name + ": EOF exception found" ) |
| 1079 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1080 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1081 | except Exception: |
| 1082 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1083 | main.cleanup() |
| 1084 | main.exit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1085 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1086 | def verifyStaticGWandMAC( self, host ): |
| 1087 | """ |
| 1088 | Verify if the static gateway and mac address assignment""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1089 | if self.handle: |
| 1090 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1091 | # h1 arp -an |
| 1092 | cmd = host + " arp -an " |
| 1093 | self.handle.sendline( cmd ) |
| 1094 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1095 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1096 | main.log.info( host + " arp -an = " + response ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1097 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1098 | except pexpect.TIMEOUT: |
| 1099 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1100 | main.log.error(self.name + ": " + self.handle.before) |
| 1101 | main.cleanup() |
| 1102 | main.exit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1103 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1104 | main.log.error( self.name + ": EOF exception found" ) |
| 1105 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1106 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1107 | except Exception: |
| 1108 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1109 | main.cleanup() |
| 1110 | main.exit() |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1111 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1112 | def getMacAddress( self, host ): |
| 1113 | """ |
| 1114 | Verifies the host's ip configured or not.""" |
| 1115 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1116 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1117 | response = self.execute( |
| 1118 | cmd=host + |
| 1119 | " ifconfig", |
| 1120 | prompt="mininet>", |
| 1121 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1122 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1123 | main.log.error( self.name + ": EOF exception found" ) |
| 1124 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1125 | main.cleanup() |
| 1126 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1127 | except Exception: |
| 1128 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1129 | main.cleanup() |
| 1130 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1131 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 1132 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1133 | macAddressSearch = re.search( pattern, response, re.I ) |
| 1134 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1135 | main.log.info( |
| 1136 | self.name + |
| 1137 | ": Mac-Address of Host " + |
| 1138 | host + |
| 1139 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1140 | macAddress ) |
| 1141 | return macAddress |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1142 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1143 | main.log.error( self.name + ": Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1144 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1145 | def getInterfaceMACAddress( self, host, interface ): |
| 1146 | """ |
| 1147 | Return the IP address of the interface on the given host""" |
| 1148 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1149 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1150 | response = self.execute( cmd=host + " ifconfig " + interface, |
| 1151 | prompt="mininet>", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1152 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1153 | main.log.error( self.name + ": EOF exception found" ) |
| 1154 | main.log.error( self.name + ": " + self.handle.before ) |
| 1155 | main.cleanup() |
| 1156 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1157 | except Exception: |
| 1158 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1159 | main.cleanup() |
| 1160 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1161 | |
| 1162 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1163 | macAddressSearch = re.search( pattern, response, re.I ) |
| 1164 | if macAddressSearch is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1165 | main.log.info( "No mac address found in %s" % response ) |
| 1166 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1167 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1168 | main.log.info( |
| 1169 | "Mac-Address of " + |
| 1170 | host + |
| 1171 | ":" + |
| 1172 | interface + |
| 1173 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1174 | macAddress ) |
| 1175 | return macAddress |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1176 | else: |
| 1177 | main.log.error( "Connection failed to the host" ) |
| 1178 | |
sathishm | ad95346 | 2015-12-03 17:42:07 +0530 | [diff] [blame] | 1179 | def getIPAddress( self, host , proto='IPV4'): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1180 | """ |
| 1181 | Verifies the host's ip configured or not.""" |
| 1182 | if self.handle: |
| 1183 | try: |
| 1184 | response = self.execute( |
| 1185 | cmd=host + |
| 1186 | " ifconfig", |
| 1187 | prompt="mininet>", |
| 1188 | timeout=10 ) |
| 1189 | except pexpect.EOF: |
| 1190 | main.log.error( self.name + ": EOF exception found" ) |
| 1191 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1192 | main.cleanup() |
| 1193 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1194 | except Exception: |
| 1195 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1196 | main.cleanup() |
| 1197 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1198 | |
sathishm | ad95346 | 2015-12-03 17:42:07 +0530 | [diff] [blame] | 1199 | pattern = '' |
| 1200 | if proto == 'IPV4': |
| 1201 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 1202 | else: |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 1203 | pattern = "inet6\saddr:\s([\w,:]*)/\d+\sScope:Global" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1204 | ipAddressSearch = re.search( pattern, response ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1205 | main.log.info( |
| 1206 | self.name + |
| 1207 | ": IP-Address of Host " + |
| 1208 | host + |
| 1209 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1210 | ipAddressSearch.group( 1 ) ) |
| 1211 | return ipAddressSearch.group( 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1212 | else: |
| 1213 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1214 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1215 | def getSwitchDPID( self, switch ): |
| 1216 | """ |
| 1217 | return the datapath ID of the switch""" |
| 1218 | if self.handle: |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1219 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1220 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1221 | response = self.execute( |
| 1222 | cmd=cmd, |
| 1223 | prompt="mininet>", |
| 1224 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1225 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1226 | main.log.error( self.name + ": EOF exception found" ) |
| 1227 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1228 | main.cleanup() |
| 1229 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1230 | except Exception: |
| 1231 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1232 | main.cleanup() |
| 1233 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 1234 | pattern = r'^(?P<dpid>\w)+' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1235 | result = re.search( pattern, response, re.MULTILINE ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1236 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1237 | main.log.info( |
| 1238 | "Couldn't find DPID for switch %s, found: %s" % |
| 1239 | ( switch, response ) ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1240 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1241 | return str( result.group( 0 ) ).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1242 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1243 | main.log.error( "Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1244 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1245 | def getDPID( self, switch ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 1246 | if self.handle: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1247 | self.handle.sendline( "" ) |
| 1248 | self.expect( "mininet>" ) |
| 1249 | cmd = "py %s.dpid" % switch |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 1250 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1251 | response = self.execute( |
| 1252 | cmd=cmd, |
| 1253 | prompt="mininet>", |
| 1254 | timeout=10 ) |
| 1255 | self.handle.expect( "mininet>" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 1256 | response = self.handle.before |
| 1257 | return response |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1258 | except pexpect.TIMEOUT: |
| 1259 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1260 | main.log.error(self.name + ": " + self.handle.before) |
| 1261 | main.cleanup() |
| 1262 | main.exit() |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 1263 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1264 | main.log.error( self.name + ": EOF exception found" ) |
| 1265 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 1266 | main.cleanup() |
| 1267 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1268 | except Exception: |
| 1269 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1270 | main.cleanup() |
| 1271 | main.exit() |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 1272 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1273 | def getInterfaces( self, node ): |
| 1274 | """ |
| 1275 | return information dict about interfaces connected to the node""" |
| 1276 | if self.handle: |
| 1277 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1278 | ' % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1279 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1280 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1281 | response = self.execute( |
| 1282 | cmd=cmd, |
| 1283 | prompt="mininet>", |
| 1284 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1285 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1286 | main.log.error( self.name + ": EOF exception found" ) |
| 1287 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1288 | main.cleanup() |
| 1289 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1290 | except Exception: |
| 1291 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1292 | main.cleanup() |
| 1293 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1294 | return response |
| 1295 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1296 | main.log.error( "Connection failed to the node" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1297 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1298 | def dump( self ): |
| 1299 | main.log.info( self.name + ": Dump node info" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1300 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1301 | response = self.execute( |
| 1302 | cmd='dump', |
| 1303 | prompt='mininet>', |
| 1304 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1305 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1306 | main.log.error( self.name + ": EOF exception found" ) |
| 1307 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1308 | main.cleanup() |
| 1309 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1310 | except Exception: |
| 1311 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1312 | main.cleanup() |
| 1313 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 1314 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1315 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1316 | def intfs( self ): |
| 1317 | main.log.info( self.name + ": List interfaces" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1318 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1319 | response = self.execute( |
| 1320 | cmd='intfs', |
| 1321 | prompt='mininet>', |
| 1322 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1323 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1324 | main.log.error( self.name + ": EOF exception found" ) |
| 1325 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1326 | main.cleanup() |
| 1327 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1328 | except Exception: |
| 1329 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1330 | main.cleanup() |
| 1331 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1332 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1333 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1334 | def net( self ): |
| 1335 | main.log.info( self.name + ": List network connections" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1336 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1337 | response = self.execute( cmd='net', prompt='mininet>', timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1338 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1339 | main.log.error( self.name + ": EOF exception found" ) |
| 1340 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1341 | main.cleanup() |
| 1342 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1343 | except Exception: |
| 1344 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1345 | main.cleanup() |
| 1346 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1347 | return response |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1348 | |
YPZhang | 81a7d4e | 2016-04-18 13:10:17 -0700 | [diff] [blame] | 1349 | def links( self, timeout=20 ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1350 | main.log.info( self.name + ": List network links" ) |
| 1351 | try: |
| 1352 | response = self.execute( cmd='links', prompt='mininet>', |
YPZhang | 81a7d4e | 2016-04-18 13:10:17 -0700 | [diff] [blame] | 1353 | timeout=timeout ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1354 | except pexpect.EOF: |
| 1355 | main.log.error( self.name + ": EOF exception found" ) |
| 1356 | main.log.error( self.name + ": " + self.handle.before ) |
| 1357 | main.cleanup() |
| 1358 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1359 | except Exception: |
| 1360 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1361 | main.cleanup() |
| 1362 | main.exit() |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1363 | return response |
| 1364 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1365 | def iperftcpAll(self, hosts, timeout=6): |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1366 | ''' |
| 1367 | Runs the iperftcp function with a given set of hosts and specified timeout. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1368 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1369 | @parm: |
| 1370 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 1371 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 1372 | ''' |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1373 | try: |
| 1374 | for host1 in hosts: |
| 1375 | for host2 in hosts: |
| 1376 | if host1 != host2: |
| 1377 | if self.iperftcp(host1, host2, timeout) == main.FALSE: |
| 1378 | main.log.error(self.name + ": iperftcp test failed for " + host1 + " and " + host2) |
| 1379 | except Exception: |
| 1380 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1381 | main.cleanup() |
| 1382 | main.exit() |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1383 | |
| 1384 | def iperftcp(self, host1="h1", host2="h2", timeout=6): |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1385 | ''' |
| 1386 | Creates an iperf TCP test between two hosts. Returns main.TRUE if test results |
| 1387 | are valid. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1388 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1389 | @parm: |
| 1390 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 1391 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 1392 | ''' |
| 1393 | main.log.info( self.name + ": Simple iperf TCP test between two hosts" ) |
| 1394 | try: |
| 1395 | # Setup the mininet command |
| 1396 | cmd1 = 'iperf ' + host1 + " " + host2 |
| 1397 | self.handle.sendline( cmd1 ) |
| 1398 | outcome = self.handle.expect( "mininet>", timeout ) |
| 1399 | response = self.handle.before |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1400 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1401 | # checks if there are results in the mininet response |
| 1402 | if "Results:" in response: |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 1403 | main.log.report(self.name + ": iperf test completed") |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1404 | # parse the mn results |
| 1405 | response = response.split("\r\n") |
| 1406 | response = response[len(response)-2] |
| 1407 | response = response.split(": ") |
| 1408 | response = response[len(response)-1] |
| 1409 | response = response.replace("[", "") |
| 1410 | response = response.replace("]", "") |
| 1411 | response = response.replace("\'", "") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1412 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1413 | # this is the bandwith two and from the two hosts |
| 1414 | bandwidth = response.split(", ") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1415 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1416 | # there should be two elements in the bandwidth list |
| 1417 | # ['host1 to host2', 'host2 to host1"] |
| 1418 | if len(bandwidth) == 2: |
| 1419 | main.log.report(self.name + ": iperf test successful") |
| 1420 | return main.TRUE |
| 1421 | else: |
| 1422 | main.log.error(self.name + ": invalid iperf results") |
| 1423 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1424 | else: |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1425 | main.log.error( self.name + ": iperf test failed" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1426 | return main.FALSE |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1427 | except pexpect.TIMEOUT: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1428 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1429 | main.log.error( self.name + " response: " + |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 1430 | repr( self.handle.before ) ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1431 | # NOTE: Send ctrl-c to make sure iperf is done |
| 1432 | self.handle.sendline( "\x03" ) |
| 1433 | self.handle.expect( "Interrupt" ) |
| 1434 | self.handle.expect( "mininet>" ) |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1435 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1436 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1437 | main.log.error( self.name + ": EOF exception found" ) |
| 1438 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1439 | main.cleanup() |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1440 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1441 | except Exception: |
| 1442 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1443 | main.cleanup() |
| 1444 | main.exit() |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1445 | |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 1446 | def iperftcpipv6(self, host1="h1", host2="h2", timeout=50): |
| 1447 | main.log.info( self.name + ": Simple iperf TCP test between two hosts" ) |
| 1448 | try: |
| 1449 | IP1 = self.getIPAddress( host1, proto='IPV6' ) |
| 1450 | cmd1 = host1 +' iperf -V -sD -B '+ str(IP1) |
| 1451 | self.handle.sendline( cmd1 ) |
| 1452 | outcome1 = self.handle.expect( "mininet>") |
| 1453 | cmd2 = host2 +' iperf -V -c '+ str(IP1) +' -t 5' |
| 1454 | self.handle.sendline( cmd2 ) |
| 1455 | outcome2 = self.handle.expect( "mininet>") |
| 1456 | response1 = self.handle.before |
| 1457 | response2 = self.handle.after |
| 1458 | print response1,response2 |
| 1459 | pattern = "connected with "+ str(IP1) |
| 1460 | if pattern in response1: |
| 1461 | main.log.report(self.name + ": iperf test completed") |
| 1462 | return main.TRUE |
| 1463 | else: |
| 1464 | main.log.error( self.name + ": iperf test failed" ) |
| 1465 | return main.FALSE |
| 1466 | except pexpect.TIMEOUT: |
| 1467 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1468 | main.log.error( self.name + " response: " + repr( self.handle.before ) ) |
| 1469 | self.handle.sendline( "\x03" ) |
| 1470 | self.handle.expect( "Interrupt" ) |
| 1471 | self.handle.expect( "mininet>" ) |
| 1472 | return main.FALSE |
| 1473 | except pexpect.EOF: |
| 1474 | main.log.error( self.name + ": EOF exception found" ) |
| 1475 | main.log.error( self.name + ": " + self.handle.before ) |
| 1476 | main.cleanup() |
| 1477 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1478 | except Exception: |
| 1479 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1480 | main.cleanup() |
| 1481 | main.exit() |
Jon Hall | 439c891 | 2016-04-15 02:22:03 -0700 | [diff] [blame] | 1482 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1483 | def iperfudpAll(self, hosts, bandwidth="10M"): |
| 1484 | ''' |
| 1485 | Runs the iperfudp function with a given set of hosts and specified |
| 1486 | bandwidth |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1487 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1488 | @param: |
| 1489 | bandwidth: the targeted bandwidth, in megabits ('M') |
| 1490 | ''' |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1491 | try: |
| 1492 | for host1 in hosts: |
| 1493 | for host2 in hosts: |
| 1494 | if host1 != host2: |
| 1495 | if self.iperfudp(host1, host2, bandwidth) == main.FALSE: |
| 1496 | main.log.error(self.name + ": iperfudp test failed for " + host1 + " and " + host2) |
| 1497 | except TypeError: |
| 1498 | main.log.exception(self.name + ": Object not as expected") |
| 1499 | return main.FALSE |
| 1500 | except Exception: |
| 1501 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1502 | main.cleanup() |
| 1503 | main.exit() |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1504 | |
| 1505 | def iperfudp( self, bandwidth="10M", host1="h1", host2="h2"): |
| 1506 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1507 | ''' |
| 1508 | Creates an iperf UDP test with a specific bandwidth. |
| 1509 | Returns true if results are valid. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1510 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1511 | @param: |
| 1512 | bandwidth: the targeted bandwidth, in megabits ('M'), to run the test |
| 1513 | ''' |
| 1514 | main.log.info(self.name + ": Simple iperf UDP test between two hosts") |
| 1515 | try: |
| 1516 | # setup the mininet command |
| 1517 | cmd = 'iperfudp ' + bandwidth + " " + host1 + " " + host2 |
| 1518 | self.handle.sendline(cmd) |
| 1519 | self.handle.expect("mininet>") |
| 1520 | response = self.handle.before |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1521 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1522 | # check if there are in results in the mininet response |
| 1523 | if "Results:" in response: |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 1524 | main.log.report(self.name + ": iperfudp test completed") |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1525 | # parse the results |
| 1526 | response = response.split("\r\n") |
| 1527 | response = response[len(response)-2] |
| 1528 | response = response.split(": ") |
| 1529 | response = response[len(response)-1] |
| 1530 | response = response.replace("[", "") |
| 1531 | response = response.replace("]", "") |
| 1532 | response = response.replace("\'", "") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1533 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1534 | mnBandwidth = response.split(", ") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1535 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1536 | # check to see if there are at least three entries |
| 1537 | # ['bandwidth', 'host1 to host2', 'host2 to host1'] |
| 1538 | if len(mnBandwidth) == 3: |
| 1539 | # if one entry is blank then something is wrong |
| 1540 | for item in mnBandwidth: |
| 1541 | if item == "": |
| 1542 | main.log.error(self.name + ": Could not parse iperf output") |
| 1543 | main.log.error(self.name + ": invalid iperfudp results") |
| 1544 | return main.FALSE |
| 1545 | # otherwise results are vaild |
| 1546 | main.log.report(self.name + ": iperfudp test successful") |
| 1547 | return main.TRUE |
| 1548 | else: |
| 1549 | main.log.error(self.name + ": invalid iperfudp results") |
| 1550 | return main.FALSE |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1551 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1552 | except pexpect.TIMEOUT: |
| 1553 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1554 | main.log.error(self.name + ": " + self.handle.before) |
| 1555 | main.cleanup() |
| 1556 | main.exit() |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1557 | except pexpect.EOF: |
| 1558 | main.log.error( self.name + ": EOF exception found" ) |
| 1559 | main.log.error( self.name + ": " + self.handle.before ) |
| 1560 | main.cleanup() |
| 1561 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1562 | except Exception: |
| 1563 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1564 | main.cleanup() |
| 1565 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1566 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1567 | def nodes( self ): |
| 1568 | main.log.info( self.name + ": List all nodes." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1569 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1570 | response = self.execute( |
| 1571 | cmd='nodes', |
| 1572 | prompt='mininet>', |
| 1573 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1574 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1575 | main.log.error( self.name + ": EOF exception found" ) |
| 1576 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1577 | main.cleanup() |
| 1578 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1579 | except Exception: |
| 1580 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1581 | main.cleanup() |
| 1582 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1583 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1584 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1585 | def pingpair( self ): |
| 1586 | main.log.info( self.name + ": Ping between first two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1587 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1588 | response = self.execute( |
| 1589 | cmd='pingpair', |
| 1590 | prompt='mininet>', |
| 1591 | timeout=20 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1592 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1593 | main.log.error( self.name + ": EOF exception found" ) |
| 1594 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1595 | main.cleanup() |
| 1596 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1597 | except Exception: |
| 1598 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1599 | main.cleanup() |
| 1600 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1601 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1602 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 1603 | main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1604 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1605 | else: |
| 1606 | main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1607 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1608 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1609 | def link( self, **linkargs ): |
| 1610 | """ |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1611 | Bring link( s ) between two nodes up or down |
| 1612 | """ |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1613 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1614 | args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs ) |
| 1615 | end1 = args[ "END1" ] if args[ "END1" ] is not None else "" |
| 1616 | end2 = args[ "END2" ] if args[ "END2" ] is not None else "" |
| 1617 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1618 | |
| 1619 | main.log.info( "Bring link between " + str( end1 ) + " and " + str( end2 ) + " " + str( option ) ) |
| 1620 | cmd = "link {} {} {}".format( end1, end2, option ) |
| 1621 | self.handle.sendline( cmd ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1622 | self.handle.expect( "mininet>" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1623 | response = self.handle.before |
| 1624 | main.log.info( response ) |
| 1625 | |
| 1626 | return main.TRUE |
| 1627 | except pexpect.TIMEOUT: |
| 1628 | main.log.exception( self.name + ": Command timed out" ) |
| 1629 | return None |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1630 | except pexpect.EOF: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1631 | main.log.exception( self.name + ": connection closed." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1632 | main.cleanup() |
| 1633 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1634 | except Exception: |
| 1635 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1636 | main.cleanup() |
| 1637 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1638 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1639 | def switch( self, **switchargs ): |
| 1640 | """ |
| 1641 | start/stop a switch |
| 1642 | """ |
| 1643 | args = utilities.parse_args( [ "SW", "OPTION" ], **switchargs ) |
| 1644 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1645 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1646 | command = "switch " + str( sw ) + " " + str( option ) |
| 1647 | main.log.info( command ) |
| 1648 | try: |
| 1649 | self.handle.sendline( command ) |
| 1650 | self.handle.expect( "mininet>" ) |
| 1651 | except pexpect.TIMEOUT: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1652 | main.log.error(self.name + ": TIMEOUT exception found") |
| 1653 | main.log.error(self.name + ": " + self.handle.before) |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1654 | main.cleanup() |
| 1655 | main.exit() |
| 1656 | except pexpect.EOF: |
| 1657 | main.log.error( self.name + ": EOF exception found" ) |
| 1658 | main.log.error( self.name + ": " + self.handle.before ) |
| 1659 | main.cleanup() |
| 1660 | main.exit() |
| 1661 | return main.TRUE |
| 1662 | |
pingping-lin | 5bb663b | 2015-09-24 11:47:50 -0700 | [diff] [blame] | 1663 | def node( self, nodeName, commandStr ): |
| 1664 | """ |
| 1665 | Carry out a command line on a given node |
| 1666 | @parm: |
| 1667 | nodeName: the node name in Mininet testbed |
| 1668 | commandStr: the command line will be carried out on the node |
| 1669 | Example: main.Mininet.node( nodeName="h1", commandStr="ls" ) |
| 1670 | """ |
| 1671 | command = str( nodeName ) + " " + str( commandStr ) |
| 1672 | main.log.info( command ) |
| 1673 | |
| 1674 | try: |
| 1675 | response = self.execute( cmd = command, prompt = "mininet>" ) |
| 1676 | if re.search( "Unknown command", response ): |
| 1677 | main.log.warn( response ) |
| 1678 | return main.FALSE |
Jon Hall | 9ed8f37 | 2016-02-24 17:34:07 -0800 | [diff] [blame] | 1679 | if re.search( "Permission denied", response ): |
| 1680 | main.log.warn( response ) |
| 1681 | return main.FALSE |
pingping-lin | 5bb663b | 2015-09-24 11:47:50 -0700 | [diff] [blame] | 1682 | except pexpect.EOF: |
| 1683 | main.log.error( self.name + ": EOF exception found" ) |
| 1684 | main.log.error( self.name + ": " + self.handle.before ) |
| 1685 | main.cleanup() |
| 1686 | main.exit() |
| 1687 | main.log.info( " response is :" ) |
| 1688 | main.log.info( response ) |
| 1689 | return response |
| 1690 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1691 | def yank( self, **yankargs ): |
| 1692 | """ |
| 1693 | yank a mininet switch interface to a host""" |
| 1694 | main.log.info( 'Yank the switch interface attached to a host' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1695 | args = utilities.parse_args( [ "SW", "INTF" ], **yankargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1696 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1697 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1698 | command = "py " + str( sw ) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1699 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1700 | response = self.execute( |
| 1701 | cmd=command, |
| 1702 | prompt="mininet>", |
| 1703 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1704 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1705 | main.log.error( self.name + ": EOF exception found" ) |
| 1706 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1707 | main.cleanup() |
| 1708 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1709 | except Exception: |
| 1710 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1711 | main.cleanup() |
| 1712 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 1713 | return main.TRUE |
| 1714 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1715 | def plug( self, **plugargs ): |
| 1716 | """ |
| 1717 | plug the yanked mininet switch interface to a switch""" |
| 1718 | main.log.info( 'Plug the switch interface attached to a switch' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1719 | args = utilities.parse_args( [ "SW", "INTF" ], **plugargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1720 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1721 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1722 | command = "py " + str( sw ) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1723 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1724 | response = self.execute( |
| 1725 | cmd=command, |
| 1726 | prompt="mininet>", |
| 1727 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1728 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1729 | main.log.error( self.name + ": EOF exception found" ) |
| 1730 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1731 | main.cleanup() |
| 1732 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1733 | except Exception: |
| 1734 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1735 | main.cleanup() |
| 1736 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1737 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1738 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1739 | def dpctl( self, **dpctlargs ): |
| 1740 | """ |
| 1741 | Run dpctl command on all switches.""" |
| 1742 | main.log.info( 'Run dpctl command on all switches' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1743 | args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1744 | cmd = args[ "CMD" ] if args[ "CMD" ] is not None else "" |
| 1745 | cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else "" |
| 1746 | command = "dpctl " + cmd + " " + str( cmdargs ) |
| 1747 | try: |
| 1748 | response = self.execute( |
| 1749 | cmd=command, |
| 1750 | prompt="mininet>", |
| 1751 | timeout=10 ) |
| 1752 | except pexpect.EOF: |
| 1753 | main.log.error( self.name + ": EOF exception found" ) |
| 1754 | main.log.error( self.name + ": " + self.handle.before ) |
| 1755 | main.cleanup() |
| 1756 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1757 | except Exception: |
| 1758 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1759 | main.cleanup() |
| 1760 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1761 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1762 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1763 | def getVersion( self ): |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1764 | # FIXME: What uses this? This should be refactored to get |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1765 | # version from MN and not some other file |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1766 | try: |
| 1767 | fileInput = path + '/lib/Mininet/INSTALL' |
| 1768 | version = super( Mininet, self ).getVersion() |
| 1769 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
| 1770 | for line in open( fileInput, 'r' ).readlines(): |
| 1771 | result = re.match( pattern, line ) |
| 1772 | if result: |
| 1773 | version = result.group( 0 ) |
| 1774 | return version |
| 1775 | except Exception: |
| 1776 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1777 | main.cleanup() |
| 1778 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1779 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1780 | def getSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1781 | """ |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1782 | Parameters: |
| 1783 | sw: The name of an OVS switch. Example "s1" |
| 1784 | Return: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1785 | The output of the command from the mininet cli |
| 1786 | or main.FALSE on timeout""" |
| 1787 | command = "sh ovs-vsctl get-controller " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1788 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1789 | response = self.execute( |
| 1790 | cmd=command, |
| 1791 | prompt="mininet>", |
| 1792 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1793 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1794 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1795 | else: |
| 1796 | return main.FALSE |
| 1797 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1798 | main.log.error( self.name + ": EOF exception found" ) |
| 1799 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1800 | main.cleanup() |
| 1801 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1802 | except Exception: |
| 1803 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1804 | main.cleanup() |
| 1805 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1806 | |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1807 | def assignSwController( self, sw, ip, port="6653", ptcp="" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1808 | """ |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1809 | Description: |
| 1810 | Assign switches to the controllers ( for ovs use only ) |
| 1811 | Required: |
| 1812 | sw - Name of the switch. This can be a list or a string. |
| 1813 | ip - Ip addresses of controllers. This can be a list or a string. |
| 1814 | Optional: |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1815 | port - ONOS use port 6653, if no list of ports is passed, then |
| 1816 | the all the controller will use 6653 as their port number |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1817 | ptcp - ptcp number, This can be a string or a list that has |
| 1818 | the same length as switch. This is optional and not required |
| 1819 | when using ovs switches. |
| 1820 | NOTE: If switches and ptcp are given in a list type they should have the |
| 1821 | same length and should be in the same order, Eg. sw=[ 's1' ... n ] |
| 1822 | ptcp=[ '6637' ... n ], s1 has ptcp number 6637 and so on. |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1823 | |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1824 | Return: |
| 1825 | Returns main.TRUE if mininet correctly assigned switches to |
| 1826 | controllers, otherwise it will return main.FALSE or an appropriate |
| 1827 | exception(s) |
| 1828 | """ |
| 1829 | assignResult = main.TRUE |
| 1830 | # Initial ovs command |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1831 | commandList = [] |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1832 | command = "sh ovs-vsctl set-controller " |
| 1833 | onosIp = "" |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1834 | try: |
| 1835 | if isinstance( ip, types.StringType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1836 | onosIp = "tcp:" + str( ip ) + ":" |
kelvin-onlab | 5c2df43 | 2015-06-11 17:29:56 -0700 | [diff] [blame] | 1837 | if isinstance( port, types.StringType ) or \ |
| 1838 | isinstance( port, types.IntType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1839 | onosIp += str( port ) |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1840 | elif isinstance( port, types.ListType ): |
| 1841 | main.log.error( self.name + ": Only one controller " + |
| 1842 | "assigned and a list of ports has" + |
| 1843 | " been passed" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1844 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1845 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1846 | main.log.error( self.name + ": Invalid controller port " + |
| 1847 | "number. Please specify correct " + |
| 1848 | "controller port" ) |
| 1849 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1850 | |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1851 | elif isinstance( ip, types.ListType ): |
kelvin-onlab | 5c2df43 | 2015-06-11 17:29:56 -0700 | [diff] [blame] | 1852 | if isinstance( port, types.StringType ) or \ |
| 1853 | isinstance( port, types.IntType ): |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1854 | for ipAddress in ip: |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1855 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 1856 | str( port ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1857 | elif isinstance( port, types.ListType ): |
| 1858 | if ( len( ip ) != len( port ) ): |
| 1859 | main.log.error( self.name + ": Port list = " + |
| 1860 | str( len( port ) ) + |
| 1861 | "should be the same as controller" + |
| 1862 | " ip list = " + str( len( ip ) ) ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1863 | return main.FALSE |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1864 | else: |
| 1865 | onosIp = "" |
| 1866 | for ipAddress, portNum in zip( ip, port ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1867 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 1868 | str( portNum ) + " " |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1869 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1870 | main.log.error( self.name + ": Invalid controller port " + |
| 1871 | "number. Please specify correct " + |
| 1872 | "controller port" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1873 | return main.FALSE |
kelvin-onlab | e5edb9e | 2015-06-12 09:38:47 -0700 | [diff] [blame] | 1874 | else: |
| 1875 | main.log.error( self.name + ": Invalid ip address" ) |
| 1876 | return main.FALSE |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1877 | |
| 1878 | if isinstance( sw, types.StringType ): |
| 1879 | command += sw + " " |
| 1880 | if ptcp: |
| 1881 | if isinstance( ptcp, types.StringType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1882 | command += "ptcp:" + str( ptcp ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1883 | elif isinstance( ptcp, types.ListType ): |
| 1884 | main.log.error( self.name + ": Only one switch is " + |
| 1885 | "being set and multiple PTCP is " + |
| 1886 | "being passed " ) |
| 1887 | else: |
| 1888 | main.log.error( self.name + ": Invalid PTCP" ) |
| 1889 | ptcp = "" |
| 1890 | command += onosIp |
| 1891 | commandList.append( command ) |
| 1892 | |
| 1893 | elif isinstance( sw, types.ListType ): |
| 1894 | if ptcp: |
| 1895 | if isinstance( ptcp, types.ListType ): |
| 1896 | if len( ptcp ) != len( sw ): |
| 1897 | main.log.error( self.name + ": PTCP length = " + |
| 1898 | str( len( ptcp ) ) + |
| 1899 | " is not the same as switch" + |
| 1900 | " length = " + |
| 1901 | str( len( sw ) ) ) |
| 1902 | return main.FALSE |
| 1903 | else: |
| 1904 | for switch, ptcpNum in zip( sw, ptcp ): |
| 1905 | tempCmd = "sh ovs-vsctl set-controller " |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1906 | tempCmd += switch + " ptcp:" + \ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1907 | str( ptcpNum ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1908 | tempCmd += onosIp |
| 1909 | commandList.append( tempCmd ) |
| 1910 | else: |
| 1911 | main.log.error( self.name + ": Invalid PTCP" ) |
| 1912 | return main.FALSE |
| 1913 | else: |
| 1914 | for switch in sw: |
| 1915 | tempCmd = "sh ovs-vsctl set-controller " |
| 1916 | tempCmd += switch + " " + onosIp |
| 1917 | commandList.append( tempCmd ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1918 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1919 | main.log.error( self.name + ": Invalid switch type " ) |
| 1920 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1921 | |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1922 | for cmd in commandList: |
| 1923 | try: |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1924 | self.execute( cmd=cmd, prompt="mininet>", timeout=5 ) |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1925 | except pexpect.TIMEOUT: |
| 1926 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1927 | return main.FALSE |
| 1928 | except pexpect.EOF: |
| 1929 | main.log.error( self.name + ": EOF exception found" ) |
| 1930 | main.log.error( self.name + ": " + self.handle.before ) |
| 1931 | main.cleanup() |
| 1932 | main.exit() |
| 1933 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1934 | except pexpect.EOF: |
| 1935 | main.log.error( self.name + ": EOF exception found" ) |
| 1936 | main.log.error( self.name + ": " + self.handle.before ) |
| 1937 | main.cleanup() |
| 1938 | main.exit() |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1939 | except Exception: |
| 1940 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1941 | main.cleanup() |
| 1942 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1943 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1944 | def deleteSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1945 | """ |
| 1946 | Removes the controller target from sw""" |
| 1947 | command = "sh ovs-vsctl del-controller " + str( sw ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1948 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1949 | response = self.execute( |
| 1950 | cmd=command, |
| 1951 | prompt="mininet>", |
| 1952 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1953 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1954 | main.log.error( self.name + ": EOF exception found" ) |
| 1955 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1956 | main.cleanup() |
| 1957 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 1958 | except Exception: |
| 1959 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1960 | main.cleanup() |
| 1961 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1962 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1963 | main.log.info( response ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1964 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1965 | def addSwitch( self, sw, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1966 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1967 | adds a switch to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1968 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1969 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1970 | NOTE: cannot currently specify what type of switch |
| 1971 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1972 | sw = name of the new switch as a string |
| 1973 | optional keywords: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1974 | dpid = "dpid" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1975 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1976 | """ |
| 1977 | dpid = kwargs.get( 'dpid', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1978 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1979 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1980 | response = self.execute( |
| 1981 | cmd=command, |
| 1982 | prompt="mininet>", |
| 1983 | timeout=10 ) |
| 1984 | if re.search( "already exists!", response ): |
| 1985 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1986 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1987 | elif re.search( "Error", response ): |
| 1988 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1989 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1990 | elif re.search( "usage:", response ): |
| 1991 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1992 | return main.FALSE |
| 1993 | else: |
| 1994 | return main.TRUE |
| 1995 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1996 | main.log.error( self.name + ": EOF exception found" ) |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1997 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1998 | main.cleanup() |
| 1999 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2000 | except Exception: |
| 2001 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2002 | main.cleanup() |
| 2003 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2004 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2005 | def delSwitch( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2006 | """ |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 2007 | delete a switch from the mininet topology |
| 2008 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 2009 | dynamic_topo branch |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 2010 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2011 | sw = name of the switch as a string |
| 2012 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2013 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2014 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2015 | response = self.execute( |
| 2016 | cmd=command, |
| 2017 | prompt="mininet>", |
| 2018 | timeout=10 ) |
| 2019 | if re.search( "no switch named", response ): |
| 2020 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2021 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2022 | elif re.search( "Error", response ): |
| 2023 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2024 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2025 | elif re.search( "usage:", response ): |
| 2026 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2027 | return main.FALSE |
| 2028 | else: |
| 2029 | return main.TRUE |
| 2030 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2031 | main.log.error( self.name + ": EOF exception found" ) |
| 2032 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2033 | main.cleanup() |
| 2034 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2035 | except Exception: |
| 2036 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2037 | main.cleanup() |
| 2038 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2039 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 2040 | def getSwitchRandom( self, timeout=60, nonCut=True ): |
| 2041 | """ |
| 2042 | Randomly get a switch from Mininet topology. |
| 2043 | If nonCut is True, it gets a list of non-cut switches (the deletion |
| 2044 | of a non-cut switch will not increase the number of connected |
| 2045 | components of a graph) and randomly returns one of them, otherwise |
| 2046 | it just randomly returns one switch from all current switches in |
| 2047 | Mininet. |
| 2048 | Returns the name of the chosen switch. |
| 2049 | """ |
| 2050 | import random |
| 2051 | candidateSwitches = [] |
| 2052 | try: |
| 2053 | if not nonCut: |
| 2054 | switches = self.getSwitches( timeout=timeout ) |
| 2055 | assert len( switches ) != 0 |
| 2056 | for switchName in switches.keys(): |
| 2057 | candidateSwitches.append( switchName ) |
| 2058 | else: |
| 2059 | graphDict = self.getGraphDict( timeout=timeout, useId=False ) |
| 2060 | if graphDict == None: |
| 2061 | return None |
| 2062 | self.graph.update( graphDict ) |
| 2063 | candidateSwitches = self.graph.getNonCutVertices() |
| 2064 | if candidateSwitches == None: |
| 2065 | return None |
| 2066 | elif len( candidateSwitches ) == 0: |
| 2067 | main.log.info( self.name + ": No candidate switch for deletion" ) |
| 2068 | return None |
| 2069 | else: |
| 2070 | switch = random.sample( candidateSwitches, 1 ) |
| 2071 | return switch[ 0 ] |
| 2072 | except KeyError: |
| 2073 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2074 | return None |
| 2075 | except AssertionError: |
| 2076 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 2077 | return None |
| 2078 | except Exception: |
| 2079 | main.log.exception( self.name + ": Uncaught exception" ) |
| 2080 | return None |
| 2081 | |
| 2082 | def delSwitchRandom( self, timeout=60, nonCut=True ): |
| 2083 | """ |
| 2084 | Randomly delete a switch from Mininet topology. |
| 2085 | If nonCut is True, it gets a list of non-cut switches (the deletion |
| 2086 | of a non-cut switch will not increase the number of connected |
| 2087 | components of a graph) and randomly chooses one for deletion, |
| 2088 | otherwise it just randomly delete one switch from all current |
| 2089 | switches in Mininet. |
| 2090 | Returns the name of the deleted switch |
| 2091 | """ |
| 2092 | try: |
| 2093 | switch = self.getSwitchRandom( timeout, nonCut ) |
| 2094 | if switch == None: |
| 2095 | return None |
| 2096 | else: |
| 2097 | deletionResult = self.delSwitch( switch ) |
| 2098 | if deletionResult: |
| 2099 | return switch |
| 2100 | else: |
| 2101 | return None |
| 2102 | except Exception: |
| 2103 | main.log.exception( self.name + ": Uncaught exception" ) |
| 2104 | return None |
| 2105 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2106 | def addLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2107 | """ |
| 2108 | add a link to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 2109 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 2110 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2111 | NOTE: cannot currently specify what type of link |
| 2112 | required params: |
| 2113 | node1 = the string node name of the first endpoint of the link |
| 2114 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2115 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2116 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2117 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2118 | response = self.execute( |
| 2119 | cmd=command, |
| 2120 | prompt="mininet>", |
| 2121 | timeout=10 ) |
| 2122 | if re.search( "doesnt exist!", response ): |
| 2123 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2124 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2125 | elif re.search( "Error", response ): |
| 2126 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2127 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2128 | elif re.search( "usage:", response ): |
| 2129 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2130 | return main.FALSE |
| 2131 | else: |
| 2132 | return main.TRUE |
| 2133 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2134 | main.log.error( self.name + ": EOF exception found" ) |
| 2135 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2136 | main.cleanup() |
| 2137 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2138 | except Exception: |
| 2139 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2140 | main.cleanup() |
| 2141 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2142 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2143 | def delLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2144 | """ |
| 2145 | delete a link from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 2146 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 2147 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2148 | required params: |
| 2149 | node1 = the string node name of the first endpoint of the link |
| 2150 | node2 = the string node name of the second endpoint of the link |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2151 | returns: main.FALSE on an error, else main.TRUE |
| 2152 | """ |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2153 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2154 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2155 | response = self.execute( |
| 2156 | cmd=command, |
| 2157 | prompt="mininet>", |
| 2158 | timeout=10 ) |
| 2159 | if re.search( "no node named", response ): |
| 2160 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2161 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2162 | elif re.search( "Error", response ): |
| 2163 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2164 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2165 | elif re.search( "usage:", response ): |
| 2166 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2167 | return main.FALSE |
| 2168 | else: |
| 2169 | return main.TRUE |
| 2170 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2171 | main.log.error( self.name + ": EOF exception found" ) |
| 2172 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2173 | main.cleanup() |
| 2174 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2175 | except Exception: |
| 2176 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2177 | main.cleanup() |
| 2178 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2179 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 2180 | def getLinkRandom( self, timeout=60, nonCut=True ): |
| 2181 | """ |
| 2182 | Randomly get a link from Mininet topology. |
| 2183 | If nonCut is True, it gets a list of non-cut links (the deletion |
| 2184 | of a non-cut link will not increase the number of connected |
| 2185 | component of a graph) and randomly returns one of them, otherwise |
| 2186 | it just randomly returns one link from all current links in |
| 2187 | Mininet. |
| 2188 | Returns the link as a list, e.g. [ 's1', 's2' ] |
| 2189 | """ |
| 2190 | import random |
| 2191 | candidateLinks = [] |
| 2192 | try: |
| 2193 | if not nonCut: |
| 2194 | links = self.getLinks( timeout=timeout ) |
| 2195 | assert len( links ) != 0 |
| 2196 | for link in links: |
| 2197 | # Exclude host-switch link |
| 2198 | if link[ 'node1' ].startswith( 'h' ) or link[ 'node2' ].startswith( 'h' ): |
| 2199 | continue |
| 2200 | candidateLinks.append( [ link[ 'node1' ], link[ 'node2' ] ] ) |
| 2201 | else: |
| 2202 | graphDict = self.getGraphDict( timeout=timeout, useId=False ) |
| 2203 | if graphDict == None: |
| 2204 | return None |
| 2205 | self.graph.update( graphDict ) |
| 2206 | candidateLinks = self.graph.getNonCutEdges() |
| 2207 | if candidateLinks == None: |
| 2208 | return None |
| 2209 | elif len( candidateLinks ) == 0: |
| 2210 | main.log.info( self.name + ": No candidate link for deletion" ) |
| 2211 | return None |
| 2212 | else: |
| 2213 | link = random.sample( candidateLinks, 1 ) |
| 2214 | return link[ 0 ] |
| 2215 | except KeyError: |
| 2216 | main.log.exception( self.name + ": KeyError exception found" ) |
| 2217 | return None |
| 2218 | except AssertionError: |
| 2219 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 2220 | return None |
| 2221 | except Exception: |
| 2222 | main.log.exception( self.name + ": Uncaught exception" ) |
| 2223 | return None |
| 2224 | |
| 2225 | def delLinkRandom( self, timeout=60, nonCut=True ): |
| 2226 | """ |
| 2227 | Randomly delete a link from Mininet topology. |
| 2228 | If nonCut is True, it gets a list of non-cut links (the deletion |
| 2229 | of a non-cut link will not increase the number of connected |
| 2230 | component of a graph) and randomly chooses one for deletion, |
| 2231 | otherwise it just randomly delete one link from all current links |
| 2232 | in Mininet. |
| 2233 | Returns the deleted link as a list, e.g. [ 's1', 's2' ] |
| 2234 | """ |
| 2235 | try: |
| 2236 | link = self.getLinkRandom( timeout, nonCut ) |
| 2237 | if link == None: |
| 2238 | return None |
| 2239 | else: |
| 2240 | deletionResult = self.delLink( link[ 0 ], link[ 1 ] ) |
| 2241 | if deletionResult: |
| 2242 | return link |
| 2243 | else: |
| 2244 | return None |
| 2245 | except Exception: |
| 2246 | main.log.exception( self.name + ": Uncaught exception" ) |
| 2247 | return None |
| 2248 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2249 | def addHost( self, hostname, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2250 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2251 | Add a host to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 2252 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 2253 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2254 | NOTE: cannot currently specify what type of host |
| 2255 | required params: |
| 2256 | hostname = the string hostname |
| 2257 | optional key-value params |
| 2258 | switch = "switch name" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2259 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2260 | """ |
| 2261 | switch = kwargs.get( 'switch', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2262 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2263 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2264 | response = self.execute( |
| 2265 | cmd=command, |
| 2266 | prompt="mininet>", |
| 2267 | timeout=10 ) |
| 2268 | if re.search( "already exists!", response ): |
| 2269 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2270 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2271 | elif re.search( "doesnt exists!", response ): |
| 2272 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2273 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2274 | elif re.search( "Error", response ): |
| 2275 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2276 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2277 | elif re.search( "usage:", response ): |
| 2278 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2279 | return main.FALSE |
| 2280 | else: |
| 2281 | return main.TRUE |
| 2282 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2283 | main.log.error( self.name + ": EOF exception found" ) |
| 2284 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2285 | main.cleanup() |
| 2286 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2287 | except Exception: |
| 2288 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2289 | main.cleanup() |
| 2290 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2291 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2292 | def delHost( self, hostname ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2293 | """ |
| 2294 | delete a host from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 2295 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 2296 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2297 | NOTE: this uses a custom mn function |
| 2298 | required params: |
| 2299 | hostname = the string hostname |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2300 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 2301 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2302 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2303 | response = self.execute( |
| 2304 | cmd=command, |
| 2305 | prompt="mininet>", |
| 2306 | timeout=10 ) |
| 2307 | if re.search( "no host named", response ): |
| 2308 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2309 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2310 | elif re.search( "Error", response ): |
| 2311 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2312 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2313 | elif re.search( "usage:", response ): |
| 2314 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2315 | return main.FALSE |
| 2316 | else: |
| 2317 | return main.TRUE |
| 2318 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2319 | main.log.error( self.name + ": EOF exception found" ) |
| 2320 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2321 | main.cleanup() |
| 2322 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2323 | except Exception: |
| 2324 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2325 | main.cleanup() |
| 2326 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 2327 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2328 | def disconnect( self ): |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 2329 | """ |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 2330 | Called at the end of the test to stop the mininet and |
| 2331 | disconnect the handle. |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 2332 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2333 | try: |
| 2334 | self.handle.sendline( '' ) |
| 2335 | i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ], |
| 2336 | timeout=2 ) |
| 2337 | response = main.TRUE |
| 2338 | if i == 0: |
| 2339 | response = self.stopNet() |
| 2340 | elif i == 1: |
| 2341 | return main.TRUE |
| 2342 | # print "Disconnecting Mininet" |
| 2343 | if self.handle: |
| 2344 | self.handle.sendline( "exit" ) |
| 2345 | self.handle.expect( "exit" ) |
| 2346 | self.handle.expect( "(.*)" ) |
| 2347 | else: |
| 2348 | main.log.error( "Connection failed to the host" ) |
| 2349 | return response |
| 2350 | except pexpect.EOF: |
| 2351 | main.log.error( self.name + ": EOF exception found" ) |
| 2352 | main.log.error( self.name + ": " + self.handle.before ) |
| 2353 | main.cleanup() |
| 2354 | main.exit() |
| 2355 | except Exception: |
| 2356 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2357 | main.cleanup() |
| 2358 | main.exit() |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 2359 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2360 | def stopNet( self, fileName="", timeout=5 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 2361 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 2362 | Stops mininet. |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2363 | Returns main.TRUE if the mininet successfully stops and |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 2364 | main.FALSE if the pexpect handle does not exist. |
| 2365 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 2366 | Will cleanup and exit the test if mininet fails to stop |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 2367 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 2368 | main.log.info( self.name + ": Stopping mininet..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2369 | response = '' |
| 2370 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 2371 | try: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2372 | self.handle.sendline( "" ) |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 2373 | i = self.handle.expect( [ 'mininet>', |
| 2374 | '\$', |
| 2375 | pexpect.EOF, |
| 2376 | pexpect.TIMEOUT ], |
| 2377 | timeout ) |
| 2378 | if i == 0: |
| 2379 | main.log.info( "Exiting mininet..." ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 2380 | response = self.execute( cmd="exit", |
| 2381 | prompt="(.*)", |
| 2382 | timeout=120 ) |
| 2383 | main.log.info( self.name + ": Stopped" ) |
| 2384 | self.handle.sendline( "sudo mn -c" ) |
| 2385 | response = main.TRUE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2386 | |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 2387 | elif i == 1: |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 2388 | main.log.info( " Mininet trying to exit while not " + |
| 2389 | "in the mininet prompt" ) |
Jeremy | 9cdb113 | 2016-04-19 10:50:40 -0700 | [diff] [blame] | 2390 | response = main.TRUE |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 2391 | elif i == 2: |
| 2392 | main.log.error( "Something went wrong exiting mininet" ) |
| 2393 | elif i == 3: # timeout |
| 2394 | main.log.error( "Something went wrong exiting mininet " + |
| 2395 | "TIMEOUT" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2396 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 2397 | if fileName: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2398 | self.handle.sendline( "" ) |
| 2399 | self.handle.expect( '\$' ) |
| 2400 | self.handle.sendline( |
| 2401 | "sudo kill -9 \`ps -ef | grep \"" + |
| 2402 | fileName + |
| 2403 | "\" | grep -v grep | awk '{print $2}'\`" ) |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2404 | except pexpect.TIMEOUT: |
| 2405 | main.log.error(self.name + ": TIMEOUT exception found") |
| 2406 | main.log.error(self.name + ": " + self.handle.before) |
| 2407 | main.cleanup() |
| 2408 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 2409 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2410 | main.log.error( self.name + ": EOF exception found" ) |
| 2411 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 2412 | main.cleanup() |
| 2413 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2414 | except Exception: |
| 2415 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2416 | main.cleanup() |
| 2417 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2418 | else: |
| 2419 | main.log.error( self.name + ": Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2420 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 2421 | return response |
| 2422 | |
YPZhang | 26a139e | 2016-04-25 14:01:55 -0700 | [diff] [blame] | 2423 | def arping( self, srcHost="", dstHost="10.128.20.211", ethDevice="", output=True, noResult=False ): |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 2424 | """ |
| 2425 | Description: |
| 2426 | Sends arp message from mininet host for hosts discovery |
| 2427 | Required: |
| 2428 | host - hosts name |
| 2429 | Optional: |
| 2430 | ip - ip address that does not exist in the network so there would |
| 2431 | be no reply. |
| 2432 | """ |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 2433 | if ethDevice: |
| 2434 | ethDevice = '-I ' + ethDevice + ' ' |
YPZhang | 26a139e | 2016-04-25 14:01:55 -0700 | [diff] [blame] | 2435 | cmd = srcHost + " arping -c1 " |
| 2436 | if noResult: |
| 2437 | cmd += "-w10 " # If we don't want the actural arping result, set -w10, arping will exit after 10 ms. |
| 2438 | cmd += ethDevice + dstHost |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 2439 | try: |
YPZhang | 81a7d4e | 2016-04-18 13:10:17 -0700 | [diff] [blame] | 2440 | if output: |
| 2441 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 2442 | self.handle.sendline( cmd ) |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 2443 | i = self.handle.expect( [ "mininet>", "arping: " ] ) |
| 2444 | if i == 0: |
| 2445 | return main.TRUE |
| 2446 | elif i == 1: |
| 2447 | response = self.handle.before + self.handle.after |
| 2448 | self.handle.expect( "mininet>" ) |
| 2449 | response += self.handle.before + self.handle.after |
| 2450 | main.log.warn( "Error sending arping, output was: " + |
| 2451 | response ) |
| 2452 | return main.FALSE |
| 2453 | except pexpect.TIMEOUT: |
| 2454 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 2455 | main.log.warn( self.handle.before ) |
| 2456 | return main.FALSE |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 2457 | except pexpect.EOF: |
| 2458 | main.log.error( self.name + ": EOF exception found" ) |
| 2459 | main.log.error( self.name + ": " + self.handle.before ) |
| 2460 | main.cleanup() |
| 2461 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2462 | except Exception: |
| 2463 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2464 | main.cleanup() |
| 2465 | main.exit() |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 2466 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2467 | def decToHex( self, num ): |
| 2468 | return hex( num ).split( 'x' )[ 1 ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 2469 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2470 | def getSwitchFlowCount( self, switch ): |
| 2471 | """ |
| 2472 | return the Flow Count of the switch""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2473 | if self.handle: |
| 2474 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 2475 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2476 | response = self.execute( |
| 2477 | cmd=cmd, |
| 2478 | prompt="mininet>", |
| 2479 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2480 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2481 | main.log.error( self.name + ": EOF exception found" ) |
| 2482 | main.log.error( self.name + " " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2483 | main.cleanup() |
| 2484 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2485 | except Exception: |
| 2486 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2487 | main.cleanup() |
| 2488 | main.exit() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2489 | pattern = "flow_count=(\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2490 | result = re.search( pattern, response, re.MULTILINE ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2491 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2492 | main.log.info( |
| 2493 | "Couldn't find flows on switch %s, found: %s" % |
| 2494 | ( switch, response ) ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2495 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2496 | return result.group( 1 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2497 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2498 | main.log.error( "Connection failed to the Mininet host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 2499 | |
Jon Hall | 9ed8f37 | 2016-02-24 17:34:07 -0800 | [diff] [blame] | 2500 | def checkFlows( self, sw, dumpFormat=None ): |
| 2501 | if dumpFormat: |
| 2502 | command = "sh ovs-ofctl -F " + \ |
| 2503 | dumpFormat + " dump-flows " + str( sw ) |
| 2504 | else: |
| 2505 | command = "sh ovs-ofctl dump-flows " + str( sw ) |
| 2506 | try: |
| 2507 | response = self.execute( |
| 2508 | cmd=command, |
| 2509 | prompt="mininet>", |
| 2510 | timeout=10 ) |
| 2511 | return response |
| 2512 | except pexpect.EOF: |
| 2513 | main.log.error( self.name + ": EOF exception found" ) |
| 2514 | main.log.error( self.name + ": " + self.handle.before ) |
| 2515 | main.cleanup() |
| 2516 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2517 | except Exception: |
| 2518 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2519 | main.cleanup() |
| 2520 | main.exit() |
Jon Hall | 9ed8f37 | 2016-02-24 17:34:07 -0800 | [diff] [blame] | 2521 | |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 2522 | def flowTableComp( self, flowTable1, flowTable2 ): |
| 2523 | # This function compares the selctors and treatments of each flow |
| 2524 | try: |
Jon Hall | 3c512f7 | 2016-05-06 10:44:45 -0700 | [diff] [blame] | 2525 | assert flowTable1, "flowTable1 is empty or None" |
| 2526 | assert flowTable2, "flowTable2 is empty or None" |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 2527 | returnValue = main.TRUE |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 2528 | if len(flowTable1) != len(flowTable2): |
| 2529 | main.log.warn( "Flow table lengths do not match" ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 2530 | returnValue = main.FALSE |
GlennRC | fa65fce | 2015-12-16 13:16:08 -0800 | [diff] [blame] | 2531 | dFields = ["n_bytes", "cookie", "n_packets", "duration"] |
| 2532 | for flow1, flow2 in zip(flowTable1, flowTable2): |
Jon Hall | acd1b18 | 2015-12-17 11:43:20 -0800 | [diff] [blame] | 2533 | for field in dFields: |
| 2534 | try: |
| 2535 | flow1.pop( field ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 2536 | except KeyError: |
| 2537 | pass |
Jon Hall | acd1b18 | 2015-12-17 11:43:20 -0800 | [diff] [blame] | 2538 | try: |
| 2539 | flow2.pop( field ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 2540 | except KeyError: |
| 2541 | pass |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 2542 | for i in range( len(flowTable1) ): |
GlennRC | 17bbcf5 | 2015-12-14 17:31:50 -0800 | [diff] [blame] | 2543 | if flowTable1[i] not in flowTable2: |
| 2544 | main.log.warn( "Flow tables do not match:" ) |
| 2545 | main.log.warn( "Old flow:\n{}\n not in new flow table".format( flowTable1[i] ) ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 2546 | returnValue = main.FALSE |
| 2547 | break |
| 2548 | return returnValue |
Jon Hall | 3c512f7 | 2016-05-06 10:44:45 -0700 | [diff] [blame] | 2549 | except AssertionError: |
| 2550 | main.log.exception( "Nothing to compare" ) |
| 2551 | return main.FALSE |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 2552 | except Exception: |
| 2553 | main.log.exception( "Uncaught exception!" ) |
| 2554 | main.cleanup() |
| 2555 | main.exit() |
Jon Hall | 9043c90 | 2015-07-30 14:23:44 -0700 | [diff] [blame] | 2556 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2557 | def parseFlowTable( self, flowTable, version="", debug=True ): |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2558 | ''' |
| 2559 | Discription: Parses flows into json format. |
| 2560 | NOTE: this can parse any string thats separated with commas |
| 2561 | Arguments: |
| 2562 | Required: |
| 2563 | flows: a list of strings that represnt flows |
| 2564 | Optional: |
| 2565 | version: The version of OpenFlow. Currently, 1.3 and 1.0 are supported. |
| 2566 | debug: prints out the final result |
| 2567 | returns: A list of flows in json format |
| 2568 | ''' |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2569 | jsonFlowTable = [] |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2570 | try: |
| 2571 | for flow in flowTable: |
| 2572 | jsonFlow = {} |
| 2573 | # split up the fields of the flow |
| 2574 | parsedFlow = flow.split(", ") |
| 2575 | # get rid of any spaces in front of the field |
| 2576 | for i in range( len(parsedFlow) ): |
| 2577 | item = parsedFlow[i] |
| 2578 | if item[0] == " ": |
| 2579 | parsedFlow[i] = item[1:] |
| 2580 | # grab the selector and treatment from the parsed flow |
| 2581 | # the last element is the selector and the treatment |
| 2582 | temp = parsedFlow.pop(-1) |
| 2583 | # split up the selector and the treatment |
| 2584 | temp = temp.split(" ") |
| 2585 | index = 0 |
| 2586 | # parse the flags |
| 2587 | # NOTE: This only parses one flag |
| 2588 | flag = {} |
| 2589 | if version == "1.3": |
| 2590 | flag = {"flag":[temp[index]]} |
| 2591 | index += 1 |
| 2592 | # the first element is the selector and split it up |
| 2593 | sel = temp[index] |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2594 | index += 1 |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2595 | sel = sel.split(",") |
| 2596 | # the priority is stuck in the selecter so put it back |
| 2597 | # in the flow |
| 2598 | parsedFlow.append(sel.pop(0)) |
| 2599 | # parse selector |
| 2600 | criteria = [] |
| 2601 | for item in sel: |
| 2602 | # this is the type of the packet e.g. "arp" |
| 2603 | if "=" not in item: |
| 2604 | criteria.append( {"type":item} ) |
| 2605 | else: |
| 2606 | field = item.split("=") |
| 2607 | criteria.append( {field[0]:field[1]} ) |
| 2608 | selector = {"selector": {"criteria":sorted(criteria)} } |
| 2609 | treat = temp[index] |
| 2610 | # get rid of the action part e.g. "action=output:2" |
| 2611 | # we will add it back later |
| 2612 | treat = treat.split("=") |
| 2613 | treat.pop(0) |
| 2614 | # parse treatment |
| 2615 | action = [] |
| 2616 | for item in treat: |
| 2617 | field = item.split(":") |
| 2618 | action.append( {field[0]:field[1]} ) |
| 2619 | # create the treatment field and add the actions |
| 2620 | treatment = {"treatment": {"action":sorted(action)} } |
| 2621 | # parse the rest of the flow |
| 2622 | for item in parsedFlow: |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2623 | field = item.split("=") |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2624 | jsonFlow.update( {field[0]:field[1]} ) |
| 2625 | # add the treatment and the selector to the json flow |
| 2626 | jsonFlow.update( selector ) |
| 2627 | jsonFlow.update( treatment ) |
| 2628 | jsonFlow.update( flag ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2629 | |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2630 | if debug: main.log.debug( "\033[94mJson flow:\033[0m\n{}\n".format(jsonFlow) ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2631 | |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2632 | # add the json flow to the json flow table |
| 2633 | jsonFlowTable.append( jsonFlow ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2634 | |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2635 | return jsonFlowTable |
| 2636 | |
| 2637 | except IndexError: |
| 2638 | main.log.exception( self.name + ": IndexError found" ) |
| 2639 | return None |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2640 | except pexpect.EOF: |
| 2641 | main.log.error( self.name + ": EOF exception found" ) |
| 2642 | main.log.error( self.name + ": " + self.handle.before ) |
| 2643 | main.cleanup() |
| 2644 | main.exit() |
You Wang | 91c37cf | 2016-05-23 09:39:42 -0700 | [diff] [blame] | 2645 | except Exception: |
| 2646 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2647 | main.cleanup() |
| 2648 | main.exit() |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2649 | |
Jon Hall | 0a54379 | 2015-12-14 11:00:26 -0800 | [diff] [blame] | 2650 | def getFlowTable( self, sw, version="", debug=False): |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2651 | ''' |
| 2652 | Discription: Returns the flow table(s) on a switch or switches in a list. |
| 2653 | Each element is a flow. |
| 2654 | Arguments: |
| 2655 | Required: |
| 2656 | sw: The switch name ("s1") to retrive the flow table. Can also be |
| 2657 | a list of switches. |
| 2658 | Optional: |
| 2659 | version: The version of OpenFlow. Currently, 1.3 and 1.0 are supported. |
| 2660 | debug: prints out the final result |
| 2661 | ''' |
| 2662 | try: |
| 2663 | switches = [] |
| 2664 | if type(sw) is list: |
Jon Hall | ca7ac29 | 2015-11-11 09:28:12 -0800 | [diff] [blame] | 2665 | switches.extend(sw) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2666 | else: switches.append(sw) |
| 2667 | |
| 2668 | flows = [] |
| 2669 | for s in switches: |
| 2670 | cmd = "sh ovs-ofctl dump-flows " + s |
| 2671 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2672 | if "1.0" == version: |
| 2673 | cmd += " -F OpenFlow10-table_id" |
| 2674 | elif "1.3" == version: |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2675 | cmd += " -O OpenFlow13" |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2676 | |
| 2677 | main.log.info( "Sending: " + cmd ) |
| 2678 | self.handle.sendline( cmd ) |
| 2679 | self.handle.expect( "mininet>" ) |
| 2680 | response = self.handle.before |
| 2681 | response = response.split( "\r\n" ) |
| 2682 | # dump the first two elements and the last |
| 2683 | # the first element is the command that was sent |
| 2684 | # the second is the table header |
| 2685 | # the last element is empty |
| 2686 | response = response[2:-1] |
| 2687 | flows.extend( response ) |
| 2688 | |
| 2689 | if debug: print "Flows:\n{}\n\n".format(flows) |
| 2690 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2691 | return self.parseFlowTable( flows, version, debug ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2692 | |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2693 | except pexpect.EOF: |
| 2694 | main.log.exception( self.name + ": connection closed." ) |
| 2695 | main.cleanup() |
| 2696 | main.exit() |
| 2697 | except Exception: |
| 2698 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2699 | main.cleanup() |
| 2700 | main.exit() |
| 2701 | |
| 2702 | def checkFlowId( self, sw, flowId, version="1.3", debug=True ): |
| 2703 | ''' |
| 2704 | Discription: Checks whether the ID provided matches a flow ID in Mininet |
| 2705 | Arguments: |
| 2706 | Required: |
| 2707 | sw: The switch name ("s1") to retrive the flow table. Can also be |
| 2708 | a list of switches. |
| 2709 | flowId: the flow ID in hex format. Can also be a list of IDs |
| 2710 | Optional: |
| 2711 | version: The version of OpenFlow. Currently, 1.3 and 1.0 are supported. |
| 2712 | debug: prints out the final result |
| 2713 | returns: main.TRUE if all IDs are present, otherwise returns main.FALSE |
| 2714 | NOTE: prints out IDs that are not present |
| 2715 | ''' |
| 2716 | try: |
| 2717 | main.log.info( "Getting flows from Mininet" ) |
| 2718 | flows = self.getFlowTable( sw, version, debug ) |
You Wang | 083ae98 | 2016-05-25 09:31:09 -0700 | [diff] [blame] | 2719 | if flows == None: |
| 2720 | return main.ERROR |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2721 | |
| 2722 | if debug: print "flow ids:\n{}\n\n".format(flowId) |
| 2723 | |
| 2724 | # Check flowId is a list or a string |
| 2725 | if type( flowId ) is str: |
| 2726 | result = False |
| 2727 | for f in flows: |
| 2728 | if flowId in f.get( 'cookie' ): |
| 2729 | result = True |
| 2730 | break |
| 2731 | # flowId is a list |
| 2732 | else: |
| 2733 | result = True |
| 2734 | # Get flow IDs from Mininet |
| 2735 | mnFlowIds = [ f.get( 'cookie' ) for f in flows ] |
| 2736 | # Save the IDs that are not in Mininet |
| 2737 | absentIds = [ x for x in flowId if x not in mnFlowIds ] |
| 2738 | |
| 2739 | if debug: print "mn flow ids:\n{}\n\n".format(mnFlowIds) |
| 2740 | |
| 2741 | # Print out the IDs that are not in Mininet |
| 2742 | if absentIds: |
| 2743 | main.log.warn( "Absent ids: {}".format( absentIds ) ) |
| 2744 | result = False |
| 2745 | |
| 2746 | return main.TRUE if result else main.FALSE |
| 2747 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2748 | except pexpect.EOF: |
| 2749 | main.log.error( self.name + ": EOF exception found" ) |
| 2750 | main.log.error( self.name + ": " + self.handle.before ) |
| 2751 | main.cleanup() |
| 2752 | main.exit() |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2753 | except Exception: |
| 2754 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2755 | main.cleanup() |
| 2756 | main.exit() |
| 2757 | |
| 2758 | |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 2759 | def startTcpdump( self, filename, intf="eth0", port="port 6653" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2760 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2761 | Runs tpdump on an interface and saves the file |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2762 | intf can be specified, or the default eth0 is used""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2763 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2764 | self.handle.sendline( "" ) |
| 2765 | self.handle.expect( "mininet>" ) |
| 2766 | self.handle.sendline( |
| 2767 | "sh sudo tcpdump -n -i " + |
| 2768 | intf + |
| 2769 | " " + |
| 2770 | port + |
| 2771 | " -w " + |
| 2772 | filename.strip() + |
| 2773 | " &" ) |
| 2774 | self.handle.sendline( "" ) |
| 2775 | i = self.handle.expect( [ 'No\ssuch\device', |
| 2776 | 'listening\son', |
| 2777 | pexpect.TIMEOUT, |
| 2778 | "mininet>" ], |
| 2779 | timeout=10 ) |
| 2780 | main.log.warn( self.handle.before + self.handle.after ) |
| 2781 | self.handle.sendline( "" ) |
| 2782 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2783 | if i == 0: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2784 | main.log.error( |
| 2785 | self.name + |
| 2786 | ": tcpdump - No such device exists. " + |
| 2787 | "tcpdump attempted on: " + |
| 2788 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2789 | return main.FALSE |
| 2790 | elif i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2791 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2792 | return main.TRUE |
| 2793 | elif i == 2: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2794 | main.log.error( |
| 2795 | self.name + |
| 2796 | ": tcpdump command timed out! Check interface name," + |
| 2797 | " given interface was: " + |
| 2798 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2799 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2800 | elif i == 3: |
| 2801 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2802 | return main.TRUE |
| 2803 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2804 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2805 | return main.FALSE |
| 2806 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2807 | main.log.error( self.name + ": EOF exception found" ) |
| 2808 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2809 | main.cleanup() |
| 2810 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2811 | except Exception: |
| 2812 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2813 | main.cleanup() |
| 2814 | main.exit() |
| 2815 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2816 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2817 | """ |
| 2818 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2819 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2820 | self.handle.sendline( "sh sudo pkill tcpdump" ) |
| 2821 | self.handle.expect( "mininet>" ) |
| 2822 | self.handle.sendline( "" ) |
| 2823 | self.handle.expect( "mininet>" ) |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2824 | except pexpect.TIMEOUT: |
| 2825 | main.log.error(self.name + ": TIMEOUT exception found") |
| 2826 | main.log.error(self.name + ": " + self.handle.before) |
| 2827 | main.cleanup() |
| 2828 | main.exit() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2829 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2830 | main.log.error( self.name + ": EOF exception found" ) |
| 2831 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2832 | main.cleanup() |
| 2833 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2834 | except Exception: |
| 2835 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2836 | main.cleanup() |
| 2837 | main.exit() |
| 2838 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2839 | def getPorts( self, nodeName, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2840 | """ |
| 2841 | Read ports from a Mininet switch. |
| 2842 | |
| 2843 | Returns a json structure containing information about the |
| 2844 | ports of the given switch. |
| 2845 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2846 | try: |
| 2847 | response = self.getInterfaces( nodeName ) |
| 2848 | # TODO: Sanity check on response. log if no such switch exists |
| 2849 | ports = [] |
| 2850 | for line in response.split( "\n" ): |
| 2851 | if not line.startswith( "name=" ): |
| 2852 | continue |
| 2853 | portVars = {} |
| 2854 | for var in line.split( "," ): |
| 2855 | key, value = var.split( "=" ) |
| 2856 | portVars[ key ] = value |
| 2857 | isUp = portVars.pop( 'enabled', "True" ) |
| 2858 | isUp = "True" in isUp |
| 2859 | if verbose: |
| 2860 | main.log.info( "Reading switch port %s(%s)" % |
| 2861 | ( portVars[ 'name' ], portVars[ 'mac' ] ) ) |
| 2862 | mac = portVars[ 'mac' ] |
| 2863 | if mac == 'None': |
| 2864 | mac = None |
| 2865 | ips = [] |
| 2866 | ip = portVars[ 'ip' ] |
| 2867 | if ip == 'None': |
| 2868 | ip = None |
| 2869 | ips.append( ip ) |
| 2870 | name = portVars[ 'name' ] |
| 2871 | if name == 'None': |
| 2872 | name = None |
| 2873 | portRe = r'[^\-]\d\-eth(?P<port>\d+)' |
| 2874 | if name == 'lo': |
| 2875 | portNo = 0xfffe # TODO: 1.0 value - Should we just return lo? |
| 2876 | else: |
| 2877 | portNo = re.search( portRe, name ).group( 'port' ) |
| 2878 | ports.append( { 'of_port': portNo, |
| 2879 | 'mac': str( mac ).replace( '\'', '' ), |
| 2880 | 'name': name, |
| 2881 | 'ips': ips, |
| 2882 | 'enabled': isUp } ) |
| 2883 | return ports |
| 2884 | except pexpect.EOF: |
| 2885 | main.log.error( self.name + ": EOF exception found" ) |
| 2886 | main.log.error( self.name + ": " + self.handle.before ) |
| 2887 | main.cleanup() |
| 2888 | main.exit() |
| 2889 | except Exception: |
| 2890 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2891 | main.cleanup() |
| 2892 | main.exit() |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2893 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 2894 | def getOVSPorts( self, nodeName ): |
| 2895 | """ |
| 2896 | Read ports from OVS by executing 'ovs-ofctl dump-ports-desc' command. |
| 2897 | |
| 2898 | Returns a list of dictionaries containing information about each |
| 2899 | port of the given switch. |
| 2900 | """ |
| 2901 | command = "sh ovs-ofctl dump-ports-desc " + str( nodeName ) |
| 2902 | try: |
| 2903 | response = self.execute( |
| 2904 | cmd=command, |
| 2905 | prompt="mininet>", |
| 2906 | timeout=10 ) |
| 2907 | ports = [] |
| 2908 | if response: |
| 2909 | for line in response.split( "\n" ): |
| 2910 | # Regex patterns to parse 'ovs-ofctl dump-ports-desc' output |
| 2911 | # Example port: |
| 2912 | # 1(s1-eth1): addr:ae:60:72:77:55:51 |
| 2913 | pattern = "(?P<index>\d+)\((?P<name>[^-]+-eth(?P<port>\d+))\):\saddr:(?P<mac>([a-f0-9]{2}:){5}[a-f0-9]{2})" |
| 2914 | result = re.search( pattern, line ) |
| 2915 | if result: |
| 2916 | index = result.group( 'index' ) |
| 2917 | name = result.group( 'name' ) |
| 2918 | # This port number is extracted from port name |
| 2919 | port = result.group( 'port' ) |
| 2920 | mac = result.group( 'mac' ) |
| 2921 | ports.append( { 'index': index, |
| 2922 | 'name': name, |
| 2923 | 'port': port, |
| 2924 | 'mac': mac } ) |
| 2925 | return ports |
| 2926 | except pexpect.EOF: |
| 2927 | main.log.error( self.name + ": EOF exception found" ) |
| 2928 | main.log.error( self.name + ": " + self.handle.before ) |
| 2929 | main.cleanup() |
| 2930 | main.exit() |
| 2931 | except Exception: |
| 2932 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2933 | main.cleanup() |
| 2934 | main.exit() |
| 2935 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2936 | def getSwitches( self, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2937 | """ |
| 2938 | Read switches from Mininet. |
| 2939 | |
| 2940 | Returns a dictionary whose keys are the switch names and the value is |
| 2941 | a dictionary containing information about the switch. |
| 2942 | """ |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2943 | # NOTE: To support new Mininet switch classes, just append the new |
| 2944 | # class to the switchClasses variable |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2945 | |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2946 | # Regex patterns to parse 'dump' output |
| 2947 | # Example Switches: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2948 | # <OVSSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=5238> |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2949 | # <OVSSwitch{ 'protocols': 'OpenFlow10' } s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=25974> |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2950 | # <OVSSwitchNS s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=22550> |
| 2951 | # <OVSBridge s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=26830> |
| 2952 | # <UserSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=14737> |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 2953 | try: |
| 2954 | switchClasses = r"(OVSSwitch)|(OVSBridge)|(OVSSwitchNS)|(IVSSwitch)|(LinuxBridge)|(UserSwitch)" |
| 2955 | swRE = r"<(?P<class>" + switchClasses + r")" +\ |
| 2956 | r"(?P<options>\{.*\})?\s" +\ |
| 2957 | r"(?P<name>[^:]+)\:\s" +\ |
| 2958 | r"(?P<ports>([^,]+,)*[^,\s]+)" +\ |
| 2959 | r"\spid=(?P<pid>(\d)+)" |
| 2960 | # Update mn port info |
| 2961 | self.update() |
| 2962 | output = {} |
| 2963 | dump = self.dump().split( "\n" ) |
| 2964 | for line in dump: |
| 2965 | result = re.search( swRE, line, re.I ) |
| 2966 | if result: |
| 2967 | name = result.group( 'name' ) |
| 2968 | dpid = str( self.getSwitchDPID( name ) ).zfill( 16 ) |
| 2969 | pid = result.group( 'pid' ) |
| 2970 | swClass = result.group( 'class' ) |
| 2971 | options = result.group( 'options' ) |
| 2972 | if verbose: |
| 2973 | main.log.info( "Reading switch %s(%s)" % ( name, dpid ) ) |
| 2974 | ports = self.getPorts( name ) |
| 2975 | output[ name ] = { "dpid": dpid, |
| 2976 | "ports": ports, |
| 2977 | "swClass": swClass, |
| 2978 | "pid": pid, |
| 2979 | "options": options } |
| 2980 | return output |
| 2981 | except pexpect.EOF: |
| 2982 | main.log.error( self.name + ": EOF exception found" ) |
| 2983 | main.log.error( self.name + ": " + self.handle.before ) |
| 2984 | main.cleanup() |
| 2985 | main.exit() |
| 2986 | except Exception: |
| 2987 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2988 | main.cleanup() |
| 2989 | main.exit() |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2990 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2991 | def getHosts( self, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2992 | """ |
| 2993 | Read hosts from Mininet. |
| 2994 | |
| 2995 | Returns a dictionary whose keys are the host names and the value is |
| 2996 | a dictionary containing information about the host. |
| 2997 | """ |
| 2998 | # Regex patterns to parse dump output |
| 2999 | # Example host: <Host h1: h1-eth0:10.0.0.1 pid=5227> |
kelvin-onlab | 299ab06 | 2015-07-15 10:58:27 -0700 | [diff] [blame] | 3000 | # <Host h1: pid=12725> |
| 3001 | # <VLANHost h12: h12-eth0.100.100.100:100.1.0.3 pid=30186> |
| 3002 | # <dualStackHost h19: h19-eth0:10.1.0.9 pid=30200> |
| 3003 | # <IPv6Host h18: h18-eth0:10.0.0.18 pid=30198> |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3004 | # NOTE: Does not correctly match hosts with multi-links |
| 3005 | # <Host h2: h2-eth0:10.0.0.2,h2-eth1:10.0.1.2 pid=14386> |
| 3006 | # FIXME: Fix that |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3007 | try: |
| 3008 | hostRE = r"Host\s(?P<name>[^:]+)\:((\s(?P<ifname>[^:]+)\:" +\ |
| 3009 | "(?P<ip>[^\s]+))|(\s)\spid=(?P<pid>[^>]+))" |
| 3010 | # update mn port info |
| 3011 | self.update() |
| 3012 | # Get mininet dump |
| 3013 | dump = self.dump().split( "\n" ) |
| 3014 | hosts = {} |
| 3015 | for line in dump: |
| 3016 | if "Host" in line : |
| 3017 | result = re.search( hostRE, line ) |
| 3018 | name = result.group( 'name' ) |
| 3019 | interfaces = [] |
| 3020 | response = self.getInterfaces( name ) |
| 3021 | # Populate interface info |
| 3022 | for line in response.split( "\n" ): |
| 3023 | if line.startswith( "name=" ): |
| 3024 | portVars = {} |
| 3025 | for var in line.split( "," ): |
| 3026 | key, value = var.split( "=" ) |
| 3027 | portVars[ key ] = value |
| 3028 | isUp = portVars.pop( 'enabled', "True" ) |
| 3029 | isUp = "True" in isUp |
| 3030 | if verbose: |
| 3031 | main.log.info( "Reading host port %s(%s)" % |
| 3032 | ( portVars[ 'name' ], |
| 3033 | portVars[ 'mac' ] ) ) |
| 3034 | mac = portVars[ 'mac' ] |
| 3035 | if mac == 'None': |
| 3036 | mac = None |
| 3037 | ips = [] |
| 3038 | ip = portVars[ 'ip' ] |
| 3039 | if ip == 'None': |
| 3040 | ip = None |
| 3041 | ips.append( ip ) |
| 3042 | intfName = portVars[ 'name' ] |
| 3043 | if name == 'None': |
| 3044 | name = None |
| 3045 | interfaces.append( { |
| 3046 | "name": intfName, |
| 3047 | "ips": ips, |
| 3048 | "mac": str( mac ), |
| 3049 | "isUp": isUp } ) |
| 3050 | hosts[ name ] = { "interfaces": interfaces } |
| 3051 | return hosts |
| 3052 | except pexpect.EOF: |
| 3053 | main.log.error( self.name + ": EOF exception found" ) |
| 3054 | main.log.error( self.name + ": " + self.handle.before ) |
| 3055 | main.cleanup() |
| 3056 | main.exit() |
| 3057 | except Exception: |
| 3058 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3059 | main.cleanup() |
| 3060 | main.exit() |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3061 | |
YPZhang | 81a7d4e | 2016-04-18 13:10:17 -0700 | [diff] [blame] | 3062 | def getLinks( self, timeout=20 ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3063 | """ |
| 3064 | Gathers information about current Mininet links. These links may not |
| 3065 | be up if one of the ports is down. |
| 3066 | |
| 3067 | Returns a list of dictionaries with link endpoints. |
| 3068 | |
| 3069 | The dictionary structure is: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3070 | { 'node1': str( node1 name ) |
| 3071 | 'node2': str( node2 name ) |
| 3072 | 'port1': str( port1 of_port ) |
| 3073 | 'port2': str( port2 of_port ) } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3074 | Note: The port number returned is the eth#, not necessarily the of_port |
| 3075 | number. In Mininet, for OVS switch, these should be the same. For |
| 3076 | hosts, this is just the eth#. |
| 3077 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3078 | try: |
| 3079 | self.update() |
| 3080 | response = self.links(timeout=timeout).split( '\n' ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3081 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3082 | # Examples: |
| 3083 | # s1-eth3<->s2-eth1 (OK OK) |
| 3084 | # s13-eth3<->h27-eth0 (OK OK) |
| 3085 | linkRE = "(?P<node1>[\w]+)\-eth(?P<port1>[\d]+)\<\-\>" +\ |
| 3086 | "(?P<node2>[\w]+)\-eth(?P<port2>[\d]+)" |
| 3087 | links = [] |
| 3088 | for line in response: |
| 3089 | match = re.search( linkRE, line ) |
| 3090 | if match: |
| 3091 | node1 = match.group( 'node1' ) |
| 3092 | node2 = match.group( 'node2' ) |
| 3093 | port1 = match.group( 'port1' ) |
| 3094 | port2 = match.group( 'port2' ) |
| 3095 | links.append( { 'node1': node1, |
| 3096 | 'node2': node2, |
| 3097 | 'port1': port1, |
| 3098 | 'port2': port2 } ) |
| 3099 | return links |
| 3100 | |
| 3101 | except pexpect.EOF: |
| 3102 | main.log.error( self.name + ": EOF exception found" ) |
| 3103 | main.log.error( self.name + ": " + self.handle.before ) |
| 3104 | main.cleanup() |
| 3105 | main.exit() |
| 3106 | except Exception: |
| 3107 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3108 | main.cleanup() |
| 3109 | main.exit() |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3110 | |
| 3111 | def compareSwitches( self, switches, switchesJson, portsJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3112 | """ |
| 3113 | Compare mn and onos switches |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3114 | switchesJson: parsed json object from the onos devices api |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 3115 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3116 | Dependencies: |
| 3117 | 1. numpy - "sudo pip install numpy" |
| 3118 | """ |
| 3119 | from numpy import uint64 |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 3120 | # created sorted list of dpid's in MN and ONOS for comparison |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3121 | try: |
| 3122 | mnDPIDs = [] |
| 3123 | for swName, switch in switches.iteritems(): |
| 3124 | mnDPIDs.append( switch[ 'dpid' ].lower() ) |
| 3125 | mnDPIDs.sort() |
| 3126 | if switchesJson == "": # if rest call fails |
| 3127 | main.log.error( |
| 3128 | self.name + |
| 3129 | ".compareSwitches(): Empty JSON object given from ONOS" ) |
| 3130 | return main.FALSE |
| 3131 | onos = switchesJson |
| 3132 | onosDPIDs = [] |
| 3133 | for switch in onos: |
| 3134 | if switch[ 'available' ]: |
| 3135 | onosDPIDs.append( |
| 3136 | switch[ 'id' ].replace( |
| 3137 | ":", |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3138 | '' ).replace( |
| 3139 | "of", |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3140 | '' ).lower() ) |
| 3141 | onosDPIDs.sort() |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3142 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3143 | if mnDPIDs != onosDPIDs: |
| 3144 | switchResults = main.FALSE |
| 3145 | main.log.error( "Switches in MN but not in ONOS:" ) |
| 3146 | list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ] |
| 3147 | main.log.error( str( list1 ) ) |
| 3148 | main.log.error( "Switches in ONOS but not in MN:" ) |
| 3149 | list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ] |
| 3150 | main.log.error( str( list2 ) ) |
| 3151 | else: # list of dpid's match in onos and mn |
| 3152 | switchResults = main.TRUE |
| 3153 | finalResults = switchResults |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 3154 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3155 | # FIXME: this does not look for extra ports in ONOS, only checks that |
| 3156 | # ONOS has what is in MN |
| 3157 | portsResults = main.TRUE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3158 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3159 | # PORTS |
| 3160 | for name, mnSwitch in switches.iteritems(): |
| 3161 | mnPorts = [] |
| 3162 | onosPorts = [] |
| 3163 | switchResult = main.TRUE |
| 3164 | for port in mnSwitch[ 'ports' ]: |
| 3165 | if port[ 'enabled' ]: |
| 3166 | mnPorts.append( int( port[ 'of_port' ] ) ) |
| 3167 | for onosSwitch in portsJson: |
| 3168 | if onosSwitch[ 'device' ][ 'available' ]: |
| 3169 | if onosSwitch[ 'device' ][ 'id' ].replace( |
| 3170 | ':', |
| 3171 | '' ).replace( |
| 3172 | "of", |
| 3173 | '' ) == mnSwitch[ 'dpid' ]: |
| 3174 | for port in onosSwitch[ 'ports' ]: |
| 3175 | if port[ 'isEnabled' ]: |
| 3176 | if port[ 'port' ] == 'local': |
| 3177 | # onosPorts.append( 'local' ) |
| 3178 | onosPorts.append( long( uint64( -2 ) ) ) |
| 3179 | else: |
| 3180 | onosPorts.append( int( port[ 'port' ] ) ) |
| 3181 | break |
| 3182 | mnPorts.sort( key=float ) |
| 3183 | onosPorts.sort( key=float ) |
| 3184 | |
| 3185 | mnPortsLog = mnPorts |
| 3186 | onosPortsLog = onosPorts |
| 3187 | mnPorts = [ x for x in mnPorts ] |
| 3188 | onosPorts = [ x for x in onosPorts ] |
| 3189 | |
| 3190 | # TODO: handle other reserved port numbers besides LOCAL |
| 3191 | # NOTE: Reserved ports |
| 3192 | # Local port: -2 in Openflow, ONOS shows 'local', we store as |
| 3193 | # long( uint64( -2 ) ) |
| 3194 | for mnPort in mnPortsLog: |
| 3195 | if mnPort in onosPorts: |
| 3196 | # don't set results to true here as this is just one of |
| 3197 | # many checks and it might override a failure |
| 3198 | mnPorts.remove( mnPort ) |
| 3199 | onosPorts.remove( mnPort ) |
| 3200 | |
| 3201 | # NOTE: OVS reports this as down since there is no link |
| 3202 | # So ignoring these for now |
| 3203 | # TODO: Come up with a better way of handling these |
| 3204 | if 65534 in mnPorts: |
| 3205 | mnPorts.remove( 65534 ) |
| 3206 | if long( uint64( -2 ) ) in onosPorts: |
| 3207 | onosPorts.remove( long( uint64( -2 ) ) ) |
| 3208 | if len( mnPorts ): # the ports of this switch don't match |
| 3209 | switchResult = main.FALSE |
| 3210 | main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) ) |
| 3211 | if len( onosPorts ): # the ports of this switch don't match |
| 3212 | switchResult = main.FALSE |
| 3213 | main.log.warn( |
| 3214 | "Ports in ONOS but not MN: " + |
| 3215 | str( onosPorts ) ) |
| 3216 | if switchResult == main.FALSE: |
| 3217 | main.log.error( |
| 3218 | "The list of ports for switch %s(%s) does not match:" % |
| 3219 | ( name, mnSwitch[ 'dpid' ] ) ) |
| 3220 | main.log.warn( "mn_ports[] = " + str( mnPortsLog ) ) |
| 3221 | main.log.warn( "onos_ports[] = " + str( onosPortsLog ) ) |
| 3222 | portsResults = portsResults and switchResult |
| 3223 | finalResults = finalResults and portsResults |
| 3224 | return finalResults |
| 3225 | except pexpect.EOF: |
| 3226 | main.log.error( self.name + ": EOF exception found" ) |
| 3227 | main.log.error( self.name + ": " + self.handle.before ) |
| 3228 | main.cleanup() |
| 3229 | main.exit() |
| 3230 | except Exception: |
| 3231 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3232 | main.cleanup() |
| 3233 | main.exit() |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 3234 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3235 | def compareLinks( self, switches, links, linksJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3236 | """ |
| 3237 | Compare mn and onos links |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 3238 | linksJson: parsed json object from the onos links api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 3239 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3240 | """ |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3241 | # FIXME: this does not look for extra links in ONOS, only checks that |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3242 | # ONOS has what is in MN |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3243 | try: |
| 3244 | onos = linksJson |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 3245 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3246 | mnLinks = [] |
| 3247 | for l in links: |
| 3248 | try: |
| 3249 | node1 = switches[ l[ 'node1' ] ] |
| 3250 | node2 = switches[ l[ 'node2' ] ] |
| 3251 | enabled = True |
| 3252 | for port in node1[ 'ports' ]: |
| 3253 | if port[ 'of_port' ] == l[ 'port1' ]: |
| 3254 | enabled = enabled and port[ 'enabled' ] |
| 3255 | for port in node2[ 'ports' ]: |
| 3256 | if port[ 'of_port' ] == l[ 'port2' ]: |
| 3257 | enabled = enabled and port[ 'enabled' ] |
| 3258 | if enabled: |
| 3259 | mnLinks.append( l ) |
| 3260 | except KeyError: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 3261 | pass |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3262 | if 2 * len( mnLinks ) == len( onos ): |
| 3263 | linkResults = main.TRUE |
| 3264 | else: |
| 3265 | linkResults = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3266 | main.log.error( |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3267 | "Mininet has " + str( len( mnLinks ) ) + |
| 3268 | " bidirectional links and ONOS has " + |
| 3269 | str( len( onos ) ) + " unidirectional links" ) |
| 3270 | |
| 3271 | # iterate through MN links and check if an ONOS link exists in |
| 3272 | # both directions |
| 3273 | for link in mnLinks: |
| 3274 | # TODO: Find a more efficient search method |
| 3275 | node1 = None |
| 3276 | port1 = None |
| 3277 | node2 = None |
| 3278 | port2 = None |
| 3279 | firstDir = main.FALSE |
| 3280 | secondDir = main.FALSE |
| 3281 | for swName, switch in switches.iteritems(): |
| 3282 | if swName == link[ 'node1' ]: |
| 3283 | node1 = switch[ 'dpid' ] |
| 3284 | for port in switch[ 'ports' ]: |
| 3285 | if str( port[ 'of_port' ] ) == str( link[ 'port1' ] ): |
| 3286 | port1 = port[ 'of_port' ] |
| 3287 | if node1 is not None and node2 is not None: |
| 3288 | break |
| 3289 | if swName == link[ 'node2' ]: |
| 3290 | node2 = switch[ 'dpid' ] |
| 3291 | for port in switch[ 'ports' ]: |
| 3292 | if str( port[ 'of_port' ] ) == str( link[ 'port2' ] ): |
| 3293 | port2 = port[ 'of_port' ] |
| 3294 | if node1 is not None and node2 is not None: |
| 3295 | break |
| 3296 | |
| 3297 | for onosLink in onos: |
| 3298 | onosNode1 = onosLink[ 'src' ][ 'device' ].replace( |
| 3299 | ":", '' ).replace( "of", '' ) |
| 3300 | onosNode2 = onosLink[ 'dst' ][ 'device' ].replace( |
| 3301 | ":", '' ).replace( "of", '' ) |
| 3302 | onosPort1 = onosLink[ 'src' ][ 'port' ] |
| 3303 | onosPort2 = onosLink[ 'dst' ][ 'port' ] |
| 3304 | |
| 3305 | # check onos link from node1 to node2 |
| 3306 | if str( onosNode1 ) == str( node1 ) and str( |
| 3307 | onosNode2 ) == str( node2 ): |
| 3308 | if int( onosPort1 ) == int( port1 ) and int( |
| 3309 | onosPort2 ) == int( port2 ): |
| 3310 | firstDir = main.TRUE |
| 3311 | else: |
| 3312 | main.log.warn( |
| 3313 | 'The port numbers do not match for ' + |
| 3314 | str( link ) + |
| 3315 | ' between ONOS and MN. When checking ONOS for ' + |
| 3316 | 'link %s/%s -> %s/%s' % |
| 3317 | ( node1, port1, node2, port2 ) + |
| 3318 | ' ONOS has the values %s/%s -> %s/%s' % |
| 3319 | ( onosNode1, onosPort1, onosNode2, onosPort2 ) ) |
| 3320 | |
| 3321 | # check onos link from node2 to node1 |
| 3322 | elif ( str( onosNode1 ) == str( node2 ) and |
| 3323 | str( onosNode2 ) == str( node1 ) ): |
| 3324 | if ( int( onosPort1 ) == int( port2 ) |
| 3325 | and int( onosPort2 ) == int( port1 ) ): |
| 3326 | secondDir = main.TRUE |
| 3327 | else: |
| 3328 | main.log.warn( |
| 3329 | 'The port numbers do not match for ' + |
| 3330 | str( link ) + |
| 3331 | ' between ONOS and MN. When checking ONOS for ' + |
| 3332 | 'link %s/%s -> %s/%s' % |
| 3333 | ( node1, port1, node2, port2 ) + |
| 3334 | ' ONOS has the values %s/%s -> %s/%s' % |
| 3335 | ( onosNode2, onosPort2, onosNode1, onosPort1 ) ) |
| 3336 | else: # this is not the link you're looking for |
| 3337 | pass |
| 3338 | if not firstDir: |
| 3339 | main.log.error( |
| 3340 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 3341 | ( node1, port1, node2, port2 ) ) |
| 3342 | if not secondDir: |
| 3343 | main.log.error( |
| 3344 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 3345 | ( node2, port2, node1, port1 ) ) |
| 3346 | linkResults = linkResults and firstDir and secondDir |
| 3347 | return linkResults |
| 3348 | except pexpect.EOF: |
| 3349 | main.log.error( self.name + ": EOF exception found" ) |
| 3350 | main.log.error( self.name + ": " + self.handle.before ) |
| 3351 | main.cleanup() |
| 3352 | main.exit() |
| 3353 | except Exception: |
| 3354 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3355 | main.cleanup() |
| 3356 | main.exit() |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 3357 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3358 | def compareHosts( self, hosts, hostsJson ): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 3359 | """ |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3360 | Compare mn and onos Hosts. |
| 3361 | Since Mininet hosts are quiet, ONOS will only know of them when they |
| 3362 | speak. For this reason, we will only check that the hosts in ONOS |
| 3363 | stores are in Mininet, and not vice versa. |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 3364 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3365 | Arguments: |
| 3366 | hostsJson: parsed json object from the onos hosts api |
| 3367 | Returns: |
| 3368 | """ |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 3369 | import json |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3370 | try: |
| 3371 | hostResults = main.TRUE |
| 3372 | for onosHost in hostsJson: |
| 3373 | onosMAC = onosHost[ 'mac' ].lower() |
| 3374 | match = False |
| 3375 | for mnHost, info in hosts.iteritems(): |
| 3376 | for mnIntf in info[ 'interfaces' ]: |
| 3377 | if onosMAC == mnIntf[ 'mac' ].lower(): |
| 3378 | match = True |
| 3379 | for ip in mnIntf[ 'ips' ]: |
| 3380 | if ip in onosHost[ 'ipAddresses' ]: |
| 3381 | pass # all is well |
| 3382 | else: |
| 3383 | # misssing ip |
| 3384 | main.log.error( "ONOS host " + |
| 3385 | onosHost[ 'id' ] + |
| 3386 | " has a different IP(" + |
| 3387 | str( onosHost[ 'ipAddresses' ] ) + |
| 3388 | ") than the Mininet host(" + |
| 3389 | str( ip ) + |
| 3390 | ")." ) |
| 3391 | output = json.dumps( |
| 3392 | onosHost, |
| 3393 | sort_keys=True, |
| 3394 | indent=4, |
| 3395 | separators=( ',', ': ' ) ) |
| 3396 | main.log.info( output ) |
| 3397 | hostResults = main.FALSE |
| 3398 | if not match: |
| 3399 | hostResults = main.FALSE |
| 3400 | main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " + |
| 3401 | "corresponding Mininet host." ) |
| 3402 | output = json.dumps( onosHost, |
| 3403 | sort_keys=True, |
| 3404 | indent=4, |
| 3405 | separators=( ',', ': ' ) ) |
| 3406 | main.log.info( output ) |
| 3407 | return hostResults |
| 3408 | except pexpect.EOF: |
| 3409 | main.log.error(self.name + ": EOF exception found") |
| 3410 | main.log.error(self.name + ": " + self.handle.before) |
| 3411 | main.cleanup() |
| 3412 | main.exit() |
| 3413 | except Exception: |
| 3414 | main.log.exception(self.name + ": Uncaught exception!") |
| 3415 | main.cleanup() |
| 3416 | main.exit() |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 3417 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 3418 | def getHostsOld( self ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3419 | """ |
| 3420 | Returns a list of all hosts |
| 3421 | Don't ask questions just use it""" |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3422 | try: |
| 3423 | self.handle.sendline( "" ) |
| 3424 | self.handle.expect( "mininet>" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 3425 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3426 | self.handle.sendline( "py [ host.name for host in net.hosts ]" ) |
| 3427 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 3428 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3429 | handlePy = self.handle.before |
| 3430 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 3431 | handlePy = handlePy.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 3432 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3433 | self.handle.sendline( "" ) |
| 3434 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 3435 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3436 | hostStr = handlePy.replace( "]", "" ) |
| 3437 | hostStr = hostStr.replace( "'", "" ) |
| 3438 | hostStr = hostStr.replace( "[", "" ) |
| 3439 | hostStr = hostStr.replace( " ", "" ) |
| 3440 | hostList = hostStr.split( "," ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 3441 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3442 | return hostList |
| 3443 | except pexpect.TIMEOUT: |
| 3444 | main.log.error(self.name + ": TIMEOUT exception found") |
| 3445 | main.log.error(self.name + ": " + self.handle.before) |
| 3446 | main.cleanup() |
| 3447 | main.exit() |
| 3448 | except pexpect.EOF: |
| 3449 | main.log.error( self.name + ": EOF exception found" ) |
| 3450 | main.log.error( self.name + ": " + self.handle.before ) |
| 3451 | main.cleanup() |
| 3452 | main.exit() |
| 3453 | except Exception: |
| 3454 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3455 | main.cleanup() |
| 3456 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3457 | |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3458 | def getSwitch( self ): |
| 3459 | """ |
| 3460 | Returns a list of all switches |
| 3461 | Again, don't ask question just use it... |
| 3462 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3463 | try: |
| 3464 | # get host list... |
| 3465 | hostList = self.getHosts() |
| 3466 | # Make host set |
| 3467 | hostSet = set( hostList ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3468 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3469 | # Getting all the nodes in mininet |
| 3470 | self.handle.sendline( "" ) |
| 3471 | self.handle.expect( "mininet>" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3472 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3473 | self.handle.sendline( "py [ node.name for node in net.values() ]" ) |
| 3474 | self.handle.expect( "mininet>" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3475 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3476 | handlePy = self.handle.before |
| 3477 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 3478 | handlePy = handlePy.rstrip() |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3479 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3480 | self.handle.sendline( "" ) |
| 3481 | self.handle.expect( "mininet>" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3482 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3483 | nodesStr = handlePy.replace( "]", "" ) |
| 3484 | nodesStr = nodesStr.replace( "'", "" ) |
| 3485 | nodesStr = nodesStr.replace( "[", "" ) |
| 3486 | nodesStr = nodesStr.replace( " ", "" ) |
| 3487 | nodesList = nodesStr.split( "," ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3488 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3489 | nodesSet = set( nodesList ) |
| 3490 | # discarding default controller(s) node |
| 3491 | nodesSet.discard( 'c0' ) |
| 3492 | nodesSet.discard( 'c1' ) |
| 3493 | nodesSet.discard( 'c2' ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3494 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3495 | switchSet = nodesSet - hostSet |
| 3496 | switchList = list( switchSet ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3497 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3498 | return switchList |
| 3499 | except pexpect.TIMEOUT: |
| 3500 | main.log.error(self.name + ": TIMEOUT exception found") |
| 3501 | main.log.error(self.name + ": " + self.handle.before) |
| 3502 | main.cleanup() |
| 3503 | main.exit() |
| 3504 | except pexpect.EOF: |
| 3505 | main.log.error( self.name + ": EOF exception found" ) |
| 3506 | main.log.error( self.name + ": " + self.handle.before ) |
| 3507 | main.cleanup() |
| 3508 | main.exit() |
| 3509 | except Exception: |
| 3510 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3511 | main.cleanup() |
| 3512 | main.exit() |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 3513 | |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 3514 | def getGraphDict( self, timeout=60, useId=True, includeHost=False ): |
| 3515 | """ |
| 3516 | Return a dictionary which describes the latest Mininet topology data as a |
| 3517 | graph. |
| 3518 | An example of the dictionary: |
| 3519 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 3520 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 3521 | Each vertex should at least have an 'edges' attribute which describes the |
| 3522 | adjacency information. The value of 'edges' attribute is also represented by |
| 3523 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 3524 | list of attributes. |
| 3525 | An example of the edges dictionary: |
| 3526 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 3527 | vertex3: { 'port': ..., 'weight': ... } } |
| 3528 | If useId == True, dpid/mac will be used instead of names to identify |
| 3529 | vertices, which is helpful when e.g. comparing Mininet topology with ONOS |
| 3530 | topology. |
| 3531 | If includeHost == True, all hosts (and host-switch links) will be included |
| 3532 | in topology data. |
| 3533 | Note that link or switch that are brought down by 'link x x down' or 'switch |
| 3534 | x down' commands still show in the output of Mininet CLI commands such as |
| 3535 | 'links', 'dump', etc. Thus, to ensure the correctness of this function, it is |
| 3536 | recommended to use delLink() or delSwitch functions to simulate link/switch |
| 3537 | down, and addLink() or addSwitch to add them back. |
| 3538 | """ |
| 3539 | graphDict = {} |
| 3540 | try: |
| 3541 | links = self.getLinks( timeout=timeout ) |
| 3542 | portDict = {} |
| 3543 | if useId: |
| 3544 | switches = self.getSwitches() |
| 3545 | if includeHost: |
| 3546 | hosts = self.getHosts() |
| 3547 | for link in links: |
| 3548 | # FIXME: support 'includeHost' argument |
| 3549 | if link[ 'node1' ].startswith( 'h' ) or link[ 'node2' ].startswith( 'h' ): |
| 3550 | continue |
| 3551 | nodeName1 = link[ 'node1' ] |
| 3552 | nodeName2 = link[ 'node2' ] |
| 3553 | port1 = link[ 'port1' ] |
| 3554 | port2 = link[ 'port2' ] |
| 3555 | # Loop for two nodes |
| 3556 | for i in range( 2 ): |
| 3557 | # Get port index from OVS |
| 3558 | # The index extracted from port name may be inconsistent with ONOS |
| 3559 | portIndex = -1 |
| 3560 | if not nodeName1 in portDict.keys(): |
| 3561 | portList = self.getOVSPorts( nodeName1 ) |
| 3562 | if len( portList ) == 0: |
| 3563 | main.log.warn( self.name + ": No port found on switch " + nodeName1 ) |
| 3564 | return None |
| 3565 | portDict[ nodeName1 ] = portList |
| 3566 | for port in portDict[ nodeName1 ]: |
| 3567 | if port[ 'port' ] == port1: |
| 3568 | portIndex = port[ 'index' ] |
| 3569 | break |
| 3570 | if portIndex == -1: |
| 3571 | main.log.warn( self.name + ": Cannot find port index for interface {}-eth{}".format( nodeName1, port1 ) ) |
| 3572 | return None |
| 3573 | if useId: |
| 3574 | node1 = 'of:' + str( switches[ nodeName1 ][ 'dpid' ] ) |
| 3575 | node2 = 'of:' + str( switches[ nodeName2 ][ 'dpid' ] ) |
| 3576 | else: |
| 3577 | node1 = nodeName1 |
| 3578 | node2 = nodeName2 |
| 3579 | if not node1 in graphDict.keys(): |
| 3580 | if useId: |
| 3581 | graphDict[ node1 ] = { 'edges':{}, |
| 3582 | 'dpid':switches[ nodeName1 ][ 'dpid' ], |
| 3583 | 'name':nodeName1, |
| 3584 | 'ports':switches[ nodeName1 ][ 'ports' ], |
| 3585 | 'swClass':switches[ nodeName1 ][ 'swClass' ], |
| 3586 | 'pid':switches[ nodeName1 ][ 'pid' ], |
| 3587 | 'options':switches[ nodeName1 ][ 'options' ] } |
| 3588 | else: |
| 3589 | graphDict[ node1 ] = { 'edges':{} } |
| 3590 | else: |
| 3591 | # Assert node2 is not connected to any current links of node1 |
| 3592 | assert node2 not in graphDict[ node1 ][ 'edges' ].keys() |
| 3593 | graphDict[ node1 ][ 'edges' ][ node2 ] = { 'port':portIndex } |
| 3594 | # Swap two nodes/ports |
| 3595 | nodeName1, nodeName2 = nodeName2, nodeName1 |
| 3596 | port1, port2 = port2, port1 |
| 3597 | return graphDict |
| 3598 | except KeyError: |
| 3599 | main.log.exception( self.name + ": KeyError exception found" ) |
| 3600 | return None |
| 3601 | except AssertionError: |
| 3602 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 3603 | return None |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3604 | except pexpect.EOF: |
| 3605 | main.log.error( self.name + ": EOF exception found" ) |
| 3606 | main.log.error( self.name + ": " + self.handle.before ) |
| 3607 | main.cleanup() |
| 3608 | main.exit() |
You Wang | db8cd0a | 2016-05-26 15:19:45 -0700 | [diff] [blame] | 3609 | except Exception: |
| 3610 | main.log.exception( self.name + ": Uncaught exception" ) |
| 3611 | return None |
| 3612 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3613 | def update( self ): |
| 3614 | """ |
| 3615 | updates the port address and status information for |
| 3616 | each port in mn""" |
| 3617 | # TODO: Add error checking. currently the mininet command has no output |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 3618 | main.log.info( "Updating MN port information" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 3619 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3620 | self.handle.sendline( "" ) |
| 3621 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 3622 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3623 | self.handle.sendline( "update" ) |
| 3624 | self.handle.expect( "update" ) |
| 3625 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 3626 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3627 | self.handle.sendline( "" ) |
| 3628 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 3629 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 3630 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3631 | except pexpect.TIMEOUT: |
| 3632 | main.log.error(self.name + ": TIMEOUT exception found") |
| 3633 | main.log.error(self.name + ": " + self.handle.before) |
| 3634 | main.cleanup() |
| 3635 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 3636 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 3637 | main.log.error( self.name + ": EOF exception found" ) |
| 3638 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 3639 | main.cleanup() |
| 3640 | main.exit() |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3641 | except Exception: |
| 3642 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3643 | main.cleanup() |
| 3644 | main.exit() |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 3645 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3646 | def assignVLAN( self, host, intf, vlan ): |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3647 | """ |
| 3648 | Add vlan tag to a host. |
| 3649 | Dependencies: |
| 3650 | This class depends on the "vlan" package |
| 3651 | $ sudo apt-get install vlan |
| 3652 | Configuration: |
| 3653 | Load the 8021q module into the kernel |
| 3654 | $sudo modprobe 8021q |
| 3655 | |
| 3656 | To make this setup permanent: |
| 3657 | $ sudo su -c 'echo "8021q" >> /etc/modules' |
| 3658 | """ |
| 3659 | if self.handle: |
| 3660 | try: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3661 | # get the ip address of the host |
| 3662 | main.log.info( "Get the ip address of the host" ) |
| 3663 | ipaddr = self.getIPAddress( host ) |
| 3664 | print repr( ipaddr ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3665 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3666 | # remove IP from interface intf |
| 3667 | # Ex: h1 ifconfig h1-eth0 inet 0 |
| 3668 | main.log.info( "Remove IP from interface " ) |
| 3669 | cmd2 = host + " ifconfig " + intf + " " + " inet 0 " |
| 3670 | self.handle.sendline( cmd2 ) |
| 3671 | self.handle.expect( "mininet>" ) |
| 3672 | response = self.handle.before |
| 3673 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3674 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3675 | # create VLAN interface |
| 3676 | # Ex: h1 vconfig add h1-eth0 100 |
| 3677 | main.log.info( "Create Vlan" ) |
| 3678 | cmd3 = host + " vconfig add " + intf + " " + vlan |
| 3679 | self.handle.sendline( cmd3 ) |
| 3680 | self.handle.expect( "mininet>" ) |
| 3681 | response = self.handle.before |
| 3682 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3683 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 3684 | # assign the host's IP to the VLAN interface |
| 3685 | # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1 |
| 3686 | main.log.info( "Assign the host IP to the vlan interface" ) |
| 3687 | vintf = intf + "." + vlan |
| 3688 | cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr |
| 3689 | self.handle.sendline( cmd4 ) |
| 3690 | self.handle.expect( "mininet>" ) |
| 3691 | response = self.handle.before |
| 3692 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3693 | |
| 3694 | return main.TRUE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3695 | except pexpect.TIMEOUT: |
| 3696 | main.log.error(self.name + ": TIMEOUT exception found") |
| 3697 | main.log.error(self.name + ": " + self.handle.before) |
| 3698 | main.cleanup() |
| 3699 | main.exit() |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3700 | except pexpect.EOF: |
| 3701 | main.log.error( self.name + ": EOF exception found" ) |
| 3702 | main.log.error( self.name + ": " + self.handle.before ) |
| 3703 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3704 | except Exception: |
| 3705 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3706 | return main.FALSE |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 3707 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3708 | def createHostComponent( self, name ): |
| 3709 | """ |
| 3710 | Creates a new mininet cli component with the same parameters as self. |
| 3711 | This new component is intended to be used to login to the hosts created |
| 3712 | by mininet. |
| 3713 | |
| 3714 | Arguments: |
| 3715 | name - The string of the name of this component. The new component |
| 3716 | will be assigned to main.<name> . |
| 3717 | In addition, main.<name>.name = str( name ) |
| 3718 | """ |
| 3719 | try: |
| 3720 | # look to see if this component already exists |
| 3721 | getattr( main, name ) |
| 3722 | except AttributeError: |
| 3723 | # namespace is clear, creating component |
| 3724 | main.componentDictionary[name] = main.componentDictionary[self.name].copy() |
| 3725 | main.componentDictionary[name]['connect_order'] = str( int( main.componentDictionary[name]['connect_order'] ) + 1 ) |
| 3726 | main.componentInit( name ) |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3727 | except pexpect.EOF: |
| 3728 | main.log.error( self.name + ": EOF exception found" ) |
| 3729 | main.log.error( self.name + ": " + self.handle.before ) |
| 3730 | main.cleanup() |
| 3731 | main.exit() |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3732 | except Exception: |
| 3733 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3734 | main.cleanup() |
| 3735 | main.exit() |
| 3736 | else: |
| 3737 | # namespace is not clear! |
| 3738 | main.log.error( name + " component already exists!" ) |
| 3739 | # FIXME: Should we exit here? |
| 3740 | main.cleanup() |
| 3741 | main.exit() |
| 3742 | |
| 3743 | def removeHostComponent( self, name ): |
| 3744 | """ |
| 3745 | Remove host component |
| 3746 | Arguments: |
| 3747 | name - The string of the name of the component to delete. |
| 3748 | """ |
| 3749 | try: |
| 3750 | # Get host component |
| 3751 | component = getattr( main, name ) |
| 3752 | except AttributeError: |
| 3753 | main.log.error( "Component " + name + " does not exist." ) |
| 3754 | return |
| 3755 | try: |
| 3756 | # Disconnect from component |
| 3757 | component.disconnect() |
| 3758 | # Delete component |
| 3759 | delattr( main, name ) |
| 3760 | # Delete component from ComponentDictionary |
| 3761 | del( main.componentDictionary[name] ) |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 3762 | except pexpect.EOF: |
| 3763 | main.log.error( self.name + ": EOF exception found" ) |
| 3764 | main.log.error( self.name + ": " + self.handle.before ) |
| 3765 | main.cleanup() |
| 3766 | main.exit() |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3767 | except Exception: |
| 3768 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3769 | main.cleanup() |
| 3770 | main.exit() |
| 3771 | |
| 3772 | def startHostCli( self, host=None ): |
| 3773 | """ |
| 3774 | Use the mininet m utility to connect to the host's cli |
| 3775 | """ |
| 3776 | # These are fields that can be used by scapy packets. Initialized to None |
| 3777 | self.hostIp = None |
| 3778 | self.hostMac = None |
| 3779 | try: |
| 3780 | if not host: |
| 3781 | host = self.name |
| 3782 | self.handle.sendline( self.home + "/util/m " + host ) |
| 3783 | self.handle.expect( self.hostPrompt ) |
| 3784 | return main.TRUE |
| 3785 | except pexpect.TIMEOUT: |
| 3786 | main.log.exception( self.name + ": Command timed out" ) |
| 3787 | return main.FALSE |
| 3788 | except pexpect.EOF: |
| 3789 | main.log.exception( self.name + ": connection closed." ) |
| 3790 | main.cleanup() |
| 3791 | main.exit() |
| 3792 | except Exception: |
| 3793 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3794 | main.cleanup() |
| 3795 | main.exit() |
| 3796 | |
YPZhang | 801d46d | 2016-08-08 13:26:28 -0700 | [diff] [blame] | 3797 | def changeInterfaceStatus( self, devicename, intf, status ): |
| 3798 | ''' |
| 3799 | |
| 3800 | Args: |
| 3801 | devicename: switch name |
| 3802 | intf: port name on switch |
| 3803 | status: up or down |
| 3804 | |
| 3805 | Returns: boolean to show success change status |
| 3806 | |
| 3807 | ''' |
| 3808 | if status == "down" or status == "up": |
| 3809 | try: |
| 3810 | cmd = devicename + " ifconfig " + intf + " " + status |
| 3811 | self.handle.sendline( cmd ) |
| 3812 | self.handle.expect("mininet>") |
| 3813 | return main.TRUE |
| 3814 | except pexpect.TIMEOUT: |
| 3815 | main.log.exception(self.name + ": Command timed out") |
| 3816 | return main.FALSE |
| 3817 | except pexpect.EOF: |
| 3818 | main.log.exception(self.name + ": connection closed.") |
| 3819 | main.cleanup() |
| 3820 | main.exit() |
| 3821 | except TypeError: |
| 3822 | main.log.exception(self.name + ": TypeError") |
| 3823 | main.cleanup() |
| 3824 | main.exit() |
| 3825 | except Exception: |
| 3826 | main.log.exception(self.name + ": Uncaught exception!") |
| 3827 | main.cleanup() |
| 3828 | main.exit() |
| 3829 | else: |
| 3830 | main.log.warn("Interface status should be up or down!") |
| 3831 | return main.FALSE |
| 3832 | |
| 3833 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3834 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3835 | if __name__ != "__main__": |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 3836 | sys.modules[ __name__ ] = MininetCliDriver() |