]> Git Repo - qemu.git/blob - hmp.c
c6419da72fabcdc581595852dca6126cd5893a17
[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 "sysemu/char.h"
21 #include "sysemu/block-backend.h"
22 #include "qemu/option.h"
23 #include "qemu/timer.h"
24 #include "qmp-commands.h"
25 #include "qemu/sockets.h"
26 #include "monitor/monitor.h"
27 #include "monitor/qdev.h"
28 #include "qapi/opts-visitor.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qapi/string-output-visitor.h"
31 #include "qapi/util.h"
32 #include "qapi-visit.h"
33 #include "ui/console.h"
34 #include "block/qapi.h"
35 #include "qemu-io.h"
36
37 #ifdef CONFIG_SPICE
38 #include <spice/enums.h>
39 #endif
40
41 static void hmp_handle_error(Monitor *mon, Error **errp)
42 {
43     assert(errp);
44     if (*errp) {
45         error_report_err(*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,
323                            cpu->value->u.q_sparc->pc);
324             monitor_printf(mon, " npc=0x%016" PRIx64,
325                            cpu->value->u.q_sparc->npc);
326             break;
327         case CPU_INFO_ARCH_MIPS:
328             monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.q_mips->PC);
329             break;
330         case CPU_INFO_ARCH_TRICORE:
331             monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->u.tricore->PC);
332             break;
333         default:
334             break;
335         }
336
337         if (cpu->value->halted) {
338             monitor_printf(mon, " (halted)");
339         }
340
341         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
342     }
343
344     qapi_free_CpuInfoList(cpu_list);
345 }
346
347 static void print_block_info(Monitor *mon, BlockInfo *info,
348                              BlockDeviceInfo *inserted, bool verbose)
349 {
350     ImageInfo *image_info;
351
352     assert(!info || !info->has_inserted || info->inserted == inserted);
353
354     if (info) {
355         monitor_printf(mon, "%s", info->device);
356         if (inserted && inserted->has_node_name) {
357             monitor_printf(mon, " (%s)", inserted->node_name);
358         }
359     } else {
360         assert(inserted);
361         monitor_printf(mon, "%s",
362                        inserted->has_node_name
363                        ? inserted->node_name
364                        : "<anonymous>");
365     }
366
367     if (inserted) {
368         monitor_printf(mon, ": %s (%s%s%s)\n",
369                        inserted->file,
370                        inserted->drv,
371                        inserted->ro ? ", read-only" : "",
372                        inserted->encrypted ? ", encrypted" : "");
373     } else {
374         monitor_printf(mon, ": [not inserted]\n");
375     }
376
377     if (info) {
378         if (info->has_io_status && info->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
379             monitor_printf(mon, "    I/O status:       %s\n",
380                            BlockDeviceIoStatus_lookup[info->io_status]);
381         }
382
383         if (info->removable) {
384             monitor_printf(mon, "    Removable device: %slocked, tray %s\n",
385                            info->locked ? "" : "not ",
386                            info->tray_open ? "open" : "closed");
387         }
388     }
389
390
391     if (!inserted) {
392         return;
393     }
394
395     monitor_printf(mon, "    Cache mode:       %s%s%s\n",
396                    inserted->cache->writeback ? "writeback" : "writethrough",
397                    inserted->cache->direct ? ", direct" : "",
398                    inserted->cache->no_flush ? ", ignore flushes" : "");
399
400     if (inserted->has_backing_file) {
401         monitor_printf(mon,
402                        "    Backing file:     %s "
403                        "(chain depth: %" PRId64 ")\n",
404                        inserted->backing_file,
405                        inserted->backing_file_depth);
406     }
407
408     if (inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
409         monitor_printf(mon, "    Detect zeroes:    %s\n",
410                        BlockdevDetectZeroesOptions_lookup[inserted->detect_zeroes]);
411     }
412
413     if (inserted->bps  || inserted->bps_rd  || inserted->bps_wr  ||
414         inserted->iops || inserted->iops_rd || inserted->iops_wr)
415     {
416         monitor_printf(mon, "    I/O throttling:   bps=%" PRId64
417                         " bps_rd=%" PRId64  " bps_wr=%" PRId64
418                         " bps_max=%" PRId64
419                         " bps_rd_max=%" PRId64
420                         " bps_wr_max=%" PRId64
421                         " iops=%" PRId64 " iops_rd=%" PRId64
422                         " iops_wr=%" PRId64
423                         " iops_max=%" PRId64
424                         " iops_rd_max=%" PRId64
425                         " iops_wr_max=%" PRId64
426                         " iops_size=%" PRId64
427                         " group=%s\n",
428                         inserted->bps,
429                         inserted->bps_rd,
430                         inserted->bps_wr,
431                         inserted->bps_max,
432                         inserted->bps_rd_max,
433                         inserted->bps_wr_max,
434                         inserted->iops,
435                         inserted->iops_rd,
436                         inserted->iops_wr,
437                         inserted->iops_max,
438                         inserted->iops_rd_max,
439                         inserted->iops_wr_max,
440                         inserted->iops_size,
441                         inserted->group);
442     }
443
444     if (verbose) {
445         monitor_printf(mon, "\nImages:\n");
446         image_info = inserted->image;
447         while (1) {
448                 bdrv_image_info_dump((fprintf_function)monitor_printf,
449                                      mon, image_info);
450             if (image_info->has_backing_image) {
451                 image_info = image_info->backing_image;
452             } else {
453                 break;
454             }
455         }
456     }
457 }
458
459 void hmp_info_block(Monitor *mon, const QDict *qdict)
460 {
461     BlockInfoList *block_list, *info;
462     BlockDeviceInfoList *blockdev_list, *blockdev;
463     const char *device = qdict_get_try_str(qdict, "device");
464     bool verbose = qdict_get_try_bool(qdict, "verbose", false);
465     bool nodes = qdict_get_try_bool(qdict, "nodes", false);
466     bool printed = false;
467
468     /* Print BlockBackend information */
469     if (!nodes) {
470         block_list = qmp_query_block(NULL);
471     } else {
472         block_list = NULL;
473     }
474
475     for (info = block_list; info; info = info->next) {
476         if (device && strcmp(device, info->value->device)) {
477             continue;
478         }
479
480         if (info != block_list) {
481             monitor_printf(mon, "\n");
482         }
483
484         print_block_info(mon, info->value, info->value->has_inserted
485                                            ? info->value->inserted : NULL,
486                          verbose);
487         printed = true;
488     }
489
490     qapi_free_BlockInfoList(block_list);
491
492     if ((!device && !nodes) || printed) {
493         return;
494     }
495
496     /* Print node information */
497     blockdev_list = qmp_query_named_block_nodes(NULL);
498     for (blockdev = blockdev_list; blockdev; blockdev = blockdev->next) {
499         assert(blockdev->value->has_node_name);
500         if (device && strcmp(device, blockdev->value->node_name)) {
501             continue;
502         }
503
504         if (blockdev != blockdev_list) {
505             monitor_printf(mon, "\n");
506         }
507
508         print_block_info(mon, NULL, blockdev->value, verbose);
509     }
510     qapi_free_BlockDeviceInfoList(blockdev_list);
511 }
512
513 void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
514 {
515     BlockStatsList *stats_list, *stats;
516
517     stats_list = qmp_query_blockstats(false, false, NULL);
518
519     for (stats = stats_list; stats; stats = stats->next) {
520         if (!stats->value->has_device) {
521             continue;
522         }
523
524         monitor_printf(mon, "%s:", stats->value->device);
525         monitor_printf(mon, " rd_bytes=%" PRId64
526                        " wr_bytes=%" PRId64
527                        " rd_operations=%" PRId64
528                        " wr_operations=%" PRId64
529                        " flush_operations=%" PRId64
530                        " wr_total_time_ns=%" PRId64
531                        " rd_total_time_ns=%" PRId64
532                        " flush_total_time_ns=%" PRId64
533                        " rd_merged=%" PRId64
534                        " wr_merged=%" PRId64
535                        " idle_time_ns=%" PRId64
536                        "\n",
537                        stats->value->stats->rd_bytes,
538                        stats->value->stats->wr_bytes,
539                        stats->value->stats->rd_operations,
540                        stats->value->stats->wr_operations,
541                        stats->value->stats->flush_operations,
542                        stats->value->stats->wr_total_time_ns,
543                        stats->value->stats->rd_total_time_ns,
544                        stats->value->stats->flush_total_time_ns,
545                        stats->value->stats->rd_merged,
546                        stats->value->stats->wr_merged,
547                        stats->value->stats->idle_time_ns);
548     }
549
550     qapi_free_BlockStatsList(stats_list);
551 }
552
553 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
554 {
555     VncInfo *info;
556     Error *err = NULL;
557     VncClientInfoList *client;
558
559     info = qmp_query_vnc(&err);
560     if (err) {
561         error_report_err(err);
562         return;
563     }
564
565     if (!info->enabled) {
566         monitor_printf(mon, "Server: disabled\n");
567         goto out;
568     }
569
570     monitor_printf(mon, "Server:\n");
571     if (info->has_host && info->has_service) {
572         monitor_printf(mon, "     address: %s:%s\n", info->host, info->service);
573     }
574     if (info->has_auth) {
575         monitor_printf(mon, "        auth: %s\n", info->auth);
576     }
577
578     if (!info->has_clients || info->clients == NULL) {
579         monitor_printf(mon, "Client: none\n");
580     } else {
581         for (client = info->clients; client; client = client->next) {
582             monitor_printf(mon, "Client:\n");
583             monitor_printf(mon, "     address: %s:%s\n",
584                            client->value->host,
585                            client->value->service);
586             monitor_printf(mon, "  x509_dname: %s\n",
587                            client->value->x509_dname ?
588                            client->value->x509_dname : "none");
589             monitor_printf(mon, "    username: %s\n",
590                            client->value->has_sasl_username ?
591                            client->value->sasl_username : "none");
592         }
593     }
594
595 out:
596     qapi_free_VncInfo(info);
597 }
598
599 #ifdef CONFIG_SPICE
600 void hmp_info_spice(Monitor *mon, const QDict *qdict)
601 {
602     SpiceChannelList *chan;
603     SpiceInfo *info;
604     const char *channel_name;
605     const char * const channel_names[] = {
606         [SPICE_CHANNEL_MAIN] = "main",
607         [SPICE_CHANNEL_DISPLAY] = "display",
608         [SPICE_CHANNEL_INPUTS] = "inputs",
609         [SPICE_CHANNEL_CURSOR] = "cursor",
610         [SPICE_CHANNEL_PLAYBACK] = "playback",
611         [SPICE_CHANNEL_RECORD] = "record",
612         [SPICE_CHANNEL_TUNNEL] = "tunnel",
613         [SPICE_CHANNEL_SMARTCARD] = "smartcard",
614         [SPICE_CHANNEL_USBREDIR] = "usbredir",
615         [SPICE_CHANNEL_PORT] = "port",
616 #if 0
617         /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
618          * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
619          * as quick fix for build failures with older versions. */
620         [SPICE_CHANNEL_WEBDAV] = "webdav",
621 #endif
622     };
623
624     info = qmp_query_spice(NULL);
625
626     if (!info->enabled) {
627         monitor_printf(mon, "Server: disabled\n");
628         goto out;
629     }
630
631     monitor_printf(mon, "Server:\n");
632     if (info->has_port) {
633         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
634                        info->host, info->port);
635     }
636     if (info->has_tls_port) {
637         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
638                        info->host, info->tls_port);
639     }
640     monitor_printf(mon, "    migrated: %s\n",
641                    info->migrated ? "true" : "false");
642     monitor_printf(mon, "        auth: %s\n", info->auth);
643     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
644     monitor_printf(mon, "  mouse-mode: %s\n",
645                    SpiceQueryMouseMode_lookup[info->mouse_mode]);
646
647     if (!info->has_channels || info->channels == NULL) {
648         monitor_printf(mon, "Channels: none\n");
649     } else {
650         for (chan = info->channels; chan; chan = chan->next) {
651             monitor_printf(mon, "Channel:\n");
652             monitor_printf(mon, "     address: %s:%s%s\n",
653                            chan->value->host, chan->value->port,
654                            chan->value->tls ? " [tls]" : "");
655             monitor_printf(mon, "     session: %" PRId64 "\n",
656                            chan->value->connection_id);
657             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
658                            chan->value->channel_type, chan->value->channel_id);
659
660             channel_name = "unknown";
661             if (chan->value->channel_type > 0 &&
662                 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
663                 channel_names[chan->value->channel_type]) {
664                 channel_name = channel_names[chan->value->channel_type];
665             }
666
667             monitor_printf(mon, "     channel name: %s\n", channel_name);
668         }
669     }
670
671 out:
672     qapi_free_SpiceInfo(info);
673 }
674 #endif
675
676 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
677 {
678     BalloonInfo *info;
679     Error *err = NULL;
680
681     info = qmp_query_balloon(&err);
682     if (err) {
683         error_report_err(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         error_report_err(err);
952         return;
953     }
954
955     for (i = 0; data[i]; i++) {
956         unsigned char ch = data[i];
957
958         if (ch == '\\') {
959             monitor_printf(mon, "\\\\");
960         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
961             monitor_printf(mon, "\\u%04X", ch);
962         } else {
963             monitor_printf(mon, "%c", ch);
964         }
965
966     }
967     monitor_printf(mon, "\n");
968     g_free(data);
969 }
970
971 static void hmp_cont_cb(void *opaque, int err)
972 {
973     if (!err) {
974         qmp_cont(NULL);
975     }
976 }
977
978 static bool key_is_missing(const BlockInfo *bdev)
979 {
980     return (bdev->inserted && bdev->inserted->encryption_key_missing);
981 }
982
983 void hmp_cont(Monitor *mon, const QDict *qdict)
984 {
985     BlockInfoList *bdev_list, *bdev;
986     Error *err = NULL;
987
988     bdev_list = qmp_query_block(NULL);
989     for (bdev = bdev_list; bdev; bdev = bdev->next) {
990         if (key_is_missing(bdev->value)) {
991             monitor_read_block_device_key(mon, bdev->value->device,
992                                           hmp_cont_cb, NULL);
993             goto out;
994         }
995     }
996
997     qmp_cont(&err);
998     hmp_handle_error(mon, &err);
999
1000 out:
1001     qapi_free_BlockInfoList(bdev_list);
1002 }
1003
1004 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1005 {
1006     qmp_system_wakeup(NULL);
1007 }
1008
1009 void hmp_nmi(Monitor *mon, const QDict *qdict)
1010 {
1011     Error *err = NULL;
1012
1013     qmp_inject_nmi(&err);
1014     hmp_handle_error(mon, &err);
1015 }
1016
1017 void hmp_set_link(Monitor *mon, const QDict *qdict)
1018 {
1019     const char *name = qdict_get_str(qdict, "name");
1020     bool up = qdict_get_bool(qdict, "up");
1021     Error *err = NULL;
1022
1023     qmp_set_link(name, up, &err);
1024     hmp_handle_error(mon, &err);
1025 }
1026
1027 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
1028 {
1029     const char *device = qdict_get_str(qdict, "device");
1030     const char *password = qdict_get_str(qdict, "password");
1031     Error *err = NULL;
1032
1033     qmp_block_passwd(true, device, false, NULL, password, &err);
1034     hmp_handle_error(mon, &err);
1035 }
1036
1037 void hmp_balloon(Monitor *mon, const QDict *qdict)
1038 {
1039     int64_t value = qdict_get_int(qdict, "value");
1040     Error *err = NULL;
1041
1042     qmp_balloon(value, &err);
1043     if (err) {
1044         error_report_err(err);
1045     }
1046 }
1047
1048 void hmp_block_resize(Monitor *mon, const QDict *qdict)
1049 {
1050     const char *device = qdict_get_str(qdict, "device");
1051     int64_t size = qdict_get_int(qdict, "size");
1052     Error *err = NULL;
1053
1054     qmp_block_resize(true, device, false, NULL, size, &err);
1055     hmp_handle_error(mon, &err);
1056 }
1057
1058 void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
1059 {
1060     const char *device = qdict_get_str(qdict, "device");
1061     const char *filename = qdict_get_str(qdict, "target");
1062     const char *format = qdict_get_try_str(qdict, "format");
1063     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1064     bool full = qdict_get_try_bool(qdict, "full", false);
1065     enum NewImageMode mode;
1066     Error *err = NULL;
1067
1068     if (!filename) {
1069         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1070         hmp_handle_error(mon, &err);
1071         return;
1072     }
1073
1074     if (reuse) {
1075         mode = NEW_IMAGE_MODE_EXISTING;
1076     } else {
1077         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1078     }
1079
1080     qmp_drive_mirror(device, filename, !!format, format,
1081                      false, NULL, false, NULL,
1082                      full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1083                      true, mode, false, 0, false, 0, false, 0,
1084                      false, 0, false, 0, false, true, &err);
1085     hmp_handle_error(mon, &err);
1086 }
1087
1088 void hmp_drive_backup(Monitor *mon, const QDict *qdict)
1089 {
1090     const char *device = qdict_get_str(qdict, "device");
1091     const char *filename = qdict_get_str(qdict, "target");
1092     const char *format = qdict_get_try_str(qdict, "format");
1093     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1094     bool full = qdict_get_try_bool(qdict, "full", false);
1095     enum NewImageMode mode;
1096     Error *err = NULL;
1097
1098     if (!filename) {
1099         error_setg(&err, QERR_MISSING_PARAMETER, "target");
1100         hmp_handle_error(mon, &err);
1101         return;
1102     }
1103
1104     if (reuse) {
1105         mode = NEW_IMAGE_MODE_EXISTING;
1106     } else {
1107         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1108     }
1109
1110     qmp_drive_backup(device, filename, !!format, format,
1111                      full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
1112                      true, mode, false, 0, false, NULL,
1113                      false, 0, false, 0, &err);
1114     hmp_handle_error(mon, &err);
1115 }
1116
1117 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
1118 {
1119     const char *device = qdict_get_str(qdict, "device");
1120     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
1121     const char *format = qdict_get_try_str(qdict, "format");
1122     bool reuse = qdict_get_try_bool(qdict, "reuse", false);
1123     enum NewImageMode mode;
1124     Error *err = NULL;
1125
1126     if (!filename) {
1127         /* In the future, if 'snapshot-file' is not specified, the snapshot
1128            will be taken internally. Today it's actually required. */
1129         error_setg(&err, QERR_MISSING_PARAMETER, "snapshot-file");
1130         hmp_handle_error(mon, &err);
1131         return;
1132     }
1133
1134     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
1135     qmp_blockdev_snapshot_sync(true, device, false, NULL,
1136                                filename, false, NULL,
1137                                !!format, format,
1138                                true, mode, &err);
1139     hmp_handle_error(mon, &err);
1140 }
1141
1142 void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
1143 {
1144     const char *device = qdict_get_str(qdict, "device");
1145     const char *name = qdict_get_str(qdict, "name");
1146     Error *err = NULL;
1147
1148     qmp_blockdev_snapshot_internal_sync(device, name, &err);
1149     hmp_handle_error(mon, &err);
1150 }
1151
1152 void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1153 {
1154     const char *device = qdict_get_str(qdict, "device");
1155     const char *name = qdict_get_str(qdict, "name");
1156     const char *id = qdict_get_try_str(qdict, "id");
1157     Error *err = NULL;
1158
1159     qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
1160                                                true, name, &err);
1161     hmp_handle_error(mon, &err);
1162 }
1163
1164 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1165 {
1166     qmp_migrate_cancel(NULL);
1167 }
1168
1169 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1170 {
1171     Error *err = NULL;
1172     const char *uri = qdict_get_str(qdict, "uri");
1173
1174     qmp_migrate_incoming(uri, &err);
1175
1176     hmp_handle_error(mon, &err);
1177 }
1178
1179 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1180 {
1181     double value = qdict_get_double(qdict, "value");
1182     qmp_migrate_set_downtime(value, NULL);
1183 }
1184
1185 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1186 {
1187     int64_t value = qdict_get_int(qdict, "value");
1188     Error *err = NULL;
1189
1190     qmp_migrate_set_cache_size(value, &err);
1191     if (err) {
1192         error_report_err(err);
1193         return;
1194     }
1195 }
1196
1197 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1198 {
1199     int64_t value = qdict_get_int(qdict, "value");
1200     qmp_migrate_set_speed(value, NULL);
1201 }
1202
1203 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1204 {
1205     const char *cap = qdict_get_str(qdict, "capability");
1206     bool state = qdict_get_bool(qdict, "state");
1207     Error *err = NULL;
1208     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1209     int i;
1210
1211     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
1212         if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1213             caps->value = g_malloc0(sizeof(*caps->value));
1214             caps->value->capability = i;
1215             caps->value->state = state;
1216             caps->next = NULL;
1217             qmp_migrate_set_capabilities(caps, &err);
1218             break;
1219         }
1220     }
1221
1222     if (i == MIGRATION_CAPABILITY__MAX) {
1223         error_setg(&err, QERR_INVALID_PARAMETER, cap);
1224     }
1225
1226     qapi_free_MigrationCapabilityStatusList(caps);
1227
1228     if (err) {
1229         error_report_err(err);
1230     }
1231 }
1232
1233 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1234 {
1235     const char *param = qdict_get_str(qdict, "parameter");
1236     int value = qdict_get_int(qdict, "value");
1237     Error *err = NULL;
1238     bool has_compress_level = false;
1239     bool has_compress_threads = false;
1240     bool has_decompress_threads = false;
1241     bool has_x_cpu_throttle_initial = false;
1242     bool has_x_cpu_throttle_increment = false;
1243     int i;
1244
1245     for (i = 0; i < MIGRATION_PARAMETER__MAX; i++) {
1246         if (strcmp(param, MigrationParameter_lookup[i]) == 0) {
1247             switch (i) {
1248             case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1249                 has_compress_level = true;
1250                 break;
1251             case MIGRATION_PARAMETER_COMPRESS_THREADS:
1252                 has_compress_threads = true;
1253                 break;
1254             case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1255                 has_decompress_threads = true;
1256                 break;
1257             case MIGRATION_PARAMETER_X_CPU_THROTTLE_INITIAL:
1258                 has_x_cpu_throttle_initial = true;
1259                 break;
1260             case MIGRATION_PARAMETER_X_CPU_THROTTLE_INCREMENT:
1261                 has_x_cpu_throttle_increment = true;
1262                 break;
1263             }
1264             qmp_migrate_set_parameters(has_compress_level, value,
1265                                        has_compress_threads, value,
1266                                        has_decompress_threads, value,
1267                                        has_x_cpu_throttle_initial, value,
1268                                        has_x_cpu_throttle_increment, value,
1269                                        &err);
1270             break;
1271         }
1272     }
1273
1274     if (i == MIGRATION_PARAMETER__MAX) {
1275         error_setg(&err, QERR_INVALID_PARAMETER, param);
1276     }
1277
1278     if (err) {
1279         error_report_err(err);
1280     }
1281 }
1282
1283 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1284 {
1285     Error *err = NULL;
1286     const char *protocol = qdict_get_str(qdict, "protocol");
1287     const char *hostname = qdict_get_str(qdict, "hostname");
1288     bool has_port        = qdict_haskey(qdict, "port");
1289     int port             = qdict_get_try_int(qdict, "port", -1);
1290     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1291     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1292     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1293
1294     qmp_client_migrate_info(protocol, hostname,
1295                             has_port, port, has_tls_port, tls_port,
1296                             !!cert_subject, cert_subject, &err);
1297     hmp_handle_error(mon, &err);
1298 }
1299
1300 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1301 {
1302     Error *err = NULL;
1303     qmp_migrate_start_postcopy(&err);
1304     hmp_handle_error(mon, &err);
1305 }
1306
1307 void hmp_set_password(Monitor *mon, const QDict *qdict)
1308 {
1309     const char *protocol  = qdict_get_str(qdict, "protocol");
1310     const char *password  = qdict_get_str(qdict, "password");
1311     const char *connected = qdict_get_try_str(qdict, "connected");
1312     Error *err = NULL;
1313
1314     qmp_set_password(protocol, password, !!connected, connected, &err);
1315     hmp_handle_error(mon, &err);
1316 }
1317
1318 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1319 {
1320     const char *protocol  = qdict_get_str(qdict, "protocol");
1321     const char *whenstr = qdict_get_str(qdict, "time");
1322     Error *err = NULL;
1323
1324     qmp_expire_password(protocol, whenstr, &err);
1325     hmp_handle_error(mon, &err);
1326 }
1327
1328 void hmp_eject(Monitor *mon, const QDict *qdict)
1329 {
1330     bool force = qdict_get_try_bool(qdict, "force", false);
1331     const char *device = qdict_get_str(qdict, "device");
1332     Error *err = NULL;
1333
1334     qmp_eject(device, true, force, &err);
1335     hmp_handle_error(mon, &err);
1336 }
1337
1338 static void hmp_change_read_arg(void *opaque, const char *password,
1339                                 void *readline_opaque)
1340 {
1341     qmp_change_vnc_password(password, NULL);
1342     monitor_read_command(opaque, 1);
1343 }
1344
1345 void hmp_change(Monitor *mon, const QDict *qdict)
1346 {
1347     const char *device = qdict_get_str(qdict, "device");
1348     const char *target = qdict_get_str(qdict, "target");
1349     const char *arg = qdict_get_try_str(qdict, "arg");
1350     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1351     BlockdevChangeReadOnlyMode read_only_mode = 0;
1352     Error *err = NULL;
1353
1354     if (strcmp(device, "vnc") == 0) {
1355         if (read_only) {
1356             monitor_printf(mon,
1357                            "Parameter 'read-only-mode' is invalid for VNC\n");
1358             return;
1359         }
1360         if (strcmp(target, "passwd") == 0 ||
1361             strcmp(target, "password") == 0) {
1362             if (!arg) {
1363                 monitor_read_password(mon, hmp_change_read_arg, NULL);
1364                 return;
1365             }
1366         }
1367         qmp_change("vnc", target, !!arg, arg, &err);
1368     } else {
1369         if (read_only) {
1370             read_only_mode =
1371                 qapi_enum_parse(BlockdevChangeReadOnlyMode_lookup,
1372                                 read_only, BLOCKDEV_CHANGE_READ_ONLY_MODE__MAX,
1373                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1374             if (err) {
1375                 hmp_handle_error(mon, &err);
1376                 return;
1377             }
1378         }
1379
1380         qmp_blockdev_change_medium(device, target, !!arg, arg,
1381                                    !!read_only, read_only_mode, &err);
1382         if (err &&
1383             error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
1384             error_free(err);
1385             monitor_read_block_device_key(mon, device, NULL, NULL);
1386             return;
1387         }
1388     }
1389
1390     hmp_handle_error(mon, &err);
1391 }
1392
1393 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1394 {
1395     Error *err = NULL;
1396
1397     qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1398                               qdict_get_int(qdict, "bps"),
1399                               qdict_get_int(qdict, "bps_rd"),
1400                               qdict_get_int(qdict, "bps_wr"),
1401                               qdict_get_int(qdict, "iops"),
1402                               qdict_get_int(qdict, "iops_rd"),
1403                               qdict_get_int(qdict, "iops_wr"),
1404                               false, /* no burst max via HMP */
1405                               0,
1406                               false,
1407                               0,
1408                               false,
1409                               0,
1410                               false,
1411                               0,
1412                               false,
1413                               0,
1414                               false,
1415                               0,
1416                               false, /* No default I/O size */
1417                               0,
1418                               false,
1419                               NULL, &err);
1420     hmp_handle_error(mon, &err);
1421 }
1422
1423 void hmp_block_stream(Monitor *mon, const QDict *qdict)
1424 {
1425     Error *error = NULL;
1426     const char *device = qdict_get_str(qdict, "device");
1427     const char *base = qdict_get_try_str(qdict, "base");
1428     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
1429
1430     qmp_block_stream(device, base != NULL, base, false, NULL,
1431                      qdict_haskey(qdict, "speed"), speed,
1432                      true, BLOCKDEV_ON_ERROR_REPORT, &error);
1433
1434     hmp_handle_error(mon, &error);
1435 }
1436
1437 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1438 {
1439     Error *error = NULL;
1440     const char *device = qdict_get_str(qdict, "device");
1441     int64_t value = qdict_get_int(qdict, "speed");
1442
1443     qmp_block_job_set_speed(device, value, &error);
1444
1445     hmp_handle_error(mon, &error);
1446 }
1447
1448 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1449 {
1450     Error *error = NULL;
1451     const char *device = qdict_get_str(qdict, "device");
1452     bool force = qdict_get_try_bool(qdict, "force", false);
1453
1454     qmp_block_job_cancel(device, true, force, &error);
1455
1456     hmp_handle_error(mon, &error);
1457 }
1458
1459 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1460 {
1461     Error *error = NULL;
1462     const char *device = qdict_get_str(qdict, "device");
1463
1464     qmp_block_job_pause(device, &error);
1465
1466     hmp_handle_error(mon, &error);
1467 }
1468
1469 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1470 {
1471     Error *error = NULL;
1472     const char *device = qdict_get_str(qdict, "device");
1473
1474     qmp_block_job_resume(device, &error);
1475
1476     hmp_handle_error(mon, &error);
1477 }
1478
1479 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1480 {
1481     Error *error = NULL;
1482     const char *device = qdict_get_str(qdict, "device");
1483
1484     qmp_block_job_complete(device, &error);
1485
1486     hmp_handle_error(mon, &error);
1487 }
1488
1489 typedef struct HMPMigrationStatus
1490 {
1491     QEMUTimer *timer;
1492     Monitor *mon;
1493     bool is_block_migration;
1494 } HMPMigrationStatus;
1495
1496 static void hmp_migrate_status_cb(void *opaque)
1497 {
1498     HMPMigrationStatus *status = opaque;
1499     MigrationInfo *info;
1500
1501     info = qmp_query_migrate(NULL);
1502     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1503         info->status == MIGRATION_STATUS_SETUP) {
1504         if (info->has_disk) {
1505             int progress;
1506
1507             if (info->disk->remaining) {
1508                 progress = info->disk->transferred * 100 / info->disk->total;
1509             } else {
1510                 progress = 100;
1511             }
1512
1513             monitor_printf(status->mon, "Completed %d %%\r", progress);
1514             monitor_flush(status->mon);
1515         }
1516
1517         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1518     } else {
1519         if (status->is_block_migration) {
1520             monitor_printf(status->mon, "\n");
1521         }
1522         monitor_resume(status->mon);
1523         timer_del(status->timer);
1524         g_free(status);
1525     }
1526
1527     qapi_free_MigrationInfo(info);
1528 }
1529
1530 void hmp_migrate(Monitor *mon, const QDict *qdict)
1531 {
1532     bool detach = qdict_get_try_bool(qdict, "detach", false);
1533     bool blk = qdict_get_try_bool(qdict, "blk", false);
1534     bool inc = qdict_get_try_bool(qdict, "inc", false);
1535     const char *uri = qdict_get_str(qdict, "uri");
1536     Error *err = NULL;
1537
1538     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1539     if (err) {
1540         error_report_err(err);
1541         return;
1542     }
1543
1544     if (!detach) {
1545         HMPMigrationStatus *status;
1546
1547         if (monitor_suspend(mon) < 0) {
1548             monitor_printf(mon, "terminal does not allow synchronous "
1549                            "migration, continuing detached\n");
1550             return;
1551         }
1552
1553         status = g_malloc0(sizeof(*status));
1554         status->mon = mon;
1555         status->is_block_migration = blk || inc;
1556         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1557                                           status);
1558         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1559     }
1560 }
1561
1562 void hmp_device_add(Monitor *mon, const QDict *qdict)
1563 {
1564     Error *err = NULL;
1565
1566     qmp_device_add((QDict *)qdict, NULL, &err);
1567     hmp_handle_error(mon, &err);
1568 }
1569
1570 void hmp_device_del(Monitor *mon, const QDict *qdict)
1571 {
1572     const char *id = qdict_get_str(qdict, "id");
1573     Error *err = NULL;
1574
1575     qmp_device_del(id, &err);
1576     hmp_handle_error(mon, &err);
1577 }
1578
1579 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1580 {
1581     Error *err = NULL;
1582     bool paging = qdict_get_try_bool(qdict, "paging", false);
1583     bool zlib = qdict_get_try_bool(qdict, "zlib", false);
1584     bool lzo = qdict_get_try_bool(qdict, "lzo", false);
1585     bool snappy = qdict_get_try_bool(qdict, "snappy", false);
1586     const char *file = qdict_get_str(qdict, "filename");
1587     bool has_begin = qdict_haskey(qdict, "begin");
1588     bool has_length = qdict_haskey(qdict, "length");
1589     int64_t begin = 0;
1590     int64_t length = 0;
1591     enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
1592     char *prot;
1593
1594     if (zlib + lzo + snappy > 1) {
1595         error_setg(&err, "only one of '-z|-l|-s' can be set");
1596         hmp_handle_error(mon, &err);
1597         return;
1598     }
1599
1600     if (zlib) {
1601         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1602     }
1603
1604     if (lzo) {
1605         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1606     }
1607
1608     if (snappy) {
1609         dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1610     }
1611
1612     if (has_begin) {
1613         begin = qdict_get_int(qdict, "begin");
1614     }
1615     if (has_length) {
1616         length = qdict_get_int(qdict, "length");
1617     }
1618
1619     prot = g_strconcat("file:", file, NULL);
1620
1621     qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1622                           true, dump_format, &err);
1623     hmp_handle_error(mon, &err);
1624     g_free(prot);
1625 }
1626
1627 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1628 {
1629     Error *err = NULL;
1630     QemuOpts *opts;
1631
1632     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1633     if (err) {
1634         goto out;
1635     }
1636
1637     netdev_add(opts, &err);
1638     if (err) {
1639         qemu_opts_del(opts);
1640     }
1641
1642 out:
1643     hmp_handle_error(mon, &err);
1644 }
1645
1646 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1647 {
1648     const char *id = qdict_get_str(qdict, "id");
1649     Error *err = NULL;
1650
1651     qmp_netdev_del(id, &err);
1652     hmp_handle_error(mon, &err);
1653 }
1654
1655 void hmp_object_add(Monitor *mon, const QDict *qdict)
1656 {
1657     Error *err = NULL;
1658     Error *err_end = NULL;
1659     QemuOpts *opts;
1660     char *type = NULL;
1661     char *id = NULL;
1662     OptsVisitor *ov;
1663     QDict *pdict;
1664     Visitor *v;
1665
1666     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1667     if (err) {
1668         goto out;
1669     }
1670
1671     ov = opts_visitor_new(opts);
1672     pdict = qdict_clone_shallow(qdict);
1673     v = opts_get_visitor(ov);
1674
1675     visit_start_struct(v, NULL, NULL, 0, &err);
1676     if (err) {
1677         goto out_clean;
1678     }
1679
1680     qdict_del(pdict, "qom-type");
1681     visit_type_str(v, "qom-type", &type, &err);
1682     if (err) {
1683         goto out_end;
1684     }
1685
1686     qdict_del(pdict, "id");
1687     visit_type_str(v, "id", &id, &err);
1688     if (err) {
1689         goto out_end;
1690     }
1691
1692     object_add(type, id, pdict, v, &err);
1693
1694 out_end:
1695     visit_end_struct(v, &err_end);
1696     if (!err && err_end) {
1697         qmp_object_del(id, NULL);
1698     }
1699     error_propagate(&err, err_end);
1700 out_clean:
1701     opts_visitor_cleanup(ov);
1702
1703     QDECREF(pdict);
1704     qemu_opts_del(opts);
1705     g_free(id);
1706     g_free(type);
1707
1708 out:
1709     hmp_handle_error(mon, &err);
1710 }
1711
1712 void hmp_getfd(Monitor *mon, const QDict *qdict)
1713 {
1714     const char *fdname = qdict_get_str(qdict, "fdname");
1715     Error *err = NULL;
1716
1717     qmp_getfd(fdname, &err);
1718     hmp_handle_error(mon, &err);
1719 }
1720
1721 void hmp_closefd(Monitor *mon, const QDict *qdict)
1722 {
1723     const char *fdname = qdict_get_str(qdict, "fdname");
1724     Error *err = NULL;
1725
1726     qmp_closefd(fdname, &err);
1727     hmp_handle_error(mon, &err);
1728 }
1729
1730 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1731 {
1732     const char *keys = qdict_get_str(qdict, "keys");
1733     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1734     int has_hold_time = qdict_haskey(qdict, "hold-time");
1735     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1736     Error *err = NULL;
1737     char *separator;
1738     int keyname_len;
1739
1740     while (1) {
1741         separator = strchr(keys, '-');
1742         keyname_len = separator ? separator - keys : strlen(keys);
1743
1744         /* Be compatible with old interface, convert user inputted "<" */
1745         if (keys[0] == '<' && keyname_len == 1) {
1746             keys = "less";
1747             keyname_len = 4;
1748         }
1749
1750         keylist = g_malloc0(sizeof(*keylist));
1751         keylist->value = g_malloc0(sizeof(*keylist->value));
1752
1753         if (!head) {
1754             head = keylist;
1755         }
1756         if (tmp) {
1757             tmp->next = keylist;
1758         }
1759         tmp = keylist;
1760
1761         if (strstart(keys, "0x", NULL)) {
1762             char *endp;
1763             int value = strtoul(keys, &endp, 0);
1764             assert(endp <= keys + keyname_len);
1765             if (endp != keys + keyname_len) {
1766                 goto err_out;
1767             }
1768             keylist->value->type = KEY_VALUE_KIND_NUMBER;
1769             keylist->value->u.number = value;
1770         } else {
1771             int idx = index_from_key(keys, keyname_len);
1772             if (idx == Q_KEY_CODE__MAX) {
1773                 goto err_out;
1774             }
1775             keylist->value->type = KEY_VALUE_KIND_QCODE;
1776             keylist->value->u.qcode = idx;
1777         }
1778
1779         if (!separator) {
1780             break;
1781         }
1782         keys = separator + 1;
1783     }
1784
1785     qmp_send_key(head, has_hold_time, hold_time, &err);
1786     hmp_handle_error(mon, &err);
1787
1788 out:
1789     qapi_free_KeyValueList(head);
1790     return;
1791
1792 err_out:
1793     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
1794     goto out;
1795 }
1796
1797 void hmp_screendump(Monitor *mon, const QDict *qdict)
1798 {
1799     const char *filename = qdict_get_str(qdict, "filename");
1800     Error *err = NULL;
1801
1802     qmp_screendump(filename, &err);
1803     hmp_handle_error(mon, &err);
1804 }
1805
1806 void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1807 {
1808     const char *uri = qdict_get_str(qdict, "uri");
1809     bool writable = qdict_get_try_bool(qdict, "writable", false);
1810     bool all = qdict_get_try_bool(qdict, "all", false);
1811     Error *local_err = NULL;
1812     BlockInfoList *block_list, *info;
1813     SocketAddress *addr;
1814
1815     if (writable && !all) {
1816         error_setg(&local_err, "-w only valid together with -a");
1817         goto exit;
1818     }
1819
1820     /* First check if the address is valid and start the server.  */
1821     addr = socket_parse(uri, &local_err);
1822     if (local_err != NULL) {
1823         goto exit;
1824     }
1825
1826     qmp_nbd_server_start(addr, &local_err);
1827     qapi_free_SocketAddress(addr);
1828     if (local_err != NULL) {
1829         goto exit;
1830     }
1831
1832     if (!all) {
1833         return;
1834     }
1835
1836     /* Then try adding all block devices.  If one fails, close all and
1837      * exit.
1838      */
1839     block_list = qmp_query_block(NULL);
1840
1841     for (info = block_list; info; info = info->next) {
1842         if (!info->value->has_inserted) {
1843             continue;
1844         }
1845
1846         qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1847
1848         if (local_err != NULL) {
1849             qmp_nbd_server_stop(NULL);
1850             break;
1851         }
1852     }
1853
1854     qapi_free_BlockInfoList(block_list);
1855
1856 exit:
1857     hmp_handle_error(mon, &local_err);
1858 }
1859
1860 void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1861 {
1862     const char *device = qdict_get_str(qdict, "device");
1863     bool writable = qdict_get_try_bool(qdict, "writable", false);
1864     Error *local_err = NULL;
1865
1866     qmp_nbd_server_add(device, true, writable, &local_err);
1867
1868     if (local_err != NULL) {
1869         hmp_handle_error(mon, &local_err);
1870     }
1871 }
1872
1873 void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1874 {
1875     Error *err = NULL;
1876
1877     qmp_nbd_server_stop(&err);
1878     hmp_handle_error(mon, &err);
1879 }
1880
1881 void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1882 {
1883     int cpuid;
1884     Error *err = NULL;
1885
1886     cpuid = qdict_get_int(qdict, "id");
1887     qmp_cpu_add(cpuid, &err);
1888     hmp_handle_error(mon, &err);
1889 }
1890
1891 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1892 {
1893     const char *args = qdict_get_str(qdict, "args");
1894     Error *err = NULL;
1895     QemuOpts *opts;
1896
1897     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
1898     if (opts == NULL) {
1899         error_setg(&err, "Parsing chardev args failed");
1900     } else {
1901         qemu_chr_new_from_opts(opts, NULL, &err);
1902     }
1903     hmp_handle_error(mon, &err);
1904 }
1905
1906 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1907 {
1908     Error *local_err = NULL;
1909
1910     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1911     hmp_handle_error(mon, &local_err);
1912 }
1913
1914 void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1915 {
1916     BlockBackend *blk;
1917     const char* device = qdict_get_str(qdict, "device");
1918     const char* command = qdict_get_str(qdict, "command");
1919     Error *err = NULL;
1920
1921     blk = blk_by_name(device);
1922     if (blk) {
1923         qemuio_command(blk, command);
1924     } else {
1925         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
1926                   "Device '%s' not found", device);
1927     }
1928
1929     hmp_handle_error(mon, &err);
1930 }
1931
1932 void hmp_object_del(Monitor *mon, const QDict *qdict)
1933 {
1934     const char *id = qdict_get_str(qdict, "id");
1935     Error *err = NULL;
1936
1937     qmp_object_del(id, &err);
1938     hmp_handle_error(mon, &err);
1939 }
1940
1941 void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1942 {
1943     Error *err = NULL;
1944     MemdevList *memdev_list = qmp_query_memdev(&err);
1945     MemdevList *m = memdev_list;
1946     StringOutputVisitor *ov;
1947     char *str;
1948     int i = 0;
1949
1950
1951     while (m) {
1952         ov = string_output_visitor_new(false);
1953         visit_type_uint16List(string_output_get_visitor(ov), NULL,
1954                               &m->value->host_nodes, NULL);
1955         monitor_printf(mon, "memory backend: %d\n", i);
1956         monitor_printf(mon, "  size:  %" PRId64 "\n", m->value->size);
1957         monitor_printf(mon, "  merge: %s\n",
1958                        m->value->merge ? "true" : "false");
1959         monitor_printf(mon, "  dump: %s\n",
1960                        m->value->dump ? "true" : "false");
1961         monitor_printf(mon, "  prealloc: %s\n",
1962                        m->value->prealloc ? "true" : "false");
1963         monitor_printf(mon, "  policy: %s\n",
1964                        HostMemPolicy_lookup[m->value->policy]);
1965         str = string_output_get_string(ov);
1966         monitor_printf(mon, "  host nodes: %s\n", str);
1967
1968         g_free(str);
1969         string_output_visitor_cleanup(ov);
1970         m = m->next;
1971         i++;
1972     }
1973
1974     monitor_printf(mon, "\n");
1975
1976     qapi_free_MemdevList(memdev_list);
1977 }
1978
1979 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1980 {
1981     Error *err = NULL;
1982     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1983     MemoryDeviceInfoList *info;
1984     MemoryDeviceInfo *value;
1985     PCDIMMDeviceInfo *di;
1986
1987     for (info = info_list; info; info = info->next) {
1988         value = info->value;
1989
1990         if (value) {
1991             switch (value->type) {
1992             case MEMORY_DEVICE_INFO_KIND_DIMM:
1993                 di = value->u.dimm;
1994
1995                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1996                                MemoryDeviceInfoKind_lookup[value->type],
1997                                di->id ? di->id : "");
1998                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
1999                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
2000                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
2001                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
2002                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
2003                 monitor_printf(mon, "  hotplugged: %s\n",
2004                                di->hotplugged ? "true" : "false");
2005                 monitor_printf(mon, "  hotpluggable: %s\n",
2006                                di->hotpluggable ? "true" : "false");
2007                 break;
2008             default:
2009                 break;
2010             }
2011         }
2012     }
2013
2014     qapi_free_MemoryDeviceInfoList(info_list);
2015 }
2016
2017 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
2018 {
2019     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
2020     IOThreadInfoList *info;
2021
2022     for (info = info_list; info; info = info->next) {
2023         monitor_printf(mon, "%s: thread_id=%" PRId64 "\n",
2024                        info->value->id, info->value->thread_id);
2025     }
2026
2027     qapi_free_IOThreadInfoList(info_list);
2028 }
2029
2030 void hmp_qom_list(Monitor *mon, const QDict *qdict)
2031 {
2032     const char *path = qdict_get_try_str(qdict, "path");
2033     ObjectPropertyInfoList *list;
2034     Error *err = NULL;
2035
2036     if (path == NULL) {
2037         monitor_printf(mon, "/\n");
2038         return;
2039     }
2040
2041     list = qmp_qom_list(path, &err);
2042     if (err == NULL) {
2043         ObjectPropertyInfoList *start = list;
2044         while (list != NULL) {
2045             ObjectPropertyInfo *value = list->value;
2046
2047             monitor_printf(mon, "%s (%s)\n",
2048                            value->name, value->type);
2049             list = list->next;
2050         }
2051         qapi_free_ObjectPropertyInfoList(start);
2052     }
2053     hmp_handle_error(mon, &err);
2054 }
2055
2056 void hmp_qom_set(Monitor *mon, const QDict *qdict)
2057 {
2058     const char *path = qdict_get_str(qdict, "path");
2059     const char *property = qdict_get_str(qdict, "property");
2060     const char *value = qdict_get_str(qdict, "value");
2061     Error *err = NULL;
2062     bool ambiguous = false;
2063     Object *obj;
2064
2065     obj = object_resolve_path(path, &ambiguous);
2066     if (obj == NULL) {
2067         error_set(&err, ERROR_CLASS_DEVICE_NOT_FOUND,
2068                   "Device '%s' not found", path);
2069     } else {
2070         if (ambiguous) {
2071             monitor_printf(mon, "Warning: Path '%s' is ambiguous\n", path);
2072         }
2073         object_property_parse(obj, value, property, &err);
2074     }
2075     hmp_handle_error(mon, &err);
2076 }
2077
2078 void hmp_rocker(Monitor *mon, const QDict *qdict)
2079 {
2080     const char *name = qdict_get_str(qdict, "name");
2081     RockerSwitch *rocker;
2082     Error *err = NULL;
2083
2084     rocker = qmp_query_rocker(name, &err);
2085     if (err != NULL) {
2086         hmp_handle_error(mon, &err);
2087         return;
2088     }
2089
2090     monitor_printf(mon, "name: %s\n", rocker->name);
2091     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
2092     monitor_printf(mon, "ports: %d\n", rocker->ports);
2093
2094     qapi_free_RockerSwitch(rocker);
2095 }
2096
2097 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
2098 {
2099     RockerPortList *list, *port;
2100     const char *name = qdict_get_str(qdict, "name");
2101     Error *err = NULL;
2102
2103     list = qmp_query_rocker_ports(name, &err);
2104     if (err != NULL) {
2105         hmp_handle_error(mon, &err);
2106         return;
2107     }
2108
2109     monitor_printf(mon, "            ena/    speed/ auto\n");
2110     monitor_printf(mon, "      port  link    duplex neg?\n");
2111
2112     for (port = list; port; port = port->next) {
2113         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
2114                        port->value->name,
2115                        port->value->enabled ? port->value->link_up ?
2116                        "up" : "down" : "!ena",
2117                        port->value->speed == 10000 ? "10G" : "??",
2118                        port->value->duplex ? "FD" : "HD",
2119                        port->value->autoneg ? "Yes" : "No");
2120     }
2121
2122     qapi_free_RockerPortList(list);
2123 }
2124
2125 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
2126 {
2127     RockerOfDpaFlowList *list, *info;
2128     const char *name = qdict_get_str(qdict, "name");
2129     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
2130     Error *err = NULL;
2131
2132     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
2133     if (err != NULL) {
2134         hmp_handle_error(mon, &err);
2135         return;
2136     }
2137
2138     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
2139
2140     for (info = list; info; info = info->next) {
2141         RockerOfDpaFlow *flow = info->value;
2142         RockerOfDpaFlowKey *key = flow->key;
2143         RockerOfDpaFlowMask *mask = flow->mask;
2144         RockerOfDpaFlowAction *action = flow->action;
2145
2146         if (flow->hits) {
2147             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
2148                            key->priority, key->tbl_id, flow->hits);
2149         } else {
2150             monitor_printf(mon, "%-4d %-3d     ",
2151                            key->priority, key->tbl_id);
2152         }
2153
2154         if (key->has_in_pport) {
2155             monitor_printf(mon, " pport %d", key->in_pport);
2156             if (mask->has_in_pport) {
2157                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2158             }
2159         }
2160
2161         if (key->has_vlan_id) {
2162             monitor_printf(mon, " vlan %d",
2163                            key->vlan_id & VLAN_VID_MASK);
2164             if (mask->has_vlan_id) {
2165                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2166             }
2167         }
2168
2169         if (key->has_tunnel_id) {
2170             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2171             if (mask->has_tunnel_id) {
2172                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2173             }
2174         }
2175
2176         if (key->has_eth_type) {
2177             switch (key->eth_type) {
2178             case 0x0806:
2179                 monitor_printf(mon, " ARP");
2180                 break;
2181             case 0x0800:
2182                 monitor_printf(mon, " IP");
2183                 break;
2184             case 0x86dd:
2185                 monitor_printf(mon, " IPv6");
2186                 break;
2187             case 0x8809:
2188                 monitor_printf(mon, " LACP");
2189                 break;
2190             case 0x88cc:
2191                 monitor_printf(mon, " LLDP");
2192                 break;
2193             default:
2194                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2195                 break;
2196             }
2197         }
2198
2199         if (key->has_eth_src) {
2200             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2201                 (mask->has_eth_src) &&
2202                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2203                 monitor_printf(mon, " src <any mcast/bcast>");
2204             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2205                 (mask->has_eth_src) &&
2206                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2207                 monitor_printf(mon, " src <any ucast>");
2208             } else {
2209                 monitor_printf(mon, " src %s", key->eth_src);
2210                 if (mask->has_eth_src) {
2211                     monitor_printf(mon, "(%s)", mask->eth_src);
2212                 }
2213             }
2214         }
2215
2216         if (key->has_eth_dst) {
2217             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2218                 (mask->has_eth_dst) &&
2219                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2220                 monitor_printf(mon, " dst <any mcast/bcast>");
2221             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2222                 (mask->has_eth_dst) &&
2223                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2224                 monitor_printf(mon, " dst <any ucast>");
2225             } else {
2226                 monitor_printf(mon, " dst %s", key->eth_dst);
2227                 if (mask->has_eth_dst) {
2228                     monitor_printf(mon, "(%s)", mask->eth_dst);
2229                 }
2230             }
2231         }
2232
2233         if (key->has_ip_proto) {
2234             monitor_printf(mon, " proto %d", key->ip_proto);
2235             if (mask->has_ip_proto) {
2236                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2237             }
2238         }
2239
2240         if (key->has_ip_tos) {
2241             monitor_printf(mon, " TOS %d", key->ip_tos);
2242             if (mask->has_ip_tos) {
2243                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2244             }
2245         }
2246
2247         if (key->has_ip_dst) {
2248             monitor_printf(mon, " dst %s", key->ip_dst);
2249         }
2250
2251         if (action->has_goto_tbl || action->has_group_id ||
2252             action->has_new_vlan_id) {
2253             monitor_printf(mon, " -->");
2254         }
2255
2256         if (action->has_new_vlan_id) {
2257             monitor_printf(mon, " apply new vlan %d",
2258                            ntohs(action->new_vlan_id));
2259         }
2260
2261         if (action->has_group_id) {
2262             monitor_printf(mon, " write group 0x%08x", action->group_id);
2263         }
2264
2265         if (action->has_goto_tbl) {
2266             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2267         }
2268
2269         monitor_printf(mon, "\n");
2270     }
2271
2272     qapi_free_RockerOfDpaFlowList(list);
2273 }
2274
2275 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2276 {
2277     RockerOfDpaGroupList *list, *g;
2278     const char *name = qdict_get_str(qdict, "name");
2279     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2280     Error *err = NULL;
2281     bool set = false;
2282
2283     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2284     if (err != NULL) {
2285         hmp_handle_error(mon, &err);
2286         return;
2287     }
2288
2289     monitor_printf(mon, "id (decode) --> buckets\n");
2290
2291     for (g = list; g; g = g->next) {
2292         RockerOfDpaGroup *group = g->value;
2293
2294         monitor_printf(mon, "0x%08x", group->id);
2295
2296         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2297                                          group->type == 1 ? "L2 rewrite" :
2298                                          group->type == 2 ? "L3 unicast" :
2299                                          group->type == 3 ? "L2 multicast" :
2300                                          group->type == 4 ? "L2 flood" :
2301                                          group->type == 5 ? "L3 interface" :
2302                                          group->type == 6 ? "L3 multicast" :
2303                                          group->type == 7 ? "L3 ECMP" :
2304                                          group->type == 8 ? "L2 overlay" :
2305                                          "unknown");
2306
2307         if (group->has_vlan_id) {
2308             monitor_printf(mon, " vlan %d", group->vlan_id);
2309         }
2310
2311         if (group->has_pport) {
2312             monitor_printf(mon, " pport %d", group->pport);
2313         }
2314
2315         if (group->has_index) {
2316             monitor_printf(mon, " index %d", group->index);
2317         }
2318
2319         monitor_printf(mon, ") -->");
2320
2321         if (group->has_set_vlan_id && group->set_vlan_id) {
2322             set = true;
2323             monitor_printf(mon, " set vlan %d",
2324                            group->set_vlan_id & VLAN_VID_MASK);
2325         }
2326
2327         if (group->has_set_eth_src) {
2328             if (!set) {
2329                 set = true;
2330                 monitor_printf(mon, " set");
2331             }
2332             monitor_printf(mon, " src %s", group->set_eth_src);
2333         }
2334
2335         if (group->has_set_eth_dst) {
2336             if (!set) {
2337                 set = true;
2338                 monitor_printf(mon, " set");
2339             }
2340             monitor_printf(mon, " dst %s", group->set_eth_dst);
2341         }
2342
2343         set = false;
2344
2345         if (group->has_ttl_check && group->ttl_check) {
2346             monitor_printf(mon, " check TTL");
2347         }
2348
2349         if (group->has_group_id && group->group_id) {
2350             monitor_printf(mon, " group id 0x%08x", group->group_id);
2351         }
2352
2353         if (group->has_pop_vlan && group->pop_vlan) {
2354             monitor_printf(mon, " pop vlan");
2355         }
2356
2357         if (group->has_out_pport) {
2358             monitor_printf(mon, " out pport %d", group->out_pport);
2359         }
2360
2361         if (group->has_group_ids) {
2362             struct uint32List *id;
2363
2364             monitor_printf(mon, " groups [");
2365             for (id = group->group_ids; id; id = id->next) {
2366                 monitor_printf(mon, "0x%08x", id->value);
2367                 if (id->next) {
2368                     monitor_printf(mon, ",");
2369                 }
2370             }
2371             monitor_printf(mon, "]");
2372         }
2373
2374         monitor_printf(mon, "\n");
2375     }
2376
2377     qapi_free_RockerOfDpaGroupList(list);
2378 }
This page took 0.149183 seconds and 2 git commands to generate.