blob: 32b412028c0930e643e7463c62fecf5d9e495600 [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#define LOG_TAG "BpfUtils"
18
Bernie Innocenti26ffded2018-10-19 15:41:53 +090019#include "bpf/BpfUtils.h"
20
Chenbo Feng75b410b2018-10-10 15:01:19 -070021#include <elf.h>
22#include <inttypes.h>
23#include <linux/bpf.h>
24#include <linux/if_ether.h>
25#include <linux/in.h>
Chenbo Feng9cd8f142018-12-04 16:54:56 -080026#include <linux/pfkeyv2.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070027#include <stdlib.h>
28#include <string.h>
29#include <sys/mman.h>
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070030#include <sys/resource.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070031#include <sys/socket.h>
32#include <sys/stat.h>
33#include <sys/utsname.h>
34#include <sstream>
35#include <string>
36
37#include <android-base/properties.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070038#include <android-base/unique_fd.h>
Bernie Innocenti26ffded2018-10-19 15:41:53 +090039#include <log/log.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070040#include <netdutils/MemBlock.h>
41#include <netdutils/Slice.h>
Suren Baghdasaryan9217ccb2018-12-19 17:29:13 -080042#include <processgroup/processgroup.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070043
44using android::base::GetUintProperty;
Chenbo Feng75b410b2018-10-10 15:01:19 -070045using android::base::unique_fd;
46using android::netdutils::MemBlock;
47using android::netdutils::Slice;
Chenbo Feng75b410b2018-10-10 15:01:19 -070048
Chenbo Feng9cd8f142018-12-04 16:54:56 -080049// The buffer size for the buffer that records program loading logs, needs to be large enough for
50// the largest kernel program.
Chenbo Feng75b410b2018-10-10 15:01:19 -070051
52namespace android {
53namespace bpf {
54
55/* The bpf_attr is a union which might have a much larger size then the struct we are using, while
56 * The inline initializer only reset the field we are using and leave the reset of the memory as
57 * is. The bpf kernel code will performs a much stricter check to ensure all unused field is 0. So
58 * this syscall will normally fail with E2BIG if we don't do a memset to bpf_attr.
59 */
Chenbo Feng75b410b2018-10-10 15:01:19 -070060
61int bpf(int cmd, Slice bpfAttr) {
62 return syscall(__NR_bpf, cmd, bpfAttr.base(), bpfAttr.size());
63}
64
65int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
66 uint32_t map_flags) {
67 bpf_attr attr;
68 memset(&attr, 0, sizeof(attr));
69 attr.map_type = map_type;
70 attr.key_size = key_size;
71 attr.value_size = value_size;
72 attr.max_entries = max_entries;
73 attr.map_flags = map_flags;
74
75 return bpf(BPF_MAP_CREATE, Slice(&attr, sizeof(attr)));
76}
77
78int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags) {
79 bpf_attr attr;
80 memset(&attr, 0, sizeof(attr));
81 attr.map_fd = map_fd.get();
82 attr.key = ptr_to_u64(key);
83 attr.value = ptr_to_u64(value);
84 attr.flags = flags;
85
86 return bpf(BPF_MAP_UPDATE_ELEM, Slice(&attr, sizeof(attr)));
87}
88
89int findMapEntry(const base::unique_fd& map_fd, void* key, void* value) {
90 bpf_attr attr;
91 memset(&attr, 0, sizeof(attr));
92 attr.map_fd = map_fd.get();
93 attr.key = ptr_to_u64(key);
94 attr.value = ptr_to_u64(value);
95
96 return bpf(BPF_MAP_LOOKUP_ELEM, Slice(&attr, sizeof(attr)));
97}
98
99int deleteMapEntry(const base::unique_fd& map_fd, void* key) {
100 bpf_attr attr;
101 memset(&attr, 0, sizeof(attr));
102 attr.map_fd = map_fd.get();
103 attr.key = ptr_to_u64(key);
104
105 return bpf(BPF_MAP_DELETE_ELEM, Slice(&attr, sizeof(attr)));
106}
107
108int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key) {
109 bpf_attr attr;
110 memset(&attr, 0, sizeof(attr));
111 attr.map_fd = map_fd.get();
112 attr.key = ptr_to_u64(key);
113 attr.next_key = ptr_to_u64(next_key);
114
115 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
116}
117
118int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) {
119 bpf_attr attr;
120 memset(&attr, 0, sizeof(attr));
121 attr.map_fd = map_fd.get();
122 attr.key = 0;
123 attr.next_key = ptr_to_u64(firstKey);
124
125 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
126}
127
Chenbo Feng75b410b2018-10-10 15:01:19 -0700128int bpfFdPin(const base::unique_fd& map_fd, const char* pathname) {
129 bpf_attr attr;
130 memset(&attr, 0, sizeof(attr));
131 attr.pathname = ptr_to_u64((void*)pathname);
132 attr.bpf_fd = map_fd.get();
133
134 return bpf(BPF_OBJ_PIN, Slice(&attr, sizeof(attr)));
135}
136
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800137int bpfFdGet(const char* pathname, uint32_t flag) {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700138 bpf_attr attr;
139 memset(&attr, 0, sizeof(attr));
140 attr.pathname = ptr_to_u64((void*)pathname);
141 attr.file_flags = flag;
142 return bpf(BPF_OBJ_GET, Slice(&attr, sizeof(attr)));
143}
144
Chenbo Fengc1dd7642018-12-22 11:41:20 -0800145int mapRetrieve(const char* pathname, uint32_t flag) {
146 return bpfFdGet(pathname, flag);
147}
148
Chenbo Feng75b410b2018-10-10 15:01:19 -0700149int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) {
150 bpf_attr attr;
151 memset(&attr, 0, sizeof(attr));
152 attr.target_fd = cg_fd;
153 attr.attach_bpf_fd = prog_fd;
154 attr.attach_type = type;
155
156 return bpf(BPF_PROG_ATTACH, Slice(&attr, sizeof(attr)));
157}
158
159int detachProgram(bpf_attach_type type, uint32_t cg_fd) {
160 bpf_attr attr;
161 memset(&attr, 0, sizeof(attr));
162 attr.target_fd = cg_fd;
163 attr.attach_type = type;
164
165 return bpf(BPF_PROG_DETACH, Slice(&attr, sizeof(attr)));
166}
167
168uint64_t getSocketCookie(int sockFd) {
169 uint64_t sock_cookie;
170 socklen_t cookie_len = sizeof(sock_cookie);
171 int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len);
172 if (res < 0) {
173 res = -errno;
174 ALOGE("Failed to get socket cookie: %s\n", strerror(errno));
175 errno = -res;
176 // 0 is an invalid cookie. See sock_gen_cookie.
177 return NONEXISTENT_COOKIE;
178 }
179 return sock_cookie;
180}
181
Chenbo Feng9cd8f142018-12-04 16:54:56 -0800182int synchronizeKernelRCU() {
183 // This is a temporary hack for network stats map swap on devices running
184 // 4.9 kernels. The kernel code of socket release on pf_key socket will
185 // explicitly call synchronize_rcu() which is exactly what we need.
186 int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2);
187
188 if (pfSocket < 0) {
189 int ret = -errno;
190 ALOGE("create PF_KEY socket failed: %s", strerror(errno));
191 return ret;
192 }
193
194 // When closing socket, synchronize_rcu() gets called in sock_release().
195 if (close(pfSocket)) {
196 int ret = -errno;
197 ALOGE("failed to close the PF_KEY socket: %s", strerror(errno));
198 return ret;
199 }
200 return 0;
201}
202
Chenbo Feng0a1a9a12019-04-09 12:05:04 -0700203int setrlimitForTest() {
204 // Set the memory rlimit for the test process if the default MEMLOCK rlimit is not enough.
205 struct rlimit limit = {
206 .rlim_cur = TEST_LIMIT,
207 .rlim_max = TEST_LIMIT,
208 };
209 int res = setrlimit(RLIMIT_MEMLOCK, &limit);
210 if (res) {
211 ALOGE("Failed to set the default MEMLOCK rlimit: %s", strerror(errno));
212 }
213 return res;
214}
215
Chenbo Feng79b7e612018-12-11 12:24:23 -0800216std::string BpfLevelToString(BpfLevel bpfLevel) {
217 switch (bpfLevel) {
218 case BpfLevel::NONE: return "NONE_SUPPORT";
219 case BpfLevel::BASIC: return "BPF_LEVEL_BASIC";
220 case BpfLevel::EXTENDED: return "BPF_LEVEL_EXTENDED";
221 // No default statement. We want to see errors of the form:
222 // "enumeration value 'BPF_LEVEL_xxx' not handled in switch [-Werror,-Wswitch]".
223 }
224}
225
226BpfLevel getBpfSupportLevel() {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700227 struct utsname buf;
228 int kernel_version_major;
229 int kernel_version_minor;
230
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000231 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
232 if (api_level == 0) {
233 ALOGE("Cannot determine initial API level of the device");
234 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
235 }
236
237 // Check if the device is shipped originally with android P.
238 if (api_level < MINIMUM_API_REQUIRED) return BpfLevel::NONE;
239
Chenbo Feng75b410b2018-10-10 15:01:19 -0700240 int ret = uname(&buf);
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000241 if (ret) {
242 return BpfLevel::NONE;
243 }
Chenbo Feng75b410b2018-10-10 15:01:19 -0700244 char dummy;
245 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000246 // Check the device kernel version
Chenbo Feng79b7e612018-12-11 12:24:23 -0800247 if (ret < 2) return BpfLevel::NONE;
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000248 if (kernel_version_major > 4 || (kernel_version_major == 4 && kernel_version_minor >= 14))
Chenbo Feng79b7e612018-12-11 12:24:23 -0800249 return BpfLevel::EXTENDED;
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000250 if (kernel_version_major == 4 && kernel_version_minor >= 9) return BpfLevel::BASIC;
Chenbo Feng79b7e612018-12-11 12:24:23 -0800251
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000252 return BpfLevel::NONE;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700253}
254
Chenbo Feng75b410b2018-10-10 15:01:19 -0700255} // namespace bpf
256} // namespace android