]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/bpf-foreign.c
Merge pull request #20953 from msekletar/mount-ratelimit-followup-20329
[thirdparty/systemd.git] / src / core / bpf-foreign.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "bpf-foreign.h"
4 #include "bpf-program.h"
5 #include "cgroup.h"
6 #include "memory-util.h"
7 #include "missing_magic.h"
8 #include "mountpoint-util.h"
9 #include "set.h"
10 #include "stat-util.h"
11
12 typedef struct BPFForeignKey BPFForeignKey;
13 struct BPFForeignKey {
14 uint32_t prog_id;
15 uint32_t attach_type;
16 };
17
18 static 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)
27 return log_oom();
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
39 static 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
47 static void bpf_foreign_key_hash_func(const BPFForeignKey *p, struct siphash *h) {
48 siphash24_compress(&p->prog_id, sizeof(p->prog_id), h);
49 siphash24_compress(&p->attach_type, sizeof(p->attach_type), h);
50 }
51
52 DEFINE_PRIVATE_HASH_OPS_FULL(bpf_foreign_by_key_hash_ops,
53 BPFForeignKey, bpf_foreign_key_hash_func, bpf_foreign_key_compare_func, free,
54 BPFProgram, bpf_program_free);
55
56 static int attach_programs(Unit *u, const char *path, Hashmap* foreign_by_key, uint32_t attach_flags) {
57 const BPFForeignKey *key;
58 BPFProgram *prog;
59 int r;
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);
65 if (r < 0)
66 return log_unit_error_errno(u, r, "Attaching foreign BPF program to cgroup %s failed: %m", path);
67 }
68
69 return 0;
70 }
71
72 /*
73 * Prepare foreign BPF program for installation:
74 * - Load the program from BPF filesystem to the kernel;
75 * - Store program FD identified by program ID and attach type in the unit.
76 */
77 static int bpf_foreign_prepare(
78 Unit *u,
79 enum bpf_attach_type attach_type,
80 const char *bpffs_path) {
81 _cleanup_(bpf_program_freep) BPFProgram *prog = NULL;
82 _cleanup_free_ BPFForeignKey *key = NULL;
83 uint32_t prog_id;
84 int r;
85
86 assert(u);
87 assert(bpffs_path);
88
89 r = path_is_fs_type(bpffs_path, BPF_FS_MAGIC);
90 if (r < 0)
91 return log_unit_error_errno(u, r,
92 "Failed to determine filesystem type of %s: %m", bpffs_path);
93 if (r == 0)
94 return log_unit_error_errno(u, SYNTHETIC_ERRNO(EINVAL),
95 "Path in BPF filesystem is expected.");
96
97 r = bpf_program_new_from_bpffs_path(bpffs_path, &prog);
98 if (r < 0)
99 return log_unit_error_errno(u, r, "Failed to create foreign BPFProgram: %m");
100
101 r = bpf_program_get_id_by_fd(prog->kernel_fd, &prog_id);
102 if (r < 0)
103 return log_unit_error_errno(u, r, "Failed to get BPF program id by fd: %m");
104
105 r = bpf_foreign_key_new(prog_id, attach_type, &key);
106 if (r < 0)
107 return log_unit_error_errno(u, r,
108 "Failed to create foreign BPF program key from path '%s': %m", bpffs_path);
109
110 r = hashmap_ensure_put(&u->bpf_foreign_by_key, &bpf_foreign_by_key_hash_ops, key, prog);
111 if (r == -EEXIST) {
112 log_unit_warning_errno(u, r, "Foreign BPF program already exists, ignoring: %m");
113 return 0;
114 }
115 if (r < 0)
116 return log_unit_error_errno(u, r, "Failed to put foreign BPFProgram into map: %m");
117
118 TAKE_PTR(key);
119 TAKE_PTR(prog);
120
121 return 0;
122 }
123
124 int bpf_foreign_install(Unit *u) {
125 _cleanup_free_ char *cgroup_path = NULL;
126 CGroupBPFForeignProgram *p;
127 CGroupContext *cc;
128 int r;
129
130 assert(u);
131
132 cc = unit_get_cgroup_context(u);
133 if (!cc)
134 return 0;
135
136 r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, NULL, &cgroup_path);
137 if (r < 0)
138 return log_unit_error_errno(u, r, "Failed to get cgroup path: %m");
139
140 LIST_FOREACH(programs, p, cc->bpf_foreign_programs) {
141 r = bpf_foreign_prepare(u, p->attach_type, p->bpffs_path);
142 if (r < 0)
143 return log_unit_error_errno(u, r, "Failed to prepare foreign BPF hashmap: %m");
144 }
145
146 r = attach_programs(u, cgroup_path, u->bpf_foreign_by_key, BPF_F_ALLOW_MULTI);
147 if (r < 0)
148 return log_unit_error_errno(u, r, "Failed to install foreign BPF programs: %m");
149
150 return 0;
151 }