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