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