Skip to content

Commit 43b3f0f

Browse files
author
Julia Kartseva
committed
shared, bpf: add bpf link helpers
add can_link_bpf_program and bpf_link_free helpers.
1 parent 09fc220 commit 43b3f0f

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/shared/bpf-link.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
3+
#include "bpf-link.h"
4+
5+
bool can_link_bpf_program(struct bpf_program *prog) {
6+
_cleanup_(bpf_link_freep) struct bpf_link *link = NULL;
7+
8+
assert(prog);
9+
10+
/* Pass invalid cgroup fd intentionally. */
11+
link = bpf_program__attach_cgroup(prog, /*cgroup_fd=*/-1);
12+
13+
/* EBADF indicates that bpf_link is supported by kernel. */
14+
return libbpf_get_error(link) == -EBADF;
15+
}
16+
17+
struct bpf_link *bpf_link_free(struct bpf_link *link) {
18+
/* bpf_link__destroy handles link == NULL case */
19+
(void) bpf_link__destroy(link);
20+
21+
return NULL;
22+
}

src/shared/bpf-link.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: LGPL-2.1+ */
2+
3+
#pragma once
4+
5+
#include <bpf/libbpf.h>
6+
7+
#include "macro.h"
8+
9+
bool can_link_bpf_program(struct bpf_program *prog);
10+
11+
struct bpf_link *bpf_link_free(struct bpf_link *p);
12+
DEFINE_TRIVIAL_CLEANUP_FUNC(struct bpf_link *, bpf_link_free);

src/shared/meson.build

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ if conf.get('HAVE_LIBIPTC') == 1
319319
shared_sources += files('firewall-util-iptables.c')
320320
endif
321321

322+
if conf.get('HAVE_LIBBPF') == 1
323+
shared_sources += files('''
324+
bpf-link.c
325+
bpf-link.h
326+
'''.split())
327+
endif
328+
322329
if conf.get('HAVE_KMOD') == 1
323330
shared_sources += files('module-util.c')
324331
endif
@@ -378,6 +385,7 @@ libshared_name = 'systemd-shared-@0@'.format(meson.project_version())
378385
libshared_deps = [threads,
379386
libacl,
380387
libblkid,
388+
libbpf,
381389
libcap,
382390
libcrypt,
383391
libgcrypt,

0 commit comments

Comments
 (0)