1 /* 2 * QEMU System Emulator 3 * 4 * Copyright (c) 2003-2008 Fabrice Bellard 5 * Copyright (c) 2009-2015 Red Hat Inc 6 * 7 * Authors: 8 * Juan Quintela <quintela@redhat.com> 9 * 10 * Permission is hereby granted, free of charge, to any person obtaining a copy 11 * of this software and associated documentation files (the "Software"), to deal 12 * in the Software without restriction, including without limitation the rights 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 * copies of the Software, and to permit persons to whom the Software is 15 * furnished to do so, subject to the following conditions: 16 * 17 * The above copyright notice and this permission notice shall be included in 18 * all copies or substantial portions of the Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 * THE SOFTWARE. 27 */ 28 29 #include "config-host.h" 30 #include "qemu-common.h" 31 #include "hw/boards.h" 32 #include "hw/hw.h" 33 #include "hw/qdev.h" 34 #include "net/net.h" 35 #include "monitor/monitor.h" 36 #include "sysemu/sysemu.h" 37 #include "qemu/timer.h" 38 #include "audio/audio.h" 39 #include "migration/migration.h" 40 #include "qapi/qmp/qerror.h" 41 #include "qemu/error-report.h" 42 #include "qemu/sockets.h" 43 #include "qemu/queue.h" 44 #include "sysemu/cpus.h" 45 #include "exec/memory.h" 46 #include "qmp-commands.h" 47 #include "trace.h" 48 #include "qemu/iov.h" 49 #include "block/snapshot.h" 50 #include "block/qapi.h" 51 52 53 #ifndef ETH_P_RARP 54 #define ETH_P_RARP 0x8035 55 #endif 56 #define ARP_HTYPE_ETH 0x0001 57 #define ARP_PTYPE_IP 0x0800 58 #define ARP_OP_REQUEST_REV 0x3 59 60 static bool skip_section_footers; 61 62 static int announce_self_create(uint8_t *buf, 63 uint8_t *mac_addr) 64 { 65 /* Ethernet header. */ 66 memset(buf, 0xff, 6); /* destination MAC addr */ 67 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */ 68 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */ 69 70 /* RARP header. */ 71 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */ 72 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */ 73 *(buf + 18) = 6; /* hardware addr length (ethernet) */ 74 *(buf + 19) = 4; /* protocol addr length (IPv4) */ 75 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */ 76 memcpy(buf + 22, mac_addr, 6); /* source hw addr */ 77 memset(buf + 28, 0x00, 4); /* source protocol addr */ 78 memcpy(buf + 32, mac_addr, 6); /* target hw addr */ 79 memset(buf + 38, 0x00, 4); /* target protocol addr */ 80 81 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */ 82 memset(buf + 42, 0x00, 18); 83 84 return 60; /* len (FCS will be added by hardware) */ 85 } 86 87 static void qemu_announce_self_iter(NICState *nic, void *opaque) 88 { 89 uint8_t buf[60]; 90 int len; 91 92 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr)); 93 len = announce_self_create(buf, nic->conf->macaddr.a); 94 95 qemu_send_packet_raw(qemu_get_queue(nic), buf, len); 96 } 97 98 99 static void qemu_announce_self_once(void *opaque) 100 { 101 static int count = SELF_ANNOUNCE_ROUNDS; 102 QEMUTimer *timer = *(QEMUTimer **)opaque; 103 104 qemu_foreach_nic(qemu_announce_self_iter, NULL); 105 106 if (--count) { 107 /* delay 50ms, 150ms, 250ms, ... */ 108 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 109 self_announce_delay(count)); 110 } else { 111 timer_del(timer); 112 timer_free(timer); 113 } 114 } 115 116 void qemu_announce_self(void) 117 { 118 static QEMUTimer *timer; 119 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer); 120 qemu_announce_self_once(&timer); 121 } 122 123 /***********************************************************/ 124 /* savevm/loadvm support */ 125 126 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt, 127 int64_t pos) 128 { 129 int ret; 130 QEMUIOVector qiov; 131 132 qemu_iovec_init_external(&qiov, iov, iovcnt); 133 ret = bdrv_writev_vmstate(opaque, &qiov, pos); 134 if (ret < 0) { 135 return ret; 136 } 137 138 return qiov.size; 139 } 140 141 static int block_put_buffer(void *opaque, const uint8_t *buf, 142 int64_t pos, int size) 143 { 144 bdrv_save_vmstate(opaque, buf, pos, size); 145 return size; 146 } 147 148 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size) 149 { 150 return bdrv_load_vmstate(opaque, buf, pos, size); 151 } 152 153 static int bdrv_fclose(void *opaque) 154 { 155 return bdrv_flush(opaque); 156 } 157 158 static const QEMUFileOps bdrv_read_ops = { 159 .get_buffer = block_get_buffer, 160 .close = bdrv_fclose 161 }; 162 163 static const QEMUFileOps bdrv_write_ops = { 164 .put_buffer = block_put_buffer, 165 .writev_buffer = block_writev_buffer, 166 .close = bdrv_fclose 167 }; 168 169 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable) 170 { 171 if (is_writable) { 172 return qemu_fopen_ops(bs, &bdrv_write_ops); 173 } 174 return qemu_fopen_ops(bs, &bdrv_read_ops); 175 } 176 177 178 /* QEMUFile timer support. 179 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c 180 */ 181 182 void timer_put(QEMUFile *f, QEMUTimer *ts) 183 { 184 uint64_t expire_time; 185 186 expire_time = timer_expire_time_ns(ts); 187 qemu_put_be64(f, expire_time); 188 } 189 190 void timer_get(QEMUFile *f, QEMUTimer *ts) 191 { 192 uint64_t expire_time; 193 194 expire_time = qemu_get_be64(f); 195 if (expire_time != -1) { 196 timer_mod_ns(ts, expire_time); 197 } else { 198 timer_del(ts); 199 } 200 } 201 202 203 /* VMState timer support. 204 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c 205 */ 206 207 static int get_timer(QEMUFile *f, void *pv, size_t size) 208 { 209 QEMUTimer *v = pv; 210 timer_get(f, v); 211 return 0; 212 } 213 214 static void put_timer(QEMUFile *f, void *pv, size_t size) 215 { 216 QEMUTimer *v = pv; 217 timer_put(f, v); 218 } 219 220 const VMStateInfo vmstate_info_timer = { 221 .name = "timer", 222 .get = get_timer, 223 .put = put_timer, 224 }; 225 226 227 typedef struct CompatEntry { 228 char idstr[256]; 229 int instance_id; 230 } CompatEntry; 231 232 typedef struct SaveStateEntry { 233 QTAILQ_ENTRY(SaveStateEntry) entry; 234 char idstr[256]; 235 int instance_id; 236 int alias_id; 237 int version_id; 238 int section_id; 239 SaveVMHandlers *ops; 240 const VMStateDescription *vmsd; 241 void *opaque; 242 CompatEntry *compat; 243 int is_ram; 244 } SaveStateEntry; 245 246 typedef struct SaveState { 247 QTAILQ_HEAD(, SaveStateEntry) handlers; 248 int global_section_id; 249 bool skip_configuration; 250 uint32_t len; 251 const char *name; 252 } SaveState; 253 254 static SaveState savevm_state = { 255 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers), 256 .global_section_id = 0, 257 .skip_configuration = false, 258 }; 259 260 void savevm_skip_configuration(void) 261 { 262 savevm_state.skip_configuration = true; 263 } 264 265 266 static void configuration_pre_save(void *opaque) 267 { 268 SaveState *state = opaque; 269 const char *current_name = MACHINE_GET_CLASS(current_machine)->name; 270 271 state->len = strlen(current_name); 272 state->name = current_name; 273 } 274 275 static int configuration_post_load(void *opaque, int version_id) 276 { 277 SaveState *state = opaque; 278 const char *current_name = MACHINE_GET_CLASS(current_machine)->name; 279 280 if (strncmp(state->name, current_name, state->len) != 0) { 281 error_report("Machine type received is '%s' and local is '%s'", 282 state->name, current_name); 283 return -EINVAL; 284 } 285 return 0; 286 } 287 288 static const VMStateDescription vmstate_configuration = { 289 .name = "configuration", 290 .version_id = 1, 291 .post_load = configuration_post_load, 292 .pre_save = configuration_pre_save, 293 .fields = (VMStateField[]) { 294 VMSTATE_UINT32(len, SaveState), 295 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, 0, len), 296 VMSTATE_END_OF_LIST() 297 }, 298 }; 299 300 static void dump_vmstate_vmsd(FILE *out_file, 301 const VMStateDescription *vmsd, int indent, 302 bool is_subsection); 303 304 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field, 305 int indent) 306 { 307 fprintf(out_file, "%*s{\n", indent, ""); 308 indent += 2; 309 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name); 310 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", 311 field->version_id); 312 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "", 313 field->field_exists ? "true" : "false"); 314 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size); 315 if (field->vmsd != NULL) { 316 fprintf(out_file, ",\n"); 317 dump_vmstate_vmsd(out_file, field->vmsd, indent, false); 318 } 319 fprintf(out_file, "\n%*s}", indent - 2, ""); 320 } 321 322 static void dump_vmstate_vmss(FILE *out_file, 323 const VMStateDescription **subsection, 324 int indent) 325 { 326 if (*subsection != NULL) { 327 dump_vmstate_vmsd(out_file, *subsection, indent, true); 328 } 329 } 330 331 static void dump_vmstate_vmsd(FILE *out_file, 332 const VMStateDescription *vmsd, int indent, 333 bool is_subsection) 334 { 335 if (is_subsection) { 336 fprintf(out_file, "%*s{\n", indent, ""); 337 } else { 338 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description"); 339 } 340 indent += 2; 341 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name); 342 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", 343 vmsd->version_id); 344 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "", 345 vmsd->minimum_version_id); 346 if (vmsd->fields != NULL) { 347 const VMStateField *field = vmsd->fields; 348 bool first; 349 350 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, ""); 351 first = true; 352 while (field->name != NULL) { 353 if (field->flags & VMS_MUST_EXIST) { 354 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */ 355 field++; 356 continue; 357 } 358 if (!first) { 359 fprintf(out_file, ",\n"); 360 } 361 dump_vmstate_vmsf(out_file, field, indent + 2); 362 field++; 363 first = false; 364 } 365 fprintf(out_file, "\n%*s]", indent, ""); 366 } 367 if (vmsd->subsections != NULL) { 368 const VMStateDescription **subsection = vmsd->subsections; 369 bool first; 370 371 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, ""); 372 first = true; 373 while (*subsection != NULL) { 374 if (!first) { 375 fprintf(out_file, ",\n"); 376 } 377 dump_vmstate_vmss(out_file, subsection, indent + 2); 378 subsection++; 379 first = false; 380 } 381 fprintf(out_file, "\n%*s]", indent, ""); 382 } 383 fprintf(out_file, "\n%*s}", indent - 2, ""); 384 } 385 386 static void dump_machine_type(FILE *out_file) 387 { 388 MachineClass *mc; 389 390 mc = MACHINE_GET_CLASS(current_machine); 391 392 fprintf(out_file, " \"vmschkmachine\": {\n"); 393 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name); 394 fprintf(out_file, " },\n"); 395 } 396 397 void dump_vmstate_json_to_file(FILE *out_file) 398 { 399 GSList *list, *elt; 400 bool first; 401 402 fprintf(out_file, "{\n"); 403 dump_machine_type(out_file); 404 405 first = true; 406 list = object_class_get_list(TYPE_DEVICE, true); 407 for (elt = list; elt; elt = elt->next) { 408 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data, 409 TYPE_DEVICE); 410 const char *name; 411 int indent = 2; 412 413 if (!dc->vmsd) { 414 continue; 415 } 416 417 if (!first) { 418 fprintf(out_file, ",\n"); 419 } 420 name = object_class_get_name(OBJECT_CLASS(dc)); 421 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name); 422 indent += 2; 423 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name); 424 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "", 425 dc->vmsd->version_id); 426 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "", 427 dc->vmsd->minimum_version_id); 428 429 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false); 430 431 fprintf(out_file, "\n%*s}", indent - 2, ""); 432 first = false; 433 } 434 fprintf(out_file, "\n}\n"); 435 fclose(out_file); 436 } 437 438 static int calculate_new_instance_id(const char *idstr) 439 { 440 SaveStateEntry *se; 441 int instance_id = 0; 442 443 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 444 if (strcmp(idstr, se->idstr) == 0 445 && instance_id <= se->instance_id) { 446 instance_id = se->instance_id + 1; 447 } 448 } 449 return instance_id; 450 } 451 452 static int calculate_compat_instance_id(const char *idstr) 453 { 454 SaveStateEntry *se; 455 int instance_id = 0; 456 457 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 458 if (!se->compat) { 459 continue; 460 } 461 462 if (strcmp(idstr, se->compat->idstr) == 0 463 && instance_id <= se->compat->instance_id) { 464 instance_id = se->compat->instance_id + 1; 465 } 466 } 467 return instance_id; 468 } 469 470 /* TODO: Individual devices generally have very little idea about the rest 471 of the system, so instance_id should be removed/replaced. 472 Meanwhile pass -1 as instance_id if you do not already have a clearly 473 distinguishing id for all instances of your device class. */ 474 int register_savevm_live(DeviceState *dev, 475 const char *idstr, 476 int instance_id, 477 int version_id, 478 SaveVMHandlers *ops, 479 void *opaque) 480 { 481 SaveStateEntry *se; 482 483 se = g_malloc0(sizeof(SaveStateEntry)); 484 se->version_id = version_id; 485 se->section_id = savevm_state.global_section_id++; 486 se->ops = ops; 487 se->opaque = opaque; 488 se->vmsd = NULL; 489 /* if this is a live_savem then set is_ram */ 490 if (ops->save_live_setup != NULL) { 491 se->is_ram = 1; 492 } 493 494 if (dev) { 495 char *id = qdev_get_dev_path(dev); 496 if (id) { 497 pstrcpy(se->idstr, sizeof(se->idstr), id); 498 pstrcat(se->idstr, sizeof(se->idstr), "/"); 499 g_free(id); 500 501 se->compat = g_malloc0(sizeof(CompatEntry)); 502 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr); 503 se->compat->instance_id = instance_id == -1 ? 504 calculate_compat_instance_id(idstr) : instance_id; 505 instance_id = -1; 506 } 507 } 508 pstrcat(se->idstr, sizeof(se->idstr), idstr); 509 510 if (instance_id == -1) { 511 se->instance_id = calculate_new_instance_id(se->idstr); 512 } else { 513 se->instance_id = instance_id; 514 } 515 assert(!se->compat || se->instance_id == 0); 516 /* add at the end of list */ 517 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry); 518 return 0; 519 } 520 521 int register_savevm(DeviceState *dev, 522 const char *idstr, 523 int instance_id, 524 int version_id, 525 SaveStateHandler *save_state, 526 LoadStateHandler *load_state, 527 void *opaque) 528 { 529 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers)); 530 ops->save_state = save_state; 531 ops->load_state = load_state; 532 return register_savevm_live(dev, idstr, instance_id, version_id, 533 ops, opaque); 534 } 535 536 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque) 537 { 538 SaveStateEntry *se, *new_se; 539 char id[256] = ""; 540 541 if (dev) { 542 char *path = qdev_get_dev_path(dev); 543 if (path) { 544 pstrcpy(id, sizeof(id), path); 545 pstrcat(id, sizeof(id), "/"); 546 g_free(path); 547 } 548 } 549 pstrcat(id, sizeof(id), idstr); 550 551 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { 552 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) { 553 QTAILQ_REMOVE(&savevm_state.handlers, se, entry); 554 if (se->compat) { 555 g_free(se->compat); 556 } 557 g_free(se->ops); 558 g_free(se); 559 } 560 } 561 } 562 563 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id, 564 const VMStateDescription *vmsd, 565 void *opaque, int alias_id, 566 int required_for_version) 567 { 568 SaveStateEntry *se; 569 570 /* If this triggers, alias support can be dropped for the vmsd. */ 571 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id); 572 573 se = g_malloc0(sizeof(SaveStateEntry)); 574 se->version_id = vmsd->version_id; 575 se->section_id = savevm_state.global_section_id++; 576 se->opaque = opaque; 577 se->vmsd = vmsd; 578 se->alias_id = alias_id; 579 580 if (dev) { 581 char *id = qdev_get_dev_path(dev); 582 if (id) { 583 pstrcpy(se->idstr, sizeof(se->idstr), id); 584 pstrcat(se->idstr, sizeof(se->idstr), "/"); 585 g_free(id); 586 587 se->compat = g_malloc0(sizeof(CompatEntry)); 588 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name); 589 se->compat->instance_id = instance_id == -1 ? 590 calculate_compat_instance_id(vmsd->name) : instance_id; 591 instance_id = -1; 592 } 593 } 594 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name); 595 596 if (instance_id == -1) { 597 se->instance_id = calculate_new_instance_id(se->idstr); 598 } else { 599 se->instance_id = instance_id; 600 } 601 assert(!se->compat || se->instance_id == 0); 602 /* add at the end of list */ 603 QTAILQ_INSERT_TAIL(&savevm_state.handlers, se, entry); 604 return 0; 605 } 606 607 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd, 608 void *opaque) 609 { 610 SaveStateEntry *se, *new_se; 611 612 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) { 613 if (se->vmsd == vmsd && se->opaque == opaque) { 614 QTAILQ_REMOVE(&savevm_state.handlers, se, entry); 615 if (se->compat) { 616 g_free(se->compat); 617 } 618 g_free(se); 619 } 620 } 621 } 622 623 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id) 624 { 625 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); 626 if (!se->vmsd) { /* Old style */ 627 return se->ops->load_state(f, se->opaque, version_id); 628 } 629 return vmstate_load_state(f, se->vmsd, se->opaque, version_id); 630 } 631 632 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) 633 { 634 int64_t old_offset, size; 635 636 old_offset = qemu_ftell_fast(f); 637 se->ops->save_state(f, se->opaque); 638 size = qemu_ftell_fast(f) - old_offset; 639 640 if (vmdesc) { 641 json_prop_int(vmdesc, "size", size); 642 json_start_array(vmdesc, "fields"); 643 json_start_object(vmdesc, NULL); 644 json_prop_str(vmdesc, "name", "data"); 645 json_prop_int(vmdesc, "size", size); 646 json_prop_str(vmdesc, "type", "buffer"); 647 json_end_object(vmdesc); 648 json_end_array(vmdesc); 649 } 650 } 651 652 static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc) 653 { 654 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)"); 655 if (!se->vmsd) { 656 vmstate_save_old_style(f, se, vmdesc); 657 return; 658 } 659 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc); 660 } 661 662 void savevm_skip_section_footers(void) 663 { 664 skip_section_footers = true; 665 } 666 667 /* 668 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL) 669 */ 670 static void save_section_header(QEMUFile *f, SaveStateEntry *se, 671 uint8_t section_type) 672 { 673 qemu_put_byte(f, section_type); 674 qemu_put_be32(f, se->section_id); 675 676 if (section_type == QEMU_VM_SECTION_FULL || 677 section_type == QEMU_VM_SECTION_START) { 678 /* ID string */ 679 size_t len = strlen(se->idstr); 680 qemu_put_byte(f, len); 681 qemu_put_buffer(f, (uint8_t *)se->idstr, len); 682 683 qemu_put_be32(f, se->instance_id); 684 qemu_put_be32(f, se->version_id); 685 } 686 } 687 688 /* 689 * Write a footer onto device sections that catches cases misformatted device 690 * sections. 691 */ 692 static void save_section_footer(QEMUFile *f, SaveStateEntry *se) 693 { 694 if (!skip_section_footers) { 695 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER); 696 qemu_put_be32(f, se->section_id); 697 } 698 } 699 700 bool qemu_savevm_state_blocked(Error **errp) 701 { 702 SaveStateEntry *se; 703 704 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 705 if (se->vmsd && se->vmsd->unmigratable) { 706 error_setg(errp, "State blocked by non-migratable device '%s'", 707 se->idstr); 708 return true; 709 } 710 } 711 return false; 712 } 713 714 void qemu_savevm_state_header(QEMUFile *f) 715 { 716 trace_savevm_state_header(); 717 qemu_put_be32(f, QEMU_VM_FILE_MAGIC); 718 qemu_put_be32(f, QEMU_VM_FILE_VERSION); 719 } 720 721 void qemu_savevm_state_begin(QEMUFile *f, 722 const MigrationParams *params) 723 { 724 SaveStateEntry *se; 725 int ret; 726 727 trace_savevm_state_begin(); 728 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 729 if (!se->ops || !se->ops->set_params) { 730 continue; 731 } 732 se->ops->set_params(params, se->opaque); 733 } 734 735 if (!savevm_state.skip_configuration) { 736 qemu_put_byte(f, QEMU_VM_CONFIGURATION); 737 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0); 738 } 739 740 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 741 if (!se->ops || !se->ops->save_live_setup) { 742 continue; 743 } 744 if (se->ops && se->ops->is_active) { 745 if (!se->ops->is_active(se->opaque)) { 746 continue; 747 } 748 } 749 save_section_header(f, se, QEMU_VM_SECTION_START); 750 751 ret = se->ops->save_live_setup(f, se->opaque); 752 save_section_footer(f, se); 753 if (ret < 0) { 754 qemu_file_set_error(f, ret); 755 break; 756 } 757 } 758 } 759 760 /* 761 * this function has three return values: 762 * negative: there was one error, and we have -errno. 763 * 0 : We haven't finished, caller have to go again 764 * 1 : We have finished, we can go to complete phase 765 */ 766 int qemu_savevm_state_iterate(QEMUFile *f) 767 { 768 SaveStateEntry *se; 769 int ret = 1; 770 771 trace_savevm_state_iterate(); 772 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 773 if (!se->ops || !se->ops->save_live_iterate) { 774 continue; 775 } 776 if (se->ops && se->ops->is_active) { 777 if (!se->ops->is_active(se->opaque)) { 778 continue; 779 } 780 } 781 if (qemu_file_rate_limit(f)) { 782 return 0; 783 } 784 trace_savevm_section_start(se->idstr, se->section_id); 785 786 save_section_header(f, se, QEMU_VM_SECTION_PART); 787 788 ret = se->ops->save_live_iterate(f, se->opaque); 789 trace_savevm_section_end(se->idstr, se->section_id, ret); 790 save_section_footer(f, se); 791 792 if (ret < 0) { 793 qemu_file_set_error(f, ret); 794 } 795 if (ret <= 0) { 796 /* Do not proceed to the next vmstate before this one reported 797 completion of the current stage. This serializes the migration 798 and reduces the probability that a faster changing state is 799 synchronized over and over again. */ 800 break; 801 } 802 } 803 return ret; 804 } 805 806 static bool should_send_vmdesc(void) 807 { 808 MachineState *machine = MACHINE(qdev_get_machine()); 809 return !machine->suppress_vmdesc; 810 } 811 812 void qemu_savevm_state_complete(QEMUFile *f) 813 { 814 QJSON *vmdesc; 815 int vmdesc_len; 816 SaveStateEntry *se; 817 int ret; 818 819 trace_savevm_state_complete(); 820 821 cpu_synchronize_all_states(); 822 823 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 824 if (!se->ops || !se->ops->save_live_complete) { 825 continue; 826 } 827 if (se->ops && se->ops->is_active) { 828 if (!se->ops->is_active(se->opaque)) { 829 continue; 830 } 831 } 832 trace_savevm_section_start(se->idstr, se->section_id); 833 834 save_section_header(f, se, QEMU_VM_SECTION_END); 835 836 ret = se->ops->save_live_complete(f, se->opaque); 837 trace_savevm_section_end(se->idstr, se->section_id, ret); 838 save_section_footer(f, se); 839 if (ret < 0) { 840 qemu_file_set_error(f, ret); 841 return; 842 } 843 } 844 845 vmdesc = qjson_new(); 846 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE); 847 json_start_array(vmdesc, "devices"); 848 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 849 850 if ((!se->ops || !se->ops->save_state) && !se->vmsd) { 851 continue; 852 } 853 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { 854 trace_savevm_section_skip(se->idstr, se->section_id); 855 continue; 856 } 857 858 trace_savevm_section_start(se->idstr, se->section_id); 859 860 json_start_object(vmdesc, NULL); 861 json_prop_str(vmdesc, "name", se->idstr); 862 json_prop_int(vmdesc, "instance_id", se->instance_id); 863 864 save_section_header(f, se, QEMU_VM_SECTION_FULL); 865 866 vmstate_save(f, se, vmdesc); 867 868 json_end_object(vmdesc); 869 trace_savevm_section_end(se->idstr, se->section_id, 0); 870 save_section_footer(f, se); 871 } 872 873 qemu_put_byte(f, QEMU_VM_EOF); 874 875 json_end_array(vmdesc); 876 qjson_finish(vmdesc); 877 vmdesc_len = strlen(qjson_get_str(vmdesc)); 878 879 if (should_send_vmdesc()) { 880 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION); 881 qemu_put_be32(f, vmdesc_len); 882 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len); 883 } 884 object_unref(OBJECT(vmdesc)); 885 886 qemu_fflush(f); 887 } 888 889 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size) 890 { 891 SaveStateEntry *se; 892 uint64_t ret = 0; 893 894 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 895 if (!se->ops || !se->ops->save_live_pending) { 896 continue; 897 } 898 if (se->ops && se->ops->is_active) { 899 if (!se->ops->is_active(se->opaque)) { 900 continue; 901 } 902 } 903 ret += se->ops->save_live_pending(f, se->opaque, max_size); 904 } 905 return ret; 906 } 907 908 void qemu_savevm_state_cancel(void) 909 { 910 SaveStateEntry *se; 911 912 trace_savevm_state_cancel(); 913 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 914 if (se->ops && se->ops->cancel) { 915 se->ops->cancel(se->opaque); 916 } 917 } 918 } 919 920 static int qemu_savevm_state(QEMUFile *f, Error **errp) 921 { 922 int ret; 923 MigrationParams params = { 924 .blk = 0, 925 .shared = 0 926 }; 927 928 if (qemu_savevm_state_blocked(errp)) { 929 return -EINVAL; 930 } 931 932 qemu_mutex_unlock_iothread(); 933 qemu_savevm_state_header(f); 934 qemu_savevm_state_begin(f, ¶ms); 935 qemu_mutex_lock_iothread(); 936 937 while (qemu_file_get_error(f) == 0) { 938 if (qemu_savevm_state_iterate(f) > 0) { 939 break; 940 } 941 } 942 943 ret = qemu_file_get_error(f); 944 if (ret == 0) { 945 qemu_savevm_state_complete(f); 946 ret = qemu_file_get_error(f); 947 } 948 if (ret != 0) { 949 qemu_savevm_state_cancel(); 950 error_setg_errno(errp, -ret, "Error while writing VM state"); 951 } 952 return ret; 953 } 954 955 static int qemu_save_device_state(QEMUFile *f) 956 { 957 SaveStateEntry *se; 958 959 qemu_put_be32(f, QEMU_VM_FILE_MAGIC); 960 qemu_put_be32(f, QEMU_VM_FILE_VERSION); 961 962 cpu_synchronize_all_states(); 963 964 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 965 if (se->is_ram) { 966 continue; 967 } 968 if ((!se->ops || !se->ops->save_state) && !se->vmsd) { 969 continue; 970 } 971 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) { 972 continue; 973 } 974 975 save_section_header(f, se, QEMU_VM_SECTION_FULL); 976 977 vmstate_save(f, se, NULL); 978 979 save_section_footer(f, se); 980 } 981 982 qemu_put_byte(f, QEMU_VM_EOF); 983 984 return qemu_file_get_error(f); 985 } 986 987 static SaveStateEntry *find_se(const char *idstr, int instance_id) 988 { 989 SaveStateEntry *se; 990 991 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) { 992 if (!strcmp(se->idstr, idstr) && 993 (instance_id == se->instance_id || 994 instance_id == se->alias_id)) 995 return se; 996 /* Migrating from an older version? */ 997 if (strstr(se->idstr, idstr) && se->compat) { 998 if (!strcmp(se->compat->idstr, idstr) && 999 (instance_id == se->compat->instance_id || 1000 instance_id == se->alias_id)) 1001 return se; 1002 } 1003 } 1004 return NULL; 1005 } 1006 1007 struct LoadStateEntry { 1008 QLIST_ENTRY(LoadStateEntry) entry; 1009 SaveStateEntry *se; 1010 int section_id; 1011 int version_id; 1012 }; 1013 1014 /* 1015 * Read a footer off the wire and check that it matches the expected section 1016 * 1017 * Returns: true if the footer was good 1018 * false if there is a problem (and calls error_report to say why) 1019 */ 1020 static bool check_section_footer(QEMUFile *f, LoadStateEntry *le) 1021 { 1022 uint8_t read_mark; 1023 uint32_t read_section_id; 1024 1025 if (skip_section_footers) { 1026 /* No footer to check */ 1027 return true; 1028 } 1029 1030 read_mark = qemu_get_byte(f); 1031 1032 if (read_mark != QEMU_VM_SECTION_FOOTER) { 1033 error_report("Missing section footer for %s", le->se->idstr); 1034 return false; 1035 } 1036 1037 read_section_id = qemu_get_be32(f); 1038 if (read_section_id != le->section_id) { 1039 error_report("Mismatched section id in footer for %s -" 1040 " read 0x%x expected 0x%x", 1041 le->se->idstr, read_section_id, le->section_id); 1042 return false; 1043 } 1044 1045 /* All good */ 1046 return true; 1047 } 1048 1049 void loadvm_free_handlers(MigrationIncomingState *mis) 1050 { 1051 LoadStateEntry *le, *new_le; 1052 1053 QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) { 1054 QLIST_REMOVE(le, entry); 1055 g_free(le); 1056 } 1057 } 1058 1059 int qemu_loadvm_state(QEMUFile *f) 1060 { 1061 MigrationIncomingState *mis = migration_incoming_get_current(); 1062 Error *local_err = NULL; 1063 uint8_t section_type; 1064 unsigned int v; 1065 int ret; 1066 int file_error_after_eof = -1; 1067 1068 if (qemu_savevm_state_blocked(&local_err)) { 1069 error_report_err(local_err); 1070 return -EINVAL; 1071 } 1072 1073 v = qemu_get_be32(f); 1074 if (v != QEMU_VM_FILE_MAGIC) { 1075 error_report("Not a migration stream"); 1076 return -EINVAL; 1077 } 1078 1079 v = qemu_get_be32(f); 1080 if (v == QEMU_VM_FILE_VERSION_COMPAT) { 1081 error_report("SaveVM v2 format is obsolete and don't work anymore"); 1082 return -ENOTSUP; 1083 } 1084 if (v != QEMU_VM_FILE_VERSION) { 1085 error_report("Unsupported migration stream version"); 1086 return -ENOTSUP; 1087 } 1088 1089 if (!savevm_state.skip_configuration) { 1090 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) { 1091 error_report("Configuration section missing"); 1092 return -EINVAL; 1093 } 1094 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0); 1095 1096 if (ret) { 1097 return ret; 1098 } 1099 } 1100 1101 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) { 1102 uint32_t instance_id, version_id, section_id; 1103 SaveStateEntry *se; 1104 LoadStateEntry *le; 1105 char idstr[256]; 1106 1107 trace_qemu_loadvm_state_section(section_type); 1108 switch (section_type) { 1109 case QEMU_VM_SECTION_START: 1110 case QEMU_VM_SECTION_FULL: 1111 /* Read section start */ 1112 section_id = qemu_get_be32(f); 1113 if (!qemu_get_counted_string(f, idstr)) { 1114 error_report("Unable to read ID string for section %u", 1115 section_id); 1116 return -EINVAL; 1117 } 1118 instance_id = qemu_get_be32(f); 1119 version_id = qemu_get_be32(f); 1120 1121 trace_qemu_loadvm_state_section_startfull(section_id, idstr, 1122 instance_id, version_id); 1123 /* Find savevm section */ 1124 se = find_se(idstr, instance_id); 1125 if (se == NULL) { 1126 error_report("Unknown savevm section or instance '%s' %d", 1127 idstr, instance_id); 1128 ret = -EINVAL; 1129 goto out; 1130 } 1131 1132 /* Validate version */ 1133 if (version_id > se->version_id) { 1134 error_report("savevm: unsupported version %d for '%s' v%d", 1135 version_id, idstr, se->version_id); 1136 ret = -EINVAL; 1137 goto out; 1138 } 1139 1140 /* Add entry */ 1141 le = g_malloc0(sizeof(*le)); 1142 1143 le->se = se; 1144 le->section_id = section_id; 1145 le->version_id = version_id; 1146 QLIST_INSERT_HEAD(&mis->loadvm_handlers, le, entry); 1147 1148 ret = vmstate_load(f, le->se, le->version_id); 1149 if (ret < 0) { 1150 error_report("error while loading state for instance 0x%x of" 1151 " device '%s'", instance_id, idstr); 1152 goto out; 1153 } 1154 if (!check_section_footer(f, le)) { 1155 ret = -EINVAL; 1156 goto out; 1157 } 1158 break; 1159 case QEMU_VM_SECTION_PART: 1160 case QEMU_VM_SECTION_END: 1161 section_id = qemu_get_be32(f); 1162 1163 trace_qemu_loadvm_state_section_partend(section_id); 1164 QLIST_FOREACH(le, &mis->loadvm_handlers, entry) { 1165 if (le->section_id == section_id) { 1166 break; 1167 } 1168 } 1169 if (le == NULL) { 1170 error_report("Unknown savevm section %d", section_id); 1171 ret = -EINVAL; 1172 goto out; 1173 } 1174 1175 ret = vmstate_load(f, le->se, le->version_id); 1176 if (ret < 0) { 1177 error_report("error while loading state section id %d(%s)", 1178 section_id, le->se->idstr); 1179 goto out; 1180 } 1181 if (!check_section_footer(f, le)) { 1182 ret = -EINVAL; 1183 goto out; 1184 } 1185 break; 1186 default: 1187 error_report("Unknown savevm section type %d", section_type); 1188 ret = -EINVAL; 1189 goto out; 1190 } 1191 } 1192 1193 file_error_after_eof = qemu_file_get_error(f); 1194 1195 /* 1196 * Try to read in the VMDESC section as well, so that dumping tools that 1197 * intercept our migration stream have the chance to see it. 1198 */ 1199 1200 /* We've got to be careful; if we don't read the data and just shut the fd 1201 * then the sender can error if we close while it's still sending. 1202 * We also mustn't read data that isn't there; some transports (RDMA) 1203 * will stall waiting for that data when the source has already closed. 1204 */ 1205 if (should_send_vmdesc()) { 1206 uint8_t *buf; 1207 uint32_t size; 1208 section_type = qemu_get_byte(f); 1209 1210 if (section_type != QEMU_VM_VMDESCRIPTION) { 1211 error_report("Expected vmdescription section, but got %d", 1212 section_type); 1213 /* 1214 * It doesn't seem worth failing at this point since 1215 * we apparently have an otherwise valid VM state 1216 */ 1217 } else { 1218 buf = g_malloc(0x1000); 1219 size = qemu_get_be32(f); 1220 1221 while (size > 0) { 1222 uint32_t read_chunk = MIN(size, 0x1000); 1223 qemu_get_buffer(f, buf, read_chunk); 1224 size -= read_chunk; 1225 } 1226 g_free(buf); 1227 } 1228 } 1229 1230 cpu_synchronize_all_post_init(); 1231 1232 ret = 0; 1233 1234 out: 1235 if (ret == 0) { 1236 /* We may not have a VMDESC section, so ignore relative errors */ 1237 ret = file_error_after_eof; 1238 } 1239 1240 return ret; 1241 } 1242 1243 static BlockDriverState *find_vmstate_bs(void) 1244 { 1245 BlockDriverState *bs = NULL; 1246 while ((bs = bdrv_next(bs))) { 1247 if (bdrv_can_snapshot(bs)) { 1248 return bs; 1249 } 1250 } 1251 return NULL; 1252 } 1253 1254 /* 1255 * Deletes snapshots of a given name in all opened images. 1256 */ 1257 static int del_existing_snapshots(Monitor *mon, const char *name) 1258 { 1259 BlockDriverState *bs; 1260 QEMUSnapshotInfo sn1, *snapshot = &sn1; 1261 Error *err = NULL; 1262 1263 bs = NULL; 1264 while ((bs = bdrv_next(bs))) { 1265 if (bdrv_can_snapshot(bs) && 1266 bdrv_snapshot_find(bs, snapshot, name) >= 0) { 1267 bdrv_snapshot_delete_by_id_or_name(bs, name, &err); 1268 if (err) { 1269 monitor_printf(mon, 1270 "Error while deleting snapshot on device '%s':" 1271 " %s\n", 1272 bdrv_get_device_name(bs), 1273 error_get_pretty(err)); 1274 error_free(err); 1275 return -1; 1276 } 1277 } 1278 } 1279 1280 return 0; 1281 } 1282 1283 void hmp_savevm(Monitor *mon, const QDict *qdict) 1284 { 1285 BlockDriverState *bs, *bs1; 1286 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; 1287 int ret; 1288 QEMUFile *f; 1289 int saved_vm_running; 1290 uint64_t vm_state_size; 1291 qemu_timeval tv; 1292 struct tm tm; 1293 const char *name = qdict_get_try_str(qdict, "name"); 1294 Error *local_err = NULL; 1295 1296 /* Verify if there is a device that doesn't support snapshots and is writable */ 1297 bs = NULL; 1298 while ((bs = bdrv_next(bs))) { 1299 1300 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 1301 continue; 1302 } 1303 1304 if (!bdrv_can_snapshot(bs)) { 1305 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n", 1306 bdrv_get_device_name(bs)); 1307 return; 1308 } 1309 } 1310 1311 bs = find_vmstate_bs(); 1312 if (!bs) { 1313 monitor_printf(mon, "No block device can accept snapshots\n"); 1314 return; 1315 } 1316 1317 saved_vm_running = runstate_is_running(); 1318 1319 ret = global_state_store(); 1320 if (ret) { 1321 monitor_printf(mon, "Error saving global state\n"); 1322 return; 1323 } 1324 vm_stop(RUN_STATE_SAVE_VM); 1325 1326 memset(sn, 0, sizeof(*sn)); 1327 1328 /* fill auxiliary fields */ 1329 qemu_gettimeofday(&tv); 1330 sn->date_sec = tv.tv_sec; 1331 sn->date_nsec = tv.tv_usec * 1000; 1332 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); 1333 1334 if (name) { 1335 ret = bdrv_snapshot_find(bs, old_sn, name); 1336 if (ret >= 0) { 1337 pstrcpy(sn->name, sizeof(sn->name), old_sn->name); 1338 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str); 1339 } else { 1340 pstrcpy(sn->name, sizeof(sn->name), name); 1341 } 1342 } else { 1343 /* cast below needed for OpenBSD where tv_sec is still 'long' */ 1344 localtime_r((const time_t *)&tv.tv_sec, &tm); 1345 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm); 1346 } 1347 1348 /* Delete old snapshots of the same name */ 1349 if (name && del_existing_snapshots(mon, name) < 0) { 1350 goto the_end; 1351 } 1352 1353 /* save the VM state */ 1354 f = qemu_fopen_bdrv(bs, 1); 1355 if (!f) { 1356 monitor_printf(mon, "Could not open VM state file\n"); 1357 goto the_end; 1358 } 1359 ret = qemu_savevm_state(f, &local_err); 1360 vm_state_size = qemu_ftell(f); 1361 qemu_fclose(f); 1362 if (ret < 0) { 1363 monitor_printf(mon, "%s\n", error_get_pretty(local_err)); 1364 error_free(local_err); 1365 goto the_end; 1366 } 1367 1368 /* create the snapshots */ 1369 1370 bs1 = NULL; 1371 while ((bs1 = bdrv_next(bs1))) { 1372 if (bdrv_can_snapshot(bs1)) { 1373 /* Write VM state size only to the image that contains the state */ 1374 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0); 1375 ret = bdrv_snapshot_create(bs1, sn); 1376 if (ret < 0) { 1377 monitor_printf(mon, "Error while creating snapshot on '%s'\n", 1378 bdrv_get_device_name(bs1)); 1379 } 1380 } 1381 } 1382 1383 the_end: 1384 if (saved_vm_running) { 1385 vm_start(); 1386 } 1387 } 1388 1389 void qmp_xen_save_devices_state(const char *filename, Error **errp) 1390 { 1391 QEMUFile *f; 1392 int saved_vm_running; 1393 int ret; 1394 1395 saved_vm_running = runstate_is_running(); 1396 vm_stop(RUN_STATE_SAVE_VM); 1397 global_state_store_running(); 1398 1399 f = qemu_fopen(filename, "wb"); 1400 if (!f) { 1401 error_setg_file_open(errp, errno, filename); 1402 goto the_end; 1403 } 1404 ret = qemu_save_device_state(f); 1405 qemu_fclose(f); 1406 if (ret < 0) { 1407 error_setg(errp, QERR_IO_ERROR); 1408 } 1409 1410 the_end: 1411 if (saved_vm_running) { 1412 vm_start(); 1413 } 1414 } 1415 1416 int load_vmstate(const char *name) 1417 { 1418 BlockDriverState *bs, *bs_vm_state; 1419 QEMUSnapshotInfo sn; 1420 QEMUFile *f; 1421 int ret; 1422 1423 bs_vm_state = find_vmstate_bs(); 1424 if (!bs_vm_state) { 1425 error_report("No block device supports snapshots"); 1426 return -ENOTSUP; 1427 } 1428 1429 /* Don't even try to load empty VM states */ 1430 ret = bdrv_snapshot_find(bs_vm_state, &sn, name); 1431 if (ret < 0) { 1432 return ret; 1433 } else if (sn.vm_state_size == 0) { 1434 error_report("This is a disk-only snapshot. Revert to it offline " 1435 "using qemu-img."); 1436 return -EINVAL; 1437 } 1438 1439 /* Verify if there is any device that doesn't support snapshots and is 1440 writable and check if the requested snapshot is available too. */ 1441 bs = NULL; 1442 while ((bs = bdrv_next(bs))) { 1443 1444 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 1445 continue; 1446 } 1447 1448 if (!bdrv_can_snapshot(bs)) { 1449 error_report("Device '%s' is writable but does not support snapshots.", 1450 bdrv_get_device_name(bs)); 1451 return -ENOTSUP; 1452 } 1453 1454 ret = bdrv_snapshot_find(bs, &sn, name); 1455 if (ret < 0) { 1456 error_report("Device '%s' does not have the requested snapshot '%s'", 1457 bdrv_get_device_name(bs), name); 1458 return ret; 1459 } 1460 } 1461 1462 /* Flush all IO requests so they don't interfere with the new state. */ 1463 bdrv_drain_all(); 1464 1465 bs = NULL; 1466 while ((bs = bdrv_next(bs))) { 1467 if (bdrv_can_snapshot(bs)) { 1468 ret = bdrv_snapshot_goto(bs, name); 1469 if (ret < 0) { 1470 error_report("Error %d while activating snapshot '%s' on '%s'", 1471 ret, name, bdrv_get_device_name(bs)); 1472 return ret; 1473 } 1474 } 1475 } 1476 1477 /* restore the VM state */ 1478 f = qemu_fopen_bdrv(bs_vm_state, 0); 1479 if (!f) { 1480 error_report("Could not open VM state file"); 1481 return -EINVAL; 1482 } 1483 1484 qemu_system_reset(VMRESET_SILENT); 1485 migration_incoming_state_new(f); 1486 ret = qemu_loadvm_state(f); 1487 1488 qemu_fclose(f); 1489 migration_incoming_state_destroy(); 1490 if (ret < 0) { 1491 error_report("Error %d while loading VM state", ret); 1492 return ret; 1493 } 1494 1495 return 0; 1496 } 1497 1498 void hmp_delvm(Monitor *mon, const QDict *qdict) 1499 { 1500 BlockDriverState *bs; 1501 Error *err; 1502 const char *name = qdict_get_str(qdict, "name"); 1503 1504 if (!find_vmstate_bs()) { 1505 monitor_printf(mon, "No block device supports snapshots\n"); 1506 return; 1507 } 1508 1509 bs = NULL; 1510 while ((bs = bdrv_next(bs))) { 1511 if (bdrv_can_snapshot(bs)) { 1512 err = NULL; 1513 bdrv_snapshot_delete_by_id_or_name(bs, name, &err); 1514 if (err) { 1515 monitor_printf(mon, 1516 "Error while deleting snapshot on device '%s':" 1517 " %s\n", 1518 bdrv_get_device_name(bs), 1519 error_get_pretty(err)); 1520 error_free(err); 1521 } 1522 } 1523 } 1524 } 1525 1526 void hmp_info_snapshots(Monitor *mon, const QDict *qdict) 1527 { 1528 BlockDriverState *bs, *bs1; 1529 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s; 1530 int nb_sns, i, ret, available; 1531 int total; 1532 int *available_snapshots; 1533 1534 bs = find_vmstate_bs(); 1535 if (!bs) { 1536 monitor_printf(mon, "No available block device supports snapshots\n"); 1537 return; 1538 } 1539 1540 nb_sns = bdrv_snapshot_list(bs, &sn_tab); 1541 if (nb_sns < 0) { 1542 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns); 1543 return; 1544 } 1545 1546 if (nb_sns == 0) { 1547 monitor_printf(mon, "There is no snapshot available.\n"); 1548 return; 1549 } 1550 1551 available_snapshots = g_malloc0(sizeof(int) * nb_sns); 1552 total = 0; 1553 for (i = 0; i < nb_sns; i++) { 1554 sn = &sn_tab[i]; 1555 available = 1; 1556 bs1 = NULL; 1557 1558 while ((bs1 = bdrv_next(bs1))) { 1559 if (bdrv_can_snapshot(bs1) && bs1 != bs) { 1560 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str); 1561 if (ret < 0) { 1562 available = 0; 1563 break; 1564 } 1565 } 1566 } 1567 1568 if (available) { 1569 available_snapshots[total] = i; 1570 total++; 1571 } 1572 } 1573 1574 if (total > 0) { 1575 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL); 1576 monitor_printf(mon, "\n"); 1577 for (i = 0; i < total; i++) { 1578 sn = &sn_tab[available_snapshots[i]]; 1579 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn); 1580 monitor_printf(mon, "\n"); 1581 } 1582 } else { 1583 monitor_printf(mon, "There is no suitable snapshot available\n"); 1584 } 1585 1586 g_free(sn_tab); 1587 g_free(available_snapshots); 1588 1589 } 1590 1591 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev) 1592 { 1593 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK, 1594 memory_region_name(mr), dev); 1595 } 1596 1597 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev) 1598 { 1599 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK); 1600 } 1601 1602 void vmstate_register_ram_global(MemoryRegion *mr) 1603 { 1604 vmstate_register_ram(mr, NULL); 1605 } 1606