]> Git Repo - qemu.git/blob - hmp.c
Merge branch 'qspi.2' of git://developer.petalogix.com/public/qemu
[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_drive_mirror(Monitor *mon, const QDict *qdict)
774 {
775     const char *device = qdict_get_str(qdict, "device");
776     const char *filename = qdict_get_str(qdict, "target");
777     const char *format = qdict_get_try_str(qdict, "format");
778     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
779     int full = qdict_get_try_bool(qdict, "full", 0);
780     enum NewImageMode mode;
781     Error *errp = NULL;
782
783     if (!filename) {
784         error_set(&errp, QERR_MISSING_PARAMETER, "target");
785         hmp_handle_error(mon, &errp);
786         return;
787     }
788
789     if (reuse) {
790         mode = NEW_IMAGE_MODE_EXISTING;
791     } else {
792         mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
793     }
794
795     qmp_drive_mirror(device, filename, !!format, format,
796                      full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
797                      true, mode, false, 0,
798                      false, 0, false, 0, &errp);
799     hmp_handle_error(mon, &errp);
800 }
801
802 void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
803 {
804     const char *device = qdict_get_str(qdict, "device");
805     const char *filename = qdict_get_try_str(qdict, "snapshot-file");
806     const char *format = qdict_get_try_str(qdict, "format");
807     int reuse = qdict_get_try_bool(qdict, "reuse", 0);
808     enum NewImageMode mode;
809     Error *errp = NULL;
810
811     if (!filename) {
812         /* In the future, if 'snapshot-file' is not specified, the snapshot
813            will be taken internally. Today it's actually required. */
814         error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
815         hmp_handle_error(mon, &errp);
816         return;
817     }
818
819     mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
820     qmp_blockdev_snapshot_sync(device, filename, !!format, format,
821                                true, mode, &errp);
822     hmp_handle_error(mon, &errp);
823 }
824
825 void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
826 {
827     qmp_migrate_cancel(NULL);
828 }
829
830 void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
831 {
832     double value = qdict_get_double(qdict, "value");
833     qmp_migrate_set_downtime(value, NULL);
834 }
835
836 void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
837 {
838     int64_t value = qdict_get_int(qdict, "value");
839     Error *err = NULL;
840
841     qmp_migrate_set_cache_size(value, &err);
842     if (err) {
843         monitor_printf(mon, "%s\n", error_get_pretty(err));
844         error_free(err);
845         return;
846     }
847 }
848
849 void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
850 {
851     int64_t value = qdict_get_int(qdict, "value");
852     qmp_migrate_set_speed(value, NULL);
853 }
854
855 void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
856 {
857     const char *cap = qdict_get_str(qdict, "capability");
858     bool state = qdict_get_bool(qdict, "state");
859     Error *err = NULL;
860     MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
861     int i;
862
863     for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
864         if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
865             caps->value = g_malloc0(sizeof(*caps->value));
866             caps->value->capability = i;
867             caps->value->state = state;
868             caps->next = NULL;
869             qmp_migrate_set_capabilities(caps, &err);
870             break;
871         }
872     }
873
874     if (i == MIGRATION_CAPABILITY_MAX) {
875         error_set(&err, QERR_INVALID_PARAMETER, cap);
876     }
877
878     qapi_free_MigrationCapabilityStatusList(caps);
879
880     if (err) {
881         monitor_printf(mon, "migrate_set_parameter: %s\n",
882                        error_get_pretty(err));
883         error_free(err);
884     }
885 }
886
887 void hmp_set_password(Monitor *mon, const QDict *qdict)
888 {
889     const char *protocol  = qdict_get_str(qdict, "protocol");
890     const char *password  = qdict_get_str(qdict, "password");
891     const char *connected = qdict_get_try_str(qdict, "connected");
892     Error *err = NULL;
893
894     qmp_set_password(protocol, password, !!connected, connected, &err);
895     hmp_handle_error(mon, &err);
896 }
897
898 void hmp_expire_password(Monitor *mon, const QDict *qdict)
899 {
900     const char *protocol  = qdict_get_str(qdict, "protocol");
901     const char *whenstr = qdict_get_str(qdict, "time");
902     Error *err = NULL;
903
904     qmp_expire_password(protocol, whenstr, &err);
905     hmp_handle_error(mon, &err);
906 }
907
908 void hmp_eject(Monitor *mon, const QDict *qdict)
909 {
910     int force = qdict_get_try_bool(qdict, "force", 0);
911     const char *device = qdict_get_str(qdict, "device");
912     Error *err = NULL;
913
914     qmp_eject(device, true, force, &err);
915     hmp_handle_error(mon, &err);
916 }
917
918 static void hmp_change_read_arg(Monitor *mon, const char *password,
919                                 void *opaque)
920 {
921     qmp_change_vnc_password(password, NULL);
922     monitor_read_command(mon, 1);
923 }
924
925 void hmp_change(Monitor *mon, const QDict *qdict)
926 {
927     const char *device = qdict_get_str(qdict, "device");
928     const char *target = qdict_get_str(qdict, "target");
929     const char *arg = qdict_get_try_str(qdict, "arg");
930     Error *err = NULL;
931
932     if (strcmp(device, "vnc") == 0 &&
933             (strcmp(target, "passwd") == 0 ||
934              strcmp(target, "password") == 0)) {
935         if (!arg) {
936             monitor_read_password(mon, hmp_change_read_arg, NULL);
937             return;
938         }
939     }
940
941     qmp_change(device, target, !!arg, arg, &err);
942     if (error_is_set(&err) &&
943         error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
944         error_free(err);
945         monitor_read_block_device_key(mon, device, NULL, NULL);
946         return;
947     }
948     hmp_handle_error(mon, &err);
949 }
950
951 void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
952 {
953     Error *err = NULL;
954
955     qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
956                               qdict_get_int(qdict, "bps"),
957                               qdict_get_int(qdict, "bps_rd"),
958                               qdict_get_int(qdict, "bps_wr"),
959                               qdict_get_int(qdict, "iops"),
960                               qdict_get_int(qdict, "iops_rd"),
961                               qdict_get_int(qdict, "iops_wr"), &err);
962     hmp_handle_error(mon, &err);
963 }
964
965 void hmp_block_stream(Monitor *mon, const QDict *qdict)
966 {
967     Error *error = NULL;
968     const char *device = qdict_get_str(qdict, "device");
969     const char *base = qdict_get_try_str(qdict, "base");
970     int64_t speed = qdict_get_try_int(qdict, "speed", 0);
971
972     qmp_block_stream(device, base != NULL, base,
973                      qdict_haskey(qdict, "speed"), speed,
974                      BLOCKDEV_ON_ERROR_REPORT, true, &error);
975
976     hmp_handle_error(mon, &error);
977 }
978
979 void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
980 {
981     Error *error = NULL;
982     const char *device = qdict_get_str(qdict, "device");
983     int64_t value = qdict_get_int(qdict, "speed");
984
985     qmp_block_job_set_speed(device, value, &error);
986
987     hmp_handle_error(mon, &error);
988 }
989
990 void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
991 {
992     Error *error = NULL;
993     const char *device = qdict_get_str(qdict, "device");
994     bool force = qdict_get_try_bool(qdict, "force", 0);
995
996     qmp_block_job_cancel(device, true, force, &error);
997
998     hmp_handle_error(mon, &error);
999 }
1000
1001 void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1002 {
1003     Error *error = NULL;
1004     const char *device = qdict_get_str(qdict, "device");
1005
1006     qmp_block_job_pause(device, &error);
1007
1008     hmp_handle_error(mon, &error);
1009 }
1010
1011 void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1012 {
1013     Error *error = NULL;
1014     const char *device = qdict_get_str(qdict, "device");
1015
1016     qmp_block_job_resume(device, &error);
1017
1018     hmp_handle_error(mon, &error);
1019 }
1020
1021 void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1022 {
1023     Error *error = NULL;
1024     const char *device = qdict_get_str(qdict, "device");
1025
1026     qmp_block_job_complete(device, &error);
1027
1028     hmp_handle_error(mon, &error);
1029 }
1030
1031 typedef struct MigrationStatus
1032 {
1033     QEMUTimer *timer;
1034     Monitor *mon;
1035     bool is_block_migration;
1036 } MigrationStatus;
1037
1038 static void hmp_migrate_status_cb(void *opaque)
1039 {
1040     MigrationStatus *status = opaque;
1041     MigrationInfo *info;
1042
1043     info = qmp_query_migrate(NULL);
1044     if (!info->has_status || strcmp(info->status, "active") == 0) {
1045         if (info->has_disk) {
1046             int progress;
1047
1048             if (info->disk->remaining) {
1049                 progress = info->disk->transferred * 100 / info->disk->total;
1050             } else {
1051                 progress = 100;
1052             }
1053
1054             monitor_printf(status->mon, "Completed %d %%\r", progress);
1055             monitor_flush(status->mon);
1056         }
1057
1058         qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1059     } else {
1060         if (status->is_block_migration) {
1061             monitor_printf(status->mon, "\n");
1062         }
1063         monitor_resume(status->mon);
1064         qemu_del_timer(status->timer);
1065         g_free(status);
1066     }
1067
1068     qapi_free_MigrationInfo(info);
1069 }
1070
1071 void hmp_migrate(Monitor *mon, const QDict *qdict)
1072 {
1073     int detach = qdict_get_try_bool(qdict, "detach", 0);
1074     int blk = qdict_get_try_bool(qdict, "blk", 0);
1075     int inc = qdict_get_try_bool(qdict, "inc", 0);
1076     const char *uri = qdict_get_str(qdict, "uri");
1077     Error *err = NULL;
1078
1079     qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1080     if (err) {
1081         monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1082         error_free(err);
1083         return;
1084     }
1085
1086     if (!detach) {
1087         MigrationStatus *status;
1088
1089         if (monitor_suspend(mon) < 0) {
1090             monitor_printf(mon, "terminal does not allow synchronous "
1091                            "migration, continuing detached\n");
1092             return;
1093         }
1094
1095         status = g_malloc0(sizeof(*status));
1096         status->mon = mon;
1097         status->is_block_migration = blk || inc;
1098         status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1099                                           status);
1100         qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1101     }
1102 }
1103
1104 void hmp_device_del(Monitor *mon, const QDict *qdict)
1105 {
1106     const char *id = qdict_get_str(qdict, "id");
1107     Error *err = NULL;
1108
1109     qmp_device_del(id, &err);
1110     hmp_handle_error(mon, &err);
1111 }
1112
1113 void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1114 {
1115     Error *errp = NULL;
1116     int paging = qdict_get_try_bool(qdict, "paging", 0);
1117     const char *file = qdict_get_str(qdict, "filename");
1118     bool has_begin = qdict_haskey(qdict, "begin");
1119     bool has_length = qdict_haskey(qdict, "length");
1120     int64_t begin = 0;
1121     int64_t length = 0;
1122     char *prot;
1123
1124     if (has_begin) {
1125         begin = qdict_get_int(qdict, "begin");
1126     }
1127     if (has_length) {
1128         length = qdict_get_int(qdict, "length");
1129     }
1130
1131     prot = g_strconcat("file:", file, NULL);
1132
1133     qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
1134                           &errp);
1135     hmp_handle_error(mon, &errp);
1136     g_free(prot);
1137 }
1138
1139 void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1140 {
1141     Error *err = NULL;
1142     QemuOpts *opts;
1143
1144     opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1145     if (error_is_set(&err)) {
1146         goto out;
1147     }
1148
1149     netdev_add(opts, &err);
1150     if (error_is_set(&err)) {
1151         qemu_opts_del(opts);
1152     }
1153
1154 out:
1155     hmp_handle_error(mon, &err);
1156 }
1157
1158 void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1159 {
1160     const char *id = qdict_get_str(qdict, "id");
1161     Error *err = NULL;
1162
1163     qmp_netdev_del(id, &err);
1164     hmp_handle_error(mon, &err);
1165 }
1166
1167 void hmp_getfd(Monitor *mon, const QDict *qdict)
1168 {
1169     const char *fdname = qdict_get_str(qdict, "fdname");
1170     Error *errp = NULL;
1171
1172     qmp_getfd(fdname, &errp);
1173     hmp_handle_error(mon, &errp);
1174 }
1175
1176 void hmp_closefd(Monitor *mon, const QDict *qdict)
1177 {
1178     const char *fdname = qdict_get_str(qdict, "fdname");
1179     Error *errp = NULL;
1180
1181     qmp_closefd(fdname, &errp);
1182     hmp_handle_error(mon, &errp);
1183 }
1184
1185 void hmp_send_key(Monitor *mon, const QDict *qdict)
1186 {
1187     const char *keys = qdict_get_str(qdict, "keys");
1188     KeyValueList *keylist, *head = NULL, *tmp = NULL;
1189     int has_hold_time = qdict_haskey(qdict, "hold-time");
1190     int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1191     Error *err = NULL;
1192     char keyname_buf[16];
1193     char *separator;
1194     int keyname_len;
1195
1196     while (1) {
1197         separator = strchr(keys, '-');
1198         keyname_len = separator ? separator - keys : strlen(keys);
1199         pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1200
1201         /* Be compatible with old interface, convert user inputted "<" */
1202         if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1203             pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1204             keyname_len = 4;
1205         }
1206         keyname_buf[keyname_len] = 0;
1207
1208         keylist = g_malloc0(sizeof(*keylist));
1209         keylist->value = g_malloc0(sizeof(*keylist->value));
1210
1211         if (!head) {
1212             head = keylist;
1213         }
1214         if (tmp) {
1215             tmp->next = keylist;
1216         }
1217         tmp = keylist;
1218
1219         if (strstart(keyname_buf, "0x", NULL)) {
1220             char *endp;
1221             int value = strtoul(keyname_buf, &endp, 0);
1222             if (*endp != '\0') {
1223                 goto err_out;
1224             }
1225             keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1226             keylist->value->number = value;
1227         } else {
1228             int idx = index_from_key(keyname_buf);
1229             if (idx == Q_KEY_CODE_MAX) {
1230                 goto err_out;
1231             }
1232             keylist->value->kind = KEY_VALUE_KIND_QCODE;
1233             keylist->value->qcode = idx;
1234         }
1235
1236         if (!separator) {
1237             break;
1238         }
1239         keys = separator + 1;
1240     }
1241
1242     qmp_send_key(head, has_hold_time, hold_time, &err);
1243     hmp_handle_error(mon, &err);
1244
1245 out:
1246     qapi_free_KeyValueList(head);
1247     return;
1248
1249 err_out:
1250     monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1251     goto out;
1252 }
1253
1254 void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1255 {
1256     const char *filename = qdict_get_str(qdict, "filename");
1257     Error *err = NULL;
1258
1259     qmp_screendump(filename, &err);
1260     hmp_handle_error(mon, &err);
1261 }
This page took 0.094055 seconds and 4 git commands to generate.