xref: /openbmc/qemu/monitor/hmp-cmds.c (revision a83e24ba)
1 /*
2  * Human Monitor Interface commands
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 "qemu/osdep.h"
17 #include "monitor/hmp.h"
18 #include "net/net.h"
19 #include "net/eth.h"
20 #include "chardev/char.h"
21 #include "sysemu/block-backend.h"
22 #include "sysemu/runstate.h"
23 #include "qemu/config-file.h"
24 #include "qemu/option.h"
25 #include "qemu/timer.h"
26 #include "qemu/sockets.h"
27 #include "monitor/monitor-internal.h"
28 #include "qapi/error.h"
29 #include "qapi/clone-visitor.h"
30 #include "qapi/opts-visitor.h"
31 #include "qapi/qapi-builtin-visit.h"
32 #include "qapi/qapi-commands-block.h"
33 #include "qapi/qapi-commands-char.h"
34 #include "qapi/qapi-commands-control.h"
35 #include "qapi/qapi-commands-machine.h"
36 #include "qapi/qapi-commands-migration.h"
37 #include "qapi/qapi-commands-misc.h"
38 #include "qapi/qapi-commands-net.h"
39 #include "qapi/qapi-commands-rocker.h"
40 #include "qapi/qapi-commands-run-state.h"
41 #include "qapi/qapi-commands-tpm.h"
42 #include "qapi/qapi-commands-ui.h"
43 #include "qapi/qapi-visit-net.h"
44 #include "qapi/qapi-visit-migration.h"
45 #include "qapi/qmp/qdict.h"
46 #include "qapi/qmp/qerror.h"
47 #include "qapi/string-input-visitor.h"
48 #include "qapi/string-output-visitor.h"
49 #include "qom/object_interfaces.h"
50 #include "ui/console.h"
51 #include "qemu/cutils.h"
52 #include "qemu/error-report.h"
53 #include "exec/ramlist.h"
54 #include "hw/intc/intc.h"
55 #include "hw/rdma/rdma.h"
56 #include "migration/snapshot.h"
57 #include "migration/misc.h"
58 
59 #ifdef CONFIG_SPICE
60 #include <spice/enums.h>
61 #endif
62 
63 void hmp_handle_error(Monitor *mon, Error *err)
64 {
65     if (err) {
66         error_reportf_err(err, "Error: ");
67     }
68 }
69 
70 /*
71  * Produce a strList from a comma separated list.
72  * A NULL or empty input string return NULL.
73  */
74 static strList *strList_from_comma_list(const char *in)
75 {
76     strList *res = NULL;
77     strList **hook = &res;
78 
79     while (in && in[0]) {
80         char *comma = strchr(in, ',');
81         *hook = g_new0(strList, 1);
82 
83         if (comma) {
84             (*hook)->value = g_strndup(in, comma - in);
85             in = comma + 1; /* skip the , */
86         } else {
87             (*hook)->value = g_strdup(in);
88             in = NULL;
89         }
90         hook = &(*hook)->next;
91     }
92 
93     return res;
94 }
95 
96 void hmp_info_name(Monitor *mon, const QDict *qdict)
97 {
98     NameInfo *info;
99 
100     info = qmp_query_name(NULL);
101     if (info->has_name) {
102         monitor_printf(mon, "%s\n", info->name);
103     }
104     qapi_free_NameInfo(info);
105 }
106 
107 void hmp_info_version(Monitor *mon, const QDict *qdict)
108 {
109     VersionInfo *info;
110 
111     info = qmp_query_version(NULL);
112 
113     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
114                    info->qemu->major, info->qemu->minor, info->qemu->micro,
115                    info->package);
116 
117     qapi_free_VersionInfo(info);
118 }
119 
120 void hmp_info_kvm(Monitor *mon, const QDict *qdict)
121 {
122     KvmInfo *info;
123 
124     info = qmp_query_kvm(NULL);
125     monitor_printf(mon, "kvm support: ");
126     if (info->present) {
127         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
128     } else {
129         monitor_printf(mon, "not compiled\n");
130     }
131 
132     qapi_free_KvmInfo(info);
133 }
134 
135 void hmp_info_status(Monitor *mon, const QDict *qdict)
136 {
137     StatusInfo *info;
138 
139     info = qmp_query_status(NULL);
140 
141     monitor_printf(mon, "VM status: %s%s",
142                    info->running ? "running" : "paused",
143                    info->singlestep ? " (single step mode)" : "");
144 
145     if (!info->running && info->status != RUN_STATE_PAUSED) {
146         monitor_printf(mon, " (%s)", RunState_str(info->status));
147     }
148 
149     monitor_printf(mon, "\n");
150 
151     qapi_free_StatusInfo(info);
152 }
153 
154 void hmp_info_uuid(Monitor *mon, const QDict *qdict)
155 {
156     UuidInfo *info;
157 
158     info = qmp_query_uuid(NULL);
159     monitor_printf(mon, "%s\n", info->UUID);
160     qapi_free_UuidInfo(info);
161 }
162 
163 void hmp_info_chardev(Monitor *mon, const QDict *qdict)
164 {
165     ChardevInfoList *char_info, *info;
166 
167     char_info = qmp_query_chardev(NULL);
168     for (info = char_info; info; info = info->next) {
169         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
170                                                  info->value->filename);
171     }
172 
173     qapi_free_ChardevInfoList(char_info);
174 }
175 
176 void hmp_info_mice(Monitor *mon, const QDict *qdict)
177 {
178     MouseInfoList *mice_list, *mouse;
179 
180     mice_list = qmp_query_mice(NULL);
181     if (!mice_list) {
182         monitor_printf(mon, "No mouse devices connected\n");
183         return;
184     }
185 
186     for (mouse = mice_list; mouse; mouse = mouse->next) {
187         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
188                        mouse->value->current ? '*' : ' ',
189                        mouse->value->index, mouse->value->name,
190                        mouse->value->absolute ? " (absolute)" : "");
191     }
192 
193     qapi_free_MouseInfoList(mice_list);
194 }
195 
196 static char *SocketAddress_to_str(SocketAddress *addr)
197 {
198     switch (addr->type) {
199     case SOCKET_ADDRESS_TYPE_INET:
200         return g_strdup_printf("tcp:%s:%s",
201                                addr->u.inet.host,
202                                addr->u.inet.port);
203     case SOCKET_ADDRESS_TYPE_UNIX:
204         return g_strdup_printf("unix:%s",
205                                addr->u.q_unix.path);
206     case SOCKET_ADDRESS_TYPE_FD:
207         return g_strdup_printf("fd:%s", addr->u.fd.str);
208     case SOCKET_ADDRESS_TYPE_VSOCK:
209         return g_strdup_printf("tcp:%s:%s",
210                                addr->u.vsock.cid,
211                                addr->u.vsock.port);
212     default:
213         return g_strdup("unknown address type");
214     }
215 }
216 
217 void hmp_info_migrate(Monitor *mon, const QDict *qdict)
218 {
219     MigrationInfo *info;
220 
221     info = qmp_query_migrate(NULL);
222 
223     migration_global_dump(mon);
224 
225     if (info->has_status) {
226         monitor_printf(mon, "Migration status: %s",
227                        MigrationStatus_str(info->status));
228         if (info->status == MIGRATION_STATUS_FAILED &&
229             info->has_error_desc) {
230             monitor_printf(mon, " (%s)\n", info->error_desc);
231         } else {
232             monitor_printf(mon, "\n");
233         }
234 
235         monitor_printf(mon, "total time: %" PRIu64 " ms\n",
236                        info->total_time);
237         if (info->has_expected_downtime) {
238             monitor_printf(mon, "expected downtime: %" PRIu64 " ms\n",
239                            info->expected_downtime);
240         }
241         if (info->has_downtime) {
242             monitor_printf(mon, "downtime: %" PRIu64 " ms\n",
243                            info->downtime);
244         }
245         if (info->has_setup_time) {
246             monitor_printf(mon, "setup: %" PRIu64 " ms\n",
247                            info->setup_time);
248         }
249     }
250 
251     if (info->has_ram) {
252         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
253                        info->ram->transferred >> 10);
254         monitor_printf(mon, "throughput: %0.2f mbps\n",
255                        info->ram->mbps);
256         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
257                        info->ram->remaining >> 10);
258         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
259                        info->ram->total >> 10);
260         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
261                        info->ram->duplicate);
262         monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
263                        info->ram->skipped);
264         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
265                        info->ram->normal);
266         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
267                        info->ram->normal_bytes >> 10);
268         monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
269                        info->ram->dirty_sync_count);
270         monitor_printf(mon, "page size: %" PRIu64 " kbytes\n",
271                        info->ram->page_size >> 10);
272         monitor_printf(mon, "multifd bytes: %" PRIu64 " kbytes\n",
273                        info->ram->multifd_bytes >> 10);
274         monitor_printf(mon, "pages-per-second: %" PRIu64 "\n",
275                        info->ram->pages_per_second);
276 
277         if (info->ram->dirty_pages_rate) {
278             monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
279                            info->ram->dirty_pages_rate);
280         }
281         if (info->ram->postcopy_requests) {
282             monitor_printf(mon, "postcopy request count: %" PRIu64 "\n",
283                            info->ram->postcopy_requests);
284         }
285     }
286 
287     if (info->has_disk) {
288         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
289                        info->disk->transferred >> 10);
290         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
291                        info->disk->remaining >> 10);
292         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
293                        info->disk->total >> 10);
294     }
295 
296     if (info->has_xbzrle_cache) {
297         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
298                        info->xbzrle_cache->cache_size);
299         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
300                        info->xbzrle_cache->bytes >> 10);
301         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
302                        info->xbzrle_cache->pages);
303         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 " pages\n",
304                        info->xbzrle_cache->cache_miss);
305         monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
306                        info->xbzrle_cache->cache_miss_rate);
307         monitor_printf(mon, "xbzrle encoding rate: %0.2f\n",
308                        info->xbzrle_cache->encoding_rate);
309         monitor_printf(mon, "xbzrle overflow: %" PRIu64 "\n",
310                        info->xbzrle_cache->overflow);
311     }
312 
313     if (info->has_compression) {
314         monitor_printf(mon, "compression pages: %" PRIu64 " pages\n",
315                        info->compression->pages);
316         monitor_printf(mon, "compression busy: %" PRIu64 "\n",
317                        info->compression->busy);
318         monitor_printf(mon, "compression busy rate: %0.2f\n",
319                        info->compression->busy_rate);
320         monitor_printf(mon, "compressed size: %" PRIu64 " kbytes\n",
321                        info->compression->compressed_size >> 10);
322         monitor_printf(mon, "compression rate: %0.2f\n",
323                        info->compression->compression_rate);
324     }
325 
326     if (info->has_cpu_throttle_percentage) {
327         monitor_printf(mon, "cpu throttle percentage: %" PRIu64 "\n",
328                        info->cpu_throttle_percentage);
329     }
330 
331     if (info->has_postcopy_blocktime) {
332         monitor_printf(mon, "postcopy blocktime: %u\n",
333                        info->postcopy_blocktime);
334     }
335 
336     if (info->has_postcopy_vcpu_blocktime) {
337         Visitor *v;
338         char *str;
339         v = string_output_visitor_new(false, &str);
340         visit_type_uint32List(v, NULL, &info->postcopy_vcpu_blocktime,
341                               &error_abort);
342         visit_complete(v, &str);
343         monitor_printf(mon, "postcopy vcpu blocktime: %s\n", str);
344         g_free(str);
345         visit_free(v);
346     }
347     if (info->has_socket_address) {
348         SocketAddressList *addr;
349 
350         monitor_printf(mon, "socket address: [\n");
351 
352         for (addr = info->socket_address; addr; addr = addr->next) {
353             char *s = SocketAddress_to_str(addr->value);
354             monitor_printf(mon, "\t%s\n", s);
355             g_free(s);
356         }
357         monitor_printf(mon, "]\n");
358     }
359     qapi_free_MigrationInfo(info);
360 }
361 
362 void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
363 {
364     MigrationCapabilityStatusList *caps, *cap;
365 
366     caps = qmp_query_migrate_capabilities(NULL);
367 
368     if (caps) {
369         for (cap = caps; cap; cap = cap->next) {
370             monitor_printf(mon, "%s: %s\n",
371                            MigrationCapability_str(cap->value->capability),
372                            cap->value->state ? "on" : "off");
373         }
374     }
375 
376     qapi_free_MigrationCapabilityStatusList(caps);
377 }
378 
379 void hmp_info_migrate_parameters(Monitor *mon, const QDict *qdict)
380 {
381     MigrationParameters *params;
382 
383     params = qmp_query_migrate_parameters(NULL);
384 
385     if (params) {
386         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
387             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_INITIAL),
388             params->announce_initial);
389         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
390             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_MAX),
391             params->announce_max);
392         monitor_printf(mon, "%s: %" PRIu64 "\n",
393             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_ROUNDS),
394             params->announce_rounds);
395         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
396             MigrationParameter_str(MIGRATION_PARAMETER_ANNOUNCE_STEP),
397             params->announce_step);
398         assert(params->has_compress_level);
399         monitor_printf(mon, "%s: %u\n",
400             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_LEVEL),
401             params->compress_level);
402         assert(params->has_compress_threads);
403         monitor_printf(mon, "%s: %u\n",
404             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_THREADS),
405             params->compress_threads);
406         assert(params->has_compress_wait_thread);
407         monitor_printf(mon, "%s: %s\n",
408             MigrationParameter_str(MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD),
409             params->compress_wait_thread ? "on" : "off");
410         assert(params->has_decompress_threads);
411         monitor_printf(mon, "%s: %u\n",
412             MigrationParameter_str(MIGRATION_PARAMETER_DECOMPRESS_THREADS),
413             params->decompress_threads);
414         assert(params->has_throttle_trigger_threshold);
415         monitor_printf(mon, "%s: %u\n",
416             MigrationParameter_str(MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD),
417             params->throttle_trigger_threshold);
418         assert(params->has_cpu_throttle_initial);
419         monitor_printf(mon, "%s: %u\n",
420             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL),
421             params->cpu_throttle_initial);
422         assert(params->has_cpu_throttle_increment);
423         monitor_printf(mon, "%s: %u\n",
424             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT),
425             params->cpu_throttle_increment);
426         assert(params->has_cpu_throttle_tailslow);
427         monitor_printf(mon, "%s: %s\n",
428             MigrationParameter_str(MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW),
429             params->cpu_throttle_tailslow ? "on" : "off");
430         assert(params->has_max_cpu_throttle);
431         monitor_printf(mon, "%s: %u\n",
432             MigrationParameter_str(MIGRATION_PARAMETER_MAX_CPU_THROTTLE),
433             params->max_cpu_throttle);
434         assert(params->has_tls_creds);
435         monitor_printf(mon, "%s: '%s'\n",
436             MigrationParameter_str(MIGRATION_PARAMETER_TLS_CREDS),
437             params->tls_creds);
438         assert(params->has_tls_hostname);
439         monitor_printf(mon, "%s: '%s'\n",
440             MigrationParameter_str(MIGRATION_PARAMETER_TLS_HOSTNAME),
441             params->tls_hostname);
442         assert(params->has_max_bandwidth);
443         monitor_printf(mon, "%s: %" PRIu64 " bytes/second\n",
444             MigrationParameter_str(MIGRATION_PARAMETER_MAX_BANDWIDTH),
445             params->max_bandwidth);
446         assert(params->has_downtime_limit);
447         monitor_printf(mon, "%s: %" PRIu64 " ms\n",
448             MigrationParameter_str(MIGRATION_PARAMETER_DOWNTIME_LIMIT),
449             params->downtime_limit);
450         assert(params->has_x_checkpoint_delay);
451         monitor_printf(mon, "%s: %u ms\n",
452             MigrationParameter_str(MIGRATION_PARAMETER_X_CHECKPOINT_DELAY),
453             params->x_checkpoint_delay);
454         assert(params->has_block_incremental);
455         monitor_printf(mon, "%s: %s\n",
456             MigrationParameter_str(MIGRATION_PARAMETER_BLOCK_INCREMENTAL),
457             params->block_incremental ? "on" : "off");
458         monitor_printf(mon, "%s: %u\n",
459             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_CHANNELS),
460             params->multifd_channels);
461         monitor_printf(mon, "%s: %s\n",
462             MigrationParameter_str(MIGRATION_PARAMETER_MULTIFD_COMPRESSION),
463             MultiFDCompression_str(params->multifd_compression));
464         monitor_printf(mon, "%s: %" PRIu64 " bytes\n",
465             MigrationParameter_str(MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE),
466             params->xbzrle_cache_size);
467         monitor_printf(mon, "%s: %" PRIu64 "\n",
468             MigrationParameter_str(MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH),
469             params->max_postcopy_bandwidth);
470         monitor_printf(mon, "%s: '%s'\n",
471             MigrationParameter_str(MIGRATION_PARAMETER_TLS_AUTHZ),
472             params->tls_authz);
473 
474         if (params->has_block_bitmap_mapping) {
475             const BitmapMigrationNodeAliasList *bmnal;
476 
477             monitor_printf(mon, "%s:\n",
478                            MigrationParameter_str(
479                                MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING));
480 
481             for (bmnal = params->block_bitmap_mapping;
482                  bmnal;
483                  bmnal = bmnal->next)
484             {
485                 const BitmapMigrationNodeAlias *bmna = bmnal->value;
486                 const BitmapMigrationBitmapAliasList *bmbal;
487 
488                 monitor_printf(mon, "  '%s' -> '%s'\n",
489                                bmna->node_name, bmna->alias);
490 
491                 for (bmbal = bmna->bitmaps; bmbal; bmbal = bmbal->next) {
492                     const BitmapMigrationBitmapAlias *bmba = bmbal->value;
493 
494                     monitor_printf(mon, "    '%s' -> '%s'\n",
495                                    bmba->name, bmba->alias);
496                 }
497             }
498         }
499     }
500 
501     qapi_free_MigrationParameters(params);
502 }
503 
504 void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
505 {
506     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
507                    qmp_query_migrate_cache_size(NULL) >> 10);
508 }
509 
510 
511 #ifdef CONFIG_VNC
512 /* Helper for hmp_info_vnc_clients, _servers */
513 static void hmp_info_VncBasicInfo(Monitor *mon, VncBasicInfo *info,
514                                   const char *name)
515 {
516     monitor_printf(mon, "  %s: %s:%s (%s%s)\n",
517                    name,
518                    info->host,
519                    info->service,
520                    NetworkAddressFamily_str(info->family),
521                    info->websocket ? " (Websocket)" : "");
522 }
523 
524 /* Helper displaying and auth and crypt info */
525 static void hmp_info_vnc_authcrypt(Monitor *mon, const char *indent,
526                                    VncPrimaryAuth auth,
527                                    VncVencryptSubAuth *vencrypt)
528 {
529     monitor_printf(mon, "%sAuth: %s (Sub: %s)\n", indent,
530                    VncPrimaryAuth_str(auth),
531                    vencrypt ? VncVencryptSubAuth_str(*vencrypt) : "none");
532 }
533 
534 static void hmp_info_vnc_clients(Monitor *mon, VncClientInfoList *client)
535 {
536     while (client) {
537         VncClientInfo *cinfo = client->value;
538 
539         hmp_info_VncBasicInfo(mon, qapi_VncClientInfo_base(cinfo), "Client");
540         monitor_printf(mon, "    x509_dname: %s\n",
541                        cinfo->has_x509_dname ?
542                        cinfo->x509_dname : "none");
543         monitor_printf(mon, "    sasl_username: %s\n",
544                        cinfo->has_sasl_username ?
545                        cinfo->sasl_username : "none");
546 
547         client = client->next;
548     }
549 }
550 
551 static void hmp_info_vnc_servers(Monitor *mon, VncServerInfo2List *server)
552 {
553     while (server) {
554         VncServerInfo2 *sinfo = server->value;
555         hmp_info_VncBasicInfo(mon, qapi_VncServerInfo2_base(sinfo), "Server");
556         hmp_info_vnc_authcrypt(mon, "    ", sinfo->auth,
557                                sinfo->has_vencrypt ? &sinfo->vencrypt : NULL);
558         server = server->next;
559     }
560 }
561 
562 void hmp_info_vnc(Monitor *mon, const QDict *qdict)
563 {
564     VncInfo2List *info2l, *info2l_head;
565     Error *err = NULL;
566 
567     info2l = qmp_query_vnc_servers(&err);
568     info2l_head = info2l;
569     if (err) {
570         hmp_handle_error(mon, err);
571         return;
572     }
573     if (!info2l) {
574         monitor_printf(mon, "None\n");
575         return;
576     }
577 
578     while (info2l) {
579         VncInfo2 *info = info2l->value;
580         monitor_printf(mon, "%s:\n", info->id);
581         hmp_info_vnc_servers(mon, info->server);
582         hmp_info_vnc_clients(mon, info->clients);
583         if (!info->server) {
584             /* The server entry displays its auth, we only
585              * need to display in the case of 'reverse' connections
586              * where there's no server.
587              */
588             hmp_info_vnc_authcrypt(mon, "  ", info->auth,
589                                info->has_vencrypt ? &info->vencrypt : NULL);
590         }
591         if (info->has_display) {
592             monitor_printf(mon, "  Display: %s\n", info->display);
593         }
594         info2l = info2l->next;
595     }
596 
597     qapi_free_VncInfo2List(info2l_head);
598 
599 }
600 #endif
601 
602 #ifdef CONFIG_SPICE
603 void hmp_info_spice(Monitor *mon, const QDict *qdict)
604 {
605     SpiceChannelList *chan;
606     SpiceInfo *info;
607     const char *channel_name;
608     const char * const channel_names[] = {
609         [SPICE_CHANNEL_MAIN] = "main",
610         [SPICE_CHANNEL_DISPLAY] = "display",
611         [SPICE_CHANNEL_INPUTS] = "inputs",
612         [SPICE_CHANNEL_CURSOR] = "cursor",
613         [SPICE_CHANNEL_PLAYBACK] = "playback",
614         [SPICE_CHANNEL_RECORD] = "record",
615         [SPICE_CHANNEL_TUNNEL] = "tunnel",
616         [SPICE_CHANNEL_SMARTCARD] = "smartcard",
617         [SPICE_CHANNEL_USBREDIR] = "usbredir",
618         [SPICE_CHANNEL_PORT] = "port",
619 #if 0
620         /* minimum spice-protocol is 0.12.3, webdav was added in 0.12.7,
621          * no easy way to #ifdef (SPICE_CHANNEL_* is a enum).  Disable
622          * as quick fix for build failures with older versions. */
623         [SPICE_CHANNEL_WEBDAV] = "webdav",
624 #endif
625     };
626 
627     info = qmp_query_spice(NULL);
628 
629     if (!info->enabled) {
630         monitor_printf(mon, "Server: disabled\n");
631         goto out;
632     }
633 
634     monitor_printf(mon, "Server:\n");
635     if (info->has_port) {
636         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
637                        info->host, info->port);
638     }
639     if (info->has_tls_port) {
640         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
641                        info->host, info->tls_port);
642     }
643     monitor_printf(mon, "    migrated: %s\n",
644                    info->migrated ? "true" : "false");
645     monitor_printf(mon, "        auth: %s\n", info->auth);
646     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
647     monitor_printf(mon, "  mouse-mode: %s\n",
648                    SpiceQueryMouseMode_str(info->mouse_mode));
649 
650     if (!info->has_channels || info->channels == NULL) {
651         monitor_printf(mon, "Channels: none\n");
652     } else {
653         for (chan = info->channels; chan; chan = chan->next) {
654             monitor_printf(mon, "Channel:\n");
655             monitor_printf(mon, "     address: %s:%s%s\n",
656                            chan->value->host, chan->value->port,
657                            chan->value->tls ? " [tls]" : "");
658             monitor_printf(mon, "     session: %" PRId64 "\n",
659                            chan->value->connection_id);
660             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
661                            chan->value->channel_type, chan->value->channel_id);
662 
663             channel_name = "unknown";
664             if (chan->value->channel_type > 0 &&
665                 chan->value->channel_type < ARRAY_SIZE(channel_names) &&
666                 channel_names[chan->value->channel_type]) {
667                 channel_name = channel_names[chan->value->channel_type];
668             }
669 
670             monitor_printf(mon, "     channel name: %s\n", channel_name);
671         }
672     }
673 
674 out:
675     qapi_free_SpiceInfo(info);
676 }
677 #endif
678 
679 void hmp_info_balloon(Monitor *mon, const QDict *qdict)
680 {
681     BalloonInfo *info;
682     Error *err = NULL;
683 
684     info = qmp_query_balloon(&err);
685     if (err) {
686         hmp_handle_error(mon, err);
687         return;
688     }
689 
690     monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
691 
692     qapi_free_BalloonInfo(info);
693 }
694 
695 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
696 {
697     PciMemoryRegionList *region;
698 
699     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
700     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
701                    dev->slot, dev->function);
702     monitor_printf(mon, "    ");
703 
704     if (dev->class_info->has_desc) {
705         monitor_printf(mon, "%s", dev->class_info->desc);
706     } else {
707         monitor_printf(mon, "Class %04" PRId64, dev->class_info->q_class);
708     }
709 
710     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
711                    dev->id->vendor, dev->id->device);
712     if (dev->id->has_subsystem_vendor && dev->id->has_subsystem) {
713         monitor_printf(mon, "      PCI subsystem %04" PRIx64 ":%04" PRIx64 "\n",
714                        dev->id->subsystem_vendor, dev->id->subsystem);
715     }
716 
717     if (dev->has_irq) {
718         monitor_printf(mon, "      IRQ %" PRId64 ", pin %c\n",
719                        dev->irq, (char)('A' + dev->irq_pin - 1));
720     }
721 
722     if (dev->has_pci_bridge) {
723         monitor_printf(mon, "      BUS %" PRId64 ".\n",
724                        dev->pci_bridge->bus->number);
725         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
726                        dev->pci_bridge->bus->secondary);
727         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
728                        dev->pci_bridge->bus->subordinate);
729 
730         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
731                        dev->pci_bridge->bus->io_range->base,
732                        dev->pci_bridge->bus->io_range->limit);
733 
734         monitor_printf(mon,
735                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
736                        dev->pci_bridge->bus->memory_range->base,
737                        dev->pci_bridge->bus->memory_range->limit);
738 
739         monitor_printf(mon, "      prefetchable memory range "
740                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
741                        dev->pci_bridge->bus->prefetchable_range->base,
742                        dev->pci_bridge->bus->prefetchable_range->limit);
743     }
744 
745     for (region = dev->regions; region; region = region->next) {
746         uint64_t addr, size;
747 
748         addr = region->value->address;
749         size = region->value->size;
750 
751         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
752 
753         if (!strcmp(region->value->type, "io")) {
754             monitor_printf(mon, "I/O at 0x%04" PRIx64
755                                 " [0x%04" PRIx64 "].\n",
756                            addr, addr + size - 1);
757         } else {
758             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
759                                " [0x%08" PRIx64 "].\n",
760                            region->value->mem_type_64 ? 64 : 32,
761                            region->value->prefetch ? " prefetchable" : "",
762                            addr, addr + size - 1);
763         }
764     }
765 
766     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
767 
768     if (dev->has_pci_bridge) {
769         if (dev->pci_bridge->has_devices) {
770             PciDeviceInfoList *cdev;
771             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
772                 hmp_info_pci_device(mon, cdev->value);
773             }
774         }
775     }
776 }
777 
778 static int hmp_info_irq_foreach(Object *obj, void *opaque)
779 {
780     InterruptStatsProvider *intc;
781     InterruptStatsProviderClass *k;
782     Monitor *mon = opaque;
783 
784     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
785         intc = INTERRUPT_STATS_PROVIDER(obj);
786         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
787         uint64_t *irq_counts;
788         unsigned int nb_irqs, i;
789         if (k->get_statistics &&
790             k->get_statistics(intc, &irq_counts, &nb_irqs)) {
791             if (nb_irqs > 0) {
792                 monitor_printf(mon, "IRQ statistics for %s:\n",
793                                object_get_typename(obj));
794                 for (i = 0; i < nb_irqs; i++) {
795                     if (irq_counts[i] > 0) {
796                         monitor_printf(mon, "%2d: %" PRId64 "\n", i,
797                                        irq_counts[i]);
798                     }
799                 }
800             }
801         } else {
802             monitor_printf(mon, "IRQ statistics not available for %s.\n",
803                            object_get_typename(obj));
804         }
805     }
806 
807     return 0;
808 }
809 
810 void hmp_info_irq(Monitor *mon, const QDict *qdict)
811 {
812     object_child_foreach_recursive(object_get_root(),
813                                    hmp_info_irq_foreach, mon);
814 }
815 
816 static int hmp_info_pic_foreach(Object *obj, void *opaque)
817 {
818     InterruptStatsProvider *intc;
819     InterruptStatsProviderClass *k;
820     Monitor *mon = opaque;
821 
822     if (object_dynamic_cast(obj, TYPE_INTERRUPT_STATS_PROVIDER)) {
823         intc = INTERRUPT_STATS_PROVIDER(obj);
824         k = INTERRUPT_STATS_PROVIDER_GET_CLASS(obj);
825         if (k->print_info) {
826             k->print_info(intc, mon);
827         } else {
828             monitor_printf(mon, "Interrupt controller information not available for %s.\n",
829                            object_get_typename(obj));
830         }
831     }
832 
833     return 0;
834 }
835 
836 void hmp_info_pic(Monitor *mon, const QDict *qdict)
837 {
838     object_child_foreach_recursive(object_get_root(),
839                                    hmp_info_pic_foreach, mon);
840 }
841 
842 static int hmp_info_rdma_foreach(Object *obj, void *opaque)
843 {
844     RdmaProvider *rdma;
845     RdmaProviderClass *k;
846     Monitor *mon = opaque;
847 
848     if (object_dynamic_cast(obj, INTERFACE_RDMA_PROVIDER)) {
849         rdma = RDMA_PROVIDER(obj);
850         k = RDMA_PROVIDER_GET_CLASS(obj);
851         if (k->print_statistics) {
852             k->print_statistics(mon, rdma);
853         } else {
854             monitor_printf(mon, "RDMA statistics not available for %s.\n",
855                            object_get_typename(obj));
856         }
857     }
858 
859     return 0;
860 }
861 
862 void hmp_info_rdma(Monitor *mon, const QDict *qdict)
863 {
864     object_child_foreach_recursive(object_get_root(),
865                                    hmp_info_rdma_foreach, mon);
866 }
867 
868 void hmp_info_pci(Monitor *mon, const QDict *qdict)
869 {
870     PciInfoList *info_list, *info;
871     Error *err = NULL;
872 
873     info_list = qmp_query_pci(&err);
874     if (err) {
875         monitor_printf(mon, "PCI devices not supported\n");
876         error_free(err);
877         return;
878     }
879 
880     for (info = info_list; info; info = info->next) {
881         PciDeviceInfoList *dev;
882 
883         for (dev = info->value->devices; dev; dev = dev->next) {
884             hmp_info_pci_device(mon, dev->value);
885         }
886     }
887 
888     qapi_free_PciInfoList(info_list);
889 }
890 
891 void hmp_info_tpm(Monitor *mon, const QDict *qdict)
892 {
893     TPMInfoList *info_list, *info;
894     Error *err = NULL;
895     unsigned int c = 0;
896     TPMPassthroughOptions *tpo;
897     TPMEmulatorOptions *teo;
898 
899     info_list = qmp_query_tpm(&err);
900     if (err) {
901         monitor_printf(mon, "TPM device not supported\n");
902         error_free(err);
903         return;
904     }
905 
906     if (info_list) {
907         monitor_printf(mon, "TPM device:\n");
908     }
909 
910     for (info = info_list; info; info = info->next) {
911         TPMInfo *ti = info->value;
912         monitor_printf(mon, " tpm%d: model=%s\n",
913                        c, TpmModel_str(ti->model));
914 
915         monitor_printf(mon, "  \\ %s: type=%s",
916                        ti->id, TpmTypeOptionsKind_str(ti->options->type));
917 
918         switch (ti->options->type) {
919         case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
920             tpo = ti->options->u.passthrough.data;
921             monitor_printf(mon, "%s%s%s%s",
922                            tpo->has_path ? ",path=" : "",
923                            tpo->has_path ? tpo->path : "",
924                            tpo->has_cancel_path ? ",cancel-path=" : "",
925                            tpo->has_cancel_path ? tpo->cancel_path : "");
926             break;
927         case TPM_TYPE_OPTIONS_KIND_EMULATOR:
928             teo = ti->options->u.emulator.data;
929             monitor_printf(mon, ",chardev=%s", teo->chardev);
930             break;
931         case TPM_TYPE_OPTIONS_KIND__MAX:
932             break;
933         }
934         monitor_printf(mon, "\n");
935         c++;
936     }
937     qapi_free_TPMInfoList(info_list);
938 }
939 
940 void hmp_quit(Monitor *mon, const QDict *qdict)
941 {
942     monitor_suspend(mon);
943     qmp_quit(NULL);
944 }
945 
946 void hmp_stop(Monitor *mon, const QDict *qdict)
947 {
948     qmp_stop(NULL);
949 }
950 
951 void hmp_sync_profile(Monitor *mon, const QDict *qdict)
952 {
953     const char *op = qdict_get_try_str(qdict, "op");
954 
955     if (op == NULL) {
956         bool on = qsp_is_enabled();
957 
958         monitor_printf(mon, "sync-profile is %s\n", on ? "on" : "off");
959         return;
960     }
961     if (!strcmp(op, "on")) {
962         qsp_enable();
963     } else if (!strcmp(op, "off")) {
964         qsp_disable();
965     } else if (!strcmp(op, "reset")) {
966         qsp_reset();
967     } else {
968         Error *err = NULL;
969 
970         error_setg(&err, QERR_INVALID_PARAMETER, op);
971         hmp_handle_error(mon, err);
972     }
973 }
974 
975 void hmp_system_reset(Monitor *mon, const QDict *qdict)
976 {
977     qmp_system_reset(NULL);
978 }
979 
980 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
981 {
982     qmp_system_powerdown(NULL);
983 }
984 
985 void hmp_exit_preconfig(Monitor *mon, const QDict *qdict)
986 {
987     Error *err = NULL;
988 
989     qmp_x_exit_preconfig(&err);
990     hmp_handle_error(mon, err);
991 }
992 
993 void hmp_cpu(Monitor *mon, const QDict *qdict)
994 {
995     int64_t cpu_index;
996 
997     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
998             use it are converted to the QAPI */
999     cpu_index = qdict_get_int(qdict, "index");
1000     if (monitor_set_cpu(cpu_index) < 0) {
1001         monitor_printf(mon, "invalid CPU index\n");
1002     }
1003 }
1004 
1005 void hmp_memsave(Monitor *mon, const QDict *qdict)
1006 {
1007     uint32_t size = qdict_get_int(qdict, "size");
1008     const char *filename = qdict_get_str(qdict, "filename");
1009     uint64_t addr = qdict_get_int(qdict, "val");
1010     Error *err = NULL;
1011     int cpu_index = monitor_get_cpu_index();
1012 
1013     if (cpu_index < 0) {
1014         monitor_printf(mon, "No CPU available\n");
1015         return;
1016     }
1017 
1018     qmp_memsave(addr, size, filename, true, cpu_index, &err);
1019     hmp_handle_error(mon, err);
1020 }
1021 
1022 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
1023 {
1024     uint32_t size = qdict_get_int(qdict, "size");
1025     const char *filename = qdict_get_str(qdict, "filename");
1026     uint64_t addr = qdict_get_int(qdict, "val");
1027     Error *err = NULL;
1028 
1029     qmp_pmemsave(addr, size, filename, &err);
1030     hmp_handle_error(mon, err);
1031 }
1032 
1033 void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
1034 {
1035     const char *chardev = qdict_get_str(qdict, "device");
1036     const char *data = qdict_get_str(qdict, "data");
1037     Error *err = NULL;
1038 
1039     qmp_ringbuf_write(chardev, data, false, 0, &err);
1040 
1041     hmp_handle_error(mon, err);
1042 }
1043 
1044 void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
1045 {
1046     uint32_t size = qdict_get_int(qdict, "size");
1047     const char *chardev = qdict_get_str(qdict, "device");
1048     char *data;
1049     Error *err = NULL;
1050     int i;
1051 
1052     data = qmp_ringbuf_read(chardev, size, false, 0, &err);
1053     if (err) {
1054         hmp_handle_error(mon, err);
1055         return;
1056     }
1057 
1058     for (i = 0; data[i]; i++) {
1059         unsigned char ch = data[i];
1060 
1061         if (ch == '\\') {
1062             monitor_printf(mon, "\\\\");
1063         } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
1064             monitor_printf(mon, "\\u%04X", ch);
1065         } else {
1066             monitor_printf(mon, "%c", ch);
1067         }
1068 
1069     }
1070     monitor_printf(mon, "\n");
1071     g_free(data);
1072 }
1073 
1074 void hmp_cont(Monitor *mon, const QDict *qdict)
1075 {
1076     Error *err = NULL;
1077 
1078     qmp_cont(&err);
1079     hmp_handle_error(mon, err);
1080 }
1081 
1082 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
1083 {
1084     Error *err = NULL;
1085 
1086     qmp_system_wakeup(&err);
1087     hmp_handle_error(mon, err);
1088 }
1089 
1090 void hmp_nmi(Monitor *mon, const QDict *qdict)
1091 {
1092     Error *err = NULL;
1093 
1094     qmp_inject_nmi(&err);
1095     hmp_handle_error(mon, err);
1096 }
1097 
1098 void hmp_set_link(Monitor *mon, const QDict *qdict)
1099 {
1100     const char *name = qdict_get_str(qdict, "name");
1101     bool up = qdict_get_bool(qdict, "up");
1102     Error *err = NULL;
1103 
1104     qmp_set_link(name, up, &err);
1105     hmp_handle_error(mon, err);
1106 }
1107 
1108 void hmp_balloon(Monitor *mon, const QDict *qdict)
1109 {
1110     int64_t value = qdict_get_int(qdict, "value");
1111     Error *err = NULL;
1112 
1113     qmp_balloon(value, &err);
1114     hmp_handle_error(mon, err);
1115 }
1116 
1117 void hmp_loadvm(Monitor *mon, const QDict *qdict)
1118 {
1119     int saved_vm_running  = runstate_is_running();
1120     const char *name = qdict_get_str(qdict, "name");
1121     Error *err = NULL;
1122 
1123     vm_stop(RUN_STATE_RESTORE_VM);
1124 
1125     if (load_snapshot(name, &err) == 0 && saved_vm_running) {
1126         vm_start();
1127     }
1128     hmp_handle_error(mon, err);
1129 }
1130 
1131 void hmp_savevm(Monitor *mon, const QDict *qdict)
1132 {
1133     Error *err = NULL;
1134 
1135     save_snapshot(qdict_get_try_str(qdict, "name"), &err);
1136     hmp_handle_error(mon, err);
1137 }
1138 
1139 void hmp_delvm(Monitor *mon, const QDict *qdict)
1140 {
1141     BlockDriverState *bs;
1142     Error *err = NULL;
1143     const char *name = qdict_get_str(qdict, "name");
1144 
1145     if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
1146         error_prepend(&err,
1147                       "deleting snapshot on device '%s': ",
1148                       bdrv_get_device_name(bs));
1149     }
1150     hmp_handle_error(mon, err);
1151 }
1152 
1153 void hmp_announce_self(Monitor *mon, const QDict *qdict)
1154 {
1155     const char *interfaces_str = qdict_get_try_str(qdict, "interfaces");
1156     const char *id = qdict_get_try_str(qdict, "id");
1157     AnnounceParameters *params = QAPI_CLONE(AnnounceParameters,
1158                                             migrate_announce_params());
1159 
1160     qapi_free_strList(params->interfaces);
1161     params->interfaces = strList_from_comma_list(interfaces_str);
1162     params->has_interfaces = params->interfaces != NULL;
1163     params->id = g_strdup(id);
1164     params->has_id = !!params->id;
1165     qmp_announce_self(params, NULL);
1166     qapi_free_AnnounceParameters(params);
1167 }
1168 
1169 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1170 {
1171     qmp_migrate_cancel(NULL);
1172 }
1173 
1174 void hmp_migrate_continue(Monitor *mon, const QDict *qdict)
1175 {
1176     Error *err = NULL;
1177     const char *state = qdict_get_str(qdict, "state");
1178     int val = qapi_enum_parse(&MigrationStatus_lookup, state, -1, &err);
1179 
1180     if (val >= 0) {
1181         qmp_migrate_continue(val, &err);
1182     }
1183 
1184     hmp_handle_error(mon, err);
1185 }
1186 
1187 void hmp_migrate_incoming(Monitor *mon, const QDict *qdict)
1188 {
1189     Error *err = NULL;
1190     const char *uri = qdict_get_str(qdict, "uri");
1191 
1192     qmp_migrate_incoming(uri, &err);
1193 
1194     hmp_handle_error(mon, err);
1195 }
1196 
1197 void hmp_migrate_recover(Monitor *mon, const QDict *qdict)
1198 {
1199     Error *err = NULL;
1200     const char *uri = qdict_get_str(qdict, "uri");
1201 
1202     qmp_migrate_recover(uri, &err);
1203 
1204     hmp_handle_error(mon, err);
1205 }
1206 
1207 void hmp_migrate_pause(Monitor *mon, const QDict *qdict)
1208 {
1209     Error *err = NULL;
1210 
1211     qmp_migrate_pause(&err);
1212 
1213     hmp_handle_error(mon, err);
1214 }
1215 
1216 /* Kept for backwards compatibility */
1217 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1218 {
1219     Error *err = NULL;
1220 
1221     double value = qdict_get_double(qdict, "value");
1222     qmp_migrate_set_downtime(value, &err);
1223     hmp_handle_error(mon, err);
1224 }
1225 
1226 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1227 {
1228     int64_t value = qdict_get_int(qdict, "value");
1229     Error *err = NULL;
1230 
1231     qmp_migrate_set_cache_size(value, &err);
1232     hmp_handle_error(mon, err);
1233 }
1234 
1235 /* Kept for backwards compatibility */
1236 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1237 {
1238     Error *err = NULL;
1239 
1240     int64_t value = qdict_get_int(qdict, "value");
1241     qmp_migrate_set_speed(value, &err);
1242     hmp_handle_error(mon, err);
1243 }
1244 
1245 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1246 {
1247     const char *cap = qdict_get_str(qdict, "capability");
1248     bool state = qdict_get_bool(qdict, "state");
1249     Error *err = NULL;
1250     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1251     int val;
1252 
1253     val = qapi_enum_parse(&MigrationCapability_lookup, cap, -1, &err);
1254     if (val < 0) {
1255         goto end;
1256     }
1257 
1258     caps->value = g_malloc0(sizeof(*caps->value));
1259     caps->value->capability = val;
1260     caps->value->state = state;
1261     caps->next = NULL;
1262     qmp_migrate_set_capabilities(caps, &err);
1263 
1264 end:
1265     qapi_free_MigrationCapabilityStatusList(caps);
1266     hmp_handle_error(mon, err);
1267 }
1268 
1269 void hmp_migrate_set_parameter(Monitor *mon, const QDict *qdict)
1270 {
1271     const char *param = qdict_get_str(qdict, "parameter");
1272     const char *valuestr = qdict_get_str(qdict, "value");
1273     Visitor *v = string_input_visitor_new(valuestr);
1274     MigrateSetParameters *p = g_new0(MigrateSetParameters, 1);
1275     uint64_t valuebw = 0;
1276     uint64_t cache_size;
1277     Error *err = NULL;
1278     int val, ret;
1279 
1280     val = qapi_enum_parse(&MigrationParameter_lookup, param, -1, &err);
1281     if (val < 0) {
1282         goto cleanup;
1283     }
1284 
1285     switch (val) {
1286     case MIGRATION_PARAMETER_COMPRESS_LEVEL:
1287         p->has_compress_level = true;
1288         visit_type_int(v, param, &p->compress_level, &err);
1289         break;
1290     case MIGRATION_PARAMETER_COMPRESS_THREADS:
1291         p->has_compress_threads = true;
1292         visit_type_int(v, param, &p->compress_threads, &err);
1293         break;
1294     case MIGRATION_PARAMETER_COMPRESS_WAIT_THREAD:
1295         p->has_compress_wait_thread = true;
1296         visit_type_bool(v, param, &p->compress_wait_thread, &err);
1297         break;
1298     case MIGRATION_PARAMETER_DECOMPRESS_THREADS:
1299         p->has_decompress_threads = true;
1300         visit_type_int(v, param, &p->decompress_threads, &err);
1301         break;
1302     case MIGRATION_PARAMETER_THROTTLE_TRIGGER_THRESHOLD:
1303         p->has_throttle_trigger_threshold = true;
1304         visit_type_int(v, param, &p->throttle_trigger_threshold, &err);
1305         break;
1306     case MIGRATION_PARAMETER_CPU_THROTTLE_INITIAL:
1307         p->has_cpu_throttle_initial = true;
1308         visit_type_int(v, param, &p->cpu_throttle_initial, &err);
1309         break;
1310     case MIGRATION_PARAMETER_CPU_THROTTLE_INCREMENT:
1311         p->has_cpu_throttle_increment = true;
1312         visit_type_int(v, param, &p->cpu_throttle_increment, &err);
1313         break;
1314     case MIGRATION_PARAMETER_CPU_THROTTLE_TAILSLOW:
1315         p->has_cpu_throttle_tailslow = true;
1316         visit_type_bool(v, param, &p->cpu_throttle_tailslow, &err);
1317         break;
1318     case MIGRATION_PARAMETER_MAX_CPU_THROTTLE:
1319         p->has_max_cpu_throttle = true;
1320         visit_type_int(v, param, &p->max_cpu_throttle, &err);
1321         break;
1322     case MIGRATION_PARAMETER_TLS_CREDS:
1323         p->has_tls_creds = true;
1324         p->tls_creds = g_new0(StrOrNull, 1);
1325         p->tls_creds->type = QTYPE_QSTRING;
1326         visit_type_str(v, param, &p->tls_creds->u.s, &err);
1327         break;
1328     case MIGRATION_PARAMETER_TLS_HOSTNAME:
1329         p->has_tls_hostname = true;
1330         p->tls_hostname = g_new0(StrOrNull, 1);
1331         p->tls_hostname->type = QTYPE_QSTRING;
1332         visit_type_str(v, param, &p->tls_hostname->u.s, &err);
1333         break;
1334     case MIGRATION_PARAMETER_TLS_AUTHZ:
1335         p->has_tls_authz = true;
1336         p->tls_authz = g_new0(StrOrNull, 1);
1337         p->tls_authz->type = QTYPE_QSTRING;
1338         visit_type_str(v, param, &p->tls_authz->u.s, &err);
1339         break;
1340     case MIGRATION_PARAMETER_MAX_BANDWIDTH:
1341         p->has_max_bandwidth = true;
1342         /*
1343          * Can't use visit_type_size() here, because it
1344          * defaults to Bytes rather than Mebibytes.
1345          */
1346         ret = qemu_strtosz_MiB(valuestr, NULL, &valuebw);
1347         if (ret < 0 || valuebw > INT64_MAX
1348             || (size_t)valuebw != valuebw) {
1349             error_setg(&err, "Invalid size %s", valuestr);
1350             break;
1351         }
1352         p->max_bandwidth = valuebw;
1353         break;
1354     case MIGRATION_PARAMETER_DOWNTIME_LIMIT:
1355         p->has_downtime_limit = true;
1356         visit_type_int(v, param, &p->downtime_limit, &err);
1357         break;
1358     case MIGRATION_PARAMETER_X_CHECKPOINT_DELAY:
1359         p->has_x_checkpoint_delay = true;
1360         visit_type_int(v, param, &p->x_checkpoint_delay, &err);
1361         break;
1362     case MIGRATION_PARAMETER_BLOCK_INCREMENTAL:
1363         p->has_block_incremental = true;
1364         visit_type_bool(v, param, &p->block_incremental, &err);
1365         break;
1366     case MIGRATION_PARAMETER_MULTIFD_CHANNELS:
1367         p->has_multifd_channels = true;
1368         visit_type_int(v, param, &p->multifd_channels, &err);
1369         break;
1370     case MIGRATION_PARAMETER_MULTIFD_COMPRESSION:
1371         p->has_multifd_compression = true;
1372         visit_type_MultiFDCompression(v, param, &p->multifd_compression,
1373                                       &err);
1374         break;
1375     case MIGRATION_PARAMETER_MULTIFD_ZLIB_LEVEL:
1376         p->has_multifd_zlib_level = true;
1377         visit_type_int(v, param, &p->multifd_zlib_level, &err);
1378         break;
1379     case MIGRATION_PARAMETER_MULTIFD_ZSTD_LEVEL:
1380         p->has_multifd_zstd_level = true;
1381         visit_type_int(v, param, &p->multifd_zstd_level, &err);
1382         break;
1383     case MIGRATION_PARAMETER_XBZRLE_CACHE_SIZE:
1384         p->has_xbzrle_cache_size = true;
1385         if (!visit_type_size(v, param, &cache_size, &err)) {
1386             break;
1387         }
1388         if (cache_size > INT64_MAX || (size_t)cache_size != cache_size) {
1389             error_setg(&err, "Invalid size %s", valuestr);
1390             break;
1391         }
1392         p->xbzrle_cache_size = cache_size;
1393         break;
1394     case MIGRATION_PARAMETER_MAX_POSTCOPY_BANDWIDTH:
1395         p->has_max_postcopy_bandwidth = true;
1396         visit_type_size(v, param, &p->max_postcopy_bandwidth, &err);
1397         break;
1398     case MIGRATION_PARAMETER_ANNOUNCE_INITIAL:
1399         p->has_announce_initial = true;
1400         visit_type_size(v, param, &p->announce_initial, &err);
1401         break;
1402     case MIGRATION_PARAMETER_ANNOUNCE_MAX:
1403         p->has_announce_max = true;
1404         visit_type_size(v, param, &p->announce_max, &err);
1405         break;
1406     case MIGRATION_PARAMETER_ANNOUNCE_ROUNDS:
1407         p->has_announce_rounds = true;
1408         visit_type_size(v, param, &p->announce_rounds, &err);
1409         break;
1410     case MIGRATION_PARAMETER_ANNOUNCE_STEP:
1411         p->has_announce_step = true;
1412         visit_type_size(v, param, &p->announce_step, &err);
1413         break;
1414     case MIGRATION_PARAMETER_BLOCK_BITMAP_MAPPING:
1415         error_setg(&err, "The block-bitmap-mapping parameter can only be set "
1416                    "through QMP");
1417         break;
1418     default:
1419         assert(0);
1420     }
1421 
1422     if (err) {
1423         goto cleanup;
1424     }
1425 
1426     qmp_migrate_set_parameters(p, &err);
1427 
1428  cleanup:
1429     qapi_free_MigrateSetParameters(p);
1430     visit_free(v);
1431     hmp_handle_error(mon, err);
1432 }
1433 
1434 void hmp_client_migrate_info(Monitor *mon, const QDict *qdict)
1435 {
1436     Error *err = NULL;
1437     const char *protocol = qdict_get_str(qdict, "protocol");
1438     const char *hostname = qdict_get_str(qdict, "hostname");
1439     bool has_port        = qdict_haskey(qdict, "port");
1440     int port             = qdict_get_try_int(qdict, "port", -1);
1441     bool has_tls_port    = qdict_haskey(qdict, "tls-port");
1442     int tls_port         = qdict_get_try_int(qdict, "tls-port", -1);
1443     const char *cert_subject = qdict_get_try_str(qdict, "cert-subject");
1444 
1445     qmp_client_migrate_info(protocol, hostname,
1446                             has_port, port, has_tls_port, tls_port,
1447                             !!cert_subject, cert_subject, &err);
1448     hmp_handle_error(mon, err);
1449 }
1450 
1451 void hmp_migrate_start_postcopy(Monitor *mon, const QDict *qdict)
1452 {
1453     Error *err = NULL;
1454     qmp_migrate_start_postcopy(&err);
1455     hmp_handle_error(mon, err);
1456 }
1457 
1458 void hmp_x_colo_lost_heartbeat(Monitor *mon, const QDict *qdict)
1459 {
1460     Error *err = NULL;
1461 
1462     qmp_x_colo_lost_heartbeat(&err);
1463     hmp_handle_error(mon, err);
1464 }
1465 
1466 void hmp_set_password(Monitor *mon, const QDict *qdict)
1467 {
1468     const char *protocol  = qdict_get_str(qdict, "protocol");
1469     const char *password  = qdict_get_str(qdict, "password");
1470     const char *connected = qdict_get_try_str(qdict, "connected");
1471     Error *err = NULL;
1472 
1473     qmp_set_password(protocol, password, !!connected, connected, &err);
1474     hmp_handle_error(mon, err);
1475 }
1476 
1477 void hmp_expire_password(Monitor *mon, const QDict *qdict)
1478 {
1479     const char *protocol  = qdict_get_str(qdict, "protocol");
1480     const char *whenstr = qdict_get_str(qdict, "time");
1481     Error *err = NULL;
1482 
1483     qmp_expire_password(protocol, whenstr, &err);
1484     hmp_handle_error(mon, err);
1485 }
1486 
1487 
1488 #ifdef CONFIG_VNC
1489 static void hmp_change_read_arg(void *opaque, const char *password,
1490                                 void *readline_opaque)
1491 {
1492     qmp_change_vnc_password(password, NULL);
1493     monitor_read_command(opaque, 1);
1494 }
1495 #endif
1496 
1497 void hmp_change(Monitor *mon, const QDict *qdict)
1498 {
1499     const char *device = qdict_get_str(qdict, "device");
1500     const char *target = qdict_get_str(qdict, "target");
1501     const char *arg = qdict_get_try_str(qdict, "arg");
1502     const char *read_only = qdict_get_try_str(qdict, "read-only-mode");
1503     BlockdevChangeReadOnlyMode read_only_mode = 0;
1504     Error *err = NULL;
1505 
1506 #ifdef CONFIG_VNC
1507     if (strcmp(device, "vnc") == 0) {
1508         if (read_only) {
1509             monitor_printf(mon,
1510                            "Parameter 'read-only-mode' is invalid for VNC\n");
1511             return;
1512         }
1513         if (strcmp(target, "passwd") == 0 ||
1514             strcmp(target, "password") == 0) {
1515             if (!arg) {
1516                 MonitorHMP *hmp_mon = container_of(mon, MonitorHMP, common);
1517                 monitor_read_password(hmp_mon, hmp_change_read_arg, NULL);
1518                 return;
1519             }
1520         }
1521         qmp_change("vnc", target, !!arg, arg, &err);
1522     } else
1523 #endif
1524     {
1525         if (read_only) {
1526             read_only_mode =
1527                 qapi_enum_parse(&BlockdevChangeReadOnlyMode_lookup,
1528                                 read_only,
1529                                 BLOCKDEV_CHANGE_READ_ONLY_MODE_RETAIN, &err);
1530             if (err) {
1531                 goto end;
1532             }
1533         }
1534 
1535         qmp_blockdev_change_medium(true, device, false, NULL, target,
1536                                    !!arg, arg, !!read_only, read_only_mode,
1537                                    &err);
1538     }
1539 
1540 end:
1541     hmp_handle_error(mon, err);
1542 }
1543 
1544 typedef struct HMPMigrationStatus
1545 {
1546     QEMUTimer *timer;
1547     Monitor *mon;
1548     bool is_block_migration;
1549 } HMPMigrationStatus;
1550 
1551 static void hmp_migrate_status_cb(void *opaque)
1552 {
1553     HMPMigrationStatus *status = opaque;
1554     MigrationInfo *info;
1555 
1556     info = qmp_query_migrate(NULL);
1557     if (!info->has_status || info->status == MIGRATION_STATUS_ACTIVE ||
1558         info->status == MIGRATION_STATUS_SETUP) {
1559         if (info->has_disk) {
1560             int progress;
1561 
1562             if (info->disk->remaining) {
1563                 progress = info->disk->transferred * 100 / info->disk->total;
1564             } else {
1565                 progress = 100;
1566             }
1567 
1568             monitor_printf(status->mon, "Completed %d %%\r", progress);
1569             monitor_flush(status->mon);
1570         }
1571 
1572         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
1573     } else {
1574         if (status->is_block_migration) {
1575             monitor_printf(status->mon, "\n");
1576         }
1577         if (info->has_error_desc) {
1578             error_report("%s", info->error_desc);
1579         }
1580         monitor_resume(status->mon);
1581         timer_del(status->timer);
1582         timer_free(status->timer);
1583         g_free(status);
1584     }
1585 
1586     qapi_free_MigrationInfo(info);
1587 }
1588 
1589 void hmp_migrate(Monitor *mon, const QDict *qdict)
1590 {
1591     bool detach = qdict_get_try_bool(qdict, "detach", false);
1592     bool blk = qdict_get_try_bool(qdict, "blk", false);
1593     bool inc = qdict_get_try_bool(qdict, "inc", false);
1594     bool resume = qdict_get_try_bool(qdict, "resume", false);
1595     const char *uri = qdict_get_str(qdict, "uri");
1596     Error *err = NULL;
1597 
1598     qmp_migrate(uri, !!blk, blk, !!inc, inc,
1599                 false, false, true, resume, &err);
1600     if (err) {
1601         hmp_handle_error(mon, err);
1602         return;
1603     }
1604 
1605     if (!detach) {
1606         HMPMigrationStatus *status;
1607 
1608         if (monitor_suspend(mon) < 0) {
1609             monitor_printf(mon, "terminal does not allow synchronous "
1610                            "migration, continuing detached\n");
1611             return;
1612         }
1613 
1614         status = g_malloc0(sizeof(*status));
1615         status->mon = mon;
1616         status->is_block_migration = blk || inc;
1617         status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
1618                                           status);
1619         timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
1620     }
1621 }
1622 
1623 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1624 {
1625     Error *err = NULL;
1626     QemuOpts *opts;
1627 
1628     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1629     if (err) {
1630         goto out;
1631     }
1632 
1633     netdev_add(opts, &err);
1634     if (err) {
1635         qemu_opts_del(opts);
1636     }
1637 
1638 out:
1639     hmp_handle_error(mon, err);
1640 }
1641 
1642 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1643 {
1644     const char *id = qdict_get_str(qdict, "id");
1645     Error *err = NULL;
1646 
1647     qmp_netdev_del(id, &err);
1648     hmp_handle_error(mon, err);
1649 }
1650 
1651 void hmp_object_add(Monitor *mon, const QDict *qdict)
1652 {
1653     Error *err = NULL;
1654     QemuOpts *opts;
1655     Object *obj = NULL;
1656 
1657     opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1658     if (err) {
1659         goto end;
1660     }
1661 
1662     obj = user_creatable_add_opts(opts, &err);
1663     qemu_opts_del(opts);
1664 
1665 end:
1666     hmp_handle_error(mon, err);
1667 
1668     if (obj) {
1669         object_unref(obj);
1670     }
1671 }
1672 
1673 void hmp_getfd(Monitor *mon, const QDict *qdict)
1674 {
1675     const char *fdname = qdict_get_str(qdict, "fdname");
1676     Error *err = NULL;
1677 
1678     qmp_getfd(fdname, &err);
1679     hmp_handle_error(mon, err);
1680 }
1681 
1682 void hmp_closefd(Monitor *mon, const QDict *qdict)
1683 {
1684     const char *fdname = qdict_get_str(qdict, "fdname");
1685     Error *err = NULL;
1686 
1687     qmp_closefd(fdname, &err);
1688     hmp_handle_error(mon, err);
1689 }
1690 
1691 void hmp_sendkey(Monitor *mon, const QDict *qdict)
1692 {
1693     const char *keys = qdict_get_str(qdict, "keys");
1694     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1695     int has_hold_time = qdict_haskey(qdict, "hold-time");
1696     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1697     Error *err = NULL;
1698     const char *separator;
1699     int keyname_len;
1700 
1701     while (1) {
1702         separator = qemu_strchrnul(keys, '-');
1703         keyname_len = separator - keys;
1704 
1705         /* Be compatible with old interface, convert user inputted "<" */
1706         if (keys[0] == '<' && keyname_len == 1) {
1707             keys = "less";
1708             keyname_len = 4;
1709         }
1710 
1711         keylist = g_malloc0(sizeof(*keylist));
1712         keylist->value = g_malloc0(sizeof(*keylist->value));
1713 
1714         if (!head) {
1715             head = keylist;
1716         }
1717         if (tmp) {
1718             tmp->next = keylist;
1719         }
1720         tmp = keylist;
1721 
1722         if (strstart(keys, "0x", NULL)) {
1723             char *endp;
1724             int value = strtoul(keys, &endp, 0);
1725             assert(endp <= keys + keyname_len);
1726             if (endp != keys + keyname_len) {
1727                 goto err_out;
1728             }
1729             keylist->value->type = KEY_VALUE_KIND_NUMBER;
1730             keylist->value->u.number.data = value;
1731         } else {
1732             int idx = index_from_key(keys, keyname_len);
1733             if (idx == Q_KEY_CODE__MAX) {
1734                 goto err_out;
1735             }
1736             keylist->value->type = KEY_VALUE_KIND_QCODE;
1737             keylist->value->u.qcode.data = idx;
1738         }
1739 
1740         if (!*separator) {
1741             break;
1742         }
1743         keys = separator + 1;
1744     }
1745 
1746     qmp_send_key(head, has_hold_time, hold_time, &err);
1747     hmp_handle_error(mon, err);
1748 
1749 out:
1750     qapi_free_KeyValueList(head);
1751     return;
1752 
1753 err_out:
1754     monitor_printf(mon, "invalid parameter: %.*s\n", keyname_len, keys);
1755     goto out;
1756 }
1757 
1758 void hmp_screendump(Monitor *mon, const QDict *qdict)
1759 {
1760     const char *filename = qdict_get_str(qdict, "filename");
1761     const char *id = qdict_get_try_str(qdict, "device");
1762     int64_t head = qdict_get_try_int(qdict, "head", 0);
1763     Error *err = NULL;
1764 
1765     qmp_screendump(filename, id != NULL, id, id != NULL, head, &err);
1766     hmp_handle_error(mon, err);
1767 }
1768 
1769 void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1770 {
1771     const char *args = qdict_get_str(qdict, "args");
1772     Error *err = NULL;
1773     QemuOpts *opts;
1774 
1775     opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args, true);
1776     if (opts == NULL) {
1777         error_setg(&err, "Parsing chardev args failed");
1778     } else {
1779         qemu_chr_new_from_opts(opts, NULL, &err);
1780         qemu_opts_del(opts);
1781     }
1782     hmp_handle_error(mon, err);
1783 }
1784 
1785 void hmp_chardev_change(Monitor *mon, const QDict *qdict)
1786 {
1787     const char *args = qdict_get_str(qdict, "args");
1788     const char *id;
1789     Error *err = NULL;
1790     ChardevBackend *backend = NULL;
1791     ChardevReturn *ret = NULL;
1792     QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("chardev"), args,
1793                                              true);
1794     if (!opts) {
1795         error_setg(&err, "Parsing chardev args failed");
1796         goto end;
1797     }
1798 
1799     id = qdict_get_str(qdict, "id");
1800     if (qemu_opts_id(opts)) {
1801         error_setg(&err, "Unexpected 'id' parameter");
1802         goto end;
1803     }
1804 
1805     backend = qemu_chr_parse_opts(opts, &err);
1806     if (!backend) {
1807         goto end;
1808     }
1809 
1810     ret = qmp_chardev_change(id, backend, &err);
1811 
1812 end:
1813     qapi_free_ChardevReturn(ret);
1814     qapi_free_ChardevBackend(backend);
1815     qemu_opts_del(opts);
1816     hmp_handle_error(mon, err);
1817 }
1818 
1819 void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1820 {
1821     Error *local_err = NULL;
1822 
1823     qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1824     hmp_handle_error(mon, local_err);
1825 }
1826 
1827 void hmp_chardev_send_break(Monitor *mon, const QDict *qdict)
1828 {
1829     Error *local_err = NULL;
1830 
1831     qmp_chardev_send_break(qdict_get_str(qdict, "id"), &local_err);
1832     hmp_handle_error(mon, local_err);
1833 }
1834 
1835 void hmp_object_del(Monitor *mon, const QDict *qdict)
1836 {
1837     const char *id = qdict_get_str(qdict, "id");
1838     Error *err = NULL;
1839 
1840     user_creatable_del(id, &err);
1841     hmp_handle_error(mon, err);
1842 }
1843 
1844 void hmp_info_memory_devices(Monitor *mon, const QDict *qdict)
1845 {
1846     Error *err = NULL;
1847     MemoryDeviceInfoList *info_list = qmp_query_memory_devices(&err);
1848     MemoryDeviceInfoList *info;
1849     VirtioPMEMDeviceInfo *vpi;
1850     VirtioMEMDeviceInfo *vmi;
1851     MemoryDeviceInfo *value;
1852     PCDIMMDeviceInfo *di;
1853 
1854     for (info = info_list; info; info = info->next) {
1855         value = info->value;
1856 
1857         if (value) {
1858             switch (value->type) {
1859             case MEMORY_DEVICE_INFO_KIND_DIMM:
1860             case MEMORY_DEVICE_INFO_KIND_NVDIMM:
1861                 di = value->type == MEMORY_DEVICE_INFO_KIND_DIMM ?
1862                      value->u.dimm.data : value->u.nvdimm.data;
1863                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1864                                MemoryDeviceInfoKind_str(value->type),
1865                                di->id ? di->id : "");
1866                 monitor_printf(mon, "  addr: 0x%" PRIx64 "\n", di->addr);
1867                 monitor_printf(mon, "  slot: %" PRId64 "\n", di->slot);
1868                 monitor_printf(mon, "  node: %" PRId64 "\n", di->node);
1869                 monitor_printf(mon, "  size: %" PRIu64 "\n", di->size);
1870                 monitor_printf(mon, "  memdev: %s\n", di->memdev);
1871                 monitor_printf(mon, "  hotplugged: %s\n",
1872                                di->hotplugged ? "true" : "false");
1873                 monitor_printf(mon, "  hotpluggable: %s\n",
1874                                di->hotpluggable ? "true" : "false");
1875                 break;
1876             case MEMORY_DEVICE_INFO_KIND_VIRTIO_PMEM:
1877                 vpi = value->u.virtio_pmem.data;
1878                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1879                                MemoryDeviceInfoKind_str(value->type),
1880                                vpi->id ? vpi->id : "");
1881                 monitor_printf(mon, "  memaddr: 0x%" PRIx64 "\n", vpi->memaddr);
1882                 monitor_printf(mon, "  size: %" PRIu64 "\n", vpi->size);
1883                 monitor_printf(mon, "  memdev: %s\n", vpi->memdev);
1884                 break;
1885             case MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM:
1886                 vmi = value->u.virtio_mem.data;
1887                 monitor_printf(mon, "Memory device [%s]: \"%s\"\n",
1888                                MemoryDeviceInfoKind_str(value->type),
1889                                vmi->id ? vmi->id : "");
1890                 monitor_printf(mon, "  memaddr: 0x%" PRIx64 "\n", vmi->memaddr);
1891                 monitor_printf(mon, "  node: %" PRId64 "\n", vmi->node);
1892                 monitor_printf(mon, "  requested-size: %" PRIu64 "\n",
1893                                vmi->requested_size);
1894                 monitor_printf(mon, "  size: %" PRIu64 "\n", vmi->size);
1895                 monitor_printf(mon, "  max-size: %" PRIu64 "\n", vmi->max_size);
1896                 monitor_printf(mon, "  block-size: %" PRIu64 "\n",
1897                                vmi->block_size);
1898                 monitor_printf(mon, "  memdev: %s\n", vmi->memdev);
1899                 break;
1900             default:
1901                 g_assert_not_reached();
1902             }
1903         }
1904     }
1905 
1906     qapi_free_MemoryDeviceInfoList(info_list);
1907     hmp_handle_error(mon, err);
1908 }
1909 
1910 void hmp_info_iothreads(Monitor *mon, const QDict *qdict)
1911 {
1912     IOThreadInfoList *info_list = qmp_query_iothreads(NULL);
1913     IOThreadInfoList *info;
1914     IOThreadInfo *value;
1915 
1916     for (info = info_list; info; info = info->next) {
1917         value = info->value;
1918         monitor_printf(mon, "%s:\n", value->id);
1919         monitor_printf(mon, "  thread_id=%" PRId64 "\n", value->thread_id);
1920         monitor_printf(mon, "  poll-max-ns=%" PRId64 "\n", value->poll_max_ns);
1921         monitor_printf(mon, "  poll-grow=%" PRId64 "\n", value->poll_grow);
1922         monitor_printf(mon, "  poll-shrink=%" PRId64 "\n", value->poll_shrink);
1923     }
1924 
1925     qapi_free_IOThreadInfoList(info_list);
1926 }
1927 
1928 void hmp_rocker(Monitor *mon, const QDict *qdict)
1929 {
1930     const char *name = qdict_get_str(qdict, "name");
1931     RockerSwitch *rocker;
1932     Error *err = NULL;
1933 
1934     rocker = qmp_query_rocker(name, &err);
1935     if (err != NULL) {
1936         hmp_handle_error(mon, err);
1937         return;
1938     }
1939 
1940     monitor_printf(mon, "name: %s\n", rocker->name);
1941     monitor_printf(mon, "id: 0x%" PRIx64 "\n", rocker->id);
1942     monitor_printf(mon, "ports: %d\n", rocker->ports);
1943 
1944     qapi_free_RockerSwitch(rocker);
1945 }
1946 
1947 void hmp_rocker_ports(Monitor *mon, const QDict *qdict)
1948 {
1949     RockerPortList *list, *port;
1950     const char *name = qdict_get_str(qdict, "name");
1951     Error *err = NULL;
1952 
1953     list = qmp_query_rocker_ports(name, &err);
1954     if (err != NULL) {
1955         hmp_handle_error(mon, err);
1956         return;
1957     }
1958 
1959     monitor_printf(mon, "            ena/    speed/ auto\n");
1960     monitor_printf(mon, "      port  link    duplex neg?\n");
1961 
1962     for (port = list; port; port = port->next) {
1963         monitor_printf(mon, "%10s  %-4s   %-3s  %2s  %-3s\n",
1964                        port->value->name,
1965                        port->value->enabled ? port->value->link_up ?
1966                        "up" : "down" : "!ena",
1967                        port->value->speed == 10000 ? "10G" : "??",
1968                        port->value->duplex ? "FD" : "HD",
1969                        port->value->autoneg ? "Yes" : "No");
1970     }
1971 
1972     qapi_free_RockerPortList(list);
1973 }
1974 
1975 void hmp_rocker_of_dpa_flows(Monitor *mon, const QDict *qdict)
1976 {
1977     RockerOfDpaFlowList *list, *info;
1978     const char *name = qdict_get_str(qdict, "name");
1979     uint32_t tbl_id = qdict_get_try_int(qdict, "tbl_id", -1);
1980     Error *err = NULL;
1981 
1982     list = qmp_query_rocker_of_dpa_flows(name, tbl_id != -1, tbl_id, &err);
1983     if (err != NULL) {
1984         hmp_handle_error(mon, err);
1985         return;
1986     }
1987 
1988     monitor_printf(mon, "prio tbl hits key(mask) --> actions\n");
1989 
1990     for (info = list; info; info = info->next) {
1991         RockerOfDpaFlow *flow = info->value;
1992         RockerOfDpaFlowKey *key = flow->key;
1993         RockerOfDpaFlowMask *mask = flow->mask;
1994         RockerOfDpaFlowAction *action = flow->action;
1995 
1996         if (flow->hits) {
1997             monitor_printf(mon, "%-4d %-3d %-4" PRIu64,
1998                            key->priority, key->tbl_id, flow->hits);
1999         } else {
2000             monitor_printf(mon, "%-4d %-3d     ",
2001                            key->priority, key->tbl_id);
2002         }
2003 
2004         if (key->has_in_pport) {
2005             monitor_printf(mon, " pport %d", key->in_pport);
2006             if (mask->has_in_pport) {
2007                 monitor_printf(mon, "(0x%x)", mask->in_pport);
2008             }
2009         }
2010 
2011         if (key->has_vlan_id) {
2012             monitor_printf(mon, " vlan %d",
2013                            key->vlan_id & VLAN_VID_MASK);
2014             if (mask->has_vlan_id) {
2015                 monitor_printf(mon, "(0x%x)", mask->vlan_id);
2016             }
2017         }
2018 
2019         if (key->has_tunnel_id) {
2020             monitor_printf(mon, " tunnel %d", key->tunnel_id);
2021             if (mask->has_tunnel_id) {
2022                 monitor_printf(mon, "(0x%x)", mask->tunnel_id);
2023             }
2024         }
2025 
2026         if (key->has_eth_type) {
2027             switch (key->eth_type) {
2028             case 0x0806:
2029                 monitor_printf(mon, " ARP");
2030                 break;
2031             case 0x0800:
2032                 monitor_printf(mon, " IP");
2033                 break;
2034             case 0x86dd:
2035                 monitor_printf(mon, " IPv6");
2036                 break;
2037             case 0x8809:
2038                 monitor_printf(mon, " LACP");
2039                 break;
2040             case 0x88cc:
2041                 monitor_printf(mon, " LLDP");
2042                 break;
2043             default:
2044                 monitor_printf(mon, " eth type 0x%04x", key->eth_type);
2045                 break;
2046             }
2047         }
2048 
2049         if (key->has_eth_src) {
2050             if ((strcmp(key->eth_src, "01:00:00:00:00:00") == 0) &&
2051                 (mask->has_eth_src) &&
2052                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2053                 monitor_printf(mon, " src <any mcast/bcast>");
2054             } else if ((strcmp(key->eth_src, "00:00:00:00:00:00") == 0) &&
2055                 (mask->has_eth_src) &&
2056                 (strcmp(mask->eth_src, "01:00:00:00:00:00") == 0)) {
2057                 monitor_printf(mon, " src <any ucast>");
2058             } else {
2059                 monitor_printf(mon, " src %s", key->eth_src);
2060                 if (mask->has_eth_src) {
2061                     monitor_printf(mon, "(%s)", mask->eth_src);
2062                 }
2063             }
2064         }
2065 
2066         if (key->has_eth_dst) {
2067             if ((strcmp(key->eth_dst, "01:00:00:00:00:00") == 0) &&
2068                 (mask->has_eth_dst) &&
2069                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2070                 monitor_printf(mon, " dst <any mcast/bcast>");
2071             } else if ((strcmp(key->eth_dst, "00:00:00:00:00:00") == 0) &&
2072                 (mask->has_eth_dst) &&
2073                 (strcmp(mask->eth_dst, "01:00:00:00:00:00") == 0)) {
2074                 monitor_printf(mon, " dst <any ucast>");
2075             } else {
2076                 monitor_printf(mon, " dst %s", key->eth_dst);
2077                 if (mask->has_eth_dst) {
2078                     monitor_printf(mon, "(%s)", mask->eth_dst);
2079                 }
2080             }
2081         }
2082 
2083         if (key->has_ip_proto) {
2084             monitor_printf(mon, " proto %d", key->ip_proto);
2085             if (mask->has_ip_proto) {
2086                 monitor_printf(mon, "(0x%x)", mask->ip_proto);
2087             }
2088         }
2089 
2090         if (key->has_ip_tos) {
2091             monitor_printf(mon, " TOS %d", key->ip_tos);
2092             if (mask->has_ip_tos) {
2093                 monitor_printf(mon, "(0x%x)", mask->ip_tos);
2094             }
2095         }
2096 
2097         if (key->has_ip_dst) {
2098             monitor_printf(mon, " dst %s", key->ip_dst);
2099         }
2100 
2101         if (action->has_goto_tbl || action->has_group_id ||
2102             action->has_new_vlan_id) {
2103             monitor_printf(mon, " -->");
2104         }
2105 
2106         if (action->has_new_vlan_id) {
2107             monitor_printf(mon, " apply new vlan %d",
2108                            ntohs(action->new_vlan_id));
2109         }
2110 
2111         if (action->has_group_id) {
2112             monitor_printf(mon, " write group 0x%08x", action->group_id);
2113         }
2114 
2115         if (action->has_goto_tbl) {
2116             monitor_printf(mon, " goto tbl %d", action->goto_tbl);
2117         }
2118 
2119         monitor_printf(mon, "\n");
2120     }
2121 
2122     qapi_free_RockerOfDpaFlowList(list);
2123 }
2124 
2125 void hmp_rocker_of_dpa_groups(Monitor *mon, const QDict *qdict)
2126 {
2127     RockerOfDpaGroupList *list, *g;
2128     const char *name = qdict_get_str(qdict, "name");
2129     uint8_t type = qdict_get_try_int(qdict, "type", 9);
2130     Error *err = NULL;
2131 
2132     list = qmp_query_rocker_of_dpa_groups(name, type != 9, type, &err);
2133     if (err != NULL) {
2134         hmp_handle_error(mon, err);
2135         return;
2136     }
2137 
2138     monitor_printf(mon, "id (decode) --> buckets\n");
2139 
2140     for (g = list; g; g = g->next) {
2141         RockerOfDpaGroup *group = g->value;
2142         bool set = false;
2143 
2144         monitor_printf(mon, "0x%08x", group->id);
2145 
2146         monitor_printf(mon, " (type %s", group->type == 0 ? "L2 interface" :
2147                                          group->type == 1 ? "L2 rewrite" :
2148                                          group->type == 2 ? "L3 unicast" :
2149                                          group->type == 3 ? "L2 multicast" :
2150                                          group->type == 4 ? "L2 flood" :
2151                                          group->type == 5 ? "L3 interface" :
2152                                          group->type == 6 ? "L3 multicast" :
2153                                          group->type == 7 ? "L3 ECMP" :
2154                                          group->type == 8 ? "L2 overlay" :
2155                                          "unknown");
2156 
2157         if (group->has_vlan_id) {
2158             monitor_printf(mon, " vlan %d", group->vlan_id);
2159         }
2160 
2161         if (group->has_pport) {
2162             monitor_printf(mon, " pport %d", group->pport);
2163         }
2164 
2165         if (group->has_index) {
2166             monitor_printf(mon, " index %d", group->index);
2167         }
2168 
2169         monitor_printf(mon, ") -->");
2170 
2171         if (group->has_set_vlan_id && group->set_vlan_id) {
2172             set = true;
2173             monitor_printf(mon, " set vlan %d",
2174                            group->set_vlan_id & VLAN_VID_MASK);
2175         }
2176 
2177         if (group->has_set_eth_src) {
2178             if (!set) {
2179                 set = true;
2180                 monitor_printf(mon, " set");
2181             }
2182             monitor_printf(mon, " src %s", group->set_eth_src);
2183         }
2184 
2185         if (group->has_set_eth_dst) {
2186             if (!set) {
2187                 monitor_printf(mon, " set");
2188             }
2189             monitor_printf(mon, " dst %s", group->set_eth_dst);
2190         }
2191 
2192         if (group->has_ttl_check && group->ttl_check) {
2193             monitor_printf(mon, " check TTL");
2194         }
2195 
2196         if (group->has_group_id && group->group_id) {
2197             monitor_printf(mon, " group id 0x%08x", group->group_id);
2198         }
2199 
2200         if (group->has_pop_vlan && group->pop_vlan) {
2201             monitor_printf(mon, " pop vlan");
2202         }
2203 
2204         if (group->has_out_pport) {
2205             monitor_printf(mon, " out pport %d", group->out_pport);
2206         }
2207 
2208         if (group->has_group_ids) {
2209             struct uint32List *id;
2210 
2211             monitor_printf(mon, " groups [");
2212             for (id = group->group_ids; id; id = id->next) {
2213                 monitor_printf(mon, "0x%08x", id->value);
2214                 if (id->next) {
2215                     monitor_printf(mon, ",");
2216                 }
2217             }
2218             monitor_printf(mon, "]");
2219         }
2220 
2221         monitor_printf(mon, "\n");
2222     }
2223 
2224     qapi_free_RockerOfDpaGroupList(list);
2225 }
2226 
2227 void hmp_info_ramblock(Monitor *mon, const QDict *qdict)
2228 {
2229     ram_block_dump(mon);
2230 }
2231 
2232 void hmp_info_vm_generation_id(Monitor *mon, const QDict *qdict)
2233 {
2234     Error *err = NULL;
2235     GuidInfo *info = qmp_query_vm_generation_id(&err);
2236     if (info) {
2237         monitor_printf(mon, "%s\n", info->guid);
2238     }
2239     hmp_handle_error(mon, err);
2240     qapi_free_GuidInfo(info);
2241 }
2242 
2243 void hmp_info_memory_size_summary(Monitor *mon, const QDict *qdict)
2244 {
2245     Error *err = NULL;
2246     MemoryInfo *info = qmp_query_memory_size_summary(&err);
2247     if (info) {
2248         monitor_printf(mon, "base memory: %" PRIu64 "\n",
2249                        info->base_memory);
2250 
2251         if (info->has_plugged_memory) {
2252             monitor_printf(mon, "plugged memory: %" PRIu64 "\n",
2253                            info->plugged_memory);
2254         }
2255 
2256         qapi_free_MemoryInfo(info);
2257     }
2258     hmp_handle_error(mon, err);
2259 }
2260