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 |
| 497 | pingCmd = str( host ) + cmd + prefix + str( temp[1:] ) |
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 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [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[] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 688 | ( which is correct behavior since the interfaces |
| 689 | haven't moved ). |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 690 | """ |
| 691 | if self.handle: |
| 692 | try: |
| 693 | # Bring link between oldSw-host down |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 694 | cmd = "py net.configLinkStatus('" + oldSw + "'," + "'" + host +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 695 | "'," + "'down')" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 696 | print "cmd1= ", cmd |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 697 | response = self.execute( cmd=cmd, |
| 698 | prompt="mininet>", |
| 699 | timeout=10 ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 700 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 701 | # Determine hostintf and Oldswitchintf |
| 702 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + oldSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 703 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 704 | print "cmd2= ", cmd |
| 705 | self.handle.sendline( cmd ) |
| 706 | self.handle.expect( "mininet>" ) |
| 707 | |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 708 | # Determine ip and mac address of the host-oldSw interface |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 709 | cmd = "px ipaddr = hintf.IP()" |
| 710 | print "cmd3= ", cmd |
| 711 | self.handle.sendline( cmd ) |
| 712 | self.handle.expect( "mininet>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 713 | |
| 714 | cmd = "px macaddr = hintf.MAC()" |
| 715 | print "cmd3= ", cmd |
| 716 | self.handle.sendline( cmd ) |
| 717 | self.handle.expect( "mininet>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 718 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 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>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 730 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 731 | # Determine hostintf and Newswitchintf |
| 732 | cmd = "px hintf,sintf = " + host + ".connectionsTo(" + newSw +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 733 | ")[0]" |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 734 | print "cmd6= ", cmd |
| 735 | self.handle.sendline( cmd ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 736 | self.handle.expect( "mininet>" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 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>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 743 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 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>" ) |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 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>" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 755 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 756 | cmd = "net" |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 757 | print "cmd9 = ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 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 |
shahshreya | 7353786 | 2015-02-11 15:15:24 -0800 | [diff] [blame] | 763 | cmd = host + " ifconfig" |
| 764 | print "cmd10= ", cmd |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 765 | self.handle.sendline( cmd ) |
| 766 | self.handle.expect( "mininet>" ) |
| 767 | print "ifconfig o/p = ", self.handle.before |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 768 | |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 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 | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 775 | def changeIP( self, host, intf, newIP, newNetmask ): |
| 776 | """ |
| 777 | Changes the ip address of a host on the fly |
| 778 | 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] | 779 | if self.handle: |
| 780 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 781 | cmd = host + " ifconfig " + intf + " " + \ |
| 782 | newIP + " " + 'netmask' + " " + newNetmask |
| 783 | self.handle.sendline( cmd ) |
| 784 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 785 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 786 | main.log.info( "response = " + response ) |
| 787 | main.log.info( |
| 788 | "Ip of host " + |
| 789 | host + |
| 790 | " changed to new IP " + |
| 791 | newIP ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 792 | return main.TRUE |
| 793 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 794 | main.log.error( self.name + ": EOF exception found" ) |
| 795 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 796 | return main.FALSE |
| 797 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 798 | def changeDefaultGateway( self, host, newGW ): |
| 799 | """ |
| 800 | Changes the default gateway of a host |
| 801 | Ex: h1 route add default gw 10.0.1.2""" |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 802 | if self.handle: |
| 803 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 804 | cmd = host + " route add default gw " + newGW |
| 805 | self.handle.sendline( cmd ) |
| 806 | self.handle.expect( "mininet>" ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 807 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 808 | main.log.info( "response = " + response ) |
| 809 | main.log.info( |
| 810 | "Default gateway of host " + |
| 811 | host + |
| 812 | " changed to " + |
| 813 | newGW ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 814 | return main.TRUE |
| 815 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 816 | main.log.error( self.name + ": EOF exception found" ) |
| 817 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 818 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 819 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 820 | def addStaticMACAddress( self, host, GW, macaddr ): |
| 821 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 822 | Changes the mac address of a gateway host""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 823 | if self.handle: |
| 824 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 825 | # h1 arp -s 10.0.1.254 00:00:00:00:11:11 |
| 826 | cmd = host + " arp -s " + GW + " " + macaddr |
| 827 | self.handle.sendline( cmd ) |
| 828 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 829 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 830 | main.log.info( "response = " + response ) |
| 831 | main.log.info( |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 832 | "Mac address of gateway " + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 833 | GW + |
| 834 | " changed to " + |
| 835 | macaddr ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 836 | return main.TRUE |
| 837 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 838 | main.log.error( self.name + ": EOF exception found" ) |
| 839 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 840 | return main.FALSE |
| 841 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 842 | def verifyStaticGWandMAC( self, host ): |
| 843 | """ |
| 844 | Verify if the static gateway and mac address assignment""" |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 845 | if self.handle: |
| 846 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 847 | # h1 arp -an |
| 848 | cmd = host + " arp -an " |
| 849 | self.handle.sendline( cmd ) |
| 850 | self.handle.expect( "mininet>" ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 851 | response = self.handle.before |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 852 | main.log.info( host + " arp -an = " + response ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 853 | return main.TRUE |
| 854 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 855 | main.log.error( self.name + ": EOF exception found" ) |
| 856 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 857 | return main.FALSE |
| 858 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 859 | def getMacAddress( self, host ): |
| 860 | """ |
| 861 | Verifies the host's ip configured or not.""" |
| 862 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 863 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 864 | response = self.execute( |
| 865 | cmd=host + |
| 866 | " ifconfig", |
| 867 | prompt="mininet>", |
| 868 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 869 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 870 | main.log.error( self.name + ": EOF exception found" ) |
| 871 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 872 | main.cleanup() |
| 873 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 874 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 875 | 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] | 876 | macAddressSearch = re.search( pattern, response, re.I ) |
| 877 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 878 | main.log.info( |
| 879 | self.name + |
| 880 | ": Mac-Address of Host " + |
| 881 | host + |
| 882 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 883 | macAddress ) |
| 884 | return macAddress |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 885 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 886 | main.log.error( self.name + ": Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 887 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 888 | def getInterfaceMACAddress( self, host, interface ): |
| 889 | """ |
| 890 | Return the IP address of the interface on the given host""" |
| 891 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 892 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 893 | response = self.execute( cmd=host + " ifconfig " + interface, |
| 894 | prompt="mininet>", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 895 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 896 | main.log.error( self.name + ": EOF exception found" ) |
| 897 | main.log.error( self.name + ": " + self.handle.before ) |
| 898 | main.cleanup() |
| 899 | main.exit() |
| 900 | |
| 901 | 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] | 902 | macAddressSearch = re.search( pattern, response, re.I ) |
| 903 | if macAddressSearch is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 904 | main.log.info( "No mac address found in %s" % response ) |
| 905 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 906 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 907 | main.log.info( |
| 908 | "Mac-Address of " + |
| 909 | host + |
| 910 | ":" + |
| 911 | interface + |
| 912 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 913 | macAddress ) |
| 914 | return macAddress |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 915 | else: |
| 916 | main.log.error( "Connection failed to the host" ) |
| 917 | |
sathishm | ad95346 | 2015-12-03 17:42:07 +0530 | [diff] [blame^] | 918 | def getIPAddress( self, host , proto='IPV4'): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 919 | """ |
| 920 | Verifies the host's ip configured or not.""" |
| 921 | if self.handle: |
| 922 | try: |
| 923 | response = self.execute( |
| 924 | cmd=host + |
| 925 | " ifconfig", |
| 926 | prompt="mininet>", |
| 927 | timeout=10 ) |
| 928 | except pexpect.EOF: |
| 929 | main.log.error( self.name + ": EOF exception found" ) |
| 930 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 931 | main.cleanup() |
| 932 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 933 | |
sathishm | ad95346 | 2015-12-03 17:42:07 +0530 | [diff] [blame^] | 934 | pattern = '' |
| 935 | if proto == 'IPV4': |
| 936 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 937 | else: |
| 938 | pattern = "inet6\saddr:\s([\w,:]*)/" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 939 | ipAddressSearch = re.search( pattern, response ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 940 | main.log.info( |
| 941 | self.name + |
| 942 | ": IP-Address of Host " + |
| 943 | host + |
| 944 | " is " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 945 | ipAddressSearch.group( 1 ) ) |
| 946 | return ipAddressSearch.group( 1 ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 947 | else: |
| 948 | main.log.error( self.name + ": Connection failed to the host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 949 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 950 | def getSwitchDPID( self, switch ): |
| 951 | """ |
| 952 | return the datapath ID of the switch""" |
| 953 | if self.handle: |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 954 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 955 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 956 | response = self.execute( |
| 957 | cmd=cmd, |
| 958 | prompt="mininet>", |
| 959 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 960 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 961 | main.log.error( self.name + ": EOF exception found" ) |
| 962 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 963 | main.cleanup() |
| 964 | main.exit() |
Jon Hall | 28bf54b | 2014-12-17 16:25:44 -0800 | [diff] [blame] | 965 | pattern = r'^(?P<dpid>\w)+' |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 966 | result = re.search( pattern, response, re.MULTILINE ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 967 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 968 | main.log.info( |
| 969 | "Couldn't find DPID for switch %s, found: %s" % |
| 970 | ( switch, response ) ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 971 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 972 | return str( result.group( 0 ) ).lower() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 973 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 974 | main.log.error( "Connection failed to the host" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 975 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 976 | def getDPID( self, switch ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 977 | if self.handle: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 978 | self.handle.sendline( "" ) |
| 979 | self.expect( "mininet>" ) |
| 980 | cmd = "py %s.dpid" % switch |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 981 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 982 | response = self.execute( |
| 983 | cmd=cmd, |
| 984 | prompt="mininet>", |
| 985 | timeout=10 ) |
| 986 | self.handle.expect( "mininet>" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 987 | response = self.handle.before |
| 988 | return response |
| 989 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 990 | main.log.error( self.name + ": EOF exception found" ) |
| 991 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 992 | main.cleanup() |
| 993 | main.exit() |
| 994 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 995 | def getInterfaces( self, node ): |
| 996 | """ |
| 997 | return information dict about interfaces connected to the node""" |
| 998 | if self.handle: |
| 999 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 1000 | ' % (i.name, i.MAC(), i.IP(), i.isUp())' |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1001 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1002 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1003 | response = self.execute( |
| 1004 | cmd=cmd, |
| 1005 | prompt="mininet>", |
| 1006 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1007 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1008 | main.log.error( self.name + ": EOF exception found" ) |
| 1009 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1010 | main.cleanup() |
| 1011 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1012 | return response |
| 1013 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1014 | main.log.error( "Connection failed to the node" ) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 1015 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1016 | def dump( self ): |
| 1017 | main.log.info( self.name + ": Dump node info" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1018 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1019 | response = self.execute( |
| 1020 | cmd='dump', |
| 1021 | prompt='mininet>', |
| 1022 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1023 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1024 | main.log.error( self.name + ": EOF exception found" ) |
| 1025 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1026 | main.cleanup() |
| 1027 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 1028 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1029 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1030 | def intfs( self ): |
| 1031 | main.log.info( self.name + ": List interfaces" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1032 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1033 | response = self.execute( |
| 1034 | cmd='intfs', |
| 1035 | prompt='mininet>', |
| 1036 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1037 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1038 | main.log.error( self.name + ": EOF exception found" ) |
| 1039 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1040 | main.cleanup() |
| 1041 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1042 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1043 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1044 | def net( self ): |
| 1045 | main.log.info( self.name + ": List network connections" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1046 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1047 | response = self.execute( cmd='net', prompt='mininet>', timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1048 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1049 | main.log.error( self.name + ": EOF exception found" ) |
| 1050 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1051 | main.cleanup() |
| 1052 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1053 | return response |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1054 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1055 | def links( self ): |
| 1056 | main.log.info( self.name + ": List network links" ) |
| 1057 | try: |
| 1058 | response = self.execute( cmd='links', prompt='mininet>', |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1059 | timeout=20 ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1060 | except pexpect.EOF: |
| 1061 | main.log.error( self.name + ": EOF exception found" ) |
| 1062 | main.log.error( self.name + ": " + self.handle.before ) |
| 1063 | main.cleanup() |
| 1064 | main.exit() |
| 1065 | return response |
| 1066 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1067 | def iperftcpAll(self, hosts, timeout=6): |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1068 | ''' |
| 1069 | Runs the iperftcp function with a given set of hosts and specified timeout. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1070 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1071 | @parm: |
| 1072 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 1073 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 1074 | ''' |
| 1075 | for host1 in hosts: |
| 1076 | for host2 in hosts: |
| 1077 | if host1 != host2: |
| 1078 | if self.iperftcp(host1, host2, timeout) == main.FALSE: |
| 1079 | main.log.error(self.name + ": iperftcp test failed for " + host1 + " and " + host2) |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1080 | |
| 1081 | def iperftcp(self, host1="h1", host2="h2", timeout=6): |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1082 | ''' |
| 1083 | Creates an iperf TCP test between two hosts. Returns main.TRUE if test results |
| 1084 | are valid. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1085 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1086 | @parm: |
| 1087 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 1088 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 1089 | ''' |
| 1090 | main.log.info( self.name + ": Simple iperf TCP test between two hosts" ) |
| 1091 | try: |
| 1092 | # Setup the mininet command |
| 1093 | cmd1 = 'iperf ' + host1 + " " + host2 |
| 1094 | self.handle.sendline( cmd1 ) |
| 1095 | outcome = self.handle.expect( "mininet>", timeout ) |
| 1096 | response = self.handle.before |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1097 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1098 | # checks if there are results in the mininet response |
| 1099 | if "Results:" in response: |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 1100 | main.log.report(self.name + ": iperf test completed") |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1101 | # parse the mn results |
| 1102 | response = response.split("\r\n") |
| 1103 | response = response[len(response)-2] |
| 1104 | response = response.split(": ") |
| 1105 | response = response[len(response)-1] |
| 1106 | response = response.replace("[", "") |
| 1107 | response = response.replace("]", "") |
| 1108 | response = response.replace("\'", "") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1109 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1110 | # this is the bandwith two and from the two hosts |
| 1111 | bandwidth = response.split(", ") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1112 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1113 | # there should be two elements in the bandwidth list |
| 1114 | # ['host1 to host2', 'host2 to host1"] |
| 1115 | if len(bandwidth) == 2: |
| 1116 | main.log.report(self.name + ": iperf test successful") |
| 1117 | return main.TRUE |
| 1118 | else: |
| 1119 | main.log.error(self.name + ": invalid iperf results") |
| 1120 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1121 | else: |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1122 | main.log.error( self.name + ": iperf test failed" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1123 | return main.FALSE |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1124 | except pexpect.TIMEOUT: |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1125 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1126 | main.log.error( self.name + " response: " + |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 1127 | repr( self.handle.before ) ) |
Jon Hall | 3b489db | 2015-10-05 14:38:37 -0700 | [diff] [blame] | 1128 | # NOTE: Send ctrl-c to make sure iperf is done |
| 1129 | self.handle.sendline( "\x03" ) |
| 1130 | self.handle.expect( "Interrupt" ) |
| 1131 | self.handle.expect( "mininet>" ) |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1132 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1133 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1134 | main.log.error( self.name + ": EOF exception found" ) |
| 1135 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1136 | main.cleanup() |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1137 | main.exit() |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1138 | |
| 1139 | def iperfudpAll(self, hosts, bandwidth="10M"): |
| 1140 | ''' |
| 1141 | Runs the iperfudp function with a given set of hosts and specified |
| 1142 | bandwidth |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1143 | |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1144 | @param: |
| 1145 | bandwidth: the targeted bandwidth, in megabits ('M') |
| 1146 | ''' |
| 1147 | for host1 in hosts: |
| 1148 | for host2 in hosts: |
| 1149 | if host1 != host2: |
| 1150 | if self.iperfudp(host1, host2, bandwidth) == main.FALSE: |
| 1151 | main.log.error(self.name + ": iperfudp test failed for " + host1 + " and " + host2) |
| 1152 | |
| 1153 | def iperfudp( self, bandwidth="10M", host1="h1", host2="h2"): |
| 1154 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1155 | ''' |
| 1156 | Creates an iperf UDP test with a specific bandwidth. |
| 1157 | Returns true if results are valid. |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1158 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1159 | @param: |
| 1160 | bandwidth: the targeted bandwidth, in megabits ('M'), to run the test |
| 1161 | ''' |
| 1162 | main.log.info(self.name + ": Simple iperf UDP test between two hosts") |
| 1163 | try: |
| 1164 | # setup the mininet command |
| 1165 | cmd = 'iperfudp ' + bandwidth + " " + host1 + " " + host2 |
| 1166 | self.handle.sendline(cmd) |
| 1167 | self.handle.expect("mininet>") |
| 1168 | response = self.handle.before |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1169 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1170 | # check if there are in results in the mininet response |
| 1171 | if "Results:" in response: |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 1172 | main.log.report(self.name + ": iperfudp test completed") |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1173 | # parse the results |
| 1174 | response = response.split("\r\n") |
| 1175 | response = response[len(response)-2] |
| 1176 | response = response.split(": ") |
| 1177 | response = response[len(response)-1] |
| 1178 | response = response.replace("[", "") |
| 1179 | response = response.replace("]", "") |
| 1180 | response = response.replace("\'", "") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1181 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1182 | mnBandwidth = response.split(", ") |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1183 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1184 | # check to see if there are at least three entries |
| 1185 | # ['bandwidth', 'host1 to host2', 'host2 to host1'] |
| 1186 | if len(mnBandwidth) == 3: |
| 1187 | # if one entry is blank then something is wrong |
| 1188 | for item in mnBandwidth: |
| 1189 | if item == "": |
| 1190 | main.log.error(self.name + ": Could not parse iperf output") |
| 1191 | main.log.error(self.name + ": invalid iperfudp results") |
| 1192 | return main.FALSE |
| 1193 | # otherwise results are vaild |
| 1194 | main.log.report(self.name + ": iperfudp test successful") |
| 1195 | return main.TRUE |
| 1196 | else: |
| 1197 | main.log.error(self.name + ": invalid iperfudp results") |
| 1198 | return main.FALSE |
GlennRC | 61321f2 | 2015-07-16 13:36:54 -0700 | [diff] [blame] | 1199 | |
kelvin-onlab | 7cce938 | 2015-07-17 10:21:03 -0700 | [diff] [blame] | 1200 | except pexpect.EOF: |
| 1201 | main.log.error( self.name + ": EOF exception found" ) |
| 1202 | main.log.error( self.name + ": " + self.handle.before ) |
| 1203 | main.cleanup() |
| 1204 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1205 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1206 | def nodes( self ): |
| 1207 | main.log.info( self.name + ": List all nodes." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1208 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1209 | response = self.execute( |
| 1210 | cmd='nodes', |
| 1211 | prompt='mininet>', |
| 1212 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1213 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1214 | main.log.error( self.name + ": EOF exception found" ) |
| 1215 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1216 | main.cleanup() |
| 1217 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 1218 | return response |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1219 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1220 | def pingpair( self ): |
| 1221 | main.log.info( self.name + ": Ping between first two hosts" ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1222 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1223 | response = self.execute( |
| 1224 | cmd='pingpair', |
| 1225 | prompt='mininet>', |
| 1226 | timeout=20 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1227 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1228 | main.log.error( self.name + ": EOF exception found" ) |
| 1229 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1230 | main.cleanup() |
| 1231 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1232 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1233 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 1234 | main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1235 | return main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1236 | else: |
| 1237 | main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1238 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1239 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1240 | def link( self, **linkargs ): |
| 1241 | """ |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1242 | Bring link( s ) between two nodes up or down |
| 1243 | """ |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1244 | try: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1245 | args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs ) |
| 1246 | end1 = args[ "END1" ] if args[ "END1" ] is not None else "" |
| 1247 | end2 = args[ "END2" ] if args[ "END2" ] is not None else "" |
| 1248 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1249 | |
| 1250 | main.log.info( "Bring link between " + str( end1 ) + " and " + str( end2 ) + " " + str( option ) ) |
| 1251 | cmd = "link {} {} {}".format( end1, end2, option ) |
| 1252 | self.handle.sendline( cmd ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1253 | self.handle.expect( "mininet>" ) |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1254 | response = self.handle.before |
| 1255 | main.log.info( response ) |
| 1256 | |
| 1257 | return main.TRUE |
| 1258 | except pexpect.TIMEOUT: |
| 1259 | main.log.exception( self.name + ": Command timed out" ) |
| 1260 | return None |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1261 | except pexpect.EOF: |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1262 | main.log.exception( self.name + ": connection closed." ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1263 | main.cleanup() |
| 1264 | main.exit() |
GlennRC | ed77124 | 2016-01-13 17:02:47 -0800 | [diff] [blame] | 1265 | except Exception: |
| 1266 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1267 | main.cleanup() |
| 1268 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1269 | |
pingping-lin | 8244a3b | 2015-09-16 13:36:56 -0700 | [diff] [blame] | 1270 | def switch( self, **switchargs ): |
| 1271 | """ |
| 1272 | start/stop a switch |
| 1273 | """ |
| 1274 | args = utilities.parse_args( [ "SW", "OPTION" ], **switchargs ) |
| 1275 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1276 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1277 | command = "switch " + str( sw ) + " " + str( option ) |
| 1278 | main.log.info( command ) |
| 1279 | try: |
| 1280 | self.handle.sendline( command ) |
| 1281 | self.handle.expect( "mininet>" ) |
| 1282 | except pexpect.TIMEOUT: |
| 1283 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1284 | main.cleanup() |
| 1285 | main.exit() |
| 1286 | except pexpect.EOF: |
| 1287 | main.log.error( self.name + ": EOF exception found" ) |
| 1288 | main.log.error( self.name + ": " + self.handle.before ) |
| 1289 | main.cleanup() |
| 1290 | main.exit() |
| 1291 | return main.TRUE |
| 1292 | |
pingping-lin | 5bb663b | 2015-09-24 11:47:50 -0700 | [diff] [blame] | 1293 | def node( self, nodeName, commandStr ): |
| 1294 | """ |
| 1295 | Carry out a command line on a given node |
| 1296 | @parm: |
| 1297 | nodeName: the node name in Mininet testbed |
| 1298 | commandStr: the command line will be carried out on the node |
| 1299 | Example: main.Mininet.node( nodeName="h1", commandStr="ls" ) |
| 1300 | """ |
| 1301 | command = str( nodeName ) + " " + str( commandStr ) |
| 1302 | main.log.info( command ) |
| 1303 | |
| 1304 | try: |
| 1305 | response = self.execute( cmd = command, prompt = "mininet>" ) |
| 1306 | if re.search( "Unknown command", response ): |
| 1307 | main.log.warn( response ) |
| 1308 | return main.FALSE |
Jon Hall | 9ed8f37 | 2016-02-24 17:34:07 -0800 | [diff] [blame] | 1309 | if re.search( "Permission denied", response ): |
| 1310 | main.log.warn( response ) |
| 1311 | return main.FALSE |
pingping-lin | 5bb663b | 2015-09-24 11:47:50 -0700 | [diff] [blame] | 1312 | except pexpect.TIMEOUT: |
| 1313 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1314 | main.cleanup() |
| 1315 | main.exit() |
| 1316 | except pexpect.EOF: |
| 1317 | main.log.error( self.name + ": EOF exception found" ) |
| 1318 | main.log.error( self.name + ": " + self.handle.before ) |
| 1319 | main.cleanup() |
| 1320 | main.exit() |
| 1321 | main.log.info( " response is :" ) |
| 1322 | main.log.info( response ) |
| 1323 | return response |
| 1324 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1325 | def yank( self, **yankargs ): |
| 1326 | """ |
| 1327 | yank a mininet switch interface to a host""" |
| 1328 | main.log.info( 'Yank the switch interface attached to a host' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1329 | args = utilities.parse_args( [ "SW", "INTF" ], **yankargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1330 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1331 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1332 | command = "py " + str( sw ) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1333 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1334 | response = self.execute( |
| 1335 | cmd=command, |
| 1336 | prompt="mininet>", |
| 1337 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1338 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1339 | main.log.error( self.name + ": EOF exception found" ) |
| 1340 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1341 | main.cleanup() |
| 1342 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 1343 | return main.TRUE |
| 1344 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1345 | def plug( self, **plugargs ): |
| 1346 | """ |
| 1347 | plug the yanked mininet switch interface to a switch""" |
| 1348 | main.log.info( 'Plug the switch interface attached to a switch' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1349 | args = utilities.parse_args( [ "SW", "INTF" ], **plugargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1350 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1351 | intf = args[ "INTF" ] if args[ "INTF" ] is not None else "" |
| 1352 | command = "py " + str( sw ) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1353 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1354 | response = self.execute( |
| 1355 | cmd=command, |
| 1356 | prompt="mininet>", |
| 1357 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1358 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1359 | main.log.error( self.name + ": EOF exception found" ) |
| 1360 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1361 | main.cleanup() |
| 1362 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1363 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1364 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1365 | def dpctl( self, **dpctlargs ): |
| 1366 | """ |
| 1367 | Run dpctl command on all switches.""" |
| 1368 | main.log.info( 'Run dpctl command on all switches' ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 1369 | args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1370 | cmd = args[ "CMD" ] if args[ "CMD" ] is not None else "" |
| 1371 | cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else "" |
| 1372 | command = "dpctl " + cmd + " " + str( cmdargs ) |
| 1373 | try: |
| 1374 | response = self.execute( |
| 1375 | cmd=command, |
| 1376 | prompt="mininet>", |
| 1377 | timeout=10 ) |
| 1378 | except pexpect.EOF: |
| 1379 | main.log.error( self.name + ": EOF exception found" ) |
| 1380 | main.log.error( self.name + ": " + self.handle.before ) |
| 1381 | main.cleanup() |
| 1382 | main.exit() |
| 1383 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1384 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1385 | def getVersion( self ): |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1386 | # FIXME: What uses this? This should be refactored to get |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 1387 | # version from MN and not some other file |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1388 | fileInput = path + '/lib/Mininet/INSTALL' |
| 1389 | version = super( Mininet, self ).getVersion() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1390 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1391 | for line in open( fileInput, 'r' ).readlines(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1392 | result = re.match( pattern, line ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1393 | if result: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1394 | version = result.group( 0 ) |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1395 | return version |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1396 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1397 | def getSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1398 | """ |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1399 | Parameters: |
| 1400 | sw: The name of an OVS switch. Example "s1" |
| 1401 | Return: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1402 | The output of the command from the mininet cli |
| 1403 | or main.FALSE on timeout""" |
| 1404 | command = "sh ovs-vsctl get-controller " + str( sw ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1405 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1406 | response = self.execute( |
| 1407 | cmd=command, |
| 1408 | prompt="mininet>", |
| 1409 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1410 | if response: |
Jon Hall | ec3c21e | 2014-11-10 22:22:37 -0500 | [diff] [blame] | 1411 | return response |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1412 | else: |
| 1413 | return main.FALSE |
| 1414 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1415 | main.log.error( self.name + ": EOF exception found" ) |
| 1416 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1417 | main.cleanup() |
| 1418 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1419 | |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1420 | def assignSwController( self, sw, ip, port="6653", ptcp="" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1421 | """ |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1422 | Description: |
| 1423 | Assign switches to the controllers ( for ovs use only ) |
| 1424 | Required: |
| 1425 | sw - Name of the switch. This can be a list or a string. |
| 1426 | ip - Ip addresses of controllers. This can be a list or a string. |
| 1427 | Optional: |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 1428 | port - ONOS use port 6653, if no list of ports is passed, then |
| 1429 | the all the controller will use 6653 as their port number |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1430 | ptcp - ptcp number, This can be a string or a list that has |
| 1431 | the same length as switch. This is optional and not required |
| 1432 | when using ovs switches. |
| 1433 | NOTE: If switches and ptcp are given in a list type they should have the |
| 1434 | same length and should be in the same order, Eg. sw=[ 's1' ... n ] |
| 1435 | ptcp=[ '6637' ... n ], s1 has ptcp number 6637 and so on. |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 1436 | |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1437 | Return: |
| 1438 | Returns main.TRUE if mininet correctly assigned switches to |
| 1439 | controllers, otherwise it will return main.FALSE or an appropriate |
| 1440 | exception(s) |
| 1441 | """ |
| 1442 | assignResult = main.TRUE |
| 1443 | # Initial ovs command |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1444 | commandList = [] |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1445 | command = "sh ovs-vsctl set-controller " |
| 1446 | onosIp = "" |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1447 | try: |
| 1448 | if isinstance( ip, types.StringType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1449 | onosIp = "tcp:" + str( ip ) + ":" |
kelvin-onlab | 5c2df43 | 2015-06-11 17:29:56 -0700 | [diff] [blame] | 1450 | if isinstance( port, types.StringType ) or \ |
| 1451 | isinstance( port, types.IntType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1452 | onosIp += str( port ) |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1453 | elif isinstance( port, types.ListType ): |
| 1454 | main.log.error( self.name + ": Only one controller " + |
| 1455 | "assigned and a list of ports has" + |
| 1456 | " been passed" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1457 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1458 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1459 | main.log.error( self.name + ": Invalid controller port " + |
| 1460 | "number. Please specify correct " + |
| 1461 | "controller port" ) |
| 1462 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1463 | |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1464 | elif isinstance( ip, types.ListType ): |
kelvin-onlab | 5c2df43 | 2015-06-11 17:29:56 -0700 | [diff] [blame] | 1465 | if isinstance( port, types.StringType ) or \ |
| 1466 | isinstance( port, types.IntType ): |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1467 | for ipAddress in ip: |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1468 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 1469 | str( port ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1470 | elif isinstance( port, types.ListType ): |
| 1471 | if ( len( ip ) != len( port ) ): |
| 1472 | main.log.error( self.name + ": Port list = " + |
| 1473 | str( len( port ) ) + |
| 1474 | "should be the same as controller" + |
| 1475 | " ip list = " + str( len( ip ) ) ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1476 | return main.FALSE |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1477 | else: |
| 1478 | onosIp = "" |
| 1479 | for ipAddress, portNum in zip( ip, port ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1480 | onosIp += "tcp:" + str( ipAddress ) + ":" + \ |
| 1481 | str( portNum ) + " " |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1482 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1483 | main.log.error( self.name + ": Invalid controller port " + |
| 1484 | "number. Please specify correct " + |
| 1485 | "controller port" ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1486 | return main.FALSE |
kelvin-onlab | e5edb9e | 2015-06-12 09:38:47 -0700 | [diff] [blame] | 1487 | else: |
| 1488 | main.log.error( self.name + ": Invalid ip address" ) |
| 1489 | return main.FALSE |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1490 | |
| 1491 | if isinstance( sw, types.StringType ): |
| 1492 | command += sw + " " |
| 1493 | if ptcp: |
| 1494 | if isinstance( ptcp, types.StringType ): |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1495 | command += "ptcp:" + str( ptcp ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1496 | elif isinstance( ptcp, types.ListType ): |
| 1497 | main.log.error( self.name + ": Only one switch is " + |
| 1498 | "being set and multiple PTCP is " + |
| 1499 | "being passed " ) |
| 1500 | else: |
| 1501 | main.log.error( self.name + ": Invalid PTCP" ) |
| 1502 | ptcp = "" |
| 1503 | command += onosIp |
| 1504 | commandList.append( command ) |
| 1505 | |
| 1506 | elif isinstance( sw, types.ListType ): |
| 1507 | if ptcp: |
| 1508 | if isinstance( ptcp, types.ListType ): |
| 1509 | if len( ptcp ) != len( sw ): |
| 1510 | main.log.error( self.name + ": PTCP length = " + |
| 1511 | str( len( ptcp ) ) + |
| 1512 | " is not the same as switch" + |
| 1513 | " length = " + |
| 1514 | str( len( sw ) ) ) |
| 1515 | return main.FALSE |
| 1516 | else: |
| 1517 | for switch, ptcpNum in zip( sw, ptcp ): |
| 1518 | tempCmd = "sh ovs-vsctl set-controller " |
kelvin-onlab | f713d7c | 2015-06-11 14:29:05 -0700 | [diff] [blame] | 1519 | tempCmd += switch + " ptcp:" + \ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1520 | str( ptcpNum ) + " " |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1521 | tempCmd += onosIp |
| 1522 | commandList.append( tempCmd ) |
| 1523 | else: |
| 1524 | main.log.error( self.name + ": Invalid PTCP" ) |
| 1525 | return main.FALSE |
| 1526 | else: |
| 1527 | for switch in sw: |
| 1528 | tempCmd = "sh ovs-vsctl set-controller " |
| 1529 | tempCmd += switch + " " + onosIp |
| 1530 | commandList.append( tempCmd ) |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1531 | else: |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1532 | main.log.error( self.name + ": Invalid switch type " ) |
| 1533 | return main.FALSE |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1534 | |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1535 | for cmd in commandList: |
| 1536 | try: |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 1537 | self.execute( cmd=cmd, prompt="mininet>", timeout=5 ) |
kelvin-onlab | 4f9f7e0 | 2015-06-11 14:07:20 -0700 | [diff] [blame] | 1538 | except pexpect.TIMEOUT: |
| 1539 | main.log.error( self.name + ": pexpect.TIMEOUT found" ) |
| 1540 | return main.FALSE |
| 1541 | except pexpect.EOF: |
| 1542 | main.log.error( self.name + ": EOF exception found" ) |
| 1543 | main.log.error( self.name + ": " + self.handle.before ) |
| 1544 | main.cleanup() |
| 1545 | main.exit() |
| 1546 | return main.TRUE |
| 1547 | except Exception: |
| 1548 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1549 | main.cleanup() |
| 1550 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1551 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1552 | def deleteSwController( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1553 | """ |
| 1554 | Removes the controller target from sw""" |
| 1555 | command = "sh ovs-vsctl del-controller " + str( sw ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1556 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1557 | response = self.execute( |
| 1558 | cmd=command, |
| 1559 | prompt="mininet>", |
| 1560 | timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1561 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1562 | main.log.error( self.name + ": EOF exception found" ) |
| 1563 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1564 | main.cleanup() |
| 1565 | main.exit() |
| 1566 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1567 | main.log.info( response ) |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1568 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1569 | def addSwitch( self, sw, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1570 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1571 | adds a switch to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1572 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1573 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1574 | NOTE: cannot currently specify what type of switch |
| 1575 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1576 | sw = name of the new switch as a string |
| 1577 | optional keywords: |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1578 | dpid = "dpid" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1579 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1580 | """ |
| 1581 | dpid = kwargs.get( 'dpid', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1582 | command = "addswitch " + str( sw ) + " " + str( dpid ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1583 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1584 | response = self.execute( |
| 1585 | cmd=command, |
| 1586 | prompt="mininet>", |
| 1587 | timeout=10 ) |
| 1588 | if re.search( "already exists!", response ): |
| 1589 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1590 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1591 | elif re.search( "Error", response ): |
| 1592 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1593 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1594 | elif re.search( "usage:", response ): |
| 1595 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1596 | return main.FALSE |
| 1597 | else: |
| 1598 | return main.TRUE |
| 1599 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1600 | main.log.error( self.name + ": EOF exception found" ) |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1601 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1602 | main.cleanup() |
| 1603 | main.exit() |
| 1604 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1605 | def delSwitch( self, sw ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1606 | """ |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1607 | delete a switch from the mininet topology |
| 1608 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1609 | dynamic_topo branch |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1610 | required params: |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1611 | sw = name of the switch as a string |
| 1612 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1613 | command = "delswitch " + str( sw ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1614 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1615 | response = self.execute( |
| 1616 | cmd=command, |
| 1617 | prompt="mininet>", |
| 1618 | timeout=10 ) |
| 1619 | if re.search( "no switch named", response ): |
| 1620 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1621 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1622 | elif re.search( "Error", response ): |
| 1623 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1624 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1625 | elif re.search( "usage:", response ): |
| 1626 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1627 | return main.FALSE |
| 1628 | else: |
| 1629 | return main.TRUE |
| 1630 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1631 | main.log.error( self.name + ": EOF exception found" ) |
| 1632 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1633 | main.cleanup() |
| 1634 | main.exit() |
| 1635 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1636 | def addLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1637 | """ |
| 1638 | add a link to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1639 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1640 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1641 | NOTE: cannot currently specify what type of link |
| 1642 | required params: |
| 1643 | node1 = the string node name of the first endpoint of the link |
| 1644 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1645 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1646 | command = "addlink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1647 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1648 | response = self.execute( |
| 1649 | cmd=command, |
| 1650 | prompt="mininet>", |
| 1651 | timeout=10 ) |
| 1652 | if re.search( "doesnt exist!", response ): |
| 1653 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1654 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1655 | elif re.search( "Error", response ): |
| 1656 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1657 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1658 | elif re.search( "usage:", response ): |
| 1659 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1660 | return main.FALSE |
| 1661 | else: |
| 1662 | return main.TRUE |
| 1663 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1664 | main.log.error( self.name + ": EOF exception found" ) |
| 1665 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1666 | main.cleanup() |
| 1667 | main.exit() |
| 1668 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1669 | def delLink( self, node1, node2 ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1670 | """ |
| 1671 | delete a link from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1672 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1673 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1674 | required params: |
| 1675 | node1 = the string node name of the first endpoint of the link |
| 1676 | node2 = the string node name of the second endpoint of the link |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1677 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1678 | command = "dellink " + str( node1 ) + " " + str( node2 ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1679 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1680 | response = self.execute( |
| 1681 | cmd=command, |
| 1682 | prompt="mininet>", |
| 1683 | timeout=10 ) |
| 1684 | if re.search( "no node named", response ): |
| 1685 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1686 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1687 | elif re.search( "Error", response ): |
| 1688 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1689 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1690 | elif re.search( "usage:", response ): |
| 1691 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1692 | return main.FALSE |
| 1693 | else: |
| 1694 | return main.TRUE |
| 1695 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1696 | main.log.error( self.name + ": EOF exception found" ) |
| 1697 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1698 | main.cleanup() |
| 1699 | main.exit() |
| 1700 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1701 | def addHost( self, hostname, **kwargs ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1702 | """ |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1703 | Add a host to the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1704 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1705 | dynamic_topo branch |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1706 | NOTE: cannot currently specify what type of host |
| 1707 | required params: |
| 1708 | hostname = the string hostname |
| 1709 | optional key-value params |
| 1710 | switch = "switch name" |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1711 | returns: main.FALSE on an error, else main.TRUE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1712 | """ |
| 1713 | switch = kwargs.get( 'switch', '' ) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1714 | command = "addhost " + str( hostname ) + " " + str( switch ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1715 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1716 | response = self.execute( |
| 1717 | cmd=command, |
| 1718 | prompt="mininet>", |
| 1719 | timeout=10 ) |
| 1720 | if re.search( "already exists!", response ): |
| 1721 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1722 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1723 | elif re.search( "doesnt exists!", response ): |
| 1724 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1725 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1726 | elif re.search( "Error", response ): |
| 1727 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1728 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1729 | elif re.search( "usage:", response ): |
| 1730 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1731 | return main.FALSE |
| 1732 | else: |
| 1733 | return main.TRUE |
| 1734 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1735 | main.log.error( self.name + ": EOF exception found" ) |
| 1736 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1737 | main.cleanup() |
| 1738 | main.exit() |
| 1739 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 1740 | def delHost( self, hostname ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1741 | """ |
| 1742 | delete a host from the mininet topology |
Jon Hall | be6dfc4 | 2015-01-12 17:37:25 -0800 | [diff] [blame] | 1743 | NOTE: This uses a custom mn function. MN repo should be on |
Jon Hall | 272a4db | 2015-01-12 17:43:48 -0800 | [diff] [blame] | 1744 | dynamic_topo branch |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1745 | NOTE: this uses a custom mn function |
| 1746 | required params: |
| 1747 | hostname = the string hostname |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1748 | returns: main.FALSE on an error, else main.TRUE""" |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 1749 | command = "delhost " + str( hostname ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1750 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1751 | response = self.execute( |
| 1752 | cmd=command, |
| 1753 | prompt="mininet>", |
| 1754 | timeout=10 ) |
| 1755 | if re.search( "no host named", response ): |
| 1756 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1757 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1758 | elif re.search( "Error", response ): |
| 1759 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1760 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1761 | elif re.search( "usage:", response ): |
| 1762 | main.log.warn( response ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1763 | return main.FALSE |
| 1764 | else: |
| 1765 | return main.TRUE |
| 1766 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1767 | main.log.error( self.name + ": EOF exception found" ) |
| 1768 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 1769 | main.cleanup() |
| 1770 | main.exit() |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 1771 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1772 | def disconnect( self ): |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1773 | """ |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1774 | Called at the end of the test to stop the mininet and |
| 1775 | disconnect the handle. |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1776 | """ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1777 | self.handle.sendline( '' ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1778 | i = self.handle.expect( [ 'mininet>', pexpect.EOF, pexpect.TIMEOUT ], |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1779 | timeout=2 ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1780 | response = main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1781 | if i == 0: |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 1782 | response = self.stopNet() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1783 | elif i == 1: |
| 1784 | return main.TRUE |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1785 | # print "Disconnecting Mininet" |
| 1786 | if self.handle: |
| 1787 | self.handle.sendline( "exit" ) |
| 1788 | self.handle.expect( "exit" ) |
| 1789 | self.handle.expect( "(.*)" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1790 | else: |
| 1791 | main.log.error( "Connection failed to the host" ) |
kelvin-onlab | a148458 | 2015-02-02 15:46:20 -0800 | [diff] [blame] | 1792 | return response |
| 1793 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1794 | def stopNet( self, fileName="", timeout=5 ): |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1795 | """ |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1796 | Stops mininet. |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 1797 | Returns main.TRUE if the mininet successfully stops and |
Jon Hall | 21270ac | 2015-02-16 17:59:55 -0800 | [diff] [blame] | 1798 | main.FALSE if the pexpect handle does not exist. |
| 1799 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1800 | Will cleanup and exit the test if mininet fails to stop |
kelvin-onlab | 00ac67b | 2015-02-04 09:52:02 -0800 | [diff] [blame] | 1801 | """ |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1802 | main.log.info( self.name + ": Stopping mininet..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1803 | response = '' |
| 1804 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1805 | try: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1806 | self.handle.sendline( "" ) |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1807 | i = self.handle.expect( [ 'mininet>', |
| 1808 | '\$', |
| 1809 | pexpect.EOF, |
| 1810 | pexpect.TIMEOUT ], |
| 1811 | timeout ) |
| 1812 | if i == 0: |
| 1813 | main.log.info( "Exiting mininet..." ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1814 | response = self.execute( |
| 1815 | cmd="exit", |
| 1816 | prompt="(.*)", |
| 1817 | timeout=120 ) |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1818 | main.log.info( self.name + ": Stopped" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1819 | self.handle.sendline( "sudo mn -c" ) |
shahshreya | 328c2a7 | 2014-11-17 10:19:50 -0800 | [diff] [blame] | 1820 | response = main.TRUE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1821 | |
kelvin-onlab | 56a3f46 | 2015-02-06 14:04:43 -0800 | [diff] [blame] | 1822 | if i == 1: |
| 1823 | main.log.info( " Mininet trying to exit while not " + |
| 1824 | "in the mininet prompt" ) |
| 1825 | elif i == 2: |
| 1826 | main.log.error( "Something went wrong exiting mininet" ) |
| 1827 | elif i == 3: # timeout |
| 1828 | main.log.error( "Something went wrong exiting mininet " + |
| 1829 | "TIMEOUT" ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 1830 | |
Hari Krishna | b35c6d0 | 2015-03-18 11:13:51 -0700 | [diff] [blame] | 1831 | if fileName: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 1832 | self.handle.sendline( "" ) |
| 1833 | self.handle.expect( '\$' ) |
| 1834 | self.handle.sendline( |
| 1835 | "sudo kill -9 \`ps -ef | grep \"" + |
| 1836 | fileName + |
| 1837 | "\" | grep -v grep | awk '{print $2}'\`" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1838 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1839 | main.log.error( self.name + ": EOF exception found" ) |
| 1840 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 1841 | main.cleanup() |
| 1842 | main.exit() |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1843 | else: |
| 1844 | main.log.error( self.name + ": Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1845 | response = main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1846 | return response |
| 1847 | |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1848 | def arping( self, srcHost="", dstHost="10.128.20.211", ethDevice="" ): |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1849 | """ |
| 1850 | Description: |
| 1851 | Sends arp message from mininet host for hosts discovery |
| 1852 | Required: |
| 1853 | host - hosts name |
| 1854 | Optional: |
| 1855 | ip - ip address that does not exist in the network so there would |
| 1856 | be no reply. |
| 1857 | """ |
kelvin-onlab | f0594d7 | 2015-05-19 17:25:12 -0700 | [diff] [blame] | 1858 | if ethDevice: |
| 1859 | ethDevice = '-I ' + ethDevice + ' ' |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1860 | cmd = srcHost + " arping -c1 " + ethDevice + dstHost |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1861 | try: |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1862 | main.log.info( "Sending: " + cmd ) |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1863 | self.handle.sendline( cmd ) |
Jon Hall | a5cb341 | 2015-08-18 14:08:22 -0700 | [diff] [blame] | 1864 | i = self.handle.expect( [ "mininet>", "arping: " ] ) |
| 1865 | if i == 0: |
| 1866 | return main.TRUE |
| 1867 | elif i == 1: |
| 1868 | response = self.handle.before + self.handle.after |
| 1869 | self.handle.expect( "mininet>" ) |
| 1870 | response += self.handle.before + self.handle.after |
| 1871 | main.log.warn( "Error sending arping, output was: " + |
| 1872 | response ) |
| 1873 | return main.FALSE |
| 1874 | except pexpect.TIMEOUT: |
| 1875 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1876 | main.log.warn( self.handle.before ) |
| 1877 | return main.FALSE |
kelvin-onlab | 65782a8 | 2015-05-07 14:12:13 -0700 | [diff] [blame] | 1878 | except pexpect.EOF: |
| 1879 | main.log.error( self.name + ": EOF exception found" ) |
| 1880 | main.log.error( self.name + ": " + self.handle.before ) |
| 1881 | main.cleanup() |
| 1882 | main.exit() |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 1883 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1884 | def decToHex( self, num ): |
| 1885 | return hex( num ).split( 'x' )[ 1 ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1886 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1887 | def getSwitchFlowCount( self, switch ): |
| 1888 | """ |
| 1889 | return the Flow Count of the switch""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1890 | if self.handle: |
| 1891 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 1892 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1893 | response = self.execute( |
| 1894 | cmd=cmd, |
| 1895 | prompt="mininet>", |
| 1896 | timeout=10 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1897 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1898 | main.log.error( self.name + ": EOF exception found" ) |
| 1899 | main.log.error( self.name + " " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1900 | main.cleanup() |
| 1901 | main.exit() |
| 1902 | pattern = "flow_count=(\d+)" |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1903 | result = re.search( pattern, response, re.MULTILINE ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1904 | if result is None: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1905 | main.log.info( |
| 1906 | "Couldn't find flows on switch %s, found: %s" % |
| 1907 | ( switch, response ) ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1908 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1909 | return result.group( 1 ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 1910 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 1911 | main.log.error( "Connection failed to the Mininet host" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 1912 | |
Jon Hall | 9ed8f37 | 2016-02-24 17:34:07 -0800 | [diff] [blame] | 1913 | def checkFlows( self, sw, dumpFormat=None ): |
| 1914 | if dumpFormat: |
| 1915 | command = "sh ovs-ofctl -F " + \ |
| 1916 | dumpFormat + " dump-flows " + str( sw ) |
| 1917 | else: |
| 1918 | command = "sh ovs-ofctl dump-flows " + str( sw ) |
| 1919 | try: |
| 1920 | response = self.execute( |
| 1921 | cmd=command, |
| 1922 | prompt="mininet>", |
| 1923 | timeout=10 ) |
| 1924 | return response |
| 1925 | except pexpect.EOF: |
| 1926 | main.log.error( self.name + ": EOF exception found" ) |
| 1927 | main.log.error( self.name + ": " + self.handle.before ) |
| 1928 | main.cleanup() |
| 1929 | main.exit() |
| 1930 | |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 1931 | def flowTableComp( self, flowTable1, flowTable2 ): |
| 1932 | # This function compares the selctors and treatments of each flow |
| 1933 | try: |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 1934 | returnValue = main.TRUE |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 1935 | if len(flowTable1) != len(flowTable2): |
| 1936 | main.log.warn( "Flow table lengths do not match" ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 1937 | returnValue = main.FALSE |
GlennRC | fa65fce | 2015-12-16 13:16:08 -0800 | [diff] [blame] | 1938 | dFields = ["n_bytes", "cookie", "n_packets", "duration"] |
| 1939 | for flow1, flow2 in zip(flowTable1, flowTable2): |
Jon Hall | acd1b18 | 2015-12-17 11:43:20 -0800 | [diff] [blame] | 1940 | for field in dFields: |
| 1941 | try: |
| 1942 | flow1.pop( field ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 1943 | except KeyError: |
| 1944 | pass |
Jon Hall | acd1b18 | 2015-12-17 11:43:20 -0800 | [diff] [blame] | 1945 | try: |
| 1946 | flow2.pop( field ) |
Jon Hall | 41d39f1 | 2016-04-11 22:54:35 -0700 | [diff] [blame] | 1947 | except KeyError: |
| 1948 | pass |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 1949 | for i in range( len(flowTable1) ): |
GlennRC | 17bbcf5 | 2015-12-14 17:31:50 -0800 | [diff] [blame] | 1950 | if flowTable1[i] not in flowTable2: |
| 1951 | main.log.warn( "Flow tables do not match:" ) |
| 1952 | 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] | 1953 | returnValue = main.FALSE |
| 1954 | break |
| 1955 | return returnValue |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 1956 | except Exception: |
| 1957 | main.log.exception( "Uncaught exception!" ) |
| 1958 | main.cleanup() |
| 1959 | main.exit() |
Jon Hall | 9043c90 | 2015-07-30 14:23:44 -0700 | [diff] [blame] | 1960 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 1961 | def parseFlowTable( self, flowTable, version="", debug=True ): |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1962 | ''' |
| 1963 | Discription: Parses flows into json format. |
| 1964 | NOTE: this can parse any string thats separated with commas |
| 1965 | Arguments: |
| 1966 | Required: |
| 1967 | flows: a list of strings that represnt flows |
| 1968 | Optional: |
| 1969 | version: The version of OpenFlow. Currently, 1.3 and 1.0 are supported. |
| 1970 | debug: prints out the final result |
| 1971 | returns: A list of flows in json format |
| 1972 | ''' |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 1973 | jsonFlowTable = [] |
| 1974 | for flow in flowTable: |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 1975 | jsonFlow = {} |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 1976 | # split up the fields of the flow |
| 1977 | parsedFlow = flow.split(", ") |
| 1978 | # get rid of any spaces in front of the field |
| 1979 | for i in range( len(parsedFlow) ): |
| 1980 | item = parsedFlow[i] |
| 1981 | if item[0] == " ": |
| 1982 | parsedFlow[i] = item[1:] |
| 1983 | # grab the selector and treatment from the parsed flow |
| 1984 | # the last element is the selector and the treatment |
| 1985 | temp = parsedFlow.pop(-1) |
| 1986 | # split up the selector and the treatment |
| 1987 | temp = temp.split(" ") |
| 1988 | index = 0 |
| 1989 | # parse the flags |
| 1990 | # NOTE: This only parses one flag |
| 1991 | flag = {} |
| 1992 | if version == "1.3": |
| 1993 | flag = {"flag":[temp[index]]} |
| 1994 | index += 1 |
| 1995 | # the first element is the selector and split it up |
| 1996 | sel = temp[index] |
| 1997 | index += 1 |
| 1998 | sel = sel.split(",") |
| 1999 | # the priority is stuck in the selecter so put it back |
| 2000 | # in the flow |
| 2001 | parsedFlow.append(sel.pop(0)) |
| 2002 | # parse selector |
| 2003 | criteria = [] |
| 2004 | for item in sel: |
| 2005 | # this is the type of the packet e.g. "arp" |
| 2006 | if "=" not in item: |
| 2007 | criteria.append( {"type":item} ) |
| 2008 | else: |
| 2009 | field = item.split("=") |
| 2010 | criteria.append( {field[0]:field[1]} ) |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 2011 | selector = {"selector": {"criteria":sorted(criteria)} } |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2012 | treat = temp[index] |
| 2013 | # get rid of the action part e.g. "action=output:2" |
| 2014 | # we will add it back later |
| 2015 | treat = treat.split("=") |
| 2016 | treat.pop(0) |
| 2017 | # parse treatment |
| 2018 | action = [] |
| 2019 | for item in treat: |
| 2020 | field = item.split(":") |
| 2021 | action.append( {field[0]:field[1]} ) |
| 2022 | # create the treatment field and add the actions |
GlennRC | 68467eb | 2015-11-16 18:01:01 -0800 | [diff] [blame] | 2023 | treatment = {"treatment": {"action":sorted(action)} } |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2024 | # parse the rest of the flow |
| 2025 | for item in parsedFlow: |
| 2026 | field = item.split("=") |
| 2027 | jsonFlow.update( {field[0]:field[1]} ) |
| 2028 | # add the treatment and the selector to the json flow |
| 2029 | jsonFlow.update( selector ) |
| 2030 | jsonFlow.update( treatment ) |
| 2031 | jsonFlow.update( flag ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2032 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2033 | 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] | 2034 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2035 | # add the json flow to the json flow table |
| 2036 | jsonFlowTable.append( jsonFlow ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2037 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2038 | return jsonFlowTable |
| 2039 | |
Jon Hall | 0a54379 | 2015-12-14 11:00:26 -0800 | [diff] [blame] | 2040 | def getFlowTable( self, sw, version="", debug=False): |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2041 | ''' |
| 2042 | Discription: Returns the flow table(s) on a switch or switches in a list. |
| 2043 | Each element is a flow. |
| 2044 | Arguments: |
| 2045 | Required: |
| 2046 | sw: The switch name ("s1") to retrive the flow table. Can also be |
| 2047 | a list of switches. |
| 2048 | Optional: |
| 2049 | version: The version of OpenFlow. Currently, 1.3 and 1.0 are supported. |
| 2050 | debug: prints out the final result |
| 2051 | ''' |
| 2052 | try: |
| 2053 | switches = [] |
| 2054 | if type(sw) is list: |
Jon Hall | ca7ac29 | 2015-11-11 09:28:12 -0800 | [diff] [blame] | 2055 | switches.extend(sw) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2056 | else: switches.append(sw) |
| 2057 | |
| 2058 | flows = [] |
| 2059 | for s in switches: |
| 2060 | cmd = "sh ovs-ofctl dump-flows " + s |
| 2061 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2062 | if "1.0" == version: |
| 2063 | cmd += " -F OpenFlow10-table_id" |
| 2064 | elif "1.3" == version: |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2065 | cmd += " -O OpenFlow13" |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2066 | |
| 2067 | main.log.info( "Sending: " + cmd ) |
| 2068 | self.handle.sendline( cmd ) |
| 2069 | self.handle.expect( "mininet>" ) |
| 2070 | response = self.handle.before |
| 2071 | response = response.split( "\r\n" ) |
| 2072 | # dump the first two elements and the last |
| 2073 | # the first element is the command that was sent |
| 2074 | # the second is the table header |
| 2075 | # the last element is empty |
| 2076 | response = response[2:-1] |
| 2077 | flows.extend( response ) |
| 2078 | |
| 2079 | if debug: print "Flows:\n{}\n\n".format(flows) |
| 2080 | |
GlennRC | 528ad29 | 2015-11-12 10:38:18 -0800 | [diff] [blame] | 2081 | return self.parseFlowTable( flows, version, debug ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2082 | |
| 2083 | except pexpect.TIMEOUT: |
| 2084 | main.log.exception( self.name + ": Command timed out" ) |
| 2085 | return None |
| 2086 | except pexpect.EOF: |
| 2087 | main.log.exception( self.name + ": connection closed." ) |
| 2088 | main.cleanup() |
| 2089 | main.exit() |
| 2090 | except Exception: |
| 2091 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2092 | main.cleanup() |
| 2093 | main.exit() |
| 2094 | |
| 2095 | def checkFlowId( self, sw, flowId, version="1.3", debug=True ): |
| 2096 | ''' |
| 2097 | Discription: Checks whether the ID provided matches a flow ID in Mininet |
| 2098 | Arguments: |
| 2099 | Required: |
| 2100 | sw: The switch name ("s1") to retrive the flow table. Can also be |
| 2101 | a list of switches. |
| 2102 | flowId: the flow ID in hex format. Can also be a list of IDs |
| 2103 | Optional: |
| 2104 | version: The version of OpenFlow. Currently, 1.3 and 1.0 are supported. |
| 2105 | debug: prints out the final result |
| 2106 | returns: main.TRUE if all IDs are present, otherwise returns main.FALSE |
| 2107 | NOTE: prints out IDs that are not present |
| 2108 | ''' |
| 2109 | try: |
| 2110 | main.log.info( "Getting flows from Mininet" ) |
| 2111 | flows = self.getFlowTable( sw, version, debug ) |
| 2112 | |
| 2113 | if debug: print "flow ids:\n{}\n\n".format(flowId) |
| 2114 | |
| 2115 | # Check flowId is a list or a string |
| 2116 | if type( flowId ) is str: |
| 2117 | result = False |
| 2118 | for f in flows: |
| 2119 | if flowId in f.get( 'cookie' ): |
| 2120 | result = True |
| 2121 | break |
| 2122 | # flowId is a list |
| 2123 | else: |
| 2124 | result = True |
| 2125 | # Get flow IDs from Mininet |
| 2126 | mnFlowIds = [ f.get( 'cookie' ) for f in flows ] |
| 2127 | # Save the IDs that are not in Mininet |
| 2128 | absentIds = [ x for x in flowId if x not in mnFlowIds ] |
| 2129 | |
| 2130 | if debug: print "mn flow ids:\n{}\n\n".format(mnFlowIds) |
| 2131 | |
| 2132 | # Print out the IDs that are not in Mininet |
| 2133 | if absentIds: |
| 2134 | main.log.warn( "Absent ids: {}".format( absentIds ) ) |
| 2135 | result = False |
| 2136 | |
| 2137 | return main.TRUE if result else main.FALSE |
| 2138 | |
| 2139 | except Exception: |
| 2140 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2141 | main.cleanup() |
| 2142 | main.exit() |
| 2143 | |
| 2144 | |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 2145 | def startTcpdump( self, filename, intf="eth0", port="port 6653" ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2146 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2147 | Runs tpdump on an interface and saves the file |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2148 | intf can be specified, or the default eth0 is used""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2149 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2150 | self.handle.sendline( "" ) |
| 2151 | self.handle.expect( "mininet>" ) |
| 2152 | self.handle.sendline( |
| 2153 | "sh sudo tcpdump -n -i " + |
| 2154 | intf + |
| 2155 | " " + |
| 2156 | port + |
| 2157 | " -w " + |
| 2158 | filename.strip() + |
| 2159 | " &" ) |
| 2160 | self.handle.sendline( "" ) |
| 2161 | i = self.handle.expect( [ 'No\ssuch\device', |
| 2162 | 'listening\son', |
| 2163 | pexpect.TIMEOUT, |
| 2164 | "mininet>" ], |
| 2165 | timeout=10 ) |
| 2166 | main.log.warn( self.handle.before + self.handle.after ) |
| 2167 | self.handle.sendline( "" ) |
| 2168 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2169 | if i == 0: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2170 | main.log.error( |
| 2171 | self.name + |
| 2172 | ": tcpdump - No such device exists. " + |
| 2173 | "tcpdump attempted on: " + |
| 2174 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2175 | return main.FALSE |
| 2176 | elif i == 1: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2177 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2178 | return main.TRUE |
| 2179 | elif i == 2: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2180 | main.log.error( |
| 2181 | self.name + |
| 2182 | ": tcpdump command timed out! Check interface name," + |
| 2183 | " given interface was: " + |
| 2184 | intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2185 | return main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2186 | elif i == 3: |
| 2187 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2188 | return main.TRUE |
| 2189 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2190 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2191 | return main.FALSE |
| 2192 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2193 | main.log.error( self.name + ": EOF exception found" ) |
| 2194 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2195 | main.cleanup() |
| 2196 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2197 | except Exception: |
| 2198 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2199 | main.cleanup() |
| 2200 | main.exit() |
| 2201 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2202 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2203 | """ |
| 2204 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2205 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2206 | self.handle.sendline( "sh sudo pkill tcpdump" ) |
| 2207 | self.handle.expect( "mininet>" ) |
| 2208 | self.handle.sendline( "" ) |
| 2209 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2210 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2211 | main.log.error( self.name + ": EOF exception found" ) |
| 2212 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2213 | main.cleanup() |
| 2214 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 2215 | except Exception: |
| 2216 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2217 | main.cleanup() |
| 2218 | main.exit() |
| 2219 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2220 | def getPorts( self, nodeName, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2221 | """ |
| 2222 | Read ports from a Mininet switch. |
| 2223 | |
| 2224 | Returns a json structure containing information about the |
| 2225 | ports of the given switch. |
| 2226 | """ |
| 2227 | response = self.getInterfaces( nodeName ) |
| 2228 | # TODO: Sanity check on response. log if no such switch exists |
| 2229 | ports = [] |
| 2230 | for line in response.split( "\n" ): |
| 2231 | if not line.startswith( "name=" ): |
| 2232 | continue |
| 2233 | portVars = {} |
| 2234 | for var in line.split( "," ): |
| 2235 | key, value = var.split( "=" ) |
| 2236 | portVars[ key ] = value |
| 2237 | isUp = portVars.pop( 'enabled', "True" ) |
| 2238 | isUp = "True" in isUp |
| 2239 | if verbose: |
| 2240 | main.log.info( "Reading switch port %s(%s)" % |
| 2241 | ( portVars[ 'name' ], portVars[ 'mac' ] ) ) |
| 2242 | mac = portVars[ 'mac' ] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2243 | if mac == 'None': |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2244 | mac = None |
| 2245 | ips = [] |
| 2246 | ip = portVars[ 'ip' ] |
| 2247 | if ip == 'None': |
| 2248 | ip = None |
| 2249 | ips.append( ip ) |
| 2250 | name = portVars[ 'name' ] |
| 2251 | if name == 'None': |
| 2252 | name = None |
| 2253 | portRe = r'[^\-]\d\-eth(?P<port>\d+)' |
| 2254 | if name == 'lo': |
| 2255 | portNo = 0xfffe # TODO: 1.0 value - Should we just return lo? |
| 2256 | else: |
| 2257 | portNo = re.search( portRe, name ).group( 'port' ) |
| 2258 | ports.append( { 'of_port': portNo, |
| 2259 | 'mac': str( mac ).replace( '\'', '' ), |
| 2260 | 'name': name, |
| 2261 | 'ips': ips, |
| 2262 | 'enabled': isUp } ) |
| 2263 | return ports |
| 2264 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2265 | def getSwitches( self, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2266 | """ |
| 2267 | Read switches from Mininet. |
| 2268 | |
| 2269 | Returns a dictionary whose keys are the switch names and the value is |
| 2270 | a dictionary containing information about the switch. |
| 2271 | """ |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2272 | # NOTE: To support new Mininet switch classes, just append the new |
| 2273 | # class to the switchClasses variable |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2274 | |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2275 | # Regex patterns to parse 'dump' output |
| 2276 | # Example Switches: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2277 | # <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] | 2278 | # <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] | 2279 | # <OVSSwitchNS s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None,s1-eth3:None pid=22550> |
| 2280 | # <OVSBridge s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=26830> |
| 2281 | # <UserSwitch s1: lo:127.0.0.1,s1-eth1:None,s1-eth2:None pid=14737> |
| 2282 | switchClasses = r"(OVSSwitch)|(OVSBridge)|(OVSSwitchNS)|(IVSSwitch)|(LinuxBridge)|(UserSwitch)" |
| 2283 | swRE = r"<(?P<class>" + switchClasses + r")" +\ |
| 2284 | r"(?P<options>\{.*\})?\s" +\ |
| 2285 | r"(?P<name>[^:]+)\:\s" +\ |
| 2286 | r"(?P<ports>([^,]+,)*[^,\s]+)" +\ |
| 2287 | r"\spid=(?P<pid>(\d)+)" |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2288 | # Update mn port info |
| 2289 | self.update() |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2290 | output = {} |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2291 | dump = self.dump().split( "\n" ) |
| 2292 | for line in dump: |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2293 | result = re.search( swRE, line, re.I ) |
| 2294 | if result: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2295 | name = result.group( 'name' ) |
| 2296 | dpid = str( self.getSwitchDPID( name ) ).zfill( 16 ) |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2297 | pid = result.group( 'pid' ) |
| 2298 | swClass = result.group( 'class' ) |
| 2299 | options = result.group( 'options' ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2300 | if verbose: |
| 2301 | main.log.info( "Reading switch %s(%s)" % ( name, dpid ) ) |
| 2302 | ports = self.getPorts( name ) |
Jon Hall | a22481b | 2015-07-28 17:46:01 -0700 | [diff] [blame] | 2303 | output[ name ] = { "dpid": dpid, |
| 2304 | "ports": ports, |
| 2305 | "swClass": swClass, |
| 2306 | "pid": pid, |
| 2307 | "options": options } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2308 | return output |
| 2309 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2310 | def getHosts( self, verbose=False ): |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2311 | """ |
| 2312 | Read hosts from Mininet. |
| 2313 | |
| 2314 | Returns a dictionary whose keys are the host names and the value is |
| 2315 | a dictionary containing information about the host. |
| 2316 | """ |
| 2317 | # Regex patterns to parse dump output |
| 2318 | # Example host: <Host h1: h1-eth0:10.0.0.1 pid=5227> |
kelvin-onlab | 299ab06 | 2015-07-15 10:58:27 -0700 | [diff] [blame] | 2319 | # <Host h1: pid=12725> |
| 2320 | # <VLANHost h12: h12-eth0.100.100.100:100.1.0.3 pid=30186> |
| 2321 | # <dualStackHost h19: h19-eth0:10.1.0.9 pid=30200> |
| 2322 | # <IPv6Host h18: h18-eth0:10.0.0.18 pid=30198> |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2323 | # NOTE: Does not correctly match hosts with multi-links |
| 2324 | # <Host h2: h2-eth0:10.0.0.2,h2-eth1:10.0.1.2 pid=14386> |
| 2325 | # FIXME: Fix that |
kelvin-onlab | d48a68c | 2015-07-13 16:01:36 -0700 | [diff] [blame] | 2326 | hostRE = r"Host\s(?P<name>[^:]+)\:((\s(?P<ifname>[^:]+)\:" +\ |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2327 | "(?P<ip>[^\s]+))|(\s)\spid=(?P<pid>[^>]+))" |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2328 | # update mn port info |
| 2329 | self.update() |
| 2330 | # Get mininet dump |
| 2331 | dump = self.dump().split( "\n" ) |
Jon Hall | 5b0120a | 2015-06-12 17:35:53 -0700 | [diff] [blame] | 2332 | hosts = {} |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2333 | for line in dump: |
kelvin-onlab | d48a68c | 2015-07-13 16:01:36 -0700 | [diff] [blame] | 2334 | if "Host" in line : |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2335 | result = re.search( hostRE, line ) |
| 2336 | name = result.group( 'name' ) |
| 2337 | interfaces = [] |
| 2338 | response = self.getInterfaces( name ) |
| 2339 | # Populate interface info |
| 2340 | for line in response.split( "\n" ): |
| 2341 | if line.startswith( "name=" ): |
| 2342 | portVars = {} |
| 2343 | for var in line.split( "," ): |
| 2344 | key, value = var.split( "=" ) |
| 2345 | portVars[ key ] = value |
| 2346 | isUp = portVars.pop( 'enabled', "True" ) |
| 2347 | isUp = "True" in isUp |
| 2348 | if verbose: |
| 2349 | main.log.info( "Reading host port %s(%s)" % |
| 2350 | ( portVars[ 'name' ], |
| 2351 | portVars[ 'mac' ] ) ) |
| 2352 | mac = portVars[ 'mac' ] |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2353 | if mac == 'None': |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2354 | mac = None |
| 2355 | ips = [] |
| 2356 | ip = portVars[ 'ip' ] |
| 2357 | if ip == 'None': |
| 2358 | ip = None |
| 2359 | ips.append( ip ) |
| 2360 | intfName = portVars[ 'name' ] |
| 2361 | if name == 'None': |
| 2362 | name = None |
| 2363 | interfaces.append( { |
| 2364 | "name": intfName, |
| 2365 | "ips": ips, |
| 2366 | "mac": str( mac ), |
| 2367 | "isUp": isUp } ) |
Jon Hall | 5b0120a | 2015-06-12 17:35:53 -0700 | [diff] [blame] | 2368 | hosts[ name ] = { "interfaces": interfaces } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2369 | return hosts |
| 2370 | |
| 2371 | def getLinks( self ): |
| 2372 | """ |
| 2373 | Gathers information about current Mininet links. These links may not |
| 2374 | be up if one of the ports is down. |
| 2375 | |
| 2376 | Returns a list of dictionaries with link endpoints. |
| 2377 | |
| 2378 | The dictionary structure is: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2379 | { 'node1': str( node1 name ) |
| 2380 | 'node2': str( node2 name ) |
| 2381 | 'port1': str( port1 of_port ) |
| 2382 | 'port2': str( port2 of_port ) } |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2383 | Note: The port number returned is the eth#, not necessarily the of_port |
| 2384 | number. In Mininet, for OVS switch, these should be the same. For |
| 2385 | hosts, this is just the eth#. |
| 2386 | """ |
| 2387 | self.update() |
| 2388 | response = self.links().split( '\n' ) |
| 2389 | |
| 2390 | # Examples: |
| 2391 | # s1-eth3<->s2-eth1 (OK OK) |
| 2392 | # s13-eth3<->h27-eth0 (OK OK) |
| 2393 | linkRE = "(?P<node1>[\w]+)\-eth(?P<port1>[\d]+)\<\-\>" +\ |
| 2394 | "(?P<node2>[\w]+)\-eth(?P<port2>[\d]+)" |
| 2395 | links = [] |
| 2396 | for line in response: |
| 2397 | match = re.search( linkRE, line ) |
| 2398 | if match: |
| 2399 | node1 = match.group( 'node1' ) |
| 2400 | node2 = match.group( 'node2' ) |
| 2401 | port1 = match.group( 'port1' ) |
| 2402 | port2 = match.group( 'port2' ) |
| 2403 | links.append( { 'node1': node1, |
| 2404 | 'node2': node2, |
| 2405 | 'port1': port1, |
| 2406 | 'port2': port2 } ) |
| 2407 | return links |
| 2408 | |
| 2409 | def compareSwitches( self, switches, switchesJson, portsJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2410 | """ |
| 2411 | Compare mn and onos switches |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2412 | switchesJson: parsed json object from the onos devices api |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2413 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2414 | Dependencies: |
| 2415 | 1. numpy - "sudo pip install numpy" |
| 2416 | """ |
| 2417 | from numpy import uint64 |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2418 | # created sorted list of dpid's in MN and ONOS for comparison |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2419 | mnDPIDs = [] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2420 | for swName, switch in switches.iteritems(): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2421 | mnDPIDs.append( switch[ 'dpid' ].lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2422 | mnDPIDs.sort() |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2423 | if switchesJson == "": # if rest call fails |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2424 | main.log.error( |
| 2425 | self.name + |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 2426 | ".compareSwitches(): Empty JSON object given from ONOS" ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2427 | return main.FALSE |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2428 | onos = switchesJson |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2429 | onosDPIDs = [] |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2430 | for switch in onos: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2431 | if switch[ 'available' ]: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2432 | onosDPIDs.append( |
| 2433 | switch[ 'id' ].replace( |
| 2434 | ":", |
| 2435 | '' ).replace( |
| 2436 | "of", |
| 2437 | '' ).lower() ) |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2438 | onosDPIDs.sort() |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2439 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2440 | if mnDPIDs != onosDPIDs: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2441 | switchResults = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2442 | main.log.error( "Switches in MN but not in ONOS:" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2443 | list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2444 | main.log.error( str( list1 ) ) |
| 2445 | main.log.error( "Switches in ONOS but not in MN:" ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2446 | list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2447 | main.log.error( str( list2 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2448 | else: # list of dpid's match in onos and mn |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2449 | switchResults = main.TRUE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2450 | finalResults = switchResults |
Jon Hall | 3d87d50 | 2014-10-17 18:37:42 -0400 | [diff] [blame] | 2451 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2452 | # FIXME: this does not look for extra ports in ONOS, only checks that |
| 2453 | # ONOS has what is in MN |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2454 | portsResults = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2455 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2456 | # PORTS |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2457 | for name, mnSwitch in switches.iteritems(): |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2458 | mnPorts = [] |
| 2459 | onosPorts = [] |
| 2460 | switchResult = main.TRUE |
| 2461 | for port in mnSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2462 | if port[ 'enabled' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2463 | mnPorts.append( int( port[ 'of_port' ] ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2464 | for onosSwitch in portsJson: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2465 | if onosSwitch[ 'device' ][ 'available' ]: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2466 | if onosSwitch[ 'device' ][ 'id' ].replace( |
| 2467 | ':', |
| 2468 | '' ).replace( |
| 2469 | "of", |
| 2470 | '' ) == mnSwitch[ 'dpid' ]: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2471 | for port in onosSwitch[ 'ports' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2472 | if port[ 'isEnabled' ]: |
| 2473 | if port[ 'port' ] == 'local': |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2474 | # onosPorts.append( 'local' ) |
| 2475 | onosPorts.append( long( uint64( -2 ) ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2476 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2477 | onosPorts.append( int( port[ 'port' ] ) ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2478 | break |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2479 | mnPorts.sort( key=float ) |
| 2480 | onosPorts.sort( key=float ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2481 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2482 | mnPortsLog = mnPorts |
| 2483 | onosPortsLog = onosPorts |
| 2484 | mnPorts = [ x for x in mnPorts ] |
| 2485 | onosPorts = [ x for x in onosPorts ] |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2486 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2487 | # TODO: handle other reserved port numbers besides LOCAL |
| 2488 | # NOTE: Reserved ports |
| 2489 | # Local port: -2 in Openflow, ONOS shows 'local', we store as |
| 2490 | # long( uint64( -2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2491 | for mnPort in mnPortsLog: |
| 2492 | if mnPort in onosPorts: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2493 | # don't set results to true here as this is just one of |
| 2494 | # many checks and it might override a failure |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2495 | mnPorts.remove( mnPort ) |
| 2496 | onosPorts.remove( mnPort ) |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2497 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2498 | # NOTE: OVS reports this as down since there is no link |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2499 | # So ignoring these for now |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2500 | # TODO: Come up with a better way of handling these |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2501 | if 65534 in mnPorts: |
| 2502 | mnPorts.remove( 65534 ) |
| 2503 | if long( uint64( -2 ) ) in onosPorts: |
| 2504 | onosPorts.remove( long( uint64( -2 ) ) ) |
| 2505 | if len( mnPorts ): # the ports of this switch don't match |
| 2506 | switchResult = main.FALSE |
| 2507 | main.log.warn( "Ports in MN but not ONOS: " + str( mnPorts ) ) |
| 2508 | if len( onosPorts ): # the ports of this switch don't match |
| 2509 | switchResult = main.FALSE |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2510 | main.log.warn( |
| 2511 | "Ports in ONOS but not MN: " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2512 | str( onosPorts ) ) |
| 2513 | if switchResult == main.FALSE: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2514 | main.log.error( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2515 | "The list of ports for switch %s(%s) does not match:" % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2516 | ( name, mnSwitch[ 'dpid' ] ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2517 | main.log.warn( "mn_ports[] = " + str( mnPortsLog ) ) |
| 2518 | main.log.warn( "onos_ports[] = " + str( onosPortsLog ) ) |
| 2519 | portsResults = portsResults and switchResult |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2520 | finalResults = finalResults and portsResults |
| 2521 | return finalResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2522 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2523 | def compareLinks( self, switches, links, linksJson ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2524 | """ |
| 2525 | Compare mn and onos links |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2526 | linksJson: parsed json object from the onos links api |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2527 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2528 | """ |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2529 | # 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] | 2530 | # ONOS has what is in MN |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2531 | onos = linksJson |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2532 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2533 | mnLinks = [] |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2534 | for l in links: |
| 2535 | try: |
| 2536 | node1 = switches[ l[ 'node1' ] ] |
| 2537 | node2 = switches[ l[ 'node2' ] ] |
| 2538 | enabled = True |
| 2539 | for port in node1[ 'ports' ]: |
| 2540 | if port[ 'of_port' ] == l[ 'port1' ]: |
| 2541 | enabled = enabled and port[ 'enabled' ] |
| 2542 | for port in node2[ 'ports' ]: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2543 | if port[ 'of_port' ] == l[ 'port2' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2544 | enabled = enabled and port[ 'enabled' ] |
| 2545 | if enabled: |
| 2546 | mnLinks.append( l ) |
| 2547 | except KeyError: |
| 2548 | pass |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2549 | if 2 * len( mnLinks ) == len( onos ): |
| 2550 | linkResults = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2551 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2552 | linkResults = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2553 | main.log.error( |
Jon Hall | 328ddca | 2015-01-28 15:57:15 -0800 | [diff] [blame] | 2554 | "Mininet has " + str( len( mnLinks ) ) + |
| 2555 | " bidirectional links and ONOS has " + |
| 2556 | str( len( onos ) ) + " unidirectional links" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2557 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2558 | # iterate through MN links and check if an ONOS link exists in |
| 2559 | # both directions |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2560 | for link in mnLinks: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2561 | # TODO: Find a more efficient search method |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2562 | node1 = None |
| 2563 | port1 = None |
| 2564 | node2 = None |
| 2565 | port2 = None |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2566 | firstDir = main.FALSE |
| 2567 | secondDir = main.FALSE |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2568 | for swName, switch in switches.iteritems(): |
| 2569 | if swName == link[ 'node1' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2570 | node1 = switch[ 'dpid' ] |
| 2571 | for port in switch[ 'ports' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2572 | if str( port[ 'of_port' ] ) == str( link[ 'port1' ] ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2573 | port1 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2574 | if node1 is not None and node2 is not None: |
| 2575 | break |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2576 | if swName == link[ 'node2' ]: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2577 | node2 = switch[ 'dpid' ] |
| 2578 | for port in switch[ 'ports' ]: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2579 | if str( port[ 'of_port' ] ) == str( link[ 'port2' ] ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2580 | port2 = port[ 'of_port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2581 | if node1 is not None and node2 is not None: |
| 2582 | break |
| 2583 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2584 | for onosLink in onos: |
| 2585 | onosNode1 = onosLink[ 'src' ][ 'device' ].replace( |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2586 | ":", '' ).replace( "of", '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2587 | onosNode2 = onosLink[ 'dst' ][ 'device' ].replace( |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2588 | ":", '' ).replace( "of", '' ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2589 | onosPort1 = onosLink[ 'src' ][ 'port' ] |
| 2590 | onosPort2 = onosLink[ 'dst' ][ 'port' ] |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2591 | |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2592 | # check onos link from node1 to node2 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2593 | if str( onosNode1 ) == str( node1 ) and str( |
| 2594 | onosNode2 ) == str( node2 ): |
| 2595 | if int( onosPort1 ) == int( port1 ) and int( |
| 2596 | onosPort2 ) == int( port2 ): |
| 2597 | firstDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2598 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2599 | main.log.warn( |
| 2600 | 'The port numbers do not match for ' + |
| 2601 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2602 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2603 | 'link %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2604 | ( node1, port1, node2, port2 ) + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2605 | ' ONOS has the values %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2606 | ( onosNode1, onosPort1, onosNode2, onosPort2 ) ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2607 | |
| 2608 | # check onos link from node2 to node1 |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2609 | elif ( str( onosNode1 ) == str( node2 ) and |
| 2610 | str( onosNode2 ) == str( node1 ) ): |
| 2611 | if ( int( onosPort1 ) == int( port2 ) |
| 2612 | and int( onosPort2 ) == int( port1 ) ): |
| 2613 | secondDir = main.TRUE |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2614 | else: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2615 | main.log.warn( |
| 2616 | 'The port numbers do not match for ' + |
| 2617 | str( link ) + |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2618 | ' between ONOS and MN. When checking ONOS for ' + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2619 | 'link %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2620 | ( node1, port1, node2, port2 ) + |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2621 | ' ONOS has the values %s/%s -> %s/%s' % |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2622 | ( onosNode2, onosPort2, onosNode1, onosPort1 ) ) |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2623 | else: # this is not the link you're looking for |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2624 | pass |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2625 | if not firstDir: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2626 | main.log.error( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2627 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 2628 | ( node1, port1, node2, port2 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2629 | if not secondDir: |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2630 | main.log.error( |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2631 | 'ONOS does not have the link %s/%s -> %s/%s' % |
| 2632 | ( node2, port2, node1, port1 ) ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2633 | linkResults = linkResults and firstDir and secondDir |
| 2634 | return linkResults |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2635 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2636 | def compareHosts( self, hosts, hostsJson ): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2637 | """ |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2638 | Compare mn and onos Hosts. |
| 2639 | Since Mininet hosts are quiet, ONOS will only know of them when they |
| 2640 | speak. For this reason, we will only check that the hosts in ONOS |
| 2641 | stores are in Mininet, and not vice versa. |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2642 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2643 | Arguments: |
| 2644 | hostsJson: parsed json object from the onos hosts api |
| 2645 | Returns: |
| 2646 | """ |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2647 | import json |
| 2648 | hostResults = main.TRUE |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2649 | for onosHost in hostsJson: |
| 2650 | onosMAC = onosHost[ 'mac' ].lower() |
| 2651 | match = False |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2652 | for mnHost, info in hosts.iteritems(): |
| 2653 | for mnIntf in info[ 'interfaces' ]: |
| 2654 | if onosMAC == mnIntf[ 'mac' ].lower(): |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2655 | match = True |
| 2656 | for ip in mnIntf[ 'ips' ]: |
Jon Hall | feff308 | 2015-05-19 10:23:26 -0700 | [diff] [blame] | 2657 | if ip in onosHost[ 'ipAddresses' ]: |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2658 | pass # all is well |
| 2659 | else: |
| 2660 | # misssing ip |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2661 | main.log.error( "ONOS host " + |
| 2662 | onosHost[ 'id' ] + |
| 2663 | " has a different IP(" + |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2664 | str( onosHost[ 'ipAddresses' ] ) + |
| 2665 | ") than the Mininet host(" + |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2666 | str( ip ) + |
| 2667 | ")." ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2668 | output = json.dumps( |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2669 | onosHost, |
| 2670 | sort_keys=True, |
| 2671 | indent=4, |
| 2672 | separators=( ',', ': ' ) ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2673 | main.log.info( output ) |
| 2674 | hostResults = main.FALSE |
| 2675 | if not match: |
| 2676 | hostResults = main.FALSE |
| 2677 | main.log.error( "ONOS host " + onosHost[ 'id' ] + " has no " + |
| 2678 | "corresponding Mininet host." ) |
| 2679 | output = json.dumps( onosHost, |
| 2680 | sort_keys=True, |
| 2681 | indent=4, |
| 2682 | separators=( ',', ': ' ) ) |
| 2683 | main.log.info( output ) |
Jon Hall | ff6b4b2 | 2015-02-23 09:25:15 -0800 | [diff] [blame] | 2684 | return hostResults |
| 2685 | |
Jon Hall | afa8a47 | 2015-06-12 14:02:42 -0700 | [diff] [blame] | 2686 | def getHostsOld( self ): |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2687 | """ |
| 2688 | Returns a list of all hosts |
| 2689 | Don't ask questions just use it""" |
| 2690 | self.handle.sendline( "" ) |
| 2691 | self.handle.expect( "mininet>" ) |
Jon Hall | 72cf1dc | 2014-10-20 21:04:50 -0400 | [diff] [blame] | 2692 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2693 | self.handle.sendline( "py [ host.name for host in net.hosts ]" ) |
| 2694 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2695 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2696 | handlePy = self.handle.before |
| 2697 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 2698 | handlePy = handlePy.rstrip() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2699 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2700 | self.handle.sendline( "" ) |
| 2701 | self.handle.expect( "mininet>" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 2702 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2703 | hostStr = handlePy.replace( "]", "" ) |
| 2704 | hostStr = hostStr.replace( "'", "" ) |
| 2705 | hostStr = hostStr.replace( "[", "" ) |
kelvin-onlab | 2ccad6e | 2015-05-18 10:36:54 -0700 | [diff] [blame] | 2706 | hostStr = hostStr.replace( " ", "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2707 | hostList = hostStr.split( "," ) |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 2708 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 2709 | return hostList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 2710 | |
kelvin-onlab | fa6ada8 | 2015-06-11 13:06:24 -0700 | [diff] [blame] | 2711 | def getSwitch( self ): |
| 2712 | """ |
| 2713 | Returns a list of all switches |
| 2714 | Again, don't ask question just use it... |
| 2715 | """ |
| 2716 | # get host list... |
| 2717 | hostList = self.getHosts() |
| 2718 | # Make host set |
| 2719 | hostSet = set( hostList ) |
| 2720 | |
| 2721 | # Getting all the nodes in mininet |
| 2722 | self.handle.sendline( "" ) |
| 2723 | self.handle.expect( "mininet>" ) |
| 2724 | |
| 2725 | self.handle.sendline( "py [ node.name for node in net.values() ]" ) |
| 2726 | self.handle.expect( "mininet>" ) |
| 2727 | |
| 2728 | handlePy = self.handle.before |
| 2729 | handlePy = handlePy.split( "]\r\n", 1 )[ 1 ] |
| 2730 | handlePy = handlePy.rstrip() |
| 2731 | |
| 2732 | self.handle.sendline( "" ) |
| 2733 | self.handle.expect( "mininet>" ) |
| 2734 | |
| 2735 | nodesStr = handlePy.replace( "]", "" ) |
| 2736 | nodesStr = nodesStr.replace( "'", "" ) |
| 2737 | nodesStr = nodesStr.replace( "[", "" ) |
| 2738 | nodesStr = nodesStr.replace( " ", "" ) |
| 2739 | nodesList = nodesStr.split( "," ) |
| 2740 | |
| 2741 | nodesSet = set( nodesList ) |
| 2742 | # discarding default controller(s) node |
| 2743 | nodesSet.discard( 'c0' ) |
| 2744 | nodesSet.discard( 'c1' ) |
| 2745 | nodesSet.discard( 'c2' ) |
| 2746 | |
| 2747 | switchSet = nodesSet - hostSet |
| 2748 | switchList = list( switchSet ) |
| 2749 | |
| 2750 | return switchList |
| 2751 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2752 | def update( self ): |
| 2753 | """ |
| 2754 | updates the port address and status information for |
| 2755 | each port in mn""" |
| 2756 | # TODO: Add error checking. currently the mininet command has no output |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 2757 | main.log.info( "Updating MN port information" ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2758 | try: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2759 | self.handle.sendline( "" ) |
| 2760 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2761 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2762 | self.handle.sendline( "update" ) |
| 2763 | self.handle.expect( "update" ) |
| 2764 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2765 | |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2766 | self.handle.sendline( "" ) |
| 2767 | self.handle.expect( "mininet>" ) |
Jon Hall | 3848172 | 2014-11-04 16:50:05 -0500 | [diff] [blame] | 2768 | |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2769 | return main.TRUE |
| 2770 | except pexpect.EOF: |
Jon Hall | 7eb3840 | 2015-01-08 17:19:54 -0800 | [diff] [blame] | 2771 | main.log.error( self.name + ": EOF exception found" ) |
| 2772 | main.log.error( self.name + ": " + self.handle.before ) |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 2773 | main.cleanup() |
| 2774 | main.exit() |
| 2775 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2776 | def assignVLAN( self, host, intf, vlan ): |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2777 | """ |
| 2778 | Add vlan tag to a host. |
| 2779 | Dependencies: |
| 2780 | This class depends on the "vlan" package |
| 2781 | $ sudo apt-get install vlan |
| 2782 | Configuration: |
| 2783 | Load the 8021q module into the kernel |
| 2784 | $sudo modprobe 8021q |
| 2785 | |
| 2786 | To make this setup permanent: |
| 2787 | $ sudo su -c 'echo "8021q" >> /etc/modules' |
| 2788 | """ |
| 2789 | if self.handle: |
| 2790 | try: |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2791 | # get the ip address of the host |
| 2792 | main.log.info( "Get the ip address of the host" ) |
| 2793 | ipaddr = self.getIPAddress( host ) |
| 2794 | print repr( ipaddr ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2795 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2796 | # remove IP from interface intf |
| 2797 | # Ex: h1 ifconfig h1-eth0 inet 0 |
| 2798 | main.log.info( "Remove IP from interface " ) |
| 2799 | cmd2 = host + " ifconfig " + intf + " " + " inet 0 " |
| 2800 | self.handle.sendline( cmd2 ) |
| 2801 | self.handle.expect( "mininet>" ) |
| 2802 | response = self.handle.before |
| 2803 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2804 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2805 | # create VLAN interface |
| 2806 | # Ex: h1 vconfig add h1-eth0 100 |
| 2807 | main.log.info( "Create Vlan" ) |
| 2808 | cmd3 = host + " vconfig add " + intf + " " + vlan |
| 2809 | self.handle.sendline( cmd3 ) |
| 2810 | self.handle.expect( "mininet>" ) |
| 2811 | response = self.handle.before |
| 2812 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2813 | |
Jon Hall | d80cc14 | 2015-07-06 13:36:05 -0700 | [diff] [blame] | 2814 | # assign the host's IP to the VLAN interface |
| 2815 | # Ex: h1 ifconfig h1-eth0.100 inet 10.0.0.1 |
| 2816 | main.log.info( "Assign the host IP to the vlan interface" ) |
| 2817 | vintf = intf + "." + vlan |
| 2818 | cmd4 = host + " ifconfig " + vintf + " " + " inet " + ipaddr |
| 2819 | self.handle.sendline( cmd4 ) |
| 2820 | self.handle.expect( "mininet>" ) |
| 2821 | response = self.handle.before |
| 2822 | main.log.info( "====> %s ", response ) |
kaouthera | 3f13ca2 | 2015-05-05 15:01:41 -0700 | [diff] [blame] | 2823 | |
| 2824 | return main.TRUE |
| 2825 | except pexpect.EOF: |
| 2826 | main.log.error( self.name + ": EOF exception found" ) |
| 2827 | main.log.error( self.name + ": " + self.handle.before ) |
| 2828 | return main.FALSE |
| 2829 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 2830 | def createHostComponent( self, name ): |
| 2831 | """ |
| 2832 | Creates a new mininet cli component with the same parameters as self. |
| 2833 | This new component is intended to be used to login to the hosts created |
| 2834 | by mininet. |
| 2835 | |
| 2836 | Arguments: |
| 2837 | name - The string of the name of this component. The new component |
| 2838 | will be assigned to main.<name> . |
| 2839 | In addition, main.<name>.name = str( name ) |
| 2840 | """ |
| 2841 | try: |
| 2842 | # look to see if this component already exists |
| 2843 | getattr( main, name ) |
| 2844 | except AttributeError: |
| 2845 | # namespace is clear, creating component |
| 2846 | main.componentDictionary[name] = main.componentDictionary[self.name].copy() |
| 2847 | main.componentDictionary[name]['connect_order'] = str( int( main.componentDictionary[name]['connect_order'] ) + 1 ) |
| 2848 | main.componentInit( name ) |
| 2849 | except Exception: |
| 2850 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2851 | main.cleanup() |
| 2852 | main.exit() |
| 2853 | else: |
| 2854 | # namespace is not clear! |
| 2855 | main.log.error( name + " component already exists!" ) |
| 2856 | # FIXME: Should we exit here? |
| 2857 | main.cleanup() |
| 2858 | main.exit() |
| 2859 | |
| 2860 | def removeHostComponent( self, name ): |
| 2861 | """ |
| 2862 | Remove host component |
| 2863 | Arguments: |
| 2864 | name - The string of the name of the component to delete. |
| 2865 | """ |
| 2866 | try: |
| 2867 | # Get host component |
| 2868 | component = getattr( main, name ) |
| 2869 | except AttributeError: |
| 2870 | main.log.error( "Component " + name + " does not exist." ) |
| 2871 | return |
| 2872 | try: |
| 2873 | # Disconnect from component |
| 2874 | component.disconnect() |
| 2875 | # Delete component |
| 2876 | delattr( main, name ) |
| 2877 | # Delete component from ComponentDictionary |
| 2878 | del( main.componentDictionary[name] ) |
| 2879 | except Exception: |
| 2880 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2881 | main.cleanup() |
| 2882 | main.exit() |
| 2883 | |
| 2884 | def startHostCli( self, host=None ): |
| 2885 | """ |
| 2886 | Use the mininet m utility to connect to the host's cli |
| 2887 | """ |
| 2888 | # These are fields that can be used by scapy packets. Initialized to None |
| 2889 | self.hostIp = None |
| 2890 | self.hostMac = None |
| 2891 | try: |
| 2892 | if not host: |
| 2893 | host = self.name |
| 2894 | self.handle.sendline( self.home + "/util/m " + host ) |
| 2895 | self.handle.expect( self.hostPrompt ) |
| 2896 | return main.TRUE |
| 2897 | except pexpect.TIMEOUT: |
| 2898 | main.log.exception( self.name + ": Command timed out" ) |
| 2899 | return main.FALSE |
| 2900 | except pexpect.EOF: |
| 2901 | main.log.exception( self.name + ": connection closed." ) |
| 2902 | main.cleanup() |
| 2903 | main.exit() |
| 2904 | except Exception: |
| 2905 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2906 | main.cleanup() |
| 2907 | main.exit() |
| 2908 | |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2909 | def startScapy( self, mplsPath="" ): |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 2910 | """ |
| 2911 | Start the Scapy cli |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2912 | optional: |
| 2913 | mplsPath - The path where the MPLS class is located |
| 2914 | 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] | 2915 | """ |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2916 | mplsLines = ['import imp', |
| 2917 | 'imp.load_source( "mplsClass", "{}mplsClass.py" )'.format(mplsPath), |
| 2918 | 'from mplsClass import MPLS', |
| 2919 | 'bind_layers(Ether, MPLS, type = 0x8847)', |
| 2920 | 'bind_layers(MPLS, MPLS, bottom_of_label_stack = 0)', |
| 2921 | 'bind_layers(MPLS, IP)'] |
| 2922 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 2923 | try: |
| 2924 | self.handle.sendline( "scapy" ) |
| 2925 | self.handle.expect( self.scapyPrompt ) |
| 2926 | self.handle.sendline( "conf.color_theme = NoTheme()" ) |
| 2927 | self.handle.expect( self.scapyPrompt ) |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 2928 | if mplsPath: |
| 2929 | main.log.info( "Adding MPLS class" ) |
| 2930 | main.log.info( "MPLS class path: " + mplsPath ) |
| 2931 | for line in mplsLines: |
| 2932 | main.log.info( "sending line: " + line ) |
| 2933 | self.handle.sendline( line ) |
| 2934 | self.handle.expect( self.scapyPrompt ) |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 2935 | return main.TRUE |
| 2936 | except pexpect.TIMEOUT: |
| 2937 | main.log.exception( self.name + ": Command timed out" ) |
| 2938 | return main.FALSE |
| 2939 | except pexpect.EOF: |
| 2940 | main.log.exception( self.name + ": connection closed." ) |
| 2941 | main.cleanup() |
| 2942 | main.exit() |
| 2943 | except Exception: |
| 2944 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2945 | main.cleanup() |
| 2946 | main.exit() |
| 2947 | |
| 2948 | def stopScapy( self ): |
| 2949 | """ |
| 2950 | Exit the Scapy cli |
| 2951 | """ |
| 2952 | try: |
| 2953 | self.handle.sendline( "exit()" ) |
| 2954 | self.handle.expect( self.hostPrompt ) |
| 2955 | return main.TRUE |
| 2956 | except pexpect.TIMEOUT: |
| 2957 | main.log.exception( self.name + ": Command timed out" ) |
| 2958 | return main.FALSE |
| 2959 | except pexpect.EOF: |
| 2960 | main.log.exception( self.name + ": connection closed." ) |
| 2961 | main.cleanup() |
| 2962 | main.exit() |
| 2963 | except Exception: |
| 2964 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 2965 | main.cleanup() |
| 2966 | main.exit() |
| 2967 | |
| 2968 | def buildEther( self, **kwargs ): |
| 2969 | """ |
| 2970 | Build an Ethernet frame |
| 2971 | |
| 2972 | Will create a frame class with the given options. If a field is |
| 2973 | left blank it will default to the below value unless it is |
| 2974 | overwritten by the next frame. |
| 2975 | Default frame: |
| 2976 | ###[ Ethernet ]### |
| 2977 | dst= ff:ff:ff:ff:ff:ff |
| 2978 | src= 00:00:00:00:00:00 |
| 2979 | type= 0x800 |
| 2980 | |
| 2981 | Returns main.TRUE or main.FALSE on error |
| 2982 | """ |
| 2983 | try: |
| 2984 | # Set the Ethernet frame |
| 2985 | cmd = 'ether = Ether( ' |
| 2986 | options = [] |
| 2987 | for key, value in kwargs.iteritems(): |
| 2988 | if isinstance( value, str ): |
| 2989 | value = '"' + value + '"' |
| 2990 | options.append( str( key ) + "=" + str( value ) ) |
| 2991 | cmd += ", ".join( options ) |
| 2992 | cmd += ' )' |
| 2993 | self.handle.sendline( cmd ) |
| 2994 | self.handle.expect( self.scapyPrompt ) |
| 2995 | if "Traceback" in self.handle.before: |
| 2996 | # KeyError, SyntaxError, ... |
| 2997 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 2998 | return main.FALSE |
| 2999 | self.handle.sendline( "packet = ether" ) |
| 3000 | self.handle.expect( self.scapyPrompt ) |
| 3001 | if "Traceback" in self.handle.before: |
| 3002 | # KeyError, SyntaxError, ... |
| 3003 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3004 | return main.FALSE |
| 3005 | return main.TRUE |
| 3006 | except pexpect.TIMEOUT: |
| 3007 | main.log.exception( self.name + ": Command timed out" ) |
| 3008 | return main.FALSE |
| 3009 | except pexpect.EOF: |
| 3010 | main.log.exception( self.name + ": connection closed." ) |
| 3011 | main.cleanup() |
| 3012 | main.exit() |
| 3013 | except Exception: |
| 3014 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3015 | main.cleanup() |
| 3016 | main.exit() |
| 3017 | |
| 3018 | def buildIP( self, **kwargs ): |
| 3019 | """ |
| 3020 | Build an IP frame |
| 3021 | |
| 3022 | Will create a frame class with the given options. If a field is |
| 3023 | left blank it will default to the below value unless it is |
| 3024 | overwritten by the next frame. |
| 3025 | Default frame: |
| 3026 | ###[ IP ]### |
| 3027 | version= 4 |
| 3028 | ihl= None |
| 3029 | tos= 0x0 |
| 3030 | len= None |
| 3031 | id= 1 |
| 3032 | flags= |
| 3033 | frag= 0 |
| 3034 | ttl= 64 |
| 3035 | proto= hopopt |
| 3036 | chksum= None |
| 3037 | src= 127.0.0.1 |
| 3038 | dst= 127.0.0.1 |
| 3039 | \options\ |
| 3040 | |
| 3041 | Returns main.TRUE or main.FALSE on error |
| 3042 | """ |
| 3043 | try: |
| 3044 | # Set the IP frame |
| 3045 | cmd = 'ip = IP( ' |
| 3046 | options = [] |
| 3047 | for key, value in kwargs.iteritems(): |
| 3048 | if isinstance( value, str ): |
| 3049 | value = '"' + value + '"' |
| 3050 | options.append( str( key ) + "=" + str( value ) ) |
| 3051 | cmd += ", ".join( options ) |
| 3052 | cmd += ' )' |
| 3053 | self.handle.sendline( cmd ) |
| 3054 | self.handle.expect( self.scapyPrompt ) |
| 3055 | if "Traceback" in self.handle.before: |
| 3056 | # KeyError, SyntaxError, ... |
| 3057 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3058 | return main.FALSE |
| 3059 | self.handle.sendline( "packet = ether/ip" ) |
| 3060 | self.handle.expect( self.scapyPrompt ) |
| 3061 | if "Traceback" in self.handle.before: |
| 3062 | # KeyError, SyntaxError, ... |
| 3063 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3064 | return main.FALSE |
| 3065 | return main.TRUE |
| 3066 | except pexpect.TIMEOUT: |
| 3067 | main.log.exception( self.name + ": Command timed out" ) |
| 3068 | return main.FALSE |
| 3069 | except pexpect.EOF: |
| 3070 | main.log.exception( self.name + ": connection closed." ) |
| 3071 | main.cleanup() |
| 3072 | main.exit() |
| 3073 | except Exception: |
| 3074 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3075 | main.cleanup() |
| 3076 | main.exit() |
| 3077 | |
| 3078 | def buildIPv6( self, **kwargs ): |
| 3079 | """ |
| 3080 | Build an IPv6 frame |
| 3081 | |
| 3082 | Will create a frame class with the given options. If a field is |
| 3083 | left blank it will default to the below value unless it is |
| 3084 | overwritten by the next frame. |
| 3085 | Default frame: |
| 3086 | ###[ IPv6 ]### |
| 3087 | version= 6 |
| 3088 | tc= 0 |
| 3089 | fl= 0 |
| 3090 | plen= None |
| 3091 | nh= No Next Header |
| 3092 | hlim= 64 |
| 3093 | src= ::1 |
| 3094 | dst= ::1 |
| 3095 | |
| 3096 | Returns main.TRUE or main.FALSE on error |
| 3097 | """ |
| 3098 | try: |
| 3099 | # Set the IPv6 frame |
| 3100 | cmd = 'ipv6 = IPv6( ' |
| 3101 | options = [] |
| 3102 | for key, value in kwargs.iteritems(): |
| 3103 | if isinstance( value, str ): |
| 3104 | value = '"' + value + '"' |
| 3105 | options.append( str( key ) + "=" + str( value ) ) |
| 3106 | cmd += ", ".join( options ) |
| 3107 | cmd += ' )' |
| 3108 | self.handle.sendline( cmd ) |
| 3109 | self.handle.expect( self.scapyPrompt ) |
| 3110 | if "Traceback" in self.handle.before: |
| 3111 | # KeyError, SyntaxError, ... |
| 3112 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3113 | return main.FALSE |
| 3114 | self.handle.sendline( "packet = ether/ipv6" ) |
| 3115 | self.handle.expect( self.scapyPrompt ) |
| 3116 | if "Traceback" in self.handle.before: |
| 3117 | # KeyError, SyntaxError, ... |
| 3118 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3119 | return main.FALSE |
| 3120 | return main.TRUE |
| 3121 | except pexpect.TIMEOUT: |
| 3122 | main.log.exception( self.name + ": Command timed out" ) |
| 3123 | return main.FALSE |
| 3124 | except pexpect.EOF: |
| 3125 | main.log.exception( self.name + ": connection closed." ) |
| 3126 | main.cleanup() |
| 3127 | main.exit() |
| 3128 | except Exception: |
| 3129 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3130 | main.cleanup() |
| 3131 | main.exit() |
| 3132 | |
| 3133 | def buildTCP( self, ipVersion=4, **kwargs ): |
| 3134 | """ |
| 3135 | Build an TCP frame |
| 3136 | |
| 3137 | Will create a frame class with the given options. If a field is |
| 3138 | left blank it will default to the below value unless it is |
| 3139 | overwritten by the next frame. |
| 3140 | |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 3141 | NOTE: Some arguments require quotes around them. It's up to you to |
| 3142 | know which ones and to add them yourself. Arguments with an asterisk |
| 3143 | do not need quotes. |
| 3144 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3145 | Options: |
| 3146 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 3147 | frame to use to encapsulate into |
| 3148 | Default frame: |
| 3149 | ###[ TCP ]### |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 3150 | sport= ftp_data * |
| 3151 | dport= http * |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3152 | seq= 0 |
| 3153 | ack= 0 |
| 3154 | dataofs= None |
| 3155 | reserved= 0 |
| 3156 | flags= S |
| 3157 | window= 8192 |
| 3158 | chksum= None |
| 3159 | urgptr= 0 |
| 3160 | options= {} |
| 3161 | |
| 3162 | Returns main.TRUE or main.FALSE on error |
| 3163 | """ |
| 3164 | try: |
| 3165 | # Set the TCP frame |
| 3166 | cmd = 'tcp = TCP( ' |
| 3167 | options = [] |
| 3168 | for key, value in kwargs.iteritems(): |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3169 | options.append( str( key ) + "=" + str( value ) ) |
| 3170 | cmd += ", ".join( options ) |
| 3171 | cmd += ' )' |
| 3172 | self.handle.sendline( cmd ) |
| 3173 | self.handle.expect( self.scapyPrompt ) |
| 3174 | if "Traceback" in self.handle.before: |
| 3175 | # KeyError, SyntaxError, ... |
| 3176 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3177 | return main.FALSE |
| 3178 | if str( ipVersion ) is '4': |
| 3179 | self.handle.sendline( "packet = ether/ip/tcp" ) |
| 3180 | elif str( ipVersion ) is '6': |
| 3181 | self.handle.sendline( "packet = ether/ipv6/tcp" ) |
| 3182 | else: |
| 3183 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 3184 | repr( ipVersion ) ) |
| 3185 | return main.FALSE |
| 3186 | self.handle.expect( self.scapyPrompt ) |
| 3187 | if "Traceback" in self.handle.before: |
| 3188 | # KeyError, SyntaxError, ... |
| 3189 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3190 | return main.FALSE |
| 3191 | return main.TRUE |
| 3192 | except pexpect.TIMEOUT: |
| 3193 | main.log.exception( self.name + ": Command timed out" ) |
| 3194 | return main.FALSE |
| 3195 | except pexpect.EOF: |
| 3196 | main.log.exception( self.name + ": connection closed." ) |
| 3197 | main.cleanup() |
| 3198 | main.exit() |
| 3199 | except Exception: |
| 3200 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3201 | main.cleanup() |
| 3202 | main.exit() |
| 3203 | |
| 3204 | def buildUDP( self, ipVersion=4, **kwargs ): |
| 3205 | """ |
| 3206 | Build an UDP frame |
| 3207 | |
| 3208 | Will create a frame class with the given options. If a field is |
| 3209 | left blank it will default to the below value unless it is |
| 3210 | overwritten by the next frame. |
| 3211 | |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 3212 | NOTE: Some arguments require quotes around them. It's up to you to |
| 3213 | know which ones and to add them yourself. Arguments with an asterisk |
| 3214 | do not need quotes. |
| 3215 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3216 | Options: |
| 3217 | ipVersion - Either 4 (default) or 6, indicates what Internet Protocol |
| 3218 | frame to use to encapsulate into |
| 3219 | Default frame: |
| 3220 | ###[ UDP ]### |
GlennRC | 956ea74 | 2015-11-05 16:14:15 -0800 | [diff] [blame] | 3221 | sport= domain * |
| 3222 | dport= domain * |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3223 | len= None |
| 3224 | chksum= None |
| 3225 | |
| 3226 | Returns main.TRUE or main.FALSE on error |
| 3227 | """ |
| 3228 | try: |
| 3229 | # Set the UDP frame |
| 3230 | cmd = 'udp = UDP( ' |
| 3231 | options = [] |
| 3232 | for key, value in kwargs.iteritems(): |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3233 | options.append( str( key ) + "=" + str( value ) ) |
| 3234 | cmd += ", ".join( options ) |
| 3235 | cmd += ' )' |
| 3236 | self.handle.sendline( cmd ) |
| 3237 | self.handle.expect( self.scapyPrompt ) |
| 3238 | if "Traceback" in self.handle.before: |
| 3239 | # KeyError, SyntaxError, ... |
| 3240 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3241 | return main.FALSE |
| 3242 | if str( ipVersion ) is '4': |
| 3243 | self.handle.sendline( "packet = ether/ip/udp" ) |
| 3244 | elif str( ipVersion ) is '6': |
| 3245 | self.handle.sendline( "packet = ether/ipv6/udp" ) |
| 3246 | else: |
| 3247 | main.log.error( "Unrecognized option for ipVersion, given " + |
| 3248 | repr( ipVersion ) ) |
| 3249 | return main.FALSE |
| 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 | return main.TRUE |
| 3256 | except pexpect.TIMEOUT: |
| 3257 | main.log.exception( self.name + ": Command timed out" ) |
| 3258 | return main.FALSE |
| 3259 | except pexpect.EOF: |
| 3260 | main.log.exception( self.name + ": connection closed." ) |
| 3261 | main.cleanup() |
| 3262 | main.exit() |
| 3263 | except Exception: |
| 3264 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3265 | main.cleanup() |
| 3266 | main.exit() |
| 3267 | |
| 3268 | def buildICMP( self, **kwargs ): |
| 3269 | """ |
| 3270 | Build an ICMP frame |
| 3271 | |
| 3272 | Will create a frame class with the given options. If a field is |
| 3273 | left blank it will default to the below value unless it is |
| 3274 | overwritten by the next frame. |
| 3275 | Default frame: |
| 3276 | ###[ ICMP ]### |
| 3277 | type= echo-request |
| 3278 | code= 0 |
| 3279 | chksum= None |
| 3280 | id= 0x0 |
| 3281 | seq= 0x0 |
| 3282 | |
| 3283 | Returns main.TRUE or main.FALSE on error |
| 3284 | """ |
| 3285 | try: |
| 3286 | # Set the ICMP frame |
| 3287 | cmd = 'icmp = ICMP( ' |
| 3288 | options = [] |
| 3289 | for key, value in kwargs.iteritems(): |
| 3290 | if isinstance( value, str ): |
| 3291 | value = '"' + value + '"' |
| 3292 | options.append( str( key ) + "=" + str( value ) ) |
| 3293 | cmd += ", ".join( options ) |
| 3294 | cmd += ' )' |
| 3295 | self.handle.sendline( cmd ) |
| 3296 | self.handle.expect( self.scapyPrompt ) |
| 3297 | if "Traceback" in self.handle.before: |
| 3298 | # KeyError, SyntaxError, ... |
| 3299 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3300 | return main.FALSE |
| 3301 | self.handle.sendline( "packet = ether/ip/icmp" ) |
| 3302 | self.handle.expect( self.scapyPrompt ) |
| 3303 | if "Traceback" in self.handle.before: |
| 3304 | # KeyError, SyntaxError, ... |
| 3305 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3306 | return main.FALSE |
| 3307 | return main.TRUE |
| 3308 | except pexpect.TIMEOUT: |
| 3309 | main.log.exception( self.name + ": Command timed out" ) |
| 3310 | return main.FALSE |
| 3311 | except pexpect.EOF: |
| 3312 | main.log.exception( self.name + ": connection closed." ) |
| 3313 | main.cleanup() |
| 3314 | main.exit() |
| 3315 | except Exception: |
| 3316 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3317 | main.cleanup() |
| 3318 | main.exit() |
| 3319 | |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 3320 | def sendPacket( self, iface=None, packet=None, timeout=1 ): |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3321 | """ |
| 3322 | Send a packet with either the given scapy packet command, or use the |
| 3323 | packet saved in the variable 'packet'. |
| 3324 | |
| 3325 | Examples of a valid string for packet: |
| 3326 | |
| 3327 | Simple IP packet |
| 3328 | packet='Ether(dst="a6:d9:26:df:1d:4b")/IP(dst="10.0.0.2")' |
| 3329 | |
| 3330 | A Ping with two vlan tags |
| 3331 | packet='Ether(dst='ff:ff:ff:ff:ff:ff')/Dot1Q(vlan=1)/Dot1Q(vlan=10)/ |
| 3332 | IP(dst='255.255.255.255', src='192.168.0.1')/ICMP()' |
| 3333 | |
| 3334 | Returns main.TRUE or main.FALSE on error |
| 3335 | """ |
| 3336 | try: |
| 3337 | # TODO: add all params, or use kwargs |
| 3338 | sendCmd = 'srp( ' |
| 3339 | if packet: |
| 3340 | sendCmd += packet |
| 3341 | else: |
| 3342 | sendCmd += "packet" |
GlennRC | 073e8bc | 2015-10-27 17:11:28 -0700 | [diff] [blame] | 3343 | if iface: |
| 3344 | sendCmd += ", iface='{}'".format( iface ) |
| 3345 | |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 3346 | sendCmd += ', timeout=' + str( timeout ) + ')' |
| 3347 | self.handle.sendline( sendCmd ) |
| 3348 | self.handle.expect( self.scapyPrompt ) |
| 3349 | if "Traceback" in self.handle.before: |
| 3350 | # KeyError, SyntaxError, ... |
| 3351 | main.log.error( "Error in sending command: " + self.handle.before ) |
| 3352 | return main.FALSE |
| 3353 | # TODO: Check # of packets sent? |
| 3354 | return main.TRUE |
| 3355 | except pexpect.TIMEOUT: |
| 3356 | main.log.exception( self.name + ": Command timed out" ) |
| 3357 | return main.FALSE |
| 3358 | except pexpect.EOF: |
| 3359 | main.log.exception( self.name + ": connection closed." ) |
| 3360 | main.cleanup() |
| 3361 | main.exit() |
| 3362 | except Exception: |
| 3363 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3364 | main.cleanup() |
| 3365 | main.exit() |
| 3366 | |
| 3367 | def startFilter( self, ifaceName=None, sniffCount=1, pktFilter="ip" ): |
| 3368 | """ |
| 3369 | Listen for packets using the given filters |
| 3370 | |
| 3371 | Options: |
| 3372 | ifaceName - the name of the interface to listen on. If none is given, |
| 3373 | defaults to <host name>-eth0 |
| 3374 | pktFilter - A string in Berkeley Packet Filter (BPF) format which |
| 3375 | specifies which packets to sniff |
| 3376 | sniffCount - The number of matching packets to capture before returning |
| 3377 | |
| 3378 | Returns main.TRUE or main.FALSE on error |
| 3379 | """ |
| 3380 | try: |
| 3381 | # TODO: add all params, or use kwargs |
| 3382 | ifaceName = str( ifaceName ) if ifaceName else self.name + "-eth0" |
| 3383 | # Set interface |
| 3384 | self.handle.sendline( ' conf.iface = "' + ifaceName + '"' ) |
| 3385 | self.handle.expect( self.scapyPrompt ) |
| 3386 | cmd = 'pkt = sniff(count = ' + str( sniffCount ) +\ |
| 3387 | ', filter = "' + str( pktFilter ) + '")' |
| 3388 | self.handle.sendline( cmd ) |
| 3389 | self.handle.expect( '"\)\r\n' ) |
| 3390 | # TODO: parse this? |
| 3391 | return main.TRUE |
| 3392 | except pexpect.TIMEOUT: |
| 3393 | main.log.exception( self.name + ": Command timed out" ) |
| 3394 | return main.FALSE |
| 3395 | except pexpect.EOF: |
| 3396 | main.log.exception( self.name + ": connection closed." ) |
| 3397 | main.cleanup() |
| 3398 | main.exit() |
| 3399 | except Exception: |
| 3400 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3401 | main.cleanup() |
| 3402 | main.exit() |
| 3403 | |
| 3404 | def checkFilter( self ): |
| 3405 | """ |
| 3406 | Check that a filter returned and returns the reponse |
| 3407 | """ |
| 3408 | try: |
| 3409 | i = self.handle.expect( [ self.scapyPrompt, pexpect.TIMEOUT ] ) |
| 3410 | if i == 0: |
| 3411 | return main.TRUE |
| 3412 | else: |
| 3413 | return main.FALSE |
| 3414 | except pexpect.EOF: |
| 3415 | main.log.exception( self.name + ": connection closed." ) |
| 3416 | main.cleanup() |
| 3417 | main.exit() |
| 3418 | except Exception: |
| 3419 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3420 | main.cleanup() |
| 3421 | main.exit() |
| 3422 | |
| 3423 | def killFilter( self ): |
| 3424 | """ |
| 3425 | Kill a scapy filter |
| 3426 | """ |
| 3427 | try: |
| 3428 | self.handle.send( "\x03" ) # Send a ctrl-c to kill the filter |
| 3429 | self.handle.expect( self.scapyPrompt ) |
| 3430 | return self.handle.before |
| 3431 | except pexpect.TIMEOUT: |
| 3432 | main.log.exception( self.name + ": Command timed out" ) |
| 3433 | return None |
| 3434 | except pexpect.EOF: |
| 3435 | main.log.exception( self.name + ": connection closed." ) |
| 3436 | main.cleanup() |
| 3437 | main.exit() |
| 3438 | except Exception: |
| 3439 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3440 | main.cleanup() |
| 3441 | main.exit() |
| 3442 | |
| 3443 | def readPackets( self ): |
| 3444 | """ |
| 3445 | Read all the packets captured by the previous filter |
| 3446 | """ |
| 3447 | try: |
| 3448 | self.handle.sendline( "for p in pkt: p \n") |
| 3449 | self.handle.expect( "for p in pkt: p \r\n... \r\n" ) |
| 3450 | self.handle.expect( self.scapyPrompt ) |
| 3451 | except pexpect.TIMEOUT: |
| 3452 | main.log.exception( self.name + ": Command timed out" ) |
| 3453 | return None |
| 3454 | except pexpect.EOF: |
| 3455 | main.log.exception( self.name + ": connection closed." ) |
| 3456 | main.cleanup() |
| 3457 | main.exit() |
| 3458 | except Exception: |
| 3459 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3460 | main.cleanup() |
| 3461 | main.exit() |
| 3462 | return self.handle.before |
| 3463 | |
| 3464 | def updateSelf( self ): |
| 3465 | """ |
| 3466 | Updates local MAC and IP fields |
| 3467 | """ |
| 3468 | self.hostMac = self.getMac() |
| 3469 | self.hostIp = self.getIp() |
| 3470 | |
| 3471 | def getMac( self, ifaceName=None ): |
| 3472 | """ |
| 3473 | Save host's MAC address |
| 3474 | """ |
| 3475 | try: |
| 3476 | ifaceName = str( ifaceName ) if ifaceName else self.name + "-eth0" |
| 3477 | cmd = 'get_if_hwaddr("' + str( ifaceName ) + '")' |
| 3478 | self.handle.sendline( cmd ) |
| 3479 | self.handle.expect( self.scapyPrompt ) |
| 3480 | pattern = r'(([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))' |
| 3481 | match = re.search( pattern, self.handle.before ) |
| 3482 | if match: |
| 3483 | return match.group() |
| 3484 | else: |
| 3485 | # the command will have an exception if iface doesn't exist |
| 3486 | return None |
| 3487 | except pexpect.TIMEOUT: |
| 3488 | main.log.exception( self.name + ": Command timed out" ) |
| 3489 | return None |
| 3490 | except pexpect.EOF: |
| 3491 | main.log.exception( self.name + ": connection closed." ) |
| 3492 | main.cleanup() |
| 3493 | main.exit() |
| 3494 | except Exception: |
| 3495 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3496 | main.cleanup() |
| 3497 | main.exit() |
| 3498 | |
| 3499 | def getIp( self, ifaceName=None ): |
| 3500 | """ |
| 3501 | Save host's IP address |
| 3502 | """ |
| 3503 | try: |
| 3504 | ifaceName = ifaceName if ifaceName else self.name + "-eth0" |
| 3505 | cmd = 'get_if_addr("' + str( ifaceName ) + '")' |
| 3506 | self.handle.sendline( cmd ) |
| 3507 | self.handle.expect( self.scapyPrompt ) |
| 3508 | |
| 3509 | pattern = r'(((2[0-5]|1[0-9]|[0-9])?[0-9]\.){3}((2[0-5]|1[0-9]|[0-9])?[0-9]))' |
| 3510 | match = re.search( pattern, self.handle.before ) |
| 3511 | if match: |
| 3512 | # NOTE: The command will return 0.0.0.0 if the iface doesn't exist |
| 3513 | return match.group() |
| 3514 | else: |
| 3515 | return None |
| 3516 | except pexpect.TIMEOUT: |
| 3517 | main.log.exception( self.name + ": Command timed out" ) |
| 3518 | return None |
| 3519 | except pexpect.EOF: |
| 3520 | main.log.exception( self.name + ": connection closed." ) |
| 3521 | main.cleanup() |
| 3522 | main.exit() |
| 3523 | except Exception: |
| 3524 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 3525 | main.cleanup() |
| 3526 | main.exit() |
| 3527 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3528 | if __name__ != "__main__": |
kelvin-onlab | 5090714 | 2015-04-01 13:37:45 -0700 | [diff] [blame] | 3529 | sys.modules[ __name__ ] = MininetCliDriver() |