From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by dpdk.org (Postfix) with ESMTP id A75E67CBF for ; Tue, 30 May 2017 10:35:46 +0200 (CEST) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga105.jf.intel.com with ESMTP; 30 May 2017 01:35:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.38,417,1491289200"; d="scan'208";a="93354567" Received: from gklab-246-073.igk.intel.com (HELO Sent) ([10.217.246.73]) by orsmga002.jf.intel.com with SMTP; 30 May 2017 01:35:43 -0700 Received: by Sent (sSMTP sendmail emulation); Tue, 30 May 2017 10:33:49 +0200 From: Jacek Piasecki To: dev@dpdk.org Cc: bruce.richardson@intel.com, deepak.k.jain@intel.com, michalx.k.jastrzebski@intel.com, Kuba Kozak Date: Tue, 30 May 2017 10:30:37 +0200 Message-Id: <1496133037-3014-4-git-send-email-jacekx.piasecki@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1496133037-3014-1-git-send-email-jacekx.piasecki@intel.com> References: <1496133037-3014-1-git-send-email-jacekx.piasecki@intel.com> Subject: [dpdk-dev] [PATCH 3/3] app/testpmd: changed example to parse options from cfg file X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 30 May 2017 08:35:47 -0000 From: Kuba Kozak This patch shows how to pass arguments to application using config.ini file. If a --cfgfile-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 Suggested-by: Bruce Richardson --- 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 #endif #include +#include #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 #endif +#include #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