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