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