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