]> git.proxmox.com Git - systemd.git/blob - src/shared/bpf-dlopen.c
New upstream version 249~rc1
[systemd.git] / src / shared / bpf-dlopen.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "dlfcn-util.h"
5 #include "bpf-dlopen.h"
6
7 #if HAVE_LIBBPF
8 static void *bpf_dl = NULL;
9
10 struct bpf_link* (*sym_bpf_program__attach_cgroup)(struct bpf_program *, int);
11 long (*sym_libbpf_get_error)(const void *);
12 int (*sym_bpf_link__fd)(const struct bpf_link *);
13 int (*sym_bpf_link__destroy)(struct bpf_link *);
14 int (*sym_bpf_map__fd)(const struct bpf_map *);
15 const char* (*sym_bpf_map__name)(const struct bpf_map *);
16 int (*sym_bpf_map__resize)(struct bpf_map *, __u32);
17 int (*sym_bpf_map_update_elem)(int, const void *, const void *, __u64);
18 int (*sym_bpf_object__open_skeleton)(struct bpf_object_skeleton *, const struct bpf_object_open_opts *);
19 int (*sym_bpf_object__load_skeleton)(struct bpf_object_skeleton *);
20 int (*sym_bpf_object__attach_skeleton)(struct bpf_object_skeleton *);
21 void (*sym_bpf_object__detach_skeleton)(struct bpf_object_skeleton *);
22 void (*sym_bpf_object__destroy_skeleton)(struct bpf_object_skeleton *);
23 bool (*sym_bpf_probe_prog_type)(enum bpf_prog_type, __u32);
24 const char* (*sym_bpf_program__name)(const struct bpf_program *);
25
26 int dlopen_bpf(void) {
27 _cleanup_(dlclosep) void *dl = NULL;
28 int r;
29
30 if (bpf_dl)
31 return 0; /* Already loaded */
32
33 dl = dlopen("libbpf.so.0", RTLD_LAZY);
34 if (!dl)
35 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
36 "libbpf is not installed: %s", dlerror());
37
38 r = dlsym_many_and_warn(
39 dl,
40 LOG_DEBUG,
41 DLSYM_ARG(bpf_link__destroy),
42 DLSYM_ARG(bpf_link__fd),
43 DLSYM_ARG(bpf_map__fd),
44 DLSYM_ARG(bpf_map__name),
45 DLSYM_ARG(bpf_map__resize),
46 DLSYM_ARG(bpf_map_update_elem),
47 DLSYM_ARG(bpf_object__open_skeleton),
48 DLSYM_ARG(bpf_object__load_skeleton),
49 DLSYM_ARG(bpf_object__attach_skeleton),
50 DLSYM_ARG(bpf_object__detach_skeleton),
51 DLSYM_ARG(bpf_object__destroy_skeleton),
52 DLSYM_ARG(bpf_probe_prog_type),
53 DLSYM_ARG(bpf_program__attach_cgroup),
54 DLSYM_ARG(bpf_program__name),
55 DLSYM_ARG(libbpf_get_error),
56 NULL);
57 if (r < 0)
58 return r;
59
60 /* Note that we never release the reference here, because there's no real reason to, after all this
61 * was traditionally a regular shared library dependency which lives forever too. */
62 bpf_dl = TAKE_PTR(dl);
63 return 1;
64 }
65
66 #else
67
68 int dlopen_bpf(void) {
69 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
70 "libbpf support is not compiled in.");
71 }
72 #endif