From: Amery Hung <ameryhung@gmail.com>
To: bpf@vger.kernel.org
Cc: netdev@vger.kernel.org, alexei.starovoitov@gmail.com,
andrii@kernel.org, daniel@iogearbox.net, tj@kernel.org,
martin.lau@kernel.org, ameryhung@gmail.com, kernel-team@meta.com
Subject: [RFC PATCH v1 bpf-next 3/4] libbpf: Add bpf_struct_ops_associate_prog() API
Date: Fri, 10 Oct 2025 10:49:52 -0700 [thread overview]
Message-ID: <20251010174953.2884682-4-ameryhung@gmail.com> (raw)
In-Reply-To: <20251010174953.2884682-1-ameryhung@gmail.com>
Add low-level wrapper API for BPF_STRUCT_OPS_ASSOCIATE_PROG command in
bpf() syscall.
Signed-off-by: Amery Hung <ameryhung@gmail.com>
---
tools/lib/bpf/bpf.c | 18 ++++++++++++++++++
tools/lib/bpf/bpf.h | 19 +++++++++++++++++++
tools/lib/bpf/libbpf.map | 1 +
3 files changed, 38 insertions(+)
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 339b19797237..230fc2fa98f9 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -1397,3 +1397,21 @@ int bpf_prog_stream_read(int prog_fd, __u32 stream_id, void *buf, __u32 buf_len,
err = sys_bpf(BPF_PROG_STREAM_READ_BY_FD, &attr, attr_sz);
return libbpf_err_errno(err);
}
+
+int bpf_struct_ops_associate_prog(int map_fd, int prog_fd,
+ struct bpf_struct_ops_associate_prog_opts *opts)
+{
+ const size_t attr_sz = offsetofend(union bpf_attr, struct_ops_assoc_prog);
+ union bpf_attr attr;
+ int err;
+
+ if (!OPTS_VALID(opts, bpf_struct_ops_associate_prog_opts))
+ return libbpf_err(-EINVAL);
+
+ memset(&attr, 0, attr_sz);
+ attr.struct_ops_assoc_prog.map_fd = map_fd;
+ attr.struct_ops_assoc_prog.prog_fd = prog_fd;
+
+ err = sys_bpf(BPF_STRUCT_OPS_ASSOCIATE_PROG, &attr, attr_sz);
+ return libbpf_err_errno(err);
+}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index e983a3e40d61..99fe189ca7c6 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -733,6 +733,25 @@ struct bpf_prog_stream_read_opts {
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);
+struct bpf_struct_ops_associate_prog_opts {
+ size_t sz;
+ size_t :0;
+};
+#define bpf_struct_ops_associate_prog_opts__last_field sz
+/**
+ * @brief **bpf_struct_ops_associate_prog** associate a BPF program with a
+ * struct_ops map.
+ *
+ * @param map_fd FD for the struct_ops map to be associated with a BPF progam
+ * @param prog_fd FD for the BPF program
+ * @param opts optional options, can be NULL
+ *
+ * @return 0 on success; negative error code, otherwise (errno is also set to
+ * the error code)
+ */
+LIBBPF_API int bpf_struct_ops_associate_prog(int map_fd, int prog_fd,
+ struct bpf_struct_ops_associate_prog_opts *opts);
+
#ifdef __cplusplus
} /* extern "C" */
#endif
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 8ed8749907d4..3a156a663210 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -451,4 +451,5 @@ LIBBPF_1.7.0 {
global:
bpf_map__set_exclusive_program;
bpf_map__exclusive_program;
+ bpf_struct_ops_associate_prog;
} LIBBPF_1.6.0;
--
2.47.3
next prev parent reply other threads:[~2025-10-10 17:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-10 17:49 [RFC PATCH v1 bpf-next 0/4] Support associating BPF programs with struct_ops Amery Hung
2025-10-10 17:49 ` [RFC PATCH v1 bpf-next 1/4] bpf: Allow verifier to fixup kernel module kfuncs Amery Hung
2025-10-14 0:10 ` Andrii Nakryiko
2025-10-16 18:29 ` Amery Hung
2025-10-10 17:49 ` [RFC PATCH v1 bpf-next 2/4] bpf: Support associating BPF program with struct_ops Amery Hung
2025-10-14 0:10 ` Andrii Nakryiko
2025-10-15 22:35 ` Amery Hung
2025-10-16 18:41 ` Andrii Nakryiko
2025-10-10 17:49 ` Amery Hung [this message]
2025-10-14 0:10 ` [RFC PATCH v1 bpf-next 3/4] libbpf: Add bpf_struct_ops_associate_prog() API Andrii Nakryiko
2025-10-10 17:49 ` [RFC PATCH v1 bpf-next 4/4] selftests/bpf: Test BPF_STRUCT_OPS_ASSOCIATE_PROG command Amery Hung
2025-10-14 0:10 ` Andrii Nakryiko
2025-10-16 18:32 ` Amery Hung
2025-10-14 0:10 ` [RFC PATCH v1 bpf-next 0/4] Support associating BPF programs with struct_ops Andrii Nakryiko
2025-10-16 18:30 ` Amery Hung
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251010174953.2884682-4-ameryhung@gmail.com \
--to=ameryhung@gmail.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrii@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=kernel-team@meta.com \
--cc=martin.lau@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).