DPDK patches and discussions
 help / color / mirror / Atom feed
From: "Morten Brørup" <mb@smartsharesystems.com>
To: "Chengwen Feng" <fengchengwen@huawei.com>, <thomas@monjalon.net>,
	<ferruh.yigit@xilinx.com>, <kevin.laatz@intel.com>,
	<bruce.richardson@intel.com>, <andrew.rybchenko@oktetlabs.ru>,
	<jerinj@marvell.com>, <sachin.saxena@oss.nxp.com>,
	<hemant.agrawal@nxp.com>
Cc: <dev@dpdk.org>
Subject: RE: [PATCH v2 1/5] telemetry: escape special char when tel string
Date: Fri, 17 Jun 2022 13:16:08 +0200	[thread overview]
Message-ID: <98CBD80474FA8B44BF855DF32C47DC35D8713C@smartserver.smartshare.dk> (raw)
In-Reply-To: <20220617094624.17578-2-fengchengwen@huawei.com>

> From: Chengwen Feng [mailto:fengchengwen@huawei.com]
> Sent: Friday, 17 June 2022 11.46
> 
> This patch supports escape special characters (including: \",\\,/,\b,
> /f,/n,/r,/t) when telemetry string.
> This patch is used to support telemetry xxx-dump commands which the
> string may include special characters.
> 
> Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
> ---
>  lib/telemetry/telemetry.c | 96 +++++++++++++++++++++++++++++++++++++--
>  1 file changed, 93 insertions(+), 3 deletions(-)
> 
> diff --git a/lib/telemetry/telemetry.c b/lib/telemetry/telemetry.c
> index c6fd03a5ab..0f762f633e 100644
> --- a/lib/telemetry/telemetry.c
> +++ b/lib/telemetry/telemetry.c
> @@ -215,6 +215,94 @@ container_to_json(const struct rte_tel_data *d,
> char *out_buf, size_t buf_len)
>  	return used;
>  }
> 
> +static bool
> +json_is_special_char(char ch)
> +{
> +	static unsigned char is_spec[256] = { 0 };
> +	static bool init_once;
> +
> +	if (!init_once) {
> +		is_spec['\"'] = 1;
> +		is_spec['\\'] = 1;
> +		is_spec['/'] = 1;
> +		is_spec['\b'] = 1;
> +		is_spec['\f'] = 1;
> +		is_spec['\n'] = 1;
> +		is_spec['\r'] = 1;
> +		is_spec['\t'] = 1;
> +		init_once = true;
> +	}
> +
> +	return (bool)is_spec[(unsigned char)ch];
> +}

Here's a suggestion for simplifying the code:

Remove json_is_special_char(), and update json_escape_special_char() and json_format_string() as follows:

> +
> +static size_t
> +json_escape_special_char(char *buf, const char ch)

Consider making this function inline.

> +{
> +	size_t used = 0;
> +
> +	switch (ch) {
> +	case '\"':
> +		buf[used++] = '\\';
> +		buf[used++] = '\"';
> +		break;
> +	case '\\':
> +		buf[used++] = '\\';
> +		buf[used++] = '\\';
> +		break;
> +	case '/':
> +		buf[used++] = '\\';
> +		buf[used++] = '/';
> +		break;
> +	case '\b':
> +		buf[used++] = '\\';
> +		buf[used++] = 'b';
> +		break;
> +	case '\f':
> +		buf[used++] = '\\';
> +		buf[used++] = 'f';
> +		break;
> +	case '\n':
> +		buf[used++] = '\\';
> +		buf[used++] = 'n';
> +		break;
> +	case '\r':
> +		buf[used++] = '\\';
> +		buf[used++] = 'r';
> +		break;
> +	case '\t':
> +		buf[used++] = '\\';
> +		buf[used++] = 't';
> +		break;
> +	default:

Handle non-escaped characters in the default case here instead:

+		buf[used++] = ch;

> +		break;
> +	}
> +
> +	return used;
> +}
> +
> +static size_t
> +json_format_string(char *buf, size_t len, const char *str)
> +{
> +	size_t used = 0;
> +
> +	while (*str) {
> +		if (unlikely(len < used + 2)) {
> +			TMTY_LOG(WARNING, "Insufficient buffer when json
> format string\n");
> +			break;
> +		}
> +

-- replace:
> +		if (json_is_special_char(*str))
> +			used += json_escape_special_char(buf + used, *str);
> +		else
> +			buf[used++] = *str;
> +
> +		str++;
-- by:
+		used += json_escape_special_char(buf + used, *str++);
--

End of suggestion. Feel free to use it or not. :-)

> +	}
> +
> +	return used;
> +}
> +
>  static void
>  output_json(const char *cmd, const struct rte_tel_data *d, int s)
>  {
> @@ -232,9 +320,11 @@ output_json(const char *cmd, const struct
> rte_tel_data *d, int s)
>  				MAX_CMD_LEN, cmd ? cmd : "none");
>  		break;
>  	case RTE_TEL_STRING:
> -		used = snprintf(out_buf, sizeof(out_buf),
> "{\"%.*s\":\"%.*s\"}",
> -				MAX_CMD_LEN, cmd,
> -				RTE_TEL_MAX_SINGLE_STRING_LEN, d->data.str);
> +		used = snprintf(out_buf, sizeof(out_buf), "{\"%.*s\":\"",
> +				MAX_CMD_LEN, cmd);
> +		used += json_format_string(out_buf + used,
> +				sizeof(out_buf) - used - 3, d->data.str);
> +		used += snprintf(out_buf + used, sizeof(out_buf) - used,
> "\"}");

I looked for missing 0-termination in json_format_string(), but that is not required due to the immediately following snprintf().

>  		break;
>  	case RTE_TEL_DICT:
>  		prefix_used = snprintf(out_buf, sizeof(out_buf),
> "{\"%.*s\":",
> --
> 2.33.0
> 

If you want to make it generic, these four cases in output_json() also need to JSON encode the strings:

case RTE_TEL_DICT:
	case RTE_TEL_STRING_VAL: ...
	case RTE_TEL_CONTAINER: ... (strings only)

case RTE_TEL_ARRAY_STRING:
	if (d->type == RTE_TEL_ARRAY_STRING) ...
	else if (d->type == RTE_TEL_ARRAY_CONTAINER) ... (strings only)


However, JSON encoding of strings inside arrays and containers is not required for the dump purposes addressed by this patch series, so I consider this patch complete without it. No need to add "feature creep" to this series.


Reviewed-by: Morten Brørup <mb@smartsharesystems.com>


  reply	other threads:[~2022-06-17 11:16 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15  7:39 [PATCH 0/5] support telemetry dump dev Chengwen Feng
2022-06-15  7:39 ` [PATCH 1/5] usertools: use non-strict when json-loads in telemetry Chengwen Feng
2022-06-15 13:54   ` Morten Brørup
2022-06-15 16:54     ` Bruce Richardson
2022-06-15 18:01       ` Morten Brørup
2022-06-15 20:09       ` Morten Brørup
2022-06-15  7:39 ` [PATCH 2/5] dmadev: support telemetry dump dmadev Chengwen Feng
2022-06-15  7:39 ` [PATCH 3/5] eventdev: support telemetry dump eventdev Chengwen Feng
2022-06-15  7:39 ` [PATCH 4/5] rawdev: support telemetry dump rawdev Chengwen Feng
2022-06-15  7:39 ` [PATCH 5/5] ethdev: support telemetry private dump Chengwen Feng
2022-06-15 20:15 ` [PATCH 0/5] support telemetry dump dev Morten Brørup
2022-06-16  8:19   ` Bruce Richardson
2022-06-16  9:00     ` Morten Brørup
2022-06-17  9:46 ` [PATCH v2 0/5] support telemetry dump Chengwen Feng
2022-06-17  9:46   ` [PATCH v2 1/5] telemetry: escape special char when tel string Chengwen Feng
2022-06-17 11:16     ` Morten Brørup [this message]
2022-06-17 11:25       ` Bruce Richardson
2022-06-17 17:05         ` Stephen Hemminger
2022-06-18  3:52           ` fengchengwen
2022-06-18  9:59             ` Morten Brørup
2022-06-22  7:57               ` Power, Ciara
2022-06-22  9:19                 ` Bruce Richardson
2022-06-23 16:45                   ` Bruce Richardson
2022-06-17 11:27     ` Bruce Richardson
2022-06-17  9:46   ` [PATCH v2 2/5] dmadev: support telemetry dump dmadev Chengwen Feng
2022-06-17  9:46   ` [PATCH v2 3/5] eventdev: support telemetry dump eventdev Chengwen Feng
2022-06-17  9:46   ` [PATCH v2 4/5] rawdev: support telemetry dump rawdev Chengwen Feng
2022-06-17  9:46   ` [PATCH v2 5/5] ethdev: support telemetry private dump Chengwen Feng
2022-09-13  2:44 ` [PATCH v3 0/4] support telemetry dump dev Chengwen Feng
2022-09-13  2:44   ` [PATCH v3 1/4] dmadev: support telemetry dump dmadev Chengwen Feng
2022-09-13  2:44   ` [PATCH v3 2/4] eventdev: support telemetry dump eventdev Chengwen Feng
2022-09-13  2:44   ` [PATCH v3 3/4] rawdev: support telemetry dump rawdev Chengwen Feng
2022-09-13  2:44   ` [PATCH v3 4/4] ethdev: support telemetry private dump Chengwen Feng
2022-09-13  7:13 ` [PATCH v4 0/4] support telemetry dump dev Chengwen Feng
2022-09-13  7:13   ` [PATCH v4 1/4] dmadev: support telemetry dump dmadev Chengwen Feng
2022-09-13  7:13   ` [PATCH v4 2/4] eventdev: support telemetry dump eventdev Chengwen Feng
2022-09-13  7:13   ` [PATCH v4 3/4] rawdev: support telemetry dump rawdev Chengwen Feng
2022-09-13  7:13   ` [PATCH v4 4/4] ethdev: support telemetry private dump Chengwen Feng
2022-10-03  7:28   ` [PATCH v4 0/4] support telemetry dump dev David Marchand

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=98CBD80474FA8B44BF855DF32C47DC35D8713C@smartserver.smartshare.dk \
    --to=mb@smartsharesystems.com \
    --cc=andrew.rybchenko@oktetlabs.ru \
    --cc=bruce.richardson@intel.com \
    --cc=dev@dpdk.org \
    --cc=fengchengwen@huawei.com \
    --cc=ferruh.yigit@xilinx.com \
    --cc=hemant.agrawal@nxp.com \
    --cc=jerinj@marvell.com \
    --cc=kevin.laatz@intel.com \
    --cc=sachin.saxena@oss.nxp.com \
    --cc=thomas@monjalon.net \
    /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).