feat(tests): IPv6 fixes
[csit.git] / GPL / tools / trex / trex_astf_stop.py
1 #!/usr/bin/python3
2
3 # Copyright (c) 2023 Cisco and/or its affiliates.
4 #
5 # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 #
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:
11 #
12 #     http://www.apache.org/licenses/LICENSE-2.0
13 #     https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14 #
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
18 # Apache 2.
19 #
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.
25
26 """This script uses T-REX advanced stateful API to drive t-rex instance.
27
28 Requirements:
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.astf.api library
32 - Script must be executed on a node with T-REX instance
33 """
34
35 import argparse
36 import json
37 import sys
38
39 from collections import OrderedDict  # Needed to parse xstats representation.
40
41 sys.path.insert(
42     0, "/opt/trex-core-3.03/scripts/automation/trex_control_plane/interactive/"
43 )
44 from trex.astf.api import ASTFClient
45
46
47 def main():
48     """Stop traffic if any is running. Report xstats."""
49     parser = argparse.ArgumentParser()
50     parser.add_argument(
51         "--xstat", type=str, nargs="*", help="Reference xstat object if any."
52     )
53     args = parser.parse_args()
54
55     client = ASTFClient()
56     try:
57         client.connect()
58         client.acquire(force=True)
59         client.stop()
60
61         xstats = list()
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)):
65             if args.xstat[i]:
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=(",", ": ")))
72     finally:
73         client.reset()
74         client.disconnect()
75
76     for idx,stat in enumerate(zip(xstats[0::2], xstats[1::2])):
77         lost_r = stat[0]["tx_good_packets"] - stat[1]["rx_good_packets"]
78         lost_l = stat[1]["tx_good_packets"] - stat[0]["rx_good_packets"]
79         print(f"packets lost from {idx*2} --> {idx*2+1}:   {lost_r} pkts")
80         print(f"packets lost from {idx*2+1} --> {idx*2}:   {lost_l} pkts")
81
82     total_rcvd = 0
83     total_sent = 0
84     for stat in xstats:
85         total_rcvd += stat["rx_good_packets"]
86         total_sent += stat["tx_good_packets"]
87
88     print(
89         f"cps='unknown'; "
90         f"total_received={total_rcvd}; "
91         f"total_sent={total_sent}; "
92         f"frame_loss={total_sent - total_rcvd}; "
93         f"latency_stream_0(usec)=-1/-1/-1; "
94         f"latency_stream_1(usec)=-1/-1/-1; "
95         f"latency_hist_stream_0=; "
96         f"latency_hist_stream_1=; "
97     )
98
99
100 if __name__ == "__main__":
101     main()