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