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