blob: 3ff6db79b9b78911b0c39930c50293caa3f1cf92 [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
16#include "qemu-common.h"
Markus Armbrustera0b1a662015-03-17 18:16:21 +010017#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010018#include "sysemu/sysemu.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050019#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020020#include "sysemu/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020021#include "ui/qemu-spice.h"
22#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010023#include "sysemu/kvm.h"
24#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060025#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010026#include "sysemu/blockdev.h"
Max Reitz373340b2015-10-19 17:53:22 +020027#include "sysemu/block-backend.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010028#include "qom/qom-qobject.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010029#include "qapi/qmp/qerror.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010030#include "qapi/qmp/qobject.h"
31#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020032#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010033#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020034#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020035#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050036
37NameInfo *qmp_query_name(Error **errp)
38{
39 NameInfo *info = g_malloc0(sizeof(*info));
40
41 if (qemu_name) {
42 info->has_name = true;
43 info->name = g_strdup(qemu_name);
44 }
45
46 return info;
47}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030048
Markus Armbruster7daecb32014-05-02 13:26:31 +020049VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030050{
Eric Blake4752cdb2015-05-04 09:05:31 -060051 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030052 const char *version = QEMU_VERSION;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050053 const char *tmp;
54 int err;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030055
Eric Blake4752cdb2015-05-04 09:05:31 -060056 info->qemu = g_new0(VersionTriple, 1);
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050057 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
58 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030059 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050060
61 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
62 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030063 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050064
65 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
66 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030067 info->package = g_strdup(QEMU_PKGVERSION);
68
69 return info;
70}
Luiz Capitulino292a2602011-09-12 15:10:53 -030071
72KvmInfo *qmp_query_kvm(Error **errp)
73{
74 KvmInfo *info = g_malloc0(sizeof(*info));
75
76 info->enabled = kvm_enabled();
77 info->present = kvm_available();
78
79 return info;
80}
81
Luiz Capitulinoefab7672011-09-13 17:16:25 -030082UuidInfo *qmp_query_uuid(Error **errp)
83{
84 UuidInfo *info = g_malloc0(sizeof(*info));
85 char uuid[64];
86
87 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
88 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
89 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
90 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
91 qemu_uuid[14], qemu_uuid[15]);
92
93 info->UUID = g_strdup(uuid);
94 return info;
95}
96
Markus Armbruster7daecb32014-05-02 13:26:31 +020097void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030098{
99 no_shutdown = 0;
100 qemu_system_shutdown_request();
101}
102
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300103void qmp_stop(Error **errp)
104{
Paolo Bonzini1e998142012-10-23 14:54:21 +0200105 if (runstate_check(RUN_STATE_INMIGRATE)) {
106 autostart = 0;
107 } else {
108 vm_stop(RUN_STATE_PAUSED);
109 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300110}
111
Luiz Capitulino38d22652011-09-15 14:41:46 -0300112void qmp_system_reset(Error **errp)
113{
114 qemu_system_reset_request();
115}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300116
117void qmp_system_powerdown(Error **erp)
118{
119 qemu_system_powerdown_request();
120}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300121
122void qmp_cpu(int64_t index, Error **errp)
123{
124 /* Just do nothing */
125}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200126
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200127void qmp_cpu_add(int64_t id, Error **errp)
128{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200129 MachineClass *mc;
130
131 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300132 if (mc->hot_add_cpu) {
133 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200134 } else {
135 error_setg(errp, "Not supported");
136 }
137}
138
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200139#ifndef CONFIG_VNC
140/* If VNC support is enabled, the "true" query-vnc command is
141 defined in the VNC subsystem */
142VncInfo *qmp_query_vnc(Error **errp)
143{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100144 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200145 return NULL;
146};
Leon Yu89db21772015-02-02 05:08:51 +0000147
148VncInfo2List *qmp_query_vnc_servers(Error **errp)
149{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100150 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000151 return NULL;
152};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200153#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200154
155#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100156/*
157 * qmp-commands.hx ensures that QMP command query-spice exists only
158 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
159 * result. However, the QAPI schema is blissfully unaware of that,
160 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200161 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
162 * one, or else linking fails. FIXME Educate the QAPI schema on
163 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100164 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200165SpiceInfo *qmp_query_spice(Error **errp)
166{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100167 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200168};
169#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200170
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200171void qmp_cont(Error **errp)
172{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100173 Error *local_err = NULL;
Max Reitz373340b2015-10-19 17:53:22 +0200174 BlockBackend *blk;
Markus Armbrusterab319792014-05-02 13:26:42 +0200175 BlockDriverState *bs;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200176
Hu Taoede085b2013-04-26 11:24:40 +0800177 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400178 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200179 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300180 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
181 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200182 }
183
Max Reitz373340b2015-10-19 17:53:22 +0200184 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
185 blk_iostatus_reset(blk);
Markus Armbrusterab319792014-05-02 13:26:42 +0200186 }
187 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100188 bdrv_add_key(bs, NULL, &local_err);
189 if (local_err) {
190 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200191 return;
192 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200193 }
194
Paolo Bonzini1e998142012-10-23 14:54:21 +0200195 if (runstate_check(RUN_STATE_INMIGRATE)) {
196 autostart = 1;
197 } else {
198 vm_start();
199 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200200}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600201
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100202void qmp_system_wakeup(Error **errp)
203{
204 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
205}
206
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600207ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600208{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600209 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600210 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600211 ObjectPropertyInfoList *props = NULL;
212 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000213 ObjectPropertyIterator iter;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600214
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600215 obj = object_resolve_path(path, &ambiguous);
216 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400217 if (ambiguous) {
218 error_setg(errp, "Path '%s' is ambiguous", path);
219 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100220 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
221 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400222 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600223 return NULL;
224 }
225
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000226 object_property_iter_init(&iter, obj);
227 while ((prop = object_property_iter_next(&iter))) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600228 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600229
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600230 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600231 entry->next = props;
232 props = entry;
233
234 entry->value->name = g_strdup(prop->name);
235 entry->value->type = g_strdup(prop->type);
236 }
237
238 return props;
239}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600240
Markus Armbruster6eb39372015-09-16 13:06:25 +0200241void qmp_qom_set(const char *path, const char *property, QObject *value,
242 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600243{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600244 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600245
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600246 obj = object_resolve_path(path, NULL);
247 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100248 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100249 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100250 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600251 }
252
Markus Armbruster485febc2015-03-13 17:25:50 +0100253 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600254}
255
Markus Armbruster6eb39372015-09-16 13:06:25 +0200256QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600257{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600258 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600259
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600260 obj = object_resolve_path(path, NULL);
261 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100262 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100263 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200264 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600265 }
266
Markus Armbruster6eb39372015-09-16 13:06:25 +0200267 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600268}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200269
270void qmp_set_password(const char *protocol, const char *password,
271 bool has_connected, const char *connected, Error **errp)
272{
273 int disconnect_if_connected = 0;
274 int fail_if_connected = 0;
275 int rc;
276
277 if (has_connected) {
278 if (strcmp(connected, "fail") == 0) {
279 fail_if_connected = 1;
280 } else if (strcmp(connected, "disconnect") == 0) {
281 disconnect_if_connected = 1;
282 } else if (strcmp(connected, "keep") == 0) {
283 /* nothing */
284 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100285 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200286 return;
287 }
288 }
289
290 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100291 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200292 return;
293 }
294 rc = qemu_spice_set_passwd(password, fail_if_connected,
295 disconnect_if_connected);
296 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100297 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200298 }
299 return;
300 }
301
302 if (strcmp(protocol, "vnc") == 0) {
303 if (fail_if_connected || disconnect_if_connected) {
304 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100305 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200306 return;
307 }
308 /* Note that setting an empty password will not disable login through
309 * this interface. */
310 rc = vnc_display_password(NULL, password);
311 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100312 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200313 }
314 return;
315 }
316
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100317 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200318}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200319
320void qmp_expire_password(const char *protocol, const char *whenstr,
321 Error **errp)
322{
323 time_t when;
324 int rc;
325
326 if (strcmp(whenstr, "now") == 0) {
327 when = 0;
328 } else if (strcmp(whenstr, "never") == 0) {
329 when = TIME_MAX;
330 } else if (whenstr[0] == '+') {
331 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
332 } else {
333 when = strtoull(whenstr, NULL, 10);
334 }
335
336 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100337 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200338 return;
339 }
340 rc = qemu_spice_set_pw_expire(when);
341 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100342 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200343 }
344 return;
345 }
346
347 if (strcmp(protocol, "vnc") == 0) {
348 rc = vnc_display_pw_expire(NULL, when);
349 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100350 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200351 }
352 return;
353 }
354
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100355 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200356}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200357
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200358#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200359void qmp_change_vnc_password(const char *password, Error **errp)
360{
361 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100362 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200363 }
364}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200365
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200366static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200367{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200368 QemuOptsList *olist = qemu_find_opts("vnc");
369 QemuOpts *opts;
370
371 if (strstr(target, "id=")) {
372 error_setg(errp, "id not supported");
373 return;
374 }
375
376 opts = qemu_opts_find(olist, "default");
377 if (opts) {
378 qemu_opts_del(opts);
379 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100380 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800381 if (!opts) {
382 return;
383 }
384
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200385 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200386}
387
388static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
389 Error **errp)
390{
391 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
392 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100393 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200394 } else {
395 qmp_change_vnc_password(arg, errp);
396 }
397 } else {
398 qmp_change_vnc_listen(target, errp);
399 }
400}
401#else
402void qmp_change_vnc_password(const char *password, Error **errp)
403{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100404 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200405}
406static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
407 Error **errp)
408{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100409 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200410}
411#endif /* !CONFIG_VNC */
412
413void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200414 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200415{
416 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200417 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200418 } else {
Max Reitz39ff43d2015-11-11 04:49:44 +0100419 qmp_blockdev_change_medium(device, target, has_arg, arg, false, 0,
420 errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200421 }
422}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600423
424static void qom_list_types_tramp(ObjectClass *klass, void *data)
425{
426 ObjectTypeInfoList *e, **pret = data;
427 ObjectTypeInfo *info;
428
429 info = g_malloc0(sizeof(*info));
430 info->name = g_strdup(object_class_get_name(klass));
431
432 e = g_malloc0(sizeof(*e));
433 e->value = info;
434 e->next = *pret;
435 *pret = e;
436}
437
438ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
439 const char *implements,
440 bool has_abstract,
441 bool abstract,
442 Error **errp)
443{
444 ObjectTypeInfoList *ret = NULL;
445
446 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
447
448 return ret;
449}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500450
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200451/* Return a DevicePropertyInfo for a qdev property.
452 *
453 * If a qdev property with the given name does not exist, use the given default
454 * type. If the qdev property info should not be shown, return NULL.
455 *
456 * The caller must free the return value.
457 */
458static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
459 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800460 const char *default_type,
461 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200462{
463 DevicePropertyInfo *info;
464 Property *prop;
465
466 do {
467 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
468 if (strcmp(name, prop->name) != 0) {
469 continue;
470 }
471
472 /*
473 * TODO Properties without a parser are just for dirty hacks.
474 * qdev_prop_ptr is the only such PropertyInfo. It's marked
475 * for removal. This conditional should be removed along with
476 * it.
477 */
478 if (!prop->info->set) {
479 return NULL; /* no way to set it, don't show */
480 }
481
482 info = g_malloc0(sizeof(*info));
483 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800484 info->type = g_strdup(prop->info->name);
485 info->has_description = !!prop->info->description;
486 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200487 return info;
488 }
489 klass = object_class_get_parent(klass);
490 } while (klass != object_class_by_name(TYPE_DEVICE));
491
492 /* Not a qdev property, use the default type */
493 info = g_malloc0(sizeof(*info));
494 info->name = g_strdup(name);
495 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800496 info->has_description = !!description;
497 info->description = g_strdup(description);
498
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200499 return info;
500}
501
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500502DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
503 Error **errp)
504{
505 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200506 Object *obj;
507 ObjectProperty *prop;
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000508 ObjectPropertyIterator iter;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500509 DevicePropertyInfoList *prop_list = NULL;
510
511 klass = object_class_by_name(typename);
512 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100513 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
514 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500515 return NULL;
516 }
517
518 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
519 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100520 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500521 return NULL;
522 }
523
Markus Armbrusteredb15232015-10-01 10:59:57 +0200524 if (object_class_is_abstract(klass)) {
525 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
526 "non-abstract device type");
527 return NULL;
528 }
529
Markus Armbruster4c315c22015-10-01 10:59:58 +0200530 if (DEVICE_CLASS(klass)->cannot_destroy_with_object_finalize_yet) {
531 error_setg(errp, "Can't list properties of device '%s'", typename);
532 return NULL;
533 }
534
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200535 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500536
Daniel P. Berrange7746abd2015-12-09 12:34:02 +0000537 object_property_iter_init(&iter, obj);
538 while ((prop = object_property_iter_next(&iter))) {
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200539 DevicePropertyInfo *info;
540 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500541
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200542 /* Skip Object and DeviceState properties */
543 if (strcmp(prop->name, "type") == 0 ||
544 strcmp(prop->name, "realized") == 0 ||
545 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200546 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200547 strcmp(prop->name, "parent_bus") == 0) {
548 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500549 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200550
551 /* Skip legacy properties since they are just string versions of
552 * properties that we already list.
553 */
554 if (strstart(prop->name, "legacy-", NULL)) {
555 continue;
556 }
557
Gonglei07d09c52014-10-07 14:33:23 +0800558 info = make_device_property_info(klass, prop->name, prop->type,
559 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200560 if (!info) {
561 continue;
562 }
563
564 entry = g_malloc0(sizeof(*entry));
565 entry->value = info;
566 entry->next = prop_list;
567 prop_list = entry;
568 }
569
570 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500571
572 return prop_list;
573}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500574
Anthony Liguori76b64a72012-08-14 22:17:36 -0500575CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
576{
577 return arch_query_cpu_definitions(errp);
578}
579
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300580void qmp_add_client(const char *protocol, const char *fdname,
581 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
582 Error **errp)
583{
584 CharDriverState *s;
585 int fd;
586
587 fd = monitor_get_fd(cur_mon, fdname, errp);
588 if (fd < 0) {
589 return;
590 }
591
592 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100593 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300594 close(fd);
595 return;
596 }
597 skipauth = has_skipauth ? skipauth : false;
598 tls = has_tls ? tls : false;
599 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
600 error_setg(errp, "spice failed to add client");
601 close(fd);
602 }
603 return;
604#ifdef CONFIG_VNC
605 } else if (strcmp(protocol, "vnc") == 0) {
606 skipauth = has_skipauth ? skipauth : false;
607 vnc_display_add_client(NULL, fd, skipauth);
608 return;
609#endif
610 } else if ((s = qemu_chr_find(protocol)) != NULL) {
611 if (qemu_chr_add_client(s, fd) < 0) {
612 error_setg(errp, "failed to add client");
613 close(fd);
614 return;
615 }
616 return;
617 }
618
619 error_setg(errp, "protocol '%s' is invalid", protocol);
620 close(fd);
621}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100622
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100623void object_add(const char *type, const char *id, const QDict *qdict,
624 Visitor *v, Error **errp)
625{
626 Object *obj;
Eduardo Habkostc3481242014-04-16 14:39:38 -0300627 ObjectClass *klass;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100628 const QDictEntry *e;
629 Error *local_err = NULL;
630
Eduardo Habkostc3481242014-04-16 14:39:38 -0300631 klass = object_class_by_name(type);
632 if (!klass) {
Paolo Bonzinid1169462014-05-14 17:43:13 +0800633 error_setg(errp, "invalid object type: %s", type);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100634 return;
635 }
636
Eduardo Habkostc3481242014-04-16 14:39:38 -0300637 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
638 error_setg(errp, "object type '%s' isn't supported by object-add",
639 type);
640 return;
641 }
642
643 if (object_class_is_abstract(klass)) {
644 error_setg(errp, "object type '%s' is abstract", type);
645 return;
646 }
647
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100648 obj = object_new(type);
649 if (qdict) {
650 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
651 object_property_set(obj, v, e->key, &local_err);
652 if (local_err) {
Igor Mammedov69252c02014-01-16 17:34:36 +0100653 goto out;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100654 }
655 }
656 }
657
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100658 object_property_add_child(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200659 id, obj, &local_err);
Igor Mammedov269e09f2014-01-16 17:34:38 +0100660 if (local_err) {
661 goto out;
662 }
663
Igor Mammedova790f4e2014-06-02 15:24:59 +0200664 user_creatable_complete(obj, &local_err);
665 if (local_err) {
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100666 object_property_del(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200667 id, &error_abort);
668 goto out;
669 }
Igor Mammedov69252c02014-01-16 17:34:36 +0100670out:
671 if (local_err) {
672 error_propagate(errp, local_err);
673 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100674 object_unref(obj);
675}
676
Markus Armbruster6eb39372015-09-16 13:06:25 +0200677void qmp_object_add(const char *type, const char *id,
678 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100679{
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100680 const QDict *pdict = NULL;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100681 QmpInputVisitor *qiv;
682
683 if (props) {
684 pdict = qobject_to_qdict(props);
685 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100686 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
687 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100688 }
689 }
690
691 qiv = qmp_input_visitor_new(props);
Markus Armbruster485febc2015-03-13 17:25:50 +0100692 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100693 qmp_input_visitor_cleanup(qiv);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100694}
695
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100696void qmp_object_del(const char *id, Error **errp)
697{
698 Object *container;
699 Object *obj;
700
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100701 container = object_get_objects_root();
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100702 obj = object_resolve_path_component(container, id);
703 if (!obj) {
704 error_setg(errp, "object id not found");
705 return;
706 }
Lin Mad6edb152015-03-30 16:36:28 +0800707
708 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
709 error_setg(errp, "%s is in use, can not be deleted", id);
710 return;
711 }
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100712 object_unparent(obj);
713}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200714
715MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
716{
717 MemoryDeviceInfoList *head = NULL;
718 MemoryDeviceInfoList **prev = &head;
719
720 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
721
722 return head;
723}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200724
725ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
726{
727 bool ambig;
728 ACPIOSTInfoList *head = NULL;
729 ACPIOSTInfoList **prev = &head;
730 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
731
732 if (obj) {
733 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
734 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
735
736 adevc->ospm_status(adev, &prev);
737 } else {
738 error_setg(errp, "command is not supported, missing ACPI device");
739 }
740
741 return head;
742}