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