licenses: Dedicated directory for non Apache files
[vswitchperf.git] / 3rd_party / ixia / pass_fail.tcl
1 #!/usr/bin/env tclsh
2
3 # Copyright (c) 2014, Ixia
4 # Copyright (c) 2015-2016, Intel Corporation
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # 1. Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 #
14 # 2. Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the distribution.
17 #
18 # 3. Neither the name of the copyright holder nor the names of its
19 # contributors may be used to endorse or promote products derived
20 # from this software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 # POSSIBILITY OF SUCH DAMAGE.
34
35 # This file is a modified version of a script generated by Ixia
36 # IxExplorer.
37
38 lappend auto_path [list $lib_path]
39
40 package req IxTclHal
41
42 ###################################################################
43 ########################## Configuration ##########################
44 ###################################################################
45
46 # Verify that the IXIA chassis spec is given
47
48 set reqVars [list "host" "card" "port1" "port2"]
49
50 foreach var $reqVars {
51     set var_ns [namespace which -variable "$var"]
52     if { [string compare $var_ns ""] == 0 } {
53         errorMsg "The '$var' variable is undefined. Did you set it?"
54         return -1
55     }
56 }
57
58 # constants
59
60 set fullHex                             "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"
61 set hexToC5                             "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5"
62
63 #set payloadLookup(64)                   [string range $fullHex 0 11]
64 set payloadLookup(64)                   "000102030405"
65 set payloadLookup(128)                  "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445"
66 set payloadLookup(256)                  "$hexToC5"
67 set payloadLookup(512)                  "$fullHex$hexToC5"
68 set payloadLookup(1024)                 "$fullHex$fullHex$fullHex$hexToC5"
69
70 ###################################################################
71 ###################### Chassis Configuration ######################
72 ###################################################################
73
74 if {[isUNIX]} {
75     if {[ixConnectToTclServer $host]} {
76         errorMsg "Error connecting to Tcl Server $host"
77         return $::TCL_ERROR
78     }
79 }
80
81 ######### Chassis #########
82
83 # Now connect to the chassis
84 if [ixConnectToChassis $host] {
85     ixPuts $::ixErrorInfo
86     return 1
87 }
88
89 # Get the chassis ID to use in port lists
90 set chassis [ixGetChassisID $host]
91
92 #########  Ports #########
93
94 set portList [list [list $chassis $card $port1] \
95                    [list $chassis $card $port2]]
96
97 # Clear ownership of the ports we’ll use
98 if [ixClearOwnership $portList force] {
99     ixPuts $::ixErrorInfo
100     return 1
101 }
102
103 # Take ownership of the ports we’ll use
104 if [ixTakeOwnership $portList] {
105     ixPuts $::ixErrorInfo
106     return 1
107 }
108
109 foreach portElem $portList {
110     set chasNum [lindex $portElem 0]
111     set cardNum [lindex $portElem 1]
112     set portNum [lindex $portElem 2]
113
114     port setFactoryDefaults $chasNum $cardNum $portNum
115     port config -speed                                10000
116     port config -flowControl                          true
117     port config -transmitMode                         portTxModeAdvancedScheduler
118     port config -receiveMode                          [expr $::portCapture|$::portRxModeWidePacketGroup]
119     port config -advertise100FullDuplex               false
120     port config -advertise100HalfDuplex               false
121     port config -advertise10FullDuplex                false
122     port config -advertise10HalfDuplex                false
123     port config -portMode                             port10GigLanMode
124     port config -enableTxRxSyncStatsMode              true
125     port config -txRxSyncInterval                     2000
126     port config -enableTransparentDynamicRateChange   true
127     port config -enableDynamicMPLSMode                true
128     if {[port set $chasNum $cardNum $portNum]} {
129         errorMsg "Error calling port set $chasNum $cardNum $portNum"
130     }
131
132     packetGroup setDefault
133     packetGroup config -numTimeBins                   1
134     if {[packetGroup setRx $chasNum $cardNum $portNum]} {
135         errorMsg "Error calling packetGroup setRx $chasNum $cardNum $portNum"
136     }
137
138     sfpPlus setDefault
139     sfpPlus config -enableAutomaticDetect             false
140     sfpPlus config -txPreTapControlValue              1
141     sfpPlus config -txMainTapControlValue             63
142     sfpPlus config -txPostTapControlValue             2
143     sfpPlus config -rxEqualizerControlValue           0
144     if {[sfpPlus set $chasNum $cardNum $portNum]} {
145         errorMsg "Error calling sfpPlus set $chasNum $cardNum $portNum"
146     }
147
148     filter setDefault
149     filter config -captureTriggerFrameSizeFrom        48
150     filter config -captureTriggerFrameSizeTo          48
151     filter config -captureFilterFrameSizeFrom         48
152     filter config -captureFilterFrameSizeTo           48
153     filter config -userDefinedStat1FrameSizeFrom      48
154     filter config -userDefinedStat1FrameSizeTo        48
155     filter config -userDefinedStat2FrameSizeFrom      48
156     filter config -userDefinedStat2FrameSizeTo        48
157     filter config -asyncTrigger1FrameSizeFrom         48
158     filter config -asyncTrigger1FrameSizeTo           48
159     filter config -asyncTrigger2FrameSizeFrom         48
160     filter config -asyncTrigger2FrameSizeTo           48
161     filter config -userDefinedStat1Enable             true
162     filter config -userDefinedStat2Enable             true
163     filter config -asyncTrigger1Enable                true
164     filter config -asyncTrigger2Enable                true
165     if {[filter set $chasNum $cardNum $portNum]} {
166         errorMsg "Error calling filter set $chasNum $cardNum $portNum"
167     }
168
169     filterPallette setDefault
170     filterPallette config -pattern1                   00
171     filterPallette config -patternMask1               00
172     filterPallette config -patternOffset1             20
173     filterPallette config -patternOffset2             20
174     if {[filterPallette set $chasNum $cardNum $portNum]} {
175         errorMsg "Error calling filterPallette set $chasNum $cardNum $portNum"
176     }
177
178     capture setDefault
179     capture config -sliceSize                         65536
180     if {[capture set $chasNum $cardNum $portNum]} {
181         errorMsg "Error calling capture set $chasNum $cardNum $portNum"
182     }
183
184     if {[interfaceTable select $chasNum $cardNum $portNum]} {
185         errorMsg "Error calling interfaceTable select $chasNum $cardNum $portNum"
186     }
187
188     interfaceTable setDefault
189     if {[interfaceTable set]} {
190         errorMsg "Error calling interfaceTable set"
191     }
192
193     interfaceTable clearAllInterfaces
194     if {[interfaceTable write]} {
195         errorMsg "Error calling interfaceTable write"
196     }
197
198     ixEnablePortIntrinsicLatencyAdjustment $chasNum $cardNum $portNum true
199 }
200
201 ixWritePortsToHardware portList
202
203 if {[ixCheckLinkState $portList] != 0} {
204     errorMsg "One or more port links are down"
205 }
206
207 proc sendTraffic { flowSpec trafficSpec } {
208     # Send traffic from IXIA.
209     #
210     # Transmits traffic from Rx port (port1), and captures traffic at
211     # Tx port (port2).
212     #
213     # Parameters:
214     #   flowSpec - a dict detailing how the packet should be sent. Should be
215     #     of format:
216     #         {type, numpkts, duration, framerate}
217     #   trafficSpec - a dict describing the packet to be sent. Should be
218     #     of format:
219     #         { l2, vlan, l3}
220     #     where each item is in turn a dict detailing the configuration of each
221     #     layer of the packet
222     # Returns:
223     #   Output from Rx end of Ixia if duration != 0, else 0
224
225     ##################################################
226     ################# Initialisation #################
227     ##################################################
228
229     # Configure global variables. See documentation on 'global' for more
230     # information on why this is necessary
231     #   https://www.tcl.tk/man/tcl8.5/tutorial/Tcl13.html
232     global portList
233
234     # Extract the provided dictionaries to local variables to simplify the
235     # rest of the script
236
237     # flow spec
238
239     set streamType              [dict get $flowSpec type]
240     set numPkts                 [dict get $flowSpec numpkts]
241     set duration                [expr {[dict get $flowSpec duration] * 1000}]
242     set frameRate               [dict get $flowSpec framerate]
243
244     # traffic spec
245
246     # extract nested dictionaries
247     set trafficSpec_l2          [dict get $trafficSpec l2]
248     set trafficSpec_l3          [dict get $trafficSpec l3]
249     set trafficSpec_l4          [dict get $trafficSpec l4]
250     set trafficSpec_vlan        [dict get $trafficSpec vlan]
251
252     set frameSize               [dict get $trafficSpec_l2 framesize]
253     set srcMac                  [dict get $trafficSpec_l2 srcmac]
254     set dstMac                  [dict get $trafficSpec_l2 dstmac]
255     set srcPort                 [dict get $trafficSpec_l4 srcport]
256     set dstPort                 [dict get $trafficSpec_l4 dstport]
257
258     set proto                   [dict get $trafficSpec_l3 proto]
259     set srcIp                   [dict get $trafficSpec_l3 srcip]
260     set dstIp                   [dict get $trafficSpec_l3 dstip]
261
262     if {[dict exists $trafficSpec_l3 protocolpadbytes]} {
263         set protocolPad             [dict get $trafficSpec_l4 protocolpad]
264         set protocolPadBytes        [dict get $trafficSpec_l4 protocolpadbytes]
265     }
266
267     set vlanEnabled             [dict get $trafficSpec_vlan enabled]
268     if {$vlanEnabled == 1 } {
269         # these keys won't exist if vlan wasn't enabled
270         set vlanId                  [dict get $trafficSpec_vlan id]
271         set vlanUserPrio            [dict get $trafficSpec_vlan priority]
272         set vlanCfi                 [dict get $trafficSpec_vlan cfi]
273     }
274
275     ##################################################
276     ##################### Streams ####################
277     ##################################################
278
279     streamRegion get $::chassis $::card $::port1
280     if {[streamRegion enableGenerateWarningList $::chassis $::card $::port1 false]} {
281         errorMsg "Error calling streamRegion enableGenerateWarningList  $::chassis $::card $::port1 false"
282     }
283
284     set streamId 1
285
286     stream setDefault
287     stream config -ifg                                9.6
288     stream config -ifgMIN                             19.2
289     stream config -ifgMAX                             25.6
290     stream config -ibg                                9.6
291     stream config -isg                                9.6
292     stream config -rateMode                           streamRateModePercentRate
293     stream config -percentPacketRate                  $frameRate
294     stream config -framesize                          $frameSize
295     stream config -frameType                          "08 00"
296     stream config -sa                                 $srcMac
297     stream config -da                                 $dstMac
298     stream config -numSA                              16
299     stream config -numDA                              16
300     stream config -asyncIntEnable                     true
301     stream config -dma                                $streamType
302     stream config -numBursts                          1
303     stream config -numFrames                          $numPkts
304     stream config -patternType                        incrByte
305     stream config -dataPattern                        x00010203
306     stream config -pattern                            "00 01 02 03"
307
308     protocol setDefault
309     protocol config -name                             ipV4
310     protocol config -ethernetType                     ethernetII
311     if {$vlanEnabled == 1} {
312         protocol config -enable802dot1qTag            vlanSingle
313     }
314
315     if {[info exists protocolPad]} {
316         protocol config -enableProtocolPad                $protocolPad
317     }
318
319     ip setDefault
320     ip config -ipProtocol                             ipV4Protocol[string totitle $proto]
321     ip config -checksum                               "f6 75"
322     ip config -sourceIpAddr                           $srcIp
323     ip config -sourceIpAddrRepeatCount                10
324     ip config -sourceClass                            classA
325     ip config -destIpAddr                             $dstIp
326     ip config -destIpAddrRepeatCount                  10
327     ip config -destClass                              classA
328     ip config -destMacAddr                            $dstMac
329     ip config -destDutIpAddr                          0.0.0.0
330     ip config -ttl                                    64
331     if {[ip set $::chassis $::card $::port1]} {
332         errorMsg "Error calling ip set $::chassis $::card $::port1"
333     }
334
335     "$proto" setDefault
336     "$proto" config -checksum                         "25 81"
337     if {["$proto" set $::chassis $::card $::port1]} {
338         errorMsg "Error calling $proto set $::chassis $::card $::port"
339     }
340
341     if {[info exists protocolPad]} {
342         protocolPad setDefault
343         # VxLAN header with VNI 99 (0x63)
344         # Inner SRC 01:02:03:04:05:06
345         # Inner DST 06:05:04:03:02:01
346         # IP SRC 192.168.0.2
347         # IP DST 192.168.240.9
348         # SRC port 3000 (0x0BB8)
349         # DST port 3001 (0x0BB9)
350         # length 26
351         # UDP Checksum 0x2E93
352
353         # From encap case capture
354         protocolPad config -dataBytes "$protocolPadBytes"
355         if {[protocolPad set $::chassis $::card $::port1]} {
356             errorMsg "Error calling protocolPad set $::chassis $::card $::port"
357             set retCode $::TCL_ERROR
358         }
359     }
360
361     if {$vlanEnabled == 1 } {
362         vlan setDefault
363         vlan config -vlanID                               $vlanId
364         vlan config -userPriority                         $vlanUserPrio
365         vlan config -cfi                                  $vlanCfi
366         vlan config -mode                                 vIdle
367         vlan config -repeat                               10
368         vlan config -step                                 1
369         vlan config -maskval                              "0000XXXXXXXXXXXX"
370         vlan config -protocolTagId                        vlanProtocolTag8100
371     }
372
373     if {[vlan set $::chassis $::card $::port1]} {
374         errorMsg "Error calling vlan set $::chassis $::card $::port1"
375     }
376
377     if {[port isValidFeature $::chassis $::card $::port1 $::portFeatureTableUdf]} {
378         tableUdf setDefault
379         tableUdf clearColumns
380         if {[tableUdf set $::chassis $::card $::port1]} {
381             errorMsg "Error calling tableUdf set $::chassis $::card $::port1"
382         }
383     }
384
385     if {[port isValidFeature $::chassis $::card $::port1 $::portFeatureRandomFrameSizeWeightedPair]} {
386         weightedRandomFramesize setDefault
387         if {[weightedRandomFramesize set $::chassis $::card $::port1]} {
388             errorMsg "Error calling weightedRandomFramesize set $::chassis $::card $::port1"
389         }
390     }
391
392     if {$proto == "tcp"} {
393         tcp setDefault
394         tcp config -sourcePort                            $srcPort
395         tcp config -destPort                              $dstPort
396         if {[tcp set $::chassis $::card $::port1 ]} {
397             errorMsg "Error setting tcp on port $::chassis.$::card.$::port1"
398         }
399
400         if {$vlanEnabled != 1} {
401             udf setDefault
402             udf config -repeat                                1
403             udf config -continuousCount                       true
404             udf config -initval                               {00 00 00 01}
405             udf config -updown                                uuuu
406             udf config -cascadeType                           udfCascadeNone
407             udf config -step                                  1
408
409             packetGroup setDefault
410             packetGroup config -insertSequenceSignature       true
411             packetGroup config -sequenceNumberOffset          38
412             packetGroup config -signatureOffset               42
413             packetGroup config -signature                     "08 71 18 05"
414             packetGroup config -groupIdOffset                 52
415             packetGroup config -groupId                       $streamId
416             packetGroup config -allocateUdf                   true
417             if {[packetGroup setTx $::chassis $::card $::port1 $streamId]} {
418                 errorMsg "Error calling packetGroup setTx $::chassis $::card $::port1 $streamId"
419             }
420         }
421     } elseif {$proto == "udp"} {
422         udp setDefault
423         udp config -sourcePort                            $srcPort
424         udp config -destPort                              $dstPort
425         set packetSize               [dict get $trafficSpec_l3 packetsize]
426         stream config -framesize                          $packetSize
427         if {[udp set $::chassis $::card $::port1]} {
428             errorMsg "Error setting udp on port $::chassis.$::card.$::port1"
429         }
430         errorMsg "frameSize: $frameSize, packetSize: $packetSize, srcMac: $srcMac, dstMac: $dstMac, srcPort: $srcPort, dstPort: $dstPort"
431         if {[info exists protocolPad]} {
432             errorMsg "protocolPad: $protocolPad, protocolPadBytes: $protocolPadBytes"
433         }
434     }
435
436     if {[stream set $::chassis $::card $::port1 $streamId]} {
437         errorMsg "Error calling stream set $::chassis $::card $::port1 $streamId"
438     }
439
440     incr streamId
441     streamRegion generateWarningList $::chassis $::card $::port1
442     ixWriteConfigToHardware portList -noProtocolServer
443
444     if {[packetGroup getRx $::chassis $::card $::port2]} {
445         errorMsg "Error calling packetGroup getRx $::chassis $::card $::port2"
446     }
447
448     ##################################################
449     ######### Traffic Transmit and Results ###########
450     ##################################################
451
452     # Transmit traffic
453
454     logMsg "Clearing stats for all ports"
455     ixClearStats portList
456
457     logMsg "Starting packet groups on port $::port2"
458     ixStartPortPacketGroups $::chassis $::card $::port2
459
460     logMsg "Starting Capture on port $::port2"
461     ixStartPortCapture $::chassis $::card $::port2
462
463     logMsg "Starting transmit on port $::port1"
464     ixStartPortTransmit $::chassis $::card $::port1
465
466     # If duration=0 is passed, exit after starting transmit
467
468     if {$duration == 0} {
469         logMsg "Sending traffic until interrupted"
470         return
471     }
472
473     logMsg "Waiting for $duration ms"
474
475     # Wait for duration - 1 second to get traffic rate
476
477     after [expr "$duration - 1"]
478
479     # Get result
480
481     set result                                        [stopTraffic]
482
483     if {$streamType == "contPacket"} {
484         return $result
485     } elseif {$streamType == "stopStream"} {
486         set payError 0
487         set seqError 0
488         set captureLimit 3000
489
490         # explode results from 'stopTraffic' for ease of use later
491         set framesSent [lindex $result 0]
492         set framesRecv [lindex $result 1]
493         set bytesSent [lindex $result 2]
494         set bytesRecv [lindex $result 3]
495
496         if {$framesSent <= $captureLimit} {
497             captureBuffer get $::chassis $::card $::port2 1 $framesSent
498             set capturedFrames [captureBuffer cget -numFrames]
499
500             set notCaptured [expr "$framesRecv - $capturedFrames"]
501             if {$notCaptured != 0} {
502                 errorMsg "'$notCaptured' frames were not captured"
503             }
504
505             if {$proto == "tcp"} {
506                 for {set z 1} {$z <= $capturedFrames} {incr z} {
507                     captureBuffer getframe $z
508                     set capFrame [captureBuffer cget -frame]
509                     regsub -all " " $capFrame "" frameNoSpaces
510                     set frameNoSpaces
511
512                     set startPayload 108
513                     set endPayload [expr "[expr "$frameSize * 2"] - 9"]
514                     set payload [string range $frameNoSpaces $startPayload $endPayload]
515
516                     if {$vlanEnabled != 1} {
517                         set startSequence 76
518                         set endSequence 83
519                         set sequence [string range $frameNoSpaces $startSequence $endSequence]
520                         scan $sequence %x seqDecimal
521                         set seqDecimal
522                         if {"$payload" != $::payloadLookup($frameSize)} {
523                             errorMsg "frame '$z' payload: invalid payload"
524                             incr payError
525                         }
526                         # variable z increments from 1 to total number of packets
527                         # captured TCP sequence numbers start at 0, not 1. When
528                         # comparing sequence numbers for captured frames, reduce
529                         # variable z by 1 i.e. frame 1 with sequence 0 is compared
530                         # to expected sequence 0.
531                         if {$seqDecimal != $z-1} {
532                             errorMsg "frame '$z' sequence number: Found '$seqDecimal'. Expected '$z'"
533                             incr seqError
534                         }
535                     }
536                 }
537             }
538             logMsg "Sequence Errors: $seqError"
539             logMsg "Payload Errors:  $payError\n"
540         } else {
541             errorMsg "Too many packets for capture."
542         }
543
544         set result [list $framesSent $framesRecv $bytesSent $bytesRecv $payError $seqError]
545         return $result
546     } else {
547         errorMsg "streamtype is not supported: '$streamType'"
548     }
549 }
550
551 proc stopTraffic {} {
552     # Stop sending traffic from IXIA.
553     #
554     # Stops Transmit of traffic from Rx port.
555     #
556     # Returns:
557     #   Output from Rx end of Ixia.
558
559     ##################################################
560     ################# Initialisation #################
561     ##################################################
562
563     # Configure global variables. See documentation on 'global' for more
564     # information on why this is necessary
565     #   https://www.tcl.tk/man/tcl8.5/tutorial/Tcl13.html
566     global portList
567
568     ##################################################
569     ####### Stop Traffic Transmit and Results ########
570     ##################################################
571
572      # Read frame rate of transmission
573
574     if {[stat getRate statAllStats $::chassis $::card $::port1]} {
575         errorMsg "Error reading stat rate on port $::chassis $::card $::port1"
576         return $::TCL_ERROR
577     }
578
579     set sendRate [stat cget -framesSent]
580     set sendRateBytes [stat cget -bytesSent]
581
582     if {[stat getRate statAllStats $::chassis $::card $::port2]} {
583         errorMsg "Error reading stat rate on port $::chassis $::card $::port2"
584         return $::TCL_ERROR
585     }
586
587     set recvRate [stat cget -framesReceived]
588     set recvRateBytes [stat cget -bytesReceived]
589
590     # Wait for a second, else we get funny framerate statistics
591     after 1
592
593     # Stop transmission of traffic
594     ixStopTransmit portList
595
596     if {[ixCheckTransmitDone portList] == $::TCL_ERROR} {
597         return -code error
598     } else {
599         logMsg "Transmission is complete.\n"
600     }
601
602     ixStopPacketGroups portList
603     ixStopCapture portList
604
605     # Get statistics
606
607     if {[stat get statAllStats $::chassis $::card $::port1]} {
608         errorMsg "Error reading stat on port $::chassis $::card $::port1"
609         return $::TCL_ERROR
610     }
611
612     set bytesSent [stat cget -bytesSent]
613     set framesSent [stat cget -framesSent]
614
615     if {[stat get statAllStats $::chassis $::card $::port2]} {
616         errorMsg "Error reading stat on port $::chassis $::card $::port2"
617         return $::TCL_ERROR
618     }
619
620     set bytesRecv [stat cget -bytesReceived]
621     set framesRecv [stat cget -framesReceived]
622
623     set bytesDropped [expr "$bytesSent - $bytesRecv"]
624     set framesDropped [expr "$framesSent - $framesRecv"]
625
626     logMsg "Frames Sent:       $framesSent"
627     logMsg "Frames Recv:       $framesRecv"
628     logMsg "Frames Dropped:    $framesDropped\n"
629
630     logMsg "Bytes Sent:        $bytesSent"
631     logMsg "Bytes Recv:        $bytesRecv"
632     logMsg "Bytes Dropped:     $bytesDropped\n"
633
634     logMsg "Frame Rate Sent:   $sendRate"
635     logMsg "Frame Rate Recv:   $recvRate\n"
636
637     set result [list $framesSent $framesRecv $bytesSent $bytesRecv $sendRate $recvRate $sendRateBytes $recvRateBytes]
638
639     return $result
640 }
641
642 proc rfcThroughputTest { testSpec trafficSpec } {
643     # Execute RFC tests from IXIA.
644     #
645     # Wraps the sendTraffic proc, repeatedly calling it, storing the result and
646     # performing an iterative binary search to find the highest possible RFC
647     # transmission rate. Abides by the specification of RFC2544 as given by the
648     # IETF:
649     #
650     #   https://www.ietf.org/rfc/rfc2544.txt
651     #
652     # Parameters:
653     #   testSpec - a dict detailing how the test should be run. Should be
654     #     of format:
655     #         {numtrials, duration, lossrate}
656     #   trafficSpec - a dict describing the packet to be sent. Should be
657     #     of format:
658     #         { l2, l3}
659     #     where each item is in turn a dict detailing the configuration of each
660     #     layer of the packet
661     # Returns:
662     #   Highest rate with acceptable packet loss.
663
664     ##################################################
665     ################# Initialisation #################
666     ##################################################
667
668     # Configure global variables. See documentation on 'global' for more
669     # information on why this is necessary
670     #   https://www.tcl.tk/man/tcl8.5/tutorial/Tcl13.html
671     global portList
672
673     # Extract the provided dictionaries to local variables to simplify the
674     # rest of the script
675
676     # testSpec
677
678     # RFC2544 to IXIA terminology mapping (it affects Ixia configuration below):
679     # Test    => Trial
680     # Trial   => Iteration
681     set numTrials               [dict get $testSpec tests]  ;# we don't use this yet
682     set duration                [dict get $testSpec duration]
683     set lossRate                [dict get $testSpec lossrate]
684     set multipleStream          [dict get $testSpec multipleStreams]  ;# we don't use this yet
685
686     # variables used for binary search of results
687     set min 1
688     set max 100
689     set diff [expr "$max - $min"]
690
691     set result [list 0 0 0 0 0 0 0 0]  ;# best result found so far
692     set percentRate 100  ;# starting throughput percentage rate
693
694     ##################################################
695     ######### Traffic Transmit and Results ###########
696     ##################################################
697
698     # iterate a maximum of 20 times, sending packets at a set  rate to
699     # find fastest possible rate with acceptable packetloss
700     #
701     # As a reminder, the binary search works something like this:
702     #
703     #   percentRate < idealValue --> min = percentRate
704     #   percentRate > idealValue --> max = percentRate
705     #   percentRate = idealValue --> max = min = percentRate
706     #
707     for {set i 0} {$i < 20} {incr i} {
708         dict set flowSpec type                        "contPacket"
709         dict set flowSpec numpkts                     100 ;# this can be bypassed
710         dict set flowSpec duration                    $duration
711         dict set flowSpec framerate                   $percentRate
712
713         set flowStats [sendTraffic $flowSpec $trafficSpec]
714
715         # explode results from 'sendTraffic' for ease of use later
716         set framesSent [lindex $flowStats 0]
717         set framesRecv [lindex $flowStats 1]
718         set sendRate [lindex $flowStats 4]
719
720         set framesDropped [expr "$framesSent - $framesRecv"]
721         if {$framesSent > 0} {
722             set framesDroppedRate [expr "double($framesDropped) / $framesSent"]
723         } else {
724             set framesDroppedRate 100
725         }
726
727         # check if we've already found the rate before 10 iterations, i.e.
728         # 'percentRate = idealValue'. This is as accurate as we can get with
729         # integer values.
730         if {[expr "$max - $min"] <= 0.5 } {
731             break
732         }
733
734         # handle 'percentRate <= idealValue' case
735         if {$framesDroppedRate <= $lossRate} {
736             logMsg "Frame sendRate of '$sendRate' pps succeeded ('$framesDropped' frames dropped)"
737
738             set result $flowStats
739             set min $percentRate
740
741             set percentRate [expr "$percentRate + ([expr "$max - $min"] * 0.5)"]
742         # handle the 'percentRate > idealValue' case
743         } else {
744             if {$framesDropped == $framesSent} {
745                 errorMsg "Dropped all frames!"
746             }
747
748             errorMsg "Frame sendRate of '$sendRate' pps failed ('$framesDropped' frames dropped)"
749
750             set max $percentRate
751             set percentRate [expr "$percentRate - ([expr "$max - $min"] * 0.5)"]
752         }
753     }
754
755     set bestRate [lindex $result 4]
756
757     logMsg "$lossRate% packet loss rate: $bestRate"
758
759     return $result
760 }