blob: 41006f5eef24717b6f56f597af6419ff9228fa92 [file] [log] [blame]
Anthony Liguori48a32be2011-09-02 12:34:48 -05001/*
2 * Human Monitor Interface
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
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 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010012 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
Anthony Liguori48a32be2011-09-02 12:34:48 -050014 */
15
16#include "hmp.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020017#include "net/net.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020018#include "sysemu/char.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/option.h"
20#include "qemu/timer.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050021#include "qmp-commands.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010022#include "qemu/sockets.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010023#include "monitor/monitor.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010024#include "qapi/opts-visitor.h"
Hu Taoeb1539b2014-05-14 17:43:35 +080025#include "qapi/string-output-visitor.h"
26#include "qapi-visit.h"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010027#include "ui/console.h"
Wenchao Xiabd093a32013-06-06 12:28:00 +080028#include "block/qapi.h"
Kevin Wolf587da2c2013-06-05 14:19:41 +020029#include "qemu-io.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050030
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020031static void hmp_handle_error(Monitor *mon, Error **errp)
32{
Markus Armbruster415168e2014-05-02 13:26:34 +020033 assert(errp);
34 if (*errp) {
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020035 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
36 error_free(*errp);
37 }
38}
39
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080040void hmp_info_name(Monitor *mon, const QDict *qdict)
Anthony Liguori48a32be2011-09-02 12:34:48 -050041{
42 NameInfo *info;
43
44 info = qmp_query_name(NULL);
45 if (info->has_name) {
46 monitor_printf(mon, "%s\n", info->name);
47 }
48 qapi_free_NameInfo(info);
49}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030050
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080051void hmp_info_version(Monitor *mon, const QDict *qdict)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030052{
53 VersionInfo *info;
54
55 info = qmp_query_version(NULL);
56
57 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
58 info->qemu.major, info->qemu.minor, info->qemu.micro,
59 info->package);
60
61 qapi_free_VersionInfo(info);
62}
Luiz Capitulino292a2602011-09-12 15:10:53 -030063
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080064void hmp_info_kvm(Monitor *mon, const QDict *qdict)
Luiz Capitulino292a2602011-09-12 15:10:53 -030065{
66 KvmInfo *info;
67
68 info = qmp_query_kvm(NULL);
69 monitor_printf(mon, "kvm support: ");
70 if (info->present) {
71 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
72 } else {
73 monitor_printf(mon, "not compiled\n");
74 }
75
76 qapi_free_KvmInfo(info);
77}
78
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080079void hmp_info_status(Monitor *mon, const QDict *qdict)
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -030080{
81 StatusInfo *info;
82
83 info = qmp_query_status(NULL);
84
85 monitor_printf(mon, "VM status: %s%s",
86 info->running ? "running" : "paused",
87 info->singlestep ? " (single step mode)" : "");
88
89 if (!info->running && info->status != RUN_STATE_PAUSED) {
90 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
91 }
92
93 monitor_printf(mon, "\n");
94
95 qapi_free_StatusInfo(info);
96}
97
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080098void hmp_info_uuid(Monitor *mon, const QDict *qdict)
Luiz Capitulinoefab7672011-09-13 17:16:25 -030099{
100 UuidInfo *info;
101
102 info = qmp_query_uuid(NULL);
103 monitor_printf(mon, "%s\n", info->UUID);
104 qapi_free_UuidInfo(info);
105}
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300106
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800107void hmp_info_chardev(Monitor *mon, const QDict *qdict)
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300108{
109 ChardevInfoList *char_info, *info;
110
111 char_info = qmp_query_chardev(NULL);
112 for (info = char_info; info; info = info->next) {
113 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
114 info->value->filename);
115 }
116
117 qapi_free_ChardevInfoList(char_info);
118}
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300119
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800120void hmp_info_mice(Monitor *mon, const QDict *qdict)
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300121{
122 MouseInfoList *mice_list, *mouse;
123
124 mice_list = qmp_query_mice(NULL);
125 if (!mice_list) {
126 monitor_printf(mon, "No mouse devices connected\n");
127 return;
128 }
129
130 for (mouse = mice_list; mouse; mouse = mouse->next) {
131 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
132 mouse->value->current ? '*' : ' ',
133 mouse->value->index, mouse->value->name,
134 mouse->value->absolute ? " (absolute)" : "");
135 }
136
137 qapi_free_MouseInfoList(mice_list);
138}
139
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800140void hmp_info_migrate(Monitor *mon, const QDict *qdict)
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300141{
142 MigrationInfo *info;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300143 MigrationCapabilityStatusList *caps, *cap;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300144
145 info = qmp_query_migrate(NULL);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300146 caps = qmp_query_migrate_capabilities(NULL);
147
148 /* do not display parameters during setup */
149 if (info->has_status && caps) {
150 monitor_printf(mon, "capabilities: ");
151 for (cap = caps; cap; cap = cap->next) {
152 monitor_printf(mon, "%s: %s ",
153 MigrationCapability_lookup[cap->value->capability],
154 cap->value->state ? "on" : "off");
155 }
156 monitor_printf(mon, "\n");
157 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300158
159 if (info->has_status) {
160 monitor_printf(mon, "Migration status: %s\n", info->status);
Juan Quintela7aa939a2012-08-18 13:17:10 +0200161 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
162 info->total_time);
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200163 if (info->has_expected_downtime) {
164 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
165 info->expected_downtime);
166 }
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200167 if (info->has_downtime) {
168 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
169 info->downtime);
170 }
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400171 if (info->has_setup_time) {
172 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
173 info->setup_time);
174 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300175 }
176
177 if (info->has_ram) {
178 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
179 info->ram->transferred >> 10);
Michael R. Hines7e114f82013-06-25 21:35:30 -0400180 monitor_printf(mon, "throughput: %0.2f mbps\n",
181 info->ram->mbps);
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300182 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
183 info->ram->remaining >> 10);
184 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
185 info->ram->total >> 10);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300186 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
187 info->ram->duplicate);
Peter Lievenf1c72792013-03-26 10:58:37 +0100188 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
189 info->ram->skipped);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300190 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
191 info->ram->normal);
192 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
193 info->ram->normal_bytes >> 10);
ChenLiang58570ed2014-04-04 17:57:55 +0800194 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
195 info->ram->dirty_sync_count);
Juan Quintela8d017192012-08-13 12:31:25 +0200196 if (info->ram->dirty_pages_rate) {
197 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
198 info->ram->dirty_pages_rate);
199 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300200 }
201
202 if (info->has_disk) {
203 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
204 info->disk->transferred >> 10);
205 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
206 info->disk->remaining >> 10);
207 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
208 info->disk->total >> 10);
209 }
210
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300211 if (info->has_xbzrle_cache) {
212 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
213 info->xbzrle_cache->cache_size);
214 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
215 info->xbzrle_cache->bytes >> 10);
216 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
217 info->xbzrle_cache->pages);
218 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
219 info->xbzrle_cache->cache_miss);
ChenLiang8bc39232014-04-04 17:57:56 +0800220 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
221 info->xbzrle_cache->cache_miss_rate);
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300222 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
223 info->xbzrle_cache->overflow);
224 }
225
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300226 qapi_free_MigrationInfo(info);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300227 qapi_free_MigrationCapabilityStatusList(caps);
228}
229
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800230void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300231{
232 MigrationCapabilityStatusList *caps, *cap;
233
234 caps = qmp_query_migrate_capabilities(NULL);
235
236 if (caps) {
237 monitor_printf(mon, "capabilities: ");
238 for (cap = caps; cap; cap = cap->next) {
239 monitor_printf(mon, "%s: %s ",
240 MigrationCapability_lookup[cap->value->capability],
241 cap->value->state ? "on" : "off");
242 }
243 monitor_printf(mon, "\n");
244 }
245
246 qapi_free_MigrationCapabilityStatusList(caps);
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300247}
248
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800249void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300250{
251 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
252 qmp_query_migrate_cache_size(NULL) >> 10);
253}
254
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800255void hmp_info_cpus(Monitor *mon, const QDict *qdict)
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300256{
257 CpuInfoList *cpu_list, *cpu;
258
259 cpu_list = qmp_query_cpus(NULL);
260
261 for (cpu = cpu_list; cpu; cpu = cpu->next) {
262 int active = ' ';
263
264 if (cpu->value->CPU == monitor_get_cpu_index()) {
265 active = '*';
266 }
267
Aurelien Jarno852bef02012-10-19 23:19:19 +0200268 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300269
270 if (cpu->value->has_pc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200271 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300272 }
273 if (cpu->value->has_nip) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200274 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300275 }
276 if (cpu->value->has_npc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200277 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300278 }
279 if (cpu->value->has_PC) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200280 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300281 }
282
283 if (cpu->value->halted) {
284 monitor_printf(mon, " (halted)");
285 }
286
287 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
288 }
289
290 qapi_free_CpuInfoList(cpu_list);
291}
292
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800293void hmp_info_block(Monitor *mon, const QDict *qdict)
Luiz Capitulinob2023812011-09-21 17:16:47 -0300294{
295 BlockInfoList *block_list, *info;
Wenchao Xiabd093a32013-06-06 12:28:00 +0800296 ImageInfo *image_info;
Wenchao Xiae73fe2b2013-06-06 12:28:01 +0800297 const char *device = qdict_get_try_str(qdict, "device");
298 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300299
300 block_list = qmp_query_block(NULL);
301
302 for (info = block_list; info; info = info->next) {
Wenchao Xiae73fe2b2013-06-06 12:28:01 +0800303 if (device && strcmp(device, info->value->device)) {
304 continue;
305 }
Luiz Capitulinob2023812011-09-21 17:16:47 -0300306
Kevin Wolffbe2e262013-06-19 16:10:55 +0200307 if (info != block_list) {
308 monitor_printf(mon, "\n");
Luiz Capitulinob2023812011-09-21 17:16:47 -0300309 }
310
Kevin Wolffbe2e262013-06-19 16:10:55 +0200311 monitor_printf(mon, "%s", info->value->device);
312 if (info->value->has_inserted) {
313 monitor_printf(mon, ": %s (%s%s%s)\n",
314 info->value->inserted->file,
315 info->value->inserted->drv,
316 info->value->inserted->ro ? ", read-only" : "",
317 info->value->inserted->encrypted ? ", encrypted" : "");
318 } else {
319 monitor_printf(mon, ": [not inserted]\n");
320 }
321
322 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
323 monitor_printf(mon, " I/O status: %s\n",
Luiz Capitulinob2023812011-09-21 17:16:47 -0300324 BlockDeviceIoStatus_lookup[info->value->io_status]);
325 }
326
Kevin Wolffbe2e262013-06-19 16:10:55 +0200327 if (info->value->removable) {
328 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
329 info->value->locked ? "" : "not ",
330 info->value->tray_open ? "open" : "closed");
331 }
Luiz Capitulinob2023812011-09-21 17:16:47 -0300332
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800333
Kevin Wolffbe2e262013-06-19 16:10:55 +0200334 if (!info->value->has_inserted) {
335 continue;
336 }
337
338 if (info->value->inserted->has_backing_file) {
339 monitor_printf(mon,
340 " Backing file: %s "
341 "(chain depth: %" PRId64 ")\n",
342 info->value->inserted->backing_file,
343 info->value->inserted->backing_file_depth);
344 }
345
Peter Lieven465bee12014-05-18 00:58:19 +0200346 if (info->value->inserted->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF) {
347 monitor_printf(mon, " Detect zeroes: %s\n",
348 BlockdevDetectZeroesOptions_lookup[info->value->inserted->detect_zeroes]);
349 }
350
Kevin Wolffbe2e262013-06-19 16:10:55 +0200351 if (info->value->inserted->bps
352 || info->value->inserted->bps_rd
353 || info->value->inserted->bps_wr
354 || info->value->inserted->iops
355 || info->value->inserted->iops_rd
356 || info->value->inserted->iops_wr)
357 {
358 monitor_printf(mon, " I/O throttling: bps=%" PRId64
359 " bps_rd=%" PRId64 " bps_wr=%" PRId64
Benoît Canet3e9fab62013-09-02 14:14:40 +0200360 " bps_max=%" PRId64
361 " bps_rd_max=%" PRId64
362 " bps_wr_max=%" PRId64
Kevin Wolffbe2e262013-06-19 16:10:55 +0200363 " iops=%" PRId64 " iops_rd=%" PRId64
Benoît Canet3e9fab62013-09-02 14:14:40 +0200364 " iops_wr=%" PRId64
365 " iops_max=%" PRId64
366 " iops_rd_max=%" PRId64
Benoît Canet2024c1d2013-09-02 14:14:41 +0200367 " iops_wr_max=%" PRId64
368 " iops_size=%" PRId64 "\n",
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800369 info->value->inserted->bps,
370 info->value->inserted->bps_rd,
371 info->value->inserted->bps_wr,
Benoît Canet3e9fab62013-09-02 14:14:40 +0200372 info->value->inserted->bps_max,
373 info->value->inserted->bps_rd_max,
374 info->value->inserted->bps_wr_max,
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800375 info->value->inserted->iops,
376 info->value->inserted->iops_rd,
Benoît Canet3e9fab62013-09-02 14:14:40 +0200377 info->value->inserted->iops_wr,
378 info->value->inserted->iops_max,
379 info->value->inserted->iops_rd_max,
Benoît Canet2024c1d2013-09-02 14:14:41 +0200380 info->value->inserted->iops_wr_max,
381 info->value->inserted->iops_size);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300382 }
383
Kevin Wolffbe2e262013-06-19 16:10:55 +0200384 if (verbose) {
385 monitor_printf(mon, "\nImages:\n");
386 image_info = info->value->inserted->image;
387 while (1) {
388 bdrv_image_info_dump((fprintf_function)monitor_printf,
389 mon, image_info);
390 if (image_info->has_backing_image) {
391 image_info = image_info->backing_image;
392 } else {
393 break;
394 }
395 }
396 }
Luiz Capitulinob2023812011-09-21 17:16:47 -0300397 }
398
399 qapi_free_BlockInfoList(block_list);
400}
401
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800402void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
Luiz Capitulinof11f57e2011-09-22 15:56:36 -0300403{
404 BlockStatsList *stats_list, *stats;
405
406 stats_list = qmp_query_blockstats(NULL);
407
408 for (stats = stats_list; stats; stats = stats->next) {
409 if (!stats->value->has_device) {
410 continue;
411 }
412
413 monitor_printf(mon, "%s:", stats->value->device);
414 monitor_printf(mon, " rd_bytes=%" PRId64
415 " wr_bytes=%" PRId64
416 " rd_operations=%" PRId64
417 " wr_operations=%" PRId64
418 " flush_operations=%" PRId64
419 " wr_total_time_ns=%" PRId64
420 " rd_total_time_ns=%" PRId64
421 " flush_total_time_ns=%" PRId64
422 "\n",
423 stats->value->stats->rd_bytes,
424 stats->value->stats->wr_bytes,
425 stats->value->stats->rd_operations,
426 stats->value->stats->wr_operations,
427 stats->value->stats->flush_operations,
428 stats->value->stats->wr_total_time_ns,
429 stats->value->stats->rd_total_time_ns,
430 stats->value->stats->flush_total_time_ns);
431 }
432
433 qapi_free_BlockStatsList(stats_list);
434}
435
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800436void hmp_info_vnc(Monitor *mon, const QDict *qdict)
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200437{
438 VncInfo *info;
439 Error *err = NULL;
440 VncClientInfoList *client;
441
442 info = qmp_query_vnc(&err);
443 if (err) {
444 monitor_printf(mon, "%s\n", error_get_pretty(err));
445 error_free(err);
446 return;
447 }
448
449 if (!info->enabled) {
450 monitor_printf(mon, "Server: disabled\n");
451 goto out;
452 }
453
454 monitor_printf(mon, "Server:\n");
455 if (info->has_host && info->has_service) {
456 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
457 }
458 if (info->has_auth) {
459 monitor_printf(mon, " auth: %s\n", info->auth);
460 }
461
462 if (!info->has_clients || info->clients == NULL) {
463 monitor_printf(mon, "Client: none\n");
464 } else {
465 for (client = info->clients; client; client = client->next) {
466 monitor_printf(mon, "Client:\n");
467 monitor_printf(mon, " address: %s:%s\n",
468 client->value->host, client->value->service);
469 monitor_printf(mon, " x509_dname: %s\n",
470 client->value->x509_dname ?
471 client->value->x509_dname : "none");
472 monitor_printf(mon, " username: %s\n",
473 client->value->has_sasl_username ?
474 client->value->sasl_username : "none");
475 }
476 }
477
478out:
479 qapi_free_VncInfo(info);
480}
481
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800482void hmp_info_spice(Monitor *mon, const QDict *qdict)
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200483{
484 SpiceChannelList *chan;
485 SpiceInfo *info;
486
487 info = qmp_query_spice(NULL);
488
489 if (!info->enabled) {
490 monitor_printf(mon, "Server: disabled\n");
491 goto out;
492 }
493
494 monitor_printf(mon, "Server:\n");
495 if (info->has_port) {
496 monitor_printf(mon, " address: %s:%" PRId64 "\n",
497 info->host, info->port);
498 }
499 if (info->has_tls_port) {
500 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
501 info->host, info->tls_port);
502 }
Yonit Halperin61c4efe2012-08-21 11:51:58 +0300503 monitor_printf(mon, " migrated: %s\n",
504 info->migrated ? "true" : "false");
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200505 monitor_printf(mon, " auth: %s\n", info->auth);
506 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
Alon Levy4efee022012-03-29 23:23:14 +0200507 monitor_printf(mon, " mouse-mode: %s\n",
508 SpiceQueryMouseMode_lookup[info->mouse_mode]);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200509
510 if (!info->has_channels || info->channels == NULL) {
511 monitor_printf(mon, "Channels: none\n");
512 } else {
513 for (chan = info->channels; chan; chan = chan->next) {
514 monitor_printf(mon, "Channel:\n");
515 monitor_printf(mon, " address: %s:%s%s\n",
516 chan->value->host, chan->value->port,
517 chan->value->tls ? " [tls]" : "");
518 monitor_printf(mon, " session: %" PRId64 "\n",
519 chan->value->connection_id);
520 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
521 chan->value->channel_type, chan->value->channel_id);
522 }
523 }
524
525out:
526 qapi_free_SpiceInfo(info);
527}
528
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800529void hmp_info_balloon(Monitor *mon, const QDict *qdict)
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200530{
531 BalloonInfo *info;
532 Error *err = NULL;
533
534 info = qmp_query_balloon(&err);
535 if (err) {
536 monitor_printf(mon, "%s\n", error_get_pretty(err));
537 error_free(err);
538 return;
539 }
540
Luiz Capitulino01ceb972012-12-03 15:56:41 -0200541 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200542
543 qapi_free_BalloonInfo(info);
544}
545
Luiz Capitulino79627472011-10-21 14:15:33 -0200546static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
547{
548 PciMemoryRegionList *region;
549
550 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
551 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
552 dev->slot, dev->function);
553 monitor_printf(mon, " ");
554
555 if (dev->class_info.has_desc) {
556 monitor_printf(mon, "%s", dev->class_info.desc);
557 } else {
Tomoki Sekiyama6f880092013-08-07 11:39:43 -0400558 monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
Luiz Capitulino79627472011-10-21 14:15:33 -0200559 }
560
561 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
562 dev->id.vendor, dev->id.device);
563
564 if (dev->has_irq) {
565 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
566 }
567
568 if (dev->has_pci_bridge) {
569 monitor_printf(mon, " BUS %" PRId64 ".\n",
570 dev->pci_bridge->bus.number);
571 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
572 dev->pci_bridge->bus.secondary);
573 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
574 dev->pci_bridge->bus.subordinate);
575
576 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
577 dev->pci_bridge->bus.io_range->base,
578 dev->pci_bridge->bus.io_range->limit);
579
580 monitor_printf(mon,
581 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
582 dev->pci_bridge->bus.memory_range->base,
583 dev->pci_bridge->bus.memory_range->limit);
584
585 monitor_printf(mon, " prefetchable memory range "
586 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
587 dev->pci_bridge->bus.prefetchable_range->base,
588 dev->pci_bridge->bus.prefetchable_range->limit);
589 }
590
591 for (region = dev->regions; region; region = region->next) {
592 uint64_t addr, size;
593
594 addr = region->value->address;
595 size = region->value->size;
596
597 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
598
599 if (!strcmp(region->value->type, "io")) {
600 monitor_printf(mon, "I/O at 0x%04" PRIx64
601 " [0x%04" PRIx64 "].\n",
602 addr, addr + size - 1);
603 } else {
604 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
605 " [0x%08" PRIx64 "].\n",
606 region->value->mem_type_64 ? 64 : 32,
607 region->value->prefetch ? " prefetchable" : "",
608 addr, addr + size - 1);
609 }
610 }
611
612 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
613
614 if (dev->has_pci_bridge) {
615 if (dev->pci_bridge->has_devices) {
616 PciDeviceInfoList *cdev;
617 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
618 hmp_info_pci_device(mon, cdev->value);
619 }
620 }
621 }
622}
623
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800624void hmp_info_pci(Monitor *mon, const QDict *qdict)
Luiz Capitulino79627472011-10-21 14:15:33 -0200625{
Stefan Bergerf46cee32012-01-11 10:51:52 -0500626 PciInfoList *info_list, *info;
Luiz Capitulino79627472011-10-21 14:15:33 -0200627 Error *err = NULL;
628
Stefan Bergerf46cee32012-01-11 10:51:52 -0500629 info_list = qmp_query_pci(&err);
Luiz Capitulino79627472011-10-21 14:15:33 -0200630 if (err) {
631 monitor_printf(mon, "PCI devices not supported\n");
632 error_free(err);
633 return;
634 }
635
Stefan Bergerf46cee32012-01-11 10:51:52 -0500636 for (info = info_list; info; info = info->next) {
Luiz Capitulino79627472011-10-21 14:15:33 -0200637 PciDeviceInfoList *dev;
638
639 for (dev = info->value->devices; dev; dev = dev->next) {
640 hmp_info_pci_device(mon, dev->value);
641 }
642 }
643
Stefan Bergerf46cee32012-01-11 10:51:52 -0500644 qapi_free_PciInfoList(info_list);
Luiz Capitulino79627472011-10-21 14:15:33 -0200645}
646
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800647void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +0000648{
649 BlockJobInfoList *list;
650 Error *err = NULL;
651
652 list = qmp_query_block_jobs(&err);
653 assert(!err);
654
655 if (!list) {
656 monitor_printf(mon, "No active jobs\n");
657 return;
658 }
659
660 while (list) {
661 if (strcmp(list->value->type, "stream") == 0) {
662 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
663 " of %" PRId64 " bytes, speed limit %" PRId64
664 " bytes/s\n",
665 list->value->device,
666 list->value->offset,
667 list->value->len,
668 list->value->speed);
669 } else {
670 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
671 " of %" PRId64 " bytes, speed limit %" PRId64
672 " bytes/s\n",
673 list->value->type,
674 list->value->device,
675 list->value->offset,
676 list->value->len,
677 list->value->speed);
678 }
679 list = list->next;
680 }
681}
682
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500683void hmp_info_tpm(Monitor *mon, const QDict *qdict)
684{
685 TPMInfoList *info_list, *info;
686 Error *err = NULL;
687 unsigned int c = 0;
688 TPMPassthroughOptions *tpo;
689
690 info_list = qmp_query_tpm(&err);
691 if (err) {
692 monitor_printf(mon, "TPM device not supported\n");
693 error_free(err);
694 return;
695 }
696
697 if (info_list) {
698 monitor_printf(mon, "TPM device:\n");
699 }
700
701 for (info = info_list; info; info = info->next) {
702 TPMInfo *ti = info->value;
703 monitor_printf(mon, " tpm%d: model=%s\n",
704 c, TpmModel_lookup[ti->model]);
705
706 monitor_printf(mon, " \\ %s: type=%s",
Corey Bryant88ca7bc2013-03-20 12:34:48 -0400707 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500708
Corey Bryant88ca7bc2013-03-20 12:34:48 -0400709 switch (ti->options->kind) {
710 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
711 tpo = ti->options->passthrough;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500712 monitor_printf(mon, "%s%s%s%s",
713 tpo->has_path ? ",path=" : "",
714 tpo->has_path ? tpo->path : "",
715 tpo->has_cancel_path ? ",cancel-path=" : "",
716 tpo->has_cancel_path ? tpo->cancel_path : "");
717 break;
718 case TPM_TYPE_OPTIONS_KIND_MAX:
719 break;
720 }
721 monitor_printf(mon, "\n");
722 c++;
723 }
724 qapi_free_TPMInfoList(info_list);
725}
726
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300727void hmp_quit(Monitor *mon, const QDict *qdict)
728{
729 monitor_suspend(mon);
730 qmp_quit(NULL);
731}
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300732
733void hmp_stop(Monitor *mon, const QDict *qdict)
734{
735 qmp_stop(NULL);
736}
Luiz Capitulino38d22652011-09-15 14:41:46 -0300737
738void hmp_system_reset(Monitor *mon, const QDict *qdict)
739{
740 qmp_system_reset(NULL);
741}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300742
743void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
744{
745 qmp_system_powerdown(NULL);
746}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300747
748void hmp_cpu(Monitor *mon, const QDict *qdict)
749{
750 int64_t cpu_index;
751
752 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
753 use it are converted to the QAPI */
754 cpu_index = qdict_get_int(qdict, "index");
755 if (monitor_set_cpu(cpu_index) < 0) {
756 monitor_printf(mon, "invalid CPU index\n");
757 }
758}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200759
760void hmp_memsave(Monitor *mon, const QDict *qdict)
761{
762 uint32_t size = qdict_get_int(qdict, "size");
763 const char *filename = qdict_get_str(qdict, "filename");
764 uint64_t addr = qdict_get_int(qdict, "val");
Markus Armbrustere940f542014-05-02 13:26:29 +0200765 Error *err = NULL;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200766
Markus Armbrustere940f542014-05-02 13:26:29 +0200767 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
768 hmp_handle_error(mon, &err);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200769}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200770
771void hmp_pmemsave(Monitor *mon, const QDict *qdict)
772{
773 uint32_t size = qdict_get_int(qdict, "size");
774 const char *filename = qdict_get_str(qdict, "filename");
775 uint64_t addr = qdict_get_int(qdict, "val");
Markus Armbrustere940f542014-05-02 13:26:29 +0200776 Error *err = NULL;
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200777
Markus Armbrustere940f542014-05-02 13:26:29 +0200778 qmp_pmemsave(addr, size, filename, &err);
779 hmp_handle_error(mon, &err);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200780}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200781
Markus Armbruster3949e592013-02-06 21:27:24 +0100782void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
Lei Li1f590cf2013-01-25 00:03:20 +0800783{
Lei Li1f590cf2013-01-25 00:03:20 +0800784 const char *chardev = qdict_get_str(qdict, "device");
785 const char *data = qdict_get_str(qdict, "data");
Markus Armbrustere940f542014-05-02 13:26:29 +0200786 Error *err = NULL;
Lei Li1f590cf2013-01-25 00:03:20 +0800787
Markus Armbrustere940f542014-05-02 13:26:29 +0200788 qmp_ringbuf_write(chardev, data, false, 0, &err);
Lei Li1f590cf2013-01-25 00:03:20 +0800789
Markus Armbrustere940f542014-05-02 13:26:29 +0200790 hmp_handle_error(mon, &err);
Lei Li1f590cf2013-01-25 00:03:20 +0800791}
792
Markus Armbruster3949e592013-02-06 21:27:24 +0100793void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
Lei Li49b6d722013-01-25 00:03:21 +0800794{
795 uint32_t size = qdict_get_int(qdict, "size");
796 const char *chardev = qdict_get_str(qdict, "device");
Markus Armbruster3ab651f2013-02-06 21:27:15 +0100797 char *data;
Markus Armbrustere940f542014-05-02 13:26:29 +0200798 Error *err = NULL;
Markus Armbruster543f3412013-02-06 21:27:26 +0100799 int i;
Lei Li49b6d722013-01-25 00:03:21 +0800800
Markus Armbrustere940f542014-05-02 13:26:29 +0200801 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
802 if (err) {
803 monitor_printf(mon, "%s\n", error_get_pretty(err));
804 error_free(err);
Lei Li49b6d722013-01-25 00:03:21 +0800805 return;
806 }
807
Markus Armbruster543f3412013-02-06 21:27:26 +0100808 for (i = 0; data[i]; i++) {
809 unsigned char ch = data[i];
810
811 if (ch == '\\') {
812 monitor_printf(mon, "\\\\");
813 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
814 monitor_printf(mon, "\\u%04X", ch);
815 } else {
816 monitor_printf(mon, "%c", ch);
817 }
818
819 }
820 monitor_printf(mon, "\n");
Markus Armbruster3ab651f2013-02-06 21:27:15 +0100821 g_free(data);
Lei Li49b6d722013-01-25 00:03:21 +0800822}
823
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200824static void hmp_cont_cb(void *opaque, int err)
825{
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200826 if (!err) {
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300827 qmp_cont(NULL);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200828 }
829}
830
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300831static bool key_is_missing(const BlockInfo *bdev)
832{
833 return (bdev->inserted && bdev->inserted->encryption_key_missing);
834}
835
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200836void hmp_cont(Monitor *mon, const QDict *qdict)
837{
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300838 BlockInfoList *bdev_list, *bdev;
Markus Armbrustere940f542014-05-02 13:26:29 +0200839 Error *err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200840
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300841 bdev_list = qmp_query_block(NULL);
842 for (bdev = bdev_list; bdev; bdev = bdev->next) {
843 if (key_is_missing(bdev->value)) {
844 monitor_read_block_device_key(mon, bdev->value->device,
845 hmp_cont_cb, NULL);
846 goto out;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200847 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200848 }
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300849
Markus Armbrustere940f542014-05-02 13:26:29 +0200850 qmp_cont(&err);
851 hmp_handle_error(mon, &err);
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300852
853out:
854 qapi_free_BlockInfoList(bdev_list);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200855}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200856
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100857void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
858{
859 qmp_system_wakeup(NULL);
860}
861
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200862void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
863{
Markus Armbrustere940f542014-05-02 13:26:29 +0200864 Error *err = NULL;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200865
Markus Armbrustere940f542014-05-02 13:26:29 +0200866 qmp_inject_nmi(&err);
867 hmp_handle_error(mon, &err);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200868}
Luiz Capitulino4b371562011-11-23 13:11:55 -0200869
870void hmp_set_link(Monitor *mon, const QDict *qdict)
871{
872 const char *name = qdict_get_str(qdict, "name");
873 int up = qdict_get_bool(qdict, "up");
Markus Armbrustere940f542014-05-02 13:26:29 +0200874 Error *err = NULL;
Luiz Capitulino4b371562011-11-23 13:11:55 -0200875
Markus Armbrustere940f542014-05-02 13:26:29 +0200876 qmp_set_link(name, up, &err);
877 hmp_handle_error(mon, &err);
Luiz Capitulino4b371562011-11-23 13:11:55 -0200878}
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200879
880void hmp_block_passwd(Monitor *mon, const QDict *qdict)
881{
882 const char *device = qdict_get_str(qdict, "device");
883 const char *password = qdict_get_str(qdict, "password");
Markus Armbrustere940f542014-05-02 13:26:29 +0200884 Error *err = NULL;
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200885
Markus Armbrustere940f542014-05-02 13:26:29 +0200886 qmp_block_passwd(true, device, false, NULL, password, &err);
887 hmp_handle_error(mon, &err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200888}
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200889
890void hmp_balloon(Monitor *mon, const QDict *qdict)
891{
892 int64_t value = qdict_get_int(qdict, "value");
Markus Armbrustere940f542014-05-02 13:26:29 +0200893 Error *err = NULL;
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200894
Markus Armbrustere940f542014-05-02 13:26:29 +0200895 qmp_balloon(value, &err);
896 if (err) {
897 monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
898 error_free(err);
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200899 }
900}
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200901
902void hmp_block_resize(Monitor *mon, const QDict *qdict)
903{
904 const char *device = qdict_get_str(qdict, "device");
905 int64_t size = qdict_get_int(qdict, "size");
Markus Armbrustere940f542014-05-02 13:26:29 +0200906 Error *err = NULL;
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200907
Markus Armbrustere940f542014-05-02 13:26:29 +0200908 qmp_block_resize(true, device, false, NULL, size, &err);
909 hmp_handle_error(mon, &err);
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200910}
Luiz Capitulino6106e242011-11-25 16:15:19 -0200911
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200912void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
913{
914 const char *device = qdict_get_str(qdict, "device");
915 const char *filename = qdict_get_str(qdict, "target");
916 const char *format = qdict_get_try_str(qdict, "format");
917 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
918 int full = qdict_get_try_bool(qdict, "full", 0);
919 enum NewImageMode mode;
Markus Armbrustere940f542014-05-02 13:26:29 +0200920 Error *err = NULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200921
922 if (!filename) {
Markus Armbrustere940f542014-05-02 13:26:29 +0200923 error_set(&err, QERR_MISSING_PARAMETER, "target");
924 hmp_handle_error(mon, &err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200925 return;
926 }
927
928 if (reuse) {
929 mode = NEW_IMAGE_MODE_EXISTING;
930 } else {
931 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
932 }
933
934 qmp_drive_mirror(device, filename, !!format, format,
935 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
Paolo Bonzini08e4ed62013-01-22 09:03:13 +0100936 true, mode, false, 0, false, 0, false, 0,
Markus Armbrustere940f542014-05-02 13:26:29 +0200937 false, 0, false, 0, &err);
938 hmp_handle_error(mon, &err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200939}
940
Stefan Hajnoczide909302013-06-26 14:11:58 +0200941void hmp_drive_backup(Monitor *mon, const QDict *qdict)
942{
943 const char *device = qdict_get_str(qdict, "device");
944 const char *filename = qdict_get_str(qdict, "target");
945 const char *format = qdict_get_try_str(qdict, "format");
946 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
947 int full = qdict_get_try_bool(qdict, "full", 0);
948 enum NewImageMode mode;
Markus Armbrustere940f542014-05-02 13:26:29 +0200949 Error *err = NULL;
Stefan Hajnoczide909302013-06-26 14:11:58 +0200950
951 if (!filename) {
Markus Armbrustere940f542014-05-02 13:26:29 +0200952 error_set(&err, QERR_MISSING_PARAMETER, "target");
953 hmp_handle_error(mon, &err);
Stefan Hajnoczide909302013-06-26 14:11:58 +0200954 return;
955 }
956
957 if (reuse) {
958 mode = NEW_IMAGE_MODE_EXISTING;
959 } else {
960 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
961 }
962
963 qmp_drive_backup(device, filename, !!format, format,
964 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
Markus Armbrustere940f542014-05-02 13:26:29 +0200965 true, mode, false, 0, false, 0, false, 0, &err);
966 hmp_handle_error(mon, &err);
Stefan Hajnoczide909302013-06-26 14:11:58 +0200967}
968
Luiz Capitulino6106e242011-11-25 16:15:19 -0200969void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
970{
971 const char *device = qdict_get_str(qdict, "device");
972 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
973 const char *format = qdict_get_try_str(qdict, "format");
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100974 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
975 enum NewImageMode mode;
Markus Armbrustere940f542014-05-02 13:26:29 +0200976 Error *err = NULL;
Luiz Capitulino6106e242011-11-25 16:15:19 -0200977
978 if (!filename) {
979 /* In the future, if 'snapshot-file' is not specified, the snapshot
980 will be taken internally. Today it's actually required. */
Markus Armbrustere940f542014-05-02 13:26:29 +0200981 error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
982 hmp_handle_error(mon, &err);
Luiz Capitulino6106e242011-11-25 16:15:19 -0200983 return;
984 }
985
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100986 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Benoît Canet0901f672014-01-23 21:31:38 +0100987 qmp_blockdev_snapshot_sync(true, device, false, NULL,
988 filename, false, NULL,
989 !!format, format,
Markus Armbrustere940f542014-05-02 13:26:29 +0200990 true, mode, &err);
991 hmp_handle_error(mon, &err);
Luiz Capitulino6106e242011-11-25 16:15:19 -0200992}
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200993
Wenchao Xia775ca882013-09-11 14:04:37 +0800994void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
995{
996 const char *device = qdict_get_str(qdict, "device");
997 const char *name = qdict_get_str(qdict, "name");
Markus Armbrustere940f542014-05-02 13:26:29 +0200998 Error *err = NULL;
Wenchao Xia775ca882013-09-11 14:04:37 +0800999
Markus Armbrustere940f542014-05-02 13:26:29 +02001000 qmp_blockdev_snapshot_internal_sync(device, name, &err);
1001 hmp_handle_error(mon, &err);
Wenchao Xia775ca882013-09-11 14:04:37 +08001002}
1003
Wenchao Xia7a4ed2e2013-09-11 14:04:38 +08001004void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
1005{
1006 const char *device = qdict_get_str(qdict, "device");
1007 const char *name = qdict_get_str(qdict, "name");
1008 const char *id = qdict_get_try_str(qdict, "id");
Markus Armbrustere940f542014-05-02 13:26:29 +02001009 Error *err = NULL;
Wenchao Xia7a4ed2e2013-09-11 14:04:38 +08001010
1011 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
Markus Armbrustere940f542014-05-02 13:26:29 +02001012 true, name, &err);
1013 hmp_handle_error(mon, &err);
Wenchao Xia7a4ed2e2013-09-11 14:04:38 +08001014}
1015
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001016void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1017{
1018 qmp_migrate_cancel(NULL);
1019}
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001020
1021void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1022{
1023 double value = qdict_get_double(qdict, "value");
1024 qmp_migrate_set_downtime(value, NULL);
1025}
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001026
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001027void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1028{
1029 int64_t value = qdict_get_int(qdict, "value");
1030 Error *err = NULL;
1031
1032 qmp_migrate_set_cache_size(value, &err);
1033 if (err) {
1034 monitor_printf(mon, "%s\n", error_get_pretty(err));
1035 error_free(err);
1036 return;
1037 }
1038}
1039
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001040void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1041{
1042 int64_t value = qdict_get_int(qdict, "value");
1043 qmp_migrate_set_speed(value, NULL);
1044}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -02001045
Orit Wasserman00458432012-08-06 21:42:48 +03001046void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1047{
1048 const char *cap = qdict_get_str(qdict, "capability");
1049 bool state = qdict_get_bool(qdict, "state");
1050 Error *err = NULL;
1051 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1052 int i;
1053
1054 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1055 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1056 caps->value = g_malloc0(sizeof(*caps->value));
1057 caps->value->capability = i;
1058 caps->value->state = state;
1059 caps->next = NULL;
1060 qmp_migrate_set_capabilities(caps, &err);
1061 break;
1062 }
1063 }
1064
1065 if (i == MIGRATION_CAPABILITY_MAX) {
1066 error_set(&err, QERR_INVALID_PARAMETER, cap);
1067 }
1068
1069 qapi_free_MigrationCapabilityStatusList(caps);
1070
1071 if (err) {
Orit Wassermana31ca012013-01-31 09:12:19 +02001072 monitor_printf(mon, "migrate_set_capability: %s\n",
Orit Wasserman00458432012-08-06 21:42:48 +03001073 error_get_pretty(err));
1074 error_free(err);
1075 }
1076}
1077
Luiz Capitulinofbf796f2011-12-07 11:17:51 -02001078void hmp_set_password(Monitor *mon, const QDict *qdict)
1079{
1080 const char *protocol = qdict_get_str(qdict, "protocol");
1081 const char *password = qdict_get_str(qdict, "password");
1082 const char *connected = qdict_get_try_str(qdict, "connected");
1083 Error *err = NULL;
1084
1085 qmp_set_password(protocol, password, !!connected, connected, &err);
1086 hmp_handle_error(mon, &err);
1087}
Luiz Capitulino9ad53722011-12-07 11:47:57 -02001088
1089void hmp_expire_password(Monitor *mon, const QDict *qdict)
1090{
1091 const char *protocol = qdict_get_str(qdict, "protocol");
1092 const char *whenstr = qdict_get_str(qdict, "time");
1093 Error *err = NULL;
1094
1095 qmp_expire_password(protocol, whenstr, &err);
1096 hmp_handle_error(mon, &err);
1097}
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02001098
1099void hmp_eject(Monitor *mon, const QDict *qdict)
1100{
1101 int force = qdict_get_try_bool(qdict, "force", 0);
1102 const char *device = qdict_get_str(qdict, "device");
1103 Error *err = NULL;
1104
1105 qmp_eject(device, true, force, &err);
1106 hmp_handle_error(mon, &err);
1107}
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001108
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01001109static void hmp_change_read_arg(void *opaque, const char *password,
1110 void *readline_opaque)
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001111{
1112 qmp_change_vnc_password(password, NULL);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01001113 monitor_read_command(opaque, 1);
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001114}
1115
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001116void hmp_change(Monitor *mon, const QDict *qdict)
1117{
1118 const char *device = qdict_get_str(qdict, "device");
1119 const char *target = qdict_get_str(qdict, "target");
1120 const char *arg = qdict_get_try_str(qdict, "arg");
1121 Error *err = NULL;
1122
1123 if (strcmp(device, "vnc") == 0 &&
1124 (strcmp(target, "passwd") == 0 ||
1125 strcmp(target, "password") == 0)) {
1126 if (!arg) {
1127 monitor_read_password(mon, hmp_change_read_arg, NULL);
1128 return;
1129 }
1130 }
1131
1132 qmp_change(device, target, !!arg, arg, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001133 if (err &&
Luiz Capitulinoab878dd2012-08-06 15:55:22 -03001134 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
Luiz Capitulinoeef5ad12012-08-06 15:49:34 -03001135 error_free(err);
1136 monitor_read_block_device_key(mon, device, NULL, NULL);
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001137 return;
1138 }
1139 hmp_handle_error(mon, &err);
1140}
Luiz Capitulino80047da2011-12-14 16:49:14 -02001141
1142void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1143{
1144 Error *err = NULL;
1145
1146 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1147 qdict_get_int(qdict, "bps"),
1148 qdict_get_int(qdict, "bps_rd"),
1149 qdict_get_int(qdict, "bps_wr"),
1150 qdict_get_int(qdict, "iops"),
1151 qdict_get_int(qdict, "iops_rd"),
Benoît Canet3e9fab62013-09-02 14:14:40 +02001152 qdict_get_int(qdict, "iops_wr"),
1153 false, /* no burst max via HMP */
1154 0,
1155 false,
1156 0,
1157 false,
1158 0,
1159 false,
1160 0,
1161 false,
1162 0,
1163 false,
Benoît Canet2024c1d2013-09-02 14:14:41 +02001164 0,
1165 false, /* No default I/O size */
Benoît Canet3e9fab62013-09-02 14:14:40 +02001166 0, &err);
Luiz Capitulino80047da2011-12-14 16:49:14 -02001167 hmp_handle_error(mon, &err);
1168}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001169
1170void hmp_block_stream(Monitor *mon, const QDict *qdict)
1171{
1172 Error *error = NULL;
1173 const char *device = qdict_get_str(qdict, "device");
1174 const char *base = qdict_get_try_str(qdict, "base");
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001175 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001176
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001177 qmp_block_stream(device, base != NULL, base,
Paolo Bonzini1d809092012-09-28 17:22:59 +02001178 qdict_haskey(qdict, "speed"), speed,
Anthony Liguori46663e52013-09-17 11:10:47 -05001179 true, BLOCKDEV_ON_ERROR_REPORT, &error);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001180
1181 hmp_handle_error(mon, &error);
1182}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001183
1184void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1185{
1186 Error *error = NULL;
1187 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzinic6db2392012-05-08 16:51:56 +02001188 int64_t value = qdict_get_int(qdict, "speed");
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001189
1190 qmp_block_job_set_speed(device, value, &error);
1191
1192 hmp_handle_error(mon, &error);
1193}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001194
1195void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1196{
1197 Error *error = NULL;
1198 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001199 bool force = qdict_get_try_bool(qdict, "force", 0);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001200
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001201 qmp_block_job_cancel(device, true, force, &error);
1202
1203 hmp_handle_error(mon, &error);
1204}
1205
1206void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1207{
1208 Error *error = NULL;
1209 const char *device = qdict_get_str(qdict, "device");
1210
1211 qmp_block_job_pause(device, &error);
1212
1213 hmp_handle_error(mon, &error);
1214}
1215
1216void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1217{
1218 Error *error = NULL;
1219 const char *device = qdict_get_str(qdict, "device");
1220
1221 qmp_block_job_resume(device, &error);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001222
1223 hmp_handle_error(mon, &error);
1224}
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001225
Paolo Bonziniaeae8832012-10-18 16:49:21 +02001226void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1227{
1228 Error *error = NULL;
1229 const char *device = qdict_get_str(qdict, "device");
1230
1231 qmp_block_job_complete(device, &error);
1232
1233 hmp_handle_error(mon, &error);
1234}
1235
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001236typedef struct MigrationStatus
1237{
1238 QEMUTimer *timer;
1239 Monitor *mon;
1240 bool is_block_migration;
1241} MigrationStatus;
1242
1243static void hmp_migrate_status_cb(void *opaque)
1244{
1245 MigrationStatus *status = opaque;
1246 MigrationInfo *info;
1247
1248 info = qmp_query_migrate(NULL);
Soramichi AKIYAMAdde3a212014-01-27 19:46:11 +09001249 if (!info->has_status || strcmp(info->status, "active") == 0 ||
1250 strcmp(info->status, "setup") == 0) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001251 if (info->has_disk) {
1252 int progress;
1253
1254 if (info->disk->remaining) {
1255 progress = info->disk->transferred * 100 / info->disk->total;
1256 } else {
1257 progress = 100;
1258 }
1259
1260 monitor_printf(status->mon, "Completed %d %%\r", progress);
1261 monitor_flush(status->mon);
1262 }
1263
Alex Blighbc72ad62013-08-21 16:03:08 +01001264 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001265 } else {
1266 if (status->is_block_migration) {
1267 monitor_printf(status->mon, "\n");
1268 }
1269 monitor_resume(status->mon);
Alex Blighbc72ad62013-08-21 16:03:08 +01001270 timer_del(status->timer);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001271 g_free(status);
1272 }
1273
1274 qapi_free_MigrationInfo(info);
1275}
1276
1277void hmp_migrate(Monitor *mon, const QDict *qdict)
1278{
1279 int detach = qdict_get_try_bool(qdict, "detach", 0);
1280 int blk = qdict_get_try_bool(qdict, "blk", 0);
1281 int inc = qdict_get_try_bool(qdict, "inc", 0);
1282 const char *uri = qdict_get_str(qdict, "uri");
1283 Error *err = NULL;
1284
1285 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1286 if (err) {
1287 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1288 error_free(err);
1289 return;
1290 }
1291
1292 if (!detach) {
1293 MigrationStatus *status;
1294
1295 if (monitor_suspend(mon) < 0) {
1296 monitor_printf(mon, "terminal does not allow synchronous "
1297 "migration, continuing detached\n");
1298 return;
1299 }
1300
1301 status = g_malloc0(sizeof(*status));
1302 status->mon = mon;
1303 status->is_block_migration = blk || inc;
Alex Blighbc72ad62013-08-21 16:03:08 +01001304 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001305 status);
Alex Blighbc72ad62013-08-21 16:03:08 +01001306 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001307 }
1308}
Luiz Capitulinoa15fef22012-03-29 12:38:50 -03001309
1310void hmp_device_del(Monitor *mon, const QDict *qdict)
1311{
1312 const char *id = qdict_get_str(qdict, "id");
1313 Error *err = NULL;
1314
1315 qmp_device_del(id, &err);
1316 hmp_handle_error(mon, &err);
1317}
Wen Congyang783e9b42012-05-07 12:10:47 +08001318
1319void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1320{
Markus Armbrustere940f542014-05-02 13:26:29 +02001321 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001322 int paging = qdict_get_try_bool(qdict, "paging", 0);
Qiao Nuohan1b7a0f72014-04-17 16:15:07 +08001323 int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1324 int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1325 int snappy = qdict_get_try_bool(qdict, "snappy", 0);
Luiz Capitulino75363762012-09-21 13:53:00 -03001326 const char *file = qdict_get_str(qdict, "filename");
Wen Congyang783e9b42012-05-07 12:10:47 +08001327 bool has_begin = qdict_haskey(qdict, "begin");
1328 bool has_length = qdict_haskey(qdict, "length");
1329 int64_t begin = 0;
1330 int64_t length = 0;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001331 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
Luiz Capitulino75363762012-09-21 13:53:00 -03001332 char *prot;
Wen Congyang783e9b42012-05-07 12:10:47 +08001333
Qiao Nuohan1b7a0f72014-04-17 16:15:07 +08001334 if (zlib + lzo + snappy > 1) {
Markus Armbrustere940f542014-05-02 13:26:29 +02001335 error_setg(&err, "only one of '-z|-l|-s' can be set");
1336 hmp_handle_error(mon, &err);
Qiao Nuohan1b7a0f72014-04-17 16:15:07 +08001337 return;
1338 }
1339
1340 if (zlib) {
1341 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1342 }
1343
1344 if (lzo) {
1345 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1346 }
1347
1348 if (snappy) {
1349 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1350 }
1351
Wen Congyang783e9b42012-05-07 12:10:47 +08001352 if (has_begin) {
1353 begin = qdict_get_int(qdict, "begin");
1354 }
1355 if (has_length) {
1356 length = qdict_get_int(qdict, "length");
1357 }
1358
Luiz Capitulino75363762012-09-21 13:53:00 -03001359 prot = g_strconcat("file:", file, NULL);
1360
1361 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
Markus Armbrustere940f542014-05-02 13:26:29 +02001362 true, dump_format, &err);
1363 hmp_handle_error(mon, &err);
Luiz Capitulino75363762012-09-21 13:53:00 -03001364 g_free(prot);
Wen Congyang783e9b42012-05-07 12:10:47 +08001365}
Luiz Capitulino928059a2012-04-18 17:34:15 -03001366
1367void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1368{
1369 Error *err = NULL;
1370 QemuOpts *opts;
1371
1372 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001373 if (err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001374 goto out;
1375 }
1376
1377 netdev_add(opts, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001378 if (err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001379 qemu_opts_del(opts);
1380 }
1381
1382out:
1383 hmp_handle_error(mon, &err);
1384}
Luiz Capitulino5f964152012-04-16 14:36:32 -03001385
1386void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1387{
1388 const char *id = qdict_get_str(qdict, "id");
1389 Error *err = NULL;
1390
1391 qmp_netdev_del(id, &err);
1392 hmp_handle_error(mon, &err);
1393}
Corey Bryant208c9d12012-06-22 14:36:09 -04001394
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001395void hmp_object_add(Monitor *mon, const QDict *qdict)
1396{
1397 Error *err = NULL;
Markus Armbrusterf9f3a5e2014-05-07 09:53:51 +02001398 Error *err_end = NULL;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001399 QemuOpts *opts;
1400 char *type = NULL;
1401 char *id = NULL;
1402 void *dummy = NULL;
1403 OptsVisitor *ov;
1404 QDict *pdict;
1405
1406 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1407 if (err) {
1408 goto out;
1409 }
1410
1411 ov = opts_visitor_new(opts);
1412 pdict = qdict_clone_shallow(qdict);
1413
1414 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1415 if (err) {
1416 goto out_clean;
1417 }
1418
1419 qdict_del(pdict, "qom-type");
1420 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1421 if (err) {
Markus Armbrusterf9f3a5e2014-05-07 09:53:51 +02001422 goto out_end;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001423 }
1424
1425 qdict_del(pdict, "id");
1426 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1427 if (err) {
Markus Armbrusterf9f3a5e2014-05-07 09:53:51 +02001428 goto out_end;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001429 }
1430
1431 object_add(type, id, pdict, opts_get_visitor(ov), &err);
Markus Armbrusterf9f3a5e2014-05-07 09:53:51 +02001432
1433out_end:
1434 visit_end_struct(opts_get_visitor(ov), &err_end);
1435 if (!err && err_end) {
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001436 qmp_object_del(id, NULL);
1437 }
Markus Armbrusterf9f3a5e2014-05-07 09:53:51 +02001438 error_propagate(&err, err_end);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001439out_clean:
1440 opts_visitor_cleanup(ov);
1441
1442 QDECREF(pdict);
1443 qemu_opts_del(opts);
1444 g_free(id);
1445 g_free(type);
1446 g_free(dummy);
1447
1448out:
1449 hmp_handle_error(mon, &err);
1450}
1451
Corey Bryant208c9d12012-06-22 14:36:09 -04001452void hmp_getfd(Monitor *mon, const QDict *qdict)
1453{
1454 const char *fdname = qdict_get_str(qdict, "fdname");
Markus Armbrustere940f542014-05-02 13:26:29 +02001455 Error *err = NULL;
Corey Bryant208c9d12012-06-22 14:36:09 -04001456
Markus Armbrustere940f542014-05-02 13:26:29 +02001457 qmp_getfd(fdname, &err);
1458 hmp_handle_error(mon, &err);
Corey Bryant208c9d12012-06-22 14:36:09 -04001459}
1460
1461void hmp_closefd(Monitor *mon, const QDict *qdict)
1462{
1463 const char *fdname = qdict_get_str(qdict, "fdname");
Markus Armbrustere940f542014-05-02 13:26:29 +02001464 Error *err = NULL;
Corey Bryant208c9d12012-06-22 14:36:09 -04001465
Markus Armbrustere940f542014-05-02 13:26:29 +02001466 qmp_closefd(fdname, &err);
1467 hmp_handle_error(mon, &err);
Corey Bryant208c9d12012-06-22 14:36:09 -04001468}
Amos Konge4c8f002012-08-31 10:56:26 +08001469
1470void hmp_send_key(Monitor *mon, const QDict *qdict)
1471{
1472 const char *keys = qdict_get_str(qdict, "keys");
Luiz Capitulino9f328972012-09-20 14:19:47 -03001473 KeyValueList *keylist, *head = NULL, *tmp = NULL;
Amos Konge4c8f002012-08-31 10:56:26 +08001474 int has_hold_time = qdict_haskey(qdict, "hold-time");
1475 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1476 Error *err = NULL;
1477 char keyname_buf[16];
1478 char *separator;
Luiz Capitulino9f328972012-09-20 14:19:47 -03001479 int keyname_len;
Amos Konge4c8f002012-08-31 10:56:26 +08001480
1481 while (1) {
1482 separator = strchr(keys, '-');
1483 keyname_len = separator ? separator - keys : strlen(keys);
1484 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1485
1486 /* Be compatible with old interface, convert user inputted "<" */
1487 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1488 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1489 keyname_len = 4;
1490 }
1491 keyname_buf[keyname_len] = 0;
1492
Amos Konge4c8f002012-08-31 10:56:26 +08001493 keylist = g_malloc0(sizeof(*keylist));
Luiz Capitulino9f328972012-09-20 14:19:47 -03001494 keylist->value = g_malloc0(sizeof(*keylist->value));
Amos Konge4c8f002012-08-31 10:56:26 +08001495
1496 if (!head) {
1497 head = keylist;
1498 }
1499 if (tmp) {
1500 tmp->next = keylist;
1501 }
1502 tmp = keylist;
1503
Luiz Capitulino9f328972012-09-20 14:19:47 -03001504 if (strstart(keyname_buf, "0x", NULL)) {
1505 char *endp;
1506 int value = strtoul(keyname_buf, &endp, 0);
1507 if (*endp != '\0') {
1508 goto err_out;
1509 }
1510 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1511 keylist->value->number = value;
1512 } else {
1513 int idx = index_from_key(keyname_buf);
1514 if (idx == Q_KEY_CODE_MAX) {
1515 goto err_out;
1516 }
1517 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1518 keylist->value->qcode = idx;
1519 }
1520
Amos Konge4c8f002012-08-31 10:56:26 +08001521 if (!separator) {
1522 break;
1523 }
1524 keys = separator + 1;
1525 }
1526
Luiz Capitulino9f328972012-09-20 14:19:47 -03001527 qmp_send_key(head, has_hold_time, hold_time, &err);
Amos Konge4c8f002012-08-31 10:56:26 +08001528 hmp_handle_error(mon, &err);
Luiz Capitulino9f328972012-09-20 14:19:47 -03001529
1530out:
1531 qapi_free_KeyValueList(head);
1532 return;
1533
1534err_out:
1535 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1536 goto out;
Amos Konge4c8f002012-08-31 10:56:26 +08001537}
Luiz Capitulinoad39cf62012-05-24 13:48:23 -03001538
1539void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1540{
1541 const char *filename = qdict_get_str(qdict, "filename");
1542 Error *err = NULL;
1543
1544 qmp_screendump(filename, &err);
1545 hmp_handle_error(mon, &err);
1546}
Paolo Bonzini40577252012-08-23 11:53:04 +02001547
1548void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1549{
1550 const char *uri = qdict_get_str(qdict, "uri");
1551 int writable = qdict_get_try_bool(qdict, "writable", 0);
1552 int all = qdict_get_try_bool(qdict, "all", 0);
1553 Error *local_err = NULL;
1554 BlockInfoList *block_list, *info;
1555 SocketAddress *addr;
1556
1557 if (writable && !all) {
1558 error_setg(&local_err, "-w only valid together with -a");
1559 goto exit;
1560 }
1561
1562 /* First check if the address is valid and start the server. */
1563 addr = socket_parse(uri, &local_err);
1564 if (local_err != NULL) {
1565 goto exit;
1566 }
1567
1568 qmp_nbd_server_start(addr, &local_err);
1569 qapi_free_SocketAddress(addr);
1570 if (local_err != NULL) {
1571 goto exit;
1572 }
1573
1574 if (!all) {
1575 return;
1576 }
1577
1578 /* Then try adding all block devices. If one fails, close all and
1579 * exit.
1580 */
1581 block_list = qmp_query_block(NULL);
1582
1583 for (info = block_list; info; info = info->next) {
1584 if (!info->value->has_inserted) {
1585 continue;
1586 }
1587
1588 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1589
1590 if (local_err != NULL) {
1591 qmp_nbd_server_stop(NULL);
1592 break;
1593 }
1594 }
1595
1596 qapi_free_BlockInfoList(block_list);
1597
1598exit:
1599 hmp_handle_error(mon, &local_err);
1600}
1601
1602void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1603{
1604 const char *device = qdict_get_str(qdict, "device");
1605 int writable = qdict_get_try_bool(qdict, "writable", 0);
1606 Error *local_err = NULL;
1607
1608 qmp_nbd_server_add(device, true, writable, &local_err);
1609
1610 if (local_err != NULL) {
1611 hmp_handle_error(mon, &local_err);
1612 }
1613}
1614
1615void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1616{
Markus Armbrustere940f542014-05-02 13:26:29 +02001617 Error *err = NULL;
Paolo Bonzini40577252012-08-23 11:53:04 +02001618
Markus Armbrustere940f542014-05-02 13:26:29 +02001619 qmp_nbd_server_stop(&err);
1620 hmp_handle_error(mon, &err);
Paolo Bonzini40577252012-08-23 11:53:04 +02001621}
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001622
Jason J. Herneabf23322013-12-11 13:24:14 -05001623void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1624{
1625 int cpuid;
1626 Error *err = NULL;
1627
1628 cpuid = qdict_get_int(qdict, "id");
1629 qmp_cpu_add(cpuid, &err);
1630 hmp_handle_error(mon, &err);
1631}
1632
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001633void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1634{
1635 const char *args = qdict_get_str(qdict, "args");
1636 Error *err = NULL;
1637 QemuOpts *opts;
1638
1639 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1640 if (opts == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001641 error_setg(&err, "Parsing chardev args failed");
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001642 } else {
1643 qemu_chr_new_from_opts(opts, NULL, &err);
1644 }
1645 hmp_handle_error(mon, &err);
1646}
1647
1648void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1649{
1650 Error *local_err = NULL;
1651
1652 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1653 hmp_handle_error(mon, &local_err);
1654}
Kevin Wolf587da2c2013-06-05 14:19:41 +02001655
1656void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1657{
1658 BlockDriverState *bs;
1659 const char* device = qdict_get_str(qdict, "device");
1660 const char* command = qdict_get_str(qdict, "command");
1661 Error *err = NULL;
1662
1663 bs = bdrv_find(device);
1664 if (bs) {
1665 qemuio_command(bs, command);
1666 } else {
1667 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1668 }
1669
1670 hmp_handle_error(mon, &err);
1671}
Paolo Bonziniab2d0532013-12-20 23:21:09 +01001672
1673void hmp_object_del(Monitor *mon, const QDict *qdict)
1674{
1675 const char *id = qdict_get_str(qdict, "id");
1676 Error *err = NULL;
1677
1678 qmp_object_del(id, &err);
1679 hmp_handle_error(mon, &err);
1680}
Hu Taoeb1539b2014-05-14 17:43:35 +08001681
1682void hmp_info_memdev(Monitor *mon, const QDict *qdict)
1683{
1684 Error *err = NULL;
1685 MemdevList *memdev_list = qmp_query_memdev(&err);
1686 MemdevList *m = memdev_list;
1687 StringOutputVisitor *ov;
1688 int i = 0;
1689
1690
1691 while (m) {
1692 ov = string_output_visitor_new(false);
1693 visit_type_uint16List(string_output_get_visitor(ov),
1694 &m->value->host_nodes, NULL, NULL);
1695 monitor_printf(mon, "memory device %d\n", i);
1696 monitor_printf(mon, " size: %" PRId64 "\n", m->value->size);
1697 monitor_printf(mon, " merge: %s\n",
1698 m->value->merge ? "true" : "false");
1699 monitor_printf(mon, " dump: %s\n",
1700 m->value->dump ? "true" : "false");
1701 monitor_printf(mon, " prealloc: %s\n",
1702 m->value->prealloc ? "true" : "false");
1703 monitor_printf(mon, " policy: %s\n",
1704 HostMemPolicy_lookup[m->value->policy]);
1705 monitor_printf(mon, " host nodes: %s\n",
1706 string_output_get_string(ov));
1707
1708 string_output_visitor_cleanup(ov);
1709 m = m->next;
1710 i++;
1711 }
1712
1713 monitor_printf(mon, "\n");
1714}