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