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