Skip to content

Commit f2c7910

Browse files
liu-song-6kernel-patches-bot
authored andcommitted
libbpf: support test run of raw tracepoint programs
Add bpf_prog_test_run_opts() with support of new fields in bpf_attr.test, namely, flags and cpu. Also extend _opts operations to support outputs via opts. Acked-by: Andrii Nakryiko <andriin@fb.com> Signed-off-by: Song Liu <songliubraving@fb.com>
1 parent 6bec95c commit f2c7910

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,37 @@ int bpf_prog_test_run_xattr(struct bpf_prog_test_run_attr *test_attr)
712712
return ret;
713713
}
714714

715+
int bpf_prog_test_run_opts(int prog_fd, struct bpf_test_run_opts *opts)
716+
{
717+
union bpf_attr attr;
718+
int ret;
719+
720+
if (!OPTS_VALID(opts, bpf_test_run_opts))
721+
return -EINVAL;
722+
723+
memset(&attr, 0, sizeof(attr));
724+
attr.test.prog_fd = prog_fd;
725+
attr.test.cpu = OPTS_GET(opts, cpu, 0);
726+
attr.test.flags = OPTS_GET(opts, flags, 0);
727+
attr.test.repeat = OPTS_GET(opts, repeat, 0);
728+
attr.test.duration = OPTS_GET(opts, duration, 0);
729+
attr.test.ctx_size_in = OPTS_GET(opts, ctx_size_in, 0);
730+
attr.test.ctx_size_out = OPTS_GET(opts, ctx_size_out, 0);
731+
attr.test.data_size_in = OPTS_GET(opts, data_size_in, 0);
732+
attr.test.data_size_out = OPTS_GET(opts, data_size_out, 0);
733+
attr.test.ctx_in = ptr_to_u64(OPTS_GET(opts, ctx_in, NULL));
734+
attr.test.ctx_out = ptr_to_u64(OPTS_GET(opts, ctx_out, NULL));
735+
attr.test.data_in = ptr_to_u64(OPTS_GET(opts, data_in, NULL));
736+
attr.test.data_out = ptr_to_u64(OPTS_GET(opts, data_out, NULL));
737+
738+
ret = sys_bpf(BPF_PROG_TEST_RUN, &attr, sizeof(attr));
739+
OPTS_SET(opts, data_size_out, attr.test.data_size_out);
740+
OPTS_SET(opts, ctx_size_out, attr.test.ctx_size_out);
741+
OPTS_SET(opts, duration, attr.test.duration);
742+
OPTS_SET(opts, retval, attr.test.retval);
743+
return ret;
744+
}
745+
715746
static int bpf_obj_get_next_id(__u32 start_id, __u32 *next_id, int cmd)
716747
{
717748
union bpf_attr attr;

tools/lib/bpf/bpf.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,32 @@ struct bpf_prog_bind_opts {
251251

252252
LIBBPF_API int bpf_prog_bind_map(int prog_fd, int map_fd,
253253
const struct bpf_prog_bind_opts *opts);
254+
255+
struct bpf_test_run_opts {
256+
size_t sz; /* size of this struct for forward/backward compatibility */
257+
const void *data_in; /* optional */
258+
void *data_out; /* optional */
259+
__u32 data_size_in;
260+
__u32 data_size_out; /* in: max length of data_out
261+
* out: length of data_out
262+
*/
263+
const void *ctx_in; /* optional */
264+
void *ctx_out; /* optional */
265+
__u32 ctx_size_in;
266+
__u32 ctx_size_out; /* in: max length of ctx_out
267+
* out: length of cxt_out
268+
*/
269+
__u32 retval; /* out: return code of the BPF program */
270+
int repeat;
271+
__u32 duration; /* out: average per repetition in ns */
272+
__u32 flags;
273+
__u32 cpu;
274+
};
275+
#define bpf_test_run_opts__last_field cpu
276+
277+
LIBBPF_API int bpf_prog_test_run_opts(int prog_fd,
278+
struct bpf_test_run_opts *opts);
279+
254280
#ifdef __cplusplus
255281
} /* extern "C" */
256282
#endif

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ LIBBPF_0.1.0 {
303303
LIBBPF_0.2.0 {
304304
global:
305305
bpf_prog_bind_map;
306+
bpf_prog_test_run_opts;
306307
bpf_program__section_name;
307308
perf_buffer__buffer_cnt;
308309
perf_buffer__buffer_fd;

tools/lib/bpf/libbpf_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ static inline bool libbpf_validate_opts(const char *opts,
136136
((opts) && opts->sz >= offsetofend(typeof(*(opts)), field))
137137
#define OPTS_GET(opts, field, fallback_value) \
138138
(OPTS_HAS(opts, field) ? (opts)->field : fallback_value)
139+
#define OPTS_SET(opts, field, value) \
140+
do { \
141+
if (OPTS_HAS(opts, field)) \
142+
(opts)->field = value; \
143+
} while (0)
139144

140145
int parse_cpu_mask_str(const char *s, bool **mask, int *mask_sz);
141146
int parse_cpu_mask_file(const char *fcpu, bool **mask, int *mask_sz);

0 commit comments

Comments
 (0)