blob: c7c56d5ea0e1c975c32319dbcbbc153b0a10f72b [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 = {
Maciej Żenczykowski974e0ed2020-01-20 15:55:57 -080089 .rlim_cur = 8388608, // 8 MiB
90 .rlim_max = 8388608, // 8 MiB
Chenbo Feng0a1a9a12019-04-09 12:05:04 -070091 };
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
Maciej Żenczykowski07375e22020-02-19 14:23:59 -080099#define KVER(a, b, c) ((a)*65536 + (b)*256 + (c))
100
101unsigned kernelVersion() {
102 struct utsname buf;
103 int ret = uname(&buf);
104 if (ret) return 0;
105
106 unsigned kver_major;
107 unsigned kver_minor;
108 unsigned kver_sub;
109 char dummy;
110 ret = sscanf(buf.release, "%u.%u.%u%c", &kver_major, &kver_minor, &kver_sub, &dummy);
111 // Check the device kernel version
112 if (ret < 3) return 0;
113
114 return KVER(kver_major, kver_minor, kver_sub);
115}
116
Chenbo Feng79b7e612018-12-11 12:24:23 -0800117std::string BpfLevelToString(BpfLevel bpfLevel) {
118 switch (bpfLevel) {
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -0800119 case BpfLevel::NONE:
120 return "None [pre-4.9]";
121 case BpfLevel::BASIC_4_9:
122 return "Basic [4.9]";
123 case BpfLevel::EXTENDED_4_14:
124 return "Extended [4.14]";
125 case BpfLevel::EXTENDED_4_19:
126 return "Extended [4.19]";
127 case BpfLevel::EXTENDED_5_4:
128 return "Extended [5.4+]";
129 // No default statement. We want to see errors of the form:
130 // "enumeration value 'BPF_LEVEL_xxx' not handled in switch [-Werror,-Wswitch]".
Chenbo Feng79b7e612018-12-11 12:24:23 -0800131 }
132}
133
Maciej Żenczykowskifd90bc82020-02-10 19:27:47 -0800134static BpfLevel getUncachedBpfSupportLevel() {
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000135 uint64_t api_level = GetUintProperty<uint64_t>("ro.product.first_api_level", 0);
136 if (api_level == 0) {
137 ALOGE("Cannot determine initial API level of the device");
138 api_level = GetUintProperty<uint64_t>("ro.build.version.sdk", 0);
139 }
140
141 // Check if the device is shipped originally with android P.
142 if (api_level < MINIMUM_API_REQUIRED) return BpfLevel::NONE;
143
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800144 unsigned kver = kernelVersion();
Maciej Żenczykowski8a09a5a2020-02-17 17:54:51 -0800145
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800146 if (kver >= KVER(5, 4, 0)) return BpfLevel::EXTENDED_5_4;
147 if (kver >= KVER(4, 19, 0)) return BpfLevel::EXTENDED_4_19;
148 if (kver >= KVER(4, 14, 0)) return BpfLevel::EXTENDED_4_14;
149 if (kver >= KVER(4, 9, 0)) return BpfLevel::BASIC_4_9;
Chenbo Feng79b7e612018-12-11 12:24:23 -0800150
Maciej Żenczykowski1b5491d2019-11-15 00:54:05 +0000151 return BpfLevel::NONE;
Chenbo Feng75b410b2018-10-10 15:01:19 -0700152}
153
Maciej Żenczykowskifd90bc82020-02-10 19:27:47 -0800154BpfLevel getBpfSupportLevel() {
Maciej Żenczykowski8bd5f392020-02-11 11:13:18 -0800155 static BpfLevel cache = getUncachedBpfSupportLevel();
Maciej Żenczykowskifd90bc82020-02-10 19:27:47 -0800156 return cache;
157}
158
Chenbo Feng75b410b2018-10-10 15:01:19 -0700159} // namespace bpf
160} // namespace android