From 513cb0f391d4df3d3028d7a8c3c1b5282c59fb54 Mon Sep 17 00:00:00 2001 From: wanghang73 Date: Wed, 31 Dec 2025 14:17:05 +0800 Subject: [PATCH 1/2] 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 From 7a4b323090610a9d7404b15dc097095a917cc135 Mon Sep 17 00:00:00 2001 From: wanghang73 Date: Wed, 31 Dec 2025 16:19:12 +0800 Subject: [PATCH 2/2] urma: refactor urma_admin res subcommand --- src/urma/tools/urma_admin/CMakeLists.txt | 1 + src/urma/tools/urma_admin/admin_cmd.c | 739 +--------------------- src/urma/tools/urma_admin/admin_cmd.h | 6 +- src/urma/tools/urma_admin/admin_cmd_res.c | 571 +++++++++++++++++ 4 files changed, 579 insertions(+), 738 deletions(-) create mode 100644 src/urma/tools/urma_admin/admin_cmd_res.c diff --git a/src/urma/tools/urma_admin/CMakeLists.txt b/src/urma/tools/urma_admin/CMakeLists.txt index 4827efa..1660163 100644 --- a/src/urma/tools/urma_admin/CMakeLists.txt +++ b/src/urma/tools/urma_admin/CMakeLists.txt @@ -7,6 +7,7 @@ add_executable(urma_admin admin_cmd_agg.c admin_cmd_dev.c admin_cmd_eid.c + admin_cmd_res.c admin_cmd_show.c admin_cmd.c admin_file_ops.c diff --git a/src/urma/tools/urma_admin/admin_cmd.c b/src/urma/tools/urma_admin/admin_cmd.c index e97f24b..7e6347e 100644 --- a/src/urma/tools/urma_admin/admin_cmd.c +++ b/src/urma/tools/urma_admin/admin_cmd.c @@ -37,738 +37,7 @@ #include "admin_cmd.h" -typedef struct netlink_cb_par { - uint32_t type; - uint32_t key; -} netlink_cb_par; - -static int cmd_nlsend(struct nl_sock *sock, int genl_id, urma_cmd_hdr_t *hdr) -{ - 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"); - return -1; - } - - if (hdr->command == URMA_CORE_CMD_QUERY_RES || hdr->command == URMA_CORE_CMD_ADD_EID || - hdr->command == URMA_CORE_CMD_DEL_EID) { - nlmsg_flags = NLM_F_DUMP; - } - - msg_hdr = genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, genl_id, 0, nlmsg_flags, (uint8_t)hdr->command, - UBCORE_GENL_FAMILY_VERSION); - if (msg_hdr == NULL) { - (void)printf("Unable to write genl header\n"); - nlmsg_free(msg); - return -1; - } - - ret = nla_put_u32(msg, UBCORE_HDR_ARGS_LEN, hdr->args_len); - if (ret < 0) { - (void)printf("Unable to add args_len: %d\n", ret); - nlmsg_free(msg); - return ret; - } - - ret = nla_put_u64(msg, UBCORE_HDR_ARGS_ADDR, hdr->args_addr); - if (ret < 0) { - (void)printf("Unable to add args_addr: %d\n", ret); - nlmsg_free(msg); - return ret; - } - - ret = nl_send_auto(sock, msg); - if (ret < 0) { - (void)printf("Netlink send failed, ret:%d, errno:%d, cmd:%u.\n", ret, errno, hdr->command); - nlmsg_free(msg); - return ret; - } - - nlmsg_free(msg); - return ret; -} - -static struct nl_sock *alloc_and_connect_nl(int *genl_id) -{ - int ret; - struct nl_sock *sock = nl_socket_alloc(); - if (!sock) { - (void)printf("Failed to nl_socket_alloc\n"); - return NULL; - } - ret = genl_connect(sock); - if (ret < 0) { - (void)printf("Failed to nl_connect, ret:%d, errno:%d\n", ret, errno); - nl_socket_free(sock); - return NULL; - } - *genl_id = genl_ctrl_resolve(sock, UBCORE_GENL_FAMILY_NAME); - if (*genl_id < 0) { - (void)printf("Resolving of \"%s\" failed, ret:%d\n", UBCORE_GENL_FAMILY_NAME, *genl_id); - nl_close(sock); - nl_socket_free(sock); - return NULL; - } - return sock; -} - -static inline void admin_print_stats(const admin_cmd_query_stats_t *arg) -{ - (void)printf("tx_pkt : %lu\n", arg->out.tx_pkt); - (void)printf("rx_pkt : %lu\n", arg->out.rx_pkt); - (void)printf("tx_bytes : %lu\n", arg->out.tx_bytes); - (void)printf("rx_bytes : %lu\n", arg->out.rx_bytes); - (void)printf("tx_pkt_err : %lu\n", arg->out.tx_pkt_err); - (void)printf("rx_pkt_err : %lu\n", arg->out.rx_pkt_err); -} - -static int admin_cmd_query_stats(struct nl_sock *sock, const tool_config_t *cfg, int genl_id) -{ - urma_cmd_hdr_t hdr; - admin_cmd_query_stats_t arg = {0}; - - hdr.command = (uint32_t)URMA_CORE_CMD_QUERY_STATS; - hdr.args_len = (uint32_t)sizeof(admin_cmd_query_stats_t); - hdr.args_addr = (uint64_t)&arg; - - (void)memcpy(arg.in.dev_name, cfg->dev_name, strlen(cfg->dev_name)); - arg.in.key = cfg->key.key; - arg.in.type = (uint32_t)cfg->key.type; - - int ret = cmd_nlsend(sock, genl_id, &hdr); - if (ret < 0) { - (void)printf("Failed to cmd_nlsend, ret: %d, command: %u, errno: %d.\n", ret, hdr.command, errno); - return ret; - } - - ret = nl_recvmsgs_default(sock); - if (ret < 0) { - (void)printf("query stats fail, please check input, ret:%d, errno:%d.\n", ret, errno); - return ret; - } - - admin_print_stats(&arg); - return 0; -} - -int admin_show_stats(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - - if (cfg->key.type < TOOL_STATS_KEY_VTP || cfg->key.type > TOOL_STATS_KEY_URMA_DEV) { - (void)printf("Invalid type: %d.\n", (int)cfg->key.type); - return -1; - } - if (cfg->key.type == TOOL_STATS_KEY_TPG || cfg->key.type == TOOL_STATS_KEY_JETTY_GROUP) { - (void)printf("Type: %d currently not supported.\n", (int)cfg->key.type); - return -1; - } - if (cfg->key.type >= TOOL_STATS_KEY_VTP && cfg->key.type <= TOOL_STATS_KEY_TPG) { - (void)printf("urma_admin do not support query tp stats.\n"); - return -1; - } - sock = alloc_and_connect_nl(&genl_id); - if (sock == NULL) { - return -1; - } - if (admin_cmd_query_stats(sock, cfg, genl_id) < 0) { - (void)printf("Failed to query stats by ioctl.\n"); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - - nl_close(sock); - nl_socket_free(sock); - return 0; -} - -static const char *g_query_res_type[] = { - [0] = NULL, - [TOOL_RES_KEY_VTP] = "RES_VTP", - [TOOL_RES_KEY_TP] = "RES_TP", - [TOOL_RES_KEY_TPG] = "RES_TPG", - [TOOL_RES_KEY_UTP] = "RES_UTP", - [TOOL_RES_KEY_JFS] = "RES_JFS", - [TOOL_RES_KEY_JFR] = "RES_JFR", - [TOOL_RES_KEY_JETTY] = "RES_JETTY", - [TOOL_RES_KEY_JETTY_GROUP] = "RES_JETTY_GRP", - [TOOL_RES_KEY_JFC] = "RES_JFC", - [TOOL_RES_KEY_RC] = "RES_RC", - [TOOL_RES_KEY_SEG] = "RES_SEG", - [TOOL_RES_KEY_DEV_TA] = "RES_DEV_TA", - [TOOL_RES_KEY_DEV_TP] = "RES_DEV_TP", -}; - -static void admin_print_res_jfs(struct nlattr *head) -{ - int type = nla_type(head); - if (type == UBCORE_RES_JFS_VAL) { - tool_res_jfs_val_t *val = (tool_res_jfs_val_t *)nla_data(head); - (void)printf("jfs_id : %u\n", val->jfs_id); - (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jetty_state_to_string(val->state)); - (void)printf("depth : %u\n", val->depth); - (void)printf("pri : %u\n", (uint32_t)val->pri); - (void)printf("jfc_id : %u\n", val->jfc_id); - } -} - -static void admin_print_res_jfr(struct nlattr *head) -{ - int type = nla_type(head); - if (type == UBCORE_RES_JFR_VAL) { - tool_res_jfr_val_t *val = (tool_res_jfr_val_t *)nla_data(head); - (void)printf("jfr_id : %u\n", val->jfr_id); - (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jfr_state_to_string(val->state)); - (void)printf("depth : %u\n", val->depth); - (void)printf("jfc_id : %u\n", val->jfc_id); - } -} - -static void admin_print_res_jetty(struct nlattr *head) -{ - int type = nla_type(head); - if (type == UBCORE_RES_JETTY_VAL) { - tool_res_jetty_val_t *val = (tool_res_jetty_val_t *)nla_data(head); - (void)printf("jetty_id : %u\n", val->jetty_id); - (void)printf("send_jfc_id : %u\n", val->send_jfc_id); - (void)printf("recv_jfc_id : %u\n", val->recv_jfc_id); - (void)printf("jfr_id : %u\n", val->jfr_id); - (void)printf("jfs_depth : %u\n", val->jfs_depth); - (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jetty_state_to_string(val->state)); - (void)printf("pri : %u\n", (uint32_t)val->pri); - } -} - -static void admin_print_res_jetty_grp(struct nlattr *head, int len) -{ - struct nlattr *nla; - int rem; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_JTGRP_JETTY_CNT) { - (void)printf("jetty_cnt : %u\n", nla_get_u32(nla)); - (void)printf("jetty : "); - } - - if (type == UBCORE_RES_JTGRP_JETTY_VAL) { - (void)printf("%u ", nla_get_u32(nla)); - } - } - (void)printf("\n"); -} - -static void admin_print_res_jfc(struct nlattr *head) -{ - int type = nla_type(head); - if (type == UBCORE_RES_JFC_VAL) { - tool_res_jfc_val_t *val = (tool_res_jfc_val_t *)nla_data(head); - (void)printf("jfc_id : %u\n", val->jfc_id); - (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jfc_state_to_string(val->state)); - (void)printf("depth : %u\n", val->depth); - } -} - -static void admin_print_res_rc(struct nlattr *head) -{ - int type = nla_type(head); - if (type == UBCORE_RES_RC_VAL) { - tool_res_rc_val_t *val = (tool_res_rc_val_t *)nla_data(head); - (void)printf("type : %u\n", val->type); - (void)printf("rc_id : %u\n", val->rc_id); - (void)printf("depth : %hu\n", val->depth); - (void)printf("state : %u\n", (uint32_t)val->state); - } -} - -static void admin_print_res_seg(struct nlattr *head, int len) -{ - struct nlattr *nla; - int rem; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_SEGVAL_SEG_CNT) { - (void)printf("seg_cnt : %u\n", nla_get_u32(nla)); - (void)printf("seg : \n"); - } - - if (type == UBCORE_RES_SEGVAL_SEG_VAL) { - tool_seg_info_t *val = (tool_seg_info_t *)nla_data(nla); - (void)printf("seg_list idx: %u\n", i); - (void)printf("eid :" EID_FMT " \n", EID_ARGS(val->ubva.eid)); - (void)printf("va : %lu\n", val->ubva.va); - (void)printf("len : %lu\n", val->len); - (void)printf("token_id : %u\n", val->token_id); - (void)printf("\n"); - i++; - } - } - (void)printf("\n"); -} - -static void admin_print_res_dev(struct nlattr *head, int len) -{ - int rem; - struct nlattr *nla; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - switch (type) { - case UBCORE_RES_DEV_SEG_CNT: { - (void)printf("----------SEG----------\n"); - (void)printf("seg_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_JFS_CNT: { - (void)printf("\n----------JFS----------\n"); - (void)printf("jfs_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_JFR_CNT: { - (void)printf("\n----------JFR----------\n"); - (void)printf("jfr_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_JFC_CNT: { - (void)printf("\n----------JFC----------\n"); - (void)printf("jfc_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_JETTY_CNT: { - (void)printf("\n---------JETTY---------\n"); - (void)printf("jetty_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_JTGRP_CNT: { - (void)printf("\n------JETTY_GROUP------\n"); - (void)printf("jetty_group_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_RC_CNT: { - (void)printf("\n----------RC-----------\n"); - (void)printf("rc_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_VTP_CNT: { - (void)printf("\n----------VTP----------\n"); - (void)printf("vtp_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_TP_CNT: { - (void)printf("\n----------TP-----------\n"); - (void)printf("tp_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_TPG_CNT: { - (void)printf("\n----------TPG----------\n"); - (void)printf("tpg_cnt :%u \n", nla_get_u32(nla)); - break; - } - case UBCORE_RES_DEV_UTP_CNT: { - (void)printf("\n----------UTP----------\n"); - (void)printf("utp_cnt :%u \n", nla_get_u32(nla)); - break; - } - default: - break; - } - } - (void)printf("\n"); -} - -static void print_query_res(struct nlattr *attr_ptr, netlink_cb_par *cb_par, int len) -{ - (void)printf("**********%s**********\n", g_query_res_type[cb_par->type]); - switch (cb_par->type) { - case TOOL_RES_KEY_JETTY_GROUP: - admin_print_res_jetty_grp(attr_ptr, len); - break; - case TOOL_RES_KEY_SEG: - admin_print_res_seg(attr_ptr, len); - break; - case TOOL_RES_KEY_DEV_TA: - admin_print_res_dev(attr_ptr, len); - break; - case TOOL_RES_KEY_JFS: - admin_print_res_jfs(attr_ptr); - break; - case TOOL_RES_KEY_JFR: - admin_print_res_jfr(attr_ptr); - break; - case TOOL_RES_KEY_JETTY: - admin_print_res_jetty(attr_ptr); - break; - case TOOL_RES_KEY_JFC: - admin_print_res_jfc(attr_ptr); - break; - case TOOL_RES_KEY_RC: - admin_print_res_rc(attr_ptr); - break; - default: - break; - } -} - -static int cb_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 len = genlmsg_attrlen(genlhdr, 0); - - netlink_cb_par *cb_par = (netlink_cb_par *)arg; - print_query_res(attr_ptr, cb_par, len); - - return 0; -} - -static void admin_list_res_jfs(struct nlattr *head, int len) -{ - int rem; - struct nlattr *nla; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_DEV_JFS_CNT) { - (void)printf("\n----------JFS----------\n"); - (void)printf("jfs_cnt :%u \n", nla_get_u32(nla)); - } - if (type == UBCORE_RES_DEV_JFS_VAL) { - (void)printf("jfs_id[%u] \t:%u\n", i, nla_get_u32(nla)); - i++; - } - } -} - -static void admin_list_res_jfr(struct nlattr *head, int len) -{ - int rem; - struct nlattr *nla; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_DEV_JFR_CNT) { - (void)printf("\n----------JFR----------\n"); - (void)printf("jfr_cnt :%u \n", nla_get_u32(nla)); - } - if (type == UBCORE_RES_DEV_JFR_VAL) { - (void)printf("jfr_id[%u] \t:%u\n", i, nla_get_u32(nla)); - i++; - } - } -} - -static void admin_list_res_jetty(struct nlattr *head, int len) -{ - int rem; - struct nlattr *nla; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_DEV_JETTY_CNT) { - (void)printf("\n---------JETTY---------\n"); - (void)printf("jetty_cnt :%u \n", nla_get_u32(nla)); - i = 0; - } - if (type == UBCORE_RES_DEV_JETTY_VAL) { - (void)printf("jetty_id[%u] \t:%u\n", i, nla_get_u32(nla)); - i++; - } - } -} - -static void admin_list_res_jetty_grp(struct nlattr *head, int len) -{ - struct nlattr *nla; - int rem; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_JTGRP_JETTY_CNT) { - (void)printf("\n------JETTY_GROUP------\n"); - (void)printf("jetty_group_cnt :%u \n", nla_get_u32(nla)); - } - - if (type == UBCORE_RES_JTGRP_JETTY_VAL) { - (void)printf("jetty_group_id[%u] \t:%u\n", i, nla_get_u32(nla)); - i++; - } - } -} - -static void admin_list_res_jfc(struct nlattr *head, int len) -{ - int rem; - struct nlattr *nla; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_DEV_JFC_CNT) { - (void)printf("\n----------JFC----------\n"); - (void)printf("jfc_cnt :%u \n", nla_get_u32(nla)); - } - if (type == UBCORE_RES_DEV_JFC_VAL) { - (void)printf("jfc_id[%u] \t:%u\n", i, nla_get_u32(nla)); - i++; - } - } -} - -static void admin_list_res_rc(struct nlattr *head, int len) -{ - int rem; - struct nlattr *nla; - uint32_t i = 0; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_DEV_RC_CNT) { - (void)printf("\n----------RC-----------\n"); - (void)printf("rc_cnt :%u \n", nla_get_u32(nla)); - } - if (type == UBCORE_RES_DEV_RC_VAL) { - (void)printf("rc_id[%u] \t:%u\n", i, nla_get_u32(nla)); - i++; - } - } -} - -static void admin_list_res_seg(struct nlattr *head, int len) -{ - int rem; - uint32_t i = 0; - struct nlattr *nla; - - nla_for_each_attr(nla, head, len, rem) - { - int type = nla_type(nla); - if (type == UBCORE_RES_SEGVAL_SEG_CNT) { - (void)printf("seg_cnt : %u\n", nla_get_u32(nla)); - } - - if (type == UBCORE_RES_SEGVAL_SEG_VAL) { - tool_seg_info_t *val = (tool_seg_info_t *)nla_data(nla); - (void)printf("seg_list idx: %u\n", i); - (void)printf("eid :" EID_FMT " \n", EID_ARGS(val->ubva.eid)); - (void)printf("va : %lu\n", val->ubva.va); - (void)printf("len : %lu\n", val->len); - (void)printf("token_id : %u\n", val->token_id); - (void)printf("\n"); - i++; - } - } -} - -static void print_list_res(struct nlattr *attr_ptr, netlink_cb_par *cb_par, int len) -{ - (void)printf("**********%s**********\n", g_query_res_type[cb_par->type]); - switch (cb_par->type) { - case TOOL_RES_KEY_JETTY_GROUP: - admin_list_res_jetty_grp(attr_ptr, len); - break; - case TOOL_RES_KEY_SEG: - admin_list_res_seg(attr_ptr, len); - break; - case TOOL_RES_KEY_JFS: - admin_list_res_jfs(attr_ptr, len); - break; - case TOOL_RES_KEY_JFR: - admin_list_res_jfr(attr_ptr, len); - break; - case TOOL_RES_KEY_JETTY: - admin_list_res_jetty(attr_ptr, len); - break; - case TOOL_RES_KEY_JFC: - admin_list_res_jfc(attr_ptr, len); - break; - case TOOL_RES_KEY_RC: - admin_list_res_rc(attr_ptr, len); - break; - default: - break; - } -} - -static int cb_handler_list(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 len = genlmsg_attrlen(genlhdr, 0); - - netlink_cb_par *cb_par = (netlink_cb_par *)arg; - print_list_res(attr_ptr, cb_par, len); - - return 0; -} - -static int admin_cmd_query_res(struct nl_sock *sock, const tool_config_t *cfg, int genl_id, netlink_cb_par *cb_arg) -{ - admin_cmd_query_res_t *arg; - urma_cmd_hdr_t hdr; - arg = calloc(1, sizeof(admin_cmd_query_res_t)); - if (arg == NULL) { - return -1; - } - - arg->in.key = cfg->key.key; - arg->in.type = cfg->key.type; - arg->in.key_ext = cfg->key.key_ext; - if (arg->in.type == TOOL_RES_KEY_DEV_TA && cfg->key.key_cnt == 0) { - arg->in.key_cnt = 1; - } else { - arg->in.key_cnt = cfg->key.key_cnt; - } - (void)memcpy(arg->in.dev_name, cfg->dev_name, strlen(cfg->dev_name)); - cb_arg->type = arg->in.type; - cb_arg->key = arg->in.key; - - hdr.command = (uint32_t)URMA_CORE_CMD_QUERY_RES; - hdr.args_len = (uint32_t)sizeof(admin_cmd_query_res_t); - hdr.args_addr = (uint64_t)arg; - - int ret = cmd_nlsend(sock, genl_id, &hdr); - if (ret < 0) { - (void)printf("Failed to cmd_nlsend, ret: %d, command: %u, errno: %d.\n", ret, hdr.command, errno); - free(arg); - 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); - } - free(arg); - return ret; -} - -int admin_show_res(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - netlink_cb_par nl_cb_agr; - - if (cfg->key.type < TOOL_RES_KEY_VTP || cfg->key.type > TOOL_RES_KEY_DEV_TA) { - (void)printf("Invalid type: %d.\n", (int)cfg->key.type); - return -1; - } - if ((cfg->key.type >= TOOL_RES_KEY_VTP && cfg->key.type <= TOOL_RES_KEY_UTP) || - cfg->key.type == TOOL_RES_KEY_DEV_TP) { - (void)printf("urma_admin do not support query tp stats.\n"); - return -1; - } - if (cfg->key.key_cnt == 0 && cfg->key.type != TOOL_RES_KEY_DEV_TA) { - (void)printf("key_cnt in show_res cannot be 0 when type is not dev.\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_handler, &nl_cb_agr); - if (admin_cmd_query_res(sock, cfg, genl_id, &nl_cb_agr) < 0) { - (void)printf("Failed to query stats by ioctl.\n"); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - - nl_close(sock); - nl_socket_free(sock); - return 0; -} - -static int admin_cmd_list_res(struct nl_sock *sock, const tool_config_t *cfg, int genl_id, netlink_cb_par *cb_arg) -{ - admin_cmd_query_res_t *arg; - urma_cmd_hdr_t hdr; - arg = calloc(1, sizeof(admin_cmd_query_res_t)); - if (arg == NULL) { - return -1; - } - - arg->in.key = cfg->key.key; - arg->in.type = cfg->key.type; - arg->in.key_ext = cfg->key.key_ext; - arg->in.key_cnt = cfg->key.key_cnt; - (void)memcpy(arg->in.dev_name, cfg->dev_name, strlen(cfg->dev_name)); - cb_arg->type = arg->in.type; - cb_arg->key = arg->in.key; - - hdr.command = (uint32_t)URMA_CORE_CMD_QUERY_RES; - hdr.args_len = (uint32_t)sizeof(admin_cmd_query_res_t); - hdr.args_addr = (uint64_t)arg; - - int ret = cmd_nlsend(sock, genl_id, &hdr); - if (ret < 0) { - (void)printf("Failed to cmd_nlsend, ret: %d, command: %u, errno: %d.\n", ret, hdr.command, errno); - free(arg); - 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); - } - free(arg); - return ret; -} - -int admin_list_res(tool_config_t *cfg) -{ - struct nl_sock *sock = NULL; - int genl_id; - netlink_cb_par nl_cb_agr; - - if ((cfg->key.type >= TOOL_RES_KEY_VTP && cfg->key.type <= TOOL_RES_KEY_UTP) || - cfg->key.type >= TOOL_RES_KEY_DEV_TA) { - (void)printf("urma_admin do not support query tp and dev stats.\n"); - return -1; - } - if (cfg->key.key_cnt != 0) { - (void)printf("key_cnt in list_res should equal 0.\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_handler_list, &nl_cb_agr); - if (admin_cmd_list_res(sock, cfg, genl_id, &nl_cb_agr) < 0) { - (void)printf("Failed to query stats by ioctl.\n"); - nl_close(sock); - nl_socket_free(sock); - return -1; - } - - nl_close(sock); - nl_socket_free(sock); - return 0; -} - -static int admin_set_reserved_jetty_id_range(tool_config_t *cfg) +static int admin_set_reserved_jetty_id_range(admin_config_t *cfg) { char jetty_id_range[VALUE_LEN_MAX] = {0}; @@ -793,12 +62,12 @@ int admin_cmd_main(admin_config_t *cfg) {"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}, + {"show_stats", admin_cmd_show_stats_legacy}, + {"show_res", admin_cmd_show_res_legacy}, + {"list_res", admin_cmd_list_res_legacy}, {"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}, // {"show", admin_cmd_show}, {"agg", admin_cmd_agg}, diff --git a/src/urma/tools/urma_admin/admin_cmd.h b/src/urma/tools/urma_admin/admin_cmd.h index 26b953e..c1ed8b0 100644 --- a/src/urma/tools/urma_admin/admin_cmd.h +++ b/src/urma/tools/urma_admin/admin_cmd.h @@ -166,14 +166,14 @@ struct cmd_agg_del_arg { // Legacy command 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_cmd_show_stats_legacy(admin_config_t *cfg); +int admin_cmd_show_res_legacy(admin_config_t *cfg); +int admin_cmd_list_res_legacy(admin_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); // New command int admin_cmd_main(admin_config_t *cfg); diff --git a/src/urma/tools/urma_admin/admin_cmd_res.c b/src/urma/tools/urma_admin/admin_cmd_res.c new file mode 100644 index 0000000..8fc96d2 --- /dev/null +++ b/src/urma/tools/urma_admin/admin_cmd_res.c @@ -0,0 +1,571 @@ +/* + * SPDX-License-Identifier: MIT + * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved. + * Description: eid sub-command source file for urma_admin + * Author: Wang Hang + * Create: 2025-12-31 + * Note: + * History: 2025-12-31 create file + */ + +#include +#include + +#include "admin_netlink.h" + +#include "admin_cmd.h" + +static inline void admin_print_stats(const admin_cmd_query_stats_t *arg) +{ + (void)printf("tx_pkt : %lu\n", arg->out.tx_pkt); + (void)printf("rx_pkt : %lu\n", arg->out.rx_pkt); + (void)printf("tx_bytes : %lu\n", arg->out.tx_bytes); + (void)printf("rx_bytes : %lu\n", arg->out.rx_bytes); + (void)printf("tx_pkt_err : %lu\n", arg->out.tx_pkt_err); + (void)printf("rx_pkt_err : %lu\n", arg->out.rx_pkt_err); +} + +int admin_cmd_show_stats_legacy(admin_config_t *cfg) +{ + if (cfg->key.type < TOOL_STATS_KEY_VTP || cfg->key.type > TOOL_STATS_KEY_URMA_DEV) { + (void)printf("Invalid type: %d.\n", (int)cfg->key.type); + return -1; + } + if (cfg->key.type == TOOL_STATS_KEY_TPG || cfg->key.type == TOOL_STATS_KEY_JETTY_GROUP) { + (void)printf("Type: %d currently not supported.\n", (int)cfg->key.type); + return -1; + } + if (cfg->key.type >= TOOL_STATS_KEY_VTP && cfg->key.type <= TOOL_STATS_KEY_TPG) { + (void)printf("urma_admin do not support query tp stats.\n"); + return -1; + } + + admin_cmd_query_stats_t arg = {0}; + (void)memcpy(arg.in.dev_name, cfg->dev_name, strlen(cfg->dev_name)); + arg.in.key = cfg->key.key; + arg.in.type = (uint32_t)cfg->key.type; + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_CMD_QUERY_STATS, 0); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_u32(msg, UBCORE_HDR_ARGS_LEN, (uint32_t)sizeof(admin_cmd_query_stats_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); + + if (ret == 0) { + admin_print_stats(&arg); + } + return ret; +} + +static const char *g_query_res_type[] = { + [0] = NULL, + [TOOL_RES_KEY_VTP] = "RES_VTP", + [TOOL_RES_KEY_TP] = "RES_TP", + [TOOL_RES_KEY_TPG] = "RES_TPG", + [TOOL_RES_KEY_UTP] = "RES_UTP", + [TOOL_RES_KEY_JFS] = "RES_JFS", + [TOOL_RES_KEY_JFR] = "RES_JFR", + [TOOL_RES_KEY_JETTY] = "RES_JETTY", + [TOOL_RES_KEY_JETTY_GROUP] = "RES_JETTY_GRP", + [TOOL_RES_KEY_JFC] = "RES_JFC", + [TOOL_RES_KEY_RC] = "RES_RC", + [TOOL_RES_KEY_SEG] = "RES_SEG", + [TOOL_RES_KEY_DEV_TA] = "RES_DEV_TA", + [TOOL_RES_KEY_DEV_TP] = "RES_DEV_TP", +}; + +static void admin_print_res_jfs(struct nlattr *head) +{ + int type = nla_type(head); + if (type == UBCORE_RES_JFS_VAL) { + tool_res_jfs_val_t *val = (tool_res_jfs_val_t *)nla_data(head); + (void)printf("jfs_id : %u\n", val->jfs_id); + (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jetty_state_to_string(val->state)); + (void)printf("depth : %u\n", val->depth); + (void)printf("pri : %u\n", (uint32_t)val->pri); + (void)printf("jfc_id : %u\n", val->jfc_id); + } +} + +static void admin_print_res_jfr(struct nlattr *head) +{ + int type = nla_type(head); + if (type == UBCORE_RES_JFR_VAL) { + tool_res_jfr_val_t *val = (tool_res_jfr_val_t *)nla_data(head); + (void)printf("jfr_id : %u\n", val->jfr_id); + (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jfr_state_to_string(val->state)); + (void)printf("depth : %u\n", val->depth); + (void)printf("jfc_id : %u\n", val->jfc_id); + } +} + +static void admin_print_res_jetty(struct nlattr *head) +{ + int type = nla_type(head); + if (type == UBCORE_RES_JETTY_VAL) { + tool_res_jetty_val_t *val = (tool_res_jetty_val_t *)nla_data(head); + (void)printf("jetty_id : %u\n", val->jetty_id); + (void)printf("send_jfc_id : %u\n", val->send_jfc_id); + (void)printf("recv_jfc_id : %u\n", val->recv_jfc_id); + (void)printf("jfr_id : %u\n", val->jfr_id); + (void)printf("jfs_depth : %u\n", val->jfs_depth); + (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jetty_state_to_string(val->state)); + (void)printf("pri : %u\n", (uint32_t)val->pri); + } +} + +static void admin_print_res_jetty_grp(struct nlattr *head, int len) +{ + struct nlattr *nla; + int rem; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_JTGRP_JETTY_CNT) { + (void)printf("jetty_cnt : %u\n", nla_get_u32(nla)); + (void)printf("jetty : "); + } + + if (type == UBCORE_RES_JTGRP_JETTY_VAL) { + (void)printf("%u ", nla_get_u32(nla)); + } + } + (void)printf("\n"); +} + +static void admin_print_res_jfc(struct nlattr *head) +{ + int type = nla_type(head); + if (type == UBCORE_RES_JFC_VAL) { + tool_res_jfc_val_t *val = (tool_res_jfc_val_t *)nla_data(head); + (void)printf("jfc_id : %u\n", val->jfc_id); + (void)printf("state : %u [%s]\n", (uint32_t)val->state, urma_jfc_state_to_string(val->state)); + (void)printf("depth : %u\n", val->depth); + } +} + +static void admin_print_res_rc(struct nlattr *head) +{ + int type = nla_type(head); + if (type == UBCORE_RES_RC_VAL) { + tool_res_rc_val_t *val = (tool_res_rc_val_t *)nla_data(head); + (void)printf("type : %u\n", val->type); + (void)printf("rc_id : %u\n", val->rc_id); + (void)printf("depth : %hu\n", val->depth); + (void)printf("state : %u\n", (uint32_t)val->state); + } +} + +static void admin_print_res_seg(struct nlattr *head, int len) +{ + struct nlattr *nla; + int rem; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_SEGVAL_SEG_CNT) { + (void)printf("seg_cnt : %u\n", nla_get_u32(nla)); + (void)printf("seg : \n"); + } + + if (type == UBCORE_RES_SEGVAL_SEG_VAL) { + tool_seg_info_t *val = (tool_seg_info_t *)nla_data(nla); + (void)printf("seg_list idx: %u\n", i); + (void)printf("eid :" EID_FMT " \n", EID_ARGS(val->ubva.eid)); + (void)printf("va : %lu\n", val->ubva.va); + (void)printf("len : %lu\n", val->len); + (void)printf("token_id : %u\n", val->token_id); + (void)printf("\n"); + i++; + } + } + (void)printf("\n"); +} + +static void admin_print_res_dev(struct nlattr *head, int len) +{ + int rem; + struct nlattr *nla; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + switch (type) { + case UBCORE_RES_DEV_SEG_CNT: { + (void)printf("----------SEG----------\n"); + (void)printf("seg_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_JFS_CNT: { + (void)printf("\n----------JFS----------\n"); + (void)printf("jfs_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_JFR_CNT: { + (void)printf("\n----------JFR----------\n"); + (void)printf("jfr_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_JFC_CNT: { + (void)printf("\n----------JFC----------\n"); + (void)printf("jfc_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_JETTY_CNT: { + (void)printf("\n---------JETTY---------\n"); + (void)printf("jetty_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_JTGRP_CNT: { + (void)printf("\n------JETTY_GROUP------\n"); + (void)printf("jetty_group_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_RC_CNT: { + (void)printf("\n----------RC-----------\n"); + (void)printf("rc_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_VTP_CNT: { + (void)printf("\n----------VTP----------\n"); + (void)printf("vtp_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_TP_CNT: { + (void)printf("\n----------TP-----------\n"); + (void)printf("tp_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_TPG_CNT: { + (void)printf("\n----------TPG----------\n"); + (void)printf("tpg_cnt :%u \n", nla_get_u32(nla)); + break; + } + case UBCORE_RES_DEV_UTP_CNT: { + (void)printf("\n----------UTP----------\n"); + (void)printf("utp_cnt :%u \n", nla_get_u32(nla)); + break; + } + default: + break; + } + } + (void)printf("\n"); +} + +static void print_query_res(struct nlattr *attr_ptr, admin_config_t *cfg, int len) +{ + (void)printf("**********%s**********\n", g_query_res_type[cfg->key.type]); + switch (cfg->key.type) { + case TOOL_RES_KEY_JETTY_GROUP: + admin_print_res_jetty_grp(attr_ptr, len); + break; + case TOOL_RES_KEY_SEG: + admin_print_res_seg(attr_ptr, len); + break; + case TOOL_RES_KEY_DEV_TA: + admin_print_res_dev(attr_ptr, len); + break; + case TOOL_RES_KEY_JFS: + admin_print_res_jfs(attr_ptr); + break; + case TOOL_RES_KEY_JFR: + admin_print_res_jfr(attr_ptr); + break; + case TOOL_RES_KEY_JETTY: + admin_print_res_jetty(attr_ptr); + break; + case TOOL_RES_KEY_JFC: + admin_print_res_jfc(attr_ptr); + break; + case TOOL_RES_KEY_RC: + admin_print_res_rc(attr_ptr); + break; + default: + break; + } +} + +static int cb_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 len = genlmsg_attrlen(genlhdr, 0); + + admin_config_t *cfg = (admin_config_t *)arg; + print_query_res(attr_ptr, cfg, len); + + return 0; +} + +static void admin_list_res_jfs(struct nlattr *head, int len) +{ + int rem; + struct nlattr *nla; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_DEV_JFS_CNT) { + (void)printf("\n----------JFS----------\n"); + (void)printf("jfs_cnt :%u \n", nla_get_u32(nla)); + } + if (type == UBCORE_RES_DEV_JFS_VAL) { + (void)printf("jfs_id[%u] \t:%u\n", i, nla_get_u32(nla)); + i++; + } + } +} + +static void admin_list_res_jfr(struct nlattr *head, int len) +{ + int rem; + struct nlattr *nla; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_DEV_JFR_CNT) { + (void)printf("\n----------JFR----------\n"); + (void)printf("jfr_cnt :%u \n", nla_get_u32(nla)); + } + if (type == UBCORE_RES_DEV_JFR_VAL) { + (void)printf("jfr_id[%u] \t:%u\n", i, nla_get_u32(nla)); + i++; + } + } +} + +static void admin_list_res_jetty(struct nlattr *head, int len) +{ + int rem; + struct nlattr *nla; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_DEV_JETTY_CNT) { + (void)printf("\n---------JETTY---------\n"); + (void)printf("jetty_cnt :%u \n", nla_get_u32(nla)); + i = 0; + } + if (type == UBCORE_RES_DEV_JETTY_VAL) { + (void)printf("jetty_id[%u] \t:%u\n", i, nla_get_u32(nla)); + i++; + } + } +} + +static void admin_list_res_jetty_grp(struct nlattr *head, int len) +{ + struct nlattr *nla; + int rem; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_JTGRP_JETTY_CNT) { + (void)printf("\n------JETTY_GROUP------\n"); + (void)printf("jetty_group_cnt :%u \n", nla_get_u32(nla)); + } + + if (type == UBCORE_RES_JTGRP_JETTY_VAL) { + (void)printf("jetty_group_id[%u] \t:%u\n", i, nla_get_u32(nla)); + i++; + } + } +} + +static void admin_list_res_jfc(struct nlattr *head, int len) +{ + int rem; + struct nlattr *nla; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_DEV_JFC_CNT) { + (void)printf("\n----------JFC----------\n"); + (void)printf("jfc_cnt :%u \n", nla_get_u32(nla)); + } + if (type == UBCORE_RES_DEV_JFC_VAL) { + (void)printf("jfc_id[%u] \t:%u\n", i, nla_get_u32(nla)); + i++; + } + } +} + +static void admin_list_res_rc(struct nlattr *head, int len) +{ + int rem; + struct nlattr *nla; + uint32_t i = 0; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_DEV_RC_CNT) { + (void)printf("\n----------RC-----------\n"); + (void)printf("rc_cnt :%u \n", nla_get_u32(nla)); + } + if (type == UBCORE_RES_DEV_RC_VAL) { + (void)printf("rc_id[%u] \t:%u\n", i, nla_get_u32(nla)); + i++; + } + } +} + +static void admin_list_res_seg(struct nlattr *head, int len) +{ + int rem; + uint32_t i = 0; + struct nlattr *nla; + + nla_for_each_attr(nla, head, len, rem) + { + int type = nla_type(nla); + if (type == UBCORE_RES_SEGVAL_SEG_CNT) { + (void)printf("seg_cnt : %u\n", nla_get_u32(nla)); + } + + if (type == UBCORE_RES_SEGVAL_SEG_VAL) { + tool_seg_info_t *val = (tool_seg_info_t *)nla_data(nla); + (void)printf("seg_list idx: %u\n", i); + (void)printf("eid :" EID_FMT " \n", EID_ARGS(val->ubva.eid)); + (void)printf("va : %lu\n", val->ubva.va); + (void)printf("len : %lu\n", val->len); + (void)printf("token_id : %u\n", val->token_id); + (void)printf("\n"); + i++; + } + } +} + +static void print_list_res(struct nlattr *attr_ptr, admin_config_t *cfg, int len) +{ + (void)printf("**********%s**********\n", g_query_res_type[cfg->key.type]); + switch (cfg->key.type) { + case TOOL_RES_KEY_JETTY_GROUP: + admin_list_res_jetty_grp(attr_ptr, len); + break; + case TOOL_RES_KEY_SEG: + admin_list_res_seg(attr_ptr, len); + break; + case TOOL_RES_KEY_JFS: + admin_list_res_jfs(attr_ptr, len); + break; + case TOOL_RES_KEY_JFR: + admin_list_res_jfr(attr_ptr, len); + break; + case TOOL_RES_KEY_JETTY: + admin_list_res_jetty(attr_ptr, len); + break; + case TOOL_RES_KEY_JFC: + admin_list_res_jfc(attr_ptr, len); + break; + case TOOL_RES_KEY_RC: + admin_list_res_rc(attr_ptr, len); + break; + default: + break; + } +} + +static int cb_handler_list(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 len = genlmsg_attrlen(genlhdr, 0); + + admin_config_t *cfg = (admin_config_t *)arg; + print_list_res(attr_ptr, cfg, len); + + return 0; +} + +int admin_cmd_show_res_legacy(admin_config_t *cfg) +{ + if (cfg->key.type < TOOL_RES_KEY_VTP || cfg->key.type > TOOL_RES_KEY_DEV_TA) { + (void)printf("Invalid type: %d.\n", (int)cfg->key.type); + return -1; + } + if ((cfg->key.type >= TOOL_RES_KEY_VTP && cfg->key.type <= TOOL_RES_KEY_UTP) || + cfg->key.type == TOOL_RES_KEY_DEV_TP) { + (void)printf("urma_admin do not support query tp stats.\n"); + return -1; + } + if (cfg->key.key_cnt == 0 && cfg->key.type != TOOL_RES_KEY_DEV_TA) { + (void)printf("key_cnt in show_res cannot be 0 when type is not dev.\n"); + return -1; + } + + admin_cmd_query_res_t arg = {0}; + arg.in.key = cfg->key.key; + arg.in.type = cfg->key.type; + arg.in.key_ext = cfg->key.key_ext; + if (arg.in.type == TOOL_RES_KEY_DEV_TA && cfg->key.key_cnt == 0) { + arg.in.key_cnt = 1; + } else { + arg.in.key_cnt = cfg->key.key_cnt; + } + (void)memcpy(arg.in.dev_name, cfg->dev_name, strlen(cfg->dev_name)); + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_CMD_QUERY_RES, NLM_F_DUMP); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_u32(msg, UBCORE_HDR_ARGS_LEN, (uint32_t)sizeof(admin_cmd_query_res_t)); + admin_nl_put_u64(msg, UBCORE_HDR_ARGS_ADDR, (uint64_t)(uintptr_t)&arg); + + int ret = admin_nl_send_recv_msg(msg, cb_handler, cfg); + admin_nl_free_msg(msg); + return ret; +} + +int admin_cmd_list_res_legacy(admin_config_t *cfg) +{ + if ((cfg->key.type >= TOOL_RES_KEY_VTP && cfg->key.type <= TOOL_RES_KEY_UTP) || + cfg->key.type >= TOOL_RES_KEY_DEV_TA) { + (void)printf("urma_admin do not support query tp and dev stats.\n"); + return -1; + } + if (cfg->key.key_cnt != 0) { + (void)printf("key_cnt in list_res should equal 0.\n"); + return -1; + } + + admin_cmd_query_res_t arg = {0}; + arg.in.key = cfg->key.key; + arg.in.type = cfg->key.type; + arg.in.key_ext = cfg->key.key_ext; + arg.in.key_cnt = cfg->key.key_cnt; + (void)memcpy(arg.in.dev_name, cfg->dev_name, strlen(cfg->dev_name)); + + struct nl_msg *msg = admin_nl_alloc_msg(URMA_CORE_CMD_QUERY_RES, NLM_F_DUMP); + if (msg == NULL) { + return -ENOMEM; + } + + admin_nl_put_u32(msg, UBCORE_HDR_ARGS_LEN, (uint32_t)sizeof(admin_cmd_query_res_t)); + admin_nl_put_u64(msg, UBCORE_HDR_ARGS_ADDR, (uint64_t)(uintptr_t)&arg); + + int ret = admin_nl_send_recv_msg(msg, cb_handler_list, cfg); + admin_nl_free_msg(msg); + return ret; +} -- Gitee