Switch licenses in GPL directory
[csit.git] / GPL / tools / trex / trex_astf_stop.py
1 #!/usr/bin/python3
2
3 # Copyright (c) 2020 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 Apache 2.
18 #
19 # Unless required by applicable law or agreed to in writing, software
20 # distributed under the License is distributed on an "AS IS" BASIS,
21 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 # See the License for the specific language governing permissions and
23 # limitations under the License.
24
25 """This script uses T-REX advanced stateful API to drive t-rex instance.
26
27 Requirements:
28 - T-REX: https://github.com/cisco-system-traffic-generator/trex-core
29  - compiled and running T-REX process (eg. ./t-rex-64 -i)
30  - trex.astf.api library
31 - Script must be executed on a node with T-REX instance
32
33 Functionality:
34 1. Stop any running traffic
35 2. Optionally restore reference counter values.
36 3. Return conter differences.
37 """
38
39 import argparse
40 import json
41 import sys
42
43 from collections import OrderedDict  # Needed to parse xstats representation.
44
45 sys.path.insert(
46     0, u"/opt/trex-core-2.82/scripts/automation/trex_control_plane/interactive/"
47 )
48 from trex.astf.api import *
49
50
51 def main():
52     """Stop traffic if any is running. Report xstats."""
53     parser = argparse.ArgumentParser()
54     parser.add_argument(
55         u"--xstat0", type=str, default=u"",
56         help=u"Reference xstat object if any."
57     )
58     parser.add_argument(
59         u"--xstat1", type=str, default=u"",
60         help=u"Reference xstat object if any."
61     )
62     args = parser.parse_args()
63
64     client = ASTFClient()
65     try:
66         # connect to server
67         client.connect()
68
69         client.acquire(force=True)
70         client.stop()
71
72         # Read the stats after the test,
73         # we need to update values before the last trial started.
74         if args.xstat0:
75             snapshot = eval(args.xstat0)
76             client.ports[0].get_xstats().reference_stats = snapshot
77         if args.xstat1:
78             snapshot = eval(args.xstat1)
79             client.ports[1].get_xstats().reference_stats = snapshot
80         # Now we can call the official method to get differences.
81         xstats0 = client.get_xstats(0)
82         xstats1 = client.get_xstats(1)
83
84     # If TRexError happens, let the script fail with stack trace.
85     finally:
86         client.clear_profile()
87         client.disconnect()
88
89     # TODO: check xstats format
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": ")))
94
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
98
99     print(f"packets lost from 0 --> 1:   {lost_a} pkts")
100     print(f"packets lost from 1 --> 0:   {lost_b} pkts")
101
102     total_rcvd, total_sent = rx_0 + rx_1, tx_0 + tx_1
103     total_lost = total_sent - total_rcvd
104     print(
105         f"cps='unknown'; "
106         f"total_received={total_rcvd}; "
107         f"total_sent={total_sent}; "
108         f"frame_loss={total_lost}; "
109         f"latency_stream_0(usec)=-1/-1/-1; "
110         f"latency_stream_1(usec)=-1/-1/-1; "
111         f"latency_hist_stream_0={}; "
112         f"latency_hist_stream_1={}; "
113     )
114
115
116 if __name__ == u"__main__":
117     main()