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