]> git.proxmox.com Git - mirror_qemu.git/blame - qmp.c
numa: handle mmaped memory allocation failure correctly
[mirror_qemu.git] / qmp.c
CommitLineData
48a32bed
AL
1/*
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 *
6b620ca3
PB
12 * Contributions after 2012-01-13 are licensed under the terms of the
13 * GNU GPL, version 2 or (at your option) any later version.
48a32bed
AL
14 */
15
16#include "qemu-common.h"
9c17d615 17#include "sysemu/sysemu.h"
48a32bed 18#include "qmp-commands.h"
dccfcd0e 19#include "sysemu/char.h"
fbf796fd
LC
20#include "ui/qemu-spice.h"
21#include "ui/vnc.h"
9c17d615
PB
22#include "sysemu/kvm.h"
23#include "sysemu/arch_init.h"
b4b12c62 24#include "hw/qdev.h"
9c17d615 25#include "sysemu/blockdev.h"
14cccb61 26#include "qom/qom-qobject.h"
cff8b2c6
PB
27#include "qapi/qmp/qobject.h"
28#include "qapi/qmp-input-visitor.h"
69ca3ea5 29#include "hw/boards.h"
269e09f3 30#include "qom/object_interfaces.h"
48a32bed
AL
31
32NameInfo *qmp_query_name(Error **errp)
33{
34 NameInfo *info = g_malloc0(sizeof(*info));
35
36 if (qemu_name) {
37 info->has_name = true;
38 info->name = g_strdup(qemu_name);
39 }
40
41 return info;
42}
b9c15f16 43
7daecb30 44VersionInfo *qmp_query_version(Error **errp)
b9c15f16
LC
45{
46 VersionInfo *info = g_malloc0(sizeof(*info));
47 const char *version = QEMU_VERSION;
48 char *tmp;
49
50 info->qemu.major = strtol(version, &tmp, 10);
51 tmp++;
52 info->qemu.minor = strtol(tmp, &tmp, 10);
53 tmp++;
54 info->qemu.micro = strtol(tmp, &tmp, 10);
55 info->package = g_strdup(QEMU_PKGVERSION);
56
57 return info;
58}
292a2602
LC
59
60KvmInfo *qmp_query_kvm(Error **errp)
61{
62 KvmInfo *info = g_malloc0(sizeof(*info));
63
64 info->enabled = kvm_enabled();
65 info->present = kvm_available();
66
67 return info;
68}
69
efab767e
LC
70UuidInfo *qmp_query_uuid(Error **errp)
71{
72 UuidInfo *info = g_malloc0(sizeof(*info));
73 char uuid[64];
74
75 snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
76 qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
77 qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
78 qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
79 qemu_uuid[14], qemu_uuid[15]);
80
81 info->UUID = g_strdup(uuid);
82 return info;
83}
84
7daecb30 85void qmp_quit(Error **errp)
7a7f325e
LC
86{
87 no_shutdown = 0;
88 qemu_system_shutdown_request();
89}
90
5f158f21
LC
91void qmp_stop(Error **errp)
92{
1e998146
PB
93 if (runstate_check(RUN_STATE_INMIGRATE)) {
94 autostart = 0;
95 } else {
96 vm_stop(RUN_STATE_PAUSED);
97 }
5f158f21
LC
98}
99
38d22653
LC
100void qmp_system_reset(Error **errp)
101{
102 qemu_system_reset_request();
103}
5bc465e4
LC
104
105void qmp_system_powerdown(Error **erp)
106{
107 qemu_system_powerdown_request();
108}
755f1968
LC
109
110void qmp_cpu(int64_t index, Error **errp)
111{
112 /* Just do nothing */
113}
2b54aa87 114
69ca3ea5
IM
115void qmp_cpu_add(int64_t id, Error **errp)
116{
0056ae24
MA
117 MachineClass *mc;
118
119 mc = MACHINE_GET_CLASS(current_machine);
958db90c
MA
120 if (mc->hot_add_cpu) {
121 mc->hot_add_cpu(id, errp);
69ca3ea5
IM
122 } else {
123 error_setg(errp, "Not supported");
124 }
125}
126
2b54aa87
LC
127#ifndef CONFIG_VNC
128/* If VNC support is enabled, the "true" query-vnc command is
129 defined in the VNC subsystem */
130VncInfo *qmp_query_vnc(Error **errp)
131{
132 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
133 return NULL;
134};
135#endif
d1f29646
LC
136
137#ifndef CONFIG_SPICE
138/* If SPICE support is enabled, the "true" query-spice command is
139 defined in the SPICE subsystem. Also note that we use a small
140 trick to maintain query-spice's original behavior, which is not
141 to be available in the namespace if SPICE is not compiled in */
142SpiceInfo *qmp_query_spice(Error **errp)
143{
144 error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
145 return NULL;
146};
147#endif
e42e818b 148
e42e818b
LC
149void qmp_cont(Error **errp)
150{
ab31979a 151 BlockDriverState *bs;
e42e818b 152
ede085b3 153 if (runstate_needs_reset()) {
f231b88d 154 error_setg(errp, "Resetting the Virtual Machine is required");
e42e818b 155 return;
ad02b96a
LC
156 } else if (runstate_check(RUN_STATE_SUSPENDED)) {
157 return;
e42e818b
LC
158 }
159
ab31979a
MA
160 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
161 bdrv_iostatus_reset(bs);
162 }
163 for (bs = bdrv_next(NULL); bs; bs = bdrv_next(bs)) {
164 if (bdrv_key_required(bs)) {
165 error_set(errp, QERR_DEVICE_ENCRYPTED,
166 bdrv_get_device_name(bs),
167 bdrv_get_encrypted_filename(bs));
168 return;
169 }
e42e818b
LC
170 }
171
1e998146
PB
172 if (runstate_check(RUN_STATE_INMIGRATE)) {
173 autostart = 1;
174 } else {
175 vm_start();
176 }
e42e818b 177}
b4b12c62 178
9b9df25a
GH
179void qmp_system_wakeup(Error **errp)
180{
181 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
182}
183
57c9fafe 184ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
b4b12c62 185{
57c9fafe 186 Object *obj;
b4b12c62 187 bool ambiguous = false;
57c9fafe
AL
188 ObjectPropertyInfoList *props = NULL;
189 ObjectProperty *prop;
b4b12c62 190
57c9fafe
AL
191 obj = object_resolve_path(path, &ambiguous);
192 if (obj == NULL) {
79772087
MT
193 if (ambiguous) {
194 error_setg(errp, "Path '%s' is ambiguous", path);
195 } else {
196 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
197 }
b4b12c62
AL
198 return NULL;
199 }
200
57c9fafe
AL
201 QTAILQ_FOREACH(prop, &obj->properties, node) {
202 ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
b4b12c62 203
57c9fafe 204 entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
b4b12c62
AL
205 entry->next = props;
206 props = entry;
207
208 entry->value->name = g_strdup(prop->name);
209 entry->value->type = g_strdup(prop->type);
210 }
211
212 return props;
213}
eb6e8ea5
AL
214
215/* FIXME: teach qapi about how to pass through Visitors */
216int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
217{
218 const char *path = qdict_get_str(qdict, "path");
219 const char *property = qdict_get_str(qdict, "property");
220 QObject *value = qdict_get(qdict, "value");
221 Error *local_err = NULL;
57c9fafe 222 Object *obj;
eb6e8ea5 223
57c9fafe
AL
224 obj = object_resolve_path(path, NULL);
225 if (!obj) {
eb6e8ea5
AL
226 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
227 goto out;
228 }
229
9f5f1350 230 object_property_set_qobject(obj, value, property, &local_err);
eb6e8ea5
AL
231
232out:
233 if (local_err) {
234 qerror_report_err(local_err);
235 error_free(local_err);
236 return -1;
237 }
238
239 return 0;
240}
241
242int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
243{
244 const char *path = qdict_get_str(qdict, "path");
245 const char *property = qdict_get_str(qdict, "property");
246 Error *local_err = NULL;
57c9fafe 247 Object *obj;
eb6e8ea5 248
57c9fafe
AL
249 obj = object_resolve_path(path, NULL);
250 if (!obj) {
eb6e8ea5
AL
251 error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
252 goto out;
253 }
254
9f5f1350 255 *ret = object_property_get_qobject(obj, property, &local_err);
eb6e8ea5
AL
256
257out:
258 if (local_err) {
259 qerror_report_err(local_err);
260 error_free(local_err);
261 return -1;
262 }
263
264 return 0;
265}
fbf796fd
LC
266
267void qmp_set_password(const char *protocol, const char *password,
268 bool has_connected, const char *connected, Error **errp)
269{
270 int disconnect_if_connected = 0;
271 int fail_if_connected = 0;
272 int rc;
273
274 if (has_connected) {
275 if (strcmp(connected, "fail") == 0) {
276 fail_if_connected = 1;
277 } else if (strcmp(connected, "disconnect") == 0) {
278 disconnect_if_connected = 1;
279 } else if (strcmp(connected, "keep") == 0) {
280 /* nothing */
281 } else {
282 error_set(errp, QERR_INVALID_PARAMETER, "connected");
283 return;
284 }
285 }
286
287 if (strcmp(protocol, "spice") == 0) {
288 if (!using_spice) {
289 /* correct one? spice isn't a device ,,, */
290 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
291 return;
292 }
293 rc = qemu_spice_set_passwd(password, fail_if_connected,
294 disconnect_if_connected);
295 if (rc != 0) {
296 error_set(errp, QERR_SET_PASSWD_FAILED);
297 }
298 return;
299 }
300
301 if (strcmp(protocol, "vnc") == 0) {
302 if (fail_if_connected || disconnect_if_connected) {
303 /* vnc supports "connected=keep" only */
304 error_set(errp, QERR_INVALID_PARAMETER, "connected");
305 return;
306 }
307 /* Note that setting an empty password will not disable login through
308 * this interface. */
309 rc = vnc_display_password(NULL, password);
310 if (rc < 0) {
311 error_set(errp, QERR_SET_PASSWD_FAILED);
312 }
313 return;
314 }
315
316 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
317}
9ad5372d
LC
318
319void qmp_expire_password(const char *protocol, const char *whenstr,
320 Error **errp)
321{
322 time_t when;
323 int rc;
324
325 if (strcmp(whenstr, "now") == 0) {
326 when = 0;
327 } else if (strcmp(whenstr, "never") == 0) {
328 when = TIME_MAX;
329 } else if (whenstr[0] == '+') {
330 when = time(NULL) + strtoull(whenstr+1, NULL, 10);
331 } else {
332 when = strtoull(whenstr, NULL, 10);
333 }
334
335 if (strcmp(protocol, "spice") == 0) {
336 if (!using_spice) {
337 /* correct one? spice isn't a device ,,, */
338 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
339 return;
340 }
341 rc = qemu_spice_set_pw_expire(when);
342 if (rc != 0) {
343 error_set(errp, QERR_SET_PASSWD_FAILED);
344 }
345 return;
346 }
347
348 if (strcmp(protocol, "vnc") == 0) {
349 rc = vnc_display_pw_expire(NULL, when);
350 if (rc != 0) {
351 error_set(errp, QERR_SET_PASSWD_FAILED);
352 }
353 return;
354 }
355
356 error_set(errp, QERR_INVALID_PARAMETER, "protocol");
357}
270b243f 358
333a96ec 359#ifdef CONFIG_VNC
270b243f
LC
360void qmp_change_vnc_password(const char *password, Error **errp)
361{
362 if (vnc_display_password(NULL, password) < 0) {
363 error_set(errp, QERR_SET_PASSWD_FAILED);
364 }
365}
333a96ec 366
007fcd3e 367static void qmp_change_vnc_listen(const char *target, Error **errp)
333a96ec 368{
007fcd3e 369 vnc_display_open(NULL, target, errp);
333a96ec
LC
370}
371
372static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
373 Error **errp)
374{
375 if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) {
376 if (!has_arg) {
377 error_set(errp, QERR_MISSING_PARAMETER, "password");
378 } else {
379 qmp_change_vnc_password(arg, errp);
380 }
381 } else {
382 qmp_change_vnc_listen(target, errp);
383 }
384}
385#else
386void qmp_change_vnc_password(const char *password, Error **errp)
387{
388 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
389}
390static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
391 Error **errp)
392{
393 error_set(errp, QERR_FEATURE_DISABLED, "vnc");
394}
395#endif /* !CONFIG_VNC */
396
397void qmp_change(const char *device, const char *target,
7daecb30 398 bool has_arg, const char *arg, Error **errp)
333a96ec
LC
399{
400 if (strcmp(device, "vnc") == 0) {
7daecb30 401 qmp_change_vnc(target, has_arg, arg, errp);
333a96ec 402 } else {
7daecb30 403 qmp_change_blockdev(device, target, arg, errp);
333a96ec
LC
404 }
405}
5eeee3fa
AL
406
407static void qom_list_types_tramp(ObjectClass *klass, void *data)
408{
409 ObjectTypeInfoList *e, **pret = data;
410 ObjectTypeInfo *info;
411
412 info = g_malloc0(sizeof(*info));
413 info->name = g_strdup(object_class_get_name(klass));
414
415 e = g_malloc0(sizeof(*e));
416 e->value = info;
417 e->next = *pret;
418 *pret = e;
419}
420
421ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
422 const char *implements,
423 bool has_abstract,
424 bool abstract,
425 Error **errp)
426{
427 ObjectTypeInfoList *ret = NULL;
428
429 object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
430
431 return ret;
432}
1daa31b9
AL
433
434DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
435 Error **errp)
436{
437 ObjectClass *klass;
438 Property *prop;
439 DevicePropertyInfoList *prop_list = NULL;
440
441 klass = object_class_by_name(typename);
442 if (klass == NULL) {
443 error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
444 return NULL;
445 }
446
447 klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
448 if (klass == NULL) {
449 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
450 "name", TYPE_DEVICE);
451 return NULL;
452 }
453
454 do {
455 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
456 DevicePropertyInfoList *entry;
457 DevicePropertyInfo *info;
458
459 /*
460 * TODO Properties without a parser are just for dirty hacks.
461 * qdev_prop_ptr is the only such PropertyInfo. It's marked
462 * for removal. This conditional should be removed along with
463 * it.
464 */
465 if (!prop->info->set) {
466 continue; /* no way to set it, don't show */
467 }
468
469 info = g_malloc0(sizeof(*info));
470 info->name = g_strdup(prop->name);
471 info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
472
473 entry = g_malloc0(sizeof(*entry));
474 entry->value = info;
475 entry->next = prop_list;
476 prop_list = entry;
477 }
478 klass = object_class_get_parent(klass);
479 } while (klass != object_class_by_name(TYPE_DEVICE));
480
481 return prop_list;
482}
e4e31c63 483
76b64a7a
AL
484CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
485{
486 return arch_query_cpu_definitions(errp);
487}
488
b224e5e2
LC
489void qmp_add_client(const char *protocol, const char *fdname,
490 bool has_skipauth, bool skipauth, bool has_tls, bool tls,
491 Error **errp)
492{
493 CharDriverState *s;
494 int fd;
495
496 fd = monitor_get_fd(cur_mon, fdname, errp);
497 if (fd < 0) {
498 return;
499 }
500
501 if (strcmp(protocol, "spice") == 0) {
502 if (!using_spice) {
503 error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
504 close(fd);
505 return;
506 }
507 skipauth = has_skipauth ? skipauth : false;
508 tls = has_tls ? tls : false;
509 if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
510 error_setg(errp, "spice failed to add client");
511 close(fd);
512 }
513 return;
514#ifdef CONFIG_VNC
515 } else if (strcmp(protocol, "vnc") == 0) {
516 skipauth = has_skipauth ? skipauth : false;
517 vnc_display_add_client(NULL, fd, skipauth);
518 return;
519#endif
520 } else if ((s = qemu_chr_find(protocol)) != NULL) {
521 if (qemu_chr_add_client(s, fd) < 0) {
522 error_setg(errp, "failed to add client");
523 close(fd);
524 return;
525 }
526 return;
527 }
528
529 error_setg(errp, "protocol '%s' is invalid", protocol);
530 close(fd);
531}
ab2d0531 532
cff8b2c6
PB
533void object_add(const char *type, const char *id, const QDict *qdict,
534 Visitor *v, Error **errp)
535{
536 Object *obj;
c3481247 537 ObjectClass *klass;
cff8b2c6
PB
538 const QDictEntry *e;
539 Error *local_err = NULL;
540
c3481247
EH
541 klass = object_class_by_name(type);
542 if (!klass) {
d1169464 543 error_setg(errp, "invalid object type: %s", type);
cff8b2c6
PB
544 return;
545 }
546
c3481247
EH
547 if (!object_class_dynamic_cast(klass, TYPE_USER_CREATABLE)) {
548 error_setg(errp, "object type '%s' isn't supported by object-add",
549 type);
550 return;
551 }
552
553 if (object_class_is_abstract(klass)) {
554 error_setg(errp, "object type '%s' is abstract", type);
555 return;
556 }
557
cff8b2c6
PB
558 obj = object_new(type);
559 if (qdict) {
560 for (e = qdict_first(qdict); e; e = qdict_next(qdict, e)) {
561 object_property_set(obj, v, e->key, &local_err);
562 if (local_err) {
69252c04 563 goto out;
cff8b2c6
PB
564 }
565 }
566 }
567
a790f4ec
IM
568 object_property_add_child(container_get(object_get_root(), "/objects"),
569 id, obj, &local_err);
269e09f3
IM
570 if (local_err) {
571 goto out;
572 }
573
a790f4ec
IM
574 user_creatable_complete(obj, &local_err);
575 if (local_err) {
576 object_property_del(container_get(object_get_root(), "/objects"),
577 id, &error_abort);
578 goto out;
579 }
69252c04
IM
580out:
581 if (local_err) {
582 error_propagate(errp, local_err);
583 }
cff8b2c6
PB
584 object_unref(obj);
585}
586
587int qmp_object_add(Monitor *mon, const QDict *qdict, QObject **ret)
588{
589 const char *type = qdict_get_str(qdict, "qom-type");
590 const char *id = qdict_get_str(qdict, "id");
591 QObject *props = qdict_get(qdict, "props");
592 const QDict *pdict = NULL;
593 Error *local_err = NULL;
594 QmpInputVisitor *qiv;
595
596 if (props) {
597 pdict = qobject_to_qdict(props);
598 if (!pdict) {
599 error_set(&local_err, QERR_INVALID_PARAMETER_TYPE, "props", "dict");
600 goto out;
601 }
602 }
603
604 qiv = qmp_input_visitor_new(props);
605 object_add(type, id, pdict, qmp_input_get_visitor(qiv), &local_err);
606 qmp_input_visitor_cleanup(qiv);
607
608out:
609 if (local_err) {
610 qerror_report_err(local_err);
611 error_free(local_err);
612 return -1;
613 }
614
615 return 0;
616}
617
ab2d0531
PB
618void qmp_object_del(const char *id, Error **errp)
619{
620 Object *container;
621 Object *obj;
622
623 container = container_get(object_get_root(), "/objects");
624 obj = object_resolve_path_component(container, id);
625 if (!obj) {
626 error_setg(errp, "object id not found");
627 return;
628 }
629 object_unparent(obj);
630}