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