Support live migration for qemu-vdagent chardev
cover Support live migration for qemu-vdagent chardev
From: Hyman Huang <[email protected]>
Our goal is to migrate VMs that are configured with qemu-vdagent-typed chardev while allowing the agent to continue working without having to restart the service in guest.
Let's justify which fields should be taken into account for struct VDAgentChardev.
struct VDAgentChardev {
Chardev parent;
/* config */
bool mouse;
bool clipboard;
/* guest vdagent */
uint32_t caps;
VDIChunkHeader chunk;
uint32_t chunksize;
uint8_t *msgbuf;
uint32_t msgsize;
uint8_t *xbuf;
uint32_t xoff, xsize;
Buffer outbuf;
/* mouse */
DeviceState mouse_dev;
uint32_t mouse_x;
uint32_t mouse_y;
uint32_t mouse_btn;
uint32_t mouse_display;
QemuInputHandlerState *mouse_hs;
/* clipboard */
QemuClipboardPeer cbpeer;
uint32_t last_serial[QEMU_CLIPBOARD_SELECTION__COUNT];
uint32_t cbpending[QEMU_CLIPBOARD_SELECTION__COUNT];
};parent:
No dynamic information is generated. skip migrating.
mouse, clipboard:
The mouse and clipboard should be set up identically on both sides. Skip migrating.
caps:
Store the negotiated caps between the client and the guest. Should migrate.
chunk, ... outbuf:
The spice agent protocol's message transportation between the client and the guest is implemented using all of these fields, however the message loss can be tolerated by guests because the issue may occur in the real world as well.
Could skip migrating.
mouse_dev, ... mouse_hs:
The mouse state can be reset after a live migration since the agent working inside the guest does not heavily depend on them. Could skip migrating
cbpeer:
Since the cbpeer would lose the data it references to if the qemu clipboard data was not migrated, this field can also be initialized after live migration.
Could skip migrating
last_serial, cbpending:
It is necessary for the agent to function after live migration. Should migrate.
For the last_serial, saving & loading its value to make ensure the client receives the most recent clipboard data from the guest after live migration.
For the cbpending, saving & loading its value aims to inform the guest that the clipboard has been released and is now empty in case that the guest acts strangely while supposing that the requested data can be properly retrieved.
To summarize, all we need to do is migrate the caps, last_serial and cbpendings fields of the struct VDAgentChardev,
Please review, thanks
Yong
Hyman Huang (3):
vdagent: Wrap vdagent_register_to_qemu_clipboard function
vdagent: Set up mouse and clipboard after live migration
vdagent: Drop blocker to support migration
ui/trace-events | 1 + ui/vdagent.c | 102 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 18 deletions(-)
Signature · 1 line
2.27.0
RFC 1/3 vdagent: Wrap vdagent_register_to_qemu_clipboard function
From: Hyman Huang <[email protected]>
For direct use in the upcoming commit, wrap the vdagent registry logic as vdagent_register_to_qemu_clipboard.
Meanwhile, add a trace event for vdagent_recv_caps.
--- ui/trace-events | 1 + ui/vdagent.c | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-)
ui/trace-events +1 -0
@@ -140,6 +140,7 @@ vdagent_send(const char *name) "msg %s" vdagent_send_empty_clipboard(void) "" vdagent_recv_chunk(uint32_t size) "size %d" vdagent_recv_msg(const char *name, uint32_t size) "msg %s, size %d" +vdagent_recv_caps(uint32_t caps) "received caps %u" vdagent_peer_cap(const char *name) "cap %s" vdagent_cb_grab_selection(const char *name) "selection %s" vdagent_cb_grab_discard(const char *name, int cur, int recv) "selection %s, cur:%d recv:%d"
ui/vdagent.c +16 -7
@@ -694,6 +694,18 @@ static void vdagent_chr_open(Chardev *chr, *be_opened = true; } +static void vdagent_register_to_qemu_clipboard (VDAgentChardev *vd) +{ +if (vd->cbpeer.notifier.notify ==NULL ) { +qemu_clipboard_reset_serial (); + + vd->cbpeer.name ="vdagent" ; + vd->cbpeer.notifier.notify = vdagent_clipboard_notify; + vd->cbpeer.request = vdagent_clipboard_request; +qemu_clipboard_peer_register (&vd->cbpeer); + } +} +static void vdagent_chr_recv_caps (VDAgentChardev *vd,VDAgentMessage *msg) {VDAgentAnnounceCapabilities *caps = (void *)msg->data;
@@ -711,6 +723,8 @@ static void vdagent_chr_recv_caps(VDAgentChardev *vd, VDAgentMessage *msg) } vd->caps = caps->caps[0 ]; +trace_vdagent_recv_caps (vd->caps); +if (caps->request) {vdagent_send_caps (vd, false); }
@@ -720,13 +734,8 @@ static void vdagent_chr_recv_caps(VDAgentChardev *vd, VDAgentMessage *msg)memset (vd->last_serial,0 ,sizeof (vd->last_serial)); -if (have_clipboard (vd) && vd->cbpeer.notifier.notify ==NULL ) { -qemu_clipboard_reset_serial (); - - vd->cbpeer.name ="vdagent" ; - vd->cbpeer.notifier.notify = vdagent_clipboard_notify; - vd->cbpeer.request = vdagent_clipboard_request; -qemu_clipboard_peer_register (&vd->cbpeer); +if (have_clipboard (vd)) { +vdagent_register_to_qemu_clipboard (vd); } }
Signature · 1 line
2.27.0
RFC 2/3 vdagent: Set up mouse and clipboard after live migration
In reply to [email protected]From: Hyman Huang <[email protected]>
The struct VDAgentChardev's caps, last_serial, and cbpending fields need to be migrated in order to allow live migration for vdagent. And the clipboard and mouse should be configured to correspond with the previously negotiated caps on the destination.
--- ui/vdagent.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+)
ui/vdagent.c +68 -0
@@ -7,6 +7,7 @@#include "qemu/units.h" #include "hw/qdev-core.h" #include "migration/blocker.h" +#include "migration/vmstate.h" #include "ui/clipboard.h" #include "ui/console.h" #include "ui/input.h"
@@ -912,6 +913,72 @@ static void vdagent_chr_parse(QemuOpts *opts, ChardevBackend *backend, cfg->clipboard =qemu_opt_get_bool (opts,"clipboard" ,VDAGENT_CLIPBOARD_DEFAULT ); } +static void vdagent_release_clipboard_all_types (VDAgentChardev *vd, +QemuClipboardSelection s) +{ +uint32_t type; + +for (type =0 ; type <QEMU_CLIPBOARD_TYPE__COUNT ; type++) { +if (vd->cbpending[s] & (1 << type)) { + vd->cbpending[s] &= ~(1 << type); + g_autofree VDAgentMessage *msg = +g_malloc0 (sizeof (VDAgentMessage) +sizeof (uint32_t )); + +uint8_t *selection = msg->data; + *selection = s; + msg->size +=sizeof (uint32_t ); + msg->type =VD_AGENT_CLIPBOARD_RELEASE ; + +vdagent_send_msg (vd, msg); + } + } +} + +static int vdagent_post_load (void *opaque,int version_id) +{ +VDAgentChardev *vd =QEMU_VDAGENT_CHARDEV (opaque); +QemuClipboardSelection s =QEMU_CLIPBOARD_SELECTION_CLIPBOARD ; + +if (vd->caps) { +if (have_mouse (vd)) { + vd->mouse_hs = +qemu_input_handler_register (&vd->mouse_dev, + &vdagent_mouse_handler); +if (vd->mouse_hs) { +qemu_input_handler_activate (vd->mouse_hs); + } + } + +if (have_clipboard (vd)) { +vdagent_register_to_qemu_clipboard (vd); +if (have_selection (vd)) { +for (; s <QEMU_CLIPBOARD_SELECTION__COUNT ; s++) { +vdagent_release_clipboard_all_types (vd, s); + } + }else { +vdagent_release_clipboard_all_types (vd, s); + } + } + } + +return 0 ; +} + +static const VMStateDescription vmstate_vdagent = { + .name ="vdagent" , + .version_id =1 , + .minimum_version_id =1 , + .post_load = vdagent_post_load, + .fields = (VMStateField []){ +VMSTATE_UINT32 (caps, VDAgentChardev), +VMSTATE_UINT32_ARRAY (last_serial, VDAgentChardev, +QEMU_CLIPBOARD_SELECTION__COUNT ), +VMSTATE_UINT32_ARRAY (cbpending, VDAgentChardev, +QEMU_CLIPBOARD_SELECTION__COUNT ), +VMSTATE_END_OF_LIST () + } +}; +/* ------------------------------------------------------------------ */ static void vdagent_chr_class_init (ObjectClass *oc,void *data)
@@ -930,6 +997,7 @@ static void vdagent_chr_init(Object *obj)VDAgentChardev *vd =QEMU_VDAGENT_CHARDEV (obj);buffer_init (&vd->outbuf,"vdagent-outbuf" ); +vmstate_register (NULL ,0 , &vmstate_vdagent, vd);error_setg (&vd->migration_blocker,"The vdagent chardev doesn't yet support migration" ); }
Signature · 1 line
2.27.0
RFC 3/3 vdagent: Drop blocker to support migration
In reply to [email protected]From: Hyman Huang <[email protected]>
--- ui/vdagent.c | 11 ----------- 1 file changed, 11 deletions(-)
ui/vdagent.c +0 -11
@@ -6,7 +6,6 @@#include "qemu/option.h" #include "qemu/units.h" #include "hw/qdev-core.h" -#include "migration/blocker.h" #include "migration/vmstate.h" #include "ui/clipboard.h" #include "ui/console.h"
@@ -33,9 +32,6 @@struct VDAgentChardev {Chardev parent; -/* TODO: migration isn't yet supported */ -Error *migration_blocker; -/* config */ bool mouse;bool clipboard;
@@ -673,10 +669,6 @@ static void vdagent_chr_open(Chardev *chr,return ;#endif -if (migrate_add_blocker (&vd->migration_blocker, errp) !=0 ) { -return ; - } - vd->mouse =VDAGENT_MOUSE_DEFAULT ;if (cfg->has_mouse) { vd->mouse = cfg->mouse;
@@ -998,15 +990,12 @@ static void vdagent_chr_init(Object *obj)buffer_init (&vd->outbuf,"vdagent-outbuf" );vmstate_register (NULL ,0 , &vmstate_vdagent, vd); -error_setg (&vd->migration_blocker, -"The vdagent chardev doesn't yet support migration" ); }static void vdagent_chr_fini (Object *obj) {VDAgentChardev *vd =QEMU_VDAGENT_CHARDEV (obj); -migrate_del_blocker (&vd->migration_blocker);vdagent_disconnect (vd);if (vd->mouse_hs) {qemu_input_handler_unregister (vd->mouse_hs);
Signature · 1 line
2.27.0
Hi
Quoted 7 lines · On Fri, Mar 21, 2025 at 7:40 AM <[email protected]> wrote:
From: Hyman Huang <[email protected]>
Our goal is to migrate VMs that are configured with qemu-vdagent-typed chardev while allowing the agent to continue working without having to restart the service in guest.
I sent a more complete series last week: "[PATCH for-10.1 00/10] Support vdagent migration"
https://patchew.org/QEMU/[email protected]/
Quoted 45 lines
Let's justify which fields should be taken into account for struct VDAgentChardev.
struct VDAgentChardev { Chardev parent; /* config */ bool mouse; bool clipboard; /* guest vdagent */ uint32_t caps; VDIChunkHeader chunk; uint32_t chunksize; uint8_t *msgbuf; uint32_t msgsize; uint8_t *xbuf; uint32_t xoff, xsize; Buffer outbuf; /* mouse */ DeviceState mouse_dev; uint32_t mouse_x; uint32_t mouse_y; uint32_t mouse_btn; uint32_t mouse_display; QemuInputHandlerState *mouse_hs; /* clipboard */ QemuClipboardPeer cbpeer; uint32_t last_serial[QEMU_CLIPBOARD_SELECTION__COUNT]; uint32_t cbpending[QEMU_CLIPBOARD_SELECTION__COUNT]; };parent:
No dynamic information is generated. skip migrating.mouse, clipboard:
The mouse and clipboard should be set up identically on both sides. Skip migrating.caps:
Store the negotiated caps between the client and the guest. Should migrate.
Quoted 6 lines
chunk, ... outbuf:
The spice agent protocol's message transportation between the client and the guest is implemented using all of these fields, however the message loss can be tolerated by guests because the issue may occur in the real world as well.
Could skip migrating.
It's part of the host/guest state, data will be lost and it's likely the communication will break if it's not migrated.
Quoted 5 lines
mouse_dev, ...mouse_hs:
The mouse state can be reset after a live migration since the agent working inside the guest does not heavily depend on them. Could skip migrating
same
Quoted 6 lines
cbpeer:
Since the cbpeer would lose the data it references to if the qemu clipboard data was not migrated, this field can also be initialized after live migration.
Could skip migrating
We should migrate the clipboard content too, to avoid having to request it again, or have a noticeable effect.
Quoted 32 lines
last_serial, cbpending:
It is necessary for the agent to function after live migration. Should migrate.For the
last_serial, saving & loading its value to make ensure the client receives the most recent clipboard data from the guest after live migration.For the cbpending, saving & loading its value aims to inform the guest that the clipboard has been released and is now empty in case that the guest acts strangely while supposing that the requested data can be properly retrieved.
To summarize, all we need to do is migrate the caps,
last_serialand cbpendings fields of the struct VDAgentChardev,Please review, thanks
Yong
Hyman Huang (3):
vdagent: Wrapvdagent_register_to_qemu_clipboardfunction
vdagent: Set up mouse and clipboard after live migration
vdagent: Drop blocker to support migrationui/trace-events | 1 + ui/vdagent.c | 102 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 18 deletions(-)--
2.27.0
Quoted 11 lines · Marc-André Lureau
Hi
Quoted 7 lines · On Fri, Mar 21, 2025 at 7:40 AM <[email protected]> wrote:
From: Hyman Huang <[email protected]>
Our goal is to migrate VMs that are configured with qemu-vdagent-typed chardev while allowing the agent to continue working without having to restart the service in guest.
Quoted 4 lines
I sent a more complete series last week: "[PATCH for-10.1 00/10] Support vdagent migration"
Thanks for your reminder, indeed, it migrates the qemu clipboard content as well, more complete.
Thanks for the work, we'll pick your patchset and try it.
Quoted 110 lines
Quoted 48 lines
Let's justify which fields should be taken into account for struct VDAgentChardev.
struct VDAgentChardev { Chardev parent; /* config */ bool mouse; bool clipboard; /* guest vdagent */ uint32_t caps; VDIChunkHeader chunk; uint32_t chunksize; uint8_t *msgbuf; uint32_t msgsize; uint8_t *xbuf; uint32_t xoff, xsize; Buffer outbuf; /* mouse */ DeviceState mouse_dev; uint32_t mouse_x; uint32_t mouse_y; uint32_t mouse_btn; uint32_t mouse_display; QemuInputHandlerState *mouse_hs; /* clipboard */ QemuClipboardPeer cbpeer; uint32_t last_serial[QEMU_CLIPBOARD_SELECTION__COUNT]; uint32_t cbpending[QEMU_CLIPBOARD_SELECTION__COUNT]; };parent:
No dynamic information is generated. skip migrating.mouse, clipboard:
The mouse and clipboard should be set up identically on both sides. Skip migrating.caps:
Store the negotiated caps between the client and the guest. Should migrate.Quoted 6 lines
chunk, ... outbuf:
The spice agent protocol's message transportation between the client and the guest is implemented using all of these fields, however the message loss can be tolerated by guests because the issue may occur in the real world as well.
Could skip migrating.It's part of the host/guest state, data will be lost and it's likely the communication will break if it's not migrated.
Quoted 5 lines
mouse_dev, ...mouse_hs:
The mouse state can be reset after a live migration since the agent working inside the guest does not heavily depend on them. Could skip migratingsame
Quoted 6 lines
cbpeer:
Since the cbpeer would lose the data it references to if the qemu clipboard data was not migrated, this field can also be initialized after live migration.
Could skip migratingWe should migrate the clipboard content too, to avoid having to request it again, or have a noticeable effect.
Quoted 32 lines
last_serial, cbpending:
It is necessary for the agent to function after live migration. Should migrate.For the
last_serial, saving & loading its value to make ensure the client receives the most recent clipboard data from the guest after live migration.For the cbpending, saving & loading its value aims to inform the guest that the clipboard has been released and is now empty in case that the guest acts strangely while supposing that the requested data can be properly retrieved.
To summarize, all we need to do is migrate the caps,
last_serialand cbpendings fields of the struct VDAgentChardev,Please review, thanks
Yong
Hyman Huang (3):
vdagent: Wrapvdagent_register_to_qemu_clipboardfunction
vdagent: Set up mouse and clipboard after live migration
vdagent: Drop blocker to support migrationui/trace-events | 1 + ui/vdagent.c | 102 +++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 85 insertions(+), 18 deletions(-)--
2.27.0
Signature · 1 line
Best regards