Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 1 | # osmo_gsm_tester: specifics for running an iperf3 client and server |
| 2 | # |
| 3 | # Copyright (C) 2018 by sysmocom - s.f.m.c. GmbH |
| 4 | # |
| 5 | # Author: Pau Espin Pedrol <pespin@sysmocom.de> |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as |
| 9 | # published by the Free Software Foundation, either version 3 of the |
| 10 | # License, or (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | import os |
| 21 | import json |
| 22 | |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 23 | from . import log, util, process, pcap_recorder, run_node, remote |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 24 | |
Pau Espin Pedrol | 64f0b1b | 2018-11-09 17:44:10 +0100 | [diff] [blame] | 25 | def iperf3_result_to_json(file): |
| 26 | with open(file) as f: |
| 27 | # Sometimes iperf3 provides 2 dictionaries, the 2nd one being an error about being interrupted (by us). |
| 28 | # json parser doesn't support (raises exception) parsing several dictionaries at a time (not a valid json object). |
| 29 | # We are only interested in the first dictionary, the regular results one: |
| 30 | d = f.read().split("\n}\n")[0] + "\n}\n" |
| 31 | data = json.loads(d) |
| 32 | return data |
| 33 | |
| 34 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 35 | class IPerf3Server(log.Origin): |
| 36 | |
Pau Espin Pedrol | cf6a360 | 2018-11-14 16:38:19 +0100 | [diff] [blame] | 37 | DEFAULT_SRV_PORT = 5003 |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 38 | LOGFILE = 'iperf3_srv.json' |
| 39 | REMOTE_DIR = '/tmp' |
Pau Espin Pedrol | cf6a360 | 2018-11-14 16:38:19 +0100 | [diff] [blame] | 40 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 41 | def __init__(self, suite_run, ip_address): |
| 42 | super().__init__(log.C_RUN, 'iperf3-srv_%s' % ip_address.get('addr')) |
| 43 | self.run_dir = None |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 44 | self.process = None |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 45 | self._run_node = None |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 46 | self.suite_run = suite_run |
| 47 | self.ip_address = ip_address |
Pau Espin Pedrol | cf6a360 | 2018-11-14 16:38:19 +0100 | [diff] [blame] | 48 | self._port = IPerf3Server.DEFAULT_SRV_PORT |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 49 | self.log_file = None |
| 50 | self.rem_host = None |
| 51 | self.remote_log_file = None |
| 52 | self.log_copied = False |
| 53 | |
| 54 | def cleanup(self): |
| 55 | if self.process is None: |
| 56 | return |
| 57 | if self.runs_locally(): |
| 58 | return |
| 59 | # copy back files (may not exist, for instance if there was an early error of process): |
| 60 | try: |
| 61 | self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file) |
| 62 | except Exception as e: |
| 63 | self.log(repr(e)) |
| 64 | |
| 65 | def runs_locally(self): |
| 66 | locally = not self._run_node or self._run_node.is_local() |
| 67 | return locally |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 68 | |
| 69 | def start(self): |
| 70 | self.log('Starting iperf3-srv') |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 71 | self.log_copied = False |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 72 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 73 | self.log_file = self.run_dir.new_file(IPerf3Server.LOGFILE) |
| 74 | if self.runs_locally(): |
| 75 | self.start_locally() |
| 76 | else: |
| 77 | self.start_remotely() |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 78 | |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 79 | def start_remotely(self): |
| 80 | self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr()) |
| 81 | remote_prefix_dir = util.Dir(IPerf3Server.REMOTE_DIR) |
| 82 | remote_run_dir = util.Dir(remote_prefix_dir.child('srv-' + str(self))) |
| 83 | self.remote_log_file = remote_run_dir.child(IPerf3Server.LOGFILE) |
| 84 | |
| 85 | self.rem_host.recreate_remote_dir(remote_run_dir) |
| 86 | |
| 87 | args = ('iperf3', '-s', '-B', self.addr(), |
| 88 | '-p', str(self._port), '-J', |
| 89 | '--logfile', self.remote_log_file) |
| 90 | self.process = self.rem_host.RemoteProcess(self.name(), args) |
| 91 | self.suite_run.remember_to_stop(self.process) |
| 92 | self.process.launch() |
| 93 | |
| 94 | def start_locally(self): |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 95 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
| 96 | 'host %s and port not 22' % self.addr()) |
| 97 | |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 98 | args = ('iperf3', '-s', '-B', self.addr(), |
| 99 | '-p', str(self._port), '-J', |
| 100 | '--logfile', os.path.abspath(self.log_file)) |
| 101 | |
| 102 | self.process = process.Process(self.name(), self.run_dir, args, env={}) |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 103 | self.suite_run.remember_to_stop(self.process) |
| 104 | self.process.launch() |
| 105 | |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 106 | def set_run_node(self, run_node): |
| 107 | self._run_node = run_node |
| 108 | |
Pau Espin Pedrol | cf6a360 | 2018-11-14 16:38:19 +0100 | [diff] [blame] | 109 | def set_port(self, port): |
| 110 | self._port = port |
| 111 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 112 | def stop(self): |
| 113 | self.suite_run.stop_process(self.process) |
| 114 | |
| 115 | def get_results(self): |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 116 | if not self.runs_locally() and not self.log_copied: |
| 117 | self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file) |
| 118 | self.log_copied = True |
Pau Espin Pedrol | 64f0b1b | 2018-11-09 17:44:10 +0100 | [diff] [blame] | 119 | return iperf3_result_to_json(self.log_file) |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 120 | |
| 121 | def addr(self): |
| 122 | return self.ip_address.get('addr') |
| 123 | |
| 124 | def port(self): |
| 125 | return self._port |
| 126 | |
Pau Espin Pedrol | 0df6317 | 2018-11-14 16:39:30 +0100 | [diff] [blame] | 127 | def __str__(self): |
| 128 | return "%s:%u" %(self.addr(), self.port()) |
| 129 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 130 | def running(self): |
| 131 | return not self.process.terminated() |
| 132 | |
| 133 | def create_client(self): |
| 134 | return IPerf3Client(self.suite_run, self) |
| 135 | |
| 136 | class IPerf3Client(log.Origin): |
| 137 | |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 138 | REMOTE_DIR = '/tmp' |
| 139 | LOGFILE = 'iperf3_cli.json' |
| 140 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 141 | def __init__(self, suite_run, iperf3srv): |
| 142 | super().__init__(log.C_RUN, 'iperf3-cli_%s' % iperf3srv.addr()) |
| 143 | self.run_dir = None |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 144 | self.process = None |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 145 | self._run_node = None |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 146 | self.server = iperf3srv |
| 147 | self.suite_run = suite_run |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 148 | self.log_file = None |
| 149 | self.rem_host = None |
| 150 | self.remote_log_file = None |
| 151 | self.log_copied = False |
| 152 | |
| 153 | def runs_locally(self): |
| 154 | locally = not self._run_node or self._run_node.is_local() |
| 155 | return locally |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 156 | |
Pau Espin Pedrol | 0fc7437 | 2018-11-14 16:44:57 +0100 | [diff] [blame] | 157 | def prepare_test_proc(self, netns=None): |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 158 | self.log('Starting iperf3-client connecting to %s:%d' % (self.server.addr(), self.server.port())) |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 159 | self.log_copied = False |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 160 | self.run_dir = util.Dir(self.suite_run.get_test_run_dir().new_dir(self.name())) |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 161 | self.log_file = self.run_dir.new_file(IPerf3Client.LOGFILE) |
| 162 | if self.runs_locally(): |
Pau Espin Pedrol | 9c38328 | 2020-02-20 14:26:33 +0100 | [diff] [blame^] | 163 | return self.prepare_test_proc_locally(netns) |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 164 | else: |
Pau Espin Pedrol | 9c38328 | 2020-02-20 14:26:33 +0100 | [diff] [blame^] | 165 | return self.prepare_test_proc_remotely(netns) |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 166 | |
Pau Espin Pedrol | 9c38328 | 2020-02-20 14:26:33 +0100 | [diff] [blame^] | 167 | def prepare_test_proc_remotely(self, netns): |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 168 | self.rem_host = remote.RemoteHost(self.run_dir, self._run_node.ssh_user(), self._run_node.ssh_addr()) |
| 169 | |
| 170 | remote_prefix_dir = util.Dir(IPerf3Client.REMOTE_DIR) |
| 171 | remote_run_dir = util.Dir(remote_prefix_dir.child('cli-' + str(self))) |
| 172 | self.remote_log_file = remote_run_dir.child(IPerf3Client.LOGFILE) |
| 173 | |
| 174 | self.rem_host.recreate_remote_dir(remote_run_dir) |
| 175 | |
| 176 | popen_args = ('iperf3', '-c', self.server.addr(), |
| 177 | '-p', str(self.server.port()), '-J', |
| 178 | '--logfile', self.remote_log_file) |
| 179 | if netns: |
| 180 | self.process = self.rem_host.RemoteNetNSProcess(self.name(), netns, popen_args, env={}) |
| 181 | else: |
| 182 | self.process = self.rem_host.RemoteProcess(self.name(), popen_args, env={}) |
| 183 | return self.process |
| 184 | |
Pau Espin Pedrol | 9c38328 | 2020-02-20 14:26:33 +0100 | [diff] [blame^] | 185 | def prepare_test_proc_locally(self, netns): |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 186 | pcap_recorder.PcapRecorder(self.suite_run, self.run_dir.new_dir('pcap'), None, |
| 187 | 'host %s and port not 22' % self.server.addr(), netns) |
| 188 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 189 | popen_args = ('iperf3', '-c', self.server.addr(), |
| 190 | '-p', str(self.server.port()), '-J', |
| 191 | '--logfile', os.path.abspath(self.log_file)) |
| 192 | if netns: |
| 193 | self.process = process.NetNSProcess(self.name(), self.run_dir, netns, popen_args, env={}) |
| 194 | else: |
| 195 | self.process = process.Process(self.name(), self.run_dir, popen_args, env={}) |
Pau Espin Pedrol | 0fc7437 | 2018-11-14 16:44:57 +0100 | [diff] [blame] | 196 | return self.process |
| 197 | |
| 198 | def run_test_sync(self, netns=None): |
| 199 | self.prepare_test_proc(netns) |
Pau Espin Pedrol | 79df739 | 2018-11-12 18:15:30 +0100 | [diff] [blame] | 200 | self.process.launch_sync() |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 201 | return self.get_results() |
| 202 | |
| 203 | def get_results(self): |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 204 | if not self.runs_locally() and not self.log_copied: |
| 205 | self.rem_host.scpfrom('scp-back-log', self.remote_log_file, self.log_file) |
| 206 | self.log_copied = True |
Pau Espin Pedrol | 64f0b1b | 2018-11-09 17:44:10 +0100 | [diff] [blame] | 207 | return iperf3_result_to_json(self.log_file) |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 208 | |
Pau Espin Pedrol | c852ad8 | 2020-02-11 17:43:58 +0100 | [diff] [blame] | 209 | def set_run_node(self, run_node): |
| 210 | self._run_node = run_node |
| 211 | |
| 212 | def __str__(self): |
| 213 | # FIXME: somehow differentiate between several clients connected to same server? |
| 214 | return "%s:%u" %(self.server.addr(), self.server.port()) |
| 215 | |
Pau Espin Pedrol | 8a72586 | 2018-10-26 15:54:28 +0200 | [diff] [blame] | 216 | # vim: expandtab tabstop=4 shiftwidth=4 |