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