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