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