Skip to contentSkip to conversation

bpftool: Fix some json formatting for struct_ops

PATCH Applied Manu Bretelle 4 participants 6 messages Oct 18, 2023 – Oct 19, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover bpftool: Fix some json formatting for struct_ops

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(-)
Signature · 1 line
2.39.3
#1 lore raw

PATCH 1/2 bpftool: fix printing of pointer value

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)"
```
---
 tools/bpf/bpftool/btf_dumper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/bpf/bpftool/btf_dumper.c +1 -1
index 1b7f69714604..527fe867a8fb 100644
@@ -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);
 }
Signature · 1 line
2.39.3
#2 lore raw

PATCH 2/2 bpftool: wrap struct_ops dump in an array

In reply to Manu Bretelle

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
```
---
 tools/bpf/bpftool/struct_ops.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/bpf/bpftool/struct_ops.c +6 -0
index 3ebc9fe91e0e..d573f2640d8e 100644
@@ -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);
Signature · 1 line
2.39.3
maintainer Acked-by on the v1 series
#3 lore raw
In reply to Manu Bretelle
Quoted 6 lines · Manu Bretelle

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.

Hi Manu,

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

Thanks,
Eduard

Quoted 8 lines

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 on the v1 series
#4 lore raw
In reply to Manu Bretelle
Quoted 7 lines · Manu Bretelle

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.

Quoted 8 lines

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

Thanks a lot for these fixes!

#5 lore raw
In reply to Manu Bretelle
This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Daniel Borkmann <[email protected]>:

Quoted 8 lines · 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!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html