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