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 | |
Peter Maydell | 1393a48 | 2016-01-26 18:16:54 +0000 | [diff] [blame] | 16 | #include "qemu/osdep.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 17 | #include "qemu/cutils.h" |
Markus Armbruster | d49b683 | 2015-03-17 18:29:20 +0100 | [diff] [blame] | 18 | #include "qemu/error-report.h" |
Markus Armbruster | db72581 | 2019-08-12 07:23:50 +0200 | [diff] [blame] | 19 | #include "qemu/main-loop.h" |
Juan Quintela | 795c40b | 2017-04-06 12:00:28 +0200 | [diff] [blame] | 20 | #include "migration/blocker.h" |
Juan Quintela | f4dbe1b | 2017-04-05 15:54:10 +0200 | [diff] [blame] | 21 | #include "exec.h" |
Juan Quintela | 7fcac4a | 2017-04-05 15:58:29 +0200 | [diff] [blame] | 22 | #include "fd.h" |
Juan Quintela | 61e8b14 | 2017-04-05 17:40:11 +0200 | [diff] [blame] | 23 | #include "socket.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 24 | #include "sysemu/runstate.h" |
Markus Armbruster | 46517dd | 2019-08-12 07:23:57 +0200 | [diff] [blame] | 25 | #include "sysemu/sysemu.h" |
Juan Quintela | e1a3ece | 2017-04-17 20:32:36 +0200 | [diff] [blame] | 26 | #include "rdma.h" |
Juan Quintela | 7b1e1a2 | 2017-04-17 20:26:27 +0200 | [diff] [blame] | 27 | #include "ram.h" |
Juan Quintela | 84a899d | 2017-04-24 18:53:30 +0200 | [diff] [blame] | 28 | #include "migration/global_state.h" |
Juan Quintela | c4b63b7 | 2017-04-24 19:02:44 +0200 | [diff] [blame] | 29 | #include "migration/misc.h" |
Juan Quintela | 6666c96 | 2017-04-24 20:07:27 +0200 | [diff] [blame] | 30 | #include "migration.h" |
Juan Quintela | 20a519a | 2017-04-20 14:48:46 +0200 | [diff] [blame] | 31 | #include "savevm.h" |
Juan Quintela | 40014d8 | 2017-04-17 19:34:36 +0200 | [diff] [blame] | 32 | #include "qemu-file-channel.h" |
Juan Quintela | 08a0aee | 2017-04-20 18:52:18 +0200 | [diff] [blame] | 33 | #include "qemu-file.h" |
Juan Quintela | 987772d | 2017-04-17 19:02:59 +0200 | [diff] [blame] | 34 | #include "migration/vmstate.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 35 | #include "block/block.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 36 | #include "qapi/error.h" |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 37 | #include "qapi/clone-visitor.h" |
| 38 | #include "qapi/qapi-visit-sockets.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 39 | #include "qapi/qapi-commands-migration.h" |
| 40 | #include "qapi/qapi-events-migration.h" |
Markus Armbruster | cc7a8ea | 2015-03-17 17:22:46 +0100 | [diff] [blame] | 41 | #include "qapi/qmp/qerror.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 42 | #include "qapi/qmp/qnull.h" |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 43 | #include "qemu/rcu.h" |
Juan Quintela | 2c9e6fe | 2017-04-21 14:31:22 +0200 | [diff] [blame] | 44 | #include "block.h" |
Juan Quintela | be07b0a | 2017-04-20 13:12:24 +0200 | [diff] [blame] | 45 | #include "postcopy-ram.h" |
Juan Quintela | 766bd17 | 2012-07-23 05:45:29 +0200 | [diff] [blame] | 46 | #include "qemu/thread.h" |
Kazuya Saito | c09e5bb | 2013-02-22 17:36:19 +0100 | [diff] [blame] | 47 | #include "trace.h" |
Juan Quintela | 5118042 | 2017-04-24 20:50:19 +0200 | [diff] [blame] | 48 | #include "exec/target_page.h" |
Daniel P. Berrange | 61b67d4 | 2016-04-27 11:05:01 +0100 | [diff] [blame] | 49 | #include "io/channel-buffer.h" |
zhanghailiang | 35a6ed4 | 2016-10-27 14:42:52 +0800 | [diff] [blame] | 50 | #include "migration/colo.h" |
Peter Xu | 4ffdb33 | 2017-06-27 12:10:18 +0800 | [diff] [blame] | 51 | #include "hw/boards.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 52 | #include "hw/qdev-properties.h" |
Peter Xu | 9d18af9 | 2017-06-27 12:10:19 +0800 | [diff] [blame] | 53 | #include "monitor/monitor.h" |
Dr. David Alan Gilbert | 50510ea | 2019-02-27 13:24:05 +0000 | [diff] [blame] | 54 | #include "net/announce.h" |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 55 | #include "qemu/queue.h" |
Juan Quintela | d32ca5a | 2020-01-22 16:16:07 +0100 | [diff] [blame] | 56 | #include "multifd.h" |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 57 | |
Jason J. Herne | dc32562 | 2015-09-08 13:12:37 -0400 | [diff] [blame] | 58 | #define MAX_THROTTLE (32 << 20) /* Migration transfer speed throttling */ |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 59 | |
Juan Quintela | 5b4e1eb | 2012-12-19 10:40:48 +0100 | [diff] [blame] | 60 | /* Amount of time to allocate to each "chunk" of bandwidth-throttled |
| 61 | * data. */ |
| 62 | #define BUFFER_DELAY 100 |
| 63 | #define XFER_LIMIT_RATIO (1000 / BUFFER_DELAY) |
| 64 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 65 | /* Time in milliseconds we are allowed to stop the source, |
| 66 | * for sending the last part */ |
| 67 | #define DEFAULT_MIGRATE_SET_DOWNTIME 300 |
| 68 | |
Daniel Henrique Barboza | 87c9cc1 | 2017-02-22 12:17:29 -0300 | [diff] [blame] | 69 | /* Maximum migrate downtime set to 2000 seconds */ |
| 70 | #define MAX_MIGRATE_DOWNTIME_SECONDS 2000 |
| 71 | #define MAX_MIGRATE_DOWNTIME (MAX_MIGRATE_DOWNTIME_SECONDS * 1000) |
| 72 | |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 73 | /* Default compression thread count */ |
| 74 | #define DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT 8 |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 75 | /* Default decompression thread count, usually decompression is at |
| 76 | * least 4 times as fast as compression.*/ |
| 77 | #define DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT 2 |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 78 | /*0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
| 79 | #define DEFAULT_MIGRATE_COMPRESS_LEVEL 1 |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 80 | /* Define default autoconverge cpu throttle migration parameters */ |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 81 | #define DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD 50 |
Jason J. Herne | d85a31d | 2016-04-21 14:07:18 -0400 | [diff] [blame] | 82 | #define DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL 20 |
| 83 | #define DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT 10 |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 84 | #define DEFAULT_MIGRATE_MAX_CPU_THROTTLE 99 |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 85 | |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 86 | /* Migration XBZRLE default cache size */ |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 87 | #define DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE (64 * 1024 * 1024) |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 88 | |
Zhang Chen | 131b215 | 2018-09-03 12:38:45 +0800 | [diff] [blame] | 89 | /* The delay time (in ms) between two COLO checkpoints */ |
| 90 | #define DEFAULT_MIGRATE_X_CHECKPOINT_DELAY (200 * 100) |
Juan Quintela | 4075fb1 | 2016-01-15 08:56:17 +0100 | [diff] [blame] | 91 | #define DEFAULT_MIGRATE_MULTIFD_CHANNELS 2 |
Juan Quintela | 96eef04 | 2019-01-16 10:35:55 +0100 | [diff] [blame] | 92 | #define DEFAULT_MIGRATE_MULTIFD_COMPRESSION MULTIFD_COMPRESSION_NONE |
Juan Quintela | 9004db4 | 2020-01-23 17:08:52 +0100 | [diff] [blame] | 93 | /* 0: means nocompress, 1: best speed, ... 9: best compress ratio */ |
| 94 | #define DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL 1 |
Juan Quintela | 6a9ad15 | 2020-01-23 17:41:36 +0100 | [diff] [blame] | 95 | /* 0: means nocompress, 1: best speed, ... 20: best compress ratio */ |
| 96 | #define DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL 1 |
zhanghailiang | 68b5359 | 2016-10-27 14:43:01 +0800 | [diff] [blame] | 97 | |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 98 | /* Background transfer rate for postcopy, 0 means unlimited, note |
| 99 | * that page requests can still exceed this limit. |
| 100 | */ |
| 101 | #define DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH 0 |
| 102 | |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 103 | /* |
| 104 | * Parameters for self_announce_delay giving a stream of RARP/ARP |
| 105 | * packets after migration. |
| 106 | */ |
| 107 | #define DEFAULT_MIGRATE_ANNOUNCE_INITIAL 50 |
| 108 | #define DEFAULT_MIGRATE_ANNOUNCE_MAX 550 |
| 109 | #define DEFAULT_MIGRATE_ANNOUNCE_ROUNDS 5 |
| 110 | #define DEFAULT_MIGRATE_ANNOUNCE_STEP 100 |
| 111 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 112 | static NotifierList migration_state_notifiers = |
| 113 | NOTIFIER_LIST_INITIALIZER(migration_state_notifiers); |
| 114 | |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 115 | static bool deferred_incoming; |
| 116 | |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 117 | /* Messages sent on the return path from destination to source */ |
| 118 | enum mig_rp_message_type { |
| 119 | MIG_RP_MSG_INVALID = 0, /* Must be 0 */ |
| 120 | MIG_RP_MSG_SHUT, /* sibling will not send any more RP messages */ |
| 121 | MIG_RP_MSG_PONG, /* Response to a PING; data (seq: be32 ) */ |
| 122 | |
| 123 | MIG_RP_MSG_REQ_PAGES_ID, /* data (start: be64, len: be32, id: string) */ |
| 124 | MIG_RP_MSG_REQ_PAGES, /* data (start: be64, len: be32) */ |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 125 | MIG_RP_MSG_RECV_BITMAP, /* send recved_bitmap back to source */ |
Peter Xu | 13955b8 | 2018-05-02 18:47:30 +0800 | [diff] [blame] | 126 | MIG_RP_MSG_RESUME_ACK, /* tell source that we are ready to resume */ |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 127 | |
| 128 | MIG_RP_MSG_MAX |
| 129 | }; |
| 130 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 131 | /* When we add fault tolerance, we could have several |
| 132 | migrations at once. For now we don't need to add |
| 133 | dynamic creation of migration */ |
| 134 | |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 135 | static MigrationState *current_migration; |
Peter Xu | e1b1b1b | 2018-05-02 18:47:35 +0800 | [diff] [blame] | 136 | static MigrationIncomingState *current_incoming; |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 137 | |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 138 | static bool migration_object_check(MigrationState *ms, Error **errp); |
Dr. David Alan Gilbert | 0331c8c | 2017-10-20 10:05:56 +0100 | [diff] [blame] | 139 | static int migration_maybe_pause(MigrationState *s, |
| 140 | int *current_active_state, |
| 141 | int new_state); |
Dr. David Alan Gilbert | 892ae71 | 2019-02-27 16:49:00 +0000 | [diff] [blame] | 142 | static void migrate_fd_cancel(MigrationState *s); |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 143 | |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 144 | void migration_object_init(void) |
| 145 | { |
Peter Xu | 4ffdb33 | 2017-06-27 12:10:18 +0800 | [diff] [blame] | 146 | MachineState *ms = MACHINE(qdev_get_machine()); |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 147 | Error *err = NULL; |
Peter Xu | 4ffdb33 | 2017-06-27 12:10:18 +0800 | [diff] [blame] | 148 | |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 149 | /* This can only be called once. */ |
| 150 | assert(!current_migration); |
| 151 | current_migration = MIGRATION_OBJ(object_new(TYPE_MIGRATION)); |
Peter Xu | 4ffdb33 | 2017-06-27 12:10:18 +0800 | [diff] [blame] | 152 | |
Peter Xu | e1b1b1b | 2018-05-02 18:47:35 +0800 | [diff] [blame] | 153 | /* |
| 154 | * Init the migrate incoming object as well no matter whether |
| 155 | * we'll use it or not. |
| 156 | */ |
| 157 | assert(!current_incoming); |
| 158 | current_incoming = g_new0(MigrationIncomingState, 1); |
| 159 | current_incoming->state = MIGRATION_STATUS_NONE; |
| 160 | current_incoming->postcopy_remote_fds = |
| 161 | g_array_new(FALSE, TRUE, sizeof(struct PostCopyFD)); |
| 162 | qemu_mutex_init(¤t_incoming->rp_mutex); |
| 163 | qemu_event_init(¤t_incoming->main_thread_load_event, false); |
| 164 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_dst, 0); |
| 165 | qemu_sem_init(¤t_incoming->postcopy_pause_sem_fault, 0); |
| 166 | |
| 167 | init_dirty_bitmap_incoming_migration(); |
| 168 | |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 169 | if (!migration_object_check(current_migration, &err)) { |
| 170 | error_report_err(err); |
| 171 | exit(1); |
| 172 | } |
| 173 | |
Peter Xu | 4ffdb33 | 2017-06-27 12:10:18 +0800 | [diff] [blame] | 174 | /* |
| 175 | * We cannot really do this in migration_instance_init() since at |
| 176 | * that time global properties are not yet applied, then this |
| 177 | * value will be definitely replaced by something else. |
| 178 | */ |
| 179 | if (ms->enforce_config_section) { |
| 180 | current_migration->send_configuration = true; |
| 181 | } |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 182 | } |
| 183 | |
Dr. David Alan Gilbert | 892ae71 | 2019-02-27 16:49:00 +0000 | [diff] [blame] | 184 | void migration_shutdown(void) |
Vladimir Sementsov-Ogievskiy | 1f89560 | 2017-12-28 12:16:16 +0300 | [diff] [blame] | 185 | { |
Dr. David Alan Gilbert | 892ae71 | 2019-02-27 16:49:00 +0000 | [diff] [blame] | 186 | /* |
| 187 | * Cancel the current migration - that will (eventually) |
| 188 | * stop the migration using this structure |
| 189 | */ |
| 190 | migrate_fd_cancel(current_migration); |
Vladimir Sementsov-Ogievskiy | 1f89560 | 2017-12-28 12:16:16 +0300 | [diff] [blame] | 191 | object_unref(OBJECT(current_migration)); |
| 192 | } |
| 193 | |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 194 | /* For outgoing */ |
Juan Quintela | 859bc75 | 2012-08-13 09:42:49 +0200 | [diff] [blame] | 195 | MigrationState *migrate_get_current(void) |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 196 | { |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 197 | /* This can only be called after the object created. */ |
| 198 | assert(current_migration); |
| 199 | return current_migration; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 200 | } |
| 201 | |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 202 | MigrationIncomingState *migration_incoming_get_current(void) |
| 203 | { |
Peter Xu | e1b1b1b | 2018-05-02 18:47:35 +0800 | [diff] [blame] | 204 | assert(current_incoming); |
| 205 | return current_incoming; |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void migration_incoming_state_destroy(void) |
| 209 | { |
Juan Quintela | b4b076d | 2017-01-23 22:32:06 +0100 | [diff] [blame] | 210 | struct MigrationIncomingState *mis = migration_incoming_get_current(); |
| 211 | |
Peter Xu | 3482655 | 2017-05-19 14:43:29 +0800 | [diff] [blame] | 212 | if (mis->to_src_file) { |
Peter Xu | 660819b | 2017-05-19 14:43:30 +0800 | [diff] [blame] | 213 | /* Tell source that we are done */ |
| 214 | migrate_send_rp_shut(mis, qemu_file_get_error(mis->from_src_file) != 0); |
Peter Xu | 3482655 | 2017-05-19 14:43:29 +0800 | [diff] [blame] | 215 | qemu_fclose(mis->to_src_file); |
| 216 | mis->to_src_file = NULL; |
| 217 | } |
| 218 | |
Peter Xu | 660819b | 2017-05-19 14:43:30 +0800 | [diff] [blame] | 219 | if (mis->from_src_file) { |
| 220 | qemu_fclose(mis->from_src_file); |
| 221 | mis->from_src_file = NULL; |
| 222 | } |
Dr. David Alan Gilbert | 00fa4fc | 2018-03-12 17:21:04 +0000 | [diff] [blame] | 223 | if (mis->postcopy_remote_fds) { |
| 224 | g_array_free(mis->postcopy_remote_fds, TRUE); |
| 225 | mis->postcopy_remote_fds = NULL; |
| 226 | } |
Peter Xu | 660819b | 2017-05-19 14:43:30 +0800 | [diff] [blame] | 227 | |
Dr. David Alan Gilbert | 5089e18 | 2017-08-25 15:19:39 +0100 | [diff] [blame] | 228 | qemu_event_reset(&mis->main_thread_load_event); |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 229 | |
| 230 | if (mis->socket_address_list) { |
| 231 | qapi_free_SocketAddressList(mis->socket_address_list); |
| 232 | mis->socket_address_list = NULL; |
| 233 | } |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 234 | } |
| 235 | |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 236 | static void migrate_generate_event(int new_state) |
| 237 | { |
| 238 | if (migrate_use_events()) { |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 239 | qapi_event_send_migration(new_state); |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Dr. David Alan Gilbert | 0f073f4 | 2018-04-16 18:09:30 +0100 | [diff] [blame] | 243 | static bool migrate_late_block_activate(void) |
| 244 | { |
| 245 | MigrationState *s; |
| 246 | |
| 247 | s = migrate_get_current(); |
| 248 | |
| 249 | return s->enabled_capabilities[ |
| 250 | MIGRATION_CAPABILITY_LATE_BLOCK_ACTIVATE]; |
| 251 | } |
| 252 | |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 253 | /* |
| 254 | * Called on -incoming with a defer: uri. |
| 255 | * The migration can be started later after any parameters have been |
| 256 | * changed. |
| 257 | */ |
| 258 | static void deferred_incoming_migration(Error **errp) |
| 259 | { |
| 260 | if (deferred_incoming) { |
| 261 | error_setg(errp, "Incoming migration already deferred"); |
| 262 | } |
| 263 | deferred_incoming = true; |
| 264 | } |
| 265 | |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 266 | /* |
| 267 | * Send a message on the return channel back to the source |
| 268 | * of the migration. |
| 269 | */ |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 270 | static int migrate_send_rp_message(MigrationIncomingState *mis, |
| 271 | enum mig_rp_message_type message_type, |
| 272 | uint16_t len, void *data) |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 273 | { |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 274 | int ret = 0; |
| 275 | |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 276 | trace_migrate_send_rp_message((int)message_type, len); |
| 277 | qemu_mutex_lock(&mis->rp_mutex); |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * It's possible that the file handle got lost due to network |
| 281 | * failures. |
| 282 | */ |
| 283 | if (!mis->to_src_file) { |
| 284 | ret = -EIO; |
| 285 | goto error; |
| 286 | } |
| 287 | |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 288 | qemu_put_be16(mis->to_src_file, (unsigned int)message_type); |
| 289 | qemu_put_be16(mis->to_src_file, len); |
| 290 | qemu_put_buffer(mis->to_src_file, data, len); |
| 291 | qemu_fflush(mis->to_src_file); |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 292 | |
| 293 | /* It's possible that qemu file got error during sending */ |
| 294 | ret = qemu_file_get_error(mis->to_src_file); |
| 295 | |
| 296 | error: |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 297 | qemu_mutex_unlock(&mis->rp_mutex); |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 298 | return ret; |
Juan Quintela | da6f179 | 2017-04-24 17:37:14 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 301 | /* Request a range of pages from the source VM at the given |
| 302 | * start address. |
| 303 | * rbname: Name of the RAMBlock to request the page in, if NULL it's the same |
| 304 | * as the last request (a name must have been given previously) |
| 305 | * Start: Address offset within the RB |
| 306 | * Len: Length in bytes required - must be a multiple of pagesize |
| 307 | */ |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 308 | int migrate_send_rp_req_pages(MigrationIncomingState *mis, const char *rbname, |
| 309 | ram_addr_t start, size_t len) |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 310 | { |
Stefan Weil | cb8d4c8 | 2016-03-23 15:59:57 +0100 | [diff] [blame] | 311 | uint8_t bufc[12 + 1 + 255]; /* start (8), len (4), rbname up to 256 */ |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 312 | size_t msglen = 12; /* start + len */ |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 313 | enum mig_rp_message_type msg_type; |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 314 | |
| 315 | *(uint64_t *)bufc = cpu_to_be64((uint64_t)start); |
| 316 | *(uint32_t *)(bufc + 8) = cpu_to_be32((uint32_t)len); |
| 317 | |
| 318 | if (rbname) { |
| 319 | int rbname_len = strlen(rbname); |
| 320 | assert(rbname_len < 256); |
| 321 | |
| 322 | bufc[msglen++] = rbname_len; |
| 323 | memcpy(bufc + msglen, rbname, rbname_len); |
| 324 | msglen += rbname_len; |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 325 | msg_type = MIG_RP_MSG_REQ_PAGES_ID; |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 326 | } else { |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 327 | msg_type = MIG_RP_MSG_REQ_PAGES; |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 328 | } |
Peter Xu | d6208e3 | 2018-02-08 18:31:12 +0800 | [diff] [blame] | 329 | |
| 330 | return migrate_send_rp_message(mis, msg_type, msglen, bufc); |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Zhang Chen | aad555c | 2018-09-03 12:38:47 +0800 | [diff] [blame] | 333 | static bool migration_colo_enabled; |
| 334 | bool migration_incoming_colo_enabled(void) |
| 335 | { |
| 336 | return migration_colo_enabled; |
| 337 | } |
| 338 | |
| 339 | void migration_incoming_disable_colo(void) |
| 340 | { |
| 341 | migration_colo_enabled = false; |
| 342 | } |
| 343 | |
| 344 | void migration_incoming_enable_colo(void) |
| 345 | { |
| 346 | migration_colo_enabled = true; |
| 347 | } |
| 348 | |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 349 | void migrate_add_address(SocketAddress *address) |
| 350 | { |
| 351 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 352 | SocketAddressList *addrs; |
| 353 | |
| 354 | addrs = g_new0(SocketAddressList, 1); |
| 355 | addrs->next = mis->socket_address_list; |
| 356 | mis->socket_address_list = addrs; |
| 357 | addrs->value = QAPI_CLONE(SocketAddress, address); |
| 358 | } |
| 359 | |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 360 | void qemu_start_incoming_migration(const char *uri, Error **errp) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 361 | { |
aliguori | 34c9dd8 | 2008-10-13 03:14:31 +0000 | [diff] [blame] | 362 | const char *p; |
| 363 | |
Peter Xu | 3ab7238 | 2018-08-15 21:37:37 +0800 | [diff] [blame] | 364 | qapi_event_send_migration(MIGRATION_STATUS_SETUP); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 365 | if (!strcmp(uri, "defer")) { |
| 366 | deferred_incoming_migration(errp); |
| 367 | } else if (strstart(uri, "tcp:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 368 | tcp_start_incoming_migration(p, errp); |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 369 | #ifdef CONFIG_RDMA |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 370 | } else if (strstart(uri, "rdma:", &p)) { |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 371 | rdma_start_incoming_migration(p, errp); |
| 372 | #endif |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 373 | } else if (strstart(uri, "exec:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 374 | exec_start_incoming_migration(p, errp); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 375 | } else if (strstart(uri, "unix:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 376 | unix_start_incoming_migration(p, errp); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 377 | } else if (strstart(uri, "fd:", &p)) { |
Paolo Bonzini | 43eaae2 | 2012-10-02 18:21:18 +0200 | [diff] [blame] | 378 | fd_start_incoming_migration(p, errp); |
Dr. David Alan Gilbert | adde220 | 2015-02-19 11:40:27 +0000 | [diff] [blame] | 379 | } else { |
Markus Armbruster | 312fd5f | 2013-02-08 21:22:16 +0100 | [diff] [blame] | 380 | error_setg(errp, "unknown migration protocol: %s", uri); |
Juan Quintela | 8ca5e80 | 2010-06-09 14:10:54 +0200 | [diff] [blame] | 381 | } |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 384 | static void process_incoming_migration_bh(void *opaque) |
| 385 | { |
| 386 | Error *local_err = NULL; |
| 387 | MigrationIncomingState *mis = opaque; |
| 388 | |
Dr. David Alan Gilbert | 0f073f4 | 2018-04-16 18:09:30 +0100 | [diff] [blame] | 389 | /* If capability late_block_activate is set: |
| 390 | * Only fire up the block code now if we're going to restart the |
| 391 | * VM, else 'cont' will do it. |
| 392 | * This causes file locking to happen; so we don't want it to happen |
| 393 | * unless we really are starting the VM. |
| 394 | */ |
| 395 | if (!migrate_late_block_activate() || |
| 396 | (autostart && (!global_state_received() || |
| 397 | global_state_get_runstate() == RUN_STATE_RUNNING))) { |
| 398 | /* Make sure all file formats flush their mutable metadata. |
| 399 | * If we get an error here, just don't restart the VM yet. */ |
| 400 | bdrv_invalidate_cache_all(&local_err); |
| 401 | if (local_err) { |
| 402 | error_report_err(local_err); |
| 403 | local_err = NULL; |
| 404 | autostart = false; |
| 405 | } |
Kevin Wolf | d35ff5e | 2017-04-04 17:29:03 +0200 | [diff] [blame] | 406 | } |
| 407 | |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 408 | /* |
| 409 | * This must happen after all error conditions are dealt with and |
| 410 | * we're sure the VM is going to be running on this host. |
| 411 | */ |
Dr. David Alan Gilbert | 7659505 | 2019-02-27 13:24:08 +0000 | [diff] [blame] | 412 | qemu_announce_self(&mis->announce_timer, migrate_announce_params()); |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 413 | |
Juan Quintela | f986c3d | 2016-01-14 16:52:55 +0100 | [diff] [blame] | 414 | if (multifd_load_cleanup(&local_err) != 0) { |
| 415 | error_report_err(local_err); |
| 416 | autostart = false; |
| 417 | } |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 418 | /* If global state section was not received or we are in running |
| 419 | state, we need to obey autostart. Any other state is set with |
| 420 | runstate_set. */ |
| 421 | |
Vladimir Sementsov-Ogievskiy | b35ebdf | 2018-03-13 15:34:01 -0400 | [diff] [blame] | 422 | dirty_bitmap_mig_before_vm_start(); |
| 423 | |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 424 | if (!global_state_received() || |
| 425 | global_state_get_runstate() == RUN_STATE_RUNNING) { |
| 426 | if (autostart) { |
| 427 | vm_start(); |
| 428 | } else { |
| 429 | runstate_set(RUN_STATE_PAUSED); |
| 430 | } |
Zhang Chen | db00972 | 2019-03-03 22:50:17 +0800 | [diff] [blame] | 431 | } else if (migration_incoming_colo_enabled()) { |
| 432 | migration_incoming_disable_colo(); |
| 433 | vm_start(); |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 434 | } else { |
| 435 | runstate_set(global_state_get_runstate()); |
| 436 | } |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 437 | /* |
| 438 | * This must happen after any state changes since as soon as an external |
| 439 | * observer sees this event they might start to prod at the VM assuming |
| 440 | * it's ready to use. |
| 441 | */ |
| 442 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
| 443 | MIGRATION_STATUS_COMPLETED); |
| 444 | qemu_bh_delete(mis->bh); |
| 445 | migration_incoming_state_destroy(); |
| 446 | } |
| 447 | |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 448 | static void process_incoming_migration_co(void *opaque) |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 449 | { |
Juan Quintela | b4b076d | 2017-01-23 22:32:06 +0100 | [diff] [blame] | 450 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Dr. David Alan Gilbert | e9bef23 | 2015-11-05 18:11:21 +0000 | [diff] [blame] | 451 | PostcopyState ps; |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 452 | int ret; |
Zhang Chen | 8e48ac9 | 2018-09-03 12:38:46 +0800 | [diff] [blame] | 453 | Error *local_err = NULL; |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 454 | |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 455 | assert(mis->from_src_file); |
Lidong Chen | 9237098 | 2018-08-06 21:29:35 +0800 | [diff] [blame] | 456 | mis->migration_incoming_co = qemu_coroutine_self(); |
Dr. David Alan Gilbert | 67f11b5 | 2017-02-24 18:28:34 +0000 | [diff] [blame] | 457 | mis->largest_page_size = qemu_ram_pagesize_largest(); |
Dr. David Alan Gilbert | 093e3c4 | 2015-11-05 18:10:52 +0000 | [diff] [blame] | 458 | postcopy_state_set(POSTCOPY_INCOMING_NONE); |
zhanghailiang | 93d7af6 | 2015-12-16 11:47:34 +0000 | [diff] [blame] | 459 | migrate_set_state(&mis->state, MIGRATION_STATUS_NONE, |
| 460 | MIGRATION_STATUS_ACTIVE); |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 461 | ret = qemu_loadvm_state(mis->from_src_file); |
Dr. David Alan Gilbert | bca7856 | 2015-05-21 13:24:14 +0100 | [diff] [blame] | 462 | |
Dr. David Alan Gilbert | e9bef23 | 2015-11-05 18:11:21 +0000 | [diff] [blame] | 463 | ps = postcopy_state_get(); |
| 464 | trace_process_incoming_migration_co_end(ret, ps); |
| 465 | if (ps != POSTCOPY_INCOMING_NONE) { |
| 466 | if (ps == POSTCOPY_INCOMING_ADVISE) { |
| 467 | /* |
| 468 | * Where a migration had postcopy enabled (and thus went to advise) |
| 469 | * but managed to complete within the precopy period, we can use |
| 470 | * the normal exit. |
| 471 | */ |
| 472 | postcopy_ram_incoming_cleanup(mis); |
| 473 | } else if (ret >= 0) { |
| 474 | /* |
| 475 | * Postcopy was started, cleanup should happen at the end of the |
| 476 | * postcopy thread. |
| 477 | */ |
| 478 | trace_process_incoming_migration_co_postcopy_end_main(); |
| 479 | return; |
| 480 | } |
| 481 | /* Else if something went wrong then just fall out of the normal exit */ |
| 482 | } |
| 483 | |
zhanghailiang | 25d0c16 | 2016-10-27 14:42:55 +0800 | [diff] [blame] | 484 | /* we get COLO info, and know if we are in COLO mode */ |
Zhang Chen | aad555c | 2018-09-03 12:38:47 +0800 | [diff] [blame] | 485 | if (!ret && migration_incoming_colo_enabled()) { |
Zhang Chen | 8e48ac9 | 2018-09-03 12:38:46 +0800 | [diff] [blame] | 486 | /* Make sure all file formats flush their mutable metadata */ |
| 487 | bdrv_invalidate_cache_all(&local_err); |
| 488 | if (local_err) { |
Zhang Chen | 8e48ac9 | 2018-09-03 12:38:46 +0800 | [diff] [blame] | 489 | error_report_err(local_err); |
Fei Li | 6d99c2d | 2019-01-13 22:08:49 +0800 | [diff] [blame] | 490 | goto fail; |
Zhang Chen | 8e48ac9 | 2018-09-03 12:38:46 +0800 | [diff] [blame] | 491 | } |
| 492 | |
zhanghailiang | 25d0c16 | 2016-10-27 14:42:55 +0800 | [diff] [blame] | 493 | qemu_thread_create(&mis->colo_incoming_thread, "COLO incoming", |
| 494 | colo_process_incoming_thread, mis, QEMU_THREAD_JOINABLE); |
| 495 | mis->have_colo_incoming_thread = true; |
| 496 | qemu_coroutine_yield(); |
| 497 | |
| 498 | /* Wait checkpoint incoming thread exit before free resource */ |
| 499 | qemu_thread_join(&mis->colo_incoming_thread); |
Zhang Chen | 13af18f | 2018-09-03 12:38:48 +0800 | [diff] [blame] | 500 | /* We hold the global iothread lock, so it is safe here */ |
| 501 | colo_release_ram_cache(); |
zhanghailiang | 25d0c16 | 2016-10-27 14:42:55 +0800 | [diff] [blame] | 502 | } |
| 503 | |
Paolo Bonzini | 1c12e1f | 2012-08-07 10:51:51 +0200 | [diff] [blame] | 504 | if (ret < 0) { |
Peter Lieven | db80fac | 2014-06-10 11:29:16 +0200 | [diff] [blame] | 505 | error_report("load of migration failed: %s", strerror(-ret)); |
Fei Li | 6d99c2d | 2019-01-13 22:08:49 +0800 | [diff] [blame] | 506 | goto fail; |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 507 | } |
Denis V. Lunev | 0aa6aef | 2016-02-24 11:53:38 +0300 | [diff] [blame] | 508 | mis->bh = qemu_bh_new(process_incoming_migration_bh, mis); |
| 509 | qemu_bh_schedule(mis->bh); |
Lidong Chen | 9237098 | 2018-08-06 21:29:35 +0800 | [diff] [blame] | 510 | mis->migration_incoming_co = NULL; |
Fei Li | 6d99c2d | 2019-01-13 22:08:49 +0800 | [diff] [blame] | 511 | return; |
| 512 | fail: |
| 513 | local_err = NULL; |
| 514 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
| 515 | MIGRATION_STATUS_FAILED); |
| 516 | qemu_fclose(mis->from_src_file); |
| 517 | if (multifd_load_cleanup(&local_err) != 0) { |
| 518 | error_report_err(local_err); |
| 519 | } |
| 520 | exit(EXIT_FAILURE); |
Juan Quintela | 511c023 | 2010-06-09 14:10:55 +0200 | [diff] [blame] | 521 | } |
| 522 | |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 523 | /** |
| 524 | * @migration_incoming_setup: Setup incoming migration |
| 525 | * |
| 526 | * Returns 0 for no error or 1 for error |
| 527 | * |
| 528 | * @f: file for main migration channel |
| 529 | * @errp: where to put errors |
| 530 | */ |
| 531 | static int migration_incoming_setup(QEMUFile *f, Error **errp) |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 532 | { |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 533 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 534 | Error *local_err = NULL; |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 535 | |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 536 | if (multifd_load_setup(&local_err) != 0) { |
Juan Quintela | f986c3d | 2016-01-14 16:52:55 +0100 | [diff] [blame] | 537 | /* We haven't been able to create multifd threads |
| 538 | nothing better to do */ |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 539 | error_report_err(local_err); |
Juan Quintela | f986c3d | 2016-01-14 16:52:55 +0100 | [diff] [blame] | 540 | exit(EXIT_FAILURE); |
| 541 | } |
| 542 | |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 543 | if (!mis->from_src_file) { |
| 544 | mis->from_src_file = f; |
| 545 | } |
Daniel P. Berrange | 06ad513 | 2016-04-27 11:04:56 +0100 | [diff] [blame] | 546 | qemu_file_set_blocking(f, false); |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 547 | return 0; |
Juan Quintela | e595a01 | 2017-07-17 12:30:25 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Juan Quintela | 36c2f8b | 2018-03-07 08:40:52 +0100 | [diff] [blame] | 550 | void migration_incoming_process(void) |
Juan Quintela | e595a01 | 2017-07-17 12:30:25 +0200 | [diff] [blame] | 551 | { |
| 552 | Coroutine *co = qemu_coroutine_create(process_incoming_migration_co, NULL); |
Paolo Bonzini | 0b8b875 | 2016-07-04 19:10:01 +0200 | [diff] [blame] | 553 | qemu_coroutine_enter(co); |
Paolo Bonzini | 82a4da7 | 2012-08-07 10:57:43 +0200 | [diff] [blame] | 554 | } |
| 555 | |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 556 | /* Returns true if recovered from a paused migration, otherwise false */ |
| 557 | static bool postcopy_try_recover(QEMUFile *f) |
Juan Quintela | e595a01 | 2017-07-17 12:30:25 +0200 | [diff] [blame] | 558 | { |
Peter Xu | d96c9e8 | 2018-05-02 18:47:26 +0800 | [diff] [blame] | 559 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 560 | |
| 561 | if (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { |
| 562 | /* Resumed from a paused postcopy migration */ |
| 563 | |
| 564 | mis->from_src_file = f; |
| 565 | /* Postcopy has standalone thread to do vm load */ |
| 566 | qemu_file_set_blocking(f, true); |
| 567 | |
| 568 | /* Re-configure the return path */ |
| 569 | mis->to_src_file = qemu_file_get_return_path(f); |
| 570 | |
| 571 | migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_PAUSED, |
| 572 | MIGRATION_STATUS_POSTCOPY_RECOVER); |
| 573 | |
| 574 | /* |
| 575 | * Here, we only wake up the main loading thread (while the |
| 576 | * fault thread will still be waiting), so that we can receive |
| 577 | * commands from source now, and answer it if needed. The |
| 578 | * fault thread will be woken up afterwards until we are sure |
| 579 | * that source is ready to reply to page requests. |
| 580 | */ |
| 581 | qemu_sem_post(&mis->postcopy_pause_sem_dst); |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 582 | return true; |
Peter Xu | d96c9e8 | 2018-05-02 18:47:26 +0800 | [diff] [blame] | 583 | } |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 584 | |
| 585 | return false; |
| 586 | } |
| 587 | |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 588 | void migration_fd_process_incoming(QEMUFile *f, Error **errp) |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 589 | { |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 590 | Error *local_err = NULL; |
| 591 | |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 592 | if (postcopy_try_recover(f)) { |
| 593 | return; |
| 594 | } |
| 595 | |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 596 | if (migration_incoming_setup(f, &local_err)) { |
| 597 | if (local_err) { |
| 598 | error_propagate(errp, local_err); |
| 599 | } |
| 600 | return; |
| 601 | } |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 602 | migration_incoming_process(); |
Juan Quintela | e595a01 | 2017-07-17 12:30:25 +0200 | [diff] [blame] | 603 | } |
| 604 | |
Fei Li | 49ed0d2 | 2019-01-13 22:08:46 +0800 | [diff] [blame] | 605 | void migration_ioc_process_incoming(QIOChannel *ioc, Error **errp) |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 606 | { |
| 607 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 608 | Error *local_err = NULL; |
Peter Xu | a429e7f | 2018-06-27 21:22:46 +0800 | [diff] [blame] | 609 | bool start_migration; |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 610 | |
| 611 | if (!mis->from_src_file) { |
Peter Xu | a429e7f | 2018-06-27 21:22:46 +0800 | [diff] [blame] | 612 | /* The first connection (multifd may have multiple) */ |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 613 | QEMUFile *f = qemu_fopen_channel_input(ioc); |
Peter Xu | a429e7f | 2018-06-27 21:22:46 +0800 | [diff] [blame] | 614 | |
| 615 | /* If it's a recovery, we're done */ |
Peter Xu | 884835f | 2018-06-27 21:22:45 +0800 | [diff] [blame] | 616 | if (postcopy_try_recover(f)) { |
| 617 | return; |
| 618 | } |
Peter Xu | a429e7f | 2018-06-27 21:22:46 +0800 | [diff] [blame] | 619 | |
Juan Quintela | b673eab | 2019-06-12 11:44:19 +0200 | [diff] [blame] | 620 | if (migration_incoming_setup(f, &local_err)) { |
| 621 | if (local_err) { |
| 622 | error_propagate(errp, local_err); |
| 623 | } |
| 624 | return; |
| 625 | } |
Peter Xu | a429e7f | 2018-06-27 21:22:46 +0800 | [diff] [blame] | 626 | |
| 627 | /* |
| 628 | * Common migration only needs one channel, so we can start |
| 629 | * right now. Multifd needs more than one channel, we wait. |
| 630 | */ |
| 631 | start_migration = !migrate_use_multifd(); |
| 632 | } else { |
| 633 | /* Multiple connections */ |
| 634 | assert(migrate_use_multifd()); |
Fei Li | 49ed0d2 | 2019-01-13 22:08:46 +0800 | [diff] [blame] | 635 | start_migration = multifd_recv_new_channel(ioc, &local_err); |
| 636 | if (local_err) { |
| 637 | error_propagate(errp, local_err); |
| 638 | return; |
| 639 | } |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 640 | } |
Peter Xu | 81e6205 | 2018-06-27 21:22:44 +0800 | [diff] [blame] | 641 | |
Peter Xu | a429e7f | 2018-06-27 21:22:46 +0800 | [diff] [blame] | 642 | if (start_migration) { |
Peter Xu | 81e6205 | 2018-06-27 21:22:44 +0800 | [diff] [blame] | 643 | migration_incoming_process(); |
| 644 | } |
Juan Quintela | 4f0fae7 | 2017-07-24 12:42:02 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Juan Quintela | 428d890 | 2017-07-24 13:06:25 +0200 | [diff] [blame] | 647 | /** |
| 648 | * @migration_has_all_channels: We have received all channels that we need |
| 649 | * |
| 650 | * Returns true when we have got connections to all the channels that |
| 651 | * we need for migration. |
| 652 | */ |
| 653 | bool migration_has_all_channels(void) |
| 654 | { |
Daniel P. Berrangé | ca273df | 2018-06-19 17:35:52 +0100 | [diff] [blame] | 655 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Juan Quintela | 62c1e0c | 2018-02-19 18:59:02 +0100 | [diff] [blame] | 656 | bool all_channels; |
| 657 | |
| 658 | all_channels = multifd_recv_all_channels_created(); |
| 659 | |
Daniel P. Berrangé | ca273df | 2018-06-19 17:35:52 +0100 | [diff] [blame] | 660 | return all_channels && mis->from_src_file != NULL; |
Juan Quintela | 428d890 | 2017-07-24 13:06:25 +0200 | [diff] [blame] | 661 | } |
| 662 | |
Dr. David Alan Gilbert | 6decec9 | 2015-11-05 18:10:47 +0000 | [diff] [blame] | 663 | /* |
Dr. David Alan Gilbert | 6decec9 | 2015-11-05 18:10:47 +0000 | [diff] [blame] | 664 | * Send a 'SHUT' message on the return channel with the given value |
| 665 | * to indicate that we've finished with the RP. Non-0 value indicates |
| 666 | * error. |
| 667 | */ |
| 668 | void migrate_send_rp_shut(MigrationIncomingState *mis, |
| 669 | uint32_t value) |
| 670 | { |
| 671 | uint32_t buf; |
| 672 | |
| 673 | buf = cpu_to_be32(value); |
| 674 | migrate_send_rp_message(mis, MIG_RP_MSG_SHUT, sizeof(buf), &buf); |
| 675 | } |
| 676 | |
| 677 | /* |
| 678 | * Send a 'PONG' message on the return channel with the given value |
| 679 | * (normally in response to a 'PING') |
| 680 | */ |
| 681 | void migrate_send_rp_pong(MigrationIncomingState *mis, |
| 682 | uint32_t value) |
| 683 | { |
| 684 | uint32_t buf; |
| 685 | |
| 686 | buf = cpu_to_be32(value); |
| 687 | migrate_send_rp_message(mis, MIG_RP_MSG_PONG, sizeof(buf), &buf); |
| 688 | } |
| 689 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 690 | void migrate_send_rp_recv_bitmap(MigrationIncomingState *mis, |
| 691 | char *block_name) |
| 692 | { |
| 693 | char buf[512]; |
| 694 | int len; |
| 695 | int64_t res; |
| 696 | |
| 697 | /* |
| 698 | * First, we send the header part. It contains only the len of |
| 699 | * idstr, and the idstr itself. |
| 700 | */ |
| 701 | len = strlen(block_name); |
| 702 | buf[0] = len; |
| 703 | memcpy(buf + 1, block_name, len); |
| 704 | |
| 705 | if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) { |
| 706 | error_report("%s: MSG_RP_RECV_BITMAP only used for recovery", |
| 707 | __func__); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | migrate_send_rp_message(mis, MIG_RP_MSG_RECV_BITMAP, len + 1, buf); |
| 712 | |
| 713 | /* |
| 714 | * Next, we dump the received bitmap to the stream. |
| 715 | * |
| 716 | * TODO: currently we are safe since we are the only one that is |
| 717 | * using the to_src_file handle (fault thread is still paused), |
| 718 | * and it's ok even not taking the mutex. However the best way is |
| 719 | * to take the lock before sending the message header, and release |
| 720 | * the lock after sending the bitmap. |
| 721 | */ |
| 722 | qemu_mutex_lock(&mis->rp_mutex); |
| 723 | res = ramblock_recv_bitmap_send(mis->to_src_file, block_name); |
| 724 | qemu_mutex_unlock(&mis->rp_mutex); |
| 725 | |
| 726 | trace_migrate_send_rp_recv_bitmap(block_name, res); |
| 727 | } |
| 728 | |
Peter Xu | 13955b8 | 2018-05-02 18:47:30 +0800 | [diff] [blame] | 729 | void migrate_send_rp_resume_ack(MigrationIncomingState *mis, uint32_t value) |
| 730 | { |
| 731 | uint32_t buf; |
| 732 | |
| 733 | buf = cpu_to_be32(value); |
| 734 | migrate_send_rp_message(mis, MIG_RP_MSG_RESUME_ACK, sizeof(buf), &buf); |
| 735 | } |
| 736 | |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 737 | MigrationCapabilityStatusList *qmp_query_migrate_capabilities(Error **errp) |
| 738 | { |
| 739 | MigrationCapabilityStatusList *head = NULL; |
| 740 | MigrationCapabilityStatusList *caps; |
| 741 | MigrationState *s = migrate_get_current(); |
| 742 | int i; |
| 743 | |
Michael Tokarev | 387eede | 2013-10-05 13:18:28 +0400 | [diff] [blame] | 744 | caps = NULL; /* silence compiler warning */ |
Eric Blake | 7fb1cf1 | 2015-11-18 01:52:57 -0700 | [diff] [blame] | 745 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
Dr. David Alan Gilbert | ed1701c | 2017-05-15 15:05:29 +0100 | [diff] [blame] | 746 | #ifndef CONFIG_LIVE_BLOCK_MIGRATION |
| 747 | if (i == MIGRATION_CAPABILITY_BLOCK) { |
| 748 | continue; |
| 749 | } |
| 750 | #endif |
Orit Wasserman | bbf6da3 | 2012-08-06 21:42:47 +0300 | [diff] [blame] | 751 | if (head == NULL) { |
| 752 | head = g_malloc0(sizeof(*caps)); |
| 753 | caps = head; |
| 754 | } else { |
| 755 | caps->next = g_malloc0(sizeof(*caps)); |
| 756 | caps = caps->next; |
| 757 | } |
| 758 | caps->value = |
| 759 | g_malloc(sizeof(*caps->value)); |
| 760 | caps->value->capability = i; |
| 761 | caps->value->state = s->enabled_capabilities[i]; |
| 762 | } |
| 763 | |
| 764 | return head; |
| 765 | } |
| 766 | |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 767 | MigrationParameters *qmp_query_migrate_parameters(Error **errp) |
| 768 | { |
| 769 | MigrationParameters *params; |
| 770 | MigrationState *s = migrate_get_current(); |
| 771 | |
Markus Armbruster | e87fae4 | 2017-07-18 12:57:38 +0200 | [diff] [blame] | 772 | /* TODO use QAPI_CLONE() instead of duplicating it inline */ |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 773 | params = g_malloc0(sizeof(*params)); |
Eric Blake | de63ab6 | 2016-09-08 22:14:15 -0500 | [diff] [blame] | 774 | params->has_compress_level = true; |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 775 | params->compress_level = s->parameters.compress_level; |
Eric Blake | de63ab6 | 2016-09-08 22:14:15 -0500 | [diff] [blame] | 776 | params->has_compress_threads = true; |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 777 | params->compress_threads = s->parameters.compress_threads; |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 778 | params->has_compress_wait_thread = true; |
| 779 | params->compress_wait_thread = s->parameters.compress_wait_thread; |
Eric Blake | de63ab6 | 2016-09-08 22:14:15 -0500 | [diff] [blame] | 780 | params->has_decompress_threads = true; |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 781 | params->decompress_threads = s->parameters.decompress_threads; |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 782 | params->has_throttle_trigger_threshold = true; |
| 783 | params->throttle_trigger_threshold = s->parameters.throttle_trigger_threshold; |
Eric Blake | de63ab6 | 2016-09-08 22:14:15 -0500 | [diff] [blame] | 784 | params->has_cpu_throttle_initial = true; |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 785 | params->cpu_throttle_initial = s->parameters.cpu_throttle_initial; |
Eric Blake | de63ab6 | 2016-09-08 22:14:15 -0500 | [diff] [blame] | 786 | params->has_cpu_throttle_increment = true; |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 787 | params->cpu_throttle_increment = s->parameters.cpu_throttle_increment; |
Markus Armbruster | 8cc99dc | 2017-07-18 12:04:54 +0200 | [diff] [blame] | 788 | params->has_tls_creds = true; |
Daniel P. Berrange | 69ef1f3 | 2016-04-27 11:05:15 +0100 | [diff] [blame] | 789 | params->tls_creds = g_strdup(s->parameters.tls_creds); |
Markus Armbruster | 8cc99dc | 2017-07-18 12:04:54 +0200 | [diff] [blame] | 790 | params->has_tls_hostname = true; |
Daniel P. Berrange | 69ef1f3 | 2016-04-27 11:05:15 +0100 | [diff] [blame] | 791 | params->tls_hostname = g_strdup(s->parameters.tls_hostname); |
Daniel P. Berrange | d2f1d29 | 2019-02-27 14:53:24 +0000 | [diff] [blame] | 792 | params->has_tls_authz = true; |
Mao Zhongyi | 7cd75cb | 2020-03-25 09:49:30 +0800 | [diff] [blame] | 793 | params->tls_authz = g_strdup(s->parameters.tls_authz ? |
| 794 | s->parameters.tls_authz : ""); |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 795 | params->has_max_bandwidth = true; |
| 796 | params->max_bandwidth = s->parameters.max_bandwidth; |
| 797 | params->has_downtime_limit = true; |
| 798 | params->downtime_limit = s->parameters.downtime_limit; |
zhanghailiang | fe39a4d | 2016-11-02 15:42:09 +0800 | [diff] [blame] | 799 | params->has_x_checkpoint_delay = true; |
zhanghailiang | 68b5359 | 2016-10-27 14:43:01 +0800 | [diff] [blame] | 800 | params->x_checkpoint_delay = s->parameters.x_checkpoint_delay; |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 801 | params->has_block_incremental = true; |
| 802 | params->block_incremental = s->parameters.block_incremental; |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 803 | params->has_multifd_channels = true; |
| 804 | params->multifd_channels = s->parameters.multifd_channels; |
Juan Quintela | 96eef04 | 2019-01-16 10:35:55 +0100 | [diff] [blame] | 805 | params->has_multifd_compression = true; |
| 806 | params->multifd_compression = s->parameters.multifd_compression; |
Juan Quintela | 9004db4 | 2020-01-23 17:08:52 +0100 | [diff] [blame] | 807 | params->has_multifd_zlib_level = true; |
| 808 | params->multifd_zlib_level = s->parameters.multifd_zlib_level; |
Juan Quintela | 6a9ad15 | 2020-01-23 17:41:36 +0100 | [diff] [blame] | 809 | params->has_multifd_zstd_level = true; |
| 810 | params->multifd_zstd_level = s->parameters.multifd_zstd_level; |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 811 | params->has_xbzrle_cache_size = true; |
| 812 | params->xbzrle_cache_size = s->parameters.xbzrle_cache_size; |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 813 | params->has_max_postcopy_bandwidth = true; |
| 814 | params->max_postcopy_bandwidth = s->parameters.max_postcopy_bandwidth; |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 815 | params->has_max_cpu_throttle = true; |
| 816 | params->max_cpu_throttle = s->parameters.max_cpu_throttle; |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 817 | params->has_announce_initial = true; |
| 818 | params->announce_initial = s->parameters.announce_initial; |
| 819 | params->has_announce_max = true; |
| 820 | params->announce_max = s->parameters.announce_max; |
| 821 | params->has_announce_rounds = true; |
| 822 | params->announce_rounds = s->parameters.announce_rounds; |
| 823 | params->has_announce_step = true; |
| 824 | params->announce_step = s->parameters.announce_step; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 825 | |
| 826 | return params; |
| 827 | } |
| 828 | |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 829 | AnnounceParameters *migrate_announce_params(void) |
| 830 | { |
| 831 | static AnnounceParameters ap; |
| 832 | |
| 833 | MigrationState *s = migrate_get_current(); |
| 834 | |
| 835 | ap.initial = s->parameters.announce_initial; |
| 836 | ap.max = s->parameters.announce_max; |
| 837 | ap.rounds = s->parameters.announce_rounds; |
| 838 | ap.step = s->parameters.announce_step; |
| 839 | |
| 840 | return ≈ |
| 841 | } |
| 842 | |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 843 | /* |
| 844 | * Return true if we're already in the middle of a migration |
| 845 | * (i.e. any of the active or setup states) |
| 846 | */ |
Jia Lina | 3d63da1 | 2018-10-26 16:36:20 +0800 | [diff] [blame] | 847 | bool migration_is_setup_or_active(int state) |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 848 | { |
| 849 | switch (state) { |
| 850 | case MIGRATION_STATUS_ACTIVE: |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 851 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
Peter Xu | a688d2c | 2018-05-02 18:47:18 +0800 | [diff] [blame] | 852 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 853 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 854 | case MIGRATION_STATUS_SETUP: |
Dr. David Alan Gilbert | 31e0607 | 2017-10-20 10:05:51 +0100 | [diff] [blame] | 855 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
| 856 | case MIGRATION_STATUS_DEVICE: |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 857 | case MIGRATION_STATUS_WAIT_UNPLUG: |
zhanghailiang | 19dd408 | 2020-02-24 14:54:12 +0800 | [diff] [blame] | 858 | case MIGRATION_STATUS_COLO: |
Dr. David Alan Gilbert | f6844b9 | 2015-11-05 18:10:48 +0000 | [diff] [blame] | 859 | return true; |
| 860 | |
| 861 | default: |
| 862 | return false; |
| 863 | |
| 864 | } |
| 865 | } |
| 866 | |
Juan Quintela | 392d87e | 2020-01-21 15:39:23 +0100 | [diff] [blame] | 867 | bool migration_is_running(int state) |
| 868 | { |
| 869 | switch (state) { |
| 870 | case MIGRATION_STATUS_ACTIVE: |
| 871 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
| 872 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
| 873 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
| 874 | case MIGRATION_STATUS_SETUP: |
| 875 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
| 876 | case MIGRATION_STATUS_DEVICE: |
| 877 | case MIGRATION_STATUS_WAIT_UNPLUG: |
| 878 | case MIGRATION_STATUS_CANCELLING: |
Juan Quintela | 392d87e | 2020-01-21 15:39:23 +0100 | [diff] [blame] | 879 | return true; |
| 880 | |
| 881 | default: |
| 882 | return false; |
| 883 | |
| 884 | } |
| 885 | } |
| 886 | |
Wei Yang | 640dfb1 | 2019-07-16 08:54:11 +0800 | [diff] [blame] | 887 | static void populate_time_info(MigrationInfo *info, MigrationState *s) |
| 888 | { |
| 889 | info->has_status = true; |
| 890 | info->has_setup_time = true; |
| 891 | info->setup_time = s->setup_time; |
| 892 | if (s->state == MIGRATION_STATUS_COMPLETED) { |
| 893 | info->has_total_time = true; |
| 894 | info->total_time = s->total_time; |
| 895 | info->has_downtime = true; |
| 896 | info->downtime = s->downtime; |
| 897 | } else { |
| 898 | info->has_total_time = true; |
| 899 | info->total_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - |
| 900 | s->start_time; |
| 901 | info->has_expected_downtime = true; |
| 902 | info->expected_downtime = s->expected_downtime; |
| 903 | } |
| 904 | } |
| 905 | |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 906 | static void populate_ram_info(MigrationInfo *info, MigrationState *s) |
| 907 | { |
| 908 | info->has_ram = true; |
| 909 | info->ram = g_malloc0(sizeof(*info->ram)); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 910 | info->ram->transferred = ram_counters.transferred; |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 911 | info->ram->total = ram_bytes_total(); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 912 | info->ram->duplicate = ram_counters.duplicate; |
Juan Quintela | bedf53c | 2017-03-13 20:35:54 +0100 | [diff] [blame] | 913 | /* legacy value. It is not used anymore */ |
| 914 | info->ram->skipped = 0; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 915 | info->ram->normal = ram_counters.normal; |
| 916 | info->ram->normal_bytes = ram_counters.normal * |
Juan Quintela | 20afaed | 2017-03-21 09:09:14 +0100 | [diff] [blame] | 917 | qemu_target_page_size(); |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 918 | info->ram->mbps = s->mbps; |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 919 | info->ram->dirty_sync_count = ram_counters.dirty_sync_count; |
| 920 | info->ram->postcopy_requests = ram_counters.postcopy_requests; |
Chao Fan | 030ce1f | 2017-03-21 10:22:43 +0800 | [diff] [blame] | 921 | info->ram->page_size = qemu_target_page_size(); |
Juan Quintela | a61c45b | 2018-06-26 15:20:11 +0200 | [diff] [blame] | 922 | info->ram->multifd_bytes = ram_counters.multifd_bytes; |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 923 | info->ram->pages_per_second = s->pages_per_second; |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 924 | |
Juan Quintela | 114f5ae | 2017-05-04 10:09:21 +0200 | [diff] [blame] | 925 | if (migrate_use_xbzrle()) { |
| 926 | info->has_xbzrle_cache = true; |
| 927 | info->xbzrle_cache = g_malloc0(sizeof(*info->xbzrle_cache)); |
| 928 | info->xbzrle_cache->cache_size = migrate_xbzrle_cache_size(); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 929 | info->xbzrle_cache->bytes = xbzrle_counters.bytes; |
| 930 | info->xbzrle_cache->pages = xbzrle_counters.pages; |
| 931 | info->xbzrle_cache->cache_miss = xbzrle_counters.cache_miss; |
| 932 | info->xbzrle_cache->cache_miss_rate = xbzrle_counters.cache_miss_rate; |
| 933 | info->xbzrle_cache->overflow = xbzrle_counters.overflow; |
Juan Quintela | 114f5ae | 2017-05-04 10:09:21 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Xiao Guangrong | 76e0300 | 2018-09-06 15:01:00 +0800 | [diff] [blame] | 936 | if (migrate_use_compression()) { |
| 937 | info->has_compression = true; |
| 938 | info->compression = g_malloc0(sizeof(*info->compression)); |
| 939 | info->compression->pages = compression_counters.pages; |
| 940 | info->compression->busy = compression_counters.busy; |
| 941 | info->compression->busy_rate = compression_counters.busy_rate; |
| 942 | info->compression->compressed_size = |
| 943 | compression_counters.compressed_size; |
| 944 | info->compression->compression_rate = |
| 945 | compression_counters.compression_rate; |
| 946 | } |
| 947 | |
Juan Quintela | 338182c | 2017-05-03 13:16:38 +0200 | [diff] [blame] | 948 | if (cpu_throttle_active()) { |
| 949 | info->has_cpu_throttle_percentage = true; |
| 950 | info->cpu_throttle_percentage = cpu_throttle_get_percentage(); |
| 951 | } |
| 952 | |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 953 | if (s->state != MIGRATION_STATUS_COMPLETED) { |
| 954 | info->ram->remaining = ram_bytes_remaining(); |
Juan Quintela | 9360447 | 2017-06-06 19:49:03 +0200 | [diff] [blame] | 955 | info->ram->dirty_pages_rate = ram_counters.dirty_pages_rate; |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 956 | } |
| 957 | } |
| 958 | |
Juan Quintela | 930ac04 | 2017-05-04 10:21:46 +0200 | [diff] [blame] | 959 | static void populate_disk_info(MigrationInfo *info) |
| 960 | { |
| 961 | if (blk_mig_active()) { |
| 962 | info->has_disk = true; |
| 963 | info->disk = g_malloc0(sizeof(*info->disk)); |
| 964 | info->disk->transferred = blk_mig_bytes_transferred(); |
| 965 | info->disk->remaining = blk_mig_bytes_remaining(); |
| 966 | info->disk->total = blk_mig_bytes_total(); |
| 967 | } |
| 968 | } |
| 969 | |
Alexey Perevalov | 65ace06 | 2018-03-22 21:17:27 +0300 | [diff] [blame] | 970 | static void fill_source_migration_info(MigrationInfo *info) |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 971 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 972 | MigrationState *s = migrate_get_current(); |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 973 | |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 974 | switch (s->state) { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 975 | case MIGRATION_STATUS_NONE: |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 976 | /* no migration has happened ever */ |
Alexey Perevalov | 65ace06 | 2018-03-22 21:17:27 +0300 | [diff] [blame] | 977 | /* do not overwrite destination migration status */ |
| 978 | return; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 979 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 980 | case MIGRATION_STATUS_SETUP: |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 981 | info->has_status = true; |
Michael R. Hines | ed4fbd1 | 2013-07-22 10:01:58 -0400 | [diff] [blame] | 982 | info->has_total_time = false; |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 983 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 984 | case MIGRATION_STATUS_ACTIVE: |
| 985 | case MIGRATION_STATUS_CANCELLING: |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 986 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
Dr. David Alan Gilbert | 31e0607 | 2017-10-20 10:05:51 +0100 | [diff] [blame] | 987 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
| 988 | case MIGRATION_STATUS_DEVICE: |
Peter Xu | a688d2c | 2018-05-02 18:47:18 +0800 | [diff] [blame] | 989 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 990 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
Wei Yang | 640dfb1 | 2019-07-16 08:54:11 +0800 | [diff] [blame] | 991 | /* TODO add some postcopy stats */ |
| 992 | populate_time_info(info, s); |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 993 | populate_ram_info(info, s); |
Juan Quintela | 930ac04 | 2017-05-04 10:21:46 +0200 | [diff] [blame] | 994 | populate_disk_info(info); |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 995 | break; |
zhanghailiang | 0b827d5 | 2016-10-27 14:42:54 +0800 | [diff] [blame] | 996 | case MIGRATION_STATUS_COLO: |
| 997 | info->has_status = true; |
| 998 | /* TODO: display COLO specific information (checkpoint info etc.) */ |
| 999 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1000 | case MIGRATION_STATUS_COMPLETED: |
Wei Yang | 640dfb1 | 2019-07-16 08:54:11 +0800 | [diff] [blame] | 1001 | populate_time_info(info, s); |
Dr. David Alan Gilbert | a22463a | 2016-06-13 12:16:41 +0100 | [diff] [blame] | 1002 | populate_ram_info(info, s); |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 1003 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1004 | case MIGRATION_STATUS_FAILED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 1005 | info->has_status = true; |
Daniel P. Berrange | d59ce6f | 2016-04-27 11:05:00 +0100 | [diff] [blame] | 1006 | if (s->error) { |
| 1007 | info->has_error_desc = true; |
| 1008 | info->error_desc = g_strdup(error_get_pretty(s->error)); |
| 1009 | } |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 1010 | break; |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1011 | case MIGRATION_STATUS_CANCELLED: |
Luiz Capitulino | 791e7c8 | 2011-09-13 17:37:16 -0300 | [diff] [blame] | 1012 | info->has_status = true; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 1013 | break; |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 1014 | case MIGRATION_STATUS_WAIT_UNPLUG: |
| 1015 | info->has_status = true; |
| 1016 | break; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 1017 | } |
zhanghailiang | cde63fb | 2015-03-13 16:08:41 +0800 | [diff] [blame] | 1018 | info->status = s->state; |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1021 | /** |
| 1022 | * @migration_caps_check - check capability validity |
| 1023 | * |
| 1024 | * @cap_list: old capability list, array of bool |
| 1025 | * @params: new capabilities to be applied soon |
| 1026 | * @errp: set *errp if the check failed, with reason |
| 1027 | * |
| 1028 | * Returns true if check passed, otherwise false. |
| 1029 | */ |
| 1030 | static bool migrate_caps_check(bool *cap_list, |
| 1031 | MigrationCapabilityStatusList *params, |
| 1032 | Error **errp) |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 1033 | { |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 1034 | MigrationCapabilityStatusList *cap; |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1035 | bool old_postcopy_cap; |
Alexey Perevalov | d7651f1 | 2017-09-19 19:47:56 +0300 | [diff] [blame] | 1036 | MigrationIncomingState *mis = migration_incoming_get_current(); |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 1037 | |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1038 | old_postcopy_cap = cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]; |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 1039 | |
| 1040 | for (cap = params; cap; cap = cap->next) { |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1041 | cap_list[cap->value->capability] = cap->value->state; |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 1042 | } |
Dr. David Alan Gilbert | 53dd370 | 2015-11-05 18:10:51 +0000 | [diff] [blame] | 1043 | |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1044 | #ifndef CONFIG_LIVE_BLOCK_MIGRATION |
| 1045 | if (cap_list[MIGRATION_CAPABILITY_BLOCK]) { |
| 1046 | error_setg(errp, "QEMU compiled without old-style (blk/-b, inc/-i) " |
| 1047 | "block migration"); |
| 1048 | error_append_hint(errp, "Use drive_mirror+NBD instead.\n"); |
| 1049 | return false; |
| 1050 | } |
| 1051 | #endif |
| 1052 | |
Zhang Chen | 7e934f5 | 2018-11-15 03:09:12 +0800 | [diff] [blame] | 1053 | #ifndef CONFIG_REPLICATION |
| 1054 | if (cap_list[MIGRATION_CAPABILITY_X_COLO]) { |
| 1055 | error_setg(errp, "QEMU compiled without replication module" |
| 1056 | " can't enable COLO"); |
| 1057 | error_append_hint(errp, "Please enable replication before COLO.\n"); |
| 1058 | return false; |
| 1059 | } |
| 1060 | #endif |
| 1061 | |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1062 | if (cap_list[MIGRATION_CAPABILITY_POSTCOPY_RAM]) { |
Dr. David Alan Gilbert | 096631b | 2016-06-13 12:16:45 +0100 | [diff] [blame] | 1063 | /* This check is reasonably expensive, so only when it's being |
| 1064 | * set the first time, also it's only the destination that needs |
| 1065 | * special support. |
| 1066 | */ |
| 1067 | if (!old_postcopy_cap && runstate_check(RUN_STATE_INMIGRATE) && |
Alexey Perevalov | d7651f1 | 2017-09-19 19:47:56 +0300 | [diff] [blame] | 1068 | !postcopy_ram_supported_by_host(mis)) { |
Dr. David Alan Gilbert | 096631b | 2016-06-13 12:16:45 +0100 | [diff] [blame] | 1069 | /* postcopy_ram_supported_by_host will have emitted a more |
| 1070 | * detailed message |
| 1071 | */ |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1072 | error_setg(errp, "Postcopy is not supported"); |
| 1073 | return false; |
Dr. David Alan Gilbert | 096631b | 2016-06-13 12:16:45 +0100 | [diff] [blame] | 1074 | } |
Yury Kotov | 1826906 | 2019-02-15 20:45:45 +0300 | [diff] [blame] | 1075 | |
| 1076 | if (cap_list[MIGRATION_CAPABILITY_X_IGNORE_SHARED]) { |
| 1077 | error_setg(errp, "Postcopy is not compatible with ignore-shared"); |
| 1078 | return false; |
| 1079 | } |
Dr. David Alan Gilbert | 53dd370 | 2015-11-05 18:10:51 +0000 | [diff] [blame] | 1080 | } |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1081 | |
| 1082 | return true; |
| 1083 | } |
| 1084 | |
Alexey Perevalov | 65ace06 | 2018-03-22 21:17:27 +0300 | [diff] [blame] | 1085 | static void fill_destination_migration_info(MigrationInfo *info) |
| 1086 | { |
| 1087 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 1088 | |
Juan Quintela | 9aca82b | 2019-02-27 11:51:27 +0100 | [diff] [blame] | 1089 | if (mis->socket_address_list) { |
| 1090 | info->has_socket_address = true; |
| 1091 | info->socket_address = |
| 1092 | QAPI_CLONE(SocketAddressList, mis->socket_address_list); |
| 1093 | } |
| 1094 | |
Alexey Perevalov | 65ace06 | 2018-03-22 21:17:27 +0300 | [diff] [blame] | 1095 | switch (mis->state) { |
| 1096 | case MIGRATION_STATUS_NONE: |
| 1097 | return; |
| 1098 | break; |
| 1099 | case MIGRATION_STATUS_SETUP: |
| 1100 | case MIGRATION_STATUS_CANCELLING: |
| 1101 | case MIGRATION_STATUS_CANCELLED: |
| 1102 | case MIGRATION_STATUS_ACTIVE: |
| 1103 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
Peter Xu | 3c9928d | 2018-07-10 17:18:56 +0800 | [diff] [blame] | 1104 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
| 1105 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
Alexey Perevalov | 65ace06 | 2018-03-22 21:17:27 +0300 | [diff] [blame] | 1106 | case MIGRATION_STATUS_FAILED: |
| 1107 | case MIGRATION_STATUS_COLO: |
| 1108 | info->has_status = true; |
| 1109 | break; |
| 1110 | case MIGRATION_STATUS_COMPLETED: |
| 1111 | info->has_status = true; |
| 1112 | fill_destination_postcopy_migration_info(info); |
| 1113 | break; |
| 1114 | } |
| 1115 | info->status = mis->state; |
| 1116 | } |
| 1117 | |
| 1118 | MigrationInfo *qmp_query_migrate(Error **errp) |
| 1119 | { |
| 1120 | MigrationInfo *info = g_malloc0(sizeof(*info)); |
| 1121 | |
| 1122 | fill_destination_migration_info(info); |
| 1123 | fill_source_migration_info(info); |
| 1124 | |
| 1125 | return info; |
| 1126 | } |
| 1127 | |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1128 | void qmp_migrate_set_capabilities(MigrationCapabilityStatusList *params, |
| 1129 | Error **errp) |
| 1130 | { |
| 1131 | MigrationState *s = migrate_get_current(); |
| 1132 | MigrationCapabilityStatusList *cap; |
Peter Xu | dd0ee30 | 2018-03-05 17:49:38 +0800 | [diff] [blame] | 1133 | bool cap_list[MIGRATION_CAPABILITY__MAX]; |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1134 | |
Juan Quintela | 392d87e | 2020-01-21 15:39:23 +0100 | [diff] [blame] | 1135 | if (migration_is_running(s->state)) { |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1136 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
| 1137 | return; |
| 1138 | } |
| 1139 | |
Peter Xu | dd0ee30 | 2018-03-05 17:49:38 +0800 | [diff] [blame] | 1140 | memcpy(cap_list, s->enabled_capabilities, sizeof(cap_list)); |
| 1141 | if (!migrate_caps_check(cap_list, params, errp)) { |
Peter Xu | 4a84214 | 2017-07-18 11:39:08 +0800 | [diff] [blame] | 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | for (cap = params; cap; cap = cap->next) { |
| 1146 | s->enabled_capabilities[cap->value->capability] = cap->value->state; |
| 1147 | } |
Orit Wasserman | 0045843 | 2012-08-06 21:42:48 +0300 | [diff] [blame] | 1148 | } |
| 1149 | |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1150 | /* |
| 1151 | * Check whether the parameters are valid. Error will be put into errp |
| 1152 | * (if provided). Return true if valid, otherwise false. |
| 1153 | */ |
| 1154 | static bool migrate_params_check(MigrationParameters *params, Error **errp) |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1155 | { |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1156 | if (params->has_compress_level && |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 1157 | (params->compress_level > 9)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1158 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "compress_level", |
| 1159 | "is invalid, it should be in the range of 0 to 9"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1160 | return false; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1161 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1162 | |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 1163 | if (params->has_compress_threads && (params->compress_threads < 1)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1164 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1165 | "compress_threads", |
| 1166 | "is invalid, it should be in the range of 1 to 255"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1167 | return false; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1168 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1169 | |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 1170 | if (params->has_decompress_threads && (params->decompress_threads < 1)) { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 1171 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1172 | "decompress_threads", |
| 1173 | "is invalid, it should be in the range of 1 to 255"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1174 | return false; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1175 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1176 | |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1177 | if (params->has_throttle_trigger_threshold && |
| 1178 | (params->throttle_trigger_threshold < 1 || |
| 1179 | params->throttle_trigger_threshold > 100)) { |
| 1180 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1181 | "throttle_trigger_threshold", |
| 1182 | "an integer in the range of 1 to 100"); |
| 1183 | return false; |
| 1184 | } |
| 1185 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1186 | if (params->has_cpu_throttle_initial && |
| 1187 | (params->cpu_throttle_initial < 1 || |
| 1188 | params->cpu_throttle_initial > 99)) { |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1189 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
Jason J. Herne | d85a31d | 2016-04-21 14:07:18 -0400 | [diff] [blame] | 1190 | "cpu_throttle_initial", |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1191 | "an integer in the range of 1 to 99"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1192 | return false; |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1193 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1194 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1195 | if (params->has_cpu_throttle_increment && |
| 1196 | (params->cpu_throttle_increment < 1 || |
| 1197 | params->cpu_throttle_increment > 99)) { |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1198 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
Jason J. Herne | d85a31d | 2016-04-21 14:07:18 -0400 | [diff] [blame] | 1199 | "cpu_throttle_increment", |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1200 | "an integer in the range of 1 to 99"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1201 | return false; |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1202 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1203 | |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 1204 | if (params->has_max_bandwidth && (params->max_bandwidth > SIZE_MAX)) { |
Mao Zhongyi | 2ee30cf | 2020-03-31 16:22:05 +0800 | [diff] [blame] | 1205 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1206 | "max_bandwidth", |
| 1207 | "an integer in the range of 0 to "stringify(SIZE_MAX) |
| 1208 | " bytes/second"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1209 | return false; |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 1210 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1211 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 1212 | if (params->has_downtime_limit && |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 1213 | (params->downtime_limit > MAX_MIGRATE_DOWNTIME)) { |
Mao Zhongyi | 2ee30cf | 2020-03-31 16:22:05 +0800 | [diff] [blame] | 1214 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1215 | "downtime_limit", |
| 1216 | "an integer in the range of 0 to " |
Mao Zhongyi | 7ac5529 | 2020-03-31 16:22:07 +0800 | [diff] [blame^] | 1217 | stringify(MAX_MIGRATE_DOWNTIME)" ms"); |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1218 | return false; |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 1219 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1220 | |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 1221 | /* x_checkpoint_delay is now always positive */ |
| 1222 | |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 1223 | if (params->has_multifd_channels && (params->multifd_channels < 1)) { |
Juan Quintela | 4075fb1 | 2016-01-15 08:56:17 +0100 | [diff] [blame] | 1224 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1225 | "multifd_channels", |
| 1226 | "is invalid, it should be in the range of 1 to 255"); |
| 1227 | return false; |
| 1228 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1229 | |
Juan Quintela | 9004db4 | 2020-01-23 17:08:52 +0100 | [diff] [blame] | 1230 | if (params->has_multifd_zlib_level && |
| 1231 | (params->multifd_zlib_level > 9)) { |
| 1232 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zlib_level", |
| 1233 | "is invalid, it should be in the range of 0 to 9"); |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
Juan Quintela | 6a9ad15 | 2020-01-23 17:41:36 +0100 | [diff] [blame] | 1237 | if (params->has_multifd_zstd_level && |
| 1238 | (params->multifd_zstd_level > 20)) { |
| 1239 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "multifd_zstd_level", |
| 1240 | "is invalid, it should be in the range of 0 to 20"); |
| 1241 | return false; |
| 1242 | } |
| 1243 | |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 1244 | if (params->has_xbzrle_cache_size && |
| 1245 | (params->xbzrle_cache_size < qemu_target_page_size() || |
| 1246 | !is_power_of_2(params->xbzrle_cache_size))) { |
| 1247 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1248 | "xbzrle_cache_size", |
| 1249 | "is invalid, it should be bigger than target page size" |
Mao Zhongyi | 06b1c6f | 2020-03-20 22:32:16 +0800 | [diff] [blame] | 1250 | " and a power of 2"); |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 1251 | return false; |
| 1252 | } |
| 1253 | |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 1254 | if (params->has_max_cpu_throttle && |
| 1255 | (params->max_cpu_throttle < params->cpu_throttle_initial || |
| 1256 | params->max_cpu_throttle > 99)) { |
| 1257 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1258 | "max_cpu_throttle", |
| 1259 | "an integer in the range of cpu_throttle_initial to 99"); |
| 1260 | return false; |
| 1261 | } |
| 1262 | |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 1263 | if (params->has_announce_initial && |
| 1264 | params->announce_initial > 100000) { |
| 1265 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1266 | "announce_initial", |
| 1267 | "is invalid, it must be less than 100000 ms"); |
| 1268 | return false; |
| 1269 | } |
| 1270 | if (params->has_announce_max && |
| 1271 | params->announce_max > 100000) { |
| 1272 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1273 | "announce_max", |
| 1274 | "is invalid, it must be less than 100000 ms"); |
| 1275 | return false; |
| 1276 | } |
| 1277 | if (params->has_announce_rounds && |
| 1278 | params->announce_rounds > 1000) { |
| 1279 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1280 | "announce_rounds", |
| 1281 | "is invalid, it must be in the range of 0 to 1000"); |
| 1282 | return false; |
| 1283 | } |
| 1284 | if (params->has_announce_step && |
| 1285 | (params->announce_step < 1 || |
| 1286 | params->announce_step > 10000)) { |
| 1287 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 1288 | "announce_step", |
| 1289 | "is invalid, it must be in the range of 1 to 10000 ms"); |
| 1290 | return false; |
| 1291 | } |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1292 | return true; |
| 1293 | } |
| 1294 | |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1295 | static void migrate_params_test_apply(MigrateSetParameters *params, |
| 1296 | MigrationParameters *dest) |
| 1297 | { |
| 1298 | *dest = migrate_get_current()->parameters; |
| 1299 | |
| 1300 | /* TODO use QAPI_CLONE() instead of duplicating it inline */ |
| 1301 | |
| 1302 | if (params->has_compress_level) { |
| 1303 | dest->compress_level = params->compress_level; |
| 1304 | } |
| 1305 | |
| 1306 | if (params->has_compress_threads) { |
| 1307 | dest->compress_threads = params->compress_threads; |
| 1308 | } |
| 1309 | |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1310 | if (params->has_compress_wait_thread) { |
| 1311 | dest->compress_wait_thread = params->compress_wait_thread; |
| 1312 | } |
| 1313 | |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1314 | if (params->has_decompress_threads) { |
| 1315 | dest->decompress_threads = params->decompress_threads; |
| 1316 | } |
| 1317 | |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1318 | if (params->has_throttle_trigger_threshold) { |
| 1319 | dest->throttle_trigger_threshold = params->throttle_trigger_threshold; |
| 1320 | } |
| 1321 | |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1322 | if (params->has_cpu_throttle_initial) { |
| 1323 | dest->cpu_throttle_initial = params->cpu_throttle_initial; |
| 1324 | } |
| 1325 | |
| 1326 | if (params->has_cpu_throttle_increment) { |
| 1327 | dest->cpu_throttle_increment = params->cpu_throttle_increment; |
| 1328 | } |
| 1329 | |
| 1330 | if (params->has_tls_creds) { |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1331 | assert(params->tls_creds->type == QTYPE_QSTRING); |
| 1332 | dest->tls_creds = g_strdup(params->tls_creds->u.s); |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | if (params->has_tls_hostname) { |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1336 | assert(params->tls_hostname->type == QTYPE_QSTRING); |
| 1337 | dest->tls_hostname = g_strdup(params->tls_hostname->u.s); |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | if (params->has_max_bandwidth) { |
| 1341 | dest->max_bandwidth = params->max_bandwidth; |
| 1342 | } |
| 1343 | |
| 1344 | if (params->has_downtime_limit) { |
| 1345 | dest->downtime_limit = params->downtime_limit; |
| 1346 | } |
| 1347 | |
| 1348 | if (params->has_x_checkpoint_delay) { |
| 1349 | dest->x_checkpoint_delay = params->x_checkpoint_delay; |
| 1350 | } |
| 1351 | |
| 1352 | if (params->has_block_incremental) { |
| 1353 | dest->block_incremental = params->block_incremental; |
| 1354 | } |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 1355 | if (params->has_multifd_channels) { |
| 1356 | dest->multifd_channels = params->multifd_channels; |
Juan Quintela | 5e7577a | 2017-10-09 18:07:56 +0200 | [diff] [blame] | 1357 | } |
Juan Quintela | 96eef04 | 2019-01-16 10:35:55 +0100 | [diff] [blame] | 1358 | if (params->has_multifd_compression) { |
| 1359 | dest->multifd_compression = params->multifd_compression; |
| 1360 | } |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 1361 | if (params->has_xbzrle_cache_size) { |
| 1362 | dest->xbzrle_cache_size = params->xbzrle_cache_size; |
| 1363 | } |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 1364 | if (params->has_max_postcopy_bandwidth) { |
| 1365 | dest->max_postcopy_bandwidth = params->max_postcopy_bandwidth; |
| 1366 | } |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 1367 | if (params->has_max_cpu_throttle) { |
| 1368 | dest->max_cpu_throttle = params->max_cpu_throttle; |
| 1369 | } |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 1370 | if (params->has_announce_initial) { |
| 1371 | dest->announce_initial = params->announce_initial; |
| 1372 | } |
| 1373 | if (params->has_announce_max) { |
| 1374 | dest->announce_max = params->announce_max; |
| 1375 | } |
| 1376 | if (params->has_announce_rounds) { |
| 1377 | dest->announce_rounds = params->announce_rounds; |
| 1378 | } |
| 1379 | if (params->has_announce_step) { |
| 1380 | dest->announce_step = params->announce_step; |
| 1381 | } |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1382 | } |
| 1383 | |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 1384 | static void migrate_params_apply(MigrateSetParameters *params, Error **errp) |
Peter Xu | 16d063b | 2017-07-18 11:39:04 +0800 | [diff] [blame] | 1385 | { |
| 1386 | MigrationState *s = migrate_get_current(); |
| 1387 | |
Markus Armbruster | e87fae4 | 2017-07-18 12:57:38 +0200 | [diff] [blame] | 1388 | /* TODO use QAPI_CLONE() instead of duplicating it inline */ |
| 1389 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1390 | if (params->has_compress_level) { |
| 1391 | s->parameters.compress_level = params->compress_level; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1392 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1393 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1394 | if (params->has_compress_threads) { |
| 1395 | s->parameters.compress_threads = params->compress_threads; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1396 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1397 | |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 1398 | if (params->has_compress_wait_thread) { |
| 1399 | s->parameters.compress_wait_thread = params->compress_wait_thread; |
| 1400 | } |
| 1401 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1402 | if (params->has_decompress_threads) { |
| 1403 | s->parameters.decompress_threads = params->decompress_threads; |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1404 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1405 | |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 1406 | if (params->has_throttle_trigger_threshold) { |
| 1407 | s->parameters.throttle_trigger_threshold = params->throttle_trigger_threshold; |
| 1408 | } |
| 1409 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1410 | if (params->has_cpu_throttle_initial) { |
| 1411 | s->parameters.cpu_throttle_initial = params->cpu_throttle_initial; |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1412 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1413 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1414 | if (params->has_cpu_throttle_increment) { |
| 1415 | s->parameters.cpu_throttle_increment = params->cpu_throttle_increment; |
Jason J. Herne | 1626fee | 2015-09-08 13:12:34 -0400 | [diff] [blame] | 1416 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1417 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1418 | if (params->has_tls_creds) { |
Daniel P. Berrange | 69ef1f3 | 2016-04-27 11:05:15 +0100 | [diff] [blame] | 1419 | g_free(s->parameters.tls_creds); |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1420 | assert(params->tls_creds->type == QTYPE_QSTRING); |
| 1421 | s->parameters.tls_creds = g_strdup(params->tls_creds->u.s); |
Daniel P. Berrange | 69ef1f3 | 2016-04-27 11:05:15 +0100 | [diff] [blame] | 1422 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1423 | |
Eric Blake | 7f375e0 | 2016-09-08 22:14:16 -0500 | [diff] [blame] | 1424 | if (params->has_tls_hostname) { |
Daniel P. Berrange | 69ef1f3 | 2016-04-27 11:05:15 +0100 | [diff] [blame] | 1425 | g_free(s->parameters.tls_hostname); |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1426 | assert(params->tls_hostname->type == QTYPE_QSTRING); |
| 1427 | s->parameters.tls_hostname = g_strdup(params->tls_hostname->u.s); |
Daniel P. Berrange | 69ef1f3 | 2016-04-27 11:05:15 +0100 | [diff] [blame] | 1428 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1429 | |
Daniel P. Berrange | d2f1d29 | 2019-02-27 14:53:24 +0000 | [diff] [blame] | 1430 | if (params->has_tls_authz) { |
| 1431 | g_free(s->parameters.tls_authz); |
| 1432 | assert(params->tls_authz->type == QTYPE_QSTRING); |
| 1433 | s->parameters.tls_authz = g_strdup(params->tls_authz->u.s); |
| 1434 | } |
| 1435 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 1436 | if (params->has_max_bandwidth) { |
| 1437 | s->parameters.max_bandwidth = params->max_bandwidth; |
Dr. David Alan Gilbert | c38c1c1 | 2019-03-08 10:12:10 +0000 | [diff] [blame] | 1438 | if (s->to_dst_file && !migration_in_postcopy()) { |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 1439 | qemu_file_set_rate_limit(s->to_dst_file, |
| 1440 | s->parameters.max_bandwidth / XFER_LIMIT_RATIO); |
| 1441 | } |
| 1442 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1443 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 1444 | if (params->has_downtime_limit) { |
| 1445 | s->parameters.downtime_limit = params->downtime_limit; |
| 1446 | } |
zhanghailiang | 68b5359 | 2016-10-27 14:43:01 +0800 | [diff] [blame] | 1447 | |
| 1448 | if (params->has_x_checkpoint_delay) { |
| 1449 | s->parameters.x_checkpoint_delay = params->x_checkpoint_delay; |
zhanghailiang | 479125d | 2017-01-17 20:57:42 +0800 | [diff] [blame] | 1450 | if (migration_in_colo_state()) { |
| 1451 | colo_checkpoint_notify(s); |
| 1452 | } |
zhanghailiang | 68b5359 | 2016-10-27 14:43:01 +0800 | [diff] [blame] | 1453 | } |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1454 | |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 1455 | if (params->has_block_incremental) { |
| 1456 | s->parameters.block_incremental = params->block_incremental; |
| 1457 | } |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 1458 | if (params->has_multifd_channels) { |
| 1459 | s->parameters.multifd_channels = params->multifd_channels; |
Juan Quintela | 4075fb1 | 2016-01-15 08:56:17 +0100 | [diff] [blame] | 1460 | } |
Juan Quintela | 96eef04 | 2019-01-16 10:35:55 +0100 | [diff] [blame] | 1461 | if (params->has_multifd_compression) { |
| 1462 | s->parameters.multifd_compression = params->multifd_compression; |
| 1463 | } |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 1464 | if (params->has_xbzrle_cache_size) { |
| 1465 | s->parameters.xbzrle_cache_size = params->xbzrle_cache_size; |
| 1466 | xbzrle_cache_resize(params->xbzrle_cache_size, errp); |
| 1467 | } |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 1468 | if (params->has_max_postcopy_bandwidth) { |
| 1469 | s->parameters.max_postcopy_bandwidth = params->max_postcopy_bandwidth; |
Dr. David Alan Gilbert | c38c1c1 | 2019-03-08 10:12:10 +0000 | [diff] [blame] | 1470 | if (s->to_dst_file && migration_in_postcopy()) { |
| 1471 | qemu_file_set_rate_limit(s->to_dst_file, |
| 1472 | s->parameters.max_postcopy_bandwidth / XFER_LIMIT_RATIO); |
| 1473 | } |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 1474 | } |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 1475 | if (params->has_max_cpu_throttle) { |
| 1476 | s->parameters.max_cpu_throttle = params->max_cpu_throttle; |
| 1477 | } |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 1478 | if (params->has_announce_initial) { |
| 1479 | s->parameters.announce_initial = params->announce_initial; |
| 1480 | } |
| 1481 | if (params->has_announce_max) { |
| 1482 | s->parameters.announce_max = params->announce_max; |
| 1483 | } |
| 1484 | if (params->has_announce_rounds) { |
| 1485 | s->parameters.announce_rounds = params->announce_rounds; |
| 1486 | } |
| 1487 | if (params->has_announce_step) { |
| 1488 | s->parameters.announce_step = params->announce_step; |
| 1489 | } |
Liang Li | 85de832 | 2015-03-23 16:32:28 +0800 | [diff] [blame] | 1490 | } |
| 1491 | |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1492 | void qmp_migrate_set_parameters(MigrateSetParameters *params, Error **errp) |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1493 | { |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1494 | MigrationParameters tmp; |
| 1495 | |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1496 | /* TODO Rewrite "" to null instead */ |
| 1497 | if (params->has_tls_creds |
| 1498 | && params->tls_creds->type == QTYPE_QNULL) { |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 1499 | qobject_unref(params->tls_creds->u.n); |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1500 | params->tls_creds->type = QTYPE_QSTRING; |
| 1501 | params->tls_creds->u.s = strdup(""); |
| 1502 | } |
| 1503 | /* TODO Rewrite "" to null instead */ |
| 1504 | if (params->has_tls_hostname |
| 1505 | && params->tls_hostname->type == QTYPE_QNULL) { |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 1506 | qobject_unref(params->tls_hostname->u.n); |
Markus Armbruster | 01fa559 | 2017-07-18 14:42:04 +0200 | [diff] [blame] | 1507 | params->tls_hostname->type = QTYPE_QSTRING; |
| 1508 | params->tls_hostname->u.s = strdup(""); |
| 1509 | } |
| 1510 | |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 1511 | migrate_params_test_apply(params, &tmp); |
| 1512 | |
| 1513 | if (!migrate_params_check(&tmp, errp)) { |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1514 | /* Invalid parameter */ |
| 1515 | return; |
| 1516 | } |
| 1517 | |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 1518 | migrate_params_apply(params, errp); |
Peter Xu | 476c72a | 2017-07-18 11:39:05 +0800 | [diff] [blame] | 1519 | } |
| 1520 | |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 1521 | |
Dr. David Alan Gilbert | 4886a1b | 2015-11-05 18:10:56 +0000 | [diff] [blame] | 1522 | void qmp_migrate_start_postcopy(Error **errp) |
| 1523 | { |
| 1524 | MigrationState *s = migrate_get_current(); |
| 1525 | |
Vladimir Sementsov-Ogievskiy | 16b0fd3 | 2018-03-13 15:34:01 -0400 | [diff] [blame] | 1526 | if (!migrate_postcopy()) { |
Dr. David Alan Gilbert | a54d340 | 2015-11-12 11:34:44 +0000 | [diff] [blame] | 1527 | error_setg(errp, "Enable postcopy with migrate_set_capability before" |
Dr. David Alan Gilbert | 4886a1b | 2015-11-05 18:10:56 +0000 | [diff] [blame] | 1528 | " the start of migration"); |
| 1529 | return; |
| 1530 | } |
| 1531 | |
| 1532 | if (s->state == MIGRATION_STATUS_NONE) { |
| 1533 | error_setg(errp, "Postcopy must be started after migration has been" |
| 1534 | " started"); |
| 1535 | return; |
| 1536 | } |
| 1537 | /* |
| 1538 | * we don't error if migration has finished since that would be racy |
| 1539 | * with issuing this command. |
| 1540 | */ |
| 1541 | atomic_set(&s->start_postcopy, true); |
| 1542 | } |
| 1543 | |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1544 | /* shared migration helpers */ |
| 1545 | |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 1546 | void migrate_set_state(int *state, int old_state, int new_state) |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 1547 | { |
Peter Xu | a31fede | 2017-08-30 16:32:01 +0800 | [diff] [blame] | 1548 | assert(new_state < MIGRATION_STATUS__MAX); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 1549 | if (atomic_cmpxchg(state, old_state, new_state) == old_state) { |
Peter Xu | a31fede | 2017-08-30 16:32:01 +0800 | [diff] [blame] | 1550 | trace_migrate_set_state(MigrationStatus_str(new_state)); |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 1551 | migrate_generate_event(new_state); |
Zhanghaoyu (A) | 51cf4c1 | 2013-11-07 11:01:15 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
Peter Xu | 4e4a3d3 | 2017-07-18 11:39:09 +0800 | [diff] [blame] | 1555 | static MigrationCapabilityStatusList *migrate_cap_add( |
| 1556 | MigrationCapabilityStatusList *list, |
| 1557 | MigrationCapability index, |
| 1558 | bool state) |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 1559 | { |
| 1560 | MigrationCapabilityStatusList *cap; |
| 1561 | |
| 1562 | cap = g_new0(MigrationCapabilityStatusList, 1); |
| 1563 | cap->value = g_new0(MigrationCapabilityStatus, 1); |
Peter Xu | 4e4a3d3 | 2017-07-18 11:39:09 +0800 | [diff] [blame] | 1564 | cap->value->capability = index; |
| 1565 | cap->value->state = state; |
| 1566 | cap->next = list; |
| 1567 | |
| 1568 | return cap; |
| 1569 | } |
| 1570 | |
| 1571 | void migrate_set_block_enabled(bool value, Error **errp) |
| 1572 | { |
| 1573 | MigrationCapabilityStatusList *cap; |
| 1574 | |
| 1575 | cap = migrate_cap_add(NULL, MIGRATION_CAPABILITY_BLOCK, value); |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 1576 | qmp_migrate_set_capabilities(cap, errp); |
| 1577 | qapi_free_MigrationCapabilityStatusList(cap); |
| 1578 | } |
| 1579 | |
| 1580 | static void migrate_set_block_incremental(MigrationState *s, bool value) |
| 1581 | { |
| 1582 | s->parameters.block_incremental = value; |
| 1583 | } |
| 1584 | |
| 1585 | static void block_cleanup_parameters(MigrationState *s) |
| 1586 | { |
| 1587 | if (s->must_remove_block_options) { |
| 1588 | /* setting to false can never fail */ |
| 1589 | migrate_set_block_enabled(false, &error_abort); |
| 1590 | migrate_set_block_incremental(s, false); |
| 1591 | s->must_remove_block_options = false; |
| 1592 | } |
| 1593 | } |
| 1594 | |
Yury Kotov | fd392cf | 2019-04-08 14:33:43 +0300 | [diff] [blame] | 1595 | static void migrate_fd_cleanup(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1596 | { |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 1597 | qemu_bh_delete(s->cleanup_bh); |
| 1598 | s->cleanup_bh = NULL; |
| 1599 | |
Peter Xu | 0ceccd8 | 2018-01-03 20:20:06 +0800 | [diff] [blame] | 1600 | qemu_savevm_state_cleanup(); |
| 1601 | |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 1602 | if (s->to_dst_file) { |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 1603 | QEMUFile *tmp; |
Juan Quintela | f986c3d | 2016-01-14 16:52:55 +0100 | [diff] [blame] | 1604 | |
Alexey Kardashevskiy | 9013dca | 2014-03-11 10:42:29 +1100 | [diff] [blame] | 1605 | trace_migrate_fd_cleanup(); |
Paolo Bonzini | 404a7c0 | 2013-02-22 17:36:46 +0100 | [diff] [blame] | 1606 | qemu_mutex_unlock_iothread(); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 1607 | if (s->migration_thread_running) { |
| 1608 | qemu_thread_join(&s->thread); |
| 1609 | s->migration_thread_running = false; |
| 1610 | } |
Paolo Bonzini | 404a7c0 | 2013-02-22 17:36:46 +0100 | [diff] [blame] | 1611 | qemu_mutex_lock_iothread(); |
| 1612 | |
Fei Li | 1398b2e | 2019-01-13 22:08:47 +0800 | [diff] [blame] | 1613 | multifd_save_cleanup(); |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 1614 | qemu_mutex_lock(&s->qemu_file_lock); |
| 1615 | tmp = s->to_dst_file; |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 1616 | s->to_dst_file = NULL; |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 1617 | qemu_mutex_unlock(&s->qemu_file_lock); |
| 1618 | /* |
| 1619 | * Close the file handle without the lock to make sure the |
| 1620 | * critical section won't block for long. |
| 1621 | */ |
| 1622 | qemu_fclose(tmp); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Wei Yang | 8f8d528 | 2019-07-17 08:53:41 +0800 | [diff] [blame] | 1625 | assert(!migration_is_active(s)); |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 1626 | |
Liang Li | 94f5a43 | 2015-11-02 15:37:00 +0800 | [diff] [blame] | 1627 | if (s->state == MIGRATION_STATUS_CANCELLING) { |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 1628 | migrate_set_state(&s->state, MIGRATION_STATUS_CANCELLING, |
Liang Li | 94f5a43 | 2015-11-02 15:37:00 +0800 | [diff] [blame] | 1629 | MIGRATION_STATUS_CANCELLED); |
Paolo Bonzini | 7a2c172 | 2013-02-22 17:36:09 +0100 | [diff] [blame] | 1630 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1631 | |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 1632 | if (s->error) { |
| 1633 | /* It is used on info migrate. We can't free it */ |
| 1634 | error_report_err(error_copy(s->error)); |
| 1635 | } |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 1636 | notifier_list_notify(&migration_state_notifiers, s); |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 1637 | block_cleanup_parameters(s); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Yury Kotov | fd392cf | 2019-04-08 14:33:43 +0300 | [diff] [blame] | 1640 | static void migrate_fd_cleanup_schedule(MigrationState *s) |
| 1641 | { |
| 1642 | /* |
| 1643 | * Ref the state for bh, because it may be called when |
| 1644 | * there're already no other refs |
| 1645 | */ |
| 1646 | object_ref(OBJECT(s)); |
| 1647 | qemu_bh_schedule(s->cleanup_bh); |
| 1648 | } |
| 1649 | |
| 1650 | static void migrate_fd_cleanup_bh(void *opaque) |
| 1651 | { |
| 1652 | MigrationState *s = opaque; |
| 1653 | migrate_fd_cleanup(s); |
| 1654 | object_unref(OBJECT(s)); |
| 1655 | } |
| 1656 | |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 1657 | void migrate_set_error(MigrationState *s, const Error *error) |
| 1658 | { |
Daniel Brodsky | 6e8a355 | 2020-04-03 21:21:08 -0700 | [diff] [blame] | 1659 | QEMU_LOCK_GUARD(&s->error_mutex); |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 1660 | if (!s->error) { |
| 1661 | s->error = error_copy(error); |
| 1662 | } |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 1663 | } |
| 1664 | |
Daniel P. Berrange | d59ce6f | 2016-04-27 11:05:00 +0100 | [diff] [blame] | 1665 | void migrate_fd_error(MigrationState *s, const Error *error) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1666 | { |
Peter Maydell | 2517405 | 2016-10-21 18:41:45 +0100 | [diff] [blame] | 1667 | trace_migrate_fd_error(error_get_pretty(error)); |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 1668 | assert(s->to_dst_file == NULL); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 1669 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 1670 | MIGRATION_STATUS_FAILED); |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 1671 | migrate_set_error(s, error); |
Juan Quintela | 458cf28 | 2011-02-22 23:32:54 +0100 | [diff] [blame] | 1672 | } |
| 1673 | |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 1674 | static void migrate_fd_cancel(MigrationState *s) |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1675 | { |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 1676 | int old_state ; |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 1677 | QEMUFile *f = migrate_get_current()->to_dst_file; |
Alexey Kardashevskiy | 9013dca | 2014-03-11 10:42:29 +1100 | [diff] [blame] | 1678 | trace_migrate_fd_cancel(); |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1679 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 1680 | if (s->rp_state.from_dst_file) { |
| 1681 | /* shutdown the rp socket, so causing the rp thread to shutdown */ |
| 1682 | qemu_file_shutdown(s->rp_state.from_dst_file); |
| 1683 | } |
| 1684 | |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 1685 | do { |
| 1686 | old_state = s->state; |
Juan Quintela | 392d87e | 2020-01-21 15:39:23 +0100 | [diff] [blame] | 1687 | if (!migration_is_running(old_state)) { |
Zhanghaoyu (A) | 6f2b811 | 2013-11-07 08:21:23 +0000 | [diff] [blame] | 1688 | break; |
| 1689 | } |
Dr. David Alan Gilbert | a7b36b4 | 2017-10-20 10:05:55 +0100 | [diff] [blame] | 1690 | /* If the migration is paused, kick it out of the pause */ |
| 1691 | if (old_state == MIGRATION_STATUS_PRE_SWITCHOVER) { |
| 1692 | qemu_sem_post(&s->pause_sem); |
| 1693 | } |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 1694 | migrate_set_state(&s->state, old_state, MIGRATION_STATUS_CANCELLING); |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1695 | } while (s->state != MIGRATION_STATUS_CANCELLING); |
Dr. David Alan Gilbert | a26ba26 | 2015-01-08 11:11:32 +0000 | [diff] [blame] | 1696 | |
| 1697 | /* |
| 1698 | * If we're unlucky the migration code might be stuck somewhere in a |
| 1699 | * send/write while the network has failed and is waiting to timeout; |
| 1700 | * if we've got shutdown(2) available then we can force it to quit. |
| 1701 | * The outgoing qemu file gets closed in migrate_fd_cleanup that is |
| 1702 | * called in a bh, so there is no race against this cancel. |
| 1703 | */ |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1704 | if (s->state == MIGRATION_STATUS_CANCELLING && f) { |
Dr. David Alan Gilbert | a26ba26 | 2015-01-08 11:11:32 +0000 | [diff] [blame] | 1705 | qemu_file_shutdown(f); |
| 1706 | } |
zhanghailiang | 1d2acc3 | 2017-01-24 15:59:52 +0800 | [diff] [blame] | 1707 | if (s->state == MIGRATION_STATUS_CANCELLING && s->block_inactive) { |
| 1708 | Error *local_err = NULL; |
| 1709 | |
| 1710 | bdrv_invalidate_cache_all(&local_err); |
| 1711 | if (local_err) { |
| 1712 | error_report_err(local_err); |
| 1713 | } else { |
| 1714 | s->block_inactive = false; |
| 1715 | } |
| 1716 | } |
aliguori | 065e281 | 2008-11-11 16:46:33 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 1719 | void add_migration_state_change_notifier(Notifier *notify) |
| 1720 | { |
| 1721 | notifier_list_add(&migration_state_notifiers, notify); |
| 1722 | } |
| 1723 | |
| 1724 | void remove_migration_state_change_notifier(Notifier *notify) |
| 1725 | { |
Paolo Bonzini | 3155252 | 2012-01-13 17:34:01 +0100 | [diff] [blame] | 1726 | notifier_remove(notify); |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 1727 | } |
| 1728 | |
Stefan Hajnoczi | 02edd2e | 2013-07-29 15:01:58 +0200 | [diff] [blame] | 1729 | bool migration_in_setup(MigrationState *s) |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 1730 | { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1731 | return s->state == MIGRATION_STATUS_SETUP; |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 1732 | } |
| 1733 | |
Juan Quintela | 7073693 | 2011-02-23 00:43:59 +0100 | [diff] [blame] | 1734 | bool migration_has_finished(MigrationState *s) |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 1735 | { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1736 | return s->state == MIGRATION_STATUS_COMPLETED; |
Gerd Hoffmann | 99a0db9 | 2010-12-13 17:30:12 +0100 | [diff] [blame] | 1737 | } |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 1738 | |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 1739 | bool migration_has_failed(MigrationState *s) |
| 1740 | { |
zhanghailiang | 3119473 | 2015-03-13 16:08:38 +0800 | [diff] [blame] | 1741 | return (s->state == MIGRATION_STATUS_CANCELLED || |
| 1742 | s->state == MIGRATION_STATUS_FAILED); |
Gerd Hoffmann | afe2df6 | 2011-10-25 13:50:11 +0200 | [diff] [blame] | 1743 | } |
| 1744 | |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 1745 | bool migration_in_postcopy(void) |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 1746 | { |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 1747 | MigrationState *s = migrate_get_current(); |
| 1748 | |
Dr. David Alan Gilbert | 3748fef | 2019-09-23 18:49:42 +0100 | [diff] [blame] | 1749 | switch (s->state) { |
| 1750 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
| 1751 | case MIGRATION_STATUS_POSTCOPY_PAUSED: |
| 1752 | case MIGRATION_STATUS_POSTCOPY_RECOVER: |
| 1753 | return true; |
| 1754 | default: |
| 1755 | return false; |
| 1756 | } |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 1757 | } |
| 1758 | |
Dr. David Alan Gilbert | b82fc32 | 2016-02-22 17:17:32 +0000 | [diff] [blame] | 1759 | bool migration_in_postcopy_after_devices(MigrationState *s) |
| 1760 | { |
Juan Quintela | 5727309 | 2017-03-20 22:25:28 +0100 | [diff] [blame] | 1761 | return migration_in_postcopy() && s->postcopy_after_devices; |
Dr. David Alan Gilbert | b82fc32 | 2016-02-22 17:17:32 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Juan Quintela | fab3500 | 2017-03-22 17:36:57 +0100 | [diff] [blame] | 1764 | bool migration_is_idle(void) |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1765 | { |
Markus Armbruster | daff7f0 | 2019-04-01 11:08:25 +0200 | [diff] [blame] | 1766 | MigrationState *s = current_migration; |
| 1767 | |
| 1768 | if (!s) { |
| 1769 | return true; |
| 1770 | } |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1771 | |
| 1772 | switch (s->state) { |
| 1773 | case MIGRATION_STATUS_NONE: |
| 1774 | case MIGRATION_STATUS_CANCELLED: |
| 1775 | case MIGRATION_STATUS_COMPLETED: |
| 1776 | case MIGRATION_STATUS_FAILED: |
| 1777 | return true; |
| 1778 | case MIGRATION_STATUS_SETUP: |
| 1779 | case MIGRATION_STATUS_CANCELLING: |
| 1780 | case MIGRATION_STATUS_ACTIVE: |
| 1781 | case MIGRATION_STATUS_POSTCOPY_ACTIVE: |
| 1782 | case MIGRATION_STATUS_COLO: |
Dr. David Alan Gilbert | 31e0607 | 2017-10-20 10:05:51 +0100 | [diff] [blame] | 1783 | case MIGRATION_STATUS_PRE_SWITCHOVER: |
| 1784 | case MIGRATION_STATUS_DEVICE: |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 1785 | case MIGRATION_STATUS_WAIT_UNPLUG: |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1786 | return false; |
| 1787 | case MIGRATION_STATUS__MAX: |
| 1788 | g_assert_not_reached(); |
| 1789 | } |
| 1790 | |
| 1791 | return false; |
| 1792 | } |
| 1793 | |
Wei Yang | 8f8d528 | 2019-07-17 08:53:41 +0800 | [diff] [blame] | 1794 | bool migration_is_active(MigrationState *s) |
| 1795 | { |
| 1796 | return (s->state == MIGRATION_STATUS_ACTIVE || |
| 1797 | s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); |
| 1798 | } |
| 1799 | |
Peter Xu | 3e0c805 | 2018-02-08 18:31:15 +0800 | [diff] [blame] | 1800 | void migrate_init(MigrationState *s) |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 1801 | { |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1802 | /* |
| 1803 | * Reinitialise all migration state, except |
| 1804 | * parameters/capabilities that the user set, and |
| 1805 | * locks. |
| 1806 | */ |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1807 | s->cleanup_bh = 0; |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 1808 | s->to_dst_file = NULL; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1809 | s->state = MIGRATION_STATUS_NONE; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1810 | s->rp_state.from_dst_file = NULL; |
| 1811 | s->rp_state.error = false; |
| 1812 | s->mbps = 0.0; |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 1813 | s->pages_per_second = 0.0; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1814 | s->downtime = 0; |
| 1815 | s->expected_downtime = 0; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1816 | s->setup_time = 0; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1817 | s->start_postcopy = false; |
Dr. David Alan Gilbert | b82fc32 | 2016-02-22 17:17:32 +0000 | [diff] [blame] | 1818 | s->postcopy_after_devices = false; |
Dr. David Alan Gilbert | 389775d | 2015-11-12 15:38:27 +0000 | [diff] [blame] | 1819 | s->migration_thread_running = false; |
Daniel P. Berrange | d59ce6f | 2016-04-27 11:05:00 +0100 | [diff] [blame] | 1820 | error_free(s->error); |
| 1821 | s->error = NULL; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 1822 | |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 1823 | migrate_set_state(&s->state, MIGRATION_STATUS_NONE, MIGRATION_STATUS_SETUP); |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 1824 | |
Peter Xu | 4af246a | 2018-01-03 20:20:08 +0800 | [diff] [blame] | 1825 | s->start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 1826 | s->total_time = 0; |
Peter Xu | 7287cbd | 2018-01-03 20:20:09 +0800 | [diff] [blame] | 1827 | s->vm_was_running = false; |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 1828 | s->iteration_initial_bytes = 0; |
| 1829 | s->threshold_size = 0; |
Juan Quintela | 0edda1c | 2010-05-11 16:28:39 +0200 | [diff] [blame] | 1830 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 1831 | |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 1832 | static GSList *migration_blockers; |
| 1833 | |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1834 | int migrate_add_blocker(Error *reason, Error **errp) |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 1835 | { |
Markus Armbruster | 811f865 | 2019-04-01 11:08:24 +0200 | [diff] [blame] | 1836 | if (only_migratable) { |
Markus Armbruster | 4b57664 | 2018-10-17 10:26:25 +0200 | [diff] [blame] | 1837 | error_propagate_prepend(errp, error_copy(reason), |
| 1838 | "disallowing migration blocker " |
Greg Kurz | b6eca81 | 2019-05-16 12:58:05 +0200 | [diff] [blame] | 1839 | "(--only-migratable) for: "); |
Ashijeet Acharya | b67b8c3 | 2017-01-16 17:01:54 +0530 | [diff] [blame] | 1840 | return -EACCES; |
| 1841 | } |
| 1842 | |
Juan Quintela | fab3500 | 2017-03-22 17:36:57 +0100 | [diff] [blame] | 1843 | if (migration_is_idle()) { |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1844 | migration_blockers = g_slist_prepend(migration_blockers, reason); |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Markus Armbruster | 4b57664 | 2018-10-17 10:26:25 +0200 | [diff] [blame] | 1848 | error_propagate_prepend(errp, error_copy(reason), |
| 1849 | "disallowing migration blocker " |
| 1850 | "(migration in progress) for: "); |
Ashijeet Acharya | fe44dc9 | 2017-01-16 17:01:53 +0530 | [diff] [blame] | 1851 | return -EBUSY; |
Anthony Liguori | fa2756b | 2011-11-14 15:09:43 -0600 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | void migrate_del_blocker(Error *reason) |
| 1855 | { |
| 1856 | migration_blockers = g_slist_remove(migration_blockers, reason); |
| 1857 | } |
| 1858 | |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 1859 | void qmp_migrate_incoming(const char *uri, Error **errp) |
| 1860 | { |
| 1861 | Error *local_err = NULL; |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 1862 | static bool once = true; |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 1863 | |
| 1864 | if (!deferred_incoming) { |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 1865 | error_setg(errp, "For use with '-incoming defer'"); |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 1866 | return; |
| 1867 | } |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 1868 | if (!once) { |
| 1869 | error_setg(errp, "The incoming migration has already been started"); |
Yury Kotov | 603d5a4 | 2019-11-13 20:53:25 +0300 | [diff] [blame] | 1870 | return; |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 1871 | } |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 1872 | |
| 1873 | qemu_start_incoming_migration(uri, &local_err); |
| 1874 | |
| 1875 | if (local_err) { |
| 1876 | error_propagate(errp, local_err); |
| 1877 | return; |
| 1878 | } |
| 1879 | |
Dr. David Alan Gilbert | 4debb5f | 2015-02-26 14:54:41 +0000 | [diff] [blame] | 1880 | once = false; |
Dr. David Alan Gilbert | bf1ae1f | 2015-02-19 11:40:28 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Peter Xu | 02affd4 | 2018-05-02 18:47:36 +0800 | [diff] [blame] | 1883 | void qmp_migrate_recover(const char *uri, Error **errp) |
| 1884 | { |
| 1885 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 1886 | |
| 1887 | if (mis->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { |
| 1888 | error_setg(errp, "Migrate recover can only be run " |
| 1889 | "when postcopy is paused."); |
| 1890 | return; |
| 1891 | } |
| 1892 | |
| 1893 | if (atomic_cmpxchg(&mis->postcopy_recover_triggered, |
| 1894 | false, true) == true) { |
| 1895 | error_setg(errp, "Migrate recovery is triggered already"); |
| 1896 | return; |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * Note that this call will never start a real migration; it will |
| 1901 | * only re-setup the migration stream and poke existing migration |
| 1902 | * to continue using that newly established channel. |
| 1903 | */ |
| 1904 | qemu_start_incoming_migration(uri, errp); |
| 1905 | } |
| 1906 | |
Peter Xu | bfbf89c | 2018-05-02 18:47:39 +0800 | [diff] [blame] | 1907 | void qmp_migrate_pause(Error **errp) |
| 1908 | { |
| 1909 | MigrationState *ms = migrate_get_current(); |
| 1910 | MigrationIncomingState *mis = migration_incoming_get_current(); |
| 1911 | int ret; |
| 1912 | |
| 1913 | if (ms->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
| 1914 | /* Source side, during postcopy */ |
| 1915 | qemu_mutex_lock(&ms->qemu_file_lock); |
| 1916 | ret = qemu_file_shutdown(ms->to_dst_file); |
| 1917 | qemu_mutex_unlock(&ms->qemu_file_lock); |
| 1918 | if (ret) { |
| 1919 | error_setg(errp, "Failed to pause source migration"); |
| 1920 | } |
| 1921 | return; |
| 1922 | } |
| 1923 | |
| 1924 | if (mis->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
| 1925 | ret = qemu_file_shutdown(mis->from_src_file); |
| 1926 | if (ret) { |
| 1927 | error_setg(errp, "Failed to pause destination migration"); |
| 1928 | } |
| 1929 | return; |
| 1930 | } |
| 1931 | |
| 1932 | error_setg(errp, "migrate-pause is currently only supported " |
| 1933 | "during postcopy-active state"); |
| 1934 | } |
| 1935 | |
Greg Kurz | 24f3902 | 2016-05-04 21:44:19 +0200 | [diff] [blame] | 1936 | bool migration_is_blocked(Error **errp) |
| 1937 | { |
| 1938 | if (qemu_savevm_state_blocked(errp)) { |
| 1939 | return true; |
| 1940 | } |
| 1941 | |
| 1942 | if (migration_blockers) { |
Eduardo Habkost | 250561e | 2017-06-08 10:39:05 -0300 | [diff] [blame] | 1943 | error_propagate(errp, error_copy(migration_blockers->data)); |
Greg Kurz | 24f3902 | 2016-05-04 21:44:19 +0200 | [diff] [blame] | 1944 | return true; |
| 1945 | } |
| 1946 | |
| 1947 | return false; |
| 1948 | } |
| 1949 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 1950 | /* Returns true if continue to migrate, or false if error detected */ |
| 1951 | static bool migrate_prepare(MigrationState *s, bool blk, bool blk_inc, |
| 1952 | bool resume, Error **errp) |
| 1953 | { |
| 1954 | Error *local_err = NULL; |
| 1955 | |
| 1956 | if (resume) { |
| 1957 | if (s->state != MIGRATION_STATUS_POSTCOPY_PAUSED) { |
| 1958 | error_setg(errp, "Cannot resume if there is no " |
| 1959 | "paused migration"); |
| 1960 | return false; |
| 1961 | } |
Peter Xu | 97ca211 | 2018-07-23 20:33:03 +0800 | [diff] [blame] | 1962 | |
| 1963 | /* |
| 1964 | * Postcopy recovery won't work well with release-ram |
| 1965 | * capability since release-ram will drop the page buffer as |
| 1966 | * long as the page is put into the send buffer. So if there |
| 1967 | * is a network failure happened, any page buffers that have |
| 1968 | * not yet reached the destination VM but have already been |
| 1969 | * sent from the source VM will be lost forever. Let's refuse |
| 1970 | * the client from resuming such a postcopy migration. |
| 1971 | * Luckily release-ram was designed to only be used when src |
| 1972 | * and destination VMs are on the same host, so it should be |
| 1973 | * fine. |
| 1974 | */ |
| 1975 | if (migrate_release_ram()) { |
| 1976 | error_setg(errp, "Postcopy recovery cannot work " |
| 1977 | "when release-ram capability is set"); |
| 1978 | return false; |
| 1979 | } |
| 1980 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 1981 | /* This is a resume, skip init status */ |
| 1982 | return true; |
| 1983 | } |
| 1984 | |
Juan Quintela | 392d87e | 2020-01-21 15:39:23 +0100 | [diff] [blame] | 1985 | if (migration_is_running(s->state)) { |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 1986 | error_setg(errp, QERR_MIGRATION_ACTIVE); |
| 1987 | return false; |
| 1988 | } |
| 1989 | |
| 1990 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
| 1991 | error_setg(errp, "Guest is waiting for an incoming migration"); |
| 1992 | return false; |
| 1993 | } |
| 1994 | |
| 1995 | if (migration_is_blocked(errp)) { |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
| 1999 | if (blk || blk_inc) { |
| 2000 | if (migrate_use_block() || migrate_use_block_incremental()) { |
| 2001 | error_setg(errp, "Command options are incompatible with " |
| 2002 | "current migration capabilities"); |
| 2003 | return false; |
| 2004 | } |
| 2005 | migrate_set_block_enabled(true, &local_err); |
| 2006 | if (local_err) { |
| 2007 | error_propagate(errp, local_err); |
| 2008 | return false; |
| 2009 | } |
| 2010 | s->must_remove_block_options = true; |
| 2011 | } |
| 2012 | |
| 2013 | if (blk_inc) { |
| 2014 | migrate_set_block_incremental(s, true); |
| 2015 | } |
| 2016 | |
| 2017 | migrate_init(s); |
Ivan Ren | 87f3bd8 | 2019-08-02 18:18:41 +0800 | [diff] [blame] | 2018 | /* |
| 2019 | * set ram_counters memory to zero for a |
| 2020 | * new migration |
| 2021 | */ |
| 2022 | memset(&ram_counters, 0, sizeof(ram_counters)); |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 2023 | |
| 2024 | return true; |
| 2025 | } |
| 2026 | |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2027 | void qmp_migrate(const char *uri, bool has_blk, bool blk, |
| 2028 | bool has_inc, bool inc, bool has_detach, bool detach, |
Peter Xu | 7a4da28 | 2018-05-02 18:47:23 +0800 | [diff] [blame] | 2029 | bool has_resume, bool resume, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2030 | { |
Paolo Bonzini | be7059c | 2012-10-03 14:34:33 +0200 | [diff] [blame] | 2031 | Error *local_err = NULL; |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 2032 | MigrationState *s = migrate_get_current(); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2033 | const char *p; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2034 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 2035 | if (!migrate_prepare(s, has_blk && blk, has_inc && inc, |
| 2036 | has_resume && resume, errp)) { |
| 2037 | /* Error detected, put into errp */ |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2038 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2039 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2040 | |
| 2041 | if (strstart(uri, "tcp:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 2042 | tcp_start_outgoing_migration(s, p, &local_err); |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 2043 | #ifdef CONFIG_RDMA |
Michael R. Hines | 41310c6 | 2013-12-19 04:52:01 +0800 | [diff] [blame] | 2044 | } else if (strstart(uri, "rdma:", &p)) { |
Michael R. Hines | 2da776d | 2013-07-22 10:01:54 -0400 | [diff] [blame] | 2045 | rdma_start_outgoing_migration(s, p, &local_err); |
| 2046 | #endif |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2047 | } else if (strstart(uri, "exec:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 2048 | exec_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2049 | } else if (strstart(uri, "unix:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 2050 | unix_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2051 | } else if (strstart(uri, "fd:", &p)) { |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 2052 | fd_start_outgoing_migration(s, p, &local_err); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2053 | } else { |
Markus Armbruster | c6bd8c7 | 2015-03-17 11:54:50 +0100 | [diff] [blame] | 2054 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "uri", |
| 2055 | "a valid migration protocol"); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 2056 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 2057 | MIGRATION_STATUS_FAILED); |
Dr. David Alan Gilbert | 09576e7 | 2018-03-16 20:21:14 +0000 | [diff] [blame] | 2058 | block_cleanup_parameters(s); |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2059 | return; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2060 | } |
| 2061 | |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 2062 | if (local_err) { |
Daniel P. Berrange | d59ce6f | 2016-04-27 11:05:00 +0100 | [diff] [blame] | 2063 | migrate_fd_error(s, local_err); |
Paolo Bonzini | f37afb5 | 2012-10-02 10:02:46 +0200 | [diff] [blame] | 2064 | error_propagate(errp, local_err); |
Luiz Capitulino | e1c37d0 | 2011-12-05 14:48:01 -0200 | [diff] [blame] | 2065 | return; |
Juan Quintela | 1299c63 | 2011-11-09 21:29:01 +0100 | [diff] [blame] | 2066 | } |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2067 | } |
| 2068 | |
Luiz Capitulino | 6cdedb0 | 2011-11-27 22:54:09 -0200 | [diff] [blame] | 2069 | void qmp_migrate_cancel(Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2070 | { |
Juan Quintela | 17549e8 | 2011-10-05 13:50:43 +0200 | [diff] [blame] | 2071 | migrate_fd_cancel(migrate_get_current()); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2072 | } |
| 2073 | |
Dr. David Alan Gilbert | 89cfc02 | 2017-10-20 10:05:53 +0100 | [diff] [blame] | 2074 | void qmp_migrate_continue(MigrationStatus state, Error **errp) |
| 2075 | { |
| 2076 | MigrationState *s = migrate_get_current(); |
| 2077 | if (s->state != state) { |
| 2078 | error_setg(errp, "Migration not in expected state: %s", |
| 2079 | MigrationStatus_str(s->state)); |
| 2080 | return; |
| 2081 | } |
| 2082 | qemu_sem_post(&s->pause_sem); |
| 2083 | } |
| 2084 | |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 2085 | void qmp_migrate_set_cache_size(int64_t value, Error **errp) |
| 2086 | { |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 2087 | MigrateSetParameters p = { |
| 2088 | .has_xbzrle_cache_size = true, |
| 2089 | .xbzrle_cache_size = value, |
| 2090 | }; |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 2091 | |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 2092 | qmp_migrate_set_parameters(&p, errp); |
Orit Wasserman | 9e1ba4c | 2012-08-06 21:42:54 +0300 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | int64_t qmp_query_migrate_cache_size(Error **errp) |
| 2096 | { |
| 2097 | return migrate_xbzrle_cache_size(); |
| 2098 | } |
| 2099 | |
Luiz Capitulino | 3dc8538 | 2011-11-28 11:59:37 -0200 | [diff] [blame] | 2100 | void qmp_migrate_set_speed(int64_t value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2101 | { |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 2102 | MigrateSetParameters p = { |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 2103 | .has_max_bandwidth = true, |
| 2104 | .max_bandwidth = value, |
| 2105 | }; |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2106 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 2107 | qmp_migrate_set_parameters(&p, errp); |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2108 | } |
| 2109 | |
Luiz Capitulino | 4f0a993 | 2011-11-27 23:18:01 -0200 | [diff] [blame] | 2110 | void qmp_migrate_set_downtime(double value, Error **errp) |
Juan Quintela | cab3014 | 2011-02-22 23:54:21 +0100 | [diff] [blame] | 2111 | { |
Daniel Henrique Barboza | 87c9cc1 | 2017-02-22 12:17:29 -0300 | [diff] [blame] | 2112 | if (value < 0 || value > MAX_MIGRATE_DOWNTIME_SECONDS) { |
Mao Zhongyi | 2ee30cf | 2020-03-31 16:22:05 +0800 | [diff] [blame] | 2113 | error_setg(errp, QERR_INVALID_PARAMETER_VALUE, |
| 2114 | "downtime_limit", |
| 2115 | "an integer in the range of 0 to " |
| 2116 | stringify(MAX_MIGRATE_DOWNTIME_SECONDS)" seconds"); |
Daniel Henrique Barboza | 87c9cc1 | 2017-02-22 12:17:29 -0300 | [diff] [blame] | 2117 | return; |
| 2118 | } |
| 2119 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 2120 | value *= 1000; /* Convert to milliseconds */ |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 2121 | |
Markus Armbruster | 1bda8b3 | 2017-07-18 13:42:11 +0200 | [diff] [blame] | 2122 | MigrateSetParameters p = { |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 2123 | .has_downtime_limit = true, |
Fangrui Song | 2667c98 | 2019-11-22 09:00:38 +0100 | [diff] [blame] | 2124 | .downtime_limit = (int64_t)value, |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 2125 | }; |
| 2126 | |
| 2127 | qmp_migrate_set_parameters(&p, errp); |
aliguori | 5bb7910 | 2008-10-13 03:12:02 +0000 | [diff] [blame] | 2128 | } |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 2129 | |
Pavel Butsykin | 53f09a1 | 2017-02-03 18:23:20 +0300 | [diff] [blame] | 2130 | bool migrate_release_ram(void) |
| 2131 | { |
| 2132 | MigrationState *s; |
| 2133 | |
| 2134 | s = migrate_get_current(); |
| 2135 | |
| 2136 | return s->enabled_capabilities[MIGRATION_CAPABILITY_RELEASE_RAM]; |
| 2137 | } |
| 2138 | |
Dr. David Alan Gilbert | 53dd370 | 2015-11-05 18:10:51 +0000 | [diff] [blame] | 2139 | bool migrate_postcopy_ram(void) |
| 2140 | { |
| 2141 | MigrationState *s; |
| 2142 | |
| 2143 | s = migrate_get_current(); |
| 2144 | |
Dr. David Alan Gilbert | 32c3db5 | 2016-03-11 09:53:36 +0000 | [diff] [blame] | 2145 | return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_RAM]; |
Dr. David Alan Gilbert | 53dd370 | 2015-11-05 18:10:51 +0000 | [diff] [blame] | 2146 | } |
| 2147 | |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 2148 | bool migrate_postcopy(void) |
| 2149 | { |
Vladimir Sementsov-Ogievskiy | dd6bb91 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 2150 | return migrate_postcopy_ram() || migrate_dirty_bitmaps(); |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 2151 | } |
| 2152 | |
Chegu Vinod | bde1e2e | 2013-06-24 03:49:42 -0600 | [diff] [blame] | 2153 | bool migrate_auto_converge(void) |
| 2154 | { |
| 2155 | MigrationState *s; |
| 2156 | |
| 2157 | s = migrate_get_current(); |
| 2158 | |
| 2159 | return s->enabled_capabilities[MIGRATION_CAPABILITY_AUTO_CONVERGE]; |
| 2160 | } |
| 2161 | |
Peter Lieven | 323004a | 2013-07-18 09:48:50 +0200 | [diff] [blame] | 2162 | bool migrate_zero_blocks(void) |
| 2163 | { |
| 2164 | MigrationState *s; |
| 2165 | |
| 2166 | s = migrate_get_current(); |
| 2167 | |
| 2168 | return s->enabled_capabilities[MIGRATION_CAPABILITY_ZERO_BLOCKS]; |
| 2169 | } |
| 2170 | |
Alexey Perevalov | f22f928 | 2018-03-22 21:17:22 +0300 | [diff] [blame] | 2171 | bool migrate_postcopy_blocktime(void) |
| 2172 | { |
| 2173 | MigrationState *s; |
| 2174 | |
| 2175 | s = migrate_get_current(); |
| 2176 | |
| 2177 | return s->enabled_capabilities[MIGRATION_CAPABILITY_POSTCOPY_BLOCKTIME]; |
| 2178 | } |
| 2179 | |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 2180 | bool migrate_use_compression(void) |
| 2181 | { |
Liang Li | dde4e69 | 2015-03-23 16:32:26 +0800 | [diff] [blame] | 2182 | MigrationState *s; |
| 2183 | |
| 2184 | s = migrate_get_current(); |
| 2185 | |
| 2186 | return s->enabled_capabilities[MIGRATION_CAPABILITY_COMPRESS]; |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | int migrate_compress_level(void) |
| 2190 | { |
| 2191 | MigrationState *s; |
| 2192 | |
| 2193 | s = migrate_get_current(); |
| 2194 | |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 2195 | return s->parameters.compress_level; |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | int migrate_compress_threads(void) |
| 2199 | { |
| 2200 | MigrationState *s; |
| 2201 | |
| 2202 | s = migrate_get_current(); |
| 2203 | |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 2204 | return s->parameters.compress_threads; |
Liang Li | 8706d2d | 2015-03-23 16:32:17 +0800 | [diff] [blame] | 2205 | } |
| 2206 | |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 2207 | int migrate_compress_wait_thread(void) |
| 2208 | { |
| 2209 | MigrationState *s; |
| 2210 | |
| 2211 | s = migrate_get_current(); |
| 2212 | |
| 2213 | return s->parameters.compress_wait_thread; |
| 2214 | } |
| 2215 | |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 2216 | int migrate_decompress_threads(void) |
| 2217 | { |
| 2218 | MigrationState *s; |
| 2219 | |
| 2220 | s = migrate_get_current(); |
| 2221 | |
Daniel P. Berrange | 2594f56 | 2016-04-27 11:05:14 +0100 | [diff] [blame] | 2222 | return s->parameters.decompress_threads; |
Liang Li | 3fcb38c | 2015-03-23 16:32:18 +0800 | [diff] [blame] | 2223 | } |
| 2224 | |
Vladimir Sementsov-Ogievskiy | 55efc8c | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 2225 | bool migrate_dirty_bitmaps(void) |
| 2226 | { |
| 2227 | MigrationState *s; |
| 2228 | |
| 2229 | s = migrate_get_current(); |
| 2230 | |
| 2231 | return s->enabled_capabilities[MIGRATION_CAPABILITY_DIRTY_BITMAPS]; |
| 2232 | } |
| 2233 | |
Yury Kotov | 1826906 | 2019-02-15 20:45:45 +0300 | [diff] [blame] | 2234 | bool migrate_ignore_shared(void) |
| 2235 | { |
| 2236 | MigrationState *s; |
| 2237 | |
| 2238 | s = migrate_get_current(); |
| 2239 | |
| 2240 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_IGNORE_SHARED]; |
| 2241 | } |
| 2242 | |
Yury Kotov | b9d68df | 2019-09-03 19:22:44 +0300 | [diff] [blame] | 2243 | bool migrate_validate_uuid(void) |
| 2244 | { |
| 2245 | MigrationState *s; |
| 2246 | |
| 2247 | s = migrate_get_current(); |
| 2248 | |
| 2249 | return s->enabled_capabilities[MIGRATION_CAPABILITY_VALIDATE_UUID]; |
| 2250 | } |
| 2251 | |
Juan Quintela | b05dc72 | 2015-07-07 14:44:05 +0200 | [diff] [blame] | 2252 | bool migrate_use_events(void) |
| 2253 | { |
| 2254 | MigrationState *s; |
| 2255 | |
| 2256 | s = migrate_get_current(); |
| 2257 | |
| 2258 | return s->enabled_capabilities[MIGRATION_CAPABILITY_EVENTS]; |
| 2259 | } |
| 2260 | |
Juan Quintela | 30126bb | 2016-01-14 12:23:00 +0100 | [diff] [blame] | 2261 | bool migrate_use_multifd(void) |
| 2262 | { |
| 2263 | MigrationState *s; |
| 2264 | |
| 2265 | s = migrate_get_current(); |
| 2266 | |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 2267 | return s->enabled_capabilities[MIGRATION_CAPABILITY_MULTIFD]; |
Juan Quintela | 30126bb | 2016-01-14 12:23:00 +0100 | [diff] [blame] | 2268 | } |
| 2269 | |
Dr. David Alan Gilbert | 93fbd03 | 2017-10-20 10:05:50 +0100 | [diff] [blame] | 2270 | bool migrate_pause_before_switchover(void) |
| 2271 | { |
| 2272 | MigrationState *s; |
| 2273 | |
| 2274 | s = migrate_get_current(); |
| 2275 | |
| 2276 | return s->enabled_capabilities[ |
| 2277 | MIGRATION_CAPABILITY_PAUSE_BEFORE_SWITCHOVER]; |
| 2278 | } |
| 2279 | |
Juan Quintela | 4075fb1 | 2016-01-15 08:56:17 +0100 | [diff] [blame] | 2280 | int migrate_multifd_channels(void) |
| 2281 | { |
| 2282 | MigrationState *s; |
| 2283 | |
| 2284 | s = migrate_get_current(); |
| 2285 | |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 2286 | return s->parameters.multifd_channels; |
Juan Quintela | 4075fb1 | 2016-01-15 08:56:17 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
Juan Quintela | ab7cbb0 | 2019-05-15 13:37:46 +0200 | [diff] [blame] | 2289 | MultiFDCompression migrate_multifd_compression(void) |
| 2290 | { |
| 2291 | MigrationState *s; |
| 2292 | |
| 2293 | s = migrate_get_current(); |
| 2294 | |
| 2295 | return s->parameters.multifd_compression; |
| 2296 | } |
| 2297 | |
Juan Quintela | 9004db4 | 2020-01-23 17:08:52 +0100 | [diff] [blame] | 2298 | int migrate_multifd_zlib_level(void) |
| 2299 | { |
| 2300 | MigrationState *s; |
| 2301 | |
| 2302 | s = migrate_get_current(); |
| 2303 | |
| 2304 | return s->parameters.multifd_zlib_level; |
| 2305 | } |
| 2306 | |
Juan Quintela | 6a9ad15 | 2020-01-23 17:41:36 +0100 | [diff] [blame] | 2307 | int migrate_multifd_zstd_level(void) |
| 2308 | { |
| 2309 | MigrationState *s; |
| 2310 | |
| 2311 | s = migrate_get_current(); |
| 2312 | |
| 2313 | return s->parameters.multifd_zstd_level; |
| 2314 | } |
| 2315 | |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 2316 | int migrate_use_xbzrle(void) |
| 2317 | { |
| 2318 | MigrationState *s; |
| 2319 | |
| 2320 | s = migrate_get_current(); |
| 2321 | |
| 2322 | return s->enabled_capabilities[MIGRATION_CAPABILITY_XBZRLE]; |
| 2323 | } |
| 2324 | |
| 2325 | int64_t migrate_xbzrle_cache_size(void) |
| 2326 | { |
| 2327 | MigrationState *s; |
| 2328 | |
| 2329 | s = migrate_get_current(); |
| 2330 | |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 2331 | return s->parameters.xbzrle_cache_size; |
Orit Wasserman | 17ad9b3 | 2012-08-06 21:42:53 +0300 | [diff] [blame] | 2332 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 2333 | |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 2334 | static int64_t migrate_max_postcopy_bandwidth(void) |
| 2335 | { |
| 2336 | MigrationState *s; |
| 2337 | |
| 2338 | s = migrate_get_current(); |
| 2339 | |
| 2340 | return s->parameters.max_postcopy_bandwidth; |
| 2341 | } |
| 2342 | |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 2343 | bool migrate_use_block(void) |
| 2344 | { |
| 2345 | MigrationState *s; |
| 2346 | |
| 2347 | s = migrate_get_current(); |
| 2348 | |
| 2349 | return s->enabled_capabilities[MIGRATION_CAPABILITY_BLOCK]; |
| 2350 | } |
| 2351 | |
Peter Xu | c788ada | 2017-06-26 18:28:55 +0800 | [diff] [blame] | 2352 | bool migrate_use_return_path(void) |
| 2353 | { |
| 2354 | MigrationState *s; |
| 2355 | |
| 2356 | s = migrate_get_current(); |
| 2357 | |
| 2358 | return s->enabled_capabilities[MIGRATION_CAPABILITY_RETURN_PATH]; |
| 2359 | } |
| 2360 | |
Juan Quintela | 2833c59 | 2017-04-05 18:32:37 +0200 | [diff] [blame] | 2361 | bool migrate_use_block_incremental(void) |
| 2362 | { |
| 2363 | MigrationState *s; |
| 2364 | |
| 2365 | s = migrate_get_current(); |
| 2366 | |
| 2367 | return s->parameters.block_incremental; |
| 2368 | } |
| 2369 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2370 | /* migration thread support */ |
| 2371 | /* |
| 2372 | * Something bad happened to the RP stream, mark an error |
| 2373 | * The caller shall print or trace something to indicate why |
| 2374 | */ |
| 2375 | static void mark_source_rp_bad(MigrationState *s) |
| 2376 | { |
| 2377 | s->rp_state.error = true; |
| 2378 | } |
| 2379 | |
| 2380 | static struct rp_cmd_args { |
| 2381 | ssize_t len; /* -1 = variable */ |
| 2382 | const char *name; |
| 2383 | } rp_cmd_args[] = { |
| 2384 | [MIG_RP_MSG_INVALID] = { .len = -1, .name = "INVALID" }, |
| 2385 | [MIG_RP_MSG_SHUT] = { .len = 4, .name = "SHUT" }, |
| 2386 | [MIG_RP_MSG_PONG] = { .len = 4, .name = "PONG" }, |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2387 | [MIG_RP_MSG_REQ_PAGES] = { .len = 12, .name = "REQ_PAGES" }, |
| 2388 | [MIG_RP_MSG_REQ_PAGES_ID] = { .len = -1, .name = "REQ_PAGES_ID" }, |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 2389 | [MIG_RP_MSG_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" }, |
Peter Xu | 13955b8 | 2018-05-02 18:47:30 +0800 | [diff] [blame] | 2390 | [MIG_RP_MSG_RESUME_ACK] = { .len = 4, .name = "RESUME_ACK" }, |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2391 | [MIG_RP_MSG_MAX] = { .len = -1, .name = "MAX" }, |
| 2392 | }; |
| 2393 | |
| 2394 | /* |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2395 | * Process a request for pages received on the return path, |
| 2396 | * We're allowed to send more than requested (e.g. to round to our page size) |
| 2397 | * and we don't need to send pages that have already been sent. |
| 2398 | */ |
| 2399 | static void migrate_handle_rp_req_pages(MigrationState *ms, const char* rbname, |
| 2400 | ram_addr_t start, size_t len) |
| 2401 | { |
Wei Yang | 038adc2 | 2019-10-13 10:11:45 +0800 | [diff] [blame] | 2402 | long our_host_ps = qemu_real_host_page_size; |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2403 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2404 | trace_migrate_handle_rp_req_pages(rbname, start, len); |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2405 | |
| 2406 | /* |
| 2407 | * Since we currently insist on matching page sizes, just sanity check |
| 2408 | * we're being asked for whole host pages. |
| 2409 | */ |
| 2410 | if (start & (our_host_ps-1) || |
| 2411 | (len & (our_host_ps-1))) { |
| 2412 | error_report("%s: Misaligned page request, start: " RAM_ADDR_FMT |
| 2413 | " len: %zd", __func__, start, len); |
| 2414 | mark_source_rp_bad(ms); |
| 2415 | return; |
| 2416 | } |
| 2417 | |
Juan Quintela | 9650689 | 2017-03-14 18:41:03 +0100 | [diff] [blame] | 2418 | if (ram_save_queue_pages(rbname, start, len)) { |
Dr. David Alan Gilbert | 6c595cd | 2015-11-05 18:11:08 +0000 | [diff] [blame] | 2419 | mark_source_rp_bad(ms); |
| 2420 | } |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 2423 | /* Return true to retry, false to quit */ |
| 2424 | static bool postcopy_pause_return_path_thread(MigrationState *s) |
| 2425 | { |
| 2426 | trace_postcopy_pause_return_path(); |
| 2427 | |
| 2428 | qemu_sem_wait(&s->postcopy_pause_rp_sem); |
| 2429 | |
| 2430 | trace_postcopy_pause_return_path_continued(); |
| 2431 | |
| 2432 | return true; |
| 2433 | } |
| 2434 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 2435 | static int migrate_handle_rp_recv_bitmap(MigrationState *s, char *block_name) |
| 2436 | { |
| 2437 | RAMBlock *block = qemu_ram_block_by_name(block_name); |
| 2438 | |
| 2439 | if (!block) { |
| 2440 | error_report("%s: invalid block name '%s'", __func__, block_name); |
| 2441 | return -EINVAL; |
| 2442 | } |
| 2443 | |
| 2444 | /* Fetch the received bitmap and refresh the dirty bitmap */ |
| 2445 | return ram_dirty_bitmap_reload(s, block); |
| 2446 | } |
| 2447 | |
Peter Xu | 13955b8 | 2018-05-02 18:47:30 +0800 | [diff] [blame] | 2448 | static int migrate_handle_rp_resume_ack(MigrationState *s, uint32_t value) |
| 2449 | { |
| 2450 | trace_source_return_path_thread_resume_ack(value); |
| 2451 | |
| 2452 | if (value != MIGRATION_RESUME_ACK_VALUE) { |
| 2453 | error_report("%s: illegal resume_ack value %"PRIu32, |
| 2454 | __func__, value); |
| 2455 | return -1; |
| 2456 | } |
| 2457 | |
| 2458 | /* Now both sides are active. */ |
| 2459 | migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_RECOVER, |
| 2460 | MIGRATION_STATUS_POSTCOPY_ACTIVE); |
| 2461 | |
Peter Xu | 9419069 | 2018-05-02 18:47:34 +0800 | [diff] [blame] | 2462 | /* Notify send thread that time to continue send pages */ |
| 2463 | qemu_sem_post(&s->rp_state.rp_sem); |
Peter Xu | 13955b8 | 2018-05-02 18:47:30 +0800 | [diff] [blame] | 2464 | |
| 2465 | return 0; |
| 2466 | } |
| 2467 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2468 | /* |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2469 | * Handles messages sent on the return path towards the source VM |
| 2470 | * |
| 2471 | */ |
| 2472 | static void *source_return_path_thread(void *opaque) |
| 2473 | { |
| 2474 | MigrationState *ms = opaque; |
| 2475 | QEMUFile *rp = ms->rp_state.from_dst_file; |
| 2476 | uint16_t header_len, header_type; |
Peter Xu | 568b01c | 2016-03-09 14:12:12 +0800 | [diff] [blame] | 2477 | uint8_t buf[512]; |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2478 | uint32_t tmp32, sibling_error; |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2479 | ram_addr_t start = 0; /* =0 to silence warning */ |
| 2480 | size_t len = 0, expected_len; |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2481 | int res; |
| 2482 | |
| 2483 | trace_source_return_path_thread_entry(); |
Lidong Chen | 74637e6 | 2018-08-06 21:29:29 +0800 | [diff] [blame] | 2484 | rcu_register_thread(); |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 2485 | |
| 2486 | retry: |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2487 | while (!ms->rp_state.error && !qemu_file_get_error(rp) && |
| 2488 | migration_is_setup_or_active(ms->state)) { |
| 2489 | trace_source_return_path_thread_loop_top(); |
| 2490 | header_type = qemu_get_be16(rp); |
| 2491 | header_len = qemu_get_be16(rp); |
| 2492 | |
Peter Xu | 7a9ddfb | 2018-02-08 18:31:05 +0800 | [diff] [blame] | 2493 | if (qemu_file_get_error(rp)) { |
| 2494 | mark_source_rp_bad(ms); |
| 2495 | goto out; |
| 2496 | } |
| 2497 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2498 | if (header_type >= MIG_RP_MSG_MAX || |
| 2499 | header_type == MIG_RP_MSG_INVALID) { |
| 2500 | error_report("RP: Received invalid message 0x%04x length 0x%04x", |
Mao Zhongyi | ed8b282 | 2020-03-28 01:19:08 +0800 | [diff] [blame] | 2501 | header_type, header_len); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2502 | mark_source_rp_bad(ms); |
| 2503 | goto out; |
| 2504 | } |
| 2505 | |
| 2506 | if ((rp_cmd_args[header_type].len != -1 && |
| 2507 | header_len != rp_cmd_args[header_type].len) || |
Peter Xu | 568b01c | 2016-03-09 14:12:12 +0800 | [diff] [blame] | 2508 | header_len > sizeof(buf)) { |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2509 | error_report("RP: Received '%s' message (0x%04x) with" |
Mao Zhongyi | ed8b282 | 2020-03-28 01:19:08 +0800 | [diff] [blame] | 2510 | "incorrect length %d expecting %zu", |
| 2511 | rp_cmd_args[header_type].name, header_type, header_len, |
| 2512 | (size_t)rp_cmd_args[header_type].len); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2513 | mark_source_rp_bad(ms); |
| 2514 | goto out; |
| 2515 | } |
| 2516 | |
| 2517 | /* We know we've got a valid header by this point */ |
| 2518 | res = qemu_get_buffer(rp, buf, header_len); |
| 2519 | if (res != header_len) { |
| 2520 | error_report("RP: Failed reading data for message 0x%04x" |
| 2521 | " read %d expected %d", |
| 2522 | header_type, res, header_len); |
| 2523 | mark_source_rp_bad(ms); |
| 2524 | goto out; |
| 2525 | } |
| 2526 | |
| 2527 | /* OK, we have the message and the data */ |
| 2528 | switch (header_type) { |
| 2529 | case MIG_RP_MSG_SHUT: |
Peter Maydell | 4d88513 | 2016-06-10 17:09:22 +0100 | [diff] [blame] | 2530 | sibling_error = ldl_be_p(buf); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2531 | trace_source_return_path_thread_shut(sibling_error); |
| 2532 | if (sibling_error) { |
| 2533 | error_report("RP: Sibling indicated error %d", sibling_error); |
| 2534 | mark_source_rp_bad(ms); |
| 2535 | } |
| 2536 | /* |
| 2537 | * We'll let the main thread deal with closing the RP |
| 2538 | * we could do a shutdown(2) on it, but we're the only user |
| 2539 | * anyway, so there's nothing gained. |
| 2540 | */ |
| 2541 | goto out; |
| 2542 | |
| 2543 | case MIG_RP_MSG_PONG: |
Peter Maydell | 4d88513 | 2016-06-10 17:09:22 +0100 | [diff] [blame] | 2544 | tmp32 = ldl_be_p(buf); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2545 | trace_source_return_path_thread_pong(tmp32); |
| 2546 | break; |
| 2547 | |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2548 | case MIG_RP_MSG_REQ_PAGES: |
Peter Maydell | 4d88513 | 2016-06-10 17:09:22 +0100 | [diff] [blame] | 2549 | start = ldq_be_p(buf); |
| 2550 | len = ldl_be_p(buf + 8); |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2551 | migrate_handle_rp_req_pages(ms, NULL, start, len); |
| 2552 | break; |
| 2553 | |
| 2554 | case MIG_RP_MSG_REQ_PAGES_ID: |
| 2555 | expected_len = 12 + 1; /* header + termination */ |
| 2556 | |
| 2557 | if (header_len >= expected_len) { |
Peter Maydell | 4d88513 | 2016-06-10 17:09:22 +0100 | [diff] [blame] | 2558 | start = ldq_be_p(buf); |
| 2559 | len = ldl_be_p(buf + 8); |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2560 | /* Now we expect an idstr */ |
| 2561 | tmp32 = buf[12]; /* Length of the following idstr */ |
| 2562 | buf[13 + tmp32] = '\0'; |
| 2563 | expected_len += tmp32; |
| 2564 | } |
| 2565 | if (header_len != expected_len) { |
| 2566 | error_report("RP: Req_Page_id with length %d expecting %zd", |
Mao Zhongyi | ed8b282 | 2020-03-28 01:19:08 +0800 | [diff] [blame] | 2567 | header_len, expected_len); |
Dr. David Alan Gilbert | 1e2d90e | 2015-11-05 18:11:07 +0000 | [diff] [blame] | 2568 | mark_source_rp_bad(ms); |
| 2569 | goto out; |
| 2570 | } |
| 2571 | migrate_handle_rp_req_pages(ms, (char *)&buf[13], start, len); |
| 2572 | break; |
| 2573 | |
Peter Xu | a335deb | 2018-05-02 18:47:28 +0800 | [diff] [blame] | 2574 | case MIG_RP_MSG_RECV_BITMAP: |
| 2575 | if (header_len < 1) { |
| 2576 | error_report("%s: missing block name", __func__); |
| 2577 | mark_source_rp_bad(ms); |
| 2578 | goto out; |
| 2579 | } |
| 2580 | /* Format: len (1B) + idstr (<255B). This ends the idstr. */ |
| 2581 | buf[buf[0] + 1] = '\0'; |
| 2582 | if (migrate_handle_rp_recv_bitmap(ms, (char *)(buf + 1))) { |
| 2583 | mark_source_rp_bad(ms); |
| 2584 | goto out; |
| 2585 | } |
| 2586 | break; |
| 2587 | |
Peter Xu | 13955b8 | 2018-05-02 18:47:30 +0800 | [diff] [blame] | 2588 | case MIG_RP_MSG_RESUME_ACK: |
| 2589 | tmp32 = ldl_be_p(buf); |
| 2590 | if (migrate_handle_rp_resume_ack(ms, tmp32)) { |
| 2591 | mark_source_rp_bad(ms); |
| 2592 | goto out; |
| 2593 | } |
| 2594 | break; |
| 2595 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2596 | default: |
| 2597 | break; |
| 2598 | } |
| 2599 | } |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 2600 | |
| 2601 | out: |
| 2602 | res = qemu_file_get_error(rp); |
| 2603 | if (res) { |
Dr. David Alan Gilbert | fb14a42 | 2019-10-07 11:35:07 +0100 | [diff] [blame] | 2604 | if (res == -EIO && migration_in_postcopy()) { |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 2605 | /* |
| 2606 | * Maybe there is something we can do: it looks like a |
| 2607 | * network down issue, and we pause for a recovery. |
| 2608 | */ |
| 2609 | if (postcopy_pause_return_path_thread(ms)) { |
| 2610 | /* Reload rp, reset the rest */ |
Marc-André Lureau | 0284a2a | 2018-09-25 13:22:45 +0400 | [diff] [blame] | 2611 | if (rp != ms->rp_state.from_dst_file) { |
| 2612 | qemu_fclose(rp); |
| 2613 | rp = ms->rp_state.from_dst_file; |
| 2614 | } |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 2615 | ms->rp_state.error = false; |
| 2616 | goto retry; |
| 2617 | } |
| 2618 | } |
| 2619 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2620 | trace_source_return_path_thread_bad_end(); |
| 2621 | mark_source_rp_bad(ms); |
| 2622 | } |
| 2623 | |
| 2624 | trace_source_return_path_thread_end(); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2625 | ms->rp_state.from_dst_file = NULL; |
| 2626 | qemu_fclose(rp); |
Lidong Chen | 74637e6 | 2018-08-06 21:29:29 +0800 | [diff] [blame] | 2627 | rcu_unregister_thread(); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2628 | return NULL; |
| 2629 | } |
| 2630 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 2631 | static int open_return_path_on_source(MigrationState *ms, |
| 2632 | bool create_thread) |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2633 | { |
| 2634 | |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 2635 | ms->rp_state.from_dst_file = qemu_file_get_return_path(ms->to_dst_file); |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2636 | if (!ms->rp_state.from_dst_file) { |
| 2637 | return -1; |
| 2638 | } |
| 2639 | |
| 2640 | trace_open_return_path_on_source(); |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 2641 | |
| 2642 | if (!create_thread) { |
| 2643 | /* We're done */ |
| 2644 | return 0; |
| 2645 | } |
| 2646 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2647 | qemu_thread_create(&ms->rp_state.rp_thread, "return path", |
| 2648 | source_return_path_thread, ms, QEMU_THREAD_JOINABLE); |
| 2649 | |
| 2650 | trace_open_return_path_on_source_continue(); |
| 2651 | |
| 2652 | return 0; |
| 2653 | } |
| 2654 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2655 | /* Returns 0 if the RP was ok, otherwise there was an error on the RP */ |
| 2656 | static int await_return_path_close_on_source(MigrationState *ms) |
| 2657 | { |
| 2658 | /* |
| 2659 | * If this is a normal exit then the destination will send a SHUT and the |
| 2660 | * rp_thread will exit, however if there's an error we need to cause |
| 2661 | * it to exit. |
| 2662 | */ |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 2663 | if (qemu_file_get_error(ms->to_dst_file) && ms->rp_state.from_dst_file) { |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 2664 | /* |
| 2665 | * shutdown(2), if we have it, will cause it to unblock if it's stuck |
| 2666 | * waiting for the destination. |
| 2667 | */ |
| 2668 | qemu_file_shutdown(ms->rp_state.from_dst_file); |
| 2669 | mark_source_rp_bad(ms); |
| 2670 | } |
| 2671 | trace_await_return_path_close_on_source_joining(); |
| 2672 | qemu_thread_join(&ms->rp_state.rp_thread); |
| 2673 | trace_await_return_path_close_on_source_close(); |
| 2674 | return ms->rp_state.error; |
| 2675 | } |
| 2676 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2677 | /* |
| 2678 | * Switch from normal iteration to postcopy |
| 2679 | * Returns non-0 on error |
| 2680 | */ |
Peter Xu | 7287cbd | 2018-01-03 20:20:09 +0800 | [diff] [blame] | 2681 | static int postcopy_start(MigrationState *ms) |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2682 | { |
| 2683 | int ret; |
Daniel P. Berrange | 61b67d4 | 2016-04-27 11:05:01 +0100 | [diff] [blame] | 2684 | QIOChannelBuffer *bioc; |
| 2685 | QEMUFile *fb; |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2686 | int64_t time_at_stop = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 2687 | int64_t bandwidth = migrate_max_postcopy_bandwidth(); |
Dr. David Alan Gilbert | ef8d648 | 2017-02-02 15:59:09 +0000 | [diff] [blame] | 2688 | bool restart_block = false; |
Dr. David Alan Gilbert | 0331c8c | 2017-10-20 10:05:56 +0100 | [diff] [blame] | 2689 | int cur_state = MIGRATION_STATUS_ACTIVE; |
| 2690 | if (!migrate_pause_before_switchover()) { |
| 2691 | migrate_set_state(&ms->state, MIGRATION_STATUS_ACTIVE, |
| 2692 | MIGRATION_STATUS_POSTCOPY_ACTIVE); |
| 2693 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2694 | |
| 2695 | trace_postcopy_start(); |
| 2696 | qemu_mutex_lock_iothread(); |
| 2697 | trace_postcopy_start_set_run(); |
| 2698 | |
Daniel Henrique Barboza | fb06411 | 2018-12-05 17:47:01 -0200 | [diff] [blame] | 2699 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2700 | global_state_store(); |
| 2701 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
Kevin Wolf | 76b1c7f | 2015-12-22 14:07:08 +0100 | [diff] [blame] | 2702 | if (ret < 0) { |
| 2703 | goto fail; |
| 2704 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2705 | |
Dr. David Alan Gilbert | 0331c8c | 2017-10-20 10:05:56 +0100 | [diff] [blame] | 2706 | ret = migration_maybe_pause(ms, &cur_state, |
| 2707 | MIGRATION_STATUS_POSTCOPY_ACTIVE); |
| 2708 | if (ret < 0) { |
| 2709 | goto fail; |
| 2710 | } |
| 2711 | |
Kevin Wolf | 76b1c7f | 2015-12-22 14:07:08 +0100 | [diff] [blame] | 2712 | ret = bdrv_inactivate_all(); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2713 | if (ret < 0) { |
| 2714 | goto fail; |
| 2715 | } |
Dr. David Alan Gilbert | ef8d648 | 2017-02-02 15:59:09 +0000 | [diff] [blame] | 2716 | restart_block = true; |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2717 | |
| 2718 | /* |
Dr. David Alan Gilbert | 1c0d249 | 2015-11-11 14:02:27 +0000 | [diff] [blame] | 2719 | * Cause any non-postcopiable, but iterative devices to |
| 2720 | * send out their final data. |
| 2721 | */ |
Fam Zheng | a1fbe75 | 2017-06-17 00:06:58 +0800 | [diff] [blame] | 2722 | qemu_savevm_state_complete_precopy(ms->to_dst_file, true, false); |
Dr. David Alan Gilbert | 1c0d249 | 2015-11-11 14:02:27 +0000 | [diff] [blame] | 2723 | |
| 2724 | /* |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2725 | * in Finish migrate and with the io-lock held everything should |
| 2726 | * be quiet, but we've potentially still got dirty pages and we |
| 2727 | * need to tell the destination to throw any pages it's already received |
| 2728 | * that are dirty |
| 2729 | */ |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 2730 | if (migrate_postcopy_ram()) { |
| 2731 | if (ram_postcopy_send_discard_bitmap(ms)) { |
| 2732 | error_report("postcopy send discard bitmap failed"); |
| 2733 | goto fail; |
| 2734 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | /* |
| 2738 | * send rest of state - note things that are doing postcopy |
| 2739 | * will notice we're in POSTCOPY_ACTIVE and not actually |
| 2740 | * wrap their state up here |
| 2741 | */ |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 2742 | /* 0 max-postcopy-bandwidth means unlimited */ |
| 2743 | if (!bandwidth) { |
| 2744 | qemu_file_set_rate_limit(ms->to_dst_file, INT64_MAX); |
| 2745 | } else { |
| 2746 | qemu_file_set_rate_limit(ms->to_dst_file, bandwidth / XFER_LIMIT_RATIO); |
| 2747 | } |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 2748 | if (migrate_postcopy_ram()) { |
| 2749 | /* Ping just for debugging, helps line traces up */ |
| 2750 | qemu_savevm_send_ping(ms->to_dst_file, 2); |
| 2751 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2752 | |
| 2753 | /* |
| 2754 | * While loading the device state we may trigger page transfer |
| 2755 | * requests and the fd must be free to process those, and thus |
| 2756 | * the destination must read the whole device state off the fd before |
| 2757 | * it starts processing it. Unfortunately the ad-hoc migration format |
| 2758 | * doesn't allow the destination to know the size to read without fully |
| 2759 | * parsing it through each devices load-state code (especially the open |
| 2760 | * coded devices that use get/put). |
| 2761 | * So we wrap the device state up in a package with a length at the start; |
| 2762 | * to do this we use a qemu_buf to hold the whole of the device state. |
| 2763 | */ |
Daniel P. Berrange | 61b67d4 | 2016-04-27 11:05:01 +0100 | [diff] [blame] | 2764 | bioc = qio_channel_buffer_new(4096); |
Daniel P. Berrange | 6f01f13 | 2016-09-30 11:57:14 +0100 | [diff] [blame] | 2765 | qio_channel_set_name(QIO_CHANNEL(bioc), "migration-postcopy-buffer"); |
Daniel P. Berrange | 61b67d4 | 2016-04-27 11:05:01 +0100 | [diff] [blame] | 2766 | fb = qemu_fopen_channel_output(QIO_CHANNEL(bioc)); |
| 2767 | object_unref(OBJECT(bioc)); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2768 | |
Dr. David Alan Gilbert | c76201a | 2015-11-05 18:11:18 +0000 | [diff] [blame] | 2769 | /* |
| 2770 | * Make sure the receiver can get incoming pages before we send the rest |
| 2771 | * of the state |
| 2772 | */ |
| 2773 | qemu_savevm_send_postcopy_listen(fb); |
| 2774 | |
Fam Zheng | a1fbe75 | 2017-06-17 00:06:58 +0800 | [diff] [blame] | 2775 | qemu_savevm_state_complete_precopy(fb, false, false); |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 2776 | if (migrate_postcopy_ram()) { |
| 2777 | qemu_savevm_send_ping(fb, 3); |
| 2778 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2779 | |
| 2780 | qemu_savevm_send_postcopy_run(fb); |
| 2781 | |
| 2782 | /* <><> end of stuff going into the package */ |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2783 | |
Dr. David Alan Gilbert | ef8d648 | 2017-02-02 15:59:09 +0000 | [diff] [blame] | 2784 | /* Last point of recovery; as soon as we send the package the destination |
| 2785 | * can open devices and potentially start running. |
| 2786 | * Lets just check again we've not got any errors. |
| 2787 | */ |
| 2788 | ret = qemu_file_get_error(ms->to_dst_file); |
| 2789 | if (ret) { |
| 2790 | error_report("postcopy_start: Migration stream errored (pre package)"); |
| 2791 | goto fail_closefb; |
| 2792 | } |
| 2793 | |
| 2794 | restart_block = false; |
| 2795 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2796 | /* Now send that blob */ |
Daniel P. Berrange | 61b67d4 | 2016-04-27 11:05:01 +0100 | [diff] [blame] | 2797 | if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage)) { |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2798 | goto fail_closefb; |
| 2799 | } |
| 2800 | qemu_fclose(fb); |
Dr. David Alan Gilbert | b82fc32 | 2016-02-22 17:17:32 +0000 | [diff] [blame] | 2801 | |
| 2802 | /* Send a notify to give a chance for anything that needs to happen |
| 2803 | * at the transition to postcopy and after the device state; in particular |
| 2804 | * spice needs to trigger a transition now |
| 2805 | */ |
| 2806 | ms->postcopy_after_devices = true; |
| 2807 | notifier_list_notify(&migration_state_notifiers, ms); |
| 2808 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2809 | ms->downtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) - time_at_stop; |
| 2810 | |
| 2811 | qemu_mutex_unlock_iothread(); |
| 2812 | |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 2813 | if (migrate_postcopy_ram()) { |
| 2814 | /* |
| 2815 | * Although this ping is just for debug, it could potentially be |
| 2816 | * used for getting a better measurement of downtime at the source. |
| 2817 | */ |
| 2818 | qemu_savevm_send_ping(ms->to_dst_file, 4); |
| 2819 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2820 | |
Pavel Butsykin | ced1c61 | 2017-02-03 18:23:21 +0300 | [diff] [blame] | 2821 | if (migrate_release_ram()) { |
| 2822 | ram_postcopy_migrated_memory_release(ms); |
| 2823 | } |
| 2824 | |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 2825 | ret = qemu_file_get_error(ms->to_dst_file); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2826 | if (ret) { |
| 2827 | error_report("postcopy_start: Migration stream errored"); |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 2828 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2829 | MIGRATION_STATUS_FAILED); |
| 2830 | } |
| 2831 | |
| 2832 | return ret; |
| 2833 | |
| 2834 | fail_closefb: |
| 2835 | qemu_fclose(fb); |
| 2836 | fail: |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 2837 | migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE, |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2838 | MIGRATION_STATUS_FAILED); |
Dr. David Alan Gilbert | ef8d648 | 2017-02-02 15:59:09 +0000 | [diff] [blame] | 2839 | if (restart_block) { |
| 2840 | /* A failure happened early enough that we know the destination hasn't |
| 2841 | * accessed block devices, so we're safe to recover. |
| 2842 | */ |
| 2843 | Error *local_err = NULL; |
| 2844 | |
| 2845 | bdrv_invalidate_cache_all(&local_err); |
| 2846 | if (local_err) { |
| 2847 | error_report_err(local_err); |
| 2848 | } |
| 2849 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 2850 | qemu_mutex_unlock_iothread(); |
| 2851 | return -1; |
| 2852 | } |
| 2853 | |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2854 | /** |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 2855 | * migration_maybe_pause: Pause if required to by |
| 2856 | * migrate_pause_before_switchover called with the iothread locked |
| 2857 | * Returns: 0 on success |
| 2858 | */ |
Dr. David Alan Gilbert | 0331c8c | 2017-10-20 10:05:56 +0100 | [diff] [blame] | 2859 | static int migration_maybe_pause(MigrationState *s, |
| 2860 | int *current_active_state, |
| 2861 | int new_state) |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 2862 | { |
| 2863 | if (!migrate_pause_before_switchover()) { |
| 2864 | return 0; |
| 2865 | } |
| 2866 | |
| 2867 | /* Since leaving this state is not atomic with posting the semaphore |
| 2868 | * it's possible that someone could have issued multiple migrate_continue |
| 2869 | * and the semaphore is incorrectly positive at this point; |
| 2870 | * the docs say it's undefined to reinit a semaphore that's already |
| 2871 | * init'd, so use timedwait to eat up any existing posts. |
| 2872 | */ |
| 2873 | while (qemu_sem_timedwait(&s->pause_sem, 1) == 0) { |
| 2874 | /* This block intentionally left blank */ |
| 2875 | } |
| 2876 | |
Zhimin Feng | 8958338 | 2020-01-14 17:43:09 +0800 | [diff] [blame] | 2877 | /* |
| 2878 | * If the migration is cancelled when it is in the completion phase, |
| 2879 | * the migration state is set to MIGRATION_STATUS_CANCELLING. |
| 2880 | * So we don't need to wait a semaphore, otherwise we would always |
| 2881 | * wait for the 'pause_sem' semaphore. |
| 2882 | */ |
| 2883 | if (s->state != MIGRATION_STATUS_CANCELLING) { |
| 2884 | qemu_mutex_unlock_iothread(); |
| 2885 | migrate_set_state(&s->state, *current_active_state, |
| 2886 | MIGRATION_STATUS_PRE_SWITCHOVER); |
| 2887 | qemu_sem_wait(&s->pause_sem); |
| 2888 | migrate_set_state(&s->state, MIGRATION_STATUS_PRE_SWITCHOVER, |
| 2889 | new_state); |
| 2890 | *current_active_state = new_state; |
| 2891 | qemu_mutex_lock_iothread(); |
| 2892 | } |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 2893 | |
Dr. David Alan Gilbert | 0331c8c | 2017-10-20 10:05:56 +0100 | [diff] [blame] | 2894 | return s->state == new_state ? 0 : -EINVAL; |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | /** |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2898 | * migration_completion: Used by migration_thread when there's not much left. |
| 2899 | * The caller 'breaks' the loop when this returns. |
| 2900 | * |
| 2901 | * @s: Current migration state |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2902 | */ |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 2903 | static void migration_completion(MigrationState *s) |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2904 | { |
| 2905 | int ret; |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 2906 | int current_active_state = s->state; |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2907 | |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2908 | if (s->state == MIGRATION_STATUS_ACTIVE) { |
| 2909 | qemu_mutex_lock_iothread(); |
Peter Xu | 64909f9 | 2018-01-03 20:20:10 +0800 | [diff] [blame] | 2910 | s->downtime_start = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Daniel Henrique Barboza | fb06411 | 2018-12-05 17:47:01 -0200 | [diff] [blame] | 2911 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, NULL); |
Peter Xu | 7287cbd | 2018-01-03 20:20:09 +0800 | [diff] [blame] | 2912 | s->vm_was_running = runstate_is_running(); |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2913 | ret = global_state_store(); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2914 | |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2915 | if (!ret) { |
Fam Zheng | a1fbe75 | 2017-06-17 00:06:58 +0800 | [diff] [blame] | 2916 | bool inactivate = !migrate_colo_enabled(); |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2917 | ret = vm_stop_force_state(RUN_STATE_FINISH_MIGRATE); |
Kevin Wolf | f07fa4c | 2017-05-22 17:10:38 +0200 | [diff] [blame] | 2918 | if (ret >= 0) { |
Dr. David Alan Gilbert | 0331c8c | 2017-10-20 10:05:56 +0100 | [diff] [blame] | 2919 | ret = migration_maybe_pause(s, ¤t_active_state, |
| 2920 | MIGRATION_STATUS_DEVICE); |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 2921 | } |
| 2922 | if (ret >= 0) { |
Kevin Wolf | f07fa4c | 2017-05-22 17:10:38 +0200 | [diff] [blame] | 2923 | qemu_file_set_rate_limit(s->to_dst_file, INT64_MAX); |
Fam Zheng | a1fbe75 | 2017-06-17 00:06:58 +0800 | [diff] [blame] | 2924 | ret = qemu_savevm_state_complete_precopy(s->to_dst_file, false, |
| 2925 | inactivate); |
Kevin Wolf | f07fa4c | 2017-05-22 17:10:38 +0200 | [diff] [blame] | 2926 | } |
Fam Zheng | a1fbe75 | 2017-06-17 00:06:58 +0800 | [diff] [blame] | 2927 | if (inactivate && ret >= 0) { |
| 2928 | s->block_inactive = true; |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2929 | } |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2930 | } |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2931 | qemu_mutex_unlock_iothread(); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2932 | |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2933 | if (ret < 0) { |
| 2934 | goto fail; |
| 2935 | } |
| 2936 | } else if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
| 2937 | trace_migration_completion_postcopy_end(); |
| 2938 | |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 2939 | qemu_savevm_state_complete_postcopy(s->to_dst_file); |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2940 | trace_migration_completion_postcopy_end_after_complete(); |
| 2941 | } |
| 2942 | |
| 2943 | /* |
| 2944 | * If rp was opened we must clean up the thread before |
| 2945 | * cleaning everything else up (since if there are no failures |
| 2946 | * it will wait for the destination to send it's status in |
| 2947 | * a SHUT command). |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2948 | */ |
Peter Xu | 0425dc9 | 2017-05-31 18:35:34 +0800 | [diff] [blame] | 2949 | if (s->rp_state.from_dst_file) { |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2950 | int rp_error; |
Peter Xu | 0425dc9 | 2017-05-31 18:35:34 +0800 | [diff] [blame] | 2951 | trace_migration_return_path_end_before(); |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2952 | rp_error = await_return_path_close_on_source(s); |
Peter Xu | 0425dc9 | 2017-05-31 18:35:34 +0800 | [diff] [blame] | 2953 | trace_migration_return_path_end_after(rp_error); |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2954 | if (rp_error) { |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2955 | goto fail_invalidate; |
Dr. David Alan Gilbert | b10ac0c | 2015-11-05 18:11:06 +0000 | [diff] [blame] | 2956 | } |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2957 | } |
| 2958 | |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 2959 | if (qemu_file_get_error(s->to_dst_file)) { |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2960 | trace_migration_completion_file_err(); |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2961 | goto fail_invalidate; |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2962 | } |
| 2963 | |
zhanghailiang | 0b827d5 | 2016-10-27 14:42:54 +0800 | [diff] [blame] | 2964 | if (!migrate_colo_enabled()) { |
| 2965 | migrate_set_state(&s->state, current_active_state, |
| 2966 | MIGRATION_STATUS_COMPLETED); |
| 2967 | } |
| 2968 | |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2969 | return; |
| 2970 | |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2971 | fail_invalidate: |
| 2972 | /* If not doing postcopy, vm_start() will be called: let's regain |
| 2973 | * control on images. |
| 2974 | */ |
Dr. David Alan Gilbert | 6039dd5 | 2018-02-05 09:13:37 +0000 | [diff] [blame] | 2975 | if (s->state == MIGRATION_STATUS_ACTIVE || |
| 2976 | s->state == MIGRATION_STATUS_DEVICE) { |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2977 | Error *local_err = NULL; |
| 2978 | |
zhanghailiang | 1d2acc3 | 2017-01-24 15:59:52 +0800 | [diff] [blame] | 2979 | qemu_mutex_lock_iothread(); |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2980 | bdrv_invalidate_cache_all(&local_err); |
| 2981 | if (local_err) { |
| 2982 | error_report_err(local_err); |
zhanghailiang | 1d2acc3 | 2017-01-24 15:59:52 +0800 | [diff] [blame] | 2983 | } else { |
| 2984 | s->block_inactive = false; |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2985 | } |
zhanghailiang | 1d2acc3 | 2017-01-24 15:59:52 +0800 | [diff] [blame] | 2986 | qemu_mutex_unlock_iothread(); |
Greg Kurz | fe904ea | 2016-05-18 15:44:36 +0200 | [diff] [blame] | 2987 | } |
| 2988 | |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2989 | fail: |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 2990 | migrate_set_state(&s->state, current_active_state, |
| 2991 | MIGRATION_STATUS_FAILED); |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 2992 | } |
| 2993 | |
zhanghailiang | 35a6ed4 | 2016-10-27 14:42:52 +0800 | [diff] [blame] | 2994 | bool migrate_colo_enabled(void) |
| 2995 | { |
| 2996 | MigrationState *s = migrate_get_current(); |
| 2997 | return s->enabled_capabilities[MIGRATION_CAPABILITY_X_COLO]; |
| 2998 | } |
| 2999 | |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3000 | typedef enum MigThrError { |
| 3001 | /* No error detected */ |
| 3002 | MIG_THR_ERR_NONE = 0, |
| 3003 | /* Detected error, but resumed successfully */ |
| 3004 | MIG_THR_ERR_RECOVERED = 1, |
| 3005 | /* Detected fatal error, need to exit */ |
| 3006 | MIG_THR_ERR_FATAL = 2, |
| 3007 | } MigThrError; |
| 3008 | |
Peter Xu | 9419069 | 2018-05-02 18:47:34 +0800 | [diff] [blame] | 3009 | static int postcopy_resume_handshake(MigrationState *s) |
| 3010 | { |
| 3011 | qemu_savevm_send_postcopy_resume(s->to_dst_file); |
| 3012 | |
| 3013 | while (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { |
| 3014 | qemu_sem_wait(&s->rp_state.rp_sem); |
| 3015 | } |
| 3016 | |
| 3017 | if (s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE) { |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
| 3021 | return -1; |
| 3022 | } |
| 3023 | |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3024 | /* Return zero if success, or <0 for error */ |
| 3025 | static int postcopy_do_resume(MigrationState *s) |
| 3026 | { |
Peter Xu | d1b8ead | 2018-05-02 18:47:31 +0800 | [diff] [blame] | 3027 | int ret; |
| 3028 | |
| 3029 | /* |
| 3030 | * Call all the resume_prepare() hooks, so that modules can be |
| 3031 | * ready for the migration resume. |
| 3032 | */ |
| 3033 | ret = qemu_savevm_state_resume_prepare(s); |
| 3034 | if (ret) { |
| 3035 | error_report("%s: resume_prepare() failure detected: %d", |
| 3036 | __func__, ret); |
| 3037 | return ret; |
| 3038 | } |
| 3039 | |
| 3040 | /* |
Peter Xu | 9419069 | 2018-05-02 18:47:34 +0800 | [diff] [blame] | 3041 | * Last handshake with destination on the resume (destination will |
| 3042 | * switch to postcopy-active afterwards) |
Peter Xu | d1b8ead | 2018-05-02 18:47:31 +0800 | [diff] [blame] | 3043 | */ |
Peter Xu | 9419069 | 2018-05-02 18:47:34 +0800 | [diff] [blame] | 3044 | ret = postcopy_resume_handshake(s); |
| 3045 | if (ret) { |
| 3046 | error_report("%s: handshake failed: %d", __func__, ret); |
| 3047 | return ret; |
| 3048 | } |
Peter Xu | d1b8ead | 2018-05-02 18:47:31 +0800 | [diff] [blame] | 3049 | |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3050 | return 0; |
| 3051 | } |
| 3052 | |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3053 | /* |
| 3054 | * We don't return until we are in a safe state to continue current |
| 3055 | * postcopy migration. Returns MIG_THR_ERR_RECOVERED if recovered, or |
| 3056 | * MIG_THR_ERR_FATAL if unrecovery failure happened. |
| 3057 | */ |
| 3058 | static MigThrError postcopy_pause(MigrationState *s) |
| 3059 | { |
| 3060 | assert(s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3061 | |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3062 | while (true) { |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 3063 | QEMUFile *file; |
| 3064 | |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3065 | migrate_set_state(&s->state, s->state, |
| 3066 | MIGRATION_STATUS_POSTCOPY_PAUSED); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3067 | |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3068 | /* Current channel is possibly broken. Release it. */ |
| 3069 | assert(s->to_dst_file); |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 3070 | qemu_mutex_lock(&s->qemu_file_lock); |
| 3071 | file = s->to_dst_file; |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3072 | s->to_dst_file = NULL; |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 3073 | qemu_mutex_unlock(&s->qemu_file_lock); |
| 3074 | |
| 3075 | qemu_file_shutdown(file); |
| 3076 | qemu_fclose(file); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3077 | |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3078 | error_report("Detected IO failure for postcopy. " |
| 3079 | "Migration paused."); |
| 3080 | |
| 3081 | /* |
| 3082 | * We wait until things fixed up. Then someone will setup the |
| 3083 | * status back for us. |
| 3084 | */ |
| 3085 | while (s->state == MIGRATION_STATUS_POSTCOPY_PAUSED) { |
| 3086 | qemu_sem_wait(&s->postcopy_pause_sem); |
| 3087 | } |
| 3088 | |
| 3089 | if (s->state == MIGRATION_STATUS_POSTCOPY_RECOVER) { |
| 3090 | /* Woken up by a recover procedure. Give it a shot */ |
| 3091 | |
| 3092 | /* |
| 3093 | * Firstly, let's wake up the return path now, with a new |
| 3094 | * return path channel. |
| 3095 | */ |
| 3096 | qemu_sem_post(&s->postcopy_pause_rp_sem); |
| 3097 | |
| 3098 | /* Do the resume logic */ |
| 3099 | if (postcopy_do_resume(s) == 0) { |
| 3100 | /* Let's continue! */ |
| 3101 | trace_postcopy_pause_continued(); |
| 3102 | return MIG_THR_ERR_RECOVERED; |
| 3103 | } else { |
| 3104 | /* |
| 3105 | * Something wrong happened during the recovery, let's |
| 3106 | * pause again. Pause is always better than throwing |
| 3107 | * data away. |
| 3108 | */ |
| 3109 | continue; |
| 3110 | } |
| 3111 | } else { |
| 3112 | /* This is not right... Time to quit. */ |
| 3113 | return MIG_THR_ERR_FATAL; |
| 3114 | } |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3115 | } |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | static MigThrError migration_detect_error(MigrationState *s) |
| 3119 | { |
| 3120 | int ret; |
Dr. David Alan Gilbert | c3c5eae | 2019-02-19 19:59:28 +0000 | [diff] [blame] | 3121 | int state = s->state; |
Yury Kotov | 3d661c8 | 2019-04-22 13:34:20 +0300 | [diff] [blame] | 3122 | Error *local_error = NULL; |
Dr. David Alan Gilbert | c3c5eae | 2019-02-19 19:59:28 +0000 | [diff] [blame] | 3123 | |
| 3124 | if (state == MIGRATION_STATUS_CANCELLING || |
| 3125 | state == MIGRATION_STATUS_CANCELLED) { |
| 3126 | /* End the migration, but don't set the state to failed */ |
| 3127 | return MIG_THR_ERR_FATAL; |
| 3128 | } |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3129 | |
| 3130 | /* Try to detect any file errors */ |
Yury Kotov | 3d661c8 | 2019-04-22 13:34:20 +0300 | [diff] [blame] | 3131 | ret = qemu_file_get_error_obj(s->to_dst_file, &local_error); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3132 | if (!ret) { |
| 3133 | /* Everything is fine */ |
Yury Kotov | 3d661c8 | 2019-04-22 13:34:20 +0300 | [diff] [blame] | 3134 | assert(!local_error); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3135 | return MIG_THR_ERR_NONE; |
| 3136 | } |
| 3137 | |
Yury Kotov | 3d661c8 | 2019-04-22 13:34:20 +0300 | [diff] [blame] | 3138 | if (local_error) { |
| 3139 | migrate_set_error(s, local_error); |
| 3140 | error_free(local_error); |
| 3141 | } |
| 3142 | |
Dr. David Alan Gilbert | c3c5eae | 2019-02-19 19:59:28 +0000 | [diff] [blame] | 3143 | if (state == MIGRATION_STATUS_POSTCOPY_ACTIVE && ret == -EIO) { |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3144 | /* |
| 3145 | * For postcopy, we allow the network to be down for a |
| 3146 | * while. After that, it can be continued by a |
| 3147 | * recovery phase. |
| 3148 | */ |
| 3149 | return postcopy_pause(s); |
| 3150 | } else { |
| 3151 | /* |
| 3152 | * For precopy (or postcopy with error outside IO), we fail |
| 3153 | * with no time. |
| 3154 | */ |
Dr. David Alan Gilbert | c3c5eae | 2019-02-19 19:59:28 +0000 | [diff] [blame] | 3155 | migrate_set_state(&s->state, state, MIGRATION_STATUS_FAILED); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3156 | trace_migration_thread_file_err(); |
| 3157 | |
| 3158 | /* Time to stop the migration, now. */ |
| 3159 | return MIG_THR_ERR_FATAL; |
| 3160 | } |
| 3161 | } |
| 3162 | |
Wei Yang | 268dcd4 | 2019-09-12 10:49:57 +0800 | [diff] [blame] | 3163 | /* How many bytes have we transferred since the beginning of the migration */ |
Juan Quintela | 0c8f0ef | 2018-06-26 15:38:00 +0200 | [diff] [blame] | 3164 | static uint64_t migration_total_bytes(MigrationState *s) |
| 3165 | { |
Juan Quintela | b9ee2f7 | 2016-01-15 11:40:13 +0100 | [diff] [blame] | 3166 | return qemu_ftell(s->to_dst_file) + ram_counters.multifd_bytes; |
Juan Quintela | 0c8f0ef | 2018-06-26 15:38:00 +0200 | [diff] [blame] | 3167 | } |
| 3168 | |
Peter Xu | cf011f0 | 2018-01-03 20:20:11 +0800 | [diff] [blame] | 3169 | static void migration_calculate_complete(MigrationState *s) |
| 3170 | { |
Juan Quintela | 0c8f0ef | 2018-06-26 15:38:00 +0200 | [diff] [blame] | 3171 | uint64_t bytes = migration_total_bytes(s); |
Peter Xu | cf011f0 | 2018-01-03 20:20:11 +0800 | [diff] [blame] | 3172 | int64_t end_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
Juan Quintela | 6cde6fb | 2018-06-26 15:26:35 +0200 | [diff] [blame] | 3173 | int64_t transfer_time; |
Peter Xu | cf011f0 | 2018-01-03 20:20:11 +0800 | [diff] [blame] | 3174 | |
| 3175 | s->total_time = end_time - s->start_time; |
| 3176 | if (!s->downtime) { |
| 3177 | /* |
| 3178 | * It's still not set, so we are precopy migration. For |
| 3179 | * postcopy, downtime is calculated during postcopy_start(). |
| 3180 | */ |
| 3181 | s->downtime = end_time - s->downtime_start; |
| 3182 | } |
| 3183 | |
Juan Quintela | 6cde6fb | 2018-06-26 15:26:35 +0200 | [diff] [blame] | 3184 | transfer_time = s->total_time - s->setup_time; |
| 3185 | if (transfer_time) { |
| 3186 | s->mbps = ((double) bytes * 8.0) / transfer_time / 1000; |
Peter Xu | cf011f0 | 2018-01-03 20:20:11 +0800 | [diff] [blame] | 3187 | } |
| 3188 | } |
| 3189 | |
Ivan Ren | 87f3bd8 | 2019-08-02 18:18:41 +0800 | [diff] [blame] | 3190 | static void update_iteration_initial_status(MigrationState *s) |
| 3191 | { |
| 3192 | /* |
| 3193 | * Update these three fields at the same time to avoid mismatch info lead |
| 3194 | * wrong speed calculation. |
| 3195 | */ |
| 3196 | s->iteration_start_time = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 3197 | s->iteration_initial_bytes = migration_total_bytes(s); |
| 3198 | s->iteration_initial_pages = ram_get_total_transferred_pages(); |
| 3199 | } |
| 3200 | |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3201 | static void migration_update_counters(MigrationState *s, |
| 3202 | int64_t current_time) |
| 3203 | { |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 3204 | uint64_t transferred, transferred_pages, time_spent; |
Juan Quintela | 0c8f0ef | 2018-06-26 15:38:00 +0200 | [diff] [blame] | 3205 | uint64_t current_bytes; /* bytes transferred since the beginning */ |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3206 | double bandwidth; |
| 3207 | |
| 3208 | if (current_time < s->iteration_start_time + BUFFER_DELAY) { |
| 3209 | return; |
| 3210 | } |
| 3211 | |
Juan Quintela | 0c8f0ef | 2018-06-26 15:38:00 +0200 | [diff] [blame] | 3212 | current_bytes = migration_total_bytes(s); |
| 3213 | transferred = current_bytes - s->iteration_initial_bytes; |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3214 | time_spent = current_time - s->iteration_start_time; |
| 3215 | bandwidth = (double)transferred / time_spent; |
Wei Wang | 0781c1e | 2018-01-22 19:36:39 +0800 | [diff] [blame] | 3216 | s->threshold_size = bandwidth * s->parameters.downtime_limit; |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3217 | |
| 3218 | s->mbps = (((double) transferred * 8.0) / |
| 3219 | ((double) time_spent / 1000.0)) / 1000.0 / 1000.0; |
| 3220 | |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 3221 | transferred_pages = ram_get_total_transferred_pages() - |
| 3222 | s->iteration_initial_pages; |
| 3223 | s->pages_per_second = (double) transferred_pages / |
| 3224 | (((double) time_spent / 1000.0)); |
| 3225 | |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3226 | /* |
| 3227 | * if we haven't sent anything, we don't want to |
| 3228 | * recalculate. 10000 is a small enough number for our purposes |
| 3229 | */ |
| 3230 | if (ram_counters.dirty_pages_rate && transferred > 10000) { |
Balamuruhan S | 650af89 | 2018-06-12 14:20:09 +0530 | [diff] [blame] | 3231 | s->expected_downtime = ram_counters.remaining / bandwidth; |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | qemu_file_reset_rate_limit(s->to_dst_file); |
| 3235 | |
Ivan Ren | 87f3bd8 | 2019-08-02 18:18:41 +0800 | [diff] [blame] | 3236 | update_iteration_initial_status(s); |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3237 | |
| 3238 | trace_migrate_transferred(transferred, time_spent, |
Wei Wang | 0781c1e | 2018-01-22 19:36:39 +0800 | [diff] [blame] | 3239 | bandwidth, s->threshold_size); |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3240 | } |
| 3241 | |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3242 | /* Migration thread iteration status */ |
| 3243 | typedef enum { |
| 3244 | MIG_ITERATE_RESUME, /* Resume current iteration */ |
| 3245 | MIG_ITERATE_SKIP, /* Skip current iteration */ |
| 3246 | MIG_ITERATE_BREAK, /* Break the loop */ |
| 3247 | } MigIterateState; |
| 3248 | |
| 3249 | /* |
| 3250 | * Return true if continue to the next iteration directly, false |
| 3251 | * otherwise. |
| 3252 | */ |
| 3253 | static MigIterateState migration_iteration_run(MigrationState *s) |
| 3254 | { |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 3255 | uint64_t pending_size, pend_pre, pend_compat, pend_post; |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3256 | bool in_postcopy = s->state == MIGRATION_STATUS_POSTCOPY_ACTIVE; |
| 3257 | |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 3258 | qemu_savevm_state_pending(s->to_dst_file, s->threshold_size, &pend_pre, |
| 3259 | &pend_compat, &pend_post); |
| 3260 | pending_size = pend_pre + pend_compat + pend_post; |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3261 | |
| 3262 | trace_migrate_pending(pending_size, s->threshold_size, |
Vladimir Sementsov-Ogievskiy | 4799502 | 2018-03-13 15:34:00 -0400 | [diff] [blame] | 3263 | pend_pre, pend_compat, pend_post); |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3264 | |
| 3265 | if (pending_size && pending_size >= s->threshold_size) { |
| 3266 | /* Still a significant amount to transfer */ |
Wei Yang | 52aec70 | 2019-07-18 16:37:47 +0800 | [diff] [blame] | 3267 | if (!in_postcopy && pend_pre <= s->threshold_size && |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3268 | atomic_read(&s->start_postcopy)) { |
| 3269 | if (postcopy_start(s)) { |
| 3270 | error_report("%s: postcopy failed to start", __func__); |
| 3271 | } |
| 3272 | return MIG_ITERATE_SKIP; |
| 3273 | } |
| 3274 | /* Just another iteration step */ |
Wei Yang | 17d9351 | 2019-10-06 06:05:16 +0800 | [diff] [blame] | 3275 | qemu_savevm_state_iterate(s->to_dst_file, in_postcopy); |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3276 | } else { |
| 3277 | trace_migration_thread_low_pending(pending_size); |
| 3278 | migration_completion(s); |
| 3279 | return MIG_ITERATE_BREAK; |
| 3280 | } |
| 3281 | |
| 3282 | return MIG_ITERATE_RESUME; |
| 3283 | } |
| 3284 | |
Peter Xu | 199aa6d | 2018-01-03 20:20:15 +0800 | [diff] [blame] | 3285 | static void migration_iteration_finish(MigrationState *s) |
| 3286 | { |
| 3287 | /* If we enabled cpu throttling for auto-converge, turn it off. */ |
| 3288 | cpu_throttle_stop(); |
| 3289 | |
| 3290 | qemu_mutex_lock_iothread(); |
| 3291 | switch (s->state) { |
| 3292 | case MIGRATION_STATUS_COMPLETED: |
| 3293 | migration_calculate_complete(s); |
| 3294 | runstate_set(RUN_STATE_POSTMIGRATE); |
| 3295 | break; |
| 3296 | |
| 3297 | case MIGRATION_STATUS_ACTIVE: |
| 3298 | /* |
| 3299 | * We should really assert here, but since it's during |
| 3300 | * migration, let's try to reduce the usage of assertions. |
| 3301 | */ |
| 3302 | if (!migrate_colo_enabled()) { |
| 3303 | error_report("%s: critical error: calling COLO code without " |
| 3304 | "COLO enabled", __func__); |
| 3305 | } |
| 3306 | migrate_start_colo_process(s); |
| 3307 | /* |
| 3308 | * Fixme: we will run VM in COLO no matter its old running state. |
| 3309 | * After exited COLO, we will keep running. |
| 3310 | */ |
| 3311 | s->vm_was_running = true; |
| 3312 | /* Fallthrough */ |
| 3313 | case MIGRATION_STATUS_FAILED: |
| 3314 | case MIGRATION_STATUS_CANCELLED: |
Dr. David Alan Gilbert | 57225e5 | 2018-07-19 10:22:57 +0100 | [diff] [blame] | 3315 | case MIGRATION_STATUS_CANCELLING: |
Peter Xu | 199aa6d | 2018-01-03 20:20:15 +0800 | [diff] [blame] | 3316 | if (s->vm_was_running) { |
| 3317 | vm_start(); |
| 3318 | } else { |
| 3319 | if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { |
| 3320 | runstate_set(RUN_STATE_POSTMIGRATE); |
| 3321 | } |
| 3322 | } |
| 3323 | break; |
| 3324 | |
| 3325 | default: |
| 3326 | /* Should not reach here, but if so, forgive the VM. */ |
| 3327 | error_report("%s: Unknown ending state %d", __func__, s->state); |
| 3328 | break; |
| 3329 | } |
Yury Kotov | fd392cf | 2019-04-08 14:33:43 +0300 | [diff] [blame] | 3330 | migrate_fd_cleanup_schedule(s); |
Peter Xu | 199aa6d | 2018-01-03 20:20:15 +0800 | [diff] [blame] | 3331 | qemu_mutex_unlock_iothread(); |
| 3332 | } |
| 3333 | |
Dr. David Alan Gilbert | ad767be | 2018-06-13 11:26:41 +0100 | [diff] [blame] | 3334 | void migration_make_urgent_request(void) |
| 3335 | { |
| 3336 | qemu_sem_post(&migrate_get_current()->rate_limit_sem); |
| 3337 | } |
| 3338 | |
| 3339 | void migration_consume_urgent_request(void) |
| 3340 | { |
| 3341 | qemu_sem_wait(&migrate_get_current()->rate_limit_sem); |
| 3342 | } |
| 3343 | |
Dr. David Alan Gilbert | 97e1e06 | 2019-12-05 10:29:18 +0000 | [diff] [blame] | 3344 | /* Returns true if the rate limiting was broken by an urgent request */ |
| 3345 | bool migration_rate_limit(void) |
| 3346 | { |
| 3347 | int64_t now = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
| 3348 | MigrationState *s = migrate_get_current(); |
| 3349 | |
| 3350 | bool urgent = false; |
| 3351 | migration_update_counters(s, now); |
| 3352 | if (qemu_file_rate_limit(s->to_dst_file)) { |
| 3353 | /* |
| 3354 | * Wait for a delay to do rate limiting OR |
| 3355 | * something urgent to post the semaphore. |
| 3356 | */ |
| 3357 | int ms = s->iteration_start_time + BUFFER_DELAY - now; |
| 3358 | trace_migration_rate_limit_pre(ms); |
| 3359 | if (qemu_sem_timedwait(&s->rate_limit_sem, ms) == 0) { |
| 3360 | /* |
| 3361 | * We were woken by one or more urgent things but |
| 3362 | * the timedwait will have consumed one of them. |
| 3363 | * The service routine for the urgent wake will dec |
| 3364 | * the semaphore itself for each item it consumes, |
| 3365 | * so add this one we just eat back. |
| 3366 | */ |
| 3367 | qemu_sem_post(&s->rate_limit_sem); |
| 3368 | urgent = true; |
| 3369 | } |
| 3370 | trace_migration_rate_limit_post(urgent); |
| 3371 | } |
| 3372 | return urgent; |
| 3373 | } |
| 3374 | |
Dr. David Alan Gilbert | 70b2047 | 2015-11-05 18:10:49 +0000 | [diff] [blame] | 3375 | /* |
| 3376 | * Master migration thread on the source VM. |
| 3377 | * It drives the migration and pumps the data down the outgoing channel. |
| 3378 | */ |
Juan Quintela | 5f496a1 | 2013-02-22 17:36:30 +0100 | [diff] [blame] | 3379 | static void *migration_thread(void *opaque) |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 3380 | { |
Juan Quintela | 9848a40 | 2012-12-19 09:55:50 +0100 | [diff] [blame] | 3381 | MigrationState *s = opaque; |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 3382 | int64_t setup_start = qemu_clock_get_ms(QEMU_CLOCK_HOST); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3383 | MigThrError thr_error; |
Dr. David Alan Gilbert | ad767be | 2018-06-13 11:26:41 +0100 | [diff] [blame] | 3384 | bool urgent = false; |
Juan Quintela | 76f5933 | 2012-10-03 20:16:24 +0200 | [diff] [blame] | 3385 | |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 3386 | rcu_register_thread(); |
| 3387 | |
Dr. David Alan Gilbert | 892ae71 | 2019-02-27 16:49:00 +0000 | [diff] [blame] | 3388 | object_ref(OBJECT(s)); |
Ivan Ren | 87f3bd8 | 2019-08-02 18:18:41 +0800 | [diff] [blame] | 3389 | update_iteration_initial_status(s); |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3390 | |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 3391 | qemu_savevm_state_header(s->to_dst_file); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3392 | |
Peter Xu | 62a0265 | 2017-06-14 15:55:58 +0800 | [diff] [blame] | 3393 | /* |
| 3394 | * If we opened the return path, we need to make sure dst has it |
| 3395 | * opened as well. |
| 3396 | */ |
| 3397 | if (s->rp_state.from_dst_file) { |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3398 | /* Now tell the dest that it should open its end so it can reply */ |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 3399 | qemu_savevm_send_open_return_path(s->to_dst_file); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3400 | |
| 3401 | /* And do a ping that will make stuff easier to debug */ |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 3402 | qemu_savevm_send_ping(s->to_dst_file, 1); |
Peter Xu | 0425dc9 | 2017-05-31 18:35:34 +0800 | [diff] [blame] | 3403 | } |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3404 | |
Vladimir Sementsov-Ogievskiy | 58110f0 | 2017-07-10 19:30:16 +0300 | [diff] [blame] | 3405 | if (migrate_postcopy()) { |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3406 | /* |
| 3407 | * Tell the destination that we *might* want to do postcopy later; |
| 3408 | * if the other end can't do postcopy it should fail now, nice and |
| 3409 | * early. |
| 3410 | */ |
zhanghailiang | 89a02a9 | 2016-01-15 11:37:42 +0800 | [diff] [blame] | 3411 | qemu_savevm_send_postcopy_advise(s->to_dst_file); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Zhang Chen | aad555c | 2018-09-03 12:38:47 +0800 | [diff] [blame] | 3414 | if (migrate_colo_enabled()) { |
| 3415 | /* Notify migration destination that we enable COLO */ |
| 3416 | qemu_savevm_send_colo_enable(s->to_dst_file); |
| 3417 | } |
| 3418 | |
Juan Quintela | 9907e84 | 2017-06-28 11:52:24 +0200 | [diff] [blame] | 3419 | qemu_savevm_state_setup(s->to_dst_file); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 3420 | |
Keqian Zhu | d05de9e | 2020-02-04 13:08:41 +0800 | [diff] [blame] | 3421 | if (qemu_savevm_state_guest_unplug_pending()) { |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 3422 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 3423 | MIGRATION_STATUS_WAIT_UNPLUG); |
| 3424 | |
| 3425 | while (s->state == MIGRATION_STATUS_WAIT_UNPLUG && |
| 3426 | qemu_savevm_state_guest_unplug_pending()) { |
| 3427 | qemu_sem_timedwait(&s->wait_unplug_sem, 250); |
| 3428 | } |
| 3429 | |
| 3430 | migrate_set_state(&s->state, MIGRATION_STATUS_WAIT_UNPLUG, |
| 3431 | MIGRATION_STATUS_ACTIVE); |
| 3432 | } |
| 3433 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 3434 | s->setup_time = qemu_clock_get_ms(QEMU_CLOCK_HOST) - setup_start; |
zhanghailiang | 48781e5 | 2015-12-16 11:47:33 +0000 | [diff] [blame] | 3435 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 3436 | MIGRATION_STATUS_ACTIVE); |
Michael R. Hines | 29ae8a4 | 2013-07-22 10:01:57 -0400 | [diff] [blame] | 3437 | |
Dr. David Alan Gilbert | 9ec055a | 2015-11-05 18:10:58 +0000 | [diff] [blame] | 3438 | trace_migration_thread_setup_complete(); |
| 3439 | |
Wei Yang | 8f8d528 | 2019-07-17 08:53:41 +0800 | [diff] [blame] | 3440 | while (migration_is_active(s)) { |
Dr. David Alan Gilbert | ad767be | 2018-06-13 11:26:41 +0100 | [diff] [blame] | 3441 | if (urgent || !qemu_file_rate_limit(s->to_dst_file)) { |
Peter Xu | 2ad8730 | 2018-01-03 20:20:14 +0800 | [diff] [blame] | 3442 | MigIterateState iter_state = migration_iteration_run(s); |
| 3443 | if (iter_state == MIG_ITERATE_SKIP) { |
| 3444 | continue; |
| 3445 | } else if (iter_state == MIG_ITERATE_BREAK) { |
Dr. David Alan Gilbert | 09f6c85 | 2015-08-13 11:51:31 +0100 | [diff] [blame] | 3446 | break; |
Juan Quintela | c369f40 | 2012-10-03 20:33:34 +0200 | [diff] [blame] | 3447 | } |
| 3448 | } |
Paolo Bonzini | f4410a5 | 2013-02-22 17:36:20 +0100 | [diff] [blame] | 3449 | |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3450 | /* |
| 3451 | * Try to detect any kind of failures, and see whether we |
| 3452 | * should stop the migration now. |
| 3453 | */ |
| 3454 | thr_error = migration_detect_error(s); |
| 3455 | if (thr_error == MIG_THR_ERR_FATAL) { |
| 3456 | /* Stop migration */ |
Paolo Bonzini | fd45ee2 | 2013-02-22 17:36:33 +0100 | [diff] [blame] | 3457 | break; |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3458 | } else if (thr_error == MIG_THR_ERR_RECOVERED) { |
| 3459 | /* |
| 3460 | * Just recovered from a e.g. network failure, reset all |
| 3461 | * the local variables. This is important to avoid |
| 3462 | * breaking transferred_bytes and bandwidth calculation |
| 3463 | */ |
Ivan Ren | 87f3bd8 | 2019-08-02 18:18:41 +0800 | [diff] [blame] | 3464 | update_iteration_initial_status(s); |
Paolo Bonzini | fd45ee2 | 2013-02-22 17:36:33 +0100 | [diff] [blame] | 3465 | } |
Peter Xu | b15df1a | 2018-01-03 20:20:13 +0800 | [diff] [blame] | 3466 | |
Dr. David Alan Gilbert | 97e1e06 | 2019-12-05 10:29:18 +0000 | [diff] [blame] | 3467 | urgent = migration_rate_limit(); |
Paolo Bonzini | a3fa1d7 | 2013-02-22 17:36:18 +0100 | [diff] [blame] | 3468 | } |
| 3469 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3470 | trace_migration_thread_after_loop(); |
Peter Xu | 199aa6d | 2018-01-03 20:20:15 +0800 | [diff] [blame] | 3471 | migration_iteration_finish(s); |
Dr. David Alan Gilbert | 892ae71 | 2019-02-27 16:49:00 +0000 | [diff] [blame] | 3472 | object_unref(OBJECT(s)); |
Paolo Bonzini | ab28bd2 | 2015-07-09 08:55:38 +0200 | [diff] [blame] | 3473 | rcu_unregister_thread(); |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 3474 | return NULL; |
| 3475 | } |
| 3476 | |
Dr. David Alan Gilbert | cce8040 | 2017-12-15 17:16:54 +0000 | [diff] [blame] | 3477 | void migrate_fd_connect(MigrationState *s, Error *error_in) |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 3478 | { |
Juan Quintela | 00f4b57 | 2019-06-12 11:33:27 +0200 | [diff] [blame] | 3479 | Error *local_err = NULL; |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3480 | int64_t rate_limit; |
| 3481 | bool resume = s->state == MIGRATION_STATUS_POSTCOPY_PAUSED; |
| 3482 | |
Ashijeet Acharya | 2ff3025 | 2016-09-15 21:50:28 +0530 | [diff] [blame] | 3483 | s->expected_downtime = s->parameters.downtime_limit; |
Marc-André Lureau | 9cbc364 | 2020-03-25 19:47:21 +0100 | [diff] [blame] | 3484 | if (resume) { |
| 3485 | assert(s->cleanup_bh); |
| 3486 | } else { |
| 3487 | assert(!s->cleanup_bh); |
| 3488 | s->cleanup_bh = qemu_bh_new(migrate_fd_cleanup_bh, s); |
| 3489 | } |
Dr. David Alan Gilbert | cce8040 | 2017-12-15 17:16:54 +0000 | [diff] [blame] | 3490 | if (error_in) { |
| 3491 | migrate_fd_error(s, error_in); |
| 3492 | migrate_fd_cleanup(s); |
| 3493 | return; |
| 3494 | } |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 3495 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3496 | if (resume) { |
| 3497 | /* This is a resumed migration */ |
Peter Xu | 8504dde | 2019-09-06 21:01:03 +0800 | [diff] [blame] | 3498 | rate_limit = s->parameters.max_postcopy_bandwidth / |
| 3499 | XFER_LIMIT_RATIO; |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3500 | } else { |
| 3501 | /* This is a fresh new migration */ |
| 3502 | rate_limit = s->parameters.max_bandwidth / XFER_LIMIT_RATIO; |
Paolo Bonzini | 442773c | 2013-02-22 17:36:44 +0100 | [diff] [blame] | 3503 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3504 | /* Notify before starting migration thread */ |
| 3505 | notifier_list_notify(&migration_state_notifiers, s); |
| 3506 | } |
| 3507 | |
| 3508 | qemu_file_set_rate_limit(s->to_dst_file, rate_limit); |
| 3509 | qemu_file_set_blocking(s->to_dst_file, true); |
Stefan Hajnoczi | 9287ac2 | 2013-07-29 15:01:57 +0200 | [diff] [blame] | 3510 | |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3511 | /* |
Peter Xu | c788ada | 2017-06-26 18:28:55 +0800 | [diff] [blame] | 3512 | * Open the return path. For postcopy, it is used exclusively. For |
| 3513 | * precopy, only if user specified "return-path" capability would |
| 3514 | * QEMU uses the return path. |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3515 | */ |
Peter Xu | c788ada | 2017-06-26 18:28:55 +0800 | [diff] [blame] | 3516 | if (migrate_postcopy_ram() || migrate_use_return_path()) { |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3517 | if (open_return_path_on_source(s, !resume)) { |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3518 | error_report("Unable to open return-path for postcopy"); |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3519 | migrate_set_state(&s->state, s->state, MIGRATION_STATUS_FAILED); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3520 | migrate_fd_cleanup(s); |
| 3521 | return; |
| 3522 | } |
| 3523 | } |
| 3524 | |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3525 | if (resume) { |
Peter Xu | 135b87b | 2018-05-02 18:47:25 +0800 | [diff] [blame] | 3526 | /* Wakeup the main migration thread to do the recovery */ |
| 3527 | migrate_set_state(&s->state, MIGRATION_STATUS_POSTCOPY_PAUSED, |
| 3528 | MIGRATION_STATUS_POSTCOPY_RECOVER); |
| 3529 | qemu_sem_post(&s->postcopy_pause_sem); |
Peter Xu | d3e35b8 | 2018-05-02 18:47:24 +0800 | [diff] [blame] | 3530 | return; |
| 3531 | } |
| 3532 | |
Juan Quintela | 00f4b57 | 2019-06-12 11:33:27 +0200 | [diff] [blame] | 3533 | if (multifd_save_setup(&local_err) != 0) { |
| 3534 | error_report_err(local_err); |
Juan Quintela | f986c3d | 2016-01-14 16:52:55 +0100 | [diff] [blame] | 3535 | migrate_set_state(&s->state, MIGRATION_STATUS_SETUP, |
| 3536 | MIGRATION_STATUS_FAILED); |
| 3537 | migrate_fd_cleanup(s); |
| 3538 | return; |
| 3539 | } |
Pankaj Gupta | 009fad7 | 2017-01-23 19:12:56 +0530 | [diff] [blame] | 3540 | qemu_thread_create(&s->thread, "live_migration", migration_thread, s, |
Paolo Bonzini | bb1fadc | 2013-02-22 17:36:21 +0100 | [diff] [blame] | 3541 | QEMU_THREAD_JOINABLE); |
Dr. David Alan Gilbert | 1d34e4b | 2015-11-05 18:11:05 +0000 | [diff] [blame] | 3542 | s->migration_thread_running = true; |
Juan Quintela | 0d82d0e | 2012-10-03 14:18:33 +0200 | [diff] [blame] | 3543 | } |
Dr. David Alan Gilbert | 093e3c4 | 2015-11-05 18:10:52 +0000 | [diff] [blame] | 3544 | |
Peter Xu | 9d18af9 | 2017-06-27 12:10:19 +0800 | [diff] [blame] | 3545 | void migration_global_dump(Monitor *mon) |
| 3546 | { |
| 3547 | MigrationState *ms = migrate_get_current(); |
| 3548 | |
Juan Quintela | 6f0f642 | 2017-10-26 11:49:57 +0200 | [diff] [blame] | 3549 | monitor_printf(mon, "globals:\n"); |
| 3550 | monitor_printf(mon, "store-global-state: %s\n", |
| 3551 | ms->store_global_state ? "on" : "off"); |
| 3552 | monitor_printf(mon, "only-migratable: %s\n", |
Markus Armbruster | 811f865 | 2019-04-01 11:08:24 +0200 | [diff] [blame] | 3553 | only_migratable ? "on" : "off"); |
Juan Quintela | 6f0f642 | 2017-10-26 11:49:57 +0200 | [diff] [blame] | 3554 | monitor_printf(mon, "send-configuration: %s\n", |
| 3555 | ms->send_configuration ? "on" : "off"); |
| 3556 | monitor_printf(mon, "send-section-footer: %s\n", |
| 3557 | ms->send_section_footer ? "on" : "off"); |
Xiao Guangrong | f548222 | 2018-05-03 16:06:11 +0800 | [diff] [blame] | 3558 | monitor_printf(mon, "decompress-error-check: %s\n", |
| 3559 | ms->decompress_error_check ? "on" : "off"); |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 3560 | monitor_printf(mon, "clear-bitmap-shift: %u\n", |
| 3561 | ms->clear_bitmap_shift); |
Peter Xu | 9d18af9 | 2017-06-27 12:10:19 +0800 | [diff] [blame] | 3562 | } |
| 3563 | |
Peter Xu | 2081475 | 2017-07-18 11:39:03 +0800 | [diff] [blame] | 3564 | #define DEFINE_PROP_MIG_CAP(name, x) \ |
| 3565 | DEFINE_PROP_BOOL(name, MigrationState, enabled_capabilities[x], false) |
| 3566 | |
Peter Xu | 5272298 | 2017-06-27 12:10:14 +0800 | [diff] [blame] | 3567 | static Property migration_properties[] = { |
| 3568 | DEFINE_PROP_BOOL("store-global-state", MigrationState, |
| 3569 | store_global_state, true), |
Peter Xu | 71dd4c1 | 2017-06-27 12:10:16 +0800 | [diff] [blame] | 3570 | DEFINE_PROP_BOOL("send-configuration", MigrationState, |
| 3571 | send_configuration, true), |
Peter Xu | 15c3850 | 2017-06-27 12:10:17 +0800 | [diff] [blame] | 3572 | DEFINE_PROP_BOOL("send-section-footer", MigrationState, |
| 3573 | send_section_footer, true), |
Xiao Guangrong | f548222 | 2018-05-03 16:06:11 +0800 | [diff] [blame] | 3574 | DEFINE_PROP_BOOL("decompress-error-check", MigrationState, |
| 3575 | decompress_error_check, true), |
Peter Xu | 002cad6 | 2019-06-03 14:50:56 +0800 | [diff] [blame] | 3576 | DEFINE_PROP_UINT8("x-clear-bitmap-shift", MigrationState, |
| 3577 | clear_bitmap_shift, CLEAR_BITMAP_SHIFT_DEFAULT), |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3578 | |
| 3579 | /* Migration parameters */ |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3580 | DEFINE_PROP_UINT8("x-compress-level", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3581 | parameters.compress_level, |
| 3582 | DEFAULT_MIGRATE_COMPRESS_LEVEL), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3583 | DEFINE_PROP_UINT8("x-compress-threads", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3584 | parameters.compress_threads, |
| 3585 | DEFAULT_MIGRATE_COMPRESS_THREAD_COUNT), |
Xiao Guangrong | 1d58872 | 2018-08-21 16:10:20 +0800 | [diff] [blame] | 3586 | DEFINE_PROP_BOOL("x-compress-wait-thread", MigrationState, |
| 3587 | parameters.compress_wait_thread, true), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3588 | DEFINE_PROP_UINT8("x-decompress-threads", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3589 | parameters.decompress_threads, |
| 3590 | DEFAULT_MIGRATE_DECOMPRESS_THREAD_COUNT), |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 3591 | DEFINE_PROP_UINT8("x-throttle-trigger-threshold", MigrationState, |
| 3592 | parameters.throttle_trigger_threshold, |
| 3593 | DEFAULT_MIGRATE_THROTTLE_TRIGGER_THRESHOLD), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3594 | DEFINE_PROP_UINT8("x-cpu-throttle-initial", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3595 | parameters.cpu_throttle_initial, |
| 3596 | DEFAULT_MIGRATE_CPU_THROTTLE_INITIAL), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3597 | DEFINE_PROP_UINT8("x-cpu-throttle-increment", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3598 | parameters.cpu_throttle_increment, |
| 3599 | DEFAULT_MIGRATE_CPU_THROTTLE_INCREMENT), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3600 | DEFINE_PROP_SIZE("x-max-bandwidth", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3601 | parameters.max_bandwidth, MAX_THROTTLE), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3602 | DEFINE_PROP_UINT64("x-downtime-limit", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3603 | parameters.downtime_limit, |
| 3604 | DEFAULT_MIGRATE_SET_DOWNTIME), |
Juan Quintela | 741d408 | 2017-12-01 13:08:38 +0100 | [diff] [blame] | 3605 | DEFINE_PROP_UINT32("x-checkpoint-delay", MigrationState, |
Peter Xu | 89632fa | 2017-07-18 11:39:02 +0800 | [diff] [blame] | 3606 | parameters.x_checkpoint_delay, |
| 3607 | DEFAULT_MIGRATE_X_CHECKPOINT_DELAY), |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 3608 | DEFINE_PROP_UINT8("multifd-channels", MigrationState, |
| 3609 | parameters.multifd_channels, |
Juan Quintela | 4075fb1 | 2016-01-15 08:56:17 +0100 | [diff] [blame] | 3610 | DEFAULT_MIGRATE_MULTIFD_CHANNELS), |
Juan Quintela | 96eef04 | 2019-01-16 10:35:55 +0100 | [diff] [blame] | 3611 | DEFINE_PROP_MULTIFD_COMPRESSION("multifd-compression", MigrationState, |
| 3612 | parameters.multifd_compression, |
| 3613 | DEFAULT_MIGRATE_MULTIFD_COMPRESSION), |
Juan Quintela | 9004db4 | 2020-01-23 17:08:52 +0100 | [diff] [blame] | 3614 | DEFINE_PROP_UINT8("multifd-zlib-level", MigrationState, |
| 3615 | parameters.multifd_zlib_level, |
| 3616 | DEFAULT_MIGRATE_MULTIFD_ZLIB_LEVEL), |
Juan Quintela | 6a9ad15 | 2020-01-23 17:41:36 +0100 | [diff] [blame] | 3617 | DEFINE_PROP_UINT8("multifd-zstd-level", MigrationState, |
| 3618 | parameters.multifd_zstd_level, |
| 3619 | DEFAULT_MIGRATE_MULTIFD_ZSTD_LEVEL), |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 3620 | DEFINE_PROP_SIZE("xbzrle-cache-size", MigrationState, |
| 3621 | parameters.xbzrle_cache_size, |
| 3622 | DEFAULT_MIGRATE_XBZRLE_CACHE_SIZE), |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 3623 | DEFINE_PROP_SIZE("max-postcopy-bandwidth", MigrationState, |
| 3624 | parameters.max_postcopy_bandwidth, |
| 3625 | DEFAULT_MIGRATE_MAX_POSTCOPY_BANDWIDTH), |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 3626 | DEFINE_PROP_UINT8("max-cpu-throttle", MigrationState, |
| 3627 | parameters.max_cpu_throttle, |
| 3628 | DEFAULT_MIGRATE_MAX_CPU_THROTTLE), |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 3629 | DEFINE_PROP_SIZE("announce-initial", MigrationState, |
| 3630 | parameters.announce_initial, |
| 3631 | DEFAULT_MIGRATE_ANNOUNCE_INITIAL), |
| 3632 | DEFINE_PROP_SIZE("announce-max", MigrationState, |
| 3633 | parameters.announce_max, |
| 3634 | DEFAULT_MIGRATE_ANNOUNCE_MAX), |
| 3635 | DEFINE_PROP_SIZE("announce-rounds", MigrationState, |
| 3636 | parameters.announce_rounds, |
| 3637 | DEFAULT_MIGRATE_ANNOUNCE_ROUNDS), |
| 3638 | DEFINE_PROP_SIZE("announce-step", MigrationState, |
| 3639 | parameters.announce_step, |
| 3640 | DEFAULT_MIGRATE_ANNOUNCE_STEP), |
Peter Xu | 2081475 | 2017-07-18 11:39:03 +0800 | [diff] [blame] | 3641 | |
| 3642 | /* Migration capabilities */ |
| 3643 | DEFINE_PROP_MIG_CAP("x-xbzrle", MIGRATION_CAPABILITY_XBZRLE), |
| 3644 | DEFINE_PROP_MIG_CAP("x-rdma-pin-all", MIGRATION_CAPABILITY_RDMA_PIN_ALL), |
| 3645 | DEFINE_PROP_MIG_CAP("x-auto-converge", MIGRATION_CAPABILITY_AUTO_CONVERGE), |
| 3646 | DEFINE_PROP_MIG_CAP("x-zero-blocks", MIGRATION_CAPABILITY_ZERO_BLOCKS), |
| 3647 | DEFINE_PROP_MIG_CAP("x-compress", MIGRATION_CAPABILITY_COMPRESS), |
| 3648 | DEFINE_PROP_MIG_CAP("x-events", MIGRATION_CAPABILITY_EVENTS), |
| 3649 | DEFINE_PROP_MIG_CAP("x-postcopy-ram", MIGRATION_CAPABILITY_POSTCOPY_RAM), |
| 3650 | DEFINE_PROP_MIG_CAP("x-colo", MIGRATION_CAPABILITY_X_COLO), |
| 3651 | DEFINE_PROP_MIG_CAP("x-release-ram", MIGRATION_CAPABILITY_RELEASE_RAM), |
| 3652 | DEFINE_PROP_MIG_CAP("x-block", MIGRATION_CAPABILITY_BLOCK), |
| 3653 | DEFINE_PROP_MIG_CAP("x-return-path", MIGRATION_CAPABILITY_RETURN_PATH), |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 3654 | DEFINE_PROP_MIG_CAP("x-multifd", MIGRATION_CAPABILITY_MULTIFD), |
Peter Xu | 2081475 | 2017-07-18 11:39:03 +0800 | [diff] [blame] | 3655 | |
Peter Xu | 5272298 | 2017-06-27 12:10:14 +0800 | [diff] [blame] | 3656 | DEFINE_PROP_END_OF_LIST(), |
| 3657 | }; |
| 3658 | |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3659 | static void migration_class_init(ObjectClass *klass, void *data) |
| 3660 | { |
| 3661 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 3662 | |
| 3663 | dc->user_creatable = false; |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 3664 | device_class_set_props(dc, migration_properties); |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3665 | } |
| 3666 | |
Marc-André Lureau | b91bf5e | 2017-08-01 17:04:18 +0100 | [diff] [blame] | 3667 | static void migration_instance_finalize(Object *obj) |
| 3668 | { |
| 3669 | MigrationState *ms = MIGRATION_OBJ(obj); |
| 3670 | MigrationParameters *params = &ms->parameters; |
| 3671 | |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 3672 | qemu_mutex_destroy(&ms->error_mutex); |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 3673 | qemu_mutex_destroy(&ms->qemu_file_lock); |
Marc-André Lureau | b91bf5e | 2017-08-01 17:04:18 +0100 | [diff] [blame] | 3674 | g_free(params->tls_hostname); |
| 3675 | g_free(params->tls_creds); |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 3676 | qemu_sem_destroy(&ms->wait_unplug_sem); |
Dr. David Alan Gilbert | ad767be | 2018-06-13 11:26:41 +0100 | [diff] [blame] | 3677 | qemu_sem_destroy(&ms->rate_limit_sem); |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 3678 | qemu_sem_destroy(&ms->pause_sem); |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3679 | qemu_sem_destroy(&ms->postcopy_pause_sem); |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 3680 | qemu_sem_destroy(&ms->postcopy_pause_rp_sem); |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3681 | qemu_sem_destroy(&ms->rp_state.rp_sem); |
Marc-André Lureau | ab105cc | 2018-03-06 18:09:59 +0100 | [diff] [blame] | 3682 | error_free(ms->error); |
Marc-André Lureau | b91bf5e | 2017-08-01 17:04:18 +0100 | [diff] [blame] | 3683 | } |
| 3684 | |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3685 | static void migration_instance_init(Object *obj) |
| 3686 | { |
| 3687 | MigrationState *ms = MIGRATION_OBJ(obj); |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 3688 | MigrationParameters *params = &ms->parameters; |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3689 | |
| 3690 | ms->state = MIGRATION_STATUS_NONE; |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3691 | ms->mbps = -1; |
Xiao Guangrong | aecbfe9 | 2019-01-11 14:37:30 +0800 | [diff] [blame] | 3692 | ms->pages_per_second = -1; |
Dr. David Alan Gilbert | e91d895 | 2017-10-20 10:05:52 +0100 | [diff] [blame] | 3693 | qemu_sem_init(&ms->pause_sem, 0); |
Juan Quintela | 87db1a7 | 2017-09-05 12:50:22 +0200 | [diff] [blame] | 3694 | qemu_mutex_init(&ms->error_mutex); |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 3695 | |
| 3696 | params->tls_hostname = g_strdup(""); |
| 3697 | params->tls_creds = g_strdup(""); |
| 3698 | |
| 3699 | /* Set has_* up only for parameter checks */ |
| 3700 | params->has_compress_level = true; |
| 3701 | params->has_compress_threads = true; |
| 3702 | params->has_decompress_threads = true; |
Keqian Zhu | dc14a47 | 2020-02-24 10:31:42 +0800 | [diff] [blame] | 3703 | params->has_throttle_trigger_threshold = true; |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 3704 | params->has_cpu_throttle_initial = true; |
| 3705 | params->has_cpu_throttle_increment = true; |
| 3706 | params->has_max_bandwidth = true; |
| 3707 | params->has_downtime_limit = true; |
| 3708 | params->has_x_checkpoint_delay = true; |
| 3709 | params->has_block_incremental = true; |
Juan Quintela | cbfd6c9 | 2019-02-06 13:54:06 +0100 | [diff] [blame] | 3710 | params->has_multifd_channels = true; |
Juan Quintela | 96eef04 | 2019-01-16 10:35:55 +0100 | [diff] [blame] | 3711 | params->has_multifd_compression = true; |
Juan Quintela | 9004db4 | 2020-01-23 17:08:52 +0100 | [diff] [blame] | 3712 | params->has_multifd_zlib_level = true; |
Juan Quintela | 6a9ad15 | 2020-01-23 17:41:36 +0100 | [diff] [blame] | 3713 | params->has_multifd_zstd_level = true; |
Juan Quintela | 73af8dd | 2017-10-05 21:30:10 +0200 | [diff] [blame] | 3714 | params->has_xbzrle_cache_size = true; |
Dr. David Alan Gilbert | 7e555c6 | 2018-06-13 11:26:40 +0100 | [diff] [blame] | 3715 | params->has_max_postcopy_bandwidth = true; |
Li Qiang | 4cbc9c7 | 2018-08-01 06:00:20 -0700 | [diff] [blame] | 3716 | params->has_max_cpu_throttle = true; |
Dr. David Alan Gilbert | ee3d96b | 2019-02-27 13:24:06 +0000 | [diff] [blame] | 3717 | params->has_announce_initial = true; |
| 3718 | params->has_announce_max = true; |
| 3719 | params->has_announce_rounds = true; |
| 3720 | params->has_announce_step = true; |
Peter Xu | b23c2ad | 2018-05-02 18:47:19 +0800 | [diff] [blame] | 3721 | |
| 3722 | qemu_sem_init(&ms->postcopy_pause_sem, 0); |
Peter Xu | 14b1742 | 2018-05-02 18:47:21 +0800 | [diff] [blame] | 3723 | qemu_sem_init(&ms->postcopy_pause_rp_sem, 0); |
Peter Xu | edd090c | 2018-05-02 18:47:32 +0800 | [diff] [blame] | 3724 | qemu_sem_init(&ms->rp_state.rp_sem, 0); |
Dr. David Alan Gilbert | ad767be | 2018-06-13 11:26:41 +0100 | [diff] [blame] | 3725 | qemu_sem_init(&ms->rate_limit_sem, 0); |
Jens Freimann | c7e0acd | 2019-10-29 12:49:02 +0100 | [diff] [blame] | 3726 | qemu_sem_init(&ms->wait_unplug_sem, 0); |
Peter Xu | 62df066 | 2018-05-02 18:47:38 +0800 | [diff] [blame] | 3727 | qemu_mutex_init(&ms->qemu_file_lock); |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 3728 | } |
| 3729 | |
| 3730 | /* |
| 3731 | * Return true if check pass, false otherwise. Error will be put |
| 3732 | * inside errp if provided. |
| 3733 | */ |
| 3734 | static bool migration_object_check(MigrationState *ms, Error **errp) |
| 3735 | { |
Peter Xu | 6b19a7d | 2017-07-18 11:39:10 +0800 | [diff] [blame] | 3736 | MigrationCapabilityStatusList *head = NULL; |
| 3737 | /* Assuming all off */ |
| 3738 | bool cap_list[MIGRATION_CAPABILITY__MAX] = { 0 }, ret; |
| 3739 | int i; |
| 3740 | |
Peter Xu | 8b0b29d | 2017-07-18 11:39:06 +0800 | [diff] [blame] | 3741 | if (!migrate_params_check(&ms->parameters, errp)) { |
| 3742 | return false; |
| 3743 | } |
| 3744 | |
Peter Xu | 6b19a7d | 2017-07-18 11:39:10 +0800 | [diff] [blame] | 3745 | for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) { |
| 3746 | if (ms->enabled_capabilities[i]) { |
| 3747 | head = migrate_cap_add(head, i, true); |
| 3748 | } |
| 3749 | } |
| 3750 | |
| 3751 | ret = migrate_caps_check(cap_list, head, errp); |
| 3752 | |
| 3753 | /* It works with head == NULL */ |
| 3754 | qapi_free_MigrationCapabilityStatusList(head); |
| 3755 | |
| 3756 | return ret; |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | static const TypeInfo migration_type = { |
| 3760 | .name = TYPE_MIGRATION, |
Peter Xu | 01f6e14 | 2017-06-28 15:15:44 +0800 | [diff] [blame] | 3761 | /* |
Peter Xu | c8d3ff3 | 2017-07-05 16:21:23 +0800 | [diff] [blame] | 3762 | * NOTE: TYPE_MIGRATION is not really a device, as the object is |
| 3763 | * not created using qdev_create(), it is not attached to the qdev |
| 3764 | * device tree, and it is never realized. |
| 3765 | * |
| 3766 | * TODO: Make this TYPE_OBJECT once QOM provides something like |
| 3767 | * TYPE_DEVICE's "-global" properties. |
Peter Xu | 01f6e14 | 2017-06-28 15:15:44 +0800 | [diff] [blame] | 3768 | */ |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3769 | .parent = TYPE_DEVICE, |
| 3770 | .class_init = migration_class_init, |
| 3771 | .class_size = sizeof(MigrationClass), |
| 3772 | .instance_size = sizeof(MigrationState), |
| 3773 | .instance_init = migration_instance_init, |
Marc-André Lureau | b91bf5e | 2017-08-01 17:04:18 +0100 | [diff] [blame] | 3774 | .instance_finalize = migration_instance_finalize, |
Peter Xu | e5cb7e7 | 2017-06-27 12:10:13 +0800 | [diff] [blame] | 3775 | }; |
| 3776 | |
| 3777 | static void register_migration_types(void) |
| 3778 | { |
| 3779 | type_register_static(&migration_type); |
| 3780 | } |
| 3781 | |
| 3782 | type_init(register_migration_types); |