janitor: do not include qemu-char everywhere
[sdk/emulator/qemu.git] / hmp.c
1 /*
2  * Human Monitor Interface
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  * Contributions after 2012-01-13 are licensed under the terms of the
13  * GNU GPL, version 2 or (at your option) any later version.
14  */
15
16 #include "hmp.h"
17 #include "net.h"
18 #include "qemu-char.h"
19 #include "qemu-option.h"
20 #include "qemu-timer.h"
21 #include "qmp-commands.h"
22 #include "qemu_socket.h"
23 #include "monitor.h"
24 #include "console.h"
25
26 static void hmp_handle_error(Monitor *mon, Error **errp)
27 {
28     if (error_is_set(errp)) {
29         monitor_printf(mon, "%s\n", error_get_pretty(*errp));
30         error_free(*errp);
31     }
32 }
33
34 void hmp_info_name(Monitor *mon)
35 {
36     NameInfo *info;
37
38     info = qmp_query_name(NULL);
39     if (info->has_name) {
40         monitor_printf(mon, "%s\n", info->name);
41     }
42     qapi_free_NameInfo(info);
43 }
44
45 void hmp_info_version(Monitor *mon)
46 {
47     VersionInfo *info;
48
49     info = qmp_query_version(NULL);
50
51     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
52                    info->qemu.major, info->qemu.minor, info->qemu.micro,
53                    info->package);
54
55     qapi_free_VersionInfo(info);
56 }
57
58 void hmp_info_kvm(Monitor *mon)
59 {
60     KvmInfo *info;
61
62     info = qmp_query_kvm(NULL);
63     monitor_printf(mon, "kvm support: ");
64     if (info->present) {
65         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
66     } else {
67         monitor_printf(mon, "not compiled\n");
68     }
69
70     qapi_free_KvmInfo(info);
71 }
72
73 void hmp_info_status(Monitor *mon)
74 {
75     StatusInfo *info;
76
77     info = qmp_query_status(NULL);
78
79     monitor_printf(mon, "VM status: %s%s",
80                    info->running ? "running" : "paused",
81                    info->singlestep ? " (single step mode)" : "");
82
83     if (!info->running && info->status != RUN_STATE_PAUSED) {
84         monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
85     }
86
87     monitor_printf(mon, "\n");
88
89     qapi_free_StatusInfo(info);
90 }
91
92 void hmp_info_uuid(Monitor *mon)
93 {
94     UuidInfo *info;
95
96     info = qmp_query_uuid(NULL);
97     monitor_printf(mon, "%s\n", info->UUID);
98     qapi_free_UuidInfo(info);
99 }
100
101 void hmp_info_chardev(Monitor *mon)
102 {
103     ChardevInfoList *char_info, *info;
104
105     char_info = qmp_query_chardev(NULL);
106     for (info = char_info; info; info = info->next) {
107         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
108                                                  info->value->filename);
109     }
110
111     qapi_free_ChardevInfoList(char_info);
112 }
113
114 void hmp_info_mice(Monitor *mon)
115 {
116     MouseInfoList *mice_list, *mouse;
117
118     mice_list = qmp_query_mice(NULL);
119     if (!mice_list) {
120         monitor_printf(mon, "No mouse devices connected\n");
121         return;
122     }
123
124     for (mouse = mice_list; mouse; mouse = mouse->next) {
125         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
126                        mouse->value->current ? '*' : ' ',
127                        mouse->value->index, mouse->value->name,
128                        mouse->value->absolute ? " (absolute)" : "");
129     }
130
131     qapi_free_MouseInfoList(mice_list);
132 }
133
134 void hmp_info_migrate(Monitor *mon)
135 {
136     MigrationInfo *info;
137     MigrationCapabilityStatusList *caps, *cap;
138
139     info = qmp_query_migrate(NULL);
140     caps = qmp_query_migrate_capabilities(NULL);
141
142     /* do not display parameters during setup */
143     if (info->has_status && caps) {
144         monitor_printf(mon, "capabilities: ");
145         for (cap = caps; cap; cap = cap->next) {
146             monitor_printf(mon, "%s: %s ",
147                            MigrationCapability_lookup[cap->value->capability],
148                            cap->value->state ? "on" : "off");
149         }
150         monitor_printf(mon, "\n");
151     }
152
153     if (info->has_status) {
154         monitor_printf(mon, "Migration status: %s\n", info->status);
155         monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
156                        info->total_time);
157         if (info->has_expected_downtime) {
158             monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
159                            info->expected_downtime);
160         }
161         if (info->has_downtime) {
162             monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
163                            info->downtime);
164         }
165     }
166
167     if (info->has_ram) {
168         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
169                        info->ram->transferred >> 10);
170         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
171                        info->ram->remaining >> 10);
172         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
173                        info->ram->total >> 10);
174         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
175                        info->ram->duplicate);
176         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
177                        info->ram->normal);
178         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
179                        info->ram->normal_bytes >> 10);
180         if (info->ram->dirty_pages_rate) {
181             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
182                            info->ram->dirty_pages_rate);
183         }
184     }
185
186     if (info->has_disk) {
187         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
188                        info->disk->transferred >> 10);
189         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
190                        info->disk->remaining >> 10);
191         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
192                        info->disk->total >> 10);
193     }
194
195     if (info->has_xbzrle_cache) {
196         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
197                        info->xbzrle_cache->cache_size);
198         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
199                        info->xbzrle_cache->bytes >> 10);
200         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
201                        info->xbzrle_cache->pages);
202         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
203                        info->xbzrle_cache->cache_miss);
204         monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
205                        info->xbzrle_cache->overflow);
206     }
207
208     qapi_free_MigrationInfo(info);
209     qapi_free_MigrationCapabilityStatusList(caps);
210 }
211
212 void hmp_info_migrate_capabilities(Monitor *mon)
213 {
214     MigrationCapabilityStatusList *caps, *cap;
215
216     caps = qmp_query_migrate_capabilities(NULL);
217
218     if (caps) {
219         monitor_printf(mon, "capabilities: ");
220         for (cap = caps; cap; cap = cap->next) {
221             monitor_printf(mon, "%s: %s ",
222                            MigrationCapability_lookup[cap->value->capability],
223                            cap->value->state ? "on" : "off");
224         }
225         monitor_printf(mon, "\n");
226     }
227
228     qapi_free_MigrationCapabilityStatusList(caps);
229 }
230
231 void hmp_info_migrate_cache_size(Monitor *mon)
232 {
233     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
234                    qmp_query_migrate_cache_size(NULL) >> 10);
235 }
236
237 void hmp_info_cpus(Monitor *mon)
238 {
239     CpuInfoList *cpu_list, *cpu;
240
241     cpu_list = qmp_query_cpus(NULL);
242
243     for (cpu = cpu_list; cpu; cpu = cpu->next) {
244         int active = ' ';
245
246         if (cpu->value->CPU == monitor_get_cpu_index()) {
247             active = '*';
248         }
249
250         monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
251
252         if (cpu->value->has_pc) {
253             monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
254         }
255         if (cpu->value->has_nip) {
256             monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
257         }
258         if (cpu->value->has_npc) {
259             monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
260         }
261         if (cpu->value->has_PC) {
262             monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
263         }
264
265         if (cpu->value->halted) {
266             monitor_printf(mon, " (halted)");
267         }
268
269         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
270     }
271
272     qapi_free_CpuInfoList(cpu_list);
273 }
274
275 void hmp_info_block(Monitor *mon)
276 {
277     BlockInfoList *block_list, *info;
278
279     block_list = qmp_query_block(NULL);
280
281     for (info = block_list; info; info = info->next) {
282         monitor_printf(mon, "%s: removable=%d",
283                        info->value->device, info->value->removable);
284
285         if (info->value->removable) {
286             monitor_printf(mon, " locked=%d", info->value->locked);
287             monitor_printf(mon, " tray-open=%d", info->value->tray_open);
288         }
289
290         if (info->value->has_io_status) {
291             monitor_printf(mon, " io-status=%s",
292                            BlockDeviceIoStatus_lookup[info->value->io_status]);
293         }
294
295         if (info->value->has_inserted) {
296             monitor_printf(mon, " file=");
297             monitor_print_filename(mon, info->value->inserted->file);
298
299             if (info->value->inserted->has_backing_file) {
300                 monitor_printf(mon, " backing_file=");
301                 monitor_print_filename(mon, info->value->inserted->backing_file);
302                 monitor_printf(mon, " backing_file_depth=%" PRId64,
303                     info->value->inserted->backing_file_depth);
304             }
305             monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
306                            info->value->inserted->ro,
307                            info->value->inserted->drv,
308                            info->value->inserted->encrypted);
309
310             monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
311                             " bps_wr=%" PRId64 " iops=%" PRId64
312                             " iops_rd=%" PRId64 " iops_wr=%" PRId64,
313                             info->value->inserted->bps,
314                             info->value->inserted->bps_rd,
315                             info->value->inserted->bps_wr,
316                             info->value->inserted->iops,
317                             info->value->inserted->iops_rd,
318                             info->value->inserted->iops_wr);
319         } else {
320             monitor_printf(mon, " [not inserted]");
321         }
322
323         monitor_printf(mon, "\n");
324     }
325
326     qapi_free_BlockInfoList(block_list);
327 }
328
329 void hmp_info_blockstats(Monitor *mon)
330 {
331     BlockStatsList *stats_list, *stats;
332
333     stats_list = qmp_query_blockstats(NULL);
334
335     for (stats = stats_list; stats; stats = stats->next) {
336         if (!stats->value->has_device) {
337             continue;
338         }
339
340         monitor_printf(mon, "%s:", stats->value->device);
341         monitor_printf(mon, " rd_bytes=%" PRId64
342                        " wr_bytes=%" PRId64
343                        " rd_operations=%" PRId64
344                        " wr_operations=%" PRId64
345                        " flush_operations=%" PRId64
346                        " wr_total_time_ns=%" PRId64
347                        " rd_total_time_ns=%" PRId64
348                        " flush_total_time_ns=%" PRId64
349                        "\n",
350                        stats->value->stats->rd_bytes,
351                        stats->value->stats->wr_bytes,
352                        stats->value->stats->rd_operations,
353                        stats->value->stats->wr_operations,
354                        stats->value->stats->flush_operations,
355                        stats->value->stats->wr_total_time_ns,
356                        stats->value->stats->rd_total_time_ns,
357                        stats->value->stats->flush_total_time_ns);
358     }
359
360     qapi_free_BlockStatsList(stats_list);
361 }
362
363 void hmp_info_vnc(Monitor *mon)
364 {
365     VncInfo *info;
366     Error *err = NULL;
367     VncClientInfoList *client;
368
369     info = qmp_query_vnc(&err);
370     if (err) {
371         monitor_printf(mon, "%s\n", error_get_pretty(err));
372         error_free(err);
373         return;
374     }
375
376     if (!info->enabled) {
377         monitor_printf(mon, "Server: disabled\n");
378         goto out;
379     }
380
381     monitor_printf(mon, "Server:\n");
382     if (info->has_host && info->has_service) {
383         monitor_printf(mon, "     address: %s:%s\n", info->host, info->service);
384     }
385     if (info->has_auth) {
386         monitor_printf(mon, "        auth: %s\n", info->auth);
387     }
388
389     if (!info->has_clients || info->clients == NULL) {
390         monitor_printf(mon, "Client: none\n");
391     } else {
392         for (client = info->clients; client; client = client->next) {
393             monitor_printf(mon, "Client:\n");
394             monitor_printf(mon, "     address: %s:%s\n",
395                            client->value->host, client->value->service);
396             monitor_printf(mon, "  x509_dname: %s\n",
397                            client->value->x509_dname ?
398                            client->value->x509_dname : "none");
399             monitor_printf(mon, "    username: %s\n",
400                            client->value->has_sasl_username ?
401                            client->value->sasl_username : "none");
402         }
403     }
404
405 out:
406     qapi_free_VncInfo(info);
407 }
408
409 void hmp_info_spice(Monitor *mon)
410 {
411     SpiceChannelList *chan;
412     SpiceInfo *info;
413
414     info = qmp_query_spice(NULL);
415
416     if (!info->enabled) {
417         monitor_printf(mon, "Server: disabled\n");
418         goto out;
419     }
420
421     monitor_printf(mon, "Server:\n");
422     if (info->has_port) {
423         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
424                        info->host, info->port);
425     }
426     if (info->has_tls_port) {
427         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
428                        info->host, info->tls_port);
429     }
430     monitor_printf(mon, "    migrated: %s\n",
431                    info->migrated ? "true" : "false");
432     monitor_printf(mon, "        auth: %s\n", info->auth);
433     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
434     monitor_printf(mon, "  mouse-mode: %s\n",
435                    SpiceQueryMouseMode_lookup[info->mouse_mode]);
436
437     if (!info->has_channels || info->channels == NULL) {
438         monitor_printf(mon, "Channels: none\n");
439     } else {
440         for (chan = info->channels; chan; chan = chan->next) {
441             monitor_printf(mon, "Channel:\n");
442             monitor_printf(mon, "     address: %s:%s%s\n",
443                            chan->value->host, chan->value->port,
444                            chan->value->tls ? " [tls]" : "");
445             monitor_printf(mon, "     session: %" PRId64 "\n",
446                            chan->value->connection_id);
447             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
448                            chan->value->channel_type, chan->value->channel_id);
449         }
450     }
451
452 out:
453     qapi_free_SpiceInfo(info);
454 }
455
456 void hmp_info_balloon(Monitor *mon)
457 {
458     BalloonInfo *info;
459     Error *err = NULL;
460
461     info = qmp_query_balloon(&err);
462     if (err) {
463         monitor_printf(mon, "%s\n", error_get_pretty(err));
464         error_free(err);
465         return;
466     }
467
468     monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
469     if (info->has_mem_swapped_in) {
470         monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
471     }
472     if (info->has_mem_swapped_out) {
473         monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
474     }
475     if (info->has_major_page_faults) {
476         monitor_printf(mon, " major_page_faults=%" PRId64,
477                        info->major_page_faults);
478     }
479     if (info->has_minor_page_faults) {
480         monitor_printf(mon, " minor_page_faults=%" PRId64,
481                        info->minor_page_faults);
482     }
483     if (info->has_free_mem) {
484         monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
485     }
486     if (info->has_total_mem) {
487         monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
488     }
489
490     monitor_printf(mon, "\n");
491
492     qapi_free_BalloonInfo(info);
493 }
494
495 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
496 {
497     PciMemoryRegionList *region;
498
499     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
500     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
501                    dev->slot, dev->function);
502     monitor_printf(mon, "    ");
503
504     if (dev->class_info.has_desc) {
505         monitor_printf(mon, "%s", dev->class_info.desc);
506     } else {
507         monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
508     }
509
510     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
511                    dev->id.vendor, dev->id.device);
512
513     if (dev->has_irq) {
514         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
515     }
516
517     if (dev->has_pci_bridge) {
518         monitor_printf(mon, "      BUS %" PRId64 ".\n",
519                        dev->pci_bridge->bus.number);
520         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
521                        dev->pci_bridge->bus.secondary);
522         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
523                        dev->pci_bridge->bus.subordinate);
524
525         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
526                        dev->pci_bridge->bus.io_range->base,
527                        dev->pci_bridge->bus.io_range->limit);
528
529         monitor_printf(mon,
530                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
531                        dev->pci_bridge->bus.memory_range->base,
532                        dev->pci_bridge->bus.memory_range->limit);
533
534         monitor_printf(mon, "      prefetchable memory range "
535                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
536                        dev->pci_bridge->bus.prefetchable_range->base,
537                        dev->pci_bridge->bus.prefetchable_range->limit);
538     }
539
540     for (region = dev->regions; region; region = region->next) {
541         uint64_t addr, size;
542
543         addr = region->value->address;
544         size = region->value->size;
545
546         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
547
548         if (!strcmp(region->value->type, "io")) {
549             monitor_printf(mon, "I/O at 0x%04" PRIx64
550                                 " [0x%04" PRIx64 "].\n",
551                            addr, addr + size - 1);
552         } else {
553             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
554                                " [0x%08" PRIx64 "].\n",
555                            region->value->mem_type_64 ? 64 : 32,
556                            region->value->prefetch ? " prefetchable" : "",
557                            addr, addr + size - 1);
558         }
559     }
560
561     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
562
563     if (dev->has_pci_bridge) {
564         if (dev->pci_bridge->has_devices) {
565             PciDeviceInfoList *cdev;
566             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
567                 hmp_info_pci_device(mon, cdev->value);
568             }
569         }
570     }
571 }
572
573 void hmp_info_pci(Monitor *mon)
574 {
575     PciInfoList *info_list, *info;
576     Error *err = NULL;
577
578     info_list = qmp_query_pci(&err);
579     if (err) {
580         monitor_printf(mon, "PCI devices not supported\n");
581         error_free(err);
582         return;
583     }
584
585     for (info = info_list; info; info = info->next) {
586         PciDeviceInfoList *dev;
587
588         for (dev = info->value->devices; dev; dev = dev->next) {
589             hmp_info_pci_device(mon, dev->value);
590         }
591     }
592
593     qapi_free_PciInfoList(info_list);
594 }
595
596 void hmp_info_block_jobs(Monitor *mon)
597 {
598     BlockJobInfoList *list;
599     Error *err = NULL;
600
601     list = qmp_query_block_jobs(&err);
602     assert(!err);
603
604     if (!list) {
605         monitor_printf(mon, "No active jobs\n");
606         return;
607     }
608
609     while (list) {
610         if (strcmp(list->value->type, "stream") == 0) {
611             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
612                            " of %" PRId64 " bytes, speed limit %" PRId64
613                            " bytes/s\n",
614                            list->value->device,
615                            list->value->offset,
616                            list->value->len,
617                            list->value->speed);
618         } else {
619             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
620                            " of %" PRId64 " bytes, speed limit %" PRId64
621                            " bytes/s\n",
622                            list->value->type,
623                            list->value->device,
624                            list->value->offset,
625                            list->value->len,
626                            list->value->speed);
627         }
628         list = list->next;
629     }
630 }
631
632 void hmp_quit(Monitor *mon, const QDict *qdict)
633 {
634     monitor_suspend(mon);
635     qmp_quit(NULL);
636 }
637
638 void hmp_stop(Monitor *mon, const QDict *qdict)
639 {
640     qmp_stop(NULL);
641 }
642
643 void hmp_system_reset(Monitor *mon, const QDict *qdict)
644 {
645     qmp_system_reset(NULL);
646 }
647
648 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
649 {
650     qmp_system_powerdown(NULL);
651 }
652
653 void hmp_cpu(Monitor *mon, const QDict *qdict)
654 {
655     int64_t cpu_index;
656
657     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
658             use it are converted to the QAPI */
659     cpu_index = qdict_get_int(qdict, "index");
660     if (monitor_set_cpu(cpu_index) < 0) {
661         monitor_printf(mon, "invalid CPU index\n");
662     }
663 }
664
665 void hmp_memsave(Monitor *mon, const QDict *qdict)
666 {
667     uint32_t size = qdict_get_int(qdict, "size");
668     const char *filename = qdict_get_str(qdict, "filename");
669     uint64_t addr = qdict_get_int(qdict, "val");
670     Error *errp = NULL;
671
672     qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
673     hmp_handle_error(mon, &errp);
674 }
675
676 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
677 {
678     uint32_t size = qdict_get_int(qdict, "size");
679     const char *filename = qdict_get_str(qdict, "filename");
680     uint64_t addr = qdict_get_int(qdict, "val");
681     Error *errp = NULL;
682
683     qmp_pmemsave(addr, size, filename, &errp);
684     hmp_handle_error(mon, &errp);
685 }
686
687 static void hmp_cont_cb(void *opaque, int err)
688 {
689     if (!err) {
690         qmp_cont(NULL);
691     }
692 }
693
694 static bool key_is_missing(const BlockInfo *bdev)
695 {
696     return (bdev->inserted && bdev->inserted->encryption_key_missing);
697 }
698
699 void hmp_cont(Monitor *mon, const QDict *qdict)
700 {
701     BlockInfoList *bdev_list, *bdev;
702     Error *errp = NULL;
703
704     bdev_list = qmp_query_block(NULL);
705     for (bdev = bdev_list; bdev; bdev = bdev->next) {
706         if (key_is_missing(bdev->value)) {
707             monitor_read_block_device_key(mon, bdev->value->device,
708                                           hmp_cont_cb, NULL);
709             goto out;
710         }
711     }
712
713     qmp_cont(&errp);
714     hmp_handle_error(mon, &errp);
715
716 out:
717     qapi_free_BlockInfoList(bdev_list);
718 }
719
720 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
721 {
722     qmp_system_wakeup(NULL);
723 }
724
725 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
726 {
727     Error *errp = NULL;
728
729     qmp_inject_nmi(&errp);
730     hmp_handle_error(mon, &errp);
731 }
732
733 void hmp_set_link(Monitor *mon, const QDict *qdict)
734 {
735     const char *name = qdict_get_str(qdict, "name");
736     int up = qdict_get_bool(qdict, "up");
737     Error *errp = NULL;
738
739     qmp_set_link(name, up, &errp);
740     hmp_handle_error(mon, &errp);
741 }
742
743 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
744 {
745     const char *device = qdict_get_str(qdict, "device");
746     const char *password = qdict_get_str(qdict, "password");
747     Error *errp = NULL;
748
749     qmp_block_passwd(device, password, &errp);
750     hmp_handle_error(mon, &errp);
751 }
752
753 void hmp_balloon(Monitor *mon, const QDict *qdict)
754 {
755     int64_t value = qdict_get_int(qdict, "value");
756     Error *errp = NULL;
757
758     qmp_balloon(value, &errp);
759     if (error_is_set(&errp)) {
760         monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
761         error_free(errp);
762     }
763 }
764
765 void hmp_block_resize(Monitor *mon, const QDict *qdict)
766 {
767     const char *device = qdict_get_str(qdict, "device");
768     int64_t size = qdict_get_int(qdict, "size");
769     Error *errp = NULL;
770
771     qmp_block_resize(device, size, &errp);
772     hmp_handle_error(mon, &errp);
773 }
774
775 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
776 {
777     const char *device = qdict_get_str(qdict, "device");
778     const char *filename = qdict_get_str(qdict, "target");
779     const char *format = qdict_get_try_str(qdict, "format");
780     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
781     int full = qdict_get_try_bool(qdict, "full", 0);
782     enum NewImageMode mode;
783     Error *errp = NULL;
784
785     if (!filename) {
786         error_set(&errp, QERR_MISSING_PARAMETER, "target");
787         hmp_handle_error(mon, &errp);
788         return;
789     }
790
791     if (reuse) {
792         mode = NEW_IMAGE_MODE_EXISTING;
793     } else {
794         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
795     }
796
797     qmp_drive_mirror(device, filename, !!format, format,
798                      full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
799                      true, mode, false, 0,
800                      false, 0, false, 0, &errp);
801     hmp_handle_error(mon, &errp);
802 }
803
804 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
805 {
806     const char *device = qdict_get_str(qdict, "device");
807     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
808     const char *format = qdict_get_try_str(qdict, "format");
809     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
810     enum NewImageMode mode;
811     Error *errp = NULL;
812
813     if (!filename) {
814         /* In the future, if 'snapshot-file' is not specified, the snapshot
815            will be taken internally. Today it's actually required. */
816         error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
817         hmp_handle_error(mon, &errp);
818         return;
819     }
820
821     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
822     qmp_blockdev_snapshot_sync(device, filename, !!format, format,
823                                true, mode, &errp);
824     hmp_handle_error(mon, &errp);
825 }
826
827 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
828 {
829     qmp_migrate_cancel(NULL);
830 }
831
832 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
833 {
834     double value = qdict_get_double(qdict, "value");
835     qmp_migrate_set_downtime(value, NULL);
836 }
837
838 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
839 {
840     int64_t value = qdict_get_int(qdict, "value");
841     Error *err = NULL;
842
843     qmp_migrate_set_cache_size(value, &err);
844     if (err) {
845         monitor_printf(mon, "%s\n", error_get_pretty(err));
846         error_free(err);
847         return;
848     }
849 }
850
851 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
852 {
853     int64_t value = qdict_get_int(qdict, "value");
854     qmp_migrate_set_speed(value, NULL);
855 }
856
857 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
858 {
859     const char *cap = qdict_get_str(qdict, "capability");
860     bool state = qdict_get_bool(qdict, "state");
861     Error *err = NULL;
862     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
863     int i;
864
865     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
866         if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
867             caps->value = g_malloc0(sizeof(*caps->value));
868             caps->value->capability = i;
869             caps->value->state = state;
870             caps->next = NULL;
871             qmp_migrate_set_capabilities(caps, &err);
872             break;
873         }
874     }
875
876     if (i == MIGRATION_CAPABILITY_MAX) {
877         error_set(&err, QERR_INVALID_PARAMETER, cap);
878     }
879
880     qapi_free_MigrationCapabilityStatusList(caps);
881
882     if (err) {
883         monitor_printf(mon, "migrate_set_parameter: %s\n",
884                        error_get_pretty(err));
885         error_free(err);
886     }
887 }
888
889 void hmp_set_password(Monitor *mon, const QDict *qdict)
890 {
891     const char *protocol  = qdict_get_str(qdict, "protocol");
892     const char *password  = qdict_get_str(qdict, "password");
893     const char *connected = qdict_get_try_str(qdict, "connected");
894     Error *err = NULL;
895
896     qmp_set_password(protocol, password, !!connected, connected, &err);
897     hmp_handle_error(mon, &err);
898 }
899
900 void hmp_expire_password(Monitor *mon, const QDict *qdict)
901 {
902     const char *protocol  = qdict_get_str(qdict, "protocol");
903     const char *whenstr = qdict_get_str(qdict, "time");
904     Error *err = NULL;
905
906     qmp_expire_password(protocol, whenstr, &err);
907     hmp_handle_error(mon, &err);
908 }
909
910 void hmp_eject(Monitor *mon, const QDict *qdict)
911 {
912     int force = qdict_get_try_bool(qdict, "force", 0);
913     const char *device = qdict_get_str(qdict, "device");
914     Error *err = NULL;
915
916     qmp_eject(device, true, force, &err);
917     hmp_handle_error(mon, &err);
918 }
919
920 static void hmp_change_read_arg(Monitor *mon, const char *password,
921                                 void *opaque)
922 {
923     qmp_change_vnc_password(password, NULL);
924     monitor_read_command(mon, 1);
925 }
926
927 void hmp_change(Monitor *mon, const QDict *qdict)
928 {
929     const char *device = qdict_get_str(qdict, "device");
930     const char *target = qdict_get_str(qdict, "target");
931     const char *arg = qdict_get_try_str(qdict, "arg");
932     Error *err = NULL;
933
934     if (strcmp(device, "vnc") == 0 &&
935             (strcmp(target, "passwd") == 0 ||
936              strcmp(target, "password") == 0)) {
937         if (!arg) {
938             monitor_read_password(mon, hmp_change_read_arg, NULL);
939             return;
940         }
941     }
942
943     qmp_change(device, target, !!arg, arg, &err);
944     if (error_is_set(&err) &&
945         error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
946         error_free(err);
947         monitor_read_block_device_key(mon, device, NULL, NULL);
948         return;
949     }
950     hmp_handle_error(mon, &err);
951 }
952
953 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
954 {
955     Error *err = NULL;
956
957     qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
958                               qdict_get_int(qdict, "bps"),
959                               qdict_get_int(qdict, "bps_rd"),
960                               qdict_get_int(qdict, "bps_wr"),
961                               qdict_get_int(qdict, "iops"),
962                               qdict_get_int(qdict, "iops_rd"),
963                               qdict_get_int(qdict, "iops_wr"), &err);
964     hmp_handle_error(mon, &err);
965 }
966
967 void hmp_block_stream(Monitor *mon, const QDict *qdict)
968 {
969     Error *error = NULL;
970     const char *device = qdict_get_str(qdict, "device");
971     const char *base = qdict_get_try_str(qdict, "base");
972     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
973
974     qmp_block_stream(device, base != NULL, base,
975                      qdict_haskey(qdict, "speed"), speed,
976                      BLOCKDEV_ON_ERROR_REPORT, true, &error);
977
978     hmp_handle_error(mon, &error);
979 }
980
981 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
982 {
983     Error *error = NULL;
984     const char *device = qdict_get_str(qdict, "device");
985     int64_t value = qdict_get_int(qdict, "speed");
986
987     qmp_block_job_set_speed(device, value, &error);
988
989     hmp_handle_error(mon, &error);
990 }
991
992 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
993 {
994     Error *error = NULL;
995     const char *device = qdict_get_str(qdict, "device");
996     bool force = qdict_get_try_bool(qdict, "force", 0);
997
998     qmp_block_job_cancel(device, true, force, &error);
999
1000     hmp_handle_error(mon, &error);
1001 }
1002
1003 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1004 {
1005     Error *error = NULL;
1006     const char *device = qdict_get_str(qdict, "device");
1007
1008     qmp_block_job_pause(device, &error);
1009
1010     hmp_handle_error(mon, &error);
1011 }
1012
1013 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1014 {
1015     Error *error = NULL;
1016     const char *device = qdict_get_str(qdict, "device");
1017
1018     qmp_block_job_resume(device, &error);
1019
1020     hmp_handle_error(mon, &error);
1021 }
1022
1023 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1024 {
1025     Error *error = NULL;
1026     const char *device = qdict_get_str(qdict, "device");
1027
1028     qmp_block_job_complete(device, &error);
1029
1030     hmp_handle_error(mon, &error);
1031 }
1032
1033 typedef struct MigrationStatus
1034 {
1035     QEMUTimer *timer;
1036     Monitor *mon;
1037     bool is_block_migration;
1038 } MigrationStatus;
1039
1040 static void hmp_migrate_status_cb(void *opaque)
1041 {
1042     MigrationStatus *status = opaque;
1043     MigrationInfo *info;
1044
1045     info = qmp_query_migrate(NULL);
1046     if (!info->has_status || strcmp(info->status, "active") == 0) {
1047         if (info->has_disk) {
1048             int progress;
1049
1050             if (info->disk->remaining) {
1051                 progress = info->disk->transferred * 100 / info->disk->total;
1052             } else {
1053                 progress = 100;
1054             }
1055
1056             monitor_printf(status->mon, "Completed %d %%\r", progress);
1057             monitor_flush(status->mon);
1058         }
1059
1060         qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1061     } else {
1062         if (status->is_block_migration) {
1063             monitor_printf(status->mon, "\n");
1064         }
1065         monitor_resume(status->mon);
1066         qemu_del_timer(status->timer);
1067         g_free(status);
1068     }
1069
1070     qapi_free_MigrationInfo(info);
1071 }
1072
1073 void hmp_migrate(Monitor *mon, const QDict *qdict)
1074 {
1075     int detach = qdict_get_try_bool(qdict, "detach", 0);
1076     int blk = qdict_get_try_bool(qdict, "blk", 0);
1077     int inc = qdict_get_try_bool(qdict, "inc", 0);
1078     const char *uri = qdict_get_str(qdict, "uri");
1079     Error *err = NULL;
1080
1081     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1082     if (err) {
1083         monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1084         error_free(err);
1085         return;
1086     }
1087
1088     if (!detach) {
1089         MigrationStatus *status;
1090
1091         if (monitor_suspend(mon) < 0) {
1092             monitor_printf(mon, "terminal does not allow synchronous "
1093                            "migration, continuing detached\n");
1094             return;
1095         }
1096
1097         status = g_malloc0(sizeof(*status));
1098         status->mon = mon;
1099         status->is_block_migration = blk || inc;
1100         status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1101                                           status);
1102         qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1103     }
1104 }
1105
1106 void hmp_device_del(Monitor *mon, const QDict *qdict)
1107 {
1108     const char *id = qdict_get_str(qdict, "id");
1109     Error *err = NULL;
1110
1111     qmp_device_del(id, &err);
1112     hmp_handle_error(mon, &err);
1113 }
1114
1115 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1116 {
1117     Error *errp = NULL;
1118     int paging = qdict_get_try_bool(qdict, "paging", 0);
1119     const char *file = qdict_get_str(qdict, "filename");
1120     bool has_begin = qdict_haskey(qdict, "begin");
1121     bool has_length = qdict_haskey(qdict, "length");
1122     int64_t begin = 0;
1123     int64_t length = 0;
1124     char *prot;
1125
1126     if (has_begin) {
1127         begin = qdict_get_int(qdict, "begin");
1128     }
1129     if (has_length) {
1130         length = qdict_get_int(qdict, "length");
1131     }
1132
1133     prot = g_strconcat("file:", file, NULL);
1134
1135     qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1136                           &errp);
1137     hmp_handle_error(mon, &errp);
1138     g_free(prot);
1139 }
1140
1141 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1142 {
1143     Error *err = NULL;
1144     QemuOpts *opts;
1145
1146     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1147     if (error_is_set(&err)) {
1148         goto out;
1149     }
1150
1151     netdev_add(opts, &err);
1152     if (error_is_set(&err)) {
1153         qemu_opts_del(opts);
1154     }
1155
1156 out:
1157     hmp_handle_error(mon, &err);
1158 }
1159
1160 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1161 {
1162     const char *id = qdict_get_str(qdict, "id");
1163     Error *err = NULL;
1164
1165     qmp_netdev_del(id, &err);
1166     hmp_handle_error(mon, &err);
1167 }
1168
1169 void hmp_getfd(Monitor *mon, const QDict *qdict)
1170 {
1171     const char *fdname = qdict_get_str(qdict, "fdname");
1172     Error *errp = NULL;
1173
1174     qmp_getfd(fdname, &errp);
1175     hmp_handle_error(mon, &errp);
1176 }
1177
1178 void hmp_closefd(Monitor *mon, const QDict *qdict)
1179 {
1180     const char *fdname = qdict_get_str(qdict, "fdname");
1181     Error *errp = NULL;
1182
1183     qmp_closefd(fdname, &errp);
1184     hmp_handle_error(mon, &errp);
1185 }
1186
1187 void hmp_send_key(Monitor *mon, const QDict *qdict)
1188 {
1189     const char *keys = qdict_get_str(qdict, "keys");
1190     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1191     int has_hold_time = qdict_haskey(qdict, "hold-time");
1192     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1193     Error *err = NULL;
1194     char keyname_buf[16];
1195     char *separator;
1196     int keyname_len;
1197
1198     while (1) {
1199         separator = strchr(keys, '-');
1200         keyname_len = separator ? separator - keys : strlen(keys);
1201         pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1202
1203         /* Be compatible with old interface, convert user inputted "<" */
1204         if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1205             pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1206             keyname_len = 4;
1207         }
1208         keyname_buf[keyname_len] = 0;
1209
1210         keylist = g_malloc0(sizeof(*keylist));
1211         keylist->value = g_malloc0(sizeof(*keylist->value));
1212
1213         if (!head) {
1214             head = keylist;
1215         }
1216         if (tmp) {
1217             tmp->next = keylist;
1218         }
1219         tmp = keylist;
1220
1221         if (strstart(keyname_buf, "0x", NULL)) {
1222             char *endp;
1223             int value = strtoul(keyname_buf, &endp, 0);
1224             if (*endp != '\0') {
1225                 goto err_out;
1226             }
1227             keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1228             keylist->value->number = value;
1229         } else {
1230             int idx = index_from_key(keyname_buf);
1231             if (idx == Q_KEY_CODE_MAX) {
1232                 goto err_out;
1233             }
1234             keylist->value->kind = KEY_VALUE_KIND_QCODE;
1235             keylist->value->qcode = idx;
1236         }
1237
1238         if (!separator) {
1239             break;
1240         }
1241         keys = separator + 1;
1242     }
1243
1244     qmp_send_key(head, has_hold_time, hold_time, &err);
1245     hmp_handle_error(mon, &err);
1246
1247 out:
1248     qapi_free_KeyValueList(head);
1249     return;
1250
1251 err_out:
1252     monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1253     goto out;
1254 }
1255
1256 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1257 {
1258     const char *filename = qdict_get_str(qdict, "filename");
1259     Error *err = NULL;
1260
1261     qmp_screendump(filename, &err);
1262     hmp_handle_error(mon, &err);
1263 }
1264
1265 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1266 {
1267     const char *uri = qdict_get_str(qdict, "uri");
1268     int writable = qdict_get_try_bool(qdict, "writable", 0);
1269     int all = qdict_get_try_bool(qdict, "all", 0);
1270     Error *local_err = NULL;
1271     BlockInfoList *block_list, *info;
1272     SocketAddress *addr;
1273
1274     if (writable && !all) {
1275         error_setg(&local_err, "-w only valid together with -a");
1276         goto exit;
1277     }
1278
1279     /* First check if the address is valid and start the server.  */
1280     addr = socket_parse(uri, &local_err);
1281     if (local_err != NULL) {
1282         goto exit;
1283     }
1284
1285     qmp_nbd_server_start(addr, &local_err);
1286     qapi_free_SocketAddress(addr);
1287     if (local_err != NULL) {
1288         goto exit;
1289     }
1290
1291     if (!all) {
1292         return;
1293     }
1294
1295     /* Then try adding all block devices.  If one fails, close all and
1296      * exit.
1297      */
1298     block_list = qmp_query_block(NULL);
1299
1300     for (info = block_list; info; info = info->next) {
1301         if (!info->value->has_inserted) {
1302             continue;
1303         }
1304
1305         qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1306
1307         if (local_err != NULL) {
1308             qmp_nbd_server_stop(NULL);
1309             break;
1310         }
1311     }
1312
1313     qapi_free_BlockInfoList(block_list);
1314
1315 exit:
1316     hmp_handle_error(mon, &local_err);
1317 }
1318
1319 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1320 {
1321     const char *device = qdict_get_str(qdict, "device");
1322     int writable = qdict_get_try_bool(qdict, "writable", 0);
1323     Error *local_err = NULL;
1324
1325     qmp_nbd_server_add(device, true, writable, &local_err);
1326
1327     if (local_err != NULL) {
1328         hmp_handle_error(mon, &local_err);
1329     }
1330 }
1331
1332 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1333 {
1334     Error *errp = NULL;
1335
1336     qmp_nbd_server_stop(&errp);
1337     hmp_handle_error(mon, &errp);
1338 }