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