blob: e2570acb60df05d23ec3c732bd39972c602fb717 [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("../")
Jon Hall1ccf82c2014-10-15 14:55:16 -040034from math import pow
adminbae64d82013-08-01 10:50:15 -070035from drivers.common.cli.emulatordriver import Emulator
36from drivers.common.clidriver import CLI
37
38class MininetCliDriver(Emulator):
39 '''
Jon Hall41f40e82014-04-08 16:43:17 -070040 MininetCliDriver is the basic driver which will handle the Mininet functions
adminbae64d82013-08-01 10:50:15 -070041 '''
42 def __init__(self):
43 super(Emulator, self).__init__()
44 self.handle = self
45 self.wrapped = sys.modules[__name__]
46 self.flag = 0
47
48 def connect(self, **connectargs):
Jon Hall41f40e82014-04-08 16:43:17 -070049 '''
50 Here the main is the TestON instance after creating all the log handles.
51 '''
adminbae64d82013-08-01 10:50:15 -070052 for key in connectargs:
53 vars(self)[key] = connectargs[key]
54
55 self.name = self.options['name']
56 self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd)
57
58 self.ssh_handle = self.handle
59
adminbae64d82013-08-01 10:50:15 -070060 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -070061 main.log.info(self.name+": Clearing any residual state or processes")
adminbae64d82013-08-01 10:50:15 -070062 self.handle.sendline("sudo mn -c")
adminf939f8b2014-04-03 17:22:56 -070063 i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070064 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070065 main.log.info(self.name+": Sending sudo password")
adminf939f8b2014-04-03 17:22:56 -070066 self.handle.sendline(self.pwd)
67 i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070068 if i==1:
Jon Hallf2942ce2014-04-10 16:00:16 -070069 main.log.info(self.name+": Clean")
adminbae64d82013-08-01 10:50:15 -070070 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070071 main.log.error(self.name+": Connection terminated")
adminbae64d82013-08-01 10:50:15 -070072 elif i==3: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -070073 main.log.error(self.name+": Something while cleaning MN took too long... " )
adminbae64d82013-08-01 10:50:15 -070074
Jon Hallf2942ce2014-04-10 16:00:16 -070075 main.log.info(self.name+": building fresh mininet")
adminbeea0032014-01-23 14:54:13 -080076 #### for reactive/PARP enabled tests
shahshreyaf4d4d0c2014-10-10 12:11:10 -070077 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller'] + " " + self.options['arg3']
Jon Hall1ccf82c2014-10-15 14:55:16 -040078
79 argList = self.options['arg1'].split(",")
80 global topoArgList
81 topoArgList = argList[0].split(" ")
82 argList = map(int, argList[1:])
83 topoArgList = topoArgList[1:] + argList
84
85 #### for proactive flow with static ARP entries
shahshreyaf4d4d0c2014-10-10 12:11:10 -070086 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller'] + " " + self.options['arg3']
adminbae64d82013-08-01 10:50:15 -070087 self.handle.sendline(cmdString)
Jon Hall333fa8c2014-04-11 11:24:58 -070088 self.handle.expect(["sudo mn",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070089 while 1:
90 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
91 if i==0:
Jon Hallf2942ce2014-04-10 16:00:16 -070092 main.log.info(self.name+": mininet built")
adminbae64d82013-08-01 10:50:15 -070093 return main.TRUE
94 if i==1:
Jon Hall1645caa2014-11-18 16:27:14 -050095 self.handle.expect(["\n",pexpect.EOF,pexpect.TIMEOUT])
adminbae64d82013-08-01 10:50:15 -070096 main.log.info(self.handle.before)
97 elif i==2:
Jon Hallf2942ce2014-04-10 16:00:16 -070098 main.log.error(self.name+": Launching mininet failed...")
adminbae64d82013-08-01 10:50:15 -070099 return main.FALSE
100 elif i==3:
Jon Hallf2942ce2014-04-10 16:00:16 -0700101 main.log.error(self.name+": Connection timeout")
adminbae64d82013-08-01 10:50:15 -0700102 return main.FALSE
103 elif i==4: #timeout
Jon Hallf2942ce2014-04-10 16:00:16 -0700104 main.log.error(self.name+": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -0700105 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700106 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
107 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700108 else:#if no handle
109 main.log.error(self.name+": Connection failed to the host "+self.user_name+"@"+self.ip_address)
110 main.log.error(self.name+": Failed to connect to the Mininet")
adminbae64d82013-08-01 10:50:15 -0700111 return main.FALSE
Jon Hall1ccf82c2014-10-15 14:55:16 -0400112
113 def num_switches_n_links(self,topoType,depth,fanout):
114 if topoType == 'tree':
115 if fanout is None: #In tree topology, if fanout arg is not given, by default it is 2
116 fanout = 2
117 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500118 count = 0
Jon Hall1ccf82c2014-10-15 14:55:16 -0400119 while(k <= depth-1):
Jon Hall38481722014-11-04 16:50:05 -0500120 count = count + pow(fanout,k)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400121 k = k+1
Jon Hall38481722014-11-04 16:50:05 -0500122 num_switches = count
Jon Hall1ccf82c2014-10-15 14:55:16 -0400123 while(k <= depth-2):
124 '''depth-2 gives you only core links and not considering edge links as seen by ONOS
125 If all the links including edge links are required, do depth-1
126 '''
Jon Hall38481722014-11-04 16:50:05 -0500127 count = count + pow(fanout,k)
Jon Hall1ccf82c2014-10-15 14:55:16 -0400128 k = k+1
Jon Hall38481722014-11-04 16:50:05 -0500129 num_links = count * fanout
Jon Hall1ccf82c2014-10-15 14:55:16 -0400130 #print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
131
132 elif topoType =='linear':
133 if fanout is None: #In linear topology, if fanout or num_hosts_per_sw is not given, by default it is 1
134 fanout = 1
135 num_switches = depth
136 num_hosts_per_sw = fanout
137 total_num_hosts = num_switches * num_hosts_per_sw
138 num_links = total_num_hosts + (num_switches - 1)
139 print "num_switches for %s(%d,%d) = %d and links=%d" %(topoType,depth,fanout,num_switches,num_links)
140 topoDict = {}
141 topoDict = {"num_switches":int(num_switches), "num_corelinks":int(num_links)}
142 return topoDict
143
144
145 def calculate_sw_and_links(self):
146 topoDict = self.num_switches_n_links(*topoArgList)
147 return topoDict
148
Jon Hall1645caa2014-11-18 16:27:14 -0500149 def pingall(self, timeout=300):
adminbae64d82013-08-01 10:50:15 -0700150 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700151 Verifies the reachability of the hosts using pingall command.
Jon Hall1645caa2014-11-18 16:27:14 -0500152 Optional parameter timeout allows you to specify how long to wait for pingall to complete
153 Returns:
154 main.TRUE if pingall completes with no pings dropped
155 otherwise main.FALSE
adminbae64d82013-08-01 10:50:15 -0700156 '''
157 if self.handle :
Jon Hallf2942ce2014-04-10 16:00:16 -0700158 main.log.info(self.name+": Checking reachabilty to the hosts using pingall")
Jon Hall6094a362014-04-11 14:46:56 -0700159 try:
Jon Hall1645caa2014-11-18 16:27:14 -0500160 response = self.execute(cmd="pingall",prompt="mininet>",timeout=int(timeout))
Jon Hallb1290e82014-11-18 16:17:48 -0500161 except pexpect.EOF:
Jon Hall6094a362014-04-11 14:46:56 -0700162 main.log.error(self.name + ": EOF exception found")
163 main.log.error(self.name + ": " + self.handle.before)
Jon Hallb1290e82014-11-18 16:17:48 -0500164 main.cleanup()
165 main.exit()
166 except pexpect.TIMEOUT:
167 #We may not want to kill the test if pexpect times out
168 main.log.error(self.name + ": TIMEOUT exception found")
169 main.log.error(self.name + ": " + str(self.handle.before) )
170 #NOTE: mininet's pingall rounds, so we will check the number of passed and number of failed
171 pattern = "Results\:\s0\%\sdropped\s\((?P<passed>[\d]+)/(?P=passed)"
Jon Hallf2942ce2014-04-10 16:00:16 -0700172 if re.search(pattern,response):
173 main.log.info(self.name+": All hosts are reachable")
adminbae64d82013-08-01 10:50:15 -0700174 return main.TRUE
175 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700176 main.log.error(self.name+": Unable to reach all the hosts")
Jon Hallb1290e82014-11-18 16:17:48 -0500177 main.log.info("Pingall ouput: " + str(response))
178 #NOTE: Send ctrl-c to make sure pingall is done
179 self.handle.send("\x03")
180 self.handle.expect("Interrupt")
181 self.handle.expect("mininet>")
adminbae64d82013-08-01 10:50:15 -0700182 return main.FALSE
183 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700184 main.log.error(self.name+": Connection failed to the host")
Jon Hallb1290e82014-11-18 16:17:48 -0500185 main.cleanup()
186 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700187
188 def fpingHost(self,**pingParams):
189 '''
190 Uses the fping package for faster pinging...
191 *requires fping to be installed on machine running mininet
192 '''
193 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700194 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700195 self.handle.sendline(command)
Jon Hall333fa8c2014-04-11 11:24:58 -0700196 self.handle.expect([args["TARGET"],pexpect.EOF,pexpect.TIMEOUT])
197 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
adminaeedddd2013-08-02 15:14:15 -0700198 response = self.handle.before
199 if re.search(":\s-" ,response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700200 main.log.info(self.name+": Ping fail")
adminaeedddd2013-08-02 15:14:15 -0700201 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700202 elif re.search(":\s\d{1,2}\.\d\d", response):
Jon Hallf2942ce2014-04-10 16:00:16 -0700203 main.log.info(self.name+": Ping good!")
adminaeedddd2013-08-02 15:14:15 -0700204 return main.TRUE
Jon Hallf2942ce2014-04-10 16:00:16 -0700205 main.log.info(self.name+": Install fping on mininet machine... ")
206 main.log.info(self.name+": \n---\n"+response)
adminaeedddd2013-08-02 15:14:15 -0700207 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700208
209 def pingHost(self,**pingParams):
Jon Hallf2942ce2014-04-10 16:00:16 -0700210 '''
211 Ping from one mininet host to another
212 Currently the only supported Params: SRC and TARGET
213 '''
adminbae64d82013-08-01 10:50:15 -0700214 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
215 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
Jon Hall0819fd92014-05-23 12:08:13 -0700216 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700217 try:
Jon Hall6e18c7b2014-04-23 16:26:33 -0700218 main.log.warn("Sending: " + command)
219 #response = self.execute(cmd=command,prompt="mininet",timeout=10 )
220 self.handle.sendline(command)
221 i = self.handle.expect([command,pexpect.TIMEOUT])
222 if i == 1:
223 main.log.error(self.name + ": timeout when waiting for response from mininet")
224 main.log.error("response: " + str(self.handle.before))
225 i = self.handle.expect(["mininet>",pexpect.TIMEOUT])
226 if i == 1:
227 main.log.error(self.name + ": timeout when waiting for response from mininet")
228 main.log.error("response: " + str(self.handle.before))
229 response = self.handle.before
Jon Hall6094a362014-04-11 14:46:56 -0700230 except pexpect.EOF:
231 main.log.error(self.name + ": EOF exception found")
232 main.log.error(self.name + ": " + self.handle.before)
233 main.cleanup()
234 main.exit()
Jon Hallf2942ce2014-04-10 16:00:16 -0700235 main.log.info(self.name+": Ping Response: "+ response )
236 #if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
237 if re.search(',\s0\%\spacket\sloss',response):
Jon Hall6e18c7b2014-04-23 16:26:33 -0700238 main.log.info(self.name+": no packets lost, host is reachable")
adminbae64d82013-08-01 10:50:15 -0700239 main.last_result = main.TRUE
240 return main.TRUE
241 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700242 main.log.error(self.name+": PACKET LOST, HOST IS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700243 main.last_result = main.FALSE
244 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700245
246 def checkIP(self,host):
247 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700248 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700249 '''
250 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700251 try:
252 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
253 except pexpect.EOF:
254 main.log.error(self.name + ": EOF exception found")
255 main.log.error(self.name + ": " + self.handle.before)
256 main.cleanup()
257 main.exit()
adminbae64d82013-08-01 10:50:15 -0700258
259 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 -0700260 #pattern = "inet addr:10.0.0.6"
Jon Hallf2942ce2014-04-10 16:00:16 -0700261 #if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
262 if re.search(pattern,response):
263 main.log.info(self.name+": Host Ip configured properly")
adminbae64d82013-08-01 10:50:15 -0700264 return main.TRUE
265 else:
Jon Hallf2942ce2014-04-10 16:00:16 -0700266 main.log.error(self.name+": Host IP not found")
adminbae64d82013-08-01 10:50:15 -0700267 return main.FALSE
268 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700269 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700270
271 def verifySSH(self,**connectargs):
Jon Hall6094a362014-04-11 14:46:56 -0700272 try:
273 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
274 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
275 for key in connectargs:
276 vars(self)[key] = connectargs[key]
277 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
278 except pexpect.EOF:
279 main.log.error(self.name + ": EOF exception found")
280 main.log.error(self.name + ": " + self.handle.before)
281 main.cleanup()
282 main.exit()
adminbae64d82013-08-01 10:50:15 -0700283 import time
284 time.sleep(20)
285 if self.flag == 0:
286 self.flag = 1
287 return main.FALSE
288 else :
289 return main.TRUE
290
291 def getMacAddress(self,host):
292 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700293 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700294 '''
295 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700296 try:
297 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
298 except pexpect.EOF:
299 main.log.error(self.name + ": EOF exception found")
300 main.log.error(self.name + ": " + self.handle.before)
301 main.cleanup()
302 main.exit()
adminbae64d82013-08-01 10:50:15 -0700303
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700304 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
305 mac_address_search = re.search(pattern, response, re.I)
306 mac_address = mac_address_search.group().split(" ")[1]
Jon Hallf2942ce2014-04-10 16:00:16 -0700307 main.log.info(self.name+": Mac-Address of Host "+ host + " is " + mac_address)
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700308 return mac_address
adminbae64d82013-08-01 10:50:15 -0700309 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700310 main.log.error(self.name+": Connection failed to the host")
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700311
312 def getInterfaceMACAddress(self,host, interface):
313 '''
314 Return the IP address of the interface on the given host
315 '''
316 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700317 try:
318 response = self.execute(cmd=host+" ifconfig " + interface,
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700319 prompt="mininet>",timeout=10)
Jon Hall6094a362014-04-11 14:46:56 -0700320 except pexpect.EOF:
321 main.log.error(self.name + ": EOF exception found")
322 main.log.error(self.name + ": " + self.handle.before)
323 main.cleanup()
324 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700325
326 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
327 mac_address_search = re.search(pattern, response, re.I)
328 if mac_address_search is None:
329 main.log.info("No mac address found in %s" % response)
330 return main.FALSE
331 mac_address = mac_address_search.group().split(" ")[1]
332 main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address)
333 return mac_address
334 else:
335 main.log.error("Connection failed to the host")
336
adminbae64d82013-08-01 10:50:15 -0700337 def getIPAddress(self,host):
338 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700339 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700340 '''
341 if self.handle :
Jon Hall6094a362014-04-11 14:46:56 -0700342 try:
343 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
344 except pexpect.EOF:
345 main.log.error(self.name + ": EOF exception found")
346 main.log.error(self.name + ": " + self.handle.before)
347 main.cleanup()
348 main.exit()
adminbae64d82013-08-01 10:50:15 -0700349
350 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
351 ip_address_search = re.search(pattern, response)
Jon Hallf2942ce2014-04-10 16:00:16 -0700352 main.log.info(self.name+": IP-Address of Host "+host +" is "+ip_address_search.group(1))
adminbae64d82013-08-01 10:50:15 -0700353 return ip_address_search.group(1)
354 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700355 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700356
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700357 def getSwitchDPID(self,switch):
358 '''
359 return the datapath ID of the switch
360 '''
361 if self.handle :
362 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700363 try:
364 response = self.execute(cmd=cmd,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()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700370 pattern = r'^(?P<dpid>\d)+'
371 result = re.search(pattern, response, re.MULTILINE)
372 if result is None:
373 main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response))
374 return main.FALSE
Jon Hallc1a1d242014-07-21 16:03:33 -0700375 return str(result.group(0))
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700376 else:
377 main.log.error("Connection failed to the host")
378
admin2580a0e2014-07-29 11:24:34 -0700379 def getDPID(self, switch):
380 if self.handle:
381 self.handle.sendline("")
382 self.expect("mininet>")
383 cmd = "py %s.dpid" %switch
384 try:
385 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
386 self.handle.expect("mininet>")
387 response = self.handle.before
388 return response
389 except pexpect.EOF:
390 main.log.error(self.name + ": EOF exception found")
391 main.log.error(self.name + ": " + self.handle.before)
392 main.cleanup()
393 main.exit()
394
395
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700396 def getInterfaces(self, node):
397 '''
398 return information dict about interfaces connected to the node
399 '''
400 if self.handle :
Jon Hall38481722014-11-04 16:50:05 -0500401 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700402 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700403 try:
404 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
405 except pexpect.EOF:
406 main.log.error(self.name + ": EOF exception found")
407 main.log.error(self.name + ": " + self.handle.before)
408 main.cleanup()
409 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700410 return response
411 else:
412 main.log.error("Connection failed to the node")
413
adminbae64d82013-08-01 10:50:15 -0700414 def dump(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700415 main.log.info(self.name+": Dump node info")
Jon Hall6094a362014-04-11 14:46:56 -0700416 try:
417 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
418 except pexpect.EOF:
419 main.log.error(self.name + ": EOF exception found")
420 main.log.error(self.name + ": " + self.handle.before)
421 main.cleanup()
422 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700423 return response
adminbae64d82013-08-01 10:50:15 -0700424
425 def intfs(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700426 main.log.info(self.name+": List interfaces")
Jon Hall6094a362014-04-11 14:46:56 -0700427 try:
428 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
429 except pexpect.EOF:
430 main.log.error(self.name + ": EOF exception found")
431 main.log.error(self.name + ": " + self.handle.before)
432 main.cleanup()
433 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700434 return response
adminbae64d82013-08-01 10:50:15 -0700435
436 def net(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700437 main.log.info(self.name+": List network connections")
Jon Hall6094a362014-04-11 14:46:56 -0700438 try:
439 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
440 except pexpect.EOF:
441 main.log.error(self.name + ": EOF exception found")
442 main.log.error(self.name + ": " + self.handle.before)
443 main.cleanup()
444 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700445 return response
adminbae64d82013-08-01 10:50:15 -0700446
447 def iperf(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700448 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700449 try:
450 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
451 except pexpect.EOF:
452 main.log.error(self.name + ": EOF exception found")
453 main.log.error(self.name + ": " + self.handle.before)
454 main.cleanup()
455 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700456 return response
adminbae64d82013-08-01 10:50:15 -0700457
458 def iperfudp(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700459 main.log.info(self.name+": Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700460 try:
461 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
462 except pexpect.EOF:
463 main.log.error(self.name + ": EOF exception found")
464 main.log.error(self.name + ": " + self.handle.before)
465 main.cleanup()
466 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700467 return response
adminbae64d82013-08-01 10:50:15 -0700468
469 def nodes(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700470 main.log.info(self.name+": List all nodes.")
Jon Hall6094a362014-04-11 14:46:56 -0700471 try:
472 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
473 except pexpect.EOF:
474 main.log.error(self.name + ": EOF exception found")
475 main.log.error(self.name + ": " + self.handle.before)
476 main.cleanup()
477 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700478 return response
adminbae64d82013-08-01 10:50:15 -0700479
480 def pingpair(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700481 main.log.info(self.name+": Ping between first two hosts")
Jon Hall6094a362014-04-11 14:46:56 -0700482 try:
483 response = self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
484 except pexpect.EOF:
485 main.log.error(self.name + ": EOF exception found")
486 main.log.error(self.name + ": " + self.handle.before)
487 main.cleanup()
488 main.exit()
adminbae64d82013-08-01 10:50:15 -0700489
Jon Hallf2942ce2014-04-10 16:00:16 -0700490 #if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
491 if re.search(',\s0\%\spacket\sloss',response):
492 main.log.info(self.name+": Ping between two hosts SUCCESSFUL")
adminbae64d82013-08-01 10:50:15 -0700493 main.last_result = main.TRUE
494 return main.TRUE
495 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700496 main.log.error(self.name+": PACKET LOST, HOSTS NOT REACHABLE")
adminbae64d82013-08-01 10:50:15 -0700497 main.last_result = main.FALSE
498 return main.FALSE
499
500 def link(self,**linkargs):
501 '''
502 Bring link(s) between two nodes up or down
503 '''
adminbae64d82013-08-01 10:50:15 -0700504 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
505 end1 = args["END1"] if args["END1"] != None else ""
506 end2 = args["END2"] if args["END2"] != None else ""
507 option = args["OPTION"] if args["OPTION"] != None else ""
Jon Hall38481722014-11-04 16:50:05 -0500508 main.log.info("Bring link between '"+ end1 +"' and '" + end2 + "' '" + option + "'")
adminbae64d82013-08-01 10:50:15 -0700509 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
Jon Hall6094a362014-04-11 14:46:56 -0700510 try:
Jon Halle80ef8c2014-04-29 15:29:13 -0700511 #response = self.execute(cmd=command,prompt="mininet>",timeout=10)
512 self.handle.sendline(command)
513 self.handle.expect("mininet>")
Jon Hall6094a362014-04-11 14:46:56 -0700514 except pexpect.EOF:
515 main.log.error(self.name + ": EOF exception found")
516 main.log.error(self.name + ": " + self.handle.before)
517 main.cleanup()
518 main.exit()
adminbae64d82013-08-01 10:50:15 -0700519 return main.TRUE
520
521
admin530b4c92013-08-14 16:54:35 -0700522 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700523 '''
admin530b4c92013-08-14 16:54:35 -0700524 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700525 '''
admin530b4c92013-08-14 16:54:35 -0700526 main.log.info('Yank the switch interface attached to a host')
527 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700528 sw = args["SW"] if args["SW"] !=None else ""
529 intf = args["INTF"] if args["INTF"] != None else ""
530 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700531 try:
532 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
533 except pexpect.EOF:
534 main.log.error(self.name + ": EOF exception found")
535 main.log.error(self.name + ": " + self.handle.before)
536 main.cleanup()
537 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700538 return main.TRUE
539
540 def plug(self, **plugargs):
541 '''
542 plug the yanked mininet switch interface to a switch
543 '''
544 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700545 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700546 sw = args["SW"] if args["SW"] !=None else ""
547 intf = args["INTF"] if args["INTF"] != None else ""
548 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700549 try:
550 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
551 except pexpect.EOF:
552 main.log.error(self.name + ": EOF exception found")
553 main.log.error(self.name + ": " + self.handle.before)
554 main.cleanup()
555 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700556 return main.TRUE
557
558
559
adminbae64d82013-08-01 10:50:15 -0700560 def dpctl(self,**dpctlargs):
561 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700562 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700563 '''
564 main.log.info('Run dpctl command on all switches')
565 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
566 cmd = args["CMD"] if args["CMD"] != None else ""
567 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
568 command = "dpctl "+cmd + " " + str(cmdargs)
Jon Hall6094a362014-04-11 14:46:56 -0700569 try:
570 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
571 except pexpect.EOF:
572 main.log.error(self.name + ": EOF exception found")
573 main.log.error(self.name + ": " + self.handle.before)
574 main.cleanup()
575 main.exit()
adminbae64d82013-08-01 10:50:15 -0700576 return main.TRUE
577
578
579 def get_version(self):
580 file_input = path+'/lib/Mininet/INSTALL'
581 version = super(Mininet, self).get_version()
582 pattern = 'Mininet\s\w\.\w\.\w\w*'
583 for line in open(file_input,'r').readlines():
584 result = re.match(pattern, line)
585 if result:
586 version = result.group(0)
Jon Hallec3c21e2014-11-10 22:22:37 -0500587 return version
adminbae64d82013-08-01 10:50:15 -0700588
Jon Hallec3c21e2014-11-10 22:22:37 -0500589 def get_sw_controller(self, sw):
590 '''
591 Parameters:
592 sw: The name of an OVS switch. Example "s1"
593 Return:
594 The output of the command from the mininet cli or main.FALSE on timeout
595 '''
admin2a9548d2014-06-17 14:08:07 -0700596 command = "sh ovs-vsctl get-controller "+str(sw)
597 try:
Jon Hallec3c21e2014-11-10 22:22:37 -0500598 response = self.execute(cmd=command, prompt="mininet>", timeout=10)
admin2a9548d2014-06-17 14:08:07 -0700599 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500600 return response
admin2a9548d2014-06-17 14:08:07 -0700601 else:
602 return main.FALSE
603 except pexpect.EOF:
604 main.log.error(self.name + ": EOF exception found")
605 main.log.error(self.name + ": " + self.handle.before)
606 main.cleanup()
607 main.exit()
adminbae64d82013-08-01 10:50:15 -0700608
609 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700610 '''
611 count is only needed if there is more than 1 controller
612 '''
613 args = utilities.parse_args(["COUNT"],**kwargs)
614 count = args["COUNT"] if args!={} else 1
615
616 argstring = "SW"
617 for j in range(count):
618 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
619 args = utilities.parse_args(argstring.split(","),**kwargs)
620
adminbae64d82013-08-01 10:50:15 -0700621 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700622 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700623 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
624
625 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
626 for j in range(count):
627 i=j+1
628 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
629 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
630 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
631 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
632 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700633 try:
634 self.execute(cmd=command,prompt="mininet>",timeout=5)
635 except pexpect.EOF:
636 main.log.error(self.name + ": EOF exception found")
637 main.log.error(self.name + ": " + self.handle.before)
638 main.cleanup()
639 main.exit()
640 except:
641 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
642 main.log.error( traceback.print_exc() )
643 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
644 main.cleanup()
645 main.exit()
adminbae64d82013-08-01 10:50:15 -0700646
Jon Hall0819fd92014-05-23 12:08:13 -0700647 def delete_sw_controller(self,sw):
648 '''
649 Removes the controller target from sw
650 '''
651
652 command = "sh ovs-vsctl del-controller "+str(sw)
653 try:
654 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
655 except pexpect.EOF:
656 main.log.error(self.name + ": EOF exception found")
657 main.log.error(self.name + ": " + self.handle.before)
658 main.cleanup()
659 main.exit()
660 else:
661 main.log.info(response)
662
Jon Hallb1290e82014-11-18 16:17:48 -0500663 def add_switch( self, sw, **kwargs ):
664 '''
665 adds a switch to the mininet topology
666 NOTE: this uses a custom mn function
667 NOTE: cannot currently specify what type of switch
668 required params:
669 switchname = name of the new switch as a string
670 optional keyvalues:
671 dpid = "dpid"
672 returns: main.FASLE on an error, else main.TRUE
673 '''
674 dpid = kwargs.get('dpid', '')
675 command = "addswitch " + sw + " " + str(dpid)
676 try:
677 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
678 if re.search("already exists!", response):
679 main.log.warn(response)
680 return main.FALSE
681 elif re.search("Error", response):
682 main.log.warn(response)
683 return main.FALSE
684 elif re.search("usage:", response):
685 main.log.warn(response)
686 return main.FALSE
687 else:
688 return main.TRUE
689 except pexpect.EOF:
690 main.log.error(self.name + ": EOF exception found")
691 main.log.error(self.name + ": " + self.handle.before)
692 main.cleanup()
693 main.exit()
694
695 def del_switch( self, sw ):
696 '''
697 delete a switch from the mininet topology
698 NOTE: this uses a custom mn function
699 required params:
700 switchname = name of the switch as a string
701 returns: main.FASLE on an error, else main.TRUE
702 '''
703 command = "delswitch " + sw
704 try:
705 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
706 if re.search("no switch named", response):
707 main.log.warn(response)
708 return main.FALSE
709 elif re.search("Error", response):
710 main.log.warn(response)
711 return main.FALSE
712 elif re.search("usage:", response):
713 main.log.warn(response)
714 return main.FALSE
715 else:
716 return main.TRUE
717 except pexpect.EOF:
718 main.log.error(self.name + ": EOF exception found")
719 main.log.error(self.name + ": " + self.handle.before)
720 main.cleanup()
721 main.exit()
722
723 def add_link( self, node1, node2 ):
724 '''
725 add a link to the mininet topology
726 NOTE: this uses a custom mn function
727 NOTE: cannot currently specify what type of link
728 required params:
729 node1 = the string node name of the first endpoint of the link
730 node2 = the string node name of the second endpoint of the link
731 returns: main.FASLE on an error, else main.TRUE
732 '''
733 command = "addlink " + node1 + " " + node2
734 try:
735 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
736 if re.search("doesnt exist!", response):
737 main.log.warn(response)
738 return main.FALSE
739 elif re.search("Error", response):
740 main.log.warn(response)
741 return main.FALSE
742 elif re.search("usage:", response):
743 main.log.warn(response)
744 return main.FALSE
745 else:
746 return main.TRUE
747 except pexpect.EOF:
748 main.log.error(self.name + ": EOF exception found")
749 main.log.error(self.name + ": " + self.handle.before)
750 main.cleanup()
751 main.exit()
752
753 def del_link( self, node1, node2 ):
754 '''
755 delete a link from the mininet topology
756 NOTE: this uses a custom mn function
757 required params:
758 node1 = the string node name of the first endpoint of the link
759 node2 = the string node name of the second endpoint of the link
760 returns: main.FASLE on an error, else main.TRUE
761 '''
762 command = "dellink " + node1 + " " + node2
763 try:
764 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
765 if re.search("no node named", response):
766 main.log.warn(response)
767 return main.FALSE
768 elif re.search("Error", response):
769 main.log.warn(response)
770 return main.FALSE
771 elif re.search("usage:", response):
772 main.log.warn(response)
773 return main.FALSE
774 else:
775 return main.TRUE
776 except pexpect.EOF:
777 main.log.error(self.name + ": EOF exception found")
778 main.log.error(self.name + ": " + self.handle.before)
779 main.cleanup()
780 main.exit()
781
782 def add_host( self, hostname, **kwargs ):
783 '''
784 Add a host to the mininet topology
785 NOTE: this uses a custom mn function
786 NOTE: cannot currently specify what type of host
787 required params:
788 hostname = the string hostname
789 optional key-value params
790 switch = "switch name"
791 returns: main.FASLE on an error, else main.TRUE
792 '''
793 switch = kwargs.get('switch', '')
794 command = "addhost " + hostname + " " + switch
795 try:
796 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
797 if re.search("already exists!", response):
798 main.log.warn(response)
799 return main.FALSE
800 elif re.search("doesnt exists!", response):
801 main.log.warn(response)
802 return main.FALSE
803 elif re.search("Error", response):
804 main.log.warn(response)
805 return main.FALSE
806 elif re.search("usage:", response):
807 main.log.warn(response)
808 return main.FALSE
809 else:
810 return main.TRUE
811 except pexpect.EOF:
812 main.log.error(self.name + ": EOF exception found")
813 main.log.error(self.name + ": " + self.handle.before)
814 main.cleanup()
815 main.exit()
816
817 def del_host( self, hostname ):
818 '''
819 delete a host from the mininet topology
820 NOTE: this uses a custom mn function
821 required params:
822 hostname = the string hostname
823 returns: main.FASLE on an error, else main.TRUE
824 '''
825 command = "delhost " + hostname
826 try:
827 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
828 if re.search("no host named", response):
829 main.log.warn(response)
830 return main.FALSE
831 elif re.search("Error", response):
832 main.log.warn(response)
833 return main.FALSE
834 elif re.search("usage:", response):
835 main.log.warn(response)
836 return main.FALSE
837 else:
838 return main.TRUE
839 except pexpect.EOF:
840 main.log.error(self.name + ": EOF exception found")
841 main.log.error(self.name + ": " + self.handle.before)
842 main.cleanup()
843 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -0700844
adminbae64d82013-08-01 10:50:15 -0700845 def disconnect(self):
Jon Hallf2942ce2014-04-10 16:00:16 -0700846 main.log.info(self.name+": Disconnecting mininet...")
adminbae64d82013-08-01 10:50:15 -0700847 response = ''
848 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700849 try:
850 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
851 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
Jon Halle80ef8c2014-04-29 15:29:13 -0700852 self.handle.sendline("sudo mn -c")
shahshreya328c2a72014-11-17 10:19:50 -0800853 response = main.TRUE
Jon Hall6094a362014-04-11 14:46:56 -0700854 except pexpect.EOF:
855 main.log.error(self.name + ": EOF exception found")
856 main.log.error(self.name + ": " + self.handle.before)
857 main.cleanup()
858 main.exit()
adminbae64d82013-08-01 10:50:15 -0700859 else :
Jon Hallf2942ce2014-04-10 16:00:16 -0700860 main.log.error(self.name+": Connection failed to the host")
adminbae64d82013-08-01 10:50:15 -0700861 response = main.FALSE
862 return response
admin07529932013-11-22 14:58:28 -0800863
864 def arping(self, src, dest, destmac):
865 self.handle.sendline('')
Jon Hall333fa8c2014-04-11 11:24:58 -0700866 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800867
868 self.handle.sendline(src + ' arping ' + dest)
869 try:
Jon Hall333fa8c2014-04-11 11:24:58 -0700870 self.handle.expect([destmac,pexpect.EOF,pexpect.TIMEOUT])
Jon Hallf2942ce2014-04-10 16:00:16 -0700871 main.log.info(self.name+": ARP successful")
Jon Hall333fa8c2014-04-11 11:24:58 -0700872 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800873 return main.TRUE
874 except:
Jon Hallf2942ce2014-04-10 16:00:16 -0700875 main.log.warn(self.name+": ARP FAILURE")
Jon Hall333fa8c2014-04-11 11:24:58 -0700876 self.handle.expect(["mininet",pexpect.EOF,pexpect.TIMEOUT])
admin07529932013-11-22 14:58:28 -0800877 return main.FALSE
878
879 def decToHex(num):
880 return hex(num).split('x')[1]
admin2a9548d2014-06-17 14:08:07 -0700881
882 def getSwitchFlowCount(self, switch):
883 '''
884 return the Flow Count of the switch
885 '''
886 if self.handle:
887 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
888 try:
889 response = self.execute(cmd=cmd, prompt="mininet>", timeout=10)
890 except pexpect.EOF:
891 main.log.error(self.name + ": EOF exception found")
892 main.log.error(self.name + " " + self.handle.before)
893 main.cleanup()
894 main.exit()
895 pattern = "flow_count=(\d+)"
896 result = re.search(pattern, response, re.MULTILINE)
897 if result is None:
admin2a9548d2014-06-17 14:08:07 -0700898 main.log.info("Couldn't find flows on switch '', found: %s" % (switch, response))
899 return main.FALSE
900 return result.group(1)
901 else:
902 main.log.error("Connection failed to the Mininet host")
903
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -0700904 def check_flows(self, sw, dump_format=None):
905 if dump_format:
906 command = "sh ovs-ofctl -F " + dump_format + " dump-flows " + str(sw)
907 else:
908 command = "sh ovs-ofctl dump-flows "+str(sw)
admin2a9548d2014-06-17 14:08:07 -0700909 try:
910 response=self.execute(cmd=command,prompt="mininet>",timeout=10)
911 return response
912 except pexpect.EOF:
913 main.log.error(self.name + ": EOF exception found")
914 main.log.error(self.name + ": " + self.handle.before)
915 main.cleanup()
916 main.exit()
917 else:
918 main.log.info(response)
919
920 def start_tcpdump(self, filename, intf = "eth0", port = "port 6633"):
921 '''
922 Runs tpdump on an intferface and saves the file
923 intf can be specified, or the default eth0 is used
924 '''
925 try:
926 self.handle.sendline("")
927 self.handle.expect("mininet>")
928 self.handle.sendline("sh sudo tcpdump -n -i "+ intf + " " + port + " -w " + filename.strip() + " &")
929 self.handle.sendline("")
admin2a9548d2014-06-17 14:08:07 -0700930 i=self.handle.expect(['No\ssuch\device','listening\son',pexpect.TIMEOUT,"mininet>"],timeout=10)
931 main.log.warn(self.handle.before + self.handle.after)
Jon Hallb1290e82014-11-18 16:17:48 -0500932 self.handle.sendline("")
933 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -0700934 if i == 0:
935 main.log.error(self.name + ": tcpdump - No such device exists. tcpdump attempted on: " + intf)
936 return main.FALSE
937 elif i == 1:
938 main.log.info(self.name + ": tcpdump started on " + intf)
939 return main.TRUE
940 elif i == 2:
941 main.log.error(self.name + ": tcpdump command timed out! Check interface name, given interface was: " + intf)
942 return main.FALSE
943 elif i ==3:
944 main.log.info(self.name +": " + self.handle.before)
945 return main.TRUE
946 else:
947 main.log.error(self.name + ": tcpdump - unexpected response")
948 return main.FALSE
949 except pexpect.EOF:
950 main.log.error(self.name + ": EOF exception found")
951 main.log.error(self.name + ": " + self.handle.before)
952 main.cleanup()
953 main.exit()
954 except:
955 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
956 main.log.error( traceback.print_exc() )
957 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
958 main.cleanup()
959 main.exit()
960
961 def stop_tcpdump(self):
962 "pkills tcpdump"
963 try:
964 self.handle.sendline("sh sudo pkill tcpdump")
Jon Hallb1290e82014-11-18 16:17:48 -0500965 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -0700966 self.handle.sendline("")
967 self.handle.expect("mininet>")
968 except pexpect.EOF:
969 main.log.error(self.name + ": EOF exception found")
970 main.log.error(self.name + ": " + self.handle.before)
971 main.cleanup()
972 main.exit()
973 except:
974 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
975 main.log.error( traceback.print_exc() )
976 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
977 main.cleanup()
978 main.exit()
979
Jon Hall3d87d502014-10-17 18:37:42 -0400980 def compare_switches(self, topo, switches_json):
981 '''
982 Compare mn and onos switches
983 topo: sts TestONTopology object
984 switches_json: parsed json object from the onos devices api
985
986 This uses the sts TestONTopology object
987
988 '''
989 import json
Jon Hall42db6dc2014-10-24 19:03:48 -0400990 #main.log.debug("Switches_json string: ", switches_json)
Jon Hall3d87d502014-10-17 18:37:42 -0400991 output = {"switches":[]}
992 for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
Jon Hall3d87d502014-10-17 18:37:42 -0400993 ports = []
994 for port in switch.ports.values():
Jon Hall3d87d502014-10-17 18:37:42 -0400995 ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
996 output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
Jon Hall3d87d502014-10-17 18:37:42 -0400997
Jon Hall42db6dc2014-10-24 19:03:48 -0400998 #print "mn"
Jon Hall3d87d502014-10-17 18:37:42 -0400999 #print json.dumps(output, sort_keys=True,indent=4,separators=(',', ': '))
Jon Hall42db6dc2014-10-24 19:03:48 -04001000 #print "onos"
1001 #print json.dumps(switches_json, sort_keys=True,indent=4,separators=(',', ': '))
Jon Hall3d87d502014-10-17 18:37:42 -04001002
1003
1004 # created sorted list of dpid's in MN and ONOS for comparison
1005 mnDPIDs=[]
1006 for switch in output['switches']:
1007 mnDPIDs.append(switch['dpid'])
1008 mnDPIDs.sort()
Jon Hall38481722014-11-04 16:50:05 -05001009 #print "List of Mininet switch DPID's"
Jon Hall3d87d502014-10-17 18:37:42 -04001010 #print mnDPIDs
1011 if switches_json == "":#if rest call fails
Jon Hall42db6dc2014-10-24 19:03:48 -04001012 main.log.error(self.name + ".compare_switches(): Empty JSON object given from ONOS")
Jon Hall3d87d502014-10-17 18:37:42 -04001013 return main.FALSE
1014 onos=switches_json
1015 onosDPIDs=[]
1016 for switch in onos:
Jon Hall38481722014-11-04 16:50:05 -05001017 if switch['available'] == True:
1018 onosDPIDs.append(switch['id'].replace(":",'').replace("of",''))
1019 #else:
1020 #print "Switch is unavailable:"
1021 #print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001022 onosDPIDs.sort()
Jon Hall38481722014-11-04 16:50:05 -05001023 #print "List of ONOS switch DPID's"
Jon Hall3d87d502014-10-17 18:37:42 -04001024 #print onosDPIDs
1025
1026 if mnDPIDs!=onosDPIDs:
1027 switch_results = main.FALSE
1028 main.log.report( "Switches in MN but not in ONOS:")
1029 main.log.report( str([switch for switch in mnDPIDs if switch not in onosDPIDs]))
1030 main.log.report( "Switches in ONOS but not in MN:")
1031 main.log.report( str([switch for switch in onosDPIDs if switch not in mnDPIDs]))
1032 else:#list of dpid's match in onos and mn
1033 #main.log.report("DEBUG: The dpid's of the switches in Mininet and ONOS match")
1034 switch_results = main.TRUE
1035 return switch_results
1036
1037
1038
Jon Hall72cf1dc2014-10-20 21:04:50 -04001039 def compare_ports(self, topo, ports_json):
1040 '''
1041 Compare mn and onos ports
1042 topo: sts TestONTopology object
1043 ports_json: parsed json object from the onos ports api
1044
1045 Dependencies:
1046 1. This uses the sts TestONTopology object
1047 2. numpy - "sudo pip install numpy"
1048
1049 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001050 #FIXME: this does not look for extra ports in ONOS, only checks that ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001051 import json
1052 from numpy import uint64
Jon Hallb1290e82014-11-18 16:17:48 -05001053 ports_results = main.TRUE
Jon Hall72cf1dc2014-10-20 21:04:50 -04001054 output = {"switches":[]}
1055 for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
Jon Hall72cf1dc2014-10-20 21:04:50 -04001056 ports = []
1057 for port in switch.ports.values():
1058 #print port.hw_addr.toStr(separator = '')
Jon Hall39f29df2014-11-04 19:30:21 -05001059 tmp_port = {}
1060 tmp_port['of_port'] = port.port_no
1061 tmp_port['mac'] = str(port.hw_addr).replace('\'','')
1062 tmp_port['name'] = port.name
1063 tmp_port['enabled'] = port.enabled
1064
1065 ports.append(tmp_port)
1066 tmp_switch = {}
1067 tmp_switch['name'] = switch.name
1068 tmp_switch['dpid'] = str(switch.dpid).zfill(16)
1069 tmp_switch['ports'] = ports
1070
1071 output['switches'].append(tmp_switch)
Jon Hall72cf1dc2014-10-20 21:04:50 -04001072
1073
1074 ################ports#############
Jon Hall39f29df2014-11-04 19:30:21 -05001075 for mn_switch in output['switches']:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001076 mn_ports = []
1077 onos_ports = []
Jon Hallb1290e82014-11-18 16:17:48 -05001078 switch_result = main.TRUE
Jon Hall39f29df2014-11-04 19:30:21 -05001079 for port in mn_switch['ports']:
Jon Hall38481722014-11-04 16:50:05 -05001080 if port['enabled'] == True:
1081 mn_ports.append(port['of_port'])
Jon Hallb1290e82014-11-18 16:17:48 -05001082 #else: #DEBUG only
1083 # main.log.warn("Port %s on switch %s is down" % ( str(port['of_port']) , str(mn_switch['name'])) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001084 for onos_switch in ports_json:
Jon Hall38481722014-11-04 16:50:05 -05001085 #print "Iterating through a new switch as seen by ONOS"
1086 #print onos_switch
1087 if onos_switch['device']['available'] == True:
Jon Hall39f29df2014-11-04 19:30:21 -05001088 if onos_switch['device']['id'].replace(':','').replace("of", '') == mn_switch['dpid']:
Jon Hall38481722014-11-04 16:50:05 -05001089 for port in onos_switch['ports']:
1090 if port['isEnabled']:
1091 #print "Iterating through available ports on the switch"
1092 #print port
Jon Hallb1290e82014-11-18 16:17:48 -05001093 if port['port'] == 'local':
1094 #onos_ports.append('local')
1095 onos_ports.append(long(uint64(-2)))
1096 else:
1097 onos_ports.append(int(port['port']))
1098 '''
1099 else: #This is likely a new reserved port implemented
1100 main.log.error("unkown port '" + str(port['port']) )
1101 '''
Jon Hall1645caa2014-11-18 16:27:14 -05001102 #else: #DEBUG
1103 # main.log.warn("Port %s on switch %s is down" % ( str(port['port']) , str(onos_switch['device']['id'])) )
Jon Hallb1290e82014-11-18 16:17:48 -05001104 break
Jon Hall72cf1dc2014-10-20 21:04:50 -04001105 mn_ports.sort(key=float)
1106 onos_ports.sort(key=float)
1107 #print "\nPorts for Switch %s:" % (switch['name'])
1108 #print "\tmn_ports[] = ", mn_ports
1109 #print "\tonos_ports[] = ", onos_ports
Jon Hallb1290e82014-11-18 16:17:48 -05001110 mn_ports_log = mn_ports
1111 onos_ports_log = onos_ports
1112 mn_ports = [x for x in mn_ports]
1113 onos_ports = [x for x in onos_ports]
Jon Hall38481722014-11-04 16:50:05 -05001114
Jon Hall72cf1dc2014-10-20 21:04:50 -04001115 #TODO: handle other reserved port numbers besides LOCAL
Jon Hallb1290e82014-11-18 16:17:48 -05001116 #NOTE: Reserved ports
1117 # Local port: -2 in Openflow, ONOS shows 'local', we store as long(uint64(-2))
1118 for mn_port in mn_ports_log:
1119 if mn_port in onos_ports:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001120 #don't set results to true here as this is just one of many checks and it might override a failure
Jon Hallb1290e82014-11-18 16:17:48 -05001121 mn_ports.remove(mn_port)
1122 onos_ports.remove(mn_port)
1123 #NOTE: OVS reports this as down since there is no link
1124 # So ignoring these for now
1125 #TODO: Come up with a better way of handling these
1126 if 65534 in mn_ports:
1127 mn_ports.remove(65534)
1128 if long(uint64(-2)) in onos_ports:
1129 onos_ports.remove( long(uint64(-2)) )
1130 if len(mn_ports): #the ports of this switch don't match
1131 switch_result = main.FALSE
1132 main.log.warn("Ports in MN but not ONOS: " + str(mn_ports) )
1133 if len(onos_ports): #the ports of this switch don't match
1134 switch_result = main.FALSE
1135 main.log.warn("Ports in ONOS but not MN: " + str(onos_ports) )
1136 if switch_result == main.FALSE:
Jon Hall39f29df2014-11-04 19:30:21 -05001137 main.log.report("The list of ports for switch %s(%s) does not match:" % (mn_switch['name'], mn_switch['dpid']) )
Jon Hallb1290e82014-11-18 16:17:48 -05001138 main.log.warn("mn_ports[] = " + str(mn_ports_log))
1139 main.log.warn("onos_ports[] = " + str(onos_ports_log))
1140 ports_results = ports_results and switch_result
1141 return ports_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001142
1143
1144
1145
1146 def compare_links(self, topo, links_json):
1147 '''
1148 Compare mn and onos links
1149 topo: sts TestONTopology object
1150 links_json: parsed json object from the onos links api
1151
1152 This uses the sts TestONTopology object
1153
1154 '''
Jon Hall1c9e8732014-10-27 19:29:27 -04001155 #FIXME: this does not look for extra links in ONOS, only checks that ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001156 import json
1157 link_results = main.TRUE
1158 output = {"switches":[]}
1159 onos = links_json
1160 for switch in topo.graph.switches: #iterate through the MN topology and pull out switches and and port info
Jon Hall38481722014-11-04 16:50:05 -05001161 # print "Iterating though switches as seen by Mininet"
1162 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001163 ports = []
1164 for port in switch.ports.values():
1165 #print port.hw_addr.toStr(separator = '')
1166 ports.append({'of_port': port.port_no, 'mac': str(port.hw_addr).replace('\'',''), 'name': port.name})
1167 output['switches'].append({"name": switch.name, "dpid": str(switch.dpid).zfill(16), "ports": ports })
1168 #######Links########
1169
Jon Hall38481722014-11-04 16:50:05 -05001170 mn_links = [link for link in topo.patch_panel.network_links if (link.port1.enabled and link.port2.enabled)]
1171 #print "mn_links:"
1172 #print mn_links
1173 if 2*len(mn_links) == len(onos):
Jon Hall72cf1dc2014-10-20 21:04:50 -04001174 link_results = main.TRUE
1175 else:
1176 link_results = main.FALSE
Jon Hall38481722014-11-04 16:50:05 -05001177 main.log.report("Mininet has %i bidirectional links and ONOS has %i unidirectional links" % (len(mn_links), len(onos) ))
Jon Hall72cf1dc2014-10-20 21:04:50 -04001178
1179
1180 # iterate through MN links and check if an ONOS link exists in both directions
1181 # NOTE: Will currently only show mn links as down if they are cut through STS.
1182 # We can either do everything through STS or wait for up_network_links
1183 # and down_network_links to be fully implemented.
Jon Hall38481722014-11-04 16:50:05 -05001184 for link in mn_links:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001185 #print "Link: %s" % link
1186 #TODO: Find a more efficient search method
1187 node1 = None
1188 port1 = None
1189 node2 = None
1190 port2 = None
1191 first_dir = main.FALSE
1192 second_dir = main.FALSE
1193 for switch in output['switches']:
1194 #print "Switch: %s" % switch['name']
1195 if switch['name'] == link.node1.name:
1196 node1 = switch['dpid']
1197 for port in switch['ports']:
1198 if str(port['name']) == str(link.port1):
1199 port1 = port['of_port']
1200 if node1 is not None and node2 is not None:
1201 break
1202 if switch['name'] == link.node2.name:
1203 node2 = switch['dpid']
1204 for port in switch['ports']:
1205 if str(port['name']) == str(link.port2):
1206 port2 = port['of_port']
1207 if node1 is not None and node2 is not None:
1208 break
1209
1210
1211 for onos_link in onos:
1212 onos_node1 = onos_link['src']['device'].replace(":",'').replace("of", '')
1213 onos_node2 = onos_link['dst']['device'].replace(":",'').replace("of", '')
1214 onos_port1 = onos_link['src']['port']
1215 onos_port2 = onos_link['dst']['port']
1216
1217 #print "Checking ONOS for link %s/%s -> %s/%s and" % (node1, port1, node2, port2)
1218 #print "Checking ONOS for link %s/%s -> %s/%s" % (node2, port2, node1, port1)
1219 # check onos link from node1 to node2
1220 if str(onos_node1) == str(node1) and str(onos_node2) == str(node2):
1221 if int(onos_port1) == int(port1) and int(onos_port2) == int(port2):
1222 first_dir = main.TRUE
1223 else:
Jon Hallb1290e82014-11-18 16:17:48 -05001224 main.log.warn('The port numbers do not match for ' +str(link) +\
Jon Hall72cf1dc2014-10-20 21:04:50 -04001225 ' between ONOS and MN. When cheking ONOS for link '+\
1226 '%s/%s -> %s/%s' % (node1, port1, node2, port2)+\
1227 ' ONOS has the values %s/%s -> %s/%s' %\
1228 (onos_node1, onos_port1, onos_node2, onos_port2))
1229
1230 # check onos link from node2 to node1
1231 elif ( str(onos_node1) == str(node2) and str(onos_node2) == str(node1) ):
1232 if ( int(onos_port1) == int(port2) and int(onos_port2) == int(port1) ):
1233 second_dir = main.TRUE
1234 else:
Jon Hallb1290e82014-11-18 16:17:48 -05001235 main.log.warn('The port numbers do not match for ' +str(link) +\
Jon Hall72cf1dc2014-10-20 21:04:50 -04001236 ' between ONOS and MN. When cheking ONOS for link '+\
1237 '%s/%s -> %s/%s' % (node2, port2, node1, port1)+\
1238 ' ONOS has the values %s/%s -> %s/%s' %\
1239 (onos_node2, onos_port2, onos_node1, onos_port1))
1240 else:#this is not the link you're looking for
1241 pass
1242 if not first_dir:
1243 main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node1, port1, node2, port2))
1244 if not second_dir:
1245 main.log.report('ONOS does not have the link %s/%s -> %s/%s' % (node2, port2, node1, port1))
1246 link_results = link_results and first_dir and second_dir
Jon Hall62df9242014-10-22 12:20:17 -04001247 return link_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001248
1249
andrewonlab3f0a4af2014-10-17 12:25:14 -04001250 def get_hosts(self):
1251 '''
1252 Returns a list of all hosts
1253 Don't ask questions just use it
1254 '''
1255 self.handle.sendline("")
1256 self.handle.expect("mininet>")
1257
1258 self.handle.sendline("py [ host.name for host in net.hosts ]")
1259 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001260
andrewonlab3f0a4af2014-10-17 12:25:14 -04001261 handle_py = self.handle.before
1262 handle_py = handle_py.split("]\r\n",1)[1]
1263 handle_py = handle_py.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001264
andrewonlab3f0a4af2014-10-17 12:25:14 -04001265 self.handle.sendline("")
1266 self.handle.expect("mininet>")
admin2a9548d2014-06-17 14:08:07 -07001267
andrewonlab3f0a4af2014-10-17 12:25:14 -04001268 host_str = handle_py.replace("]", "")
1269 host_str = host_str.replace("'", "")
1270 host_str = host_str.replace("[", "")
1271 host_list = host_str.split(",")
1272
1273 return host_list
adminbae64d82013-08-01 10:50:15 -07001274
Jon Hall38481722014-11-04 16:50:05 -05001275
1276 def update(self):
1277 '''
1278 updates the port address and status information for each port in mn
1279 '''
1280 #TODO: Add error checking. currently the mininet command has no output
1281 main.log.info("Updateing MN port information")
Jon Hallb1290e82014-11-18 16:17:48 -05001282 try:
1283 self.handle.sendline("")
1284 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001285
Jon Hallb1290e82014-11-18 16:17:48 -05001286 self.handle.sendline("update")
1287 self.handle.expect("update")
1288 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001289
Jon Hallb1290e82014-11-18 16:17:48 -05001290 self.handle.sendline("")
1291 self.handle.expect("mininet>")
Jon Hall38481722014-11-04 16:50:05 -05001292
Jon Hallb1290e82014-11-18 16:17:48 -05001293 return main.TRUE
1294 except pexpect.EOF:
1295 main.log.error(self.name + ": EOF exception found")
1296 main.log.error(self.name + ": " + self.handle.before)
1297 main.cleanup()
1298 main.exit()
1299
adminbae64d82013-08-01 10:50:15 -07001300if __name__ != "__main__":
1301 import sys
1302 sys.modules[__name__] = MininetCliDriver()
admin2a9548d2014-06-17 14:08:07 -07001303