blob: 3fcdaa46d55646ef89ea33a196a93cd8fc7d23b5 [file] [log] [blame]
Patrick Rohrbdafd742022-02-23 19:29:52 +01001/*
2 * Copyright (C) 2022 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
18#include <gtest/gtest.h>
19#include <vintf/VintfObject.h>
20
21#include <fstream>
22#include <string>
23
Maciej Żenczykowski099e1962023-05-04 11:16:26 +000024#include "bpf/KernelUtils.h"
25
Patrick Rohrbdafd742022-02-23 19:29:52 +010026namespace android {
27namespace net {
28
29namespace {
30
31using ::android::vintf::RuntimeInfo;
32using ::android::vintf::VintfObject;
33
34class KernelConfigVerifier final {
35 public:
36 KernelConfigVerifier() : mRuntimeInfo(VintfObject::GetRuntimeInfo()) {}
37
38 bool hasOption(const std::string& option) const {
39 const auto& configMap = mRuntimeInfo->kernelConfigs();
40 auto it = configMap.find(option);
41 if (it != configMap.cend()) {
42 return it->second == "y";
43 }
44 return false;
45 }
46
47 private:
48 std::shared_ptr<const RuntimeInfo> mRuntimeInfo;
49};
50
51} // namespace
52
53/**
54 * If this test fails, enable the following kernel modules in your kernel config:
55 * CONFIG_NET_CLS_MATCHALL=y
56 * CONFIG_NET_ACT_POLICE=y
57 * CONFIG_NET_ACT_BPF=y
Maciej Żenczykowskib5f98e62022-12-16 20:29:31 +000058 * CONFIG_BPF_JIT=y
Patrick Rohrbdafd742022-02-23 19:29:52 +010059 */
60TEST(KernelTest, TestRateLimitingSupport) {
Patrick Rohrbdafd742022-02-23 19:29:52 +010061 KernelConfigVerifier configVerifier;
62 ASSERT_TRUE(configVerifier.hasOption("CONFIG_NET_CLS_MATCHALL"));
63 ASSERT_TRUE(configVerifier.hasOption("CONFIG_NET_ACT_POLICE"));
64 ASSERT_TRUE(configVerifier.hasOption("CONFIG_NET_ACT_BPF"));
Maciej Żenczykowskib5f98e62022-12-16 20:29:31 +000065 ASSERT_TRUE(configVerifier.hasOption("CONFIG_BPF_JIT"));
Patrick Rohrbdafd742022-02-23 19:29:52 +010066}
67
Maciej Żenczykowski099e1962023-05-04 11:16:26 +000068TEST(KernelTest, TestBpfJitAlwaysOn) {
69 // 32-bit arm & x86 kernels aren't capable of JIT-ing all of our BPF code,
70 if (bpf::isKernel32Bit()) GTEST_SKIP() << "Exempt on 32-bit kernel.";
71 KernelConfigVerifier configVerifier;
72 ASSERT_TRUE(configVerifier.hasOption("CONFIG_BPF_JIT_ALWAYS_ON"));
73}
74
75/* Android 14/U should only launch on 64-bit kernels
76 * T launches on 5.10/5.15
77 * U launches on 5.15/6.1
78 * So >=5.16 implies isKernel64Bit()
79 */
80TEST(KernelTest, TestKernel64Bit) {
81 if (!bpf::isAtLeastKernelVersion(5, 16, 0)) GTEST_SKIP() << "Exempt on < 5.16 kernel.";
82 ASSERT_TRUE(bpf::isKernel64Bit());
83}
84
Maciej Żenczykowski863804d2023-08-09 21:59:43 +000085// Android V requires 4.19+
86TEST(KernelTest, TestKernel419) {
87 ASSERT_TRUE(bpf::isAtLeastKernelVersion(4, 19, 0));
88}
89
Patrick Rohrbdafd742022-02-23 19:29:52 +010090} // namespace net
91} // namespace android