From: Julia Kartseva Date: Fri, 12 Feb 2021 03:38:36 +0000 (-0800) Subject: shared, bpf: add bpf link helpers X-Git-Tag: v249-rc1~339^2~11 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=43b3f0fb00541cce01daadee8e67192464eec76e;p=thirdparty%2Fsystemd.git shared, bpf: add bpf link helpers add can_link_bpf_program and bpf_link_free helpers. --- diff --git a/src/shared/bpf-link.c b/src/shared/bpf-link.c new file mode 100644 index 00000000000..94d1a8c7ac5 --- /dev/null +++ b/src/shared/bpf-link.c @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "bpf-link.h" + +bool can_link_bpf_program(struct bpf_program *prog) { + _cleanup_(bpf_link_freep) struct bpf_link *link = NULL; + + assert(prog); + + /* Pass invalid cgroup fd intentionally. */ + link = bpf_program__attach_cgroup(prog, /*cgroup_fd=*/-1); + + /* EBADF indicates that bpf_link is supported by kernel. */ + return libbpf_get_error(link) == -EBADF; +} + +struct bpf_link *bpf_link_free(struct bpf_link *link) { + /* bpf_link__destroy handles link == NULL case */ + (void) bpf_link__destroy(link); + + return NULL; +} diff --git a/src/shared/bpf-link.h b/src/shared/bpf-link.h new file mode 100644 index 00000000000..7d529ad2cd1 --- /dev/null +++ b/src/shared/bpf-link.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#pragma once + +#include + +#include "macro.h" + +bool can_link_bpf_program(struct bpf_program *prog); + +struct bpf_link *bpf_link_free(struct bpf_link *p); +DEFINE_TRIVIAL_CLEANUP_FUNC(struct bpf_link *, bpf_link_free); diff --git a/src/shared/meson.build b/src/shared/meson.build index a2e4958fdcd..4b590be1e03 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -319,6 +319,13 @@ if conf.get('HAVE_LIBIPTC') == 1 shared_sources += files('firewall-util-iptables.c') endif +if conf.get('HAVE_LIBBPF') == 1 + shared_sources += files(''' + bpf-link.c + bpf-link.h + '''.split()) +endif + if conf.get('HAVE_KMOD') == 1 shared_sources += files('module-util.c') endif @@ -378,6 +385,7 @@ libshared_name = 'systemd-shared-@0@'.format(meson.project_version()) libshared_deps = [threads, libacl, libblkid, + libbpf, libcap, libcrypt, libgcrypt,