bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops
@ 2023-10-18 23:01 Manu Bretelle
  2023-10-18 23:01 ` [PATCH bpf-next 1/2] bpftool: fix printing of pointer value Manu Bretelle
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Manu Bretelle @ 2023-10-18 23:01 UTC (permalink / raw)
  To: bpf, quentin, andrii, daniel, ast, martin.lau, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa

When dumping struct_ops with bpftool, the json produced was invalid.
1) pointer values where not printed with surrounding quotes, causing an
invalid json integer to be emitted
2) when bpftool struct_ops dump id <id>, the 2 dictionaries were not
wrapped in a array, here also causing an invalid json payload to be
emitted. 

Manu Bretelle (2):
  bpftool: fix printing of pointer value
  bpftool: wrap struct_ops dump in an array

 tools/bpf/bpftool/btf_dumper.c | 2 +-
 tools/bpf/bpftool/struct_ops.c | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

-- 
2.39.3


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 1/2] bpftool: fix printing of pointer value
  2023-10-18 23:01 [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Manu Bretelle
@ 2023-10-18 23:01 ` Manu Bretelle
  2023-10-18 23:01 ` [PATCH bpf-next 2/2] bpftool: wrap struct_ops dump in an array Manu Bretelle
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Manu Bretelle @ 2023-10-18 23:01 UTC (permalink / raw)
  To: bpf, quentin, andrii, daniel, ast, martin.lau, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa

When printing a pointer value, "%p" will either print the hexadecimal
value of the pointer (e.g `0x1234`), or `(nil)` when NULL.

Both of those are invalid json "integer" values and need to be wrapped
in quotes.

Before:
```
$ sudo bpftool struct_ops dump  name ned_dummy_cca | grep next
                    "next": (nil),
$ sudo bpftool struct_ops dump  name ned_dummy_cca | \
    jq '.[1].bpf_struct_ops_tcp_congestion_ops.data.list.next'
parse error: Invalid numeric literal at line 29, column 34
```

After:
```
$ sudo ./bpftool struct_ops dump  name ned_dummy_cca | grep next
                    "next": "(nil)",
$ sudo ./bpftool struct_ops dump  name ned_dummy_cca | \
    jq '.[1].bpf_struct_ops_tcp_congestion_ops.data.list.next'
"(nil)"
```

Signed-off-by: Manu Bretelle <chantr4@gmail.com>
---
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/bpf/bpftool/btf_dumper.c b/tools/bpf/bpftool/btf_dumper.c
index 1b7f69714604..527fe867a8fb 100644
--- a/tools/bpf/bpftool/btf_dumper.c
+++ b/tools/bpf/bpftool/btf_dumper.c
@@ -127,7 +127,7 @@ static void btf_dumper_ptr(const struct btf_dumper *d,
 
 print_ptr_value:
 	if (d->is_plain_text)
-		jsonw_printf(d->jw, "%p", (void *)value);
+		jsonw_printf(d->jw, "\"%p\"", (void *)value);
 	else
 		jsonw_printf(d->jw, "%lu", value);
 }
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH bpf-next 2/2] bpftool: wrap struct_ops dump in an array
  2023-10-18 23:01 [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Manu Bretelle
  2023-10-18 23:01 ` [PATCH bpf-next 1/2] bpftool: fix printing of pointer value Manu Bretelle
@ 2023-10-18 23:01 ` Manu Bretelle
  2023-10-19 12:56 ` [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Eduard Zingerman
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Manu Bretelle @ 2023-10-18 23:01 UTC (permalink / raw)
  To: bpf, quentin, andrii, daniel, ast, martin.lau, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa

When dumping a struct_ops, 2 dictionaries are emitted.

When using `name`, they were already wrapped in an array, but not when
using `id`. Causing `jq` to fail at parsing the payload as it reached
the comma following the first dict.

This change wraps those dictionaries in an array so valid json is
emitted.

Before, jq fails to parse the output:
```
 $ sudo bpftool struct_ops dump id 1523612 | jq . > /dev/null
parse error: Expected value before ',' at line 19, column 2
```

After, no error parsing the output:
```
sudo ./bpftool  struct_ops dump id 1523612 | jq . > /dev/null
```

Signed-off-by: Manu Bretelle <chantr4@gmail.com>
---
 tools/bpf/bpftool/struct_ops.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/bpf/bpftool/struct_ops.c b/tools/bpf/bpftool/struct_ops.c
index 3ebc9fe91e0e..d573f2640d8e 100644
--- a/tools/bpf/bpftool/struct_ops.c
+++ b/tools/bpf/bpftool/struct_ops.c
@@ -276,6 +276,9 @@ static struct res do_one_id(const char *id_str, work_func func, void *data,
 
 	res.nr_maps++;
 
+	if (wtr)
+		jsonw_start_array(wtr);
+
 	if (func(fd, info, data, wtr))
 		res.nr_errs++;
 	else if (!wtr && json_output)
@@ -288,6 +291,9 @@ static struct res do_one_id(const char *id_str, work_func func, void *data,
 		 */
 		jsonw_null(json_wtr);
 
+	if (wtr)
+		jsonw_end_array(wtr);
+
 done:
 	free(info);
 	close(fd);
-- 
2.39.3


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops
  2023-10-18 23:01 [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Manu Bretelle
  2023-10-18 23:01 ` [PATCH bpf-next 1/2] bpftool: fix printing of pointer value Manu Bretelle
  2023-10-18 23:01 ` [PATCH bpf-next 2/2] bpftool: wrap struct_ops dump in an array Manu Bretelle
@ 2023-10-19 12:56 ` Eduard Zingerman
  2023-10-19 14:28 ` Quentin Monnet
  2023-10-19 14:40 ` patchwork-bot+netdevbpf
  4 siblings, 0 replies; 6+ messages in thread
From: Eduard Zingerman @ 2023-10-19 12:56 UTC (permalink / raw)
  To: Manu Bretelle, bpf, quentin, andrii, daniel, ast, martin.lau,
	song, john.fastabend, kpsingh, sdf, haoluo, jolsa

On Wed, 2023-10-18 at 16:01 -0700, Manu Bretelle wrote:
> When dumping struct_ops with bpftool, the json produced was invalid.
> 1) pointer values where not printed with surrounding quotes, causing an
> invalid json integer to be emitted
> 2) when bpftool struct_ops dump id <id>, the 2 dictionaries were not
> wrapped in a array, here also causing an invalid json payload to be
> emitted. 

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

Hi Manu,

I've tested this patch-set and everything seems to work as expected.

Thanks,
Eduard

> Manu Bretelle (2):
>   bpftool: fix printing of pointer value
>   bpftool: wrap struct_ops dump in an array
> 
>  tools/bpf/bpftool/btf_dumper.c | 2 +-
>  tools/bpf/bpftool/struct_ops.c | 6 ++++++
>  2 files changed, 7 insertions(+), 1 deletion(-)
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops
  2023-10-18 23:01 [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Manu Bretelle
                   ` (2 preceding siblings ...)
  2023-10-19 12:56 ` [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Eduard Zingerman
@ 2023-10-19 14:28 ` Quentin Monnet
  2023-10-19 14:40 ` patchwork-bot+netdevbpf
  4 siblings, 0 replies; 6+ messages in thread
From: Quentin Monnet @ 2023-10-19 14:28 UTC (permalink / raw)
  To: Manu Bretelle, bpf, andrii, daniel, ast, martin.lau, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa

On 19/10/2023 00:01, Manu Bretelle wrote:
> When dumping struct_ops with bpftool, the json produced was invalid.
> 1) pointer values where not printed with surrounding quotes, causing an
> invalid json integer to be emitted
> 2) when bpftool struct_ops dump id <id>, the 2 dictionaries were not
> wrapped in a array, here also causing an invalid json payload to be
> emitted. 
> 
> Manu Bretelle (2):
>   bpftool: fix printing of pointer value
>   bpftool: wrap struct_ops dump in an array
> 
>  tools/bpf/bpftool/btf_dumper.c | 2 +-
>  tools/bpf/bpftool/struct_ops.c | 6 ++++++
>  2 files changed, 7 insertions(+), 1 deletion(-)
> 

Acked-by: Quentin Monnet <quentin@isovalent.com>

Thanks a lot for these fixes!

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops
  2023-10-18 23:01 [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Manu Bretelle
                   ` (3 preceding siblings ...)
  2023-10-19 14:28 ` Quentin Monnet
@ 2023-10-19 14:40 ` patchwork-bot+netdevbpf
  4 siblings, 0 replies; 6+ messages in thread
From: patchwork-bot+netdevbpf @ 2023-10-19 14:40 UTC (permalink / raw)
  To: Manu Bretelle
  Cc: bpf, quentin, andrii, daniel, ast, martin.lau, song,
	john.fastabend, kpsingh, sdf, haoluo, jolsa

Hello:

This series was applied to bpf/bpf-next.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Wed, 18 Oct 2023 16:01:31 -0700 you wrote:
> When dumping struct_ops with bpftool, the json produced was invalid.
> 1) pointer values where not printed with surrounding quotes, causing an
> invalid json integer to be emitted
> 2) when bpftool struct_ops dump id <id>, the 2 dictionaries were not
> wrapped in a array, here also causing an invalid json payload to be
> emitted.
> 
> [...]

Here is the summary with links:
  - [bpf-next,1/2] bpftool: fix printing of pointer value
    https://git.kernel.org/bpf/bpf-next/c/90704b4be0b0
  - [bpf-next,2/2] bpftool: wrap struct_ops dump in an array
    https://git.kernel.org/bpf/bpf-next/c/6bd5e167af2e

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-10-19 14:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-18 23:01 [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Manu Bretelle
2023-10-18 23:01 ` [PATCH bpf-next 1/2] bpftool: fix printing of pointer value Manu Bretelle
2023-10-18 23:01 ` [PATCH bpf-next 2/2] bpftool: wrap struct_ops dump in an array Manu Bretelle
2023-10-19 12:56 ` [PATCH bpf-next 0/2] bpftool: Fix some json formatting for struct_ops Eduard Zingerman
2023-10-19 14:28 ` Quentin Monnet
2023-10-19 14:40 ` patchwork-bot+netdevbpf

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).