DPDK patches and discussions
 help / color / mirror / Atom feed
From: Jacek Piasecki <jacekx.piasecki@intel.com>
To: dev@dpdk.org
Cc: bruce.richardson@intel.com, deepak.k.jain@intel.com,
	michalx.k.jastrzebski@intel.com,
	Kuba Kozak <kubax.kozak@intel.com>
Subject: [dpdk-dev] [PATCH 3/3] app/testpmd: changed example to parse options from cfg file
Date: Tue, 30 May 2017 10:30:37 +0200	[thread overview]
Message-ID: <1496133037-3014-4-git-send-email-jacekx.piasecki@intel.com> (raw)
In-Reply-To: <1496133037-3014-1-git-send-email-jacekx.piasecki@intel.com>

From: Kuba Kozak <kubax.kozak@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@intel.com>
Suggested-by: Bruce Richardson <bruce.richardson@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

  parent reply	other threads:[~2017-05-30  8:35 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-30  8:30 [dpdk-dev] [PATCH 0/3] Add support for using a config file for DPDK Jacek Piasecki
2017-05-30  8:30 ` [dpdk-dev] [PATCH 1/3] cfgfile: add new API functions Jacek Piasecki
2017-05-31 14:20   ` Bruce Richardson
2017-05-31 14:22     ` Bruce Richardson
2017-06-26 10:59   ` [dpdk-dev] [PATCH v2 0/7] Add support for using a config file for DPDK Jacek Piasecki
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 1/7] cfgfile: remove EAL dependency Jacek Piasecki
2017-06-26 13:12       ` Dumitrescu, Cristian
2017-06-27 10:26       ` [dpdk-dev] [PATCH v3 0/4] Rework cfgfile API to enable apps config file support Jacek Piasecki
2017-06-27 10:26         ` [dpdk-dev] [PATCH v3 1/4] cfgfile: remove EAL dependency Jacek Piasecki
2017-06-30  9:44           ` Bruce Richardson
2017-06-30 11:16             ` Bruce Richardson
2017-06-27 10:26         ` [dpdk-dev] [PATCH v3 2/4] cfgfile: add new functions to API Jacek Piasecki
2017-06-30  9:55           ` Bruce Richardson
2017-06-30 10:28           ` Bruce Richardson
2017-06-27 10:26         ` [dpdk-dev] [PATCH v3 3/4] cfgfile: rework of load function Jacek Piasecki
2017-06-30 11:18           ` Bruce Richardson
2017-06-27 10:26         ` [dpdk-dev] [PATCH v3 4/4] test/cfgfile: add new unit test Jacek Piasecki
2017-06-30 11:20         ` [dpdk-dev] [PATCH v3 0/4] Rework cfgfile API to enable apps config file support Bruce Richardson
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 2/7] cfgfile: add new functions to API Jacek Piasecki
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 3/7] cfgfile: rework of load function Jacek Piasecki
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 4/7] test/cfgfile: add new unit test Jacek Piasecki
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 5/7] eal: add functions parsing EAL arguments Jacek Piasecki
2017-06-27 10:52       ` [dpdk-dev] [PATCH v3 0/3] EAL change for using a config file for DPDK Jacek Piasecki
2017-06-27 10:52         ` [dpdk-dev] [PATCH v3 1/3] eal: add functions parsing EAL arguments Jacek Piasecki
2017-06-30 16:04           ` Bruce Richardson
2017-07-10 12:44           ` [dpdk-dev] [PATCH v4 0/5] Rework cfgfile API to enable apps config file support Jacek Piasecki
2017-07-10 12:44             ` [dpdk-dev] [PATCH v4 1/5] cfgfile: remove EAL dependency Jacek Piasecki
2017-08-30 17:58               ` Bruce Richardson
2017-07-10 12:44             ` [dpdk-dev] [PATCH v4 2/5] cfgfile: change existing API functions Jacek Piasecki
2017-08-30 20:07               ` Bruce Richardson
2017-07-10 12:44             ` [dpdk-dev] [PATCH v4 3/5] cfgfile: add new functions to API Jacek Piasecki
2017-08-30 20:18               ` Bruce Richardson
2017-07-10 12:44             ` [dpdk-dev] [PATCH v4 4/5] cfgfile: rework of load function Jacek Piasecki
2017-08-30 20:24               ` Bruce Richardson
2017-07-10 12:44             ` [dpdk-dev] [PATCH v4 5/5] test/cfgfile: add new unit test Jacek Piasecki
2017-08-30 20:25               ` Bruce Richardson
2017-09-04  9:21                 ` Bruce Richardson
2017-09-04  9:30               ` Bruce Richardson
2017-09-15 13:56                 ` Thomas Monjalon
2017-09-18 13:49                   ` Jastrzebski, MichalX K
2017-07-10 15:13             ` [dpdk-dev] [PATCH v4 0/5] Rework cfgfile API to enable apps config file support Thomas Monjalon
2017-07-20 21:48               ` Thomas Monjalon
2017-07-10 12:51           ` [dpdk-dev] [PATCH v4 0/3] EAL change for using a config file for DPDK Kuba Kozak
2017-07-10 12:51             ` [dpdk-dev] [PATCH v4 1/3] eal: add functions parsing EAL arguments Kuba Kozak
2017-07-13  9:26               ` [dpdk-dev] [PATCH v5 0/3] EAL change for using a config file for DPDK Kuba Kozak
2017-07-13 10:07               ` Kuba Kozak
2017-07-13 10:07                 ` [dpdk-dev] [PATCH v5 1/3] eal: add functions parsing EAL arguments Kuba Kozak
2017-07-13 10:07                 ` [dpdk-dev] [PATCH v5 2/3] app/testpmd: add parse options from cfg file Kuba Kozak
2017-07-13 10:07                 ` [dpdk-dev] [PATCH v5 3/3] app/testpmd: add parse options from JSON " Kuba Kozak
2019-01-23 19:31                 ` [dpdk-dev] [PATCH v5 0/3] EAL change for using a config file for DPDK Ferruh Yigit
2019-01-23 20:26                   ` Thomas Monjalon
2019-01-24 13:54                     ` Ferruh Yigit
2019-01-24 14:32                       ` Thomas Monjalon
2019-01-24 14:46                         ` Ferruh Yigit
2019-01-24 16:06                           ` Thomas Monjalon
2019-01-24 16:18                             ` Ferruh Yigit
2019-01-24 17:45                               ` Thomas Monjalon
2019-01-28 14:43                                 ` Ferruh Yigit
2017-07-10 12:51             ` [dpdk-dev] [PATCH v4 2/3] app/testpmd: add parse options from cfg file Kuba Kozak
2017-07-10 12:51             ` [dpdk-dev] [PATCH v4 3/3] app/testpmd: add parse options from JSON " Kuba Kozak
2017-06-27 10:52         ` [dpdk-dev] [PATCH v3 2/3] app/testpmd: changed example to parse options from " Jacek Piasecki
2017-06-27 10:52         ` [dpdk-dev] [PATCH v3 3/3] app/testpmd: add parse arguments from JSON config file Jacek Piasecki
2017-07-05  0:00         ` [dpdk-dev] [PATCH v3 0/3] EAL change for using a config file for DPDK Thomas Monjalon
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 6/7] app/testpmd: changed example to parse options from cfg file Jacek Piasecki
2017-06-26 10:59     ` [dpdk-dev] [PATCH v2 7/7] app/testpmd: add parse arguments from JSON config file Jacek Piasecki
2017-05-30  8:30 ` [dpdk-dev] [PATCH 2/3] eal: add functions parsing EAL arguments Jacek Piasecki
2017-05-31 15:46   ` Bruce Richardson
2017-05-30  8:30 ` Jacek Piasecki [this message]
2017-06-20  2:13   ` [dpdk-dev] [PATCH 3/3] app/testpmd: changed example to parse options from cfg file Wu, Jingjing
2017-06-20 10:10     ` Kozak, KubaX

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=1496133037-3014-4-git-send-email-jacekx.piasecki@intel.com \
    --to=jacekx.piasecki@intel.com \
    --cc=bruce.richardson@intel.com \
    --cc=deepak.k.jain@intel.com \
    --cc=dev@dpdk.org \
    --cc=kubax.kozak@intel.com \
    --cc=michalx.k.jastrzebski@intel.com \
    /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).