DPDK patches and discussions
 help / color / mirror / Atom feed
From: Kuba Kozak <kubax.kozak@intel.com>
To: dev@dpdk.org
Cc: deepak.k.jain@intel.com, bruce.richardson@intel.com,
	michalx.k.jastrzebski@intel.com, jacekx.piasecki@intel.com,
	Kuba Kozak <kubax.kozak@intel.com>
Subject: [dpdk-dev] [PATCH v5 3/3] app/testpmd: add parse options from JSON cfg file
Date: Thu, 13 Jul 2017 12:07:50 +0200	[thread overview]
Message-ID: <1499940470-31628-4-git-send-email-kubax.kozak@intel.com> (raw)
In-Reply-To: <1499940470-31628-1-git-send-email-kubax.kozak@intel.com>

This patch shows usage of Jansson library to parse
application arguments from JSON config file.

https://github.com/akheron/jansson

If a --cfgfile-path <path> option is passed into commandline
non EAL section, then the disired JSON file is loaded and used
by app. In case when JSON doesn't exist an INI file is loaded.
The INI file can be passed also from --cfgfile-path option.

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/Makefile    |  6 ++++
 app/test-pmd/config.json | 33 +++++++++++++++++
 app/test-pmd/testpmd.c   | 94 +++++++++++++++++++++++++++++++++++++++++++++++-
 config/common_base       |  5 +++
 4 files changed, 137 insertions(+), 1 deletion(-)
 create mode 100644 app/test-pmd/config.json

diff --git a/app/test-pmd/Makefile b/app/test-pmd/Makefile
index c36be19..a1c84cc 100644
--- a/app/test-pmd/Makefile
+++ b/app/test-pmd/Makefile
@@ -83,6 +83,12 @@ endif
 
 endif
 
+ifeq ($(CONFIG_RTE_JSON_SUPPORT),y)
+ifeq ($(CONFIG_RTE_LIBRTE_CFGFILE),y)
+LDLIBS += -ljansson
+endif
+endif
+
 CFLAGS_cmdline.o := -D_GNU_SOURCE
 
 include $(RTE_SDK)/mk/rte.app.mk
diff --git a/app/test-pmd/config.json b/app/test-pmd/config.json
new file mode 100644
index 0000000..4589dbc
--- /dev/null
+++ b/app/test-pmd/config.json
@@ -0,0 +1,33 @@
+{
+	"DPDK":
+	{
+		"v": "",
+		"l": "0-4",
+		"n": "4",
+		"master-lcore": "0",
+		"proc-type": "primary"
+	},
+	"TEST-PMD":
+	{
+		"i": "",
+		"portmask": "0xff",
+		"nb-cores": "4",
+		"port-topology": "paired"
+	},
+	"DPDK.vdev0":
+	{
+		"net_ring0": ""
+	},
+	"DPDK.vdev1":
+	{
+		"net_ring1": ""
+	},
+	"DPDK.vdev2":
+	{
+		"net_ring2": ""
+	},
+	"DPDK.vdev3":
+	{
+		"net_ring3": ""
+	}
+}
diff --git a/app/test-pmd/testpmd.c b/app/test-pmd/testpmd.c
index 7b82976..014bb46 100644
--- a/app/test-pmd/testpmd.c
+++ b/app/test-pmd/testpmd.c
@@ -46,6 +46,10 @@
 
 #include <stdint.h>
 #include <unistd.h>
+
+#ifdef RTE_JSON_SUPPORT
+#include <jansson.h>
+#endif
 #include <inttypes.h>
 
 #include <rte_common.h>
@@ -2290,6 +2294,87 @@ cfgfile_load_path(int argc, char **argv)
 	}
 	return NULL;
 }
+
+#ifdef RTE_JSON_SUPPORT
+/*
+ * Decoding JSON structure to rte_cfgfile structure.
+ * Returns handler to cfgfile object, NULL if error.
+ */
+static struct
+rte_cfgfile *l3fwd_json_to_cfg(json_t *json, int flags)
+{
+	if (!json) {
+		printf("Error: JSON structure is NULL, nothing to parse\n");
+		return NULL;
+	}
+	/* create an empty instance of cfgfile structure */
+	struct rte_cfgfile *cfgfile = rte_cfgfile_create(flags);
+
+	if (!cfgfile)
+		return NULL;
+
+	const char *section;
+	json_t *entry;
+
+	/* set pointer to first section */
+	void *iter_section = json_object_iter(json);
+
+	while (iter_section) {
+
+		section = json_object_iter_key(iter_section);
+		entry = json_object_iter_value(iter_section);
+
+		/* add parsed section name of current section to cfgfile */
+		rte_cfgfile_add_section(cfgfile, section);
+
+		/* set pointer to first entry */
+		void *iter_entry = json_object_iter(entry);
+
+		while (iter_entry) {
+
+			const char *key;
+			const char *value;
+
+			key = json_object_iter_key(iter_entry);
+			value = json_string_value(
+					json_object_iter_value(iter_entry));
+
+			/* add parsed key and value of current entry */
+			/* to cfgfile */
+			rte_cfgfile_add_entry(cfgfile, section, key, value);
+
+			/* pointer to next entry */
+			iter_entry = json_object_iter_next(entry, iter_entry);
+		}
+		/* pointer to next section */
+		iter_section = json_object_iter_next(json, iter_section);
+	}
+	return cfgfile;
+}
+
+/*
+ * Check presence and load JSON file to rte_cfgfile structure.
+ * Returns handler to cfgfile object, NULL if error.
+ */
+static struct
+rte_cfgfile *l3fwd_load_json_to_cfg(const char *path)
+{
+	struct rte_cfgfile *cfgfile = NULL;
+	/* check if config file exist */
+	if (access(path, F_OK) != -1) {
+		/* parse JSON file */
+		json_error_t error;
+		json_t *json = json_load_file(path, 0, &error);
+
+		if (json)
+			cfgfile = l3fwd_json_to_cfg(json, 0);
+		else
+			fprintf(stderr, "JSON error on line %d: %s\n",
+							error.line, error.text);
+	}
+	return cfgfile;
+}
+#endif
 #endif
 
 #define APP_NAME "TEST-PMD"
@@ -2318,9 +2403,16 @@ main(int argc, char** argv)
 				"in default directory)\n");
 		config_file = strdup("config.ini");
 	}
-
+#ifndef RTE_JSON_SUPPORT
 	cfg = rte_cfgfile_load(config_file, CFG_FLAG_EMPTY_VALUES);
+#endif
 
+#ifdef RTE_JSON_SUPPORT
+	if (strstr(config_file, ".ini"))
+		cfg = rte_cfgfile_load(config_file, CFG_FLAG_EMPTY_VALUES);
+	else if (strstr(config_file, ".json"))
+		cfg = l3fwd_load_json_to_cfg(config_file);
+#endif
 	if (cfg == NULL) {
 		printf("Info: Valid cfgfile not found\n");
 	} else {
diff --git a/config/common_base b/config/common_base
index 8ae6e92..98e8fa2 100644
--- a/config/common_base
+++ b/config/common_base
@@ -744,3 +744,8 @@ CONFIG_RTE_APP_CRYPTO_PERF=y
 # Compile the eventdev application
 #
 CONFIG_RTE_APP_EVENTDEV=y
+
+#
+# Compile JSON support
+#
+CONFIG_RTE_JSON_SUPPORT=n
\ No newline at end of file
-- 
2.7.4

  parent reply	other threads:[~2017-07-13 10:26 UTC|newest]

Thread overview: 71+ 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                 ` Kuba Kozak [this message]
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 ` [dpdk-dev] [PATCH 3/3] app/testpmd: changed example to parse options from cfg file Jacek Piasecki
2017-06-20  2:13   ` Wu, Jingjing
2017-06-20 10:10     ` Kozak, KubaX
2018-05-16 14:07 [dpdk-dev] [PATCH v5 3/3] app/testpmd: add parse options from JSON " Iremonger, Bernard

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