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