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