Debian Codegraph @

link/cgroup.go

raw symbols SCIP
1 //go:build !windows
2
3 package link
4
5 import (
6 "errors"
7 "fmt"
8 "os"
9
10 "github.com/cilium/ebpf"
11 "github.com/cilium/ebpf/internal/sys"
12 )
13
14 type cgroupAttachFlags uint32
15
16 const (
17 // Allow programs attached to sub-cgroups to override the verdict of this
18 // program.
19 flagAllowOverride cgroupAttachFlags = 1 << iota
20 // Allow attaching multiple programs to the cgroup. Only works if the cgroup
21 // has zero or more programs attached using the Multi flag. Implies override.
22 flagAllowMulti
23 // Set automatically by progAttachCgroup.Update(). Used for updating a
24 // specific given program attached in multi-mode.
25 flagReplace
26 )
27
28 type CgroupOptions struct {
29 // Path to a cgroupv2 folder.
30 Path string
31 // One of the AttachCgroup* constants
32 Attach ebpf.AttachType
33 // Program must be of type CGroup*, and the attach type must match Attach.
34 Program *ebpf.Program
35 }
36
37 // AttachCgroup links a BPF program to a cgroup.
38 //
39 // If the running kernel doesn't support bpf_link, attempts to emulate its
40 // semantics using the legacy PROG_ATTACH mechanism. If bpf_link is not
41 // available, the returned [Link] will not support pinning to bpffs.
42 //
43 // If you need more control over attachment flags or the attachment mechanism
44 // used, look at [RawAttachProgram] and [AttachRawLink] instead.
45 func AttachCgroup(opts CgroupOptions) (cg Link, err error) {
46 cgroup, err := os.Open(opts.Path)
47 if err != nil {
48 return nil, fmt.Errorf("can't open cgroup: %s", err)
49 }
50 defer func() {
51 if _, ok := cg.(*progAttachCgroup); ok {
52 // Skip closing the cgroup handle if we return a valid progAttachCgroup,
53 // where the handle is retained to implement Update().
54 return
55 }
56 cgroup.Close()
57 }()
58
59 cg, err = newLinkCgroup(cgroup, opts.Attach, opts.Program)
60 if err == nil {
61 return cg, nil
62 }
63
64 if errors.Is(err, ErrNotSupported) {
65 cg, err = newProgAttachCgroup(cgroup, opts.Attach, opts.Program, flagAllowMulti)
66 }
67 if errors.Is(err, ErrNotSupported) {
68 cg, err = newProgAttachCgroup(cgroup, opts.Attach, opts.Program, flagAllowOverride)
69 }
70 if err != nil {
71 return nil, err
72 }
73
74 return cg, nil
75 }
76
77 type progAttachCgroup struct {
78 cgroup *os.File
79 current *ebpf.Program
80 attachType ebpf.AttachType
81 flags cgroupAttachFlags
82 }
83
84 var _ Link = (*progAttachCgroup)(nil)
85
86 func (cg *progAttachCgroup) isLink() {}
87
88 // newProgAttachCgroup attaches prog to cgroup using BPF_PROG_ATTACH.
89 // cgroup and prog are retained by [progAttachCgroup].
90 func newProgAttachCgroup(cgroup *os.File, attach ebpf.AttachType, prog *ebpf.Program, flags cgroupAttachFlags) (*progAttachCgroup, error) {
91 if flags&flagAllowMulti > 0 {
92 if err := haveProgAttachReplace(); err != nil {
93 return nil, fmt.Errorf("can't support multiple programs: %w", err)
94 }
95 }
96
97 // Use a program handle that cannot be closed by the caller.
98 clone, err := prog.Clone()
99 if err != nil {
100 return nil, err
101 }
102
103 err = RawAttachProgram(RawAttachProgramOptions{
104 Target: int(cgroup.Fd()),
105 Program: clone,
106 Flags: uint32(flags),
107 Attach: attach,
108 })
109 if err != nil {
110 clone.Close()
111 return nil, fmt.Errorf("cgroup: %w", err)
112 }
113
114 return &progAttachCgroup{cgroup, clone, attach, flags}, nil
115 }
116
117 func (cg *progAttachCgroup) Close() error {
118 defer cg.cgroup.Close()
119 defer cg.current.Close()
120
121 err := RawDetachProgram(RawDetachProgramOptions{
122 Target: int(cg.cgroup.Fd()),
123 Program: cg.current,
124 Attach: cg.attachType,
125 })
126 if err != nil {
127 return fmt.Errorf("close cgroup: %s", err)
128 }
129 return nil
130 }
131
132 func (cg *progAttachCgroup) Update(prog *ebpf.Program) error {
133 new, err := prog.Clone()
134 if err != nil {
135 return err
136 }
137
138 args := RawAttachProgramOptions{
139 Target: int(cg.cgroup.Fd()),
140 Program: prog,
141 Attach: cg.attachType,
142 Flags: uint32(cg.flags),
143 }
144
145 if cg.flags&flagAllowMulti > 0 {
146 // Atomically replacing multiple programs requires at least
147 // 5.5 (commit 7dd68b3279f17921 "bpf: Support replacing cgroup-bpf
148 // program in MULTI mode")
149 args.Anchor = ReplaceProgram(cg.current)
150 }
151
152 if err := RawAttachProgram(args); err != nil {
153 new.Close()
154 return fmt.Errorf("can't update cgroup: %s", err)
155 }
156
157 cg.current.Close()
158 cg.current = new
159 return nil
160 }
161
162 func (cg *progAttachCgroup) Pin(string) error {
163 return fmt.Errorf("can't pin cgroup: %w", ErrNotSupported)
164 }
165
166 func (cg *progAttachCgroup) Unpin() error {
167 return fmt.Errorf("can't unpin cgroup: %w", ErrNotSupported)
168 }
169
170 func (cg *progAttachCgroup) Detach() error {
171 return fmt.Errorf("can't detach cgroup: %w", ErrNotSupported)
172 }
173
174 func (cg *progAttachCgroup) Info() (*Info, error) {
175 return nil, fmt.Errorf("can't get cgroup info: %w", ErrNotSupported)
176 }
177
178 type linkCgroup struct {
179 RawLink
180 }
181
182 var _ Link = (*linkCgroup)(nil)
183
184 // newLinkCgroup attaches prog to cgroup using BPF_LINK_CREATE.
185 func newLinkCgroup(cgroup *os.File, attach ebpf.AttachType, prog *ebpf.Program) (*linkCgroup, error) {
186 link, err := AttachRawLink(RawLinkOptions{
187 Target: int(cgroup.Fd()),
188 Program: prog,
189 Attach: attach,
190 })
191 if err != nil {
192 return nil, err
193 }
194
195 return &linkCgroup{*link}, err
196 }
197
198 func (cg *linkCgroup) Info() (*Info, error) {
199 var info sys.CgroupLinkInfo
200 if err := sys.ObjInfo(cg.fd, &info); err != nil {
201 return nil, fmt.Errorf("cgroup link info: %s", err)
202 }
203 extra := &CgroupInfo{
204 CgroupId: info.CgroupId,
205 AttachType: info.AttachType,
206 }
207
208 return &Info{
209 info.Type,
210 info.Id,
211 ebpf.ProgramID(info.ProgId),
212 extra,
213 }, nil
214 }