CSIT-687: Directory structure reorganization
[csit.git] / resources / tools / trex / trex_stateless_stop.py
1 #!/usr/bin/python
2
3 # Copyright (c) 2016 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """This script uses T-REX stateless API to drive t-rex instance.
17
18 Requirements:
19 - T-REX: https://github.com/cisco-system-traffic-generator/trex-core
20  - compiled and running T-REX process (eg. ./t-rex-64 -i -c 4)
21  - trex_stl_lib.api library
22 - Script must be executed on a node with T-REX instance
23
24 Functionality:
25 1. Stop any running traffic
26
27 """
28
29 import sys
30 import json
31
32 sys.path.insert(0, "/opt/trex-core-2.25/scripts/automation/"+\
33                    "trex_control_plane/stl/")
34 from trex_stl_lib.api import *
35
36
37 def stop_all_traffic_streams():
38     """Stop traffic if any is running.
39
40     :return: nothing
41     """
42
43     # create client
44     client = STLClient(verbose_level=LoggerApi.VERBOSE_QUIET)
45
46     try:
47         # connect to server
48         client.connect()
49
50         client.acquire(force=True)
51         client.stop(ports=[0, 1])
52
53         # read the stats after the test
54         stats = client.get_stats()
55
56         print("#####statistics (approx.)#####")
57         print(json.dumps(stats, indent=4,
58                          separators=(',', ': '), sort_keys=True))
59
60         lost_a = stats[0]["opackets"] - stats[1]["ipackets"]
61         lost_b = stats[1]["opackets"] - stats[0]["ipackets"]
62
63         print("\npackets lost from 0 --> 1:   {0} pkts".format(lost_a))
64         print("packets lost from 1 --> 0:   {0} pkts".format(lost_b))
65
66     except STLError as ex_error:
67         print_error(str(ex_error))
68         sys.exit(1)
69
70     finally:
71         client.disconnect()
72
73
74 def print_error(msg):
75     """Print error message on stderr.
76
77     :param msg: Error message to print.
78     :type msg: string
79     :return: nothing
80     """
81
82     sys.stderr.write(msg+'\n')
83
84
85 def main():
86     """Main function."""
87
88     stop_all_traffic_streams()
89
90 if __name__ == "__main__":
91     main()

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.