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