[PATCH RFC bpf-next 13/52] libbpf: add ability to set the meta threshold on setting XDP prog

Alexander Lobakin
2022-06-28T19:51:49Z

Covered functions:
* bpf_link_create() - via &bpf_link_create_ops;
* bpf_link_update() - via &bpf_link_update_ops;
* bpf_xdp_attach() - via &bpf_xdp_attach_ops;
* bpf_set_link_xdp_fd_opts() - via &bpf_xdp_set_link_opts;

No support for bpf_get_link_xdp_info()/&xdp_link_info as we store
additional data in the kernel in BPF link mode only.

Signed-off-by: Alexander Lobakin <alexandr.lobakin@intel.com>
---
tools/lib/bpf/bpf.c | 3 +++
tools/lib/bpf/bpf.h | 8 +++++++-
tools/lib/bpf/libbpf.h | 4 ++--
tools/lib/bpf/netlink.c | 10 ++++++++++
4 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 6036dc75cc7b..e7c713a418f6 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -807,6 +807,9 @@ int bpf_link_create(int prog_fd, int target_fd,
break;
case BPF_XDP:
attr.link_create.xdp.btf_id = OPTS_GET(opts, xdp.btf_id, 0);
+ attr.link_create.xdp.meta_thresh = OPTS_GET(opts,
+ xdp.meta_thresh,
+ 0);
if (!OPTS_ZEROED(opts, xdp))
return libbpf_err(-EINVAL);
break;
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 4e17995fdaff..c0f54f24d675 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -385,6 +385,8 @@ struct bpf_link_create_opts {
struct {
/* target metadata BTF + type ID */
__aligned_u64 btf_id;
+ /* frame size to start composing XDP metadata from */
+ __u32 meta_thresh;
} xdp;
};
size_t :0;
@@ -408,11 +410,15 @@ struct bpf_link_update_opts {
struct {
/* new target metadata BTF + type ID */
__aligned_u64 new_btf_id;
+ /* new frame size to start composing XDP
+ * metadata from
+ */
+ __u32 new_meta_thresh;
} xdp;
};
size_t :0;
};
-#define bpf_link_update_opts__last_field xdp.new_btf_id
+#define bpf_link_update_opts__last_field xdp.new_meta_thresh

LIBBPF_API int bpf_link_update(int link_fd, int new_prog_fd,
const struct bpf_link_update_opts *opts);
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 4f77128ba770..99ac94f148fc 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1193,7 +1193,7 @@ struct xdp_link_info {
struct bpf_xdp_set_link_opts {
size_t sz;
int old_fd;
- __u32 :32;
+ __u32 meta_thresh;
__u64 btf_id;
size_t :0;
};
@@ -1213,7 +1213,7 @@ LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
struct bpf_xdp_attach_opts {
size_t sz;
int old_prog_fd;
- __u32 :32;
+ __u32 meta_thresh;
__u64 btf_id;
size_t :0;
};
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 104a809d5fb2..ac2a87243ecd 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -236,6 +236,7 @@ struct __bpf_set_link_xdp_fd_opts {
int old_fd;
__u32 flags;
__u64 btf_id;
+ __u32 meta_thresh;
};

static int
@@ -276,6 +277,13 @@ __bpf_set_link_xdp_fd_replace(const struct __bpf_set_link_xdp_fd_opts *opts)
if (ret < 0)
return ret;
}
+ if (opts->meta_thresh) {
+ ret = nlattr_add(&req, IFLA_XDP_META_THRESH,
+ &opts->meta_thresh,
+ sizeof(opts->meta_thresh));
+ if (ret < 0)
+ return ret;
+ }
nlattr_end_nested(&req, nla);

return libbpf_netlink_send_recv(&req, NULL, NULL, NULL);
@@ -300,6 +308,7 @@ int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, const struct bpf_xdp_a
sl_opts.old_fd = -1;

sl_opts.btf_id = OPTS_GET(opts, btf_id, 0);
+ sl_opts.meta_thresh = OPTS_GET(opts, meta_thresh, 0);

err = __bpf_set_link_xdp_fd_replace(&sl_opts);
return libbpf_err(err);
@@ -330,6 +339,7 @@ int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
}

sl_opts.btf_id = OPTS_GET(opts, btf_id, 0);
+ sl_opts.meta_thresh = OPTS_GET(opts, meta_thresh, 0);

ret = __bpf_set_link_xdp_fd_replace(&sl_opts);
return libbpf_err(ret);
--
2.36.1