3 # Copyright (c) 2021 Cisco and/or its affiliates.
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 # Licensed under the Apache License 2.0 or
8 # GNU General Public License v2.0 or later; you may not use this file
9 # except in compliance with one of these Licenses. You
10 # may obtain a copy of the Licenses at:
12 # http://www.apache.org/licenses/LICENSE-2.0
13 # https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
15 # Note: If this file is linked with Scapy, which is GPLv2+, your use of it
16 # must be under GPLv2+. If at any point in the future it is no longer linked
17 # with Scapy (or other GPLv2+ licensed software), you are free to choose
20 # Unless required by applicable law or agreed to in writing, software
21 # distributed under the License is distributed on an "AS IS" BASIS,
22 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 # See the License for the specific language governing permissions and
24 # limitations under the License.
26 """This script uses T-REX stateless API to drive t-rex instance.
29 - T-REX: https://github.com/cisco-system-traffic-generator/trex-core
30 - compiled and running T-REX process (eg. ./t-rex-64 -i)
31 - trex.stl.api library
32 - Script must be executed on a node with T-REX instance
35 1. Stop any running traffic
36 2. Optionally restore reference counter values.
37 3. Return conter differences.
44 from collections import OrderedDict # Needed to parse xstats representation.
47 0, u"/opt/trex-core-2.86/scripts/automation/trex_control_plane/interactive/"
49 from trex.stl.api import *
53 """Stop traffic if any is running. Report xstats."""
54 parser = argparse.ArgumentParser()
56 u"--xstat0", type=str, default=u"",
57 help=u"Reference xstat object if any."
60 u"--xstat1", type=str, default=u"",
61 help=u"Reference xstat object if any."
63 args = parser.parse_args()
70 client.acquire(force=True)
71 # TODO: Support unidirection.
72 client.stop(ports=[0, 1])
74 # Read the stats after the test,
75 # we need to update values before the last trial started.
77 snapshot = eval(args.xstat0)
78 client.ports[0].get_xstats().reference_stats = snapshot
80 snapshot = eval(args.xstat1)
81 client.ports[1].get_xstats().reference_stats = snapshot
82 # Now we can call the official method to get differences.
83 xstats0 = client.get_xstats(0)
84 xstats1 = client.get_xstats(1)
86 # If STLError happens, let the script fail with stack trace.
90 print(u"##### statistics port 0 #####")
91 print(json.dumps(xstats0, indent=4, separators=(u",", u": ")))
92 print(u"##### statistics port 1 #####")
93 print(json.dumps(xstats1, indent=4, separators=(u",", u": ")))
95 tx_0, rx_0 = xstats0[u"tx_good_packets"], xstats0[u"rx_good_packets"]
96 tx_1, rx_1 = xstats1[u"tx_good_packets"], xstats1[u"rx_good_packets"]
97 lost_a, lost_b = tx_0 - rx_1, tx_1 - rx_0
99 print(f"\npackets lost from 0 --> 1: {lost_a} pkts")
100 print(f"packets lost from 1 --> 0: {lost_b} pkts")
102 total_rcvd, total_sent = rx_0 + rx_1, tx_0 + tx_1
103 total_lost = total_sent - total_rcvd
106 f"total_received={total_rcvd}; "
107 f"total_sent={total_sent}; "
108 f"frame_loss={total_lost}; "
109 f"target_duration='manual'; "
110 f"approximated_duration='manual'; "
111 f"approximated_rate='unknown'; "
112 f"latency_stream_0(usec)=-1/-1/-1; "
113 f"latency_stream_1(usec)=-1/-1/-1; "
117 if __name__ == u"__main__":