]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared, bpf: add bpf link helpers
authorJulia Kartseva <hex@fb.com>
Fri, 12 Feb 2021 03:38:36 +0000 (19:38 -0800)
committerJulia Kartseva <hex@fb.com>
Mon, 26 Apr 2021 23:21:59 +0000 (16:21 -0700)
add can_link_bpf_program and bpf_link_free helpers.

src/shared/bpf-link.c [new file with mode: 0644]
src/shared/bpf-link.h [new file with mode: 0644]
src/shared/meson.build

diff --git a/src/shared/bpf-link.c b/src/shared/bpf-link.c
new file mode 100644 (file)
index 0000000..94d1a8c
--- /dev/null
@@ -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 (file)
index 0000000..7d529ad
--- /dev/null
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#pragma once
+
+#include <bpf/libbpf.h>
+
+#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);
index a2e4958fdcd4a65e45557218aa8d8ed339821e35..4b590be1e03e5975645d51cbf0595941de7bdd60 100644 (file)
@@ -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,