| 1 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
if not libbpf.found() |
| 4 |
conf.set10('BPF_FRAMEWORK', false) |
| 5 |
else |
| 6 |
clang_found = false |
| 7 |
clang_supports_bpf = false |
| 8 |
bpf_gcc_found = false |
| 9 |
bpftool_strip = false |
| 10 |
deps_found = false |
| 11 |
|
| 12 |
if bpf_compiler == 'clang' |
| 13 |
# Support 'versioned' clang/llvm-strip binaries, as seen on Debian/Ubuntu |
| 14 |
# (like clang-10/llvm-strip-10) |
| 15 |
if meson.is_cross_build() or cc.get_id() != 'clang' or cc.cmd_array()[0].contains('afl-clang') or cc.cmd_array()[0].contains('hfuzz-clang') |
| 16 |
r = find_program('clang', |
| 17 |
required : bpf_framework, |
| 18 |
version : '>= 10.0.0') |
| 19 |
clang_found = r.found() |
| 20 |
if clang_found |
| 21 |
clang = r.full_path() |
| 22 |
endif |
| 23 |
else |
| 24 |
clang_found = true |
| 25 |
clang = cc.cmd_array() |
| 26 |
endif |
| 27 |
|
| 28 |
if clang_found |
| 29 |
# Check if 'clang -target bpf' is supported. |
| 30 |
clang_supports_bpf = run_command(clang, '-target', 'bpf', '--print-supported-cpus', check : false).returncode() == 0 |
| 31 |
endif |
| 32 |
if bpf_framework.enabled() and not clang_supports_bpf |
| 33 |
error('bpf-framework was enabled but clang does not support bpf') |
| 34 |
endif |
| 35 |
elif bpf_compiler == 'gcc' |
| 36 |
bpf_gcc = find_program('bpf-gcc', |
| 37 |
'bpf-none-gcc', |
| 38 |
'bpf-unknown-none-gcc', |
| 39 |
required : true, |
| 40 |
version : '>= 13.1.0') |
| 41 |
bpf_gcc_found = bpf_gcc.found() |
| 42 |
endif |
| 43 |
|
| 44 |
if clang_supports_bpf or bpf_gcc_found |
| 45 |
# Debian installs this in /usr/sbin/ which is not in $PATH. |
| 46 |
# We check for 'bpftool' first, honouring $PATH, and in /usr/sbin/ for Debian. |
| 47 |
# We use 'bpftool gen object' subcommand for bpftool strip, it was added by d80b2fcbe0a023619e0fc73112f2a02c2662f6ab (v5.13). |
| 48 |
bpftool = find_program('bpftool', |
| 49 |
'/usr/sbin/bpftool', |
| 50 |
required : bpf_framework.enabled() and bpf_compiler == 'gcc', |
| 51 |
version : bpf_compiler == 'gcc' ? '>= 7.0.0' : '>= 5.13.0') |
| 52 |
|
| 53 |
if bpftool.found() |
| 54 |
bpftool_strip = true |
| 55 |
deps_found = true |
| 56 |
elif bpf_compiler == 'clang' |
| 57 |
# We require the 'bpftool gen skeleton' subcommand, it was added by 985ead416df39d6fe8e89580cc1db6aa273e0175 (v5.6). |
| 58 |
bpftool = find_program('bpftool', |
| 59 |
'/usr/sbin/bpftool', |
| 60 |
required : bpf_framework, |
| 61 |
version : '>= 5.6.0') |
| 62 |
endif |
| 63 |
|
| 64 |
# We use `llvm-strip` as a fallback if `bpftool gen object` strip support is not available. |
| 65 |
if not bpftool_strip and bpftool.found() and clang_supports_bpf |
| 66 |
if not meson.is_cross_build() |
| 67 |
llvm_strip_bin = run_command(clang, '--print-prog-name', 'llvm-strip', |
| 68 |
check : true).stdout().strip() |
| 69 |
else |
| 70 |
llvm_strip_bin = 'llvm-strip' |
| 71 |
endif |
| 72 |
llvm_strip = find_program(llvm_strip_bin, |
| 73 |
required : bpf_framework, |
| 74 |
version : '>= 10.0.0') |
| 75 |
deps_found = llvm_strip.found() |
| 76 |
endif |
| 77 |
endif |
| 78 |
|
| 79 |
# Can build BPF program from source code in restricted C |
| 80 |
conf.set10('BPF_FRAMEWORK', deps_found) |
| 81 |
endif |
| 82 |
|
| 83 |
if conf.get('BPF_FRAMEWORK') == 1 |
| 84 |
bpf_clang_flags = [ |
| 85 |
'-std=gnu17', |
| 86 |
'-Wno-compare-distinct-pointer-types', |
| 87 |
'-Wno-microsoft-anon-tag', |
| 88 |
'-fms-extensions', |
| 89 |
'-fno-stack-protector', |
| 90 |
'-O2', |
| 91 |
'-target', |
| 92 |
'bpf', |
| 93 |
'-g', |
| 94 |
'-c', |
| 95 |
] |
| 96 |
|
| 97 |
bpf_gcc_flags = [ |
| 98 |
'-std=gnu17', |
| 99 |
'-fms-extensions', |
| 100 |
'-fno-stack-protector', |
| 101 |
'-fno-ssa-phiopt', |
| 102 |
'-O2', |
| 103 |
'-mcpu=v3', |
| 104 |
'-mco-re', |
| 105 |
'-gbtf', |
| 106 |
'-c', |
| 107 |
] |
| 108 |
|
| 109 |
# If c_args contains these flags copy them along with the values, in order to avoid breaking |
| 110 |
# reproducible builds and other functionality |
| 111 |
propagate_cflags = [ |
| 112 |
'-ffile-prefix-map=', |
| 113 |
'-fdebug-prefix-map=', |
| 114 |
'-fmacro-prefix-map=', |
| 115 |
'--sysroot=', |
| 116 |
] |
| 117 |
|
| 118 |
foreach opt : c_args |
| 119 |
foreach flag : propagate_cflags |
| 120 |
if opt.startswith(flag) |
| 121 |
bpf_clang_flags += [opt] |
| 122 |
bpf_gcc_flags += [opt] |
| 123 |
break |
| 124 |
endif |
| 125 |
endforeach |
| 126 |
endforeach |
| 127 |
|
| 128 |
# Generate defines that are appropriate to tell the compiler what architecture |
| 129 |
# we're compiling for. By default we just map meson's cpu_family to __<cpu_family>__. |
| 130 |
# This dictionary contains the exceptions where this doesn't work. |
| 131 |
# |
| 132 |
# C.f. https://mesonbuild.com/Reference-tables.html#cpu-families |
| 133 |
# and src/basic/missing_syscall_def.h. |
| 134 |
|
| 135 |
# Start with older ABI. When define is missing, we're likely targeting that. |
| 136 |
ppc64_elf_version = '1' |
| 137 |
|
| 138 |
if host_machine.cpu_family() == 'ppc64' |
| 139 |
# cc doesn't have to be bpf_compiler, but they should be targeting the same ABI |
| 140 |
call_elf_value = cc.get_define('_CALL_ELF') |
| 141 |
if call_elf_value != '' |
| 142 |
ppc64_elf_version = call_elf_value |
| 143 |
endif |
| 144 |
endif |
| 145 |
|
| 146 |
cpu_arch_defines = { |
| 147 |
'ppc' : ['-D__powerpc__', '-D__TARGET_ARCH_powerpc'], |
| 148 |
'ppc64' : ['-D__powerpc64__', '-D__TARGET_ARCH_powerpc', '-D_CALL_ELF=' + ppc64_elf_version], |
| 149 |
'riscv32' : ['-D__riscv', '-D__riscv_xlen=32', '-D__TARGET_ARCH_riscv'], |
| 150 |
'riscv64' : ['-D__riscv', '-D__riscv_xlen=64', '-D__TARGET_ARCH_riscv'], |
| 151 |
'x86' : ['-D__i386__', '-D__TARGET_ARCH_x86'], |
| 152 |
's390x' : ['-D__s390__', '-D__s390x__', '-D__TARGET_ARCH_s390'], |
| 153 |
|
| 154 |
# For arm, assume hardware fp is available. |
| 155 |
'arm' : ['-D__arm__', '-D__ARM_PCS_VFP', '-D__TARGET_ARCH_arm'], |
| 156 |
'loongarch64' : ['-D__loongarch__', '-D__loongarch_grlen=64', '-D__TARGET_ARCH_loongarch'] |
| 157 |
} |
| 158 |
|
| 159 |
bpf_arch_flags = cpu_arch_defines.get(host_machine.cpu_family(), |
| 160 |
['-D__@0@__'.format(host_machine.cpu_family())]) |
| 161 |
if bpf_compiler == 'gcc' |
| 162 |
bpf_arch_flags += ['-m' + host_machine.endian() + '-endian'] |
| 163 |
endif |
| 164 |
|
| 165 |
libbpf_include_dir = libbpf.get_variable(pkgconfig : 'includedir') |
| 166 |
|
| 167 |
bpf_o_unstripped_cmd = [] |
| 168 |
if bpf_compiler == 'clang' |
| 169 |
bpf_o_unstripped_cmd += [ |
| 170 |
clang, |
| 171 |
bpf_clang_flags, |
| 172 |
bpf_arch_flags, |
| 173 |
] |
| 174 |
elif bpf_compiler == 'gcc' |
| 175 |
bpf_o_unstripped_cmd += [ |
| 176 |
bpf_gcc, |
| 177 |
bpf_gcc_flags, |
| 178 |
bpf_arch_flags, |
| 179 |
] |
| 180 |
endif |
| 181 |
|
| 182 |
bpf_o_unstripped_cmd += ['-I.', '-include', 'config.h'] |
| 183 |
|
| 184 |
if cc.get_id() == 'gcc' or meson.is_cross_build() |
| 185 |
if cc.get_id() != 'gcc' |
| 186 |
warning('Cross compiler is not gcc. Guessing the target triplet for bpf likely fails.') |
| 187 |
endif |
| 188 |
target_triplet_cmd = run_command(cc.cmd_array(), '-print-multiarch', check: false) |
| 189 |
else |
| 190 |
# clang does not support -print-multiarch (D133170) and its -dump-machine |
| 191 |
# does not match multiarch. Query gcc instead. |
| 192 |
target_triplet_cmd = run_command('gcc', '-print-multiarch', check: false) |
| 193 |
endif |
| 194 |
if target_triplet_cmd.returncode() == 0 |
| 195 |
sysroot = meson.get_external_property('sys_root', '/') |
| 196 |
target_triplet = target_triplet_cmd.stdout().strip() |
| 197 |
target_include_dir = sysroot / 'usr' / 'include' |
| 198 |
target_triple_include_dir = target_include_dir / target_triplet |
| 199 |
isystem_dir = '' |
| 200 |
if fs.is_dir(target_triple_include_dir) |
| 201 |
isystem_dir = target_triple_include_dir |
| 202 |
elif fs.is_dir(target_include_dir) |
| 203 |
isystem_dir = target_include_dir |
| 204 |
endif |
| 205 |
if isystem_dir != '' |
| 206 |
bpf_o_unstripped_cmd += [ |
| 207 |
'-isystem', isystem_dir |
| 208 |
] |
| 209 |
endif |
| 210 |
endif |
| 211 |
|
| 212 |
bpf_o_unstripped_cmd += [ |
| 213 |
'-idirafter', |
| 214 |
libbpf_include_dir, |
| 215 |
'@INPUT@', |
| 216 |
'-o', |
| 217 |
'@OUTPUT@' |
| 218 |
] |
| 219 |
|
| 220 |
if bpftool_strip |
| 221 |
bpf_o_cmd = [ |
| 222 |
bpftool, |
| 223 |
'gen', |
| 224 |
'object', |
| 225 |
'@OUTPUT@', |
| 226 |
'@INPUT@' |
| 227 |
] |
| 228 |
elif bpf_compiler == 'clang' |
| 229 |
bpf_o_cmd = [ |
| 230 |
llvm_strip, |
| 231 |
'-g', |
| 232 |
'@INPUT@', |
| 233 |
'-o', |
| 234 |
'@OUTPUT@' |
| 235 |
] |
| 236 |
endif |
| 237 |
|
| 238 |
skel_h_cmd = [ |
| 239 |
bpftool, |
| 240 |
'gen', |
| 241 |
'skeleton', |
| 242 |
'@INPUT@' |
| 243 |
] |
| 244 |
endif |
| 245 |
|
| 246 |
use_provided_vmlinux_h = false |
| 247 |
use_generated_vmlinux_h = false |
| 248 |
provided_vmlinux_h_path = get_option('vmlinux-h-path') |
| 249 |
|
| 250 |
# For the more complex BPF programs we really want a vmlinux.h (which is arch |
| 251 |
# specific, but only somewhat bound to kernel version). Ideally the kernel |
| 252 |
# development headers would ship that, but right now they don't. Hence address |
| 253 |
# this in two ways: |
| 254 |
# |
| 255 |
# 1. Provide a vmlinux.h at build time |
| 256 |
# 2. Generate the file on the fly where possible (which requires /sys/ to be mounted) |
| 257 |
# |
| 258 |
# We generally prefer the former (to support reproducible builds), but will |
| 259 |
# fallback to the latter. |
| 260 |
|
| 261 |
if conf.get('BPF_FRAMEWORK') == 1 |
| 262 |
enable_vmlinux_h = get_option('vmlinux-h') |
| 263 |
|
| 264 |
if enable_vmlinux_h == 'auto' |
| 265 |
if provided_vmlinux_h_path != '' |
| 266 |
use_provided_vmlinux_h = true |
| 267 |
elif fs.exists('/sys/kernel/btf/vmlinux') and \ |
| 268 |
bpftool.found() and \ |
| 269 |
(host_machine.cpu_family() == build_machine.cpu_family()) and \ |
| 270 |
host_machine.cpu_family() in ['x86_64', 'aarch64'] |
| 271 |
|
| 272 |
# We will only generate a vmlinux.h from the running |
| 273 |
# kernel if the host and build machine are of the same |
| 274 |
# family. Also for now we focus on x86_64 and aarch64, |
| 275 |
# since other archs don't seem to be ready yet. |
| 276 |
|
| 277 |
use_generated_vmlinux_h = true |
| 278 |
endif |
| 279 |
elif enable_vmlinux_h == 'provided' |
| 280 |
use_provided_vmlinux_h = true |
| 281 |
elif enable_vmlinux_h == 'generated' |
| 282 |
if not fs.exists('/sys/kernel/btf/vmlinux') |
| 283 |
error('BTF data from kernel not available (/sys/kernel/btf/vmlinux missing), cannot generate vmlinux.h, but was asked to.') |
| 284 |
endif |
| 285 |
if not bpftool.found() |
| 286 |
error('bpftool not available, cannot generate vmlinux.h, but was asked to.') |
| 287 |
endif |
| 288 |
use_generated_vmlinux_h = true |
| 289 |
endif |
| 290 |
endif |
| 291 |
|
| 292 |
vmlinux_h_dependency = [] |
| 293 |
if use_provided_vmlinux_h |
| 294 |
if not fs.exists(provided_vmlinux_h_path) |
| 295 |
error('Path to provided vmlinux.h does not exist.') |
| 296 |
endif |
| 297 |
bpf_o_unstripped_cmd += ['-I' + fs.parent(provided_vmlinux_h_path)] |
| 298 |
message(f'Using provided @provided_vmlinux_h_path@') |
| 299 |
elif use_generated_vmlinux_h |
| 300 |
vmlinux_h_dependency = custom_target( |
| 301 |
output: 'vmlinux.h', |
| 302 |
command : [ bpftool, 'btf', 'dump', 'file', '/sys/kernel/btf/vmlinux', 'format', 'c' ], |
| 303 |
capture : true) |
| 304 |
|
| 305 |
bpf_o_unstripped_cmd += ['-I' + fs.parent(vmlinux_h_dependency.full_path())] |
| 306 |
generated_sources += vmlinux_h_dependency |
| 307 |
message('Using generated @0@'.format(vmlinux_h_dependency.full_path())) |
| 308 |
else |
| 309 |
message('Using neither provided nor generated vmlinux.h, some features will not be available.') |
| 310 |
endif |
| 311 |
|
| 312 |
conf.set10('HAVE_VMLINUX_H', use_provided_vmlinux_h or use_generated_vmlinux_h) |
| 313 |
|
| 314 |
# 'enum lsm_integrity_type' was added together with the bdev_setintegrity LSM |
| 315 |
# hook in kernel commit 2deeb6c333e5 (v6.5). The generated vmlinux.h reflects |
| 316 |
# the running kernel's BTF; a provided vmlinux.h can be older, so probe. |
| 317 |
have_lsm_integrity_type = false |
| 318 |
if use_generated_vmlinux_h |
| 319 |
have_lsm_integrity_type = true |
| 320 |
elif use_provided_vmlinux_h |
| 321 |
have_lsm_integrity_type = cc.compiles( |
| 322 |
'#include "@0@"\nenum lsm_integrity_type _t;\n'.format(provided_vmlinux_h_path), |
| 323 |
name : 'enum lsm_integrity_type in vmlinux.h') |
| 324 |
endif |
| 325 |
conf.set10('HAVE_LSM_INTEGRITY_TYPE', have_lsm_integrity_type) |
| 326 |
|
| 327 |
conf.set10('ENABLE_SYSCTL_BPF', conf.get('HAVE_VMLINUX_H') == 1 and libbpf.version().version_compare('>= 0.7')) |
| 328 |
|
| 329 |
bpf_programs = [ |
| 330 |
{ |
| 331 |
'source' : files('bind-iface.bpf.c'), |
| 332 |
'condition' : 'BPF_FRAMEWORK', |
| 333 |
}, |
| 334 |
{ |
| 335 |
'source' : files('restrict-fsaccess.bpf.c'), |
| 336 |
'condition' : 'HAVE_LSM_INTEGRITY_TYPE', |
| 337 |
'depends' : vmlinux_h_dependency, |
| 338 |
}, |
| 339 |
{ |
| 340 |
'source' : files('restrict-fs.bpf.c'), |
| 341 |
'condition' : 'BPF_FRAMEWORK', |
| 342 |
}, |
| 343 |
{ |
| 344 |
'source' : files('restrict-ifaces.bpf.c'), |
| 345 |
'condition' : 'BPF_FRAMEWORK', |
| 346 |
}, |
| 347 |
{ |
| 348 |
'source' : files('socket-bind.bpf.c'), |
| 349 |
'condition' : 'BPF_FRAMEWORK', |
| 350 |
}, |
| 351 |
{ |
| 352 |
'source' : files('sysctl-monitor.bpf.c'), |
| 353 |
'condition' : 'ENABLE_SYSCTL_BPF', |
| 354 |
'depends' : vmlinux_h_dependency, |
| 355 |
}, |
| 356 |
{ |
| 357 |
'source' : files('userns-restrict.bpf.c'), |
| 358 |
'condition' : 'HAVE_VMLINUX_H', |
| 359 |
'depends' : vmlinux_h_dependency, |
| 360 |
}, |
| 361 |
] |
| 362 |
|
| 363 |
bpf_programs_by_name = {} |
| 364 |
bpf_sources = [] |
| 365 |
|
| 366 |
foreach program : bpf_programs |
| 367 |
if conf.get(program['condition']) != 1 |
| 368 |
continue |
| 369 |
endif |
| 370 |
|
| 371 |
source = program['source'][0] |
| 372 |
# Strip .bpf.c extension |
| 373 |
name = fs.stem(fs.stem(source)) |
| 374 |
|
| 375 |
bpf_o_unstripped = custom_target( |
| 376 |
input : source, |
| 377 |
output : name + '.bpf.unstripped.o', |
| 378 |
command : bpf_o_unstripped_cmd, |
| 379 |
depends : program.get('depends', [])) |
| 380 |
|
| 381 |
bpf_o = custom_target( |
| 382 |
input : bpf_o_unstripped, |
| 383 |
output : name + '.bpf.o', |
| 384 |
command : bpf_o_cmd) |
| 385 |
|
| 386 |
skel_h = custom_target( |
| 387 |
input : bpf_o, |
| 388 |
output : name + '.bpf.skel.h', |
| 389 |
command : skel_h_cmd, |
| 390 |
capture : true) |
| 391 |
|
| 392 |
# The wrapper is written at meson setup time and found via the |
| 393 |
# include path, so we don't need to list it as a build-time source. |
| 394 |
# Keeping it out of bpf_programs_by_name also keeps it out of the |
| 395 |
# clang-tidy per-source test loop, which would otherwise fall back |
| 396 |
# to a BPF compile_commands.json entry (no -Isrc/shared) and fail |
| 397 |
# to resolve bpf-util.h. |
| 398 |
configure_file( |
| 399 |
input : 'bpf-skel-wrapper.h.in', |
| 400 |
output : name + '-skel.h', |
| 401 |
configuration : { 'NAME' : name }) |
| 402 |
|
| 403 |
bpf_programs_by_name += { name : skel_h } |
| 404 |
generated_sources += skel_h |
| 405 |
bpf_sources += source |
| 406 |
endforeach |
| 407 |
|
| 408 |
if bpf_sources.length() > 0 |
| 409 |
meson.add_postconf_script( |
| 410 |
python, |
| 411 |
files('merge-bpf-compdb.py'), |
| 412 |
bpf_sources, |
| 413 |
'--', |
| 414 |
bpf_o_unstripped_cmd) |
| 415 |
endif |