]>
Commit | Line | Data |
---|---|---|
48a32bed | 1 | /* |
f1b3ccfa | 2 | * QEMU Management Protocol commands |
48a32bed AL |
3 | * |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | ||
d38ea87a | 16 | #include "qemu/osdep.h" |
a8d25326 | 17 | #include "qemu-common.h" |
67a1de0d | 18 | #include "qemu-version.h" |
f348b6d1 | 19 | #include "qemu/cutils.h" |
922a01a0 | 20 | #include "qemu/option.h" |
a0b1a66e | 21 | #include "monitor/monitor.h" |
9c17d615 | 22 | #include "sysemu/sysemu.h" |
213dcb06 | 23 | #include "qemu/config-file.h" |
cea25275 | 24 | #include "qemu/uuid.h" |
8228e353 | 25 | #include "chardev/char.h" |
fbf796fd LC |
26 | #include "ui/qemu-spice.h" |
27 | #include "ui/vnc.h" | |
9c17d615 | 28 | #include "sysemu/kvm.h" |
54d31236 | 29 | #include "sysemu/runstate.h" |
9c17d615 | 30 | #include "sysemu/arch_init.h" |
9c17d615 | 31 | #include "sysemu/blockdev.h" |
373340b2 | 32 | #include "sysemu/block-backend.h" |
e688df6b | 33 | #include "qapi/error.h" |
112ed241 | 34 | #include "qapi/qapi-commands-block-core.h" |
8ac25c84 | 35 | #include "qapi/qapi-commands-machine.h" |
112ed241 MA |
36 | #include "qapi/qapi-commands-misc.h" |
37 | #include "qapi/qapi-commands-ui.h" | |
cc7a8ea7 | 38 | #include "qapi/qmp/qerror.h" |
2cc0e2e8 | 39 | #include "hw/mem/memory-device.h" |
02419bcb | 40 | #include "hw/acpi/acpi_dev_interface.h" |
48a32bed AL |
41 | |
42 | NameInfo *qmp_query_name(Error **errp) | |
43 | { | |
44 | NameInfo *info = g_malloc0(sizeof(*info)); | |
45 | ||
46 | if (qemu_name) { | |
47 | info->has_name = true; | |
48 | info->name = g_strdup(qemu_name); | |
49 | } | |
50 | ||
51 | return info; | |
52 | } | |
b9c15f16 | 53 | |
7daecb30 | 54 | VersionInfo *qmp_query_version(Error **errp) |
b9c15f16 | 55 | { |
4752cdbb | 56 | VersionInfo *info = g_new0(VersionInfo, 1); |
b9c15f16 | 57 | |
4752cdbb | 58 | info->qemu = g_new0(VersionTriple, 1); |
3688d8c7 MAL |
59 | info->qemu->major = QEMU_VERSION_MAJOR; |
60 | info->qemu->minor = QEMU_VERSION_MINOR; | |
61 | info->qemu->micro = QEMU_VERSION_MICRO; | |
b9c15f16 LC |
62 | info->package = g_strdup(QEMU_PKGVERSION); |
63 | ||
64 | return info; | |
65 | } | |
292a2602 LC |
66 | |
67 | KvmInfo *qmp_query_kvm(Error **errp) | |
68 | { | |
69 | KvmInfo *info = g_malloc0(sizeof(*info)); | |
70 | ||
71 | info->enabled = kvm_enabled(); | |
72 | info->present = kvm_available(); | |
73 | ||
74 | return info; | |
75 | } | |
76 | ||
efab767e LC |
77 | UuidInfo *qmp_query_uuid(Error **errp) |
78 | { | |
79 | UuidInfo *info = g_malloc0(sizeof(*info)); | |
efab767e | 80 | |
9c5ce8db | 81 | info->UUID = qemu_uuid_unparse_strdup(&qemu_uuid); |
efab767e LC |
82 | return info; |
83 | } | |
84 | ||
7daecb30 | 85 | void qmp_quit(Error **errp) |
7a7f325e LC |
86 | { |
87 | no_shutdown = 0; | |
92548938 | 88 | qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT); |
7a7f325e LC |
89 | } |
90 | ||
5f158f21 LC |
91 | void qmp_stop(Error **errp) |
92 | { | |
65d64f36 PX |
93 | /* if there is a dump in background, we should wait until the dump |
94 | * finished */ | |
95 | if (dump_in_progress()) { | |
96 | error_setg(errp, "There is a dump in process, please wait."); | |
97 | return; | |
98 | } | |
99 | ||
1e998146 PB |
100 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
101 | autostart = 0; | |
102 | } else { | |
103 | vm_stop(RUN_STATE_PAUSED); | |
104 | } | |
5f158f21 LC |
105 | } |
106 | ||
38d22653 LC |
107 | void qmp_system_reset(Error **errp) |
108 | { | |
92548938 | 109 | qemu_system_reset_request(SHUTDOWN_CAUSE_HOST_QMP_SYSTEM_RESET); |
38d22653 | 110 | } |
5bc465e4 | 111 | |
ec48595e | 112 | void qmp_system_powerdown(Error **errp) |
5bc465e4 LC |
113 | { |
114 | qemu_system_powerdown_request(); | |
115 | } | |
755f1968 | 116 | |
361ac948 | 117 | void qmp_x_exit_preconfig(Error **errp) |
047f7038 IM |
118 | { |
119 | if (!runstate_check(RUN_STATE_PRECONFIG)) { | |
120 | error_setg(errp, "The command is permitted only in '%s' state", | |
121 | RunState_str(RUN_STATE_PRECONFIG)); | |
122 | return; | |
123 | } | |
124 | qemu_exit_preconfig_request(); | |
125 | } | |
126 | ||
e42e818b LC |
127 | void qmp_cont(Error **errp) |
128 | { | |
373340b2 | 129 | BlockBackend *blk; |
68d00e42 | 130 | BlockJob *job; |
788cf9f8 | 131 | Error *local_err = NULL; |
e42e818b | 132 | |
65d64f36 PX |
133 | /* if there is a dump in background, we should wait until the dump |
134 | * finished */ | |
135 | if (dump_in_progress()) { | |
136 | error_setg(errp, "There is a dump in process, please wait."); | |
137 | return; | |
138 | } | |
139 | ||
ede085b3 | 140 | if (runstate_needs_reset()) { |
f231b88d | 141 | error_setg(errp, "Resetting the Virtual Machine is required"); |
e42e818b | 142 | return; |
ad02b96a LC |
143 | } else if (runstate_check(RUN_STATE_SUSPENDED)) { |
144 | return; | |
9183dd15 VSO |
145 | } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) { |
146 | error_setg(errp, "Migration is not finalized yet"); | |
147 | return; | |
e42e818b LC |
148 | } |
149 | ||
373340b2 HR |
150 | for (blk = blk_next(NULL); blk; blk = blk_next(blk)) { |
151 | blk_iostatus_reset(blk); | |
ab31979a | 152 | } |
7c8eece4 | 153 | |
68d00e42 VSO |
154 | for (job = block_job_next(NULL); job; job = block_job_next(job)) { |
155 | block_job_iostatus_reset(job); | |
156 | } | |
157 | ||
76b1c7fe | 158 | /* Continuing after completed migration. Images have been inactivated to |
ace21a58 KW |
159 | * allow the destination to take control. Need to get control back now. |
160 | * | |
161 | * If there are no inactive block nodes (e.g. because the VM was just | |
162 | * paused rather than completing a migration), bdrv_inactivate_all() simply | |
163 | * doesn't do anything. */ | |
164 | bdrv_invalidate_cache_all(&local_err); | |
165 | if (local_err) { | |
166 | error_propagate(errp, local_err); | |
167 | return; | |
76b1c7fe KW |
168 | } |
169 | ||
1e998146 PB |
170 | if (runstate_check(RUN_STATE_INMIGRATE)) { |
171 | autostart = 1; | |
172 | } else { | |
173 | vm_start(); | |
174 | } | |
e42e818b | 175 | } |
b4b12c62 | 176 | |
9b9df25a GH |
177 | void qmp_system_wakeup(Error **errp) |
178 | { | |
fb064112 DHB |
179 | if (!qemu_wakeup_suspend_enabled()) { |
180 | error_setg(errp, | |
181 | "wake-up from suspend is not supported by this guest"); | |
182 | return; | |
183 | } | |
184 | ||
185 | qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, errp); | |
9b9df25a GH |
186 | } |
187 | ||
fbf796fd LC |
188 | void qmp_set_password(const char *protocol, const char *password, |
189 | bool has_connected, const char *connected, Error **errp) | |
190 | { | |
191 | int disconnect_if_connected = 0; | |
192 | int fail_if_connected = 0; | |
193 | int rc; | |
194 | ||
195 | if (has_connected) { | |
196 | if (strcmp(connected, "fail") == 0) { | |
197 | fail_if_connected = 1; | |
198 | } else if (strcmp(connected, "disconnect") == 0) { | |
199 | disconnect_if_connected = 1; | |
200 | } else if (strcmp(connected, "keep") == 0) { | |
201 | /* nothing */ | |
202 | } else { | |
c6bd8c70 | 203 | error_setg(errp, QERR_INVALID_PARAMETER, "connected"); |
fbf796fd LC |
204 | return; |
205 | } | |
206 | } | |
207 | ||
208 | if (strcmp(protocol, "spice") == 0) { | |
b25d81ba | 209 | if (!qemu_using_spice(errp)) { |
fbf796fd LC |
210 | return; |
211 | } | |
212 | rc = qemu_spice_set_passwd(password, fail_if_connected, | |
213 | disconnect_if_connected); | |
214 | if (rc != 0) { | |
c6bd8c70 | 215 | error_setg(errp, QERR_SET_PASSWD_FAILED); |
fbf796fd LC |
216 | } |
217 | return; | |
218 | } | |
219 | ||
220 | if (strcmp(protocol, "vnc") == 0) { | |
221 | if (fail_if_connected || disconnect_if_connected) { | |
222 | /* vnc supports "connected=keep" only */ | |
c6bd8c70 | 223 | error_setg(errp, QERR_INVALID_PARAMETER, "connected"); |
fbf796fd LC |
224 | return; |
225 | } | |
226 | /* Note that setting an empty password will not disable login through | |
227 | * this interface. */ | |
228 | rc = vnc_display_password(NULL, password); | |
229 | if (rc < 0) { | |
c6bd8c70 | 230 | error_setg(errp, QERR_SET_PASSWD_FAILED); |
fbf796fd LC |
231 | } |
232 | return; | |
233 | } | |
234 | ||
c6bd8c70 | 235 | error_setg(errp, QERR_INVALID_PARAMETER, "protocol"); |
fbf796fd | 236 | } |
9ad5372d LC |
237 | |
238 | void qmp_expire_password(const char *protocol, const char *whenstr, | |
239 | Error **errp) | |
240 | { | |
241 | time_t when; | |
242 | int rc; | |
243 | ||
244 | if (strcmp(whenstr, "now") == 0) { | |
245 | when = 0; | |
246 | } else if (strcmp(whenstr, "never") == 0) { | |
247 | when = TIME_MAX; | |
248 | } else if (whenstr[0] == '+') { | |
249 | when = time(NULL) + strtoull(whenstr+1, NULL, 10); | |
250 | } else { | |
251 | when = strtoull(whenstr, NULL, 10); | |
252 | } | |
253 | ||
254 | if (strcmp(protocol, "spice") == 0) { | |
b25d81ba | 255 | if (!qemu_using_spice(errp)) { |
9ad5372d LC |
256 | return; |
257 | } | |
258 | rc = qemu_spice_set_pw_expire(when); | |
259 | if (rc != 0) { | |
c6bd8c70 | 260 | error_setg(errp, QERR_SET_PASSWD_FAILED); |
9ad5372d LC |
261 | } |
262 | return; | |
263 | } | |
264 | ||
265 | if (strcmp(protocol, "vnc") == 0) { | |
266 | rc = vnc_display_pw_expire(NULL, when); | |
267 | if (rc != 0) { | |
c6bd8c70 | 268 | error_setg(errp, QERR_SET_PASSWD_FAILED); |
9ad5372d LC |
269 | } |
270 | return; | |
271 | } | |
272 | ||
c6bd8c70 | 273 | error_setg(errp, QERR_INVALID_PARAMETER, "protocol"); |
9ad5372d | 274 | } |
270b243f | 275 | |
333a96ec | 276 | #ifdef CONFIG_VNC |
270b243f LC |
277 | void qmp_change_vnc_password(const char *password, Error **errp) |
278 | { | |
279 | if (vnc_display_password(NULL, password) < 0) { | |
c6bd8c70 | 280 | error_setg(errp, QERR_SET_PASSWD_FAILED); |
270b243f LC |
281 | } |
282 | } | |
333a96ec | 283 | |
007fcd3e | 284 | static void qmp_change_vnc_listen(const char *target, Error **errp) |
333a96ec | 285 | { |
4db14629 GH |
286 | QemuOptsList *olist = qemu_find_opts("vnc"); |
287 | QemuOpts *opts; | |
288 | ||
289 | if (strstr(target, "id=")) { | |
290 | error_setg(errp, "id not supported"); | |
291 | return; | |
292 | } | |
293 | ||
294 | opts = qemu_opts_find(olist, "default"); | |
295 | if (opts) { | |
296 | qemu_opts_del(opts); | |
297 | } | |
70b94331 | 298 | opts = vnc_parse(target, errp); |
f7801c5c GA |
299 | if (!opts) { |
300 | return; | |
301 | } | |
302 | ||
4db14629 | 303 | vnc_display_open("default", errp); |
333a96ec LC |
304 | } |
305 | ||
306 | static void qmp_change_vnc(const char *target, bool has_arg, const char *arg, | |
307 | Error **errp) | |
308 | { | |
309 | if (strcmp(target, "passwd") == 0 || strcmp(target, "password") == 0) { | |
310 | if (!has_arg) { | |
c6bd8c70 | 311 | error_setg(errp, QERR_MISSING_PARAMETER, "password"); |
333a96ec LC |
312 | } else { |
313 | qmp_change_vnc_password(arg, errp); | |
314 | } | |
315 | } else { | |
316 | qmp_change_vnc_listen(target, errp); | |
317 | } | |
318 | } | |
333a96ec LC |
319 | #endif /* !CONFIG_VNC */ |
320 | ||
321 | void qmp_change(const char *device, const char *target, | |
7daecb30 | 322 | bool has_arg, const char *arg, Error **errp) |
333a96ec LC |
323 | { |
324 | if (strcmp(device, "vnc") == 0) { | |
05eb4a25 | 325 | #ifdef CONFIG_VNC |
7daecb30 | 326 | qmp_change_vnc(target, has_arg, arg, errp); |
05eb4a25 MAL |
327 | #else |
328 | error_setg(errp, QERR_FEATURE_DISABLED, "vnc"); | |
329 | #endif | |
333a96ec | 330 | } else { |
70e2cb3b KW |
331 | qmp_blockdev_change_medium(true, device, false, NULL, target, |
332 | has_arg, arg, false, 0, errp); | |
333a96ec LC |
333 | } |
334 | } | |
5eeee3fa | 335 | |
b224e5e2 LC |
336 | void qmp_add_client(const char *protocol, const char *fdname, |
337 | bool has_skipauth, bool skipauth, bool has_tls, bool tls, | |
338 | Error **errp) | |
339 | { | |
0ec7b3e7 | 340 | Chardev *s; |
b224e5e2 LC |
341 | int fd; |
342 | ||
343 | fd = monitor_get_fd(cur_mon, fdname, errp); | |
344 | if (fd < 0) { | |
345 | return; | |
346 | } | |
347 | ||
348 | if (strcmp(protocol, "spice") == 0) { | |
b25d81ba | 349 | if (!qemu_using_spice(errp)) { |
b224e5e2 LC |
350 | close(fd); |
351 | return; | |
352 | } | |
353 | skipauth = has_skipauth ? skipauth : false; | |
354 | tls = has_tls ? tls : false; | |
355 | if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) { | |
356 | error_setg(errp, "spice failed to add client"); | |
357 | close(fd); | |
358 | } | |
359 | return; | |
360 | #ifdef CONFIG_VNC | |
361 | } else if (strcmp(protocol, "vnc") == 0) { | |
362 | skipauth = has_skipauth ? skipauth : false; | |
363 | vnc_display_add_client(NULL, fd, skipauth); | |
364 | return; | |
365 | #endif | |
366 | } else if ((s = qemu_chr_find(protocol)) != NULL) { | |
367 | if (qemu_chr_add_client(s, fd) < 0) { | |
368 | error_setg(errp, "failed to add client"); | |
369 | close(fd); | |
370 | return; | |
371 | } | |
372 | return; | |
373 | } | |
374 | ||
375 | error_setg(errp, "protocol '%s' is invalid", protocol); | |
376 | close(fd); | |
377 | } | |
ab2d0531 | 378 | |
cff8b2c6 | 379 | |
6f2e2730 IM |
380 | MemoryDeviceInfoList *qmp_query_memory_devices(Error **errp) |
381 | { | |
2cc0e2e8 | 382 | return qmp_memory_device_list(); |
6f2e2730 | 383 | } |
02419bcb IM |
384 | |
385 | ACPIOSTInfoList *qmp_query_acpi_ospm_status(Error **errp) | |
386 | { | |
387 | bool ambig; | |
388 | ACPIOSTInfoList *head = NULL; | |
389 | ACPIOSTInfoList **prev = &head; | |
390 | Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, &ambig); | |
391 | ||
392 | if (obj) { | |
393 | AcpiDeviceIfClass *adevc = ACPI_DEVICE_IF_GET_CLASS(obj); | |
394 | AcpiDeviceIf *adev = ACPI_DEVICE_IF(obj); | |
395 | ||
396 | adevc->ospm_status(adev, &prev); | |
397 | } else { | |
398 | error_setg(errp, "command is not supported, missing ACPI device"); | |
399 | } | |
400 | ||
401 | return head; | |
402 | } | |
9aa3397f VG |
403 | |
404 | MemoryInfo *qmp_query_memory_size_summary(Error **errp) | |
405 | { | |
406 | MemoryInfo *mem_info = g_malloc0(sizeof(MemoryInfo)); | |
407 | ||
408 | mem_info->base_memory = ram_size; | |
409 | ||
410 | mem_info->plugged_memory = get_plugged_memory_size(); | |
411 | mem_info->has_plugged_memory = | |
412 | mem_info->plugged_memory != (uint64_t)-1; | |
413 | ||
414 | return mem_info; | |
415 | } |