|
| 1 | +import aexpect |
| 2 | +import os |
| 3 | +import re |
| 4 | +import logging as log |
| 5 | + |
| 6 | +from avocado.utils import process |
| 7 | + |
| 8 | +from virttest import virsh |
| 9 | +from virttest import utils_misc |
| 10 | +from virttest import utils_net |
| 11 | +from virttest import utils_package |
| 12 | +from virttest import data_dir |
| 13 | +from virttest import remote |
| 14 | + |
| 15 | +from provider.virtual_network import utils_win |
| 16 | + |
| 17 | + |
| 18 | +logging = log.getLogger('avocado.' + __name__) |
| 19 | + |
| 20 | + |
| 21 | +def run(test, params, env): |
| 22 | + """ |
| 23 | + Test UDP packet transfer from VM to remote host during migration. |
| 24 | + |
| 25 | + 1. Setup VM and remote host environment |
| 26 | + 2. Install and configure netperf on remote host and VM |
| 27 | + 3. Start packet capture on remote host |
| 28 | + 4. Run UDP transfer test from VM to remote host |
| 29 | + 5. Verify packets are captured correctly |
| 30 | + 6. Clean up environment |
| 31 | + """ |
| 32 | + |
| 33 | + def disable_firewall(session, params, guest_os_type="linux"): |
| 34 | + """ |
| 35 | + Disable firewall on the target system. |
| 36 | +
|
| 37 | + :param session: Session object for executing commands |
| 38 | + :param params: Params object |
| 39 | + :param guest_os_type: OS type ("linux" or "windows") |
| 40 | + """ |
| 41 | + firewall_cmd = params.get("firewall_cmd") |
| 42 | + |
| 43 | + if guest_os_type == "linux": |
| 44 | + status = session.cmd_status(firewall_cmd) |
| 45 | + if status != 0: |
| 46 | + logging.warning("Failed to disable firewall or already disabled") |
| 47 | + else: |
| 48 | + output = session.cmd_output(firewall_cmd) |
| 49 | + test.log.debug("Windows firewall disabled: %s", output) |
| 50 | + |
| 51 | + |
| 52 | + def transfer_netperf(host_session, guest_session, guest_os_type, src_guest_ip, src_host_ip, password, netperf_install_path='C:\\Program Files'): |
| 53 | + """ |
| 54 | + Transfer and install netperf using tp-libvirt session methods. |
| 55 | +
|
| 56 | + :param host_session: Host session object |
| 57 | + :param guest_session: Guest session object |
| 58 | + :param guest_os_type: Guest OS type ("linux" or "windows") |
| 59 | + :param src_guest_ip: Source guest IP address |
| 60 | + :param src_host_ip: Source host IP address |
| 61 | + :param password: Password for authentication |
| 62 | + :param netperf_install_path: Installation path for Windows netperf |
| 63 | + """ |
| 64 | + if guest_os_type == 'linux': |
| 65 | + logging.info('Transfer netperf tar to guest') |
| 66 | + list_cmd = 'ls -ld /home/netperf-2.7.1' |
| 67 | + status, output = guest_session.cmd_status_output(list_cmd) |
| 68 | + if r'No such file or directory' not in output: |
| 69 | + guest_session.cmd('rm -rf /home/netperf-2.7.1') |
| 70 | + |
| 71 | + host_session.cmd('scp %s root@%s:/home' % (netperf_linux_path, src_guest_ip)) |
| 72 | + |
| 73 | + logging.info('Install netperf in guest firstly') |
| 74 | + install_cmd = ('tar jxf /home/netperf-2.7.1.tar.bz2 -C /home/ && ' |
| 75 | + 'cd /home/netperf-2.7.1 && ./autogen.sh && ' |
| 76 | + './configure && make && make install') |
| 77 | + guest_session.cmd(install_cmd, timeout=600) |
| 78 | + |
| 79 | + status, output = guest_session.cmd_status_output(list_cmd) |
| 80 | + if r'No such file or directory' in output: |
| 81 | + test.fail('Fail to install netperf in guest') |
| 82 | + else: |
| 83 | + utils_win.pscp_file(guest_session, netperf_windows_path, |
| 84 | + netperf_install_path, src_host_ip, 'root', password) |
| 85 | + check_installed_cmd = 'dir "%s"|findstr /I netperf' % netperf_install_path |
| 86 | + output = guest_session.cmd_output(check_installed_cmd) |
| 87 | + if 'netperf' in output: |
| 88 | + logging.info('Success to transfer netperf.exe from ' |
| 89 | + 'src host to guest') |
| 90 | + else: |
| 91 | + test.fail('Fail to transfer netperf.exe from ' |
| 92 | + 'src host to guest') |
| 93 | + |
| 94 | + |
| 95 | + def run_netserver(host_session): |
| 96 | + """ |
| 97 | + Install and run netserver. |
| 98 | +
|
| 99 | + :param host_session: Host session object for executing commands |
| 100 | + """ |
| 101 | + logging.info('Install netserver on host firstly') |
| 102 | + list_cmd = 'ls -ld /home/netperf-2.7.1' |
| 103 | + if r'No such file or directory' not in \ |
| 104 | + host_session.cmd_output(list_cmd): |
| 105 | + host_session.cmd_output('rm -rf /home/netperf-2.7.1') |
| 106 | + install_cmd = 'tar jxf %s -C /home/ && ' \ |
| 107 | + 'cd /home/netperf-2.7.1 && ./autogen.sh && ' \ |
| 108 | + './configure && make && make install' % netperf_linux_path |
| 109 | + host_session.cmd_output(cmd=install_cmd) |
| 110 | + if r'No such file or directory' in host_session.cmd_output( |
| 111 | + list_cmd): |
| 112 | + test.fail('Fail to install netperf on host') |
| 113 | + o = host_session.cmd_output('netstat -anp |grep 12865') |
| 114 | + if o: |
| 115 | + used_pid = o.split('LISTEN')[1].strip().split('/')[0] |
| 116 | + host_session.cmd_output('kill -9 %s' % used_pid) |
| 117 | + output = host_session.cmd_output('netserver') |
| 118 | + else: |
| 119 | + output = host_session.cmd_output('netserver') |
| 120 | + if re.search(r'libsctp', output): |
| 121 | + host_session.cmd_output('yum install -y libsctp*') |
| 122 | + output = host_session.cmd_output('netserver') |
| 123 | + if 'netserver' not in host_session.cmd_output( |
| 124 | + 'pgrep -xl netserver') or ('Starting netserver' not in output): |
| 125 | + test.fail("Fail to start netserver") |
| 126 | + |
| 127 | + |
| 128 | + def run_netperf(guest_session, dst_host_ip, guest_os_type, packet_size, netperf_install_path='C:\\Program Files'): |
| 129 | + """ |
| 130 | + Run netperf UDP test using tp-libvirt session methods. |
| 131 | +
|
| 132 | + :param guest_session: Guest session object |
| 133 | + :param dst_host_ip: Destination host IP address |
| 134 | + :param guest_os_type: Guest OS type ("linux" or "windows") |
| 135 | + :param packet_size: UDP packet size for netperf test |
| 136 | + :param netperf_install_path: Installation path for Windows netperf |
| 137 | + :return: netperf log filename |
| 138 | + """ |
| 139 | + netperf_log = 'netperf_log_%s' % utils_misc.generate_random_string(6) |
| 140 | + if guest_os_type == 'linux': |
| 141 | + guest_session.cmd('cd /home') |
| 142 | + guest_session.cmd('netperf -H %s -t UDP_STREAM -- -m %s > %s &' % |
| 143 | + (dst_host_ip, packet_size, netperf_log)) |
| 144 | + else: |
| 145 | + guest_session.cmd('cd %s' % netperf_install_path) |
| 146 | + guest_session.cmd('netperf.exe -H %s -t UDP_STREAM -- -m %s > %s &' % |
| 147 | + (dst_host_ip, packet_size, netperf_log)) |
| 148 | + return netperf_log |
| 149 | + |
| 150 | + |
| 151 | + def check_netperf_log(guest_session, netperf_log, guest_os_type, packet_size, netperf_install_path='C:\\Program Files'): |
| 152 | + """ |
| 153 | + Check netperf log results using tp-libvirt session methods. |
| 154 | +
|
| 155 | + :param guest_session: Guest session object |
| 156 | + :param netperf_log: Netperf log filename to check |
| 157 | + :param guest_os_type: Guest OS type ("linux" or "windows") |
| 158 | + :param packet_size: Expected UDP packet size in log |
| 159 | + :param netperf_install_path: Installation path for Windows netperf |
| 160 | + :return: netperf log filename |
| 161 | + """ |
| 162 | + if guest_os_type == 'linux': |
| 163 | + if utils_misc.wait_for( |
| 164 | + lambda: 'netperf' not in guest_session.cmd_output('pgrep -xl netperf'), 120, step=3.0): |
| 165 | + logging.info('Finish to execute netperf in guest') |
| 166 | + else: |
| 167 | + test.fail('Timeout to execute netperf in guest under 120s') |
| 168 | + else: |
| 169 | + cmd = 'tasklist /FI "imagename eq netperf.exe"' |
| 170 | + guest_session.cmd('cd %s' % netperf_install_path) |
| 171 | + if utils_misc.wait_for(lambda: not re.search( |
| 172 | + r'netperf.exe', guest_session.cmd_output(cmd)), 120, step=3.0): |
| 173 | + logging.info('Finish to execute netperf in guest') |
| 174 | + else: |
| 175 | + test.fail('Timeout to execute netperf in guest under 120s') |
| 176 | + data_match = 'MIGRATED UDP STREAM TEST from' |
| 177 | + viewlog_cmd = 'cat /home/%s' % netperf_log |
| 178 | + if guest_os_type == 'windows': |
| 179 | + viewlog_cmd = 'type %s' % netperf_log |
| 180 | + output = guest_session.cmd_output(viewlog_cmd) |
| 181 | + if data_match and str(packet_size) in output: |
| 182 | + logging.info('The log of netperf is right') |
| 183 | + else: |
| 184 | + test.fail("The log of netperf isn't right") |
| 185 | + return netperf_log |
| 186 | + |
| 187 | + |
| 188 | + vm_name = params.get("main_vm") |
| 189 | + vm = env.get_vm(vm_name) |
| 190 | + netperf_linux_path = os.path.join(os.path.dirname(__file__), params.get('netperf_linux')) |
| 191 | + netperf_windows_path = os.path.join(os.path.dirname(__file__), params.get('netperf_windows')) |
| 192 | + |
| 193 | + # Migration parameters |
| 194 | + remote_host = params.get("remote_host") |
| 195 | + remote_user = params.get("remote_user", "root") |
| 196 | + remote_passwd = params.get("remote_passwd") |
| 197 | + guest_os_type = params.get("guest_os_type", "linux") |
| 198 | + packet_size = params.get("udp_packet_size") |
| 199 | + tcpdump_log = "/tmp/udp_capture.log" |
| 200 | + local_host_session = aexpect.ShellSession('ls') |
| 201 | + |
| 202 | + try: |
| 203 | + test.log.debug("TEST_STEP1: Boot up a guest on src host") |
| 204 | + if not vm.is_alive(): |
| 205 | + vm.start() |
| 206 | + vm_session = vm.wait_for_login() |
| 207 | + |
| 208 | + vm_ip = vm.get_address() |
| 209 | + logging.info("VM IP: %s", vm_ip) |
| 210 | + logging.info("Remote host: %s", remote_host) |
| 211 | + remote_session = remote.remote_login("ssh", remote_host, "22", |
| 212 | + remote_user, remote_passwd, r"[\#\$]\s*$") |
| 213 | + |
| 214 | + # Install and run netserver on remote host |
| 215 | + test.log.debug("TEST_STEP2: Start netperf server on remote host") |
| 216 | + # Setup SSH keys for Linux guests |
| 217 | + if guest_os_type == 'linux': |
| 218 | + password = params.get('guest_passwd', 'kvmautotest') |
| 219 | + remote_session.cmd_output('sshpass -p %s ssh-copy-id -o ' |
| 220 | + '"StrictHostKeyChecking no" -i ' |
| 221 | + '/root/.ssh/id_rsa.pub root@%s' % (password, vm_ip)) |
| 222 | + run_netserver(remote_session) |
| 223 | + |
| 224 | + # Start packet capture on remote host |
| 225 | + test.log.debug("TEST_STEP3: Capture the packet from guest") |
| 226 | + if not utils_package.package_install('tcpdump', session=remote_session): |
| 227 | + test.fail("tcpdump package install failed") |
| 228 | + tcpdump_log_file = os.path.join(data_dir.get_tmp_dir(), 'UDP_migration_tcpdump.log') |
| 229 | + remote_session.sendline('tcpdump -n udp and src %s > %s 2>&1' |
| 230 | + % (vm_ip, tcpdump_log_file)) |
| 231 | + |
| 232 | + |
| 233 | + test.log.debug("TEST_STEP4: Run netperf client command in the guest") |
| 234 | + # Stop firewall for guest |
| 235 | + if guest_os_type == 'linux': |
| 236 | + process.run('sshpass -p %s ssh-copy-id -o "StrictHostKeyChecking no" -i ' |
| 237 | + '/root/.ssh/id_rsa.pub root@%s' % |
| 238 | + (password, vm_ip)) |
| 239 | + disable_firewall(vm_session, params, guest_os_type) |
| 240 | + disable_firewall(remote_session, params, "linux") |
| 241 | + |
| 242 | + # Transfer and install netperf in guest via refactored function |
| 243 | + password = params.get('guest_passwd', 'kvmautotest') |
| 244 | + transfer_netperf(local_host_session, vm_session, guest_os_type, vm_ip, remote_host, password) |
| 245 | + |
| 246 | + try: |
| 247 | + netperf_log = run_netperf(vm_session, remote_host, guest_os_type, packet_size) |
| 248 | + check_netperf_log(vm_session, netperf_log, guest_os_type, packet_size) |
| 249 | + |
| 250 | + finally: |
| 251 | + if guest_os_type == 'linux': |
| 252 | + vm_session.sendline('rm -rf /home/netperf_log*') |
| 253 | + else: |
| 254 | + vm_session.sendline('del netperf_log*') |
| 255 | + |
| 256 | + test.log.debug("TEST_STEP5: Verify packet capture") |
| 257 | + utils_misc.wait_for(lambda: True, 5) # Wait for packets to be written |
| 258 | + with open(tcpdump_log_file) as f: |
| 259 | + for line in f: |
| 260 | + logging.info(line.strip()) |
| 261 | + if re.search('IP %s\\.\\d+ > %s\\.\\d+: UDP, length %s' |
| 262 | + % (vm_ip, remote_host, packet_size), line): |
| 263 | + logging.info('Captured packet: %s on the remote host ' |
| 264 | + 'matches the expectation' % line.strip()) |
| 265 | + break |
| 266 | + else: |
| 267 | + test.fail('Captured packet on the remote host does not' |
| 268 | + ' match the expectation') |
| 269 | + |
| 270 | + test.log.debug("UDP transfer test completed successfully") |
| 271 | + |
| 272 | + finally: |
| 273 | + try: |
| 274 | + if 'vm_session' in locals(): |
| 275 | + if guest_os_type == 'linux': |
| 276 | + vm_session.cmd_status('rm -rf /home/netperf_log*') |
| 277 | + else: |
| 278 | + vm_session.cmd_status('del netperf_log*') |
| 279 | + except Exception as e: |
| 280 | + logging.warning("Failed to cleanup netperf logs: %s", e) |
| 281 | + |
| 282 | + # Cleanup |
| 283 | + try: |
| 284 | + if 'remote_session' in locals(): |
| 285 | + # Stop tcpdump |
| 286 | + remote_session.cmd_status("pkill tcpdump") |
| 287 | + # Stop netserver |
| 288 | + remote_session.cmd_status("pkill netserver") |
| 289 | + # Remove log file |
| 290 | + remote_session.cmd_status(f"rm -f {tcpdump_log}") |
| 291 | + remote_session.cmd_status(f"rm -f {tcpdump_log_file}") |
| 292 | + remote_session.close() |
| 293 | + except Exception as e: |
| 294 | + logging.warning("Cleanup failed: %s", e) |
| 295 | + |
| 296 | + try: |
| 297 | + if 'vm_session' in locals(): |
| 298 | + vm_session.close() |
| 299 | + except Exception as e: |
| 300 | + logging.warning("VM session cleanup failed: %s", e) |
0 commit comments