blob: 64e0baac1681df3124899fa0721f0f73d0506820 [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 Bonzini28ecbae2012-11-28 12:06:30 +010024#include "ui/console.h"
Kevin Wolf587da2c2013-06-05 14:19:41 +020025#include "qemu-io.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050026
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -020027static void hmp_handle_error(Monitor *mon, Error **errp)
28{
29 if (error_is_set(errp)) {
30 monitor_printf(mon, "%s\n", error_get_pretty(*errp));
31 error_free(*errp);
32 }
33}
34
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080035void hmp_info_name(Monitor *mon, const QDict *qdict)
Anthony Liguori48a32be2011-09-02 12:34:48 -050036{
37 NameInfo *info;
38
39 info = qmp_query_name(NULL);
40 if (info->has_name) {
41 monitor_printf(mon, "%s\n", info->name);
42 }
43 qapi_free_NameInfo(info);
44}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030045
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080046void hmp_info_version(Monitor *mon, const QDict *qdict)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030047{
48 VersionInfo *info;
49
50 info = qmp_query_version(NULL);
51
52 monitor_printf(mon, "%" PRId64 ".%" PRId64 ".%" PRId64 "%s\n",
53 info->qemu.major, info->qemu.minor, info->qemu.micro,
54 info->package);
55
56 qapi_free_VersionInfo(info);
57}
Luiz Capitulino292a2602011-09-12 15:10:53 -030058
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080059void hmp_info_kvm(Monitor *mon, const QDict *qdict)
Luiz Capitulino292a2602011-09-12 15:10:53 -030060{
61 KvmInfo *info;
62
63 info = qmp_query_kvm(NULL);
64 monitor_printf(mon, "kvm support: ");
65 if (info->present) {
66 monitor_printf(mon, "%s\n", info->enabled ? "enabled" : "disabled");
67 } else {
68 monitor_printf(mon, "not compiled\n");
69 }
70
71 qapi_free_KvmInfo(info);
72}
73
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080074void hmp_info_status(Monitor *mon, const QDict *qdict)
Luiz Capitulino1fa9a5e2011-09-12 17:54:20 -030075{
76 StatusInfo *info;
77
78 info = qmp_query_status(NULL);
79
80 monitor_printf(mon, "VM status: %s%s",
81 info->running ? "running" : "paused",
82 info->singlestep ? " (single step mode)" : "");
83
84 if (!info->running && info->status != RUN_STATE_PAUSED) {
85 monitor_printf(mon, " (%s)", RunState_lookup[info->status]);
86 }
87
88 monitor_printf(mon, "\n");
89
90 qapi_free_StatusInfo(info);
91}
92
Wenchao Xia84f2d0e2013-01-14 14:06:25 +080093void hmp_info_uuid(Monitor *mon, const QDict *qdict)
Luiz Capitulinoefab7672011-09-13 17:16:25 -030094{
95 UuidInfo *info;
96
97 info = qmp_query_uuid(NULL);
98 monitor_printf(mon, "%s\n", info->UUID);
99 qapi_free_UuidInfo(info);
100}
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300101
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800102void hmp_info_chardev(Monitor *mon, const QDict *qdict)
Luiz Capitulinoc5a415a2011-09-14 16:05:49 -0300103{
104 ChardevInfoList *char_info, *info;
105
106 char_info = qmp_query_chardev(NULL);
107 for (info = char_info; info; info = info->next) {
108 monitor_printf(mon, "%s: filename=%s\n", info->value->label,
109 info->value->filename);
110 }
111
112 qapi_free_ChardevInfoList(char_info);
113}
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300114
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800115void hmp_info_mice(Monitor *mon, const QDict *qdict)
Luiz Capitulinoe235cec2011-09-21 15:29:55 -0300116{
117 MouseInfoList *mice_list, *mouse;
118
119 mice_list = qmp_query_mice(NULL);
120 if (!mice_list) {
121 monitor_printf(mon, "No mouse devices connected\n");
122 return;
123 }
124
125 for (mouse = mice_list; mouse; mouse = mouse->next) {
126 monitor_printf(mon, "%c Mouse #%" PRId64 ": %s%s\n",
127 mouse->value->current ? '*' : ' ',
128 mouse->value->index, mouse->value->name,
129 mouse->value->absolute ? " (absolute)" : "");
130 }
131
132 qapi_free_MouseInfoList(mice_list);
133}
134
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800135void hmp_info_migrate(Monitor *mon, const QDict *qdict)
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300136{
137 MigrationInfo *info;
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300138 MigrationCapabilityStatusList *caps, *cap;
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300139
140 info = qmp_query_migrate(NULL);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300141 caps = qmp_query_migrate_capabilities(NULL);
142
143 /* do not display parameters during setup */
144 if (info->has_status && caps) {
145 monitor_printf(mon, "capabilities: ");
146 for (cap = caps; cap; cap = cap->next) {
147 monitor_printf(mon, "%s: %s ",
148 MigrationCapability_lookup[cap->value->capability],
149 cap->value->state ? "on" : "off");
150 }
151 monitor_printf(mon, "\n");
152 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300153
154 if (info->has_status) {
155 monitor_printf(mon, "Migration status: %s\n", info->status);
Juan Quintela7aa939a2012-08-18 13:17:10 +0200156 monitor_printf(mon, "total time: %" PRIu64 " milliseconds\n",
157 info->total_time);
Juan Quintela2c52ddf2012-08-13 09:53:12 +0200158 if (info->has_expected_downtime) {
159 monitor_printf(mon, "expected downtime: %" PRIu64 " milliseconds\n",
160 info->expected_downtime);
161 }
Juan Quintela9c5a9fc2012-08-13 09:35:16 +0200162 if (info->has_downtime) {
163 monitor_printf(mon, "downtime: %" PRIu64 " milliseconds\n",
164 info->downtime);
165 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300166 }
167
168 if (info->has_ram) {
169 monitor_printf(mon, "transferred ram: %" PRIu64 " kbytes\n",
170 info->ram->transferred >> 10);
171 monitor_printf(mon, "remaining ram: %" PRIu64 " kbytes\n",
172 info->ram->remaining >> 10);
173 monitor_printf(mon, "total ram: %" PRIu64 " kbytes\n",
174 info->ram->total >> 10);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300175 monitor_printf(mon, "duplicate: %" PRIu64 " pages\n",
176 info->ram->duplicate);
Peter Lievenf1c72792013-03-26 10:58:37 +0100177 monitor_printf(mon, "skipped: %" PRIu64 " pages\n",
178 info->ram->skipped);
Orit Wasserman004d4c12012-08-06 21:42:56 +0300179 monitor_printf(mon, "normal: %" PRIu64 " pages\n",
180 info->ram->normal);
181 monitor_printf(mon, "normal bytes: %" PRIu64 " kbytes\n",
182 info->ram->normal_bytes >> 10);
Juan Quintela8d017192012-08-13 12:31:25 +0200183 if (info->ram->dirty_pages_rate) {
184 monitor_printf(mon, "dirty pages rate: %" PRIu64 " pages\n",
185 info->ram->dirty_pages_rate);
186 }
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300187 }
188
189 if (info->has_disk) {
190 monitor_printf(mon, "transferred disk: %" PRIu64 " kbytes\n",
191 info->disk->transferred >> 10);
192 monitor_printf(mon, "remaining disk: %" PRIu64 " kbytes\n",
193 info->disk->remaining >> 10);
194 monitor_printf(mon, "total disk: %" PRIu64 " kbytes\n",
195 info->disk->total >> 10);
196 }
197
Orit Wassermanf36d55a2012-08-06 21:42:57 +0300198 if (info->has_xbzrle_cache) {
199 monitor_printf(mon, "cache size: %" PRIu64 " bytes\n",
200 info->xbzrle_cache->cache_size);
201 monitor_printf(mon, "xbzrle transferred: %" PRIu64 " kbytes\n",
202 info->xbzrle_cache->bytes >> 10);
203 monitor_printf(mon, "xbzrle pages: %" PRIu64 " pages\n",
204 info->xbzrle_cache->pages);
205 monitor_printf(mon, "xbzrle cache miss: %" PRIu64 "\n",
206 info->xbzrle_cache->cache_miss);
207 monitor_printf(mon, "xbzrle overflow : %" PRIu64 "\n",
208 info->xbzrle_cache->overflow);
209 }
210
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300211 qapi_free_MigrationInfo(info);
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300212 qapi_free_MigrationCapabilityStatusList(caps);
213}
214
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800215void hmp_info_migrate_capabilities(Monitor *mon, const QDict *qdict)
Orit Wassermanbbf6da32012-08-06 21:42:47 +0300216{
217 MigrationCapabilityStatusList *caps, *cap;
218
219 caps = qmp_query_migrate_capabilities(NULL);
220
221 if (caps) {
222 monitor_printf(mon, "capabilities: ");
223 for (cap = caps; cap; cap = cap->next) {
224 monitor_printf(mon, "%s: %s ",
225 MigrationCapability_lookup[cap->value->capability],
226 cap->value->state ? "on" : "off");
227 }
228 monitor_printf(mon, "\n");
229 }
230
231 qapi_free_MigrationCapabilityStatusList(caps);
Luiz Capitulino791e7c82011-09-13 17:37:16 -0300232}
233
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800234void hmp_info_migrate_cache_size(Monitor *mon, const QDict *qdict)
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300235{
236 monitor_printf(mon, "xbzrel cache size: %" PRId64 " kbytes\n",
237 qmp_query_migrate_cache_size(NULL) >> 10);
238}
239
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800240void hmp_info_cpus(Monitor *mon, const QDict *qdict)
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300241{
242 CpuInfoList *cpu_list, *cpu;
243
244 cpu_list = qmp_query_cpus(NULL);
245
246 for (cpu = cpu_list; cpu; cpu = cpu->next) {
247 int active = ' ';
248
249 if (cpu->value->CPU == monitor_get_cpu_index()) {
250 active = '*';
251 }
252
Aurelien Jarno852bef02012-10-19 23:19:19 +0200253 monitor_printf(mon, "%c CPU #%" PRId64 ":", active, cpu->value->CPU);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300254
255 if (cpu->value->has_pc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200256 monitor_printf(mon, " pc=0x%016" PRIx64, cpu->value->pc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300257 }
258 if (cpu->value->has_nip) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200259 monitor_printf(mon, " nip=0x%016" PRIx64, cpu->value->nip);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300260 }
261 if (cpu->value->has_npc) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200262 monitor_printf(mon, " npc=0x%016" PRIx64, cpu->value->npc);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300263 }
264 if (cpu->value->has_PC) {
Aurelien Jarno852bef02012-10-19 23:19:19 +0200265 monitor_printf(mon, " PC=0x%016" PRIx64, cpu->value->PC);
Luiz Capitulinode0b36b2011-09-21 16:38:35 -0300266 }
267
268 if (cpu->value->halted) {
269 monitor_printf(mon, " (halted)");
270 }
271
272 monitor_printf(mon, " thread_id=%" PRId64 "\n", cpu->value->thread_id);
273 }
274
275 qapi_free_CpuInfoList(cpu_list);
276}
277
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800278void hmp_info_block(Monitor *mon, const QDict *qdict)
Luiz Capitulinob2023812011-09-21 17:16:47 -0300279{
280 BlockInfoList *block_list, *info;
281
282 block_list = qmp_query_block(NULL);
283
284 for (info = block_list; info; info = info->next) {
285 monitor_printf(mon, "%s: removable=%d",
286 info->value->device, info->value->removable);
287
288 if (info->value->removable) {
289 monitor_printf(mon, " locked=%d", info->value->locked);
290 monitor_printf(mon, " tray-open=%d", info->value->tray_open);
291 }
292
293 if (info->value->has_io_status) {
294 monitor_printf(mon, " io-status=%s",
295 BlockDeviceIoStatus_lookup[info->value->io_status]);
296 }
297
298 if (info->value->has_inserted) {
299 monitor_printf(mon, " file=");
300 monitor_print_filename(mon, info->value->inserted->file);
301
302 if (info->value->inserted->has_backing_file) {
303 monitor_printf(mon, " backing_file=");
304 monitor_print_filename(mon, info->value->inserted->backing_file);
Benoît Canet75115d92012-08-02 10:22:49 +0200305 monitor_printf(mon, " backing_file_depth=%" PRId64,
306 info->value->inserted->backing_file_depth);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300307 }
308 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
309 info->value->inserted->ro,
310 info->value->inserted->drv,
311 info->value->inserted->encrypted);
Zhi Yong Wu727f0052011-11-08 13:00:31 +0800312
313 monitor_printf(mon, " bps=%" PRId64 " bps_rd=%" PRId64
314 " bps_wr=%" PRId64 " iops=%" PRId64
315 " iops_rd=%" PRId64 " iops_wr=%" PRId64,
316 info->value->inserted->bps,
317 info->value->inserted->bps_rd,
318 info->value->inserted->bps_wr,
319 info->value->inserted->iops,
320 info->value->inserted->iops_rd,
321 info->value->inserted->iops_wr);
Luiz Capitulinob2023812011-09-21 17:16:47 -0300322 } else {
323 monitor_printf(mon, " [not inserted]");
324 }
325
326 monitor_printf(mon, "\n");
327 }
328
329 qapi_free_BlockInfoList(block_list);
330}
331
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800332void hmp_info_blockstats(Monitor *mon, const QDict *qdict)
Luiz Capitulinof11f57e2011-09-22 15:56:36 -0300333{
334 BlockStatsList *stats_list, *stats;
335
336 stats_list = qmp_query_blockstats(NULL);
337
338 for (stats = stats_list; stats; stats = stats->next) {
339 if (!stats->value->has_device) {
340 continue;
341 }
342
343 monitor_printf(mon, "%s:", stats->value->device);
344 monitor_printf(mon, " rd_bytes=%" PRId64
345 " wr_bytes=%" PRId64
346 " rd_operations=%" PRId64
347 " wr_operations=%" PRId64
348 " flush_operations=%" PRId64
349 " wr_total_time_ns=%" PRId64
350 " rd_total_time_ns=%" PRId64
351 " flush_total_time_ns=%" PRId64
352 "\n",
353 stats->value->stats->rd_bytes,
354 stats->value->stats->wr_bytes,
355 stats->value->stats->rd_operations,
356 stats->value->stats->wr_operations,
357 stats->value->stats->flush_operations,
358 stats->value->stats->wr_total_time_ns,
359 stats->value->stats->rd_total_time_ns,
360 stats->value->stats->flush_total_time_ns);
361 }
362
363 qapi_free_BlockStatsList(stats_list);
364}
365
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800366void hmp_info_vnc(Monitor *mon, const QDict *qdict)
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200367{
368 VncInfo *info;
369 Error *err = NULL;
370 VncClientInfoList *client;
371
372 info = qmp_query_vnc(&err);
373 if (err) {
374 monitor_printf(mon, "%s\n", error_get_pretty(err));
375 error_free(err);
376 return;
377 }
378
379 if (!info->enabled) {
380 monitor_printf(mon, "Server: disabled\n");
381 goto out;
382 }
383
384 monitor_printf(mon, "Server:\n");
385 if (info->has_host && info->has_service) {
386 monitor_printf(mon, " address: %s:%s\n", info->host, info->service);
387 }
388 if (info->has_auth) {
389 monitor_printf(mon, " auth: %s\n", info->auth);
390 }
391
392 if (!info->has_clients || info->clients == NULL) {
393 monitor_printf(mon, "Client: none\n");
394 } else {
395 for (client = info->clients; client; client = client->next) {
396 monitor_printf(mon, "Client:\n");
397 monitor_printf(mon, " address: %s:%s\n",
398 client->value->host, client->value->service);
399 monitor_printf(mon, " x509_dname: %s\n",
400 client->value->x509_dname ?
401 client->value->x509_dname : "none");
402 monitor_printf(mon, " username: %s\n",
403 client->value->has_sasl_username ?
404 client->value->sasl_username : "none");
405 }
406 }
407
408out:
409 qapi_free_VncInfo(info);
410}
411
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800412void hmp_info_spice(Monitor *mon, const QDict *qdict)
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200413{
414 SpiceChannelList *chan;
415 SpiceInfo *info;
416
417 info = qmp_query_spice(NULL);
418
419 if (!info->enabled) {
420 monitor_printf(mon, "Server: disabled\n");
421 goto out;
422 }
423
424 monitor_printf(mon, "Server:\n");
425 if (info->has_port) {
426 monitor_printf(mon, " address: %s:%" PRId64 "\n",
427 info->host, info->port);
428 }
429 if (info->has_tls_port) {
430 monitor_printf(mon, " address: %s:%" PRId64 " [tls]\n",
431 info->host, info->tls_port);
432 }
Yonit Halperin61c4efe2012-08-21 11:51:58 +0300433 monitor_printf(mon, " migrated: %s\n",
434 info->migrated ? "true" : "false");
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200435 monitor_printf(mon, " auth: %s\n", info->auth);
436 monitor_printf(mon, " compiled: %s\n", info->compiled_version);
Alon Levy4efee022012-03-29 23:23:14 +0200437 monitor_printf(mon, " mouse-mode: %s\n",
438 SpiceQueryMouseMode_lookup[info->mouse_mode]);
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200439
440 if (!info->has_channels || info->channels == NULL) {
441 monitor_printf(mon, "Channels: none\n");
442 } else {
443 for (chan = info->channels; chan; chan = chan->next) {
444 monitor_printf(mon, "Channel:\n");
445 monitor_printf(mon, " address: %s:%s%s\n",
446 chan->value->host, chan->value->port,
447 chan->value->tls ? " [tls]" : "");
448 monitor_printf(mon, " session: %" PRId64 "\n",
449 chan->value->connection_id);
450 monitor_printf(mon, " channel: %" PRId64 ":%" PRId64 "\n",
451 chan->value->channel_type, chan->value->channel_id);
452 }
453 }
454
455out:
456 qapi_free_SpiceInfo(info);
457}
458
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800459void hmp_info_balloon(Monitor *mon, const QDict *qdict)
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200460{
461 BalloonInfo *info;
462 Error *err = NULL;
463
464 info = qmp_query_balloon(&err);
465 if (err) {
466 monitor_printf(mon, "%s\n", error_get_pretty(err));
467 error_free(err);
468 return;
469 }
470
Luiz Capitulino01ceb972012-12-03 15:56:41 -0200471 monitor_printf(mon, "balloon: actual=%" PRId64 "\n", info->actual >> 20);
Luiz Capitulino96637bc2011-10-21 11:41:37 -0200472
473 qapi_free_BalloonInfo(info);
474}
475
Luiz Capitulino79627472011-10-21 14:15:33 -0200476static void hmp_info_pci_device(Monitor *mon, const PciDeviceInfo *dev)
477{
478 PciMemoryRegionList *region;
479
480 monitor_printf(mon, " Bus %2" PRId64 ", ", dev->bus);
481 monitor_printf(mon, "device %3" PRId64 ", function %" PRId64 ":\n",
482 dev->slot, dev->function);
483 monitor_printf(mon, " ");
484
485 if (dev->class_info.has_desc) {
486 monitor_printf(mon, "%s", dev->class_info.desc);
487 } else {
488 monitor_printf(mon, "Class %04" PRId64, dev->class_info.class);
489 }
490
491 monitor_printf(mon, ": PCI device %04" PRIx64 ":%04" PRIx64 "\n",
492 dev->id.vendor, dev->id.device);
493
494 if (dev->has_irq) {
495 monitor_printf(mon, " IRQ %" PRId64 ".\n", dev->irq);
496 }
497
498 if (dev->has_pci_bridge) {
499 monitor_printf(mon, " BUS %" PRId64 ".\n",
500 dev->pci_bridge->bus.number);
501 monitor_printf(mon, " secondary bus %" PRId64 ".\n",
502 dev->pci_bridge->bus.secondary);
503 monitor_printf(mon, " subordinate bus %" PRId64 ".\n",
504 dev->pci_bridge->bus.subordinate);
505
506 monitor_printf(mon, " IO range [0x%04"PRIx64", 0x%04"PRIx64"]\n",
507 dev->pci_bridge->bus.io_range->base,
508 dev->pci_bridge->bus.io_range->limit);
509
510 monitor_printf(mon,
511 " memory range [0x%08"PRIx64", 0x%08"PRIx64"]\n",
512 dev->pci_bridge->bus.memory_range->base,
513 dev->pci_bridge->bus.memory_range->limit);
514
515 monitor_printf(mon, " prefetchable memory range "
516 "[0x%08"PRIx64", 0x%08"PRIx64"]\n",
517 dev->pci_bridge->bus.prefetchable_range->base,
518 dev->pci_bridge->bus.prefetchable_range->limit);
519 }
520
521 for (region = dev->regions; region; region = region->next) {
522 uint64_t addr, size;
523
524 addr = region->value->address;
525 size = region->value->size;
526
527 monitor_printf(mon, " BAR%" PRId64 ": ", region->value->bar);
528
529 if (!strcmp(region->value->type, "io")) {
530 monitor_printf(mon, "I/O at 0x%04" PRIx64
531 " [0x%04" PRIx64 "].\n",
532 addr, addr + size - 1);
533 } else {
534 monitor_printf(mon, "%d bit%s memory at 0x%08" PRIx64
535 " [0x%08" PRIx64 "].\n",
536 region->value->mem_type_64 ? 64 : 32,
537 region->value->prefetch ? " prefetchable" : "",
538 addr, addr + size - 1);
539 }
540 }
541
542 monitor_printf(mon, " id \"%s\"\n", dev->qdev_id);
543
544 if (dev->has_pci_bridge) {
545 if (dev->pci_bridge->has_devices) {
546 PciDeviceInfoList *cdev;
547 for (cdev = dev->pci_bridge->devices; cdev; cdev = cdev->next) {
548 hmp_info_pci_device(mon, cdev->value);
549 }
550 }
551 }
552}
553
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800554void hmp_info_pci(Monitor *mon, const QDict *qdict)
Luiz Capitulino79627472011-10-21 14:15:33 -0200555{
Stefan Bergerf46cee32012-01-11 10:51:52 -0500556 PciInfoList *info_list, *info;
Luiz Capitulino79627472011-10-21 14:15:33 -0200557 Error *err = NULL;
558
Stefan Bergerf46cee32012-01-11 10:51:52 -0500559 info_list = qmp_query_pci(&err);
Luiz Capitulino79627472011-10-21 14:15:33 -0200560 if (err) {
561 monitor_printf(mon, "PCI devices not supported\n");
562 error_free(err);
563 return;
564 }
565
Stefan Bergerf46cee32012-01-11 10:51:52 -0500566 for (info = info_list; info; info = info->next) {
Luiz Capitulino79627472011-10-21 14:15:33 -0200567 PciDeviceInfoList *dev;
568
569 for (dev = info->value->devices; dev; dev = dev->next) {
570 hmp_info_pci_device(mon, dev->value);
571 }
572 }
573
Stefan Bergerf46cee32012-01-11 10:51:52 -0500574 qapi_free_PciInfoList(info_list);
Luiz Capitulino79627472011-10-21 14:15:33 -0200575}
576
Wenchao Xia84f2d0e2013-01-14 14:06:25 +0800577void hmp_info_block_jobs(Monitor *mon, const QDict *qdict)
Stefan Hajnoczifb5458c2012-01-18 14:40:49 +0000578{
579 BlockJobInfoList *list;
580 Error *err = NULL;
581
582 list = qmp_query_block_jobs(&err);
583 assert(!err);
584
585 if (!list) {
586 monitor_printf(mon, "No active jobs\n");
587 return;
588 }
589
590 while (list) {
591 if (strcmp(list->value->type, "stream") == 0) {
592 monitor_printf(mon, "Streaming device %s: Completed %" PRId64
593 " of %" PRId64 " bytes, speed limit %" PRId64
594 " bytes/s\n",
595 list->value->device,
596 list->value->offset,
597 list->value->len,
598 list->value->speed);
599 } else {
600 monitor_printf(mon, "Type %s, device %s: Completed %" PRId64
601 " of %" PRId64 " bytes, speed limit %" PRId64
602 " bytes/s\n",
603 list->value->type,
604 list->value->device,
605 list->value->offset,
606 list->value->len,
607 list->value->speed);
608 }
609 list = list->next;
610 }
611}
612
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500613void hmp_info_tpm(Monitor *mon, const QDict *qdict)
614{
615 TPMInfoList *info_list, *info;
616 Error *err = NULL;
617 unsigned int c = 0;
618 TPMPassthroughOptions *tpo;
619
620 info_list = qmp_query_tpm(&err);
621 if (err) {
622 monitor_printf(mon, "TPM device not supported\n");
623 error_free(err);
624 return;
625 }
626
627 if (info_list) {
628 monitor_printf(mon, "TPM device:\n");
629 }
630
631 for (info = info_list; info; info = info->next) {
632 TPMInfo *ti = info->value;
633 monitor_printf(mon, " tpm%d: model=%s\n",
634 c, TpmModel_lookup[ti->model]);
635
636 monitor_printf(mon, " \\ %s: type=%s",
Corey Bryant88ca7bc2013-03-20 12:34:48 -0400637 ti->id, TpmTypeOptionsKind_lookup[ti->options->kind]);
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500638
Corey Bryant88ca7bc2013-03-20 12:34:48 -0400639 switch (ti->options->kind) {
640 case TPM_TYPE_OPTIONS_KIND_PASSTHROUGH:
641 tpo = ti->options->passthrough;
Stefan Bergerd1a0cf72013-02-27 12:47:49 -0500642 monitor_printf(mon, "%s%s%s%s",
643 tpo->has_path ? ",path=" : "",
644 tpo->has_path ? tpo->path : "",
645 tpo->has_cancel_path ? ",cancel-path=" : "",
646 tpo->has_cancel_path ? tpo->cancel_path : "");
647 break;
648 case TPM_TYPE_OPTIONS_KIND_MAX:
649 break;
650 }
651 monitor_printf(mon, "\n");
652 c++;
653 }
654 qapi_free_TPMInfoList(info_list);
655}
656
Luiz Capitulino7a7f3252011-09-15 14:20:28 -0300657void hmp_quit(Monitor *mon, const QDict *qdict)
658{
659 monitor_suspend(mon);
660 qmp_quit(NULL);
661}
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300662
663void hmp_stop(Monitor *mon, const QDict *qdict)
664{
665 qmp_stop(NULL);
666}
Luiz Capitulino38d22652011-09-15 14:41:46 -0300667
668void hmp_system_reset(Monitor *mon, const QDict *qdict)
669{
670 qmp_system_reset(NULL);
671}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300672
673void hmp_system_powerdown(Monitor *mon, const QDict *qdict)
674{
675 qmp_system_powerdown(NULL);
676}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300677
678void hmp_cpu(Monitor *mon, const QDict *qdict)
679{
680 int64_t cpu_index;
681
682 /* XXX: drop the monitor_set_cpu() usage when all HMP commands that
683 use it are converted to the QAPI */
684 cpu_index = qdict_get_int(qdict, "index");
685 if (monitor_set_cpu(cpu_index) < 0) {
686 monitor_printf(mon, "invalid CPU index\n");
687 }
688}
Luiz Capitulino0cfd6a92011-11-22 16:32:37 -0200689
690void hmp_memsave(Monitor *mon, const QDict *qdict)
691{
692 uint32_t size = qdict_get_int(qdict, "size");
693 const char *filename = qdict_get_str(qdict, "filename");
694 uint64_t addr = qdict_get_int(qdict, "val");
695 Error *errp = NULL;
696
697 qmp_memsave(addr, size, filename, true, monitor_get_cpu_index(), &errp);
698 hmp_handle_error(mon, &errp);
699}
Luiz Capitulino6d3962b2011-11-22 17:26:46 -0200700
701void hmp_pmemsave(Monitor *mon, const QDict *qdict)
702{
703 uint32_t size = qdict_get_int(qdict, "size");
704 const char *filename = qdict_get_str(qdict, "filename");
705 uint64_t addr = qdict_get_int(qdict, "val");
706 Error *errp = NULL;
707
708 qmp_pmemsave(addr, size, filename, &errp);
709 hmp_handle_error(mon, &errp);
710}
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200711
Markus Armbruster3949e592013-02-06 21:27:24 +0100712void hmp_ringbuf_write(Monitor *mon, const QDict *qdict)
Lei Li1f590cf2013-01-25 00:03:20 +0800713{
Lei Li1f590cf2013-01-25 00:03:20 +0800714 const char *chardev = qdict_get_str(qdict, "device");
715 const char *data = qdict_get_str(qdict, "data");
716 Error *errp = NULL;
717
Markus Armbruster3949e592013-02-06 21:27:24 +0100718 qmp_ringbuf_write(chardev, data, false, 0, &errp);
Lei Li1f590cf2013-01-25 00:03:20 +0800719
720 hmp_handle_error(mon, &errp);
721}
722
Markus Armbruster3949e592013-02-06 21:27:24 +0100723void hmp_ringbuf_read(Monitor *mon, const QDict *qdict)
Lei Li49b6d722013-01-25 00:03:21 +0800724{
725 uint32_t size = qdict_get_int(qdict, "size");
726 const char *chardev = qdict_get_str(qdict, "device");
Markus Armbruster3ab651f2013-02-06 21:27:15 +0100727 char *data;
Lei Li49b6d722013-01-25 00:03:21 +0800728 Error *errp = NULL;
Markus Armbruster543f3412013-02-06 21:27:26 +0100729 int i;
Lei Li49b6d722013-01-25 00:03:21 +0800730
Markus Armbruster3949e592013-02-06 21:27:24 +0100731 data = qmp_ringbuf_read(chardev, size, false, 0, &errp);
Lei Li49b6d722013-01-25 00:03:21 +0800732 if (errp) {
733 monitor_printf(mon, "%s\n", error_get_pretty(errp));
734 error_free(errp);
735 return;
736 }
737
Markus Armbruster543f3412013-02-06 21:27:26 +0100738 for (i = 0; data[i]; i++) {
739 unsigned char ch = data[i];
740
741 if (ch == '\\') {
742 monitor_printf(mon, "\\\\");
743 } else if ((ch < 0x20 && ch != '\n' && ch != '\t') || ch == 0x7F) {
744 monitor_printf(mon, "\\u%04X", ch);
745 } else {
746 monitor_printf(mon, "%c", ch);
747 }
748
749 }
750 monitor_printf(mon, "\n");
Markus Armbruster3ab651f2013-02-06 21:27:15 +0100751 g_free(data);
Lei Li49b6d722013-01-25 00:03:21 +0800752}
753
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200754static void hmp_cont_cb(void *opaque, int err)
755{
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200756 if (!err) {
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300757 qmp_cont(NULL);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200758 }
759}
760
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300761static bool key_is_missing(const BlockInfo *bdev)
762{
763 return (bdev->inserted && bdev->inserted->encryption_key_missing);
764}
765
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200766void hmp_cont(Monitor *mon, const QDict *qdict)
767{
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300768 BlockInfoList *bdev_list, *bdev;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200769 Error *errp = NULL;
770
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300771 bdev_list = qmp_query_block(NULL);
772 for (bdev = bdev_list; bdev; bdev = bdev->next) {
773 if (key_is_missing(bdev->value)) {
774 monitor_read_block_device_key(mon, bdev->value->device,
775 hmp_cont_cb, NULL);
776 goto out;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200777 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200778 }
Luiz Capitulino8b7f6fb2012-07-26 20:41:53 -0300779
780 qmp_cont(&errp);
781 hmp_handle_error(mon, &errp);
782
783out:
784 qapi_free_BlockInfoList(bdev_list);
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200785}
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200786
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100787void hmp_system_wakeup(Monitor *mon, const QDict *qdict)
788{
789 qmp_system_wakeup(NULL);
790}
791
Luiz Capitulinoab49ab52011-11-23 12:55:53 -0200792void hmp_inject_nmi(Monitor *mon, const QDict *qdict)
793{
794 Error *errp = NULL;
795
796 qmp_inject_nmi(&errp);
797 hmp_handle_error(mon, &errp);
798}
Luiz Capitulino4b371562011-11-23 13:11:55 -0200799
800void hmp_set_link(Monitor *mon, const QDict *qdict)
801{
802 const char *name = qdict_get_str(qdict, "name");
803 int up = qdict_get_bool(qdict, "up");
804 Error *errp = NULL;
805
806 qmp_set_link(name, up, &errp);
807 hmp_handle_error(mon, &errp);
808}
Luiz Capitulinoa4dea8a2011-11-23 13:28:21 -0200809
810void hmp_block_passwd(Monitor *mon, const QDict *qdict)
811{
812 const char *device = qdict_get_str(qdict, "device");
813 const char *password = qdict_get_str(qdict, "password");
814 Error *errp = NULL;
815
816 qmp_block_passwd(device, password, &errp);
817 hmp_handle_error(mon, &errp);
818}
Luiz Capitulinod72f3262011-11-25 14:38:09 -0200819
820void hmp_balloon(Monitor *mon, const QDict *qdict)
821{
822 int64_t value = qdict_get_int(qdict, "value");
823 Error *errp = NULL;
824
825 qmp_balloon(value, &errp);
826 if (error_is_set(&errp)) {
827 monitor_printf(mon, "balloon: %s\n", error_get_pretty(errp));
828 error_free(errp);
829 }
830}
Luiz Capitulino5e7caac2011-11-25 14:57:10 -0200831
832void hmp_block_resize(Monitor *mon, const QDict *qdict)
833{
834 const char *device = qdict_get_str(qdict, "device");
835 int64_t size = qdict_get_int(qdict, "size");
836 Error *errp = NULL;
837
838 qmp_block_resize(device, size, &errp);
839 hmp_handle_error(mon, &errp);
840}
Luiz Capitulino6106e242011-11-25 16:15:19 -0200841
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200842void hmp_drive_mirror(Monitor *mon, const QDict *qdict)
843{
844 const char *device = qdict_get_str(qdict, "device");
845 const char *filename = qdict_get_str(qdict, "target");
846 const char *format = qdict_get_try_str(qdict, "format");
847 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
848 int full = qdict_get_try_bool(qdict, "full", 0);
849 enum NewImageMode mode;
850 Error *errp = NULL;
851
852 if (!filename) {
853 error_set(&errp, QERR_MISSING_PARAMETER, "target");
854 hmp_handle_error(mon, &errp);
855 return;
856 }
857
858 if (reuse) {
859 mode = NEW_IMAGE_MODE_EXISTING;
860 } else {
861 mode = NEW_IMAGE_MODE_ABSOLUTE_PATHS;
862 }
863
864 qmp_drive_mirror(device, filename, !!format, format,
865 full ? MIRROR_SYNC_MODE_FULL : MIRROR_SYNC_MODE_TOP,
Paolo Bonzini08e4ed62013-01-22 09:03:13 +0100866 true, mode, false, 0, false, 0, false, 0,
Paolo Bonzinib952b552012-10-18 16:49:28 +0200867 false, 0, false, 0, &errp);
Paolo Bonzinid9b902d2012-10-18 16:49:24 +0200868 hmp_handle_error(mon, &errp);
869}
870
Luiz Capitulino6106e242011-11-25 16:15:19 -0200871void hmp_snapshot_blkdev(Monitor *mon, const QDict *qdict)
872{
873 const char *device = qdict_get_str(qdict, "device");
874 const char *filename = qdict_get_try_str(qdict, "snapshot-file");
875 const char *format = qdict_get_try_str(qdict, "format");
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100876 int reuse = qdict_get_try_bool(qdict, "reuse", 0);
877 enum NewImageMode mode;
Luiz Capitulino6106e242011-11-25 16:15:19 -0200878 Error *errp = NULL;
879
880 if (!filename) {
881 /* In the future, if 'snapshot-file' is not specified, the snapshot
882 will be taken internally. Today it's actually required. */
883 error_set(&errp, QERR_MISSING_PARAMETER, "snapshot-file");
884 hmp_handle_error(mon, &errp);
885 return;
886 }
887
Paolo Bonzini6cc2a412012-03-06 18:55:59 +0100888 mode = reuse ? NEW_IMAGE_MODE_EXISTING : NEW_IMAGE_MODE_ABSOLUTE_PATHS;
889 qmp_blockdev_snapshot_sync(device, filename, !!format, format,
890 true, mode, &errp);
Luiz Capitulino6106e242011-11-25 16:15:19 -0200891 hmp_handle_error(mon, &errp);
892}
Luiz Capitulino6cdedb02011-11-27 22:54:09 -0200893
894void hmp_migrate_cancel(Monitor *mon, const QDict *qdict)
895{
896 qmp_migrate_cancel(NULL);
897}
Luiz Capitulino4f0a9932011-11-27 23:18:01 -0200898
899void hmp_migrate_set_downtime(Monitor *mon, const QDict *qdict)
900{
901 double value = qdict_get_double(qdict, "value");
902 qmp_migrate_set_downtime(value, NULL);
903}
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200904
Orit Wasserman9e1ba4c2012-08-06 21:42:54 +0300905void hmp_migrate_set_cache_size(Monitor *mon, const QDict *qdict)
906{
907 int64_t value = qdict_get_int(qdict, "value");
908 Error *err = NULL;
909
910 qmp_migrate_set_cache_size(value, &err);
911 if (err) {
912 monitor_printf(mon, "%s\n", error_get_pretty(err));
913 error_free(err);
914 return;
915 }
916}
917
Luiz Capitulino3dc85382011-11-28 11:59:37 -0200918void hmp_migrate_set_speed(Monitor *mon, const QDict *qdict)
919{
920 int64_t value = qdict_get_int(qdict, "value");
921 qmp_migrate_set_speed(value, NULL);
922}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200923
Orit Wasserman00458432012-08-06 21:42:48 +0300924void hmp_migrate_set_capability(Monitor *mon, const QDict *qdict)
925{
926 const char *cap = qdict_get_str(qdict, "capability");
927 bool state = qdict_get_bool(qdict, "state");
928 Error *err = NULL;
929 MigrationCapabilityStatusList *caps = g_malloc0(sizeof(*caps));
930 int i;
931
932 for (i = 0; i < MIGRATION_CAPABILITY_MAX; i++) {
933 if (strcmp(cap, MigrationCapability_lookup[i]) == 0) {
934 caps->value = g_malloc0(sizeof(*caps->value));
935 caps->value->capability = i;
936 caps->value->state = state;
937 caps->next = NULL;
938 qmp_migrate_set_capabilities(caps, &err);
939 break;
940 }
941 }
942
943 if (i == MIGRATION_CAPABILITY_MAX) {
944 error_set(&err, QERR_INVALID_PARAMETER, cap);
945 }
946
947 qapi_free_MigrationCapabilityStatusList(caps);
948
949 if (err) {
Orit Wassermana31ca012013-01-31 09:12:19 +0200950 monitor_printf(mon, "migrate_set_capability: %s\n",
Orit Wasserman00458432012-08-06 21:42:48 +0300951 error_get_pretty(err));
952 error_free(err);
953 }
954}
955
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200956void hmp_set_password(Monitor *mon, const QDict *qdict)
957{
958 const char *protocol = qdict_get_str(qdict, "protocol");
959 const char *password = qdict_get_str(qdict, "password");
960 const char *connected = qdict_get_try_str(qdict, "connected");
961 Error *err = NULL;
962
963 qmp_set_password(protocol, password, !!connected, connected, &err);
964 hmp_handle_error(mon, &err);
965}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200966
967void hmp_expire_password(Monitor *mon, const QDict *qdict)
968{
969 const char *protocol = qdict_get_str(qdict, "protocol");
970 const char *whenstr = qdict_get_str(qdict, "time");
971 Error *err = NULL;
972
973 qmp_expire_password(protocol, whenstr, &err);
974 hmp_handle_error(mon, &err);
975}
Luiz Capitulinoc245b6a2011-12-07 16:02:36 -0200976
977void hmp_eject(Monitor *mon, const QDict *qdict)
978{
979 int force = qdict_get_try_bool(qdict, "force", 0);
980 const char *device = qdict_get_str(qdict, "device");
981 Error *err = NULL;
982
983 qmp_eject(device, true, force, &err);
984 hmp_handle_error(mon, &err);
985}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200986
987static void hmp_change_read_arg(Monitor *mon, const char *password,
988 void *opaque)
989{
990 qmp_change_vnc_password(password, NULL);
991 monitor_read_command(mon, 1);
992}
993
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200994void hmp_change(Monitor *mon, const QDict *qdict)
995{
996 const char *device = qdict_get_str(qdict, "device");
997 const char *target = qdict_get_str(qdict, "target");
998 const char *arg = qdict_get_try_str(qdict, "arg");
999 Error *err = NULL;
1000
1001 if (strcmp(device, "vnc") == 0 &&
1002 (strcmp(target, "passwd") == 0 ||
1003 strcmp(target, "password") == 0)) {
1004 if (!arg) {
1005 monitor_read_password(mon, hmp_change_read_arg, NULL);
1006 return;
1007 }
1008 }
1009
1010 qmp_change(device, target, !!arg, arg, &err);
Luiz Capitulinoab878dd2012-08-06 15:55:22 -03001011 if (error_is_set(&err) &&
1012 error_get_class(err) == ERROR_CLASS_DEVICE_ENCRYPTED) {
Luiz Capitulinoeef5ad12012-08-06 15:49:34 -03001013 error_free(err);
1014 monitor_read_block_device_key(mon, device, NULL, NULL);
Luiz Capitulino333a96e2011-12-08 11:13:50 -02001015 return;
1016 }
1017 hmp_handle_error(mon, &err);
1018}
Luiz Capitulino80047da2011-12-14 16:49:14 -02001019
1020void hmp_block_set_io_throttle(Monitor *mon, const QDict *qdict)
1021{
1022 Error *err = NULL;
1023
1024 qmp_block_set_io_throttle(qdict_get_str(qdict, "device"),
1025 qdict_get_int(qdict, "bps"),
1026 qdict_get_int(qdict, "bps_rd"),
1027 qdict_get_int(qdict, "bps_wr"),
1028 qdict_get_int(qdict, "iops"),
1029 qdict_get_int(qdict, "iops_rd"),
1030 qdict_get_int(qdict, "iops_wr"), &err);
1031 hmp_handle_error(mon, &err);
1032}
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001033
1034void hmp_block_stream(Monitor *mon, const QDict *qdict)
1035{
1036 Error *error = NULL;
1037 const char *device = qdict_get_str(qdict, "device");
1038 const char *base = qdict_get_try_str(qdict, "base");
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001039 int64_t speed = qdict_get_try_int(qdict, "speed", 0);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001040
Stefan Hajnoczic83c66c2012-04-25 16:51:03 +01001041 qmp_block_stream(device, base != NULL, base,
Paolo Bonzini1d809092012-09-28 17:22:59 +02001042 qdict_haskey(qdict, "speed"), speed,
1043 BLOCKDEV_ON_ERROR_REPORT, true, &error);
Stefan Hajnoczi12bd4512012-01-18 14:40:46 +00001044
1045 hmp_handle_error(mon, &error);
1046}
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001047
1048void hmp_block_job_set_speed(Monitor *mon, const QDict *qdict)
1049{
1050 Error *error = NULL;
1051 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzinic6db2392012-05-08 16:51:56 +02001052 int64_t value = qdict_get_int(qdict, "speed");
Stefan Hajnoczi2d47c6e2012-01-18 14:40:47 +00001053
1054 qmp_block_job_set_speed(device, value, &error);
1055
1056 hmp_handle_error(mon, &error);
1057}
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001058
1059void hmp_block_job_cancel(Monitor *mon, const QDict *qdict)
1060{
1061 Error *error = NULL;
1062 const char *device = qdict_get_str(qdict, "device");
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001063 bool force = qdict_get_try_bool(qdict, "force", 0);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001064
Paolo Bonzini6e37fb82012-09-28 17:22:51 +02001065 qmp_block_job_cancel(device, true, force, &error);
1066
1067 hmp_handle_error(mon, &error);
1068}
1069
1070void hmp_block_job_pause(Monitor *mon, const QDict *qdict)
1071{
1072 Error *error = NULL;
1073 const char *device = qdict_get_str(qdict, "device");
1074
1075 qmp_block_job_pause(device, &error);
1076
1077 hmp_handle_error(mon, &error);
1078}
1079
1080void hmp_block_job_resume(Monitor *mon, const QDict *qdict)
1081{
1082 Error *error = NULL;
1083 const char *device = qdict_get_str(qdict, "device");
1084
1085 qmp_block_job_resume(device, &error);
Stefan Hajnoczi370521a2012-01-18 14:40:48 +00001086
1087 hmp_handle_error(mon, &error);
1088}
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001089
Paolo Bonziniaeae8832012-10-18 16:49:21 +02001090void hmp_block_job_complete(Monitor *mon, const QDict *qdict)
1091{
1092 Error *error = NULL;
1093 const char *device = qdict_get_str(qdict, "device");
1094
1095 qmp_block_job_complete(device, &error);
1096
1097 hmp_handle_error(mon, &error);
1098}
1099
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001100typedef struct MigrationStatus
1101{
1102 QEMUTimer *timer;
1103 Monitor *mon;
1104 bool is_block_migration;
1105} MigrationStatus;
1106
1107static void hmp_migrate_status_cb(void *opaque)
1108{
1109 MigrationStatus *status = opaque;
1110 MigrationInfo *info;
1111
1112 info = qmp_query_migrate(NULL);
1113 if (!info->has_status || strcmp(info->status, "active") == 0) {
1114 if (info->has_disk) {
1115 int progress;
1116
1117 if (info->disk->remaining) {
1118 progress = info->disk->transferred * 100 / info->disk->total;
1119 } else {
1120 progress = 100;
1121 }
1122
1123 monitor_printf(status->mon, "Completed %d %%\r", progress);
1124 monitor_flush(status->mon);
1125 }
1126
1127 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock) + 1000);
1128 } else {
1129 if (status->is_block_migration) {
1130 monitor_printf(status->mon, "\n");
1131 }
1132 monitor_resume(status->mon);
1133 qemu_del_timer(status->timer);
1134 g_free(status);
1135 }
1136
1137 qapi_free_MigrationInfo(info);
1138}
1139
1140void hmp_migrate(Monitor *mon, const QDict *qdict)
1141{
1142 int detach = qdict_get_try_bool(qdict, "detach", 0);
1143 int blk = qdict_get_try_bool(qdict, "blk", 0);
1144 int inc = qdict_get_try_bool(qdict, "inc", 0);
1145 const char *uri = qdict_get_str(qdict, "uri");
1146 Error *err = NULL;
1147
1148 qmp_migrate(uri, !!blk, blk, !!inc, inc, false, false, &err);
1149 if (err) {
1150 monitor_printf(mon, "migrate: %s\n", error_get_pretty(err));
1151 error_free(err);
1152 return;
1153 }
1154
1155 if (!detach) {
1156 MigrationStatus *status;
1157
1158 if (monitor_suspend(mon) < 0) {
1159 monitor_printf(mon, "terminal does not allow synchronous "
1160 "migration, continuing detached\n");
1161 return;
1162 }
1163
1164 status = g_malloc0(sizeof(*status));
1165 status->mon = mon;
1166 status->is_block_migration = blk || inc;
1167 status->timer = qemu_new_timer_ms(rt_clock, hmp_migrate_status_cb,
1168 status);
1169 qemu_mod_timer(status->timer, qemu_get_clock_ms(rt_clock));
1170 }
1171}
Luiz Capitulinoa15fef22012-03-29 12:38:50 -03001172
1173void hmp_device_del(Monitor *mon, const QDict *qdict)
1174{
1175 const char *id = qdict_get_str(qdict, "id");
1176 Error *err = NULL;
1177
1178 qmp_device_del(id, &err);
1179 hmp_handle_error(mon, &err);
1180}
Wen Congyang783e9b42012-05-07 12:10:47 +08001181
1182void hmp_dump_guest_memory(Monitor *mon, const QDict *qdict)
1183{
1184 Error *errp = NULL;
1185 int paging = qdict_get_try_bool(qdict, "paging", 0);
Luiz Capitulino75363762012-09-21 13:53:00 -03001186 const char *file = qdict_get_str(qdict, "filename");
Wen Congyang783e9b42012-05-07 12:10:47 +08001187 bool has_begin = qdict_haskey(qdict, "begin");
1188 bool has_length = qdict_haskey(qdict, "length");
1189 int64_t begin = 0;
1190 int64_t length = 0;
Luiz Capitulino75363762012-09-21 13:53:00 -03001191 char *prot;
Wen Congyang783e9b42012-05-07 12:10:47 +08001192
1193 if (has_begin) {
1194 begin = qdict_get_int(qdict, "begin");
1195 }
1196 if (has_length) {
1197 length = qdict_get_int(qdict, "length");
1198 }
1199
Luiz Capitulino75363762012-09-21 13:53:00 -03001200 prot = g_strconcat("file:", file, NULL);
1201
1202 qmp_dump_guest_memory(paging, prot, has_begin, begin, has_length, length,
Wen Congyang783e9b42012-05-07 12:10:47 +08001203 &errp);
1204 hmp_handle_error(mon, &errp);
Luiz Capitulino75363762012-09-21 13:53:00 -03001205 g_free(prot);
Wen Congyang783e9b42012-05-07 12:10:47 +08001206}
Luiz Capitulino928059a2012-04-18 17:34:15 -03001207
1208void hmp_netdev_add(Monitor *mon, const QDict *qdict)
1209{
1210 Error *err = NULL;
1211 QemuOpts *opts;
1212
1213 opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict, &err);
1214 if (error_is_set(&err)) {
1215 goto out;
1216 }
1217
1218 netdev_add(opts, &err);
1219 if (error_is_set(&err)) {
1220 qemu_opts_del(opts);
1221 }
1222
1223out:
1224 hmp_handle_error(mon, &err);
1225}
Luiz Capitulino5f964152012-04-16 14:36:32 -03001226
1227void hmp_netdev_del(Monitor *mon, const QDict *qdict)
1228{
1229 const char *id = qdict_get_str(qdict, "id");
1230 Error *err = NULL;
1231
1232 qmp_netdev_del(id, &err);
1233 hmp_handle_error(mon, &err);
1234}
Corey Bryant208c9d12012-06-22 14:36:09 -04001235
1236void hmp_getfd(Monitor *mon, const QDict *qdict)
1237{
1238 const char *fdname = qdict_get_str(qdict, "fdname");
1239 Error *errp = NULL;
1240
1241 qmp_getfd(fdname, &errp);
1242 hmp_handle_error(mon, &errp);
1243}
1244
1245void hmp_closefd(Monitor *mon, const QDict *qdict)
1246{
1247 const char *fdname = qdict_get_str(qdict, "fdname");
1248 Error *errp = NULL;
1249
1250 qmp_closefd(fdname, &errp);
1251 hmp_handle_error(mon, &errp);
1252}
Amos Konge4c8f002012-08-31 10:56:26 +08001253
1254void hmp_send_key(Monitor *mon, const QDict *qdict)
1255{
1256 const char *keys = qdict_get_str(qdict, "keys");
Luiz Capitulino9f328972012-09-20 14:19:47 -03001257 KeyValueList *keylist, *head = NULL, *tmp = NULL;
Amos Konge4c8f002012-08-31 10:56:26 +08001258 int has_hold_time = qdict_haskey(qdict, "hold-time");
1259 int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
1260 Error *err = NULL;
1261 char keyname_buf[16];
1262 char *separator;
Luiz Capitulino9f328972012-09-20 14:19:47 -03001263 int keyname_len;
Amos Konge4c8f002012-08-31 10:56:26 +08001264
1265 while (1) {
1266 separator = strchr(keys, '-');
1267 keyname_len = separator ? separator - keys : strlen(keys);
1268 pstrcpy(keyname_buf, sizeof(keyname_buf), keys);
1269
1270 /* Be compatible with old interface, convert user inputted "<" */
1271 if (!strncmp(keyname_buf, "<", 1) && keyname_len == 1) {
1272 pstrcpy(keyname_buf, sizeof(keyname_buf), "less");
1273 keyname_len = 4;
1274 }
1275 keyname_buf[keyname_len] = 0;
1276
Amos Konge4c8f002012-08-31 10:56:26 +08001277 keylist = g_malloc0(sizeof(*keylist));
Luiz Capitulino9f328972012-09-20 14:19:47 -03001278 keylist->value = g_malloc0(sizeof(*keylist->value));
Amos Konge4c8f002012-08-31 10:56:26 +08001279
1280 if (!head) {
1281 head = keylist;
1282 }
1283 if (tmp) {
1284 tmp->next = keylist;
1285 }
1286 tmp = keylist;
1287
Luiz Capitulino9f328972012-09-20 14:19:47 -03001288 if (strstart(keyname_buf, "0x", NULL)) {
1289 char *endp;
1290 int value = strtoul(keyname_buf, &endp, 0);
1291 if (*endp != '\0') {
1292 goto err_out;
1293 }
1294 keylist->value->kind = KEY_VALUE_KIND_NUMBER;
1295 keylist->value->number = value;
1296 } else {
1297 int idx = index_from_key(keyname_buf);
1298 if (idx == Q_KEY_CODE_MAX) {
1299 goto err_out;
1300 }
1301 keylist->value->kind = KEY_VALUE_KIND_QCODE;
1302 keylist->value->qcode = idx;
1303 }
1304
Amos Konge4c8f002012-08-31 10:56:26 +08001305 if (!separator) {
1306 break;
1307 }
1308 keys = separator + 1;
1309 }
1310
Luiz Capitulino9f328972012-09-20 14:19:47 -03001311 qmp_send_key(head, has_hold_time, hold_time, &err);
Amos Konge4c8f002012-08-31 10:56:26 +08001312 hmp_handle_error(mon, &err);
Luiz Capitulino9f328972012-09-20 14:19:47 -03001313
1314out:
1315 qapi_free_KeyValueList(head);
1316 return;
1317
1318err_out:
1319 monitor_printf(mon, "invalid parameter: %s\n", keyname_buf);
1320 goto out;
Amos Konge4c8f002012-08-31 10:56:26 +08001321}
Luiz Capitulinoad39cf62012-05-24 13:48:23 -03001322
1323void hmp_screen_dump(Monitor *mon, const QDict *qdict)
1324{
1325 const char *filename = qdict_get_str(qdict, "filename");
1326 Error *err = NULL;
1327
1328 qmp_screendump(filename, &err);
1329 hmp_handle_error(mon, &err);
1330}
Paolo Bonzini40577252012-08-23 11:53:04 +02001331
1332void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
1333{
1334 const char *uri = qdict_get_str(qdict, "uri");
1335 int writable = qdict_get_try_bool(qdict, "writable", 0);
1336 int all = qdict_get_try_bool(qdict, "all", 0);
1337 Error *local_err = NULL;
1338 BlockInfoList *block_list, *info;
1339 SocketAddress *addr;
1340
1341 if (writable && !all) {
1342 error_setg(&local_err, "-w only valid together with -a");
1343 goto exit;
1344 }
1345
1346 /* First check if the address is valid and start the server. */
1347 addr = socket_parse(uri, &local_err);
1348 if (local_err != NULL) {
1349 goto exit;
1350 }
1351
1352 qmp_nbd_server_start(addr, &local_err);
1353 qapi_free_SocketAddress(addr);
1354 if (local_err != NULL) {
1355 goto exit;
1356 }
1357
1358 if (!all) {
1359 return;
1360 }
1361
1362 /* Then try adding all block devices. If one fails, close all and
1363 * exit.
1364 */
1365 block_list = qmp_query_block(NULL);
1366
1367 for (info = block_list; info; info = info->next) {
1368 if (!info->value->has_inserted) {
1369 continue;
1370 }
1371
1372 qmp_nbd_server_add(info->value->device, true, writable, &local_err);
1373
1374 if (local_err != NULL) {
1375 qmp_nbd_server_stop(NULL);
1376 break;
1377 }
1378 }
1379
1380 qapi_free_BlockInfoList(block_list);
1381
1382exit:
1383 hmp_handle_error(mon, &local_err);
1384}
1385
1386void hmp_nbd_server_add(Monitor *mon, const QDict *qdict)
1387{
1388 const char *device = qdict_get_str(qdict, "device");
1389 int writable = qdict_get_try_bool(qdict, "writable", 0);
1390 Error *local_err = NULL;
1391
1392 qmp_nbd_server_add(device, true, writable, &local_err);
1393
1394 if (local_err != NULL) {
1395 hmp_handle_error(mon, &local_err);
1396 }
1397}
1398
1399void hmp_nbd_server_stop(Monitor *mon, const QDict *qdict)
1400{
1401 Error *errp = NULL;
1402
1403 qmp_nbd_server_stop(&errp);
1404 hmp_handle_error(mon, &errp);
1405}
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001406
1407void hmp_chardev_add(Monitor *mon, const QDict *qdict)
1408{
1409 const char *args = qdict_get_str(qdict, "args");
1410 Error *err = NULL;
1411 QemuOpts *opts;
1412
1413 opts = qemu_opts_parse(qemu_find_opts("chardev"), args, 1);
1414 if (opts == NULL) {
Markus Armbruster312fd5f2013-02-08 21:22:16 +01001415 error_setg(&err, "Parsing chardev args failed");
Gerd Hoffmannf1088902012-12-19 10:33:40 +01001416 } else {
1417 qemu_chr_new_from_opts(opts, NULL, &err);
1418 }
1419 hmp_handle_error(mon, &err);
1420}
1421
1422void hmp_chardev_remove(Monitor *mon, const QDict *qdict)
1423{
1424 Error *local_err = NULL;
1425
1426 qmp_chardev_remove(qdict_get_str(qdict, "id"), &local_err);
1427 hmp_handle_error(mon, &local_err);
1428}
Kevin Wolf587da2c2013-06-05 14:19:41 +02001429
1430void hmp_qemu_io(Monitor *mon, const QDict *qdict)
1431{
1432 BlockDriverState *bs;
1433 const char* device = qdict_get_str(qdict, "device");
1434 const char* command = qdict_get_str(qdict, "command");
1435 Error *err = NULL;
1436
1437 bs = bdrv_find(device);
1438 if (bs) {
1439 qemuio_command(bs, command);
1440 } else {
1441 error_set(&err, QERR_DEVICE_NOT_FOUND, device);
1442 }
1443
1444 hmp_handle_error(mon, &err);
1445}