libbpf: add bpf_prog_test_run_xattr
authorLorenz Bauer <lmb@cloudflare.com>
Mon, 3 Dec 2018 11:31:25 +0000 (11:31 +0000)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 4 Dec 2018 16:18:13 +0000 (08:18 -0800)
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>
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h
tools/lib/bpf/libbpf.map

index c19226cccf39f7c52ce3a6eaedcd5db7e3521fb7..5c3be06bf0ddfa73fc9bcc9f940aa4b1555e8874 100644 (file)
@@ -463,6 +463,29 @@ int bpf_prog_test_run(int prog_fd, int repeat, void *data, __u32 size,
        return ret;
 }
 
+int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
+{
+       union bpf_attr attr;
+       int ret;
+
+       if (!test_attr->data_out && test_attr->data_size_out > 0)
+               return -EINVAL;
+
+       bzero(&attr, sizeof(attr));
+       attr.test.prog_fd = test_attr->prog_fd;
+       attr.test.data_in = ptr_to_u64(test_attr->data_in);
+       attr.test.data_out = ptr_to_u64(test_attr->data_out);
+       attr.test.data_size_in = test_attr->data_size_in;
+       attr.test.data_size_out = test_attr->data_size_out;
+       attr.test.repeat = test_attr->repeat;
+
+       ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
+       test_attr->data_size_out = attr.test.data_size_out;
+       test_attr->retval = attr.test.retval;
+       test_attr->duration = attr.test.duration;
+       return ret;
+}
+
 int bpf_prog_get_next_id(__u32 start_id, __u32 *next_id)
 {
        union bpf_attr attr;
index 60392b70587ca1645479c319dd8552e179153e2f..098e6f793b7653ece3a98c959944d261d768d6e0 100644 (file)
@@ -118,6 +118,25 @@ LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
 LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
 LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
                                enum bpf_attach_type type);
+
+struct bpf_prog_test_run_attr {
+       int prog_fd;
+       int repeat;
+       const void *data_in;
+       __u32 data_size_in;
+       void *data_out;      /* optional */
+       __u32 data_size_out; /* in: max length of data_out
+                             * out: length of data_out */
+       __u32 retval;        /* out: return code of the BPF program */
+       __u32 duration;      /* out: average per repetition in ns */
+};
+
+LIBBPF_API int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr);
+
+/*
+ * bpf_prog_test_run does not check that data_out is large enough. Consider
+ * using bpf_prog_test_run_xattr instead.
+ */
 LIBBPF_API int bpf_prog_test_run(int prog_fd, int repeat, void *data,
                                 __u32 size, void *data_out, __u32 *size_out,
                                 __u32 *retval, __u32 *duration);
index 4fb29f6d7a80b4a6b0dc1037ab1cf65727baf64d..8deff22d61bba2a52989ec8845bf4846b210b258 100644 (file)
@@ -65,6 +65,7 @@ LIBBPF_0.0.1 {
                bpf_prog_load_xattr;
                bpf_prog_query;
                bpf_prog_test_run;
+               bpf_prog_test_run_xattr;
                bpf_program__fd;
                bpf_program__is_kprobe;
                bpf_program__is_perf_event;