3 # Copyright (c) 2021 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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 """This script gets a bandwith limit together with other parameters, reads
17 the iPerf3 configuration and sends the traffic. At the end, it measures
18 the packet loss and latency.
27 def simple_burst(args):
28 """Send traffic and measure throughput.
30 :param args: Named arguments from command line.
37 if args.rate and args.frame_size:
38 iperf_frame_size = args.frame_size - 18
39 iperf_rate = float(args.rate)
41 int(args.frame_size) * float(iperf_rate) / args.instances
43 if args.warmup_time > 0:
45 for i in range(0, args.instances):
48 f"ip netns exec {args.namespace} "
49 if args.namespace else u""
52 cmd += f"--client {args.host} "
53 cmd += f"--bind {args.bind} "
54 if args.rate and args.frame_size:
55 cmd += f"--bandwidth {bandwidth} "
56 cmd += f"--length {iperf_frame_size} "
57 cmd += f"--port {5201 + i} "
58 cmd += f"--parallel {args.parallel} "
59 cmd += f"--time {args.warmup_time} "
61 cmd += f"--affinity {args.affinity} "
67 subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
70 for i in range(0, args.instances):
71 if1, _ = if1_process[i].communicate(
72 timeout=args.warmup_time + 60)
81 for i in range(0, args.instances):
83 cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
85 cmd += f"--client {args.host} "
86 cmd += f"--bind {args.bind} "
87 if args.rate and args.frame_size:
88 cmd += f"--bandwidth {bandwidth} "
89 cmd += f"--length {iperf_frame_size} "
90 cmd += f"--port {5201 + i} "
91 cmd += f"--parallel {args.parallel} "
92 cmd += f"--time {args.duration} "
94 cmd += f"--affinity {args.affinity} "
100 subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
104 for i in range(0, args.instances):
105 print(if1_process[i].pid)
107 for i in range(0, args.instances):
108 if1, _ = if1_process[i].communicate(timeout=args.duration + 60)
109 if1_results.append(json.loads(if1))
110 if1_results[i][u"end"][u"command"] = cmd
111 print(f"{json.dumps(if1_results[i]['end'], indent = 4)}")
115 """Main function for the traffic generator using iPerf3.
117 It verifies the given command line arguments and runs "simple_burst"
120 parser = argparse.ArgumentParser()
122 u"--namespace", required=False, type=str,
123 help=u"Port netns name to run iPerf client on."
126 u"--host", required=True, type=str,
127 help=u"Run in client mode, connecting to an iPerf server host."
130 u"--bind", required=True, type=str,
131 help=u"Client bind IP address."
134 u"--udp", action=u"store_true", default=False,
138 u"--affinity", required=False, type=str,
139 help=u"Set the CPU affinity, if possible."
142 u"--duration", required=True, type=float,
143 help=u"Duration of traffic run in seconds (-1=infinite)."
146 u"--frame_size", required=False,
147 help=u"Size of a Frame without padding and IPG."
150 u"--rate", required=False,
151 help=u"Traffic rate with included units (pps)."
154 u"--traffic_directions", default=1, type=int,
155 help=u"Send bi- (2) or uni- (1) directional traffic."
158 u"--warmup_time", type=float, default=5.0,
159 help=u"Traffic warm-up time in seconds, (0=disable)."
162 u"--async_start", action=u"store_true", default=False,
163 help=u"Non-blocking call of the script."
166 u"--instances", default=1, type=int,
167 help=u"The number of simultaneous client instances."
170 u"--parallel", default=8, type=int,
171 help=u"The number of simultaneous client streams."
174 args = parser.parse_args()
176 # Currently limiting to uni- directional traffic.
177 if args.traffic_directions != 1:
178 print(f"Currently only uni- directional traffic is supported!")
184 if __name__ == u"__main__":