2
0

qmp-cmds.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "qemu/osdep.h"
  16. #include "qemu/sockets.h"
  17. #include "monitor-internal.h"
  18. #include "monitor/qdev.h"
  19. #include "monitor/qmp-helpers.h"
  20. #include "sysemu/sysemu.h"
  21. #include "sysemu/kvm.h"
  22. #include "sysemu/runstate.h"
  23. #include "sysemu/runstate-action.h"
  24. #include "sysemu/block-backend.h"
  25. #include "qapi/error.h"
  26. #include "qapi/qapi-init-commands.h"
  27. #include "qapi/qapi-commands-control.h"
  28. #include "qapi/qapi-commands-misc.h"
  29. #include "qapi/qmp/qerror.h"
  30. #include "qapi/type-helpers.h"
  31. #include "hw/mem/memory-device.h"
  32. #include "hw/intc/intc.h"
  33. #include "hw/rdma/rdma.h"
  34. NameInfo *qmp_query_name(Error **errp)
  35. {
  36. NameInfo *info = g_malloc0(sizeof(*info));
  37. info->name = g_strdup(qemu_name);
  38. return info;
  39. }
  40. void qmp_quit(Error **errp)
  41. {
  42. shutdown_action = SHUTDOWN_ACTION_POWEROFF;
  43. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_QMP_QUIT);
  44. }
  45. void qmp_stop(Error **errp)
  46. {
  47. /* if there is a dump in background, we should wait until the dump
  48. * finished */
  49. if (qemu_system_dump_in_progress()) {
  50. error_setg(errp, "There is a dump in process, please wait.");
  51. return;
  52. }
  53. if (runstate_check(RUN_STATE_INMIGRATE)) {
  54. autostart = 0;
  55. } else {
  56. vm_stop(RUN_STATE_PAUSED);
  57. }
  58. }
  59. void qmp_cont(Error **errp)
  60. {
  61. BlockBackend *blk;
  62. BlockJob *job;
  63. Error *local_err = NULL;
  64. /* if there is a dump in background, we should wait until the dump
  65. * finished */
  66. if (qemu_system_dump_in_progress()) {
  67. error_setg(errp, "There is a dump in process, please wait.");
  68. return;
  69. }
  70. if (runstate_needs_reset()) {
  71. error_setg(errp, "Resetting the Virtual Machine is required");
  72. return;
  73. } else if (runstate_check(RUN_STATE_SUSPENDED)) {
  74. return;
  75. } else if (runstate_check(RUN_STATE_FINISH_MIGRATE)) {
  76. error_setg(errp, "Migration is not finalized yet");
  77. return;
  78. }
  79. for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
  80. blk_iostatus_reset(blk);
  81. }
  82. WITH_JOB_LOCK_GUARD() {
  83. for (job = block_job_next_locked(NULL); job;
  84. job = block_job_next_locked(job)) {
  85. block_job_iostatus_reset_locked(job);
  86. }
  87. }
  88. /* Continuing after completed migration. Images have been inactivated to
  89. * allow the destination to take control. Need to get control back now.
  90. *
  91. * If there are no inactive block nodes (e.g. because the VM was just
  92. * paused rather than completing a migration), bdrv_inactivate_all() simply
  93. * doesn't do anything. */
  94. bdrv_activate_all(&local_err);
  95. if (local_err) {
  96. error_propagate(errp, local_err);
  97. return;
  98. }
  99. if (runstate_check(RUN_STATE_INMIGRATE)) {
  100. autostart = 1;
  101. } else {
  102. vm_start();
  103. }
  104. }
  105. void qmp_add_client(const char *protocol, const char *fdname,
  106. bool has_skipauth, bool skipauth, bool has_tls, bool tls,
  107. Error **errp)
  108. {
  109. static const struct {
  110. const char *name;
  111. bool (*add_client)(int fd, bool has_skipauth, bool skipauth,
  112. bool has_tls, bool tls, Error **errp);
  113. } protocol_table[] = {
  114. { "spice", qmp_add_client_spice },
  115. #ifdef CONFIG_VNC
  116. { "vnc", qmp_add_client_vnc },
  117. #endif
  118. #ifdef CONFIG_DBUS_DISPLAY
  119. { "@dbus-display", qmp_add_client_dbus_display },
  120. #endif
  121. };
  122. int fd, i;
  123. fd = monitor_get_fd(monitor_cur(), fdname, errp);
  124. if (fd < 0) {
  125. return;
  126. }
  127. if (!fd_is_socket(fd)) {
  128. error_setg(errp, "parameter @fdname must name a socket");
  129. close(fd);
  130. return;
  131. }
  132. for (i = 0; i < ARRAY_SIZE(protocol_table); i++) {
  133. if (!strcmp(protocol, protocol_table[i].name)) {
  134. if (!protocol_table[i].add_client(fd, has_skipauth, skipauth,
  135. has_tls, tls, errp)) {
  136. close(fd);
  137. }
  138. return;
  139. }
  140. }
  141. if (!qmp_add_client_char(fd, has_skipauth, skipauth, has_tls, tls,
  142. protocol, errp)) {
  143. close(fd);
  144. }
  145. }
  146. char *qmp_human_monitor_command(const char *command_line, bool has_cpu_index,
  147. int64_t cpu_index, Error **errp)
  148. {
  149. char *output = NULL;
  150. MonitorHMP hmp = {};
  151. monitor_data_init(&hmp.common, false, true, false);
  152. if (has_cpu_index) {
  153. int ret = monitor_set_cpu(&hmp.common, cpu_index);
  154. if (ret < 0) {
  155. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "cpu-index",
  156. "a CPU number");
  157. goto out;
  158. }
  159. }
  160. handle_hmp_command(&hmp, command_line);
  161. WITH_QEMU_LOCK_GUARD(&hmp.common.mon_lock) {
  162. output = g_strdup(hmp.common.outbuf->str);
  163. }
  164. out:
  165. monitor_data_destroy(&hmp.common);
  166. return output;
  167. }
  168. static void __attribute__((__constructor__)) monitor_init_qmp_commands(void)
  169. {
  170. /*
  171. * Two command lists:
  172. * - qmp_commands contains all QMP commands
  173. * - qmp_cap_negotiation_commands contains just
  174. * "qmp_capabilities", to enforce capability negotiation
  175. */
  176. qmp_init_marshal(&qmp_commands);
  177. qmp_register_command(&qmp_commands, "device_add",
  178. qmp_device_add, 0, 0);
  179. QTAILQ_INIT(&qmp_cap_negotiation_commands);
  180. qmp_register_command(&qmp_cap_negotiation_commands, "qmp_capabilities",
  181. qmp_marshal_qmp_capabilities,
  182. QCO_ALLOW_PRECONFIG, 0);
  183. }