tools/bpf: add log_level to bpf_load_program_attr
Subsystems (6)
PATCH v3 1/1 tools/bpf: add log_level to bpf_load_program_attr
The kernel verifier has three levels of logs:
0: no logs
1: logs mostly useful
Quoted 1 line
1: verbose
Current libbpf API functions bpf_load_program_xattr() and bpf_load_program() cannot specify log_level.
The bcc, however, provides an interface for user to specify log_level 2 for verbose output.
This patch added log_level into structure bpf_load_program_attr, so users, including bcc, can use bpf_load_program_xattr() to change log_level. The supported log_level is 0, 1, and 2.
The bpf selftest test_sock.c is modified to enable log_level = 2. If the "verbose" in test_sock.c is changed to true,
the test will output logs like below:
$ ./test_sock func#0 @0 0: R1=ctx(id=0,off=0,imm=0) R10=fp0,call_-1 0: (bf) r6 = r1 1: R1=ctx(id=0,off=0,imm=0) R6_w=ctx(id=0,off=0,imm=0) R10=fp0,call_-1 1: (61) r7 = *(u32 *)(r6 +28) invalid bpf_context access off=28 size=4
Test case: bind4 load with invalid access: src_ip6 .. [PASS]
...
Test case: bind6 allow all .. [PASS]
Summary: 16 PASSED, 0 FAILED
Some test_sock tests are negative tests and verbose verifier log will be printed out as shown in the above.
--- tools/lib/bpf/bpf.c | 22 +++++++++++++++++----- tools/lib/bpf/bpf.h | 1 + tools/testing/selftests/bpf/test_sock.c | 9 ++++++++- 3 files changed, 26 insertions(+), 6 deletions(-)
Changelog:
v2 -> v3:
. returning error immediately for inconsistent log_level/log_buf/log_buf_sz
values
. for log_level=1/2, always doing logging during bpf_prog_load.
v1 -> v2:
. make log_level as the last member of struct bpf_load_program_attr. . return -EINVAL if bpf_load_program_attr.log_level > 2.
tools/lib/bpf/bpf.c +17 -5
@@ -214,10 +214,15 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, {void *finfo =NULL , *linfo =NULL ;union bpf_attr attr; +__u32 log_level;__u32 name_len;int fd; -if (!load_attr) +if (!load_attr || !log_buf != !log_buf_sz) +return -EINVAL ; + + log_level = load_attr->log_level; +if (log_level >2 || (log_level && !log_buf))return -EINVAL ; name_len = load_attr->name ?strlen (load_attr->name) :0 ;
@@ -228,9 +233,16 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, attr.insn_cnt = (__u32 )load_attr->insns_cnt; attr.insns =ptr_to_u64 (load_attr->insns); attr.license =ptr_to_u64 (load_attr->license); - attr.log_buf =ptr_to_u64 (NULL ); - attr.log_size =0 ; - attr.log_level =0 ; + + attr.log_level = log_level; +if (log_level) { + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size = log_buf_sz; + }else { + attr.log_buf =ptr_to_u64 (NULL ); + attr.log_size =0 ; + } + attr.kern_version = load_attr->kern_version; attr.prog_ifindex = load_attr->prog_ifindex; attr.prog_btf_fd = load_attr->prog_btf_fd;
@@ -286,7 +298,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, goto done; } -if (!log_buf || !log_buf_sz) +if (log_level || !log_buf) goto done;/* Try again with log */
tools/lib/bpf/bpf.h +1 -0
@@ -85,6 +85,7 @@ struct bpf_load_program_attr {__u32 line_info_rec_size;const void *line_info;__u32 line_info_cnt; +__u32 log_level; };/* Flags to direct loading requirements */
tools/testing/selftests/bpf/test_sock.c +8 -1
@@ -20,6 +20,7 @@#define MAX_INSNS 512char bpf_log_buf[BPF_LOG_BUF_SIZE ]; +static bool verbose = false;struct sock_test {const char *descr;
@@ -325,6 +326,7 @@ static int load_sock_prog(const struct bpf_insn *prog, enum bpf_attach_type attach_type) {struct bpf_load_program_attr attr; +int ret;memset (&attr,0 ,sizeof (struct bpf_load_program_attr )); attr.prog_type =BPF_PROG_TYPE_CGROUP_SOCK ;
@@ -332,8 +334,13 @@ static int load_sock_prog(const struct bpf_insn *prog, attr.insns = prog; attr.insns_cnt =probe_prog_length (attr.insns); attr.license ="GPL" ; + attr.log_level =2 ; -return bpf_load_program_xattr (&attr, bpf_log_buf,BPF_LOG_BUF_SIZE ); + ret =bpf_load_program_xattr (&attr, bpf_log_buf,BPF_LOG_BUF_SIZE ); +if (verbose && ret <0 ) +fprintf (stderr,"%s\n" , bpf_log_buf); + +return ret; } static int attach_sock_prog(int cgfd, int progfd,
Signature · 1 line
2.17.1
Alexei Starovoitov
AppliedQuoted 117 lines · Yonghong Song
The kernel verifier has three levels of logs:
0: no logs
1: logs mostly usefulQuoted 1 line
1: verbose
Current libbpf API functions
bpf_load_program_xattr()andbpf_load_program()cannot specifylog_level.
The bcc, however, provides an interface for user to specifylog_level2 for verbose output.This patch added
log_levelinto structurebpf_load_program_attr, so users, including bcc, can usebpf_load_program_xattr()to changelog_level. The supportedlog_levelis 0, 1, and 2.The bpf selftest
test_sock.cis modified to enablelog_level= 2. If the "verbose" intest_sock.cis changed to true,
the test will output logs like below:$ ./test_sock func#0 @0 0: R1=ctx(id=0,off=0,imm=0) R10=fp0,call_-1 0: (bf) r6 = r1 1: R1=ctx(id=0,off=0,imm=0) R6_w=ctx(id=0,off=0,imm=0) R10=fp0,call_-1 1: (61) r7 = *(u32 *)(r6 +28) invalid bpf_context access off=28 size=4Test case: bind4 load with invalid access:
src_ip6.. [PASS]
...
Test case: bind6 allow all .. [PASS]
Summary: 16 PASSED, 0 FAILEDSome
test_socktests are negative tests and verbose verifier log will be printed out as shown in the above.Signed-off-by Yonghong Song <[email protected]>--- tools/lib/bpf/bpf.c | 22 +++++++++++++++++----- tools/lib/bpf/bpf.h | 1 + tools/testing/selftests/bpf/test_sock.c | 9 ++++++++- 3 files changed, 26 insertions(+), 6 deletions(-)Changelog:
v2 -> v3:
. returning error immediately for inconsistent log_level/log_buf/log_buf_sz
values
. for log_level=1/2, always doing logging duringbpf_prog_load.
v1 -> v2:
. makelog_levelas the last member of structbpf_load_program_attr. . return-EINVALif bpf_load_program_attr.log_level > 2.tools/lib/bpf/bpf.c +17
-5@@ -214,10 +214,15 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, {void *finfo =NULL , *linfo =NULL ;union bpf_attr attr; +__u32 log_level;__u32 name_len;int fd; -if (!load_attr) +if (!load_attr || !log_buf != !log_buf_sz) +return -EINVAL ; + + log_level = load_attr->log_level; +if (log_level >2 || (log_level && !log_buf))return -EINVAL ; name_len = load_attr->name ?strlen (load_attr->name) :0 ;@@ -228,9 +233,16 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, attr.insn_cnt = (__u32 )load_attr->insns_cnt; attr.insns =ptr_to_u64 (load_attr->insns); attr.license =ptr_to_u64 (load_attr->license); - attr.log_buf =ptr_to_u64 (NULL ); - attr.log_size =0 ; - attr.log_level =0 ; + + attr.log_level = log_level; +if (log_level) { + attr.log_buf =ptr_to_u64 (log_buf); + attr.log_size = log_buf_sz; + }else { + attr.log_buf =ptr_to_u64 (NULL ); + attr.log_size =0 ; + } + attr.kern_version = load_attr->kern_version; attr.prog_ifindex = load_attr->prog_ifindex; attr.prog_btf_fd = load_attr->prog_btf_fd;@@ -286,7 +298,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, goto done; } -if (!log_buf || !log_buf_sz) +if (log_level || !log_buf) goto done;/* Try again with log */ tools/lib/bpf/bpf.h +1
-0@@ -85,6 +85,7 @@ struct bpf_load_program_attr {__u32 line_info_rec_size;const void *line_info;__u32 line_info_cnt; +__u32 log_level; };/* Flags to direct loading requirements */ tools/testing/selftests/bpf/test_sock.c +0
-0
tools/testing/selftests/bpf/test_sock.c line 23
Quoted 4 lines
tools/testing/selftests/bpf/test_sock.c +1
-0@@ -20,6 +20,7 @@#define MAX_INSNS 512char bpf_log_buf[BPF_LOG_BUF_SIZE ]; +static bool verbose = false;
If there was a command line flag to toggle 'verbose' it would have made it easier to test. In this form test_sock.c needs to be manually edited to see that log_level=2 is actually working.
Applied to bpf-next anyway.