From 6a70198e7480ce9deda3b405cb2d11cf49847540 Mon Sep 17 00:00:00 2001 From: Daniel Borkmann Date: Fri, 5 May 2023 13:21:41 +0000 Subject: [PATCH] libbpf: Add opts-based attach/detach/query API for tcx tbd Co-developed-by: Nikolay Aleksandrov Signed-off-by: Nikolay Aleksandrov Signed-off-by: Daniel Borkmann --- tools/lib/bpf/bpf.c | 5 +++++ tools/lib/bpf/bpf.h | 7 +++++++ tools/lib/bpf/libbpf.c | 42 +++++++++++++++++++++++++++++++++++----- tools/lib/bpf/libbpf.h | 17 ++++++++++++++++ tools/lib/bpf/libbpf.map | 1 + 5 files changed, 67 insertions(+), 5 deletions(-) diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index ba9f4fa80849a2..0918721d3c5eae 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c @@ -765,6 +765,11 @@ int bpf_link_create(int prog_fd, int target_fd, if (!OPTS_ZEROED(opts, tracing)) return libbpf_err(-EINVAL); break; + case BPF_TCX_INGRESS: + case BPF_TCX_EGRESS: + attr.link_create.tcx.relative_fd = OPTS_GET(opts, tcx.relative_fd, 0); + attr.link_create.tcx.expected_revision = OPTS_GET(opts, tcx.expected_revision, 0); + break; default: if (!OPTS_ZEROED(opts, flags)) return libbpf_err(-EINVAL); diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index 480c584a6f7fef..12591516dca0b3 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h @@ -370,6 +370,13 @@ struct bpf_link_create_opts { struct { __u64 cookie; } tracing; + struct { + union { + __u32 relative_fd; + __u32 relative_id; + }; + __u32 expected_revision; + } tcx; }; size_t :0; }; diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 8f681c964f66c7..58c2d80b92d45d 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -11683,11 +11683,10 @@ static int attach_lsm(const struct bpf_program *prog, long cookie, struct bpf_li } static struct bpf_link * -bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id, - const char *target_name) +bpf_program__attach_fd_opts(const struct bpf_program *prog, + const struct bpf_link_create_opts *opts, + int target_fd, const char *target_name) { - DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts, - .target_btf_id = btf_id); enum bpf_attach_type attach_type; char errmsg[STRERR_BUFSIZE]; struct bpf_link *link; @@ -11705,7 +11704,7 @@ bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id link->detach = &bpf_link__detach_fd; attach_type = bpf_program__expected_attach_type(prog); - link_fd = bpf_link_create(prog_fd, target_fd, attach_type, &opts); + link_fd = bpf_link_create(prog_fd, target_fd, attach_type, opts); if (link_fd < 0) { link_fd = -errno; free(link); @@ -11718,6 +11717,16 @@ bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id return link; } +static struct bpf_link * +bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id, + const char *target_name) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts, + .target_btf_id = btf_id); + + return bpf_program__attach_fd_opts(prog, &opts, target_fd, target_name); +} + struct bpf_link * bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd) { @@ -11736,6 +11745,29 @@ struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifi return bpf_program__attach_fd(prog, ifindex, 0, "xdp"); } +struct bpf_link * +bpf_program__attach_tcx_opts(const struct bpf_program *prog, + const struct bpf_tcx_opts *opts) +{ + DECLARE_LIBBPF_OPTS(bpf_link_create_opts, link_create_opts); + int ifindex = OPTS_GET(opts, ifindex, 0); + + if (!OPTS_VALID(opts, bpf_tcx_opts)) + return libbpf_err_ptr(-EINVAL); + if (!ifindex) { + pr_warn("prog '%s': target netdevice ifindex cannot be zero\n", + prog->name); + return libbpf_err_ptr(-EINVAL); + } + + link_create_opts.tcx.expected_revision = OPTS_GET(opts, expected_revision, 0); + link_create_opts.tcx.relative_fd = OPTS_GET(opts, relative_fd, 0); + link_create_opts.flags = OPTS_GET(opts, flags, 0); + + /* target_fd/target_ifindex use the same field in LINK_CREATE */ + return bpf_program__attach_fd_opts(prog, &link_create_opts, ifindex, "tc"); +} + 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 754da73c643b26..8ffba0f67c606d 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h @@ -718,6 +718,23 @@ LIBBPF_API struct bpf_link * bpf_program__attach_freplace(const struct bpf_program *prog, int target_fd, const char *attach_func_name); +struct bpf_tcx_opts { + /* size of this struct, for forward/backward compatibility */ + size_t sz; + int ifindex; + __u32 flags; + union { + __u32 relative_fd; + __u32 relative_id; + }; + __u32 expected_revision; +}; +#define bpf_tcx_opts__last_field expected_revision + +LIBBPF_API struct bpf_link * +bpf_program__attach_tcx_opts(const struct bpf_program *prog, + const struct bpf_tcx_opts *opts); + struct bpf_map; LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map); diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map index a29b90e9713c31..f66b714512c2aa 100644 --- a/tools/lib/bpf/libbpf.map +++ b/tools/lib/bpf/libbpf.map @@ -396,4 +396,5 @@ LIBBPF_1.3.0 { global: bpf_obj_pin_opts; bpf_prog_detach_opts; + bpf_program__attach_tcx_opts; } LIBBPF_1.2.0;