From patchwork Sun Feb 5 09:12:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: ben@skyportsystems.com X-Patchwork-Id: 9556019 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5103D602B1 for ; Sun, 5 Feb 2017 09:17:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4165C26E82 for ; Sun, 5 Feb 2017 09:17:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3389B26E39; Sun, 5 Feb 2017 09:17:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 7CB7826E39 for ; Sun, 5 Feb 2017 09:17:51 +0000 (UTC) Received: from localhost ([::1]:42357 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1caIxC-0006Go-FI for patchwork-qemu-devel@patchwork.kernel.org; Sun, 05 Feb 2017 04:17:50 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59610) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1caIsK-0002f8-7u for qemu-devel@nongnu.org; Sun, 05 Feb 2017 04:12:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1caIsH-0001td-W0 for qemu-devel@nongnu.org; Sun, 05 Feb 2017 04:12:48 -0500 Received: from mail-pg0-x235.google.com ([2607:f8b0:400e:c05::235]:34237) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1caIsH-0001tY-O2 for qemu-devel@nongnu.org; Sun, 05 Feb 2017 04:12:45 -0500 Received: by mail-pg0-x235.google.com with SMTP id 14so19485676pgg.1 for ; Sun, 05 Feb 2017 01:12:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skyportsystems.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=c3Ju2lJCh9KizfXpD0SF8Mkq9dleGWgTaGgjuQ5pQ0A=; b=Ef7sS+119f4wQrj2rewxLbiRmR6qT6t3moQwYnyMtrFqsABoCd66AZ04HIcMeziZXF 1FFxHw+FYlWl0N54c2CkLcqiDBYcJJGmNf+DJ/ynwKxlRdO0X2nDIakzlg4asyYomusl /V2UTD5o6lAA3GNIB67JWhtixpNKbBWL+7gLg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=c3Ju2lJCh9KizfXpD0SF8Mkq9dleGWgTaGgjuQ5pQ0A=; b=KgqMJ3a62x9u2gIRJi+GsvIBxgrZx4ZOcqme2DVIzEDyTuM8QTzZ5s/Fu8r+oONlFZ KtWp/9HfTHfCClp2gGobFr6hG0B8NopG1fKVjQ/JcluUz5JCeY0L4rltQR0JGHX+lw0P U5nr8/Zu/WLsjY1T3l+ebwCd4aKRSwZW8v24iNYRQpL977krc+6t5ediX5upZMXMKmnC GqeO5odwqmYgsTuO0bXyFj4qDO1fbJufSl/+qY5q21BO7Ecxa9pII5v9i5BRWCBiG6mC h0qJ0qBqHPNPP/i3+TASsWZoZfuaJjADHsTaflVMhlsb4lm65za2B+irMNGwaOPNF8kE 2Xdw== X-Gm-Message-State: AIkVDXLCtl3VmxBNiy4jKPtDBLVAJD6vb8YD8fNFH2cNSvTyBIqHISHA8Sq61mVUJSheh4QV X-Received: by 10.99.98.132 with SMTP id w126mr6831242pgb.59.1486285964850; Sun, 05 Feb 2017 01:12:44 -0800 (PST) Received: from Arrow.corp.skyportsystems.com (76-236-31-201.lightspeed.sntcca.sbcglobal.net. [76.236.31.201]) by smtp.gmail.com with ESMTPSA id s24sm2347127pgo.25.2017.02.05.01.12.44 (version=TLS1 cipher=AES128-SHA bits=128/128); Sun, 05 Feb 2017 01:12:44 -0800 (PST) From: ben@skyportsystems.com To: qemu-devel@nongnu.org Date: Sun, 5 Feb 2017 01:12:05 -0800 Message-Id: X-Mailer: git-send-email 2.10.1 (Apple Git-78) In-Reply-To: References: In-Reply-To: References: X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:400e:c05::235 Subject: [Qemu-devel] [PATCH v5 10/10] tests: Add unit tests for the VM Generation ID feature X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: imammedo@redhat.com, lersek@redhat.com, Ben Warren , mst@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP From: Ben Warren The following tests are implemented: * test that a GUID passed in by command line is propagated to the guest. * test that changing the GUID at runtime via the monitor is reflected in the guest. * test that the "auto" argument to the GUID generates a different, and correct GUID as seen by the guest. This patch is loosely based on a previous patch from: Gal Hammer and Igor Mammedov Signed-off-by: Ben Warren --- tests/Makefile.include | 2 + tests/vmgenid-test.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 tests/vmgenid-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index 634394a..ca4b3f7 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -241,6 +241,7 @@ check-qtest-i386-y += tests/usb-hcd-xhci-test$(EXESUF) gcov-files-i386-y += hw/usb/hcd-xhci.c check-qtest-i386-y += tests/pc-cpu-test$(EXESUF) check-qtest-i386-y += tests/q35-test$(EXESUF) +check-qtest-i386-y += tests/vmgenid-test$(EXESUF) gcov-files-i386-y += hw/pci-host/q35.c check-qtest-i386-$(CONFIG_VHOST_NET_TEST_i386) += tests/vhost-user-test$(EXESUF) ifeq ($(CONFIG_VHOST_NET_TEST_i386),) @@ -726,6 +727,7 @@ tests/ivshmem-test$(EXESUF): tests/ivshmem-test.o contrib/ivshmem-server/ivshmem tests/vhost-user-bridge$(EXESUF): tests/vhost-user-bridge.o contrib/libvhost-user/libvhost-user.o $(test-util-obj-y) tests/test-uuid$(EXESUF): tests/test-uuid.o $(test-util-obj-y) tests/test-arm-mptimer$(EXESUF): tests/test-arm-mptimer.o +tests/vmgenid-test$(EXESUF): tests/vmgenid-test.o tests/migration/stress$(EXESUF): tests/migration/stress.o $(call quiet-command, $(LINKPROG) -static -O3 $(PTHREAD_LIB) -o $@ $< ,"LINK","$(TARGET_DIR)$@") diff --git a/tests/vmgenid-test.c b/tests/vmgenid-test.c new file mode 100644 index 0000000..0f5ba07 --- /dev/null +++ b/tests/vmgenid-test.c @@ -0,0 +1,184 @@ +/* + * QTest testcase for VM Generation ID + * + * Copyright (c) 2016 Red Hat, Inc. + * Copyright (c) 2017 Skyport Systems + * + * 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 +#include +#include +#include "qemu/osdep.h" +#include "qemu/bitmap.h" +#include "qemu/uuid.h" +#include "hw/acpi/acpi-defs.h" +#include "acpi-utils.h" +#include "libqtest.h" + +#define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87" + +static uint64_t vgia; + +typedef struct { + AcpiTableHeader header; + gchar name_op; + gchar vgia[4]; + gchar val_op; + uint64_t vgia_val; +} QEMU_PACKED VgidTable; + +static uint64_t find_vgia(void) +{ + uint32_t off; + AcpiRsdpDescriptor rsdp_table; + uint32_t rsdt; + AcpiRsdtDescriptorRev1 rsdt_table; + int tables_nr; + uint32_t *tables; + AcpiTableHeader ssdt_table; + VgidTable vgid_table; + int i; + + /* First, find the RSDP */ + for (off = 0xf0000; off < 0x100000; off += 0x10) { + uint8_t sig[] = "RSD PTR "; + + for (i = 0; i < sizeof sig - 1; ++i) { + sig[i] = readb(off + i); + } + + if (!memcmp(sig, "RSD PTR ", sizeof sig)) { + break; + } + } + g_assert_cmphex(off, <, 0x100000); + + /* Parse the RSDP header so we can find the RSDT */ + ACPI_READ_FIELD(rsdp_table.signature, off); + ACPI_ASSERT_CMP64(rsdp_table.signature, "RSD PTR "); + + ACPI_READ_FIELD(rsdp_table.checksum, off); + ACPI_READ_ARRAY(rsdp_table.oem_id, off); + ACPI_READ_FIELD(rsdp_table.revision, off); + ACPI_READ_FIELD(rsdp_table.rsdt_physical_address, off); + + rsdt = rsdp_table.rsdt_physical_address; + /* read the header */ + ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt); + ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT"); + + /* compute the table entries in rsdt */ + tables_nr = (rsdt_table.length - sizeof(AcpiRsdtDescriptorRev1)) / + sizeof(uint32_t); + g_assert_cmpint(tables_nr, >, 0); + + /* get the addresses of the tables pointed by rsdt */ + tables = g_new0(uint32_t, tables_nr); + ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt); + + for (i = 0; i < tables_nr; i++) { + ACPI_READ_TABLE_HEADER(&ssdt_table, tables[i]); + if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) { + /* the first entry in the table should be VGIA + * That's all we need */ + ACPI_READ_FIELD(vgid_table.name_op, tables[i]); + g_assert(vgid_table.name_op == 0x08); /* name */ + ACPI_READ_ARRAY(vgid_table.vgia, tables[i]); + g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0); + ACPI_READ_FIELD(vgid_table.val_op, tables[i]); + g_assert(vgid_table.val_op == 0x0E); /* qword */ + ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]); + return vgid_table.vgia_val; + } + } + return 0; +} + +static void vmgenid_read_guid(uint8_t *guid) +{ + int i; + + if (vgia == 0) { + vgia = find_vgia(); + } + g_assert(vgia); + + /* Read the GUID directly from guest memory */ + for (i = 0; i < 16; i++) { + guid[i] = readb(vgia + i); + } +} + +static void vmgenid_test(void) +{ + uint8_t measured[16]; + QemuUUID expected; + g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); + vmgenid_read_guid(measured); + g_assert(memcmp(measured, expected.data, sizeof(measured)) == 0); +} + +static void vmgenid_set_guid_test(void) +{ + QDict *response; + gchar *cmd; + uint8_t measured[16]; + QemuUUID expected; + g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); + /* Change the GUID slightly */ + expected.data[0] += 1; + + cmd = g_strdup_printf("{ 'execute': 'qom-set', 'arguments': { " + "'path': '/machine/peripheral/testvgid', " + "'property': 'guid', 'value': '%s' } }", + qemu_uuid_unparse_strdup(&expected)); + response = qmp(cmd); + g_assert(qdict_haskey(response, "return")); + QDECREF(response); + + vmgenid_read_guid(measured); + g_assert(memcmp(measured, expected.data, sizeof(measured)) == 0); +} + +static void vmgenid_set_guid_auto_test(void) +{ + QDict *response; + QemuUUID measured; + QemuUUID expected; + + /* Read the initial value */ + vmgenid_read_guid(expected.data); + + /* Setting to 'auto' generates a random GUID */ + response = qmp("{ 'execute': 'qom-set', 'arguments': { " + "'path': '/machine/peripheral/testvgid', " + "'property': 'guid', 'value': 'auto' } }"); + + g_assert(qdict_haskey(response, "return")); + QDECREF(response); + + vmgenid_read_guid(measured.data); + g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) != 0); +} + +int main(int argc, char **argv) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + + qtest_start("-machine accel=tcg -device vmgenid,id=testvgid," + "guid=" VGID_GUID); + qtest_add_func("/vmgenid/vmgenid", vmgenid_test); + qtest_add_func("/vmgenid/vmgenid/set-guid", vmgenid_set_guid_test); + qtest_add_func("/vmgenid/vmgenid/set-guid-auto", + vmgenid_set_guid_auto_test); + ret = g_test_run(); + + qtest_end(); + + return ret; +}