Skip to content

Commit 7eaefbd

Browse files
AsphalttKernel Patches Daemon
authored andcommitted
libbpf: Capture error message on freplace attach failure
Extend 'bpf_link_create()' to support capturing log output from the kernel when creating a freplace link. Additionally, introduce a new API, 'bpf_program__attach_freplace_log()', to allow users to retrieve detailed error message when a freplace program fails to attach. Signed-off-by: Leon Hwang <[email protected]>
1 parent 1d9a918 commit 7eaefbd

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

tools/lib/bpf/bpf.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ int bpf_link_create(int prog_fd, int target_fd,
772772
{
773773
const size_t attr_sz = offsetofend(union bpf_attr, link_create);
774774
__u32 target_btf_id, iter_info_len, relative_id;
775+
struct bpf_common_attr common_attrs;
775776
int fd, err, relative_fd;
776777
union bpf_attr attr;
777778

@@ -785,7 +786,9 @@ int bpf_link_create(int prog_fd, int target_fd,
785786
if (iter_info_len || target_btf_id) {
786787
if (iter_info_len && target_btf_id)
787788
return libbpf_err(-EINVAL);
788-
if (!OPTS_ZEROED(opts, target_btf_id))
789+
if (iter_info_len && !OPTS_ZEROED(opts, target_btf_id))
790+
return libbpf_err(-EINVAL);
791+
if (target_btf_id && !OPTS_ZEROED(opts, tracing.log_size))
789792
return libbpf_err(-EINVAL);
790793
}
791794

@@ -795,7 +798,12 @@ int bpf_link_create(int prog_fd, int target_fd,
795798
attr.link_create.attach_type = attach_type;
796799
attr.link_create.flags = OPTS_GET(opts, flags, 0);
797800

801+
memset(&common_attrs, 0, sizeof(common_attrs));
798802
if (target_btf_id) {
803+
common_attrs.log_buf = (__u64) OPTS_GET(opts, tracing.log_buf, NULL);
804+
common_attrs.log_size = OPTS_GET(opts, tracing.log_size, 0);
805+
if (common_attrs.log_buf && !feat_supported(NULL, FEAT_EXTENDED_SYSCALL))
806+
return libbpf_err(-EOPNOTSUPP);
799807
attr.link_create.target_btf_id = target_btf_id;
800808
goto proceed;
801809
}
@@ -931,7 +939,9 @@ int bpf_link_create(int prog_fd, int target_fd,
931939
break;
932940
}
933941
proceed:
934-
fd = sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz);
942+
fd = !common_attrs.log_buf ? sys_bpf_fd(BPF_LINK_CREATE, &attr, attr_sz)
943+
: sys_bpf_fd_extended(BPF_LINK_CREATE, &attr, attr_sz,
944+
&common_attrs, sizeof(common_attrs));
935945
if (fd >= 0)
936946
return fd;
937947
/* we'll get EINVAL if LINK_CREATE doesn't support attaching fentry

tools/lib/bpf/bpf.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ struct bpf_link_create_opts {
422422
} uprobe_multi;
423423
struct {
424424
__u64 cookie;
425+
const char *log_buf;
426+
unsigned int log_size;
425427
} tracing;
426428
struct {
427429
__u32 pf;

tools/lib/bpf/libbpf.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12949,9 +12949,11 @@ bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
1294912949
return bpf_program_attach_fd(prog, ifindex, "netkit", &link_create_opts);
1295012950
}
1295112951

12952-
struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
12953-
int target_fd,
12954-
const char *attach_func_name)
12952+
struct bpf_link *bpf_program__attach_freplace_log(const struct bpf_program *prog,
12953+
int target_fd,
12954+
const char *attach_func_name,
12955+
const char *log_buf,
12956+
unsigned int log_size)
1295512957
{
1295612958
int btf_id;
1295712959

@@ -12975,7 +12977,8 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
1297512977
return libbpf_err_ptr(btf_id);
1297612978

1297712979
target_opts.target_btf_id = btf_id;
12978-
12980+
target_opts.tracing.log_buf = log_buf;
12981+
target_opts.tracing.log_size = log_size;
1297912982
return bpf_program_attach_fd(prog, target_fd, "freplace",
1298012983
&target_opts);
1298112984
} else {
@@ -12986,6 +12989,13 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
1298612989
}
1298712990
}
1298812991

12992+
struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
12993+
int target_fd,
12994+
const char *attach_func_name)
12995+
{
12996+
return bpf_program__attach_freplace_log(prog, target_fd, attach_func_name, NULL, 0);
12997+
}
12998+
1298912999
struct bpf_link *
1299013000
bpf_program__attach_iter(const struct bpf_program *prog,
1299113001
const struct bpf_iter_attach_opts *opts)

tools/lib/bpf/libbpf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,10 @@ bpf_program__attach_sockmap(const struct bpf_program *prog, int map_fd);
829829
LIBBPF_API struct bpf_link *
830830
bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
831831
LIBBPF_API struct bpf_link *
832+
bpf_program__attach_freplace_log(const struct bpf_program *prog,
833+
int target_fd, const char *attach_func_name,
834+
const char *log_buf, unsigned int log_size);
835+
LIBBPF_API struct bpf_link *
832836
bpf_program__attach_freplace(const struct bpf_program *prog,
833837
int target_fd, const char *attach_func_name);
834838

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,5 +449,6 @@ LIBBPF_1.6.0 {
449449

450450
LIBBPF_1.7.0 {
451451
global:
452+
bpf_program__attach_freplace_log;
452453
probe_sys_bpf_extended;
453454
} LIBBPF_1.6.0;

0 commit comments

Comments
 (0)