Skip to contentSkip to conversation

virtio-net: live-TAP local migration

PATCH Superseded Vladimir Sementsov-Ogievskiy 4 participants 17 messages Feb 19 – Feb 27

Subsystems (2)

v11 of 21 Superseded Latest is v21 →

lore
Share & tools
lore
#0 lore raw

v11 cover virtio-net: live-TAP local migration

Hi all!

Here is a new migration parameter "local", which allows to enable local migration of TAP virtio-net backend (and maybe other devices and backends in future), including its properties and open fds.

With this new option, management software doesn't need to initialize new TAP and do a switch to it. Nothing should be done around virtio-net in local migration: it just migrates and continues to use same TAP device. So we avoid extra logic in management software, extra allocations in kernel (for new TAP), and corresponding extra delay in migration downtime.

v11: rebranding "backend-transfer" -> "local"
06: require script=no and downscript=no
instead of requiring absent options,
for future compatibility

Vladimir Sementsov-Ogievskiy (8):
net/tap: move vhost-net open() calls to tap_parse_vhost_fds() net/tap: move vhost initialization to tap_setup_vhost()
qapi: add local migration parameter
net: introduce vmstate_net_peer_backend
virtio-net: support local migration of backend net/tap: support local migration with virtio-net tests/functional: add skipWithoutSudo() decorator tests/functional: add test_tap_migration

 hw/core/machine.c                             |   1 +
 hw/net/virtio-net.c                           | 137 +++++-
 include/hw/virtio/virtio-net.h                |   2 +
 include/migration/misc.h                      |   2 +
 include/net/net.h                             |   6 +
 migration/options.c                           |  18 +-
 net/net.c                                     |  47 ++
 net/tap.c                                     | 189 +++++++--
 qapi/migration.json                           |  14 +-
 qapi/net.json                                 |   6 +-
 tests/functional/qemu_test/decorators.py      |  16 +
 tests/functional/x86_64/meson.build           |   1 +
 tests/functional/x86_64/test_tap_migration.py | 401 ++++++++++++++++++
 13 files changed, 795 insertions(+), 45 deletions(-)
 create mode 100755 tests/functional/x86_64/test_tap_migration.py
Signature · 1 line
2.52.0
#1 lore raw

PATCH v11 1/8 net/tap: move vhost-net open() calls to tap_parse_vhost_fds()

1. Simplify code path: get vhostfds for all cases in one function.

2. Prepare for further tap-fd-migraton feature, when we'll need to postpone vhost initialization up to post-load stage.

---
 net/tap.c | 37 +++++++++++++++++++++----------------
 1 file changed, 21 insertions(+), 16 deletions(-)
net/tap.c +21 -16
index 1623ddff5f..1245b98408 100644
@@ -736,8 +736,7 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
         }
     }
 
-    if (tap->has_vhost ? tap->vhost :
-        (vhostfd != -1) || (tap->has_vhostforce && tap->vhostforce)) {
+    if (vhostfd != -1) {
         VhostNetOptions options;
 
         options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
@@ -747,17 +746,6 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
         } else {
             options.busyloop_timeout = 0;
         }
-
-        if (vhostfd == -1) {
-            vhostfd = open("/dev/vhost-net", O_RDWR);
-            if (vhostfd < 0) {
-                error_setg_file_open(errp, errno, "/dev/vhost-net");
-                goto failed;
-            }
-            if (!qemu_set_blocking(vhostfd, false, errp)) {
-                goto failed;
-            }
-        }
         options.opaque = (void *)(uintptr_t)vhostfd;
         options.nvqs = 2;
         options.feature_bits = kernel_feature_bits;
@@ -839,13 +827,30 @@ static int tap_parse_fds_and_queues(const NetdevTapOptions *tap, int **fds,
 static bool tap_parse_vhost_fds(const NetdevTapOptions *tap, int **vhost_fds,
                                 int queues, Error **errp)
 {
-    if (!(tap->vhostfd || tap->vhostfds)) {
+    bool need_vhost = tap->has_vhost ? tap->vhost :
+        ((tap->vhostfd || tap->vhostfds) ||
+         (tap->has_vhostforce && tap->vhostforce));
+
+    if (!need_vhost) {
         *vhost_fds = NULL;
         return true;
     }
 
-    if (net_parse_fds(tap->fd ?: tap->fds, vhost_fds, queues, errp) < 0) {
-        return false;
+    if (tap->vhostfd || tap->vhostfds) {
+        if (net_parse_fds(tap->fd ?: tap->fds, vhost_fds, queues, errp) < 0) {
+            return false;
+        }
+    } else if (!(tap->vhostfd || tap->vhostfds)) {
+        *vhost_fds = g_new(int, queues);
+        for (int i = 0; i < queues; i++) {
+            int vhostfd = open("/dev/vhost-net", O_RDWR);
+            if (vhostfd < 0) {
+                error_setg_file_open(errp, errno, "/dev/vhost-net");
+                net_free_fds(*vhost_fds, i);
+                return false;
+            }
+            (*vhost_fds)[i] = vhostfd;
+        }
     }
 
     if (!unblock_fds(*vhost_fds, queues, errp)) {
Signature · 1 line
2.52.0
#2 lore raw

PATCH v11 2/8 net/tap: move vhost initialization to tap_setup_vhost()

In reply to Vladimir Sementsov-Ogievskiy

Make a new helper function in a way it can be reused later for TAP fd-migration feature: we'll need to initialize vhost in a later point when we doesn't have access to QAPI parameters.

---
 net/tap.c | 62 ++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 38 insertions(+), 24 deletions(-)
net/tap.c +38 -24
index 1245b98408..294ca61f8b 100644
@@ -71,6 +71,8 @@ static const int kernel_feature_bits[] = {
 typedef struct TAPState {
     NetClientState nc;
     int fd;
+    int vhostfd;
+    uint32_t vhost_busyloop_timeout;
     char down_script[1024];
     char down_script_arg[128];
     uint8_t buf[NET_BUFSIZE];
@@ -702,6 +704,38 @@ static int net_tap_init(const NetdevTapOptions *tap, int *vnet_hdr,
     return fd;
 }
 
+static bool tap_setup_vhost(TAPState *s, Error **errp)
+{
+    VhostNetOptions options;
+
+    if (s->vhostfd == -1) {
+        return true;
+    }
+
+    options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
+    options.net_backend = &s->nc;
+    options.busyloop_timeout = s->vhost_busyloop_timeout;
+    options.opaque = (void *)(uintptr_t)s->vhostfd;
+    options.nvqs = 2;
+    options.feature_bits = kernel_feature_bits;
+    options.get_acked_features = NULL;
+    options.save_acked_features = NULL;
+    options.max_tx_queue_size = 0;
+    options.is_vhost_user = false;
+
+    s->vhost_net = vhost_net_init(&options);
+    if (!s->vhost_net) {
+        error_setg(errp,
+                   "vhost-net requested but could not be initialized");
+        return false;
+    }
+
+    /* vhostfd ownership is passed to s->vhost_net */
+    s->vhostfd = -1;
+
+    return true;
+}
+
 static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
                              const char *name,
                              const char *ifname, const char *script,
@@ -736,30 +770,10 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,
         }
     }
 
-    if (vhostfd != -1) {
-        VhostNetOptions options;
-
-        options.backend_type = VHOST_BACKEND_TYPE_KERNEL;
-        options.net_backend = &s->nc;
-        if (tap->has_poll_us) {
-            options.busyloop_timeout = tap->poll_us;
-        } else {
-            options.busyloop_timeout = 0;
-        }
-        options.opaque = (void *)(uintptr_t)vhostfd;
-        options.nvqs = 2;
-        options.feature_bits = kernel_feature_bits;
-        options.get_acked_features = NULL;
-        options.save_acked_features = NULL;
-        options.max_tx_queue_size = 0;
-        options.is_vhost_user = false;
-
-        s->vhost_net = vhost_net_init(&options);
-        if (!s->vhost_net) {
-            error_setg(errp,
-                       "vhost-net requested but could not be initialized");
-            goto failed;
-        }
+    s->vhostfd = vhostfd;
+    s->vhost_busyloop_timeout = tap->has_poll_us ? tap->poll_us : 0;
+    if (!tap_setup_vhost(s, errp)) {
+        return false;
     }
 
     return true;
Signature · 1 line
2.52.0
#3 lore raw

PATCH v11 3/8 qapi: add local migration parameter

In reply to Vladimir Sementsov-Ogievskiy

We are going to implement local-migration feature: some devices will be able to transfer open file descriptors through migration stream (which must UNIX domain socket for that purpose). This allows to transfer the whole backend state without reconnecting and restarting the backend service. For example, virtio-net will migrate its attached TAP netdev, together with its connected file descriptors.

In this commit we introduce a migration parameter, which enables the feature for supporting devices (no one at the moment).

---
 include/migration/misc.h |  2 ++
 migration/options.c      | 18 +++++++++++++++++-
 qapi/migration.json      | 14 ++++++++++++--
 3 files changed, 31 insertions(+), 3 deletions(-)
include/migration/misc.h +2 -0
index e26d418a6e..f9b6a67129 100644
@@ -152,4 +152,6 @@ bool multifd_device_state_save_thread_should_exit(void);
 void multifd_abort_device_state_save_threads(void);
 bool multifd_join_device_state_save_threads(void);
 
+bool migrate_local(void);
+
 #endif
migration/options.c +17 -1
index 1ffe85a2d8..92144da3ac 100644
@@ -13,6 +13,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu/error-report.h"
+#include "qapi/util.h"
 #include "exec/target_page.h"
 #include "qapi/clone-visitor.h"
 #include "qapi/error.h"
@@ -24,6 +25,7 @@
 #include "migration/colo.h"
 #include "migration/cpr.h"
 #include "migration/misc.h"
+#include "migration/options.h"
 #include "migration.h"
 #include "migration-stats.h"
 #include "qemu-file.h"
@@ -336,6 +338,12 @@ bool migrate_mapped_ram(void)
     return s->capabilities[MIGRATION_CAPABILITY_MAPPED_RAM];
 }
 
+bool migrate_local(void)
+{
+    MigrationState *s = migrate_get_current();
+    return s->parameters.local;
+}
+
 bool migrate_ignore_shared(void)
 {
     MigrationState *s = migrate_get_current();
@@ -1047,7 +1055,7 @@ static void migrate_mark_all_params_present(MigrationParameters *p)
         &p->has_announce_step, &p->has_block_bitmap_mapping,
         &p->has_x_vcpu_dirty_limit_period, &p->has_vcpu_dirty_limit,
         &p->has_mode, &p->has_zero_page_detection, &p->has_direct_io,
-        &p->has_cpr_exec_command,
+        &p->has_cpr_exec_command, &p->has_local,
     };
 
     len = ARRAY_SIZE(has_fields);
@@ -1386,6 +1394,10 @@ static void migrate_params_test_apply(MigrationParameters *params,
     if (params->has_cpr_exec_command) {
         dest->cpr_exec_command = params->cpr_exec_command;
     }
+
+    if (params->has_local) {
+        dest->local = params->local;
+    }
 }
 
 static void migrate_params_apply(MigrationParameters *params)
@@ -1514,6 +1526,10 @@ static void migrate_params_apply(MigrationParameters *params)
         s->parameters.cpr_exec_command =
             QAPI_CLONE(strList, params->cpr_exec_command);
     }
+
+    if (params->has_local) {
+        s->parameters.local = params->local;
+    }
 }
 
 void qmp_migrate_set_parameters(MigrationParameters *params, Error **errp)
qapi/migration.json +12 -2
index f925e5541b..f110bd8399 100644
@@ -828,7 +828,8 @@
            'mode',
            'zero-page-detection',
            'direct-io',
-           'cpr-exec-command'] }
+           'cpr-exec-command',
+           'local'] }
 
 ##
 # @migrate-set-parameters:
@@ -1004,6 +1005,14 @@
 #     is @cpr-exec.  The first list element is the program's filename,
 #     the remainder its arguments.  (Since 10.2)
 #
+# @local: Enable local migration feature for devices that support
+#     it.  The feature is for local migation only and rely on the
+#     channel support for passing file desciptors (it must be a UNIX
+#     socket).  In general that means that backend state and its file
+#     descriptors are passed to the destination in the migration
+#     channel.  Individual devices declare the support for local
+#     migration by per-device local-migration option.  (Since 11.0)
+#
 # Features:
 #
 # @unstable: Members @x-checkpoint-delay and
@@ -1043,7 +1052,8 @@
             '*mode': 'MigMode',
             '*zero-page-detection': 'ZeroPageDetection',
             '*direct-io': 'bool',
-            '*cpr-exec-command': [ 'str' ]} }
+            '*cpr-exec-command': [ 'str' ],
+            '*local': 'bool' } }
 
 ##
 # @query-migrate-parameters:
Signature · 1 line
2.52.0
Reviewed-by on v11 3/8
#4 lore raw

Re: [PATCH v11 3/8] qapi: add local migration parameter

Quoted 11 lines · Vladimir Sementsov-Ogievskiy

We are going to implement local-migration feature: some devices will be able to transfer open file descriptors through migration stream (which must UNIX domain socket for that purpose). This allows to transfer the whole backend state without reconnecting and restarting the backend service. For example, virtio-net will migrate its attached TAP netdev, together with its connected file descriptors.

In this commit we introduce a migration parameter, which enables the feature for supporting devices (no one at the moment).

PS: one nitpick is maybe we don't need to spell out the per-device option in the doc of "local", it also doesn't need to be called "local-migration" on every device if it will be presnet.. no strong feelings.

Thanks,

Signature · 1 line
Peter Xu
#5 lore raw

Re: [PATCH v11 3/8] qapi: add local migration parameter

Quoted 11 lines · Peter Xu
Quoted 10 lines · Vladimir Sementsov-Ogievskiy

We are going to implement local-migration feature: some devices will be able to transfer open file descriptors through migration stream (which must UNIX domain socket for that purpose). This allows to transfer the whole backend state without reconnecting and restarting the backend service. For example, virtio-net will migrate its attached TAP netdev, together with its connected file descriptors.

In this commit we introduce a migration parameter, which enables the feature for supporting devices (no one at the moment).

Quoted 8 lines
Quoted 1 line

PS: one nitpick is maybe we don't need to spell out the per-device option in the doc of "local", it also doesn't need to be called "local-migration" on every device if it will be presnet.. no strong feelings.

I thought about it.. But couldn't come up with a more generic phrase. And I think it's bad to omit the fact that the parameter doesn't automatically enable the feature without some flag on device side.

+# channel. Individual devices declare the support for local
+# migration by per-device local-migration option. (Since 11.0)

Hmm. After merging at least virtio-net/TAP migration we can write:

In general devices may have an option (like local-migration for virtio-net) which in conjunction with "local" set to true enables the local-migration feature for that device.

And to be prepared for it now:

In general devices may have an option which in conjunction with "local" set to true enables the local-migration feature for that device.

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw

Re: [PATCH v11 3/8] qapi: add local migration parameter

Quoted 34 lines · Vladimir Sementsov-Ogievskiy
Quoted 11 lines · Peter Xu
Quoted 10 lines · Vladimir Sementsov-Ogievskiy

We are going to implement local-migration feature: some devices will be able to transfer open file descriptors through migration stream (which must UNIX domain socket for that purpose). This allows to transfer the whole backend state without reconnecting and restarting the backend service. For example, virtio-net will migrate its attached TAP netdev, together with its connected file descriptors.

In this commit we introduce a migration parameter, which enables the feature for supporting devices (no one at the moment).

Quoted 8 lines
Quoted 1 line

PS: one nitpick is maybe we don't need to spell out the per-device option in the doc of "local", it also doesn't need to be called "local-migration" on every device if it will be presnet.. no strong feelings.

I thought about it.. But couldn't come up with a more generic phrase. And I think it's bad to omit the fact that the parameter doesn't automatically enable the feature without some flag on device side.

qapi/migration.json +2 -0
+#     channel.  Individual devices declare the support for local
+#     migration by per-device local-migration option.  (Since 11.0)

Hmm. After merging at least virtio-net/TAP migration we can write:

In general devices may have an option (like local-migration for virtio-net) which in conjunction with "local" set to true enables the local-migration feature for that device.

Quoted 5 lines

And to be prepared for it now:

In general devices may have an option which in conjunction with "local" set to true enables the local-migration feature for that device.

Yep, this looks good. The per-device option is not required IMHO, so "may have" is accurate to me.

For example, when we introduce a completely new device, it can support migrate_is_local() since the first day, then it doesn't need the per device knob.

OTOH, the global "local" parameter is needed only because we want a promise from the user that this is a local migration, instead of relying on URI being unix sockets. One example is in kubevirt AFAIU we use unix sockets even for remote migrations (where the unix socket will only be used as a proxy to route things outside of the container). Hence "local" deduces "unix sockets", not vice versa.

Thanks,

Signature · 1 line
Peter Xu
#7 lore raw

PATCH v11 4/8 net: introduce vmstate_net_peer_backend

In reply to Vladimir Sementsov-Ogievskiy

To implement backend migration in virtio-net in the next commit, we need a generic API to migrate net backend. Here is it.

---
 include/net/net.h |  4 ++++
 net/net.c         | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)
include/net/net.h +4 -0
index 45bc86fc86..aa34043b1a 100644
@@ -5,6 +5,7 @@
 #include "qapi/qapi-types-net.h"
 #include "net/queue.h"
 #include "hw/core/qdev-properties-system.h"
+#include "migration/vmstate.h"
 
 #define MAC_FMT "%02X:%02X:%02X:%02X:%02X:%02X"
 #define MAC_ARG(x) ((uint8_t *)(x))[0], ((uint8_t *)(x))[1], \
@@ -110,6 +111,7 @@ typedef struct NetClientInfo {
     SetSteeringEBPF *set_steering_ebpf;
     NetCheckPeerType *check_peer_type;
     GetVHostNet *get_vhost_net;
+    const VMStateDescription *backend_vmsd;
 } NetClientInfo;
 
 struct NetClientState {
@@ -354,4 +356,6 @@ static inline bool net_peer_needs_padding(NetClientState *nc)
   return nc->peer && !nc->peer->do_not_pad;
 }
 
+extern const VMStateInfo vmstate_net_peer_backend;
+
 #endif
net/net.c +47 -0

56 lines not on this page · raw message

Signature · 1 line
2.52.0
#8 lore raw

PATCH v11 5/8 virtio-net: support local migration of backend

In reply to Vladimir Sementsov-Ogievskiy

Add virtio-net option local-migration, which is true by default, but false for older machine types, which doesn't support the feature.

When both global migration parameter "local" and new virtio-net parameter "local-migration" are true, virtio-net transfer the whole net backend to the destination, including open file descriptors. Of-course, its only for local migration and the channel must be UNIX domain socket.

This way management tool should not care about creating new TAP, and should not handle switching to it. Migration downtime become shorter.

Support for TAP will come in the next commit.

---
 hw/core/machine.c              |   1 +
 hw/net/virtio-net.c            | 137 ++++++++++++++++++++++++++++++++-
 include/hw/virtio/virtio-net.h |   2 +
 include/net/net.h              |   2 +
 4 files changed, 141 insertions(+), 1 deletion(-)
hw/core/machine.c +1 -0
index d4ef620c17..b35d0d204b 100644
@@ -40,6 +40,7 @@
 
 GlobalProperty hw_compat_10_2[] = {
     { "scsi-block", "migrate-pr", "off" },
+    { TYPE_VIRTIO_NET, "local-migration", "false" },
 };
 const size_t hw_compat_10_2_len = G_N_ELEMENTS(hw_compat_10_2);
 
hw/net/virtio-net.c +136 -1

199 lines not on this page · raw message

include/hw/virtio/virtio-net.h +2 -0
index 5b8ab7bda7..0470e47777 100644
@@ -231,6 +231,8 @@ struct VirtIONet {
     struct EBPFRSSContext ebpf_rss;
     uint32_t nr_ebpf_rss_fds;
     char **ebpf_rss_fds;
+    bool peers_wait_incoming;
+    bool local_migration;
 };
 
 size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
include/net/net.h +2 -0
index aa34043b1a..d4cf399d4a 100644
@@ -82,6 +82,7 @@ typedef void (SocketReadStateFinalize)(SocketReadState *rs);
 typedef void (NetAnnounce)(NetClientState *);
 typedef bool (SetSteeringEBPF)(NetClientState *, int);
 typedef bool (NetCheckPeerType)(NetClientState *, ObjectClass *, Error **);
+typedef bool (IsWaitIncoming)(NetClientState *);
 typedef struct vhost_net *(GetVHostNet)(NetClientState *nc);
 
 typedef struct NetClientInfo {
@@ -110,6 +111,7 @@ typedef struct NetClientInfo {
     NetAnnounce *announce;
     SetSteeringEBPF *set_steering_ebpf;
     NetCheckPeerType *check_peer_type;
+    IsWaitIncoming *is_wait_incoming;
     GetVHostNet *get_vhost_net;
     const VMStateDescription *backend_vmsd;
 } NetClientInfo;
Signature · 1 line
2.52.0
#9 lore raw

PATCH v11 6/8 net/tap: support local migration with virtio-net

In reply to Vladimir Sementsov-Ogievskiy

Support transferring of TAP state (including open fd) through migration stream as part of viritio-net "local-migration".

Add new option, incoming-fds, which should be set to true to trigger new logic.

---
 net/tap.c     | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 qapi/net.json |  6 +++-
 2 files changed, 96 insertions(+), 2 deletions(-)
net/tap.c +91 -1
index 294ca61f8b..f6e4a36e97 100644
@@ -393,6 +393,65 @@ static VHostNetState *tap_get_vhost_net(NetClientState *nc)
     return s->vhost_net;
 }
 
+static bool tap_is_wait_incoming(NetClientState *nc)
+{
+    TAPState *s = DO_UPCAST(TAPState, nc, nc);
+    assert(nc->info->type == NET_CLIENT_DRIVER_TAP);
+    return s->fd == -1;
+}
+
+static int tap_pre_load(void *opaque)
+{
+    TAPState *s = opaque;
+
+    if (s->fd != -1) {
+        error_report(
+            "TAP is already initialized and cannot receive incoming fd");
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+static bool tap_setup_vhost(TAPState *s, Error **errp);
+
+static int tap_post_load(void *opaque, int version_id)
+{
+    TAPState *s = opaque;
+    Error *local_err = NULL;
+
+    tap_read_poll(s, true);
+
+    if (s->fd < 0) {
+        return -1;
+    }
+
+    if (!tap_setup_vhost(s, &local_err)) {
+        error_prepend(&local_err,
+                      "Failed to setup vhost during TAP post-load: ");
+        error_report_err(local_err);
+        return -1;
+    }
+
+    return 0;
+}
+
+static const VMStateDescription vmstate_tap = {
+    .name = "net-tap",
+    .pre_load = tap_pre_load,
+    .post_load = tap_post_load,
+    .fields = (const VMStateField[]) {
+        VMSTATE_FD(fd, TAPState),
+        VMSTATE_BOOL(using_vnet_hdr, TAPState),
+        VMSTATE_BOOL(has_ufo, TAPState),
+        VMSTATE_BOOL(has_uso, TAPState),
+        VMSTATE_BOOL(has_tunnel, TAPState),
+        VMSTATE_BOOL(enabled, TAPState),
+        VMSTATE_UINT32(host_vnet_hdr_len, TAPState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
 /* fd support */
 
 static NetClientInfo net_tap_info = {
@@ -412,7 +471,9 @@ static NetClientInfo net_tap_info = {
     .set_vnet_le = tap_set_vnet_le,
     .set_vnet_be = tap_set_vnet_be,
     .set_steering_ebpf = tap_set_steering_ebpf,
+    .is_wait_incoming = tap_is_wait_incoming,
     .get_vhost_net = tap_get_vhost_net,
+    .backend_vmsd = &vmstate_tap,
 };
 
 static TAPState *net_tap_fd_init(NetClientState *peer,
@@ -905,6 +966,12 @@ int net_init_tap(const Netdev *netdev, const char *name,
         return -1;
     }
 
+    if (tap->incoming_fds && (tap->fd || tap->fds || tap->helper)) {
+        error_setg(errp, "incoming-fds is incompatible with "
+                   "fd=, fds=, helper=");
+        return -1;
+    }
+
     queues = tap_parse_fds_and_queues(tap, &fds, errp);
     if (queues < 0) {
         return -1;
@@ -923,7 +990,24 @@ int net_init_tap(const Netdev *netdev, const char *name,
         goto fail;
     }
 
-    if (fds) {
+    if (tap->incoming_fds) {
+        for (i = 0; i < queues; i++) {
+            NetClientState *nc;
+            TAPState *s;
+
+            nc = qemu_new_net_client(&net_tap_info, peer, "tap", name);
+            qemu_set_info_str(nc, "incoming");
+
+            s = DO_UPCAST(TAPState, nc, nc);
+            s->fd = -1;
+            if (vhost_fds) {
+                s->vhostfd = vhost_fds[i];
+                s->vhost_busyloop_timeout = tap->has_poll_us ? tap->poll_us : 0;
+            } else {
+                s->vhostfd = -1;
+            }
+        }
+    } else if (fds) {
         for (i = 0; i < queues; i++) {
             if (i == 0) {
                 vnet_hdr = tap_probe_vnet_hdr(fds[i], errp);
@@ -949,6 +1033,12 @@ int net_init_tap(const Netdev *netdev, const char *name,
         g_autofree char *downscript =
             tap_parse_script(tap->downscript, DEFAULT_NETWORK_DOWN_SCRIPT);
 
+        if (tap->incoming_fds && (script || downscript)) {
+            error_setg(errp,
+                       "incoming-fds requires script=no and downscript=no");
+            return -1;
+        }
+
         if (tap->ifname) {
             pstrcpy(ifname, sizeof ifname, tap->ifname);
         } else {
qapi/net.json +5 -1
index 118bd34965..79f5ce9f43 100644
@@ -355,6 +355,9 @@
 # @poll-us: maximum number of microseconds that could be spent on busy
 #     polling for tap (since 2.7)
 #
+# @incoming-fds: do not open/connnect any resources, instead wait for
+#     TAP state from incoming migration stream.  (Since 11.0)
+#
 # Since: 1.2
 ##
 { 'struct': 'NetdevTapOptions',
@@ -373,7 +376,8 @@
     '*vhostfds':   'str',
     '*vhostforce': 'bool',
     '*queues':     'uint32',
-    '*poll-us':    'uint32'} }
+    '*poll-us':    'uint32',
+    '*incoming-fds': 'bool' } }
 
 ##
 # @NetdevSocketOptions:
Signature · 1 line
2.52.0
#10 lore raw

Re: [PATCH v11 6/8] net/tap: support local migration with virtio-net

Quoted 6 lines · On 2/19/26, 6:56 AM, "Vladimir Sementsov-Ogievskiy"…

+ if (tap->incoming_fds && (script || downscript)) {
+ error_setg(errp,
+ "incoming-fds requires script=no and downscript=no");
+ return -1;
+ }
+

This conditional will never trigger because we are in an else{} block that implies !tap->incoming_fds. I think we need to handle this case higher up.

Other than that, this patch set LGTM. I was able to test it, and the performance was impressive.

Thanks,
Ben

#11 lore raw

Re: [PATCH v11 6/8] net/tap: support local migration with virtio-net

Quoted 10 lines · Ben
Quoted 6 lines · On 2/19/26, 6:56 AM, "Vladimir Sementsov-Ogievskiy"…

+ if (tap->incoming_fds && (script || downscript)) {
+ error_setg(errp,
+ "incoming-fds requires script=no and downscript=no");
+ return -1;
+ }
+

Quoted 3 lines

This conditional will never trigger because we are in an else{} block that implies !tap->incoming_fds. I think we need to handle this case higher up.

Oh, right, will fix.

Quoted 4 lines

Other than that, this patch set LGTM. I was able to test it, and the performance was impressive.

Thanks for testing!

Signature · 2 lines
Best regards,
Vladimir
#12 lore raw
In reply to Vladimir Sementsov-Ogievskiy

Based-on: <[email protected]>
"[PATCH v3 00/12] net: refactoring and fixes"

Quoted 46 lines · Vladimir Sementsov-Ogievskiy

Hi all!

Here is a new migration parameter "local", which allows to enable local migration of TAP virtio-net backend (and maybe other devices and backends in future), including its properties and open fds.

With this new option, management software doesn't need to initialize new TAP and do a switch to it. Nothing should be done around virtio-net in local migration: it just migrates and continues to use same TAP device. So we avoid extra logic in management software, extra allocations in kernel (for new TAP), and corresponding extra delay in migration downtime.

v11: rebranding "backend-transfer" -> "local"
06: require script=no and downscript=no
instead of requiring absent options,
for future compatibility

Vladimir Sementsov-Ogievskiy (8):
net/tap: move vhost-net open() calls to tap_parse_vhost_fds() net/tap: move vhost initialization to tap_setup_vhost()
qapi: add local migration parameter
net: introduce vmstate_net_peer_backend
virtio-net: support local migration of backend net/tap: support local migration with virtio-net tests/functional: add skipWithoutSudo() decorator tests/functional: add test_tap_migration

  hw/core/machine.c                             |   1 +
  hw/net/virtio-net.c                           | 137 +++++-
  include/hw/virtio/virtio-net.h                |   2 +
  include/migration/misc.h                      |   2 +
  include/net/net.h                             |   6 +
  migration/options.c                           |  18 +-
  net/net.c                                     |  47 ++
  net/tap.c                                     | 189 +++++++--
  qapi/migration.json                           |  14 +-
  qapi/net.json                                 |   6 +-
  tests/functional/qemu_test/decorators.py      |  16 +
  tests/functional/x86_64/meson.build           |   1 +
  tests/functional/x86_64/test_tap_migration.py | 401 ++++++++++++++++++
  13 files changed, 795 insertions(+), 45 deletions(-)

create mode 100755 tests/functional/x86_64/test_tap_migration.py

Signature · 2 lines
Best regards,
Vladimir
#13 lore raw

PATCH v11 7/8 tests/functional: add skipWithoutSudo() decorator

In reply to Vladimir Sementsov-Ogievskiy

To be used in the next commit: that would be a test for TAP networking, and it will need to setup TAP device.

---
 tests/functional/qemu_test/decorators.py | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
tests/functional/qemu_test/decorators.py +16 -0
index fcf236ecfd..aa135acc78 100644
@@ -6,6 +6,7 @@
 import os
 import platform
 import resource
+import subprocess
 from unittest import skipIf, skipUnless
 
 from .cmd import which
@@ -177,3 +178,18 @@ def skipLockedMemoryTest(locked_memory):
         ulimit_memory == resource.RLIM_INFINITY or ulimit_memory >= locked_memory * 1024,
         f'Test required {locked_memory} kB of available locked memory',
     )
+
+'''
+Decorator to skip execution of a test if passwordless
+sudo command is not available.
+'''
+def skipWithoutSudo():
+    proc = subprocess.run(["sudo", "-n", "/bin/true"],
+                          stdin=subprocess.PIPE,
+                          stdout=subprocess.PIPE,
+                          stderr=subprocess.STDOUT,
+                          universal_newlines=True,
+                          check=False)
+
+    return skipUnless(proc.returncode == 0,
+                      f'requires password-less sudo access: {proc.stdout}')
Signature · 1 line
2.52.0
#14 lore raw

PATCH v11 8/8 tests/functional: add test_tap_migration

In reply to Vladimir Sementsov-Ogievskiy

Add test for a new local-migration migration of virtio-net/tap, with fd passing through UNIX socket.

---
 tests/functional/x86_64/meson.build           |   1 +
 tests/functional/x86_64/test_tap_migration.py | 401 ++++++++++++++++++
 2 files changed, 402 insertions(+)
 create mode 100755 tests/functional/x86_64/test_tap_migration.py
tests/functional/x86_64/meson.build +1 -0
index f78eec5e6c..d23b0cc727 100644
@@ -36,4 +36,5 @@ tests_x86_64_system_thorough = [
   'vfio_user_client',
   'virtio_balloon',
   'virtio_gpu',
+  'tap_migration',
 ]
tests/functional/x86_64/test_tap_migration.py new file +401 -0

401 lines not on this page · raw message

Signature · 1 line
2.52.0
#15 lore raw

Re: [PATCH v11 8/8] tests/functional: add test_tap_migration

Quoted 43 lines · Vladimir Sementsov-Ogievskiy

Add test for a new local-migration migration of virtio-net/tap, with fd passing through UNIX socket.

---
  tests/functional/x86_64/meson.build           |   1 +
  tests/functional/x86_64/test_tap_migration.py | 401 ++++++++++++++++++
  2 files changed, 402 insertions(+)

create mode 100755 tests/functional/x86_64/test_tap_migration.py

tests/functional/x86_64/meson.build +1 -0
index f78eec5e6c..d23b0cc727 100644
@@ -36,4 +36,5 @@ tests_x86_64_system_thorough = [
    'vfio_user_client',
    'virtio_balloon',
    'virtio_gpu',
+  'tap_migration',
  ]
tests/functional/x86_64/test_tap_migration.py new file +17 -0
new file mode 100755
index 0000000000..c629b44f66
@@ -0,0 +1,401 @@
+#!/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
+from typing import Tuple
+
+from qemu_test import (

tests/functional/x86_64/test_tap_migration.py line 27

Quoted 10 lines
tests/functional/x86_64/test_tap_migration.py new file +10 -0
+    LinuxKernelTest,
+    Asset,
+    exec_command_and_wait_for_pattern,
+)
+from qemu_test.decorators import skipWithoutSudo
+
+GUEST_IP = "10.0.1.2"
+GUEST_IP_MASK = f"{GUEST_IP}/24"
+GUEST_MAC = "d6:0d:75:f8:0f:b7"
+HOST_IP = "10.0.1.1"

Is it ok to use hard-coded IP addresses here? What if there is already another unrelated 10.0.1.x network on the host (e.g. from a libvirt setup or a VPN)?

tests/functional/x86_64/test_tap_migration.py line 36

Quoted 9 lines
tests/functional/x86_64/test_tap_migration.py new file +9 -0
+HOST_IP_MASK = f"{HOST_IP}/24"
+TAP_ID = "tap0"
+TAP_ID2 = "tap1"
+TAP_MAC = "e6:1d:44:b5:03:5d"
+
+
+def ip(args, check=True) -> None:
+    """Run ip command with sudo"""
+    run(["sudo", "ip"] + args, check=check)

I think you need to check for the "ip" binary somewhere and skip the test if it is not available (e.g. on a unusual Linux distro or a *BSD host).

Thomas

#16 lore raw

Re: [PATCH v11 8/8] tests/functional: add test_tap_migration

Quoted 48 lines · Thomas Huth
Quoted 47 lines · Vladimir Sementsov-Ogievskiy

Add test for a new local-migration migration of virtio-net/tap, with fd passing through UNIX socket.

---
  tests/functional/x86_64/meson.build           |   1 + tests/functional/x86_64/test_tap_migration.py | 401 ++++++++++++++++++

  2 files changed, 402 insertions(+)

  create mode 100755 tests/functional/x86_64/test_tap_migration.py

tests/functional/x86_64/meson.build +1 -0
index f78eec5e6c..d23b0cc727 100644
@@ -36,4 +36,5 @@ tests_x86_64_system_thorough = [
     'vfio_user_client',
     'virtio_balloon',
     'virtio_gpu',
+  'tap_migration',

  ]

tests/functional/x86_64/test_tap_migration.py new file +21 -0
new file mode 100755
index 0000000000..c629b44f66
@@ -0,0 +1,401 @@
+#!/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
+from typing import Tuple
+
+from qemu_test import (
+    LinuxKernelTest,
+    Asset,
+    exec_command_and_wait_for_pattern,
+)
Quoted 8 lines

tests/functional/x86_64/test_tap_migration.py line 27

Quoted 6 lines
tests/functional/x86_64/test_tap_migration.py new file +6 -0
+from qemu_test.decorators import skipWithoutSudo
+
+GUEST_IP = "10.0.1.2"
+GUEST_IP_MASK = f"{GUEST_IP}/24"
+GUEST_MAC = "d6:0d:75:f8:0f:b7"
+HOST_IP = "10.0.1.1"

Is it ok to use hard-coded IP addresses here? What if there is already another unrelated 10.0.1.x network on the host (e.g. from a libvirt setup or a VPN)?

Agree, that's bad.. What to do? AI suggest use isolated network namespace (netns). I'll try it.

Quoted 12 lines

tests/functional/x86_64/test_tap_migration.py line 36

Quoted 10 lines
tests/functional/x86_64/test_tap_migration.py new file +9 -0
+HOST_IP_MASK = f"{HOST_IP}/24"
+TAP_ID = "tap0"
+TAP_ID2 = "tap1"
+TAP_MAC = "e6:1d:44:b5:03:5d"
+
+
+def ip(args, check=True) -> None:
+    """Run ip command with sudo"""
+    run(["sudo", "ip"] + args, check=check)

I think you need to check for the "ip" binary somewhere and skip the test if it is not available (e.g. on a unusual Linux distro or a *BSD host).

ok

Signature · 2 lines
Best regards,
Vladimir