]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include "bpf-foreign.h" | |
4 | #include "bpf-program.h" | |
5 | #include "cgroup.h" | |
6 | #include "memory-util.h" | |
7 | #include "mountpoint-util.h" | |
8 | #include "set.h" | |
9 | ||
10 | typedef struct BPFForeignKey BPFForeignKey; | |
11 | struct BPFForeignKey { | |
12 | uint32_t prog_id; | |
13 | uint32_t attach_type; | |
14 | }; | |
15 | ||
16 | static int bpf_foreign_key_new(uint32_t prog_id, | |
17 | enum bpf_attach_type attach_type, | |
18 | BPFForeignKey **ret) { | |
19 | _cleanup_free_ BPFForeignKey *p = NULL; | |
20 | ||
21 | assert(ret); | |
22 | ||
23 | p = new(BPFForeignKey, 1); | |
24 | if (!p) | |
25 | return log_oom(); | |
26 | ||
27 | *p = (BPFForeignKey) { | |
28 | .prog_id = prog_id, | |
29 | .attach_type = attach_type, | |
30 | }; | |
31 | ||
32 | *ret = TAKE_PTR(p); | |
33 | ||
34 | return 0; | |
35 | } | |
36 | ||
37 | static int bpf_foreign_key_compare_func(const BPFForeignKey *a, const BPFForeignKey *b) { | |
38 | int r = CMP(a->prog_id, b->prog_id); | |
39 | if (r != 0) | |
40 | return r; | |
41 | ||
42 | return CMP(a->attach_type, b->attach_type); | |
43 | } | |
44 | ||
45 | static void bpf_foreign_key_hash_func(const BPFForeignKey *p, struct siphash *h) { | |
46 | siphash24_compress(&p->prog_id, sizeof(p->prog_id), h); | |
47 | siphash24_compress(&p->attach_type, sizeof(p->attach_type), h); | |
48 | } | |
49 | ||
50 | DEFINE_PRIVATE_HASH_OPS_FULL(bpf_foreign_by_key_hash_ops, | |
51 | BPFForeignKey, bpf_foreign_key_hash_func, bpf_foreign_key_compare_func, free, | |
52 | BPFProgram, bpf_program_unref); | |
53 | ||
54 | static int attach_programs(Unit *u, const char *path, Hashmap* foreign_by_key, uint32_t attach_flags) { | |
55 | const BPFForeignKey *key; | |
56 | BPFProgram *prog; | |
57 | int r; | |
58 | ||
59 | assert(u); | |
60 | ||
61 | HASHMAP_FOREACH_KEY(prog, key, foreign_by_key) { | |
62 | r = bpf_program_cgroup_attach(prog, key->attach_type, path, attach_flags); | |
63 | if (r < 0) | |
64 | return log_unit_error_errno(u, r, "Attaching foreign BPF program to cgroup %s failed: %m", path); | |
65 | } | |
66 | ||
67 | return 0; | |
68 | } | |
69 | ||
70 | /* | |
71 | * Prepare foreign BPF program for installation: | |
72 | * - Load the program from BPF filesystem to the kernel; | |
73 | * - Store program FD identified by program ID and attach type in the unit. | |
74 | */ | |
75 | static int bpf_foreign_prepare( | |
76 | Unit *u, | |
77 | enum bpf_attach_type attach_type, | |
78 | const char *bpffs_path) { | |
79 | _cleanup_(bpf_program_unrefp) BPFProgram *prog = NULL; | |
80 | _cleanup_free_ BPFForeignKey *key = NULL; | |
81 | uint32_t prog_id; | |
82 | int r; | |
83 | ||
84 | assert(u); | |
85 | assert(bpffs_path); | |
86 | ||
87 | r = bpf_program_new_from_bpffs_path(bpffs_path, &prog); | |
88 | if (r < 0) | |
89 | return log_unit_error_errno(u, r, "Failed to create foreign BPFProgram: %m"); | |
90 | ||
91 | r = bpf_program_get_id_by_fd(prog->kernel_fd, &prog_id); | |
92 | if (r < 0) | |
93 | return log_unit_error_errno(u, r, "Failed to get BPF program id by fd: %m"); | |
94 | ||
95 | r = bpf_foreign_key_new(prog_id, attach_type, &key); | |
96 | if (r < 0) | |
97 | return log_unit_error_errno(u, r, | |
98 | "Failed to create foreign BPF program key from path '%s': %m", bpffs_path); | |
99 | ||
100 | r = hashmap_ensure_put(&u->bpf_foreign_by_key, &bpf_foreign_by_key_hash_ops, key, prog); | |
101 | if (r == -EEXIST) { | |
102 | log_unit_warning_errno(u, r, "Foreign BPF program already exists, ignoring: %m"); | |
103 | return 0; | |
104 | } | |
105 | if (r < 0) | |
106 | return log_unit_error_errno(u, r, "Failed to put foreign BPFProgram into map: %m"); | |
107 | ||
108 | TAKE_PTR(key); | |
109 | TAKE_PTR(prog); | |
110 | ||
111 | return 0; | |
112 | } | |
113 | ||
114 | int bpf_foreign_supported(void) { | |
115 | int r; | |
116 | ||
117 | r = cg_all_unified(); | |
118 | if (r <= 0) | |
119 | return r; | |
120 | ||
121 | return path_is_mount_point("/sys/fs/bpf", NULL, 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 | } |