From 513cb0f391d4df3d3028d7a8c3c1b5282c59fb54 Mon Sep 17 00:00:00 2001 From: wanghang73 Date: Wed, 31 Dec 2025 14:17:05 +0800 Subject: [PATCH] urma: refactor urma_admin eid subcommand --- src/urma/lib/urma/core/include/urma_cmd.h | 1 + src/urma/tools/urma_admin/admin_cmd.c | 305 +------------------ src/urma/tools/urma_admin/admin_cmd.h | 12 +- src/urma/tools/urma_admin/admin_cmd_dev.c | 39 +++ src/urma/tools/urma_admin/admin_cmd_eid.c | 206 ++++++++++++- src/urma/tools/urma_admin/admin_netlink.c | 8 +- src/urma/tools/urma_admin/admin_netlink.h | 16 +- src/urma/tools/urma_admin/admin_parameters.c | 23 ++ src/urma/tools/urma_admin/admin_parameters.h | 1 + 9 files changed, 291 insertions(+), 320 deletions(-) diff --git a/src/urma/lib/urma/core/include/urma_cmd.h b/src/urma/lib/urma/core/include/urma_cmd.h index 5dda3e6..0f3db31 100644 --- a/src/urma/lib/urma/core/include/urma_cmd.h +++ b/src/urma/lib/urma/core/include/urma_cmd.h @@ -42,6 +42,7 @@ typedef enum urma_core_cmd { URMA_CORE_SET_DEV_SHARING_MODE, URMA_CORE_EXPOSE_DEV_NS, URMA_CORE_UNEXPOSE_DEV_NS, + URMA_CORE_SET_DEV_EID_NS, URMA_CORE_GET_TOPO_INFO, } urma_core_cmd_t; diff --git a/src/urma/tools/urma_admin/admin_cmd.c b/src/urma/tools/urma_admin/admin_cmd.c index 19e5ff2..e97f24b 100644 --- a/src/urma/tools/urma_admin/admin_cmd.c +++ b/src/urma/tools/urma_admin/admin_cmd.c @@ -116,208 +116,6 @@ static struct nl_sock *alloc_and_connect_nl(int *genl_id) return sock; } -static int urma_admin_cmd_add_eid(struct nl_sock *sock, const tool_config_t *cfg, int genl_id) -{ - int ret; - urma_cmd_hdr_t hdr; - admin_core_cmd_update_eid_t arg = {0}; - int ns_fd = -1; - - hdr.command = (uint32_t)URMA_CORE_CMD_ADD_EID; - hdr.args_len = (uint32_t)sizeof(admin_core_cmd_update_eid_t); - hdr.args_addr = (uint64_t)&arg; - - (void)memcpy(arg.in.dev_name, cfg->dev_name, URMA_ADMIN_MAX_DEV_NAME); - arg.in.eid_index = cfg->idx; - if (strlen(cfg->ns) > 0 && (ns_fd = admin_get_ns_fd(cfg->ns)) < 0) { - (void)printf("set ns failed, cmd:%u, ns %s.\n", hdr.command, cfg->ns); - return -1; - } - arg.in.ns_fd = ns_fd; - ret = cmd_nlsend(sock, genl_id, &hdr); - if (ret < 0) { - (void)close(ns_fd); - (void)printf("cmd_nlsend failed, ret:%d, errno:%d, cmd:%u.\n", ret, errno, hdr.command); - return ret; - } - ret = nl_recvmsgs_default(sock); - if (ret < 0) { - (void)printf("Failed to nl_recvmsgs_default, ret: %d, command: %u, errno: %d.\n", ret, hdr.command, errno); - } - (void)close(ns_fd); - return ret; -} - -static int urma_admin_cmd_del_eid(struct nl_sock *sock, const tool_config_t *cfg, int genl_id) -{ - int ret; - urma_cmd_hdr_t hdr; - admin_core_cmd_update_eid_t arg = {0}; - - hdr.command = (uint32_t)URMA_CORE_CMD_DEL_EID; - hdr.args_len = (uint32_t)sizeof(admin_core_cmd_update_eid_t); - hdr.args_addr = (uint64_t)&arg; - - (void)memcpy(arg.in.dev_name, cfg->dev_name, URMA_ADMIN_MAX_DEV_NAME); - arg.in.eid_index = cfg->idx; - arg.in.ns_fd = -1; - ret = cmd_nlsend(sock, genl_id, &hdr); - if (ret < 0) { - (void)printf("cmd_nlsend failed, ret:%d, errno:%d, cmd:%u.\n", ret, errno, hdr.command); - return ret; - } - ret = nl_recvmsgs_default(sock); - if (ret < 0) { - (void)printf("Failed to nl_recvmsgs_default, ret: %d, command: %u, errno: %d.\n", ret, hdr.command, errno); - } - return ret; -} - -static int urma_admin_cmd_set_eid_mode(struct nl_sock *sock, const tool_config_t *cfg, int genl_id) -{ - int ret; - urma_cmd_hdr_t hdr; - admin_core_cmd_set_eid_mode_t arg = {0}; - - hdr.command = (uint32_t)URMA_CORE_CMD_SET_EID_MODE; - hdr.args_len = (uint32_t)sizeof(admin_core_cmd_set_eid_mode_t); - hdr.args_addr = (uint64_t)&arg; - - (void)memcpy(arg.in.dev_name, cfg->dev_name, URMA_ADMIN_MAX_DEV_NAME); - arg.in.eid_mode = cfg->dynamic_eid_mode; - ret = cmd_nlsend(sock, genl_id, &hdr); - if (ret < 0) { - (void)printf("cmd_nlsend failed, ret:%d, errno:%d, cmd:%u.\n", ret, errno, hdr.command); - return ret; - } - ret = nl_recvmsgs_default(sock); - if (ret < 0) { - (void)printf("Failed to nl_recvmsgs_default, ret: %d, command: %u, errno: %d.\n", ret, hdr.command, errno); - } - return ret; -} - -static int cb_update_eid_handler(struct nl_msg *msg, void *arg) -{ - struct nlmsghdr *hdr = nlmsg_hdr(msg); - struct genlmsghdr *genlhdr = genlmsg_hdr(hdr); - struct nlattr *attr_ptr = genlmsg_data(genlhdr); - int *ret = arg; - - if (arg == NULL) { - return 0; - } - - if (genlhdr->cmd != (int)URMA_CORE_CMD_ADD_EID && genlhdr->cmd != (int)URMA_CORE_CMD_DEL_EID) { - return 0; - } - - *ret = nla_get_s32(attr_ptr); - if (*ret == 0) { - return 0; - } else if (*ret == 1) { - (void)usleep(1); // ret == 1 means in progress, genl will try again. - } else { - (void)printf("Failed to %s, invalid parameter.\n", - (genlhdr->cmd == (int)URMA_CORE_CMD_ADD_EID) ? "add eid" : "del eid"); - } - - return 0; -} - -int admin_add_eid(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - int ret = 0; - - if (*cfg->dev_name && is_1650(cfg->dev_name)) { - (void)printf("This operation is not supported on 1650.\n"); - return -1; - } - - sock = alloc_and_connect_nl(&genl_id); - if (sock == NULL) { - return -1; - } - (void)nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, cb_update_eid_handler, &ret); - /* Automatically switch to static mode */ - if (urma_admin_cmd_set_eid_mode(sock, cfg, genl_id) < 0) { - (void)printf("Failed to urma admin set eid mode, errno:%d\n", errno); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - if (urma_admin_cmd_add_eid(sock, cfg, genl_id) < 0) { - (void)printf("Failed to urma admin add eid, errno:%d\n", errno); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - nl_close(sock); - nl_socket_free(sock); - return ret; -} - -int admin_del_eid(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - int ret = 0; - - if (*cfg->dev_name && is_1650(cfg->dev_name)) { - (void)printf("This operation is not supported on 1650.\n"); - return -1; - } - - sock = alloc_and_connect_nl(&genl_id); - if (sock == NULL) { - return -1; - } - (void)nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, cb_update_eid_handler, &ret); - /* Automatically switch to static mode */ - if (urma_admin_cmd_set_eid_mode(sock, cfg, genl_id) < 0) { - (void)printf("Failed to urma admin set eid mode, errno:%d\n", errno); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - if (urma_admin_cmd_del_eid(sock, cfg, genl_id) < 0) { - (void)printf("Failed to urma admin del eid, errno:%d\n", errno); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - nl_close(sock); - nl_socket_free(sock); - return ret; -} - -int admin_set_eid_mode(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - - if (*cfg->dev_name && is_1650(cfg->dev_name)) { - (void)printf("This operation is not supported on 1650.\n"); - return -1; - } - - sock = alloc_and_connect_nl(&genl_id); - if (sock == NULL) { - return -1; - } - if (urma_admin_cmd_set_eid_mode(sock, cfg, genl_id) < 0) { - (void)printf("Failed to urma admin del eid, errno:%d\n", errno); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - nl_close(sock); - nl_socket_free(sock); - return 0; -} - static inline void admin_print_stats(const admin_cmd_query_stats_t *arg) { (void)printf("tx_pkt : %lu\n", arg->out.tx_pkt); @@ -970,99 +768,6 @@ int admin_list_res(tool_config_t *cfg) return 0; } -static int ns_cb_handler(struct nl_msg *msg, void *arg) -{ - return NL_OK; -} - -static int admin_nl_send_recv(struct nl_sock *sock, struct nl_msg *msg) -{ - int ret = nl_send_auto(sock, msg); - if (ret < 0) { - (void)printf("Netlink send failed, ret:%d, errno: %d..\n", ret, errno); - return ret; - } - - ret = nl_recvmsgs_default(sock); - if (ret < 0) { - (void)printf("Netlink recv failed, ret:%d, errno:%d.\n", ret, errno); - } - return ret; -} - -int admin_set_ns_mode(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - - sock = alloc_and_connect_nl(&genl_id); - if (sock == NULL) { - return -1; - } - - nl_socket_modify_cb(sock, NL_CB_VALID, NL_CB_CUSTOM, ns_cb_handler, NULL); - - void *msg_hdr; - struct nl_msg *msg; - int ret = 0, nlmsg_flags = 0; - - msg = nlmsg_alloc(); - if (msg == NULL) { - (void)printf("Unable to allocate netlink message\n"); - ret = -ENOMEM; - goto close_sock; - } - - msg_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, genl_id, 0, nlmsg_flags, URMA_CORE_SET_NS_MODE, - UBCORE_GENL_FAMILY_VERSION); - if (msg_hdr == NULL) { - (void)printf("Unable to write genl header\n"); - ret = -ENOMEM; - goto out; - } - - ret = nla_put_u8(msg, UBCORE_ATTR_NS_MODE, cfg->ns_mode); - if (ret < 0) { - (void)printf("Unable to add ns mode: %d\n", ret); - goto out; - } - - ret = admin_nl_send_recv(sock, msg); - -out: - nlmsg_free(msg); -close_sock: - nl_close(sock); - nl_socket_free(sock); - return ret; -} - -int admin_set_dev_ns(tool_config_t *cfg) -{ - int ns_fd = admin_get_ns_fd(cfg->ns); - if (ns_fd < 0) { - (void)printf("set ns failed, ns %s.\n", cfg->ns); - return ns_fd; - } - - int ret = 0; - - struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_SET_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 admin_set_reserved_jetty_id_range(tool_config_t *cfg) { char jetty_id_range[VALUE_LEN_MAX] = {0}; @@ -1085,13 +790,13 @@ int admin_cmd_main(admin_config_t *cfg) { static const admin_cmd_t cmds[] = { {NULL, admin_cmd_show}, - {"add_eid", admin_add_eid}, - {"del_eid", admin_del_eid}, - {"set_eid_mode", admin_set_eid_mode}, + {"add_eid", admin_cmd_add_eid_legacy}, + {"del_eid", admin_cmd_del_eid_legacy}, + {"set_eid_mode", admin_cmd_set_eid_mode_legacy}, {"show_stats", admin_show_stats}, {"show_res", admin_show_res}, - {"set_ns_mode", admin_set_ns_mode}, - {"set_dev_ns", admin_set_dev_ns}, + {"set_ns_mode", admin_cmd_set_ns_mode_legacy}, + {"set_dev_ns", admin_cmd_set_dev_ns_legacy}, {"set_reserved_jetty", admin_set_reserved_jetty_id_range}, {"list_res", admin_list_res}, // diff --git a/src/urma/tools/urma_admin/admin_cmd.h b/src/urma/tools/urma_admin/admin_cmd.h index 8bbcb16..26b953e 100644 --- a/src/urma/tools/urma_admin/admin_cmd.h +++ b/src/urma/tools/urma_admin/admin_cmd.h @@ -92,6 +92,7 @@ enum { UBCORE_ATTR_NS_MODE, UBCORE_ATTR_DEV_NAME, UBCORE_ATTR_NS_FD, + UBCORE_ATTR_EID_IDX, UBCORE_ATTR_AFTER_LAST }; @@ -167,13 +168,12 @@ struct cmd_agg_del_arg { int admin_show_utp(const tool_config_t *cfg); int admin_show_stats(tool_config_t *cfg); int admin_show_res(tool_config_t *cfg); -int admin_add_eid(tool_config_t *cfg); -int admin_del_eid(tool_config_t *cfg); -int admin_set_eid_mode(tool_config_t *cfg); -int admin_set_ns_mode(tool_config_t *cfg); -int admin_set_dev_ns(tool_config_t *cfg); +int admin_cmd_add_eid_legacy(admin_config_t *cfg); +int admin_cmd_del_eid_legacy(admin_config_t *cfg); +int admin_cmd_set_eid_mode_legacy(admin_config_t *cfg); +int admin_cmd_set_ns_mode_legacy(admin_config_t *cfg); +int admin_cmd_set_dev_ns_legacy(admin_config_t *cfg); int admin_list_res(tool_config_t *cfg); -int admin_show_topo_info(tool_config_t *cfg); // New command int admin_cmd_main(admin_config_t *cfg); diff --git a/src/urma/tools/urma_admin/admin_cmd_dev.c b/src/urma/tools/urma_admin/admin_cmd_dev.c index 6e9f34d..5c2bc43 100644 --- a/src/urma/tools/urma_admin/admin_cmd_dev.c +++ b/src/urma/tools/urma_admin/admin_cmd_dev.c @@ -166,3 +166,42 @@ int admin_cmd_dev(admin_config_t *cfg) }; return exec_cmd(cfg, cmds); } + +// Legacy cmd +int admin_cmd_set_ns_mode_legacy(admin_config_t *cfg) +{ + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_SET_NS_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); + + int ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + return ret; +} + +int admin_cmd_set_dev_ns_legacy(admin_config_t *cfg) +{ + int ns_fd = admin_get_ns_fd(cfg->ns); + if (ns_fd < 0) { + (void)printf("set ns failed, ns %s.\n", cfg->ns); + return ns_fd; + } + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_SET_DEV_NS, 0); + if (msg == NULL) { + close(ns_fd); + return -ENOMEM; + } + + admin_nl_put_string(msg, UBCORE_ATTR_DEV_NAME, cfg->dev_name); + admin_nl_put_u32(msg, UBCORE_ATTR_NS_FD, ns_fd); + + int ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + close(ns_fd); + return ret; +} diff --git a/src/urma/tools/urma_admin/admin_cmd_eid.c b/src/urma/tools/urma_admin/admin_cmd_eid.c index 088c8ab..5a6b750 100644 --- a/src/urma/tools/urma_admin/admin_cmd_eid.c +++ b/src/urma/tools/urma_admin/admin_cmd_eid.c @@ -8,10 +8,126 @@ * History: 2025-12-26 create file */ +#include #include +#include + +#include "admin_netlink.h" #include "admin_cmd.h" +static int nl_set_eid_mode(const admin_config_t *cfg) +{ + admin_core_cmd_set_eid_mode_t arg = {0}; + (void)memcpy(arg.in.dev_name, cfg->dev_name, URMA_ADMIN_MAX_DEV_NAME); + arg.in.eid_mode = cfg->dynamic_eid_mode; + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_CMD_SET_EID_MODE, 0); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_u32(msg, UBCORE_HDR_ARGS_LEN, (uint32_t)sizeof(admin_core_cmd_set_eid_mode_t)); + admin_nl_put_u64(msg, UBCORE_HDR_ARGS_ADDR, (uint64_t)(uintptr_t)&arg); + + int ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + return ret; +} + +static int nl_set_eid_ns(const admin_config_t *cfg) +{ + 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_SET_DEV_EID_NS, 0); + if (msg == NULL) { + close(ns_fd); + return -ENOMEM; + } + + admin_nl_put_string(msg, UBCORE_ATTR_DEV_NAME, cfg->dev_name); + admin_nl_put_u32(msg, UBCORE_ATTR_EID_IDX, cfg->idx); + admin_nl_put_u32(msg, UBCORE_ATTR_NS_FD, ns_fd); + + int ret = admin_nl_send_recv_msg_default(msg); + admin_nl_free_msg(msg); + close(ns_fd); + return ret; +} + +static int cb_update_eid_handler(struct nl_msg *msg, void *arg) +{ + struct nlmsghdr *hdr = nlmsg_hdr(msg); + struct genlmsghdr *genlhdr = genlmsg_hdr(hdr); + struct nlattr *attr_ptr = genlmsg_data(genlhdr); + int *ret = arg; + + if (arg == NULL) { + return 0; + } + + *ret = nla_get_s32(attr_ptr); + if (*ret == 0) { + return 0; + } else if (*ret == 1) { + (void)usleep(1); // ret == 1 means in progress, genl will try again. + } else { + (void)printf("Failed to %s, invalid parameter.\n", + (genlhdr->cmd == (int)URMA_CORE_CMD_ADD_EID) ? "add eid" : "del eid"); + } + + return 0; +} + +static int nl_add_eid(const admin_config_t *cfg) +{ + admin_core_cmd_update_eid_t arg = {0}; + (void)memcpy(arg.in.dev_name, cfg->dev_name, URMA_ADMIN_MAX_DEV_NAME); + arg.in.eid_index = cfg->idx; + if (strlen(cfg->ns) > 0 && (arg.in.ns_fd = admin_get_ns_fd(cfg->ns)) < 0) { + (void)printf("set ns failed, ns %s.\n", cfg->ns); + return -1; + } + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_CMD_ADD_EID, 0); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_u32(msg, UBCORE_HDR_ARGS_LEN, (uint32_t)sizeof(admin_core_cmd_update_eid_t)); + admin_nl_put_u64(msg, UBCORE_HDR_ARGS_ADDR, (uint64_t)(uintptr_t)&arg); + + int cb_ret = 0; + int ret = admin_nl_send_recv_msg(msg, cb_update_eid_handler, &cb_ret); + admin_nl_free_msg(msg); + return cb_ret | ret; +} + +static int nl_del_eid(const admin_config_t *cfg) +{ + admin_core_cmd_update_eid_t arg = {0}; + (void)memcpy(arg.in.dev_name, cfg->dev_name, URMA_ADMIN_MAX_DEV_NAME); + arg.in.eid_index = cfg->idx; + arg.in.ns_fd = -1; + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_CMD_DEL_EID, 0); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_u32(msg, UBCORE_HDR_ARGS_LEN, (uint32_t)sizeof(admin_core_cmd_update_eid_t)); + admin_nl_put_u64(msg, UBCORE_HDR_ARGS_ADDR, (uint64_t)(uintptr_t)&arg); + + int cb_ret = 0; + int ret = admin_nl_send_recv_msg(msg, cb_update_eid_handler, &cb_ret); + admin_nl_free_msg(msg); + return ret | cb_ret; +} + static int cmd_eid_usage(admin_config_t *cfg) { printf("Usage: urma_admin eid add [ DEV ] [ EID_IDX ] [ EID ] --ns [ NETNS ] --mode [ EID_MODE ]\n" @@ -35,7 +151,15 @@ static int cmd_eid_add(admin_config_t *cfg) return ret; } - printf("TODO add eid %s %u\n", cfg->dev_name, cfg->idx); + /* Automatically switch to static mode */ + if ((ret = nl_set_eid_mode(cfg)) != 0) { + printf("Failed to set eid mode, ret:%d\n", ret); + return ret; + } + if ((ret = nl_add_eid(cfg)) != 0) { + printf("Failed to add eid, ret:%d\n", ret); + return ret; + } return 0; } @@ -49,19 +173,39 @@ static int cmd_eid_del(admin_config_t *cfg) return ret; } - printf("TODO del eid %s %u\n", cfg->dev_name, cfg->idx); + /* Automatically switch to static mode */ + if ((ret = nl_set_eid_mode(cfg)) < 0) { + printf("Failed to set eid mode, ret:%d\n", ret); + return ret; + } + if ((ret = nl_del_eid(cfg)) != 0) { + printf("Failed to delete eid, ret:%d\n", ret); + return ret; + } return 0; } static int cmd_eid_set_mode(admin_config_t *cfg) { - printf("TODO set eid mode %s %u\n", cfg->dev_name, cfg->idx); + int ret; + if ((ret = pop_arg_eid_mode(cfg)) != 0) { + return ret; + } + if ((ret = nl_set_eid_mode(cfg)) != 0) { + return ret; + } return 0; } static int cmd_eid_set_ns(admin_config_t *cfg) { - printf("TODO set eid ns %s %u\n", cfg->dev_name, cfg->idx); + int ret; + if ((ret = pop_arg_ns(cfg)) != 0) { + return ret; + } + if ((ret = nl_set_eid_ns(cfg)) != 0) { + return ret; + } return 0; } @@ -95,3 +239,57 @@ int admin_cmd_eid(admin_config_t *cfg) }; return exec_cmd(cfg, cmds); } + +// Legacy cmd +int admin_cmd_add_eid_legacy(admin_config_t *cfg) +{ + if (*cfg->dev_name && is_1650(cfg->dev_name)) { + (void)printf("This operation is not supported on 1650.\n"); + return -1; + } + + int ret; + + /* Automatically switch to static mode */ + if ((ret = nl_set_eid_mode(cfg)) != 0) { + printf("Failed to set eid mode, ret:%d\n", ret); + return ret; + } + if ((ret = nl_add_eid(cfg)) != 0) { + printf("Failed to add eid, ret:%d\n", ret); + return ret; + } + + return 0; +} + +int admin_cmd_del_eid_legacy(admin_config_t *cfg) +{ + if (*cfg->dev_name && is_1650(cfg->dev_name)) { + (void)printf("This operation is not supported on 1650.\n"); + return -1; + } + + int ret; + + /* Automatically switch to static mode */ + if ((ret = nl_set_eid_mode(cfg)) < 0) { + printf("Failed to set eid mode, ret:%d\n", ret); + return ret; + } + if ((ret = nl_del_eid(cfg)) != 0) { + printf("Failed to delete eid, ret:%d\n", ret); + return ret; + } + + return 0; +} + +int admin_cmd_set_eid_mode_legacy(admin_config_t *cfg) +{ + if (*cfg->dev_name && is_1650(cfg->dev_name)) { + (void)printf("This operation is not supported on 1650.\n"); + return -1; + } + return nl_set_eid_mode(cfg); +} diff --git a/src/urma/tools/urma_admin/admin_netlink.c b/src/urma/tools/urma_admin/admin_netlink.c index fdfed63..ade3cc9 100644 --- a/src/urma/tools/urma_admin/admin_netlink.c +++ b/src/urma/tools/urma_admin/admin_netlink.c @@ -166,7 +166,13 @@ int admin_nl_recv_msg(int (*cb)(struct nl_msg *msg, void *arg), void *arg) ret = nl_recvmsgs_default(sock); if (ret < 0) { - printf("Netlink recv failed, ret:%d\n", ret); + printf("Failed to recv netlink msg, ret:%d\n", ret); + return ret; + } + + ret = nl_socket_modify_cb(sock, NL_CB_MSG_IN, NL_CB_CUSTOM, NULL, arg); + if (ret < 0) { + printf("Failed to reset netlink callback, ret:%d\n", ret); return ret; } diff --git a/src/urma/tools/urma_admin/admin_netlink.h b/src/urma/tools/urma_admin/admin_netlink.h index 0cdec01..94374db 100644 --- a/src/urma/tools/urma_admin/admin_netlink.h +++ b/src/urma/tools/urma_admin/admin_netlink.h @@ -53,20 +53,20 @@ static inline int admin_nl_put_string(struct nl_msg *msg, int attr, const char * return ret; } -static inline int admin_nl_put_u32(struct nl_msg *msg, int attr, uint32_t value) +static inline int admin_nl_put_u8(struct nl_msg *msg, int attr, uint8_t value) { - int ret = nla_put_u32(msg, attr, value); + int ret = nla_put_u8(msg, attr, value); if (ret != 0) { - printf("Failed to put u32 attribute %d, ret: %d\n", attr, ret); + printf("Failed to put u8 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) +static inline int admin_nl_put_u32(struct nl_msg *msg, int attr, uint32_t value) { - int ret = nla_put_u8(msg, attr, value); + int ret = nla_put_u32(msg, attr, value); if (ret != 0) { - printf("Failed to put u8 attribute %d, ret: %d\n", attr, ret); + printf("Failed to put u32 attribute %d, ret: %d\n", attr, ret); } return ret; } @@ -75,11 +75,9 @@ static inline int admin_nl_put_u64(struct nl_msg *msg, int attr, uint64_t value) { int ret = nla_put_u64(msg, attr, value); if (ret != 0) { - printf("Failed to put string attribute %d, ret: %d\n", attr, ret); + printf("Failed to put u64 attribute %d, ret: %d\n", attr, ret); } return ret; } -int cmd_nlsend_legacy(struct nl_msg *msg, urma_cmd_hdr_t *hdr); - #endif diff --git a/src/urma/tools/urma_admin/admin_parameters.c b/src/urma/tools/urma_admin/admin_parameters.c index befec28..2e9f16a 100644 --- a/src/urma/tools/urma_admin/admin_parameters.c +++ b/src/urma/tools/urma_admin/admin_parameters.c @@ -450,6 +450,29 @@ int pop_arg_eid_idx(admin_config_t *cfg) return ret; } +int pop_arg_eid_mode(admin_config_t *cfg) +{ + char *arg = pop_arg(cfg); + if (arg == NULL) { + printf("No eid mode specified.\n"); + return -EINVAL; + } + + const char *eid_mode_static = "static"; + const char *eid_mode_dynamic = "dynamic"; + + if (strncmp(arg, eid_mode_static, strlen(eid_mode_static) + 1) == 0) { + cfg->dynamic_eid_mode = false; + } else if (strncmp(arg, eid_mode_dynamic, strlen(eid_mode_dynamic) + 1) == 0) { + cfg->dynamic_eid_mode = true; + } else { + printf("Invalid eid mode:%s, expect 'dynamic' or 'static'.\n", arg); + return -EINVAL; + } + + return 0; +} + #define ADMIN_NET_NS_PATH_MAX_LEN 256 /* Path1 format: /var/run/netns/$ns_name */ #define ADMIN_NET_NS_PATH1_PREFIX "/var/run/netns/" diff --git a/src/urma/tools/urma_admin/admin_parameters.h b/src/urma/tools/urma_admin/admin_parameters.h index 746ed06..23a1e08 100644 --- a/src/urma/tools/urma_admin/admin_parameters.h +++ b/src/urma/tools/urma_admin/admin_parameters.h @@ -303,6 +303,7 @@ 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); +int pop_arg_eid_mode(admin_config_t *cfg); int admin_get_ns_fd(const char *ns); -- Gitee