3 # Start an iPerf connection stream between two Linux namespaces ##
11 """ "Create an iPerf connection stream between two namespaces.
14 iperf = VppIperf() # Create the iPerf Object
15 iperf.client_ns = 'ns1' # Client Namespace
16 iperf.server_ns = 'ns2' # Server Namespace
17 iperf.server_ip = '10.0.0.102' # Server IP Address
18 iperf.start() # Start the connection stream
21 iperf.duration = 15 # Time to transmit for in seconds (Default=10)
23 ## Optionally set any iperf client & server args
25 # Run 4 parallel streams, write to logfile & bind to port 5202
26 iperf.client_args='-P 4 --logfile /tmp/vpp-vm-tests/vpp_iperf.log -p 5202'
27 iperf.server_args='-p 5202'
30 def __init__(self, server_ns=None, client_ns=None, server_ip=None, logger=None):
31 self.server_ns = server_ns
32 self.client_ns = client_ns
33 self.server_ip = server_ip
38 # Set the iperf executable
39 self.iperf = os.path.join(os.getenv("TEST_DATA_DIR") or "/", "usr/bin/iperf")
41 def ensure_init(self):
42 if self.server_ns and self.client_ns and self.server_ip:
46 "Error: Cannot Start." "iPerf object has not been initialized"
49 def start_iperf_server(self):
59 args.extend(self.server_args.split())
62 return subprocess.run(
64 timeout=self.duration + 5,
67 stdout=subprocess.PIPE,
68 stderr=subprocess.STDOUT,
70 except subprocess.TimeoutExpired as e:
71 raise Exception("Error: Timeout expired for iPerf", e.output)
73 def start_iperf_client(self):
85 args.extend(self.client_args.split())
88 return subprocess.run(
90 timeout=self.duration + 5,
95 except subprocess.TimeoutExpired as e:
96 raise Exception("Error: Timeout expired for iPerf", e.output)
98 def start(self, server_only=False, client_only=False):
101 Starts the iperf server daemon & runs the iperf client.
103 server_only -- start the iperf server daemon only
104 client_only -- run the iperf client only
105 Return True if we have no errors in iPerf client, else False.
109 self.start_iperf_server()
111 result = self.start_iperf_client()
112 self.logger.debug(f"Iperf client args: {result.args}")
113 self.logger.debug(result.stdout)
116 f"Error starting Iperf Client in Namespace: {self.client_ns}"
118 self.logger.error(f"Iperf client args: {result.args}")
119 self.logger.error(f"Iperf client has errors: {result.stderr}")
125 ## Functions to start and stop iPerf using the iPerf object
128 client_ns="iprf_client_ns",
129 server_ns="iprf_server_ns",
130 server_ipv4_address="10.0.0.102",
131 server_ipv6_address="2001:1::2",
139 """Start an iperf connection stream using the iPerf object.
141 Starts iPerf an connection stream between an iPerf client in the
142 client namespace (client_ns) and a server in another
143 namespace (server_ns).
146 client_ns - iPerf client namespace
147 server_ns - iPerf server namespace
148 server_ipv4_address - ipv4 address of the server, if ip_version=4
149 server_ipv6_address - ipv6 address of the server, if ip_version=6
150 client_args - Additonal iperf control arguments to be passed
151 to the iperf client from the test (str)
152 server_args - Additonal iperf control arguments to be passed
153 to the iperf server from the test (str)
154 duration - Iperf duration in seconds
155 server_only - start iperf server only
156 client_only - start the iperf client only
160 iperf_server_ip = server_ipv4_address
161 elif ip_version == 6:
162 iperf_server_ip = server_ipv6_address
163 client_args = "-V" + " " + client_args
164 server_args = "-V" + " " + server_args
166 iperf.client_ns = client_ns
167 iperf.server_ns = server_ns
168 iperf.server_ip = iperf_server_ip
169 iperf.client_args = client_args
170 iperf.server_args = server_args
171 iperf.duration = duration
172 iperf.logger = logger
173 return iperf.start(server_only=server_only, client_only=client_only)
177 args = ["pkill", "iperf"]
178 args = " ".join(args)
180 return subprocess.run(
189 if __name__ == "__main__":
190 # Run iPerf using default settings
192 iperf.client_ns = "ns1"
193 iperf.server_ns = "ns2"
194 iperf.server_ip = "10.0.0.102"