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