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 | feff308 | 2015-05-19 10:23:26 -0700 | [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( "\$" ) |
Jon Hall | 390696c | 2015-05-05 17:13:41 -0700 | [diff] [blame] | 451 | i = self.handle.expect( [ '\$', 'mininet>', pexpect.TIMEOUT, |
| 452 | pexpect.EOF ], 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" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 463 | else: |
| 464 | main.log.error( "Connection failed to the host" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 465 | return main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 466 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 467 | def getFlowTable( self, protoVersion, sw ): |
| 468 | """ |
| 469 | TODO document usage |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 470 | TODO add option to look at cookies. ignoring them for now |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 471 | |
| 472 | print "get_flowTable(" + str( protoVersion ) +" " + str( sw ) +")" |
| 473 | NOTE: Use format to force consistent flow table output across |
| 474 | versions""" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 475 | self.handle.sendline( "cd" ) |
| 476 | self.handle.expect( [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 477 | if protoVersion == 1.0: |
| 478 | command = "sudo ovs-ofctl dump-flows " + sw + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 479 | " -F OpenFlow10-table_id | awk '{OFS=\",\" ; print $1 $3 $6 \ |
| 480 | $7 $8}' | cut -d ',' -f 2- | sort -n -k1 -r" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 481 | self.handle.sendline( command ) |
| 482 | self.handle.expect( [ "k1 -r", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 483 | self.handle.expect( |
| 484 | [ "OFPST_FLOW", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 485 | response = self.handle.before |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 486 | # print "response=", response |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 487 | return response |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 488 | elif protoVersion == 1.3: |
| 489 | command = "sudo ovs-ofctl dump-flows " + sw + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 490 | " -O OpenFlow13 | awk '{OFS=\",\" ; print $1 $3 $6 $7}'\ |
| 491 | | cut -d ',' -f 2- | sort -n -k1 -r" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 492 | self.handle.sendline( command ) |
| 493 | self.handle.expect( [ "k1 -r", pexpect.EOF, pexpect.TIMEOUT ] ) |
| 494 | self.handle.expect( |
| 495 | [ "OFPST_FLOW", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 496 | response = self.handle.before |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 497 | # print "response=", response |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 498 | return response |
| 499 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 500 | main.log.error( |
| 501 | "Unknown protoVersion in get_flowTable(). given: (" + |
| 502 | str( |
| 503 | type( protoVersion ) ) + |
| 504 | ") '" + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 505 | str( protoVersion ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 506 | "'" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 507 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 508 | def flowComp( self, flow1, flow2 ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 509 | if flow1 == flow2: |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 510 | return main.TRUE |
| 511 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 512 | main.log.info( "Flow tables do not match, printing tables:" ) |
| 513 | main.log.info( "Flow Table 1:" ) |
| 514 | main.log.info( flow1 ) |
| 515 | main.log.info( "Flow Table 2:" ) |
| 516 | main.log.info( flow2 ) |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 517 | return main.FALSE |
| 518 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 519 | def setIpTablesOUTPUT( self, dstIp, dstPort, action='add', |
| 520 | packetType='tcp', rule='DROP' ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 521 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 522 | Description: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 523 | add or remove iptables rule to DROP ( default ) |
| 524 | packets from specific IP and PORT |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 525 | Usage: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 526 | * specify action ( 'add' or 'remove' ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 527 | 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] | 528 | delete that specific rule. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 529 | * specify the destination ip to block with dstIp |
| 530 | * specify destination port to block to dstPort |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 531 | * optional packet type to block ( default tcp ) |
| 532 | * optional iptables rule ( default DROP ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 533 | WARNING: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 534 | * This function uses root privilege iptables command which may result |
| 535 | in unwanted network errors. USE WITH CAUTION |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 536 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 537 | import re |
| 538 | import time |
| 539 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 540 | # NOTE********* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 541 | # The strict checking methods of this driver function is intentional |
| 542 | # to discourage any misuse or error of iptables, which can cause |
| 543 | # severe network errors |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 544 | # ************* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 545 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 546 | # NOTE: Sleep needed to give some time |
| 547 | # for rule to be added and registered |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 548 | # to the instance |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 549 | time.sleep( 5 ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 550 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 551 | actionType = action.lower() |
| 552 | if actionType != 'add' and actionType != 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 553 | main.log.error( |
| 554 | "Invalid action type. 'add' or 'remove' table rule" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 555 | if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 556 | # NOTE: Currently only supports rules DROP, ACCEPT, and LOG |
| 557 | main.log.error( |
| 558 | "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 559 | return |
| 560 | return |
| 561 | else: |
| 562 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 563 | # 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] | 564 | # 'iptables:'... message. We expect to see this message. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 565 | # Otherwise, if there IS an existing rule, we will get the prompt |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 566 | # back, hence why we expect $ for remove type. We want to remove |
| 567 | # an already existing rule |
| 568 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 569 | if actionType == 'add': |
| 570 | # NOTE: "iptables:" expect is a result of |
| 571 | # return from the command |
| 572 | # iptables -C ... |
| 573 | # Any changes by the iptables command return string |
| 574 | # will result in failure of the function. ( deemed unlikely |
| 575 | # at the time of writing this function ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 576 | # Check for existing rules on current input |
| 577 | self.handle.sendline( "" ) |
| 578 | self.handle.expect( "\$" ) |
| 579 | self.handle.sendline( |
| 580 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 581 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 582 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 583 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 584 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 585 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 586 | " -j " + |
| 587 | str( rule ) ) |
| 588 | i = self.handle.expect( [ "iptables:", "\$" ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 589 | print i |
| 590 | print self.handle.before |
| 591 | print "after: " |
| 592 | print self.handle.after |
| 593 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 594 | elif actionType == 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 595 | # Check for existing rules on current input |
| 596 | self.handle.sendline( "" ) |
| 597 | self.handle.expect( "\$" ) |
| 598 | self.handle.sendline( |
| 599 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 600 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 601 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 602 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 603 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 604 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 605 | " -j " + |
| 606 | str( rule ) ) |
| 607 | self.handle.expect( "\$" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 608 | print "before: " |
| 609 | print self.handle.before |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 610 | actualString = self.handle.after |
| 611 | expectString = "iptables:" |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 612 | print "Actual String:" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 613 | print actualString |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 614 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 615 | if re.search( expectString, actualString ): |
| 616 | matchResult = main.TRUE |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 617 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 618 | matchResult = main.FALSE |
| 619 | # If matchResult is main.TRUE, it means there is no matching rule. |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 620 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 621 | # If tables does not exist and expected prompt is returned, |
| 622 | # go ahead and add iptables rule |
| 623 | if matchResult == main.TRUE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 624 | # Ensure action type is add |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 625 | if actionType == 'add': |
| 626 | # -A is the 'append' action of iptables |
| 627 | actionAdd = '-A' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 628 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 629 | self.handle.sendline( "" ) |
| 630 | self.handle.sendline( |
| 631 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 632 | actionAdd + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 633 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 634 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 635 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 636 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 637 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 638 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 639 | " -j " + |
| 640 | str( 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 | infoString = "Rules added to " + str( self.name ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 643 | infoString += "iptables rule added to block IP: " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 644 | str( dstIp ) |
| 645 | infoString += "Port: " + \ |
| 646 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 647 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 648 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 649 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 650 | self.handle.expect( |
| 651 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 652 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 653 | main.log.error( |
| 654 | self.name + |
| 655 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 656 | except Exception: |
| 657 | main.log.exception( self.name + |
| 658 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 659 | main.cleanup() |
| 660 | main.exit() |
| 661 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 662 | main.log.error( |
| 663 | "Given rule already exists, but attempted to add it" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 664 | # If matchResult is 0, it means there IS a matching rule provided |
| 665 | elif matchResult == main.FALSE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 666 | # Ensure action type is remove |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 667 | if actionType == 'remove': |
| 668 | # -D is the 'delete' rule of iptables |
| 669 | actionRemove = '-D' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 670 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 671 | self.handle.sendline( "" ) |
| 672 | # Delete a specific rule specified into the function |
| 673 | self.handle.sendline( |
| 674 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 675 | actionRemove + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 676 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 677 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 678 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 679 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 680 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 681 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 682 | " -j " + |
| 683 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 684 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 685 | infoString = "Rules removed from " + str( self.name ) |
| 686 | infoString += " iptables rule removed \ |
| 687 | from blocking IP: " + \ |
| 688 | str( dstIp ) |
| 689 | infoString += " Port: " + \ |
| 690 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 691 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 692 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 693 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 694 | self.handle.expect( |
| 695 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 696 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 697 | main.log.error( |
| 698 | self.name + |
| 699 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 700 | except Exception: |
| 701 | main.log.exception( self.name + |
| 702 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 703 | main.cleanup() |
| 704 | main.exit() |
| 705 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 706 | main.log.error( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 707 | "Given rule does not exist,\ |
| 708 | but attempted to remove it" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 709 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 710 | # NOTE: If a bad usage of this function occurs, exit the entire |
| 711 | # test |
| 712 | main.log.error( "Bad rule given for iptables. Exiting..." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 713 | main.cleanup() |
| 714 | main.exit() |
| 715 | |
| 716 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 717 | if __name__ != "__main__": |
| 718 | import sys |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 719 | sys.modules[ __name__ ] = RemoteMininetDriver() |