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