admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 26-Oct-2012 |
| 4 | |
| 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 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 |
| 11 | (at your option) any later version. |
| 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 |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | |
| 22 | MininetCliDriver is the basic driver which will handle the Mininet functions |
| 23 | ''' |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 24 | import traceback |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 25 | import pexpect |
| 26 | import struct |
| 27 | import fcntl |
| 28 | import os |
| 29 | import signal |
| 30 | import re |
| 31 | import sys |
| 32 | import core.teston |
| 33 | sys.path.append("../") |
| 34 | from drivers.common.cli.emulatordriver import Emulator |
| 35 | from drivers.common.clidriver import CLI |
| 36 | from time import time |
| 37 | |
| 38 | class RemoteMininetDriver(Emulator): |
| 39 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 40 | RemoteMininetCliDriver is the basic driver which will handle the Mininet functions |
| 41 | The main different between this and the MininetCliDriver is that this one does not build the mininet. |
| 42 | It assumes that there is already a mininet running on the target. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | ''' |
| 44 | def __init__(self): |
| 45 | super(Emulator, self).__init__() |
| 46 | self.handle = self |
| 47 | self.wrapped = sys.modules[__name__] |
| 48 | self.flag = 0 |
| 49 | |
| 50 | def connect(self, **connectargs): |
| 51 | #,user_name, ip_address, pwd,options): |
| 52 | # Here the main is the TestON instance after creating all the log handles. |
| 53 | for key in connectargs: |
| 54 | vars(self)[key] = connectargs[key] |
| 55 | |
| 56 | self.name = self.options['name'] |
| 57 | self.handle = super(RemoteMininetDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd) |
| 58 | |
| 59 | self.ssh_handle = self.handle |
| 60 | |
| 61 | # Copying the readme file to process the |
| 62 | if self.handle : |
| 63 | return main.TRUE |
| 64 | |
| 65 | else : |
| 66 | main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 67 | main.log.error("Failed to connect to the Mininet") |
| 68 | return main.FALSE |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 69 | |
| 70 | #********************************************************************************************* |
| 71 | #********************************************************************************************* |
| 72 | # checkForLoss will determine if any of the pings had any packets lost during the course of |
| 73 | # the pingLong. |
| 74 | #********************************************************************************************* |
| 75 | #********************************************************************************************* |
| 76 | |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 77 | def checkForLoss(self, pingList): |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 78 | ''' |
| 79 | Returns main.FALSE for 0% packet loss and |
| 80 | Returns main.ERROR if "found multiple mininet" is found and |
| 81 | Returns main.TRUE else |
| 82 | ''' |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 83 | #TODO: maybe we want to return the % loss instead? This way we can set an acceptible loss %. |
| 84 | #EX: 393 packets transmitted, 380 received, 3% packet loss, time 78519ms |
| 85 | # we may need to return a float to get around rounding errors |
| 86 | |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 87 | import os |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 88 | self.handle.sendline("") |
| 89 | self.handle.expect("\$") |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 90 | #Clear any output waiting in the bg from killing pings |
| 91 | self.handle.sendline("") |
| 92 | self.handle.expect("\$") |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 93 | self.handle.sendline("cat " + pingList) |
| 94 | self.handle.expect(pingList) |
| 95 | self.handle.expect("\$") |
| 96 | outputs = self.handle.before + self.handle.after |
| 97 | if re.search(" 0% packet loss",outputs): |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 98 | return main.FALSE |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 99 | elif re.search("found multiple mininet",outputs): |
| 100 | return main.ERROR |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 101 | else: |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 102 | main.log.error("Error, unexpected output in the ping file") |
| 103 | main.log.warn( outputs ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 104 | return main.TRUE |
| 105 | |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 106 | |
| 107 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | def pingLong(self,**pingParams): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 109 | ''' |
| 110 | Starts a continuous ping on the mininet host outputing to a file in the /tmp dir. |
| 111 | ''' |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 112 | self.handle.sendline("") |
| 113 | self.handle.expect("\$") |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 114 | args = utilities.parse_args(["SRC","TARGET","PINGTIME"],**pingParams) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 115 | precmd = "sudo rm /tmp/ping." + args["SRC"] |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 116 | self.execute(cmd=precmd,prompt="(.*)",timeout=10) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 117 | command = "sudo mininet/util/m " + args["SRC"] + " ping "+args ["TARGET"]+" -i .2 -w " + str(args['PINGTIME']) + " > /tmp/ping." + args["SRC"] + " &" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 118 | main.log.info( command ) |
| 119 | self.execute(cmd=command,prompt="(.*)",timeout=10) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 120 | self.handle.sendline("") |
| 121 | self.handle.expect("\$") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | return main.TRUE |
| 123 | |
| 124 | def pingstatus(self,**pingParams): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 125 | ''' |
| 126 | Tails the respective ping output file and check that there is a moving "64 bytes" |
| 127 | ''' |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 128 | self.handle.sendline("") |
| 129 | self.handle.expect("\$") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | args = utilities.parse_args(["SRC"],**pingParams) |
| 131 | self.handle.sendline("tail /tmp/ping." + args["SRC"]) |
| 132 | self.handle.expect("tail") |
| 133 | self.handle.expect("\$") |
| 134 | result = self.handle.before + self.handle.after |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 135 | self.handle.sendline("") |
| 136 | self.handle.expect("\$") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 137 | if re.search('Unreachable', result ): |
| 138 | main.log.info("Unreachable found in ping logs...") |
| 139 | return main.FALSE |
| 140 | elif re.search('64\sbytes', result): |
| 141 | main.log.info("Pings look good") |
| 142 | return main.TRUE |
| 143 | else: |
| 144 | main.log.info("No, or faulty ping data...") |
| 145 | return main.FALSE |
| 146 | |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 147 | def pingKill(self, testONUser, testONIP): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 148 | ''' |
| 149 | Kills all continuous ping processes. |
| 150 | Then copies all the ping files to the TestStation. |
| 151 | ''' |
| 152 | import time |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 153 | self.handle.sendline("") |
| 154 | self.handle.expect("\$") |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 155 | command = "sudo kill -SIGINT `pgrep ping`" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | main.log.info( command ) |
| 157 | self.execute(cmd=command,prompt="(.*)",timeout=10) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 158 | #Commenting out in case TestON and MN are on the same machine. scp overrights the file anyways |
| 159 | #main.log.info( "Removing old ping data" ) |
| 160 | #command = "rm /tmp/ping.*" |
| 161 | #os.popen(command) |
| 162 | #time.sleep(2) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 163 | main.log.info( "Transferring ping files to TestStation" ) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 164 | command = "scp /tmp/ping.* "+ str(testONUser) + "@" + str(testONIP) + ":/tmp/" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 165 | self.execute(cmd=command,prompt="100%",timeout=20) |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 166 | #Make sure the output is cleared |
| 167 | self.handle.sendline("") |
| 168 | self.handle.expect("\$") |
| 169 | self.handle.sendline("") |
| 170 | self.handle.expect("\$") |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 171 | self.handle.sendline("") |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 172 | i=self.handle.expect(["password","\$"]) |
| 173 | if i == 0: |
| 174 | main.log.error("Error, sudo asking for password") |
| 175 | main.log.error(self.handle.before) |
| 176 | return main.FALSE |
| 177 | else: |
| 178 | return main.TRUE |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 179 | |
| 180 | def pingLongKill(self): |
| 181 | import time |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 182 | self.handle.sendline("") |
| 183 | self.handle.expect("\$") |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 184 | command = "sudo kill -SIGING `pgrep ping`" |
| 185 | main.log.info(command) |
| 186 | self.execute(cmd=command,prompt="(.*)",timeout=10) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 187 | self.handle.sendline("") |
| 188 | self.handle.expect("\$") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | return main.TRUE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 190 | |
| 191 | def pingHostOptical(self,**pingParams): |
| 192 | ''' |
| 193 | This function is only for Packey Optical related ping |
| 194 | Use the next pingHost() function for all normal scenarios) |
| 195 | Ping from one mininet host to another |
| 196 | Currently the only supported Params: SRC and TARGET |
| 197 | ''' |
| 198 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
| 199 | #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"] |
| 200 | command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8" |
| 201 | try: |
| 202 | main.log.warn("Sending: " + command) |
| 203 | #response = self.execute(cmd=command,prompt="mininet",timeout=10 ) |
| 204 | self.handle.sendline(command) |
| 205 | i = self.handle.expect([command,pexpect.TIMEOUT]) |
| 206 | if i == 1: |
| 207 | main.log.error(self.name + ": timeout when waiting for response from mininet") |
| 208 | main.log.error("response: " + str(self.handle.before)) |
| 209 | i = self.handle.expect(["mininet>",pexpect.TIMEOUT]) |
| 210 | if i == 1: |
| 211 | main.log.error(self.name + ": timeout when waiting for response from mininet") |
| 212 | main.log.error("response: " + str(self.handle.before)) |
| 213 | response = self.handle.before |
| 214 | except pexpect.EOF: |
| 215 | main.log.error(self.name + ": EOF exception found") |
| 216 | main.log.error(self.name + ": " + self.handle.before) |
| 217 | main.cleanup() |
| 218 | main.exit() |
| 219 | main.log.info(self.name+": Ping Response: "+ response ) |
| 220 | #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"): |
| 221 | if re.search(',\s0\%\spacket\sloss',response): |
| 222 | main.log.info(self.name+": no packets lost, host is reachable") |
| 223 | main.last_result = main.TRUE |
| 224 | return main.TRUE |
| 225 | else : |
| 226 | main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE") |
| 227 | main.last_result = main.FALSE |
| 228 | return main.FALSE |
| 229 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 230 | def pingHost(self,**pingParams): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 231 | ''' |
| 232 | Pings between two hosts on remote mininet |
| 233 | ''' |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 234 | self.handle.sendline("") |
| 235 | self.handle.expect("\$") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 236 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 237 | #command = "mininet/util/m " + args["SRC"] + " ping "+args ["TARGET"]+" -c 4 -W 1 -i .2" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 238 | command = "mininet/util/m " + args["SRC"] + " ping "+args ["TARGET"]+" -c 4 -W 1 -i .2" |
| 239 | main.log.info ( command ) |
| 240 | response = self.execute(cmd=command,prompt="rtt",timeout=10 ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 241 | #self.handle.sendline("") |
| 242 | #self.handle.expect("\$") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 243 | if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"): |
| 244 | main.log.info("NO PACKET LOSS, HOST IS REACHABLE") |
| 245 | main.last_result = main.TRUE |
| 246 | return main.TRUE |
| 247 | else : |
| 248 | main.log.error("PACKET LOST, HOST IS NOT REACHABLE") |
| 249 | main.last_result = main.FALSE |
| 250 | return main.FALSE |
| 251 | |
| 252 | |
| 253 | def checknum(self,num): |
| 254 | ''' |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 255 | Verifies the correct number of switches are running |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 256 | ''' |
| 257 | if self.handle : |
| 258 | self.handle.sendline("") |
| 259 | self.handle.expect("\$") |
| 260 | self.handle.sendline('ifconfig -a | grep "sw.. " | wc -l') |
| 261 | self.handle.expect("wc") |
| 262 | self.handle.expect("\$") |
| 263 | response = self.handle.before |
| 264 | self.handle.sendline('ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l') |
| 265 | self.handle.expect("color") |
| 266 | self.handle.expect("\$") |
| 267 | response2 = self.handle.before |
| 268 | |
| 269 | if re.search(num, response): |
| 270 | if re.search(num, response2): |
| 271 | return main.TRUE |
| 272 | else: |
| 273 | return main.FALSE |
| 274 | else: |
| 275 | return main.FALSE |
| 276 | else : |
| 277 | main.log.error("Connection failed to the host") |
| 278 | |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 279 | def start_tcpdump(self, filename, intf = "eth0", port = "port 6633", user="admin"): |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 280 | ''' |
| 281 | Runs tpdump on an intferface and saves the file |
| 282 | intf can be specified, or the default eth0 is used |
| 283 | ''' |
| 284 | try: |
| 285 | self.handle.sendline("") |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 286 | self.handle.sendline("sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " -Z " + user + " &") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 287 | self.handle.sendline("") |
| 288 | self.handle.sendline("") |
| 289 | i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"\$"],timeout=10) |
| 290 | main.log.warn(self.handle.before + self.handle.after) |
| 291 | if i == 0: |
| 292 | main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf) |
| 293 | return main.FALSE |
| 294 | elif i == 1: |
| 295 | main.log.info(self.name + ": tcpdump started on " + intf) |
| 296 | return main.TRUE |
| 297 | elif i == 2: |
| 298 | main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf) |
| 299 | return main.FALSE |
| 300 | elif i ==3: |
| 301 | main.log.info(self.name +": " + self.handle.before) |
| 302 | return main.TRUE |
| 303 | else: |
| 304 | main.log.error(self.name + ": tcpdump - unexpected response") |
| 305 | return main.FALSE |
| 306 | except pexpect.EOF: |
| 307 | main.log.error(self.name + ": EOF exception found") |
| 308 | main.log.error(self.name + ": " + self.handle.before) |
| 309 | main.cleanup() |
| 310 | main.exit() |
| 311 | except: |
| 312 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 313 | main.log.error( traceback.print_exc() ) |
| 314 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 315 | main.cleanup() |
| 316 | main.exit() |
| 317 | |
| 318 | def stop_tcpdump(self): |
| 319 | "pkills tcpdump" |
| 320 | try: |
| 321 | self.handle.sendline("sudo pkill tcpdump") |
| 322 | self.handle.sendline("") |
| 323 | self.handle.sendline("") |
| 324 | self.handle.expect("\$") |
| 325 | except pexpect.EOF: |
| 326 | main.log.error(self.name + ": EOF exception found") |
| 327 | main.log.error(self.name + ": " + self.handle.before) |
| 328 | main.cleanup() |
| 329 | main.exit() |
| 330 | except: |
| 331 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 332 | main.log.error( traceback.print_exc() ) |
| 333 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 334 | main.cleanup() |
| 335 | main.exit() |
| 336 | |
shahshreya | 4d79aab | 2014-11-07 15:20:41 -0800 | [diff] [blame] | 337 | def run_optical_mn_script(self): |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 338 | ''' |
| 339 | This function is only meant for Packet Optical. |
| 340 | It runs the python script "optical.py" to create the packet layer(mn) |
| 341 | topology |
| 342 | ''' |
| 343 | try: |
| 344 | self.handle.sendline("") |
| 345 | self.handle.expect("\$") |
| 346 | self.handle.sendline("cd ~") |
| 347 | self.handle.expect("\$") |
| 348 | self.handle.sendline("sudo python optical.py") |
| 349 | self.handle.expect(">") |
| 350 | return main.TRUE |
| 351 | except pexpect.EOF: |
| 352 | main.log.error(self.name + ": EOF exception found") |
| 353 | main.log.error(self.name + ": " + self.handle.before) |
| 354 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 355 | |
| 356 | def disconnect(self): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 357 | ''' |
| 358 | Called at the end of the test to disconnect the handle. |
| 359 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 360 | response = '' |
| 361 | #print "Disconnecting Mininet" |
| 362 | if self.handle: |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 363 | self.handle.sendline("exit") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 364 | self.handle.expect("exit") |
| 365 | self.handle.expect("(.*)") |
| 366 | response = self.handle.before |
| 367 | |
| 368 | else : |
| 369 | main.log.error("Connection failed to the host") |
| 370 | response = main.FALSE |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 371 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 372 | |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 373 | def get_flowTable(self, protoVersion, sw): |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 374 | #TODO document usage |
| 375 | #FIXME: clean up print statements and such |
santhosh | 9da9389 | 2014-07-28 14:11:19 -0700 | [diff] [blame] | 376 | self.handle.sendline("cd") |
Jon Hall | 2ef1e9e | 2014-11-18 14:27:05 -0500 | [diff] [blame] | 377 | self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 378 | #TODO: Write seperate versions of the function for this, possibly a string that tells it which switch is in use? |
| 379 | #For 1.0 version of OVS |
| 380 | #command = "sudo ovs-ofctl dump-flows " + sw + " | awk '{OFS=\",\" ; print $1 $6 $7 }' |sort -n -k1" |
| 381 | #for 1.3 version of OVS |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 382 | #command = "sudo ovs-ofctl dump-flows " + sw + " | awk '{OFS=\",\" ; print $1 $3 $7 $8}' |sort -n -k1" |
| 383 | #NOTE: Use format to force consistent flow table output across versions |
| 384 | if protoVersion==1.0: |
| 385 | command = "sudo ovs-ofctl dump-flows " + sw + " -F OpenFlow10-table_id | awk '{OFS=\",\" ; print $1 $3 $6 $7 $8}' |sort -n -k1" |
| 386 | self.handle.sendline(command) |
| 387 | self.handle.expect(["sort -n -k1",pexpect.EOF,pexpect.TIMEOUT]) |
| 388 | self.handle.expect(["OFPST_FLOW",pexpect.EOF,pexpect.TIMEOUT]) |
| 389 | response = self.handle.before |
| 390 | #print "response=", response |
| 391 | return response |
| 392 | elif protoVersion==1.3: |
| 393 | command = "sudo ovs-ofctl dump-flows " + sw + " -O OpenFlow13 | awk '{OFS=\",\" ; print $1 $3 $6 $7}' |sort -n -k1" |
| 394 | self.handle.sendline(command) |
| 395 | #print "ovs-vsctl Command sent status." |
| 396 | #self.handle.expect(["sort -n -k1",pexpect.EOF,pexpect.TIMEOUT]) |
| 397 | #print "sort return status: " |
| 398 | #print self.handle.expect(["sort -n -k1",pexpect.EOF,pexpect.TIMEOUT]) |
| 399 | #print self.handle.before |
| 400 | self.handle.expect(["OFPST_FLOW",pexpect.EOF,pexpect.TIMEOUT]) |
| 401 | #print "OFPST_FLOW expected status: " |
| 402 | #print self.handle.expect(["OFPST_FLOW",pexpect.EOF,pexpect.TIMEOUT]) |
| 403 | #print self.handle.before |
| 404 | response = self.handle.before |
| 405 | #print "response=", response |
| 406 | return response |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 407 | |
| 408 | |
| 409 | def flow_comp(self,flow1,flow2): |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 410 | #print "Inside flow compare function" |
| 411 | #print "Flow1 Table:" |
| 412 | #for flow in flow1: |
| 413 | #print flow1 |
| 414 | #print "Flow2 Table" |
| 415 | #for flow in flow2: |
| 416 | #print flow2 |
| 417 | |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 418 | if flow1==flow2: |
| 419 | return main.TRUE |
| 420 | else: |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 421 | main.log.info("Flow tables do not match, printing tables:") |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 422 | #main.log.info("Flow Table 1:") |
| 423 | #main.log.info(flow1) |
| 424 | #main.log.info("Flow Table 2:") |
| 425 | #main.log.info(flow2) |
santhosh | 19fd803 | 2014-07-29 11:56:17 -0700 | [diff] [blame] | 426 | return main.FALSE |
| 427 | |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 428 | def setIpTablesOUTPUT(self, dst_ip, dst_port, action='add', packet_type='tcp',rule='DROP'): |
| 429 | ''' |
| 430 | Description: |
| 431 | add or remove iptables rule to DROP (default) packets from specific IP and PORT |
| 432 | Usage: |
| 433 | * specify action ('add' or 'remove') |
| 434 | when removing, pass in the same argument as you would add. It will |
| 435 | delete that specific rule. |
| 436 | * specify the destination ip to block with dst_ip |
| 437 | * specify destination port to block to dst_port |
| 438 | * optional packet type to block (default tcp) |
| 439 | * optional iptables rule (default DROP) |
| 440 | WARNING: |
| 441 | * This function uses root privilege iptables command which may result in |
| 442 | unwanted network errors. USE WITH CAUTION |
| 443 | ''' |
| 444 | import re |
| 445 | import time |
| 446 | |
| 447 | #NOTE********* |
| 448 | # The strict checking methods of this driver function is intentional |
| 449 | # to discourage any misuse or error of iptables, which can cause |
| 450 | # severe network errors |
| 451 | #************* |
| 452 | |
| 453 | #NOTE: Sleep needed to give some time for rule to be added and registered |
| 454 | # to the instance |
| 455 | time.sleep(5) |
| 456 | |
| 457 | action_type = action.lower() |
| 458 | if action_type != 'add' and action_type !='remove': |
| 459 | main.log.error("Invalid action type. 'add' or 'remove' table rule") |
| 460 | if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG': |
| 461 | #NOTE: Currently only supports rules DROP, ACCEPT, and LOG |
| 462 | main.log.error("Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only.") |
| 463 | return |
| 464 | return |
| 465 | else: |
| 466 | |
| 467 | #If there is no existing rule in the iptables, we will see an |
| 468 | #'iptables:'... message. We expect to see this message. |
| 469 | #Otherwise, if there IS an existing rule, we will get the prompt |
| 470 | # back, hence why we expect $ for remove type. We want to remove |
| 471 | # an already existing rule |
| 472 | |
| 473 | if action_type == 'add': |
| 474 | #NOTE: "iptables:" expect is a result of return from the command |
| 475 | # iptables -C ... |
| 476 | # Any changes by the iptables command return string |
| 477 | # will result in failure of the function. (deemed unlikely |
| 478 | # at the time of writing this function) |
| 479 | #Check for existing rules on current input |
| 480 | self.handle.sendline("") |
| 481 | self.handle.expect("\$") |
| 482 | self.handle.sendline("sudo iptables -C OUTPUT -p "+str(packet_type)+ |
| 483 | " -d "+ str(dst_ip)+" --dport "+str(dst_port)+" -j "+str(rule)) |
| 484 | i = self.handle.expect(["iptables:", "\$"]) |
| 485 | print i |
| 486 | print self.handle.before |
| 487 | print "after: " |
| 488 | print self.handle.after |
| 489 | |
| 490 | elif action_type == 'remove': |
| 491 | #Check for existing rules on current input |
| 492 | self.handle.sendline("") |
| 493 | self.handle.expect("\$") |
| 494 | self.handle.sendline("sudo iptables -C OUTPUT -p "+str(packet_type)+ |
| 495 | " -d "+ str(dst_ip)+" --dport "+str(dst_port)+" -j "+str(rule)) |
| 496 | self.handle.expect("\$") |
| 497 | print "before: " |
| 498 | print self.handle.before |
| 499 | actual_string = self.handle.after |
| 500 | expect_string = "iptables:" |
| 501 | print "Actual String:" |
| 502 | print actual_string |
| 503 | |
| 504 | if re.search(expect_string, actual_string): |
| 505 | match_result = main.TRUE |
| 506 | else: |
| 507 | match_result = main.FALSE |
| 508 | #If match_result is main.TRUE, it means there is no matching rule. |
| 509 | |
| 510 | #If tables does not exist and expected prompt is returned, go ahead and |
| 511 | #add iptables rule |
| 512 | if match_result == main.TRUE: |
| 513 | #Ensure action type is add |
| 514 | if action_type == 'add': |
| 515 | #-A is the 'append' action of iptables |
| 516 | action_add = '-A' |
| 517 | try: |
| 518 | self.handle.sendline("") |
| 519 | self.handle.sendline("sudo iptables "+action_add+" OUTPUT -p "+str(packet_type)+ |
| 520 | " -d "+ str(dst_ip)+" --dport "+str(dst_port)+" -j "+str(rule)) |
| 521 | |
| 522 | info_string = "Rules added to "+str(self.name) |
| 523 | info_string += "iptable rule added to block IP: "+str(dst_ip) |
| 524 | info_string += "Port: "+str(dst_port)+" Rule: "+str(rule) |
| 525 | |
| 526 | main.log.info(info_string) |
| 527 | |
| 528 | self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT]) |
| 529 | except pexpect.TIMEOUT: |
| 530 | main.log.error(self.name + ": Timeout exception in setIpTables function") |
| 531 | except: |
| 532 | main.log.error( traceback.print_exc()) |
| 533 | main.cleanup() |
| 534 | main.exit() |
| 535 | else: |
| 536 | main.log.error("Given rule already exists, but attempted to add it") |
| 537 | #If match_result is 0, it means there IS a matching rule provided |
| 538 | elif match_result == main.FALSE: |
| 539 | #Ensure action type is remove |
| 540 | if action_type == 'remove': |
| 541 | #-D is the 'delete' rule of iptables |
| 542 | action_remove = '-D' |
| 543 | try: |
| 544 | self.handle.sendline("") |
| 545 | #Delete a specific rule specified into the function |
| 546 | self.handle.sendline("sudo iptables "+action_remove+" OUTPUT -p "+str(packet_type)+ |
| 547 | " -d "+ str(dst_ip)+" --dport "+str(dst_port)+" -j "+str(rule)) |
| 548 | |
| 549 | info_string = "Rules removed from "+str(self.name) |
| 550 | info_string += " iptables rule removed from blocking IP: "+str(dst_ip) |
| 551 | info_string += " Port: "+str(dst_port)+" Rule: "+str(rule) |
| 552 | |
| 553 | main.log.info(info_string) |
| 554 | |
| 555 | self.handle.expect(["\$",pexpect.EOF,pexpect.TIMEOUT]) |
| 556 | except pexpect.TIMEOUT: |
| 557 | main.log.error(self.name + ": Timeout exception in setIpTables function") |
| 558 | except: |
| 559 | main.log.error( traceback.print_exc()) |
| 560 | main.cleanup() |
| 561 | main.exit() |
| 562 | else: |
| 563 | main.log.error("Given rule does not exist, but attempted to remove it") |
| 564 | else: |
| 565 | #NOTE: If a bad usage of this function occurs, exit the entire test |
| 566 | main.log.error("Bad rule given for iptables. Exiting...") |
| 567 | main.cleanup() |
| 568 | main.exit() |
| 569 | |
| 570 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 571 | if __name__ != "__main__": |
| 572 | import sys |
| 573 | sys.modules[__name__] = RemoteMininetDriver() |