diff --git a/src/urma/lib/urma/core/include/urma_cmd.h b/src/urma/lib/urma/core/include/urma_cmd.h index b6a66214590603f85d5b1dcb84c3aceb7b35828c..5dda3e6fd18600c253c5e098d50e6ad9b557b445 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 965ed9783fc3446a49e14a35af75dcbed543a274..6e9f34d8c4d7363f2e94dd1280ad6c81dddb1583 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 f7cde4dc6cd7a5822516d563a24162d4dc371712..0cdec018a8311361f030c63c890808cf85f7dd25 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 ac78d005f93f898d28ca840b5400d88125a512b3..befec28d9b4888aa8322cade5594c64e243c3d5f 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 c84e42142ccd77c070a65bc19c8ce7713a3666b5..746ed0651bc9650a1ba126fff18c2120595ca88e 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);