libxl: refactor suspend/resume code
PATCH v6 1/1 libxl: refactor suspend/resume code
This patch series refactors the suspend/resume code to minimize Remus specific code in libxl. There are a couple of trivial bug fixes too.
Changes in V6:
Rebase to current tip.
Changes in V5:
This series is just a resend, rebasing the patches to the latest tip. It depends on Stefano's "V5: libxl: save/restore qemu physmap".
Changes in V4:
1. Incorporated Ian Campbell's comments on the suspend_cancel support patch.
Changes in V3:
1. rebase patches based on Stefano's patches
use qmp_save instead of qmp_migrate
2. check if qemu moves to "running" state after resuming the device model
3. Moved comments on the co-operative suspend to libxl.h
Changes in V2:
1. migrate code is refactored as save_config , create child, do_preamble instead of coaelscing them all into one single function.
2. More documentation for suspend_cancel parameter in domain_resume
3. Minor nits
Shriram
PATCH v6 1/1 libxl: QMP stop/resume & refactor QEMU suspend/resume/save
# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1337283418 25200
# Node ID 07d5f26fee0a65c8145bd1028568693e45cfd25c # Parent f8279258e3c96baccb8338a47af068bd650b121a
libxl: QMP stop/resume & refactor QEMU suspend/resume/save
Implement QMP stop and resume functionality and split device model save into 3 parts:
suspend_dm(domid)
save_dm(domid, fd)
resume_dm(domid)
Integrate Device model suspend into suspend_common_callback
tools/libxl/libxl_dom.c +61 -19
@@ -587,6 +587,54 @@return rc ?0 :1 ; } +int libxl__domain_suspend_device_model (libxl__gc *gc,uint32_t domid) +{ +libxl_ctx *ctx =libxl__gc_owner (gc); +int ret =0 ; +const char *filename =libxl__device_model_savefile (gc, domid); + +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : { +LIBXL__LOG (ctx,LIBXL__LOG_DEBUG , +"Saving device model state to %s" , filename); +libxl__qemu_traditional_cmd (gc, domid,"save" ); +libxl__wait_for_device_model (gc, domid,"paused" ,NULL ,NULL ,NULL ); +break ; + } +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if (libxl__qmp_stop (gc, domid)) +return ERROR_FAIL ; +/* Save DM state into filename */ + ret =libxl__qmp_save (gc, domid, filename); +if (ret) +unlink (filename); +break ; +default : +return ERROR_INVAL ; + } + +return ret; +} + +int libxl__domain_resume_device_model (libxl__gc *gc,uint32_t domid) +{ + +switch (libxl__device_model_version_running (gc, domid)) { +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : { +libxl__qemu_traditional_cmd (gc, domid,"continue" ); +libxl__wait_for_device_model (gc, domid,"running" ,NULL ,NULL ,NULL ); +break ; + } +case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : +if (libxl__qmp_resume (gc, domid)) +return ERROR_FAIL ; +default : +return ERROR_INVAL ; + } + +return 0 ; +} +static int libxl__domain_suspend_common_callback (void *data) {struct suspendinfo *si = data;
@@ -616,7 +664,7 @@return 0 ; } si->guest_responded =1 ; -return 1 ; + goto guest_suspended; }if (si->hvm && (!hvm_pvdrv || hvm_s_state)) {
@@ -694,7 +742,7 @@ shutdown_reason = (info.flags >> XEN_DOMINF_shutdownshift) & XEN_DOMINF_shutdownmask;if (shutdown_reason == SHUTDOWN_suspend) {LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"guest has suspended" ); -return 1 ; + goto guest_suspended; } }
@@ -703,6 +751,17 @@LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"guest did not suspend" );return 0 ; + + guest_suspended: +if (si->hvm) { + ret =libxl__domain_suspend_device_model (si->gc, si->domid); +if (ret) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR , +"libxl__domain_suspend_device_model failed ret=%d" , ret); +return 0 ; + } + } +return 1 ; } static inline char *save_helper(libxl__gc *gc, uint32_t domid,
@@ -885,23 +944,6 @@struct stat st;uint32_t qemu_state_len; -switch (libxl__device_model_version_running (gc, domid)) { -case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN_TRADITIONAL : { -LIBXL__LOG (ctx,LIBXL__LOG_DEBUG , -"Saving device model state to %s" , filename); -libxl__qemu_traditional_cmd (gc, domid,"save" ); -libxl__wait_for_device_model (gc, domid,"paused" ,NULL ,NULL ,NULL ); -break ; - } -case LIBXL_DEVICE_MODEL_VERSION_QEMU_XEN : - ret =libxl__qmp_save (gc, domid, (char *)filename); -if (ret) - goto out; -break ; -default : -return ERROR_INVAL ; - } -if (stat (filename, &st) <0 ) {LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Unable to stat qemu save file\n" );
tools/libxl/libxl_internal.h +6 -0
@@ -759,6 +759,8 @@libxl_domain_type type, int live, int debug); _hidden const char *libxl__device_model_savefile(libxl__gc *gc, uint32_t domid); +_hidden int libxl__domain_suspend_device_model(libxl__gc *gc, uint32_t domid); +_hidden int libxl__domain_resume_device_model(libxl__gc *gc, uint32_t domid); _hidden int libxl__domain_save_device_model(libxl__gc *gc, uint32_t domid, int fd); _hidden void libxl__userdata_destroyall(libxl__gc *gc, uint32_t domid);
@@ -1276,6 +1278,10 @@ _hidden int libxl__qmp_pci_add(libxl__gc *gc, int d, libxl_device_pci *pcidev); _hidden int libxl__qmp_pci_del(libxl__gc *gc, int domid,libxl_device_pci *pcidev); +/* Suspend QEMU. */ +_hidden int libxl__qmp_stop(libxl__gc *gc, int domid); +/* Resume QEMU. */ +_hidden int libxl__qmp_resume(libxl__gc *gc, int domid);/* Save current QEMU state into fd. */ _hidden int libxl__qmp_save(libxl__gc *gc, int domid, const char *filename);/* close and free the QMP handler */
tools/libxl/libxl_qmp.c +32 -0
@@ -883,6 +883,38 @@return rc; } +int libxl__qmp_stop (libxl__gc *gc,int domid) +{ +libxl__qmp_handler *qmp =NULL ; +int rc =0 ; + + qmp =libxl__qmp_initialize (gc, domid); +if (!qmp) +return ERROR_FAIL ; + + rc =qmp_synchronous_send (qmp,"stop" ,NULL , +NULL ,NULL , qmp->timeout); + +libxl__qmp_close (qmp); +return rc; +} + +int libxl__qmp_resume (libxl__gc *gc,int domid) +{ +libxl__qmp_handler *qmp =NULL ; +int rc =0 ; + + qmp =libxl__qmp_initialize (gc, domid); +if (!qmp) +return ERROR_FAIL ; + + rc =qmp_synchronous_send (qmp,"cont" ,NULL , +NULL ,NULL , qmp->timeout); + +libxl__qmp_close (qmp); +return rc; +} +int libxl__qmp_initializations (libxl__gc *gc,uint32_t domid,const libxl_domain_config *guest_config) {
PATCH v6 1/1 libxl: support suspend_cancel in domain_resume
In reply to Shriram Rajagopalan# HG changeset patch
# User Shriram Rajagopalan <[email protected]> # Date 1337283420 25200
# Node ID d5d5d596044526b59f6a3e6fd81f6171c90efe6e # Parent 07d5f26fee0a65c8145bd1028568693e45cfd25c
libxl: support suspend_cancel in domain_resume
Add an extra parameter to libxl_domain_resume indicating if the caller wishes to use the SUSPEND_CANCEL style resume instead of the normal resume.
tools/libxl/libxl.c +13 -8
@@ -374,24 +374,29 @@return rc; } -int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid) +int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid,int suspend_cancel) {GC_INIT (ctx);int rc =0 ; -if (LIBXL__DOMAIN_IS_TYPE (gc, domid,HVM )) { -LIBXL__LOG (ctx,LIBXL__LOG_DEBUG ,"Called domain_resume on " -"non-cooperative hvm domain %u" , domid); - rc =ERROR_NI ; - goto out; - } -if (xc_domain_resume (ctx->xch, domid,0 )) { +if (xc_domain_resume (ctx->xch, domid, suspend_cancel)) {LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"xc_domain_resume failed for domain %u" , domid); rc =ERROR_FAIL ; goto out; } + +if (LIBXL__DOMAIN_IS_TYPE (gc, domid,HVM )) { + rc =libxl__domain_resume_device_model (gc, domid); +if (rc) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR , +"failed to resume device model for domain %u:%d" , + domid, rc); + goto out; + } + } +if (!xs_resume_domain (ctx->xsh, domid)) { LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "xs_resume_domain failed for domain %u",
tools/libxl/libxl.h +6 -1
@@ -527,7 +527,12 @@void libxl_domain_config_dispose (libxl_domain_config *d_config);int libxl_domain_suspend (libxl_ctx *ctx,libxl_domain_suspend_info *info,uint32_t domid,int fd); -int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid); + +/* @param suspend_cancel [from xenctrl.h:xc_domain_resume( @param fast )] +* If this parameter is true, use co-operative resume. The guest +* must support this. +*/ +int libxl_domain_resume (libxl_ctx *ctx,uint32_t domid,int suspend_cancel);int libxl_domain_shutdown (libxl_ctx *ctx,uint32_t domid);int libxl_domain_reboot (libxl_ctx *ctx,uint32_t domid);int libxl_domain_destroy (libxl_ctx *ctx,uint32_t domid);
tools/libxl/xl_cmdimpl.c +2 -2
@@ -2889,7 +2889,7 @@if (common_domname) {libxl_domain_rename (ctx, domid, away_domname, common_domname); } - rc =libxl_domain_resume (ctx, domid); + rc =libxl_domain_resume (ctx, domid,0 );if (!rc)fprintf (stderr,"migration sender: Resumed OK.\n" );fprintf (stderr,"Migration failed due to problems at target.\n" );
@@ -2911,7 +2911,7 @@close (send_fd);migration_child_report (child, recv_fd);fprintf (stderr,"Migration failed, resuming at sender.\n" ); -libxl_domain_resume (ctx, domid); +libxl_domain_resume (ctx, domid,0 );exit (-ERROR_FAIL ); failed_badly:
PATCH v6 1/1 libxl: refactor migrate_domain and generalize migrate_receive
In reply to Shriram Rajagopalan# HG changeset patch
# User Shriram Rajagopalan <[email protected]>
# Date 1337283422 25200
# Node ID b633a458bf3a931ad610363a8ce55b2970f7da65 # Parent d5d5d596044526b59f6a3e6fd81f6171c90efe6e
libxl: refactor migrate_domain and generalize migrate_receive
Refactor some tasks like establishing the migration channel, initial migration protocol exchange into separate functions, to facilitate re-use, when remus support is introduced. Also, make migrate_receive generic (instead of resorting to stdin and stdout as the file descriptors for communication).
tools/libxl/xl_cmdimpl.c +83 -51
@@ -2670,6 +2670,42 @@exit (0 ); } +static pid_t create_migration_child (const char *rune,int *send_fd, +int *recv_fd) +{ +int sendpipe[2 ], recvpipe[2 ]; +pid_t child =-1 ; + +if (!rune || !send_fd || !recv_fd) +return -1 ; + +MUST (libxl_pipe (ctx, sendpipe) ); +MUST (libxl_pipe (ctx, recvpipe) ); + + child =xl_fork (ctx); + +if (!child) { +dup2 (sendpipe[0 ],0 ); +dup2 (recvpipe[1 ],1 ); +close (sendpipe[0 ]);close (sendpipe[1 ]); +close (recvpipe[0 ]);close (recvpipe[1 ]); +execlp ("sh" ,"sh" ,"-c" ,rune,(char *)0 ); +perror ("failed to exec sh" ); +exit (-1 ); + } + +close (sendpipe[0 ]); +close (recvpipe[1 ]); + *send_fd = sendpipe[1 ]; + *recv_fd = recvpipe[0 ]; + +/* if receiver dies, we get an error and can clean up +rather than just dying */ +signal (SIGPIPE ,SIG_IGN ); + +return child; +} +static int migrate_read_fixedmessage (int fd,const void *msg,int msgsz,const char *what,const char *rune) {char buf[msgsz];
@@ -2755,52 +2791,17 @@ migration_child =0 ; } -static void migrate_domain (const char *domain_spec,const char *rune, -const char *override_config_file) +static void migrate_do_preamble (int send_fd,int recv_fd,pid_t child, +uint8_t *config_data,int config_len, +const char *rune) { -pid_t child =-1 ; -int rc; -int sendpipe[2 ], recvpipe[2 ]; -int send_fd, recv_fd; -libxl_domain_suspend_info suspinfo; -char *away_domname; -char rc_buf; -uint8_t *config_data; -int config_len; - -save_domain_core_begin (domain_spec, override_config_file, - &config_data, &config_len); - -if (!config_len) { -fprintf (stderr,"No config file stored for running domain and " -"none supplied - cannot migrate.\n" ); +int rc =0 ; + +if (send_fd <0 || recv_fd <0 ) { +fprintf (stderr,"migrate_do_preamble: invalid file descriptors\n" );exit (1 ); } -MUST (libxl_pipe (ctx, sendpipe) ); -MUST (libxl_pipe (ctx, recvpipe) ); - - child =xl_fork (ctx); - -if (!child) { -dup2 (sendpipe[0 ],0 ); -dup2 (recvpipe[1 ],1 ); -close (sendpipe[0 ]);close (sendpipe[1 ]); -close (recvpipe[0 ]);close (recvpipe[1 ]); -execlp ("sh" ,"sh" ,"-c" ,rune,(char *)0 ); -perror ("failed to exec sh" ); -exit (-1 ); - } - -close (sendpipe[0 ]); -close (recvpipe[1 ]); - send_fd = sendpipe[1 ]; - recv_fd = recvpipe[0 ]; - -signal (SIGPIPE ,SIG_IGN ); -/* if receiver dies, we get an error and can clean up -rather than just dying */ - rc =migrate_read_fixedmessage (recv_fd, migrate_receiver_banner,sizeof (migrate_receiver_banner)-1 ,"banner" , rune);
@@ -2813,6 +2814,34 @@save_domain_core_writeconfig (send_fd,"migration stream" , config_data, config_len); +} + +static void migrate_domain (const char *domain_spec,const char *rune, +const char *override_config_file) +{ +pid_t child =-1 ; +int rc; +int send_fd =-1 , recv_fd =-1 ; +libxl_domain_suspend_info suspinfo; +char *away_domname; +char rc_buf; +uint8_t *config_data; +int config_len; + +save_domain_core_begin (domain_spec, override_config_file, + &config_data, &config_len); + +if (!config_len) { +fprintf (stderr,"No config file stored for running domain and " +"none supplied - cannot migrate.\n" ); +exit (1 ); + } + + child =create_migration_child (rune, &send_fd, &recv_fd); + +migrate_do_preamble (send_fd, recv_fd, child, config_data, config_len, + rune); +xtl_stdiostream_adjust_flags (logger,XTL_STDIOSTREAM_HIDE_PROGRESS ,0 );memset (&suspinfo,0 ,sizeof (suspinfo));
@@ -2936,7 +2965,8 @@if (rc) {fprintf (stderr,"core dump failed (rc=%d)\n" ,rc);exit (-1 ); } } -static void migrate_receive (int debug,int daemonize,int monitor) +static void migrate_receive (int debug,int daemonize,int monitor, +int send_fd,int recv_fd) {int rc, rc2;char rc_buf;
@@ -2948,7 +2978,7 @@fprintf (stderr,"migration target: Ready to receive domain.\n" ); - CHK_ERRNO( libxl_write_exactly(ctx, 1, + CHK_ERRNO( libxl_write_exactly(ctx, send_fd, migrate_receiver_banner, sizeof(migrate_receiver_banner)-1, "migration ack stream",
@@ -2960,7 +2990,7 @@ dom_info.monitor = monitor; dom_info.paused =1 ; dom_info.restore_file ="incoming migration stream" ; - dom_info.migrate_fd =0 ;/* stdin */ + dom_info.migrate_fd = recv_fd; dom_info.migration_domname_r = &migration_domname; dom_info.incr_generationid =0 ;
@@ -2974,13 +3004,13 @@fprintf (stderr,"migration target: Transfer complete," " requesting permission to start domain.\n" ); - rc =libxl_write_exactly (ctx,1 , + rc =libxl_write_exactly (ctx, send_fd, migrate_receiver_ready,sizeof (migrate_receiver_ready),"migration ack stream" ,"ready message" );if (rc)exit (-rc); - rc =migrate_read_fixedmessage (0 , migrate_permission_to_go, + rc =migrate_read_fixedmessage (recv_fd, migrate_permission_to_go,sizeof (migrate_permission_to_go),"GO message" ,0 );if (rc) goto perhaps_destroy_notify_rc;
@@ -2999,7 +3029,7 @@ rc =0 ; perhaps_destroy_notify_rc: - rc2 =libxl_write_exactly (ctx,1 , + rc2 =libxl_write_exactly (ctx, send_fd, migrate_report,sizeof (migrate_report),"migration ack stream" ,"success/failure report" );
@@ -3007,7 +3037,7 @@ rc_buf = -rc;assert (!!rc_buf == !!rc); - rc2 =libxl_write_exactly (ctx,1 , &rc_buf,1 , + rc2 =libxl_write_exactly (ctx, send_fd, &rc_buf,1 ,"migration ack stream" ,"success/failure code" );if (rc2)exit (-ERROR_BADFAIL );
@@ -3025,7 +3055,7 @@fprintf (stderr,"migration target: Cleanup OK, granting sender" " permission to resume.\n" ); - rc2 = libxl_write_exactly(ctx, 1, + rc2 = libxl_write_exactly(ctx, send_fd, migrate_permission_to_go, sizeof(migrate_permission_to_go), "migration ack stream",
@@ -3122,7 +3152,9 @@help ("migrate-receive" );return 2 ; } -migrate_receive (debug, daemonize, monitor); +migrate_receive (debug, daemonize, monitor, +STDOUT_FILENO ,STDIN_FILENO ); +return 0 ; }
PATCH v6 1/1 libxl: resume instead of unpause on xl save -c
In reply to Shriram Rajagopalan# HG changeset patch
# User Shriram Rajagopalan <[email protected]> # Date 1337283425 25200
# Node ID 24c462a07e167e4ce35a22197dbef74853b08359 # Parent b633a458bf3a931ad610363a8ce55b2970f7da65
libxl: resume instead of unpause on xl save -c
The guest is "suspended" via libxl_domain_suspend when taking a snapshot. So call libxl_domain_resume instead of libxl_domain_unpause, when taking a checkpoint of the domain (using xl save -c).
tools/libxl/xl_cmdimpl.c +1 -1
@@ -2663,7 +2663,7 @@close (fd);if (checkpoint) -libxl_domain_unpause (ctx, domid); +libxl_domain_resume (ctx, domid,1 );else libxl_domain_destroy (ctx, domid);
Quoted 3 lines · Shriram Rajagopalan
This patch series refactors the suspend/resume code to minimize Remus specific code in libxl. There are a couple of trivial bug fixes too.
I've applied all four of these as well as the two patches from "libxl:
Remus support". Thanks for your contribution, and thanks for your patience in particular.
I fixed up a minor reject in "libxl: refactor migrate_domain and generalize migrate_receive" due to the context line
dom_info.restore_file = "incoming migration stream";
having been removed. It's hard to imagine I messed that up but you'd best check!
BTW I tested PV migrate, HVM migrate with qemu-xen-traditional (stub and non-stub) and PVHVM migrate with qemu-xen-traditional (stub and non-stub). All seemed OK.
Ian.
Quoted 9 lines · Ian Campbell
Quoted 3 lines · Shriram Rajagopalan
This patch series refactors the suspend/resume code to minimize Remus specific code in libxl. There are a couple of trivial bug fixes too.
I've applied all four of these as well as the two patches from "libxl:
Remus support". Thanks for your contribution, and thanks for your patience in particular.
Quoted 7 lines
I fixed up a minor reject in "libxl: refactor
migrate_domainand generalizemigrate_receive" due to the context linedom_info.restore_file = "incoming migration stream";having been removed. It's hard to imagine I messed that up but you'd best check!
Strange.. the c/s on my local box is 25334 (before applying the patches). I believe thats where xen-unstable.hg but the patches were applied against staging/xen-unstable.hg..
And I think the issue you faced was because of a conflict with c/s 25344: xl make clear distinction between "filename" and "data source"
that george submitted. Its in staging but not in the main unstable repo.
BTW I tested PV migrate, HVM migrate with qemu-xen-traditional (stub and
Quoted 4 lines
non-stub) and PVHVM migrate with qemu-xen-traditional (stub and non-stub). All seemed OK.
Thanks a lot :).
Quoted 4 lines
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 26 lines · Shriram Rajagopalan
On Fri, May 18, 2012 at 6:07 AM, Ian Campbell <[email protected]> wrote:
On Thu, 2012-05-17 at 20:45 +0100, Shriram Rajagopalan wrote:
> This patch series refactors the suspend/resume code to minimize
> Remus specific code in libxl. There are a couple of trivial bug
> fixes too.I've applied all four of these as well as the two patches from "libxl:
Remus support". Thanks for your contribution, and thanks for your
patience in particular.I fixed up a minor reject in "libxl: refactor
migrate_domainand
generalizemigrate_receive" due to the context line
dom_info.restore_file = "incoming migration stream";
having been removed. It's hard to imagine I messed that up but you'd
best check!
Quoted 4 lines
Strange.. the c/s on my local box is 25334 (before applying the patches).
I believe thats where xen-unstable.hg but the patches were applied against staging/xen-unstable.hg..
Yes, patches are always applied against staging.
Quoted 3 lines
And I think the issue you faced was because of a conflict with c/s 25344: xl make clear distinction between "filename" and "data source"
Yes, I think so, as I said it was just a line in the context which differs.
Quoted 2 lines
that george submitted. Its in staging but not in the main unstable repo.
Right, things are applied to staging and propagate automatically to the main repo after automated testing. Nothing is ever applied direct to the main repo.
Testing is a bit broken at the minute so the main repo is a little bit behind.
Quoted 9 lines
BTW I tested PV migrate, HVM migrate with qemu-xen-traditional (stub and
non-stub) and PVHVM migrate with qemu-xen-traditional (stub and
non-stub). All seemed OK.Thanks a lot :).
No problem.