blob: f3a8f851ce7789771868edc22fc68bd18ec62ffd [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>
Maciej Żenczykowskib44e2872023-06-12 23:10:52 -070020#include <libbpf.h>
Joel Fernandes48452882018-12-17 13:47:54 -080021#include <stdlib.h>
22#include <unistd.h>
23#include <iostream>
Ken Chenb1d48882021-10-25 21:11:22 +080024#include "bpf/BpfMap.h"
25#include "bpf/BpfUtils.h"
Joel Fernandes48452882018-12-17 13:47:54 -080026#include "include/libbpf_android.h"
27
Connor O'Brien50913142022-01-27 22:47:11 -080028using ::testing::TestWithParam;
Joel Fernandes48452882018-12-17 13:47:54 -080029
30namespace android {
31namespace bpf {
32
Connor O'Brien00fa9632022-01-14 15:13:20 -080033class BpfLoadTest : public TestWithParam<std::string> {
Joel Fernandes48452882018-12-17 13:47:54 -080034 protected:
35 BpfLoadTest() {}
Maciej Żenczykowskicd5c3022020-01-26 20:17:58 -080036 int mProgFd;
Connor O'Brien00fa9632022-01-14 15:13:20 -080037 std::string mTpProgPath;
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -080038 std::string mTpNeverLoadProgPath;
Connor O'Brien00fa9632022-01-14 15:13:20 -080039 std::string mTpMapPath;;
Joel Fernandes48452882018-12-17 13:47:54 -080040
41 void SetUp() {
Connor O'Brien50913142022-01-27 22:47:11 -080042 mTpProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_switch";
Connor O'Brien00fa9632022-01-14 15:13:20 -080043 unlink(mTpProgPath.c_str());
44
Connor O'Brien50913142022-01-27 22:47:11 -080045 mTpNeverLoadProgPath = "/sys/fs/bpf/prog_" + GetParam() + "_tracepoint_sched_sched_wakeup";
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -080046 unlink(mTpNeverLoadProgPath.c_str());
47
Connor O'Brien50913142022-01-27 22:47:11 -080048 mTpMapPath = "/sys/fs/bpf/map_" + GetParam() + "_cpu_pid_map";
Connor O'Brien00fa9632022-01-14 15:13:20 -080049 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080050
Connor O'Brien50913142022-01-27 22:47:11 -080051 auto progPath = android::base::GetExecutableDirectory() + "/" + GetParam() + ".o";
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070052 bool critical = true;
Steven Moreland0f10f3f2019-12-12 14:22:34 -080053
54 bpf_prog_type kAllowed[] = {
55 BPF_PROG_TYPE_UNSPEC,
56 };
Connor O'Brien6c0ce9f2022-12-01 20:08:17 -080057
58 Location loc = {
59 .dir = "",
60 .prefix = "",
61 .allowedDomainBitmask = 0,
62 .allowedProgTypes = kAllowed,
63 .allowedProgTypesLength = arraysize(kAllowed),
64 };
65 EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, loc), -1);
Steven Moreland0f10f3f2019-12-12 14:22:34 -080066
Connor O'Brien50913142022-01-27 22:47:11 -080067 EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070068 EXPECT_EQ(false, critical);
Joel Fernandes48452882018-12-17 13:47:54 -080069
Maciej Żenczykowskib44e2872023-06-12 23:10:52 -070070 mProgFd = retrieveProgram(mTpProgPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080071 EXPECT_GT(mProgFd, 0);
72
Joel Fernandes48452882018-12-17 13:47:54 -080073 int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch");
74 EXPECT_NE(ret, 0);
75 }
76
77 void TearDown() {
Joel Fernandes48452882018-12-17 13:47:54 -080078 close(mProgFd);
Connor O'Brien00fa9632022-01-14 15:13:20 -080079 unlink(mTpProgPath.c_str());
80 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080081 }
82
83 void checkMapNonZero() {
84 // The test program installs a tracepoint on sched:sched_switch
85 // and expects the kernel to populate a PID corresponding to CPU
Connor O'Brien00fa9632022-01-14 15:13:20 -080086 android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080087
88 // Wait for program to run a little
89 sleep(1);
90
91 int non_zero = 0;
92 const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val,
93 BpfMap<uint32_t, uint32_t>& map) {
94 if (val && !non_zero) {
95 non_zero = 1;
96 }
97
98 UNUSED(key);
99 UNUSED(map);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800100 return base::Result<void>();
Joel Fernandes48452882018-12-17 13:47:54 -0800101 };
102
Bernie Innocenti43d25382020-02-10 07:25:16 +0900103 EXPECT_RESULT_OK(m.iterateWithValue(iterFunc));
Joel Fernandes48452882018-12-17 13:47:54 -0800104 EXPECT_EQ(non_zero, 1);
105 }
Connor O'Brien35425e52022-01-18 21:41:16 -0800106
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800107 void checkKernelVersionEnforced() {
Maciej Żenczykowskib44e2872023-06-12 23:10:52 -0700108 EXPECT_EQ(retrieveProgram(mTpNeverLoadProgPath.c_str()), -1);
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800109 EXPECT_EQ(errno, ENOENT);
110 }
Joel Fernandes48452882018-12-17 13:47:54 -0800111};
112
Maciej Żenczykowskib44e2872023-06-12 23:10:52 -0700113INSTANTIATE_TEST_SUITE_P(BpfLoadTests, BpfLoadTest, ::testing::Values("bpfLoadTpProg"));
Connor O'Brien00fa9632022-01-14 15:13:20 -0800114
115TEST_P(BpfLoadTest, bpfCheckMap) {
Joel Fernandes48452882018-12-17 13:47:54 -0800116 checkMapNonZero();
117}
118
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800119TEST_P(BpfLoadTest, bpfCheckMinKernelVersionEnforced) {
120 checkKernelVersionEnforced();
121}
122
Joel Fernandes48452882018-12-17 13:47:54 -0800123} // namespace bpf
124} // namespace android