OSDN Git Service

target/openrisc: Reorg tlb lookup
[qmiga/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 "qemu/osdep.h"
17 #include "hmp.h"
18 #include "net/net.h"
19 #include "net/eth.h"
20 #include "chardev/char.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/sysemu.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/timer.h"
26 #include "qemu/sockets.h"
27 #include "monitor/monitor.h"
28 #include "monitor/qdev.h"
29 #include "qapi/error.h"
30 #include "qapi/opts-visitor.h"
31 #include "qapi/qapi-builtin-visit.h"
32 #include "qapi/qapi-commands-block.h"
33 #include "qapi/qapi-commands-char.h"
34 #include "qapi/qapi-commands-migration.h"
35 #include "qapi/qapi-commands-misc.h"
36 #include "qapi/qapi-commands-net.h"
37 #include "qapi/qapi-commands-rocker.h"
38 #include "qapi/qapi-commands-run-state.h"
39 #include "qapi/qapi-commands-tpm.h"
40 #include "qapi/qapi-commands-ui.h"
41 #include "qapi/qmp/qdict.h"
42 #include "qapi/qmp/qerror.h"
43 #include "qapi/string-input-visitor.h"
44 #include "qapi/string-output-visitor.h"
45 #include "qom/object_interfaces.h"
46 #include "ui/console.h"
47 #include "block/nbd.h"
48 #include "block/qapi.h"
49 #include "qemu-io.h"
50 #include "qemu/cutils.h"
51 #include "qemu/error-report.h"
52 #include "exec/ramlist.h"
53 #include "hw/intc/intc.h"
54 #include "migration/snapshot.h"
55 #include "migration/misc.h"
56
57 #ifdef CONFIG_SPICE
58 #include <spice/enums.h>
59 #endif
60
61 static void hmp_handle_error(Monitor *mon, Error **errp)
62 {
63     assert(errp);
64     if (*errp) {
65         error_report_err(*errp);
66     }
67 }
68
69 void hmp_info_name(Monitor *mon, const QDict *qdict)
70 {
71     NameInfo *info;
72
73     info = qmp_query_name(NULL);
74     if (info->has_name) {
75         monitor_printf(mon, "%s\n", info->name);
76     }
77     qapi_free_NameInfo(info);
78 }
79
80 void hmp_info_version(Monitor *mon, const QDict *qdict)
81 {
82     VersionInfo *info;
83
84     info = qmp_query_version(NULL);
85
86     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
87                    info->qemu->major, info->qemu->minor, info->qemu->micro,
88                    info->package);
89
90     qapi_free_VersionInfo(info);
91 }
92
93 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
94 {
95     KvmInfo *info;
96
97     info = qmp_query_kvm(NULL);
98     monitor_printf(mon, "kvm support: ");
99     if (info->present) {
100         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
101     } else {
102         monitor_printf(mon, "not compiled\n");
103     }
104
105     qapi_free_KvmInfo(info);
106 }
107
108 void hmp_info_status(Monitor *mon, const QDict *qdict)
109 {
110     StatusInfo *info;
111
112     info = qmp_query_status(NULL);
113
114     monitor_printf(mon, "VM status: %s%s",
115                    info->running ? "running" : "paused",
116                    info->singlestep ? " (single step mode)" : "");
117
118     if (!info->running && info->status != RUN_STATE_PAUSED) {
119         monitor_printf(mon, " (%s)", RunState_str(info->status));
120     }
121
122     monitor_printf(mon, "\n");
123
124     qapi_free_StatusInfo(info);
125 }
126
127 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
128 {
129     UuidInfo *info;
130
131     info = qmp_query_uuid(NULL);
132     monitor_printf(mon, "%s\n", info->UUID);
133     qapi_free_UuidInfo(info);
134 }
135
136 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
137 {
138     ChardevInfoList *char_info, *info;
139
140     char_info = qmp_query_chardev(NULL);
141     for (info = char_info; info; info = info->next) {
142         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
143                                                  info->value->filename);
144     }
145
146     qapi_free_ChardevInfoList(char_info);
147 }
148
149 void hmp_info_mice(Monitor *mon, const QDict *qdict)
150 {
151     MouseInfoList *mice_list, *mouse;
152
153     mice_list = qmp_query_mice(NULL);
154     if (!mice_list) {
155         monitor_printf(mon, "No mouse devices connected\n");
156         return;
157     }
158
159     for (mouse = mice_list; mouse; mouse = mouse->next) {
160         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
161                        mouse->value->current ? '*' : ' ',
162                        mouse->value->index, mouse->value->name,
163                        mouse->value->absolute ? " (absolute)" : "");
164     }
165
166     qapi_free_MouseInfoList(mice_list);
167 }
168
169 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
170 {
171     MigrationInfo *info;
172     MigrationCapabilityStatusList *caps, *cap;
173
174     info = qmp_query_migrate(NULL);
175     caps = qmp_query_migrate_capabilities(NULL);
176
177     migration_global_dump(mon);
178
179     /* do not display parameters during setup */
180     if (info->has_status && caps) {
181         monitor_printf(mon, "capabilities: ");
182         for (cap = caps; cap; cap = cap->next) {
183             monitor_printf(mon, "%s: %s ",
184                            MigrationCapability_str(cap->value->capability),
185                            cap->value->state ? "on" : "off");
186         }
187         monitor_printf(mon, "\n");
188     }
189
190     if (info->has_status) {
191         monitor_printf(mon, "Migration status: %s",
192                        MigrationStatus_str(info->status));
193         if (info->status == MIGRATION_STATUS_FAILED &&
194             info->has_error_desc) {
195             monitor_printf(mon, " (%s)\n", info->error_desc);
196         } else {
197             monitor_printf(mon, "\n");
198         }
199
200         monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
201                        info->total_time);
202         if (info->has_expected_downtime) {
203             monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
204                            info->expected_downtime);
205         }
206         if (info->has_downtime) {
207             monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
208                            info->downtime);
209         }
210         if (info->has_setup_time) {
211             monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
212                            info->setup_time);
213         }
214     }
215
216     if (info->has_ram) {
217         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
218                        info->ram->transferred >> 10);
219         monitor_printf(mon, "throughput: %0.2f mbps\n",
220                        info->ram->mbps);
221         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
222                        info->ram->remaining >> 10);
223         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
224                        info->ram->total >> 10);
225         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
226                        info->ram->duplicate);
227         monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
228                        info->ram->skipped);
229         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
230                        info->ram->normal);
231         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
232                        info->ram->normal_bytes >> 10);
233         monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
234                        info->ram->dirty_sync_count);
235         monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
236                        info->ram->page_size >> 10);
237         monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
238                        info->ram->multifd_bytes >> 10);
239
240         if (info->ram->dirty_pages_rate) {
241             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
242                            info->ram->dirty_pages_rate);
243         }
244         if (info->ram->postcopy_requests) {
245             monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
246                            info->ram->postcopy_requests);
247         }
248     }
249
250     if (info->has_disk) {
251         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
252                        info->disk->transferred >> 10);
253         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
254                        info->disk->remaining >> 10);
255         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
256                        info->disk->total >> 10);
257     }
258
259     if (info->has_xbzrle_cache) {
260         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
261                        info->xbzrle_cache->cache_size);
262         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
263                        info->xbzrle_cache->bytes >> 10);
264         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
265                        info->xbzrle_cache->pages);
266         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
267                        info->xbzrle_cache->cache_miss);
268         monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
269                        info->xbzrle_cache->cache_miss_rate);
270         monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
271                        info->xbzrle_cache->overflow);
272     }
273
274     if (info->has_cpu_throttle_percentage) {
275         monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
276                        info->cpu_throttle_percentage);
277     }
278
279     if (info->has_postcopy_blocktime) {
280         monitor_printf(mon, "postcopy blocktime: %u\n",
281                        info->postcopy_blocktime);
282     }
283
284     if (info->has_postcopy_vcpu_blocktime) {
285         Visitor *v;
286         char *str;
287         v = string_output_visitor_new(false, &str);
288         visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime, NULL);
289         visit_complete(v, &str);
290         monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
291         g_free(str);
292         visit_free(v);
293     }
294     qapi_free_MigrationInfo(info);
295     qapi_free_MigrationCapabilityStatusList(caps);
296 }
297
298 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
299 {
300     MigrationCapabilityStatusList *caps, *cap;
301
302     caps = qmp_query_migrate_capabilities(NULL);
303
304     if (caps) {
305         for (cap = caps; cap; cap = cap->next) {
306             monitor_printf(mon, "%s: %s\n",
307                            MigrationCapability_str(cap->value->capability),
308                            cap->value->state ? "on" : "off");
309         }
310     }
311
312     qapi_free_MigrationCapabilityStatusList(caps);
313 }
314
315 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
316 {
317     MigrationParameters *params;
318
319     params = qmp_query_migrate_parameters(NULL);
320
321     if (params) {
322         assert(params->has_compress_level);
323         monitor_printf(mon, "%s: %u\n",
324             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
325             params->compress_level);
326         assert(params->has_compress_threads);
327         monitor_printf(mon, "%s: %u\n",
328             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
329             params->compress_threads);
330         assert(params->has_decompress_threads);
331         monitor_printf(mon, "%s: %u\n",
332             MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
333             params->decompress_threads);
334         assert(params->has_cpu_throttle_initial);
335         monitor_printf(mon, "%s: %u\n",
336             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
337             params->cpu_throttle_initial);
338         assert(params->has_cpu_throttle_increment);
339         monitor_printf(mon, "%s: %u\n",
340             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
341             params->cpu_throttle_increment);
342         assert(params->has_tls_creds);
343         monitor_printf(mon, "%s: '%s'\n",
344             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
345             params->tls_creds);
346         assert(params->has_tls_hostname);
347         monitor_printf(mon, "%s: '%s'\n",
348             MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
349             params->tls_hostname);
350         assert(params->has_max_bandwidth);
351         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
352             MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
353             params->max_bandwidth);
354         assert(params->has_downtime_limit);
355         monitor_printf(mon, "%s: %" PRIu64 " milliseconds\n",
356             MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
357             params->downtime_limit);
358         assert(params->has_x_checkpoint_delay);
359         monitor_printf(mon, "%s: %u\n",
360             MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
361             params->x_checkpoint_delay);
362         assert(params->has_block_incremental);
363         monitor_printf(mon, "%s: %s\n",
364             MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
365             params->block_incremental ? "on" : "off");
366         monitor_printf(mon, "%s: %u\n",
367             MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_CHANNELS),
368             params->x_multifd_channels);
369         monitor_printf(mon, "%s: %u\n",
370             MigrationParameter_str(MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT),
371             params->x_multifd_page_count);
372         monitor_printf(mon, "%s: %" PRIu64 "\n",
373             MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
374             params->xbzrle_cache_size);
375         monitor_printf(mon, "%s: %" PRIu64 "\n",
376             MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
377             params->max_postcopy_bandwidth);
378     }
379
380     qapi_free_MigrationParameters(params);
381 }
382
383 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
384 {
385     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
386                    qmp_query_migrate_cache_size(NULL) >> 10);
387 }
388
389 void hmp_info_cpus(Monitor *mon, const QDict *qdict)
390 {
391     CpuInfoFastList *cpu_list, *cpu;
392
393     cpu_list = qmp_query_cpus_fast(NULL);
394
395     for (cpu = cpu_list; cpu; cpu = cpu->next) {
396         int active = ' ';
397
398         if (cpu->value->cpu_index == monitor_get_cpu_index()) {
399             active = '*';
400         }
401
402         monitor_printf(mon, "%c CPU #%" PRId64 ":", active,
403                        cpu->value->cpu_index);
404         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
405     }
406
407     qapi_free_CpuInfoFastList(cpu_list);
408 }
409
410 static void print_block_info(Monitor *mon, BlockInfo *info,
411                              BlockDeviceInfo *inserted, bool verbose)
412 {
413     ImageInfo *image_info;
414
415     assert(!info || !info->has_inserted || info->inserted == inserted);
416
417     if (info && *info->device) {
418         monitor_printf(mon, "%s", info->device);
419         if (inserted && inserted->has_node_name) {
420             monitor_printf(mon, " (%s)", inserted->node_name);
421         }
422     } else {
423         assert(info || inserted);
424         monitor_printf(mon, "%s",
425                        inserted && inserted->has_node_name ? inserted->node_name
426                        : info && info->has_qdev ? info->qdev
427                        : "<anonymous>");
428     }
429
430     if (inserted) {
431         monitor_printf(mon, ": %s (%s%s%s)\n",
432                        inserted->file,
433                        inserted->drv,
434                        inserted->ro ? ", read-only" : "",
435                        inserted->encrypted ? ", encrypted" : "");
436     } else {
437         monitor_printf(mon, ": [not inserted]\n");
438     }
439
440     if (info) {
441         if (info->has_qdev) {
442             monitor_printf(mon, "    Attached to:      %s\n", info->qdev);
443         }
444         if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
445             monitor_printf(mon, "    I/O status:       %s\n",
446                            BlockDeviceIoStatus_str(info->io_status));
447         }
448
449         if (info->removable) {
450             monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
451                            info->locked ? "" : "not ",
452                            info->tray_open ? "open" : "closed");
453         }
454     }
455
456
457     if (!inserted) {
458         return;
459     }
460
461     monitor_printf(mon, "    Cache mode:       %s%s%s\n",
462                    inserted->cache->writeback ? "writeback" : "writethrough",
463                    inserted->cache->direct ? ", direct" : "",
464                    inserted->cache->no_flush ? ", ignore flushes" : "");
465
466     if (inserted->has_backing_file) {
467         monitor_printf(mon,
468                        "    Backing file:     %s "
469                        "(chain depth: %" PRId64 ")\n",
470                        inserted->backing_file,
471                        inserted->backing_file_depth);
472     }
473
474     if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
475         monitor_printf(mon, "    Detect zeroes:    %s\n",
476                 BlockdevDetectZeroesOptions_str(inserted->detect_zeroes));
477     }
478
479     if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
480         inserted->iops || inserted->iops_rd || inserted->iops_wr)
481     {
482         monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
483                         " bps_rd=%" PRId64  " bps_wr=%" PRId64
484                         " bps_max=%" PRId64
485                         " bps_rd_max=%" PRId64
486                         " bps_wr_max=%" PRId64
487                         " iops=%" PRId64 " iops_rd=%" PRId64
488                         " iops_wr=%" PRId64
489                         " iops_max=%" PRId64
490                         " iops_rd_max=%" PRId64
491                         " iops_wr_max=%" PRId64
492                         " iops_size=%" PRId64
493                         " group=%s\n",
494                         inserted->bps,
495                         inserted->bps_rd,
496                         inserted->bps_wr,
497                         inserted->bps_max,
498                         inserted->bps_rd_max,
499                         inserted->bps_wr_max,
500                         inserted->iops,
501                         inserted->iops_rd,
502                         inserted->iops_wr,
503                         inserted->iops_max,
504                         inserted->iops_rd_max,
505                         inserted->iops_wr_max,
506                         inserted->iops_size,
507                         inserted->group);
508     }
509
510     if (verbose) {
511         monitor_printf(mon, "\nImages:\n");
512         image_info = inserted->image;
513         while (1) {
514                 bdrv_image_info_dump((fprintf_function)monitor_printf,
515                                      mon, image_info);
516             if (image_info->has_backing_image) {
517                 image_info = image_info->backing_image;
518             } else {
519                 break;
520             }
521         }
522     }
523 }
524
525 void hmp_info_block(Monitor *mon, const QDict *qdict)
526 {
527     BlockInfoList *block_list, *info;
528     BlockDeviceInfoList *blockdev_list, *blockdev;
529     const char *device = qdict_get_try_str(qdict, "device");
530     bool verbose = qdict_get_try_bool(qdict, "verbose", false);
531     bool nodes = qdict_get_try_bool(qdict, "nodes", false);
532     bool printed = false;
533
534     /* Print BlockBackend information */
535     if (!nodes) {
536         block_list = qmp_query_block(NULL);
537     } else {
538         block_list = NULL;
539     }
540
541     for (info = block_list; info; info = info->next) {
542         if (device && strcmp(device, info->value->device)) {
543             continue;
544         }
545
546         if (info != block_list) {
547             monitor_printf(mon, "\n");
548         }
549
550         print_block_info(mon, info->value, info->value->has_inserted
551                                            ? info->value->inserted : NULL,
552                          verbose);
553         printed = true;
554     }
555
556     qapi_free_BlockInfoList(block_list);
557
558     if ((!device && !nodes) || printed) {
559         return;
560     }
561
562     /* Print node information */
563     blockdev_list = qmp_query_named_block_nodes(NULL);
564     for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
565         assert(blockdev->value->has_node_name);
566         if (device && strcmp(device, blockdev->value->node_name)) {
567             continue;
568         }
569
570         if (blockdev != blockdev_list) {
571             monitor_printf(mon, "\n");
572         }
573
574         print_block_info(mon, NULL, blockdev->value, verbose);
575     }
576     qapi_free_BlockDeviceInfoList(blockdev_list);
577 }
578
579 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
580 {
581     BlockStatsList *stats_list, *stats;
582
583     stats_list = qmp_query_blockstats(false, false, NULL);
584
585     for (stats = stats_list; stats; stats = stats->next) {
586         if (!stats->value->has_device) {
587             continue;
588         }
589
590         monitor_printf(mon, "%s:", stats->value->device);
591         monitor_printf(mon, " rd_bytes=%" PRId64
592                        " wr_bytes=%" PRId64
593                        " rd_operations=%" PRId64
594                        " wr_operations=%" PRId64
595                        " flush_operations=%" PRId64
596                        " wr_total_time_ns=%" PRId64
597                        " rd_total_time_ns=%" PRId64
598                        " flush_total_time_ns=%" PRId64
599                        " rd_merged=%" PRId64
600                        " wr_merged=%" PRId64
601                        " idle_time_ns=%" PRId64
602                        "\n",
603                        stats->value->stats->rd_bytes,
604                        stats->value->stats->wr_bytes,
605                        stats->value->stats->rd_operations,
606                        stats->value->stats->wr_operations,
607                        stats->value->stats->flush_operations,
608                        stats->value->stats->wr_total_time_ns,
609                        stats->value->stats->rd_total_time_ns,
610                        stats->value->stats->flush_total_time_ns,
611                        stats->value->stats->rd_merged,
612                        stats->value->stats->wr_merged,
613                        stats->value->stats->idle_time_ns);
614     }
615
616     qapi_free_BlockStatsList(stats_list);
617 }
618
619 /* Helper for hmp_info_vnc_clients, _servers */
620 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
621                                   const char *name)
622 {
623     monitor_printf(mon, "  %s: %s:%s (%s%s)\n",
624                    name,
625                    info->host,
626                    info->service,
627                    NetworkAddressFamily_str(info->family),
628                    info->websocket ? " (Websocket)" : "");
629 }
630
631 /* Helper displaying and auth and crypt info */
632 static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
633                                    VncPrimaryAuth auth,
634                                    VncVencryptSubAuth *vencrypt)
635 {
636     monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
637                    VncPrimaryAuth_str(auth),
638                    vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
639 }
640
641 static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
642 {
643     while (client) {
644         VncClientInfo *cinfo = client->value;
645
646         hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
647         monitor_printf(mon, "    x509_dname: %s\n",
648                        cinfo->has_x509_dname ?
649                        cinfo->x509_dname : "none");
650         monitor_printf(mon, "    sasl_username: %s\n",
651                        cinfo->has_sasl_username ?
652                        cinfo->sasl_username : "none");
653
654         client = client->next;
655     }
656 }
657
658 static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
659 {
660     while (server) {
661         VncServerInfo2 *sinfo = server->value;
662         hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
663         hmp_info_vnc_authcrypt(mon, "    ", sinfo->auth,
664                                sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
665         server = server->next;
666     }
667 }
668
669 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
670 {
671     VncInfo2List *info2l;
672     Error *err = NULL;
673
674     info2l = qmp_query_vnc_servers(&err);
675     if (err) {
676         hmp_handle_error(mon, &err);
677         return;
678     }
679     if (!info2l) {
680         monitor_printf(mon, "None\n");
681         return;
682     }
683
684     while (info2l) {
685         VncInfo2 *info = info2l->value;
686         monitor_printf(mon, "%s:\n", info->id);
687         hmp_info_vnc_servers(mon, info->server);
688         hmp_info_vnc_clients(mon, info->clients);
689         if (!info->server) {
690             /* The server entry displays its auth, we only
691              * need to display in the case of 'reverse' connections
692              * where there's no server.
693              */
694             hmp_info_vnc_authcrypt(mon, "  ", info->auth,
695                                info->has_vencrypt ? &info->vencrypt : NULL);
696         }
697         if (info->has_display) {
698             monitor_printf(mon, "  Display: %s\n", info->display);
699         }
700         info2l = info2l->next;
701     }
702
703     qapi_free_VncInfo2List(info2l);
704
705 }
706
707 #ifdef CONFIG_SPICE
708 void hmp_info_spice(Monitor *mon, const QDict *qdict)
709 {
710     SpiceChannelList *chan;
711     SpiceInfo *info;
712     const char *channel_name;
713     const char * const channel_names[] = {
714         [SPICE_CHANNEL_MAIN] = "main",
715         [SPICE_CHANNEL_DISPLAY] = "display",
716         [SPICE_CHANNEL_INPUTS] = "inputs",
717         [SPICE_CHANNEL_CURSOR] = "cursor",
718         [SPICE_CHANNEL_PLAYBACK] = "playback",
719         [SPICE_CHANNEL_RECORD] = "record",
720         [SPICE_CHANNEL_TUNNEL] = "tunnel",
721         [SPICE_CHANNEL_SMARTCARD] = "smartcard",
722         [SPICE_CHANNEL_USBREDIR] = "usbredir",
723         [SPICE_CHANNEL_PORT] = "port",
724 #if 0
725         /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
726          * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
727          * as quick fix for build failures with older versions. */
728         [SPICE_CHANNEL_WEBDAV] = "webdav",
729 #endif
730     };
731
732     info = qmp_query_spice(NULL);
733
734     if (!info->enabled) {
735         monitor_printf(mon, "Server: disabled\n");
736         goto out;
737     }
738
739     monitor_printf(mon, "Server:\n");
740     if (info->has_port) {
741         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
742                        info->host, info->port);
743     }
744     if (info->has_tls_port) {
745         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
746                        info->host, info->tls_port);
747     }
748     monitor_printf(mon, "    migrated: %s\n",
749                    info->migrated ? "true" : "false");
750     monitor_printf(mon, "        auth: %s\n", info->auth);
751     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
752     monitor_printf(mon, "  mouse-mode: %s\n",
753                    SpiceQueryMouseMode_str(info->mouse_mode));
754
755     if (!info->has_channels || info->channels == NULL) {
756         monitor_printf(mon, "Channels: none\n");
757     } else {
758         for (chan = info->channels; chan; chan = chan->next) {
759             monitor_printf(mon, "Channel:\n");
760             monitor_printf(mon, "     address: %s:%s%s\n",
761                            chan->value->host, chan->value->port,
762                            chan->value->tls ? " [tls]" : "");
763             monitor_printf(mon, "     session: %" PRId64 "\n",
764                            chan->value->connection_id);
765             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
766                            chan->value->channel_type, chan->value->channel_id);
767
768             channel_name = "unknown";
769             if (chan->value->channel_type > 0 &&
770                 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
771                 channel_names[chan->value->channel_type]) {
772                 channel_name = channel_names[chan->value->channel_type];
773             }
774
775             monitor_printf(mon, "     channel name: %s\n", channel_name);
776         }
777     }
778
779 out:
780     qapi_free_SpiceInfo(info);
781 }
782 #endif
783
784 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
785 {
786     BalloonInfo *info;
787     Error *err = NULL;
788
789     info = qmp_query_balloon(&err);
790     if (err) {
791         hmp_handle_error(mon, &err);
792         return;
793     }
794
795     monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
796
797     qapi_free_BalloonInfo(info);
798 }
799
800 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
801 {
802     PciMemoryRegionList *region;
803
804     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
805     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
806                    dev->slot, dev->function);
807     monitor_printf(mon, "    ");
808
809     if (dev->class_info->has_desc) {
810         monitor_printf(mon, "%s", dev->class_info->desc);
811     } else {
812         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
813     }
814
815     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
816                    dev->id->vendor, dev->id->device);
817
818     if (dev->has_irq) {
819         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
820     }
821
822     if (dev->has_pci_bridge) {
823         monitor_printf(mon, "      BUS %" PRId64 ".\n",
824                        dev->pci_bridge->bus->number);
825         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
826                        dev->pci_bridge->bus->secondary);
827         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
828                        dev->pci_bridge->bus->subordinate);
829
830         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
831                        dev->pci_bridge->bus->io_range->base,
832                        dev->pci_bridge->bus->io_range->limit);
833
834         monitor_printf(mon,
835                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
836                        dev->pci_bridge->bus->memory_range->base,
837                        dev->pci_bridge->bus->memory_range->limit);
838
839         monitor_printf(mon, "      prefetchable memory range "
840                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
841                        dev->pci_bridge->bus->prefetchable_range->base,
842                        dev->pci_bridge->bus->prefetchable_range->limit);
843     }
844
845     for (region = dev->regions; region; region = region->next) {
846         uint64_t addr, size;
847
848         addr = region->value->address;
849         size = region->value->size;
850
851         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
852
853         if (!strcmp(region->value->type, "io")) {
854             monitor_printf(mon, "I/O at 0x%04" PRIx64
855                                 " [0x%04" PRIx64 "].\n",
856                            addr, addr + size - 1);
857         } else {
858             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
859                                " [0x%08" PRIx64 "].\n",
860                            region->value->mem_type_64 ? 64 : 32,
861                            region->value->prefetch ? " prefetchable" : "",
862                            addr, addr + size - 1);
863         }
864     }
865
866     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
867
868     if (dev->has_pci_bridge) {
869         if (dev->pci_bridge->has_devices) {
870             PciDeviceInfoList *cdev;
871             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
872                 hmp_info_pci_device(mon, cdev->value);
873             }
874         }
875     }
876 }
877
878 static int hmp_info_irq_foreach(Object *obj, void *opaque)
879 {
880     InterruptStatsProvider *intc;
881     InterruptStatsProviderClass *k;
882     Monitor *mon = opaque;
883
884     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
885         intc = INTERRUPT_STATS_PROVIDER(obj);
886         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
887         uint64_t *irq_counts;
888         unsigned int nb_irqs, i;
889         if (k->get_statistics &&
890             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
891             if (nb_irqs > 0) {
892                 monitor_printf(mon, "IRQ statistics for %s:\n",
893                                object_get_typename(obj));
894                 for (i = 0; i < nb_irqs; i++) {
895                     if (irq_counts[i] > 0) {
896                         monitor_printf(mon, "%2d: %" PRId64 "\n", i,
897                                        irq_counts[i]);
898                     }
899                 }
900             }
901         } else {
902             monitor_printf(mon, "IRQ statistics not available for %s.\n",
903                            object_get_typename(obj));
904         }
905     }
906
907     return 0;
908 }
909
910 void hmp_info_irq(Monitor *mon, const QDict *qdict)
911 {
912     object_child_foreach_recursive(object_get_root(),
913                                    hmp_info_irq_foreach, mon);
914 }
915
916 static int hmp_info_pic_foreach(Object *obj, void *opaque)
917 {
918     InterruptStatsProvider *intc;
919     InterruptStatsProviderClass *k;
920     Monitor *mon = opaque;
921
922     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
923         intc = INTERRUPT_STATS_PROVIDER(obj);
924         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
925         if (k->print_info) {
926             k->print_info(intc, mon);
927         } else {
928             monitor_printf(mon, "Interrupt controller information not available for %s.\n",
929                            object_get_typename(obj));
930         }
931     }
932
933     return 0;
934 }
935
936 void hmp_info_pic(Monitor *mon, const QDict *qdict)
937 {
938     object_child_foreach_recursive(object_get_root(),
939                                    hmp_info_pic_foreach, mon);
940 }
941
942 void hmp_info_pci(Monitor *mon, const QDict *qdict)
943 {
944     PciInfoList *info_list, *info;
945     Error *err = NULL;
946
947     info_list = qmp_query_pci(&err);
948     if (err) {
949         monitor_printf(mon, "PCI devices not supported\n");
950         error_free(err);
951         return;
952     }
953
954     for (info = info_list; info; info = info->next) {
955         PciDeviceInfoList *dev;
956
957         for (dev = info->value->devices; dev; dev = dev->next) {
958             hmp_info_pci_device(mon, dev->value);
959         }
960     }
961
962     qapi_free_PciInfoList(info_list);
963 }
964
965 void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
966 {
967     BlockJobInfoList *list;
968     Error *err = NULL;
969
970     list = qmp_query_block_jobs(&err);
971     assert(!err);
972
973     if (!list) {
974         monitor_printf(mon, "No active jobs\n");
975         return;
976     }
977
978     while (list) {
979         if (strcmp(list->value->type, "stream") == 0) {
980             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
981                            " of %" PRId64 " bytes, speed limit %" PRId64
982                            " bytes/s\n",
983                            list->value->device,
984                            list->value->offset,
985                            list->value->len,
986                            list->value->speed);
987         } else {
988             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
989                            " of %" PRId64 " bytes, speed limit %" PRId64
990                            " bytes/s\n",
991                            list->value->type,
992                            list->value->device,
993                            list->value->offset,
994                            list->value->len,
995                            list->value->speed);
996         }
997         list = list->next;
998     }
999
1000     qapi_free_BlockJobInfoList(list);
1001 }
1002
1003 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
1004 {
1005     TPMInfoList *info_list, *info;
1006     Error *err = NULL;
1007     unsigned int c = 0;
1008     TPMPassthroughOptions *tpo;
1009     TPMEmulatorOptions *teo;
1010
1011     info_list = qmp_query_tpm(&err);
1012     if (err) {
1013         monitor_printf(mon, "TPM device not supported\n");
1014         error_free(err);
1015         return;
1016     }
1017
1018     if (info_list) {
1019         monitor_printf(mon, "TPM device:\n");
1020     }
1021
1022     for (info = info_list; info; info = info->next) {
1023         TPMInfo *ti = info->value;
1024         monitor_printf(mon, " tpm%d: model=%s\n",
1025                        c, TpmModel_str(ti->model));
1026
1027         monitor_printf(mon, "  \\ %s: type=%s",
1028                        ti->id, TpmTypeOptionsKind_str(ti->options->type));
1029
1030         switch (ti->options->type) {
1031         case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
1032             tpo = ti->options->u.passthrough.data;
1033             monitor_printf(mon, "%s%s%s%s",
1034                            tpo->has_path ? ",path=" : "",
1035                            tpo->has_path ? tpo->path : "",
1036                            tpo->has_cancel_path ? ",cancel-path=" : "",
1037                            tpo->has_cancel_path ? tpo->cancel_path : "");
1038             break;
1039         case TPM_TYPE_OPTIONS_KIND_EMULATOR:
1040             teo = ti->options->u.emulator.data;
1041             monitor_printf(mon, ",chardev=%s", teo->chardev);
1042             break;
1043         case TPM_TYPE_OPTIONS_KIND__MAX:
1044             break;
1045         }
1046         monitor_printf(mon, "\n");
1047         c++;
1048     }
1049     qapi_free_TPMInfoList(info_list);
1050 }
1051
1052 void hmp_quit(Monitor *mon, const QDict *qdict)
1053 {
1054     monitor_suspend(mon);
1055     qmp_quit(NULL);
1056 }
1057
1058 void hmp_stop(Monitor *mon, const QDict *qdict)
1059 {
1060     qmp_stop(NULL);
1061 }
1062
1063 void hmp_system_reset(Monitor *mon, const QDict *qdict)
1064 {
1065     qmp_system_reset(NULL);
1066 }
1067
1068 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
1069 {
1070     qmp_system_powerdown(NULL);
1071 }
1072
1073 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
1074 {
1075     Error *err = NULL;
1076
1077     qmp_exit_preconfig(&err);
1078     hmp_handle_error(mon, &err);
1079 }
1080
1081 void hmp_cpu(Monitor *mon, const QDict *qdict)
1082 {
1083     int64_t cpu_index;
1084
1085     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
1086             use it are converted to the QAPI */
1087     cpu_index = qdict_get_int(qdict, "index");
1088     if (monitor_set_cpu(cpu_index) < 0) {
1089         monitor_printf(mon, "invalid CPU index\n");
1090     }
1091 }
1092
1093 void hmp_memsave(Monitor *mon, const QDict *qdict)
1094 {
1095     uint32_t size = qdict_get_int(qdict, "size");
1096     const char *filename = qdict_get_str(qdict, "filename");
1097     uint64_t addr = qdict_get_int(qdict, "val");
1098     Error *err = NULL;
1099     int cpu_index = monitor_get_cpu_index();
1100
1101     if (cpu_index < 0) {
1102         monitor_printf(mon, "No CPU available\n");
1103         return;
1104     }
1105
1106     qmp_memsave(addr, size, filename, true, cpu_index, &err);
1107     hmp_handle_error(mon, &err);
1108 }
1109
1110 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1111 {
1112     uint32_t size = qdict_get_int(qdict, "size");
1113     const char *filename = qdict_get_str(qdict, "filename");
1114     uint64_t addr = qdict_get_int(qdict, "val");
1115     Error *err = NULL;
1116
1117     qmp_pmemsave(addr, size, filename, &err);
1118     hmp_handle_error(mon, &err);
1119 }
1120
1121 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1122 {
1123     const char *chardev = qdict_get_str(qdict, "device");
1124     const char *data = qdict_get_str(qdict, "data");
1125     Error *err = NULL;
1126
1127     qmp_ringbuf_write(chardev, data, false, 0, &err);
1128
1129     hmp_handle_error(mon, &err);
1130 }
1131
1132 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1133 {
1134     uint32_t size = qdict_get_int(qdict, "size");
1135     const char *chardev = qdict_get_str(qdict, "device");
1136     char *data;
1137     Error *err = NULL;
1138     int i;
1139
1140     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1141     if (err) {
1142         hmp_handle_error(mon, &err);
1143         return;
1144     }
1145
1146     for (i = 0; data[i]; i++) {
1147         unsigned char ch = data[i];
1148
1149         if (ch == '\\') {
1150             monitor_printf(mon, "\\\\");
1151         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1152             monitor_printf(mon, "\\u%04X", ch);
1153         } else {
1154             monitor_printf(mon, "%c", ch);
1155         }
1156
1157     }
1158     monitor_printf(mon, "\n");
1159     g_free(data);
1160 }
1161
1162 void hmp_cont(Monitor *mon, const QDict *qdict)
1163 {
1164     Error *err = NULL;
1165
1166     qmp_cont(&err);
1167     hmp_handle_error(mon, &err);
1168 }
1169
1170 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1171 {
1172     qmp_system_wakeup(NULL);
1173 }
1174
1175 void hmp_nmi(Monitor *mon, const QDict *qdict)
1176 {
1177     Error *err = NULL;
1178
1179     qmp_inject_nmi(&err);
1180     hmp_handle_error(mon, &err);
1181 }
1182
1183 void hmp_set_link(Monitor *mon, const QDict *qdict)
1184 {
1185     const char *name = qdict_get_str(qdict, "name");
1186     bool up = qdict_get_bool(qdict, "up");
1187     Error *err = NULL;
1188
1189     qmp_set_link(name, up, &err);
1190     hmp_handle_error(mon, &err);
1191 }
1192
1193 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1194 {
1195     const char *device = qdict_get_str(qdict, "device");
1196     const char *password = qdict_get_str(qdict, "password");
1197     Error *err = NULL;
1198
1199     qmp_block_passwd(true, device, false, NULL, password, &err);
1200     hmp_handle_error(mon, &err);
1201 }
1202
1203 void hmp_balloon(Monitor *mon, const QDict *qdict)
1204 {
1205     int64_t value = qdict_get_int(qdict, "value");
1206     Error *err = NULL;
1207
1208     qmp_balloon(value, &err);
1209     hmp_handle_error(mon, &err);
1210 }
1211
1212 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1213 {
1214     const char *device = qdict_get_str(qdict, "device");
1215     int64_t size = qdict_get_int(qdict, "size");
1216     Error *err = NULL;
1217
1218     qmp_block_resize(true, device, false, NULL, size, &err);
1219     hmp_handle_error(mon, &err);
1220 }
1221
1222 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1223 {
1224     const char *filename = qdict_get_str(qdict, "target");
1225     const char *format = qdict_get_try_str(qdict, "format");
1226     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1227     bool full = qdict_get_try_bool(qdict, "full", false);
1228     Error *err = NULL;
1229     DriveMirror mirror = {
1230         .device = (char *)qdict_get_str(qdict, "device"),
1231         .target = (char *)filename,
1232         .has_format = !!format,
1233         .format = (char *)format,
1234         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1235         .has_mode = true,
1236         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1237         .unmap = true,
1238     };
1239
1240     if (!filename) {
1241         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1242         hmp_handle_error(mon, &err);
1243         return;
1244     }
1245     qmp_drive_mirror(&mirror, &err);
1246     hmp_handle_error(mon, &err);
1247 }
1248
1249 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1250 {
1251     const char *device = qdict_get_str(qdict, "device");
1252     const char *filename = qdict_get_str(qdict, "target");
1253     const char *format = qdict_get_try_str(qdict, "format");
1254     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1255     bool full = qdict_get_try_bool(qdict, "full", false);
1256     bool compress = qdict_get_try_bool(qdict, "compress", false);
1257     Error *err = NULL;
1258     DriveBackup backup = {
1259         .device = (char *)device,
1260         .target = (char *)filename,
1261         .has_format = !!format,
1262         .format = (char *)format,
1263         .sync = full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1264         .has_mode = true,
1265         .mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS,
1266         .has_compress = !!compress,
1267         .compress = compress,
1268     };
1269
1270     if (!filename) {
1271         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1272         hmp_handle_error(mon, &err);
1273         return;
1274     }
1275
1276     qmp_drive_backup(&backup, &err);
1277     hmp_handle_error(mon, &err);
1278 }
1279
1280 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1281 {
1282     const char *device = qdict_get_str(qdict, "device");
1283     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1284     const char *format = qdict_get_try_str(qdict, "format");
1285     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1286     enum NewImageMode mode;
1287     Error *err = NULL;
1288
1289     if (!filename) {
1290         /* In the future, if 'snapshot-file' is not specified, the snapshot
1291            will be taken internally. Today it's actually required. */
1292         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1293         hmp_handle_error(mon, &err);
1294         return;
1295     }
1296
1297     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1298     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1299                                filename, false, NULL,
1300                                !!format, format,
1301                                true, mode, &err);
1302     hmp_handle_error(mon, &err);
1303 }
1304
1305 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1306 {
1307     const char *device = qdict_get_str(qdict, "device");
1308     const char *name = qdict_get_str(qdict, "name");
1309     Error *err = NULL;
1310
1311     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1312     hmp_handle_error(mon, &err);
1313 }
1314
1315 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1316 {
1317     const char *device = qdict_get_str(qdict, "device");
1318     const char *name = qdict_get_str(qdict, "name");
1319     const char *id = qdict_get_try_str(qdict, "id");
1320     Error *err = NULL;
1321
1322     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1323                                                true, name, &err);
1324     hmp_handle_error(mon, &err);
1325 }
1326
1327 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1328 {
1329     int saved_vm_running  = runstate_is_running();
1330     const char *name = qdict_get_str(qdict, "name");
1331     Error *err = NULL;
1332
1333     vm_stop(RUN_STATE_RESTORE_VM);
1334
1335     if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1336         vm_start();
1337     }
1338     hmp_handle_error(mon, &err);
1339 }
1340
1341 void hmp_savevm(Monitor *mon, const QDict *qdict)
1342 {
1343     Error *err = NULL;
1344
1345     save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1346     hmp_handle_error(mon, &err);
1347 }
1348
1349 void hmp_delvm(Monitor *mon, const QDict *qdict)
1350 {
1351     BlockDriverState *bs;
1352     Error *err = NULL;
1353     const char *name = qdict_get_str(qdict, "name");
1354
1355     if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1356         error_reportf_err(err,
1357                           "Error while deleting snapshot on device '%s': ",
1358                           bdrv_get_device_name(bs));
1359     }
1360 }
1361
1362 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
1363 {
1364     BlockDriverState *bs, *bs1;
1365     BdrvNextIterator it1;
1366     QEMUSnapshotInfo *sn_tab, *sn;
1367     bool no_snapshot = true;
1368     int nb_sns, i;
1369     int total;
1370     int *global_snapshots;
1371     AioContext *aio_context;
1372
1373     typedef struct SnapshotEntry {
1374         QEMUSnapshotInfo sn;
1375         QTAILQ_ENTRY(SnapshotEntry) next;
1376     } SnapshotEntry;
1377
1378     typedef struct ImageEntry {
1379         const char *imagename;
1380         QTAILQ_ENTRY(ImageEntry) next;
1381         QTAILQ_HEAD(, SnapshotEntry) snapshots;
1382     } ImageEntry;
1383
1384     QTAILQ_HEAD(, ImageEntry) image_list =
1385         QTAILQ_HEAD_INITIALIZER(image_list);
1386
1387     ImageEntry *image_entry, *next_ie;
1388     SnapshotEntry *snapshot_entry;
1389
1390     bs = bdrv_all_find_vmstate_bs();
1391     if (!bs) {
1392         monitor_printf(mon, "No available block device supports snapshots\n");
1393         return;
1394     }
1395     aio_context = bdrv_get_aio_context(bs);
1396
1397     aio_context_acquire(aio_context);
1398     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1399     aio_context_release(aio_context);
1400
1401     if (nb_sns < 0) {
1402         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1403         return;
1404     }
1405
1406     for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
1407         int bs1_nb_sns = 0;
1408         ImageEntry *ie;
1409         SnapshotEntry *se;
1410         AioContext *ctx = bdrv_get_aio_context(bs1);
1411
1412         aio_context_acquire(ctx);
1413         if (bdrv_can_snapshot(bs1)) {
1414             sn = NULL;
1415             bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
1416             if (bs1_nb_sns > 0) {
1417                 no_snapshot = false;
1418                 ie = g_new0(ImageEntry, 1);
1419                 ie->imagename = bdrv_get_device_name(bs1);
1420                 QTAILQ_INIT(&ie->snapshots);
1421                 QTAILQ_INSERT_TAIL(&image_list, ie, next);
1422                 for (i = 0; i < bs1_nb_sns; i++) {
1423                     se = g_new0(SnapshotEntry, 1);
1424                     se->sn = sn[i];
1425                     QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
1426                 }
1427             }
1428             g_free(sn);
1429         }
1430         aio_context_release(ctx);
1431     }
1432
1433     if (no_snapshot) {
1434         monitor_printf(mon, "There is no snapshot available.\n");
1435         return;
1436     }
1437
1438     global_snapshots = g_new0(int, nb_sns);
1439     total = 0;
1440     for (i = 0; i < nb_sns; i++) {
1441         SnapshotEntry *next_sn;
1442         if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
1443             global_snapshots[total] = i;
1444             total++;
1445             QTAILQ_FOREACH(image_entry, &image_list, next) {
1446                 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
1447                                     next, next_sn) {
1448                     if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
1449                         QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
1450                                       next);
1451                         g_free(snapshot_entry);
1452                     }
1453                 }
1454             }
1455         }
1456     }
1457
1458     monitor_printf(mon, "List of snapshots present on all disks:\n");
1459
1460     if (total > 0) {
1461         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1462         monitor_printf(mon, "\n");
1463         for (i = 0; i < total; i++) {
1464             sn = &sn_tab[global_snapshots[i]];
1465             /* The ID is not guaranteed to be the same on all images, so
1466              * overwrite it.
1467              */
1468             pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
1469             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
1470             monitor_printf(mon, "\n");
1471         }
1472     } else {
1473         monitor_printf(mon, "None\n");
1474     }
1475
1476     QTAILQ_FOREACH(image_entry, &image_list, next) {
1477         if (QTAILQ_EMPTY(&image_entry->snapshots)) {
1478             continue;
1479         }
1480         monitor_printf(mon,
1481                        "\nList of partial (non-loadable) snapshots on '%s':\n",
1482                        image_entry->imagename);
1483         bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
1484         monitor_printf(mon, "\n");
1485         QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
1486             bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
1487                                &snapshot_entry->sn);
1488             monitor_printf(mon, "\n");
1489         }
1490     }
1491
1492     QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
1493         SnapshotEntry *next_sn;
1494         QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
1495                             next_sn) {
1496             g_free(snapshot_entry);
1497         }
1498         g_free(image_entry);
1499     }
1500     g_free(sn_tab);
1501     g_free(global_snapshots);
1502
1503 }
1504
1505 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1506 {
1507     qmp_migrate_cancel(NULL);
1508 }
1509
1510 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
1511 {
1512     Error *err = NULL;
1513     const char *state = qdict_get_str(qdict, "state");
1514     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
1515
1516     if (val >= 0) {
1517         qmp_migrate_continue(val, &err);
1518     }
1519
1520     hmp_handle_error(mon, &err);
1521 }
1522
1523 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1524 {
1525     Error *err = NULL;
1526     const char *uri = qdict_get_str(qdict, "uri");
1527
1528     qmp_migrate_incoming(uri, &err);
1529
1530     hmp_handle_error(mon, &err);
1531 }
1532
1533 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
1534 {
1535     Error *err = NULL;
1536     const char *uri = qdict_get_str(qdict, "uri");
1537
1538     qmp_migrate_recover(uri, &err);
1539
1540     hmp_handle_error(mon, &err);
1541 }
1542
1543 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
1544 {
1545     Error *err = NULL;
1546
1547     qmp_migrate_pause(&err);
1548
1549     hmp_handle_error(mon, &err);
1550 }
1551
1552 /* Kept for backwards compatibility */
1553 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1554 {
1555     double value = qdict_get_double(qdict, "value");
1556     qmp_migrate_set_downtime(value, NULL);
1557 }
1558
1559 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1560 {
1561     int64_t value = qdict_get_int(qdict, "value");
1562     Error *err = NULL;
1563
1564     qmp_migrate_set_cache_size(value, &err);
1565     hmp_handle_error(mon, &err);
1566 }
1567
1568 /* Kept for backwards compatibility */
1569 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1570 {
1571     int64_t value = qdict_get_int(qdict, "value");
1572     qmp_migrate_set_speed(value, NULL);
1573 }
1574
1575 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1576 {
1577     const char *cap = qdict_get_str(qdict, "capability");
1578     bool state = qdict_get_bool(qdict, "state");
1579     Error *err = NULL;
1580     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1581     int val;
1582
1583     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1584     if (val < 0) {
1585         goto end;
1586     }
1587
1588     caps->value = g_malloc0(sizeof(*caps->value));
1589     caps->value->capability = val;
1590     caps->value->state = state;
1591     caps->next = NULL;
1592     qmp_migrate_set_capabilities(caps, &err);
1593
1594 end:
1595     qapi_free_MigrationCapabilityStatusList(caps);
1596     hmp_handle_error(mon, &err);
1597 }
1598
1599 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1600 {
1601     const char *param = qdict_get_str(qdict, "parameter");
1602     const char *valuestr = qdict_get_str(qdict, "value");
1603     Visitor *v = string_input_visitor_new(valuestr);
1604     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1605     uint64_t valuebw = 0;
1606     uint64_t cache_size;
1607     Error *err = NULL;
1608     int val, ret;
1609
1610     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1611     if (val < 0) {
1612         goto cleanup;
1613     }
1614
1615     switch (val) {
1616     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1617         p->has_compress_level = true;
1618         visit_type_int(v, param, &p->compress_level, &err);
1619         break;
1620     case MIGRATION_PARAMETER_COMPRESS_THREADS:
1621         p->has_compress_threads = true;
1622         visit_type_int(v, param, &p->compress_threads, &err);
1623         break;
1624     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1625         p->has_decompress_threads = true;
1626         visit_type_int(v, param, &p->decompress_threads, &err);
1627         break;
1628     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1629         p->has_cpu_throttle_initial = true;
1630         visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1631         break;
1632     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1633         p->has_cpu_throttle_increment = true;
1634         visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1635         break;
1636     case MIGRATION_PARAMETER_TLS_CREDS:
1637         p->has_tls_creds = true;
1638         p->tls_creds = g_new0(StrOrNull, 1);
1639         p->tls_creds->type = QTYPE_QSTRING;
1640         visit_type_str(v, param, &p->tls_creds->u.s, &err);
1641         break;
1642     case MIGRATION_PARAMETER_TLS_HOSTNAME:
1643         p->has_tls_hostname = true;
1644         p->tls_hostname = g_new0(StrOrNull, 1);
1645         p->tls_hostname->type = QTYPE_QSTRING;
1646         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1647         break;
1648     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1649         p->has_max_bandwidth = true;
1650         /*
1651          * Can't use visit_type_size() here, because it
1652          * defaults to Bytes rather than Mebibytes.
1653          */
1654         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1655         if (ret < 0 || valuebw > INT64_MAX
1656             || (size_t)valuebw != valuebw) {
1657             error_setg(&err, "Invalid size %s", valuestr);
1658             break;
1659         }
1660         p->max_bandwidth = valuebw;
1661         break;
1662     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1663         p->has_downtime_limit = true;
1664         visit_type_int(v, param, &p->downtime_limit, &err);
1665         break;
1666     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1667         p->has_x_checkpoint_delay = true;
1668         visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1669         break;
1670     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1671         p->has_block_incremental = true;
1672         visit_type_bool(v, param, &p->block_incremental, &err);
1673         break;
1674     case MIGRATION_PARAMETER_X_MULTIFD_CHANNELS:
1675         p->has_x_multifd_channels = true;
1676         visit_type_int(v, param, &p->x_multifd_channels, &err);
1677         break;
1678     case MIGRATION_PARAMETER_X_MULTIFD_PAGE_COUNT:
1679         p->has_x_multifd_page_count = true;
1680         visit_type_int(v, param, &p->x_multifd_page_count, &err);
1681         break;
1682     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
1683         p->has_xbzrle_cache_size = true;
1684         visit_type_size(v, param, &cache_size, &err);
1685         if (err || cache_size > INT64_MAX
1686             || (size_t)cache_size != cache_size) {
1687             error_setg(&err, "Invalid size %s", valuestr);
1688             break;
1689         }
1690         p->xbzrle_cache_size = cache_size;
1691         break;
1692     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
1693         p->has_max_postcopy_bandwidth = true;
1694         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
1695         break;
1696     default:
1697         assert(0);
1698     }
1699
1700     if (err) {
1701         goto cleanup;
1702     }
1703
1704     qmp_migrate_set_parameters(p, &err);
1705
1706  cleanup:
1707     qapi_free_MigrateSetParameters(p);
1708     visit_free(v);
1709     hmp_handle_error(mon, &err);
1710 }
1711
1712 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1713 {
1714     Error *err = NULL;
1715     const char *protocol = qdict_get_str(qdict, "protocol");
1716     const char *hostname = qdict_get_str(qdict, "hostname");
1717     bool has_port        = qdict_haskey(qdict, "port");
1718     int port             = qdict_get_try_int(qdict, "port", -1);
1719     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1720     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1721     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1722
1723     qmp_client_migrate_info(protocol, hostname,
1724                             has_port, port, has_tls_port, tls_port,
1725                             !!cert_subject, cert_subject, &err);
1726     hmp_handle_error(mon, &err);
1727 }
1728
1729 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1730 {
1731     Error *err = NULL;
1732     qmp_migrate_start_postcopy(&err);
1733     hmp_handle_error(mon, &err);
1734 }
1735
1736 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1737 {
1738     Error *err = NULL;
1739
1740     qmp_x_colo_lost_heartbeat(&err);
1741     hmp_handle_error(mon, &err);
1742 }
1743
1744 void hmp_set_password(Monitor *mon, const QDict *qdict)
1745 {
1746     const char *protocol  = qdict_get_str(qdict, "protocol");
1747     const char *password  = qdict_get_str(qdict, "password");
1748     const char *connected = qdict_get_try_str(qdict, "connected");
1749     Error *err = NULL;
1750
1751     qmp_set_password(protocol, password, !!connected, connected, &err);
1752     hmp_handle_error(mon, &err);
1753 }
1754
1755 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1756 {
1757     const char *protocol  = qdict_get_str(qdict, "protocol");
1758     const char *whenstr = qdict_get_str(qdict, "time");
1759     Error *err = NULL;
1760
1761     qmp_expire_password(protocol, whenstr, &err);
1762     hmp_handle_error(mon, &err);
1763 }
1764
1765 void hmp_eject(Monitor *mon, const QDict *qdict)
1766 {
1767     bool force = qdict_get_try_bool(qdict, "force", false);
1768     const char *device = qdict_get_str(qdict, "device");
1769     Error *err = NULL;
1770
1771     qmp_eject(true, device, false, NULL, true, force, &err);
1772     hmp_handle_error(mon, &err);
1773 }
1774
1775 static void hmp_change_read_arg(void *opaque, const char *password,
1776                                 void *readline_opaque)
1777 {
1778     qmp_change_vnc_password(password, NULL);
1779     monitor_read_command(opaque, 1);
1780 }
1781
1782 void hmp_change(Monitor *mon, const QDict *qdict)
1783 {
1784     const char *device = qdict_get_str(qdict, "device");
1785     const char *target = qdict_get_str(qdict, "target");
1786     const char *arg = qdict_get_try_str(qdict, "arg");
1787     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1788     BlockdevChangeReadOnlyMode read_only_mode = 0;
1789     Error *err = NULL;
1790
1791     if (strcmp(device, "vnc") == 0) {
1792         if (read_only) {
1793             monitor_printf(mon,
1794                            "Parameter 'read-only-mode' is invalid for VNC\n");
1795             return;
1796         }
1797         if (strcmp(target, "passwd") == 0 ||
1798             strcmp(target, "password") == 0) {
1799             if (!arg) {
1800                 monitor_read_password(mon, hmp_change_read_arg, NULL);
1801                 return;
1802             }
1803         }
1804         qmp_change("vnc", target, !!arg, arg, &err);
1805     } else {
1806         if (read_only) {
1807             read_only_mode =
1808                 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1809                                 read_only,
1810                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1811             if (err) {
1812                 hmp_handle_error(mon, &err);
1813                 return;
1814             }
1815         }
1816
1817         qmp_blockdev_change_medium(true, device, false, NULL, target,
1818                                    !!arg, arg, !!read_only, read_only_mode,
1819                                    &err);
1820     }
1821
1822     hmp_handle_error(mon, &err);
1823 }
1824
1825 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1826 {
1827     Error *err = NULL;
1828     char *device = (char *) qdict_get_str(qdict, "device");
1829     BlockIOThrottle throttle = {
1830         .bps = qdict_get_int(qdict, "bps"),
1831         .bps_rd = qdict_get_int(qdict, "bps_rd"),
1832         .bps_wr = qdict_get_int(qdict, "bps_wr"),
1833         .iops = qdict_get_int(qdict, "iops"),
1834         .iops_rd = qdict_get_int(qdict, "iops_rd"),
1835         .iops_wr = qdict_get_int(qdict, "iops_wr"),
1836     };
1837
1838     /* qmp_block_set_io_throttle has separate parameters for the
1839      * (deprecated) block device name and the qdev ID but the HMP
1840      * version has only one, so we must decide which one to pass. */
1841     if (blk_by_name(device)) {
1842         throttle.has_device = true;
1843         throttle.device = device;
1844     } else {
1845         throttle.has_id = true;
1846         throttle.id = device;
1847     }
1848
1849     qmp_block_set_io_throttle(&throttle, &err);
1850     hmp_handle_error(mon, &err);
1851 }
1852
1853 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1854 {
1855     Error *error = NULL;
1856     const char *device = qdict_get_str(qdict, "device");
1857     const char *base = qdict_get_try_str(qdict, "base");
1858     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1859
1860     qmp_block_stream(true, device, device, base != NULL, base, false, NULL,
1861                      false, NULL, qdict_haskey(qdict, "speed"), speed,
1862                      true, BLOCKDEV_ON_ERROR_REPORT, &error);
1863
1864     hmp_handle_error(mon, &error);
1865 }
1866
1867 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1868 {
1869     Error *error = NULL;
1870     const char *device = qdict_get_str(qdict, "device");
1871     int64_t value = qdict_get_int(qdict, "speed");
1872
1873     qmp_block_job_set_speed(device, value, &error);
1874
1875     hmp_handle_error(mon, &error);
1876 }
1877
1878 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1879 {
1880     Error *error = NULL;
1881     const char *device = qdict_get_str(qdict, "device");
1882     bool force = qdict_get_try_bool(qdict, "force", false);
1883
1884     qmp_block_job_cancel(device, true, force, &error);
1885
1886     hmp_handle_error(mon, &error);
1887 }
1888
1889 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1890 {
1891     Error *error = NULL;
1892     const char *device = qdict_get_str(qdict, "device");
1893
1894     qmp_block_job_pause(device, &error);
1895
1896     hmp_handle_error(mon, &error);
1897 }
1898
1899 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1900 {
1901     Error *error = NULL;
1902     const char *device = qdict_get_str(qdict, "device");
1903
1904     qmp_block_job_resume(device, &error);
1905
1906     hmp_handle_error(mon, &error);
1907 }
1908
1909 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1910 {
1911     Error *error = NULL;
1912     const char *device = qdict_get_str(qdict, "device");
1913
1914     qmp_block_job_complete(device, &error);
1915
1916     hmp_handle_error(mon, &error);
1917 }
1918
1919 typedef struct HMPMigrationStatus
1920 {
1921     QEMUTimer *timer;
1922     Monitor *mon;
1923     bool is_block_migration;
1924 } HMPMigrationStatus;
1925
1926 static void hmp_migrate_status_cb(void *opaque)
1927 {
1928     HMPMigrationStatus *status = opaque;
1929     MigrationInfo *info;
1930
1931     info = qmp_query_migrate(NULL);
1932     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1933         info->status == MIGRATION_STATUS_SETUP) {
1934         if (info->has_disk) {
1935             int progress;
1936
1937             if (info->disk->remaining) {
1938                 progress = info->disk->transferred * 100 / info->disk->total;
1939             } else {
1940                 progress = 100;
1941             }
1942
1943             monitor_printf(status->mon, "Completed %d %%\r", progress);
1944             monitor_flush(status->mon);
1945         }
1946
1947         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1948     } else {
1949         if (status->is_block_migration) {
1950             monitor_printf(status->mon, "\n");
1951         }
1952         if (info->has_error_desc) {
1953             error_report("%s", info->error_desc);
1954         }
1955         monitor_resume(status->mon);
1956         timer_del(status->timer);
1957         g_free(status);
1958     }
1959
1960     qapi_free_MigrationInfo(info);
1961 }
1962
1963 void hmp_migrate(Monitor *mon, const QDict *qdict)
1964 {
1965     bool detach = qdict_get_try_bool(qdict, "detach", false);
1966     bool blk = qdict_get_try_bool(qdict, "blk", false);
1967     bool inc = qdict_get_try_bool(qdict, "inc", false);
1968     bool resume = qdict_get_try_bool(qdict, "resume", false);
1969     const char *uri = qdict_get_str(qdict, "uri");
1970     Error *err = NULL;
1971
1972     qmp_migrate(uri, !!blk, blk, !!inc, inc,
1973                 false, false, true, resume, &err);
1974     if (err) {
1975         hmp_handle_error(mon, &err);
1976         return;
1977     }
1978
1979     if (!detach) {
1980         HMPMigrationStatus *status;
1981
1982         if (monitor_suspend(mon) < 0) {
1983             monitor_printf(mon, "terminal does not allow synchronous "
1984                            "migration, continuing detached\n");
1985             return;
1986         }
1987
1988         status = g_malloc0(sizeof(*status));
1989         status->mon = mon;
1990         status->is_block_migration = blk || inc;
1991         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1992                                           status);
1993         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1994     }
1995 }
1996
1997 void hmp_device_add(Monitor *mon, const QDict *qdict)
1998 {
1999     Error *err = NULL;
2000
2001     qmp_device_add((QDict *)qdict, NULL, &err);
2002     hmp_handle_error(mon, &err);
2003 }
2004
2005 void hmp_device_del(Monitor *mon, const QDict *qdict)
2006 {
2007     const char *id = qdict_get_str(qdict, "id");
2008     Error *err = NULL;
2009
2010     qmp_device_del(id, &err);
2011     hmp_handle_error(mon, &err);
2012 }
2013
2014 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
2015 {
2016     Error *err = NULL;
2017     bool win_dmp = qdict_get_try_bool(qdict, "windmp", false);
2018     bool paging = qdict_get_try_bool(qdict, "paging", false);
2019     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
2020     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
2021     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
2022     const char *file = qdict_get_str(qdict, "filename");
2023     bool has_begin = qdict_haskey(qdict, "begin");
2024     bool has_length = qdict_haskey(qdict, "length");
2025     bool has_detach = qdict_haskey(qdict, "detach");
2026     int64_t begin = 0;
2027     int64_t length = 0;
2028     bool detach = false;
2029     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
2030     char *prot;
2031
2032     if (zlib + lzo + snappy + win_dmp > 1) {
2033         error_setg(&err, "only one of '-z|-l|-s|-w' can be set");
2034         hmp_handle_error(mon, &err);
2035         return;
2036     }
2037
2038     if (win_dmp) {
2039         dump_format = DUMP_GUEST_MEMORY_FORMAT_WIN_DMP;
2040     }
2041
2042     if (zlib) {
2043         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
2044     }
2045
2046     if (lzo) {
2047         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
2048     }
2049
2050     if (snappy) {
2051         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
2052     }
2053
2054     if (has_begin) {
2055         begin = qdict_get_int(qdict, "begin");
2056     }
2057     if (has_length) {
2058         length = qdict_get_int(qdict, "length");
2059     }
2060     if (has_detach) {
2061         detach = qdict_get_bool(qdict, "detach");
2062     }
2063
2064     prot = g_strconcat("file:", file, NULL);
2065
2066     qmp_dump_guest_memory(paging, prot, true, detach, has_begin, begin,
2067                           has_length, length, true, dump_format, &err);
2068     hmp_handle_error(mon, &err);
2069     g_free(prot);
2070 }
2071
2072 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
2073 {
2074     Error *err = NULL;
2075     QemuOpts *opts;
2076
2077     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
2078     if (err) {
2079         goto out;
2080     }
2081
2082     netdev_add(opts, &err);
2083     if (err) {
2084         qemu_opts_del(opts);
2085     }
2086
2087 out:
2088     hmp_handle_error(mon, &err);
2089 }
2090
2091 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
2092 {
2093     const char *id = qdict_get_str(qdict, "id");
2094     Error *err = NULL;
2095
2096     qmp_netdev_del(id, &err);
2097     hmp_handle_error(mon, &err);
2098 }
2099
2100 void hmp_object_add(Monitor *mon, const QDict *qdict)
2101 {
2102     Error *err = NULL;
2103     QemuOpts *opts;
2104     Object *obj = NULL;
2105
2106     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
2107     if (err) {
2108         hmp_handle_error(mon, &err);
2109         return;
2110     }
2111
2112     obj = user_creatable_add_opts(opts, &err);
2113     qemu_opts_del(opts);
2114
2115     if (err) {
2116         hmp_handle_error(mon, &err);
2117     }
2118     if (obj) {
2119         object_unref(obj);
2120     }
2121 }
2122
2123 void hmp_getfd(Monitor *mon, const QDict *qdict)
2124 {
2125     const char *fdname = qdict_get_str(qdict, "fdname");
2126     Error *err = NULL;
2127
2128     qmp_getfd(fdname, &err);
2129     hmp_handle_error(mon, &err);
2130 }
2131
2132 void hmp_closefd(Monitor *mon, const QDict *qdict)
2133 {
2134     const char *fdname = qdict_get_str(qdict, "fdname");
2135     Error *err = NULL;
2136
2137     qmp_closefd(fdname, &err);
2138     hmp_handle_error(mon, &err);
2139 }
2140
2141 void hmp_sendkey(Monitor *mon, const QDict *qdict)
2142 {
2143     const char *keys = qdict_get_str(qdict, "keys");
2144     KeyValueList *keylist, *head = NULL, *tmp = NULL;
2145     int has_hold_time = qdict_haskey(qdict, "hold-time");
2146     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
2147     Error *err = NULL;
2148     const char *separator;
2149     int keyname_len;
2150
2151     while (1) {
2152         separator = qemu_strchrnul(keys, '-');
2153         keyname_len = separator - keys;
2154
2155         /* Be compatible with old interface, convert user inputted "<" */
2156         if (keys[0] == '<' && keyname_len == 1) {
2157             keys = "less";
2158             keyname_len = 4;
2159         }
2160
2161         keylist = g_malloc0(sizeof(*keylist));
2162         keylist->value = g_malloc0(sizeof(*keylist->value));
2163
2164         if (!head) {
2165             head = keylist;
2166         }
2167         if (tmp) {
2168             tmp->next = keylist;
2169         }
2170         tmp = keylist;
2171
2172         if (strstart(keys, "0x", NULL)) {
2173             char *endp;
2174             int value = strtoul(keys, &endp, 0);
2175             assert(endp <= keys + keyname_len);
2176             if (endp != keys + keyname_len) {
2177                 goto err_out;
2178             }
2179             keylist->value->type = KEY_VALUE_KIND_NUMBER;
2180             keylist->value->u.number.data = value;
2181         } else {
2182             int idx = index_from_key(keys, keyname_len);
2183             if (idx == Q_KEY_CODE__MAX) {
2184                 goto err_out;
2185             }
2186             keylist->value->type = KEY_VALUE_KIND_QCODE;
2187             keylist->value->u.qcode.data = idx;
2188         }
2189
2190         if (!*separator) {
2191             break;
2192         }
2193         keys = separator + 1;
2194     }
2195
2196     qmp_send_key(head, has_hold_time, hold_time, &err);
2197     hmp_handle_error(mon, &err);
2198
2199 out:
2200     qapi_free_KeyValueList(head);
2201     return;
2202
2203 err_out:
2204     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
2205     goto out;
2206 }
2207
2208 void hmp_screendump(Monitor *mon, const QDict *qdict)
2209 {
2210     const char *filename = qdict_get_str(qdict, "filename");
2211     const char *id = qdict_get_try_str(qdict, "device");
2212     int64_t head = qdict_get_try_int(qdict, "head", 0);
2213     Error *err = NULL;
2214
2215     qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
2216     hmp_handle_error(mon, &err);
2217 }
2218
2219 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2220 {
2221     const char *uri = qdict_get_str(qdict, "uri");
2222     bool writable = qdict_get_try_bool(qdict, "writable", false);
2223     bool all = qdict_get_try_bool(qdict, "all", false);
2224     Error *local_err = NULL;
2225     BlockInfoList *block_list, *info;
2226     SocketAddress *addr;
2227
2228     if (writable && !all) {
2229         error_setg(&local_err, "-w only valid together with -a");
2230         goto exit;
2231     }
2232
2233     /* First check if the address is valid and start the server.  */
2234     addr = socket_parse(uri, &local_err);
2235     if (local_err != NULL) {
2236         goto exit;
2237     }
2238
2239     nbd_server_start(addr, NULL, &local_err);
2240     qapi_free_SocketAddress(addr);
2241     if (local_err != NULL) {
2242         goto exit;
2243     }
2244
2245     if (!all) {
2246         return;
2247     }
2248
2249     /* Then try adding all block devices.  If one fails, close all and
2250      * exit.
2251      */
2252     block_list = qmp_query_block(NULL);
2253
2254     for (info = block_list; info; info = info->next) {
2255         if (!info->value->has_inserted) {
2256             continue;
2257         }
2258
2259         qmp_nbd_server_add(info->value->device, false, NULL,
2260                            true, writable, &local_err);
2261
2262         if (local_err != NULL) {
2263             qmp_nbd_server_stop(NULL);
2264             break;
2265         }
2266     }
2267
2268     qapi_free_BlockInfoList(block_list);
2269
2270 exit:
2271     hmp_handle_error(mon, &local_err);
2272 }
2273
2274 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
2275 {
2276     const char *device = qdict_get_str(qdict, "device");
2277     const char *name = qdict_get_try_str(qdict, "name");
2278     bool writable = qdict_get_try_bool(qdict, "writable", false);
2279     Error *local_err = NULL;
2280
2281     qmp_nbd_server_add(device, !!name, name, true, writable, &local_err);
2282     hmp_handle_error(mon, &local_err);
2283 }
2284
2285 void hmp_nbd_server_remove(Monitor *mon, const QDict *qdict)
2286 {
2287     const char *name = qdict_get_str(qdict, "name");
2288     bool force = qdict_get_try_bool(qdict, "force", false);
2289     Error *err = NULL;
2290
2291     /* Rely on NBD_SERVER_REMOVE_MODE_SAFE being the default */
2292     qmp_nbd_server_remove(name, force, NBD_SERVER_REMOVE_MODE_HARD, &err);
2293     hmp_handle_error(mon, &err);
2294 }
2295
2296 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
2297 {
2298     Error *err = NULL;
2299
2300     qmp_nbd_server_stop(&err);
2301     hmp_handle_error(mon, &err);
2302 }
2303
2304 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
2305 {
2306     int cpuid;
2307     Error *err = NULL;
2308
2309     cpuid = qdict_get_int(qdict, "id");
2310     qmp_cpu_add(cpuid, &err);
2311     hmp_handle_error(mon, &err);
2312 }
2313
2314 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
2315 {
2316     const char *args = qdict_get_str(qdict, "args");
2317     Error *err = NULL;
2318     QemuOpts *opts;
2319
2320     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
2321     if (opts == NULL) {
2322         error_setg(&err, "Parsing chardev args failed");
2323     } else {
2324         qemu_chr_new_from_opts(opts, &err);
2325         qemu_opts_del(opts);
2326     }
2327     hmp_handle_error(mon, &err);
2328 }
2329
2330 void hmp_chardev_change(Monitor *mon, const QDict *qdict)
2331 {
2332     const char *args = qdict_get_str(qdict, "args");
2333     const char *id;
2334     Error *err = NULL;
2335     ChardevBackend *backend = NULL;
2336     ChardevReturn *ret = NULL;
2337     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
2338                                              true);
2339     if (!opts) {
2340         error_setg(&err, "Parsing chardev args failed");
2341         goto end;
2342     }
2343
2344     id = qdict_get_str(qdict, "id");
2345     if (qemu_opts_id(opts)) {
2346         error_setg(&err, "Unexpected 'id' parameter");
2347         goto end;
2348     }
2349
2350     backend = qemu_chr_parse_opts(opts, &err);
2351     if (!backend) {
2352         goto end;
2353     }
2354
2355     ret = qmp_chardev_change(id, backend, &err);
2356
2357 end:
2358     qapi_free_ChardevReturn(ret);
2359     qapi_free_ChardevBackend(backend);
2360     qemu_opts_del(opts);
2361     hmp_handle_error(mon, &err);
2362 }
2363
2364 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
2365 {
2366     Error *local_err = NULL;
2367
2368     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
2369     hmp_handle_error(mon, &local_err);
2370 }
2371
2372 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
2373 {
2374     Error *local_err = NULL;
2375
2376     qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
2377     hmp_handle_error(mon, &local_err);
2378 }
2379
2380 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
2381 {
2382     BlockBackend *blk;
2383     BlockBackend *local_blk = NULL;
2384     const char* device = qdict_get_str(qdict, "device");
2385     const char* command = qdict_get_str(qdict, "command");
2386     Error *err = NULL;
2387     int ret;
2388
2389     blk = blk_by_name(device);
2390     if (!blk) {
2391         BlockDriverState *bs = bdrv_lookup_bs(NULL, device, &err);
2392         if (bs) {
2393             blk = local_blk = blk_new(0, BLK_PERM_ALL);
2394             ret = blk_insert_bs(blk, bs, &err);
2395             if (ret < 0) {
2396                 goto fail;
2397             }
2398         } else {
2399             goto fail;
2400         }
2401     }
2402
2403     /*
2404      * Notably absent: Proper permission management. This is sad, but it seems
2405      * almost impossible to achieve without changing the semantics and thereby
2406      * limiting the use cases of the qemu-io HMP command.
2407      *
2408      * In an ideal world we would unconditionally create a new BlockBackend for
2409      * qemuio_command(), but we have commands like 'reopen' and want them to
2410      * take effect on the exact BlockBackend whose name the user passed instead
2411      * of just on a temporary copy of it.
2412      *
2413      * Another problem is that deleting the temporary BlockBackend involves
2414      * draining all requests on it first, but some qemu-iotests cases want to
2415      * issue multiple aio_read/write requests and expect them to complete in
2416      * the background while the monitor has already returned.
2417      *
2418      * This is also what prevents us from saving the original permissions and
2419      * restoring them later: We can't revoke permissions until all requests
2420      * have completed, and we don't know when that is nor can we really let
2421      * anything else run before we have revoken them to avoid race conditions.
2422      *
2423      * What happens now is that command() in qemu-io-cmds.c can extend the
2424      * permissions if necessary for the qemu-io command. And they simply stay
2425      * extended, possibly resulting in a read-only guest device keeping write
2426      * permissions. Ugly, but it appears to be the lesser evil.
2427      */
2428     qemuio_command(blk, command);
2429
2430 fail:
2431     blk_unref(local_blk);
2432     hmp_handle_error(mon, &err);
2433 }
2434
2435 void hmp_object_del(Monitor *mon, const QDict *qdict)
2436 {
2437     const char *id = qdict_get_str(qdict, "id");
2438     Error *err = NULL;
2439
2440     user_creatable_del(id, &err);
2441     hmp_handle_error(mon, &err);
2442 }
2443
2444 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
2445 {
2446     Error *err = NULL;
2447     MemdevList *memdev_list = qmp_query_memdev(&err);
2448     MemdevList *m = memdev_list;
2449     Visitor *v;
2450     char *str;
2451
2452     while (m) {
2453         v = string_output_visitor_new(false, &str);
2454         visit_type_uint16List(v, NULL, &m->value->host_nodes, NULL);
2455         monitor_printf(mon, "memory backend: %s\n", m->value->id);
2456         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
2457         monitor_printf(mon, "  merge: %s\n",
2458                        m->value->merge ? "true" : "false");
2459         monitor_printf(mon, "  dump: %s\n",
2460                        m->value->dump ? "true" : "false");
2461         monitor_printf(mon, "  prealloc: %s\n",
2462                        m->value->prealloc ? "true" : "false");
2463         monitor_printf(mon, "  policy: %s\n",
2464                        HostMemPolicy_str(m->value->policy));
2465         visit_complete(v, &str);
2466         monitor_printf(mon, "  host nodes: %s\n", str);
2467
2468         g_free(str);
2469         visit_free(v);
2470         m = m->next;
2471     }
2472
2473     monitor_printf(mon, "\n");
2474
2475     qapi_free_MemdevList(memdev_list);
2476     hmp_handle_error(mon, &err);
2477 }
2478
2479 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
2480 {
2481     Error *err = NULL;
2482     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
2483     MemoryDeviceInfoList *info;
2484     MemoryDeviceInfo *value;
2485     PCDIMMDeviceInfo *di;
2486
2487     for (info = info_list; info; info = info->next) {
2488         value = info->value;
2489
2490         if (value) {
2491             switch (value->type) {
2492             case MEMORY_DEVICE_INFO_KIND_DIMM:
2493                 di = value->u.dimm.data;
2494                 break;
2495
2496             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
2497                 di = value->u.nvdimm.data;
2498                 break;
2499
2500             default:
2501                 di = NULL;
2502                 break;
2503             }
2504
2505             if (di) {
2506                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
2507                                MemoryDeviceInfoKind_str(value->type),
2508                                di->id ? di->id : "");
2509                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
2510                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2511                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2512                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2513                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2514                 monitor_printf(mon, "  hotplugged: %s\n",
2515                                di->hotplugged ? "true" : "false");
2516                 monitor_printf(mon, "  hotpluggable: %s\n",
2517                                di->hotpluggable ? "true" : "false");
2518             }
2519         }
2520     }
2521
2522     qapi_free_MemoryDeviceInfoList(info_list);
2523     hmp_handle_error(mon, &err);
2524 }
2525
2526 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2527 {
2528     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2529     IOThreadInfoList *info;
2530     IOThreadInfo *value;
2531
2532     for (info = info_list; info; info = info->next) {
2533         value = info->value;
2534         monitor_printf(mon, "%s:\n", value->id);
2535         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
2536         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
2537         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
2538         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
2539     }
2540
2541     qapi_free_IOThreadInfoList(info_list);
2542 }
2543
2544 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2545 {
2546     const char *path = qdict_get_try_str(qdict, "path");
2547     ObjectPropertyInfoList *list;
2548     Error *err = NULL;
2549
2550     if (path == NULL) {
2551         monitor_printf(mon, "/\n");
2552         return;
2553     }
2554
2555     list = qmp_qom_list(path, &err);
2556     if (err == NULL) {
2557         ObjectPropertyInfoList *start = list;
2558         while (list != NULL) {
2559             ObjectPropertyInfo *value = list->value;
2560
2561             monitor_printf(mon, "%s (%s)\n",
2562                            value->name, value->type);
2563             list = list->next;
2564         }
2565         qapi_free_ObjectPropertyInfoList(start);
2566     }
2567     hmp_handle_error(mon, &err);
2568 }
2569
2570 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2571 {
2572     const char *path = qdict_get_str(qdict, "path");
2573     const char *property = qdict_get_str(qdict, "property");
2574     const char *value = qdict_get_str(qdict, "value");
2575     Error *err = NULL;
2576     bool ambiguous = false;
2577     Object *obj;
2578
2579     obj = object_resolve_path(path, &ambiguous);
2580     if (obj == NULL) {
2581         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2582                   "Device '%s' not found", path);
2583     } else {
2584         if (ambiguous) {
2585             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2586         }
2587         object_property_parse(obj, value, property, &err);
2588     }
2589     hmp_handle_error(mon, &err);
2590 }
2591
2592 void hmp_rocker(Monitor *mon, const QDict *qdict)
2593 {
2594     const char *name = qdict_get_str(qdict, "name");
2595     RockerSwitch *rocker;
2596     Error *err = NULL;
2597
2598     rocker = qmp_query_rocker(name, &err);
2599     if (err != NULL) {
2600         hmp_handle_error(mon, &err);
2601         return;
2602     }
2603
2604     monitor_printf(mon, "name: %s\n", rocker->name);
2605     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2606     monitor_printf(mon, "ports: %d\n", rocker->ports);
2607
2608     qapi_free_RockerSwitch(rocker);
2609 }
2610
2611 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2612 {
2613     RockerPortList *list, *port;
2614     const char *name = qdict_get_str(qdict, "name");
2615     Error *err = NULL;
2616
2617     list = qmp_query_rocker_ports(name, &err);
2618     if (err != NULL) {
2619         hmp_handle_error(mon, &err);
2620         return;
2621     }
2622
2623     monitor_printf(mon, "            ena/    speed/ auto\n");
2624     monitor_printf(mon, "      port  link    duplex neg?\n");
2625
2626     for (port = list; port; port = port->next) {
2627         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2628                        port->value->name,
2629                        port->value->enabled ? port->value->link_up ?
2630                        "up" : "down" : "!ena",
2631                        port->value->speed == 10000 ? "10G" : "??",
2632                        port->value->duplex ? "FD" : "HD",
2633                        port->value->autoneg ? "Yes" : "No");
2634     }
2635
2636     qapi_free_RockerPortList(list);
2637 }
2638
2639 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2640 {
2641     RockerOfDpaFlowList *list, *info;
2642     const char *name = qdict_get_str(qdict, "name");
2643     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2644     Error *err = NULL;
2645
2646     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2647     if (err != NULL) {
2648         hmp_handle_error(mon, &err);
2649         return;
2650     }
2651
2652     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2653
2654     for (info = list; info; info = info->next) {
2655         RockerOfDpaFlow *flow = info->value;
2656         RockerOfDpaFlowKey *key = flow->key;
2657         RockerOfDpaFlowMask *mask = flow->mask;
2658         RockerOfDpaFlowAction *action = flow->action;
2659
2660         if (flow->hits) {
2661             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2662                            key->priority, key->tbl_id, flow->hits);
2663         } else {
2664             monitor_printf(mon, "%-4d %-3d     ",
2665                            key->priority, key->tbl_id);
2666         }
2667
2668         if (key->has_in_pport) {
2669             monitor_printf(mon, " pport %d", key->in_pport);
2670             if (mask->has_in_pport) {
2671                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2672             }
2673         }
2674
2675         if (key->has_vlan_id) {
2676             monitor_printf(mon, " vlan %d",
2677                            key->vlan_id & VLAN_VID_MASK);
2678             if (mask->has_vlan_id) {
2679                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2680             }
2681         }
2682
2683         if (key->has_tunnel_id) {
2684             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2685             if (mask->has_tunnel_id) {
2686                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2687             }
2688         }
2689
2690         if (key->has_eth_type) {
2691             switch (key->eth_type) {
2692             case 0x0806:
2693                 monitor_printf(mon, " ARP");
2694                 break;
2695             case 0x0800:
2696                 monitor_printf(mon, " IP");
2697                 break;
2698             case 0x86dd:
2699                 monitor_printf(mon, " IPv6");
2700                 break;
2701             case 0x8809:
2702                 monitor_printf(mon, " LACP");
2703                 break;
2704             case 0x88cc:
2705                 monitor_printf(mon, " LLDP");
2706                 break;
2707             default:
2708                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2709                 break;
2710             }
2711         }
2712
2713         if (key->has_eth_src) {
2714             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2715                 (mask->has_eth_src) &&
2716                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2717                 monitor_printf(mon, " src <any mcast/bcast>");
2718             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2719                 (mask->has_eth_src) &&
2720                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2721                 monitor_printf(mon, " src <any ucast>");
2722             } else {
2723                 monitor_printf(mon, " src %s", key->eth_src);
2724                 if (mask->has_eth_src) {
2725                     monitor_printf(mon, "(%s)", mask->eth_src);
2726                 }
2727             }
2728         }
2729
2730         if (key->has_eth_dst) {
2731             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2732                 (mask->has_eth_dst) &&
2733                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2734                 monitor_printf(mon, " dst <any mcast/bcast>");
2735             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2736                 (mask->has_eth_dst) &&
2737                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2738                 monitor_printf(mon, " dst <any ucast>");
2739             } else {
2740                 monitor_printf(mon, " dst %s", key->eth_dst);
2741                 if (mask->has_eth_dst) {
2742                     monitor_printf(mon, "(%s)", mask->eth_dst);
2743                 }
2744             }
2745         }
2746
2747         if (key->has_ip_proto) {
2748             monitor_printf(mon, " proto %d", key->ip_proto);
2749             if (mask->has_ip_proto) {
2750                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2751             }
2752         }
2753
2754         if (key->has_ip_tos) {
2755             monitor_printf(mon, " TOS %d", key->ip_tos);
2756             if (mask->has_ip_tos) {
2757                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2758             }
2759         }
2760
2761         if (key->has_ip_dst) {
2762             monitor_printf(mon, " dst %s", key->ip_dst);
2763         }
2764
2765         if (action->has_goto_tbl || action->has_group_id ||
2766             action->has_new_vlan_id) {
2767             monitor_printf(mon, " -->");
2768         }
2769
2770         if (action->has_new_vlan_id) {
2771             monitor_printf(mon, " apply new vlan %d",
2772                            ntohs(action->new_vlan_id));
2773         }
2774
2775         if (action->has_group_id) {
2776             monitor_printf(mon, " write group 0x%08x", action->group_id);
2777         }
2778
2779         if (action->has_goto_tbl) {
2780             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2781         }
2782
2783         monitor_printf(mon, "\n");
2784     }
2785
2786     qapi_free_RockerOfDpaFlowList(list);
2787 }
2788
2789 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2790 {
2791     RockerOfDpaGroupList *list, *g;
2792     const char *name = qdict_get_str(qdict, "name");
2793     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2794     Error *err = NULL;
2795     bool set = false;
2796
2797     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2798     if (err != NULL) {
2799         hmp_handle_error(mon, &err);
2800         return;
2801     }
2802
2803     monitor_printf(mon, "id (decode) --> buckets\n");
2804
2805     for (g = list; g; g = g->next) {
2806         RockerOfDpaGroup *group = g->value;
2807
2808         monitor_printf(mon, "0x%08x", group->id);
2809
2810         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2811                                          group->type == 1 ? "L2 rewrite" :
2812                                          group->type == 2 ? "L3 unicast" :
2813                                          group->type == 3 ? "L2 multicast" :
2814                                          group->type == 4 ? "L2 flood" :
2815                                          group->type == 5 ? "L3 interface" :
2816                                          group->type == 6 ? "L3 multicast" :
2817                                          group->type == 7 ? "L3 ECMP" :
2818                                          group->type == 8 ? "L2 overlay" :
2819                                          "unknown");
2820
2821         if (group->has_vlan_id) {
2822             monitor_printf(mon, " vlan %d", group->vlan_id);
2823         }
2824
2825         if (group->has_pport) {
2826             monitor_printf(mon, " pport %d", group->pport);
2827         }
2828
2829         if (group->has_index) {
2830             monitor_printf(mon, " index %d", group->index);
2831         }
2832
2833         monitor_printf(mon, ") -->");
2834
2835         if (group->has_set_vlan_id && group->set_vlan_id) {
2836             set = true;
2837             monitor_printf(mon, " set vlan %d",
2838                            group->set_vlan_id & VLAN_VID_MASK);
2839         }
2840
2841         if (group->has_set_eth_src) {
2842             if (!set) {
2843                 set = true;
2844                 monitor_printf(mon, " set");
2845             }
2846             monitor_printf(mon, " src %s", group->set_eth_src);
2847         }
2848
2849         if (group->has_set_eth_dst) {
2850             if (!set) {
2851                 set = true;
2852                 monitor_printf(mon, " set");
2853             }
2854             monitor_printf(mon, " dst %s", group->set_eth_dst);
2855         }
2856
2857         set = false;
2858
2859         if (group->has_ttl_check && group->ttl_check) {
2860             monitor_printf(mon, " check TTL");
2861         }
2862
2863         if (group->has_group_id && group->group_id) {
2864             monitor_printf(mon, " group id 0x%08x", group->group_id);
2865         }
2866
2867         if (group->has_pop_vlan && group->pop_vlan) {
2868             monitor_printf(mon, " pop vlan");
2869         }
2870
2871         if (group->has_out_pport) {
2872             monitor_printf(mon, " out pport %d", group->out_pport);
2873         }
2874
2875         if (group->has_group_ids) {
2876             struct uint32List *id;
2877
2878             monitor_printf(mon, " groups [");
2879             for (id = group->group_ids; id; id = id->next) {
2880                 monitor_printf(mon, "0x%08x", id->value);
2881                 if (id->next) {
2882                     monitor_printf(mon, ",");
2883                 }
2884             }
2885             monitor_printf(mon, "]");
2886         }
2887
2888         monitor_printf(mon, "\n");
2889     }
2890
2891     qapi_free_RockerOfDpaGroupList(list);
2892 }
2893
2894 void hmp_info_dump(Monitor *mon, const QDict *qdict)
2895 {
2896     DumpQueryResult *result = qmp_query_dump(NULL);
2897
2898     assert(result && result->status < DUMP_STATUS__MAX);
2899     monitor_printf(mon, "Status: %s\n", DumpStatus_str(result->status));
2900
2901     if (result->status == DUMP_STATUS_ACTIVE) {
2902         float percent = 0;
2903         assert(result->total != 0);
2904         percent = 100.0 * result->completed / result->total;
2905         monitor_printf(mon, "Finished: %.2f %%\n", percent);
2906     }
2907
2908     qapi_free_DumpQueryResult(result);
2909 }
2910
2911 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2912 {
2913     ram_block_dump(mon);
2914 }
2915
2916 void hmp_hotpluggable_cpus(Monitor *mon, const QDict *qdict)
2917 {
2918     Error *err = NULL;
2919     HotpluggableCPUList *l = qmp_query_hotpluggable_cpus(&err);
2920     HotpluggableCPUList *saved = l;
2921     CpuInstanceProperties *c;
2922
2923     if (err != NULL) {
2924         hmp_handle_error(mon, &err);
2925         return;
2926     }
2927
2928     monitor_printf(mon, "Hotpluggable CPUs:\n");
2929     while (l) {
2930         monitor_printf(mon, "  type: \"%s\"\n", l->value->type);
2931         monitor_printf(mon, "  vcpus_count: \"%" PRIu64 "\"\n",
2932                        l->value->vcpus_count);
2933         if (l->value->has_qom_path) {
2934             monitor_printf(mon, "  qom_path: \"%s\"\n", l->value->qom_path);
2935         }
2936
2937         c = l->value->props;
2938         monitor_printf(mon, "  CPUInstance Properties:\n");
2939         if (c->has_node_id) {
2940             monitor_printf(mon, "    node-id: \"%" PRIu64 "\"\n", c->node_id);
2941         }
2942         if (c->has_socket_id) {
2943             monitor_printf(mon, "    socket-id: \"%" PRIu64 "\"\n", c->socket_id);
2944         }
2945         if (c->has_core_id) {
2946             monitor_printf(mon, "    core-id: \"%" PRIu64 "\"\n", c->core_id);
2947         }
2948         if (c->has_thread_id) {
2949             monitor_printf(mon, "    thread-id: \"%" PRIu64 "\"\n", c->thread_id);
2950         }
2951
2952         l = l->next;
2953     }
2954
2955     qapi_free_HotpluggableCPUList(saved);
2956 }
2957
2958 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2959 {
2960     Error *err = NULL;
2961     GuidInfo *info = qmp_query_vm_generation_id(&err);
2962     if (info) {
2963         monitor_printf(mon, "%s\n", info->guid);
2964     }
2965     hmp_handle_error(mon, &err);
2966     qapi_free_GuidInfo(info);
2967 }
2968
2969 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
2970 {
2971     Error *err = NULL;
2972     MemoryInfo *info = qmp_query_memory_size_summary(&err);
2973     if (info) {
2974         monitor_printf(mon, "base memory: %" PRIu64 "\n",
2975                        info->base_memory);
2976
2977         if (info->has_plugged_memory) {
2978             monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
2979                            info->plugged_memory);
2980         }
2981
2982         qapi_free_MemoryInfo(info);
2983     }
2984     hmp_handle_error(mon, &err);
2985 }