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