871b25232818b284ba76488cff2c8dea3fb0d2bf
[csit.git] / GPL / tools / trex / trex_astf_stop.py
1 #!/usr/bin/python3
2
3 # Copyright (c) 2021 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 Functionality:
35 1. Stop any running traffic
36 2. Optionally restore reference counter values.
37 3. Return conter differences.
38 """
39
40 import argparse
41 import json
42 import sys
43
44 sys.path.insert(
45     0, u"/opt/trex-core-2.88/scripts/automation/trex_control_plane/interactive/"
46 )
47 from trex.astf.api import ASTFClient
48
49
50 def main():
51     """Stop traffic if any is running. Report xstats."""
52     parser = argparse.ArgumentParser()
53     parser.add_argument(
54         u"--xstat0", type=str, default=u"",
55         help=u"Reference xstat object if any."
56     )
57     parser.add_argument(
58         u"--xstat1", type=str, default=u"",
59         help=u"Reference xstat object if any."
60     )
61     args = parser.parse_args()
62
63     client = ASTFClient()
64     try:
65         # connect to server
66         client.connect()
67
68         client.acquire(force=True)
69         client.stop()
70
71         # Read the stats after the test,
72         # we need to update values before the last trial started.
73         if args.xstat0:
74             snapshot = eval(args.xstat0)
75             client.ports[0].get_xstats().reference_stats = snapshot
76         if args.xstat1:
77             snapshot = eval(args.xstat1)
78             client.ports[1].get_xstats().reference_stats = snapshot
79         # Now we can call the official method to get differences.
80         xstats0 = client.get_xstats(0)
81         xstats1 = client.get_xstats(1)
82
83     # If TRexError happens, let the script fail with stack trace.
84     finally:
85         client.clear_profile()
86         client.disconnect()
87
88     # TODO: check xstats format
89     print(u"##### statistics port 0 #####")
90     print(json.dumps(xstats0, indent=4, separators=(u",", u": ")))
91     print(u"##### statistics port 1 #####")
92     print(json.dumps(xstats1, indent=4, separators=(u",", u": ")))
93
94     tx_0, rx_0 = xstats0[u"tx_good_packets"], xstats0[u"rx_good_packets"]
95     tx_1, rx_1 = xstats1[u"tx_good_packets"], xstats1[u"rx_good_packets"]
96     lost_a, lost_b = tx_0 - rx_1, tx_1 - rx_0
97
98     print(f"packets lost from 0 --> 1:   {lost_a} pkts")
99     print(f"packets lost from 1 --> 0:   {lost_b} pkts")
100
101     total_rcvd, total_sent = rx_0 + rx_1, tx_0 + tx_1
102     total_lost = total_sent - total_rcvd
103     print(
104         f"cps='unknown'; "
105         f"total_received={total_rcvd}; "
106         f"total_sent={total_sent}; "
107         f"frame_loss={total_lost}; "
108         f"latency_stream_0(usec)=-1/-1/-1; "
109         f"latency_stream_1(usec)=-1/-1/-1; "
110         f"latency_hist_stream_0=; "
111         f"latency_hist_stream_1=; "
112     )
113
114
115 if __name__ == u"__main__":
116     main()