2
0

dbus-vmstate.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. return NULL;
  98. }
  99. result = g_dbus_proxy_get_cached_property(proxy, "Id");
  100. if (!result) {
  101. g_set_error_literal(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  102. "VMState Id property is missing.");
  103. return NULL;
  104. }
  105. id = g_variant_dup_string(result, &size);
  106. if (ids && !g_hash_table_remove(ids, id)) {
  107. g_clear_pointer(&id, g_free);
  108. g_clear_object(&proxy);
  109. continue;
  110. }
  111. if (size == 0 || size >= 256) {
  112. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  113. "VMState Id '%s' is invalid.", id);
  114. return NULL;
  115. }
  116. if (!g_hash_table_insert(proxies, id, proxy)) {
  117. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  118. "Duplicated VMState Id '%s'", id);
  119. return NULL;
  120. }
  121. id = NULL;
  122. proxy = NULL;
  123. g_clear_pointer(&result, g_variant_unref);
  124. }
  125. if (ids) {
  126. g_autofree char **left = NULL;
  127. left = (char **)g_hash_table_get_keys_as_array(ids, NULL);
  128. if (*left) {
  129. g_autofree char *leftids = g_strjoinv(",", left);
  130. g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
  131. "Required VMState Id are missing: %s", leftids);
  132. return NULL;
  133. }
  134. }
  135. return g_steal_pointer(&proxies);
  136. }
  137. static int
  138. dbus_load_state_proxy(GDBusProxy *proxy, const uint8_t *data, size_t size)
  139. {
  140. g_autoptr(GError) err = NULL;
  141. g_autoptr(GVariant) result = NULL;
  142. g_autoptr(GVariant) value = NULL;
  143. value = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
  144. data, size, sizeof(char));
  145. result = g_dbus_proxy_call_sync(proxy, "Load",
  146. g_variant_new("(@ay)",
  147. g_steal_pointer(&value)),
  148. G_DBUS_CALL_FLAGS_NO_AUTO_START,
  149. -1, NULL, &err);
  150. if (!result) {
  151. error_report("%s: Failed to Load: %s", __func__, err->message);
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. static int dbus_vmstate_post_load(void *opaque, int version_id)
  157. {
  158. DBusVMState *self = DBUS_VMSTATE(opaque);
  159. g_autoptr(GInputStream) m = NULL;
  160. g_autoptr(GDataInputStream) s = NULL;
  161. g_autoptr(GError) err = NULL;
  162. g_autoptr(GHashTable) proxies = NULL;
  163. uint32_t nelem;
  164. trace_dbus_vmstate_post_load(version_id);
  165. proxies = dbus_get_proxies(self, &err);
  166. if (!proxies) {
  167. error_report("%s: Failed to get proxies: %s", __func__, err->message);
  168. return -1;
  169. }
  170. m = g_memory_input_stream_new_from_data(self->data, self->data_size, NULL);
  171. s = g_data_input_stream_new(m);
  172. g_data_input_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  173. nelem = g_data_input_stream_read_uint32(s, NULL, &err);
  174. if (err) {
  175. goto error;
  176. }
  177. while (nelem > 0) {
  178. GDBusProxy *proxy = NULL;
  179. uint32_t len;
  180. gsize bytes_read, avail;
  181. char id[256];
  182. len = g_data_input_stream_read_uint32(s, NULL, &err);
  183. if (err) {
  184. goto error;
  185. }
  186. if (len >= 256) {
  187. error_report("%s: Invalid DBus vmstate proxy name %u",
  188. __func__, len);
  189. return -1;
  190. }
  191. if (!g_input_stream_read_all(G_INPUT_STREAM(s), id, len,
  192. &bytes_read, NULL, &err)) {
  193. goto error;
  194. }
  195. g_return_val_if_fail(bytes_read == len, -1);
  196. id[len] = 0;
  197. trace_dbus_vmstate_loading(id);
  198. proxy = g_hash_table_lookup(proxies, id);
  199. if (!proxy) {
  200. error_report("%s: Failed to find proxy Id '%s'", __func__, id);
  201. return -1;
  202. }
  203. len = g_data_input_stream_read_uint32(s, NULL, &err);
  204. avail = g_buffered_input_stream_get_available(
  205. G_BUFFERED_INPUT_STREAM(s));
  206. if (len > DBUS_VMSTATE_SIZE_LIMIT || len > avail) {
  207. error_report("%s: Invalid vmstate size: %u", __func__, len);
  208. return -1;
  209. }
  210. if (dbus_load_state_proxy(proxy,
  211. g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s),
  212. NULL),
  213. len) < 0) {
  214. error_report("%s: Failed to restore Id '%s'", __func__, id);
  215. return -1;
  216. }
  217. if (!g_seekable_seek(G_SEEKABLE(s), len, G_SEEK_CUR, NULL, &err)) {
  218. goto error;
  219. }
  220. nelem -= 1;
  221. }
  222. return 0;
  223. error:
  224. error_report("%s: Failed to read from stream: %s", __func__, err->message);
  225. return -1;
  226. }
  227. static void
  228. dbus_save_state_proxy(gpointer key,
  229. gpointer value,
  230. gpointer user_data)
  231. {
  232. GDataOutputStream *s = user_data;
  233. const char *id = key;
  234. GDBusProxy *proxy = value;
  235. g_autoptr(GVariant) result = NULL;
  236. g_autoptr(GVariant) child = NULL;
  237. g_autoptr(GError) err = NULL;
  238. const uint8_t *data;
  239. gsize size;
  240. trace_dbus_vmstate_saving(id);
  241. result = g_dbus_proxy_call_sync(proxy, "Save",
  242. NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START,
  243. -1, NULL, &err);
  244. if (!result) {
  245. error_report("%s: Failed to Save: %s", __func__, err->message);
  246. return;
  247. }
  248. child = g_variant_get_child_value(result, 0);
  249. data = g_variant_get_fixed_array(child, &size, sizeof(char));
  250. if (!data) {
  251. error_report("%s: Failed to Save: not a byte array", __func__);
  252. return;
  253. }
  254. if (size > DBUS_VMSTATE_SIZE_LIMIT) {
  255. error_report("%s: Too large vmstate data to save: %zu",
  256. __func__, (size_t)size);
  257. return;
  258. }
  259. if (!g_data_output_stream_put_uint32(s, strlen(id), NULL, &err) ||
  260. !g_data_output_stream_put_string(s, id, NULL, &err) ||
  261. !g_data_output_stream_put_uint32(s, size, NULL, &err) ||
  262. !g_output_stream_write_all(G_OUTPUT_STREAM(s),
  263. data, size, NULL, NULL, &err)) {
  264. error_report("%s: Failed to write to stream: %s",
  265. __func__, err->message);
  266. }
  267. }
  268. static int dbus_vmstate_pre_save(void *opaque)
  269. {
  270. DBusVMState *self = DBUS_VMSTATE(opaque);
  271. g_autoptr(GOutputStream) m = NULL;
  272. g_autoptr(GDataOutputStream) s = NULL;
  273. g_autoptr(GHashTable) proxies = NULL;
  274. g_autoptr(GError) err = NULL;
  275. trace_dbus_vmstate_pre_save();
  276. proxies = dbus_get_proxies(self, &err);
  277. if (!proxies) {
  278. error_report("%s: Failed to get proxies: %s", __func__, err->message);
  279. return -1;
  280. }
  281. m = g_memory_output_stream_new_resizable();
  282. s = g_data_output_stream_new(m);
  283. g_data_output_stream_set_byte_order(s, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
  284. if (!g_data_output_stream_put_uint32(s, g_hash_table_size(proxies),
  285. NULL, &err)) {
  286. error_report("%s: Failed to write to stream: %s",
  287. __func__, err->message);
  288. return -1;
  289. }
  290. g_hash_table_foreach(proxies, dbus_save_state_proxy, s);
  291. if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m))
  292. > UINT32_MAX) {
  293. error_report("%s: DBus vmstate buffer is too large", __func__);
  294. return -1;
  295. }
  296. if (!g_output_stream_close(G_OUTPUT_STREAM(m), NULL, &err)) {
  297. error_report("%s: Failed to close stream: %s", __func__, err->message);
  298. return -1;
  299. }
  300. g_free(self->data);
  301. self->data_size =
  302. g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m));
  303. self->data =
  304. g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m));
  305. return 0;
  306. }
  307. static const VMStateDescription dbus_vmstate = {
  308. .name = TYPE_DBUS_VMSTATE,
  309. .version_id = 0,
  310. .pre_save = dbus_vmstate_pre_save,
  311. .post_load = dbus_vmstate_post_load,
  312. .fields = (VMStateField[]) {
  313. VMSTATE_UINT32(data_size, DBusVMState),
  314. VMSTATE_VBUFFER_ALLOC_UINT32(data, DBusVMState, 0, 0, data_size),
  315. VMSTATE_END_OF_LIST()
  316. }
  317. };
  318. static void
  319. dbus_vmstate_complete(UserCreatable *uc, Error **errp)
  320. {
  321. DBusVMState *self = DBUS_VMSTATE(uc);
  322. g_autoptr(GError) err = NULL;
  323. if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE, NULL)) {
  324. error_setg(errp, "There is already an instance of %s",
  325. TYPE_DBUS_VMSTATE);
  326. return;
  327. }
  328. if (!self->dbus_addr) {
  329. error_setg(errp, QERR_MISSING_PARAMETER, "addr");
  330. return;
  331. }
  332. self->bus = g_dbus_connection_new_for_address_sync(self->dbus_addr,
  333. G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
  334. G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
  335. NULL, NULL, &err);
  336. if (err) {
  337. error_setg(errp, "failed to connect to DBus: '%s'", err->message);
  338. return;
  339. }
  340. if (vmstate_register(VMSTATE_IF(self), VMSTATE_INSTANCE_ID_ANY,
  341. &dbus_vmstate, self) < 0) {
  342. error_setg(errp, "Failed to register vmstate");
  343. }
  344. }
  345. static void
  346. dbus_vmstate_finalize(Object *o)
  347. {
  348. DBusVMState *self = DBUS_VMSTATE(o);
  349. vmstate_unregister(VMSTATE_IF(self), &dbus_vmstate, self);
  350. g_clear_object(&self->bus);
  351. g_free(self->dbus_addr);
  352. g_free(self->id_list);
  353. g_free(self->data);
  354. }
  355. static char *
  356. get_dbus_addr(Object *o, Error **errp)
  357. {
  358. DBusVMState *self = DBUS_VMSTATE(o);
  359. return g_strdup(self->dbus_addr);
  360. }
  361. static void
  362. set_dbus_addr(Object *o, const char *str, Error **errp)
  363. {
  364. DBusVMState *self = DBUS_VMSTATE(o);
  365. g_free(self->dbus_addr);
  366. self->dbus_addr = g_strdup(str);
  367. }
  368. static char *
  369. get_id_list(Object *o, Error **errp)
  370. {
  371. DBusVMState *self = DBUS_VMSTATE(o);
  372. return g_strdup(self->id_list);
  373. }
  374. static void
  375. set_id_list(Object *o, const char *str, Error **errp)
  376. {
  377. DBusVMState *self = DBUS_VMSTATE(o);
  378. g_free(self->id_list);
  379. self->id_list = g_strdup(str);
  380. }
  381. static char *
  382. dbus_vmstate_get_id(VMStateIf *vmif)
  383. {
  384. return g_strdup(TYPE_DBUS_VMSTATE);
  385. }
  386. static void
  387. dbus_vmstate_class_init(ObjectClass *oc, void *data)
  388. {
  389. UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
  390. VMStateIfClass *vc = VMSTATE_IF_CLASS(oc);
  391. ucc->complete = dbus_vmstate_complete;
  392. vc->get_id = dbus_vmstate_get_id;
  393. object_class_property_add_str(oc, "addr",
  394. get_dbus_addr, set_dbus_addr);
  395. object_class_property_add_str(oc, "id-list",
  396. get_id_list, set_id_list);
  397. }
  398. static const TypeInfo dbus_vmstate_info = {
  399. .name = TYPE_DBUS_VMSTATE,
  400. .parent = TYPE_OBJECT,
  401. .instance_size = sizeof(DBusVMState),
  402. .instance_finalize = dbus_vmstate_finalize,
  403. .class_init = dbus_vmstate_class_init,
  404. .interfaces = (InterfaceInfo[]) {
  405. { TYPE_USER_CREATABLE },
  406. { TYPE_VMSTATE_IF },
  407. { }
  408. }
  409. };
  410. static void
  411. register_types(void)
  412. {
  413. type_register_static(&dbus_vmstate_info);
  414. }
  415. type_init(register_types);