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 | ''' |
| 24 | |
| 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: |
| 112 | response = self.execute(cmd="pingall",prompt="mininet>",timeout=10) |
| 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 | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 158 | command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1" |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 159 | try: |
| 160 | response = self.execute(cmd=command,prompt="mininet",timeout=10 ) |
| 161 | except pexpect.EOF: |
| 162 | main.log.error(self.name + ": EOF exception found") |
| 163 | main.log.error(self.name + ": " + self.handle.before) |
| 164 | main.cleanup() |
| 165 | main.exit() |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 166 | main.log.info(self.name+": Ping Response: "+ response ) |
| 167 | #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"): |
| 168 | if re.search(',\s0\%\spacket\sloss',response): |
| 169 | main.log.info(self.name+": NO PACKET LOSS, HOST IS REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 170 | main.last_result = main.TRUE |
| 171 | return main.TRUE |
| 172 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 173 | main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 174 | main.last_result = main.FALSE |
| 175 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 176 | |
| 177 | def checkIP(self,host): |
| 178 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 179 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 180 | ''' |
| 181 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 182 | try: |
| 183 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 184 | except pexpect.EOF: |
| 185 | main.log.error(self.name + ": EOF exception found") |
| 186 | main.log.error(self.name + ": " + self.handle.before) |
| 187 | main.cleanup() |
| 188 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | |
| 190 | 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})" |
| 191 | #pattern = "inet\saddr:10.0.0.6" |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 192 | #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") : |
| 193 | if re.search(pattern,response): |
| 194 | main.log.info(self.name+": Host Ip configured properly") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 195 | return main.TRUE |
| 196 | else: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 197 | main.log.error(self.name+": Host IP not found") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 198 | return main.FALSE |
| 199 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 200 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | |
| 202 | def verifySSH(self,**connectargs): |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 203 | try: |
| 204 | response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 205 | response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10) |
| 206 | for key in connectargs: |
| 207 | vars(self)[key] = connectargs[key] |
| 208 | response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10) |
| 209 | except pexpect.EOF: |
| 210 | main.log.error(self.name + ": EOF exception found") |
| 211 | main.log.error(self.name + ": " + self.handle.before) |
| 212 | main.cleanup() |
| 213 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | import time |
| 215 | time.sleep(20) |
| 216 | if self.flag == 0: |
| 217 | self.flag = 1 |
| 218 | return main.FALSE |
| 219 | else : |
| 220 | return main.TRUE |
| 221 | |
| 222 | def getMacAddress(self,host): |
| 223 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 224 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | ''' |
| 226 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 227 | try: |
| 228 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 229 | except pexpect.EOF: |
| 230 | main.log.error(self.name + ": EOF exception found") |
| 231 | main.log.error(self.name + ": " + self.handle.before) |
| 232 | main.cleanup() |
| 233 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 234 | |
Ahmed El-Hassany | f720e20 | 2014-04-04 16:11:36 -0700 | [diff] [blame] | 235 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 236 | mac_address_search = re.search(pattern, response, re.I) |
| 237 | mac_address = mac_address_search.group().split(" ")[1] |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 238 | 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] | 239 | return mac_address |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 240 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 241 | main.log.error(self.name+": Connection failed to the host") |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 242 | |
| 243 | def getInterfaceMACAddress(self,host, interface): |
| 244 | ''' |
| 245 | Return the IP address of the interface on the given host |
| 246 | ''' |
| 247 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 248 | try: |
| 249 | response = self.execute(cmd=host+" ifconfig " + interface, |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 250 | prompt="mininet>",timeout=10) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 251 | except pexpect.EOF: |
| 252 | main.log.error(self.name + ": EOF exception found") |
| 253 | main.log.error(self.name + ": " + self.handle.before) |
| 254 | main.cleanup() |
| 255 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 256 | |
| 257 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 258 | mac_address_search = re.search(pattern, response, re.I) |
| 259 | if mac_address_search is None: |
| 260 | main.log.info("No mac address found in %s" % response) |
| 261 | return main.FALSE |
| 262 | mac_address = mac_address_search.group().split(" ")[1] |
| 263 | main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address) |
| 264 | return mac_address |
| 265 | else: |
| 266 | main.log.error("Connection failed to the host") |
| 267 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 268 | def getIPAddress(self,host): |
| 269 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 270 | Verifies the host's ip configured or not. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 271 | ''' |
| 272 | if self.handle : |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 273 | try: |
| 274 | response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10) |
| 275 | except pexpect.EOF: |
| 276 | main.log.error(self.name + ": EOF exception found") |
| 277 | main.log.error(self.name + ": " + self.handle.before) |
| 278 | main.cleanup() |
| 279 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 280 | |
| 281 | pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)" |
| 282 | ip_address_search = re.search(pattern, response) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 283 | 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] | 284 | return ip_address_search.group(1) |
| 285 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 286 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 287 | |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 288 | def getSwitchDPID(self,switch): |
| 289 | ''' |
| 290 | return the datapath ID of the switch |
| 291 | ''' |
| 292 | if self.handle : |
| 293 | cmd = "py %s.dpid" % switch |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 294 | try: |
| 295 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 296 | except pexpect.EOF: |
| 297 | main.log.error(self.name + ": EOF exception found") |
| 298 | main.log.error(self.name + ": " + self.handle.before) |
| 299 | main.cleanup() |
| 300 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 301 | pattern = r'^(?P<dpid>\d)+' |
| 302 | result = re.search(pattern, response, re.MULTILINE) |
| 303 | if result is None: |
| 304 | main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response)) |
| 305 | return main.FALSE |
| 306 | return int(result.group('dpid')) |
| 307 | else: |
| 308 | main.log.error("Connection failed to the host") |
| 309 | |
| 310 | def getInterfaces(self, node): |
| 311 | ''' |
| 312 | return information dict about interfaces connected to the node |
| 313 | ''' |
| 314 | if self.handle : |
| 315 | cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())' |
| 316 | cmd += ' for i in %s.intfs.values()])' % node |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 317 | try: |
| 318 | response = self.execute(cmd=cmd,prompt="mininet>",timeout=10) |
| 319 | except pexpect.EOF: |
| 320 | main.log.error(self.name + ": EOF exception found") |
| 321 | main.log.error(self.name + ": " + self.handle.before) |
| 322 | main.cleanup() |
| 323 | main.exit() |
Ahmed El-Hassany | fd32918 | 2014-04-10 11:38:16 -0700 | [diff] [blame] | 324 | return response |
| 325 | else: |
| 326 | main.log.error("Connection failed to the node") |
| 327 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 328 | def dump(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 329 | main.log.info(self.name+": Dump node info") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 330 | try: |
| 331 | response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10) |
| 332 | except pexpect.EOF: |
| 333 | main.log.error(self.name + ": EOF exception found") |
| 334 | main.log.error(self.name + ": " + self.handle.before) |
| 335 | main.cleanup() |
| 336 | main.exit() |
Ahmed El-Hassany | d1f7170 | 2014-04-04 16:12:45 -0700 | [diff] [blame] | 337 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 338 | |
| 339 | def intfs(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 340 | main.log.info(self.name+": List interfaces") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 341 | try: |
| 342 | response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10) |
| 343 | except pexpect.EOF: |
| 344 | main.log.error(self.name + ": EOF exception found") |
| 345 | main.log.error(self.name + ": " + self.handle.before) |
| 346 | main.cleanup() |
| 347 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 348 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 349 | |
| 350 | def net(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 351 | main.log.info(self.name+": List network connections") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 352 | try: |
| 353 | response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10) |
| 354 | except pexpect.EOF: |
| 355 | main.log.error(self.name + ": EOF exception found") |
| 356 | main.log.error(self.name + ": " + self.handle.before) |
| 357 | main.cleanup() |
| 358 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 359 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 360 | |
| 361 | def iperf(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 362 | 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] | 363 | try: |
| 364 | response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10) |
| 365 | except pexpect.EOF: |
| 366 | main.log.error(self.name + ": EOF exception found") |
| 367 | main.log.error(self.name + ": " + self.handle.before) |
| 368 | main.cleanup() |
| 369 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 370 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 371 | |
| 372 | def iperfudp(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 373 | 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] | 374 | try: |
| 375 | response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10) |
| 376 | except pexpect.EOF: |
| 377 | main.log.error(self.name + ": EOF exception found") |
| 378 | main.log.error(self.name + ": " + self.handle.before) |
| 379 | main.cleanup() |
| 380 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 381 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 382 | |
| 383 | def nodes(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 384 | main.log.info(self.name+": List all nodes.") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 385 | try: |
| 386 | response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10) |
| 387 | except pexpect.EOF: |
| 388 | main.log.error(self.name + ": EOF exception found") |
| 389 | main.log.error(self.name + ": " + self.handle.before) |
| 390 | main.cleanup() |
| 391 | main.exit() |
Jon Hall | 668ed80 | 2014-04-08 17:17:59 -0700 | [diff] [blame] | 392 | return response |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 393 | |
| 394 | def pingpair(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 395 | main.log.info(self.name+": Ping between first two hosts") |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 396 | try: |
| 397 | response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20) |
| 398 | except pexpect.EOF: |
| 399 | main.log.error(self.name + ": EOF exception found") |
| 400 | main.log.error(self.name + ": " + self.handle.before) |
| 401 | main.cleanup() |
| 402 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 403 | |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 404 | #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"): |
| 405 | if re.search(',\s0\%\spacket\sloss',response): |
| 406 | main.log.info(self.name+": Ping between two hosts SUCCESSFUL") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 407 | main.last_result = main.TRUE |
| 408 | return main.TRUE |
| 409 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 410 | main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 411 | main.last_result = main.FALSE |
| 412 | return main.FALSE |
| 413 | |
| 414 | def link(self,**linkargs): |
| 415 | ''' |
| 416 | Bring link(s) between two nodes up or down |
| 417 | ''' |
| 418 | main.log.info('Bring link(s) between two nodes up or down') |
| 419 | args = utilities.parse_args(["END1","END2","OPTION"],**linkargs) |
| 420 | end1 = args["END1"] if args["END1"] != None else "" |
| 421 | end2 = args["END2"] if args["END2"] != None else "" |
| 422 | option = args["OPTION"] if args["OPTION"] != None else "" |
| 423 | command = "link "+str(end1) + " " + str(end2)+ " " + str(option) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 424 | try: |
| 425 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 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 | return main.TRUE |
| 432 | |
| 433 | |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 434 | def yank(self,**yankargs): |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 435 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 436 | yank a mininet switch interface to a host |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 437 | ''' |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 438 | main.log.info('Yank the switch interface attached to a host') |
| 439 | args = utilities.parse_args(["SW","INTF"],**yankargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 440 | sw = args["SW"] if args["SW"] !=None else "" |
| 441 | intf = args["INTF"] if args["INTF"] != None else "" |
| 442 | command = "py "+ str(sw) + '.detach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 443 | try: |
| 444 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 445 | except pexpect.EOF: |
| 446 | main.log.error(self.name + ": EOF exception found") |
| 447 | main.log.error(self.name + ": " + self.handle.before) |
| 448 | main.cleanup() |
| 449 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 450 | return main.TRUE |
| 451 | |
| 452 | def plug(self, **plugargs): |
| 453 | ''' |
| 454 | plug the yanked mininet switch interface to a switch |
| 455 | ''' |
| 456 | main.log.info('Plug the switch interface attached to a switch') |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 457 | args = utilities.parse_args(["SW","INTF"],**plugargs) |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 458 | sw = args["SW"] if args["SW"] !=None else "" |
| 459 | intf = args["INTF"] if args["INTF"] != None else "" |
| 460 | command = "py "+ str(sw) + '.attach("' + str(intf) + '")' |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 461 | try: |
| 462 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 463 | except pexpect.EOF: |
| 464 | main.log.error(self.name + ": EOF exception found") |
| 465 | main.log.error(self.name + ": " + self.handle.before) |
| 466 | main.cleanup() |
| 467 | main.exit() |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 468 | return main.TRUE |
| 469 | |
| 470 | |
| 471 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 472 | def dpctl(self,**dpctlargs): |
| 473 | ''' |
Jon Hall | 41f40e8 | 2014-04-08 16:43:17 -0700 | [diff] [blame] | 474 | Run dpctl command on all switches. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 475 | ''' |
| 476 | main.log.info('Run dpctl command on all switches') |
| 477 | args = utilities.parse_args(["CMD","ARGS"],**dpctlargs) |
| 478 | cmd = args["CMD"] if args["CMD"] != None else "" |
| 479 | cmdargs = args["ARGS"] if args["ARGS"] != None else "" |
| 480 | command = "dpctl "+cmd + " " + str(cmdargs) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 481 | try: |
| 482 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 483 | except pexpect.EOF: |
| 484 | main.log.error(self.name + ": EOF exception found") |
| 485 | main.log.error(self.name + ": " + self.handle.before) |
| 486 | main.cleanup() |
| 487 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 488 | return main.TRUE |
| 489 | |
| 490 | |
| 491 | def get_version(self): |
| 492 | file_input = path+'/lib/Mininet/INSTALL' |
| 493 | version = super(Mininet, self).get_version() |
| 494 | pattern = 'Mininet\s\w\.\w\.\w\w*' |
| 495 | for line in open(file_input,'r').readlines(): |
| 496 | result = re.match(pattern, line) |
| 497 | if result: |
| 498 | version = result.group(0) |
| 499 | return version |
| 500 | |
| 501 | def get_sw_controller(self,sw): |
| 502 | command = "sh ovs-vsctl get-controller "+str(sw) |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 503 | try: |
| 504 | response = self.execute(cmd=command,prompt="mininet>",timeout=10) |
| 505 | except pexpect.EOF: |
| 506 | main.log.error(self.name + ": EOF exception found") |
| 507 | main.log.error(self.name + ": " + self.handle.before) |
| 508 | main.cleanup() |
| 509 | main.exit() |
| 510 | else: |
| 511 | main.log.info(response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 512 | |
| 513 | def assign_sw_controller(self,**kwargs): |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 514 | ''' |
| 515 | count is only needed if there is more than 1 controller |
| 516 | ''' |
| 517 | args = utilities.parse_args(["COUNT"],**kwargs) |
| 518 | count = args["COUNT"] if args!={} else 1 |
| 519 | |
| 520 | argstring = "SW" |
| 521 | for j in range(count): |
| 522 | argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1) |
| 523 | args = utilities.parse_args(argstring.split(","),**kwargs) |
| 524 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 525 | sw = args["SW"] if args["SW"] != None else "" |
admin | 530b4c9 | 2013-08-14 16:54:35 -0700 | [diff] [blame] | 526 | ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else "" |
Jon Hall | f89c855 | 2014-04-02 13:14:06 -0700 | [diff] [blame] | 527 | ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else "" |
| 528 | |
| 529 | command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " " |
| 530 | for j in range(count): |
| 531 | i=j+1 |
| 532 | args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs) |
| 533 | ip = args["IP"+str(i)] if args["IP"+str(i)] != None else "" |
| 534 | port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else "" |
| 535 | tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else "" |
| 536 | command = command + tcp |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 537 | try: |
| 538 | self.execute(cmd=command,prompt="mininet>",timeout=5) |
| 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 | except: |
| 545 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 546 | main.log.error( traceback.print_exc() ) |
| 547 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 548 | main.cleanup() |
| 549 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 550 | |
| 551 | def disconnect(self): |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 552 | main.log.info(self.name+": Disconnecting mininet...") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 553 | response = '' |
| 554 | if self.handle: |
Jon Hall | 6094a36 | 2014-04-11 14:46:56 -0700 | [diff] [blame] | 555 | try: |
| 556 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 557 | response = self.execute(cmd="exit",prompt="(.*)",timeout=120) |
| 558 | except pexpect.EOF: |
| 559 | main.log.error(self.name + ": EOF exception found") |
| 560 | main.log.error(self.name + ": " + self.handle.before) |
| 561 | main.cleanup() |
| 562 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 563 | else : |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 564 | main.log.error(self.name+": Connection failed to the host") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 565 | response = main.FALSE |
| 566 | return response |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 567 | |
| 568 | def arping(self, src, dest, destmac): |
| 569 | self.handle.sendline('') |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 570 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 571 | |
| 572 | self.handle.sendline(src + ' arping ' + dest) |
| 573 | try: |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 574 | self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT]) |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 575 | main.log.info(self.name+": ARP successful") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 576 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 577 | return main.TRUE |
| 578 | except: |
Jon Hall | f2942ce | 2014-04-10 16:00:16 -0700 | [diff] [blame] | 579 | main.log.warn(self.name+": ARP FAILURE") |
Jon Hall | 333fa8c | 2014-04-11 11:24:58 -0700 | [diff] [blame] | 580 | self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT]) |
admin | 0752993 | 2013-11-22 14:58:28 -0800 | [diff] [blame] | 581 | return main.FALSE |
| 582 | |
| 583 | def decToHex(num): |
| 584 | return hex(num).split('x')[1] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 585 | |
| 586 | if __name__ != "__main__": |
| 587 | import sys |
| 588 | sys.modules[__name__] = MininetCliDriver() |