]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/bpf-program.c
bpf: set BPF_F_ALLOW_OVERRIDE when attaching a cgroup program if Delegate=yes is set
[thirdparty/systemd.git] / src / basic / bpf-program.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2016 Daniel Mack
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include "alloc-util.h"
26 #include "bpf-program.h"
27 #include "fd-util.h"
28 #include "log.h"
29 #include "missing.h"
30
31 int bpf_program_new(uint32_t prog_type, BPFProgram **ret) {
32 _cleanup_(bpf_program_unrefp) BPFProgram *p = NULL;
33
34 p = new0(BPFProgram, 1);
35 if (!p)
36 return log_oom();
37
38 p->prog_type = prog_type;
39 p->kernel_fd = -1;
40
41 *ret = p;
42 p = NULL;
43 return 0;
44 }
45
46 BPFProgram *bpf_program_unref(BPFProgram *p) {
47 if (!p)
48 return NULL;
49
50 safe_close(p->kernel_fd);
51 free(p->instructions);
52
53 return mfree(p);
54 }
55
56 int bpf_program_add_instructions(BPFProgram *p, const struct bpf_insn *instructions, size_t count) {
57
58 assert(p);
59
60 if (!GREEDY_REALLOC(p->instructions, p->allocated, p->n_instructions + count))
61 return -ENOMEM;
62
63 memcpy(p->instructions + p->n_instructions, instructions, sizeof(struct bpf_insn) * count);
64 p->n_instructions += count;
65
66 return 0;
67 }
68
69 int bpf_program_load_kernel(BPFProgram *p, char *log_buf, size_t log_size) {
70 union bpf_attr attr;
71
72 assert(p);
73
74 if (p->kernel_fd >= 0)
75 return -EBUSY;
76
77 attr = (union bpf_attr) {
78 .prog_type = p->prog_type,
79 .insns = PTR_TO_UINT64(p->instructions),
80 .insn_cnt = p->n_instructions,
81 .license = PTR_TO_UINT64("GPL"),
82 .log_buf = PTR_TO_UINT64(log_buf),
83 .log_level = !!log_buf,
84 .log_size = log_size,
85 };
86
87 p->kernel_fd = bpf(BPF_PROG_LOAD, &attr, sizeof(attr));
88 if (p->kernel_fd < 0)
89 return -errno;
90
91 return 0;
92 }
93
94 int bpf_program_cgroup_attach(BPFProgram *p, int type, const char *path, uint32_t flags) {
95 _cleanup_close_ int fd = -1;
96 union bpf_attr attr;
97
98 assert(p);
99 assert(type >= 0);
100 assert(path);
101
102 fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
103 if (fd < 0)
104 return -errno;
105
106 attr = (union bpf_attr) {
107 .attach_type = type,
108 .target_fd = fd,
109 .attach_bpf_fd = p->kernel_fd,
110 .attach_flags = flags,
111 };
112
113 if (bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)) < 0)
114 return -errno;
115
116 return 0;
117 }
118
119 int bpf_program_cgroup_detach(int type, const char *path) {
120 _cleanup_close_ int fd = -1;
121 union bpf_attr attr;
122
123 assert(path);
124
125 fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
126 if (fd < 0)
127 return -errno;
128
129 attr = (union bpf_attr) {
130 .attach_type = type,
131 .target_fd = fd,
132 };
133
134 if (bpf(BPF_PROG_DETACH, &attr, sizeof(attr)) < 0)
135 return -errno;
136
137 return 0;
138 }
139
140 int bpf_map_new(enum bpf_map_type type, size_t key_size, size_t value_size, size_t max_entries, uint32_t flags) {
141 union bpf_attr attr = {
142 .map_type = type,
143 .key_size = key_size,
144 .value_size = value_size,
145 .max_entries = max_entries,
146 .map_flags = flags,
147 };
148 int fd;
149
150 fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
151 if (fd < 0)
152 return -errno;
153
154 return fd;
155 }
156
157 int bpf_map_update_element(int fd, const void *key, void *value) {
158
159 union bpf_attr attr = {
160 .map_fd = fd,
161 .key = PTR_TO_UINT64(key),
162 .value = PTR_TO_UINT64(value),
163 };
164
165 if (bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)) < 0)
166 return -errno;
167
168 return 0;
169 }
170
171 int bpf_map_lookup_element(int fd, const void *key, void *value) {
172
173 union bpf_attr attr = {
174 .map_fd = fd,
175 .key = PTR_TO_UINT64(key),
176 .value = PTR_TO_UINT64(value),
177 };
178
179 if (bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)) < 0)
180 return -errno;
181
182 return 0;
183 }