admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Oct-2012 |
| 4 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 5 | author:: Anil Kumar ( anilkumar.s@paxterrasolutions.com ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 6 | |
| 7 | |
| 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 |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 11 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 12 | |
| 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. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | MininetCliDriver is the basic driver which will handle the Mininet functions |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 23 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import re |
| 26 | import sys |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 27 | import os |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 28 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 29 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 30 | |
| 31 | class RemoteMininetDriver( Emulator ): |
| 32 | |
| 33 | """ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 34 | RemoteMininetCliDriver is the basic driver which will handle the Mininet |
| 35 | functions. The main different between this and the MininetCliDriver is that |
| 36 | this one does not build the mininet. It assumes that there is already a |
| 37 | mininet running on the target. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 38 | """ |
| 39 | def __init__( self ): |
| 40 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 41 | self.handle = self |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 42 | self.name = None |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 43 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 44 | self.flag = 0 |
| 45 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 46 | def connect( self, **connectargs ): |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 47 | """,user_name, ip_address, pwd,options ): |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 48 | Here the main is the TestON instance after creating all the log |
| 49 | handles.""" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | for key in connectargs: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 51 | vars( self )[ key ] = connectargs[ key ] |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 52 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 53 | self.name = self.options[ 'name' ] |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 54 | |
| 55 | try: |
| 56 | if os.getenv( str( self.ip_address ) ) != None: |
| 57 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 58 | else: |
| 59 | main.log.info( self.name + |
| 60 | ": Trying to connect to " + |
| 61 | self.ip_address ) |
| 62 | |
| 63 | except KeyError: |
| 64 | main.log.info( "Invalid host name," + |
| 65 | " connecting to local host instead" ) |
| 66 | self.ip_address = 'localhost' |
| 67 | except Exception as inst: |
| 68 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 69 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 70 | self.handle = super( |
| 71 | RemoteMininetDriver, |
| 72 | self ).connect( |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 73 | user_name=self.user_name, |
| 74 | ip_address=self.ip_address, |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 75 | port=None, |
| 76 | pwd=self.pwd ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 77 | |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 78 | # Copying the readme file to process the |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 79 | if self.handle: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 80 | return main.TRUE |
| 81 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 82 | else: |
| 83 | main.log.error( |
| 84 | "Connection failed to the host " + |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 85 | self.user_name + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 86 | "@" + |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 87 | self.ip_address ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 88 | main.log.error( "Failed to connect to the Mininet" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 89 | return main.FALSE |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 90 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 91 | def checkForLoss( self, pingList ): |
| 92 | """ |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 93 | Returns main.FALSE for 0% packet loss and |
| 94 | Returns main.ERROR if "found multiple mininet" is found and |
| 95 | Returns main.TRUE else |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 96 | """ |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 97 | self.handle.sendline( "" ) |
| 98 | self.handle.expect( "\$" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 99 | self.handle.sendline( "" ) |
| 100 | self.handle.expect( "\$" ) |
| 101 | self.handle.sendline( "cat " + pingList ) |
| 102 | self.handle.expect( pingList ) |
| 103 | self.handle.expect( "\$" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 104 | outputs = self.handle.before + self.handle.after |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 105 | if re.search( " 0% packet loss", outputs ): |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 106 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 107 | elif re.search( "found multiple mininet", outputs ): |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 108 | return main.ERROR |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 109 | else: |
Jon Hall | 80daded | 2015-05-27 16:07:00 -0700 | [diff] [blame] | 110 | # TODO: Parse for failed pings, give some truncated output |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 111 | main.log.error( "Error, unexpected output in the ping file" ) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 112 | main.log.warn( outputs ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 113 | return main.TRUE |
| 114 | |
shahshreya | 3140b1a | 2015-04-28 14:22:15 -0700 | [diff] [blame] | 115 | def arping( self, host="", ip="10.128.20.211" ): |
| 116 | """ |
| 117 | Description: |
| 118 | Sends arp message from mininet host for hosts discovery |
| 119 | Required: |
| 120 | host - hosts name |
| 121 | Optional: |
| 122 | ip - ip address that does not exist in the network so there would |
| 123 | be no reply. |
| 124 | """ |
| 125 | cmd = " py " + host + ".cmd(\"arping -c 1 " + ip + "\")" |
| 126 | try: |
| 127 | main.log.warn( "Sending: " + cmd ) |
| 128 | self.handle.sendline( cmd ) |
| 129 | response = self.handle.before |
| 130 | self.handle.sendline( "" ) |
| 131 | self.handle.expect( "mininet>" ) |
| 132 | return main.TRUE |
| 133 | |
| 134 | except pexpect.EOF: |
| 135 | main.log.error( self.name + ": EOF exception found" ) |
| 136 | main.log.error( self.name + ": " + self.handle.before ) |
| 137 | main.cleanup() |
| 138 | main.exit() |
| 139 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 140 | def pingLong( self, **pingParams ): |
| 141 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 142 | Starts a continuous ping on the mininet host outputting |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 143 | to a file in the /tmp dir. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 144 | """ |
| 145 | self.handle.sendline( "" ) |
| 146 | self.handle.expect( "\$" ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 147 | args = utilities.parse_args( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 148 | [ "SRC", "TARGET", "PINGTIME" ], **pingParams ) |
| 149 | precmd = "sudo rm /tmp/ping." + args[ "SRC" ] |
| 150 | self.execute( cmd=precmd, prompt="(.*)", timeout=10 ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 151 | command = "sudo mininet/util/m " + args[ "SRC" ] + " ping " +\ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 152 | args[ "TARGET" ] + " -i .2 -w " +\ |
| 153 | str( args[ 'PINGTIME' ] ) + " -D > /tmp/ping." +\ |
| 154 | args[ "SRC" ] + " &" |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 155 | main.log.info( command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 156 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 157 | self.handle.sendline( "" ) |
| 158 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 159 | return main.TRUE |
| 160 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 161 | def pingstatus( self, **pingParams ): |
| 162 | """ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 163 | Tails the respective ping output file and check that |
| 164 | there is a moving "64 bytes" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 165 | """ |
| 166 | self.handle.sendline( "" ) |
| 167 | self.handle.expect( "\$" ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 168 | args = utilities.parse_args( [ "SRC" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 169 | self.handle.sendline( "tail /tmp/ping." + args[ "SRC" ] ) |
| 170 | self.handle.expect( "tail" ) |
| 171 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 172 | result = self.handle.before + self.handle.after |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 173 | self.handle.sendline( "" ) |
| 174 | self.handle.expect( "\$" ) |
| 175 | if re.search( 'Unreachable', result ): |
| 176 | main.log.info( "Unreachable found in ping logs..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 177 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 178 | elif re.search( '64\sbytes', result ): |
| 179 | main.log.info( "Pings look good" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 180 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 181 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 182 | main.log.info( "No, or faulty ping data..." ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 184 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 185 | def pingKill( self, testONUser, testONIP ): |
| 186 | """ |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 187 | Kills all continuous ping processes. |
| 188 | Then copies all the ping files to the TestStation. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 189 | """ |
| 190 | self.handle.sendline( "" ) |
| 191 | self.handle.expect( "\$" ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 192 | command = "sudo kill -SIGINT `pgrep ping`" |
| 193 | main.log.info( command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 194 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 195 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | main.log.info( "Transferring ping files to TestStation" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 197 | command = "scp /tmp/ping.* " + \ |
| 198 | str( testONUser ) + "@" + str( testONIP ) + ":/tmp/" |
| 199 | self.execute( cmd=command, prompt="100%", timeout=20 ) |
| 200 | # Make sure the output is cleared |
| 201 | self.handle.sendline( "" ) |
| 202 | self.handle.expect( "\$" ) |
| 203 | self.handle.sendline( "" ) |
| 204 | self.handle.expect( "\$" ) |
| 205 | self.handle.sendline( "" ) |
| 206 | i = self.handle.expect( [ "password", "\$" ] ) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 207 | if i == 0: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 208 | main.log.error( "Error, sudo asking for password" ) |
| 209 | main.log.error( self.handle.before ) |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 210 | return main.FALSE |
| 211 | else: |
| 212 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 213 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 214 | def pingLongKill( self ): |
| 215 | self.handle.sendline( "" ) |
| 216 | self.handle.expect( "\$" ) |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 217 | command = "sudo kill -SIGING `pgrep ping`" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 218 | main.log.info( command ) |
| 219 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 220 | self.handle.sendline( "" ) |
| 221 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 222 | return main.TRUE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 223 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 224 | def pingHostOptical( self, **pingParams ): |
| 225 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 226 | This function is only for Packet Optical related ping |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 227 | Use the next pingHost() function for all normal scenarios ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 228 | Ping from one mininet host to another |
| 229 | Currently the only supported Params: SRC and TARGET |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 230 | """ |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 231 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 232 | command = args[ "SRC" ] + " ping " + \ |
| 233 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 234 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 235 | main.log.warn( "Sending: " + command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 236 | self.handle.sendline( command ) |
| 237 | i = self.handle.expect( [ command, pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 238 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 239 | main.log.error( |
| 240 | self.name + |
| 241 | ": timeout when waiting for response from mininet" ) |
| 242 | main.log.error( "response: " + str( self.handle.before ) ) |
| 243 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 244 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 245 | main.log.error( |
| 246 | self.name + |
| 247 | ": timeout when waiting for response from mininet" ) |
| 248 | main.log.error( "response: " + str( self.handle.before ) ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 249 | response = self.handle.before |
| 250 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 251 | main.log.error( self.name + ": EOF exception found" ) |
| 252 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 253 | main.cleanup() |
| 254 | main.exit() |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 255 | main.log.info( self.name + ": Ping Response: " + response ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 256 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 257 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 258 | main.lastResult = main.TRUE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 259 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 260 | else: |
| 261 | main.log.error( |
| 262 | self.name + |
| 263 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 264 | main.lastResult = main.FALSE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 265 | return main.FALSE |
| 266 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 267 | def pingHost( self, **pingParams ): |
| 268 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 269 | Pings between two hosts on remote mininet |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 270 | """ |
| 271 | self.handle.sendline( "" ) |
| 272 | self.handle.expect( "\$" ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 273 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 274 | command = "mininet/util/m " + \ |
| 275 | args[ "SRC" ] + " ping " + args[ "TARGET" ] + " -c 4 -W 1 -i .2" |
| 276 | main.log.info( command ) |
| 277 | response = self.execute( cmd=command, prompt="rtt", timeout=10 ) |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 278 | if utilities.assert_matches( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 279 | expect=',\s0\%\spacket\sloss', |
| 280 | actual=response, |
| 281 | onpass="No Packet loss", |
| 282 | onfail="Host is not reachable" ): |
| 283 | main.log.info( "NO PACKET LOSS, HOST IS REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 284 | main.lastResult = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 285 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 286 | else: |
| 287 | main.log.error( "PACKET LOST, HOST IS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 288 | main.lastResult = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 289 | return main.FALSE |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 290 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 291 | def checknum( self, num ): |
| 292 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 293 | Verifies the correct number of switches are running |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 294 | """ |
| 295 | if self.handle: |
| 296 | self.handle.sendline( "" ) |
| 297 | self.handle.expect( "\$" ) |
| 298 | self.handle.sendline( 'ifconfig -a | grep "sw.. " | wc -l' ) |
| 299 | self.handle.expect( "wc" ) |
| 300 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 301 | response = self.handle.before |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 302 | self.handle.sendline( |
| 303 | 'ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l' ) |
| 304 | self.handle.expect( "color" ) |
| 305 | self.handle.expect( "\$" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 306 | response2 = self.handle.before |
| 307 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 308 | if re.search( num, response ): |
| 309 | if re.search( num, response2 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 310 | return main.TRUE |
| 311 | else: |
| 312 | return main.FALSE |
| 313 | else: |
| 314 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 315 | else: |
| 316 | main.log.error( "Connection failed to the host" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 317 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 318 | def startTcpdump( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 319 | self, |
| 320 | filename, |
| 321 | intf="eth0", |
| 322 | port="port 6633", |
| 323 | user="admin" ): |
| 324 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 325 | Runs tcpdump on an interface and saves the file |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 326 | intf can be specified, or the default eth0 is used |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 327 | """ |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 328 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 329 | self.handle.sendline( "" ) |
| 330 | self.handle.sendline( |
| 331 | "sudo tcpdump -n -i " + |
| 332 | intf + |
| 333 | " " + |
| 334 | port + |
| 335 | " -w " + |
| 336 | filename.strip() + |
| 337 | " -Z " + |
| 338 | user + |
| 339 | " &" ) |
| 340 | self.handle.sendline( "" ) |
| 341 | self.handle.sendline( "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 342 | i = self.handle.expect( [ 'No\ssuch\device', 'listening\son', |
| 343 | pexpect.TIMEOUT, "\$" ], timeout=10 ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 344 | main.log.info( self.handle.before + self.handle.after ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 345 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 346 | main.log.error( self.name + ": tcpdump - No such device exists.\ |
| 347 | tcpdump attempted on: " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 348 | return main.FALSE |
| 349 | elif i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 350 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 351 | return main.TRUE |
| 352 | elif i == 2: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 353 | main.log.error( self.name + ": tcpdump command timed out!\ |
| 354 | Check interface name, given interface was: " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 355 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 356 | elif i == 3: |
| 357 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 358 | return main.TRUE |
| 359 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 360 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 361 | return main.FALSE |
| 362 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 363 | main.log.error( self.name + ": EOF exception found" ) |
| 364 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 365 | main.cleanup() |
| 366 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 367 | except Exception: |
| 368 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 369 | main.cleanup() |
| 370 | main.exit() |
| 371 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 372 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 373 | """ |
| 374 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 375 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 376 | self.handle.sendline( "sudo pkill tcpdump" ) |
| 377 | self.handle.sendline( "" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 378 | self.handle.expect( "\$" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 379 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 380 | main.log.error( self.name + ": EOF exception found" ) |
| 381 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 382 | main.cleanup() |
| 383 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 384 | except Exception: |
| 385 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 386 | main.cleanup() |
| 387 | main.exit() |
| 388 | |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 389 | def runOpticalMnScript( self,name = 'onos', ctrllerIP = None ): |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 390 | import time |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 391 | import types |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 392 | """ |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 393 | Description: |
| 394 | This function is only meant for Packet Optical. |
| 395 | It runs python script "opticalTest.py" to create the |
| 396 | packet layer( mn ) and optical topology |
| 397 | Optional: |
| 398 | name - Name of onos directory. (ONOS | onos) |
| 399 | Required: |
| 400 | ctrllerIP = Controller(s) IP address |
| 401 | TODO: If no ctrllerIP is provided, a default |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 402 | $OC1 can be accepted |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 403 | """ |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 404 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 405 | self.handle.sendline( "" ) |
| 406 | self.handle.expect( "\$" ) |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 407 | self.handle.sendline( "cd ~/" + name + "/tools/test/topos" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 408 | self.handle.expect( "topos\$" ) |
| 409 | if ctrllerIP == None: |
| 410 | main.log.info( "You need to specify the IP" ) |
| 411 | return main.FALSE |
| 412 | else: |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 413 | controller = '' |
| 414 | if isinstance( ctrllerIP, types.ListType ): |
| 415 | for i in xrange( len( ctrllerIP ) ): |
| 416 | controller += ctrllerIP[i] + ' ' |
| 417 | main.log.info( "Mininet topology is being loaded with " + |
| 418 | "controllers: " + controller ) |
| 419 | elif isinstance( ctrllerIP, types.StringType ): |
| 420 | controller = ctrllerIP |
| 421 | main.log.info( "Mininet topology is being loaded with " + |
| 422 | "controller: " + controller ) |
| 423 | else: |
| 424 | main.log.info( "You need to specify a valid IP" ) |
| 425 | return main.FALSE |
| 426 | cmd = "sudo -E python opticalTest.py " + controller |
| 427 | main.log.info( self.name + ": cmd = " + cmd ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 428 | self.handle.sendline( cmd ) |
shahshreya | d524a94 | 2015-04-21 09:55:16 -0700 | [diff] [blame] | 429 | time.sleep(30) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 430 | self.handle.sendline( "" ) |
shahshreya | 3140b1a | 2015-04-28 14:22:15 -0700 | [diff] [blame] | 431 | self.handle.sendline( "" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 432 | self.handle.expect("mininet>") |
| 433 | return main.TRUE |
| 434 | except pexpect.EOF: |
| 435 | main.log.error( self.name + ": EOF exception found" ) |
| 436 | main.log.error( self.name + ": " + self.handle.before ) |
| 437 | return main.FALSE |
| 438 | |
| 439 | def attachLincOESession( self ): |
| 440 | """ |
| 441 | Since executing opticalTest.py will give you mininet |
| 442 | prompt, you would at some point require to get onto |
| 443 | console of LincOE ((linc@onosTestBench)1>) to execute |
| 444 | commands like bring a optical port up or down on a ROADM |
| 445 | You can attach to console of Linc-OE session by a cmd: |
| 446 | sudo ~/linc-oe/rel/linc/bin/linc attach |
| 447 | """ |
| 448 | try: |
| 449 | self.handle.sendline( "" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 450 | self.handle.expect( "\$" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 451 | self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 452 | self.handle.expect( ">" ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 453 | return main.TRUE |
| 454 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 455 | main.log.error( self.name + ": EOF exception found" ) |
| 456 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 457 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 458 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 459 | def disconnect( self ): |
| 460 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 461 | Called at the end of the test to disconnect the handle. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 462 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 463 | if self.handle: |
shahshreya | 95dd6f8 | 2015-02-05 15:59:16 -0800 | [diff] [blame] | 464 | # Close the ssh connection |
| 465 | self.handle.sendline( "" ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 466 | # self.handle.expect( "\$" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 467 | i = self.handle.expect( [ '\$', 'mininet>', pexpect.TIMEOUT, |
| 468 | pexpect.EOF ], timeout=2 ) |
shahshreya | dccfbc6 | 2015-02-20 11:40:09 -0800 | [diff] [blame] | 469 | if i == 0: |
| 470 | self.handle.sendline( "exit" ) |
| 471 | self.handle.expect( "closed" ) |
| 472 | elif i == 1: |
| 473 | self.handle.sendline( "exit" ) |
| 474 | self.handle.expect( "exit" ) |
| 475 | self.handle.expect('\$') |
| 476 | self.handle.sendline( "exit" ) |
| 477 | self.handle.expect( "exit" ) |
| 478 | self.handle.expect( "closed" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 479 | else: |
| 480 | main.log.error( "Connection failed to the host" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 481 | return main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 482 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 483 | def getFlowTable( self, protoVersion, sw ): |
| 484 | """ |
| 485 | TODO document usage |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 486 | TODO add option to look at cookies. ignoring them for now |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 487 | |
| 488 | print "get_flowTable(" + str( protoVersion ) +" " + str( sw ) +")" |
| 489 | NOTE: Use format to force consistent flow table output across |
Jon Hall | de5ef2a | 2015-07-22 14:45:24 -0700 | [diff] [blame] | 490 | versions |
| 491 | """ |
| 492 | try: |
| 493 | self.handle.sendline( "cd" ) |
| 494 | self.handle.expect( "\$" ) |
| 495 | if protoVersion == 1.0: |
| 496 | command = "sudo ovs-ofctl dump-flows " + sw + \ |
| 497 | " -F OpenFlow10-table_id | awk '{OFS=\",\" ; print $1 $3 $6 \ |
| 498 | $7 $8}' | cut -d ',' -f 2- | sort -n -k1 -r" |
| 499 | self.handle.sendline( command ) |
Jon Hall | 7d7ef3f | 2015-07-22 15:39:24 -0700 | [diff] [blame] | 500 | self.handle.expect( "sort" ) |
Jon Hall | de5ef2a | 2015-07-22 14:45:24 -0700 | [diff] [blame] | 501 | self.handle.expect( "OFPST_FLOW" ) |
| 502 | response = self.handle.before |
| 503 | # print "response=", response |
| 504 | return response |
| 505 | elif protoVersion == 1.3: |
| 506 | command = "sudo ovs-ofctl dump-flows " + sw + \ |
| 507 | " -O OpenFlow13 | awk '{OFS=\",\" ; print $1 $3 $6 $7}'\ |
| 508 | | cut -d ',' -f 2- | sort -n -k1 -r" |
| 509 | self.handle.sendline( command ) |
Jon Hall | 7d7ef3f | 2015-07-22 15:39:24 -0700 | [diff] [blame] | 510 | self.handle.expect( "sort" ) |
Jon Hall | de5ef2a | 2015-07-22 14:45:24 -0700 | [diff] [blame] | 511 | self.handle.expect( "OFPST_FLOW" ) |
| 512 | response = self.handle.before |
| 513 | # print "response=", response |
| 514 | return response |
| 515 | else: |
| 516 | main.log.error( |
| 517 | "Unknown protoVersion in get_flowTable(). given: (" + |
| 518 | str( type( protoVersion ) ) + |
| 519 | ") '" + str( protoVersion ) + "'" ) |
| 520 | except pexpect.EOF: |
| 521 | main.log.error( self.name + ": EOF exception found" ) |
| 522 | main.log.error( self.name + ": " + self.handle.before ) |
| 523 | main.cleanup() |
| 524 | main.exit() |
| 525 | except pexpect.TIMEOUT: |
| 526 | main.log.exception( self.name + ": Timeout exception: " ) |
| 527 | return None |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 528 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 529 | def flowComp( self, flow1, flow2 ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 530 | if flow1 == flow2: |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 531 | return main.TRUE |
| 532 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 533 | main.log.info( "Flow tables do not match, printing tables:" ) |
| 534 | main.log.info( "Flow Table 1:" ) |
| 535 | main.log.info( flow1 ) |
| 536 | main.log.info( "Flow Table 2:" ) |
| 537 | main.log.info( flow2 ) |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 538 | return main.FALSE |
| 539 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 540 | def setIpTablesOUTPUT( self, dstIp, dstPort, action='add', |
| 541 | packetType='tcp', rule='DROP' ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 542 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 543 | Description: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 544 | add or remove iptables rule to DROP ( default ) |
| 545 | packets from specific IP and PORT |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 546 | Usage: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 547 | * specify action ( 'add' or 'remove' ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 548 | when removing, pass in the same argument as you would add. It will |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 549 | delete that specific rule. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 550 | * specify the destination ip to block with dstIp |
| 551 | * specify destination port to block to dstPort |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 552 | * optional packet type to block ( default tcp ) |
| 553 | * optional iptables rule ( default DROP ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 554 | WARNING: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 555 | * This function uses root privilege iptables command which may result |
| 556 | in unwanted network errors. USE WITH CAUTION |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 557 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 558 | import re |
| 559 | import time |
| 560 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 561 | # NOTE********* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 562 | # The strict checking methods of this driver function is intentional |
| 563 | # to discourage any misuse or error of iptables, which can cause |
| 564 | # severe network errors |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 565 | # ************* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 566 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 567 | # NOTE: Sleep needed to give some time |
| 568 | # for rule to be added and registered |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 569 | # to the instance |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 570 | time.sleep( 5 ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 571 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 572 | actionType = action.lower() |
| 573 | if actionType != 'add' and actionType != 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 574 | main.log.error( |
| 575 | "Invalid action type. 'add' or 'remove' table rule" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 576 | if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 577 | # NOTE: Currently only supports rules DROP, ACCEPT, and LOG |
| 578 | main.log.error( |
| 579 | "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 580 | return |
| 581 | return |
| 582 | else: |
| 583 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 584 | # If there is no existing rule in the iptables, we will see an |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 585 | # 'iptables:'... message. We expect to see this message. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 586 | # Otherwise, if there IS an existing rule, we will get the prompt |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 587 | # back, hence why we expect $ for remove type. We want to remove |
| 588 | # an already existing rule |
| 589 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 590 | if actionType == 'add': |
| 591 | # NOTE: "iptables:" expect is a result of |
| 592 | # return from the command |
| 593 | # iptables -C ... |
| 594 | # Any changes by the iptables command return string |
| 595 | # will result in failure of the function. ( deemed unlikely |
| 596 | # at the time of writing this function ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 597 | # Check for existing rules on current input |
| 598 | self.handle.sendline( "" ) |
| 599 | self.handle.expect( "\$" ) |
| 600 | self.handle.sendline( |
| 601 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 603 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 604 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 605 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 606 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 607 | " -j " + |
| 608 | str( rule ) ) |
| 609 | i = self.handle.expect( [ "iptables:", "\$" ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 610 | print i |
| 611 | print self.handle.before |
| 612 | print "after: " |
| 613 | print self.handle.after |
| 614 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 615 | elif actionType == 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 616 | # Check for existing rules on current input |
| 617 | self.handle.sendline( "" ) |
| 618 | self.handle.expect( "\$" ) |
| 619 | self.handle.sendline( |
| 620 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 621 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 622 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 623 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 624 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 625 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 626 | " -j " + |
| 627 | str( rule ) ) |
| 628 | self.handle.expect( "\$" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 629 | print "before: " |
| 630 | print self.handle.before |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 631 | actualString = self.handle.after |
| 632 | expectString = "iptables:" |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 633 | print "Actual String:" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 634 | print actualString |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 635 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 636 | if re.search( expectString, actualString ): |
| 637 | matchResult = main.TRUE |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 638 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 639 | matchResult = main.FALSE |
| 640 | # If matchResult is main.TRUE, it means there is no matching rule. |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 641 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 642 | # If tables does not exist and expected prompt is returned, |
| 643 | # go ahead and add iptables rule |
| 644 | if matchResult == main.TRUE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 645 | # Ensure action type is add |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 646 | if actionType == 'add': |
| 647 | # -A is the 'append' action of iptables |
| 648 | actionAdd = '-A' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 649 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 650 | self.handle.sendline( "" ) |
| 651 | self.handle.sendline( |
| 652 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 653 | actionAdd + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 654 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 655 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 656 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 657 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 658 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 659 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 660 | " -j " + |
| 661 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 662 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 663 | infoString = "Rules added to " + str( self.name ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 664 | infoString += "iptables rule added to block IP: " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 665 | str( dstIp ) |
| 666 | infoString += "Port: " + \ |
| 667 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 668 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 669 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 670 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 671 | self.handle.expect( |
| 672 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 673 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 674 | main.log.error( |
| 675 | self.name + |
| 676 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 677 | except Exception: |
| 678 | main.log.exception( self.name + |
| 679 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 680 | main.cleanup() |
| 681 | main.exit() |
| 682 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 683 | main.log.error( |
| 684 | "Given rule already exists, but attempted to add it" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 685 | # If matchResult is 0, it means there IS a matching rule provided |
| 686 | elif matchResult == main.FALSE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 687 | # Ensure action type is remove |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 688 | if actionType == 'remove': |
| 689 | # -D is the 'delete' rule of iptables |
| 690 | actionRemove = '-D' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 691 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 692 | self.handle.sendline( "" ) |
| 693 | # Delete a specific rule specified into the function |
| 694 | self.handle.sendline( |
| 695 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 696 | actionRemove + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 697 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 698 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 699 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 700 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 701 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 702 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 703 | " -j " + |
| 704 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 705 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 706 | infoString = "Rules removed from " + str( self.name ) |
| 707 | infoString += " iptables rule removed \ |
| 708 | from blocking IP: " + \ |
| 709 | str( dstIp ) |
| 710 | infoString += " Port: " + \ |
| 711 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 712 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 713 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 714 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 715 | self.handle.expect( |
| 716 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 717 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 718 | main.log.error( |
| 719 | self.name + |
| 720 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 721 | except Exception: |
| 722 | main.log.exception( self.name + |
| 723 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 724 | main.cleanup() |
| 725 | main.exit() |
| 726 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 727 | main.log.error( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 728 | "Given rule does not exist,\ |
| 729 | but attempted to remove it" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 730 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 731 | # NOTE: If a bad usage of this function occurs, exit the entire |
| 732 | # test |
| 733 | main.log.error( "Bad rule given for iptables. Exiting..." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 734 | main.cleanup() |
| 735 | main.exit() |
| 736 | |
| 737 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 738 | if __name__ != "__main__": |
| 739 | import sys |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 740 | sys.modules[ __name__ ] = RemoteMininetDriver() |