aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU live migration |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * Copyright Dell MessageOne 2008 |
| 6 | * |
| 7 | * Authors: |
| 8 | * Anthony Liguori <aliguori@us.ibm.com> |
| 9 | * Charles Duffy <charles_duffy@messageone.com> |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 12 | * the COPYING file in the top-level directory. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include "qemu-common.h" |
| 17 | #include "qemu_socket.h" |
| 18 | #include "migration.h" |
| 19 | #include "qemu-char.h" |
| 20 | #include "sysemu.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 21 | #include "buffered_file.h" |
| 22 | #include "block.h" |
| 23 | |
| 24 | //#define DEBUG_MIGRATION_EXEC |
| 25 | |
| 26 | #ifdef DEBUG_MIGRATION_EXEC |
| 27 | #define dprintf(fmt, ...) \ |
| 28 | do { printf("migration-exec: " fmt, ## __VA_ARGS__); } while (0) |
| 29 | #else |
| 30 | #define dprintf(fmt, ...) \ |
| 31 | do { } while (0) |
| 32 | #endif |
| 33 | |
| 34 | static int file_errno(FdMigrationState *s) |
| 35 | { |
| 36 | return errno; |
| 37 | } |
| 38 | |
| 39 | static int file_write(FdMigrationState *s, const void * buf, size_t size) |
| 40 | { |
| 41 | return write(s->fd, buf, size); |
| 42 | } |
| 43 | |
| 44 | static int exec_close(FdMigrationState *s) |
| 45 | { |
| 46 | dprintf("exec_close\n"); |
| 47 | if (s->opaque) { |
| 48 | qemu_fclose(s->opaque); |
| 49 | s->opaque = NULL; |
| 50 | s->fd = -1; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 55 | MigrationState *exec_start_outgoing_migration(Monitor *mon, |
| 56 | const char *command, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 57 | int64_t bandwidth_limit, |
| 58 | int detach, |
| 59 | int blk, |
| 60 | int inc) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 61 | { |
| 62 | FdMigrationState *s; |
| 63 | FILE *f; |
| 64 | |
| 65 | s = qemu_mallocz(sizeof(*s)); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 66 | |
| 67 | f = popen(command, "w"); |
| 68 | if (f == NULL) { |
| 69 | dprintf("Unable to popen exec target\n"); |
| 70 | goto err_after_alloc; |
| 71 | } |
| 72 | |
| 73 | s->fd = fileno(f); |
| 74 | if (s->fd == -1) { |
| 75 | dprintf("Unable to retrieve file descriptor for popen'd handle\n"); |
| 76 | goto err_after_open; |
| 77 | } |
| 78 | |
Chris Lalancette | 9075000 | 2009-08-05 17:07:35 +0200 | [diff] [blame] | 79 | socket_set_nonblock(s->fd); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 80 | |
| 81 | s->opaque = qemu_popen(f, "w"); |
| 82 | |
aliguori | 8ad9fa5 | 2008-11-12 22:29:11 +0000 | [diff] [blame] | 83 | s->close = exec_close; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 84 | s->get_error = file_errno; |
| 85 | s->write = file_write; |
| 86 | s->mig_state.cancel = migrate_fd_cancel; |
| 87 | s->mig_state.get_status = migrate_fd_get_status; |
| 88 | s->mig_state.release = migrate_fd_release; |
| 89 | |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 90 | s->mig_state.blk = blk; |
| 91 | s->mig_state.shared = inc; |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 92 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 93 | s->state = MIG_STATE_ACTIVE; |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 94 | s->mon = NULL; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 95 | s->bandwidth_limit = bandwidth_limit; |
| 96 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 97 | if (!detach) { |
| 98 | migrate_fd_monitor_suspend(s, mon); |
| 99 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 100 | |
| 101 | migrate_fd_connect(s); |
| 102 | return &s->mig_state; |
| 103 | |
| 104 | err_after_open: |
| 105 | pclose(f); |
| 106 | err_after_alloc: |
| 107 | qemu_free(s); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 108 | return NULL; |
| 109 | } |
| 110 | |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 111 | static void exec_accept_incoming_migration(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 112 | { |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 113 | QEMUFile *f = opaque; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 114 | int ret; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 115 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 116 | ret = qemu_loadvm_state(f); |
| 117 | if (ret < 0) { |
| 118 | fprintf(stderr, "load of migration failed\n"); |
| 119 | goto err; |
| 120 | } |
| 121 | qemu_announce_self(); |
| 122 | dprintf("successfully loaded vm state\n"); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 123 | /* we've successfully migrated, close the fd */ |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 124 | qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL); |
Paolo Bonzini | d399f67 | 2009-07-27 23:17:51 +0200 | [diff] [blame] | 125 | if (autostart) |
| 126 | vm_start(); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 127 | |
| 128 | err: |
| 129 | qemu_fclose(f); |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | int exec_start_incoming_migration(const char *command) |
| 133 | { |
| 134 | QEMUFile *f; |
| 135 | |
| 136 | dprintf("Attempting to start an incoming migration\n"); |
| 137 | f = qemu_popen_cmd(command, "r"); |
| 138 | if(f == NULL) { |
| 139 | dprintf("Unable to apply qemu wrapper to popen file\n"); |
| 140 | return -errno; |
| 141 | } |
| 142 | |
Paolo Bonzini | 7f79dd2 | 2009-08-12 14:17:35 +0200 | [diff] [blame] | 143 | qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, |
Chris Lalancette | 8a43b1e | 2009-05-25 16:38:23 +0200 | [diff] [blame] | 144 | exec_accept_incoming_migration, NULL, |
| 145 | (void *)(unsigned long)f); |
| 146 | |
| 147 | return 0; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 148 | } |