]>
Commit | Line | Data |
---|---|---|
35a6ed4f HZ |
1 | /* |
2 | * COarse-grain LOck-stepping Virtual Machines for Non-stop Service (COLO) | |
3 | * (a.k.a. Fault Tolerance or Continuous Replication) | |
4 | * | |
5 | * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. | |
6 | * Copyright (c) 2016 FUJITSU LIMITED | |
7 | * Copyright (c) 2016 Intel Corporation | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or | |
10 | * later. See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include "qemu/osdep.h" | |
0b827d5e | 14 | #include "sysemu/sysemu.h" |
e688df6b | 15 | #include "qapi/error.h" |
9af23989 | 16 | #include "qapi/qapi-commands-migration.h" |
6666c96a | 17 | #include "migration.h" |
08a0aee1 | 18 | #include "qemu-file.h" |
20a519a0 | 19 | #include "savevm.h" |
35a6ed4f | 20 | #include "migration/colo.h" |
2c9e6fec | 21 | #include "block.h" |
a91246c9 | 22 | #include "io/channel-buffer.h" |
0b827d5e | 23 | #include "trace.h" |
56ba83d2 | 24 | #include "qemu/error-report.h" |
db725815 | 25 | #include "qemu/main-loop.h" |
d4842052 | 26 | #include "qemu/rcu.h" |
d89e666e | 27 | #include "migration/failover.h" |
0393031a | 28 | #include "migration/ram.h" |
335d10cd | 29 | #ifdef CONFIG_REPLICATION |
b0262955 | 30 | #include "block/replication.h" |
335d10cd | 31 | #endif |
131b2153 ZC |
32 | #include "net/colo-compare.h" |
33 | #include "net/colo.h" | |
8e48ac95 | 34 | #include "block/block.h" |
9ecff6d6 | 35 | #include "qapi/qapi-events-migration.h" |
f56c0065 | 36 | #include "qapi/qmp/qerror.h" |
3f6df99d | 37 | #include "sysemu/cpus.h" |
54d31236 | 38 | #include "sysemu/runstate.h" |
7b343530 | 39 | #include "net/filter.h" |
35a6ed4f | 40 | |
a8664ba5 | 41 | static bool vmstate_loading; |
131b2153 | 42 | static Notifier packets_compare_notifier; |
a8664ba5 | 43 | |
5ed0deca ZC |
44 | /* User need to know colo mode after COLO failover */ |
45 | static COLOMode last_colo_mode; | |
46 | ||
a91246c9 HZ |
47 | #define COLO_BUFFER_BASE_SIZE (4 * 1024 * 1024) |
48 | ||
0b827d5e HZ |
49 | bool migration_in_colo_state(void) |
50 | { | |
51 | MigrationState *s = migrate_get_current(); | |
52 | ||
53 | return (s->state == MIGRATION_STATUS_COLO); | |
54 | } | |
55 | ||
25d0c16f HZ |
56 | bool migration_incoming_in_colo_state(void) |
57 | { | |
58 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
59 | ||
60 | return mis && (mis->state == MIGRATION_STATUS_COLO); | |
61 | } | |
62 | ||
b3f7f0c5 HZ |
63 | static bool colo_runstate_is_stopped(void) |
64 | { | |
65 | return runstate_check(RUN_STATE_COLO) || !runstate_is_running(); | |
66 | } | |
67 | ||
9d2db376 HZ |
68 | static void secondary_vm_do_failover(void) |
69 | { | |
3ebb9c4f ZC |
70 | /* COLO needs enable block-replication */ |
71 | #ifdef CONFIG_REPLICATION | |
9d2db376 HZ |
72 | int old_state; |
73 | MigrationIncomingState *mis = migration_incoming_get_current(); | |
8e48ac95 | 74 | Error *local_err = NULL; |
9d2db376 | 75 | |
a8664ba5 HZ |
76 | /* Can not do failover during the process of VM's loading VMstate, Or |
77 | * it will break the secondary VM. | |
78 | */ | |
79 | if (vmstate_loading) { | |
80 | old_state = failover_set_state(FAILOVER_STATUS_ACTIVE, | |
81 | FAILOVER_STATUS_RELAUNCH); | |
82 | if (old_state != FAILOVER_STATUS_ACTIVE) { | |
83 | error_report("Unknown error while do failover for secondary VM," | |
977c736f | 84 | "old_state: %s", FailoverStatus_str(old_state)); |
a8664ba5 HZ |
85 | } |
86 | return; | |
87 | } | |
88 | ||
9d2db376 HZ |
89 | migrate_set_state(&mis->state, MIGRATION_STATUS_COLO, |
90 | MIGRATION_STATUS_COMPLETED); | |
91 | ||
8e48ac95 ZC |
92 | replication_stop_all(true, &local_err); |
93 | if (local_err) { | |
94 | error_report_err(local_err); | |
27d07fcf | 95 | local_err = NULL; |
8e48ac95 ZC |
96 | } |
97 | ||
7b343530 HZ |
98 | /* Notify all filters of all NIC to do checkpoint */ |
99 | colo_notify_filters_event(COLO_EVENT_FAILOVER, &local_err); | |
100 | if (local_err) { | |
101 | error_report_err(local_err); | |
102 | } | |
103 | ||
9d2db376 HZ |
104 | if (!autostart) { |
105 | error_report("\"-S\" qemu option will be ignored in secondary side"); | |
106 | /* recover runstate to normal migration finish state */ | |
107 | autostart = true; | |
108 | } | |
c937b9a6 HZ |
109 | /* |
110 | * Make sure COLO incoming thread not block in recv or send, | |
111 | * If mis->from_src_file and mis->to_src_file use the same fd, | |
112 | * The second shutdown() will return -1, we ignore this value, | |
113 | * It is harmless. | |
114 | */ | |
115 | if (mis->from_src_file) { | |
116 | qemu_file_shutdown(mis->from_src_file); | |
117 | } | |
118 | if (mis->to_src_file) { | |
119 | qemu_file_shutdown(mis->to_src_file); | |
120 | } | |
9d2db376 HZ |
121 | |
122 | old_state = failover_set_state(FAILOVER_STATUS_ACTIVE, | |
123 | FAILOVER_STATUS_COMPLETED); | |
124 | if (old_state != FAILOVER_STATUS_ACTIVE) { | |
125 | error_report("Incorrect state (%s) while doing failover for " | |
977c736f | 126 | "secondary VM", FailoverStatus_str(old_state)); |
9d2db376 HZ |
127 | return; |
128 | } | |
c937b9a6 HZ |
129 | /* Notify COLO incoming thread that failover work is finished */ |
130 | qemu_sem_post(&mis->colo_incoming_sem); | |
1fe6ab26 | 131 | |
9d2db376 HZ |
132 | /* For Secondary VM, jump to incoming co */ |
133 | if (mis->migration_incoming_co) { | |
134 | qemu_coroutine_enter(mis->migration_incoming_co); | |
135 | } | |
3ebb9c4f ZC |
136 | #else |
137 | abort(); | |
138 | #endif | |
9d2db376 HZ |
139 | } |
140 | ||
b3f7f0c5 HZ |
141 | static void primary_vm_do_failover(void) |
142 | { | |
3ebb9c4f | 143 | #ifdef CONFIG_REPLICATION |
b3f7f0c5 HZ |
144 | MigrationState *s = migrate_get_current(); |
145 | int old_state; | |
8e48ac95 | 146 | Error *local_err = NULL; |
b3f7f0c5 HZ |
147 | |
148 | migrate_set_state(&s->state, MIGRATION_STATUS_COLO, | |
149 | MIGRATION_STATUS_COMPLETED); | |
2518aec1 HZ |
150 | /* |
151 | * kick COLO thread which might wait at | |
152 | * qemu_sem_wait(&s->colo_checkpoint_sem). | |
153 | */ | |
ae4c2099 | 154 | colo_checkpoint_notify(s); |
b3f7f0c5 | 155 | |
c937b9a6 HZ |
156 | /* |
157 | * Wake up COLO thread which may blocked in recv() or send(), | |
158 | * The s->rp_state.from_dst_file and s->to_dst_file may use the | |
159 | * same fd, but we still shutdown the fd for twice, it is harmless. | |
160 | */ | |
161 | if (s->to_dst_file) { | |
162 | qemu_file_shutdown(s->to_dst_file); | |
163 | } | |
164 | if (s->rp_state.from_dst_file) { | |
165 | qemu_file_shutdown(s->rp_state.from_dst_file); | |
166 | } | |
167 | ||
b3f7f0c5 HZ |
168 | old_state = failover_set_state(FAILOVER_STATUS_ACTIVE, |
169 | FAILOVER_STATUS_COMPLETED); | |
170 | if (old_state != FAILOVER_STATUS_ACTIVE) { | |
171 | error_report("Incorrect state (%s) while doing failover for Primary VM", | |
977c736f | 172 | FailoverStatus_str(old_state)); |
b3f7f0c5 HZ |
173 | return; |
174 | } | |
8e48ac95 ZC |
175 | |
176 | replication_stop_all(true, &local_err); | |
177 | if (local_err) { | |
178 | error_report_err(local_err); | |
179 | local_err = NULL; | |
180 | } | |
181 | ||
c937b9a6 HZ |
182 | /* Notify COLO thread that failover work is finished */ |
183 | qemu_sem_post(&s->colo_exit_sem); | |
3ebb9c4f ZC |
184 | #else |
185 | abort(); | |
186 | #endif | |
b3f7f0c5 HZ |
187 | } |
188 | ||
aad555c2 ZC |
189 | COLOMode get_colo_mode(void) |
190 | { | |
191 | if (migration_in_colo_state()) { | |
192 | return COLO_MODE_PRIMARY; | |
193 | } else if (migration_incoming_in_colo_state()) { | |
194 | return COLO_MODE_SECONDARY; | |
195 | } else { | |
41b6b779 | 196 | return COLO_MODE_NONE; |
aad555c2 ZC |
197 | } |
198 | } | |
199 | ||
c0913d1d | 200 | void colo_do_failover(void) |
b3f7f0c5 HZ |
201 | { |
202 | /* Make sure VM stopped while failover happened. */ | |
203 | if (!colo_runstate_is_stopped()) { | |
204 | vm_stop_force_state(RUN_STATE_COLO); | |
205 | } | |
206 | ||
2b9f6bf3 | 207 | switch (last_colo_mode = get_colo_mode()) { |
82cd368c | 208 | case COLO_MODE_PRIMARY: |
b3f7f0c5 | 209 | primary_vm_do_failover(); |
82cd368c ZC |
210 | break; |
211 | case COLO_MODE_SECONDARY: | |
9d2db376 | 212 | secondary_vm_do_failover(); |
82cd368c ZC |
213 | break; |
214 | default: | |
215 | error_report("colo_do_failover failed because the colo mode" | |
216 | " could not be obtained"); | |
b3f7f0c5 HZ |
217 | } |
218 | } | |
219 | ||
335d10cd | 220 | #ifdef CONFIG_REPLICATION |
2c9639ec ZC |
221 | void qmp_xen_set_replication(bool enable, bool primary, |
222 | bool has_failover, bool failover, | |
223 | Error **errp) | |
224 | { | |
225 | ReplicationMode mode = primary ? | |
226 | REPLICATION_MODE_PRIMARY : | |
227 | REPLICATION_MODE_SECONDARY; | |
228 | ||
229 | if (has_failover && enable) { | |
230 | error_setg(errp, "Parameter 'failover' is only for" | |
231 | " stopping replication"); | |
232 | return; | |
233 | } | |
234 | ||
235 | if (enable) { | |
236 | replication_start_all(mode, errp); | |
237 | } else { | |
238 | if (!has_failover) { | |
239 | failover = NULL; | |
240 | } | |
241 | replication_stop_all(failover, failover ? NULL : errp); | |
242 | } | |
243 | } | |
244 | ||
daa33c52 ZC |
245 | ReplicationStatus *qmp_query_xen_replication_status(Error **errp) |
246 | { | |
247 | Error *err = NULL; | |
248 | ReplicationStatus *s = g_new0(ReplicationStatus, 1); | |
249 | ||
250 | replication_get_error_all(&err); | |
251 | if (err) { | |
252 | s->error = true; | |
253 | s->has_desc = true; | |
254 | s->desc = g_strdup(error_get_pretty(err)); | |
255 | } else { | |
256 | s->error = false; | |
257 | } | |
258 | ||
259 | error_free(err); | |
260 | return s; | |
261 | } | |
262 | ||
263 | void qmp_xen_colo_do_checkpoint(Error **errp) | |
264 | { | |
735527e1 MA |
265 | Error *err = NULL; |
266 | ||
267 | replication_do_checkpoint_all(&err); | |
268 | if (err) { | |
269 | error_propagate(errp, err); | |
270 | return; | |
271 | } | |
0e8818f0 ZC |
272 | /* Notify all filters of all NIC to do checkpoint */ |
273 | colo_notify_filters_event(COLO_EVENT_CHECKPOINT, errp); | |
daa33c52 | 274 | } |
335d10cd | 275 | #endif |
daa33c52 | 276 | |
f56c0065 ZC |
277 | COLOStatus *qmp_query_colo_status(Error **errp) |
278 | { | |
279 | COLOStatus *s = g_new0(COLOStatus, 1); | |
280 | ||
281 | s->mode = get_colo_mode(); | |
5ed0deca | 282 | s->last_mode = last_colo_mode; |
f56c0065 ZC |
283 | |
284 | switch (failover_get_state()) { | |
285 | case FAILOVER_STATUS_NONE: | |
286 | s->reason = COLO_EXIT_REASON_NONE; | |
287 | break; | |
1fe6ab26 | 288 | case FAILOVER_STATUS_COMPLETED: |
f56c0065 ZC |
289 | s->reason = COLO_EXIT_REASON_REQUEST; |
290 | break; | |
291 | default: | |
3a43ac47 ZC |
292 | if (migration_in_colo_state()) { |
293 | s->reason = COLO_EXIT_REASON_PROCESSING; | |
294 | } else { | |
295 | s->reason = COLO_EXIT_REASON_ERROR; | |
296 | } | |
f56c0065 ZC |
297 | } |
298 | ||
299 | return s; | |
300 | } | |
301 | ||
4f97558e HZ |
302 | static void colo_send_message(QEMUFile *f, COLOMessage msg, |
303 | Error **errp) | |
304 | { | |
305 | int ret; | |
306 | ||
307 | if (msg >= COLO_MESSAGE__MAX) { | |
308 | error_setg(errp, "%s: Invalid message", __func__); | |
309 | return; | |
310 | } | |
311 | qemu_put_be32(f, msg); | |
312 | qemu_fflush(f); | |
313 | ||
314 | ret = qemu_file_get_error(f); | |
315 | if (ret < 0) { | |
316 | error_setg_errno(errp, -ret, "Can't send COLO message"); | |
317 | } | |
977c736f | 318 | trace_colo_send_message(COLOMessage_str(msg)); |
4f97558e HZ |
319 | } |
320 | ||
a91246c9 HZ |
321 | static void colo_send_message_value(QEMUFile *f, COLOMessage msg, |
322 | uint64_t value, Error **errp) | |
323 | { | |
324 | Error *local_err = NULL; | |
325 | int ret; | |
326 | ||
327 | colo_send_message(f, msg, &local_err); | |
328 | if (local_err) { | |
329 | error_propagate(errp, local_err); | |
330 | return; | |
331 | } | |
332 | qemu_put_be64(f, value); | |
333 | qemu_fflush(f); | |
334 | ||
335 | ret = qemu_file_get_error(f); | |
336 | if (ret < 0) { | |
337 | error_setg_errno(errp, -ret, "Failed to send value for message:%s", | |
977c736f | 338 | COLOMessage_str(msg)); |
a91246c9 HZ |
339 | } |
340 | } | |
341 | ||
4f97558e HZ |
342 | static COLOMessage colo_receive_message(QEMUFile *f, Error **errp) |
343 | { | |
344 | COLOMessage msg; | |
345 | int ret; | |
346 | ||
347 | msg = qemu_get_be32(f); | |
348 | ret = qemu_file_get_error(f); | |
349 | if (ret < 0) { | |
350 | error_setg_errno(errp, -ret, "Can't receive COLO message"); | |
351 | return msg; | |
352 | } | |
353 | if (msg >= COLO_MESSAGE__MAX) { | |
354 | error_setg(errp, "%s: Invalid message", __func__); | |
355 | return msg; | |
356 | } | |
977c736f | 357 | trace_colo_receive_message(COLOMessage_str(msg)); |
4f97558e HZ |
358 | return msg; |
359 | } | |
360 | ||
361 | static void colo_receive_check_message(QEMUFile *f, COLOMessage expect_msg, | |
362 | Error **errp) | |
363 | { | |
364 | COLOMessage msg; | |
365 | Error *local_err = NULL; | |
366 | ||
367 | msg = colo_receive_message(f, &local_err); | |
368 | if (local_err) { | |
369 | error_propagate(errp, local_err); | |
370 | return; | |
371 | } | |
372 | if (msg != expect_msg) { | |
373 | error_setg(errp, "Unexpected COLO message %d, expected %d", | |
374 | msg, expect_msg); | |
375 | } | |
376 | } | |
377 | ||
4291d372 HZ |
378 | static uint64_t colo_receive_message_value(QEMUFile *f, uint32_t expect_msg, |
379 | Error **errp) | |
380 | { | |
381 | Error *local_err = NULL; | |
382 | uint64_t value; | |
383 | int ret; | |
384 | ||
385 | colo_receive_check_message(f, expect_msg, &local_err); | |
386 | if (local_err) { | |
387 | error_propagate(errp, local_err); | |
388 | return 0; | |
389 | } | |
390 | ||
391 | value = qemu_get_be64(f); | |
392 | ret = qemu_file_get_error(f); | |
393 | if (ret < 0) { | |
394 | error_setg_errno(errp, -ret, "Failed to get value for COLO message: %s", | |
977c736f | 395 | COLOMessage_str(expect_msg)); |
4291d372 HZ |
396 | } |
397 | return value; | |
398 | } | |
399 | ||
a91246c9 HZ |
400 | static int colo_do_checkpoint_transaction(MigrationState *s, |
401 | QIOChannelBuffer *bioc, | |
402 | QEMUFile *fb) | |
4f97558e HZ |
403 | { |
404 | Error *local_err = NULL; | |
a91246c9 | 405 | int ret = -1; |
4f97558e HZ |
406 | |
407 | colo_send_message(s->to_dst_file, COLO_MESSAGE_CHECKPOINT_REQUEST, | |
408 | &local_err); | |
409 | if (local_err) { | |
410 | goto out; | |
411 | } | |
412 | ||
413 | colo_receive_check_message(s->rp_state.from_dst_file, | |
414 | COLO_MESSAGE_CHECKPOINT_REPLY, &local_err); | |
415 | if (local_err) { | |
416 | goto out; | |
417 | } | |
a91246c9 HZ |
418 | /* Reset channel-buffer directly */ |
419 | qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL); | |
420 | bioc->usage = 0; | |
4f97558e | 421 | |
a91246c9 | 422 | qemu_mutex_lock_iothread(); |
b3f7f0c5 HZ |
423 | if (failover_get_state() != FAILOVER_STATUS_NONE) { |
424 | qemu_mutex_unlock_iothread(); | |
425 | goto out; | |
426 | } | |
a91246c9 HZ |
427 | vm_stop_force_state(RUN_STATE_COLO); |
428 | qemu_mutex_unlock_iothread(); | |
429 | trace_colo_vm_state_change("run", "stop"); | |
b3f7f0c5 HZ |
430 | /* |
431 | * Failover request bh could be called after vm_stop_force_state(), | |
432 | * So we need check failover_request_is_active() again. | |
433 | */ | |
434 | if (failover_get_state() != FAILOVER_STATUS_NONE) { | |
435 | goto out; | |
436 | } | |
a91246c9 | 437 | qemu_mutex_lock_iothread(); |
3ebb9c4f ZC |
438 | |
439 | #ifdef CONFIG_REPLICATION | |
8e48ac95 ZC |
440 | replication_do_checkpoint_all(&local_err); |
441 | if (local_err) { | |
442 | qemu_mutex_unlock_iothread(); | |
443 | goto out; | |
444 | } | |
3ebb9c4f ZC |
445 | #else |
446 | abort(); | |
447 | #endif | |
4f97558e HZ |
448 | |
449 | colo_send_message(s->to_dst_file, COLO_MESSAGE_VMSTATE_SEND, &local_err); | |
450 | if (local_err) { | |
3f6df99d ZC |
451 | qemu_mutex_unlock_iothread(); |
452 | goto out; | |
453 | } | |
454 | /* Note: device state is saved into buffer */ | |
455 | ret = qemu_save_device_state(fb); | |
456 | ||
457 | qemu_mutex_unlock_iothread(); | |
458 | if (ret < 0) { | |
4f97558e HZ |
459 | goto out; |
460 | } | |
91fe9a8d RL |
461 | |
462 | if (migrate_auto_converge()) { | |
463 | mig_throttle_counter_reset(); | |
464 | } | |
3f6df99d ZC |
465 | /* |
466 | * Only save VM's live state, which not including device state. | |
467 | * TODO: We may need a timeout mechanism to prevent COLO process | |
468 | * to be blocked here. | |
469 | */ | |
470 | qemu_savevm_live_state(s->to_dst_file); | |
471 | ||
472 | qemu_fflush(fb); | |
473 | ||
a91246c9 HZ |
474 | /* |
475 | * We need the size of the VMstate data in Secondary side, | |
476 | * With which we can decide how much data should be read. | |
477 | */ | |
478 | colo_send_message_value(s->to_dst_file, COLO_MESSAGE_VMSTATE_SIZE, | |
479 | bioc->usage, &local_err); | |
480 | if (local_err) { | |
481 | goto out; | |
482 | } | |
4f97558e | 483 | |
a91246c9 HZ |
484 | qemu_put_buffer(s->to_dst_file, bioc->data, bioc->usage); |
485 | qemu_fflush(s->to_dst_file); | |
486 | ret = qemu_file_get_error(s->to_dst_file); | |
487 | if (ret < 0) { | |
488 | goto out; | |
489 | } | |
4f97558e HZ |
490 | |
491 | colo_receive_check_message(s->rp_state.from_dst_file, | |
492 | COLO_MESSAGE_VMSTATE_RECEIVED, &local_err); | |
493 | if (local_err) { | |
494 | goto out; | |
495 | } | |
496 | ||
4fa8ed25 LS |
497 | qemu_event_reset(&s->colo_checkpoint_event); |
498 | colo_notify_compares_event(NULL, COLO_EVENT_CHECKPOINT, &local_err); | |
499 | if (local_err) { | |
500 | goto out; | |
501 | } | |
502 | ||
4f97558e HZ |
503 | colo_receive_check_message(s->rp_state.from_dst_file, |
504 | COLO_MESSAGE_VMSTATE_LOADED, &local_err); | |
505 | if (local_err) { | |
506 | goto out; | |
507 | } | |
508 | ||
a91246c9 HZ |
509 | ret = 0; |
510 | ||
511 | qemu_mutex_lock_iothread(); | |
512 | vm_start(); | |
513 | qemu_mutex_unlock_iothread(); | |
514 | trace_colo_vm_state_change("stop", "run"); | |
4f97558e | 515 | |
4f97558e HZ |
516 | out: |
517 | if (local_err) { | |
518 | error_report_err(local_err); | |
519 | } | |
a91246c9 | 520 | return ret; |
4f97558e HZ |
521 | } |
522 | ||
131b2153 ZC |
523 | static void colo_compare_notify_checkpoint(Notifier *notifier, void *data) |
524 | { | |
525 | colo_checkpoint_notify(data); | |
526 | } | |
527 | ||
0b827d5e HZ |
528 | static void colo_process_checkpoint(MigrationState *s) |
529 | { | |
a91246c9 HZ |
530 | QIOChannelBuffer *bioc; |
531 | QEMUFile *fb = NULL; | |
4f97558e HZ |
532 | Error *local_err = NULL; |
533 | int ret; | |
534 | ||
2b9f6bf3 | 535 | if (get_colo_mode() != COLO_MODE_PRIMARY) { |
5ed0deca ZC |
536 | error_report("COLO mode must be COLO_MODE_PRIMARY"); |
537 | return; | |
538 | } | |
539 | ||
aef06085 HZ |
540 | failover_init_state(); |
541 | ||
56ba83d2 HZ |
542 | s->rp_state.from_dst_file = qemu_file_get_return_path(s->to_dst_file); |
543 | if (!s->rp_state.from_dst_file) { | |
544 | error_report("Open QEMUFile from_dst_file failed"); | |
545 | goto out; | |
546 | } | |
547 | ||
131b2153 ZC |
548 | packets_compare_notifier.notify = colo_compare_notify_checkpoint; |
549 | colo_compare_register_notifier(&packets_compare_notifier); | |
550 | ||
4f97558e HZ |
551 | /* |
552 | * Wait for Secondary finish loading VM states and enter COLO | |
553 | * restore. | |
554 | */ | |
555 | colo_receive_check_message(s->rp_state.from_dst_file, | |
556 | COLO_MESSAGE_CHECKPOINT_READY, &local_err); | |
557 | if (local_err) { | |
558 | goto out; | |
559 | } | |
a91246c9 | 560 | bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE); |
77ef2dc1 | 561 | fb = qemu_file_new_output(QIO_CHANNEL(bioc)); |
a91246c9 | 562 | object_unref(OBJECT(bioc)); |
4f97558e | 563 | |
0b827d5e | 564 | qemu_mutex_lock_iothread(); |
3ebb9c4f | 565 | #ifdef CONFIG_REPLICATION |
8e48ac95 ZC |
566 | replication_start_all(REPLICATION_MODE_PRIMARY, &local_err); |
567 | if (local_err) { | |
568 | qemu_mutex_unlock_iothread(); | |
569 | goto out; | |
570 | } | |
3ebb9c4f ZC |
571 | #else |
572 | abort(); | |
573 | #endif | |
8e48ac95 | 574 | |
0b827d5e HZ |
575 | vm_start(); |
576 | qemu_mutex_unlock_iothread(); | |
577 | trace_colo_vm_state_change("stop", "run"); | |
578 | ||
0e0f0479 ZC |
579 | timer_mod(s->colo_delay_timer, qemu_clock_get_ms(QEMU_CLOCK_HOST) + |
580 | s->parameters.x_checkpoint_delay); | |
479125d5 | 581 | |
4f97558e | 582 | while (s->state == MIGRATION_STATUS_COLO) { |
b3f7f0c5 HZ |
583 | if (failover_get_state() != FAILOVER_STATUS_NONE) { |
584 | error_report("failover request"); | |
585 | goto out; | |
586 | } | |
587 | ||
bb70b66e | 588 | qemu_event_wait(&s->colo_checkpoint_event); |
18cc23d7 | 589 | |
2518aec1 HZ |
590 | if (s->state != MIGRATION_STATUS_COLO) { |
591 | goto out; | |
592 | } | |
a91246c9 | 593 | ret = colo_do_checkpoint_transaction(s, bioc, fb); |
4f97558e HZ |
594 | if (ret < 0) { |
595 | goto out; | |
596 | } | |
597 | } | |
0b827d5e | 598 | |
56ba83d2 | 599 | out: |
4f97558e HZ |
600 | /* Throw the unreported error message after exited from loop */ |
601 | if (local_err) { | |
602 | error_report_err(local_err); | |
603 | } | |
604 | ||
a91246c9 HZ |
605 | if (fb) { |
606 | qemu_fclose(fb); | |
607 | } | |
608 | ||
9ecff6d6 HZ |
609 | /* |
610 | * There are only two reasons we can get here, some error happened | |
611 | * or the user triggered failover. | |
612 | */ | |
613 | switch (failover_get_state()) { | |
1fe6ab26 | 614 | case FAILOVER_STATUS_COMPLETED: |
9ecff6d6 HZ |
615 | qapi_event_send_colo_exit(COLO_MODE_PRIMARY, |
616 | COLO_EXIT_REASON_REQUEST); | |
617 | break; | |
618 | default: | |
3a43ac47 ZC |
619 | qapi_event_send_colo_exit(COLO_MODE_PRIMARY, |
620 | COLO_EXIT_REASON_ERROR); | |
9ecff6d6 HZ |
621 | } |
622 | ||
c937b9a6 HZ |
623 | /* Hope this not to be too long to wait here */ |
624 | qemu_sem_wait(&s->colo_exit_sem); | |
625 | qemu_sem_destroy(&s->colo_exit_sem); | |
131b2153 ZC |
626 | |
627 | /* | |
628 | * It is safe to unregister notifier after failover finished. | |
629 | * Besides, colo_delay_timer and colo_checkpoint_sem can't be | |
3a4452d8 | 630 | * released before unregister notifier, or there will be use-after-free |
131b2153 ZC |
631 | * error. |
632 | */ | |
633 | colo_compare_unregister_notifier(&packets_compare_notifier); | |
131b2153 | 634 | timer_free(s->colo_delay_timer); |
bb70b66e | 635 | qemu_event_destroy(&s->colo_checkpoint_event); |
131b2153 | 636 | |
c937b9a6 HZ |
637 | /* |
638 | * Must be called after failover BH is completed, | |
639 | * Or the failover BH may shutdown the wrong fd that | |
640 | * re-used by other threads after we release here. | |
641 | */ | |
56ba83d2 HZ |
642 | if (s->rp_state.from_dst_file) { |
643 | qemu_fclose(s->rp_state.from_dst_file); | |
ac183dac | 644 | s->rp_state.from_dst_file = NULL; |
56ba83d2 | 645 | } |
0b827d5e HZ |
646 | } |
647 | ||
479125d5 HZ |
648 | void colo_checkpoint_notify(void *opaque) |
649 | { | |
650 | MigrationState *s = opaque; | |
651 | int64_t next_notify_time; | |
652 | ||
bb70b66e | 653 | qemu_event_set(&s->colo_checkpoint_event); |
479125d5 HZ |
654 | s->colo_checkpoint_time = qemu_clock_get_ms(QEMU_CLOCK_HOST); |
655 | next_notify_time = s->colo_checkpoint_time + | |
656 | s->parameters.x_checkpoint_delay; | |
657 | timer_mod(s->colo_delay_timer, next_notify_time); | |
658 | } | |
659 | ||
0b827d5e HZ |
660 | void migrate_start_colo_process(MigrationState *s) |
661 | { | |
662 | qemu_mutex_unlock_iothread(); | |
bb70b66e | 663 | qemu_event_init(&s->colo_checkpoint_event, false); |
479125d5 HZ |
664 | s->colo_delay_timer = timer_new_ms(QEMU_CLOCK_HOST, |
665 | colo_checkpoint_notify, s); | |
666 | ||
c937b9a6 | 667 | qemu_sem_init(&s->colo_exit_sem, 0); |
0b827d5e HZ |
668 | colo_process_checkpoint(s); |
669 | qemu_mutex_lock_iothread(); | |
670 | } | |
25d0c16f | 671 | |
6ad8ad38 HZ |
672 | static void colo_incoming_process_checkpoint(MigrationIncomingState *mis, |
673 | QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp) | |
674 | { | |
675 | uint64_t total_size; | |
676 | uint64_t value; | |
677 | Error *local_err = NULL; | |
678 | int ret; | |
679 | ||
680 | qemu_mutex_lock_iothread(); | |
681 | vm_stop_force_state(RUN_STATE_COLO); | |
6ad8ad38 | 682 | qemu_mutex_unlock_iothread(); |
9c5c8ff2 | 683 | trace_colo_vm_state_change("run", "stop"); |
6ad8ad38 HZ |
684 | |
685 | /* FIXME: This is unnecessary for periodic checkpoint mode */ | |
686 | colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_REPLY, | |
687 | &local_err); | |
688 | if (local_err) { | |
689 | error_propagate(errp, local_err); | |
690 | return; | |
691 | } | |
692 | ||
693 | colo_receive_check_message(mis->from_src_file, | |
694 | COLO_MESSAGE_VMSTATE_SEND, &local_err); | |
695 | if (local_err) { | |
696 | error_propagate(errp, local_err); | |
697 | return; | |
698 | } | |
699 | ||
700 | qemu_mutex_lock_iothread(); | |
786d8b8e | 701 | cpu_synchronize_all_states(); |
6ad8ad38 HZ |
702 | ret = qemu_loadvm_state_main(mis->from_src_file, mis); |
703 | qemu_mutex_unlock_iothread(); | |
704 | ||
705 | if (ret < 0) { | |
706 | error_setg(errp, "Load VM's live state (ram) error"); | |
707 | return; | |
708 | } | |
709 | ||
710 | value = colo_receive_message_value(mis->from_src_file, | |
711 | COLO_MESSAGE_VMSTATE_SIZE, &local_err); | |
712 | if (local_err) { | |
713 | error_propagate(errp, local_err); | |
714 | return; | |
715 | } | |
716 | ||
717 | /* | |
718 | * Read VM device state data into channel buffer, | |
719 | * It's better to re-use the memory allocated. | |
720 | * Here we need to handle the channel buffer directly. | |
721 | */ | |
722 | if (value > bioc->capacity) { | |
723 | bioc->capacity = value; | |
724 | bioc->data = g_realloc(bioc->data, bioc->capacity); | |
725 | } | |
726 | total_size = qemu_get_buffer(mis->from_src_file, bioc->data, value); | |
727 | if (total_size != value) { | |
728 | error_setg(errp, "Got %" PRIu64 " VMState data, less than expected" | |
729 | " %" PRIu64, total_size, value); | |
730 | return; | |
731 | } | |
732 | bioc->usage = total_size; | |
733 | qio_channel_io_seek(QIO_CHANNEL(bioc), 0, 0, NULL); | |
734 | ||
735 | colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_RECEIVED, | |
736 | &local_err); | |
737 | if (local_err) { | |
738 | error_propagate(errp, local_err); | |
739 | return; | |
740 | } | |
741 | ||
742 | qemu_mutex_lock_iothread(); | |
743 | vmstate_loading = true; | |
24fa16f8 | 744 | colo_flush_ram_cache(); |
6ad8ad38 HZ |
745 | ret = qemu_load_device_state(fb); |
746 | if (ret < 0) { | |
747 | error_setg(errp, "COLO: load device state failed"); | |
92c932de | 748 | vmstate_loading = false; |
6ad8ad38 HZ |
749 | qemu_mutex_unlock_iothread(); |
750 | return; | |
751 | } | |
752 | ||
753 | #ifdef CONFIG_REPLICATION | |
754 | replication_get_error_all(&local_err); | |
755 | if (local_err) { | |
756 | error_propagate(errp, local_err); | |
92c932de | 757 | vmstate_loading = false; |
6ad8ad38 HZ |
758 | qemu_mutex_unlock_iothread(); |
759 | return; | |
760 | } | |
761 | ||
762 | /* discard colo disk buffer */ | |
763 | replication_do_checkpoint_all(&local_err); | |
764 | if (local_err) { | |
765 | error_propagate(errp, local_err); | |
92c932de | 766 | vmstate_loading = false; |
6ad8ad38 HZ |
767 | qemu_mutex_unlock_iothread(); |
768 | return; | |
769 | } | |
770 | #else | |
771 | abort(); | |
772 | #endif | |
773 | /* Notify all filters of all NIC to do checkpoint */ | |
774 | colo_notify_filters_event(COLO_EVENT_CHECKPOINT, &local_err); | |
775 | ||
776 | if (local_err) { | |
777 | error_propagate(errp, local_err); | |
92c932de | 778 | vmstate_loading = false; |
6ad8ad38 HZ |
779 | qemu_mutex_unlock_iothread(); |
780 | return; | |
781 | } | |
782 | ||
783 | vmstate_loading = false; | |
784 | vm_start(); | |
6ad8ad38 | 785 | qemu_mutex_unlock_iothread(); |
9c5c8ff2 | 786 | trace_colo_vm_state_change("stop", "run"); |
6ad8ad38 HZ |
787 | |
788 | if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) { | |
6ad8ad38 HZ |
789 | return; |
790 | } | |
791 | ||
792 | colo_send_message(mis->to_src_file, COLO_MESSAGE_VMSTATE_LOADED, | |
793 | &local_err); | |
2155ceaf | 794 | error_propagate(errp, local_err); |
6ad8ad38 HZ |
795 | } |
796 | ||
797 | static void colo_wait_handle_message(MigrationIncomingState *mis, | |
798 | QEMUFile *fb, QIOChannelBuffer *bioc, Error **errp) | |
4f97558e HZ |
799 | { |
800 | COLOMessage msg; | |
801 | Error *local_err = NULL; | |
802 | ||
6ad8ad38 | 803 | msg = colo_receive_message(mis->from_src_file, &local_err); |
4f97558e HZ |
804 | if (local_err) { |
805 | error_propagate(errp, local_err); | |
806 | return; | |
807 | } | |
808 | ||
809 | switch (msg) { | |
810 | case COLO_MESSAGE_CHECKPOINT_REQUEST: | |
6ad8ad38 | 811 | colo_incoming_process_checkpoint(mis, fb, bioc, errp); |
4f97558e HZ |
812 | break; |
813 | default: | |
4f97558e HZ |
814 | error_setg(errp, "Got unknown COLO message: %d", msg); |
815 | break; | |
816 | } | |
817 | } | |
818 | ||
795969ab RL |
819 | void colo_shutdown(void) |
820 | { | |
821 | MigrationIncomingState *mis = NULL; | |
822 | MigrationState *s = NULL; | |
823 | ||
824 | switch (get_colo_mode()) { | |
825 | case COLO_MODE_PRIMARY: | |
826 | s = migrate_get_current(); | |
827 | qemu_event_set(&s->colo_checkpoint_event); | |
828 | qemu_sem_post(&s->colo_exit_sem); | |
829 | break; | |
830 | case COLO_MODE_SECONDARY: | |
831 | mis = migration_incoming_get_current(); | |
832 | qemu_sem_post(&mis->colo_incoming_sem); | |
833 | break; | |
834 | default: | |
835 | break; | |
836 | } | |
837 | } | |
838 | ||
25d0c16f HZ |
839 | void *colo_process_incoming_thread(void *opaque) |
840 | { | |
841 | MigrationIncomingState *mis = opaque; | |
4291d372 HZ |
842 | QEMUFile *fb = NULL; |
843 | QIOChannelBuffer *bioc = NULL; /* Cache incoming device state */ | |
4f97558e | 844 | Error *local_err = NULL; |
25d0c16f | 845 | |
74637e6f | 846 | rcu_register_thread(); |
c937b9a6 HZ |
847 | qemu_sem_init(&mis->colo_incoming_sem, 0); |
848 | ||
25d0c16f HZ |
849 | migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE, |
850 | MIGRATION_STATUS_COLO); | |
851 | ||
2b9f6bf3 | 852 | if (get_colo_mode() != COLO_MODE_SECONDARY) { |
5ed0deca ZC |
853 | error_report("COLO mode must be COLO_MODE_SECONDARY"); |
854 | return NULL; | |
855 | } | |
856 | ||
aef06085 HZ |
857 | failover_init_state(); |
858 | ||
56ba83d2 HZ |
859 | mis->to_src_file = qemu_file_get_return_path(mis->from_src_file); |
860 | if (!mis->to_src_file) { | |
861 | error_report("COLO incoming thread: Open QEMUFile to_src_file failed"); | |
862 | goto out; | |
863 | } | |
864 | /* | |
865 | * Note: the communication between Primary side and Secondary side | |
866 | * should be sequential, we set the fd to unblocked in migration incoming | |
867 | * coroutine, and here we are in the COLO incoming thread, so it is ok to | |
868 | * set the fd back to blocked. | |
869 | */ | |
870 | qemu_file_set_blocking(mis->from_src_file, true); | |
871 | ||
0393031a HZ |
872 | colo_incoming_start_dirty_log(); |
873 | ||
4291d372 | 874 | bioc = qio_channel_buffer_new(COLO_BUFFER_BASE_SIZE); |
77ef2dc1 | 875 | fb = qemu_file_new_input(QIO_CHANNEL(bioc)); |
4291d372 HZ |
876 | object_unref(OBJECT(bioc)); |
877 | ||
131b2153 | 878 | qemu_mutex_lock_iothread(); |
3ebb9c4f | 879 | #ifdef CONFIG_REPLICATION |
8e48ac95 ZC |
880 | replication_start_all(REPLICATION_MODE_SECONDARY, &local_err); |
881 | if (local_err) { | |
882 | qemu_mutex_unlock_iothread(); | |
883 | goto out; | |
884 | } | |
3ebb9c4f ZC |
885 | #else |
886 | abort(); | |
887 | #endif | |
131b2153 | 888 | vm_start(); |
131b2153 | 889 | qemu_mutex_unlock_iothread(); |
9c5c8ff2 | 890 | trace_colo_vm_state_change("stop", "run"); |
131b2153 | 891 | |
4f97558e HZ |
892 | colo_send_message(mis->to_src_file, COLO_MESSAGE_CHECKPOINT_READY, |
893 | &local_err); | |
894 | if (local_err) { | |
895 | goto out; | |
896 | } | |
897 | ||
898 | while (mis->state == MIGRATION_STATUS_COLO) { | |
6ad8ad38 | 899 | colo_wait_handle_message(mis, fb, bioc, &local_err); |
4f97558e | 900 | if (local_err) { |
6ad8ad38 HZ |
901 | error_report_err(local_err); |
902 | break; | |
4f97558e | 903 | } |
92c932de LS |
904 | |
905 | if (failover_get_state() == FAILOVER_STATUS_RELAUNCH) { | |
906 | failover_set_state(FAILOVER_STATUS_RELAUNCH, | |
907 | FAILOVER_STATUS_NONE); | |
908 | failover_request_active(NULL); | |
909 | break; | |
910 | } | |
911 | ||
9d2db376 HZ |
912 | if (failover_get_state() != FAILOVER_STATUS_NONE) { |
913 | error_report("failover request"); | |
6ad8ad38 | 914 | break; |
4f97558e HZ |
915 | } |
916 | } | |
25d0c16f | 917 | |
56ba83d2 | 918 | out: |
3a43ac47 ZC |
919 | /* |
920 | * There are only two reasons we can get here, some error happened | |
921 | * or the user triggered failover. | |
922 | */ | |
9ecff6d6 | 923 | switch (failover_get_state()) { |
1fe6ab26 | 924 | case FAILOVER_STATUS_COMPLETED: |
9ecff6d6 HZ |
925 | qapi_event_send_colo_exit(COLO_MODE_SECONDARY, |
926 | COLO_EXIT_REASON_REQUEST); | |
927 | break; | |
928 | default: | |
3a43ac47 ZC |
929 | qapi_event_send_colo_exit(COLO_MODE_SECONDARY, |
930 | COLO_EXIT_REASON_ERROR); | |
9ecff6d6 HZ |
931 | } |
932 | ||
4291d372 HZ |
933 | if (fb) { |
934 | qemu_fclose(fb); | |
935 | } | |
936 | ||
c937b9a6 HZ |
937 | /* Hope this not to be too long to loop here */ |
938 | qemu_sem_wait(&mis->colo_incoming_sem); | |
939 | qemu_sem_destroy(&mis->colo_incoming_sem); | |
25d0c16f | 940 | |
74637e6f | 941 | rcu_unregister_thread(); |
25d0c16f HZ |
942 | return NULL; |
943 | } |