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