blob: 1413de439f766c6d276d8d3e3ee77925a9836cb2 [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"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010027#include "qom/qom-qobject.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010028#include "qapi/qmp/qerror.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010029#include "qapi/qmp/qobject.h"
30#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020031#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010032#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020033#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020034#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050035
36NameInfo *qmp_query_name(Error **errp)
37{
38 NameInfo *info = g_malloc0(sizeof(*info));
39
40 if (qemu_name) {
41 info->has_name = true;
42 info->name = g_strdup(qemu_name);
43 }
44
45 return info;
46}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030047
Markus Armbruster7daecb32014-05-02 13:26:31 +020048VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030049{
Eric Blake4752cdb2015-05-04 09:05:31 -060050 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030051 const char *version = QEMU_VERSION;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050052 const char *tmp;
53 int err;
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030054
Eric Blake4752cdb2015-05-04 09:05:31 -060055 info->qemu = g_new0(VersionTriple, 1);
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050056 err = qemu_strtoll(version, &tmp, 10, &info->qemu->major);
57 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030058 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050059
60 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->minor);
61 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030062 tmp++;
Carlos L. Torresd4ba8cb2015-07-19 18:02:21 -050063
64 err = qemu_strtoll(tmp, &tmp, 10, &info->qemu->micro);
65 assert(err == 0);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030066 info->package = g_strdup(QEMU_PKGVERSION);
67
68 return info;
69}
Luiz Capitulino292a2602011-09-12 15:10:53 -030070
71KvmInfo *qmp_query_kvm(Error **errp)
72{
73 KvmInfo *info = g_malloc0(sizeof(*info));
74
75 info->enabled = kvm_enabled();
76 info->present = kvm_available();
77
78 return info;
79}
80
Luiz Capitulinoefab7672011-09-13 17:16:25 -030081UuidInfo *qmp_query_uuid(Error **errp)
82{
83 UuidInfo *info = g_malloc0(sizeof(*info));
84 char uuid[64];
85
86 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
87 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
88 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
89 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
90 qemu_uuid[14], qemu_uuid[15]);
91
92 info->UUID = g_strdup(uuid);
93 return info;
94}
95
Markus Armbruster7daecb32014-05-02 13:26:31 +020096void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030097{
98 no_shutdown = 0;
99 qemu_system_shutdown_request();
100}
101
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300102void qmp_stop(Error **errp)
103{
Paolo Bonzini1e998142012-10-23 14:54:21 +0200104 if (runstate_check(RUN_STATE_INMIGRATE)) {
105 autostart = 0;
106 } else {
107 vm_stop(RUN_STATE_PAUSED);
108 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300109}
110
Luiz Capitulino38d22652011-09-15 14:41:46 -0300111void qmp_system_reset(Error **errp)
112{
113 qemu_system_reset_request();
114}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300115
116void qmp_system_powerdown(Error **erp)
117{
118 qemu_system_powerdown_request();
119}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300120
121void qmp_cpu(int64_t index, Error **errp)
122{
123 /* Just do nothing */
124}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200125
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200126void qmp_cpu_add(int64_t id, Error **errp)
127{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200128 MachineClass *mc;
129
130 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300131 if (mc->hot_add_cpu) {
132 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200133 } else {
134 error_setg(errp, "Not supported");
135 }
136}
137
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200138#ifndef CONFIG_VNC
139/* If VNC support is enabled, the "true" query-vnc command is
140 defined in the VNC subsystem */
141VncInfo *qmp_query_vnc(Error **errp)
142{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100143 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200144 return NULL;
145};
Leon Yu89db21772015-02-02 05:08:51 +0000146
147VncInfo2List *qmp_query_vnc_servers(Error **errp)
148{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100149 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000150 return NULL;
151};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200152#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200153
154#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100155/*
156 * qmp-commands.hx ensures that QMP command query-spice exists only
157 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
158 * result. However, the QAPI schema is blissfully unaware of that,
159 * and the QAPI code generator happily generates a dead
Markus Armbruster7fad30f2015-09-16 13:06:19 +0200160 * qmp_marshal_query_spice() that calls qmp_query_spice(). Provide it
161 * one, or else linking fails. FIXME Educate the QAPI schema on
162 * CONFIG_SPICE.
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100163 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200164SpiceInfo *qmp_query_spice(Error **errp)
165{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100166 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200167};
168#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200169
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200170void qmp_cont(Error **errp)
171{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100172 Error *local_err = NULL;
Markus Armbrusterab319792014-05-02 13:26:42 +0200173 BlockDriverState *bs;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200174
Hu Taoede085b2013-04-26 11:24:40 +0800175 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400176 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200177 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300178 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
179 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200180 }
181
Markus Armbrusterab319792014-05-02 13:26:42 +0200182 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
183 bdrv_iostatus_reset(bs);
184 }
185 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100186 bdrv_add_key(bs, NULL, &local_err);
187 if (local_err) {
188 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200189 return;
190 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200191 }
192
Paolo Bonzini1e998142012-10-23 14:54:21 +0200193 if (runstate_check(RUN_STATE_INMIGRATE)) {
194 autostart = 1;
195 } else {
196 vm_start();
197 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200198}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600199
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100200void qmp_system_wakeup(Error **errp)
201{
202 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
203}
204
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600205ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600206{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600207 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600208 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600209 ObjectPropertyInfoList *props = NULL;
210 ObjectProperty *prop;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600211
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600212 obj = object_resolve_path(path, &ambiguous);
213 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400214 if (ambiguous) {
215 error_setg(errp, "Path '%s' is ambiguous", path);
216 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100217 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
218 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400219 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600220 return NULL;
221 }
222
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600223 QTAILQ_FOREACH(prop, &obj->properties, node) {
224 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600225
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600226 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600227 entry->next = props;
228 props = entry;
229
230 entry->value->name = g_strdup(prop->name);
231 entry->value->type = g_strdup(prop->type);
232 }
233
234 return props;
235}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600236
Markus Armbruster6eb39372015-09-16 13:06:25 +0200237void qmp_qom_set(const char *path, const char *property, QObject *value,
238 Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600239{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600240 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600241
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600242 obj = object_resolve_path(path, NULL);
243 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100244 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100245 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100246 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600247 }
248
Markus Armbruster485febc2015-03-13 17:25:50 +0100249 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600250}
251
Markus Armbruster6eb39372015-09-16 13:06:25 +0200252QObject *qmp_qom_get(const char *path, const char *property, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600253{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600254 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600255
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600256 obj = object_resolve_path(path, NULL);
257 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100258 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100259 "Device '%s' not found", path);
Markus Armbruster6eb39372015-09-16 13:06:25 +0200260 return NULL;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600261 }
262
Markus Armbruster6eb39372015-09-16 13:06:25 +0200263 return object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600264}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200265
266void qmp_set_password(const char *protocol, const char *password,
267 bool has_connected, const char *connected, Error **errp)
268{
269 int disconnect_if_connected = 0;
270 int fail_if_connected = 0;
271 int rc;
272
273 if (has_connected) {
274 if (strcmp(connected, "fail") == 0) {
275 fail_if_connected = 1;
276 } else if (strcmp(connected, "disconnect") == 0) {
277 disconnect_if_connected = 1;
278 } else if (strcmp(connected, "keep") == 0) {
279 /* nothing */
280 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100281 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200282 return;
283 }
284 }
285
286 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100287 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200288 return;
289 }
290 rc = qemu_spice_set_passwd(password, fail_if_connected,
291 disconnect_if_connected);
292 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100293 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200294 }
295 return;
296 }
297
298 if (strcmp(protocol, "vnc") == 0) {
299 if (fail_if_connected || disconnect_if_connected) {
300 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100301 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200302 return;
303 }
304 /* Note that setting an empty password will not disable login through
305 * this interface. */
306 rc = vnc_display_password(NULL, password);
307 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100308 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200309 }
310 return;
311 }
312
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100313 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200314}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200315
316void qmp_expire_password(const char *protocol, const char *whenstr,
317 Error **errp)
318{
319 time_t when;
320 int rc;
321
322 if (strcmp(whenstr, "now") == 0) {
323 when = 0;
324 } else if (strcmp(whenstr, "never") == 0) {
325 when = TIME_MAX;
326 } else if (whenstr[0] == '+') {
327 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
328 } else {
329 when = strtoull(whenstr, NULL, 10);
330 }
331
332 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100333 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200334 return;
335 }
336 rc = qemu_spice_set_pw_expire(when);
337 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100338 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200339 }
340 return;
341 }
342
343 if (strcmp(protocol, "vnc") == 0) {
344 rc = vnc_display_pw_expire(NULL, when);
345 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100346 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200347 }
348 return;
349 }
350
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100351 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200352}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200353
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200354#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200355void qmp_change_vnc_password(const char *password, Error **errp)
356{
357 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100358 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200359 }
360}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200361
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200362static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200363{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200364 QemuOptsList *olist = qemu_find_opts("vnc");
365 QemuOpts *opts;
366
367 if (strstr(target, "id=")) {
368 error_setg(errp, "id not supported");
369 return;
370 }
371
372 opts = qemu_opts_find(olist, "default");
373 if (opts) {
374 qemu_opts_del(opts);
375 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100376 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800377 if (!opts) {
378 return;
379 }
380
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200381 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200382}
383
384static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
385 Error **errp)
386{
387 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
388 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100389 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200390 } else {
391 qmp_change_vnc_password(arg, errp);
392 }
393 } else {
394 qmp_change_vnc_listen(target, errp);
395 }
396}
397#else
398void qmp_change_vnc_password(const char *password, Error **errp)
399{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100400 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200401}
402static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
403 Error **errp)
404{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100405 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200406}
407#endif /* !CONFIG_VNC */
408
409void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200410 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200411{
412 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200413 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200414 } else {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200415 qmp_change_blockdev(device, target, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200416 }
417}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600418
419static void qom_list_types_tramp(ObjectClass *klass, void *data)
420{
421 ObjectTypeInfoList *e, **pret = data;
422 ObjectTypeInfo *info;
423
424 info = g_malloc0(sizeof(*info));
425 info->name = g_strdup(object_class_get_name(klass));
426
427 e = g_malloc0(sizeof(*e));
428 e->value = info;
429 e->next = *pret;
430 *pret = e;
431}
432
433ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
434 const char *implements,
435 bool has_abstract,
436 bool abstract,
437 Error **errp)
438{
439 ObjectTypeInfoList *ret = NULL;
440
441 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
442
443 return ret;
444}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500445
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200446/* Return a DevicePropertyInfo for a qdev property.
447 *
448 * If a qdev property with the given name does not exist, use the given default
449 * type. If the qdev property info should not be shown, return NULL.
450 *
451 * The caller must free the return value.
452 */
453static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
454 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800455 const char *default_type,
456 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200457{
458 DevicePropertyInfo *info;
459 Property *prop;
460
461 do {
462 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
463 if (strcmp(name, prop->name) != 0) {
464 continue;
465 }
466
467 /*
468 * TODO Properties without a parser are just for dirty hacks.
469 * qdev_prop_ptr is the only such PropertyInfo. It's marked
470 * for removal. This conditional should be removed along with
471 * it.
472 */
473 if (!prop->info->set) {
474 return NULL; /* no way to set it, don't show */
475 }
476
477 info = g_malloc0(sizeof(*info));
478 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800479 info->type = g_strdup(prop->info->name);
480 info->has_description = !!prop->info->description;
481 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200482 return info;
483 }
484 klass = object_class_get_parent(klass);
485 } while (klass != object_class_by_name(TYPE_DEVICE));
486
487 /* Not a qdev property, use the default type */
488 info = g_malloc0(sizeof(*info));
489 info->name = g_strdup(name);
490 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800491 info->has_description = !!description;
492 info->description = g_strdup(description);
493
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200494 return info;
495}
496
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500497DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
498 Error **errp)
499{
500 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200501 Object *obj;
502 ObjectProperty *prop;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500503 DevicePropertyInfoList *prop_list = NULL;
504
505 klass = object_class_by_name(typename);
506 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100507 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
508 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500509 return NULL;
510 }
511
512 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
513 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100514 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500515 return NULL;
516 }
517
Markus Armbrusteredb15232015-10-01 10:59:57 +0200518 if (object_class_is_abstract(klass)) {
519 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name",
520 "non-abstract device type");
521 return NULL;
522 }
523
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200524 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500525
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200526 QTAILQ_FOREACH(prop, &obj->properties, node) {
527 DevicePropertyInfo *info;
528 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500529
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200530 /* Skip Object and DeviceState properties */
531 if (strcmp(prop->name, "type") == 0 ||
532 strcmp(prop->name, "realized") == 0 ||
533 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200534 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200535 strcmp(prop->name, "parent_bus") == 0) {
536 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500537 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200538
539 /* Skip legacy properties since they are just string versions of
540 * properties that we already list.
541 */
542 if (strstart(prop->name, "legacy-", NULL)) {
543 continue;
544 }
545
Gonglei07d09c52014-10-07 14:33:23 +0800546 info = make_device_property_info(klass, prop->name, prop->type,
547 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200548 if (!info) {
549 continue;
550 }
551
552 entry = g_malloc0(sizeof(*entry));
553 entry->value = info;
554 entry->next = prop_list;
555 prop_list = entry;
556 }
557
558 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500559
560 return prop_list;
561}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500562
Anthony Liguori76b64a72012-08-14 22:17:36 -0500563CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
564{
565 return arch_query_cpu_definitions(errp);
566}
567
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300568void qmp_add_client(const char *protocol, const char *fdname,
569 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
570 Error **errp)
571{
572 CharDriverState *s;
573 int fd;
574
575 fd = monitor_get_fd(cur_mon, fdname, errp);
576 if (fd < 0) {
577 return;
578 }
579
580 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100581 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300582 close(fd);
583 return;
584 }
585 skipauth = has_skipauth ? skipauth : false;
586 tls = has_tls ? tls : false;
587 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
588 error_setg(errp, "spice failed to add client");
589 close(fd);
590 }
591 return;
592#ifdef CONFIG_VNC
593 } else if (strcmp(protocol, "vnc") == 0) {
594 skipauth = has_skipauth ? skipauth : false;
595 vnc_display_add_client(NULL, fd, skipauth);
596 return;
597#endif
598 } else if ((s = qemu_chr_find(protocol)) != NULL) {
599 if (qemu_chr_add_client(s, fd) < 0) {
600 error_setg(errp, "failed to add client");
601 close(fd);
602 return;
603 }
604 return;
605 }
606
607 error_setg(errp, "protocol '%s' is invalid", protocol);
608 close(fd);
609}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100610
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100611void object_add(const char *type, const char *id, const QDict *qdict,
612 Visitor *v, Error **errp)
613{
614 Object *obj;
Eduardo Habkostc3481242014-04-16 14:39:38 -0300615 ObjectClass *klass;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100616 const QDictEntry *e;
617 Error *local_err = NULL;
618
Eduardo Habkostc3481242014-04-16 14:39:38 -0300619 klass = object_class_by_name(type);
620 if (!klass) {
Paolo Bonzinid1169462014-05-14 17:43:13 +0800621 error_setg(errp, "invalid object type: %s", type);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100622 return;
623 }
624
Eduardo Habkostc3481242014-04-16 14:39:38 -0300625 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
626 error_setg(errp, "object type '%s' isn't supported by object-add",
627 type);
628 return;
629 }
630
631 if (object_class_is_abstract(klass)) {
632 error_setg(errp, "object type '%s' is abstract", type);
633 return;
634 }
635
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100636 obj = object_new(type);
637 if (qdict) {
638 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
639 object_property_set(obj, v, e->key, &local_err);
640 if (local_err) {
Igor Mammedov69252c02014-01-16 17:34:36 +0100641 goto out;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100642 }
643 }
644 }
645
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100646 object_property_add_child(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200647 id, obj, &local_err);
Igor Mammedov269e09f2014-01-16 17:34:38 +0100648 if (local_err) {
649 goto out;
650 }
651
Igor Mammedova790f4e2014-06-02 15:24:59 +0200652 user_creatable_complete(obj, &local_err);
653 if (local_err) {
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100654 object_property_del(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200655 id, &error_abort);
656 goto out;
657 }
Igor Mammedov69252c02014-01-16 17:34:36 +0100658out:
659 if (local_err) {
660 error_propagate(errp, local_err);
661 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100662 object_unref(obj);
663}
664
Markus Armbruster6eb39372015-09-16 13:06:25 +0200665void qmp_object_add(const char *type, const char *id,
666 bool has_props, QObject *props, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100667{
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100668 const QDict *pdict = NULL;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100669 QmpInputVisitor *qiv;
670
671 if (props) {
672 pdict = qobject_to_qdict(props);
673 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100674 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
675 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100676 }
677 }
678
679 qiv = qmp_input_visitor_new(props);
Markus Armbruster485febc2015-03-13 17:25:50 +0100680 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100681 qmp_input_visitor_cleanup(qiv);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100682}
683
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100684void qmp_object_del(const char *id, Error **errp)
685{
686 Object *container;
687 Object *obj;
688
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100689 container = object_get_objects_root();
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100690 obj = object_resolve_path_component(container, id);
691 if (!obj) {
692 error_setg(errp, "object id not found");
693 return;
694 }
Lin Mad6edb152015-03-30 16:36:28 +0800695
696 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
697 error_setg(errp, "%s is in use, can not be deleted", id);
698 return;
699 }
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100700 object_unparent(obj);
701}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200702
703MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
704{
705 MemoryDeviceInfoList *head = NULL;
706 MemoryDeviceInfoList **prev = &head;
707
708 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
709
710 return head;
711}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200712
713ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
714{
715 bool ambig;
716 ACPIOSTInfoList *head = NULL;
717 ACPIOSTInfoList **prev = &head;
718 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
719
720 if (obj) {
721 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
722 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
723
724 adevc->ospm_status(adev, &prev);
725 } else {
726 error_setg(errp, "command is not supported, missing ACPI device");
727 }
728
729 return head;
730}