From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by dpdk.space (Postfix) with ESMTP id 41A38A045E for ; Fri, 31 May 2019 10:55:19 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 347901B954; Fri, 31 May 2019 10:55:19 +0200 (CEST) Received: from tama50.ecl.ntt.co.jp (tama50.ecl.ntt.co.jp [129.60.39.147]) by dpdk.org (Postfix) with ESMTP id 89530378B for ; Fri, 31 May 2019 10:55:16 +0200 (CEST) Received: from vc1.ecl.ntt.co.jp (vc1.ecl.ntt.co.jp [129.60.86.153]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id x4V8tF0Z027729; Fri, 31 May 2019 17:55:15 +0900 Received: from vc1.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 77721EA85FD; Fri, 31 May 2019 17:55:15 +0900 (JST) Received: from localhost.localdomain (lobster.nslab.ecl.ntt.co.jp [129.60.13.95]) by vc1.ecl.ntt.co.jp (Postfix) with ESMTP id 69135EA8609; Fri, 31 May 2019 17:55:15 +0900 (JST) From: ogawa.yasufumi@lab.ntt.co.jp To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp Date: Fri, 31 May 2019 17:52:36 +0900 Message-Id: <1559292762-27042-5-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1559292762-27042-1-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> References: <1559292762-27042-1-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> X-TM-AS-MML: disable Subject: [spp] [PATCH 04/10] spp_pcap: revise parser functions X-BeenThere: spp@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Soft Patch Panel List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: spp-bounces@dpdk.org Sender: "spp" From: Yasufumi Ogawa This update is to revise name of functions, struct, and variables for refactoring. Signed-off-by: Yasufumi Ogawa --- src/pcap/cmd_parser.c | 99 ++++++++++--------- src/pcap/cmd_parser.h | 2 +- src/pcap/cmd_runner.c | 4 +- .../secondary/spp_worker_th/cmd_parser.c | 4 +- 4 files changed, 58 insertions(+), 51 deletions(-) diff --git a/src/pcap/cmd_parser.c b/src/pcap/cmd_parser.c index 1624347..577e952 100644 --- a/src/pcap/cmd_parser.c +++ b/src/pcap/cmd_parser.c @@ -38,41 +38,47 @@ set_string_value_parse_error(struct sppwk_parse_err_msg *wk_err_msg, /* Split command line parameter with spaces */ static int -parse_parameter_value(char *string, int max, int *argc, char *argv[]) +parse_parameter_value(char *string, int max, int *nof_tokens, char *tokens[]) { int cnt = 0; const char *delim = " "; - char *argv_tok = NULL; + char *token = NULL; char *saveptr = NULL; - argv_tok = strtok_r(string, delim, &saveptr); - while (argv_tok != NULL) { + token = strtok_r(string, delim, &saveptr); + while (token != NULL) { if (cnt >= max) return SPPWK_RET_NG; - argv[cnt] = argv_tok; + tokens[cnt] = token; cnt++; - argv_tok = strtok_r(NULL, delim, &saveptr); + token = strtok_r(NULL, delim, &saveptr); } - *argc = cnt; + *nof_tokens = cnt; return SPPWK_RET_OK; } -/* command list for parse */ -struct parse_command_list { - const char *name; /* Command name */ - int param_min; /* Min number of parameters */ - int param_max; /* Max number of parameters */ - int (*func)(struct spp_command_request *request, int argc, - char *argv[], struct sppwk_parse_err_msg *error, - int maxargc); +/** + * A set of attributes of commands for parsing. The fourth member of function + * pointer is the operator function for the command. + */ +struct pcap_cmd_parse_attr { + const char *cmd_name; + int nof_params_min; + int nof_params_max; + int (*func)(struct spp_command_request *request, int nof_tokens, + char *tokens[], struct sppwk_parse_err_msg *error, + int nof_max_tokens); /* Pointer to command handling function */ enum pcap_cmd_type type; /* Command type */ }; -/* command list */ -static struct parse_command_list command_list_pcap[] = { +/** + * List of command attributes defines the name of command, number of params + * and operator functions. + */ +static struct pcap_cmd_parse_attr pcap_cmd_attrs[] = { { "_get_client_id", 1, 1, NULL, PCAP_CMDTYPE_CLIENT_ID }, { "status", 1, 1, NULL, PCAP_CMDTYPE_STATUS }, { "exit", 1, 1, NULL, PCAP_CMDTYPE_EXIT }, @@ -81,53 +87,54 @@ static struct parse_command_list command_list_pcap[] = { { "", 0, 0, NULL, 0 } /* termination */ }; -/* Parse command line parameters */ +/* Parse command requested from spp-ctl. */ static int -parse_command_in_list(struct spp_command_request *request, - const char *request_str, - struct sppwk_parse_err_msg *wk_err_msg) +parse_pcap_cmd(struct spp_command_request *request, + const char *request_str, + struct sppwk_parse_err_msg *wk_err_msg) { - int ret = SPPWK_RET_OK; - int command_name_check = 0; - struct parse_command_list *list = NULL; - int i = 0; - int argc = 0; - char *argv[SPPWK_MAX_PARAMS]; + int is_invalid_cmd = 0; + struct pcap_cmd_parse_attr *cmd_attr = NULL; + int ret; + int i; + char *tokens[SPPWK_MAX_PARAMS]; + int nof_tokens = 0; char tmp_str[SPPWK_MAX_PARAMS * SPPWK_VAL_BUFSZ]; - memset(argv, 0x00, sizeof(argv)); + memset(tokens, 0x00, sizeof(tokens)); memset(tmp_str, 0x00, sizeof(tmp_str)); strcpy(tmp_str, request_str); ret = parse_parameter_value(tmp_str, SPPWK_MAX_PARAMS, - &argc, argv); + &nof_tokens, tokens); if (ret < SPPWK_RET_OK) { RTE_LOG(ERR, PCAP_PARSER, "Parameter number over limit." "request_str=%s\n", request_str); return set_parse_error(wk_err_msg, SPPWK_PARSE_WRONG_FORMAT, NULL); } - RTE_LOG(DEBUG, PCAP_PARSER, "Decode array. num=%d\n", argc); + RTE_LOG(DEBUG, PCAP_PARSER, "Decode array. num=%d\n", nof_tokens); - for (i = 0; command_list_pcap[i].name[0] != '\0'; i++) { - list = &command_list_pcap[i]; - if (strcmp(argv[0], list->name) != 0) + for (i = 0; pcap_cmd_attrs[i].cmd_name[0] != '\0'; i++) { + cmd_attr = &pcap_cmd_attrs[i]; + if (strcmp(tokens[0], cmd_attr->cmd_name) != 0) continue; - if (unlikely(argc < list->param_min) || - unlikely(list->param_max < argc)) { - command_name_check = 1; + if (unlikely(nof_tokens < cmd_attr->nof_params_min) || + unlikely(cmd_attr->nof_params_max < nof_tokens)) { + is_invalid_cmd = 1; continue; } - request->cmd_attrs[0].type = command_list_pcap[i].type; - if (list->func != NULL) - return (*list->func)(request, argc, argv, wk_err_msg, - list->param_max); + request->cmd_attrs[0].type = pcap_cmd_attrs[i].type; + if (cmd_attr->func != NULL) + return (*cmd_attr->func)( + request, nof_tokens, tokens, wk_err_msg, + cmd_attr->nof_params_max); return SPPWK_RET_OK; } - if (command_name_check != 0) { + if (is_invalid_cmd != 0) { RTE_LOG(ERR, PCAP_PARSER, "Parameter number out of range." "request_str=%s\n", request_str); return set_parse_error(wk_err_msg, @@ -136,13 +143,13 @@ parse_command_in_list(struct spp_command_request *request, RTE_LOG(ERR, PCAP_PARSER, "Unknown command. command=%s, request_str=%s\n", - argv[0], request_str); - return set_string_value_parse_error(wk_err_msg, argv[0], "command"); + tokens[0], request_str); + return set_string_value_parse_error(wk_err_msg, tokens[0], "command"); } -/* parse request from no-null-terminated string */ +/* Parse request of non null terminated string. */ int -spp_command_parse_request( +sppwk_parse_req( struct spp_command_request *request, const char *request_str, size_t request_str_len, struct sppwk_parse_err_msg *wk_err_msg) @@ -152,7 +159,7 @@ spp_command_parse_request( /* parse request */ request->num_command = 1; - ret = parse_command_in_list(request, request_str, wk_err_msg); + ret = parse_pcap_cmd(request, request_str, wk_err_msg); if (unlikely(ret != SPPWK_RET_OK)) { RTE_LOG(ERR, PCAP_PARSER, "Cannot parse command request. " diff --git a/src/pcap/cmd_parser.h b/src/pcap/cmd_parser.h index 706144d..9a91216 100644 --- a/src/pcap/cmd_parser.h +++ b/src/pcap/cmd_parser.h @@ -93,7 +93,7 @@ struct sppwk_parse_err_msg { * @retval SPPWK_RET_OK succeeded. * @retval !0 failed. */ -int spp_command_parse_request(struct spp_command_request *request, +int sppwk_parse_req(struct spp_command_request *request, const char *request_str, size_t request_str_len, struct sppwk_parse_err_msg *error); diff --git a/src/pcap/cmd_runner.c b/src/pcap/cmd_runner.c index 9d63bde..3080242 100644 --- a/src/pcap/cmd_runner.c +++ b/src/pcap/cmd_runner.c @@ -868,8 +868,8 @@ process_request(int *sock, const char *request_str, size_t request_str_len) (int)request_str_len, request_str); /* parse request message */ - ret = spp_command_parse_request( - &request, request_str, request_str_len, &parse_error); + ret = sppwk_parse_req(&request, request_str, request_str_len, + &parse_error); if (unlikely(ret != SPPWK_RET_OK)) { /* send error response */ set_parse_error_to_results(command_results, &request, diff --git a/src/shared/secondary/spp_worker_th/cmd_parser.c b/src/shared/secondary/spp_worker_th/cmd_parser.c index 621bea4..9fc88dd 100644 --- a/src/shared/secondary/spp_worker_th/cmd_parser.c +++ b/src/shared/secondary/spp_worker_th/cmd_parser.c @@ -1016,8 +1016,8 @@ parse_cmd_port(struct sppwk_cmd_req *request, int argc, char *argv[], } /** - * Attributes of commands for parsing. The last member of function pointer - * is the operator function for the command. + * A set of attributes of commands for parsing. The last member of function + * pointer is the operator function for the command. */ struct cmd_parse_attrs { const char *cmd_name; -- 2.17.1