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