dbus-vmstate.c 15 KB

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