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