Debian Codegraph @

src/core/bpf-foreign.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <linux/bpf.h>
4 #include <linux/magic.h>
5
6 #include "alloc-util.h"
7 #include "bpf-foreign.h"
8 #include "bpf-program.h"
9 #include "cgroup.h"
10 #include "errno-util.h"
11 #include "hash-funcs.h"
12 #include "hashmap.h"
13 #include "siphash24.h"
14 #include "stat-util.h"
15 #include "unit.h"
16
17 typedef struct BPFForeignKey {
18 uint32_t prog_id;
19 uint32_t attach_type;
20 } BPFForeignKey;
21
22 static int bpf_foreign_key_new(uint32_t prog_id,
23 enum bpf_attach_type attach_type,
24 BPFForeignKey **ret) {
25 _cleanup_free_ BPFForeignKey *p = NULL;
26
27 assert(ret);
28
29 p = new(BPFForeignKey, 1);
30 if (!p)
31 return -ENOMEM;
32
33 *p = (BPFForeignKey) {
34 .prog_id = prog_id,
35 .attach_type = attach_type,
36 };
37
38 *ret = TAKE_PTR(p);
39
40 return 0;
41 }
42
43 static int bpf_foreign_key_compare_func(const BPFForeignKey *a, const BPFForeignKey *b) {
44 int r = CMP(a->prog_id, b->prog_id);
45 if (r != 0)
46 return r;
47
48 return CMP(a->attach_type, b->attach_type);
49 }
50
51 static void bpf_foreign_key_hash_func(const BPFForeignKey *p, struct siphash *h) {
52 siphash24_compress_typesafe(p->prog_id, h);
53 siphash24_compress_typesafe(p->attach_type, h);
54 }
55
56 DEFINE_PRIVATE_HASH_OPS_FULL(bpf_foreign_by_key_hash_ops,
57 BPFForeignKey, bpf_foreign_key_hash_func, bpf_foreign_key_compare_func, free,
58 BPFProgram, bpf_program_free);
59
60 static int attach_programs(Unit *u, const char *path, Hashmap* foreign_by_key, uint32_t attach_flags) {
61 const BPFForeignKey *key;
62 BPFProgram *prog;
63 int r, ret = 0;
64
65 assert(u);
66
67 HASHMAP_FOREACH_KEY(prog, key, foreign_by_key) {
68 r = bpf_program_cgroup_attach(prog, key->attach_type, path, attach_flags);
69 if (r < 0)
70 RET_GATHER(ret, log_unit_error_errno(u, r, "bpf-foreign: Attaching foreign BPF program to cgroup %s failed: %m", path));
71 }
72
73 return ret;
74 }
75
76 /*
77 * Prepare foreign BPF program for installation:
78 * - Load the program from BPF filesystem to the kernel;
79 * - Store program FD identified by program ID and attach type in the unit.
80 */
81 static int bpf_foreign_prepare(
82 Unit *u,
83 enum bpf_attach_type attach_type,
84 const char *bpffs_path) {
85
86 _cleanup_(bpf_program_freep) BPFProgram *prog = NULL;
87 _cleanup_free_ BPFForeignKey *key = NULL;
88 uint32_t prog_id;
89 int r;
90
91 assert(u);
92 assert(bpffs_path);
93
94 r = path_is_fs_type(bpffs_path, BPF_FS_MAGIC);
95 if (r == -ENOENT) {
96 log_unit_warning_errno(u, r, "bpf-foreign: foreign program %s does not exist, skipping.", bpffs_path);
97 return 0;
98 }
99 if (r < 0)
100 return log_unit_error_errno(u, r,
101 "bpf-foreign: Failed to determine filesystem type of %s: %m", bpffs_path);
102 if (r == 0)
103 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
104 "bpf-foreign: Path in BPF filesystem is expected.");
105
106 CGroupRuntime *crt = unit_get_cgroup_runtime(u);
107 if (!crt)
108 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
109 "Failed to get control group runtime object.");
110
111 r = bpf_program_new_from_bpffs_path(bpffs_path, &prog);
112 if (r < 0)
113 return log_unit_error_errno(u, r, "bpf-foreign: Failed to create foreign BPF program: %m");
114
115 r = bpf_program_get_id_by_fd(prog->kernel_fd, &prog_id);
116 if (r < 0)
117 return log_unit_error_errno(u, r, "bpf-foreign: Failed to get BPF program id from fd: %m");
118
119 r = bpf_foreign_key_new(prog_id, attach_type, &key);
120 if (r < 0)
121 return log_unit_error_errno(u, r,
122 "bpf-foreign: Failed to create foreign BPF program key from path '%s': %m", bpffs_path);
123
124 r = hashmap_ensure_put(&crt->bpf_foreign_by_key, &bpf_foreign_by_key_hash_ops, key, prog);
125 if (r == -EEXIST) {
126 log_unit_warning_errno(u, r, "bpf-foreign: Foreign BPF program already exists, ignoring: %m");
127 return 0;
128 }
129 if (r < 0)
130 return log_unit_error_errno(u, r, "bpf-foreign: Failed to put foreign BPF program into map: %m");
131
132 TAKE_PTR(key);
133 TAKE_PTR(prog);
134
135 return 0;
136 }
137
138 int bpf_foreign_install(Unit *u) {
139 _cleanup_free_ char *cgroup_path = NULL;
140 CGroupContext *cc;
141 CGroupRuntime *crt;
142 int r, ret = 0;
143
144 assert(u);
145
146 cc = unit_get_cgroup_context(u);
147 if (!cc)
148 return 0;
149
150 crt = unit_get_cgroup_runtime(u);
151 if (!crt)
152 return 0;
153
154 r = cg_get_path(crt->cgroup_path, /* suffix= */ NULL, &cgroup_path);
155 if (r < 0)
156 return log_unit_error_errno(u, r, "bpf-foreign: Failed to get cgroup path: %m");
157
158 LIST_FOREACH(programs, p, cc->bpf_foreign_programs)
159 RET_GATHER(ret, bpf_foreign_prepare(u, p->attach_type, p->bpffs_path));
160
161 return RET_GATHER(ret, attach_programs(u, cgroup_path, crt->bpf_foreign_by_key, BPF_F_ALLOW_MULTI));
162 }