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