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 module 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):
47 cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
49 cmd += f"--client {args.host} "
50 cmd += f"--bind {args.bind} "
51 if args.rate and args.frame_size:
52 cmd += f"--bandwidth {bandwidth} "
53 cmd += f"--length {iperf_frame_size} "
54 cmd += f"--port {5201 + i} "
55 cmd += f"--parallel {args.parallel} "
56 cmd += f"--time {args.warmup_time} "
58 cmd += f"--affinity {args.affinity} "
64 subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
67 for i in range(0, args.instances):
68 if1, _ = if1_process[i].communicate(
69 timeout=args.warmup_time + 60)
78 for i in range(0, args.instances):
80 cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
82 cmd += f"--client {args.host} "
83 cmd += f"--bind {args.bind} "
84 if args.rate and args.frame_size:
85 cmd += f"--bandwidth {bandwidth} "
86 cmd += f"--length {iperf_frame_size} "
87 cmd += f"--port {5201 + i} "
88 cmd += f"--parallel {args.parallel} "
89 cmd += f"--time {args.duration} "
91 cmd += f"--affinity {args.affinity} "
97 subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
101 for i in range(0, args.instances):
102 print(if1_process[i].pid)
104 for i in range(0, args.instances):
105 if1, _ = if1_process[i].communicate(timeout=args.duration + 60)
106 if1_results.append(json.loads(if1))
107 if1_results[i][u"end"][u"command"] = cmd
108 print(f"{json.dumps(if1_results[i]['end'], indent = 4)}")
112 """Main function for the traffic generator using iPerf3.
114 It verifies the given command line arguments and runs "simple_burst"
117 parser = argparse.ArgumentParser()
119 u"--namespace", required=False, type=str,
120 help=u"Port netns name to run iPerf client on."
123 u"--host", required=True, type=str,
124 help=u"Run in client mode, connecting to an iPerf server host."
127 u"--bind", required=True, type=str,
128 help=u"Client bind IP address."
131 u"--udp", action=u"store_true", default=False,
135 u"--affinity", required=False, type=str,
136 help=u"Set the CPU affinity, if possible."
139 u"--duration", required=True, type=float,
140 help=u"Duration of traffic run in seconds (-1=infinite)."
143 u"--frame_size", required=False,
144 help=u"Size of a Frame without padding and IPG."
147 u"--rate", required=False,
148 help=u"Traffic rate with included units (pps)."
151 u"--traffic_directions", default=1, type=int,
152 help=u"Send bi- (2) or uni- (1) directional traffic."
155 u"--warmup_time", type=float, default=5.0,
156 help=u"Traffic warm-up time in seconds, (0=disable)."
159 u"--async_start", action=u"store_true", default=False,
160 help=u"Non-blocking call of the script."
163 u"--instances", default=1, type=int,
164 help=u"The number of simultaneous client instances."
167 u"--parallel", default=8, type=int,
168 help=u"The number of simultaneous client streams."
171 args = parser.parse_args()
173 # Currently limiting to uni- directional traffic.
174 if args.traffic_directions != 1:
175 print(f"Currently only uni- directional traffic is supported!")
181 if __name__ == u"__main__":