Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | # Start an iPerf connection stream between two Linux namespaces ## |
| 4 | |
| 5 | import subprocess |
| 6 | import os |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 7 | import sys |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class 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 Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 30 | def __init__(self, server_ns=None, client_ns=None, server_ip=None, logger=None): |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 31 | 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 Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 37 | self.logger = logger |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 38 | # 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 Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 50 | args = [ |
| 51 | "ip", |
| 52 | "netns", |
| 53 | "exec", |
| 54 | self.server_ns, |
| 55 | self.iperf, |
| 56 | "-s", |
| 57 | "-D", |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 58 | ] |
| 59 | args.extend(self.server_args.split()) |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 60 | args = " ".join(args) |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 61 | try: |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 62 | return subprocess.run( |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 63 | args, |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 64 | timeout=self.duration + 5, |
| 65 | encoding="utf-8", |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 66 | shell=True, |
| 67 | stdout=subprocess.PIPE, |
| 68 | stderr=subprocess.STDOUT, |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 69 | ) |
| 70 | except subprocess.TimeoutExpired as e: |
| 71 | raise Exception("Error: Timeout expired for iPerf", e.output) |
| 72 | |
| 73 | def start_iperf_client(self): |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 74 | 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 Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 86 | args = " ".join(args) |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 87 | try: |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 88 | return subprocess.run( |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 89 | args, |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 90 | timeout=self.duration + 5, |
| 91 | encoding="utf-8", |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 92 | capture_output=True, |
| 93 | shell=True, |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 94 | ) |
| 95 | except subprocess.TimeoutExpired as e: |
| 96 | raise Exception("Error: Timeout expired for iPerf", e.output) |
| 97 | |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 98 | def start(self, server_only=False, client_only=False): |
| 99 | """Runs iPerf. |
Naveen Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 100 | |
Naveen Joy | e416893 | 2022-10-04 14:22:05 -0700 | [diff] [blame] | 101 | 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 |
| 126 | def 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 | |
| 176 | def 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 Joy | 7ea7ab5 | 2021-05-11 10:31:18 -0700 | [diff] [blame] | 187 | |
| 188 | |
| 189 | if __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() |