blob: 42b5ea8a5231c1f4a872c75eb2a0e5d66f55636d [file] [log] [blame]
Naveen Joy7ea7ab52021-05-11 10:31:18 -07001#!/usr/bin/env python
2
3# Start an iPerf connection stream between two Linux namespaces ##
4
5import subprocess
6import os
Naveen Joye4168932022-10-04 14:22:05 -07007import sys
Naveen Joy7ea7ab52021-05-11 10:31:18 -07008
9
10class VppIperf:
11 """ "Create an iPerf connection stream between two namespaces.
12
13 Usage:
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
19
20 Optional:
21 iperf.duration = 15 # Time to transmit for in seconds (Default=10)
22
23 ## Optionally set any iperf client & server args
24 Example:
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'
28 """
29
Naveen Joye4168932022-10-04 14:22:05 -070030 def __init__(self, server_ns=None, client_ns=None, server_ip=None, logger=None):
Naveen Joy7ea7ab52021-05-11 10:31:18 -070031 self.server_ns = server_ns
32 self.client_ns = client_ns
33 self.server_ip = server_ip
34 self.duration = 10
35 self.client_args = ""
36 self.server_args = ""
Naveen Joye4168932022-10-04 14:22:05 -070037 self.logger = logger
Naveen Joy7ea7ab52021-05-11 10:31:18 -070038 # Set the iperf executable
39 self.iperf = os.path.join(os.getenv("TEST_DATA_DIR") or "/", "usr/bin/iperf")
40
41 def ensure_init(self):
42 if self.server_ns and self.client_ns and self.server_ip:
43 return True
44 else:
45 raise Exception(
46 "Error: Cannot Start." "iPerf object has not been initialized"
47 )
48
49 def start_iperf_server(self):
Naveen Joy7ea7ab52021-05-11 10:31:18 -070050 args = [
51 "ip",
52 "netns",
53 "exec",
54 self.server_ns,
55 self.iperf,
56 "-s",
57 "-D",
Naveen Joy7ea7ab52021-05-11 10:31:18 -070058 ]
59 args.extend(self.server_args.split())
Naveen Joye4168932022-10-04 14:22:05 -070060 args = " ".join(args)
Naveen Joy7ea7ab52021-05-11 10:31:18 -070061 try:
Naveen Joye4168932022-10-04 14:22:05 -070062 return subprocess.run(
Naveen Joy7ea7ab52021-05-11 10:31:18 -070063 args,
Naveen Joy7ea7ab52021-05-11 10:31:18 -070064 timeout=self.duration + 5,
65 encoding="utf-8",
Naveen Joye4168932022-10-04 14:22:05 -070066 shell=True,
67 stdout=subprocess.PIPE,
68 stderr=subprocess.STDOUT,
Naveen Joy7ea7ab52021-05-11 10:31:18 -070069 )
70 except subprocess.TimeoutExpired as e:
71 raise Exception("Error: Timeout expired for iPerf", e.output)
72
73 def start_iperf_client(self):
Naveen Joy7ea7ab52021-05-11 10:31:18 -070074 args = [
75 "ip",
76 "netns",
77 "exec",
78 self.client_ns,
79 self.iperf,
80 "-c",
81 self.server_ip,
82 "-t",
83 str(self.duration),
84 ]
85 args.extend(self.client_args.split())
Naveen Joye4168932022-10-04 14:22:05 -070086 args = " ".join(args)
Naveen Joy7ea7ab52021-05-11 10:31:18 -070087 try:
Naveen Joye4168932022-10-04 14:22:05 -070088 return subprocess.run(
Naveen Joy7ea7ab52021-05-11 10:31:18 -070089 args,
Naveen Joy7ea7ab52021-05-11 10:31:18 -070090 timeout=self.duration + 5,
91 encoding="utf-8",
Naveen Joye4168932022-10-04 14:22:05 -070092 capture_output=True,
93 shell=True,
Naveen Joy7ea7ab52021-05-11 10:31:18 -070094 )
95 except subprocess.TimeoutExpired as e:
96 raise Exception("Error: Timeout expired for iPerf", e.output)
97
Naveen Joye4168932022-10-04 14:22:05 -070098 def start(self, server_only=False, client_only=False):
99 """Runs iPerf.
Naveen Joy7ea7ab52021-05-11 10:31:18 -0700100
Naveen Joye4168932022-10-04 14:22:05 -0700101 Starts the iperf server daemon & runs the iperf client.
102 arguments:-
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.
106 """
107 self.ensure_init()
108 if not client_only:
109 self.start_iperf_server()
110 if not server_only:
111 result = self.start_iperf_client()
112 self.logger.debug(f"Iperf client args: {result.args}")
113 self.logger.debug(result.stdout)
114 if result.stderr:
115 self.logger.error(
116 f"Error starting Iperf Client in Namespace: {self.client_ns}"
117 )
118 self.logger.error(f"Iperf client args: {result.args}")
119 self.logger.error(f"Iperf client has errors: {result.stderr}")
120 return False
121 else:
122 return True
123
124
125## Functions to start and stop iPerf using the iPerf object
126def start_iperf(
127 ip_version,
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",
132 client_args="",
133 server_args="",
134 duration=10,
135 server_only=False,
136 client_only=False,
137 logger=None,
138):
139 """Start an iperf connection stream using the iPerf object.
140
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).
144 Parameters:
145 ip_version - 4 or 6
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
157 logger - test logger
158 """
159 if ip_version == 4:
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
165 iperf = VppIperf()
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)
174
175
176def stop_iperf():
177 args = ["pkill", "iperf"]
178 args = " ".join(args)
179 try:
180 return subprocess.run(
181 args,
182 encoding="utf-8",
183 shell=True,
184 )
185 except Exception:
186 pass
Naveen Joy7ea7ab52021-05-11 10:31:18 -0700187
188
189if __name__ == "__main__":
190 # Run iPerf using default settings
191 iperf = VppIperf()
192 iperf.client_ns = "ns1"
193 iperf.server_ns = "ns2"
194 iperf.server_ip = "10.0.0.102"
195 iperf.duration = 20
196 iperf.start()