tools/ocaml: Stabilize domain_getinfo for Oxenstored
PATCH v3 1/3 ocaml/libs: Implement a dynamically-loaded plugin for Xenctrl.domain_getinfo
In reply to Andrii SultanovThis 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.
---
* Reformat files appropriately
* Rename all instances of 'xenstoredglue', 'xenstored_glue', 'xsglue' to 'xsd_glue' for consistency
* Remove _v1 suffix from all static functions
* Remove leftover #defines left unused by the previous changes
* Fix inconsistent tabs/spaces usage, added blank lines where appropriate.
* Define the 'xsd_glue_failwith' macro as suggested
* Remove first_domain argument in 'domain_getinfolist' function
* Remove unnecessary memory allocation alignment code in 'domain_getinfolist'
* Improve error handling in 'domain_getinfolist' as suggested.
* Add SPDX license headers to new files.
tools/ocaml/Makefile | 1 + tools/ocaml/libs/Makefile | 2 +- tools/ocaml/libs/xsd_glue/META.in | 4 + tools/ocaml/libs/xsd_glue/Makefile | 46 +++++ .../xsd_glue/domain_getinfo_plugin_v1/META.in | 5 + .../domain_getinfo_plugin_v1/Makefile | 38 ++++ .../domain_getinfo_stubs_v1.c | 162 ++++++++++++++++++ .../domain_getinfo_v1.ml | 35 ++++ .../domain_getinfo_v1.mli | 1 + .../libs/xsd_glue/plugin_interface_v1.ml | 30 ++++ .../libs/xsd_glue/plugin_interface_v1.mli | 38 ++++ 11 files changed, 361 insertions(+), 1 deletion(-) create mode 100644 tools/ocaml/libs/xsd_glue/META.in create mode 100644 tools/ocaml/libs/xsd_glue/Makefile create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/META.in create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/Makefile create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli create mode 100644 tools/ocaml/libs/xsd_glue/plugin_interface_v1.ml create mode 100644 tools/ocaml/libs/xsd_glue/plugin_interface_v1.mli
tools/ocaml/Makefile +1 -0
@@ -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/xsd_glue $(MAKE) -C xenstored .PHONY: format
tools/ocaml/libs/Makefile +1 -1
@@ -4,7 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk SUBDIRS= \ mmap \ eventchn xc \ - xb xs + xb xs xsd_glue .PHONY: all all: subdirs-all
tools/ocaml/libs/xsd_glue/META.in new file +4 -0
@@ -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"
tools/ocaml/libs/xsd_glue/Makefile new file +46 -0
46 lines not on this page · raw message
tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/META.in new file +5 -0
@@ -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"
tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/Makefile new file +38 -0
@@ -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/xsd_glue +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_xsd_glue = $(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)/ocaml/xsd_glue/xenctrl_plugin + $(INSTALL_PROG) domain_getinfo_v1.cmxs $(DESTDIR)$(LIBEXEC)/ocaml/xsd_glue/xenctrl_plugin + +.PHONY: uninstall +uninstall: + rm -f $(DESTDIR)$(LIBEXEC)/ocaml/xsd_glue/xenctrl_plugin/domain_getinfo_v1.cmxs + +include $(OCAML_TOPLEVEL)/Makefile.rules +
tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c new file +162 -0
162 lines not on this page · raw message
tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml new file +35 -0
@@ -0,0 +1,35 @@ +(* SPDX-License-Identifier: LGPL-2.1-only WITH OCaml-LGPL-linking-exception *) +(** Minimal interface on top of unstable Xenctrl for Oxenstored's usage *) + +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_xsd_glue_xc_interface_open" + + external domain_getinfo : handle -> domid -> domaininfo + = "stub_xsd_glue_xc_domain_getinfo" + + external domain_getinfolist : handle -> domaininfo array + = "stub_xsd_glue_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__
tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli new file +1 -0
@@ -0,0 +1 @@ +(* SPDX-License-Identifier: LGPL-2.1-only WITH OCaml-LGPL-linking-exception *)
tools/ocaml/libs/xsd_glue/plugin_interface_v1.ml new file +30 -0
@@ -0,0 +1,30 @@ +(* SPDX-License-Identifier: LGPL-2.1-only WITH OCaml-LGPL-linking-exception *) + +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 -> 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"
tools/ocaml/libs/xsd_glue/plugin_interface_v1.mli new file +38 -0
@@ -0,0 +1,38 @@ +(* SPDX-License-Identifier: LGPL-2.1-only WITH OCaml-LGPL-linking-exception *) + +(** 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 -> 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)
Signature · 1 line
2.39.2
PATCH v3 3/3 tools/oxenstored: Switch to using the plugin for Xenctrl.domain_getinfo
In reply to Andrii SultanovAlso run 'make format' on the file to reformat an adjacent block of code correctly.
---
* This commit is a result of the split of one oxenstored commit into two.
tools/ocaml/xenstored/domains.ml | 51 ++++++++++++++++---------------- 1 file changed, 25 insertions(+), 26 deletions(-)
tools/ocaml/xenstored/domains.ml +25 -26
@@ -49,8 +49,8 @@ module Plugin = let handle = Plugin.interface_open () type domains = { - eventchn: Event.t; - table: (Xenctrl.domid, Domain.t) Hashtbl.t; + eventchn : Event.t; + table : (Plugin.domid, Domain.t) Hashtbl.t; (* N.B. the Queue module is not thread-safe but oxenstored is single-threaded. *) (* Domains queue up to regain conflict-credit; we have a queue for
@@ -121,30 +121,29 @@ let cleanup doms = let notify = ref false in let dead_dom = ref [] in - Hashtbl.iter (fun id _ -> if id <> 0 then - try - let info = Xenctrl.domain_getinfo xc id in - if info.Xenctrl.shutdown || info.Xenctrl.dying then ( - debug "Domain %u died (dying=%b, shutdown %b -- code %d)" - id info.Xenctrl.dying info.Xenctrl.shutdown info.Xenctrl.shutdown_code; - if info.Xenctrl.dying then - dead_dom := id :: !dead_dom - else - notify := true; - ) - with Xenctrl.Error _ -> - debug "Domain %u died -- no domain info" id; - dead_dom := id :: !dead_dom; - ) doms.table; - List.iter (fun id -> - let dom = Hashtbl.find doms.table id in - Domain.close dom; - Hashtbl.remove doms.table id; - if dom.Domain.conflict_credit <= !Define.conflict_burst_limit - then ( - remove_from_queue dom doms.doms_with_conflict_penalty; - if (dom.Domain.conflict_credit <= 0.) then remove_from_queue dom doms.doms_conflict_paused - ) + Hashtbl.iter + (fun id _ -> + if id <> 0 then ( + try + let info = Plugin.domain_getinfo handle id in + if info.Plugin.shutdown || info.Plugin.dying then ( + debug "Domain %u died (dying=%b, shutdown %b -- code %d)" id + info.Plugin.dying info.Plugin.shutdown info.Plugin.shutdown_code; + if info.Plugin.dying then dead_dom := id :: !dead_dom else notify := true) + with Plugin.Error _ -> + debug "Domain %u died -- no domain info" id; + dead_dom := id :: !dead_dom)) + doms.table; + List.iter + (fun id -> + let dom = Hashtbl.find doms.table id in + Domain.close dom; + Hashtbl.remove doms.table id; + if dom.Domain.conflict_credit <= !Define.conflict_burst_limit + then ( + remove_from_queue dom doms.doms_with_conflict_penalty; + if (dom.Domain.conflict_credit <= 0.) then remove_from_queue dom doms.doms_conflict_paused + ) ) !dead_dom; !notify, !dead_dom
Signature · 1 line
2.39.2
v3 cover tools/ocaml: Stabilize domain_getinfo for Oxenstored
Changes from V2:
* Drop the upstreamed "Build infrastructure" commit
* Split the last commit into two: Loading the plugin in oxenstored and switching to it
* Remove configure and paths.m4 defines as suggested.
* Reformat files appropriately
* Rename all instances of 'xenstoredglue', 'xenstored_glue', 'xsglue' to 'xsd_glue' for consistency
* Remove _v1 suffix from all static functions
* Remove leftover #defines left unused by the previous changes
* Fix inconsistent tabs/spaces usage, added blank lines where appropriate.
* Define the 'xsd_glue_failwith' macro as suggested
* Remove first_domain argument in 'domain_getinfolist' function
* Remove unnecessary memory allocation alignment code in 'domain_getinfolist'
* Improve error handling in 'domain_getinfolist' as suggested.
* Add SPDX license headers to new files.
Patch series on Gitlab for ease of review: https://gitlab.com/xen-project/people/asultanov/xen/-/compare/staging...plugin-v3
These changes passed the Gitlab CI:
- Loading the plugin: https://gitlab.com/xen-project/people/asultanov/xen/-/pipelines/1445544074
- Switching to it as well: https://gitlab.com/xen-project/people/asultanov/xen/-/pipelines/1445699231
The patch series has also been tested manually.
Andrii Sultanov (3):
ocaml/libs: Implement a dynamically-loaded plugin for
Xenctrl.domain_getinfo
tools/oxenstored: Load the plugin for Xenctrl.domain_getinfo tools/oxenstored: Switch to using the plugin for
Xenctrl.domain_getinfo
tools/ocaml/Makefile | 1 + tools/ocaml/libs/Makefile | 2 +- tools/ocaml/libs/xsd_glue/META.in | 4 + tools/ocaml/libs/xsd_glue/Makefile | 46 +++++ .../xsd_glue/domain_getinfo_plugin_v1/META.in | 5 + .../domain_getinfo_plugin_v1/Makefile | 38 ++++ .../domain_getinfo_stubs_v1.c | 162 ++++++++++++++++++ .../domain_getinfo_v1.ml | 35 ++++ .../domain_getinfo_v1.mli | 1 + .../libs/xsd_glue/plugin_interface_v1.ml | 30 ++++ .../libs/xsd_glue/plugin_interface_v1.mli | 38 ++++ tools/ocaml/xenstored/Makefile | 5 +- tools/ocaml/xenstored/domains.ml | 79 ++++++--- tools/ocaml/xenstored/paths.ml.in | 1 + 14 files changed, 419 insertions(+), 28 deletions(-) create mode 100644 tools/ocaml/libs/xsd_glue/META.in create mode 100644 tools/ocaml/libs/xsd_glue/Makefile create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/META.in create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/Makefile create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_stubs_v1.c create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_v1.ml create mode 100644 tools/ocaml/libs/xsd_glue/domain_getinfo_plugin_v1/domain_getinfo_v1.mli create mode 100644 tools/ocaml/libs/xsd_glue/plugin_interface_v1.ml create mode 100644 tools/ocaml/libs/xsd_glue/plugin_interface_v1.mli
Signature · 1 line
2.39.2
PATCH v3 2/3 tools/oxenstored: Load the plugin for Xenctrl.domain_getinfo
Oxenstored dynamically loads the plugin provided in ocaml/libs/xsd_glue. The plugin is verified to be providing the specified plugin_interface during its loading.
If a V2 of the plugin is produced, V1 will still be present, and a new version should only be loaded if it's verified to exist (New oxenstored can run in an environment with only V1 of the plugin).
The plugin is not switched to as of yet, the old Xenctrl stubs are still used.
---
* This commit is a result of the split of one oxenstored commit into two.
* Remove configure and paths.m4 defines as suggested.
* Reformat files appropriately
* Use libexec/ocaml as the plugin root dir instead of libexec/bin
tools/ocaml/xenstored/Makefile | 5 ++++- tools/ocaml/xenstored/domains.ml | 28 ++++++++++++++++++++++++++++ tools/ocaml/xenstored/paths.ml.in | 1 + 3 files changed, 33 insertions(+), 1 deletion(-)
tools/ocaml/xenstored/Makefile +4 -1
@@ -15,7 +15,8 @@ OCAMLINCLUDE += \ -I $(OCAML_TOPLEVEL)/libs/xb \ -I $(OCAML_TOPLEVEL)/libs/mmap \ -I $(OCAML_TOPLEVEL)/libs/xc \ - -I $(OCAML_TOPLEVEL)/libs/eventchn + -I $(OCAML_TOPLEVEL)/libs/eventchn \ + -I $(OCAML_TOPLEVEL)/libs/xsd_glue LIBS = syslog.cma syslog.cmxa poll.cma poll.cmxa syslog_OBJS = syslog
@@ -59,6 +60,7 @@ INTF = symbol.cmi trie.cmi syslog.cmi systemd.cmi poll.cmi XENSTOREDLIBS = \ unix.cmxa \ + dynlink.cmxa \ -ccopt -L -ccopt . syslog.cmxa \ -ccopt -L -ccopt . systemd.cmxa \ -ccopt -L -ccopt . poll.cmxa \
@@ -66,6 +68,7 @@ XENSTOREDLIBS = \ -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/eventchn $(OCAML_TOPLEVEL)/libs/eventchn/xeneventchn.cmxa \ -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xc $(OCAML_TOPLEVEL)/libs/xc/xenctrl.cmxa \ -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xb $(OCAML_TOPLEVEL)/libs/xb/xenbus.cmxa \ + -ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xsd_glue $(OCAML_TOPLEVEL)/libs/xsd_glue/plugin_interface_v1.cmxa \ -ccopt -L -ccopt $(XEN_ROOT)/tools/libs/ctrl PROGRAMS = oxenstored
tools/ocaml/xenstored/domains.ml +28 -0
@@ -20,6 +20,34 @@ let warn fmt = Logging.warn "domains" fmt let xc = Xenctrl.interface_open () +let load_plug fname = + let fail_with fmt = Printf.ksprintf failwith fmt in + let fname = Dynlink.adapt_filename fname in + if Sys.file_exists fname then + try Dynlink.loadfile fname with + | Dynlink.Error err as e -> + error "ERROR loading plugin '%s': %s\n%!" fname + (Dynlink.error_message err); + raise e + | _ -> fail_with "Unknown error while loading plugin" + else fail_with "Plugin file '%s' does not exist" fname + +let () = + let plugins_dir = Paths.libexec ^ "/ocaml/xsd_glue/xenctrl_plugin/" in + let filepath = plugins_dir ^ "domain_getinfo_v1.cmxs" in + debug "Trying to load plugin '%s'\n%!" filepath; + let list_files = Sys.readdir plugins_dir in + debug "Directory listing of '%s'\n%!" plugins_dir ; + Array.iter (fun x -> debug "\t%s\n%!" x) list_files; + Dynlink.allow_only [ "Plugin_interface_v1" ]; + load_plug filepath + +module Plugin = + (val Plugin_interface_v1.get_plugin_v1 () + : Plugin_interface_v1.Domain_getinfo_V1) + +let handle = Plugin.interface_open () + type domains = { eventchn: Event.t; table: (Xenctrl.domid, Domain.t) Hashtbl.t;
tools/ocaml/xenstored/paths.ml.in +1 -0
@@ -2,3 +2,4 @@ let xen_log_dir = "@XEN_LOG_DIR@" let xen_config_dir = "@XEN_CONFIG_DIR@" let xen_run_dir = "@XEN_RUN_DIR@" let xen_run_stored = "@XEN_RUN_STORED@" +let libexec = "@LIBEXEC@"
Signature · 1 line
2.39.2
Quoted 25 lines · Andrii Sultanov
Changes from V2:
* Drop the upstreamed "Build infrastructure" commit
* Split the last commit into two: Loading the plugin in oxenstored and switching to it
* Remove configure and paths.m4 defines as suggested.
* Reformat files appropriately
* Rename all instances of 'xenstoredglue', 'xenstored_glue', 'xsglue' to 'xsd_glue' for consistency
* Remove_v1suffix from all static functions
* Remove leftover #defines left unused by the previous changes
* Fix inconsistent tabs/spaces usage, added blank lines where appropriate.
* Define the 'xsd_glue_failwith' macro as suggested
* Removefirst_domainargument in 'domain_getinfolist' function
* Remove unnecessary memory allocation alignment code in 'domain_getinfolist'
* Improve error handling in 'domain_getinfolist' as suggested.
* Add SPDX license headers to new files.Patch series on Gitlab for ease of review: https://gitlab.com/xen-project/people/asultanov/xen/-/compare/staging...plugin-v3
These changes passed the Gitlab CI:
- Loading the plugin: https://gitlab.com/xen-project/people/asultanov/xen/-/pipelines/1445544074
- Switching to it as well: https://gitlab.com/xen-project/people/asultanov/xen/-/pipelines/1445699231
Quoted 8 lines
The patch series has also been tested manually.
Andrii Sultanov (3):
ocaml/libs: Implement a dynamically-loaded plugin for
Xenctrl.domain_getinfo
tools/oxenstored: Load the plugin for Xenctrl.domain_getinfotools/oxenstored: Switch to using the plugin for
Xenctrl.domain_getinfo