1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "dlfcn-util.h"
4 #include "bpf-dlopen.h"
5 #include "log.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 struct bpf_link* (*sym_bpf_program__attach_lsm)(struct bpf_program *);
12 long (*sym_libbpf_get_error)(const void *);
13 int (*sym_bpf_link__fd)(const struct bpf_link *);
14 int (*sym_bpf_link__destroy)(struct bpf_link *);
15 int (*sym_bpf_map__fd)(const struct bpf_map *);
16 const char* (*sym_bpf_map__name)(const struct bpf_map *);
17 int (*sym_bpf_create_map)(enum bpf_map_type,  int key_size, int value_size, int max_entries, __u32 map_flags);
18 int (*sym_bpf_map__resize)(struct bpf_map *, __u32);
19 int (*sym_bpf_map_update_elem)(int, const void *, const void *, __u64);
20 int (*sym_bpf_map_delete_elem)(int, const void *);
21 int (*sym_bpf_map__set_inner_map_fd)(struct bpf_map *, int);
22 int (*sym_bpf_object__open_skeleton)(struct bpf_object_skeleton *, const struct bpf_object_open_opts *);
23 int (*sym_bpf_object__load_skeleton)(struct bpf_object_skeleton *);
24 int (*sym_bpf_object__attach_skeleton)(struct bpf_object_skeleton *);
25 void (*sym_bpf_object__detach_skeleton)(struct bpf_object_skeleton *);
26 void (*sym_bpf_object__destroy_skeleton)(struct bpf_object_skeleton *);
27 bool (*sym_bpf_probe_prog_type)(enum bpf_prog_type, __u32);
28 const char* (*sym_bpf_program__name)(const struct bpf_program *);
29 
dlopen_bpf(void)30 int dlopen_bpf(void) {
31         return dlopen_many_sym_or_warn(
32                         &bpf_dl, "libbpf.so.0", LOG_DEBUG,
33                         DLSYM_ARG(bpf_link__destroy),
34                         DLSYM_ARG(bpf_link__fd),
35                         DLSYM_ARG(bpf_map__fd),
36                         DLSYM_ARG(bpf_map__name),
37                         DLSYM_ARG(bpf_create_map),
38                         DLSYM_ARG(bpf_map__resize),
39                         DLSYM_ARG(bpf_map_update_elem),
40                         DLSYM_ARG(bpf_map_delete_elem),
41                         DLSYM_ARG(bpf_map__set_inner_map_fd),
42                         DLSYM_ARG(bpf_object__open_skeleton),
43                         DLSYM_ARG(bpf_object__load_skeleton),
44                         DLSYM_ARG(bpf_object__attach_skeleton),
45                         DLSYM_ARG(bpf_object__detach_skeleton),
46                         DLSYM_ARG(bpf_object__destroy_skeleton),
47                         DLSYM_ARG(bpf_probe_prog_type),
48                         DLSYM_ARG(bpf_program__attach_cgroup),
49                         DLSYM_ARG(bpf_program__attach_lsm),
50                         DLSYM_ARG(bpf_program__name),
51                         DLSYM_ARG(libbpf_get_error));
52 }
53 
54 #else
55 
dlopen_bpf(void)56 int dlopen_bpf(void) {
57         return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
58                                "libbpf support is not compiled in.");
59 }
60 #endif
61