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"
22 #include "qom/object.h"
25 #define TYPE_DBUS_VMSTATE "dbus-vmstate"
26 OBJECT_DECLARE_SIMPLE_TYPE(DBusVMState
,
41 static const GDBusPropertyInfo vmstate_property_info
[] = {
42 { -1, (char *) "Id", (char *) "s",
43 G_DBUS_PROPERTY_INFO_FLAGS_READABLE
, NULL
},
46 static const GDBusPropertyInfo
* const vmstate_property_info_pointers
[] = {
47 &vmstate_property_info
[0],
51 static const GDBusInterfaceInfo vmstate1_interface_info
= {
53 (char *) "org.qemu.VMState1",
54 (GDBusMethodInfo
**) NULL
,
55 (GDBusSignalInfo
**) NULL
,
56 (GDBusPropertyInfo
**) &vmstate_property_info_pointers
,
60 #define DBUS_VMSTATE_SIZE_LIMIT (1 * MiB)
63 get_id_list_set(DBusVMState
*self
)
65 g_auto(GStrv
) ids
= NULL
;
66 g_autoptr(GHashTable
) set
= NULL
;
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
]);
80 return g_steal_pointer(&set
);
84 dbus_get_proxies(DBusVMState
*self
, GError
**err
)
86 g_autoptr(GHashTable
) proxies
= NULL
;
87 g_autoptr(GHashTable
) ids
= NULL
;
88 g_auto(GStrv
) names
= NULL
;
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
);
96 names
= qemu_dbus_get_queued_owners(self
->bus
, "org.qemu.VMState1", &error
);
98 g_set_error(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
, "%s",
99 error_get_pretty(error
));
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
;
110 proxy
= g_dbus_proxy_new_sync(self
->bus
, G_DBUS_PROXY_FLAGS_NONE
,
111 (GDBusInterfaceInfo
*) &vmstate1_interface_info
,
113 "/org/qemu/VMState1",
120 result
= g_dbus_proxy_get_cached_property(proxy
, "Id");
122 g_set_error_literal(err
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
123 "VMState Id property is missing.");
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
);
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
);
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
);
147 g_clear_pointer(&result
, g_variant_unref
);
151 g_autofree
char **left
= NULL
;
153 left
= (char **)g_hash_table_get_keys_as_array(ids
, NULL
);
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
);
162 return g_steal_pointer(&proxies
);
166 dbus_load_state_proxy(GDBusProxy
*proxy
, const uint8_t *data
, size_t size
)
168 g_autoptr(GError
) err
= NULL
;
169 g_autoptr(GVariant
) result
= NULL
;
170 g_autoptr(GVariant
) value
= NULL
;
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
,
180 error_report("%s: Failed to Load: %s", __func__
, err
->message
);
187 static int dbus_vmstate_post_load(void *opaque
, int version_id
)
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
;
196 trace_dbus_vmstate_post_load(version_id
);
198 proxies
= dbus_get_proxies(self
, &err
);
200 error_report("%s: Failed to get proxies: %s", __func__
, err
->message
);
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 g_buffered_input_stream_set_buffer_size(G_BUFFERED_INPUT_STREAM(s
),
208 DBUS_VMSTATE_SIZE_LIMIT
);
210 nelem
= g_data_input_stream_read_uint32(s
, NULL
, &err
);
216 GDBusProxy
*proxy
= NULL
;
218 gsize bytes_read
, avail
;
221 len
= g_data_input_stream_read_uint32(s
, NULL
, &err
);
226 error_report("%s: Invalid DBus vmstate proxy name %u",
230 if (!g_input_stream_read_all(G_INPUT_STREAM(s
), id
, len
,
231 &bytes_read
, NULL
, &err
)) {
234 if (bytes_read
!= len
) {
235 error_report("%s: Short read", __func__
);
240 trace_dbus_vmstate_loading(id
);
242 proxy
= g_hash_table_lookup(proxies
, id
);
244 error_report("%s: Failed to find proxy Id '%s'", __func__
, id
);
248 len
= g_data_input_stream_read_uint32(s
, NULL
, &err
);
249 if (len
> DBUS_VMSTATE_SIZE_LIMIT
) {
250 error_report("%s: Invalid vmstate size: %u", __func__
, len
);
254 g_buffered_input_stream_fill(G_BUFFERED_INPUT_STREAM(s
), len
, NULL
,
260 avail
= g_buffered_input_stream_get_available(
261 G_BUFFERED_INPUT_STREAM(s
));
263 error_report("%s: Not enough data available to load for Id: '%s'. "
264 "Available data size: %zu, Actual vmstate size: %u",
265 __func__
, id
, avail
, len
);
269 if (dbus_load_state_proxy(proxy
,
270 g_buffered_input_stream_peek_buffer(G_BUFFERED_INPUT_STREAM(s
),
273 error_report("%s: Failed to restore Id '%s'", __func__
, id
);
277 if (!g_seekable_seek(G_SEEKABLE(s
), len
, G_SEEK_CUR
, NULL
, &err
)) {
287 error_report("%s: Failed to read from stream: %s", __func__
, err
->message
);
292 dbus_save_state_proxy(gpointer key
,
296 GDataOutputStream
*s
= user_data
;
297 const char *id
= key
;
298 GDBusProxy
*proxy
= value
;
299 g_autoptr(GVariant
) result
= NULL
;
300 g_autoptr(GVariant
) child
= NULL
;
301 g_autoptr(GError
) err
= NULL
;
305 trace_dbus_vmstate_saving(id
);
307 result
= g_dbus_proxy_call_sync(proxy
, "Save",
308 NULL
, G_DBUS_CALL_FLAGS_NO_AUTO_START
,
311 error_report("%s: Failed to Save: %s", __func__
, err
->message
);
315 child
= g_variant_get_child_value(result
, 0);
316 data
= g_variant_get_fixed_array(child
, &size
, sizeof(char));
318 error_report("%s: Failed to Save: not a byte array", __func__
);
321 if (size
> DBUS_VMSTATE_SIZE_LIMIT
) {
322 error_report("%s: Too large vmstate data to save: %zu",
323 __func__
, (size_t)size
);
327 if (!g_data_output_stream_put_uint32(s
, strlen(id
), NULL
, &err
) ||
328 !g_data_output_stream_put_string(s
, id
, NULL
, &err
) ||
329 !g_data_output_stream_put_uint32(s
, size
, NULL
, &err
) ||
330 !g_output_stream_write_all(G_OUTPUT_STREAM(s
),
331 data
, size
, NULL
, NULL
, &err
)) {
332 error_report("%s: Failed to write to stream: %s",
333 __func__
, err
->message
);
337 static int dbus_vmstate_pre_save(void *opaque
)
339 DBusVMState
*self
= DBUS_VMSTATE(opaque
);
340 g_autoptr(GOutputStream
) m
= NULL
;
341 g_autoptr(GDataOutputStream
) s
= NULL
;
342 g_autoptr(GHashTable
) proxies
= NULL
;
343 g_autoptr(GError
) err
= NULL
;
345 trace_dbus_vmstate_pre_save();
347 proxies
= dbus_get_proxies(self
, &err
);
349 error_report("%s: Failed to get proxies: %s", __func__
, err
->message
);
353 m
= g_memory_output_stream_new_resizable();
354 s
= g_data_output_stream_new(m
);
355 g_data_output_stream_set_byte_order(s
, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN
);
357 if (!g_data_output_stream_put_uint32(s
, g_hash_table_size(proxies
),
359 error_report("%s: Failed to write to stream: %s",
360 __func__
, err
->message
);
364 g_hash_table_foreach(proxies
, dbus_save_state_proxy
, s
);
366 if (g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m
))
368 error_report("%s: DBus vmstate buffer is too large", __func__
);
372 if (!g_output_stream_close(G_OUTPUT_STREAM(m
), NULL
, &err
)) {
373 error_report("%s: Failed to close stream: %s", __func__
, err
->message
);
379 g_memory_output_stream_get_size(G_MEMORY_OUTPUT_STREAM(m
));
381 g_memory_output_stream_steal_data(G_MEMORY_OUTPUT_STREAM(m
));
386 static const VMStateDescription dbus_vmstate
= {
387 .name
= TYPE_DBUS_VMSTATE
,
389 .pre_save
= dbus_vmstate_pre_save
,
390 .post_load
= dbus_vmstate_post_load
,
391 .fields
= (VMStateField
[]) {
392 VMSTATE_UINT32(data_size
, DBusVMState
),
393 VMSTATE_VBUFFER_ALLOC_UINT32(data
, DBusVMState
, 0, 0, data_size
),
394 VMSTATE_END_OF_LIST()
399 dbus_vmstate_complete(UserCreatable
*uc
, Error
**errp
)
401 DBusVMState
*self
= DBUS_VMSTATE(uc
);
402 g_autoptr(GError
) err
= NULL
;
404 if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE
, NULL
)) {
405 error_setg(errp
, "There is already an instance of %s",
410 if (!self
->dbus_addr
) {
411 error_setg(errp
, QERR_MISSING_PARAMETER
, "addr");
415 self
->bus
= g_dbus_connection_new_for_address_sync(self
->dbus_addr
,
416 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT
|
417 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION
,
420 error_setg(errp
, "failed to connect to DBus: '%s'", err
->message
);
424 if (vmstate_register(VMSTATE_IF(self
), VMSTATE_INSTANCE_ID_ANY
,
425 &dbus_vmstate
, self
) < 0) {
426 error_setg(errp
, "Failed to register vmstate");
431 dbus_vmstate_finalize(Object
*o
)
433 DBusVMState
*self
= DBUS_VMSTATE(o
);
435 vmstate_unregister(VMSTATE_IF(self
), &dbus_vmstate
, self
);
437 g_clear_object(&self
->bus
);
438 g_free(self
->dbus_addr
);
439 g_free(self
->id_list
);
444 get_dbus_addr(Object
*o
, Error
**errp
)
446 DBusVMState
*self
= DBUS_VMSTATE(o
);
448 return g_strdup(self
->dbus_addr
);
452 set_dbus_addr(Object
*o
, const char *str
, Error
**errp
)
454 DBusVMState
*self
= DBUS_VMSTATE(o
);
456 g_free(self
->dbus_addr
);
457 self
->dbus_addr
= g_strdup(str
);
461 get_id_list(Object
*o
, Error
**errp
)
463 DBusVMState
*self
= DBUS_VMSTATE(o
);
465 return g_strdup(self
->id_list
);
469 set_id_list(Object
*o
, const char *str
, Error
**errp
)
471 DBusVMState
*self
= DBUS_VMSTATE(o
);
473 g_free(self
->id_list
);
474 self
->id_list
= g_strdup(str
);
478 dbus_vmstate_get_id(VMStateIf
*vmif
)
480 return g_strdup(TYPE_DBUS_VMSTATE
);
484 dbus_vmstate_class_init(ObjectClass
*oc
, void *data
)
486 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
487 VMStateIfClass
*vc
= VMSTATE_IF_CLASS(oc
);
489 ucc
->complete
= dbus_vmstate_complete
;
490 vc
->get_id
= dbus_vmstate_get_id
;
492 object_class_property_add_str(oc
, "addr",
493 get_dbus_addr
, set_dbus_addr
);
494 object_class_property_add_str(oc
, "id-list",
495 get_id_list
, set_id_list
);
498 static const TypeInfo dbus_vmstate_info
= {
499 .name
= TYPE_DBUS_VMSTATE
,
500 .parent
= TYPE_OBJECT
,
501 .instance_size
= sizeof(DBusVMState
),
502 .instance_finalize
= dbus_vmstate_finalize
,
503 .class_init
= dbus_vmstate_class_init
,
504 .interfaces
= (InterfaceInfo
[]) {
505 { TYPE_USER_CREATABLE
},
514 type_register_static(&dbus_vmstate_info
);
517 type_init(register_types
);