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