aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU live migration |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2008 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. See |
| 10 | * the COPYING file in the top-level directory. |
| 11 | * |
Paolo Bonzini | 6b620ca | 2012-01-13 17:44:23 +0100 | [diff] [blame] | 12 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 13 | * GNU GPL, version 2 or (at your option) any later version. |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "qemu-common.h" |
| 17 | #include "migration.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 18 | #include "monitor.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 19 | #include "buffered_file.h" |
| 20 | #include "sysemu.h" |
| 21 | #include "block.h" |
| 22 | #include "qemu_socket.h" |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 23 | #include "block-migration.h" |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 24 | #include "qmp-commands.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 25 | |
| 26 | //#define DEBUG_MIGRATION |
| 27 | |
| 28 | #ifdef DEBUG_MIGRATION |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 29 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 30 | do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
| 31 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 32 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 33 | do { } while (0) |
| 34 | #endif |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 35 | |
Juan Quintela | 7dc688e | 2011-02-23 00:48:46 +0100 | [diff] [blame] | 36 | enum { |
| 37 | MIG_STATE_ERROR, |
| 38 | MIG_STATE_SETUP, |
| 39 | MIG_STATE_CANCELLED, |
| 40 | MIG_STATE_ACTIVE, |
| 41 | MIG_STATE_COMPLETED, |
| 42 | }; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 43 | |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 44 | #define MAX_THROTTLE (32 << 20) /* Migration speed throttling */ |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 45 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 46 | static NotifierList migration_state_notifiers = |
| 47 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); |
| 48 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 49 | /* When we add fault tolerance, we could have several |
| 50 | migrations at once. For now we don't need to add |
| 51 | dynamic creation of migration */ |
| 52 | |
| 53 | static MigrationState *migrate_get_current(void) |
| 54 | { |
| 55 | static MigrationState current_migration = { |
| 56 | .state = MIG_STATE_SETUP, |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 57 | .bandwidth_limit = MAX_THROTTLE, |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | return ¤t_migration; |
| 61 | } |
| 62 | |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 63 | int qemu_start_incoming_migration(const char *uri) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 64 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 65 | const char *p; |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 66 | int ret; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 67 | |
| 68 | if (strstart(uri, "tcp:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 69 | ret = tcp_start_incoming_migration(p); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 70 | #if !defined(WIN32) |
| 71 | else if (strstart(uri, "exec:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 72 | ret = exec_start_incoming_migration(p); |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 73 | else if (strstart(uri, "unix:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 74 | ret = unix_start_incoming_migration(p); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 75 | else if (strstart(uri, "fd:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 76 | ret = fd_start_incoming_migration(p); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 77 | #endif |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 78 | else { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 79 | fprintf(stderr, "unknown migration protocol: %s\n", uri); |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 80 | ret = -EPROTONOSUPPORT; |
| 81 | } |
| 82 | return ret; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 85 | void process_incoming_migration(QEMUFile *f) |
| 86 | { |
| 87 | if (qemu_loadvm_state(f) < 0) { |
| 88 | fprintf(stderr, "load of migration failed\n"); |
| 89 | exit(0); |
| 90 | } |
| 91 | qemu_announce_self(); |
| 92 | DPRINTF("successfully loaded vm state\n"); |
| 93 | |
Anthony Liguori | 0f15423 | 2011-11-14 15:09:45 -0600 | [diff] [blame] | 94 | /* Make sure all file formats flush their mutable metadata */ |
| 95 | bdrv_invalidate_cache_all(); |
| 96 | |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 97 | if (autostart) { |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 98 | vm_start(); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 99 | } else { |
Luiz Capitulino | 0461d5a | 2011-09-30 14:45:27 -0300 | [diff] [blame] | 100 | runstate_set(RUN_STATE_PRELAUNCH); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame] | 101 | } |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 104 | /* amount of nanoseconds we are willing to wait for migration to be down. |
| 105 | * the choice of nanoseconds is because it is the maximum resolution that |
| 106 | * get_clock() can achieve. It is an internal measure. All user-visible |
| 107 | * units must be in seconds */ |
| 108 | static uint64_t max_downtime = 30000000; |
| 109 | |
| 110 | uint64_t migrate_max_downtime(void) |
| 111 | { |
| 112 | return max_downtime; |
| 113 | } |
| 114 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 115 | MigrationInfo *qmp_query_migrate(Error **errp) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 116 | { |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 117 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 118 | MigrationState *s = migrate_get_current(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 119 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 120 | switch (s->state) { |
| 121 | case MIG_STATE_SETUP: |
| 122 | /* no migration has happened ever */ |
| 123 | break; |
| 124 | case MIG_STATE_ACTIVE: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 125 | info->has_status = true; |
| 126 | info->status = g_strdup("active"); |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 127 | |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 128 | info->has_ram = true; |
| 129 | info->ram = g_malloc0(sizeof(*info->ram)); |
| 130 | info->ram->transferred = ram_bytes_transferred(); |
| 131 | info->ram->remaining = ram_bytes_remaining(); |
| 132 | info->ram->total = ram_bytes_total(); |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 133 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 134 | if (blk_mig_active()) { |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 135 | info->has_disk = true; |
| 136 | info->disk = g_malloc0(sizeof(*info->disk)); |
| 137 | info->disk->transferred = blk_mig_bytes_transferred(); |
| 138 | info->disk->remaining = blk_mig_bytes_remaining(); |
| 139 | info->disk->total = blk_mig_bytes_total(); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 140 | } |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 141 | break; |
| 142 | case MIG_STATE_COMPLETED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 143 | info->has_status = true; |
| 144 | info->status = g_strdup("completed"); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 145 | break; |
| 146 | case MIG_STATE_ERROR: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 147 | info->has_status = true; |
| 148 | info->status = g_strdup("failed"); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 149 | break; |
| 150 | case MIG_STATE_CANCELLED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 151 | info->has_status = true; |
| 152 | info->status = g_strdup("cancelled"); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 153 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 154 | } |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 155 | |
| 156 | return info; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 157 | } |
| 158 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 159 | /* shared migration helpers */ |
| 160 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 161 | static int migrate_fd_cleanup(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 162 | { |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 163 | int ret = 0; |
| 164 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 165 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 166 | |
| 167 | if (s->file) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 168 | DPRINTF("closing file\n"); |
Eduardo Habkost | a6d34a9 | 2011-11-10 10:41:42 -0200 | [diff] [blame] | 169 | ret = qemu_fclose(s->file); |
Jan Kiszka | 5d39c79 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 170 | s->file = NULL; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Jan Kiszka | 84ec655 | 2011-08-05 09:11:26 +0200 | [diff] [blame] | 173 | if (s->fd != -1) { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 174 | close(s->fd); |
Jan Kiszka | 84ec655 | 2011-08-05 09:11:26 +0200 | [diff] [blame] | 175 | s->fd = -1; |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 176 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 177 | |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 178 | return ret; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 181 | void migrate_fd_error(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 182 | { |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 183 | DPRINTF("setting error state\n"); |
| 184 | s->state = MIG_STATE_ERROR; |
Juan Quintela | e0eb739 | 2011-10-05 14:27:52 +0200 | [diff] [blame] | 185 | notifier_list_notify(&migration_state_notifiers, s); |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 186 | migrate_fd_cleanup(s); |
| 187 | } |
| 188 | |
Juan Quintela | 458cf28 | 2011-02-22 23:32:54 +0100 | [diff] [blame] | 189 | static void migrate_fd_completed(MigrationState *s) |
| 190 | { |
| 191 | DPRINTF("setting completed state\n"); |
| 192 | if (migrate_fd_cleanup(s) < 0) { |
| 193 | s->state = MIG_STATE_ERROR; |
| 194 | } else { |
| 195 | s->state = MIG_STATE_COMPLETED; |
| 196 | runstate_set(RUN_STATE_POSTMIGRATE); |
| 197 | } |
Juan Quintela | e0eb739 | 2011-10-05 14:27:52 +0200 | [diff] [blame] | 198 | notifier_list_notify(&migration_state_notifiers, s); |
Juan Quintela | 458cf28 | 2011-02-22 23:32:54 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 201 | static void migrate_fd_put_notify(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 202 | { |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 203 | MigrationState *s = opaque; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 204 | |
| 205 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 206 | qemu_file_put_notify(s->file); |
Luiz Capitulino | 1fdc11c | 2011-10-28 14:59:52 -0200 | [diff] [blame] | 207 | if (s->file && qemu_file_get_error(s->file)) { |
Yoshiaki Tamura | 2350e13 | 2011-02-23 00:01:24 +0900 | [diff] [blame] | 208 | migrate_fd_error(s); |
| 209 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 212 | static ssize_t migrate_fd_put_buffer(void *opaque, const void *data, |
| 213 | size_t size) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 214 | { |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 215 | MigrationState *s = opaque; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 216 | ssize_t ret; |
| 217 | |
Juan Quintela | fdbecb5 | 2011-09-21 22:37:29 +0200 | [diff] [blame] | 218 | if (s->state != MIG_STATE_ACTIVE) { |
| 219 | return -EIO; |
| 220 | } |
| 221 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 222 | do { |
| 223 | ret = s->write(s, data, size); |
Uri Lublin | 95b134e | 2009-05-19 14:08:53 +0300 | [diff] [blame] | 224 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 225 | |
| 226 | if (ret == -1) |
| 227 | ret = -(s->get_error(s)); |
| 228 | |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 229 | if (ret == -EAGAIN) { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 230 | qemu_set_fd_handler2(s->fd, NULL, NULL, migrate_fd_put_notify, s); |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 231 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 236 | static void migrate_fd_put_ready(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 237 | { |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 238 | MigrationState *s = opaque; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 239 | int ret; |
| 240 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 241 | if (s->state != MIG_STATE_ACTIVE) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 242 | DPRINTF("put_ready returning because of non-active state\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 246 | DPRINTF("iterate\n"); |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 247 | ret = qemu_savevm_state_iterate(s->file); |
Juan Quintela | 3934638 | 2011-09-22 11:02:14 +0200 | [diff] [blame] | 248 | if (ret < 0) { |
| 249 | migrate_fd_error(s); |
| 250 | } else if (ret == 1) { |
Luiz Capitulino | 1354869 | 2011-07-29 15:36:43 -0300 | [diff] [blame] | 251 | int old_vm_running = runstate_is_running(); |
Anthony Liguori | eeb34af | 2009-07-09 13:25:47 -0500 | [diff] [blame] | 252 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 253 | DPRINTF("done iterating\n"); |
Luiz Capitulino | 8a9236f | 2011-10-14 11:18:09 -0300 | [diff] [blame] | 254 | vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 255 | |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 256 | if (qemu_savevm_state_complete(s->file) < 0) { |
Juan Quintela | 67afff7 | 2011-02-22 23:18:20 +0100 | [diff] [blame] | 257 | migrate_fd_error(s); |
aliguori | b161d12 | 2009-04-05 19:30:33 +0000 | [diff] [blame] | 258 | } else { |
Juan Quintela | 458cf28 | 2011-02-22 23:32:54 +0100 | [diff] [blame] | 259 | migrate_fd_completed(s); |
aliguori | b161d12 | 2009-04-05 19:30:33 +0000 | [diff] [blame] | 260 | } |
Juan Quintela | 48a2f4d | 2010-05-11 23:28:53 +0200 | [diff] [blame] | 261 | if (s->state != MIG_STATE_COMPLETED) { |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 262 | if (old_vm_running) { |
| 263 | vm_start(); |
| 264 | } |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 265 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 269 | static void migrate_fd_cancel(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 270 | { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 271 | if (s->state != MIG_STATE_ACTIVE) |
| 272 | return; |
| 273 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 274 | DPRINTF("cancelling migration\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 275 | |
| 276 | s->state = MIG_STATE_CANCELLED; |
Juan Quintela | e0eb739 | 2011-10-05 14:27:52 +0200 | [diff] [blame] | 277 | notifier_list_notify(&migration_state_notifiers, s); |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 278 | qemu_savevm_state_cancel(s->file); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 279 | |
| 280 | migrate_fd_cleanup(s); |
| 281 | } |
| 282 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 283 | static void migrate_fd_wait_for_unfreeze(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 284 | { |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 285 | MigrationState *s = opaque; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 286 | int ret; |
| 287 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 288 | DPRINTF("wait for unfreeze\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 289 | if (s->state != MIG_STATE_ACTIVE) |
| 290 | return; |
| 291 | |
| 292 | do { |
| 293 | fd_set wfds; |
| 294 | |
| 295 | FD_ZERO(&wfds); |
| 296 | FD_SET(s->fd, &wfds); |
| 297 | |
| 298 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); |
| 299 | } while (ret == -1 && (s->get_error(s)) == EINTR); |
Juan Quintela | af50945 | 2011-09-21 22:46:36 +0200 | [diff] [blame] | 300 | |
| 301 | if (ret == -1) { |
Juan Quintela | dcd1d22 | 2011-09-21 23:01:54 +0200 | [diff] [blame] | 302 | qemu_file_set_error(s->file, -s->get_error(s)); |
Juan Quintela | af50945 | 2011-09-21 22:46:36 +0200 | [diff] [blame] | 303 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 306 | static int migrate_fd_close(void *opaque) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 307 | { |
Juan Quintela | 22f00a4 | 2010-05-11 15:56:35 +0200 | [diff] [blame] | 308 | MigrationState *s = opaque; |
Uri Lublin | e19252d | 2009-06-08 14:28:01 +0300 | [diff] [blame] | 309 | |
| 310 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 311 | return s->close(s); |
| 312 | } |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 313 | |
| 314 | void add_migration_state_change_notifier(Notifier *notify) |
| 315 | { |
| 316 | notifier_list_add(&migration_state_notifiers, notify); |
| 317 | } |
| 318 | |
| 319 | void remove_migration_state_change_notifier(Notifier *notify) |
| 320 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 321 | notifier_remove(notify); |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 324 | bool migration_is_active(MigrationState *s) |
| 325 | { |
| 326 | return s->state == MIG_STATE_ACTIVE; |
| 327 | } |
| 328 | |
Juan Quintela | 7073693 | 2011-02-23 00:43:59 +0100 | [diff] [blame] | 329 | bool migration_has_finished(MigrationState *s) |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 330 | { |
Juan Quintela | 7073693 | 2011-02-23 00:43:59 +0100 | [diff] [blame] | 331 | return s->state == MIG_STATE_COMPLETED; |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 332 | } |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 333 | |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 334 | bool migration_has_failed(MigrationState *s) |
| 335 | { |
| 336 | return (s->state == MIG_STATE_CANCELLED || |
| 337 | s->state == MIG_STATE_ERROR); |
| 338 | } |
| 339 | |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 340 | void migrate_fd_connect(MigrationState *s) |
| 341 | { |
| 342 | int ret; |
| 343 | |
Juan Quintela | d5934dd | 2010-05-11 23:01:53 +0200 | [diff] [blame] | 344 | s->state = MIG_STATE_ACTIVE; |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 345 | s->file = qemu_fopen_ops_buffered(s, |
| 346 | s->bandwidth_limit, |
| 347 | migrate_fd_put_buffer, |
| 348 | migrate_fd_put_ready, |
| 349 | migrate_fd_wait_for_unfreeze, |
| 350 | migrate_fd_close); |
| 351 | |
| 352 | DPRINTF("beginning savevm\n"); |
Luiz Capitulino | 539de12 | 2011-12-05 14:06:56 -0200 | [diff] [blame] | 353 | ret = qemu_savevm_state_begin(s->file, s->blk, s->shared); |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 354 | if (ret < 0) { |
| 355 | DPRINTF("failed, %d\n", ret); |
| 356 | migrate_fd_error(s); |
| 357 | return; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 358 | } |
Juan Quintela | 8b6b99b | 2011-09-11 20:28:22 +0200 | [diff] [blame] | 359 | migrate_fd_put_ready(s); |
| 360 | } |
| 361 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 362 | static MigrationState *migrate_init(int blk, int inc) |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 363 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 364 | MigrationState *s = migrate_get_current(); |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 365 | int64_t bandwidth_limit = s->bandwidth_limit; |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 366 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 367 | memset(s, 0, sizeof(*s)); |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 368 | s->bandwidth_limit = bandwidth_limit; |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 369 | s->blk = blk; |
| 370 | s->shared = inc; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 371 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 372 | s->bandwidth_limit = bandwidth_limit; |
Juan Quintela | d5934dd | 2010-05-11 23:01:53 +0200 | [diff] [blame] | 373 | s->state = MIG_STATE_SETUP; |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 374 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 375 | return s; |
| 376 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 377 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 378 | static GSList *migration_blockers; |
| 379 | |
| 380 | void migrate_add_blocker(Error *reason) |
| 381 | { |
| 382 | migration_blockers = g_slist_prepend(migration_blockers, reason); |
| 383 | } |
| 384 | |
| 385 | void migrate_del_blocker(Error *reason) |
| 386 | { |
| 387 | migration_blockers = g_slist_remove(migration_blockers, reason); |
| 388 | } |
| 389 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 390 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
| 391 | bool has_inc, bool inc, bool has_detach, bool detach, |
| 392 | Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 393 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 394 | MigrationState *s = migrate_get_current(); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 395 | const char *p; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 396 | int ret; |
| 397 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 398 | if (s->state == MIG_STATE_ACTIVE) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 399 | error_set(errp, QERR_MIGRATION_ACTIVE); |
| 400 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 401 | } |
| 402 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 403 | if (qemu_savevm_state_blocked(errp)) { |
| 404 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 407 | if (migration_blockers) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 408 | *errp = error_copy(migration_blockers->data); |
| 409 | return; |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 410 | } |
| 411 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 412 | s = migrate_init(blk, inc); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 413 | |
| 414 | if (strstart(uri, "tcp:", &p)) { |
| 415 | ret = tcp_start_outgoing_migration(s, p); |
| 416 | #if !defined(WIN32) |
| 417 | } else if (strstart(uri, "exec:", &p)) { |
| 418 | ret = exec_start_outgoing_migration(s, p); |
| 419 | } else if (strstart(uri, "unix:", &p)) { |
| 420 | ret = unix_start_outgoing_migration(s, p); |
| 421 | } else if (strstart(uri, "fd:", &p)) { |
| 422 | ret = fd_start_outgoing_migration(s, p); |
| 423 | #endif |
| 424 | } else { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 425 | error_set(errp, QERR_INVALID_PARAMETER_VALUE, "uri", "a valid migration protocol"); |
| 426 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (ret < 0) { |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 430 | DPRINTF("migration failed: %s\n", strerror(-ret)); |
| 431 | /* FIXME: we should return meaningful errors */ |
| 432 | error_set(errp, QERR_UNDEFINED_ERROR); |
| 433 | return; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 434 | } |
| 435 | |
Juan Quintela | e0eb739 | 2011-10-05 14:27:52 +0200 | [diff] [blame] | 436 | notifier_list_notify(&migration_state_notifiers, s); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Luiz Capitulino | 6cdedb0 | 2011-11-27 22:54:09 -0200 | [diff] [blame] | 439 | void qmp_migrate_cancel(Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 440 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 441 | migrate_fd_cancel(migrate_get_current()); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 444 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 445 | { |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 446 | MigrationState *s; |
| 447 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 448 | if (value < 0) { |
| 449 | value = 0; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 450 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 451 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 452 | s = migrate_get_current(); |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 453 | s->bandwidth_limit = value; |
Juan Quintela | d0ae46c | 2011-02-23 00:33:19 +0100 | [diff] [blame] | 454 | qemu_file_set_rate_limit(s->file, s->bandwidth_limit); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 455 | } |
| 456 | |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 457 | void qmp_migrate_set_downtime(double value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 458 | { |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 459 | value *= 1e9; |
| 460 | value = MAX(0, MIN(UINT64_MAX, value)); |
| 461 | max_downtime = (uint64_t)value; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 462 | } |