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