2
0

qmp.c 16 KB

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