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