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