Python3: TRex driver
[csit.git] / resources / tools / trex / trex_stateless_stop.py
1 #!/usr/bin/python3
2
3 # Copyright (c) 2019 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)
21  - trex.stl.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 import sys
29 import json
30
31 sys.path.insert(0, "/opt/trex-core-2.54/scripts/automation/"+\
32                    "trex_control_plane/interactive/")
33 from trex.stl.api import *
34
35
36 def main():
37     """Stop traffic if any is running."""
38     client = STLClient()
39     try:
40         # connect to server
41         client.connect()
42
43         client.acquire(force=True)
44         client.stop(ports=[0, 1])
45
46         # read the stats after the test
47         stats = client.get_stats()
48
49         print("#####statistics (approx.)#####")
50         print(json.dumps(stats, indent=4, separators=(',', ': ')))
51
52         lost_a = stats[0]["opackets"] - stats[1]["ipackets"]
53         lost_b = stats[1]["opackets"] - stats[0]["ipackets"]
54
55         print("\npackets lost from 0 --> 1:   {0} pkts".format(lost_a))
56         print("packets lost from 1 --> 0:   {0} pkts".format(lost_b))
57     except STLError as ex_error:
58         print(ex_error, file=sys.stderr)
59         sys.exit(1)
60     finally:
61         client.disconnect()
62
63 if __name__ == "__main__":
64     main()