diff options
author | James Hilliard <james.hilliard1@gmail.com> | 2021-08-11 21:59:19 -0600 |
---|---|---|
committer | Zbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl> | 2021-12-07 18:37:17 +0100 |
commit | d40ce018147cece145a190949128f7f8adaeb2c6 (patch) | |
tree | 17802859139ef39a75600ba51f10bc3595884b83 /src/core | |
parent | d4f8cd4d833a8d863f6e499c7c077ee10a882ae9 (diff) | |
download | systemd-d40ce018147cece145a190949128f7f8adaeb2c6.tar.gz |
bpf: refactor skeleton generation
This should hopefully fix cross compilation for the bpf programs.
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/bpf-socket-bind.c | 2 | ||||
-rw-r--r-- | src/core/bpf/meson.build | 61 | ||||
-rw-r--r-- | src/core/bpf/restrict_fs/meson.build | 26 | ||||
-rw-r--r-- | src/core/bpf/restrict_fs/restrict-fs-skel.h | 14 | ||||
-rw-r--r-- | src/core/bpf/restrict_ifaces/meson.build | 22 | ||||
-rw-r--r-- | src/core/bpf/restrict_ifaces/restrict-ifaces-skel.h | 14 | ||||
-rw-r--r-- | src/core/bpf/socket_bind/meson.build | 22 | ||||
-rw-r--r-- | src/core/bpf/socket_bind/socket-bind-skel.h | 14 | ||||
-rw-r--r-- | src/core/meson.build | 2 | ||||
-rw-r--r-- | src/core/restrict-ifaces.c | 2 |
10 files changed, 154 insertions, 25 deletions
diff --git a/src/core/bpf-socket-bind.c b/src/core/bpf-socket-bind.c index e9c0a2f9bd..c5176aa481 100644 --- a/src/core/bpf-socket-bind.c +++ b/src/core/bpf-socket-bind.c @@ -11,7 +11,7 @@ /* libbpf, clang, llvm and bpftool compile time dependencies are satisfied */ #include "bpf-dlopen.h" #include "bpf-link.h" -#include "bpf/socket_bind/socket-bind.skel.h" +#include "bpf/socket_bind/socket-bind-skel.h" #include "bpf/socket_bind/socket-bind-api.bpf.h" static struct socket_bind_bpf *socket_bind_bpf_free(struct socket_bind_bpf *obj) { diff --git a/src/core/bpf/meson.build b/src/core/bpf/meson.build new file mode 100644 index 0000000000..ab1a7167a6 --- /dev/null +++ b/src/core/bpf/meson.build @@ -0,0 +1,61 @@ +# SPDX-License-Identifier: LGPL-2.1+ + +if conf.get('BPF_FRAMEWORK') == 1 + clang_flags = [ + '-Wno-compare-distinct-pointer-types', + '-O2', + '-target', + 'bpf', + '-g', + '-c', + ] + + clang_arch_flag = '-D__@0@__'.format(host_machine.cpu_family()) + + if meson.version().version_compare('>= 0.58') + libbpf_include_dir = libbpf.get_variable('includedir') + else + libbpf_include_dir = libbpf.get_variable(pkgconfig : 'includedir') + endif + + bpf_o_unstripped_cmd = [ + clang, + clang_flags, + clang_arch_flag, + '-I.' + ] + + if not meson.is_cross_build() + target_triplet_cmd = run_command('gcc', '-dumpmachine', check: false) + if target_triplet_cmd.returncode() == 0 + target_triplet = target_triplet_cmd.stdout().strip() + bpf_o_unstripped_cmd += [ + '-isystem', + '/usr/include/@0@'.format(target_triplet) + ] + endif + endif + + bpf_o_unstripped_cmd += [ + '-idirafter', + libbpf_include_dir, + '@INPUT@', + '-o', + '@OUTPUT@' + ] + + bpf_o_cmd = [ + llvm_strip, + '-g', + '@INPUT@', + '-o', + '@OUTPUT@' + ] + + skel_h_cmd = [ + bpftool, + 'g', + 's', + '@INPUT@' + ] +endif diff --git a/src/core/bpf/restrict_fs/meson.build b/src/core/bpf/restrict_fs/meson.build index c92cccae26..dfe3f12033 100644 --- a/src/core/bpf/restrict_fs/meson.build +++ b/src/core/bpf/restrict_fs/meson.build @@ -1,14 +1,22 @@ # SPDX-License-Identifier: LGPL-2.1-or-later if conf.get('BPF_FRAMEWORK') == 1 - restrict_fs_skel_h = custom_target( - 'restrict-fs-skel.h', + restrict_fs_bpf_o_unstripped = custom_target( + 'restrict-fs.bpf.unstripped.o', input : 'restrict-fs.bpf.c', - output : 'restrict-fs-skel.h', - command : [build_bpf_skel_py, - '--clang_exec', clang.path(), - '--llvm_strip_exec', llvm_strip.path(), - '--bpftool_exec', bpftool.path(), - '--arch', host_machine.cpu_family(), - '@INPUT@', '@OUTPUT@']) + output : 'restrict-fs.bpf.unstripped.o', + command : bpf_o_unstripped_cmd) + + restrict_fs_bpf_o = custom_target( + 'restrict-fs.bpf.o', + input : restrict_fs_bpf_o_unstripped, + output : 'restrict-fs.bpf.o', + command : bpf_o_cmd) + + restrict_fs_skel_h = custom_target( + 'restrict-fs.skel.h', + input : restrict_fs_bpf_o, + output : 'restrict-fs.skel.h', + command : skel_h_cmd, + capture : true) endif diff --git a/src/core/bpf/restrict_fs/restrict-fs-skel.h b/src/core/bpf/restrict_fs/restrict-fs-skel.h new file mode 100644 index 0000000000..412cf62eda --- /dev/null +++ b/src/core/bpf/restrict_fs/restrict-fs-skel.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +/* The SPDX header above is actually correct in claiming this was + * LGPL-2.1-or-later, because it is. Since the kernel doesn't consider that + * compatible with GPL we will claim this to be GPL however, which should be + * fine given that LGPL-2.1-or-later downgrades to GPL if needed. + */ + +/* libbpf is used via dlopen(), so rename symbols */ +#define bpf_object__open_skeleton sym_bpf_object__open_skeleton +#define bpf_object__load_skeleton sym_bpf_object__load_skeleton +#define bpf_object__destroy_skeleton sym_bpf_object__destroy_skeleton + +#include "bpf/restrict_fs/restrict-fs.skel.h" diff --git a/src/core/bpf/restrict_ifaces/meson.build b/src/core/bpf/restrict_ifaces/meson.build index 38f7dbbd51..b7e2bc1519 100644 --- a/src/core/bpf/restrict_ifaces/meson.build +++ b/src/core/bpf/restrict_ifaces/meson.build @@ -1,14 +1,22 @@ # SPDX-License-Identifier: LGPL-2.1-or-later if conf.get('BPF_FRAMEWORK') == 1 + restrict_ifaces_bpf_o_unstripped = custom_target( + 'restrict-ifaces.bpf.unstripped.o', + input : 'restrict-ifaces.bpf.c', + output : 'restrict-ifaces.bpf.unstripped.o', + command : bpf_o_unstripped_cmd) + + restrict_ifaces_bpf_o = custom_target( + 'restrict-ifaces.bpf.o', + input : restrict_ifaces_bpf_o_unstripped, + output : 'restrict-ifaces.bpf.o', + command : bpf_o_cmd) + restrict_ifaces_skel_h = custom_target( 'restrict-ifaces.skel.h', - input : 'restrict-ifaces.bpf.c', + input : restrict_ifaces_bpf_o, output : 'restrict-ifaces.skel.h', - command : [build_bpf_skel_py, - '--clang_exec', clang.path(), - '--llvm_strip_exec', llvm_strip.path(), - '--bpftool_exec', bpftool.path(), - '--arch', host_machine.cpu_family(), - '@INPUT@', '@OUTPUT@']) + command : skel_h_cmd, + capture : true) endif diff --git a/src/core/bpf/restrict_ifaces/restrict-ifaces-skel.h b/src/core/bpf/restrict_ifaces/restrict-ifaces-skel.h new file mode 100644 index 0000000000..f937490954 --- /dev/null +++ b/src/core/bpf/restrict_ifaces/restrict-ifaces-skel.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +/* The SPDX header above is actually correct in claiming this was + * LGPL-2.1-or-later, because it is. Since the kernel doesn't consider that + * compatible with GPL we will claim this to be GPL however, which should be + * fine given that LGPL-2.1-or-later downgrades to GPL if needed. + */ + +/* libbpf is used via dlopen(), so rename symbols */ +#define bpf_object__open_skeleton sym_bpf_object__open_skeleton +#define bpf_object__load_skeleton sym_bpf_object__load_skeleton +#define bpf_object__destroy_skeleton sym_bpf_object__destroy_skeleton + +#include "bpf/restrict_ifaces/restrict-ifaces.skel.h" diff --git a/src/core/bpf/socket_bind/meson.build b/src/core/bpf/socket_bind/meson.build index f5cfc04b00..8211a7a5c9 100644 --- a/src/core/bpf/socket_bind/meson.build +++ b/src/core/bpf/socket_bind/meson.build @@ -1,14 +1,22 @@ # SPDX-License-Identifier: LGPL-2.1-or-later if conf.get('BPF_FRAMEWORK') == 1 + socket_bind_bpf_o_unstripped = custom_target( + 'socket-bind.bpf.unstripped.o', + input : 'socket-bind.bpf.c', + output : 'socket-bind.bpf.unstripped.o', + command : bpf_o_unstripped_cmd) + + socket_bind_bpf_o = custom_target( + 'socket-bind.bpf.o', + input : socket_bind_bpf_o_unstripped, + output : 'socket-bind.bpf.o', + command : bpf_o_cmd) + socket_bind_skel_h = custom_target( 'socket-bind.skel.h', - input : 'socket-bind.bpf.c', + input : socket_bind_bpf_o, output : 'socket-bind.skel.h', - command : [build_bpf_skel_py, - '--clang_exec', clang.path(), - '--llvm_strip_exec', llvm_strip.path(), - '--bpftool_exec', bpftool.path(), - '--arch', host_machine.cpu_family(), - '@INPUT@', '@OUTPUT@']) + command : skel_h_cmd, + capture : true) endif diff --git a/src/core/bpf/socket_bind/socket-bind-skel.h b/src/core/bpf/socket_bind/socket-bind-skel.h new file mode 100644 index 0000000000..e0d16269cd --- /dev/null +++ b/src/core/bpf/socket_bind/socket-bind-skel.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +/* The SPDX header above is actually correct in claiming this was + * LGPL-2.1-or-later, because it is. Since the kernel doesn't consider that + * compatible with GPL we will claim this to be GPL however, which should be + * fine given that LGPL-2.1-or-later downgrades to GPL if needed. + */ + +/* libbpf is used via dlopen(), so rename symbols */ +#define bpf_object__open_skeleton sym_bpf_object__open_skeleton +#define bpf_object__load_skeleton sym_bpf_object__load_skeleton +#define bpf_object__destroy_skeleton sym_bpf_object__destroy_skeleton + +#include "bpf/socket_bind/socket-bind.skel.h" diff --git a/src/core/meson.build b/src/core/meson.build index c02543bd06..92a811f2f0 100644 --- a/src/core/meson.build +++ b/src/core/meson.build @@ -133,6 +133,8 @@ libcore_sources = ''' unit.h '''.split() +subdir('bpf') + subdir('bpf/socket_bind') if conf.get('BPF_FRAMEWORK') == 1 libcore_sources += [socket_bind_skel_h] diff --git a/src/core/restrict-ifaces.c b/src/core/restrict-ifaces.c index a17c5d2bf7..7650031434 100644 --- a/src/core/restrict-ifaces.c +++ b/src/core/restrict-ifaces.c @@ -10,7 +10,7 @@ #include "bpf-dlopen.h" #include "bpf-link.h" -#include "bpf/restrict_ifaces/restrict-ifaces.skel.h" +#include "bpf/restrict_ifaces/restrict-ifaces-skel.h" static struct restrict_ifaces_bpf *restrict_ifaces_bpf_free(struct restrict_ifaces_bpf *obj) { restrict_ifaces_bpf__destroy(obj); |