Patchwork [bpf-next,3/6] tools/bpf: bpftool, split the function do_dump()

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

Comments

Prashant Bhole - Oct. 9, 2018, 1:04 a.m.
do_dump() function in bpftool/map.c has deep indentations. In order
to reduce deep indent, let's move element printing code out of
do_dump() into dump_map_elem() function.

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 | 83 ++++++++++++++++++++++++-----------------
 1 file changed, 49 insertions(+), 34 deletions(-)
Song Liu - Oct. 9, 2018, 6:58 a.m.
On Mon, Oct 8, 2018 at 6:06 PM Prashant Bhole
<bhole_prashant_q7@lab.ntt.co.jp> wrote:
>
> do_dump() function in bpftool/map.c has deep indentations. In order
> to reduce deep indent, let's move element printing code out of
> do_dump() into dump_map_elem() function.
>
> 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 | 83 ++++++++++++++++++++++++-----------------
>  1 file changed, 49 insertions(+), 34 deletions(-)
>
> diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
> index 6003e9598973..28d365435fea 100644
> --- a/tools/bpf/bpftool/map.c
> +++ b/tools/bpf/bpftool/map.c
> @@ -658,6 +658,54 @@ static int do_show(int argc, char **argv)
>         return errno == ENOENT ? 0 : -1;
>  }
>
> +static int dump_map_elem(int fd, void *key, void *value,
> +                        struct bpf_map_info *map_info, struct btf *btf,
> +                        json_writer_t *btf_wtr)
> +{
> +       int num_elems = 0;
> +
> +       if (!bpf_map_lookup_elem(fd, key, value)) {
> +               if (json_output) {
> +                       print_entry_json(map_info, key, value, btf);
> +               } else {
> +                       if (btf) {
> +                               struct btf_dumper d = {
> +                                       .btf = btf,
> +                                       .jw = btf_wtr,
> +                                       .is_plain_text = true,
> +                               };
> +
> +                               do_dump_btf(&d, map_info, key, value);
> +                       } else {
> +                               print_entry_plain(map_info, key, value);
> +                       }
> +                       num_elems++;
> +               }
> +               return num_elems;
> +       }
> +
> +       /* lookup error handling */
> +       if (map_is_map_of_maps(map_info->type) ||
> +           map_is_map_of_progs(map_info->type))
> +               return 0;
> +
> +       if (json_output) {
> +               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",
> +                                  "can't lookup element");
> +               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");
> +       }
> +
> +       return 0;
> +}
> +
>  static int do_dump(int argc, char **argv)
>  {
>         struct bpf_map_info info = {};
> @@ -713,40 +761,7 @@ static int do_dump(int argc, char **argv)
>                                 err = 0;
>                         break;
>                 }
> -
> -               if (!bpf_map_lookup_elem(fd, key, value)) {
> -                       if (json_output)
> -                               print_entry_json(&info, key, value, btf);
> -                       else
> -                               if (btf) {
> -                                       struct btf_dumper d = {
> -                                               .btf = btf,
> -                                               .jw = btf_wtr,
> -                                               .is_plain_text = true,
> -                                       };
> -
> -                                       do_dump_btf(&d, &info, key, value);
> -                               } else {
> -                                       print_entry_plain(&info, key, value);
> -                               }
> -                       num_elems++;
> -               } else if (!map_is_map_of_maps(info.type) &&
> -                          !map_is_map_of_progs(info.type)) {
> -                       if (json_output) {
> -                               jsonw_name(json_wtr, "key");
> -                               print_hex_data_json(key, info.key_size);
> -                               jsonw_name(json_wtr, "value");
> -                               jsonw_start_object(json_wtr);
> -                               jsonw_string_field(json_wtr, "error",
> -                                                  "can't lookup element");
> -                               jsonw_end_object(json_wtr);
> -                       } else {
> -                               p_info("can't lookup element with key: ");
> -                               fprint_hex(stderr, key, info.key_size, " ");
> -                               fprintf(stderr, "\n");
> -                       }
> -               }
> -
> +               num_elems += dump_map_elem(fd, key, value, &info, btf, btf_wtr);
>                 prev_key = key;
>         }
>
> --
> 2.17.1
>
>

Patch

diff --git a/tools/bpf/bpftool/map.c b/tools/bpf/bpftool/map.c
index 6003e9598973..28d365435fea 100644
--- a/tools/bpf/bpftool/map.c
+++ b/tools/bpf/bpftool/map.c
@@ -658,6 +658,54 @@  static int do_show(int argc, char **argv)
 	return errno == ENOENT ? 0 : -1;
 }
 
+static int dump_map_elem(int fd, void *key, void *value,
+			 struct bpf_map_info *map_info, struct btf *btf,
+			 json_writer_t *btf_wtr)
+{
+	int num_elems = 0;
+
+	if (!bpf_map_lookup_elem(fd, key, value)) {
+		if (json_output) {
+			print_entry_json(map_info, key, value, btf);
+		} else {
+			if (btf) {
+				struct btf_dumper d = {
+					.btf = btf,
+					.jw = btf_wtr,
+					.is_plain_text = true,
+				};
+
+				do_dump_btf(&d, map_info, key, value);
+			} else {
+				print_entry_plain(map_info, key, value);
+			}
+			num_elems++;
+		}
+		return num_elems;
+	}
+
+	/* lookup error handling */
+	if (map_is_map_of_maps(map_info->type) ||
+	    map_is_map_of_progs(map_info->type))
+		return 0;
+
+	if (json_output) {
+		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",
+				   "can't lookup element");
+		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");
+	}
+
+	return 0;
+}
+
 static int do_dump(int argc, char **argv)
 {
 	struct bpf_map_info info = {};
@@ -713,40 +761,7 @@  static int do_dump(int argc, char **argv)
 				err = 0;
 			break;
 		}
-
-		if (!bpf_map_lookup_elem(fd, key, value)) {
-			if (json_output)
-				print_entry_json(&info, key, value, btf);
-			else
-				if (btf) {
-					struct btf_dumper d = {
-						.btf = btf,
-						.jw = btf_wtr,
-						.is_plain_text = true,
-					};
-
-					do_dump_btf(&d, &info, key, value);
-				} else {
-					print_entry_plain(&info, key, value);
-				}
-			num_elems++;
-		} else if (!map_is_map_of_maps(info.type) &&
-			   !map_is_map_of_progs(info.type)) {
-			if (json_output) {
-				jsonw_name(json_wtr, "key");
-				print_hex_data_json(key, info.key_size);
-				jsonw_name(json_wtr, "value");
-				jsonw_start_object(json_wtr);
-				jsonw_string_field(json_wtr, "error",
-						   "can't lookup element");
-				jsonw_end_object(json_wtr);
-			} else {
-				p_info("can't lookup element with key: ");
-				fprint_hex(stderr, key, info.key_size, " ");
-				fprintf(stderr, "\n");
-			}
-		}
-
+		num_elems += dump_map_elem(fd, key, value, &info, btf, btf_wtr);
 		prev_key = key;
 	}