Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 17 | #include <android-base/file.h> |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 18 | #include <android-base/macros.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <unistd.h> |
| 22 | #include <iostream> |
Ken Chen | b1d4888 | 2021-10-25 21:11:22 +0800 | [diff] [blame] | 23 | #include "bpf/BpfMap.h" |
| 24 | #include "bpf/BpfUtils.h" |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 25 | #include "include/libbpf_android.h" |
| 26 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 27 | using ::testing::TestWithParam; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | namespace bpf { |
| 31 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 32 | class BpfLoadTest : public TestWithParam<std::string> { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 33 | protected: |
| 34 | BpfLoadTest() {} |
Maciej Żenczykowski | cd5c302 | 2020-01-26 20:17:58 -0800 | [diff] [blame] | 35 | int mProgFd; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 36 | std::string mTpProgPath; |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 37 | std::string mTpNeverLoadProgPath; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 38 | std::string mTpMapPath;; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 39 | |
| 40 | void SetUp() { |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 41 | mTpProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_switch"; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 42 | unlink(mTpProgPath.c_str()); |
| 43 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 44 | mTpNeverLoadProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_wakeup"; |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 45 | unlink(mTpNeverLoadProgPath.c_str()); |
| 46 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 47 | mTpMapPath = "/sys/fs/bpf/map_" + GetParam() + "_cpu_pid_map"; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 48 | unlink(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 49 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 50 | auto progPath = android::base::GetExecutableDirectory() + "/" + GetParam() + ".o"; |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 51 | bool critical = true; |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 52 | |
| 53 | bpf_prog_type kAllowed[] = { |
| 54 | BPF_PROG_TYPE_UNSPEC, |
| 55 | }; |
Connor O'Brien | 6c0ce9f | 2022-12-01 20:08:17 -0800 | [diff] [blame] | 56 | |
| 57 | Location loc = { |
| 58 | .dir = "", |
| 59 | .prefix = "", |
| 60 | .allowedDomainBitmask = 0, |
| 61 | .allowedProgTypes = kAllowed, |
| 62 | .allowedProgTypesLength = arraysize(kAllowed), |
| 63 | }; |
| 64 | EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, loc), -1); |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 65 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 66 | EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 67 | EXPECT_EQ(false, critical); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 68 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 69 | mProgFd = bpf_obj_get(mTpProgPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 70 | EXPECT_GT(mProgFd, 0); |
| 71 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 72 | int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch"); |
| 73 | EXPECT_NE(ret, 0); |
| 74 | } |
| 75 | |
| 76 | void TearDown() { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 77 | close(mProgFd); |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 78 | unlink(mTpProgPath.c_str()); |
| 79 | unlink(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void checkMapNonZero() { |
| 83 | // The test program installs a tracepoint on sched:sched_switch |
| 84 | // and expects the kernel to populate a PID corresponding to CPU |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 85 | android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 86 | |
| 87 | // Wait for program to run a little |
| 88 | sleep(1); |
| 89 | |
| 90 | int non_zero = 0; |
| 91 | const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val, |
| 92 | BpfMap<uint32_t, uint32_t>& map) { |
| 93 | if (val && !non_zero) { |
| 94 | non_zero = 1; |
| 95 | } |
| 96 | |
| 97 | UNUSED(key); |
| 98 | UNUSED(map); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 99 | return base::Result<void>(); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
Bernie Innocenti | 43d2538 | 2020-02-10 07:25:16 +0900 | [diff] [blame] | 102 | EXPECT_RESULT_OK(m.iterateWithValue(iterFunc)); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 103 | EXPECT_EQ(non_zero, 1); |
| 104 | } |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 105 | |
| 106 | void checkMapBtf() { |
| 107 | // Earlier kernels lack BPF_BTF_LOAD support |
| 108 | if (!isAtLeastKernelVersion(4, 19, 0)) GTEST_SKIP() << "pre-4.19 kernel does not support BTF"; |
| 109 | |
Ken Chen | 8693c78 | 2022-07-09 14:22:38 +0800 | [diff] [blame] | 110 | const bool haveBtf = GetParam().find("Btf") != std::string::npos; |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 111 | |
| 112 | std::string str; |
| 113 | EXPECT_EQ(android::base::ReadFileToString(mTpMapPath, &str), haveBtf); |
| 114 | if (haveBtf) EXPECT_FALSE(str.empty()); |
| 115 | } |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 116 | |
| 117 | void checkKernelVersionEnforced() { |
| 118 | EXPECT_EQ(bpf_obj_get(mTpNeverLoadProgPath.c_str()), -1); |
| 119 | EXPECT_EQ(errno, ENOENT); |
| 120 | } |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 123 | INSTANTIATE_TEST_SUITE_P(BpfLoadTests, BpfLoadTest, |
Ken Chen | 8693c78 | 2022-07-09 14:22:38 +0800 | [diff] [blame] | 124 | ::testing::Values("bpfLoadTpProg", "bpfLoadTpProgBtf")); |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 125 | |
| 126 | TEST_P(BpfLoadTest, bpfCheckMap) { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 127 | checkMapNonZero(); |
| 128 | } |
| 129 | |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 130 | TEST_P(BpfLoadTest, bpfCheckBtf) { |
| 131 | checkMapBtf(); |
| 132 | } |
| 133 | |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 134 | TEST_P(BpfLoadTest, bpfCheckMinKernelVersionEnforced) { |
| 135 | checkKernelVersionEnforced(); |
| 136 | } |
| 137 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 138 | } // namespace bpf |
| 139 | } // namespace android |