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