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