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