You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Copyright 2018 Open Networking Foundation (ONF) |
| 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 8 | |
| 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | This driver is used to interact with a physical network that SDN controller is controlling. |
| 24 | |
| 25 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 26 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 27 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 28 | |
| 29 | """ |
| 30 | import pexpect |
| 31 | import os |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 32 | import re |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 33 | import types |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 34 | import time |
| 35 | import itertools |
| 36 | import random |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 37 | from drivers.common.clidriver import CLI |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 38 | from core.graph import Graph |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 39 | |
| 40 | class NetworkDriver( CLI ): |
| 41 | |
| 42 | def __init__( self ): |
| 43 | """ |
| 44 | switches: a dictionary that maps switch names to components |
| 45 | hosts: a dictionary that maps host names to components |
| 46 | """ |
| 47 | self.name = None |
| 48 | self.home = None |
| 49 | self.handle = None |
| 50 | self.switches = {} |
| 51 | self.hosts = {} |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 52 | self.links = {} |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 53 | super( NetworkDriver, self ).__init__() |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 54 | self.graph = Graph() |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 55 | |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 56 | def connect( self, **connectargs ): |
| 57 | """ |
| 58 | Creates ssh handle for the SDN network "bench". |
| 59 | NOTE: |
| 60 | The ip_address would come from the topo file using the host tag, the |
| 61 | value can be an environment variable as well as a "localhost" to get |
| 62 | the ip address needed to ssh to the "bench" |
| 63 | """ |
| 64 | try: |
| 65 | for key in connectargs: |
| 66 | vars( self )[ key ] = connectargs[ key ] |
| 67 | self.name = self.options[ 'name' ] |
| 68 | try: |
| 69 | if os.getenv( str( self.ip_address ) ) is not None: |
| 70 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 71 | else: |
| 72 | main.log.info( self.name + |
| 73 | ": Trying to connect to " + |
| 74 | self.ip_address ) |
| 75 | except KeyError: |
| 76 | main.log.info( "Invalid host name," + |
| 77 | " connecting to local host instead" ) |
| 78 | self.ip_address = 'localhost' |
| 79 | except Exception as inst: |
| 80 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 81 | |
| 82 | self.handle = super( NetworkDriver, self ).connect( |
| 83 | user_name=self.user_name, |
| 84 | ip_address=self.ip_address, |
| 85 | port=self.port, |
| 86 | pwd=self.pwd ) |
| 87 | |
| 88 | if self.handle: |
| 89 | main.log.info( "Connected to network bench node" ) |
| 90 | return self.handle |
| 91 | else: |
| 92 | main.log.info( "Failed to create handle" ) |
| 93 | return main.FALSE |
| 94 | except pexpect.EOF: |
| 95 | main.log.error( self.name + ": EOF exception found" ) |
| 96 | main.log.error( self.name + ": " + self.handle.before ) |
| 97 | main.cleanAndExit() |
| 98 | except Exception: |
| 99 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 100 | main.cleanAndExit() |
| 101 | |
| 102 | def disconnect( self ): |
| 103 | """ |
| 104 | Called when test is complete to disconnect the handle. |
| 105 | """ |
| 106 | response = main.TRUE |
| 107 | try: |
| 108 | if self.handle: |
| 109 | self.handle.sendline( "exit" ) |
| 110 | self.handle.expect( "closed" ) |
| 111 | except pexpect.EOF: |
| 112 | main.log.error( self.name + ": EOF exception found" ) |
| 113 | main.log.error( self.name + ": " + self.handle.before ) |
| 114 | except Exception: |
| 115 | main.log.exception( self.name + ": Connection failed to the host" ) |
| 116 | response = main.FALSE |
| 117 | return response |
| 118 | |
| 119 | def connectToNet( self ): |
| 120 | """ |
| 121 | Connect to an existing physical network by getting information |
| 122 | of all switch and host components created |
| 123 | """ |
| 124 | try: |
| 125 | for key, value in main.componentDictionary.items(): |
| 126 | if hasattr( main, key ): |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 127 | if value[ 'type' ] in [ 'MininetSwitchDriver', 'OFDPASwitchDriver', 'StratumOSSwitchDriver' ]: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 128 | component = getattr( main, key ) |
| 129 | shortName = component.options[ 'shortName' ] |
| 130 | localName = self.name + "-" + shortName |
| 131 | self.copyComponent( key, localName ) |
| 132 | self.switches[ shortName ] = getattr( main, localName ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 133 | elif value[ 'type' ] in [ 'MininetHostDriver', 'HostDriver' ]: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 134 | component = getattr( main, key ) |
| 135 | shortName = component.options[ 'shortName' ] |
| 136 | localName = self.name + "-" + shortName |
| 137 | self.copyComponent( key, localName ) |
| 138 | self.hosts[ shortName ] = getattr( main, localName ) |
| 139 | main.log.debug( self.name + ": found switches: {}".format( self.switches ) ) |
| 140 | main.log.debug( self.name + ": found hosts: {}".format( self.hosts ) ) |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 141 | return main.TRUE |
| 142 | except Exception: |
| 143 | main.log.error( self.name + ": failed to connect to network" ) |
| 144 | return main.FALSE |
| 145 | |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 146 | def disconnectFromNet( self ): |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 147 | """ |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 148 | Disconnect from the physical network connected |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 149 | """ |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 150 | try: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 151 | for key, value in main.componentDictionary.items(): |
| 152 | if hasattr( main, key ) and key.startswith( self.name + "-" ): |
| 153 | self.removeComponent( key ) |
| 154 | self.switches = {} |
| 155 | self.hosts = {} |
| 156 | return main.TRUE |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 157 | except Exception: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 158 | main.log.error( self.name + ": failed to disconnect from network" ) |
| 159 | return main.FALSE |
| 160 | |
| 161 | def copyComponent( self, name, newName ): |
| 162 | """ |
| 163 | Copy the component initialized from the .topo file |
| 164 | The copied components are only supposed to be called within this driver |
| 165 | Required: |
| 166 | name: name of the component to be copied |
| 167 | newName: name of the new component |
| 168 | """ |
| 169 | try: |
| 170 | main.componentDictionary[ newName ] = main.componentDictionary[ name ].copy() |
| 171 | main.componentInit( newName ) |
| 172 | except Exception: |
| 173 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 174 | main.cleanAndExit() |
| 175 | |
| 176 | def removeHostComponent( self, name ): |
| 177 | """ |
| 178 | Remove host component |
| 179 | Required: |
| 180 | name: name of the component to be removed |
| 181 | """ |
| 182 | try: |
| 183 | self.removeComponent( name ) |
| 184 | except Exception: |
| 185 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 186 | main.cleanAndExit() |
| 187 | |
| 188 | def removeComponent( self, name ): |
| 189 | """ |
| 190 | Remove host/switch component |
| 191 | Required: |
| 192 | name: name of the component to be removed |
| 193 | """ |
| 194 | try: |
| 195 | component = getattr( main, name ) |
| 196 | except AttributeError: |
| 197 | main.log.error( "Component " + name + " does not exist." ) |
| 198 | return main.FALSE |
| 199 | try: |
| 200 | # Disconnect from component |
| 201 | component.disconnect() |
| 202 | # Delete component |
| 203 | delattr( main, name ) |
| 204 | # Delete component from ComponentDictionary |
| 205 | del( main.componentDictionary[ name ] ) |
| 206 | return main.TRUE |
| 207 | except Exception: |
| 208 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 209 | main.cleanAndExit() |
| 210 | |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 211 | def createHostComponent( self, name ): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 212 | """ |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 213 | Creates host component with the same parameters as the one copied to local. |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 214 | Arguments: |
| 215 | name - The string of the name of this component. The new component |
| 216 | will be assigned to main.<name> . |
| 217 | In addition, main.<name>.name = str( name ) |
| 218 | """ |
| 219 | try: |
| 220 | # look to see if this component already exists |
| 221 | getattr( main, name ) |
| 222 | except AttributeError: |
| 223 | # namespace is clear, creating component |
| 224 | localName = self.name + "-" + name |
| 225 | main.componentDictionary[ name ] = main.componentDictionary[ localName ].copy() |
| 226 | main.componentInit( name ) |
| 227 | except Exception: |
| 228 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 229 | main.cleanAndExit() |
| 230 | else: |
| 231 | # namespace is not clear! |
| 232 | main.log.error( name + " component already exists!" ) |
| 233 | main.cleanAndExit() |
| 234 | |
| 235 | def connectInbandHosts( self ): |
| 236 | """ |
| 237 | Connect to hosts using data plane IPs specified |
| 238 | """ |
| 239 | result = main.TRUE |
| 240 | try: |
| 241 | for hostName, hostComponent in self.hosts.items(): |
| 242 | if hostComponent.options[ 'inband' ] == 'True': |
| 243 | main.log.info( self.name + ": connecting inband host " + hostName ) |
| 244 | result = hostComponent.connectInband() and result |
| 245 | return result |
| 246 | except Exception: |
| 247 | main.log.error( self.name + ": failed to connect to inband hosts" ) |
| 248 | return main.FALSE |
| 249 | |
| 250 | def disconnectInbandHosts( self ): |
| 251 | """ |
| 252 | Terminate the connections to hosts using data plane IPs |
| 253 | """ |
| 254 | result = main.TRUE |
| 255 | try: |
| 256 | for hostName, hostComponent in self.hosts.items(): |
| 257 | if hostComponent.options[ 'inband' ] == 'True': |
| 258 | main.log.info( self.name + ": disconnecting inband host " + hostName ) |
| 259 | result = hostComponent.disconnectInband() and result |
| 260 | return result |
| 261 | except Exception: |
| 262 | main.log.error( self.name + ": failed to disconnect inband hosts" ) |
| 263 | return main.FALSE |
| 264 | |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 265 | def getSwitches( self, timeout=60, excludeNodes=[], includeStopped=False ): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 266 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 267 | Return a dictionary which maps short names to switch data |
| 268 | If includeStopped is True, stopped switches will also be included |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 269 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 270 | switches = {} |
| 271 | for switchName, switchComponent in self.switches.items(): |
| 272 | if switchName in excludeNodes: |
| 273 | continue |
| 274 | if not includeStopped and not switchComponent.isup: |
| 275 | continue |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 276 | try: |
| 277 | dpid = switchComponent.dpid |
| 278 | except AttributeError: |
| 279 | main.log.warn( "Switch has no dpid, ignore this if not an OpenFlow switch" ) |
| 280 | dpid = "0x0" |
| 281 | dpid = dpid.replace( '0x', '' ).zfill( 16 ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 282 | ports = switchComponent.ports |
| 283 | swClass = 'Unknown' |
| 284 | pid = None |
| 285 | options = None |
| 286 | switches[ switchName ] = { "dpid": dpid, |
| 287 | "ports": ports, |
| 288 | "swClass": swClass, |
| 289 | "pid": pid, |
| 290 | "options": options } |
| 291 | return switches |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 292 | |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 293 | def getHosts( self, hostClass=None ): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 294 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 295 | Return a dictionary which maps short names to host data |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 296 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 297 | hosts = {} |
| 298 | for hostName, hostComponent in self.hosts.items(): |
| 299 | interfaces = hostComponent.interfaces |
| 300 | hosts[ hostName ] = { "interfaces": interfaces } |
| 301 | return hosts |
| 302 | |
| 303 | def updateLinks( self, timeout=60, excludeNodes=[] ): |
| 304 | """ |
| 305 | Update self.links by getting up-to-date port information from |
| 306 | switches |
| 307 | """ |
| 308 | # TODO: also inlcude switch-to-host links |
| 309 | self.links = {} |
| 310 | for node1 in self.switches.keys(): |
| 311 | if node1 in excludeNodes: |
| 312 | continue |
| 313 | self.links[ node1 ] = {} |
| 314 | self.switches[ node1 ].updatePorts() |
| 315 | for port in self.switches[ node1 ].ports: |
| 316 | if not port[ 'enabled' ]: |
| 317 | continue |
| 318 | node2 = getattr( main, port[ 'node2' ] ).shortName |
| 319 | if node2 in excludeNodes: |
| 320 | continue |
| 321 | port1 = port[ 'of_port' ] |
| 322 | port2 = port[ 'port2' ] |
| 323 | if not self.links[ node1 ].get( node2 ): |
| 324 | self.links[ node1 ][ node2 ] = {} |
| 325 | # Check if this link already exists |
| 326 | if self.links.get( node2 ): |
| 327 | if self.links[ node2 ].get( node1 ): |
| 328 | if self.links[ node2 ].get( node1 ).get( port2 ): |
| 329 | assert self.links[ node2 ][ node1 ][ port2 ] == port1 |
| 330 | continue |
| 331 | self.links[ node1 ][ node2 ][ port1 ] = port2 |
| 332 | |
| 333 | def getLinks( self, timeout=60, excludeNodes=[] ): |
| 334 | """ |
| 335 | Return a list of links specify both node names and port numbers |
| 336 | """ |
| 337 | self.updateLinks( timeout=timeout, excludeNodes=excludeNodes ) |
| 338 | links = [] |
| 339 | for node1, nodeLinks in self.links.items(): |
| 340 | for node2, ports in nodeLinks.items(): |
| 341 | for port1, port2 in ports.items(): |
| 342 | links.append( { 'node1': node1, 'node2': node2, |
| 343 | 'port1': port1, 'port2': port2 } ) |
| 344 | return links |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 345 | |
| 346 | def getMacAddress( self, host ): |
| 347 | """ |
| 348 | Return MAC address of a host |
| 349 | """ |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 350 | try: |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 351 | hostComponent = self.hosts[ host ] |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 352 | response = hostComponent.ifconfig() |
| 353 | pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' |
| 354 | macAddressSearch = re.search( pattern, response, re.I ) |
| 355 | macAddress = macAddressSearch.group().split( " " )[ 1 ] |
| 356 | main.log.info( self.name + ": Mac-Address of Host " + host + " is " + macAddress ) |
| 357 | return macAddress |
| 358 | except Exception: |
| 359 | main.log.error( self.name + ": failed to get host MAC address" ) |
| 360 | |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 361 | def runCmdOnHost( self, hostName, cmd ): |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 362 | """ |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 363 | Run shell command on specified host and return output |
| 364 | Required: |
| 365 | hostName: name of the host e.g. "h1" |
| 366 | cmd: command to run on the host |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 367 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 368 | hostComponent = self.hosts[ hostName ] |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 369 | if hostComponent: |
| 370 | return hostComponent.command( cmd ) |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 371 | return None |
| 372 | |
| 373 | def assignSwController( self, sw, ip, port="6653", ptcp="" ): |
| 374 | """ |
| 375 | Description: |
| 376 | Assign switches to the controllers |
| 377 | Required: |
| 378 | sw - Short name of the switch specified in the .topo file, e.g. "s1". |
| 379 | It can also be a list of switch names. |
| 380 | ip - Ip addresses of controllers. This can be a list or a string. |
| 381 | Optional: |
| 382 | port - ONOS use port 6653, if no list of ports is passed, then |
| 383 | the all the controller will use 6653 as their port number |
| 384 | ptcp - ptcp number, This can be a string or a list that has |
| 385 | the same length as switch. This is optional and not required |
| 386 | when using ovs switches. |
| 387 | NOTE: If switches and ptcp are given in a list type they should have the |
| 388 | same length and should be in the same order, Eg. sw=[ 's1' ... n ] |
| 389 | ptcp=[ '6637' ... n ], s1 has ptcp number 6637 and so on. |
| 390 | |
| 391 | Return: |
| 392 | Returns main.TRUE if switches are correctly assigned to controllers, |
| 393 | otherwise it will return main.FALSE or an appropriate exception(s) |
| 394 | """ |
| 395 | switchList = [] |
| 396 | ptcpList = None |
| 397 | try: |
| 398 | if isinstance( sw, types.StringType ): |
| 399 | switchList.append( sw ) |
| 400 | if ptcp: |
| 401 | if isinstance( ptcp, types.StringType ): |
| 402 | ptcpList = [ ptcp ] |
| 403 | elif isinstance( ptcp, types.ListType ): |
| 404 | main.log.error( self.name + ": Only one switch is " + |
| 405 | "being set and multiple PTCP is " + |
| 406 | "being passed " ) |
| 407 | return main.FALSE |
| 408 | else: |
| 409 | main.log.error( self.name + ": Invalid PTCP" ) |
| 410 | return main.FALSE |
| 411 | |
| 412 | elif isinstance( sw, types.ListType ): |
| 413 | switchList = sw |
| 414 | if ptcp: |
| 415 | if isinstance( ptcp, types.ListType ): |
| 416 | if len( ptcp ) != len( sw ): |
| 417 | main.log.error( self.name + ": PTCP length = " + |
| 418 | str( len( ptcp ) ) + |
| 419 | " is not the same as switch" + |
| 420 | " length = " + |
| 421 | str( len( sw ) ) ) |
| 422 | return main.FALSE |
| 423 | else: |
| 424 | ptcpList = ptcp |
| 425 | else: |
| 426 | main.log.error( self.name + ": Invalid PTCP" ) |
| 427 | return main.FALSE |
| 428 | else: |
| 429 | main.log.error( self.name + ": Invalid switch type " ) |
| 430 | return main.FALSE |
| 431 | |
| 432 | assignResult = main.TRUE |
| 433 | index = 0 |
| 434 | for switch in switchList: |
| 435 | assigned = False |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 436 | switchComponent = self.switches[ switch ] |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 437 | if switchComponent: |
| 438 | ptcp = ptcpList[ index ] if ptcpList else "" |
| 439 | assignResult = assignResult and switchComponent.assignSwController( ip=ip, port=port, ptcp=ptcp ) |
| 440 | assigned = True |
| 441 | if not assigned: |
| 442 | main.log.error( self.name + ": Not able to find switch " + switch ) |
| 443 | assignResult = main.FALSE |
| 444 | index += 1 |
| 445 | return assignResult |
| 446 | |
| 447 | except Exception: |
| 448 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 449 | main.cleanAndExit() |
| 450 | |
| 451 | def pingall( self, protocol="IPv4", timeout=300, shortCircuit=False, acceptableFailed=0 ): |
| 452 | """ |
| 453 | Description: |
| 454 | Verifies the reachability of the hosts using ping command. |
| 455 | Optional: |
| 456 | protocol - use ping6 command if specified as "IPv6" |
| 457 | timeout( seconds ) - How long to wait before breaking the pingall |
| 458 | shortCircuit - Break the pingall based on the number of failed hosts ping |
| 459 | acceptableFailed - Set the number of acceptable failed pings for the |
| 460 | function to still return main.TRUE |
| 461 | Returns: |
| 462 | main.TRUE if pingall completes with no pings dropped |
| 463 | otherwise main.FALSE |
| 464 | """ |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 465 | try: |
| 466 | timeout = int( timeout ) |
| 467 | main.log.info( self.name + ": Checking reachabilty to the hosts using ping" ) |
| 468 | failedPings = 0 |
| 469 | returnValue = main.TRUE |
| 470 | ipv6 = True if protocol == "IPv6" else False |
| 471 | startTime = time.time() |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 472 | hostPairs = itertools.permutations( list( self.hosts.values() ), 2 ) |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 473 | for hostPair in list( hostPairs ): |
| 474 | ipDst = hostPair[ 1 ].options[ 'ip6' ] if ipv6 else hostPair[ 1 ].options[ 'ip' ] |
| 475 | pingResult = hostPair[ 0 ].ping( ipDst, ipv6=ipv6 ) |
| 476 | returnValue = returnValue and pingResult |
| 477 | if ( time.time() - startTime ) > timeout: |
| 478 | returnValue = main.FALSE |
| 479 | main.log.error( self.name + |
| 480 | ": Aborting pingall - " + |
| 481 | "Function took too long " ) |
| 482 | break |
| 483 | if not pingResult: |
| 484 | failedPings = failedPings + 1 |
| 485 | if failedPings > acceptableFailed: |
| 486 | returnValue = main.FALSE |
| 487 | if shortCircuit: |
| 488 | main.log.error( self.name + |
| 489 | ": Aborting pingall - " |
| 490 | + str( failedPings ) + |
| 491 | " pings failed" ) |
| 492 | break |
| 493 | return returnValue |
| 494 | except Exception: |
| 495 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 496 | main.cleanAndExit() |
| 497 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 498 | def pingallHosts( self, hostList, ipv6=False, wait=1, useScapy=False ): |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 499 | """ |
| 500 | Ping all specified IPv4 hosts |
| 501 | |
| 502 | Acceptable hostList: |
| 503 | - [ 'h1','h2','h3','h4' ] |
| 504 | |
| 505 | Returns main.TRUE if all hosts specified can reach |
| 506 | each other |
| 507 | |
| 508 | Returns main.FALSE if one or more of hosts specified |
| 509 | cannot reach each other""" |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 510 | hostComponentList = [] |
| 511 | for hostName in hostList: |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 512 | hostComponent = self.hosts[ str( hostName ) ] |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 513 | if hostComponent: |
| 514 | hostComponentList.append( hostComponent ) |
| 515 | try: |
| 516 | main.log.info( "Testing reachability between specified hosts" ) |
| 517 | isReachable = main.TRUE |
| 518 | pingResponse = "IPv4 ping across specified hosts\n" |
| 519 | failedPings = 0 |
| 520 | hostPairs = itertools.permutations( list( hostComponentList ), 2 ) |
| 521 | for hostPair in list( hostPairs ): |
| 522 | pingResponse += hostPair[ 0 ].options[ 'shortName' ] + " -> " |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 523 | ipDst = hostPair[ 1 ].options.get( 'ip6', hostPair[ 1 ].options[ 'ip' ] ) if ipv6 else hostPair[ 1 ].options[ 'ip' ] |
| 524 | srcIface = hostPair[ 0 ].interfaces[0].get( 'name' ) |
| 525 | dstIface = hostPair[ 1 ].interfaces[0].get( 'name' ) |
| 526 | srcMac = hostPair[0].interfaces[0].get( 'mac' ) |
| 527 | dstMac = hostPair[1].interfaces[0].get( 'mac' ) |
| 528 | if useScapy: |
| 529 | main.log.debug( "Pinging from " + str( hostPair[ 0 ].shortName ) + " to " + str( hostPair[ 1 ].shortName ) ) |
| 530 | srcIPs = hostPair[ 0 ].interfaces[0].get( 'ips' ) |
| 531 | dstIPs = hostPair[ 1 ].interfaces[0].get( 'ips' ) |
Siddesh | d984084 | 2021-08-06 19:26:05 +0000 | [diff] [blame] | 532 | srcVLANs = hostPair[0].interfaces[0].get( 'vlan' ) |
| 533 | if srcVLANs: |
| 534 | VLAN = srcVLANs[0] |
| 535 | else: |
| 536 | VLAN=None |
| 537 | dstVLANs = hostPair[1].interfaces[0].get( 'vlan' ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 538 | # Use scapy to send and recieve packets |
| 539 | hostPair[ 1 ].startScapy( ifaceName=dstIface ) |
| 540 | hostPair[ 1 ].addRoutes() |
Siddesh | d984084 | 2021-08-06 19:26:05 +0000 | [diff] [blame] | 541 | filters = [] |
| 542 | if srcMac: |
| 543 | filters.append( "ether src host %s" % srcMac ) |
| 544 | if srcIPs[0]: |
| 545 | filters.append( "ip src host %s" % srcIPs[0] ) |
| 546 | hostPair[ 1 ].startFilter( ifaceName=dstIface, pktFilter=" and ".join(filters) ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 547 | hostPair[ 0 ].startScapy( ifaceName=srcIface ) |
| 548 | hostPair[ 0 ].addRoutes() |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 549 | hostPair[ 0 ].buildEther( src=srcMac, dst=dstMac ) |
Siddesh | d984084 | 2021-08-06 19:26:05 +0000 | [diff] [blame] | 550 | if VLAN: |
| 551 | hostPair[ 0 ].buildVLAN( vlan=VLAN ) |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 552 | hostPair[ 0 ].buildIP( src=srcIPs[0], dst=dstIPs[0] ) |
Siddesh | d984084 | 2021-08-06 19:26:05 +0000 | [diff] [blame] | 553 | hostPair[ 0 ].buildICMP( vlan=VLAN ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 554 | hostPair[ 0 ].sendPacket( iface=srcIface ) |
| 555 | |
| 556 | waiting = not hostPair[ 1 ].checkFilter() |
| 557 | if not waiting: |
| 558 | pingResult = main.FALSE |
| 559 | packets = hostPair[ 1 ].readPackets() |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 560 | main.log.warn( repr( packets ) ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 561 | for packet in packets.splitlines(): |
| 562 | main.log.debug( packet ) |
| 563 | if srcIPs[0] in packet: |
| 564 | pingResult = main.TRUE |
| 565 | else: |
| 566 | main.log.warn( "Did not receive packets, killing filter" ) |
| 567 | kill = hostPair[ 1 ].killFilter() |
| 568 | main.log.debug( kill ) |
| 569 | hostPair[ 1 ].handle.sendline( "" ) |
| 570 | hostPair[ 1 ].handle.expect( hostPair[ 1 ].scapyPrompt ) |
| 571 | main.log.debug( hostPair[ 1 ].handle.before ) |
| 572 | # One of the host to host pair is unreachable |
| 573 | pingResult = main.FALSE |
| 574 | hostPair[ 0 ].stopScapy() |
| 575 | hostPair[ 1 ].stopScapy() |
| 576 | else: |
| 577 | pingResult = hostPair[ 0 ].ping( ipDst, interface=srcIface, wait=int( wait ) ) |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 578 | if pingResult: |
| 579 | pingResponse += hostPair[ 1 ].options[ 'shortName' ] |
| 580 | else: |
| 581 | pingResponse += "X" |
| 582 | # One of the host to host pair is unreachable |
| 583 | isReachable = main.FALSE |
| 584 | failedPings += 1 |
| 585 | pingResponse += "\n" |
| 586 | main.log.info( pingResponse + "Failed pings: " + str( failedPings ) ) |
| 587 | return isReachable |
| 588 | except Exception: |
| 589 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 590 | main.cleanAndExit() |
| 591 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 592 | def pingallHostsUnidirectional( self, srcList, dstList, ipv6=False, wait=1, acceptableFailed=0, useScapy=False ): |
| 593 | """ |
| 594 | Verify ping from each host in srcList to each host in dstList |
| 595 | |
| 596 | acceptableFailed: max number of acceptable failed pings |
| 597 | |
| 598 | Returns main.TRUE if all src hosts can reach all dst hosts |
| 599 | Returns main.FALSE if one or more of src hosts cannot reach one or more of dst hosts |
| 600 | """ |
| 601 | try: |
| 602 | main.log.info( "Verifying ping from each src host to each dst host" ) |
| 603 | |
| 604 | srcComponentList = [] |
| 605 | for hostName in srcList: |
| 606 | hostComponent = self.hosts[ hostName ] |
| 607 | if hostComponent: |
| 608 | main.log.debug( repr( hostComponent ) ) |
| 609 | srcComponentList.append( hostComponent ) |
| 610 | dstComponentList = [] |
| 611 | for hostName in dstList: |
| 612 | hostComponent = self.hosts[ hostName ] |
| 613 | if hostComponent: |
| 614 | main.log.debug( repr( hostComponent ) ) |
| 615 | dstComponentList.append( hostComponent ) |
| 616 | |
| 617 | isReachable = main.TRUE |
| 618 | wait = int( wait ) |
| 619 | cmd = " ping" + ("6" if ipv6 else "") + " -c 1 -i 1 -W " + str( wait ) + " " |
| 620 | pingResponse = "Ping output:\n" |
| 621 | failedPingsTotal = 0 |
| 622 | for srcHost in srcComponentList: |
| 623 | pingResponse += str( str( srcHost.shortName ) + " -> " ) |
| 624 | for dstHost in dstComponentList: |
| 625 | failedPings = 0 |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 626 | dstIP = dstHost.ip_address |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 627 | assert dstIP, "Not able to get IP address of host {}".format( dstHost ) |
| 628 | for iface in srcHost.interfaces: |
| 629 | # FIXME This only works if one iface name is configured |
| 630 | # NOTE: We can use an IP with -I instead of an interface name as well |
| 631 | name = iface.get( 'name' ) |
| 632 | if name: |
| 633 | cmd += " -I %s " % name |
| 634 | |
| 635 | if useScapy: |
| 636 | while failedPings <= acceptableFailed: |
| 637 | main.log.debug( "Pinging from " + str( srcHost.shortName ) + " to " + str( dstHost.shortName ) ) |
| 638 | # Use scapy to send and recieve packets |
| 639 | dstHost.startFilter() |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 640 | srcHost.buildEther( src=srcHost.interfaces[0].get( 'mac'), dst=dstHost.interfaces[0].get( 'mac') ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 641 | srcHost.sendPacket() |
| 642 | output = dstHost.checkFilter() |
| 643 | main.log.debug( output ) |
| 644 | if output: |
Jon Hall | 9b0de1f | 2020-08-24 15:38:04 -0700 | [diff] [blame] | 645 | # TODO: parse output? |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 646 | packets = dstHost.readPackets() |
| 647 | for packet in packets.splitlines(): |
| 648 | main.log.debug( packet ) |
| 649 | pingResponse += " " + str( dstHost.shortName ) |
| 650 | break |
| 651 | else: |
| 652 | kill = dstHost.killFilter() |
| 653 | main.log.debug( kill ) |
| 654 | dstHost.handle.sendline( "" ) |
| 655 | dstHost.handle.expect( dstHost.scapyPrompt ) |
| 656 | main.log.debug( dstHost.handle.before ) |
| 657 | failedPings += 1 |
| 658 | time.sleep(1) |
| 659 | if failedPings > acceptableFailed: |
| 660 | # One of the host to host pair is unreachable |
| 661 | pingResponse += " X" |
| 662 | isReachable = main.FALSE |
| 663 | failedPingsTotal += 1 |
| 664 | |
| 665 | else: |
| 666 | pingCmd = cmd + str( dstIP ) |
| 667 | while failedPings <= acceptableFailed: |
| 668 | main.log.debug( "Pinging from " + str( srcHost.shortName ) + " to " + str( dstHost.shortName ) ) |
| 669 | self.handle.sendline( pingCmd ) |
| 670 | self.handle.expect( self.prompt, timeout=wait + 5 ) |
| 671 | response = self.handle.before |
| 672 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 673 | pingResponse += " " + str( dstHost.shortName ) |
| 674 | break |
| 675 | else: |
| 676 | failedPings += 1 |
| 677 | time.sleep(1) |
| 678 | if failedPings > acceptableFailed: |
| 679 | # One of the host to host pair is unreachable |
| 680 | pingResponse += " X" |
| 681 | isReachable = main.FALSE |
| 682 | failedPingsTotal += 1 |
| 683 | pingResponse += "\n" |
| 684 | main.log.info( pingResponse + "Failed pings: " + str( failedPingsTotal ) ) |
| 685 | return isReachable |
| 686 | except AssertionError: |
| 687 | main.log.exception( "" ) |
| 688 | return main.FALSE |
| 689 | except pexpect.TIMEOUT: |
| 690 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 691 | response = self.handle.before |
| 692 | # NOTE: Send ctrl-c to make sure command is stopped |
| 693 | self.exitFromCmd( [ "Interrupt", self.prompt ] ) |
| 694 | response += self.handle.before + self.handle.after |
| 695 | self.handle.sendline( "" ) |
| 696 | self.handle.expect( self.prompt ) |
| 697 | response += self.handle.before + self.handle.after |
| 698 | main.log.debug( response ) |
| 699 | return main.FALSE |
| 700 | except pexpect.EOF: |
| 701 | main.log.error( self.name + ": EOF exception found" ) |
| 702 | main.log.error( self.name + ": " + self.handle.before ) |
| 703 | main.cleanAndExit() |
| 704 | except Exception: |
| 705 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 706 | main.cleanAndExit() |
| 707 | |
You Wang | 84f981d | 2018-01-12 16:11:50 -0800 | [diff] [blame] | 708 | def iperftcp( self, host1="h1", host2="h2", timeout=6 ): |
| 709 | ''' |
| 710 | Creates an iperf TCP test between two hosts. Returns main.TRUE if test results |
| 711 | are valid. |
| 712 | Optional: |
| 713 | timeout: The defualt timeout is 6 sec to allow enough time for a successful test to complete, |
| 714 | and short enough to stop an unsuccessful test from quiting and cleaning up mininet. |
| 715 | ''' |
| 716 | main.log.info( self.name + ": Simple iperf TCP test between two hosts" ) |
| 717 | # TODO: complete this function |
| 718 | return main.TRUE |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 719 | |
| 720 | def update( self ): |
| 721 | return main.TRUE |
| 722 | |
| 723 | def verifyHostIp( self, hostList=[], prefix="", update=False ): |
| 724 | """ |
| 725 | Description: |
| 726 | Verify that all hosts have IP address assigned to them |
| 727 | Optional: |
| 728 | hostList: If specified, verifications only happen to the hosts |
| 729 | in hostList |
| 730 | prefix: at least one of the ip address assigned to the host |
| 731 | needs to have the specified prefix |
| 732 | Returns: |
| 733 | main.TRUE if all hosts have specific IP address assigned; |
| 734 | main.FALSE otherwise |
| 735 | """ |
| 736 | try: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 737 | if not hostList: |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 738 | hostList = self.hosts.keys() |
| 739 | for hostName, hostComponent in self.hosts.items(): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 740 | if hostName not in hostList: |
| 741 | continue |
| 742 | ipList = [] |
| 743 | ipa = hostComponent.ip() |
| 744 | ipv4Pattern = r'inet ((?:[0-9]{1,3}\.){3}[0-9]{1,3})/' |
| 745 | ipList += re.findall( ipv4Pattern, ipa ) |
| 746 | # It's tricky to make regex for IPv6 addresses and this one is simplified |
| 747 | ipv6Pattern = r'inet6 ((?:[0-9a-fA-F]{1,4})?(?:[:0-9a-fA-F]{1,4}){1,7}(?:::)?(?:[:0-9a-fA-F]{1,4}){1,7})/' |
| 748 | ipList += re.findall( ipv6Pattern, ipa ) |
| 749 | main.log.debug( self.name + ": IP list on host " + str( hostName ) + ": " + str( ipList ) ) |
| 750 | if not ipList: |
| 751 | main.log.warn( self.name + ": Failed to discover any IP addresses on host " + str( hostName ) ) |
| 752 | else: |
| 753 | if not any( ip.startswith( str( prefix ) ) for ip in ipList ): |
| 754 | main.log.warn( self.name + ": None of the IPs on host " + str( hostName ) + " has prefix " + str( prefix ) ) |
| 755 | else: |
| 756 | main.log.debug( self.name + ": Found matching IP on host " + str( hostName ) ) |
| 757 | hostList.remove( hostName ) |
| 758 | return main.FALSE if hostList else main.TRUE |
| 759 | except KeyError: |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 760 | main.log.exception( self.name + ": host data not as expected: " + self.hosts.keys() ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 761 | return None |
| 762 | except pexpect.EOF: |
| 763 | main.log.error( self.name + ": EOF exception found" ) |
| 764 | main.log.error( self.name + ": " + self.handle.before ) |
| 765 | main.cleanAndExit() |
| 766 | except Exception: |
| 767 | main.log.exception( self.name + ": Uncaught exception" ) |
| 768 | return None |
| 769 | |
| 770 | def addRoute( self, host, dstIP, interface, ipv6=False ): |
| 771 | """ |
| 772 | Add a route to host |
| 773 | Ex: h1 route add -host 224.2.0.1 h1-eth0 |
| 774 | """ |
| 775 | try: |
| 776 | if ipv6: |
| 777 | cmd = "sudo route -A inet6 add " |
| 778 | else: |
| 779 | cmd = "sudo route add -host " |
| 780 | cmd += str( dstIP ) + " " + str( interface ) |
| 781 | response = self.runCmdOnHost( host, cmd ) |
| 782 | main.log.debug( "response = " + response ) |
| 783 | return main.TRUE |
| 784 | except pexpect.TIMEOUT: |
| 785 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 786 | main.log.error( self.name + ": " + self.handle.before ) |
| 787 | main.cleanAndExit() |
| 788 | except pexpect.EOF: |
| 789 | main.log.error( self.name + ": EOF exception found" ) |
| 790 | main.log.error( self.name + ": " + self.handle.before ) |
| 791 | return main.FALSE |
| 792 | except Exception: |
| 793 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 794 | main.cleanAndExit() |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 795 | |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 796 | def getIPAddress( self, host, iface=None, proto='IPV4' ): |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 797 | """ |
| 798 | Returns IP address of the host |
| 799 | """ |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 800 | hostComponent = self.hosts[ host ] |
| 801 | if hostComponent: |
| 802 | return hostComponent.getIPAddress( iface=iface, proto=proto ) |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 803 | else: |
Jon Hall | 32c90f3 | 2021-06-24 16:32:44 -0700 | [diff] [blame] | 804 | main.log.warn( self.name + ": Could not find host with short name '%s'" % host ) |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 805 | return None |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 806 | |
| 807 | def getLinkRandom( self, timeout=60, nonCut=True, excludeNodes=[], skipLinks=[] ): |
| 808 | """ |
| 809 | Randomly get a link from network topology. |
| 810 | If nonCut is True, it gets a list of non-cut links (the deletion |
| 811 | of a non-cut link will not increase the number of connected |
| 812 | component of a graph) and randomly returns one of them, otherwise |
| 813 | it just randomly returns one link from all current links. |
| 814 | excludeNodes will be passed to getLinks and getGraphDict method. |
| 815 | Any link that has either end included in skipLinks will be excluded. |
| 816 | Returns the link as a list, e.g. [ 's1', 's2' ]. |
| 817 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 818 | candidateLinks = [] |
| 819 | try: |
| 820 | if not nonCut: |
| 821 | links = self.getLinks( timeout=timeout, excludeNodes=excludeNodes ) |
| 822 | assert len( links ) != 0 |
| 823 | for link in links: |
| 824 | # Exclude host-switch link |
| 825 | if link[ 'node1' ].startswith( 'h' ) or link[ 'node2' ].startswith( 'h' ): |
| 826 | continue |
| 827 | candidateLinks.append( [ link[ 'node1' ], link[ 'node2' ] ] ) |
| 828 | else: |
| 829 | graphDict = self.getGraphDict( timeout=timeout, useId=False, |
| 830 | excludeNodes=excludeNodes ) |
| 831 | if graphDict is None: |
| 832 | return None |
| 833 | self.graph.update( graphDict ) |
| 834 | candidateLinks = self.graph.getNonCutEdges() |
| 835 | candidateLinks = [ link for link in candidateLinks |
| 836 | if link[0] not in skipLinks and link[1] not in skipLinks ] |
| 837 | if candidateLinks is None: |
| 838 | return None |
| 839 | elif len( candidateLinks ) == 0: |
| 840 | main.log.info( self.name + ": No candidate link for deletion" ) |
| 841 | return None |
| 842 | else: |
| 843 | link = random.sample( candidateLinks, 1 ) |
| 844 | return link[ 0 ] |
| 845 | except KeyError: |
| 846 | main.log.exception( self.name + ": KeyError exception found" ) |
| 847 | return None |
| 848 | except AssertionError: |
| 849 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 850 | return None |
| 851 | except Exception: |
| 852 | main.log.exception( self.name + ": Uncaught exception" ) |
| 853 | return None |
| 854 | |
| 855 | def getSwitchRandom( self, timeout=60, nonCut=True, excludeNodes=[], skipSwitches=[] ): |
| 856 | """ |
| 857 | Randomly get a switch from network topology. |
| 858 | If nonCut is True, it gets a list of non-cut switches (the deletion |
| 859 | of a non-cut switch will not increase the number of connected |
| 860 | components of a graph) and randomly returns one of them, otherwise |
| 861 | it just randomly returns one switch from all current switches in |
| 862 | Mininet. |
| 863 | excludeNodes will be pased to getSwitches and getGraphDict method. |
| 864 | Switches specified in skipSwitches will be excluded. |
| 865 | Returns the name of the chosen switch. |
| 866 | """ |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 867 | candidateSwitches = [] |
| 868 | try: |
| 869 | if not nonCut: |
| 870 | switches = self.getSwitches( timeout=timeout, excludeNodes=excludeNodes ) |
| 871 | assert len( switches ) != 0 |
| 872 | for switchName in switches.keys(): |
| 873 | candidateSwitches.append( switchName ) |
| 874 | else: |
| 875 | graphDict = self.getGraphDict( timeout=timeout, useId=False, |
| 876 | excludeNodes=excludeNodes ) |
| 877 | if graphDict is None: |
| 878 | return None |
| 879 | self.graph.update( graphDict ) |
| 880 | candidateSwitches = self.graph.getNonCutVertices() |
| 881 | candidateSwitches = [ switch for switch in candidateSwitches if switch not in skipSwitches ] |
| 882 | if candidateSwitches is None: |
| 883 | return None |
| 884 | elif len( candidateSwitches ) == 0: |
| 885 | main.log.info( self.name + ": No candidate switch for deletion" ) |
| 886 | return None |
| 887 | else: |
| 888 | switch = random.sample( candidateSwitches, 1 ) |
| 889 | return switch[ 0 ] |
| 890 | except KeyError: |
| 891 | main.log.exception( self.name + ": KeyError exception found" ) |
| 892 | return None |
| 893 | except AssertionError: |
| 894 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 895 | return None |
| 896 | except Exception: |
| 897 | main.log.exception( self.name + ": Uncaught exception" ) |
| 898 | return None |
| 899 | |
| 900 | def getGraphDict( self, timeout=60, useId=True, includeHost=False, |
| 901 | excludeNodes=[] ): |
| 902 | """ |
| 903 | Return a dictionary which describes the latest network topology data as a |
| 904 | graph. |
| 905 | An example of the dictionary: |
| 906 | { vertex1: { 'edges': ..., 'name': ..., 'protocol': ... }, |
| 907 | vertex2: { 'edges': ..., 'name': ..., 'protocol': ... } } |
| 908 | Each vertex should at least have an 'edges' attribute which describes the |
| 909 | adjacency information. The value of 'edges' attribute is also represented by |
| 910 | a dictionary, which maps each edge (identified by the neighbor vertex) to a |
| 911 | list of attributes. |
| 912 | An example of the edges dictionary: |
| 913 | 'edges': { vertex2: { 'port': ..., 'weight': ... }, |
| 914 | vertex3: { 'port': ..., 'weight': ... } } |
| 915 | If useId == True, dpid/mac will be used instead of names to identify |
| 916 | vertices, which is helpful when e.g. comparing network topology with ONOS |
| 917 | topology. |
| 918 | If includeHost == True, all hosts (and host-switch links) will be included |
| 919 | in topology data. |
| 920 | excludeNodes will be passed to getSwitches and getLinks methods to exclude |
| 921 | unexpected switches and links. |
| 922 | """ |
| 923 | # TODO: support excludeNodes |
| 924 | graphDict = {} |
| 925 | try: |
| 926 | links = self.getLinks( timeout=timeout, excludeNodes=excludeNodes ) |
| 927 | portDict = {} |
| 928 | switches = self.getSwitches( excludeNodes=excludeNodes ) |
| 929 | if includeHost: |
| 930 | hosts = self.getHosts() |
| 931 | for link in links: |
| 932 | # TODO: support 'includeHost' argument |
| 933 | if link[ 'node1' ].startswith( 'h' ) or link[ 'node2' ].startswith( 'h' ): |
| 934 | continue |
| 935 | nodeName1 = link[ 'node1' ] |
| 936 | nodeName2 = link[ 'node2' ] |
| 937 | if not self.switches[ nodeName1 ].isup or not self.switches[ nodeName2 ].isup: |
| 938 | continue |
| 939 | port1 = link[ 'port1' ] |
| 940 | port2 = link[ 'port2' ] |
| 941 | # Loop for two nodes |
| 942 | for i in range( 2 ): |
| 943 | portIndex = port1 |
| 944 | if useId: |
| 945 | node1 = 'of:' + str( switches[ nodeName1 ][ 'dpid' ] ) |
| 946 | node2 = 'of:' + str( switches[ nodeName2 ][ 'dpid' ] ) |
| 947 | else: |
| 948 | node1 = nodeName1 |
| 949 | node2 = nodeName2 |
| 950 | if node1 not in graphDict.keys(): |
| 951 | if useId: |
| 952 | graphDict[ node1 ] = { 'edges': {}, |
| 953 | 'dpid': switches[ nodeName1 ][ 'dpid' ], |
| 954 | 'name': nodeName1, |
| 955 | 'ports': switches[ nodeName1 ][ 'ports' ], |
| 956 | 'swClass': switches[ nodeName1 ][ 'swClass' ], |
| 957 | 'pid': switches[ nodeName1 ][ 'pid' ], |
| 958 | 'options': switches[ nodeName1 ][ 'options' ] } |
| 959 | else: |
| 960 | graphDict[ node1 ] = { 'edges': {} } |
| 961 | else: |
| 962 | # Assert node2 is not connected to any current links of node1 |
| 963 | # assert node2 not in graphDict[ node1 ][ 'edges' ].keys() |
| 964 | pass |
| 965 | for port in switches[ nodeName1 ][ 'ports' ]: |
| 966 | if port[ 'of_port' ] == str( portIndex ): |
| 967 | # Use -1 as index for disabled port |
| 968 | if port[ 'enabled' ]: |
| 969 | graphDict[ node1 ][ 'edges' ][ node2 ] = { 'port': portIndex } |
| 970 | else: |
| 971 | graphDict[ node1 ][ 'edges' ][ node2 ] = { 'port': -1 } |
| 972 | # Swap two nodes/ports |
| 973 | nodeName1, nodeName2 = nodeName2, nodeName1 |
| 974 | port1, port2 = port2, port1 |
| 975 | # Remove links with disabled ports |
| 976 | linksToRemove = [] |
| 977 | for node, edges in graphDict.items(): |
| 978 | for neighbor, port in edges[ 'edges' ].items(): |
| 979 | if port[ 'port' ] == -1: |
| 980 | linksToRemove.append( ( node, neighbor ) ) |
| 981 | for node1, node2 in linksToRemove: |
| 982 | for i in range( 2 ): |
| 983 | if graphDict.get( node1 )[ 'edges' ].get( node2 ): |
| 984 | graphDict[ node1 ][ 'edges' ].pop( node2 ) |
| 985 | node1, node2 = node2, node1 |
| 986 | return graphDict |
| 987 | except KeyError: |
| 988 | main.log.exception( self.name + ": KeyError exception found" ) |
| 989 | return None |
| 990 | except AssertionError: |
| 991 | main.log.exception( self.name + ": AssertionError exception found" ) |
| 992 | return None |
| 993 | except pexpect.EOF: |
| 994 | main.log.error( self.name + ": EOF exception found" ) |
| 995 | main.log.error( self.name + ": " + self.handle.before ) |
| 996 | main.cleanAndExit() |
| 997 | except Exception: |
| 998 | main.log.exception( self.name + ": Uncaught exception" ) |
| 999 | return None |
| 1000 | |
| 1001 | def switch( self, **switchargs ): |
| 1002 | """ |
| 1003 | start/stop a switch |
| 1004 | """ |
| 1005 | args = utilities.parse_args( [ "SW", "OPTION" ], **switchargs ) |
| 1006 | sw = args[ "SW" ] if args[ "SW" ] is not None else "" |
| 1007 | option = args[ "OPTION" ] if args[ "OPTION" ] is not None else "" |
| 1008 | try: |
| 1009 | switchComponent = self.switches[ sw ] |
| 1010 | if option == 'stop': |
| 1011 | switchComponent.stopOfAgent() |
| 1012 | elif option == 'start': |
| 1013 | switchComponent.startOfAgent() |
| 1014 | else: |
| 1015 | main.log.warn( self.name + ": Unknown switch command" ) |
| 1016 | return main.FALSE |
| 1017 | return main.TRUE |
| 1018 | except KeyError: |
| 1019 | main.log.error( self.name + ": Not able to find switch [}".format( sw ) ) |
| 1020 | except pexpect.TIMEOUT: |
| 1021 | main.log.error( self.name + ": TIMEOUT exception found" ) |
| 1022 | main.log.error( self.name + ": " + self.handle.before ) |
| 1023 | return None |
| 1024 | except pexpect.EOF: |
| 1025 | main.log.error( self.name + ": EOF exception found" ) |
| 1026 | main.log.error( self.name + ": " + self.handle.before ) |
| 1027 | main.cleanAndExit() |
| 1028 | except Exception: |
| 1029 | main.log.exception( self.name + ": Uncaught exception" ) |
| 1030 | main.cleanAndExit() |
| 1031 | |
| 1032 | def discoverHosts( self, hostList=[], wait=1000, dstIp="6.6.6.6", dstIp6="1020::3fe" ): |
| 1033 | ''' |
| 1034 | Hosts in hostList will do a single ARP/ND to a non-existent address for ONOS to |
| 1035 | discover them. A host will use arping/ndisc6 to send ARP/ND depending on if it |
| 1036 | has IPv4/IPv6 addresses configured. |
| 1037 | Optional: |
| 1038 | hostList: a list of names of the hosts that need to be discovered. If not |
| 1039 | specified mininet will send ping from all the hosts |
| 1040 | wait: timeout for ARP/ND in milliseconds |
| 1041 | dstIp: destination address used by IPv4 hosts |
| 1042 | dstIp6: destination address used by IPv6 hosts |
| 1043 | Returns: |
| 1044 | main.TRUE if all packets were successfully sent. Otherwise main.FALSE |
| 1045 | ''' |
| 1046 | try: |
| 1047 | hosts = self.getHosts() |
| 1048 | if not hostList: |
| 1049 | hostList = hosts.keys() |
| 1050 | discoveryResult = main.TRUE |
| 1051 | for host in hostList: |
| 1052 | flushCmd = "" |
| 1053 | cmd = "" |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 1054 | intf = hosts[host]['interfaces'][0].get( 'name' ) |
| 1055 | hostIp = self.getIPAddress( host, iface=intf ) or hosts[host]['interfaces'][0].get( 'ips', False ) |
| 1056 | if hostIp: |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 1057 | flushCmd = "sudo ip neigh flush all" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 1058 | intfStr = "-i {}".format( intf ) if intf else "" |
Jon Hall | 627b157 | 2020-12-01 12:01:15 -0800 | [diff] [blame] | 1059 | srcIp = "-S {}".format( hostIp if isinstance( hostIp, types.StringType ) else hostIp[0] ) if intf else "" |
| 1060 | cmd = "sudo arping -c 1 -w {} {} {} {}".format( wait, intfStr, srcIp, dstIp ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 1061 | main.log.debug( "Sending IPv4 arping from host {}".format( host ) ) |
| 1062 | elif self.getIPAddress( host, proto='IPV6' ): |
| 1063 | flushCmd = "sudo ip -6 neigh flush all" |
| 1064 | intf = hosts[host]['interfaces'][0]['name'] |
| 1065 | cmd = "ndisc6 -r 1 -w {} {} {}".format( wait, dstIp6, intf ) |
| 1066 | main.log.debug( "Sending IPv6 ND from host {}".format( host ) ) |
| 1067 | else: |
| 1068 | main.log.warn( "No IP addresses configured on host {}, skipping discovery".format( host ) ) |
| 1069 | discoveryResult = main.FALSE |
| 1070 | if cmd: |
| 1071 | self.runCmdOnHost( host, flushCmd ) |
| 1072 | self.runCmdOnHost( host, cmd ) |
| 1073 | return discoveryResult |
| 1074 | except Exception: |
| 1075 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 1076 | main.cleanAndExit() |