blob: 046d80f28cccfc194f9ea076bbe2dc754412e40e [file] [log] [blame]
Chenbo Feng75b410b2018-10-10 15:01:19 -07001/*
2 * Copyright (C) 2017 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#ifndef BPF_BPFUTILS_H
18#define BPF_BPFUTILS_H
19
20#include <linux/bpf.h>
21#include <linux/if_ether.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070022#include <linux/unistd.h>
23#include <net/if.h>
24#include <stdlib.h>
25#include <string.h>
26#include <sys/socket.h>
27
Steven Morelande7cd2a72020-01-10 17:49:35 -080028#include <string>
29
markchien90b02a02020-11-13 10:54:25 +080030#ifdef BPF_FD_JUST_USE_INT
31 #define BPF_FD_TYPE int
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -080032 #define BPF_FD_TO_U32(x) static_cast<__u32>(x)
markchien90b02a02020-11-13 10:54:25 +080033#else
34 #include <android-base/unique_fd.h>
35 #define BPF_FD_TYPE base::unique_fd&
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -080036 #define BPF_FD_TO_U32(x) static_cast<__u32>((x).get())
markchien90b02a02020-11-13 10:54:25 +080037#endif
Chenbo Feng75b410b2018-10-10 15:01:19 -070038
Chenbo Feng75b410b2018-10-10 15:01:19 -070039#define ptr_to_u64(x) ((uint64_t)(uintptr_t)(x))
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070040
Chenbo Feng75b410b2018-10-10 15:01:19 -070041namespace android {
42namespace bpf {
43
Chenbo Feng79b7e612018-12-11 12:24:23 -080044enum class BpfLevel {
45 // Devices shipped before P or kernel version is lower than 4.9 do not
46 // have eBPF enabled.
47 NONE,
48 // Devices shipped in P with android 4.9 kernel only have the basic eBPF
49 // functionalities such as xt_bpf and cgroup skb filter.
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -080050 BASIC_4_9,
Chenbo Feng79b7e612018-12-11 12:24:23 -080051 // For devices that have 4.14 kernel. It supports advanced features like
52 // map_in_map and cgroup socket filter.
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -080053 EXTENDED_4_14,
54 EXTENDED_4_19,
55 EXTENDED_5_4,
Chenbo Feng79b7e612018-12-11 12:24:23 -080056};
57
Chenbo Feng75b410b2018-10-10 15:01:19 -070058constexpr const int OVERFLOW_COUNTERSET = 2;
59
60constexpr const uint64_t NONEXISTENT_COOKIE = 0;
61
62constexpr const int MINIMUM_API_REQUIRED = 28;
63
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080064/* Note: bpf_attr is a union which might have a much larger size then the anonymous struct portion
65 * of it that we are using. The kernel's bpf() system call will perform a strict check to ensure
66 * all unused portions are zero. It will fail with E2BIG if we don't fully zero bpf_attr.
67 */
68
Maciej Żenczykowski06caf872020-02-11 16:42:21 -080069inline int bpf(int cmd, const bpf_attr& attr) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080070 return syscall(__NR_bpf, cmd, &attr, sizeof(attr));
71}
72
Maciej Żenczykowski06caf872020-02-11 16:42:21 -080073inline int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size,
74 uint32_t max_entries, uint32_t map_flags) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080075 return bpf(BPF_MAP_CREATE, {
76 .map_type = map_type,
77 .key_size = key_size,
78 .value_size = value_size,
79 .max_entries = max_entries,
80 .map_flags = map_flags,
81 });
82}
83
markchien90b02a02020-11-13 10:54:25 +080084inline int writeToMapEntry(const BPF_FD_TYPE map_fd, const void* key, const void* value,
Maciej Żenczykowski06caf872020-02-11 16:42:21 -080085 uint64_t flags) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080086 return bpf(BPF_MAP_UPDATE_ELEM, {
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -080087 .map_fd = BPF_FD_TO_U32(map_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080088 .key = ptr_to_u64(key),
89 .value = ptr_to_u64(value),
90 .flags = flags,
91 });
92}
93
markchien90b02a02020-11-13 10:54:25 +080094inline int findMapEntry(const BPF_FD_TYPE map_fd, const void* key, void* value) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080095 return bpf(BPF_MAP_LOOKUP_ELEM, {
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -080096 .map_fd = BPF_FD_TO_U32(map_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -080097 .key = ptr_to_u64(key),
98 .value = ptr_to_u64(value),
99 });
100}
101
markchien90b02a02020-11-13 10:54:25 +0800102inline int deleteMapEntry(const BPF_FD_TYPE map_fd, const void* key) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800103 return bpf(BPF_MAP_DELETE_ELEM, {
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -0800104 .map_fd = BPF_FD_TO_U32(map_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800105 .key = ptr_to_u64(key),
106 });
107}
108
markchien90b02a02020-11-13 10:54:25 +0800109inline int getNextMapKey(const BPF_FD_TYPE map_fd, const void* key, void* next_key) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800110 return bpf(BPF_MAP_GET_NEXT_KEY, {
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -0800111 .map_fd = BPF_FD_TO_U32(map_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800112 .key = ptr_to_u64(key),
113 .next_key = ptr_to_u64(next_key),
114 });
115}
116
markchien90b02a02020-11-13 10:54:25 +0800117inline int getFirstMapKey(const BPF_FD_TYPE map_fd, void* firstKey) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800118 return getNextMapKey(map_fd, NULL, firstKey);
119}
120
markchien90b02a02020-11-13 10:54:25 +0800121inline int bpfFdPin(const BPF_FD_TYPE map_fd, const char* pathname) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800122 return bpf(BPF_OBJ_PIN, {
123 .pathname = ptr_to_u64(pathname),
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -0800124 .bpf_fd = BPF_FD_TO_U32(map_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800125 });
126}
127
Maciej Żenczykowski06caf872020-02-11 16:42:21 -0800128inline int bpfFdGet(const char* pathname, uint32_t flag) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800129 return bpf(BPF_OBJ_GET, {
130 .pathname = ptr_to_u64(pathname),
131 .file_flags = flag,
132 });
133}
134
Maciej Żenczykowski06caf872020-02-11 16:42:21 -0800135inline int mapRetrieve(const char* pathname, uint32_t flag) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800136 return bpfFdGet(pathname, flag);
137}
138
Maciej Żenczykowskiaa295c82020-06-16 17:02:48 -0700139inline int mapRetrieveRW(const char* pathname) {
140 return mapRetrieve(pathname, 0);
141}
142
143inline int mapRetrieveRO(const char* pathname) {
144 return mapRetrieve(pathname, BPF_F_RDONLY);
145}
146
147inline int mapRetrieveWO(const char* pathname) {
148 return mapRetrieve(pathname, BPF_F_WRONLY);
149}
150
151inline int retrieveProgram(const char* pathname) {
152 return bpfFdGet(pathname, BPF_F_RDONLY);
153}
154
markchien90b02a02020-11-13 10:54:25 +0800155inline int attachProgram(bpf_attach_type type, const BPF_FD_TYPE prog_fd,
156 const BPF_FD_TYPE cg_fd) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800157 return bpf(BPF_PROG_ATTACH, {
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -0800158 .target_fd = BPF_FD_TO_U32(cg_fd),
159 .attach_bpf_fd = BPF_FD_TO_U32(prog_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800160 .attach_type = type,
161 });
162}
163
markchien90b02a02020-11-13 10:54:25 +0800164inline int detachProgram(bpf_attach_type type, const BPF_FD_TYPE cg_fd) {
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800165 return bpf(BPF_PROG_DETACH, {
Maciej Żenczykowski38fb4302020-11-30 17:06:15 -0800166 .target_fd = BPF_FD_TO_U32(cg_fd),
Maciej Żenczykowskib479fd62020-01-16 02:18:11 -0800167 .attach_type = type,
168 });
169}
170
Chenbo Feng75b410b2018-10-10 15:01:19 -0700171uint64_t getSocketCookie(int sockFd);
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -0800172int synchronizeKernelRCU();
Chenbo Feng0a1a9a12019-04-09 12:05:04 -0700173int setrlimitForTest();
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800174unsigned kernelVersion();
Chenbo Feng79b7e612018-12-11 12:24:23 -0800175std::string BpfLevelToString(BpfLevel BpfLevel);
176BpfLevel getBpfSupportLevel();
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -0800177
Maciej Żenczykowski06caf872020-02-11 16:42:21 -0800178inline bool isBpfSupported() {
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -0800179 return getBpfSupportLevel() != BpfLevel::NONE;
180}
Chenbo Feng75b410b2018-10-10 15:01:19 -0700181
Chenbo Feng79b7e612018-12-11 12:24:23 -0800182#define SKIP_IF_BPF_NOT_SUPPORTED \
183 do { \
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -0800184 if (!android::bpf::isBpfSupported()) { \
Chenbo Feng79b7e612018-12-11 12:24:23 -0800185 GTEST_LOG_(INFO) << "This test is skipped since bpf is not available\n"; \
186 return; \
187 } \
188 } while (0)
189
Maciej Żenczykowskic3a640d2020-02-11 15:01:21 -0800190#define SKIP_IF_BPF_SUPPORTED \
191 do { \
192 if (android::bpf::isBpfSupported()) return; \
Chenbo Feng75b410b2018-10-10 15:01:19 -0700193 } while (0)
194
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -0800195#define SKIP_IF_EXTENDED_BPF_NOT_SUPPORTED \
196 do { \
197 if (android::bpf::getBpfSupportLevel() < android::bpf::BpfLevel::EXTENDED_4_14) { \
198 GTEST_LOG_(INFO) << "This test is skipped since extended bpf feature" \
199 << "not supported\n"; \
200 return; \
201 } \
Maciej Żenczykowski672b0e72020-02-12 04:15:20 -0800202 } while (0)
203
Chenbo Feng75b410b2018-10-10 15:01:19 -0700204} // namespace bpf
205} // namespace android
206
207#endif