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