Skip to content

Commit 1a70a53

Browse files
KentaTadaqmonnet
authored andcommitted
bpftool: Query only cgroup-related attach types
When CONFIG_NETKIT=y, bpftool-cgroup shows error even if the cgroup's path is correct: $ bpftool cgroup tree /sys/fs/cgroup CgroupPath ID AttachType AttachFlags Name Error: can't query bpf programs attached to /sys/fs/cgroup: No such device or address >From strace and kernel tracing, I found netkit returned ENXIO and this command failed. I think this AttachType(BPF_NETKIT_PRIMARY) is not relevant to cgroup. bpftool-cgroup should query just only cgroup-related attach types. v2->v3: - removed an unnecessary check v1->v2: - used an array of cgroup attach types Signed-off-by: Kenta Tada <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 5f63dbb commit 1a70a53

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/cgroup.c

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,38 @@
1919

2020
#include "main.h"
2121

22+
static const int cgroup_attach_types[] = {
23+
BPF_CGROUP_INET_INGRESS,
24+
BPF_CGROUP_INET_EGRESS,
25+
BPF_CGROUP_INET_SOCK_CREATE,
26+
BPF_CGROUP_INET_SOCK_RELEASE,
27+
BPF_CGROUP_INET4_BIND,
28+
BPF_CGROUP_INET6_BIND,
29+
BPF_CGROUP_INET4_POST_BIND,
30+
BPF_CGROUP_INET6_POST_BIND,
31+
BPF_CGROUP_INET4_CONNECT,
32+
BPF_CGROUP_INET6_CONNECT,
33+
BPF_CGROUP_UNIX_CONNECT,
34+
BPF_CGROUP_INET4_GETPEERNAME,
35+
BPF_CGROUP_INET6_GETPEERNAME,
36+
BPF_CGROUP_UNIX_GETPEERNAME,
37+
BPF_CGROUP_INET4_GETSOCKNAME,
38+
BPF_CGROUP_INET6_GETSOCKNAME,
39+
BPF_CGROUP_UNIX_GETSOCKNAME,
40+
BPF_CGROUP_UDP4_SENDMSG,
41+
BPF_CGROUP_UDP6_SENDMSG,
42+
BPF_CGROUP_UNIX_SENDMSG,
43+
BPF_CGROUP_UDP4_RECVMSG,
44+
BPF_CGROUP_UDP6_RECVMSG,
45+
BPF_CGROUP_UNIX_RECVMSG,
46+
BPF_CGROUP_SOCK_OPS,
47+
BPF_CGROUP_DEVICE,
48+
BPF_CGROUP_SYSCTL,
49+
BPF_CGROUP_GETSOCKOPT,
50+
BPF_CGROUP_SETSOCKOPT,
51+
BPF_LSM_CGROUP
52+
};
53+
2254
#define HELP_SPEC_ATTACH_FLAGS \
2355
"ATTACH_FLAGS := { multi | override }"
2456

@@ -183,13 +215,13 @@ static int count_attached_bpf_progs(int cgroup_fd, enum bpf_attach_type type)
183215

184216
static int cgroup_has_attached_progs(int cgroup_fd)
185217
{
186-
enum bpf_attach_type type;
218+
unsigned int i = 0;
187219
bool no_prog = true;
188220

189-
for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
190-
int count = count_attached_bpf_progs(cgroup_fd, type);
221+
for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++) {
222+
int count = count_attached_bpf_progs(cgroup_fd, cgroup_attach_types[i]);
191223

192-
if (count < 0 && errno != EINVAL)
224+
if (count < 0)
193225
return -1;
194226

195227
if (count > 0) {

0 commit comments

Comments
 (0)