blob: dea8340e51713ebb300c028da3ad39aee12d83ec [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'''
24
25import 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 Hall6e18c7b2014-04-23 16:26:33 -0700158 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i .2 -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 '''
192 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700193 try:
194 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
195 except pexpect.EOF:
196 main.log.error(self.name + ": EOF exception found")
197 main.log.error(self.name + ": " + self.handle.before)
198 main.cleanup()
199 main.exit()
adminbae64d82013-08-01 10:50:15 -0700200
201 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})"
202 #pattern = "inet\saddr:10.0.0.6"
Jon Hallf2942ce2014-04-10 16:00:16 -0700203 #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
204 if re.search(pattern,response):
205 main.log.info(self.name+": Host Ip configured properly")
adminbae64d82013-08-01 10:50:15 -0700206 return main.TRUE
207 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700208 main.log.error(self.name+": Host IP not found")
adminbae64d82013-08-01 10:50:15 -0700209 return main.FALSE
210 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700211 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700212
213 def verifySSH(self,**connectargs):
Jon Hall6094a362014-04-11 14:46:56 -0700214 try:
215 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
216 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
217 for key in connectargs:
218 vars(self)[key] = connectargs[key]
219 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
220 except pexpect.EOF:
221 main.log.error(self.name + ": EOF exception found")
222 main.log.error(self.name + ": " + self.handle.before)
223 main.cleanup()
224 main.exit()
adminbae64d82013-08-01 10:50:15 -0700225 import time
226 time.sleep(20)
227 if self.flag == 0:
228 self.flag = 1
229 return main.FALSE
230 else :
231 return main.TRUE
232
233 def getMacAddress(self,host):
234 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700235 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700236 '''
237 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700238 try:
239 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
240 except pexpect.EOF:
241 main.log.error(self.name + ": EOF exception found")
242 main.log.error(self.name + ": " + self.handle.before)
243 main.cleanup()
244 main.exit()
adminbae64d82013-08-01 10:50:15 -0700245
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700246 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
247 mac_address_search = re.search(pattern, response, re.I)
248 mac_address = mac_address_search.group().split(" ")[1]
Jon Hallf2942ce2014-04-10 16:00:16 -0700249 main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address)
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700250 return mac_address
adminbae64d82013-08-01 10:50:15 -0700251 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700252 main.log.error(self.name+": Connection failed to the host")
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700253
254 def getInterfaceMACAddress(self,host, interface):
255 '''
256 Return the IP address of the interface on the given host
257 '''
258 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700259 try:
260 response = self.execute(cmd=host+" ifconfig " + interface,
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700261 prompt="mininet>",timeout=10)
Jon Hall6094a362014-04-11 14:46:56 -0700262 except pexpect.EOF:
263 main.log.error(self.name + ": EOF exception found")
264 main.log.error(self.name + ": " + self.handle.before)
265 main.cleanup()
266 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700267
268 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
269 mac_address_search = re.search(pattern, response, re.I)
270 if mac_address_search is None:
271 main.log.info("No mac address found in %s" % response)
272 return main.FALSE
273 mac_address = mac_address_search.group().split(" ")[1]
274 main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address)
275 return mac_address
276 else:
277 main.log.error("Connection failed to the host")
278
adminbae64d82013-08-01 10:50:15 -0700279 def getIPAddress(self,host):
280 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700281 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700282 '''
283 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700284 try:
285 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
286 except pexpect.EOF:
287 main.log.error(self.name + ": EOF exception found")
288 main.log.error(self.name + ": " + self.handle.before)
289 main.cleanup()
290 main.exit()
adminbae64d82013-08-01 10:50:15 -0700291
292 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
293 ip_address_search = re.search(pattern, response)
Jon Hallf2942ce2014-04-10 16:00:16 -0700294 main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1))
adminbae64d82013-08-01 10:50:15 -0700295 return ip_address_search.group(1)
296 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700297 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700298
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700299 def getSwitchDPID(self,switch):
300 '''
301 return the datapath ID of the switch
302 '''
303 if self.handle :
304 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700305 try:
306 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
307 except pexpect.EOF:
308 main.log.error(self.name + ": EOF exception found")
309 main.log.error(self.name + ": " + self.handle.before)
310 main.cleanup()
311 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700312 pattern = r'^(?P<dpid>\d)+'
313 result = re.search(pattern, response, re.MULTILINE)
314 if result is None:
315 main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response))
316 return main.FALSE
317 return int(result.group('dpid'))
318 else:
319 main.log.error("Connection failed to the host")
320
321 def getInterfaces(self, node):
322 '''
323 return information dict about interfaces connected to the node
324 '''
325 if self.handle :
326 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
327 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700328 try:
329 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
330 except pexpect.EOF:
331 main.log.error(self.name + ": EOF exception found")
332 main.log.error(self.name + ": " + self.handle.before)
333 main.cleanup()
334 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700335 return response
336 else:
337 main.log.error("Connection failed to the node")
338
adminbae64d82013-08-01 10:50:15 -0700339 def dump(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700340 main.log.info(self.name+": Dump node info")
Jon Hall6094a362014-04-11 14:46:56 -0700341 try:
342 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
343 except pexpect.EOF:
344 main.log.error(self.name + ": EOF exception found")
345 main.log.error(self.name + ": " + self.handle.before)
346 main.cleanup()
347 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700348 return response
adminbae64d82013-08-01 10:50:15 -0700349
350 def intfs(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700351 main.log.info(self.name+": List interfaces")
Jon Hall6094a362014-04-11 14:46:56 -0700352 try:
353 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
354 except pexpect.EOF:
355 main.log.error(self.name + ": EOF exception found")
356 main.log.error(self.name + ": " + self.handle.before)
357 main.cleanup()
358 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700359 return response
adminbae64d82013-08-01 10:50:15 -0700360
361 def net(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700362 main.log.info(self.name+": List network connections")
Jon Hall6094a362014-04-11 14:46:56 -0700363 try:
364 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
365 except pexpect.EOF:
366 main.log.error(self.name + ": EOF exception found")
367 main.log.error(self.name + ": " + self.handle.before)
368 main.cleanup()
369 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700370 return response
adminbae64d82013-08-01 10:50:15 -0700371
372 def iperf(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700373 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700374 try:
375 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
376 except pexpect.EOF:
377 main.log.error(self.name + ": EOF exception found")
378 main.log.error(self.name + ": " + self.handle.before)
379 main.cleanup()
380 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700381 return response
adminbae64d82013-08-01 10:50:15 -0700382
383 def iperfudp(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700384 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700385 try:
386 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
387 except pexpect.EOF:
388 main.log.error(self.name + ": EOF exception found")
389 main.log.error(self.name + ": " + self.handle.before)
390 main.cleanup()
391 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700392 return response
adminbae64d82013-08-01 10:50:15 -0700393
394 def nodes(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700395 main.log.info(self.name+": List all nodes.")
Jon Hall6094a362014-04-11 14:46:56 -0700396 try:
397 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
398 except pexpect.EOF:
399 main.log.error(self.name + ": EOF exception found")
400 main.log.error(self.name + ": " + self.handle.before)
401 main.cleanup()
402 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700403 return response
adminbae64d82013-08-01 10:50:15 -0700404
405 def pingpair(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700406 main.log.info(self.name+": Ping between first two hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700407 try:
408 response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
409 except pexpect.EOF:
410 main.log.error(self.name + ": EOF exception found")
411 main.log.error(self.name + ": " + self.handle.before)
412 main.cleanup()
413 main.exit()
adminbae64d82013-08-01 10:50:15 -0700414
Jon Hallf2942ce2014-04-10 16:00:16 -0700415 #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
416 if re.search(',\s0\%\spacket\sloss',response):
417 main.log.info(self.name+": Ping between two hosts SUCCESSFUL")
adminbae64d82013-08-01 10:50:15 -0700418 main.last_result = main.TRUE
419 return main.TRUE
420 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700421 main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700422 main.last_result = main.FALSE
423 return main.FALSE
424
425 def link(self,**linkargs):
426 '''
427 Bring link(s) between two nodes up or down
428 '''
429 main.log.info('Bring link(s) between two nodes up or down')
430 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
431 end1 = args["END1"] if args["END1"] != None else ""
432 end2 = args["END2"] if args["END2"] != None else ""
433 option = args["OPTION"] if args["OPTION"] != None else ""
434 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
Jon Hall6094a362014-04-11 14:46:56 -0700435 try:
436 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
437 except pexpect.EOF:
438 main.log.error(self.name + ": EOF exception found")
439 main.log.error(self.name + ": " + self.handle.before)
440 main.cleanup()
441 main.exit()
adminbae64d82013-08-01 10:50:15 -0700442 return main.TRUE
443
444
admin530b4c92013-08-14 16:54:35 -0700445 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700446 '''
admin530b4c92013-08-14 16:54:35 -0700447 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700448 '''
admin530b4c92013-08-14 16:54:35 -0700449 main.log.info('Yank the switch interface attached to a host')
450 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700451 sw = args["SW"] if args["SW"] !=None else ""
452 intf = args["INTF"] if args["INTF"] != None else ""
453 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700454 try:
455 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
456 except pexpect.EOF:
457 main.log.error(self.name + ": EOF exception found")
458 main.log.error(self.name + ": " + self.handle.before)
459 main.cleanup()
460 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700461 return main.TRUE
462
463 def plug(self, **plugargs):
464 '''
465 plug the yanked mininet switch interface to a switch
466 '''
467 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700468 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700469 sw = args["SW"] if args["SW"] !=None else ""
470 intf = args["INTF"] if args["INTF"] != None else ""
471 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700472 try:
473 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
474 except pexpect.EOF:
475 main.log.error(self.name + ": EOF exception found")
476 main.log.error(self.name + ": " + self.handle.before)
477 main.cleanup()
478 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700479 return main.TRUE
480
481
482
adminbae64d82013-08-01 10:50:15 -0700483 def dpctl(self,**dpctlargs):
484 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700485 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700486 '''
487 main.log.info('Run dpctl command on all switches')
488 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
489 cmd = args["CMD"] if args["CMD"] != None else ""
490 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
491 command = "dpctl "+cmd + " " + str(cmdargs)
Jon Hall6094a362014-04-11 14:46:56 -0700492 try:
493 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
494 except pexpect.EOF:
495 main.log.error(self.name + ": EOF exception found")
496 main.log.error(self.name + ": " + self.handle.before)
497 main.cleanup()
498 main.exit()
adminbae64d82013-08-01 10:50:15 -0700499 return main.TRUE
500
501
502 def get_version(self):
503 file_input = path+'/lib/Mininet/INSTALL'
504 version = super(Mininet, self).get_version()
505 pattern = 'Mininet\s\w\.\w\.\w\w*'
506 for line in open(file_input,'r').readlines():
507 result = re.match(pattern, line)
508 if result:
509 version = result.group(0)
510 return version
511
512 def get_sw_controller(self,sw):
513 command = "sh ovs-vsctl get-controller "+str(sw)
Jon Hall6094a362014-04-11 14:46:56 -0700514 try:
515 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
516 except pexpect.EOF:
517 main.log.error(self.name + ": EOF exception found")
518 main.log.error(self.name + ": " + self.handle.before)
519 main.cleanup()
520 main.exit()
521 else:
522 main.log.info(response)
adminbae64d82013-08-01 10:50:15 -0700523
524 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700525 '''
526 count is only needed if there is more than 1 controller
527 '''
528 args = utilities.parse_args(["COUNT"],**kwargs)
529 count = args["COUNT"] if args!={} else 1
530
531 argstring = "SW"
532 for j in range(count):
533 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
534 args = utilities.parse_args(argstring.split(","),**kwargs)
535
adminbae64d82013-08-01 10:50:15 -0700536 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700537 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700538 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
539
540 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
541 for j in range(count):
542 i=j+1
543 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
544 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
545 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
546 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
547 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700548 try:
549 self.execute(cmd=command,prompt="mininet>",timeout=5)
550 except pexpect.EOF:
551 main.log.error(self.name + ": EOF exception found")
552 main.log.error(self.name + ": " + self.handle.before)
553 main.cleanup()
554 main.exit()
555 except:
556 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
557 main.log.error( traceback.print_exc() )
558 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
559 main.cleanup()
560 main.exit()
adminbae64d82013-08-01 10:50:15 -0700561
562 def disconnect(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700563 main.log.info(self.name+": Disconnecting mininet...")
adminbae64d82013-08-01 10:50:15 -0700564 response = ''
565 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700566 try:
567 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
568 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
569 except pexpect.EOF:
570 main.log.error(self.name + ": EOF exception found")
571 main.log.error(self.name + ": " + self.handle.before)
572 main.cleanup()
573 main.exit()
adminbae64d82013-08-01 10:50:15 -0700574 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700575 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700576 response = main.FALSE
577 return response
admin07529932013-11-22 14:58:28 -0800578
579 def arping(self, src, dest, destmac):
580 self.handle.sendline('')
Jon Hall333fa8c2014-04-11 11:24:58 -0700581 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800582
583 self.handle.sendline(src + ' arping ' + dest)
584 try:
Jon Hall333fa8c2014-04-11 11:24:58 -0700585 self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT])
Jon Hallf2942ce2014-04-10 16:00:16 -0700586 main.log.info(self.name+": ARP successful")
Jon Hall333fa8c2014-04-11 11:24:58 -0700587 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800588 return main.TRUE
589 except:
Jon Hallf2942ce2014-04-10 16:00:16 -0700590 main.log.warn(self.name+": ARP FAILURE")
Jon Hall333fa8c2014-04-11 11:24:58 -0700591 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800592 return main.FALSE
593
594 def decToHex(num):
595 return hex(num).split('x')[1]
adminbae64d82013-08-01 10:50:15 -0700596
597if __name__ != "__main__":
598 import sys
599 sys.modules[__name__] = MininetCliDriver()