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 |
| 4 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 5 | author: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 6 | |
| 7 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 12 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 17 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 22 | MininetCliDriver is the basic driver which will handle the Mininet functions |
| 23 | |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 24 | Some functions rely on STS module. To install this, |
| 25 | git clone https://github.com/jhall11/sts.git |
| 26 | |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 27 | Some functions rely on a modified version of Mininet. These functions |
| 28 | should all be noted in the comments. To get this MN version run these commands |
| 29 | from within your Mininet folder: |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 30 | git remote add jhall11 https://github.com/jhall11/mininet.git |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 31 | git fetch jhall11 |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 32 | git checkout -b dynamic_topo remotes/jhall11/dynamic_topo |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 33 | git pull |
| 34 | |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 35 | |
| 36 | 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] | 37 | changed when switching branches.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 38 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | import re |
| 40 | import sys |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 41 | sys.path.append( "../" ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 42 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | from drivers.common.cli.emulatordriver import Emulator |
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 | |
| 46 | class MininetCliDriver( Emulator ): |
| 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 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 55 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | self.flag = 0 |
| 57 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 58 | def connect( self, **connectargs ): |
| 59 | """ |
| 60 | Here the main is the TestON instance after creating |
| 61 | all the log handles.""" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 62 | try: |
| 63 | for key in connectargs: |
| 64 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 65 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 66 | self.name = self.options[ 'name' ] |
| 67 | self.handle = super( |
| 68 | MininetCliDriver, |
| 69 | self ).connect( |
| 70 | user_name=self.user_name, |
| 71 | ip_address=self.ip_address, |
| 72 | port=None, |
| 73 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 74 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 75 | if self.handle: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 76 | main.log.info( "Connection successful to the host " + |
| 77 | self.user_name + |
| 78 | "@" + |
| 79 | self.ip_address ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 80 | return main.TRUE |
| 81 | else: |
| 82 | main.log.error( "Connection failed to the host " + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 83 | self.user_name + |
| 84 | "@" + |
| 85 | self.ip_address ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 86 | main.log.error( "Failed to connect to the Mininet CLI" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 87 | return main.FALSE |
| 88 | except pexpect.EOF: |
| 89 | main.log.error( self.name + ": EOF exception found" ) |
| 90 | main.log.error( self.name + ": " + self.handle.before ) |
| 91 | main.cleanup() |
| 92 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 93 | except Exception: |
| 94 | main.log.exception( self.name + ": Uncaught exception!" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 95 | main.cleanup() |
| 96 | main.exit() |
| 97 | |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 98 | def startNet( self, topoFile='', args='', timeout=120 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 99 | """ |
| 100 | Starts Mininet accepts a topology(.py) file and/or an optional |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 101 | argument ,to start the mininet, as a parameter. |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 102 | Returns main.TRUE if the mininet starts successfully and |
| 103 | main.FALSE otherwise |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 104 | """ |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 105 | if self.handle: |
| 106 | main.log.info( |
| 107 | self.name + |
| 108 | ": Clearing any residual state or processes" ) |
| 109 | self.handle.sendline( "sudo mn -c" ) |
| 110 | i = self.handle.expect( [ 'password\sfor\s', |
| 111 | 'Cleanup\scomplete', |
| 112 | pexpect.EOF, |
| 113 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 114 | timeout ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 115 | if i == 0: |
| 116 | main.log.info( self.name + ": Sending sudo password" ) |
| 117 | self.handle.sendline( self.pwd ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 118 | i = self.handle.expect( [ '%s:' % self.user, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 119 | '\$', |
| 120 | pexpect.EOF, |
| 121 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 122 | timeout ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 123 | if i == 1: |
| 124 | main.log.info( self.name + ": Clean" ) |
| 125 | elif i == 2: |
| 126 | main.log.error( self.name + ": Connection terminated" ) |
| 127 | elif i == 3: # timeout |
| 128 | main.log.error( |
| 129 | self.name + |
| 130 | ": Something while cleaning MN took too long... " ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 131 | if topoFile == '' and args == '': |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 132 | main.log.info( self.name + ": building fresh mininet" ) |
| 133 | # for reactive/PARP enabled tests |
| 134 | cmdString = "sudo mn " + self.options[ 'arg1' ] +\ |
| 135 | " " + self.options[ 'arg2' ] +\ |
| 136 | " --mac --controller " +\ |
| 137 | self.options[ 'controller' ] + " " +\ |
| 138 | self.options[ 'arg3' ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 139 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 140 | argList = self.options[ 'arg1' ].split( "," ) |
| 141 | global topoArgList |
| 142 | topoArgList = argList[ 0 ].split( " " ) |
| 143 | argList = map( int, argList[ 1: ] ) |
| 144 | topoArgList = topoArgList[ 1: ] + argList |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 145 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 146 | self.handle.sendline( cmdString ) |
| 147 | self.handle.expect( [ "sudo mn", |
| 148 | pexpect.EOF, |
| 149 | pexpect.TIMEOUT ] ) |
| 150 | while True: |
| 151 | i = self.handle.expect( [ 'mininet>', |
| 152 | '\*\*\*', |
| 153 | 'Exception', |
| 154 | pexpect.EOF, |
| 155 | pexpect.TIMEOUT ], |
| 156 | timeout ) |
| 157 | if i == 0: |
| 158 | main.log.info( self.name + ": mininet built" ) |
| 159 | return main.TRUE |
| 160 | if i == 1: |
| 161 | self.handle.expect( |
| 162 | [ "\n", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 163 | main.log.info( self.handle.before ) |
| 164 | elif i == 2: |
| 165 | main.log.error( |
| 166 | self.name + |
| 167 | ": Launching mininet failed..." ) |
| 168 | return main.FALSE |
| 169 | elif i == 3: |
| 170 | main.log.error( self.name + ": Connection timeout" ) |
| 171 | return main.FALSE |
| 172 | elif i == 4: # timeout |
| 173 | main.log.error( |
| 174 | self.name + |
| 175 | ": Something took too long... " ) |
| 176 | return main.FALSE |
| 177 | return main.TRUE |
| 178 | else: |
| 179 | main.log.info( "Starting topo file " + topoFile ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 180 | if args is None: |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 181 | args = '' |
| 182 | else: |
| 183 | main.log.info( "args = " + args) |
| 184 | self.handle.sendline( 'sudo ' + topoFile + ' ' + args) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 185 | i = self.handle.expect( [ 'mininet>', |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 186 | pexpect.EOF, |
| 187 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 188 | timeout) |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 189 | if i == 0: |
| 190 | main.log.info(self.name + ": Network started") |
| 191 | return main.TRUE |
kelvin-onlab | ec228b8 | 2015-02-09 15:45:55 -0800 | [diff] [blame] | 192 | elif i == 1: |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 193 | main.log.error( self.name + ": Connection timeout" ) |
| 194 | return main.FALSE |
kelvin-onlab | ec228b8 | 2015-02-09 15:45:55 -0800 | [diff] [blame] | 195 | elif i == 2: # timeout |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 196 | main.log.error( |
| 197 | self.name + |
| 198 | ": Something took too long... " ) |
| 199 | return main.FALSE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 200 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 201 | else: # if no handle |
| 202 | main.log.error( |
| 203 | self.name + |
| 204 | ": Connection failed to the host " + |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 205 | self.user_name + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 206 | "@" + |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 207 | self.ip_address ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 208 | main.log.error( self.name + ": Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 209 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 210 | |
kelvin-onlab | fccaafa | 2015-01-20 13:50:44 -0800 | [diff] [blame] | 211 | def numSwitchesNlinks( self, topoType, depth, fanout ): |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 212 | if topoType == 'tree': |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 213 | # In tree topology, if fanout arg is not given, by default it is 2 |
| 214 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 215 | fanout = 2 |
| 216 | k = 0 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 217 | count = 0 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 218 | while( k <= depth - 1 ): |
| 219 | count = count + pow( fanout, k ) |
| 220 | k = k + 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 221 | numSwitches = count |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 222 | while( k <= depth - 2 ): |
| 223 | # depth-2 gives you only core links and not considering |
| 224 | # edge links as seen by ONOS. If all the links including |
| 225 | # edge links are required, do depth-1 |
| 226 | count = count + pow( fanout, k ) |
| 227 | k = k + 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 228 | numLinks = count * fanout |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 229 | # print "num_switches for %s(%d,%d) = %d and links=%d" %( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 230 | # topoType,depth,fanout,numSwitches,numLinks ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 231 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 232 | elif topoType == 'linear': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 233 | # In linear topology, if fanout or numHostsPerSw is not given, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 234 | # by default it is 1 |
| 235 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 236 | fanout = 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 237 | numSwitches = depth |
| 238 | numHostsPerSw = fanout |
| 239 | totalNumHosts = numSwitches * numHostsPerSw |
| 240 | numLinks = totalNumHosts + ( numSwitches - 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 241 | print "num_switches for %s(%d,%d) = %d and links=%d" %\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 242 | ( topoType, depth, fanout, numSwitches, numLinks ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 243 | topoDict = { "num_switches": int( numSwitches ), |
| 244 | "num_corelinks": int( numLinks ) } |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 245 | return topoDict |
| 246 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 247 | def calculateSwAndLinks( self ): |
| 248 | topoDict = self.numSwitchesN_links( *topoArgList ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 249 | return topoDict |
| 250 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 251 | def pingall( self, timeout=300 ): |
| 252 | """ |
| 253 | Verifies the reachability of the hosts using pingall command. |
| 254 | Optional parameter timeout allows you to specify how long to |
| 255 | wait for pingall to complete |
| 256 | Returns: |
| 257 | main.TRUE if pingall completes with no pings dropped |
| 258 | otherwise main.FALSE""" |
| 259 | if self.handle: |
| 260 | main.log.info( |
| 261 | self.name + |
| 262 | ": Checking reachabilty to the hosts using pingall" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 263 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 264 | response = self.execute( |
| 265 | cmd="pingall", |
| 266 | prompt="mininet>", |
| 267 | timeout=int( timeout ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 268 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 269 | main.log.error( self.name + ": EOF exception found" ) |
| 270 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 271 | main.cleanup() |
| 272 | main.exit() |
| 273 | except pexpect.TIMEOUT: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 274 | # We may not want to kill the test if pexpect times out |
| 275 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 276 | main.log.error( self.name + |
| 277 | ": " + |
| 278 | str( self.handle.before ) ) |
| 279 | # NOTE: mininet's pingall rounds, so we will check the number of |
| 280 | # passed and number of failed |
| 281 | pattern = "Results\:\s0\%\sdropped\s\(" +\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 282 | "(?P<passed>[\d]+)/(?P=passed)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 283 | if re.search( pattern, response ): |
| 284 | main.log.info( self.name + ": All hosts are reachable" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 285 | return main.TRUE |
| 286 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 287 | main.log.error( self.name + ": Unable to reach all the hosts" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 288 | main.log.info( "Pingall output: " + str( response ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 289 | # NOTE: Send ctrl-c to make sure pingall is done |
| 290 | self.handle.send( "\x03" ) |
| 291 | self.handle.expect( "Interrupt" ) |
| 292 | self.handle.expect( "mininet>" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 293 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 294 | else: |
| 295 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 296 | main.cleanup() |
| 297 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 298 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 299 | def fpingHost( self, **pingParams ): |
| 300 | """ |
| 301 | Uses the fping package for faster pinging... |
| 302 | *requires fping to be installed on machine running mininet""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 303 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 304 | command = args[ "SRC" ] + \ |
| 305 | " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ] |
| 306 | self.handle.sendline( command ) |
| 307 | self.handle.expect( |
| 308 | [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] ) |
| 309 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 310 | response = self.handle.before |
| 311 | if re.search( ":\s-", response ): |
| 312 | main.log.info( self.name + ": Ping fail" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 313 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 314 | elif re.search( ":\s\d{1,2}\.\d\d", response ): |
| 315 | main.log.info( self.name + ": Ping good!" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 316 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 317 | main.log.info( self.name + ": Install fping on mininet machine... " ) |
| 318 | main.log.info( self.name + ": \n---\n" + response ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 319 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 320 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 321 | def pingHost( self, **pingParams ): |
| 322 | """ |
| 323 | Ping from one mininet host to another |
| 324 | Currently the only supported Params: SRC and TARGET""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 325 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 326 | command = args[ "SRC" ] + " ping " + \ |
| 327 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 328 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 329 | main.log.info( "Sending: " + command ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 330 | self.handle.sendline( command ) |
| 331 | i = self.handle.expect( [ command, pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 332 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 333 | main.log.error( |
| 334 | self.name + |
| 335 | ": timeout when waiting for response from mininet" ) |
| 336 | main.log.error( "response: " + str( self.handle.before ) ) |
| 337 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 338 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 339 | main.log.error( |
| 340 | self.name + |
| 341 | ": timeout when waiting for response from mininet" ) |
| 342 | main.log.error( "response: " + str( self.handle.before ) ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 343 | response = self.handle.before |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 344 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 345 | main.log.error( self.name + ": EOF exception found" ) |
| 346 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 347 | main.cleanup() |
| 348 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 349 | main.log.info( self.name + ": Ping Response: " + response ) |
| 350 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 351 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 352 | main.lastResult = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 353 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 354 | else: |
| 355 | main.log.error( |
| 356 | self.name + |
| 357 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 358 | main.lastResult = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 359 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 360 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 361 | def checkIP( self, host ): |
| 362 | """ |
| 363 | Verifies the host's ip configured or not.""" |
| 364 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 365 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 366 | response = self.execute( |
| 367 | cmd=host + |
| 368 | " ifconfig", |
| 369 | prompt="mininet>", |
| 370 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 371 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 372 | main.log.error( self.name + ": EOF exception found" ) |
| 373 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 374 | main.cleanup() |
| 375 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 376 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 377 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 378 | "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\ |
| 379 | "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\ |
| 380 | "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\ |
| 381 | "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\ |
| 382 | "[0-9]|25[0-5]|[0-9]{1,2})" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 383 | # pattern = "inet addr:10.0.0.6" |
| 384 | if re.search( pattern, response ): |
| 385 | main.log.info( self.name + ": Host Ip configured properly" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 386 | return main.TRUE |
| 387 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 388 | main.log.error( self.name + ": Host IP not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 389 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 390 | else: |
| 391 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 392 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 393 | def verifySSH( self, **connectargs ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 394 | # FIXME: Who uses this and what is the purpose? seems very specific |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 395 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 396 | response = self.execute( |
| 397 | cmd="h1 /usr/sbin/sshd -D&", |
| 398 | prompt="mininet>", |
| 399 | timeout=10 ) |
| 400 | response = self.execute( |
| 401 | cmd="h4 /usr/sbin/sshd -D&", |
| 402 | prompt="mininet>", |
| 403 | timeout=10 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 404 | for key in connectargs: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 405 | vars( self )[ key ] = connectargs[ key ] |
| 406 | response = self.execute( |
| 407 | cmd="xterm h1 h4 ", |
| 408 | prompt="mininet>", |
| 409 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 410 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 411 | main.log.error( self.name + ": EOF exception found" ) |
| 412 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 413 | main.cleanup() |
| 414 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 415 | import time |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 416 | time.sleep( 20 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 417 | if self.flag == 0: |
| 418 | self.flag = 1 |
| 419 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 420 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 421 | return main.TRUE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 422 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 423 | def moveHost( self, host, oldSw, newSw, ): |
| 424 | """ |
| 425 | Moves a host from one switch to another on the fly |
| 426 | Note: The intf between host and oldSw when detached |
| 427 | using detach(), will still show up in the 'net' |
| 428 | cmd, because switch.detach() doesn't affect switch.intfs[] |
| 429 | (which is correct behavior since the interfaces |
| 430 | haven't moved). |
| 431 | """ |
| 432 | if self.handle: |
| 433 | try: |
| 434 | # Bring link between oldSw-host down |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 435 | cmd = "py net.configLinkStatus('" + oldSw + "'," + "'"+ host +\ |
| 436 | "'," + "'down')" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 437 | print "cmd1= ", cmd |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 438 | response = self.execute( cmd=cmd, |
| 439 | prompt="mininet>", |
| 440 | timeout=10 ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 441 | |
| 442 | # Determine hostintf and Oldswitchintf |
| 443 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 444 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 445 | print "cmd2= ", cmd |
| 446 | self.handle.sendline( cmd ) |
| 447 | self.handle.expect( "mininet>" ) |
| 448 | |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 449 | # Determine ip and mac address of the host-oldSw interface |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 450 | cmd = "px ipaddr = hintf.IP()" |
| 451 | print "cmd3= ", cmd |
| 452 | self.handle.sendline( cmd ) |
| 453 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 454 | |
| 455 | cmd = "px macaddr = hintf.MAC()" |
| 456 | print "cmd3= ", cmd |
| 457 | self.handle.sendline( cmd ) |
| 458 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 459 | |
| 460 | # Detach interface between oldSw-host |
| 461 | cmd = "px " + oldSw + ".detach( sintf )" |
| 462 | print "cmd4= ", cmd |
| 463 | self.handle.sendline( cmd ) |
| 464 | self.handle.expect( "mininet>" ) |
| 465 | |
| 466 | # Add link between host-newSw |
| 467 | cmd = "py net.addLink(" + host + "," + newSw + ")" |
| 468 | print "cmd5= ", cmd |
| 469 | self.handle.sendline( cmd ) |
| 470 | self.handle.expect( "mininet>" ) |
| 471 | |
| 472 | # Determine hostintf and Newswitchintf |
| 473 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 474 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 475 | print "cmd6= ", cmd |
| 476 | self.handle.sendline( cmd ) |
| 477 | self.handle.expect( "mininet>" ) |
| 478 | |
| 479 | # Attach interface between newSw-host |
| 480 | cmd = "px " + newSw + ".attach( sintf )" |
| 481 | print "cmd3= ", cmd |
| 482 | self.handle.sendline( cmd ) |
| 483 | self.handle.expect( "mininet>" ) |
| 484 | |
| 485 | # Set ipaddress of the host-newSw interface |
| 486 | cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)" |
| 487 | print "cmd7 = ", cmd |
| 488 | self.handle.sendline( cmd ) |
| 489 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 490 | |
| 491 | # Set macaddress of the host-newSw interface |
| 492 | cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)" |
| 493 | print "cmd8 = ", cmd |
| 494 | self.handle.sendline( cmd ) |
| 495 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 496 | |
| 497 | cmd = "net" |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 498 | print "cmd9 = ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 499 | self.handle.sendline( cmd ) |
| 500 | self.handle.expect( "mininet>" ) |
| 501 | print "output = ", self.handle.before |
| 502 | |
| 503 | # Determine ipaddress of the host-newSw interface |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 504 | cmd = host + " ifconfig" |
| 505 | print "cmd10= ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 506 | self.handle.sendline( cmd ) |
| 507 | self.handle.expect( "mininet>" ) |
| 508 | print "ifconfig o/p = ", self.handle.before |
| 509 | |
| 510 | return main.TRUE |
| 511 | except pexpect.EOF: |
| 512 | main.log.error( self.name + ": EOF exception found" ) |
| 513 | main.log.error( self.name + ": " + self.handle.before ) |
| 514 | return main.FALSE |
| 515 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 516 | def changeIP( self, host, intf, newIP, newNetmask ): |
| 517 | """ |
| 518 | Changes the ip address of a host on the fly |
| 519 | 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] | 520 | if self.handle: |
| 521 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 522 | cmd = host + " ifconfig " + intf + " " + \ |
| 523 | newIP + " " + 'netmask' + " " + newNetmask |
| 524 | self.handle.sendline( cmd ) |
| 525 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 526 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 527 | main.log.info( "response = " + response ) |
| 528 | main.log.info( |
| 529 | "Ip of host " + |
| 530 | host + |
| 531 | " changed to new IP " + |
| 532 | newIP ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 533 | return main.TRUE |
| 534 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 535 | main.log.error( self.name + ": EOF exception found" ) |
| 536 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 537 | return main.FALSE |
| 538 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 539 | def changeDefaultGateway( self, host, newGW ): |
| 540 | """ |
| 541 | Changes the default gateway of a host |
| 542 | Ex: h1 route add default gw 10.0.1.2""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 543 | if self.handle: |
| 544 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 545 | cmd = host + " route add default gw " + newGW |
| 546 | self.handle.sendline( cmd ) |
| 547 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 548 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 549 | main.log.info( "response = " + response ) |
| 550 | main.log.info( |
| 551 | "Default gateway of host " + |
| 552 | host + |
| 553 | " changed to " + |
| 554 | newGW ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 555 | return main.TRUE |
| 556 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 557 | main.log.error( self.name + ": EOF exception found" ) |
| 558 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 559 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 560 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 561 | def addStaticMACAddress( self, host, GW, macaddr ): |
| 562 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 563 | Changes the mac address of a gateway host""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 564 | if self.handle: |
| 565 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 566 | # h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
| 567 | cmd = host + " arp -s " + GW + " " + macaddr |
| 568 | self.handle.sendline( cmd ) |
| 569 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 570 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 571 | main.log.info( "response = " + response ) |
| 572 | main.log.info( |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 573 | "Mac address of gateway " + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 574 | GW + |
| 575 | " changed to " + |
| 576 | macaddr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 577 | return main.TRUE |
| 578 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 579 | main.log.error( self.name + ": EOF exception found" ) |
| 580 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 581 | return main.FALSE |
| 582 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 583 | def verifyStaticGWandMAC( self, host ): |
| 584 | """ |
| 585 | Verify if the static gateway and mac address assignment""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 586 | if self.handle: |
| 587 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 588 | # h1 arp -an |
| 589 | cmd = host + " arp -an " |
| 590 | self.handle.sendline( cmd ) |
| 591 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 592 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 593 | main.log.info( host + " arp -an = " + response ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 594 | return main.TRUE |
| 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 ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 598 | return main.FALSE |
| 599 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 600 | def getMacAddress( self, host ): |
| 601 | """ |
| 602 | Verifies the host's ip configured or not.""" |
| 603 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 604 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 605 | response = self.execute( |
| 606 | cmd=host + |
| 607 | " ifconfig", |
| 608 | prompt="mininet>", |
| 609 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 610 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 611 | main.log.error( self.name + ": EOF exception found" ) |
| 612 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 613 | main.cleanup() |
| 614 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 615 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 616 | 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] | 617 | macAddressSearch = re.search( pattern, response, re.I ) |
| 618 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 619 | main.log.info( |
| 620 | self.name + |
| 621 | ": Mac-Address of Host " + |
| 622 | host + |
| 623 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 624 | macAddress ) |
| 625 | return macAddress |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 626 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 627 | main.log.error( self.name + ": Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 628 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 629 | def getInterfaceMACAddress( self, host, interface ): |
| 630 | """ |
| 631 | Return the IP address of the interface on the given host""" |
| 632 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 633 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 634 | response = self.execute( cmd=host + " ifconfig " + interface, |
| 635 | prompt="mininet>", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 636 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 637 | main.log.error( self.name + ": EOF exception found" ) |
| 638 | main.log.error( self.name + ": " + self.handle.before ) |
| 639 | main.cleanup() |
| 640 | main.exit() |
| 641 | |
| 642 | 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] | 643 | macAddressSearch = re.search( pattern, response, re.I ) |
| 644 | if macAddressSearch is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 645 | main.log.info( "No mac address found in %s" % response ) |
| 646 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 647 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 648 | main.log.info( |
| 649 | "Mac-Address of " + |
| 650 | host + |
| 651 | ":" + |
| 652 | interface + |
| 653 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 654 | macAddress ) |
| 655 | return macAddress |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 656 | else: |
| 657 | main.log.error( "Connection failed to the host" ) |
| 658 | |
| 659 | def getIPAddress( self, host ): |
| 660 | """ |
| 661 | Verifies the host's ip configured or not.""" |
| 662 | if self.handle: |
| 663 | try: |
| 664 | response = self.execute( |
| 665 | cmd=host + |
| 666 | " ifconfig", |
| 667 | prompt="mininet>", |
| 668 | timeout=10 ) |
| 669 | except pexpect.EOF: |
| 670 | main.log.error( self.name + ": EOF exception found" ) |
| 671 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 672 | main.cleanup() |
| 673 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 674 | |
| 675 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 676 | ipAddressSearch = re.search( pattern, response ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 677 | main.log.info( |
| 678 | self.name + |
| 679 | ": IP-Address of Host " + |
| 680 | host + |
| 681 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 682 | ipAddressSearch.group( 1 ) ) |
| 683 | return ipAddressSearch.group( 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 684 | else: |
| 685 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 686 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 687 | def getSwitchDPID( self, switch ): |
| 688 | """ |
| 689 | return the datapath ID of the switch""" |
| 690 | if self.handle: |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 691 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 692 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 693 | response = self.execute( |
| 694 | cmd=cmd, |
| 695 | prompt="mininet>", |
| 696 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 697 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 698 | main.log.error( self.name + ": EOF exception found" ) |
| 699 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 700 | main.cleanup() |
| 701 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 702 | pattern = r'^(?P<dpid>\w)+' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 703 | result = re.search( pattern, response, re.MULTILINE ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 704 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 705 | main.log.info( |
| 706 | "Couldn't find DPID for switch %s, found: %s" % |
| 707 | ( switch, response ) ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 708 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 709 | return str( result.group( 0 ) ).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 710 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 711 | main.log.error( "Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 712 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 713 | def getDPID( self, switch ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 714 | if self.handle: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 715 | self.handle.sendline( "" ) |
| 716 | self.expect( "mininet>" ) |
| 717 | cmd = "py %s.dpid" % switch |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 718 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 719 | response = self.execute( |
| 720 | cmd=cmd, |
| 721 | prompt="mininet>", |
| 722 | timeout=10 ) |
| 723 | self.handle.expect( "mininet>" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 724 | response = self.handle.before |
| 725 | return response |
| 726 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 727 | main.log.error( self.name + ": EOF exception found" ) |
| 728 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 729 | main.cleanup() |
| 730 | main.exit() |
| 731 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 732 | def getInterfaces( self, node ): |
| 733 | """ |
| 734 | return information dict about interfaces connected to the node""" |
| 735 | if self.handle: |
| 736 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 737 | ' % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 738 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 739 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 740 | response = self.execute( |
| 741 | cmd=cmd, |
| 742 | prompt="mininet>", |
| 743 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 744 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 745 | main.log.error( self.name + ": EOF exception found" ) |
| 746 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 747 | main.cleanup() |
| 748 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 749 | return response |
| 750 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 751 | main.log.error( "Connection failed to the node" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 752 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 753 | def dump( self ): |
| 754 | main.log.info( self.name + ": Dump node info" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 755 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 756 | response = self.execute( |
| 757 | cmd='dump', |
| 758 | prompt='mininet>', |
| 759 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 760 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 761 | main.log.error( self.name + ": EOF exception found" ) |
| 762 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 763 | main.cleanup() |
| 764 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 765 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 766 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 767 | def intfs( self ): |
| 768 | main.log.info( self.name + ": List interfaces" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 769 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 770 | response = self.execute( |
| 771 | cmd='intfs', |
| 772 | prompt='mininet>', |
| 773 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 774 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 775 | main.log.error( self.name + ": EOF exception found" ) |
| 776 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 777 | main.cleanup() |
| 778 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 779 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 780 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 781 | def net( self ): |
| 782 | main.log.info( self.name + ": List network connections" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 783 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 784 | response = self.execute( cmd='net', prompt='mininet>', timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 785 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 786 | main.log.error( self.name + ": EOF exception found" ) |
| 787 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 788 | main.cleanup() |
| 789 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 790 | return response |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 791 | |
| 792 | def iperf( self, host1, host2 ): |
| 793 | main.log.info( |
| 794 | self.name + |
| 795 | ": Simple iperf TCP test between two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 796 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 797 | cmd1 = 'iperf ' + host1 + " " + host2 |
| 798 | self.handle.sendline( cmd1 ) |
| 799 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 800 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 801 | if re.search( 'Results:', response ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 802 | main.log.info( self.name + ": iperf test successful" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 803 | return main.TRUE |
| 804 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 805 | main.log.error( self.name + ": iperf test failed" ) |
| 806 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 807 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 808 | main.log.error( self.name + ": EOF exception found" ) |
| 809 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 810 | main.cleanup() |
| 811 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 812 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 813 | def iperfudp( self ): |
| 814 | main.log.info( |
| 815 | self.name + |
| 816 | ": Simple iperf TCP test between two " + |
| 817 | "(optionally specified) hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 818 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 819 | response = self.execute( |
| 820 | cmd='iperfudp', |
| 821 | prompt='mininet>', |
| 822 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 823 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 824 | main.log.error( self.name + ": EOF exception found" ) |
| 825 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 826 | main.cleanup() |
| 827 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 828 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 829 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 830 | def nodes( self ): |
| 831 | main.log.info( self.name + ": List all nodes." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 832 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 833 | response = self.execute( |
| 834 | cmd='nodes', |
| 835 | prompt='mininet>', |
| 836 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 837 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 838 | main.log.error( self.name + ": EOF exception found" ) |
| 839 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 840 | main.cleanup() |
| 841 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 842 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 843 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 844 | def pingpair( self ): |
| 845 | main.log.info( self.name + ": Ping between first two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 846 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 847 | response = self.execute( |
| 848 | cmd='pingpair', |
| 849 | prompt='mininet>', |
| 850 | timeout=20 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 851 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 852 | main.log.error( self.name + ": EOF exception found" ) |
| 853 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 854 | main.cleanup() |
| 855 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 856 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 857 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 858 | main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 859 | main.lastResult = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 860 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 861 | else: |
| 862 | main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 863 | main.lastResult = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 864 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 865 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 866 | def link( self, **linkargs ): |
| 867 | """ |
| 868 | Bring link( s ) between two nodes up or down""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 869 | args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 870 | end1 = args[ "END1" ] if args[ "END1" ] is not None else "" |
| 871 | end2 = args[ "END2" ] if args[ "END2" ] is not None else "" |
| 872 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 873 | main.log.info( |
| 874 | "Bring link between '" + |
| 875 | end1 + |
| 876 | "' and '" + |
| 877 | end2 + |
| 878 | "' '" + |
| 879 | option + |
| 880 | "'" ) |
| 881 | command = "link " + \ |
| 882 | str( end1 ) + " " + str( end2 ) + " " + str( option ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 883 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 884 | self.handle.sendline( command ) |
| 885 | self.handle.expect( "mininet>" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 886 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 887 | main.log.error( self.name + ": EOF exception found" ) |
| 888 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 889 | main.cleanup() |
| 890 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 891 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 892 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 893 | def yank( self, **yankargs ): |
| 894 | """ |
| 895 | yank a mininet switch interface to a host""" |
| 896 | main.log.info( 'Yank the switch interface attached to a host' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 897 | args = utilities.parse_args( [ "SW", "INTF" ], **yankargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 898 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 899 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 900 | command = "py " + str( sw ) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 901 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 902 | response = self.execute( |
| 903 | cmd=command, |
| 904 | prompt="mininet>", |
| 905 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 906 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 907 | main.log.error( self.name + ": EOF exception found" ) |
| 908 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 909 | main.cleanup() |
| 910 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 911 | return main.TRUE |
| 912 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 913 | def plug( self, **plugargs ): |
| 914 | """ |
| 915 | plug the yanked mininet switch interface to a switch""" |
| 916 | main.log.info( 'Plug the switch interface attached to a switch' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 917 | args = utilities.parse_args( [ "SW", "INTF" ], **plugargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 918 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 919 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 920 | command = "py " + str( sw ) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 921 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 922 | response = self.execute( |
| 923 | cmd=command, |
| 924 | prompt="mininet>", |
| 925 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 926 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 927 | main.log.error( self.name + ": EOF exception found" ) |
| 928 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 929 | main.cleanup() |
| 930 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 931 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 932 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 933 | def dpctl( self, **dpctlargs ): |
| 934 | """ |
| 935 | Run dpctl command on all switches.""" |
| 936 | main.log.info( 'Run dpctl command on all switches' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 937 | args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 938 | cmd = args[ "CMD" ] if args[ "CMD" ] is not None else "" |
| 939 | cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else "" |
| 940 | command = "dpctl " + cmd + " " + str( cmdargs ) |
| 941 | try: |
| 942 | response = self.execute( |
| 943 | cmd=command, |
| 944 | prompt="mininet>", |
| 945 | timeout=10 ) |
| 946 | except pexpect.EOF: |
| 947 | main.log.error( self.name + ": EOF exception found" ) |
| 948 | main.log.error( self.name + ": " + self.handle.before ) |
| 949 | main.cleanup() |
| 950 | main.exit() |
| 951 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 952 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 953 | def getVersion( self ): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 954 | #FIXME: What uses this? This should be refactored to get |
| 955 | # version from MN and not some other file |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 956 | fileInput = path + '/lib/Mininet/INSTALL' |
| 957 | version = super( Mininet, self ).getVersion() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 958 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 959 | for line in open( fileInput, 'r' ).readlines(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 960 | result = re.match( pattern, line ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 961 | if result: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 962 | version = result.group( 0 ) |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 963 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 964 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 965 | def getSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 966 | """ |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 967 | Parameters: |
| 968 | sw: The name of an OVS switch. Example "s1" |
| 969 | Return: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 970 | The output of the command from the mininet cli |
| 971 | or main.FALSE on timeout""" |
| 972 | command = "sh ovs-vsctl get-controller " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 973 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 974 | response = self.execute( |
| 975 | cmd=command, |
| 976 | prompt="mininet>", |
| 977 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 978 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 979 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 980 | else: |
| 981 | return main.FALSE |
| 982 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 983 | main.log.error( self.name + ": EOF exception found" ) |
| 984 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 985 | main.cleanup() |
| 986 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 987 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 988 | def assignSwController( self, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 989 | """ |
| 990 | count is only needed if there is more than 1 controller""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 991 | args = utilities.parse_args( [ "COUNT" ], **kwargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 992 | count = args[ "COUNT" ] if args != {} else 1 |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 993 | |
| 994 | argstring = "SW" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 995 | for j in range( count ): |
| 996 | argstring = argstring + ",IP" + \ |
| 997 | str( j + 1 ) + ",PORT" + str( j + 1 ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 998 | args = utilities.parse_args( argstring.split( "," ), **kwargs ) |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 999 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1000 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1001 | ptcpA = int( args[ "PORT1" ] ) + \ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1002 | int( sw ) if args[ "PORT1" ] is not None else "" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1003 | ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else "" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1004 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1005 | command = "sh ovs-vsctl set-controller s" + \ |
| 1006 | str( sw ) + " " + ptcpB + " " |
| 1007 | for j in range( count ): |
| 1008 | i = j + 1 |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1009 | args = utilities.parse_args( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1010 | [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs ) |
| 1011 | ip = args[ |
| 1012 | "IP" + |
| 1013 | str( i ) ] if args[ |
| 1014 | "IP" + |
| 1015 | str( i ) ] is not None else "" |
| 1016 | port = args[ |
| 1017 | "PORT" + |
| 1018 | str( i ) ] if args[ |
| 1019 | "PORT" + |
| 1020 | str( i ) ] is not None else "" |
| 1021 | tcp = "tcp:" + str( ip ) + ":" + str( port ) + \ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1022 | " " if ip != "" else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1023 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1024 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1025 | self.execute( cmd=command, prompt="mininet>", timeout=5 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1026 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1027 | main.log.error( self.name + ": EOF exception found" ) |
| 1028 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1029 | main.cleanup() |
| 1030 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1031 | except Exception: |
| 1032 | main.log.exception( self.name + ": Uncaught exception!" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1033 | main.cleanup() |
| 1034 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1035 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1036 | def deleteSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1037 | """ |
| 1038 | Removes the controller target from sw""" |
| 1039 | command = "sh ovs-vsctl del-controller " + str( sw ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1040 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1041 | response = self.execute( |
| 1042 | cmd=command, |
| 1043 | prompt="mininet>", |
| 1044 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1045 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1046 | main.log.error( self.name + ": EOF exception found" ) |
| 1047 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1048 | main.cleanup() |
| 1049 | main.exit() |
| 1050 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1051 | main.log.info( response ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1052 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1053 | def addSwitch( self, sw, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1054 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1055 | adds a switch to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1056 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1057 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1058 | NOTE: cannot currently specify what type of switch |
| 1059 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1060 | sw = name of the new switch as a string |
| 1061 | optional keywords: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1062 | dpid = "dpid" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1063 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1064 | """ |
| 1065 | dpid = kwargs.get( 'dpid', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1066 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1067 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1068 | response = self.execute( |
| 1069 | cmd=command, |
| 1070 | prompt="mininet>", |
| 1071 | timeout=10 ) |
| 1072 | if re.search( "already exists!", response ): |
| 1073 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1074 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1075 | elif re.search( "Error", response ): |
| 1076 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1077 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1078 | elif re.search( "usage:", response ): |
| 1079 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1080 | return main.FALSE |
| 1081 | else: |
| 1082 | return main.TRUE |
| 1083 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1084 | main.log.error( self.name + ": EOF exception found" ) |
| 1085 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1086 | main.cleanup() |
| 1087 | main.exit() |
| 1088 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1089 | def delSwitch( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1090 | """ |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1091 | delete a switch from the mininet topology |
| 1092 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1093 | dynamic_topo branch |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1094 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1095 | sw = name of the switch as a string |
| 1096 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1097 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1098 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1099 | response = self.execute( |
| 1100 | cmd=command, |
| 1101 | prompt="mininet>", |
| 1102 | timeout=10 ) |
| 1103 | if re.search( "no switch named", response ): |
| 1104 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1105 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1106 | elif re.search( "Error", response ): |
| 1107 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1108 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1109 | elif re.search( "usage:", response ): |
| 1110 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1111 | return main.FALSE |
| 1112 | else: |
| 1113 | return main.TRUE |
| 1114 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1115 | main.log.error( self.name + ": EOF exception found" ) |
| 1116 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1117 | main.cleanup() |
| 1118 | main.exit() |
| 1119 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1120 | def addLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1121 | """ |
| 1122 | add a link to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1123 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1124 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1125 | NOTE: cannot currently specify what type of link |
| 1126 | required params: |
| 1127 | node1 = the string node name of the first endpoint of the link |
| 1128 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1129 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1130 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1131 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1132 | response = self.execute( |
| 1133 | cmd=command, |
| 1134 | prompt="mininet>", |
| 1135 | timeout=10 ) |
| 1136 | if re.search( "doesnt exist!", response ): |
| 1137 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1138 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1139 | elif re.search( "Error", response ): |
| 1140 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1141 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1142 | elif re.search( "usage:", response ): |
| 1143 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1144 | return main.FALSE |
| 1145 | else: |
| 1146 | return main.TRUE |
| 1147 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1148 | main.log.error( self.name + ": EOF exception found" ) |
| 1149 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1150 | main.cleanup() |
| 1151 | main.exit() |
| 1152 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1153 | def delLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1154 | """ |
| 1155 | delete a link from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1156 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1157 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1158 | required params: |
| 1159 | node1 = the string node name of the first endpoint of the link |
| 1160 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1161 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1162 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1163 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1164 | response = self.execute( |
| 1165 | cmd=command, |
| 1166 | prompt="mininet>", |
| 1167 | timeout=10 ) |
| 1168 | if re.search( "no node named", response ): |
| 1169 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1170 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1171 | elif re.search( "Error", response ): |
| 1172 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1173 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1174 | elif re.search( "usage:", response ): |
| 1175 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1176 | return main.FALSE |
| 1177 | else: |
| 1178 | return main.TRUE |
| 1179 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1180 | main.log.error( self.name + ": EOF exception found" ) |
| 1181 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1182 | main.cleanup() |
| 1183 | main.exit() |
| 1184 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1185 | def addHost( self, hostname, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1186 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1187 | Add a host to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1188 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1189 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1190 | NOTE: cannot currently specify what type of host |
| 1191 | required params: |
| 1192 | hostname = the string hostname |
| 1193 | optional key-value params |
| 1194 | switch = "switch name" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1195 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1196 | """ |
| 1197 | switch = kwargs.get( 'switch', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1198 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1199 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1200 | response = self.execute( |
| 1201 | cmd=command, |
| 1202 | prompt="mininet>", |
| 1203 | timeout=10 ) |
| 1204 | if re.search( "already exists!", response ): |
| 1205 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1206 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1207 | elif re.search( "doesnt exists!", response ): |
| 1208 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1209 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1210 | elif re.search( "Error", response ): |
| 1211 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1212 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1213 | elif re.search( "usage:", response ): |
| 1214 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1215 | return main.FALSE |
| 1216 | else: |
| 1217 | return main.TRUE |
| 1218 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1219 | main.log.error( self.name + ": EOF exception found" ) |
| 1220 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1221 | main.cleanup() |
| 1222 | main.exit() |
| 1223 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1224 | def delHost( self, hostname ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1225 | """ |
| 1226 | delete a host from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1227 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1228 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1229 | NOTE: this uses a custom mn function |
| 1230 | required params: |
| 1231 | hostname = the string hostname |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1232 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1233 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1234 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1235 | response = self.execute( |
| 1236 | cmd=command, |
| 1237 | prompt="mininet>", |
| 1238 | timeout=10 ) |
| 1239 | if re.search( "no host named", response ): |
| 1240 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1241 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1242 | elif re.search( "Error", response ): |
| 1243 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1244 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1245 | elif re.search( "usage:", response ): |
| 1246 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1247 | return main.FALSE |
| 1248 | else: |
| 1249 | return main.TRUE |
| 1250 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1251 | main.log.error( self.name + ": EOF exception found" ) |
| 1252 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1253 | main.cleanup() |
| 1254 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1255 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1256 | def disconnect( self ): |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1257 | """ |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1258 | Called at the end of the test to stop the mininet and |
| 1259 | disconnect the handle. |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1260 | """ |
| 1261 | self.handle.sendline('') |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1262 | i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ], |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1263 | timeout=2) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1264 | if i == 0: |
| 1265 | self.stopNet() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1266 | elif i == 1: |
| 1267 | return main.TRUE |
| 1268 | response = main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1269 | # print "Disconnecting Mininet" |
| 1270 | if self.handle: |
| 1271 | self.handle.sendline( "exit" ) |
| 1272 | self.handle.expect( "exit" ) |
| 1273 | self.handle.expect( "(.*)" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1274 | else: |
| 1275 | main.log.error( "Connection failed to the host" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1276 | return response |
| 1277 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1278 | def stopNet( self, fileName = "", timeout=5): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1279 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1280 | Stops mininet. |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1281 | Returns main.TRUE if the mininet successfully stops and |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1282 | main.FALSE if the pexpect handle does not exist. |
| 1283 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1284 | Will cleanup and exit the test if mininet fails to stop |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1285 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1286 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1287 | main.log.info( self.name + ": Stopping mininet..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1288 | response = '' |
| 1289 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1290 | try: |
kelvin-onlab | 26bc17f | 2015-02-06 14:08:59 -0800 | [diff] [blame] | 1291 | self.handle.sendline("") |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1292 | i = self.handle.expect( [ 'mininet>', |
| 1293 | '\$', |
| 1294 | pexpect.EOF, |
| 1295 | pexpect.TIMEOUT ], |
| 1296 | timeout ) |
| 1297 | if i == 0: |
| 1298 | main.log.info( "Exiting mininet..." ) |
| 1299 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1300 | response = self.execute( |
| 1301 | cmd="exit", |
| 1302 | prompt="(.*)", |
| 1303 | timeout=120 ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1304 | main.log.info( self.name + ": Stopped") |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1305 | self.handle.sendline( "sudo mn -c" ) |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 1306 | response = main.TRUE |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1307 | |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1308 | if i == 1: |
| 1309 | main.log.info( " Mininet trying to exit while not " + |
| 1310 | "in the mininet prompt" ) |
| 1311 | elif i == 2: |
| 1312 | main.log.error( "Something went wrong exiting mininet" ) |
| 1313 | elif i == 3: # timeout |
| 1314 | main.log.error( "Something went wrong exiting mininet " + |
| 1315 | "TIMEOUT" ) |
| 1316 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1317 | if fileName: |
| 1318 | self.handle.sendline("") |
| 1319 | self.handle.expect('\$') |
| 1320 | self.handle.sendline("sudo kill -9 \`ps -ef | grep \""+ fileName +"\" | grep -v grep | awk '{print $2}'\`") |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1321 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1322 | main.log.error( self.name + ": EOF exception found" ) |
| 1323 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1324 | main.cleanup() |
| 1325 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1326 | else: |
| 1327 | main.log.error( self.name + ": Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1328 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1329 | return response |
| 1330 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1331 | def arping( self, src, dest, destmac ): |
| 1332 | self.handle.sendline( '' ) |
| 1333 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1334 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1335 | self.handle.sendline( src + ' arping ' + dest ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1336 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1337 | self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] ) |
| 1338 | main.log.info( self.name + ": ARP successful" ) |
| 1339 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1340 | return main.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1341 | except Exception: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1342 | main.log.warn( self.name + ": ARP FAILURE" ) |
| 1343 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1344 | return main.FALSE |
| 1345 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1346 | def decToHex( self, num ): |
| 1347 | return hex( num ).split( 'x' )[ 1 ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1348 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1349 | def getSwitchFlowCount( self, switch ): |
| 1350 | """ |
| 1351 | return the Flow Count of the switch""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1352 | if self.handle: |
| 1353 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 1354 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1355 | response = self.execute( |
| 1356 | cmd=cmd, |
| 1357 | prompt="mininet>", |
| 1358 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1359 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1360 | main.log.error( self.name + ": EOF exception found" ) |
| 1361 | main.log.error( self.name + " " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1362 | main.cleanup() |
| 1363 | main.exit() |
| 1364 | pattern = "flow_count=(\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1365 | result = re.search( pattern, response, re.MULTILINE ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1366 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1367 | main.log.info( |
| 1368 | "Couldn't find flows on switch %s, found: %s" % |
| 1369 | ( switch, response ) ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1370 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1371 | return result.group( 1 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1372 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1373 | main.log.error( "Connection failed to the Mininet host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1374 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1375 | def checkFlows( self, sw, dumpFormat=None ): |
| 1376 | if dumpFormat: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1377 | command = "sh ovs-ofctl -F " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1378 | dumpFormat + " dump-flows " + str( sw ) |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 1379 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1380 | command = "sh ovs-ofctl dump-flows " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1381 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1382 | response = self.execute( |
| 1383 | cmd=command, |
| 1384 | prompt="mininet>", |
| 1385 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1386 | return response |
| 1387 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1388 | main.log.error( self.name + ": EOF exception found" ) |
| 1389 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1390 | main.cleanup() |
| 1391 | main.exit() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1392 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1393 | def startTcpdump( self, filename, intf="eth0", port="port 6633" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1394 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1395 | Runs tpdump on an interface and saves the file |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1396 | intf can be specified, or the default eth0 is used""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1397 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1398 | self.handle.sendline( "" ) |
| 1399 | self.handle.expect( "mininet>" ) |
| 1400 | self.handle.sendline( |
| 1401 | "sh sudo tcpdump -n -i " + |
| 1402 | intf + |
| 1403 | " " + |
| 1404 | port + |
| 1405 | " -w " + |
| 1406 | filename.strip() + |
| 1407 | " &" ) |
| 1408 | self.handle.sendline( "" ) |
| 1409 | i = self.handle.expect( [ 'No\ssuch\device', |
| 1410 | 'listening\son', |
| 1411 | pexpect.TIMEOUT, |
| 1412 | "mininet>" ], |
| 1413 | timeout=10 ) |
| 1414 | main.log.warn( self.handle.before + self.handle.after ) |
| 1415 | self.handle.sendline( "" ) |
| 1416 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1417 | if i == 0: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1418 | main.log.error( |
| 1419 | self.name + |
| 1420 | ": tcpdump - No such device exists. " + |
| 1421 | "tcpdump attempted on: " + |
| 1422 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1423 | return main.FALSE |
| 1424 | elif i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1425 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1426 | return main.TRUE |
| 1427 | elif i == 2: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1428 | main.log.error( |
| 1429 | self.name + |
| 1430 | ": tcpdump command timed out! Check interface name," + |
| 1431 | " given interface was: " + |
| 1432 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1433 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1434 | elif i == 3: |
| 1435 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1436 | return main.TRUE |
| 1437 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1438 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1439 | return main.FALSE |
| 1440 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1441 | main.log.error( self.name + ": EOF exception found" ) |
| 1442 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1443 | main.cleanup() |
| 1444 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1445 | except Exception: |
| 1446 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1447 | main.cleanup() |
| 1448 | main.exit() |
| 1449 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1450 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1451 | """ |
| 1452 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1453 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1454 | self.handle.sendline( "sh sudo pkill tcpdump" ) |
| 1455 | self.handle.expect( "mininet>" ) |
| 1456 | self.handle.sendline( "" ) |
| 1457 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1458 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1459 | main.log.error( self.name + ": EOF exception found" ) |
| 1460 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1461 | main.cleanup() |
| 1462 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 1463 | except Exception: |
| 1464 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1465 | main.cleanup() |
| 1466 | main.exit() |
| 1467 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1468 | def compareSwitches( self, topo, switchesJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1469 | """ |
| 1470 | Compare mn and onos switches |
| 1471 | topo: sts TestONTopology object |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1472 | switchesJson: parsed json object from the onos devices api |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1473 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1474 | This uses the sts TestONTopology object""" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1475 | # main.log.debug( "Switches_json string: ", switchesJson ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1476 | output = { "switches": [] } |
| 1477 | # iterate through the MN topology and pull out switches and and port |
| 1478 | # info |
| 1479 | for switch in topo.graph.switches: |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1480 | ports = [] |
| 1481 | for port in switch.ports.values(): |
kelvin-onlab | 652e1dd | 2015-01-20 17:01:39 -0800 | [diff] [blame] | 1482 | ports.append( { 'of_port': port.port_no, |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1483 | 'mac': str( port.hw_addr ).replace( '\'', '' ), |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1484 | 'name': port.name } ) |
| 1485 | output[ 'switches' ].append( { |
| 1486 | "name": switch.name, |
| 1487 | "dpid": str( switch.dpid ).zfill( 16 ), |
| 1488 | "ports": ports } ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1489 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1490 | # print "mn" |
| 1491 | # print json.dumps( output, |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1492 | # sort_keys=True, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1493 | # indent=4, |
| 1494 | # separators=( ',', ': ' ) ) |
| 1495 | # print "onos" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1496 | # print json.dumps( switchesJson, |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1497 | # sort_keys=True, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1498 | # indent=4, |
| 1499 | # separators=( ',', ': ' ) ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1500 | |
| 1501 | # created sorted list of dpid's in MN and ONOS for comparison |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1502 | mnDPIDs = [] |
| 1503 | for switch in output[ 'switches' ]: |
| 1504 | mnDPIDs.append( switch[ 'dpid' ].lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1505 | mnDPIDs.sort() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1506 | # print "List of Mininet switch DPID's" |
| 1507 | # print mnDPIDs |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1508 | if switchesJson == "": # if rest call fails |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1509 | main.log.error( |
| 1510 | self.name + |
| 1511 | ".compare_switches(): Empty JSON object given from ONOS" ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1512 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1513 | onos = switchesJson |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1514 | onosDPIDs = [] |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1515 | for switch in onos: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1516 | if switch[ 'available' ]: |
| 1517 | onosDPIDs.append( |
| 1518 | switch[ 'id' ].replace( |
| 1519 | ":", |
| 1520 | '' ).replace( |
| 1521 | "of", |
| 1522 | '' ).lower() ) |
| 1523 | # else: |
| 1524 | # print "Switch is unavailable:" |
| 1525 | # print switch |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1526 | onosDPIDs.sort() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1527 | # print "List of ONOS switch DPID's" |
| 1528 | # print onosDPIDs |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1529 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1530 | if mnDPIDs != onosDPIDs: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1531 | switchResults = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1532 | main.log.report( "Switches in MN but not in ONOS:" ) |
| 1533 | list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ] |
| 1534 | main.log.report( str( list1 ) ) |
| 1535 | main.log.report( "Switches in ONOS but not in MN:" ) |
| 1536 | list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ] |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1537 | main.log.report( str( list2 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1538 | else: # list of dpid's match in onos and mn |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1539 | switchResults = main.TRUE |
| 1540 | return switchResults |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 1541 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1542 | def comparePorts( self, topo, portsJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1543 | """ |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1544 | Compare mn and onos ports |
| 1545 | topo: sts TestONTopology object |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1546 | portsJson: parsed json object from the onos ports api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1547 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1548 | Dependencies: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1549 | 1. This uses the sts TestONTopology object |
| 1550 | 2. numpy - "sudo pip install numpy" |
| 1551 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1552 | """ |
| 1553 | # FIXME: this does not look for extra ports in ONOS, only checks that |
| 1554 | # ONOS has what is in MN |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1555 | from numpy import uint64 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1556 | portsResults = main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1557 | output = { "switches": [] } |
| 1558 | # iterate through the MN topology and pull out switches and and port |
| 1559 | # info |
| 1560 | for switch in topo.graph.switches: |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1561 | ports = [] |
| 1562 | for port in switch.ports.values(): |
kelvin-onlab | 652e1dd | 2015-01-20 17:01:39 -0800 | [diff] [blame] | 1563 | # print port.hw_addr.toStr( separator='' ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1564 | tmpPort = { 'of_port': port.port_no, |
| 1565 | 'mac': str( port.hw_addr ).replace( '\'', '' ), |
| 1566 | 'name': port.name, |
| 1567 | 'enabled': port.enabled } |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1568 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1569 | ports.append( tmpPort ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1570 | tmpSwitch = { 'name': switch.name, |
| 1571 | 'dpid': str( switch.dpid ).zfill( 16 ), |
| 1572 | 'ports': ports } |
Jon Hall | 39f29df | 2014-11-04 19:30:21 -0500 | [diff] [blame] | 1573 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1574 | output[ 'switches' ].append( tmpSwitch ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1575 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1576 | # PORTS |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1577 | for mnSwitch in output[ 'switches' ]: |
| 1578 | mnPorts = [] |
| 1579 | onosPorts = [] |
| 1580 | switchResult = main.TRUE |
| 1581 | for port in mnSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1582 | if port[ 'enabled' ]: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1583 | mnPorts.append( port[ 'of_port' ] ) |
| 1584 | for onosSwitch in portsJson: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1585 | # print "Iterating through a new switch as seen by ONOS" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1586 | # print onosSwitch |
| 1587 | if onosSwitch[ 'device' ][ 'available' ]: |
| 1588 | if onosSwitch[ 'device' ][ 'id' ].replace( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1589 | ':', |
| 1590 | '' ).replace( |
| 1591 | "of", |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1592 | '' ) == mnSwitch[ 'dpid' ]: |
| 1593 | for port in onosSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1594 | if port[ 'isEnabled' ]: |
| 1595 | if port[ 'port' ] == 'local': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1596 | # onosPorts.append( 'local' ) |
| 1597 | onosPorts.append( long( uint64( -2 ) ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1598 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1599 | onosPorts.append( int( port[ 'port' ] ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1600 | break |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1601 | mnPorts.sort( key=float ) |
| 1602 | onosPorts.sort( key=float ) |
| 1603 | # print "\nPorts for Switch %s:" % ( mnSwitch[ 'name' ] ) |
| 1604 | # print "\tmn_ports[] = ", mnPorts |
| 1605 | # print "\tonos_ports[] = ", onosPorts |
| 1606 | mnPortsLog = mnPorts |
| 1607 | onosPortsLog = onosPorts |
| 1608 | mnPorts = [ x for x in mnPorts ] |
| 1609 | onosPorts = [ x for x in onosPorts ] |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1610 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1611 | # TODO: handle other reserved port numbers besides LOCAL |
| 1612 | # NOTE: Reserved ports |
| 1613 | # Local port: -2 in Openflow, ONOS shows 'local', we store as |
| 1614 | # long( uint64( -2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1615 | for mnPort in mnPortsLog: |
| 1616 | if mnPort in onosPorts: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1617 | # don't set results to true here as this is just one of |
| 1618 | # many checks and it might override a failure |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1619 | mnPorts.remove( mnPort ) |
| 1620 | onosPorts.remove( mnPort ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1621 | # NOTE: OVS reports this as down since there is no link |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1622 | # So ignoring these for now |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1623 | # TODO: Come up with a better way of handling these |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1624 | if 65534 in mnPorts: |
| 1625 | mnPorts.remove( 65534 ) |
| 1626 | if long( uint64( -2 ) ) in onosPorts: |
| 1627 | onosPorts.remove( long( uint64( -2 ) ) ) |
| 1628 | if len( mnPorts ): # the ports of this switch don't match |
| 1629 | switchResult = main.FALSE |
| 1630 | main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) ) |
| 1631 | if len( onosPorts ): # the ports of this switch don't match |
| 1632 | switchResult = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1633 | main.log.warn( |
| 1634 | "Ports in ONOS but not MN: " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1635 | str( onosPorts ) ) |
| 1636 | if switchResult == main.FALSE: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1637 | main.log.report( |
| 1638 | "The list of ports for switch %s(%s) does not match:" % |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1639 | ( mnSwitch[ 'name' ], mnSwitch[ 'dpid' ] ) ) |
| 1640 | main.log.warn( "mn_ports[] = " + str( mnPortsLog ) ) |
| 1641 | main.log.warn( "onos_ports[] = " + str( onosPortsLog ) ) |
| 1642 | portsResults = portsResults and switchResult |
| 1643 | return portsResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1644 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1645 | def compareLinks( self, topo, linksJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1646 | """ |
| 1647 | Compare mn and onos links |
| 1648 | topo: sts TestONTopology object |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1649 | linksJson: parsed json object from the onos links api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1650 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1651 | This uses the sts TestONTopology object""" |
| 1652 | # 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] | 1653 | # ONOS has what is in MN |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1654 | output = { "switches": [] } |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1655 | onos = linksJson |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1656 | # iterate through the MN topology and pull out switches and and port |
| 1657 | # info |
| 1658 | for switch in topo.graph.switches: |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1659 | # print "Iterating though switches as seen by Mininet" |
| 1660 | # print switch |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1661 | ports = [] |
| 1662 | for port in switch.ports.values(): |
kelvin-onlab | 652e1dd | 2015-01-20 17:01:39 -0800 | [diff] [blame] | 1663 | # print port.hw_addr.toStr( separator='' ) |
| 1664 | ports.append( { 'of_port': port.port_no, |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1665 | 'mac': str( port.hw_addr ).replace( '\'', '' ), |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1666 | 'name': port.name } ) |
| 1667 | output[ 'switches' ].append( { |
| 1668 | "name": switch.name, |
| 1669 | "dpid": str( switch.dpid ).zfill( 16 ), |
| 1670 | "ports": ports } ) |
| 1671 | # LINKS |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1672 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1673 | mnLinks = [ |
kelvin-onlab | 9592d13 | 2015-01-20 17:18:02 -0800 | [diff] [blame] | 1674 | link for link in topo.patch_panel.network_links if ( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1675 | link.port1.enabled and link.port2.enabled ) ] |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1676 | if 2 * len( mnLinks ) == len( onos ): |
| 1677 | linkResults = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1678 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1679 | linkResults = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1680 | main.log.report( |
Jon Hall | 328ddca | 2015-01-28 15:57:15 -0800 | [diff] [blame] | 1681 | "Mininet has " + str( len( mnLinks ) ) + |
| 1682 | " bidirectional links and ONOS has " + |
| 1683 | str( len( onos ) ) + " unidirectional links" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1684 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1685 | # iterate through MN links and check if an ONOS link exists in |
| 1686 | # both directions |
| 1687 | # NOTE: Will currently only show mn links as down if they are |
| 1688 | # cut through STS. We can either do everything through STS or |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1689 | # wait for upNetworkLinks and downNetworkLinks to be |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1690 | # fully implemented. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1691 | for link in mnLinks: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1692 | # print "Link: %s" % link |
| 1693 | # TODO: Find a more efficient search method |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1694 | node1 = None |
| 1695 | port1 = None |
| 1696 | node2 = None |
| 1697 | port2 = None |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1698 | firstDir = main.FALSE |
| 1699 | secondDir = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1700 | for switch in output[ 'switches' ]: |
| 1701 | # print "Switch: %s" % switch[ 'name' ] |
| 1702 | if switch[ 'name' ] == link.node1.name: |
| 1703 | node1 = switch[ 'dpid' ] |
| 1704 | for port in switch[ 'ports' ]: |
| 1705 | if str( port[ 'name' ] ) == str( link.port1 ): |
| 1706 | port1 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1707 | if node1 is not None and node2 is not None: |
| 1708 | break |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1709 | if switch[ 'name' ] == link.node2.name: |
| 1710 | node2 = switch[ 'dpid' ] |
| 1711 | for port in switch[ 'ports' ]: |
| 1712 | if str( port[ 'name' ] ) == str( link.port2 ): |
| 1713 | port2 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1714 | if node1 is not None and node2 is not None: |
| 1715 | break |
| 1716 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1717 | for onosLink in onos: |
| 1718 | onosNode1 = onosLink[ 'src' ][ 'device' ].replace( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1719 | ":", |
| 1720 | '' ).replace( |
| 1721 | "of", |
| 1722 | '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1723 | onosNode2 = onosLink[ 'dst' ][ 'device' ].replace( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1724 | ":", |
| 1725 | '' ).replace( |
| 1726 | "of", |
| 1727 | '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1728 | onosPort1 = onosLink[ 'src' ][ 'port' ] |
| 1729 | onosPort2 = onosLink[ 'dst' ][ 'port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1730 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1731 | # check onos link from node1 to node2 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1732 | if str( onosNode1 ) == str( node1 ) and str( |
| 1733 | onosNode2 ) == str( node2 ): |
| 1734 | if int( onosPort1 ) == int( port1 ) and int( |
| 1735 | onosPort2 ) == int( port2 ): |
| 1736 | firstDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1737 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1738 | main.log.warn( |
| 1739 | 'The port numbers do not match for ' + |
| 1740 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1741 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1742 | 'link %s/%s -> %s/%s' % |
| 1743 | ( node1, |
| 1744 | port1, |
| 1745 | node2, |
| 1746 | port2 ) + |
| 1747 | ' ONOS has the values %s/%s -> %s/%s' % |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1748 | ( onosNode1, |
| 1749 | onosPort1, |
| 1750 | onosNode2, |
| 1751 | onosPort2 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1752 | |
| 1753 | # check onos link from node2 to node1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1754 | elif ( str( onosNode1 ) == str( node2 ) and |
| 1755 | str( onosNode2 ) == str( node1 ) ): |
| 1756 | if ( int( onosPort1 ) == int( port2 ) |
| 1757 | and int( onosPort2 ) == int( port1 ) ): |
| 1758 | secondDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1759 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1760 | main.log.warn( |
| 1761 | 'The port numbers do not match for ' + |
| 1762 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1763 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1764 | 'link %s/%s -> %s/%s' % |
| 1765 | ( node2, |
| 1766 | port2, |
| 1767 | node1, |
| 1768 | port1 ) + |
| 1769 | ' ONOS has the values %s/%s -> %s/%s' % |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1770 | ( onosNode2, |
| 1771 | onosPort2, |
| 1772 | onosNode1, |
| 1773 | onosPort1 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1774 | else: # this is not the link you're looking for |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1775 | pass |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1776 | if not firstDir: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1777 | main.log.report( |
| 1778 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 1779 | ( node1, port1, node2, port2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1780 | if not secondDir: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1781 | main.log.report( |
| 1782 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 1783 | ( node2, port2, node1, port1 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1784 | linkResults = linkResults and firstDir and secondDir |
| 1785 | return linkResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1786 | |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1787 | def compareHosts( self, topo, hostsJson ): |
| 1788 | """ |
| 1789 | Compare mn and onos Hosts. |
| 1790 | Since Mininet hosts are quiet, ONOS will only know of them when they |
| 1791 | speak. For this reason, we will only check that the hosts in ONOS |
| 1792 | stores are in Mininet, and not vice versa. |
| 1793 | topo: sts TestONTopology object |
| 1794 | hostsJson: parsed json object from the onos hosts api |
| 1795 | |
| 1796 | This uses the sts TestONTopology object""" |
| 1797 | import json |
| 1798 | hostResults = main.TRUE |
| 1799 | hosts = [] |
| 1800 | # iterate through the MN topology and pull out hosts |
| 1801 | for mnHost in topo.graph.hosts: |
| 1802 | interfaces = [] |
| 1803 | for intf in mnHost.interfaces: |
| 1804 | interfaces.append( { |
| 1805 | "name": intf.name, # str |
| 1806 | "ips": [ str( ip ) for ip in intf.ips ], # list of IPAddrs |
| 1807 | # hw_addr is of type EthAddr, Not JSON serializable |
| 1808 | "hw_addr": str( intf.hw_addr ) } ) |
| 1809 | hosts.append( { |
| 1810 | "name": mnHost.name, # str |
| 1811 | "interfaces": interfaces } ) # list |
| 1812 | for onosHost in hostsJson: |
| 1813 | onosMAC = onosHost[ 'mac' ].lower() |
| 1814 | match = False |
| 1815 | for mnHost in hosts: |
| 1816 | for mnIntf in mnHost[ 'interfaces' ]: |
| 1817 | if onosMAC == mnIntf[ 'hw_addr' ].lower() : |
| 1818 | match = True |
| 1819 | for ip in mnIntf[ 'ips' ]: |
| 1820 | if ip in onosHost[ 'ips' ]: |
| 1821 | pass # all is well |
| 1822 | else: |
| 1823 | # misssing ip |
| 1824 | main.log.error( "ONOS host " + onosHost[ 'id' ] |
| 1825 | + " has a different IP than " + |
| 1826 | "the Mininet host." ) |
| 1827 | output = json.dumps( |
| 1828 | onosHost, |
| 1829 | sort_keys=True, |
| 1830 | indent=4, |
| 1831 | separators=( ',', ': ' ) ) |
| 1832 | main.log.info( output ) |
| 1833 | hostResults = main.FALSE |
| 1834 | if not match: |
| 1835 | hostResults = main.FALSE |
| 1836 | main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " + |
| 1837 | "corresponding Mininet host." ) |
| 1838 | output = json.dumps( onosHost, |
| 1839 | sort_keys=True, |
| 1840 | indent=4, |
| 1841 | separators=( ',', ': ' ) ) |
| 1842 | main.log.info( output ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1843 | return hostResults |
| 1844 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1845 | def getHosts( self ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1846 | """ |
| 1847 | Returns a list of all hosts |
| 1848 | Don't ask questions just use it""" |
| 1849 | self.handle.sendline( "" ) |
| 1850 | self.handle.expect( "mininet>" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 1851 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1852 | self.handle.sendline( "py [ host.name for host in net.hosts ]" ) |
| 1853 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1854 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1855 | handlePy = self.handle.before |
| 1856 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 1857 | handlePy = handlePy.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1858 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1859 | self.handle.sendline( "" ) |
| 1860 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1861 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1862 | hostStr = handlePy.replace( "]", "" ) |
| 1863 | hostStr = hostStr.replace( "'", "" ) |
| 1864 | hostStr = hostStr.replace( "[", "" ) |
| 1865 | hostList = hostStr.split( "," ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 1866 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1867 | return hostList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1868 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1869 | def update( self ): |
| 1870 | """ |
| 1871 | updates the port address and status information for |
| 1872 | each port in mn""" |
| 1873 | # TODO: Add error checking. currently the mininet command has no output |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1874 | main.log.info( "Updating MN port information" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1875 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1876 | self.handle.sendline( "" ) |
| 1877 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1878 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1879 | self.handle.sendline( "update" ) |
| 1880 | self.handle.expect( "update" ) |
| 1881 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1882 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1883 | self.handle.sendline( "" ) |
| 1884 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 1885 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1886 | return main.TRUE |
| 1887 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1888 | main.log.error( self.name + ": EOF exception found" ) |
| 1889 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1890 | main.cleanup() |
| 1891 | main.exit() |
| 1892 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1893 | if __name__ != "__main__": |
| 1894 | import sys |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1895 | sys.modules[ __name__ ] = MininetCliDriver() |