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> |
Maciej Żenczykowski | b44e287 | 2023-06-12 23:10:52 -0700 | [diff] [blame] | 20 | #include <libbpf.h> |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <unistd.h> |
| 23 | #include <iostream> |
Ken Chen | b1d4888 | 2021-10-25 21:11:22 +0800 | [diff] [blame] | 24 | #include "bpf/BpfMap.h" |
| 25 | #include "bpf/BpfUtils.h" |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 26 | #include "include/libbpf_android.h" |
| 27 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 28 | using ::testing::TestWithParam; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace bpf { |
| 32 | |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 33 | class BpfLoadTest : public TestWithParam<std::string> { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 34 | protected: |
| 35 | BpfLoadTest() {} |
Maciej Żenczykowski | cd5c302 | 2020-01-26 20:17:58 -0800 | [diff] [blame] | 36 | int mProgFd; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 37 | std::string mTpProgPath; |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 38 | std::string mTpNeverLoadProgPath; |
Neill Kapron | bc4ae41 | 2024-04-09 17:04:25 +0000 | [diff] [blame] | 39 | std::string mTpMapPath; |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 40 | |
| 41 | void SetUp() { |
Neill Kapron | bc4ae41 | 2024-04-09 17:04:25 +0000 | [diff] [blame] | 42 | /* |
| 43 | * b/326156952 |
| 44 | * |
| 45 | * Kernels prior to 5.11 used rlimit memlock accounting for bpf memory |
| 46 | * allocations, and therefore require increasing the rlimit of this |
| 47 | * process for the maps to be created successfully. |
| 48 | * |
| 49 | * 5.11 introduces cgroup-based accounting as discussed here: |
| 50 | * https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/ |
| 51 | */ |
| 52 | if (!isAtLeastKernelVersion(5, 11, 0)) EXPECT_EQ(setrlimitForTest(), 0); |
| 53 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 54 | mTpProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_switch"; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 55 | unlink(mTpProgPath.c_str()); |
| 56 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 57 | mTpNeverLoadProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_wakeup"; |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 58 | unlink(mTpNeverLoadProgPath.c_str()); |
| 59 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 60 | mTpMapPath = "/sys/fs/bpf/map_" + GetParam() + "_cpu_pid_map"; |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 61 | unlink(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 62 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 63 | auto progPath = android::base::GetExecutableDirectory() + "/" + GetParam() + ".o"; |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 64 | bool critical = true; |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 65 | |
| 66 | bpf_prog_type kAllowed[] = { |
| 67 | BPF_PROG_TYPE_UNSPEC, |
| 68 | }; |
Connor O'Brien | 6c0ce9f | 2022-12-01 20:08:17 -0800 | [diff] [blame] | 69 | |
| 70 | Location loc = { |
| 71 | .dir = "", |
| 72 | .prefix = "", |
Connor O'Brien | 6c0ce9f | 2022-12-01 20:08:17 -0800 | [diff] [blame] | 73 | .allowedProgTypes = kAllowed, |
| 74 | .allowedProgTypesLength = arraysize(kAllowed), |
| 75 | }; |
| 76 | EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, loc), -1); |
Steven Moreland | 0f10f3f | 2019-12-12 14:22:34 -0800 | [diff] [blame] | 77 | |
Connor O'Brien | 5091314 | 2022-01-27 22:47:11 -0800 | [diff] [blame] | 78 | EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0); |
Maciej Żenczykowski | 89515d9 | 2020-06-14 19:27:33 -0700 | [diff] [blame] | 79 | EXPECT_EQ(false, critical); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 80 | |
Maciej Żenczykowski | b44e287 | 2023-06-12 23:10:52 -0700 | [diff] [blame] | 81 | mProgFd = retrieveProgram(mTpProgPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 82 | EXPECT_GT(mProgFd, 0); |
| 83 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 84 | int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch"); |
| 85 | EXPECT_NE(ret, 0); |
| 86 | } |
| 87 | |
| 88 | void TearDown() { |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 89 | close(mProgFd); |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 90 | unlink(mTpProgPath.c_str()); |
| 91 | unlink(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void checkMapNonZero() { |
| 95 | // The test program installs a tracepoint on sched:sched_switch |
| 96 | // and expects the kernel to populate a PID corresponding to CPU |
Connor O'Brien | 00fa963 | 2022-01-14 15:13:20 -0800 | [diff] [blame] | 97 | android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str()); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 98 | |
| 99 | // Wait for program to run a little |
| 100 | sleep(1); |
| 101 | |
| 102 | int non_zero = 0; |
| 103 | const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val, |
| 104 | BpfMap<uint32_t, uint32_t>& map) { |
| 105 | if (val && !non_zero) { |
| 106 | non_zero = 1; |
| 107 | } |
| 108 | |
| 109 | UNUSED(key); |
| 110 | UNUSED(map); |
Steven Moreland | e7cd2a7 | 2020-01-10 17:49:35 -0800 | [diff] [blame] | 111 | return base::Result<void>(); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 112 | }; |
| 113 | |
Bernie Innocenti | 43d2538 | 2020-02-10 07:25:16 +0900 | [diff] [blame] | 114 | EXPECT_RESULT_OK(m.iterateWithValue(iterFunc)); |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 115 | EXPECT_EQ(non_zero, 1); |
| 116 | } |
Connor O'Brien | 35425e5 | 2022-01-18 21:41:16 -0800 | [diff] [blame] | 117 | |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 118 | void checkKernelVersionEnforced() { |
Maciej Żenczykowski | b44e287 | 2023-06-12 23:10:52 -0700 | [diff] [blame] | 119 | EXPECT_EQ(retrieveProgram(mTpNeverLoadProgPath.c_str()), -1); |
Connor O'Brien | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 120 | EXPECT_EQ(errno, ENOENT); |
| 121 | } |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 122 | }; |
| 123 | |
Maciej Żenczykowski | b44e287 | 2023-06-12 23:10:52 -0700 | [diff] [blame] | 124 | INSTANTIATE_TEST_SUITE_P(BpfLoadTests, BpfLoadTest, ::testing::Values("bpfLoadTpProg")); |
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 | 0ea4c6b | 2022-01-14 00:18:11 -0800 | [diff] [blame] | 130 | TEST_P(BpfLoadTest, bpfCheckMinKernelVersionEnforced) { |
| 131 | checkKernelVersionEnforced(); |
| 132 | } |
| 133 | |
Joel Fernandes | 4845288 | 2018-12-17 13:47:54 -0800 | [diff] [blame] | 134 | } // namespace bpf |
| 135 | } // namespace android |