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