Soft Patch Panel
 help / color / mirror / Atom feed
From: ogawa.yasufumi@lab.ntt.co.jp
To: spp@dpdk.org, ferruh.yigit@intel.com, ogawa.yasufumi@lab.ntt.co.jp
Subject: [spp] [PATCH 04/10] spp_pcap: revise parser functions
Date: Fri, 31 May 2019 17:52:36 +0900	[thread overview]
Message-ID: <1559292762-27042-5-git-send-email-ogawa.yasufumi@lab.ntt.co.jp> (raw)
In-Reply-To: <1559292762-27042-1-git-send-email-ogawa.yasufumi@lab.ntt.co.jp>

From: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>

This update is to revise name of functions, struct, and variables for
refactoring.

Signed-off-by: Yasufumi Ogawa <ogawa.yasufumi@lab.ntt.co.jp>
---
 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


  parent reply	other threads:[~2019-05-31  8:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-31  8:52 [spp] [PATCH 00/10] Refactor cmd parser in spp_pcap ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 01/10] spp_pcap: rename enum spp_command_type ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 02/10] spp_pcap: rename struct spp_command ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 03/10] spp_pcap: revise command response codes ogawa.yasufumi
2019-05-31  8:52 ` ogawa.yasufumi [this message]
2019-05-31  8:52 ` [spp] [PATCH 05/10] spp_pcap: refactor func for splitting cmd tokens ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 06/10] spp_pcap: revise log msgs in parser func ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 07/10] spp_pcap: remove unused string list ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 08/10] spp_pcap: refactor comments of spp_pcap ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 09/10] spp_pcap: rename spp_get_core_status ogawa.yasufumi
2019-05-31  8:52 ` [spp] [PATCH 10/10] spp_pcap: revise name of vars for parser error ogawa.yasufumi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1559292762-27042-5-git-send-email-ogawa.yasufumi@lab.ntt.co.jp \
    --to=ogawa.yasufumi@lab.ntt.co.jp \
    --cc=ferruh.yigit@intel.com \
    --cc=spp@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).