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 | ''' |
| 192 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 193 | try: |
| 194 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 195 | except pexpect.EOF: |
| 196 | main.log.error(self.name + ": EOF exception found") |
| 197 | main.log.error(self.name + ": " + self.handle.before) |
| 198 | main.cleanup() |
| 199 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | |
| 201 | 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] | 202 | #pattern = "inet addr:10.0.0.6" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 203 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") : |
| 204 | if re.search(pattern,response): |
| 205 | main.log.info(self.name+": Host Ip configured properly") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 206 | return main.TRUE |
| 207 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 208 | main.log.error(self.name+": Host IP not found") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 209 | return main.FALSE |
| 210 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 211 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 212 | |
| 213 | def verifySSH(self,**connectargs): |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 214 | try: |
| 215 | response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 216 | response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 217 | for key in connectargs: |
| 218 | vars(self)[key] = connectargs[key] |
| 219 | response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10) |
| 220 | except pexpect.EOF: |
| 221 | main.log.error(self.name + ": EOF exception found") |
| 222 | main.log.error(self.name + ": " + self.handle.before) |
| 223 | main.cleanup() |
| 224 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | import time |
| 226 | time.sleep(20) |
| 227 | if self.flag == 0: |
| 228 | self.flag = 1 |
| 229 | return main.FALSE |
| 230 | else : |
| 231 | return main.TRUE |
| 232 | |
| 233 | def getMacAddress(self,host): |
| 234 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 235 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 236 | ''' |
| 237 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 238 | try: |
| 239 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 240 | except pexpect.EOF: |
| 241 | main.log.error(self.name + ": EOF exception found") |
| 242 | main.log.error(self.name + ": " + self.handle.before) |
| 243 | main.cleanup() |
| 244 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 245 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 246 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 247 | mac_address_search = re.search(pattern, response, re.I) |
| 248 | mac_address = mac_address_search.group().split(" ")[1] |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 249 | 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] | 250 | return mac_address |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 251 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 252 | main.log.error(self.name+": Connection failed to the host") |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 253 | |
| 254 | def getInterfaceMACAddress(self,host, interface): |
| 255 | ''' |
| 256 | Return the IP address of the interface on the given host |
| 257 | ''' |
| 258 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 259 | try: |
| 260 | response = self.execute(cmd=host+" ifconfig " + interface, |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 261 | prompt="mininet>",timeout=10) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 262 | except pexpect.EOF: |
| 263 | main.log.error(self.name + ": EOF exception found") |
| 264 | main.log.error(self.name + ": " + self.handle.before) |
| 265 | main.cleanup() |
| 266 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 267 | |
| 268 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 269 | mac_address_search = re.search(pattern, response, re.I) |
| 270 | if mac_address_search is None: |
| 271 | main.log.info("No mac address found in %s" % response) |
| 272 | return main.FALSE |
| 273 | mac_address = mac_address_search.group().split(" ")[1] |
| 274 | main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address) |
| 275 | return mac_address |
| 276 | else: |
| 277 | main.log.error("Connection failed to the host") |
| 278 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 279 | def getIPAddress(self,host): |
| 280 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 281 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 282 | ''' |
| 283 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 284 | try: |
| 285 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 286 | except pexpect.EOF: |
| 287 | main.log.error(self.name + ": EOF exception found") |
| 288 | main.log.error(self.name + ": " + self.handle.before) |
| 289 | main.cleanup() |
| 290 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 291 | |
| 292 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 293 | ip_address_search = re.search(pattern, response) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 294 | 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] | 295 | return ip_address_search.group(1) |
| 296 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 297 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 298 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 299 | def getSwitchDPID(self,switch): |
| 300 | ''' |
| 301 | return the datapath ID of the switch |
| 302 | ''' |
| 303 | if self.handle : |
| 304 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 305 | try: |
| 306 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 307 | except pexpect.EOF: |
| 308 | main.log.error(self.name + ": EOF exception found") |
| 309 | main.log.error(self.name + ": " + self.handle.before) |
| 310 | main.cleanup() |
| 311 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 312 | pattern = r'^(?P<dpid>\d)+' |
| 313 | result = re.search(pattern, response, re.MULTILINE) |
| 314 | if result is None: |
| 315 | main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response)) |
| 316 | return main.FALSE |
Jon Hall | c1a1d24 | 2014-07-21 16:03:33 -0700 | [diff] [blame] | 317 | return str(result.group(0)) |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 318 | else: |
| 319 | main.log.error("Connection failed to the host") |
| 320 | |
admin | 2580a0e | 2014-07-29 11:24:34 -0700 | [diff] [blame] | 321 | def getDPID(self, switch): |
| 322 | if self.handle: |
| 323 | self.handle.sendline("") |
| 324 | self.expect("mininet>") |
| 325 | cmd = "py %s.dpid" %switch |
| 326 | try: |
| 327 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 328 | self.handle.expect("mininet>") |
| 329 | response = self.handle.before |
| 330 | return response |
| 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() |
| 336 | |
| 337 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 338 | def getInterfaces(self, node): |
| 339 | ''' |
| 340 | return information dict about interfaces connected to the node |
| 341 | ''' |
| 342 | if self.handle : |
| 343 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())' |
| 344 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 345 | try: |
| 346 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 347 | except pexpect.EOF: |
| 348 | main.log.error(self.name + ": EOF exception found") |
| 349 | main.log.error(self.name + ": " + self.handle.before) |
| 350 | main.cleanup() |
| 351 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 352 | return response |
| 353 | else: |
| 354 | main.log.error("Connection failed to the node") |
| 355 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 356 | def dump(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 357 | main.log.info(self.name+": Dump node info") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 358 | try: |
| 359 | response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10) |
| 360 | except pexpect.EOF: |
| 361 | main.log.error(self.name + ": EOF exception found") |
| 362 | main.log.error(self.name + ": " + self.handle.before) |
| 363 | main.cleanup() |
| 364 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 365 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 366 | |
| 367 | def intfs(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 368 | main.log.info(self.name+": List interfaces") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 369 | try: |
| 370 | response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10) |
| 371 | except pexpect.EOF: |
| 372 | main.log.error(self.name + ": EOF exception found") |
| 373 | main.log.error(self.name + ": " + self.handle.before) |
| 374 | main.cleanup() |
| 375 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 376 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 377 | |
| 378 | def net(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 379 | main.log.info(self.name+": List network connections") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 380 | try: |
| 381 | response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10) |
| 382 | except pexpect.EOF: |
| 383 | main.log.error(self.name + ": EOF exception found") |
| 384 | main.log.error(self.name + ": " + self.handle.before) |
| 385 | main.cleanup() |
| 386 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 387 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 388 | |
| 389 | def iperf(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 390 | 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] | 391 | try: |
| 392 | response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10) |
| 393 | except pexpect.EOF: |
| 394 | main.log.error(self.name + ": EOF exception found") |
| 395 | main.log.error(self.name + ": " + self.handle.before) |
| 396 | main.cleanup() |
| 397 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 398 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 399 | |
| 400 | def iperfudp(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 401 | 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] | 402 | try: |
| 403 | response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10) |
| 404 | except pexpect.EOF: |
| 405 | main.log.error(self.name + ": EOF exception found") |
| 406 | main.log.error(self.name + ": " + self.handle.before) |
| 407 | main.cleanup() |
| 408 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 409 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 410 | |
| 411 | def nodes(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 412 | main.log.info(self.name+": List all nodes.") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 413 | try: |
| 414 | response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10) |
| 415 | except pexpect.EOF: |
| 416 | main.log.error(self.name + ": EOF exception found") |
| 417 | main.log.error(self.name + ": " + self.handle.before) |
| 418 | main.cleanup() |
| 419 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 420 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 421 | |
| 422 | def pingpair(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 423 | main.log.info(self.name+": Ping between first two hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 424 | try: |
| 425 | response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20) |
| 426 | except pexpect.EOF: |
| 427 | main.log.error(self.name + ": EOF exception found") |
| 428 | main.log.error(self.name + ": " + self.handle.before) |
| 429 | main.cleanup() |
| 430 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 431 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 432 | #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"): |
| 433 | if re.search(',\s0\%\spacket\sloss',response): |
| 434 | main.log.info(self.name+": Ping between two hosts SUCCESSFUL") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 435 | main.last_result = main.TRUE |
| 436 | return main.TRUE |
| 437 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 438 | main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 439 | main.last_result = main.FALSE |
| 440 | return main.FALSE |
| 441 | |
| 442 | def link(self,**linkargs): |
| 443 | ''' |
| 444 | Bring link(s) between two nodes up or down |
| 445 | ''' |
| 446 | main.log.info('Bring link(s) between two nodes up or down') |
| 447 | args = utilities.parse_args(["END1","END2","OPTION"],**linkargs) |
| 448 | end1 = args["END1"] if args["END1"] != None else "" |
| 449 | end2 = args["END2"] if args["END2"] != None else "" |
| 450 | option = args["OPTION"] if args["OPTION"] != None else "" |
| 451 | command = "link "+str(end1) + " " + str(end2)+ " " + str(option) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 452 | try: |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 453 | #response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 454 | self.handle.sendline(command) |
| 455 | self.handle.expect("mininet>") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 456 | except pexpect.EOF: |
| 457 | main.log.error(self.name + ": EOF exception found") |
| 458 | main.log.error(self.name + ": " + self.handle.before) |
| 459 | main.cleanup() |
| 460 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 461 | return main.TRUE |
| 462 | |
| 463 | |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 464 | def yank(self,**yankargs): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 465 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 466 | yank a mininet switch interface to a host |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 467 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 468 | main.log.info('Yank the switch interface attached to a host') |
| 469 | args = utilities.parse_args(["SW","INTF"],**yankargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 470 | sw = args["SW"] if args["SW"] !=None else "" |
| 471 | intf = args["INTF"] if args["INTF"] != None else "" |
| 472 | command = "py "+ str(sw) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 473 | try: |
| 474 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 475 | except pexpect.EOF: |
| 476 | main.log.error(self.name + ": EOF exception found") |
| 477 | main.log.error(self.name + ": " + self.handle.before) |
| 478 | main.cleanup() |
| 479 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 480 | return main.TRUE |
| 481 | |
| 482 | def plug(self, **plugargs): |
| 483 | ''' |
| 484 | plug the yanked mininet switch interface to a switch |
| 485 | ''' |
| 486 | main.log.info('Plug the switch interface attached to a switch') |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 487 | args = utilities.parse_args(["SW","INTF"],**plugargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 488 | sw = args["SW"] if args["SW"] !=None else "" |
| 489 | intf = args["INTF"] if args["INTF"] != None else "" |
| 490 | command = "py "+ str(sw) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 491 | try: |
| 492 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 493 | except pexpect.EOF: |
| 494 | main.log.error(self.name + ": EOF exception found") |
| 495 | main.log.error(self.name + ": " + self.handle.before) |
| 496 | main.cleanup() |
| 497 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 498 | return main.TRUE |
| 499 | |
| 500 | |
| 501 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 502 | def dpctl(self,**dpctlargs): |
| 503 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 504 | Run dpctl command on all switches. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 505 | ''' |
| 506 | main.log.info('Run dpctl command on all switches') |
| 507 | args = utilities.parse_args(["CMD","ARGS"],**dpctlargs) |
| 508 | cmd = args["CMD"] if args["CMD"] != None else "" |
| 509 | cmdargs = args["ARGS"] if args["ARGS"] != None else "" |
| 510 | command = "dpctl "+cmd + " " + str(cmdargs) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 511 | try: |
| 512 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 513 | except pexpect.EOF: |
| 514 | main.log.error(self.name + ": EOF exception found") |
| 515 | main.log.error(self.name + ": " + self.handle.before) |
| 516 | main.cleanup() |
| 517 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 518 | return main.TRUE |
| 519 | |
| 520 | |
| 521 | def get_version(self): |
| 522 | file_input = path+'/lib/Mininet/INSTALL' |
| 523 | version = super(Mininet, self).get_version() |
| 524 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
| 525 | for line in open(file_input,'r').readlines(): |
| 526 | result = re.match(pattern, line) |
| 527 | if result: |
| 528 | version = result.group(0) |
| 529 | return version |
| 530 | |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 531 | def get_sw_controller_sanity(self, sw): |
| 532 | command = "sh ovs-vsctl get-controller "+str(sw) |
| 533 | try: |
| 534 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 535 | if response: |
| 536 | return main.TRUE |
| 537 | else: |
| 538 | return main.FALSE |
| 539 | except pexpect.EOF: |
| 540 | main.log.error(self.name + ": EOF exception found") |
| 541 | main.log.error(self.name + ": " + self.handle.before) |
| 542 | main.cleanup() |
| 543 | main.exit() |
| 544 | else: |
| 545 | main.log.info(response) |
| 546 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 547 | def get_sw_controller(self,sw): |
| 548 | command = "sh ovs-vsctl get-controller "+str(sw) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 549 | try: |
| 550 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 551 | print(response) |
| 552 | if response: |
admin | dc1c507 | 2014-06-24 15:57:19 -0700 | [diff] [blame] | 553 | print("**********************") |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 554 | return response |
| 555 | else: |
| 556 | return main.FALSE |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 557 | except pexpect.EOF: |
| 558 | main.log.error(self.name + ": EOF exception found") |
| 559 | main.log.error(self.name + ": " + self.handle.before) |
| 560 | main.cleanup() |
| 561 | main.exit() |
| 562 | else: |
| 563 | main.log.info(response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 564 | |
| 565 | def assign_sw_controller(self,**kwargs): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 566 | ''' |
| 567 | count is only needed if there is more than 1 controller |
| 568 | ''' |
| 569 | args = utilities.parse_args(["COUNT"],**kwargs) |
| 570 | count = args["COUNT"] if args!={} else 1 |
| 571 | |
| 572 | argstring = "SW" |
| 573 | for j in range(count): |
| 574 | argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1) |
| 575 | args = utilities.parse_args(argstring.split(","),**kwargs) |
| 576 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 577 | sw = args["SW"] if args["SW"] != None else "" |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 578 | ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 579 | ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else "" |
| 580 | |
| 581 | command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " " |
| 582 | for j in range(count): |
| 583 | i=j+1 |
| 584 | args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs) |
| 585 | ip = args["IP"+str(i)] if args["IP"+str(i)] != None else "" |
| 586 | port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else "" |
| 587 | tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else "" |
| 588 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 589 | try: |
| 590 | self.execute(cmd=command,prompt="mininet>",timeout=5) |
| 591 | except pexpect.EOF: |
| 592 | main.log.error(self.name + ": EOF exception found") |
| 593 | main.log.error(self.name + ": " + self.handle.before) |
| 594 | main.cleanup() |
| 595 | main.exit() |
| 596 | except: |
| 597 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 598 | main.log.error( traceback.print_exc() ) |
| 599 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 600 | main.cleanup() |
| 601 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 602 | |
Jon Hall | 0819fd9 | 2014-05-23 12:08:13 -0700 | [diff] [blame] | 603 | def delete_sw_controller(self,sw): |
| 604 | ''' |
| 605 | Removes the controller target from sw |
| 606 | ''' |
| 607 | |
| 608 | command = "sh ovs-vsctl del-controller "+str(sw) |
| 609 | try: |
| 610 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 611 | except pexpect.EOF: |
| 612 | main.log.error(self.name + ": EOF exception found") |
| 613 | main.log.error(self.name + ": " + self.handle.before) |
| 614 | main.cleanup() |
| 615 | main.exit() |
| 616 | else: |
| 617 | main.log.info(response) |
| 618 | |
| 619 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 620 | def disconnect(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 621 | main.log.info(self.name+": Disconnecting mininet...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 622 | response = '' |
| 623 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 624 | try: |
| 625 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 626 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
Jon Hall | e80ef8c | 2014-04-29 15:29:13 -0700 | [diff] [blame] | 627 | self.handle.sendline("sudo mn -c") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 628 | except pexpect.EOF: |
| 629 | main.log.error(self.name + ": EOF exception found") |
| 630 | main.log.error(self.name + ": " + self.handle.before) |
| 631 | main.cleanup() |
| 632 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 633 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 634 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 635 | response = main.FALSE |
| 636 | return response |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 637 | |
| 638 | def arping(self, src, dest, destmac): |
| 639 | self.handle.sendline('') |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 640 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 641 | |
| 642 | self.handle.sendline(src + ' arping ' + dest) |
| 643 | try: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 644 | self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 645 | main.log.info(self.name+": ARP successful") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 646 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 647 | return main.TRUE |
| 648 | except: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 649 | main.log.warn(self.name+": ARP FAILURE") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 650 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 651 | return main.FALSE |
| 652 | |
| 653 | def decToHex(num): |
| 654 | return hex(num).split('x')[1] |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 655 | |
| 656 | def getSwitchFlowCount(self, switch): |
| 657 | ''' |
| 658 | return the Flow Count of the switch |
| 659 | ''' |
| 660 | if self.handle: |
| 661 | cmd = "sh ovs-ofctl dump-aggregate %s" % switch |
| 662 | try: |
| 663 | response = self.execute(cmd=cmd, prompt="mininet>", timeout=10) |
| 664 | except pexpect.EOF: |
| 665 | main.log.error(self.name + ": EOF exception found") |
| 666 | main.log.error(self.name + " " + self.handle.before) |
| 667 | main.cleanup() |
| 668 | main.exit() |
| 669 | pattern = "flow_count=(\d+)" |
| 670 | result = re.search(pattern, response, re.MULTILINE) |
| 671 | if result is None: |
| 672 | print "no flow on switch print test" |
| 673 | main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response)) |
| 674 | return main.FALSE |
| 675 | return result.group(1) |
| 676 | else: |
| 677 | main.log.error("Connection failed to the Mininet host") |
| 678 | |
| 679 | def check_flows(self, sw): |
| 680 | command = "sh ovs-ofctl dump-flows "+str(sw) |
| 681 | try: |
| 682 | response=self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 683 | return response |
| 684 | except pexpect.EOF: |
| 685 | main.log.error(self.name + ": EOF exception found") |
| 686 | main.log.error(self.name + ": " + self.handle.before) |
| 687 | main.cleanup() |
| 688 | main.exit() |
| 689 | else: |
| 690 | main.log.info(response) |
| 691 | |
| 692 | def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"): |
| 693 | ''' |
| 694 | Runs tpdump on an intferface and saves the file |
| 695 | intf can be specified, or the default eth0 is used |
| 696 | ''' |
| 697 | try: |
| 698 | self.handle.sendline("") |
| 699 | self.handle.expect("mininet>") |
| 700 | self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &") |
| 701 | self.handle.sendline("") |
| 702 | self.handle.sendline("") |
| 703 | i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10) |
| 704 | main.log.warn(self.handle.before + self.handle.after) |
| 705 | if i == 0: |
| 706 | main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf) |
| 707 | return main.FALSE |
| 708 | elif i == 1: |
| 709 | main.log.info(self.name + ": tcpdump started on " + intf) |
| 710 | return main.TRUE |
| 711 | elif i == 2: |
| 712 | main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf) |
| 713 | return main.FALSE |
| 714 | elif i ==3: |
| 715 | main.log.info(self.name +": " + self.handle.before) |
| 716 | return main.TRUE |
| 717 | else: |
| 718 | main.log.error(self.name + ": tcpdump - unexpected response") |
| 719 | return main.FALSE |
| 720 | except pexpect.EOF: |
| 721 | main.log.error(self.name + ": EOF exception found") |
| 722 | main.log.error(self.name + ": " + self.handle.before) |
| 723 | main.cleanup() |
| 724 | main.exit() |
| 725 | except: |
| 726 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 727 | main.log.error( traceback.print_exc() ) |
| 728 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 729 | main.cleanup() |
| 730 | main.exit() |
| 731 | |
| 732 | def stop_tcpdump(self): |
| 733 | "pkills tcpdump" |
| 734 | try: |
| 735 | self.handle.sendline("sh sudo pkill tcpdump") |
| 736 | self.handle.sendline("") |
| 737 | self.handle.sendline("") |
| 738 | self.handle.expect("mininet>") |
| 739 | except pexpect.EOF: |
| 740 | main.log.error(self.name + ": EOF exception found") |
| 741 | main.log.error(self.name + ": " + self.handle.before) |
| 742 | main.cleanup() |
| 743 | main.exit() |
| 744 | except: |
| 745 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 746 | main.log.error( traceback.print_exc() ) |
| 747 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 748 | main.cleanup() |
| 749 | main.exit() |
| 750 | |
Jon Hall | 7c3d109 | 2014-08-11 15:08:31 -0700 | [diff] [blame] | 751 | def compare_topo(self, topo, onos_json): |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 752 | ''' |
| 753 | compares mn topology with ONOS topology |
| 754 | onos_list is a list of ONOS controllers, each element of the list should be (handle, name, ip, port) |
| 755 | onos_json is the output of the onos get_json function calling the /wm/onos/topology REST API |
Jon Hall | e3a7450 | 2014-07-25 11:13:16 -0700 | [diff] [blame] | 756 | Returns: True if MN and ONOS topology match and False if the differ. |
| 757 | Differences between ONOS and MN topology will be printed to the log. |
| 758 | |
| 759 | Dependency: Requires STS to be installed on the TestON machine. STS can be pulled |
| 760 | from https://github.com/ucb-sts/sts.git . Currently the required functions from STS are located in the |
| 761 | topology_refactoring2 branch, but may be merged into the master branch soon. You may need to install some |
| 762 | python modules such as networkx to use the STS functions. |
| 763 | |
Jon Hall | 38f5012 | 2014-08-05 13:16:52 -0700 | [diff] [blame] | 764 | To install sts: |
| 765 | $ git clone git://github.com/ucb-sts/sts.git |
| 766 | $ cd sts |
| 767 | $ git clone -b debugger git://github.com/ucb-sts/pox.git |
| 768 | $ sudo apt-get install python-dev |
| 769 | $ ./tools/install_hassel_python.sh |
| 770 | $ sudo pip install networkx |
| 771 | |
| 772 | Include sts in your PYTHONPATH. it should looks comething like: |
| 773 | PYTHONPATH=/home/admin/TestON:/home/admin/sts |
| 774 | |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 775 | ''' |
admin | 680b78c | 2014-08-08 11:46:45 -0700 | [diff] [blame] | 776 | import sys |
| 777 | sys.path.append("~/sts") |
Jon Hall | e934133 | 2014-08-07 10:09:36 -0700 | [diff] [blame] | 778 | #NOTE: Create this once per Test and pass the TestONTopology object around. It takes too long to create this object. |
| 779 | # This will make it easier to use the sts methods for severing links and solve that issue |
Jon Hall | 0d88a25 | 2014-07-25 11:22:40 -0700 | [diff] [blame] | 780 | import json |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 781 | |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 782 | link_results = main.TRUE |
| 783 | switch_results = main.TRUE |
| 784 | port_results = main.TRUE |
| 785 | |
| 786 | ########Switches####### |
| 787 | output = {"switches":[]} |
Jon Hall | 0d88a25 | 2014-07-25 11:22:40 -0700 | [diff] [blame] | 788 | for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 789 | ports = [] |
| 790 | for port in switch.ports.values(): |
Jon Hall | 38f5012 | 2014-08-05 13:16:52 -0700 | [diff] [blame] | 791 | #print port.hw_addr.toStr(separator = '') |
| 792 | ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name}) |
| 793 | output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports }) |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 794 | #print output |
| 795 | |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 796 | #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': ')) |
| 797 | |
| 798 | |
Jon Hall | 0d88a25 | 2014-07-25 11:22:40 -0700 | [diff] [blame] | 799 | # created sorted list of dpid's in MN and ONOS for comparison |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 800 | mnDPIDs=[] |
| 801 | for switch in output['switches']: |
| 802 | mnDPIDs.append(switch['dpid']) |
| 803 | mnDPIDs.sort() |
Jon Hall | e934133 | 2014-08-07 10:09:36 -0700 | [diff] [blame] | 804 | #print mnDPIDs |
Jon Hall | 0d88a25 | 2014-07-25 11:22:40 -0700 | [diff] [blame] | 805 | if onos_json == "":#if rest call fails |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 806 | main.log.error(self.name + ".compare_topo(): Empty JSON object given from ONOS rest call") |
| 807 | return main.FALSE |
| 808 | onos=onos_json |
| 809 | onosDPIDs=[] |
| 810 | for switch in onos['switches']: |
| 811 | onosDPIDs.append(switch['dpid'].replace(":",'')) |
| 812 | onosDPIDs.sort() |
Jon Hall | e934133 | 2014-08-07 10:09:36 -0700 | [diff] [blame] | 813 | #print onosDPIDs |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 814 | |
| 815 | if mnDPIDs!=onosDPIDs: |
| 816 | switch_results = main.FALSE |
| 817 | main.log.report( "Switches in MN but not in ONOS:") |
| 818 | main.log.report( str([switch for switch in mnDPIDs if switch not in onosDPIDs])) |
| 819 | main.log.report( "Switches in ONOS but not in MN:") |
| 820 | main.log.report( str([switch for switch in onosDPIDs if switch not in mnDPIDs])) |
| 821 | else:#list of dpid's match in onos and mn |
| 822 | switch_results = main.TRUE |
| 823 | |
| 824 | ################ports############# |
| 825 | for switch in output['switches']: |
| 826 | mn_ports = [] |
| 827 | onos_ports = [] |
| 828 | for port in switch['ports']: |
| 829 | mn_ports.append(port['of_port']) |
| 830 | for onos_switch in onos['switches']: |
| 831 | if onos_switch['dpid'].replace(':','') == switch['dpid']: |
| 832 | for port in onos_switch['ports']: |
| 833 | onos_ports.append(port['portNumber']) |
| 834 | mn_ports.sort() |
| 835 | onos_ports.sort() |
| 836 | if mn_ports == onos_ports: |
| 837 | pass #don't set results to true here as this is just one of many checks and it might override a failure |
| 838 | else: #the ports of this switch don't match |
| 839 | port_results = main.FALSE |
| 840 | main.log.report("ports in MN switch %s(%s) but not in ONOS:" % (switch['name'],switch['dpid'])) |
| 841 | main.log.report( str([port for port in mn_ports if port not in onos_ports])) |
| 842 | main.log.report("ports in ONOS switch %s(%s) but not in MN:" % (switch['name'],switch['dpid'])) |
| 843 | main.log.report( str([port for port in onos_ports if port not in mn_ports])) |
| 844 | |
| 845 | |
| 846 | #######Links######## |
Jon Hall | 0d88a25 | 2014-07-25 11:22:40 -0700 | [diff] [blame] | 847 | # iterate through MN links and check if and ONOS link exists in both directions |
Jon Hall | e934133 | 2014-08-07 10:09:36 -0700 | [diff] [blame] | 848 | # NOTE: Will currently only show mn links as down if they are cut through STS. |
| 849 | # We can either do everything through STS or wait for up_network_links |
| 850 | # and down_network_links to be fully implemented. |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 851 | for link in topo.patch_panel.network_links: |
| 852 | #print "Link: %s" % link |
| 853 | #TODO: Find a more efficient search method |
| 854 | node1 = None |
| 855 | port1 = None |
| 856 | node2 = None |
| 857 | port2 = None |
| 858 | first_dir = main.FALSE |
| 859 | second_dir = main.FALSE |
| 860 | for switch in output['switches']: |
| 861 | if switch['name'] == link.node1.name: |
| 862 | node1 = switch['dpid'] |
| 863 | for port in switch['ports']: |
| 864 | if str(port['name']) == str(link.port1): |
| 865 | port1 = port['of_port'] |
| 866 | if node1 is not None and node2 is not None: |
| 867 | break |
| 868 | if switch['name'] == link.node2.name: |
| 869 | node2 = switch['dpid'] |
| 870 | for port in switch['ports']: |
| 871 | if str(port['name']) == str(link.port2): |
| 872 | port2 = port['of_port'] |
| 873 | if node1 is not None and node2 is not None: |
| 874 | break |
| 875 | # check onos link from node1 to node2 |
| 876 | for onos_link in onos['links']: |
| 877 | if onos_link['src']['dpid'].replace(":",'') == node1 and onos_link['dst']['dpid'].replace(":",'') == node2: |
| 878 | if onos_link['src']['portNumber'] == port1 and onos_link['dst']['portNumber'] == port2: |
| 879 | first_dir = main.TRUE |
| 880 | else: |
| 881 | main.log.report('the port numbers do not match for ' +str(link) + ' between ONOS and MN') |
| 882 | #print node1, ' to ', node2 |
| 883 | elif onos_link['src']['dpid'].replace(":",'') == node2 and onos_link['dst']['dpid'].replace(":",'') == node1: |
| 884 | if onos_link['src']['portNumber'] == port2 and onos_link['dst']['portNumber'] == port1: |
| 885 | second_dir = main.TRUE |
| 886 | else: |
| 887 | main.log.report('the port numbers do not match for ' +str(link) + ' between ONOS and MN') |
| 888 | #print node2, ' to ', node1 |
| 889 | else:#this is not the link you're looking for |
| 890 | pass |
| 891 | if not first_dir: |
| 892 | main.log.report('ONOS has issues with the link from '+str(link.node1.name) +"(dpid: "+ str(node1)+"):"+str(link.port1)+"(portNumber: "+str(port1)+")"+ ' to ' + str(link.node2.name) +"(dpid: "+ str(node2)+"):"+str(link.port2)+"(portNumber: "+str(port2)+")") |
| 893 | if not second_dir: |
| 894 | main.log.report('ONOS has issues with the link from '+str(link.node2.name) +"(dpid: "+ str(node2)+"):"+str(link.port2)+"(portNumber: "+str(port2)+")"+ ' to ' + str(link.node1.name) +"(dpid: "+ str(node1)+"):"+str(link.port1)+"(portNumber: "+str(port1)+")") |
| 895 | link_results = link_results and first_dir and second_dir |
| 896 | |
| 897 | |
| 898 | results = switch_results and port_results and link_results |
Jon Hall | e934133 | 2014-08-07 10:09:36 -0700 | [diff] [blame] | 899 | # if not results: #To print out both topologies |
| 900 | # main.log.error("Topology comparison failed, printing json objects, MN then ONOS") |
| 901 | # main.log.error(str(json.dumps(output, sort_keys=True,indent=4,separators=(',', ': ')))) |
| 902 | # main.log.error('MN Links:') |
| 903 | # for link in topo.patch_panel.network_links: main.log.error(str("\tLink: %s" % link)) |
| 904 | # main.log.error(str(json.dumps(onos, sort_keys=True,indent=4,separators=(',', ': ')))) |
Jon Hall | 8412ed1 | 2014-07-24 16:33:58 -0700 | [diff] [blame] | 905 | return results |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 906 | |
| 907 | |
| 908 | |
| 909 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 910 | |
| 911 | if __name__ != "__main__": |
| 912 | import sys |
| 913 | sys.modules[__name__] = MininetCliDriver() |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 914 | |