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