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