From 93dd051385400935ab9dc5dc5f456594bc4c696f Mon Sep 17 00:00:00 2001 From: w00808059 Date: Mon, 29 Dec 2025 20:09:55 +0800 Subject: [PATCH] urma: add admin cmds to enhance container ns operations. --- src/urma/lib/urma/core/include/urma_cmd.h | 3 + src/urma/tools/urma_admin/admin_cmd_dev.c | 63 ++++++++++++++++++-- src/urma/tools/urma_admin/admin_netlink.h | 11 +++- src/urma/tools/urma_admin/admin_parameters.c | 30 ++++++++++ src/urma/tools/urma_admin/admin_parameters.h | 1 + 5 files changed, 101 insertions(+), 7 deletions(-) diff --git a/src/urma/lib/urma/core/include/urma_cmd.h b/src/urma/lib/urma/core/include/urma_cmd.h index b6a6621..5dda3e6 100644 --- a/src/urma/lib/urma/core/include/urma_cmd.h +++ b/src/urma/lib/urma/core/include/urma_cmd.h @@ -39,6 +39,9 @@ typedef enum urma_core_cmd { URMA_CORE_CMD_SET_EID_MODE, URMA_CORE_SET_NS_MODE, URMA_CORE_SET_DEV_NS, + URMA_CORE_SET_DEV_SHARING_MODE, + URMA_CORE_EXPOSE_DEV_NS, + URMA_CORE_UNEXPOSE_DEV_NS, URMA_CORE_GET_TOPO_INFO, } urma_core_cmd_t; diff --git a/src/urma/tools/urma_admin/admin_cmd_dev.c b/src/urma/tools/urma_admin/admin_cmd_dev.c index 965ed97..6e9f34d 100644 --- a/src/urma/tools/urma_admin/admin_cmd_dev.c +++ b/src/urma/tools/urma_admin/admin_cmd_dev.c @@ -27,8 +27,23 @@ static int cmd_dev_usage(admin_config_t *cfg) static int cmd_dev_set_sharing(admin_config_t *cfg) { - printf("TODO set sharing %s\n", cfg->dev_name); - return 0; + int ret; + + if ((ret = pop_arg_sharing(cfg)) != 0) { + return ret; + } + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_SET_DEV_SHARING_MODE, 0); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_string(msg, UBCORE_ATTR_DEV_NAME, cfg->dev_name); + admin_nl_put_u8(msg, UBCORE_ATTR_NS_MODE, cfg->ns_mode); + ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + + return ret; } static int cmd_dev_set_ns(admin_config_t *cfg) @@ -86,8 +101,26 @@ static int cmd_dev_expose(admin_config_t *cfg) return ret; } - printf("TODO expose %s to %s\n", cfg->dev_name, cfg->ns); - return 0; + int ns_fd = admin_get_ns_fd(cfg->ns); + if (ns_fd < 0) { + (void)printf("Failed to get ns fd, ns %s.\n", cfg->ns); + return ns_fd; + } + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_EXPOSE_DEV_NS, 0); + if (msg == NULL) { + ret = -ENOMEM; + goto close_ns_fd; + } + + admin_nl_put_string(msg, UBCORE_ATTR_DEV_NAME, cfg->dev_name); + admin_nl_put_u32(msg, UBCORE_ATTR_NS_FD, ns_fd); + ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + +close_ns_fd: + (void)close(ns_fd); + return ret; } static int cmd_dev_unexpose(admin_config_t *cfg) @@ -100,8 +133,26 @@ static int cmd_dev_unexpose(admin_config_t *cfg) return ret; } - printf("TODO unexpose %s to %s\n", cfg->dev_name, cfg->ns); - return 0; + int ns_fd = admin_get_ns_fd(cfg->ns); + if (ns_fd < 0) { + (void)printf("Failed to get ns fd, ns %s.\n", cfg->ns); + return ns_fd; + } + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_UNEXPOSE_DEV_NS, 0); + if (msg == NULL) { + ret = -ENOMEM; + goto close_ns_fd; + } + + admin_nl_put_string(msg, UBCORE_ATTR_DEV_NAME, cfg->dev_name); + admin_nl_put_u32(msg, UBCORE_ATTR_NS_FD, ns_fd); + ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + +close_ns_fd: + (void)close(ns_fd); + return ret; } int admin_cmd_dev(admin_config_t *cfg) diff --git a/src/urma/tools/urma_admin/admin_netlink.h b/src/urma/tools/urma_admin/admin_netlink.h index f7cde4d..0cdec01 100644 --- a/src/urma/tools/urma_admin/admin_netlink.h +++ b/src/urma/tools/urma_admin/admin_netlink.h @@ -57,7 +57,16 @@ static inline int admin_nl_put_u32(struct nl_msg *msg, int attr, uint32_t value) { int ret = nla_put_u32(msg, attr, value); if (ret != 0) { - printf("Failed to put string attribute %d, ret: %d\n", attr, ret); + printf("Failed to put u32 attribute %d, ret: %d\n", attr, ret); + } + return ret; +} + +static inline int admin_nl_put_u8(struct nl_msg *msg, int attr, uint8_t value) +{ + int ret = nla_put_u8(msg, attr, value); + if (ret != 0) { + printf("Failed to put u8 attribute %d, ret: %d\n", attr, ret); } return ret; } diff --git a/src/urma/tools/urma_admin/admin_parameters.c b/src/urma/tools/urma_admin/admin_parameters.c index ac78d00..befec28 100644 --- a/src/urma/tools/urma_admin/admin_parameters.c +++ b/src/urma/tools/urma_admin/admin_parameters.c @@ -295,6 +295,25 @@ static int admin_parse_ns(char *buf, tool_config_t *cfg) return 0; } +static int admin_parse_sharing(char *buf, tool_config_t *cfg) +{ + if (buf == NULL) { + (void)printf("Invalid argument.\n"); + return -EINVAL; + } + + // 先复用ns_mode + if (strcmp(buf, "on") == 0) { + cfg->ns_mode = 1; + } else if (strcmp(buf, "off") == 0) { + cfg->ns_mode = 0; + } else { + URMA_ADMIN_LOG("Invalid sharing mode:%s, expect 'on' or 'off'.\n", buf); + return -1; + } + return 0; +} + int admin_parse_args(int argc, char *argv[], tool_config_t *cfg) { int ret = 0; @@ -398,6 +417,17 @@ int pop_arg_ns(admin_config_t *cfg) return ret; } +int pop_arg_sharing(admin_config_t *cfg) +{ + char *arg = pop_arg(cfg); + if (arg == NULL) { + printf("No sharing mode specified.\n"); + return -EINVAL; + } + int ret = admin_parse_sharing(arg, cfg); + return ret; +} + int pop_arg_eid(admin_config_t *cfg) { char *arg = pop_arg(cfg); diff --git a/src/urma/tools/urma_admin/admin_parameters.h b/src/urma/tools/urma_admin/admin_parameters.h index c84e421..746ed06 100644 --- a/src/urma/tools/urma_admin/admin_parameters.h +++ b/src/urma/tools/urma_admin/admin_parameters.h @@ -300,6 +300,7 @@ void usage(const char *argv0); char *pop_arg(admin_config_t *cmds); int pop_arg_dev(admin_config_t *cmds); int pop_arg_ns(admin_config_t *cfg); +int pop_arg_sharing(admin_config_t *cfg); int pop_arg_eid(admin_config_t *cfg); int pop_arg_eid_idx(admin_config_t *cfg); -- Gitee