blob: d14c7fc33b76668960d178a36f4d9764a5b29605 [file] [log] [blame]
Joel Fernandes48452882018-12-17 13:47:54 -08001/*
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'Brien00fa9632022-01-14 15:13:20 -080017#include <android-base/file.h>
Joel Fernandes48452882018-12-17 13:47:54 -080018#include <android-base/macros.h>
19#include <gtest/gtest.h>
20#include <stdlib.h>
21#include <unistd.h>
22#include <iostream>
Ken Chenb1d48882021-10-25 21:11:22 +080023#include "bpf/BpfMap.h"
24#include "bpf/BpfUtils.h"
Joel Fernandes48452882018-12-17 13:47:54 -080025#include "include/libbpf_android.h"
26
Connor O'Brien50913142022-01-27 22:47:11 -080027using ::testing::TestWithParam;
Joel Fernandes48452882018-12-17 13:47:54 -080028
29namespace android {
30namespace bpf {
31
Connor O'Brien00fa9632022-01-14 15:13:20 -080032class BpfLoadTest : public TestWithParam<std::string> {
Joel Fernandes48452882018-12-17 13:47:54 -080033 protected:
34 BpfLoadTest() {}
Maciej Żenczykowskicd5c3022020-01-26 20:17:58 -080035 int mProgFd;
Connor O'Brien00fa9632022-01-14 15:13:20 -080036 std::string mTpProgPath;
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -080037 std::string mTpNeverLoadProgPath;
Connor O'Brien00fa9632022-01-14 15:13:20 -080038 std::string mTpMapPath;;
Joel Fernandes48452882018-12-17 13:47:54 -080039
40 void SetUp() {
Connor O'Brien50913142022-01-27 22:47:11 -080041 mTpProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_switch";
Connor O'Brien00fa9632022-01-14 15:13:20 -080042 unlink(mTpProgPath.c_str());
43
Connor O'Brien50913142022-01-27 22:47:11 -080044 mTpNeverLoadProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_wakeup";
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -080045 unlink(mTpNeverLoadProgPath.c_str());
46
Connor O'Brien50913142022-01-27 22:47:11 -080047 mTpMapPath = "/sys/fs/bpf/map_" + GetParam() + "_cpu_pid_map";
Connor O'Brien00fa9632022-01-14 15:13:20 -080048 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080049
Connor O'Brien50913142022-01-27 22:47:11 -080050 auto progPath = android::base::GetExecutableDirectory() + "/" + GetParam() + ".o";
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070051 bool critical = true;
Connor O'Brien50913142022-01-27 22:47:11 -080052 EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070053 EXPECT_EQ(false, critical);
Joel Fernandes48452882018-12-17 13:47:54 -080054
Connor O'Brien00fa9632022-01-14 15:13:20 -080055 mProgFd = bpf_obj_get(mTpProgPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080056 EXPECT_GT(mProgFd, 0);
57
Joel Fernandes48452882018-12-17 13:47:54 -080058 int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch");
59 EXPECT_NE(ret, 0);
60 }
61
62 void TearDown() {
Joel Fernandes48452882018-12-17 13:47:54 -080063 close(mProgFd);
Connor O'Brien00fa9632022-01-14 15:13:20 -080064 unlink(mTpProgPath.c_str());
65 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080066 }
67
68 void checkMapNonZero() {
69 // The test program installs a tracepoint on sched:sched_switch
70 // and expects the kernel to populate a PID corresponding to CPU
Connor O'Brien00fa9632022-01-14 15:13:20 -080071 android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080072
73 // Wait for program to run a little
74 sleep(1);
75
76 int non_zero = 0;
77 const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val,
78 BpfMap<uint32_t, uint32_t>& map) {
79 if (val && !non_zero) {
80 non_zero = 1;
81 }
82
83 UNUSED(key);
84 UNUSED(map);
Steven Morelande7cd2a72020-01-10 17:49:35 -080085 return base::Result<void>();
Joel Fernandes48452882018-12-17 13:47:54 -080086 };
87
Bernie Innocenti43d25382020-02-10 07:25:16 +090088 EXPECT_RESULT_OK(m.iterateWithValue(iterFunc));
Joel Fernandes48452882018-12-17 13:47:54 -080089 EXPECT_EQ(non_zero, 1);
90 }
Connor O'Brien35425e52022-01-18 21:41:16 -080091
92 void checkMapBtf() {
93 // Earlier kernels lack BPF_BTF_LOAD support
94 if (!isAtLeastKernelVersion(4, 19, 0)) GTEST_SKIP() << "pre-4.19 kernel does not support BTF";
95
96 const bool haveBtf = GetParam().find("btf") != std::string::npos;
97
98 std::string str;
99 EXPECT_EQ(android::base::ReadFileToString(mTpMapPath, &str), haveBtf);
100 if (haveBtf) EXPECT_FALSE(str.empty());
101 }
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800102
103 void checkKernelVersionEnforced() {
104 EXPECT_EQ(bpf_obj_get(mTpNeverLoadProgPath.c_str()), -1);
105 EXPECT_EQ(errno, ENOENT);
106 }
Joel Fernandes48452882018-12-17 13:47:54 -0800107};
108
Connor O'Brien00fa9632022-01-14 15:13:20 -0800109INSTANTIATE_TEST_SUITE_P(BpfLoadTests, BpfLoadTest,
Connor O'Brien50913142022-01-27 22:47:11 -0800110 ::testing::Values("bpf_load_tp_prog",
111 "bpf_load_tp_prog_btf"));
Connor O'Brien00fa9632022-01-14 15:13:20 -0800112
113TEST_P(BpfLoadTest, bpfCheckMap) {
Joel Fernandes48452882018-12-17 13:47:54 -0800114 checkMapNonZero();
115}
116
Connor O'Brien35425e52022-01-18 21:41:16 -0800117TEST_P(BpfLoadTest, bpfCheckBtf) {
118 checkMapBtf();
119}
120
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800121TEST_P(BpfLoadTest, bpfCheckMinKernelVersionEnforced) {
122 checkKernelVersionEnforced();
123}
124
Joel Fernandes48452882018-12-17 13:47:54 -0800125} // namespace bpf
126} // namespace android