aya-rs / aya

Aya is an eBPF library for the Rust programming language, built with a focus on developer experience and operability.
https://aya-rs.dev/book/
Apache License 2.0
3.1k stars 273 forks source link

Can't attach a CGroup program #1014

Closed rani-sharim closed 1 month ago

rani-sharim commented 1 month ago

Kernel version: 5.10.220-209.869.amzn2.x86_64

Every cgroup template I try to execute fails to attach the program (I replaced the cgroup path from /sys/fs/cgroup/unified to /sys/fs/cgroup since it doesn't exist on my system) fails with the error

Error: `bpf_link_create` failed

Caused by:
    Bad file descriptor (os error 9)

Would appreciate help figuring out why?

reyzell commented 1 month ago

Can you share the exact command you're running? Does the directory exist where you're trying to set up the mount point? Here are a few commands to look around that might give more context:

# Look for "cgroup" and "cgroup2" file systems via:
grep cgroup /proc/filesystems

# See what cgroups are mounted via:
grep cgroup /proc/mounts

# Determine if v1 or v2 are enabled via:
stat -fc %T /sys/fs/cgroup

# Get a feel for bpf and cgroup layout via:
tree -L 2 /sys/fs
rani-sharim commented 1 month ago

Edit: I understood from this that cgroup2 wasn't enabled. I enabled it and it seems to work now. Thanks!

What I'm doing is :

  1. Generating a new project using cargo generate http://github.com/aya-rs/aya-template and choosing for example cgroup_sockopt as project type.
  2. Trying to run the project using RUST_LOG=info cargo xtask run --release.
  3. This tries to attach to /sys/fs/cgroup/unified which doesn't exist on my system, so i change it to /sys/fs/cgroup (since from what i understood this is the root now).

As for the other commands

$ grep cgroup /proc/filesystems
nodev   cgroup
nodev   cgroup2

$ grep cgroup /proc/mounts
tmpfs /sys/fs/cgroup tmpfs ro,nosuid,nodev,noexec,mode=755 0 0
cgroup /sys/fs/cgroup/systemd cgroup rw,nosuid,nodev,noexec,relatime,xattr,release_agent=/usr/lib/systemd/systemd-cgroups-agent,name=systemd 0 0
cgroup /sys/fs/cgroup/blkio cgroup rw,nosuid,nodev,noexec,relatime,blkio 0 0
cgroup /sys/fs/cgroup/cpu,cpuacct cgroup rw,nosuid,nodev,noexec,relatime,cpu,cpuacct 0 0
cgroup /sys/fs/cgroup/net_cls,net_prio cgroup rw,nosuid,nodev,noexec,relatime,net_cls,net_prio 0 0
cgroup /sys/fs/cgroup/perf_event cgroup rw,nosuid,nodev,noexec,relatime,perf_event 0 0
cgroup /sys/fs/cgroup/freezer cgroup rw,nosuid,nodev,noexec,relatime,freezer 0 0
cgroup /sys/fs/cgroup/pids cgroup rw,nosuid,nodev,noexec,relatime,pids 0 0
cgroup /sys/fs/cgroup/devices cgroup rw,nosuid,nodev,noexec,relatime,devices 0 0
cgroup /sys/fs/cgroup/cpuset cgroup rw,nosuid,nodev,noexec,relatime,cpuset 0 0
cgroup /sys/fs/cgroup/memory cgroup rw,nosuid,nodev,noexec,relatime,memory 0 0
cgroup /sys/fs/cgroup/hugetlb cgroup rw,nosuid,nodev,noexec,relatime,hugetlb 0 0

$ stat -fc %T /sys/fs/cgroup
tmpfs

$ tree -L 2 /sys/fs
/sys/fs
|-- bpf
|-- cgroup
|   |-- blkio
|   |-- cpu -> cpu,cpuacct
|   |-- cpuacct -> cpu,cpuacct
|   |-- cpu,cpuacct
|   |-- cpuset
|   |-- devices
|   |-- freezer
|   |-- hugetlb
|   |-- memory
|   |-- net_cls -> net_cls,net_prio
|   |-- net_cls,net_prio
|   |-- net_prio -> net_cls,net_prio
|   |-- perf_event
|   |-- pids
|   `-- systemd
|-- pstore [error opening dir]
`-- xfs
    |-- stats
    `-- xvda1

21 directories, 0 files