Skip to content

Commit c76e4c2

Browse files
Taeungborkmann
authored andcommitted
libbpf: Show supported ELF section names when failing to guess prog/attach type
We need to let users check their wrong ELF section name with proper ELF section names when they fail 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 Signed-off-by: Taeung Song <[email protected]> Cc: Quentin Monnet <[email protected]> Cc: Jakub Kicinski <[email protected]> Cc: Andrey Ignatov <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Acked-by: Jakub Kicinski <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent ffcf7ce commit c76e4c2

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

tools/bpf/bpftool/prog.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -930,10 +930,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
930930
err = libbpf_prog_type_by_name(type, &attr.prog_type,
931931
&expected_attach_type);
932932
free(type);
933-
if (err < 0) {
934-
p_err("unknown program type '%s'", *argv);
933+
if (err < 0)
935934
goto err_free_reuse_maps;
936-
}
935+
937936
NEXT_ARG();
938937
} else if (is_prefix(*argv, "map")) {
939938
void *new_map_replace;
@@ -1028,11 +1027,8 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
10281027

10291028
err = libbpf_prog_type_by_name(sec_name, &prog_type,
10301029
&expected_attach_type);
1031-
if (err < 0) {
1032-
p_err("failed to guess program type based on section name %s\n",
1033-
sec_name);
1030+
if (err < 0)
10341031
goto err_close_obj;
1035-
}
10361032
}
10371033

10381034
bpf_program__set_ifindex(pos, ifindex);

tools/lib/bpf/libbpf.c

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,9 +2667,38 @@ static const struct {
26672667
#undef BPF_EAPROG_SEC
26682668
#undef BPF_APROG_COMPAT
26692669

2670+
#define MAX_TYPE_NAME_SIZE 32
2671+
2672+
static char *libbpf_get_type_names(bool attach_type)
2673+
{
2674+
int i, len = ARRAY_SIZE(section_names) * MAX_TYPE_NAME_SIZE;
2675+
char *buf;
2676+
2677+
buf = malloc(len);
2678+
if (!buf)
2679+
return NULL;
2680+
2681+
buf[0] = '\0';
2682+
/* Forge string buf with all available names */
2683+
for (i = 0; i < ARRAY_SIZE(section_names); i++) {
2684+
if (attach_type && !section_names[i].is_attachable)
2685+
continue;
2686+
2687+
if (strlen(buf) + strlen(section_names[i].sec) + 2 > len) {
2688+
free(buf);
2689+
return NULL;
2690+
}
2691+
strcat(buf, " ");
2692+
strcat(buf, section_names[i].sec);
2693+
}
2694+
2695+
return buf;
2696+
}
2697+
26702698
int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
26712699
enum bpf_attach_type *expected_attach_type)
26722700
{
2701+
char *type_names;
26732702
int i;
26742703

26752704
if (!name)
@@ -2682,12 +2711,20 @@ int libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
26822711
*expected_attach_type = section_names[i].expected_attach_type;
26832712
return 0;
26842713
}
2714+
pr_warning("failed to guess program type based on ELF section name '%s'\n", name);
2715+
type_names = libbpf_get_type_names(false);
2716+
if (type_names != NULL) {
2717+
pr_info("supported section(type) names are:%s\n", type_names);
2718+
free(type_names);
2719+
}
2720+
26852721
return -EINVAL;
26862722
}
26872723

26882724
int libbpf_attach_type_by_name(const char *name,
26892725
enum bpf_attach_type *attach_type)
26902726
{
2727+
char *type_names;
26912728
int i;
26922729

26932730
if (!name)
@@ -2701,6 +2738,13 @@ int libbpf_attach_type_by_name(const char *name,
27012738
*attach_type = section_names[i].attach_type;
27022739
return 0;
27032740
}
2741+
pr_warning("failed to guess attach type based on ELF section name '%s'\n", name);
2742+
type_names = libbpf_get_type_names(true);
2743+
if (type_names != NULL) {
2744+
pr_info("attachable section(type) names are:%s\n", type_names);
2745+
free(type_names);
2746+
}
2747+
27042748
return -EINVAL;
27052749
}
27062750

@@ -2907,8 +2951,6 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
29072951
err = bpf_program__identify_section(prog, &prog_type,
29082952
&expected_attach_type);
29092953
if (err < 0) {
2910-
pr_warning("failed to guess program type based on section name %s\n",
2911-
prog->section_name);
29122954
bpf_object__close(obj);
29132955
return -EINVAL;
29142956
}

tools/testing/selftests/bpf/test_socket_cookie.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,8 @@ static int run_test(int cgfd)
158158
bpf_object__for_each_program(prog, pobj) {
159159
prog_name = bpf_program__title(prog, /*needs_copy*/ false);
160160

161-
if (libbpf_attach_type_by_name(prog_name, &attach_type)) {
162-
log_err("Unexpected prog: %s", prog_name);
161+
if (libbpf_attach_type_by_name(prog_name, &attach_type))
163162
goto err;
164-
}
165163

166164
err = bpf_prog_attach(bpf_program__fd(prog), cgfd, attach_type,
167165
BPF_F_ALLOW_OVERRIDE);

0 commit comments

Comments
 (0)