Debian Codegraph @

tests/linux_kernel/helpers/test_bpf.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import contextlib
5 import errno
6 import os
7 import resource
8 import unittest
9
10 from _drgn_util.platform import NORMALIZED_MACHINE_NAME
11 from drgn import cast
12 from drgn.helpers.linux.bpf import (
13 bpf_btf_for_each,
14 bpf_link_for_each,
15 bpf_map_by_id,
16 bpf_map_for_each,
17 bpf_prog_by_id,
18 bpf_prog_for_each,
19 bpf_prog_used_maps,
20 cgroup_bpf_prog_for_each,
21 cgroup_bpf_prog_for_each_effective,
22 )
23 from drgn.helpers.linux.cgroup import cgroup_get_from_path
24 from drgn.helpers.linux.fs import fget
25 from drgn.helpers.linux.pid import find_task
26 from tests.linux_kernel import LinuxKernelTestCase
27 from tests.linux_kernel.bpf import (
28 BPF_CGROUP_INET_INGRESS,
29 BPF_EXIT_INSN,
30 BPF_F_ALLOW_MULTI,
31 BPF_LD_MAP_FD,
32 BPF_MAP_TYPE_HASH,
33 BPF_MOV64_IMM,
34 BPF_PROG_TYPE_CGROUP_SKB,
35 BPF_PROG_TYPE_SOCKET_FILTER,
36 BPF_REG_0,
37 _SYS_bpf,
38 bpf_btf_ids,
39 bpf_link_create,
40 bpf_link_ids,
41 bpf_map_create,
42 bpf_map_get_info_by_fd,
43 bpf_map_ids,
44 bpf_prog_attach,
45 bpf_prog_get_info_by_fd,
46 bpf_prog_ids,
47 bpf_prog_load,
48 )
49 from tests.linux_kernel.helpers.test_cgroup import tmp_cgroups
50
51
52 class BpfTestCase(LinuxKernelTestCase):
53 INSNS = (
54 BPF_MOV64_IMM(BPF_REG_0, 0),
55 BPF_EXIT_INSN(),
56 )
57
58 @classmethod
59 def setUpClass(cls):
60 super().setUpClass()
61 if _SYS_bpf is None:
62 raise unittest.SkipTest(
63 f"bpf syscall number is not known on {NORMALIZED_MACHINE_NAME}"
64 )
65 # Before the patch series culminating in Linux kernel commit
66 # 3ac1f01b43b6 ("bpf: Eliminate rlimit-based memory accounting for bpf
67 # progs") (in v5.11), BPF program and map memory usage was limited by
68 # RLIMIT_MEMLOCK. At that time (before Linux kernel commit 9dcc38e2813e
69 # ("Increase default MLOCK_LIMIT to 8 MiB") (in v5.16)), the limit was
70 # only 64kB. We only allocate a few small objects at a time, but with
71 # 64k pages, we can easily blow that limit.
72 memlock_limit = 8 * 1024 * 1024
73 old_limit = resource.getrlimit(resource.RLIMIT_MEMLOCK)
74 if old_limit[0] < memlock_limit:
75 resource.setrlimit(
76 resource.RLIMIT_MEMLOCK,
77 (memlock_limit, max(memlock_limit, old_limit[1])),
78 )
79 cls.addClassCleanup(resource.setrlimit, resource.RLIMIT_MEMLOCK, old_limit)
80 try:
81 os.close(bpf_map_create(BPF_MAP_TYPE_HASH, 8, 8, 8))
82 except OSError as e:
83 if e.errno != errno.ENOSYS:
84 raise
85 raise unittest.SkipTest(
86 "kernel does not support bpf syscall (CONFIG_BPF_SYSCALL)"
87 )
88
89
90 class TestBpf(BpfTestCase):
91 def test_bpf_btf_for_each(self):
92 # BTF was added in Linux kernel commit 69b693f0aefa ("bpf: btf:
93 # Introduce BPF Type Format (BTF)") (in v4.18) and had IDs from the
94 # start, but there was no API to get them until commit 1b9ed84ecf26
95 # ("bpf: add new BPF_BTF_GET_NEXT_ID syscall command") (in v5.4). The
96 # only kernel version that we support in between is v4.19, which we can
97 # live without testing.
98 #
99 # Note that before Linux kernel commits 5329722057d4 ("bpf: Assign ID
100 # to vmlinux BTF and return extra info for BTF in GET_OBJ_INFO") and
101 # 36e68442d1af ("bpf: Load and verify kernel module BTFs") (in v5.11),
102 # there won't be any BTF IDs unless they were explicitly added outside
103 # of the test suite.
104 try:
105 expected_ids = list(bpf_btf_ids())
106 except OSError as e:
107 if e.errno != errno.EINVAL:
108 raise
109 self.skipTest("kernel does not support BPF_BTF_GET_NEXT_ID")
110 self.assertCountEqual(
111 [btf.id.value_() for btf in bpf_btf_for_each(self.prog)], expected_ids
112 )
113
114 def test_bpf_link_for_each(self):
115 with tmp_cgroups() as cgroups:
116 fds = []
117 try:
118 for cgroup in cgroups:
119 fds.append(os.open(cgroup, os.O_RDONLY | os.O_DIRECTORY))
120 for i in range(3):
121 # Cgroup BPF programs didn't exist before Linux kernel
122 # commit 3007098494be ("cgroup: add support for eBPF
123 # programs") (in v4.10).
124 try:
125 prog_fd = bpf_prog_load(
126 BPF_PROG_TYPE_CGROUP_SKB,
127 self.INSNS,
128 b"GPL",
129 expected_attach_type=BPF_CGROUP_INET_INGRESS,
130 )
131 fds.append(prog_fd)
132 except OSError as e:
133 if e.errno != errno.EINVAL:
134 raise
135 self.skipTest(
136 "kernel does not support BPF_PROG_TYPE_CGROUP_SKB"
137 )
138 # BPF links didn't exist before Linux kernel commit
139 # 70ed506c3bbc ("bpf: Introduce pinnable bpf_link
140 # abstraction") (in v5.7).
141 try:
142 fds.append(
143 bpf_link_create(
144 prog_fd, fds[i % len(cgroups)], BPF_CGROUP_INET_INGRESS
145 )
146 )
147 except OSError as e:
148 if e.errno != errno.EINVAL:
149 raise
150 self.skipTest("kernel does not support BPF_LINK_CREATE")
151
152 # bpf_link_for_each() isn't supported before Linux v5.8, which
153 # added IDs for BPF links in commit a3b80e107894 ("bpf:
154 # Allocate ID for bpf_link") and an API to get them in commit
155 # 2d602c8cf40d ("bpf: Support GET_FD_BY_ID and GET_NEXT_ID for
156 # bpf_link").
157 try:
158 expected_ids = list(bpf_link_ids())
159 except OSError as e:
160 if e.errno != errno.EINVAL:
161 raise
162 self.skipTest("kernel does not support BPF_LINK_GET_NEXT_ID")
163 self.assertCountEqual(
164 [link.id.value_() for link in bpf_link_for_each(self.prog)],
165 expected_ids,
166 )
167 finally:
168 for fd in fds:
169 os.close(fd)
170
171 def test_bpf_map_for_each(self):
172 fds = []
173 try:
174 for i in range(3):
175 fds.append(bpf_map_create(BPF_MAP_TYPE_HASH, 8, 8, 8))
176
177 # bpf_map_for_each() isn't supported before Linux v4.13, which
178 # added IDs for BPF maps in commit f3f1c054c288 ("bpf: Introduce
179 # bpf_map ID") and a API to get them in commit 34ad5580f8f9 ("bpf:
180 # Add BPF_(PROG|MAP)_GET_NEXT_ID command").
181 try:
182 expected_ids = list(bpf_map_ids())
183 except OSError as e:
184 if e.errno != errno.EINVAL:
185 raise
186 self.skipTest("kernel does not support BPF_MAP_GET_NEXT_ID")
187
188 self.assertCountEqual(
189 [map.id.value_() for map in bpf_map_for_each(self.prog)], expected_ids
190 )
191 finally:
192 for fd in fds:
193 os.close(fd)
194
195 def test_bpf_prog_for_each(self):
196 fds = []
197 try:
198 for i in range(3):
199 fds.append(
200 bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, self.INSNS, b"GPL")
201 )
202
203 # bpf_prog_for_each() isn't supported before Linux v4.13, which
204 # added IDs for BPF programs in commit dc4bb0e23561 ("bpf:
205 # Introduce bpf_prog ID") and an API to get them in commit
206 # 34ad5580f8f9 ("bpf: Add BPF_(PROG|MAP)_GET_NEXT_ID command").
207 try:
208 expected_ids = list(bpf_prog_ids())
209 except OSError as e:
210 if e.errno != errno.EINVAL:
211 raise
212 self.skipTest("kernel does not support BPF_PROG_GET_NEXT_ID")
213
214 self.assertCountEqual(
215 [prog.aux.id.value_() for prog in bpf_prog_for_each(self.prog)],
216 expected_ids,
217 )
218 finally:
219 for fd in fds:
220 os.close(fd)
221
222 def test_bpf_prog_used_maps(self):
223 with contextlib.ExitStack() as exit_stack:
224 map_fd = bpf_map_create(BPF_MAP_TYPE_HASH, 8, 8, 8)
225 exit_stack.callback(os.close, map_fd)
226
227 prog_fd = bpf_prog_load(
228 BPF_PROG_TYPE_SOCKET_FILTER,
229 BPF_LD_MAP_FD(BPF_REG_0, map_fd) + self.INSNS,
230 b"GPL",
231 )
232 exit_stack.callback(os.close, prog_fd)
233
234 bpf_prog = cast(
235 "struct bpf_prog *",
236 fget(find_task(self.prog, os.getpid()), prog_fd).private_data,
237 )
238
239 bpf_map = cast(
240 "struct bpf_map *",
241 fget(find_task(self.prog, os.getpid()), map_fd).private_data,
242 )
243
244 self.assertEqual(list(bpf_prog_used_maps(bpf_prog)), [bpf_map])
245
246 def test_cgroup_bpf_prog_for_each(self):
247 with tmp_cgroups() as (parent_cgroup, child_cgroup):
248 fds = []
249 try:
250 parent_cgroup_fd = os.open(parent_cgroup, os.O_RDONLY | os.O_DIRECTORY)
251 fds.append(parent_cgroup_fd)
252 child_cgroup_fd = os.open(child_cgroup, os.O_RDONLY | os.O_DIRECTORY)
253 fds.append(child_cgroup_fd)
254 try:
255 parent_prog_fd = bpf_prog_load(
256 BPF_PROG_TYPE_CGROUP_SKB,
257 self.INSNS,
258 b"GPL",
259 expected_attach_type=BPF_CGROUP_INET_INGRESS,
260 )
261 fds.append(parent_prog_fd)
262 except OSError as e:
263 if e.errno != errno.EINVAL:
264 raise
265 # If the kernel doesn't support cgroup BPF programs, the
266 # helpers should return empty lists.
267 parent_ids = child_ids = child_effective_ids = []
268 else:
269 parent_prog_id = bpf_prog_get_info_by_fd(parent_prog_fd).id
270 parent_ids = [parent_prog_id]
271
272 # If the kernel supports BPF_F_ALLOW_MULTI, test with
273 # multiple programs.
274 try:
275 bpf_prog_attach(
276 parent_cgroup_fd,
277 parent_prog_fd,
278 BPF_CGROUP_INET_INGRESS,
279 attach_flags=BPF_F_ALLOW_MULTI,
280 )
281 except OSError as e:
282 if e.errno != errno.EINVAL:
283 raise
284 bpf_prog_attach(
285 parent_cgroup_fd, parent_prog_fd, BPF_CGROUP_INET_INGRESS
286 )
287 child_ids = []
288 child_effective_ids = [parent_prog_id]
289 else:
290 child_prog_fd = bpf_prog_load(
291 BPF_PROG_TYPE_CGROUP_SKB,
292 self.INSNS,
293 b"GPL",
294 expected_attach_type=BPF_CGROUP_INET_INGRESS,
295 )
296 fds.append(child_prog_fd)
297 child_prog_id = bpf_prog_get_info_by_fd(child_prog_fd).id
298 bpf_prog_attach(
299 child_cgroup_fd, child_prog_fd, BPF_CGROUP_INET_INGRESS
300 )
301 child_ids = [child_prog_id]
302 child_effective_ids = [parent_prog_id, child_prog_id]
303
304 parent_cgrp = cgroup_get_from_path(self.prog, parent_cgroup.name)
305 child_cgrp = cgroup_get_from_path(
306 self.prog, parent_cgroup.name + "/" + child_cgroup.name
307 )
308
309 self.assertCountEqual(
310 [
311 prog.aux.id.value_()
312 for prog in cgroup_bpf_prog_for_each(
313 parent_cgrp, BPF_CGROUP_INET_INGRESS
314 )
315 ],
316 parent_ids,
317 )
318 self.assertCountEqual(
319 [
320 prog.aux.id.value_()
321 for prog in cgroup_bpf_prog_for_each(
322 child_cgrp, BPF_CGROUP_INET_INGRESS
323 )
324 ],
325 child_ids,
326 )
327
328 self.assertCountEqual(
329 [
330 prog.aux.id.value_()
331 for prog in cgroup_bpf_prog_for_each_effective(
332 parent_cgrp, BPF_CGROUP_INET_INGRESS
333 )
334 ],
335 parent_ids,
336 )
337 self.assertCountEqual(
338 [
339 prog.aux.id.value_()
340 for prog in cgroup_bpf_prog_for_each_effective(
341 child_cgrp, BPF_CGROUP_INET_INGRESS
342 )
343 ],
344 child_effective_ids,
345 )
346 finally:
347 for fd in fds:
348 os.close(fd)
349
350 def test_bpf_prog_by_id(self):
351 fd = bpf_prog_load(BPF_PROG_TYPE_SOCKET_FILTER, self.INSNS, b"GPL")
352 self.addCleanup(os.close, fd)
353 try:
354 prog_id = bpf_prog_get_info_by_fd(fd).id
355 except OSError as e:
356 # bpf_prog_by_id() and BPF program IDs aren't supported before
357 # Linux v4.13, which added IDs for BPF programs in commit
358 # dc4bb0e23561 ("bpf: Introduce bpf_prog ID") and an API to get
359 # them in commit 34ad5580f8f9 ("bpf: Add BPF_(PROG|MAP)_GET_NEXT_ID
360 # command").
361 if e.errno != errno.EINVAL:
362 raise
363 self.skipTest("kernel does not support BPF_PROG_GET_NEXT_ID")
364
365 prog = bpf_prog_by_id(self.prog, prog_id)
366 self.assertNotEqual(prog.value_(), 0)
367 self.assertEqual(prog.aux.id.value_(), prog_id)
368
369 non_existent_prog = bpf_prog_by_id(self.prog, 0x7FFFFFFF)
370 self.assertEqual(non_existent_prog.value_(), 0)
371
372 def test_bpf_map_by_id(self):
373 fd = bpf_map_create(BPF_MAP_TYPE_HASH, 8, 8, 8)
374 self.addCleanup(os.close, fd)
375 try:
376 map_id = bpf_map_get_info_by_fd(fd).id
377 except OSError as e:
378 # bpf_map_by_id() and BPF map IDs aren't supported before
379 # Linux v4.13, which added IDs for BPF maps in commit
380 # f3f1c054c288 ("bpf: Introduce bpf_map ID") and an API to get
381 # them in commit 34ad5580f8f9 ("bpf: Add BPF_(PROG|MAP)_GET_NEXT_ID
382 # command").
383 if e.errno != errno.EINVAL:
384 raise
385 self.skipTest("kernel does not support BPF_MAP_GET_NEXT_ID")
386
387 bpf_map = bpf_map_by_id(self.prog, map_id)
388 self.assertNotEqual(bpf_map.value_(), 0)
389 self.assertEqual(bpf_map.id.value_(), map_id)
390
391 non_existent_map = bpf_map_by_id(self.prog, 0x7FFFFFFF)
392 self.assertEqual(non_existent_map.value_(), 0)