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