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 |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 37 | import types |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 38 | import os |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 39 | from math import pow |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 42 | |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 43 | class MininetCliDriver( Emulator ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 44 | |
| 45 | """ |
| 46 | MininetCliDriver is the basic driver which will handle |
| 47 | the Mininet functions""" |
| 48 | def __init__( self ): |
| 49 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | self.handle = self |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 51 | self.name = None |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 52 | self.home = None |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 53 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | self.flag = 0 |
| 55 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 56 | def connect( self, **connectargs ): |
| 57 | """ |
| 58 | Here the main is the TestON instance after creating |
| 59 | all the log handles.""" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 60 | try: |
| 61 | for key in connectargs: |
| 62 | vars( self )[ key ] = connectargs[ key ] |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 63 | self.home = "~/mininet" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 64 | self.name = self.options[ 'name' ] |
kelvin-onlab | d9e23de | 2015-08-06 10:34:44 -0700 | [diff] [blame] | 65 | for key in self.options: |
| 66 | if key == "home": |
| 67 | self.home = self.options[ 'home' ] |
| 68 | break |
| 69 | if self.home is None or self.home == "": |
| 70 | self.home = "~/mininet" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 71 | |
| 72 | try: |
| 73 | if os.getenv( str( self.ip_address ) ) != None: |
| 74 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 75 | else: |
| 76 | main.log.info( self.name + |
| 77 | ": Trying to connect to " + |
| 78 | self.ip_address ) |
| 79 | |
| 80 | except KeyError: |
| 81 | main.log.info( "Invalid host name," + |
| 82 | " connecting to local host instead" ) |
| 83 | self.ip_address = 'localhost' |
| 84 | except Exception as inst: |
| 85 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 86 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 87 | self.handle = super( |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 88 | MininetCliDriver, |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 89 | self ).connect( |
| 90 | user_name=self.user_name, |
| 91 | ip_address=self.ip_address, |
| 92 | port=None, |
| 93 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 94 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 95 | if self.handle: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 96 | main.log.info( "Connection successful to the host " + |
| 97 | self.user_name + |
| 98 | "@" + |
| 99 | self.ip_address ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 100 | return main.TRUE |
| 101 | else: |
| 102 | main.log.error( "Connection failed to the host " + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 103 | self.user_name + |
| 104 | "@" + |
| 105 | self.ip_address ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 106 | main.log.error( "Failed to connect to the Mininet CLI" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 107 | return main.FALSE |
| 108 | except pexpect.EOF: |
| 109 | main.log.error( self.name + ": EOF exception found" ) |
| 110 | main.log.error( self.name + ": " + self.handle.before ) |
| 111 | main.cleanup() |
| 112 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 113 | except Exception: |
| 114 | main.log.exception( self.name + ": Uncaught exception!" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 115 | main.cleanup() |
| 116 | main.exit() |
| 117 | |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 118 | def startNet( self, topoFile='', args='', mnCmd='', timeout=120 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 119 | """ |
kelvin-onlab | f512e94 | 2015-06-08 19:42:59 -0700 | [diff] [blame] | 120 | Description: |
| 121 | Starts Mininet accepts a topology(.py) file and/or an optional |
| 122 | argument, to start the mininet, as a parameter. |
| 123 | Can also send regular mininet command to load up desired topology. |
| 124 | Eg. Pass in a string 'sudo mn --topo=tree,3,3' to mnCmd |
| 125 | Options: |
| 126 | topoFile = file path for topology file (.py) |
| 127 | args = extra option added when starting the topology from the file |
| 128 | mnCmd = Mininet command use to start topology |
| 129 | Returns: |
| 130 | main.TRUE if the mininet starts successfully, main.FALSE |
| 131 | otherwise |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 132 | """ |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 133 | if self.handle: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 134 | # make sure old networks are cleaned up |
| 135 | main.log.info( self.name + |
| 136 | ": Clearing any residual state or processes" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 137 | self.handle.sendline( "sudo mn -c" ) |
| 138 | i = self.handle.expect( [ 'password\sfor\s', |
| 139 | 'Cleanup\scomplete', |
| 140 | pexpect.EOF, |
| 141 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 142 | timeout ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 143 | if i == 0: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 144 | # Sudo asking for password |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 145 | main.log.info( self.name + ": Sending sudo password" ) |
| 146 | self.handle.sendline( self.pwd ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 147 | i = self.handle.expect( [ '%s:' % self.user, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 148 | '\$', |
| 149 | pexpect.EOF, |
| 150 | pexpect.TIMEOUT ], |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 151 | timeout ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 152 | if i == 1: |
| 153 | main.log.info( self.name + ": Clean" ) |
| 154 | elif i == 2: |
| 155 | main.log.error( self.name + ": Connection terminated" ) |
| 156 | elif i == 3: # timeout |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 157 | main.log.error( self.name + ": Something while cleaning " + |
| 158 | "Mininet took too long... " ) |
| 159 | # Craft the string to start mininet |
| 160 | cmdString = "sudo " |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 161 | if not mnCmd: |
| 162 | if topoFile is None or topoFile == '': # If no file is given |
| 163 | main.log.info( self.name + ": building fresh Mininet" ) |
| 164 | cmdString += "mn " |
| 165 | if args is None or args == '': |
| 166 | # If no args given, use args from .topo file |
| 167 | args = self.options[ 'arg1' ] +\ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 168 | " " + self.options[ 'arg2' ] +\ |
| 169 | " --mac --controller " +\ |
| 170 | self.options[ 'controller' ] + " " +\ |
| 171 | self.options[ 'arg3' ] |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 172 | else: # else only use given args |
| 173 | pass |
| 174 | # TODO: allow use of topo args and method args? |
| 175 | else: # Use given topology file |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 176 | main.log.info( |
| 177 | "Starting Mininet from topo file " + |
| 178 | topoFile ) |
kelvin-onlab | 10e8d39 | 2015-06-03 13:53:45 -0700 | [diff] [blame] | 179 | cmdString += topoFile + " " |
| 180 | if args is None: |
| 181 | args = '' |
| 182 | # TODO: allow use of args from .topo file? |
| 183 | cmdString += args |
| 184 | else: |
| 185 | main.log.info( "Starting Mininet topology using '" + mnCmd + |
| 186 | "' command" ) |
| 187 | cmdString += mnCmd |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 188 | # Send the command and check if network started |
| 189 | self.handle.sendline( "" ) |
| 190 | self.handle.expect( '\$' ) |
| 191 | main.log.info( "Sending '" + cmdString + "' to " + self.name ) |
| 192 | self.handle.sendline( cmdString ) |
| 193 | while True: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 194 | i = self.handle.expect( [ 'mininet>', |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 195 | 'Exception', |
| 196 | '\*\*\*', |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 197 | pexpect.EOF, |
| 198 | pexpect.TIMEOUT ], |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 199 | timeout ) |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 200 | if i == 0: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 201 | main.log.info( self.name + ": Mininet built" ) |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 202 | return main.TRUE |
kelvin-onlab | ec228b8 | 2015-02-09 15:45:55 -0800 | [diff] [blame] | 203 | elif i == 1: |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 204 | response = str( self.handle.before + |
| 205 | self.handle.after ) |
| 206 | self.handle.expect( '\$' ) |
| 207 | response += str( self.handle.before + |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 208 | self.handle.after ) |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 209 | main.log.error( |
| 210 | self.name + |
| 211 | ": Launching Mininet failed: " + response ) |
| 212 | return main.FALSE |
| 213 | elif i == 2: |
| 214 | self.handle.expect( [ "\n", |
| 215 | pexpect.EOF, |
| 216 | pexpect.TIMEOUT ], |
| 217 | timeout ) |
| 218 | main.log.info( self.handle.before ) |
| 219 | elif i == 3: |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 220 | main.log.error( self.name + ": Connection timeout" ) |
| 221 | return main.FALSE |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 222 | elif i == 4: # timeout |
kelvin-onlab | ef0cc1c | 2015-02-09 15:20:26 -0800 | [diff] [blame] | 223 | main.log.error( |
| 224 | self.name + |
| 225 | ": Something took too long... " ) |
| 226 | return main.FALSE |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 227 | # Why did we hit this part? |
| 228 | main.log.error( "startNet did not return correctly" ) |
| 229 | return main.FASLE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 230 | else: # if no handle |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 231 | main.log.error( self.name + ": Connection failed to the host " + |
| 232 | self.user_name + "@" + self.ip_address ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 233 | main.log.error( self.name + ": Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 234 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 235 | |
kelvin-onlab | fccaafa | 2015-01-20 13:50:44 -0800 | [diff] [blame] | 236 | def numSwitchesNlinks( self, topoType, depth, fanout ): |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 237 | if topoType == 'tree': |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 238 | # In tree topology, if fanout arg is not given, by default it is 2 |
| 239 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 240 | fanout = 2 |
| 241 | k = 0 |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 242 | count = 0 |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 243 | while( k <= depth - 1 ): |
| 244 | count = count + pow( fanout, k ) |
| 245 | k = k + 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 246 | numSwitches = count |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 247 | while( k <= depth - 2 ): |
| 248 | # depth-2 gives you only core links and not considering |
| 249 | # edge links as seen by ONOS. If all the links including |
| 250 | # edge links are required, do depth-1 |
| 251 | count = count + pow( fanout, k ) |
| 252 | k = k + 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 253 | numLinks = count * fanout |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 254 | # print "num_switches for %s(%d,%d) = %d and links=%d" %( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 255 | # topoType,depth,fanout,numSwitches,numLinks ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 256 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 257 | elif topoType == 'linear': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 258 | # In linear topology, if fanout or numHostsPerSw is not given, |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 259 | # by default it is 1 |
| 260 | if fanout is None: |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 261 | fanout = 1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 262 | numSwitches = depth |
| 263 | numHostsPerSw = fanout |
| 264 | totalNumHosts = numSwitches * numHostsPerSw |
| 265 | numLinks = totalNumHosts + ( numSwitches - 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 266 | print "num_switches for %s(%d,%d) = %d and links=%d" %\ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 267 | ( topoType, depth, fanout, numSwitches, numLinks ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 268 | topoDict = { "num_switches": int( numSwitches ), |
| 269 | "num_corelinks": int( numLinks ) } |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 270 | return topoDict |
| 271 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 272 | def calculateSwAndLinks( self ): |
Jon Hall | 689d8e4 | 2015-04-03 13:59:24 -0700 | [diff] [blame] | 273 | """ |
| 274 | Calculate the number of switches and links in a topo.""" |
| 275 | # TODO: combine this function and numSwitchesNlinks |
| 276 | argList = self.options[ 'arg1' ].split( "," ) |
| 277 | topoArgList = argList[ 0 ].split( " " ) |
| 278 | argList = map( int, argList[ 1: ] ) |
| 279 | topoArgList = topoArgList[ 1: ] + argList |
| 280 | |
| 281 | topoDict = self.numSwitchesNlinks( *topoArgList ) |
Jon Hall | 1ccf82c | 2014-10-15 14:55:16 -0400 | [diff] [blame] | 282 | return topoDict |
| 283 | |
GlennRC | f07c44a | 2015-09-18 13:33:46 -0700 | [diff] [blame] | 284 | def pingall( self, protocol="IPv4", timeout=300, shortCircuit=False, acceptableFailed=0 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 285 | """ |
| 286 | Verifies the reachability of the hosts using pingall command. |
| 287 | Optional parameter timeout allows you to specify how long to |
| 288 | wait for pingall to complete |
kelvin-onlab | fbcd82f | 2015-04-02 12:06:00 -0700 | [diff] [blame] | 289 | Optional: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 290 | timeout( seconds ) - How long to wait before breaking the pingall |
kelvin-onlab | fbcd82f | 2015-04-02 12:06:00 -0700 | [diff] [blame] | 291 | shortCircuit - Break the pingall based on the number of failed hosts |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 292 | ping |
| 293 | acceptableFailed - Set the number of acceptable failed pings for the |
| 294 | function to still return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 295 | Returns: |
| 296 | main.TRUE if pingall completes with no pings dropped |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 297 | otherwise main.FALSE |
| 298 | """ |
| 299 | import time |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 300 | try: |
Jon Hall | fb760a0 | 2015-04-13 15:35:03 -0700 | [diff] [blame] | 301 | timeout = int( timeout ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 302 | if self.handle: |
| 303 | main.log.info( |
| 304 | self.name + |
| 305 | ": Checking reachabilty to the hosts using pingall" ) |
| 306 | response = "" |
| 307 | failedPings = 0 |
| 308 | returnValue = main.TRUE |
GlennRC | f07c44a | 2015-09-18 13:33:46 -0700 | [diff] [blame] | 309 | cmd = "pingall" |
| 310 | if protocol == "IPv6": |
| 311 | cmd = "py net.pingAll6()" |
| 312 | self.handle.sendline( cmd ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 313 | startTime = time.time() |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 314 | while True: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 315 | i = self.handle.expect( [ "mininet>", "X", |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 316 | pexpect.EOF, |
| 317 | pexpect.TIMEOUT ], |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 318 | timeout ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 319 | if i == 0: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 320 | main.log.info( self.name + ": pingall finished" ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 321 | response += self.handle.before |
| 322 | break |
| 323 | elif i == 1: |
| 324 | response += self.handle.before + self.handle.after |
| 325 | failedPings = failedPings + 1 |
kelvin-onlab | d26a374 | 2015-04-06 15:31:16 -0700 | [diff] [blame] | 326 | if failedPings > acceptableFailed: |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 327 | returnValue = main.FALSE |
| 328 | if shortCircuit: |
| 329 | main.log.error( self.name + |
| 330 | ": Aborting pingall - " |
| 331 | + str( failedPings ) + |
| 332 | " pings failed" ) |
| 333 | break |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 334 | if ( time.time() - startTime ) > timeout: |
| 335 | returnValue = main.FALSE |
| 336 | main.log.error( self.name + |
| 337 | ": Aborting pingall - " + |
| 338 | "Function took too long " ) |
| 339 | break |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 340 | elif i == 2: |
| 341 | main.log.error( self.name + |
| 342 | ": EOF exception found" ) |
| 343 | main.log.error( self.name + ": " + |
| 344 | self.handle.before ) |
| 345 | main.cleanup() |
| 346 | main.exit() |
| 347 | elif i == 3: |
| 348 | response += self.handle.before |
| 349 | main.log.error( self.name + |
| 350 | ": TIMEOUT exception found" ) |
| 351 | main.log.error( self.name + |
| 352 | ": " + |
| 353 | str( response ) ) |
| 354 | # NOTE: Send ctrl-c to make sure pingall is done |
| 355 | self.handle.sendline( "\x03" ) |
| 356 | self.handle.expect( "Interrupt" ) |
| 357 | self.handle.expect( "mininet>" ) |
| 358 | break |
| 359 | pattern = "Results\:" |
| 360 | main.log.info( "Pingall output: " + str( response ) ) |
| 361 | if re.search( pattern, response ): |
| 362 | main.log.info( self.name + ": Pingall finished with " |
| 363 | + str( failedPings ) + " failed pings" ) |
| 364 | return returnValue |
| 365 | else: |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 366 | # NOTE: Send ctrl-c to make sure pingall is done |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 367 | self.handle.sendline( "\x03" ) |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 368 | self.handle.expect( "Interrupt" ) |
kelvin-onlab | c44f019 | 2015-04-02 22:08:41 -0700 | [diff] [blame] | 369 | self.handle.expect( "mininet>" ) |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 370 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 371 | else: |
kelvin-onlab | d9a8ed3 | 2015-04-03 13:55:28 -0700 | [diff] [blame] | 372 | main.log.error( self.name + ": Connection failed to the host" ) |
| 373 | main.cleanup() |
| 374 | main.exit() |
| 375 | except pexpect.TIMEOUT: |
| 376 | if response: |
| 377 | main.log.info( "Pingall output: " + str( response ) ) |
| 378 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 379 | return main.FALSE |
| 380 | except pexpect.EOF: |
| 381 | main.log.error( self.name + ": EOF exception found" ) |
| 382 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 383 | main.cleanup() |
| 384 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 385 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 386 | def fpingHost( self, **pingParams ): |
| 387 | """ |
| 388 | Uses the fping package for faster pinging... |
| 389 | *requires fping to be installed on machine running mininet""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 390 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 391 | command = args[ "SRC" ] + \ |
| 392 | " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ] |
| 393 | self.handle.sendline( command ) |
| 394 | self.handle.expect( |
| 395 | [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] ) |
| 396 | self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 397 | response = self.handle.before |
| 398 | if re.search( ":\s-", response ): |
| 399 | main.log.info( self.name + ": Ping fail" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 400 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 401 | elif re.search( ":\s\d{1,2}\.\d\d", response ): |
| 402 | main.log.info( self.name + ": Ping good!" ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 403 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 404 | main.log.info( self.name + ": Install fping on mininet machine... " ) |
| 405 | main.log.info( self.name + ": \n---\n" + response ) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 406 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 407 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 408 | def pingallHosts( self, hostList, wait=1 ): |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 409 | """ |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 410 | Ping all specified IPv4 hosts |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 411 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 412 | Acceptable hostList: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 413 | - [ 'h1','h2','h3','h4' ] |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 414 | |
| 415 | Returns main.TRUE if all hosts specified can reach |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 416 | each other |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 417 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 418 | Returns main.FALSE if one or more of hosts specified |
| 419 | cannot reach each other""" |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 420 | wait = int( wait ) |
| 421 | cmd = " ping -c 1 -i 1 -W " + str( wait ) + " " |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 422 | |
| 423 | try: |
| 424 | main.log.info( "Testing reachability between specified hosts" ) |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 425 | |
andrew@onlab.us | defe38c | 2015-05-14 19:18:18 -0400 | [diff] [blame] | 426 | isReachable = main.TRUE |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 427 | pingResponse = "IPv4 ping across specified hosts\n" |
| 428 | failedPings = 0 |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 429 | for host in hostList: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 430 | listIndex = hostList.index( host ) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 431 | # List of hosts to ping other than itself |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 432 | pingList = hostList[ :listIndex ] + \ |
| 433 | hostList[ ( listIndex + 1 ): ] |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 434 | |
| 435 | pingResponse += str(str(host) + " -> ") |
| 436 | |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 437 | for temp in pingList: |
| 438 | # Current host pings all other hosts specified |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 439 | pingCmd = str( host ) + cmd + str( temp ) |
Jon Hall | 934576d | 2015-10-09 10:12:22 -0700 | [diff] [blame] | 440 | self.handle.sendline( pingCmd ) |
| 441 | self.handle.expect( "mininet>", timeout=wait + 1 ) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 442 | response = self.handle.before |
| 443 | if re.search( ',\s0\%\spacket\sloss', response ): |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 444 | pingResponse += str(" h" + str( temp[1:] )) |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 445 | else: |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 446 | pingResponse += " X" |
andrew@onlab.us | defe38c | 2015-05-14 19:18:18 -0400 | [diff] [blame] | 447 | # One of the host to host pair is unreachable |
| 448 | isReachable = main.FALSE |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 449 | failedPings += 1 |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 450 | pingResponse += "\n" |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 451 | main.log.info( pingResponse + "Failed pings: " + str(failedPings) ) |
kelvin-onlab | 2ff5702 | 2015-05-29 10:48:51 -0700 | [diff] [blame] | 452 | return isReachable |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 453 | except pexpect.TIMEOUT: |
| 454 | main.log.exception( self.name + ": TIMEOUT exception" ) |
Hari Krishna | 4223dbd | 2015-08-13 16:29:53 -0700 | [diff] [blame] | 455 | return main.FALSE |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 456 | except pexpect.EOF: |
| 457 | main.log.error( self.name + ": EOF exception found" ) |
| 458 | main.log.error( self.name + ": " + self.handle.before ) |
| 459 | main.cleanup() |
| 460 | main.exit() |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 461 | except Exception: |
| 462 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 463 | main.cleanup() |
| 464 | main.exit() |
andrew@onlab.us | 9fdee81 | 2015-05-14 17:23:26 -0400 | [diff] [blame] | 465 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 466 | def pingIpv6Hosts( self, hostList, prefix='1000::', wait=1 ): |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 467 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 468 | IPv6 ping all hosts in hostList. If no prefix passed this will use |
| 469 | default prefix of 1000:: |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 470 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 471 | Returns main.TRUE if all hosts specified can reach each other |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 472 | |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 473 | Returns main.FALSE if one or more of hosts specified cannot reach each other |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 474 | """ |
| 475 | try: |
| 476 | main.log.info( "Testing reachability between specified IPv6 hosts" ) |
| 477 | isReachable = main.TRUE |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 478 | wait = int( wait ) |
| 479 | cmd = " ping6 -c 1 -i 1 -W " + str( wait ) + " " |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 480 | pingResponse = "IPv6 Pingall output:\n" |
| 481 | failedPings = 0 |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 482 | for host in hostList: |
| 483 | listIndex = hostList.index( host ) |
| 484 | # List of hosts to ping other than itself |
| 485 | pingList = hostList[ :listIndex ] + \ |
| 486 | hostList[ ( listIndex + 1 ): ] |
| 487 | |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 488 | pingResponse += str(str(host) + " -> ") |
| 489 | |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 490 | for temp in pingList: |
| 491 | # Current host pings all other hosts specified |
| 492 | pingCmd = str( host ) + cmd + prefix + str( temp[1:] ) |
Jon Hall | 934576d | 2015-10-09 10:12:22 -0700 | [diff] [blame] | 493 | self.handle.sendline( pingCmd ) |
| 494 | self.handle.expect( "mininet>", timeout=wait + 1 ) |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 495 | response = self.handle.before |
| 496 | if re.search( ',\s0\%\spacket\sloss', response ): |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 497 | pingResponse += str(" h" + str( temp[1:] )) |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 498 | else: |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 499 | pingResponse += " X" |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 500 | # One of the host to host pair is unreachable |
| 501 | isReachable = main.FALSE |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 502 | failedPings += 1 |
GlennRC | d10d3cc | 2015-09-24 12:47:16 -0700 | [diff] [blame] | 503 | pingResponse += "\n" |
GlennRC | 6d50627 | 2015-09-25 11:36:07 -0700 | [diff] [blame] | 504 | main.log.info( pingResponse + "Failed pings: " + str(failedPings) ) |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 505 | return isReachable |
| 506 | |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 507 | except pexpect.TIMEOUT: |
| 508 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 509 | return main.FALSE |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 510 | except pexpect.EOF: |
| 511 | main.log.error( self.name + ": EOF exception found" ) |
| 512 | main.log.error( self.name + ": " + self.handle.before ) |
| 513 | main.cleanup() |
| 514 | main.exit() |
Hari Krishna | 3bf8ea8 | 2015-08-11 09:02:02 -0700 | [diff] [blame] | 515 | except Exception: |
| 516 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 517 | main.cleanup() |
| 518 | main.exit() |
Hari Krishna | 9592fc8 | 2015-07-31 15:11:15 -0700 | [diff] [blame] | 519 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 520 | def pingHost( self, **pingParams ): |
| 521 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 522 | Ping from one mininet host to another |
| 523 | Currently the only supported Params: SRC, TARGET, and WAIT |
| 524 | """ |
| 525 | args = utilities.parse_args( [ "SRC", "TARGET", 'WAIT' ], **pingParams ) |
| 526 | wait = args['WAIT'] |
| 527 | wait = int( wait if wait else 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 528 | command = args[ "SRC" ] + " ping " + \ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 529 | args[ "TARGET" ] + " -c 1 -i 1 -W " + str( wait ) + " " |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 530 | try: |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 531 | main.log.info( "Sending: " + command ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 532 | self.handle.sendline( command ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 533 | i = self.handle.expect( [ command, pexpect.TIMEOUT ], |
| 534 | timeout=wait + 1 ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 535 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 536 | main.log.error( |
| 537 | self.name + |
| 538 | ": timeout when waiting for response from mininet" ) |
| 539 | main.log.error( "response: " + str( self.handle.before ) ) |
| 540 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 541 | if i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 542 | main.log.error( |
| 543 | self.name + |
| 544 | ": timeout when waiting for response from mininet" ) |
| 545 | main.log.error( "response: " + str( self.handle.before ) ) |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 546 | response = self.handle.before |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 547 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 548 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
| 549 | return main.TRUE |
| 550 | else: |
| 551 | main.log.error( |
| 552 | self.name + |
| 553 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 554 | return main.FALSE |
| 555 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 556 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 557 | main.log.error( self.name + ": EOF exception found" ) |
| 558 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 559 | main.cleanup() |
| 560 | main.exit() |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 561 | except Exception: |
| 562 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 563 | main.cleanup() |
| 564 | main.exit() |
| 565 | |
| 566 | def ping6pair( self, **pingParams ): |
| 567 | """ |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 568 | IPv6 Ping between a pair of mininet hosts |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 569 | Currently the only supported Params are: SRC, TARGET, and WAIT |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 570 | FLOWLABEL and -I (src interface) will be added later after running some tests. |
| 571 | Example: main.Mininet1.ping6pair( src="h1", target="1000::2" ) |
| 572 | """ |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 573 | args = utilities.parse_args( [ "SRC", "TARGET", 'WAIT' ], **pingParams ) |
| 574 | wait = args['WAIT'] |
| 575 | wait = int( wait if wait else 1 ) |
| 576 | command = args[ "SRC" ] + " ping " + \ |
| 577 | args[ "TARGET" ] + " -c 1 -i 1 -W " + str( wait ) + " " |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 578 | try: |
| 579 | main.log.info( "Sending: " + command ) |
| 580 | self.handle.sendline( command ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 581 | i = self.handle.expect( [ command, pexpect.TIMEOUT ], |
| 582 | timeout=wait + 1 ) |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 583 | if i == 1: |
| 584 | main.log.error( |
| 585 | self.name + |
| 586 | ": timeout when waiting for response from mininet" ) |
| 587 | main.log.error( "response: " + str( self.handle.before ) ) |
| 588 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
| 589 | if i == 1: |
| 590 | main.log.error( |
| 591 | self.name + |
| 592 | ": timeout when waiting for response from mininet" ) |
| 593 | main.log.error( "response: " + str( self.handle.before ) ) |
| 594 | response = self.handle.before |
| 595 | main.log.info( self.name + ": Ping Response: " + response ) |
| 596 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 597 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
GlennRC | 2cf7d95 | 2015-09-11 16:32:13 -0700 | [diff] [blame] | 598 | return main.TRUE |
Hari Krishna | 012a1c1 | 2015-08-25 14:23:58 -0700 | [diff] [blame] | 599 | else: |
| 600 | main.log.error( |
| 601 | self.name + |
| 602 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 603 | return main.FALSE |
| 604 | |
| 605 | except pexpect.EOF: |
| 606 | main.log.error( self.name + ": EOF exception found" ) |
| 607 | main.log.error( self.name + ": " + self.handle.before ) |
| 608 | main.cleanup() |
| 609 | main.exit() |
| 610 | except Exception: |
| 611 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 612 | main.cleanup() |
| 613 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 614 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 615 | def checkIP( self, host ): |
| 616 | """ |
| 617 | Verifies the host's ip configured or not.""" |
| 618 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 619 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 620 | response = self.execute( |
| 621 | cmd=host + |
| 622 | " ifconfig", |
| 623 | prompt="mininet>", |
| 624 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 625 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 626 | main.log.error( self.name + ": EOF exception found" ) |
| 627 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 628 | main.cleanup() |
| 629 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 630 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 631 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 632 | "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\ |
| 633 | "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\ |
| 634 | "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\ |
| 635 | "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\ |
| 636 | "[0-9]|25[0-5]|[0-9]{1,2})" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 637 | # pattern = "inet addr:10.0.0.6" |
| 638 | if re.search( pattern, response ): |
| 639 | main.log.info( self.name + ": Host Ip configured properly" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 640 | return main.TRUE |
| 641 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 642 | main.log.error( self.name + ": Host IP not found" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 643 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 644 | else: |
| 645 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 646 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 647 | def verifySSH( self, **connectargs ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 648 | # FIXME: Who uses this and what is the purpose? seems very specific |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 649 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 650 | response = self.execute( |
| 651 | cmd="h1 /usr/sbin/sshd -D&", |
| 652 | prompt="mininet>", |
| 653 | timeout=10 ) |
| 654 | response = self.execute( |
| 655 | cmd="h4 /usr/sbin/sshd -D&", |
| 656 | prompt="mininet>", |
| 657 | timeout=10 ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 658 | for key in connectargs: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 659 | vars( self )[ key ] = connectargs[ key ] |
| 660 | response = self.execute( |
| 661 | cmd="xterm h1 h4 ", |
| 662 | prompt="mininet>", |
| 663 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 664 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 665 | main.log.error( self.name + ": EOF exception found" ) |
| 666 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 667 | main.cleanup() |
| 668 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 669 | import time |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 670 | time.sleep( 20 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 671 | if self.flag == 0: |
| 672 | self.flag = 1 |
| 673 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 674 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 675 | return main.TRUE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 676 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 677 | def moveHost( self, host, oldSw, newSw, ): |
| 678 | """ |
| 679 | Moves a host from one switch to another on the fly |
| 680 | Note: The intf between host and oldSw when detached |
| 681 | using detach(), will still show up in the 'net' |
| 682 | cmd, because switch.detach() doesn't affect switch.intfs[] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 683 | ( which is correct behavior since the interfaces |
| 684 | haven't moved ). |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 685 | """ |
| 686 | if self.handle: |
| 687 | try: |
| 688 | # Bring link between oldSw-host down |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 689 | cmd = "py net.configLinkStatus('" + oldSw + "'," + "'" + host +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 690 | "'," + "'down')" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 691 | print "cmd1= ", cmd |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 692 | response = self.execute( cmd=cmd, |
| 693 | prompt="mininet>", |
| 694 | timeout=10 ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 695 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 696 | # Determine hostintf and Oldswitchintf |
| 697 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 698 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 699 | print "cmd2= ", cmd |
| 700 | self.handle.sendline( cmd ) |
| 701 | self.handle.expect( "mininet>" ) |
| 702 | |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 703 | # Determine ip and mac address of the host-oldSw interface |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 704 | cmd = "px ipaddr = hintf.IP()" |
| 705 | print "cmd3= ", cmd |
| 706 | self.handle.sendline( cmd ) |
| 707 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 708 | |
| 709 | cmd = "px macaddr = hintf.MAC()" |
| 710 | print "cmd3= ", cmd |
| 711 | self.handle.sendline( cmd ) |
| 712 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 713 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 714 | # Detach interface between oldSw-host |
| 715 | cmd = "px " + oldSw + ".detach( sintf )" |
| 716 | print "cmd4= ", cmd |
| 717 | self.handle.sendline( cmd ) |
| 718 | self.handle.expect( "mininet>" ) |
| 719 | |
| 720 | # Add link between host-newSw |
| 721 | cmd = "py net.addLink(" + host + "," + newSw + ")" |
| 722 | print "cmd5= ", cmd |
| 723 | self.handle.sendline( cmd ) |
| 724 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 725 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 726 | # Determine hostintf and Newswitchintf |
| 727 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 728 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 729 | print "cmd6= ", cmd |
| 730 | self.handle.sendline( cmd ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 731 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 732 | |
| 733 | # Attach interface between newSw-host |
| 734 | cmd = "px " + newSw + ".attach( sintf )" |
| 735 | print "cmd3= ", cmd |
| 736 | self.handle.sendline( cmd ) |
| 737 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 738 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 739 | # Set ipaddress of the host-newSw interface |
| 740 | cmd = "px " + host + ".setIP( ip = ipaddr, intf = hintf)" |
| 741 | print "cmd7 = ", cmd |
| 742 | self.handle.sendline( cmd ) |
| 743 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 744 | |
| 745 | # Set macaddress of the host-newSw interface |
| 746 | cmd = "px " + host + ".setMAC( mac = macaddr, intf = hintf)" |
| 747 | print "cmd8 = ", cmd |
| 748 | self.handle.sendline( cmd ) |
| 749 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 750 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 751 | cmd = "net" |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 752 | print "cmd9 = ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 753 | self.handle.sendline( cmd ) |
| 754 | self.handle.expect( "mininet>" ) |
| 755 | print "output = ", self.handle.before |
| 756 | |
| 757 | # Determine ipaddress of the host-newSw interface |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 758 | cmd = host + " ifconfig" |
| 759 | print "cmd10= ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 760 | self.handle.sendline( cmd ) |
| 761 | self.handle.expect( "mininet>" ) |
| 762 | print "ifconfig o/p = ", self.handle.before |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 763 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 764 | return main.TRUE |
| 765 | except pexpect.EOF: |
| 766 | main.log.error( self.name + ": EOF exception found" ) |
| 767 | main.log.error( self.name + ": " + self.handle.before ) |
| 768 | return main.FALSE |
| 769 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 770 | def changeIP( self, host, intf, newIP, newNetmask ): |
| 771 | """ |
| 772 | Changes the ip address of a host on the fly |
| 773 | 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] | 774 | if self.handle: |
| 775 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 776 | cmd = host + " ifconfig " + intf + " " + \ |
| 777 | newIP + " " + 'netmask' + " " + newNetmask |
| 778 | self.handle.sendline( cmd ) |
| 779 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 780 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 781 | main.log.info( "response = " + response ) |
| 782 | main.log.info( |
| 783 | "Ip of host " + |
| 784 | host + |
| 785 | " changed to new IP " + |
| 786 | newIP ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 787 | return main.TRUE |
| 788 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 789 | main.log.error( self.name + ": EOF exception found" ) |
| 790 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 791 | return main.FALSE |
| 792 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 793 | def changeDefaultGateway( self, host, newGW ): |
| 794 | """ |
| 795 | Changes the default gateway of a host |
| 796 | Ex: h1 route add default gw 10.0.1.2""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 797 | if self.handle: |
| 798 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 799 | cmd = host + " route add default gw " + newGW |
| 800 | self.handle.sendline( cmd ) |
| 801 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 802 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 803 | main.log.info( "response = " + response ) |
| 804 | main.log.info( |
| 805 | "Default gateway of host " + |
| 806 | host + |
| 807 | " changed to " + |
| 808 | newGW ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 809 | return main.TRUE |
| 810 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 811 | main.log.error( self.name + ": EOF exception found" ) |
| 812 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 813 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 814 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 815 | def addStaticMACAddress( self, host, GW, macaddr ): |
| 816 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 817 | Changes the mac address of a gateway host""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 818 | if self.handle: |
| 819 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 820 | # h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
| 821 | cmd = host + " arp -s " + GW + " " + macaddr |
| 822 | self.handle.sendline( cmd ) |
| 823 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 824 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 825 | main.log.info( "response = " + response ) |
| 826 | main.log.info( |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 827 | "Mac address of gateway " + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 828 | GW + |
| 829 | " changed to " + |
| 830 | macaddr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 831 | return main.TRUE |
| 832 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 833 | main.log.error( self.name + ": EOF exception found" ) |
| 834 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 835 | return main.FALSE |
| 836 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 837 | def verifyStaticGWandMAC( self, host ): |
| 838 | """ |
| 839 | Verify if the static gateway and mac address assignment""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 840 | if self.handle: |
| 841 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 842 | # h1 arp -an |
| 843 | cmd = host + " arp -an " |
| 844 | self.handle.sendline( cmd ) |
| 845 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 846 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 847 | main.log.info( host + " arp -an = " + response ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 848 | return main.TRUE |
| 849 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 850 | main.log.error( self.name + ": EOF exception found" ) |
| 851 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 852 | return main.FALSE |
| 853 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 854 | def getMacAddress( self, host ): |
| 855 | """ |
| 856 | Verifies the host's ip configured or not.""" |
| 857 | if self.handle: |
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=host + |
| 861 | " ifconfig", |
| 862 | prompt="mininet>", |
| 863 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 864 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 865 | main.log.error( self.name + ": EOF exception found" ) |
| 866 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 867 | main.cleanup() |
| 868 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 869 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 870 | 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] | 871 | macAddressSearch = re.search( pattern, response, re.I ) |
| 872 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 873 | main.log.info( |
| 874 | self.name + |
| 875 | ": Mac-Address of Host " + |
| 876 | host + |
| 877 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 878 | macAddress ) |
| 879 | return macAddress |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 880 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 881 | main.log.error( self.name + ": Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 882 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 883 | def getInterfaceMACAddress( self, host, interface ): |
| 884 | """ |
| 885 | Return the IP address of the interface on the given host""" |
| 886 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 887 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 888 | response = self.execute( cmd=host + " ifconfig " + interface, |
| 889 | prompt="mininet>", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 890 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 891 | main.log.error( self.name + ": EOF exception found" ) |
| 892 | main.log.error( self.name + ": " + self.handle.before ) |
| 893 | main.cleanup() |
| 894 | main.exit() |
| 895 | |
| 896 | 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] | 897 | macAddressSearch = re.search( pattern, response, re.I ) |
| 898 | if macAddressSearch is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 899 | main.log.info( "No mac address found in %s" % response ) |
| 900 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 901 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 902 | main.log.info( |
| 903 | "Mac-Address of " + |
| 904 | host + |
| 905 | ":" + |
| 906 | interface + |
| 907 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 908 | macAddress ) |
| 909 | return macAddress |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 910 | else: |
| 911 | main.log.error( "Connection failed to the host" ) |
| 912 | |
| 913 | def getIPAddress( self, host ): |
| 914 | """ |
| 915 | Verifies the host's ip configured or not.""" |
| 916 | if self.handle: |
| 917 | try: |
| 918 | response = self.execute( |
| 919 | cmd=host + |
| 920 | " ifconfig", |
| 921 | prompt="mininet>", |
| 922 | timeout=10 ) |
| 923 | except pexpect.EOF: |
| 924 | main.log.error( self.name + ": EOF exception found" ) |
| 925 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 926 | main.cleanup() |
| 927 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 928 | |
| 929 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 930 | ipAddressSearch = re.search( pattern, response ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 931 | main.log.info( |
| 932 | self.name + |
| 933 | ": IP-Address of Host " + |
| 934 | host + |
| 935 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 936 | ipAddressSearch.group( 1 ) ) |
| 937 | return ipAddressSearch.group( 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 938 | else: |
| 939 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 940 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 941 | def getSwitchDPID( self, switch ): |
| 942 | """ |
| 943 | return the datapath ID of the switch""" |
| 944 | if self.handle: |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 945 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 946 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 947 | response = self.execute( |
| 948 | cmd=cmd, |
| 949 | prompt="mininet>", |
| 950 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 951 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 952 | main.log.error( self.name + ": EOF exception found" ) |
| 953 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 954 | main.cleanup() |
| 955 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 956 | pattern = r'^(?P<dpid>\w)+' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 957 | result = re.search( pattern, response, re.MULTILINE ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 958 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 959 | main.log.info( |
| 960 | "Couldn't find DPID for switch %s, found: %s" % |
| 961 | ( switch, response ) ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 962 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 963 | return str( result.group( 0 ) ).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 964 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 965 | main.log.error( "Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 966 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 967 | def getDPID( self, switch ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 968 | if self.handle: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 969 | self.handle.sendline( "" ) |
| 970 | self.expect( "mininet>" ) |
| 971 | cmd = "py %s.dpid" % switch |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 972 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 973 | response = self.execute( |
| 974 | cmd=cmd, |
| 975 | prompt="mininet>", |
| 976 | timeout=10 ) |
| 977 | self.handle.expect( "mininet>" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 978 | response = self.handle.before |
| 979 | return response |
| 980 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 981 | main.log.error( self.name + ": EOF exception found" ) |
| 982 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 983 | main.cleanup() |
| 984 | main.exit() |
| 985 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 986 | def getInterfaces( self, node ): |
| 987 | """ |
| 988 | return information dict about interfaces connected to the node""" |
| 989 | if self.handle: |
| 990 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 991 | ' % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 992 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 993 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 994 | response = self.execute( |
| 995 | cmd=cmd, |
| 996 | prompt="mininet>", |
| 997 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 998 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 999 | main.log.error( self.name + ": EOF exception found" ) |
| 1000 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1001 | main.cleanup() |
| 1002 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1003 | return response |
| 1004 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1005 | main.log.error( "Connection failed to the node" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1006 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1007 | def dump( self ): |
| 1008 | main.log.info( self.name + ": Dump node info" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1009 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1010 | response = self.execute( |
| 1011 | cmd='dump', |
| 1012 | prompt='mininet>', |
| 1013 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1014 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1015 | main.log.error( self.name + ": EOF exception found" ) |
| 1016 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1017 | main.cleanup() |
| 1018 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 1019 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1020 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1021 | def intfs( self ): |
| 1022 | main.log.info( self.name + ": List interfaces" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1023 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1024 | response = self.execute( |
| 1025 | cmd='intfs', |
| 1026 | prompt='mininet>', |
| 1027 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1028 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1029 | main.log.error( self.name + ": EOF exception found" ) |
| 1030 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1031 | main.cleanup() |
| 1032 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1033 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1034 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1035 | def net( self ): |
| 1036 | main.log.info( self.name + ": List network connections" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1037 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1038 | response = self.execute( cmd='net', prompt='mininet>', timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1039 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1040 | main.log.error( self.name + ": EOF exception found" ) |
| 1041 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1042 | main.cleanup() |
| 1043 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1044 | return response |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1045 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1046 | def links( self ): |
| 1047 | main.log.info( self.name + ": List network links" ) |
| 1048 | try: |
| 1049 | response = self.execute( cmd='links', prompt='mininet>', |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1050 | timeout=20 ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1051 | except pexpect.EOF: |
| 1052 | main.log.error( self.name + ": EOF exception found" ) |
| 1053 | main.log.error( self.name + ": " + self.handle.before ) |
| 1054 | main.cleanup() |
| 1055 | main.exit() |
| 1056 | return response |
| 1057 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1058 | def iperftcpAll(self, hosts, timeout=6): |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1059 | ''' |
| 1060 | Runs the iperftcp function with a given set of hosts and specified timeout. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1061 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1062 | @parm: |
| 1063 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 1064 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 1065 | ''' |
| 1066 | for host1 in hosts: |
| 1067 | for host2 in hosts: |
| 1068 | if host1 != host2: |
| 1069 | if self.iperftcp(host1, host2, timeout) == main.FALSE: |
| 1070 | main.log.error(self.name + ": iperftcp test failed for " + host1 + " and " + host2) |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1071 | |
| 1072 | def iperftcp(self, host1="h1", host2="h2", timeout=6): |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1073 | ''' |
| 1074 | Creates an iperf TCP test between two hosts. Returns main.TRUE if test results |
| 1075 | are valid. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1076 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1077 | @parm: |
| 1078 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 1079 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 1080 | ''' |
| 1081 | main.log.info( self.name + ": Simple iperf TCP test between two hosts" ) |
| 1082 | try: |
| 1083 | # Setup the mininet command |
| 1084 | cmd1 = 'iperf ' + host1 + " " + host2 |
| 1085 | self.handle.sendline( cmd1 ) |
| 1086 | outcome = self.handle.expect( "mininet>", timeout ) |
| 1087 | response = self.handle.before |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1088 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1089 | # checks if there are results in the mininet response |
| 1090 | if "Results:" in response: |
| 1091 | main.log.report(self.name + ": iperf test completed") |
| 1092 | # parse the mn results |
| 1093 | response = response.split("\r\n") |
| 1094 | response = response[len(response)-2] |
| 1095 | response = response.split(": ") |
| 1096 | response = response[len(response)-1] |
| 1097 | response = response.replace("[", "") |
| 1098 | response = response.replace("]", "") |
| 1099 | response = response.replace("\'", "") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1100 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1101 | # this is the bandwith two and from the two hosts |
| 1102 | bandwidth = response.split(", ") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1103 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1104 | # there should be two elements in the bandwidth list |
| 1105 | # ['host1 to host2', 'host2 to host1"] |
| 1106 | if len(bandwidth) == 2: |
| 1107 | main.log.report(self.name + ": iperf test successful") |
| 1108 | return main.TRUE |
| 1109 | else: |
| 1110 | main.log.error(self.name + ": invalid iperf results") |
| 1111 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1112 | else: |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1113 | main.log.error( self.name + ": iperf test failed" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1114 | return main.FALSE |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1115 | except pexpect.TIMEOUT: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1116 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1117 | main.log.error( self.name + " response: " + |
| 1118 | repr ( self.handle.before ) ) |
| 1119 | # NOTE: Send ctrl-c to make sure iperf is done |
| 1120 | self.handle.sendline( "\x03" ) |
| 1121 | self.handle.expect( "Interrupt" ) |
| 1122 | self.handle.expect( "mininet>" ) |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1123 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1124 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1125 | main.log.error( self.name + ": EOF exception found" ) |
| 1126 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1127 | main.cleanup() |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1128 | main.exit() |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1129 | |
| 1130 | def iperfudpAll(self, hosts, bandwidth="10M"): |
| 1131 | ''' |
| 1132 | Runs the iperfudp function with a given set of hosts and specified |
| 1133 | bandwidth |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1134 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1135 | @param: |
| 1136 | bandwidth: the targeted bandwidth, in megabits ('M') |
| 1137 | ''' |
| 1138 | for host1 in hosts: |
| 1139 | for host2 in hosts: |
| 1140 | if host1 != host2: |
| 1141 | if self.iperfudp(host1, host2, bandwidth) == main.FALSE: |
| 1142 | main.log.error(self.name + ": iperfudp test failed for " + host1 + " and " + host2) |
| 1143 | |
| 1144 | def iperfudp( self, bandwidth="10M", host1="h1", host2="h2"): |
| 1145 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1146 | ''' |
| 1147 | Creates an iperf UDP test with a specific bandwidth. |
| 1148 | Returns true if results are valid. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1149 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1150 | @param: |
| 1151 | bandwidth: the targeted bandwidth, in megabits ('M'), to run the test |
| 1152 | ''' |
| 1153 | main.log.info(self.name + ": Simple iperf UDP test between two hosts") |
| 1154 | try: |
| 1155 | # setup the mininet command |
| 1156 | cmd = 'iperfudp ' + bandwidth + " " + host1 + " " + host2 |
| 1157 | self.handle.sendline(cmd) |
| 1158 | self.handle.expect("mininet>") |
| 1159 | response = self.handle.before |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1160 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1161 | # check if there are in results in the mininet response |
| 1162 | if "Results:" in response: |
| 1163 | main.log.report(self.name + ": iperfudp test completed") |
| 1164 | # parse the results |
| 1165 | response = response.split("\r\n") |
| 1166 | response = response[len(response)-2] |
| 1167 | response = response.split(": ") |
| 1168 | response = response[len(response)-1] |
| 1169 | response = response.replace("[", "") |
| 1170 | response = response.replace("]", "") |
| 1171 | response = response.replace("\'", "") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1172 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1173 | mnBandwidth = response.split(", ") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1174 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1175 | # check to see if there are at least three entries |
| 1176 | # ['bandwidth', 'host1 to host2', 'host2 to host1'] |
| 1177 | if len(mnBandwidth) == 3: |
| 1178 | # if one entry is blank then something is wrong |
| 1179 | for item in mnBandwidth: |
| 1180 | if item == "": |
| 1181 | main.log.error(self.name + ": Could not parse iperf output") |
| 1182 | main.log.error(self.name + ": invalid iperfudp results") |
| 1183 | return main.FALSE |
| 1184 | # otherwise results are vaild |
| 1185 | main.log.report(self.name + ": iperfudp test successful") |
| 1186 | return main.TRUE |
| 1187 | else: |
| 1188 | main.log.error(self.name + ": invalid iperfudp results") |
| 1189 | return main.FALSE |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1190 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1191 | except pexpect.EOF: |
| 1192 | main.log.error( self.name + ": EOF exception found" ) |
| 1193 | main.log.error( self.name + ": " + self.handle.before ) |
| 1194 | main.cleanup() |
| 1195 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1196 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1197 | def nodes( self ): |
| 1198 | main.log.info( self.name + ": List all nodes." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1199 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1200 | response = self.execute( |
| 1201 | cmd='nodes', |
| 1202 | prompt='mininet>', |
| 1203 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1204 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1205 | main.log.error( self.name + ": EOF exception found" ) |
| 1206 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1207 | main.cleanup() |
| 1208 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1209 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1210 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1211 | def pingpair( self ): |
| 1212 | main.log.info( self.name + ": Ping between first two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1213 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1214 | response = self.execute( |
| 1215 | cmd='pingpair', |
| 1216 | prompt='mininet>', |
| 1217 | timeout=20 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1218 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1219 | main.log.error( self.name + ": EOF exception found" ) |
| 1220 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1221 | main.cleanup() |
| 1222 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1223 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1224 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 1225 | main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1226 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1227 | else: |
| 1228 | main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1229 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1230 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1231 | def link( self, **linkargs ): |
| 1232 | """ |
| 1233 | Bring link( s ) between two nodes up or down""" |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1234 | args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1235 | end1 = args[ "END1" ] if args[ "END1" ] is not None else "" |
| 1236 | end2 = args[ "END2" ] if args[ "END2" ] is not None else "" |
| 1237 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1238 | main.log.info( |
| 1239 | "Bring link between '" + |
| 1240 | end1 + |
| 1241 | "' and '" + |
| 1242 | end2 + |
| 1243 | "' '" + |
| 1244 | option + |
| 1245 | "'" ) |
| 1246 | command = "link " + \ |
| 1247 | str( end1 ) + " " + str( end2 ) + " " + str( option ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1248 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1249 | self.handle.sendline( command ) |
| 1250 | self.handle.expect( "mininet>" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1251 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1252 | main.log.error( self.name + ": EOF exception found" ) |
| 1253 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1254 | main.cleanup() |
| 1255 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1256 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1257 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1258 | def switch( self, **switchargs ): |
| 1259 | """ |
| 1260 | start/stop a switch |
| 1261 | """ |
| 1262 | args = utilities.parse_args( [ "SW", "OPTION" ], **switchargs ) |
| 1263 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1264 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1265 | command = "switch " + str( sw ) + " " + str( option ) |
| 1266 | main.log.info( command ) |
| 1267 | try: |
| 1268 | self.handle.sendline( command ) |
| 1269 | self.handle.expect( "mininet>" ) |
| 1270 | except pexpect.TIMEOUT: |
| 1271 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1272 | main.cleanup() |
| 1273 | main.exit() |
| 1274 | except pexpect.EOF: |
| 1275 | main.log.error( self.name + ": EOF exception found" ) |
| 1276 | main.log.error( self.name + ": " + self.handle.before ) |
| 1277 | main.cleanup() |
| 1278 | main.exit() |
| 1279 | return main.TRUE |
| 1280 | |
pingping-lin | 5bb663b | 2015-09-24 11:47:50 -0700 | [diff] [blame] | 1281 | def node( self, nodeName, commandStr ): |
| 1282 | """ |
| 1283 | Carry out a command line on a given node |
| 1284 | @parm: |
| 1285 | nodeName: the node name in Mininet testbed |
| 1286 | commandStr: the command line will be carried out on the node |
| 1287 | Example: main.Mininet.node( nodeName="h1", commandStr="ls" ) |
| 1288 | """ |
| 1289 | command = str( nodeName ) + " " + str( commandStr ) |
| 1290 | main.log.info( command ) |
| 1291 | |
| 1292 | try: |
| 1293 | response = self.execute( cmd = command, prompt = "mininet>" ) |
| 1294 | if re.search( "Unknown command", response ): |
| 1295 | main.log.warn( response ) |
| 1296 | return main.FALSE |
| 1297 | except pexpect.TIMEOUT: |
| 1298 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1299 | main.cleanup() |
| 1300 | main.exit() |
| 1301 | except pexpect.EOF: |
| 1302 | main.log.error( self.name + ": EOF exception found" ) |
| 1303 | main.log.error( self.name + ": " + self.handle.before ) |
| 1304 | main.cleanup() |
| 1305 | main.exit() |
| 1306 | main.log.info( " response is :" ) |
| 1307 | main.log.info( response ) |
| 1308 | return response |
| 1309 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1310 | def yank( self, **yankargs ): |
| 1311 | """ |
| 1312 | yank a mininet switch interface to a host""" |
| 1313 | main.log.info( 'Yank the switch interface attached to a host' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1314 | args = utilities.parse_args( [ "SW", "INTF" ], **yankargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1315 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1316 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1317 | command = "py " + str( sw ) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1318 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1319 | response = self.execute( |
| 1320 | cmd=command, |
| 1321 | prompt="mininet>", |
| 1322 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1323 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1324 | main.log.error( self.name + ": EOF exception found" ) |
| 1325 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1326 | main.cleanup() |
| 1327 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 1328 | return main.TRUE |
| 1329 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1330 | def plug( self, **plugargs ): |
| 1331 | """ |
| 1332 | plug the yanked mininet switch interface to a switch""" |
| 1333 | main.log.info( 'Plug the switch interface attached to a switch' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1334 | args = utilities.parse_args( [ "SW", "INTF" ], **plugargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1335 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1336 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1337 | command = "py " + str( sw ) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1338 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1339 | response = self.execute( |
| 1340 | cmd=command, |
| 1341 | prompt="mininet>", |
| 1342 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1343 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1344 | main.log.error( self.name + ": EOF exception found" ) |
| 1345 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1346 | main.cleanup() |
| 1347 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1348 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1349 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1350 | def dpctl( self, **dpctlargs ): |
| 1351 | """ |
| 1352 | Run dpctl command on all switches.""" |
| 1353 | main.log.info( 'Run dpctl command on all switches' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1354 | args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1355 | cmd = args[ "CMD" ] if args[ "CMD" ] is not None else "" |
| 1356 | cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else "" |
| 1357 | command = "dpctl " + cmd + " " + str( cmdargs ) |
| 1358 | try: |
| 1359 | response = self.execute( |
| 1360 | cmd=command, |
| 1361 | prompt="mininet>", |
| 1362 | timeout=10 ) |
| 1363 | except pexpect.EOF: |
| 1364 | main.log.error( self.name + ": EOF exception found" ) |
| 1365 | main.log.error( self.name + ": " + self.handle.before ) |
| 1366 | main.cleanup() |
| 1367 | main.exit() |
| 1368 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1369 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1370 | def getVersion( self ): |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1371 | # FIXME: What uses this? This should be refactored to get |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1372 | # version from MN and not some other file |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1373 | fileInput = path + '/lib/Mininet/INSTALL' |
| 1374 | version = super( Mininet, self ).getVersion() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1375 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1376 | for line in open( fileInput, 'r' ).readlines(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1377 | result = re.match( pattern, line ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1378 | if result: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1379 | version = result.group( 0 ) |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1380 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1381 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1382 | def getSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1383 | """ |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1384 | Parameters: |
| 1385 | sw: The name of an OVS switch. Example "s1" |
| 1386 | Return: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1387 | The output of the command from the mininet cli |
| 1388 | or main.FALSE on timeout""" |
| 1389 | command = "sh ovs-vsctl get-controller " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1390 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1391 | response = self.execute( |
| 1392 | cmd=command, |
| 1393 | prompt="mininet>", |
| 1394 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1395 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1396 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1397 | else: |
| 1398 | return main.FALSE |
| 1399 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1400 | main.log.error( self.name + ": EOF exception found" ) |
| 1401 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1402 | main.cleanup() |
| 1403 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1404 | |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1405 | def assignSwController( self, sw, ip, port="6653", ptcp="" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1406 | """ |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1407 | Description: |
| 1408 | Assign switches to the controllers ( for ovs use only ) |
| 1409 | Required: |
| 1410 | sw - Name of the switch. This can be a list or a string. |
| 1411 | ip - Ip addresses of controllers. This can be a list or a string. |
| 1412 | Optional: |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1413 | port - ONOS use port 6653, if no list of ports is passed, then |
| 1414 | the all the controller will use 6653 as their port number |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1415 | ptcp - ptcp number, This can be a string or a list that has |
| 1416 | the same length as switch. This is optional and not required |
| 1417 | when using ovs switches. |
| 1418 | NOTE: If switches and ptcp are given in a list type they should have the |
| 1419 | same length and should be in the same order, Eg. sw=[ 's1' ... n ] |
| 1420 | ptcp=[ '6637' ... n ], s1 has ptcp number 6637 and so on. |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1421 | |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1422 | Return: |
| 1423 | Returns main.TRUE if mininet correctly assigned switches to |
| 1424 | controllers, otherwise it will return main.FALSE or an appropriate |
| 1425 | exception(s) |
| 1426 | """ |
| 1427 | assignResult = main.TRUE |
| 1428 | # Initial ovs command |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1429 | commandList = [] |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1430 | command = "sh ovs-vsctl set-controller " |
| 1431 | onosIp = "" |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1432 | try: |
| 1433 | if isinstance( ip, types.StringType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1434 | onosIp = "tcp:" + str( ip ) + ":" |
kelvin-onlab | 5c2df43 | 2015-06-11 17:29:56 -0700 | [diff] [blame] | 1435 | if isinstance( port, types.StringType ) or \ |
| 1436 | isinstance( port, types.IntType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1437 | onosIp += str( port ) |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1438 | elif isinstance( port, types.ListType ): |
| 1439 | main.log.error( self.name + ": Only one controller " + |
| 1440 | "assigned and a list of ports has" + |
| 1441 | " been passed" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1442 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1443 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1444 | main.log.error( self.name + ": Invalid controller port " + |
| 1445 | "number. Please specify correct " + |
| 1446 | "controller port" ) |
| 1447 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1448 | |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1449 | elif isinstance( ip, types.ListType ): |
kelvin-onlab | 5c2df43 | 2015-06-11 17:29:56 -0700 | [diff] [blame] | 1450 | if isinstance( port, types.StringType ) or \ |
| 1451 | isinstance( port, types.IntType ): |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1452 | for ipAddress in ip: |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1453 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 1454 | str( port ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1455 | elif isinstance( port, types.ListType ): |
| 1456 | if ( len( ip ) != len( port ) ): |
| 1457 | main.log.error( self.name + ": Port list = " + |
| 1458 | str( len( port ) ) + |
| 1459 | "should be the same as controller" + |
| 1460 | " ip list = " + str( len( ip ) ) ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1461 | return main.FALSE |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1462 | else: |
| 1463 | onosIp = "" |
| 1464 | for ipAddress, portNum in zip( ip, port ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1465 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 1466 | str( portNum ) + " " |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1467 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1468 | main.log.error( self.name + ": Invalid controller port " + |
| 1469 | "number. Please specify correct " + |
| 1470 | "controller port" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1471 | return main.FALSE |
kelvin-onlab | e5edb9e | 2015-06-12 09:38:47 -0700 | [diff] [blame] | 1472 | else: |
| 1473 | main.log.error( self.name + ": Invalid ip address" ) |
| 1474 | return main.FALSE |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1475 | |
| 1476 | if isinstance( sw, types.StringType ): |
| 1477 | command += sw + " " |
| 1478 | if ptcp: |
| 1479 | if isinstance( ptcp, types.StringType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1480 | command += "ptcp:" + str( ptcp ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1481 | elif isinstance( ptcp, types.ListType ): |
| 1482 | main.log.error( self.name + ": Only one switch is " + |
| 1483 | "being set and multiple PTCP is " + |
| 1484 | "being passed " ) |
| 1485 | else: |
| 1486 | main.log.error( self.name + ": Invalid PTCP" ) |
| 1487 | ptcp = "" |
| 1488 | command += onosIp |
| 1489 | commandList.append( command ) |
| 1490 | |
| 1491 | elif isinstance( sw, types.ListType ): |
| 1492 | if ptcp: |
| 1493 | if isinstance( ptcp, types.ListType ): |
| 1494 | if len( ptcp ) != len( sw ): |
| 1495 | main.log.error( self.name + ": PTCP length = " + |
| 1496 | str( len( ptcp ) ) + |
| 1497 | " is not the same as switch" + |
| 1498 | " length = " + |
| 1499 | str( len( sw ) ) ) |
| 1500 | return main.FALSE |
| 1501 | else: |
| 1502 | for switch, ptcpNum in zip( sw, ptcp ): |
| 1503 | tempCmd = "sh ovs-vsctl set-controller " |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1504 | tempCmd += switch + " ptcp:" + \ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1505 | str( ptcpNum ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1506 | tempCmd += onosIp |
| 1507 | commandList.append( tempCmd ) |
| 1508 | else: |
| 1509 | main.log.error( self.name + ": Invalid PTCP" ) |
| 1510 | return main.FALSE |
| 1511 | else: |
| 1512 | for switch in sw: |
| 1513 | tempCmd = "sh ovs-vsctl set-controller " |
| 1514 | tempCmd += switch + " " + onosIp |
| 1515 | commandList.append( tempCmd ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1516 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1517 | main.log.error( self.name + ": Invalid switch type " ) |
| 1518 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1519 | |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1520 | for cmd in commandList: |
| 1521 | try: |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1522 | self.execute( cmd=cmd, prompt="mininet>", timeout=5 ) |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1523 | except pexpect.TIMEOUT: |
| 1524 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1525 | return main.FALSE |
| 1526 | except pexpect.EOF: |
| 1527 | main.log.error( self.name + ": EOF exception found" ) |
| 1528 | main.log.error( self.name + ": " + self.handle.before ) |
| 1529 | main.cleanup() |
| 1530 | main.exit() |
| 1531 | return main.TRUE |
| 1532 | except Exception: |
| 1533 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1534 | main.cleanup() |
| 1535 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1536 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1537 | def deleteSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1538 | """ |
| 1539 | Removes the controller target from sw""" |
| 1540 | command = "sh ovs-vsctl del-controller " + str( sw ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1541 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1542 | response = self.execute( |
| 1543 | cmd=command, |
| 1544 | prompt="mininet>", |
| 1545 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1546 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1547 | main.log.error( self.name + ": EOF exception found" ) |
| 1548 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1549 | main.cleanup() |
| 1550 | main.exit() |
| 1551 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1552 | main.log.info( response ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1553 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1554 | def addSwitch( self, sw, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1555 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1556 | adds a switch to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1557 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1558 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1559 | NOTE: cannot currently specify what type of switch |
| 1560 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1561 | sw = name of the new switch as a string |
| 1562 | optional keywords: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1563 | dpid = "dpid" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1564 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1565 | """ |
| 1566 | dpid = kwargs.get( 'dpid', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1567 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1568 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1569 | response = self.execute( |
| 1570 | cmd=command, |
| 1571 | prompt="mininet>", |
| 1572 | timeout=10 ) |
| 1573 | if re.search( "already exists!", response ): |
| 1574 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1575 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1576 | elif re.search( "Error", response ): |
| 1577 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1578 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1579 | elif re.search( "usage:", response ): |
| 1580 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1581 | return main.FALSE |
| 1582 | else: |
| 1583 | return main.TRUE |
| 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" ) |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1586 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1587 | main.cleanup() |
| 1588 | main.exit() |
| 1589 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1590 | def delSwitch( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1591 | """ |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1592 | delete a switch from the mininet topology |
| 1593 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1594 | dynamic_topo branch |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1595 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1596 | sw = name of the switch as a string |
| 1597 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1598 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1599 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1600 | response = self.execute( |
| 1601 | cmd=command, |
| 1602 | prompt="mininet>", |
| 1603 | timeout=10 ) |
| 1604 | if re.search( "no switch named", response ): |
| 1605 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1606 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1607 | elif re.search( "Error", response ): |
| 1608 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1609 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1610 | elif re.search( "usage:", response ): |
| 1611 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1612 | return main.FALSE |
| 1613 | else: |
| 1614 | return main.TRUE |
| 1615 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1616 | main.log.error( self.name + ": EOF exception found" ) |
| 1617 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1618 | main.cleanup() |
| 1619 | main.exit() |
| 1620 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1621 | def addLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1622 | """ |
| 1623 | add a link to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1624 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1625 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1626 | NOTE: cannot currently specify what type of link |
| 1627 | required params: |
| 1628 | node1 = the string node name of the first endpoint of the link |
| 1629 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1630 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1631 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1632 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1633 | response = self.execute( |
| 1634 | cmd=command, |
| 1635 | prompt="mininet>", |
| 1636 | timeout=10 ) |
| 1637 | if re.search( "doesnt exist!", response ): |
| 1638 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1639 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1640 | elif re.search( "Error", response ): |
| 1641 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1642 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1643 | elif re.search( "usage:", response ): |
| 1644 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1645 | return main.FALSE |
| 1646 | else: |
| 1647 | return main.TRUE |
| 1648 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1649 | main.log.error( self.name + ": EOF exception found" ) |
| 1650 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1651 | main.cleanup() |
| 1652 | main.exit() |
| 1653 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1654 | def delLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1655 | """ |
| 1656 | delete a link from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1657 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1658 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1659 | required params: |
| 1660 | node1 = the string node name of the first endpoint of the link |
| 1661 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1662 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1663 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1664 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1665 | response = self.execute( |
| 1666 | cmd=command, |
| 1667 | prompt="mininet>", |
| 1668 | timeout=10 ) |
| 1669 | if re.search( "no node named", response ): |
| 1670 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1671 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1672 | elif re.search( "Error", response ): |
| 1673 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1674 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1675 | elif re.search( "usage:", response ): |
| 1676 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1677 | return main.FALSE |
| 1678 | else: |
| 1679 | return main.TRUE |
| 1680 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1681 | main.log.error( self.name + ": EOF exception found" ) |
| 1682 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1683 | main.cleanup() |
| 1684 | main.exit() |
| 1685 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1686 | def addHost( self, hostname, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1687 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1688 | Add a host to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1689 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1690 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1691 | NOTE: cannot currently specify what type of host |
| 1692 | required params: |
| 1693 | hostname = the string hostname |
| 1694 | optional key-value params |
| 1695 | switch = "switch name" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1696 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1697 | """ |
| 1698 | switch = kwargs.get( 'switch', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1699 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1700 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1701 | response = self.execute( |
| 1702 | cmd=command, |
| 1703 | prompt="mininet>", |
| 1704 | timeout=10 ) |
| 1705 | if re.search( "already exists!", response ): |
| 1706 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1707 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1708 | elif re.search( "doesnt exists!", response ): |
| 1709 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1710 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1711 | elif re.search( "Error", response ): |
| 1712 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1713 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1714 | elif re.search( "usage:", response ): |
| 1715 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1716 | return main.FALSE |
| 1717 | else: |
| 1718 | return main.TRUE |
| 1719 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1720 | main.log.error( self.name + ": EOF exception found" ) |
| 1721 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1722 | main.cleanup() |
| 1723 | main.exit() |
| 1724 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1725 | def delHost( self, hostname ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1726 | """ |
| 1727 | delete a host from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1728 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1729 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1730 | NOTE: this uses a custom mn function |
| 1731 | required params: |
| 1732 | hostname = the string hostname |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1733 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1734 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1735 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1736 | response = self.execute( |
| 1737 | cmd=command, |
| 1738 | prompt="mininet>", |
| 1739 | timeout=10 ) |
| 1740 | if re.search( "no host named", response ): |
| 1741 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1742 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1743 | elif re.search( "Error", response ): |
| 1744 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1745 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1746 | elif re.search( "usage:", response ): |
| 1747 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1748 | return main.FALSE |
| 1749 | else: |
| 1750 | return main.TRUE |
| 1751 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1752 | main.log.error( self.name + ": EOF exception found" ) |
| 1753 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1754 | main.cleanup() |
| 1755 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1756 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1757 | def disconnect( self ): |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1758 | """ |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1759 | Called at the end of the test to stop the mininet and |
| 1760 | disconnect the handle. |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1761 | """ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1762 | self.handle.sendline( '' ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1763 | i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ], |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1764 | timeout=2 ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1765 | response = main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1766 | if i == 0: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1767 | response = self.stopNet() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1768 | elif i == 1: |
| 1769 | return main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1770 | # print "Disconnecting Mininet" |
| 1771 | if self.handle: |
| 1772 | self.handle.sendline( "exit" ) |
| 1773 | self.handle.expect( "exit" ) |
| 1774 | self.handle.expect( "(.*)" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1775 | else: |
| 1776 | main.log.error( "Connection failed to the host" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1777 | return response |
| 1778 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1779 | def stopNet( self, fileName="", timeout=5 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1780 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1781 | Stops mininet. |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1782 | Returns main.TRUE if the mininet successfully stops and |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1783 | main.FALSE if the pexpect handle does not exist. |
| 1784 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1785 | Will cleanup and exit the test if mininet fails to stop |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1786 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1787 | main.log.info( self.name + ": Stopping mininet..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1788 | response = '' |
| 1789 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1790 | try: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1791 | self.handle.sendline( "" ) |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1792 | i = self.handle.expect( [ 'mininet>', |
| 1793 | '\$', |
| 1794 | pexpect.EOF, |
| 1795 | pexpect.TIMEOUT ], |
| 1796 | timeout ) |
| 1797 | if i == 0: |
| 1798 | main.log.info( "Exiting mininet..." ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1799 | response = self.execute( |
| 1800 | cmd="exit", |
| 1801 | prompt="(.*)", |
| 1802 | timeout=120 ) |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1803 | main.log.info( self.name + ": Stopped" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1804 | self.handle.sendline( "sudo mn -c" ) |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 1805 | response = main.TRUE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1806 | |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1807 | if i == 1: |
| 1808 | main.log.info( " Mininet trying to exit while not " + |
| 1809 | "in the mininet prompt" ) |
| 1810 | elif i == 2: |
| 1811 | main.log.error( "Something went wrong exiting mininet" ) |
| 1812 | elif i == 3: # timeout |
| 1813 | main.log.error( "Something went wrong exiting mininet " + |
| 1814 | "TIMEOUT" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1815 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1816 | if fileName: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1817 | self.handle.sendline( "" ) |
| 1818 | self.handle.expect( '\$' ) |
| 1819 | self.handle.sendline( |
| 1820 | "sudo kill -9 \`ps -ef | grep \"" + |
| 1821 | fileName + |
| 1822 | "\" | grep -v grep | awk '{print $2}'\`" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1823 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1824 | main.log.error( self.name + ": EOF exception found" ) |
| 1825 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1826 | main.cleanup() |
| 1827 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1828 | else: |
| 1829 | main.log.error( self.name + ": Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1830 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1831 | return response |
| 1832 | |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1833 | def arping( self, srcHost="", dstHost="10.128.20.211", ethDevice="" ): |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1834 | """ |
| 1835 | Description: |
| 1836 | Sends arp message from mininet host for hosts discovery |
| 1837 | Required: |
| 1838 | host - hosts name |
| 1839 | Optional: |
| 1840 | ip - ip address that does not exist in the network so there would |
| 1841 | be no reply. |
| 1842 | """ |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 1843 | if ethDevice: |
| 1844 | ethDevice = '-I ' + ethDevice + ' ' |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1845 | cmd = srcHost + " arping -c1 " + ethDevice + dstHost |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1846 | try: |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1847 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1848 | self.handle.sendline( cmd ) |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1849 | i = self.handle.expect( [ "mininet>", "arping: " ] ) |
| 1850 | if i == 0: |
| 1851 | return main.TRUE |
| 1852 | elif i == 1: |
| 1853 | response = self.handle.before + self.handle.after |
| 1854 | self.handle.expect( "mininet>" ) |
| 1855 | response += self.handle.before + self.handle.after |
| 1856 | main.log.warn( "Error sending arping, output was: " + |
| 1857 | response ) |
| 1858 | return main.FALSE |
| 1859 | except pexpect.TIMEOUT: |
| 1860 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1861 | main.log.warn( self.handle.before ) |
| 1862 | return main.FALSE |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1863 | except pexpect.EOF: |
| 1864 | main.log.error( self.name + ": EOF exception found" ) |
| 1865 | main.log.error( self.name + ": " + self.handle.before ) |
| 1866 | main.cleanup() |
| 1867 | main.exit() |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1868 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1869 | def decToHex( self, num ): |
| 1870 | return hex( num ).split( 'x' )[ 1 ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1871 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1872 | def getSwitchFlowCount( self, switch ): |
| 1873 | """ |
| 1874 | return the Flow Count of the switch""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1875 | if self.handle: |
| 1876 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 1877 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1878 | response = self.execute( |
| 1879 | cmd=cmd, |
| 1880 | prompt="mininet>", |
| 1881 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1882 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1883 | main.log.error( self.name + ": EOF exception found" ) |
| 1884 | main.log.error( self.name + " " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1885 | main.cleanup() |
| 1886 | main.exit() |
| 1887 | pattern = "flow_count=(\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1888 | result = re.search( pattern, response, re.MULTILINE ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1889 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1890 | main.log.info( |
| 1891 | "Couldn't find flows on switch %s, found: %s" % |
| 1892 | ( switch, response ) ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1893 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1894 | return result.group( 1 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1895 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1896 | main.log.error( "Connection failed to the Mininet host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1897 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1898 | def checkFlows( self, sw, dumpFormat=None ): |
| 1899 | if dumpFormat: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1900 | command = "sh ovs-ofctl -F " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1901 | dumpFormat + " dump-flows " + str( sw ) |
Ahmed El-Hassany | b6545eb | 2014-08-01 11:32:10 -0700 | [diff] [blame] | 1902 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1903 | command = "sh ovs-ofctl dump-flows " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1904 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1905 | response = self.execute( |
| 1906 | cmd=command, |
| 1907 | prompt="mininet>", |
| 1908 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1909 | return response |
| 1910 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1911 | main.log.error( self.name + ": EOF exception found" ) |
| 1912 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1913 | main.cleanup() |
| 1914 | main.exit() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1915 | |
Jon Hall | 9043c90 | 2015-07-30 14:23:44 -0700 | [diff] [blame] | 1916 | def getFlowTable( self, protoVersion, sw ): |
| 1917 | """ |
| 1918 | Returns certain fields of an OVS flow table. Will force output to |
| 1919 | either OF 1.0 or 1.3 format for consistency. |
| 1920 | |
| 1921 | TODO add option to look at cookies. ignoring them for now |
| 1922 | |
| 1923 | NOTE: Use format to force consistent flow table output across |
| 1924 | versions |
| 1925 | """ |
| 1926 | try: |
| 1927 | self.handle.sendline( "" ) |
| 1928 | self.handle.expect( "mininet>" ) |
| 1929 | command = "sh ovs-ofctl dump-flows " + sw |
| 1930 | if protoVersion == 1.0: |
| 1931 | command += " -F OpenFlow10-table_id | awk '{OFS=\",\" ;" +\ |
| 1932 | " print $1 $3 $6 $7 $8}' | " |
| 1933 | elif protoVersion == 1.3: |
| 1934 | command += " -O OpenFlow13 | awk '{OFS=\",\" ;" +\ |
| 1935 | " print $1 $3 $6 $7}' | " |
| 1936 | else: |
| 1937 | main.log.error( |
| 1938 | "Unknown protoVersion in getFlowTable(). given: (" + |
| 1939 | str( type( protoVersion ) ) + |
| 1940 | ") '" + str( protoVersion ) + "'" ) |
| 1941 | return None |
| 1942 | command += "cut -d ',' -f 2- | sort -n -k1 -r" |
| 1943 | self.handle.sendline( command ) |
| 1944 | self.handle.expect( "sort" ) |
| 1945 | self.handle.expect( "OFPST_FLOW" ) |
| 1946 | response = self.handle.before |
| 1947 | return response |
| 1948 | except pexpect.EOF: |
| 1949 | main.log.error( self.name + ": EOF exception found" ) |
| 1950 | main.log.error( self.name + ": " + self.handle.before ) |
| 1951 | main.cleanup() |
| 1952 | main.exit() |
| 1953 | except pexpect.TIMEOUT: |
| 1954 | main.log.exception( self.name + ": Timeout exception: " ) |
| 1955 | return None |
| 1956 | |
| 1957 | def flowComp( self, flow1, flow2 ): |
| 1958 | if flow1 == flow2: |
| 1959 | return main.TRUE |
| 1960 | else: |
| 1961 | main.log.info( "Flow tables do not match, printing tables:" ) |
| 1962 | main.log.info( "Flow Table 1:" ) |
| 1963 | main.log.info( flow1 ) |
| 1964 | main.log.info( "Flow Table 2:" ) |
| 1965 | main.log.info( flow2 ) |
| 1966 | return main.FALSE |
| 1967 | |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1968 | def startTcpdump( self, filename, intf="eth0", port="port 6653" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1969 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1970 | Runs tpdump on an interface and saves the file |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1971 | intf can be specified, or the default eth0 is used""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1972 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1973 | self.handle.sendline( "" ) |
| 1974 | self.handle.expect( "mininet>" ) |
| 1975 | self.handle.sendline( |
| 1976 | "sh sudo tcpdump -n -i " + |
| 1977 | intf + |
| 1978 | " " + |
| 1979 | port + |
| 1980 | " -w " + |
| 1981 | filename.strip() + |
| 1982 | " &" ) |
| 1983 | self.handle.sendline( "" ) |
| 1984 | i = self.handle.expect( [ 'No\ssuch\device', |
| 1985 | 'listening\son', |
| 1986 | pexpect.TIMEOUT, |
| 1987 | "mininet>" ], |
| 1988 | timeout=10 ) |
| 1989 | main.log.warn( self.handle.before + self.handle.after ) |
| 1990 | self.handle.sendline( "" ) |
| 1991 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1992 | if i == 0: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1993 | main.log.error( |
| 1994 | self.name + |
| 1995 | ": tcpdump - No such device exists. " + |
| 1996 | "tcpdump attempted on: " + |
| 1997 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1998 | return main.FALSE |
| 1999 | elif i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2000 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2001 | return main.TRUE |
| 2002 | elif i == 2: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2003 | main.log.error( |
| 2004 | self.name + |
| 2005 | ": tcpdump command timed out! Check interface name," + |
| 2006 | " given interface was: " + |
| 2007 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2008 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2009 | elif i == 3: |
| 2010 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2011 | return main.TRUE |
| 2012 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2013 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2014 | return main.FALSE |
| 2015 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2016 | main.log.error( self.name + ": EOF exception found" ) |
| 2017 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2018 | main.cleanup() |
| 2019 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2020 | except Exception: |
| 2021 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2022 | main.cleanup() |
| 2023 | main.exit() |
| 2024 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2025 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2026 | """ |
| 2027 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2028 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2029 | self.handle.sendline( "sh sudo pkill tcpdump" ) |
| 2030 | self.handle.expect( "mininet>" ) |
| 2031 | self.handle.sendline( "" ) |
| 2032 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2033 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2034 | main.log.error( self.name + ": EOF exception found" ) |
| 2035 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2036 | main.cleanup() |
| 2037 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2038 | except Exception: |
| 2039 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2040 | main.cleanup() |
| 2041 | main.exit() |
| 2042 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2043 | def getPorts( self, nodeName, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2044 | """ |
| 2045 | Read ports from a Mininet switch. |
| 2046 | |
| 2047 | Returns a json structure containing information about the |
| 2048 | ports of the given switch. |
| 2049 | """ |
| 2050 | response = self.getInterfaces( nodeName ) |
| 2051 | # TODO: Sanity check on response. log if no such switch exists |
| 2052 | ports = [] |
| 2053 | for line in response.split( "\n" ): |
| 2054 | if not line.startswith( "name=" ): |
| 2055 | continue |
| 2056 | portVars = {} |
| 2057 | for var in line.split( "," ): |
| 2058 | key, value = var.split( "=" ) |
| 2059 | portVars[ key ] = value |
| 2060 | isUp = portVars.pop( 'enabled', "True" ) |
| 2061 | isUp = "True" in isUp |
| 2062 | if verbose: |
| 2063 | main.log.info( "Reading switch port %s(%s)" % |
| 2064 | ( portVars[ 'name' ], portVars[ 'mac' ] ) ) |
| 2065 | mac = portVars[ 'mac' ] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2066 | if mac == 'None': |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2067 | mac = None |
| 2068 | ips = [] |
| 2069 | ip = portVars[ 'ip' ] |
| 2070 | if ip == 'None': |
| 2071 | ip = None |
| 2072 | ips.append( ip ) |
| 2073 | name = portVars[ 'name' ] |
| 2074 | if name == 'None': |
| 2075 | name = None |
| 2076 | portRe = r'[^\-]\d\-eth(?P<port>\d+)' |
| 2077 | if name == 'lo': |
| 2078 | portNo = 0xfffe # TODO: 1.0 value - Should we just return lo? |
| 2079 | else: |
| 2080 | portNo = re.search( portRe, name ).group( 'port' ) |
| 2081 | ports.append( { 'of_port': portNo, |
| 2082 | 'mac': str( mac ).replace( '\'', '' ), |
| 2083 | 'name': name, |
| 2084 | 'ips': ips, |
| 2085 | 'enabled': isUp } ) |
| 2086 | return ports |
| 2087 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2088 | def getSwitches( self, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2089 | """ |
| 2090 | Read switches from Mininet. |
| 2091 | |
| 2092 | Returns a dictionary whose keys are the switch names and the value is |
| 2093 | a dictionary containing information about the switch. |
| 2094 | """ |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2095 | # NOTE: To support new Mininet switch classes, just append the new |
| 2096 | # class to the switchClasses variable |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2097 | |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2098 | # Regex patterns to parse 'dump' output |
| 2099 | # Example Switches: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2100 | # <OVSSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=5238> |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2101 | # <OVSSwitch{ 'protocols': 'OpenFlow10' } s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=25974> |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2102 | # <OVSSwitchNS s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=22550> |
| 2103 | # <OVSBridge s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=26830> |
| 2104 | # <UserSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=14737> |
| 2105 | switchClasses = r"(OVSSwitch)|(OVSBridge)|(OVSSwitchNS)|(IVSSwitch)|(LinuxBridge)|(UserSwitch)" |
| 2106 | swRE = r"<(?P<class>" + switchClasses + r")" +\ |
| 2107 | r"(?P<options>\{.*\})?\s" +\ |
| 2108 | r"(?P<name>[^:]+)\:\s" +\ |
| 2109 | r"(?P<ports>([^,]+,)*[^,\s]+)" +\ |
| 2110 | r"\spid=(?P<pid>(\d)+)" |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2111 | # Update mn port info |
| 2112 | self.update() |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2113 | output = {} |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2114 | dump = self.dump().split( "\n" ) |
| 2115 | for line in dump: |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2116 | result = re.search( swRE, line, re.I ) |
| 2117 | if result: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2118 | name = result.group( 'name' ) |
| 2119 | dpid = str( self.getSwitchDPID( name ) ).zfill( 16 ) |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2120 | pid = result.group( 'pid' ) |
| 2121 | swClass = result.group( 'class' ) |
| 2122 | options = result.group( 'options' ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2123 | if verbose: |
| 2124 | main.log.info( "Reading switch %s(%s)" % ( name, dpid ) ) |
| 2125 | ports = self.getPorts( name ) |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2126 | output[ name ] = { "dpid": dpid, |
| 2127 | "ports": ports, |
| 2128 | "swClass": swClass, |
| 2129 | "pid": pid, |
| 2130 | "options": options } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2131 | return output |
| 2132 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2133 | def getHosts( self, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2134 | """ |
| 2135 | Read hosts from Mininet. |
| 2136 | |
| 2137 | Returns a dictionary whose keys are the host names and the value is |
| 2138 | a dictionary containing information about the host. |
| 2139 | """ |
| 2140 | # Regex patterns to parse dump output |
| 2141 | # Example host: <Host h1: h1-eth0:10.0.0.1 pid=5227> |
kelvin-onlab | 299ab06 | 2015-07-15 10:58:27 -0700 | [diff] [blame] | 2142 | # <Host h1: pid=12725> |
| 2143 | # <VLANHost h12: h12-eth0.100.100.100:100.1.0.3 pid=30186> |
| 2144 | # <dualStackHost h19: h19-eth0:10.1.0.9 pid=30200> |
| 2145 | # <IPv6Host h18: h18-eth0:10.0.0.18 pid=30198> |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2146 | # NOTE: Does not correctly match hosts with multi-links |
| 2147 | # <Host h2: h2-eth0:10.0.0.2,h2-eth1:10.0.1.2 pid=14386> |
| 2148 | # FIXME: Fix that |
kelvin-onlab | d48a68c | 2015-07-13 16:01:36 -0700 | [diff] [blame] | 2149 | hostRE = r"Host\s(?P<name>[^:]+)\:((\s(?P<ifname>[^:]+)\:" +\ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2150 | "(?P<ip>[^\s]+))|(\s)\spid=(?P<pid>[^>]+))" |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2151 | # update mn port info |
| 2152 | self.update() |
| 2153 | # Get mininet dump |
| 2154 | dump = self.dump().split( "\n" ) |
Jon Hall | 5b0120a | 2015-06-12 17:35:53 -0700 | [diff] [blame] | 2155 | hosts = {} |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2156 | for line in dump: |
kelvin-onlab | d48a68c | 2015-07-13 16:01:36 -0700 | [diff] [blame] | 2157 | if "Host" in line : |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2158 | result = re.search( hostRE, line ) |
| 2159 | name = result.group( 'name' ) |
| 2160 | interfaces = [] |
| 2161 | response = self.getInterfaces( name ) |
| 2162 | # Populate interface info |
| 2163 | for line in response.split( "\n" ): |
| 2164 | if line.startswith( "name=" ): |
| 2165 | portVars = {} |
| 2166 | for var in line.split( "," ): |
| 2167 | key, value = var.split( "=" ) |
| 2168 | portVars[ key ] = value |
| 2169 | isUp = portVars.pop( 'enabled', "True" ) |
| 2170 | isUp = "True" in isUp |
| 2171 | if verbose: |
| 2172 | main.log.info( "Reading host port %s(%s)" % |
| 2173 | ( portVars[ 'name' ], |
| 2174 | portVars[ 'mac' ] ) ) |
| 2175 | mac = portVars[ 'mac' ] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2176 | if mac == 'None': |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2177 | mac = None |
| 2178 | ips = [] |
| 2179 | ip = portVars[ 'ip' ] |
| 2180 | if ip == 'None': |
| 2181 | ip = None |
| 2182 | ips.append( ip ) |
| 2183 | intfName = portVars[ 'name' ] |
| 2184 | if name == 'None': |
| 2185 | name = None |
| 2186 | interfaces.append( { |
| 2187 | "name": intfName, |
| 2188 | "ips": ips, |
| 2189 | "mac": str( mac ), |
| 2190 | "isUp": isUp } ) |
Jon Hall | 5b0120a | 2015-06-12 17:35:53 -0700 | [diff] [blame] | 2191 | hosts[ name ] = { "interfaces": interfaces } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2192 | return hosts |
| 2193 | |
| 2194 | def getLinks( self ): |
| 2195 | """ |
| 2196 | Gathers information about current Mininet links. These links may not |
| 2197 | be up if one of the ports is down. |
| 2198 | |
| 2199 | Returns a list of dictionaries with link endpoints. |
| 2200 | |
| 2201 | The dictionary structure is: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2202 | { 'node1': str( node1 name ) |
| 2203 | 'node2': str( node2 name ) |
| 2204 | 'port1': str( port1 of_port ) |
| 2205 | 'port2': str( port2 of_port ) } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2206 | Note: The port number returned is the eth#, not necessarily the of_port |
| 2207 | number. In Mininet, for OVS switch, these should be the same. For |
| 2208 | hosts, this is just the eth#. |
| 2209 | """ |
| 2210 | self.update() |
| 2211 | response = self.links().split( '\n' ) |
| 2212 | |
| 2213 | # Examples: |
| 2214 | # s1-eth3<->s2-eth1 (OK OK) |
| 2215 | # s13-eth3<->h27-eth0 (OK OK) |
| 2216 | linkRE = "(?P<node1>[\w]+)\-eth(?P<port1>[\d]+)\<\-\>" +\ |
| 2217 | "(?P<node2>[\w]+)\-eth(?P<port2>[\d]+)" |
| 2218 | links = [] |
| 2219 | for line in response: |
| 2220 | match = re.search( linkRE, line ) |
| 2221 | if match: |
| 2222 | node1 = match.group( 'node1' ) |
| 2223 | node2 = match.group( 'node2' ) |
| 2224 | port1 = match.group( 'port1' ) |
| 2225 | port2 = match.group( 'port2' ) |
| 2226 | links.append( { 'node1': node1, |
| 2227 | 'node2': node2, |
| 2228 | 'port1': port1, |
| 2229 | 'port2': port2 } ) |
| 2230 | return links |
| 2231 | |
| 2232 | def compareSwitches( self, switches, switchesJson, portsJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2233 | """ |
| 2234 | Compare mn and onos switches |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2235 | switchesJson: parsed json object from the onos devices api |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2236 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2237 | Dependencies: |
| 2238 | 1. numpy - "sudo pip install numpy" |
| 2239 | """ |
| 2240 | from numpy import uint64 |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2241 | # created sorted list of dpid's in MN and ONOS for comparison |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2242 | mnDPIDs = [] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2243 | for swName, switch in switches.iteritems(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2244 | mnDPIDs.append( switch[ 'dpid' ].lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2245 | mnDPIDs.sort() |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2246 | if switchesJson == "": # if rest call fails |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2247 | main.log.error( |
| 2248 | self.name + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 2249 | ".compareSwitches(): Empty JSON object given from ONOS" ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2250 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2251 | onos = switchesJson |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2252 | onosDPIDs = [] |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2253 | for switch in onos: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2254 | if switch[ 'available' ]: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2255 | onosDPIDs.append( |
| 2256 | switch[ 'id' ].replace( |
| 2257 | ":", |
| 2258 | '' ).replace( |
| 2259 | "of", |
| 2260 | '' ).lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2261 | onosDPIDs.sort() |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2262 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2263 | if mnDPIDs != onosDPIDs: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2264 | switchResults = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2265 | main.log.error( "Switches in MN but not in ONOS:" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2266 | list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2267 | main.log.error( str( list1 ) ) |
| 2268 | main.log.error( "Switches in ONOS but not in MN:" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2269 | list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2270 | main.log.error( str( list2 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2271 | else: # list of dpid's match in onos and mn |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2272 | switchResults = main.TRUE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2273 | finalResults = switchResults |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2274 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2275 | # FIXME: this does not look for extra ports in ONOS, only checks that |
| 2276 | # ONOS has what is in MN |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2277 | portsResults = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2278 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2279 | # PORTS |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2280 | for name, mnSwitch in switches.iteritems(): |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2281 | mnPorts = [] |
| 2282 | onosPorts = [] |
| 2283 | switchResult = main.TRUE |
| 2284 | for port in mnSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2285 | if port[ 'enabled' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2286 | mnPorts.append( int( port[ 'of_port' ] ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2287 | for onosSwitch in portsJson: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2288 | if onosSwitch[ 'device' ][ 'available' ]: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2289 | if onosSwitch[ 'device' ][ 'id' ].replace( |
| 2290 | ':', |
| 2291 | '' ).replace( |
| 2292 | "of", |
| 2293 | '' ) == mnSwitch[ 'dpid' ]: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2294 | for port in onosSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2295 | if port[ 'isEnabled' ]: |
| 2296 | if port[ 'port' ] == 'local': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2297 | # onosPorts.append( 'local' ) |
| 2298 | onosPorts.append( long( uint64( -2 ) ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2299 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2300 | onosPorts.append( int( port[ 'port' ] ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2301 | break |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2302 | mnPorts.sort( key=float ) |
| 2303 | onosPorts.sort( key=float ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2304 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2305 | mnPortsLog = mnPorts |
| 2306 | onosPortsLog = onosPorts |
| 2307 | mnPorts = [ x for x in mnPorts ] |
| 2308 | onosPorts = [ x for x in onosPorts ] |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2309 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2310 | # TODO: handle other reserved port numbers besides LOCAL |
| 2311 | # NOTE: Reserved ports |
| 2312 | # Local port: -2 in Openflow, ONOS shows 'local', we store as |
| 2313 | # long( uint64( -2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2314 | for mnPort in mnPortsLog: |
| 2315 | if mnPort in onosPorts: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2316 | # don't set results to true here as this is just one of |
| 2317 | # many checks and it might override a failure |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2318 | mnPorts.remove( mnPort ) |
| 2319 | onosPorts.remove( mnPort ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2320 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2321 | # NOTE: OVS reports this as down since there is no link |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2322 | # So ignoring these for now |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2323 | # TODO: Come up with a better way of handling these |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2324 | if 65534 in mnPorts: |
| 2325 | mnPorts.remove( 65534 ) |
| 2326 | if long( uint64( -2 ) ) in onosPorts: |
| 2327 | onosPorts.remove( long( uint64( -2 ) ) ) |
| 2328 | if len( mnPorts ): # the ports of this switch don't match |
| 2329 | switchResult = main.FALSE |
| 2330 | main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) ) |
| 2331 | if len( onosPorts ): # the ports of this switch don't match |
| 2332 | switchResult = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2333 | main.log.warn( |
| 2334 | "Ports in ONOS but not MN: " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2335 | str( onosPorts ) ) |
| 2336 | if switchResult == main.FALSE: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2337 | main.log.error( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2338 | "The list of ports for switch %s(%s) does not match:" % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2339 | ( name, mnSwitch[ 'dpid' ] ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2340 | main.log.warn( "mn_ports[] = " + str( mnPortsLog ) ) |
| 2341 | main.log.warn( "onos_ports[] = " + str( onosPortsLog ) ) |
| 2342 | portsResults = portsResults and switchResult |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2343 | finalResults = finalResults and portsResults |
| 2344 | return finalResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2345 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2346 | def compareLinks( self, switches, links, linksJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2347 | """ |
| 2348 | Compare mn and onos links |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2349 | linksJson: parsed json object from the onos links api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2350 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2351 | """ |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2352 | # 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] | 2353 | # ONOS has what is in MN |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2354 | onos = linksJson |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2355 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2356 | mnLinks = [] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2357 | for l in links: |
| 2358 | try: |
| 2359 | node1 = switches[ l[ 'node1' ] ] |
| 2360 | node2 = switches[ l[ 'node2' ] ] |
| 2361 | enabled = True |
| 2362 | for port in node1[ 'ports' ]: |
| 2363 | if port[ 'of_port' ] == l[ 'port1' ]: |
| 2364 | enabled = enabled and port[ 'enabled' ] |
| 2365 | for port in node2[ 'ports' ]: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2366 | if port[ 'of_port' ] == l[ 'port2' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2367 | enabled = enabled and port[ 'enabled' ] |
| 2368 | if enabled: |
| 2369 | mnLinks.append( l ) |
| 2370 | except KeyError: |
| 2371 | pass |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2372 | if 2 * len( mnLinks ) == len( onos ): |
| 2373 | linkResults = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2374 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2375 | linkResults = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2376 | main.log.error( |
Jon Hall | 328ddca | 2015-01-28 15:57:15 -0800 | [diff] [blame] | 2377 | "Mininet has " + str( len( mnLinks ) ) + |
| 2378 | " bidirectional links and ONOS has " + |
| 2379 | str( len( onos ) ) + " unidirectional links" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2380 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2381 | # iterate through MN links and check if an ONOS link exists in |
| 2382 | # both directions |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2383 | for link in mnLinks: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2384 | # TODO: Find a more efficient search method |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2385 | node1 = None |
| 2386 | port1 = None |
| 2387 | node2 = None |
| 2388 | port2 = None |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2389 | firstDir = main.FALSE |
| 2390 | secondDir = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2391 | for swName, switch in switches.iteritems(): |
| 2392 | if swName == link[ 'node1' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2393 | node1 = switch[ 'dpid' ] |
| 2394 | for port in switch[ 'ports' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2395 | if str( port[ 'of_port' ] ) == str( link[ 'port1' ] ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2396 | port1 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2397 | if node1 is not None and node2 is not None: |
| 2398 | break |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2399 | if swName == link[ 'node2' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2400 | node2 = switch[ 'dpid' ] |
| 2401 | for port in switch[ 'ports' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2402 | if str( port[ 'of_port' ] ) == str( link[ 'port2' ] ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2403 | port2 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2404 | if node1 is not None and node2 is not None: |
| 2405 | break |
| 2406 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2407 | for onosLink in onos: |
| 2408 | onosNode1 = onosLink[ 'src' ][ 'device' ].replace( |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2409 | ":", '' ).replace( "of", '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2410 | onosNode2 = onosLink[ 'dst' ][ 'device' ].replace( |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2411 | ":", '' ).replace( "of", '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2412 | onosPort1 = onosLink[ 'src' ][ 'port' ] |
| 2413 | onosPort2 = onosLink[ 'dst' ][ 'port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2414 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2415 | # check onos link from node1 to node2 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2416 | if str( onosNode1 ) == str( node1 ) and str( |
| 2417 | onosNode2 ) == str( node2 ): |
| 2418 | if int( onosPort1 ) == int( port1 ) and int( |
| 2419 | onosPort2 ) == int( port2 ): |
| 2420 | firstDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2421 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2422 | main.log.warn( |
| 2423 | 'The port numbers do not match for ' + |
| 2424 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2425 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2426 | 'link %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2427 | ( node1, port1, node2, port2 ) + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2428 | ' ONOS has the values %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2429 | ( onosNode1, onosPort1, onosNode2, onosPort2 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2430 | |
| 2431 | # check onos link from node2 to node1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2432 | elif ( str( onosNode1 ) == str( node2 ) and |
| 2433 | str( onosNode2 ) == str( node1 ) ): |
| 2434 | if ( int( onosPort1 ) == int( port2 ) |
| 2435 | and int( onosPort2 ) == int( port1 ) ): |
| 2436 | secondDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2437 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2438 | main.log.warn( |
| 2439 | 'The port numbers do not match for ' + |
| 2440 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2441 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2442 | 'link %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2443 | ( node1, port1, node2, port2 ) + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2444 | ' ONOS has the values %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2445 | ( onosNode2, onosPort2, onosNode1, onosPort1 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2446 | else: # this is not the link you're looking for |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2447 | pass |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2448 | if not firstDir: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2449 | main.log.error( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2450 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 2451 | ( node1, port1, node2, port2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2452 | if not secondDir: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2453 | main.log.error( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2454 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 2455 | ( node2, port2, node1, port1 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2456 | linkResults = linkResults and firstDir and secondDir |
| 2457 | return linkResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2458 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2459 | def compareHosts( self, hosts, hostsJson ): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2460 | """ |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2461 | Compare mn and onos Hosts. |
| 2462 | Since Mininet hosts are quiet, ONOS will only know of them when they |
| 2463 | speak. For this reason, we will only check that the hosts in ONOS |
| 2464 | stores are in Mininet, and not vice versa. |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2465 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2466 | Arguments: |
| 2467 | hostsJson: parsed json object from the onos hosts api |
| 2468 | Returns: |
| 2469 | """ |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2470 | import json |
| 2471 | hostResults = main.TRUE |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2472 | for onosHost in hostsJson: |
| 2473 | onosMAC = onosHost[ 'mac' ].lower() |
| 2474 | match = False |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2475 | for mnHost, info in hosts.iteritems(): |
| 2476 | for mnIntf in info[ 'interfaces' ]: |
| 2477 | if onosMAC == mnIntf[ 'mac' ].lower(): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2478 | match = True |
| 2479 | for ip in mnIntf[ 'ips' ]: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 2480 | if ip in onosHost[ 'ipAddresses' ]: |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2481 | pass # all is well |
| 2482 | else: |
| 2483 | # misssing ip |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2484 | main.log.error( "ONOS host " + |
| 2485 | onosHost[ 'id' ] + |
| 2486 | " has a different IP(" + |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2487 | str( onosHost[ 'ipAddresses' ] ) + |
| 2488 | ") than the Mininet host(" + |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2489 | str( ip ) + |
| 2490 | ")." ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2491 | output = json.dumps( |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2492 | onosHost, |
| 2493 | sort_keys=True, |
| 2494 | indent=4, |
| 2495 | separators=( ',', ': ' ) ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2496 | main.log.info( output ) |
| 2497 | hostResults = main.FALSE |
| 2498 | if not match: |
| 2499 | hostResults = main.FALSE |
| 2500 | main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " + |
| 2501 | "corresponding Mininet host." ) |
| 2502 | output = json.dumps( onosHost, |
| 2503 | sort_keys=True, |
| 2504 | indent=4, |
| 2505 | separators=( ',', ': ' ) ) |
| 2506 | main.log.info( output ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2507 | return hostResults |
| 2508 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2509 | def getHostsOld( self ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2510 | """ |
| 2511 | Returns a list of all hosts |
| 2512 | Don't ask questions just use it""" |
| 2513 | self.handle.sendline( "" ) |
| 2514 | self.handle.expect( "mininet>" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2515 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2516 | self.handle.sendline( "py [ host.name for host in net.hosts ]" ) |
| 2517 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2518 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2519 | handlePy = self.handle.before |
| 2520 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 2521 | handlePy = handlePy.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2522 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2523 | self.handle.sendline( "" ) |
| 2524 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2525 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2526 | hostStr = handlePy.replace( "]", "" ) |
| 2527 | hostStr = hostStr.replace( "'", "" ) |
| 2528 | hostStr = hostStr.replace( "[", "" ) |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame] | 2529 | hostStr = hostStr.replace( " ", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2530 | hostList = hostStr.split( "," ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 2531 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2532 | return hostList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2533 | |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 2534 | def getSwitch( self ): |
| 2535 | """ |
| 2536 | Returns a list of all switches |
| 2537 | Again, don't ask question just use it... |
| 2538 | """ |
| 2539 | # get host list... |
| 2540 | hostList = self.getHosts() |
| 2541 | # Make host set |
| 2542 | hostSet = set( hostList ) |
| 2543 | |
| 2544 | # Getting all the nodes in mininet |
| 2545 | self.handle.sendline( "" ) |
| 2546 | self.handle.expect( "mininet>" ) |
| 2547 | |
| 2548 | self.handle.sendline( "py [ node.name for node in net.values() ]" ) |
| 2549 | self.handle.expect( "mininet>" ) |
| 2550 | |
| 2551 | handlePy = self.handle.before |
| 2552 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 2553 | handlePy = handlePy.rstrip() |
| 2554 | |
| 2555 | self.handle.sendline( "" ) |
| 2556 | self.handle.expect( "mininet>" ) |
| 2557 | |
| 2558 | nodesStr = handlePy.replace( "]", "" ) |
| 2559 | nodesStr = nodesStr.replace( "'", "" ) |
| 2560 | nodesStr = nodesStr.replace( "[", "" ) |
| 2561 | nodesStr = nodesStr.replace( " ", "" ) |
| 2562 | nodesList = nodesStr.split( "," ) |
| 2563 | |
| 2564 | nodesSet = set( nodesList ) |
| 2565 | # discarding default controller(s) node |
| 2566 | nodesSet.discard( 'c0' ) |
| 2567 | nodesSet.discard( 'c1' ) |
| 2568 | nodesSet.discard( 'c2' ) |
| 2569 | |
| 2570 | switchSet = nodesSet - hostSet |
| 2571 | switchList = list( switchSet ) |
| 2572 | |
| 2573 | return switchList |
| 2574 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2575 | def update( self ): |
| 2576 | """ |
| 2577 | updates the port address and status information for |
| 2578 | each port in mn""" |
| 2579 | # TODO: Add error checking. currently the mininet command has no output |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2580 | main.log.info( "Updating MN port information" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2581 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2582 | self.handle.sendline( "" ) |
| 2583 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2584 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2585 | self.handle.sendline( "update" ) |
| 2586 | self.handle.expect( "update" ) |
| 2587 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2588 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2589 | self.handle.sendline( "" ) |
| 2590 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2591 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2592 | return main.TRUE |
| 2593 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2594 | main.log.error( self.name + ": EOF exception found" ) |
| 2595 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2596 | main.cleanup() |
| 2597 | main.exit() |
| 2598 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2599 | def assignVLAN( self, host, intf, vlan ): |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2600 | """ |
| 2601 | Add vlan tag to a host. |
| 2602 | Dependencies: |
| 2603 | This class depends on the "vlan" package |
| 2604 | $ sudo apt-get install vlan |
| 2605 | Configuration: |
| 2606 | Load the 8021q module into the kernel |
| 2607 | $sudo modprobe 8021q |
| 2608 | |
| 2609 | To make this setup permanent: |
| 2610 | $ sudo su -c 'echo "8021q" >> /etc/modules' |
| 2611 | """ |
| 2612 | if self.handle: |
| 2613 | try: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2614 | # get the ip address of the host |
| 2615 | main.log.info( "Get the ip address of the host" ) |
| 2616 | ipaddr = self.getIPAddress( host ) |
| 2617 | print repr( ipaddr ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2618 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2619 | # remove IP from interface intf |
| 2620 | # Ex: h1 ifconfig h1-eth0 inet 0 |
| 2621 | main.log.info( "Remove IP from interface " ) |
| 2622 | cmd2 = host + " ifconfig " + intf + " " + " inet 0 " |
| 2623 | self.handle.sendline( cmd2 ) |
| 2624 | self.handle.expect( "mininet>" ) |
| 2625 | response = self.handle.before |
| 2626 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2627 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2628 | # create VLAN interface |
| 2629 | # Ex: h1 vconfig add h1-eth0 100 |
| 2630 | main.log.info( "Create Vlan" ) |
| 2631 | cmd3 = host + " vconfig add " + intf + " " + vlan |
| 2632 | self.handle.sendline( cmd3 ) |
| 2633 | self.handle.expect( "mininet>" ) |
| 2634 | response = self.handle.before |
| 2635 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2636 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2637 | # assign the host's IP to the VLAN interface |
| 2638 | # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1 |
| 2639 | main.log.info( "Assign the host IP to the vlan interface" ) |
| 2640 | vintf = intf + "." + vlan |
| 2641 | cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr |
| 2642 | self.handle.sendline( cmd4 ) |
| 2643 | self.handle.expect( "mininet>" ) |
| 2644 | response = self.handle.before |
| 2645 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2646 | |
| 2647 | return main.TRUE |
| 2648 | except pexpect.EOF: |
| 2649 | main.log.error( self.name + ": EOF exception found" ) |
| 2650 | main.log.error( self.name + ": " + self.handle.before ) |
| 2651 | return main.FALSE |
| 2652 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2653 | if __name__ != "__main__": |
| 2654 | import sys |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 2655 | sys.modules[ __name__ ] = MininetCliDriver() |