[PATCH v2 2/3] ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo

Andrii Sultanov posted 3 patches 3 months ago
There is a newer version of this series
[PATCH v2 2/3] ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo
Posted by Andrii Sultanov 3 months ago
This plugin intends to hide the unstable Xenctrl interface under a
stable one. In case of the change in the interface, a V2 of this plugin
would need to be produced, but V1 with the old interface would
need to be kept (with potential change in the implementation) in the
meantime.

To reduce the need for such changes in the future, this plugin only
provides the absolute minimum functionality that Oxenstored uses - only
three fields of the domaininfo struct are used and presented here.

Oxenstored currently uses the single-domain domain_getinfo function,
whereas Cxenstored uses the more-efficient domain_getinfolist. Both of
these are provided in the plugin to allow a transition from one to the
other without modifying the interface in the future. Both return
identical structures and rely on the same fields in xenctrl, thus if one
of them breaks, both will break, and a new version of the interface would
need to be issued.

Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
---
 tools/ocaml/Makefile                          |   1 +
 tools/ocaml/libs/Makefile                     |   2 +-
 tools/ocaml/libs/xenstoredglue/META.in        |   4 +
 tools/ocaml/libs/xenstoredglue/Makefile       |  46 +++++
 .../domain_getinfo_plugin_v1/META.in          |   5 +
 .../domain_getinfo_plugin_v1/Makefile         |  38 ++++
 .../domain_getinfo_stubs_v1.c                 | 172 ++++++++++++++++++
 .../domain_getinfo_v1.ml                      |  36 ++++
 .../domain_getinfo_v1.mli                     |   0
 .../libs/xenstoredglue/plugin_interface_v1.ml |  28 +++
 .../xenstoredglue/plugin_interface_v1.mli     |  36 ++++
 11 files changed, 367 insertions(+), 1 deletion(-)
 create mode 100644 tools/ocaml/libs/xenstoredglue/META.in
 create mode 100644 tools/ocaml/libs/xenstoredglue/Makefile
 create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in
 create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
 create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
 create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml
 create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli
 create mode 100644 tools/ocaml/libs/xenstoredglue/plugin_interface_v1.ml
 create mode 100644 tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli

diff --git a/tools/ocaml/Makefile b/tools/ocaml/Makefile
index 1557fd6c3c..eb426f2ee5 100644
--- a/tools/ocaml/Makefile
+++ b/tools/ocaml/Makefile
@@ -29,6 +29,7 @@ build-tools-oxenstored:
 	$(MAKE) -s -C libs/mmap
 	$(MAKE) -s -C libs/xb
 	$(MAKE) -s -C libs/xc
+	$(MAKE) -s -C libs/xenstoredglue
 	$(MAKE) -C xenstored
 
 .PHONY: format
diff --git a/tools/ocaml/libs/Makefile b/tools/ocaml/libs/Makefile
index 89350aa12f..828fbf859d 100644
--- a/tools/ocaml/libs/Makefile
+++ b/tools/ocaml/libs/Makefile
@@ -4,7 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS= \
 	mmap \
 	eventchn xc \
-	xb xs
+	xb xs xenstoredglue
 
 .PHONY: all
 all: subdirs-all
diff --git a/tools/ocaml/libs/xenstoredglue/META.in b/tools/ocaml/libs/xenstoredglue/META.in
new file mode 100644
index 0000000000..dbd584ac17
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/META.in
@@ -0,0 +1,4 @@
+version = "@VERSION@"
+description = "A small library on top of unstable Xenctrl interfaces used by Oxenstored"
+archive(byte) = "plugin_interface_v1.cma"
+archive(native) = "plugin_interface_v1.cmxa"
diff --git a/tools/ocaml/libs/xenstoredglue/Makefile b/tools/ocaml/libs/xenstoredglue/Makefile
new file mode 100644
index 0000000000..95818cdf83
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/Makefile
@@ -0,0 +1,46 @@
+OCAML_TOPLEVEL=$(CURDIR)/../..
+XEN_ROOT=$(OCAML_TOPLEVEL)/../..
+include $(OCAML_TOPLEVEL)/common.make
+
+SUBDIRS= domain_getinfo_plugin_v1
+
+CFLAGS += $(CFLAGS_xeninclude)
+OCAMLOPTFLAGS += -opaque
+
+OBJS = plugin_interface_v1
+INTF = $(foreach obj, $(OBJS),$(obj).cmi)
+LIBS = plugin_interface_v1.cma plugin_interface_v1.cmxa
+LIBS_plugin_interface_v1 =
+plugin_interface_v1_OBJS=$(OBJS)
+
+.PHONY: all
+all: $(INTF) $(LIBS) $(PROGRAMS) subdirs-all
+
+bins: $(PROGRAMS)
+
+libs: $(LIBS)
+
+plugin_interface_v1 = $(OBJS)
+
+OCAML_NOC_LIBRARY = plugin_interface_v1
+
+.PHONY: install
+install: $(LIBS) META subdirs-install
+	mkdir -p $(OCAMLDESTDIR)
+	$(OCAMLFIND) remove -destdir $(OCAMLDESTDIR) xenstored_glue
+	$(OCAMLFIND) install -destdir $(OCAMLDESTDIR) -ldconf ignore xenstored_glue META $(INTF) $(LIBS)
+	$(OCAMLFIND) remove -destdir $(OCAMLDESTDIR) xenstored_glue_dev
+	$(OCAMLFIND) install -destdir $(OCAMLDESTDIR) -ldconf ignore xenstored_glue_dev META $(INTF) $(LIBS) *.ml *.mli
+
+.PHONY: uninstall
+uninstall: subdirs-uninstall
+	$(OCAMLFIND) remove -destdir $(OCAMLDESTDIR) xenstored_glue
+	$(OCAMLFIND) remove -destdir $(OCAMLDESTDIR) xenstored_glue_dev
+
+.PHONY: clean
+clean: subdirs-clean
+
+.PHONY: distclean
+distclean: subdirs-distclean
+
+include $(OCAML_TOPLEVEL)/Makefile.rules
diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in
new file mode 100644
index 0000000000..fb917def62
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in
@@ -0,0 +1,5 @@
+version = "@VERSION@"
+description = "Xenstored plugin for Xenctrl.domain_getinfo unstable interface - V1"
+requires = "plugin_interface_v1"
+archive(byte) = "domain_getinfo_v1.cma"
+archive(native) = "domain_getinfo_v1.cmxa"
diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
new file mode 100644
index 0000000000..9c40026cab
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
@@ -0,0 +1,38 @@
+OCAML_TOPLEVEL=$(CURDIR)/../../..
+XEN_ROOT=$(OCAML_TOPLEVEL)/../..
+include $(OCAML_TOPLEVEL)/common.make
+
+CFLAGS += -I $(OCAML_TOPLEVEL)/libs -I $(OCAML_TOPLEVEL)/libs/xenstoredglue
+CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude) $(APPEND_CFLAGS)
+OCAMLOPTFLAGS += -opaque
+OCAMLINCLUDE += -I ./ -I ../
+
+OBJS = domain_getinfo_v1
+INTF = $(foreach obj, $(OBJS),$(obj).cmi)
+LIBS = domain_getinfo_v1.cmxa domain_getinfo_v1.cmxs
+
+LIBS_xenstoredglue = $(call xenlibs-ldflags-ldlibs,xenctrl)
+
+all: $(INTF) $(LIBS) $(PROGRAMS)
+
+bins: $(PROGRAMS)
+
+libs: $(LIBS)
+
+domain_getinfo_v1_OBJS = $(OBJS)
+domain_getinfo_v1 = $(OBJS)
+domain_getinfo_v1_C_OBJS = domain_getinfo_stubs_v1
+
+OCAML_DYN_LIBRARY = domain_getinfo_v1
+
+.PHONY: install
+install: $(LIBS) META
+	$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC_BIN)/xenstored_glue/xenctrl_plugin
+	$(INSTALL_PROG) domain_getinfo_v1.cmxs $(DESTDIR)$(LIBEXEC_BIN)/xenstored_glue/xenctrl_plugin
+
+.PHONY: uninstall
+uninstall:
+	rm -f $(DESTDIR)/xenstored_glue/xenctrl_plugin/domain_getinfo_v1.cmxs
+
+include $(OCAML_TOPLEVEL)/Makefile.rules
+
diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
new file mode 100644
index 0000000000..69eddd6412
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
@@ -0,0 +1,172 @@
+#define _GNU_SOURCE
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#define CAML_NAME_SPACE
+#include <caml/alloc.h>
+#include <caml/memory.h>
+#include <caml/signals.h>
+#include <caml/fail.h>
+#include <caml/callback.h>
+#include <caml/custom.h>
+
+#include <xen-tools/common-macros.h>
+#include <xenctrl.h>
+
+#include "xen-caml-compat.h"
+
+#define ERR_MSG_LEN (XC_MAX_ERROR_MSG_LEN + 6)
+#define MAX_FUNC_LINE_LEN 64
+#define failwith_xc_v1(xch) xsglue_failwith_xc(xch, __FUNCTION__, __LINE__)
+
+/* This is a minimal stub to xenctrl for oxenstored's purposes
+   For the full xenctrl stubs, see tools/ocaml/libs/xc/xenctrl_stubs.c */
+
+static inline xc_interface *xsglue_xch_of_val_v1(value v)
+{
+	xc_interface *xch = *(xc_interface **)Data_custom_val(v);
+
+	return xch;
+}
+
+static void xsglue_xenctrl_finalize(value v)
+{
+	xc_interface *xch = xsglue_xch_of_val_v1(v);
+
+	xc_interface_close(xch);
+}
+
+static struct custom_operations xsglue_xenctrl_ops = {
+	.identifier  = "xenctrl",
+	.finalize    = xsglue_xenctrl_finalize,
+	.compare     = custom_compare_default,     /* Can't compare     */
+	.hash        = custom_hash_default,        /* Can't hash        */
+	.serialize   = custom_serialize_default,   /* Can't serialize   */
+	.deserialize = custom_deserialize_default, /* Can't deserialize */
+	.compare_ext = custom_compare_ext_default, /* Can't compare     */
+};
+
+static void Noreturn xsglue_failwith_xc(xc_interface *xch,
+		const char* func,
+		unsigned int line)
+{
+	const xc_error *error = xch ? xc_get_last_error(xch) : NULL;
+        char *str = NULL;
+	CAMLparam0();
+        CAMLlocal1(msg);
+
+#define ERR (error && error->code != XC_ERROR_NONE)
+
+        int ret = asprintf(&str,
+                           "%d: %s%s%s - called from %s:%u",
+                           ERR ? error->code : errno,
+                           ERR ? xc_error_code_to_desc(error->code) : strerror(errno),
+                           ERR ? ": " : "",
+                           ERR ? error->message : "",
+                           func, line);
+
+#undef ERR
+
+        if (!*str || (ret == -1))
+                caml_raise_out_of_memory();
+
+        msg = caml_copy_string(str);
+        free(str);
+
+        caml_raise_with_arg(*caml_named_value("xsg.error_v1"), msg);
+}
+
+CAMLprim value stub_xsglue_xc_interface_open(value unit)
+{
+	CAMLparam1(unit);
+	CAMLlocal1(result);
+	xc_interface *xch;
+
+	result = caml_alloc_custom(&xsglue_xenctrl_ops, sizeof(xch), 0, 1);
+
+	caml_enter_blocking_section();
+	xch = xc_interface_open(NULL, NULL, 0);
+	caml_leave_blocking_section();
+
+	if (!xch)
+		failwith_xc_v1(xch);
+
+	*(xc_interface **)Data_custom_val(result) = xch;
+
+	CAMLreturn(result);
+}
+
+static value xsglue_alloc_domaininfo_v1(const xc_domaininfo_t *info)
+{
+	CAMLparam0();
+	CAMLlocal1(result);
+	result = caml_alloc_tuple(4);
+
+	Store_field(result,  0, Val_int(info->domain));
+	Store_field(result,  1, Val_bool(info->flags & XEN_DOMINF_dying));
+	Store_field(result,  2, Val_bool(info->flags & XEN_DOMINF_shutdown));
+	Store_field(result,  3, Val_int(MASK_EXTR(info->flags, XEN_DOMINF_shutdownmask)));
+
+	CAMLreturn(result);
+}
+
+CAMLprim value stub_xsglue_xc_domain_getinfo(value xch_val, value domid)
+{
+	CAMLparam2(xch_val, domid);
+	CAMLlocal1(result);
+	xc_interface *xch = xsglue_xch_of_val_v1(xch_val);
+	xc_domaininfo_t info;
+	int ret, domid_c;
+
+	domid_c = Int_val(domid);
+	caml_enter_blocking_section();
+	ret = xc_domain_getinfo_single(xch, domid_c, &info);
+	caml_leave_blocking_section();
+
+	if (ret < 0)
+		failwith_xc_v1(xch);
+
+	result = xsglue_alloc_domaininfo_v1(&info);
+
+	CAMLreturn(result);
+}
+
+CAMLprim value stub_xsglue_xc_domain_getinfolist(value xch_val, value first_domain)
+{
+	CAMLparam2(xch_val, first_domain);
+	CAMLlocal1(result);
+	xc_interface *xch = xsglue_xch_of_val_v1(xch_val);
+	xc_domaininfo_t *info;
+	int i, ret, toalloc, retval;
+	uint32_t num_domains;
+	uint32_t c_first_domain;
+
+	/* get the minimum number of allocate byte we need and bump it up to page boundary */
+	c_first_domain = Int_val(first_domain);
+	num_domains = DOMID_FIRST_RESERVED - c_first_domain;
+	toalloc = (sizeof(xc_domaininfo_t) * num_domains) | 0xfff;
+	ret = posix_memalign((void **) ((void *) &info), 4096, toalloc);
+	if (ret)
+		caml_raise_out_of_memory();
+
+	caml_enter_blocking_section();
+	retval = xc_domain_getinfolist(xch, c_first_domain, num_domains, info);
+	caml_leave_blocking_section();
+
+	if (retval < 0) {
+		free(info);
+		failwith_xc_v1(xch);
+	} else if (retval > 0) {
+		result = caml_alloc(retval, 0);
+		for (i = 0; i < retval; i++) {
+			caml_modify(&Field(result, i), xsglue_alloc_domaininfo_v1(info + i));
+		}
+	} else {
+		result = Atom(0);
+	}
+
+	free(info);
+	CAMLreturn(result);
+}
diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml
new file mode 100644
index 0000000000..6f282e4257
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml
@@ -0,0 +1,36 @@
+(** Minimal interface on top of unstable Xenctrl for Oxenstored's usage *)
+
+(** For the full Xenctrl interface, see: tools/ocaml/libs/xc/ *)
+
+module P = Plugin_interface_v1
+
+module M : P.Domain_getinfo_V1 = struct
+  exception Error of string
+
+  type domid = int
+  type handle
+
+  type domaininfo = {
+    domid : domid;
+    dying : bool;
+    shutdown : bool;
+    shutdown_code : int;
+  }
+
+  external interface_open : unit -> handle = "stub_xsglue_xc_interface_open"
+
+  external domain_getinfo : handle -> domid -> domaininfo
+    = "stub_xsglue_xc_domain_getinfo"
+
+  external domain_getinfolist : handle -> domid -> domaininfo array
+    = "stub_xsglue_xc_domain_getinfolist"
+
+  let _ = Callback.register_exception "xsg.error_v1" (Error "register_callback")
+end
+
+let () =
+  Printf.ksprintf !P.logging_function "Registration of %s plugin started\n%!"
+    __MODULE__;
+  P.register_plugin_v1 (module M : P.Domain_getinfo_V1);
+  Printf.ksprintf !P.logging_function "Registration of %s plugin successful\n%!"
+    __MODULE__
diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.ml b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.ml
new file mode 100644
index 0000000000..131b29d3ee
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.ml
@@ -0,0 +1,28 @@
+module type Domain_getinfo_V1 = sig
+  exception Error of string
+
+  type domid = int
+  type handle
+
+  type domaininfo = {
+    domid : domid;
+    dying : bool;
+    shutdown : bool;
+    shutdown_code : int;
+  }
+
+  val interface_open : unit -> handle
+  val domain_getinfo : handle -> domid -> domaininfo
+  val domain_getinfolist : handle -> domid -> domaininfo array
+end
+
+let ignore_logging : string -> unit = ignore
+let logging_function = ref ignore_logging
+let register_logging_function func = logging_function := func
+let plugin_implementation_v1 : (module Domain_getinfo_V1) option ref = ref None
+let register_plugin_v1 m = plugin_implementation_v1 := Some m
+
+let get_plugin_v1 () : (module Domain_getinfo_V1) =
+  match !plugin_implementation_v1 with
+  | Some s -> s
+  | None -> failwith "No plugin loaded"
diff --git a/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
new file mode 100644
index 0000000000..d073a44d0f
--- /dev/null
+++ b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
@@ -0,0 +1,36 @@
+(** To avoid breaking the plugin interface, this module needs to be
+    standalone and can't rely on any other Xen library. Even unrelated
+    changes in the interfaces of those modules would change the hash
+    of this interface and break the plugin system.
+    It can only depend on Stdlib, therefore all of the types (domid,
+    domaininfo etc.) are redefined here instead of using alternatives
+    defined elsewhere.
+
+    NOTE: The signature of this interface should not be changed (no
+    functions or types can be added, modified, or removed). If
+    underlying Xenctrl changes require a new interface, a V2 with a
+    corresponding plugin should be created.
+ *)
+
+module type Domain_getinfo_V1 = sig
+  exception Error of string
+
+  type domid = int
+  type handle
+
+  type domaininfo = {
+    domid : domid;
+    dying : bool;
+    shutdown : bool;
+    shutdown_code : int;
+  }
+
+  val interface_open : unit -> handle
+  val domain_getinfo : handle -> domid -> domaininfo
+  val domain_getinfolist : handle -> domid -> domaininfo array
+end
+
+val register_logging_function : (string -> unit) -> unit
+val logging_function : (string -> unit) ref
+val register_plugin_v1 : (module Domain_getinfo_V1) -> unit
+val get_plugin_v1 : unit -> (module Domain_getinfo_V1)
-- 
2.39.2
Re: [PATCH v2 2/3] ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo
Posted by Andrew Cooper 2 months, 4 weeks ago
On 03/09/2024 12:44 pm, Andrii Sultanov wrote:
>  tools/ocaml/Makefile                          |   1 +
>  tools/ocaml/libs/Makefile                     |   2 +-
>  tools/ocaml/libs/xenstoredglue/META.in        |   4 +
>  tools/ocaml/libs/xenstoredglue/Makefile       |  46 +++++
>  .../domain_getinfo_plugin_v1/META.in          |   5 +
>  .../domain_getinfo_plugin_v1/Makefile         |  38 ++++
>  .../domain_getinfo_stubs_v1.c                 | 172 ++++++++++++++++++
>  .../domain_getinfo_v1.ml                      |  36 ++++
>  .../domain_getinfo_v1.mli                     |   0
>  .../libs/xenstoredglue/plugin_interface_v1.ml |  28 +++
>  .../xenstoredglue/plugin_interface_v1.mli     |  36 ++++
>  11 files changed, 367 insertions(+), 1 deletion(-)
>  create mode 100644 tools/ocaml/libs/xenstoredglue/META.in
>  create mode 100644 tools/ocaml/libs/xenstoredglue/Makefile
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli
>  create mode 100644 tools/ocaml/libs/xenstoredglue/plugin_interface_v1.ml
>  create mode 100644 tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli

Only noticed after some trial builds.

This patch should include tools/ocaml/libs/xenstoredglue/.gitignore
which causes these paths:

tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/.ocamldep.make
tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.annot
tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.cmi
tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.cmx
tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.cmxa
tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.cmxs

to get ignored.  Although, I don't see any local gitignores, so I
suspect it's still the root .gitignore which wants adjusting.

I'll see about doing another cleanup patch.

~Andrew

Re: [PATCH v2 2/3] ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo
Posted by Andrew Cooper 2 months, 4 weeks ago
On 03/09/2024 12:44 pm, Andrii Sultanov wrote:
> This plugin intends to hide the unstable Xenctrl interface under a
> stable one. In case of the change in the interface, a V2 of this plugin
> would need to be produced, but V1 with the old interface would
> need to be kept (with potential change in the implementation) in the
> meantime.
>
> To reduce the need for such changes in the future, this plugin only
> provides the absolute minimum functionality that Oxenstored uses - only
> three fields of the domaininfo struct are used and presented here.
>
> Oxenstored currently uses the single-domain domain_getinfo function,
> whereas Cxenstored uses the more-efficient domain_getinfolist. Both of
> these are provided in the plugin to allow a transition from one to the
> other without modifying the interface in the future. Both return
> identical structures and rely on the same fields in xenctrl, thus if one
> of them breaks, both will break, and a new version of the interface would
> need to be issued.
>
> Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
> ---

Normally you should put a short set of notes here (under --- in the
commit message) about what has changed from the prior version you posted.

>  tools/ocaml/Makefile                          |   1 +
>  tools/ocaml/libs/Makefile                     |   2 +-
>  tools/ocaml/libs/xenstoredglue/META.in        |   4 +
>  tools/ocaml/libs/xenstoredglue/Makefile       |  46 +++++
>  .../domain_getinfo_plugin_v1/META.in          |   5 +
>  .../domain_getinfo_plugin_v1/Makefile         |  38 ++++
>  .../domain_getinfo_stubs_v1.c                 | 172 ++++++++++++++++++
>  .../domain_getinfo_v1.ml                      |  36 ++++
>  .../domain_getinfo_v1.mli                     |   0
>  .../libs/xenstoredglue/plugin_interface_v1.ml |  28 +++
>  .../xenstoredglue/plugin_interface_v1.mli     |  36 ++++
>  11 files changed, 367 insertions(+), 1 deletion(-)
>  create mode 100644 tools/ocaml/libs/xenstoredglue/META.in
>  create mode 100644 tools/ocaml/libs/xenstoredglue/Makefile
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml
>  create mode 100644 tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli
>  create mode 100644 tools/ocaml/libs/xenstoredglue/plugin_interface_v1.ml
>  create mode 100644 tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli

We still have a mix of names even within this patch.  xenstoredglue,
xenstored_glue, xsglue

Could we see about using xsd_glue as the top level name here, to get it
a bit shorter and easier to read?

> diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> new file mode 100644
> index 0000000000..9c40026cab
> --- /dev/null
> +++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> @@ -0,0 +1,38 @@
> +OCAML_TOPLEVEL=$(CURDIR)/../../..
> +XEN_ROOT=$(OCAML_TOPLEVEL)/../..
> +include $(OCAML_TOPLEVEL)/common.make
> +
> +CFLAGS += -I $(OCAML_TOPLEVEL)/libs -I $(OCAML_TOPLEVEL)/libs/xenstoredglue
> +CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude) $(APPEND_CFLAGS)
> +OCAMLOPTFLAGS += -opaque
> +OCAMLINCLUDE += -I ./ -I ../

I think we only need "-I ../" here.  xen-caml-compat.h is the only local
header used.

> diff --git a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
> new file mode 100644
> index 0000000000..69eddd6412
> --- /dev/null
> +++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
> @@ -0,0 +1,172 @@

Sorry for missing this before, but new files want to contain SDPX
headers.  For this,

/* SPDX-License-Identifier: LGPL-2.1-only WITH
OCaml-LGPL-linking-exception */

which I had to go and research when sorting out xen-caml-compat.h


For Ocaml files, suppose we want.

(* SPDX-License-Identifier: LGPL-2.1-only WITH
OCaml-LGPL-linking-exception *)


The SPDX header should always be the first line of the file.

> +#define _GNU_SOURCE
> +
> +#include <stdlib.h>
> +#include <string.h>
> +#include <errno.h>
> +
> +#define CAML_NAME_SPACE
> +#include <caml/alloc.h>
> +#include <caml/memory.h>
> +#include <caml/signals.h>
> +#include <caml/fail.h>
> +#include <caml/callback.h>
> +#include <caml/custom.h>
> +
> +#include <xen-tools/common-macros.h>
> +#include <xenctrl.h>
> +
> +#include "xen-caml-compat.h"
> +
> +#define ERR_MSG_LEN (XC_MAX_ERROR_MSG_LEN + 6)
> +#define MAX_FUNC_LINE_LEN 64

These two are obsolete given the rework of xsglue_failwith_xc()

> +#define failwith_xc_v1(xch) xsglue_failwith_xc(xch, __FUNCTION__, __LINE__)

This should be moved lower and adjusted.  See later.

> +
> +/* This is a minimal stub to xenctrl for oxenstored's purposes
> +   For the full xenctrl stubs, see tools/ocaml/libs/xc/xenctrl_stubs.c */

These comments aren't helpful IMO.  It's said many times elsewhere.

> +
> +static inline xc_interface *xsglue_xch_of_val_v1(value v)

static functions don't a _v1 suffix, seeing as they're local to a file
with a _v1 in it's name.

> +{
> +	xc_interface *xch = *(xc_interface **)Data_custom_val(v);
> +
> +	return xch;
> +}
> +
> +static void xsglue_xenctrl_finalize(value v)
> +{
> +	xc_interface *xch = xsglue_xch_of_val_v1(v);
> +
> +	xc_interface_close(xch);
> +}
> +
> +static struct custom_operations xsglue_xenctrl_ops = {
> +	.identifier  = "xenctrl",

I presume this identifier gets elsewhere?  Perhaps
"xsd_glue.domain_getinfo_v1.xenctrl" or so?

> +	.finalize    = xsglue_xenctrl_finalize,
> +	.compare     = custom_compare_default,     /* Can't compare     */
> +	.hash        = custom_hash_default,        /* Can't hash        */
> +	.serialize   = custom_serialize_default,   /* Can't serialize   */
> +	.deserialize = custom_deserialize_default, /* Can't deserialize */
> +	.compare_ext = custom_compare_ext_default, /* Can't compare     */
> +};
> +

There's a trick with the C preprocessor where you can

#define xsglue_failwith(xch) xsglue_failwith(xch, __func__, __LINE__)

to add extra arguments to callsites.  The only requirement is that it
either needs to be after the function of the same name, or that you
define the function using:

static void Noreturn (xsglue_failwith)(xc_interface *xch ...)


I'd put the macro and the declaration together here.  Also, use __func__
rather than __FUNCTION__.

> +static void Noreturn xsglue_failwith_xc(xc_interface *xch,
> +		const char* func,
> +		unsigned int line)

The _xc() suffix isn't very helpful when there's also an xsglue_ prefix.

I'd simply name this xsglue_failwith(...) which is clear enough when used.

Also, 'const char *fun' with the * to the right of the space.

> +{
> +	const xc_error *error = xch ? xc_get_last_error(xch) : NULL;
> +        char *str = NULL;
> +	CAMLparam0();
> +        CAMLlocal1(msg);

Mixing tabs and spaces.

> <snip>
>
> +static value xsglue_alloc_domaininfo_v1(const xc_domaininfo_t *info)
> +{
> +	CAMLparam0();
> +	CAMLlocal1(result);

We try to split declarations from regular logic, so a blank line here
please.

> +	result = caml_alloc_tuple(4);
> +
> +	Store_field(result,  0, Val_int(info->domain));
> +	Store_field(result,  1, Val_bool(info->flags & XEN_DOMINF_dying));
> +	Store_field(result,  2, Val_bool(info->flags & XEN_DOMINF_shutdown));
> +	Store_field(result,  3, Val_int(MASK_EXTR(info->flags, XEN_DOMINF_shutdownmask)));
> +
> +	CAMLreturn(result);
> +}
> +
> +CAMLprim value stub_xsglue_xc_domain_getinfo(value xch_val, value domid)
> +{
> +	CAMLparam2(xch_val, domid);
> +	CAMLlocal1(result);
> +	xc_interface *xch = xsglue_xch_of_val_v1(xch_val);
> +	xc_domaininfo_t info;
> +	int ret, domid_c;
> +
> +	domid_c = Int_val(domid);

I'd suggests a blank line here, or to initialise domid_c at declaration.

> +	caml_enter_blocking_section();
> +	ret = xc_domain_getinfo_single(xch, domid_c, &info);
> +	caml_leave_blocking_section();
> +
> +	if (ret < 0)
> +		failwith_xc_v1(xch);
> +
> +	result = xsglue_alloc_domaininfo_v1(&info);
> +
> +	CAMLreturn(result);
> +}
> +
> +CAMLprim value stub_xsglue_xc_domain_getinfolist(value xch_val, value first_domain)
> +{
> +	CAMLparam2(xch_val, first_domain);
> +	CAMLlocal1(result);
> +	xc_interface *xch = xsglue_xch_of_val_v1(xch_val);
> +	xc_domaininfo_t *info;
> +	int i, ret, toalloc, retval;
> +	uint32_t num_domains;
> +	uint32_t c_first_domain;
> +
> +	/* get the minimum number of allocate byte we need and bump it up to page boundary */
> +	c_first_domain = Int_val(first_domain);

Passing a first_domain of anything other than 0 breaks the atomicity
that we're trying to create by asking for all domains together.

It wants dropping from here, and the plugin interface.

> +	num_domains = DOMID_FIRST_RESERVED - c_first_domain;
> +	toalloc = (sizeof(xc_domaininfo_t) * num_domains) | 0xfff;
> +	ret = posix_memalign((void **) ((void *) &info), 4096, toalloc);

This is nonsense, and appears to have been so for ages in the Xenctrl stubs.

xc_domain_getinfolist() bounce-buffers the array anyway (to get
hypercall-safe buffers from the kernel), so there's no point doing any
local trickery.  Simply:

    info = malloc(sizeof(xc_domaininfo_t) * DOMID_FIRST_RESERVED);
    if ( !info )
        caml_raise_out_of_memory();

will be fine.

> +	if (ret)
> +		caml_raise_out_of_memory();
> +
> +	caml_enter_blocking_section();
> +	retval = xc_domain_getinfolist(xch, c_first_domain, num_domains, info);
> +	caml_leave_blocking_section();
> +
> +	if (retval < 0) {
> +		free(info);
> +		failwith_xc_v1(xch);
> +	} else if (retval > 0) {
> +		result = caml_alloc(retval, 0);
> +		for (i = 0; i < retval; i++) {
> +			caml_modify(&Field(result, i), xsglue_alloc_domaininfo_v1(info + i));
> +		}
> +	} else {
> +		result = Atom(0);
> +	}

While this works, there are better ways of writing the logic. 
failwith() is Noreturn, so it's easier to follow as:

    if (retval < 0) {
        ...
    }

    if (retval > 0) {
        ...

but...  You're dom0, asking for all domains.  Getting a retval of 0 is
also some kind of error, so I'd suggest:

    if (retval <= 0) {
        free(info);
        xsglue_failwith(xch);
    }

    result = caml_alloc(retval, 0);
    for (i = 0; i < retval; i++) {
        caml_modify(&Field(result, i), xsglue_alloc_domaininfo_v1(info +
i));
    }

    free(info);
    Camlreturn(result);

which is simpler still.


> diff --git a/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> new file mode 100644
> index 0000000000..d073a44d0f
> --- /dev/null
> +++ b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> @@ -0,0 +1,36 @@
> +(** To avoid breaking the plugin interface, this module needs to be
> +    standalone and can't rely on any other Xen library. Even unrelated
> +    changes in the interfaces of those modules would change the hash
> +    of this interface and break the plugin system.
> +    It can only depend on Stdlib, therefore all of the types (domid,
> +    domaininfo etc.) are redefined here instead of using alternatives
> +    defined elsewhere.
> +
> +    NOTE: The signature of this interface should not be changed (no
> +    functions or types can be added, modified, or removed). If
> +    underlying Xenctrl changes require a new interface, a V2 with a
> +    corresponding plugin should be created.
> + *)

There's a rune to run ocp-indent in the Xen tree, in lieu of the full
Ocaml dev stack.

make -C tools/ocaml format

and the delta for this patch is just:

--- a/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
+++ b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
@@ -10,7 +10,7 @@
     functions or types can be added, modified, or removed). If
     underlying Xenctrl changes require a new interface, a V2 with a
     corresponding plugin should be created.
- *)
+*)
 
 module type Domain_getinfo_V1 = sig
   exception Error of string

~Andrew

Re: [PATCH v2 2/3] ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo
Posted by Andrii Sultanov 2 months, 4 weeks ago
> > diff --git
a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> > new file mode 100644
> > index 0000000000..9c40026cab
> > --- /dev/null
> > +++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> > @@ -0,0 +1,38 @@
> > +OCAML_TOPLEVEL=$(CURDIR)/../../..
> > +XEN_ROOT=$(OCAML_TOPLEVEL)/../..
> > +include $(OCAML_TOPLEVEL)/common.make
> > +
> > +CFLAGS += -I $(OCAML_TOPLEVEL)/libs -I
$(OCAML_TOPLEVEL)/libs/xenstoredglue
> > +CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude) $(APPEND_CFLAGS)
> > +OCAMLOPTFLAGS += -opaque
> > +OCAMLINCLUDE += -I ./ -I ../
>
> I think we only need "-I ../" here.  xen-caml-compat.h is the only local
> header used.

With only "-I ../", the build fails:
```
ocamlopt -g -ccopt "   " -dtypes -I ../ -w F -warn-error F -opaque -shared
-linkall -o domain_getinfo_v1.cmxs  domain_getinfo_v1.cmxa
/usr/bin/ld: cannot find -ldomain_getinfo_v1_stubs: No such file or
directory
collect2: error: ld returned 1 exit status
```

Thank you very much for the rest, I will submit a new version of the patch
series shortly.

On Fri, Sep 6, 2024 at 2:38 PM Andrew Cooper <andrew.cooper3@citrix.com>
wrote:

> On 03/09/2024 12:44 pm, Andrii Sultanov wrote:
> > This plugin intends to hide the unstable Xenctrl interface under a
> > stable one. In case of the change in the interface, a V2 of this plugin
> > would need to be produced, but V1 with the old interface would
> > need to be kept (with potential change in the implementation) in the
> > meantime.
> >
> > To reduce the need for such changes in the future, this plugin only
> > provides the absolute minimum functionality that Oxenstored uses - only
> > three fields of the domaininfo struct are used and presented here.
> >
> > Oxenstored currently uses the single-domain domain_getinfo function,
> > whereas Cxenstored uses the more-efficient domain_getinfolist. Both of
> > these are provided in the plugin to allow a transition from one to the
> > other without modifying the interface in the future. Both return
> > identical structures and rely on the same fields in xenctrl, thus if one
> > of them breaks, both will break, and a new version of the interface would
> > need to be issued.
> >
> > Signed-off-by: Andrii Sultanov <andrii.sultanov@cloud.com>
> > ---
>
> Normally you should put a short set of notes here (under --- in the
> commit message) about what has changed from the prior version you posted.
>
> >  tools/ocaml/Makefile                          |   1 +
> >  tools/ocaml/libs/Makefile                     |   2 +-
> >  tools/ocaml/libs/xenstoredglue/META.in        |   4 +
> >  tools/ocaml/libs/xenstoredglue/Makefile       |  46 +++++
> >  .../domain_getinfo_plugin_v1/META.in          |   5 +
> >  .../domain_getinfo_plugin_v1/Makefile         |  38 ++++
> >  .../domain_getinfo_stubs_v1.c                 | 172 ++++++++++++++++++
> >  .../domain_getinfo_v1.ml                      |  36 ++++
> >  .../domain_getinfo_v1.mli                     |   0
> >  .../libs/xenstoredglue/plugin_interface_v1.ml |  28 +++
> >  .../xenstoredglue/plugin_interface_v1.mli     |  36 ++++
> >  11 files changed, 367 insertions(+), 1 deletion(-)
> >  create mode 100644 tools/ocaml/libs/xenstoredglue/META.in
> >  create mode 100644 tools/ocaml/libs/xenstoredglue/Makefile
> >  create mode 100644
> tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/META.in
> >  create mode 100644
> tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> >  create mode 100644
> tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
> >  create mode 100644
> tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/
> domain_getinfo_v1.ml
> >  create mode 100644
> tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli
> >  create mode 100644 tools/ocaml/libs/xenstoredglue/
> plugin_interface_v1.ml
> >  create mode 100644
> tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
>
> We still have a mix of names even within this patch.  xenstoredglue,
> xenstored_glue, xsglue
>
> Could we see about using xsd_glue as the top level name here, to get it
> a bit shorter and easier to read?
>
> > diff --git
> a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> > new file mode 100644
> > index 0000000000..9c40026cab
> > --- /dev/null
> > +++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> > @@ -0,0 +1,38 @@
> > +OCAML_TOPLEVEL=$(CURDIR)/../../..
> > +XEN_ROOT=$(OCAML_TOPLEVEL)/../..
> > +include $(OCAML_TOPLEVEL)/common.make
> > +
> > +CFLAGS += -I $(OCAML_TOPLEVEL)/libs -I
> $(OCAML_TOPLEVEL)/libs/xenstoredglue
> > +CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude) $(APPEND_CFLAGS)
> > +OCAMLOPTFLAGS += -opaque
> > +OCAMLINCLUDE += -I ./ -I ../
>
> I think we only need "-I ../" here.  xen-caml-compat.h is the only local
> header used.
>
> > diff --git
> a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
> b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
> > new file mode 100644
> > index 0000000000..69eddd6412
> > --- /dev/null
> > +++
> b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c
> > @@ -0,0 +1,172 @@
>
> Sorry for missing this before, but new files want to contain SDPX
> headers.  For this,
>
> /* SPDX-License-Identifier: LGPL-2.1-only WITH
> OCaml-LGPL-linking-exception */
>
> which I had to go and research when sorting out xen-caml-compat.h
>
>
> For Ocaml files, suppose we want.
>
> (* SPDX-License-Identifier: LGPL-2.1-only WITH
> OCaml-LGPL-linking-exception *)
>
>
> The SPDX header should always be the first line of the file.
>
> > +#define _GNU_SOURCE
> > +
> > +#include <stdlib.h>
> > +#include <string.h>
> > +#include <errno.h>
> > +
> > +#define CAML_NAME_SPACE
> > +#include <caml/alloc.h>
> > +#include <caml/memory.h>
> > +#include <caml/signals.h>
> > +#include <caml/fail.h>
> > +#include <caml/callback.h>
> > +#include <caml/custom.h>
> > +
> > +#include <xen-tools/common-macros.h>
> > +#include <xenctrl.h>
> > +
> > +#include "xen-caml-compat.h"
> > +
> > +#define ERR_MSG_LEN (XC_MAX_ERROR_MSG_LEN + 6)
> > +#define MAX_FUNC_LINE_LEN 64
>
> These two are obsolete given the rework of xsglue_failwith_xc()
>
> > +#define failwith_xc_v1(xch) xsglue_failwith_xc(xch, __FUNCTION__,
> __LINE__)
>
> This should be moved lower and adjusted.  See later.
>
> > +
> > +/* This is a minimal stub to xenctrl for oxenstored's purposes
> > +   For the full xenctrl stubs, see tools/ocaml/libs/xc/xenctrl_stubs.c
> */
>
> These comments aren't helpful IMO.  It's said many times elsewhere.
>
> > +
> > +static inline xc_interface *xsglue_xch_of_val_v1(value v)
>
> static functions don't a _v1 suffix, seeing as they're local to a file
> with a _v1 in it's name.
>
> > +{
> > +     xc_interface *xch = *(xc_interface **)Data_custom_val(v);
> > +
> > +     return xch;
> > +}
> > +
> > +static void xsglue_xenctrl_finalize(value v)
> > +{
> > +     xc_interface *xch = xsglue_xch_of_val_v1(v);
> > +
> > +     xc_interface_close(xch);
> > +}
> > +
> > +static struct custom_operations xsglue_xenctrl_ops = {
> > +     .identifier  = "xenctrl",
>
> I presume this identifier gets elsewhere?  Perhaps
> "xsd_glue.domain_getinfo_v1.xenctrl" or so?
>
> > +     .finalize    = xsglue_xenctrl_finalize,
> > +     .compare     = custom_compare_default,     /* Can't compare     */
> > +     .hash        = custom_hash_default,        /* Can't hash        */
> > +     .serialize   = custom_serialize_default,   /* Can't serialize   */
> > +     .deserialize = custom_deserialize_default, /* Can't deserialize */
> > +     .compare_ext = custom_compare_ext_default, /* Can't compare     */
> > +};
> > +
>
> There's a trick with the C preprocessor where you can
>
> #define xsglue_failwith(xch) xsglue_failwith(xch, __func__, __LINE__)
>
> to add extra arguments to callsites.  The only requirement is that it
> either needs to be after the function of the same name, or that you
> define the function using:
>
> static void Noreturn (xsglue_failwith)(xc_interface *xch ...)
>
>
> I'd put the macro and the declaration together here.  Also, use __func__
> rather than __FUNCTION__.
>
> > +static void Noreturn xsglue_failwith_xc(xc_interface *xch,
> > +             const char* func,
> > +             unsigned int line)
>
> The _xc() suffix isn't very helpful when there's also an xsglue_ prefix.
>
> I'd simply name this xsglue_failwith(...) which is clear enough when used.
>
> Also, 'const char *fun' with the * to the right of the space.
>
> > +{
> > +     const xc_error *error = xch ? xc_get_last_error(xch) : NULL;
> > +        char *str = NULL;
> > +     CAMLparam0();
> > +        CAMLlocal1(msg);
>
> Mixing tabs and spaces.
>
> > <snip>
> >
> > +static value xsglue_alloc_domaininfo_v1(const xc_domaininfo_t *info)
> > +{
> > +     CAMLparam0();
> > +     CAMLlocal1(result);
>
> We try to split declarations from regular logic, so a blank line here
> please.
>
> > +     result = caml_alloc_tuple(4);
> > +
> > +     Store_field(result,  0, Val_int(info->domain));
> > +     Store_field(result,  1, Val_bool(info->flags & XEN_DOMINF_dying));
> > +     Store_field(result,  2, Val_bool(info->flags &
> XEN_DOMINF_shutdown));
> > +     Store_field(result,  3, Val_int(MASK_EXTR(info->flags,
> XEN_DOMINF_shutdownmask)));
> > +
> > +     CAMLreturn(result);
> > +}
> > +
> > +CAMLprim value stub_xsglue_xc_domain_getinfo(value xch_val, value domid)
> > +{
> > +     CAMLparam2(xch_val, domid);
> > +     CAMLlocal1(result);
> > +     xc_interface *xch = xsglue_xch_of_val_v1(xch_val);
> > +     xc_domaininfo_t info;
> > +     int ret, domid_c;
> > +
> > +     domid_c = Int_val(domid);
>
> I'd suggests a blank line here, or to initialise domid_c at declaration.
>
> > +     caml_enter_blocking_section();
> > +     ret = xc_domain_getinfo_single(xch, domid_c, &info);
> > +     caml_leave_blocking_section();
> > +
> > +     if (ret < 0)
> > +             failwith_xc_v1(xch);
> > +
> > +     result = xsglue_alloc_domaininfo_v1(&info);
> > +
> > +     CAMLreturn(result);
> > +}
> > +
> > +CAMLprim value stub_xsglue_xc_domain_getinfolist(value xch_val, value
> first_domain)
> > +{
> > +     CAMLparam2(xch_val, first_domain);
> > +     CAMLlocal1(result);
> > +     xc_interface *xch = xsglue_xch_of_val_v1(xch_val);
> > +     xc_domaininfo_t *info;
> > +     int i, ret, toalloc, retval;
> > +     uint32_t num_domains;
> > +     uint32_t c_first_domain;
> > +
> > +     /* get the minimum number of allocate byte we need and bump it up
> to page boundary */
> > +     c_first_domain = Int_val(first_domain);
>
> Passing a first_domain of anything other than 0 breaks the atomicity
> that we're trying to create by asking for all domains together.
>
> It wants dropping from here, and the plugin interface.
>
> > +     num_domains = DOMID_FIRST_RESERVED - c_first_domain;
> > +     toalloc = (sizeof(xc_domaininfo_t) * num_domains) | 0xfff;
> > +     ret = posix_memalign((void **) ((void *) &info), 4096, toalloc);
>
> This is nonsense, and appears to have been so for ages in the Xenctrl
> stubs.
>
> xc_domain_getinfolist() bounce-buffers the array anyway (to get
> hypercall-safe buffers from the kernel), so there's no point doing any
> local trickery.  Simply:
>
>     info = malloc(sizeof(xc_domaininfo_t) * DOMID_FIRST_RESERVED);
>     if ( !info )
>         caml_raise_out_of_memory();
>
> will be fine.
>
> > +     if (ret)
> > +             caml_raise_out_of_memory();
> > +
> > +     caml_enter_blocking_section();
> > +     retval = xc_domain_getinfolist(xch, c_first_domain, num_domains,
> info);
> > +     caml_leave_blocking_section();
> > +
> > +     if (retval < 0) {
> > +             free(info);
> > +             failwith_xc_v1(xch);
> > +     } else if (retval > 0) {
> > +             result = caml_alloc(retval, 0);
> > +             for (i = 0; i < retval; i++) {
> > +                     caml_modify(&Field(result, i),
> xsglue_alloc_domaininfo_v1(info + i));
> > +             }
> > +     } else {
> > +             result = Atom(0);
> > +     }
>
> While this works, there are better ways of writing the logic.
> failwith() is Noreturn, so it's easier to follow as:
>
>     if (retval < 0) {
>         ...
>     }
>
>     if (retval > 0) {
>         ...
>
> but...  You're dom0, asking for all domains.  Getting a retval of 0 is
> also some kind of error, so I'd suggest:
>
>     if (retval <= 0) {
>         free(info);
>         xsglue_failwith(xch);
>     }
>
>     result = caml_alloc(retval, 0);
>     for (i = 0; i < retval; i++) {
>         caml_modify(&Field(result, i), xsglue_alloc_domaininfo_v1(info +
> i));
>     }
>
>     free(info);
>     Camlreturn(result);
>
> which is simpler still.
>
>
> > diff --git a/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> > new file mode 100644
> > index 0000000000..d073a44d0f
> > --- /dev/null
> > +++ b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> > @@ -0,0 +1,36 @@
> > +(** To avoid breaking the plugin interface, this module needs to be
> > +    standalone and can't rely on any other Xen library. Even unrelated
> > +    changes in the interfaces of those modules would change the hash
> > +    of this interface and break the plugin system.
> > +    It can only depend on Stdlib, therefore all of the types (domid,
> > +    domaininfo etc.) are redefined here instead of using alternatives
> > +    defined elsewhere.
> > +
> > +    NOTE: The signature of this interface should not be changed (no
> > +    functions or types can be added, modified, or removed). If
> > +    underlying Xenctrl changes require a new interface, a V2 with a
> > +    corresponding plugin should be created.
> > + *)
>
> There's a rune to run ocp-indent in the Xen tree, in lieu of the full
> Ocaml dev stack.
>
> make -C tools/ocaml format
>
> and the delta for this patch is just:
>
> --- a/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> +++ b/tools/ocaml/libs/xenstoredglue/plugin_interface_v1.mli
> @@ -10,7 +10,7 @@
>      functions or types can be added, modified, or removed). If
>      underlying Xenctrl changes require a new interface, a V2 with a
>      corresponding plugin should be created.
> - *)
> +*)
>
>  module type Domain_getinfo_V1 = sig
>    exception Error of string
>
> ~Andrew
>
Re: [PATCH v2 2/3] ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo
Posted by Andrew Cooper 2 months, 4 weeks ago
On 06/09/2024 3:20 pm, Andrii Sultanov wrote:
>> > diff --git
> a/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
> b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
>> > new file mode 100644
>> > index 0000000000..9c40026cab
>> > --- /dev/null
>> > +++ b/tools/ocaml/libs/xenstoredglue/domain_getinfo_plugin_v1/Makefile
>> > @@ -0,0 +1,38 @@
>> > +OCAML_TOPLEVEL=$(CURDIR)/../../..
>> > +XEN_ROOT=$(OCAML_TOPLEVEL)/../..
>> > +include $(OCAML_TOPLEVEL)/common.make
>> > +
>> > +CFLAGS += -I $(OCAML_TOPLEVEL)/libs -I
> $(OCAML_TOPLEVEL)/libs/xenstoredglue
>> > +CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude) $(APPEND_CFLAGS)
>> > +OCAMLOPTFLAGS += -opaque
>> > +OCAMLINCLUDE += -I ./ -I ../
>>
>> I think we only need "-I ../" here.  xen-caml-compat.h is the only local
>> header used.
> 
> With only "-I ../", the build fails:
> ```
> ocamlopt -g -ccopt "   " -dtypes -I ../ -w F -warn-error F -opaque
> -shared -linkall -o domain_getinfo_v1.cmxs  domain_getinfo_v1.cmxa
> /usr/bin/ld: cannot find -ldomain_getinfo_v1_stubs: No such file or
> directory
> collect2: error: ld returned 1 exit status
> ```

Oh ok.  Better keep it then.

~Andrew