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