Patchwork [bpf-next,4/6] tools/bpf: bpftool, print strerror when map lookup error occurs

login
register
mail settings
Submitter Prashant Bhole
Date Oct. 9, 2018, 1:04 a.m.
Message ID <20181009010454.6652-5-bhole_prashant_q7@lab.ntt.co.jp>
Download mbox | patch
Permalink /patch/630907/
State New
Headers show

Comments

Prashant Bhole - Oct. 9, 2018, 1:04 a.m.
Since map lookup error can be ENOENT or EOPNOTSUPP, let's print
strerror() as error message in normal and JSON output.

This patch adds helper function print_entry_error() to print
entry from lookup error occurs

Example: Following example dumps a map which does not support lookup.

Output before:
root# bpftool map -jp dump id 40
[
    "key": ["0x0a","0x00","0x00","0x00"
    ],
    "value": {
        "error": "can\'t lookup element"
    },
    "key": ["0x0b","0x00","0x00","0x00"
    ],
    "value": {
        "error": "can\'t lookup element"
    }
]

root# bpftool map dump id 40
can't lookup element with key:
0a 00 00 00
can't lookup element with key:
0b 00 00 00
Found 0 elements

Output after changes:
root# bpftool map dump -jp  id 45
[
    "key": ["0x0a","0x00","0x00","0x00"
    ],
    "value": {
        "error": "Operation not supported"
    },
    "key": ["0x0b","0x00","0x00","0x00"
    ],
    "value": {
        "error": "Operation not supported"
    }
]

root# bpftool map dump id 45
key:
0a 00 00 00
value:
Operation not supported
key:
0b 00 00 00
value:
Operation not supported
Found 0 elements

Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/bpf/bpftool/map.c | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)
Song Liu - Oct. 9, 2018, 7 a.m.
On Mon, Oct 8, 2018 at 6:06 PM Prashant Bhole
<bhole_prashant_q7@lab.ntt.co.jp> wrote:
>
> Since map lookup error can be ENOENT or EOPNOTSUPP, let's print
> strerror() as error message in normal and JSON output.
>
> This patch adds helper function print_entry_error() to print
> entry from lookup error occurs
>
> Example: Following example dumps a map which does not support lookup.
>
> Output before:
> root# bpftool map -jp dump id 40
> [
>     "key": ["0x0a","0x00","0x00","0x00"
>     ],
>     "value": {
>         "error": "can\'t lookup element"
>     },
>     "key": ["0x0b","0x00","0x00","0x00"
>     ],
>     "value": {
>         "error": "can\'t lookup element"
>     }
> ]
>
> root# bpftool map dump id 40
> can't lookup element with key:
> 0a 00 00 00
> can't lookup element with key:
> 0b 00 00 00
> Found 0 elements
>
> Output after changes:
> root# bpftool map dump -jp  id 45
> [
>     "key": ["0x0a","0x00","0x00","0x00"
>     ],
>     "value": {
>         "error": "Operation not supported"
>     },
>     "key": ["0x0b","0x00","0x00","0x00"
>     ],
>     "value": {
>         "error": "Operation not supported"
>     }
> ]
>
> root# bpftool map dump id 45
> key:
> 0a 00 00 00
> value:
> Operation not supported
> key:
> 0b 00 00 00
> value:
> Operation not supported
> Found 0 elements
>
> Signed-off-by: Prashant Bhole <bhole_prashant_q7@lab.ntt.co.jp>
> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
> Acked-by: Alexei Starovoitov <ast@kernel.org>

Acked-by: Song Liu <songliubraving@fb.com>

> ---
>  tools/bpf/bpftool/map.c | 29 ++++++++++++++++++++++++-----
>  1 file changed, 24 insertions(+), 5 deletions(-)
>
> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
> index 28d365435fea..9f5de48f8a99 100644
> --- a/tools/bpf/bpftool/map.c
> +++ b/tools/bpf/bpftool/map.c
> @@ -336,6 +336,25 @@ static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
>         jsonw_end_object(json_wtr);
>  }
>
> +static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
> +                             const char *value)
> +{
> +       int value_size = strlen(value);
> +       bool single_line, break_names;
> +
> +       break_names = info->key_size > 16 || value_size > 16;
> +       single_line = info->key_size + value_size <= 24 && !break_names;
> +
> +       printf("key:%c", break_names ? '\n' : ' ');
> +       fprint_hex(stdout, key, info->key_size, " ");
> +
> +       printf(single_line ? "  " : "\n");
> +
> +       printf("value:%c%s", break_names ? '\n' : ' ', value);
> +
> +       printf("\n");
> +}
> +
>  static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
>                               unsigned char *value)
>  {
> @@ -663,6 +682,7 @@ static int dump_map_elem(int fd, void *key, void *value,
>                          json_writer_t *btf_wtr)
>  {
>         int num_elems = 0;
> +       int lookup_errno;
>
>         if (!bpf_map_lookup_elem(fd, key, value)) {
>                 if (json_output) {
> @@ -685,6 +705,8 @@ static int dump_map_elem(int fd, void *key, void *value,
>         }
>
>         /* lookup error handling */
> +       lookup_errno = errno;
> +
>         if (map_is_map_of_maps(map_info->type) ||
>             map_is_map_of_progs(map_info->type))
>                 return 0;
> @@ -694,13 +716,10 @@ static int dump_map_elem(int fd, void *key, void *value,
>                 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",
> -                                  "can't lookup element");
> +               jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
>                 jsonw_end_object(json_wtr);
>         } else {
> -               p_info("can't lookup element with key: ");
> -               fprint_hex(stderr, key, map_info->key_size, " ");
> -               fprintf(stderr, "\n");
> +               print_entry_error(map_info, key, strerror(lookup_errno));
>         }
>
>         return 0;
> --
> 2.17.1
>
>

Patch

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 28d365435fea..9f5de48f8a99 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -336,6 +336,25 @@  static void print_entry_json(struct bpf_map_info *info, unsigned char *key,
 	jsonw_end_object(json_wtr);
 }
 
+static void print_entry_error(struct bpf_map_info *info, unsigned char *key,
+			      const char *value)
+{
+	int value_size = strlen(value);
+	bool single_line, break_names;
+
+	break_names = info->key_size > 16 || value_size > 16;
+	single_line = info->key_size + value_size <= 24 && !break_names;
+
+	printf("key:%c", break_names ? '\n' : ' ');
+	fprint_hex(stdout, key, info->key_size, " ");
+
+	printf(single_line ? "  " : "\n");
+
+	printf("value:%c%s", break_names ? '\n' : ' ', value);
+
+	printf("\n");
+}
+
 static void print_entry_plain(struct bpf_map_info *info, unsigned char *key,
 			      unsigned char *value)
 {
@@ -663,6 +682,7 @@  static int dump_map_elem(int fd, void *key, void *value,
 			 json_writer_t *btf_wtr)
 {
 	int num_elems = 0;
+	int lookup_errno;
 
 	if (!bpf_map_lookup_elem(fd, key, value)) {
 		if (json_output) {
@@ -685,6 +705,8 @@  static int dump_map_elem(int fd, void *key, void *value,
 	}
 
 	/* lookup error handling */
+	lookup_errno = errno;
+
 	if (map_is_map_of_maps(map_info->type) ||
 	    map_is_map_of_progs(map_info->type))
 		return 0;
@@ -694,13 +716,10 @@  static int dump_map_elem(int fd, void *key, void *value,
 		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",
-				   "can't lookup element");
+		jsonw_string_field(json_wtr, "error", strerror(lookup_errno));
 		jsonw_end_object(json_wtr);
 	} else {
-		p_info("can't lookup element with key: ");
-		fprint_hex(stderr, key, map_info->key_size, " ");
-		fprintf(stderr, "\n");
+		print_entry_error(map_info, key, strerror(lookup_errno));
 	}
 
 	return 0;