Skip to content

Commit

Permalink
libbpf: Add support for BPF tc link
Browse files Browse the repository at this point in the history
Implement tc BPF link support for libbpf. The bpf_program__attach_fd()
API has been refactored slightly in order to pass bpf_link_create_opts.
A new bpf_program__attach_tcx() has been added on top of this which
allows for passing ifindex and other relevant parameters.

Co-developed-by: Nikolay Aleksandrov <[email protected]>
Signed-off-by: Nikolay Aleksandrov <[email protected]>
Signed-off-by: Daniel Borkmann <[email protected]>
  • Loading branch information
borkmann committed May 17, 2023
1 parent 947f61c commit d0b894b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
4 changes: 4 additions & 0 deletions tools/lib/bpf/bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,10 @@ 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);
default:
if (!OPTS_ZEROED(opts, flags))
return libbpf_err(-EINVAL);
Expand Down
7 changes: 7 additions & 0 deletions tools/lib/bpf/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,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;
};
Expand Down
42 changes: 37 additions & 5 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -11566,11 +11566,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;
Expand All @@ -11588,7 +11587,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);
Expand All @@ -11601,6 +11600,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)
{
Expand All @@ -11619,6 +11628,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_revision(const struct bpf_program *prog, int ifindex,
__u32 flags, __u32 relative_object,
__u32 expected_revision)
{
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
.tcx.expected_revision = expected_revision,
.tcx.relative_fd = relative_object,
.flags = flags,
);

/* target_fd/target_ifindex use the same field in LINK_CREATE */
return bpf_program__attach_fd_opts(prog, &opts, ifindex, "tc");
}

struct bpf_link *
bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
__u32 flags, __u32 relative_object)
{
return bpf_program__attach_tcx_revision(prog, ifindex, flags,
relative_object, 0);
}

struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd,
const char *attach_func_name)
Expand Down
7 changes: 7 additions & 0 deletions tools/lib/bpf/libbpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,13 @@ bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd);
LIBBPF_API struct bpf_link *
bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex);
LIBBPF_API struct bpf_link *
bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
__u32 flags, __u32 relative_object);
LIBBPF_API struct bpf_link *
bpf_program__attach_tcx_revision(const struct bpf_program *prog, int ifindex,
__u32 flags, __u32 relative_object,
__u32 expected_revision);
LIBBPF_API struct bpf_link *
bpf_program__attach_freplace(const struct bpf_program *prog,
int target_fd, const char *attach_func_name);

Expand Down
2 changes: 2 additions & 0 deletions tools/lib/bpf/libbpf.map
Original file line number Diff line number Diff line change
Expand Up @@ -395,4 +395,6 @@ LIBBPF_1.2.0 {
LIBBPF_1.3.0 {
global:
bpf_prog_detach_opts;
bpf_program__attach_tcx;
bpf_program__attach_tcx_revision;
} LIBBPF_1.2.0;

0 comments on commit d0b894b

Please sign in to comment.