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 |
| 7 | |
| 8 | |
| 9 | class VppIperf: |
| 10 | """ "Create an iPerf connection stream between two namespaces. |
| 11 | |
| 12 | Usage: |
| 13 | iperf = VppIperf() # Create the iPerf Object |
| 14 | iperf.client_ns = 'ns1' # Client Namespace |
| 15 | iperf.server_ns = 'ns2' # Server Namespace |
| 16 | iperf.server_ip = '10.0.0.102' # Server IP Address |
| 17 | iperf.start() # Start the connection stream |
| 18 | |
| 19 | Optional: |
| 20 | iperf.duration = 15 # Time to transmit for in seconds (Default=10) |
| 21 | |
| 22 | ## Optionally set any iperf client & server args |
| 23 | Example: |
| 24 | # Run 4 parallel streams, write to logfile & bind to port 5202 |
| 25 | iperf.client_args='-P 4 --logfile /tmp/vpp-vm-tests/vpp_iperf.log -p 5202' |
| 26 | iperf.server_args='-p 5202' |
| 27 | """ |
| 28 | |
| 29 | def __init__(self, server_ns=None, client_ns=None, server_ip=None): |
| 30 | self.server_ns = server_ns |
| 31 | self.client_ns = client_ns |
| 32 | self.server_ip = server_ip |
| 33 | self.duration = 10 |
| 34 | self.client_args = "" |
| 35 | self.server_args = "" |
| 36 | # Set the iperf executable |
| 37 | self.iperf = os.path.join(os.getenv("TEST_DATA_DIR") or "/", "usr/bin/iperf") |
| 38 | |
| 39 | def ensure_init(self): |
| 40 | if self.server_ns and self.client_ns and self.server_ip: |
| 41 | return True |
| 42 | else: |
| 43 | raise Exception( |
| 44 | "Error: Cannot Start." "iPerf object has not been initialized" |
| 45 | ) |
| 46 | |
| 47 | def start_iperf_server(self): |
| 48 | print("Starting iPerf Server Daemon in Namespace ", self.server_ns) |
| 49 | args = [ |
| 50 | "ip", |
| 51 | "netns", |
| 52 | "exec", |
| 53 | self.server_ns, |
| 54 | self.iperf, |
| 55 | "-s", |
| 56 | "-D", |
| 57 | "-B", |
| 58 | self.server_ip, |
| 59 | ] |
| 60 | args.extend(self.server_args.split()) |
| 61 | try: |
| 62 | subprocess.run( |
| 63 | args, |
| 64 | stderr=subprocess.STDOUT, |
| 65 | timeout=self.duration + 5, |
| 66 | encoding="utf-8", |
| 67 | ) |
| 68 | except subprocess.TimeoutExpired as e: |
| 69 | raise Exception("Error: Timeout expired for iPerf", e.output) |
| 70 | |
| 71 | def start_iperf_client(self): |
| 72 | print("Starting iPerf Client in Namespace ", self.client_ns) |
| 73 | args = [ |
| 74 | "ip", |
| 75 | "netns", |
| 76 | "exec", |
| 77 | self.client_ns, |
| 78 | self.iperf, |
| 79 | "-c", |
| 80 | self.server_ip, |
| 81 | "-t", |
| 82 | str(self.duration), |
| 83 | ] |
| 84 | args.extend(self.client_args.split()) |
| 85 | try: |
| 86 | subprocess.run( |
| 87 | args, |
| 88 | stderr=subprocess.STDOUT, |
| 89 | timeout=self.duration + 5, |
| 90 | encoding="utf-8", |
| 91 | ) |
| 92 | except subprocess.TimeoutExpired as e: |
| 93 | raise Exception("Error: Timeout expired for iPerf", e.output) |
| 94 | |
| 95 | def start(self): |
| 96 | """Run iPerf and return True if successful""" |
| 97 | self.ensure_init() |
| 98 | try: |
| 99 | self.start_iperf_server() |
| 100 | except Exception as e: |
| 101 | subprocess.run(["pkill", "iperf"]) |
| 102 | raise Exception("Error starting iPerf Server", e) |
| 103 | |
| 104 | try: |
| 105 | self.start_iperf_client() |
| 106 | except Exception as e: |
| 107 | raise Exception("Error starting iPerf Client", e) |
| 108 | subprocess.run(["pkill", "iperf"]) |
| 109 | |
| 110 | |
| 111 | if __name__ == "__main__": |
| 112 | # Run iPerf using default settings |
| 113 | iperf = VppIperf() |
| 114 | iperf.client_ns = "ns1" |
| 115 | iperf.server_ns = "ns2" |
| 116 | iperf.server_ip = "10.0.0.102" |
| 117 | iperf.duration = 20 |
| 118 | iperf.start() |