Skip to content

Commit

Permalink
Merge branch 'Stop using bpf_object__find_program_by_title API'
Browse files Browse the repository at this point in the history
Kui-Feng Lee says:

====================

bpf_object__find_program_by_title is going to be deprecated since
v0.7.  Replace all use cases with bpf_object__find_program_by_name if
possible, or use bpf_object__for_each_program to iterate over
programs, matching section names.

V3 fixes a broken test case, fexit_bpf2bpf, in selftests/bpf, using
bpf_obj__for_each_program API instead.

[v2] https://lore.kernel.org/bpf/20211211003608.2764928-1-kuifeng@fb.com/
[v1] https://lore.kernel.org/bpf/20211210190855.1369060-1-kuifeng@fb.com/T/
====================

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
  • Loading branch information
Andrii Nakryiko committed Dec 14, 2021
2 parents c164b8b + 0da2596 commit a34efe5
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 71 deletions.
11 changes: 10 additions & 1 deletion samples/bpf/hbm.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ static void do_error(char *msg, bool errno_flag)

static int prog_load(char *prog)
{
struct bpf_program *pos;
const char *sec_name;

obj = bpf_object__open_file(prog, NULL);
if (libbpf_get_error(obj)) {
printf("ERROR: opening BPF object file failed\n");
Expand All @@ -132,7 +135,13 @@ static int prog_load(char *prog)
goto err;
}

bpf_prog = bpf_object__find_program_by_title(obj, "cgroup_skb/egress");
bpf_object__for_each_program(pos, obj) {
sec_name = bpf_program__section_name(pos);
if (sec_name && !strcmp(sec_name, "cgroup_skb/egress")) {
bpf_prog = pos;
break;
}
}
if (!bpf_prog) {
printf("ERROR: finding a prog in obj file failed\n");
goto err;
Expand Down
12 changes: 10 additions & 2 deletions samples/bpf/xdp_fwd_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ int main(int argc, char **argv)
.prog_type = BPF_PROG_TYPE_XDP,
};
const char *prog_name = "xdp_fwd";
struct bpf_program *prog;
struct bpf_program *prog = NULL;
struct bpf_program *pos;
const char *sec_name;
int prog_fd, map_fd = -1;
char filename[PATH_MAX];
struct bpf_object *obj;
Expand Down Expand Up @@ -134,7 +136,13 @@ int main(int argc, char **argv)
return 1;
}

prog = bpf_object__find_program_by_title(obj, prog_name);
bpf_object__for_each_program(pos, obj) {
sec_name = bpf_program__section_name(pos);
if (sec_name && !strcmp(sec_name, prog_name)) {
prog = pos;
break;
}
}
prog_fd = bpf_program__fd(prog);
if (prog_fd < 0) {
printf("program not found: %s\n", strerror(prog_fd));
Expand Down
1 change: 1 addition & 0 deletions tools/lib/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ struct btf;
LIBBPF_API struct btf *bpf_object__btf(const struct bpf_object *obj);
LIBBPF_API int bpf_object__btf_fd(const struct bpf_object *obj);

LIBBPF_DEPRECATED_SINCE(0, 7, "use bpf_object__find_program_by_name() instead")
LIBBPF_API struct bpf_program *
bpf_object__find_program_by_title(const struct bpf_object *obj,
const char *title);
Expand Down
13 changes: 12 additions & 1 deletion tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -3257,10 +3257,21 @@ static void trace__set_bpf_map_syscalls(struct trace *trace)

static struct bpf_program *trace__find_bpf_program_by_title(struct trace *trace, const char *name)
{
struct bpf_program *pos, *prog = NULL;
const char *sec_name;

if (trace->bpf_obj == NULL)
return NULL;

return bpf_object__find_program_by_title(trace->bpf_obj, name);
bpf_object__for_each_program(pos, trace->bpf_obj) {
sec_name = bpf_program__section_name(pos);
if (sec_name && !strcmp(sec_name, name)) {
prog = pos;
break;
}
}

return prog;
}

static struct bpf_program *trace__find_syscall_bpf_prog(struct trace *trace, struct syscall *sc,
Expand Down
4 changes: 2 additions & 2 deletions tools/testing/selftests/bpf/prog_tests/bpf_obj_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ void serial_test_bpf_obj_id(void)
if (CHECK_FAIL(err))
goto done;

prog = bpf_object__find_program_by_title(objs[i],
"raw_tp/sys_enter");
prog = bpf_object__find_program_by_name(objs[i],
"test_obj_id");
if (CHECK_FAIL(!prog))
goto done;
links[i] = bpf_program__attach(prog);
Expand Down
18 changes: 9 additions & 9 deletions tools/testing/selftests/bpf/prog_tests/connect_force_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ static int run_test(int cgroup_fd, int server_fd, int family, int type)
goto close_bpf_object;
}

prog = bpf_object__find_program_by_title(obj, v4 ?
"cgroup/connect4" :
"cgroup/connect6");
prog = bpf_object__find_program_by_name(obj, v4 ?
"connect4" :
"connect6");
if (CHECK(!prog, "find_prog", "connect prog not found\n")) {
err = -EIO;
goto close_bpf_object;
Expand All @@ -83,9 +83,9 @@ static int run_test(int cgroup_fd, int server_fd, int family, int type)
goto close_bpf_object;
}

prog = bpf_object__find_program_by_title(obj, v4 ?
"cgroup/getpeername4" :
"cgroup/getpeername6");
prog = bpf_object__find_program_by_name(obj, v4 ?
"getpeername4" :
"getpeername6");
if (CHECK(!prog, "find_prog", "getpeername prog not found\n")) {
err = -EIO;
goto close_bpf_object;
Expand All @@ -99,9 +99,9 @@ static int run_test(int cgroup_fd, int server_fd, int family, int type)
goto close_bpf_object;
}

prog = bpf_object__find_program_by_title(obj, v4 ?
"cgroup/getsockname4" :
"cgroup/getsockname6");
prog = bpf_object__find_program_by_name(obj, v4 ?
"getsockname4" :
"getsockname6");
if (CHECK(!prog, "find_prog", "getsockname prog not found\n")) {
err = -EIO;
goto close_bpf_object;
Expand Down
79 changes: 53 additions & 26 deletions tools/testing/selftests/bpf/prog_tests/core_reloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ static int duration = 0;

#define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)

#define MODULES_CASE(name, sec_name, tp_name) { \
#define MODULES_CASE(name, pg_name, tp_name) { \
.case_name = name, \
.bpf_obj_file = "test_core_reloc_module.o", \
.btf_src_file = NULL, /* find in kernel module BTFs */ \
Expand All @@ -28,7 +28,7 @@ static int duration = 0;
.comm_len = sizeof("test_progs"), \
}, \
.output_len = sizeof(struct core_reloc_module_output), \
.prog_sec_name = sec_name, \
.prog_name = pg_name, \
.raw_tp_name = tp_name, \
.trigger = __trigger_module_test_read, \
.needs_testmod = true, \
Expand All @@ -43,7 +43,9 @@ static int duration = 0;
#define FLAVORS_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_flavors.o", \
.btf_src_file = "btf__core_reloc_" #name ".o" \
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_flavors" \

#define FLAVORS_CASE(name) { \
FLAVORS_CASE_COMMON(name), \
Expand All @@ -66,7 +68,9 @@ static int duration = 0;
#define NESTING_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_nesting.o", \
.btf_src_file = "btf__core_reloc_" #name ".o"
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_nesting" \

#define NESTING_CASE(name) { \
NESTING_CASE_COMMON(name), \
Expand All @@ -91,7 +95,9 @@ static int duration = 0;
#define ARRAYS_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_arrays.o", \
.btf_src_file = "btf__core_reloc_" #name ".o"
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_arrays" \

#define ARRAYS_CASE(name) { \
ARRAYS_CASE_COMMON(name), \
Expand Down Expand Up @@ -123,7 +129,9 @@ static int duration = 0;
#define PRIMITIVES_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_primitives.o", \
.btf_src_file = "btf__core_reloc_" #name ".o"
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_primitives" \

#define PRIMITIVES_CASE(name) { \
PRIMITIVES_CASE_COMMON(name), \
Expand Down Expand Up @@ -158,6 +166,8 @@ static int duration = 0;
.e = 5, .f = 6, .g = 7, .h = 8, \
}, \
.output_len = sizeof(struct core_reloc_mods_output), \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_mods", \
}

#define PTR_AS_ARR_CASE(name) { \
Expand All @@ -174,6 +184,8 @@ static int duration = 0;
.a = 3, \
}, \
.output_len = sizeof(struct core_reloc_ptr_as_arr), \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_ptr_as_arr", \
}

#define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) { \
Expand All @@ -190,7 +202,9 @@ static int duration = 0;
#define INTS_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_ints.o", \
.btf_src_file = "btf__core_reloc_" #name ".o"
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_ints"

#define INTS_CASE(name) { \
INTS_CASE_COMMON(name), \
Expand All @@ -208,7 +222,9 @@ static int duration = 0;
#define FIELD_EXISTS_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_existence.o", \
.btf_src_file = "btf__core_reloc_" #name ".o" \
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_existence"

#define BITFIELDS_CASE_COMMON(objfile, test_name_prefix, name) \
.case_name = test_name_prefix#name, \
Expand All @@ -223,6 +239,8 @@ static int duration = 0;
.output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output) \
__VA_ARGS__, \
.output_len = sizeof(struct core_reloc_bitfields_output), \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_bitfields", \
}, { \
BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o", \
"direct:", name), \
Expand All @@ -231,25 +249,29 @@ static int duration = 0;
.output = STRUCT_TO_CHAR_PTR(core_reloc_bitfields_output) \
__VA_ARGS__, \
.output_len = sizeof(struct core_reloc_bitfields_output), \
.prog_sec_name = "tp_btf/sys_enter", \
.prog_name = "test_core_bitfields_direct", \
}


#define BITFIELDS_ERR_CASE(name) { \
BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_probed.o", \
"probed:", name), \
.fails = true, \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_bitfields", \
}, { \
BITFIELDS_CASE_COMMON("test_core_reloc_bitfields_direct.o", \
"direct:", name), \
.prog_sec_name = "tp_btf/sys_enter", \
.fails = true, \
.prog_name = "test_core_bitfields_direct", \
}

#define SIZE_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_size.o", \
.btf_src_file = "btf__core_reloc_" #name ".o"
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_size"

#define SIZE_OUTPUT_DATA(type) \
STRUCT_TO_CHAR_PTR(core_reloc_size_output) { \
Expand Down Expand Up @@ -277,8 +299,10 @@ static int duration = 0;

#define TYPE_BASED_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_type_based.o", \
.btf_src_file = "btf__core_reloc_" #name ".o" \
.bpf_obj_file = "test_core_reloc_type_based.o", \
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_type_based"

#define TYPE_BASED_CASE(name, ...) { \
TYPE_BASED_CASE_COMMON(name), \
Expand All @@ -295,7 +319,9 @@ static int duration = 0;
#define TYPE_ID_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_type_id.o", \
.btf_src_file = "btf__core_reloc_" #name ".o" \
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_type_id"

#define TYPE_ID_CASE(name, setup_fn) { \
TYPE_ID_CASE_COMMON(name), \
Expand All @@ -312,7 +338,9 @@ static int duration = 0;
#define ENUMVAL_CASE_COMMON(name) \
.case_name = #name, \
.bpf_obj_file = "test_core_reloc_enumval.o", \
.btf_src_file = "btf__core_reloc_" #name ".o" \
.btf_src_file = "btf__core_reloc_" #name ".o", \
.raw_tp_name = "sys_enter", \
.prog_name = "test_core_enumval"

#define ENUMVAL_CASE(name, ...) { \
ENUMVAL_CASE_COMMON(name), \
Expand Down Expand Up @@ -342,7 +370,7 @@ struct core_reloc_test_case {
bool fails;
bool needs_testmod;
bool relaxed_core_relocs;
const char *prog_sec_name;
const char *prog_name;
const char *raw_tp_name;
setup_test_fn setup;
trigger_test_fn trigger;
Expand Down Expand Up @@ -497,11 +525,13 @@ static struct core_reloc_test_case test_cases[] = {
.comm_len = sizeof("test_progs"),
},
.output_len = sizeof(struct core_reloc_kernel_output),
.raw_tp_name = "sys_enter",
.prog_name = "test_core_kernel",
},

/* validate we can find kernel module BTF types for relocs/attach */
MODULES_CASE("module_probed", "raw_tp/bpf_testmod_test_read", "bpf_testmod_test_read"),
MODULES_CASE("module_direct", "tp_btf/bpf_testmod_test_read", NULL),
MODULES_CASE("module_probed", "test_core_module_probed", "bpf_testmod_test_read"),
MODULES_CASE("module_direct", "test_core_module_direct", NULL),

/* validate BPF program can use multiple flavors to match against
* single target BTF type
Expand Down Expand Up @@ -580,6 +610,8 @@ static struct core_reloc_test_case test_cases[] = {
.c = 0, /* BUG in clang, should be 3 */
},
.output_len = sizeof(struct core_reloc_misc_output),
.raw_tp_name = "sys_enter",
.prog_name = "test_core_misc",
},

/* validate field existence checks */
Expand Down Expand Up @@ -848,14 +880,9 @@ void test_core_reloc(void)
if (!ASSERT_OK_PTR(obj, "obj_open"))
goto cleanup;

probe_name = "raw_tracepoint/sys_enter";
tp_name = "sys_enter";
if (test_case->prog_sec_name) {
probe_name = test_case->prog_sec_name;
tp_name = test_case->raw_tp_name; /* NULL for tp_btf */
}

prog = bpf_object__find_program_by_title(obj, probe_name);
probe_name = test_case->prog_name;
tp_name = test_case->raw_tp_name; /* NULL for tp_btf */
prog = bpf_object__find_program_by_name(obj, probe_name);
if (CHECK(!prog, "find_probe",
"prog '%s' not found\n", probe_name))
goto cleanup;
Expand Down
Loading

0 comments on commit a34efe5

Please sign in to comment.