blob: 09ac89cb131d3568cba49df2b34b3c23b2ad94ea [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created 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
22MininetCliDriver is the basic driver which will handle the Mininet functions
23'''
admin2a9548d2014-06-17 14:08:07 -070024import traceback
adminbae64d82013-08-01 10:50:15 -070025import pexpect
26import struct
27import fcntl
28import os
29import signal
30import re
31import sys
32import core.teston
33sys.path.append("../")
34from drivers.common.cli.emulatordriver import Emulator
35from drivers.common.clidriver import CLI
36
37class MininetCliDriver(Emulator):
38 '''
Jon Hall41f40e82014-04-08 16:43:17 -070039 MininetCliDriver is the basic driver which will handle the Mininet functions
adminbae64d82013-08-01 10:50:15 -070040 '''
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 Hall41f40e82014-04-08 16:43:17 -070048 '''
49 Here the main is the TestON instance after creating all the log handles.
50 '''
adminbae64d82013-08-01 10:50:15 -070051 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
adminbae64d82013-08-01 10:50:15 -070059 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -070060 main.log.info(self.name+": Clearing any residual state or processes")
adminbae64d82013-08-01 10:50:15 -070061 self.handle.sendline("sudo mn -c")
adminf939f8b2014-04-03 17:22:56 -070062 i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070063 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070064 main.log.info(self.name+": Sending sudo password")
adminf939f8b2014-04-03 17:22:56 -070065 self.handle.sendline(self.pwd)
66 i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070067 if i==1:
Jon Hallf2942ce2014-04-10 16:00:16 -070068 main.log.info(self.name+": Clean")
adminbae64d82013-08-01 10:50:15 -070069 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070070 main.log.error(self.name+": Connection terminated")
adminbae64d82013-08-01 10:50:15 -070071 elif i==3: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -070072 main.log.error(self.name+": Something while cleaning MN took too long... " )
adminbae64d82013-08-01 10:50:15 -070073
Jon Hallf2942ce2014-04-10 16:00:16 -070074 main.log.info(self.name+": building fresh mininet")
adminbeea0032014-01-23 14:54:13 -080075 #### for reactive/PARP enabled tests
admin07529932013-11-22 14:58:28 -080076 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller']
adminbeea0032014-01-23 14:54:13 -080077 #### for proactive flow with static ARP entries
78 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller']
adminbae64d82013-08-01 10:50:15 -070079 self.handle.sendline(cmdString)
Jon Hall333fa8c2014-04-11 11:24:58 -070080 self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070081 while 1:
82 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
83 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070084 main.log.info(self.name+": mininet built")
adminbae64d82013-08-01 10:50:15 -070085 return main.TRUE
86 if i==1:
Jon Hall333fa8c2014-04-11 11:24:58 -070087 self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070088 main.log.info(self.handle.before)
89 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070090 main.log.error(self.name+": Launching mininet failed...")
adminbae64d82013-08-01 10:50:15 -070091 return main.FALSE
92 elif i==3:
Jon Hallf2942ce2014-04-10 16:00:16 -070093 main.log.error(self.name+": Connection timeout")
adminbae64d82013-08-01 10:50:15 -070094 return main.FALSE
95 elif i==4: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -070096 main.log.error(self.name+": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -070097 return main.FALSE
adminbae64d82013-08-01 10:50:15 -070098 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
99 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700100 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")
adminbae64d82013-08-01 10:50:15 -0700103 return main.FALSE
104
105 def pingall(self):
106 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700107 Verifies the reachability of the hosts using pingall command.
adminbae64d82013-08-01 10:50:15 -0700108 '''
109 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -0700110 main.log.info(self.name+": Checking reachabilty to the hosts using pingall")
Jon Hall6094a362014-04-11 14:46:56 -0700111 try:
Jon Hallefa4fa12014-04-14 11:40:21 -0700112 response = self.execute(cmd="pingall",prompt="mininet>",timeout=120)
Jon Hall6094a362014-04-11 14:46:56 -0700113 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()
adminbae64d82013-08-01 10:50:15 -0700118 pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$'
Jon Hallf2942ce2014-04-10 16:00:16 -0700119 #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")
adminbae64d82013-08-01 10:50:15 -0700122 return main.TRUE
123 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700124 main.log.error(self.name+": Unable to reach all the hosts")
adminbae64d82013-08-01 10:50:15 -0700125 return main.FALSE
126 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700127 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700128 return main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700129
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)
admin530b4c92013-08-14 16:54:35 -0700136 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700137 self.handle.sendline(command)
Jon Hall333fa8c2014-04-11 11:24:58 -0700138 self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT])
139 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
adminaeedddd2013-08-02 15:14:15 -0700140 response = self.handle.before
141 if re.search(":\s-" ,response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700142 main.log.info(self.name+": Ping fail")
adminaeedddd2013-08-02 15:14:15 -0700143 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700144 elif re.search(":\s\d{1,2}\.\d\d", response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700145 main.log.info(self.name+": Ping good!")
adminaeedddd2013-08-02 15:14:15 -0700146 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700147 main.log.info(self.name+": Install fping on mininet machine... ")
148 main.log.info(self.name+": \n---\n"+response)
adminaeedddd2013-08-02 15:14:15 -0700149 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700150
151 def pingHost(self,**pingParams):
Jon Hallf2942ce2014-04-10 16:00:16 -0700152 '''
153 Ping from one mininet host to another
154 Currently the only supported Params: SRC and TARGET
155 '''
adminbae64d82013-08-01 10:50:15 -0700156 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
157 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
Jon Hall0819fd92014-05-23 12:08:13 -0700158 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700159 try:
Jon Hall6e18c7b2014-04-23 16:26:33 -0700160 main.log.warn("Sending: " + command)
161 #response = self.execute(cmd=command,prompt="mininet",timeout=10 )
162 self.handle.sendline(command)
163 i = self.handle.expect([command,pexpect.TIMEOUT])
164 if i == 1:
165 main.log.error(self.name + ": timeout when waiting for response from mininet")
166 main.log.error("response: " + str(self.handle.before))
167 i = self.handle.expect(["mininet>",pexpect.TIMEOUT])
168 if i == 1:
169 main.log.error(self.name + ": timeout when waiting for response from mininet")
170 main.log.error("response: " + str(self.handle.before))
171 response = self.handle.before
Jon Hall6094a362014-04-11 14:46:56 -0700172 except pexpect.EOF:
173 main.log.error(self.name + ": EOF exception found")
174 main.log.error(self.name + ": " + self.handle.before)
175 main.cleanup()
176 main.exit()
Jon Hallf2942ce2014-04-10 16:00:16 -0700177 main.log.info(self.name+": Ping Response: "+ response )
178 #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
179 if re.search(',\s0\%\spacket\sloss',response):
Jon Hall6e18c7b2014-04-23 16:26:33 -0700180 main.log.info(self.name+": no packets lost, host is reachable")
adminbae64d82013-08-01 10:50:15 -0700181 main.last_result = main.TRUE
182 return main.TRUE
183 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700184 main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700185 main.last_result = main.FALSE
186 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700187
188 def checkIP(self,host):
189 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700190 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700191 '''
Jon Hallbd795bf2014-06-18 09:46:32 -0700192 self.handle.sendline("")
adminbae64d82013-08-01 10:50:15 -0700193 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700194 try:
195 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
admin0583f572014-06-17 15:33:20 -0700196 print("response 2"+ response)
197 self.handle.sendline(host+" ifconfig")
198 self.handle.expect(["mininet>",pexpect.TIMEOUT])
199 response = self.handle.before + self.handle.after
admin2a9548d2014-06-17 14:08:07 -0700200 print(response)
Jon Hall6094a362014-04-11 14:46:56 -0700201 except pexpect.EOF:
202 main.log.error(self.name + ": EOF exception found")
203 main.log.error(self.name + ": " + self.handle.before)
204 main.cleanup()
205 main.exit()
adminbae64d82013-08-01 10:50:15 -0700206
207 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})"
admin2a9548d2014-06-17 14:08:07 -0700208 #pattern = "inet addr:10.0.0.6"
Jon Hallf2942ce2014-04-10 16:00:16 -0700209 #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
210 if re.search(pattern,response):
211 main.log.info(self.name+": Host Ip configured properly")
adminbae64d82013-08-01 10:50:15 -0700212 return main.TRUE
213 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700214 main.log.error(self.name+": Host IP not found")
adminbae64d82013-08-01 10:50:15 -0700215 return main.FALSE
216 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700217 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700218
219 def verifySSH(self,**connectargs):
Jon Hall6094a362014-04-11 14:46:56 -0700220 try:
221 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
222 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
223 for key in connectargs:
224 vars(self)[key] = connectargs[key]
225 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
226 except pexpect.EOF:
227 main.log.error(self.name + ": EOF exception found")
228 main.log.error(self.name + ": " + self.handle.before)
229 main.cleanup()
230 main.exit()
adminbae64d82013-08-01 10:50:15 -0700231 import time
232 time.sleep(20)
233 if self.flag == 0:
234 self.flag = 1
235 return main.FALSE
236 else :
237 return main.TRUE
238
239 def getMacAddress(self,host):
240 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700241 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700242 '''
243 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700244 try:
245 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
246 except pexpect.EOF:
247 main.log.error(self.name + ": EOF exception found")
248 main.log.error(self.name + ": " + self.handle.before)
249 main.cleanup()
250 main.exit()
adminbae64d82013-08-01 10:50:15 -0700251
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700252 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
253 mac_address_search = re.search(pattern, response, re.I)
254 mac_address = mac_address_search.group().split(" ")[1]
Jon Hallf2942ce2014-04-10 16:00:16 -0700255 main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address)
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700256 return mac_address
adminbae64d82013-08-01 10:50:15 -0700257 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700258 main.log.error(self.name+": Connection failed to the host")
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700259
260 def getInterfaceMACAddress(self,host, interface):
261 '''
262 Return the IP address of the interface on the given host
263 '''
264 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700265 try:
266 response = self.execute(cmd=host+" ifconfig " + interface,
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700267 prompt="mininet>",timeout=10)
Jon Hall6094a362014-04-11 14:46:56 -0700268 except pexpect.EOF:
269 main.log.error(self.name + ": EOF exception found")
270 main.log.error(self.name + ": " + self.handle.before)
271 main.cleanup()
272 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700273
274 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
275 mac_address_search = re.search(pattern, response, re.I)
276 if mac_address_search is None:
277 main.log.info("No mac address found in %s" % response)
278 return main.FALSE
279 mac_address = mac_address_search.group().split(" ")[1]
280 main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address)
281 return mac_address
282 else:
283 main.log.error("Connection failed to the host")
284
adminbae64d82013-08-01 10:50:15 -0700285 def getIPAddress(self,host):
286 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700287 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700288 '''
289 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700290 try:
291 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
292 except pexpect.EOF:
293 main.log.error(self.name + ": EOF exception found")
294 main.log.error(self.name + ": " + self.handle.before)
295 main.cleanup()
296 main.exit()
adminbae64d82013-08-01 10:50:15 -0700297
298 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
299 ip_address_search = re.search(pattern, response)
Jon Hallf2942ce2014-04-10 16:00:16 -0700300 main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1))
adminbae64d82013-08-01 10:50:15 -0700301 return ip_address_search.group(1)
302 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700303 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700304
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700305 def getSwitchDPID(self,switch):
306 '''
307 return the datapath ID of the switch
308 '''
309 if self.handle :
310 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700311 try:
312 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
313 except pexpect.EOF:
314 main.log.error(self.name + ": EOF exception found")
315 main.log.error(self.name + ": " + self.handle.before)
316 main.cleanup()
317 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700318 pattern = r'^(?P<dpid>\d)+'
319 result = re.search(pattern, response, re.MULTILINE)
320 if result is None:
321 main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response))
322 return main.FALSE
323 return int(result.group('dpid'))
324 else:
325 main.log.error("Connection failed to the host")
326
327 def getInterfaces(self, node):
328 '''
329 return information dict about interfaces connected to the node
330 '''
331 if self.handle :
332 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
333 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700334 try:
335 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
336 except pexpect.EOF:
337 main.log.error(self.name + ": EOF exception found")
338 main.log.error(self.name + ": " + self.handle.before)
339 main.cleanup()
340 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700341 return response
342 else:
343 main.log.error("Connection failed to the node")
344
adminbae64d82013-08-01 10:50:15 -0700345 def dump(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700346 main.log.info(self.name+": Dump node info")
Jon Hall6094a362014-04-11 14:46:56 -0700347 try:
348 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
349 except pexpect.EOF:
350 main.log.error(self.name + ": EOF exception found")
351 main.log.error(self.name + ": " + self.handle.before)
352 main.cleanup()
353 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700354 return response
adminbae64d82013-08-01 10:50:15 -0700355
356 def intfs(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700357 main.log.info(self.name+": List interfaces")
Jon Hall6094a362014-04-11 14:46:56 -0700358 try:
359 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
360 except pexpect.EOF:
361 main.log.error(self.name + ": EOF exception found")
362 main.log.error(self.name + ": " + self.handle.before)
363 main.cleanup()
364 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700365 return response
adminbae64d82013-08-01 10:50:15 -0700366
367 def net(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700368 main.log.info(self.name+": List network connections")
Jon Hall6094a362014-04-11 14:46:56 -0700369 try:
370 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
371 except pexpect.EOF:
372 main.log.error(self.name + ": EOF exception found")
373 main.log.error(self.name + ": " + self.handle.before)
374 main.cleanup()
375 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700376 return response
adminbae64d82013-08-01 10:50:15 -0700377
378 def iperf(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700379 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700380 try:
381 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
382 except pexpect.EOF:
383 main.log.error(self.name + ": EOF exception found")
384 main.log.error(self.name + ": " + self.handle.before)
385 main.cleanup()
386 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700387 return response
adminbae64d82013-08-01 10:50:15 -0700388
389 def iperfudp(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700390 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700391 try:
392 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
393 except pexpect.EOF:
394 main.log.error(self.name + ": EOF exception found")
395 main.log.error(self.name + ": " + self.handle.before)
396 main.cleanup()
397 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700398 return response
adminbae64d82013-08-01 10:50:15 -0700399
400 def nodes(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700401 main.log.info(self.name+": List all nodes.")
Jon Hall6094a362014-04-11 14:46:56 -0700402 try:
403 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
404 except pexpect.EOF:
405 main.log.error(self.name + ": EOF exception found")
406 main.log.error(self.name + ": " + self.handle.before)
407 main.cleanup()
408 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700409 return response
adminbae64d82013-08-01 10:50:15 -0700410
411 def pingpair(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700412 main.log.info(self.name+": Ping between first two hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700413 try:
414 response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
415 except pexpect.EOF:
416 main.log.error(self.name + ": EOF exception found")
417 main.log.error(self.name + ": " + self.handle.before)
418 main.cleanup()
419 main.exit()
adminbae64d82013-08-01 10:50:15 -0700420
Jon Hallf2942ce2014-04-10 16:00:16 -0700421 #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
422 if re.search(',\s0\%\spacket\sloss',response):
423 main.log.info(self.name+": Ping between two hosts SUCCESSFUL")
adminbae64d82013-08-01 10:50:15 -0700424 main.last_result = main.TRUE
425 return main.TRUE
426 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700427 main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700428 main.last_result = main.FALSE
429 return main.FALSE
430
431 def link(self,**linkargs):
432 '''
433 Bring link(s) between two nodes up or down
434 '''
435 main.log.info('Bring link(s) between two nodes up or down')
436 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
437 end1 = args["END1"] if args["END1"] != None else ""
438 end2 = args["END2"] if args["END2"] != None else ""
439 option = args["OPTION"] if args["OPTION"] != None else ""
440 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
Jon Hall6094a362014-04-11 14:46:56 -0700441 try:
Jon Halle80ef8c2014-04-29 15:29:13 -0700442 #response = self.execute(cmd=command,prompt="mininet>",timeout=10)
443 self.handle.sendline(command)
444 self.handle.expect("mininet>")
Jon Hall6094a362014-04-11 14:46:56 -0700445 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()
adminbae64d82013-08-01 10:50:15 -0700450 return main.TRUE
451
452
admin530b4c92013-08-14 16:54:35 -0700453 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700454 '''
admin530b4c92013-08-14 16:54:35 -0700455 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700456 '''
admin530b4c92013-08-14 16:54:35 -0700457 main.log.info('Yank the switch interface attached to a host')
458 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700459 sw = args["SW"] if args["SW"] !=None else ""
460 intf = args["INTF"] if args["INTF"] != None else ""
461 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700462 try:
463 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
464 except pexpect.EOF:
465 main.log.error(self.name + ": EOF exception found")
466 main.log.error(self.name + ": " + self.handle.before)
467 main.cleanup()
468 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700469 return main.TRUE
470
471 def plug(self, **plugargs):
472 '''
473 plug the yanked mininet switch interface to a switch
474 '''
475 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700476 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700477 sw = args["SW"] if args["SW"] !=None else ""
478 intf = args["INTF"] if args["INTF"] != None else ""
479 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700480 try:
481 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
482 except pexpect.EOF:
483 main.log.error(self.name + ": EOF exception found")
484 main.log.error(self.name + ": " + self.handle.before)
485 main.cleanup()
486 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700487 return main.TRUE
488
489
490
adminbae64d82013-08-01 10:50:15 -0700491 def dpctl(self,**dpctlargs):
492 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700493 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700494 '''
495 main.log.info('Run dpctl command on all switches')
496 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
497 cmd = args["CMD"] if args["CMD"] != None else ""
498 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
499 command = "dpctl "+cmd + " " + str(cmdargs)
Jon Hall6094a362014-04-11 14:46:56 -0700500 try:
501 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
502 except pexpect.EOF:
503 main.log.error(self.name + ": EOF exception found")
504 main.log.error(self.name + ": " + self.handle.before)
505 main.cleanup()
506 main.exit()
adminbae64d82013-08-01 10:50:15 -0700507 return main.TRUE
508
509
510 def get_version(self):
511 file_input = path+'/lib/Mininet/INSTALL'
512 version = super(Mininet, self).get_version()
513 pattern = 'Mininet\s\w\.\w\.\w\w*'
514 for line in open(file_input,'r').readlines():
515 result = re.match(pattern, line)
516 if result:
517 version = result.group(0)
518 return version
519
admin2a9548d2014-06-17 14:08:07 -0700520 def get_sw_controller_sanity(self, sw):
521 command = "sh ovs-vsctl get-controller "+str(sw)
522 try:
523 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
524 if response:
525 return main.TRUE
526 else:
527 return main.FALSE
528 except pexpect.EOF:
529 main.log.error(self.name + ": EOF exception found")
530 main.log.error(self.name + ": " + self.handle.before)
531 main.cleanup()
532 main.exit()
533 else:
534 main.log.info(response)
535
adminbae64d82013-08-01 10:50:15 -0700536 def get_sw_controller(self,sw):
537 command = "sh ovs-vsctl get-controller "+str(sw)
Jon Hall6094a362014-04-11 14:46:56 -0700538 try:
Jon Hallbd795bf2014-06-18 09:46:32 -0700539 self.handle.expect("mininet")
Jon Hall6094a362014-04-11 14:46:56 -0700540 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
admin2a9548d2014-06-17 14:08:07 -0700541 print(response)
542 if response:
543 return response
544 else:
545 return main.FALSE
Jon Hall6094a362014-04-11 14:46:56 -0700546 except pexpect.EOF:
547 main.log.error(self.name + ": EOF exception found")
548 main.log.error(self.name + ": " + self.handle.before)
549 main.cleanup()
550 main.exit()
551 else:
552 main.log.info(response)
adminbae64d82013-08-01 10:50:15 -0700553
554 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700555 '''
556 count is only needed if there is more than 1 controller
557 '''
558 args = utilities.parse_args(["COUNT"],**kwargs)
559 count = args["COUNT"] if args!={} else 1
560
561 argstring = "SW"
562 for j in range(count):
563 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
564 args = utilities.parse_args(argstring.split(","),**kwargs)
565
adminbae64d82013-08-01 10:50:15 -0700566 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700567 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700568 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
569
570 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
571 for j in range(count):
572 i=j+1
573 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
574 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
575 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
576 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
577 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700578 try:
579 self.execute(cmd=command,prompt="mininet>",timeout=5)
580 except pexpect.EOF:
581 main.log.error(self.name + ": EOF exception found")
582 main.log.error(self.name + ": " + self.handle.before)
583 main.cleanup()
584 main.exit()
585 except:
586 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
587 main.log.error( traceback.print_exc() )
588 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
589 main.cleanup()
590 main.exit()
adminbae64d82013-08-01 10:50:15 -0700591
Jon Hall0819fd92014-05-23 12:08:13 -0700592 def delete_sw_controller(self,sw):
593 '''
594 Removes the controller target from sw
595 '''
596
597 command = "sh ovs-vsctl del-controller "+str(sw)
598 try:
599 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
600 except pexpect.EOF:
601 main.log.error(self.name + ": EOF exception found")
602 main.log.error(self.name + ": " + self.handle.before)
603 main.cleanup()
604 main.exit()
605 else:
606 main.log.info(response)
607
608
adminbae64d82013-08-01 10:50:15 -0700609 def disconnect(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700610 main.log.info(self.name+": Disconnecting mininet...")
adminbae64d82013-08-01 10:50:15 -0700611 response = ''
612 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700613 try:
614 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
615 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
Jon Halle80ef8c2014-04-29 15:29:13 -0700616 self.handle.sendline("sudo mn -c")
Jon Hall6094a362014-04-11 14:46:56 -0700617 except pexpect.EOF:
618 main.log.error(self.name + ": EOF exception found")
619 main.log.error(self.name + ": " + self.handle.before)
620 main.cleanup()
621 main.exit()
adminbae64d82013-08-01 10:50:15 -0700622 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700623 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700624 response = main.FALSE
625 return response
admin07529932013-11-22 14:58:28 -0800626
627 def arping(self, src, dest, destmac):
628 self.handle.sendline('')
Jon Hall333fa8c2014-04-11 11:24:58 -0700629 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800630
631 self.handle.sendline(src + ' arping ' + dest)
632 try:
Jon Hall333fa8c2014-04-11 11:24:58 -0700633 self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT])
Jon Hallf2942ce2014-04-10 16:00:16 -0700634 main.log.info(self.name+": ARP successful")
Jon Hall333fa8c2014-04-11 11:24:58 -0700635 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800636 return main.TRUE
637 except:
Jon Hallf2942ce2014-04-10 16:00:16 -0700638 main.log.warn(self.name+": ARP FAILURE")
Jon Hall333fa8c2014-04-11 11:24:58 -0700639 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800640 return main.FALSE
641
642 def decToHex(num):
643 return hex(num).split('x')[1]
admin2a9548d2014-06-17 14:08:07 -0700644
645 def getSwitchFlowCount(self, switch):
646 '''
647 return the Flow Count of the switch
648 '''
649 if self.handle:
650 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
651 try:
652 response = self.execute(cmd=cmd, prompt="mininet>", timeout=10)
653 except pexpect.EOF:
654 main.log.error(self.name + ": EOF exception found")
655 main.log.error(self.name + " " + self.handle.before)
656 main.cleanup()
657 main.exit()
658 pattern = "flow_count=(\d+)"
659 result = re.search(pattern, response, re.MULTILINE)
660 if result is None:
661 print "no flow on switch print test"
662 main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response))
663 return main.FALSE
664 return result.group(1)
665 else:
666 main.log.error("Connection failed to the Mininet host")
667
668 def check_flows(self, sw):
669 command = "sh ovs-ofctl dump-flows "+str(sw)
670 try:
671 response=self.execute(cmd=command,prompt="mininet>",timeout=10)
672 return response
673 except pexpect.EOF:
674 main.log.error(self.name + ": EOF exception found")
675 main.log.error(self.name + ": " + self.handle.before)
676 main.cleanup()
677 main.exit()
678 else:
679 main.log.info(response)
680
681 def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"):
682 '''
683 Runs tpdump on an intferface and saves the file
684 intf can be specified, or the default eth0 is used
685 '''
686 try:
687 self.handle.sendline("")
688 self.handle.expect("mininet>")
689 self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &")
690 self.handle.sendline("")
691 self.handle.sendline("")
692 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10)
693 main.log.warn(self.handle.before + self.handle.after)
694 if i == 0:
695 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
696 return main.FALSE
697 elif i == 1:
698 main.log.info(self.name + ": tcpdump started on " + intf)
699 return main.TRUE
700 elif i == 2:
701 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
702 return main.FALSE
703 elif i ==3:
704 main.log.info(self.name +": " + self.handle.before)
705 return main.TRUE
706 else:
707 main.log.error(self.name + ": tcpdump - unexpected response")
708 return main.FALSE
709 except pexpect.EOF:
710 main.log.error(self.name + ": EOF exception found")
711 main.log.error(self.name + ": " + self.handle.before)
712 main.cleanup()
713 main.exit()
714 except:
715 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
716 main.log.error( traceback.print_exc() )
717 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
718 main.cleanup()
719 main.exit()
720
721 def stop_tcpdump(self):
722 "pkills tcpdump"
723 try:
724 self.handle.sendline("sh sudo pkill tcpdump")
725 self.handle.sendline("")
726 self.handle.sendline("")
727 self.handle.expect("mininet>")
728 except pexpect.EOF:
729 main.log.error(self.name + ": EOF exception found")
730 main.log.error(self.name + ": " + self.handle.before)
731 main.cleanup()
732 main.exit()
733 except:
734 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
735 main.log.error( traceback.print_exc() )
736 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
737 main.cleanup()
738 main.exit()
739
740
741
742
743
744
adminbae64d82013-08-01 10:50:15 -0700745
746if __name__ != "__main__":
747 import sys
748 sys.modules[__name__] = MininetCliDriver()
admin2a9548d2014-06-17 14:08:07 -0700749