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