blob: 03eba96193b2c283d06ad58697a1361b169e1e61 [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"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010017#include "sysemu/sysemu.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050018#include "qmp-commands.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020019#include "sysemu/char.h"
Luiz Capitulinofbf796f2011-12-07 11:17:51 -020020#include "ui/qemu-spice.h"
21#include "ui/vnc.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010022#include "sysemu/kvm.h"
23#include "sysemu/arch_init.h"
Anthony Liguorib4b12c62011-12-12 14:29:34 -060024#include "hw/qdev.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010025#include "sysemu/blockdev.h"
Paolo Bonzini14cccb62012-12-17 18:19:50 +010026#include "qom/qom-qobject.h"
Markus Armbrustercc7a8ea2015-03-17 17:22:46 +010027#include "qapi/qmp/qerror.h"
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +010028#include "qapi/qmp/qobject.h"
29#include "qapi/qmp-input-visitor.h"
Igor Mammedov69ca3ea2013-04-30 15:41:25 +020030#include "hw/boards.h"
Igor Mammedov269e09f2014-01-16 17:34:38 +010031#include "qom/object_interfaces.h"
Igor Mammedov6f2e2732014-06-16 19:12:25 +020032#include "hw/mem/pc-dimm.h"
Igor Mammedov02419bc2014-06-16 19:12:28 +020033#include "hw/acpi/acpi_dev_interface.h"
Anthony Liguori48a32be2011-09-02 12:34:48 -050034
35NameInfo *qmp_query_name(Error **errp)
36{
37 NameInfo *info = g_malloc0(sizeof(*info));
38
39 if (qemu_name) {
40 info->has_name = true;
41 info->name = g_strdup(qemu_name);
42 }
43
44 return info;
45}
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030046
Markus Armbruster7daecb32014-05-02 13:26:31 +020047VersionInfo *qmp_query_version(Error **errp)
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030048{
Eric Blake4752cdb2015-05-04 09:05:31 -060049 VersionInfo *info = g_new0(VersionInfo, 1);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030050 const char *version = QEMU_VERSION;
51 char *tmp;
52
Eric Blake4752cdb2015-05-04 09:05:31 -060053 info->qemu = g_new0(VersionTriple, 1);
54 info->qemu->major = strtol(version, &tmp, 10);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030055 tmp++;
Eric Blake4752cdb2015-05-04 09:05:31 -060056 info->qemu->minor = strtol(tmp, &tmp, 10);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030057 tmp++;
Eric Blake4752cdb2015-05-04 09:05:31 -060058 info->qemu->micro = strtol(tmp, &tmp, 10);
Luiz Capitulinob9c15f12011-08-26 17:38:13 -030059 info->package = g_strdup(QEMU_PKGVERSION);
60
61 return info;
62}
Luiz Capitulino292a2602011-09-12 15:10:53 -030063
64KvmInfo *qmp_query_kvm(Error **errp)
65{
66 KvmInfo *info = g_malloc0(sizeof(*info));
67
68 info->enabled = kvm_enabled();
69 info->present = kvm_available();
70
71 return info;
72}
73
Luiz Capitulinoefab7672011-09-13 17:16:25 -030074UuidInfo *qmp_query_uuid(Error **errp)
75{
76 UuidInfo *info = g_malloc0(sizeof(*info));
77 char uuid[64];
78
79 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
80 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
81 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
82 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
83 qemu_uuid[14], qemu_uuid[15]);
84
85 info->UUID = g_strdup(uuid);
86 return info;
87}
88
Markus Armbruster7daecb32014-05-02 13:26:31 +020089void qmp_quit(Error **errp)
Luiz Capitulino7a7f3252011-09-15 14:20:28 -030090{
91 no_shutdown = 0;
92 qemu_system_shutdown_request();
93}
94
Luiz Capitulino5f158f22011-09-15 14:34:39 -030095void qmp_stop(Error **errp)
96{
Paolo Bonzini1e998142012-10-23 14:54:21 +020097 if (runstate_check(RUN_STATE_INMIGRATE)) {
98 autostart = 0;
99 } else {
100 vm_stop(RUN_STATE_PAUSED);
101 }
Luiz Capitulino5f158f22011-09-15 14:34:39 -0300102}
103
Luiz Capitulino38d22652011-09-15 14:41:46 -0300104void qmp_system_reset(Error **errp)
105{
106 qemu_system_reset_request();
107}
Luiz Capitulino5bc465e2011-09-28 11:06:15 -0300108
109void qmp_system_powerdown(Error **erp)
110{
111 qemu_system_powerdown_request();
112}
Luiz Capitulino755f1962011-10-06 14:31:39 -0300113
114void qmp_cpu(int64_t index, Error **errp)
115{
116 /* Just do nothing */
117}
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200118
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200119void qmp_cpu_add(int64_t id, Error **errp)
120{
Marcel Apfelbaum0056ae22014-03-05 19:30:47 +0200121 MachineClass *mc;
122
123 mc = MACHINE_GET_CLASS(current_machine);
Marcel Apfelbaum958db902014-04-09 20:34:53 +0300124 if (mc->hot_add_cpu) {
125 mc->hot_add_cpu(id, errp);
Igor Mammedov69ca3ea2013-04-30 15:41:25 +0200126 } else {
127 error_setg(errp, "Not supported");
128 }
129}
130
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200131#ifndef CONFIG_VNC
132/* If VNC support is enabled, the "true" query-vnc command is
133 defined in the VNC subsystem */
134VncInfo *qmp_query_vnc(Error **errp)
135{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100136 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200137 return NULL;
138};
Leon Yu89db21772015-02-02 05:08:51 +0000139
140VncInfo2List *qmp_query_vnc_servers(Error **errp)
141{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100142 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Leon Yu89db21772015-02-02 05:08:51 +0000143 return NULL;
144};
Luiz Capitulino2b54aa82011-10-17 16:41:22 -0200145#endif
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200146
147#ifndef CONFIG_SPICE
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100148/*
149 * qmp-commands.hx ensures that QMP command query-spice exists only
150 * #ifdef CONFIG_SPICE. Necessary for an accurate query-commands
151 * result. However, the QAPI schema is blissfully unaware of that,
152 * and the QAPI code generator happily generates a dead
153 * qmp_marshal_input_query_spice() that calls qmp_query_spice().
154 * Provide it one, or else linking fails.
155 * FIXME Educate the QAPI schema on CONFIG_SPICE.
156 */
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200157SpiceInfo *qmp_query_spice(Error **errp)
158{
Markus Armbrusterad0ec142015-01-13 15:56:11 +0100159 abort();
Luiz Capitulinod1f29642011-10-20 17:01:33 -0200160};
161#endif
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200162
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200163void qmp_cont(Error **errp)
164{
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100165 Error *local_err = NULL;
Markus Armbrusterab319792014-05-02 13:26:42 +0200166 BlockDriverState *bs;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200167
Hu Taoede085b2013-04-26 11:24:40 +0800168 if (runstate_needs_reset()) {
Cole Robinsonf231b882014-03-21 19:42:26 -0400169 error_setg(errp, "Resetting the Virtual Machine is required");
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200170 return;
Luiz Capitulinoad02b962012-04-27 13:33:36 -0300171 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
172 return;
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200173 }
174
Markus Armbrusterab319792014-05-02 13:26:42 +0200175 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
176 bdrv_iostatus_reset(bs);
177 }
178 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
Markus Armbruster4d2855a2015-01-29 10:37:00 +0100179 bdrv_add_key(bs, NULL, &local_err);
180 if (local_err) {
181 error_propagate(errp, local_err);
Markus Armbrusterab319792014-05-02 13:26:42 +0200182 return;
183 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200184 }
185
Paolo Bonzini1e998142012-10-23 14:54:21 +0200186 if (runstate_check(RUN_STATE_INMIGRATE)) {
187 autostart = 1;
188 } else {
189 vm_start();
190 }
Luiz Capitulinoe42e8182011-11-22 17:58:31 -0200191}
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600192
Gerd Hoffmann9b9df252012-02-23 13:45:21 +0100193void qmp_system_wakeup(Error **errp)
194{
195 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
196}
197
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600198ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600199{
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600200 Object *obj;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600201 bool ambiguous = false;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600202 ObjectPropertyInfoList *props = NULL;
203 ObjectProperty *prop;
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600204
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600205 obj = object_resolve_path(path, &ambiguous);
206 if (obj == NULL) {
Michael Tokarev79772082014-05-04 12:23:59 +0400207 if (ambiguous) {
208 error_setg(errp, "Path '%s' is ambiguous", path);
209 } else {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100210 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
211 "Device '%s' not found", path);
Michael Tokarev79772082014-05-04 12:23:59 +0400212 }
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600213 return NULL;
214 }
215
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600216 QTAILQ_FOREACH(prop, &obj->properties, node) {
217 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600218
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600219 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
Anthony Liguorib4b12c62011-12-12 14:29:34 -0600220 entry->next = props;
221 props = entry;
222
223 entry->value->name = g_strdup(prop->name);
224 entry->value->type = g_strdup(prop->type);
225 }
226
227 return props;
228}
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600229
230/* FIXME: teach qapi about how to pass through Visitors */
Markus Armbruster485febc2015-03-13 17:25:50 +0100231void qmp_qom_set(QDict *qdict, QObject **ret, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600232{
233 const char *path = qdict_get_str(qdict, "path");
234 const char *property = qdict_get_str(qdict, "property");
235 QObject *value = qdict_get(qdict, "value");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600236 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600237
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600238 obj = object_resolve_path(path, NULL);
239 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100240 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100241 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100242 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600243 }
244
Markus Armbruster485febc2015-03-13 17:25:50 +0100245 object_property_set_qobject(obj, value, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600246}
247
Markus Armbruster485febc2015-03-13 17:25:50 +0100248void qmp_qom_get(QDict *qdict, QObject **ret, Error **errp)
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600249{
250 const char *path = qdict_get_str(qdict, "path");
251 const char *property = qdict_get_str(qdict, "property");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600252 Object *obj;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600253
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600254 obj = object_resolve_path(path, NULL);
255 if (!obj) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100256 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
Markus Armbruster75158eb2015-03-16 08:57:47 +0100257 "Device '%s' not found", path);
Markus Armbruster485febc2015-03-13 17:25:50 +0100258 return;
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600259 }
260
Markus Armbruster485febc2015-03-13 17:25:50 +0100261 *ret = object_property_get_qobject(obj, property, errp);
Anthony Liguorieb6e8ea2011-12-12 14:29:35 -0600262}
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200263
264void qmp_set_password(const char *protocol, const char *password,
265 bool has_connected, const char *connected, Error **errp)
266{
267 int disconnect_if_connected = 0;
268 int fail_if_connected = 0;
269 int rc;
270
271 if (has_connected) {
272 if (strcmp(connected, "fail") == 0) {
273 fail_if_connected = 1;
274 } else if (strcmp(connected, "disconnect") == 0) {
275 disconnect_if_connected = 1;
276 } else if (strcmp(connected, "keep") == 0) {
277 /* nothing */
278 } else {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100279 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200280 return;
281 }
282 }
283
284 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100285 if (!qemu_using_spice(errp)) {
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200286 return;
287 }
288 rc = qemu_spice_set_passwd(password, fail_if_connected,
289 disconnect_if_connected);
290 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100291 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200292 }
293 return;
294 }
295
296 if (strcmp(protocol, "vnc") == 0) {
297 if (fail_if_connected || disconnect_if_connected) {
298 /* vnc supports "connected=keep" only */
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100299 error_setg(errp, QERR_INVALID_PARAMETER, "connected");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200300 return;
301 }
302 /* Note that setting an empty password will not disable login through
303 * this interface. */
304 rc = vnc_display_password(NULL, password);
305 if (rc < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100306 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200307 }
308 return;
309 }
310
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100311 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulinofbf796f2011-12-07 11:17:51 -0200312}
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200313
314void qmp_expire_password(const char *protocol, const char *whenstr,
315 Error **errp)
316{
317 time_t when;
318 int rc;
319
320 if (strcmp(whenstr, "now") == 0) {
321 when = 0;
322 } else if (strcmp(whenstr, "never") == 0) {
323 when = TIME_MAX;
324 } else if (whenstr[0] == '+') {
325 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
326 } else {
327 when = strtoull(whenstr, NULL, 10);
328 }
329
330 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100331 if (!qemu_using_spice(errp)) {
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200332 return;
333 }
334 rc = qemu_spice_set_pw_expire(when);
335 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100336 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200337 }
338 return;
339 }
340
341 if (strcmp(protocol, "vnc") == 0) {
342 rc = vnc_display_pw_expire(NULL, when);
343 if (rc != 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100344 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200345 }
346 return;
347 }
348
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100349 error_setg(errp, QERR_INVALID_PARAMETER, "protocol");
Luiz Capitulino9ad53722011-12-07 11:47:57 -0200350}
Luiz Capitulino270b2432011-12-08 11:45:55 -0200351
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200352#ifdef CONFIG_VNC
Luiz Capitulino270b2432011-12-08 11:45:55 -0200353void qmp_change_vnc_password(const char *password, Error **errp)
354{
355 if (vnc_display_password(NULL, password) < 0) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100356 error_setg(errp, QERR_SET_PASSWD_FAILED);
Luiz Capitulino270b2432011-12-08 11:45:55 -0200357 }
358}
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200359
Paolo Bonzini007fcd3e2012-10-18 09:01:01 +0200360static void qmp_change_vnc_listen(const char *target, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200361{
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200362 QemuOptsList *olist = qemu_find_opts("vnc");
363 QemuOpts *opts;
364
365 if (strstr(target, "id=")) {
366 error_setg(errp, "id not supported");
367 return;
368 }
369
370 opts = qemu_opts_find(olist, "default");
371 if (opts) {
372 qemu_opts_del(opts);
373 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100374 opts = vnc_parse(target, errp);
Gongleif7801c52015-02-05 17:43:35 +0800375 if (!opts) {
376 return;
377 }
378
Gerd Hoffmann4db14622014-09-16 12:33:03 +0200379 vnc_display_open("default", errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200380}
381
382static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
383 Error **errp)
384{
385 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
386 if (!has_arg) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100387 error_setg(errp, QERR_MISSING_PARAMETER, "password");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200388 } else {
389 qmp_change_vnc_password(arg, errp);
390 }
391 } else {
392 qmp_change_vnc_listen(target, errp);
393 }
394}
395#else
396void qmp_change_vnc_password(const char *password, Error **errp)
397{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100398 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200399}
400static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
401 Error **errp)
402{
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100403 error_setg(errp, QERR_FEATURE_DISABLED, "vnc");
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200404}
405#endif /* !CONFIG_VNC */
406
407void qmp_change(const char *device, const char *target,
Markus Armbruster7daecb32014-05-02 13:26:31 +0200408 bool has_arg, const char *arg, Error **errp)
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200409{
410 if (strcmp(device, "vnc") == 0) {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200411 qmp_change_vnc(target, has_arg, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200412 } else {
Markus Armbruster7daecb32014-05-02 13:26:31 +0200413 qmp_change_blockdev(device, target, arg, errp);
Luiz Capitulino333a96e2011-12-08 11:13:50 -0200414 }
415}
Anthony Liguori5eeee3f2011-12-22 14:40:54 -0600416
417static void qom_list_types_tramp(ObjectClass *klass, void *data)
418{
419 ObjectTypeInfoList *e, **pret = data;
420 ObjectTypeInfo *info;
421
422 info = g_malloc0(sizeof(*info));
423 info->name = g_strdup(object_class_get_name(klass));
424
425 e = g_malloc0(sizeof(*e));
426 e->value = info;
427 e->next = *pret;
428 *pret = e;
429}
430
431ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
432 const char *implements,
433 bool has_abstract,
434 bool abstract,
435 Error **errp)
436{
437 ObjectTypeInfoList *ret = NULL;
438
439 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
440
441 return ret;
442}
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500443
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200444/* Return a DevicePropertyInfo for a qdev property.
445 *
446 * If a qdev property with the given name does not exist, use the given default
447 * type. If the qdev property info should not be shown, return NULL.
448 *
449 * The caller must free the return value.
450 */
451static DevicePropertyInfo *make_device_property_info(ObjectClass *klass,
452 const char *name,
Gonglei07d09c52014-10-07 14:33:23 +0800453 const char *default_type,
454 const char *description)
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200455{
456 DevicePropertyInfo *info;
457 Property *prop;
458
459 do {
460 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
461 if (strcmp(name, prop->name) != 0) {
462 continue;
463 }
464
465 /*
466 * TODO Properties without a parser are just for dirty hacks.
467 * qdev_prop_ptr is the only such PropertyInfo. It's marked
468 * for removal. This conditional should be removed along with
469 * it.
470 */
471 if (!prop->info->set) {
472 return NULL; /* no way to set it, don't show */
473 }
474
475 info = g_malloc0(sizeof(*info));
476 info->name = g_strdup(prop->name);
Gonglei07d09c52014-10-07 14:33:23 +0800477 info->type = g_strdup(prop->info->name);
478 info->has_description = !!prop->info->description;
479 info->description = g_strdup(prop->info->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200480 return info;
481 }
482 klass = object_class_get_parent(klass);
483 } while (klass != object_class_by_name(TYPE_DEVICE));
484
485 /* Not a qdev property, use the default type */
486 info = g_malloc0(sizeof(*info));
487 info->name = g_strdup(name);
488 info->type = g_strdup(default_type);
Gonglei07d09c52014-10-07 14:33:23 +0800489 info->has_description = !!description;
490 info->description = g_strdup(description);
491
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200492 return info;
493}
494
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500495DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
496 Error **errp)
497{
498 ObjectClass *klass;
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200499 Object *obj;
500 ObjectProperty *prop;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500501 DevicePropertyInfoList *prop_list = NULL;
502
503 klass = object_class_by_name(typename);
504 if (klass == NULL) {
Markus Armbruster75158eb2015-03-16 08:57:47 +0100505 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
506 "Device '%s' not found", typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500507 return NULL;
508 }
509
510 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
511 if (klass == NULL) {
Markus Armbrusterc6bd8c72015-03-17 11:54:50 +0100512 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "name", TYPE_DEVICE);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500513 return NULL;
514 }
515
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200516 obj = object_new(typename);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500517
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200518 QTAILQ_FOREACH(prop, &obj->properties, node) {
519 DevicePropertyInfo *info;
520 DevicePropertyInfoList *entry;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500521
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200522 /* Skip Object and DeviceState properties */
523 if (strcmp(prop->name, "type") == 0 ||
524 strcmp(prop->name, "realized") == 0 ||
525 strcmp(prop->name, "hotpluggable") == 0 ||
Stefan Hajnoczi4115dd62014-07-09 14:01:31 +0200526 strcmp(prop->name, "hotplugged") == 0 ||
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200527 strcmp(prop->name, "parent_bus") == 0) {
528 continue;
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500529 }
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200530
531 /* Skip legacy properties since they are just string versions of
532 * properties that we already list.
533 */
534 if (strstart(prop->name, "legacy-", NULL)) {
535 continue;
536 }
537
Gonglei07d09c52014-10-07 14:33:23 +0800538 info = make_device_property_info(klass, prop->name, prop->type,
539 prop->description);
Stefan Hajnoczif4eb32b2014-05-20 14:29:01 +0200540 if (!info) {
541 continue;
542 }
543
544 entry = g_malloc0(sizeof(*entry));
545 entry->value = info;
546 entry->next = prop_list;
547 prop_list = entry;
548 }
549
550 object_unref(obj);
Anthony Liguori1daa31b2012-08-10 11:04:09 -0500551
552 return prop_list;
553}
Anthony Liguorie4e31c62012-08-10 11:04:13 -0500554
Anthony Liguori76b64a72012-08-14 22:17:36 -0500555CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
556{
557 return arch_query_cpu_definitions(errp);
558}
559
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300560void qmp_add_client(const char *protocol, const char *fdname,
561 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
562 Error **errp)
563{
564 CharDriverState *s;
565 int fd;
566
567 fd = monitor_get_fd(cur_mon, fdname, errp);
568 if (fd < 0) {
569 return;
570 }
571
572 if (strcmp(protocol, "spice") == 0) {
Markus Armbrusterb25d81b2015-01-13 17:07:15 +0100573 if (!qemu_using_spice(errp)) {
Luiz Capitulinob224e5e2012-09-13 16:52:20 -0300574 close(fd);
575 return;
576 }
577 skipauth = has_skipauth ? skipauth : false;
578 tls = has_tls ? tls : false;
579 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
580 error_setg(errp, "spice failed to add client");
581 close(fd);
582 }
583 return;
584#ifdef CONFIG_VNC
585 } else if (strcmp(protocol, "vnc") == 0) {
586 skipauth = has_skipauth ? skipauth : false;
587 vnc_display_add_client(NULL, fd, skipauth);
588 return;
589#endif
590 } else if ((s = qemu_chr_find(protocol)) != NULL) {
591 if (qemu_chr_add_client(s, fd) < 0) {
592 error_setg(errp, "failed to add client");
593 close(fd);
594 return;
595 }
596 return;
597 }
598
599 error_setg(errp, "protocol '%s' is invalid", protocol);
600 close(fd);
601}
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100602
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100603void object_add(const char *type, const char *id, const QDict *qdict,
604 Visitor *v, Error **errp)
605{
606 Object *obj;
Eduardo Habkostc3481242014-04-16 14:39:38 -0300607 ObjectClass *klass;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100608 const QDictEntry *e;
609 Error *local_err = NULL;
610
Eduardo Habkostc3481242014-04-16 14:39:38 -0300611 klass = object_class_by_name(type);
612 if (!klass) {
Paolo Bonzinid1169462014-05-14 17:43:13 +0800613 error_setg(errp, "invalid object type: %s", type);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100614 return;
615 }
616
Eduardo Habkostc3481242014-04-16 14:39:38 -0300617 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
618 error_setg(errp, "object type '%s' isn't supported by object-add",
619 type);
620 return;
621 }
622
623 if (object_class_is_abstract(klass)) {
624 error_setg(errp, "object type '%s' is abstract", type);
625 return;
626 }
627
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100628 obj = object_new(type);
629 if (qdict) {
630 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
631 object_property_set(obj, v, e->key, &local_err);
632 if (local_err) {
Igor Mammedov69252c02014-01-16 17:34:36 +0100633 goto out;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100634 }
635 }
636 }
637
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100638 object_property_add_child(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200639 id, obj, &local_err);
Igor Mammedov269e09f2014-01-16 17:34:38 +0100640 if (local_err) {
641 goto out;
642 }
643
Igor Mammedova790f4e2014-06-02 15:24:59 +0200644 user_creatable_complete(obj, &local_err);
645 if (local_err) {
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100646 object_property_del(object_get_objects_root(),
Igor Mammedova790f4e2014-06-02 15:24:59 +0200647 id, &error_abort);
648 goto out;
649 }
Igor Mammedov69252c02014-01-16 17:34:36 +0100650out:
651 if (local_err) {
652 error_propagate(errp, local_err);
653 }
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100654 object_unref(obj);
655}
656
Markus Armbruster485febc2015-03-13 17:25:50 +0100657void qmp_object_add(QDict *qdict, QObject **ret, Error **errp)
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100658{
659 const char *type = qdict_get_str(qdict, "qom-type");
660 const char *id = qdict_get_str(qdict, "id");
661 QObject *props = qdict_get(qdict, "props");
662 const QDict *pdict = NULL;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100663 QmpInputVisitor *qiv;
664
665 if (props) {
666 pdict = qobject_to_qdict(props);
667 if (!pdict) {
Markus Armbruster485febc2015-03-13 17:25:50 +0100668 error_setg(errp, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
669 return;
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100670 }
671 }
672
673 qiv = qmp_input_visitor_new(props);
Markus Armbruster485febc2015-03-13 17:25:50 +0100674 object_add(type, id, pdict, qmp_input_get_visitor(qiv), errp);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100675 qmp_input_visitor_cleanup(qiv);
Paolo Bonzinicff8b2c2013-12-20 23:21:10 +0100676}
677
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100678void qmp_object_del(const char *id, Error **errp)
679{
680 Object *container;
681 Object *obj;
682
Daniel P. Berrangebc2256c2015-05-13 17:14:05 +0100683 container = object_get_objects_root();
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100684 obj = object_resolve_path_component(container, id);
685 if (!obj) {
686 error_setg(errp, "object id not found");
687 return;
688 }
Lin Mad6edb152015-03-30 16:36:28 +0800689
690 if (!user_creatable_can_be_deleted(USER_CREATABLE(obj), errp)) {
691 error_setg(errp, "%s is in use, can not be deleted", id);
692 return;
693 }
Paolo Bonziniab2d0532013-12-20 23:21:09 +0100694 object_unparent(obj);
695}
Igor Mammedov6f2e2732014-06-16 19:12:25 +0200696
697MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp)
698{
699 MemoryDeviceInfoList *head = NULL;
700 MemoryDeviceInfoList **prev = &head;
701
702 qmp_pc_dimm_device_list(qdev_get_machine(), &prev);
703
704 return head;
705}
Igor Mammedov02419bc2014-06-16 19:12:28 +0200706
707ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp)
708{
709 bool ambig;
710 ACPIOSTInfoList *head = NULL;
711 ACPIOSTInfoList **prev = &head;
712 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig);
713
714 if (obj) {
715 AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj);
716 AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj);
717
718 adevc->ospm_status(adev, &prev);
719 } else {
720 error_setg(errp, "command is not supported, missing ACPI device");
721 }
722
723 return head;
724}