xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: rshriram@cs.ubc.ca
To: xen-devel@lists.xensource.com
Cc: anthony.perard@citrix.com, brendan@cs.ubc.ca,
	ian.jackson@eu.citrix.com, ian.campbell@citrix.com
Subject: [PATCH 5 of 6] libxl: refactor migrate_domain and generalize	migrate_receive
Date: Mon, 30 Jan 2012 17:05:10 -0800	[thread overview]
Message-ID: <d79c7a853c644d459cda.1327971910@athos.nss.cs.ubc.ca> (raw)
In-Reply-To: <patchbomb.1327971905@athos.nss.cs.ubc.ca>

# HG changeset patch
# User Shriram Rajagopalan <rshriram@cs.ubc.ca>
# Date 1327971533 28800
# Node ID d79c7a853c644d459cda93bf61657be48104cd63
# Parent  efe92d80c47487485056266a1404a8d2817b7f09
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).

Signed-off-by: Shriram Rajagopalan <rshriram@cs.ubc.ca>

diff -r efe92d80c474 -r d79c7a853c64 tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c	Mon Jan 30 16:58:47 2012 -0800
+++ b/tools/libxl/xl_cmdimpl.c	Mon Jan 30 16:58:53 2012 -0800
@@ -2531,6 +2531,43 @@ static int save_domain(const char *p, co
     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 = libxl_fork(ctx);
+    if (child==-1) exit(1);
+
+    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];
@@ -2616,18 +2653,23 @@ static void migration_child_report(pid_t
     migration_child = 0;
 }
 
-static void migrate_domain(const char *domain_spec, const char *rune,
-                           const char *override_config_file)
+/* It is okay to have a NULL rune (we could be communicating over tcp sockets
+ * but not both NULL rune and NULL(send_fd, recv_fd).
+ */
+static void migrate_do_preamble(const char *domain_spec, const char *rune,
+                                const char *override_config_file,
+                                int *send_fd, int *recv_fd, pid_t *mchild)
 {
-    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;
+    int rc = 0;
     uint8_t *config_data;
     int config_len;
+    pid_t child = -1;
+
+    if (!send_fd || !recv_fd) {
+        fprintf(stderr, "Cannot create migration channel:"
+                "Null file descriptors supplied!\n");
+        exit(1);
+    }
 
     save_domain_core_begin(domain_spec, override_config_file,
                            &config_data, &config_len);
@@ -2638,43 +2680,44 @@ static void migrate_domain(const char *d
         exit(1);
     }
 
-    MUST( libxl_pipe(ctx, sendpipe) );
-    MUST( libxl_pipe(ctx, recvpipe) );
-
-    child = libxl_fork(ctx);
-    if (child==-1) exit(1);
-
-    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,
+    if (*send_fd < 0 || *recv_fd < 0) {
+        if (rune)
+            child = create_migration_child(rune, send_fd, recv_fd);
+        if (child < 0) {
+            fprintf(stderr, "failed to create migration channel"
+                    " - empty command ?\n");
+            exit(1);
+        }
+    }
+
+    rc = migrate_read_fixedmessage(*recv_fd, migrate_receiver_banner,
                                    sizeof(migrate_receiver_banner)-1,
                                    "banner", rune);
     if (rc) {
-        close(send_fd);
-        migration_child_report(child, recv_fd);
+        close(*send_fd);
+        migration_child_report(child, *recv_fd);
         exit(-rc);
     }
 
-    save_domain_core_writeconfig(send_fd, "migration stream",
+    save_domain_core_writeconfig(*send_fd, "migration stream",
                                  config_data, config_len);
-
+    if (mchild)
+        *mchild = child;
+    return;
+}
+
+static void migrate_domain(const char *domain_spec, const char *rune,
+                           const char *override_config_file)
+{
+    int rc;
+    int send_fd = -1, recv_fd = -1;
+    libxl_domain_suspend_info suspinfo;
+    char *away_domname;
+    char rc_buf;
+    pid_t child = -1;
+
+    migrate_do_preamble(domain_spec, rune, override_config_file,
+                        &send_fd, &recv_fd, &child);
     xtl_stdiostream_adjust_flags(logger, XTL_STDIOSTREAM_HIDE_PROGRESS, 0);
 
     memset(&suspinfo, 0, sizeof(suspinfo));
@@ -2798,7 +2841,12 @@ static void core_dump_domain(const char 
     if (rc) { fprintf(stderr,"core dump failed (rc=%d)\n",rc);exit(-1); }
 }
 
-static void migrate_receive(int debug, int daemonize, int monitor)
+/* Explicitly specifying a send & recv fd allows us to switch to a tcp socket
+ * based migration/replication channel in future, instead of exec/forking from
+ * an ssh channel.
+ */
+static void migrate_receive(int debug, int daemonize, int monitor,
+                            int send_fd, int recv_fd)
 {
     int rc, rc2;
     char rc_buf;
@@ -2810,7 +2858,7 @@ static void migrate_receive(int debug, i
 
     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",
@@ -2822,7 +2870,7 @@ static void migrate_receive(int debug, i
     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.no_incr_generationid = 1;
 
@@ -2836,13 +2884,13 @@ static void migrate_receive(int debug, i
     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;
@@ -2861,7 +2909,7 @@ static void migrate_receive(int debug, i
     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");
@@ -2869,7 +2917,7 @@ static void migrate_receive(int debug, i
 
     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);
@@ -2887,7 +2935,7 @@ static void migrate_receive(int debug, i
         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",
@@ -2983,7 +3031,8 @@ int main_migrate_receive(int argc, char 
         help("migrate-receive");
         return 2;
     }
-    migrate_receive(debug, daemonize, monitor);
+    migrate_receive(debug, daemonize, monitor,
+                    /* write to stdout */1, /* read from stdin */0);
     return 0;
 }

  parent reply	other threads:[~2012-01-31  1:05 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-31  1:05 [PATCH 0 of 6] libxl: refactor suspend/resume code rshriram
2012-01-31  1:05 ` [PATCH 1 of 6] libxl: helper function to send commands to traditional qemu rshriram
2012-01-31  9:46   ` Ian Campbell
2012-01-31 17:30     ` Shriram Rajagopalan
2012-02-09 18:08       ` Ian Jackson
2012-01-31  1:05 ` [PATCH 2 of 6] libxl: bugfix: create_domain() return to caller if !daemonize rshriram
2012-01-31  9:47   ` Ian Campbell
2012-02-09 18:06     ` Ian Jackson
2012-01-31  1:05 ` [PATCH 3 of 6] libxl: QMP stop/resume & refactor QEMU suspend/resume/save rshriram
2012-01-31  9:54   ` Ian Campbell
2012-01-31 17:32     ` Shriram Rajagopalan
2012-01-31 23:53       ` Shriram Rajagopalan
2012-02-01 10:46         ` Ian Campbell
2012-01-31  1:05 ` [PATCH 4 of 6] libxl: support suspend_cancel in domain_resume rshriram
2012-01-31 10:00   ` Ian Campbell
2012-01-31 10:04     ` Ian Campbell
2012-01-31 17:52     ` Shriram Rajagopalan
2012-02-01 10:47       ` Ian Campbell
2012-02-01 10:53       ` Ian Campbell
2012-02-01 15:48         ` Pasi Kärkkäinen
2012-02-01 16:02           ` Ian Campbell
2012-02-01 19:30             ` Shriram Rajagopalan
2012-02-02  7:04               ` Shriram Rajagopalan
2012-02-02  7:59                 ` Ian Campbell
2012-02-02  7:58               ` Ian Campbell
2012-01-31  1:05 ` rshriram [this message]
2012-01-31 10:19   ` [PATCH 5 of 6] libxl: refactor migrate_domain and generalize migrate_receive Ian Campbell
2012-01-31  1:05 ` [PATCH 6 of 6] libxl: resume instead of unpause on xl save -c rshriram
2012-01-31 10:21   ` Ian Campbell
2012-01-31 17:55     ` Shriram Rajagopalan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d79c7a853c644d459cda.1327971910@athos.nss.cs.ubc.ca \
    --to=rshriram@cs.ubc.ca \
    --cc=anthony.perard@citrix.com \
    --cc=brendan@cs.ubc.ca \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).