blob: e784a6763157ddc8ce001a4a995578885a4c3249 [file] [log] [blame]
Anthony Liguori48a32be2011-09-02 12:34:48 -05001/*
2 * QEMU Management Protocol
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
Peter Maydelld38ea872016-01-29 17:50:05 +000016#include "qemu/osdep.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020017#include "qemu/cutils.h"
Markus Armbrustera0b1a662015-03-17 18:16:21 +010018#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010019#include "sysemu/sysemu.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050020#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020021#include "sysemu/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020022#include "ui/qemu-spice.h"
23#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010024#include "sysemu/kvm.h"
25#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060026#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010027#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020028#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010029#include "qom/qom-qobject.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010030#include "qapi/qmp/qerror.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010031#include "qapi/qmp/qobject.h"
32#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020033#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010034#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020035#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020036#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050037
38NameInfo *qmp_query_name(Error **errp)
39{
40 NameInfo *info = g_malloc0(sizeof(*info));
41
42 if (qemu_name) {
43 info->has_name = true;
44 info->name = g_strdup(qemu_name);
45 }
46
47 return info;
48}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030049
Markus Armbruster7daecb32014-05-02 13:26:31 +020050VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030051{
Eric Blake4752cdb2015-05-04 09:05:31 -060052 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030053 const char *version = QEMU_VERSION;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050054 const char *tmp;
55 int err;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030056
Eric Blake4752cdb2015-05-04 09:05:31 -060057 info->qemu = g_new0(VersionTriple, 1);
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050058 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
59 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030060 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050061
62 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
63 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030064 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050065
66 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
67 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030068 info->package = g_strdup(QEMU_PKGVERSION);
69
70 return info;
71}
Luiz Capitulino292a2602011-09-12 15:10:53 -030072
73KvmInfo *qmp_query_kvm(Error **errp)
74{
75 KvmInfo *info = g_malloc0(sizeof(*info));
76
77 info->enabled = kvm_enabled();
78 info->present = kvm_available();
79
80 return info;
81}
82
Luiz Capitulinoefab7672011-09-13 17:16:25 -030083UuidInfo *qmp_query_uuid(Error **errp)
84{
85 UuidInfo *info = g_malloc0(sizeof(*info));
86 char uuid[64];
87
88 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
89 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
90 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
91 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
92 qemu_uuid[14], qemu_uuid[15]);
93
94 info->UUID = g_strdup(uuid);
95 return info;
96}
97
Markus Armbruster7daecb32014-05-02 13:26:31 +020098void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030099{
100 no_shutdown = 0;
101 qemu_system_shutdown_request();
102}
103
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300104void qmp_stop(Error **errp)
105{
Peter Xu65d64f32016-02-18 13:16:49 +0800106 /* if there is a dump in background, we should wait until the dump
107 * finished */
108 if (dump_in_progress()) {
109 error_setg(errp, "There is a dump in process, please wait.");
110 return;
111 }
112
Paolo Bonzini1e998142012-10-23 14:54:21 +0200113 if (runstate_check(RUN_STATE_INMIGRATE)) {
114 autostart = 0;
115 } else {
116 vm_stop(RUN_STATE_PAUSED);
117 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300118}
119
Luiz Capitulino38d22652011-09-15 14:41:46 -0300120void qmp_system_reset(Error **errp)
121{
122 qemu_system_reset_request();
123}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300124
125void qmp_system_powerdown(Error **erp)
126{
127 qemu_system_powerdown_request();
128}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300129
130void qmp_cpu(int64_t index, Error **errp)
131{
132 /* Just do nothing */
133}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200134
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200135void qmp_cpu_add(int64_t id, Error **errp)
136{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200137 MachineClass *mc;
138
139 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300140 if (mc->hot_add_cpu) {
141 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200142 } else {
143 error_setg(errp, "Not supported");
144 }
145}
146
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200147#ifndef CONFIG_VNC
148/* If VNC support is enabled, the "true" query-vnc command is
149 defined in the VNC subsystem */
150VncInfo *qmp_query_vnc(Error **errp)
151{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100152 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200153 return NULL;
154};
Leon Yu89db21772015-02-02 05:08:51 +0000155
156VncInfo2List *qmp_query_vnc_servers(Error **errp)
157{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100158 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000159 return NULL;
160};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200161#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200162
163#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100164/*
165 * qmp-commands.hx ensures that QMP command query-spice exists only
166 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
167 * result. However, the QAPI schema is blissfully unaware of that,
168 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200169 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
170 * one, or else linking fails. FIXME Educate the QAPI schema on
171 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100172 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200173SpiceInfo *qmp_query_spice(Error **errp)
174{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100175 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200176};
177#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200178
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200179void qmp_cont(Error **errp)
180{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100181 Error *local_err = NULL;
Max Reitz373340b2015-10-19 17:53:22 +0200182 BlockBackend *blk;
Markus Armbrusterab319792014-05-02 13:26:42 +0200183 BlockDriverState *bs;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200184
Peter Xu65d64f32016-02-18 13:16:49 +0800185 /* if there is a dump in background, we should wait until the dump
186 * finished */
187 if (dump_in_progress()) {
188 error_setg(errp, "There is a dump in process, please wait.");
189 return;
190 }
191
Hu Taoede085b2013-04-26 11:24:40 +0800192 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400193 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200194 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300195 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
196 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200197 }
198
Max Reitz373340b2015-10-19 17:53:22 +0200199 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
200 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200201 }
202 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100203 bdrv_add_key(bs, NULL, &local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200206 return;
207 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200208 }
209
Kevin Wolf76b1c7f2015-12-22 14:07:08 +0100210 /* Continuing after completed migration. Images have been inactivated to
211 * allow the destination to take control. Need to get control back now. */
212 if (runstate_check(RUN_STATE_FINISH_MIGRATE) ||
213 runstate_check(RUN_STATE_POSTMIGRATE))
214 {
215 bdrv_invalidate_cache_all(&local_err);
216 if (local_err) {
217 error_propagate(errp, local_err);
218 return;
219 }
220 }
221
Paolo Bonzini1e998142012-10-23 14:54:21 +0200222 if (runstate_check(RUN_STATE_INMIGRATE)) {
223 autostart = 1;
224 } else {
225 vm_start();
226 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200227}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600228
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100229void qmp_system_wakeup(Error **errp)
230{
231 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
232}
233
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600234ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600235{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600236 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600237 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600238 ObjectPropertyInfoList *props = NULL;
239 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000240 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600241
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600242 obj = object_resolve_path(path, &ambiguous);
243 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400244 if (ambiguous) {
245 error_setg(errp, "Path '%s' is ambiguous", path);
246 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100247 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
248 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400249 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600250 return NULL;
251 }
252
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000253 object_property_iter_init(&iter, obj);
254 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600255 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600256
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600257 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600258 entry->next = props;
259 props = entry;
260
261 entry->value->name = g_strdup(prop->name);
262 entry->value->type = g_strdup(prop->type);
263 }
264
265 return props;
266}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600267
Markus Armbruster6eb39372015-09-16 13:06:25 +0200268void qmp_qom_set(const char *path, const char *property, QObject *value,
269 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600270{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600271 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600272
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600273 obj = object_resolve_path(path, NULL);
274 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100275 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100276 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100277 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600278 }
279
Markus Armbruster485febc2015-03-13 17:25:50 +0100280 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600281}
282
Markus Armbruster6eb39372015-09-16 13:06:25 +0200283QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600284{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600285 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600286
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600287 obj = object_resolve_path(path, NULL);
288 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100289 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100290 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200291 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600292 }
293
Markus Armbruster6eb39372015-09-16 13:06:25 +0200294 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600295}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200296
297void qmp_set_password(const char *protocol, const char *password,
298 bool has_connected, const char *connected, Error **errp)
299{
300 int disconnect_if_connected = 0;
301 int fail_if_connected = 0;
302 int rc;
303
304 if (has_connected) {
305 if (strcmp(connected, "fail") == 0) {
306 fail_if_connected = 1;
307 } else if (strcmp(connected, "disconnect") == 0) {
308 disconnect_if_connected = 1;
309 } else if (strcmp(connected, "keep") == 0) {
310 /* nothing */
311 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100312 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200313 return;
314 }
315 }
316
317 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100318 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200319 return;
320 }
321 rc = qemu_spice_set_passwd(password, fail_if_connected,
322 disconnect_if_connected);
323 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100324 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200325 }
326 return;
327 }
328
329 if (strcmp(protocol, "vnc") == 0) {
330 if (fail_if_connected || disconnect_if_connected) {
331 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100332 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200333 return;
334 }
335 /* Note that setting an empty password will not disable login through
336 * this interface. */
337 rc = vnc_display_password(NULL, password);
338 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100339 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200340 }
341 return;
342 }
343
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100344 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200345}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200346
347void qmp_expire_password(const char *protocol, const char *whenstr,
348 Error **errp)
349{
350 time_t when;
351 int rc;
352
353 if (strcmp(whenstr, "now") == 0) {
354 when = 0;
355 } else if (strcmp(whenstr, "never") == 0) {
356 when = TIME_MAX;
357 } else if (whenstr[0] == '+') {
358 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
359 } else {
360 when = strtoull(whenstr, NULL, 10);
361 }
362
363 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100364 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200365 return;
366 }
367 rc = qemu_spice_set_pw_expire(when);
368 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100369 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200370 }
371 return;
372 }
373
374 if (strcmp(protocol, "vnc") == 0) {
375 rc = vnc_display_pw_expire(NULL, when);
376 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100377 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200378 }
379 return;
380 }
381
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100382 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200383}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200384
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200385#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200386void qmp_change_vnc_password(const char *password, Error **errp)
387{
388 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100389 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200390 }
391}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200392
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200393static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200394{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200395 QemuOptsList *olist = qemu_find_opts("vnc");
396 QemuOpts *opts;
397
398 if (strstr(target, "id=")) {
399 error_setg(errp, "id not supported");
400 return;
401 }
402
403 opts = qemu_opts_find(olist, "default");
404 if (opts) {
405 qemu_opts_del(opts);
406 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100407 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800408 if (!opts) {
409 return;
410 }
411
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200412 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200413}
414
415static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
416 Error **errp)
417{
418 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
419 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100420 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200421 } else {
422 qmp_change_vnc_password(arg, errp);
423 }
424 } else {
425 qmp_change_vnc_listen(target, errp);
426 }
427}
428#else
429void qmp_change_vnc_password(const char *password, Error **errp)
430{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100431 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200432}
433static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
434 Error **errp)
435{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100436 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200437}
438#endif /* !CONFIG_VNC */
439
440void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200441 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200442{
443 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200444 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200445 } else {
Max Reitz39ff43d2015-11-11 04:49:44 +0100446 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
447 errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200448 }
449}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600450
451static void qom_list_types_tramp(ObjectClass *klass, void *data)
452{
453 ObjectTypeInfoList *e, **pret = data;
454 ObjectTypeInfo *info;
455
456 info = g_malloc0(sizeof(*info));
457 info->name = g_strdup(object_class_get_name(klass));
458
459 e = g_malloc0(sizeof(*e));
460 e->value = info;
461 e->next = *pret;
462 *pret = e;
463}
464
465ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
466 const char *implements,
467 bool has_abstract,
468 bool abstract,
469 Error **errp)
470{
471 ObjectTypeInfoList *ret = NULL;
472
473 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
474
475 return ret;
476}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500477
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200478/* Return a DevicePropertyInfo for a qdev property.
479 *
480 * If a qdev property with the given name does not exist, use the given default
481 * type. If the qdev property info should not be shown, return NULL.
482 *
483 * The caller must free the return value.
484 */
485static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
486 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800487 const char *default_type,
488 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200489{
490 DevicePropertyInfo *info;
491 Property *prop;
492
493 do {
494 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
495 if (strcmp(name, prop->name) != 0) {
496 continue;
497 }
498
499 /*
500 * TODO Properties without a parser are just for dirty hacks.
501 * qdev_prop_ptr is the only such PropertyInfo. It's marked
502 * for removal. This conditional should be removed along with
503 * it.
504 */
505 if (!prop->info->set) {
506 return NULL; /* no way to set it, don't show */
507 }
508
509 info = g_malloc0(sizeof(*info));
510 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800511 info->type = g_strdup(prop->info->name);
512 info->has_description = !!prop->info->description;
513 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200514 return info;
515 }
516 klass = object_class_get_parent(klass);
517 } while (klass != object_class_by_name(TYPE_DEVICE));
518
519 /* Not a qdev property, use the default type */
520 info = g_malloc0(sizeof(*info));
521 info->name = g_strdup(name);
522 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800523 info->has_description = !!description;
524 info->description = g_strdup(description);
525
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200526 return info;
527}
528
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500529DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
530 Error **errp)
531{
532 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200533 Object *obj;
534 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000535 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500536 DevicePropertyInfoList *prop_list = NULL;
537
538 klass = object_class_by_name(typename);
539 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100540 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
541 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500542 return NULL;
543 }
544
545 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
546 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100547 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500548 return NULL;
549 }
550
Markus Armbrusteredb15232015-10-01 10:59:57 +0200551 if (object_class_is_abstract(klass)) {
552 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
553 "non-abstract device type");
554 return NULL;
555 }
556
Markus Armbruster4c315c22015-10-01 10:59:58 +0200557 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
558 error_setg(errp, "Can't list properties of device '%s'", typename);
559 return NULL;
560 }
561
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200562 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500563
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000564 object_property_iter_init(&iter, obj);
565 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200566 DevicePropertyInfo *info;
567 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500568
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200569 /* Skip Object and DeviceState properties */
570 if (strcmp(prop->name, "type") == 0 ||
571 strcmp(prop->name, "realized") == 0 ||
572 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200573 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200574 strcmp(prop->name, "parent_bus") == 0) {
575 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500576 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200577
578 /* Skip legacy properties since they are just string versions of
579 * properties that we already list.
580 */
581 if (strstart(prop->name, "legacy-", NULL)) {
582 continue;
583 }
584
Gonglei07d09c52014-10-07 14:33:23 +0800585 info = make_device_property_info(klass, prop->name, prop->type,
586 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200587 if (!info) {
588 continue;
589 }
590
591 entry = g_malloc0(sizeof(*entry));
592 entry->value = info;
593 entry->next = prop_list;
594 prop_list = entry;
595 }
596
597 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500598
599 return prop_list;
600}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500601
Anthony Liguori76b64a72012-08-14 22:17:36 -0500602CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
603{
604 return arch_query_cpu_definitions(errp);
605}
606
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300607void qmp_add_client(const char *protocol, const char *fdname,
608 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
609 Error **errp)
610{
611 CharDriverState *s;
612 int fd;
613
614 fd = monitor_get_fd(cur_mon, fdname, errp);
615 if (fd < 0) {
616 return;
617 }
618
619 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100620 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300621 close(fd);
622 return;
623 }
624 skipauth = has_skipauth ? skipauth : false;
625 tls = has_tls ? tls : false;
626 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
627 error_setg(errp, "spice failed to add client");
628 close(fd);
629 }
630 return;
631#ifdef CONFIG_VNC
632 } else if (strcmp(protocol, "vnc") == 0) {
633 skipauth = has_skipauth ? skipauth : false;
634 vnc_display_add_client(NULL, fd, skipauth);
635 return;
636#endif
637 } else if ((s = qemu_chr_find(protocol)) != NULL) {
638 if (qemu_chr_add_client(s, fd) < 0) {
639 error_setg(errp, "failed to add client");
640 close(fd);
641 return;
642 }
643 return;
644 }
645
646 error_setg(errp, "protocol '%s' is invalid", protocol);
647 close(fd);
648}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100649
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100650
Markus Armbruster6eb39372015-09-16 13:06:25 +0200651void qmp_object_add(const char *type, const char *id,
652 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100653{
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100654 const QDict *pdict = NULL;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100655 QmpInputVisitor *qiv;
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000656 Object *obj;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100657
658 if (props) {
659 pdict = qobject_to_qdict(props);
660 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100661 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
662 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100663 }
664 }
665
Eric Blake240f64b2016-04-28 15:45:14 -0600666 qiv = qmp_input_visitor_new(props, true);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000667 obj = user_creatable_add_type(type, id, pdict,
668 qmp_input_get_visitor(qiv), errp);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100669 qmp_input_visitor_cleanup(qiv);
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000670 if (obj) {
671 object_unref(obj);
672 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100673}
674
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100675void qmp_object_del(const char *id, Error **errp)
676{
Daniel P. Berrange90998d52016-02-10 18:40:59 +0000677 user_creatable_del(id, errp);
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100678}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200679
680MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
681{
682 MemoryDeviceInfoList *head = NULL;
683 MemoryDeviceInfoList **prev = &head;
684
685 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
686
687 return head;
688}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200689
690ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
691{
692 bool ambig;
693 ACPIOSTInfoList *head = NULL;
694 ACPIOSTInfoList **prev = &head;
695 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
696
697 if (obj) {
698 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
699 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
700
701 adevc->ospm_status(adev, &prev);
702 } else {
703 error_setg(errp, "command is not supported, missing ACPI device");
704 }
705
706 return head;
707}