From ce0058360bb5d90d87b49fb9a4aac68e7e039970 Mon Sep 17 00:00:00 2001 From: Mykyta Poturai Date: Mon, 1 Apr 2024 12:54:25 +0300 Subject: [PATCH] xen-shell-cmd: Allow config selection for domain creation Add linker script to collect all domain configs in one place. Add macro DECL_CONFIG to mark domain config structures. Specific config can be selected for domain creation by name via the shell command line parameter. Signed-off-by: Mykyta Poturai --- xen-dom-mgmt/include/xen_dom_mgmt.h | 2 + xen-shell-cmd/CMakeLists.txt | 1 + xen-shell-cmd/linker.ld | 6 +++ xen-shell-cmd/src/xen_cmds.c | 57 ++++++++++++++++++++++++----- 4 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 xen-shell-cmd/linker.ld diff --git a/xen-dom-mgmt/include/xen_dom_mgmt.h b/xen-dom-mgmt/include/xen_dom_mgmt.h index cfb1be82..e42ac6b3 100644 --- a/xen-dom-mgmt/include/xen_dom_mgmt.h +++ b/xen-dom-mgmt/include/xen_dom_mgmt.h @@ -19,6 +19,8 @@ int domain_pause(uint32_t domid); int domain_unpause(uint32_t domid); int domain_post_create(const struct xen_domain_cfg *domcfg, uint32_t domid); +#define DECL_CONFIG static __attribute__((__section__(".domain_configs"))) __used + #ifdef __cplusplus } #endif diff --git a/xen-shell-cmd/CMakeLists.txt b/xen-shell-cmd/CMakeLists.txt index ba2a3153..7046871e 100644 --- a/xen-shell-cmd/CMakeLists.txt +++ b/xen-shell-cmd/CMakeLists.txt @@ -4,6 +4,7 @@ add_library(XEN_SHELL INTERFACE) zephyr_library() zephyr_library_sources(src/xen_cmds.c) +zephyr_linker_sources(DATA_SECTIONS linker.ld) zephyr_library_sources_ifdef(CONFIG_XRUN_SHELL_CMDS src/xrun_cmds.c) zephyr_library_sources_ifdef(CONFIG_XSTAT_SHELL_CMDS src/xstat_cmds.c) zephyr_library_link_libraries(XEN_SHELL) diff --git a/xen-shell-cmd/linker.ld b/xen-shell-cmd/linker.ld new file mode 100644 index 00000000..01059198 --- /dev/null +++ b/xen-shell-cmd/linker.ld @@ -0,0 +1,6 @@ +.domain_configs : +{ + _domain_configs_start = .; + KEEP(*(.domain_configs)) + _domain_configs_end = .; +} > RAM diff --git a/xen-shell-cmd/src/xen_cmds.c b/xen-shell-cmd/src/xen_cmds.c index a6737334..9f38f4fe 100644 --- a/xen-shell-cmd/src/xen_cmds.c +++ b/xen-shell-cmd/src/xen_cmds.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #ifdef CONFIG_XEN_CONSOLE_SRV @@ -15,7 +16,8 @@ LOG_MODULE_REGISTER(xen_shell); -extern struct xen_domain_cfg domd_cfg; +extern struct xen_domain_cfg _domain_configs_start[]; +extern struct xen_domain_cfg _domain_configs_end[]; uint32_t parse_domid(size_t argc, char **argv) { @@ -32,23 +34,58 @@ uint32_t parse_domid(size_t argc, char **argv) return 0; } +char *parse_name(size_t argc, char **argv) +{ + /* first would be the cmd name, start from second */ + int pos = 3; + + if (argv[pos][0] == '-' && argv[pos][1] == 'c') { + /* Take next value after "-c" option */ + pos++; + return argv[pos]; + } + + /* Use zero as invalid value */ + return NULL; +} + static int domu_create(const struct shell *shell, int argc, char **argv) { uint32_t domid; + char *name; + struct xen_domain_cfg *cfg; - if (argc != 3) + if (argc != 5) return -EINVAL; domid = parse_domid(argc, argv); if (!domid) { - LOG_ERR("Invalid domid passed to create cmd"); + shell_error(shell, "Invalid domid passed to create cmd"); return -EINVAL; } - /* - * TODO: this should be changed in app code. - * Not all domains using domd config - */ - return domain_create(&domd_cfg, domid); + + name = parse_name(argc, argv); + if (!name) { + shell_error(shell, "Invalid config passed to create cmd"); + return -EINVAL; + } + + for (cfg = _domain_configs_start; cfg < _domain_configs_end; cfg++) { + if (strncmp(cfg->name, name, CONTAINER_NAME_SIZE) == 0) { + break; + } + } + + if (cfg == _domain_configs_end) { + shell_error(shell, "Config with name %s not found", name); + shell_error(shell, "Available configs are:"); + for (cfg = _domain_configs_start; cfg < _domain_configs_end; cfg++) { + shell_error(shell, "%s", cfg->name); + } + return -EINVAL; + } + + return domain_create(cfg, domid); } int domu_destroy(const struct shell *shell, size_t argc, char **argv) @@ -131,8 +168,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE( subcmd_xu, SHELL_CMD_ARG(create, NULL, " Create Xen domain\n" - " Usage: create -d \n", - domu_create, 3, 0), + " Usage: create -d -c \n", + domu_create, 5, 0), SHELL_CMD_ARG(destroy, NULL, " Destroy Xen domain\n" " Usage: destroy -d \n",