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