net/tap: deprecate "no" as special value for script/downscript
Subsystems (4)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH v21 1/16 net/tap: rework tap_parse_script
Factor out tap_is_explicit_no_script() helper, to simplify further changes.
Avoid extra copying by simpler code flow: first check for NULL / empty / "no" cases, then get default or do copying respectively.
--- net/tap.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-)
net/tap.c +22 -6
@@ -92,19 +92,35 @@ static void launch_script(const char *setup_script, const char *ifname,static void tap_send (void *opaque);static void tap_writable (void *opaque); -static char *tap_parse_script (const char *script_arg,const char *default_path) +static bool tap_is_explicit_no_script (const char *script_arg_value) { - g_autofree char *res = g_strdup(script_arg); +if (!script_arg_value) { +return false; + } + +if (script_arg_value[0 ] =='\0' ) { +return true; + } -if (!res) { - res =get_relocated_path (default_path); +if (strcmp (script_arg_value,"no" ) ==0 ) { +return true; } -if (res[0 ] =='\0' ||strcmp (res,"no" ) ==0 ) { +return false; +} + +static char *tap_parse_script (const char *script_arg_value, +const char *default_path) +{ +if (tap_is_explicit_no_script (script_arg_value)) {return NULL ; } -return g_steal_pointer (&res); +if (!script_arg_value) { +return get_relocated_path (default_path); + } + +return g_strdup (script_arg_value); }static void tap_update_fd_handler (TAPState *s)
Signature · 1 line
2.43.0
PATCH v21 2/16 net/tap: improve script/downscript options documentation
In reply to a message not in the archiveProperly document defaults and special values of "" and "no".
--- qapi/net.json | 12 +++++++++--- qemu-options.hx | 9 +++++---- 2 files changed, 14 insertions(+), 7 deletions(-)
qapi/net.json +9 -3
@@ -399,9 +399,15 @@ # @fds: multiple file descriptors of already opened multiqueue capable # tap # -# @script: script to initialize the interface -# -# @downscript: script to shut down the interface +# @script: script to initialize the interface. An empty string or +# "no" disables script execution. Defaults to +# ``<sysconfdir>/qemu-ifup``, where ``<sysconfdir>`` is the +# system configuration directory at build time (typically /etc). +# +# @downscript: script to shut down the interface. An empty string or +# "no" disables script execution. Defaults to +# ``<sysconfdir>/qemu-ifdown``, where ``<sysconfdir>`` is the +# system configuration directory at build time (typically /etc). # # @br: bridge name (since 2.8) #
qemu-options.hx +5 -4
@@ -3014,7 +3014,7 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n" " to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n" " to deconfigure it\n" - " use '[down]script=no' to disable script execution\n" + " use '[down]script=no' or '[down]script=' to disable script execution\n" " use network helper 'helper' (default=" DEFAULT_BRIDGE_HELPER ") to\n" " configure it\n" " use 'fd=h' to connect to an already opened TAP interface\n"
@@ -3550,9 +3550,10 @@ SRST Use the network script file to configure it and the network script dfile to deconfigure it. If name is not provided, the OS automatically provides one. The default network configure script is - ``/etc/qemu-ifup`` and the default network deconfigure script is - ``/etc/qemu-ifdown``. Use ``script=no`` or ``downscript=no`` to - disable script execution. + ``<sysconfdir>/qemu-ifup`` and the default network deconfigure script is + ``<sysconfdir>/qemu-ifdown``, where ``<sysconfdir>`` is the system + configuration directory at build time (typically ``/etc``). + Use ``[down]script=no`` or ``[down]script=`` to disable script execution. If running QEMU as an unprivileged user, use the network helper to configure the TAP interface and attach it to the bridge.
Signature · 1 line
2.43.0
PATCH v21 3/16 net/tap: deprecate "no" as special value for script/downscript
In reply to a message not in the archiveThe interface is ambiguous, as "no" is valid file name. So, using "no" as a special value to disable script is deprecated. Use an empty string ("script=" / "downscript=") instead.
In a future version, "no" will be treated as a plain file name, just like any other non-empty value.
Document the deprecation in docs/about/deprecated.rst, qapi/net.json, and qemu-options.hx. Update other docs to use empty string instead of "no". Add a warning.
--- docs/about/deprecated.rst | 18 ++++++++++++++++++ docs/system/i386/microvm.rst | 4 ++-- docs/system/i386/xenpvh.rst | 2 +- docs/system/ppc/ppce500.rst | 4 ++-- docs/system/riscv/microchip-icicle-kit.rst | 2 +- docs/system/riscv/sifive_u.rst | 2 +- net/tap.c | 17 +++++++++++------ qapi/net.json | 14 ++++++++++---- qemu-options.hx | 8 ++++++-- 9 files changed, 52 insertions(+), 19 deletions(-)
docs/about/deprecated.rst +18 -0
@@ -71,6 +71,15 @@ flexible enough. The monitor objects have been converted to QOM, so ``-mon mode=control`` is replaced by ``-object monitor-qmp``. The short convenience options are not deprecated, only ``-mon``. +``script=no`` and ``downscript=no`` for ``-netdev tap`` (since 11.2) +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +The special value ``"no"`` for the ``script`` and ``downscript`` +parameters of ``-netdev tap`` disables script execution. This special +treatment of ``"no"`` is deprecated. Use an empty string (``script=`` +or ``downscript=``) to disable script execution instead. In a future +version, ``"no"`` will be treated as a plain file name. + QEMU Machine Protocol (QMP) commands ------------------------------------
@@ -164,6 +173,15 @@ Use ``job-finalize`` instead. Use ``query-accelerators`` instead. +``"no"`` as value of ``script``/``downscript`` for tap in ``netdev_add`` (since 11.2) +''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' + +The special value ``"no"`` for the ``script`` and ``downscript`` +parameters of ``netdev_add`` with ``type=tap`` disables script +execution. This special treatment of ``"no"`` is deprecated. Use an +empty string instead. In a future version, ``"no"`` will be treated as +a plain file name. + Human Machine Protocol (HMP) commands -------------------------------------
docs/system/i386/microvm.rst +2 -2
@@ -79,7 +79,7 @@ legacy ``ISA serial`` device as console:: -serial stdio \ -drive id=test,file=test.img,format=raw,if=none \ -device virtio-blk-device,drive=test \ - -netdev tap,id=tap0,script=no,downscript=no \ + -netdev tap,id=tap0,script=,downscript= \ -device virtio-net-device,netdev=tap0 While the example above works, you might be interested in reducing the
@@ -103,7 +103,7 @@ disabled:: -device virtconsole,chardev=virtiocon0 \ -drive id=test,file=test.img,format=raw,if=none \ -device virtio-blk-device,drive=test \ - -netdev tap,id=tap0,script=no,downscript=no \ + -netdev tap,id=tap0,script=,downscript= \ -device virtio-net-device,netdev=tap0
docs/system/i386/xenpvh.rst +1 -1
@@ -42,7 +42,7 @@ case you need to construct one manually: -vnc none \ -display none \ -device virtio-net-pci,id=nic0,netdev=net0,mac=00:16:3e:5c:81:78 \ - -netdev type=tap,id=net0,ifname=vif3.0-emu,br=xenbr0,script=no,downscript=no \ + -netdev type=tap,id=net0,ifname=vif3.0-emu,br=xenbr0,script=,downscript= \ -smp 4,maxcpus=4 \ -nographic \ -machine xenpvh,ram-low-base=0,ram-low-size=2147483648,ram-high-base=4294967296,ram-high-size=2147483648,pci-ecam-base=824633720832,pci-ecam-size=268435456,pci-mmio-base=4026531840,pci-mmio-size=33554432,pci-mmio-high-base=824902156288,pci-mmio-high-size=68719476736 \
docs/system/ppc/ppce500.rst +2 -2
@@ -158,14 +158,14 @@ interface at PCI address 0.1.0, but we can switch that to an e1000 NIC by: $ qemu-system-ppc64 -M ppce500 -smp 4 -m 2G \ -display none -serial stdio \ -bios u-boot \ - -nic tap,ifname=tap0,script=no,downscript=no,model=e1000 + -nic tap,ifname=tap0,script=,downscript=,model=e1000 The QEMU ``ppce500`` machine can also dynamically instantiate an eTSEC device if “-device eTSEC” is given to QEMU: .. code-block:: bash - -netdev tap,ifname=tap0,script=no,downscript=no,id=net0 -device eTSEC,netdev=net0 + -netdev tap,ifname=tap0,script=,downscript=,id=net0 -device eTSEC,netdev=net0 Root file system on flash drive -------------------------------
docs/system/riscv/microchip-icicle-kit.rst +1 -1
@@ -84,7 +84,7 @@ Then we can boot the machine by: $ qemu-system-riscv64 -M microchip-icicle-kit -smp 5 -m 2G \ -sd path/to/sdcard.img \ -nic user,model=cadence_gem \ - -nic tap,ifname=tap,model=cadence_gem,script=no \ + -nic tap,ifname=tap,model=cadence_gem,script= \ -display none -serial stdio \ -kernel path/to/u-boot/build/dir/u-boot.bin \ -dtb path/to/u-boot/build/dir/u-boot.dtb
docs/system/riscv/sifive_u.rst +1 -1
@@ -199,7 +199,7 @@ To boot the VxWorks kernel in QEMU with the ``sifive_u`` machine, use: $ qemu-system-riscv64 -M sifive_u -smp 5 -m 2G \ -display none -serial stdio \ - -nic tap,ifname=tap0,script=no,downscript=no \ + -nic tap,ifname=tap0,script=,downscript= \ -kernel /path/to/vxWorks \ -append "gem(0,0)host:vxWorks h=192.168.200.1 e=192.168.200.2:ffffff00 u=target pw=vxTarget f=0x01"
net/tap.c +11 -6
@@ -92,7 +92,8 @@ static void launch_script(const char *setup_script, const char *ifname,static void tap_send (void *opaque);static void tap_writable (void *opaque); -static bool tap_is_explicit_no_script (const char *script_arg_value) +static bool tap_is_explicit_no_script (const char *script_arg_name, +const char *script_arg_value) {if (!script_arg_value) {return false;
@@ -103,16 +104,19 @@ static bool tap_is_explicit_no_script(const char *script_arg_value) }if (strcmp (script_arg_value,"no" ) ==0 ) { +warn_report ("'%s=no' is deprecated; use '%s=' instead" , + script_arg_name, script_arg_name);return true; }return false; } -static char *tap_parse_script (const char *script_arg_value, +static char *tap_parse_script (const char *script_arg_name, +const char *script_arg_value,const char *default_path) { -if (tap_is_explicit_no_script (script_arg_value)) { +if (tap_is_explicit_no_script (script_arg_name, script_arg_value)) {return NULL ; }
@@ -741,7 +745,7 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer,qemu_set_info_str (&s->nc,"helper=%s" , tap->helper); }else {qemu_set_info_str (&s->nc,"ifname=%s,script=%s,downscript=%s" , ifname, - script ?:"no" , downscript ?:"no" ); + script ?:"" , downscript ?:"" );if (downscript) {snprintf (s->down_script,sizeof (s->down_script),"%s" , downscript);
@@ -947,9 +951,10 @@ int net_init_tap(const Netdev *netdev, const char *name, } }else { g_autofree char *script = -tap_parse_script (tap->script,DEFAULT_NETWORK_SCRIPT ); +tap_parse_script ("script" , tap->script,DEFAULT_NETWORK_SCRIPT ); g_autofree char *downscript = -tap_parse_script (tap->downscript,DEFAULT_NETWORK_DOWN_SCRIPT ); +tap_parse_script ("downscript" , tap->downscript, +DEFAULT_NETWORK_DOWN_SCRIPT );if (tap->ifname) {pstrcpy (ifname,sizeof ifname, tap->ifname);
qapi/net.json +10 -4
@@ -399,15 +399,21 @@ # @fds: multiple file descriptors of already opened multiqueue capable # tap # -# @script: script to initialize the interface. An empty string or -# "no" disables script execution. Defaults to +# @script: script to initialize the interface. An empty string +# disables script execution. Defaults to # ``<sysconfdir>/qemu-ifup``, where ``<sysconfdir>`` is the # system configuration directory at build time (typically /etc). +# Using "no" to disable script execution is deprecated (since +# 11.2); use an empty string instead. In a future version, "no" +# will be treated as a plain file name. # -# @downscript: script to shut down the interface. An empty string or -# "no" disables script execution. Defaults to +# @downscript: script to shut down the interface. An empty string +# disables script execution. Defaults to # ``<sysconfdir>/qemu-ifdown``, where ``<sysconfdir>`` is the # system configuration directory at build time (typically /etc). +# Using "no" to disable script execution is deprecated (since +# 11.2); use an empty string instead. In a future version, "no" +# will be treated as a plain file name. # # @br: bridge name (since 2.8) #
qemu-options.hx +6 -2
@@ -3014,7 +3014,8 @@ DEF("netdev", HAS_ARG, QEMU_OPTION_netdev, " use network scripts 'file' (default=" DEFAULT_NETWORK_SCRIPT ")\n" " to configure it and 'dfile' (default=" DEFAULT_NETWORK_DOWN_SCRIPT ")\n" " to deconfigure it\n" - " use '[down]script=no' or '[down]script=' to disable script execution\n" + " use '[down]script=' to disable script execution\n" + " ('[down]script=no' is deprecated and will be treated as a file name in future)\n" " use network helper 'helper' (default=" DEFAULT_BRIDGE_HELPER ") to\n" " configure it\n" " use 'fd=h' to connect to an already opened TAP interface\n"
@@ -3553,7 +3554,10 @@ SRST ``<sysconfdir>/qemu-ifup`` and the default network deconfigure script is ``<sysconfdir>/qemu-ifdown``, where ``<sysconfdir>`` is the system configuration directory at build time (typically ``/etc``). - Use ``[down]script=no`` or ``[down]script=`` to disable script execution. + Use ``[down]script=`` to disable script execution. + Using ``[down]script=no`` is deprecated; it disables script + execution now, but in a future version it will be treated as a + plain file name. If running QEMU as an unprivileged user, use the network helper to configure the TAP interface and attach it to the bridge.
Signature · 1 line
2.43.0
PATCH v21 5/16 net/tap: move vhost initialization to tap_setup_vhost()
In reply to a message not in the archiveMake 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
@@ -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];
@@ -722,6 +724,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,
@@ -756,30 +790,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.43.0
PATCH v21 6/16 net/tap: use container_of instead of DO_UPCAST
In reply to a message not in the archiveWe are going to QOMify tap backend, which includes deriving TAPState from Object. So "NetClientState nc" will not be a first member. Let's parepare for this change, and use container_of(), which will work regardless position of "nc" field.
--- net/tap.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-)
net/tap.c +18 -18
@@ -175,7 +175,7 @@ static ssize_t tap_write_packet(TAPState *s, const struct iovec *iov, int iovcntstatic ssize_t tap_receive_iov (NetClientState *nc,const struct iovec *iov,int iovcnt) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);const struct iovec *iovp = iov; g_autofree struct iovec *iov_copy = NULL;struct virtio_net_hdr hdr = { };
@@ -211,7 +211,7 @@ ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)static void tap_send_completed (NetClientState *nc,ssize_t len) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);tap_read_poll (s, true); }
@@ -271,7 +271,7 @@ static void tap_send(void *opaque)static bool tap_has_ufo (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );
@@ -280,7 +280,7 @@ static bool tap_has_ufo(NetClientState *nc)static bool tap_has_uso (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );
@@ -289,7 +289,7 @@ static bool tap_has_uso(NetClientState *nc)static bool tap_has_tunnel (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );return s->has_tunnel;
@@ -297,7 +297,7 @@ static bool tap_has_tunnel(NetClientState *nc)static bool tap_has_vnet_hdr (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );
@@ -311,7 +311,7 @@ static bool tap_has_vnet_hdr_len(NetClientState *nc, int len)static void tap_set_vnet_hdr_len (NetClientState *nc,int len) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );
@@ -322,21 +322,21 @@ static void tap_set_vnet_hdr_len(NetClientState *nc, int len)static int tap_set_vnet_le (NetClientState *nc,bool is_le) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);return tap_fd_set_vnet_le (s->fd, is_le); }static int tap_set_vnet_be (NetClientState *nc,bool is_be) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);return tap_fd_set_vnet_be (s->fd, is_be); }static void tap_set_offload (NetClientState *nc,const NetOffloads *ol) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);if (s->fd <0 ) {return ; }
@@ -357,7 +357,7 @@ static void tap_exit_notify(Notifier *notifier, void *data)static void tap_cleanup (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);if (s->vhost_net) {vhost_net_cleanup (s->vhost_net);
@@ -381,14 +381,14 @@ static void tap_cleanup(NetClientState *nc)static void tap_poll (NetClientState *nc,bool enable) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);tap_read_poll (s, enable);tap_write_poll (s, enable); }static bool tap_set_steering_ebpf (NetClientState *nc,int prog_fd) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );return tap_fd_set_steering_ebpf (s->fd, prog_fd) ==0 ;
@@ -396,7 +396,7 @@ static bool tap_set_steering_ebpf(NetClientState *nc, int prog_fd)int tap_get_fd (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );return s->fd; }
@@ -408,7 +408,7 @@ int tap_get_fd(NetClientState *nc)*/ static VHostNetState *tap_get_vhost_net (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);assert (nc->info->type ==NET_CLIENT_DRIVER_TAP );return s->vhost_net; }
@@ -447,7 +447,7 @@ static TAPState *net_tap_fd_init(NetClientState *peer, nc =qemu_new_net_client (&net_tap_info, peer, model, name); - s =DO_UPCAST (TAPState, nc, nc); + s =container_of (nc, TAPState, nc); s->fd = fd; s->host_vnet_hdr_len = vnet_hdr ?sizeof (struct virtio_net_hdr ) :0 ;
@@ -1016,7 +1016,7 @@ fail:int tap_enable (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);int ret;if (s->enabled) {
@@ -1033,7 +1033,7 @@ int tap_enable(NetClientState *nc)int tap_disable (NetClientState *nc) { -TAPState *s =DO_UPCAST (TAPState, nc, nc); +TAPState *s =container_of (nc, TAPState, nc);int ret;if (s->enabled ==0 ) {
Signature · 1 line
2.43.0
PATCH v21 7/16 net/tap: QOMify tap backend
In reply to a message not in the archiveWe prepare for being able to migrate TAP backend. We'll need a user change-able property for it, which can be set from machine type. So, let's QOMify it first.
--- include/net/net.h | 7 +++++++ include/net/tap.h | 2 ++ net/net.c | 14 +++++++------- net/tap.c | 48 +++++++++++++++++++++++++++++++++++++++-------- 4 files changed, 56 insertions(+), 15 deletions(-)
include/net/net.h +7 -0
@@ -160,6 +160,13 @@ char *qemu_mac_strdup_printf(const uint8_t *macaddr);NetClientState *qemu_find_netdev (const char *id);int qemu_find_net_clients_except (const char *id,NetClientState **ncs,NetClientDriver type,int max); +void qemu_net_client_setup (NetClientState *nc, +NetClientInfo *info, +NetClientState *peer, +const char *model, +const char *name, +NetClientDestructor *destructor, +bool is_datapath); NetClientState *qemu_new_net_client(NetClientInfo *info, NetClientState *peer, const char *model,
include/net/tap.h +2 -0
@@ -28,6 +28,8 @@#include "standard-headers/linux/virtio_net.h" +#define TYPE_TAP_NETDEV "tap-netdev" +int tap_enable (NetClientState *nc);int tap_disable (NetClientState *nc);
net/net.c +7 -7
@@ -261,13 +261,13 @@ static ssize_t qemu_deliver_packet_iov(NetClientState *sender,int iovcnt, void *opaque); -static void qemu_net_client_setup (NetClientState *nc, -NetClientInfo *info, -NetClientState *peer, -const char *model, -const char *name, -NetClientDestructor *destructor, -bool is_datapath) +void qemu_net_client_setup (NetClientState *nc, +NetClientInfo *info, +NetClientState *peer, +const char *model, +const char *name, +NetClientDestructor *destructor, +bool is_datapath) { nc->info = info; nc->model =g_strdup (model);
net/tap.c +40 -8
87 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v21 10/16 migration/fd: fix fd leak when fd is not valid for migration
In reply to a message not in the archiveBoth fd_connect_outgoing() and fd_connect_incoming() own the file descriptor after resolving it from the monitor, but forget to close it when migration_fd_valid() check fails.
--- migration/fd.c | 2 ++ 1 file changed, 2 insertions(+)
migration/fd.c +2 -0
@@ -62,6 +62,7 @@ QIOChannel *fd_connect_outgoing(MigrationState *s, const char *fdname,if (!migration_fd_valid (fd)) {error_setg (errp,"fd: migration to a file is not supported." " Use file: instead." ); +close (fd); goto out; }
@@ -97,6 +98,7 @@ void fd_connect_incoming(const char *fdname, Error **errp)if (!migration_fd_valid (fd)) {error_setg (errp,"fd: migration to a file is not supported." " Use file: instead." ); +close (fd);return ; }
Signature · 1 line
2.43.0
Re: [PATCH v21 10/16] migration/fd: fix fd leak when fd is not valid for migration
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Both
fd_connect_outgoing()andfd_connect_incoming()own the file descriptor after resolving it from the monitor, but forget to close it whenmigration_fd_valid()check fails.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Oops.. thanks for fixing.
Quoted 27 lines
--- migration/fd.c | 2 ++ 1 file changed, 2 insertions(+)migration/fd.c +2
-0@@ -62,6 +62,7 @@ QIOChannel *fd_connect_outgoing(MigrationState *s, const char *fdname,if (!migration_fd_valid (fd)) {error_setg (errp,"fd: migration to a file is not supported." " Use file: instead." ); +close (fd); goto out; }@@ -97,6 +98,7 @@ void fd_connect_incoming(const char *fdname, Error **errp)if (!migration_fd_valid (fd)) {error_setg (errp,"fd: migration to a file is not supported." " Use file: instead." ); +close (fd);return ; }Signature · 1 line
2.43.0
Signature · 1 line
Peter Xu
PATCH v21 11/16 migration/fd: add errp parameter to migration_fd_valid()
In reply to a message not in the archiveLet migration_fd_valid() report the error itself, instead of duplicating the error message in both callers. This also prepares for adding one more check (with a different error message) to the function.
--- migration/fd.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-)
migration/fd.c +6 -12
@@ -37,16 +37,14 @@ static bool fd_is_pipe(int fd)return S_ISFIFO (statbuf.st_mode); } -static bool migration_fd_valid (int fd) +static bool migration_fd_valid (int fd,Error **errp) { -if (fd_is_socket (fd)) { -return true; - } - -if (fd_is_pipe (fd)) { +if (fd_is_socket (fd) ||fd_is_pipe (fd)) {return true; } +error_setg (errp,"fd: migration to a file is not supported." +" Use file: instead." );return false; }
@@ -59,9 +57,7 @@ QIOChannel *fd_connect_outgoing(MigrationState *s, const char *fdname, goto out; } -if (!migration_fd_valid (fd)) { -error_setg (errp,"fd: migration to a file is not supported." -" Use file: instead." ); +if (!migration_fd_valid (fd, errp)) {close (fd); goto out; }
@@ -95,9 +91,7 @@ void fd_connect_incoming(const char *fdname, Error **errp)return ; } -if (!migration_fd_valid (fd)) { -error_setg (errp,"fd: migration to a file is not supported." -" Use file: instead." ); +if (!migration_fd_valid (fd, errp)) {close (fd);return ; }
Signature · 1 line
2.43.0
Re: [PATCH v21 11/16] migration/fd: add errp parameter to migration_fd_valid()
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
Let
migration_fd_valid()report the error itself, instead of duplicating the error message in both callers. This also prepares for adding one more check (with a different error message) to the function.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
Signature · 1 line
Peter Xu
PATCH v21 12/16 migration: check that transfer is UNIX socket when "local" set
In reply to a message not in the archiveAs documented, for "local", the migration channel must be direct UNIX socket connection from source to target (except for CPR_EXEC mode). We can't check for it being "direct", but let's at least check that we deal with UNIX socket.
--- migration/fd.c | 14 ++++++++++++++ migration/migration.c | 13 +++++++++++++ 2 files changed, 27 insertions(+)
migration/fd.c +14 -0
@@ -39,6 +39,20 @@ static bool fd_is_pipe(int fd)static bool migration_fd_valid (int fd,Error **errp) { +if (migrate_local () &&migrate_mode () !=MIG_MODE_CPR_EXEC ) { +struct sockaddr_storage ss; +socklen_t sslen =sizeof (ss); + +if (getsockname (fd, (struct sockaddr *)&ss, &sslen) <0 || + ss.ss_family !=AF_UNIX ) { +error_setg (errp, +"local migration requires a UNIX domain socket channel" ); +return false; + } + +return true; + } +if (fd_is_socket (fd) ||fd_is_pipe (fd)) {return true; }
migration/migration.c +13 -0
@@ -260,6 +260,19 @@ migration_capabilities_and_transport_compatible(MigrationAddress *addr, errp); } +if (migrate_local () &&migrate_mode () !=MIG_MODE_CPR_EXEC && + (addr->transport !=MIGRATION_ADDRESS_TYPE_SOCKET || + (addr->u.socket.type !=SOCKET_ADDRESS_TYPE_UNIX && + addr->u.socket.type !=SOCKET_ADDRESS_TYPE_FD ))) { +/* +* For SOCKET_ADDRESS_TYPE_FD we do check the fd itself later +* in migration_fd_valid(). +*/ +error_setg (errp, +"local migration requires a UNIX domain socket channel" ); +return false; + } +return true; }
Signature · 1 line
2.43.0
Re: [PATCH v21 12/16] migration: check that transfer is UNIX socket when "local" set
Quoted 6 lines · Vladimir Sementsov-Ogievskiy
As documented, for "local", the migration channel must be direct UNIX socket connection from source to target (except for
CPR_EXECmode). We can't check for it being "direct", but let's at least check that we deal with UNIX socket.Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>
I slightly prefer sticking with socket_local_address() to avoid invoking the syscall directly, but I understand your point, likely about unnecessary allocation. It's OK too,
Signature · 1 line
Peter Xu
PATCH v21 13/16 virtio-net: support local migration of backend
In reply to a message not in the archiveNext commit will introduce live-migration (with fd-passing) for TAP net backend. So, now we prepare virtio-net for it
Add virtio-net option local-migration, which is true by default, but false for older machine types, which doesn't support the feature.
We introduce interface for live-migrating backends:
1. ->is_wait_incoming() handler, so that virtio-net knows, that backend is not fully intialized, as it waits for incoming migration stream.
2. MIG_PRI_BACKEND priority: backends should migrate with higher priority than virtio-net, so that we can do final preparations here in post-load handlers and be sure, that backends are already prepared.
--- hw/net/virtio-net.c | 89 +++++++++++++++++++++++++++++++++- include/hw/virtio/virtio-net.h | 1 + include/migration/vmstate.h | 2 + include/net/net.h | 2 + 4 files changed, 93 insertions(+), 1 deletion(-)
hw/net/virtio-net.c +88 -1
@@ -40,6 +40,7 @@#include "migration/misc.h" #include "standard-headers/linux/ethtool.h" #include "system/system.h" +#include "system/runstate.h" #include "system/replay.h" #include "trace.h" #include "monitor/qdev.h"
@@ -3079,7 +3080,17 @@ static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) n->multiqueue = multiqueue;virtio_net_change_num_queues (n, max *2 +1 ); -virtio_net_set_queue_pairs (n); +/* +* virtio_net_set_multiqueue() called from set_features(0) on early +* reset, when peer may wait for incoming (and is not initialized +* yet). +* Don't worry about it: virtio_net_set_queue_pairs() will be called +* later from virtio_net_post_load_device(), and anyway will be +* no-op for local incoming migration with live backend passing. +*/ +if (!n->peers_wait_incoming) { +virtio_net_set_queue_pairs (n); + } }static int virtio_net_pre_load_queues (VirtIODevice *vdev,uint32_t n)
@@ -3108,6 +3119,17 @@ static void virtio_net_get_features(VirtIODevice *vdev, uint64_t *features,virtio_add_feature_ex (features,VIRTIO_NET_F_MAC ); +if (n->peers_wait_incoming) { +/* +* Excessive feature set is OK for early initialization when +* we wait for local incoming migration: actual guest-negotiated +* features will come with migration stream anyway. And we are sure +* that we support same host-features as source, because the backend +* is the same (the same TAP device, for example). +*/ +return ; + } +if (!peer_has_vnet_hdr (n)) {virtio_clear_feature_ex (features,VIRTIO_NET_F_CSUM );virtio_clear_feature_ex (features,VIRTIO_NET_F_HOST_TSO4 );
@@ -3204,6 +3226,7 @@ static int virtio_net_post_load_device(void *opaque, int version_id)VirtIODevice *vdev =VIRTIO_DEVICE (n);int i, link_down;bool has_tunnel_hdr =virtio_has_tunnel_hdr (vdev->guest_features_ex); +Error *local_err =NULL ;trace_virtio_net_post_load_device (); virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
@@ -3261,6 +3284,20 @@ static int virtio_net_post_load_device(void *opaque, int version_id) }virtio_net_commit_rss_config (n); + +/* +* If live-migration is enabled for some backend, than backend +* has already been migrated at higher priority (MIG_PRI_BACKEND) +* and virtio_net_vnet_post_load() has already called +* peer_test_vnet_hdr(). Recompute host_features so that virtio-net +* reflects the capabilities of the restored backend. +*/ +virtio_net_get_features (vdev, &vdev->host_features, &local_err); +if (local_err) { +error_report_err (local_err); +return -EINVAL ; + } +return 0 ; }
@@ -3411,6 +3448,14 @@ static int virtio_net_vnet_post_load(void *opaque, int version_id) {struct VirtIONetMigTmp *tmp = opaque; +/* +* If live-migration is enabled for some backend, than backend +* has already been migrated at higher priority (MIG_PRI_BACKEND), +* so n->has_vnet_hdr can be refreshed from the live backend right +* here. +*/ +peer_test_vnet_hdr (tmp->parent); +if (tmp->has_vnet_hdr && !peer_has_vnet_hdr (tmp->parent)) {error_report ("virtio-net: saved image requires vnet_hdr=on" );return -EINVAL ;
@@ -3890,6 +3935,42 @@ static bool failover_hide_primary_device(DeviceListener *listener,return qatomic_read (&n->failover_primary_hidden); } +static bool virtio_net_check_peers_wait_incoming (VirtIONet *n,bool *waiting, +Error **errp) +{ +bool has_waiting = false; +bool has_not_waiting = false; + +for (int i =0 ; i < n->max_queue_pairs; i++) { +NetClientState *peer = n->nic->ncs[i].peer; +if (!peer) { +continue ; + } + +if (peer->info->is_wait_incoming && + peer->info->is_wait_incoming (peer)) { + has_waiting = true; + }else { + has_not_waiting = true; + } + +if (has_waiting && has_not_waiting) { +error_setg (errp,"Mixed peer states: some peers wait for incoming " +"migration while others don't" ); +return false; + } + } + +if (has_waiting && !runstate_check (RUN_STATE_INMIGRATE )) { +error_setg (errp,"Peers wait for incoming, but it's not an incoming " +"migration." ); +return false; + } + + *waiting = has_waiting; +return true; +} +static void virtio_net_device_realize (DeviceState *dev,Error **errp) {VirtIODevice *vdev =VIRTIO_DEVICE (dev);
@@ -4027,6 +4108,12 @@ static void virtio_net_device_realize(DeviceState *dev, Error **errp) n->nic->ncs[i].do_not_pad = true; } +if (!virtio_net_check_peers_wait_incoming (n, &n->peers_wait_incoming, + errp)) { +virtio_cleanup (vdev); +return ; + } +peer_test_vnet_hdr (n);if (peer_has_vnet_hdr (n)) { n->host_hdr_len =sizeof (struct virtio_net_hdr );
include/hw/virtio/virtio-net.h +1 -0
@@ -230,6 +230,7 @@ struct VirtIONet {struct EBPFRSSContext ebpf_rss;uint32_t nr_ebpf_rss_fds;char **ebpf_rss_fds; +bool peers_wait_incoming; }; size_t virtio_net_handle_ctrl_iov(VirtIODevice *vdev,
include/migration/vmstate.h +2 -0
@@ -178,6 +178,8 @@ typedef enum {MIG_PRI_LOW ,/* Must happen after default */ MIG_PRI_DEFAULT , +MIG_PRI_BACKEND ,/* Must happen before emulated devices, */ +/* e.g. virtio-net */ MIG_PRI_IOMMU ,/* Must happen before PCI devices */ MIG_PRI_PCI_BUS ,/* Must happen before IOMMU */ MIG_PRI_VIRTIO_MEM, /* Must happen before IOMMU */
include/net/net.h +2 -0
@@ -81,6 +81,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 {
@@ -109,6 +110,7 @@ typedef struct NetClientInfo {NetAnnounce *announce;SetSteeringEBPF *set_steering_ebpf;NetCheckPeerType *check_peer_type; +IsWaitIncoming *is_wait_incoming;GetVHostNet *get_vhost_net; } NetClientInfo;
Signature · 1 line
2.43.0
PATCH v21 15/16 net/tap: support local migration with virtio-net
In reply to a message not in the archiveSupport transferring of TAP state (including open fd).
Add new property "x-permit-local-migration", which defines whether local migration is actually permitted for this TAP device.
Note that local-migration is enabled by global "local" migration parameter, but individual devices may have additional options to enable/disable it per device.
The tricky thing is that we need to know whether to call open/connect in TAP initialization code, i.e. we need to know the value of migration parameter "local" when creating the TAP device. For incoming migration, we can know only for TAP devices created with QMP after setting the migration parameter with QMP.
So the full picture is:
On source, to start outgoing "local" migration you need:
- migration parameter "local" set to true
- "x-permit-local-migration" TAP option set to true
If at least one of these options is not set, TAP backend doesn't participate in migration.
On target, things are more difficult:
Same, you need both "local" and "x-permit-local-migration" be set. And same, if one of them is not set, TAP backend is initialized as usual, and doesn't accept any incoming state.
Additionally, if you are going to set "local", it must be set before creating the TAP device. If TAP device created with "local" unset, it initializes as usual. If you enable "local" after it and start incoming migration, it will fail in .pre_load handler of TAP backend.
Moreover, there are interface restrictions: if you create TAP device when QEMU is in INCOMING state, and both "local" and "x-permit-local-migration" set, most of TAP options are not allowed, and script/downscript are required to be explicitly disabled.
--- net/tap.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++-- qapi/net.json | 29 ++++++++- 2 files changed, 189 insertions(+), 7 deletions(-)
net/tap.c +161 -6
@@ -38,12 +38,17 @@#include "monitor/monitor.h" #include "system/runstate.h" #include "system/system.h" +#include "migration/misc.h" #include "qapi/error.h" #include "qemu/cutils.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" #include "qemu/sockets.h" #include "hw/virtio/vhost.h" +#include "hw/core/vmstate-if.h" +#include "migration/vmstate.h" +#include "qom/object.h" +#include "qom/compat-properties.h" #include "net/tap.h" #include "net/util.h"
@@ -71,6 +76,8 @@ static const int kernel_feature_bits[] = { OBJECT_DECLARE_SIMPLE_TYPE(TAPState, TAP_NETDEV) +static const VMStateDescription vmstate_tap; +struct TAPState {Object parent_obj;
@@ -95,6 +102,7 @@ struct TAPState {int queue_index;bool enable_poll_on_resume;VMChangeStateEntry *vmstate; +bool permit_local_migration; }; static void launch_script(const char *setup_script, const char *ifname,
@@ -412,6 +420,8 @@ static void tap_cleanup(NetClientState *nc)tap_write_poll (s, false);close (s->fd); s->fd =-1 ; + +vmstate_unregister (VMSTATE_IF (s), &vmstate_tap, s); }static void tap_poll (NetClientState *nc,bool enable)
@@ -448,6 +458,78 @@ static VHostNetState *tap_get_vhost_net(NetClientState *nc)return s->vhost_net; } +static bool tap_is_wait_incoming (NetClientState *nc) +{ +TAPState *s =container_of (nc, TAPState, nc); +assert (nc->info->type ==NET_CLIENT_DRIVER_TAP ); +return s->fd ==-1 ; +} + +static bool tap_pre_load (void *opaque,Error **errp) +{ +ERRP_GUARD (); +TAPState *s = opaque; + +if (s->fd !=-1 ) { +error_setg (errp, +"TAP is already initialized and cannot receive " +"incoming fd" ); +error_append_hint (errp, +"Migration parameter 'local' must be set" +" before creating the TAP device." ); +return false; + } + +return true; +} + +static bool tap_setup_vhost (TAPState *s,Error **errp); + +static bool tap_post_load (void *opaque,int version_id,Error **errp) +{ +ERRP_GUARD (); +TAPState *s = opaque; + +tap_read_poll (s, true); + +if (s->fd <0 ) { +error_setg (errp,"FD was not loaded during incoming migration" ); +return false; + } + +if (!tap_setup_vhost (s, errp)) { +error_prepend (errp, +"Failed to setup vhost during TAP post-load: " ); +return false; + } + +return true; +} + +static bool tap_needed (void *opaque) +{ +TAPState *s = opaque; + +return s->permit_local_migration &&migrate_local (); +} + +static const VMStateDescription vmstate_tap = { + .name ="net-tap" , + .priority =MIG_PRI_BACKEND , + .pre_load_errp = tap_pre_load, + .post_load_errp = tap_post_load, + .needed = tap_needed, + .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 () + } +};static char *tap_vmstate_if_get_id (VMStateIf *obj) {
@@ -456,17 +538,42 @@ static char *tap_vmstate_if_get_id(VMStateIf *obj)return res; } +static bool tap_get_permit_local_migration_prop (Object *obj,Error **errp) +{ +TAPState *s =TAP_NETDEV (obj); +return s->permit_local_migration; +} + +static void tap_set_permit_local_migration_prop (Object *obj,bool value, +Error **errp) +{ +TAPState *s =TAP_NETDEV (obj); + s->permit_local_migration = value; +} + +static void tap_instance_init (Object *obj) +{ +TAPState *s =TAP_NETDEV (obj); + s->permit_local_migration = false; +} +static void tap_class_init (ObjectClass *klass,const void *data) {VMStateIfClass *vc =VMSTATE_IF_CLASS (klass); vc->get_id = tap_vmstate_if_get_id; + +object_class_property_add_bool (klass,"x-permit-local-migration" , + tap_get_permit_local_migration_prop, + tap_set_permit_local_migration_prop); }static const TypeInfo tap_netdev_info = { .name =TYPE_TAP_NETDEV , .parent =TYPE_OBJECT , .instance_size =sizeof (TAPState), + .instance_init = tap_instance_init, + .instance_post_init = object_apply_compat_props, .class_init = tap_class_init, .interfaces = (const InterfaceInfo []) { {TYPE_VMSTATE_IF },
@@ -499,13 +606,16 @@ 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, };static TAPState *new_tap (NetClientState *peer,const char *model,const char *name, -int queue_index) +int queue_index, +bool has_permit_local_migration, +bool permit_local_migration) {TAPState *s =TAP_NETDEV (object_new (TYPE_TAP_NETDEV ));
@@ -514,6 +624,12 @@ static TAPState *new_tap(NetClientState *peer, s->queue_index = queue_index; +if (has_permit_local_migration) { + s->permit_local_migration = permit_local_migration; + } + +vmstate_register (VMSTATE_IF (s),VMSTATE_INSTANCE_ID_ANY , &vmstate_tap, s); +return s; }
@@ -522,10 +638,14 @@ static TAPState *net_tap_fd_init(NetClientState *peer,const char *name, int fd, int vnet_hdr, - int queue_index) + int queue_index, + bool has_permit_local_migration, + bool permit_local_migration) { NetOffloads ol = {}; -TAPState *s =new_tap (peer, model, name, queue_index); +TAPState *s =new_tap (peer, model, name, queue_index, + has_permit_local_migration, + permit_local_migration); s->fd = fd; s->host_vnet_hdr_len = vnet_hdr ?sizeof (struct virtio_net_hdr ) :0 ;
@@ -762,7 +882,7 @@ int net_init_bridge(const Netdev *netdev, const char *name,close (fd);return -1 ; } - s =net_tap_fd_init (peer,"bridge" , name, fd, vnet_hdr,0 ); + s =net_tap_fd_init (peer,"bridge" , name, fd, vnet_hdr,0 , true, false);qemu_set_info_str (&s->nc,"helper=%s,br=%s" , helper, br);
@@ -842,7 +962,9 @@ static bool net_init_tap_one(const NetdevTapOptions *tap, NetClientState *peer, Error **errp) {TAPState *s =net_tap_fd_init (peer, tap->helper ?"bridge" :"tap" , - name, fd, vnet_hdr, queue_index); + name, fd, vnet_hdr, queue_index, + tap->has_x_permit_local_migration, + tap->x_permit_local_migration);bool sndbuf_required = tap->has_sndbuf;int sndbuf = (tap->has_sndbuf && tap->sndbuf) ?MIN (tap->sndbuf,INT_MAX ) :INT_MAX ;
@@ -990,6 +1112,7 @@ int net_init_tap(const Netdev *netdev, const char *name,/* for the no-fd, no-helper case */ char ifname[128 ];int *fds =NULL , *vhost_fds =NULL ; +bool incoming_fds;assert (netdev->type ==NET_CLIENT_DRIVER_TAP ); tap = &netdev->u.tap;
@@ -1012,6 +1135,23 @@ int net_init_tap(const Netdev *netdev, const char *name,return -1 ; } + incoming_fds = tap->x_permit_local_migration &&migrate_local () && +runstate_check (RUN_STATE_INMIGRATE ); + +if (incoming_fds && + (tap->fd || tap->fds || tap->helper || tap->br || tap->ifname || + tap->has_sndbuf || tap->has_vnet_hdr || + !tap_is_explicit_no_script ("script" , tap->script) || + !tap_is_explicit_no_script ("downscript" , tap->downscript))) { +error_setg (errp,"Local incoming migration of TAP device (-incoming, " +"migration parameter @local is set, " +"TAP parameter @x-permit-local-migration is set) " +"is incompatible with " +"fd=, fds=, helper=, br=, ifname=, sndbuf= and vnet_hdr=, " +"and requires explicit empty script= and downscript=" ); +return -1 ; + } + queues =tap_parse_fds_and_queues (tap, &fds, errp);if (queues <0 ) {return -1 ;
@@ -1030,7 +1170,22 @@ int net_init_tap(const Netdev *netdev, const char *name, goto fail; } -if (fds) { +if (incoming_fds) { +for (i =0 ; i < queues; i++) { +TAPState *s =new_tap (peer,"tap" , name, i, + tap->has_x_permit_local_migration, + tap->x_permit_local_migration); +qemu_set_info_str (&s->nc,"incoming" ); + + 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);
qapi/net.json +28 -1
@@ -437,6 +437,31 @@ # @poll-us: maximum number of microseconds that could be spent on busy # polling for tap (since 2.7) # +# @x-permit-local-migration: permit local migration for this TAP +# backend. When set, local migration is enabled/disabled by +# migration parameter @local for this TAP backend. When unset, +# migration parameter @local is ignored for this TAP backend. +# +# To be able to do incoming local migration of a TAP backend, +# migration parameter @local must be set _before_ creating the +# TAP backend. Otherwise, TAP backend is initialized as usual, +# opening/creating TAP devices in kernel. In this case further +# local incoming migration (with migration parameter @local set +# after creating TAP backend with @x-permit-local-migration +# parameter set) will simply fail. +# +# Moreover, when QEMU is in incoming migration state, migration +# parameter @local is set and @x-permit-local-migration is set, +# the following options are not supported and must not be set: +# @fd, @fds, @helper, @br, @ifname, @sndbuf, @vnet_hdr. +# Additionally, @script and @downscript must be explicitly +# disabled then. +# (default: false, since 11.2) +# +# Features: +# +# @unstable: Member @x-permit-local-migration is experimental. +# # Since: 1.2 ## { 'struct': 'NetdevTapOptions',
@@ -455,7 +480,9 @@ '*vhostfds': 'str', '*vhostforce': 'bool', '*queues': 'uint32', - '*poll-us': 'uint32'} } + '*poll-us': 'uint32', + '*x-permit-local-migration': { + 'type': 'bool', 'features' : [ 'unstable'] } } } ## # @NetdevSocketOptions:
Signature · 1 line
2.43.0
Re: [PATCH v21 15/16] net/tap: support local migration with virtio-net
Quoted 38 lines · Vladimir Sementsov-Ogievskiy
Support transferring of TAP state (including open fd).
Add new property "x-permit-local-migration", which defines whether local migration is actually permitted for this TAP device.
Note that local-migration is enabled by global "local" migration parameter, but individual devices may have additional options to enable/disable it per device.
The tricky thing is that we need to know whether to call open/connect in TAP initialization code, i.e. we need to know the value of migration parameter "local" when creating the TAP device. For incoming migration, we can know only for TAP devices created with QMP after setting the migration parameter with QMP.
So the full picture is:
On source, to start outgoing "local" migration you need:
- migration parameter "local" set to true
- "x-permit-local-migration" TAP option set to trueIf at least one of these options is not set, TAP backend doesn't participate in migration.
On target, things are more difficult:
Same, you need both "local" and "x-permit-local-migration" be set. And same, if one of them is not set, TAP backend is initialized as usual, and doesn't accept any incoming state.
Additionally, if you are going to set "local", it must be set before creating the TAP device. If TAP device created with "local" unset, it initializes as usual. If you enable "local" after it and start incoming migration, it will fail in .pre_load handler of TAP backend.
Quoted 8 lines
Moreover, there are interface restrictions: if you create TAP device when QEMU is in INCOMING state, and both "local" and "x-permit-local-migration" set, most of TAP options are not allowed, and script/downscript are required to be explicitly disabled.
Signed-off-by Vladimir Sementsov-Ogievskiy <[email protected]>Reviewed-by Ben Chaney <[email protected]>
QAPI schema
PATCH v21 16/16 tests/functional: add test_tap_migration
In reply to a message not in the archiveAdd 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 | 461 ++++++++++++++++++ 2 files changed, 462 insertions(+) create mode 100755 tests/functional/x86_64/test_tap_migration.py
tests/functional/x86_64/meson.build +1 -0
@@ -47,4 +47,5 @@ tests_x86_64_system_thorough = [ 'virtio_balloon', 'virtio_gpu', 'rebuild_vmfd', + 'tap_migration', ]
tests/functional/x86_64/test_tap_migration.py new file +461 -0
461 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v21 00/16] virtio-net: live-TAP local migration
In reply to a message not in the archiveping)
Quoted 417 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here is a migration for TAP net backend, including its properties and open fds.
With this new feature, 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.
v21:
09: wording, add r-b and a-b by Markus and Peter
10-12: new, reworkd v20/10
15: rename x-local-migration-supported to x-permit-local-migration,
small docs improvements, keep r-bv21 is pushed to
https://gitlab.com/vsementsov/qemu.git
tag: up-tap-fd-migration-with-bk-opt-v21To run the test, use sudo, as test needs to configure TAP device:
sudo PYTHONPATH=python:tests/functional \ QEMU_TEST_QEMU_BINARY=$PWD/build/qemu-system-x86_64 \ MESON_BUILD_ROOT=$PWD/build \ ./build/pyvenv/bin/python3 tests/functional/x86_64/test_tap_migration.pyOr, to test the feature by hand, you may follow the instruction at the and of this document (search for "How to run local fd migration by hand" caption).
Why this series, and why do it this way
=======================================This came up in the v19 discussion, so it's summarized here to avoid repeating the same questions.
There was a large discussion at v19, covered many questions. I'm summarizing them below.v19: https://lore.kernel.org/qemu-devel/[email protected]/
Why local migration matters
---------------------------Local migration (migrating to a new QEMU process on the same host) is not a rare operation for in the cloud. When upgrading QEMU, _all_ VMs on _all_ hosts are locally migrated. The freeze time of such update affects _all_ customers, and some are sensitive to even small freezes. Remote migrations, by contrast, are done mostly to release a physical server for service/redeploy, which is relatively infrequent.
The aggregate impact is large, and reducing the per-VM downtime is significant.
Why not just create a new TAP and switch to it
----------------------------------------------It is possible to create a new TAP device on the target and migrate the usual way. But the overhead is in switching to the new TAP in our cloud virtual networking component. Could the switching itself be optimized to always be very fast? Probably yes, but we chose a different approach: drop extra components from the process and avoid any switching at all. Both the cloud networking component and the storage backend (with the vhost-user-blk live-update series) don't even know that QEMU is migrating. This gives a simpler design (fewer components involved) and shows good results -- several times reduction in freeze time.
Also, on hosts with many CPUs, TAP queues allocate a noticeable amount of RAM, so having 2x queues during migration is an overhead.
Why not use a persistent TAP (open the same TAP on both source and target)
-------------------------------------------------------------------------First, same memory overhead like with "new TAP" case.
Second,
MAX_TAP_QUEUESis 256 in the kernel; if the source already has more
than 128 queues open, the target cannot open the same device.[Note also, that such persistent TAP migration will not "just work", it also needs QEMU changes like patch 12/15 to avoid packet lost]
Why not pass FDs externally (e.g. from libvirt / management tool)
------------------------------------------------------------------ The management tool would need to store (or retrieve from source QEMU) and pass these FDs, requiring it to know about FDs that QEMU already owns. Libvirt may restart, and that shouldn't break migration.
- Adding an API to retrieve FDs from QEMU just to pass them to target QEMU is not a clearer solution.
- QEMU owns the TAP fd and has full knowledge of its state. Pushing this responsibility to an external tool means the tool needs to understand QEMU internals -- that's a worse separation of concerns.
- QEMU already passes FDs through migration for VFIO devices (CPR), we do continue this practiceLooking at wider picture, where we want to migrate not only TAP, but also vfio devices (already implemented as CPR migration, but may be updated to use similar approach as in this series, to use one migration channel), vhost-user-blk (my another series in flight) and vhost-user-fs (not yet published), vhost-vsock migration ("[PATCH v3 0/7] migration/cpr: support vhost-vsock devices" in flight-series from Virtuozzo), it seems to be a good generic approach: simply pass backends (including open FDs) to the target, not involving mgmt. Qemu has full knowledge about these FDs and owns the whole state. Migrating them in QEMU looks correct.
Why not use the cpr-transfer approach (pass FDs before device creation)
-----------------------------------------------------------------------The CPR approach passes FDs _before_ any devices are created on the target. While this avoids touching frontend code (patch 11/15), it means the target starts using (calling ioctls on) FDs that are still actively used by the running source QEMU. That works for TAP, but it does _not_ work with vhost-user-blk, where sharing the fd between source and running target can break things. My series uses the same approach for both net and storage, and it also works with CPR (see below).
That's not saying about "local" approach provides simpler user experience than cpr-transfer:
- no need in secondary migration channel
- no need to work with unavailable QMP for target process until "migrate" on sourceDoes this work with CPR?
------------------------Yes. For cpr-transfer, TAP migrates through the main migration channel like other (non-CPR) devices. For cpr-exec, TAP FDs don't have
FD_CLOEXECset, so they are inherited by the new process as-is; the same core mechanism (load_fd/save_fdinmigration/vmstate-types.c) is used. The 'local' migration parameter documentation now also covers cpr-exec mode (skip the UNIX-socket requirement in that case). New test cases exercising both cpr-transfer and cpr-exec with TAP local migration will be sent as separate series.How to run local fd migration by hand
=====================================The walkthrough uses four terminals:
source-cmd -- source VM console (serial output, guest login) source-qmp -- QMP connection to the source QEMU target-cmd -- target VM console (serial output, guest login after migration) target-qmp -- QMP connection to the target QEMU1. Prerequisites
----------------QEMU=/path/to/your/build/qemu-system-x86_64# download same image as in testwget -O /tmp/alpine.iso "https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/x86_64/alpine-standard-3.22.1-x86_64.iso"# prepare tap device (be careful to not break your own networks)sudo ip tuntap add dev tap0 mode tap
multi_queuesudo ip addr add 192.168.100.1/24 dev tap0 sudo ip link set tap0 up2. Start source VM
------------------In source-cmd, run:
$QEMU \ -name source \ -machine q35 \ -accel kvm \ -m 1G \ -object memory-backend-file,id=ram0,size=1G,mem-path=/dev/shm/qemu_migration_test,share=on \ -machine memory-backend=ram0 \ -drive file=/tmp/alpine.iso,media=cdrom,format=raw \ -device pcie-pci-bridge,id=pci.1,bus=pcie.0 \ -serial stdio \ -nographic \ -S \ -qmp unix:/tmp/qmp-source.sock,server=on,wait=offNote: the netdev and virtio-net device are added via QMP below for symmetry with the target. On the source you could also pass them on the command line (with x-permit-local-migration=on).
In source-qmp, connect and add the TAP netdev and virtio-net device:
socat - UNIX-CONNECT:/tmp/qmp-source.sock {"execute": "qmp_capabilities"} {"execute": "netdev_add", "arguments": { "id": "netdev.1", "type": "tap", "ifname": "tap0", "queues": 4, "vnet_hdr": true, "script": "no", "downscript": "no", "x-permit-local-migration": true }} {"execute": "device_add", "arguments": { "driver": "virtio-net-pci", "id": "vnet.1", "netdev": "netdev.1", "bus": "pci.1", "mq": true, "vectors": 18, "romfile": "", "disable-legacy": "off" }} {"execute": "cont"}Wait for Alpine to boot in source-cmd. When you see the login prompt, log in as root (no password):
localhost login: root
Configure the guest network:
ip addr add 192.168.100.2/24 dev eth0
ip link set eth0 upVerify connectivity from the guest:
ping -c 3 192.168.100.1And from the host (in any spare terminal):
ping -c 3 192.168.100.23. Start target VM
------------------The TAP netdev must be created via QMP after enabling the "local" migration parameter -- the target will not open tap0 itself; instead it will receive the TAP file descriptors from the source over the migration channel.
In target-cmd, run:
$QEMU \ -name target \ -machine q35 \ -accel kvm \ -m 1G \ -object memory-backend-file,id=ram0,size=1G,mem-path=/dev/shm/qemu_migration_test,share=on \ -machine memory-backend=ram0 \ -drive file=/tmp/alpine.iso,media=cdrom,format=raw \ -device pcie-pci-bridge,id=pci.1,bus=pcie.0 \ -serial stdio \ -nographic \ -qmp unix:/tmp/qmp-target.sock,server=on,wait=off \ -incoming deferIn target-qmp, connect, enable local migration, and add the TAP netdev and virtio-net device. The "local" parameter must be set before creating the TAP. Do not pass ifname/fd -- the fd will arrive via the migration channel:
socat - UNIX-CONNECT:/tmp/qmp-target.sock {"execute": "qmp_capabilities"} {"execute": "migrate-set-capabilities", "arguments": { "capabilities": [ {"capability": "events", "state": true}, {"capability": "x-ignore-shared", "state": true} ] }} {"execute": "migrate-set-parameters", "arguments": {"local": true}} {"execute": "netdev_add", "arguments": { "id": "netdev.1", "type": "tap", "queues": 4, "script": "", "downscript": "", "x-permit-local-migration": true }} {"execute": "device_add", "arguments": { "driver": "virtio-net-pci", "id": "vnet.1", "netdev": "netdev.1", "bus": "pci.1", "mq": true, "vectors": 18, "romfile": "", "disable-legacy": "off" }}4. Start migration
------------------In target-qmp, tell the target to listen for the incoming migration:
{"execute": "migrate-incoming", "arguments": {"uri": "unix:/tmp/migration.sock"}}In source-qmp, configure migration capabilities and parameters:
{"execute": "migrate-set-capabilities", "arguments": { "capabilities": [ {"capability": "events", "state": true}, {"capability": "x-ignore-shared", "state": true} ] }} {"execute": "migrate-set-parameters", "arguments": {"local": true}}In source-qmp, trigger the migration:
{"execute": "migrate", "arguments": {"uri": "unix:/tmp/migration.sock"}}Poll migration status until it completes (source-qmp):
{"execute": "query-migrate"} # repeat until "status" == "completed"Or just wait for the MIGRATION event that QEMU emits automatically:
# {"event": "MIGRATION", "data": {"status": "completed"}, ...}Once the source reports "completed", resume the target VM (target-qmp):
{"execute": "cont"}The target VM is now running with the migrated state and the TAP file descriptors that were passed from the source. Still, target console (in target-cmd) may still be empty, until you at least press Enter in it.
Verify that the guest is still reachable from the host:
ping -c 3 192.168.100.2And from inside the guest in target-cmd:
ping -c 3 192.168.100.15. Cleanup
----------Shut down the target VM (target-qmp):
{"execute": "quit"}Shut down the source VM (source-qmp):
{"execute": "quit"}Remove the TAP device:
sudo ip tuntap del tap0 mode tap
multi_queueRemove the shared memory file:
rm
/dev/shm/qemu_migration_testRemove leftover sockets if they still exist:
rm -f
/tmp/migration.sock/tmp/qmp-source.sock/tmp/qmp-target.sockVladimir Sementsov-Ogievskiy (16):
net/tap: reworktap_parse_script
net/tap: improve script/downscript options documentation net/tap: deprecate "no" as special value for script/downscript net/tap: move vhost-netopen()calls totap_parse_vhost_fds()net/tap: move vhost initialization totap_setup_vhost()net/tap: usecontainer_ofinstead ofDO_UPCASTnet/tap: QOMify tap backend
net/tap: addTYPE_VMSTATE_IFinterface
qapi: add local migration parameter
migration/fd: fix fd leak when fd is not valid for migration migration/fd: add errp parameter tomigration_fd_valid()
migration: check that transfer is UNIX socket when "local" set
virtio-net: support local migration of backend net/tap: disable read polling for stopped VM net/tap: support local migration with virtio-net tests/functional: addtest_tap_migrationdocs/about/deprecated.rst | 18 + docs/system/i386/microvm.rst | 4 +- docs/system/i386/xenpvh.rst | 2 +- docs/system/ppc/ppce500.rst | 4 +- docs/system/riscv/microchip-icicle-kit.rst | 2 +- docs/system/riscv/sifive_u.rst | 2 +- hw/net/virtio-net.c | 89 +++- include/hw/virtio/virtio-net.h | 1 + include/migration/misc.h | 2 + include/migration/vmstate.h | 2 + include/net/net.h | 9 + include/net/tap.h | 2 + migration/fd.c | 28 +- migration/migration.c | 13 + migration/options.c | 18 +- net/net.c | 14 +- net/tap.c | 446 ++++++++++++++--- qapi/migration.json | 21 +- qapi/net.json | 47 +- qemu-options.hx | 13 +- tests/functional/x86_64/meson.build | 1 + tests/functional/x86_64/test_tap_migration.py | 461 ++++++++++++++++++ 22 files changed, 1081 insertions(+), 118 deletions(-)create mode 100755
tests/functional/x86_64/test_tap_migration.py
Signature · 2 lines
Best regards, Vladimir
Re: [PATCH v21 00/16] virtio-net: live-TAP local migration
In reply to a message not in the archiveOne more gently ping)
Seems, all questions discussed. Could we proceed with these series?
Quoted 417 lines · Vladimir Sementsov-Ogievskiy
Hi all!
Here is a migration for TAP net backend, including its properties and open fds.
With this new feature, 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.
v21:
09: wording, add r-b and a-b by Markus and Peter
10-12: new, reworkd v20/10
15: rename x-local-migration-supported to x-permit-local-migration,
small docs improvements, keep r-bv21 is pushed to
https://gitlab.com/vsementsov/qemu.git
tag: up-tap-fd-migration-with-bk-opt-v21To run the test, use sudo, as test needs to configure TAP device:
sudo PYTHONPATH=python:tests/functional \ QEMU_TEST_QEMU_BINARY=$PWD/build/qemu-system-x86_64 \ MESON_BUILD_ROOT=$PWD/build \ ./build/pyvenv/bin/python3 tests/functional/x86_64/test_tap_migration.pyOr, to test the feature by hand, you may follow the instruction at the and of this document (search for "How to run local fd migration by hand" caption).
Why this series, and why do it this way
=======================================This came up in the v19 discussion, so it's summarized here to avoid repeating the same questions.
There was a large discussion at v19, covered many questions. I'm summarizing them below.v19: https://lore.kernel.org/qemu-devel/[email protected]/
Why local migration matters
---------------------------Local migration (migrating to a new QEMU process on the same host) is not a rare operation for in the cloud. When upgrading QEMU, _all_ VMs on _all_ hosts are locally migrated. The freeze time of such update affects _all_ customers, and some are sensitive to even small freezes. Remote migrations, by contrast, are done mostly to release a physical server for service/redeploy, which is relatively infrequent.
The aggregate impact is large, and reducing the per-VM downtime is significant.
Why not just create a new TAP and switch to it
----------------------------------------------It is possible to create a new TAP device on the target and migrate the usual way. But the overhead is in switching to the new TAP in our cloud virtual networking component. Could the switching itself be optimized to always be very fast? Probably yes, but we chose a different approach: drop extra components from the process and avoid any switching at all. Both the cloud networking component and the storage backend (with the vhost-user-blk live-update series) don't even know that QEMU is migrating. This gives a simpler design (fewer components involved) and shows good results -- several times reduction in freeze time.
Also, on hosts with many CPUs, TAP queues allocate a noticeable amount of RAM, so having 2x queues during migration is an overhead.
Why not use a persistent TAP (open the same TAP on both source and target)
-------------------------------------------------------------------------First, same memory overhead like with "new TAP" case.
Second,
MAX_TAP_QUEUESis 256 in the kernel; if the source already has more
than 128 queues open, the target cannot open the same device.[Note also, that such persistent TAP migration will not "just work", it also needs QEMU changes like patch 12/15 to avoid packet lost]
Why not pass FDs externally (e.g. from libvirt / management tool)
------------------------------------------------------------------ The management tool would need to store (or retrieve from source QEMU) and pass these FDs, requiring it to know about FDs that QEMU already owns. Libvirt may restart, and that shouldn't break migration.
- Adding an API to retrieve FDs from QEMU just to pass them to target QEMU is not a clearer solution.
- QEMU owns the TAP fd and has full knowledge of its state. Pushing this responsibility to an external tool means the tool needs to understand QEMU internals -- that's a worse separation of concerns.
- QEMU already passes FDs through migration for VFIO devices (CPR), we do continue this practiceLooking at wider picture, where we want to migrate not only TAP, but also vfio devices (already implemented as CPR migration, but may be updated to use similar approach as in this series, to use one migration channel), vhost-user-blk (my another series in flight) and vhost-user-fs (not yet published), vhost-vsock migration ("[PATCH v3 0/7] migration/cpr: support vhost-vsock devices" in flight-series from Virtuozzo), it seems to be a good generic approach: simply pass backends (including open FDs) to the target, not involving mgmt. Qemu has full knowledge about these FDs and owns the whole state. Migrating them in QEMU looks correct.
Why not use the cpr-transfer approach (pass FDs before device creation)
-----------------------------------------------------------------------The CPR approach passes FDs _before_ any devices are created on the target. While this avoids touching frontend code (patch 11/15), it means the target starts using (calling ioctls on) FDs that are still actively used by the running source QEMU. That works for TAP, but it does _not_ work with vhost-user-blk, where sharing the fd between source and running target can break things. My series uses the same approach for both net and storage, and it also works with CPR (see below).
That's not saying about "local" approach provides simpler user experience than cpr-transfer:
- no need in secondary migration channel
- no need to work with unavailable QMP for target process until "migrate" on sourceDoes this work with CPR?
------------------------Yes. For cpr-transfer, TAP migrates through the main migration channel like other (non-CPR) devices. For cpr-exec, TAP FDs don't have
FD_CLOEXECset, so they are inherited by the new process as-is; the same core mechanism (load_fd/save_fdinmigration/vmstate-types.c) is used. The 'local' migration parameter documentation now also covers cpr-exec mode (skip the UNIX-socket requirement in that case). New test cases exercising both cpr-transfer and cpr-exec with TAP local migration will be sent as separate series.How to run local fd migration by hand
=====================================The walkthrough uses four terminals:
source-cmd -- source VM console (serial output, guest login) source-qmp -- QMP connection to the source QEMU target-cmd -- target VM console (serial output, guest login after migration) target-qmp -- QMP connection to the target QEMU1. Prerequisites
----------------QEMU=/path/to/your/build/qemu-system-x86_64# download same image as in testwget -O /tmp/alpine.iso "https://dl-cdn.alpinelinux.org/alpine/v3.22/releases/x86_64/alpine-standard-3.22.1-x86_64.iso"# prepare tap device (be careful to not break your own networks)sudo ip tuntap add dev tap0 mode tap
multi_queuesudo ip addr add 192.168.100.1/24 dev tap0 sudo ip link set tap0 up2. Start source VM
------------------In source-cmd, run:
$QEMU \ -name source \ -machine q35 \ -accel kvm \ -m 1G \ -object memory-backend-file,id=ram0,size=1G,mem-path=/dev/shm/qemu_migration_test,share=on \ -machine memory-backend=ram0 \ -drive file=/tmp/alpine.iso,media=cdrom,format=raw \ -device pcie-pci-bridge,id=pci.1,bus=pcie.0 \ -serial stdio \ -nographic \ -S \ -qmp unix:/tmp/qmp-source.sock,server=on,wait=offNote: the netdev and virtio-net device are added via QMP below for symmetry with the target. On the source you could also pass them on the command line (with x-permit-local-migration=on).
In source-qmp, connect and add the TAP netdev and virtio-net device:
socat - UNIX-CONNECT:/tmp/qmp-source.sock {"execute": "qmp_capabilities"} {"execute": "netdev_add", "arguments": { "id": "netdev.1", "type": "tap", "ifname": "tap0", "queues": 4, "vnet_hdr": true, "script": "no", "downscript": "no", "x-permit-local-migration": true }} {"execute": "device_add", "arguments": { "driver": "virtio-net-pci", "id": "vnet.1", "netdev": "netdev.1", "bus": "pci.1", "mq": true, "vectors": 18, "romfile": "", "disable-legacy": "off" }} {"execute": "cont"}Wait for Alpine to boot in source-cmd. When you see the login prompt, log in as root (no password):
localhost login: root
Configure the guest network:
ip addr add 192.168.100.2/24 dev eth0
ip link set eth0 upVerify connectivity from the guest:
ping -c 3 192.168.100.1And from the host (in any spare terminal):
ping -c 3 192.168.100.23. Start target VM
------------------The TAP netdev must be created via QMP after enabling the "local" migration parameter -- the target will not open tap0 itself; instead it will receive the TAP file descriptors from the source over the migration channel.
In target-cmd, run:
$QEMU \ -name target \ -machine q35 \ -accel kvm \ -m 1G \ -object memory-backend-file,id=ram0,size=1G,mem-path=/dev/shm/qemu_migration_test,share=on \ -machine memory-backend=ram0 \ -drive file=/tmp/alpine.iso,media=cdrom,format=raw \ -device pcie-pci-bridge,id=pci.1,bus=pcie.0 \ -serial stdio \ -nographic \ -qmp unix:/tmp/qmp-target.sock,server=on,wait=off \ -incoming deferIn target-qmp, connect, enable local migration, and add the TAP netdev and virtio-net device. The "local" parameter must be set before creating the TAP. Do not pass ifname/fd -- the fd will arrive via the migration channel:
socat - UNIX-CONNECT:/tmp/qmp-target.sock {"execute": "qmp_capabilities"} {"execute": "migrate-set-capabilities", "arguments": { "capabilities": [ {"capability": "events", "state": true}, {"capability": "x-ignore-shared", "state": true} ] }} {"execute": "migrate-set-parameters", "arguments": {"local": true}} {"execute": "netdev_add", "arguments": { "id": "netdev.1", "type": "tap", "queues": 4, "script": "", "downscript": "", "x-permit-local-migration": true }} {"execute": "device_add", "arguments": { "driver": "virtio-net-pci", "id": "vnet.1", "netdev": "netdev.1", "bus": "pci.1", "mq": true, "vectors": 18, "romfile": "", "disable-legacy": "off" }}4. Start migration
------------------In target-qmp, tell the target to listen for the incoming migration:
{"execute": "migrate-incoming", "arguments": {"uri": "unix:/tmp/migration.sock"}}In source-qmp, configure migration capabilities and parameters:
{"execute": "migrate-set-capabilities", "arguments": { "capabilities": [ {"capability": "events", "state": true}, {"capability": "x-ignore-shared", "state": true} ] }} {"execute": "migrate-set-parameters", "arguments": {"local": true}}In source-qmp, trigger the migration:
{"execute": "migrate", "arguments": {"uri": "unix:/tmp/migration.sock"}}Poll migration status until it completes (source-qmp):
{"execute": "query-migrate"} # repeat until "status" == "completed"Or just wait for the MIGRATION event that QEMU emits automatically:
# {"event": "MIGRATION", "data": {"status": "completed"}, ...}Once the source reports "completed", resume the target VM (target-qmp):
{"execute": "cont"}The target VM is now running with the migrated state and the TAP file descriptors that were passed from the source. Still, target console (in target-cmd) may still be empty, until you at least press Enter in it.
Verify that the guest is still reachable from the host:
ping -c 3 192.168.100.2And from inside the guest in target-cmd:
ping -c 3 192.168.100.15. Cleanup
----------Shut down the target VM (target-qmp):
{"execute": "quit"}Shut down the source VM (source-qmp):
{"execute": "quit"}Remove the TAP device:
sudo ip tuntap del tap0 mode tap
multi_queueRemove the shared memory file:
rm
/dev/shm/qemu_migration_testRemove leftover sockets if they still exist:
rm -f
/tmp/migration.sock/tmp/qmp-source.sock/tmp/qmp-target.sockVladimir Sementsov-Ogievskiy (16):
net/tap: reworktap_parse_script
net/tap: improve script/downscript options documentation net/tap: deprecate "no" as special value for script/downscript net/tap: move vhost-netopen()calls totap_parse_vhost_fds()net/tap: move vhost initialization totap_setup_vhost()net/tap: usecontainer_ofinstead ofDO_UPCASTnet/tap: QOMify tap backend
net/tap: addTYPE_VMSTATE_IFinterface
qapi: add local migration parameter
migration/fd: fix fd leak when fd is not valid for migration migration/fd: add errp parameter tomigration_fd_valid()
migration: check that transfer is UNIX socket when "local" set
virtio-net: support local migration of backend net/tap: disable read polling for stopped VM net/tap: support local migration with virtio-net tests/functional: addtest_tap_migrationdocs/about/deprecated.rst | 18 + docs/system/i386/microvm.rst | 4 +- docs/system/i386/xenpvh.rst | 2 +- docs/system/ppc/ppce500.rst | 4 +- docs/system/riscv/microchip-icicle-kit.rst | 2 +- docs/system/riscv/sifive_u.rst | 2 +- hw/net/virtio-net.c | 89 +++- include/hw/virtio/virtio-net.h | 1 + include/migration/misc.h | 2 + include/migration/vmstate.h | 2 + include/net/net.h | 9 + include/net/tap.h | 2 + migration/fd.c | 28 +- migration/migration.c | 13 + migration/options.c | 18 +- net/net.c | 14 +- net/tap.c | 446 ++++++++++++++--- qapi/migration.json | 21 +- qapi/net.json | 47 +- qemu-options.hx | 13 +- tests/functional/x86_64/meson.build | 1 + tests/functional/x86_64/test_tap_migration.py | 461 ++++++++++++++++++ 22 files changed, 1081 insertions(+), 118 deletions(-)create mode 100755
tests/functional/x86_64/test_tap_migration.py
Signature · 2 lines
Best regards, Vladimir