qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

tests: add dbus-vmstate-test

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

+511 -1
+1
MAINTAINERS
··· 2210 2210 M: Marc-André Lureau <marcandre.lureau@redhat.com> 2211 2211 S: Maintained 2212 2212 F: backends/dbus-vmstate.c 2213 + F: tests/dbus-vmstate* 2213 2214 F: util/dbus.c 2214 2215 F: include/qemu/dbus.h 2215 2216 F: docs/interop/dbus.rst
+21 -1
tests/Makefile.include
··· 158 158 159 159 check-qtest-generic-y += tests/device-introspect-test$(EXESUF) 160 160 check-qtest-generic-y += tests/cdrom-test$(EXESUF) 161 + DBUS_DAEMON := $(shell which dbus-daemon 2>/dev/null) 162 + ifneq ($(GDBUS_CODEGEN),) 163 + ifneq ($(DBUS_DAEMON),) 164 + check-qtest-pci-$(CONFIG_GIO) += tests/dbus-vmstate-test$(EXESUF) 165 + endif 166 + endif 161 167 162 168 check-qtest-pci-$(CONFIG_RTL8139_PCI) += tests/rtl8139-test$(EXESUF) 163 169 check-qtest-pci-$(CONFIG_VGA) += tests/display-vga-test$(EXESUF) 164 170 check-qtest-pci-$(CONFIG_HDA) += tests/intel-hda-test$(EXESUF) 165 171 check-qtest-pci-$(CONFIG_IVSHMEM_DEVICE) += tests/ivshmem-test$(EXESUF) 166 - 167 172 check-qtest-i386-$(CONFIG_ISA_TESTDEV) = tests/endianness-test$(EXESUF) 168 173 check-qtest-i386-y += tests/fdc-test$(EXESUF) 169 174 check-qtest-i386-y += tests/ide-test$(EXESUF) ··· 634 639 @mv tests/qapi-schema/doc-good-qapi-doc.texi $@ 635 640 @rm -f tests/qapi-schema/doc-good-qapi-*.[ch] tests/qapi-schema/doc-good-qmp-*.[ch] 636 641 642 + tests/dbus-vmstate1.h tests/dbus-vmstate1.c: tests/dbus-vmstate1-gen-timestamp ; 643 + tests/dbus-vmstate1-gen-timestamp: $(SRC_PATH)/tests/dbus-vmstate1.xml 644 + $(call quiet-command,$(GDBUS_CODEGEN) $< \ 645 + --interface-prefix org.qemu --generate-c-code tests/dbus-vmstate1, \ 646 + "GEN","$(@:%-timestamp=%)") 647 + @>$@ 648 + 649 + tests/dbus-vmstate-test.o-cflags := -DSRCDIR="$(SRC_PATH)" 650 + tests/dbus-vmstate1.o-cflags := $(GIO_CFLAGS) 651 + tests/dbus-vmstate1.o-libs := $(GIO_LIBS) 652 + 653 + tests/dbus-vmstate-test.o: tests/dbus-vmstate1.h 654 + 637 655 tests/test-string-output-visitor$(EXESUF): tests/test-string-output-visitor.o $(test-qapi-obj-y) 638 656 tests/test-string-input-visitor$(EXESUF): tests/test-string-input-visitor.o $(test-qapi-obj-y) 639 657 tests/test-qmp-event$(EXESUF): tests/test-qmp-event.o $(test-qapi-obj-y) tests/test-qapi-events.o ··· 837 855 tests/test-filter-redirector$(EXESUF): tests/test-filter-redirector.o $(qtest-obj-y) 838 856 tests/test-x86-cpuid-compat$(EXESUF): tests/test-x86-cpuid-compat.o $(qtest-obj-y) 839 857 tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o contrib/ivshmem-server/ivshmem-server.o $(libqos-pc-obj-y) $(libqos-spapr-obj-y) 858 + tests/dbus-vmstate-test$(EXESUF): tests/dbus-vmstate-test.o tests/migration-helpers.o tests/dbus-vmstate1.o $(libqos-pc-obj-y) $(libqos-spapr-obj-y) 840 859 tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o $(test-util-obj-y) libvhost-user.a 841 860 tests/test-uuid$(EXESUF): tests/test-uuid.o $(test-util-obj-y) 842 861 tests/test-arm-mptimer$(EXESUF): tests/test-arm-mptimer.o ··· 1195 1214 rm -rf $(check-unit-y) tests/*.o $(QEMU_IOTESTS_HELPERS-y) 1196 1215 rm -rf $(sort $(foreach target,$(SYSEMU_TARGET_LIST), $(check-qtest-$(target)-y)) $(check-qtest-generic-y)) 1197 1216 rm -f tests/test-qapi-gen-timestamp 1217 + rm -f tests/dbus-vmstate1-gen-timestamp 1198 1218 rm -rf $(TESTS_VENV_DIR) $(TESTS_RESULTS_DIR) 1199 1219 1200 1220 clean: check-clean
+95
tests/dbus-vmstate-daemon.sh
··· 1 + #!/bin/sh 2 + 3 + # dbus-daemon wrapper script for dbus-vmstate testing 4 + # 5 + # This script allows to tweak the dbus-daemon policy during the test 6 + # to test different configurations. 7 + # 8 + # This program is free software; you can redistribute it and/or modify 9 + # it under the terms of the GNU General Public License as published by 10 + # the Free Software Foundation; either version 2 of the License, or 11 + # (at your option) any later version. 12 + # 13 + # This program is distributed in the hope that it will be useful, 14 + # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 + # GNU General Public License for more details. 17 + # 18 + # You should have received a copy of the GNU General Public License 19 + # along with this program; if not, see <http://www.gnu.org/licenses/>. 20 + # 21 + # Copyright (C) 2019 Red Hat, Inc. 22 + 23 + write_config() 24 + { 25 + CONF="$1" 26 + cat > "$CONF" <<EOF 27 + <busconfig> 28 + <type>session</type> 29 + <listen>unix:tmpdir=$DBUS_VMSTATE_TEST_TMPDIR</listen> 30 + 31 + <policy context="default"> 32 + <!-- Holes must be punched in service configuration files for 33 + name ownership and sending method calls --> 34 + <deny own="*"/> 35 + <deny send_type="method_call"/> 36 + 37 + <!-- Signals and reply messages (method returns, errors) are allowed 38 + by default --> 39 + <allow send_type="signal"/> 40 + <allow send_requested_reply="true" send_type="method_return"/> 41 + <allow send_requested_reply="true" send_type="error"/> 42 + 43 + <!-- All messages may be received by default --> 44 + <allow receive_type="method_call"/> 45 + <allow receive_type="method_return"/> 46 + <allow receive_type="error"/> 47 + <allow receive_type="signal"/> 48 + 49 + <!-- Allow anyone to talk to the message bus --> 50 + <allow send_destination="org.freedesktop.DBus" 51 + send_interface="org.freedesktop.DBus" /> 52 + <allow send_destination="org.freedesktop.DBus" 53 + send_interface="org.freedesktop.DBus.Introspectable"/> 54 + <allow send_destination="org.freedesktop.DBus" 55 + send_interface="org.freedesktop.DBus.Properties"/> 56 + <!-- But disallow some specific bus services --> 57 + <deny send_destination="org.freedesktop.DBus" 58 + send_interface="org.freedesktop.DBus" 59 + send_member="UpdateActivationEnvironment"/> 60 + <deny send_destination="org.freedesktop.DBus" 61 + send_interface="org.freedesktop.DBus.Debug.Stats"/> 62 + <deny send_destination="org.freedesktop.DBus" 63 + send_interface="org.freedesktop.systemd1.Activator"/> 64 + 65 + <allow own="org.qemu.VMState1"/> 66 + <allow send_destination="org.qemu.VMState1"/> 67 + <allow receive_sender="org.qemu.VMState1"/> 68 + 69 + </policy> 70 + 71 + <include if_selinux_enabled="yes" 72 + selinux_root_relative="yes">contexts/dbus_contexts</include> 73 + 74 + </busconfig> 75 + EOF 76 + } 77 + 78 + ARGS= 79 + for arg in "$@" 80 + do 81 + case $arg in 82 + --config-file=*) 83 + CONF="${arg#*=}" 84 + write_config "$CONF" 85 + ARGS="$ARGS $1" 86 + shift 87 + ;; 88 + *) 89 + ARGS="$ARGS $1" 90 + shift 91 + ;; 92 + esac 93 + done 94 + 95 + exec dbus-daemon $ARGS
+382
tests/dbus-vmstate-test.c
··· 1 + #include "qemu/osdep.h" 2 + #include <glib/gstdio.h> 3 + #include <gio/gio.h> 4 + #include "libqtest.h" 5 + #include "qemu-common.h" 6 + #include "dbus-vmstate1.h" 7 + #include "migration-helpers.h" 8 + 9 + static char *workdir; 10 + 11 + typedef struct TestServerId { 12 + const char *name; 13 + const char *data; 14 + size_t size; 15 + } TestServerId; 16 + 17 + static const TestServerId idA = { 18 + "idA", "I'am\0idA!", sizeof("I'am\0idA!") 19 + }; 20 + 21 + static const TestServerId idB = { 22 + "idB", "I'am\0idB!", sizeof("I'am\0idB!") 23 + }; 24 + 25 + typedef struct TestServer { 26 + const TestServerId *id; 27 + bool save_called; 28 + bool load_called; 29 + } TestServer; 30 + 31 + typedef struct Test { 32 + const char *id_list; 33 + bool migrate_fail; 34 + bool without_dst_b; 35 + TestServer srcA; 36 + TestServer dstA; 37 + TestServer srcB; 38 + TestServer dstB; 39 + GMainLoop *loop; 40 + QTestState *src_qemu; 41 + } Test; 42 + 43 + static gboolean 44 + vmstate_load(VMState1 *object, GDBusMethodInvocation *invocation, 45 + const gchar *arg_data, gpointer user_data) 46 + { 47 + TestServer *h = user_data; 48 + g_autoptr(GVariant) var = NULL; 49 + GVariant *args; 50 + const uint8_t *data; 51 + size_t size; 52 + 53 + args = g_dbus_method_invocation_get_parameters(invocation); 54 + var = g_variant_get_child_value(args, 0); 55 + data = g_variant_get_fixed_array(var, &size, sizeof(char)); 56 + g_assert_cmpuint(size, ==, h->id->size); 57 + g_assert(!memcmp(data, h->id->data, h->id->size)); 58 + h->load_called = true; 59 + 60 + g_dbus_method_invocation_return_value(invocation, g_variant_new("()")); 61 + return TRUE; 62 + } 63 + 64 + static gboolean 65 + vmstate_save(VMState1 *object, GDBusMethodInvocation *invocation, 66 + gpointer user_data) 67 + { 68 + TestServer *h = user_data; 69 + GVariant *var; 70 + 71 + var = g_variant_new_fixed_array(G_VARIANT_TYPE_BYTE, 72 + h->id->data, h->id->size, sizeof(char)); 73 + g_dbus_method_invocation_return_value(invocation, 74 + g_variant_new("(@ay)", var)); 75 + h->save_called = true; 76 + 77 + return TRUE; 78 + } 79 + 80 + typedef struct WaitNamed { 81 + GMainLoop *loop; 82 + bool named; 83 + } WaitNamed; 84 + 85 + static void 86 + named_cb(GDBusConnection *connection, 87 + const gchar *name, 88 + gpointer user_data) 89 + { 90 + WaitNamed *t = user_data; 91 + 92 + t->named = true; 93 + g_main_loop_quit(t->loop); 94 + } 95 + 96 + static GDBusConnection * 97 + get_connection(Test *test, guint *ownid) 98 + { 99 + g_autofree gchar *addr = NULL; 100 + WaitNamed *wait; 101 + GError *err = NULL; 102 + GDBusConnection *c; 103 + 104 + wait = g_new0(WaitNamed, 1); 105 + wait->loop = test->loop; 106 + addr = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SESSION, NULL, &err); 107 + g_assert_no_error(err); 108 + 109 + c = g_dbus_connection_new_for_address_sync( 110 + addr, 111 + G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION | 112 + G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT, 113 + NULL, NULL, &err); 114 + g_assert_no_error(err); 115 + *ownid = g_bus_own_name_on_connection(c, "org.qemu.VMState1", 116 + G_BUS_NAME_OWNER_FLAGS_NONE, 117 + named_cb, named_cb, wait, g_free); 118 + if (!wait->named) { 119 + g_main_loop_run(wait->loop); 120 + } 121 + 122 + return c; 123 + } 124 + 125 + static GDBusObjectManagerServer * 126 + get_server(GDBusConnection *conn, TestServer *s, const TestServerId *id) 127 + { 128 + g_autoptr(GDBusObjectSkeleton) sk = NULL; 129 + g_autoptr(VMState1Skeleton) v = NULL; 130 + GDBusObjectManagerServer *os; 131 + 132 + s->id = id; 133 + os = g_dbus_object_manager_server_new("/org/qemu"); 134 + sk = g_dbus_object_skeleton_new("/org/qemu/VMState1"); 135 + 136 + v = VMSTATE1_SKELETON(vmstate1_skeleton_new()); 137 + g_object_set(v, "id", id->name, NULL); 138 + 139 + g_signal_connect(v, "handle-load", G_CALLBACK(vmstate_load), s); 140 + g_signal_connect(v, "handle-save", G_CALLBACK(vmstate_save), s); 141 + 142 + g_dbus_object_skeleton_add_interface(sk, G_DBUS_INTERFACE_SKELETON(v)); 143 + g_dbus_object_manager_server_export(os, sk); 144 + g_dbus_object_manager_server_set_connection(os, conn); 145 + 146 + return os; 147 + } 148 + 149 + static void 150 + set_id_list(Test *test, QTestState *s) 151 + { 152 + if (!test->id_list) { 153 + return; 154 + } 155 + 156 + g_assert(!qmp_rsp_is_err(qtest_qmp(s, 157 + "{ 'execute': 'qom-set', 'arguments': " 158 + "{ 'path': '/objects/dv', 'property': 'id-list', 'value': %s } }", 159 + test->id_list))); 160 + } 161 + 162 + static gpointer 163 + dbus_vmstate_thread(gpointer data) 164 + { 165 + GMainLoop *loop = data; 166 + 167 + g_main_loop_run(loop); 168 + 169 + return NULL; 170 + } 171 + 172 + static void 173 + test_dbus_vmstate(Test *test) 174 + { 175 + g_autofree char *src_qemu_args = NULL; 176 + g_autofree char *dst_qemu_args = NULL; 177 + g_autoptr(GTestDBus) srcbus = NULL; 178 + g_autoptr(GTestDBus) dstbus = NULL; 179 + g_autoptr(GDBusConnection) srcconnA = NULL; 180 + g_autoptr(GDBusConnection) srcconnB = NULL; 181 + g_autoptr(GDBusConnection) dstconnA = NULL; 182 + g_autoptr(GDBusConnection) dstconnB = NULL; 183 + g_autoptr(GDBusObjectManagerServer) srcserverA = NULL; 184 + g_autoptr(GDBusObjectManagerServer) srcserverB = NULL; 185 + g_autoptr(GDBusObjectManagerServer) dstserverA = NULL; 186 + g_autoptr(GDBusObjectManagerServer) dstserverB = NULL; 187 + g_auto(GStrv) srcaddr = NULL; 188 + g_auto(GStrv) dstaddr = NULL; 189 + g_autoptr(GThread) thread = NULL; 190 + g_autoptr(GMainLoop) loop = NULL; 191 + g_autofree char *uri = NULL; 192 + QTestState *src_qemu = NULL, *dst_qemu = NULL; 193 + guint ownsrcA, ownsrcB, owndstA, owndstB; 194 + 195 + uri = g_strdup_printf("unix:%s/migsocket", workdir); 196 + 197 + loop = g_main_loop_new(NULL, FALSE); 198 + test->loop = loop; 199 + 200 + srcbus = g_test_dbus_new(G_TEST_DBUS_NONE); 201 + g_test_dbus_up(srcbus); 202 + srcconnA = get_connection(test, &ownsrcA); 203 + srcserverA = get_server(srcconnA, &test->srcA, &idA); 204 + srcconnB = get_connection(test, &ownsrcB); 205 + srcserverB = get_server(srcconnB, &test->srcB, &idB); 206 + 207 + /* remove ,guid=foo part */ 208 + srcaddr = g_strsplit(g_test_dbus_get_bus_address(srcbus), ",", 2); 209 + src_qemu_args = 210 + g_strdup_printf("-object dbus-vmstate,id=dv,addr=%s", srcaddr[0]); 211 + 212 + dstbus = g_test_dbus_new(G_TEST_DBUS_NONE); 213 + g_test_dbus_up(dstbus); 214 + dstconnA = get_connection(test, &owndstA); 215 + dstserverA = get_server(dstconnA, &test->dstA, &idA); 216 + if (!test->without_dst_b) { 217 + dstconnB = get_connection(test, &owndstB); 218 + dstserverB = get_server(dstconnB, &test->dstB, &idB); 219 + } 220 + 221 + dstaddr = g_strsplit(g_test_dbus_get_bus_address(dstbus), ",", 2); 222 + dst_qemu_args = 223 + g_strdup_printf("-object dbus-vmstate,id=dv,addr=%s -incoming %s", 224 + dstaddr[0], uri); 225 + 226 + src_qemu = qtest_init(src_qemu_args); 227 + dst_qemu = qtest_init(dst_qemu_args); 228 + set_id_list(test, src_qemu); 229 + set_id_list(test, dst_qemu); 230 + 231 + thread = g_thread_new("dbus-vmstate-thread", dbus_vmstate_thread, loop); 232 + 233 + migrate_qmp(src_qemu, uri, "{}"); 234 + test->src_qemu = src_qemu; 235 + if (test->migrate_fail) { 236 + wait_for_migration_fail(src_qemu, true); 237 + qtest_set_expected_status(dst_qemu, 1); 238 + } else { 239 + wait_for_migration_complete(src_qemu); 240 + } 241 + 242 + qtest_quit(dst_qemu); 243 + qtest_quit(src_qemu); 244 + g_bus_unown_name(ownsrcA); 245 + g_bus_unown_name(ownsrcB); 246 + g_bus_unown_name(owndstA); 247 + if (!test->without_dst_b) { 248 + g_bus_unown_name(owndstB); 249 + } 250 + 251 + g_main_loop_quit(test->loop); 252 + } 253 + 254 + static void 255 + check_not_migrated(TestServer *s, TestServer *d) 256 + { 257 + assert(!s->save_called); 258 + assert(!s->load_called); 259 + assert(!d->save_called); 260 + assert(!d->load_called); 261 + } 262 + 263 + static void 264 + check_migrated(TestServer *s, TestServer *d) 265 + { 266 + assert(s->save_called); 267 + assert(!s->load_called); 268 + assert(!d->save_called); 269 + assert(d->load_called); 270 + } 271 + 272 + static void 273 + test_dbus_vmstate_without_list(void) 274 + { 275 + Test test = { 0, }; 276 + 277 + test_dbus_vmstate(&test); 278 + 279 + check_migrated(&test.srcA, &test.dstA); 280 + check_migrated(&test.srcB, &test.dstB); 281 + } 282 + 283 + static void 284 + test_dbus_vmstate_with_list(void) 285 + { 286 + Test test = { .id_list = "idA,idB" }; 287 + 288 + test_dbus_vmstate(&test); 289 + 290 + check_migrated(&test.srcA, &test.dstA); 291 + check_migrated(&test.srcB, &test.dstB); 292 + } 293 + 294 + static void 295 + test_dbus_vmstate_only_a(void) 296 + { 297 + Test test = { .id_list = "idA" }; 298 + 299 + test_dbus_vmstate(&test); 300 + 301 + check_migrated(&test.srcA, &test.dstA); 302 + check_not_migrated(&test.srcB, &test.dstB); 303 + } 304 + 305 + static void 306 + test_dbus_vmstate_missing_src(void) 307 + { 308 + Test test = { .id_list = "idA,idC", .migrate_fail = true }; 309 + 310 + /* run in subprocess to silence QEMU error reporting */ 311 + if (g_test_subprocess()) { 312 + test_dbus_vmstate(&test); 313 + check_not_migrated(&test.srcA, &test.dstA); 314 + check_not_migrated(&test.srcB, &test.dstB); 315 + return; 316 + } 317 + 318 + g_test_trap_subprocess(NULL, 0, 0); 319 + g_test_trap_assert_passed(); 320 + } 321 + 322 + static void 323 + test_dbus_vmstate_missing_dst(void) 324 + { 325 + Test test = { .id_list = "idA,idB", 326 + .without_dst_b = true, 327 + .migrate_fail = true }; 328 + 329 + /* run in subprocess to silence QEMU error reporting */ 330 + if (g_test_subprocess()) { 331 + test_dbus_vmstate(&test); 332 + assert(test.srcA.save_called); 333 + assert(test.srcB.save_called); 334 + assert(!test.dstB.save_called); 335 + return; 336 + } 337 + 338 + g_test_trap_subprocess(NULL, 0, 0); 339 + g_test_trap_assert_passed(); 340 + } 341 + 342 + int 343 + main(int argc, char **argv) 344 + { 345 + GError *err = NULL; 346 + g_autofree char *dbus_daemon = NULL; 347 + int ret; 348 + 349 + dbus_daemon = g_build_filename(G_STRINGIFY(SRCDIR), 350 + "tests", 351 + "dbus-vmstate-daemon.sh", 352 + NULL); 353 + g_setenv("G_TEST_DBUS_DAEMON", dbus_daemon, true); 354 + 355 + g_test_init(&argc, &argv, NULL); 356 + 357 + workdir = g_dir_make_tmp("dbus-vmstate-test-XXXXXX", &err); 358 + if (!workdir) { 359 + g_error("Unable to create temporary dir: %s\n", err->message); 360 + exit(1); 361 + } 362 + 363 + g_setenv("DBUS_VMSTATE_TEST_TMPDIR", workdir, true); 364 + 365 + qtest_add_func("/dbus-vmstate/without-list", 366 + test_dbus_vmstate_without_list); 367 + qtest_add_func("/dbus-vmstate/with-list", 368 + test_dbus_vmstate_with_list); 369 + qtest_add_func("/dbus-vmstate/only-a", 370 + test_dbus_vmstate_only_a); 371 + qtest_add_func("/dbus-vmstate/missing-src", 372 + test_dbus_vmstate_missing_src); 373 + qtest_add_func("/dbus-vmstate/missing-dst", 374 + test_dbus_vmstate_missing_dst); 375 + 376 + ret = g_test_run(); 377 + 378 + rmdir(workdir); 379 + g_free(workdir); 380 + 381 + return ret; 382 + }
+12
tests/dbus-vmstate1.xml
··· 1 + <?xml version="1.0"?> 2 + <node name="/" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd"> 3 + <interface name="org.qemu.VMState1"> 4 + <property name="Id" type="s" access="read"/> 5 + <method name="Load"> 6 + <arg type="ay" name="data" direction="in"/> 7 + </method> 8 + <method name="Save"> 9 + <arg type="ay" name="data" direction="out"/> 10 + </method> 11 + </interface> 12 + </node>