]>
Commit | Line | Data |
---|---|---|
71e5200f DM |
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) { | |
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 | }; | |
111 | ||
112 | if (bpf(BPF_PROG_ATTACH, &attr, sizeof(attr)) < 0) | |
113 | return -errno; | |
114 | ||
115 | return 0; | |
116 | } | |
117 | ||
118 | int bpf_program_cgroup_detach(int type, const char *path) { | |
119 | _cleanup_close_ int fd = -1; | |
120 | union bpf_attr attr; | |
121 | ||
122 | assert(path); | |
123 | ||
124 | fd = open(path, O_DIRECTORY|O_RDONLY|O_CLOEXEC); | |
125 | if (fd < 0) | |
126 | return -errno; | |
127 | ||
128 | attr = (union bpf_attr) { | |
129 | .attach_type = type, | |
130 | .target_fd = fd, | |
131 | }; | |
132 | ||
133 | if (bpf(BPF_PROG_DETACH, &attr, sizeof(attr)) < 0) | |
134 | return -errno; | |
135 | ||
136 | return 0; | |
137 | } | |
138 | ||
139 | int bpf_map_new(enum bpf_map_type type, size_t key_size, size_t value_size, size_t max_entries, uint32_t flags) { | |
140 | union bpf_attr attr = { | |
141 | .map_type = type, | |
142 | .key_size = key_size, | |
143 | .value_size = value_size, | |
144 | .max_entries = max_entries, | |
145 | .map_flags = flags, | |
146 | }; | |
147 | int fd; | |
148 | ||
149 | fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); | |
150 | if (fd < 0) | |
151 | return -errno; | |
152 | ||
153 | return fd; | |
154 | } | |
155 | ||
156 | int bpf_map_update_element(int fd, const void *key, void *value) { | |
157 | ||
158 | union bpf_attr attr = { | |
159 | .map_fd = fd, | |
160 | .key = PTR_TO_UINT64(key), | |
161 | .value = PTR_TO_UINT64(value), | |
162 | }; | |
163 | ||
164 | if (bpf(BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)) < 0) | |
165 | return -errno; | |
166 | ||
167 | return 0; | |
168 | } | |
169 | ||
170 | int bpf_map_lookup_element(int fd, const void *key, void *value) { | |
171 | ||
172 | union bpf_attr attr = { | |
173 | .map_fd = fd, | |
174 | .key = PTR_TO_UINT64(key), | |
175 | .value = PTR_TO_UINT64(value), | |
176 | }; | |
177 | ||
178 | if (bpf(BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)) < 0) | |
179 | return -errno; | |
180 | ||
181 | return 0; | |
182 | } |