Skip to content

Commit 64a9759

Browse files
lmbAlexei Starovoitov
authored andcommitted
libbpf: add bpf_prog_test_run_xattr
Add a new function, which encourages safe usage of the test interface. bpf_prog_test_run continues to work as before, but should be considered unsafe. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 2587a97 commit 64a9759

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,29 @@ int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
463463
return ret;
464464
}
465465

466+
int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
467+
{
468+
union bpf_attr attr;
469+
int ret;
470+
471+
if (!test_attr->data_out && test_attr->data_size_out > 0)
472+
return -EINVAL;
473+
474+
bzero(&attr, sizeof(attr));
475+
attr.test.prog_fd = test_attr->prog_fd;
476+
attr.test.data_in = ptr_to_u64(test_attr->data_in);
477+
attr.test.data_out = ptr_to_u64(test_attr->data_out);
478+
attr.test.data_size_in = test_attr->data_size_in;
479+
attr.test.data_size_out = test_attr->data_size_out;
480+
attr.test.repeat = test_attr->repeat;
481+
482+
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
483+
test_attr->data_size_out = attr.test.data_size_out;
484+
test_attr->retval = attr.test.retval;
485+
test_attr->duration = attr.test.duration;
486+
return ret;
487+
}
488+
466489
int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
467490
{
468491
union bpf_attr attr;

tools/lib/bpf/bpf.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
118118
LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
119119
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
120120
enum bpf_attach_type type);
121+
122+
struct bpf_prog_test_run_attr {
123+
int prog_fd;
124+
int repeat;
125+
const void *data_in;
126+
__u32 data_size_in;
127+
void *data_out; /* optional */
128+
__u32 data_size_out; /* in: max length of data_out
129+
* out: length of data_out */
130+
__u32 retval; /* out: return code of the BPF program */
131+
__u32 duration; /* out: average per repetition in ns */
132+
};
133+
134+
LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
135+
136+
/*
137+
* bpf_prog_test_run does not check that data_out is large enough. Consider
138+
* using bpf_prog_test_run_xattr instead.
139+
*/
121140
LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
122141
__u32 size, void *data_out, __u32 *size_out,
123142
__u32 *retval, __u32 *duration);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ LIBBPF_0.0.1 {
6565
bpf_prog_load_xattr;
6666
bpf_prog_query;
6767
bpf_prog_test_run;
68+
bpf_prog_test_run_xattr;
6869
bpf_program__fd;
6970
bpf_program__is_kprobe;
7071
bpf_program__is_perf_event;

0 commit comments

Comments
 (0)