diff mbox series

[v2,bpf-next,10/11] bpftool: Move get_prog_info() into do_show_link()

Message ID 20230608103523.102267-11-laoar.shao@gmail.com (mailing list archive)
State Superseded
Delegated to: BPF
Headers show
Series bpf: Support ->fill_link_info for kprobe_multi and perf_event links | expand

Checks

Context Check Description
bpf/vmtest-bpf-next-PR success PR summary
bpf/vmtest-bpf-next-VM_Test-1 success Logs for ShellCheck
bpf/vmtest-bpf-next-VM_Test-2 success Logs for build for aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-4 success Logs for build for x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-5 success Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-6 success Logs for set-matrix
bpf/vmtest-bpf-next-VM_Test-3 success Logs for build for s390x with gcc
bpf/vmtest-bpf-next-VM_Test-7 success Logs for test_maps on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-19 success Logs for test_progs_no_alu32_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-22 success Logs for test_progs_parallel on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-25 success Logs for test_verifier on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-9 success Logs for test_maps on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-10 success Logs for test_maps on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-11 success Logs for test_progs on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-13 success Logs for test_progs on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-14 success Logs for test_progs on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-15 success Logs for test_progs_no_alu32 on aarch64 with gcc
bpf/vmtest-bpf-next-VM_Test-17 success Logs for test_progs_no_alu32 on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-18 success Logs for test_progs_no_alu32 on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-20 success Logs for test_progs_no_alu32_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-21 success Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-23 success Logs for test_progs_parallel on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-24 success Logs for test_progs_parallel on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-27 success Logs for test_verifier on x86_64 with gcc
bpf/vmtest-bpf-next-VM_Test-28 success Logs for test_verifier on x86_64 with llvm-16
bpf/vmtest-bpf-next-VM_Test-29 success Logs for veristat
bpf/vmtest-bpf-next-VM_Test-26 success Logs for test_verifier on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-16 success Logs for test_progs_no_alu32 on s390x with gcc
bpf/vmtest-bpf-next-VM_Test-12 success Logs for test_progs on s390x with gcc
netdev/series_format success Posting correctly formatted
netdev/tree_selection success Clearly marked for bpf-next, async
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 8 this patch: 8
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 8 this patch: 8
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 8 this patch: 8
netdev/checkpatch warning WARNING: line length of 81 exceeds 80 columns
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-next-VM_Test-8 success Logs for test_maps on s390x with gcc

Commit Message

Yafang Shao June 8, 2023, 10:35 a.m. UTC
As a preparation for a subsequent change, the function get_prog_info() is
moved into do_show_link() with no functional alteration. This adjustment
paves the way for the upcoming modification.

Signed-off-by: Yafang Shao <laoar.shao@gmail.com>
---
 tools/bpf/bpftool/link.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/link.c b/tools/bpf/bpftool/link.c
index c8033c3..a2b75f4 100644
--- a/tools/bpf/bpftool/link.c
+++ b/tools/bpf/bpftool/link.c
@@ -195,11 +195,10 @@  static int get_prog_info(int prog_id, struct bpf_prog_info *info)
 	kernel_syms_destroy(&dd);
 }
 
-static int show_link_close_json(int fd, struct bpf_link_info *info)
+static int show_link_close_json(int fd, struct bpf_link_info *info,
+				const struct bpf_prog_info *prog_info)
 {
-	struct bpf_prog_info prog_info;
 	const char *prog_type_str;
-	int err;
 
 	jsonw_start_object(json_wtr);
 
@@ -211,16 +210,12 @@  static int show_link_close_json(int fd, struct bpf_link_info *info)
 				   u64_to_ptr(info->raw_tracepoint.tp_name));
 		break;
 	case BPF_LINK_TYPE_TRACING:
-		err = get_prog_info(info->prog_id, &prog_info);
-		if (err)
-			return err;
-
-		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
+		prog_type_str = libbpf_bpf_prog_type_str(prog_info->type);
 		/* libbpf will return NULL for variants unknown to it. */
 		if (prog_type_str)
 			jsonw_string_field(json_wtr, "prog_type", prog_type_str);
 		else
-			jsonw_uint_field(json_wtr, "prog_type", prog_info.type);
+			jsonw_uint_field(json_wtr, "prog_type", prog_info->type);
 
 		show_link_attach_type_json(info->tracing.attach_type,
 					   json_wtr);
@@ -412,11 +407,10 @@  static void show_kprobe_multi_plain(struct bpf_link_info *info)
 	kernel_syms_destroy(&dd);
 }
 
-static int show_link_close_plain(int fd, struct bpf_link_info *info)
+static int show_link_close_plain(int fd, struct bpf_link_info *info,
+				 const struct bpf_prog_info *prog_info)
 {
-	struct bpf_prog_info prog_info;
 	const char *prog_type_str;
-	int err;
 
 	show_link_header_plain(info);
 
@@ -426,16 +420,12 @@  static int show_link_close_plain(int fd, struct bpf_link_info *info)
 		       (const char *)u64_to_ptr(info->raw_tracepoint.tp_name));
 		break;
 	case BPF_LINK_TYPE_TRACING:
-		err = get_prog_info(info->prog_id, &prog_info);
-		if (err)
-			return err;
-
-		prog_type_str = libbpf_bpf_prog_type_str(prog_info.type);
+		prog_type_str = libbpf_bpf_prog_type_str(prog_info->type);
 		/* libbpf will return NULL for variants unknown to it. */
 		if (prog_type_str)
 			printf("\n\tprog_type %s  ", prog_type_str);
 		else
-			printf("\n\tprog_type %u  ", prog_info.type);
+			printf("\n\tprog_type %u  ", prog_info->type);
 
 		show_link_attach_type_plain(info->tracing.attach_type);
 		if (info->tracing.target_obj_id || info->tracing.target_btf_id)
@@ -479,6 +469,7 @@  static int show_link_close_plain(int fd, struct bpf_link_info *info)
 
 static int do_show_link(int fd)
 {
+	struct bpf_prog_info prog_info;
 	struct bpf_link_info info;
 	__u32 len = sizeof(info);
 	__u64 *addrs = NULL;
@@ -486,6 +477,7 @@  static int do_show_link(int fd)
 	int count;
 	int err;
 
+	memset(&prog_info, 0, sizeof(info));
 	memset(&info, 0, sizeof(info));
 again:
 	err = bpf_link_get_info_by_fd(fd, &info, &len);
@@ -495,6 +487,13 @@  static int do_show_link(int fd)
 		close(fd);
 		return err;
 	}
+
+	if (!prog_info.type) {
+		err = get_prog_info(info.prog_id, &prog_info);
+		if (err)
+			return err;
+	}
+
 	if (info.type == BPF_LINK_TYPE_RAW_TRACEPOINT &&
 	    !info.raw_tracepoint.tp_name) {
 		info.raw_tracepoint.tp_name = (unsigned long)&buf;
@@ -523,9 +522,9 @@  static int do_show_link(int fd)
 	}
 
 	if (json_output)
-		show_link_close_json(fd, &info);
+		show_link_close_json(fd, &info, &prog_info);
 	else
-		show_link_close_plain(fd, &info);
+		show_link_close_plain(fd, &info, &prog_info);
 
 	if (addrs)
 		free(addrs);