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

[PATCH 1/2] tools/ocaml: check physinfo and domain_getinfolist in the bindings test



The Xenctrl C stubs build OCaml values by hand, and nothing checks that
the blocks they allocate match the types declared in xenctrl.mli. A
mismatch, e.g. a record or variant argument with a different number of
fields, isn't caught at build time.

So, extend test-ocaml-bindings to also call physinfo() and
domain_getinfolist(), check the tag and size of each returned block
against the declared type, check the flag lists, and read every field.

This covers the Arm specific parts too, i.e. arm_physinfo_caps and
xen_arm_arch_domainconfig, which were tested on arm64 under QEMU.

Signed-off-by: Julian Vetter <julian.vetter@xxxxxxxxxx>
---
 tools/ocaml/tests/bindings/main.ml | 126 ++++++++++++++++++++++++++++-
 1 file changed, 125 insertions(+), 1 deletion(-)

diff --git a/tools/ocaml/tests/bindings/main.ml 
b/tools/ocaml/tests/bindings/main.ml
index 785837e270..02049d2f79 100644
--- a/tools/ocaml/tests/bindings/main.ml
+++ b/tools/ocaml/tests/bindings/main.ml
@@ -2,18 +2,142 @@ open Printf
 
 let xch = Xenctrl.interface_open ()
 
+let nr_failures = ref 0
+
+let fail fmt =
+  ksprintf (fun s -> incr nr_failures; printf "  Fail: %s\n%!" s) fmt
+
+(* Check that [v] is a block with the given tag and number of fields. *)
+let check_block name v ~tag ~size =
+  let r = Obj.repr v in
+  if Obj.is_int r then
+    fail "%s: immediate value, expected a block" name
+  else if Obj.tag r <> tag || Obj.size r <> size then
+    fail "%s: tag %d, size %d, expected tag %d, size %d"
+      name (Obj.tag r) (Obj.size r) tag size
+
+let check_record name v ~size = check_block name v ~tag:0 ~size
+
+(* Check that [v] is one of the [nr] constant constructors of a variant. *)
+let check_enum name v ~nr =
+  let r = Obj.repr v in
+  let ok = Obj.is_int r && (Obj.obj r : int) >= 0 && (Obj.obj r : int) < nr in
+  if not ok then
+    fail "%s: not one of the %d constructors" name nr;
+  ok
+
+(* XEN_DOMCTL_CONFIG_GIC_* *)
+let string_of_gic_version = function
+  | 1 -> "GICv2"
+  | 2 -> "GICv3"
+  | v -> fail "unknown gic_version %d" v; "unknown"
+
+let string_of_cap_flag = function
+  | Xenctrl.CAP_HVM -> "hvm"
+  | Xenctrl.CAP_PV -> "pv"
+  | Xenctrl.CAP_DirectIO -> "directio"
+  | Xenctrl.CAP_HAP -> "hap"
+  | Xenctrl.CAP_Shadow -> "shadow"
+  | Xenctrl.CAP_IOMMU_HAP_PT_SHARE -> "iommu_hap_pt_share"
+  | Xenctrl.CAP_Vmtrace -> "vmtrace"
+  | Xenctrl.CAP_Vpmu -> "vpmu"
+  | Xenctrl.CAP_Gnttab_v1 -> "gnttab-v1"
+  | Xenctrl.CAP_Gnttab_v2 -> "gnttab-v2"
+
 let test_xenversion () =
   let ver = Xenctrl.version xch in
+  check_record "version" ver ~size:3;
   printf "Xen version: %d.%d%s\n" ver.major ver.minor ver.extra
 
+let test_physinfo () =
+  printf "Test physinfo\n";
+
+  let p = Xenctrl.physinfo xch in
+  check_record "physinfo" p ~size:11;
+  printf "  threads_per_core %d, cores_per_socket %d, nr_cpus %d\n"
+    p.Xenctrl.threads_per_core p.Xenctrl.cores_per_socket p.Xenctrl.nr_cpus;
+  printf "  max_node_id %d, cpu_khz %d, max_nr_cpus %d\n"
+    p.Xenctrl.max_node_id p.Xenctrl.cpu_khz p.Xenctrl.max_nr_cpus;
+  printf "  total_pages %nd, free_pages %nd, scrub_pages %nd\n"
+    p.Xenctrl.total_pages p.Xenctrl.free_pages p.Xenctrl.scrub_pages;
+
+  (* Only match on the flags once they are known to be valid constructors. *)
+  if List.for_all (fun f -> check_enum "physinfo cap flag" f ~nr:10)
+      p.Xenctrl.capabilities then
+    printf "  capabilities: %s\n"
+      (String.concat " " (List.map string_of_cap_flag p.Xenctrl.capabilities));
+
+  match p.Xenctrl.arch_capabilities with
+  | Xenctrl.ARM caps as a ->
+    check_block "arch_capabilities" a ~tag:0 ~size:1;
+    check_record "arm_physinfo_caps" caps ~size:1;
+    printf "  ARM: sve_vl %d\n" caps.Xenctrl.sve_vl
+  | Xenctrl.X86 flags as a ->
+    check_block "arch_capabilities" a ~tag:1 ~size:1;
+    printf "  x86: %d arch capability flags\n" (List.length flags)
+
+let test_domain_getinfolist () =
+  printf "Test domain_getinfolist\n";
+
+  let doms = Xenctrl.domain_getinfolist xch 0 in
+  if doms = [] then fail "no domains, expected at least d0";
+
+  let check_dom (d : Xenctrl.domaininfo) =
+    let name = sprintf "d%d" d.Xenctrl.domid in
+
+    check_record name d ~size:17;
+    if Array.length d.Xenctrl.handle <> 16 then
+      fail "%s: handle has %d bytes, expected 16"
+        name (Array.length d.Xenctrl.handle);
+
+    printf "  %s: dying %b, shutdown %b (code %d), paused %b, blocked %b, 
running %b, hvm %b\n"
+      name d.Xenctrl.dying d.Xenctrl.shutdown d.Xenctrl.shutdown_code
+      d.Xenctrl.paused d.Xenctrl.blocked d.Xenctrl.running d.Xenctrl.hvm_guest;
+    printf "  %s: pages %nd/%nd, shared_info_frame %Ld, cpu_time %Ld\n"
+      name d.Xenctrl.total_memory_pages d.Xenctrl.max_memory_pages
+      d.Xenctrl.shared_info_frame d.Xenctrl.cpu_time;
+    printf "  %s: vcpus %d (max id %d), ssidref %ld\n"
+      name d.Xenctrl.nr_online_vcpus d.Xenctrl.max_vcpu_id d.Xenctrl.ssidref;
+
+    match d.Xenctrl.arch_config with
+    | Xenctrl.ARM arm as a ->
+      check_block (name ^ " arch_config") a ~tag:0 ~size:1;
+      check_record (name ^ " xen_arm_arch_domainconfig") arm ~size:2;
+      printf "  %s: ARM %s, nr_spis %d\n" name
+        (string_of_gic_version arm.Xenctrl.gic_version) arm.Xenctrl.nr_spis
+    | Xenctrl.X86 x86 as a ->
+      check_block (name ^ " arch_config") a ~tag:1 ~size:1;
+      check_record (name ^ " xen_x86_arch_domainconfig") x86 ~size:2;
+      List.iter (fun f -> ignore (check_enum (name ^ " emulation flag") f 
~nr:11))
+        x86.Xenctrl.emulation_flags;
+      List.iter (fun f -> ignore (check_enum (name ^ " misc flag") f ~nr:1))
+        x86.Xenctrl.misc_flags;
+      printf "  %s: x86, %d emulation flags, %d misc flags\n" name
+        (List.length x86.Xenctrl.emulation_flags)
+        (List.length x86.Xenctrl.misc_flags)
+  in
+
+  List.iter check_dom doms
+
+let run name f =
+  try f () with
+  | Xenctrl.Error e -> fail "%s: Xenctrl.Error %s" name e
+
 
 let () =
   printf "Test Ocaml Bindings\n%!";
 
-  test_xenversion ();
+  run "version" test_xenversion;
+  run "physinfo" test_physinfo;
+  run "domain_getinfolist" test_domain_getinfolist;
 
   printf "Starting GC.compact()\n%!";
   Gc.compact ();
   printf "GC done\n";
 
+  if !nr_failures > 0 then begin
+    printf "%d failures\n" !nr_failures;
+    exit 1
+  end;
+
   ()
-- 
2.53.0



--
Julian Vetter | Vates Hypervisor & Kernel Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

 


Rackspace

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