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 | * |
| 12 | */ |
| 13 | |
| 14 | #include "qemu-common.h" |
| 15 | #include "migration.h" |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 16 | #include "monitor.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 17 | #include "buffered_file.h" |
| 18 | #include "sysemu.h" |
| 19 | #include "block.h" |
| 20 | #include "qemu_socket.h" |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 21 | #include "block-migration.h" |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 22 | #include "qemu-objects.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 23 | |
| 24 | //#define DEBUG_MIGRATION |
| 25 | |
| 26 | #ifdef DEBUG_MIGRATION |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 27 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 28 | do { printf("migration: " fmt, ## __VA_ARGS__); } while (0) |
| 29 | #else |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 30 | #define DPRINTF(fmt, ...) \ |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 31 | do { } while (0) |
| 32 | #endif |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 33 | |
| 34 | /* Migration speed throttling */ |
Michael S. Tsirkin | 3d002df | 2010-11-23 19:05:54 +0200 | [diff] [blame] | 35 | static int64_t max_throttle = (32 << 20); |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 36 | |
| 37 | static MigrationState *current_migration; |
| 38 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 39 | static NotifierList migration_state_notifiers = |
| 40 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); |
| 41 | |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 42 | int qemu_start_incoming_migration(const char *uri) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 43 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 44 | const char *p; |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 45 | int ret; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 46 | |
| 47 | if (strstart(uri, "tcp:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 48 | ret = tcp_start_incoming_migration(p); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 49 | #if !defined(WIN32) |
| 50 | else if (strstart(uri, "exec:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 51 | ret = exec_start_incoming_migration(p); |
Chris Lalancette | 4951f65 | 2009-08-05 17:24:29 +0200 | [diff] [blame] | 52 | else if (strstart(uri, "unix:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 53 | ret = unix_start_incoming_migration(p); |
Paolo Bonzini | 5ac1fad | 2009-08-18 15:56:25 +0200 | [diff] [blame] | 54 | else if (strstart(uri, "fd:", &p)) |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 55 | ret = fd_start_incoming_migration(p); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 56 | #endif |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 57 | else { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 58 | fprintf(stderr, "unknown migration protocol: %s\n", uri); |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 59 | ret = -EPROTONOSUPPORT; |
| 60 | } |
| 61 | return ret; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 64 | void process_incoming_migration(QEMUFile *f) |
| 65 | { |
| 66 | if (qemu_loadvm_state(f) < 0) { |
| 67 | fprintf(stderr, "load of migration failed\n"); |
| 68 | exit(0); |
| 69 | } |
| 70 | qemu_announce_self(); |
| 71 | DPRINTF("successfully loaded vm state\n"); |
| 72 | |
Amit Shah | 8e84865 | 2010-07-27 15:49:19 +0530 | [diff] [blame] | 73 | incoming_expected = false; |
| 74 | |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame^] | 75 | if (autostart) { |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 76 | vm_start(); |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame^] | 77 | } else { |
| 78 | runstate_set(RSTATE_PRE_LAUNCH); |
| 79 | } |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 82 | int do_migrate(Monitor *mon, const QDict *qdict, QObject **ret_data) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 83 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 84 | MigrationState *s = NULL; |
| 85 | const char *p; |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 86 | int detach = qdict_get_try_bool(qdict, "detach", 0); |
| 87 | int blk = qdict_get_try_bool(qdict, "blk", 0); |
| 88 | int inc = qdict_get_try_bool(qdict, "inc", 0); |
Luiz Capitulino | f18c16d | 2009-08-28 15:27:14 -0300 | [diff] [blame] | 89 | const char *uri = qdict_get_str(qdict, "uri"); |
Jan Kiszka | 1302425 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 90 | |
| 91 | if (current_migration && |
| 92 | current_migration->get_status(current_migration) == MIG_STATE_ACTIVE) { |
| 93 | monitor_printf(mon, "migration already in progress\n"); |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 94 | return -1; |
Jan Kiszka | 1302425 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Alex Williamson | dc91212 | 2011-01-11 14:39:43 -0700 | [diff] [blame] | 97 | if (qemu_savevm_state_blocked(mon)) { |
| 98 | return -1; |
| 99 | } |
| 100 | |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 101 | if (strstart(uri, "tcp:", &p)) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 102 | s = tcp_start_outgoing_migration(mon, p, max_throttle, detach, |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 103 | blk, inc); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 104 | #if !defined(WIN32) |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 105 | } else if (strstart(uri, "exec:", &p)) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 106 | s = exec_start_outgoing_migration(mon, p, max_throttle, detach, |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 107 | blk, inc); |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 108 | } else if (strstart(uri, "unix:", &p)) { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 109 | s = unix_start_outgoing_migration(mon, p, max_throttle, detach, |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 110 | blk, inc); |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 111 | } else if (strstart(uri, "fd:", &p)) { |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 112 | s = fd_start_outgoing_migration(mon, p, max_throttle, detach, |
Luiz Capitulino | eb159d1 | 2010-05-28 15:25:24 -0300 | [diff] [blame] | 113 | blk, inc); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 114 | #endif |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 115 | } else { |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 116 | monitor_printf(mon, "unknown migration protocol: %s\n", uri); |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 117 | return -1; |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 118 | } |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 119 | |
| 120 | if (s == NULL) { |
| 121 | monitor_printf(mon, "migration failed\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | if (current_migration) { |
| 126 | current_migration->release(current_migration); |
| 127 | } |
| 128 | |
| 129 | current_migration = s; |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 130 | notifier_list_notify(&migration_state_notifiers, NULL); |
Luiz Capitulino | b5d17ad | 2010-02-10 23:49:57 -0200 | [diff] [blame] | 131 | return 0; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Luiz Capitulino | ef4b7ee | 2010-02-10 23:49:48 -0200 | [diff] [blame] | 134 | int do_migrate_cancel(Monitor *mon, const QDict *qdict, QObject **ret_data) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 135 | { |
| 136 | MigrationState *s = current_migration; |
| 137 | |
| 138 | if (s) |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 139 | s->cancel(s); |
Luiz Capitulino | ef4b7ee | 2010-02-10 23:49:48 -0200 | [diff] [blame] | 140 | |
| 141 | return 0; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Luiz Capitulino | ef4b7ee | 2010-02-10 23:49:48 -0200 | [diff] [blame] | 144 | int do_migrate_set_speed(Monitor *mon, const QDict *qdict, QObject **ret_data) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 145 | { |
Jes Sorensen | ed3d4a8 | 2010-10-21 17:15:48 +0200 | [diff] [blame] | 146 | int64_t d; |
Glauber Costa | daa91de | 2009-05-20 18:26:58 -0400 | [diff] [blame] | 147 | FdMigrationState *s; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 148 | |
Jes Sorensen | ed3d4a8 | 2010-10-21 17:15:48 +0200 | [diff] [blame] | 149 | d = qdict_get_int(qdict, "value"); |
Michael S. Tsirkin | 3d002df | 2010-11-23 19:05:54 +0200 | [diff] [blame] | 150 | if (d < 0) { |
| 151 | d = 0; |
| 152 | } |
Markus Armbruster | 5667c49 | 2010-01-25 14:23:04 +0100 | [diff] [blame] | 153 | max_throttle = d; |
Glauber Costa | daa91de | 2009-05-20 18:26:58 -0400 | [diff] [blame] | 154 | |
Jan Kiszka | 5d39c79 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 155 | s = migrate_to_fms(current_migration); |
| 156 | if (s && s->file) { |
Glauber Costa | daa91de | 2009-05-20 18:26:58 -0400 | [diff] [blame] | 157 | qemu_file_set_rate_limit(s->file, max_throttle); |
| 158 | } |
Luiz Capitulino | ef4b7ee | 2010-02-10 23:49:48 -0200 | [diff] [blame] | 159 | |
| 160 | return 0; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Glauber Costa | a0a3fd6 | 2009-05-28 15:22:57 -0400 | [diff] [blame] | 163 | /* amount of nanoseconds we are willing to wait for migration to be down. |
| 164 | * the choice of nanoseconds is because it is the maximum resolution that |
| 165 | * get_clock() can achieve. It is an internal measure. All user-visible |
| 166 | * units must be in seconds */ |
| 167 | static uint64_t max_downtime = 30000000; |
| 168 | |
| 169 | uint64_t migrate_max_downtime(void) |
| 170 | { |
| 171 | return max_downtime; |
| 172 | } |
| 173 | |
Luiz Capitulino | ef4b7ee | 2010-02-10 23:49:48 -0200 | [diff] [blame] | 174 | int do_migrate_set_downtime(Monitor *mon, const QDict *qdict, |
| 175 | QObject **ret_data) |
Glauber Costa | 2ea4295 | 2009-05-28 15:22:58 -0400 | [diff] [blame] | 176 | { |
Glauber Costa | 2ea4295 | 2009-05-28 15:22:58 -0400 | [diff] [blame] | 177 | double d; |
| 178 | |
Markus Armbruster | b0fbf7d | 2010-01-25 14:23:07 +0100 | [diff] [blame] | 179 | d = qdict_get_double(qdict, "value") * 1e9; |
| 180 | d = MAX(0, MIN(UINT64_MAX, d)); |
Glauber Costa | 2ea4295 | 2009-05-28 15:22:58 -0400 | [diff] [blame] | 181 | max_downtime = (uint64_t)d; |
Luiz Capitulino | ef4b7ee | 2010-02-10 23:49:48 -0200 | [diff] [blame] | 182 | |
| 183 | return 0; |
Glauber Costa | 2ea4295 | 2009-05-28 15:22:58 -0400 | [diff] [blame] | 184 | } |
| 185 | |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 186 | static void migrate_print_status(Monitor *mon, const char *name, |
| 187 | const QDict *status_dict) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 188 | { |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 189 | QDict *qdict; |
| 190 | |
| 191 | qdict = qobject_to_qdict(qdict_get(status_dict, name)); |
| 192 | |
| 193 | monitor_printf(mon, "transferred %s: %" PRIu64 " kbytes\n", name, |
| 194 | qdict_get_int(qdict, "transferred") >> 10); |
| 195 | monitor_printf(mon, "remaining %s: %" PRIu64 " kbytes\n", name, |
| 196 | qdict_get_int(qdict, "remaining") >> 10); |
| 197 | monitor_printf(mon, "total %s: %" PRIu64 " kbytes\n", name, |
| 198 | qdict_get_int(qdict, "total") >> 10); |
| 199 | } |
| 200 | |
| 201 | void do_info_migrate_print(Monitor *mon, const QObject *data) |
| 202 | { |
| 203 | QDict *qdict; |
| 204 | |
| 205 | qdict = qobject_to_qdict(data); |
| 206 | |
| 207 | monitor_printf(mon, "Migration status: %s\n", |
| 208 | qdict_get_str(qdict, "status")); |
| 209 | |
| 210 | if (qdict_haskey(qdict, "ram")) { |
| 211 | migrate_print_status(mon, "ram", qdict); |
| 212 | } |
| 213 | |
| 214 | if (qdict_haskey(qdict, "disk")) { |
| 215 | migrate_print_status(mon, "disk", qdict); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static void migrate_put_status(QDict *qdict, const char *name, |
| 220 | uint64_t trans, uint64_t rem, uint64_t total) |
| 221 | { |
| 222 | QObject *obj; |
| 223 | |
| 224 | obj = qobject_from_jsonf("{ 'transferred': %" PRId64 ", " |
| 225 | "'remaining': %" PRId64 ", " |
| 226 | "'total': %" PRId64 " }", trans, rem, total); |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 227 | qdict_put_obj(qdict, name, obj); |
| 228 | } |
| 229 | |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 230 | void do_info_migrate(Monitor *mon, QObject **ret_data) |
| 231 | { |
| 232 | QDict *qdict; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 233 | MigrationState *s = current_migration; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 234 | |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 235 | if (s) { |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 236 | switch (s->get_status(s)) { |
| 237 | case MIG_STATE_ACTIVE: |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 238 | qdict = qdict_new(); |
| 239 | qdict_put(qdict, "status", qstring_from_str("active")); |
| 240 | |
| 241 | migrate_put_status(qdict, "ram", ram_bytes_transferred(), |
| 242 | ram_bytes_remaining(), ram_bytes_total()); |
| 243 | |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 244 | if (blk_mig_active()) { |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 245 | migrate_put_status(qdict, "disk", blk_mig_bytes_transferred(), |
| 246 | blk_mig_bytes_remaining(), |
| 247 | blk_mig_bytes_total()); |
Jan Kiszka | 25f2364 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 248 | } |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 249 | |
| 250 | *ret_data = QOBJECT(qdict); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 251 | break; |
| 252 | case MIG_STATE_COMPLETED: |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 253 | *ret_data = qobject_from_jsonf("{ 'status': 'completed' }"); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 254 | break; |
| 255 | case MIG_STATE_ERROR: |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 256 | *ret_data = qobject_from_jsonf("{ 'status': 'failed' }"); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 257 | break; |
| 258 | case MIG_STATE_CANCELLED: |
Luiz Capitulino | c86a668 | 2009-12-10 17:16:05 -0200 | [diff] [blame] | 259 | *ret_data = qobject_from_jsonf("{ 'status': 'cancelled' }"); |
aliguori | ff8d81d | 2008-10-24 22:10:31 +0000 | [diff] [blame] | 260 | break; |
| 261 | } |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 265 | /* shared migration helpers */ |
| 266 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 267 | void migrate_fd_monitor_suspend(FdMigrationState *s, Monitor *mon) |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 268 | { |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 269 | s->mon = mon; |
| 270 | if (monitor_suspend(mon) == 0) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 271 | DPRINTF("suspending monitor\n"); |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 272 | } else { |
| 273 | monitor_printf(mon, "terminal does not allow synchronous " |
aliguori | cde76ee | 2009-03-05 23:01:51 +0000 | [diff] [blame] | 274 | "migration, continuing detached\n"); |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 275 | } |
aliguori | 731b036 | 2009-03-05 23:01:42 +0000 | [diff] [blame] | 276 | } |
| 277 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 278 | void migrate_fd_error(FdMigrationState *s) |
| 279 | { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 280 | DPRINTF("setting error state\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 281 | s->state = MIG_STATE_ERROR; |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 282 | notifier_list_notify(&migration_state_notifiers, NULL); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 283 | migrate_fd_cleanup(s); |
| 284 | } |
| 285 | |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 286 | int migrate_fd_cleanup(FdMigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 287 | { |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 288 | int ret = 0; |
| 289 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 290 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 291 | |
| 292 | if (s->file) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 293 | DPRINTF("closing file\n"); |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 294 | if (qemu_fclose(s->file) != 0) { |
| 295 | ret = -1; |
| 296 | } |
Jan Kiszka | 5d39c79 | 2009-11-30 18:21:19 +0100 | [diff] [blame] | 297 | s->file = NULL; |
Jan Kiszka | 84ec655 | 2011-08-05 09:11:26 +0200 | [diff] [blame] | 298 | } else { |
| 299 | if (s->mon) { |
| 300 | monitor_resume(s->mon); |
| 301 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Jan Kiszka | 84ec655 | 2011-08-05 09:11:26 +0200 | [diff] [blame] | 304 | if (s->fd != -1) { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 305 | close(s->fd); |
Jan Kiszka | 84ec655 | 2011-08-05 09:11:26 +0200 | [diff] [blame] | 306 | s->fd = -1; |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 307 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 308 | |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 309 | return ret; |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void migrate_fd_put_notify(void *opaque) |
| 313 | { |
| 314 | FdMigrationState *s = opaque; |
| 315 | |
| 316 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
| 317 | qemu_file_put_notify(s->file); |
| 318 | } |
| 319 | |
| 320 | ssize_t migrate_fd_put_buffer(void *opaque, const void *data, size_t size) |
| 321 | { |
| 322 | FdMigrationState *s = opaque; |
| 323 | ssize_t ret; |
| 324 | |
| 325 | do { |
| 326 | ret = s->write(s, data, size); |
Uri Lublin | 95b134e | 2009-05-19 14:08:53 +0300 | [diff] [blame] | 327 | } while (ret == -1 && ((s->get_error(s)) == EINTR)); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 328 | |
| 329 | if (ret == -1) |
| 330 | ret = -(s->get_error(s)); |
| 331 | |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 332 | if (ret == -EAGAIN) { |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 333 | 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] | 334 | } else if (ret < 0) { |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 335 | s->state = MIG_STATE_ERROR; |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 336 | notifier_list_notify(&migration_state_notifiers, NULL); |
Marcelo Tosatti | e447b1a | 2010-08-19 10:18:39 -0300 | [diff] [blame] | 337 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 338 | |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | void migrate_fd_connect(FdMigrationState *s) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | s->file = qemu_fopen_ops_buffered(s, |
| 347 | s->bandwidth_limit, |
| 348 | migrate_fd_put_buffer, |
| 349 | migrate_fd_put_ready, |
| 350 | migrate_fd_wait_for_unfreeze, |
| 351 | migrate_fd_close); |
| 352 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 353 | DPRINTF("beginning savevm\n"); |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 354 | ret = qemu_savevm_state_begin(s->mon, s->file, s->mig_state.blk, |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 355 | s->mig_state.shared); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 356 | if (ret < 0) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 357 | DPRINTF("failed, %d\n", ret); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 358 | migrate_fd_error(s); |
| 359 | return; |
| 360 | } |
lirans@il.ibm.com | c163b5c | 2009-11-02 15:40:58 +0200 | [diff] [blame] | 361 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 362 | migrate_fd_put_ready(s); |
| 363 | } |
| 364 | |
| 365 | void migrate_fd_put_ready(void *opaque) |
| 366 | { |
| 367 | FdMigrationState *s = opaque; |
| 368 | |
| 369 | if (s->state != MIG_STATE_ACTIVE) { |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 370 | DPRINTF("put_ready returning because of non-active state\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 374 | DPRINTF("iterate\n"); |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 375 | if (qemu_savevm_state_iterate(s->mon, s->file) == 1) { |
aliguori | b161d12 | 2009-04-05 19:30:33 +0000 | [diff] [blame] | 376 | int state; |
Anthony Liguori | eeb34af | 2009-07-09 13:25:47 -0500 | [diff] [blame] | 377 | int old_vm_running = vm_running; |
| 378 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 379 | DPRINTF("done iterating\n"); |
Luiz Capitulino | 1dfb4dd | 2011-07-29 14:26:33 -0300 | [diff] [blame] | 380 | vm_stop(RSTATE_PRE_MIGRATE); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 381 | |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 382 | if ((qemu_savevm_state_complete(s->mon, s->file)) < 0) { |
Anthony Liguori | eeb34af | 2009-07-09 13:25:47 -0500 | [diff] [blame] | 383 | if (old_vm_running) { |
| 384 | vm_start(); |
| 385 | } |
aliguori | b161d12 | 2009-04-05 19:30:33 +0000 | [diff] [blame] | 386 | state = MIG_STATE_ERROR; |
| 387 | } else { |
| 388 | state = MIG_STATE_COMPLETED; |
| 389 | } |
Anthony Liguori | 41ef56e | 2010-06-02 14:55:25 -0500 | [diff] [blame] | 390 | if (migrate_fd_cleanup(s) < 0) { |
| 391 | if (old_vm_running) { |
| 392 | vm_start(); |
| 393 | } |
| 394 | state = MIG_STATE_ERROR; |
| 395 | } |
Luiz Capitulino | f5bbfba | 2011-07-29 15:04:45 -0300 | [diff] [blame^] | 396 | if (state == MIG_STATE_COMPLETED) { |
| 397 | runstate_set(RSTATE_POST_MIGRATE); |
| 398 | } |
aliguori | b161d12 | 2009-04-05 19:30:33 +0000 | [diff] [blame] | 399 | s->state = state; |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 400 | notifier_list_notify(&migration_state_notifiers, NULL); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | int migrate_fd_get_status(MigrationState *mig_state) |
| 405 | { |
| 406 | FdMigrationState *s = migrate_to_fms(mig_state); |
| 407 | return s->state; |
| 408 | } |
| 409 | |
| 410 | void migrate_fd_cancel(MigrationState *mig_state) |
| 411 | { |
| 412 | FdMigrationState *s = migrate_to_fms(mig_state); |
| 413 | |
| 414 | if (s->state != MIG_STATE_ACTIVE) |
| 415 | return; |
| 416 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 417 | DPRINTF("cancelling migration\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 418 | |
| 419 | s->state = MIG_STATE_CANCELLED; |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 420 | notifier_list_notify(&migration_state_notifiers, NULL); |
Jan Kiszka | f327aa0 | 2009-11-30 18:21:21 +0100 | [diff] [blame] | 421 | qemu_savevm_state_cancel(s->mon, s->file); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 422 | |
| 423 | migrate_fd_cleanup(s); |
| 424 | } |
| 425 | |
| 426 | void migrate_fd_release(MigrationState *mig_state) |
| 427 | { |
| 428 | FdMigrationState *s = migrate_to_fms(mig_state); |
| 429 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 430 | DPRINTF("releasing state\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 431 | |
| 432 | if (s->state == MIG_STATE_ACTIVE) { |
| 433 | s->state = MIG_STATE_CANCELLED; |
Jan Kiszka | 9e8dd45 | 2011-06-20 14:06:26 +0200 | [diff] [blame] | 434 | notifier_list_notify(&migration_state_notifiers, NULL); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 435 | migrate_fd_cleanup(s); |
| 436 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 437 | g_free(s); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void migrate_fd_wait_for_unfreeze(void *opaque) |
| 441 | { |
| 442 | FdMigrationState *s = opaque; |
| 443 | int ret; |
| 444 | |
malc | d0f2c4c | 2010-02-07 02:03:50 +0300 | [diff] [blame] | 445 | DPRINTF("wait for unfreeze\n"); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 446 | if (s->state != MIG_STATE_ACTIVE) |
| 447 | return; |
| 448 | |
| 449 | do { |
| 450 | fd_set wfds; |
| 451 | |
| 452 | FD_ZERO(&wfds); |
| 453 | FD_SET(s->fd, &wfds); |
| 454 | |
| 455 | ret = select(s->fd + 1, NULL, &wfds, NULL, NULL); |
| 456 | } while (ret == -1 && (s->get_error(s)) == EINTR); |
| 457 | } |
| 458 | |
| 459 | int migrate_fd_close(void *opaque) |
| 460 | { |
| 461 | FdMigrationState *s = opaque; |
Uri Lublin | e19252d | 2009-06-08 14:28:01 +0300 | [diff] [blame] | 462 | |
Jan Kiszka | 84ec655 | 2011-08-05 09:11:26 +0200 | [diff] [blame] | 463 | if (s->mon) { |
| 464 | monitor_resume(s->mon); |
| 465 | } |
Uri Lublin | e19252d | 2009-06-08 14:28:01 +0300 | [diff] [blame] | 466 | qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 467 | return s->close(s); |
| 468 | } |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 469 | |
| 470 | void add_migration_state_change_notifier(Notifier *notify) |
| 471 | { |
| 472 | notifier_list_add(&migration_state_notifiers, notify); |
| 473 | } |
| 474 | |
| 475 | void remove_migration_state_change_notifier(Notifier *notify) |
| 476 | { |
| 477 | notifier_list_remove(&migration_state_notifiers, notify); |
| 478 | } |
| 479 | |
| 480 | int get_migration_state(void) |
| 481 | { |
| 482 | if (current_migration) { |
| 483 | return migrate_fd_get_status(current_migration); |
| 484 | } else { |
| 485 | return MIG_STATE_ERROR; |
| 486 | } |
| 487 | } |