Skip to contentSkip to conversation

libbpf: Show supported ELF section names on when failed to guess a prog/attach type

PATCH Applied Taeung Song 4 participants 4 messages Jan 21, 2019 – Jan 23, 2019

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

PATCH v4 1/1 libbpf: Show supported ELF section names on when failed to guess a prog/attach type

We need to let users check their wrong ELF section name
with proper ELF section names when failed to get a prog/attach type from it. Because users can't realize libbpf guess prog/attach types from given ELF section names.
For example, when a 'cgroup' section name of a BPF program is used, show available ELF section names(types).

Before:

    $ bpftool prog load bpf-prog.o /sys/fs/bpf/prog1
    Error: failed to guess program type based on ELF section name cgroup

After:

libbpf: failed to guess program type based on ELF section name 'cgroup'
libbpf: supported section(type) names are: socket kprobe/ kretprobe/ classifier action tracepoint/ raw_tracepoint/ xdp perf_event lwt_in lwt_out lwt_xmit lwt_seg6local cgroup_skb/ingress cgroup_skb/egress cgroup/skb cgroup/sock cgroup/post_bind4 cgroup/post_bind6 cgroup/dev sockops sk_skb/stream_parser sk_skb/stream_verdict sk_skb sk_msg lirc_mode2 flow_dissector cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6

Cc Quentin Monnet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrey Ignatov <[email protected]>
---
 tools/bpf/bpftool/prog.c                      | 10 ++--
 tools/lib/bpf/libbpf.c                        | 46 ++++++++++++++++++-
 .../selftests/bpf/test_socket_cookie.c        |  4 +-
 3 files changed, 48 insertions(+), 12 deletions(-)
tools/bpf/bpftool/prog.c +3 -7
index 2d1bb7d6ff51..0640e9bc0ada 100644
@@ -930,10 +930,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 			err = libbpf_prog_type_by_name(type, &attr.prog_type,
 						       &expected_attach_type);
 			free(type);
-			if (err < 0) {
-				p_err("unknown program type '%s'", *argv);
+			if (err < 0)
 				goto err_free_reuse_maps;
-			}
+
 			NEXT_ARG();
 		} else if (is_prefix(*argv, "map")) {
 			void *new_map_replace;
@@ -1028,11 +1027,8 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 
 			err = libbpf_prog_type_by_name(sec_name, &prog_type,
 						       &expected_attach_type);
-			if (err < 0) {
-				p_err("failed to guess program type based on section name %s\n",
-				      sec_name);
+			if (err < 0)
 				goto err_close_obj;
-			}
 		}
 
 		bpf_program__set_ifindex(pos, ifindex);
tools/lib/bpf/libbpf.c +44 -2

79 lines not on this page · raw message

tools/testing/selftests/bpf/test_socket_cookie.c +1 -3
index fc7832ee566b..e51d63786ff8 100644
@@ -158,10 +158,8 @@ static int run_test(int cgfd)
 	bpf_object__for_each_program(prog, pobj) {
 		prog_name = bpf_program__title(prog, /*needs_copy*/ false);
 
-		if (libbpf_attach_type_by_name(prog_name, &attach_type)) {
-			log_err("Unexpected prog: %s", prog_name);
+		if (libbpf_attach_type_by_name(prog_name, &attach_type))
 			goto err;
-		}
 
 		err = bpf_prog_attach(bpf_program__fd(prog), cgfd, attach_type,
 				      BPF_F_ALLOW_OVERRIDE);
Signature · 1 line
2.17.1
Reviewed-by on v4 1/1
#1 lore raw

2019-01-21 22:06 UTC+0900 ~ Taeung Song <[email protected]>

Quoted 14 lines

We need to let users check their wrong ELF section name
with proper ELF section names when failed to get a prog/attach type from it. Because users can't realize libbpf guess prog/attach types from given ELF section names.
For example, when a 'cgroup' section name of a BPF program is used, show available ELF section names(types).

Before:

    $ bpftool prog load bpf-prog.o /sys/fs/bpf/prog1
    Error: failed to guess program type based on ELF section name cgroup

After:

Quoted 7 lines

libbpf: failed to guess program type based on ELF section name 'cgroup'
libbpf: supported section(type) names are: socket kprobe/ kretprobe/ classifier action tracepoint/ raw_tracepoint/ xdp perf_event lwt_in lwt_out lwt_xmit lwt_seg6local cgroup_skb/ingress cgroup_skb/egress cgroup/skb cgroup/sock cgroup/post_bind4 cgroup/post_bind6 cgroup/dev sockops sk_skb/stream_parser sk_skb/stream_verdict sk_skb sk_msg lirc_mode2 flow_dissector cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6

Cc Quentin Monnet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrey Ignatov <[email protected]>

Thanks for the modifications! This version looks good to me.

Quoted 12 lines
---
 tools/bpf/bpftool/prog.c                      | 10 ++--
 tools/lib/bpf/libbpf.c                        | 46 ++++++++++++++++++-
 .../selftests/bpf/test_socket_cookie.c        |  4 +-
 3 files changed, 48 insertions(+), 12 deletions(-)
tools/bpf/bpftool/prog.c +0 -0
index 2d1bb7d6ff51..0640e9bc0ada 100644

tools/bpf/bpftool/prog.c int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type, · line 2716

Quoted 6 lines
tools/bpf/bpftool/prog.c +3 -0
@@ -2682,12 +2711,20 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
 		*expected_attach_type = section_names[i].expected_attach_type;
 		return 0;
 	}
+	pr_warning("failed to guess program type based on ELF section name '%s'\n", name);
+	type_names = libbpf_get_type_names(false);
+	if (type_names != NULL) {

For next time: No need for the "!= NULL" part here (just checking on "if (type_names)" is enough). But don't respin your series just for that :).

Quoted 14 lines
tools/bpf/bpftool/prog.c +5 -0
+		pr_info("supported section(type) names are:%s\n", type_names);
+		free(type_names);
+	}
+
 	return -EINVAL;
 }
 
 int libbpf_attach_type_by_name(const char *name,
 			       enum bpf_attach_type *attach_type)
 {
+	char *type_names;
 	int i;
 
 	if (!name)
Acked-by on v4 1/1
#2 lore raw
In reply to Taeung Song
Quoted 14 lines · Taeung Song

We need to let users check their wrong ELF section name
with proper ELF section names when failed to get a prog/attach type from it. Because users can't realize libbpf guess prog/attach types from given ELF section names.
For example, when a 'cgroup' section name of a BPF program is used, show available ELF section names(types).

Before:

    $ bpftool prog load bpf-prog.o /sys/fs/bpf/prog1
    Error: failed to guess program type based on ELF section name cgroup

After:

Quoted 7 lines

libbpf: failed to guess program type based on ELF section name 'cgroup'
libbpf: supported section(type) names are: socket kprobe/ kretprobe/ classifier action tracepoint/ raw_tracepoint/ xdp perf_event lwt_in lwt_out lwt_xmit lwt_seg6local cgroup_skb/ingress cgroup_skb/egress cgroup/skb cgroup/sock cgroup/post_bind4 cgroup/post_bind6 cgroup/dev sockops sk_skb/stream_parser sk_skb/stream_verdict sk_skb sk_msg lirc_mode2 flow_dissector cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6

Cc Quentin Monnet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrey Ignatov <[email protected]>
maintainer Applied
#3 lore raw
In reply to Taeung Song
Quoted 14 lines · Taeung Song

We need to let users check their wrong ELF section name
with proper ELF section names when failed to get a prog/attach type from it. Because users can't realize libbpf guess prog/attach types from given ELF section names.
For example, when a 'cgroup' section name of a BPF program is used, show available ELF section names(types).

Before:

    $ bpftool prog load bpf-prog.o /sys/fs/bpf/prog1
    Error: failed to guess program type based on ELF section name cgroup

After:

Quoted 7 lines

libbpf: failed to guess program type based on ELF section name 'cgroup'
libbpf: supported section(type) names are: socket kprobe/ kretprobe/ classifier action tracepoint/ raw_tracepoint/ xdp perf_event lwt_in lwt_out lwt_xmit lwt_seg6local cgroup_skb/ingress cgroup_skb/egress cgroup/skb cgroup/sock cgroup/post_bind4 cgroup/post_bind6 cgroup/dev sockops sk_skb/stream_parser sk_skb/stream_verdict sk_skb sk_msg lirc_mode2 flow_dissector cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6

Cc Quentin Monnet <[email protected]>
Cc Jakub Kicinski <[email protected]>
Cc Andrey Ignatov <[email protected]>

Applied, thanks!