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