Skip to content

Commit

Permalink
xen-dom-mgmt: Allow config selection for domain creation
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Deedone committed Apr 26, 2024
1 parent 6d82d62 commit fc16035
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
10 changes: 10 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ config XEN_DOMAIN_MANAGEMENT
Enable domain management library. This library allows you
to create, destroy and manage xen domains.

config XEN_DOMCFG_SECTION
bool "Enable Xen domain config section"
default y
depends on XEN_DOMAIN_MANAGEMENT
help
Enable gathering domain configurations to a common section.
This allows you to declare multiple domain configs with
a DECL_CONFIG macro. Configs can be later accessed by
their name.

config XEN_CONSOLE_SRV
bool "Enable Xen Console server"
help
Expand Down
3 changes: 3 additions & 0 deletions xen-dom-mgmt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ target_include_directories(XENDOM_MGMT INTERFACE include)
zephyr_library()
zephyr_library_sources(src/xen-dom-mgmt.c src/xen-dom-fdt.c src/mem-mgmt.c src/xl_parser.c)
zephyr_library_sources(src/xen-dom-xs.c)
if(CONFIG_XEN_DOMCFG_SECTION)
zephyr_linker_sources(DATA_SECTIONS linker.ld)
endif()
zephyr_library_link_libraries(XENDOM_MGMT)
zephyr_include_directories(include)
9 changes: 9 additions & 0 deletions xen-dom-mgmt/include/xen_dom_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ int domain_destroy(uint32_t domid);
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);
struct xen_domain_cfg *domain_find_config(char *name);

#ifdef CONFIG_XEN_DOMCFG_SECTION
#define DECL_CONFIG static __section(".domain_configs") __used
extern struct xen_domain_cfg _domain_configs_start[];
extern struct xen_domain_cfg _domain_configs_end[];
#else
#define DECL_CONFIG
#endif

#ifdef __cplusplus
}
Expand Down
6 changes: 6 additions & 0 deletions xen-dom-mgmt/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.domain_configs :
{
_domain_configs_start = .;
KEEP(*(.domain_configs))
_domain_configs_end = .;
} > RAM
22 changes: 22 additions & 0 deletions xen-dom-mgmt/src/xen-dom-mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <domain.h>
#include <xen-dom-fdt.h>
#include <xen-dom-xs.h>
#include <xen_dom_mgmt.h>
#include <mem-mgmt.h>
#include <uimage.h>
#include <zimage.h>
Expand Down Expand Up @@ -900,6 +901,27 @@ int domain_post_create(const struct xen_domain_cfg *domcfg, uint32_t domid)
return rc;
}

/**
* Find the configuration for a Xen domain by name.
*
* @param name The name of the Xen domain to search for.
* @return A pointer to the Xen domain configuration if found, NULL otherwise.
*/
struct xen_domain_cfg *domain_find_config(char *name)
{
__maybe_unused struct xen_domain_cfg *cfg = NULL;

#ifdef CONFIG_XEN_DOMCFG_SECTION
for (cfg = _domain_configs_start; cfg < _domain_configs_end; cfg++) {
if (strncmp(cfg->name, name, CONTAINER_NAME_SIZE) == 0) {
return cfg;
}
}
#endif

return NULL;
}

static int init_domain0(void)
{
int ret = 0;
Expand Down
49 changes: 37 additions & 12 deletions xen-shell-cmd/src/xen_cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <zephyr/shell/shell.h>
#include <zephyr/logging/log.h>
#include <stdlib.h>
#include <string.h>

#include <xen_dom_mgmt.h>
#ifdef CONFIG_XEN_CONSOLE_SRV
Expand All @@ -15,8 +16,6 @@

LOG_MODULE_REGISTER(xen_shell);

extern struct xen_domain_cfg domd_cfg;

uint32_t parse_domid(size_t argc, char **argv)
{
/* first would be the cmd name, start from second */
Expand All @@ -32,29 +31,55 @@ 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)
{
int ret;
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;
}

name = parse_name(argc, argv);
if (!name) {
shell_error(shell, "Invalid config passed to create cmd");
return -EINVAL;
}
/*
* TODO: this should be changed in app code.
* Not all domains using domd config
*/
ret = domain_create(&domd_cfg, domid);

cfg = domain_find_config(name);
if (!cfg) {
shell_error(shell, "Config %s not found", name);
return -EINVAL;
}

ret = domain_create(cfg, domid);
if (ret) {
return ret; /* domain_create should care about error logs */
}

return domain_post_create(&domd_cfg, domid);
return domain_post_create(cfg, domid);
}

int domu_destroy(const struct shell *shell, size_t argc, char **argv)
Expand Down Expand Up @@ -137,8 +162,8 @@ SHELL_STATIC_SUBCMD_SET_CREATE(
subcmd_xu,
SHELL_CMD_ARG(create, NULL,
" Create Xen domain\n"
" Usage: create -d <domid>\n",
domu_create, 3, 0),
" Usage: create -d <domid> -c <config_name>\n",
domu_create, 5, 0),
SHELL_CMD_ARG(destroy, NULL,
" Destroy Xen domain\n"
" Usage: destroy -d <domid>\n",
Expand Down

0 comments on commit fc16035

Please sign in to comment.