blob: 6193754b8083c0e7b0beed00a0a621c57c265d78 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
Jon Hall7eb38402015-01-08 17:19:54 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Oct-2012
4
Jon Hallbe6dfc42015-01-12 17:37:25 -08005author: Anil Kumar ( anilkumar.s@paxterrasolutions.com )
adminbae64d82013-08-01 10:50:15 -07006
7
Jon Hall7eb38402015-01-08 17:19:54 -08008TestON is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 2 of the License, or
11( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070012
Jon Hall7eb38402015-01-08 17:19:54 -080013TestON is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
adminbae64d82013-08-01 10:50:15 -070017
Jon Hall7eb38402015-01-08 17:19:54 -080018You should have received a copy of the GNU General Public License
19along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070020
21
Jon Hallbe6dfc42015-01-12 17:37:25 -080022MininetCliDriver is the basic driver which will handle the Mininet functions
23
24Some functions rely on a modified version of Mininet. These functions
25should all be noted in the comments. To get this MN version run these commands
26from within your Mininet folder:
27
28 git remote add jhall11 https://github.com/jhall11/sts.git
29 git fetch jhall11
30 git checkout -b jhall11/topology_refactoring2 remotes/jhall11/topology_refactoring2
31 git pull
32
33Note that you may need to run 'sudo make develop' if your mnexec.c file
34changed when switching branches."""
admin2a9548d2014-06-17 14:08:07 -070035import traceback
adminbae64d82013-08-01 10:50:15 -070036import pexpect
adminbae64d82013-08-01 10:50:15 -070037import re
38import sys
Jon Hall7eb38402015-01-08 17:19:54 -080039sys.path.append( "../" )
Jon Hall1ccf82c2014-10-15 14:55:16 -040040from math import pow
adminbae64d82013-08-01 10:50:15 -070041from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070042
Jon Hall7eb38402015-01-08 17:19:54 -080043
44class MininetCliDriver( Emulator ):
45
46 """
47 MininetCliDriver is the basic driver which will handle
48 the Mininet functions"""
49 def __init__( self ):
50 super( Emulator, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070051 self.handle = self
Jon Hall7eb38402015-01-08 17:19:54 -080052 self.wrapped = sys.modules[ __name__ ]
adminbae64d82013-08-01 10:50:15 -070053 self.flag = 0
54
Jon Hall7eb38402015-01-08 17:19:54 -080055 def connect( self, **connectargs ):
56 """
57 Here the main is the TestON instance after creating
58 all the log handles."""
adminbae64d82013-08-01 10:50:15 -070059 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -080060 vars( self )[ key ] = connectargs[ key ]
Jon Hallfbc828e2015-01-06 17:30:19 -080061
Jon Hall7eb38402015-01-08 17:19:54 -080062 self.name = self.options[ 'name' ]
63 self.handle = super(
64 MininetCliDriver,
65 self ).connect(
66 user_name=self.user_name,
67 ip_address=self.ip_address,
68 port=None,
69 pwd=self.pwd )
Jon Hallfbc828e2015-01-06 17:30:19 -080070
adminbae64d82013-08-01 10:50:15 -070071 self.ssh_handle = self.handle
Jon Hallfbc828e2015-01-06 17:30:19 -080072
Jon Hall7eb38402015-01-08 17:19:54 -080073 if self.handle:
74 main.log.info(
75 self.name +
76 ": Clearing any residual state or processes" )
77 self.handle.sendline( "sudo mn -c" )
78 i = self.handle.expect( [ 'password\sfor\s',
79 'Cleanup\scomplete',
80 pexpect.EOF,
81 pexpect.TIMEOUT ],
82 120 )
83 if i == 0:
84 main.log.info( self.name + ": Sending sudo password" )
85 self.handle.sendline( self.pwd )
86 i = self.handle.expect( [ '%s:' % ( self.user ),
87 '\$',
88 pexpect.EOF,
89 pexpect.TIMEOUT ],
90 120 )
91 if i == 1:
92 main.log.info( self.name + ": Clean" )
93 elif i == 2:
94 main.log.error( self.name + ": Connection terminated" )
95 elif i == 3: # timeout
96 main.log.error(
97 self.name +
98 ": Something while cleaning MN took too long... " )
Jon Hallfbc828e2015-01-06 17:30:19 -080099
Jon Hall7eb38402015-01-08 17:19:54 -0800100 main.log.info( self.name + ": building fresh mininet" )
101 # for reactive/PARP enabled tests
102 cmdString = "sudo mn " + self.options[ 'arg1' ] +\
103 " " + self.options[ 'arg2' ] +\
104 " --mac --controller " +\
105 self.options[ 'controller' ] + " " +\
106 self.options[ 'arg3' ]
Jon Hallfbc828e2015-01-06 17:30:19 -0800107
Jon Hall7eb38402015-01-08 17:19:54 -0800108 argList = self.options[ 'arg1' ].split( "," )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400109 global topoArgList
Jon Hall7eb38402015-01-08 17:19:54 -0800110 topoArgList = argList[ 0 ].split( " " )
111 argList = map( int, argList[ 1: ] )
112 topoArgList = topoArgList[ 1: ] + argList
Jon Hallfbc828e2015-01-06 17:30:19 -0800113
Jon Hall7eb38402015-01-08 17:19:54 -0800114 self.handle.sendline( cmdString )
115 self.handle.expect( [ "sudo mn", pexpect.EOF, pexpect.TIMEOUT ] )
116 while True:
117 i = self.handle.expect( [ 'mininet>',
118 '\*\*\*',
119 'Exception',
120 pexpect.EOF,
121 pexpect.TIMEOUT ],
122 300 )
123 if i == 0:
124 main.log.info( self.name + ": mininet built" )
adminbae64d82013-08-01 10:50:15 -0700125 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800126 if i == 1:
127 self.handle.expect(
128 [ "\n", pexpect.EOF, pexpect.TIMEOUT ] )
129 main.log.info( self.handle.before )
130 elif i == 2:
131 main.log.error(
132 self.name +
133 ": Launching mininet failed..." )
adminbae64d82013-08-01 10:50:15 -0700134 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800135 elif i == 3:
136 main.log.error( self.name + ": Connection timeout" )
adminbae64d82013-08-01 10:50:15 -0700137 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800138 elif i == 4: # timeout
139 main.log.error(
140 self.name +
141 ": Something took too long... " )
adminbae64d82013-08-01 10:50:15 -0700142 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700143 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800144 else: # if no handle
145 main.log.error(
146 self.name +
147 ": Connection failed to the host " +
148 self.user_name +
149 "@" +
150 self.ip_address )
151 main.log.error( self.name + ": Failed to connect to the Mininet" )
adminbae64d82013-08-01 10:50:15 -0700152 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800153
Jon Hall7eb38402015-01-08 17:19:54 -0800154 def num_switches_n_links( self, topoType, depth, fanout ):
Jon Hall1ccf82c2014-10-15 14:55:16 -0400155 if topoType == 'tree':
Jon Hall7eb38402015-01-08 17:19:54 -0800156 # In tree topology, if fanout arg is not given, by default it is 2
157 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400158 fanout = 2
159 k = 0
Jon Hall38481722014-11-04 16:50:05 -0500160 count = 0
Jon Hall7eb38402015-01-08 17:19:54 -0800161 while( k <= depth - 1 ):
162 count = count + pow( fanout, k )
163 k = k + 1
Jon Hall38481722014-11-04 16:50:05 -0500164 num_switches = count
Jon Hall7eb38402015-01-08 17:19:54 -0800165 while( k <= depth - 2 ):
166 # depth-2 gives you only core links and not considering
167 # edge links as seen by ONOS. If all the links including
168 # edge links are required, do depth-1
169 count = count + pow( fanout, k )
170 k = k + 1
Jon Hall38481722014-11-04 16:50:05 -0500171 num_links = count * fanout
Jon Hall7eb38402015-01-08 17:19:54 -0800172 # print "num_switches for %s(%d,%d) = %d and links=%d" %(
173 # topoType,depth,fanout,num_switches,num_links )
Jon Hallfbc828e2015-01-06 17:30:19 -0800174
Jon Hall7eb38402015-01-08 17:19:54 -0800175 elif topoType == 'linear':
176 # In linear topology, if fanout or num_hosts_per_sw is not given,
177 # by default it is 1
178 if fanout is None:
Jon Hall1ccf82c2014-10-15 14:55:16 -0400179 fanout = 1
180 num_switches = depth
181 num_hosts_per_sw = fanout
182 total_num_hosts = num_switches * num_hosts_per_sw
Jon Hall7eb38402015-01-08 17:19:54 -0800183 num_links = total_num_hosts + ( num_switches - 1 )
184 print "num_switches for %s(%d,%d) = %d and links=%d" %\
185 ( topoType, depth, fanout, num_switches, num_links )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400186 topoDict = {}
Jon Hall7eb38402015-01-08 17:19:54 -0800187 topoDict = {
188 "num_switches": int( num_switches ),
189 "num_corelinks": int( num_links ) }
Jon Hall1ccf82c2014-10-15 14:55:16 -0400190 return topoDict
191
Jon Hall7eb38402015-01-08 17:19:54 -0800192 def calculate_sw_and_links( self ):
193 topoDict = self.num_switches_n_links( *topoArgList )
Jon Hall1ccf82c2014-10-15 14:55:16 -0400194 return topoDict
195
Jon Hall7eb38402015-01-08 17:19:54 -0800196 def pingall( self, timeout=300 ):
197 """
198 Verifies the reachability of the hosts using pingall command.
199 Optional parameter timeout allows you to specify how long to
200 wait for pingall to complete
201 Returns:
202 main.TRUE if pingall completes with no pings dropped
203 otherwise main.FALSE"""
204 if self.handle:
205 main.log.info(
206 self.name +
207 ": Checking reachabilty to the hosts using pingall" )
Jon Hall6094a362014-04-11 14:46:56 -0700208 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800209 response = self.execute(
210 cmd="pingall",
211 prompt="mininet>",
212 timeout=int( timeout ) )
Jon Hallb1290e82014-11-18 16:17:48 -0500213 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800214 main.log.error( self.name + ": EOF exception found" )
215 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500216 main.cleanup()
217 main.exit()
218 except pexpect.TIMEOUT:
Jon Hall7eb38402015-01-08 17:19:54 -0800219 # We may not want to kill the test if pexpect times out
220 main.log.error( self.name + ": TIMEOUT exception found" )
221 main.log.error( self.name +
222 ": " +
223 str( self.handle.before ) )
224 # NOTE: mininet's pingall rounds, so we will check the number of
225 # passed and number of failed
226 pattern = "Results\:\s0\%\sdropped\s\(" +\
227 "(?P<passed>[\d]+)/(?P=passed)"
228 if re.search( pattern, response ):
229 main.log.info( self.name + ": All hosts are reachable" )
adminbae64d82013-08-01 10:50:15 -0700230 return main.TRUE
231 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800232 main.log.error( self.name + ": Unable to reach all the hosts" )
233 main.log.info( "Pingall ouput: " + str( response ) )
234 # NOTE: Send ctrl-c to make sure pingall is done
235 self.handle.send( "\x03" )
236 self.handle.expect( "Interrupt" )
237 self.handle.expect( "mininet>" )
adminbae64d82013-08-01 10:50:15 -0700238 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800239 else:
240 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallb1290e82014-11-18 16:17:48 -0500241 main.cleanup()
242 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700243
Jon Hall7eb38402015-01-08 17:19:54 -0800244 def fpingHost( self, **pingParams ):
245 """
246 Uses the fping package for faster pinging...
247 *requires fping to be installed on machine running mininet"""
248 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
249 command = args[ "SRC" ] + \
250 " fping -i 100 -t 20 -C 1 -q " + args[ "TARGET" ]
251 self.handle.sendline( command )
252 self.handle.expect(
253 [ args[ "TARGET" ], pexpect.EOF, pexpect.TIMEOUT ] )
254 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
255 response = self.handle.before
256 if re.search( ":\s-", response ):
257 main.log.info( self.name + ": Ping fail" )
adminaeedddd2013-08-02 15:14:15 -0700258 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800259 elif re.search( ":\s\d{1,2}\.\d\d", response ):
260 main.log.info( self.name + ": Ping good!" )
adminaeedddd2013-08-02 15:14:15 -0700261 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800262 main.log.info( self.name + ": Install fping on mininet machine... " )
263 main.log.info( self.name + ": \n---\n" + response )
adminaeedddd2013-08-02 15:14:15 -0700264 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800265
Jon Hall7eb38402015-01-08 17:19:54 -0800266 def pingHost( self, **pingParams ):
267 """
268 Ping from one mininet host to another
269 Currently the only supported Params: SRC and TARGET"""
270 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
271 command = args[ "SRC" ] + " ping " + \
272 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
Jon Hall6094a362014-04-11 14:46:56 -0700273 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800274 main.log.warn( "Sending: " + command )
275 self.handle.sendline( command )
276 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700277 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800278 main.log.error(
279 self.name +
280 ": timeout when waiting for response from mininet" )
281 main.log.error( "response: " + str( self.handle.before ) )
282 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700283 if i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -0800284 main.log.error(
285 self.name +
286 ": timeout when waiting for response from mininet" )
287 main.log.error( "response: " + str( self.handle.before ) )
Jon Hall6e18c7b2014-04-23 16:26:33 -0700288 response = self.handle.before
Jon Hallfbc828e2015-01-06 17:30:19 -0800289 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800290 main.log.error( self.name + ": EOF exception found" )
291 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700292 main.cleanup()
293 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -0800294 main.log.info( self.name + ": Ping Response: " + response )
295 if re.search( ',\s0\%\spacket\sloss', response ):
296 main.log.info( self.name + ": no packets lost, host is reachable" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800297 main.last_result = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700298 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800299 else:
300 main.log.error(
301 self.name +
302 ": PACKET LOST, HOST IS NOT REACHABLE" )
adminbae64d82013-08-01 10:50:15 -0700303 main.last_result = main.FALSE
304 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800305
Jon Hall7eb38402015-01-08 17:19:54 -0800306 def checkIP( self, host ):
307 """
308 Verifies the host's ip configured or not."""
309 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700310 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800311 response = self.execute(
312 cmd=host +
313 " ifconfig",
314 prompt="mininet>",
315 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800316 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800317 main.log.error( self.name + ": EOF exception found" )
318 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700319 main.cleanup()
320 main.exit()
adminbae64d82013-08-01 10:50:15 -0700321
Jon Hall7eb38402015-01-08 17:19:54 -0800322 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|" +\
323 "2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}" +\
324 "[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})." +\
325 "([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|" +\
326 "[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4]" +\
327 "[0-9]|25[0-5]|[0-9]{1,2})"
328 # pattern = "inet addr:10.0.0.6"
329 if re.search( pattern, response ):
330 main.log.info( self.name + ": Host Ip configured properly" )
adminbae64d82013-08-01 10:50:15 -0700331 return main.TRUE
332 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800333 main.log.error( self.name + ": Host IP not found" )
adminbae64d82013-08-01 10:50:15 -0700334 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800335 else:
336 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800337
Jon Hall7eb38402015-01-08 17:19:54 -0800338 def verifySSH( self, **connectargs ):
Jon Hall6094a362014-04-11 14:46:56 -0700339 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800340 response = self.execute(
341 cmd="h1 /usr/sbin/sshd -D&",
342 prompt="mininet>",
343 timeout=10 )
344 response = self.execute(
345 cmd="h4 /usr/sbin/sshd -D&",
346 prompt="mininet>",
347 timeout=10 )
Jon Hall6094a362014-04-11 14:46:56 -0700348 for key in connectargs:
Jon Hall7eb38402015-01-08 17:19:54 -0800349 vars( self )[ key ] = connectargs[ key ]
350 response = self.execute(
351 cmd="xterm h1 h4 ",
352 prompt="mininet>",
353 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800354 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800355 main.log.error( self.name + ": EOF exception found" )
356 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700357 main.cleanup()
358 main.exit()
adminbae64d82013-08-01 10:50:15 -0700359 import time
Jon Hall7eb38402015-01-08 17:19:54 -0800360 time.sleep( 20 )
adminbae64d82013-08-01 10:50:15 -0700361 if self.flag == 0:
362 self.flag = 1
363 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800364 else:
adminbae64d82013-08-01 10:50:15 -0700365 return main.TRUE
shahshreyae6c7cf42014-11-26 16:39:01 -0800366
Jon Hall7eb38402015-01-08 17:19:54 -0800367 def changeIP( self, host, intf, newIP, newNetmask ):
368 """
369 Changes the ip address of a host on the fly
370 Ex: h2 ifconfig h2-eth0 10.0.1.2 netmask 255.255.255.0"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800371 if self.handle:
372 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800373 cmd = host + " ifconfig " + intf + " " + \
374 newIP + " " + 'netmask' + " " + newNetmask
375 self.handle.sendline( cmd )
376 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800377 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800378 main.log.info( "response = " + response )
379 main.log.info(
380 "Ip of host " +
381 host +
382 " changed to new IP " +
383 newIP )
shahshreyae6c7cf42014-11-26 16:39:01 -0800384 return main.TRUE
385 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800386 main.log.error( self.name + ": EOF exception found" )
387 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800388 return main.FALSE
389
Jon Hall7eb38402015-01-08 17:19:54 -0800390 def changeDefaultGateway( self, host, newGW ):
391 """
392 Changes the default gateway of a host
393 Ex: h1 route add default gw 10.0.1.2"""
shahshreyae6c7cf42014-11-26 16:39:01 -0800394 if self.handle:
395 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800396 cmd = host + " route add default gw " + newGW
397 self.handle.sendline( cmd )
398 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800399 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800400 main.log.info( "response = " + response )
401 main.log.info(
402 "Default gateway of host " +
403 host +
404 " changed to " +
405 newGW )
shahshreyae6c7cf42014-11-26 16:39:01 -0800406 return main.TRUE
407 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800408 main.log.error( self.name + ": EOF exception found" )
409 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800410 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800411
Jon Hall7eb38402015-01-08 17:19:54 -0800412 def addStaticMACAddress( self, host, GW, macaddr ):
413 """
414 Changes the mac address of a geateway host"""
shahshreyad0c80432014-12-04 16:56:05 -0800415 if self.handle:
416 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800417 # h1 arp -s 10.0.1.254 00:00:00:00:11:11
418 cmd = host + " arp -s " + GW + " " + macaddr
419 self.handle.sendline( cmd )
420 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800421 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800422 main.log.info( "response = " + response )
423 main.log.info(
424 "Mac adrress of gateway " +
425 GW +
426 " changed to " +
427 macaddr )
shahshreyad0c80432014-12-04 16:56:05 -0800428 return main.TRUE
429 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800430 main.log.error( self.name + ": EOF exception found" )
431 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800432 return main.FALSE
433
Jon Hall7eb38402015-01-08 17:19:54 -0800434 def verifyStaticGWandMAC( self, host ):
435 """
436 Verify if the static gateway and mac address assignment"""
shahshreyad0c80432014-12-04 16:56:05 -0800437 if self.handle:
438 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800439 # h1 arp -an
440 cmd = host + " arp -an "
441 self.handle.sendline( cmd )
442 self.handle.expect( "mininet>" )
shahshreyad0c80432014-12-04 16:56:05 -0800443 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800444 main.log.info( host + " arp -an = " + response )
shahshreyad0c80432014-12-04 16:56:05 -0800445 return main.TRUE
446 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800447 main.log.error( self.name + ": EOF exception found" )
448 main.log.error( self.name + ": " + self.handle.before )
shahshreyad0c80432014-12-04 16:56:05 -0800449 return main.FALSE
450
Jon Hall7eb38402015-01-08 17:19:54 -0800451 def getMacAddress( self, host ):
452 """
453 Verifies the host's ip configured or not."""
454 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700455 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800456 response = self.execute(
457 cmd=host +
458 " ifconfig",
459 prompt="mininet>",
460 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800461 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800462 main.log.error( self.name + ": EOF exception found" )
463 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700464 main.cleanup()
465 main.exit()
adminbae64d82013-08-01 10:50:15 -0700466
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700467 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
Jon Hall7eb38402015-01-08 17:19:54 -0800468 mac_address_search = re.search( pattern, response, re.I )
469 mac_address = mac_address_search.group().split( " " )[ 1 ]
470 main.log.info(
471 self.name +
472 ": Mac-Address of Host " +
473 host +
474 " is " +
475 mac_address )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700476 return mac_address
477 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800478 main.log.error( self.name + ": Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700479
Jon Hall7eb38402015-01-08 17:19:54 -0800480 def getInterfaceMACAddress( self, host, interface ):
481 """
482 Return the IP address of the interface on the given host"""
483 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -0700484 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800485 response = self.execute( cmd=host + " ifconfig " + interface,
486 prompt="mininet>", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800487 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800488 main.log.error( self.name + ": EOF exception found" )
489 main.log.error( self.name + ": " + self.handle.before )
490 main.cleanup()
491 main.exit()
492
493 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
494 mac_address_search = re.search( pattern, response, re.I )
495 if mac_address_search is None:
496 main.log.info( "No mac address found in %s" % response )
497 return main.FALSE
498 mac_address = mac_address_search.group().split( " " )[ 1 ]
499 main.log.info(
500 "Mac-Address of " +
501 host +
502 ":" +
503 interface +
504 " is " +
505 mac_address )
506 return mac_address
507 else:
508 main.log.error( "Connection failed to the host" )
509
510 def getIPAddress( self, host ):
511 """
512 Verifies the host's ip configured or not."""
513 if self.handle:
514 try:
515 response = self.execute(
516 cmd=host +
517 " ifconfig",
518 prompt="mininet>",
519 timeout=10 )
520 except pexpect.EOF:
521 main.log.error( self.name + ": EOF exception found" )
522 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700523 main.cleanup()
524 main.exit()
adminbae64d82013-08-01 10:50:15 -0700525
526 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -0800527 ip_address_search = re.search( pattern, response )
528 main.log.info(
529 self.name +
530 ": IP-Address of Host " +
531 host +
532 " is " +
533 ip_address_search.group( 1 ) )
534 return ip_address_search.group( 1 )
535 else:
536 main.log.error( self.name + ": Connection failed to the host" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800537
Jon Hall7eb38402015-01-08 17:19:54 -0800538 def getSwitchDPID( self, switch ):
539 """
540 return the datapath ID of the switch"""
541 if self.handle:
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700542 cmd = "py %s.dpid" % switch
Jon Hall6094a362014-04-11 14:46:56 -0700543 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800544 response = self.execute(
545 cmd=cmd,
546 prompt="mininet>",
547 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800548 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800549 main.log.error( self.name + ": EOF exception found" )
550 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700551 main.cleanup()
552 main.exit()
Jon Hall28bf54b2014-12-17 16:25:44 -0800553 pattern = r'^(?P<dpid>\w)+'
Jon Hall7eb38402015-01-08 17:19:54 -0800554 result = re.search( pattern, response, re.MULTILINE )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700555 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -0800556 main.log.info(
557 "Couldn't find DPID for switch %s, found: %s" %
558 ( switch, response ) )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700559 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800560 return str( result.group( 0 ) ).lower()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700561 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800562 main.log.error( "Connection failed to the host" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700563
Jon Hall7eb38402015-01-08 17:19:54 -0800564 def getDPID( self, switch ):
admin2580a0e2014-07-29 11:24:34 -0700565 if self.handle:
Jon Hall7eb38402015-01-08 17:19:54 -0800566 self.handle.sendline( "" )
567 self.expect( "mininet>" )
568 cmd = "py %s.dpid" % switch
admin2580a0e2014-07-29 11:24:34 -0700569 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800570 response = self.execute(
571 cmd=cmd,
572 prompt="mininet>",
573 timeout=10 )
574 self.handle.expect( "mininet>" )
admin2580a0e2014-07-29 11:24:34 -0700575 response = self.handle.before
576 return response
577 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800578 main.log.error( self.name + ": EOF exception found" )
579 main.log.error( self.name + ": " + self.handle.before )
admin2580a0e2014-07-29 11:24:34 -0700580 main.cleanup()
581 main.exit()
582
Jon Hall7eb38402015-01-08 17:19:54 -0800583 def getInterfaces( self, node ):
584 """
585 return information dict about interfaces connected to the node"""
586 if self.handle:
587 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,enabled=%s"' +\
588 ' % (i.name, i.MAC(), i.IP(), i.isUp())'
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700589 cmd += ' for i in %s.intfs.values()])' % node
Jon Hall6094a362014-04-11 14:46:56 -0700590 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800591 response = self.execute(
592 cmd=cmd,
593 prompt="mininet>",
594 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800595 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800596 main.log.error( self.name + ": EOF exception found" )
597 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700598 main.cleanup()
599 main.exit()
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700600 return response
601 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800602 main.log.error( "Connection failed to the node" )
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700603
Jon Hall7eb38402015-01-08 17:19:54 -0800604 def dump( self ):
605 main.log.info( self.name + ": Dump node info" )
Jon Hall6094a362014-04-11 14:46:56 -0700606 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800607 response = self.execute(
608 cmd='dump',
609 prompt='mininet>',
610 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800611 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800612 main.log.error( self.name + ": EOF exception found" )
613 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700614 main.cleanup()
615 main.exit()
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700616 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800617
Jon Hall7eb38402015-01-08 17:19:54 -0800618 def intfs( self ):
619 main.log.info( self.name + ": List interfaces" )
Jon Hall6094a362014-04-11 14:46:56 -0700620 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800621 response = self.execute(
622 cmd='intfs',
623 prompt='mininet>',
624 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800625 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800626 main.log.error( self.name + ": EOF exception found" )
627 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700628 main.cleanup()
629 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700630 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800631
Jon Hall7eb38402015-01-08 17:19:54 -0800632 def net( self ):
633 main.log.info( self.name + ": List network connections" )
Jon Hall6094a362014-04-11 14:46:56 -0700634 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800635 response = self.execute( cmd='net', prompt='mininet>', timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800636 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800637 main.log.error( self.name + ": EOF exception found" )
638 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700639 main.cleanup()
640 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700641 return response
Jon Hall7eb38402015-01-08 17:19:54 -0800642
643 def iperf( self, host1, host2 ):
644 main.log.info(
645 self.name +
646 ": Simple iperf TCP test between two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700647 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800648 cmd1 = 'iperf ' + host1 + " " + host2
649 self.handle.sendline( cmd1 )
650 self.handle.expect( "mininet>" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800651 response = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -0800652 if re.search( 'Results:', response ):
653 main.log.info( self.name + ": iperf test succssful" )
shahshreyae6c7cf42014-11-26 16:39:01 -0800654 return main.TRUE
655 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800656 main.log.error( self.name + ": iperf test failed" )
657 return main.FALSE
shahshreyae6c7cf42014-11-26 16:39:01 -0800658 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800659 main.log.error( self.name + ": EOF exception found" )
660 main.log.error( self.name + ": " + self.handle.before )
shahshreyae6c7cf42014-11-26 16:39:01 -0800661 main.cleanup()
662 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800663
Jon Hall7eb38402015-01-08 17:19:54 -0800664 def iperfudp( self ):
665 main.log.info(
666 self.name +
667 ": Simple iperf TCP test between two " +
668 "(optionally specified) hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700669 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800670 response = self.execute(
671 cmd='iperfudp',
672 prompt='mininet>',
673 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800674 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800675 main.log.error( self.name + ": EOF exception found" )
676 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700677 main.cleanup()
678 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700679 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800680
Jon Hall7eb38402015-01-08 17:19:54 -0800681 def nodes( self ):
682 main.log.info( self.name + ": List all nodes." )
Jon Hall6094a362014-04-11 14:46:56 -0700683 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800684 response = self.execute(
685 cmd='nodes',
686 prompt='mininet>',
687 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800688 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800689 main.log.error( self.name + ": EOF exception found" )
690 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700691 main.cleanup()
692 main.exit()
Jon Hall668ed802014-04-08 17:17:59 -0700693 return response
Jon Hallfbc828e2015-01-06 17:30:19 -0800694
Jon Hall7eb38402015-01-08 17:19:54 -0800695 def pingpair( self ):
696 main.log.info( self.name + ": Ping between first two hosts" )
Jon Hall6094a362014-04-11 14:46:56 -0700697 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800698 response = self.execute(
699 cmd='pingpair',
700 prompt='mininet>',
701 timeout=20 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800702 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800703 main.log.error( self.name + ": EOF exception found" )
704 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700705 main.cleanup()
706 main.exit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800707
Jon Hall7eb38402015-01-08 17:19:54 -0800708 if re.search( ',\s0\%\spacket\sloss', response ):
709 main.log.info( self.name + ": Ping between two hosts SUCCESSFUL" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800710 main.last_result = main.TRUE
adminbae64d82013-08-01 10:50:15 -0700711 return main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800712 else:
713 main.log.error( self.name + ": PACKET LOST, HOSTS NOT REACHABLE" )
adminbae64d82013-08-01 10:50:15 -0700714 main.last_result = main.FALSE
715 return main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -0800716
Jon Hall7eb38402015-01-08 17:19:54 -0800717 def link( self, **linkargs ):
718 """
719 Bring link( s ) between two nodes up or down"""
720 args = utilities.parse_args( [ "END1", "END2", "OPTION" ], **linkargs )
721 end1 = args[ "END1" ] if args[ "END1" ] is not None else ""
722 end2 = args[ "END2" ] if args[ "END2" ] is not None else ""
723 option = args[ "OPTION" ] if args[ "OPTION" ] is not None else ""
724 main.log.info(
725 "Bring link between '" +
726 end1 +
727 "' and '" +
728 end2 +
729 "' '" +
730 option +
731 "'" )
732 command = "link " + \
733 str( end1 ) + " " + str( end2 ) + " " + str( option )
Jon Hall6094a362014-04-11 14:46:56 -0700734 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800735 self.handle.sendline( command )
736 self.handle.expect( "mininet>" )
Jon Hallfbc828e2015-01-06 17:30:19 -0800737 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800738 main.log.error( self.name + ": EOF exception found" )
739 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700740 main.cleanup()
741 main.exit()
adminbae64d82013-08-01 10:50:15 -0700742 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800743
Jon Hall7eb38402015-01-08 17:19:54 -0800744 def yank( self, **yankargs ):
745 """
746 yank a mininet switch interface to a host"""
747 main.log.info( 'Yank the switch interface attached to a host' )
748 args = utilities.parse_args( [ "SW", "INTF" ], **yankargs )
749 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
750 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
751 command = "py " + str( sw ) + '.detach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700752 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800753 response = self.execute(
754 cmd=command,
755 prompt="mininet>",
756 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800757 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800758 main.log.error( self.name + ": EOF exception found" )
759 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700760 main.cleanup()
761 main.exit()
adminaeedddd2013-08-02 15:14:15 -0700762 return main.TRUE
763
Jon Hall7eb38402015-01-08 17:19:54 -0800764 def plug( self, **plugargs ):
765 """
766 plug the yanked mininet switch interface to a switch"""
767 main.log.info( 'Plug the switch interface attached to a switch' )
768 args = utilities.parse_args( [ "SW", "INTF" ], **plugargs )
769 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
770 intf = args[ "INTF" ] if args[ "INTF" ] is not None else ""
771 command = "py " + str( sw ) + '.attach("' + str(intf) + '")'
Jon Hall6094a362014-04-11 14:46:56 -0700772 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800773 response = self.execute(
774 cmd=command,
775 prompt="mininet>",
776 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800777 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800778 main.log.error( self.name + ": EOF exception found" )
779 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700780 main.cleanup()
781 main.exit()
adminbae64d82013-08-01 10:50:15 -0700782 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800783
Jon Hall7eb38402015-01-08 17:19:54 -0800784 def dpctl( self, **dpctlargs ):
785 """
786 Run dpctl command on all switches."""
787 main.log.info( 'Run dpctl command on all switches' )
788 args = utilities.parse_args( [ "CMD", "ARGS" ], **dpctlargs )
789 cmd = args[ "CMD" ] if args[ "CMD" ] is not None else ""
790 cmdargs = args[ "ARGS" ] if args[ "ARGS" ] is not None else ""
791 command = "dpctl " + cmd + " " + str( cmdargs )
792 try:
793 response = self.execute(
794 cmd=command,
795 prompt="mininet>",
796 timeout=10 )
797 except pexpect.EOF:
798 main.log.error( self.name + ": EOF exception found" )
799 main.log.error( self.name + ": " + self.handle.before )
800 main.cleanup()
801 main.exit()
802 return main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -0800803
Jon Hall7eb38402015-01-08 17:19:54 -0800804 def get_version( self ):
805 file_input = path + '/lib/Mininet/INSTALL'
806 version = super( Mininet, self ).get_version()
adminbae64d82013-08-01 10:50:15 -0700807 pattern = 'Mininet\s\w\.\w\.\w\w*'
Jon Hall7eb38402015-01-08 17:19:54 -0800808 for line in open( file_input, 'r' ).readlines():
809 result = re.match( pattern, line )
adminbae64d82013-08-01 10:50:15 -0700810 if result:
Jon Hall7eb38402015-01-08 17:19:54 -0800811 version = result.group( 0 )
Jon Hallec3c21e2014-11-10 22:22:37 -0500812 return version
adminbae64d82013-08-01 10:50:15 -0700813
Jon Hall7eb38402015-01-08 17:19:54 -0800814 def get_sw_controller( self, sw ):
815 """
Jon Hallec3c21e2014-11-10 22:22:37 -0500816 Parameters:
817 sw: The name of an OVS switch. Example "s1"
818 Return:
Jon Hall7eb38402015-01-08 17:19:54 -0800819 The output of the command from the mininet cli
820 or main.FALSE on timeout"""
821 command = "sh ovs-vsctl get-controller " + str( sw )
admin2a9548d2014-06-17 14:08:07 -0700822 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800823 response = self.execute(
824 cmd=command,
825 prompt="mininet>",
826 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -0700827 if response:
Jon Hallec3c21e2014-11-10 22:22:37 -0500828 return response
admin2a9548d2014-06-17 14:08:07 -0700829 else:
830 return main.FALSE
831 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800832 main.log.error( self.name + ": EOF exception found" )
833 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -0700834 main.cleanup()
835 main.exit()
adminbae64d82013-08-01 10:50:15 -0700836
Jon Hall7eb38402015-01-08 17:19:54 -0800837 def assign_sw_controller( self, **kwargs ):
838 """
839 count is only needed if there is more than 1 controller"""
840 args = utilities.parse_args( [ "COUNT" ], **kwargs )
841 count = args[ "COUNT" ] if args != {} else 1
Jon Hallf89c8552014-04-02 13:14:06 -0700842
843 argstring = "SW"
Jon Hall7eb38402015-01-08 17:19:54 -0800844 for j in range( count ):
845 argstring = argstring + ",IP" + \
846 str( j + 1 ) + ",PORT" + str( j + 1 )
847 args = utilities.parse_args( argstring.split( "," ), **kwargs )
Jon Hallf89c8552014-04-02 13:14:06 -0700848
Jon Hall7eb38402015-01-08 17:19:54 -0800849 sw = args[ "SW" ] if args[ "SW" ] is not None else ""
850 ptcpA = int( args[ "PORT1" ] ) + \
851 int( sw ) if args[ "PORT1" ] is not None else ""
852 ptcpB = "ptcp:" + str( ptcpA ) if ptcpA != "" else ""
Jon Hallfbc828e2015-01-06 17:30:19 -0800853
Jon Hall7eb38402015-01-08 17:19:54 -0800854 command = "sh ovs-vsctl set-controller s" + \
855 str( sw ) + " " + ptcpB + " "
856 for j in range( count ):
857 i = j + 1
858 args = utilities.parse_args(
859 [ "IP" + str( i ), "PORT" + str( i ) ], **kwargs )
860 ip = args[
861 "IP" +
862 str( i ) ] if args[
863 "IP" +
864 str( i ) ] is not None else ""
865 port = args[
866 "PORT" +
867 str( i ) ] if args[
868 "PORT" +
869 str( i ) ] is not None else ""
870 tcp = "tcp:" + str( ip ) + ":" + str( port ) + \
871 " " if ip != "" else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700872 command = command + tcp
Jon Hall6094a362014-04-11 14:46:56 -0700873 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800874 self.execute( cmd=command, prompt="mininet>", timeout=5 )
Jon Hall6094a362014-04-11 14:46:56 -0700875 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800876 main.log.error( self.name + ": EOF exception found" )
877 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -0700878 main.cleanup()
879 main.exit()
880 except:
Jon Hall7eb38402015-01-08 17:19:54 -0800881 main.log.info( self.name + ":" * 50 )
Jon Hall6094a362014-04-11 14:46:56 -0700882 main.log.error( traceback.print_exc() )
Jon Hall7eb38402015-01-08 17:19:54 -0800883 main.log.info(":" * 50 )
Jon Hall6094a362014-04-11 14:46:56 -0700884 main.cleanup()
885 main.exit()
adminbae64d82013-08-01 10:50:15 -0700886
Jon Hall7eb38402015-01-08 17:19:54 -0800887 def delete_sw_controller( self, sw ):
888 """
889 Removes the controller target from sw"""
890 command = "sh ovs-vsctl del-controller " + str( sw )
Jon Hall0819fd92014-05-23 12:08:13 -0700891 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800892 response = self.execute(
893 cmd=command,
894 prompt="mininet>",
895 timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800896 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800897 main.log.error( self.name + ": EOF exception found" )
898 main.log.error( self.name + ": " + self.handle.before )
Jon Hall0819fd92014-05-23 12:08:13 -0700899 main.cleanup()
900 main.exit()
901 else:
Jon Hall7eb38402015-01-08 17:19:54 -0800902 main.log.info( response )
Jon Hall0819fd92014-05-23 12:08:13 -0700903
Jon Hallb1290e82014-11-18 16:17:48 -0500904 def add_switch( self, sw, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -0800905 """
Jon Hallb1290e82014-11-18 16:17:48 -0500906 adds a switch to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -0800907 NOTE: This uses a custom mn function. MN repo should be on
908 jhall11/topology_refactoring2 branch
Jon Hallb1290e82014-11-18 16:17:48 -0500909 NOTE: cannot currently specify what type of switch
910 required params:
911 switchname = name of the new switch as a string
912 optional keyvalues:
913 dpid = "dpid"
914 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -0800915 """
916 dpid = kwargs.get( 'dpid', '' )
Jon Hallffb386d2014-11-21 13:43:38 -0800917 command = "addswitch " + str( sw ) + " " + str( dpid )
Jon Hallb1290e82014-11-18 16:17:48 -0500918 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800919 response = self.execute(
920 cmd=command,
921 prompt="mininet>",
922 timeout=10 )
923 if re.search( "already exists!", response ):
924 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500925 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800926 elif re.search( "Error", response ):
927 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500928 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800929 elif re.search( "usage:", response ):
930 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500931 return main.FALSE
932 else:
933 return main.TRUE
934 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800935 main.log.error( self.name + ": EOF exception found" )
936 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500937 main.cleanup()
938 main.exit()
939
940 def del_switch( self, sw ):
Jon Hall7eb38402015-01-08 17:19:54 -0800941 """
Jon Hallbe6dfc42015-01-12 17:37:25 -0800942 delete a switch from the mininet topology
943 NOTE: This uses a custom mn function. MN repo should be on
944 jhall11/topology_refactoring2 branch
945 required params:
Jon Hallb1290e82014-11-18 16:17:48 -0500946 switchname = name of the switch as a string
Jon Hallbe6dfc42015-01-12 17:37:25 -0800947 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800948 command = "delswitch " + str( sw )
Jon Hallb1290e82014-11-18 16:17:48 -0500949 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800950 response = self.execute(
951 cmd=command,
952 prompt="mininet>",
953 timeout=10 )
954 if re.search( "no switch named", response ):
955 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500956 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800957 elif re.search( "Error", response ):
958 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500959 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800960 elif re.search( "usage:", response ):
961 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500962 return main.FALSE
963 else:
964 return main.TRUE
965 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800966 main.log.error( self.name + ": EOF exception found" )
967 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -0500968 main.cleanup()
969 main.exit()
970
971 def add_link( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -0800972 """
973 add a link to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -0800974 NOTE: This uses a custom mn function. MN repo should be on
975 jhall11/topology_refactoring2 branch
Jon Hall7eb38402015-01-08 17:19:54 -0800976 NOTE: cannot currently specify what type of link
977 required params:
978 node1 = the string node name of the first endpoint of the link
979 node2 = the string node name of the second endpoint of the link
980 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -0800981 command = "addlink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -0500982 try:
Jon Hall7eb38402015-01-08 17:19:54 -0800983 response = self.execute(
984 cmd=command,
985 prompt="mininet>",
986 timeout=10 )
987 if re.search( "doesnt exist!", response ):
988 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500989 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800990 elif re.search( "Error", response ):
991 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500992 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -0800993 elif re.search( "usage:", response ):
994 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -0500995 return main.FALSE
996 else:
997 return main.TRUE
998 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -0800999 main.log.error( self.name + ": EOF exception found" )
1000 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001001 main.cleanup()
1002 main.exit()
1003
1004 def del_link( self, node1, node2 ):
Jon Hall7eb38402015-01-08 17:19:54 -08001005 """
1006 delete a link from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001007 NOTE: This uses a custom mn function. MN repo should be on
1008 jhall11/topology_refactoring2 branch
Jon Hall7eb38402015-01-08 17:19:54 -08001009 required params:
1010 node1 = the string node name of the first endpoint of the link
1011 node2 = the string node name of the second endpoint of the link
1012 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001013 command = "dellink " + str( node1 ) + " " + str( node2 )
Jon Hallb1290e82014-11-18 16:17:48 -05001014 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001015 response = self.execute(
1016 cmd=command,
1017 prompt="mininet>",
1018 timeout=10 )
1019 if re.search( "no node named", response ):
1020 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001021 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001022 elif re.search( "Error", response ):
1023 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001024 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001025 elif re.search( "usage:", response ):
1026 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001027 return main.FALSE
1028 else:
1029 return main.TRUE
1030 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001031 main.log.error( self.name + ": EOF exception found" )
1032 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001033 main.cleanup()
1034 main.exit()
1035
1036 def add_host( self, hostname, **kwargs ):
Jon Hall7eb38402015-01-08 17:19:54 -08001037 """
Jon Hallb1290e82014-11-18 16:17:48 -05001038 Add a host to the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001039 NOTE: This uses a custom mn function. MN repo should be on
1040 jhall11/topology_refactoring2 branch
Jon Hallb1290e82014-11-18 16:17:48 -05001041 NOTE: cannot currently specify what type of host
1042 required params:
1043 hostname = the string hostname
1044 optional key-value params
1045 switch = "switch name"
1046 returns: main.FASLE on an error, else main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001047 """
1048 switch = kwargs.get( 'switch', '' )
Jon Hallffb386d2014-11-21 13:43:38 -08001049 command = "addhost " + str( hostname ) + " " + str( switch )
Jon Hallb1290e82014-11-18 16:17:48 -05001050 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001051 response = self.execute(
1052 cmd=command,
1053 prompt="mininet>",
1054 timeout=10 )
1055 if re.search( "already exists!", response ):
1056 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001057 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001058 elif re.search( "doesnt exists!", response ):
1059 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001060 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001061 elif re.search( "Error", response ):
1062 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001063 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001064 elif re.search( "usage:", response ):
1065 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001066 return main.FALSE
1067 else:
1068 return main.TRUE
1069 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001070 main.log.error( self.name + ": EOF exception found" )
1071 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001072 main.cleanup()
1073 main.exit()
1074
1075 def del_host( self, hostname ):
Jon Hall7eb38402015-01-08 17:19:54 -08001076 """
1077 delete a host from the mininet topology
Jon Hallbe6dfc42015-01-12 17:37:25 -08001078 NOTE: This uses a custom mn function. MN repo should be on
1079 jhall11/topology_refactoring2 branch
Jon Hall7eb38402015-01-08 17:19:54 -08001080 NOTE: this uses a custom mn function
1081 required params:
1082 hostname = the string hostname
1083 returns: main.FASLE on an error, else main.TRUE"""
Jon Hallffb386d2014-11-21 13:43:38 -08001084 command = "delhost " + str( hostname )
Jon Hallb1290e82014-11-18 16:17:48 -05001085 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001086 response = self.execute(
1087 cmd=command,
1088 prompt="mininet>",
1089 timeout=10 )
1090 if re.search( "no host named", response ):
1091 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001092 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001093 elif re.search( "Error", response ):
1094 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001095 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001096 elif re.search( "usage:", response ):
1097 main.log.warn( response )
Jon Hallb1290e82014-11-18 16:17:48 -05001098 return main.FALSE
1099 else:
1100 return main.TRUE
1101 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001102 main.log.error( self.name + ": EOF exception found" )
1103 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001104 main.cleanup()
1105 main.exit()
Jon Hall0819fd92014-05-23 12:08:13 -07001106
Jon Hall7eb38402015-01-08 17:19:54 -08001107 def disconnect( self ):
1108 main.log.info( self.name + ": Disconnecting mininet..." )
adminbae64d82013-08-01 10:50:15 -07001109 response = ''
1110 if self.handle:
Jon Hall6094a362014-04-11 14:46:56 -07001111 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001112 response = self.execute(
1113 cmd="exit",
1114 prompt="(.*)",
1115 timeout=120 )
1116 response = self.execute(
1117 cmd="exit",
1118 prompt="(.*)",
1119 timeout=120 )
1120 self.handle.sendline( "sudo mn -c" )
shahshreya328c2a72014-11-17 10:19:50 -08001121 response = main.TRUE
Jon Hallfbc828e2015-01-06 17:30:19 -08001122 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001123 main.log.error( self.name + ": EOF exception found" )
1124 main.log.error( self.name + ": " + self.handle.before )
Jon Hall6094a362014-04-11 14:46:56 -07001125 main.cleanup()
1126 main.exit()
Jon Hall7eb38402015-01-08 17:19:54 -08001127 else:
1128 main.log.error( self.name + ": Connection failed to the host" )
adminbae64d82013-08-01 10:50:15 -07001129 response = main.FALSE
Jon Hallfbc828e2015-01-06 17:30:19 -08001130 return response
1131
Jon Hall7eb38402015-01-08 17:19:54 -08001132 def arping( self, src, dest, destmac ):
1133 self.handle.sendline( '' )
1134 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001135
Jon Hall7eb38402015-01-08 17:19:54 -08001136 self.handle.sendline( src + ' arping ' + dest )
admin07529932013-11-22 14:58:28 -08001137 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001138 self.handle.expect( [ destmac, pexpect.EOF, pexpect.TIMEOUT ] )
1139 main.log.info( self.name + ": ARP successful" )
1140 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001141 return main.TRUE
1142 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001143 main.log.warn( self.name + ": ARP FAILURE" )
1144 self.handle.expect( [ "mininet", pexpect.EOF, pexpect.TIMEOUT ] )
admin07529932013-11-22 14:58:28 -08001145 return main.FALSE
1146
Jon Hall7eb38402015-01-08 17:19:54 -08001147 def decToHex( self, num ):
1148 return hex( num ).split( 'x' )[ 1 ]
Jon Hallfbc828e2015-01-06 17:30:19 -08001149
Jon Hall7eb38402015-01-08 17:19:54 -08001150 def getSwitchFlowCount( self, switch ):
1151 """
1152 return the Flow Count of the switch"""
admin2a9548d2014-06-17 14:08:07 -07001153 if self.handle:
1154 cmd = "sh ovs-ofctl dump-aggregate %s" % switch
1155 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001156 response = self.execute(
1157 cmd=cmd,
1158 prompt="mininet>",
1159 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001160 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001161 main.log.error( self.name + ": EOF exception found" )
1162 main.log.error( self.name + " " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001163 main.cleanup()
1164 main.exit()
1165 pattern = "flow_count=(\d+)"
Jon Hall7eb38402015-01-08 17:19:54 -08001166 result = re.search( pattern, response, re.MULTILINE )
admin2a9548d2014-06-17 14:08:07 -07001167 if result is None:
Jon Hall7eb38402015-01-08 17:19:54 -08001168 main.log.info(
1169 "Couldn't find flows on switch %s, found: %s" %
1170 ( switch, response ) )
admin2a9548d2014-06-17 14:08:07 -07001171 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001172 return result.group( 1 )
admin2a9548d2014-06-17 14:08:07 -07001173 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001174 main.log.error( "Connection failed to the Mininet host" )
Jon Hallfbc828e2015-01-06 17:30:19 -08001175
Jon Hall7eb38402015-01-08 17:19:54 -08001176 def check_flows( self, sw, dump_format=None ):
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001177 if dump_format:
Jon Hall7eb38402015-01-08 17:19:54 -08001178 command = "sh ovs-ofctl -F " + \
1179 dump_format + " dump-flows " + str( sw )
Ahmed El-Hassanyb6545eb2014-08-01 11:32:10 -07001180 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001181 command = "sh ovs-ofctl dump-flows " + str( sw )
admin2a9548d2014-06-17 14:08:07 -07001182 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001183 response = self.execute(
1184 cmd=command,
1185 prompt="mininet>",
1186 timeout=10 )
admin2a9548d2014-06-17 14:08:07 -07001187 return response
1188 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001189 main.log.error( self.name + ": EOF exception found" )
1190 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001191 main.cleanup()
1192 main.exit()
1193 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001194 main.log.info( response )
admin2a9548d2014-06-17 14:08:07 -07001195
Jon Hall7eb38402015-01-08 17:19:54 -08001196 def start_tcpdump( self, filename, intf="eth0", port="port 6633" ):
1197 """
1198 Runs tpdump on an intferface and saves the file
1199 intf can be specified, or the default eth0 is used"""
admin2a9548d2014-06-17 14:08:07 -07001200 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001201 self.handle.sendline( "" )
1202 self.handle.expect( "mininet>" )
1203 self.handle.sendline(
1204 "sh sudo tcpdump -n -i " +
1205 intf +
1206 " " +
1207 port +
1208 " -w " +
1209 filename.strip() +
1210 " &" )
1211 self.handle.sendline( "" )
1212 i = self.handle.expect( [ 'No\ssuch\device',
1213 'listening\son',
1214 pexpect.TIMEOUT,
1215 "mininet>" ],
1216 timeout=10 )
1217 main.log.warn( self.handle.before + self.handle.after )
1218 self.handle.sendline( "" )
1219 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001220 if i == 0:
Jon Hall7eb38402015-01-08 17:19:54 -08001221 main.log.error(
1222 self.name +
1223 ": tcpdump - No such device exists. " +
1224 "tcpdump attempted on: " +
1225 intf )
admin2a9548d2014-06-17 14:08:07 -07001226 return main.FALSE
1227 elif i == 1:
Jon Hall7eb38402015-01-08 17:19:54 -08001228 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -07001229 return main.TRUE
1230 elif i == 2:
Jon Hall7eb38402015-01-08 17:19:54 -08001231 main.log.error(
1232 self.name +
1233 ": tcpdump command timed out! Check interface name," +
1234 " given interface was: " +
1235 intf )
admin2a9548d2014-06-17 14:08:07 -07001236 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001237 elif i == 3:
1238 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001239 return main.TRUE
1240 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001241 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -07001242 return main.FALSE
1243 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001244 main.log.error( self.name + ": EOF exception found" )
1245 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001246 main.cleanup()
1247 main.exit()
1248 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001249 main.log.info( self.name + ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001250 main.log.error( traceback.print_exc() )
Jon Hall7eb38402015-01-08 17:19:54 -08001251 main.log.info(":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001252 main.cleanup()
1253 main.exit()
1254
Jon Hall7eb38402015-01-08 17:19:54 -08001255 def stop_tcpdump( self ):
admin2a9548d2014-06-17 14:08:07 -07001256 "pkills tcpdump"
1257 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001258 self.handle.sendline( "sh sudo pkill tcpdump" )
1259 self.handle.expect( "mininet>" )
1260 self.handle.sendline( "" )
1261 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001262 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001263 main.log.error( self.name + ": EOF exception found" )
1264 main.log.error( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -07001265 main.cleanup()
1266 main.exit()
1267 except:
Jon Hall7eb38402015-01-08 17:19:54 -08001268 main.log.info( self.name + ":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001269 main.log.error( traceback.print_exc() )
Jon Hall7eb38402015-01-08 17:19:54 -08001270 main.log.info(":" * 50 )
admin2a9548d2014-06-17 14:08:07 -07001271 main.cleanup()
1272 main.exit()
1273
Jon Hall7eb38402015-01-08 17:19:54 -08001274 def compare_switches( self, topo, switches_json ):
1275 """
1276 Compare mn and onos switches
1277 topo: sts TestONTopology object
1278 switches_json: parsed json object from the onos devices api
Jon Hall3d87d502014-10-17 18:37:42 -04001279
Jon Hall7eb38402015-01-08 17:19:54 -08001280 This uses the sts TestONTopology object"""
1281 # main.log.debug( "Switches_json string: ", switches_json )
1282 output = { "switches": [] }
1283 # iterate through the MN topology and pull out switches and and port
1284 # info
1285 for switch in topo.graph.switches:
Jon Hall3d87d502014-10-17 18:37:42 -04001286 ports = []
1287 for port in switch.ports.values():
Jon Hall7eb38402015-01-08 17:19:54 -08001288 ports.append( { 'of_port': port.port_no,
1289 'mac': str( port.hw_addr ).replace( '\'',
1290 ''),
1291 'name': port.name } )
1292 output[ 'switches' ].append( {
1293 "name": switch.name,
1294 "dpid": str( switch.dpid ).zfill( 16 ),
1295 "ports": ports } )
Jon Hall3d87d502014-10-17 18:37:42 -04001296
Jon Hall7eb38402015-01-08 17:19:54 -08001297 # print "mn"
1298 # print json.dumps( output,
1299 # sort_keys=True,
1300 # indent=4,
1301 # separators=( ',', ': ' ) )
1302 # print "onos"
1303 # print json.dumps( switches_json,
1304 # sort_keys=True,
1305 # indent=4,
1306 # separators=( ',', ': ' ) )
Jon Hall3d87d502014-10-17 18:37:42 -04001307
1308 # created sorted list of dpid's in MN and ONOS for comparison
Jon Hall7eb38402015-01-08 17:19:54 -08001309 mnDPIDs = []
1310 for switch in output[ 'switches' ]:
1311 mnDPIDs.append( switch[ 'dpid' ].lower() )
Jon Hall3d87d502014-10-17 18:37:42 -04001312 mnDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001313 # print "List of Mininet switch DPID's"
1314 # print mnDPIDs
1315 if switches_json == "": # if rest call fails
1316 main.log.error(
1317 self.name +
1318 ".compare_switches(): Empty JSON object given from ONOS" )
Jon Hall3d87d502014-10-17 18:37:42 -04001319 return main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001320 onos = switches_json
1321 onosDPIDs = []
Jon Hall3d87d502014-10-17 18:37:42 -04001322 for switch in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001323 if switch[ 'available' ]:
1324 onosDPIDs.append(
1325 switch[ 'id' ].replace(
1326 ":",
1327 '' ).replace(
1328 "of",
1329 '' ).lower() )
1330 # else:
1331 # print "Switch is unavailable:"
1332 # print switch
Jon Hall3d87d502014-10-17 18:37:42 -04001333 onosDPIDs.sort()
Jon Hall7eb38402015-01-08 17:19:54 -08001334 # print "List of ONOS switch DPID's"
1335 # print onosDPIDs
Jon Hall3d87d502014-10-17 18:37:42 -04001336
Jon Hall7eb38402015-01-08 17:19:54 -08001337 if mnDPIDs != onosDPIDs:
Jon Hall3d87d502014-10-17 18:37:42 -04001338 switch_results = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001339 main.log.report( "Switches in MN but not in ONOS:" )
1340 list1 = [ switch for switch in mnDPIDs if switch not in onosDPIDs ]
1341 main.log.report( str( list1 ) )
1342 main.log.report( "Switches in ONOS but not in MN:" )
1343 list2 = [ switch for switch in onosDPIDs if switch not in mnDPIDs ]
1344 main.log.report(str( list2 ) )
1345 else: # list of dpid's match in onos and mn
Jon Hall3d87d502014-10-17 18:37:42 -04001346 switch_results = main.TRUE
1347 return switch_results
1348
Jon Hall7eb38402015-01-08 17:19:54 -08001349 def compare_ports( self, topo, ports_json ):
1350 """
Jon Hall72cf1dc2014-10-20 21:04:50 -04001351 Compare mn and onos ports
1352 topo: sts TestONTopology object
1353 ports_json: parsed json object from the onos ports api
1354
Jon Hallfbc828e2015-01-06 17:30:19 -08001355 Dependencies:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001356 1. This uses the sts TestONTopology object
1357 2. numpy - "sudo pip install numpy"
1358
Jon Hall7eb38402015-01-08 17:19:54 -08001359 """
1360 # FIXME: this does not look for extra ports in ONOS, only checks that
1361 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001362 from numpy import uint64
Jon Hallb1290e82014-11-18 16:17:48 -05001363 ports_results = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001364 output = { "switches": [] }
1365 # iterate through the MN topology and pull out switches and and port
1366 # info
1367 for switch in topo.graph.switches:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001368 ports = []
1369 for port in switch.ports.values():
Jon Hall7eb38402015-01-08 17:19:54 -08001370 # print port.hw_addr.toStr( separator='' )
Jon Hall39f29df2014-11-04 19:30:21 -05001371 tmp_port = {}
Jon Hall7eb38402015-01-08 17:19:54 -08001372 tmp_port[ 'of_port' ] = port.port_no
1373 tmp_port[ 'mac' ] = str( port.hw_addr ).replace( '\'', '' )
1374 tmp_port[ 'name' ] = port.name
1375 tmp_port[ 'enabled' ] = port.enabled
Jon Hall39f29df2014-11-04 19:30:21 -05001376
Jon Hall7eb38402015-01-08 17:19:54 -08001377 ports.append( tmp_port )
Jon Hall39f29df2014-11-04 19:30:21 -05001378 tmp_switch = {}
Jon Hall7eb38402015-01-08 17:19:54 -08001379 tmp_switch[ 'name' ] = switch.name
1380 tmp_switch[ 'dpid' ] = str( switch.dpid ).zfill( 16 )
1381 tmp_switch[ 'ports' ] = ports
Jon Hall39f29df2014-11-04 19:30:21 -05001382
Jon Hall7eb38402015-01-08 17:19:54 -08001383 output[ 'switches' ].append( tmp_switch )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001384
Jon Hall7eb38402015-01-08 17:19:54 -08001385 # PORTS
1386 for mn_switch in output[ 'switches' ]:
Jon Hall72cf1dc2014-10-20 21:04:50 -04001387 mn_ports = []
1388 onos_ports = []
Jon Hallb1290e82014-11-18 16:17:48 -05001389 switch_result = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001390 for port in mn_switch[ 'ports' ]:
1391 if port[ 'enabled' ]:
1392 mn_ports.append( port[ 'of_port' ] )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001393 for onos_switch in ports_json:
Jon Hall7eb38402015-01-08 17:19:54 -08001394 # print "Iterating through a new switch as seen by ONOS"
1395 # print onos_switch
1396 if onos_switch[ 'device' ][ 'available' ]:
1397 if onos_switch[ 'device' ][ 'id' ].replace(
1398 ':',
1399 '' ).replace(
1400 "of",
1401 '' ) == mn_switch[ 'dpid' ]:
1402 for port in onos_switch[ 'ports' ]:
1403 if port[ 'isEnabled' ]:
1404 if port[ 'port' ] == 'local':
1405 # onos_ports.append( 'local' )
1406 onos_ports.append( long( uint64( -2 ) ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001407 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001408 onos_ports.append( int( port[ 'port' ] ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001409 break
Jon Hall7eb38402015-01-08 17:19:54 -08001410 mn_ports.sort( key=float )
1411 onos_ports.sort( key=float )
1412 # print "\nPorts for Switch %s:" % ( mn_switch[ 'name' ] )
1413 # print "\tmn_ports[] = ", mn_ports
1414 # print "\tonos_ports[] = ", onos_ports
Jon Hallb1290e82014-11-18 16:17:48 -05001415 mn_ports_log = mn_ports
1416 onos_ports_log = onos_ports
Jon Hall7eb38402015-01-08 17:19:54 -08001417 mn_ports = [ x for x in mn_ports ]
1418 onos_ports = [ x for x in onos_ports ]
Jon Hall38481722014-11-04 16:50:05 -05001419
Jon Hall7eb38402015-01-08 17:19:54 -08001420 # TODO: handle other reserved port numbers besides LOCAL
1421 # NOTE: Reserved ports
1422 # Local port: -2 in Openflow, ONOS shows 'local', we store as
1423 # long( uint64( -2 ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001424 for mn_port in mn_ports_log:
1425 if mn_port in onos_ports:
Jon Hall7eb38402015-01-08 17:19:54 -08001426 # don't set results to true here as this is just one of
1427 # many checks and it might override a failure
1428 mn_ports.remove( mn_port )
1429 onos_ports.remove( mn_port )
1430 # NOTE: OVS reports this as down since there is no link
Jon Hallb1290e82014-11-18 16:17:48 -05001431 # So ignoring these for now
Jon Hall7eb38402015-01-08 17:19:54 -08001432 # TODO: Come up with a better way of handling these
Jon Hallb1290e82014-11-18 16:17:48 -05001433 if 65534 in mn_ports:
Jon Hall7eb38402015-01-08 17:19:54 -08001434 mn_ports.remove( 65534 )
1435 if long( uint64( -2 ) ) in onos_ports:
1436 onos_ports.remove( long( uint64( -2 ) ) )
1437 if len( mn_ports ): # the ports of this switch don't match
Jon Hallb1290e82014-11-18 16:17:48 -05001438 switch_result = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001439 main.log.warn( "Ports in MN but not ONOS: " + str( mn_ports ) )
1440 if len( onos_ports ): # the ports of this switch don't match
Jon Hallb1290e82014-11-18 16:17:48 -05001441 switch_result = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001442 main.log.warn(
1443 "Ports in ONOS but not MN: " +
1444 str( onos_ports ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001445 if switch_result == main.FALSE:
Jon Hall7eb38402015-01-08 17:19:54 -08001446 main.log.report(
1447 "The list of ports for switch %s(%s) does not match:" %
1448 ( mn_switch[ 'name' ], mn_switch[ 'dpid' ] ) )
1449 main.log.warn( "mn_ports[] = " + str( mn_ports_log ) )
1450 main.log.warn( "onos_ports[] = " + str( onos_ports_log ) )
Jon Hallb1290e82014-11-18 16:17:48 -05001451 ports_results = ports_results and switch_result
1452 return ports_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001453
Jon Hall7eb38402015-01-08 17:19:54 -08001454 def compare_links( self, topo, links_json ):
1455 """
1456 Compare mn and onos links
1457 topo: sts TestONTopology object
1458 links_json: parsed json object from the onos links api
Jon Hall72cf1dc2014-10-20 21:04:50 -04001459
Jon Hall7eb38402015-01-08 17:19:54 -08001460 This uses the sts TestONTopology object"""
1461 # FIXME: this does not look for extra links in ONOS, only checks that
1462 # ONOS has what is in MN
Jon Hall72cf1dc2014-10-20 21:04:50 -04001463 link_results = main.TRUE
Jon Hall7eb38402015-01-08 17:19:54 -08001464 output = { "switches": [] }
Jon Hall72cf1dc2014-10-20 21:04:50 -04001465 onos = links_json
Jon Hall7eb38402015-01-08 17:19:54 -08001466 # iterate through the MN topology and pull out switches and and port
1467 # info
1468 for switch in topo.graph.switches:
Jon Hall38481722014-11-04 16:50:05 -05001469 # print "Iterating though switches as seen by Mininet"
1470 # print switch
Jon Hall72cf1dc2014-10-20 21:04:50 -04001471 ports = []
1472 for port in switch.ports.values():
Jon Hall7eb38402015-01-08 17:19:54 -08001473 # print port.hw_addr.toStr( separator='' )
1474 ports.append( { 'of_port': port.port_no,
1475 'mac': str( port.hw_addr ).replace( '\'',
1476 ''),
1477 'name': port.name } )
1478 output[ 'switches' ].append( {
1479 "name": switch.name,
1480 "dpid": str( switch.dpid ).zfill( 16 ),
1481 "ports": ports } )
1482 # LINKS
Jon Hall72cf1dc2014-10-20 21:04:50 -04001483
Jon Hall7eb38402015-01-08 17:19:54 -08001484 mn_links = [
1485 link for link in topo.patch_panel.network_links if (
1486 link.port1.enabled and link.port2.enabled ) ]
1487 if 2 * len( mn_links ) == len( onos ):
Jon Hall72cf1dc2014-10-20 21:04:50 -04001488 link_results = main.TRUE
1489 else:
1490 link_results = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001491 main.log.report(
1492 "Mininet has %i bidirectional links and " +
1493 "ONOS has %i unidirectional links" %
1494 ( len( mn_links ), len( onos ) ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001495
Jon Hall7eb38402015-01-08 17:19:54 -08001496 # iterate through MN links and check if an ONOS link exists in
1497 # both directions
1498 # NOTE: Will currently only show mn links as down if they are
1499 # cut through STS. We can either do everything through STS or
1500 # wait for up_network_links and down_network_links to be
1501 # fully implemented.
Jon Hallfbc828e2015-01-06 17:30:19 -08001502 for link in mn_links:
Jon Hall7eb38402015-01-08 17:19:54 -08001503 # print "Link: %s" % link
1504 # TODO: Find a more efficient search method
Jon Hall72cf1dc2014-10-20 21:04:50 -04001505 node1 = None
1506 port1 = None
1507 node2 = None
1508 port2 = None
1509 first_dir = main.FALSE
1510 second_dir = main.FALSE
Jon Hall7eb38402015-01-08 17:19:54 -08001511 for switch in output[ 'switches' ]:
1512 # print "Switch: %s" % switch[ 'name' ]
1513 if switch[ 'name' ] == link.node1.name:
1514 node1 = switch[ 'dpid' ]
1515 for port in switch[ 'ports' ]:
1516 if str( port[ 'name' ] ) == str( link.port1 ):
1517 port1 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001518 if node1 is not None and node2 is not None:
1519 break
Jon Hall7eb38402015-01-08 17:19:54 -08001520 if switch[ 'name' ] == link.node2.name:
1521 node2 = switch[ 'dpid' ]
1522 for port in switch[ 'ports' ]:
1523 if str( port[ 'name' ] ) == str( link.port2 ):
1524 port2 = port[ 'of_port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001525 if node1 is not None and node2 is not None:
1526 break
1527
Jon Hall72cf1dc2014-10-20 21:04:50 -04001528 for onos_link in onos:
Jon Hall7eb38402015-01-08 17:19:54 -08001529 onos_node1 = onos_link[ 'src' ][ 'device' ].replace(
1530 ":",
1531 '' ).replace(
1532 "of",
1533 '' )
1534 onos_node2 = onos_link[ 'dst' ][ 'device' ].replace(
1535 ":",
1536 '' ).replace(
1537 "of",
1538 '' )
1539 onos_port1 = onos_link[ 'src' ][ 'port' ]
1540 onos_port2 = onos_link[ 'dst' ][ 'port' ]
Jon Hall72cf1dc2014-10-20 21:04:50 -04001541
Jon Hall72cf1dc2014-10-20 21:04:50 -04001542 # check onos link from node1 to node2
Jon Hall7eb38402015-01-08 17:19:54 -08001543 if str( onos_node1 ) == str( node1 ) and str(
1544 onos_node2 ) == str( node2 ):
1545 if int( onos_port1 ) == int( port1 ) and int(
1546 onos_port2 ) == int( port2 ):
Jon Hall72cf1dc2014-10-20 21:04:50 -04001547 first_dir = main.TRUE
1548 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001549 main.log.warn(
1550 'The port numbers do not match for ' +
1551 str( link ) +
1552 ' between ONOS and MN. When cheking ONOS for ' +
1553 'link %s/%s -> %s/%s' %
1554 ( node1,
1555 port1,
1556 node2,
1557 port2 ) +
1558 ' ONOS has the values %s/%s -> %s/%s' %
1559 ( onos_node1,
1560 onos_port1,
1561 onos_node2,
1562 onos_port2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001563
1564 # check onos link from node2 to node1
Jon Hall7eb38402015-01-08 17:19:54 -08001565 elif ( str( onos_node1 ) == str( node2 ) and
1566 str( onos_node2 ) == str( node1 ) ):
1567 if ( int( onos_port1 ) == int( port2 )
1568 and int( onos_port2 ) == int( port1 ) ):
Jon Hall72cf1dc2014-10-20 21:04:50 -04001569 second_dir = main.TRUE
1570 else:
Jon Hall7eb38402015-01-08 17:19:54 -08001571 main.log.warn(
1572 'The port numbers do not match for ' +
1573 str( link ) +
1574 ' between ONOS and MN. When cheking ONOS for ' +
1575 'link %s/%s -> %s/%s' %
1576 ( node2,
1577 port2,
1578 node1,
1579 port1 ) +
1580 ' ONOS has the values %s/%s -> %s/%s' %
1581 ( onos_node2,
1582 onos_port2,
1583 onos_node1,
1584 onos_port1 ) )
1585 else: # this is not the link you're looking for
Jon Hall72cf1dc2014-10-20 21:04:50 -04001586 pass
1587 if not first_dir:
Jon Hall7eb38402015-01-08 17:19:54 -08001588 main.log.report(
1589 'ONOS does not have the link %s/%s -> %s/%s' %
1590 ( node1, port1, node2, port2 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001591 if not second_dir:
Jon Hall7eb38402015-01-08 17:19:54 -08001592 main.log.report(
1593 'ONOS does not have the link %s/%s -> %s/%s' %
1594 ( node2, port2, node1, port1 ) )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001595 link_results = link_results and first_dir and second_dir
Jon Hall62df9242014-10-22 12:20:17 -04001596 return link_results
Jon Hall72cf1dc2014-10-20 21:04:50 -04001597
Jon Hall7eb38402015-01-08 17:19:54 -08001598 def get_hosts( self ):
1599 """
1600 Returns a list of all hosts
1601 Don't ask questions just use it"""
1602 self.handle.sendline( "" )
1603 self.handle.expect( "mininet>" )
Jon Hall72cf1dc2014-10-20 21:04:50 -04001604
Jon Hall7eb38402015-01-08 17:19:54 -08001605 self.handle.sendline( "py [ host.name for host in net.hosts ]" )
1606 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001607
andrewonlab3f0a4af2014-10-17 12:25:14 -04001608 handle_py = self.handle.before
Jon Hall7eb38402015-01-08 17:19:54 -08001609 handle_py = handle_py.split( "]\r\n", 1 )[ 1 ]
andrewonlab3f0a4af2014-10-17 12:25:14 -04001610 handle_py = handle_py.rstrip()
admin2a9548d2014-06-17 14:08:07 -07001611
Jon Hall7eb38402015-01-08 17:19:54 -08001612 self.handle.sendline( "" )
1613 self.handle.expect( "mininet>" )
admin2a9548d2014-06-17 14:08:07 -07001614
Jon Hall7eb38402015-01-08 17:19:54 -08001615 host_str = handle_py.replace( "]", "" )
1616 host_str = host_str.replace( "'", "" )
1617 host_str = host_str.replace( "[", "" )
1618 host_list = host_str.split( "," )
andrewonlab3f0a4af2014-10-17 12:25:14 -04001619
Jon Hallfbc828e2015-01-06 17:30:19 -08001620 return host_list
adminbae64d82013-08-01 10:50:15 -07001621
Jon Hall7eb38402015-01-08 17:19:54 -08001622 def update( self ):
1623 """
1624 updates the port address and status information for
1625 each port in mn"""
1626 # TODO: Add error checking. currently the mininet command has no output
1627 main.log.info( "Updateing MN port information" )
Jon Hallb1290e82014-11-18 16:17:48 -05001628 try:
Jon Hall7eb38402015-01-08 17:19:54 -08001629 self.handle.sendline( "" )
1630 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001631
Jon Hall7eb38402015-01-08 17:19:54 -08001632 self.handle.sendline( "update" )
1633 self.handle.expect( "update" )
1634 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001635
Jon Hall7eb38402015-01-08 17:19:54 -08001636 self.handle.sendline( "" )
1637 self.handle.expect( "mininet>" )
Jon Hall38481722014-11-04 16:50:05 -05001638
Jon Hallb1290e82014-11-18 16:17:48 -05001639 return main.TRUE
1640 except pexpect.EOF:
Jon Hall7eb38402015-01-08 17:19:54 -08001641 main.log.error( self.name + ": EOF exception found" )
1642 main.log.error( self.name + ": " + self.handle.before )
Jon Hallb1290e82014-11-18 16:17:48 -05001643 main.cleanup()
1644 main.exit()
1645
adminbae64d82013-08-01 10:50:15 -07001646if __name__ != "__main__":
1647 import sys
Jon Hall7eb38402015-01-08 17:19:54 -08001648 sys.modules[ __name__ ] = MininetCliDriver()