Skip to content

Commit

Permalink
bpftool: Query only cgroup-related attach types
Browse files Browse the repository at this point in the history
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 <tadakentaso@gmail.com>
Reviewed-by: Quentin Monnet <qmo@kernel.org>
Link: https://lore.kernel.org/r/20240607111704.6716-1-tadakentaso@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
KentaTada authored and qmonnet committed Jun 27, 2024
1 parent 5f63dbb commit 1a70a53
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/cgroup.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@

#include "main.h"

static const int cgroup_attach_types[] = {
BPF_CGROUP_INET_INGRESS,
BPF_CGROUP_INET_EGRESS,
BPF_CGROUP_INET_SOCK_CREATE,
BPF_CGROUP_INET_SOCK_RELEASE,
BPF_CGROUP_INET4_BIND,
BPF_CGROUP_INET6_BIND,
BPF_CGROUP_INET4_POST_BIND,
BPF_CGROUP_INET6_POST_BIND,
BPF_CGROUP_INET4_CONNECT,
BPF_CGROUP_INET6_CONNECT,
BPF_CGROUP_UNIX_CONNECT,
BPF_CGROUP_INET4_GETPEERNAME,
BPF_CGROUP_INET6_GETPEERNAME,
BPF_CGROUP_UNIX_GETPEERNAME,
BPF_CGROUP_INET4_GETSOCKNAME,
BPF_CGROUP_INET6_GETSOCKNAME,
BPF_CGROUP_UNIX_GETSOCKNAME,
BPF_CGROUP_UDP4_SENDMSG,
BPF_CGROUP_UDP6_SENDMSG,
BPF_CGROUP_UNIX_SENDMSG,
BPF_CGROUP_UDP4_RECVMSG,
BPF_CGROUP_UDP6_RECVMSG,
BPF_CGROUP_UNIX_RECVMSG,
BPF_CGROUP_SOCK_OPS,
BPF_CGROUP_DEVICE,
BPF_CGROUP_SYSCTL,
BPF_CGROUP_GETSOCKOPT,
BPF_CGROUP_SETSOCKOPT,
BPF_LSM_CGROUP
};

#define HELP_SPEC_ATTACH_FLAGS \
"ATTACH_FLAGS := { multi | override }"

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

static int cgroup_has_attached_progs(int cgroup_fd)
{
enum bpf_attach_type type;
unsigned int i = 0;
bool no_prog = true;

for (type = 0; type < __MAX_BPF_ATTACH_TYPE; type++) {
int count = count_attached_bpf_progs(cgroup_fd, type);
for (i = 0; i < ARRAY_SIZE(cgroup_attach_types); i++) {
int count = count_attached_bpf_progs(cgroup_fd, cgroup_attach_types[i]);

if (count < 0 && errno != EINVAL)
if (count < 0)
return -1;

if (count > 0) {
Expand Down

0 comments on commit 1a70a53

Please sign in to comment.