Skip to content

Commit 3bbc1ba

Browse files
kkdwivediAlexei Starovoitov
authored andcommitted
libbpf: Introduce bpf_prog_stream_read() API
Introduce a libbpf API so that users can read data from a given BPF stream for a BPF prog fd. For now, only the low-level syscall wrapper is provided, we can add a bpf_program__* accessor as a follow up if needed. Acked-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 21a3afc commit 3bbc1ba

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,3 +1375,23 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts)
13751375
fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz);
13761376
return libbpf_err_errno(fd);
13771377
}
1378+
1379+
int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
1380+
struct bpf_prog_stream_read_opts *opts)
1381+
{
1382+
const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read);
1383+
union bpf_attr attr;
1384+
int err;
1385+
1386+
if (!OPTS_VALID(opts, bpf_prog_stream_read_opts))
1387+
return libbpf_err(-EINVAL);
1388+
1389+
memset(&attr, 0, attr_sz);
1390+
attr.prog_stream_read.stream_buf = ptr_to_u64(buf);
1391+
attr.prog_stream_read.stream_buf_len = buf_len;
1392+
attr.prog_stream_read.stream_id = stream_id;
1393+
attr.prog_stream_read.prog_fd = prog_fd;
1394+
1395+
err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz);
1396+
return libbpf_err_errno(err);
1397+
}

tools/lib/bpf/bpf.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,27 @@ struct bpf_token_create_opts {
709709
LIBBPF_API int bpf_token_create(int bpffs_fd,
710710
struct bpf_token_create_opts *opts);
711711

712+
struct bpf_prog_stream_read_opts {
713+
size_t sz;
714+
size_t :0;
715+
};
716+
#define bpf_prog_stream_read_opts__last_field sz
717+
/**
718+
* @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF
719+
* program.
720+
*
721+
* @param prog_fd FD for the BPF program whose BPF stream is to be read.
722+
* @param stream_id ID of the BPF stream to be read.
723+
* @param buf Buffer to read data into from the BPF stream.
724+
* @param buf_len Maximum number of bytes to read from the BPF stream.
725+
* @param opts optional options, can be NULL
726+
*
727+
* @return The number of bytes read, on success; negative error code, otherwise
728+
* (errno is also set to the error code)
729+
*/
730+
LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
731+
struct bpf_prog_stream_read_opts *opts);
732+
712733
#ifdef __cplusplus
713734
} /* extern "C" */
714735
#endif

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ LIBBPF_1.6.0 {
437437
bpf_linker__add_fd;
438438
bpf_linker__new_fd;
439439
bpf_object__prepare;
440+
bpf_prog_stream_read;
440441
bpf_program__attach_cgroup_opts;
441442
bpf_program__func_info;
442443
bpf_program__func_info_cnt;

0 commit comments

Comments
 (0)