qmp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. *
  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-common.h"
  16. #include "sysemu/sysemu.h"
  17. #include "qmp-commands.h"
  18. #include "char/char.h"
  19. #include "ui/qemu-spice.h"
  20. #include "ui/vnc.h"
  21. #include "sysemu/kvm.h"
  22. #include "sysemu/arch_init.h"
  23. #include "hw/qdev.h"
  24. #include "sysemu/blockdev.h"
  25. #include "qom/qom-qobject.h"
  26. NameInfo *qmp_query_name(Error **errp)
  27. {
  28. NameInfo *info = g_malloc0(sizeof(*info));
  29. if (qemu_name) {
  30. info->has_name = true;
  31. info->name = g_strdup(qemu_name);
  32. }
  33. return info;
  34. }
  35. VersionInfo *qmp_query_version(Error **err)
  36. {
  37. VersionInfo *info = g_malloc0(sizeof(*info));
  38. const char *version = QEMU_VERSION;
  39. char *tmp;
  40. info->qemu.major = strtol(version, &tmp, 10);
  41. tmp++;
  42. info->qemu.minor = strtol(tmp, &tmp, 10);
  43. tmp++;
  44. info->qemu.micro = strtol(tmp, &tmp, 10);
  45. info->package = g_strdup(QEMU_PKGVERSION);
  46. return info;
  47. }
  48. KvmInfo *qmp_query_kvm(Error **errp)
  49. {
  50. KvmInfo *info = g_malloc0(sizeof(*info));
  51. info->enabled = kvm_enabled();
  52. info->present = kvm_available();
  53. return info;
  54. }
  55. UuidInfo *qmp_query_uuid(Error **errp)
  56. {
  57. UuidInfo *info = g_malloc0(sizeof(*info));
  58. char uuid[64];
  59. snprintf(uuid, sizeof(uuid), UUID_FMT, qemu_uuid[0], qemu_uuid[1],
  60. qemu_uuid[2], qemu_uuid[3], qemu_uuid[4], qemu_uuid[5],
  61. qemu_uuid[6], qemu_uuid[7], qemu_uuid[8], qemu_uuid[9],
  62. qemu_uuid[10], qemu_uuid[11], qemu_uuid[12], qemu_uuid[13],
  63. qemu_uuid[14], qemu_uuid[15]);
  64. info->UUID = g_strdup(uuid);
  65. return info;
  66. }
  67. void qmp_quit(Error **err)
  68. {
  69. no_shutdown = 0;
  70. qemu_system_shutdown_request();
  71. }
  72. void qmp_stop(Error **errp)
  73. {
  74. if (runstate_check(RUN_STATE_INMIGRATE)) {
  75. autostart = 0;
  76. } else {
  77. vm_stop(RUN_STATE_PAUSED);
  78. }
  79. }
  80. void qmp_system_reset(Error **errp)
  81. {
  82. qemu_system_reset_request();
  83. }
  84. void qmp_system_powerdown(Error **erp)
  85. {
  86. qemu_system_powerdown_request();
  87. }
  88. void qmp_cpu(int64_t index, Error **errp)
  89. {
  90. /* Just do nothing */
  91. }
  92. #ifndef CONFIG_VNC
  93. /* If VNC support is enabled, the "true" query-vnc command is
  94. defined in the VNC subsystem */
  95. VncInfo *qmp_query_vnc(Error **errp)
  96. {
  97. error_set(errp, QERR_FEATURE_DISABLED, "vnc");
  98. return NULL;
  99. };
  100. #endif
  101. #ifndef CONFIG_SPICE
  102. /* If SPICE support is enabled, the "true" query-spice command is
  103. defined in the SPICE subsystem. Also note that we use a small
  104. trick to maintain query-spice's original behavior, which is not
  105. to be available in the namespace if SPICE is not compiled in */
  106. SpiceInfo *qmp_query_spice(Error **errp)
  107. {
  108. error_set(errp, QERR_COMMAND_NOT_FOUND, "query-spice");
  109. return NULL;
  110. };
  111. #endif
  112. static void iostatus_bdrv_it(void *opaque, BlockDriverState *bs)
  113. {
  114. bdrv_iostatus_reset(bs);
  115. }
  116. static void encrypted_bdrv_it(void *opaque, BlockDriverState *bs)
  117. {
  118. Error **err = opaque;
  119. if (!error_is_set(err) && bdrv_key_required(bs)) {
  120. error_set(err, QERR_DEVICE_ENCRYPTED, bdrv_get_device_name(bs),
  121. bdrv_get_encrypted_filename(bs));
  122. }
  123. }
  124. void qmp_cont(Error **errp)
  125. {
  126. Error *local_err = NULL;
  127. if (runstate_check(RUN_STATE_INTERNAL_ERROR) ||
  128. runstate_check(RUN_STATE_SHUTDOWN)) {
  129. error_set(errp, QERR_RESET_REQUIRED);
  130. return;
  131. } else if (runstate_check(RUN_STATE_SUSPENDED)) {
  132. return;
  133. }
  134. bdrv_iterate(iostatus_bdrv_it, NULL);
  135. bdrv_iterate(encrypted_bdrv_it, &local_err);
  136. if (local_err) {
  137. error_propagate(errp, local_err);
  138. return;
  139. }
  140. if (runstate_check(RUN_STATE_INMIGRATE)) {
  141. autostart = 1;
  142. } else {
  143. vm_start();
  144. }
  145. }
  146. void qmp_system_wakeup(Error **errp)
  147. {
  148. qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
  149. }
  150. ObjectPropertyInfoList *qmp_qom_list(const char *path, Error **errp)
  151. {
  152. Object *obj;
  153. bool ambiguous = false;
  154. ObjectPropertyInfoList *props = NULL;
  155. ObjectProperty *prop;
  156. obj = object_resolve_path(path, &ambiguous);
  157. if (obj == NULL) {
  158. error_set(errp, QERR_DEVICE_NOT_FOUND, path);
  159. return NULL;
  160. }
  161. QTAILQ_FOREACH(prop, &obj->properties, node) {
  162. ObjectPropertyInfoList *entry = g_malloc0(sizeof(*entry));
  163. entry->value = g_malloc0(sizeof(ObjectPropertyInfo));
  164. entry->next = props;
  165. props = entry;
  166. entry->value->name = g_strdup(prop->name);
  167. entry->value->type = g_strdup(prop->type);
  168. }
  169. return props;
  170. }
  171. /* FIXME: teach qapi about how to pass through Visitors */
  172. int qmp_qom_set(Monitor *mon, const QDict *qdict, QObject **ret)
  173. {
  174. const char *path = qdict_get_str(qdict, "path");
  175. const char *property = qdict_get_str(qdict, "property");
  176. QObject *value = qdict_get(qdict, "value");
  177. Error *local_err = NULL;
  178. Object *obj;
  179. obj = object_resolve_path(path, NULL);
  180. if (!obj) {
  181. error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
  182. goto out;
  183. }
  184. object_property_set_qobject(obj, value, property, &local_err);
  185. out:
  186. if (local_err) {
  187. qerror_report_err(local_err);
  188. error_free(local_err);
  189. return -1;
  190. }
  191. return 0;
  192. }
  193. int qmp_qom_get(Monitor *mon, const QDict *qdict, QObject **ret)
  194. {
  195. const char *path = qdict_get_str(qdict, "path");
  196. const char *property = qdict_get_str(qdict, "property");
  197. Error *local_err = NULL;
  198. Object *obj;
  199. obj = object_resolve_path(path, NULL);
  200. if (!obj) {
  201. error_set(&local_err, QERR_DEVICE_NOT_FOUND, path);
  202. goto out;
  203. }
  204. *ret = object_property_get_qobject(obj, property, &local_err);
  205. out:
  206. if (local_err) {
  207. qerror_report_err(local_err);
  208. error_free(local_err);
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. void qmp_set_password(const char *protocol, const char *password,
  214. bool has_connected, const char *connected, Error **errp)
  215. {
  216. int disconnect_if_connected = 0;
  217. int fail_if_connected = 0;
  218. int rc;
  219. if (has_connected) {
  220. if (strcmp(connected, "fail") == 0) {
  221. fail_if_connected = 1;
  222. } else if (strcmp(connected, "disconnect") == 0) {
  223. disconnect_if_connected = 1;
  224. } else if (strcmp(connected, "keep") == 0) {
  225. /* nothing */
  226. } else {
  227. error_set(errp, QERR_INVALID_PARAMETER, "connected");
  228. return;
  229. }
  230. }
  231. if (strcmp(protocol, "spice") == 0) {
  232. if (!using_spice) {
  233. /* correct one? spice isn't a device ,,, */
  234. error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
  235. return;
  236. }
  237. rc = qemu_spice_set_passwd(password, fail_if_connected,
  238. disconnect_if_connected);
  239. if (rc != 0) {
  240. error_set(errp, QERR_SET_PASSWD_FAILED);
  241. }
  242. return;
  243. }
  244. if (strcmp(protocol, "vnc") == 0) {
  245. if (fail_if_connected || disconnect_if_connected) {
  246. /* vnc supports "connected=keep" only */
  247. error_set(errp, QERR_INVALID_PARAMETER, "connected");
  248. return;
  249. }
  250. /* Note that setting an empty password will not disable login through
  251. * this interface. */
  252. rc = vnc_display_password(NULL, password);
  253. if (rc < 0) {
  254. error_set(errp, QERR_SET_PASSWD_FAILED);
  255. }
  256. return;
  257. }
  258. error_set(errp, QERR_INVALID_PARAMETER, "protocol");
  259. }
  260. void qmp_expire_password(const char *protocol, const char *whenstr,
  261. Error **errp)
  262. {
  263. time_t when;
  264. int rc;
  265. if (strcmp(whenstr, "now") == 0) {
  266. when = 0;
  267. } else if (strcmp(whenstr, "never") == 0) {
  268. when = TIME_MAX;
  269. } else if (whenstr[0] == '+') {
  270. when = time(NULL) + strtoull(whenstr+1, NULL, 10);
  271. } else {
  272. when = strtoull(whenstr, NULL, 10);
  273. }
  274. if (strcmp(protocol, "spice") == 0) {
  275. if (!using_spice) {
  276. /* correct one? spice isn't a device ,,, */
  277. error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
  278. return;
  279. }
  280. rc = qemu_spice_set_pw_expire(when);
  281. if (rc != 0) {
  282. error_set(errp, QERR_SET_PASSWD_FAILED);
  283. }
  284. return;
  285. }
  286. if (strcmp(protocol, "vnc") == 0) {
  287. rc = vnc_display_pw_expire(NULL, when);
  288. if (rc != 0) {
  289. error_set(errp, QERR_SET_PASSWD_FAILED);
  290. }
  291. return;
  292. }
  293. error_set(errp, QERR_INVALID_PARAMETER, "protocol");
  294. }
  295. #ifdef CONFIG_VNC
  296. void qmp_change_vnc_password(const char *password, Error **errp)
  297. {
  298. if (vnc_display_password(NULL, password) < 0) {
  299. error_set(errp, QERR_SET_PASSWD_FAILED);
  300. }
  301. }
  302. static void qmp_change_vnc_listen(const char *target, Error **errp)
  303. {
  304. vnc_display_open(NULL, target, errp);
  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) {
  311. error_set(errp, QERR_MISSING_PARAMETER, "password");
  312. } else {
  313. qmp_change_vnc_password(arg, errp);
  314. }
  315. } else {
  316. qmp_change_vnc_listen(target, errp);
  317. }
  318. }
  319. #else
  320. void qmp_change_vnc_password(const char *password, Error **errp)
  321. {
  322. error_set(errp, QERR_FEATURE_DISABLED, "vnc");
  323. }
  324. static void qmp_change_vnc(const char *target, bool has_arg, const char *arg,
  325. Error **errp)
  326. {
  327. error_set(errp, QERR_FEATURE_DISABLED, "vnc");
  328. }
  329. #endif /* !CONFIG_VNC */
  330. void qmp_change(const char *device, const char *target,
  331. bool has_arg, const char *arg, Error **err)
  332. {
  333. if (strcmp(device, "vnc") == 0) {
  334. qmp_change_vnc(target, has_arg, arg, err);
  335. } else {
  336. qmp_change_blockdev(device, target, has_arg, arg, err);
  337. }
  338. }
  339. static void qom_list_types_tramp(ObjectClass *klass, void *data)
  340. {
  341. ObjectTypeInfoList *e, **pret = data;
  342. ObjectTypeInfo *info;
  343. info = g_malloc0(sizeof(*info));
  344. info->name = g_strdup(object_class_get_name(klass));
  345. e = g_malloc0(sizeof(*e));
  346. e->value = info;
  347. e->next = *pret;
  348. *pret = e;
  349. }
  350. ObjectTypeInfoList *qmp_qom_list_types(bool has_implements,
  351. const char *implements,
  352. bool has_abstract,
  353. bool abstract,
  354. Error **errp)
  355. {
  356. ObjectTypeInfoList *ret = NULL;
  357. object_class_foreach(qom_list_types_tramp, implements, abstract, &ret);
  358. return ret;
  359. }
  360. DevicePropertyInfoList *qmp_device_list_properties(const char *typename,
  361. Error **errp)
  362. {
  363. ObjectClass *klass;
  364. Property *prop;
  365. DevicePropertyInfoList *prop_list = NULL;
  366. klass = object_class_by_name(typename);
  367. if (klass == NULL) {
  368. error_set(errp, QERR_DEVICE_NOT_FOUND, typename);
  369. return NULL;
  370. }
  371. klass = object_class_dynamic_cast(klass, TYPE_DEVICE);
  372. if (klass == NULL) {
  373. error_set(errp, QERR_INVALID_PARAMETER_VALUE,
  374. "name", TYPE_DEVICE);
  375. return NULL;
  376. }
  377. do {
  378. for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
  379. DevicePropertyInfoList *entry;
  380. DevicePropertyInfo *info;
  381. /*
  382. * TODO Properties without a parser are just for dirty hacks.
  383. * qdev_prop_ptr is the only such PropertyInfo. It's marked
  384. * for removal. This conditional should be removed along with
  385. * it.
  386. */
  387. if (!prop->info->set) {
  388. continue; /* no way to set it, don't show */
  389. }
  390. info = g_malloc0(sizeof(*info));
  391. info->name = g_strdup(prop->name);
  392. info->type = g_strdup(prop->info->legacy_name ?: prop->info->name);
  393. entry = g_malloc0(sizeof(*entry));
  394. entry->value = info;
  395. entry->next = prop_list;
  396. prop_list = entry;
  397. }
  398. klass = object_class_get_parent(klass);
  399. } while (klass != object_class_by_name(TYPE_DEVICE));
  400. return prop_list;
  401. }
  402. CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp)
  403. {
  404. return arch_query_cpu_definitions(errp);
  405. }
  406. void qmp_add_client(const char *protocol, const char *fdname,
  407. bool has_skipauth, bool skipauth, bool has_tls, bool tls,
  408. Error **errp)
  409. {
  410. CharDriverState *s;
  411. int fd;
  412. fd = monitor_get_fd(cur_mon, fdname, errp);
  413. if (fd < 0) {
  414. return;
  415. }
  416. if (strcmp(protocol, "spice") == 0) {
  417. if (!using_spice) {
  418. error_set(errp, QERR_DEVICE_NOT_ACTIVE, "spice");
  419. close(fd);
  420. return;
  421. }
  422. skipauth = has_skipauth ? skipauth : false;
  423. tls = has_tls ? tls : false;
  424. if (qemu_spice_display_add_client(fd, skipauth, tls) < 0) {
  425. error_setg(errp, "spice failed to add client");
  426. close(fd);
  427. }
  428. return;
  429. #ifdef CONFIG_VNC
  430. } else if (strcmp(protocol, "vnc") == 0) {
  431. skipauth = has_skipauth ? skipauth : false;
  432. vnc_display_add_client(NULL, fd, skipauth);
  433. return;
  434. #endif
  435. } else if ((s = qemu_chr_find(protocol)) != NULL) {
  436. if (qemu_chr_add_client(s, fd) < 0) {
  437. error_setg(errp, "failed to add client");
  438. close(fd);
  439. return;
  440. }
  441. return;
  442. }
  443. error_setg(errp, "protocol '%s' is invalid", protocol);
  444. close(fd);
  445. }