blob: 9a587fbc4d99d9cf3b0282506676915226467a9b [file] [log] [blame]
Chenbo Fengf2759682017-10-10 17:31:57 -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#include <linux/bpf.h>
18#include <linux/if_ether.h>
19#include <linux/in.h>
20#include <stdlib.h>
21#include <string.h>
Chenbo Fengcc6cbb72018-05-08 13:45:08 -070022#include <inttypes.h>
Chenbo Fengf2759682017-10-10 17:31:57 -070023#include <sys/socket.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080024#include <sys/stat.h>
Chenbo Fengf43bf812017-12-15 18:27:22 -080025#include <sys/utsname.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080026#include <sstream>
27#include <string>
Chenbo Fengf2759682017-10-10 17:31:57 -070028
Chenbo Fengcc6cbb72018-05-08 13:45:08 -070029#include <android-base/properties.h>
Chenbo Fengc10a8a42017-12-15 13:56:33 -080030#include <android-base/stringprintf.h>
31#include <android-base/unique_fd.h>
32#include <netdutils/Slice.h>
33#include <netdutils/StatusOr.h>
34#include "bpf/BpfUtils.h"
Chenbo Fengf2759682017-10-10 17:31:57 -070035
Chenbo Fengc10a8a42017-12-15 13:56:33 -080036using android::base::StringPrintf;
37using android::base::unique_fd;
Chenbo Fengcc6cbb72018-05-08 13:45:08 -070038using android::base::GetUintProperty;
Chenbo Fengf2759682017-10-10 17:31:57 -070039using android::netdutils::Slice;
Chenbo Fengc10a8a42017-12-15 13:56:33 -080040using android::netdutils::statusFromErrno;
41using android::netdutils::StatusOr;
Chenbo Fengf2759682017-10-10 17:31:57 -070042
Chenbo Feng2c67f262018-04-26 10:37:55 -070043#define ptr_to_u64(x) ((uint64_t)(uintptr_t)x)
44#define DEFAULT_LOG_LEVEL 1
45
Chenbo Fengf2759682017-10-10 17:31:57 -070046namespace android {
Chenbo Fengf2759682017-10-10 17:31:57 -070047namespace bpf {
48
Chenbo Feng29503212018-04-24 11:50:02 -070049/* The bpf_attr is a union which might have a much larger size then the struct we are using, while
50 * The inline initializer only reset the field we are using and leave the reset of the memory as
51 * is. The bpf kernel code will performs a much stricter check to ensure all unused field is 0. So
52 * this syscall will normally fail with E2BIG if we don't do a memset to bpf_attr.
53 */
Chenbo Feng2c67f262018-04-26 10:37:55 -070054bool operator==(const StatsKey& lhs, const StatsKey& rhs) {
55 return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag) && (lhs.counterSet == rhs.counterSet) &&
56 (lhs.ifaceIndex == rhs.ifaceIndex));
57}
58
59bool operator==(const UidTag& lhs, const UidTag& rhs) {
60 return ((lhs.uid == rhs.uid) && (lhs.tag == rhs.tag));
61}
62
63bool operator==(const StatsValue& lhs, const StatsValue& rhs) {
64 return ((lhs.rxBytes == rhs.rxBytes) && (lhs.txBytes == rhs.txBytes) &&
65 (lhs.rxPackets == rhs.rxPackets) && (lhs.txPackets == rhs.txPackets));
66}
67
Chenbo Fengf2759682017-10-10 17:31:57 -070068int bpf(int cmd, Slice bpfAttr) {
69 return syscall(__NR_bpf, cmd, bpfAttr.base(), bpfAttr.size());
70}
71
Chenbo Fengc10a8a42017-12-15 13:56:33 -080072int createMap(bpf_map_type map_type, uint32_t key_size, uint32_t value_size, uint32_t max_entries,
73 uint32_t map_flags) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -080074 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -070075 memset(&attr, 0, sizeof(attr));
76 attr.map_type = map_type;
77 attr.key_size = key_size;
78 attr.value_size = value_size;
79 attr.max_entries = max_entries;
80 attr.map_flags = map_flags;
81
82 return bpf(BPF_MAP_CREATE, Slice(&attr, sizeof(attr)));
83}
84
Chenbo Fengc10a8a42017-12-15 13:56:33 -080085int writeToMapEntry(const base::unique_fd& map_fd, void* key, void* value, uint64_t flags) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -080086 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -070087 memset(&attr, 0, sizeof(attr));
Chenbo Fengc10a8a42017-12-15 13:56:33 -080088 attr.map_fd = map_fd.get();
Chenbo Fengf2759682017-10-10 17:31:57 -070089 attr.key = ptr_to_u64(key);
90 attr.value = ptr_to_u64(value);
91 attr.flags = flags;
92
93 return bpf(BPF_MAP_UPDATE_ELEM, Slice(&attr, sizeof(attr)));
94}
95
Chenbo Fengc10a8a42017-12-15 13:56:33 -080096int findMapEntry(const base::unique_fd& map_fd, void* key, void* value) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -080097 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -070098 memset(&attr, 0, sizeof(attr));
Chenbo Fengc10a8a42017-12-15 13:56:33 -080099 attr.map_fd = map_fd.get();
Chenbo Fengf2759682017-10-10 17:31:57 -0700100 attr.key = ptr_to_u64(key);
101 attr.value = ptr_to_u64(value);
102
103 return bpf(BPF_MAP_LOOKUP_ELEM, Slice(&attr, sizeof(attr)));
104}
105
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800106int deleteMapEntry(const base::unique_fd& map_fd, void* key) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800107 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700108 memset(&attr, 0, sizeof(attr));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800109 attr.map_fd = map_fd.get();
Chenbo Fengf2759682017-10-10 17:31:57 -0700110 attr.key = ptr_to_u64(key);
111
112 return bpf(BPF_MAP_DELETE_ELEM, Slice(&attr, sizeof(attr)));
113}
114
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800115int getNextMapKey(const base::unique_fd& map_fd, void* key, void* next_key) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800116 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700117 memset(&attr, 0, sizeof(attr));
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800118 attr.map_fd = map_fd.get();
Chenbo Fengf2759682017-10-10 17:31:57 -0700119 attr.key = ptr_to_u64(key);
120 attr.next_key = ptr_to_u64(next_key);
121
122 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
123}
124
Chenbo Feng29503212018-04-24 11:50:02 -0700125int getFirstMapKey(const base::unique_fd& map_fd, void* firstKey) {
126 bpf_attr attr;
127 memset(&attr, 0, sizeof(attr));
128 attr.map_fd = map_fd.get();
129 attr.key = 0;
130 attr.next_key = ptr_to_u64(firstKey);
131
132 return bpf(BPF_MAP_GET_NEXT_KEY, Slice(&attr, sizeof(attr)));
133}
134
Chenbo Fengf2759682017-10-10 17:31:57 -0700135int bpfProgLoad(bpf_prog_type prog_type, Slice bpf_insns, const char* license,
136 uint32_t kern_version, Slice bpf_log) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800137 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700138 memset(&attr, 0, sizeof(attr));
139 attr.prog_type = prog_type;
140 attr.insns = ptr_to_u64(bpf_insns.base());
141 attr.insn_cnt = bpf_insns.size() / sizeof(struct bpf_insn);
142 attr.license = ptr_to_u64((void*)license);
143 attr.log_buf = ptr_to_u64(bpf_log.base());
144 attr.log_size = bpf_log.size();
145 attr.log_level = DEFAULT_LOG_LEVEL;
146 attr.kern_version = kern_version;
147 int ret = bpf(BPF_PROG_LOAD, Slice(&attr, sizeof(attr)));
148
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800149 if (ret < 0) {
150 std::string prog_log = netdutils::toString(bpf_log);
151 std::istringstream iss(prog_log);
152 for (std::string line; std::getline(iss, line);) {
153 ALOGE("%s", line.c_str());
154 }
155 }
Chenbo Fengf2759682017-10-10 17:31:57 -0700156 return ret;
157}
158
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800159int mapPin(const base::unique_fd& map_fd, const char* pathname) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800160 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700161 memset(&attr, 0, sizeof(attr));
162 attr.pathname = ptr_to_u64((void*)pathname);
Chenbo Fengc10a8a42017-12-15 13:56:33 -0800163 attr.bpf_fd = map_fd.get();
Chenbo Fengf2759682017-10-10 17:31:57 -0700164
165 return bpf(BPF_OBJ_PIN, Slice(&attr, sizeof(attr)));
166}
167
Chenbo Fengbe33bd52017-11-27 18:21:54 -0800168int mapRetrieve(const char* pathname, uint32_t flag) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800169 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700170 memset(&attr, 0, sizeof(attr));
171 attr.pathname = ptr_to_u64((void*)pathname);
Chenbo Fengbe33bd52017-11-27 18:21:54 -0800172 attr.file_flags = flag;
Chenbo Fengf2759682017-10-10 17:31:57 -0700173 return bpf(BPF_OBJ_GET, Slice(&attr, sizeof(attr)));
174}
175
176int attachProgram(bpf_attach_type type, uint32_t prog_fd, uint32_t cg_fd) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800177 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700178 memset(&attr, 0, sizeof(attr));
179 attr.target_fd = cg_fd;
180 attr.attach_bpf_fd = prog_fd;
181 attr.attach_type = type;
182
183 return bpf(BPF_PROG_ATTACH, Slice(&attr, sizeof(attr)));
184}
185
186int detachProgram(bpf_attach_type type, uint32_t cg_fd) {
Christopher Ferrisaa31c622018-01-31 17:20:25 -0800187 bpf_attr attr;
Chenbo Fengf2759682017-10-10 17:31:57 -0700188 memset(&attr, 0, sizeof(attr));
189 attr.target_fd = cg_fd;
190 attr.attach_type = type;
191
192 return bpf(BPF_PROG_DETACH, Slice(&attr, sizeof(attr)));
193}
194
Chenbo Fenged37fea2017-12-13 19:35:01 -0800195uint64_t getSocketCookie(int sockFd) {
196 uint64_t sock_cookie;
197 socklen_t cookie_len = sizeof(sock_cookie);
198 int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len);
199 if (res < 0) {
200 res = -errno;
201 ALOGE("Failed to get socket cookie: %s\n", strerror(errno));
202 errno = -res;
Chenbo Feng0cef0cd2018-04-13 19:50:49 -0700203 // 0 is an invalid cookie. See sock_gen_cookie.
204 return NONEXISTENT_COOKIE;
Chenbo Fenged37fea2017-12-13 19:35:01 -0800205 }
206 return sock_cookie;
207}
208
Chenbo Fengf43bf812017-12-15 18:27:22 -0800209bool hasBpfSupport() {
210 struct utsname buf;
211 int kernel_version_major;
212 int kernel_version_minor;
213
Chenbo Fengcc6cbb72018-05-08 13:45:08 -0700214 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
215 if (api_level == 0) {
216 ALOGE("Cannot determine initial API level of the device");
217 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
218 }
219
Chenbo Fengf43bf812017-12-15 18:27:22 -0800220 int ret = uname(&buf);
221 if (ret) {
222 return false;
223 }
224 char dummy;
225 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
Chenbo Fengcc6cbb72018-05-08 13:45:08 -0700226 if (ret >= 2 && ((kernel_version_major > 4) ||
227 (kernel_version_major == 4 && kernel_version_minor >= 9))) {
228 // Check if the device is shipped originally with android P.
229 return api_level >= MINIMUM_API_REQUIRED;
230 }
231 return false;
Chenbo Fengf43bf812017-12-15 18:27:22 -0800232}
233
Chenbo Fengf2759682017-10-10 17:31:57 -0700234} // namespace bpf
Chenbo Fengf2759682017-10-10 17:31:57 -0700235} // namespace android