From patchwork Sun Jul 2 03:54:03 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Denis V. Lunev\" via" X-Patchwork-Id: 9821045 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 03FED6035F for ; Sun, 2 Jul 2017 03:55:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D0A1A27F60 for ; Sun, 2 Jul 2017 03:55:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BFE5827F86; Sun, 2 Jul 2017 03:55:10 +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 7B3A227F60 for ; Sun, 2 Jul 2017 03:55:08 +0000 (UTC) Received: from localhost ([::1]:56646 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRVyW-0002kW-1Z for patchwork-qemu-devel@patchwork.kernel.org; Sat, 01 Jul 2017 23:55:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44245) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dRVxu-0002iC-3a for qemu-devel@nongnu.org; Sat, 01 Jul 2017 23:54:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dRVxr-0008Ke-0n for qemu-devel@nongnu.org; Sat, 01 Jul 2017 23:54:30 -0400 Received: from mail-it0-x230.google.com ([2607:f8b0:4001:c0b::230]:35626) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dRVxq-0008K5-Qs for qemu-devel@nongnu.org; Sat, 01 Jul 2017 23:54:26 -0400 Received: by mail-it0-x230.google.com with SMTP id v202so75670256itb.0 for ; Sat, 01 Jul 2017 20:54:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=skyportsystems.com; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=wOaloOewuMxqvPX9TqUTCH0ZhjqoW93GHGnFl8wfQYI=; b=M90UVQYwddu7365Js7bs726JOEEvIZVQ7mQvFQxtPuYxg1sHpdcAZv1dTnns7AbPa9 ZM1DeM0Wn+NUsd7kFCOm0nEyl2P4KIAJi4H4XAEMOMMuJ3Fv7WEtJAJ2FfGlMFaLMimu eGrJmtXvGFMuvJyGPySPNLofN4A2Bwy0Tu8qs= 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:mime-version :content-transfer-encoding; bh=wOaloOewuMxqvPX9TqUTCH0ZhjqoW93GHGnFl8wfQYI=; b=ZY82j5FdQ4qYxZriELVSDQ0xspIcTlGlR/hsXBbEIZAk6tK4wrfUcBj+Vbo27LLwYr EoVDdpYgUIcT/UsFpuIuqn9o4Ab9UxVGMLeWsq9/RZ+d5aMEa81UPwQpR4L7XKCEASo8 ksvG3xaKYKOf0RDlDIOSRP6ElMG7Sm7ZUJLWNbDL6xYphev40cc81/lOtlhqqxL3WnEt URC2S0oNPIAbHEcqtIkbV3rA4XJVkxA4QQcKe3o3WxH0/4sCmxiMZHeeI9fIZwL0dsGD jKGun18Y7Lq8OgwxkYNyE8ukRicSmbhyFKIniCN/famYyuZrmNCeL2SYRVF3ldqNc8NP qWBQ== X-Gm-Message-State: AKS2vOx+z6/bnHHyTRRGa0WZI8TaOcGAaMB0CSPG8fbm0iNmOCQUL819 yqRchB3CmVMX+ysqT235Rg== X-Received: by 10.36.139.1 with SMTP id g1mr25263703ite.18.1498967665834; Sat, 01 Jul 2017 20:54:25 -0700 (PDT) Received: from Arrow.com (d72-38-63-15.commercial1.cgocable.net. [72.38.63.15]) by smtp.gmail.com with ESMTPSA id b70sm6463501ioe.57.2017.07.01.20.54.23 (version=TLS1 cipher=AES128-SHA bits=128/128); Sat, 01 Jul 2017 20:54:25 -0700 (PDT) To: qemu-devel@nongnu.org, mst@redhat.com Date: Sat, 1 Jul 2017 23:54:03 -0400 Message-Id: X-Mailer: git-send-email 2.11.0 (Apple Git-81) MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4001:c0b::230 Subject: [Qemu-devel] [PATCH v2] 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: , X-Patchwork-Original-From: Ben Warren via Qemu-devel From: "Denis V. Lunev\" via" Reply-To: ben@skyportsystems.com Cc: imammedo@redhat.com, lersek@redhat.com, marcandre.lureau@gmail.com, Ben Warren 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. Read the GUID from guest memory * test that the "auto" argument to the GUID generates a valid GUID, as seen by the guest. * test that a GUID passed in can be queried from the monitor This patch is loosely based on a previous patch from: Gal Hammer and Igor Mammedov Signed-off-by: Ben Warren Reviewed-by: Igor Mammedov Reviewed-by: Marc-André Lureau --- v1->v2: free g_alloc'd tables tests/Makefile.include | 2 + tests/vmgenid-test.c | 203 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 tests/vmgenid-test.c diff --git a/tests/Makefile.include b/tests/Makefile.include index ae889ca..18cd06a 100644 --- a/tests/Makefile.include +++ b/tests/Makefile.include @@ -250,6 +250,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),) @@ -760,6 +761,7 @@ tests/test-uuid$(EXESUF): tests/test-uuid.o $(test-util-obj-y) tests/test-arm-mptimer$(EXESUF): tests/test-arm-mptimer.o tests/test-qapi-util$(EXESUF): tests/test-qapi-util.o $(test-util-obj-y) tests/numa-test$(EXESUF): tests/numa-test.o +tests/vmgenid-test$(EXESUF): tests/vmgenid-test.o tests/acpi-utils.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..e7ba38c --- /dev/null +++ b/tests/vmgenid-test.c @@ -0,0 +1,203 @@ +/* + * 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" +#define VMGENID_GUID_OFFSET 40 /* allow space for + * OVMF SDT Header Probe Supressor + */ +#define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */ +#define RSDP_SLEEP_US 100000 /* Sleep for 100ms between tries */ +#define RSDP_TRIES_MAX 100 /* Max total time is 10 seconds */ + +typedef struct { + AcpiTableHeader header; + gchar name_op; + gchar vgia[4]; + gchar val_op; + uint32_t vgia_val; +} QEMU_PACKED VgidTable; + +static uint32_t acpi_find_vgia(void) +{ + uint32_t rsdp_offset; + uint32_t guid_offset = 0; + AcpiRsdpDescriptor rsdp_table; + uint32_t rsdt; + AcpiRsdtDescriptorRev1 rsdt_table; + int tables_nr; + uint32_t *tables; + AcpiTableHeader ssdt_table; + VgidTable vgid_table; + int i; + + /* Tables may take a short time to be set up by the guest */ + for (i = 0; i < RSDP_TRIES_MAX; i++) { + rsdp_offset = acpi_find_rsdp_address(); + if (rsdp_offset < RSDP_ADDR_INVALID) { + break; + } + g_usleep(RSDP_SLEEP_US); + } + g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID); + + acpi_parse_rsdp_table(rsdp_offset, &rsdp_table); + + 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 == 0x0C); /* dword */ + ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]); + /* The GUID is written at a fixed offset into the fw_cfg file + * in order to implement the "OVMF SDT Header probe suppressor" + * see docs/specs/vmgenid.txt for more details + */ + guid_offset = vgid_table.vgia_val + VMGENID_GUID_OFFSET; + break; + } + } + g_free(tables); + return guid_offset; +} + +static void read_guid_from_memory(QemuUUID *guid) +{ + uint32_t vmgenid_addr; + int i; + + vmgenid_addr = acpi_find_vgia(); + g_assert(vmgenid_addr); + + /* Read the GUID directly from guest memory */ + for (i = 0; i < 16; i++) { + guid->data[i] = readb(vmgenid_addr + i); + } + /* The GUID is in little-endian format in the guest, while QEMU + * uses big-endian. Swap after reading. + */ + qemu_uuid_bswap(guid); +} + +static void read_guid_from_monitor(QemuUUID *guid) +{ + QDict *rsp, *rsp_ret; + const char *guid_str; + + rsp = qmp("{ 'execute': 'query-vm-generation-id' }"); + if (qdict_haskey(rsp, "return")) { + rsp_ret = qdict_get_qdict(rsp, "return"); + g_assert(qdict_haskey(rsp_ret, "guid")); + guid_str = qdict_get_str(rsp_ret, "guid"); + g_assert(qemu_uuid_parse(guid_str, guid) == 0); + } + QDECREF(rsp); +} + +static void vmgenid_set_guid_test(void) +{ + QemuUUID expected, measured; + gchar *cmd; + + g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); + + cmd = g_strdup_printf("-machine accel=tcg -device vmgenid,id=testvgid," + "guid=%s", VGID_GUID); + qtest_start(cmd); + + /* Read the GUID from accessing guest memory */ + read_guid_from_memory(&measured); + g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0); + + qtest_quit(global_qtest); + g_free(cmd); +} + +static void vmgenid_set_guid_auto_test(void) +{ + const char *cmd; + QemuUUID measured; + + cmd = "-machine accel=tcg -device vmgenid,id=testvgid," "guid=auto"; + qtest_start(cmd); + + read_guid_from_memory(&measured); + + /* Just check that the GUID is non-null */ + g_assert(!qemu_uuid_is_null(&measured)); + + qtest_quit(global_qtest); +} + +static void vmgenid_query_monitor_test(void) +{ + QemuUUID expected, measured; + gchar *cmd; + + g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); + + cmd = g_strdup_printf("-machine accel=tcg -device vmgenid,id=testvgid," + "guid=%s", VGID_GUID); + qtest_start(cmd); + + /* Read the GUID via the monitor */ + read_guid_from_monitor(&measured); + g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0); + + qtest_quit(global_qtest); + g_free(cmd); +} + +int main(int argc, char **argv) +{ + int ret; + + g_test_init(&argc, &argv, NULL); + + qtest_add_func("/vmgenid/vmgenid/set-guid", + vmgenid_set_guid_test); + qtest_add_func("/vmgenid/vmgenid/set-guid-auto", + vmgenid_set_guid_auto_test); + qtest_add_func("/vmgenid/vmgenid/query-monitor", + vmgenid_query_monitor_test); + ret = g_test_run(); + + return ret; +}