[PATCH RFC bpf-next 15/52] libbpf: add bpf_program__attach_xdp_opts()
From: Alexander Lobakin
Date: Tue Jun 28 2022 - 15:54:24 EST
Add a version of bpf_program__attach_xdp() which can take an
optional pointer to &bpf_xdp_attach_opts to carry opts from it to
bpf_link_create(), primarily to be able to specify a BTF/type ID and
a metadata threshold when attaching an XDP program.
This struct is originally designed for bpf_xdp_{at,de}tach(), reuse
it here as well to not spawn entities (with ::old_prog_fd reused for
XDP flags via union).
Signed-off-by: Alexander Lobakin <alexandr.lobakin@xxxxxxxxx>
---
tools/lib/bpf/libbpf.c | 16 ++++++++++++++++
tools/lib/bpf/libbpf.h | 27 ++++++++++++++++++---------
tools/lib/bpf/libbpf.map | 1 +
3 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index f4014c09f1cf..b6cc238a2657 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -12010,6 +12010,22 @@ struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifi
return bpf_program__attach_fd(prog, ifindex, NULL, "xdp");
}
+struct bpf_link *
+bpf_program__attach_xdp_opts(const struct bpf_program *prog, int ifindex,
+ const struct bpf_xdp_attach_opts *opts)
+{
+ LIBBPF_OPTS(bpf_link_create_opts, lc_opts);
+
+ if (!OPTS_VALID(opts, bpf_xdp_attach_opts))
+ return libbpf_err_ptr(-EINVAL);
+
+ lc_opts.flags = OPTS_GET(opts, flags, 0);
+ lc_opts.xdp.btf_id = OPTS_GET(opts, btf_id, 0);
+ lc_opts.xdp.meta_thresh = OPTS_GET(opts, meta_thresh, 0);
+
+ return bpf_program__attach_fd(prog, ifindex, &lc_opts, "xdp");
+}
+
struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd,
const char *attach_func_name)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 99ac94f148fc..d6dd05b5b753 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -678,8 +678,26 @@ LIBBPF_API struct bpf_link *
bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd);
LIBBPF_API struct bpf_link *
bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
+
+struct bpf_xdp_attach_opts {
+ size_t sz;
+ union {
+ int old_prog_fd;
+ /* for bpf_program__attach_xdp_opts() */
+ __u32 flags;
+ };
+ __u32 meta_thresh;
+ __u64 btf_id;
+ size_t :0;
+};
+#define bpf_xdp_attach_opts__last_field btf_id
+
LIBBPF_API struct bpf_link *
bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
+LIBBPF_API struct bpf_link *
+bpf_program__attach_xdp_opts(const struct bpf_program *prog, int ifindex,
+ const struct bpf_xdp_attach_opts *opts);
+
LIBBPF_API struct bpf_link *
bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, const char *attach_func_name);
@@ -1210,15 +1228,6 @@ LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_query() instead")
LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
size_t info_size, __u32 flags);
-struct bpf_xdp_attach_opts {
- size_t sz;
- int old_prog_fd;
- __u32 meta_thresh;
- __u64 btf_id;
- size_t :0;
-};
-#define bpf_xdp_attach_opts__last_field btf_id
-
struct bpf_xdp_query_opts {
size_t sz;
__u32 prog_id; /* output */
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index f0987df15b7a..d14bbf82e37c 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -464,6 +464,7 @@ LIBBPF_1.0.0 {
global:
btf__add_enum64;
btf__add_enum64_value;
+ bpf_program__attach_xdp_opts;
libbpf_bpf_attach_type_str;
libbpf_bpf_link_type_str;
libbpf_bpf_map_type_str;
--
2.36.1