Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 2 | Copyright 2016 Open Networking Foundation ( ONF ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 11 | ( at your option ) any later version. |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 20 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 21 | import time |
| 22 | import re |
| 23 | import imp |
| 24 | import json |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 25 | from core import utilities |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 26 | |
| 27 | |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 28 | class Topology: |
| 29 | |
| 30 | def __init__( self ): |
| 31 | self.default = '' |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 32 | |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 33 | """ |
| 34 | These functions can be used for topology comparisons |
| 35 | """ |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 36 | def getAll( self, function, needRetry=False, kwargs={}, inJson=False ): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 37 | """ |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 38 | Description: |
| 39 | get all devices/links/hosts/ports of the onosCli |
| 40 | Required: |
| 41 | * function - name of the function |
| 42 | * needRetry - it will retry if this is true. |
| 43 | * kwargs - kwargs of the function |
| 44 | * inJson - True if want it in Json form |
| 45 | Returns: |
| 46 | Returns the list of the result. |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 47 | """ |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 48 | returnList = [] |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 49 | threads = [] |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 50 | for ctrl in main.Cluster.active(): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 51 | func = getattr( ctrl.CLI, function ) |
| 52 | t = main.Thread( target=utilities.retry if needRetry else func, |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 53 | name=function + "-" + str( ctrl ), |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 54 | args=[ func, [ None ] ] if needRetry else [], |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 55 | kwargs=kwargs ) |
| 56 | threads.append( t ) |
| 57 | t.start() |
| 58 | |
| 59 | for t in threads: |
| 60 | t.join() |
| 61 | if inJson: |
| 62 | try: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 63 | returnList.append( json.loads( t.result ) ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 64 | except ( ValueError, TypeError ): |
| 65 | main.log.exception( "Error parsing hosts results" ) |
| 66 | main.log.error( repr( t.result ) ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 67 | returnList.append( None ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 68 | else: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 69 | returnList.append( t.result ) |
| 70 | return returnList |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 71 | |
| 72 | def compareDevicePort( self, Mininet, controller, mnSwitches, devices, ports ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 73 | """ |
| 74 | Description: |
| 75 | compares the devices and port of the onos to the mininet. |
| 76 | Required: |
| 77 | * Mininet - mininet driver to use |
| 78 | * controller - controller position of the devices |
| 79 | * mnSwitches - switches of mininet |
| 80 | * devices - devices of the onos |
| 81 | * ports - ports of the onos |
| 82 | Returns: |
| 83 | Returns main.TRUE if the results are matching else |
| 84 | Returns main.FALSE |
| 85 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 86 | if devices[ controller ] and ports[ controller ] and \ |
| 87 | "Error" not in devices[ controller ] and \ |
| 88 | "Error" not in ports[ controller ]: |
| 89 | try: |
| 90 | currentDevicesResult = Mininet.compareSwitches( |
| 91 | mnSwitches, |
| 92 | json.loads( devices[ controller ] ), |
| 93 | json.loads( ports[ controller ] ) ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 94 | except ( TypeError, ValueError ): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 95 | main.log.error( |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 96 | "Could not load json: {0} or {1}".format( str( devices[ controller ] ), |
| 97 | str( ports[ controller ] ) ) ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 98 | currentDevicesResult = main.FALSE |
| 99 | else: |
| 100 | currentDevicesResult = main.FALSE |
| 101 | return currentDevicesResult |
| 102 | |
| 103 | def compareBase( self, compareElem, controller, compareF, compareArg ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 104 | """ |
| 105 | Description: |
| 106 | compares the links/hosts of the onos to the mininet. |
| 107 | Required: |
| 108 | * compareElem - list of links/hosts of the onos |
| 109 | * controller - controller position of the devices |
| 110 | * compareF - function of the mininet that will compare the |
| 111 | results |
| 112 | * compareArg - arg of the compareF. |
| 113 | Returns: |
| 114 | Returns main.TRUE if the results are matching else |
| 115 | Returns main.FALSE |
| 116 | """ |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 117 | if compareElem[ controller ] and "Error" not in compareElem[ controller ]: |
| 118 | try: |
| 119 | if isinstance( compareArg, list ): |
| 120 | compareArg.append( json.loads( compareElem[ controller ] ) ) |
| 121 | else: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 122 | compareArg = [ compareArg, json.loads( compareElem[ controller ] ) ] |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 123 | |
| 124 | currentCompareResult = compareF( *compareArg ) |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 125 | except( TypeError, ValueError ): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 126 | main.log.error( |
| 127 | "Could not load json: {0} or {1}".format( str( compareElem[ controller ] ) ) ) |
| 128 | currentCompareResult = main.FALSE |
| 129 | else: |
| 130 | currentCompareResult = main.FALSE |
| 131 | |
| 132 | return currentCompareResult |
| 133 | |
Devin Lim | 0c972b7 | 2018-02-08 14:53:59 -0800 | [diff] [blame] | 134 | def compareTopos( self, Mininet, attempts=1, includeCaseDesc=True ): |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 135 | """ |
| 136 | Description: |
| 137 | compares the links and hosts and switches of the onos to the mininet. |
| 138 | Required: |
| 139 | * Mininet - Mininet driver to use. |
| 140 | * attempts - number of attempts to compare in case |
| 141 | the result is different after a certain time. |
| 142 | Returns: |
| 143 | Returns main.TRUE if the results are matching else |
| 144 | Returns main.FALSE |
| 145 | """ |
Devin Lim | 0c972b7 | 2018-02-08 14:53:59 -0800 | [diff] [blame] | 146 | if includeCaseDesc: |
| 147 | main.case( "Compare ONOS Topology view to Mininet topology" ) |
| 148 | main.caseExplanation = "Compare topology elements between Mininet" +\ |
| 149 | " and ONOS" |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 150 | main.log.info( "Gathering topology information from Mininet" ) |
| 151 | devicesResults = main.FALSE # Overall Boolean for device correctness |
| 152 | linksResults = main.FALSE # Overall Boolean for link correctness |
| 153 | hostsResults = main.FALSE # Overall Boolean for host correctness |
| 154 | deviceFails = [] # Nodes where devices are incorrect |
| 155 | linkFails = [] # Nodes where links are incorrect |
| 156 | hostFails = [] # Nodes where hosts are incorrect |
| 157 | |
| 158 | mnSwitches = Mininet.getSwitches() |
| 159 | mnLinks = Mininet.getLinks() |
| 160 | mnHosts = Mininet.getHosts() |
| 161 | |
| 162 | main.step( "Comparing Mininet topology to ONOS topology" ) |
| 163 | |
| 164 | while ( attempts >= 0 ) and\ |
| 165 | ( not devicesResults or not linksResults or not hostsResults ): |
| 166 | main.log.info( "Sleeping {} seconds".format( 2 ) ) |
| 167 | time.sleep( 2 ) |
| 168 | if not devicesResults: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 169 | devices = self.getAll( "devices", False ) |
| 170 | ports = self.getAll( "ports", False ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 171 | devicesResults = main.TRUE |
| 172 | deviceFails = [] # Reset for each failed attempt |
| 173 | if not linksResults: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 174 | links = self.getAll( "links", False ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 175 | linksResults = main.TRUE |
| 176 | linkFails = [] # Reset for each failed attempt |
| 177 | if not hostsResults: |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 178 | hosts = self.getAll( "hosts", False ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 179 | hostsResults = main.TRUE |
| 180 | hostFails = [] # Reset for each failed attempt |
| 181 | |
| 182 | # Check for matching topology on each node |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 183 | for controller in main.Cluster.getRunningPos(): |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 184 | controllerStr = str( controller + 1 ) # ONOS node number |
| 185 | # Compare Devices |
| 186 | currentDevicesResult = self.compareDevicePort( Mininet, controller, |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 187 | mnSwitches, |
| 188 | devices, ports ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 189 | if not currentDevicesResult: |
| 190 | deviceFails.append( controllerStr ) |
| 191 | devicesResults = devicesResults and currentDevicesResult |
| 192 | # Compare Links |
| 193 | currentLinksResult = self.compareBase( links, controller, |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 194 | Mininet.compareLinks, |
| 195 | [ mnSwitches, mnLinks ] ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 196 | if not currentLinksResult: |
| 197 | linkFails.append( controllerStr ) |
| 198 | linksResults = linksResults and currentLinksResult |
| 199 | # Compare Hosts |
| 200 | currentHostsResult = self.compareBase( hosts, controller, |
| 201 | Mininet.compareHosts, |
| 202 | mnHosts ) |
| 203 | if not currentHostsResult: |
| 204 | hostFails.append( controllerStr ) |
| 205 | hostsResults = hostsResults and currentHostsResult |
| 206 | # Decrement Attempts Remaining |
| 207 | attempts -= 1 |
| 208 | |
| 209 | utilities.assert_equals( expect=[], |
| 210 | actual=deviceFails, |
| 211 | onpass="ONOS correctly discovered all devices", |
| 212 | onfail="ONOS incorrectly discovered devices on nodes: " + |
| 213 | str( deviceFails ) ) |
| 214 | utilities.assert_equals( expect=[], |
| 215 | actual=linkFails, |
| 216 | onpass="ONOS correctly discovered all links", |
| 217 | onfail="ONOS incorrectly discovered links on nodes: " + |
| 218 | str( linkFails ) ) |
| 219 | utilities.assert_equals( expect=[], |
| 220 | actual=hostFails, |
| 221 | onpass="ONOS correctly discovered all hosts", |
| 222 | onfail="ONOS incorrectly discovered hosts on nodes: " + |
| 223 | str( hostFails ) ) |
| 224 | topoResults = hostsResults and linksResults and devicesResults |
| 225 | utilities.assert_equals( expect=main.TRUE, |
| 226 | actual=topoResults, |
| 227 | onpass="ONOS correctly discovered the topology", |
| 228 | onfail="ONOS incorrectly discovered the topology" ) |
Devin Lim | 142b534 | 2017-07-20 15:22:39 -0700 | [diff] [blame] | 229 | return topoResults |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 230 | |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 231 | def ping( self, srcList, dstList, ipv6=False, expect=True, wait=1, acceptableFailed=0, collectT3=True, t3Simple=False ): |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 232 | """ |
| 233 | Description: |
| 234 | Ping from every host in srcList to every host in dstList and |
| 235 | verify if ping results are as expected. |
| 236 | Pings are executed in parallel from host components |
| 237 | Options: |
| 238 | src: a list of source host names, e.g. [ "h1", "h2" ] |
| 239 | dst: a list of destination host names, e.g. [ "h3", "h4" ] |
| 240 | expect: expect ping result to pass if True, otherwise fail |
| 241 | acceptableFailed: maximum number of failed pings acceptable for |
| 242 | each src-dst host pair |
| 243 | collectT3: save t3-troubleshoot output for src and dst host that failed to ping |
You Wang | 0fa76e7 | 2018-05-18 11:33:25 -0700 | [diff] [blame] | 244 | t3Simple: use t3-troubleshoot-simple command when collecting t3 output |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 245 | Returns: |
| 246 | main.TRUE if all ping results are expected, otherwise main.FALSE |
| 247 | """ |
| 248 | main.log.info( "Pinging from {} to {}, expected result is {}".format( srcList, dstList, |
| 249 | "pass" if expect else "fail" ) ) |
| 250 | # Verify host component has been created |
| 251 | srcIpList = {} |
| 252 | for src in srcList: |
| 253 | if not hasattr( main, src ): |
| 254 | main.log.info( "Creating component for host {}".format( src ) ) |
| 255 | main.Network.createHostComponent( src ) |
| 256 | hostHandle = getattr( main, src ) |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 257 | if hasattr( main, 'Mininet1' ): |
| 258 | main.log.info( "Starting CLI on host {}".format( src ) ) |
| 259 | hostHandle.startHostCli() |
| 260 | else: |
| 261 | hostHandle.connectInband() |
Siddesh | a2938fe | 2021-04-06 02:46:06 +0000 | [diff] [blame] | 262 | hostHandle = getattr( main, src ) |
| 263 | srcIpList[ src ] = main.Network.getIPAddress( src, proto='IPV6' if ipv6 else 'IPV4', iface=hostHandle.interfaces[0].get("name") ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 264 | unexpectedPings = [] |
| 265 | for dst in dstList: |
Siddesh | a2938fe | 2021-04-06 02:46:06 +0000 | [diff] [blame] | 266 | hostHandle = getattr( main, dst ) |
| 267 | dstIp = main.Network.getIPAddress( dst, proto='IPV6' if ipv6 else 'IPV4', iface=hostHandle.interfaces[0].get("name") ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 268 | # Start pings from src hosts in parallel |
| 269 | pool = [] |
| 270 | for src in srcList: |
| 271 | srcIp = srcIpList[ src ] |
You Wang | be98fb1 | 2018-05-24 16:15:17 -0700 | [diff] [blame] | 272 | if not srcIp or not dstIp: |
| 273 | if expect: |
| 274 | unexpectedPings.append( [ src, dst, "no IP" ] ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 275 | continue |
You Wang | be98fb1 | 2018-05-24 16:15:17 -0700 | [diff] [blame] | 276 | if srcIp == dstIp: |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 277 | continue |
| 278 | hostHandle = getattr( main, src ) |
| 279 | thread = main.Thread( target=utilities.retry, |
| 280 | name="{}-{}".format( srcIp, dstIp ), |
| 281 | args=[ hostHandle.pingHostSetAlternative, [ main.FALSE ] ], |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 282 | kwargs={ "args": ( [ dstIp ], wait, ipv6 ), |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 283 | "attempts": acceptableFailed + 1, |
| 284 | "sleep": 1 } ) |
| 285 | pool.append( thread ) |
| 286 | thread.start() |
| 287 | # Wait for threads to finish and check ping result |
| 288 | for thread in pool: |
| 289 | thread.join( 10 ) |
| 290 | srcIp, dstIp = thread.name.split( "-" ) |
| 291 | if expect and not thread.result or not expect and thread.result: |
| 292 | unexpectedPings.append( [ srcIp, dstIp, "fail" if expect else "pass" ] ) |
You Wang | 8574776 | 2018-05-11 15:51:50 -0700 | [diff] [blame] | 293 | main.log.info( "Unexpected pings: {}".format( unexpectedPings ) ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 294 | if collectT3: |
| 295 | for unexpectedPing in unexpectedPings: |
| 296 | if unexpectedPing[ 2 ] == "no IP": |
| 297 | continue |
| 298 | srcIp = unexpectedPing[ 0 ] |
| 299 | dstIp = unexpectedPing[ 1 ] |
| 300 | main.log.debug( "Collecting t3 with source {} and destination {}".format( srcIp, dstIp ) ) |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 301 | cmdList = main.Cluster.active( 0 ).CLI.composeT3Command( srcIp, dstIp, ipv6, True, t3Simple ) |
| 302 | if not cmdList: |
| 303 | main.log.warn( "Failed to compose T3 command with source {} and destination {}".format( srcIp, dstIp ) ) |
| 304 | continue |
| 305 | for i in range( 0, len( cmdList ) ): |
| 306 | cmd = cmdList[ i ] |
| 307 | main.log.debug( "t3 command: {}".format( cmd ) ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 308 | main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, cmd, main.logdir, |
You Wang | 54b1d67 | 2018-06-11 16:44:13 -0700 | [diff] [blame] | 309 | "t3-CASE{}-{}-{}-route{}-".format( main.CurrentTestCaseNumber, srcIp, dstIp, i ), |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 310 | timeout=10, |
| 311 | cliPort=main.Cluster.active(0).CLI.karafPort ) |
You Wang | 5da39c8 | 2018-04-26 22:55:08 -0700 | [diff] [blame] | 312 | return main.FALSE if unexpectedPings else main.TRUE |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 313 | |
| 314 | def sendScapyPackets( self, sender, receiver, pktFilter, pkt, sIface=None, dIface=None, expect=True, acceptableFailed=0, collectT3=True, t3Command="" ): |
| 315 | """ |
| 316 | Description: |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 317 | Send Scapy packets from sender to receiver and verify if result is as expected and retry if neccessary |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 318 | If collectT3 is True and t3Command is specified, collect t3-troubleshoot output on unexpected scapy results |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 319 | Options: |
| 320 | sender: the component of the host that is sending packets |
| 321 | receiver: the component of the host that is receiving packets |
| 322 | pktFilter: packet filter used by receiver |
| 323 | pkt: keyword that is expected to be conrained in the received packets |
| 324 | expect: expect receiver to receive the packet if True, otherwise not receiving the packet |
| 325 | acceptableFailed: maximum number of unexpected scapy results acceptable |
| 326 | collectT3: save t3-troubleshoot output for unexpected scapy results |
| 327 | Returns: |
| 328 | main.TRUE if scapy result is expected, otherwise main.FALSE |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 329 | Note: It is assumed that Scapy is already started on the destination host |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 330 | """ |
| 331 | main.log.info( "Sending scapy packets from {} to {}, expected result is {}".format( sender.name, receiver.name, |
| 332 | "pass" if expect else "fail" ) ) |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 333 | scapyResult = utilities.retry( self.sendScapyPacketsHelper, |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 334 | main.FALSE, |
| 335 | args=( sender, receiver, pktFilter, pkt, |
| 336 | sIface, dIface, expect ), |
| 337 | attempts=acceptableFailed + 1, |
| 338 | sleep=1 ) |
| 339 | if not scapyResult and collectT3 and t3Command: |
| 340 | main.log.debug( "Collecting t3 with source {} and destination {}".format( sender.name, receiver.name ) ) |
| 341 | main.log.debug( "t3 command: {}".format( t3Command ) ) |
| 342 | main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, t3Command, main.logdir, |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 343 | "t3-CASE{}-{}-{}-".format( main.CurrentTestCaseNumber, sender.name, receiver.name ), |
| 344 | cliPort=main.Cluster.active(0).CLI.karafPort ) |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 345 | return scapyResult |
| 346 | |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 347 | def sendScapyPacketsHelper( self, sender, receiver, pktFilter, pkt, sIface=None, dIface=None, expect=True ): |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 348 | """ |
| 349 | Description: |
| 350 | Send Scapy packets from sender to receiver and verify if result is as expected |
| 351 | Options: |
| 352 | sender: the component of the host that is sending packets |
| 353 | receiver: the component of the host that is receiving packets |
| 354 | pktFilter: packet filter used by receiver |
| 355 | pkt: keyword that is expected to be conrained in the received packets |
| 356 | expect: expect receiver to receive the packet if True, otherwise not receiving the packet |
| 357 | Returns: |
| 358 | main.TRUE if scapy result is expected, otherwise main.FALSE |
| 359 | """ |
You Wang | 3a5f74c | 2018-08-03 14:58:15 -0700 | [diff] [blame] | 360 | started = receiver.startFilter( ifaceName=dIface, pktFilter=pktFilter ) |
| 361 | if not started: |
| 362 | main.log.error("Failed to start Scapy packet filter") |
| 363 | return main.FALSE |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 364 | sender.sendPacket( iface=sIface ) |
| 365 | finished = receiver.checkFilter() |
| 366 | packet = "" |
| 367 | if finished: |
You Wang | 548db38 | 2020-08-12 09:17:13 -0700 | [diff] [blame] | 368 | packets = receiver.readPackets( detailed=True ) |
You Wang | c564c6f | 2018-05-01 15:24:57 -0700 | [diff] [blame] | 369 | for packet in packets.splitlines(): |
| 370 | main.log.debug( packet ) |
| 371 | else: |
| 372 | kill = receiver.killFilter() |
| 373 | main.log.debug( kill ) |
| 374 | sender.handle.sendline( "" ) |
| 375 | sender.handle.expect( sender.scapyPrompt ) |
| 376 | main.log.debug( sender.handle.before ) |
| 377 | packetCaptured = True if pkt in packet else False |
| 378 | return main.TRUE if packetCaptured == expect else main.FALSE |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 379 | |
| 380 | def pingAndCapture( self, srcHost, dstIp, dstHost, dstIntf, ipv6=False, expect=True, acceptableFailed=0, collectT3=True, t3Simple=False ): |
| 381 | """ |
| 382 | Description: |
| 383 | Ping from src host to dst IP and capture packets at dst Host using Scapy and retry if neccessary |
| 384 | If collectT3 is True, collect t3-troubleshoot output on unexpected scapy results |
| 385 | Options: |
| 386 | srcHost: name of the source host |
| 387 | dstIp: destination IP of the ping packets |
| 388 | dstHost: host that runs Scapy to capture the packets |
| 389 | dstIntf: name of the interface on the destination host |
| 390 | ipv6: ping with IPv6 if True; Otherwise IPv4 |
| 391 | expect: use True if the ping is expected to be captured at destination; |
| 392 | Otherwise False |
| 393 | acceptableFailed: maximum number of failed pings acceptable |
| 394 | collectT3: save t3-troubleshoot output for src and dst host that failed to ping |
| 395 | t3Simple: use t3-troubleshoot-simple command when collecting t3 output |
| 396 | Returns: |
| 397 | main.TRUE if packet capturing result is expected, otherwise main.FALSE |
| 398 | Note: It is assumed that Scapy is already started on the destination host |
| 399 | """ |
| 400 | main.log.info( "Pinging from {} to {}, expected {} capture the packet at {}".format( srcHost, dstIp, |
| 401 | "to" if expect else "not to", dstHost ) ) |
| 402 | # Verify host component has been created |
| 403 | if not hasattr( main, srcHost ): |
| 404 | main.log.info( "Creating component for host {}".format( srcHost ) ) |
| 405 | main.Network.createHostComponent( srcHost ) |
| 406 | srcHandle = getattr( main, srcHost ) |
| 407 | main.log.info( "Starting CLI on host {}".format( srcHost ) ) |
| 408 | srcHandle.startHostCli() |
| 409 | trafficResult = utilities.retry( self.pingAndCaptureHelper, |
| 410 | main.FALSE, |
| 411 | args=( srcHost, dstIp, dstHost, dstIntf, ipv6, expect ), |
| 412 | attempts=acceptableFailed + 1, |
| 413 | sleep=1 ) |
| 414 | if not trafficResult and collectT3: |
| 415 | srcIp = main.Network.getIPAddress( srcHost, proto='IPV6' if ipv6 else 'IPV4' ) |
| 416 | main.log.debug( "Collecting t3 with source {} and destination {}".format( srcIp, dstIp ) ) |
| 417 | cmdList = main.Cluster.active( 0 ).CLI.composeT3Command( srcIp, dstIp, ipv6, True, t3Simple ) |
| 418 | if not cmdList: |
| 419 | main.log.warn( "Failed to compose T3 command with source {} and destination {}".format( srcIp, dstIp ) ) |
| 420 | for i in range( 0, len( cmdList ) ): |
| 421 | cmd = cmdList[ i ] |
| 422 | main.log.debug( "t3 command: {}".format( cmd ) ) |
| 423 | main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, cmd, main.logdir, |
| 424 | "t3-CASE{}-{}-{}-route{}-".format( main.CurrentTestCaseNumber, srcIp, dstIp, i ), |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 425 | timeout=10, |
| 426 | cliPort=main.Cluster.active(0).CLI.karafPort ) |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 427 | return trafficResult |
| 428 | |
| 429 | def pingAndCaptureHelper( self, srcHost, dstIp, dstHost, dstIntf, ipv6=False, expect=True ): |
| 430 | """ |
| 431 | Description: |
| 432 | Ping from src host to dst IP and capture packets at dst Host using Scapy |
| 433 | Options: |
| 434 | srcHost: name of the source host |
| 435 | dstIp: destination IP of the ping packets |
| 436 | dstHost: host that runs Scapy to capture the packets |
| 437 | dstIntf: name of the interface on the destination host |
| 438 | ipv6: ping with IPv6 if True; Otherwise IPv4 |
| 439 | expect: use True if the ping is expected to be captured at destination; |
| 440 | Otherwise False |
| 441 | Returns: |
| 442 | main.TRUE if packet capturing result is expected, otherwise main.FALSE |
| 443 | """ |
| 444 | packetCaptured = True |
| 445 | srcHandle = getattr( main, srcHost ) |
| 446 | dstHandle = getattr( main, dstHost ) |
You Wang | aec6a09 | 2018-08-06 15:36:31 -0700 | [diff] [blame] | 447 | pktFilter = "ip6 host {}".format( dstIp ) if ipv6 else "ip host {}".format( dstIp ) |
| 448 | started = dstHandle.startFilter( ifaceName=dstIntf, pktFilter=pktFilter ) |
You Wang | 3a5f74c | 2018-08-03 14:58:15 -0700 | [diff] [blame] | 449 | if not started: |
| 450 | main.log.error("Failed to start Scapy packet filter") |
| 451 | return main.FALSE |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 452 | srcHandle.pingHostSetAlternative( [ dstIp ], IPv6=ipv6 ) |
| 453 | finished = dstHandle.checkFilter() |
| 454 | packet = "" |
| 455 | if finished: |
You Wang | 548db38 | 2020-08-12 09:17:13 -0700 | [diff] [blame] | 456 | packets = receiver.readPackets( detailed=True ) |
You Wang | 2cb7017 | 2018-07-25 16:44:13 -0700 | [diff] [blame] | 457 | for packet in packets.splitlines(): |
| 458 | main.log.debug( packet ) |
| 459 | else: |
| 460 | kill = dstHandle.killFilter() |
| 461 | main.log.debug( kill ) |
| 462 | packetCaptured = True if "dst={}".format( dstIp ) in packet else False |
| 463 | return main.TRUE if packetCaptured == expect else main.FALSE |