blob: 8e853b9ea3af46a72cf66657080afc69857c2f11 [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;
Steven Moreland0f10f3f2019-12-12 14:22:34 -080052
53 bpf_prog_type kAllowed[] = {
54 BPF_PROG_TYPE_UNSPEC,
55 };
Maciej Żenczykowski41817132022-06-03 08:41:04 -070056 EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, "", 0, kAllowed,
Steven Moreland0f10f3f2019-12-12 14:22:34 -080057 arraysize(kAllowed)),
58 -1);
59
Connor O'Brien50913142022-01-27 22:47:11 -080060 EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070061 EXPECT_EQ(false, critical);
Joel Fernandes48452882018-12-17 13:47:54 -080062
Connor O'Brien00fa9632022-01-14 15:13:20 -080063 mProgFd = bpf_obj_get(mTpProgPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080064 EXPECT_GT(mProgFd, 0);
65
Joel Fernandes48452882018-12-17 13:47:54 -080066 int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch");
67 EXPECT_NE(ret, 0);
68 }
69
70 void TearDown() {
Joel Fernandes48452882018-12-17 13:47:54 -080071 close(mProgFd);
Connor O'Brien00fa9632022-01-14 15:13:20 -080072 unlink(mTpProgPath.c_str());
73 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080074 }
75
76 void checkMapNonZero() {
77 // The test program installs a tracepoint on sched:sched_switch
78 // and expects the kernel to populate a PID corresponding to CPU
Connor O'Brien00fa9632022-01-14 15:13:20 -080079 android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080080
81 // Wait for program to run a little
82 sleep(1);
83
84 int non_zero = 0;
85 const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val,
86 BpfMap<uint32_t, uint32_t>& map) {
87 if (val && !non_zero) {
88 non_zero = 1;
89 }
90
91 UNUSED(key);
92 UNUSED(map);
Steven Morelande7cd2a72020-01-10 17:49:35 -080093 return base::Result<void>();
Joel Fernandes48452882018-12-17 13:47:54 -080094 };
95
Bernie Innocenti43d25382020-02-10 07:25:16 +090096 EXPECT_RESULT_OK(m.iterateWithValue(iterFunc));
Joel Fernandes48452882018-12-17 13:47:54 -080097 EXPECT_EQ(non_zero, 1);
98 }
Connor O'Brien35425e52022-01-18 21:41:16 -080099
100 void checkMapBtf() {
101 // Earlier kernels lack BPF_BTF_LOAD support
102 if (!isAtLeastKernelVersion(4, 19, 0)) GTEST_SKIP() << "pre-4.19 kernel does not support BTF";
103
Ken Chen8693c782022-07-09 14:22:38 +0800104 const bool haveBtf = GetParam().find("Btf") != std::string::npos;
Connor O'Brien35425e52022-01-18 21:41:16 -0800105
106 std::string str;
107 EXPECT_EQ(android::base::ReadFileToString(mTpMapPath, &str), haveBtf);
108 if (haveBtf) EXPECT_FALSE(str.empty());
109 }
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800110
111 void checkKernelVersionEnforced() {
112 EXPECT_EQ(bpf_obj_get(mTpNeverLoadProgPath.c_str()), -1);
113 EXPECT_EQ(errno, ENOENT);
114 }
Joel Fernandes48452882018-12-17 13:47:54 -0800115};
116
Connor O'Brien00fa9632022-01-14 15:13:20 -0800117INSTANTIATE_TEST_SUITE_P(BpfLoadTests, BpfLoadTest,
Ken Chen8693c782022-07-09 14:22:38 +0800118 ::testing::Values("bpfLoadTpProg", "bpfLoadTpProgBtf"));
Connor O'Brien00fa9632022-01-14 15:13:20 -0800119
120TEST_P(BpfLoadTest, bpfCheckMap) {
Joel Fernandes48452882018-12-17 13:47:54 -0800121 checkMapNonZero();
122}
123
Connor O'Brien35425e52022-01-18 21:41:16 -0800124TEST_P(BpfLoadTest, bpfCheckBtf) {
125 checkMapBtf();
126}
127
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800128TEST_P(BpfLoadTest, bpfCheckMinKernelVersionEnforced) {
129 checkKernelVersionEnforced();
130}
131
Joel Fernandes48452882018-12-17 13:47:54 -0800132} // namespace bpf
133} // namespace android