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