Debian Codegraph @

src/shared/bpf-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <syslog.h>
5
6 #include "sd-dlopen.h"
7
8 #if HAVE_LIBBPF
9
10 #include <bpf/bpf.h> /* IWYU pragma: export */
11 #include <bpf/libbpf.h> /* IWYU pragma: export */
12
13 #include "dlfcn-util.h"
14 #include "shared-forward.h"
15
16 /* Always redeclare these so DLSYM_PROTOTYPE's typeof() resolves regardless of libbpf version;
17 * suppress the warning when the libbpf headers already declare them.
18 * - bpf_map_create, bpf_object__next_map: available since libbpf 0.7
19 * - bpf_token_create: available since libbpf 1.5
20 * - bpf_create_map: removed in libbpf 1.0
21 */
22 DISABLE_WARNING_REDUNDANT_DECLS;
23 /* NOLINTBEGIN(readability-redundant-declaration) */
24 struct bpf_map_create_opts;
25 struct bpf_token_create_opts;
26 extern int bpf_create_map(enum bpf_map_type, int key_size, int value_size, int max_entries, __u32 map_flags);
27 extern int bpf_map_create(enum bpf_map_type, const char *, __u32, __u32, __u32, const struct bpf_map_create_opts *);
28 extern struct bpf_map* bpf_object__next_map(const struct bpf_object *obj, const struct bpf_map *map);
29 extern int bpf_token_create(int bpffs_fd, struct bpf_token_create_opts *opts);
30 /* NOLINTEND(readability-redundant-declaration) */
31 REENABLE_WARNING;
32
33 extern DLSYM_PROTOTYPE(bpf_create_map);
34 extern DLSYM_PROTOTYPE(bpf_link__destroy);
35 extern DLSYM_PROTOTYPE(bpf_link__fd);
36 extern DLSYM_PROTOTYPE(bpf_link__open);
37 extern DLSYM_PROTOTYPE(bpf_link__pin);
38 extern DLSYM_PROTOTYPE(bpf_map__fd);
39 extern DLSYM_PROTOTYPE(bpf_map__name);
40 extern DLSYM_PROTOTYPE(bpf_map__set_inner_map_fd);
41 extern DLSYM_PROTOTYPE(bpf_map__set_max_entries);
42 extern DLSYM_PROTOTYPE(bpf_map__set_pin_path);
43 extern DLSYM_PROTOTYPE(bpf_map_create);
44 extern DLSYM_PROTOTYPE(bpf_map_delete_elem);
45 extern DLSYM_PROTOTYPE(bpf_map_get_fd_by_id);
46 extern DLSYM_PROTOTYPE(bpf_map_lookup_elem);
47 extern DLSYM_PROTOTYPE(bpf_map_update_elem);
48 extern DLSYM_PROTOTYPE(bpf_obj_get_info_by_fd);
49 /* The *_skeleton APIs are autogenerated by bpftool, the targets can be found
50 * in ./build/src/core/bpf/socket-bind/socket-bind.skel.h */
51 extern DLSYM_PROTOTYPE(bpf_object__attach_skeleton);
52 extern DLSYM_PROTOTYPE(bpf_object__destroy_skeleton);
53 extern DLSYM_PROTOTYPE(bpf_object__detach_skeleton);
54 extern DLSYM_PROTOTYPE(bpf_object__load_skeleton);
55 extern DLSYM_PROTOTYPE(bpf_object__name);
56 extern DLSYM_PROTOTYPE(bpf_object__next_map);
57 extern DLSYM_PROTOTYPE(bpf_object__open_skeleton);
58 extern DLSYM_PROTOTYPE(bpf_object__pin_maps);
59 extern DLSYM_PROTOTYPE(bpf_program__attach);
60 extern DLSYM_PROTOTYPE(bpf_program__attach_cgroup);
61 extern DLSYM_PROTOTYPE(bpf_program__attach_lsm);
62 extern DLSYM_PROTOTYPE(bpf_program__name);
63 extern DLSYM_PROTOTYPE(bpf_program__set_autoload);
64 extern DLSYM_PROTOTYPE(bpf_token_create);
65 extern DLSYM_PROTOTYPE(libbpf_set_print);
66 extern DLSYM_PROTOTYPE(ring_buffer__epoll_fd);
67 extern DLSYM_PROTOTYPE(ring_buffer__free);
68 extern DLSYM_PROTOTYPE(ring_buffer__new);
69 extern DLSYM_PROTOTYPE(ring_buffer__poll);
70
71 /* Helper that uses the available variant behind the new API. */
72 static inline int compat_bpf_map_create(
73 enum bpf_map_type map_type,
74 const char *map_name,
75 __u32 key_size,
76 __u32 value_size,
77 __u32 max_entries,
78 const struct bpf_map_create_opts *opts) {
79
80 if (sym_bpf_map_create)
81 return sym_bpf_map_create(map_type, map_name, key_size,
82 value_size, max_entries, opts);
83
84 return sym_bpf_create_map(map_type, key_size, value_size, max_entries,
85 0 /* opts->map_flags, but opts is always NULL for us so skip build dependency on the type */);
86 }
87
88 /* libbpf sometimes returns error codes that make sense only in the kernel, like 524 for EOPNOTSUPP. Use
89 * this helper instead of libbpf_get_error() to ensure some of the known ones are translated into errnos
90 * we understand. */
91 int bpf_get_error_translated(const void *ptr);
92
93 #define BPF_NOTE(priority) \
94 SD_ELF_NOTE_DLOPEN("bpf", \
95 "Support firewalling and sandboxing with BPF", \
96 priority, \
97 "libbpf.so.1", "libbpf.so.0")
98
99 #define DLOPEN_BPF(log_level, priority) \
100 ({ \
101 BPF_NOTE(priority); \
102 dlopen_bpf(log_level); \
103 })
104 #else
105 #define DLOPEN_BPF(log_level, priority) dlopen_bpf(log_level)
106 #endif
107
108 int dlopen_bpf(int log_level);