38b7c2674504ae65d90c19ea4a00b796690569e4
[csit.git] / GPL / tools / trex / trex_stl_profile.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 module gets a traffic profile together with other parameters, reads
26 the profile and sends the traffic. At the end, it measures the packet loss and
27 latency.
28 """
29
30 import argparse
31 import json
32 import sys
33 import time
34
35 sys.path.insert(
36     0, u"/opt/trex-core-2.86/scripts/automation/trex_control_plane/interactive/"
37 )
38 from trex.stl.api import *
39
40
41 def fmt_latency(lat_min, lat_avg, lat_max, hdrh):
42     """Return formatted, rounded latency.
43
44     :param lat_min: Min latency
45     :param lat_avg: Average latency
46     :param lat_max: Max latency
47     :param hdrh: Base64 encoded compressed HDRHistogram object.
48     :type lat_min: str
49     :type lat_avg: str
50     :type lat_max: str
51     :type hdrh: str
52     :return: Formatted and rounded output (hdrh unchanged) "min/avg/max/hdrh".
53     :rtype: str
54     """
55     try:
56         t_min = int(round(float(lat_min)))
57     except ValueError:
58         t_min = int(-1)
59     try:
60         t_avg = int(round(float(lat_avg)))
61     except ValueError:
62         t_avg = int(-1)
63     try:
64         t_max = int(round(float(lat_max)))
65     except ValueError:
66         t_max = int(-1)
67
68     return u"/".join(str(tmp) for tmp in (t_min, t_avg, t_max, hdrh))
69
70
71 def simple_burst(
72         profile_file,
73         duration,
74         framesize,
75         rate,
76         port_0,
77         port_1,
78         latency,
79         async_start=False,
80         traffic_directions=2,
81         force=False,
82     ):
83     """Send traffic and measure packet loss and latency.
84
85     Procedure:
86      - reads the given traffic profile with streams,
87      - connects to the T-rex client,
88      - resets the ports,
89      - removes all existing streams,
90      - adds streams from the traffic profile to the ports,
91      - if the warm-up time is more than 0, sends the warm-up traffic, reads the
92        statistics,
93      - clears the statistics from the client,
94      - starts the traffic,
95      - waits for the defined time (or runs forever if async mode is defined),
96      - stops the traffic,
97      - reads and displays the statistics and
98      - disconnects from the client.
99
100     :param profile_file: A python module with T-rex traffic profile.
101     :param framesize: Frame size.
102     :param duration: Duration of traffic run in seconds (-1=infinite).
103     :param rate: Traffic rate [percentage, pps, bps].
104     :param port_0: Port 0 on the traffic generator.
105     :param port_1: Port 1 on the traffic generator.
106     :param latency: With latency stats.
107     :param async_start: Start the traffic and exit.
108     :param traffic_directions: Bidirectional (2) or unidirectional (1) traffic.
109     :param force: Force start regardless of ports state.
110     :type profile_file: str
111     :type framesize: int or str
112     :type duration: float
113     :type rate: str
114     :type port_0: int
115     :type port_1: int
116     :type latency: bool
117     :type async_start: bool
118     :type traffic_directions: int
119     :type force: bool
120     """
121     client = None
122     total_rcvd = 0
123     total_sent = 0
124     approximated_duration = 0.0
125     lost_a = 0
126     lost_b = 0
127     lat_a = u"-1/-1/-1/"
128     lat_b = u"-1/-1/-1/"
129
130     # Read the profile:
131     try:
132         print(f"### Profile file:\n{profile_file}")
133         profile = STLProfile.load(
134             profile_file, direction=0, port_id=0, framesize=framesize,
135             rate=rate
136         )
137         streams = profile.get_streams()
138     except STLError:
139         print(f"Error while loading profile '{profile_file}'!")
140         raise
141
142     try:
143         # Create the client:
144         client = STLClient()
145         # Connect to server:
146         client.connect()
147         # Prepare our ports (the machine has 0 <--> 1 with static route):
148         client.reset(ports=[port_0, port_1])
149         client.remove_all_streams(ports=[port_0, port_1])
150
151         if u"macsrc" in profile_file:
152             client.set_port_attr(ports=[port_0, port_1], promiscuous=True)
153         if isinstance(framesize, int):
154             last_stream_a = int((len(streams) - 2 ) / 2)
155             last_stream_b = (last_stream_a * 2)
156             client.add_streams(streams[0:last_stream_a], ports=[port_0])
157             if traffic_directions > 1:
158                 client.add_streams(
159                     streams[last_stream_a:last_stream_b], ports=[port_1])
160         elif isinstance(framesize, str):
161             client.add_streams(streams[0:3], ports=[port_0])
162             if traffic_directions > 1:
163                 client.add_streams(streams[3:6], ports=[port_1])
164         if latency:
165             try:
166                 if isinstance(framesize, int):
167                     client.add_streams(streams[last_stream_b], ports=[port_0])
168                     if traffic_directions > 1:
169                         client.add_streams(
170                             streams[last_stream_b + 1], ports=[port_1])
171                 elif isinstance(framesize, str):
172                     latency = False
173             except STLError:
174                 # Disable latency if NIC does not support requested stream type
175                 print(u"##### FAILED to add latency streams #####")
176                 latency = False
177         ports = [port_0]
178         if traffic_directions > 1:
179             ports.append(port_1)
180
181         # Clear the stats before injecting:
182         client.clear_stats()
183         lost_a = 0
184         lost_b = 0
185
186         # Choose rate and start traffic:
187         client.start(
188             ports=ports,
189             mult=rate,
190             duration=duration,
191             force=force,
192             core_mask=STLClient.CORE_MASK_PIN,
193         )
194
195         if async_start:
196             # For async stop, we need to export the current snapshot.
197             xsnap0 = client.ports[0].get_xstats().reference_stats
198             print(f"Xstats snapshot 0: {xsnap0!r}")
199             if traffic_directions > 1:
200                 xsnap1 = client.ports[1].get_xstats().reference_stats
201                 print(f"Xstats snapshot 1: {xsnap1!r}")
202         else:
203             # Block until done:
204             time_start = time.monotonic()
205             client.wait_on_traffic(ports=ports, timeout=duration+30)
206             time_stop = time.monotonic()
207             approximated_duration = time_stop - time_start
208
209             if client.get_warnings():
210                 for warning in client.get_warnings():
211                     print(warning)
212
213             # Read the stats after the test
214             stats = client.get_stats()
215
216             print(u"##### Statistics #####")
217             print(json.dumps(stats, indent=4, separators=(u",", u": ")))
218
219             lost_a = stats[port_0][u"opackets"] - stats[port_1][u"ipackets"]
220             if traffic_directions > 1:
221                 lost_b = stats[port_1][u"opackets"] - stats[port_0][u"ipackets"]
222
223             # Stats index is not a port number, but "pgid".
224             if latency:
225                 lat_obj = stats[u"latency"][0][u"latency"]
226                 lat_a = fmt_latency(
227                     str(lat_obj[u"total_min"]), str(lat_obj[u"average"]),
228                     str(lat_obj[u"total_max"]), str(lat_obj[u"hdrh"]))
229                 if traffic_directions > 1:
230                     lat_obj = stats[u"latency"][1][u"latency"]
231                     lat_b = fmt_latency(
232                         str(lat_obj[u"total_min"]), str(lat_obj[u"average"]),
233                         str(lat_obj[u"total_max"]), str(lat_obj[u"hdrh"]))
234
235             if traffic_directions > 1:
236                 total_sent = stats[0][u"opackets"] + stats[1][u"opackets"]
237                 total_rcvd = stats[0][u"ipackets"] + stats[1][u"ipackets"]
238             else:
239                 total_sent = stats[port_0][u"opackets"]
240                 total_rcvd = stats[port_1][u"ipackets"]
241
242             print(f"\npackets lost from {port_0} --> {port_1}: {lost_a} pkts")
243             if traffic_directions > 1:
244                 print(f"packets lost from {port_1} --> {port_0}: {lost_b} pkts")
245
246     except STLError:
247         print(u"T-Rex STL runtime error!", file=sys.stderr)
248         raise
249
250     finally:
251         if async_start:
252             if client:
253                 client.disconnect(stop_traffic=False, release_ports=True)
254         else:
255             if client:
256                 client.disconnect()
257             print(
258                 f"rate={rate!r}; "
259                 f"total_received={total_rcvd}; "
260                 f"total_sent={total_sent}; "
261                 f"frame_loss={lost_a + lost_b}; "
262                 f"target_duration={duration!r}; "
263                 f"approximated_duration={approximated_duration!r}; "
264                 f"latency_stream_0(usec)={lat_a}; "
265                 f"latency_stream_1(usec)={lat_b}; "
266             )
267
268
269 def main():
270     """Main function for the traffic generator using T-rex.
271
272     It verifies the given command line arguments and runs "simple_burst"
273     function.
274     """
275     parser = argparse.ArgumentParser()
276     parser.add_argument(
277         u"-p", u"--profile", required=True, type=str,
278         help=u"Python traffic profile."
279     )
280     parser.add_argument(
281         u"-d", u"--duration", required=True, type=float,
282         help=u"Duration of traffic run."
283     )
284     parser.add_argument(
285         u"-s", u"--frame_size", required=True,
286         help=u"Size of a Frame without padding and IPG."
287     )
288     parser.add_argument(
289         u"-r", u"--rate", required=True,
290         help=u"Traffic rate with included units (pps)."
291     )
292     parser.add_argument(
293         u"--port_0", required=True, type=int,
294         help=u"Port 0 on the traffic generator."
295     )
296     parser.add_argument(
297         u"--port_1", required=True, type=int,
298         help=u"Port 1 on the traffic generator."
299     )
300     parser.add_argument(
301         u"--async_start", action=u"store_true", default=False,
302         help=u"Non-blocking call of the script."
303     )
304     parser.add_argument(
305         u"--latency", action=u"store_true", default=False,
306         help=u"Add latency stream."
307     )
308     parser.add_argument(
309         u"--traffic_directions", type=int, default=2,
310         help=u"Send bi- (2) or uni- (1) directional traffic."
311     )
312     parser.add_argument(
313         u"--force", action=u"store_true", default=False,
314         help=u"Force start regardless of ports state."
315     )
316
317     args = parser.parse_args()
318
319     try:
320         framesize = int(args.frame_size)
321     except ValueError:
322         framesize = args.frame_size
323
324     simple_burst(
325         profile_file=args.profile,
326         duration=args.duration,
327         framesize=framesize,
328         rate=args.rate,
329         port_0=args.port_0,
330         port_1=args.port_1,
331         latency=args.latency,
332         async_start=args.async_start,
333         traffic_directions=args.traffic_directions,
334         force=args.force,
335     )
336
337
338 if __name__ == u"__main__":
339     main()