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