| Patrick Rohr | bdafd74 | 2022-02-23 19:29:52 +0100 | [diff] [blame] | 1 | /* |
| 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 Żenczykowski | 099e196 | 2023-05-04 11:16:26 +0000 | [diff] [blame] | 24 | #include "bpf/KernelUtils.h" |
| 25 | |
| Patrick Rohr | bdafd74 | 2022-02-23 19:29:52 +0100 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace net { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | using ::android::vintf::RuntimeInfo; |
| 32 | using ::android::vintf::VintfObject; |
| 33 | |
| 34 | class 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 Żenczykowski | b5f98e6 | 2022-12-16 20:29:31 +0000 | [diff] [blame] | 58 | * CONFIG_BPF_JIT=y |
| Patrick Rohr | bdafd74 | 2022-02-23 19:29:52 +0100 | [diff] [blame] | 59 | */ |
| 60 | TEST(KernelTest, TestRateLimitingSupport) { |
| Patrick Rohr | bdafd74 | 2022-02-23 19:29:52 +0100 | [diff] [blame] | 61 | 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 Żenczykowski | b5f98e6 | 2022-12-16 20:29:31 +0000 | [diff] [blame] | 65 | ASSERT_TRUE(configVerifier.hasOption("CONFIG_BPF_JIT")); |
| Patrick Rohr | bdafd74 | 2022-02-23 19:29:52 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| Maciej Żenczykowski | 099e196 | 2023-05-04 11:16:26 +0000 | [diff] [blame] | 68 | TEST(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 | */ |
| 80 | TEST(KernelTest, TestKernel64Bit) { |
| 81 | if (!bpf::isAtLeastKernelVersion(5, 16, 0)) GTEST_SKIP() << "Exempt on < 5.16 kernel."; |
| 82 | ASSERT_TRUE(bpf::isKernel64Bit()); |
| 83 | } |
| 84 | |
| Maciej Żenczykowski | 863804d | 2023-08-09 21:59:43 +0000 | [diff] [blame^] | 85 | // Android V requires 4.19+ |
| 86 | TEST(KernelTest, TestKernel419) { |
| 87 | ASSERT_TRUE(bpf::isAtLeastKernelVersion(4, 19, 0)); |
| 88 | } |
| 89 | |
| Patrick Rohr | bdafd74 | 2022-02-23 19:29:52 +0100 | [diff] [blame] | 90 | } // namespace net |
| 91 | } // namespace android |