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