blob: d7f5c9beecff145e5865ff3993f35389f47b657d [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>
Suren Baghdasaryan9217ccb2018-12-19 17:29:13 -080040#include <processgroup/processgroup.h>
Chenbo Feng75b410b2018-10-10 15:01:19 -070041
42using android::base::GetUintProperty;
Chenbo Feng75b410b2018-10-10 15:01:19 -070043using android::base::unique_fd;
Chenbo Feng75b410b2018-10-10 15:01:19 -070044
Chenbo Feng9cd8f142018-12-04 16:54:56 -080045// The buffer size for the buffer that records program loading logs, needs to be large enough for
46// the largest kernel program.
Chenbo Feng75b410b2018-10-10 15:01:19 -070047
48namespace android {
49namespace bpf {
50
Chenbo Feng75b410b2018-10-10 15:01:19 -070051uint64_t getSocketCookie(int sockFd) {
52 uint64_t sock_cookie;
53 socklen_t cookie_len = sizeof(sock_cookie);
54 int res = getsockopt(sockFd, SOL_SOCKET, SO_COOKIE, &sock_cookie, &cookie_len);
55 if (res < 0) {
56 res = -errno;
57 ALOGE("Failed to get socket cookie: %s\n", strerror(errno));
58 errno = -res;
59 // 0 is an invalid cookie. See sock_gen_cookie.
60 return NONEXISTENT_COOKIE;
61 }
62 return sock_cookie;
63}
64
Chenbo Feng9cd8f142018-12-04 16:54:56 -080065int synchronizeKernelRCU() {
66 // This is a temporary hack for network stats map swap on devices running
67 // 4.9 kernels. The kernel code of socket release on pf_key socket will
68 // explicitly call synchronize_rcu() which is exactly what we need.
69 int pfSocket = socket(AF_KEY, SOCK_RAW | SOCK_CLOEXEC, PF_KEY_V2);
70
71 if (pfSocket < 0) {
72 int ret = -errno;
73 ALOGE("create PF_KEY socket failed: %s", strerror(errno));
74 return ret;
75 }
76
77 // When closing socket, synchronize_rcu() gets called in sock_release().
78 if (close(pfSocket)) {
79 int ret = -errno;
80 ALOGE("failed to close the PF_KEY socket: %s", strerror(errno));
81 return ret;
82 }
83 return 0;
84}
85
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070086int setrlimitForTest() {
87 // Set the memory rlimit for the test process if the default MEMLOCK rlimit is not enough.
88 struct rlimit limit = {
89 .rlim_cur = TEST_LIMIT,
90 .rlim_max = TEST_LIMIT,
91 };
92 int res = setrlimit(RLIMIT_MEMLOCK, &limit);
93 if (res) {
94 ALOGE("Failed to set the default MEMLOCK rlimit: %s", strerror(errno));
95 }
96 return res;
97}
98
Chenbo Feng79b7e612018-12-11 12:24:23 -080099std::string BpfLevelToString(BpfLevel bpfLevel) {
100 switch (bpfLevel) {
101 case BpfLevel::NONE: return "NONE_SUPPORT";
102 case BpfLevel::BASIC: return "BPF_LEVEL_BASIC";
103 case BpfLevel::EXTENDED: return "BPF_LEVEL_EXTENDED";
104 // No default statement. We want to see errors of the form:
105 // "enumeration value 'BPF_LEVEL_xxx' not handled in switch [-Werror,-Wswitch]".
106 }
107}
108
109BpfLevel getBpfSupportLevel() {
Chenbo Feng75b410b2018-10-10 15:01:19 -0700110 struct utsname buf;
111 int kernel_version_major;
112 int kernel_version_minor;
113
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000114 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
115 if (api_level == 0) {
116 ALOGE("Cannot determine initial API level of the device");
117 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
118 }
119
120 // Check if the device is shipped originally with android P.
121 if (api_level < MINIMUM_API_REQUIRED) return BpfLevel::NONE;
122
Chenbo Feng75b410b2018-10-10 15:01:19 -0700123 int ret = uname(&buf);
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000124 if (ret) {
125 return BpfLevel::NONE;
126 }
Chenbo Feng75b410b2018-10-10 15:01:19 -0700127 char dummy;
128 ret = sscanf(buf.release, "%d.%d%c", &kernel_version_major, &kernel_version_minor, &dummy);
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000129 // Check the device kernel version
Chenbo Feng79b7e612018-12-11 12:24:23 -0800130 if (ret < 2) return BpfLevel::NONE;
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000131 if (kernel_version_major > 4 || (kernel_version_major == 4 && kernel_version_minor >= 14))
Chenbo Feng79b7e612018-12-11 12:24:23 -0800132 return BpfLevel::EXTENDED;
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000133 if (kernel_version_major == 4 && kernel_version_minor >= 9) return BpfLevel::BASIC;
Chenbo Feng79b7e612018-12-11 12:24:23 -0800134
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000135 return BpfLevel::NONE;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700136}
137
Chenbo Feng75b410b2018-10-10 15:01:19 -0700138} // namespace bpf
139} // namespace android