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