[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 6/7] tools/tests/tsx: move guest creation to common area



So that other tests can reuse the creation of HVM/PV tests.

Signed-off-by: Edwin Török <edwin.torok@xxxxxxxxxx>
---
 tools/tests/common/guests.c | 87 +++++++++++++++++++++++++++++++++++++
 tools/tests/common/guests.h | 11 +++++
 tools/tests/tsx/Makefile    |  2 +
 tools/tests/tsx/test-tsx.c  | 78 ++++-----------------------------
 4 files changed, 108 insertions(+), 70 deletions(-)
 create mode 100644 tools/tests/common/guests.c
 create mode 100644 tools/tests/common/guests.h

diff --git a/tools/tests/common/guests.c b/tools/tests/common/guests.c
new file mode 100644
index 0000000000..bb33bfd8c0
--- /dev/null
+++ b/tools/tests/common/guests.c
@@ -0,0 +1,87 @@
+#define _GNU_SOURCE
+#include "guests.h"
+#include "tests.h"
+
+#include <err.h>
+#include <errno.h>
+#include <string.h>
+
+xc_interface *xch;
+xc_physinfo_t physinfo;
+bool xen_has_pv = true, xen_has_hvm = true;
+
+void test_guest_init(void)
+{
+    int rc;
+    xch = xc_interface_open(NULL, NULL, 0);
+
+    if ( !xch )
+        err(1, "xc_interface_open");
+
+    rc = xc_physinfo(xch, &physinfo);
+    if ( rc )
+        err(1, "Failed to obtain physinfo");
+
+    xen_has_hvm = physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hvm;
+    xen_has_pv = physinfo.capabilities & XEN_SYSCTL_PHYSCAP_pv;
+}
+
+void test_guest(struct xen_domctl_createdomain *c)
+{
+    uint32_t domid = 0;
+    int rc;
+
+    if (!xch)
+        return fail("test_guest_init() not called");
+
+    rc = xc_domain_create(xch, &domid, c);
+    if ( rc )
+        return fail("  Domain create failure: %d - %s\n",
+                    errno, strerror(errno));
+
+    printf("  Created d%u\n", domid);
+
+    test_guest_domid(domid);
+
+    rc = xc_domain_destroy(xch, domid);
+    if ( rc )
+        fail("  Failed to destroy domain: %d - %s\n",
+             errno, strerror(errno));
+}
+
+void test_guests(void)
+{
+    if ( xen_has_pv )
+    {
+        struct xen_domctl_createdomain c = {
+            .max_vcpus = 1,
+            .max_grant_frames = 1,
+            .grant_opts = XEN_DOMCTL_GRANT_version(1),
+        };
+
+        printf("Testing PV guest\n");
+        test_guest(&c);
+    }
+
+    if ( xen_has_hvm )
+    {
+        struct xen_domctl_createdomain c = {
+            .flags = XEN_DOMCTL_CDF_hvm,
+            .max_vcpus = 1,
+            .max_grant_frames = 1,
+            .grant_opts = XEN_DOMCTL_GRANT_version(1),
+            .arch = {
+                .emulation_flags = XEN_X86_EMU_LAPIC,
+            },
+        };
+
+        if ( physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hap )
+            c.flags |= XEN_DOMCTL_CDF_hap;
+        else if ( !(physinfo.capabilities & XEN_SYSCTL_PHYSCAP_shadow) )
+            return fail("  HVM available, but neither HAP nor Shadow\n");
+
+        printf("Testing HVM guest\n");
+        test_guest(&c);
+    }
+}
+
diff --git a/tools/tests/common/guests.h b/tools/tests/common/guests.h
new file mode 100644
index 0000000000..8cd6fc1b68
--- /dev/null
+++ b/tools/tests/common/guests.h
@@ -0,0 +1,11 @@
+#include <xenctrl.h>
+#include <xen/domctl.h>
+
+extern void test_guest_domid(domid_t domid);
+extern xc_interface *xch;
+extern xc_physinfo_t physinfo;
+extern bool xen_has_pv, xen_has_hvm;
+
+void test_guest_init(void);
+void test_guest(struct xen_domctl_createdomain *c);
+void test_guests(void);
diff --git a/tools/tests/tsx/Makefile b/tools/tests/tsx/Makefile
index 54852b0327..3ccb6dcb9f 100644
--- a/tools/tests/tsx/Makefile
+++ b/tools/tests/tsx/Makefile
@@ -11,4 +11,6 @@ CFLAGS += $(CFLAGS_libxenguest)
 LDFLAGS += $(LDLIBS_libxenctrl)
 LDFLAGS += $(LDLIBS_libxenguest)
 
+$(TARGET): ../common/guests.o
+
 include $(XEN_ROOT)/tools/tests/Rules.mk
diff --git a/tools/tests/tsx/test-tsx.c b/tools/tests/tsx/test-tsx.c
index ab099e9038..b80a812a74 100644
--- a/tools/tests/tsx/test-tsx.c
+++ b/tools/tests/tsx/test-tsx.c
@@ -31,6 +31,7 @@
 #include <xen-tools/common-macros.h>
 
 #include "tests.h"
+#include "guests.h"
 #include "xg_private.h"
 
 enum {
@@ -45,7 +46,6 @@ enum {
 #define MSR_TSX_CTRL                        0x00000122
 #define MSR_MCU_OPT_CTRL                    0x00000123
 
-static xc_interface *xch;
 
 /*
  * Policies, arranged as an array for easy collection of all of them.  We
@@ -60,10 +60,6 @@ static struct xc_cpu_policy policies[6];
 #define pv_default   policies[XEN_SYSCTL_cpu_policy_pv_default]
 #define hvm_default  policies[XEN_SYSCTL_cpu_policy_hvm_default]
 
-static bool xen_has_pv = true, xen_has_hvm = true;
-
-static xc_physinfo_t physinfo;
-
 static enum rtm_behaviour {
     RTM_UD,
     RTM_OK,
@@ -354,24 +350,16 @@ static void test_def_max_policies(void)
     }
 }
 
-static void test_guest(struct xen_domctl_createdomain *c)
+void test_guest_domid(domid_t domid)
 {
-    uint32_t domid = 0;
     int rc;
 
-    rc = xc_domain_create(xch, &domid, c);
-    if ( rc )
-        return fail("  Domain create failure: %d - %s\n",
-                    errno, strerror(errno));
-
-    printf("  Created d%u\n", domid);
-
     rc = xc_cpu_policy_get_domain(xch, domid, &guest_policy);
     if ( rc )
     {
         fail("  Failed to obtain domain policy: %d - %s\n",
              errno, strerror(errno));
-        goto out;
+        return;
     }
 
     dump_tsx_details(&guest_policy.policy, "Cur:");
@@ -408,7 +396,7 @@ static void test_guest(struct xen_domctl_createdomain *c)
         {
             fail("  Failed to set domain policy: %d - %s\n",
                  errno, strerror(errno));
-            goto out;
+            return;
         }
 
         /* Re-get the new policy. */
@@ -417,7 +405,7 @@ static void test_guest(struct xen_domctl_createdomain *c)
         {
             fail("  Failed to obtain domain policy: %d - %s\n",
                  errno, strerror(errno));
-            goto out;
+            return;
         }
 
         dump_tsx_details(&guest_policy.policy, "Cur:");
@@ -426,58 +414,17 @@ static void test_guest(struct xen_domctl_createdomain *c)
         {
             fail("  Expected CPUID.7[0].b 0x%08x differs from actual 0x%08x\n",
                  _7b0, guest_policy.policy.feat.raw[0].b);
-            goto out;
+            return;
         }
 
         if ( guest_policy.policy.feat.raw[0].d != _7d0 )
         {
             fail("  Expected CPUID.7[0].d 0x%08x differs from actual 0x%08x\n",
                  _7d0, guest_policy.policy.feat.raw[0].d);
-            goto out;
+            return;
         }
     }
 
- out:
-    rc = xc_domain_destroy(xch, domid);
-    if ( rc )
-        fail("  Failed to destroy domain: %d - %s\n",
-             errno, strerror(errno));
-}
-
-static void test_guests(void)
-{
-    if ( xen_has_pv )
-    {
-        struct xen_domctl_createdomain c = {
-            .max_vcpus = 1,
-            .max_grant_frames = 1,
-            .grant_opts = XEN_DOMCTL_GRANT_version(1),
-        };
-
-        printf("Testing PV guest\n");
-        test_guest(&c);
-    }
-
-    if ( xen_has_hvm )
-    {
-        struct xen_domctl_createdomain c = {
-            .flags = XEN_DOMCTL_CDF_hvm,
-            .max_vcpus = 1,
-            .max_grant_frames = 1,
-            .grant_opts = XEN_DOMCTL_GRANT_version(1),
-            .arch = {
-                .emulation_flags = XEN_X86_EMU_LAPIC,
-            },
-        };
-
-        if ( physinfo.capabilities & XEN_SYSCTL_PHYSCAP_hap )
-            c.flags |= XEN_DOMCTL_CDF_hap;
-        else if ( !(physinfo.capabilities & XEN_SYSCTL_PHYSCAP_shadow) )
-            return fail("  HVM available, but neither HAP nor Shadow\n");
-
-        printf("Testing HVM guest\n");
-        test_guest(&c);
-    }
 }
 
 /* Obtain some general data, then run the tests. */
@@ -521,11 +468,6 @@ static void test_tsx(void)
 
     dump_tsx_details(&host.policy, "Host:");
 
-    rc = xc_physinfo(xch, &physinfo);
-    if ( rc )
-        return fail("Failed to obtain physinfo: %d - %s\n",
-                    errno, strerror(errno));
-
     printf("  Got %u CPUs\n", physinfo.max_cpu_id + 1);
 
     test_tsx_msrs();
@@ -538,11 +480,7 @@ int test_main(int argc, char *argv[argc+1])
 {
     printf("TSX tests\n");
 
-    xch = xc_interface_open(NULL, NULL, 0);
-
-    if ( !xch )
-        err(1, "xc_interface_open");
-
+    test_guest_init();
     test_tsx();
 
     return !!nr_failures;
-- 
2.47.3




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.