diff --git a/src/urma/tools/urma_admin/admin.c b/src/urma/tools/urma_admin/admin.c index 33594243bf7fcca9e6fe264d94a85a0cbb012b59..871014b4b32af5c968bb755aa4323c945e2a23c0 100644 --- a/src/urma/tools/urma_admin/admin.c +++ b/src/urma/tools/urma_admin/admin.c @@ -27,8 +27,8 @@ #include "urma_types_str.h" #include "admin_cmd.h" -#include "admin_log.h" #include "admin_file_ops.h" +#include "admin_log.h" #include "admin_parameters.h" #define UINT8_INVALID (0xff) @@ -475,7 +475,7 @@ static void free_ubep_list(struct ub_list *ubep_list) } } -static int admin_show_ubep(const tool_config_t *cfg) +static int admin_show_ubep(tool_config_t *cfg) { int ret; struct ub_list ubep_list; @@ -497,7 +497,7 @@ free_list: return ret; } -static int admin_set_reserved_jetty_id_range(const tool_config_t *cfg) +static int admin_set_reserved_jetty_id_range(tool_config_t *cfg) { char jetty_id_range[VALUE_LEN_MAX] = {0}; @@ -515,58 +515,24 @@ static int admin_set_reserved_jetty_id_range(const tool_config_t *cfg) return admin_write_dev_file(cfg->dev_name, "reserved_jetty_id", jetty_id_range, len + 1); } -static int execute_command(const tool_config_t *cfg) +static int admin_cmd_main(admin_config_t *cfg) { - if (cfg->cmd == TOOL_CMD_ADD_EID || cfg->cmd == TOOL_CMD_DEL_EID || cfg->cmd == TOOL_CMD_SET_EID_MODE || - cfg->cmd == TOOL_CMD_SET_NS_MODE || cfg->cmd == TOOL_CMD_SET_DEV_NS || - cfg->cmd == TOOL_CMD_SET_RESERVED_JETTY) { - (void)printf("This command not support in control plane.\n"); - return -1; - } - - int ret; - - switch (cfg->cmd) { - case TOOL_CMD_SHOW: - ret = admin_show_ubep(cfg); - break; - case TOOL_CMD_ADD_EID: - ret = admin_add_eid(cfg); - break; - case TOOL_CMD_DEL_EID: - ret = admin_del_eid(cfg); - break; - case TOOL_CMD_SET_EID_MODE: - ret = admin_set_eid_mode(cfg); - break; - case TOOL_CMD_SHOW_STATS: - ret = admin_show_stats(cfg); - break; - case TOOL_CMD_SHOW_RES: - ret = admin_show_res(cfg); - break; - case TOOL_CMD_SET_NS_MODE: - ret = admin_set_ns_mode(cfg); - break; - case TOOL_CMD_SET_DEV_NS: - ret = admin_set_dev_ns(cfg); - break; - case TOOL_CMD_LIST_RES: - ret = admin_list_res(cfg); - break; - case TOOL_CMD_SET_RESERVED_JETTY: - ret = admin_set_reserved_jetty_id_range(cfg); - break; - case TOOL_CMD_SHOW_TOPO_INFO: - ret = admin_show_topo_info(cfg); - break; - case TOOL_CMD_NUM: - default: - ret = -1; - break; - } - - return ret; + static const admin_cmd_t cmds[] = { + {NULL, admin_show_ubep}, + {"show", admin_show_ubep}, + {"add_eid", admin_add_eid}, + {"del_eid", admin_del_eid}, + {"set_eid_mode", admin_set_eid_mode}, + {"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_reserved_jetty", admin_set_reserved_jetty_id_range}, + {"list_res", admin_list_res}, + {"show_topo", admin_show_topo_info}, + {0}, + }; + return admin_exec_cmd(cfg, cmds); } #define MAX_CMDLINE_LEN 896 /* must less than MAX_LOG_LEN */ @@ -602,38 +568,42 @@ static void admin_log_cmd(int argc, char *argv[], int ret) int main(int argc, char *argv[]) { - int ret; - tool_config_t tool_cfg = {0}; - if (admin_check_cmd_len(argc, argv) != 0) { (void)printf("user: %s, cmd len out of range.\n", getlogin()); return -1; } - ret = admin_parse_args(argc, argv, &tool_cfg); + int ret; + admin_config_t cfg = { + .ue_idx = OWN_UE_IDX, + }; + + ret = admin_parse_args(argc, argv, &cfg); if (ret != 0) { (void)printf("Invalid parameter.\n"); - admin_log_cmd(argc, argv, ret); - return ret; + URMA_ADMIN_LOG("Invalid parameter\n."); + usage(argv[0]); + goto exit; } - if (tool_cfg.help) { + cfg.argc = argc - optind; + cfg.argv = argv + optind; + + if (cfg.help) { /* Do not execute other operations for --help parameter */ return 0; } - if (tool_cfg.cmd == TOOL_CMD_NUM) { - URMA_ADMIN_LOG("tool_cfg.cmd == TOOL_CMD_NUM\n."); - admin_log_cmd(argc, argv, ret); - return 0; - } - ret = execute_command(&tool_cfg); + ret = admin_cmd_main(&cfg); if (ret != 0) { (void)printf("Failed to execute command.\n"); URMA_ADMIN_LOG("Failed to execute command\n."); - admin_log_cmd(argc, argv, ret); - return ret; + goto exit; } + admin_log_cmd(argc, argv, ret); + return 0; + +exit: admin_log_cmd(argc, argv, ret); return ret; } diff --git a/src/urma/tools/urma_admin/admin_cmd.c b/src/urma/tools/urma_admin/admin_cmd.c index dc6af872e3c4b8aacb71ac1b1b1fd25674e37d60..aff4a5f0d8c1a3cfa1d9021fb7d5adef4c44f262 100644 --- a/src/urma/tools/urma_admin/admin_cmd.c +++ b/src/urma/tools/urma_admin/admin_cmd.c @@ -304,12 +304,17 @@ static int cb_update_eid_handler(struct nl_msg *msg, void *arg) return 0; } -int admin_add_eid(const tool_config_t *cfg) +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; @@ -333,12 +338,17 @@ int admin_add_eid(const tool_config_t *cfg) return ret; } -int admin_del_eid(const tool_config_t *cfg) +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; @@ -362,11 +372,16 @@ int admin_del_eid(const tool_config_t *cfg) return ret; } -int admin_set_eid_mode(const tool_config_t *cfg) +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; @@ -421,11 +436,19 @@ static int admin_cmd_query_stats(struct nl_sock *sock, const tool_config_t *cfg, return 0; } -int admin_show_stats(const tool_config_t *cfg) +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; @@ -526,7 +549,7 @@ free_topo: return ret; } -int admin_show_topo_info(const tool_config_t *cfg) +int admin_show_topo_info(tool_config_t *cfg) { struct nl_sock *sock = NULL; int genl_id; @@ -1023,12 +1046,16 @@ static int admin_cmd_query_res(struct nl_sock *sock, const tool_config_t *cfg, i return ret; } -int admin_show_res(const tool_config_t *cfg) +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"); @@ -1091,7 +1118,7 @@ static int admin_cmd_list_res(struct nl_sock *sock, const tool_config_t *cfg, in return ret; } -int admin_list_res(const tool_config_t *cfg) +int admin_list_res(tool_config_t *cfg) { struct nl_sock *sock = NULL; int genl_id; @@ -1143,7 +1170,7 @@ static int admin_nl_send_recv(struct nl_sock *sock, struct nl_msg *msg) return ret; } -int admin_set_ns_mode(const tool_config_t *cfg) +int admin_set_ns_mode(tool_config_t *cfg) { struct nl_sock *sock = NULL; int genl_id; @@ -1190,7 +1217,7 @@ close_sock: return ret; } -int admin_set_dev_ns(const tool_config_t *cfg) +int admin_set_dev_ns(tool_config_t *cfg) { int ret = 0; int ns_fd = -1; diff --git a/src/urma/tools/urma_admin/admin_cmd.h b/src/urma/tools/urma_admin/admin_cmd.h index 968f34c8732c4f0cd34d9a6f8c71df85870fc679..1d08e4b3d23b7127a567a390a827b0630640c47b 100644 --- a/src/urma/tools/urma_admin/admin_cmd.h +++ b/src/urma/tools/urma_admin/admin_cmd.h @@ -138,13 +138,13 @@ enum { }; int admin_show_utp(const tool_config_t *cfg); -int admin_show_stats(const tool_config_t *cfg); -int admin_show_res(const tool_config_t *cfg); -int admin_add_eid(const tool_config_t *cfg); -int admin_del_eid(const tool_config_t *cfg); -int admin_set_eid_mode(const tool_config_t *cfg); -int admin_set_ns_mode(const tool_config_t *cfg); -int admin_set_dev_ns(const tool_config_t *cfg); -int admin_list_res(const tool_config_t *cfg); -int admin_show_topo_info(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_list_res(tool_config_t *cfg); +int admin_show_topo_info(tool_config_t *cfg); #endif diff --git a/src/urma/tools/urma_admin/admin_parameters.c b/src/urma/tools/urma_admin/admin_parameters.c index 71db9245ed41fa4116dbffbaf7482fa55f7d6527..e728e56cdf37820c22d728eaaaa2982822f68bf0 100644 --- a/src/urma/tools/urma_admin/admin_parameters.c +++ b/src/urma/tools/urma_admin/admin_parameters.c @@ -119,7 +119,7 @@ int admin_str_to_u64(const char *buf, uint64_t *u64) return 0; } -static void usage(const char *argv0) +void usage(const char *argv0) { (void)printf("Usage: %s command [command options]\n", argv0); (void)printf(" %s URMA configuration tool, chips do not support some values, which might be invalid.\n", argv0); @@ -182,34 +182,6 @@ static void usage(const char *argv0) (void)printf(" -u, --max_id <0 - U32_MAX> max reserved jetty id, U32_MAX means invalid.\n"); } -static tool_cmd_type_t parse_command(const char *argv1) -{ - int i; - - tool_cmd_t cmd[] = {{"show", TOOL_CMD_SHOW}, - {"add_eid", TOOL_CMD_ADD_EID}, - {"del_eid", TOOL_CMD_DEL_EID}, - {"set_eid_mode", TOOL_CMD_SET_EID_MODE}, - {"show_stats", TOOL_CMD_SHOW_STATS}, - {"show_res", TOOL_CMD_SHOW_RES}, - {"set_ns_mode", TOOL_CMD_SET_NS_MODE}, - {"set_dev_ns", TOOL_CMD_SET_DEV_NS}, - {"set_reserved_jetty", TOOL_CMD_SET_RESERVED_JETTY}, - {"list_res", TOOL_CMD_LIST_RES}, - {"show_topo", TOOL_CMD_SHOW_TOPO_INFO}}; - - for (i = 0; i < (int)TOOL_CMD_NUM; i++) { - if (strlen(argv1) != strlen(cmd[i].cmd)) { - continue; - } - if (strcmp(argv1, cmd[i].cmd) == 0) { - return cmd[i].type; - } - } - - return TOOL_CMD_NUM; -} - #define IPV4_MAP_IPV6_PREFIX 0x0000ffff #define EID_STR_MIN_LEN 3 static inline void ipv4_map_to_eid(uint32_t ipv4, urma_eid_t *eid) @@ -252,35 +224,6 @@ int admin_str_to_eid(const char *buf, urma_eid_t *eid) return -EINVAL; } -static void init_tool_cfg(tool_config_t *cfg) -{ - (void)memset(cfg, 0, sizeof(tool_config_t)); - cfg->specify_device = false; - cfg->whole_info = false; - cfg->ue_idx = OWN_UE_IDX; -} - -static int check_query_type(const tool_config_t *cfg) -{ - if (cfg->cmd == TOOL_CMD_SHOW_STATS) { - 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->cmd == TOOL_CMD_SHOW_RES) { - 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; - } - } - return 0; -} - static bool check_dev_name(char *dev_name) { bool ret = false; @@ -337,19 +280,6 @@ static int admin_parse_dev_name(char *buf, tool_config_t *cfg) return 0; } -static int admin_parse_resource_type(char *buf, tool_config_t *cfg) -{ - if (admin_str_to_u32(buf, &cfg->key.type) != 0) { - return -1; - } - if (check_query_type(cfg) != 0) { - (void)printf("Failed to check query type: %u.\n", cfg->key.type); - URMA_ADMIN_LOG("Failed to check query type: %u.\n", cfg->key.type); - return -1; - } - return 0; -} - static int admin_parse_ns(char *buf, tool_config_t *cfg) { if (strnlen(buf, URMA_ADMIN_MAX_NS_PATH) + 1 > URMA_ADMIN_MAX_NS_PATH) { @@ -364,7 +294,7 @@ static int admin_parse_ns(char *buf, tool_config_t *cfg) return 0; } -static int admin_inner_parse_args(int argc, char *argv[], tool_config_t *cfg) +int admin_parse_args(int argc, char *argv[], tool_config_t *cfg) { int ret = 0; while (1) { @@ -400,7 +330,7 @@ static int admin_inner_parse_args(int argc, char *argv[], tool_config_t *cfg) cfg->whole_info = true; break; case 'R': - ret = admin_parse_resource_type(optarg, cfg); + ret = admin_str_to_u32(optarg, &cfg->key.type); break; case 'k': ret = admin_str_to_u32(optarg, &cfg->key.key); @@ -430,39 +360,46 @@ static int admin_inner_parse_args(int argc, char *argv[], tool_config_t *cfg) return -1; } } - if (optind < argc - 1) { - URMA_ADMIN_LOG("optind < argc - 1\n"); - usage(argv[0]); - return -1; - } return 0; } -int admin_parse_args(int argc, char *argv[], tool_config_t *cfg) +int admin_exec_cmd(admin_config_t *cfg, const admin_cmd_t *cmds) { - int ret = 0; + if (cfg->argc == 0) { + return cmds->func(cfg); + } - if (argc == 1 || cfg == NULL) { - URMA_ADMIN_LOG("Invalid parameter\n."); - usage(argv[0]); - return -1; + const admin_cmd_t *cmd = cmds + 1; + while (cmd->name) { + if (strncmp(cmd->name, *cfg->argv, strlen(cmd->name) + 1) == 0) { + cfg->argc--; + cfg->argv++; + return cmd->func(cfg); + } + cmd++; } - init_tool_cfg(cfg); - /* First parse the command */ - cfg->cmd = parse_command(argv[1]); + printf("Unknown cmd '%s'.\n", *cfg->argv); + return 0; +} - /* Second parse the options */ - ret = admin_inner_parse_args(argc, argv, cfg); - if (ret != 0) { - return -1; +bool is_1650(const char *dev_name) +{ + char *device_path = calloc(1, DEV_PATH_MAX); + if (device_path == NULL) { + return false; } - /* Increase illegal cmd return error */ - if (cfg->cmd == TOOL_CMD_NUM && cfg->help == false) { - URMA_ADMIN_LOG("cfg->cmd == TOOL_CMD_NUM\n"); - usage(argv[0]); - return -1; + if (snprintf(device_path, DEV_PATH_MAX - 1, "%s/%s/device", SYS_CLASS_PATH, dev_name) <= 0) { + (void)printf("snprintf failed, dev:%s.\n", dev_name); + free(device_path); + return false; } - return 0; + + const uint32_t device_id_1650 = 0xa001; + uint32_t device_id; + (void)admin_parse_file_value_u32(device_path, "device", &device_id); + + free(device_path); + return device_id == device_id_1650; } diff --git a/src/urma/tools/urma_admin/admin_parameters.h b/src/urma/tools/urma_admin/admin_parameters.h index 3b8ec215c8bc97e2c528478e2368e861034f1cd0..63431af261fc3fc3106cc372a98af121c7b987d3 100644 --- a/src/urma/tools/urma_admin/admin_parameters.h +++ b/src/urma/tools/urma_admin/admin_parameters.h @@ -31,26 +31,6 @@ #define URMA_ADMIN_MAX_NS_PATH 128 /* /proc/$pid/ns/net */ #define OWN_UE_IDX (0xffff) -typedef enum tool_cmd_type { - TOOL_CMD_SHOW, - TOOL_CMD_ADD_EID, - TOOL_CMD_DEL_EID, - TOOL_CMD_SET_EID_MODE, - TOOL_CMD_SHOW_STATS, - TOOL_CMD_SHOW_RES, - TOOL_CMD_SET_NS_MODE, - TOOL_CMD_SET_DEV_NS, - TOOL_CMD_LIST_RES, - TOOL_CMD_SET_RESERVED_JETTY, - TOOL_CMD_SHOW_TOPO_INFO, - TOOL_CMD_NUM -} tool_cmd_type_t; - -typedef struct tool_cmd { - char *cmd; - tool_cmd_type_t type; -} tool_cmd_t; - /* refer to enum ubcore_stats_key_type */ typedef enum tool_stats_key_type { TOOL_STATS_KEY_VTP = 1, @@ -282,7 +262,8 @@ typedef struct tool_res_dev_val { } tool_res_dev_val_t; typedef struct tool_config { - tool_cmd_type_t cmd; + int argc; + char **argv; bool specify_device; bool whole_info; bool help; @@ -307,4 +288,16 @@ int admin_str_to_u32(const char *buf, uint32_t *u32); int admin_str_to_u64(const char *buf, uint64_t *u64); int admin_parse_args(int argc, char *argv[], tool_config_t *cfg); +typedef tool_config_t admin_config_t; + +typedef struct admin_cmd { + char *name; + int (*func)(admin_config_t *cfg); +} admin_cmd_t; + +void usage(const char *argv0); +int admin_exec_cmd(admin_config_t *cfg, const admin_cmd_t *cmds); + +bool is_1650(const char *dev_name); + #endif