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