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