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