[PATCH RFC bpf-next 12/52] libbpf: add ability to set the BTF/type ID on setting XDP prog

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

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;

bpf_link_update() got the ability to pass arbitrary link
type-specific data to the kernel, not just the old and new FDs.
No support for bpf_get_link_xdp_info()/&xdp_link_info as we store
additional data such as flags and BTF ID in the kernel in BPF link
mode only.

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

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index 240186aac8e6..6036dc75cc7b 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -805,6 +805,11 @@ int bpf_link_create(int prog_fd, int target_fd,
if (!OPTS_ZEROED(opts, tracing))
return libbpf_err(-EINVAL);
break;
+ case BPF_XDP:
+ attr.link_create.xdp.btf_id = OPTS_GET(opts, xdp.btf_id, 0);
+ if (!OPTS_ZEROED(opts, xdp))
+ return libbpf_err(-EINVAL);
+ break;
default:
if (!OPTS_ZEROED(opts, flags))
return libbpf_err(-EINVAL);
@@ -872,6 +877,20 @@ int bpf_link_update(int link_fd, int new_prog_fd,
attr.link_update.flags = OPTS_GET(opts, flags, 0);
attr.link_update.old_prog_fd = OPTS_GET(opts, old_prog_fd, 0);

+ /* As the union in both @attr and @opts is unnamed, just use a pointer
+ * to any of its members to copy the whole rest of the union/opts
+ */
+ if (opts && opts->sz > offsetof(typeof(*opts), xdp)) {
+ __u32 attr_left, opts_left;
+
+ attr_left = sizeof(attr.link_update) -
+ offsetof(typeof(attr.link_update), xdp);
+ opts_left = opts->sz - offsetof(typeof(*opts), xdp);
+
+ memcpy(&attr.link_update.xdp, &opts->xdp,
+ min(attr_left, opts_left));
+ }
+
ret = sys_bpf(BPF_LINK_UPDATE, &attr, sizeof(attr));
return libbpf_err_errno(ret);
}
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index cabc03703e29..4e17995fdaff 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -382,6 +382,10 @@ struct bpf_link_create_opts {
struct {
__u64 cookie;
} tracing;
+ struct {
+ /* target metadata BTF + type ID */
+ __aligned_u64 btf_id;
+ } xdp;
};
size_t :0;
};
@@ -397,8 +401,18 @@ struct bpf_link_update_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
__u32 flags; /* extra flags */
__u32 old_prog_fd; /* expected old program FD */
+ /* must have the same layout as the same union from
+ * bpf_attr::link_update, uses direct memcpy() to there
+ */
+ union {
+ struct {
+ /* new target metadata BTF + type ID */
+ __aligned_u64 new_btf_id;
+ } xdp;
+ };
+ size_t :0;
};
-#define bpf_link_update_opts__last_field old_prog_fd
+#define bpf_link_update_opts__last_field xdp.new_btf_id

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 4056e9038086..4f77128ba770 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -1193,9 +1193,11 @@ struct xdp_link_info {
struct bpf_xdp_set_link_opts {
size_t sz;
int old_fd;
+ __u32 :32;
+ __u64 btf_id;
size_t :0;
};
-#define bpf_xdp_set_link_opts__last_field old_fd
+#define bpf_xdp_set_link_opts__last_field btf_id

LIBBPF_DEPRECATED_SINCE(0, 8, "use bpf_xdp_attach() instead")
LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
@@ -1211,9 +1213,11 @@ 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;
+ __u64 btf_id;
size_t :0;
};
-#define bpf_xdp_attach_opts__last_field old_prog_fd
+#define bpf_xdp_attach_opts__last_field btf_id

struct bpf_xdp_query_opts {
size_t sz;
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 3a25178d0d12..104a809d5fb2 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -235,6 +235,7 @@ struct __bpf_set_link_xdp_fd_opts {
int fd;
int old_fd;
__u32 flags;
+ __u64 btf_id;
};

static int
@@ -269,6 +270,12 @@ __bpf_set_link_xdp_fd_replace(const struct __bpf_set_link_xdp_fd_opts *opts)
if (ret < 0)
return ret;
}
+ if (opts->btf_id) {
+ ret = nlattr_add(&req, IFLA_XDP_BTF_ID, &opts->btf_id,
+ sizeof(opts->btf_id));
+ if (ret < 0)
+ return ret;
+ }
nlattr_end_nested(&req, nla);

return libbpf_netlink_send_recv(&req, NULL, NULL, NULL);
@@ -292,6 +299,8 @@ int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags, const struct bpf_xdp_a
else
sl_opts.old_fd = -1;

+ sl_opts.btf_id = OPTS_GET(opts, btf_id, 0);
+
err = __bpf_set_link_xdp_fd_replace(&sl_opts);
return libbpf_err(err);
}
@@ -320,6 +329,8 @@ int bpf_set_link_xdp_fd_opts(int ifindex, int fd, __u32 flags,
flags |= XDP_FLAGS_REPLACE;
}

+ sl_opts.btf_id = OPTS_GET(opts, btf_id, 0);
+
ret = __bpf_set_link_xdp_fd_replace(&sl_opts);
return libbpf_err(ret);
}
--
2.36.1