bpftool-cgroup(8)
πΈ NAME πΈ
bpftool-cgroup β the tool for inspection and simple manipulation of eBPF progs, but make it cgroup-flavored π
(this is page 8 of the System Manager's Manual, bestie, we're deep in the system-admin section now β¨)
β¨ SYNOPSIS β¨
bpftool [OPTIONS] cgroup COMMAND
OPTIONS := { { -j | --json } [{ -p | --pretty }] | { -d | --debug } | {
-f | --bpffs } }
COMMANDS := { show | list | tree | attach | detach | help }
π CGROUP COMMANDS π
okay so here's the whole menu of things you can do, in their exact little outfits:
bpftool cgroup { show | list } CGROUP [effective]
bpftool cgroup tree [CGROUP_ROOT] [effective]
bpftool cgroup attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]
bpftool cgroup detach CGROUP ATTACH_TYPE PROG
bpftool cgroup help
PROG := { id PROG_ID | pinned FILE | tag PROG_TAG | name PROG_NAME }
ATTACH_TYPE := { cgroup_inet_ingress | cgroup_inet_egress |
cgroup_inet_sock_create | cgroup_sock_ops |
cgroup_device | cgroup_inet4_bind | cgroup_inet6_bind |
cgroup_inet4_post_bind | cgroup_inet6_post_bind |
cgroup_inet4_connect | cgroup_inet6_connect |
cgroup_unix_connect | cgroup_inet4_getpeername |
cgroup_inet6_getpeername | cgroup_unix_getpeername |
cgroup_inet4_getsockname | cgroup_inet6_getsockname |
cgroup_unix_getsockname | cgroup_udp4_sendmsg |
cgroup_udp6_sendmsg | cgroup_unix_sendmsg |
cgroup_udp4_recvmsg | cgroup_udp6_recvmsg |
cgroup_unix_recvmsg | cgroup_sysctl |
cgroup_getsockopt | cgroup_setsockopt |
cgroup_inet_sock_release }
ATTACH_FLAGS := { multi | override }
π¦ DESCRIPTION π¦
bpftool cgroup { show | list } CGROUP [effective]
Spill the tea on every program attached to the cgroup CGROUP π
The output opens with the program ID, then comes the attach type, the attach flags, and the program name. Slay, an order.
If you say effective, you get the effective programs β the ones that will actually run for events inside a cgroup. That means inherited ones plus the attached ones, all together π«Ά
bpftool cgroup tree [CGROUP_ROOT] [effective]
This one walks over every cgroup inside CGROUP_ROOT and lists all the attached programs. If you don't hand it a CGROUP_ROOT, bpftool just uses the cgroup v2 mountpoint. (Convenient, iconic, no notes β¨)
The output is giving very similar energy to cgroup show/list: it starts with the absolute cgroup path, then the program ID, attach type, attach flags, and program name.
Say effective and again you get the effective programs that will execute for events within a cgroup β inherited ones and attached ones π
bpftool cgroup attach CGROUP ATTACH_TYPE PROG [ATTACH_FLAGS]
Attach program PROG to cgroup CGROUP, using attach type ATTACH_TYPE and optional ATTACH_FLAGS. Main character move tbh.
ATTACH_FLAGS can be one of these two:
overrideβ if a sub-cgroup installs some bpf program, the program in this cgroup yields to the sub-cgroup program πmultiβ if a sub-cgroup installs some bpf program, that cgroup program gets run in addition to the program in this cgroup π₯
Now the rules, listen up:
Only one program is allowed to be attached to a cgroup with no attach flags, or with the override flag. Attach another one and the old program gets released and the new one takes its place. No cap.
Multiple programs are allowed to attach to a cgroup with multi. They run in FIFO order β first attached, first to run π
Non-default ATTACH_FLAGS are supported by kernel version 4.14 and later.
And ATTACH_TYPE can be one of:
ingressβ ingress path of the inet socket (since 4.10)egressβ egress path of the inet socket (since 4.10)sock_createβ opening of an inet socket (since 4.10)sock_opsβ various socket operations (since 4.12)deviceβ device access (since 4.15)bind4β call tobind(2)for an inet4 socket (since 4.17)bind6β call tobind(2)for an inet6 socket (since 4.17)post_bind4β return frombind(2)for an inet4 socket (since 4.17)post_bind6β return frombind(2)for an inet6 socket (since 4.17)connect4β call toconnect(2)for an inet4 socket (since 4.17)connect6β call toconnect(2)for an inet6 socket (since 4.17)connect_unixβ call toconnect(2)for a unix socket (since 6.7)sendmsg4β call tosendto(2),sendmsg(2),sendmmsg(2)for an unconnected udp4 socket (since 4.18)sendmsg6β call tosendto(2),sendmsg(2),sendmmsg(2)for an unconnected udp6 socket (since 4.18)sendmsg_unixβ call tosendto(2),sendmsg(2),sendmmsg(2)for an unconnected unix socket (since 6.7)recvmsg4β call torecvfrom(2),recvmsg(2),recvmmsg(2)for an unconnected udp4 socket (since 5.2)recvmsg6β call torecvfrom(2),recvmsg(2),recvmmsg(2)for an unconnected udp6 socket (since 5.2)recvmsg_unixβ call torecvfrom(2),recvmsg(2),recvmmsg(2)for an unconnected unix socket (since 6.7)sysctlβ sysctl access (since 5.2)getsockoptβ call togetsockopt(since 5.3)setsockoptβ call tosetsockopt(since 5.3)getpeername4β call togetpeername(2)for an inet4 socket (since 5.8)getpeername6β call togetpeername(2)for an inet6 socket (since 5.8)getpeername_unixβ call togetpeername(2)for a unix socket (since 6.7)getsockname4β call togetsockname(2)for an inet4 socket (since 5.8)getsockname6β call togetsockname(2)for an inet6 socket (since 5.8)getsockname_unixβ call togetsockname(2)for a unix socket (since 6.7)sock_releaseβ closing a userspace inet socket (since 5.9)
bpftool cgroup detach CGROUP ATTACH_TYPE PROG
Detach PROG from the cgroup CGROUP, with attach type ATTACH_TYPE. Bye bestie ππ
bpftool prog help
Print short help message.
π OPTIONS π
-h,--helpβ print short help message (similar tobpftool help). Your lil safety net π«Ά-V,--versionβ printbpftool's version number (similar tobpftool version), the number of thelibbpfversion in use, and the optional features that were included whenbpftoolwas compiled. Those optional features include linking against LLVM orlibbfdto provide the disassembler for JIT-ted programs (bpftool prog dump jited) and usage of BPF skeletons (some features likebpftool prog profileor showing pids associated to BPF objects may rely on it).-j,--jsonβ generate JSON output. For commands that cannot produce JSON, this option has no effect. (Not everything is JSON-able, and that's fine π )-p,--prettyβ generate human-readable JSON output. Implies-j. So pretty, so readable β¨-d,--debugβ print all logs available, even debug-level information. This includes logs fromlibbpfas well as from the verifier, when attempting to load programs. It's giving: everything, all at once π-f,--bpffsβ show file names of pinned programs. Pinned prog era π₯
π₯ EXAMPLES π₯
Watch and learn, this is the whole ritual:
# mount -t bpf none /sys/fs/bpf/
# mkdir /sys/fs/cgroup/test.slice
# bpftool prog load ./device_cgroup.o /sys/fs/bpf/prog
# bpftool cgroup attach /sys/fs/cgroup/test.slice/ device id 1 allow_multi
# bpftool cgroup list /sys/fs/cgroup/test.slice/
ID AttachType AttachFlags Name
1 device allow_multi bpf_prog1
# bpftool cgroup detach /sys/fs/cgroup/test.slice/ device id 1
# bpftool cgroup list /sys/fs/cgroup/test.slice/
ID AttachType AttachFlags Name
And then the list is justβ¦ empty. Ate and left no crumbs, literally π (that blank output is the point β the detach worked π)
π SEE ALSO π
Go read the whole friend group, they're all icons:
bpf(2)bpf-helpers(7)bpftool(8)bpftool-btf(8)bpftool-feature(8)bpftool-gen(8)bpftool-iter(8)bpftool-link(8)bpftool-map(8)bpftool-net(8)bpftool-perf(8)bpftool-prog(8)bpftool-struct_ops(8)bpftool-token(8)
okay that's the whole page, bestie π this was bpftool-cgroup(8) from the System Manager's Manual β section 8, system administration, main character energy only π go forth and attach your progs responsibly, mwah π§ββοΈβ
π¦ where this page comes from
- arch rolling: bpf 7.3.1-1 Β· GPL-2.0-only
- ubuntu stonking: bpftool 7.8.0+7.3.0-10.10
- ubuntu stonking: bpftool 7.8.0+7.3.0-9.9
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms