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