From: yasufum.o@gmail.com
To: spp@dpdk.org, ferruh.yigit@intel.com, yasufum.o@gmail.com
Subject: [spp] [PATCH 03/13] shared/sec: move principle JSON formatter funcs
Date: Mon, 24 Jun 2019 13:36:03 +0900 [thread overview]
Message-ID: <20190624043613.19271-4-yasufum.o@gmail.com> (raw)
In-Reply-To: <20190624043613.19271-1-yasufum.o@gmail.com>
From: Yasufumi Ogawa <yasufum.o@gmail.com>
This update is to move JSON formatter functions start with `append_json`
which have principle features, such as appending int, uint or so, to
separate other formatters have prefix `append_` but not principle
features. Added files are `json_helper.c` and json_helper.h` for the
moved functions.
Signed-off-by: Yasufumi Ogawa <yasufum.o@gmail.com>
---
src/mirror/Makefile | 1 +
.../secondary/spp_worker_th/cmd_runner.c | 127 +-----------------
.../secondary/spp_worker_th/json_helper.c | 125 +++++++++++++++++
.../secondary/spp_worker_th/json_helper.h | 38 ++++++
.../secondary/spp_worker_th/string_buffer.h | 2 +
src/vf/Makefile | 1 +
6 files changed, 168 insertions(+), 126 deletions(-)
create mode 100644 src/shared/secondary/spp_worker_th/json_helper.c
create mode 100644 src/shared/secondary/spp_worker_th/json_helper.h
diff --git a/src/mirror/Makefile b/src/mirror/Makefile
index 3e31cfa..6b6b9b9 100644
--- a/src/mirror/Makefile
+++ b/src/mirror/Makefile
@@ -22,6 +22,7 @@ SRCS-y += $(SPP_WKT_DIR)/spp_port.c
SRCS-y += $(SPP_WKT_DIR)/conn_spp_ctl.c
SRCS-y += $(SPP_WKT_DIR)/cmd_parser.c
SRCS-y += $(SPP_WKT_DIR)/cmd_runner.c
+SRCS-y += $(SPP_WKT_DIR)/json_helper.c
SRCS-y += $(SPP_WKT_DIR)/string_buffer.c
SRCS-y += $(SPP_WKT_DIR)/ringlatencystats.c
diff --git a/src/shared/secondary/spp_worker_th/cmd_runner.c b/src/shared/secondary/spp_worker_th/cmd_runner.c
index 4c4abd8..423774b 100644
--- a/src/shared/secondary/spp_worker_th/cmd_runner.c
+++ b/src/shared/secondary/spp_worker_th/cmd_runner.c
@@ -13,6 +13,7 @@
#include "spp_port.h"
#include "string_buffer.h"
+#include "json_helper.h"
#include "conn_spp_ctl.h"
#include "cmd_parser.h"
#include "cmd_runner.h"
@@ -21,17 +22,9 @@
/* request message initial size */
#define CMD_ERR_MSG_LEN 128
-#define CMD_TAG_APPEND_SIZE 16
#define CMD_REQ_BUF_INIT_SIZE 2048
#define CMD_RES_BUF_INIT_SIZE 2048
-/* TODO(yasufum) confirm why using "" for the alternative of comma? */
-#define JSON_APPEND_COMMA(flg) ((flg)?", ":"")
-#define JSON_APPEND_VALUE(format) "%s\"%s\": "format
-#define JSON_APPEND_ARRAY "%s\"%s\": [ %s ]"
-#define JSON_APPEND_BLOCK "%s\"%s\": { %s }"
-#define JSON_APPEND_BLOCK_NONAME "%s%s{ %s }"
-
enum cmd_res_code {
CMD_SUCCESS = 0,
CMD_FAILED,
@@ -597,124 +590,6 @@ sppwk_get_ethdev_port_id(enum port_type iface_type, int iface_no)
}
}
-/* append a comma for JSON format */
-static int
-append_json_comma(char **output)
-{
- *output = spp_strbuf_append(*output, ", ", strlen(", "));
- if (unlikely(*output == NULL)) {
- RTE_LOG(ERR, WK_CMD_RUNNER,
- "JSON's comma failed to add.\n");
- return SPP_RET_NG;
- }
-
- return SPP_RET_OK;
-}
-
-/* append data of unsigned integral type for JSON format */
-static int
-append_json_uint_value(const char *name, char **output, unsigned int value)
-{
- int len = strlen(*output);
- /* extend the buffer */
- *output = spp_strbuf_append(*output, "",
- strlen(name) + CMD_TAG_APPEND_SIZE*2);
- if (unlikely(*output == NULL)) {
- RTE_LOG(ERR, WK_CMD_RUNNER,
- "JSON's numeric format failed to add. "
- "(name = %s, uint = %u)\n", name, value);
- return SPP_RET_NG;
- }
-
- sprintf(&(*output)[len], JSON_APPEND_VALUE("%u"),
- JSON_APPEND_COMMA(len), name, value);
- return SPP_RET_OK;
-}
-
-/* append data of integral type for JSON format */
-static int
-append_json_int_value(const char *name, char **output, int value)
-{
- int len = strlen(*output);
- /* extend the buffer */
- *output = spp_strbuf_append(*output, "",
- strlen(name) + CMD_TAG_APPEND_SIZE*2);
- if (unlikely(*output == NULL)) {
- RTE_LOG(ERR, WK_CMD_RUNNER,
- "JSON's numeric format failed to add. "
- "(name = %s, int = %d)\n", name, value);
- return SPP_RET_NG;
- }
-
- sprintf(&(*output)[len], JSON_APPEND_VALUE("%d"),
- JSON_APPEND_COMMA(len), name, value);
- return SPP_RET_OK;
-}
-
-/* append data of string type for JSON format */
-static int
-append_json_str_value(const char *name, char **output, const char *str)
-{
- int len = strlen(*output);
- /* extend the buffer */
- *output = spp_strbuf_append(*output, "",
- strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
- if (unlikely(*output == NULL)) {
- RTE_LOG(ERR, WK_CMD_RUNNER,
- "JSON's string format failed to add. "
- "(name = %s, str = %s)\n", name, str);
- return SPP_RET_NG;
- }
-
- sprintf(&(*output)[len], JSON_APPEND_VALUE("\"%s\""),
- JSON_APPEND_COMMA(len), name, str);
- return SPP_RET_OK;
-}
-
-/* append brackets of the array for JSON format */
-static int
-append_json_array_brackets(const char *name, char **output, const char *str)
-{
- int len = strlen(*output);
- /* extend the buffer */
- *output = spp_strbuf_append(*output, "",
- strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
- if (unlikely(*output == NULL)) {
- RTE_LOG(ERR, WK_CMD_RUNNER,
- "JSON's square bracket failed to add. "
- "(name = %s, str = %s)\n", name, str);
- return SPP_RET_NG;
- }
-
- sprintf(&(*output)[len], JSON_APPEND_ARRAY,
- JSON_APPEND_COMMA(len), name, str);
- return SPP_RET_OK;
-}
-
-/* append brackets of the blocks for JSON format */
-static int
-append_json_block_brackets(const char *name, char **output, const char *str)
-{
- int len = strlen(*output);
- /* extend the buffer */
- *output = spp_strbuf_append(*output, "",
- strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
- if (unlikely(*output == NULL)) {
- RTE_LOG(ERR, WK_CMD_RUNNER,
- "JSON's curly bracket failed to add. "
- "(name = %s, str = %s)\n", name, str);
- return SPP_RET_NG;
- }
-
- if (name[0] == '\0')
- sprintf(&(*output)[len], JSON_APPEND_BLOCK_NONAME,
- JSON_APPEND_COMMA(len), name, str);
- else
- sprintf(&(*output)[len], JSON_APPEND_BLOCK,
- JSON_APPEND_COMMA(len), name, str);
- return SPP_RET_OK;
-}
-
/* Execute one command. */
static int
exec_one_cmd(const struct sppwk_cmd_attrs *cmd)
diff --git a/src/shared/secondary/spp_worker_th/json_helper.c b/src/shared/secondary/spp_worker_th/json_helper.c
new file mode 100644
index 0000000..4c1baa3
--- /dev/null
+++ b/src/shared/secondary/spp_worker_th/json_helper.c
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2019 Nippon Telegraph and Telephone Corporation
+ */
+
+#include "string_buffer.h"
+#include "json_helper.h"
+
+#define RTE_LOGTYPE_WK_JSON_HELPER RTE_LOGTYPE_USER1
+
+int
+append_json_comma(char **output)
+{
+ *output = spp_strbuf_append(*output, ", ", strlen(", "));
+ if (unlikely(*output == NULL)) {
+ RTE_LOG(ERR, WK_JSON_HELPER,
+ "JSON's comma failed to add.\n");
+ return SPP_RET_NG;
+ }
+
+ return SPP_RET_OK;
+}
+
+/* append data of unsigned integral type for JSON format */
+int
+append_json_uint_value(const char *name, char **output, unsigned int value)
+{
+ int len = strlen(*output);
+ /* extend the buffer */
+ *output = spp_strbuf_append(*output, "",
+ strlen(name) + CMD_TAG_APPEND_SIZE*2);
+ if (unlikely(*output == NULL)) {
+ RTE_LOG(ERR, WK_JSON_HELPER,
+ "JSON's numeric format failed to add. "
+ "(name = %s, uint = %u)\n", name, value);
+ return SPP_RET_NG;
+ }
+
+ sprintf(&(*output)[len], JSON_APPEND_VALUE("%u"),
+ JSON_APPEND_COMMA(len), name, value);
+ return SPP_RET_OK;
+}
+
+/* append data of integral type for JSON format */
+int
+append_json_int_value(const char *name, char **output, int value)
+{
+ int len = strlen(*output);
+ /* extend the buffer */
+ *output = spp_strbuf_append(*output, "",
+ strlen(name) + CMD_TAG_APPEND_SIZE*2);
+ if (unlikely(*output == NULL)) {
+ RTE_LOG(ERR, WK_JSON_HELPER,
+ "JSON's numeric format failed to add. "
+ "(name = %s, int = %d)\n", name, value);
+ return SPP_RET_NG;
+ }
+
+ sprintf(&(*output)[len], JSON_APPEND_VALUE("%d"),
+ JSON_APPEND_COMMA(len), name, value);
+ return SPP_RET_OK;
+}
+
+/* append data of string type for JSON format */
+int
+append_json_str_value(const char *name, char **output, const char *str)
+{
+ int len = strlen(*output);
+ /* extend the buffer */
+ *output = spp_strbuf_append(*output, "",
+ strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
+ if (unlikely(*output == NULL)) {
+ RTE_LOG(ERR, WK_JSON_HELPER,
+ "JSON's string format failed to add. "
+ "(name = %s, str = %s)\n", name, str);
+ return SPP_RET_NG;
+ }
+
+ sprintf(&(*output)[len], JSON_APPEND_VALUE("\"%s\""),
+ JSON_APPEND_COMMA(len), name, str);
+ return SPP_RET_OK;
+}
+
+/* append brackets of the array for JSON format */
+int
+append_json_array_brackets(const char *name, char **output, const char *str)
+{
+ int len = strlen(*output);
+ /* extend the buffer */
+ *output = spp_strbuf_append(*output, "",
+ strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
+ if (unlikely(*output == NULL)) {
+ RTE_LOG(ERR, WK_JSON_HELPER,
+ "JSON's square bracket failed to add. "
+ "(name = %s, str = %s)\n", name, str);
+ return SPP_RET_NG;
+ }
+
+ sprintf(&(*output)[len], JSON_APPEND_ARRAY,
+ JSON_APPEND_COMMA(len), name, str);
+ return SPP_RET_OK;
+}
+
+/* append brackets of the blocks for JSON format */
+int
+append_json_block_brackets(const char *name, char **output, const char *str)
+{
+ int len = strlen(*output);
+ /* extend the buffer */
+ *output = spp_strbuf_append(*output, "",
+ strlen(name) + strlen(str) + CMD_TAG_APPEND_SIZE);
+ if (unlikely(*output == NULL)) {
+ RTE_LOG(ERR, WK_JSON_HELPER,
+ "JSON's curly bracket failed to add. "
+ "(name = %s, str = %s)\n", name, str);
+ return SPP_RET_NG;
+ }
+
+ if (name[0] == '\0')
+ sprintf(&(*output)[len], JSON_APPEND_BLOCK_NONAME,
+ JSON_APPEND_COMMA(len), name, str);
+ else
+ sprintf(&(*output)[len], JSON_APPEND_BLOCK,
+ JSON_APPEND_COMMA(len), name, str);
+ return SPP_RET_OK;
+}
diff --git a/src/shared/secondary/spp_worker_th/json_helper.h b/src/shared/secondary/spp_worker_th/json_helper.h
new file mode 100644
index 0000000..f286404
--- /dev/null
+++ b/src/shared/secondary/spp_worker_th/json_helper.h
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2019 Nippon Telegraph and Telephone Corporation
+ */
+
+#ifndef _SPPWK_JSON_HELPER_H_
+#define _SPPWK_JSON_HELPER_H_
+
+#include "cmd_utils.h"
+
+#define CMD_TAG_APPEND_SIZE 16
+
+#define JSON_APPEND_COMMA(flg) ((flg)?", ":"")
+
+#define JSON_APPEND_VALUE(format) "%s\"%s\": "format
+
+#define JSON_APPEND_ARRAY "%s\"%s\": [ %s ]"
+
+#define JSON_APPEND_BLOCK_NONAME "%s%s{ %s }"
+#define JSON_APPEND_BLOCK "%s\"%s\": { %s }"
+
+int append_json_comma(char **output);
+
+int append_json_uint_value(const char *name, char **output,
+ unsigned int value);
+
+int append_json_int_value(const char *name, char **output,
+ int value);
+
+int append_json_str_value(const char *name, char **output,
+ const char *str);
+
+int append_json_array_brackets(const char *name, char **output,
+ const char *str);
+
+int append_json_block_brackets(const char *name, char **output,
+ const char *str);
+
+#endif
diff --git a/src/shared/secondary/spp_worker_th/string_buffer.h b/src/shared/secondary/spp_worker_th/string_buffer.h
index 34ee6cb..951f0ae 100644
--- a/src/shared/secondary/spp_worker_th/string_buffer.h
+++ b/src/shared/secondary/spp_worker_th/string_buffer.h
@@ -5,6 +5,8 @@
#ifndef _STRING_BUFFER_H_
#define _STRING_BUFFER_H_
+#include <stdlib.h>
+
/**
* @file
* SPP String buffer management
diff --git a/src/vf/Makefile b/src/vf/Makefile
index faf72ee..ca8d2b3 100644
--- a/src/vf/Makefile
+++ b/src/vf/Makefile
@@ -22,6 +22,7 @@ SRCS-y += $(SPP_WKT_DIR)/conn_spp_ctl.c
SRCS-y += $(SPP_WKT_DIR)/cmd_parser.c
SRCS-y += $(SPP_WKT_DIR)/cmd_runner.c
SRCS-y += $(SPP_WKT_DIR)/cmd_utils.c
+SRCS-y += $(SPP_WKT_DIR)/json_helper.c
SRCS-y += ../shared/common.c
SRCS-y += ../shared/secondary/utils.c ../shared/secondary/add_port.c
--
2.17.1
next prev parent reply other threads:[~2019-06-24 4:36 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-06-24 4:36 [spp] [PATCH 00/13] Move JSON utils from libs for running cmds yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 01/13] shared/sec: rename ops for setup cmd response yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 02/13] shared/sec: rename functions for spp_mirror yasufum.o
2019-06-24 4:36 ` yasufum.o [this message]
2019-06-24 4:36 ` [spp] [PATCH 04/13] shared/sec: change order of args of JSON fmtters yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 05/13] shared/sec: move JSON formatter to shard/secondary yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 06/13] shared/sec: revise including headers yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 07/13] shared/sec: move JSON formatters from cmd_runner yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 08/13] shared/sec: move rest of JSON formatters yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 09/13] shared/sec: move lcore funcs in response_info_list yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 10/13] shared/sec: move ope cli-id " yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 11/13] shared/sec: move rest of ops " yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 12/13] shared/sec: remove local funcs from header yasufum.o
2019-06-24 4:36 ` [spp] [PATCH 13/13] shared/sec: refactor comments for JSON formatter yasufum.o
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=20190624043613.19271-4-yasufum.o@gmail.com \
--to=yasufum.o@gmail.com \
--cc=ferruh.yigit@intel.com \
--cc=spp@dpdk.org \
/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).