14ec817f70eac900b754da6849f32f40d44fbf5b
[csit.git] / GPL / tools / trex / trex_stl_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 stateless 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.stl.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.stl.api import STLClient
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 = STLClient()
64     try:
65         # connect to server
66         client.connect()
67
68         client.acquire(force=True)
69         # TODO: Support unidirection.
70         client.stop(ports=[0, 1])
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 STLError happens, let the script fail with stack trace.
85     finally:
86         client.disconnect()
87
88     print(u"##### statistics port 0 #####")
89     print(json.dumps(xstats0, indent=4, separators=(u",", u": ")))
90     print(u"##### statistics port 1 #####")
91     print(json.dumps(xstats1, indent=4, separators=(u",", u": ")))
92
93     tx_0, rx_0 = xstats0[u"tx_good_packets"], xstats0[u"rx_good_packets"]
94     tx_1, rx_1 = xstats1[u"tx_good_packets"], xstats1[u"rx_good_packets"]
95     lost_a, lost_b = tx_0 - rx_1, tx_1 - rx_0
96
97     print(f"\npackets lost from 0 --> 1:   {lost_a} pkts")
98     print(f"packets lost from 1 --> 0:   {lost_b} pkts")
99
100     total_rcvd, total_sent = rx_0 + rx_1, tx_0 + tx_1
101     total_lost = total_sent - total_rcvd
102     print(
103         f"rate='unknown'; "
104         f"total_received={total_rcvd}; "
105         f"total_sent={total_sent}; "
106         f"frame_loss={total_lost}; "
107         f"target_duration='manual'; "
108         f"approximated_duration='manual'; "
109         f"approximated_rate='unknown'; "
110         f"latency_stream_0(usec)=-1/-1/-1; "
111         f"latency_stream_1(usec)=-1/-1/-1; "
112     )
113
114
115 if __name__ == u"__main__":
116     main()