Skip to content

Commit 1d67116

Browse files
Yonghong Songanakryiko
authored andcommitted
libbpf: Support link-based cgroup attach with options
Currently libbpf supports bpf_program__attach_cgroup() with signature: LIBBPF_API struct bpf_link * bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd); To support mprog style attachment, additionsl fields like flags, relative_{fd,id} and expected_revision are needed. Add a new API: LIBBPF_API struct bpf_link * bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd, const struct bpf_cgroup_opts *opts); where bpf_cgroup_opts contains all above needed fields. Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 1209339 commit 1d67116

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

tools/lib/bpf/bpf.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,50 @@ int bpf_link_create(int prog_fd, int target_fd,
837837
if (!OPTS_ZEROED(opts, netkit))
838838
return libbpf_err(-EINVAL);
839839
break;
840+
case BPF_CGROUP_INET_INGRESS:
841+
case BPF_CGROUP_INET_EGRESS:
842+
case BPF_CGROUP_INET_SOCK_CREATE:
843+
case BPF_CGROUP_INET_SOCK_RELEASE:
844+
case BPF_CGROUP_INET4_BIND:
845+
case BPF_CGROUP_INET6_BIND:
846+
case BPF_CGROUP_INET4_POST_BIND:
847+
case BPF_CGROUP_INET6_POST_BIND:
848+
case BPF_CGROUP_INET4_CONNECT:
849+
case BPF_CGROUP_INET6_CONNECT:
850+
case BPF_CGROUP_UNIX_CONNECT:
851+
case BPF_CGROUP_INET4_GETPEERNAME:
852+
case BPF_CGROUP_INET6_GETPEERNAME:
853+
case BPF_CGROUP_UNIX_GETPEERNAME:
854+
case BPF_CGROUP_INET4_GETSOCKNAME:
855+
case BPF_CGROUP_INET6_GETSOCKNAME:
856+
case BPF_CGROUP_UNIX_GETSOCKNAME:
857+
case BPF_CGROUP_UDP4_SENDMSG:
858+
case BPF_CGROUP_UDP6_SENDMSG:
859+
case BPF_CGROUP_UNIX_SENDMSG:
860+
case BPF_CGROUP_UDP4_RECVMSG:
861+
case BPF_CGROUP_UDP6_RECVMSG:
862+
case BPF_CGROUP_UNIX_RECVMSG:
863+
case BPF_CGROUP_SOCK_OPS:
864+
case BPF_CGROUP_DEVICE:
865+
case BPF_CGROUP_SYSCTL:
866+
case BPF_CGROUP_GETSOCKOPT:
867+
case BPF_CGROUP_SETSOCKOPT:
868+
case BPF_LSM_CGROUP:
869+
relative_fd = OPTS_GET(opts, cgroup.relative_fd, 0);
870+
relative_id = OPTS_GET(opts, cgroup.relative_id, 0);
871+
if (relative_fd && relative_id)
872+
return libbpf_err(-EINVAL);
873+
if (relative_id) {
874+
attr.link_create.cgroup.relative_id = relative_id;
875+
attr.link_create.flags |= BPF_F_ID;
876+
} else {
877+
attr.link_create.cgroup.relative_fd = relative_fd;
878+
}
879+
attr.link_create.cgroup.expected_revision =
880+
OPTS_GET(opts, cgroup.expected_revision, 0);
881+
if (!OPTS_ZEROED(opts, cgroup))
882+
return libbpf_err(-EINVAL);
883+
break;
840884
default:
841885
if (!OPTS_ZEROED(opts, flags))
842886
return libbpf_err(-EINVAL);

tools/lib/bpf/bpf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ struct bpf_link_create_opts {
438438
__u32 relative_id;
439439
__u64 expected_revision;
440440
} netkit;
441+
struct {
442+
__u32 relative_fd;
443+
__u32 relative_id;
444+
__u64 expected_revision;
445+
} cgroup;
441446
};
442447
size_t :0;
443448
};

tools/lib/bpf/libbpf.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12837,6 +12837,34 @@ struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifi
1283712837
return bpf_program_attach_fd(prog, ifindex, "xdp", NULL);
1283812838
}
1283912839

12840+
struct bpf_link *
12841+
bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
12842+
const struct bpf_cgroup_opts *opts)
12843+
{
12844+
LIBBPF_OPTS(bpf_link_create_opts, link_create_opts);
12845+
__u32 relative_id;
12846+
int relative_fd;
12847+
12848+
if (!OPTS_VALID(opts, bpf_cgroup_opts))
12849+
return libbpf_err_ptr(-EINVAL);
12850+
12851+
relative_id = OPTS_GET(opts, relative_id, 0);
12852+
relative_fd = OPTS_GET(opts, relative_fd, 0);
12853+
12854+
if (relative_fd && relative_id) {
12855+
pr_warn("prog '%s': relative_fd and relative_id cannot be set at the same time\n",
12856+
prog->name);
12857+
return libbpf_err_ptr(-EINVAL);
12858+
}
12859+
12860+
link_create_opts.cgroup.expected_revision = OPTS_GET(opts, expected_revision, 0);
12861+
link_create_opts.cgroup.relative_fd = relative_fd;
12862+
link_create_opts.cgroup.relative_id = relative_id;
12863+
link_create_opts.flags = OPTS_GET(opts, flags, 0);
12864+
12865+
return bpf_program_attach_fd(prog, cgroup_fd, "cgroup", &link_create_opts);
12866+
}
12867+
1284012868
struct bpf_link *
1284112869
bpf_program__attach_tcx(const struct bpf_program *prog, int ifindex,
1284212870
const struct bpf_tcx_opts *opts)

tools/lib/bpf/libbpf.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,21 @@ LIBBPF_API struct bpf_link *
877877
bpf_program__attach_netkit(const struct bpf_program *prog, int ifindex,
878878
const struct bpf_netkit_opts *opts);
879879

880+
struct bpf_cgroup_opts {
881+
/* size of this struct, for forward/backward compatibility */
882+
size_t sz;
883+
__u32 flags;
884+
__u32 relative_fd;
885+
__u32 relative_id;
886+
__u64 expected_revision;
887+
size_t :0;
888+
};
889+
#define bpf_cgroup_opts__last_field expected_revision
890+
891+
LIBBPF_API struct bpf_link *
892+
bpf_program__attach_cgroup_opts(const struct bpf_program *prog, int cgroup_fd,
893+
const struct bpf_cgroup_opts *opts);
894+
880895
struct bpf_map;
881896

882897
LIBBPF_API struct bpf_link *bpf_map__attach_struct_ops(const struct bpf_map *map);

tools/lib/bpf/libbpf.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ LIBBPF_1.6.0 {
437437
bpf_linker__add_fd;
438438
bpf_linker__new_fd;
439439
bpf_object__prepare;
440+
bpf_program__attach_cgroup_opts;
440441
bpf_program__func_info;
441442
bpf_program__func_info_cnt;
442443
bpf_program__line_info;

0 commit comments

Comments
 (0)