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 | |
| 37 | class MininetCliDriver(Emulator): |
| 38 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 39 | MininetCliDriver is the basic driver which will handle the Mininet functions |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | ''' |
| 41 | def __init__(self): |
| 42 | super(Emulator, self).__init__() |
| 43 | self.handle = self |
| 44 | self.wrapped = sys.modules[__name__] |
| 45 | self.flag = 0 |
| 46 | |
| 47 | def connect(self, **connectargs): |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 48 | ''' |
| 49 | Here the main is the TestON instance after creating all the log handles. |
| 50 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 51 | for key in connectargs: |
| 52 | vars(self)[key] = connectargs[key] |
| 53 | |
| 54 | self.name = self.options['name'] |
| 55 | self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd) |
| 56 | |
| 57 | self.ssh_handle = self.handle |
| 58 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 59 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 60 | main.log.info(self.name+": Clearing any residual state or processes") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | self.handle.sendline("sudo mn -c") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 62 | i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 63 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 64 | main.log.info(self.name+": Sending sudo password") |
admin | f939f8b | 2014-04-03 17:22:56 -0700 | [diff] [blame] | 65 | self.handle.sendline(self.pwd) |
| 66 | i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 67 | if i==1: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 68 | main.log.info(self.name+": Clean") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 70 | main.log.error(self.name+": Connection terminated") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 71 | elif i==3: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 72 | main.log.error(self.name+": Something while cleaning MN took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 74 | main.log.info(self.name+": building fresh mininet") |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame] | 75 | #### for reactive/PARP enabled tests |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 76 | cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame] | 77 | #### for proactive flow with static ARP entries |
| 78 | #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller'] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | self.handle.sendline(cmdString) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 80 | self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | while 1: |
| 82 | i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300) |
| 83 | if i==0: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 84 | main.log.info(self.name+": mininet built") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 85 | return main.TRUE |
| 86 | if i==1: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 87 | self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT]) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 88 | main.log.info(self.handle.before) |
| 89 | elif i==2: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 90 | main.log.error(self.name+": Launching mininet failed...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | return main.FALSE |
| 92 | elif i==3: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 93 | main.log.error(self.name+": Connection timeout") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 94 | return main.FALSE |
| 95 | elif i==4: #timeout |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 96 | main.log.error(self.name+": Something took too long... " ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 97 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 98 | #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "): |
| 99 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 100 | else:#if no handle |
| 101 | main.log.error(self.name+": Connection failed to the host "+self.user_name+"@"+self.ip_address) |
| 102 | main.log.error(self.name+": Failed to connect to the Mininet") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 103 | return main.FALSE |
| 104 | |
| 105 | def pingall(self): |
| 106 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 107 | Verifies the reachability of the hosts using pingall command. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | ''' |
| 109 | if self.handle : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 110 | main.log.info(self.name+": Checking reachabilty to the hosts using pingall") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 111 | try: |
Jon Hall | efa4fa1 | 2014-04-14 11:40:21 -0700 | [diff] [blame] | 112 | response = self.execute(cmd="pingall",prompt="mininet>",timeout=120) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 113 | except pexpect.EOF: |
| 114 | main.log.error(self.name + ": EOF exception found") |
| 115 | main.log.error(self.name + ": " + self.handle.before) |
| 116 | main.cleanup() |
| 117 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 118 | pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$' |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 119 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"): |
| 120 | if re.search(pattern,response): |
| 121 | main.log.info(self.name+": All hosts are reachable") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 122 | return main.TRUE |
| 123 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 124 | main.log.error(self.name+": Unable to reach all the hosts") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 125 | return main.FALSE |
| 126 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 127 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 128 | return main.FALSE |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 129 | |
| 130 | def fpingHost(self,**pingParams): |
| 131 | ''' |
| 132 | Uses the fping package for faster pinging... |
| 133 | *requires fping to be installed on machine running mininet |
| 134 | ''' |
| 135 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 136 | command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"] |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 137 | self.handle.sendline(command) |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 138 | self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT]) |
| 139 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 140 | response = self.handle.before |
| 141 | if re.search(":\s-" ,response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 142 | main.log.info(self.name+": Ping fail") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 143 | return main.FALSE |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 144 | elif re.search(":\s\d{1,2}\.\d\d", response): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 145 | main.log.info(self.name+": Ping good!") |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 146 | return main.TRUE |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 147 | main.log.info(self.name+": Install fping on mininet machine... ") |
| 148 | main.log.info(self.name+": \n---\n"+response) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 149 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | |
| 151 | def pingHost(self,**pingParams): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 152 | ''' |
| 153 | Ping from one mininet host to another |
| 154 | Currently the only supported Params: SRC and TARGET |
| 155 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | args = utilities.parse_args(["SRC","TARGET"],**pingParams) |
| 157 | #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"] |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 158 | command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 159 | try: |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 160 | main.log.warn("Sending: " + command) |
| 161 | #response = self.execute(cmd=command,prompt="mininet",timeout=10 ) |
| 162 | self.handle.sendline(command) |
| 163 | i = self.handle.expect([command,pexpect.TIMEOUT]) |
| 164 | if i == 1: |
| 165 | main.log.error(self.name + ": timeout when waiting for response from mininet") |
| 166 | main.log.error("response: " + str(self.handle.before)) |
| 167 | i = self.handle.expect(["mininet>",pexpect.TIMEOUT]) |
| 168 | if i == 1: |
| 169 | main.log.error(self.name + ": timeout when waiting for response from mininet") |
| 170 | main.log.error("response: " + str(self.handle.before)) |
| 171 | response = self.handle.before |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 172 | except pexpect.EOF: |
| 173 | main.log.error(self.name + ": EOF exception found") |
| 174 | main.log.error(self.name + ": " + self.handle.before) |
| 175 | main.cleanup() |
| 176 | main.exit() |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 177 | main.log.info(self.name+": Ping Response: "+ response ) |
| 178 | #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"): |
| 179 | if re.search(',\s0\%\spacket\sloss',response): |
Jon Hall | 6e18c7b | 2014-04-23 16:26:33 -0700 | [diff] [blame] | 180 | main.log.info(self.name+": no packets lost, host is reachable") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | main.last_result = main.TRUE |
| 182 | return main.TRUE |
| 183 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 184 | main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 185 | main.last_result = main.FALSE |
| 186 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 187 | |
| 188 | def checkIP(self,host): |
| 189 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 190 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 191 | ''' |
Jon Hall | bd795bf | 2014-06-18 09:46:32 -0700 | [diff] [blame] | 192 | self.handle.sendline("") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 193 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 194 | try: |
| 195 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 196 | except pexpect.EOF: |
| 197 | main.log.error(self.name + ": EOF exception found") |
| 198 | main.log.error(self.name + ": " + self.handle.before) |
| 199 | main.cleanup() |
| 200 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | |
| 202 | pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 203 | #pattern = "inet addr:10.0.0.6" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 204 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") : |
| 205 | if re.search(pattern,response): |
| 206 | main.log.info(self.name+": Host Ip configured properly") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 207 | return main.TRUE |
| 208 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 209 | main.log.error(self.name+": Host IP not found") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | return main.FALSE |
| 211 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 212 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | |
| 214 | def verifySSH(self,**connectargs): |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 215 | try: |
| 216 | response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 217 | response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 218 | for key in connectargs: |
| 219 | vars(self)[key] = connectargs[key] |
| 220 | response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10) |
| 221 | except pexpect.EOF: |
| 222 | main.log.error(self.name + ": EOF exception found") |
| 223 | main.log.error(self.name + ": " + self.handle.before) |
| 224 | main.cleanup() |
| 225 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 226 | import time |
| 227 | time.sleep(20) |
| 228 | if self.flag == 0: |
| 229 | self.flag = 1 |
| 230 | return main.FALSE |
| 231 | else : |
| 232 | return main.TRUE |
| 233 | |
| 234 | def getMacAddress(self,host): |
| 235 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 236 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 237 | ''' |
| 238 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 239 | try: |
| 240 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 241 | except pexpect.EOF: |
| 242 | main.log.error(self.name + ": EOF exception found") |
| 243 | main.log.error(self.name + ": " + self.handle.before) |
| 244 | main.cleanup() |
| 245 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 246 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 247 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 248 | mac_address_search = re.search(pattern, response, re.I) |
| 249 | mac_address = mac_address_search.group().split(" ")[1] |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 250 | main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address) |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 251 | return mac_address |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 252 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 253 | main.log.error(self.name+": Connection failed to the host") |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 254 | |
| 255 | def getInterfaceMACAddress(self,host, interface): |
| 256 | ''' |
| 257 | Return the IP address of the interface on the given host |
| 258 | ''' |
| 259 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 260 | try: |
| 261 | response = self.execute(cmd=host+" ifconfig " + interface, |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 262 | prompt="mininet>",timeout=10) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 263 | except pexpect.EOF: |
| 264 | main.log.error(self.name + ": EOF exception found") |
| 265 | main.log.error(self.name + ": " + self.handle.before) |
| 266 | main.cleanup() |
| 267 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 268 | |
| 269 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 270 | mac_address_search = re.search(pattern, response, re.I) |
| 271 | if mac_address_search is None: |
| 272 | main.log.info("No mac address found in %s" % response) |
| 273 | return main.FALSE |
| 274 | mac_address = mac_address_search.group().split(" ")[1] |
| 275 | main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address) |
| 276 | return mac_address |
| 277 | else: |
| 278 | main.log.error("Connection failed to the host") |
| 279 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 280 | def getIPAddress(self,host): |
| 281 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 282 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 283 | ''' |
| 284 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 285 | try: |
| 286 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 287 | except pexpect.EOF: |
| 288 | main.log.error(self.name + ": EOF exception found") |
| 289 | main.log.error(self.name + ": " + self.handle.before) |
| 290 | main.cleanup() |
| 291 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 292 | |
| 293 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 294 | ip_address_search = re.search(pattern, response) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 295 | main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1)) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 296 | return ip_address_search.group(1) |
| 297 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 298 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 299 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 300 | def getSwitchDPID(self,switch): |
| 301 | ''' |
| 302 | return the datapath ID of the switch |
| 303 | ''' |
| 304 | if self.handle : |
| 305 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 306 | try: |
| 307 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 308 | except pexpect.EOF: |
| 309 | main.log.error(self.name + ": EOF exception found") |
| 310 | main.log.error(self.name + ": " + self.handle.before) |
| 311 | main.cleanup() |
| 312 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 313 | pattern = r'^(?P<dpid>\d)+' |
| 314 | result = re.search(pattern, response, re.MULTILINE) |
| 315 | if result is None: |
| 316 | main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response)) |
| 317 | return main.FALSE |
| 318 | return int(result.group('dpid')) |
| 319 | else: |
| 320 | main.log.error("Connection failed to the host") |
| 321 | |
| 322 | def getInterfaces(self, node): |
| 323 | ''' |
| 324 | return information dict about interfaces connected to the node |
| 325 | ''' |
| 326 | if self.handle : |
| 327 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())' |
| 328 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 329 | try: |
| 330 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 331 | except pexpect.EOF: |
| 332 | main.log.error(self.name + ": EOF exception found") |
| 333 | main.log.error(self.name + ": " + self.handle.before) |
| 334 | main.cleanup() |
| 335 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 336 | return response |
| 337 | else: |
| 338 | main.log.error("Connection failed to the node") |
| 339 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 340 | def dump(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 341 | main.log.info(self.name+": Dump node info") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 342 | try: |
| 343 | response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10) |
| 344 | except pexpect.EOF: |
| 345 | main.log.error(self.name + ": EOF exception found") |
| 346 | main.log.error(self.name + ": " + self.handle.before) |
| 347 | main.cleanup() |
| 348 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 349 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 350 | |
| 351 | def intfs(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 352 | main.log.info(self.name+": List interfaces") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 353 | try: |
| 354 | response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10) |
| 355 | except pexpect.EOF: |
| 356 | main.log.error(self.name + ": EOF exception found") |
| 357 | main.log.error(self.name + ": " + self.handle.before) |
| 358 | main.cleanup() |
| 359 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 360 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 361 | |
| 362 | def net(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 363 | main.log.info(self.name+": List network connections") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 364 | try: |
| 365 | response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10) |
| 366 | except pexpect.EOF: |
| 367 | main.log.error(self.name + ": EOF exception found") |
| 368 | main.log.error(self.name + ": " + self.handle.before) |
| 369 | main.cleanup() |
| 370 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 371 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 372 | |
| 373 | def iperf(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 374 | main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 375 | try: |
| 376 | response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10) |
| 377 | except pexpect.EOF: |
| 378 | main.log.error(self.name + ": EOF exception found") |
| 379 | main.log.error(self.name + ": " + self.handle.before) |
| 380 | main.cleanup() |
| 381 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 382 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 383 | |
| 384 | def iperfudp(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 385 | main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 386 | try: |
| 387 | response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10) |
| 388 | except pexpect.EOF: |
| 389 | main.log.error(self.name + ": EOF exception found") |
| 390 | main.log.error(self.name + ": " + self.handle.before) |
| 391 | main.cleanup() |
| 392 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 393 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 394 | |
| 395 | def nodes(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 396 | main.log.info(self.name+": List all nodes.") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 397 | try: |
| 398 | response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10) |
| 399 | except pexpect.EOF: |
| 400 | main.log.error(self.name + ": EOF exception found") |
| 401 | main.log.error(self.name + ": " + self.handle.before) |
| 402 | main.cleanup() |
| 403 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 404 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 405 | |
| 406 | def pingpair(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 407 | main.log.info(self.name+": Ping between first two hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 408 | try: |
| 409 | response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20) |
| 410 | except pexpect.EOF: |
| 411 | main.log.error(self.name + ": EOF exception found") |
| 412 | main.log.error(self.name + ": " + self.handle.before) |
| 413 | main.cleanup() |
| 414 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 415 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 416 | #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"): |
| 417 | if re.search(',\s0\%\spacket\sloss',response): |
| 418 | main.log.info(self.name+": Ping between two hosts SUCCESSFUL") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 419 | main.last_result = main.TRUE |
| 420 | return main.TRUE |
| 421 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 422 | main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 423 | main.last_result = main.FALSE |
| 424 | return main.FALSE |
| 425 | |
| 426 | def link(self,**linkargs): |
| 427 | ''' |
| 428 | Bring link(s) between two nodes up or down |
| 429 | ''' |
| 430 | main.log.info('Bring link(s) between two nodes up or down') |
| 431 | args = utilities.parse_args(["END1","END2","OPTION"],**linkargs) |
| 432 | end1 = args["END1"] if args["END1"] != None else "" |
| 433 | end2 = args["END2"] if args["END2"] != None else "" |
| 434 | option = args["OPTION"] if args["OPTION"] != None else "" |
| 435 | command = "link "+str(end1) + " " + str(end2)+ " " + str(option) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 436 | try: |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 437 | #response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 438 | self.handle.sendline(command) |
| 439 | self.handle.expect("mininet>") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 440 | except pexpect.EOF: |
| 441 | main.log.error(self.name + ": EOF exception found") |
| 442 | main.log.error(self.name + ": " + self.handle.before) |
| 443 | main.cleanup() |
| 444 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 445 | return main.TRUE |
| 446 | |
| 447 | |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 448 | def yank(self,**yankargs): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 449 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 450 | yank a mininet switch interface to a host |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 451 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 452 | main.log.info('Yank the switch interface attached to a host') |
| 453 | args = utilities.parse_args(["SW","INTF"],**yankargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 454 | sw = args["SW"] if args["SW"] !=None else "" |
| 455 | intf = args["INTF"] if args["INTF"] != None else "" |
| 456 | command = "py "+ str(sw) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 457 | try: |
| 458 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 459 | except pexpect.EOF: |
| 460 | main.log.error(self.name + ": EOF exception found") |
| 461 | main.log.error(self.name + ": " + self.handle.before) |
| 462 | main.cleanup() |
| 463 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 464 | return main.TRUE |
| 465 | |
| 466 | def plug(self, **plugargs): |
| 467 | ''' |
| 468 | plug the yanked mininet switch interface to a switch |
| 469 | ''' |
| 470 | main.log.info('Plug the switch interface attached to a switch') |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 471 | args = utilities.parse_args(["SW","INTF"],**plugargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 472 | sw = args["SW"] if args["SW"] !=None else "" |
| 473 | intf = args["INTF"] if args["INTF"] != None else "" |
| 474 | command = "py "+ str(sw) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 475 | try: |
| 476 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 477 | except pexpect.EOF: |
| 478 | main.log.error(self.name + ": EOF exception found") |
| 479 | main.log.error(self.name + ": " + self.handle.before) |
| 480 | main.cleanup() |
| 481 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 482 | return main.TRUE |
| 483 | |
| 484 | |
| 485 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 486 | def dpctl(self,**dpctlargs): |
| 487 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 488 | Run dpctl command on all switches. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 489 | ''' |
| 490 | main.log.info('Run dpctl command on all switches') |
| 491 | args = utilities.parse_args(["CMD","ARGS"],**dpctlargs) |
| 492 | cmd = args["CMD"] if args["CMD"] != None else "" |
| 493 | cmdargs = args["ARGS"] if args["ARGS"] != None else "" |
| 494 | command = "dpctl "+cmd + " " + str(cmdargs) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 495 | try: |
| 496 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 497 | except pexpect.EOF: |
| 498 | main.log.error(self.name + ": EOF exception found") |
| 499 | main.log.error(self.name + ": " + self.handle.before) |
| 500 | main.cleanup() |
| 501 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 502 | return main.TRUE |
| 503 | |
| 504 | |
| 505 | def get_version(self): |
| 506 | file_input = path+'/lib/Mininet/INSTALL' |
| 507 | version = super(Mininet, self).get_version() |
| 508 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
| 509 | for line in open(file_input,'r').readlines(): |
| 510 | result = re.match(pattern, line) |
| 511 | if result: |
| 512 | version = result.group(0) |
| 513 | return version |
| 514 | |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 515 | def get_sw_controller_sanity(self, sw): |
| 516 | command = "sh ovs-vsctl get-controller "+str(sw) |
| 517 | try: |
| 518 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 519 | if response: |
| 520 | return main.TRUE |
| 521 | else: |
| 522 | return main.FALSE |
| 523 | except pexpect.EOF: |
| 524 | main.log.error(self.name + ": EOF exception found") |
| 525 | main.log.error(self.name + ": " + self.handle.before) |
| 526 | main.cleanup() |
| 527 | main.exit() |
| 528 | else: |
| 529 | main.log.info(response) |
| 530 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 531 | def get_sw_controller(self,sw): |
| 532 | command = "sh ovs-vsctl get-controller "+str(sw) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 533 | try: |
Jon Hall | bd795bf | 2014-06-18 09:46:32 -0700 | [diff] [blame] | 534 | self.handle.expect("mininet") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 535 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 536 | print(response) |
| 537 | if response: |
admin | dc1c507 | 2014-06-24 15:57:19 -0700 | [diff] [blame] | 538 | print("**********************") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 539 | return response |
| 540 | else: |
| 541 | return main.FALSE |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 542 | except pexpect.EOF: |
| 543 | main.log.error(self.name + ": EOF exception found") |
| 544 | main.log.error(self.name + ": " + self.handle.before) |
| 545 | main.cleanup() |
| 546 | main.exit() |
| 547 | else: |
| 548 | main.log.info(response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 549 | |
| 550 | def assign_sw_controller(self,**kwargs): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 551 | ''' |
| 552 | count is only needed if there is more than 1 controller |
| 553 | ''' |
| 554 | args = utilities.parse_args(["COUNT"],**kwargs) |
| 555 | count = args["COUNT"] if args!={} else 1 |
| 556 | |
| 557 | argstring = "SW" |
| 558 | for j in range(count): |
| 559 | argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1) |
| 560 | args = utilities.parse_args(argstring.split(","),**kwargs) |
| 561 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 562 | sw = args["SW"] if args["SW"] != None else "" |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 563 | ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 564 | ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else "" |
| 565 | |
| 566 | command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " " |
| 567 | for j in range(count): |
| 568 | i=j+1 |
| 569 | args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs) |
| 570 | ip = args["IP"+str(i)] if args["IP"+str(i)] != None else "" |
| 571 | port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else "" |
| 572 | tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else "" |
| 573 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 574 | try: |
| 575 | self.execute(cmd=command,prompt="mininet>",timeout=5) |
| 576 | except pexpect.EOF: |
| 577 | main.log.error(self.name + ": EOF exception found") |
| 578 | main.log.error(self.name + ": " + self.handle.before) |
| 579 | main.cleanup() |
| 580 | main.exit() |
| 581 | except: |
| 582 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 583 | main.log.error( traceback.print_exc() ) |
| 584 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 585 | main.cleanup() |
| 586 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 587 | |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 588 | def delete_sw_controller(self,sw): |
| 589 | ''' |
| 590 | Removes the controller target from sw |
| 591 | ''' |
| 592 | |
| 593 | command = "sh ovs-vsctl del-controller "+str(sw) |
| 594 | try: |
| 595 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 596 | except pexpect.EOF: |
| 597 | main.log.error(self.name + ": EOF exception found") |
| 598 | main.log.error(self.name + ": " + self.handle.before) |
| 599 | main.cleanup() |
| 600 | main.exit() |
| 601 | else: |
| 602 | main.log.info(response) |
| 603 | |
| 604 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 605 | def disconnect(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 606 | main.log.info(self.name+": Disconnecting mininet...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 607 | response = '' |
| 608 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 609 | try: |
| 610 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 611 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 612 | self.handle.sendline("sudo mn -c") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 613 | except pexpect.EOF: |
| 614 | main.log.error(self.name + ": EOF exception found") |
| 615 | main.log.error(self.name + ": " + self.handle.before) |
| 616 | main.cleanup() |
| 617 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 618 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 619 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 620 | response = main.FALSE |
| 621 | return response |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 622 | |
| 623 | def arping(self, src, dest, destmac): |
| 624 | self.handle.sendline('') |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 625 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 626 | |
| 627 | self.handle.sendline(src + ' arping ' + dest) |
| 628 | try: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 629 | self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 630 | main.log.info(self.name+": ARP successful") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 631 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 632 | return main.TRUE |
| 633 | except: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 634 | main.log.warn(self.name+": ARP FAILURE") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 635 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 636 | return main.FALSE |
| 637 | |
| 638 | def decToHex(num): |
| 639 | return hex(num).split('x')[1] |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 640 | |
| 641 | def getSwitchFlowCount(self, switch): |
| 642 | ''' |
| 643 | return the Flow Count of the switch |
| 644 | ''' |
| 645 | if self.handle: |
| 646 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 647 | try: |
| 648 | response = self.execute(cmd=cmd, prompt="mininet>", timeout=10) |
| 649 | except pexpect.EOF: |
| 650 | main.log.error(self.name + ": EOF exception found") |
| 651 | main.log.error(self.name + " " + self.handle.before) |
| 652 | main.cleanup() |
| 653 | main.exit() |
| 654 | pattern = "flow_count=(\d+)" |
| 655 | result = re.search(pattern, response, re.MULTILINE) |
| 656 | if result is None: |
| 657 | print "no flow on switch print test" |
| 658 | main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response)) |
| 659 | return main.FALSE |
| 660 | return result.group(1) |
| 661 | else: |
| 662 | main.log.error("Connection failed to the Mininet host") |
| 663 | |
| 664 | def check_flows(self, sw): |
| 665 | command = "sh ovs-ofctl dump-flows "+str(sw) |
| 666 | try: |
| 667 | response=self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 668 | return response |
| 669 | except pexpect.EOF: |
| 670 | main.log.error(self.name + ": EOF exception found") |
| 671 | main.log.error(self.name + ": " + self.handle.before) |
| 672 | main.cleanup() |
| 673 | main.exit() |
| 674 | else: |
| 675 | main.log.info(response) |
| 676 | |
| 677 | def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"): |
| 678 | ''' |
| 679 | Runs tpdump on an intferface and saves the file |
| 680 | intf can be specified, or the default eth0 is used |
| 681 | ''' |
| 682 | try: |
| 683 | self.handle.sendline("") |
| 684 | self.handle.expect("mininet>") |
| 685 | self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &") |
| 686 | self.handle.sendline("") |
| 687 | self.handle.sendline("") |
| 688 | i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10) |
| 689 | main.log.warn(self.handle.before + self.handle.after) |
| 690 | if i == 0: |
| 691 | main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf) |
| 692 | return main.FALSE |
| 693 | elif i == 1: |
| 694 | main.log.info(self.name + ": tcpdump started on " + intf) |
| 695 | return main.TRUE |
| 696 | elif i == 2: |
| 697 | main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf) |
| 698 | return main.FALSE |
| 699 | elif i ==3: |
| 700 | main.log.info(self.name +": " + self.handle.before) |
| 701 | return main.TRUE |
| 702 | else: |
| 703 | main.log.error(self.name + ": tcpdump - unexpected response") |
| 704 | return main.FALSE |
| 705 | except pexpect.EOF: |
| 706 | main.log.error(self.name + ": EOF exception found") |
| 707 | main.log.error(self.name + ": " + self.handle.before) |
| 708 | main.cleanup() |
| 709 | main.exit() |
| 710 | except: |
| 711 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 712 | main.log.error( traceback.print_exc() ) |
| 713 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 714 | main.cleanup() |
| 715 | main.exit() |
| 716 | |
| 717 | def stop_tcpdump(self): |
| 718 | "pkills tcpdump" |
| 719 | try: |
| 720 | self.handle.sendline("sh sudo pkill tcpdump") |
| 721 | self.handle.sendline("") |
| 722 | self.handle.sendline("") |
| 723 | self.handle.expect("mininet>") |
| 724 | except pexpect.EOF: |
| 725 | main.log.error(self.name + ": EOF exception found") |
| 726 | main.log.error(self.name + ": " + self.handle.before) |
| 727 | main.cleanup() |
| 728 | main.exit() |
| 729 | except: |
| 730 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 731 | main.log.error( traceback.print_exc() ) |
| 732 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 733 | main.cleanup() |
| 734 | main.exit() |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 741 | |
| 742 | if __name__ != "__main__": |
| 743 | import sys |
| 744 | sys.modules[__name__] = MininetCliDriver() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 745 | |