]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
libbpf: Introduce bpf_prog_stream_read() API
authorKumar Kartikeya Dwivedi <memxor@gmail.com>
Thu, 3 Jul 2025 20:48:16 +0000 (13:48 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 4 Jul 2025 02:30:07 +0000 (19:30 -0700)
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 <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20250703204818.925464-11-memxor@gmail.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 6eb421ccf91bde378c7cd44b454ffab5051bc562..ab40dbf9f020fb04addaa45bcddf9ac5dc80eea5 100644 (file)
@@ -1375,3 +1375,23 @@ int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts)
        fd = sys_bpf_fd(BPF_TOKEN_CREATE, &attr, attr_sz);
        return libbpf_err_errno(fd);
 }
+
+int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
+                        struct bpf_prog_stream_read_opts *opts)
+{
+       const size_t attr_sz = offsetofend(union bpf_attr, prog_stream_read);
+       union bpf_attr attr;
+       int err;
+
+       if (!OPTS_VALID(opts, bpf_prog_stream_read_opts))
+               return libbpf_err(-EINVAL);
+
+       memset(&attr, 0, attr_sz);
+       attr.prog_stream_read.stream_buf = ptr_to_u64(buf);
+       attr.prog_stream_read.stream_buf_len = buf_len;
+       attr.prog_stream_read.stream_id = stream_id;
+       attr.prog_stream_read.prog_fd = prog_fd;
+
+       err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz);
+       return libbpf_err_errno(err);
+}
index 1342564214c8b57c1f417ae07e02f382975936ca..7252150e7ad35759f2486b66c550d7d61719b4cd 100644 (file)
@@ -709,6 +709,27 @@ struct bpf_token_create_opts {
 LIBBPF_API int bpf_token_create(int bpffs_fd,
                                struct bpf_token_create_opts *opts);
 
+struct bpf_prog_stream_read_opts {
+       size_t sz;
+       size_t :0;
+};
+#define bpf_prog_stream_read_opts__last_field sz
+/**
+ * @brief **bpf_prog_stream_read** reads data from the BPF stream of a given BPF
+ * program.
+ *
+ * @param prog_fd FD for the BPF program whose BPF stream is to be read.
+ * @param stream_id ID of the BPF stream to be read.
+ * @param buf Buffer to read data into from the BPF stream.
+ * @param buf_len Maximum number of bytes to read from the BPF stream.
+ * @param opts optional options, can be NULL
+ *
+ * @return The number of bytes read, on success; negative error code, otherwise
+ * (errno is also set to the error code)
+ */
+LIBBPF_API int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
+                                   struct bpf_prog_stream_read_opts *opts);
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
index c7fc0bde56486554ff8b4fd2fa002271c76b2cd2..1bbf77326420ee99cbf6dd0c1313f18735d5163e 100644 (file)
@@ -437,6 +437,7 @@ LIBBPF_1.6.0 {
                bpf_linker__add_fd;
                bpf_linker__new_fd;
                bpf_object__prepare;
+               bpf_prog_stream_read;
                bpf_program__attach_cgroup_opts;
                bpf_program__func_info;
                bpf_program__func_info_cnt;