blob: a31445ad9fa7d44b7f24889cf3542b5a8aa2cc99 [file] [log] [blame]
Maciej Żenczykowskia728a702021-01-11 19:08:33 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
Patrick Rohr99ace232024-11-22 10:35:29 -080019#include <android-base/unique_fd.h>
Maciej Żenczykowski52018c82024-06-04 16:05:16 +000020#include <stdlib.h>
21#include <unistd.h>
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080022#include <linux/bpf.h>
23#include <linux/unistd.h>
Maciej Żenczykowski52018c82024-06-04 16:05:16 +000024#include <sys/file.h>
25
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080026
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080027namespace android {
28namespace bpf {
29
Patrick Rohr99ace232024-11-22 10:35:29 -080030using ::android::base::borrowed_fd;
Patrick Rohrc8a9dc22024-11-22 11:00:50 -080031using ::android::base::unique_fd;
Patrick Rohr99ace232024-11-22 10:35:29 -080032
Maciej Żenczykowskibe020542023-06-13 18:47:57 -070033inline uint64_t ptr_to_u64(const void * const x) {
34 return (uint64_t)(uintptr_t)x;
35}
36
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080037/* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion
38 * of it that we are using. The kernel's bpf() system call will perform a strict check to ensure
39 * all unused portions are zero. It will fail with E2BIG if we don't fully zero bpf_attr.
40 */
41
Maciej Żenczykowskid8c03fe2022-12-16 20:59:00 +000042inline int bpf(enum bpf_cmd cmd, const bpf_attr& attr) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080043 return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
44}
45
Maciej Żenczykowski340e2ff2023-10-03 07:25:38 +000046// this version is meant for use with cmd's which mutate the argument
47inline int bpf(enum bpf_cmd cmd, bpf_attr *attr) {
48 return syscall(__NR_bpf, cmd, attr, sizeof(*attr));
49}
50
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080051inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
52 uint32_t max_entries, uint32_t map_flags) {
53 return bpf(BPF_MAP_CREATE, {
54 .map_type = map_type,
55 .key_size = key_size,
56 .value_size = value_size,
57 .max_entries = max_entries,
58 .map_flags = map_flags,
59 });
60}
61
Maciej Żenczykowskic6e41222023-06-12 22:50:02 -070062// Note:
63// 'map_type' must be one of BPF_MAP_TYPE_{ARRAY,HASH}_OF_MAPS
64// 'value_size' must be sizeof(u32), ie. 4
65// 'inner_map_fd' is basically a template specifying {map_type, key_size, value_size, max_entries, map_flags}
66// of the inner map type (and possibly only key_size/value_size actually matter?).
67inline int createOuterMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
Patrick Rohr99ace232024-11-22 10:35:29 -080068 uint32_t max_entries, uint32_t map_flags,
69 const borrowed_fd& inner_map_fd) {
Maciej Żenczykowskic6e41222023-06-12 22:50:02 -070070 return bpf(BPF_MAP_CREATE, {
71 .map_type = map_type,
72 .key_size = key_size,
73 .value_size = value_size,
74 .max_entries = max_entries,
75 .map_flags = map_flags,
Patrick Rohr99ace232024-11-22 10:35:29 -080076 .inner_map_fd = static_cast<__u32>(inner_map_fd.get()),
Maciej Żenczykowskic6e41222023-06-12 22:50:02 -070077 });
78}
79
Patrick Rohr99ace232024-11-22 10:35:29 -080080inline int writeToMapEntry(const borrowed_fd& map_fd, const void* key, const void* value,
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080081 uint64_t flags) {
82 return bpf(BPF_MAP_UPDATE_ELEM, {
Patrick Rohr99ace232024-11-22 10:35:29 -080083 .map_fd = static_cast<__u32>(map_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080084 .key = ptr_to_u64(key),
85 .value = ptr_to_u64(value),
86 .flags = flags,
87 });
88}
89
Patrick Rohr99ace232024-11-22 10:35:29 -080090inline int findMapEntry(const borrowed_fd& map_fd, const void* key, void* value) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080091 return bpf(BPF_MAP_LOOKUP_ELEM, {
Patrick Rohr99ace232024-11-22 10:35:29 -080092 .map_fd = static_cast<__u32>(map_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080093 .key = ptr_to_u64(key),
94 .value = ptr_to_u64(value),
95 });
96}
97
Patrick Rohr99ace232024-11-22 10:35:29 -080098inline int deleteMapEntry(const borrowed_fd& map_fd, const void* key) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -080099 return bpf(BPF_MAP_DELETE_ELEM, {
Patrick Rohr99ace232024-11-22 10:35:29 -0800100 .map_fd = static_cast<__u32>(map_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800101 .key = ptr_to_u64(key),
102 });
103}
104
Patrick Rohr99ace232024-11-22 10:35:29 -0800105inline int getNextMapKey(const borrowed_fd& map_fd, const void* key, void* next_key) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800106 return bpf(BPF_MAP_GET_NEXT_KEY, {
Patrick Rohr99ace232024-11-22 10:35:29 -0800107 .map_fd = static_cast<__u32>(map_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800108 .key = ptr_to_u64(key),
109 .next_key = ptr_to_u64(next_key),
110 });
111}
112
Patrick Rohr99ace232024-11-22 10:35:29 -0800113inline int getFirstMapKey(const borrowed_fd& map_fd, void* firstKey) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800114 return getNextMapKey(map_fd, NULL, firstKey);
115}
116
Patrick Rohr99ace232024-11-22 10:35:29 -0800117inline int bpfFdPin(const borrowed_fd& map_fd, const char* pathname) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800118 return bpf(BPF_OBJ_PIN, {
119 .pathname = ptr_to_u64(pathname),
Patrick Rohr99ace232024-11-22 10:35:29 -0800120 .bpf_fd = static_cast<__u32>(map_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800121 });
122}
123
124inline int bpfFdGet(const char* pathname, uint32_t flag) {
125 return bpf(BPF_OBJ_GET, {
126 .pathname = ptr_to_u64(pathname),
127 .file_flags = flag,
128 });
129}
130
Patrick Rohr99ace232024-11-22 10:35:29 -0800131int bpfGetFdMapId(const borrowed_fd& map_fd);
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000132
133inline int bpfLock(int fd, short type) {
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000134 if (fd < 0) return fd; // pass any errors straight through
Maciej Żenczykowski04fb3862024-06-15 00:14:16 +0000135#ifdef BPF_MAP_LOCKLESS_FOR_TEST
136 return fd;
137#endif
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000138 int mapId = bpfGetFdMapId(fd);
Maciej Żenczykowski4acfa1f2024-06-14 14:16:31 -0700139 int saved_errno = errno;
Maciej Żenczykowski4acfa1f2024-06-14 14:16:31 -0700140 // 4.14+ required to fetch map id, but we don't want to call isAtLeastKernelVersion
141 if (mapId == -1 && saved_errno == EINVAL) return fd;
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000142 if (mapId <= 0) abort(); // should not be possible
143
144 // on __LP64__ (aka. 64-bit userspace) 'struct flock64' is the same as 'struct flock'
145 struct flock64 fl = {
146 .l_type = type, // short: F_{RD,WR,UN}LCK
147 .l_whence = SEEK_SET, // short: SEEK_{SET,CUR,END}
148 .l_start = mapId, // off_t: start offset
149 .l_len = 1, // off_t: number of bytes
150 };
151
152 // see: bionic/libc/bionic/fcntl.cpp: iff !__LP64__ this uses fcntl64
153 int ret = fcntl(fd, F_OFD_SETLK, &fl);
154 if (!ret) return fd; // success
155 close(fd);
156 return ret; // most likely -1 with errno == EAGAIN, due to already held lock
157}
158
Maciej Żenczykowski0fff8392024-06-15 02:43:12 -0700159inline int mapRetrieveLocklessRW(const char* pathname) {
160 return bpfFdGet(pathname, 0);
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000161}
Maciej Żenczykowski0fff8392024-06-15 02:43:12 -0700162
163inline int mapRetrieveExclusiveRW(const char* pathname) {
164 return bpfLock(mapRetrieveLocklessRW(pathname), F_WRLCK);
165}
166
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800167inline int mapRetrieveRW(const char* pathname) {
Maciej Żenczykowski0fff8392024-06-15 02:43:12 -0700168 return bpfLock(mapRetrieveLocklessRW(pathname), F_RDLCK);
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800169}
170
171inline int mapRetrieveRO(const char* pathname) {
Maciej Żenczykowskidfef2292024-06-04 13:48:36 +0000172 return bpfFdGet(pathname, BPF_F_RDONLY);
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800173}
174
Maciej Żenczykowski52018c82024-06-04 16:05:16 +0000175// WARNING: it's impossible to grab a shared (ie. read) lock on a write-only fd,
Maciej Żenczykowski7eb7d672024-06-14 13:55:09 -0700176// so we instead choose to grab an exclusive (ie. write) lock.
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800177inline int mapRetrieveWO(const char* pathname) {
Maciej Żenczykowski7eb7d672024-06-14 13:55:09 -0700178 return bpfLock(bpfFdGet(pathname, BPF_F_WRONLY), F_WRLCK);
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800179}
180
181inline int retrieveProgram(const char* pathname) {
182 return bpfFdGet(pathname, BPF_F_RDONLY);
183}
184
Maciej Żenczykowskie950f6d2024-04-26 11:52:25 -0700185inline bool usableProgram(const char* pathname) {
Patrick Rohrc8a9dc22024-11-22 11:00:50 -0800186 unique_fd fd(retrieveProgram(pathname));
187 return fd.ok();
Maciej Żenczykowskie950f6d2024-04-26 11:52:25 -0700188}
189
Patrick Rohr99ace232024-11-22 10:35:29 -0800190inline int attachProgram(bpf_attach_type type, const borrowed_fd& prog_fd,
191 const borrowed_fd& cg_fd, uint32_t flags = 0) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800192 return bpf(BPF_PROG_ATTACH, {
Patrick Rohr99ace232024-11-22 10:35:29 -0800193 .target_fd = static_cast<__u32>(cg_fd.get()),
194 .attach_bpf_fd = static_cast<__u32>(prog_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800195 .attach_type = type,
KaiWen Zhengcfe2f2a2022-02-08 09:38:50 +0800196 .attach_flags = flags,
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800197 });
198}
199
Patrick Rohr99ace232024-11-22 10:35:29 -0800200inline int detachProgram(bpf_attach_type type, const borrowed_fd& cg_fd) {
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800201 return bpf(BPF_PROG_DETACH, {
Patrick Rohr99ace232024-11-22 10:35:29 -0800202 .target_fd = static_cast<__u32>(cg_fd.get()),
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800203 .attach_type = type,
204 });
205}
206
Patrick Rohr99ace232024-11-22 10:35:29 -0800207inline int queryProgram(const borrowed_fd& cg_fd,
Maciej Żenczykowski340e2ff2023-10-03 07:25:38 +0000208 enum bpf_attach_type attach_type,
209 __u32 query_flags = 0,
210 __u32 attach_flags = 0) {
211 int prog_id = -1; // equivalent to an array of one integer.
212 bpf_attr arg = {
213 .query = {
Patrick Rohr99ace232024-11-22 10:35:29 -0800214 .target_fd = static_cast<__u32>(cg_fd.get()),
Maciej Żenczykowski340e2ff2023-10-03 07:25:38 +0000215 .attach_type = attach_type,
216 .query_flags = query_flags,
217 .attach_flags = attach_flags,
218 .prog_ids = ptr_to_u64(&prog_id), // pointer to output array
219 .prog_cnt = 1, // in: space - nr of ints in the array, out: used
220 }
221 };
222 int v = bpf(BPF_PROG_QUERY, &arg);
223 if (v) return v; // error case
224 if (!arg.query.prog_cnt) return 0; // no program, kernel never returns zero id
225 return prog_id; // return actual id
226}
227
Patrick Rohr99ace232024-11-22 10:35:29 -0800228inline int detachSingleProgram(bpf_attach_type type, const borrowed_fd& prog_fd,
229 const borrowed_fd& cg_fd) {
KaiWen Zhengcfe2f2a2022-02-08 09:38:50 +0800230 return bpf(BPF_PROG_DETACH, {
Patrick Rohr99ace232024-11-22 10:35:29 -0800231 .target_fd = static_cast<__u32>(cg_fd.get()),
232 .attach_bpf_fd = static_cast<__u32>(prog_fd.get()),
KaiWen Zhengcfe2f2a2022-02-08 09:38:50 +0800233 .attach_type = type,
234 });
235}
236
Ryan Zuklie2669e242022-11-30 11:12:41 -0800237// Available in 4.12 and later kernels.
Patrick Rohr99ace232024-11-22 10:35:29 -0800238inline int runProgram(const borrowed_fd& prog_fd, const void* data,
Ryan Zuklie2669e242022-11-30 11:12:41 -0800239 const uint32_t data_size) {
240 return bpf(BPF_PROG_RUN, {
241 .test = {
Patrick Rohr99ace232024-11-22 10:35:29 -0800242 .prog_fd = static_cast<__u32>(prog_fd.get()),
Ryan Zuklie2669e242022-11-30 11:12:41 -0800243 .data_size_in = data_size,
Maciej Żenczykowski325f6752023-09-06 23:50:47 +0000244 .data_in = ptr_to_u64(data),
Ryan Zuklie2669e242022-11-30 11:12:41 -0800245 },
246 });
247}
248
Maciej Żenczykowski0ce77582022-06-20 18:11:03 -0700249// BPF_OBJ_GET_INFO_BY_FD requires 4.14+ kernel
250//
251// Note: some fields are only defined in newer kernels (ie. the map_info struct grows
252// over time), so we need to check that the field we're interested in is actually
253// supported/returned by the running kernel. We do this by checking it is fully
254// within the bounds of the struct size as reported by the kernel.
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700255#define DEFINE_BPF_GET_FD(TYPE, NAME, FIELD) \
Patrick Rohr99ace232024-11-22 10:35:29 -0800256inline int bpfGetFd ## NAME(const borrowed_fd& fd) { \
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700257 struct bpf_ ## TYPE ## _info info = {}; \
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700258 union bpf_attr attr = { .info = { \
Patrick Rohr99ace232024-11-22 10:35:29 -0800259 .bpf_fd = static_cast<__u32>(fd.get()), \
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700260 .info_len = sizeof(info), \
261 .info = ptr_to_u64(&info), \
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700262 }}; \
263 int rv = bpf(BPF_OBJ_GET_INFO_BY_FD, attr); \
264 if (rv) return rv; \
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700265 if (attr.info.info_len < offsetof(bpf_ ## TYPE ## _info, FIELD) + sizeof(info.FIELD)) { \
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700266 errno = EOPNOTSUPP; \
267 return -1; \
268 }; \
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700269 return info.FIELD; \
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700270}
271
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700272// All 7 of these fields are already present in Linux v4.14 (even ACK 4.14-P)
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700273// while BPF_OBJ_GET_INFO_BY_FD is not implemented at all in v4.9 (even ACK 4.9-Q)
Patrick Rohr99ace232024-11-22 10:35:29 -0800274DEFINE_BPF_GET_FD(map, MapType, type) // int bpfGetFdMapType(const borrowed_fd& map_fd)
275DEFINE_BPF_GET_FD(map, MapId, id) // int bpfGetFdMapId(const borrowed_fd& map_fd)
276DEFINE_BPF_GET_FD(map, KeySize, key_size) // int bpfGetFdKeySize(const borrowed_fd& map_fd)
277DEFINE_BPF_GET_FD(map, ValueSize, value_size) // int bpfGetFdValueSize(const borrowed_fd& map_fd)
278DEFINE_BPF_GET_FD(map, MaxEntries, max_entries) // int bpfGetFdMaxEntries(const borrowed_fd& map_fd)
279DEFINE_BPF_GET_FD(map, MapFlags, map_flags) // int bpfGetFdMapFlags(const borrowed_fd& map_fd)
280DEFINE_BPF_GET_FD(prog, ProgId, id) // int bpfGetFdProgId(const borrowed_fd& prog_fd)
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700281
Maciej Żenczykowski008f51e2023-06-13 18:27:57 -0700282#undef DEFINE_BPF_GET_FD
Maciej Żenczykowski5c5fae72022-05-25 12:58:31 -0700283
Maciej Żenczykowskia728a702021-01-11 19:08:33 -0800284} // namespace bpf
285} // namespace android
286