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