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