blob: 4ec89be69a6c79700c907191555423d6161a06d4 [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
Joel Fernandes48452882018-12-17 13:47:54 -080028namespace android {
29namespace bpf {
30
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -070031class BpfLoadTest : public ::testing::Test {
Joel Fernandes48452882018-12-17 13:47:54 -080032 protected:
33 BpfLoadTest() {}
Maciej Żenczykowskicd5c3022020-01-26 20:17:58 -080034 int mProgFd;
Connor O'Brien00fa9632022-01-14 15:13:20 -080035 std::string mTpProgPath;
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -080036 std::string mTpNeverLoadProgPath;
Neill Kapronbc4ae412024-04-09 17:04:25 +000037 std::string mTpMapPath;
Joel Fernandes48452882018-12-17 13:47:54 -080038
39 void SetUp() {
Neill Kapronbc4ae412024-04-09 17:04:25 +000040 /*
41 * b/326156952
42 *
43 * Kernels prior to 5.11 used rlimit memlock accounting for bpf memory
44 * allocations, and therefore require increasing the rlimit of this
45 * process for the maps to be created successfully.
46 *
47 * 5.11 introduces cgroup-based accounting as discussed here:
48 * https://lore.kernel.org/bpf/20201201215900.3569844-1-guro@fb.com/
49 */
50 if (!isAtLeastKernelVersion(5, 11, 0)) EXPECT_EQ(setrlimitForTest(), 0);
51
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -070052 mTpProgPath = "/sys/fs/bpf/prog_bpfLoadTpProg_tracepoint_sched_sched_switch";
Connor O'Brien00fa9632022-01-14 15:13:20 -080053 unlink(mTpProgPath.c_str());
54
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -070055 mTpNeverLoadProgPath = "/sys/fs/bpf/prog_bpfLoadTpProg_tracepoint_sched_sched_wakeup";
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -080056 unlink(mTpNeverLoadProgPath.c_str());
57
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -070058 mTpMapPath = "/sys/fs/bpf/map_bpfLoadTpProg_cpu_pid_map";
Connor O'Brien00fa9632022-01-14 15:13:20 -080059 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080060
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -070061 auto progPath = android::base::GetExecutableDirectory() + "/bpfLoadTpProg.o";
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070062 bool critical = true;
Steven Moreland0f10f3f2019-12-12 14:22:34 -080063
64 bpf_prog_type kAllowed[] = {
65 BPF_PROG_TYPE_UNSPEC,
66 };
Connor O'Brien6c0ce9f2022-12-01 20:08:17 -080067
68 Location loc = {
69 .dir = "",
70 .prefix = "",
Connor O'Brien6c0ce9f2022-12-01 20:08:17 -080071 .allowedProgTypes = kAllowed,
72 .allowedProgTypesLength = arraysize(kAllowed),
73 };
74 EXPECT_EQ(android::bpf::loadProg(progPath.c_str(), &critical, loc), -1);
Steven Moreland0f10f3f2019-12-12 14:22:34 -080075
Maciej Żenczykowskif381ae92024-09-17 00:03:56 +000076 ASSERT_EQ(android::bpf::loadProg(progPath.c_str(), &critical), 0);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -070077 EXPECT_EQ(false, critical);
Joel Fernandes48452882018-12-17 13:47:54 -080078
Maciej Żenczykowskib44e2872023-06-12 23:10:52 -070079 mProgFd = retrieveProgram(mTpProgPath.c_str());
Maciej Żenczykowskif381ae92024-09-17 00:03:56 +000080 ASSERT_GT(mProgFd, 0);
Joel Fernandes48452882018-12-17 13:47:54 -080081
Joel Fernandes48452882018-12-17 13:47:54 -080082 int ret = bpf_attach_tracepoint(mProgFd, "sched", "sched_switch");
83 EXPECT_NE(ret, 0);
84 }
85
86 void TearDown() {
Joel Fernandes48452882018-12-17 13:47:54 -080087 close(mProgFd);
Connor O'Brien00fa9632022-01-14 15:13:20 -080088 unlink(mTpProgPath.c_str());
89 unlink(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080090 }
91
92 void checkMapNonZero() {
93 // The test program installs a tracepoint on sched:sched_switch
94 // and expects the kernel to populate a PID corresponding to CPU
Connor O'Brien00fa9632022-01-14 15:13:20 -080095 android::bpf::BpfMap<uint32_t, uint32_t> m(mTpMapPath.c_str());
Joel Fernandes48452882018-12-17 13:47:54 -080096
97 // Wait for program to run a little
98 sleep(1);
99
100 int non_zero = 0;
101 const auto iterFunc = [&non_zero](const uint32_t& key, const uint32_t& val,
102 BpfMap<uint32_t, uint32_t>& map) {
103 if (val && !non_zero) {
104 non_zero = 1;
105 }
106
107 UNUSED(key);
108 UNUSED(map);
Steven Morelande7cd2a72020-01-10 17:49:35 -0800109 return base::Result<void>();
Joel Fernandes48452882018-12-17 13:47:54 -0800110 };
111
Bernie Innocenti43d25382020-02-10 07:25:16 +0900112 EXPECT_RESULT_OK(m.iterateWithValue(iterFunc));
Joel Fernandes48452882018-12-17 13:47:54 -0800113 EXPECT_EQ(non_zero, 1);
114 }
Connor O'Brien35425e52022-01-18 21:41:16 -0800115
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800116 void checkKernelVersionEnforced() {
Maciej Żenczykowskif381ae92024-09-17 00:03:56 +0000117 ASSERT_EQ(retrieveProgram(mTpNeverLoadProgPath.c_str()), -1);
118 ASSERT_EQ(errno, ENOENT);
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800119 }
Joel Fernandes48452882018-12-17 13:47:54 -0800120};
121
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -0700122TEST_F(BpfLoadTest, bpfCheckMap) {
Joel Fernandes48452882018-12-17 13:47:54 -0800123 checkMapNonZero();
124}
125
Maciej Żenczykowskifee8f172024-08-02 10:34:20 -0700126TEST_F(BpfLoadTest, bpfCheckMinKernelVersionEnforced) {
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800127 checkKernelVersionEnforced();
128}
129
Joel Fernandes48452882018-12-17 13:47:54 -0800130} // namespace bpf
131} // namespace android