OSDN Git Service

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