]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/bpf-compat.h
Merge pull request #31648 from neighbourhoodie/review-content
[thirdparty/systemd.git] / src / shared / bpf-compat.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 /* libbpf has been moving quickly.
5 * They added new symbols in the 0.x versions and shortly after removed
6 * deprecated symbols in 1.0.
7 * We only need bpf_map_create and libbpf_probe_bpf_prog_type so we work
8 * around the incompatibility here by:
9 * - declaring both symbols, and looking for either depending on the libbpf
10 * so version we found
11 * - having helpers that automatically use the appropriate version behind the
12 * new API for easy cleanup later
13 *
14 * The advantage of doing this instead of only looking for the symbols declared at
15 * compile time is that we can then load either the old or the new symbols at runtime
16 * regardless of the version we were compiled with */
17
18
19 /* declare the struct for libbpf <= 0.6.0 -- it causes no harm on newer versions */
20 struct bpf_map_create_opts;
21
22 /* new symbols available from 0.7.0.
23 * We need the symbols here:
24 * - after bpf_map_create_opts struct has been defined for older libbpf
25 * - before the compat static inline helpers that use them.
26 * When removing this file move these back to bpf-dlopen.h */
27 extern int (*sym_bpf_map_create)(enum bpf_map_type, const char *, __u32, __u32, __u32, const struct bpf_map_create_opts *);
28 extern int (*sym_libbpf_probe_bpf_prog_type)(enum bpf_prog_type, const void *);
29 extern struct bpf_map* (*sym_bpf_object__next_map)(const struct bpf_object *obj, const struct bpf_map *map);
30
31 /* compat symbols removed in libbpf 1.0 */
32 extern int (*sym_bpf_create_map)(enum bpf_map_type, int key_size, int value_size, int max_entries, __u32 map_flags);
33 extern bool (*sym_bpf_probe_prog_type)(enum bpf_prog_type, __u32);
34
35 /* helpers to use the available variant behind new API */
36 static inline int compat_bpf_map_create(enum bpf_map_type map_type,
37 const char *map_name,
38 __u32 key_size,
39 __u32 value_size,
40 __u32 max_entries,
41 const struct bpf_map_create_opts *opts) {
42 if (sym_bpf_map_create)
43 return sym_bpf_map_create(map_type, map_name, key_size,
44 value_size, max_entries, opts);
45
46 return sym_bpf_create_map(map_type, key_size, value_size, max_entries,
47 0 /* opts->map_flags, but opts is always NULL for us so skip build dependency on the type */);
48 }
49
50 static inline int compat_libbpf_probe_bpf_prog_type(enum bpf_prog_type prog_type, const void *opts) {
51 if (sym_libbpf_probe_bpf_prog_type)
52 return sym_libbpf_probe_bpf_prog_type(prog_type, opts);
53
54 return sym_bpf_probe_prog_type(prog_type, 0);
55 }