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