3 # Copyright (c) 2023 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
39 from collections import OrderedDict # Needed to parse xstats representation.
42 0, "/opt/trex-core-3.03/scripts/automation/trex_control_plane/interactive/"
44 from trex.stl.api import STLClient
48 """Stop traffic if any is running. Report xstats."""
49 parser = argparse.ArgumentParser()
51 "--xstat", type=str, nargs="*", help="Reference xstat object if any."
53 args = parser.parse_args()
58 client.acquire(force=True)
62 # Read the stats after the test,
63 # we need to update values before the last trial started.
64 for i in range(len(client.ports)):
66 snapshot = eval(args.xstat[i])
67 client.ports[i].get_xstats().reference_stats = snapshot
68 # Now we can call the official method to get differences.
69 xstats.append(client.get_xstats(i))
70 print(f"##### statistics port {i} #####")
71 print(json.dumps(xstats[i], indent=4, separators=(",", ": ")))
75 for idx,stat in enumerate(zip(xstats[0::2], xstats[1::2])):
76 lost_r = stat[0]["tx_good_packets"] - stat[1]["rx_good_packets"]
77 lost_l = stat[1]["tx_good_packets"] - stat[0]["rx_good_packets"]
78 print(f"packets lost from {idx*2} --> {idx*2+1}: {lost_r} pkts")
79 print(f"packets lost from {idx*2+1} --> {idx*2}: {lost_l} pkts")
84 total_rcvd += stat["rx_good_packets"]
85 total_sent += stat["tx_good_packets"]
89 f"total_received={total_rcvd}; "
90 f"total_sent={total_sent}; "
91 f"frame_loss={total_sent - total_rcvd}; "
92 f"target_duration='manual'; "
93 f"approximated_duration='manual'; "
94 f"approximated_rate='unknown'; "
95 f"latency_stream_0(usec)=-1/-1/-1; "
96 f"latency_stream_1(usec)=-1/-1/-1; "
100 if __name__ == "__main__":