]> Git Repo - qemu.git/blob - hmp.c
add a boot parameter to set reboot timeout
[qemu.git] / hmp.c
1 /*
2  * Human Monitor Interface
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <[email protected]>
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 "hmp.h"
17 #include "net.h"
18 #include "qemu-option.h"
19 #include "qemu-timer.h"
20 #include "qmp-commands.h"
21 #include "monitor.h"
22 #include "console.h"
23
24 static void hmp_handle_error(Monitor *mon, Error **errp)
25 {
26     if (error_is_set(errp)) {
27         monitor_printf(mon, "%s\n", error_get_pretty(*errp));
28         error_free(*errp);
29     }
30 }
31
32 void hmp_info_name(Monitor *mon)
33 {
34     NameInfo *info;
35
36     info = qmp_query_name(NULL);
37     if (info->has_name) {
38         monitor_printf(mon, "%s\n", info->name);
39     }
40     qapi_free_NameInfo(info);
41 }
42
43 void hmp_info_version(Monitor *mon)
44 {
45     VersionInfo *info;
46
47     info = qmp_query_version(NULL);
48
49     monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
50                    info->qemu.major, info->qemu.minor, info->qemu.micro,
51                    info->package);
52
53     qapi_free_VersionInfo(info);
54 }
55
56 void hmp_info_kvm(Monitor *mon)
57 {
58     KvmInfo *info;
59
60     info = qmp_query_kvm(NULL);
61     monitor_printf(mon, "kvm support: ");
62     if (info->present) {
63         monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
64     } else {
65         monitor_printf(mon, "not compiled\n");
66     }
67
68     qapi_free_KvmInfo(info);
69 }
70
71 void hmp_info_status(Monitor *mon)
72 {
73     StatusInfo *info;
74
75     info = qmp_query_status(NULL);
76
77     monitor_printf(mon, "VM status: %s%s",
78                    info->running ? "running" : "paused",
79                    info->singlestep ? " (single step mode)" : "");
80
81     if (!info->running && info->status != RUN_STATE_PAUSED) {
82         monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
83     }
84
85     monitor_printf(mon, "\n");
86
87     qapi_free_StatusInfo(info);
88 }
89
90 void hmp_info_uuid(Monitor *mon)
91 {
92     UuidInfo *info;
93
94     info = qmp_query_uuid(NULL);
95     monitor_printf(mon, "%s\n", info->UUID);
96     qapi_free_UuidInfo(info);
97 }
98
99 void hmp_info_chardev(Monitor *mon)
100 {
101     ChardevInfoList *char_info, *info;
102
103     char_info = qmp_query_chardev(NULL);
104     for (info = char_info; info; info = info->next) {
105         monitor_printf(mon, "%s: filename=%s\n", info->value->label,
106                                                  info->value->filename);
107     }
108
109     qapi_free_ChardevInfoList(char_info);
110 }
111
112 void hmp_info_mice(Monitor *mon)
113 {
114     MouseInfoList *mice_list, *mouse;
115
116     mice_list = qmp_query_mice(NULL);
117     if (!mice_list) {
118         monitor_printf(mon, "No mouse devices connected\n");
119         return;
120     }
121
122     for (mouse = mice_list; mouse; mouse = mouse->next) {
123         monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
124                        mouse->value->current ? '*' : ' ',
125                        mouse->value->index, mouse->value->name,
126                        mouse->value->absolute ? " (absolute)" : "");
127     }
128
129     qapi_free_MouseInfoList(mice_list);
130 }
131
132 void hmp_info_migrate(Monitor *mon)
133 {
134     MigrationInfo *info;
135     MigrationCapabilityStatusList *caps, *cap;
136
137     info = qmp_query_migrate(NULL);
138     caps = qmp_query_migrate_capabilities(NULL);
139
140     /* do not display parameters during setup */
141     if (info->has_status && caps) {
142         monitor_printf(mon, "capabilities: ");
143         for (cap = caps; cap; cap = cap->next) {
144             monitor_printf(mon, "%s: %s ",
145                            MigrationCapability_lookup[cap->value->capability],
146                            cap->value->state ? "on" : "off");
147         }
148         monitor_printf(mon, "\n");
149     }
150
151     if (info->has_status) {
152         monitor_printf(mon, "Migration status: %s\n", info->status);
153         monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
154                        info->total_time);
155     }
156
157     if (info->has_ram) {
158         monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
159                        info->ram->transferred >> 10);
160         monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
161                        info->ram->remaining >> 10);
162         monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
163                        info->ram->total >> 10);
164         monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
165                        info->ram->duplicate);
166         monitor_printf(mon, "normal: %" PRIu64 " pages\n",
167                        info->ram->normal);
168         monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
169                        info->ram->normal_bytes >> 10);
170     }
171
172     if (info->has_disk) {
173         monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
174                        info->disk->transferred >> 10);
175         monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
176                        info->disk->remaining >> 10);
177         monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
178                        info->disk->total >> 10);
179     }
180
181     if (info->has_xbzrle_cache) {
182         monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
183                        info->xbzrle_cache->cache_size);
184         monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
185                        info->xbzrle_cache->bytes >> 10);
186         monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
187                        info->xbzrle_cache->pages);
188         monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
189                        info->xbzrle_cache->cache_miss);
190         monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
191                        info->xbzrle_cache->overflow);
192     }
193
194     qapi_free_MigrationInfo(info);
195     qapi_free_MigrationCapabilityStatusList(caps);
196 }
197
198 void hmp_info_migrate_capabilities(Monitor *mon)
199 {
200     MigrationCapabilityStatusList *caps, *cap;
201
202     caps = qmp_query_migrate_capabilities(NULL);
203
204     if (caps) {
205         monitor_printf(mon, "capabilities: ");
206         for (cap = caps; cap; cap = cap->next) {
207             monitor_printf(mon, "%s: %s ",
208                            MigrationCapability_lookup[cap->value->capability],
209                            cap->value->state ? "on" : "off");
210         }
211         monitor_printf(mon, "\n");
212     }
213
214     qapi_free_MigrationCapabilityStatusList(caps);
215 }
216
217 void hmp_info_migrate_cache_size(Monitor *mon)
218 {
219     monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
220                    qmp_query_migrate_cache_size(NULL) >> 10);
221 }
222
223 void hmp_info_cpus(Monitor *mon)
224 {
225     CpuInfoList *cpu_list, *cpu;
226
227     cpu_list = qmp_query_cpus(NULL);
228
229     for (cpu = cpu_list; cpu; cpu = cpu->next) {
230         int active = ' ';
231
232         if (cpu->value->CPU == monitor_get_cpu_index()) {
233             active = '*';
234         }
235
236         monitor_printf(mon, "%c CPU #%" PRId64 ": ", active, cpu->value->CPU);
237
238         if (cpu->value->has_pc) {
239             monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
240         }
241         if (cpu->value->has_nip) {
242             monitor_printf(mon, "nip=0x%016" PRIx64, cpu->value->nip);
243         }
244         if (cpu->value->has_npc) {
245             monitor_printf(mon, "pc=0x%016" PRIx64, cpu->value->pc);
246             monitor_printf(mon, "npc=0x%016" PRIx64, cpu->value->npc);
247         }
248         if (cpu->value->has_PC) {
249             monitor_printf(mon, "PC=0x%016" PRIx64, cpu->value->PC);
250         }
251
252         if (cpu->value->halted) {
253             monitor_printf(mon, " (halted)");
254         }
255
256         monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
257     }
258
259     qapi_free_CpuInfoList(cpu_list);
260 }
261
262 void hmp_info_block(Monitor *mon)
263 {
264     BlockInfoList *block_list, *info;
265
266     block_list = qmp_query_block(NULL);
267
268     for (info = block_list; info; info = info->next) {
269         monitor_printf(mon, "%s: removable=%d",
270                        info->value->device, info->value->removable);
271
272         if (info->value->removable) {
273             monitor_printf(mon, " locked=%d", info->value->locked);
274             monitor_printf(mon, " tray-open=%d", info->value->tray_open);
275         }
276
277         if (info->value->has_io_status) {
278             monitor_printf(mon, " io-status=%s",
279                            BlockDeviceIoStatus_lookup[info->value->io_status]);
280         }
281
282         if (info->value->has_inserted) {
283             monitor_printf(mon, " file=");
284             monitor_print_filename(mon, info->value->inserted->file);
285
286             if (info->value->inserted->has_backing_file) {
287                 monitor_printf(mon, " backing_file=");
288                 monitor_print_filename(mon, info->value->inserted->backing_file);
289                 monitor_printf(mon, " backing_file_depth=%" PRId64,
290                     info->value->inserted->backing_file_depth);
291             }
292             monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
293                            info->value->inserted->ro,
294                            info->value->inserted->drv,
295                            info->value->inserted->encrypted);
296
297             monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
298                             " bps_wr=%" PRId64 " iops=%" PRId64
299                             " iops_rd=%" PRId64 " iops_wr=%" PRId64,
300                             info->value->inserted->bps,
301                             info->value->inserted->bps_rd,
302                             info->value->inserted->bps_wr,
303                             info->value->inserted->iops,
304                             info->value->inserted->iops_rd,
305                             info->value->inserted->iops_wr);
306         } else {
307             monitor_printf(mon, " [not inserted]");
308         }
309
310         monitor_printf(mon, "\n");
311     }
312
313     qapi_free_BlockInfoList(block_list);
314 }
315
316 void hmp_info_blockstats(Monitor *mon)
317 {
318     BlockStatsList *stats_list, *stats;
319
320     stats_list = qmp_query_blockstats(NULL);
321
322     for (stats = stats_list; stats; stats = stats->next) {
323         if (!stats->value->has_device) {
324             continue;
325         }
326
327         monitor_printf(mon, "%s:", stats->value->device);
328         monitor_printf(mon, " rd_bytes=%" PRId64
329                        " wr_bytes=%" PRId64
330                        " rd_operations=%" PRId64
331                        " wr_operations=%" PRId64
332                        " flush_operations=%" PRId64
333                        " wr_total_time_ns=%" PRId64
334                        " rd_total_time_ns=%" PRId64
335                        " flush_total_time_ns=%" PRId64
336                        "\n",
337                        stats->value->stats->rd_bytes,
338                        stats->value->stats->wr_bytes,
339                        stats->value->stats->rd_operations,
340                        stats->value->stats->wr_operations,
341                        stats->value->stats->flush_operations,
342                        stats->value->stats->wr_total_time_ns,
343                        stats->value->stats->rd_total_time_ns,
344                        stats->value->stats->flush_total_time_ns);
345     }
346
347     qapi_free_BlockStatsList(stats_list);
348 }
349
350 void hmp_info_vnc(Monitor *mon)
351 {
352     VncInfo *info;
353     Error *err = NULL;
354     VncClientInfoList *client;
355
356     info = qmp_query_vnc(&err);
357     if (err) {
358         monitor_printf(mon, "%s\n", error_get_pretty(err));
359         error_free(err);
360         return;
361     }
362
363     if (!info->enabled) {
364         monitor_printf(mon, "Server: disabled\n");
365         goto out;
366     }
367
368     monitor_printf(mon, "Server:\n");
369     if (info->has_host && info->has_service) {
370         monitor_printf(mon, "     address: %s:%s\n", info->host, info->service);
371     }
372     if (info->has_auth) {
373         monitor_printf(mon, "        auth: %s\n", info->auth);
374     }
375
376     if (!info->has_clients || info->clients == NULL) {
377         monitor_printf(mon, "Client: none\n");
378     } else {
379         for (client = info->clients; client; client = client->next) {
380             monitor_printf(mon, "Client:\n");
381             monitor_printf(mon, "     address: %s:%s\n",
382                            client->value->host, client->value->service);
383             monitor_printf(mon, "  x509_dname: %s\n",
384                            client->value->x509_dname ?
385                            client->value->x509_dname : "none");
386             monitor_printf(mon, "    username: %s\n",
387                            client->value->has_sasl_username ?
388                            client->value->sasl_username : "none");
389         }
390     }
391
392 out:
393     qapi_free_VncInfo(info);
394 }
395
396 void hmp_info_spice(Monitor *mon)
397 {
398     SpiceChannelList *chan;
399     SpiceInfo *info;
400
401     info = qmp_query_spice(NULL);
402
403     if (!info->enabled) {
404         monitor_printf(mon, "Server: disabled\n");
405         goto out;
406     }
407
408     monitor_printf(mon, "Server:\n");
409     if (info->has_port) {
410         monitor_printf(mon, "     address: %s:%" PRId64 "\n",
411                        info->host, info->port);
412     }
413     if (info->has_tls_port) {
414         monitor_printf(mon, "     address: %s:%" PRId64 " [tls]\n",
415                        info->host, info->tls_port);
416     }
417     monitor_printf(mon, "    migrated: %s\n",
418                    info->migrated ? "true" : "false");
419     monitor_printf(mon, "        auth: %s\n", info->auth);
420     monitor_printf(mon, "    compiled: %s\n", info->compiled_version);
421     monitor_printf(mon, "  mouse-mode: %s\n",
422                    SpiceQueryMouseMode_lookup[info->mouse_mode]);
423
424     if (!info->has_channels || info->channels == NULL) {
425         monitor_printf(mon, "Channels: none\n");
426     } else {
427         for (chan = info->channels; chan; chan = chan->next) {
428             monitor_printf(mon, "Channel:\n");
429             monitor_printf(mon, "     address: %s:%s%s\n",
430                            chan->value->host, chan->value->port,
431                            chan->value->tls ? " [tls]" : "");
432             monitor_printf(mon, "     session: %" PRId64 "\n",
433                            chan->value->connection_id);
434             monitor_printf(mon, "     channel: %" PRId64 ":%" PRId64 "\n",
435                            chan->value->channel_type, chan->value->channel_id);
436         }
437     }
438
439 out:
440     qapi_free_SpiceInfo(info);
441 }
442
443 void hmp_info_balloon(Monitor *mon)
444 {
445     BalloonInfo *info;
446     Error *err = NULL;
447
448     info = qmp_query_balloon(&err);
449     if (err) {
450         monitor_printf(mon, "%s\n", error_get_pretty(err));
451         error_free(err);
452         return;
453     }
454
455     monitor_printf(mon, "balloon: actual=%" PRId64, info->actual >> 20);
456     if (info->has_mem_swapped_in) {
457         monitor_printf(mon, " mem_swapped_in=%" PRId64, info->mem_swapped_in);
458     }
459     if (info->has_mem_swapped_out) {
460         monitor_printf(mon, " mem_swapped_out=%" PRId64, info->mem_swapped_out);
461     }
462     if (info->has_major_page_faults) {
463         monitor_printf(mon, " major_page_faults=%" PRId64,
464                        info->major_page_faults);
465     }
466     if (info->has_minor_page_faults) {
467         monitor_printf(mon, " minor_page_faults=%" PRId64,
468                        info->minor_page_faults);
469     }
470     if (info->has_free_mem) {
471         monitor_printf(mon, " free_mem=%" PRId64, info->free_mem);
472     }
473     if (info->has_total_mem) {
474         monitor_printf(mon, " total_mem=%" PRId64, info->total_mem);
475     }
476
477     monitor_printf(mon, "\n");
478
479     qapi_free_BalloonInfo(info);
480 }
481
482 static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
483 {
484     PciMemoryRegionList *region;
485
486     monitor_printf(mon, "  Bus %2" PRId64 ", ", dev->bus);
487     monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
488                    dev->slot, dev->function);
489     monitor_printf(mon, "    ");
490
491     if (dev->class_info.has_desc) {
492         monitor_printf(mon, "%s", dev->class_info.desc);
493     } else {
494         monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
495     }
496
497     monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
498                    dev->id.vendor, dev->id.device);
499
500     if (dev->has_irq) {
501         monitor_printf(mon, "      IRQ %" PRId64 ".\n", dev->irq);
502     }
503
504     if (dev->has_pci_bridge) {
505         monitor_printf(mon, "      BUS %" PRId64 ".\n",
506                        dev->pci_bridge->bus.number);
507         monitor_printf(mon, "      secondary bus %" PRId64 ".\n",
508                        dev->pci_bridge->bus.secondary);
509         monitor_printf(mon, "      subordinate bus %" PRId64 ".\n",
510                        dev->pci_bridge->bus.subordinate);
511
512         monitor_printf(mon, "      IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
513                        dev->pci_bridge->bus.io_range->base,
514                        dev->pci_bridge->bus.io_range->limit);
515
516         monitor_printf(mon,
517                        "      memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
518                        dev->pci_bridge->bus.memory_range->base,
519                        dev->pci_bridge->bus.memory_range->limit);
520
521         monitor_printf(mon, "      prefetchable memory range "
522                        "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
523                        dev->pci_bridge->bus.prefetchable_range->base,
524                        dev->pci_bridge->bus.prefetchable_range->limit);
525     }
526
527     for (region = dev->regions; region; region = region->next) {
528         uint64_t addr, size;
529
530         addr = region->value->address;
531         size = region->value->size;
532
533         monitor_printf(mon, "      BAR%" PRId64 ": ", region->value->bar);
534
535         if (!strcmp(region->value->type, "io")) {
536             monitor_printf(mon, "I/O at 0x%04" PRIx64
537                                 " [0x%04" PRIx64 "].\n",
538                            addr, addr + size - 1);
539         } else {
540             monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
541                                " [0x%08" PRIx64 "].\n",
542                            region->value->mem_type_64 ? 64 : 32,
543                            region->value->prefetch ? " prefetchable" : "",
544                            addr, addr + size - 1);
545         }
546     }
547
548     monitor_printf(mon, "      id \"%s\"\n", dev->qdev_id);
549
550     if (dev->has_pci_bridge) {
551         if (dev->pci_bridge->has_devices) {
552             PciDeviceInfoList *cdev;
553             for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
554                 hmp_info_pci_device(mon, cdev->value);
555             }
556         }
557     }
558 }
559
560 void hmp_info_pci(Monitor *mon)
561 {
562     PciInfoList *info_list, *info;
563     Error *err = NULL;
564
565     info_list = qmp_query_pci(&err);
566     if (err) {
567         monitor_printf(mon, "PCI devices not supported\n");
568         error_free(err);
569         return;
570     }
571
572     for (info = info_list; info; info = info->next) {
573         PciDeviceInfoList *dev;
574
575         for (dev = info->value->devices; dev; dev = dev->next) {
576             hmp_info_pci_device(mon, dev->value);
577         }
578     }
579
580     qapi_free_PciInfoList(info_list);
581 }
582
583 void hmp_info_block_jobs(Monitor *mon)
584 {
585     BlockJobInfoList *list;
586     Error *err = NULL;
587
588     list = qmp_query_block_jobs(&err);
589     assert(!err);
590
591     if (!list) {
592         monitor_printf(mon, "No active jobs\n");
593         return;
594     }
595
596     while (list) {
597         if (strcmp(list->value->type, "stream") == 0) {
598             monitor_printf(mon, "Streaming device %s: Completed %" PRId64
599                            " of %" PRId64 " bytes, speed limit %" PRId64
600                            " bytes/s\n",
601                            list->value->device,
602                            list->value->offset,
603                            list->value->len,
604                            list->value->speed);
605         } else {
606             monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
607                            " of %" PRId64 " bytes, speed limit %" PRId64
608                            " bytes/s\n",
609                            list->value->type,
610                            list->value->device,
611                            list->value->offset,
612                            list->value->len,
613                            list->value->speed);
614         }
615         list = list->next;
616     }
617 }
618
619 void hmp_quit(Monitor *mon, const QDict *qdict)
620 {
621     monitor_suspend(mon);
622     qmp_quit(NULL);
623 }
624
625 void hmp_stop(Monitor *mon, const QDict *qdict)
626 {
627     qmp_stop(NULL);
628 }
629
630 void hmp_system_reset(Monitor *mon, const QDict *qdict)
631 {
632     qmp_system_reset(NULL);
633 }
634
635 void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
636 {
637     qmp_system_powerdown(NULL);
638 }
639
640 void hmp_cpu(Monitor *mon, const QDict *qdict)
641 {
642     int64_t cpu_index;
643
644     /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
645             use it are converted to the QAPI */
646     cpu_index = qdict_get_int(qdict, "index");
647     if (monitor_set_cpu(cpu_index) < 0) {
648         monitor_printf(mon, "invalid CPU index\n");
649     }
650 }
651
652 void hmp_memsave(Monitor *mon, const QDict *qdict)
653 {
654     uint32_t size = qdict_get_int(qdict, "size");
655     const char *filename = qdict_get_str(qdict, "filename");
656     uint64_t addr = qdict_get_int(qdict, "val");
657     Error *errp = NULL;
658
659     qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
660     hmp_handle_error(mon, &errp);
661 }
662
663 void hmp_pmemsave(Monitor *mon, const QDict *qdict)
664 {
665     uint32_t size = qdict_get_int(qdict, "size");
666     const char *filename = qdict_get_str(qdict, "filename");
667     uint64_t addr = qdict_get_int(qdict, "val");
668     Error *errp = NULL;
669
670     qmp_pmemsave(addr, size, filename, &errp);
671     hmp_handle_error(mon, &errp);
672 }
673
674 static void hmp_cont_cb(void *opaque, int err)
675 {
676     if (!err) {
677         qmp_cont(NULL);
678     }
679 }
680
681 static bool key_is_missing(const BlockInfo *bdev)
682 {
683     return (bdev->inserted && bdev->inserted->encryption_key_missing);
684 }
685
686 void hmp_cont(Monitor *mon, const QDict *qdict)
687 {
688     BlockInfoList *bdev_list, *bdev;
689     Error *errp = NULL;
690
691     bdev_list = qmp_query_block(NULL);
692     for (bdev = bdev_list; bdev; bdev = bdev->next) {
693         if (key_is_missing(bdev->value)) {
694             monitor_read_block_device_key(mon, bdev->value->device,
695                                           hmp_cont_cb, NULL);
696             goto out;
697         }
698     }
699
700     qmp_cont(&errp);
701     hmp_handle_error(mon, &errp);
702
703 out:
704     qapi_free_BlockInfoList(bdev_list);
705 }
706
707 void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
708 {
709     qmp_system_wakeup(NULL);
710 }
711
712 void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
713 {
714     Error *errp = NULL;
715
716     qmp_inject_nmi(&errp);
717     hmp_handle_error(mon, &errp);
718 }
719
720 void hmp_set_link(Monitor *mon, const QDict *qdict)
721 {
722     const char *name = qdict_get_str(qdict, "name");
723     int up = qdict_get_bool(qdict, "up");
724     Error *errp = NULL;
725
726     qmp_set_link(name, up, &errp);
727     hmp_handle_error(mon, &errp);
728 }
729
730 void hmp_block_passwd(Monitor *mon, const QDict *qdict)
731 {
732     const char *device = qdict_get_str(qdict, "device");
733     const char *password = qdict_get_str(qdict, "password");
734     Error *errp = NULL;
735
736     qmp_block_passwd(device, password, &errp);
737     hmp_handle_error(mon, &errp);
738 }
739
740 void hmp_balloon(Monitor *mon, const QDict *qdict)
741 {
742     int64_t value = qdict_get_int(qdict, "value");
743     Error *errp = NULL;
744
745     qmp_balloon(value, &errp);
746     if (error_is_set(&errp)) {
747         monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
748         error_free(errp);
749     }
750 }
751
752 void hmp_block_resize(Monitor *mon, const QDict *qdict)
753 {
754     const char *device = qdict_get_str(qdict, "device");
755     int64_t size = qdict_get_int(qdict, "size");
756     Error *errp = NULL;
757
758     qmp_block_resize(device, size, &errp);
759     hmp_handle_error(mon, &errp);
760 }
761
762 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
763 {
764     const char *device = qdict_get_str(qdict, "device");
765     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
766     const char *format = qdict_get_try_str(qdict, "format");
767     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
768     enum NewImageMode mode;
769     Error *errp = NULL;
770
771     if (!filename) {
772         /* In the future, if 'snapshot-file' is not specified, the snapshot
773            will be taken internally. Today it's actually required. */
774         error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
775         hmp_handle_error(mon, &errp);
776         return;
777     }
778
779     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
780     qmp_blockdev_snapshot_sync(device, filename, !!format, format,
781                                true, mode, &errp);
782     hmp_handle_error(mon, &errp);
783 }
784
785 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
786 {
787     qmp_migrate_cancel(NULL);
788 }
789
790 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
791 {
792     double value = qdict_get_double(qdict, "value");
793     qmp_migrate_set_downtime(value, NULL);
794 }
795
796 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
797 {
798     int64_t value = qdict_get_int(qdict, "value");
799     Error *err = NULL;
800
801     qmp_migrate_set_cache_size(value, &err);
802     if (err) {
803         monitor_printf(mon, "%s\n", error_get_pretty(err));
804         error_free(err);
805         return;
806     }
807 }
808
809 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
810 {
811     int64_t value = qdict_get_int(qdict, "value");
812     qmp_migrate_set_speed(value, NULL);
813 }
814
815 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
816 {
817     const char *cap = qdict_get_str(qdict, "capability");
818     bool state = qdict_get_bool(qdict, "state");
819     Error *err = NULL;
820     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
821     int i;
822
823     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
824         if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
825             caps->value = g_malloc0(sizeof(*caps->value));
826             caps->value->capability = i;
827             caps->value->state = state;
828             caps->next = NULL;
829             qmp_migrate_set_capabilities(caps, &err);
830             break;
831         }
832     }
833
834     if (i == MIGRATION_CAPABILITY_MAX) {
835         error_set(&err, QERR_INVALID_PARAMETER, cap);
836     }
837
838     qapi_free_MigrationCapabilityStatusList(caps);
839
840     if (err) {
841         monitor_printf(mon, "migrate_set_parameter: %s\n",
842                        error_get_pretty(err));
843         error_free(err);
844     }
845 }
846
847 void hmp_set_password(Monitor *mon, const QDict *qdict)
848 {
849     const char *protocol  = qdict_get_str(qdict, "protocol");
850     const char *password  = qdict_get_str(qdict, "password");
851     const char *connected = qdict_get_try_str(qdict, "connected");
852     Error *err = NULL;
853
854     qmp_set_password(protocol, password, !!connected, connected, &err);
855     hmp_handle_error(mon, &err);
856 }
857
858 void hmp_expire_password(Monitor *mon, const QDict *qdict)
859 {
860     const char *protocol  = qdict_get_str(qdict, "protocol");
861     const char *whenstr = qdict_get_str(qdict, "time");
862     Error *err = NULL;
863
864     qmp_expire_password(protocol, whenstr, &err);
865     hmp_handle_error(mon, &err);
866 }
867
868 void hmp_eject(Monitor *mon, const QDict *qdict)
869 {
870     int force = qdict_get_try_bool(qdict, "force", 0);
871     const char *device = qdict_get_str(qdict, "device");
872     Error *err = NULL;
873
874     qmp_eject(device, true, force, &err);
875     hmp_handle_error(mon, &err);
876 }
877
878 static void hmp_change_read_arg(Monitor *mon, const char *password,
879                                 void *opaque)
880 {
881     qmp_change_vnc_password(password, NULL);
882     monitor_read_command(mon, 1);
883 }
884
885 void hmp_change(Monitor *mon, const QDict *qdict)
886 {
887     const char *device = qdict_get_str(qdict, "device");
888     const char *target = qdict_get_str(qdict, "target");
889     const char *arg = qdict_get_try_str(qdict, "arg");
890     Error *err = NULL;
891
892     if (strcmp(device, "vnc") == 0 &&
893             (strcmp(target, "passwd") == 0 ||
894              strcmp(target, "password") == 0)) {
895         if (!arg) {
896             monitor_read_password(mon, hmp_change_read_arg, NULL);
897             return;
898         }
899     }
900
901     qmp_change(device, target, !!arg, arg, &err);
902     if (error_is_set(&err) &&
903         error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
904         error_free(err);
905         monitor_read_block_device_key(mon, device, NULL, NULL);
906         return;
907     }
908     hmp_handle_error(mon, &err);
909 }
910
911 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
912 {
913     Error *err = NULL;
914
915     qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
916                               qdict_get_int(qdict, "bps"),
917                               qdict_get_int(qdict, "bps_rd"),
918                               qdict_get_int(qdict, "bps_wr"),
919                               qdict_get_int(qdict, "iops"),
920                               qdict_get_int(qdict, "iops_rd"),
921                               qdict_get_int(qdict, "iops_wr"), &err);
922     hmp_handle_error(mon, &err);
923 }
924
925 void hmp_block_stream(Monitor *mon, const QDict *qdict)
926 {
927     Error *error = NULL;
928     const char *device = qdict_get_str(qdict, "device");
929     const char *base = qdict_get_try_str(qdict, "base");
930     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
931
932     qmp_block_stream(device, base != NULL, base,
933                      qdict_haskey(qdict, "speed"), speed, &error);
934
935     hmp_handle_error(mon, &error);
936 }
937
938 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
939 {
940     Error *error = NULL;
941     const char *device = qdict_get_str(qdict, "device");
942     int64_t value = qdict_get_int(qdict, "speed");
943
944     qmp_block_job_set_speed(device, value, &error);
945
946     hmp_handle_error(mon, &error);
947 }
948
949 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
950 {
951     Error *error = NULL;
952     const char *device = qdict_get_str(qdict, "device");
953
954     qmp_block_job_cancel(device, &error);
955
956     hmp_handle_error(mon, &error);
957 }
958
959 typedef struct MigrationStatus
960 {
961     QEMUTimer *timer;
962     Monitor *mon;
963     bool is_block_migration;
964 } MigrationStatus;
965
966 static void hmp_migrate_status_cb(void *opaque)
967 {
968     MigrationStatus *status = opaque;
969     MigrationInfo *info;
970
971     info = qmp_query_migrate(NULL);
972     if (!info->has_status || strcmp(info->status, "active") == 0) {
973         if (info->has_disk) {
974             int progress;
975
976             if (info->disk->remaining) {
977                 progress = info->disk->transferred * 100 / info->disk->total;
978             } else {
979                 progress = 100;
980             }
981
982             monitor_printf(status->mon, "Completed %d %%\r", progress);
983             monitor_flush(status->mon);
984         }
985
986         qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
987     } else {
988         if (status->is_block_migration) {
989             monitor_printf(status->mon, "\n");
990         }
991         monitor_resume(status->mon);
992         qemu_del_timer(status->timer);
993         g_free(status);
994     }
995
996     qapi_free_MigrationInfo(info);
997 }
998
999 void hmp_migrate(Monitor *mon, const QDict *qdict)
1000 {
1001     int detach = qdict_get_try_bool(qdict, "detach", 0);
1002     int blk = qdict_get_try_bool(qdict, "blk", 0);
1003     int inc = qdict_get_try_bool(qdict, "inc", 0);
1004     const char *uri = qdict_get_str(qdict, "uri");
1005     Error *err = NULL;
1006
1007     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1008     if (err) {
1009         monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1010         error_free(err);
1011         return;
1012     }
1013
1014     if (!detach) {
1015         MigrationStatus *status;
1016
1017         if (monitor_suspend(mon) < 0) {
1018             monitor_printf(mon, "terminal does not allow synchronous "
1019                            "migration, continuing detached\n");
1020             return;
1021         }
1022
1023         status = g_malloc0(sizeof(*status));
1024         status->mon = mon;
1025         status->is_block_migration = blk || inc;
1026         status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1027                                           status);
1028         qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1029     }
1030 }
1031
1032 void hmp_device_del(Monitor *mon, const QDict *qdict)
1033 {
1034     const char *id = qdict_get_str(qdict, "id");
1035     Error *err = NULL;
1036
1037     qmp_device_del(id, &err);
1038     hmp_handle_error(mon, &err);
1039 }
1040
1041 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1042 {
1043     Error *errp = NULL;
1044     int paging = qdict_get_try_bool(qdict, "paging", 0);
1045     const char *file = qdict_get_str(qdict, "protocol");
1046     bool has_begin = qdict_haskey(qdict, "begin");
1047     bool has_length = qdict_haskey(qdict, "length");
1048     int64_t begin = 0;
1049     int64_t length = 0;
1050
1051     if (has_begin) {
1052         begin = qdict_get_int(qdict, "begin");
1053     }
1054     if (has_length) {
1055         length = qdict_get_int(qdict, "length");
1056     }
1057
1058     qmp_dump_guest_memory(paging, file, has_begin, begin, has_length, length,
1059                           &errp);
1060     hmp_handle_error(mon, &errp);
1061 }
1062
1063 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1064 {
1065     Error *err = NULL;
1066     QemuOpts *opts;
1067
1068     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1069     if (error_is_set(&err)) {
1070         goto out;
1071     }
1072
1073     netdev_add(opts, &err);
1074     if (error_is_set(&err)) {
1075         qemu_opts_del(opts);
1076     }
1077
1078 out:
1079     hmp_handle_error(mon, &err);
1080 }
1081
1082 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1083 {
1084     const char *id = qdict_get_str(qdict, "id");
1085     Error *err = NULL;
1086
1087     qmp_netdev_del(id, &err);
1088     hmp_handle_error(mon, &err);
1089 }
1090
1091 void hmp_getfd(Monitor *mon, const QDict *qdict)
1092 {
1093     const char *fdname = qdict_get_str(qdict, "fdname");
1094     Error *errp = NULL;
1095
1096     qmp_getfd(fdname, &errp);
1097     hmp_handle_error(mon, &errp);
1098 }
1099
1100 void hmp_closefd(Monitor *mon, const QDict *qdict)
1101 {
1102     const char *fdname = qdict_get_str(qdict, "fdname");
1103     Error *errp = NULL;
1104
1105     qmp_closefd(fdname, &errp);
1106     hmp_handle_error(mon, &errp);
1107 }
1108
1109 void hmp_send_key(Monitor *mon, const QDict *qdict)
1110 {
1111     const char *keys = qdict_get_str(qdict, "keys");
1112     QKeyCodeList *keylist, *head = NULL, *tmp = NULL;
1113     int has_hold_time = qdict_haskey(qdict, "hold-time");
1114     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1115     Error *err = NULL;
1116     char keyname_buf[16];
1117     char *separator;
1118     int keyname_len, idx;
1119
1120     while (1) {
1121         separator = strchr(keys, '-');
1122         keyname_len = separator ? separator - keys : strlen(keys);
1123         pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1124
1125         /* Be compatible with old interface, convert user inputted "<" */
1126         if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1127             pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1128             keyname_len = 4;
1129         }
1130         keyname_buf[keyname_len] = 0;
1131
1132         idx = index_from_key(keyname_buf);
1133         if (idx == Q_KEY_CODE_MAX) {
1134             monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1135             break;
1136         }
1137
1138         keylist = g_malloc0(sizeof(*keylist));
1139         keylist->value = idx;
1140         keylist->next = NULL;
1141
1142         if (!head) {
1143             head = keylist;
1144         }
1145         if (tmp) {
1146             tmp->next = keylist;
1147         }
1148         tmp = keylist;
1149
1150         if (!separator) {
1151             break;
1152         }
1153         keys = separator + 1;
1154     }
1155
1156     if (idx != Q_KEY_CODE_MAX) {
1157         qmp_send_key(head, has_hold_time, hold_time, &err);
1158     }
1159     hmp_handle_error(mon, &err);
1160     qapi_free_QKeyCodeList(head);
1161 }
1162
1163 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1164 {
1165     const char *filename = qdict_get_str(qdict, "filename");
1166     Error *err = NULL;
1167
1168     qmp_screendump(filename, &err);
1169     hmp_handle_error(mon, &err);
1170 }
This page took 0.089984 seconds and 4 git commands to generate.