OSDN Git Service

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