[dpdk-dev] [PATCH 3/3] app/testpmd: changed example to parse options from cfg file

Jacek Piasecki jacekx.piasecki at intel.com
Tue May 30 10:30:37 CEST 2017


From: Kuba Kozak <kubax.kozak at intel.com>

This patch shows how to pass arguments to application
using config.ini file.

If a --cfgfile-path <path> option is passed into commandline
non EAL section, then the file is loaded and used by app.

If a file called config.ini is present in current working
directory, and no --cfgfile-path option is passed in, config.ini
file will be loaded and used by app.

Signed-off-by: Kuba Kozak <kubax.kozak at intel.com>
Suggested-by: Bruce Richardson <bruce.richardson at intel.com>
---
 app/test-pmd/config.ini   |   16 ++++++
 app/test-pmd/parameters.c |  135 +++++++++++++++++++++++++++++++++++++++++++--
 app/test-pmd/testpmd.c    |   51 ++++++++++++++++-
 app/test-pmd/testpmd.h    |    2 +-
 4 files changed, 196 insertions(+), 8 deletions(-)
 create mode 100644 app/test-pmd/config.ini

diff --git a/app/test-pmd/config.ini b/app/test-pmd/config.ini
new file mode 100644
index 0000000..dfafc55
--- /dev/null
+++ b/app/test-pmd/config.ini
@@ -0,0 +1,16 @@
+[DPDK]
+v =
+l = 0-4
+n = 4
+master-lcore = 0
+proc-type = primary
+vdev = net_ring0
+vdev = net_ring1
+vdev = net_ring2
+vdev = net_ring3
+
+[TEST-PMD]
+i =
+portmask = 0xf
+nb-cores = 4
+port-topology = paired
diff --git a/app/test-pmd/parameters.c b/app/test-pmd/parameters.c
index fbe6284..974c548 100644
--- a/app/test-pmd/parameters.c
+++ b/app/test-pmd/parameters.c
@@ -77,9 +77,15 @@
 #include <rte_eth_bond.h>
 #endif
 #include <rte_flow.h>
+#include <rte_cfgfile.h>
 
 #include "testpmd.h"
 
+#ifdef RTE_LIBRTE_CFGFILE
+static int cfg_argc;
+static char **cfg_argv;
+#endif
+
 static void
 usage(char* progname)
 {
@@ -549,15 +555,107 @@
 	return 0;
 }
 
+#ifdef RTE_LIBRTE_CFGFILE
+
+static void
+free_pointer_array(char **pkt)
+{
+	int i = 0;
+
+	if (pkt) {
+		while (pkt[i]) {
+			free(pkt[i]);
+			pkt[i++] = 0;
+		}
+		free(pkt);
+		pkt = 0;
+	}
+}
+
+static char *strdup_with_prefix(const char *p)
+{
+	char *np;
+
+	if (strlen(p) > 1) {
+		np = (char *)malloc(strlen(p)+3);
+		if (np)
+			strcpy(np, "--");
+	} else {
+		np = (char *)malloc(strlen(p)+2);
+		if (np)
+			strcpy(np, "-");
+	}
+	return np ? strcat(np, p) : np;
+}
+
+#define APP_SECTION "TEST-PMD"
+static int non_eal_configure(struct rte_cfgfile *cfg)
+{
+	int i, num_entries;
+
+	if (cfg == NULL) {
+		rte_errno = -EINVAL;
+		return -1;
+	}
+
+	if (!rte_cfgfile_has_section(cfg, APP_SECTION))
+		return 0;
+
+	num_entries = rte_cfgfile_section_num_entries(cfg, APP_SECTION);
+	if (num_entries <= 0)
+		return 0;
+
+	cfg_argv = malloc((num_entries * 2 + 1) * sizeof(cfg_argv[0]));
+	if (cfg_argv == NULL) {
+		rte_errno = -ENOMEM;
+		return -1;
+	}
+
+	struct rte_cfgfile_entry cfg_entries[num_entries];
+
+	rte_cfgfile_section_entries(cfg, APP_SECTION, cfg_entries, num_entries);
+
+	cfg_argc = 0;
+	for (i = 0; i < num_entries; i++) {
+		cfg_argv[cfg_argc] = strdup_with_prefix(cfg_entries[i].name);
+		if (!(cfg_argv[cfg_argc]))
+			goto allocation_error;
+		cfg_argc++;
+		if (strlen(cfg_entries[i].value)) {
+			cfg_argv[cfg_argc] = strdup(cfg_entries[i].value);
+			if (!(cfg_argv[cfg_argc]))
+				goto allocation_error;
+			cfg_argc++;
+		}
+	}
+	/* set last pointer to 0 */
+	cfg_argv[cfg_argc] = 0;
+
+	return cfg_argc;
+
+allocation_error:
+	printf("Warning: Cannot allocate memory in rte_eal_configure()\n");
+	rte_errno = ENOMEM;
+	for (i = 1; i < cfg_argc; i++) {
+		free(cfg_argv[i]);
+		cfg_argv[i] = 0;
+	}
+	return -1;
+}
+#endif
+
 void
-launch_args_parse(int argc, char** argv)
+launch_args_parse(int argc, char **argv, struct rte_cfgfile *cfg)
 {
+	int combined_argc; /* combine cfg and param versions */
+	char **combined_argv;
 	int n, opt;
 	char **argvopt;
 	int opt_idx;
 	enum { TX, RX };
 
 	static struct option lgopts[] = {
+		{ "cfgfile-path",		1, 0, 1 },
 		{ "help",			0, 0, 0 },
 #ifdef RTE_LIBRTE_CMDLINE
 		{ "interactive",		0, 0, 0 },
@@ -632,14 +730,32 @@
 		{ 0, 0, 0, 0 },
 	};
 
-	argvopt = argv;
+#ifdef RTE_LIBRTE_CFGFILE
+	int i;
+
+	non_eal_configure(cfg);
+
+	combined_argc = argc + cfg_argc;
+	combined_argv = malloc((combined_argc + 1) * sizeof(char *));
+	combined_argv[0] = argv[0];
+	for (i = 0; i < cfg_argc; i++)
+		combined_argv[i + 1] = cfg_argv[i];
+	for (i = 1; i < argc; i++)
+		combined_argv[i + cfg_argc] = argv[i];
+	combined_argv[combined_argc] = NULL;
+#else
+	combined_argc = argc;
+	combined_argv = argv;
+#endif
+
+	argvopt = combined_argv;
 
 #ifdef RTE_LIBRTE_CMDLINE
 #define SHORTOPTS "i"
 #else
 #define SHORTOPTS ""
 #endif
-	while ((opt = getopt_long(argc, argvopt, SHORTOPTS "ah",
+	while ((opt = getopt_long(combined_argc, argvopt, SHORTOPTS "ah",
 				 lgopts, &opt_idx)) != EOF) {
 		switch (opt) {
 #ifdef RTE_LIBRTE_CMDLINE
@@ -655,7 +771,7 @@
 
 		case 0: /*long options */
 			if (!strcmp(lgopts[opt_idx].name, "help")) {
-				usage(argv[0]);
+				usage(combined_argv[0]);
 				rte_exit(EXIT_SUCCESS, "Displayed help\n");
 			}
 #ifdef RTE_LIBRTE_CMDLINE
@@ -1094,14 +1210,21 @@
 
 			break;
 		case 'h':
-			usage(argv[0]);
+			usage(combined_argv[0]);
 			rte_exit(EXIT_SUCCESS, "Displayed help\n");
 			break;
+		case 1:
+			/* does nothing*/
+			break;
 		default:
-			usage(argv[0]);
+			usage(combined_argv[0]);
 			rte_exit(EXIT_FAILURE,
 				 "Command line is incomplete or incorrect\n");
 			break;
 		}
 	}
+
+#ifdef RTE_LIBRTE_CFGFILE
+	free_pointer_array(cfg_argv);
+#endif
 }
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index d1041af..0d9d363 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -87,6 +87,7 @@
 #ifdef RTE_LIBRTE_LATENCY_STATS
 #include <rte_latencystats.h>
 #endif
+#include <rte_cfgfile.h>
 
 #include "testpmd.h"
 
@@ -2248,15 +2249,54 @@ uint8_t port_is_bonding_slave(portid_t slave_pid)
 	}
 }
 
+
+
+#ifdef RTE_LIBRTE_CFGFILE
+/* Load config file path from command line */
+static char *
+cfgfile_load_path(int argc, char **argv)
+{
+	int i;
+
+	for (i = 0; i < argc; i++) {
+		if (!strcmp("--cfgfile-path", argv[i])) {
+			if (i < argc)
+				return strdup(argv[i+1]);
+		}
+	}
+	return 0;
+}
+#endif
+
 int
 main(int argc, char** argv)
 {
 	int  diag;
 	uint8_t port_id;
+	struct rte_cfgfile *cfg = NULL;
+	char *config_file = NULL;
 
 	signal(SIGINT, signal_handler);
 	signal(SIGTERM, signal_handler);
 
+#ifdef RTE_LIBRTE_CFGFILE
+	/* load --cfgfile-path argument from argv */
+	config_file = cfgfile_load_path(argc, argv);
+
+	if (config_file) {
+		printf("Info: found cfgfile-path \"%s\"\n", config_file);
+	} else {
+		printf("Info: not found cfgfile-path parameter "
+				"(searching for cfgfile "
+				"in default directory)\n");
+		config_file = strdup("config.ini");
+	}
+	cfg = rte_cfgfile_load(config_file, CFG_FLAG_EMPTY_VALUES);
+	if (cfg == NULL)
+		printf("Info: Valid cfgfile not found\n");
+	rte_eal_configure(cfg);
+#endif
+
 	diag = rte_eal_init(argc, argv);
 	if (diag < 0)
 		rte_panic("Cannot init EAL\n");
@@ -2289,7 +2329,16 @@ uint8_t port_is_bonding_slave(portid_t slave_pid)
 	argc -= diag;
 	argv += diag;
 	if (argc > 1)
-		launch_args_parse(argc, argv);
+		launch_args_parse(argc, argv, cfg);
+
+
+	rte_cfgfile_close(cfg);
+	cfg = 0;
+
+	if (config_file) {
+		free(config_file);
+		config_file = 0;
+	}
 
 	if (!nb_rxq && !nb_txq)
 		printf("Warning: Either rx or tx queues should be non-zero\n");
diff --git a/app/test-pmd/testpmd.h b/app/test-pmd/testpmd.h
index e6c43ba..5c1d477 100644
--- a/app/test-pmd/testpmd.h
+++ b/app/test-pmd/testpmd.h
@@ -499,7 +499,7 @@ enum tx_pkt_split {
 unsigned int parse_item_list(char* str, const char* item_name,
 			unsigned int max_items,
 			unsigned int *parsed_items, int check_unique_values);
-void launch_args_parse(int argc, char** argv);
+void launch_args_parse(int argc, char **argv, struct rte_cfgfile *cfg);
 void cmdline_read_from_file(const char *filename);
 void prompt(void);
 void prompt_exit(void);
-- 
1.7.9.5



More information about the dev mailing list