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