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