4 * Copyright (C) 2019 Red Hat Inc
7 * Marc-André Lureau <marcandre.lureau@redhat.com>
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.
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"
23 typedef struct DBusVMState DBusVMState
;
24 typedef struct DBusVMStateClass DBusVMStateClass
;
26 #define TYPE_DBUS_VMSTATE "dbus-vmstate"
27 #define DBUS_VMSTATE(obj) \
28 OBJECT_CHECK(DBusVMState, (obj), TYPE_DBUS_VMSTATE)
29 #define DBUS_VMSTATE_GET_CLASS(obj) \
30 OBJECT_GET_CLASS(DBusVMStateClass, (obj), TYPE_DBUS_VMSTATE)
31 #define DBUS_VMSTATE_CLASS(klass) \
32 OBJECT_CLASS_CHECK(DBusVMStateClass, (klass), TYPE_DBUS_VMSTATE)
34 struct DBusVMStateClass
{
35 ObjectClass parent_class
;
49 static const GDBusPropertyInfo vmstate_property_info
[] = {
50 { -1, (char *) "Id", (char *) "s",
51 G_DBUS_PROPERTY_INFO_FLAGS_READABLE
, NULL
},
54 static const GDBusPropertyInfo
* const vmstate_property_info_pointers
[] = {
55 &vmstate_property_info
[0],
59 static const GDBusInterfaceInfo vmstate1_interface_info
= {
61 (char *) "org.qemu.VMState1",
62 (GDBusMethodInfo
**) NULL
,
63 (GDBusSignalInfo
**) NULL
,
64 (GDBusPropertyInfo
**) &vmstate_property_info_pointers
,
68 #define DBUS_VMSTATE_SIZE_LIMIT (1 * MiB)
71 get_id_list_set(DBusVMState
*self
)
73 g_auto(GStrv
) ids
= NULL
;
74 g_autoptr(GHashTable
) set
= NULL
;
81 ids
= g_strsplit(self
->id_list
, ",", -1);
82 set
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, NULL
);
83 for (i
= 0; ids
[i
]; i
++) {
84 g_hash_table_add(set
, ids
[i
]);
88 return g_steal_pointer(&set
);
92 dbus_get_proxies(DBusVMState
*self
, GError
**err
)
94 g_autoptr(GHashTable
) proxies
= NULL
;
95 g_autoptr(GHashTable
) ids
= NULL
;
96 g_auto(GStrv
) names
= NULL
;
100 ids
= get_id_list_set(self
);
101 proxies
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
102 g_free
, g_object_unref
);
104 names
= qemu_dbus_get_queued_owners(self
->bus
, "org.qemu.VMState1", &error
);
106 g_set_error(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
, "%s",
107 error_get_pretty(error
));
112 for (i
= 0; names
[i
]; i
++) {
113 g_autoptr(GDBusProxy
) proxy
= NULL
;
114 g_autoptr(GVariant
) result
= NULL
;
115 g_autofree
char *id
= NULL
;
118 proxy
= g_dbus_proxy_new_sync(self
->bus
, G_DBUS_PROXY_FLAGS_NONE
,
119 (GDBusInterfaceInfo
*) &vmstate1_interface_info
,
121 "/org/qemu/VMState1",
128 result
= g_dbus_proxy_get_cached_property(proxy
, "Id");
130 g_set_error_literal(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
131 "VMState Id property is missing.");
135 id
= g_variant_dup_string(result
, &size
);
136 if (ids
&& !g_hash_table_remove(ids
, id
)) {
137 g_clear_pointer(&id
, g_free
);
138 g_clear_object(&proxy
);
141 if (size
== 0 || size
>= 256) {
142 g_set_error(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
143 "VMState Id '%s' is invalid.", id
);
147 if (!g_hash_table_insert(proxies
, id
, proxy
)) {
148 g_set_error(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
149 "Duplicated VMState Id '%s'", id
);
155 g_clear_pointer(&result
, g_variant_unref
);
159 g_autofree
char **left
= NULL
;
161 left
= (char **)g_hash_table_get_keys_as_array(ids
, NULL
);
163 g_autofree
char *leftids
= g_strjoinv(",", left
);
164 g_set_error(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
165 "Required VMState Id are missing: %s", leftids
);
170 return g_steal_pointer(&proxies
);
174 dbus_load_state_proxy(GDBusProxy
*proxy
, const uint8_t *data
, size_t size
)
176 g_autoptr(GError
) err
= NULL
;
177 g_autoptr(GVariant
) result
= NULL
;
178 g_autoptr(GVariant
) value
= NULL
;
180 value
= g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE
,
181 data
, size
, sizeof(char));
182 result
= g_dbus_proxy_call_sync(proxy
, "Load",
183 g_variant_new("(@ay)",
184 g_steal_pointer(&value
)),
185 G_DBUS_CALL_FLAGS_NO_AUTO_START
,
188 error_report("%s: Failed to Load: %s", __func__
, err
->message
);
195 static int dbus_vmstate_post_load(void *opaque
, int version_id
)
197 DBusVMState
*self
= DBUS_VMSTATE(opaque
);
198 g_autoptr(GInputStream
) m
= NULL
;
199 g_autoptr(GDataInputStream
) s
= NULL
;
200 g_autoptr(GError
) err
= NULL
;
201 g_autoptr(GHashTable
) proxies
= NULL
;
204 trace_dbus_vmstate_post_load(version_id
);
206 proxies
= dbus_get_proxies(self
, &err
);
208 error_report("%s: Failed to get proxies: %s", __func__
, err
->message
);
212 m
= g_memory_input_stream_new_from_data(self
->data
, self
->data_size
, NULL
);
213 s
= g_data_input_stream_new(m
);
214 g_data_input_stream_set_byte_order(s
, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
);
216 nelem
= g_data_input_stream_read_uint32(s
, NULL
, &err
);
222 GDBusProxy
*proxy
= NULL
;
224 gsize bytes_read
, avail
;
227 len
= g_data_input_stream_read_uint32(s
, NULL
, &err
);
232 error_report("%s: Invalid DBus vmstate proxy name %u",
236 if (!g_input_stream_read_all(G_INPUT_STREAM(s
), id
, len
,
237 &bytes_read
, NULL
, &err
)) {
240 g_return_val_if_fail(bytes_read
== len
, -1);
243 trace_dbus_vmstate_loading(id
);
245 proxy
= g_hash_table_lookup(proxies
, id
);
247 error_report("%s: Failed to find proxy Id '%s'", __func__
, id
);
251 len
= g_data_input_stream_read_uint32(s
, NULL
, &err
);
252 avail
= g_buffered_input_stream_get_available(
253 G_BUFFERED_INPUT_STREAM(s
));
255 if (len
> DBUS_VMSTATE_SIZE_LIMIT
|| len
> avail
) {
256 error_report("%s: Invalid vmstate size: %u", __func__
, len
);
260 if (dbus_load_state_proxy(proxy
,
261 g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s
),
264 error_report("%s: Failed to restore Id '%s'", __func__
, id
);
268 if (!g_seekable_seek(G_SEEKABLE(s
), len
, G_SEEK_CUR
, NULL
, &err
)) {
278 error_report("%s: Failed to read from stream: %s", __func__
, err
->message
);
283 dbus_save_state_proxy(gpointer key
,
287 GDataOutputStream
*s
= user_data
;
288 const char *id
= key
;
289 GDBusProxy
*proxy
= value
;
290 g_autoptr(GVariant
) result
= NULL
;
291 g_autoptr(GVariant
) child
= NULL
;
292 g_autoptr(GError
) err
= NULL
;
296 trace_dbus_vmstate_saving(id
);
298 result
= g_dbus_proxy_call_sync(proxy
, "Save",
299 NULL
, G_DBUS_CALL_FLAGS_NO_AUTO_START
,
302 error_report("%s: Failed to Save: %s", __func__
, err
->message
);
306 child
= g_variant_get_child_value(result
, 0);
307 data
= g_variant_get_fixed_array(child
, &size
, sizeof(char));
309 error_report("%s: Failed to Save: not a byte array", __func__
);
312 if (size
> DBUS_VMSTATE_SIZE_LIMIT
) {
313 error_report("%s: Too large vmstate data to save: %zu",
314 __func__
, (size_t)size
);
318 if (!g_data_output_stream_put_uint32(s
, strlen(id
), NULL
, &err
) ||
319 !g_data_output_stream_put_string(s
, id
, NULL
, &err
) ||
320 !g_data_output_stream_put_uint32(s
, size
, NULL
, &err
) ||
321 !g_output_stream_write_all(G_OUTPUT_STREAM(s
),
322 data
, size
, NULL
, NULL
, &err
)) {
323 error_report("%s: Failed to write to stream: %s",
324 __func__
, err
->message
);
328 static int dbus_vmstate_pre_save(void *opaque
)
330 DBusVMState
*self
= DBUS_VMSTATE(opaque
);
331 g_autoptr(GOutputStream
) m
= NULL
;
332 g_autoptr(GDataOutputStream
) s
= NULL
;
333 g_autoptr(GHashTable
) proxies
= NULL
;
334 g_autoptr(GError
) err
= NULL
;
336 trace_dbus_vmstate_pre_save();
338 proxies
= dbus_get_proxies(self
, &err
);
340 error_report("%s: Failed to get proxies: %s", __func__
, err
->message
);
344 m
= g_memory_output_stream_new_resizable();
345 s
= g_data_output_stream_new(m
);
346 g_data_output_stream_set_byte_order(s
, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
);
348 if (!g_data_output_stream_put_uint32(s
, g_hash_table_size(proxies
),
350 error_report("%s: Failed to write to stream: %s",
351 __func__
, err
->message
);
355 g_hash_table_foreach(proxies
, dbus_save_state_proxy
, s
);
357 if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m
))
359 error_report("%s: DBus vmstate buffer is too large", __func__
);
363 if (!g_output_stream_close(G_OUTPUT_STREAM(m
), NULL
, &err
)) {
364 error_report("%s: Failed to close stream: %s", __func__
, err
->message
);
370 g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m
));
372 g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m
));
377 static const VMStateDescription dbus_vmstate
= {
378 .name
= TYPE_DBUS_VMSTATE
,
380 .pre_save
= dbus_vmstate_pre_save
,
381 .post_load
= dbus_vmstate_post_load
,
382 .fields
= (VMStateField
[]) {
383 VMSTATE_UINT32(data_size
, DBusVMState
),
384 VMSTATE_VBUFFER_ALLOC_UINT32(data
, DBusVMState
, 0, 0, data_size
),
385 VMSTATE_END_OF_LIST()
390 dbus_vmstate_complete(UserCreatable
*uc
, Error
**errp
)
392 DBusVMState
*self
= DBUS_VMSTATE(uc
);
393 g_autoptr(GError
) err
= NULL
;
395 if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE
, NULL
)) {
396 error_setg(errp
, "There is already an instance of %s",
401 if (!self
->dbus_addr
) {
402 error_setg(errp
, QERR_MISSING_PARAMETER
, "addr");
406 self
->bus
= g_dbus_connection_new_for_address_sync(self
->dbus_addr
,
407 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
|
408 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
,
411 error_setg(errp
, "failed to connect to DBus: '%s'", err
->message
);
415 if (vmstate_register(VMSTATE_IF(self
), -1, &dbus_vmstate
, self
) < 0) {
416 error_setg(errp
, "Failed to register vmstate");
421 dbus_vmstate_finalize(Object
*o
)
423 DBusVMState
*self
= DBUS_VMSTATE(o
);
425 vmstate_unregister(VMSTATE_IF(self
), &dbus_vmstate
, self
);
427 g_clear_object(&self
->bus
);
428 g_free(self
->dbus_addr
);
429 g_free(self
->id_list
);
434 get_dbus_addr(Object
*o
, Error
**errp
)
436 DBusVMState
*self
= DBUS_VMSTATE(o
);
438 return g_strdup(self
->dbus_addr
);
442 set_dbus_addr(Object
*o
, const char *str
, Error
**errp
)
444 DBusVMState
*self
= DBUS_VMSTATE(o
);
446 g_free(self
->dbus_addr
);
447 self
->dbus_addr
= g_strdup(str
);
451 get_id_list(Object
*o
, Error
**errp
)
453 DBusVMState
*self
= DBUS_VMSTATE(o
);
455 return g_strdup(self
->id_list
);
459 set_id_list(Object
*o
, const char *str
, Error
**errp
)
461 DBusVMState
*self
= DBUS_VMSTATE(o
);
463 g_free(self
->id_list
);
464 self
->id_list
= g_strdup(str
);
468 dbus_vmstate_get_id(VMStateIf
*vmif
)
470 return g_strdup(TYPE_DBUS_VMSTATE
);
474 dbus_vmstate_class_init(ObjectClass
*oc
, void *data
)
476 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
477 VMStateIfClass
*vc
= VMSTATE_IF_CLASS(oc
);
479 ucc
->complete
= dbus_vmstate_complete
;
480 vc
->get_id
= dbus_vmstate_get_id
;
482 object_class_property_add_str(oc
, "addr",
483 get_dbus_addr
, set_dbus_addr
,
485 object_class_property_add_str(oc
, "id-list",
486 get_id_list
, set_id_list
,
490 static const TypeInfo dbus_vmstate_info
= {
491 .name
= TYPE_DBUS_VMSTATE
,
492 .parent
= TYPE_OBJECT
,
493 .instance_size
= sizeof(DBusVMState
),
494 .instance_finalize
= dbus_vmstate_finalize
,
495 .class_size
= sizeof(DBusVMStateClass
),
496 .class_init
= dbus_vmstate_class_init
,
497 .interfaces
= (InterfaceInfo
[]) {
498 { TYPE_USER_CREATABLE
},
507 type_register_static(&dbus_vmstate_info
);
510 type_init(register_types
);