[dpdk-dev] [PATCH v3] ip_pipeline: configuration file parser cleanup

Jasvinder Singh jasvinder.singh at intel.com
Wed May 11 18:36:00 CEST 2016


This patch updates the parsing routines related to packet queues
(pktq_in/out fields in the PIPELINE section) and message queues
(msgq_in/out fields of in the MSGQ Section) specified in ip_pipeline
configuration file.

In the updated routines, function "strtok_r()" is used for parsing the
string instead of manually checking the string termination, white
spaces, tabs etc., between the string tokens. Each call to strtok_r()
returns a pointer to a null-terminated string containing the next token.
If no more tokens are found, strtok_r() returns NULL. As a result of
using strtok_r(), the code size of the parsing routines is reduced
significantly.

Signed-off-by: Jasvinder Singh <jasvinder.singh at intel.com>
Acked-by: Cristian Dumitrescu <cristian.dumitrescu at intel.com>
---
v3
- add check on the number of pktq_in/out entries
- add check on the number of msgq_in/out entries
 
v2
- update the commit message
- change the local variable name from "token" to "name"

 examples/ip_pipeline/config_parse.c | 190 +++++++++---------------------------
 1 file changed, 44 insertions(+), 146 deletions(-)

diff --git a/examples/ip_pipeline/config_parse.c b/examples/ip_pipeline/config_parse.c
index ab2f637..f09e04f 100644
--- a/examples/ip_pipeline/config_parse.c
+++ b/examples/ip_pipeline/config_parse.c
@@ -1,4 +1,4 @@
-/*-
+/*-
  *   BSD LICENSE
  *
  *   Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
@@ -307,6 +307,10 @@ APP_CHECK(exp, "Parse error in section \"%s\": entry \"%s\"\n", section, entry)
 APP_CHECK(exp, "Parse error in section \"%s\", entry \"%s\": %s\n",	\
 	section, entry, message)
 
+#define PARSE_ERROR_TOO_MANY_ELEMENTS(exp, section, entry, max)		\
+APP_CHECK(exp, "Parse error in section \"%s\", entry \"%s\": "	\
+	"maximum number of elements allowed is %lu\n",	\
+	section, entry, max)
 
 #define PARSE_ERROR_MALLOC(exp)						\
 APP_CHECK(exp, "Parse error: no free memory\n")
@@ -1128,48 +1132,19 @@ parse_pipeline_pcap_sink(struct app_params *app,
 static int
 parse_pipeline_pktq_in(struct app_params *app,
 	struct app_pipeline_params *p,
-	const char *value)
+	char *value)
 {
-	const char *next = value;
-	char *end;
-	char name[APP_PARAM_NAME_SIZE];
-	size_t name_len;
 
-	while (*next != '\0') {
+	while (1) {
 		enum app_pktq_in_type type;
 		int id;
-		char *end_space;
-		char *end_tab;
+		char *name = strtok_r(value, PARSE_DELIMITER, &value);
 
-		next = skip_white_spaces(next);
-		if (!next)
+		if (name == NULL)
 			break;
 
-		end_space = strchr(next, ' ');
-		end_tab = strchr(next, '	');
-
-		if (end_space && (!end_tab))
-			end = end_space;
-		else if ((!end_space) && end_tab)
-			end = end_tab;
-		else if (end_space && end_tab)
-			end = RTE_MIN(end_space, end_tab);
-		else
-			end = NULL;
-
-		if (!end)
-			name_len = strlen(next);
-		else
-			name_len = end - next;
-
-		if (name_len == 0 || name_len == sizeof(name))
-			return -EINVAL;
-
-		strncpy(name, next, name_len);
-		name[name_len] = '\0';
-		next += name_len;
-		if (*next != '\0')
-			next++;
+		if (p->n_pktq_in == RTE_DIM(p->pktq_in))
+			return -ENOMEM;
 
 		if (validate_name(name, "RXQ", 2) == 0) {
 			type = APP_PKTQ_IN_HWQ;
@@ -1200,48 +1175,19 @@ parse_pipeline_pktq_in(struct app_params *app,
 static int
 parse_pipeline_pktq_out(struct app_params *app,
 	struct app_pipeline_params *p,
-	const char *value)
+	char *value)
 {
-	const char *next = value;
-	char *end;
-	char name[APP_PARAM_NAME_SIZE];
-	size_t name_len;
-
-	while (*next != '\0') {
-		enum app_pktq_out_type type;
+	while (1) {
+		enum app_pktq_in_type type;
 		int id;
-		char *end_space;
-		char *end_tab;
+		char *name = strtok_r(value, PARSE_DELIMITER, &value);
 
-		next = skip_white_spaces(next);
-		if (!next)
+		if (name == NULL)
 			break;
 
-		end_space = strchr(next, ' ');
-		end_tab = strchr(next, '	');
-
-		if (end_space && (!end_tab))
-			end = end_space;
-		else if ((!end_space) && end_tab)
-			end = end_tab;
-		else if (end_space && end_tab)
-			end = RTE_MIN(end_space, end_tab);
-		else
-			end = NULL;
-
-		if (!end)
-			name_len = strlen(next);
-		else
-			name_len = end - next;
-
-		if (name_len == 0 || name_len == sizeof(name))
-			return -EINVAL;
+		if (p->n_pktq_out == RTE_DIM(p->pktq_out))
+			return -ENOMEM;
 
-		strncpy(name, next, name_len);
-		name[name_len] = '\0';
-		next += name_len;
-		if (*next != '\0')
-			next++;
 		if (validate_name(name, "TXQ", 2) == 0) {
 			type = APP_PKTQ_OUT_HWQ;
 			id = APP_PARAM_ADD(app->hwq_out_params, name);
@@ -1271,47 +1217,17 @@ parse_pipeline_pktq_out(struct app_params *app,
 static int
 parse_pipeline_msgq_in(struct app_params *app,
 	struct app_pipeline_params *p,
-	const char *value)
+	char *value)
 {
-	const char *next = value;
-	char *end;
-	char name[APP_PARAM_NAME_SIZE];
-	size_t name_len;
-	ssize_t idx;
-
-	while (*next != '\0') {
-		char *end_space;
-		char *end_tab;
+	while (1) {
+		int idx;
+		char *name = strtok_r(value, PARSE_DELIMITER, &value);
 
-		next = skip_white_spaces(next);
-		if (!next)
+		if (name == NULL)
 			break;
 
-		end_space = strchr(next, ' ');
-		end_tab = strchr(next, '	');
-
-		if (end_space && (!end_tab))
-			end = end_space;
-		else if ((!end_space) && end_tab)
-			end = end_tab;
-		else if (end_space && end_tab)
-			end = RTE_MIN(end_space, end_tab);
-		else
-			end = NULL;
-
-		if (!end)
-			name_len = strlen(next);
-		else
-			name_len = end - next;
-
-		if (name_len == 0 || name_len == sizeof(name))
-			return -EINVAL;
-
-		strncpy(name, next, name_len);
-		name[name_len] = '\0';
-		next += name_len;
-		if (*next != '\0')
-			next++;
+		if (p->n_msgq_in == RTE_DIM(p->msgq_in))
+			return -ENOMEM;
 
 		if (validate_name(name, "MSGQ", 1) != 0)
 			return -EINVAL;
@@ -1330,47 +1246,17 @@ parse_pipeline_msgq_in(struct app_params *app,
 static int
 parse_pipeline_msgq_out(struct app_params *app,
 	struct app_pipeline_params *p,
-	const char *value)
+	char *value)
 {
-	const char *next = value;
-	char *end;
-	char name[APP_PARAM_NAME_SIZE];
-	size_t name_len;
-	ssize_t idx;
-
-	while (*next != '\0') {
-		char *end_space;
-		char *end_tab;
+	while (1) {
+		int idx;
+		char *name = strtok_r(value, PARSE_DELIMITER, &value);
 
-		next = skip_white_spaces(next);
-		if (!next)
+		if (name == NULL)
 			break;
 
-		end_space = strchr(next, ' ');
-		end_tab = strchr(next, '	');
-
-		if (end_space && (!end_tab))
-			end = end_space;
-		else if ((!end_space) && end_tab)
-			end = end_tab;
-		else if (end_space && end_tab)
-			end = RTE_MIN(end_space, end_tab);
-		else
-			end = NULL;
-
-		if (!end)
-			name_len = strlen(next);
-		else
-			name_len = end - next;
-
-		if (name_len == 0 || name_len == sizeof(name))
-			return -EINVAL;
-
-		strncpy(name, next, name_len);
-		name[name_len] = '\0';
-		next += name_len;
-		if (*next != '\0')
-			next++;
+		if (p->n_msgq_out == RTE_DIM(p->msgq_out))
+			return -ENOMEM;
 
 		if (validate_name(name, "MSGQ", 1) != 0)
 			return -EINVAL;
@@ -1438,6 +1324,9 @@ parse_pipeline(struct app_params *app,
 			int status = parse_pipeline_pktq_in(app, param,
 				ent->value);
 
+			PARSE_ERROR_TOO_MANY_ELEMENTS((status != -ENOMEM),
+				section_name, ent->name,
+				RTE_DIM(param->pktq_in));
 			PARSE_ERROR((status == 0), section_name,
 				ent->name);
 			continue;
@@ -1447,6 +1336,9 @@ parse_pipeline(struct app_params *app,
 			int status = parse_pipeline_pktq_out(app, param,
 				ent->value);
 
+			PARSE_ERROR_TOO_MANY_ELEMENTS((status != -ENOMEM),
+				section_name, ent->name,
+				RTE_DIM(param->pktq_out));
 			PARSE_ERROR((status == 0), section_name,
 				ent->name);
 			continue;
@@ -1456,6 +1348,9 @@ parse_pipeline(struct app_params *app,
 			int status = parse_pipeline_msgq_in(app, param,
 				ent->value);
 
+			PARSE_ERROR_TOO_MANY_ELEMENTS((status != -ENOMEM),
+				section_name, ent->name,
+				RTE_DIM(param->msgq_in));
 			PARSE_ERROR((status == 0), section_name,
 				ent->name);
 			continue;
@@ -1465,6 +1360,9 @@ parse_pipeline(struct app_params *app,
 			int status = parse_pipeline_msgq_out(app, param,
 				ent->value);
 
+			PARSE_ERROR_TOO_MANY_ELEMENTS((status != -ENOMEM),
+				section_name, ent->name,
+				RTE_DIM(param->msgq_out));
 			PARSE_ERROR((status == 0), section_name,
 				ent->name);
 			continue;
-- 
2.5.5



More information about the dev mailing list