[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 3/3] Add dbus-vmstate object
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH 3/3] Add dbus-vmstate object |
Date: |
Mon, 8 Jul 2019 11:24:37 +0400 |
When instanciated, this object will connect to the given D-Bus
bus. During migration, it will take the data from org.qemu.VMState1
instances.
See documentation for further details.
Signed-off-by: Marc-André Lureau <address@hidden>
---
MAINTAINERS | 6 +
backends/Makefile.objs | 4 +
backends/dbus-vmstate.c | 497 ++++++++++++++++++++++++++++++++++
configure | 7 +
docs/interop/dbus-vmstate.rst | 64 +++++
docs/interop/index.rst | 1 +
tests/Makefile.include | 18 +-
tests/dbus-vmstate-test.c | 387 ++++++++++++++++++++++++++
tests/dbus-vmstate1.xml | 12 +
9 files changed, 995 insertions(+), 1 deletion(-)
create mode 100644 backends/dbus-vmstate.c
create mode 100644 docs/interop/dbus-vmstate.rst
create mode 100644 tests/dbus-vmstate-test.c
create mode 100644 tests/dbus-vmstate1.xml
diff --git a/MAINTAINERS b/MAINTAINERS
index 2cce50287a..9fa592f1b3 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2144,6 +2144,12 @@ F: tests/migration-test.c
F: docs/devel/migration.rst
F: qapi/migration.json
+DBus VMState
+M: Marc-André Lureau <address@hidden>
+S: Maintained
+F: backends/dbus-vmstate.c
+F: tests/dbus-vmstate*
+
Seccomp
M: Eduardo Otubo <address@hidden>
S: Supported
diff --git a/backends/Makefile.objs b/backends/Makefile.objs
index 981e8e122f..dbbe12b225 100644
--- a/backends/Makefile.objs
+++ b/backends/Makefile.objs
@@ -17,3 +17,7 @@ endif
common-obj-$(call land,$(CONFIG_VHOST_USER),$(CONFIG_VIRTIO)) += vhost-user.o
common-obj-$(CONFIG_LINUX) += hostmem-memfd.o
+
+common-obj-$(CONFIG_GIO) += dbus-vmstate.o
+dbus-vmstate.o-cflags = $(GIO_CFLAGS)
+dbus-vmstate.o-libs = $(GIO_LIBS)
diff --git a/backends/dbus-vmstate.c b/backends/dbus-vmstate.c
new file mode 100644
index 0000000000..f9117d9fce
--- /dev/null
+++ b/backends/dbus-vmstate.c
@@ -0,0 +1,497 @@
+/*
+ * QEMU dbus-vmstate
+ *
+ * Copyright (C) 2019 Red Hat Inc
+ *
+ * Authors:
+ * Marc-André Lureau <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/error-report.h"
+#include "qapi/error.h"
+#include "qom/object_interfaces.h"
+#include "qapi/qmp/qerror.h"
+#include "migration/register.h"
+#include "migration/qemu-file-types.h"
+#include <gio/gio.h>
+
+typedef struct DBusVMState DBusVMState;
+typedef struct DBusVMStateClass DBusVMStateClass;
+
+#define TYPE_DBUS_VMSTATE "dbus-vmstate"
+#define DBUS_VMSTATE(obj) \
+ OBJECT_CHECK(DBusVMState, (obj), TYPE_DBUS_VMSTATE)
+#define DBUS_VMSTATE_GET_CLASS(obj) \
+ OBJECT_GET_CLASS(DBusVMStateClass, (obj), TYPE_DBUS_VMSTATE)
+#define DBUS_VMSTATE_CLASS(klass) \
+ OBJECT_CLASS_CHECK(DBusVMStateClass, (klass), TYPE_DBUS_VMSTATE)
+
+struct DBusVMStateClass {
+ ObjectClass parent_class;
+};
+
+struct DBusVMState {
+ Object parent;
+
+ GDBusConnection *bus;
+ char *dbus_addr;
+ char *id_list;
+};
+
+static const GDBusPropertyInfo vmstate_property_info[] = {
+ { -1, (char *) "Id", (char *) "s",
+ G_DBUS_PROPERTY_INFO_FLAGS_READABLE, NULL },
+};
+
+static const GDBusPropertyInfo * const vmstate_property_info_pointers[] = {
+ &vmstate_property_info[0],
+ NULL
+};
+
+static const GDBusInterfaceInfo vmstate1_interface_info = {
+ -1,
+ (char *) "org.qemu.VMState1",
+ (GDBusMethodInfo **) NULL,
+ (GDBusSignalInfo **) NULL,
+ (GDBusPropertyInfo **) &vmstate_property_info_pointers,
+ NULL,
+};
+
+#define DBUS_VMSTATE_SIZE_LIMIT (1 << 20) /* 1mb */
+#define DBUS_VMSTATE_SECTION 0x00
+#define DBUS_VMSTATE_EOF 0xff
+
+
+static char **
+dbus_get_vmstate1_names(DBusVMState *self, GError **err)
+{
+ char **names = NULL;
+ GDBusProxy *proxy;
+ GVariant *result = NULL, *child = NULL;
+
+ proxy = g_dbus_proxy_new_sync(self->bus, G_DBUS_PROXY_FLAGS_NONE, NULL,
+ "org.freedesktop.DBus",
+ "/org/freedesktop/DBus",
+ "org.freedesktop.DBus",
+ NULL, err);
+ if (!proxy) {
+ goto end;
+ }
+
+ result = g_dbus_proxy_call_sync(proxy, "ListQueuedOwners",
+ g_variant_new("(s)", "org.qemu.VMState1"),
+ G_DBUS_CALL_FLAGS_NO_AUTO_START,
+ -1, NULL, err);
+ if (!result) {
+ goto end;
+ }
+
+ child = g_variant_get_child_value(result, 0);
+ names = g_variant_dup_strv(child, NULL);
+
+end:
+ g_clear_pointer(&child, g_variant_unref);
+ g_clear_pointer(&result, g_variant_unref);
+ g_clear_object(&proxy);
+ return names;
+}
+
+static GHashTable *
+get_id_list_set(DBusVMState *self)
+{
+ char **ids;
+ GHashTable *set;
+ int i;
+
+ if (!self->id_list) {
+ return NULL;
+ }
+
+ ids = g_strsplit(self->id_list, ",", -1);
+ set = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+ for (i = 0; ids[i]; i++) {
+ g_hash_table_add(set, ids[i]);
+ ids[i] = NULL;
+ }
+
+ g_strfreev(ids);
+ return set;
+}
+
+static GHashTable *
+dbus_get_proxies(DBusVMState *self, GError **err)
+{
+ GError *local_err = NULL;
+ GHashTable *proxies = NULL;
+ GVariant *result = NULL;
+ char **names = NULL;
+ char **left = NULL;
+ GHashTable *ids = get_id_list_set(self);
+ size_t i;
+
+ proxies = g_hash_table_new_full(g_str_hash, g_str_equal,
+ g_free, g_object_unref);
+
+ names = dbus_get_vmstate1_names(self, &local_err);
+ if (!names) {
+ if (g_error_matches(local_err,
+ G_DBUS_ERROR, G_DBUS_ERROR_NAME_HAS_NO_OWNER)) {
+ return proxies;
+ }
+ g_propagate_error(err, local_err);
+ goto err;
+ }
+
+ for (i = 0; names[i]; i++) {
+ GDBusProxy *proxy;
+ char *id;
+ size_t size;
+
+ proxy = g_dbus_proxy_new_sync(self->bus, G_DBUS_PROXY_FLAGS_NONE,
+ (GDBusInterfaceInfo *) &vmstate1_interface_info,
+ names[i],
+ "/org/qemu/VMState1",
+ "org.qemu.VMState1",
+ NULL, err);
+ if (!proxy) {
+ goto err;
+ }
+
+ result = g_dbus_proxy_get_cached_property(proxy, "Id");
+ if (!result) {
+ g_set_error_literal(err, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "VMState Id property is missing.");
+ g_clear_object(&proxy);
+ goto err;
+ }
+
+ id = g_variant_dup_string(result, &size);
+ if (ids && !g_hash_table_remove(ids, id)) {
+ g_free(id);
+ g_clear_object(&proxy);
+ continue;
+ }
+ if (size == 0 || size >= 256) {
+ g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "VMState Id '%s' is invalid.", id);
+ g_free(id);
+ g_clear_object(&proxy);
+ goto err;
+ }
+
+ if (!g_hash_table_insert(proxies, id, proxy)) {
+ g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Duplicated VMState Id '%s'", id);
+ goto err;
+ }
+
+ g_clear_pointer(&result, g_variant_unref);
+ }
+
+ if (ids) {
+ left = (char **)g_hash_table_get_keys_as_array(ids, NULL);
+ if (*left) {
+ char *leftids = g_strjoinv(",", left);
+ g_set_error(err, G_IO_ERROR, G_IO_ERROR_FAILED,
+ "Required VMState Id are missing: %s", leftids);
+ g_free(leftids);
+ goto err;
+ }
+ g_free(left);
+ }
+
+ g_clear_pointer(&ids, g_hash_table_unref);
+ g_strfreev(names);
+ return proxies;
+
+err:
+ g_free(left);
+ g_clear_pointer(&proxies, g_hash_table_unref);
+ g_clear_pointer(&result, g_variant_unref);
+ g_clear_pointer(&ids, g_hash_table_unref);
+ g_strfreev(names);
+ return NULL;
+}
+
+static int
+dbus_load_state_proxy(GDBusProxy *proxy, const uint8_t *data, size_t size)
+{
+ GError *err = NULL;
+ GVariant *value, *result;
+ int ret = -1;
+
+ value = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
+ data, size, sizeof(char));
+ result = g_dbus_proxy_call_sync(proxy, "Load",
+ g_variant_new("(@ay)", value),
+ G_DBUS_CALL_FLAGS_NO_AUTO_START,
+ -1, NULL, &err);
+ if (!result) {
+ error_report("Failed to Load: %s", err->message);
+ goto end;
+ }
+
+ ret = 0;
+
+end:
+ g_clear_pointer(&result, g_variant_unref);
+ g_clear_error(&err);
+ return ret;
+}
+
+static int
+dbus_load_state(QEMUFile *f, void *opaque, int version_id)
+{
+ DBusVMState *self = DBUS_VMSTATE(opaque);
+ GError *err = NULL;
+ GHashTable *proxies = NULL;
+ uint8_t *data = NULL;
+ int ret = -1;
+
+ proxies = dbus_get_proxies(self, &err);
+ if (!proxies) {
+ error_report("Failed to get proxies: %s", err->message);
+ goto end;
+ }
+
+ while (qemu_get_byte(f) != DBUS_VMSTATE_EOF) {
+ GDBusProxy *proxy = NULL;
+ char id[256];
+ unsigned int size;
+
+ if (qemu_get_counted_string(f, id) == 0) {
+ error_report("Invalid vmstate Id");
+ goto end;
+ }
+
+ proxy = g_hash_table_lookup(proxies, id);
+ if (!proxy) {
+ error_report("Failed to find proxy Id '%s'", id);
+ goto end;
+ }
+
+ size = qemu_get_be32(f);
+ if (size > DBUS_VMSTATE_SIZE_LIMIT) {
+ error_report("Invalid vmstate size: %u", size);
+ goto end;
+ }
+
+ data = g_malloc(size);
+ if (qemu_get_buffer(f, data, size) != size) {
+ error_report("Failed to read %u bytes", size);
+ goto end;
+ }
+
+ if (dbus_load_state_proxy(proxy, data, size) < 0) {
+ error_report("Failed to restore Id '%s'", id);
+ goto end;
+ }
+
+ g_clear_pointer(&data, g_free);
+ g_hash_table_remove(proxies, id);
+ }
+
+ if (g_hash_table_size(proxies) != 0) {
+ error_report("Missing DBus states from migration stream.");
+ goto end;
+ }
+
+ ret = 0;
+
+end:
+ g_clear_pointer(&proxies, g_hash_table_unref);
+ g_clear_pointer(&data, g_free);
+ g_clear_error(&err);
+ return ret;
+}
+
+static void
+dbus_save_state_proxy(gpointer key,
+ gpointer value,
+ gpointer user_data)
+{
+ QEMUFile *f = user_data;
+ const char *id = key;
+ GDBusProxy *proxy = value;
+ GVariant *result = NULL, *child = NULL;
+ const uint8_t *data;
+ size_t size;
+ GError *err = NULL;
+
+ result = g_dbus_proxy_call_sync(proxy, "Save",
+ NULL, G_DBUS_CALL_FLAGS_NO_AUTO_START,
+ -1, NULL, &err);
+ if (!result) {
+ error_report("Failed to Save: %s", err->message);
+ g_clear_error(&err);
+ goto end;
+ }
+
+ child = g_variant_get_child_value(result, 0);
+ data = g_variant_get_fixed_array(child, &size, sizeof(char));
+ if (!data) {
+ error_report("Failed to Save: not a byte array");
+ goto end;
+ }
+ if (size > DBUS_VMSTATE_SIZE_LIMIT) {
+ error_report("Too much vmstate data to save: %lu", size);
+ goto end;
+ }
+
+ qemu_put_byte(f, DBUS_VMSTATE_SECTION);
+ qemu_put_counted_string(f, id);
+ qemu_put_be32(f, size);
+ qemu_put_buffer(f, data, size);
+
+end:
+ g_clear_pointer(&child, g_variant_unref);
+ g_clear_pointer(&result, g_variant_unref);
+}
+
+static void
+dbus_save_state(QEMUFile *f, void *opaque)
+{
+ DBusVMState *self = DBUS_VMSTATE(opaque);
+ GHashTable *proxies;
+ GError *err = NULL;
+
+ proxies = dbus_get_proxies(self, &err);
+ if (!proxies) {
+ error_report("Failed to get proxies: %s", err->message);
+ goto end;
+ }
+
+ /* TODO: how to report an error and cancel? */
+ g_hash_table_foreach(proxies, dbus_save_state_proxy, f);
+ qemu_put_byte(f, DBUS_VMSTATE_EOF);
+
+end:
+ g_clear_error(&err);
+ g_clear_pointer(&proxies, g_hash_table_unref);
+}
+
+static const SaveVMHandlers savevm_handlers = {
+ .save_state = dbus_save_state,
+ .load_state = dbus_load_state,
+};
+
+static void
+dbus_vmstate_complete(UserCreatable *uc, Error **errp)
+{
+ DBusVMState *self = DBUS_VMSTATE(uc);
+ GError *err = NULL;
+ GDBusConnection *bus;
+
+ if (!object_resolve_path_type("", TYPE_DBUS_VMSTATE, NULL)) {
+ error_setg(errp, "There is already an instance of %s",
+ TYPE_DBUS_VMSTATE);
+ return;
+ }
+
+ if (!self->dbus_addr) {
+ error_setg(errp, QERR_MISSING_PARAMETER, "addr");
+ return;
+ }
+
+ if (register_savevm_live(NULL, TYPE_DBUS_VMSTATE, 0, 0,
+ &savevm_handlers, self) < 0) {
+ error_setg(errp, "Failed to register savevm handler");
+ return;
+ }
+
+ bus = g_dbus_connection_new_for_address_sync(self->dbus_addr,
+ G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
+ G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
+ NULL, NULL, &err);
+ if (err) {
+ error_setg(errp, "failed to connect to DBus: '%s'", err->message);
+ g_clear_error(&err);
+ }
+
+ self->bus = bus;
+}
+
+static void
+dbus_vmstate_finalize(Object *o)
+{
+ DBusVMState *self = DBUS_VMSTATE(o);
+
+ unregister_savevm(NULL, TYPE_DBUS_VMSTATE, self);
+ g_clear_object(&self->bus);
+ g_free(self->dbus_addr);
+ g_free(self->id_list);
+}
+
+static char *
+get_dbus_addr(Object *o, Error **errp)
+{
+ DBusVMState *self = DBUS_VMSTATE(o);
+
+ return g_strdup(self->dbus_addr);
+}
+
+static void
+set_dbus_addr(Object *o, const char *str, Error **errp)
+{
+ DBusVMState *self = DBUS_VMSTATE(o);
+
+ g_free(self->dbus_addr);
+ self->dbus_addr = g_strdup(str);
+}
+
+static char *
+get_id_list(Object *o, Error **errp)
+{
+ DBusVMState *self = DBUS_VMSTATE(o);
+
+ return g_strdup(self->id_list);
+}
+
+static void
+set_id_list(Object *o, const char *str, Error **errp)
+{
+ DBusVMState *self = DBUS_VMSTATE(o);
+
+ g_free(self->id_list);
+ self->id_list = g_strdup(str);
+}
+
+static void
+dbus_vmstate_class_init(ObjectClass *oc, void *data)
+{
+ UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
+
+ ucc->complete = dbus_vmstate_complete;
+
+ object_class_property_add_str(oc, "addr",
+ get_dbus_addr, set_dbus_addr,
+ &error_abort);
+ object_class_property_add_str(oc, "id-list",
+ get_id_list, set_id_list,
+ &error_abort);
+}
+
+static const TypeInfo dbus_vmstate_info = {
+ .name = TYPE_DBUS_VMSTATE,
+ .parent = TYPE_OBJECT,
+ .instance_size = sizeof(DBusVMState),
+ .instance_finalize = dbus_vmstate_finalize,
+ .class_size = sizeof(DBusVMStateClass),
+ .class_init = dbus_vmstate_class_init,
+ .interfaces = (InterfaceInfo[]) {
+ { TYPE_USER_CREATABLE },
+ { }
+ }
+};
+
+static void
+register_types(void)
+{
+ type_register_static(&dbus_vmstate_info);
+}
+
+type_init(register_types);
diff --git a/configure b/configure
index 4983c8b533..2a64f85d20 100755
--- a/configure
+++ b/configure
@@ -3659,10 +3659,16 @@ if $pkg_config --atleast-version=$glib_req_ver gio-2.0;
then
gio=yes
gio_cflags=$($pkg_config --cflags gio-2.0)
gio_libs=$($pkg_config --libs gio-2.0)
+ gdbus_codegen=$($pkg_config --variable=gdbus_codegen gio-2.0)
else
gio=no
fi
+if $pkg_config --atleast-version=$glib_req_ver gio-unix-2.0; then
+ gio_cflags="$gio_cflags $($pkg_config --cflags gio-unix-2.0)"
+ gio_libs="$gio_libs $($pkg_config --libs gio-unix-2.0)"
+fi
+
# Sanity check that the current size_t matches the
# size that glib thinks it should be. This catches
# problems on multi-arch where people try to build
@@ -6823,6 +6829,7 @@ if test "$gio" = "yes" ; then
echo "CONFIG_GIO=y" >> $config_host_mak
echo "GIO_CFLAGS=$gio_cflags" >> $config_host_mak
echo "GIO_LIBS=$gio_libs" >> $config_host_mak
+ echo "GDBUS_CODEGEN=$gdbus_codegen" >> $config_host_mak
fi
echo "CONFIG_TLS_PRIORITY=\"$tls_priority\"" >> $config_host_mak
if test "$gnutls" = "yes" ; then
diff --git a/docs/interop/dbus-vmstate.rst b/docs/interop/dbus-vmstate.rst
new file mode 100644
index 0000000000..32d3475315
--- /dev/null
+++ b/docs/interop/dbus-vmstate.rst
@@ -0,0 +1,64 @@
+============
+DBus VMState
+============
+
+Introduction
+============
+
+Helper processes may have their state migrated with the help of the
+QEMU object "dbus-vmstate".
+
+Upon migration, QEMU will go through the queue of "org.qemu.VMState1"
+DBus name owners and query their "Id". It must be unique among the
+helpers.
+
+It will then save arbitrary data of each Id to be transferred in the
+migration stream and restored/loaded at the corresponding destination
+helper.
+
+The data amount to be transferred is limited to 1Mb. The state must be
+saved quickly (a few seconds maximum). (DBus imposes a time limit on
+reply anyway, and migration would fail if data isn't given quickly
+enough)
+
+dbus-vmstate object can be configured with the expected list of
+helpers by setting its "id-list" property, with a coma-separated Id
+list.
+
+Interface
+=========
+
+.. code:: xml
+
+ <interface name="org.qemu.VMState1">
+ <property name="Id" type="s" access="read"/>
+ <method name="Load">
+ <arg type="ay" name="data" direction="in"/>
+ </method>
+ <method name="Save">
+ <arg type="ay" name="data" direction="out"/>
+ </method>
+ </interface>
+
+"Id" property
+-------------
+
+A string that identifies the helper uniquely.
+
+Load(in u8[] bytes) method
+--------------------------
+
+The method called on destination with the state to restore.
+
+The helper may be initially started in a waiting state (with
+an --incoming argument for example), and it may resume on success.
+
+An error may be returned to the caller.
+
+Save(out u8[] bytes) method
+---------------------------
+
+The method called on the source to get the current state to be
+migrated. The helper should continue to run normally.
+
+An error may be returned to the caller.
diff --git a/docs/interop/index.rst b/docs/interop/index.rst
index b4bfcab417..6bb173cfa6 100644
--- a/docs/interop/index.rst
+++ b/docs/interop/index.rst
@@ -13,6 +13,7 @@ Contents:
:maxdepth: 2
bitmaps
+ dbus-vmstate
live-block-operations
pr-helper
vhost-user
diff --git a/tests/Makefile.include b/tests/Makefile.include
index a983dd32da..4023a20c62 100644
--- a/tests/Makefile.include
+++ b/tests/Makefile.include
@@ -156,7 +156,9 @@ check-qtest-pci-$(CONFIG_RTL8139_PCI) +=
tests/rtl8139-test$(EXESUF)
check-qtest-pci-$(CONFIG_VGA) += tests/display-vga-test$(EXESUF)
check-qtest-pci-$(CONFIG_HDA) += tests/intel-hda-test$(EXESUF)
check-qtest-pci-$(CONFIG_IVSHMEM_DEVICE) += tests/ivshmem-test$(EXESUF)
-
+ifneq ($(GDBUS_CODEGEN),)
+check-qtest-pci-$(CONFIG_GIO) += tests/dbus-vmstate-test$(EXESUF)
+endif
check-qtest-i386-$(CONFIG_ISA_TESTDEV) = tests/endianness-test$(EXESUF)
check-qtest-i386-y += tests/fdc-test$(EXESUF)
check-qtest-i386-y += tests/ide-test$(EXESUF)
@@ -616,6 +618,18 @@ tests/qapi-schema/doc-good.test.texi:
$(SRC_PATH)/tests/qapi-schema/doc-good.jso
@mv tests/qapi-schema/doc-good-qapi-doc.texi $@
@rm -f tests/qapi-schema/doc-good-qapi-*.[ch]
tests/qapi-schema/doc-good-qmp-*.[ch]
+tests/dbus-vmstate1.h tests/dbus-vmstate1.c: tests/dbus-vmstate1-gen-timestamp
;
+tests/dbus-vmstate1-gen-timestamp: $(SRC_PATH)/tests/dbus-vmstate1.xml
+ $(call quiet-command,$(GDBUS_CODEGEN) $< \
+ --interface-prefix org.qemu --generate-c-code
tests/dbus-vmstate1, \
+ "GEN","$(@:%-timestamp=%)")
+ @>$@
+
+tests/dbus-vmstate1.o-cflags := $(GIO_CFLAGS)
+tests/dbus-vmstate1.o-libs := $(GIO_LIBS)
+
+tests/dbus-vmstate-test.o: tests/dbus-vmstate1.h
+
tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o
$(test-qapi-obj-y)
tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o
$(test-qapi-obj-y)
tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y)
tests/test-qapi-events.o
@@ -818,6 +832,7 @@ tests/test-filter-mirror$(EXESUF):
tests/test-filter-mirror.o $(qtest-obj-y)
tests/test-filter-redirector$(EXESUF): tests/test-filter-redirector.o
$(qtest-obj-y)
tests/test-x86-cpuid-compat$(EXESUF): tests/test-x86-cpuid-compat.o
$(qtest-obj-y)
tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o
contrib/ivshmem-server/ivshmem-server.o $(libqos-pc-obj-y) $(libqos-spapr-obj-y)
+tests/dbus-vmstate-test$(EXESUF): tests/dbus-vmstate-test.o
tests/dbus-vmstate1.o $(libqos-pc-obj-y) $(libqos-spapr-obj-y)
tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o $(test-util-obj-y)
libvhost-user.a
tests/test-uuid$(EXESUF): tests/test-uuid.o $(test-util-obj-y)
tests/test-arm-mptimer$(EXESUF): tests/test-arm-mptimer.o
@@ -1170,6 +1185,7 @@ check-clean:
rm -rf $(check-unit-y) tests/*.o $(QEMU_IOTESTS_HELPERS-y)
rm -rf $(sort $(foreach target,$(SYSEMU_TARGET_LIST),
$(check-qtest-$(target)-y)) $(check-qtest-generic-y))
rm -f tests/test-qapi-gen-timestamp
+ rm -f tests/dbus-vmstate1-gen-timestamp
rm -rf $(TESTS_VENV_DIR) $(TESTS_RESULTS_DIR)
clean: check-clean
diff --git a/tests/dbus-vmstate-test.c b/tests/dbus-vmstate-test.c
new file mode 100644
index 0000000000..11e806442f
--- /dev/null
+++ b/tests/dbus-vmstate-test.c
@@ -0,0 +1,387 @@
+#include "qemu/osdep.h"
+#include <glib/gstdio.h>
+#include <gio/gio.h>
+#include "libqtest.h"
+#include "qemu-common.h"
+#include "dbus-vmstate1.h"
+
+static char *workdir;
+
+typedef struct TestServerId {
+ const char *name;
+ const char *data;
+ size_t size;
+} TestServerId;
+
+static const TestServerId idA = {
+ "idA", "I'am\0idA!", sizeof("I'am\0idA!")
+};
+
+static const TestServerId idB = {
+ "idB", "I'am\0idB!", sizeof("I'am\0idB!")
+};
+
+typedef struct TestServer {
+ const TestServerId *id;
+ bool save_called;
+ bool load_called;
+} TestServer;
+
+typedef struct Test {
+ const char *id_list;
+ bool migrate_fail;
+ bool without_dst_b;
+ TestServer srcA;
+ TestServer dstA;
+ TestServer srcB;
+ TestServer dstB;
+ GMainLoop *loop;
+ QTestState *src_qemu;
+} Test;
+
+static gboolean
+vmstate_load(VMState1 *object, GDBusMethodInvocation *invocation,
+ const gchar *arg_data, gpointer user_data)
+{
+ TestServer *h = user_data;
+ GVariant *args, *var;
+ const uint8_t *data;
+ size_t size;
+
+ args = g_dbus_method_invocation_get_parameters(invocation);
+ var = g_variant_get_child_value(args, 0);
+ data = g_variant_get_fixed_array(var, &size, sizeof(char));
+ g_assert_cmpuint(size, ==, h->id->size);
+ g_assert(!memcmp(data, h->id->data, h->id->size));
+ h->load_called = true;
+ g_variant_unref(var);
+
+ g_dbus_method_invocation_return_value(invocation, g_variant_new("()"));
+ return TRUE;
+}
+
+static gboolean
+vmstate_save(VMState1 *object, GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ TestServer *h = user_data;
+ GVariant *var;
+
+ var = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE,
+ h->id->data, h->id->size, sizeof(char));
+ g_dbus_method_invocation_return_value(invocation,
+ g_variant_new("(@ay)", var));
+ h->save_called = true;
+
+ return TRUE;
+}
+
+static gboolean
+wait_for_migration_complete(gpointer user_data)
+{
+ Test *test = user_data;
+ QDict *rsp_return;
+ bool stop = false;
+ const char *status;
+
+ qtest_qmp_send(test->src_qemu, "{ 'execute': 'query-migrate' }");
+ rsp_return = qtest_qmp_receive_success(test->src_qemu, NULL, NULL);
+ status = qdict_get_str(rsp_return, "status");
+ if (g_str_equal(status, "completed") || g_str_equal(status, "failed")) {
+ stop = true;
+ g_assert_cmpstr(status, ==,
+ test->migrate_fail ? "failed" : "completed");
+ }
+ qobject_unref(rsp_return);
+
+ if (stop) {
+ g_main_loop_quit(test->loop);
+ }
+ return stop ? G_SOURCE_REMOVE : G_SOURCE_CONTINUE;
+}
+
+static void migrate(QTestState *who, const char *uri)
+{
+ QDict *args, *rsp;
+
+ args = qdict_new();
+ qdict_put_str(args, "uri", uri);
+
+ rsp = qtest_qmp(who, "{ 'execute': 'migrate', 'arguments': %p }", args);
+
+ g_assert(qdict_haskey(rsp, "return"));
+ qobject_unref(rsp);
+}
+
+typedef struct WaitNamed {
+ GMainLoop *loop;
+ bool named;
+} WaitNamed;
+
+static void
+named_cb(GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ WaitNamed *t = user_data;
+
+ t->named = true;
+ g_main_loop_quit(t->loop);
+}
+
+static GDBusConnection *
+get_connection(Test *test, guint *ownid)
+{
+ WaitNamed *wait = g_new0(WaitNamed, 1);
+ GError *err = NULL;
+ GDBusConnection *c;
+ gchar *addr;
+
+ wait->loop = test->loop;
+ addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, NULL, &err);
+ g_assert_no_error(err);
+
+ c = g_dbus_connection_new_for_address_sync(
+ addr,
+ G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION |
+ G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
+ NULL, NULL, &err);
+ g_assert_no_error(err);
+ *ownid = g_bus_own_name_on_connection(c, "org.qemu.VMState1",
+ G_BUS_NAME_OWNER_FLAGS_NONE,
+ named_cb, named_cb, wait, g_free);
+ if (!wait->named) {
+ g_main_loop_run(wait->loop);
+ }
+
+ g_free(addr);
+ return c;
+}
+
+static GDBusObjectManagerServer *
+get_server(GDBusConnection *conn, TestServer *s, const TestServerId *id)
+{
+ GDBusObjectManagerServer *os;
+ GDBusObjectSkeleton *sk;
+ VMState1 *v;
+
+ s->id = id;
+ os = g_dbus_object_manager_server_new("/org/qemu");
+ sk = g_dbus_object_skeleton_new("/org/qemu/VMState1");
+
+ v = vmstate1_skeleton_new();
+ g_object_set(v, "id", id->name, NULL);
+
+ g_signal_connect(v, "handle-load", G_CALLBACK(vmstate_load), s);
+ g_signal_connect(v, "handle-save", G_CALLBACK(vmstate_save), s);
+
+ g_dbus_object_skeleton_add_interface(sk, G_DBUS_INTERFACE_SKELETON(v));
+ g_dbus_object_manager_server_export(os, sk);
+ g_dbus_object_manager_server_set_connection(os, conn);
+
+ g_clear_object(&v);
+ g_clear_object(&sk);
+
+ return os;
+}
+
+static void
+set_id_list(Test *test, QTestState *s)
+{
+ if (!test->id_list) {
+ return;
+ }
+
+ g_assert(!qmp_rsp_is_err(qtest_qmp(s,
+ "{ 'execute': 'qom-set', 'arguments': "
+ "{ 'path': '/objects/dv', 'property': 'id-list', 'value': %s } }",
+ test->id_list)));
+}
+static void
+test_dbus_vmstate(Test *test)
+{
+ QTestState *src_qemu = NULL, *dst_qemu = NULL;
+ char *src_qemu_args = NULL, *dst_qemu_args = NULL;
+ GTestDBus *srcbus = NULL, *dstbus = NULL;
+ GDBusConnection *srcconnA = NULL, *srcconnB = NULL;
+ GDBusConnection *dstconnA = NULL, *dstconnB = NULL;
+ guint ownsrcA, ownsrcB, owndstA, owndstB;
+ GDBusObjectManagerServer *srcserverA = NULL, *srcserverB = NULL;
+ GDBusObjectManagerServer *dstserverA = NULL, *dstserverB = NULL;
+ char **srcaddr = NULL, **dstaddr = NULL;
+ char *uri = g_strdup_printf("unix:%s/migsocket", workdir);
+
+ test->loop = g_main_loop_new(NULL, TRUE);
+
+ srcbus = g_test_dbus_new(G_TEST_DBUS_NONE);
+ g_test_dbus_up(srcbus);
+ srcconnA = get_connection(test, &ownsrcA);
+ srcserverA = get_server(srcconnA, &test->srcA, &idA);
+ srcconnB = get_connection(test, &ownsrcB);
+ srcserverB = get_server(srcconnB, &test->srcB, &idB);
+
+ /* remove ,guid=foo part */
+ srcaddr = g_strsplit(g_test_dbus_get_bus_address(srcbus), ",", 2);
+ src_qemu_args =
+ g_strdup_printf("-object dbus-vmstate,id=dv,addr=%s", srcaddr[0]);
+
+ dstbus = g_test_dbus_new(G_TEST_DBUS_NONE);
+ g_test_dbus_up(dstbus);
+ dstconnA = get_connection(test, &owndstA);
+ dstserverA = get_server(dstconnA, &test->dstA, &idA);
+ if (!test->without_dst_b) {
+ dstconnB = get_connection(test, &owndstB);
+ dstserverB = get_server(dstconnB, &test->dstB, &idB);
+ }
+
+ dstaddr = g_strsplit(g_test_dbus_get_bus_address(dstbus), ",", 2);
+ dst_qemu_args =
+ g_strdup_printf("-object dbus-vmstate,id=dv,addr=%s -incoming %s",
+ dstaddr[0], uri);
+
+ src_qemu = qtest_init(src_qemu_args);
+ dst_qemu = qtest_init(dst_qemu_args);
+ set_id_list(test, src_qemu);
+ set_id_list(test, dst_qemu);
+
+ migrate(src_qemu, uri);
+ test->src_qemu = src_qemu;
+ g_timeout_add_seconds(1, wait_for_migration_complete, test);
+
+ g_main_loop_run(test->loop);
+ g_main_loop_unref(test->loop);
+
+ g_free(uri);
+ if (test->migrate_fail) {
+ qtest_set_exit_status(dst_qemu, 1);
+ }
+ qtest_quit(dst_qemu);
+ qtest_quit(src_qemu);
+ g_free(dst_qemu_args);
+ g_free(src_qemu_args);
+ g_bus_unown_name(ownsrcA);
+ g_bus_unown_name(ownsrcB);
+ g_bus_unown_name(owndstA);
+ if (!test->without_dst_b) {
+ g_bus_unown_name(owndstB);
+ }
+ g_clear_object(&srcbus);
+ g_clear_object(&dstbus);
+ g_clear_object(&srcserverA);
+ g_clear_object(&dstserverA);
+ g_clear_object(&srcserverB);
+ g_clear_object(&dstserverB);
+ g_clear_object(&srcconnA);
+ g_clear_object(&dstconnA);
+ g_clear_object(&srcconnB);
+ g_clear_object(&dstconnB);
+ g_strfreev(srcaddr);
+ g_strfreev(dstaddr);
+}
+
+static void
+check_not_migrated(TestServer *s, TestServer *d)
+{
+ assert(!s->save_called);
+ assert(!s->load_called);
+ assert(!d->save_called);
+ assert(!d->load_called);
+}
+
+static void
+check_migrated(TestServer *s, TestServer *d)
+{
+ assert(s->save_called);
+ assert(!s->load_called);
+ assert(!d->save_called);
+ assert(d->load_called);
+}
+
+static void
+test_dbus_vmstate_without_list(void)
+{
+ Test test = { 0, };
+
+ test_dbus_vmstate(&test);
+
+ check_migrated(&test.srcA, &test.dstA);
+ check_migrated(&test.srcB, &test.dstB);
+}
+
+static void
+test_dbus_vmstate_with_list(void)
+{
+ Test test = { .id_list = "idA,idB" };
+
+ test_dbus_vmstate(&test);
+
+ check_migrated(&test.srcA, &test.dstA);
+ check_migrated(&test.srcB, &test.dstB);
+}
+
+static void
+test_dbus_vmstate_only_a(void)
+{
+ Test test = { .id_list = "idA" };
+
+ test_dbus_vmstate(&test);
+
+ check_migrated(&test.srcA, &test.dstA);
+ check_not_migrated(&test.srcB, &test.dstB);
+}
+
+static void
+test_dbus_vmstate_missing_src(void)
+{
+ Test test = { .id_list = "idA,idC", .migrate_fail = true };
+
+ test_dbus_vmstate(&test);
+ check_not_migrated(&test.srcA, &test.dstA);
+ check_not_migrated(&test.srcB, &test.dstB);
+}
+
+static void
+test_dbus_vmstate_missing_dst(void)
+{
+ Test test = { .id_list = "idA,idB",
+ .without_dst_b = true,
+ .migrate_fail = true };
+
+ test_dbus_vmstate(&test);
+
+ assert(test.srcA.save_called);
+ assert(test.srcB.save_called);
+ assert(!test.dstB.save_called);
+}
+
+int
+main(int argc, char **argv)
+{
+ GError *err = NULL;
+ int ret;
+
+ g_test_init(&argc, &argv, NULL);
+
+ workdir = g_dir_make_tmp("dbus-vmstate-test-XXXXXX", &err);
+ if (!workdir) {
+ g_error("Unable to create temporary dir: %s\n", err->message);
+ }
+
+ qtest_add_func("/dbus-vmstate/without-list",
+ test_dbus_vmstate_without_list);
+ qtest_add_func("/dbus-vmstate/with-list",
+ test_dbus_vmstate_with_list);
+ qtest_add_func("/dbus-vmstate/only-a",
+ test_dbus_vmstate_only_a);
+ qtest_add_func("/dbus-vmstate/missing-src",
+ test_dbus_vmstate_missing_src);
+ qtest_add_func("/dbus-vmstate/missing-dst",
+ test_dbus_vmstate_missing_dst);
+
+ ret = g_test_run();
+
+ rmdir(workdir);
+ g_free(workdir);
+
+ return ret;
+}
diff --git a/tests/dbus-vmstate1.xml b/tests/dbus-vmstate1.xml
new file mode 100644
index 0000000000..cc8563be4c
--- /dev/null
+++ b/tests/dbus-vmstate1.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0"?>
+<node name="/" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
+ <interface name="org.qemu.VMState1">
+ <property name="Id" type="s" access="read"/>
+ <method name="Load">
+ <arg type="ay" name="data" direction="in"/>
+ </method>
+ <method name="Save">
+ <arg type="ay" name="data" direction="out"/>
+ </method>
+ </interface>
+</node>
--
2.22.0.214.g8dca754b1e
- [Qemu-devel] [PATCH 0/3] Add dbus-vmstate, Marc-André Lureau, 2019/07/08
- [Qemu-devel] [PATCH 1/3] qemu-file: move qemu_{get, put}_counted_string() declarations, Marc-André Lureau, 2019/07/08
- [Qemu-devel] [PATCH 2/3] tests: add qtest_set_exit_status(), Marc-André Lureau, 2019/07/08
- [Qemu-devel] [PATCH 3/3] Add dbus-vmstate object,
Marc-André Lureau <=
- Re: [Qemu-devel] [PATCH 0/3] Add dbus-vmstate, no-reply, 2019/07/08
- Re: [Qemu-devel] [PATCH 0/3] Add dbus-vmstate, no-reply, 2019/07/08
- Re: [Qemu-devel] [PATCH 0/3] Add dbus-vmstate, Dr. David Alan Gilbert, 2019/07/08
- Re: [Qemu-devel] [PATCH 0/3] Add dbus-vmstate, Daniel P . Berrangé, 2019/07/08