From nobody Mon Nov 23 04:30:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1793633501; cv=none; d=zohomail.com; s=zohoarc; b=FnWl7zdWQvVEIRoI8IEvKWGdd756DouNBCRfMDU3zJ5or7tTsGEBDdDKSe/JgRaXNuufI8+OcQ8wQeqFgR921IXdM/kaCOeic58NWYXsbtQHLbzU0yDo1IeuliStYg3dJTyJyyRCz/iJDALjsHuRkaxQwij73dLO+Neb7FqX/x8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793633501; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=5TXJkr86UMJ/vg5MXtbaydS7YW7pbsisdmPoC/lsMSs=; b=dg9IEbBHSarqkmseQDbr+NvOyjI3i7pbEPmnujZFfMmp58NRo1WQDFPczTY3743UTSlc4a3/ibMgUFfdbzWkw96FAvn/i/rzeQda3Mtyd8pzTfcG4b2R+lQMys4uzTPJgqdB5QjpmxJRC50YuXN4YL4kIZqXm/fUNK2HSp+2Weg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1793633501438734.3735195846891; Mon, 2 Nov 2026 07:31:41 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNtzy-0006It-G1; Mon, 02 Nov 2026 10:31:02 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNtzu-0006II-31 for qemu-devel@nongnu.org; Mon, 02 Nov 2026 10:30:58 -0500 Received: from forwardcorp1d.mail.yandex.net ([178.154.239.200]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNtzs-0005Ly-0o for qemu-devel@nongnu.org; Mon, 02 Nov 2026 10:30:57 -0500 Received: from mail-nwsmtp-smtp-corp-main-56 (mail-nwsmtp-smtp-corp-main-56.yp-c.yandex.net) by forwardcorp1d.mail.yandex.net (postfix) with ESMTPS id 38CA88076D; Mon, 02 Nov 2026 18:30:47 +0300 (MSK) Received: from i115954770.yandex-team.ru by mail-nwsmtp-smtp-corp-main-56 with ESMTPSA id iUpabg2Zpa60-TgJj9RbM; Mon, 02 Nov 2026 18:30:46 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1793633446; bh=5TXJkr86UMJ/vg5MXtbaydS7YW7pbsisdmPoC/lsMSs=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=gCAfGYkoSDAbw6oCLGkWJ2WgyflPgkH7gnU1olHDJkNKRE0uJFHzcR0O4LbLLEbKi okMUnatrjFJZL2X9XYlDFv/po9C1be3p+DWokfHPBnjG55iHXLgRW+Ppywy4NZsBg6 2SDVxansXxw96/xKNt7aoMQrmOzYWiZv9cGnIsjA= Authentication-Results: mail-nwsmtp-smtp-corp-main-56; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: mst@redhat.com Cc: peterx@redhat.com, vsementsov@yandex-team.ro, qemu-devel@nongnu.org, Vladimir Sementsov-Ogievskiy , Thomas Huth , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= Subject: [PATCH v2 1/2] tests/functional: skipWithoutSudo: handle missing sudo binary Date: Mon, 2 Nov 2026 18:30:41 +0300 Message-ID: <20261102153043.3795201-2-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261102153043.3795201-1-vsementsov@yandex-team.ru> References: <20261102153043.3795201-1-vsementsov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=178.154.239.200; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp1d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1793633505406154100 Content-Type: text/plain; charset="utf-8" skipWithoutSudo() runs "sudo -n /bin/true" when the decorator is evaluated, i.e. at module import time. If sudo is not installed (as in the centos and opensuse CI containers), subprocess.run() raises FileNotFoundError and the whole test module fails to load, including the asset precache step. Skip the test instead. Fixes: ac84b9ec9604 ("tests/functional: add skipWithoutSudo() decorator") Signed-off-by: Vladimir Sementsov-Ogievskiy --- tests/functional/qemu_test/decorators.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/functional/qemu_test/decorators.py b/tests/functional/qe= mu_test/decorators.py index aa135acc785..9df8d1e3000 100644 --- a/tests/functional/qemu_test/decorators.py +++ b/tests/functional/qemu_test/decorators.py @@ -184,12 +184,15 @@ def skipLockedMemoryTest(locked_memory): sudo command is not available. ''' def skipWithoutSudo(): - proc =3D subprocess.run(["sudo", "-n", "/bin/true"], - stdin=3Dsubprocess.PIPE, - stdout=3Dsubprocess.PIPE, - stderr=3Dsubprocess.STDOUT, - universal_newlines=3DTrue, - check=3DFalse) + try: + proc =3D subprocess.run(["sudo", "-n", "/bin/true"], + stdin=3Dsubprocess.PIPE, + stdout=3Dsubprocess.PIPE, + stderr=3Dsubprocess.STDOUT, + universal_newlines=3DTrue, + check=3DFalse) + except OSError as e: + return skipUnless(False, f'requires sudo: {e}') =20 return skipUnless(proc.returncode =3D=3D 0, f'requires password-less sudo access: {proc.stdout}') --=20 2.43.0 From nobody Mon Nov 23 04:30:05 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=yandex-team.ru ARC-Seal: i=1; a=rsa-sha256; t=1793633509; cv=none; d=zohomail.com; s=zohoarc; b=KuMKpJc2xcThrektxiTMV4113MJLK5jbHOtgMQsz6Fl30BDlzKo+ObhX0qahXr7An3eHBgq6csm9mLJXnMnap3PZtKSxqUmtz/cqa3MvSDxBuKQg66h87FtcKZeXVnPxiTSgSklXYv7Ax1BCniKipIGZ0C8pe6sQNagnJGCYPOc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1793633509; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=QGG0Flnwz73I+CwEnRYT6LnOQC+Dx/gNbq6rsJvx/aM=; b=E3o7ZivNolCeoWn/Y8iXFswWVJ47nc13VvEMHTIUD1pKspKNyGT4hgGVn8HBYzmnCmnz2cuMAIk5mePovSRf+jiiTp9Pq6B+IjW2GFt/Ylk3N/4fhvGdqU+NzPObSxulOyv0V2q05RZDOILoTjZOH0V2oDAEM4LdXLuDOmR6G8k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17936335090704.092150347501843; Mon, 2 Nov 2026 07:31:49 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xNu05-0006Kk-PJ; Mon, 02 Nov 2026 10:31:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNtzw-0006Il-6R for qemu-devel@nongnu.org; Mon, 02 Nov 2026 10:31:00 -0500 Received: from forwardcorp1d.mail.yandex.net ([2a02:6b8:c41:1300:1:45:d181:df01]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xNtzs-0005MF-0n for qemu-devel@nongnu.org; Mon, 02 Nov 2026 10:30:59 -0500 Received: from mail-nwsmtp-smtp-corp-main-56 (mail-nwsmtp-smtp-corp-main-56.yp-c.yandex.net) by forwardcorp1d.mail.yandex.net (postfix) with ESMTPS id AC2AE80957; Mon, 02 Nov 2026 18:30:48 +0300 (MSK) Received: from i115954770.yandex-team.ru by mail-nwsmtp-smtp-corp-main-56 with ESMTPSA id iUpabg2Zpa60-Om0zx7uP; Mon, 02 Nov 2026 18:30:47 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1793633447; bh=QGG0Flnwz73I+CwEnRYT6LnOQC+Dx/gNbq6rsJvx/aM=; h=Message-ID:Date:In-Reply-To:Cc:Subject:References:To:From; b=o19LElKCySay6KJYH6Cl7wD7fCLboZxP3V/mCgg575hoUcCIGsXNZHQYUNL6nDwYX FPWci2jIQzASOfnjQbK8a1wS0L5sTEo+zQ5Ib0e7VAwUxpIH8LOy/X2paKNuxiyoLx T4d0VqEPYKGcrkO7wlsMPv/YrRVQpaC4YJITG25M= Authentication-Results: mail-nwsmtp-smtp-corp-main-56; dkim=pass header.i=@yandex-team.ru From: Vladimir Sementsov-Ogievskiy To: mst@redhat.com Cc: peterx@redhat.com, vsementsov@yandex-team.ro, qemu-devel@nongnu.org, Vladimir Sementsov-Ogievskiy , Paolo Bonzini , Zhao Liu , Fabiano Rosas Subject: [PATCH v2 2/2] tests/functional: add test_tap_migration Date: Mon, 2 Nov 2026 18:30:42 +0300 Message-ID: <20261102153043.3795201-3-vsementsov@yandex-team.ru> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261102153043.3795201-1-vsementsov@yandex-team.ru> References: <20261102153043.3795201-1-vsementsov@yandex-team.ru> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=2a02:6b8:c41:1300:1:45:d181:df01; envelope-from=vsementsov@yandex-team.ru; helo=forwardcorp1d.mail.yandex.net X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, UNPARSEABLE_RELAY=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @yandex-team.ru) X-ZM-MESSAGEID: 1793633512952154100 Content-Type: text/plain; charset="utf-8" Add test for a new local-migration migration of virtio-net/tap, with fd passing through UNIX socket. Signed-off-by: Vladimir Sementsov-Ogievskiy Acked-by: Peter Xu --- tests/functional/x86_64/meson.build | 1 + tests/functional/x86_64/test_tap_migration.py | 425 ++++++++++++++++++ 2 files changed, 426 insertions(+) create mode 100755 tests/functional/x86_64/test_tap_migration.py diff --git a/tests/functional/x86_64/meson.build b/tests/functional/x86_64/= meson.build index bcda07620ff..a2bbc2bbd0e 100644 --- a/tests/functional/x86_64/meson.build +++ b/tests/functional/x86_64/meson.build @@ -54,4 +54,5 @@ tests_x86_64_system_thorough =3D [ 'virtio_gpu', 'rebuild_vmfd', 'vhost_vsock_cpr', + 'tap_migration', ] diff --git a/tests/functional/x86_64/test_tap_migration.py b/tests/function= al/x86_64/test_tap_migration.py new file mode 100755 index 00000000000..a34bd94fd25 --- /dev/null +++ b/tests/functional/x86_64/test_tap_migration.py @@ -0,0 +1,425 @@ +#!/usr/bin/env python3 +# +# Functional test that tests TAP local migration +# with fd passing +# +# Copyright (c) Yandex Technologies LLC, 2026 +# +# SPDX-License-Identifier: GPL-2.0-or-later + +import os +import time +import subprocess +from subprocess import run +import signal +import ctypes +import ctypes.util +import unittest +from contextlib import contextmanager, ExitStack + +from qemu_test import ( + LinuxKernelTest, + Asset, + exec_command_and_wait_for_pattern, + skipIfMissingCommands, +) +from qemu_test.decorators import skipWithoutSudo + + +GUEST_IP =3D "192.168.100.2" +GUEST_IP_MASK =3D f"{GUEST_IP}/24" +GUEST_MAC =3D "d6:0d:75:f8:0f:b7" +HOST_IP =3D "192.168.100.1" +HOST_IP_MASK =3D f"{HOST_IP}/24" +TAP_ID =3D "tap0" +TAP_ID2 =3D "tap1" +TAP_MAC =3D "e6:1d:44:b5:03:5d" +NETNS =3D f"qemu_test_ns_{os.getpid()}" + + +def ip(args, check=3DTrue) -> None: + """Run ip command with sudo""" + run(["sudo", "ip"] + args, check=3Dcheck) + + +@contextmanager +def switch_netns(netns_name): + libc =3D ctypes.CDLL(ctypes.util.find_library("c")) + netns_path =3D f"/var/run/netns/{netns_name}" + + def switch_to_fd(fd, check: bool =3D False): + """Switch to netns by file descriptor""" + sys_setns =3D 308 + clone_newnet =3D 0x40000000 + ret =3D libc.syscall(sys_setns, fd, clone_newnet) + if check and ret !=3D 0: + raise RuntimeError("syscall SETNS failed") + + with ExitStack() as stack: + original_netns_fd =3D os.open("/proc/self/ns/net", os.O_RDONLY) + stack.callback(os.close, original_netns_fd) + + ip(["netns", "add", netns_name]) + stack.callback(ip, ["netns", "del", netns_name], check=3DFalse) + + new_netns_fd =3D os.open(netns_path, os.O_RDONLY) + stack.callback(os.close, new_netns_fd) + + switch_to_fd(new_netns_fd) + stack.callback(switch_to_fd, original_netns_fd, check=3DFalse) + + yield + + +def del_tap(tap_name: str =3D TAP_ID) -> None: + ip(["tuntap", "del", tap_name, "mode", "tap", "multi_queue"], check=3D= False) + + +def init_tap(tap_name: str =3D TAP_ID, with_ip: bool =3D True) -> None: + ip(["tuntap", "add", "dev", tap_name, "mode", "tap", "multi_queue"]) + if with_ip: + ip(["link", "set", "dev", tap_name, "address", TAP_MAC]) + ip(["addr", "add", HOST_IP_MASK, "dev", tap_name]) + ip(["link", "set", tap_name, "up"]) + + +def switch_network_to_tap2() -> None: + ip(["link", "set", TAP_ID2, "down"]) + ip(["link", "set", TAP_ID, "down"]) + ip(["addr", "delete", HOST_IP_MASK, "dev", TAP_ID]) + ip(["link", "set", "dev", TAP_ID2, "address", TAP_MAC]) + ip(["addr", "add", HOST_IP_MASK, "dev", TAP_ID2]) + ip(["link", "set", TAP_ID2, "up"]) + + +def parse_ping_line(line: str) -> float: + # suspect lines like + # [1748524876.590509] 64 bytes from 94.245.155.3 \ + # (94.245.155.3): icmp_seq=3D1 ttl=3D250 time=3D101 ms + spl =3D line.split() + return float(spl[0][1:-1]) + + +def parse_ping_output(out) -> float: + lines =3D [x for x in out.split("\n") if x.startswith("[")] + + max_gap =3D 0.0 + prev_timestamp =3D None + + for line in lines: + if "no ans" not in line: + # This is a successful ping line + try: + timestamp =3D parse_ping_line(line) + if prev_timestamp is not None: + gap =3D timestamp - prev_timestamp + if gap > max_gap: + max_gap =3D gap + prev_timestamp =3D timestamp + except (IndexError, ValueError): + # Skip malformed lines + continue + + assert prev_timestamp is not None, "No successful pings found" + assert max_gap > 0.0, "Need at least two successful pings to calculate= gap" + + return max_gap + + +def wait_migration_finish(source_vm, target_vm): + migr_events =3D ( + ("MIGRATION", {"data": {"status": "completed"}}), + ("MIGRATION", {"data": {"status": "failed"}}), + ) + + source_e =3D source_vm.events_wait(migr_events)["data"] + target_e =3D target_vm.events_wait(migr_events)["data"] + + source_s =3D source_vm.cmd("query-status")["status"] + target_s =3D target_vm.cmd("query-status")["status"] + + assert ( + source_e["status"] =3D=3D "completed" + and target_e["status"] =3D=3D "completed" + and source_s =3D=3D "postmigrate" + and target_s =3D=3D "paused" + ), f"""Migration failed: + SRC status: {source_s} + SRC event: {source_e} + TGT status: {target_s} + TGT event:{target_e}""" + + +@skipIfMissingCommands("ip", "ping") +@skipWithoutSudo() +class TAPFdMigration(LinuxKernelTest): + ASSET_ALPINE_ISO =3D Asset( + ( + "https://dl-cdn.alpinelinux.org/" + "alpine/v3.22/releases/x86_64/alpine-standard-3.22.1-x86_64.is= o" + ), + "96d1b44ea1b8a5a884f193526d92edb4676054e9fa903ad2f016441a0fe13089", + ) + + @classmethod + def setUpClass(cls): + super().setUpClass() + + # The test namespace has no external connectivity. + cls.ASSET_ALPINE_ISO.fetch() + + try: + cls.netns_context =3D switch_netns(NETNS) + cls.netns_context.__enter__() + except (OSError, subprocess.CalledProcessError) as e: + raise unittest.SkipTest(f"can't switch network namespace: {e}") + + @classmethod + def tearDownClass(cls): + if hasattr(cls, "netns_context"): + cls.netns_context.__exit__(None, None, None) + super().tearDownClass() + + def setUp(self): + super().setUp() + + self.require_accelerator("kvm") + self.set_machine("q35") + + self.setup_shared_memory() + + init_tap() + + self.outer_ping_proc =3D None + + def tearDown(self): + with ExitStack() as stack: + stack.callback(super().tearDown) + + if self.shm_path: + stack.callback(lambda p: (os.unlink(p) if os.path.exists(p) + else None), + self.shm_path) + self.shm_path =3D None + + stack.callback(del_tap, TAP_ID2) + stack.callback(del_tap, TAP_ID) + + if self.outer_ping_proc: + self.stop_outer_ping() + + def start_outer_ping(self) -> None: + assert self.outer_ping_proc is None + self.outer_ping_log =3D self.scratch_file("ping.log") + with open(self.outer_ping_log, "w", encoding=3D"utf-8") as f: + self.outer_ping_proc =3D subprocess.Popen( + ["sudo", "ping", "-i", "0", "-O", "-D", GUEST_IP], + text=3DTrue, + stdout=3Df, + ) + + def stop_outer_ping(self) -> str: + assert self.outer_ping_proc + self.outer_ping_proc.send_signal(signal.SIGINT) + + self.outer_ping_proc.communicate(timeout=3D5) + self.outer_ping_proc =3D None + + with open(self.outer_ping_log, encoding=3D"utf-8") as f: + return f.read() + + def stop_ping_and_check(self): + ping_res =3D self.stop_outer_ping() + + disconnect_time =3D parse_ping_output(ping_res) + if disconnect_time > 0.5: + self.fail(f"long disconnect time: {disconnect_time}") + + def one_ping_from_guest(self, vm) -> None: + exec_command_and_wait_for_pattern( + self, + f"ping -c 1 -W 1 {HOST_IP}", + "1 packets transmitted, 1 packets received", + "1 packets transmitted, 0 packets received", + vm=3Dvm, + ) + self.wait_for_console_pattern("# ", vm=3Dvm) + + def one_ping_from_host(self) -> None: + run( + ["ping", "-c", "1", "-W", "1", GUEST_IP], + stdout=3Dsubprocess.DEVNULL, + check=3DTrue, + ) + + def setup_shared_memory(self): + self.shm_path =3D f"/dev/shm/qemu_test_{os.getpid()}" + + try: + with open(self.shm_path, "wb") as f: + f.write(b"\0" * (1024 * 1024 * 1024)) # 1GB + except OSError as e: + self.fail(f"Failed to create shared memory file: {e}") + + def prepare_vm(self, vm, shm_path, incoming): + if not vm: + vm =3D self.vm + + vm.set_console() + vm.add_args("-accel", "kvm") + vm.add_args("-device", "pcie-pci-bridge,id=3Dpci.1,bus=3Dpcie.0") + vm.add_args("-m", "1G") + vm.add_args("-net", "none") + + vm.add_args( + "-object", + f"memory-backend-file,id=3Dram0,size=3D1G,mem-path=3D{shm_path= },share=3Don", + ) + vm.add_args("-machine", "memory-backend=3Dram0") + + vm.add_args( + "-drive", + f"file=3D{self.ASSET_ALPINE_ISO.fetch()},media=3Dcdrom,format= =3Draw", + ) + + vm.add_args("-S") + + if incoming: + vm.add_args("-incoming", "defer") + + def add_virtio_net( + self, vm, vhost: bool, tap_name: str, local: bool, incoming: bool + ): + netdev_params =3D { + "id": "netdev.1", + "vhost": vhost, + "type": "tap", + "queues": 4, + "script": "no", + "downscript": "no", + "x-permit-local-migration": local, + } + + if not (local and incoming): + netdev_params["vnet_hdr"] =3D True + netdev_params["ifname"] =3D tap_name + + vm.cmd("netdev_add", netdev_params) + + vm.cmd( + "device_add", + driver=3D"virtio-net-pci", + romfile=3D"", + id=3D"vnet.1", + netdev=3D"netdev.1", + mq=3DTrue, + vectors=3D18, + bus=3D"pci.1", + mac=3DGUEST_MAC, + disable_legacy=3D"off", + ) + + def set_migration_capabilities(self, vm, local): + vm.cmd( + "migrate-set-capabilities", + { + "capabilities": [ + {"capability": "events", "state": True}, + {"capability": "x-ignore-shared", "state": True}, + ] + }, + ) + vm.cmd("migrate-set-parameters", {"local": local}) + + def setup_guest_network(self) -> None: + exec_command_and_wait_for_pattern(self, "ip addr", "# ") + exec_command_and_wait_for_pattern( + self, + f"ip addr add {GUEST_IP_MASK} dev eth0 && " + "ip link set eth0 up && echo OK", + "OK", + ) + self.wait_for_console_pattern("# ") + + def migrate(self, vm, mig_sock): + vm.cmd("migrate", uri=3Df"unix:{mig_sock}") + + def do_test_tap_fd_migration(self, vhost, local=3DTrue): + socket_dir =3D self.socket_dir() + mig_sock =3D os.path.join(socket_dir.name, "mig.sock") + + # Setup second TAP if needed + if not local: + del_tap(TAP_ID2) + init_tap(TAP_ID2, with_ip=3DFalse) + + self.prepare_vm(self.vm, self.shm_path, False) + self.vm.launch() + self.set_migration_capabilities(self.vm, local=3Dlocal) + self.add_virtio_net(self.vm, vhost, TAP_ID, local, incoming=3DFals= e) + + self.vm.cmd("cont") + self.wait_for_console_pattern("login:") + exec_command_and_wait_for_pattern(self, "root", "# ") + + self.setup_guest_network() + + self.one_ping_from_guest(self.vm) + self.one_ping_from_host() + self.start_outer_ping() + + # Get some successful pings before migration + time.sleep(0.5) + + target_vm =3D self.get_vm(name=3D"target") + self.prepare_vm(target_vm, self.shm_path, True) + + target_vm.launch() + if not local: + tap_name =3D TAP_ID2 + else: + tap_name =3D TAP_ID + self.set_migration_capabilities(target_vm, local=3Dlocal) + self.add_virtio_net(target_vm, vhost, tap_name, local, incoming=3D= True) + + target_vm.cmd("migrate-incoming", {"uri": f"unix:{mig_sock}"}) + + self.log.info("Starting migration") + self.migrate(self.vm, mig_sock) + + self.log.info("Waiting for migration completion") + wait_migration_finish(self.vm, target_vm) + + # Switch network to tap1 if not using local-migration + if not local: + switch_network_to_tap2() + + target_vm.cmd("cont") + + self.log.info("Verifying PING on target VM after migration") + + # Keep the stopped source alive while checking the target. + time.sleep(0.3) + self.stop_ping_and_check() + + self.one_ping_from_guest(target_vm) + self.one_ping_from_host() + + self.vm.shutdown() + target_vm.shutdown() + + def test_tap_fd_migration(self): + self.do_test_tap_fd_migration(False) + + def test_tap_fd_migration_vhost(self): + self.do_test_tap_fd_migration(True) + + def test_tap_new_tap_migration(self): + self.do_test_tap_fd_migration(False, local=3DFalse) + + def test_tap_new_tap_migration_vhost(self): + self.do_test_tap_fd_migration(True, local=3DFalse) + + +if __name__ =3D=3D "__main__": + LinuxKernelTest.main() --=20 2.43.0