dbus-vmstate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * QEMU dbus-vmstate
  3. *
  4. * Copyright (C) 2019 Red Hat Inc
  5. *
  6. * Authors:
  7. * Marc-André Lureau <marcandre.lureau@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/units.h"
  14. #include "qemu/dbus.h"
  15. #include "qemu/error-report.h"
  16. #include "qapi/error.h"
  17. #include "qom/object_interfaces.h"
  18. #include "qapi/qmp/qerror.h"
  19. #include "migration/vmstate.h"
  20. #include "trace.h"
  21. typedef struct DBusVMState DBusVMState;
  22. typedef struct DBusVMStateClass DBusVMStateClass;
  23. #define TYPE_DBUS_VMSTATE "dbus-vmstate"
  24. #define DBUS_VMSTATE(obj) \
  25. OBJECT_CHECK(DBusVMState, (obj), TYPE_DBUS_VMSTATE)
  26. #define DBUS_VMSTATE_GET_CLASS(obj) \
  27. OBJECT_GET_CLASS(DBusVMStateClass, (obj), TYPE_DBUS_VMSTATE)
  28. #define DBUS_VMSTATE_CLASS(klass) \
  29. OBJECT_CLASS_CHECK(DBusVMStateClass, (klass), TYPE_DBUS_VMSTATE)
  30. struct DBusVMStateClass {
  31. ObjectClass parent_class;
  32. };
  33. struct DBusVMState {
  34. Object parent;
  35. GDBusConnection *bus;
  36. char *dbus_addr;
  37. char *id_list;
  38. uint32_t data_size;
  39. uint8_t *data;
  40. };
  41. static const GDBusPropertyInfo vmstate_property_info[] = {
  42. { -1, (char *) "Id", (char *) "s",
  43. G_DBUS_PROPERTY_INFO_FLAGS_READABLE, NULL },
  44. };
  45. static const GDBusPropertyInfo * const vmstate_property_info_pointers[] = {
  46. &vmstate_property_info[0],
  47. NULL
  48. };
  49. static const GDBusInterfaceInfo vmstate1_interface_info = {
  50. -1,
  51. (char *) "org.qemu.VMState1",
  52. (GDBusMethodInfo **) NULL,
  53. (GDBusSignalInfo **) NULL,
  54. (GDBusPropertyInfo **) &vmstate_property_info_pointers,
  55. NULL,
  56. };
  57. #define DBUS_VMSTATE_SIZE_LIMIT (1 * MiB)
  58. static GHashTable *
  59. get_id_list_set(DBusVMState *self)
  60. {
  61. g_auto(GStrv) ids = NULL;
  62. g_autoptr(GHashTable) set = NULL;
  63. int i;
  64. if (!self->id_list) {
  65. return NULL;
  66. }
  67. ids = g_strsplit(self->id_list, ",", -1);
  68. set = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
  69. for (i = 0; ids[i]; i++) {
  70. g_hash_table_add(set, ids[i]);
  71. ids[i] = NULL;
  72. }
  73. return g_steal_pointer(&set);
  74. }
  75. static GHashTable *
  76. dbus_get_proxies(DBusVMState *self, GError **err)
  77. {
  78. g_autoptr(GHashTable) proxies = NULL;
  79. g_autoptr(GHashTable) ids = NULL;
  80. g_auto(GStrv) names = NULL;
  81. Error *error = NULL;
  82. size_t i;
  83. ids = get_id_list_set(self);
  84. proxies = g_hash_table_new_full(g_str_hash, g_str_equal,
  85. g_free, g_object_unref);
  86. names = qemu_dbus_get_queued_owners(self->bus, "org.qemu.VMState1", &error);
  87. if (!names) {
  88. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED, "%s",
  89. error_get_pretty(error));
  90. error_free(error);
  91. return NULL;
  92. }
  93. for (i = 0; names[i]; i++) {
  94. g_autoptr(GDBusProxy) proxy = NULL;
  95. g_autoptr(GVariant) result = NULL;
  96. g_autofree char *id = NULL;
  97. size_t size;
  98. proxy = g_dbus_proxy_new_sync(self->bus, G_DBUS_PROXY_FLAGS_NONE,
  99. (GDBusInterfaceInfo *) &vmstate1_interface_info,
  100. names[i],
  101. "/org/qemu/VMState1",
  102. "org.qemu.VMState1",
  103. NULL, err);
  104. if (!proxy) {
  105. return NULL;
  106. }
  107. result = g_dbus_proxy_get_cached_property(proxy, "Id");
  108. if (!result) {
  109. g_set_error_literal(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  110. "VMState Id property is missing.");
  111. return NULL;
  112. }
  113. id = g_variant_dup_string(result, &size);
  114. if (ids && !g_hash_table_remove(ids, id)) {
  115. g_clear_pointer(&id, g_free);
  116. g_clear_object(&proxy);
  117. continue;
  118. }
  119. if (size == 0 || size >= 256) {
  120. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  121. "VMState Id '%s' is invalid.", id);
  122. return NULL;
  123. }
  124. if (!g_hash_table_insert(proxies, id, proxy)) {
  125. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  126. "Duplicated VMState Id '%s'", id);
  127. return NULL;
  128. }
  129. id = NULL;
  130. proxy = NULL;
  131. g_clear_pointer(&result, g_variant_unref);
  132. }
  133. if (ids) {
  134. g_autofree char **left = NULL;
  135. left = (char **)g_hash_table_get_keys_as_array(ids, NULL);
  136. if (*left) {
  137. g_autofree char *leftids = g_strjoinv(",", left);
  138. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  139. "Required VMState Id are missing: %s", leftids);
  140. return NULL;
  141. }
  142. }
  143. return g_steal_pointer(&proxies);
  144. }
  145. static int
  146. dbus_load_state_proxy(GDBusProxy *proxy, const uint8_t *data, size_t size)
  147. {
  148. g_autoptr(GError) err = NULL;
  149. g_autoptr(GVariant) result = NULL;
  150. g_autoptr(GVariant) value = NULL;
  151. value = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
  152. data, size, sizeof(char));
  153. result = g_dbus_proxy_call_sync(proxy, "Load",
  154. g_variant_new("(@ay)",
  155. g_steal_pointer(&value)),
  156. G_DBUS_CALL_FLAGS_NO_AUTO_START,
  157. -1, NULL, &err);
  158. if (!result) {
  159. error_report("%s: Failed to Load: %s", __func__, err->message);
  160. return -1;
  161. }
  162. return 0;
  163. }
  164. static int dbus_vmstate_post_load(void *opaque, int version_id)
  165. {
  166. DBusVMState *self = DBUS_VMSTATE(opaque);
  167. g_autoptr(GInputStream) m = NULL;
  168. g_autoptr(GDataInputStream) s = NULL;
  169. g_autoptr(GError) err = NULL;
  170. g_autoptr(GHashTable) proxies = NULL;
  171. uint32_t nelem;
  172. trace_dbus_vmstate_post_load(version_id);
  173. proxies = dbus_get_proxies(self, &err);
  174. if (!proxies) {
  175. error_report("%s: Failed to get proxies: %s", __func__, err->message);
  176. return -1;
  177. }
  178. m = g_memory_input_stream_new_from_data(self->data, self->data_size, NULL);
  179. s = g_data_input_stream_new(m);
  180. g_data_input_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  181. nelem = g_data_input_stream_read_uint32(s, NULL, &err);
  182. if (err) {
  183. goto error;
  184. }
  185. while (nelem > 0) {
  186. GDBusProxy *proxy = NULL;
  187. uint32_t len;
  188. gsize bytes_read, avail;
  189. char id[256];
  190. len = g_data_input_stream_read_uint32(s, NULL, &err);
  191. if (err) {
  192. goto error;
  193. }
  194. if (len >= 256) {
  195. error_report("%s: Invalid DBus vmstate proxy name %u",
  196. __func__, len);
  197. return -1;
  198. }
  199. if (!g_input_stream_read_all(G_INPUT_STREAM(s), id, len,
  200. &bytes_read, NULL, &err)) {
  201. goto error;
  202. }
  203. g_return_val_if_fail(bytes_read == len, -1);
  204. id[len] = 0;
  205. trace_dbus_vmstate_loading(id);
  206. proxy = g_hash_table_lookup(proxies, id);
  207. if (!proxy) {
  208. error_report("%s: Failed to find proxy Id '%s'", __func__, id);
  209. return -1;
  210. }
  211. len = g_data_input_stream_read_uint32(s, NULL, &err);
  212. avail = g_buffered_input_stream_get_available(
  213. G_BUFFERED_INPUT_STREAM(s));
  214. if (len > DBUS_VMSTATE_SIZE_LIMIT || len > avail) {
  215. error_report("%s: Invalid vmstate size: %u", __func__, len);
  216. return -1;
  217. }
  218. if (dbus_load_state_proxy(proxy,
  219. g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s),
  220. NULL),
  221. len) < 0) {
  222. error_report("%s: Failed to restore Id '%s'", __func__, id);
  223. return -1;
  224. }
  225. if (!g_seekable_seek(G_SEEKABLE(s), len, G_SEEK_CUR, NULL, &err)) {
  226. goto error;
  227. }
  228. nelem -= 1;
  229. }
  230. return 0;
  231. error:
  232. error_report("%s: Failed to read from stream: %s", __func__, err->message);
  233. return -1;
  234. }
  235. static void
  236. dbus_save_state_proxy(gpointer key,
  237. gpointer value,
  238. gpointer user_data)
  239. {
  240. GDataOutputStream *s = user_data;
  241. const char *id = key;
  242. GDBusProxy *proxy = value;
  243. g_autoptr(GVariant) result = NULL;
  244. g_autoptr(GVariant) child = NULL;
  245. g_autoptr(GError) err = NULL;
  246. const uint8_t *data;
  247. gsize size;
  248. trace_dbus_vmstate_saving(id);
  249. result = g_dbus_proxy_call_sync(proxy, "Save",
  250. NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START,
  251. -1, NULL, &err);
  252. if (!result) {
  253. error_report("%s: Failed to Save: %s", __func__, err->message);
  254. return;
  255. }
  256. child = g_variant_get_child_value(result, 0);
  257. data = g_variant_get_fixed_array(child, &size, sizeof(char));
  258. if (!data) {
  259. error_report("%s: Failed to Save: not a byte array", __func__);
  260. return;
  261. }
  262. if (size > DBUS_VMSTATE_SIZE_LIMIT) {
  263. error_report("%s: Too large vmstate data to save: %zu",
  264. __func__, (size_t)size);
  265. return;
  266. }
  267. if (!g_data_output_stream_put_uint32(s, strlen(id), NULL, &err) ||
  268. !g_data_output_stream_put_string(s, id, NULL, &err) ||
  269. !g_data_output_stream_put_uint32(s, size, NULL, &err) ||
  270. !g_output_stream_write_all(G_OUTPUT_STREAM(s),
  271. data, size, NULL, NULL, &err)) {
  272. error_report("%s: Failed to write to stream: %s",
  273. __func__, err->message);
  274. }
  275. }
  276. static int dbus_vmstate_pre_save(void *opaque)
  277. {
  278. DBusVMState *self = DBUS_VMSTATE(opaque);
  279. g_autoptr(GOutputStream) m = NULL;
  280. g_autoptr(GDataOutputStream) s = NULL;
  281. g_autoptr(GHashTable) proxies = NULL;
  282. g_autoptr(GError) err = NULL;
  283. trace_dbus_vmstate_pre_save();
  284. proxies = dbus_get_proxies(self, &err);
  285. if (!proxies) {
  286. error_report("%s: Failed to get proxies: %s", __func__, err->message);
  287. return -1;
  288. }
  289. m = g_memory_output_stream_new_resizable();
  290. s = g_data_output_stream_new(m);
  291. g_data_output_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  292. if (!g_data_output_stream_put_uint32(s, g_hash_table_size(proxies),
  293. NULL, &err)) {
  294. error_report("%s: Failed to write to stream: %s",
  295. __func__, err->message);
  296. return -1;
  297. }
  298. g_hash_table_foreach(proxies, dbus_save_state_proxy, s);
  299. if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m))
  300. > UINT32_MAX) {
  301. error_report("%s: DBus vmstate buffer is too large", __func__);
  302. return -1;
  303. }
  304. if (!g_output_stream_close(G_OUTPUT_STREAM(m), NULL, &err)) {
  305. error_report("%s: Failed to close stream: %s", __func__, err->message);
  306. return -1;
  307. }
  308. g_free(self->data);
  309. self->data_size =
  310. g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m));
  311. self->data =
  312. g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m));
  313. return 0;
  314. }
  315. static const VMStateDescription dbus_vmstate = {
  316. .name = TYPE_DBUS_VMSTATE,
  317. .version_id = 0,
  318. .pre_save = dbus_vmstate_pre_save,
  319. .post_load = dbus_vmstate_post_load,
  320. .fields = (VMStateField[]) {
  321. VMSTATE_UINT32(data_size, DBusVMState),
  322. VMSTATE_VBUFFER_ALLOC_UINT32(data, DBusVMState, 0, 0, data_size),
  323. VMSTATE_END_OF_LIST()
  324. }
  325. };
  326. static void
  327. dbus_vmstate_complete(UserCreatable *uc, Error **errp)
  328. {
  329. DBusVMState *self = DBUS_VMSTATE(uc);
  330. g_autoptr(GError) err = NULL;
  331. if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE, NULL)) {
  332. error_setg(errp, "There is already an instance of %s",
  333. TYPE_DBUS_VMSTATE);
  334. return;
  335. }
  336. if (!self->dbus_addr) {
  337. error_setg(errp, QERR_MISSING_PARAMETER, "addr");
  338. return;
  339. }
  340. self->bus = g_dbus_connection_new_for_address_sync(self->dbus_addr,
  341. G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
  342. G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
  343. NULL, NULL, &err);
  344. if (err) {
  345. error_setg(errp, "failed to connect to DBus: '%s'", err->message);
  346. return;
  347. }
  348. if (vmstate_register(VMSTATE_IF(self), VMSTATE_INSTANCE_ID_ANY,
  349. &dbus_vmstate, self) < 0) {
  350. error_setg(errp, "Failed to register vmstate");
  351. }
  352. }
  353. static void
  354. dbus_vmstate_finalize(Object *o)
  355. {
  356. DBusVMState *self = DBUS_VMSTATE(o);
  357. vmstate_unregister(VMSTATE_IF(self), &dbus_vmstate, self);
  358. g_clear_object(&self->bus);
  359. g_free(self->dbus_addr);
  360. g_free(self->id_list);
  361. g_free(self->data);
  362. }
  363. static char *
  364. get_dbus_addr(Object *o, Error **errp)
  365. {
  366. DBusVMState *self = DBUS_VMSTATE(o);
  367. return g_strdup(self->dbus_addr);
  368. }
  369. static void
  370. set_dbus_addr(Object *o, const char *str, Error **errp)
  371. {
  372. DBusVMState *self = DBUS_VMSTATE(o);
  373. g_free(self->dbus_addr);
  374. self->dbus_addr = g_strdup(str);
  375. }
  376. static char *
  377. get_id_list(Object *o, Error **errp)
  378. {
  379. DBusVMState *self = DBUS_VMSTATE(o);
  380. return g_strdup(self->id_list);
  381. }
  382. static void
  383. set_id_list(Object *o, const char *str, Error **errp)
  384. {
  385. DBusVMState *self = DBUS_VMSTATE(o);
  386. g_free(self->id_list);
  387. self->id_list = g_strdup(str);
  388. }
  389. static char *
  390. dbus_vmstate_get_id(VMStateIf *vmif)
  391. {
  392. return g_strdup(TYPE_DBUS_VMSTATE);
  393. }
  394. static void
  395. dbus_vmstate_class_init(ObjectClass *oc, void *data)
  396. {
  397. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  398. VMStateIfClass *vc = VMSTATE_IF_CLASS(oc);
  399. ucc->complete = dbus_vmstate_complete;
  400. vc->get_id = dbus_vmstate_get_id;
  401. object_class_property_add_str(oc, "addr",
  402. get_dbus_addr, set_dbus_addr);
  403. object_class_property_add_str(oc, "id-list",
  404. get_id_list, set_id_list);
  405. }
  406. static const TypeInfo dbus_vmstate_info = {
  407. .name = TYPE_DBUS_VMSTATE,
  408. .parent = TYPE_OBJECT,
  409. .instance_size = sizeof(DBusVMState),
  410. .instance_finalize = dbus_vmstate_finalize,
  411. .class_size = sizeof(DBusVMStateClass),
  412. .class_init = dbus_vmstate_class_init,
  413. .interfaces = (InterfaceInfo[]) {
  414. { TYPE_USER_CREATABLE },
  415. { TYPE_VMSTATE_IF },
  416. { }
  417. }
  418. };
  419. static void
  420. register_types(void)
  421. {
  422. type_register_static(&dbus_vmstate_info);
  423. }
  424. type_init(register_types);