blob: 9469163913fcf2005feeb33b0dc32548214dad1f [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"
Paolo Bonzini28ecbae2012-11-28 12:06:30 +010025#include "ui/console.h"
Wenchao Xiabd093a32013-06-06 12:28:00 +080026#include "block/qapi.h"
Kevin Wolf587da2c2013-06-05 14:19:41 +020027#include "qemu-io.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050028
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020029static void hmp_handle_error(Monitor *mon, Error **errp)
30{
31 if (error_is_set(errp)) {
32 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
33 error_free(*errp);
34 }
35}
36
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080037void hmp_info_name(Monitor *mon, const QDict *qdict)
Anthony Liguori48a32be2011-09-02 12:34:48 -050038{
39 NameInfo *info;
40
41 info = qmp_query_name(NULL);
42 if (info->has_name) {
43 monitor_printf(mon, "%s\n", info->name);
44 }
45 qapi_free_NameInfo(info);
46}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030047
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080048void hmp_info_version(Monitor *mon, const QDict *qdict)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030049{
50 VersionInfo *info;
51
52 info = qmp_query_version(NULL);
53
54 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
55 info->qemu.major, info->qemu.minor, info->qemu.micro,
56 info->package);
57
58 qapi_free_VersionInfo(info);
59}
Luiz Capitulino292a2602011-09-12 15:10:53 -030060
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080061void hmp_info_kvm(Monitor *mon, const QDict *qdict)
Luiz Capitulino292a2602011-09-12 15:10:53 -030062{
63 KvmInfo *info;
64
65 info = qmp_query_kvm(NULL);
66 monitor_printf(mon, "kvm support: ");
67 if (info->present) {
68 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
69 } else {
70 monitor_printf(mon, "not compiled\n");
71 }
72
73 qapi_free_KvmInfo(info);
74}
75
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080076void hmp_info_status(Monitor *mon, const QDict *qdict)
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -030077{
78 StatusInfo *info;
79
80 info = qmp_query_status(NULL);
81
82 monitor_printf(mon, "VM status: %s%s",
83 info->running ? "running" : "paused",
84 info->singlestep ? " (single step mode)" : "");
85
86 if (!info->running && info->status != RUN_STATE_PAUSED) {
87 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
88 }
89
90 monitor_printf(mon, "\n");
91
92 qapi_free_StatusInfo(info);
93}
94
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080095void hmp_info_uuid(Monitor *mon, const QDict *qdict)
Luiz Capitulinoefab7672011-09-13 17:16:25 -030096{
97 UuidInfo *info;
98
99 info = qmp_query_uuid(NULL);
100 monitor_printf(mon, "%s\n", info->UUID);
101 qapi_free_UuidInfo(info);
102}
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300103
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800104void hmp_info_chardev(Monitor *mon, const QDict *qdict)
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300105{
106 ChardevInfoList *char_info, *info;
107
108 char_info = qmp_query_chardev(NULL);
109 for (info = char_info; info; info = info->next) {
110 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
111 info->value->filename);
112 }
113
114 qapi_free_ChardevInfoList(char_info);
115}
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300116
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800117void hmp_info_mice(Monitor *mon, const QDict *qdict)
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300118{
119 MouseInfoList *mice_list, *mouse;
120
121 mice_list = qmp_query_mice(NULL);
122 if (!mice_list) {
123 monitor_printf(mon, "No mouse devices connected\n");
124 return;
125 }
126
127 for (mouse = mice_list; mouse; mouse = mouse->next) {
128 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
129 mouse->value->current ? '*' : ' ',
130 mouse->value->index, mouse->value->name,
131 mouse->value->absolute ? " (absolute)" : "");
132 }
133
134 qapi_free_MouseInfoList(mice_list);
135}
136
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800137void hmp_info_migrate(Monitor *mon, const QDict *qdict)
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300138{
139 MigrationInfo *info;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300140 MigrationCapabilityStatusList *caps, *cap;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300141
142 info = qmp_query_migrate(NULL);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300143 caps = qmp_query_migrate_capabilities(NULL);
144
145 /* do not display parameters during setup */
146 if (info->has_status && caps) {
147 monitor_printf(mon, "capabilities: ");
148 for (cap = caps; cap; cap = cap->next) {
149 monitor_printf(mon, "%s: %s ",
150 MigrationCapability_lookup[cap->value->capability],
151 cap->value->state ? "on" : "off");
152 }
153 monitor_printf(mon, "\n");
154 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300155
156 if (info->has_status) {
157 monitor_printf(mon, "Migration status: %s\n", info->status);
Juan Quintela7aa939a2012-08-18 13:17:10 +0200158 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
159 info->total_time);
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200160 if (info->has_expected_downtime) {
161 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
162 info->expected_downtime);
163 }
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200164 if (info->has_downtime) {
165 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
166 info->downtime);
167 }
Michael R. Hinesed4fbd12013-07-22 10:01:58 -0400168 if (info->has_setup_time) {
169 monitor_printf(mon, "setup: %" PRIu64 " milliseconds\n",
170 info->setup_time);
171 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300172 }
173
174 if (info->has_ram) {
175 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
176 info->ram->transferred >> 10);
Michael R. Hines7e114f82013-06-25 21:35:30 -0400177 monitor_printf(mon, "throughput: %0.2f mbps\n",
178 info->ram->mbps);
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300179 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
180 info->ram->remaining >> 10);
181 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
182 info->ram->total >> 10);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300183 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
184 info->ram->duplicate);
Peter Lievenf1c72792013-03-26 10:58:37 +0100185 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
186 info->ram->skipped);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300187 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
188 info->ram->normal);
189 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
190 info->ram->normal_bytes >> 10);
ChenLiang58570ed2014-04-04 17:57:55 +0800191 monitor_printf(mon, "dirty sync count: %" PRIu64 "\n",
192 info->ram->dirty_sync_count);
Juan Quintela8d017192012-08-13 12:31:25 +0200193 if (info->ram->dirty_pages_rate) {
194 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
195 info->ram->dirty_pages_rate);
196 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300197 }
198
199 if (info->has_disk) {
200 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
201 info->disk->transferred >> 10);
202 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
203 info->disk->remaining >> 10);
204 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
205 info->disk->total >> 10);
206 }
207
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300208 if (info->has_xbzrle_cache) {
209 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
210 info->xbzrle_cache->cache_size);
211 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
212 info->xbzrle_cache->bytes >> 10);
213 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
214 info->xbzrle_cache->pages);
215 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
216 info->xbzrle_cache->cache_miss);
ChenLiang8bc39232014-04-04 17:57:56 +0800217 monitor_printf(mon, "xbzrle cache miss rate: %0.2f\n",
218 info->xbzrle_cache->cache_miss_rate);
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300219 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
220 info->xbzrle_cache->overflow);
221 }
222
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300223 qapi_free_MigrationInfo(info);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300224 qapi_free_MigrationCapabilityStatusList(caps);
225}
226
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800227void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300228{
229 MigrationCapabilityStatusList *caps, *cap;
230
231 caps = qmp_query_migrate_capabilities(NULL);
232
233 if (caps) {
234 monitor_printf(mon, "capabilities: ");
235 for (cap = caps; cap; cap = cap->next) {
236 monitor_printf(mon, "%s: %s ",
237 MigrationCapability_lookup[cap->value->capability],
238 cap->value->state ? "on" : "off");
239 }
240 monitor_printf(mon, "\n");
241 }
242
243 qapi_free_MigrationCapabilityStatusList(caps);
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300244}
245
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800246void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300247{
248 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
249 qmp_query_migrate_cache_size(NULL) >> 10);
250}
251
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800252void hmp_info_cpus(Monitor *mon, const QDict *qdict)
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300253{
254 CpuInfoList *cpu_list, *cpu;
255
256 cpu_list = qmp_query_cpus(NULL);
257
258 for (cpu = cpu_list; cpu; cpu = cpu->next) {
259 int active = ' ';
260
261 if (cpu->value->CPU == monitor_get_cpu_index()) {
262 active = '*';
263 }
264
Aurelien Jarno852bef02012-10-19 23:19:19 +0200265 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300266
267 if (cpu->value->has_pc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200268 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300269 }
270 if (cpu->value->has_nip) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200271 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300272 }
273 if (cpu->value->has_npc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200274 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300275 }
276 if (cpu->value->has_PC) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200277 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300278 }
279
280 if (cpu->value->halted) {
281 monitor_printf(mon, " (halted)");
282 }
283
284 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
285 }
286
287 qapi_free_CpuInfoList(cpu_list);
288}
289
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800290void hmp_info_block(Monitor *mon, const QDict *qdict)
Luiz Capitulinob2023812011-09-21 17:16:47 -0300291{
292 BlockInfoList *block_list, *info;
Wenchao Xiabd093a32013-06-06 12:28:00 +0800293 ImageInfo *image_info;
Wenchao Xiae73fe2b2013-06-06 12:28:01 +0800294 const char *device = qdict_get_try_str(qdict, "device");
295 bool verbose = qdict_get_try_bool(qdict, "verbose", 0);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300296
297 block_list = qmp_query_block(NULL);
298
299 for (info = block_list; info; info = info->next) {
Wenchao Xiae73fe2b2013-06-06 12:28:01 +0800300 if (device && strcmp(device, info->value->device)) {
301 continue;
302 }
Luiz Capitulinob2023812011-09-21 17:16:47 -0300303
Kevin Wolffbe2e262013-06-19 16:10:55 +0200304 if (info != block_list) {
305 monitor_printf(mon, "\n");
Luiz Capitulinob2023812011-09-21 17:16:47 -0300306 }
307
Kevin Wolffbe2e262013-06-19 16:10:55 +0200308 monitor_printf(mon, "%s", info->value->device);
309 if (info->value->has_inserted) {
310 monitor_printf(mon, ": %s (%s%s%s)\n",
311 info->value->inserted->file,
312 info->value->inserted->drv,
313 info->value->inserted->ro ? ", read-only" : "",
314 info->value->inserted->encrypted ? ", encrypted" : "");
315 } else {
316 monitor_printf(mon, ": [not inserted]\n");
317 }
318
319 if (info->value->has_io_status && info->value->io_status != BLOCK_DEVICE_IO_STATUS_OK) {
320 monitor_printf(mon, " I/O status: %s\n",
Luiz Capitulinob2023812011-09-21 17:16:47 -0300321 BlockDeviceIoStatus_lookup[info->value->io_status]);
322 }
323
Kevin Wolffbe2e262013-06-19 16:10:55 +0200324 if (info->value->removable) {
325 monitor_printf(mon, " Removable device: %slocked, tray %s\n",
326 info->value->locked ? "" : "not ",
327 info->value->tray_open ? "open" : "closed");
328 }
Luiz Capitulinob2023812011-09-21 17:16:47 -0300329
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800330
Kevin Wolffbe2e262013-06-19 16:10:55 +0200331 if (!info->value->has_inserted) {
332 continue;
333 }
334
335 if (info->value->inserted->has_backing_file) {
336 monitor_printf(mon,
337 " Backing file: %s "
338 "(chain depth: %" PRId64 ")\n",
339 info->value->inserted->backing_file,
340 info->value->inserted->backing_file_depth);
341 }
342
343 if (info->value->inserted->bps
344 || info->value->inserted->bps_rd
345 || info->value->inserted->bps_wr
346 || info->value->inserted->iops
347 || info->value->inserted->iops_rd
348 || info->value->inserted->iops_wr)
349 {
350 monitor_printf(mon, " I/O throttling: bps=%" PRId64
351 " bps_rd=%" PRId64 " bps_wr=%" PRId64
Benoît Canet3e9fab62013-09-02 14:14:40 +0200352 " bps_max=%" PRId64
353 " bps_rd_max=%" PRId64
354 " bps_wr_max=%" PRId64
Kevin Wolffbe2e262013-06-19 16:10:55 +0200355 " iops=%" PRId64 " iops_rd=%" PRId64
Benoît Canet3e9fab62013-09-02 14:14:40 +0200356 " iops_wr=%" PRId64
357 " iops_max=%" PRId64
358 " iops_rd_max=%" PRId64
Benoît Canet2024c1d2013-09-02 14:14:41 +0200359 " iops_wr_max=%" PRId64
360 " iops_size=%" PRId64 "\n",
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800361 info->value->inserted->bps,
362 info->value->inserted->bps_rd,
363 info->value->inserted->bps_wr,
Benoît Canet3e9fab62013-09-02 14:14:40 +0200364 info->value->inserted->bps_max,
365 info->value->inserted->bps_rd_max,
366 info->value->inserted->bps_wr_max,
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800367 info->value->inserted->iops,
368 info->value->inserted->iops_rd,
Benoît Canet3e9fab62013-09-02 14:14:40 +0200369 info->value->inserted->iops_wr,
370 info->value->inserted->iops_max,
371 info->value->inserted->iops_rd_max,
Benoît Canet2024c1d2013-09-02 14:14:41 +0200372 info->value->inserted->iops_wr_max,
373 info->value->inserted->iops_size);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300374 }
375
Kevin Wolffbe2e262013-06-19 16:10:55 +0200376 if (verbose) {
377 monitor_printf(mon, "\nImages:\n");
378 image_info = info->value->inserted->image;
379 while (1) {
380 bdrv_image_info_dump((fprintf_function)monitor_printf,
381 mon, image_info);
382 if (image_info->has_backing_image) {
383 image_info = image_info->backing_image;
384 } else {
385 break;
386 }
387 }
388 }
Luiz Capitulinob2023812011-09-21 17:16:47 -0300389 }
390
391 qapi_free_BlockInfoList(block_list);
392}
393
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800394void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
Luiz Capitulinof11f57e2011-09-22 15:56:36 -0300395{
396 BlockStatsList *stats_list, *stats;
397
398 stats_list = qmp_query_blockstats(NULL);
399
400 for (stats = stats_list; stats; stats = stats->next) {
401 if (!stats->value->has_device) {
402 continue;
403 }
404
405 monitor_printf(mon, "%s:", stats->value->device);
406 monitor_printf(mon, " rd_bytes=%" PRId64
407 " wr_bytes=%" PRId64
408 " rd_operations=%" PRId64
409 " wr_operations=%" PRId64
410 " flush_operations=%" PRId64
411 " wr_total_time_ns=%" PRId64
412 " rd_total_time_ns=%" PRId64
413 " flush_total_time_ns=%" PRId64
414 "\n",
415 stats->value->stats->rd_bytes,
416 stats->value->stats->wr_bytes,
417 stats->value->stats->rd_operations,
418 stats->value->stats->wr_operations,
419 stats->value->stats->flush_operations,
420 stats->value->stats->wr_total_time_ns,
421 stats->value->stats->rd_total_time_ns,
422 stats->value->stats->flush_total_time_ns);
423 }
424
425 qapi_free_BlockStatsList(stats_list);
426}
427
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800428void hmp_info_vnc(Monitor *mon, const QDict *qdict)
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200429{
430 VncInfo *info;
431 Error *err = NULL;
432 VncClientInfoList *client;
433
434 info = qmp_query_vnc(&err);
435 if (err) {
436 monitor_printf(mon, "%s\n", error_get_pretty(err));
437 error_free(err);
438 return;
439 }
440
441 if (!info->enabled) {
442 monitor_printf(mon, "Server: disabled\n");
443 goto out;
444 }
445
446 monitor_printf(mon, "Server:\n");
447 if (info->has_host && info->has_service) {
448 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
449 }
450 if (info->has_auth) {
451 monitor_printf(mon, " auth: %s\n", info->auth);
452 }
453
454 if (!info->has_clients || info->clients == NULL) {
455 monitor_printf(mon, "Client: none\n");
456 } else {
457 for (client = info->clients; client; client = client->next) {
458 monitor_printf(mon, "Client:\n");
459 monitor_printf(mon, " address: %s:%s\n",
460 client->value->host, client->value->service);
461 monitor_printf(mon, " x509_dname: %s\n",
462 client->value->x509_dname ?
463 client->value->x509_dname : "none");
464 monitor_printf(mon, " username: %s\n",
465 client->value->has_sasl_username ?
466 client->value->sasl_username : "none");
467 }
468 }
469
470out:
471 qapi_free_VncInfo(info);
472}
473
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800474void hmp_info_spice(Monitor *mon, const QDict *qdict)
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200475{
476 SpiceChannelList *chan;
477 SpiceInfo *info;
478
479 info = qmp_query_spice(NULL);
480
481 if (!info->enabled) {
482 monitor_printf(mon, "Server: disabled\n");
483 goto out;
484 }
485
486 monitor_printf(mon, "Server:\n");
487 if (info->has_port) {
488 monitor_printf(mon, " address: %s:%" PRId64 "\n",
489 info->host, info->port);
490 }
491 if (info->has_tls_port) {
492 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
493 info->host, info->tls_port);
494 }
Yonit Halperin61c4efe2012-08-21 11:51:58 +0300495 monitor_printf(mon, " migrated: %s\n",
496 info->migrated ? "true" : "false");
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200497 monitor_printf(mon, " auth: %s\n", info->auth);
498 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
Alon Levy4efee022012-03-29 23:23:14 +0200499 monitor_printf(mon, " mouse-mode: %s\n",
500 SpiceQueryMouseMode_lookup[info->mouse_mode]);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200501
502 if (!info->has_channels || info->channels == NULL) {
503 monitor_printf(mon, "Channels: none\n");
504 } else {
505 for (chan = info->channels; chan; chan = chan->next) {
506 monitor_printf(mon, "Channel:\n");
507 monitor_printf(mon, " address: %s:%s%s\n",
508 chan->value->host, chan->value->port,
509 chan->value->tls ? " [tls]" : "");
510 monitor_printf(mon, " session: %" PRId64 "\n",
511 chan->value->connection_id);
512 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
513 chan->value->channel_type, chan->value->channel_id);
514 }
515 }
516
517out:
518 qapi_free_SpiceInfo(info);
519}
520
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800521void hmp_info_balloon(Monitor *mon, const QDict *qdict)
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200522{
523 BalloonInfo *info;
524 Error *err = NULL;
525
526 info = qmp_query_balloon(&err);
527 if (err) {
528 monitor_printf(mon, "%s\n", error_get_pretty(err));
529 error_free(err);
530 return;
531 }
532
Luiz Capitulino01ceb972012-12-03 15:56:41 -0200533 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200534
535 qapi_free_BalloonInfo(info);
536}
537
Luiz Capitulino79627472011-10-21 14:15:33 -0200538static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
539{
540 PciMemoryRegionList *region;
541
542 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
543 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
544 dev->slot, dev->function);
545 monitor_printf(mon, " ");
546
547 if (dev->class_info.has_desc) {
548 monitor_printf(mon, "%s", dev->class_info.desc);
549 } else {
Tomoki Sekiyama6f880092013-08-07 11:39:43 -0400550 monitor_printf(mon, "Class %04" PRId64, dev->class_info.q_class);
Luiz Capitulino79627472011-10-21 14:15:33 -0200551 }
552
553 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
554 dev->id.vendor, dev->id.device);
555
556 if (dev->has_irq) {
557 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
558 }
559
560 if (dev->has_pci_bridge) {
561 monitor_printf(mon, " BUS %" PRId64 ".\n",
562 dev->pci_bridge->bus.number);
563 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
564 dev->pci_bridge->bus.secondary);
565 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
566 dev->pci_bridge->bus.subordinate);
567
568 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
569 dev->pci_bridge->bus.io_range->base,
570 dev->pci_bridge->bus.io_range->limit);
571
572 monitor_printf(mon,
573 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
574 dev->pci_bridge->bus.memory_range->base,
575 dev->pci_bridge->bus.memory_range->limit);
576
577 monitor_printf(mon, " prefetchable memory range "
578 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
579 dev->pci_bridge->bus.prefetchable_range->base,
580 dev->pci_bridge->bus.prefetchable_range->limit);
581 }
582
583 for (region = dev->regions; region; region = region->next) {
584 uint64_t addr, size;
585
586 addr = region->value->address;
587 size = region->value->size;
588
589 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
590
591 if (!strcmp(region->value->type, "io")) {
592 monitor_printf(mon, "I/O at 0x%04" PRIx64
593 " [0x%04" PRIx64 "].\n",
594 addr, addr + size - 1);
595 } else {
596 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
597 " [0x%08" PRIx64 "].\n",
598 region->value->mem_type_64 ? 64 : 32,
599 region->value->prefetch ? " prefetchable" : "",
600 addr, addr + size - 1);
601 }
602 }
603
604 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
605
606 if (dev->has_pci_bridge) {
607 if (dev->pci_bridge->has_devices) {
608 PciDeviceInfoList *cdev;
609 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
610 hmp_info_pci_device(mon, cdev->value);
611 }
612 }
613 }
614}
615
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800616void hmp_info_pci(Monitor *mon, const QDict *qdict)
Luiz Capitulino79627472011-10-21 14:15:33 -0200617{
Stefan Bergerf46cee32012-01-11 10:51:52 -0500618 PciInfoList *info_list, *info;
Luiz Capitulino79627472011-10-21 14:15:33 -0200619 Error *err = NULL;
620
Stefan Bergerf46cee32012-01-11 10:51:52 -0500621 info_list = qmp_query_pci(&err);
Luiz Capitulino79627472011-10-21 14:15:33 -0200622 if (err) {
623 monitor_printf(mon, "PCI devices not supported\n");
624 error_free(err);
625 return;
626 }
627
Stefan Bergerf46cee32012-01-11 10:51:52 -0500628 for (info = info_list; info; info = info->next) {
Luiz Capitulino79627472011-10-21 14:15:33 -0200629 PciDeviceInfoList *dev;
630
631 for (dev = info->value->devices; dev; dev = dev->next) {
632 hmp_info_pci_device(mon, dev->value);
633 }
634 }
635
Stefan Bergerf46cee32012-01-11 10:51:52 -0500636 qapi_free_PciInfoList(info_list);
Luiz Capitulino79627472011-10-21 14:15:33 -0200637}
638
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800639void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +0000640{
641 BlockJobInfoList *list;
642 Error *err = NULL;
643
644 list = qmp_query_block_jobs(&err);
645 assert(!err);
646
647 if (!list) {
648 monitor_printf(mon, "No active jobs\n");
649 return;
650 }
651
652 while (list) {
653 if (strcmp(list->value->type, "stream") == 0) {
654 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
655 " of %" PRId64 " bytes, speed limit %" PRId64
656 " bytes/s\n",
657 list->value->device,
658 list->value->offset,
659 list->value->len,
660 list->value->speed);
661 } else {
662 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
663 " of %" PRId64 " bytes, speed limit %" PRId64
664 " bytes/s\n",
665 list->value->type,
666 list->value->device,
667 list->value->offset,
668 list->value->len,
669 list->value->speed);
670 }
671 list = list->next;
672 }
673}
674
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500675void hmp_info_tpm(Monitor *mon, const QDict *qdict)
676{
677 TPMInfoList *info_list, *info;
678 Error *err = NULL;
679 unsigned int c = 0;
680 TPMPassthroughOptions *tpo;
681
682 info_list = qmp_query_tpm(&err);
683 if (err) {
684 monitor_printf(mon, "TPM device not supported\n");
685 error_free(err);
686 return;
687 }
688
689 if (info_list) {
690 monitor_printf(mon, "TPM device:\n");
691 }
692
693 for (info = info_list; info; info = info->next) {
694 TPMInfo *ti = info->value;
695 monitor_printf(mon, " tpm%d: model=%s\n",
696 c, TpmModel_lookup[ti->model]);
697
698 monitor_printf(mon, " \\ %s: type=%s",
Corey Bryant88ca7bc2013-03-20 12:34:48 -0400699 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500700
Corey Bryant88ca7bc2013-03-20 12:34:48 -0400701 switch (ti->options->kind) {
702 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
703 tpo = ti->options->passthrough;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500704 monitor_printf(mon, "%s%s%s%s",
705 tpo->has_path ? ",path=" : "",
706 tpo->has_path ? tpo->path : "",
707 tpo->has_cancel_path ? ",cancel-path=" : "",
708 tpo->has_cancel_path ? tpo->cancel_path : "");
709 break;
710 case TPM_TYPE_OPTIONS_KIND_MAX:
711 break;
712 }
713 monitor_printf(mon, "\n");
714 c++;
715 }
716 qapi_free_TPMInfoList(info_list);
717}
718
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300719void hmp_quit(Monitor *mon, const QDict *qdict)
720{
721 monitor_suspend(mon);
722 qmp_quit(NULL);
723}
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300724
725void hmp_stop(Monitor *mon, const QDict *qdict)
726{
727 qmp_stop(NULL);
728}
Luiz Capitulino38d22652011-09-15 14:41:46 -0300729
730void hmp_system_reset(Monitor *mon, const QDict *qdict)
731{
732 qmp_system_reset(NULL);
733}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300734
735void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
736{
737 qmp_system_powerdown(NULL);
738}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300739
740void hmp_cpu(Monitor *mon, const QDict *qdict)
741{
742 int64_t cpu_index;
743
744 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
745 use it are converted to the QAPI */
746 cpu_index = qdict_get_int(qdict, "index");
747 if (monitor_set_cpu(cpu_index) < 0) {
748 monitor_printf(mon, "invalid CPU index\n");
749 }
750}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200751
752void hmp_memsave(Monitor *mon, const QDict *qdict)
753{
754 uint32_t size = qdict_get_int(qdict, "size");
755 const char *filename = qdict_get_str(qdict, "filename");
756 uint64_t addr = qdict_get_int(qdict, "val");
Markus Armbrustere940f542014-05-02 13:26:29 +0200757 Error *err = NULL;
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200758
Markus Armbrustere940f542014-05-02 13:26:29 +0200759 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &err);
760 hmp_handle_error(mon, &err);
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200761}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200762
763void hmp_pmemsave(Monitor *mon, const QDict *qdict)
764{
765 uint32_t size = qdict_get_int(qdict, "size");
766 const char *filename = qdict_get_str(qdict, "filename");
767 uint64_t addr = qdict_get_int(qdict, "val");
Markus Armbrustere940f542014-05-02 13:26:29 +0200768 Error *err = NULL;
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200769
Markus Armbrustere940f542014-05-02 13:26:29 +0200770 qmp_pmemsave(addr, size, filename, &err);
771 hmp_handle_error(mon, &err);
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200772}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200773
Markus Armbruster3949e592013-02-06 21:27:24 +0100774void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
Lei Li1f590cf2013-01-25 00:03:20 +0800775{
Lei Li1f590cf2013-01-25 00:03:20 +0800776 const char *chardev = qdict_get_str(qdict, "device");
777 const char *data = qdict_get_str(qdict, "data");
Markus Armbrustere940f542014-05-02 13:26:29 +0200778 Error *err = NULL;
Lei Li1f590cf2013-01-25 00:03:20 +0800779
Markus Armbrustere940f542014-05-02 13:26:29 +0200780 qmp_ringbuf_write(chardev, data, false, 0, &err);
Lei Li1f590cf2013-01-25 00:03:20 +0800781
Markus Armbrustere940f542014-05-02 13:26:29 +0200782 hmp_handle_error(mon, &err);
Lei Li1f590cf2013-01-25 00:03:20 +0800783}
784
Markus Armbruster3949e592013-02-06 21:27:24 +0100785void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
Lei Li49b6d722013-01-25 00:03:21 +0800786{
787 uint32_t size = qdict_get_int(qdict, "size");
788 const char *chardev = qdict_get_str(qdict, "device");
Markus Armbruster3ab651f2013-02-06 21:27:15 +0100789 char *data;
Markus Armbrustere940f542014-05-02 13:26:29 +0200790 Error *err = NULL;
Markus Armbruster543f3412013-02-06 21:27:26 +0100791 int i;
Lei Li49b6d722013-01-25 00:03:21 +0800792
Markus Armbrustere940f542014-05-02 13:26:29 +0200793 data = qmp_ringbuf_read(chardev, size, false, 0, &err);
794 if (err) {
795 monitor_printf(mon, "%s\n", error_get_pretty(err));
796 error_free(err);
Lei Li49b6d722013-01-25 00:03:21 +0800797 return;
798 }
799
Markus Armbruster543f3412013-02-06 21:27:26 +0100800 for (i = 0; data[i]; i++) {
801 unsigned char ch = data[i];
802
803 if (ch == '\\') {
804 monitor_printf(mon, "\\\\");
805 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
806 monitor_printf(mon, "\\u%04X", ch);
807 } else {
808 monitor_printf(mon, "%c", ch);
809 }
810
811 }
812 monitor_printf(mon, "\n");
Markus Armbruster3ab651f2013-02-06 21:27:15 +0100813 g_free(data);
Lei Li49b6d722013-01-25 00:03:21 +0800814}
815
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200816static void hmp_cont_cb(void *opaque, int err)
817{
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200818 if (!err) {
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300819 qmp_cont(NULL);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200820 }
821}
822
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300823static bool key_is_missing(const BlockInfo *bdev)
824{
825 return (bdev->inserted && bdev->inserted->encryption_key_missing);
826}
827
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200828void hmp_cont(Monitor *mon, const QDict *qdict)
829{
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300830 BlockInfoList *bdev_list, *bdev;
Markus Armbrustere940f542014-05-02 13:26:29 +0200831 Error *err = NULL;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200832
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300833 bdev_list = qmp_query_block(NULL);
834 for (bdev = bdev_list; bdev; bdev = bdev->next) {
835 if (key_is_missing(bdev->value)) {
836 monitor_read_block_device_key(mon, bdev->value->device,
837 hmp_cont_cb, NULL);
838 goto out;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200839 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200840 }
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300841
Markus Armbrustere940f542014-05-02 13:26:29 +0200842 qmp_cont(&err);
843 hmp_handle_error(mon, &err);
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300844
845out:
846 qapi_free_BlockInfoList(bdev_list);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200847}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200848
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100849void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
850{
851 qmp_system_wakeup(NULL);
852}
853
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200854void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
855{
Markus Armbrustere940f542014-05-02 13:26:29 +0200856 Error *err = NULL;
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200857
Markus Armbrustere940f542014-05-02 13:26:29 +0200858 qmp_inject_nmi(&err);
859 hmp_handle_error(mon, &err);
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200860}
Luiz Capitulino4b371562011-11-23 13:11:55 -0200861
862void hmp_set_link(Monitor *mon, const QDict *qdict)
863{
864 const char *name = qdict_get_str(qdict, "name");
865 int up = qdict_get_bool(qdict, "up");
Markus Armbrustere940f542014-05-02 13:26:29 +0200866 Error *err = NULL;
Luiz Capitulino4b371562011-11-23 13:11:55 -0200867
Markus Armbrustere940f542014-05-02 13:26:29 +0200868 qmp_set_link(name, up, &err);
869 hmp_handle_error(mon, &err);
Luiz Capitulino4b371562011-11-23 13:11:55 -0200870}
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200871
872void hmp_block_passwd(Monitor *mon, const QDict *qdict)
873{
874 const char *device = qdict_get_str(qdict, "device");
875 const char *password = qdict_get_str(qdict, "password");
Markus Armbrustere940f542014-05-02 13:26:29 +0200876 Error *err = NULL;
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200877
Markus Armbrustere940f542014-05-02 13:26:29 +0200878 qmp_block_passwd(true, device, false, NULL, password, &err);
879 hmp_handle_error(mon, &err);
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200880}
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200881
882void hmp_balloon(Monitor *mon, const QDict *qdict)
883{
884 int64_t value = qdict_get_int(qdict, "value");
Markus Armbrustere940f542014-05-02 13:26:29 +0200885 Error *err = NULL;
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200886
Markus Armbrustere940f542014-05-02 13:26:29 +0200887 qmp_balloon(value, &err);
888 if (err) {
889 monitor_printf(mon, "balloon: %s\n", error_get_pretty(err));
890 error_free(err);
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200891 }
892}
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200893
894void hmp_block_resize(Monitor *mon, const QDict *qdict)
895{
896 const char *device = qdict_get_str(qdict, "device");
897 int64_t size = qdict_get_int(qdict, "size");
Markus Armbrustere940f542014-05-02 13:26:29 +0200898 Error *err = NULL;
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200899
Markus Armbrustere940f542014-05-02 13:26:29 +0200900 qmp_block_resize(true, device, false, NULL, size, &err);
901 hmp_handle_error(mon, &err);
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200902}
Luiz Capitulino6106e242011-11-25 16:15:19 -0200903
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200904void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
905{
906 const char *device = qdict_get_str(qdict, "device");
907 const char *filename = qdict_get_str(qdict, "target");
908 const char *format = qdict_get_try_str(qdict, "format");
909 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
910 int full = qdict_get_try_bool(qdict, "full", 0);
911 enum NewImageMode mode;
Markus Armbrustere940f542014-05-02 13:26:29 +0200912 Error *err = NULL;
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200913
914 if (!filename) {
Markus Armbrustere940f542014-05-02 13:26:29 +0200915 error_set(&err, QERR_MISSING_PARAMETER, "target");
916 hmp_handle_error(mon, &err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200917 return;
918 }
919
920 if (reuse) {
921 mode = NEW_IMAGE_MODE_EXISTING;
922 } else {
923 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
924 }
925
926 qmp_drive_mirror(device, filename, !!format, format,
927 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
Paolo Bonzini08e4ed62013-01-22 09:03:13 +0100928 true, mode, false, 0, false, 0, false, 0,
Markus Armbrustere940f542014-05-02 13:26:29 +0200929 false, 0, false, 0, &err);
930 hmp_handle_error(mon, &err);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200931}
932
Stefan Hajnoczide909302013-06-26 14:11:58 +0200933void hmp_drive_backup(Monitor *mon, const QDict *qdict)
934{
935 const char *device = qdict_get_str(qdict, "device");
936 const char *filename = qdict_get_str(qdict, "target");
937 const char *format = qdict_get_try_str(qdict, "format");
938 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
939 int full = qdict_get_try_bool(qdict, "full", 0);
940 enum NewImageMode mode;
Markus Armbrustere940f542014-05-02 13:26:29 +0200941 Error *err = NULL;
Stefan Hajnoczide909302013-06-26 14:11:58 +0200942
943 if (!filename) {
Markus Armbrustere940f542014-05-02 13:26:29 +0200944 error_set(&err, QERR_MISSING_PARAMETER, "target");
945 hmp_handle_error(mon, &err);
Stefan Hajnoczide909302013-06-26 14:11:58 +0200946 return;
947 }
948
949 if (reuse) {
950 mode = NEW_IMAGE_MODE_EXISTING;
951 } else {
952 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
953 }
954
955 qmp_drive_backup(device, filename, !!format, format,
956 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
Markus Armbrustere940f542014-05-02 13:26:29 +0200957 true, mode, false, 0, false, 0, false, 0, &err);
958 hmp_handle_error(mon, &err);
Stefan Hajnoczide909302013-06-26 14:11:58 +0200959}
960
Luiz Capitulino6106e242011-11-25 16:15:19 -0200961void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
962{
963 const char *device = qdict_get_str(qdict, "device");
964 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
965 const char *format = qdict_get_try_str(qdict, "format");
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100966 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
967 enum NewImageMode mode;
Markus Armbrustere940f542014-05-02 13:26:29 +0200968 Error *err = NULL;
Luiz Capitulino6106e242011-11-25 16:15:19 -0200969
970 if (!filename) {
971 /* In the future, if 'snapshot-file' is not specified, the snapshot
972 will be taken internally. Today it's actually required. */
Markus Armbrustere940f542014-05-02 13:26:29 +0200973 error_set(&err, QERR_MISSING_PARAMETER, "snapshot-file");
974 hmp_handle_error(mon, &err);
Luiz Capitulino6106e242011-11-25 16:15:19 -0200975 return;
976 }
977
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100978 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
Benoît Canet0901f672014-01-23 21:31:38 +0100979 qmp_blockdev_snapshot_sync(true, device, false, NULL,
980 filename, false, NULL,
981 !!format, format,
Markus Armbrustere940f542014-05-02 13:26:29 +0200982 true, mode, &err);
983 hmp_handle_error(mon, &err);
Luiz Capitulino6106e242011-11-25 16:15:19 -0200984}
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200985
Wenchao Xia775ca882013-09-11 14:04:37 +0800986void hmp_snapshot_blkdev_internal(Monitor *mon, const QDict *qdict)
987{
988 const char *device = qdict_get_str(qdict, "device");
989 const char *name = qdict_get_str(qdict, "name");
Markus Armbrustere940f542014-05-02 13:26:29 +0200990 Error *err = NULL;
Wenchao Xia775ca882013-09-11 14:04:37 +0800991
Markus Armbrustere940f542014-05-02 13:26:29 +0200992 qmp_blockdev_snapshot_internal_sync(device, name, &err);
993 hmp_handle_error(mon, &err);
Wenchao Xia775ca882013-09-11 14:04:37 +0800994}
995
Wenchao Xia7a4ed2e2013-09-11 14:04:38 +0800996void hmp_snapshot_delete_blkdev_internal(Monitor *mon, const QDict *qdict)
997{
998 const char *device = qdict_get_str(qdict, "device");
999 const char *name = qdict_get_str(qdict, "name");
1000 const char *id = qdict_get_try_str(qdict, "id");
Markus Armbrustere940f542014-05-02 13:26:29 +02001001 Error *err = NULL;
Wenchao Xia7a4ed2e2013-09-11 14:04:38 +08001002
1003 qmp_blockdev_snapshot_delete_internal_sync(device, !!id, id,
Markus Armbrustere940f542014-05-02 13:26:29 +02001004 true, name, &err);
1005 hmp_handle_error(mon, &err);
Wenchao Xia7a4ed2e2013-09-11 14:04:38 +08001006}
1007
Luiz Capitulino6cdedb02011-11-27 22:54:09 -02001008void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
1009{
1010 qmp_migrate_cancel(NULL);
1011}
Luiz Capitulino4f0a9932011-11-27 23:18:01 -02001012
1013void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
1014{
1015 double value = qdict_get_double(qdict, "value");
1016 qmp_migrate_set_downtime(value, NULL);
1017}
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001018
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +03001019void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
1020{
1021 int64_t value = qdict_get_int(qdict, "value");
1022 Error *err = NULL;
1023
1024 qmp_migrate_set_cache_size(value, &err);
1025 if (err) {
1026 monitor_printf(mon, "%s\n", error_get_pretty(err));
1027 error_free(err);
1028 return;
1029 }
1030}
1031
Luiz Capitulino3dc85382011-11-28 11:59:37 -02001032void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
1033{
1034 int64_t value = qdict_get_int(qdict, "value");
1035 qmp_migrate_set_speed(value, NULL);
1036}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -02001037
Orit Wasserman00458432012-08-06 21:42:48 +03001038void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
1039{
1040 const char *cap = qdict_get_str(qdict, "capability");
1041 bool state = qdict_get_bool(qdict, "state");
1042 Error *err = NULL;
1043 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
1044 int i;
1045
1046 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
1047 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
1048 caps->value = g_malloc0(sizeof(*caps->value));
1049 caps->value->capability = i;
1050 caps->value->state = state;
1051 caps->next = NULL;
1052 qmp_migrate_set_capabilities(caps, &err);
1053 break;
1054 }
1055 }
1056
1057 if (i == MIGRATION_CAPABILITY_MAX) {
1058 error_set(&err, QERR_INVALID_PARAMETER, cap);
1059 }
1060
1061 qapi_free_MigrationCapabilityStatusList(caps);
1062
1063 if (err) {
Orit Wassermana31ca012013-01-31 09:12:19 +02001064 monitor_printf(mon, "migrate_set_capability: %s\n",
Orit Wasserman00458432012-08-06 21:42:48 +03001065 error_get_pretty(err));
1066 error_free(err);
1067 }
1068}
1069
Luiz Capitulinofbf796f2011-12-07 11:17:51 -02001070void hmp_set_password(Monitor *mon, const QDict *qdict)
1071{
1072 const char *protocol = qdict_get_str(qdict, "protocol");
1073 const char *password = qdict_get_str(qdict, "password");
1074 const char *connected = qdict_get_try_str(qdict, "connected");
1075 Error *err = NULL;
1076
1077 qmp_set_password(protocol, password, !!connected, connected, &err);
1078 hmp_handle_error(mon, &err);
1079}
Luiz Capitulino9ad53722011-12-07 11:47:57 -02001080
1081void hmp_expire_password(Monitor *mon, const QDict *qdict)
1082{
1083 const char *protocol = qdict_get_str(qdict, "protocol");
1084 const char *whenstr = qdict_get_str(qdict, "time");
1085 Error *err = NULL;
1086
1087 qmp_expire_password(protocol, whenstr, &err);
1088 hmp_handle_error(mon, &err);
1089}
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -02001090
1091void hmp_eject(Monitor *mon, const QDict *qdict)
1092{
1093 int force = qdict_get_try_bool(qdict, "force", 0);
1094 const char *device = qdict_get_str(qdict, "device");
1095 Error *err = NULL;
1096
1097 qmp_eject(device, true, force, &err);
1098 hmp_handle_error(mon, &err);
1099}
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001100
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01001101static void hmp_change_read_arg(void *opaque, const char *password,
1102 void *readline_opaque)
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001103{
1104 qmp_change_vnc_password(password, NULL);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +01001105 monitor_read_command(opaque, 1);
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001106}
1107
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001108void hmp_change(Monitor *mon, const QDict *qdict)
1109{
1110 const char *device = qdict_get_str(qdict, "device");
1111 const char *target = qdict_get_str(qdict, "target");
1112 const char *arg = qdict_get_try_str(qdict, "arg");
1113 Error *err = NULL;
1114
1115 if (strcmp(device, "vnc") == 0 &&
1116 (strcmp(target, "passwd") == 0 ||
1117 strcmp(target, "password") == 0)) {
1118 if (!arg) {
1119 monitor_read_password(mon, hmp_change_read_arg, NULL);
1120 return;
1121 }
1122 }
1123
1124 qmp_change(device, target, !!arg, arg, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001125 if (err &&
Luiz Capitulinoab878dd2012-08-06 15:55:22 -03001126 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
Luiz Capitulinoeef5ad12012-08-06 15:49:34 -03001127 error_free(err);
1128 monitor_read_block_device_key(mon, device, NULL, NULL);
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001129 return;
1130 }
1131 hmp_handle_error(mon, &err);
1132}
Luiz Capitulino80047da2011-12-14 16:49:14 -02001133
1134void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1135{
1136 Error *err = NULL;
1137
1138 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1139 qdict_get_int(qdict, "bps"),
1140 qdict_get_int(qdict, "bps_rd"),
1141 qdict_get_int(qdict, "bps_wr"),
1142 qdict_get_int(qdict, "iops"),
1143 qdict_get_int(qdict, "iops_rd"),
Benoît Canet3e9fab62013-09-02 14:14:40 +02001144 qdict_get_int(qdict, "iops_wr"),
1145 false, /* no burst max via HMP */
1146 0,
1147 false,
1148 0,
1149 false,
1150 0,
1151 false,
1152 0,
1153 false,
1154 0,
1155 false,
Benoît Canet2024c1d2013-09-02 14:14:41 +02001156 0,
1157 false, /* No default I/O size */
Benoît Canet3e9fab62013-09-02 14:14:40 +02001158 0, &err);
Luiz Capitulino80047da2011-12-14 16:49:14 -02001159 hmp_handle_error(mon, &err);
1160}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001161
1162void hmp_block_stream(Monitor *mon, const QDict *qdict)
1163{
1164 Error *error = NULL;
1165 const char *device = qdict_get_str(qdict, "device");
1166 const char *base = qdict_get_try_str(qdict, "base");
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001167 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001168
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001169 qmp_block_stream(device, base != NULL, base,
Paolo Bonzini1d809092012-09-28 17:22:59 +02001170 qdict_haskey(qdict, "speed"), speed,
Anthony Liguori46663e52013-09-17 11:10:47 -05001171 true, BLOCKDEV_ON_ERROR_REPORT, &error);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001172
1173 hmp_handle_error(mon, &error);
1174}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001175
1176void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1177{
1178 Error *error = NULL;
1179 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzinic6db2392012-05-08 16:51:56 +02001180 int64_t value = qdict_get_int(qdict, "speed");
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001181
1182 qmp_block_job_set_speed(device, value, &error);
1183
1184 hmp_handle_error(mon, &error);
1185}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001186
1187void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1188{
1189 Error *error = NULL;
1190 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001191 bool force = qdict_get_try_bool(qdict, "force", 0);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001192
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001193 qmp_block_job_cancel(device, true, force, &error);
1194
1195 hmp_handle_error(mon, &error);
1196}
1197
1198void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1199{
1200 Error *error = NULL;
1201 const char *device = qdict_get_str(qdict, "device");
1202
1203 qmp_block_job_pause(device, &error);
1204
1205 hmp_handle_error(mon, &error);
1206}
1207
1208void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1209{
1210 Error *error = NULL;
1211 const char *device = qdict_get_str(qdict, "device");
1212
1213 qmp_block_job_resume(device, &error);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001214
1215 hmp_handle_error(mon, &error);
1216}
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001217
Paolo Bonziniaeae8832012-10-18 16:49:21 +02001218void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1219{
1220 Error *error = NULL;
1221 const char *device = qdict_get_str(qdict, "device");
1222
1223 qmp_block_job_complete(device, &error);
1224
1225 hmp_handle_error(mon, &error);
1226}
1227
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001228typedef struct MigrationStatus
1229{
1230 QEMUTimer *timer;
1231 Monitor *mon;
1232 bool is_block_migration;
1233} MigrationStatus;
1234
1235static void hmp_migrate_status_cb(void *opaque)
1236{
1237 MigrationStatus *status = opaque;
1238 MigrationInfo *info;
1239
1240 info = qmp_query_migrate(NULL);
Soramichi AKIYAMAdde3a212014-01-27 19:46:11 +09001241 if (!info->has_status || strcmp(info->status, "active") == 0 ||
1242 strcmp(info->status, "setup") == 0) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001243 if (info->has_disk) {
1244 int progress;
1245
1246 if (info->disk->remaining) {
1247 progress = info->disk->transferred * 100 / info->disk->total;
1248 } else {
1249 progress = 100;
1250 }
1251
1252 monitor_printf(status->mon, "Completed %d %%\r", progress);
1253 monitor_flush(status->mon);
1254 }
1255
Alex Blighbc72ad62013-08-21 16:03:08 +01001256 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001257 } else {
1258 if (status->is_block_migration) {
1259 monitor_printf(status->mon, "\n");
1260 }
1261 monitor_resume(status->mon);
Alex Blighbc72ad62013-08-21 16:03:08 +01001262 timer_del(status->timer);
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001263 g_free(status);
1264 }
1265
1266 qapi_free_MigrationInfo(info);
1267}
1268
1269void hmp_migrate(Monitor *mon, const QDict *qdict)
1270{
1271 int detach = qdict_get_try_bool(qdict, "detach", 0);
1272 int blk = qdict_get_try_bool(qdict, "blk", 0);
1273 int inc = qdict_get_try_bool(qdict, "inc", 0);
1274 const char *uri = qdict_get_str(qdict, "uri");
1275 Error *err = NULL;
1276
1277 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1278 if (err) {
1279 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1280 error_free(err);
1281 return;
1282 }
1283
1284 if (!detach) {
1285 MigrationStatus *status;
1286
1287 if (monitor_suspend(mon) < 0) {
1288 monitor_printf(mon, "terminal does not allow synchronous "
1289 "migration, continuing detached\n");
1290 return;
1291 }
1292
1293 status = g_malloc0(sizeof(*status));
1294 status->mon = mon;
1295 status->is_block_migration = blk || inc;
Alex Blighbc72ad62013-08-21 16:03:08 +01001296 status->timer = timer_new_ms(QEMU_CLOCK_REALTIME, hmp_migrate_status_cb,
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001297 status);
Alex Blighbc72ad62013-08-21 16:03:08 +01001298 timer_mod(status->timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001299 }
1300}
Luiz Capitulinoa15fef22012-03-29 12:38:50 -03001301
1302void hmp_device_del(Monitor *mon, const QDict *qdict)
1303{
1304 const char *id = qdict_get_str(qdict, "id");
1305 Error *err = NULL;
1306
1307 qmp_device_del(id, &err);
1308 hmp_handle_error(mon, &err);
1309}
Wen Congyang783e9b42012-05-07 12:10:47 +08001310
1311void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1312{
Markus Armbrustere940f542014-05-02 13:26:29 +02001313 Error *err = NULL;
Wen Congyang783e9b42012-05-07 12:10:47 +08001314 int paging = qdict_get_try_bool(qdict, "paging", 0);
Qiao Nuohan1b7a0f72014-04-17 16:15:07 +08001315 int zlib = qdict_get_try_bool(qdict, "zlib", 0);
1316 int lzo = qdict_get_try_bool(qdict, "lzo", 0);
1317 int snappy = qdict_get_try_bool(qdict, "snappy", 0);
Luiz Capitulino75363762012-09-21 13:53:00 -03001318 const char *file = qdict_get_str(qdict, "filename");
Wen Congyang783e9b42012-05-07 12:10:47 +08001319 bool has_begin = qdict_haskey(qdict, "begin");
1320 bool has_length = qdict_haskey(qdict, "length");
1321 int64_t begin = 0;
1322 int64_t length = 0;
qiaonuohanb53ccc32014-02-18 14:11:36 +08001323 enum DumpGuestMemoryFormat dump_format = DUMP_GUEST_MEMORY_FORMAT_ELF;
Luiz Capitulino75363762012-09-21 13:53:00 -03001324 char *prot;
Wen Congyang783e9b42012-05-07 12:10:47 +08001325
Qiao Nuohan1b7a0f72014-04-17 16:15:07 +08001326 if (zlib + lzo + snappy > 1) {
Markus Armbrustere940f542014-05-02 13:26:29 +02001327 error_setg(&err, "only one of '-z|-l|-s' can be set");
1328 hmp_handle_error(mon, &err);
Qiao Nuohan1b7a0f72014-04-17 16:15:07 +08001329 return;
1330 }
1331
1332 if (zlib) {
1333 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_ZLIB;
1334 }
1335
1336 if (lzo) {
1337 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_LZO;
1338 }
1339
1340 if (snappy) {
1341 dump_format = DUMP_GUEST_MEMORY_FORMAT_KDUMP_SNAPPY;
1342 }
1343
Wen Congyang783e9b42012-05-07 12:10:47 +08001344 if (has_begin) {
1345 begin = qdict_get_int(qdict, "begin");
1346 }
1347 if (has_length) {
1348 length = qdict_get_int(qdict, "length");
1349 }
1350
Luiz Capitulino75363762012-09-21 13:53:00 -03001351 prot = g_strconcat("file:", file, NULL);
1352
1353 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
Markus Armbrustere940f542014-05-02 13:26:29 +02001354 true, dump_format, &err);
1355 hmp_handle_error(mon, &err);
Luiz Capitulino75363762012-09-21 13:53:00 -03001356 g_free(prot);
Wen Congyang783e9b42012-05-07 12:10:47 +08001357}
Luiz Capitulino928059a2012-04-18 17:34:15 -03001358
1359void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1360{
1361 Error *err = NULL;
1362 QemuOpts *opts;
1363
1364 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001365 if (err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001366 goto out;
1367 }
1368
1369 netdev_add(opts, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001370 if (err) {
Luiz Capitulino928059a2012-04-18 17:34:15 -03001371 qemu_opts_del(opts);
1372 }
1373
1374out:
1375 hmp_handle_error(mon, &err);
1376}
Luiz Capitulino5f964152012-04-16 14:36:32 -03001377
1378void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1379{
1380 const char *id = qdict_get_str(qdict, "id");
1381 Error *err = NULL;
1382
1383 qmp_netdev_del(id, &err);
1384 hmp_handle_error(mon, &err);
1385}
Corey Bryant208c9d12012-06-22 14:36:09 -04001386
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +01001387void hmp_object_add(Monitor *mon, const QDict *qdict)
1388{
1389 Error *err = NULL;
1390 QemuOpts *opts;
1391 char *type = NULL;
1392 char *id = NULL;
1393 void *dummy = NULL;
1394 OptsVisitor *ov;
1395 QDict *pdict;
1396
1397 opts = qemu_opts_from_qdict(qemu_find_opts("object"), qdict, &err);
1398 if (err) {
1399 goto out;
1400 }
1401
1402 ov = opts_visitor_new(opts);
1403 pdict = qdict_clone_shallow(qdict);
1404
1405 visit_start_struct(opts_get_visitor(ov), &dummy, NULL, NULL, 0, &err);
1406 if (err) {
1407 goto out_clean;
1408 }
1409
1410 qdict_del(pdict, "qom-type");
1411 visit_type_str(opts_get_visitor(ov), &type, "qom-type", &err);
1412 if (err) {
1413 goto out_clean;
1414 }
1415
1416 qdict_del(pdict, "id");
1417 visit_type_str(opts_get_visitor(ov), &id, "id", &err);
1418 if (err) {
1419 goto out_clean;
1420 }
1421
1422 object_add(type, id, pdict, opts_get_visitor(ov), &err);
1423 if (err) {
1424 goto out_clean;
1425 }
1426 visit_end_struct(opts_get_visitor(ov), &err);
1427 if (err) {
1428 qmp_object_del(id, NULL);
1429 }
1430
1431out_clean:
1432 opts_visitor_cleanup(ov);
1433
1434 QDECREF(pdict);
1435 qemu_opts_del(opts);
1436 g_free(id);
1437 g_free(type);
1438 g_free(dummy);
1439
1440out:
1441 hmp_handle_error(mon, &err);
1442}
1443
Corey Bryant208c9d12012-06-22 14:36:09 -04001444void hmp_getfd(Monitor *mon, const QDict *qdict)
1445{
1446 const char *fdname = qdict_get_str(qdict, "fdname");
Markus Armbrustere940f542014-05-02 13:26:29 +02001447 Error *err = NULL;
Corey Bryant208c9d12012-06-22 14:36:09 -04001448
Markus Armbrustere940f542014-05-02 13:26:29 +02001449 qmp_getfd(fdname, &err);
1450 hmp_handle_error(mon, &err);
Corey Bryant208c9d12012-06-22 14:36:09 -04001451}
1452
1453void hmp_closefd(Monitor *mon, const QDict *qdict)
1454{
1455 const char *fdname = qdict_get_str(qdict, "fdname");
Markus Armbrustere940f542014-05-02 13:26:29 +02001456 Error *err = NULL;
Corey Bryant208c9d12012-06-22 14:36:09 -04001457
Markus Armbrustere940f542014-05-02 13:26:29 +02001458 qmp_closefd(fdname, &err);
1459 hmp_handle_error(mon, &err);
Corey Bryant208c9d12012-06-22 14:36:09 -04001460}
Amos Konge4c8f002012-08-31 10:56:26 +08001461
1462void hmp_send_key(Monitor *mon, const QDict *qdict)
1463{
1464 const char *keys = qdict_get_str(qdict, "keys");
Luiz Capitulino9f328972012-09-20 14:19:47 -03001465 KeyValueList *keylist, *head = NULL, *tmp = NULL;
Amos Konge4c8f002012-08-31 10:56:26 +08001466 int has_hold_time = qdict_haskey(qdict, "hold-time");
1467 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1468 Error *err = NULL;
1469 char keyname_buf[16];
1470 char *separator;
Luiz Capitulino9f328972012-09-20 14:19:47 -03001471 int keyname_len;
Amos Konge4c8f002012-08-31 10:56:26 +08001472
1473 while (1) {
1474 separator = strchr(keys, '-');
1475 keyname_len = separator ? separator - keys : strlen(keys);
1476 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1477
1478 /* Be compatible with old interface, convert user inputted "<" */
1479 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1480 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1481 keyname_len = 4;
1482 }
1483 keyname_buf[keyname_len] = 0;
1484
Amos Konge4c8f002012-08-31 10:56:26 +08001485 keylist = g_malloc0(sizeof(*keylist));
Luiz Capitulino9f328972012-09-20 14:19:47 -03001486 keylist->value = g_malloc0(sizeof(*keylist->value));
Amos Konge4c8f002012-08-31 10:56:26 +08001487
1488 if (!head) {
1489 head = keylist;
1490 }
1491 if (tmp) {
1492 tmp->next = keylist;
1493 }
1494 tmp = keylist;
1495
Luiz Capitulino9f328972012-09-20 14:19:47 -03001496 if (strstart(keyname_buf, "0x", NULL)) {
1497 char *endp;
1498 int value = strtoul(keyname_buf, &endp, 0);
1499 if (*endp != '\0') {
1500 goto err_out;
1501 }
1502 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1503 keylist->value->number = value;
1504 } else {
1505 int idx = index_from_key(keyname_buf);
1506 if (idx == Q_KEY_CODE_MAX) {
1507 goto err_out;
1508 }
1509 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1510 keylist->value->qcode = idx;
1511 }
1512
Amos Konge4c8f002012-08-31 10:56:26 +08001513 if (!separator) {
1514 break;
1515 }
1516 keys = separator + 1;
1517 }
1518
Luiz Capitulino9f328972012-09-20 14:19:47 -03001519 qmp_send_key(head, has_hold_time, hold_time, &err);
Amos Konge4c8f002012-08-31 10:56:26 +08001520 hmp_handle_error(mon, &err);
Luiz Capitulino9f328972012-09-20 14:19:47 -03001521
1522out:
1523 qapi_free_KeyValueList(head);
1524 return;
1525
1526err_out:
1527 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1528 goto out;
Amos Konge4c8f002012-08-31 10:56:26 +08001529}
Luiz Capitulinoad39cf62012-05-24 13:48:23 -03001530
1531void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1532{
1533 const char *filename = qdict_get_str(qdict, "filename");
1534 Error *err = NULL;
1535
1536 qmp_screendump(filename, &err);
1537 hmp_handle_error(mon, &err);
1538}
Paolo Bonzini40577252012-08-23 11:53:04 +02001539
1540void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1541{
1542 const char *uri = qdict_get_str(qdict, "uri");
1543 int writable = qdict_get_try_bool(qdict, "writable", 0);
1544 int all = qdict_get_try_bool(qdict, "all", 0);
1545 Error *local_err = NULL;
1546 BlockInfoList *block_list, *info;
1547 SocketAddress *addr;
1548
1549 if (writable && !all) {
1550 error_setg(&local_err, "-w only valid together with -a");
1551 goto exit;
1552 }
1553
1554 /* First check if the address is valid and start the server. */
1555 addr = socket_parse(uri, &local_err);
1556 if (local_err != NULL) {
1557 goto exit;
1558 }
1559
1560 qmp_nbd_server_start(addr, &local_err);
1561 qapi_free_SocketAddress(addr);
1562 if (local_err != NULL) {
1563 goto exit;
1564 }
1565
1566 if (!all) {
1567 return;
1568 }
1569
1570 /* Then try adding all block devices. If one fails, close all and
1571 * exit.
1572 */
1573 block_list = qmp_query_block(NULL);
1574
1575 for (info = block_list; info; info = info->next) {
1576 if (!info->value->has_inserted) {
1577 continue;
1578 }
1579
1580 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1581
1582 if (local_err != NULL) {
1583 qmp_nbd_server_stop(NULL);
1584 break;
1585 }
1586 }
1587
1588 qapi_free_BlockInfoList(block_list);
1589
1590exit:
1591 hmp_handle_error(mon, &local_err);
1592}
1593
1594void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1595{
1596 const char *device = qdict_get_str(qdict, "device");
1597 int writable = qdict_get_try_bool(qdict, "writable", 0);
1598 Error *local_err = NULL;
1599
1600 qmp_nbd_server_add(device, true, writable, &local_err);
1601
1602 if (local_err != NULL) {
1603 hmp_handle_error(mon, &local_err);
1604 }
1605}
1606
1607void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1608{
Markus Armbrustere940f542014-05-02 13:26:29 +02001609 Error *err = NULL;
Paolo Bonzini40577252012-08-23 11:53:04 +02001610
Markus Armbrustere940f542014-05-02 13:26:29 +02001611 qmp_nbd_server_stop(&err);
1612 hmp_handle_error(mon, &err);
Paolo Bonzini40577252012-08-23 11:53:04 +02001613}
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001614
Jason J. Herneabf23322013-12-11 13:24:14 -05001615void hmp_cpu_add(Monitor *mon, const QDict *qdict)
1616{
1617 int cpuid;
1618 Error *err = NULL;
1619
1620 cpuid = qdict_get_int(qdict, "id");
1621 qmp_cpu_add(cpuid, &err);
1622 hmp_handle_error(mon, &err);
1623}
1624
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001625void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1626{
1627 const char *args = qdict_get_str(qdict, "args");
1628 Error *err = NULL;
1629 QemuOpts *opts;
1630
1631 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1632 if (opts == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001633 error_setg(&err, "Parsing chardev args failed");
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001634 } else {
1635 qemu_chr_new_from_opts(opts, NULL, &err);
1636 }
1637 hmp_handle_error(mon, &err);
1638}
1639
1640void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1641{
1642 Error *local_err = NULL;
1643
1644 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1645 hmp_handle_error(mon, &local_err);
1646}
Kevin Wolf587da2c2013-06-05 14:19:41 +02001647
1648void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1649{
1650 BlockDriverState *bs;
1651 const char* device = qdict_get_str(qdict, "device");
1652 const char* command = qdict_get_str(qdict, "command");
1653 Error *err = NULL;
1654
1655 bs = bdrv_find(device);
1656 if (bs) {
1657 qemuio_command(bs, command);
1658 } else {
1659 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1660 }
1661
1662 hmp_handle_error(mon, &err);
1663}
Paolo Bonziniab2d0532013-12-20 23:21:09 +01001664
1665void hmp_object_del(Monitor *mon, const QDict *qdict)
1666{
1667 const char *id = qdict_get_str(qdict, "id");
1668 Error *err = NULL;
1669
1670 qmp_object_del(id, &err);
1671 hmp_handle_error(mon, &err);
1672}