2019-06-05 19:19:41

by Krzesimir Nowak

[permalink] [raw]
Subject: [BPF v1] tools: bpftool: Fix JSON output when lookup fails

In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros
into functions") one case of error reporting was special cased, so it
could report a lookup error for a specific key when dumping the map
element. What the code forgot to do is to wrap the key and value keys
into a JSON object, so an example output of pretty JSON dump of a
sockhash map (which does not support looking up its values) is:

[
"key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
],
"value": {
"error": "Operation not supported"
},
"key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
],
"value": {
"error": "Operation not supported"
}
]

Note the key-value pairs inside the toplevel array. They should be
wrapped inside a JSON object, otherwise it is an invalid JSON. This
commit fixes this, so the output now is:

[{
"key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
],
"value": {
"error": "Operation not supported"
}
},{
"key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
],
"value": {
"error": "Operation not supported"
}
}
]

Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions")
Cc: Quentin Monnet <[email protected]>
Signed-off-by: Krzesimir Nowak <[email protected]>
---
tools/bpf/bpftool/map.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 3ec82904ccec..5da5a7311f13 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -716,12 +716,14 @@ static int dump_map_elem(int fd, void *key, void *value,
return 0;

if (json_output) {
+ jsonw_start_object(json_wtr);
jsonw_name(json_wtr, "key");
print_hex_data_json(key, map_info->key_size);
jsonw_name(json_wtr, "value");
jsonw_start_object(json_wtr);
jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
jsonw_end_object(json_wtr);
+ jsonw_end_object(json_wtr);
} else {
const char *msg = NULL;

--
2.20.1


2019-06-05 19:40:37

by Andrii Nakryiko

[permalink] [raw]
Subject: Re: [BPF v1] tools: bpftool: Fix JSON output when lookup fails

On Wed, Jun 5, 2019 at 12:18 PM Krzesimir Nowak <[email protected]> wrote:
>
> In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros
> into functions") one case of error reporting was special cased, so it
> could report a lookup error for a specific key when dumping the map
> element. What the code forgot to do is to wrap the key and value keys
> into a JSON object, so an example output of pretty JSON dump of a
> sockhash map (which does not support looking up its values) is:
>
> [
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
> ],
> "value": {
> "error": "Operation not supported"
> },
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
> ],
> "value": {
> "error": "Operation not supported"
> }
> ]
>
> Note the key-value pairs inside the toplevel array. They should be
> wrapped inside a JSON object, otherwise it is an invalid JSON. This
> commit fixes this, so the output now is:
>
> [{
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
> ],
> "value": {
> "error": "Operation not supported"
> }
> },{
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
> ],
> "value": {
> "error": "Operation not supported"
> }
> }
> ]
>
> Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions")
> Cc: Quentin Monnet <[email protected]>
> Signed-off-by: Krzesimir Nowak <[email protected]>
> ---

Acked-by: Andrii Nakryiko <[email protected]>

> tools/bpf/bpftool/map.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
> index 3ec82904ccec..5da5a7311f13 100644
> --- a/tools/bpf/bpftool/map.c
> +++ b/tools/bpf/bpftool/map.c
> @@ -716,12 +716,14 @@ static int dump_map_elem(int fd, void *key, void *value,
> return 0;
>
> if (json_output) {
> + jsonw_start_object(json_wtr);
> jsonw_name(json_wtr, "key");
> print_hex_data_json(key, map_info->key_size);
> jsonw_name(json_wtr, "value");
> jsonw_start_object(json_wtr);
> jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
> jsonw_end_object(json_wtr);
> + jsonw_end_object(json_wtr);
> } else {
> const char *msg = NULL;
>
> --
> 2.20.1
>

2019-06-05 20:13:48

by Daniel Borkmann

[permalink] [raw]
Subject: Re: [BPF v1] tools: bpftool: Fix JSON output when lookup fails

On 06/05/2019 09:17 PM, Krzesimir Nowak wrote:
> In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros
> into functions") one case of error reporting was special cased, so it
> could report a lookup error for a specific key when dumping the map
> element. What the code forgot to do is to wrap the key and value keys
> into a JSON object, so an example output of pretty JSON dump of a
> sockhash map (which does not support looking up its values) is:
>
> [
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
> ],
> "value": {
> "error": "Operation not supported"
> },
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
> ],
> "value": {
> "error": "Operation not supported"
> }
> ]
>
> Note the key-value pairs inside the toplevel array. They should be
> wrapped inside a JSON object, otherwise it is an invalid JSON. This
> commit fixes this, so the output now is:
>
> [{
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
> ],
> "value": {
> "error": "Operation not supported"
> }
> },{
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
> ],
> "value": {
> "error": "Operation not supported"
> }
> }
> ]
>
> Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions")
> Cc: Quentin Monnet <[email protected]>
> Signed-off-by: Krzesimir Nowak <[email protected]>

Applied, thanks!

2019-06-06 08:37:45

by Quentin Monnet

[permalink] [raw]
Subject: Re: [BPF v1] tools: bpftool: Fix JSON output when lookup fails

2019-06-05 21:17 UTC+0200 ~ Krzesimir Nowak <[email protected]>
> In commit 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros
> into functions") one case of error reporting was special cased, so it
> could report a lookup error for a specific key when dumping the map
> element. What the code forgot to do is to wrap the key and value keys
> into a JSON object, so an example output of pretty JSON dump of a
> sockhash map (which does not support looking up its values) is:
>
> [
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
> ],
> "value": {
> "error": "Operation not supported"
> },
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
> ],
> "value": {
> "error": "Operation not supported"
> }
> ]
>
> Note the key-value pairs inside the toplevel array. They should be
> wrapped inside a JSON object, otherwise it is an invalid JSON. This
> commit fixes this, so the output now is:
>
> [{
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x00"
> ],
> "value": {
> "error": "Operation not supported"
> }
> },{
> "key": ["0x0a","0x41","0x00","0x02","0x1f","0x78","0x00","0x01"
> ],
> "value": {
> "error": "Operation not supported"
> }
> }
> ]
>
> Fixes: 9a5ab8bf1d6d ("tools: bpftool: turn err() and info() macros into functions")
> Cc: Quentin Monnet <[email protected]>
> Signed-off-by: Krzesimir Nowak <[email protected]>

Thanks for the fix!

Quentin